index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. const os = require('os');
  5. const fs = require('fs');
  6. const path = require('path');
  7. const crypto = require('crypto');
  8. const execFile = require('child_process').execFile;
  9. const configPath = require('./config-path.js')(process.platform);
  10. const version = require("./package.json").version;
  11. const env = process.env;
  12. const user = env.LOGNAME || env.USER || env.LNAME || env.USERNAME || '';
  13. const exclusions = ['--help'];
  14. // This number must be incremented whenever the generated cache file changes.
  15. const CACHE_VERSION = 1;
  16. const configfile = '.v8flags-'+CACHE_VERSION+'-'+process.versions.v8+'.'+crypto.createHash('md5').update(user).digest('hex')+'.json';
  17. const 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) return tryOpenConfig(path.join(os.tmpdir(), configfile), cb);
  33. return cb(null, fd);
  34. });
  35. })
  36. }
  37. function tryOpenConfig (configpath, cb) {
  38. try {
  39. // if the config file is valid, it should be json and therefore
  40. // node should be able to require it directly. if this doesn't
  41. // throw, we're done!
  42. var content = require(configpath);
  43. process.nextTick(function () {
  44. cb(null, content);
  45. });
  46. } catch (e) {
  47. // if requiring the config file failed, maybe it doesn't exist, or
  48. // perhaps it has become corrupted. instead of calling back with the
  49. // content of the file, call back with a file descriptor that we can
  50. // write the cached data to
  51. fs.open(configpath, 'w+', function (err, fd) {
  52. if (err) {
  53. return cb(err);
  54. }
  55. return cb(null, fd);
  56. });
  57. }
  58. }
  59. // Node <= 9 outputs _ in flags with multiple words, while node 10
  60. // uses -. Both ways are accepted anyway, so always use `_` for better
  61. // compatibility.
  62. // We must not replace the first two --.
  63. function normalizeFlagName(flag) {
  64. return "--" + flag.slice(4).replace(/-/g, "_");
  65. }
  66. // i can't wait for the day this whole module is obsolete because these
  67. // options are available on the process object. this executes node with
  68. // `--v8-options` and parses the result, returning an array of command
  69. // line flags.
  70. function getFlags (cb) {
  71. execFile(process.execPath, ['--v8-options'], function (execErr, result) {
  72. if (execErr) {
  73. return cb(execErr);
  74. }
  75. var flags = result.match(/\s\s--[\w-]+/gm)
  76. .map(normalizeFlagName)
  77. .filter(function (name) {
  78. return exclusions.indexOf(name) === -1;
  79. });
  80. return cb(null, flags);
  81. });
  82. }
  83. // write some json to a file descriptor. if this fails, call back
  84. // with both the error and the data that was meant to be written.
  85. function writeConfig (fd, flags, cb) {
  86. var json = JSON.stringify(flags);
  87. var buf;
  88. if (Buffer.from && Buffer.from !== Uint8Array.from) {
  89. // Node.js 4.5.0 or newer
  90. buf = Buffer.from(json);
  91. } else {
  92. // Old Node.js versions
  93. // The typeof safeguard below is mostly against accidental copy-pasting
  94. // and code rewrite, it never happens as json is always a string here.
  95. if (typeof json === 'number') throw new Error('Unexpected type number');
  96. buf = new Buffer(json);
  97. }
  98. return fs.write(fd, buf, 0, buf.length, 0 , function (writeErr) {
  99. fs.close(fd, function (closeErr) {
  100. var err = writeErr || closeErr;
  101. if (err) {
  102. return cb(fail(err), flags);
  103. }
  104. return cb(null, flags);
  105. });
  106. });
  107. }
  108. module.exports = function (cb) {
  109. // bail early if this is not node
  110. var isElectron = process.versions && process.versions.electron;
  111. if (isElectron) {
  112. return process.nextTick(function () {
  113. cb(null, []);
  114. });
  115. }
  116. // attempt to open/read cache file
  117. openConfig(function (openErr, result) {
  118. if (!openErr && typeof result !== 'number') {
  119. return cb(null, result);
  120. }
  121. // if the result is not an array, we need to go fetch
  122. // the flags by invoking node with `--v8-options`
  123. getFlags(function (flagsErr, flags) {
  124. // if there was an error fetching the flags, bail immediately
  125. if (flagsErr) {
  126. return cb(flagsErr);
  127. }
  128. // if there was a problem opening the config file for writing
  129. // throw an error but include the flags anyway so that users
  130. // can continue to execute (at the expense of having to fetch
  131. // flags on every run until they fix the underyling problem).
  132. if (openErr) {
  133. return cb(fail(openErr), flags);
  134. }
  135. // write the config file to disk so subsequent runs can read
  136. // flags out of a cache file.
  137. return writeConfig(result, flags, cb);
  138. });
  139. });
  140. };
  141. module.exports.configfile = configfile;
  142. module.exports.configPath = configPath;