let fs = require('fs'); let { logger } = require('./logging'); function getShortestPrefix(radixTree, key, sliceSize) { let shortKey = key.substring(0, sliceSize); let keyCount = radixTree.countPrefix(shortKey); if (keyCount < 1) { return null; } if (key.length === sliceSize && radixTree.getPrefix(shortKey).includes(key)) { return null; } if (keyCount === 1) { return shortKey; } return getShortestPrefix(radixTree, key, sliceSize + 1); } function toISODateString(d) { function pad(n) { return n < 10 ? '0' + n : n } return d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()) + 'T' + pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()) + ':' + pad(d.getUTCSeconds()) + 'Z' } function getBuildInfo() { let buildInfoPath = process.env.NODE_PATH + '/build.info'; try { return fs.readFileSync(buildInfoPath, "utf8"); } catch (err) { if (err.code === 'ENOENT') { return "UNKNOWN_" + toISODateString(new Date()); } else { logger.error("Unexpected Error!", err); } } } function sleep(ms) { return new Promise(resolve => { setTimeout(resolve, ms) }) } function isString(s) { return typeof (s) === 'string' || s instanceof String; } function isFunction(f) { return f && {}.toString.call(f) === '[object Function]'; } /** * Parse the prototype tree to return all accessible properties till * reaching a sentinelPrototype. * * Optionally provide a filtering function to return only the names that match. * * @param {*} initialObj The starting object to derive the from * @param {*} sentinelPrototype The prototype that represents the end of the line * @param {*} filterFunc A fioltering function for the return names */ function getObjectKeysToPrototype(initialObj, sentinelPrototype, filterFunc = (e) => true) { let prototypeChain = [] var targetPrototype = initialObj; while (Object.getPrototypeOf(targetPrototype) && targetPrototype !== sentinelPrototype) { targetPrototype = Object.getPrototypeOf(targetPrototype); prototypeChain.push(targetPrototype); } // console.log("Prototype chain: %s", prototypeChain); let completePropertyNames = prototypeChain.map((obj) => { return Object.getOwnPropertyNames(obj); }) return [Object.getOwnPropertyNames(initialObj)].concat.apply([], completePropertyNames).filter(filterFunc); } exports.getShortestPrefix = getShortestPrefix; exports.toISODateString = toISODateString; exports.getBuildInfo = getBuildInfo; exports.sleep = sleep; exports.isString = isString; exports.isFunction = isFunction; exports.getObjectKeysToPrototype = getObjectKeysToPrototype;