You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

v8-compile-cache.js 10KB

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