HttpClient

A resilient HTTP Client specifically designed for HLS streaming workloads. Features include:

  • Native fetch implementation (Node 20+)
  • Exponential backoff retry strategy for transient 5xx/429 errors
  • Corporate Proxy support via ProxyAgent
  • Domain-based Proxy bypass (NO_PROXY)
  • Automatic AbortController management for request timeouts

Constructor

new HttpClient(customOptions)

Creates a new HttpClient instance.

Parameters:
NameTypeDescription
customOptions

Configuration object

Properties
NameTypeDescription
timeout

Request timeout in ms (Default: 10000)

retry

Retry strategy settings

proxy

Corporate proxy URL

noProxy

Array of domains to bypass proxy

Example
const client = new HttpClient({
timeout: 5000,
retry: { limit: 3, delay: 1000 },
proxy: 'http://proxy.corp.com:8080',
noProxy: 'localhost,127.0.0.1,.internal.com',
headers: { 'x-custom-header': 'my-custom-header' }
});

Members

noProxy

List of hostnames that should bypass the corporate proxy

primaryOrigin

Initialized request options (headers, etc.)

sensitiveHeaders

List of sensitive headers

Methods

(async) fetchText(url)

Fetches a playlist and validates its HLS structure.

Parameters:
NameTypeDescription
urlstring

The M3U8 playlist URL

Throws:

If the response body is not a valid HLS manifest

Type
InvalidPlayList
Returns:

The raw string content of the playlist

getRequestHeaders(targetUrl)

Get Request headers

Parameters:
NameTypeDescription
targetUrlstring

target url to get the request for

Returns:

request headers

(async) getStream(url)

Fetches a resource (usually a .ts segment) as a stream.

Parameters:
NameTypeDescription
urlstring

The segment URL

Throws:

If the response body is empty

Type
Error
Returns:

A readable stream of the resource

(async) handleRetry(url, attempt)

Calculates exponential backoff and waits before retrying.

Parameters:
NameTypeDescription
urlstring

url to handle retry for

attemptnumber

attempt to calculate backoff

Returns:

Fetch API Response

(async) requestWithRetry(url, attempt)

Core request wrapper with retry logic and timeout management. Handles cleanup of AbortController timers to prevent memory leaks.

Parameters:
NameTypeDefaultDescription
urlstring

url to fetch

attemptnumber0

[attempt=0] - retry attempt

Throws:

If request fails permanently or retries are exhausted.

Type
Error
Returns:

Fetch API Response

setPrimaryOrigin(url)

Sets the primary origin based on the initial playlist URL.

Parameters:
NameTypeDescription
urlstring

url to set primary origin from

shouldBypassProxy(url)

Determines if a given URL should ignore the configured proxy. Logic matches hostnames or suffix patterns (e.g., '.internal.com').

Parameters:
NameTypeDescription
urlstring

domain to bypass

Returns:

Boolean