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

chore: migrate NetworkDetails from any -> null|XMLHttpRequest|Response; #3828

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/loader/fragment-loader.ts
Expand Up @@ -8,6 +8,7 @@ import {
import type { HlsConfig } from '../config';
import type { BaseSegment, Part } from './fragment';
import type { FragLoadedData } from '../types/events';
import type { NetworkDetails } from '../types/network-details';

const MIN_CHUNK_SIZE = Math.pow(2, 17); // 128kb

Expand Down Expand Up @@ -305,7 +306,7 @@ export interface FragLoadFailResult {
// error description
text: string;
};
networkDetails: any;
networkDetails: NetworkDetails;
}

export type FragmentLoadProgressCallback = (result: FragLoadedData) => void;
17 changes: 9 additions & 8 deletions src/loader/playlist-loader.ts
Expand Up @@ -34,6 +34,7 @@ import type {
ManifestLoadingData,
TrackLoadingData,
} from '../types/events';
import type { NetworkDetails } from '../types/network-details';

function mapContextToLevelType(
context: PlaylistLoaderContext
Expand Down Expand Up @@ -308,7 +309,7 @@ class PlaylistLoader {
response: LoaderResponse,
stats: LoaderStats,
context: PlaylistLoaderContext,
networkDetails: any = null
networkDetails: NetworkDetails = null
): void {
if (context.isSidxRequest) {
this.handleSidxRequest(response, context);
Expand Down Expand Up @@ -346,15 +347,15 @@ class PlaylistLoader {
private loaderror(
response: LoaderResponse,
context: PlaylistLoaderContext,
networkDetails: any = null
networkDetails: NetworkDetails = null
): void {
this.handleNetworkError(context, networkDetails, false, response);
}

private loadtimeout(
stats: LoaderStats,
context: PlaylistLoaderContext,
networkDetails: any = null
networkDetails: NetworkDetails = null
): void {
this.handleNetworkError(context, networkDetails, true);
}
Expand All @@ -363,7 +364,7 @@ class PlaylistLoader {
response: LoaderResponse,
stats: LoaderStats,
context: PlaylistLoaderContext,
networkDetails: any
networkDetails: NetworkDetails
): void {
const hls = this.hls;
const string = response.data as string;
Expand Down Expand Up @@ -458,7 +459,7 @@ class PlaylistLoader {
response: LoaderResponse,
stats: LoaderStats,
context: PlaylistLoaderContext,
networkDetails: any
networkDetails: NetworkDetails
): void {
const hls = this.hls;
const { id, level, type } = context;
Expand Down Expand Up @@ -574,7 +575,7 @@ class PlaylistLoader {
response: LoaderResponse,
context: PlaylistLoaderContext,
reason: string,
networkDetails: any
networkDetails: NetworkDetails
): void {
this.hls.trigger(Events.ERROR, {
type: ErrorTypes.NETWORK_ERROR,
Expand All @@ -590,7 +591,7 @@ class PlaylistLoader {

private handleNetworkError(
context: PlaylistLoaderContext,
networkDetails: any,
networkDetails: NetworkDetails,
timeout = false,
response?: LoaderResponse
): void {
Expand Down Expand Up @@ -658,7 +659,7 @@ class PlaylistLoader {
response: LoaderResponse,
stats: LoaderStats,
context: PlaylistLoaderContext,
networkDetails: any
networkDetails: NetworkDetails
): void {
const {
type,
Expand Down
9 changes: 5 additions & 4 deletions src/types/events.ts
Expand Up @@ -21,6 +21,7 @@ import type { ErrorDetails, ErrorTypes } from '../errors';
import type { MetadataSample, UserdataSample } from './demuxer';
import type { AttrList } from '../utils/attr-list';
import type { HlsListeners } from '../events';
import type { NetworkDetails } from './network-details';

export interface MediaAttachingData {
media: HTMLMediaElement;
Expand Down Expand Up @@ -79,7 +80,7 @@ export interface ManifestLoadedData {
audioTracks: MediaPlaylist[];
captions?: MediaPlaylist[];
levels: LevelParsed[];
networkDetails: any;
networkDetails: NetworkDetails;
sessionData: Record<string, AttrList> | null;
stats: LoaderStats;
subtitles?: MediaPlaylist[];
Expand Down Expand Up @@ -123,7 +124,7 @@ export interface TrackLoadedData {
details: LevelDetails;
id: number;
groupId: string;
networkDetails: any;
networkDetails: NetworkDetails;
stats: LoaderStats;
deliveryDirectives: HlsUrlParameters | null;
}
Expand All @@ -132,7 +133,7 @@ export interface LevelLoadedData {
details: LevelDetails;
id: number;
level: number;
networkDetails: any;
networkDetails: NetworkDetails;
stats: LoaderStats;
deliveryDirectives: HlsUrlParameters | null;
}
Expand Down Expand Up @@ -221,7 +222,7 @@ export interface ErrorData {
level?: number | undefined;
levelRetry?: boolean;
loader?: Loader<LoaderContext>;
networkDetails?: any;
networkDetails?: NetworkDetails;
mimeType?: string;
reason?: string;
response?: LoaderResponse;
Expand Down
11 changes: 6 additions & 5 deletions src/types/loader.ts
Expand Up @@ -2,6 +2,7 @@ import type { Fragment } from '../loader/fragment';
import type { Part } from '../loader/fragment';
import type { LevelDetails } from '../loader/level-details';
import type { HlsUrlParameters } from './level';
import type { NetworkDetails } from './network-details';

export interface LoaderContext {
// target URL
Expand Down Expand Up @@ -71,14 +72,14 @@ export type LoaderOnSuccess<T extends LoaderContext> = (
response: LoaderResponse,
stats: LoaderStats,
context: T,
networkDetails: any
networkDetails: NetworkDetails
) => void;

export type LoaderOnProgress<T extends LoaderContext> = (
stats: LoaderStats,
context: T,
data: string | ArrayBuffer,
networkDetails: any
networkDetails: NetworkDetails
) => void;

export type LoaderOnError<T extends LoaderContext> = (
Expand All @@ -89,19 +90,19 @@ export type LoaderOnError<T extends LoaderContext> = (
text: string;
},
context: T,
networkDetails: any
networkDetails: NetworkDetails
) => void;

export type LoaderOnTimeout<T extends LoaderContext> = (
stats: LoaderStats,
context: T,
networkDetails: any
networkDetails: NetworkDetails
) => void;

export type LoaderOnAbort<T extends LoaderContext> = (
stats: LoaderStats,
context: T,
networkDetails: any
networkDetails: NetworkDetails
) => void;

export interface LoaderCallbacks<T extends LoaderContext> {
Expand Down
4 changes: 4 additions & 0 deletions src/types/network-details.ts
@@ -0,0 +1,4 @@
export type NetworkDetails =
| null // this is a nullable field in several callback interfaces
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer to see NetworkDetails | null where nullable.

| Response // from utils/fetch-loader.ts
| XMLHttpRequest; // from utils/xhr-loader.ts
Comment on lines +2 to +4
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for these changes. This should be helpful for anyone using the default loaders, and for improving internal type safety.

In a situation where someone provides their own loader via config.loader (or pLoader or fLoader) and they want to provide their own struct to networkDetails I wonder how using the project type exports will impact them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good point, perhaps I need to expand the callback interface to be expressed with some generics and have the default argument be NetworkDetails here.

Another option is to keep the current definition and say that a pLoader or fLoader must return one of these 3 types, in case we ever need to manipulate it internally we can have a verified interface versus a generic (that we can never verify like that), and they can cast Response | XMLHttpRequest to their custom struct in their provided callback and outside of the hls.js internals