-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <benjamingr@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
- Loading branch information
1 parent
cb5bb12
commit 3acfe9b
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} |