demo · v140
Streaming Decoder
When a base64 payload arrives in chunks (an XHR stream, a paste, a WebSocket frame), you can't just decode each chunk in isolation because the boundary may sit mid-quadruplet. The new setFromBase64 exposes read and written counts plus a lastChunkHandling option to surface exactly that partial state.
awaiting run
// Decode an incoming base64 stream chunk by chunk.
const dst = new Uint8Array(1024 * 16);
let written = 0;
let leftover = "";
for await (const chunk of source) {
const piece = leftover + chunk;
const out = dst.subarray(written);
const { read, written: w } = out.setFromBase64(piece, {
lastChunkHandling: "stop-before-partial",
});
leftover = piece.slice(read);
written += w;
}
why this matters
Without read and written you'd have to either buffer the entire base64 string (defeating streaming) or write your own quadruplet boundary detector. The flag stop-before-partial tells the platform to leave the trailing incomplete group alone so you can prepend it to the next chunk. strict rejects payloads with stray whitespace; loose matches the historical atob behaviour.
see also
- Uint8Array to/from base64 and hex — feature index
- TC39 spec