check validity of stream before returning

This commit is contained in:
mrjvs 2023-10-26 21:07:11 +02:00
parent 9d204c381f
commit bec7c07881
3 changed files with 25 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import { EmbedOutput, SourcererOutput } from '@/providers/base';
import { ProviderList } from '@/providers/get';
import { ScrapeContext } from '@/utils/context';
import { NotFoundError } from '@/utils/errors';
import { isValidStream } from '@/utils/valid';
export type IndividualSourceRunnerOptions = {
features: FeatureMap;
@ -50,7 +51,7 @@ export async function scrapeInvidualSource(
});
// stream doesn't satisfy the feature flags, so gets removed in output
if (output?.stream && !flagsAllowedInFeatures(ops.features, output.stream.flags)) {
if (output?.stream && (!isValidStream(output.stream) || !flagsAllowedInFeatures(ops.features, output.stream.flags))) {
output.stream = undefined;
}
@ -87,7 +88,9 @@ export async function scrapeIndividualEmbed(
},
});
if (!isValidStream(output.stream)) throw new NotFoundError('stream is incomplete');
if (!flagsAllowedInFeatures(ops.features, output.stream.flags))
throw new NotFoundError("stream doesn't satisfy target feature flags");
return output;
}

View File

@ -8,6 +8,7 @@ import { Stream } from '@/providers/streams';
import { ScrapeContext } from '@/utils/context';
import { NotFoundError } from '@/utils/errors';
import { reorderOnIdList } from '@/utils/list';
import { isValidStream } from '@/utils/valid';
export type RunOutput = {
sourceId: string;
@ -79,6 +80,9 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt
...contextBase,
media: ops.media,
});
if (!isValidStream(output?.stream)) {
throw new NotFoundError('stream is incomplete');
}
if (output?.stream && !flagsAllowedInFeatures(ops.features, output.stream.flags)) {
throw new NotFoundError("stream doesn't satisfy target feature flags");
}

17
src/utils/valid.ts Normal file
View File

@ -0,0 +1,17 @@
import { Stream } from '@/providers/streams';
export function isValidStream(stream: Stream | undefined): boolean {
if (!stream) return false;
if (stream.type === 'hls') {
if (!stream.playlist) return false;
return true;
}
if (stream.type === 'file') {
const validQualities = Object.values(stream.qualities).filter((v) => v.url.length > 0);
if (validQualities.length === 0) return false;
return true;
}
// unknown file type
return false;
}