Skip to content

A full-featured download manager.

1.4 KB
Raw Blame
import type { ProxyConfig } from '@shared/types/tracker'

interface TrackerRequestInit {
  method?: string
  signal?: AbortSignal
  headers?: Record<string, string>
}

export interface TrackerResponse {
  status?: number
  headers?: { get(name: string): string | null }
  body?: {
    getReader(): {
      read(): Promise<{ done: boolean; value?: Uint8Array }>
      cancel(): Promise<void>
    }
  } | null
  text(): Promise<string>
}

export interface TrackerHttpClient {
  fetch(url: string, init: TrackerRequestInit): Promise<TrackerResponse>
  close(): Promise<void>
}

export async function createTrackerHttpClient(
  proxy?: ProxyConfig
): Promise<TrackerHttpClient> {
  if (!proxy) {
    return {
      fetch: (url, init) => globalThis.fetch(url, init),
      close: async () => undefined,
    }
  }

  // The dispatcher and fetch implementation must come from the same Undici
  // package. Node's global fetch can bundle a different Undici major whose
  // dispatcher handler contract is incompatible with this ProxyAgent.
  const { fetch, ProxyAgent } = await import('undici')
  let uri = proxy.server
  if (proxy.username) {
    const url = new URL(uri)
    url.username = proxy.username
    url.password = proxy.password ?? ''
    uri = url.toString()
  }
  const dispatcher = new ProxyAgent(uri)

  return {
    fetch: (url, init) => fetch(url, { ...init, dispatcher }),
    close: () => dispatcher.close(),
  }
}