1'use strict'; 2 3const common = require('../common'); 4 5const assert = require('assert'); 6const fs = require('fs'); 7const path = require('path'); 8 9const rootDir = path.resolve(__dirname, '..', '..'); 10const cliMd = path.join(rootDir, 'doc', 'api', 'cli.md'); 11const cliText = fs.readFileSync(cliMd, { encoding: 'utf8' }); 12 13const parseSection = (text, startMarker, endMarker) => { 14 const regExp = new RegExp(`${startMarker}\r?\n([^]*)\r?\n${endMarker}`); 15 const match = text.match(regExp); 16 assert(match, 17 `Unable to locate text between '${startMarker}' and '${endMarker}'.`); 18 return match[1].split(/\r?\n/); 19}; 20 21const nodeOptionsLines = parseSection(cliText, 22 '<!-- node-options-node start -->', 23 '<!-- node-options-node end -->'); 24const v8OptionsLines = parseSection(cliText, 25 '<!-- node-options-v8 start -->', 26 '<!-- node-options-v8 end -->'); 27// Check the options are documented in alphabetical order. 28assert.deepStrictEqual(nodeOptionsLines, [...nodeOptionsLines].sort()); 29assert.deepStrictEqual(v8OptionsLines, [...v8OptionsLines].sort()); 30 31const documented = new Set(); 32for (const line of [...nodeOptionsLines, ...v8OptionsLines]) { 33 for (const match of line.matchAll(/`(-[^`]+)`/g)) { 34 const option = match[1]; 35 assert(!documented.has(option), 36 `Option '${option}' was documented more than once as an ` + 37 `allowed option for NODE_OPTIONS in ${cliMd}.`); 38 documented.add(option); 39 } 40} 41 42// Filter out options that are conditionally present. 43const conditionalOpts = [ 44 { include: common.hasCrypto, 45 filter: (opt) => { 46 return ['--openssl-config', '--tls-cipher-list', '--use-bundled-ca', 47 '--use-openssl-ca' ].includes(opt); 48 } }, 49 { 50 // We are using openssl_is_fips from the configuration because it could be 51 // the case that OpenSSL is FIPS compatible but fips has not been enabled 52 // (starting node with --enable-fips). If we use common.hasFipsCrypto 53 // that would only tells us if fips has been enabled, but in this case we 54 // want to check options which will be available regardless of whether fips 55 // is enabled at runtime or not. 56 include: process.config.variables.openssl_is_fips, 57 filter: (opt) => opt.includes('-fips') }, 58 { include: common.hasIntl, 59 filter: (opt) => opt === '--icu-data-dir' }, 60 { include: process.features.inspector, 61 filter: (opt) => opt.startsWith('--inspect') || opt === '--debug-port' }, 62]; 63documented.forEach((opt) => { 64 conditionalOpts.forEach(({ include, filter }) => { 65 if (!include && filter(opt)) { 66 documented.delete(opt); 67 } 68 }); 69}); 70 71const difference = (setA, setB) => { 72 return new Set([...setA].filter((x) => !setB.has(x))); 73}; 74 75const overdocumented = difference(documented, 76 process.allowedNodeEnvironmentFlags); 77assert.strictEqual(overdocumented.size, 0, 78 'The following options are documented as allowed in ' + 79 `NODE_OPTIONS in ${cliMd}: ` + 80 `${[...overdocumented].join(' ')} ` + 81 'but are not in process.allowedNodeEnvironmentFlags'); 82const undocumented = difference(process.allowedNodeEnvironmentFlags, 83 documented); 84// Remove intentionally undocumented options. 85assert(undocumented.delete('--debug-arraybuffer-allocations')); 86assert(undocumented.delete('--es-module-specifier-resolution')); 87assert(undocumented.delete('--experimental-report')); 88assert(undocumented.delete('--experimental-worker')); 89assert(undocumented.delete('--no-node-snapshot')); 90assert(undocumented.delete('--loader')); 91 92assert.strictEqual(undocumented.size, 0, 93 'The following options are not documented as allowed in ' + 94 `NODE_OPTIONS in ${cliMd}: ${[...undocumented].join(' ')}`); 95