GeezerVison is showing github.com — stream: make pipeTo source normalization independent of Writer · nodejs/node@04406b8
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: lib/internal/streams/iter/pull.js +2-51Lines changed: 2 additions & 51 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 |
| 9 | 9 | const { | 10 | 10 | ArrayBufferIsView, | 11 | | - ArrayFromAsync, | 12 | | - ArrayIsArray, | 13 | 11 | ArrayPrototypePush, | 14 | 12 | ArrayPrototypeSlice, | 15 | 13 | FunctionPrototypeCall, | @@ -46,9 +44,7 @@ const { | 46 | 44 | fromSync, | 47 | 45 | isSyncIterable, | 48 | 46 | isAsyncIterable, | 49 | | - isPrimitiveChunk, | 50 | 47 | isUint8ArrayBatch, | 51 | | - normalizeAsyncValue, | 52 | 48 | } = require('internal/streams/iter/from'); | 53 | 49 |
| 54 | 50 | const { | @@ -65,10 +61,7 @@ const { | 65 | 61 | } = require('internal/streams/iter/utils'); | 66 | 62 |
| 67 | 63 | const { | 68 | | - kValidatedSource, | 69 | 64 | kValidatedTransform, | 70 | | - toAsyncStreamable, | 71 | | - toStreamable, | 72 | 65 | } = require('internal/streams/iter/types'); | 73 | 66 |
| 74 | 67 | // ============================================================================= | @@ -131,22 +124,6 @@ function parsePipeToArgs(args, requiredMethod) { | 131 | 124 | }; | 132 | 125 | } | 133 | 126 |
| 134 | | -function canUseSyncIterablePipeToFastPath(source, transforms, signal) { | 135 | | - if (signal !== undefined || | 136 | | - transforms.length !== 0 || | 137 | | - isPrimitiveChunk(source) || | 138 | | - ArrayIsArray(source) || | 139 | | - source?.[kValidatedSource] || | 140 | | - !isSyncIterable(source) || | 141 | | - isAsyncIterable(source)) { | 142 | | - return false; | 143 | | - } | 144 | | - | 145 | | - // Preserve from()'s top-level protocol precedence for custom iterables. | 146 | | - return typeof source[toAsyncStreamable] !== 'function' && | 147 | | - typeof source[toStreamable] !== 'function'; | 148 | | -} | 149 | | - | 150 | 127 | // ============================================================================= | 151 | 128 | // Transform Output Flattening | 152 | 129 | // ============================================================================= | @@ -1068,9 +1045,7 @@ async function pipeTo(source, ...args) { | 1068 | 1045 | } | 1069 | 1046 |
| 1070 | 1047 | const hasWriteSync = typeof writer.writeSync === 'function'; | 1071 | | - const useSyncIterableFastPath = | 1072 | | - hasWriteSync && canUseSyncIterablePipeToFastPath(source, transforms, signal); | 1073 | | - const normalized = useSyncIterableFastPath ? undefined : from(source); | | 1048 | + const normalized = from(source); | 1074 | 1049 |
| 1075 | 1050 | let totalBytes = 0; | 1076 | 1051 | const hasWritev = typeof writer.writev === 'function'; | @@ -1141,31 +1116,7 @@ async function pipeTo(source, ...args) { | 1141 | 1116 | } | 1142 | 1117 |
| 1143 | 1118 | try { | 1144 | | - if (useSyncIterableFastPath) { | 1145 | | - // Avoid from()'s async sync-iterable batching path. This keeps writes | 1146 | | - // incremental for synchronous sources while preserving async | 1147 | | - // normalization for non-primitive yielded values. | 1148 | | - for (const value of source) { | 1149 | | - if (isUint8ArrayBatch(value)) { | 1150 | | - if (value.length > 0) { | 1151 | | - const p = writeBatch(value); | 1152 | | - if (p) await p; | 1153 | | - } | 1154 | | - continue; | 1155 | | - } | 1156 | | - if (isUint8Array(value)) { | 1157 | | - const p = writeBatch([value]); | 1158 | | - if (p) await p; | 1159 | | - continue; | 1160 | | - } | 1161 | | - | 1162 | | - const batch = await ArrayFromAsync(normalizeAsyncValue(value)); | 1163 | | - if (batch.length > 0) { | 1164 | | - const p = writeBatch(batch); | 1165 | | - if (p) await p; | 1166 | | - } | 1167 | | - } | 1168 | | - } else if (transforms.length === 0) { | | 1119 | + if (transforms.length === 0) { | 1169 | 1120 | // Fast path: no transforms - iterate normalized source directly | 1170 | 1121 | if (signal) { | 1171 | 1122 | for await (const batch of yieldAbortable(normalized, signal)) { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-iter-pipeto.js +32-7Lines changed: 32 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -247,7 +247,7 @@ async function testPipeToSyncMinimalWriter() { | 247 | 247 | assert.strictEqual(chunks.length > 0, true); | 248 | 248 | } | 249 | 249 |
| 250 | | -async function testPipeToSyncIterableFastPathWritesIncrementally() { | | 250 | +async function testPipeToSyncIterableUsesFromBatching() { | 251 | 251 | let pulled = 0; | 252 | 252 | let firstWritePulled = 0; | 253 | 253 | const chunks = []; | @@ -270,15 +270,39 @@ async function testPipeToSyncIterableFastPathWritesIncrementally() { | 270 | 270 |
| 271 | 271 | const totalBytes = await pipeTo(source(), writer); | 272 | 272 | assert.strictEqual(totalBytes, 3); | 273 | | - assert.strictEqual(firstWritePulled, 1); | | 273 | + assert.strictEqual(firstWritePulled, 3); | 274 | 274 | assert.deepStrictEqual(chunks, [ | 275 | 275 | new Uint8Array([0x61]), | 276 | 276 | new Uint8Array([0x62]), | 277 | 277 | new Uint8Array([0x63]), | 278 | 278 | ]); | 279 | 279 | } | 280 | 280 |
| 281 | | -async function testPipeToSyncIterableFastPathWriteFallback() { | | 281 | +async function testPipeToSourceNormalizationIndependentOfWriter() { | | 282 | + function source() { | | 283 | + return { | | 284 | + *[Symbol.iterator]() { | | 285 | + yield { | | 286 | + async *[Symbol.asyncIterator]() { | | 287 | + yield 'nested'; | | 288 | + }, | | 289 | + }; | | 290 | + }, | | 291 | + }; | | 292 | + } | | 293 | + | | 294 | + for (const hasWriteSync of [false, true]) { | | 295 | + const writer = { write: common.mustNotCall() }; | | 296 | + if (hasWriteSync) writer.writeSync = common.mustNotCall(); | | 297 | + | | 298 | + await assert.rejects( | | 299 | + pipeTo(source(), writer, { preventClose: true, preventFail: true }), | | 300 | + { code: 'ERR_INVALID_ARG_TYPE' }, | | 301 | + ); | | 302 | + } | | 303 | +} | | 304 | + | | 305 | +async function testPipeToSyncIterableWriteFallback() { | 282 | 306 | const asyncWrites = []; | 283 | 307 | const writer = { | 284 | 308 | writeSync(chunk) { | @@ -299,7 +323,7 @@ async function testPipeToSyncIterableFastPathWriteFallback() { | 299 | 323 | assert.deepStrictEqual(asyncWrites, [new Uint8Array([0x62])]); | 300 | 324 | } | 301 | 325 |
| 302 | | -async function testPipeToSyncIterableFastPathAsyncValue() { | | 326 | +async function testPipeToSyncIterableAsyncValue() { | 303 | 327 | const chunks = []; | 304 | 328 | const writer = { | 305 | 329 | write: common.mustNotCall(), | @@ -337,7 +361,8 @@ Promise.all([ | 337 | 361 | testPipeToSyncPreventClose(), | 338 | 362 | testPipeToMinimalWriter(), | 339 | 363 | testPipeToSyncMinimalWriter(), | 340 | | - testPipeToSyncIterableFastPathWritesIncrementally(), | 341 | | - testPipeToSyncIterableFastPathWriteFallback(), | 342 | | - testPipeToSyncIterableFastPathAsyncValue(), | | 364 | + testPipeToSyncIterableUsesFromBatching(), | | 365 | + testPipeToSyncIterableWriteFallback(), | | 366 | + testPipeToSyncIterableAsyncValue(), | | 367 | + testPipeToSourceNormalizationIndependentOfWriter(), | 343 | 368 | ]).then(common.mustCall()); |
|