mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
stream: simplify readableStreamFromIterable
Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: https://github.com/nodejs/node/pull/62651 Reviewed-By: Aviv Keller <me@aviv.sh>
This commit is contained in:
2 files changed
+23
-92
No files matched your search
@@ -24,6 +24,7 @@ const {
|
||||
Symbol,
|
||||
SymbolAsyncIterator,
|
||||
SymbolDispose,
|
||||
SymbolIterator,
|
||||
SymbolToStringTag,
|
||||
TypedArrayPrototypeGetLength,
|
||||
Uint8Array,
|
||||
@@ -32,6 +33,7 @@ const {
|
||||
const {
|
||||
AbortError,
|
||||
codes: {
|
||||
ERR_ARG_NOT_ITERABLE,
|
||||
ERR_ILLEGAL_CONSTRUCTOR,
|
||||
ERR_INVALID_ARG_TYPE,
|
||||
ERR_INVALID_ARG_VALUE,
|
||||
@@ -111,8 +113,6 @@ const {
|
||||
nonOpCancel,
|
||||
nonOpPull,
|
||||
nonOpStart,
|
||||
getIterator,
|
||||
iteratorNext,
|
||||
kType,
|
||||
kState,
|
||||
} = require('internal/webstreams/util');
|
||||
@@ -1360,41 +1360,36 @@ function createReadableStreamState() {
|
||||
|
||||
function readableStreamFromIterable(iterable) {
|
||||
let stream;
|
||||
const iteratorRecord = getIterator(iterable, 'async');
|
||||
|
||||
const iteratorGetter = iterable[SymbolAsyncIterator] ?? iterable[SymbolIterator];
|
||||
if (iteratorGetter == null || typeof iteratorGetter !== 'function') {
|
||||
throw new ERR_ARG_NOT_ITERABLE(iterable);
|
||||
}
|
||||
const iterator = FunctionPrototypeCall(iteratorGetter, iterable);
|
||||
const startAlgorithm = nonOpStart;
|
||||
|
||||
async function pullAlgorithm() {
|
||||
const nextResult = iteratorNext(iteratorRecord);
|
||||
const nextPromise = PromiseResolve(nextResult);
|
||||
return PromisePrototypeThen(nextPromise, (iterResult) => {
|
||||
if (typeof iterResult !== 'object' || iterResult === null) {
|
||||
throw new ERR_INVALID_STATE.TypeError(
|
||||
'The promise returned by the iterator.next() method must fulfill with an object');
|
||||
}
|
||||
if (iterResult.done) {
|
||||
readableStreamDefaultControllerClose(stream[kState].controller);
|
||||
} else {
|
||||
readableStreamDefaultControllerEnqueue(stream[kState].controller, iterResult.value);
|
||||
}
|
||||
});
|
||||
const iterResult = await iterator.next();
|
||||
if (typeof iterResult !== 'object' || iterResult === null) {
|
||||
throw new ERR_INVALID_STATE.TypeError(
|
||||
'The promise returned by the iterator.next() method must fulfill with an object');
|
||||
}
|
||||
if (iterResult.done) {
|
||||
readableStreamDefaultControllerClose(stream[kState].controller);
|
||||
} else {
|
||||
readableStreamDefaultControllerEnqueue(stream[kState].controller, await iterResult.value);
|
||||
}
|
||||
}
|
||||
|
||||
async function cancelAlgorithm(reason) {
|
||||
const iterator = iteratorRecord.iterator;
|
||||
const returnMethod = iterator.return;
|
||||
if (returnMethod === undefined) {
|
||||
return PromiseResolve();
|
||||
return;
|
||||
}
|
||||
const iterResult = await FunctionPrototypeCall(returnMethod, iterator, reason);
|
||||
if (typeof iterResult !== 'object' || iterResult === null) {
|
||||
throw new ERR_INVALID_STATE.TypeError(
|
||||
'The promise returned by the iterator.return() method must fulfill with an object');
|
||||
}
|
||||
const returnResult = FunctionPrototypeCall(returnMethod, iterator, reason);
|
||||
const returnPromise = PromiseResolve(returnResult);
|
||||
return PromisePrototypeThen(returnPromise, (iterResult) => {
|
||||
if (typeof iterResult !== 'object' || iterResult === null) {
|
||||
throw new ERR_INVALID_STATE.TypeError(
|
||||
'The promise returned by the iterator.return() method must fulfill with an object');
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
|
||||
stream = createReadableStream(
|
||||
|
||||
@@ -14,16 +14,12 @@ const {
|
||||
ReflectApply,
|
||||
ReflectGet,
|
||||
Symbol,
|
||||
SymbolAsyncIterator,
|
||||
SymbolIterator,
|
||||
Uint8Array,
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
codes: {
|
||||
ERR_ARG_NOT_ITERABLE,
|
||||
ERR_INVALID_ARG_VALUE,
|
||||
ERR_INVALID_STATE,
|
||||
},
|
||||
} = require('internal/errors');
|
||||
|
||||
@@ -203,64 +199,6 @@ function lazyTransfer() {
|
||||
return transfer;
|
||||
}
|
||||
|
||||
function createAsyncFromSyncIterator(syncIteratorRecord) {
|
||||
const syncIterable = {
|
||||
[SymbolIterator]: () => syncIteratorRecord.iterator,
|
||||
};
|
||||
|
||||
const asyncIterator = (async function* () {
|
||||
return yield* syncIterable;
|
||||
}());
|
||||
|
||||
const nextMethod = asyncIterator.next;
|
||||
return { iterator: asyncIterator, nextMethod, done: false };
|
||||
}
|
||||
|
||||
// Refs: https://tc39.es/ecma262/#sec-getiterator
|
||||
function getIterator(obj, kind = 'sync', method) {
|
||||
if (method === undefined) {
|
||||
if (kind === 'async') {
|
||||
method = obj[SymbolAsyncIterator];
|
||||
if (method == null) {
|
||||
const syncMethod = obj[SymbolIterator];
|
||||
|
||||
if (syncMethod === undefined) {
|
||||
throw new ERR_ARG_NOT_ITERABLE(obj);
|
||||
}
|
||||
|
||||
const syncIteratorRecord = getIterator(obj, 'sync', syncMethod);
|
||||
return createAsyncFromSyncIterator(syncIteratorRecord);
|
||||
}
|
||||
} else {
|
||||
method = obj[SymbolIterator];
|
||||
}
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
const nextMethod = iterator.next;
|
||||
return { iterator, nextMethod, done: false };
|
||||
}
|
||||
|
||||
function iteratorNext(iteratorRecord, value) {
|
||||
let result;
|
||||
if (value === undefined) {
|
||||
result = FunctionPrototypeCall(iteratorRecord.nextMethod, iteratorRecord.iterator);
|
||||
} else {
|
||||
result = FunctionPrototypeCall(iteratorRecord.nextMethod, iteratorRecord.iterator, [value]);
|
||||
}
|
||||
if (typeof result !== 'object' || result === null) {
|
||||
throw new ERR_INVALID_STATE.TypeError('The iterator.next() method must return an object');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ArrayBufferViewGetBuffer,
|
||||
ArrayBufferViewGetByteLength,
|
||||
@@ -286,8 +224,6 @@ module.exports = {
|
||||
nonOpPull,
|
||||
nonOpStart,
|
||||
nonOpWrite,
|
||||
getIterator,
|
||||
iteratorNext,
|
||||
kType,
|
||||
kState,
|
||||
};
|
||||
Reference in new issue
Block a user