Visit homepage

Blob API

  • Planted:

TL;DR: A Blob in JavaScript is read-only raw data that you can read, store, or send over a network.

A Blob object has a size property (in bytes) and a (MIME) type property. It represents the underlying data but doesn’t expose it directly. You can extract the raw bytes using blob.arrayBuffer(), for example.

blob.ts

const blob = new Blob(["tofu"], { type: "text/plain" });
console.log(blob); // { size: 4, type: "text/plain "}
const buffer = await blob.arrayBuffer();
const bytes = new Uint8Array(buffer);
console.log(bytes); // Uint8Array [116, 111, 102, 117]

Here, the bytes 116, 111, 102, and 117 represent the ASCII codepoints for the letters ‘t’, ‘o’, ‘f’, and ‘u’.

I’m adding a Blob polyfill to the Membrane JS runtime (which is a QuickJS fork with persistent state and a browser API layer). I was initially adding a FormData polyfill, which uses Blob. The File API also uses Blob.

Reply