Skip to content

Navigation Menu

Sign in
Sign up

Commit 26e32ec

Browse files
fix createWriteStream (oven-sh#1433)
* fix createWriteStream * remove comment
1 parent 3a60764 commit 26e32ec

2 files changed

Lines changed: 104 additions & 9 deletions

File tree

‎src/bun.js/fs.exports.js‎

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ function getLazyReadStream() {
339339
callback();
340340
return;
341341
}
342-
// this[readStreamPathFastPathSymbol] = false;
343342
var { path, flags, mode } = this;
344343

345344
this.#fs.open(path, flags, mode, (er, fd) => {
@@ -625,7 +624,7 @@ function getLazyWriteStream() {
625624
this._writableState.autoDestroy = val;
626625
}
627626

628-
destroySoon = this.end;
627+
destroySoon = this.end;// TODO: what is this for?
629628

630629
// noop, node has deprecated this
631630
open() {}
@@ -696,6 +695,26 @@ function getLazyWriteStream() {
696695
});
697696
}
698697

698+
_construct(callback) {
699+
if (typeof this.fd === "number") {
700+
callback();
701+
return;
702+
}
703+
var { path, flags, mode } = this;
704+
705+
this.#fs.open(path, flags, mode, (er, fd) => {
706+
if (er) {
707+
callback(er);
708+
return;
709+
}
710+
711+
this.fd = fd;
712+
callback();
713+
this.emit("open", this.fd);
714+
this.emit("ready");
715+
});
716+
}
717+
699718
_destroy(err, cb) {
700719
if (this.fd === null) {
701720
return cb(err);
@@ -745,7 +764,7 @@ function getLazyWriteStream() {
745764
chunk.length,
746765
this.pos,
747766
(err, bytes) => {
748-
ths[kIoDone] = false;
767+
this[kIoDone] = false;
749768
this.#handleWrite(err, bytes);
750769
this.emit(kIoDone);
751770

@@ -754,12 +773,19 @@ function getLazyWriteStream() {
754773
);
755774
} else {
756775
this[kIoDone] = true;
757-
this.#fs.write(this.fd, chunk, 0, chunk.length, null, (err, bytes) => {
758-
ths[kIoDone] = false;
759-
this.#handleWrite(err, bytes);
760-
this.emit(kIoDone);
761-
!err ? cb() : cb(err);
762-
});
776+
this.#fs.write(
777+
this.fd,
778+
chunk,
779+
0,
780+
chunk.length,
781+
null,
782+
(err, bytes, buffer) => {
783+
this[kIoDone] = false;
784+
this.#handleWrite(err, bytes);
785+
this.emit(kIoDone);
786+
!err ? cb() : cb(err);
787+
}
788+
);
763789
}
764790
}
765791
_write = this.#internalWrite;

‎test/bun.js/fs.test.js‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
copyFileSync,
1717
rmSync,
1818
createReadStream,
19+
createWriteStream,
1920
} from "node:fs";
2021
import { join } from "node:path";
2122

@@ -462,3 +463,71 @@ describe("createReadStream", () => {
462463
});
463464
});
464465
});
466+
467+
describe("createWriteStream", () => {
468+
it("simple write stream finishes", async () => {
469+
const path = `/tmp/fs.test.js/${Date.now()}.createWriteStream.txt`;
470+
const stream = createWriteStream(path);
471+
stream.write("Test file written successfully");
472+
stream.end();
473+
474+
return await new Promise((resolve, reject) => {
475+
stream.on("error", (e) => {
476+
reject(e);
477+
});
478+
479+
stream.on("finish", () => {
480+
expect(readFileSync(path, "utf8")).toBe(
481+
"Test file written successfully"
482+
);
483+
resolve(true);
484+
});
485+
});
486+
});
487+
488+
it("writing null throws ERR_STREAM_NULL_VALUES", async () => {
489+
const path = `/tmp/fs.test.js/${Date.now()}.createWriteStreamNulls.txt`;
490+
const stream = createWriteStream(path);
491+
try {
492+
stream.write(null);
493+
throw new Error("should not get here");
494+
} catch (exception) {
495+
expect(exception.code).toBe("ERR_STREAM_NULL_VALUES");
496+
}
497+
});
498+
499+
it("writing null with objectMode: true throws ERR_STREAM_NULL_VALUES", async () => {
500+
const path = `/tmp/fs.test.js/${Date.now()}.createWriteStreamNulls.txt`;
501+
const stream = createWriteStream(path, {
502+
objectMode: true,
503+
});
504+
try {
505+
stream.write(null);
506+
throw new Error("should not get here");
507+
} catch (exception) {
508+
expect(exception.code).toBe("ERR_STREAM_NULL_VALUES");
509+
}
510+
});
511+
512+
it("writing false throws ERR_INVALID_ARG_TYPE", async () => {
513+
const path = `/tmp/fs.test.js/${Date.now()}.createWriteStreamFalse.txt`;
514+
const stream = createWriteStream(path);
515+
try {
516+
stream.write(false);
517+
throw new Error("should not get here");
518+
} catch (exception) {
519+
expect(exception.code).toBe("ERR_INVALID_ARG_TYPE");
520+
}
521+
});
522+
523+
it("writing false with objectMode: true should not throw", async () => {
524+
const path = `/tmp/fs.test.js/${Date.now()}.createWriteStreamFalse.txt`;
525+
const stream = createWriteStream(path, {
526+
objectMode: true,
527+
});
528+
stream.write(false);
529+
stream.on("error", () => {
530+
throw new Error("should not get here");
531+
});
532+
});
533+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /