v149 · JavaScript

Streaming Chunks

Uint8Array.prototype.setFromBase64() can decode base64 directly into a pre-allocated buffer and returns { read, written } — the exact number of source characters consumed and bytes written. This makes it possible to process a large base64 payload chunk-by-chunk, tracking exactly how far you've advanced on both sides of the encoding boundary.

Source base64 (? chars):

#readwrittendecoded bytes
Run the demo to see chunk results.
// Pre-allocate a buffer chunk; capacity = desired chunk byte size
const chunkBuf = new Uint8Array(chunkSize);
let srcOffset = 0;         // how many base64 chars consumed
let totalWritten = 0;      // total bytes decoded so far
const decoded = [];        // collect all decoded bytes

while (srcOffset < base64String.length) {
  const remaining = base64String.slice(srcOffset);

  // setFromBase64 fills chunkBuf and tells you how far it got
  const { read, written } = chunkBuf.setFromBase64(remaining, {
    alphabet: 'base64',
    lastChunkHandling: 'stop-before-partial'
  });

  decoded.push(...chunkBuf.slice(0, written));
  srcOffset    += read;   // advance source pointer by chars consumed
  totalWritten += written;

  if (read === 0) break;  // no progress — shouldn't happen with valid input
}

const result = new Uint8Array(decoded);
// result holds the fully decoded bytes

see also

implementation reference

Need the exact API surface, compatibility boundaries, errors, lifecycle, and source links? Read the matching gendn reference ↗