Skip to content

Commit

Permalink
stream: throw TypeError when criteria fulfilled in getIterator
Browse files Browse the repository at this point in the history
PR-URL: #53825
Fixes: #53819
Refs: #53819
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
  • Loading branch information
jakecastelli authored and targos committed Aug 14, 2024
1 parent af99ba3 commit 2a6a12e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/internal/webstreams/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {

const {
codes: {
ERR_ARG_NOT_ITERABLE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_STATE,
ERR_OPERATION_FAILED,
Expand Down Expand Up @@ -235,6 +236,11 @@ function getIterator(obj, kind = 'sync', method) {
method = obj[SymbolAsyncIterator];
if (method === undefined) {
const syncMethod = obj[SymbolIterator];

if (syncMethod === undefined) {
throw new ERR_ARG_NOT_ITERABLE(obj);
}

const syncIteratorRecord = getIterator(obj, 'sync', syncMethod);
return createAsyncFromSyncIterator(syncIteratorRecord);
}
Expand All @@ -243,6 +249,10 @@ function getIterator(obj, kind = 'sync', method) {
}
}

if (method === undefined) {
throw new ERR_ARG_NOT_ITERABLE(obj);
}

const iterator = FunctionPrototypeCall(method, obj);
if (typeof iterator !== 'object' || iterator === null) {
throw new ERR_INVALID_STATE.TypeError('The iterator method must return an object');
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-webstream-readable-from.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

require('../common');
const assert = require('node:assert');

assert.throws(
() => ReadableStream.from({}),
{ code: 'ERR_ARG_NOT_ITERABLE', name: 'TypeError' },
);

0 comments on commit 2a6a12e

Please sign in to comment.