From ffc6d051ecc11b1fbf6889ced1574f8697a11c5c Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Fri, 7 Nov 2025 12:41:35 +0100 Subject: [PATCH] `transform`, `transformAsync`: Return empty Uint8Array by default for non-streams When `process` and `finish` return undefined for non-stream objects, return an empty Uint8Array instead of undefined, to be consistent with the streaming case where `await readToEnd(transform(stream))` returns an empty Uint8Array as well. --- lib/streams.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/streams.js b/lib/streams.js index 5db2f13..5ac7e9a 100644 --- a/lib/streams.js +++ b/lib/streams.js @@ -237,8 +237,7 @@ function transform(input, process = () => undefined, finish = () => undefined, q } const result1 = process(input); const result2 = finish(); - if (result1 !== undefined && result2 !== undefined) return concat([result1, result2]); - return result1 !== undefined ? result1 : result2; + return concat([result1, result2].filter(result => result !== undefined)); } /** @@ -263,8 +262,7 @@ async function transformAsync( } const result1 = await process(input); const result2 = await finish(); - if (result1 !== undefined && result2 !== undefined) return concat([result1, result2]); - return result1 !== undefined ? result1 : result2; + return concat([result1, result2].filter(result => result !== undefined)); } function _transformStream(input, process, finish, queuingStrategy) {