gossamer/blob

Immutable binary containers with an associated MIME type — used to assemble payloads for upload, slice large data, or stream bytes through transforms. Construct with from_bytes or from_string.

Types

A file-like object of immutable, raw data. Can be read as text, bytes, or a stream.

See Blob on MDN.

pub type Blob

Values

pub fn array_buffer(
  blob: Blob,
) -> promise.Promise(Result(array_buffer.ArrayBuffer, Nil))

Reads the blob’s contents as an ArrayBuffer. Returns an error if the blob’s source can’t be read.

pub fn bytes(
  blob: Blob,
) -> promise.Promise(Result(BitArray, Nil))

Reads the blob’s contents as a BitArray. Returns an error if the blob’s source can’t be read.

pub fn from_bytes(
  bytes: BitArray,
  content_type content_type: String,
) -> Blob

Creates a Blob wrapping the given bytes. Pass "" for content_type to leave the MIME type unset.

pub fn from_string(
  content: String,
  content_type content_type: String,
) -> Blob

Creates a Blob wrapping the given string. Pass "" for content_type to leave the MIME type unset.

pub fn mime_type(blob: Blob) -> String

The MIME type associated with the blob, or "" if no type was set at construction. Equivalent to JavaScript’s Blob.type.

pub fn new() -> Blob

Creates an empty Blob with no MIME type. Use from_bytes or from_string to construct one with contents.

pub fn revoke_object_url(url: String) -> Nil

Revokes an object URL previously created with to_object_url. Call this to release the reference once the URL is no longer needed. Unparseable URL strings are a silent no-op on every runtime (Deno throws where the spec no-ops; the FFI absorbs the throw). Equivalent to JavaScript’s URL.revokeObjectURL.

pub fn size(blob: Blob) -> Int

The size of the blob’s contents in bytes.

pub fn slice(
  blob: Blob,
  from start: Int,
  to end: Int,
  content_type content_type: String,
) -> Blob

Returns a Blob containing the bytes between start (inclusive) and end (exclusive). Negative offsets count from the end. Pass "" for content_type to leave the MIME type unset (the source blob’s type is never inherited).

pub fn stream(
  blob: Blob,
) -> readable_stream.ReadableStream(BitArray)

Returns a ReadableStream that produces the blob’s bytes in chunks. Suitable for piping into a compression or upload stream.

pub fn text(blob: Blob) -> promise.Promise(Result(String, Nil))

Reads the blob’s contents as a UTF-8 string. Returns an error if the blob’s source can’t be read.

pub fn to_object_url(blob: Blob) -> String

Creates a string containing a URL representing the blob. The URL lifetime is tied to the document or worker that created it; release it with revoke_object_url. Equivalent to JavaScript’s URL.createObjectURL.

Search Document