Skip to main content

Source

The base class representing a resource from which bytes can be read.

Methods

Promise<number>
Returns the total size of the file in bytes. This function is memoized, meaning only the first call will retrieve the size.Throws an error if the source is unsized.
Promise<number | null>
Returns the total size of the file in bytes, or null if the source is unsized. This function is memoized.
((start: number, end: number) => unknown) | null
Called each time data is retrieved from the source. Will be called with the retrieved range (end exclusive).

BufferSource

A source backed by an ArrayBuffer or ArrayBufferView, with the entire file held in memory.

Constructor

ArrayBuffer | SharedArrayBuffer | ArrayBufferView
required
The buffer to use as the source data.

Example


BlobSource

A source backed by a Blob. Since a File is also a Blob, this is the source to use when reading files off the disk in the browser.

Constructor

Blob
required
The Blob to use as the source.
BlobSourceOptions
Optional configuration.

Options

number
default:"8388608"
The maximum number of bytes the cache is allowed to hold in memory. Defaults to 8 MiB.

Example


UrlSource

A source backed by a URL. This is useful for reading data from the network. Requests will be made using an optimized reading and prefetching pattern to minimize request count and latency.

Constructor

string | URL | Request
required
The URL to fetch data from.
UrlSourceOptions
Optional configuration.

Options

RequestInit
The RequestInit used by the Fetch API. Can be used to further control the requests, such as setting custom headers.All fields will work except for signal and headers.Range; these will be overridden by Mediabunny.
(previousAttempts: number, error: unknown, url: string | URL | Request) => number | null
A function that returns the delay (in seconds) before retrying a failed request. If the function returns null, no more retries will be made.By default, uses an exponential backoff algorithm that never gives up unless a CORS error is suspected.
number
default:"67108864"
The maximum number of bytes the cache is allowed to hold in memory. Defaults to 64 MiB.
number
default:"2"
The maximum number of parallel requests to use for fetching. Defaults to 2.
typeof fetch
A WHATWG-compatible fetch function. You can use this field to polyfill the fetch function, add missing features, or use a custom implementation.

Example


FilePathSource

A source backed by a path to a file. Intended for server-side usage in Node, Bun, or Deno.
Make sure to call .dispose() on the corresponding Input when done to explicitly free the internal file handle acquired by this source.

Constructor

string
required
The path to the file to read.
FilePathSourceOptions
Optional configuration.

Options

number
default:"8388608"
The maximum number of bytes the cache is allowed to hold in memory. Defaults to 8 MiB.

Example


StreamSource

A general-purpose, callback-driven source that can get its data from anywhere.

Constructor

StreamSourceOptions
required
Configuration for the stream source.

Options

() => MaybePromise<number>
required
Called when the size of the entire file is requested. Must return or resolve to the size in bytes. This function is guaranteed to be called before read.
(start: number, end: number) => MaybePromise<Uint8Array | ReadableStream<Uint8Array>>
required
Called when data is requested. Must return or resolve to the bytes from the specified byte range, or a stream that yields these bytes.
() => unknown
Called when the Input driven by this source is disposed.
number
default:"8388608"
The maximum number of bytes the cache is allowed to hold in memory. Defaults to 8 MiB.
'none' | 'fileSystem' | 'network'
default:"'none'"
Specifies the prefetch profile that the reader should use with this source.
  • 'none': No prefetching; only the data needed in the moment is requested.
  • 'fileSystem': File system-optimized prefetching with small bidirectional prefetching aligned with page boundaries.
  • 'network': Network-optimized prefetching for high-latency environments; minimizes read calls and aggressively prefetches data when sequential access patterns are detected.

Example


ReadableStreamSource

A source backed by a ReadableStream of Uint8Array, representing an append-only byte stream of unknown length. This is useful for incrementally streaming in input files that are still being constructed, like the output chunks of MediaRecorder.
This source is unsized, meaning calls to .getSize() will throw. You should only use this source with sequential access patterns. This source does not work well with random access patterns unless you increase its max cache size.

Constructor

ReadableStream<Uint8Array>
required
The readable stream to use as the source.
ReadableStreamSourceOptions
Optional configuration.

Options

number
default:"16777216"
The maximum number of bytes the cache is allowed to hold in memory. Defaults to 16 MiB.

Example