GeezerVison is showing github.com — stream: return stream/iter from() results unchanged from from() · nodejs/node@1f791c9
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 +3-3Lines changed: 3 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -602,9 +602,9 @@ Objects implementing `Symbol.for('Stream.toAsyncStreamable')` or | 602 | 602 | precedence over the iteration protocols (`Symbol.asyncIterator`, | 603 | 603 | `Symbol.iterator`). | 604 | 604 |
| 605 | | -The readable of a [`push()`][] stream without transforms and the iterables | 606 | | -returned by [`fromReadable()`][] already yield normalized batches, so `from()` | 607 | | -returns them unchanged. | | 605 | +The readable of a [`push()`][] stream without transforms, the iterables | | 606 | +returned by [`fromReadable()`][], and the results of `from()` itself already | | 607 | +yield normalized batches, so `from()` returns them unchanged. | 608 | 608 |
| 609 | 609 | ```mjs | 610 | 610 | import { Buffer } from 'node:buffer'; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/iter/from.js +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -180,6 +180,7 @@ function createNormalizationIterator(createIterator) { | 180 | 180 | [SymbolAsyncIterator]() { | 181 | 181 | return this; | 182 | 182 | }, | | 183 | + [kValidatedSource]: true, | 183 | 184 | }, null); | 184 | 185 | } | 185 | 186 | if (iterator[kNextSyncBatch] !== undefined) { | @@ -202,6 +203,7 @@ function createNormalizationIterator(createIterator) { | 202 | 203 | [SymbolAsyncIterator]() { | 203 | 204 | return this; | 204 | 205 | }, | | 206 | + [kValidatedSource]: true, | 205 | 207 | }, null); | 206 | 208 | } | 207 | 209 | return ObjectSetPrototypeOf({ | @@ -220,6 +222,7 @@ function createNormalizationIterator(createIterator) { | 220 | 222 | [SymbolAsyncIterator]() { | 221 | 223 | return this; | 222 | 224 | }, | | 225 | + [kValidatedSource]: true, | 223 | 226 | }, null); | 224 | 227 | } | 225 | 228 |
| @@ -229,6 +232,7 @@ function createNormalizationSource(createIterator) { | 229 | 232 | [SymbolAsyncIterator]() { | 230 | 233 | return createNormalizationIterator(createIterator); | 231 | 234 | }, | | 235 | + [kValidatedSource]: true, | 232 | 236 | }; | 233 | 237 | } | 234 | 238 |
| @@ -1349,6 +1353,7 @@ function from(input) { | 1349 | 1353 | async *[SymbolAsyncIterator]() { | 1350 | 1354 | yield [chunk]; | 1351 | 1355 | }, | | 1356 | + [kValidatedSource]: true, | 1352 | 1357 | }; | 1353 | 1358 | } | 1354 | 1359 |
| @@ -1392,6 +1397,7 @@ function from(input) { | 1392 | 1397 | async *[SymbolAsyncIterator]() { | 1393 | 1398 | // Empty - yield nothing | 1394 | 1399 | }, | | 1400 | + [kValidatedSource]: true, | 1395 | 1401 | }; | 1396 | 1402 | } | 1397 | 1403 | if (isUint8Array(input[0])) { | @@ -1409,6 +1415,7 @@ function from(input) { | 1409 | 1415 | } | 1410 | 1416 | } | 1411 | 1417 | }, | | 1418 | + [kValidatedSource]: true, | 1412 | 1419 | }; | 1413 | 1420 | } | 1414 | 1421 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-from-async.js +28Lines changed: 28 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -801,7 +801,35 @@ async function testFromSyncSourceQueuesBehindReturn() { | 801 | 801 | ]); | 802 | 802 | } | 803 | 803 |
| | 804 | +// from() returns its own results unchanged: they already yield normalized | | 805 | +// batches, and normalizing them again would add a layer to every read. | | 806 | +async function testFromReturnsItsOwnResults() { | | 807 | + async function* asyncSource() { | | 808 | + yield 'a'; | | 809 | + yield [new Uint8Array([98])]; | | 810 | + } | | 811 | + | | 812 | + function* syncSource() { | | 813 | + yield 'c'; | | 814 | + yield new Uint8Array([100]); | | 815 | + } | | 816 | + const inputs = [ | | 817 | + [asyncSource(), 'ab'], | | 818 | + [syncSource(), 'cd'], | | 819 | + ['ef', 'ef'], | | 820 | + [[new Uint8Array([103]), new Uint8Array([104])], 'gh'], | | 821 | + [[], ''], | | 822 | + [{ [Symbol.for('Stream.toAsyncStreamable')]() { return 'ij'; } }, 'ij'], | | 823 | + ]; | | 824 | + for (const [input, expected] of inputs) { | | 825 | + const normalized = from(input); | | 826 | + assert.strictEqual(from(normalized), normalized); | | 827 | + assert.strictEqual(await text(from(from(normalized))), expected); | | 828 | + } | | 829 | +} | | 830 | + | 804 | 831 | Promise.all([ | | 832 | + testFromReturnsItsOwnResults(), | 805 | 833 | testFromString(), | 806 | 834 | testFromAsyncGenerator(), | 807 | 835 | testFromAsyncIteratorResultShapes(), | |
|