index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // this entire module is depressing. i should have spent my time learning
  2. // how to patch v8 so that these options would just be available on the
  3. // process object.
  4. var os = require('os');
  5. var fs = require('fs');
  6. var path = require('path');
  7. var crypto = require('crypto');
  8. var execFile = require('child_process').execFile;
  9. var configPath = require('./config-path.js')(process.platform);
  10. var version = require('./package.json').version;
  11. var env = process.env;
  12. var user = env.LOGNAME || env.USER || env.LNAME || env.USERNAME || '';
  13. var exclusions = ['--help', '--completion_bash'];
  14. // This number must be incremented whenever the generated cache file changes.
  15. var CACHE_VERSION = 2;
  16. var configfile = '.v8flags-' + CACHE_VERSION + '-' + process.versions.v8 + '.' + crypto.createHash('md5').update(user).digest('hex') + '.json';
  17. var failureMessage = [
  18. 'Unable to cache a config file for v8flags to your home directory',
  19. 'or a temporary folder. To fix this problem, please correct your',
  20. 'environment by setting HOME=/path/to/home or TEMP=/path/to/temp.',
  21. 'NOTE: the user running this must be able to access provided path.',
  22. 'If all else fails, please open an issue here:',
  23. 'http://github.com/tkellen/js-v8flags',
  24. ].join('\n');
  25. function fail(err) {
  26. err.message += '\n\n' + failureMessage;
  27. return err;
  28. }
  29. function openConfig(cb) {
  30. fs.mkdir(configPath, function() {
  31. tryOpenConfig(path.join(configPath, configfile), function(err, fd) {
  32. if (err) {
  33. return tryOpenConfig(path.join(os.tmpdir(), configfile), cb);
  34. }
  35. return cb(null, fd);
  36. });
  37. });
  38. }
  39. function tryOpenConfig(configpath, cb) {
  40. try {
  41. // if the config file is valid, it should be json and therefore
  42. // node should be able to require it directly. if this doesn't
  43. // throw, we're done!
  44. var content = require(configpath);
  45. process.nextTick(function() {
  46. cb(null, content);
  47. });
  48. } catch (e) {
  49. // if requiring the config file failed, maybe it doesn't exist, or
  50. // perhaps it has become corrupted. instead of calling back with the
  51. // content of the file, call back with a file descriptor that we can
  52. // write the cached data to
  53. fs.open(configpath, 'w+', function(err, fd) {
  54. if (err) {
  55. return cb(err);
  56. }
  57. return cb(null, fd);
  58. });
  59. }
  60. }
  61. // Node <= 9 outputs _ in flags with multiple words, while node 10
  62. // uses -. Both ways are accepted anyway, so always use `_` for better
  63. // compatibility.
  64. // We must not replace the first two --.
  65. function normalizeFlagName(flag) {
  66. return '--' + flag.slice(4).replace(/-/g, '_');
  67. }
  68. // i can't wait for the day this whole module is obsolete because these
  69. // options are available on the process object. this executes node with
  70. // `--v8-options` and parses the result, returning an array of command
  71. // line flags.
  72. function getFlags(cb) {
  73. var errored = false;
  74. var pending = 0;
  75. var flags = [];
  76. runNode('--help');
  77. runNode('--v8-options');
  78. function runNode(option) {
  79. pending++;
  80. execFile(process.execPath, [option], function(execErr, result) {
  81. if (execErr || errored) {
  82. if (!errored) {
  83. errored = true;
  84. cb(execErr);
  85. }
  86. return;
  87. }
  88. var index = result.indexOf('\nOptions:');
  89. if (index >= 0) {
  90. var regexp = /^\s\s--[\w-]+/gm;
  91. regexp.lastIndex = index;
  92. var matchedFlags = result.match(regexp);
  93. if (matchedFlags) {
  94. flags = flags.concat(matchedFlags
  95. .map(normalizeFlagName)
  96. .filter(function(name) {
  97. return exclusions.indexOf(name) === -1;
  98. })
  99. );
  100. }
  101. }
  102. if (--pending === 0) {
  103. cb(null, flags);
  104. }
  105. });
  106. }
  107. }
  108. // write some json to a file descriptor. if this fails, call back
  109. // with both the error and the data that was meant to be written.
  110. function writeConfig(fd, flags, cb) {
  111. var json = JSON.stringify(flags);
  112. var buf;
  113. if (Buffer.from && Buffer.from !== Uint8Array.from) {
  114. // Node.js 4.5.0 or newer
  115. buf = Buffer.from(json);
  116. } else {
  117. // Old Node.js versions
  118. // The typeof safeguard below is mostly against accidental copy-pasting
  119. // and code rewrite, it never happens as json is always a string here.
  120. if (typeof json === 'number') {
  121. throw new Error('Unexpected type number');
  122. }
  123. buf = new Buffer(json);
  124. }
  125. return fs.write(fd, buf, 0, buf.length, 0 , function(writeErr) {
  126. fs.close(fd, function(closeErr) {
  127. var err = writeErr || closeErr;
  128. if (err) {
  129. return cb(fail(err), flags);
  130. }
  131. return cb(null, flags);
  132. });
  133. });
  134. }
  135. module.exports = function(cb) {
  136. // bail early if this is not node
  137. var isElectron = process.versions && process.versions.electron;
  138. if (isElectron) {
  139. return process.nextTick(function() {
  140. cb(null, []);
  141. });
  142. }
  143. // attempt to open/read cache file
  144. openConfig(function(openErr, result) {
  145. if (!openErr && typeof result !== 'number') {
  146. return cb(null, result);
  147. }
  148. // if the result is not an array, we need to go fetch
  149. // the flags by invoking node with `--v8-options`
  150. getFlags(function(flagsErr, flags) {
  151. // if there was an error fetching the flags, bail immediately
  152. if (flagsErr) {
  153. return cb(flagsErr);
  154. }
  155. // if there was a problem opening the config file for writing
  156. // throw an error but include the flags anyway so that users
  157. // can continue to execute (at the expense of having to fetch
  158. // flags on every run until they fix the underyling problem).
  159. if (openErr) {
  160. return cb(fail(openErr), flags);
  161. }
  162. // write the config file to disk so subsequent runs can read
  163. // flags out of a cache file.
  164. return writeConfig(result, flags, cb);
  165. });
  166. });
  167. };
  168. module.exports.configfile = configfile;
  169. module.exports.configPath = configPath;