refactor: introduce helper showExternalUrls()

This commit is contained in:
WebStreamr 2025-06-13 10:24:32 +00:00
parent 9b5943208e
commit 15099de798
No known key found for this signature in database
3 changed files with 8 additions and 7 deletions

View file

@ -1,5 +1,5 @@
import { Extractor } from './Extractor';
import { Fetcher } from '../utils';
import { Fetcher, showExternalUrls } from '../utils';
import { Context, CountryCode, UrlResult } from '../types';
export class ExternalUrl extends Extractor {
@ -17,7 +17,7 @@ export class ExternalUrl extends Extractor {
this.fetcher = fetcher;
}
public readonly supports = (ctx: Context, url: URL): boolean => !('excludeExternalUrls' in ctx.config) && null !== url.host.match(/.*/);
public readonly supports = (ctx: Context, url: URL): boolean => showExternalUrls(ctx.config) && null !== url.host.match(/.*/);
protected readonly extractInternal = async (ctx: Context, url: URL, countryCode: CountryCode, title: string | undefined): Promise<UrlResult[]> => {
try {

View file

@ -8,6 +8,7 @@ import { flagFromCountryCode, languageFromCountryCode } from './language';
import { envGetAppName } from './env';
import { Id } from './id';
import { ExtractorRegistry } from '../extractor';
import { showExternalUrls } from './config';
interface ResolveResponse {
streams: Stream[];
@ -95,7 +96,7 @@ export class StreamResolver {
this.logger.info(`Return ${urlResults.length} streams`, ctx);
streams.push(
...urlResults.filter(urlResult => !urlResult.isExternal || this.showExternalUrls(ctx) || urlResult.error)
...urlResults.filter(urlResult => !urlResult.isExternal || showExternalUrls(ctx.config) || urlResult.error)
.map(urlResult => ({
...this.buildUrl(ctx, urlResult),
name: this.buildName(ctx, urlResult),
@ -131,14 +132,12 @@ export class StreamResolver {
return Math.min(...urlResults.map(urlResult => urlResult.ttl as number));
};
private readonly showExternalUrls = (ctx: Context): boolean => !('excludeExternalUrls' in ctx.config);
private readonly buildUrl = (ctx: Context, urlResult: UrlResult): { externalUrl: string } | { url: string } | { ytId: string } => {
if (!urlResult.isExternal) {
return { url: urlResult.url.href };
}
if (this.showExternalUrls(ctx)) {
if (showExternalUrls(ctx.config)) {
return { externalUrl: urlResult.url.href };
}
@ -150,7 +149,7 @@ export class StreamResolver {
name += urlResult.meta.height ? ` ${urlResult.meta.height}P` : ' N/A';
if (urlResult.isExternal && this.showExternalUrls(ctx)) {
if (urlResult.isExternal && showExternalUrls(ctx.config)) {
name += ` ⚠️ external`;
}

View file

@ -3,3 +3,5 @@ import { Config } from '../types';
export const getDefaultConfig = (): Config => {
return { en: 'on' };
};
export const showExternalUrls = (config: Config): boolean => !('excludeExternalUrls' in config);