Util.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.isExpired = exports.toJson = exports.toGetValue = exports.getValueP = exports.hash = exports.filePathsP = exports.readFileSync = exports.isFileSync = exports.toStringArray = exports.compact = exports.ensureString = exports.toAbsolutePath = exports.isString = exports.isNothing = void 0;
  13. const libs_1 = require("./libs");
  14. const isNothing = (value) => libs_1.R.isNil(value) || libs_1.R.isEmpty(value);
  15. exports.isNothing = isNothing;
  16. exports.isString = libs_1.R.is(String);
  17. const toAbsolutePath = (path) => {
  18. return path.startsWith('.') ? libs_1.fsPath.resolve(path) : path;
  19. };
  20. exports.toAbsolutePath = toAbsolutePath;
  21. const ensureString = (defaultValue, text) => {
  22. return typeof text === 'string' ? text : defaultValue;
  23. };
  24. exports.ensureString = ensureString;
  25. const compact = (input) => {
  26. const flat = [].concat(...input);
  27. return flat.filter((value) => !libs_1.R.isNil(value));
  28. };
  29. exports.compact = compact;
  30. exports.toStringArray = libs_1.R.pipe(exports.compact, libs_1.R.map(libs_1.R.toString));
  31. const isFileSync = (path) => {
  32. return libs_1.fs.existsSync(path) ? libs_1.fs.lstatSync(path).isFile() : false;
  33. };
  34. exports.isFileSync = isFileSync;
  35. const readFileSync = (path) => {
  36. return libs_1.fs.existsSync(path) ? libs_1.fs.readFileSync(path).toString() : undefined;
  37. };
  38. exports.readFileSync = readFileSync;
  39. const filePathsP = (basePath, ns) => __awaiter(void 0, void 0, void 0, function* () {
  40. if (!(yield libs_1.fs.pathExists(basePath)))
  41. return [];
  42. return (yield libs_1.fs.readdir(basePath))
  43. .filter(Boolean)
  44. .filter((name) => (ns ? name.startsWith(ns) : true))
  45. .filter((name) => (!ns ? !name.includes('-') : true))
  46. .map((name) => `${basePath}/${name}`);
  47. });
  48. exports.filePathsP = filePathsP;
  49. const hash = (...values) => {
  50. if (libs_1.R.pipe(exports.compact, libs_1.R.isEmpty)(values))
  51. return undefined;
  52. const resultHash = libs_1.crypto.createHash('sha1');
  53. const addValue = (value) => resultHash.update(value);
  54. const addValues = libs_1.R.forEach(addValue);
  55. libs_1.R.pipe(exports.toStringArray, addValues)(values);
  56. return resultHash.digest('hex');
  57. };
  58. exports.hash = hash;
  59. function getValueP(path, defaultValue) {
  60. return __awaiter(this, void 0, void 0, function* () {
  61. const exists = yield libs_1.fs.pathExists(path);
  62. if (!exists)
  63. return defaultValue;
  64. try {
  65. return (0, exports.toGetValue)(yield libs_1.fs.readJson(path));
  66. }
  67. catch (error) {
  68. if (error.code === 'ENOENT')
  69. return defaultValue;
  70. if (error.message === 'Cache item has expired.') {
  71. libs_1.fs.removeSync(path);
  72. return defaultValue;
  73. }
  74. throw new Error(`Failed to read cache value at: ${path}. ${error.message}`);
  75. }
  76. });
  77. }
  78. exports.getValueP = getValueP;
  79. const toGetValue = (data) => {
  80. if ((0, exports.isExpired)(data))
  81. throw new Error(`Cache item has expired.`);
  82. if (data.type === 'Date')
  83. return new Date(data.value);
  84. return data.value;
  85. };
  86. exports.toGetValue = toGetValue;
  87. const toJson = (value, ttl) => JSON.stringify({ value, type: libs_1.R.type(value), created: new Date(), ttl });
  88. exports.toJson = toJson;
  89. const isExpired = (data) => {
  90. const timeElapsed = (new Date().getTime() - new Date(data.created).getTime()) / 1000;
  91. return timeElapsed > data.ttl && data.ttl > 0;
  92. };
  93. exports.isExpired = isExpired;