Skip to content

Commit 9675863

Browse files
ErickWendelruyadorno
authored andcommitted
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: #46141 PR-URL: #46147 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 5ad6c20 commit 9675863

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/stream/promises.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const {
1313
const { pipelineImpl: pl } = require('internal/streams/pipeline');
1414
const { finished } = require('internal/streams/end-of-stream');
1515

16+
require('stream');
17+
1618
function pipeline(...streams) {
1719
return new Promise((resolve, reject) => {
1820
let signal;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable node-core/require-common-first, require-yield */
2+
'use strict';
3+
const { pipeline } = require('node:stream/promises');
4+
{
5+
// Ensure that async iterators can act as readable and writable streams
6+
async function* myCustomReadable() {
7+
yield 'Hello';
8+
yield 'World';
9+
}
10+
11+
const messages = [];
12+
async function* myCustomWritable(stream) {
13+
for await (const chunk of stream) {
14+
messages.push(chunk);
15+
}
16+
}
17+
18+
(async () => {
19+
await pipeline(
20+
myCustomReadable,
21+
myCustomWritable,
22+
);
23+
// Importing here to avoid initializing streams
24+
require('assert').deepStrictEqual(messages, ['Hello', 'World']);
25+
})()
26+
.then(require('../common').mustCall());
27+
}

0 commit comments

Comments
 (0)