Skip to content

Commit

Permalink
fix(ext/node): have process global available in Node context (#27562)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
magurotuna authored Jan 8, 2025
1 parent cabdfa8 commit 1661ddd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 4 additions & 1 deletion ext/node/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,24 @@ const fn str_to_utf16<const N: usize>(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)
// - window (deno only)

// 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"),
&str_to_utf16::<13>("clearInterval"),
&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"),
Expand Down
4 changes: 3 additions & 1 deletion ext/node/polyfills/01_require.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1031,6 +1031,7 @@ Module.prototype._compile = function (content, filename, format) {
clearInterval,
clearTimeout,
global,
process,
setImmediate,
setInterval,
setTimeout,
Expand All @@ -1049,6 +1050,7 @@ Module.prototype._compile = function (content, filename, format) {
clearInterval,
clearTimeout,
global,
process,
setImmediate,
setInterval,
setTimeout,
Expand Down

0 comments on commit 1661ddd

Please sign in to comment.