fix: refresh stale proxy token on video error
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import { state } from "./state";
|
||||
import { safeLocalStorage } from "./storage";
|
||||
import { isRecord, parseModeSources } from "./validate";
|
||||
import { loadVideoSource } from "./video";
|
||||
|
||||
export const streamUrlForMode = (mode: string, quality?: string): string => {
|
||||
const src = state.playback.modeSources[mode];
|
||||
@@ -16,3 +19,30 @@ export const streamUrlForMode = (mode: string, quality?: string): string => {
|
||||
|
||||
return url;
|
||||
};
|
||||
|
||||
export const refreshCurrentModeSource = async (signal?: AbortSignal): Promise<boolean> => {
|
||||
const mode = state.playback.currentMode;
|
||||
const res = await fetch(
|
||||
`/api/watch/episode/${state.episode.malID}/${encodeURIComponent(state.episode.current)}?mode=${encodeURIComponent(mode)}`,
|
||||
{ signal },
|
||||
);
|
||||
if (!res.ok) {
|
||||
throw new Error(`mode source refresh failed with status ${res.status}`);
|
||||
}
|
||||
|
||||
const data: unknown = await res.json();
|
||||
if (!isRecord(data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const refreshedSource = parseModeSources(data.mode_sources)[mode];
|
||||
if (!refreshedSource?.token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
state.playback.modeSources = { ...state.playback.modeSources, [mode]: refreshedSource };
|
||||
|
||||
const preferredQuality = safeLocalStorage.getItem("mal:preferred-quality") || "best";
|
||||
loadVideoSource(streamUrlForMode(mode, preferredQuality), refreshedSource.type);
|
||||
return true;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user