File API Built-ins
The File API's Blob and File, runtime globals of the CLI host.
Executive Summary
- Blob — immutable bytes with a MIME type, built from strings, buffers, views and other Blobs, read back as text, an
ArrayBufferor bytes - File — a
Blobwith a name and a last-modified time - Cloneable —
structuredCloneclones both, as the HTML Standard serializes them - Not implemented —
ReadableStreamreads (stream(),textStream()),FileReaderand blob URLs
Blob
Implements the File API Blob: new Blob(blobParts?, options?), size, type, slice(start?, end?, contentType?), text(), arrayBuffer() and bytes(). Not implemented: stream() and textStream(), which return a ReadableStream, which the engine does not have; FileReader and blob URLs. structuredClone clones a Blob as a new Blob with a copy of its bytes and its type.
blobParts converts as Web IDL's sequence<BlobPart>: any iterable object, each element a Blob, a BufferSource or a string (USVString, so a lone surrogate becomes U+FFFD before UTF-8 encoding). A view of a SharedArrayBuffer, a resizable ArrayBuffer and a view of one are a TypeError, because BufferSource has neither [AllowShared] nor [AllowResizable]; a SharedArrayBuffer itself matches no member and is a string part ("[object SharedArrayBuffer]"). A buffer's bytes are copied once every part has converted, so a later part's toString that changes an earlier buffer is seen, and a detached buffer adds no bytes. options.endings: "native" turns each CR LF, CR and LF in string parts into LF, on Windows too, where the File API's native line ending is CR LF. type and slice's contentType are lower-cased, or the empty string when they hold a character outside U+0020-U+007E. slice's positions convert as [Clamp] long long (rounded half to even) and count from the end when negative. text() decodes UTF-8 without a leading BOM; the three reads return a new promise, rejected with a TypeError on a receiver that is not a Blob.
A Blob's bytes live on the engine's heap and count against --max-memory: a Blob, slice or read that does not fit is the catchable RangeError, measured before its bytes are copied.
File
Implements the File API File, a Blob with a name and a last-modified time: new File(fileBits, fileName, options?), name and lastModified, and everything Blob.prototype has (File.prototype inherits from it, and File from Blob). fileBits converts as Blob's blobParts does and is required, as is fileName, a USVString kept as given (a / is not replaced). options is a FilePropertyBag: Blob's type and endings, and lastModified, a long long in milliseconds since the epoch that defaults to the current time. slice() on a File returns a Blob. structuredClone clones a File, or an instance of a subclass of it, as a File with a copy of its bytes, its type, name and lastModified.