UNPKG

@platformos/pos-cli

Version:

Manage your platformOS application

400 lines 14 kB
Object.defineProperty(exports, "__esModule", { value: true }); var is_1 = require("./is"); var string_1 = require("./string"); /** * Requires a module which is protected _against bundler minification. * * @param request The module path to resolve */ function dynamicRequire(mod, request) { // tslint:disable-next-line: no-unsafe-any return mod.require(request); } exports.dynamicRequire = dynamicRequire; /** * Checks whether we're in the Node.js or Browser environment * * @returns Answer to given question */ function isNodeEnv() { // tslint:disable:strict-type-predicates return Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]'; } exports.isNodeEnv = isNodeEnv; var fallbackGlobalObject = {}; /** * Safely get global scope object * * @returns Global scope object */ function getGlobalObject() { return (isNodeEnv() ? global : typeof window !== 'undefined' ? window : typeof self !== 'undefined' ? self : fallbackGlobalObject); } exports.getGlobalObject = getGlobalObject; /** * UUID4 generator * * @returns string Generated UUID4. */ function uuid4() { var global = getGlobalObject(); var crypto = global.crypto || global.msCrypto; if (!(crypto === void 0) && crypto.getRandomValues) { // Use window.crypto API if available var arr = new Uint16Array(8); crypto.getRandomValues(arr); // set 4 in byte 7 // tslint:disable-next-line:no-bitwise arr[3] = (arr[3] & 0xfff) | 0x4000; // set 2 most significant bits of byte 9 to '10' // tslint:disable-next-line:no-bitwise arr[4] = (arr[4] & 0x3fff) | 0x8000; var pad = function (num) { var v = num.toString(16); while (v.length < 4) { v = "0" + v; } return v; }; return (pad(arr[0]) + pad(arr[1]) + pad(arr[2]) + pad(arr[3]) + pad(arr[4]) + pad(arr[5]) + pad(arr[6]) + pad(arr[7])); } // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523 return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) { // tslint:disable-next-line:no-bitwise var r = (Math.random() * 16) | 0; // tslint:disable-next-line:no-bitwise var v = c === 'x' ? r : (r & 0x3) | 0x8; return v.toString(16); }); } exports.uuid4 = uuid4; /** * Parses string form of URL into an object * // borrowed from https://tools.ietf.org/html/rfc3986#appendix-B * // intentionally using regex and not <a/> href parsing trick because React Native and other * // environments where DOM might not be available * @returns parsed URL object */ function parseUrl(url) { if (!url) { return {}; } var match = url.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/); if (!match) { return {}; } // coerce to undefined values to empty string so we don't get 'undefined' var query = match[6] || ''; var fragment = match[8] || ''; return { host: match[4], path: match[5], protocol: match[2], relative: match[5] + query + fragment, }; } exports.parseUrl = parseUrl; /** * Extracts either message or type+value from an event that can be used for user-facing logs * @returns event's description */ function getEventDescription(event) { if (event.message) { return event.message; } if (event.exception && event.exception.values && event.exception.values[0]) { var exception = event.exception.values[0]; if (exception.type && exception.value) { return exception.type + ": " + exception.value; } return exception.type || exception.value || event.event_id || '<unknown>'; } return event.event_id || '<unknown>'; } exports.getEventDescription = getEventDescription; /** JSDoc */ function consoleSandbox(callback) { var global = getGlobalObject(); var levels = ['debug', 'info', 'warn', 'error', 'log', 'assert']; if (!('console' in global)) { return callback(); } var originalConsole = global.console; var wrappedLevels = {}; // Restore all wrapped console methods levels.forEach(function (level) { if (level in global.console && originalConsole[level].__sentry_original__) { wrappedLevels[level] = originalConsole[level]; originalConsole[level] = originalConsole[level].__sentry_original__; } }); // Perform callback manipulations var result = callback(); // Revert restoration to wrapped state Object.keys(wrappedLevels).forEach(function (level) { originalConsole[level] = wrappedLevels[level]; }); return result; } exports.consoleSandbox = consoleSandbox; /** * Adds exception values, type and value to an synthetic Exception. * @param event The event to modify. * @param value Value of the exception. * @param type Type of the exception. * @hidden */ function addExceptionTypeValue(event, value, type) { event.exception = event.exception || {}; event.exception.values = event.exception.values || []; event.exception.values[0] = event.exception.values[0] || {}; event.exception.values[0].value = event.exception.values[0].value || value || ''; event.exception.values[0].type = event.exception.values[0].type || type || 'Error'; } exports.addExceptionTypeValue = addExceptionTypeValue; /** * Adds exception mechanism to a given event. * @param event The event to modify. * @param mechanism Mechanism of the mechanism. * @hidden */ function addExceptionMechanism(event, mechanism) { if (mechanism === void 0) { mechanism = {}; } // TODO: Use real type with `keyof Mechanism` thingy and maybe make it better? try { // @ts-ignore // tslint:disable:no-non-null-assertion event.exception.values[0].mechanism = event.exception.values[0].mechanism || {}; Object.keys(mechanism).forEach(function (key) { // @ts-ignore event.exception.values[0].mechanism[key] = mechanism[key]; }); } catch (_oO) { // no-empty } } exports.addExceptionMechanism = addExceptionMechanism; /** * A safe form of location.href */ function getLocationHref() { try { return document.location.href; } catch (oO) { return ''; } } exports.getLocationHref = getLocationHref; /** * Given a child DOM element, returns a query-selector statement describing that * and its ancestors * e.g. [HTMLElement] => body > div > input#foo.btn[name=baz] * @returns generated DOM path */ function htmlTreeAsString(elem) { // try/catch both: // - accessing event.target (see getsentry/raven-js#838, #768) // - `htmlTreeAsString` because it's complex, and just accessing the DOM incorrectly // - can throw an exception in some circumstances. try { var currentElem = elem; var MAX_TRAVERSE_HEIGHT = 5; var MAX_OUTPUT_LEN = 80; var out = []; var height = 0; var len = 0; var separator = ' > '; var sepLength = separator.length; var nextStr = void 0; while (currentElem && height++ < MAX_TRAVERSE_HEIGHT) { nextStr = _htmlElementAsString(currentElem); // bail out if // - nextStr is the 'html' element // - the length of the string that would be created exceeds MAX_OUTPUT_LEN // (ignore this limit if we are on the first iteration) if (nextStr === 'html' || (height > 1 && len + out.length * sepLength + nextStr.length >= MAX_OUTPUT_LEN)) { break; } out.push(nextStr); len += nextStr.length; currentElem = currentElem.parentNode; } return out.reverse().join(separator); } catch (_oO) { return '<unknown>'; } } exports.htmlTreeAsString = htmlTreeAsString; /** * Returns a simple, query-selector representation of a DOM element * e.g. [HTMLElement] => input#foo.btn[name=baz] * @returns generated DOM path */ function _htmlElementAsString(el) { var elem = el; var out = []; var className; var classes; var key; var attr; var i; if (!elem || !elem.tagName) { return ''; } out.push(elem.tagName.toLowerCase()); if (elem.id) { out.push("#" + elem.id); } className = elem.className; if (className && is_1.isString(className)) { classes = className.split(/\s+/); for (i = 0; i < classes.length; i++) { out.push("." + classes[i]); } } var attrWhitelist = ['type', 'name', 'title', 'alt']; for (i = 0; i < attrWhitelist.length; i++) { key = attrWhitelist[i]; attr = elem.getAttribute(key); if (attr) { out.push("[" + key + "=\"" + attr + "\"]"); } } return out.join(''); } var INITIAL_TIME = Date.now(); var prevNow = 0; var performanceFallback = { now: function () { var now = Date.now() - INITIAL_TIME; if (now < prevNow) { now = prevNow; } prevNow = now; return now; }, timeOrigin: INITIAL_TIME, }; exports.crossPlatformPerformance = (function () { if (isNodeEnv()) { try { var perfHooks = dynamicRequire(module, 'perf_hooks'); return perfHooks.performance; } catch (_) { return performanceFallback; } } if (getGlobalObject().performance) { // Polyfill for performance.timeOrigin. // // While performance.timing.navigationStart is deprecated in favor of performance.timeOrigin, performance.timeOrigin // is not as widely supported. Namely, performance.timeOrigin is undefined in Safari as of writing. // tslint:disable-next-line:strict-type-predicates if (performance.timeOrigin === undefined) { // As of writing, performance.timing is not available in Web Workers in mainstream browsers, so it is not always a // valid fallback. In the absence of a initial time provided by the browser, fallback to INITIAL_TIME. // @ts-ignore // tslint:disable-next-line:deprecation performance.timeOrigin = (performance.timing && performance.timing.navigationStart) || INITIAL_TIME; } } return getGlobalObject().performance || performanceFallback; })(); /** * Returns a timestamp in seconds with milliseconds precision since the UNIX epoch calculated with the monotonic clock. */ function timestampWithMs() { return (exports.crossPlatformPerformance.timeOrigin + exports.crossPlatformPerformance.now()) / 1000; } exports.timestampWithMs = timestampWithMs; // https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string var SEMVER_REGEXP = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/; /** * Parses input into a SemVer interface * @param input string representation of a semver version */ function parseSemver(input) { var match = input.match(SEMVER_REGEXP) || []; var major = parseInt(match[1], 10); var minor = parseInt(match[2], 10); var patch = parseInt(match[3], 10); return { buildmetadata: match[5], major: isNaN(major) ? undefined : major, minor: isNaN(minor) ? undefined : minor, patch: isNaN(patch) ? undefined : patch, prerelease: match[4], }; } exports.parseSemver = parseSemver; var defaultRetryAfter = 60 * 1000; // 60 seconds /** * Extracts Retry-After value from the request header or returns default value * @param now current unix timestamp * @param header string representation of 'Retry-After' header */ function parseRetryAfterHeader(now, header) { if (!header) { return defaultRetryAfter; } var headerDelay = parseInt("" + header, 10); if (!isNaN(headerDelay)) { return headerDelay * 1000; } var headerDate = Date.parse("" + header); if (!isNaN(headerDate)) { return headerDate - now; } return defaultRetryAfter; } exports.parseRetryAfterHeader = parseRetryAfterHeader; var defaultFunctionName = '<anonymous>'; /** * Safely extract function name from itself */ function getFunctionName(fn) { try { if (!fn || typeof fn !== 'function') { return defaultFunctionName; } return fn.name || defaultFunctionName; } catch (e) { // Just accessing custom props in some Selenium environments // can cause a "Permission denied" exception (see raven-js#495). return defaultFunctionName; } } exports.getFunctionName = getFunctionName; /** * This function adds context (pre/post/line) lines to the provided frame * * @param lines string[] containing all lines * @param frame StackFrame that will be mutated * @param linesOfContext number of context lines we want to add pre/post */ function addContextToFrame(lines, frame, linesOfContext) { if (linesOfContext === void 0) { linesOfContext = 5; } var lineno = frame.lineno || 0; var maxLines = lines.length; var sourceLine = Math.max(Math.min(maxLines, lineno - 1), 0); frame.pre_context = lines .slice(Math.max(0, sourceLine - linesOfContext), sourceLine) .map(function (line) { return string_1.snipLine(line, 0); }); frame.context_line = string_1.snipLine(lines[Math.min(maxLines - 1, sourceLine)], frame.colno || 0); frame.post_context = lines .slice(Math.min(sourceLine + 1, maxLines), sourceLine + 1 + linesOfContext) .map(function (line) { return string_1.snipLine(line, 0); }); } exports.addContextToFrame = addContextToFrame; //# sourceMappingURL=misc.js.map