v8-compile-cache.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. 'use strict';
  2. const Module = require('module');
  3. const crypto = require('crypto');
  4. const fs = require('fs');
  5. const path = require('path');
  6. const vm = require('vm');
  7. const os = require('os');
  8. const hasOwnProperty = Object.prototype.hasOwnProperty;
  9. //------------------------------------------------------------------------------
  10. // FileSystemBlobStore
  11. //------------------------------------------------------------------------------
  12. class FileSystemBlobStore {
  13. constructor(directory, prefix) {
  14. const name = prefix ? slashEscape(prefix + '.') : '';
  15. this._blobFilename = path.join(directory, name + 'BLOB');
  16. this._mapFilename = path.join(directory, name + 'MAP');
  17. this._lockFilename = path.join(directory, name + 'LOCK');
  18. this._directory = directory;
  19. this._load();
  20. }
  21. has(key, invalidationKey) {
  22. if (hasOwnProperty.call(this._memoryBlobs, key)) {
  23. return this._invalidationKeys[key] === invalidationKey;
  24. } else if (hasOwnProperty.call(this._storedMap, key)) {
  25. return this._storedMap[key][0] === invalidationKey;
  26. }
  27. return false;
  28. }
  29. get(key, invalidationKey) {
  30. if (hasOwnProperty.call(this._memoryBlobs, key)) {
  31. if (this._invalidationKeys[key] === invalidationKey) {
  32. return this._memoryBlobs[key];
  33. }
  34. } else if (hasOwnProperty.call(this._storedMap, key)) {
  35. const mapping = this._storedMap[key];
  36. if (mapping[0] === invalidationKey) {
  37. return this._storedBlob.slice(mapping[1], mapping[2]);
  38. }
  39. }
  40. }
  41. set(key, invalidationKey, buffer) {
  42. this._invalidationKeys[key] = invalidationKey;
  43. this._memoryBlobs[key] = buffer;
  44. this._dirty = true;
  45. }
  46. delete(key) {
  47. if (hasOwnProperty.call(this._memoryBlobs, key)) {
  48. this._dirty = true;
  49. delete this._memoryBlobs[key];
  50. }
  51. if (hasOwnProperty.call(this._invalidationKeys, key)) {
  52. this._dirty = true;
  53. delete this._invalidationKeys[key];
  54. }
  55. if (hasOwnProperty.call(this._storedMap, key)) {
  56. this._dirty = true;
  57. delete this._storedMap[key];
  58. }
  59. }
  60. isDirty() {
  61. return this._dirty;
  62. }
  63. save() {
  64. const dump = this._getDump();
  65. const blobToStore = Buffer.concat(dump[0]);
  66. const mapToStore = JSON.stringify(dump[1]);
  67. try {
  68. mkdirpSync(this._directory);
  69. fs.writeFileSync(this._lockFilename, 'LOCK', {flag: 'wx'});
  70. } catch (error) {
  71. // Swallow the exception if we fail to acquire the lock.
  72. return false;
  73. }
  74. try {
  75. fs.writeFileSync(this._blobFilename, blobToStore);
  76. fs.writeFileSync(this._mapFilename, mapToStore);
  77. } catch (error) {
  78. throw error;
  79. } finally {
  80. fs.unlinkSync(this._lockFilename);
  81. }
  82. return true;
  83. }
  84. _load() {
  85. try {
  86. this._storedBlob = fs.readFileSync(this._blobFilename);
  87. this._storedMap = JSON.parse(fs.readFileSync(this._mapFilename));
  88. } catch (e) {
  89. this._storedBlob = Buffer.alloc(0);
  90. this._storedMap = {};
  91. }
  92. this._dirty = false;
  93. this._memoryBlobs = {};
  94. this._invalidationKeys = {};
  95. }
  96. _getDump() {
  97. const buffers = [];
  98. const newMap = {};
  99. let offset = 0;
  100. function push(key, invalidationKey, buffer) {
  101. buffers.push(buffer);
  102. newMap[key] = [invalidationKey, offset, offset + buffer.length];
  103. offset += buffer.length;
  104. }
  105. for (const key of Object.keys(this._memoryBlobs)) {
  106. const buffer = this._memoryBlobs[key];
  107. const invalidationKey = this._invalidationKeys[key];
  108. push(key, invalidationKey, buffer);
  109. }
  110. for (const key of Object.keys(this._storedMap)) {
  111. if (hasOwnProperty.call(newMap, key)) continue;
  112. const mapping = this._storedMap[key];
  113. const buffer = this._storedBlob.slice(mapping[1], mapping[2]);
  114. push(key, mapping[0], buffer);
  115. }
  116. return [buffers, newMap];
  117. }
  118. }
  119. //------------------------------------------------------------------------------
  120. // NativeCompileCache
  121. //------------------------------------------------------------------------------
  122. class NativeCompileCache {
  123. constructor() {
  124. this._cacheStore = null;
  125. this._previousModuleCompile = null;
  126. }
  127. setCacheStore(cacheStore) {
  128. this._cacheStore = cacheStore;
  129. }
  130. install() {
  131. const self = this;
  132. const hasRequireResolvePaths = typeof require.resolve.paths === 'function';
  133. this._previousModuleCompile = Module.prototype._compile;
  134. Module.prototype._compile = function(content, filename) {
  135. const mod = this;
  136. function require(id) {
  137. return mod.require(id);
  138. }
  139. // https://github.com/nodejs/node/blob/v10.15.3/lib/internal/modules/cjs/helpers.js#L28
  140. function resolve(request, options) {
  141. return Module._resolveFilename(request, mod, false, options);
  142. }
  143. require.resolve = resolve;
  144. // https://github.com/nodejs/node/blob/v10.15.3/lib/internal/modules/cjs/helpers.js#L37
  145. // resolve.resolve.paths was added in v8.9.0
  146. if (hasRequireResolvePaths) {
  147. resolve.paths = function paths(request) {
  148. return Module._resolveLookupPaths(request, mod, true);
  149. };
  150. }
  151. require.main = process.mainModule;
  152. // Enable support to add extra extension types
  153. require.extensions = Module._extensions;
  154. require.cache = Module._cache;
  155. const dirname = path.dirname(filename);
  156. const compiledWrapper = self._moduleCompile(filename, content);
  157. // We skip the debugger setup because by the time we run, node has already
  158. // done that itself.
  159. // `Buffer` is included for Electron.
  160. // See https://github.com/zertosh/v8-compile-cache/pull/10#issuecomment-518042543
  161. const args = [mod.exports, require, mod, filename, dirname, process, global, Buffer];
  162. return compiledWrapper.apply(mod.exports, args);
  163. };
  164. }
  165. uninstall() {
  166. Module.prototype._compile = this._previousModuleCompile;
  167. }
  168. _moduleCompile(filename, content) {
  169. // https://github.com/nodejs/node/blob/v7.5.0/lib/module.js#L511
  170. // Remove shebang
  171. var contLen = content.length;
  172. if (contLen >= 2) {
  173. if (content.charCodeAt(0) === 35/*#*/ &&
  174. content.charCodeAt(1) === 33/*!*/) {
  175. if (contLen === 2) {
  176. // Exact match
  177. content = '';
  178. } else {
  179. // Find end of shebang line and slice it off
  180. var i = 2;
  181. for (; i < contLen; ++i) {
  182. var code = content.charCodeAt(i);
  183. if (code === 10/*\n*/ || code === 13/*\r*/) break;
  184. }
  185. if (i === contLen) {
  186. content = '';
  187. } else {
  188. // Note that this actually includes the newline character(s) in the
  189. // new output. This duplicates the behavior of the regular
  190. // expression that was previously used to replace the shebang line
  191. content = content.slice(i);
  192. }
  193. }
  194. }
  195. }
  196. // create wrapper function
  197. var wrapper = Module.wrap(content);
  198. var invalidationKey = crypto
  199. .createHash('sha1')
  200. .update(content, 'utf8')
  201. .digest('hex');
  202. var buffer = this._cacheStore.get(filename, invalidationKey);
  203. var script = new vm.Script(wrapper, {
  204. filename: filename,
  205. lineOffset: 0,
  206. displayErrors: true,
  207. cachedData: buffer,
  208. produceCachedData: true,
  209. });
  210. if (script.cachedDataProduced) {
  211. this._cacheStore.set(filename, invalidationKey, script.cachedData);
  212. } else if (script.cachedDataRejected) {
  213. this._cacheStore.delete(filename);
  214. }
  215. var compiledWrapper = script.runInThisContext({
  216. filename: filename,
  217. lineOffset: 0,
  218. columnOffset: 0,
  219. displayErrors: true,
  220. });
  221. return compiledWrapper;
  222. }
  223. }
  224. //------------------------------------------------------------------------------
  225. // utilities
  226. //
  227. // https://github.com/substack/node-mkdirp/blob/f2003bb/index.js#L55-L98
  228. // https://github.com/zertosh/slash-escape/blob/e7ebb99/slash-escape.js
  229. //------------------------------------------------------------------------------
  230. function mkdirpSync(p_) {
  231. _mkdirpSync(path.resolve(p_), 0o777);
  232. }
  233. function _mkdirpSync(p, mode) {
  234. try {
  235. fs.mkdirSync(p, mode);
  236. } catch (err0) {
  237. if (err0.code === 'ENOENT') {
  238. _mkdirpSync(path.dirname(p));
  239. _mkdirpSync(p);
  240. } else {
  241. try {
  242. const stat = fs.statSync(p);
  243. if (!stat.isDirectory()) { throw err0; }
  244. } catch (err1) {
  245. throw err0;
  246. }
  247. }
  248. }
  249. }
  250. function slashEscape(str) {
  251. const ESCAPE_LOOKUP = {
  252. '\\': 'zB',
  253. ':': 'zC',
  254. '/': 'zS',
  255. '\x00': 'z0',
  256. 'z': 'zZ',
  257. };
  258. return str.replace(/[\\:\/\x00z]/g, match => (ESCAPE_LOOKUP[match]));
  259. }
  260. function supportsCachedData() {
  261. const script = new vm.Script('""', {produceCachedData: true});
  262. // chakracore, as of v1.7.1.0, returns `false`.
  263. return script.cachedDataProduced === true;
  264. }
  265. function getCacheDir() {
  266. // Avoid cache ownership issues on POSIX systems.
  267. const dirname = typeof process.getuid === 'function'
  268. ? 'v8-compile-cache-' + process.getuid()
  269. : 'v8-compile-cache';
  270. const version = typeof process.versions.v8 === 'string'
  271. ? process.versions.v8
  272. : typeof process.versions.chakracore === 'string'
  273. ? 'chakracore-' + process.versions.chakracore
  274. : 'node-' + process.version;
  275. const cacheDir = path.join(os.tmpdir(), dirname, version);
  276. return cacheDir;
  277. }
  278. function getParentName() {
  279. // `module.parent.filename` is undefined or null when:
  280. // * node -e 'require("v8-compile-cache")'
  281. // * node -r 'v8-compile-cache'
  282. // * Or, requiring from the REPL.
  283. const parentName = module.parent && typeof module.parent.filename === 'string'
  284. ? module.parent.filename
  285. : process.cwd();
  286. return parentName;
  287. }
  288. //------------------------------------------------------------------------------
  289. // main
  290. //------------------------------------------------------------------------------
  291. if (!process.env.DISABLE_V8_COMPILE_CACHE && supportsCachedData()) {
  292. const cacheDir = getCacheDir();
  293. const prefix = getParentName();
  294. const blobStore = new FileSystemBlobStore(cacheDir, prefix);
  295. const nativeCompileCache = new NativeCompileCache();
  296. nativeCompileCache.setCacheStore(blobStore);
  297. nativeCompileCache.install();
  298. process.once('exit', code => {
  299. if (blobStore.isDirty()) {
  300. blobStore.save();
  301. }
  302. nativeCompileCache.uninstall();
  303. });
  304. }
  305. module.exports.__TEST__ = {
  306. FileSystemBlobStore,
  307. NativeCompileCache,
  308. mkdirpSync,
  309. slashEscape,
  310. supportsCachedData,
  311. getCacheDir,
  312. getParentName,
  313. };