1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const path = require("path");
|
5 | const fs = require("async-file");
|
6 | const file = require("fs");
|
7 | const filenamify = require("filenamify");
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | class FileStorage {
|
23 | |
24 |
|
25 |
|
26 |
|
27 | constructor(filePath) {
|
28 | this.path = filePath;
|
29 | }
|
30 | read(keys) {
|
31 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
32 | yield this.ensureFolder();
|
33 | const data = {};
|
34 | const promises = keys.map(key => {
|
35 | const filePath = this.getFilePath(key);
|
36 | return parseFile(filePath)
|
37 | .then((obj) => {
|
38 | if (obj) {
|
39 | data[key] = obj;
|
40 | }
|
41 | });
|
42 | });
|
43 | yield Promise.all(promises);
|
44 | return data;
|
45 | });
|
46 | }
|
47 | write(changes) {
|
48 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
49 | yield this.ensureFolder();
|
50 | const promises = Object.keys(changes).map(key => {
|
51 | const filePath = this.getFilePath(key);
|
52 | return fs.exists(filePath)
|
53 | .then((exists) => {
|
54 | const newObj = Object.assign({}, changes[key]);
|
55 | newObj.eTag = (parseInt(newObj.eTag || '0', 10) + 1).toString();
|
56 | return fs.writeTextFile(filePath, JSON.stringify(newObj));
|
57 | });
|
58 | });
|
59 | yield Promise.all(promises);
|
60 | });
|
61 | }
|
62 | delete(keys) {
|
63 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
64 | yield this.ensureFolder();
|
65 | const promises = keys.map(key => {
|
66 | const filePath = this.getFilePath(key);
|
67 | return fs.exists(filePath)
|
68 | .then((exists) => {
|
69 | if (exists) {
|
70 | file.unlinkSync(filePath);
|
71 | }
|
72 | });
|
73 | });
|
74 | yield Promise.all(promises);
|
75 | });
|
76 | }
|
77 | ensureFolder() {
|
78 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
79 | if (this.pEnsureFolder) {
|
80 | return this.pEnsureFolder;
|
81 | }
|
82 | const exists = yield fs.exists(this.path);
|
83 | if (!exists) {
|
84 | this.pEnsureFolder = fs.mkdirp(this.path);
|
85 | }
|
86 | });
|
87 | }
|
88 | getFileName(key) {
|
89 | return filenamify(key);
|
90 | }
|
91 | getFilePath(key) {
|
92 | return path.join(this.path, this.getFileName(key));
|
93 | }
|
94 | }
|
95 | FileStorage.nextTag = 0;
|
96 | exports.FileStorage = FileStorage;
|
97 | function parseFile(filePath) {
|
98 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
99 | try {
|
100 | const exists = yield fs.exists(filePath);
|
101 | const data = yield (exists
|
102 | ? fs.readTextFile(filePath)
|
103 | : Promise.resolve(undefined));
|
104 | try {
|
105 | if (data) {
|
106 | return JSON.parse(data);
|
107 | }
|
108 | }
|
109 | catch (err) {
|
110 | console.warn(`FileStorage: error parsing "${filePath}": ${err.toString()}`);
|
111 | }
|
112 | return undefined;
|
113 | }
|
114 | catch (error) {
|
115 |
|
116 | if (error.code != "ENOENT") {
|
117 | console.warn(`FileStorage: error reading "${filePath}": ${error.toString()}`);
|
118 | }
|
119 | return undefined;
|
120 | }
|
121 | });
|
122 | }
|
123 |
|
\ | No newline at end of file |