Skip to content

Commit

Permalink
Merge pull request video-dev#3711 from jwalton/no-loader-headers
Browse files Browse the repository at this point in the history
fix: Replace Loader.getResponseHeader() with Loader.getCacheAge().
  • Loading branch information
robwalch authored Apr 5, 2021
2 parents c9d6056 + ca962e5 commit 629dc61
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 deletions.
3 changes: 1 addition & 2 deletions api-extractor/report/hls.js.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1555,8 +1555,7 @@ export interface Loader<T extends LoaderContext> {
context: T;
// (undocumented)
destroy(): void;
// (undocumented)
getResponseHeader(name: string): string | null;
getCacheAge?: () => number | null;
// (undocumented)
load(context: LoaderContext, config: LoaderConfiguration, callbacks: LoaderCallbacks<T>): void;
// (undocumented)
Expand Down
8 changes: 6 additions & 2 deletions src/loader/playlist-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,12 @@ class PlaylistLoader {
}

if (levelDetails.live) {
const ageHeader = loader.getResponseHeader('age');
levelDetails.ageHeader = ageHeader ? parseFloat(ageHeader) : 0;
if (loader.getCacheAge) {
levelDetails.ageHeader = loader.getCacheAge() || 0;
}
if (!loader.getCacheAge || isNaN(levelDetails.ageHeader)) {
levelDetails.ageHeader = 0;
}
}

switch (type) {
Expand Down
11 changes: 10 additions & 1 deletion src/types/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,16 @@ export interface Loader<T extends LoaderContext> {
config: LoaderConfiguration,
callbacks: LoaderCallbacks<T>
): void;
getResponseHeader(name: string): string | null;
/**
* `getCacheAge()` is called by hls.js to get the duration that a given object
* has been sitting in a cache proxy when playing live. If implemented,
* this should return a value in seconds.
*
* For HTTP based loaders, this should return the contents of the "age" header.
*
* @returns time object being lodaded
*/
getCacheAge?: () => number | null;
context: T;
loader: any;
stats: LoaderStats;
Expand Down
12 changes: 5 additions & 7 deletions src/utils/fetch-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,13 @@ class FetchLoader implements Loader<LoaderContext> {
});
}

getResponseHeader(name: string): string | null {
getCacheAge(): number | null {
let result: number | null = null;
if (this.response) {
try {
return this.response.headers.get(name);
} catch (error) {
/* Could not get header */
}
const ageHeader = this.response.headers.get('age');
result = ageHeader ? parseFloat(ageHeader) : null;
}
return null;
return result;
}

private loadProgressively(
Expand Down
13 changes: 9 additions & 4 deletions src/utils/xhr-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,16 @@ class XhrLoader implements Loader<LoaderContext> {
}
}

getResponseHeader(name: string): string | null {
if (this.loader && this.loader.getAllResponseHeaders().indexOf(name) >= 0) {
return this.loader.getResponseHeader(name);
getCacheAge(): number | null {
let result: number | null = null;
if (
this.loader &&
this.loader.getAllResponseHeaders().indexOf('age') >= 0
) {
const ageHeader = this.loader.getResponseHeader('age');
result = ageHeader ? parseFloat(ageHeader) : null;
}
return null;
return result;
}
}

Expand Down
3 changes: 0 additions & 3 deletions tests/unit/loader/fragment-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ class MockXhr implements Loader<LoaderContext> {

abort() {}
destroy(): void {}
getResponseHeader(name: string): string | null {
return null;
}
}

describe('FragmentLoader tests', function () {
Expand Down

0 comments on commit 629dc61

Please sign in to comment.