aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/builtins/js/ProcessObjectInternals.js
diff options
context:
space:
mode:
authorGravatar Alex Lam S.L <alexlamsl@gmail.com> 2022-12-18 08:37:45 +0200
committerGravatar GitHub <noreply@github.com> 2022-12-17 22:37:45 -0800
commit2b622162ef6fc999ece19a9bbbc265af326b84a7 (patch)
treeb16e767455dc2a3ff26acae6a24bebf2b72f0a72 /src/bun.js/builtins/js/ProcessObjectInternals.js
parent5a3e0836b14a34a2f46e1b38e94e4cceef9e3f3f (diff)
downloadbun-2b622162ef6fc999ece19a9bbbc265af326b84a7.tar.gz
bun-2b622162ef6fc999ece19a9bbbc265af326b84a7.tar.zst
bun-2b622162ef6fc999ece19a9bbbc265af326b84a7.zip
bug compatible with `stdin.on("readable")` (#1626)
Diffstat (limited to 'src/bun.js/builtins/js/ProcessObjectInternals.js')
-rw-r--r--src/bun.js/builtins/js/ProcessObjectInternals.js29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/bun.js/builtins/js/ProcessObjectInternals.js b/src/bun.js/builtins/js/ProcessObjectInternals.js
index e07d3389b..792f59ec3 100644
--- a/src/bun.js/builtins/js/ProcessObjectInternals.js
+++ b/src/bun.js/builtins/js/ProcessObjectInternals.js
@@ -435,7 +435,7 @@ function getStdioWriteStream(fd_, rawRequire) {
return new FastStdioWriteStream(fd_);
}
-function getStdinStream(fd, rawRequire, Bun) {
+function getStdinStream(fd_, rawRequire, Bun) {
var module = { path: "node:process", require: rawRequire };
var require = (path) => module.require(path);
@@ -449,6 +449,7 @@ function getStdinStream(fd, rawRequire, Bun) {
#writeStream;
#readable = true;
+ #unrefOnRead = false;
#writable = true;
#onFinish;
@@ -456,7 +457,7 @@ function getStdinStream(fd, rawRequire, Bun) {
#onDrain;
get isTTY() {
- return require("tty").isatty(fd);
+ return require("tty").isatty(fd_);
}
get fd() {
@@ -509,20 +510,38 @@ function getStdinStream(fd, rawRequire, Bun) {
}
}
+ on(name, callback) {
+ // Streams don't generally required to present any data when only
+ // `readable` events are present, i.e. `readableFlowing === false`
+ //
+ // However, Node.js has a this quirk whereby `process.stdin.read()`
+ // blocks under TTY mode, thus looping `.read()` in this particular
+ // case would not result in truncation.
+ //
+ // Therefore the following hack is only specific to `process.stdin`
+ // and does not apply to the underlying Stream implementation.
+ if (name === "readable") {
+ this.ref();
+ this.#unrefOnRead = true;
+ }
+ return super.on(name, callback);
+ }
+
pause() {
this.unref();
return super.pause();
}
resume() {
- this.#reader ??= Bun.stdin.stream().getReader();
this.ref();
return super.resume();
}
ref() {
+ this.#reader ??= Bun.stdin.stream().getReader();
this.#readRef ??= setInterval(() => {}, 1 << 30);
}
+
unref() {
if (this.#readRef) {
clearInterval(this.#readRef);
@@ -563,6 +582,10 @@ function getStdinStream(fd, rawRequire, Bun) {
}
_read(size) {
+ if (this.#unrefOnRead) {
+ this.unref();
+ this.#unrefOnRead = false;
+ }
this.#readInternal();
}