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.

index.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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'];
  14. // This number must be incremented whenever the generated cache file changes.
  15. var CACHE_VERSION = 1;
  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. execFile(process.execPath, ['--v8-options'], function(execErr, result) {
  74. if (execErr) {
  75. return cb(execErr);
  76. }
  77. // If the binary doesn't return flags in the way we expect, default to an empty array
  78. // Reference https://github.com/gulpjs/v8flags/issues/53
  79. var matchedFlags = result.match(/\s\s--[\w-]+/gm) || [];
  80. var flags = matchedFlags
  81. .map(normalizeFlagName)
  82. .filter(function(name) {
  83. return exclusions.indexOf(name) === -1;
  84. });
  85. return cb(null, flags);
  86. });
  87. }
  88. // write some json to a file descriptor. if this fails, call back
  89. // with both the error and the data that was meant to be written.
  90. function writeConfig(fd, flags, cb) {
  91. var json = JSON.stringify(flags);
  92. var buf;
  93. if (Buffer.from && Buffer.from !== Uint8Array.from) {
  94. // Node.js 4.5.0 or newer
  95. buf = Buffer.from(json);
  96. } else {
  97. // Old Node.js versions
  98. // The typeof safeguard below is mostly against accidental copy-pasting
  99. // and code rewrite, it never happens as json is always a string here.
  100. if (typeof json === 'number') {
  101. throw new Error('Unexpected type number');
  102. }
  103. buf = new Buffer(json);
  104. }
  105. return fs.write(fd, buf, 0, buf.length, 0 , function(writeErr) {
  106. fs.close(fd, function(closeErr) {
  107. var err = writeErr || closeErr;
  108. if (err) {
  109. return cb(fail(err), flags);
  110. }
  111. return cb(null, flags);
  112. });
  113. });
  114. }
  115. module.exports = function(cb) {
  116. // bail early if this is not node
  117. var isElectron = process.versions && process.versions.electron;
  118. if (isElectron) {
  119. return process.nextTick(function() {
  120. cb(null, []);
  121. });
  122. }
  123. // attempt to open/read cache file
  124. openConfig(function(openErr, result) {
  125. if (!openErr && typeof result !== 'number') {
  126. return cb(null, result);
  127. }
  128. // if the result is not an array, we need to go fetch
  129. // the flags by invoking node with `--v8-options`
  130. getFlags(function(flagsErr, flags) {
  131. // if there was an error fetching the flags, bail immediately
  132. if (flagsErr) {
  133. return cb(flagsErr);
  134. }
  135. // if there was a problem opening the config file for writing
  136. // throw an error but include the flags anyway so that users
  137. // can continue to execute (at the expense of having to fetch
  138. // flags on every run until they fix the underyling problem).
  139. if (openErr) {
  140. return cb(fail(openErr), flags);
  141. }
  142. // write the config file to disk so subsequent runs can read
  143. // flags out of a cache file.
  144. return writeConfig(result, flags, cb);
  145. });
  146. });
  147. };
  148. module.exports.configfile = configfile;
  149. module.exports.configPath = configPath;