1
0
Fork 0
forked from svrjs/svrjs

feat: add support for Deno 2

This commit is contained in:
Dorian Niemiec 2024-10-18 20:05:02 +02:00
parent 2f475b4066
commit 9abb89d560
4 changed files with 45 additions and 11 deletions

View file

@ -138,7 +138,9 @@ let disableMods = false;
const args = process.argv; const args = process.argv;
for ( for (
let i = let i =
process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("bun") > -1 process.argv[0].indexOf("node") > -1 ||
process.argv[0].indexOf("bun") > -1 ||
process.argv[0].indexOf("deno") > -1
? 2 ? 2
: 1; : 1;
i < args.length; i < args.length;
@ -631,6 +633,7 @@ try {
} }
if (vnum === undefined) vnum = 0; if (vnum === undefined) vnum = 0;
if (process.versions && process.versions.deno) vnum = 64;
if (process.isBun) vnum = 64; if (process.isBun) vnum = 64;
let mods = []; let mods = [];
@ -1416,13 +1419,24 @@ function SVRJSFork() {
serverconsole.locwarnmessage( serverconsole.locwarnmessage(
`${name} limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer.` `${name} limited the number of workers to one, because of startup problems in Bun 1.0 and newer with shimmed (not native) clustering module. Reliability may suffer.`
); );
} else if (
!threadLimitWarned &&
cluster.__shimmed__ &&
process.versions &&
process.versions.deno
) {
threadLimitWarned = true;
serverconsole.locwarnmessage(
`${name} limited the number of workers to one, because of startup problems in Deno with shimmed (not native) clustering module. Reliability may suffer.`
);
} }
if ( if (
!( !(
cluster.__shimmed__ && cluster.__shimmed__ &&
process.isBun && ((process.isBun &&
process.versions.bun && process.versions.bun &&
process.versions.bun[0] != "0" && process.versions.bun[0] != "0") ||
(process.versions && process.versions.deno_)) &&
Object.keys(cluster.workers) > 0 Object.keys(cluster.workers) > 0
) )
) { ) {
@ -1434,7 +1448,10 @@ function SVRJSFork() {
); );
} }
} catch (err) { } catch (err) {
if (err.name == "NotImplementedError") { if (
err.name == "NotImplementedError" ||
err.message == "Not implemented: cluster.fork"
) {
// If cluster.fork throws a NotImplementedError, shim cluster module // If cluster.fork throws a NotImplementedError, shim cluster module
cluster.bunShim(); cluster.bunShim();
if ( if (
@ -1725,6 +1742,7 @@ function start(init) {
); );
if ( if (
!process.isBun && !process.isBun &&
!(process.versions && process.versions.deno) &&
/^v(?:[0-9]\.|1[0-7]\.|18\.(?:[0-9]|1[0-8])\.|18\.19\.0|20\.(?:[0-9]|10)\.|20\.11\.0|21\.[0-5]\.|21\.6\.0|21\.6\.1(?![0-9]))/.test( /^v(?:[0-9]\.|1[0-7]\.|18\.(?:[0-9]|1[0-8])\.|18\.19\.0|20\.(?:[0-9]|10)\.|20\.11\.0|21\.[0-5]\.|21\.6\.0|21\.6\.1(?![0-9]))/.test(
process.version process.version
) )
@ -1734,6 +1752,7 @@ function start(init) {
); );
if ( if (
!process.isBun && !process.isBun &&
!(process.versions && process.versions.deno) &&
/^v(?:[0-9]\.|1[0-7]\.|18\.(?:1?[0-9])\.|18\.20\.0|20\.(?:[0-9]|1[01])\.|20\.12\.0|21\.[0-6]\.|21\.7\.0|21\.7\.1(?![0-9]))/.test( /^v(?:[0-9]\.|1[0-7]\.|18\.(?:1?[0-9])\.|18\.20\.0|20\.(?:[0-9]|1[01])\.|20\.12\.0|21\.[0-6]\.|21\.7\.0|21\.7\.1(?![0-9]))/.test(
process.version process.version
) )
@ -1805,6 +1824,8 @@ function start(init) {
serverconsole.locmessage("Server version: " + version); serverconsole.locmessage("Server version: " + version);
if (process.isBun) if (process.isBun)
serverconsole.locmessage("Bun version: v" + process.versions.bun); serverconsole.locmessage("Bun version: v" + process.versions.bun);
else if (process.versions && process.versions.deno)
serverconsole.locmessage("Deno version: " + process.versions.deno);
else serverconsole.locmessage("Node.JS version: " + process.version); else serverconsole.locmessage("Node.JS version: " + process.version);
const CPUs = os.cpus(); const CPUs = os.cpus();
if (CPUs.length > 0) if (CPUs.length > 0)

View file

@ -13,7 +13,7 @@ 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.bunShim = () => {
cluster.isMaster = !process.env.NODE_UNIQUE_ID; cluster.isMaster = !process.env.NODE_UNIQUE_ID;
@ -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;
@ -216,7 +216,7 @@ 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))
) { ) {

View file

@ -7,7 +7,12 @@ function generateServerString(exposeServerVersion) {
? `${name.replace(/ /g, "-")}/${version} (${getOS()}; ${ ? `${name.replace(/ /g, "-")}/${version} (${getOS()}; ${
process.isBun process.isBun
? "Bun/v" + process.versions.bun + "; like Node.JS/" + process.version ? "Bun/v" + process.versions.bun + "; like Node.JS/" + process.version
: "Node.JS/" + process.version : process.versions && process.versions.deno
? "Deno/v" +
process.versions.deno +
"; like Node.JS/" +
process.version
: "Node.JS/" + process.version
})` })`
: name.replace(/ /g, "-"); : name.replace(/ /g, "-");
} }

View file

@ -28,8 +28,16 @@ try {
function sendStatistics(modInfos, callback) { function sendStatistics(modInfos, callback) {
const statisticsToSend = JSON.stringify({ const statisticsToSend = JSON.stringify({
version: version, version: version,
runtime: process.isBun ? "Bun" : "Node.js", runtime: process.isBun
runtimeVersion: process.isBun ? process.versions.bun : process.version, ? "Bun"
: process.versions && process.versions.deno
? "Deno"
: "Node.js",
runtimeVersion: process.isBun
? process.versions.bun
: process.versions && process.versions.deno
? process.versions.deno
: process.version,
mods: modInfos mods: modInfos
}); });
const statisticsRequest = https.request( const statisticsRequest = https.request(