@mcma/core
Version:
Node module with type definitions and helper utils for the EBU MCMA framework
129 lines (128 loc) • 4.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Utils = void 0;
const mcma_exception_1 = require("./mcma-exception");
function isValidUrl(url) {
try {
new URL(url);
return true;
}
catch (error) {
return false;
}
}
function getTypeName(type) {
if (typeof type === "function") {
type = type.name;
}
else if (typeof type === "object") {
type = type.constructor.name;
}
else if (typeof type !== "string") {
throw new mcma_exception_1.McmaException("Invalid type");
}
return type;
}
function toBase64(text) {
// check for browser function for base64
if (typeof btoa !== "undefined") {
return btoa(text);
}
// check for Node.js Buffer class for converting
if (typeof Buffer !== "undefined" && typeof Buffer.from !== "undefined") {
return Buffer.from(text).toString("base64");
}
// not sure what platform we're on - throw an error to indicate this is not supported
throw new mcma_exception_1.McmaException("Unable to convert from plain text to base64 string. Neither the function 'btoa' nor the class 'Buffer' are defined on this platform.");
}
function fromBase64(base64Text) {
// check for browser function for base64
if (typeof atob !== "undefined") {
return atob(base64Text);
}
// check for Node.js Buffer class for converting
if (typeof Buffer !== "undefined" && typeof Buffer.from !== "undefined") {
return Buffer.from(base64Text, "base64").toString();
}
// not sure what platform we're on - throw an error to indicate this is not supported
throw new mcma_exception_1.McmaException("Unable to convert to plain text from base64 string. Neither the function 'atob' nor the class 'Buffer' are defined on this platform.");
}
async function sleep(timeout) {
return new Promise((resolve) => setTimeout(() => resolve(), timeout));
}
const dateFormat = /^(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))$|^(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))$|^(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))$/;
function isValidDateString(value) {
return typeof value === "string" && dateFormat.test(value);
}
function reviver(key, value) {
if (isValidDateString(value)) {
return new Date(value);
}
return value;
}
function ensureValidDateOrUndefined(maybeDate) {
if (maybeDate === undefined || maybeDate === null) {
return undefined;
}
const date = new Date(maybeDate);
if (isNaN(date.getTime())) {
return undefined;
}
return date;
}
function stringify(obj) {
let cache = [];
return JSON.stringify(obj, function (key, value) {
if (typeof value === "object" && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
}, 2);
}
function checkProperty(object, propertyName, expectedType, required) {
const propertyValue = object[propertyName];
const propertyType = typeof propertyValue;
if (propertyValue === undefined || propertyValue === null) {
if (required) {
throw new mcma_exception_1.McmaException("Resource of type '" + object["@type"] + "' requires property '" + propertyName + "' to be defined", null, object);
}
return;
}
if (expectedType === "url") {
if (propertyType !== "string" || !exports.Utils.isValidUrl(propertyValue)) {
throw new mcma_exception_1.McmaException("Resource of type '" + object["@type"] + "' requires property '" + propertyName + "' to have a valid URL", null, object);
}
}
else if (expectedType === "Array") {
if (!Array.isArray(propertyValue)) {
throw new mcma_exception_1.McmaException("Resource of type '" + object["@type"] + "' requires property '" + propertyName + "' to have type Array", null, object);
}
}
else if (expectedType === "object") {
if (propertyType !== "object" || Array.isArray(propertyValue)) {
throw new mcma_exception_1.McmaException("Resource of type '" + object["@type"] + "' requires property '" + propertyName + "' to have type object", null, object);
}
}
else {
if (expectedType !== propertyType) {
throw new mcma_exception_1.McmaException("Resource of type '" + object["@type"] + "' requires property '" + propertyName + "' to have type " + expectedType, null, object);
}
}
}
exports.Utils = {
isValidUrl,
getTypeName,
toBase64,
fromBase64,
sleep,
isValidDateString,
reviver,
ensureValidDateOrUndefined,
stringify,
checkProperty,
};