Skip to content

Commit 545d6d1

Browse files
kt3kcrowlKats
authored andcommitted
fix(ext/node): fix node:stream.Writable (#21297)
This change applies the same fix as nodejs/node#46818, and the original example given in #20456 works as expected. closes #20456 (cherry picked from commit bf42467)
1 parent cad998b commit 545d6d1

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

cli/tests/unit_node/stream_test.ts

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
22

3-
import { assert } from "../../../test_util/std/testing/asserts.ts";
3+
import { assert, fail } from "../../../test_util/std/testing/asserts.ts";
44
import { fromFileUrl, relative } from "../../../test_util/std/path/mod.ts";
55
import { pipeline } from "node:stream/promises";
6+
import { Writable } from "node:stream";
67
import { createReadStream, createWriteStream } from "node:fs";
78

89
Deno.test("stream/promises pipeline", async () => {
@@ -23,3 +24,37 @@ Deno.test("stream/promises pipeline", async () => {
2324
// pass
2425
}
2526
});
27+
28+
// TODO(kt3k): Remove this test case when the node compat test suite is
29+
// updated to version 18.16.0 or above.
30+
// The last case in parallel/test-stream2-transform.js covers this case.
31+
// See https://github.com/nodejs/node/pull/46818
32+
Deno.test("stream.Writable does not change the order of items", async () => {
33+
async function test() {
34+
const chunks: Uint8Array[] = [];
35+
const writable = new Writable({
36+
construct(cb) {
37+
setTimeout(cb, 10);
38+
},
39+
write(chunk, _, cb) {
40+
chunks.push(chunk);
41+
cb();
42+
},
43+
});
44+
45+
for (const i of Array(20).keys()) {
46+
writable.write(Uint8Array.from([i]));
47+
await new Promise((resolve) => setTimeout(resolve, 1));
48+
}
49+
50+
if (chunks[0][0] !== 0) {
51+
// The first chunk is swapped with the later chunk.
52+
fail("The first chunk is swapped");
53+
}
54+
}
55+
56+
for (const _ of Array(10)) {
57+
// Run it multiple times to avoid flaky false negative.
58+
await test();
59+
}
60+
});

ext/node/polyfills/_stream.mjs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1669,9 +1669,11 @@ var require_destroy = __commonJS({
16691669
}
16701670
}
16711671
try {
1672-
stream._construct(onConstruct);
1672+
stream._construct((err) => {
1673+
nextTick(onConstruct, err);
1674+
});
16731675
} catch (err) {
1674-
onConstruct(err);
1676+
nextTick(onConstruct, err);
16751677
}
16761678
}
16771679
function emitConstructNT(stream) {

0 commit comments

Comments
 (0)