LazyBlob

View Source

Summary

A lazy, streaming alternative to Blob.

Important: Since LazyBlob is not a Blob subclass, you cannot pass it directly to APIs that expect a real Blob (like new Response(blob) or formData.append('file', blob)). Instead, use one of:

  • .stream() - Returns a ReadableStream for Response and other streaming APIs
  • .toBlob() - Returns a Promise<Blob> for non-streaming APIs that require a complete Blob (e.g. FormData)

MDN Blob Reference

Signature

class LazyBlob {
  constructor(
    parts: LazyContent | BlobPartLike[],
    options: LazyBlobOptions,
  ): LazyBlob;

  // Accessors
  get [toStringTag](): string;
  get size(): number;
  get type(): string;

  // Methods
  arrayBuffer(): Promise<ArrayBuffer>;
  bytes(): Promise<Uint8Array<ArrayBuffer>>;
  slice(start: number, end: number, contentType: string): LazyBlob;
  stream(): ReadableStream<Uint8Array<ArrayBuffer>>;
  text(): Promise<string>;
  toBlob(): Promise<Blob>;
  toString(): never;
}

Constructor Params

parts

The blob parts or lazy content

options

Options for the blob

Accessors

[toStringTag]

The brand string exposed by Object.prototype.toString.call().

size

The size of the blob in bytes.

MDN Reference

type

The MIME type of the blob.

MDN Reference

Methods

arrayBuffer(): Promise

Returns the blob's contents as an ArrayBuffer.

MDN Reference

bytes(): Promise<Uint8Array>

Returns the blob's contents as a byte array.

MDN Reference

slice(start: number, end: number, contentType: string): LazyBlob

Returns a new LazyBlob that contains the data in the specified range.

MDN Reference

start

The start index (inclusive)

end

The end index (exclusive)

contentType

The content type of the new blob

stream(): ReadableStream<Uint8Array>

Returns a stream that can be used to read the blob's contents.

MDN Reference

text(): Promise

Returns the blob's contents as a string.

MDN Reference

toBlob(): Promise

Converts this LazyBlob to a native Blob.

Warning: This reads the entire content into memory, which defeats the purpose of using a lazy blob for large files. Only use this for non-streaming APIs that require a complete Blob. Use .stream() to get a ReadableStream for Response and other streaming APIs.

toString(): never