v8-compile-cache.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. this._previousModuleCompile = Module.prototype._compile;
  133. Module.prototype._compile = function(content, filename) {
  134. const mod = this;
  135. function require(id) {
  136. return mod.require(id);
  137. }
  138. require.resolve = function(request) {
  139. return Module._resolveFilename(request, mod);
  140. };
  141. require.main = process.mainModule;
  142. // Enable support to add extra extension types
  143. require.extensions = Module._extensions;
  144. require.cache = Module._cache;
  145. const dirname = path.dirname(filename);
  146. const compiledWrapper = self._moduleCompile(filename, content);
  147. // We skip the debugger setup because by the time we run, node has already
  148. // done that itself.
  149. const args = [mod.exports, require, mod, filename, dirname, process, global];
  150. return compiledWrapper.apply(mod.exports, args);
  151. };
  152. }
  153. uninstall() {
  154. Module.prototype._compile = this._previousModuleCompile;
  155. }
  156. _moduleCompile(filename, content) {
  157. // https://github.com/nodejs/node/blob/v7.5.0/lib/module.js#L511
  158. // Remove shebang
  159. var contLen = content.length;
  160. if (contLen >= 2) {
  161. if (content.charCodeAt(0) === 35/*#*/ &&
  162. content.charCodeAt(1) === 33/*!*/) {
  163. if (contLen === 2) {
  164. // Exact match
  165. content = '';
  166. } else {
  167. // Find end of shebang line and slice it off
  168. var i = 2;
  169. for (; i < contLen; ++i) {
  170. var code = content.charCodeAt(i);
  171. if (code === 10/*\n*/ || code === 13/*\r*/) break;
  172. }
  173. if (i === contLen) {
  174. content = '';
  175. } else {
  176. // Note that this actually includes the newline character(s) in the
  177. // new output. This duplicates the behavior of the regular
  178. // expression that was previously used to replace the shebang line
  179. content = content.slice(i);
  180. }
  181. }
  182. }
  183. }
  184. // create wrapper function
  185. var wrapper = Module.wrap(content);
  186. var invalidationKey = crypto
  187. .createHash('sha1')
  188. .update(content, 'utf8')
  189. .digest('hex');
  190. var buffer = this._cacheStore.get(filename, invalidationKey);
  191. var script = new vm.Script(wrapper, {
  192. filename: filename,
  193. lineOffset: 0,
  194. displayErrors: true,
  195. cachedData: buffer,
  196. produceCachedData: true,
  197. });
  198. if (script.cachedDataProduced) {
  199. this._cacheStore.set(filename, invalidationKey, script.cachedData);
  200. } else if (script.cachedDataRejected) {
  201. this._cacheStore.delete(filename);
  202. }
  203. var compiledWrapper = script.runInThisContext({
  204. filename: filename,
  205. lineOffset: 0,
  206. columnOffset: 0,
  207. displayErrors: true,
  208. });
  209. return compiledWrapper;
  210. }
  211. }
  212. //------------------------------------------------------------------------------
  213. // utilities
  214. //
  215. // https://github.com/substack/node-mkdirp/blob/f2003bb/index.js#L55-L98
  216. // https://github.com/zertosh/slash-escape/blob/e7ebb99/slash-escape.js
  217. //------------------------------------------------------------------------------
  218. function mkdirpSync(p_) {
  219. _mkdirpSync(path.resolve(p_), parseInt('0777', 8) & ~process.umask());
  220. }
  221. function _mkdirpSync(p, mode) {
  222. try {
  223. fs.mkdirSync(p, mode);
  224. } catch (err0) {
  225. if (err0.code === 'ENOENT') {
  226. _mkdirpSync(path.dirname(p));
  227. _mkdirpSync(p);
  228. } else {
  229. try {
  230. const stat = fs.statSync(p);
  231. if (!stat.isDirectory()) { throw err0; }
  232. } catch (err1) {
  233. throw err0;
  234. }
  235. }
  236. }
  237. }
  238. function slashEscape(str) {
  239. const ESCAPE_LOOKUP = {
  240. '\\': 'zB',
  241. ':': 'zC',
  242. '/': 'zS',
  243. '\x00': 'z0',
  244. 'z': 'zZ',
  245. };
  246. return str.replace(/[\\:\/\x00z]/g, match => (ESCAPE_LOOKUP[match]));
  247. }
  248. function supportsCachedData() {
  249. const script = new vm.Script('""', {produceCachedData: true});
  250. // chakracore, as of v1.7.1.0, returns `false`.
  251. return script.cachedDataProduced === true;
  252. }
  253. function getCacheDir() {
  254. // Avoid cache ownership issues on POSIX systems.
  255. const dirname = typeof process.getuid === 'function'
  256. ? 'v8-compile-cache-' + process.getuid()
  257. : 'v8-compile-cache';
  258. const version = typeof process.versions.v8 === 'string'
  259. ? process.versions.v8
  260. : typeof process.versions.chakracore === 'string'
  261. ? 'chakracore-' + process.versions.chakracore
  262. : 'node-' + process.version;
  263. const cacheDir = path.join(os.tmpdir(), dirname, version);
  264. return cacheDir;
  265. }
  266. function getParentName() {
  267. // `module.parent.filename` is undefined or null when:
  268. // * node -e 'require("v8-compile-cache")'
  269. // * node -r 'v8-compile-cache'
  270. // * Or, requiring from the REPL.
  271. const parentName = module.parent && typeof module.parent.filename === 'string'
  272. ? module.parent.filename
  273. : process.cwd();
  274. return parentName;
  275. }
  276. //------------------------------------------------------------------------------
  277. // main
  278. //------------------------------------------------------------------------------
  279. if (!process.env.DISABLE_V8_COMPILE_CACHE && supportsCachedData()) {
  280. const cacheDir = getCacheDir();
  281. const prefix = getParentName();
  282. const blobStore = new FileSystemBlobStore(cacheDir, prefix);
  283. const nativeCompileCache = new NativeCompileCache();
  284. nativeCompileCache.setCacheStore(blobStore);
  285. nativeCompileCache.install();
  286. process.once('exit', code => {
  287. if (blobStore.isDirty()) {
  288. blobStore.save();
  289. }
  290. nativeCompileCache.uninstall();
  291. });
  292. }
  293. module.exports.__TEST__ = {
  294. FileSystemBlobStore,
  295. NativeCompileCache,
  296. mkdirpSync,
  297. slashEscape,
  298. supportsCachedData,
  299. getCacheDir,
  300. getParentName,
  301. };