Skip enqueue of empty chunks in TextDecoderStream

After BOM stripping or when receiving an empty Uint8Array, the
decoded input can be zero-length. Per spec, empty chunks should
produce no output rather than enqueuing an empty string.
This commit is contained in:
Pierre Tachoire
2026-03-02 14:30:39 +01:00
parent 0749f60702
commit c1c0a7d494
2 changed files with 24 additions and 0 deletions

View File

@@ -59,3 +59,24 @@
testing.expectEqual(true, result2.done); testing.expectEqual(true, result2.done);
})(); })();
</script> </script>
<script id=text_decoder_stream_empty_chunk>
(async function() {
const tds = new TextDecoderStream();
const writer = tds.writable.getWriter();
const reader = tds.readable.getReader();
// Write an empty chunk followed by real data
await writer.write(new Uint8Array([]));
await writer.write(new Uint8Array([104, 105]));
await writer.close();
// Empty chunk should be filtered out; first read gets "hi"
const result = await reader.read();
testing.expectEqual(false, result.done);
testing.expectEqual('hi', result.value);
const result2 = await reader.read();
testing.expectEqual(true, result2.done);
})();
</script>

View File

@@ -65,6 +65,9 @@ fn decodeTransform(controller: *TransformStream.DefaultController, chunk: js.Val
input = input[3..]; input = input[3..];
} }
// Per spec, empty chunks produce no output
if (input.len == 0) return;
try controller.enqueue(.{ .string = input }); try controller.enqueue(.{ .string = input });
} }