Fix coredump when reading an empty file(node:stream:createReadStream) (#3882)

* Fix coredump when reading an empty file.
Close: #3607

* It seems that the fd is closed when `auto_close` is true.
This commit is contained in:
Ai Hoshino
2023-07-30 16:18:13 +08:00
committed by GitHub
parent 40a9ba6340
commit 413fd28120
2 changed files with 30 additions and 0 deletions

View File

@@ -4433,6 +4433,10 @@ pub const FileReader = struct {
var readable_file = File{ .loop = this.globalThis().bunVM().eventLoop() };
const result = readable_file.start(&blob.data.file);
if (result == .empty) {
this.lazy_readable = .{ .empty = {} };
return result;
}
if (result != .ready) {
return result;
}

View File

@@ -41,6 +41,32 @@ describe("Readable", () => {
readable.pipe(writable);
});
it("should be able to be piped via .pipe, issue #3607", done => {
const path = `${tmpdir()}/${Date.now()}.testReadStreamEmptyFile.txt`;
writeFileSync(path, "");
const stream = createReadStream(path);
stream.on("error", err => {
done(err);
});
let called = false;
const writable = new Writable({
write(chunk, encoding, callback) {
called = true;
callback();
},
});
writable.on("finish", () => {
try {
expect(called).toBeFalse();
} catch (err) {
return done(err);
}
done();
});
stream.pipe(writable);
});
it("should be able to be piped via .pipe, issue #3668", done => {
const path = `${tmpdir()}/${Date.now()}.testReadStream.txt`;
writeFileSync(path, "12345");