node.js
This file
Path
lib/methods/node.js
91 lines · 3.1 KiB · JavaScript
Last changed by
Fix `execaNode()` and the `node` option missing IPC in their typesSindre Sorhusbefore adoption2633abb
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
line 1why, one step back1import {execPath, execArgv} from 'node:process';Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0
line 2why, one step back2import path from 'node:path';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 3–10why, one step back3import {safeNormalizeFileUrl} from '../arguments/file-url.js';45// `execaNode()` is a shortcut for `execa(..., {node: true})`6export const mapNode = ({options}) => {7 if (options.node === false) {8 throw new TypeError('The "node" option cannot be false with `execaNode()`.');9 }10Harden some thingsSindre Sorhusbefore adoption4a0a121
line 11why, one step back11 return {options: {__proto__: null, ...options, node: true}};100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 12–13why, one step back12};13Fix template strings, `parseCommandString()` and `execaNode()` edge casesSindre Sorhusbefore adoptionfb92da1
lines 14–36why, one step back14// The subprocess must not reuse the current process' `--inspect` port.15// It must not run the current process' `node --eval` or `node --print` code either, instead of its file. Like `child_process.fork()`, this prevents fork bombs.16const getDefaultNodeOptions = () => {17 const nodeOptions = [];18 for (let index = 0; index < execArgv.length; index += 1) {19 const nodeOption = execArgv[index];20 if (REMOVED_OPTIONS_WITH_VALUE.has(nodeOption)) {21 // `-p` and `--print` do not take a value when followed by another flag, e.g. `node -p -e code`. With the other options, Node.js fails when their value starts with `-`.22 if (!execArgv[index + 1]?.startsWith('-')) {23 index += 1;24 }25 } else if (!IGNORED_OPTIONS_REGEXP.test(nodeOption)) {26 nodeOptions.push(nodeOption);27 }28 }2930 return nodeOptions;31};3233// Those options are followed by a separate value, which is removed too34const REMOVED_OPTIONS_WITH_VALUE = new Set(['-e', '--eval', '-p', '--print', '-pe', '--input-type', '--inspect-port']);35const IGNORED_OPTIONS_REGEXP = /^--(?:inspect|eval=|input-type=)/;36100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 37–42why, one step back37// Applies the `node: true` option, and the related `nodePath`/`nodeOptions` options.38// Modifies the file commands/arguments to ensure the same Node binary and flags are re-used.39// Also adds `ipc: true` and `shell: false`.40export const handleNodeOption = (file, commandArguments, {41 node: shouldHandleNode = false,42 nodePath = execPath,Fix template strings, `parseCommandString()` and `execaNode()` edge casesSindre Sorhusbefore adoptionfb92da1
line 43why, one step back43 nodeOptions = getDefaultNodeOptions(),100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 44–51why, one step back44 cwd,45 execPath: formerNodePath,46 ...options47}) => {48 if (formerNodePath !== undefined) {49 throw new TypeError('The "execPath" option has been removed. Please use the "nodePath" option instead.');50 }51Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
lines 52–55why, one step back52 if (!Array.isArray(nodeOptions) || nodeOptions.some(nodeOption => typeof nodeOption !== 'string')) {53 throw new TypeError('The "nodeOptions" option must be an array of strings.');54 }55100% test coverage (#1018)ehmickybefore adoption9f65b86
line 56why, one step back56 const normalizedNodePath = safeNormalizeFileUrl(nodePath, 'The "nodePath" option');Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0
line 57why, one step back57 const resolvedNodePath = path.resolve(cwd, normalizedNodePath);Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
line 58why, one step back58 // Use spread (which only copies own properties) to safely get shell without reading polluted prototype100% test coverage (#1018)ehmickybefore adoption9f65b86
line 59why, one step back59 const newOptions = {Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
lines 60–61why, one step back60 __proto__: null,61 shell: false,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 62–71why, one step back62 ...options,63 nodePath: resolvedNodePath,64 node: shouldHandleNode,65 cwd,66 };6768 if (!shouldHandleNode) {69 return [file, commandArguments, newOptions];70 }71Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0
line 72why, one step back72 if (path.basename(file, '.exe') === 'node') {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 73–77why, one step back73 throw new TypeError('When the "node" option is true, the first argument does not need to be "node".');74 }7576 return [77 resolvedNodePath,Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
lines 78–85why, one step back78 [79 ...nodeOptions,80 file,81 ...commandArguments,82 ],83 {84 __proto__: null,85 ...newOptions,Fix `execaNode()` and the `node` option missing IPC in their typesSindre Sorhusbefore adoption2633abb
lines 86–87why, one step back86 // Like other options, `undefined` uses the default value87 ipc: newOptions.ipc === undefined ? true : newOptions.ipc,Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
lines 88–89why, one step back88 shell: false,89 },100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 90–91why, one step back90 ];91};