aboutsummaryrefslogtreecommitdiff
path: root/src/js/node/fs.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/node/fs.js')
-rw-r--r--src/js/node/fs.js64
1 files changed, 32 insertions, 32 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() {