diff options
author | Luca Casonato <hello@lcas.dev> | 2024-08-26 12:24:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-26 12:24:27 +0200 |
commit | e53678fd5841fe7b94f8f9e16d6521201a3d2993 (patch) | |
tree | 8954b7d1e04f3319c3c71b7b7f9ca16529a8edd5 /ext/web | |
parent | 675539c7ab503e5a5bdef1d17678afb7b8dc352b (diff) |
Revert "feat(fetch): accept async iterables for body" (#25207)
Unfortunately this caused a regression:
https://github.com/denoland/deno/issues/25203.
Need to do some more upstream spec work to fix this before this can be
re-landed.
Reverts denoland/deno#24623
Diffstat (limited to 'ext/web')
-rw-r--r-- | ext/web/06_streams.js | 63 |
1 files changed, 48 insertions, 15 deletions
diff --git a/ext/web/06_streams.js b/ext/web/06_streams.js index 69d333c0d..130b24783 100644 --- a/ext/web/06_streams.js +++ b/ext/web/06_streams.js @@ -70,6 +70,7 @@ const { String, Symbol, SymbolAsyncIterator, + SymbolIterator, SymbolFor, TypeError, TypedArrayPrototypeGetBuffer, @@ -5083,6 +5084,34 @@ function initializeCountSizeFunction(globalObject) { WeakMapPrototypeSet(countSizeFunctionWeakMap, globalObject, size); } +// Ref: https://tc39.es/ecma262/#sec-getiterator +function getAsyncOrSyncIterator(obj) { + let iterator; + if (obj[SymbolAsyncIterator] != null) { + iterator = obj[SymbolAsyncIterator](); + if (!isObject(iterator)) { + throw new TypeError( + "[Symbol.asyncIterator] returned a non-object value", + ); + } + } else if (obj[SymbolIterator] != null) { + iterator = obj[SymbolIterator](); + if (!isObject(iterator)) { + throw new TypeError("[Symbol.iterator] returned a non-object value"); + } + } else { + throw new TypeError("No iterator found"); + } + if (typeof iterator.next !== "function") { + throw new TypeError("iterator.next is not a function"); + } + return iterator; +} + +function isObject(x) { + return (typeof x === "object" && x != null) || typeof x === "function"; +} + const _resourceBacking = Symbol("[[resourceBacking]]"); // This distinction exists to prevent unrefable streams being used in // regular fast streams that are unaware of refability @@ -5168,22 +5197,21 @@ class ReadableStream { } static from(asyncIterable) { - const prefix = "Failed to execute 'ReadableStream.from'"; webidl.requiredArguments( arguments.length, 1, - prefix, + "Failed to execute 'ReadableStream.from'", ); - asyncIterable = webidl.converters["async iterable<any>"]( - asyncIterable, - prefix, - "Argument 1", - ); - const iter = asyncIterable.open(); + asyncIterable = webidl.converters.any(asyncIterable); + + const iterator = getAsyncOrSyncIterator(asyncIterable); const stream = createReadableStream(noop, async () => { // deno-lint-ignore prefer-primordials - const res = await iter.next(); + const res = await iterator.next(); + if (!isObject(res)) { + throw new TypeError("iterator.next value is not an object"); + } if (res.done) { readableStreamDefaultControllerClose(stream[_controller]); } else { @@ -5193,8 +5221,17 @@ class ReadableStream { ); } }, async (reason) => { - // deno-lint-ignore prefer-primordials - await iter.return(reason); + if (iterator.return == null) { + return undefined; + } else { + // deno-lint-ignore prefer-primordials + const res = await iterator.return(reason); + if (!isObject(res)) { + throw new TypeError("iterator.return value is not an object"); + } else { + return undefined; + } + } }, 0); return stream; } @@ -6854,10 +6891,6 @@ webidl.converters.StreamPipeOptions = webidl { key: "signal", converter: webidl.converters.AbortSignal }, ]); -webidl.converters["async iterable<any>"] = webidl.createAsyncIterableConverter( - webidl.converters.any, -); - internals.resourceForReadableStream = resourceForReadableStream; export { |