UNPKG

f5-conx-core

Version:

F5 SDK for JavaScript with Typescript type definitions

168 lines 6.17 kB
/* * Copyright 2020. F5 Networks, Inc. See End User License Agreement ("EULA") for * license terms. Notwithstanding anything to the contrary in the EULA, Licensee * may copy and modify this software product for its internal business purposes. * Further, Licensee may upload, publish and distribute the modified version of * the software product on devcentral.f5.com. */ '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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.simplifyHttpResponse = exports.verifyHash = exports.isArray = exports.isObject = exports.getRandomUUID = exports.isValidJson = exports.wait = void 0; const fs_1 = __importDefault(require("fs")); const crypto_1 = __importDefault(require("crypto")); /** * delays async response of function * https://stackoverflow.com/questions/38956121/how-to-add-delay-to-promise-inside-then * @param ms time to wait * @param value value to return */ function wait(ms, value) { return new Promise((resolve) => setTimeout(resolve, ms, value)); } exports.wait = wait; /** * validates json blob * @param json * @returns parsed json object */ function isValidJson(json) { return __awaiter(this, void 0, void 0, function* () { try { return JSON.parse(json); // return true; } catch (e) { throw e; } }); } exports.isValidJson = isValidJson; /** * builds a short randon uuid - just for some randomness during testing * * @param length * @example * getRandomUUID(8) // returns 8pSJP15R * */ function getRandomUUID(length, options) { // https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript // was using the last part of a uuidv4 string, but that required an external dep to generate the uuid const result = []; const upperCase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; const lowerCase = 'abcdefghijklmnopqrstuvwxyz'; const numbers = '1234567890'; const set = []; if (options === null || options === void 0 ? void 0 : options.simple) { set.push(lowerCase, numbers); } else { set.push(upperCase, lowerCase, numbers); } const chars = set.join(''); for (let i = 0; i < length; i++) { result.push(chars.charAt(Math.floor(Math.random() * chars.length))); } return result.join(''); } exports.getRandomUUID = getRandomUUID; // https://stackoverflow.com/questions/8834126/how-to-efficiently-check-if-variable-is-array-or-object-in-nodejs-v8 function isObject(a) { // the TS v4.0+ spec recommends using the following to detect an object... // value !== null && typeof value === 'object' return (!!a) && (a.constructor === Object); } exports.isObject = isObject; function isArray(a) { return (!!a) && (a.constructor === Array); } exports.isArray = isArray; // /** // * checks if input is object // * // * ***an array is an object!!! *** // * - use Array.isArray(x) => boolean // * @param x // * @returns boolean // */ // export function isObject(x: unknown): boolean { // return (x !== null && typeof x === 'object' ? true : false); // } /** * Verify file against provided hash * * @param file local file location * @param hash expected SHA 256 hash * * @returns true/false based on hash verification result */ function verifyHash(file, extensionHash) { const createHash = crypto_1.default.createHash('sha256'); const input = fs_1.default.readFileSync(file); createHash.update(input); const computedHash = createHash.digest('hex'); if (extensionHash !== computedHash) { return false; } return true; } exports.verifyHash = verifyHash; /** * returns simplified http response object * * ```ts * return { * data: resp.data, * headers: resp.headers, * status: resp.status, * statusText: resp.statusText, * request: { * uuid: resp.config.uuid, * baseURL: resp.config.baseURL, * url: resp.config.url, * method: resp.request.method, * headers: resp.config.headers, * protocol: resp.config.httpsAgent.protocol, * timings: resp.request.timings * } * } * ``` * @param resp orgininal axios response with timing * @returns simplified http response */ function simplifyHttpResponse(resp) { var _a, _b, _c, _d, _e; return __awaiter(this, void 0, void 0, function* () { // const h = JSON.parse(JSON.stringify(resp.headers)); // only return the things we need return { status: resp.status, statusText: resp.statusText, data: resp.data, headers: resp.headers, request: { uuid: (_a = resp.config) === null || _a === void 0 ? void 0 : _a.uuid, baseURL: (_b = resp.config) === null || _b === void 0 ? void 0 : _b.baseURL, url: (_c = resp.config) === null || _c === void 0 ? void 0 : _c.url, method: resp.request.method, headers: resp.request.headers, protocol: (_e = (_d = resp.config) === null || _d === void 0 ? void 0 : _d.httpsAgent) === null || _e === void 0 ? void 0 : _e.protocol, // timings: resp.request.timings } }; }); } exports.simplifyHttpResponse = simplifyHttpResponse; //# sourceMappingURL=misc.js.map