aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/builtins/js
diff options
context:
space:
mode:
authorGravatar Alex Lam S.L <alexlamsl@gmail.com> 2022-12-14 05:02:20 +0200
committerGravatar GitHub <noreply@github.com> 2022-12-13 19:02:20 -0800
commit4e920d73b72e94d0974166a88f375a26cbc0618c (patch)
tree039eae89d091348d65a2fe2e78b3050708288bb6 /src/bun.js/builtins/js
parenta92258355dd78ccd7ac06483810a25ce436fa699 (diff)
downloadbun-4e920d73b72e94d0974166a88f375a26cbc0618c.tar.gz
bun-4e920d73b72e94d0974166a88f375a26cbc0618c.tar.zst
bun-4e920d73b72e94d0974166a88f375a26cbc0618c.zip
make `process.stdin` work under TTY (#1611)
- workarounds for #1607 & #1608 fixes #1604
Diffstat (limited to 'src/bun.js/builtins/js')
-rw-r--r--src/bun.js/builtins/js/ProcessObjectInternals.js84
1 files changed, 35 insertions, 49 deletions
diff --git a/src/bun.js/builtins/js/ProcessObjectInternals.js b/src/bun.js/builtins/js/ProcessObjectInternals.js
index bcbc7873e..5c5fab9ea 100644
--- a/src/bun.js/builtins/js/ProcessObjectInternals.js
+++ b/src/bun.js/builtins/js/ProcessObjectInternals.js
@@ -439,10 +439,12 @@ function getStdinStream(fd, rawRequire, Bun) {
var module = { path: "node:process", require: rawRequire };
var require = (path) => module.require(path);
- var { Readable, Duplex, eos, destroy } = require("node:stream");
+ var { Duplex, eos, destroy } = require("node:stream");
var StdinStream = class StdinStream extends Duplex {
- #readStream;
+ #reader;
+ // TODO: investigate https://github.com/oven-sh/bun/issues/1607
+ #readRef;
#writeStream;
#readable = true;
@@ -451,7 +453,6 @@ function getStdinStream(fd, rawRequire, Bun) {
#onFinish;
#onClose;
#onDrain;
- #onReadable;
get isTTY() {
return require("tty").isatty(fd);
@@ -461,10 +462,13 @@ function getStdinStream(fd, rawRequire, Bun) {
return fd_;
}
+ // TODO: investigate https://github.com/oven-sh/bun/issues/1608
+ _construct(callback) {
+ callback();
+ }
+
constructor() {
super({ readable: true, writable: true });
-
- this.#onReadable = (...args) => this._read(...args);
}
#onFinished(err) {
@@ -505,63 +509,45 @@ function getStdinStream(fd, rawRequire, Bun) {
callback(err);
} else {
this.#onClose = callback;
- if (this.#readStream) destroy(this.#readStream, err);
if (this.#writeStream) destroy(this.#writeStream, err);
}
}
- on(ev, cb) {
- super.on(ev, cb);
- if (!this.#readStream && (ev === "readable" || ev === "data")) {
- this.#loadReadStream();
- }
-
- return this;
+ pause() {
+ this.unref();
+ return super.pause();
}
- once(ev, cb) {
- super.once(ev, cb);
- if (!this.#readStream && (ev === "readable" || ev === "data")) {
- this.#loadReadStream();
- }
-
- return this;
- }
-
- #loadReadStream() {
- var readStream = (this.#readStream = Readable.fromWeb(
- Bun.stdin.stream(),
- ));
-
- readStream.on("data", (data) => {
- this.push(data);
- });
- readStream.ref();
-
- readStream.on("end", () => {
- this.push(null);
- });
-
- eos(readStream, (err) => {
- this.#readable = false;
- if (err) {
- destroy(readStream, err);
- }
- this.#onFinished(err);
- });
+ resume() {
+ this.#reader = Bun.stdin.stream().getReader();
+ this.ref();
+ return super.resume();
}
ref() {
- this.#readStream?.ref?.();
+ this.#readRef ??= setInterval(() => {}, 1 << 30);
}
unref() {
- this.#readStream?.unref?.();
+ if (this.#readRef) {
+ clearInterval(this.#readRef);
+ this.#readRef = null;
+ }
}
- _read(encoding, callback) {
- if (!this.#readStream) this.#loadReadStream();
-
- return this.#readStream._read(...arguments);
+ _read(size) {
+ this.#reader.read().then((data) => {
+ if (data.done) {
+ this.push(null);
+ this.pause();
+ this.#readable = false;
+ this.#onFinished();
+ } else {
+ this.push(data.value);
+ }
+ }).catch((err) => {
+ this.#readable = false;
+ this.#onFinished(err);
+ });
}
#constructWriteStream() {