import { Writable } from "stream"; import fs from "fs"; class FileWriteStream extends…
- User
import { Writable } from "stream"; import fs from "fs";
class FileWriteStream extends Writable { constructor({ highWaterMark, fileName }) { super({ highWaterMark });
this.fileName = fileName; this.fd = null; this.chunks = []; this.chunksSize = 0; this.writesCount = 0; this.errorHappened = false;}
// This will run after the constructor, and it will put off calling all the other // methods until we call the callback function _construct(callback) { fs.open(this.fileName, "w", (err, fd) => { if (err) { // so if we call the callback with an argument, it means that we have an error // and we should not proceed callback(err); } else { this.fd = fd; // no argument means it was successful callback(); } }); }
_write(chunk, encoding, callback) { this.chunks.push(chunk); this.chunksSize += chunk.length;
if (this.chunksSize > this.writableHighWaterMark) { fs.write(this.fd, Buffer.concat(this.chunks), (err) => { if (err) { return callback(new Error("you make wrong")); } this.chunks = []; this.chunksSize = 0; ++this.writesCount; callback(); }); } else { // when we're done, we should call the callback function callback(); }}
// My main question: if i comment _destroy method out, I would get: /* Number of writes: 0 hi --- STREAM ERROR EVENT CAUGHT --- EBADF: bad file descriptor, close / // And if I uncomment it out, I would get: /--- STREAM ERROR EVENT CAUGHT --- you make wrong */
// _destroy(error, callback) { // console.log("Number of writes:", this.writesCount); // if (this.fd) { // fs.close(this.fd, (err) => { // callback(err || error); // }); // } else { // callback(error); // } // } } const stream = new FileWriteStream({ highWaterMark: 20, fileName: "test.txt", });
stream.on("error", (error) => { console.log("--- STREAM ERROR EVENT CAUGHT ---", error.message); });
stream.write("hello world"); // 11 bytes, under highWaterMark
setTimeout(() => { fs.closeSync(stream.fd); }, 100);
setTimeout(() => { stream.write("this write will fail"); }, 200);
- Assistant
Here’s what’s going on under the hood, why you’re seeing two different error messages, and how you can implement a “textbook”
_destroy(and even a_final) so that you:- only ever close your file descriptor once
- get the right error surfaced
- flush any remaining buffered chunks at the end
Why the two different errors?
-
NO custom
_destroy- You manually
fs.closeSync(stream.fd)at 100 ms. - At ~200 ms you call
stream.write(…)→ inside your_writeyou do anfs.write(this.fd, …, (err) => { … }). - Since the FD is already closed, the first error you get is the raw
EBADF: bad file descriptor, close. - You wrap it in
callback(new Error("you make wrong")), but then the default implementation of.destroy()will try to close the FD again, get anotherEBADF, and that ends up being the error that makes it to the'error'listener.
- You manually
-
WITH custom
_destroy- You still get the write-time EBADF, but you immediately wrap it as
new Error("you make wrong"), so that’s the error you pass into your own_destroy. - You close the FD in your
_destroy, so there’s no “second” EBADF. - The one you see in your
'error'handler is your wrapped"you make wrong".
- You still get the write-time EBADF, but you immediately wrap it as
A “correct” implementation
import { Writable } from "stream"; import fs from "fs"; class FileWriteStream extends Writable { constructor({ highWaterMark, fileName }) { super({ highWaterMark }); this.fileName = fileName; this.fd = null; this.chunks = []; this.chunksSize = 0; this.writesCount = 0; } // 1) lazy‐open the file _construct(cb) { fs.open(this.fileName, "w", (err, fd) => { if (err) return cb(err); this.fd = fd; cb(); }); } // 2) buffer up writes _write(chunk, enc, cb) { this.chunks.push(chunk); this.chunksSize += chunk.length; if (this.chunksSize > this.writableHighWaterMark) { const buf = Buffer.concat(this.chunks); fs.write(this.fd, buf, (err) => { if (err) { // wrap or forward the fs.write error return cb(new Error(`write failed: ${err.message}`)); } this.writesCount++; this.chunks = []; this.chunksSize = 0; cb(); }); } else { cb(); } } // 3) when the stream ends, flush any remaining buffered chunks _final(cb) { if (this.chunksSize > 0) { const buf = Buffer.concat(this.chunks); fs.write(this.fd, buf, (err) => { if (!err) this.writesCount++; cb(err); }); } else { cb(); } } // 4) tear down: close fd exactly once, and propagate *either* the // write‐error or the close‐error _destroy(err, cb) { // clear it so we don’t double-close const fd = this.fd; this.fd = null; console.log("Number of fs.write calls:", this.writesCount); if (fd == null) { // nothing to close cb(err); } else { fs.close(fd, (closeErr) => { // if closing failed, report that; otherwise report the original error cb(closeErr || err); }); } } } // ——— Usage ——— const stream = new FileWriteStream({ highWaterMark: 20, fileName: "test.txt", }); stream.on("error", (err) => { console.error("STREAM ERROR:", err.message); }); // normal write stream.write("hello world"); // gracefully end the stream in 50 ms setTimeout(() => { stream.end("…and some final data"); }, 50); // now you do NOT manually fs.closeSync(stream.fd)! // Let the stream’s own _final + _destroy close it.What changed?
-
We added
_final(cb)
This guarantees that when someone calls.end(), any leftover chunks get flushed. -
We re-wrote
_destroy(err, cb)- We grab and null out
this.fdso it can’t be closed twice. - We report either the close-error (if it happens) or the original stream error.
- We grab and null out
-
We never manually
fs.closeSync(stream.fd)
You should either call.destroy()on the stream or let.end()+ your_destroydo the cleanup.
With this pattern:
• one close call
• correct error bubbling
• proper final flush…so you’ll consistently see your real write-error (or close-error), instead of a mysterious EBADF from double-closing.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.