| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8d39d0d commit 5b7d02e
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -434,6 +434,16 @@ async function appendTransformResultAsyncSlow(target, result) { | |||
| 434 | 434 | } | |
| 435 | 435 | } | |
| 436 | 436 | ||
| 437 | + function normalizeTransformResultFast(result) { | ||
| 438 | + if (isUint8ArrayBatch(result)) { | ||
| 439 | + return result.length === 0 ? null : result; | ||
| 440 | + } | ||
| 441 | + if (isUint8Array(result)) return [result]; | ||
| 442 | + if (typeof result === 'string') return [toUint8Array(result)]; | ||
| 443 | + if (isAnyArrayBuffer(result)) return [new Uint8Array(result)]; | ||
| 444 | + if (ArrayBufferIsView(result)) return [arrayBufferViewToUint8Array(result)]; | ||
| 445 | + } | ||
| 446 | + | ||
| 437 | 447 | // ============================================================================= | |
| 438 | 448 | // Sync Pipeline Implementation | |
| 439 | 449 | // ============================================================================= | |
@@ -457,7 +467,17 @@ function* applyFusedStatelessSyncTransforms(source, run) { | |||
| 457 | 467 | current = null; | |
| 458 | 468 | break; | |
| 459 | 469 | } | |
| 460 | - current = result; | ||
| 470 | + if (i === run.length - 1) { | ||
| 471 | + current = result; | ||
| 472 | + continue; | ||
| 473 | + } | ||
| 474 | + current = normalizeTransformResultFast(result); | ||
| 475 | + if (current === undefined) { | ||
| 476 | + const normalized = []; | ||
| 477 | + appendTransformResultSync(normalized, result); | ||
| 478 | + current = normalized.length === 0 ? null : normalized[0]; | ||
| 479 | + } | ||
| 480 | + if (current === null) break; | ||
| 461 | 481 | } | |
| 462 | 482 | if (current === null) continue; | |
| 463 | 483 | // Inline normalization with Uint8Array[] batch as the fast path, | |
@@ -570,21 +590,24 @@ async function* applyFusedStatelessAsyncTransforms(source, run, signal) { | |||
| 570 | 590 | for await (const chunks of source) { | |
| 571 | 591 | let current = chunks; | |
| 572 | 592 | for (let i = 0; i < run.length; i++) { | |
| 573 | - const result = run[i](current, { __proto__: null, signal }); | ||
| 593 | + let result = run[i](current, { __proto__: null, signal }); | ||
| 594 | + if (isPromise(result)) result = await result; | ||
| 574 | 595 | if (result === null) { | |
| 575 | 596 | current = null; | |
| 576 | 597 | break; | |
| 577 | 598 | } | |
| 578 | - if (isPromise(result)) { | ||
| 579 | - const resolved = await result; | ||
| 580 | - if (resolved === null) { | ||
| 581 | - current = null; | ||
| 582 | - break; | ||
| 583 | - } | ||
| 584 | - current = resolved; | ||
| 585 | - } else { | ||
| 599 | + if (i === run.length - 1) { | ||
| 586 | 600 | current = result; | |
| 601 | + continue; | ||
| 602 | + } | ||
| 603 | + current = normalizeTransformResultFast(result); | ||
| 604 | + if (current === undefined) { | ||
| 605 | + const normalized = []; | ||
| 606 | + const pendingResult = appendTransformResultAsync(normalized, result); | ||
| 607 | + if (pendingResult !== undefined) await pendingResult; | ||
| 608 | + current = normalized.length === 0 ? null : normalized[0]; | ||
| 587 | 609 | } | |
| 610 | + if (current === null) break; | ||
| 588 | 611 | } | |
| 589 | 612 | if (current === null) continue; | |
| 590 | 613 | // Normalize the final output | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,6 +59,36 @@ async function testSyncTransformReturnsFloat32Array() { | |||
| 59 | 59 | assert.strictEqual(data.byteLength, 4); | |
| 60 | 60 | } | |
| 61 | 61 | ||
| 62 | + // Consecutive stateless transforms normalize intermediate output (async) | ||
| 63 | + async function testConsecutiveTransformsNormalizeIntermediateOutput() { | ||
| 64 | + const first = (chunks) => { | ||
| 65 | + return chunks === null ? null : new Uint8Array([65]); | ||
| 66 | + }; | ||
| 67 | + let receivedBatch = false; | ||
| 68 | + const second = (chunks) => { | ||
| 69 | + if (chunks !== null) receivedBatch = Array.isArray(chunks); | ||
| 70 | + return chunks; | ||
| 71 | + }; | ||
| 72 | + const data = await bytes(pull(from('x'), first, second)); | ||
| 73 | + assert.ok(receivedBatch); | ||
| 74 | + assert.deepStrictEqual(data, new Uint8Array([65])); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + // Consecutive stateless transforms normalize intermediate output (sync) | ||
| 78 | + async function testConsecutiveSyncTransformsNormalizeIntermediateOutput() { | ||
| 79 | + const first = (chunks) => { | ||
| 80 | + return chunks === null ? null : new Uint8Array([65]); | ||
| 81 | + }; | ||
| 82 | + let receivedBatch = false; | ||
| 83 | + const second = (chunks) => { | ||
| 84 | + if (chunks !== null) receivedBatch = Array.isArray(chunks); | ||
| 85 | + return chunks; | ||
| 86 | + }; | ||
| 87 | + const data = bytesSync(pullSync(fromSync('x'), first, second)); | ||
| 88 | + assert.ok(receivedBatch); | ||
| 89 | + assert.deepStrictEqual(data, new Uint8Array([65])); | ||
| 90 | + } | ||
| 91 | + | ||
| 62 | 92 | // Stateless transform returns a sync generator (iterable) | |
| 63 | 93 | async function testTransformReturnsGenerator() { | |
| 64 | 94 | const tx = (chunks) => { | |
@@ -233,6 +263,8 @@ Promise.all([ | |||
| 233 | 263 | testSyncTransformReturnsArrayBuffer(), | |
| 234 | 264 | testTransformReturnsFloat32Array(), | |
| 235 | 265 | testSyncTransformReturnsFloat32Array(), | |
| 266 | + testConsecutiveTransformsNormalizeIntermediateOutput(), | ||
| 267 | + testConsecutiveSyncTransformsNormalizeIntermediateOutput(), | ||
| 236 | 268 | testTransformReturnsGenerator(), | |
| 237 | 269 | testSyncTransformReturnsGenerator(), | |
| 238 | 270 | testTransformReturnsAsyncGenerator(), | |
| Back | FazBrowse Home | New Git URL |
0 commit comments