@@ -631,59 +631,263 @@ async function* normalizeAsyncValue( |
631 | 631 | } |
632 | 632 |
|
633 | 633 | /** |
634 | | - * Normalize an async streamable source, yielding batches of Uint8Array. |
635 | | - * @param {AsyncIterable|Iterable} source |
| 634 | + * Normalize a value of an async source that is neither a Uint8Array nor a |
| 635 | + * Uint8Array[] batch, yielding batches of Uint8Array. Chunks are batched, |
| 636 | + * but whatever has been collected is yielded before waiting on async content |
| 637 | + * (such as a nested async iterable), so data is not held back until it ends. |
| 638 | + * @param {any} value |
| 639 | + * @param {object} context |
636 | 640 | * @yields {Uint8Array[]} |
637 | 641 | */ |
638 | | -async function* normalizeAsyncSource(source, context) { |
639 | | - throwIfNormalizationCancelled(context); |
640 | | - |
641 | | - // Prefer async iteration if available |
642 | | - if (isAsyncIterable(source)) { |
643 | | - const iterable = yieldNormalizationAbortable(source, context); |
644 | | - for await (const value of iterable) { |
645 | | - // Fast path 1: value is already a Uint8Array[] batch. Yield a batch |
646 | | - // within the bound directly: yield* of a sync generator from an async |
647 | | - // generator costs several extra promise ticks per batch. |
648 | | - if (isUint8ArrayBatch(value)) { |
649 | | - if (value.length <= FROM_BATCH_SIZE) { |
650 | | - if (value.length !== 0) yield value; |
651 | | - } else { |
652 | | - yield* yieldBoundedBatch(value); |
653 | | - } |
654 | | - continue; |
655 | | - } |
656 | | - // Fast path 2: value is a single Uint8Array (very common) |
657 | | - if (isUint8Array(value)) { |
658 | | - yield [value]; |
659 | | - continue; |
660 | | - } |
661 | | - // Slow path: normalize the value. Chunks are batched, but whatever has |
662 | | - // been collected is yielded before waiting on async content (such as a |
663 | | - // nested async iterable), so data is not held back until it ends. |
664 | | - let batch = []; |
665 | | - for await (const chunk of |
666 | | - normalizeAsyncValue(value, true, context, true)) { |
667 | | - if (chunk === kFlushBatch) { |
668 | | - if (batch.length > 0) { |
669 | | - yield batch; |
670 | | - batch = []; |
671 | | - } |
672 | | - continue; |
673 | | - } |
674 | | - ArrayPrototypePush(batch, chunk); |
675 | | - if (batch.length === FROM_BATCH_SIZE) { |
676 | | - yield batch; |
677 | | - batch = []; |
678 | | - } |
679 | | - } |
| 642 | +async function* normalizeAsyncSourceValue(value, context) { |
| 643 | + let batch = []; |
| 644 | + for await (const chunk of |
| 645 | + normalizeAsyncValue(value, true, context, true)) { |
| 646 | + if (chunk === kFlushBatch) { |
680 | 647 | if (batch.length > 0) { |
681 | 648 | yield batch; |
| 649 | + batch = []; |
682 | 650 | } |
| 651 | + continue; |
683 | 652 | } |
684 | | - return; |
| 653 | + ArrayPrototypePush(batch, chunk); |
| 654 | + if (batch.length === FROM_BATCH_SIZE) { |
| 655 | + yield batch; |
| 656 | + batch = []; |
| 657 | + } |
| 658 | + } |
| 659 | + if (batch.length > 0) { |
| 660 | + yield batch; |
| 661 | + } |
| 662 | +} |
| 663 | + |
| 664 | +const kStart = 0; |
| 665 | +const kActive = 1; |
| 666 | +const kDone = 2; |
| 667 | + |
| 668 | +/** |
| 669 | + * Iterator normalizing an async iterable source into batches of Uint8Array. |
| 670 | + * |
| 671 | + * This is what an async generator looping over the source with for await |
| 672 | + * would do, written out by hand: the source is read for every batch, and an |
| 673 | + * async generator layer costs several promises and an async frame each |
| 674 | + * time. Values that are already batches or Uint8Arrays (the common case) |
| 675 | + * are handled with one promise per batch; any other value is normalized by |
| 676 | + * normalizeAsyncSourceValue(). As with an async generator: |
| 677 | + * - nothing happens until the first next(); |
| 678 | + * - calls made while one is in progress are queued; |
| 679 | + * - an error from the source ends the iteration without closing the source, |
| 680 | + * and an error normalizing a value closes the source first; |
| 681 | + * - return() closes the value being normalized and the source, propagating |
| 682 | + * errors from closing them, and throw() closes them ignoring such errors. |
| 683 | + * @param {AsyncIterable} source |
| 684 | + * @param {object} context |
| 685 | + * @returns {object} An object with next(), return() and throw(). |
| 686 | + */ |
| 687 | +function createAsyncSourceNormalizer(source, context) { |
| 688 | + let state = kStart; |
| 689 | + // The source iterator, from yieldNormalizationAbortable(). Its return() |
| 690 | + // is an async function, so calling it cannot throw synchronously. |
| 691 | + let iterator; |
| 692 | + // The value being normalized: a sync iterator of the sub-batches of an |
| 693 | + // oversized batch, or a normalizeAsyncSourceValue() generator. |
| 694 | + let boundedBatches = null; |
| 695 | + let valueBatches = null; |
| 696 | + let busy = false; |
| 697 | + let queue = null; |
| 698 | + |
| 699 | + function drain() { |
| 700 | + if (busy || queue === null) return; |
| 701 | + const { 0: method, 1: arg, 2: resolve, 3: reject } = queue.shift(); |
| 702 | + if (queue.length === 0) queue = null; |
| 703 | + busy = true; |
| 704 | + PromisePrototypeThen(method(arg), resolve, reject); |
| 705 | + } |
| 706 | + |
| 707 | + function settled() { |
| 708 | + busy = false; |
| 709 | + if (queue !== null) PromisePrototypeThen(kResolvedPromise, drain); |
| 710 | + } |
| 711 | + |
| 712 | + function finish(result) { |
| 713 | + settled(); |
| 714 | + return result; |
| 715 | + } |
| 716 | + |
| 717 | + function fail(error) { |
| 718 | + state = kDone; |
| 719 | + settled(); |
| 720 | + throw error; |
| 721 | + } |
| 722 | + |
| 723 | + function run(method, arg) { |
| 724 | + if (busy || queue !== null) { |
| 725 | + const { promise, resolve, reject } = PromiseWithResolvers(); |
| 726 | + queue ??= []; |
| 727 | + ArrayPrototypePush(queue, [method, arg, resolve, reject]); |
| 728 | + return promise; |
| 729 | + } |
| 730 | + busy = true; |
| 731 | + return method(arg); |
| 732 | + } |
| 733 | + |
| 734 | + // Closing for a throw completion: wait, but ignore errors. |
| 735 | + function closeQuietly(it) { |
| 736 | + return PromisePrototypeThen(it.return(), undefined, () => {}); |
685 | 737 | } |
686 | 738 |
|
| 739 | + function onBodyError(error) { |
| 740 | + // Like for await when its body throws: close the source, keep the error. |
| 741 | + valueBatches = null; |
| 742 | + boundedBatches = null; |
| 743 | + state = kDone; |
| 744 | + return PromisePrototypeThen( |
| 745 | + closeQuietly(iterator), () => fail(error)); |
| 746 | + } |
| 747 | + |
| 748 | + function onSourceResult(result) { |
| 749 | + if (result.done) { |
| 750 | + state = kDone; |
| 751 | + return finish(new IterResult(true, undefined)); |
| 752 | + } |
| 753 | + try { |
| 754 | + return handleValue(result.value); |
| 755 | + } catch (error) { |
| 756 | + return onBodyError(error); |
| 757 | + } |
| 758 | + } |
| 759 | + |
| 760 | + function handleValue(value) { |
| 761 | + if (isUint8ArrayBatch(value)) { |
| 762 | + if (value.length <= FROM_BATCH_SIZE) { |
| 763 | + if (value.length === 0) return pullSource(); |
| 764 | + return finish(new IterResult(false, value)); |
| 765 | + } |
| 766 | + boundedBatches = yieldBoundedBatch(value); |
| 767 | + return pullValue(); |
| 768 | + } |
| 769 | + if (isUint8Array(value)) return finish(new IterResult(false, [value])); |
| 770 | + valueBatches = normalizeAsyncSourceValue(value, context); |
| 771 | + return pullValue(); |
| 772 | + } |
| 773 | + |
| 774 | + function onValueResult(result) { |
| 775 | + if (result.done) { |
| 776 | + valueBatches = null; |
| 777 | + return pullSource(); |
| 778 | + } |
| 779 | + return finish(new IterResult(false, result.value)); |
| 780 | + } |
| 781 | + |
| 782 | + function pullSource() { |
| 783 | + return PromisePrototypeThen(iterator.next(), onSourceResult, fail); |
| 784 | + } |
| 785 | + |
| 786 | + function pullValue() { |
| 787 | + if (boundedBatches !== null) { |
| 788 | + const result = boundedBatches.next(); |
| 789 | + if (!result.done) return finish(new IterResult(false, result.value)); |
| 790 | + boundedBatches = null; |
| 791 | + return pullSource(); |
| 792 | + } |
| 793 | + return PromisePrototypeThen( |
| 794 | + valueBatches.next(), onValueResult, onBodyError); |
| 795 | + } |
| 796 | + |
| 797 | + function doNext() { |
| 798 | + if (state === kDone) { |
| 799 | + return PromiseResolve(finish(new IterResult(true, undefined))); |
| 800 | + } |
| 801 | + if (state === kStart) { |
| 802 | + try { |
| 803 | + throwIfNormalizationCancelled(context); |
| 804 | + const iterable = yieldNormalizationAbortable(source, context); |
| 805 | + iterator = iterable[SymbolAsyncIterator](); |
| 806 | + } catch (error) { |
| 807 | + state = kDone; |
| 808 | + settled(); |
| 809 | + return PromiseReject(error); |
| 810 | + } |
| 811 | + state = kActive; |
| 812 | + } |
| 813 | + if (boundedBatches !== null || valueBatches !== null) { |
| 814 | + return PromiseResolve(pullValue()); |
| 815 | + } |
| 816 | + return pullSource(); |
| 817 | + } |
| 818 | + |
| 819 | + function doReturn(value) { |
| 820 | + const result = new IterResult(true, value); |
| 821 | + if (state !== kActive) { |
| 822 | + state = kDone; |
| 823 | + return PromiseResolve(finish(result)); |
| 824 | + } |
| 825 | + state = kDone; |
| 826 | + boundedBatches = null; |
| 827 | + const pending = valueBatches; |
| 828 | + valueBatches = null; |
| 829 | + // Close the value being normalized, then the source. If closing the |
| 830 | + // value fails, the source is still closed and the error kept. |
| 831 | + let closed; |
| 832 | + if (pending === null) { |
| 833 | + closed = iterator.return(); |
| 834 | + } else { |
| 835 | + closed = PromisePrototypeThen( |
| 836 | + pending.return(), |
| 837 | + () => iterator.return(), |
| 838 | + (error) => PromisePrototypeThen(closeQuietly(iterator), () => { |
| 839 | + throw error; |
| 840 | + })); |
| 841 | + } |
| 842 | + return PromisePrototypeThen(closed, () => finish(result), fail); |
| 843 | + } |
| 844 | + |
| 845 | + function doThrow(error) { |
| 846 | + if (state !== kActive) { |
| 847 | + state = kDone; |
| 848 | + settled(); |
| 849 | + return PromiseReject(error); |
| 850 | + } |
| 851 | + state = kDone; |
| 852 | + boundedBatches = null; |
| 853 | + const pending = valueBatches; |
| 854 | + valueBatches = null; |
| 855 | + const closed = pending === null ? closeQuietly(iterator) : |
| 856 | + PromisePrototypeThen(closeQuietly(pending), () => closeQuietly(iterator)); |
| 857 | + return PromisePrototypeThen(closed, () => fail(error)); |
| 858 | + } |
| 859 | + |
| 860 | + return ObjectSetPrototypeOf({ |
| 861 | + next() { return run(doNext); }, |
| 862 | + return(value) { return run(doReturn, value); }, |
| 863 | + throw(error) { return run(doThrow, error); }, |
| 864 | + }, null); |
| 865 | +} |
| 866 | + |
| 867 | +/** |
| 868 | + * Normalize an async streamable source, yielding batches of Uint8Array. |
| 869 | + * @param {AsyncIterable|Iterable} source |
| 870 | + * @param {object} context |
| 871 | + * @returns {object} An async iterator. |
| 872 | + */ |
| 873 | +function normalizeAsyncSource(source, context) { |
| 874 | + // Prefer async iteration if available. |
| 875 | + if (isAsyncIterable(source)) { |
| 876 | + return createAsyncSourceNormalizer(source, context); |
| 877 | + } |
| 878 | + return normalizeSyncSourceAsync(source, context); |
| 879 | +} |
| 880 | + |
| 881 | +/** |
| 882 | + * Normalize a sync streamable source for from(), yielding batches of |
| 883 | + * Uint8Array. |
| 884 | + * @param {Iterable} source |
| 885 | + * @param {object} context |
| 886 | + * @yields {Uint8Array[]} |
| 887 | + */ |
| 888 | +async function* normalizeSyncSourceAsync(source, context) { |
| 889 | + throwIfNormalizationCancelled(context); |
| 890 | + |
687 | 891 | // Fall back to sync iteration - batch sync values together with a bound. |
688 | 892 | if (isSyncIterable(source)) { |
689 | 893 | let batch = []; |
|