From 95e455d6d156aac08b9f4240b437157873a8c968 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Wed, 22 Nov 2023 23:59:53 +0900 Subject: [PATCH 1/3] fix(ext/node): fix stream.Writable port https://github.com/nodejs/node/pull/46818 --- ext/node/polyfills/_stream.mjs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ext/node/polyfills/_stream.mjs b/ext/node/polyfills/_stream.mjs index 26d5fd30a32c43..23df11ab3dd11f 100644 --- a/ext/node/polyfills/_stream.mjs +++ b/ext/node/polyfills/_stream.mjs @@ -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) { From c8f2e21ee2d03ab208212c270639795ae8e6e13d Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Thu, 23 Nov 2023 00:15:08 +0900 Subject: [PATCH 2/3] add test --- cli/tests/unit_node/stream_test.ts | 37 +++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/cli/tests/unit_node/stream_test.ts b/cli/tests/unit_node/stream_test.ts index 058d3ca7c6e412..1a6dae0775ad30 100644 --- a/cli/tests/unit_node/stream_test.ts +++ b/cli/tests/unit_node/stream_test.ts @@ -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 () => { @@ -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(); + } +}) From 778522aae984c2402ea084c796aac15a7f69b2ed Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Thu, 23 Nov 2023 00:26:54 +0900 Subject: [PATCH 3/3] fmt --- cli/tests/unit_node/stream_test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/tests/unit_node/stream_test.ts b/cli/tests/unit_node/stream_test.ts index 1a6dae0775ad30..e63171df7b6ab1 100644 --- a/cli/tests/unit_node/stream_test.ts +++ b/cli/tests/unit_node/stream_test.ts @@ -39,7 +39,7 @@ Deno.test("stream.Writable does not change the order of items", async () => { write(chunk, _, cb) { chunks.push(chunk); cb(); - } + }, }); for (const i of Array(20).keys()) { @@ -49,7 +49,7 @@ Deno.test("stream.Writable does not change the order of items", async () => { if (chunks[0][0] !== 0) { // The first chunk is swapped with the later chunk. - fail("The first chunk is swapped") + fail("The first chunk is swapped"); } } @@ -57,4 +57,4 @@ Deno.test("stream.Writable does not change the order of items", async () => { // Run it multiple times to avoid flaky false negative. await test(); } -}) +});