Skip to content

Commit 217c82e

Browse files
kt3ksatyarohith
authored andcommitted
fix(ext/node): out-of-order writes of fs.createWriteStream (#23244)
This PR follows this fix (nodejs/node#52005) in Node.js. Stream's construct callback happens one tick earlier by this change, and it prevents the reordering of the first few chunks in `node:stream.Writable` closes #20284
1 parent 255411e commit 217c82e

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

ext/node/polyfills/_stream.mjs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,7 @@ var require_destroy = __commonJS({
16651665
} else if (err) {
16661666
errorOrDestroy(stream, err, true);
16671667
} else {
1668-
process.nextTick(emitConstructNT, stream);
1668+
stream.emit(kConstruct);
16691669
}
16701670
}
16711671
try {
@@ -1676,9 +1676,6 @@ var require_destroy = __commonJS({
16761676
nextTick(onConstruct, err);
16771677
}
16781678
}
1679-
function emitConstructNT(stream) {
1680-
stream.emit(kConstruct);
1681-
}
16821679
function isRequest(stream) {
16831680
return stream && stream.setHeader && typeof stream.abort === "function";
16841681
}

tests/unit_node/fs_test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { join } from "node:path";
55
import { tmpdir } from "node:os";
66
import {
77
constants,
8+
createWriteStream,
89
existsSync,
910
mkdtempSync,
1011
promises,
@@ -14,6 +15,7 @@ import {
1415
writeFileSync,
1516
} from "node:fs";
1617
import { constants as fsPromiseConstants, cp } from "node:fs/promises";
18+
import process from "node:process";
1719
import { pathToAbsoluteFileUrl } from "../unit/test_util.ts";
1820

1921
Deno.test(
@@ -121,3 +123,36 @@ Deno.test(
121123
assert(dataRead === "Hello");
122124
},
123125
);
126+
127+
// TODO(kt3k): Delete this test case, and instead enable the compat case
128+
// `test/parallel/test-fs-writestream-open-write.js`, when we update
129+
// `tests/node_compat/runner/suite`.
130+
Deno.test("[node/fs createWriteStream", async () => {
131+
const { promise, resolve, reject } = Promise.withResolvers<void>();
132+
const tempDir = await Deno.makeTempDir();
133+
const file = join(tempDir, "file.txt");
134+
try {
135+
const w = createWriteStream(file);
136+
137+
w.on("open", () => {
138+
w.write("hello, ");
139+
140+
process.nextTick(() => {
141+
w.write("world");
142+
w.end();
143+
});
144+
});
145+
146+
w.on("close", async () => {
147+
try {
148+
assertEquals(await Deno.readTextFile(file), "hello, world");
149+
resolve();
150+
} catch (e) {
151+
reject(e);
152+
}
153+
});
154+
await promise;
155+
} finally {
156+
await Deno.remove(tempDir, { recursive: true });
157+
}
158+
});

0 commit comments

Comments
 (0)