release(video-player): v0.1.2

This commit is contained in:
2026-03-11 13:47:47 +03:00
parent 028ce21c4c
commit e4e6bc5af4
6 changed files with 118 additions and 51 deletions

View File

@@ -12,37 +12,30 @@ const detectIOS = () => {
const userAgent = navigator.userAgent || "";
return /iPad|iPhone|iPod/.test(userAgent);
};
const createAuthPlaylistLoader = ({ debug, }) => {
const createAuthPlaylistLoader = ({ debug, getToken, refreshToken, }) => {
const BaseLoader = Hls.DefaultConfig.loader;
return class AuthPlaylistLoader extends BaseLoader {
constructor(config) {
super({ ...config, debug: debug ?? false });
}
load(context, config, callbacks) {
const start = async () => {
try {
const token = await resolveVideoPlayerToken();
if (token) {
context.headers = {
...(context.headers ?? {}),
Authorization: `Bearer ${token}`,
};
}
try {
const token = getToken();
if (token) {
context.headers = {
...(context.headers ?? {}),
Authorization: `Bearer ${token}`,
};
}
catch (error) {
if (debug) {
console.warn("[VideoRuntime:HLS] Failed to append auth header to playlist request", error);
}
}
finally {
super.load(context, config, callbacks);
}
};
void start().catch(error => {
}
catch (error) {
if (debug) {
console.error("[VideoRuntime:HLS] Playlist loader start failed", error);
console.warn("[VideoRuntime:HLS] Failed to append auth header to playlist request", error);
}
});
}
// Critical path must stay sync for hls.js loader lifecycle.
super.load(context, config, callbacks);
refreshToken();
}
};
};
@@ -82,6 +75,8 @@ export class VideoPlayerRuntime {
this.currentEngine = null;
this.currentSource = null;
this.vhsAuthTokenRef = null;
this.hlsAuthTokenRef = null;
this.hlsTokenResolvePromise = null;
this.vhsRequestCleanupRef = null;
this.visibilityObserverRef = null;
this.originalPlayRef = null;
@@ -147,6 +142,8 @@ export class VideoPlayerRuntime {
this.vhsRequestCleanupRef?.();
this.vhsRequestCleanupRef = null;
this.vhsAuthTokenRef = null;
this.hlsAuthTokenRef = null;
this.hlsTokenResolvePromise = null;
if (this.playerRef) {
this.playerRef.dispose();
}
@@ -171,6 +168,29 @@ export class VideoPlayerRuntime {
getPlayer() {
return this.playerRef;
}
async ensureHlsAuthToken() {
if (this.hlsTokenResolvePromise !== null) {
await this.hlsTokenResolvePromise;
return;
}
this.hlsTokenResolvePromise = resolveVideoPlayerToken()
.then(token => {
this.hlsAuthTokenRef = token ?? null;
})
.catch(() => {
this.hlsAuthTokenRef = null;
})
.finally(() => {
this.hlsTokenResolvePromise = null;
});
await this.hlsTokenResolvePromise;
}
refreshHlsAuthTokenInBackground() {
if (this.hlsTokenResolvePromise !== null) {
return;
}
void this.ensureHlsAuthToken();
}
emit(event, payload) {
const listeners = this.eventListeners.get(event);
if (!listeners?.size) {
@@ -417,7 +437,11 @@ export class VideoPlayerRuntime {
const preferHqSettings = options.preferHQ
? { abrEwmaDefaultEstimate: 10690560 * 1.2 }
: {};
const playlistLoader = createAuthPlaylistLoader({ debug: options.debug });
const playlistLoader = createAuthPlaylistLoader({
debug: options.debug,
getToken: () => this.hlsAuthTokenRef,
refreshToken: () => this.refreshHlsAuthTokenInBackground(),
});
return {
debug: options.debug,
enableWorker: true,
@@ -456,6 +480,8 @@ export class VideoPlayerRuntime {
await this.loadVideoJsSource();
return;
}
// Resolve async token before starting HLS manifest load.
await this.ensureHlsAuthToken();
const setupHls = () => {
if (this.hlsLoaded) {
return;