aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Lawrence Chen <54008264+lawrencecchen@users.noreply.github.com> 2022-11-01 02:26:51 -0700
committerGravatar GitHub <noreply@github.com> 2022-11-01 02:26:51 -0700
commit26e32ec1cb4951f60eee0b9fc190fd2aabec149e (patch)
treee760c812af33d1276369dba6497b53a7c106e4a6
parent3a60764f32bf7aaf3d2ec6cb84d81e8384bd2417 (diff)
downloadbun-26e32ec1cb4951f60eee0b9fc190fd2aabec149e.tar.gz
bun-26e32ec1cb4951f60eee0b9fc190fd2aabec149e.tar.zst
bun-26e32ec1cb4951f60eee0b9fc190fd2aabec149e.zip
fix createWriteStream (#1433)
* fix createWriteStream * remove comment
-rw-r--r--src/bun.js/fs.exports.js44
-rw-r--r--test/bun.js/fs.test.js69
2 files changed, 104 insertions, 9 deletions
diff --git a/src/bun.js/fs.exports.js b/src/bun.js/fs.exports.js
index 019a98d6f..ebf929cb1 100644
--- a/src/bun.js/fs.exports.js
+++ b/src/bun.js/fs.exports.js
@@ -339,7 +339,6 @@ function getLazyReadStream() {
callback();
return;
}
- // this[readStreamPathFastPathSymbol] = false;
var { path, flags, mode } = this;
this.#fs.open(path, flags, mode, (er, fd) => {
@@ -625,7 +624,7 @@ function getLazyWriteStream() {
this._writableState.autoDestroy = val;
}
- destroySoon = this.end;
+ destroySoon = this.end; // TODO: what is this for?
// noop, node has deprecated this
open() {}
@@ -696,6 +695,26 @@ function getLazyWriteStream() {
});
}
+ _construct(callback) {
+ if (typeof this.fd === "number") {
+ callback();
+ return;
+ }
+ var { path, flags, mode } = this;
+
+ this.#fs.open(path, flags, mode, (er, fd) => {
+ if (er) {
+ callback(er);
+ return;
+ }
+
+ this.fd = fd;
+ callback();
+ this.emit("open", this.fd);
+ this.emit("ready");
+ });
+ }
+
_destroy(err, cb) {
if (this.fd === null) {
return cb(err);
@@ -745,7 +764,7 @@ function getLazyWriteStream() {
chunk.length,
this.pos,
(err, bytes) => {
- ths[kIoDone] = false;
+ this[kIoDone] = false;
this.#handleWrite(err, bytes);
this.emit(kIoDone);
@@ -754,12 +773,19 @@ function getLazyWriteStream() {
);
} else {
this[kIoDone] = true;
- this.#fs.write(this.fd, chunk, 0, chunk.length, null, (err, bytes) => {
- ths[kIoDone] = false;
- this.#handleWrite(err, bytes);
- this.emit(kIoDone);
- !err ? cb() : cb(err);
- });
+ this.#fs.write(
+ this.fd,
+ chunk,
+ 0,
+ chunk.length,
+ null,
+ (err, bytes, buffer) => {
+ this[kIoDone] = false;
+ this.#handleWrite(err, bytes);
+ this.emit(kIoDone);
+ !err ? cb() : cb(err);
+ }
+ );
}
}
_write = this.#internalWrite;
diff --git a/test/bun.js/fs.test.js b/test/bun.js/fs.test.js
index e2d305f01..3976ba372 100644
--- a/test/bun.js/fs.test.js
+++ b/test/bun.js/fs.test.js
@@ -16,6 +16,7 @@ import {
copyFileSync,
rmSync,
createReadStream,
+ createWriteStream,
} from "node:fs";
import { join } from "node:path";
@@ -462,3 +463,71 @@ describe("createReadStream", () => {
});
});
});
+
+describe("createWriteStream", () => {
+ it("simple write stream finishes", async () => {
+ const path = `/tmp/fs.test.js/${Date.now()}.createWriteStream.txt`;
+ const stream = createWriteStream(path);
+ stream.write("Test file written successfully");
+ stream.end();
+
+ return await new Promise((resolve, reject) => {
+ stream.on("error", (e) => {
+ reject(e);
+ });
+
+ stream.on("finish", () => {
+ expect(readFileSync(path, "utf8")).toBe(
+ "Test file written successfully"
+ );
+ resolve(true);
+ });
+ });
+ });
+
+ it("writing null throws ERR_STREAM_NULL_VALUES", async () => {
+ const path = `/tmp/fs.test.js/${Date.now()}.createWriteStreamNulls.txt`;
+ const stream = createWriteStream(path);
+ try {
+ stream.write(null);
+ throw new Error("should not get here");
+ } catch (exception) {
+ expect(exception.code).toBe("ERR_STREAM_NULL_VALUES");
+ }
+ });
+
+ it("writing null with objectMode: true throws ERR_STREAM_NULL_VALUES", async () => {
+ const path = `/tmp/fs.test.js/${Date.now()}.createWriteStreamNulls.txt`;
+ const stream = createWriteStream(path, {
+ objectMode: true,
+ });
+ try {
+ stream.write(null);
+ throw new Error("should not get here");
+ } catch (exception) {
+ expect(exception.code).toBe("ERR_STREAM_NULL_VALUES");
+ }
+ });
+
+ it("writing false throws ERR_INVALID_ARG_TYPE", async () => {
+ const path = `/tmp/fs.test.js/${Date.now()}.createWriteStreamFalse.txt`;
+ const stream = createWriteStream(path);
+ try {
+ stream.write(false);
+ throw new Error("should not get here");
+ } catch (exception) {
+ expect(exception.code).toBe("ERR_INVALID_ARG_TYPE");
+ }
+ });
+
+ it("writing false with objectMode: true should not throw", async () => {
+ const path = `/tmp/fs.test.js/${Date.now()}.createWriteStreamFalse.txt`;
+ const stream = createWriteStream(path, {
+ objectMode: true,
+ });
+ stream.write(false);
+ stream.on("error", () => {
+ throw new Error("should not get here");
+ });
+ });
+});