Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): fix node:stream.Writable #21297

Merged
merged 3 commits into from
Nov 23, 2023
Merged
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
37 changes: 36 additions & 1 deletion cli/tests/unit_node/stream_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { assert } from "../../../test_util/std/testing/asserts.ts";
import { assert, fail } from "../../../test_util/std/testing/asserts.ts";
import { fromFileUrl, relative } from "../../../test_util/std/path/mod.ts";
import { pipeline } from "node:stream/promises";
import { Writable } from "node:stream";
import { createReadStream, createWriteStream } from "node:fs";

Deno.test("stream/promises pipeline", async () => {
Expand All @@ -23,3 +24,37 @@ Deno.test("stream/promises pipeline", async () => {
// pass
}
});

// TODO(kt3k): Remove this test case when the node compat test suite is
// updated to version 18.16.0 or above.
// The last case in parallel/test-stream2-transform.js covers this case.
// See https://github.com/nodejs/node/pull/46818
Deno.test("stream.Writable does not change the order of items", async () => {
async function test() {
const chunks: Uint8Array[] = [];
const writable = new Writable({
construct(cb) {
setTimeout(cb, 10);
},
write(chunk, _, cb) {
chunks.push(chunk);
cb();
},
});

for (const i of Array(20).keys()) {
writable.write(Uint8Array.from([i]));
await new Promise((resolve) => setTimeout(resolve, 1));
}

if (chunks[0][0] !== 0) {
// The first chunk is swapped with the later chunk.
fail("The first chunk is swapped");
}
}

for (const _ of Array(10)) {
// Run it multiple times to avoid flaky false negative.
await test();
}
});
6 changes: 4 additions & 2 deletions ext/node/polyfills/_stream.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1669,9 +1669,11 @@ var require_destroy = __commonJS({
}
}
try {
stream._construct(onConstruct);
stream._construct((err) => {
nextTick(onConstruct, err);
});
} catch (err) {
onConstruct(err);
nextTick(onConstruct, err);
}
}
function emitConstructNT(stream) {
Expand Down