UNPKG

4.83 kBJavaScriptView Raw
1"use strict";
2var __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};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.FileSystemCache = void 0;
13const common_1 = require("./common");
14class FileSystemCache {
15 constructor(options = {}) {
16 this.basePath = formatPath(options.basePath);
17 this.ns = common_1.Util.hash(options.ns);
18 this.ttl = typeof options.ttl === 'undefined' ? 0 : options.ttl;
19 if (common_1.Util.isString(options.extension))
20 this.extension = options.extension;
21 if (common_1.Util.isFileSync(this.basePath)) {
22 throw new Error(`The basePath '${this.basePath}' is a file. It should be a folder.`);
23 }
24 }
25 path(key) {
26 if (common_1.Util.isNothing(key))
27 throw new Error(`Path requires a cache key.`);
28 let name = common_1.Util.hash(key);
29 if (this.ns)
30 name = `${this.ns}-${name}`;
31 if (this.extension)
32 name = `${name}.${this.extension.replace(/^\./, '')}`;
33 return `${this.basePath}/${name}`;
34 }
35 fileExists(key) {
36 return common_1.fs.pathExists(this.path(key));
37 }
38 ensureBasePath() {
39 return __awaiter(this, void 0, void 0, function* () {
40 if (!this.basePathExists)
41 yield common_1.fs.ensureDir(this.basePath);
42 this.basePathExists = true;
43 });
44 }
45 get(key, defaultValue) {
46 return common_1.Util.getValueP(this.path(key), defaultValue);
47 }
48 getSync(key, defaultValue) {
49 const path = this.path(key);
50 return common_1.fs.existsSync(path) ? common_1.Util.toGetValue(common_1.fs.readJsonSync(path)) : defaultValue;
51 }
52 set(key, value, ttl) {
53 return __awaiter(this, void 0, void 0, function* () {
54 const path = this.path(key);
55 ttl = typeof ttl === 'number' ? ttl : this.ttl;
56 yield this.ensureBasePath();
57 yield common_1.fs.outputFile(path, common_1.Util.toJson(value, ttl));
58 return { path };
59 });
60 }
61 setSync(key, value, ttl) {
62 ttl = typeof ttl === 'number' ? ttl : this.ttl;
63 common_1.fs.outputFileSync(this.path(key), common_1.Util.toJson(value, ttl));
64 return this;
65 }
66 remove(key) {
67 return common_1.fs.remove(this.path(key));
68 }
69 clear() {
70 return __awaiter(this, void 0, void 0, function* () {
71 const paths = yield common_1.Util.filePathsP(this.basePath, this.ns);
72 yield Promise.all(paths.map((path) => common_1.fs.remove(path)));
73 console.groupEnd();
74 });
75 }
76 save(input) {
77 return __awaiter(this, void 0, void 0, function* () {
78 let items = (Array.isArray(input) ? input : [input]);
79 const isValid = (item) => {
80 if (!common_1.R.is(Object, item))
81 return false;
82 return item.key && item.value;
83 };
84 items = items.filter((item) => Boolean(item));
85 items
86 .filter((item) => !isValid(item))
87 .forEach(() => {
88 const err = `Save items not valid, must be an array of {key, value} objects.`;
89 throw new Error(err);
90 });
91 if (items.length === 0)
92 return { paths: [] };
93 const paths = yield Promise.all(items.map((item) => __awaiter(this, void 0, void 0, function* () { return (yield this.set(item.key, item.value)).path; })));
94 return { paths };
95 });
96 }
97 load() {
98 return __awaiter(this, void 0, void 0, function* () {
99 const paths = yield common_1.Util.filePathsP(this.basePath, this.ns);
100 if (paths.length === 0)
101 return { files: [] };
102 const files = yield Promise.all(paths.map((path) => __awaiter(this, void 0, void 0, function* () { return ({ path, value: yield common_1.Util.getValueP(path) }); })));
103 return { files };
104 });
105 }
106}
107exports.FileSystemCache = FileSystemCache;
108function formatPath(path) {
109 path = common_1.Util.ensureString('./.cache', path);
110 path = common_1.Util.toAbsolutePath(path);
111 return path;
112}