@@ -58,6 +58,7 @@ const {
5858
5959const {
6060 IterResult,
61+ PipelineAbort,
6162 kActive,
6263 kDone,
6364 kNullOnceOption,
@@ -581,19 +582,53 @@ function* createSyncPipeline(source, transforms) {
581582// Async Pipeline Implementation
582583// =============================================================================
583584
584- // The options object passed to async transforms. Stateless transforms get a
585- // new one for every call, so it is constructed rather than created as a
586- // `{ __proto__: null, signal }` literal (a dictionary-mode object). The
587- // prototype is frozen and has no %Object.prototype% in its chain, and the
588- // non-enumerable `constructor` lets util.inspect() print the options as
589- // `TransformOptions { signal }`.
590- function TransformOptions ( signal ) {
591- this . signal = signal ;
585+ // The options object passed to an async transform: one for each transform of
586+ // a pipeline, passed to every call of a stateless transform. Its `signal` is
587+ // the pipeline's signal, created by `abort` when first read (see
588+ // PipelineAbort): until then it is an accessor, which replaces itself with a
589+ // data property holding the signal. The prototype is frozen and has no
590+ // %Object.prototype% in its chain, and the non-enumerable `constructor` lets
591+ // util.inspect() print the options as `TransformOptions { signal }`.
592+ function TransformOptions ( abort ) {
593+ ObjectDefineProperty ( this , 'signal' , {
594+ __proto__ : null ,
595+ configurable : true ,
596+ enumerable : true ,
597+ get ( ) {
598+ const signal = abort . signal ;
599+ ObjectDefineProperty ( this , 'signal' , {
600+ __proto__ : null ,
601+ configurable : true ,
602+ enumerable : true ,
603+ value : signal ,
604+ writable : true ,
605+ } ) ;
606+ return signal ;
607+ } ,
608+ set ( value ) {
609+ ObjectDefineProperty ( this , 'signal' , {
610+ __proto__ : null ,
611+ configurable : true ,
612+ enumerable : true ,
613+ value,
614+ writable : true ,
615+ } ) ;
616+ } ,
617+ } ) ;
592618}
593619TransformOptions . prototype = ObjectFreeze ( ObjectDefineProperty (
594620 { __proto__ : null } , 'constructor' ,
595621 { __proto__ : null , value : TransformOptions } ) ) ;
596622
623+ // The options for each function of a fused run of stateless transforms.
624+ function createRunOptions ( run , abort ) {
625+ const options = [ ] ;
626+ for ( let i = 0 ; i < run . length ; i ++ ) {
627+ options [ i ] = new TransformOptions ( abort ) ;
628+ }
629+ return options ;
630+ }
631+
597632/**
598633 * Close an async iterator as for await does: for a throw completion
599634 * (`quiet`), wait for it but ignore errors; otherwise reject on errors and
@@ -650,13 +685,13 @@ async function* yieldFusedStatelessOutput(current) {
650685 * @param {Array<Function> } run
651686 * @param {number } index
652687 * @param {any } result - The result of `run[index]`
653- * @param {AbortSignal } signal
688+ * @param {TransformOptions[] } runOptions - The options of each function
654689 * @yields {Uint8Array[]}
655690 */
656- async function * continueFusedStatelessBatch ( run , index , result , signal ) {
691+ async function * continueFusedStatelessBatch ( run , index , result , runOptions ) {
657692 let current ;
658693 for ( let i = index ; i < run . length ; i ++ ) {
659- if ( i !== index ) result = run [ i ] ( current , new TransformOptions ( signal ) ) ;
694+ if ( i !== index ) result = run [ i ] ( current , runOptions [ i ] ) ;
660695 if ( isPromise ( result ) ) result = await result ;
661696 if ( result === null ) return ;
662697 if ( i === run . length - 1 ) {
@@ -680,24 +715,24 @@ async function* continueFusedStatelessBatch(run, index, result, signal) {
680715 * flush each transform after all upstream data, including data emitted by
681716 * earlier flushes, has been processed by that transform.
682717 * @param {Array<Function> } run
683- * @param {AbortSignal } signal
718+ * @param {TransformOptions[] } runOptions - The options of each function
684719 * @yields {Uint8Array[]}
685720 */
686- async function * flushFusedStatelessAsyncTransforms ( run , signal ) {
721+ async function * flushFusedStatelessAsyncTransforms ( run , runOptions ) {
687722 let pending = [ ] ;
688723 for ( let i = 0 ; i < run . length ; i ++ ) {
689724 const next = [ ] ;
690725 for ( let j = 0 ; j < pending . length ; j ++ ) {
691726 const pendingResult = appendTransformResultAsync (
692727 next ,
693- run [ i ] ( pending [ j ] , new TransformOptions ( signal ) ) ) ;
728+ run [ i ] ( pending [ j ] , runOptions [ i ] ) ) ;
694729 if ( pendingResult !== undefined ) {
695730 await pendingResult ;
696731 }
697732 }
698733 const flushResult = appendTransformResultAsync (
699734 next ,
700- run [ i ] ( null , new TransformOptions ( signal ) ) ) ;
735+ run [ i ] ( null , runOptions [ i ] ) ) ;
701736 if ( flushResult !== undefined ) {
702737 await flushResult ;
703738 }
@@ -731,15 +766,14 @@ const kDelegated = Symbol('kDelegated');
731766 * if any, and close the source unless it has ended; return() propagates
732767 * errors from closing it, throw() ignores them.
733768 *
734- * INVARIANT: This function accepts a signal, NOT a pre-built options object.
735- * A fresh TransformOptions object is created for each
736- * transform invocation to prevent cross-transform mutation.
769+ * Each function of the run is passed its own options object on every call.
737770 * @param {AsyncIterable<Uint8Array[]> } source
738771 * @param {Array<Function> } run - Array of stateless transform functions
739- * @param {AbortSignal } signal - The pipeline's abort signal
772+ * @param {PipelineAbort } abort - The pipeline's abort state
740773 * @returns {AsyncIterator<Uint8Array[]> }
741774 */
742- function applyFusedStatelessAsyncTransforms ( source , run , signal ) {
775+ function applyFusedStatelessAsyncTransforms ( source , run , abort ) {
776+ const runOptions = createRunOptions ( run , abort ) ;
743777 let state = kStart ;
744778 let iterator ;
745779 let nextMethod ;
@@ -792,9 +826,9 @@ function applyFusedStatelessAsyncTransforms(source, run, signal) {
792826 function applyBatch ( chunks ) {
793827 let current = chunks ;
794828 for ( let i = 0 ; i < run . length ; i ++ ) {
795- const result = run [ i ] ( current , new TransformOptions ( signal ) ) ;
829+ const result = run [ i ] ( current , runOptions [ i ] ) ;
796830 if ( isPromise ( result ) ) {
797- delegate = continueFusedStatelessBatch ( run , i , result , signal ) ;
831+ delegate = continueFusedStatelessBatch ( run , i , result , runOptions ) ;
798832 return kDelegated ;
799833 }
800834 if ( result === null ) return null ;
@@ -804,7 +838,7 @@ function applyFusedStatelessAsyncTransforms(source, run, signal) {
804838 }
805839 current = normalizeTransformResultFast ( result ) ;
806840 if ( current === undefined ) {
807- delegate = continueFusedStatelessBatch ( run , i , result , signal ) ;
841+ delegate = continueFusedStatelessBatch ( run , i , result , runOptions ) ;
808842 return kDelegated ;
809843 }
810844 if ( current === null ) return null ;
@@ -832,7 +866,7 @@ function applyFusedStatelessAsyncTransforms(source, run, signal) {
832866 }
833867 if ( result . done ) {
834868 sourceDone = true ;
835- delegate = flushFusedStatelessAsyncTransforms ( run , signal ) ;
869+ delegate = flushFusedStatelessAsyncTransforms ( run , runOptions ) ;
836870 return pullDelegate ( ) ;
837871 }
838872 value = result . value ;
@@ -988,7 +1022,7 @@ async function* applyStatefulAsyncTransform(
9881022 * @yields {Uint8Array[]}
9891023 */
9901024async function * applyValidatedStatefulAsyncTransform (
991- source , transform , receiver , options ) {
1025+ source , transform , receiver , options , abort ) {
9921026 const output = FunctionPrototypeCall (
9931027 transform , receiver , source , options ) ;
9941028 for await ( const batch of output ) {
@@ -999,7 +1033,7 @@ async function* applyValidatedStatefulAsyncTransform(
9991033 // Check abort after the transform completes - without the
10001034 // withFlushAsync wrapper there is no extra yield to give
10011035 // the outer pipeline a chance to see the abort.
1002- options . signal ? .throwIfAborted ( ) ;
1036+ abort . throwIfAborted ( ) ;
10031037}
10041038
10051039/**
@@ -1076,22 +1110,17 @@ async function* yieldFrom(source) {
10761110 * Build the chain of transform layers of a pipeline.
10771111 * @param {AsyncIterable<Uint8Array[]> } normalized
10781112 * @param {Array } transforms
1079- * @param {AbortSignal } transformSignal
1113+ * @param {PipelineAbort } abort - The pipeline's abort state
10801114 * @returns {AsyncIterable<Uint8Array[]> }
10811115 */
1082- function createAsyncTransformLayers ( normalized , transforms , transformSignal ) {
1116+ function createAsyncTransformLayers ( normalized , transforms , abort ) {
10831117 // Apply transforms - fuse consecutive stateless transforms into a single
10841118 // layer to avoid unnecessary async ticks.
10851119 //
1086- // INVARIANT: Each transform invocation MUST receive its own fresh options
1087- // object (new TransformOptions(signal)). Transforms may mutate the options
1088- // object, so sharing a single object across invocations would allow one
1089- // transform to corrupt the options seen by another. The signal is shared
1090- // across calls (mutations to it are acceptable), but the containing options
1091- // object must be unique per call. This is enforced inside
1092- // applyFusedStatelessAsyncTransforms and applyStatefulAsyncTransform, which
1093- // accept the signal directly and create the options object per invocation.
1094- // DO NOT pass a pre-built options object.
1120+ // Each transform gets its own options object, created here once and passed
1121+ // to every call of the transform: transforms may mutate the options
1122+ // object, and must not be able to change the options another transform
1123+ // sees.
10951124 let current = normalized ;
10961125 let statelessRun = [ ] ;
10971126
@@ -1101,13 +1130,13 @@ function createAsyncTransformLayers(normalized, transforms, transformSignal) {
11011130 // Flush any accumulated stateless run before the stateful transform
11021131 if ( statelessRun . length > 0 ) {
11031132 current = applyFusedStatelessAsyncTransforms ( current , statelessRun ,
1104- transformSignal ) ;
1133+ abort ) ;
11051134 statelessRun = [ ] ;
11061135 }
1107- const opts = new TransformOptions ( transformSignal ) ;
1136+ const opts = new TransformOptions ( abort ) ;
11081137 if ( transform [ kValidatedTransform ] ) {
11091138 current = applyValidatedStatefulAsyncTransform (
1110- current , transform . transform , transform . receiver , opts ) ;
1139+ current , transform . transform , transform . receiver , opts , abort ) ;
11111140 } else {
11121141 current = applyStatefulAsyncTransform (
11131142 current , transform . transform , transform . receiver , opts ) ;
@@ -1119,7 +1148,7 @@ function createAsyncTransformLayers(normalized, transforms, transformSignal) {
11191148 // Flush remaining stateless run
11201149 if ( statelessRun . length > 0 ) {
11211150 current = applyFusedStatelessAsyncTransforms ( current , statelessRun ,
1122- transformSignal ) ;
1151+ abort ) ;
11231152 }
11241153 return current ;
11251154}
@@ -1130,9 +1159,9 @@ function createAsyncTransformLayers(normalized, transforms, transformSignal) {
11301159 * layer for every batch (see applyFusedStatelessAsyncTransforms()).
11311160 *
11321161 * When started by the first next(), it checks `signal`, then creates the
1133- * controller whose signal the transforms get, aborted when `signal` aborts.
1134- * Each batch, and the end, is passed on only if the transforms' signal has
1135- * not been aborted. If the pipeline fails, the transforms' signal is aborted
1162+ * abort state whose signal the transforms get, aborted when `signal` aborts.
1163+ * Each batch, and the end, is passed on only if the pipeline has not been
1164+ * aborted. If the pipeline fails, the transforms' signal is aborted
11361165 * with the error; if it is stopped early by return() or throw(), the
11371166 * transforms are closed and their signal is aborted.
11381167 * @param {AsyncIterable<Uint8Array[]> } source
@@ -1142,7 +1171,7 @@ function createAsyncTransformLayers(normalized, transforms, transformSignal) {
11421171 */
11431172function createAsyncTransformPipeline ( source , transforms , signal ) {
11441173 let state = kStart ;
1145- let controller ;
1174+ let abort ;
11461175 let abortHandler ;
11471176 let completed = false ;
11481177 let iterator ;
@@ -1151,10 +1180,10 @@ function createAsyncTransformPipeline(source, transforms, signal) {
11511180
11521181 // What an async generator would do in its `finally` block.
11531182 function cleanup ( ) {
1154- if ( ! completed && ! controller . signal . aborted ) {
1183+ if ( ! completed && ! abort . aborted ) {
11551184 // Consumer stopped early or return() was called.
11561185 // If a transform listener throws here, let it propagate.
1157- controller . abort ( lazyDOMException ( 'Aborted' , 'AbortError' ) ) ;
1186+ abort . abort ( lazyDOMException ( 'Aborted' , 'AbortError' ) ) ;
11581187 }
11591188 // Clean up user signal listener to prevent holding controller alive
11601189 if ( signal && abortHandler ) {
@@ -1182,9 +1211,7 @@ function createAsyncTransformPipeline(source, transforms, signal) {
11821211 state = kDone ;
11831212 try {
11841213 try {
1185- if ( ! controller . signal . aborted ) {
1186- abortSignal ( controller . signal , error ) ;
1187- }
1214+ abort . abort ( error ) ;
11881215 } finally {
11891216 cleanup ( ) ;
11901217 }
@@ -1206,17 +1233,16 @@ function createAsyncTransformPipeline(source, transforms, signal) {
12061233 // A transform can abort while completing without producing a final
12071234 // batch, for example when an async flush resolves to null. In that
12081235 // case there is no batch with which to observe the abort.
1209- controller . signal . throwIfAborted ( ) ;
1236+ abort . throwIfAborted ( ) ;
12101237 completed = true ;
12111238 return complete ( new IterResult ( true , undefined ) ) ;
12121239 }
12131240 value = result . value ;
12141241 } catch ( error ) {
12151242 return fail ( error ) ;
12161243 }
1217- try {
1218- controller . signal . throwIfAborted ( ) ;
1219- } catch ( error ) {
1244+ if ( abort . aborted ) {
1245+ const error = abort . reason ;
12201246 // Like for await when its body throws: close the transforms first.
12211247 return PromisePrototypeThen (
12221248 closeAsyncIterator ( iterator , true ) , ( ) => fail ( error ) ) ;
@@ -1249,17 +1275,16 @@ function createAsyncTransformPipeline(source, transforms, signal) {
12491275 }
12501276 state = kActive ;
12511277 const normalized = yieldAbortable ( source , signal ) ;
1252- // Create internal controller for transform cancellation.
1253- controller = new AbortController ( ) ;
1278+ abort = new PipelineAbort ( ) ;
12541279 if ( signal ) {
12551280 abortHandler = ( ) => {
1256- abortSignal ( controller . signal , signal . reason ) ;
1281+ abort . abort ( signal . reason ) ;
12571282 } ;
12581283 signal . addEventListener ( 'abort' , abortHandler , kNullOnceOption ) ;
12591284 }
12601285 try {
12611286 const current = createAsyncTransformLayers (
1262- normalized , transforms , controller . signal ) ;
1287+ normalized , transforms , abort ) ;
12631288 iterator = current [ SymbolAsyncIterator ] ( ) ;
12641289 nextMethod = iterator . next ;
12651290 } catch ( error ) {
0 commit comments