mirror of
https://github.com/nodejs/node.git
synced 2026-11-30 11:55:44 -05:00
stream: skip null output from stateful transforms
Treat null yielded by sync and async stateful transforms as no output, consistent with transform normalization semantics. Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> PR-URL: https://github.com/nodejs/node/pull/64462 Fixes: https://github.com/nodejs/node/issues/64461 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
This commit is contained in:
1 parent
707f6eacfc
commit
136d4e7628
2 files changed
+26
No files matched your search
@@ -504,6 +504,7 @@ function* withFlushSync(source) {
|
||||
function* applyStatefulSyncTransform(source, transform) {
|
||||
const output = transform(withFlushSync(source));
|
||||
for (const item of output) {
|
||||
if (item === null) continue;
|
||||
const batch = [];
|
||||
for (const chunk of flattenTransformYieldSync(item)) {
|
||||
ArrayPrototypePush(batch, chunk);
|
||||
@@ -643,6 +644,7 @@ async function* withFlushAsync(source) {
|
||||
async function* applyStatefulAsyncTransform(source, transform, options) {
|
||||
const output = transform(withFlushAsync(source), options);
|
||||
for await (const item of output) {
|
||||
if (item === null) continue;
|
||||
// Fast path: item is already a Uint8Array[] batch (e.g. compression transforms)
|
||||
if (isUint8ArrayBatch(item)) {
|
||||
if (item.length > 0) {
|
||||
|
||||
@@ -142,6 +142,28 @@ async function testStatefulSyncTransformYieldsString() {
|
||||
assert.deepStrictEqual(data, new TextEncoder().encode('world'));
|
||||
}
|
||||
|
||||
// Stateful async transform yields null (no output)
|
||||
async function testStatefulTransformYieldsNull() {
|
||||
const tx = {
|
||||
async *transform() {
|
||||
yield null;
|
||||
},
|
||||
};
|
||||
const data = await bytes(pull(from('x'), tx));
|
||||
assert.deepStrictEqual(data, new Uint8Array());
|
||||
}
|
||||
|
||||
// Stateful sync transform yields null (no output)
|
||||
async function testStatefulSyncTransformYieldsNull() {
|
||||
const tx = {
|
||||
*transform() {
|
||||
yield null;
|
||||
},
|
||||
};
|
||||
const data = bytesSync(pullSync(fromSync('x'), tx));
|
||||
assert.deepStrictEqual(data, new Uint8Array());
|
||||
}
|
||||
|
||||
// Flush returns single Uint8Array (not batch)
|
||||
async function testFlushReturnsSingleUint8Array() {
|
||||
const tx = (chunks) => {
|
||||
@@ -217,6 +239,8 @@ Promise.all([
|
||||
testStatefulTransformYieldsString(),
|
||||
testStatefulTransformYieldsArrayBuffer(),
|
||||
testStatefulSyncTransformYieldsString(),
|
||||
testStatefulTransformYieldsNull(),
|
||||
testStatefulSyncTransformYieldsNull(),
|
||||
testFlushReturnsSingleUint8Array(),
|
||||
testFlushReturnsString(),
|
||||
testSyncFlushReturnsSingleUint8Array(),
|
||||
|
||||
Reference in new issue
Block a user