Skip to content
Myr

node.js

This file

Path

lib/methods/node.js

86 lines · 2.8 KiB · JavaScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 63de206 point at it.

Improve examples used in types (#1000)ehmickybefore adoption63de206

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';

Improve examples used in types (#1000)ehmickybefore adoption63de206

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}};

Improve examples used in types (#1000)ehmickybefore adoption63de206

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

Improve examples used in types (#1000)ehmickybefore adoption63de206

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(),

Improve examples used in types (#1000)ehmickybefore adoption63de206

lines 44–52why, 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 }5152 const normalizedNodePath = safeNormalizeFileUrl(nodePath, 'The "nodePath" option');

Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0

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

Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84

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

Improve examples used in types (#1000)ehmickybefore adoption63de206

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

Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84

lines 56–57why, one step back
56 __proto__: null,
57 shell: false,

Improve examples used in types (#1000)ehmickybefore adoption63de206

lines 58–67why, one step back
58 ...options,
59 nodePath: resolvedNodePath,60 node: shouldHandleNode,61 cwd,62 };6364 if (!shouldHandleNode) {65 return [file, commandArguments, newOptions];66 }67

Upgrade dependencies (#1138)ehmickybefore adoptionabd9fd0

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

Improve examples used in types (#1000)ehmickybefore adoption63de206

lines 69–73why, one step back
69 throw new TypeError('When the "node" option is true, the first argument does not need to be "node".');
70 }7172 return [73 resolvedNodePath,

Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84

lines 74–84why, one step back
74 [
75 ...nodeOptions,76 file,77 ...commandArguments,78 ],79 {80 __proto__: null,81 ipc: true,82 ...newOptions,83 shell: false,84 },

Improve examples used in types (#1000)ehmickybefore adoption63de206

lines 85–86why, one step back
85 ];
86};