Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 1why, one step back
1import {execPath, execArgv} from 'node:process';

Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0

line 2why, one step back
2import path from 'node:path';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 3–10why, one step back
3import {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 }10

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 11why, one step back
11 return {options: {__proto__: null, ...options, node: true}};

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 12–13why, one step back
12};
13

Fix template strings, `parseCommandString()` and `execaNode()` edge casesSindre Sorhusbefore adoptionfb92da1

lines 14–36why, one step back
14// 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=)/;36

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 37–42why, one step back
37// 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 back
43 nodeOptions = getDefaultNodeOptions(),

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 44–51why, one step back
44 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 }51

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

lines 52–55why, one step back
52 if (!Array.isArray(nodeOptions) || nodeOptions.some(nodeOption => typeof nodeOption !== 'string')) {
53 throw new TypeError('The "nodeOptions" option must be an array of strings.');54 }55

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 56why, one step back
56 const normalizedNodePath = safeNormalizeFileUrl(nodePath, 'The "nodePath" option');

Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0

line 57why, one step back
57 const resolvedNodePath = path.resolve(cwd, normalizedNodePath);

Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84

line 58why, one step back
58 // Use spread (which only copies own properties) to safely get shell without reading polluted prototype

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 59why, one step back
59 const newOptions = {

Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84

lines 60–61why, one step back
60 __proto__: null,
61 shell: false,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 62–71why, one step back
62 ...options,
63 nodePath: resolvedNodePath,64 node: shouldHandleNode,65 cwd,66 };6768 if (!shouldHandleNode) {69 return [file, commandArguments, newOptions];70 }71

Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0

line 72why, one step back
72 if (path.basename(file, '.exe') === 'node') {

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 73–77why, one step back
73 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 back
78 [
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 back
86 // Like other options, `undefined` uses the default value
87 ipc: newOptions.ipc === undefined ? true : newOptions.ipc,

Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84

lines 88–89why, one step back
88 shell: false,
89 },

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 90–91why, one step back
90 ];
91};