chore: update cluster shim

This commit is contained in:
Dorian Niemiec 2024-10-19 09:10:49 +02:00
parent 7895085922
commit 6b1df975d7
2 changed files with 18 additions and 18 deletions

View file

@ -1,4 +1,4 @@
const cluster = require("./utils/clusterBunShim.js"); // Cluster shim for Bun const cluster = require("./utils/clusterShim.js"); // Cluster shim for Bun and Deno
const { add } = require("./utils/helper.js"); // Require the addition module const { add } = require("./utils/helper.js"); // Require the addition module
const modInfo = require("../modInfo.json"); // SVR.JS mod information const modInfo = require("../modInfo.json"); // SVR.JS mod information

View file

@ -13,9 +13,9 @@ if (!process.singleThreaded) {
// Clustering is not supported! // Clustering is not supported!
} }
// Cluster & IPC shim for Bun // Cluster & IPC shim for Bun and Deno
cluster.bunShim = () => { cluster.shim = () => {
cluster.isMaster = !process.env.NODE_UNIQUE_ID; cluster.isMaster = !process.env.NODE_UNIQUE_ID;
cluster.isPrimary = cluster.isMaster; cluster.isPrimary = cluster.isMaster;
cluster.isWorker = !cluster.isMaster; cluster.isWorker = !cluster.isMaster;
@ -29,7 +29,7 @@ if (!process.singleThreaded) {
isDead: () => false, isDead: () => false,
send: (message, ...params) => { send: (message, ...params) => {
process.send(message, ...params); process.send(message, ...params);
}, }
}; };
if (!process.send) { if (!process.send) {
@ -52,9 +52,9 @@ if (!process.singleThreaded) {
? path.join( ? path.join(
"\\\\?\\pipe", "\\\\?\\pipe",
process.dirname, process.dirname,
"temp/.W" + process.pid + ".ipc", "temp/.W" + process.pid + ".ipc"
) )
: process.dirname + "/temp/.W" + process.pid + ".ipc", : process.dirname + "/temp/.W" + process.pid + ".ipc"
); );
process.send = (message) => { process.send = (message) => {
@ -64,12 +64,12 @@ if (!process.singleThreaded) {
? path.join( ? path.join(
"\\\\?\\pipe", "\\\\?\\pipe",
process.dirname, process.dirname,
"temp/.P" + process.pid + ".ipc", "temp/.P" + process.pid + ".ipc"
) )
: process.dirname + "/temp/.P" + process.pid + ".ipc", : process.dirname + "/temp/.P" + process.pid + ".ipc",
() => { () => {
fakeIPCConnection.end(message); fakeIPCConnection.end(message);
}, }
); );
}; };
@ -92,7 +92,7 @@ if (!process.singleThreaded) {
let command = newArguments.shift(); let command = newArguments.shift();
let newWorker = child_process.spawn(command, newArguments, { let newWorker = child_process.spawn(command, newArguments, {
env: newEnvironment, env: newEnvironment,
stdio: ["inherit", "inherit", "inherit", "ipc"], stdio: ["inherit", "inherit", "inherit", "ipc"]
}); });
newWorker.process = newWorker; newWorker.process = newWorker;
@ -126,7 +126,7 @@ if (!process.singleThreaded) {
}; };
try { try {
worker.send(undefined); if (process.isBun) worker.send(undefined);
} catch (err) { } catch (err) {
if (err.message === "NOT IMPLEMENTED") { if (err.message === "NOT IMPLEMENTED") {
sendImplemented = false; sendImplemented = false;
@ -158,9 +158,9 @@ if (!process.singleThreaded) {
? path.join( ? path.join(
"\\\\?\\pipe", "\\\\?\\pipe",
process.dirname, process.dirname,
"temp/.P" + newWorker.process.pid + ".ipc", "temp/.P" + newWorker.process.pid + ".ipc"
) )
: process.dirname + "/temp/.P" + newWorker.process.pid + ".ipc", : process.dirname + "/temp/.P" + newWorker.process.pid + ".ipc"
); );
// Cleanup when worker process exits // Cleanup when worker process exits
@ -174,7 +174,7 @@ if (!process.singleThreaded) {
fakeParam2, fakeParam2,
fakeParam3, fakeParam3,
fakeParam4, fakeParam4,
tries, tries
) { ) {
if (!tries) tries = 0; if (!tries) tries = 0;
@ -185,12 +185,12 @@ if (!process.singleThreaded) {
? path.join( ? path.join(
"\\\\?\\pipe", "\\\\?\\pipe",
process.dirname, process.dirname,
"temp/.W" + newWorker.process.pid + ".ipc", "temp/.W" + newWorker.process.pid + ".ipc"
) )
: process.dirname + "/temp/.W" + newWorker.process.pid + ".ipc", : process.dirname + "/temp/.W" + newWorker.process.pid + ".ipc",
() => { () => {
fakeWorkerIPCConnection.end(message); fakeWorkerIPCConnection.end(message);
}, }
); );
} catch (err) { } catch (err) {
if (tries > 50) throw err; if (tries > 50) throw err;
@ -199,7 +199,7 @@ if (!process.singleThreaded) {
fakeParam2, fakeParam2,
fakeParam3, fakeParam3,
fakeParam4, fakeParam4,
tries + 1, tries + 1
); );
} }
}; };
@ -216,11 +216,11 @@ if (!process.singleThreaded) {
}; };
if ( if (
process.isBun && (process.isBun || (process.versions && process.versions.deno)) &&
(cluster.isMaster === undefined || (cluster.isMaster === undefined ||
(cluster.isMaster && process.env.NODE_UNIQUE_ID)) (cluster.isMaster && process.env.NODE_UNIQUE_ID))
) { ) {
cluster.bunShim(); cluster.shim();
} }
// Shim cluster.isPrimary field // Shim cluster.isPrimary field