Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions lib/internal/streams/iter/pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,16 @@ async function appendTransformResultAsyncSlow(target, result) {
}
}

function normalizeTransformResultFast(result) {
if (isUint8ArrayBatch(result)) {
return result.length === 0 ? null : result;
}
if (isUint8Array(result)) return [result];
if (typeof result === 'string') return [toUint8Array(result)];
if (isAnyArrayBuffer(result)) return [new Uint8Array(result)];
if (ArrayBufferIsView(result)) return [arrayBufferViewToUint8Array(result)];
}

// =============================================================================
// Sync Pipeline Implementation
// =============================================================================
Expand All @@ -457,7 +467,17 @@ function* applyFusedStatelessSyncTransforms(source, run) {
current = null;
break;
}
current = result;
if (i === run.length - 1) {
current = result;
continue;
}
current = normalizeTransformResultFast(result);
if (current === undefined) {
const normalized = [];
appendTransformResultSync(normalized, result);
current = normalized.length === 0 ? null : normalized[0];
}
if (current === null) break;
}
if (current === null) continue;
// Inline normalization with Uint8Array[] batch as the fast path,
Expand Down Expand Up @@ -570,21 +590,24 @@ async function* applyFusedStatelessAsyncTransforms(source, run, signal) {
for await (const chunks of source) {
let current = chunks;
for (let i = 0; i < run.length; i++) {
const result = run[i](current, { __proto__: null, signal });
let result = run[i](current, { __proto__: null, signal });
if (isPromise(result)) result = await result;
if (result === null) {
current = null;
break;
}
if (isPromise(result)) {
const resolved = await result;
if (resolved === null) {
current = null;
break;
}
current = resolved;
} else {
if (i === run.length - 1) {
current = result;
continue;
}
current = normalizeTransformResultFast(result);
if (current === undefined) {
const normalized = [];
const pendingResult = appendTransformResultAsync(normalized, result);
if (pendingResult !== undefined) await pendingResult;
current = normalized.length === 0 ? null : normalized[0];
}
if (current === null) break;
}
if (current === null) continue;
// Normalize the final output
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-stream-iter-transform-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,36 @@ async function testSyncTransformReturnsFloat32Array() {
assert.strictEqual(data.byteLength, 4);
}

// Consecutive stateless transforms normalize intermediate output (async)
async function testConsecutiveTransformsNormalizeIntermediateOutput() {
const first = (chunks) => {
return chunks === null ? null : new Uint8Array([65]);
};
let receivedBatch = false;
const second = (chunks) => {
if (chunks !== null) receivedBatch = Array.isArray(chunks);
return chunks;
};
const data = await bytes(pull(from('x'), first, second));
assert.ok(receivedBatch);
assert.deepStrictEqual(data, new Uint8Array([65]));
}

// Consecutive stateless transforms normalize intermediate output (sync)
async function testConsecutiveSyncTransformsNormalizeIntermediateOutput() {
const first = (chunks) => {
return chunks === null ? null : new Uint8Array([65]);
};
let receivedBatch = false;
const second = (chunks) => {
if (chunks !== null) receivedBatch = Array.isArray(chunks);
return chunks;
};
const data = bytesSync(pullSync(fromSync('x'), first, second));
assert.ok(receivedBatch);
assert.deepStrictEqual(data, new Uint8Array([65]));
}

// Stateless transform returns a sync generator (iterable)
async function testTransformReturnsGenerator() {
const tx = (chunks) => {
Expand Down Expand Up @@ -233,6 +263,8 @@ Promise.all([
testSyncTransformReturnsArrayBuffer(),
testTransformReturnsFloat32Array(),
testSyncTransformReturnsFloat32Array(),
testConsecutiveTransformsNormalizeIntermediateOutput(),
testConsecutiveSyncTransformsNormalizeIntermediateOutput(),
testTransformReturnsGenerator(),
testSyncTransformReturnsGenerator(),
testTransformReturnsAsyncGenerator(),
Expand Down
Loading