p2p-media-loader-core
Version:
P2P Media Loader core functionality
145 lines • 4.37 kB
JavaScript
export function getControlledPromise() {
let resolve;
let reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return {
promise,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
resolve: resolve,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
reject: reject,
};
}
export function joinChunks(chunks, totalBytes) {
if (totalBytes === undefined) {
totalBytes = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
}
const buffer = new Uint8Array(totalBytes);
let offset = 0;
for (const chunk of chunks) {
buffer.set(chunk, offset);
offset += chunk.byteLength;
}
return buffer;
}
export function getPercent(numerator, denominator) {
return (numerator / denominator) * 100;
}
export function getRandomItem(items) {
return items[Math.floor(Math.random() * items.length)];
}
export function utf8ToUintArray(utf8String) {
const encoder = new TextEncoder();
const bytes = new Uint8Array(utf8String.length);
encoder.encodeInto(utf8String, bytes);
return bytes;
}
export function hexToUtf8(hexString) {
const bytes = new Uint8Array(hexString.length / 2);
for (let i = 0; i < hexString.length; i += 2) {
bytes[i / 2] = parseInt(hexString.slice(i, i + 2), 16);
}
const decoder = new TextDecoder();
return decoder.decode(bytes);
}
export function* arrayBackwards(arr) {
for (let i = arr.length - 1; i >= 0; i--) {
yield arr[i];
}
}
function isObject(item) {
return !!item && typeof item === "object" && !Array.isArray(item);
}
function isArray(item) {
return Array.isArray(item);
}
export function filterUndefinedProps(obj) {
function filter(obj) {
if (isObject(obj)) {
const result = {};
Object.keys(obj).forEach((key) => {
if (obj[key] !== undefined) {
const value = filter(obj[key]);
if (value !== undefined) {
result[key] = value;
}
}
});
return result;
}
else {
return obj;
}
}
return filter(obj);
}
export function deepCopy(item) {
if (isArray(item)) {
return item.map((element) => deepCopy(element));
}
else if (isObject(item)) {
const copy = {};
for (const key of Object.keys(item)) {
copy[key] = deepCopy(item[key]);
}
return copy;
}
else {
return item;
}
}
export function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
export function overrideConfig(target, updates, defaults = {}) {
if (typeof target !== "object" ||
target === null ||
typeof updates !== "object" ||
updates === null) {
return target;
}
Object.keys(updates).forEach((key) => {
if (key === "__proto__" || key === "constructor" || key === "prototype") {
throw new Error(`Attempt to modify restricted property '${String(key)}'`);
}
const updateValue = updates[key];
const defaultValue = defaults[key];
if (key in target) {
if (updateValue === undefined) {
target[key] =
defaultValue === undefined
? undefined
: defaultValue;
}
else {
target[key] = updateValue;
}
}
});
return target;
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
export function mergeAndFilterConfig(options) {
const { defaultConfig, baseConfig = {}, specificStreamConfig = {} } = options;
const mergedConfig = deepCopy({
...defaultConfig,
...baseConfig,
...specificStreamConfig,
});
const keysOfT = Object.keys(defaultConfig);
const filteredConfig = {};
keysOfT.forEach((key) => {
if (key in mergedConfig) {
filteredConfig[key] = mergedConfig[key];
}
});
return filteredConfig;
}
//# sourceMappingURL=utils.js.map