Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
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
Copy Markdown
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
Copy Markdown
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;
}
Comment thread
debadree25 marked this conversation as resolved.

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
Original file line number Diff line number Diff line change
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,
};
Loading