summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/_process/streams.mjs
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2023-10-30 08:53:08 -0700
committerGitHub <noreply@github.com>2023-10-30 15:53:08 +0000
commit09204107d85351dae07a45f6a9684b5b6e573652 (patch)
tree5a04f3a877e677e382e34684784ddf832838bd77 /ext/node/polyfills/_process/streams.mjs
parent1acef755ca8a0a0433a98e4a66433c63ee0a3b09 (diff)
fix: implement node:tty (#20892)
Fixes #21012 Closes https://github.com/denoland/deno/issues/20855 Fixes https://github.com/denoland/deno/issues/20890 Fixes https://github.com/denoland/deno/issues/20611 Fixes https://github.com/denoland/deno/issues/20336 Fixes `create-svelte` from https://github.com/denoland/deno/issues/17248 Fixes more reports here: - https://github.com/denoland/deno/issues/6529#issuecomment-1432690559 - https://github.com/denoland/deno/issues/6529#issuecomment-1522059006 - https://github.com/denoland/deno/issues/6529#issuecomment-1695803570
Diffstat (limited to 'ext/node/polyfills/_process/streams.mjs')
-rw-r--r--ext/node/polyfills/_process/streams.mjs74
1 files changed, 15 insertions, 59 deletions
diff --git a/ext/node/polyfills/_process/streams.mjs b/ext/node/polyfills/_process/streams.mjs
index b6efef65e..39ee89a82 100644
--- a/ext/node/polyfills/_process/streams.mjs
+++ b/ext/node/polyfills/_process/streams.mjs
@@ -12,9 +12,9 @@ import {
moveCursor,
} from "ext:deno_node/internal/readline/callbacks.mjs";
import { Duplex, Readable, Writable } from "node:stream";
-import { isWindows } from "ext:deno_node/_util/os.ts";
-import { fs as fsConstants } from "ext:deno_node/internal_binding/constants.ts";
import * as io from "ext:deno_io/12_io.js";
+import * as tty from "node:tty";
+import { guessHandleType } from "ext:deno_node/internal_binding/util.ts";
// https://github.com/nodejs/node/blob/00738314828074243c9a52a228ab4c68b04259ef/lib/internal/bootstrap/switches/is_main_thread.js#L41
export function createWritableStdioStream(writer, name) {
@@ -95,60 +95,21 @@ export function createWritableStdioStream(writer, name) {
return stream;
}
-// 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
function _guessStdinType(fd) {
if (typeof fd !== "number" || fd < 0) return "UNKNOWN";
- if (Deno.isatty?.(fd)) return "TTY";
-
- try {
- const fileInfo = Deno.fstatSync?.(fd);
-
- // https://github.com/nodejs/node/blob/v18.12.1/deps/uv/src/unix/tty.c#L333
- if (!isWindows) {
- switch (fileInfo.mode & fsConstants.S_IFMT) {
- case fsConstants.S_IFREG:
- case fsConstants.S_IFCHR:
- return "FILE";
- case fsConstants.S_IFIFO:
- return "PIPE";
- case fsConstants.S_IFSOCK:
- // TODO(PolarETech): Need a better way to identify "TCP".
- // Currently, unable to exclude UDP.
- return "TCP";
- default:
- return "UNKNOWN";
- }
- }
-
- // https://github.com/nodejs/node/blob/v18.12.1/deps/uv/src/win/handle.c#L31
- if (fileInfo.isFile) {
- // TODO(PolarETech): Need a better way to identify a piped stdin on Windows.
- // On Windows, `Deno.fstatSync(rid).isFile` returns true even for a piped stdin.
- // Therefore, a piped stdin cannot be distinguished from a file by this property.
- // The mtime, atime, and birthtime of the file are "2339-01-01T00:00:00.000Z",
- // so use the property as a workaround.
- if (fileInfo.birthtime.valueOf() === 11644473600000) return "PIPE";
- return "FILE";
- }
- } catch (e) {
- // 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 (isWindows && e.code === "EISDIR") return "FILE";
- }
-
- return "UNKNOWN";
+ return guessHandleType(fd);
}
const _read = function (size) {
const p = Buffer.alloc(size || 16 * 1024);
- io.stdin?.read(p).then((length) => {
- this.push(length === null ? null : p.slice(0, length));
- }, (error) => {
- this.destroy(error);
- });
+ io.stdin?.read(p).then(
+ (length) => {
+ this.push(length === null ? null : p.slice(0, length));
+ },
+ (error) => {
+ this.destroy(error);
+ },
+ );
};
/** https://nodejs.org/api/process.html#process_process_stdin */
@@ -172,17 +133,12 @@ export const initStdin = () => {
});
break;
}
- case "TTY":
+ case "TTY": {
+ stdin = new tty.ReadStream(fd);
+ break;
+ }
case "PIPE":
case "TCP": {
- // TODO(PolarETech):
- // For TTY, `new Duplex()` should be replaced `new tty.ReadStream()` if possible.
- // There are two problems that need to be resolved.
- // 1. Using them here introduces a circular dependency.
- // 2. Creating a tty.ReadStream() is not currently supported.
- // https://github.com/nodejs/node/blob/v18.12.1/lib/internal/bootstrap/switches/is_main_thread.js#L194
- // https://github.com/nodejs/node/blob/v18.12.1/lib/tty.js#L47
-
// For PIPE and TCP, `new Duplex()` should be replaced `new net.Socket()` if possible.
// There are two problems that need to be resolved.
// 1. Using them here introduces a circular dependency.