Overview
This service handles:- Downloading text-based playlists and manifests
- Downloading binary fragment data as ArrayBuffers
- Automatic retry with exponential backoff for transient network errors
- HTTP error detection and propagation
API
TheFetchLoader object exports two methods:
Methods
fetchText
Fetches a resource as text with automatic retry.Parameters
URL of the resource to fetch
Maximum number of fetch attempts before giving up
Returns
Promise<string>
Promise resolving to the response body as text
Behavior
- Uses the Fetch API to retrieve the resource
- Checks
response.okand throwsHttpErrorif status indicates failure - Retries on network errors (but not HTTP errors)
- Applies exponential backoff starting at 100ms, increasing by 15% per retry
Example
fetchArrayBuffer
Fetches a resource as an ArrayBuffer with automatic retry.Parameters
URL of the resource to fetch
Maximum number of fetch attempts before giving up
Returns
Promise<ArrayBuffer>
Promise resolving to the response body as an ArrayBuffer
Behavior
- Uses the Fetch API to retrieve the resource
- Checks
response.okand throwsHttpErrorif status indicates failure - Retries on network errors (but not HTTP errors)
- Applies exponential backoff starting at 100ms, increasing by 15% per retry
Example
Retry mechanism
The retry logic is implemented in the internalfetchWithRetry function:
Retry behavior
First retry waits 100ms after failure
Each subsequent retry increases the delay by 15%
HTTP errors (4xx, 5xx) are not retried and throw immediately
Network failures, timeouts, and other non-HTTP errors trigger retries
Retry timeline example
Forattempts = 5:
- Initial attempt fails (network error)
- Wait 100ms, retry (fails)
- Wait 115ms (100 × 1.15), retry (fails)
- Wait 132ms (115 × 1.15), retry (fails)
- Wait 152ms (132 × 1.15), retry (succeeds)
If all retry attempts are exhausted, the last error is thrown to the caller.
Error handling
HttpError
Custom error class for HTTP status errors.Properties
HTTP status code (e.g., 404, 500)
Always set to
"HttpError"Error message in format
"HTTP {status}"Example
Error propagation
- HTTP errors (4xx, 5xx): Thrown immediately as
HttpError, no retries - Network errors: Retried up to the specified number of attempts
- After all retries fail: The last error is thrown
- Invalid attempts parameter: Throws
"Attempts less then 1"ifattempts < 1
Usage patterns
Fetching playlists
Downloading fragments
Error handling with retries
Performance considerations
- Concurrency: The loader itself does not limit concurrency. Use
Promise.allwith care to avoid overwhelming the server - Retry overhead: With 5 attempts and exponential backoff, total retry time can exceed 500ms
- Memory:
fetchArrayBufferloads the entire response into memory as an ArrayBuffer
For production use, consider implementing concurrency limits using
p-limit or similar libraries when downloading many fragments in parallel.Source location
~/workspace/source/src/background/src/services/fetch-loader.ts:14