From 8178f758bc249f12fb82fce15a8f63be1b907ddb Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Sat, 27 Apr 2024 20:25:18 +0900 Subject: fix(ext/node): support process.stdin.unref() (#22865) This PR adds private `[REF]()` and `[UNREF]()` methods to Stdin class, and call them from Node.js polyfill layer (`TTY` class). This enables `process.stdin.unref()` and `process.stdin.ref()` for the case when stdin is terminal. closes #21796 --- ext/io/12_io.js | 32 ++++++++++++++++++++++++++++++-- ext/node/polyfills/tty.js | 13 ++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) (limited to 'ext') diff --git a/ext/io/12_io.js b/ext/io/12_io.js index c43117cef..094868371 100644 --- a/ext/io/12_io.js +++ b/ext/io/12_io.js @@ -9,6 +9,7 @@ import { op_set_raw } from "ext:core/ops"; const { Uint8Array, ArrayPrototypePush, + Symbol, TypedArrayPrototypeSubarray, TypedArrayPrototypeSet, TypedArrayPrototypeGetByteLength, @@ -181,9 +182,14 @@ const STDIN_RID = 0; const STDOUT_RID = 1; const STDERR_RID = 2; +const REF = Symbol("REF"); +const UNREF = Symbol("UNREF"); + class Stdin { #rid = STDIN_RID; + #ref = true; #readable; + #opPromise; constructor() { } @@ -197,8 +203,14 @@ class Stdin { return this.#rid; } - read(p) { - return read(this.#rid, p); + async read(p) { + if (p.length === 0) return 0; + this.#opPromise = core.read(this.#rid, p); + if (!this.#ref) { + core.unrefOpPromise(this.#opPromise); + } + const nread = await this.#opPromise; + return nread === 0 ? null : nread; } readSync(p) { @@ -224,6 +236,20 @@ class Stdin { isTerminal() { return core.isTerminal(this.#rid); } + + [REF]() { + this.#ref = true; + if (this.#opPromise) { + core.refOpPromise(this.#opPromise); + } + } + + [UNREF]() { + this.#ref = false; + if (this.#opPromise) { + core.unrefOpPromise(this.#opPromise); + } + } } class Stdout { @@ -318,6 +344,7 @@ export { readAll, readAllSync, readSync, + REF, SeekMode, Stderr, stderr, @@ -327,6 +354,7 @@ export { Stdout, stdout, STDOUT_RID, + UNREF, write, writeSync, }; diff --git a/ext/node/polyfills/tty.js b/ext/node/polyfills/tty.js index c44f7e946..3a84a1f94 100644 --- a/ext/node/polyfills/tty.js +++ b/ext/node/polyfills/tty.js @@ -9,7 +9,10 @@ const { } = core; import { ERR_INVALID_FD } from "ext:deno_node/internal/errors.ts"; -import { LibuvStreamWrap } from "ext:deno_node/internal_binding/stream_wrap.ts"; +import { + kStreamBaseField, + LibuvStreamWrap, +} from "ext:deno_node/internal_binding/stream_wrap.ts"; import { providerType } from "ext:deno_node/internal_binding/async_wrap.ts"; import { Socket } from "node:net"; import { setReadStream } from "ext:deno_node/_process/streams.mjs"; @@ -36,6 +39,14 @@ class TTY extends LibuvStreamWrap { constructor(handle) { super(providerType.TTYWRAP, handle); } + + ref() { + this[kStreamBaseField][io.REF](); + } + + unref() { + this[kStreamBaseField][io.UNREF](); + } } export class ReadStream extends Socket { -- cgit v1.2.3