mirror of
https://github.com/movie-web/native-app.git
synced 2025-09-13 18:13:25 +00:00
feat: source timeout
This commit is contained in:
@@ -55,6 +55,20 @@ export const providers = makeProviders({
|
|||||||
consistentIpForRequests: true,
|
consistentIpForRequests: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
async function withTimeout<T>(
|
||||||
|
promise: Promise<T>,
|
||||||
|
timeoutMs: number,
|
||||||
|
): Promise<T> {
|
||||||
|
let timeoutHandle: NodeJS.Timeout;
|
||||||
|
const timeoutPromise = new Promise<never>((_, reject) => {
|
||||||
|
timeoutHandle = setTimeout(() => reject(new Error("Timeout")), timeoutMs);
|
||||||
|
});
|
||||||
|
|
||||||
|
return Promise.race([promise, timeoutPromise]).finally(() =>
|
||||||
|
clearTimeout(timeoutHandle),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export async function getVideoStream({
|
export async function getVideoStream({
|
||||||
media,
|
media,
|
||||||
forceVTT,
|
forceVTT,
|
||||||
@@ -64,20 +78,25 @@ export async function getVideoStream({
|
|||||||
forceVTT?: boolean;
|
forceVTT?: boolean;
|
||||||
events?: FullScraperEvents;
|
events?: FullScraperEvents;
|
||||||
}): Promise<RunOutput | null> {
|
}): Promise<RunOutput | null> {
|
||||||
const options: RunnerOptions = {
|
try {
|
||||||
media,
|
const options: RunnerOptions = {
|
||||||
events,
|
media,
|
||||||
};
|
events,
|
||||||
|
};
|
||||||
|
|
||||||
const stream = await providers.runAll(options);
|
const stream = await withTimeout(providers.runAll(options), 10000);
|
||||||
|
|
||||||
if (!stream) return null;
|
if (!stream) return null;
|
||||||
|
|
||||||
if (forceVTT) {
|
if (forceVTT) {
|
||||||
const streamResult = await convertStreamCaptionsToWebVTT(stream.stream);
|
const streamResult = await convertStreamCaptionsToWebVTT(stream.stream);
|
||||||
return { ...stream, stream: streamResult };
|
return { ...stream, stream: streamResult };
|
||||||
|
}
|
||||||
|
|
||||||
|
return stream;
|
||||||
|
} catch (error) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return stream;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getVideoStreamFromSource({
|
export async function getVideoStreamFromSource({
|
||||||
@@ -88,14 +107,21 @@ export async function getVideoStreamFromSource({
|
|||||||
sourceId: string;
|
sourceId: string;
|
||||||
media: ScrapeMedia;
|
media: ScrapeMedia;
|
||||||
events?: SourceRunnerOptions["events"];
|
events?: SourceRunnerOptions["events"];
|
||||||
}): Promise<SourcererOutput> {
|
}): Promise<SourcererOutput | null> {
|
||||||
const sourceResult = await providers.runSourceScraper({
|
try {
|
||||||
id: sourceId,
|
const sourceResult = await withTimeout(
|
||||||
media,
|
providers.runSourceScraper({
|
||||||
events,
|
id: sourceId,
|
||||||
});
|
media,
|
||||||
|
events,
|
||||||
|
}),
|
||||||
|
10000,
|
||||||
|
);
|
||||||
|
|
||||||
return sourceResult;
|
return sourceResult;
|
||||||
|
} catch (error) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getVideoStreamFromEmbed({
|
export async function getVideoStreamFromEmbed({
|
||||||
@@ -106,14 +132,21 @@ export async function getVideoStreamFromEmbed({
|
|||||||
embedId: string;
|
embedId: string;
|
||||||
url: string;
|
url: string;
|
||||||
events?: EmbedRunnerOptions["events"];
|
events?: EmbedRunnerOptions["events"];
|
||||||
}): Promise<EmbedOutput> {
|
}): Promise<EmbedOutput | null> {
|
||||||
const embedResult = await providers.runEmbedScraper({
|
try {
|
||||||
id: embedId,
|
const embedResult = await withTimeout(
|
||||||
url,
|
providers.runEmbedScraper({
|
||||||
events,
|
id: embedId,
|
||||||
});
|
url,
|
||||||
|
events,
|
||||||
|
}),
|
||||||
|
10000,
|
||||||
|
);
|
||||||
|
|
||||||
return embedResult;
|
return embedResult;
|
||||||
|
} catch (error) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findHighestQuality(
|
export function findHighestQuality(
|
||||||
|
Reference in New Issue
Block a user