mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
stream: honor stream/iter protocols on function objects
The protocol lookup used by from(), fromSync(), and the
Broadcast/Share helpers only considered values with typeof 'object',
so a function implementing, e.g., Symbol.for('Stream.toStreamable') was
rejected with ERR_INVALID_ARG_TYPE. Functions are objects and the spec
does not exclude them; the iteration protocol checks already accept
them.
Assisted-by: OpenCode
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66483
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
1 parent
6eaf91353a
commit
995dee6b6b
3 files changed
+39
-1
No files matched your search
@@ -376,7 +376,10 @@ function toWriterUint8Array(chunk) {
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function getProtocolMethod(value, symbol) {
|
||||
if (value === null || typeof value !== 'object' || !(symbol in value)) {
|
||||
// Functions are objects too, and may implement the protocols.
|
||||
if (value === null ||
|
||||
(typeof value !== 'object' && typeof value !== 'function') ||
|
||||
!(symbol in value)) {
|
||||
return undefined;
|
||||
}
|
||||
const method = value[symbol];
|
||||
|
||||
@@ -507,12 +507,35 @@ function testFromUndefinedThrows() {
|
||||
assert.throws(() => from(undefined), { code: 'ERR_INVALID_ARG_TYPE' });
|
||||
}
|
||||
|
||||
async function testFromFunctionWithProtocols() {
|
||||
// Functions are objects and may implement the protocols.
|
||||
function asyncSource() {}
|
||||
asyncSource[Symbol.for('Stream.toAsyncStreamable')] =
|
||||
async () => 'async-function';
|
||||
assert.strictEqual(await text(from(asyncSource)), 'async-function');
|
||||
|
||||
function syncSource() {}
|
||||
syncSource[Symbol.for('Stream.toStreamable')] = () => 'sync-function';
|
||||
assert.strictEqual(await text(from(syncSource)), 'sync-function');
|
||||
|
||||
async function* nested() {
|
||||
yield asyncSource;
|
||||
yield syncSource;
|
||||
}
|
||||
assert.strictEqual(await text(from(nested())),
|
||||
'async-functionsync-function');
|
||||
|
||||
// A function without a protocol is still rejected.
|
||||
assert.throws(() => from(() => {}), { code: 'ERR_INVALID_ARG_TYPE' });
|
||||
}
|
||||
|
||||
Promise.all([
|
||||
testFromString(),
|
||||
testFromAsyncGenerator(),
|
||||
testFromAsyncIteratorResultShapes(),
|
||||
testFromSourceErrorDoesNotWaitForReturn(),
|
||||
testFromBoundsNestedAsyncIterable(),
|
||||
testFromFunctionWithProtocols(),
|
||||
testFromDoesNotHoldBackNestedAsyncIterable(),
|
||||
testFromBoundsPreBatchedAsyncValues(),
|
||||
testFromSyncIterableAsAsync(),
|
||||
|
||||
@@ -239,6 +239,17 @@ function testFromSyncUndefinedThrows() {
|
||||
assert.throws(() => fromSync(undefined), { code: 'ERR_INVALID_ARG_TYPE' });
|
||||
}
|
||||
|
||||
function testFromSyncFunctionWithToStreamable() {
|
||||
// Functions are objects and may implement the protocol.
|
||||
function source() {}
|
||||
source[Symbol.for('Stream.toStreamable')] = () => 'from-function';
|
||||
assert.strictEqual(textSync(fromSync(source)), 'from-function');
|
||||
// ...also when nested inside another source.
|
||||
assert.strictEqual(textSync(fromSync([source, '!'])), 'from-function!');
|
||||
// A function without a protocol is still rejected.
|
||||
assert.throws(() => fromSync(() => {}), { code: 'ERR_INVALID_ARG_TYPE' });
|
||||
}
|
||||
|
||||
Promise.all([
|
||||
testFromSyncString(),
|
||||
testFromSyncUint8Array(),
|
||||
@@ -260,4 +271,5 @@ Promise.all([
|
||||
testFromSyncPrefersIteratorForThenableIterable(),
|
||||
testFromSyncRejectsPromise(),
|
||||
testFromSyncDataView(),
|
||||
testFromSyncFunctionWithToStreamable(),
|
||||
]).then(common.mustCall());
|
||||
Reference in new issue
Block a user