GeezerVison is showing github.com — stream: apply source normalization once at call time · nodejs/node@cafdee6
File treeExpand file treeCollapse file tree Open diff view settings lib/internal/streams/iter
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/stream_iter.md +6-3Lines changed: 6 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -693,8 +693,10 @@ added: v25.9.0 | 693 | 693 | * `signal` {AbortSignal} Abort the pipeline. | 694 | 694 | * Returns: {AsyncIterable} whose chunks fulfill with {Uint8Array\[]} | 695 | 695 |
| 696 | | -Create a lazy async pipeline. Data is not read from `source` until the | 697 | | -returned iterable is consumed. Transforms are applied in order. | | 696 | +Create a lazy async pipeline. Source conversion and streamable protocol | | 697 | +dispatch occur when `pull()` is called, but data is not read from `source` | | 698 | +until the returned iterable is consumed. A signal that is already aborted is | | 699 | +thrown synchronously after source conversion. Transforms are applied in order. | 698 | 700 |
| 699 | 701 | ```mjs | 700 | 702 | import { from, pull, text } from 'node:stream/iter'; | @@ -762,7 +764,8 @@ added: v25.9.0 | 762 | 764 | * `...transforms` {Function|Object} Zero or more sync transforms. | 763 | 765 | * Returns: {Iterable} whose chunks return {Uint8Array\[]} | 764 | 766 |
| 765 | | -Synchronous version of [`pull()`][]. All transforms must be synchronous. | | 767 | +Synchronous version of [`pull()`][]. Source conversion and streamable protocol | | 768 | +dispatch occur when `pullSync()` is called. All transforms must be synchronous. | 766 | 769 |
| 767 | 770 | ## Push streams | 768 | 771 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/pull.js +20-15Lines changed: 20 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -805,6 +805,7 @@ async function* createAsyncPipeline(source, transforms, signal) { | 805 | 805 | * @returns {Iterable<Uint8Array[]>} | 806 | 806 | */ | 807 | 807 | function pullSync(source, ...transforms) { | | 808 | + const normalized = fromSync(source); | 808 | 809 | for (let i = 0; i < transforms.length; i++) { | 809 | 810 | if (!isTransform(transforms[i])) { | 810 | 811 | throw new ERR_INVALID_ARG_TYPE( | @@ -815,7 +816,7 @@ function pullSync(source, ...transforms) { | 815 | 816 | return { | 816 | 817 | __proto__: null, | 817 | 818 | *[SymbolIterator]() { | 818 | | - yield* createSyncPipeline(fromSync(source), transforms); | | 819 | + yield* createSyncPipeline(normalized, transforms); | 819 | 820 | }, | 820 | 821 | }; | 821 | 822 | } | @@ -832,17 +833,9 @@ function pull(source, ...args) { | 832 | 833 | const signal = options?.signal; | 833 | 834 | if (signal !== undefined) { | 834 | 835 | validateAbortSignal(signal, 'options.signal'); | 835 | | - // Eagerly check abort at call time per spec | 836 | | - if (signal.aborted) { | 837 | | - return { | 838 | | - __proto__: null, | 839 | | - // eslint-disable-next-line require-yield | 840 | | - async *[SymbolAsyncIterator]() { | 841 | | - throw signal.reason; | 842 | | - }, | 843 | | - }; | 844 | | - } | 845 | 836 | } | | 837 | + const normalized = from(source); | | 838 | + signal?.throwIfAborted(); | 846 | 839 |
| 847 | 840 | return { | 848 | 841 | __proto__: null, | @@ -852,7 +845,7 @@ function pull(source, ...args) { | 852 | 845 | controller.signal : AbortSignal.any([signal, controller.signal]); | 853 | 846 |
| 854 | 847 | async function* pipeline() { | 855 | | - yield* createAsyncPipeline(from(source), transforms, iteratorSignal); | | 848 | + yield* createAsyncPipeline(normalized, transforms, iteratorSignal); | 856 | 849 | } | 857 | 850 | const iterator = pipeline(); | 858 | 851 |
| @@ -887,9 +880,6 @@ function pullWithConsumerCleanup(source, transforms, signal) { | 887 | 880 | return sourceIterator; | 888 | 881 | }, | 889 | 882 | }; | 890 | | - const pipeline = signal === undefined ? | 891 | | - pull(pipelineSource, ...transforms) : | 892 | | - pull(pipelineSource, ...transforms, { __proto__: null, signal }); | 893 | 883 | let sourceClosed = false; | 894 | 884 | let abortHandler; | 895 | 885 |
| @@ -906,6 +896,21 @@ function pullWithConsumerCleanup(source, transforms, signal) { | 906 | 896 | } | 907 | 897 | } | 908 | 898 |
| | 899 | + if (signal?.aborted) { | | 900 | + closeSource('throw', signal.reason); | | 901 | + return { | | 902 | + __proto__: null, | | 903 | + // eslint-disable-next-line require-yield | | 904 | + async *[SymbolAsyncIterator]() { | | 905 | + throw signal.reason; | | 906 | + }, | | 907 | + }; | | 908 | + } | | 909 | + | | 910 | + const pipeline = signal === undefined ? | | 911 | + pull(pipelineSource, ...transforms) : | | 912 | + pull(pipelineSource, ...transforms, { __proto__: null, signal }); | | 913 | + | 909 | 914 | if (signal !== undefined) { | 910 | 915 | abortHandler = () => closeSource('throw', signal.reason); | 911 | 916 | signal.addEventListener('abort', abortHandler, | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-pull-async.js +47-8Lines changed: 47 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,6 +11,7 @@ const { | 11 | 11 | share, | 12 | 12 | tap, | 13 | 13 | text, | | 14 | + toAsyncStreamable, | 14 | 15 | } = require('stream/iter'); | 15 | 16 |
| 16 | 17 | async function testPullIdentity() { | @@ -53,18 +54,54 @@ async function testPullWithAbortSignal() { | 53 | 54 | yield [new Uint8Array([1])]; | 54 | 55 | } | 55 | 56 |
| 56 | | - const result = pull(gen(), { signal: AbortSignal.abort() }); | 57 | | - await assert.rejects( | 58 | | - async () => { | 59 | | - // eslint-disable-next-line no-unused-vars | 60 | | - for await (const _ of result) { | 61 | | - assert.fail('Should not reach here'); | 62 | | - } | 63 | | - }, | | 57 | + assert.throws( | | 58 | + () => pull(gen(), { signal: AbortSignal.abort() }), | 64 | 59 | { name: 'AbortError' }, | 65 | 60 | ); | 66 | 61 | } | 67 | 62 |
| | 63 | +async function testPullNormalizesSourceAtCallTime() { | | 64 | + let protocolCalls = 0; | | 65 | + let iteratorCalls = 0; | | 66 | + const source = { | | 67 | + [toAsyncStreamable]() { | | 68 | + protocolCalls++; | | 69 | + return { | | 70 | + async *[Symbol.asyncIterator]() { | | 71 | + iteratorCalls++; | | 72 | + yield 'data'; | | 73 | + }, | | 74 | + }; | | 75 | + }, | | 76 | + }; | | 77 | + | | 78 | + const result = pull(source); | | 79 | + assert.strictEqual(protocolCalls, 1); | | 80 | + assert.strictEqual(iteratorCalls, 0); | | 81 | + assert.strictEqual(await text(result), 'data'); | | 82 | + assert.strictEqual(protocolCalls, 1); | | 83 | + assert.strictEqual(iteratorCalls, 1); | | 84 | +} | | 85 | + | | 86 | +function testPullPreAbortOrdering() { | | 87 | + const reason = new Error('already aborted'); | | 88 | + let protocolCalls = 0; | | 89 | + const source = { | | 90 | + [toAsyncStreamable]() { | | 91 | + protocolCalls++; | | 92 | + return from('data'); | | 93 | + }, | | 94 | + }; | | 95 | + const signal = AbortSignal.abort(reason); | | 96 | + | | 97 | + assert.throws(() => pull(source, { signal }), (error) => error === reason); | | 98 | + assert.strictEqual(protocolCalls, 1); | | 99 | + assert.throws( | | 100 | + () => pull(null, { signal }), | | 101 | + { code: 'ERR_INVALID_ARG_TYPE' }, | | 102 | + ); | | 103 | +} | | 104 | + | 68 | 105 | async function testPullChainedTransforms() { | 69 | 106 | const enc = new TextEncoder(); | 70 | 107 | const transforms = [ | @@ -475,6 +512,8 @@ async function testTransformOptionsNotShared() { | 475 | 512 | testPullStatelessTransform(), | 476 | 513 | testPullStatefulTransform(), | 477 | 514 | testPullWithAbortSignal(), | | 515 | + testPullNormalizesSourceAtCallTime(), | | 516 | + testPullPreAbortOrdering(), | 478 | 517 | testPullChainedTransforms(), | 479 | 518 | testPullSourceError(), | 480 | 519 | testTapCallbackError(), | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-pull-sync.js +32-1Lines changed: 32 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 |
| 4 | 4 | const common = require('../common'); | 5 | 5 | const assert = require('assert'); | 6 | | -const { pullSync, fromSync, bytesSync, tapSync } = require('stream/iter'); | | 6 | +const { | | 7 | + pullSync, | | 8 | + fromSync, | | 9 | + bytesSync, | | 10 | + tapSync, | | 11 | + toStreamable, | | 12 | +} = require('stream/iter'); | 7 | 13 |
| 8 | 14 | function testPullSyncIdentity() { | 9 | 15 | // No transforms - just pass through | 10 | 16 | const data = bytesSync(pullSync(fromSync('hello'))); | 11 | 17 | assert.deepStrictEqual(data, new TextEncoder().encode('hello')); | 12 | 18 | } | 13 | 19 |
| | 20 | +function testPullSyncNormalizesSourceAtCallTime() { | | 21 | + let protocolCalls = 0; | | 22 | + let iteratorCalls = 0; | | 23 | + const source = { | | 24 | + [toStreamable]() { | | 25 | + protocolCalls++; | | 26 | + return { | | 27 | + *[Symbol.iterator]() { | | 28 | + iteratorCalls++; | | 29 | + yield 'data'; | | 30 | + }, | | 31 | + }; | | 32 | + }, | | 33 | + }; | | 34 | + | | 35 | + const result = pullSync(source); | | 36 | + assert.strictEqual(protocolCalls, 1); | | 37 | + assert.strictEqual(iteratorCalls, 0); | | 38 | + assert.strictEqual(new TextDecoder().decode(bytesSync(result)), 'data'); | | 39 | + assert.strictEqual(protocolCalls, 1); | | 40 | + assert.strictEqual(iteratorCalls, 1); | | 41 | + assert.throws(() => pullSync(null), { code: 'ERR_INVALID_ARG_TYPE' }); | | 42 | +} | | 43 | + | 14 | 44 | function testPullSyncStatelessTransform() { | 15 | 45 | const upper = (chunks) => { | 16 | 46 | if (chunks === null) return null; | @@ -177,6 +207,7 @@ function testPullSyncInvalidTransform() { | 177 | 207 |
| 178 | 208 | Promise.all([ | 179 | 209 | testPullSyncIdentity(), | | 210 | + testPullSyncNormalizesSourceAtCallTime(), | 180 | 211 | testPullSyncStatelessTransform(), | 181 | 212 | testPullSyncStatefulTransform(), | 182 | 213 | testPullSyncChainedTransforms(), | |
|