index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 execFile = require('child_process').execFile;
  8. const env = process.env;
  9. const user = env.LOGNAME || env.USER || env.LNAME || env.USERNAME;
  10. const configfile = '.v8flags.'+process.versions.v8+'.'+user+'.json';
  11. const exclusions = ['--help'];
  12. const failureMessage = [
  13. 'Unable to cache a config file for v8flags to a your home directory',
  14. 'or a temporary folder. To fix this problem, please correct your',
  15. 'environment by setting HOME=/path/to/home or TEMP=/path/to/temp.',
  16. 'NOTE: the user running this must be able to access provided path.',
  17. 'If all else fails, please open an issue here:',
  18. 'http://github.com/tkellen/js-v8flags'
  19. ].join('\n');
  20. function fail (err) {
  21. err.message += '\n\n' + failureMessage;
  22. return err;
  23. }
  24. function openConfig (cb) {
  25. var userHome = require('user-home');
  26. if (!userHome) {
  27. return tryOpenConfig(path.join(os.tmpdir(), configfile), cb);
  28. }
  29. tryOpenConfig(path.join(userHome, configfile), function (err, fd) {
  30. if (err) return tryOpenConfig(path.join(os.tmpdir(), configfile), cb);
  31. return cb(null, fd);
  32. });
  33. }
  34. function tryOpenConfig (configpath, cb) {
  35. try {
  36. // if the config file is valid, it should be json and therefore
  37. // node should be able to require it directly. if this doesn't
  38. // throw, we're done!
  39. content = require(configpath);
  40. process.nextTick(function () {
  41. cb(null, content);
  42. });
  43. } catch (e) {
  44. // if requiring the config file failed, maybe it doesn't exist, or
  45. // perhaps it has become corrupted. instead of calling back with the
  46. // content of the file, call back with a file descriptor that we can
  47. // write the cached data to
  48. fs.open(configpath, 'w+', function (err, fd) {
  49. if (err) {
  50. return cb(err);
  51. }
  52. return cb(null, fd);
  53. });
  54. }
  55. }
  56. // i can't wait for the day this whole module is obsolete because these
  57. // options are available on the process object. this executes node with
  58. // `--v8-options` and parses the result, returning an array of command
  59. // line flags.
  60. function getFlags (cb) {
  61. execFile(process.execPath, ['--v8-options'], function (execErr, result) {
  62. if (execErr) {
  63. return cb(execErr);
  64. }
  65. var flags = result.match(/\s\s--(\w+)/gm).map(function (match) {
  66. return match.substring(2);
  67. }).filter(function (name) {
  68. return exclusions.indexOf(name) === -1;
  69. });
  70. return cb(null, flags);
  71. });
  72. }
  73. // write some json to a file descriptor. if this fails, call back
  74. // with both the error and the data that was meant to be written.
  75. function writeConfig (fd, flags, cb) {
  76. var buf = new Buffer(JSON.stringify(flags));
  77. return fs.write(fd, buf, 0, buf.length, 0 , function (writeErr) {
  78. fs.close(fd, function (closeErr) {
  79. var err = writeErr || closeErr;
  80. if (err) {
  81. return cb(fail(err), flags);
  82. }
  83. return cb(null, flags);
  84. });
  85. });
  86. }
  87. module.exports = function (cb) {
  88. // bail early if this is not node
  89. var isElectron = process.versions && process.versions.electron;
  90. if (isElectron) {
  91. return process.nextTick(function () {
  92. cb(null, []);
  93. });
  94. }
  95. // attempt to open/read cache file
  96. openConfig(function (openErr, result) {
  97. if (!openErr && typeof result !== 'number') {
  98. return cb(null, result);
  99. }
  100. // if the result is not an array, we need to go fetch
  101. // the flags by invoking node with `--v8-options`
  102. getFlags(function (flagsErr, flags) {
  103. // if there was an error fetching the flags, bail immediately
  104. if (flagsErr) {
  105. return cb(flagsErr);
  106. }
  107. // if there was a problem opening the config file for writing
  108. // throw an error but include the flags anyway so that users
  109. // can continue to execute (at the expense of having to fetch
  110. // flags on every run until they fix the underyling problem).
  111. if (openErr) {
  112. return cb(fail(openErr), flags);
  113. }
  114. // write the config file to disk so subsequent runs can read
  115. // flags out of a cache file.
  116. return writeConfig(result, flags, cb);
  117. });
  118. });
  119. };
  120. module.exports.configfile = configfile;