-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
173 lines (154 loc) · 5.14 KB
/
index.js
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
const fs = require('fs-extra');
const path = require('path');
const minimatch = require('minimatch');
const XXHash = require('xxhashjs');
const DEFAULT_EXCLUDE_FILE_NAME = 'cache.exclude';
const CACHE_LIFETIME = 1; // in days
const HASH_SEED = 0xE1EC791C;
const MAX_FILENAME_LENGTH = 250;
class WebFileCache {
constructor(fileName = '') {
this.useCache = true;
this.cacheDirPrivate = `.${path.sep}.cache`;
this.excludeListPrivate = [];
this.outdateTime = CACHE_LIFETIME * 86400000; // precalc milliseconds in one day
this.excludeListName = fileName;
}
/**
* Transform url or github link to path and filename
* It is important, that path and filename are unique,
* because collision can break the build
* @param {string} link link to the file
* @return {string} folder and name, where cache file can be found
* @private
*/
getCachedPath(link) {
let modifiedLink = link.replace(/:\/\//, '#'); // replace '://' for '#' in url
modifiedLink = modifiedLink.replace(/\//g, '-'); // replace '/' for '-'
const parts = modifiedLink.match(/^([^?]*)(\?(.*))?$/); // delete get parameters from url
if (parts && parts[3]) {
modifiedLink = parts[1] + XXHash.h64(parts[3], HASH_SEED);
}
if (modifiedLink.length > MAX_FILENAME_LENGTH) {
const startPart = modifiedLink.substr(0, 100);
const endPart = modifiedLink.substr(modifiedLink.length - 100);
const middlePart = XXHash.h64(modifiedLink, HASH_SEED);
modifiedLink = startPart + endPart + middlePart;
}
return path.join(this.cacheDir, modifiedLink);
}
/**
* Create all subfolders and write file to them
* @param {string} path path to the file
* @param {string} content content of the file
* @private
*/
cacheFile(filePath, content) {
const cachedPath = this.getCachedPath(filePath);
try {
fs.ensureDirSync(path.dirname(cachedPath));
fs.writeFileSync(cachedPath, content);
} catch (err) {
// did not fail if something goes wrong with cache
console.error(err);
}
}
/**
* Check, is file exist by link and return path if exist
* @param {{dirPath : string, fileName : string} | false} link link to the file
* @return {string|false} result
*/
findFile(link) {
const finalPath = this.getCachedPath(link);
return fs.existsSync(finalPath) ? finalPath : false;
}
/**
* Check, has file to be excluded from cache
* @param {string} path to the file
* @return {boolean} result
*/
isExcludedFromCache(includedPath) {
return this.excludeListPrivate.some(regexp => regexp.test(includedPath));
}
/**
* Check, should file be cached
* @param {string} includePath to the file
* @return {boolean} result
* @private
*/
toBeCached(includePath) {
return this.useCache && !this.isExcludedFromCache(includePath);
}
readCachedFile(includePath) { // eslint-disable-line class-methods-use-this
return fs.readFile(includePath, 'utf-8');
}
/**
* Check, is file outdated
* @param {string} path to the file
* @return {boolean} result
*/
isCacheFileOutdate(pathname) {
const stat = fs.statSync(pathname);
return Date.now() - stat.mtime > this.outdateTime;
}
/**
* Read includePath and use cache if needed
* @param {function} reader reader
* @param {string} includePath link to the source
* @return {string} content of cached file
* @private
*/
async read(loadFunction, includePath) {
let readFunction = loadFunction;
let includedPath = includePath;
let needCache = false;
if (this.toBeCached(includePath)) {
const result = this.findFile(includePath);
if (result && !this.isCacheFileOutdate(result)) {
// includedPath reader to local reader
includedPath = result;
readFunction = this.readCachedFile;
} else {
needCache = true;
}
}
const content = await readFunction(includedPath);
if (needCache && this.useCache) {
this.cacheFile(includePath, content);
}
return content;
}
clearCache() {
fs.removeSync(this.cacheDir);
}
set cacheDir(value) {
this.cacheDirPrivate = value.replace(/\//g, path.sep);
}
get cacheDir() {
return this.cacheDirPrivate;
}
/**
* Construct exclude regexp list from filename
* @param {string} name of exclude file. '' for default
*/
set excludeList(fileName = DEFAULT_EXCLUDE_FILE_NAME) {
const newPath = fileName;
// check is fileName exist
if (!fs.existsSync(newPath)) {
if (fileName === DEFAULT_EXCLUDE_FILE_NAME) {
// if it isn't exist and it is default, then put empty list
this.excludeListPrivate = [];
return;
}
throw new Error(`${newPath} file does not exist`);
}
const content = fs.readFileSync(newPath, 'utf8');
const filenames = content.split(/\n|\r\n/);
// filters not empty strings, and makes regular expression from template
const patterns = filenames.map(value => value.trimLeft()) // trim for "is commented" check
.filter(value => (value !== '' && value[0] !== '#'))
.map(value => minimatch.makeRe(value));
this.excludeListPrivate = patterns;
}
}
module.exports = WebFileCache;