demo · v140
Decoder Buffer
The motivating use case from chromestatus: a decoder that can't proceed until it has N bytes (frame header, message length prefix). Pre-140 you'd loop calling read(view) and concat short reads. Chrome 140 adds read(view, { min: N }) — the stream blocks until N elements are written. Compare iteration counts side by side.
without min — loop and concat
—
iterations
0
short reads
0
with min — single resolve
—
iterations
0
short reads
0
// pre-140 — loop & concat
const reader = stream.getReader({ mode: "byob" });
let total = 0;
while (total < FRAME_SIZE) {
const { value, done } = await reader.read(new Uint8Array(FRAME_SIZE - total));
if (done) break;
total += value.byteLength;
// ...append to working buffer...
}
// Chrome 140 — one call
const reader = stream.getReader({ mode: "byob" });
const { value } = await reader.read(new Uint8Array(FRAME_SIZE), { min: FRAME_SIZE });
// value.byteLength is guaranteed >= FRAME_SIZE (or stream ended)