From 75209e12f19ca5d4a2a7c9008fba63a487ad8e6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 15 Feb 2023 19:44:52 +0100 Subject: 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@/" 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 Co-authored-by: Divy Srivastava --- ext/node/polyfills/_process/streams.mjs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'ext/node/polyfills/_process/streams.mjs') diff --git a/ext/node/polyfills/_process/streams.mjs b/ext/node/polyfills/_process/streams.mjs index 30811e673..46213a4ed 100644 --- a/ext/node/polyfills/_process/streams.mjs +++ b/ext/node/polyfills/_process/streams.mjs @@ -10,7 +10,9 @@ import { } 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) { @@ -92,13 +94,13 @@ function createWritableStdioStream(writer, name) { /** https://nodejs.org/api/process.html#process_process_stderr */ export const stderr = stdio.stderr = createWritableStdioStream( - Deno.stderr, + files.stderr, "stderr", ); /** https://nodejs.org/api/process.html#process_process_stdout */ export const stdout = stdio.stdout = createWritableStdioStream( - Deno.stdout, + files.stdout, "stdout", ); @@ -113,7 +115,7 @@ function _guessStdinType(fd) { const fileInfo = Deno.fstatSync?.(fd); // https://github.com/nodejs/node/blob/v18.12.1/deps/uv/src/unix/tty.c#L333 - if (Deno.build.os !== "windows") { + if (!isWindows) { switch (fileInfo.mode & fsConstants.S_IFMT) { case fsConstants.S_IFREG: case fsConstants.S_IFCHR: @@ -143,7 +145,7 @@ function _guessStdinType(fd) { // TODO(PolarETech): Need a better way to identify a character file on Windows. // "EISDIR" error occurs when stdin is "null" on Windows, // so use the error as a workaround. - if (Deno.build.os === "windows" && e.code === "EISDIR") return "FILE"; + if (isWindows && e.code === "EISDIR") return "FILE"; } return "UNKNOWN"; @@ -151,7 +153,7 @@ function _guessStdinType(fd) { const _read = function (size) { const p = Buffer.alloc(size || 16 * 1024); - Deno.stdin?.read(p).then((length) => { + files.stdin?.read(p).then((length) => { this.push(length === null ? null : p.slice(0, length)); }, (error) => { this.destroy(error); @@ -161,7 +163,7 @@ 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 = (() => { - const fd = Deno.stdin?.rid; + const fd = files.stdin?.rid; let _stdin; const stdinType = _guessStdinType(fd); @@ -222,8 +224,8 @@ export const stdin = stdio.stdin = (() => { return _stdin; })(); -stdin.on("close", () => Deno.stdin?.close()); -stdin.fd = Deno.stdin?.rid ?? -1; +stdin.on("close", () => files.stdin?.close()); +stdin.fd = files.stdin?.rid ?? -1; Object.defineProperty(stdin, "isTTY", { enumerable: true, configurable: true, @@ -233,7 +235,7 @@ Object.defineProperty(stdin, "isTTY", { }); stdin._isRawMode = false; stdin.setRawMode = (enable) => { - Deno.stdin?.setRaw?.(enable); + files.stdin?.setRaw?.(enable); stdin._isRawMode = enable; return stdin; }; -- cgit v1.2.3