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