1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

feat: ReadableStream.from (#19446)

Closes #19417
This commit is contained in:
Leo Kettmeir 2023-07-02 19:30:05 +02:00 committed by GitHub
parent 17ddf2f97c
commit 805497a9a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 2 deletions

View file

@ -62,6 +62,7 @@ const {
// SharedArrayBufferPrototype, // SharedArrayBufferPrototype,
Symbol, Symbol,
SymbolAsyncIterator, SymbolAsyncIterator,
SymbolIterator,
SymbolFor, SymbolFor,
TypeError, TypeError,
TypedArrayPrototypeGetBuffer, TypedArrayPrototypeGetBuffer,
@ -4780,6 +4781,30 @@ function initializeCountSizeFunction(globalObject) {
WeakMapPrototypeSet(countSizeFunctionWeakMap, globalObject, size); WeakMapPrototypeSet(countSizeFunctionWeakMap, globalObject, size);
} }
async function* createAsyncFromSyncIterator(syncIterator) {
// deno-lint-ignore prefer-primordials
yield* syncIterator;
}
// Ref: https://tc39.es/ecma262/#sec-getiterator
function getIterator(obj, async = false) {
if (async) {
if (obj[SymbolAsyncIterator] === undefined) {
if (obj[SymbolIterator] === undefined) {
throw new TypeError("No iterator found");
}
return createAsyncFromSyncIterator(obj[SymbolIterator]());
} else {
return obj[SymbolAsyncIterator]();
}
} else {
if (obj[SymbolIterator] === undefined) {
throw new TypeError("No iterator found");
}
return obj[SymbolIterator]();
}
}
const _resourceBacking = Symbol("[[resourceBacking]]"); const _resourceBacking = Symbol("[[resourceBacking]]");
// This distinction exists to prevent unrefable streams being used in // This distinction exists to prevent unrefable streams being used in
// regular fast streams that are unaware of refability // regular fast streams that are unaware of refability
@ -4863,6 +4888,43 @@ class ReadableStream {
} }
} }
static from(asyncIterable) {
webidl.requiredArguments(
arguments.length,
1,
"Failed to call 'ReadableStream.from'",
);
asyncIterable = webidl.converters.any(asyncIterable);
const iterator = getIterator(asyncIterable, true);
const stream = createReadableStream(() => undefined, async () => {
// deno-lint-ignore prefer-primordials
const res = await iterator.next();
if (typeof res !== "object") {
throw new TypeError("iterator.next value is not an object");
}
if (res.done) {
readableStreamDefaultControllerClose(stream[_controller]);
} else {
readableStreamDefaultControllerEnqueue(stream[_controller], res.value);
}
}, async (reason) => {
if (typeof iterator.return === "undefined") {
return undefined;
} else {
// deno-lint-ignore prefer-primordials
const res = await iterator.return(reason);
if (typeof res !== "object") {
throw new TypeError("iterator.return value is not an object");
} else {
return undefined;
}
}
}, 0);
return stream;
}
/** @returns {boolean} */ /** @returns {boolean} */
get locked() { get locked() {
webidl.assertBranded(this, ReadableStreamPrototype); webidl.assertBranded(this, ReadableStreamPrototype);

View file

@ -2496,7 +2496,9 @@
"owning-type-message-port.any.html": false, "owning-type-message-port.any.html": false,
"owning-type-message-port.any.worker.html": false, "owning-type-message-port.any.worker.html": false,
"owning-type.any.html": false, "owning-type.any.html": false,
"owning-type.any.worker.html": false "owning-type.any.worker.html": false,
"from.any.html": true,
"from.any.worker.html": true
}, },
"transform-streams": { "transform-streams": {
"backpressure.any.html": true, "backpressure.any.html": true,
@ -9693,4 +9695,4 @@
"media-sniff.window.html": false "media-sniff.window.html": false
} }
} }
} }