Data: Blob, FormData, URL, Text Encoding and Streams
There are these standard globals, which behave as in browsers:
Blob,File: immutable bytes with a MIME type.URL.createObjectURL(blob)makes ablob:null/…address thatfetch,XMLHttpRequestand thesrcofCanvasImage,ImageandVideocan load, so three.js'sGLTFLoaderreads the textures embedded in a GLB. As in browsers, the address is valid untilURL.revokeObjectURL()or until the content stops; images and videos already loaded aren't affected by revoking it.FormData: form fields and files, sent as multipart/form-data as a request body;response.formData()parses it too. There are no form elements, sonew FormData(form)throws.URL,URLSearchParams: parsed by the WHATWG URL standard. International domain names are converted to Punycode, but without the full UTS #46 mapping table, so domains with a few special characters may come out differently from browsers.TextEncoder,TextDecoder: UTF-8 and UTF-16 are exactly as in browsers.TextDecoderalso has the legacy encodings of the Encoding standard (GBK, gb18030, Big5, Shift_JIS, EUC-JP, EUC-KR, windows-125x, ISO-8859-x, KOI8 and more), decoded by the system, where a few characters and the replacement of bad bytes may differ slightly from browsers.TextEncoderStreamandTextDecoderStreamwork on streams.FileReader(API 5): reads a Blob as an ArrayBuffer, text, a data URL or a binary string, with its events, for libraries written for it;blob.text(),arrayBuffer()andbytes()are simpler.structuredClone()(API 5): a deep copy of data as in browsers, Maps, Sets, dates, typed arrays, Blobs,ImageDataand cycles included. Functions, promises and the engine's objects (canvases, windows,URL, streams…) throw aDataCloneErrorDOMException. A transferredArrayBufferis detached on macOS 14.4 and later, and still usable before. One the engine has already read (given to WebGL, a canvas,crypto, the file system, audio orsystemAudio.getBands(), aWebSocketor aBroadcastChannel, or anImageData's pixels) stays usable on macOS 14.4 and later too, while the web preview detaches it. Don't use a buffer once it's transferred.MessageChannelandMessagePort(API 5): both ports are in the content (there are no workers), messages are copies made bystructuredCloneand arrive in tasks of their own, as libraries such as React's scheduler expect. To reach other content, useBroadcastChannel, see Messages Between Content.- Streams:
ReadableStream(byte streams and BYOB readers,for await,ReadableStream.from()included),WritableStream,TransformStreamand the two queuing strategies, from web-streams-polyfill. MessageEvent,CloseEventandProgressEventare global too, as areEvent,CustomEventandEventTarget, and the global object is alsoself(three.js and other libraries useself.URL).
js
// Parse newline-delimited JSON while it downloads
const response = await fetch('https://example.com/events.ndjson');
let buffer = '';
for await (const text of response.body.pipeThrough(new TextDecoderStream())) {
buffer += text;
const lines = buffer.split('\n');
buffer = lines.pop();
for (const line of lines) if (line) handle(JSON.parse(line));
}
// Upload a file and a field
const form = new FormData();
form.append('title', 'Screenshot');
form.append('file', new Blob([bytes], { type: 'image/png' }), 'shot.png');
await fetch('https://example.com/upload', { method: 'POST', body: form });