diff options
Diffstat (limited to 'ext/node')
| -rw-r--r-- | ext/node/01_node.js | 2 | ||||
| -rw-r--r-- | ext/node/polyfills/_next_tick.ts | 13 | ||||
| -rw-r--r-- | ext/node/polyfills/_process/process.ts | 5 | ||||
| -rw-r--r-- | ext/node/polyfills/_process/streams.mjs | 76 | ||||
| -rw-r--r-- | ext/node/polyfills/_readline.mjs | 2 | ||||
| -rw-r--r-- | ext/node/polyfills/child_process.ts | 2 | ||||
| -rw-r--r-- | ext/node/polyfills/process.ts | 49 | ||||
| -rw-r--r-- | ext/node/polyfills/timers.ts | 5 |
8 files changed, 87 insertions, 67 deletions
diff --git a/ext/node/01_node.js b/ext/node/01_node.js index 543a559f1..85346a44b 100644 --- a/ext/node/01_node.js +++ b/ext/node/01_node.js @@ -110,7 +110,7 @@ function initialize(nodeModules, nodeGlobalThisName) { value: nodeGlobalThis, }); // FIXME(bartlomieju): not nice to depend on `Deno` namespace here - internals.__bootstrapNodeProcess(Deno.args); + internals.__bootstrapNodeProcess(Deno.args, Deno.version); } internals.node = { diff --git a/ext/node/polyfills/_next_tick.ts b/ext/node/polyfills/_next_tick.ts index d5aa88218..72a6fc120 100644 --- a/ext/node/polyfills/_next_tick.ts +++ b/ext/node/polyfills/_next_tick.ts @@ -11,6 +11,11 @@ interface Tock { args: Array<unknown>; } +let nextTickEnabled = false; +export function enableNextTick() { + nextTickEnabled = true; +} + const queue = new FixedQueue(); export function processTicksAndRejections() { @@ -71,8 +76,6 @@ export function runNextTicks() { // return; if (!core.hasTickScheduled()) { core.runMicrotasks(); - } - if (!core.hasTickScheduled()) { return true; } @@ -93,6 +96,12 @@ export function nextTick<T extends Array<unknown>>( callback: (...args: T) => void, ...args: T ) { + // If we're snapshotting we don't want to push nextTick to be run. We'll + // enable next ticks in "__bootstrapNodeProcess()"; + if (!nextTickEnabled) { + return; + } + validateFunction(callback, "callback"); if (_exiting) { diff --git a/ext/node/polyfills/_process/process.ts b/ext/node/polyfills/_process/process.ts index 48b2e4620..24a4ae1a2 100644 --- a/ext/node/polyfills/_process/process.ts +++ b/ext/node/polyfills/_process/process.ts @@ -126,5 +126,8 @@ export const versions = { unicode: "14.0", ngtcp2: "0.8.1", nghttp3: "0.7.0", - ...Deno.version, + // Will be filled when calling "__bootstrapNodeProcess()", + deno: "", + v8: "", + typescript: "", }; diff --git a/ext/node/polyfills/_process/streams.mjs b/ext/node/polyfills/_process/streams.mjs index 46213a4ed..b27f75e2d 100644 --- a/ext/node/polyfills/_process/streams.mjs +++ b/ext/node/polyfills/_process/streams.mjs @@ -9,13 +9,12 @@ import { moveCursor, } from "internal:deno_node/polyfills/internal/readline/callbacks.mjs"; import { Duplex, Readable, Writable } from "internal:deno_node/polyfills/stream.ts"; -import { stdio } from "internal:deno_node/polyfills/_process/stdio.mjs"; import { isWindows } from "internal:deno_node/polyfills/_util/os.ts"; import { fs as fsConstants } from "internal:deno_node/polyfills/internal_binding/constants.ts"; import * as files from "internal:runtime/js/40_files.js"; // https://github.com/nodejs/node/blob/00738314828074243c9a52a228ab4c68b04259ef/lib/internal/bootstrap/switches/is_main_thread.js#L41 -function createWritableStdioStream(writer, name) { +export function createWritableStdioStream(writer, name) { const stream = new Writable({ write(buf, enc, cb) { if (!writer) { @@ -92,18 +91,6 @@ function createWritableStdioStream(writer, name) { return stream; } -/** https://nodejs.org/api/process.html#process_process_stderr */ -export const stderr = stdio.stderr = createWritableStdioStream( - files.stderr, - "stderr", -); - -/** https://nodejs.org/api/process.html#process_process_stdout */ -export const stdout = stdio.stdout = createWritableStdioStream( - files.stdout, - "stdout", -); - // TODO(PolarETech): This function should be replaced by // `guessHandleType()` in "../internal_binding/util.ts". // https://github.com/nodejs/node/blob/v18.12.1/src/node_util.cc#L257 @@ -162,9 +149,10 @@ const _read = function (size) { /** https://nodejs.org/api/process.html#process_process_stdin */ // https://github.com/nodejs/node/blob/v18.12.1/lib/internal/bootstrap/switches/is_main_thread.js#L189 -export const stdin = stdio.stdin = (() => { +/** Create process.stdin */ +export const initStdin = () => { const fd = files.stdin?.rid; - let _stdin; + let stdin; const stdinType = _guessStdinType(fd); switch (stdinType) { @@ -173,7 +161,7 @@ export const stdin = stdio.stdin = (() => { // use `Readable` instead. // https://github.com/nodejs/node/blob/v18.12.1/lib/internal/bootstrap/switches/is_main_thread.js#L200 // https://github.com/nodejs/node/blob/v18.12.1/lib/internal/fs/streams.js#L148 - _stdin = new Readable({ + stdin = new Readable({ highWaterMark: 64 * 1024, autoDestroy: false, read: _read, @@ -197,7 +185,7 @@ export const stdin = stdio.stdin = (() => { // 2. Creating a net.Socket() from a fd is not currently supported. // https://github.com/nodejs/node/blob/v18.12.1/lib/internal/bootstrap/switches/is_main_thread.js#L206 // https://github.com/nodejs/node/blob/v18.12.1/lib/net.js#L329 - _stdin = new Duplex({ + stdin = new Duplex({ readable: stdinType === "TTY" ? undefined : true, writable: stdinType === "TTY" ? undefined : false, readableHighWaterMark: stdinType === "TTY" ? 0 : undefined, @@ -210,39 +198,41 @@ export const stdin = stdio.stdin = (() => { if (stdinType !== "TTY") { // Make sure the stdin can't be `.end()`-ed - _stdin._writableState.ended = true; + stdin._writableState.ended = true; } break; } default: { // Provide a dummy contentless input for e.g. non-console // Windows applications. - _stdin = new Readable({ read() {} }); - _stdin.push(null); + stdin = new Readable({ read() {} }); + stdin.push(null); } } - return _stdin; -})(); -stdin.on("close", () => files.stdin?.close()); -stdin.fd = files.stdin?.rid ?? -1; -Object.defineProperty(stdin, "isTTY", { - enumerable: true, - configurable: true, - get() { - return Deno.isatty?.(Deno.stdin.rid); - }, -}); -stdin._isRawMode = false; -stdin.setRawMode = (enable) => { - files.stdin?.setRaw?.(enable); - stdin._isRawMode = enable; + stdin.on("close", () => files.stdin?.close()); + stdin.fd = files.stdin?.rid ?? -1; + Object.defineProperty(stdin, "isTTY", { + enumerable: true, + configurable: true, + get() { + return Deno.isatty?.(Deno.stdin.rid); + }, + }); + stdin._isRawMode = false; + stdin.setRawMode = (enable) => { + files.stdin?.setRaw?.(enable); + stdin._isRawMode = enable; + return stdin; + }; + Object.defineProperty(stdin, "isRaw", { + enumerable: true, + configurable: true, + get() { + return stdin._isRawMode; + }, + }); + return stdin; }; -Object.defineProperty(stdin, "isRaw", { - enumerable: true, - configurable: true, - get() { - return stdin._isRawMode; - }, -}); + diff --git a/ext/node/polyfills/_readline.mjs b/ext/node/polyfills/_readline.mjs index 6e0968af0..0665dbcf3 100644 --- a/ext/node/polyfills/_readline.mjs +++ b/ext/node/polyfills/_readline.mjs @@ -33,7 +33,7 @@ import promises from "internal:deno_node/polyfills/readline/promises.ts"; import { validateAbortSignal } from "internal:deno_node/polyfills/internal/validators.mjs"; import { promisify } from "internal:deno_node/polyfills/internal/util.mjs"; import { AbortError } from "internal:deno_node/polyfills/internal/errors.ts"; -import { process } from "internal:deno_node/polyfills/process.ts"; +import process from "internal:deno_node/polyfills/process.ts"; import { Interface as _Interface, diff --git a/ext/node/polyfills/child_process.ts b/ext/node/polyfills/child_process.ts index 06269e025..cc0e17ffb 100644 --- a/ext/node/polyfills/child_process.ts +++ b/ext/node/polyfills/child_process.ts @@ -40,7 +40,7 @@ import { promisify, } from "internal:deno_node/polyfills/util.ts"; import { createDeferredPromise } from "internal:deno_node/polyfills/internal/util.mjs"; -import { process } from "internal:deno_node/polyfills/process.ts"; +import process from "internal:deno_node/polyfills/process.ts"; import { Buffer } from "internal:deno_node/polyfills/buffer.ts"; import { convertToValidSignal, diff --git a/ext/node/polyfills/process.ts b/ext/node/polyfills/process.ts index 828b4c660..42c55ccc5 100644 --- a/ext/node/polyfills/process.ts +++ b/ext/node/polyfills/process.ts @@ -29,15 +29,17 @@ import { import { _exiting } from "internal:deno_node/polyfills/_process/exiting.ts"; export { _nextTick as nextTick, chdir, cwd, env, version, versions }; import { - stderr as stderr_, - stdin as stdin_, - stdout as stdout_, + createWritableStdioStream, + initStdin, } from "internal:deno_node/polyfills/_process/streams.mjs"; +import { stdio } from "internal:deno_node/polyfills/_process/stdio.mjs"; import { + enableNextTick, processTicksAndRejections, runNextTicks, } from "internal:deno_node/polyfills/_next_tick.ts"; import { isWindows } from "internal:deno_node/polyfills/_util/os.ts"; +import * as files from "internal:runtime/js/40_files.js"; // TODO(kt3k): This should be set at start up time export let arch = ""; @@ -50,11 +52,11 @@ export let pid = 0; // TODO(kt3k): Give better types to stdio objects // deno-lint-ignore no-explicit-any -const stderr = stderr_ as any; +let stderr = null as any; // deno-lint-ignore no-explicit-any -const stdin = stdin_ as any; +let stdin = null as any; // deno-lint-ignore no-explicit-any -const stdout = stdout_ as any; +let stdout = null as any; export { stderr, stdin, stdout }; import { getBinding } from "internal:deno_node/polyfills/internal_binding/mod.ts"; @@ -663,13 +665,10 @@ class Process extends EventEmitter { noDeprecation = false; } -// TODO(kt3k): Do the below at start up time. -/* -if (Deno.build.os === "windows") { +if (isWindows) { delete Process.prototype.getgid; delete Process.prototype.getuid; } -*/ /** https://nodejs.org/api/process.html#process_process */ const process = new Process(); @@ -689,13 +688,21 @@ export const removeAllListeners = process.removeAllListeners; // Should be called only once, in `runtime/js/99_main.js` when the runtime is // bootstrapped. -internals.__bootstrapNodeProcess = function (args: string[]) { +internals.__bootstrapNodeProcess = function ( + args: string[], + denoVersions: Record<string, string>, +) { for (let i = 0; i < args.length; i++) { argv[i + 2] = args[i]; } + for (const [key, value] of Object.entries(denoVersions)) { + versions[key] = value; + } + core.setNextTickCallback(processTicksAndRejections); core.setMacrotaskCallback(runNextTicks); + enableNextTick(); // TODO(bartlomieju): this is buggy, see https://github.com/denoland/deno/issues/16928 // We should use a specialized API in 99_main.js instead @@ -740,12 +747,22 @@ internals.__bootstrapNodeProcess = function (args: string[]) { } }); + // Initializes stdin + stdin = stdio.stdin = process.stdin = initStdin(); + + /** https://nodejs.org/api/process.html#process_process_stderr */ + stderr = stdio.stderr = process.stderr = createWritableStdioStream( + files.stderr, + "stderr", + ); + + /** https://nodejs.org/api/process.html#process_process_stdout */ + stdout = stdio.stdout = process.stdout = createWritableStdioStream( + files.stdout, + "stdout", + ); + delete internals.__bootstrapNodeProcess; }; export default process; - -//TODO(Soremwar) -//Remove on 1.0 -//Kept for backwards compatibility with std -export { process }; diff --git a/ext/node/polyfills/timers.ts b/ext/node/polyfills/timers.ts index 57ff71a38..5a650c1cc 100644 --- a/ext/node/polyfills/timers.ts +++ b/ext/node/polyfills/timers.ts @@ -7,9 +7,10 @@ import { import { validateFunction } from "internal:deno_node/polyfills/internal/validators.mjs"; import { promisify } from "internal:deno_node/polyfills/internal/util.mjs"; export { setUnrefTimeout } from "internal:deno_node/polyfills/internal/timers.mjs"; +import * as timers from "internal:deno_web/02_timers.js"; -const clearTimeout_ = globalThis.clearTimeout; -const clearInterval_ = globalThis.clearInterval; +const clearTimeout_ = timers.clearTimeout; +const clearInterval_ = timers.clearInterval; export function setTimeout( callback: (...args: unknown[]) => void, |
