From bdf1f488e9e19fa833243e292caa485fd4a17a02 Mon Sep 17 00:00:00 2001 From: jakecastelli Date: Sat, 13 Jul 2024 00:24:06 +0930 Subject: [PATCH] stream: throw TypeError when criteria fulfilled in getIterator --- lib/internal/webstreams/util.js | 10 ++++++++++ test/parallel/test-webstream-readable-from.js | 9 +++++++++ 2 files changed, 19 insertions(+) create mode 100644 test/parallel/test-webstream-readable-from.js diff --git a/lib/internal/webstreams/util.js b/lib/internal/webstreams/util.js index cce67d5b04fed2..76e58d642c8d9f 100644 --- a/lib/internal/webstreams/util.js +++ b/lib/internal/webstreams/util.js @@ -18,6 +18,7 @@ const { const { codes: { + ERR_ARG_NOT_ITERABLE, ERR_INVALID_ARG_VALUE, ERR_INVALID_STATE, ERR_OPERATION_FAILED, @@ -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); } @@ -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'); diff --git a/test/parallel/test-webstream-readable-from.js b/test/parallel/test-webstream-readable-from.js new file mode 100644 index 00000000000000..470ee5d60d76e2 --- /dev/null +++ b/test/parallel/test-webstream-readable-from.js @@ -0,0 +1,9 @@ +'use strict'; + +require('../common'); +const assert = require('node:assert'); + +assert.throws( + () => ReadableStream.from({}), + { code: 'ERR_ARG_NOT_ITERABLE', name: 'TypeError' }, +);