Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: fix readable stream as async iterator function #46147

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}