Skip to content

Commit

Permalink
feat: fetchSetup config support promise (#6714)
Browse files Browse the repository at this point in the history
  • Loading branch information
zce authored Oct 7, 2024
1 parent 4f8ef96 commit 65d6b1f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api-extractor/report/hls.js.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2139,7 +2139,7 @@ export type HlsConfig = {
};
fLoader?: FragmentLoaderConstructor;
pLoader?: PlaylistLoaderConstructor;
fetchSetup?: (context: LoaderContext, initParams: any) => Request;
fetchSetup?: (context: LoaderContext, initParams: any) => Promise<Request> | Request;
xhrSetup?: (xhr: XMLHttpRequest, url: string) => Promise<void> | void;
audioStreamController?: typeof AudioStreamController;
audioTrackController?: typeof AudioTrackController;
Expand Down
5 changes: 4 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,10 @@ export type HlsConfig = {
loader: { new (confg: HlsConfig): Loader<LoaderContext> };
fLoader?: FragmentLoaderConstructor;
pLoader?: PlaylistLoaderConstructor;
fetchSetup?: (context: LoaderContext, initParams: any) => Request;
fetchSetup?: (
context: LoaderContext,
initParams: any,
) => Promise<Request> | Request;
xhrSetup?: (xhr: XMLHttpRequest, url: string) => Promise<void> | void;

// Alt Audio
Expand Down
15 changes: 10 additions & 5 deletions src/utils/fetch-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {
} from '../types/loader';
import { LoadStats } from '../loader/load-stats';
import ChunkCache from '../demux/chunk-cache';
import { isPromise } from '../demux/transmuxer';
import { type HlsConfig } from '../config';

export function fetchSupported() {
if (
Expand All @@ -31,9 +33,9 @@ export function fetchSupported() {
const BYTERANGE = /(\d+)-(\d+)\/(\d+)/;

class FetchLoader implements Loader<LoaderContext> {
private fetchSetup: Function;
private fetchSetup: NonNullable<HlsConfig['fetchSetup']>;
private requestTimeout?: number;
private request: Request | null = null;
private request: Promise<Request> | Request | null = null;
private response: Response | null = null;
private controller: AbortController;
public context: LoaderContext | null = null;
Expand All @@ -42,7 +44,7 @@ class FetchLoader implements Loader<LoaderContext> {
public stats: LoaderStats;
private loader: Response | null = null;

constructor(config /* HlsConfig */) {
constructor(config: HlsConfig) {
this.fetchSetup = config.fetchSetup || getRequest;
this.controller = new self.AbortController();
this.stats = new LoadStats();
Expand Down Expand Up @@ -111,8 +113,11 @@ class FetchLoader implements Loader<LoaderContext> {
callbacks.onTimeout(stats, context, this.response);
}, config.timeout);

self
.fetch(this.request as Request)
const fetchPromise = isPromise(this.request)
? this.request.then(self.fetch)
: self.fetch(this.request);

fetchPromise
.then((response: Response): Promise<string | ArrayBuffer> => {
this.response = this.loader = response;

Expand Down

0 comments on commit 65d6b1f

Please sign in to comment.