Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Loader.loader and replace Loader.getResponseHeader() with Laoder.getCacheAge() #3707

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions api-extractor/report/hls.js.api.md
Expand Up @@ -1555,13 +1555,10 @@ export interface Loader<T extends LoaderContext> {
context: T;
// (undocumented)
destroy(): void;
// (undocumented)
getResponseHeader(name: string): string | null;
getCacheAge(): number;
// (undocumented)
load(context: LoaderContext, config: LoaderConfiguration, callbacks: LoaderCallbacks<T>): void;
// (undocumented)
loader: any;
// (undocumented)
stats: LoaderStats;
}

Expand Down
36 changes: 34 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/loader/playlist-loader.ts
Expand Up @@ -684,8 +684,7 @@ class PlaylistLoader {
}

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

switch (type) {
Expand Down
7 changes: 5 additions & 2 deletions src/types/loader.ts
Expand Up @@ -120,9 +120,12 @@ export interface Loader<T extends LoaderContext> {
config: LoaderConfiguration,
callbacks: LoaderCallbacks<T>
): void;
getResponseHeader(name: string): string | null;
/**
* Returns the time, in seconds, that this object has been in a proxy cache.
* For HTTP based loaders, this returns the value of the "age" HTTP header.
*/
getCacheAge(): number;
context: T;
loader: any;
stats: LoaderStats;
}

Expand Down
14 changes: 6 additions & 8 deletions src/utils/fetch-loader.ts
Expand Up @@ -37,7 +37,7 @@ class FetchLoader implements Loader<LoaderContext> {
private config: LoaderConfiguration | null = null;
private callbacks: LoaderCallbacks<LoaderContext> | null = null;
public stats: LoaderStats;
public loader: Response | null = null;
private loader: Response | null = null;

constructor(config /* HlsConfig */) {
this.fetchSetup = config.fetchSetup || getRequest;
Expand Down Expand Up @@ -160,15 +160,13 @@ class FetchLoader implements Loader<LoaderContext> {
});
}

getResponseHeader(name: string): string | null {
getCacheAge(): number {
let result: number = 0;
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) : 0;
}
return null;
return result;
}

private loadProgressively(
Expand Down
15 changes: 10 additions & 5 deletions src/utils/xhr-loader.ts
Expand Up @@ -17,7 +17,7 @@ class XhrLoader implements Loader<LoaderContext> {
private callbacks: LoaderCallbacks<LoaderContext> | null = null;
public context!: LoaderContext;

public loader: XMLHttpRequest | null = null;
private loader: XMLHttpRequest | null = null;
public stats: LoaderStats;

constructor(config /* HlsConfig */) {
Expand Down 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 {
let result: number = 0;
if (
this.loader &&
this.loader.getAllResponseHeaders().indexOf('age') >= 0
) {
const ageHeader = this.loader.getResponseHeader('age');
result = ageHeader ? parseFloat(ageHeader) : 0;
}
return null;
return result;
}
}

Expand Down
5 changes: 2 additions & 3 deletions tests/unit/loader/fragment-loader.ts
Expand Up @@ -22,7 +22,6 @@ const expect = chai.expect;

class MockXhr implements Loader<LoaderContext> {
context!: LoaderContext;
loader: any;
stats: LoadStats;
callbacks?: LoaderCallbacks<FragmentLoaderContext>;

Expand All @@ -36,8 +35,8 @@ class MockXhr implements Loader<LoaderContext> {

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

Expand Down