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: add pipeline() for webstreams #46307

Merged
merged 26 commits into from Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 34 additions & 6 deletions lib/internal/streams/pipeline.js
Expand Up @@ -35,11 +35,29 @@ const {
isReadable,
isReadableNodeStream,
isNodeStream,
isReadableStream,
isWritableStream,
isTransformStream,
} = require('internal/streams/utils');
const { AbortController } = require('internal/abort_controller');

let PassThrough;
let Readable;
let Writable;

function lazyloadReadable() {
if (!Readable) {
Readable = require('internal/streams/readable');
}
return Readable;
}

function lazyloadWritable() {
if (!Writable) {
Writable = require('internal/streams/writable');
}
return Writable;
}

function destroyer(stream, reading, writing) {
let finished = false;
Expand Down Expand Up @@ -81,11 +99,7 @@ function makeAsyncIterable(val) {
}

async function* fromReadable(val) {
if (!Readable) {
Readable = require('internal/streams/readable');
}

yield* Readable.prototype[SymbolAsyncIterator].call(val);
yield* lazyloadReadable().prototype[SymbolAsyncIterator].call(val);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary change

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok reverting this

}

async function pump(iterable, writable, finish, { end }) {
Expand Down Expand Up @@ -147,6 +161,20 @@ async function pump(iterable, writable, finish, { end }) {
}
}

function convertToNodeStreamIfWebstream(stream) {
if (isReadableStream(stream)) {
return lazyloadReadable().fromWeb(stream);
} else if (isWritableStream(stream)) {
return lazyloadWritable().fromWeb(stream);
} else if (isTransformStream(stream)) {
return Duplex.from({
writable: stream.writable,
readable: stream.readable
});
}
return stream;
}

function pipeline(...streams) {
return pipelineImpl(streams, once(popCallback(streams)));
}
Expand Down Expand Up @@ -212,7 +240,7 @@ function pipelineImpl(streams, callback, opts) {

let ret;
for (let i = 0; i < streams.length; i++) {
const stream = streams[i];
const stream = convertToNodeStreamIfWebstream(streams[i]);
const reading = i < streams.length - 1;
const writing = i > 0;
const end = reading || opts?.end !== false;
Expand Down
10 changes: 10 additions & 0 deletions lib/internal/streams/utils.js
Expand Up @@ -77,6 +77,15 @@ function isWritableStream(obj) {
);
}

function isTransformStream(obj) {
return !!(
obj &&
!isNodeStream(obj) &&
typeof obj.readable === 'object' &&
typeof obj.writable === 'object'
);
}

function isIterable(obj, isAsync) {
if (obj == null) return false;
if (isAsync === true) return typeof obj[SymbolAsyncIterator] === 'function';
Expand Down Expand Up @@ -312,4 +321,5 @@ module.exports = {
isServerRequest,
isServerResponse,
willEmitClose,
isTransformStream,
};