uyem
Version:
WebRTC client-server SFU application
125 lines • 5.41 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkDefaultAuth = exports.getRoomDirPath = exports.getBackgroundsDirPath = exports.getVideosDirPath = exports.getVideoPath = exports.parseQueryString = exports.createRandHash = exports.cleanDbUrl = exports.checkTockenDefault = exports.checkSignallingState = exports.getLocale = exports.setLogLevel = exports.log = void 0;
const date_fns_1 = require("date-fns");
const path_1 = __importDefault(require("path"));
const constants_1 = require("./constants");
const interfaces_1 = require("../types/interfaces");
const lang_1 = __importDefault(require("../locales/en/lang"));
const lang_2 = __importDefault(require("../locales/ru/lang"));
const locales = {
en: lang_1.default,
ru: lang_2.default,
};
let logLevel = constants_1.LOG_LEVEL;
// eslint-disable-next-line no-unused-vars
var LogLevel;
(function (LogLevel) {
// eslint-disable-next-line no-unused-vars
LogLevel[LogLevel["log"] = 0] = "log";
// eslint-disable-next-line no-unused-vars
LogLevel[LogLevel["info"] = 1] = "info";
// eslint-disable-next-line no-unused-vars
LogLevel[LogLevel["warn"] = 2] = "warn";
// eslint-disable-next-line no-unused-vars
LogLevel[LogLevel["error"] = 3] = "error";
})(LogLevel || (LogLevel = {}));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const log = (type, text, _data, cons) => {
const Red = '\x1b[31m';
const Reset = '\x1b[0m';
const Bright = '\x1b[1m';
const Yellow = '\x1b[33m';
const Dim = '\x1b[2m';
const Cyan = '\x1b[36m';
const date = constants_1.IS_DEV ? (0, date_fns_1.format)(new Date(), 'hh:mm:ss') : '';
if (cons) {
// eslint-disable-next-line no-console
console.log(type === 'info' ? Cyan : type === 'warn' ? Yellow : type === 'error' ? Red : Reset, '\n');
// eslint-disable-next-line no-console
console[type](constants_1.IS_DEV ? date : '', type, Reset, text, Bright, _data, Reset);
}
else if (LogLevel[type] >= logLevel || Number.isNaN(logLevel)) {
// eslint-disable-next-line no-console
console[type](constants_1.IS_DEV ? date : '', type === 'error' ? Red : type === 'warn' ? Yellow : Bright, type, Reset, text, Dim, _data, Reset, '\n');
}
};
exports.log = log;
const setLogLevel = (_logLevel) => {
if (_logLevel !== undefined) {
logLevel = _logLevel;
}
};
exports.setLogLevel = setLogLevel;
const getLocale = (value) => locales[value] || locales[interfaces_1.LocaleDefault];
exports.getLocale = getLocale;
const checkSignallingState = (signallingState) => ['have-remote-offer', 'have-local-pranswer'].includes(signallingState);
exports.checkSignallingState = checkSignallingState;
const checkTockenDefault = async (token) => {
(0, exports.log)('warn', 'Check token callback not set, use default all yes', { token });
return true;
};
exports.checkTockenDefault = checkTockenDefault;
const cleanDbUrl = (db) => {
const dbUrl = db || constants_1.DATABASE_URL;
let password = dbUrl.match(/:(?!\/).+@/);
const _password = password ? password[0] : '';
password = ':';
new Array(_password.length - 2).fill('•').forEach((item) => {
password += item;
});
password = `${password}@`;
return dbUrl.replace(_password, password);
};
exports.cleanDbUrl = cleanDbUrl;
const createRandHash = (length) => {
const getRandRange = (min, max) => {
const rand = Math.random() * (max - min) + min;
if (rand > 90 && rand < 97) {
return getRandRange(min, max);
}
return rand;
};
const min = 65;
const max = 122;
let hash = '';
for (let i = 0; i < length; i++) {
const randCode = getRandRange(min, max);
hash += String.fromCharCode(randCode);
}
return hash;
};
exports.createRandHash = createRandHash;
const parseQueryString = (query) => {
const arr = query.replace(/\??/, '').split('&');
const res = {};
arr.forEach((item) => {
if (item === '') {
return;
}
const propReg = /^\w+=/;
const prop = item.match(propReg);
const _prop = prop ? prop[0] : null;
if (!_prop) {
return;
}
const propStr = _prop.replace('=', '');
res[propStr] = item.replace(propReg, '');
});
return res;
};
exports.parseQueryString = parseQueryString;
const getVideoPath = ({ cloudPath, roomId, name, }) => path_1.default.resolve(cloudPath, `./${interfaces_1.RECORD_VIDEOS_PATH}`, `./${roomId}`, `./${name}`);
exports.getVideoPath = getVideoPath;
const getVideosDirPath = ({ cloudPath }) => path_1.default.resolve(cloudPath, `./${interfaces_1.RECORD_VIDEOS_PATH}`);
exports.getVideosDirPath = getVideosDirPath;
const getBackgroundsDirPath = ({ cloudPath }) => path_1.default.resolve(cloudPath, `./${interfaces_1.VIDEO_BACKGROUNDS_PATH}`);
exports.getBackgroundsDirPath = getBackgroundsDirPath;
const getRoomDirPath = ({ videosDirPath, roomId, }) => path_1.default.resolve(videosDirPath, `./${roomId}`);
exports.getRoomDirPath = getRoomDirPath;
const checkDefaultAuth = ({ unitId }) => unitId === constants_1.AUTH_UNIT_ID_DEFAULT;
exports.checkDefaultAuth = checkDefaultAuth;
//# sourceMappingURL=lib.js.map