Baphomet is the dedicated bot for nulloctet matrix
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.7 KiB

  1. let fs = require('fs');
  2. let { logger } = require('./logging');
  3. function getShortestPrefix(radixTree, key, sliceSize) {
  4. let shortKey = key.substring(0, sliceSize);
  5. let keyCount = radixTree.countPrefix(shortKey);
  6. if (keyCount < 1) {
  7. return null;
  8. }
  9. if (key.length === sliceSize && radixTree.getPrefix(shortKey).includes(key)) {
  10. return null;
  11. }
  12. if (keyCount === 1) {
  13. return shortKey;
  14. }
  15. return getShortestPrefix(radixTree, key, sliceSize + 1);
  16. }
  17. function toISODateString(d) {
  18. function pad(n) { return n < 10 ? '0' + n : n }
  19. return d.getUTCFullYear() + '-'
  20. + pad(d.getUTCMonth() + 1) + '-'
  21. + pad(d.getUTCDate()) + 'T'
  22. + pad(d.getUTCHours()) + ':'
  23. + pad(d.getUTCMinutes()) + ':'
  24. + pad(d.getUTCSeconds()) + 'Z'
  25. }
  26. function getBuildInfo() {
  27. let buildInfoPath = process.env.NODE_PATH + '/build.info';
  28. try {
  29. return fs.readFileSync(buildInfoPath, "utf8");
  30. } catch (err) {
  31. if (err.code === 'ENOENT') {
  32. return "UNKNOWN_" + toISODateString(new Date());
  33. } else {
  34. logger.error("Unexpected Error!", err);
  35. }
  36. }
  37. }
  38. function sleep(ms) {
  39. return new Promise(resolve => {
  40. setTimeout(resolve, ms)
  41. })
  42. }
  43. function isString(s) {
  44. return typeof (s) === 'string' || s instanceof String;
  45. }
  46. function isFunction(f) {
  47. return f && {}.toString.call(f) === '[object Function]';
  48. }
  49. /**
  50. * Parse the prototype tree to return all accessible properties till
  51. * reaching a sentinelPrototype.
  52. *
  53. * Optionally provide a filtering function to return only the names that match.
  54. *
  55. * @param {*} initialObj The starting object to derive the from
  56. * @param {*} sentinelPrototype The prototype that represents the end of the line
  57. * @param {*} filterFunc A fioltering function for the return names
  58. */
  59. function getObjectKeysToPrototype(initialObj, sentinelPrototype, filterFunc = (e) => true) {
  60. let prototypeChain = []
  61. var targetPrototype = initialObj;
  62. while (Object.getPrototypeOf(targetPrototype) && targetPrototype !== sentinelPrototype) {
  63. targetPrototype = Object.getPrototypeOf(targetPrototype);
  64. prototypeChain.push(targetPrototype);
  65. }
  66. // console.log("Prototype chain: %s", prototypeChain);
  67. let completePropertyNames = prototypeChain.map((obj) => {
  68. return Object.getOwnPropertyNames(obj);
  69. })
  70. return [Object.getOwnPropertyNames(initialObj)].concat.apply([], completePropertyNames).filter(filterFunc);
  71. }
  72. exports.getShortestPrefix = getShortestPrefix;
  73. exports.toISODateString = toISODateString;
  74. exports.getBuildInfo = getBuildInfo;
  75. exports.sleep = sleep;
  76. exports.isString = isString;
  77. exports.isFunction = isFunction;
  78. exports.getObjectKeysToPrototype = getObjectKeysToPrototype;