diff --git a/src/handler/KinoKiste.ts b/src/handler/KinoKiste.ts index 8688f80..5da2b4a 100644 --- a/src/handler/KinoKiste.ts +++ b/src/handler/KinoKiste.ts @@ -1,6 +1,6 @@ import * as cheerio from 'cheerio'; import { Handler } from './types'; -import { ImdbId, fulfillAllPromises, parseImdbId, Fetcher } from '../utils'; +import { ImdbId, parseImdbId, Fetcher } from '../utils'; import { EmbedExtractors } from '../embed-extractor'; import { Context } from '../types'; @@ -37,7 +37,7 @@ export class KinoKiste implements Handler { const $ = cheerio.load(html); - return fulfillAllPromises( + return Promise.all( $(`[data-num="${imdbId.series}x${imdbId.episode}"]`) .siblings('.mirrors') .children('[data-link]') diff --git a/src/handler/MeineCloud.ts b/src/handler/MeineCloud.ts index e846e1d..49011fb 100644 --- a/src/handler/MeineCloud.ts +++ b/src/handler/MeineCloud.ts @@ -1,6 +1,6 @@ import * as cheerio from 'cheerio'; import { Handler } from './types'; -import { Fetcher, fulfillAllPromises, parseImdbId } from '../utils'; +import { Fetcher, parseImdbId } from '../utils'; import { EmbedExtractors } from '../embed-extractor'; import { Context } from '../types'; @@ -30,7 +30,7 @@ export class MeineCloud implements Handler { const $ = cheerio.load(html); - return fulfillAllPromises( + return Promise.all( $('[data-link!=""]') .map((_i, el) => new URL(($(el).attr('data-link') as string).replace(/^(https:)?\/\//, 'https://'))) .toArray() diff --git a/src/index.ts b/src/index.ts index 58f14a0..c0b0734 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import { flag } from 'country-emoji'; import { landingTemplate } from './landingTemplate'; import { Handler, KinoKiste, MeineCloud } from './handler'; import { Dropload, EmbedExtractors, SuperVideo } from './embed-extractor'; -import { buildManifest, Fetcher, fulfillAllPromises, logInfo } from './utils'; +import { buildManifest, Fetcher, logError, logInfo } from './utils'; import { Config, UrlResult } from './types'; import fs from 'node:fs'; import * as os from 'node:os'; @@ -104,12 +104,16 @@ addon.get('/:config/stream/:type/:id.json', async function (req: Request, res: R return; } - const handlerUrlResults = await handler.handle({ ip: req.ip as string }, id); - logInfo(`${handler.id} returned ${handlerUrlResults.length} urls`); + try { + const handlerUrlResults = await handler.handle({ ip: req.ip as string }, id); + logInfo(`${handler.id} returned ${handlerUrlResults.length} urls`); - urlResults.push(...handlerUrlResults); + urlResults.push(...handlerUrlResults); + } catch (err) { + logError(`${handler.id} error: ` + err); + } }); - await fulfillAllPromises(handlerPromises); + await Promise.all(handlerPromises); urlResults.sort((a, b) => { const heightComparison = (b.height ?? 0) - (a.height ?? 0); diff --git a/src/utils/index.ts b/src/utils/index.ts index 0783fab..fc45ffa 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -3,4 +3,3 @@ export * from './embed'; export * from './imdb'; export * from './log'; export * from './manifest'; -export * from './promise'; diff --git a/src/utils/promise.test.ts b/src/utils/promise.test.ts deleted file mode 100644 index b18d952..0000000 --- a/src/utils/promise.test.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { fulfillAllPromises } from './promise'; - -describe('promise', () => { - test('fulfillAllPromises returns all resolved promise values and logs rejected ones', async () => { - const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined); - - const results = await fulfillAllPromises([ - Promise.resolve('ok'), - Promise.reject('not ok'), - ]); - - expect(results).toStrictEqual(['ok']); - expect(consoleErrorSpy).toHaveBeenCalled(); - expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('Could not fulfill promise: not ok')); - }); -}); diff --git a/src/utils/promise.ts b/src/utils/promise.ts deleted file mode 100644 index 0c20824..0000000 --- a/src/utils/promise.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { logError } from './log'; - -export const fulfillAllPromises = async (promises: Promise[]) => { - const resultValues: T[] = []; - - (await Promise.allSettled(promises)).forEach((result) => { - if (result.status === 'fulfilled') { - resultValues.push(result.value); - return; - } - - logError(`Could not fulfill promise: ${result.reason}`); - }); - - return resultValues; -};