diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2024-04-27 20:25:18 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-27 20:25:18 +0900 |
commit | 8178f758bc249f12fb82fce15a8f63be1b907ddb (patch) | |
tree | df7d923d885962ee7207423504f9df9a2229b1e3 /ext/io | |
parent | e3d79c1703b7990f8ec6e1383abe0d2a8531fae8 (diff) |
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
Diffstat (limited to 'ext/io')
-rw-r--r-- | ext/io/12_io.js | 32 |
1 files changed, 30 insertions, 2 deletions
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, }; |