3 const Module = require('module');
4 const crypto = require('crypto');
5 const fs = require('fs');
6 const path = require('path');
7 const vm = require('vm');
8 const os = require('os');
10 const hasOwnProperty = Object.prototype.hasOwnProperty;
12 //------------------------------------------------------------------------------
13 // FileSystemBlobStore
14 //------------------------------------------------------------------------------
16 class FileSystemBlobStore {
17 constructor(directory, prefix) {
18 const name = prefix ? slashEscape(prefix + '.') : '';
19 this._blobFilename = path.join(directory, name + 'BLOB');
20 this._mapFilename = path.join(directory, name + 'MAP');
21 this._lockFilename = path.join(directory, name + 'LOCK');
22 this._directory = directory;
26 has(key, invalidationKey) {
27 if (hasOwnProperty.call(this._memoryBlobs, key)) {
28 return this._invalidationKeys[key] === invalidationKey;
29 } else if (hasOwnProperty.call(this._storedMap, key)) {
30 return this._storedMap[key][0] === invalidationKey;
35 get(key, invalidationKey) {
36 if (hasOwnProperty.call(this._memoryBlobs, key)) {
37 if (this._invalidationKeys[key] === invalidationKey) {
38 return this._memoryBlobs[key];
40 } else if (hasOwnProperty.call(this._storedMap, key)) {
41 const mapping = this._storedMap[key];
42 if (mapping[0] === invalidationKey) {
43 return this._storedBlob.slice(mapping[1], mapping[2]);
48 set(key, invalidationKey, buffer) {
49 this._invalidationKeys[key] = invalidationKey;
50 this._memoryBlobs[key] = buffer;
55 if (hasOwnProperty.call(this._memoryBlobs, key)) {
57 delete this._memoryBlobs[key];
59 if (hasOwnProperty.call(this._invalidationKeys, key)) {
61 delete this._invalidationKeys[key];
63 if (hasOwnProperty.call(this._storedMap, key)) {
65 delete this._storedMap[key];
74 const dump = this._getDump();
75 const blobToStore = Buffer.concat(dump[0]);
76 const mapToStore = JSON.stringify(dump[1]);
79 mkdirpSync(this._directory);
80 fs.writeFileSync(this._lockFilename, 'LOCK', {flag: 'wx'});
82 // Swallow the exception if we fail to acquire the lock.
87 fs.writeFileSync(this._blobFilename, blobToStore);
88 fs.writeFileSync(this._mapFilename, mapToStore);
92 fs.unlinkSync(this._lockFilename);
100 this._storedBlob = fs.readFileSync(this._blobFilename);
101 this._storedMap = JSON.parse(fs.readFileSync(this._mapFilename));
103 this._storedBlob = Buffer.alloc(0);
104 this._storedMap = {};
107 this._memoryBlobs = {};
108 this._invalidationKeys = {};
116 function push(key, invalidationKey, buffer) {
117 buffers.push(buffer);
118 newMap[key] = [invalidationKey, offset, offset + buffer.length];
119 offset += buffer.length;
122 for (const key of Object.keys(this._memoryBlobs)) {
123 const buffer = this._memoryBlobs[key];
124 const invalidationKey = this._invalidationKeys[key];
125 push(key, invalidationKey, buffer);
128 for (const key of Object.keys(this._storedMap)) {
129 if (hasOwnProperty.call(newMap, key)) continue;
130 const mapping = this._storedMap[key];
131 const buffer = this._storedBlob.slice(mapping[1], mapping[2]);
132 push(key, mapping[0], buffer);
135 return [buffers, newMap];
139 //------------------------------------------------------------------------------
140 // NativeCompileCache
141 //------------------------------------------------------------------------------
143 class NativeCompileCache {
145 this._cacheStore = null;
146 this._previousModuleCompile = null;
149 setCacheStore(cacheStore) {
150 this._cacheStore = cacheStore;
155 const hasRequireResolvePaths = typeof require.resolve.paths === 'function';
156 this._previousModuleCompile = Module.prototype._compile;
157 Module.prototype._compile = function(content, filename) {
160 function require(id) {
161 return mod.require(id);
164 // https://github.com/nodejs/node/blob/v10.15.3/lib/internal/modules/cjs/helpers.js#L28
165 function resolve(request, options) {
166 return Module._resolveFilename(request, mod, false, options);
168 require.resolve = resolve;
170 // https://github.com/nodejs/node/blob/v10.15.3/lib/internal/modules/cjs/helpers.js#L37
171 // resolve.resolve.paths was added in v8.9.0
172 if (hasRequireResolvePaths) {
173 resolve.paths = function paths(request) {
174 return Module._resolveLookupPaths(request, mod, true);
178 require.main = process.mainModule;
180 // Enable support to add extra extension types
181 require.extensions = Module._extensions;
182 require.cache = Module._cache;
184 const dirname = path.dirname(filename);
186 const compiledWrapper = self._moduleCompile(filename, content);
188 // We skip the debugger setup because by the time we run, node has already
191 // `Buffer` is included for Electron.
192 // See https://github.com/zertosh/v8-compile-cache/pull/10#issuecomment-518042543
193 const args = [mod.exports, require, mod, filename, dirname, process, global, Buffer];
194 return compiledWrapper.apply(mod.exports, args);
199 Module.prototype._compile = this._previousModuleCompile;
202 _moduleCompile(filename, content) {
203 // https://github.com/nodejs/node/blob/v7.5.0/lib/module.js#L511
206 var contLen = content.length;
208 if (content.charCodeAt(0) === 35/*#*/ &&
209 content.charCodeAt(1) === 33/*!*/) {
214 // Find end of shebang line and slice it off
216 for (; i < contLen; ++i) {
217 var code = content.charCodeAt(i);
218 if (code === 10/*\n*/ || code === 13/*\r*/) break;
223 // Note that this actually includes the newline character(s) in the
224 // new output. This duplicates the behavior of the regular
225 // expression that was previously used to replace the shebang line
226 content = content.slice(i);
232 // create wrapper function
233 var wrapper = Module.wrap(content);
235 var invalidationKey = crypto
237 .update(content, 'utf8')
240 var buffer = this._cacheStore.get(filename, invalidationKey);
242 var script = new vm.Script(wrapper, {
247 produceCachedData: true,
250 if (script.cachedDataProduced) {
251 this._cacheStore.set(filename, invalidationKey, script.cachedData);
252 } else if (script.cachedDataRejected) {
253 this._cacheStore.delete(filename);
256 var compiledWrapper = script.runInThisContext({
263 return compiledWrapper;
267 //------------------------------------------------------------------------------
270 // https://github.com/substack/node-mkdirp/blob/f2003bb/index.js#L55-L98
271 // https://github.com/zertosh/slash-escape/blob/e7ebb99/slash-escape.js
272 //------------------------------------------------------------------------------
274 function mkdirpSync(p_) {
275 _mkdirpSync(path.resolve(p_), 0o777);
278 function _mkdirpSync(p, mode) {
280 fs.mkdirSync(p, mode);
282 if (err0.code === 'ENOENT') {
283 _mkdirpSync(path.dirname(p));
287 const stat = fs.statSync(p);
288 if (!stat.isDirectory()) { throw err0; }
296 function slashEscape(str) {
297 const ESCAPE_LOOKUP = {
304 return str.replace(/[\\:\/\x00z]/g, match => (ESCAPE_LOOKUP[match]));
307 function supportsCachedData() {
308 const script = new vm.Script('""', {produceCachedData: true});
309 // chakracore, as of v1.7.1.0, returns `false`.
310 return script.cachedDataProduced === true;
313 function getCacheDir() {
314 // Avoid cache ownership issues on POSIX systems.
315 const dirname = typeof process.getuid === 'function'
316 ? 'v8-compile-cache-' + process.getuid()
317 : 'v8-compile-cache';
318 const version = typeof process.versions.v8 === 'string'
319 ? process.versions.v8
320 : typeof process.versions.chakracore === 'string'
321 ? 'chakracore-' + process.versions.chakracore
322 : 'node-' + process.version;
323 const cacheDir = path.join(os.tmpdir(), dirname, version);
327 function getParentName() {
328 // `module.parent.filename` is undefined or null when:
329 // * node -e 'require("v8-compile-cache")'
330 // * node -r 'v8-compile-cache'
331 // * Or, requiring from the REPL.
332 const parentName = module.parent && typeof module.parent.filename === 'string'
333 ? module.parent.filename
338 //------------------------------------------------------------------------------
340 //------------------------------------------------------------------------------
342 if (!process.env.DISABLE_V8_COMPILE_CACHE && supportsCachedData()) {
343 const cacheDir = getCacheDir();
344 const prefix = getParentName();
345 const blobStore = new FileSystemBlobStore(cacheDir, prefix);
347 const nativeCompileCache = new NativeCompileCache();
348 nativeCompileCache.setCacheStore(blobStore);
349 nativeCompileCache.install();
351 process.once('exit', code => {
352 if (blobStore.isDirty()) {
355 nativeCompileCache.uninstall();
359 module.exports.__TEST__ = {