Skip to content

Commit

Permalink
stream: fix readable stream as async iterator function
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ErickWendel committed Jan 9, 2023
1 parent 80fa25b commit 0e07fc8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Readable.ReadableState = ReadableState;
const EE = require('events');
const { Stream, prependListener } = require('internal/streams/legacy');
const { Buffer } = require('buffer');
const Duplex = require('internal/streams/duplex');

const {
addAbortSignal,
Expand Down Expand Up @@ -87,7 +88,7 @@ function ReadableState(options, stream, isDuplex) {
// values for the readable and the writable sides of the duplex stream.
// These options can be provided separately as readableXXX and writableXXX.
if (typeof isDuplex !== 'boolean')
isDuplex = stream instanceof Stream.Duplex;
isDuplex = stream instanceof Duplex;

// Object stream flag. Used to make read(n) ignore n and to
// make all the buffer merging and length checks go away.
Expand Down Expand Up @@ -189,7 +190,7 @@ function Readable(options) {

// Checking for a Stream.Duplex instance is faster here instead of inside
// the ReadableState constructor, at least with V8 6.5.
const isDuplex = this instanceof Stream.Duplex;
const isDuplex = this instanceof Duplex;

this._readableState = new ReadableState(options, this, isDuplex);

Expand Down
5 changes: 3 additions & 2 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Writable.WritableState = WritableState;

const EE = require('events');
const Stream = require('internal/streams/legacy').Stream;
const Duplex = require('internal/streams/duplex');
const { Buffer } = require('buffer');
const destroyImpl = require('internal/streams/destroy');

Expand Down Expand Up @@ -81,7 +82,7 @@ function WritableState(options, stream, isDuplex) {
// values for the readable and the writable sides of the duplex stream,
// e.g. options.readableObjectMode vs. options.writableObjectMode, etc.
if (typeof isDuplex !== 'boolean')
isDuplex = stream instanceof Stream.Duplex;
isDuplex = stream instanceof Duplex;

// Object stream flag to indicate whether or not this stream
// contains buffers or objects.
Expand Down Expand Up @@ -228,7 +229,7 @@ function Writable(options) {

// Checking for a Stream.Duplex instance is faster here instead of inside
// the WritableState constructor, at least with V8 6.5.
const isDuplex = (this instanceof Stream.Duplex);
const isDuplex = (this instanceof Duplex);

if (!isDuplex && !FunctionPrototypeSymbolHasInstance(Writable, this))
return new Writable(options);
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-stream3-pipeline-async-iterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { pipeline } = require('node:stream/promises');

{
// Ensure that async iterators can act as readable and writable streams
async function* myCustomReadable() {
yield 'Hello';
yield 'World';
}

// eslint-disable-next-line require-yield
async function* myCustomWritable(stream) {
const messages = [];
for await (const chunk of stream) {
messages.push(chunk);
}
assert.deepStrictEqual(messages, ['Hello', 'World']);
}

pipeline(
myCustomReadable,
myCustomWritable,
)
.then(common.mustCall());
}

0 comments on commit 0e07fc8

Please sign in to comment.