mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
stream: remove isPromise utility function
The function was not checking if the parameter was actually a Promise instance, but if it has a `then` method. Removing the utility function in favor of a clearer `typeof` check, handling the case when the thenable throws if then method is accessed more than once. PR-URL: https://github.com/nodejs/node/pull/35925 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
1 parent
adae822035
commit
ad98cf04f3
2 files changed
+32
-9
No files matched your search
@@ -5,8 +5,9 @@
|
||||
|
||||
const {
|
||||
ArrayIsArray,
|
||||
ReflectApply,
|
||||
SymbolAsyncIterator,
|
||||
SymbolIterator
|
||||
SymbolIterator,
|
||||
} = primordials;
|
||||
|
||||
let eos;
|
||||
@@ -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');
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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(),
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user