1
0
Fork 0
forked from svrjs/svrjs
This repository has been archived on 2024-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
svrjs/src/utils/clusterBunShim.js

234 lines
6.5 KiB
JavaScript
Raw Normal View History

2024-08-23 19:58:15 +02:00
const net = require("net");
const os = require("os");
const path = require("path");
let cluster = {};
2024-08-24 16:54:28 +02:00
if (!process.singleThreaded) {
try {
// Import cluster module
cluster = require("cluster");
} catch (err) {
// Clustering is not supported!
}
// Cluster & IPC shim for Bun
cluster.bunShim = function () {
cluster.isMaster = !process.env.NODE_UNIQUE_ID;
cluster.isPrimary = cluster.isMaster;
cluster.isWorker = !cluster.isMaster;
cluster.__shimmed__ = true;
if (cluster.isWorker) {
// Shim the cluster.worker object for worker processes
cluster.worker = {
id: parseInt(process.env.NODE_UNIQUE_ID),
process: process,
isDead: function () {
return false;
},
send: function (message, b, c, d) {
process.send(message, b, c, d);
},
};
2024-08-23 19:58:15 +02:00
2024-08-24 16:54:28 +02:00
if (!process.send) {
// Shim the process.send function for worker processes
2024-08-23 19:58:15 +02:00
2024-08-24 16:54:28 +02:00
// Create a fake IPC server to receive messages
let fakeIPCServer = net.createServer(function (socket) {
let receivedData = "";
2024-08-23 19:58:15 +02:00
2024-08-24 16:54:28 +02:00
socket.on("data", function (data) {
receivedData += data.toString();
});
2024-08-23 19:58:15 +02:00
2024-08-24 16:54:28 +02:00
socket.on("end", function () {
process.emit("message", receivedData);
});
2024-08-23 19:58:15 +02:00
});
2024-08-24 16:54:28 +02:00
fakeIPCServer.listen(
2024-08-23 19:58:15 +02:00
os.platform() === "win32"
? path.join(
"\\\\?\\pipe",
process.dirname,
2024-08-24 16:54:28 +02:00
"temp/.W" + process.pid + ".ipc",
2024-08-23 19:58:15 +02:00
)
: process.dirname + "/temp/.W" + process.pid + ".ipc",
2024-08-23 19:58:15 +02:00
);
2024-08-24 16:54:28 +02:00
process.send = function (message) {
// Create a fake IPC connection to send messages
let fakeIPCConnection = net.createConnection(
os.platform() === "win32"
? path.join(
"\\\\?\\pipe",
process.dirname,
2024-08-24 16:54:28 +02:00
"temp/.P" + process.pid + ".ipc",
)
: process.dirname + "/temp/.P" + process.pid + ".ipc",
2024-08-24 16:54:28 +02:00
function () {
fakeIPCConnection.end(message);
},
);
};
process.removeFakeIPC = function () {
// Close IPC server
process.send = function () {};
fakeIPCServer.close();
};
}
2024-08-23 19:58:15 +02:00
}
2024-08-24 16:54:28 +02:00
// Custom implementation for cluster.fork()
cluster._workersCounter = 1;
cluster.workers = {};
cluster.fork = function (env) {
const child_process = require("child_process");
let newEnvironment = Object.assign(env ? env : process.env);
newEnvironment.NODE_UNIQUE_ID = cluster._workersCounter;
let newArguments = Object.assign(process.argv);
let command = newArguments.shift();
let newWorker = child_process.spawn(command, newArguments, {
env: newEnvironment,
stdio: ["inherit", "inherit", "inherit", "ipc"],
});
2024-08-23 19:58:15 +02:00
2024-08-24 16:54:28 +02:00
newWorker.process = newWorker;
newWorker.isDead = function () {
return newWorker.exitCode !== null || newWorker.killed;
};
newWorker.id = newEnvironment.NODE_UNIQUE_ID;
function checkSendImplementation(worker) {
let sendImplemented = true;
if (
!(
process.versions &&
process.versions.bun &&
process.versions.bun[0] != "0"
)
) {
if (!worker.send) {
sendImplemented = false;
2024-08-23 19:58:15 +02:00
}
2024-08-24 16:54:28 +02:00
let oldLog = console.log;
console.log = function (a, b, c, d, e, f) {
if (
a == "ChildProcess.prototype.send() - Sorry! Not implemented yet"
) {
throw new Error("NOT IMPLEMENTED");
} else {
oldLog(a, b, c, d, e, f);
}
};
try {
worker.send(undefined);
} catch (err) {
if (err.message === "NOT IMPLEMENTED") {
sendImplemented = false;
}
console.log(err);
2024-08-23 19:58:15 +02:00
}
2024-08-24 16:54:28 +02:00
console.log = oldLog;
2024-08-23 19:58:15 +02:00
}
2024-08-24 16:54:28 +02:00
return sendImplemented;
2024-08-23 19:58:15 +02:00
}
2024-08-24 16:54:28 +02:00
if (!checkSendImplementation(newWorker)) {
// Create a fake IPC server for worker process to receive messages
let fakeWorkerIPCServer = net.createServer(function (socket) {
let receivedData = "";
2024-08-23 19:58:15 +02:00
2024-08-24 16:54:28 +02:00
socket.on("data", function (data) {
receivedData += data.toString();
});
2024-08-23 19:58:15 +02:00
2024-08-24 16:54:28 +02:00
socket.on("end", function () {
newWorker.emit("message", receivedData);
});
2024-08-23 19:58:15 +02:00
});
2024-08-24 16:54:28 +02:00
fakeWorkerIPCServer.listen(
os.platform() === "win32"
? path.join(
"\\\\?\\pipe",
process.dirname,
2024-08-24 16:54:28 +02:00
"temp/.P" + newWorker.process.pid + ".ipc",
)
: process.dirname + "/temp/.P" + newWorker.process.pid + ".ipc",
2024-08-24 16:54:28 +02:00
);
2024-08-23 19:58:15 +02:00
2024-08-24 16:54:28 +02:00
// Cleanup when worker process exits
newWorker.on("exit", function () {
fakeWorkerIPCServer.close();
delete cluster.workers[newWorker.id];
2024-08-23 19:58:15 +02:00
});
2024-08-24 16:54:28 +02:00
newWorker.send = function (
message,
fakeParam2,
fakeParam3,
fakeParam4,
tries,
) {
if (!tries) tries = 0;
try {
// Create a fake IPC connection to send messages to worker process
let fakeWorkerIPCConnection = net.createConnection(
os.platform() === "win32"
? path.join(
"\\\\?\\pipe",
process.dirname,
2024-08-24 16:54:28 +02:00
"temp/.W" + newWorker.process.pid + ".ipc",
)
: process.dirname + "/temp/.W" + newWorker.process.pid + ".ipc",
2024-08-24 16:54:28 +02:00
function () {
fakeWorkerIPCConnection.end(message);
},
);
} catch (err) {
if (tries > 50) throw err;
newWorker.send(
message,
fakeParam2,
fakeParam3,
fakeParam4,
tries + 1,
);
}
};
} else {
newWorker.on("exit", function () {
delete cluster.workers[newWorker.id];
});
}
2024-08-23 19:58:15 +02:00
2024-08-24 16:54:28 +02:00
cluster.workers[newWorker.id] = newWorker;
cluster._workersCounter++;
return newWorker;
};
2024-08-23 19:58:15 +02:00
};
2024-08-24 16:54:28 +02:00
if (
process.isBun &&
(cluster.isMaster === undefined ||
(cluster.isMaster && process.env.NODE_UNIQUE_ID))
) {
cluster.bunShim();
}
// Shim cluster.isPrimary field
if (cluster.isPrimary === undefined && cluster.isMaster !== undefined)
cluster.isPrimary = cluster.isMaster;
}
2024-08-23 19:58:15 +02:00
module.exports = cluster;