From b8a2a11c6fdb43c9769032bb4efab5ec27df1e58 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Wed, 2 Oct 2024 02:30:19 -0700 Subject: [PATCH] lazy load these requires so that node:cluster imports faster (#14291) --- src/js/internal/cluster/RoundRobinHandle.ts | 4 +++- src/js/internal/cluster/primary.ts | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/js/internal/cluster/RoundRobinHandle.ts b/src/js/internal/cluster/RoundRobinHandle.ts index 2184cc04680b17..53305e9336062c 100644 --- a/src/js/internal/cluster/RoundRobinHandle.ts +++ b/src/js/internal/cluster/RoundRobinHandle.ts @@ -1,7 +1,8 @@ -const net = require("node:net"); const { append, init, isEmpty, peek, remove } = require("internal/linkedlist"); const { kHandle } = require("internal/shared"); +let net; + const sendHelper = $newZigFunction("node_cluster_binding.zig", "sendHelperPrimary", 4); const ArrayIsArray = Array.isArray; @@ -20,6 +21,7 @@ export default class RoundRobinHandle { server; constructor(key, address, { port, fd, flags, backlog, readableAll, writableAll }) { + net ??= require("node:net"); this.key = key; this.all = new Map(); this.free = new Map(); diff --git a/src/js/internal/cluster/primary.ts b/src/js/internal/cluster/primary.ts index e3a0d4f84492b9..315d13e0a654b5 100644 --- a/src/js/internal/cluster/primary.ts +++ b/src/js/internal/cluster/primary.ts @@ -1,5 +1,4 @@ const EventEmitter = require("node:events"); -const child_process = require("node:child_process"); const Worker = require("internal/cluster/Worker"); const RoundRobinHandle = require("internal/cluster/RoundRobinHandle"); const path = require("node:path"); @@ -8,6 +7,8 @@ const { throwNotImplemented, kHandle } = require("internal/shared"); const sendHelper = $newZigFunction("node_cluster_binding.zig", "sendHelperPrimary", 4); const onInternalMessage = $newZigFunction("node_cluster_binding.zig", "onInternalMessagePrimary", 3); +let child_process; + const ArrayPrototypeSlice = Array.prototype.slice; const ObjectValues = Object.values; const ObjectKeys = Object.keys; @@ -86,6 +87,7 @@ function createWorkerProcess(id, env) { // ArrayPrototypePush(execArgv, `--inspect-port=${getInspectPort(cluster.settings.inspectPort)}`); // } + child_process ??= require("node:child_process"); return child_process.fork(cluster.settings.exec, cluster.settings.args, { cwd: cluster.settings.cwd, env: workerEnv,