[WIP] Add majority of logic for HLS reading support

This commit is contained in:
Vanilagy
2026-01-27 14:45:44 +01:00
parent f620a328e5
commit d1fbea3e19
48 changed files with 5511 additions and 9 deletions
+81
View File
@@ -850,3 +850,84 @@ export const polyfillSymbolDispose = () => {
export const isNumber = (x: unknown) => {
return typeof x === 'number' && !Number.isNaN(x);
};
export const joinPaths = (basePath: string, relativePath: string) => {
// If relativePath is a full URL with protocol, return it as-is
if (relativePath.includes('://')) {
return relativePath;
}
let result: string;
if (relativePath.startsWith('/')) {
const protocolIndex = basePath.indexOf('://');
if (protocolIndex === -1) {
result = relativePath;
} else {
const pathStart = basePath.indexOf('/', protocolIndex + 3);
if (pathStart === -1) {
result = basePath + relativePath;
} else {
result = basePath.slice(0, pathStart) + relativePath;
}
}
} else {
const lastSlash = basePath.lastIndexOf('/');
if (lastSlash === -1) {
result = relativePath;
} else {
result = basePath.slice(0, lastSlash + 1) + relativePath;
}
}
// Normalize ./ and ../
let prefix = '';
const protocolIndex = result.indexOf('://');
if (protocolIndex !== -1) {
const pathStart = result.indexOf('/', protocolIndex + 3);
if (pathStart !== -1) {
prefix = result.slice(0, pathStart);
result = result.slice(pathStart);
}
}
const segments = result.split('/');
const normalized: string[] = [];
for (const segment of segments) {
if (segment === '..') {
normalized.pop();
} else if (segment !== '.') {
normalized.push(segment);
}
}
return prefix + normalized.join('/');
};
export const arrayCount = <T>(array: T[], predicate: (item: T) => boolean) => {
let count = 0;
for (let i = 0; i < array.length; i++) {
if (predicate(array[i]!)) {
count++;
}
}
return count;
};
export const arrayArgmin = <T>(array: T[], getValue: (item: T) => number): number => {
let minIndex = -1;
let minValue = Infinity;
for (let i = 0; i < array.length; i++) {
const value = getValue(array[i]!);
if (value < minValue) {
minValue = value;
minIndex = i;
}
}
return minIndex;
};