Skip to content

Commit

Permalink
lazy load these requires so that node:cluster imports faster (#14291)
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro authored Oct 2, 2024
1 parent 25083a4 commit b8a2a11
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/js/internal/cluster/RoundRobinHandle.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion src/js/internal/cluster/primary.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b8a2a11

Please sign in to comment.