refactor: simplify stream skipping logic

This commit is contained in:
Viren070
2025-07-02 15:39:20 +01:00
parent e8183aa725
commit 70697cbf2f
4 changed files with 8 additions and 24 deletions
+1 -1
View File
@@ -757,7 +757,7 @@ export const AIOStream = StreamSchema.extend({
.optional(),
duration: z.number().optional(),
library: z.boolean().optional(),
id: z.string().min(1),
id: z.string().min(1).optional(),
}),
});
+1 -8
View File
@@ -3,13 +3,6 @@ import { constants, createLogger, FULL_LANGUAGE_MAPPING } from '../utils';
import FileParser from './file';
const logger = createLogger('parser');
export class SkipStreamError extends Error {
constructor(message: string) {
super(message);
this.name = 'SkipStreamError';
}
}
class StreamParser {
private count = 0;
get errorRegexes(): { pattern: RegExp; message: string }[] | undefined {
@@ -59,7 +52,7 @@ class StreamParser {
constructor(protected readonly addon: Addon) {}
parse(stream: Stream): ParsedStream {
parse(stream: Stream): ParsedStream | { skip: true } {
let parsedStream: ParsedStream = {
id: this.getRandomId(),
addon: this.addon,
+3 -4
View File
@@ -10,12 +10,11 @@ import { Preset, baseOptions } from './preset';
import { Env, formatZodError, RESOURCES } from '../utils';
import { StreamParser } from '../parser';
import { createLogger } from '../utils';
import { SkipStreamError } from '../parser/streams';
const logger = createLogger('parser');
class AIOStreamsStreamParser extends StreamParser {
override parse(stream: Stream): ParsedStream {
override parse(stream: Stream): ParsedStream | { skip: true } {
const aioStream = stream as AIOStream;
const parsed = AIOStream.safeParse(aioStream);
if (!parsed.success) {
@@ -24,8 +23,8 @@ class AIOStreamsStreamParser extends StreamParser {
);
throw new Error('Invalid stream');
}
if (aioStream.streamData.id.endsWith('external-download')) {
throw new SkipStreamError('External download stream');
if (aioStream.streamData.id?.endsWith('external-download')) {
return { skip: true };
}
const addonName = this.addon?.name?.trim();
return {
+3 -11
View File
@@ -39,7 +39,6 @@ import {
import { PresetManager } from './presets';
import { StreamParser } from './parser';
import { z } from 'zod';
import { SkipStreamError } from './parser/streams';
const logger = createLogger('wrappers');
// const cache = Cache.getInstance<string, any>('wrappers');
@@ -168,16 +167,9 @@ export class Wrapper {
? PresetManager.fromId(this.addon.presetType).getParser()
: StreamParser;
const parser = new Parser(this.addon);
return streams.flatMap((stream: Stream) => {
try {
return [parser.parse(stream)];
} catch (error: any) {
if (error instanceof SkipStreamError) {
return [];
}
throw error;
}
});
return streams
.flatMap((stream: Stream) => parser.parse(stream))
.filter((stream: any) => !stream.skip);
}
async getCatalog(