123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- "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());
- });
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- 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;
- const libs_1 = require("./libs");
- const isNothing = (value) => libs_1.R.isNil(value) || libs_1.R.isEmpty(value);
- exports.isNothing = isNothing;
- exports.isString = libs_1.R.is(String);
- const toAbsolutePath = (path) => {
- return path.startsWith('.') ? libs_1.fsPath.resolve(path) : path;
- };
- exports.toAbsolutePath = toAbsolutePath;
- const ensureString = (defaultValue, text) => {
- return typeof text === 'string' ? text : defaultValue;
- };
- exports.ensureString = ensureString;
- const compact = (input) => {
- const flat = [].concat(...input);
- return flat.filter((value) => !libs_1.R.isNil(value));
- };
- exports.compact = compact;
- exports.toStringArray = libs_1.R.pipe(exports.compact, libs_1.R.map(libs_1.R.toString));
- const isFileSync = (path) => {
- return libs_1.fs.existsSync(path) ? libs_1.fs.lstatSync(path).isFile() : false;
- };
- exports.isFileSync = isFileSync;
- const readFileSync = (path) => {
- return libs_1.fs.existsSync(path) ? libs_1.fs.readFileSync(path).toString() : undefined;
- };
- exports.readFileSync = readFileSync;
- const filePathsP = (basePath, ns) => __awaiter(void 0, void 0, void 0, function* () {
- if (!(yield libs_1.fs.pathExists(basePath)))
- return [];
- return (yield libs_1.fs.readdir(basePath))
- .filter(Boolean)
- .filter((name) => (ns ? name.startsWith(ns) : true))
- .filter((name) => (!ns ? !name.includes('-') : true))
- .map((name) => `${basePath}/${name}`);
- });
- exports.filePathsP = filePathsP;
- const hash = (...values) => {
- if (libs_1.R.pipe(exports.compact, libs_1.R.isEmpty)(values))
- return undefined;
- const resultHash = libs_1.crypto.createHash('sha1');
- const addValue = (value) => resultHash.update(value);
- const addValues = libs_1.R.forEach(addValue);
- libs_1.R.pipe(exports.toStringArray, addValues)(values);
- return resultHash.digest('hex');
- };
- exports.hash = hash;
- function getValueP(path, defaultValue) {
- return __awaiter(this, void 0, void 0, function* () {
- const exists = yield libs_1.fs.pathExists(path);
- if (!exists)
- return defaultValue;
- try {
- return (0, exports.toGetValue)(yield libs_1.fs.readJson(path));
- }
- catch (error) {
- if (error.code === 'ENOENT')
- return defaultValue;
- if (error.message === 'Cache item has expired.') {
- libs_1.fs.removeSync(path);
- return defaultValue;
- }
- throw new Error(`Failed to read cache value at: ${path}. ${error.message}`);
- }
- });
- }
- exports.getValueP = getValueP;
- const toGetValue = (data) => {
- if ((0, exports.isExpired)(data))
- throw new Error(`Cache item has expired.`);
- if (data.type === 'Date')
- return new Date(data.value);
- return data.value;
- };
- exports.toGetValue = toGetValue;
- const toJson = (value, ttl) => JSON.stringify({ value, type: libs_1.R.type(value), created: new Date(), ttl });
- exports.toJson = toJson;
- const isExpired = (data) => {
- const timeElapsed = (new Date().getTime() - new Date(data.created).getTime()) / 1000;
- return timeElapsed > data.ttl && data.ttl > 0;
- };
- exports.isExpired = isExpired;
|