Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PersistentFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class PersistentFile extends EventEmitter {
this.hash.update(buffer);
}

if (this._writeStream.closed) {
if (this._writeStream.closed || this._writeStream.destroyed) {
cb();
return;
}
Expand Down
33 changes: 33 additions & 0 deletions test-node/files/persistent-file.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { strictEqual } from "node:assert";
import { once } from "node:events";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
import PersistentFile from "../../src/PersistentFile.js";

test("does not write after the file stream is destroyed", async () => {
const directory = await mkdtemp(join(tmpdir(), "formidable-destroyed-"));

try {
const file = new PersistentFile({
filepath: join(directory, "upload"),
newFilename: "upload",
originalFilename: "upload",
mimetype: "application/octet-stream",
});

file.open();
await once(file._writeStream, "open");
file.destroy();

strictEqual(file._writeStream.destroyed, true);
strictEqual(file._writeStream.closed, false);

await new Promise((resolve) => file.write(Buffer.from("ignored"), resolve));

strictEqual(file.size, 0);
} finally {
await rm(directory, { recursive: true, force: true });
}
});