From 9abb89d5604d5764a06be986d292d0a506918633 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Fri, 18 Oct 2024 20:05:02 +0200 Subject: [PATCH] feat: add support for Deno 2 --- src/index.js | 31 ++++++++++++++++++++++++++----- src/utils/clusterBunShim.js | 6 +++--- src/utils/generateServerString.js | 7 ++++++- src/utils/sendStatistics.js | 12 ++++++++++-- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index 94f43bd..972d25d 100644 --- a/src/index.js +++ b/src/index.js @@ -138,7 +138,9 @@ let disableMods = false; const args = process.argv; for ( 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 : 1; i < args.length; @@ -631,6 +633,7 @@ try { } if (vnum === undefined) vnum = 0; +if (process.versions && process.versions.deno) vnum = 64; if (process.isBun) vnum = 64; let mods = []; @@ -1416,13 +1419,24 @@ function SVRJSFork() { 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.` ); + } 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 ( !( cluster.__shimmed__ && - process.isBun && - process.versions.bun && - process.versions.bun[0] != "0" && + ((process.isBun && + process.versions.bun && + process.versions.bun[0] != "0") || + (process.versions && process.versions.deno_)) && Object.keys(cluster.workers) > 0 ) ) { @@ -1434,7 +1448,10 @@ function SVRJSFork() { ); } } 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 cluster.bunShim(); if ( @@ -1725,6 +1742,7 @@ function start(init) { ); if ( !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( process.version ) @@ -1734,6 +1752,7 @@ function start(init) { ); if ( !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( process.version ) @@ -1805,6 +1824,8 @@ function start(init) { serverconsole.locmessage("Server version: " + version); if (process.isBun) 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); const CPUs = os.cpus(); if (CPUs.length > 0) diff --git a/src/utils/clusterBunShim.js b/src/utils/clusterBunShim.js index e7c375b..5818af4 100644 --- a/src/utils/clusterBunShim.js +++ b/src/utils/clusterBunShim.js @@ -13,7 +13,7 @@ if (!process.singleThreaded) { // Clustering is not supported! } - // Cluster & IPC shim for Bun + // Cluster & IPC shim for Bun and Deno cluster.bunShim = () => { cluster.isMaster = !process.env.NODE_UNIQUE_ID; @@ -126,7 +126,7 @@ if (!process.singleThreaded) { }; try { - worker.send(undefined); + if (process.isBun) worker.send(undefined); } catch (err) { if (err.message === "NOT IMPLEMENTED") { sendImplemented = false; @@ -216,7 +216,7 @@ if (!process.singleThreaded) { }; if ( - process.isBun && + (process.isBun || (process.versions && process.versions.deno)) && (cluster.isMaster === undefined || (cluster.isMaster && process.env.NODE_UNIQUE_ID)) ) { diff --git a/src/utils/generateServerString.js b/src/utils/generateServerString.js index ccd5a74..9e764a9 100644 --- a/src/utils/generateServerString.js +++ b/src/utils/generateServerString.js @@ -7,7 +7,12 @@ function generateServerString(exposeServerVersion) { ? `${name.replace(/ /g, "-")}/${version} (${getOS()}; ${ process.isBun ? "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, "-"); } diff --git a/src/utils/sendStatistics.js b/src/utils/sendStatistics.js index e502aa8..f7e4e96 100644 --- a/src/utils/sendStatistics.js +++ b/src/utils/sendStatistics.js @@ -28,8 +28,16 @@ try { function sendStatistics(modInfos, callback) { const statisticsToSend = JSON.stringify({ version: version, - runtime: process.isBun ? "Bun" : "Node.js", - runtimeVersion: process.isBun ? process.versions.bun : process.version, + runtime: process.isBun + ? "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 }); const statisticsRequest = https.request(