aboutsummaryrefslogtreecommitdiff
path: root/src/js/node
diff options
context:
space:
mode:
authorGravatar Dylan Conway <35280289+dylan-conway@users.noreply.github.com> 2023-08-28 20:08:08 -0700
committerGravatar GitHub <noreply@github.com> 2023-08-28 20:08:08 -0700
commitd1c2d6b25ceaeb58513d8687ba7945447adb6618 (patch)
tree19292e163ea1bb3caaa58b25c8bc1478c16ab50a /src/js/node
parenta2aad40171292914bef924f12172ae00049fcef4 (diff)
downloadbun-d1c2d6b25ceaeb58513d8687ba7945447adb6618.tar.gz
bun-d1c2d6b25ceaeb58513d8687ba7945447adb6618.tar.zst
bun-d1c2d6b25ceaeb58513d8687ba7945447adb6618.zip
use `options.fd` if provided for `fs.Read/WriteStream` (#4378)
* use `options.fd` over path * tests * fix `@clack/prompts` * == null
Diffstat (limited to 'src/js/node')
-rw-r--r--src/js/node/fs.js64
-rw-r--r--src/js/node/tty.js8
2 files changed, 38 insertions, 34 deletions
diff --git a/src/js/node/fs.js b/src/js/node/fs.js
index 9cc0748cb..a6b1adb88 100644
--- a/src/js/node/fs.js
+++ b/src/js/node/fs.js
@@ -345,7 +345,7 @@ var kIoDone = Symbol.for("kIoDone");
var defaultReadStreamOptions = {
file: undefined,
- fd: undefined,
+ fd: null,
flags: "r",
encoding: undefined,
mode: 0o666,
@@ -408,6 +408,7 @@ ReadStream = (function (InternalReadStream) {
autoDestroy = defaultReadStreamOptions.autoClose,
fs = defaultReadStreamOptions.fs,
highWaterMark = defaultReadStreamOptions.highWaterMark,
+ fd = defaultReadStreamOptions.fd,
} = options;
if (pathOrFd?.constructor?.name === "URL") {
@@ -416,7 +417,13 @@ ReadStream = (function (InternalReadStream) {
// This is kinda hacky but we create a temporary object to assign props that we will later pull into the `this` context after we call super
var tempThis = {};
- if (typeof pathOrFd === "string") {
+ if (fd != null) {
+ if (typeof fd !== "number") {
+ throw new TypeError("Expected options.fd to be a number");
+ }
+ tempThis.fd = tempThis[readStreamPathOrFdSymbol] = fd;
+ tempThis.autoClose = false;
+ } else if (typeof pathOrFd === "string") {
if (pathOrFd.startsWith("file://")) {
pathOrFd = Bun.fileURLToPath(pathOrFd);
}
@@ -437,7 +444,7 @@ ReadStream = (function (InternalReadStream) {
}
// If fd not open for this file, open it
- if (!tempThis.fd) {
+ if (tempThis.fd === undefined) {
// NOTE: this fs is local to constructor, from options
tempThis.fd = fs.openSync(pathOrFd, flags, mode);
}
@@ -756,7 +763,13 @@ WriteStream = (function (InternalWriteStream) {
} = options;
var tempThis = {};
- if (typeof path === "string") {
+ if (fd != null) {
+ if (typeof fd !== "number") {
+ throw new Error("Expected options.fd to be a number");
+ }
+ tempThis.fd = fd;
+ tempThis[writeStreamPathFastPathSymbol] = false;
+ } else if (typeof path === "string") {
if (path.length === 0) {
throw new TypeError("Expected a non-empty path");
}
@@ -772,12 +785,9 @@ WriteStream = (function (InternalWriteStream) {
(start === undefined || start === 0) &&
fs.write === defaultWriteStreamOptions.fs.write &&
fs.close === defaultWriteStreamOptions.fs.close;
- } else {
- tempThis.fd = fd;
- tempThis[writeStreamPathFastPathSymbol] = false;
}
- if (!tempThis.fd) {
+ if (tempThis.fd == null) {
tempThis.fd = fs.openSync(path, flags, mode);
}
@@ -960,30 +970,20 @@ WriteStream = (function (InternalWriteStream) {
// TODO: Replace this when something like lseek is available
var native = this.pos === undefined;
+ const callback = native
+ ? (err, bytes) => {
+ this[kIoDone] = false;
+ this.#handleWrite(err, bytes);
+ this.emit(kIoDone);
+ if (cb) !err ? cb() : cb(err);
+ }
+ : () => {};
this[kIoDone] = true;
- return super.write(
- chunk,
- encoding,
- native
- ? (err, bytes) => {
- this[kIoDone] = false;
- this.#handleWrite(err, bytes);
- this.emit(kIoDone);
- if (cb) !err ? cb() : cb(err);
- }
- : () => {},
- native,
- );
- }
-
- #internalWriteSlow(chunk, encoding, cb) {
- this.#fs.write(this.fd, chunk, 0, chunk.length, this.pos, (err, bytes) => {
- this[kIoDone] = false;
- this.#handleWrite(err, bytes);
- this.emit(kIoDone);
-
- !err ? cb() : cb(err);
- });
+ if (this._write) {
+ return this._write(chunk, encoding, callback);
+ } else {
+ return super.write(chunk, encoding, callback, native);
+ }
}
end(chunk, encoding, cb) {
@@ -991,7 +991,7 @@ WriteStream = (function (InternalWriteStream) {
return super.end(chunk, encoding, cb, native);
}
- _write = this.#internalWriteSlow;
+ _write = undefined;
_writev = undefined;
get pending() {
diff --git a/src/js/node/tty.js b/src/js/node/tty.js
index 09010e8fc..d5645c6da 100644
--- a/src/js/node/tty.js
+++ b/src/js/node/tty.js
@@ -4,7 +4,9 @@ function ReadStream(fd) {
if (!(this instanceof ReadStream)) return new ReadStream(fd);
if (fd >> 0 !== fd || fd < 0) throw new RangeError("fd must be a positive integer");
- const stream = require("node:fs").ReadStream.call(this, `/dev/fd/${fd}`);
+ const stream = require("node:fs").ReadStream.call(this, "", {
+ fd,
+ });
stream.isRaw = false;
stream.isTTY = isatty(stream.fd);
@@ -93,7 +95,9 @@ function WriteStream(fd) {
if (!(this instanceof WriteStream)) return new WriteStream(fd);
if (fd >> 0 !== fd || fd < 0) throw new RangeError("fd must be a positive integer");
- const stream = require("node:fs").WriteStream.call(this, `/dev/fd/${fd}`);
+ const stream = require("node:fs").WriteStream.call(this, "", {
+ fd,
+ });
stream.columns = undefined;
stream.rows = undefined;