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: remove isPromise utility function #35925

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 11 additions & 9 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

const {
ArrayIsArray,
ReflectApply,
SymbolAsyncIterator,
SymbolIterator
SymbolIterator,
} = primordials;

let eos;
Expand Down Expand Up @@ -77,10 +78,6 @@ function popCallback(streams) {
return streams.pop();
}

function isPromise(obj) {
return !!(obj && typeof obj.then === 'function');
}

function isReadable(obj) {
return !!(obj && typeof obj.pipe === 'function');
}
Expand Down Expand Up @@ -222,14 +219,19 @@ function pipeline(...streams) {
const pt = new PassThrough({
objectMode: true
});
if (isPromise(ret)) {
ret
.then((val) => {

// Handle Promises/A+ spec, `then` could be a getter that throws on
// second use.
const then = ret?.then;
if (typeof then === 'function') {
ReflectApply(then, ret, [
(val) => {
value = val;
pt.end(val);
}, (err) => {
pt.destroy(err);
});
}
]);
} else if (isIterable(ret, true)) {
finishCount++;
pump(ret, pt, finish);
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-stream-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -1240,3 +1240,24 @@ const net = require('net');
}),
);
}
{
function createThenable() {
let counter = 0;
return {
get then() {
if (counter++) {
throw new Error('Cannot access `then` more than once');
}
return Function.prototype;
},
};
}

pipeline(
function* () {
yield 0;
},
createThenable,
() => common.mustNotCall(),
);
}