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.

84 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(buildInfoPath) {
  27. try {
  28. return fs.readFileSync(buildInfoPath, "utf8");
  29. } catch (err) {
  30. if (err.code === 'ENOENT') {
  31. return "UNKNOWN_" + toISODateString(new Date());
  32. } else {
  33. logger.error("Unexpected Error!", err);
  34. }
  35. }
  36. }
  37. function sleep(ms) {
  38. return new Promise(resolve => {
  39. setTimeout(resolve, ms)
  40. })
  41. }
  42. function isString(s) {
  43. return typeof (s) === 'string' || s instanceof String;
  44. }
  45. function isFunction(f) {
  46. return f && {}.toString.call(f) === '[object Function]';
  47. }
  48. /**
  49. * Parse the prototype tree to return all accessible properties till
  50. * reaching a sentinelPrototype.
  51. *
  52. * Optionally provide a filtering function to return only the names that match.
  53. *
  54. * @param {*} initialObj The starting object to derive the from
  55. * @param {*} sentinelPrototype The prototype that represents the end of the line
  56. * @param {*} filterFunc A fioltering function for the return names
  57. */
  58. function getObjectKeysToPrototype(initialObj, sentinelPrototype, filterFunc = (e) => true) {
  59. let prototypeChain = []
  60. var targetPrototype = initialObj;
  61. while (Object.getPrototypeOf(targetPrototype) && targetPrototype !== sentinelPrototype) {
  62. targetPrototype = Object.getPrototypeOf(targetPrototype);
  63. prototypeChain.push(targetPrototype);
  64. }
  65. // console.log("Prototype chain: %s", prototypeChain);
  66. let completePropertyNames = prototypeChain.map((obj) => {
  67. return Object.getOwnPropertyNames(obj);
  68. })
  69. return [Object.getOwnPropertyNames(initialObj)].concat.apply([], completePropertyNames).filter(filterFunc);
  70. }
  71. exports.getShortestPrefix = getShortestPrefix;
  72. exports.toISODateString = toISODateString;
  73. exports.getBuildInfo = getBuildInfo;
  74. exports.sleep = sleep;
  75. exports.isString = isString;
  76. exports.isFunction = isFunction;
  77. exports.getObjectKeysToPrototype = getObjectKeysToPrototype;