Repository navigation
Expand file tree
/
Copy pathasync.js
More file actions
205 lines (179 loc) · 5.67 KB
/
Copy pathasync.js
File metadata and controls
205 lines (179 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"use strict";
const fs = require('fs');
const path = require('path');
class CDN {
constructor(options = {}) {
this.dir = options.dir || './cdn/';
this.lockDir = options.lockDir || options.dir || './cdn/';
this.extension = options.extension || '.cdn.json';
this.lockExtension = options.lockExtension || '.cdn.lock';
this.ttl = (typeof options.ttl === 'number') ? options.ttl : 3600;
};
getFilePath(name, isLock = false) {
const dir = isLock ? this.lockDir : this.dir;
const extension = isLock ? this.lockExtension : this.extension;
return path.resolve(path.join(dir, String(name).replace(/[^a-z0-9]+/gi, '-').replace(/-+/g, '-').replace(/^-|-$/g, '') + extension));
};
async cache(name, object) {
const file = this.getFilePath(name);
try {
const json = JSON.stringify(object);
await fs.promises.mkdir(path.dirname(file), { recursive: true });
await fs.promises.writeFile(file, json);
return true;
} catch (e) {
if (e.code === 'EEXIST') {
console.error(`CDN file ${file} already exists.`);
return false;
};
console.error('Error in CDN.cache():', e);
return false;
};
};
async lock(name, content) {
const file = this.getFilePath(name, true);
try {
await fs.promises.mkdir(path.dirname(file), { recursive: true });
await fs.promises.writeFile(file, content || 'locked', { flag: 'wx' });
return true;
} catch (e) {
if (e.code === 'EEXIST') {
console.error(`Lockfile ${file} already exists.`);
return false;
};
console.error('Error in CDN.lock():', e);
return false;
};
};
async delete(name) {
const file = this.getFilePath(name);
try {
await fs.promises.unlink(file);
return true;
} catch (e) {
if (e.code === 'ENOENT') {
return true;
};
console.error('Error in CDN.delete():', e);
return false;
};
};
async unlock(name) {
const file = this.getFilePath(name, true);
try {
await fs.promises.unlink(file);
return true;
} catch (e) {
if (e.code === 'ENOENT') {
return true;
};
console.error('Error in CDN.unlock():', e);
return false;
};
};
async get(name) {
const file = this.getFilePath(name);
try {
const data = await fs.promises.readFile(file, 'utf8');
try {
return JSON.parse(data);
} catch (e) {
return data;
};
} catch (e) {
if (e.code === 'ENOENT') {
console.error(`CDN file ${file} does not exist.`);
return false;
};
console.error('Error in CDN.get():', e);
return false;
};
};
async valid(name, ttl) {
ttl = (typeof ttl === 'number') ? ttl : this.ttl;
const file = this.getFilePath(name);
try {
const stats = await fs.promises.stat(file);
return (stats.mtimeMs + ttl * 1000) > Date.now();
} catch (e) {
if (e.code === 'ENOENT') {
console.error(`CDN file ${file} does not exist.`);
return false;
};
console.error('Error in CDN.valid():', e);
return false;
};
};
async exists(name, isLock = false) {
const file = this.getFilePath(name, isLock);
try {
await fs.promises.access(file);
return true;
} catch (e) {
if (e.code === 'ENOENT') {
console.error(`${isLock ? 'Lockfile' : 'CDN file'} ${file} does not exist.`);
return false;
};
console.error('Error in CDN.valid():', e);
return false;
};
};
async all() {
try {
const files = await fs.promises.readdir(this.dir);
return files.filter(file => file.endsWith(this.extension)).map(file => file.replace(this.extension, ''));
} catch (e) {
console.error('Error in CDN.all():', e);
return [];
};
};
async allLocks() {
try {
const files = await fs.promises.readdir(this.lockDir);
return files.filter(file => file.endsWith(this.lockExtension)).map(file => file.replace(this.lockExtension, ''));
} catch (e) {
console.error('Error in CDN.allLocks():', e);
return [];
};
};
async push(name, object) {
return this.cache(name, object);
};
async set(name, object) {
return this.cache(name, object);
};
async put(name, object) {
return this.cache(name, object);
};
async update(name, object) {
return this.cache(name, object);
};
async upd(name, object) {
return this.cache(name, object);
};
async del(name) {
return this.delete(name);
};
async remove(name) {
return this.delete(name);
};
async rem(name) {
return this.delete(name);
};
async pull(name) {
return this.get(name);
};
async contents(name) {
return this.get(name);
};
async locked(name) {
return this.exists(name, true);
};
async unlocked(name) {
return !(await this.valid(name));
};
async locks() {
return this.allLocks();
};
};
module.exports = CDN;