summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Casonato <hello@lcas.dev>2024-09-04 11:04:06 +0200
committerGitHub <noreply@github.com>2024-09-04 11:04:06 +0200
commitb333dccee82f4328a65d0c3c45c7aa5c19255220 (patch)
tree6799aff43eb6c8f2e1186c18a2b973b056f967a4
parent4c3b17b54703b455d8ae4b51354d18838c090658 (diff)
feat(cli): give access to `process` global everywhere (#25291)
-rw-r--r--ext/node/global.rs4
-rw-r--r--ext/node/polyfills/01_require.js11
-rw-r--r--ext/node/polyfills/_events.mjs8
-rw-r--r--ext/node/polyfills/process.ts3
-rw-r--r--runtime/js/98_global_scope_shared.js2
-rw-r--r--tests/node_compat/polyfill_globals.js4
-rw-r--r--tests/registry/npm/@denotest/globals/1.0.0/index.d.ts1
-rw-r--r--tests/registry/npm/@denotest/globals/1.0.0/index.js5
-rw-r--r--tests/specs/lint/node_globals_no_duplicate_imports/__test__.jsonc3
-rw-r--r--tests/specs/lint/node_globals_no_duplicate_imports/error.out4
-rw-r--r--tests/testdata/npm/compare_globals/main.out4
-rw-r--r--tests/testdata/npm/compare_globals/main.ts8
12 files changed, 17 insertions, 40 deletions
diff --git a/ext/node/global.rs b/ext/node/global.rs
index 61bcc3343..4d6695431 100644
--- a/ext/node/global.rs
+++ b/ext/node/global.rs
@@ -54,7 +54,6 @@ 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 (node only)
// - setImmediate (node only)
// - setInterval (both, but different implementation)
// - setTimeout (both, but different implementation)
@@ -62,7 +61,7 @@ const fn str_to_utf16<const N: usize>(s: &str) -> [u16; N] {
// UTF-16 encodings of the managed globals. THIS LIST MUST BE SORTED.
#[rustfmt::skip]
-const MANAGED_GLOBALS: [&[u16]; 13] = [
+const MANAGED_GLOBALS: [&[u16]; 12] = [
&str_to_utf16::<6>("Buffer"),
&str_to_utf16::<17>("WorkerGlobalScope"),
&str_to_utf16::<14>("clearImmediate"),
@@ -70,7 +69,6 @@ const MANAGED_GLOBALS: [&[u16]; 13] = [
&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 d666b3927..e4a781cc4 100644
--- a/ext/node/polyfills/01_require.js
+++ b/ext/node/polyfills/01_require.js
@@ -940,12 +940,11 @@ Module.prototype.require = function (id) {
// The module wrapper looks slightly different to Node. Instead of using one
// wrapper function, we use two. The first one exists to performance optimize
-// access to magic node globals, like `Buffer` or `process`. The second one
-// is the actual wrapper function we run the users code in.
-// The only observable difference is that in Deno `arguments.callee` is not
-// null.
+// access to magic node globals, like `Buffer`. The second one is the actual
+// 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, process, setImmediate, setInterval, setTimeout, performance) { (function (exports, require, module, __filename, __dirname) {",
+ "(function (exports, require, module, __filename, __dirname, Buffer, clearImmediate, clearInterval, clearTimeout, global, setImmediate, setInterval, setTimeout, performance) { (function (exports, require, module, __filename, __dirname) {",
"\n}).call(this, exports, require, module, __filename, __dirname); })",
];
Module.wrap = function (script) {
@@ -1030,7 +1029,6 @@ Module.prototype._compile = function (content, filename, format) {
clearInterval,
clearTimeout,
global,
- process,
setImmediate,
setInterval,
setTimeout,
@@ -1049,7 +1047,6 @@ Module.prototype._compile = function (content, filename, format) {
clearInterval,
clearTimeout,
global,
- process,
setImmediate,
setInterval,
setTimeout,
diff --git a/ext/node/polyfills/_events.mjs b/ext/node/polyfills/_events.mjs
index bb3498594..12b0935e6 100644
--- a/ext/node/polyfills/_events.mjs
+++ b/ext/node/polyfills/_events.mjs
@@ -46,7 +46,6 @@ import {
} from "ext:deno_node/internal/validators.mjs";
import { spliceOne } from "ext:deno_node/_utils.ts";
import { nextTick } from "ext:deno_node/_process/process.ts";
-import { nodeGlobals } from "ext:deno_node/00_globals.js";
const kCapture = Symbol("kCapture");
const kErrorMonitor = Symbol("events.errorMonitor");
@@ -55,6 +54,11 @@ const kMaxEventTargetListenersWarned = Symbol(
"events.maxEventTargetListenersWarned",
);
+let process;
+export function setProcess(p) {
+ process = p;
+}
+
/**
* Creates a new `EventEmitter` instance.
* @param {{ captureRejections?: boolean; }} [opts]
@@ -469,7 +473,7 @@ function _addListener(target, type, listener, prepend) {
w.emitter = target;
w.type = type;
w.count = existing.length;
- nodeGlobals.process.emitWarning(w);
+ process.emitWarning(w);
}
}
diff --git a/ext/node/polyfills/process.ts b/ext/node/polyfills/process.ts
index 8d5442935..2130087fb 100644
--- a/ext/node/polyfills/process.ts
+++ b/ext/node/polyfills/process.ts
@@ -69,6 +69,7 @@ import * as constants from "ext:deno_node/internal_binding/constants.ts";
import * as uv from "ext:deno_node/internal_binding/uv.ts";
import type { BindingName } from "ext:deno_node/internal_binding/mod.ts";
import { buildAllowedFlags } from "ext:deno_node/internal/process/per_thread.mjs";
+import { setProcess } from "ext:deno_node/_events.mjs";
const notImplementedEvents = [
"multipleResolves",
@@ -960,4 +961,6 @@ internals.__bootstrapNodeProcess = function (
}
};
+setProcess(process);
+
export default process;
diff --git a/runtime/js/98_global_scope_shared.js b/runtime/js/98_global_scope_shared.js
index b6e480216..41df35c60 100644
--- a/runtime/js/98_global_scope_shared.js
+++ b/runtime/js/98_global_scope_shared.js
@@ -31,6 +31,7 @@ import * as webidl from "ext:deno_webidl/00_webidl.js";
import { DOMException } from "ext:deno_web/01_dom_exception.js";
import * as abortSignal from "ext:deno_web/03_abort_signal.js";
import * as imageData from "ext:deno_web/16_image_data.js";
+import process from "node:process";
import { loadWebGPU } from "ext:deno_webgpu/00_init.js";
import * as webgpuSurface from "ext:deno_webgpu/02_surface.js";
import { unstableIds } from "ext:runtime/90_deno_ns.js";
@@ -137,6 +138,7 @@ const windowOrWorkerGlobalScope = {
fetch: core.propWritable(fetch.fetch),
EventSource: core.propWritable(eventSource.EventSource),
performance: core.propWritable(performance.performance),
+ process: core.propWritable(process),
reportError: core.propWritable(event.reportError),
setInterval: core.propWritable(timers.setInterval),
setTimeout: core.propWritable(timers.setTimeout),
diff --git a/tests/node_compat/polyfill_globals.js b/tests/node_compat/polyfill_globals.js
index 93246d2ef..79e1cc3f9 100644
--- a/tests/node_compat/polyfill_globals.js
+++ b/tests/node_compat/polyfill_globals.js
@@ -1,5 +1,4 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
-import process from "node:process";
import { Buffer } from "node:buffer";
import {
clearImmediate,
@@ -10,15 +9,12 @@ import {
setTimeout,
} from "node:timers";
import { performance } from "node:perf_hooks";
-import console from "node:console";
globalThis.Buffer = Buffer;
globalThis.clearImmediate = clearImmediate;
globalThis.clearInterval = clearInterval;
globalThis.clearTimeout = clearTimeout;
-globalThis.console = console;
globalThis.global = globalThis;
globalThis.performance = performance;
-globalThis.process = process;
globalThis.setImmediate = setImmediate;
globalThis.setInterval = setInterval;
globalThis.setTimeout = setTimeout;
diff --git a/tests/registry/npm/@denotest/globals/1.0.0/index.d.ts b/tests/registry/npm/@denotest/globals/1.0.0/index.d.ts
index 76dd781db..548633db1 100644
--- a/tests/registry/npm/@denotest/globals/1.0.0/index.d.ts
+++ b/tests/registry/npm/@denotest/globals/1.0.0/index.d.ts
@@ -15,7 +15,6 @@ type _TestHasProcessGlobal = AssertTrue<
export function deleteSetTimeout(): void;
export function getSetTimeout(): typeof setTimeout;
-export function checkProcessGlobal(): void;
export function checkWindowGlobal(): void;
export function checkSelfGlobal(): void;
diff --git a/tests/registry/npm/@denotest/globals/1.0.0/index.js b/tests/registry/npm/@denotest/globals/1.0.0/index.js
index 64f913b37..542cb63d2 100644
--- a/tests/registry/npm/@denotest/globals/1.0.0/index.js
+++ b/tests/registry/npm/@denotest/globals/1.0.0/index.js
@@ -10,11 +10,6 @@ exports.getSetTimeout = function () {
return globalThis.setTimeout;
};
-exports.checkProcessGlobal = function () {
- console.log("process" in globalThis);
- console.log(Object.getOwnPropertyDescriptor(globalThis, "process") !== undefined);
-};
-
exports.checkWindowGlobal = function () {
console.log("window" in globalThis);
console.log(Object.getOwnPropertyDescriptor(globalThis, "window") !== undefined);
diff --git a/tests/specs/lint/node_globals_no_duplicate_imports/__test__.jsonc b/tests/specs/lint/node_globals_no_duplicate_imports/__test__.jsonc
index d8e00f47a..a632e896e 100644
--- a/tests/specs/lint/node_globals_no_duplicate_imports/__test__.jsonc
+++ b/tests/specs/lint/node_globals_no_duplicate_imports/__test__.jsonc
@@ -3,8 +3,7 @@
"steps": [
{
"args": "run main.ts",
- "output": "error.out",
- "exitCode": 1
+ "output": ""
},
{
"args": "lint main.ts",
diff --git a/tests/specs/lint/node_globals_no_duplicate_imports/error.out b/tests/specs/lint/node_globals_no_duplicate_imports/error.out
deleted file mode 100644
index 5671b17b4..000000000
--- a/tests/specs/lint/node_globals_no_duplicate_imports/error.out
+++ /dev/null
@@ -1,4 +0,0 @@
-error: Uncaught (in promise) ReferenceError: process is not defined
-const _foo = process.env.FOO;
- ^
- at [WILDCARD]main.ts:3:14
diff --git a/tests/testdata/npm/compare_globals/main.out b/tests/testdata/npm/compare_globals/main.out
index 5b1d26464..f6f90d533 100644
--- a/tests/testdata/npm/compare_globals/main.out
+++ b/tests/testdata/npm/compare_globals/main.out
@@ -15,10 +15,6 @@ setTimeout 2 function
setTimeout 3 function
setTimeout 4 function
setTimeout 5 undefined
-process 1 false
-process 2 false
-true
-true
window 1 false
window 2 false
false
diff --git a/tests/testdata/npm/compare_globals/main.ts b/tests/testdata/npm/compare_globals/main.ts
index 5019a5fd5..9482798d8 100644
--- a/tests/testdata/npm/compare_globals/main.ts
+++ b/tests/testdata/npm/compare_globals/main.ts
@@ -30,14 +30,6 @@ globals.deleteSetTimeout();
console.log("setTimeout 4", typeof globalThis.setTimeout);
console.log("setTimeout 5", typeof globals.getSetTimeout());
-// In Deno, the process global is not defined, but in Node it is.
-console.log("process 1", "process" in globalThis);
-console.log(
- "process 2",
- Object.getOwnPropertyDescriptor(globalThis, "process") !== undefined,
-);
-globals.checkProcessGlobal();
-
// In Deno 2 and Node.js, the window global is not defined.
console.log("window 1", "window" in globalThis);
console.log(