Add fetchFn to UrlSourceOptions, allow Request as UrlSource input, pass error message to getRetryDelay, add fallback for Blob.stream()

This commit is contained in:
Vanilagy
2025-09-22 11:10:39 +02:00
parent 63145c9ced
commit 15db099caf
3 changed files with 91 additions and 32 deletions
+22
View File
@@ -492,6 +492,9 @@ type UrlSourceOptions = {
// The maximum number of bytes the cache is allowed to hold
// in memory. Defaults to 8 MiB.
maxCacheSize?: number;
// Used to provide a custom fetch function
fetchFn?: typeof fetch;
};
```
@@ -516,6 +519,25 @@ const source = new UrlSource('https://example.com/bigbuckbunny.mp4', {
Not setting `getRetryDelay` will default to an infinite, capped exponential backoff pattern.
---
Use `fetchFn` to provide a custom fetch function, usually for polyfill reasons. For example, React Native's `fetch` does not support streamable response bodies, a feature that `UrlSource` requires. In this case, you could use [Expo's `fetch` function](https://docs.expo.dev/versions/latest/sdk/expo/#expofetch-api) instead:
```ts
import { UrlSource } from 'mediabunny';
import { fetch } from 'expo/fetch';
const source = new UrlSource('https://example.com/bigbuckbunny.mp4', {
fetchFn: (input, init) => {
if (typeof input !== 'string') {
// Expo requires string URLs
throw new Error('Expected a string URL.');
}
return fetch(input, init);
},
});
```
### `FilePathSource`
This input source can be used to load data directly from a file, given a file path. It requires a server-side environment such as Node, Bun, or Deno.