@@ -16,6 +16,10 @@ const { |
16 | 16 | DataViewPrototypeGetByteOffset, |
17 | 17 | FunctionPrototypeCall, |
18 | 18 | PromisePrototypeThen, |
| 19 | + PromiseResolve, |
| 20 | + PromiseWithResolvers, |
| 21 | + SafePromiseRace, |
| 22 | + Symbol, |
19 | 23 | SymbolAsyncIterator, |
20 | 24 | SymbolIterator, |
21 | 25 | TypedArrayPrototypeGetBuffer, |
@@ -29,8 +33,10 @@ const { markPromiseAsHandled } = internalBinding('util'); |
29 | 33 | const { |
30 | 34 | codes: { |
31 | 35 | ERR_INVALID_ARG_TYPE, |
| 36 | + ERR_INVALID_RETURN_VALUE, |
32 | 37 | }, |
33 | 38 | } = require('internal/errors'); |
| 39 | +const { lazyDOMException } = require('internal/util'); |
34 | 40 |
|
35 | 41 | const { |
36 | 42 | isAnyArrayBuffer, |
@@ -54,6 +60,81 @@ const { |
54 | 60 | // Bounds peak memory when arrays flow through transforms, which must |
55 | 61 | // allocate output for the entire batch at once. |
56 | 62 | const FROM_BATCH_SIZE = 128; |
| 63 | +const kNormalizationCancelled = Symbol('kNormalizationCancelled'); |
| 64 | + |
| 65 | +function createNormalizationContext() { |
| 66 | + return { |
| 67 | + __proto__: null, |
| 68 | + cancelled: false, |
| 69 | + reason: undefined, |
| 70 | + resolve: null, |
| 71 | + suppressCleanup: false, |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +function cancelNormalization(context, reason, suppressCleanup = false) { |
| 76 | + if (context.cancelled) return; |
| 77 | + context.cancelled = true; |
| 78 | + context.reason = reason; |
| 79 | + context.suppressCleanup = suppressCleanup; |
| 80 | + context.resolve?.(kNormalizationCancelled); |
| 81 | +} |
| 82 | + |
| 83 | +function throwIfNormalizationCancelled(context) { |
| 84 | + if (context?.cancelled) throw context.reason; |
| 85 | +} |
| 86 | + |
| 87 | +async function waitForNormalization(value, context) { |
| 88 | + if (context === undefined) return value; |
| 89 | + const { promise, resolve } = PromiseWithResolvers(); |
| 90 | + if (context.cancelled) { |
| 91 | + resolve(kNormalizationCancelled); |
| 92 | + } else { |
| 93 | + context.resolve = resolve; |
| 94 | + } |
| 95 | + try { |
| 96 | + const result = await SafePromiseRace([ |
| 97 | + PromiseResolve(value), |
| 98 | + promise, |
| 99 | + ]); |
| 100 | + throwIfNormalizationCancelled(context); |
| 101 | + return result; |
| 102 | + } finally { |
| 103 | + if (context.resolve === resolve) context.resolve = null; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function createNormalizationIterator(createIterator) { |
| 108 | + const context = createNormalizationContext(); |
| 109 | + const iterator = createIterator(context); |
| 110 | + return { |
| 111 | + __proto__: null, |
| 112 | + next(value) { |
| 113 | + return FunctionPrototypeCall(iterator.next, iterator, value); |
| 114 | + }, |
| 115 | + return(value) { |
| 116 | + cancelNormalization( |
| 117 | + context, lazyDOMException('Aborted', 'AbortError')); |
| 118 | + return FunctionPrototypeCall(iterator.return, iterator, value); |
| 119 | + }, |
| 120 | + throw(error) { |
| 121 | + cancelNormalization(context, error, true); |
| 122 | + return FunctionPrototypeCall(iterator.throw, iterator, error); |
| 123 | + }, |
| 124 | + [SymbolAsyncIterator]() { |
| 125 | + return this; |
| 126 | + }, |
| 127 | + }; |
| 128 | +} |
| 129 | + |
| 130 | +function createNormalizationSource(createIterator) { |
| 131 | + return { |
| 132 | + __proto__: null, |
| 133 | + [SymbolAsyncIterator]() { |
| 134 | + return createNormalizationIterator(createIterator); |
| 135 | + }, |
| 136 | + }; |
| 137 | +} |
57 | 138 |
|
58 | 139 | // ============================================================================= |
59 | 140 | // Type Guards and Detection |
@@ -256,6 +337,101 @@ function* normalizeSyncSource(source) { |
256 | 337 | } |
257 | 338 | } |
258 | 339 |
|
| 340 | +function yieldNormalizationAbortable(source, context) { |
| 341 | + if (context === undefined) return source; |
| 342 | + return { |
| 343 | + __proto__: null, |
| 344 | + [SymbolAsyncIterator]() { |
| 345 | + const iteratorMethod = source[SymbolAsyncIterator]; |
| 346 | + const iterator = FunctionPrototypeCall(iteratorMethod, source); |
| 347 | + const nextMethod = iterator.next; |
| 348 | + let completed = false; |
| 349 | + let closed = false; |
| 350 | + let reading = false; |
| 351 | + |
| 352 | + async function closeSource(suppressError) { |
| 353 | + if (closed) return; |
| 354 | + closed = true; |
| 355 | + completed = true; |
| 356 | + |
| 357 | + if (suppressError) { |
| 358 | + try { |
| 359 | + const returnMethod = iterator.return; |
| 360 | + if (typeof returnMethod === 'function') { |
| 361 | + const cleanup = PromisePrototypeThen( |
| 362 | + PromiseResolve(), |
| 363 | + () => FunctionPrototypeCall(returnMethod, iterator)); |
| 364 | + markPromiseAsHandled(cleanup); |
| 365 | + } |
| 366 | + } catch { |
| 367 | + // Cancellation has precedence over source cleanup errors. |
| 368 | + } |
| 369 | + return; |
| 370 | + } |
| 371 | + |
| 372 | + const returnMethod = iterator.return; |
| 373 | + if (typeof returnMethod === 'function') { |
| 374 | + const result = await FunctionPrototypeCall(returnMethod, iterator); |
| 375 | + if ((typeof result !== 'object' && typeof result !== 'function') || |
| 376 | + result === null) { |
| 377 | + throw new ERR_INVALID_RETURN_VALUE( |
| 378 | + 'an object', 'iterator.return()', result); |
| 379 | + } |
| 380 | + } |
| 381 | + } |
| 382 | + |
| 383 | + return { |
| 384 | + __proto__: null, |
| 385 | + async next() { |
| 386 | + if (completed) { |
| 387 | + return { __proto__: null, done: true, value: undefined }; |
| 388 | + } |
| 389 | + throwIfNormalizationCancelled(context); |
| 390 | + reading = true; |
| 391 | + |
| 392 | + try { |
| 393 | + const next = FunctionPrototypeCall(nextMethod, iterator); |
| 394 | + const result = await waitForNormalization(next, context); |
| 395 | + if ((typeof result !== 'object' && typeof result !== 'function') || |
| 396 | + result === null) { |
| 397 | + throw new ERR_INVALID_RETURN_VALUE( |
| 398 | + 'an object', 'iterator.next()', result); |
| 399 | + } |
| 400 | + if (result.done) { |
| 401 | + reading = false; |
| 402 | + throwIfNormalizationCancelled(context); |
| 403 | + completed = true; |
| 404 | + closed = true; |
| 405 | + return { __proto__: null, done: true, value: result.value }; |
| 406 | + } |
| 407 | + const value = result.value; |
| 408 | + reading = false; |
| 409 | + throwIfNormalizationCancelled(context); |
| 410 | + return { __proto__: null, done: false, value }; |
| 411 | + } catch (error) { |
| 412 | + if (context.cancelled) await closeSource(true); |
| 413 | + reading = false; |
| 414 | + throw error; |
| 415 | + } |
| 416 | + }, |
| 417 | + async return(value) { |
| 418 | + await closeSource( |
| 419 | + context.suppressCleanup || (context.cancelled && reading)); |
| 420 | + return { __proto__: null, done: true, value }; |
| 421 | + }, |
| 422 | + async throw(error) { |
| 423 | + await closeSource( |
| 424 | + context.suppressCleanup || (context.cancelled && reading)); |
| 425 | + throw error; |
| 426 | + }, |
| 427 | + [SymbolAsyncIterator]() { |
| 428 | + return this; |
| 429 | + }, |
| 430 | + }; |
| 431 | + }, |
| 432 | + }; |
| 433 | +} |
| 434 | + |
259 | 435 | // ============================================================================= |
260 | 436 | // Async Normalization (for from and async contexts) |
261 | 437 | // ============================================================================= |
@@ -266,11 +442,15 @@ function* normalizeSyncSource(source) { |
266 | 442 | * and protocol conversions. |
267 | 443 | * @yields {Uint8Array} |
268 | 444 | */ |
269 | | -async function* normalizeAsyncValue(value, allowNestedAsyncStreamables = true) { |
| 445 | +async function* normalizeAsyncValue( |
| 446 | + value, allowNestedAsyncStreamables = true, context) { |
| 447 | + throwIfNormalizationCancelled(context); |
| 448 | + |
270 | 449 | // Handle promises first |
271 | 450 | if (isPromise(value)) { |
272 | | - const resolved = await value; |
273 | | - yield* normalizeAsyncValue(resolved, allowNestedAsyncStreamables); |
| 451 | + const resolved = await waitForNormalization(value, context); |
| 452 | + yield* normalizeAsyncValue( |
| 453 | + resolved, allowNestedAsyncStreamables, context); |
274 | 454 | return; |
275 | 455 | } |
276 | 456 |
|
@@ -293,41 +473,47 @@ async function* normalizeAsyncValue(value, allowNestedAsyncStreamables = true) { |
293 | 473 | if (hasProtocol(value, toAsyncStreamable)) { |
294 | 474 | const result = FunctionPrototypeCall(value[toAsyncStreamable], value); |
295 | 475 | if (isPromise(result)) { |
296 | | - yield* normalizeAsyncValue(await result, allowNestedAsyncStreamables); |
| 476 | + yield* normalizeAsyncValue( |
| 477 | + await waitForNormalization(result, context), |
| 478 | + allowNestedAsyncStreamables, |
| 479 | + context); |
297 | 480 | } else { |
298 | | - yield* normalizeAsyncValue(result, allowNestedAsyncStreamables); |
| 481 | + yield* normalizeAsyncValue( |
| 482 | + result, allowNestedAsyncStreamables, context); |
299 | 483 | } |
300 | 484 | return; |
301 | 485 | } |
302 | 486 |
|
303 | 487 | // Handle ToStreamable protocol |
304 | 488 | if (hasProtocol(value, toStreamable)) { |
305 | 489 | const result = FunctionPrototypeCall(value[toStreamable], value); |
306 | | - yield* normalizeAsyncValue(result, allowNestedAsyncStreamables); |
| 490 | + yield* normalizeAsyncValue(result, allowNestedAsyncStreamables, context); |
307 | 491 | return; |
308 | 492 | } |
309 | 493 |
|
310 | 494 | // Handle arrays (which are also iterable, but check first for efficiency) |
311 | 495 | if (ArrayIsArray(value)) { |
312 | 496 | for (let i = 0; i < value.length; i++) { |
313 | | - yield* normalizeAsyncValue(value[i], allowNestedAsyncStreamables); |
| 497 | + yield* normalizeAsyncValue( |
| 498 | + value[i], allowNestedAsyncStreamables, context); |
314 | 499 | } |
315 | 500 | return; |
316 | 501 | } |
317 | 502 |
|
318 | 503 | // Handle async iterables (check before sync iterables since some objects |
319 | 504 | // have both) |
320 | 505 | if (isAsyncIterable(value)) { |
321 | | - for await (const item of value) { |
322 | | - yield* normalizeAsyncValue(item, allowNestedAsyncStreamables); |
| 506 | + const iterable = yieldNormalizationAbortable(value, context); |
| 507 | + for await (const item of iterable) { |
| 508 | + yield* normalizeAsyncValue(item, allowNestedAsyncStreamables, context); |
323 | 509 | } |
324 | 510 | return; |
325 | 511 | } |
326 | 512 |
|
327 | 513 | // Handle sync iterables |
328 | 514 | if (isSyncIterable(value)) { |
329 | 515 | for (const item of value) { |
330 | | - yield* normalizeAsyncValue(item, allowNestedAsyncStreamables); |
| 516 | + yield* normalizeAsyncValue(item, allowNestedAsyncStreamables, context); |
331 | 517 | } |
332 | 518 | return; |
333 | 519 | } |
@@ -346,10 +532,13 @@ async function* normalizeAsyncValue(value, allowNestedAsyncStreamables = true) { |
346 | 532 | * @param {AsyncIterable|Iterable} source |
347 | 533 | * @yields {Uint8Array[]} |
348 | 534 | */ |
349 | | -async function* normalizeAsyncSource(source) { |
| 535 | +async function* normalizeAsyncSource(source, context) { |
| 536 | + throwIfNormalizationCancelled(context); |
| 537 | + |
350 | 538 | // Prefer async iteration if available |
351 | 539 | if (isAsyncIterable(source)) { |
352 | | - for await (const value of source) { |
| 540 | + const iterable = yieldNormalizationAbortable(source, context); |
| 541 | + for await (const value of iterable) { |
353 | 542 | // Fast path 1: value is already a Uint8Array[] batch |
354 | 543 | if (isUint8ArrayBatch(value)) { |
355 | 544 | if (value.length > 0) { |
@@ -364,7 +553,7 @@ async function* normalizeAsyncSource(source) { |
364 | 553 | } |
365 | 554 | // Slow path: normalize the value |
366 | 555 | let batch = []; |
367 | | - for await (const chunk of normalizeAsyncValue(value)) { |
| 556 | + for await (const chunk of normalizeAsyncValue(value, true, context)) { |
368 | 557 | ArrayPrototypePush(batch, chunk); |
369 | 558 | if (batch.length === FROM_BATCH_SIZE) { |
370 | 559 | yield batch; |
@@ -383,6 +572,7 @@ async function* normalizeAsyncSource(source) { |
383 | 572 | let batch = []; |
384 | 573 |
|
385 | 574 | for (const value of source) { |
| 575 | + throwIfNormalizationCancelled(context); |
386 | 576 | // Fast path 1: value is already a Uint8Array[] batch |
387 | 577 | if (isUint8ArrayBatch(value)) { |
388 | 578 | // Flush any accumulated batch first |
@@ -408,7 +598,7 @@ async function* normalizeAsyncSource(source) { |
408 | 598 | batch = []; |
409 | 599 | } |
410 | 600 | let asyncBatch = []; |
411 | | - for await (const chunk of normalizeAsyncValue(value, false)) { |
| 601 | + for await (const chunk of normalizeAsyncValue(value, false, context)) { |
412 | 602 | ArrayPrototypePush(asyncBatch, chunk); |
413 | 603 | if (asyncBatch.length === FROM_BATCH_SIZE) { |
414 | 604 | yield asyncBatch; |
@@ -434,6 +624,12 @@ async function* normalizeAsyncSource(source) { |
434 | 624 | ); |
435 | 625 | } |
436 | 626 |
|
| 627 | +async function* normalizeAsyncStreamableResult(result, context) { |
| 628 | + const resolved = await waitForNormalization(result, context); |
| 629 | + const source = resolved?.[kValidatedSource] ? resolved : from(resolved); |
| 630 | + yield* yieldNormalizationAbortable(source, context); |
| 631 | +} |
| 632 | + |
437 | 633 | // ============================================================================= |
438 | 634 | // Public API: from() and fromSync() |
439 | 635 | // ============================================================================= |
@@ -610,19 +806,8 @@ function from(input) { |
610 | 806 | if (result?.[kValidatedSource]) { |
611 | 807 | return result; |
612 | 808 | } |
613 | | - return { |
614 | | - __proto__: null, |
615 | | - async *[SymbolAsyncIterator]() { |
616 | | - // The result may be a Promise. Check validated on both the Promise |
617 | | - // itself (if tagged) and the resolved value. |
618 | | - const resolved = await result; |
619 | | - if (resolved?.[kValidatedSource]) { |
620 | | - yield* resolved[SymbolAsyncIterator](); |
621 | | - return; |
622 | | - } |
623 | | - yield* from(resolved)[SymbolAsyncIterator](); |
624 | | - }, |
625 | | - }; |
| 809 | + return createNormalizationSource( |
| 810 | + (context) => normalizeAsyncStreamableResult(result, context)); |
626 | 811 | } |
627 | 812 |
|
628 | 813 | // Check toStreamable protocol (takes precedence over iteration protocols) |
@@ -640,7 +825,8 @@ function from(input) { |
640 | 825 | ); |
641 | 826 | } |
642 | 827 |
|
643 | | - return normalizeAsyncSource(input); |
| 828 | + return createNormalizationIterator( |
| 829 | + (context) => normalizeAsyncSource(input, context)); |
644 | 830 | } |
645 | 831 |
|
646 | 832 | // ============================================================================= |
|