From 1661ddd9cab350fe855da0a9d96178407dce5c51 Mon Sep 17 00:00:00 2001 From: Yusuke Tanaka Date: Tue, 7 Jan 2025 20:14:57 -0800 Subject: [PATCH] fix(ext/node): have `process` global available in Node context (#27562) This commit makes `process` global always available in Node context. `process` global was previously available explicitly in `deno_node`, but then got removed in #25291 and made globally available regardless of whether it's in Deno or Node context, so this commit does not have any effect on Deno CLI. However, for users who want to use `deno_node` ext only, it makes sense to have `process` available to simulate the Node environment individually. This change may bring some negative performance impact. To measure how large the impact would be, a very simple benchmark was performed whose results can be found at https://github.com/magurotuna/process_global_bench. --- ext/node/global.rs | 5 ++++- ext/node/polyfills/01_require.js | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ext/node/global.rs b/ext/node/global.rs index a89a3f9830..92439773d6 100644 --- a/ext/node/global.rs +++ b/ext/node/global.rs @@ -54,6 +54,8 @@ const fn str_to_utf16(s: &str) -> [u16; N] { // - clearTimeout (both, but different implementation) // - global (node only) // - performance (both, but different implementation) +// - process (always available in Node, while the availability in Deno depends +// on project creation time in Deno Deploy) // - setImmediate (node only) // - setInterval (both, but different implementation) // - setTimeout (both, but different implementation) @@ -61,7 +63,7 @@ const fn str_to_utf16(s: &str) -> [u16; N] { // UTF-16 encodings of the managed globals. THIS LIST MUST BE SORTED. #[rustfmt::skip] -const MANAGED_GLOBALS: [&[u16]; 12] = [ +const MANAGED_GLOBALS: [&[u16]; 13] = [ &str_to_utf16::<6>("Buffer"), &str_to_utf16::<17>("WorkerGlobalScope"), &str_to_utf16::<14>("clearImmediate"), @@ -69,6 +71,7 @@ const MANAGED_GLOBALS: [&[u16]; 12] = [ &str_to_utf16::<12>("clearTimeout"), &str_to_utf16::<6>("global"), &str_to_utf16::<11>("performance"), + &str_to_utf16::<7>("process"), &str_to_utf16::<4>("self"), &str_to_utf16::<12>("setImmediate"), &str_to_utf16::<11>("setInterval"), diff --git a/ext/node/polyfills/01_require.js b/ext/node/polyfills/01_require.js index 8f3201755f..3e90750cd3 100644 --- a/ext/node/polyfills/01_require.js +++ b/ext/node/polyfills/01_require.js @@ -946,7 +946,7 @@ Module.prototype.require = function (id) { // wrapper function we run the users code in. The only observable difference is // that in Deno `arguments.callee` is not null. Module.wrapper = [ - "(function (exports, require, module, __filename, __dirname, Buffer, clearImmediate, clearInterval, clearTimeout, global, setImmediate, setInterval, setTimeout, performance) { (function (exports, require, module, __filename, __dirname) {", + "(function (exports, require, module, __filename, __dirname, Buffer, clearImmediate, clearInterval, clearTimeout, global, process, setImmediate, setInterval, setTimeout, performance) { (function (exports, require, module, __filename, __dirname) {", "\n}).call(this, exports, require, module, __filename, __dirname); })", ]; Module.wrap = function (script) { @@ -1031,6 +1031,7 @@ Module.prototype._compile = function (content, filename, format) { clearInterval, clearTimeout, global, + process, setImmediate, setInterval, setTimeout, @@ -1049,6 +1050,7 @@ Module.prototype._compile = function (content, filename, format) { clearInterval, clearTimeout, global, + process, setImmediate, setInterval, setTimeout,