From 5d0946ad0e22d042be880fc2dd03a7bac06f6dab Mon Sep 17 00:00:00 2001 From: Erick Wendel Date: Fri, 20 Jan 2023 09:37:52 -0300 Subject: [PATCH] stream: fix readable stream as async iterator function Since v19.2 it's not possible to use readableStreams as async iterators (confirmed bug). This patch fixes the problem by reading the Stream.Duplex property from 'streams/duplex' instead of 'streams/legacy' module Fixes: https://github.com/nodejs/node/issues/46141 PR-URL: https://github.com/nodejs/node/pull/46147 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Robert Nagy Reviewed-By: Matteo Collina --- lib/stream/promises.js | 2 ++ .../test-stream3-pipeline-async-iterator.js | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 test/parallel/test-stream3-pipeline-async-iterator.js diff --git a/lib/stream/promises.js b/lib/stream/promises.js index 7a896f87b14392..512012860f4a7a 100644 --- a/lib/stream/promises.js +++ b/lib/stream/promises.js @@ -13,6 +13,8 @@ const { const { pipelineImpl: pl } = require('internal/streams/pipeline'); const { finished } = require('internal/streams/end-of-stream'); +require('stream'); + function pipeline(...streams) { return new Promise((resolve, reject) => { let signal; diff --git a/test/parallel/test-stream3-pipeline-async-iterator.js b/test/parallel/test-stream3-pipeline-async-iterator.js new file mode 100644 index 00000000000000..ad1e4647777bcd --- /dev/null +++ b/test/parallel/test-stream3-pipeline-async-iterator.js @@ -0,0 +1,27 @@ +/* eslint-disable node-core/require-common-first, require-yield */ +'use strict'; +const { pipeline } = require('node:stream/promises'); +{ + // Ensure that async iterators can act as readable and writable streams + async function* myCustomReadable() { + yield 'Hello'; + yield 'World'; + } + + const messages = []; + async function* myCustomWritable(stream) { + for await (const chunk of stream) { + messages.push(chunk); + } + } + + (async () => { + await pipeline( + myCustomReadable, + myCustomWritable, + ); + // Importing here to avoid initializing streams + require('assert').deepStrictEqual(messages, ['Hello', 'World']); + })() + .then(require('../common').mustCall()); +}