diff --git a/lib/internal/streams/iter/from.js b/lib/internal/streams/iter/from.js index a926eb982bd..23b7021a649 100644 --- a/lib/internal/streams/iter/from.js +++ b/lib/internal/streams/iter/from.js @@ -154,6 +154,10 @@ const kNextUncancellable = Symbol('kNextUncancellable'); const kReadUncancellable = Symbol('kReadUncancellable'); const kToIterResult = Symbol('kToIterResult'); const kReadFailed = Symbol('kReadFailed'); +// A read of the source that a cancellation can interrupt, for +// createAsyncSourceNormalizer(): see kRead in yieldNormalizationAbortable(). +const kSetReadHandlers = Symbol('kSetReadHandlers'); +const kRead = Symbol('kRead'); // The result kReadUncancellable gives once the source is done. const kDoneSourceResult = ObjectFreeze({ __proto__: null, done: true, value: undefined }); @@ -516,6 +520,55 @@ function yieldNormalizationAbortable(source, context) { } } + // The handlers of kRead, set once by kSetReadHandlers, and whether a + // kRead is pending. + let onRead = null; + let onReadError = null; + let readPending = false; + + // Fail a kRead, closing the source first if the normalization has + // been cancelled. + function failRead(error) { + if (context.cancelled) { + PromisePrototypeThen(closeSource(true), () => { + reading = false; + onReadError(error); + }); + return; + } + reading = false; + onReadError(error); + } + + // context.resolve while a kRead is pending. + function onReadCancelled() { + if (!readPending) return; + readPending = false; + context.resolve = null; + failRead(context.reason); + } + + function onReadResult(result) { + if (!readPending) return; + readPending = false; + if (context.resolve === onReadCancelled) context.resolve = null; + let iterResult; + try { + iterResult = toIterResult(result); + } catch (error) { + failRead(error); + return; + } + onRead(iterResult); + } + + function onReadRejected(error) { + if (!readPending) return; + readPending = false; + if (context.resolve === onReadCancelled) context.resolve = null; + failRead(error); + } + // Reject a pending next(), closing the source first if the // normalization has been cancelled. function rejectNext(reject, error) { @@ -604,6 +657,38 @@ function yieldNormalizationAbortable(source, context) { return PromiseResolve(next); }, [kToIterResult]: onUncancellableResult, + // Like next(), calling the handlers set by kSetReadHandlers instead + // of settling a promise of its own: onRead with the result, or + // onReadError with the error, possibly synchronously. A cancellation + // calls onReadError with its reason. The handlers are the same for + // every read, so that a read takes no closure, and the caller can + // handle the result in the same reaction. + [kSetReadHandlers](resultHandler, errorHandler) { + onRead = resultHandler; + onReadError = errorHandler; + }, + [kRead]() { + if (completed) { + onRead(new IterResult(true, undefined)); + return; + } + if (context.cancelled) { + onReadError(context.reason); + return; + } + reading = true; + let next; + try { + next = FunctionPrototypeCall(nextMethod, iterator); + } catch (error) { + failRead(error); + return; + } + readPending = true; + context.resolve = onReadCancelled; + PromisePrototypeThen(PromiseResolve(next), onReadResult, + onReadRejected); + }, [kReadFailed]() { reading = false; }, @@ -799,6 +884,11 @@ function createAsyncSourceNormalizer(source, context) { // kNextUncancellable method, for callers that never cancel the // normalization while a read is pending. let uncancellable = false; + // Whether the source is read with kRead, and the settling functions of + // the read's promise while it is pending. + let readable = false; + let resolveRead = null; + let rejectRead = null; const { run, settled } = createOperationQueue(); function finish(result) { @@ -832,16 +922,23 @@ function createAsyncSourceNormalizer(source, context) { return finish(new IterResult(true, undefined)); } try { - return handleValue(result.value); + return handleValue(result.value, result); } catch (error) { return onBodyError(error); } } - function handleValue(value) { + // `result` is the source's result for `value`. When the source is read + // with kRead or kReadUncancellable, it is an IterResult of + // yieldNormalizationAbortable(), passed on as it is for a batch passed on + // unchanged. + function handleValue(value, result) { if (isUint8ArrayBatch(value)) { if (value.length <= FROM_BATCH_SIZE) { if (value.length === 0) return pullSource(); + if (readable || uncancellable) { + return finish(result); + } return finish(new IterResult(false, value)); } boundedBatches = yieldBoundedBatch(value); @@ -877,11 +974,44 @@ function createAsyncSourceNormalizer(source, context) { return fail(error); } + // The kRead handlers: the result is handled in the reaction to the + // source's result, and settles the read's promise. + function onRead(result) { + const resolve = resolveRead; + const reject = rejectRead; + resolveRead = rejectRead = null; + let settledWith; + try { + settledWith = onSourceResult(result); + } catch (error) { + reject(error); + return; + } + resolve(settledWith); + } + + function onReadError(error) { + const reject = rejectRead; + resolveRead = rejectRead = null; + state = kDone; + settled(); + reject(error); + } + function pullSource() { if (uncancellable) { return PromisePrototypeThen(iterator[kReadUncancellable](), onUncancellableResult, onUncancellableError); } + if (readable) { + // One promise for the read, which a cancellation rejects, instead of + // next()'s promise and a reaction to it. + const { promise, resolve, reject } = PromiseWithResolvers(); + resolveRead = resolve; + rejectRead = reject; + iterator[kRead](); + return promise; + } return PromisePrototypeThen(iterator.next(), onSourceResult, fail); } @@ -905,6 +1035,10 @@ function createAsyncSourceNormalizer(source, context) { throwIfNormalizationCancelled(context); const iterable = yieldNormalizationAbortable(source, context); iterator = iterable[SymbolAsyncIterator](); + if (iterator[kSetReadHandlers] !== undefined) { + iterator[kSetReadHandlers](onRead, onReadError); + readable = true; + } } catch (error) { state = kDone; settled();