UNPKG

@betit/orion-node-sdk

Version:
120 lines 3.69 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const crypto = require("crypto"); const debug = require("debug"); const error_1 = require("../error/error"); /** * @module utils */ function debugLog(section) { return debug(section); } exports.debugLog = debugLog; // @private const UIDCHARS = 'abcdefghijklmnopqrstuvwxyz0123456789'; /** * Generate short unique ids. */ function generateId(len = 7, hex) { const BUF = crypto.randomBytes(len); if (hex) { return BUF.toString('hex'); } else { let str = []; for (let i = 0; i < BUF.length; i++) { str.push(UIDCHARS[BUF[i] % UIDCHARS.length]); } return str.join(''); } } exports.generateId = generateId; function getLineFromError(e) { // An example stack: /* Error at repl:1:1 at ContextifyScript.Script.runInThisContext (vm.js:50:33) at REPLServer.defaultEval (repl.js:242:29) ... */ // This regex gets: // (vm.js:50:33) // 1 vm.js // 2 50 // 3 33 let isError = true; if (!e || typeof e.stack !== 'string') { isError = false; e = new Error(JSON.stringify(e)); } const regex = /\((.*):(\d+):(\d+)\)$/; const match = regex.exec(e.stack.split('\n')[1] || ''); if (!match) { return { isError, stack: undefined, filepath: 'could_not_match ' + e.stack, line: '0', column: '0', }; } return { isError, stack: isError ? undefined : e.stack, filepath: match[1], line: match[2], column: match[3], }; } exports.getLineFromError = getLineFromError; function parseError(err) { // Maximum error stack size const STACK_SIZE = 5; const properties = {}; properties.isError = true; if (!err || !err.message || !err.stack) { properties.isError = false; err = new Error(JSON.stringify(err)); } Object.getOwnPropertyNames(err).forEach(function (key) { properties[key] = err[key]; }, err); // Example: https://nodejs.org/api/errors.html#errors_error_stack if (properties.stack) { const lines = properties.stack.split('\n'); const stack = []; for (let i = 1; i < lines.length && i < STACK_SIZE; i++) { stack.push(lines[i].replace(' at ', '')); } properties.stack = stack.join(''); } return properties; } exports.parseError = parseError; function StringifyError(e) { if (e instanceof error_1.OrionError) { return JSON.stringify({ message: e.message, code: e.code }); } else { return `{"message":"${e.message}"}`; } } exports.StringifyError = StringifyError; function sleep(ms) { return __awaiter(this, void 0, void 0, function* () { return new Promise((res, rej) => { setTimeout(res, ms); }); }); } exports.sleep = sleep; //# sourceMappingURL=index.js.map