summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/process.ts
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-02-15 19:44:52 +0100
committerGitHub <noreply@github.com>2023-02-15 19:44:52 +0100
commit75209e12f19ca5d4a2a7c9008fba63a487ad8e6a (patch)
treec122feabbceeef070de4d4eb23667c6153ea7eb1 /ext/node/polyfills/process.ts
parentc4b9a91e27a32c0949688034c2449936c01a44a9 (diff)
feat: wire up ext/node to the Node compatibility layer (#17785)
This PR changes Node.js/npm compatibility layer to use polyfills for built-in Node.js embedded in the snapshot (that are coming from "ext/node" extension). As a result loading `std/node`, either from "https://deno.land/std@<latest>/" or from "DENO_NODE_COMPAT_URL" env variable were removed. All code that is imported via "npm:" specifiers now uses code embedded in the snapshot. Several fixes were applied to various modules in "ext/node" to make tests pass. --------- Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com> Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com>
Diffstat (limited to 'ext/node/polyfills/process.ts')
-rw-r--r--ext/node/polyfills/process.ts45
1 files changed, 31 insertions, 14 deletions
diff --git a/ext/node/polyfills/process.ts b/ext/node/polyfills/process.ts
index 9e7c29be7..828b4c660 100644
--- a/ext/node/polyfills/process.ts
+++ b/ext/node/polyfills/process.ts
@@ -2,6 +2,7 @@
// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.
const internals = globalThis.__bootstrap.internals;
+import { core } from "internal:deno_node/polyfills/_core.ts";
import {
notImplemented,
warnNotImplemented,
@@ -32,8 +33,11 @@ import {
stdin as stdin_,
stdout as stdout_,
} from "internal:deno_node/polyfills/_process/streams.mjs";
-import { core } from "internal:deno_node/polyfills/_core.ts";
-import { processTicksAndRejections } from "internal:deno_node/polyfills/_next_tick.ts";
+import {
+ processTicksAndRejections,
+ runNextTicks,
+} from "internal:deno_node/polyfills/_next_tick.ts";
+import { isWindows } from "internal:deno_node/polyfills/_util/os.ts";
// TODO(kt3k): This should be set at start up time
export let arch = "";
@@ -71,10 +75,19 @@ const notImplementedEvents = [
"worker",
];
-export const argv = [];
+export const argv: string[] = [];
// Overwrites the 1st item with getter.
-Object.defineProperty(argv, "0", { get: Deno.execPath });
+// TODO(bartlomieju): added "configurable: true" to make this work for binary
+// commands, but that is probably a wrong solution
+// TODO(bartlomieju): move the configuration for all "argv" to
+// "internals.__bootstrapNodeProcess"
+Object.defineProperty(argv, "0", {
+ get: () => {
+ return Deno.execPath();
+ },
+ configurable: true,
+});
// Overwrites the 2st item with getter.
Object.defineProperty(argv, "1", {
get: () => {
@@ -86,13 +99,6 @@ Object.defineProperty(argv, "1", {
},
});
-// TODO(kt3k): Set the rest of args at start up time instead of defining
-// random number of getters.
-for (let i = 0; i < 30; i++) {
- const j = i;
- Object.defineProperty(argv, j + 2, { get: () => Deno.args[j] });
-}
-
/** https://nodejs.org/api/process.html#process_process_exit_code */
export const exit = (code?: number | string) => {
if (code || code === 0) {
@@ -681,9 +687,18 @@ addReadOnlyProcessAlias("throwDeprecation", "--throw-deprecation");
export const removeListener = process.removeListener;
export const removeAllListeners = process.removeAllListeners;
-// FIXME(bartlomieju): currently it's not called
-// only call this from runtime's main.js
-internals.__bootstrapNodeProcess = function () {
+// Should be called only once, in `runtime/js/99_main.js` when the runtime is
+// bootstrapped.
+internals.__bootstrapNodeProcess = function (args: string[]) {
+ for (let i = 0; i < args.length; i++) {
+ argv[i + 2] = args[i];
+ }
+
+ core.setNextTickCallback(processTicksAndRejections);
+ core.setMacrotaskCallback(runNextTicks);
+
+ // TODO(bartlomieju): this is buggy, see https://github.com/denoland/deno/issues/16928
+ // We should use a specialized API in 99_main.js instead
globalThis.addEventListener("unhandledrejection", (event) => {
if (process.listenerCount("unhandledRejection") === 0) {
// The Node.js default behavior is to raise an uncaught exception if
@@ -724,6 +739,8 @@ internals.__bootstrapNodeProcess = function () {
process.emit("exit", process.exitCode || 0);
}
});
+
+ delete internals.__bootstrapNodeProcess;
};
export default process;