index.js 4.3 KB

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