forked from barbatus/meteor-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.js
238 lines (191 loc) · 5.88 KB
/
cache.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
var path = require("path");
var fs = require("fs");
var assert = require("assert");
var LRU = require("lru-cache");
var sizeof = require('object-sizeof');
var random = require("random-js")();
var utils = require("./utils");
var globalSourceHost = require("./files-source-host");
var logger = require("./logger").default;
var pkgVersion = require("../package.json").version;
function meteorLocalDir() {
var cwdDir = process.cwd();
return cwdDir ? path.join(cwdDir, ".meteor", "local") : __dirname;
}
function ensureCacheDir(cacheDir) {
cacheDir = path.resolve(
cacheDir ||
process.env.TYPESCRIPT_CACHE_DIR ||
path.join(meteorLocalDir(), ".typescript-cache")
);
try {
utils.mkdirp(cacheDir);
} catch (error) {
if (error.code !== "EEXIST") {
throw error;
}
}
return cacheDir;
}
function Cache(length) {
assert.ok(this instanceof Cache);
var maxSize = process.env.TYPESCRIPT_CACHE_SIZE;
this._cache = new LRU({
max: maxSize || 1024 * 1024 * 100,
length: function(obj, key) {
return sizeof(obj);
}
});
}
exports.Cache = Cache;
var Cp = Cache.prototype;
Cp._get = function(cacheKey) {
var pget = logger.newProfiler("cache get");
var result = this._cache.get(cacheKey);
pget.end();
if (!result) {
var pread = logger.newProfiler("cache read");
result = this._readCache(cacheKey);
pread.end();
}
return result;
};
Cp._save = function(cacheKey, result) {
var psave = logger.newProfiler("cache save");
this._cache.set(cacheKey, result);
this._writeCacheAsync(cacheKey, result);
psave.end();
};
Cp._cacheFilename = function(cacheKey) {
// We want cacheKeys to be hex so that they work on any FS
// and never end in .cache.
if (!/^[a-f0-9]+$/.test(cacheKey)) {
throw Error("bad cacheKey: " + cacheKey);
}
return path.join(this.cacheDir, cacheKey + ".cache");
}
Cp._readFileOrNull = function(filename) {
try {
return fs.readFileSync(filename, "utf8");
} catch (e) {
if (e && e.code === "ENOENT")
return null;
throw e;
}
}
Cp._parseJSONOrNull = function(json) {
try {
return JSON.parse(json);
} catch (e) {
if (e instanceof SyntaxError)
return null;
throw e;
}
}
// Returns null if the file does not exist or can't be parsed; otherwise
// returns the parsed compileResult in the file.
Cp._readAndParseCompileResultOrNull = function(filename) {
var content = this._readFileOrNull(filename);
return this._parseJSONOrNull(content);
}
Cp._readCache = function(cacheKey) {
if (!this.cacheDir) {
return null;
}
var cacheFilename = this._cacheFilename(cacheKey);
var compileResult = this._readAndParseCompileResultOrNull(cacheFilename);
if (!compileResult) {
return null;
}
this._cache.set(cacheKey, compileResult);
return compileResult;
}
// We want to write the file atomically.
// But we also don't want to block processing on the file write.
Cp._writeFileAsync = function(filename, content) {
var tempFilename = filename + ".tmp." + random.uuid4();
fs.writeFile(tempFilename, content, function(err) {
if (err) {
logger.debug("file can't be save: %s", err.message);
return;
}
fs.rename(tempFilename, filename, function(err) {
// ignore this error too.
});
});
}
Cp._writeCacheAsync = function(cacheKey, compileResult) {
if (!this.cacheDir) return;
var cacheFilename = this._cacheFilename(cacheKey);
var cacheContents = JSON.stringify(compileResult);
this._writeFileAsync(cacheFilename, cacheContents);
}
// Cache to save and retrieve compiler results.
function CompileCache(cacheDir, sourceHost) {
Cache.apply(this);
this.cacheDir = ensureCacheDir(cacheDir);
this.sourceHost = sourceHost || globalSourceHost;
}
var CCp = CompileCache.prototype = new Cache();
CCp.get = function(filePath, options, compileFn) {
var source = this.sourceHost.get(filePath);
var cacheKey = utils.deepHash(pkgVersion, source, options);
var compileResult = this._get(cacheKey);
if (compileResult) {
logger.debug("file %s result is in cache", filePath);
}
var newResult = compileFn(compileResult);
if (newResult) {
newResult.hash = cacheKey;
this._save(cacheKey, newResult);
return newResult;
}
return compileResult;
};
CCp.getResult = function(filePath, options) {
var source = this.sourceHost.get(filePath);
var cacheKey = utils.deepHash(pkgVersion, source, options);
var compileResult = this._get(cacheKey);
return compileResult;
};
CCp.save = function(filePath, options, compileResult) {
var source = this.sourceHost.get(filePath);
var cacheKey = utils.deepHash(pkgVersion, source, options);
this._save(cacheKey, compileResult);
};
// Check if a compiler result has changed for a file
// to compile with specific options.
CCp.resultChanged = function(filePath, options) {
var source = this.sourceHost.get(filePath);
var cacheKey = utils.deepHash(pkgVersion, source, options);
var compileResult = this._cache.get(cacheKey);
if (!compileResult) {
compileResult = this._readCache(cacheKey);
}
return !compileResult;
};
exports.CompileCache = CompileCache;
/**
* Simple cache that saves file content hashes.
* Used to check if a file content has been changed
* between two successive compilations.
*/
function FileHashCache(cacheDir) {
Cache.apply(this);
this.cacheDir = ensureCacheDir(cacheDir);
}
FileHashCache.prototype = new Cache();
exports.FileHashCache = FileHashCache;
var FHCp = FileHashCache.prototype = new Cache();
FHCp.save = function(filePath, arch, content) {
var profile = { filePath: filePath, arch: arch };
var cacheKey = utils.deepHash(profile);
var contentHash = utils.deepHash(content);
this._save(cacheKey, contentHash);
};
FHCp.isChanged = function(filePath, arch, content) {
var profile = { filePath: filePath, arch: arch };
var cacheKey = utils.deepHash(profile);
var contentHash = utils.deepHash(content);
return this._get(cacheKey) !== contentHash;
};