Add builder for adding custom sources

This commit is contained in:
mrjvs
2023-12-25 01:00:19 +01:00
parent b70d9aaaf7
commit 4a557b8140
46 changed files with 251 additions and 97 deletions

View File

@@ -0,0 +1,64 @@
export const flags = {
// CORS are set to allow any origin
CORS_ALLOWED: 'cors-allowed',
// the stream is locked on IP, so only works if
// request maker is same as player (not compatible with proxies)
IP_LOCKED: 'ip-locked',
} as const;
export type Flags = (typeof flags)[keyof typeof flags];
export const targets = {
// browser with CORS restrictions
BROWSER: 'browser',
// browser, but no CORS restrictions through a browser extension
BROWSER_EXTENSION: 'browser-extension',
// native app, so no restrictions in what can be played
NATIVE: 'native',
// any target, no target restrictions
ANY: 'any',
} as const;
export type Targets = (typeof targets)[keyof typeof targets];
export type FeatureMap = {
requires: Flags[];
disallowed: Flags[];
};
export const targetToFeatures: Record<Targets, FeatureMap> = {
browser: {
requires: [flags.CORS_ALLOWED],
disallowed: [],
},
'browser-extension': {
requires: [],
disallowed: [],
},
native: {
requires: [],
disallowed: [],
},
any: {
requires: [],
disallowed: [],
},
};
export function getTargetFeatures(target: Targets, consistentIpForRequests: boolean): FeatureMap {
const features = targetToFeatures[target];
if (!consistentIpForRequests) features.disallowed.push(flags.IP_LOCKED);
return features;
}
export function flagsAllowedInFeatures(features: FeatureMap, inputFlags: Flags[]): boolean {
const hasAllFlags = features.requires.every((v) => inputFlags.includes(v));
if (!hasAllFlags) return false;
const hasDisallowedFlag = features.disallowed.some((v) => inputFlags.includes(v));
if (hasDisallowedFlag) return false;
return true;
}