@songtonyli/dsh-cli 0.1.65 → 0.1.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/README.md +1 -1
  2. package/bin/dsh.mjs +1 -1
  3. package/node_modules/@babel/runtime/helpers/esm/interopRequireWildcard.js +16 -19
  4. package/node_modules/@babel/runtime/helpers/esm/toPrimitive.js +5 -5
  5. package/node_modules/@babel/runtime/helpers/interopRequireWildcard.js +16 -19
  6. package/node_modules/@babel/runtime/helpers/toPrimitive.js +5 -5
  7. package/node_modules/@babel/runtime/package.json +1 -1
  8. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/package.json +2 -2
  9. package/node_modules/@deepseek-ai/dsh-client-ui-sidebar/lib/client.js +1 -1
  10. package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/build/esm/version.js +1 -1
  11. package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/build/esnext/version.js +1 -1
  12. package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/build/src/version.js +1 -1
  13. package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/node_modules/@opentelemetry/core/build/esm/version.js +1 -1
  14. package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/node_modules/@opentelemetry/core/build/esnext/version.js +1 -1
  15. package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/node_modules/@opentelemetry/core/build/src/version.js +1 -1
  16. package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/node_modules/@opentelemetry/core/package.json +2 -2
  17. package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/package.json +3 -3
  18. package/node_modules/@deepseek-ai/dsh-tui-app/README.i18n.yaml +6 -6
  19. package/node_modules/@deepseek-ai/dsh-tui-app/README.md +4 -2
  20. package/node_modules/@deepseek-ai/dsh-tui-app/README.zh.md +4 -2
  21. package/node_modules/@deepseek-ai/dsh-tui-app/lib/index.js +1367 -18
  22. package/node_modules/@deepseek-ai/dsh-tui-app/package.json +2 -0
  23. package/node_modules/execa/lib/arguments/command-file.js +55 -23
  24. package/node_modules/execa/lib/arguments/command.js +2 -1
  25. package/node_modules/execa/lib/arguments/cwd.js +10 -6
  26. package/node_modules/execa/lib/arguments/encoding-option.js +11 -0
  27. package/node_modules/execa/lib/arguments/escape.js +2 -2
  28. package/node_modules/execa/lib/arguments/fd-options.js +3 -7
  29. package/node_modules/execa/lib/arguments/options.js +13 -3
  30. package/node_modules/execa/lib/arguments/specific.js +2 -1
  31. package/node_modules/execa/lib/convert/duplex.js +3 -1
  32. package/node_modules/execa/lib/convert/iterable.js +3 -5
  33. package/node_modules/execa/lib/convert/readable.js +3 -1
  34. package/node_modules/execa/lib/convert/writable.js +8 -4
  35. package/node_modules/execa/lib/io/contents.js +8 -11
  36. package/node_modules/execa/lib/io/input-sync.js +8 -5
  37. package/node_modules/execa/lib/io/max-buffer.js +41 -28
  38. package/node_modules/execa/lib/io/output-sync.js +64 -40
  39. package/node_modules/execa/lib/io/strip-newline.js +4 -3
  40. package/node_modules/execa/lib/ipc/buffer-messages.js +12 -2
  41. package/node_modules/execa/lib/ipc/get-each.js +8 -2
  42. package/node_modules/execa/lib/ipc/get-one.js +3 -1
  43. package/node_modules/execa/lib/ipc/graceful.js +3 -2
  44. package/node_modules/execa/lib/ipc/incoming.js +10 -3
  45. package/node_modules/execa/lib/ipc/ipc-input.js +7 -2
  46. package/node_modules/execa/lib/ipc/send.js +3 -1
  47. package/node_modules/execa/lib/ipc/strict.js +13 -7
  48. package/node_modules/execa/lib/ipc/validation.js +1 -1
  49. package/node_modules/execa/lib/methods/bind.js +2 -8
  50. package/node_modules/execa/lib/methods/create.js +3 -1
  51. package/node_modules/execa/lib/methods/main-async.js +21 -11
  52. package/node_modules/execa/lib/methods/main-sync.js +7 -5
  53. package/node_modules/execa/lib/methods/node.js +1 -1
  54. package/node_modules/execa/lib/methods/script.js +1 -1
  55. package/node_modules/execa/lib/methods/template.js +24 -5
  56. package/node_modules/execa/lib/pipe/pipe-arguments.js +36 -5
  57. package/node_modules/execa/lib/pipe/throw.js +5 -3
  58. package/node_modules/execa/lib/resolve/all-async.js +43 -19
  59. package/node_modules/execa/lib/resolve/all-sync.js +27 -15
  60. package/node_modules/execa/lib/resolve/exit-sync.js +13 -5
  61. package/node_modules/execa/lib/resolve/stdio.js +2 -1
  62. package/node_modules/execa/lib/resolve/wait-stream.js +12 -2
  63. package/node_modules/execa/lib/resolve/wait-subprocess.js +3 -1
  64. package/node_modules/execa/lib/return/early-error.js +58 -21
  65. package/node_modules/execa/lib/return/message.js +2 -10
  66. package/node_modules/execa/lib/return/reject.js +1 -1
  67. package/node_modules/execa/lib/stdio/direction.js +36 -5
  68. package/node_modules/execa/lib/stdio/duplicate.js +26 -16
  69. package/node_modules/execa/lib/stdio/handle-async.js +6 -0
  70. package/node_modules/execa/lib/stdio/handle-sync.js +5 -3
  71. package/node_modules/execa/lib/stdio/handle.js +47 -24
  72. package/node_modules/execa/lib/stdio/native.js +3 -6
  73. package/node_modules/execa/lib/stdio/stdio-option.js +13 -18
  74. package/node_modules/execa/lib/stdio/type.js +2 -10
  75. package/node_modules/execa/lib/terminate/cancel.js +7 -0
  76. package/node_modules/execa/lib/terminate/kill-descendants.js +20 -4
  77. package/node_modules/execa/lib/transform/encoding-transform.js +15 -0
  78. package/node_modules/execa/lib/transform/generator.js +13 -2
  79. package/node_modules/execa/lib/transform/split.js +37 -23
  80. package/node_modules/execa/lib/utils/options.js +9 -0
  81. package/node_modules/execa/lib/utils/standard-stream.js +6 -0
  82. package/node_modules/execa/lib/utils/uint-array.js +1 -7
  83. package/node_modules/execa/lib/verbose/log.js +3 -6
  84. package/node_modules/execa/lib/verbose/output.js +3 -1
  85. package/node_modules/execa/package.json +51 -48
  86. package/package.json +3 -3
@@ -93,6 +93,7 @@
93
93
  "@deepseek-ai/dsh-subagent": "0.2.1-alpha.1",
94
94
  "@deepseek-ai/dsh-token-meter": "0.2.1-alpha.1",
95
95
  "@deepseek-ai/dsh-tool-todo": "0.2.1-alpha.1",
96
+ "@deepseek-ai/dsh-tool-workflow": "0.2.1-alpha.1",
96
97
  "@deepseek-ai/dsh-tools": "0.2.1-alpha.1",
97
98
  "@deepseek-ai/dsh-user-approval": "0.2.1-alpha.1",
98
99
  "@deepseek-ai/dsh-user-questions": "0.2.1-alpha.1"
@@ -127,6 +128,7 @@
127
128
  "@deepseek-ai/dsh-token-meter": "0.2.1-alpha.1",
128
129
  "@deepseek-ai/dsh-tool-jobs": "0.2.1-alpha.1",
129
130
  "@deepseek-ai/dsh-tool-todo": "0.2.1-alpha.1",
131
+ "@deepseek-ai/dsh-tool-workflow": "0.2.1-alpha.1",
130
132
  "@deepseek-ai/dsh-tools": "0.2.1-alpha.1",
131
133
  "@deepseek-ai/dsh-user-approval": "0.2.1-alpha.1",
132
134
  "@deepseek-ai/dsh-user-questions": "0.2.1-alpha.1"
@@ -12,19 +12,12 @@ export const parseCommandFile = (file, commandArguments, options) => {
12
12
  const parsed = {file, commandArguments: [...commandArguments], options};
13
13
 
14
14
  // Under a shell, or on Unix, the OS resolves the file and escapes the arguments itself
15
- if (options.shell || process.platform !== 'win32') {
16
- return parsed;
17
- }
18
-
19
- return escapeWindowsCommand(parsed);
15
+ return options.shell || process.platform !== 'win32' ? parsed : escapeWindowsCommand(parsed);
20
16
  };
21
17
 
22
18
  // Only `.exe` and `.com` files can be spawned directly; anything else needs `cmd.exe`
23
19
  const directlyExecutableRegExp = /\.(?:com|exe)$/i;
24
20
 
25
- // `.cmd` and `.bat` files re-expand their own arguments via `%*`/`%1`, so metacharacters must survive being interpreted by `cmd.exe` twice: once when the batch file is invoked and once when it forwards the arguments
26
- const batchFileRegExp = /\.(?:bat|cmd)$/i;
27
-
28
21
  const escapeWindowsCommand = parsed => {
29
22
  // Resolve the file to an absolute path, following its shebang to the interpreter if any
30
23
  const resolvedFile = resolveWithShebang(parsed);
@@ -47,17 +40,21 @@ const escapeWindowsCommand = parsed => {
47
40
  assertNoLineBreak(value);
48
41
  }
49
42
 
50
- const isDoubleEscape = resolvedFile !== undefined && batchFileRegExp.test(resolvedFile);
51
- // POSIX separators must become Windows ones (`foo/bar` -> `foo\bar`), otherwise resolution always fails with ENOENT
43
+ /*
44
+ POSIX separators must become Windows ones (`foo/bar` -> `foo\bar`), otherwise resolution always fails with ENOENT.
45
+
46
+ Unlike Rust, which quotes the batch file's path, the file is caret-escaped. `cmd.exe` only interprets it once, and the file might not be resolved, for example when it is a `cmd.exe` builtin, which would not run if quoted.
47
+ */
52
48
  const escapedFile = escapeMetaChars(path.normalize(resolvedFile ?? parsed.file));
53
- const escapedArguments = parsed.commandArguments.map(argument => escapeArgument(argument, isDoubleEscape));
49
+ const escapedArguments = parsed.commandArguments.map(argument => escapeArgument(argument));
54
50
  const commandLine = `"${[escapedFile, ...escapedArguments].join(' ')}"`;
55
51
 
56
52
  // Let `node:child_process` pass the already-escaped command line through untouched
57
53
  parsed.options.windowsVerbatimArguments = true;
58
54
  return {
59
55
  file: process.env.comspec || 'cmd.exe',
60
- commandArguments: ['/d', '/s', '/c', commandLine],
56
+ // `/e:on` enables the command extensions the `%` escaping relies on, and `/v:off` disables delayed expansion so `!` is literal, even when the registry changes either default
57
+ commandArguments: ['/d', '/e:on', '/v:off', '/s', '/c', commandLine],
61
58
  options: parsed.options,
62
59
  };
63
60
  };
@@ -127,11 +124,12 @@ const SHEBANG_BYTE_LENGTH = 150;
127
124
  // Read the file's first bytes to find its shebang interpreter, if it has one
128
125
  const readShebang = file => {
129
126
  const buffer = Buffer.alloc(SHEBANG_BYTE_LENGTH);
127
+ let bytesRead = 0;
130
128
 
131
129
  try {
132
130
  const fileDescriptor = openSync(file, 'r');
133
131
  try {
134
- readSync(fileDescriptor, buffer, 0, SHEBANG_BYTE_LENGTH, 0);
132
+ bytesRead = readSync(fileDescriptor, buffer, 0, SHEBANG_BYTE_LENGTH, 0);
135
133
  } finally {
136
134
  closeSync(fileDescriptor);
137
135
  }
@@ -139,7 +137,9 @@ const readShebang = file => {
139
137
  return undefined;
140
138
  }
141
139
 
142
- return parseShebang(buffer.toString());
140
+ // A file shorter than the buffer leaves it zero-filled, so only the bytes actually read must be parsed.
141
+ // Otherwise, a shebang line which is not terminated by a newline would include those `\0` characters.
142
+ return parseShebang(buffer.toString('utf8', 0, bytesRead));
143
143
  };
144
144
 
145
145
  const shebangRegExp = /^#!(?<line>.*)/;
@@ -153,7 +153,7 @@ const parseShebang = contents => {
153
153
  return undefined;
154
154
  }
155
155
 
156
- const [interpreterPath, argument] = shebangLine.split(' ');
156
+ const [interpreterPath, argument] = shebangLine.split(' ', 2);
157
157
  const interpreter = interpreterPath.split('/').at(-1);
158
158
  if (interpreter === 'env') {
159
159
  return argument;
@@ -174,28 +174,60 @@ const assertNoLineBreak = value => {
174
174
  // eslint-disable-next-line regexp/sort-character-class-elements
175
175
  const metaCharsRegExp = /[()\][%!^"`<>&|;, *?]/g;
176
176
 
177
- // Prefix every `cmd.exe` metacharacter with a caret to neutralize it
177
+ // Prefix every `cmd.exe` metacharacter with a caret to neutralize it. This is only used for the file, since arguments are quoted instead.
178
178
  const escapeMetaChars = value => value.replaceAll(metaCharsRegExp, '^$&');
179
179
 
180
180
  const backslashRunRegExp = /\\+/g;
181
181
 
182
- const escapeArgument = (rawArgument, doubleEscape) => {
182
+ // Matches any character requiring quoting: anything except ASCII letters, digits, a few punctuation characters that `cmd.exe` never interprets, and non-ASCII characters other than control characters
183
+ const quotedCharacterRegExp = /[^\w#$*+\-./:?@\\\u{80}-\u{10FFFF}]|\p{Control}/u;
184
+
185
+ /*
186
+ Escape an argument the same way as Rust's `std::process::Command` does for batch files, which fixed the "BatBadBut" vulnerability (CVE-2024-24576): https://github.com/rust-lang/rust/blob/main/library/std/src/sys/args/windows.rs (`append_bat_arg()`). yt-dlp uses the same algorithm.
187
+
188
+ Any argument containing a `cmd.exe` metacharacter is wrapped in double quotes, which keeps `cmd.exe` from interpreting it. `cmd.exe` might interpret it more than once: when invoking a batch file, then again when that batch file forwards it with `%*`, like npm cmd-shims do. Caret escaping depends on that count, so it breaks batch files reading their arguments directly with `%~1`, like Maven's `mvn.cmd`. Quoting does not, as long as the quotes stay balanced.
189
+
190
+ Rust only uses this for batch files. We use it for any file run through `cmd.exe`, since it is also correct when `cmd.exe` interprets the argument only once.
191
+
192
+ Limitations, which no escaping can fix since they depend on how the batch file uses its arguments:
193
+ - A batch file reading `%~1` gets the escaped argument without its surrounding quotes: double quotes are doubled, and trailing backslashes too.
194
+ - A batch file using `%~1` outside double quotes, or `%1` inside double quotes (like `if "%1" == ""`), lets `cmd.exe` interpret its metacharacters. Since `%1` includes the argument's surrounding quotes, the extra ones unquote it.
195
+ - A batch file forwarding its arguments with `call` expands `%` again, and one enabling delayed expansion expands `!` again.
196
+ */
197
+ const escapeArgument = rawArgument => {
183
198
  /*
184
- Escape backslashes and double quotes for `cmd.exe`, following the algorithm at https://web.archive.org/web/20240930203505/https://qntm.org/cmd.
199
+ Escape backslashes, following the algorithm at https://web.archive.org/web/20240930203505/https://qntm.org/cmd.
185
200
 
186
201
  A run of backslashes only needs doubling when it precedes a double quote, or the end of the argument since that becomes a double quote once the argument is wrapped below. Otherwise the backslashes would be taken as escaping that quote. Every double quote is then escaped in turn.
187
202
 
188
203
  Each backslash run is matched exactly once and consumed, so a long run cannot trigger the quadratic backtracking a naive pattern would, which would be a denial-of-service risk.
204
+
205
+ Double quotes are escaped as `""` instead of `\"`. Both are a literal double quote inside a quoted argument for the subprocess, but only `""` keeps the quotes balanced for `cmd.exe`, which does not know about backslashes. Otherwise, the metacharacters after the double quote would not be quoted anymore, allowing command injection.
206
+
207
+ `%` cannot be escaped inside double quotes, so it is replaced by `%%cd:~,%` instead. That is a literal `%` followed by an empty substring of the `%cd%` variable, which `cmd.exe` expands to nothing. This prevents `%VAR%` from being expanded.
189
208
  */
190
- const argument = `${rawArgument}`
209
+ const argument = `${rawArgument}`;
210
+ if (!shouldQuoteArgument(argument)) {
211
+ return argument;
212
+ }
213
+
214
+ const escapedArgument = argument
191
215
  .replaceAll(backslashRunRegExp, (backslashes, offset, string) => {
192
216
  const nextCharacter = string[offset + backslashes.length];
193
217
  const isPrecedesDoubleQuote = nextCharacter === '"' || nextCharacter === undefined;
194
218
  return isPrecedesDoubleQuote ? backslashes.repeat(2) : backslashes;
195
219
  })
196
- .replaceAll('"', '\\"');
220
+ .replaceAll('"', '""')
221
+ .replaceAll('%', '%%cd:~,%');
197
222
 
198
- // Wrap the whole argument in double quotes, then caret-escape the metacharacters, a second time when targeting a cmd-shim
199
- const escapedArgument = escapeMetaChars(`"${argument}"`);
200
- return doubleEscape ? escapeMetaChars(escapedArgument) : escapedArgument;
223
+ return `"${escapedArgument}"`;
201
224
  };
225
+
226
+ /*
227
+ An empty argument must be quoted, otherwise it would be dropped.
228
+ An argument ending with a backslash must be quoted, otherwise a batch file wrapping it in double quotes, like `"%~1"`, would have that backslash escape its closing double quote.
229
+ Since `"` and `%` require quoting, an unquoted argument never needs any escaping.
230
+ */
231
+ const shouldQuoteArgument = argument => argument === ''
232
+ || argument.endsWith('\\')
233
+ || quotedCharacterRegExp.test(argument);
@@ -9,7 +9,8 @@ export const handleCommand = (filePath, rawArguments, rawOptions) => {
9
9
  const startTime = getStartTime();
10
10
  const {command, escapedCommand} = joinCommand(filePath, rawArguments);
11
11
  const verbose = normalizeFdSpecificOption(rawOptions, 'verbose');
12
- const verboseInfo = getVerboseInfo(verbose, escapedCommand, {...rawOptions});
12
+ // Keep the null prototype, otherwise a polluted `Object.prototype` would inject option values
13
+ const verboseInfo = getVerboseInfo(verbose, escapedCommand, {__proto__: null, ...rawOptions});
13
14
  logCommand(escapedCommand, verboseInfo);
14
15
  return {
15
16
  command,
@@ -18,9 +18,17 @@ const getDefaultCwd = () => {
18
18
  }
19
19
  };
20
20
 
21
+ // `process.cwd()` throws if the current directory has been deleted.
22
+ // This is only used to improve an error message, so it must not throw itself, which would hide that error.
23
+ const getCurrentCwd = () => {
24
+ try {
25
+ return process.cwd();
26
+ } catch {}
27
+ };
28
+
21
29
  // When `cwd` option has an invalid value, provide with a better error message
22
30
  export const fixCwdError = (originalMessage, cwd) => {
23
- if (cwd === getDefaultCwd()) {
31
+ if (cwd === getCurrentCwd()) {
24
32
  return originalMessage;
25
33
  }
26
34
 
@@ -31,9 +39,5 @@ export const fixCwdError = (originalMessage, cwd) => {
31
39
  return `The "cwd" option is invalid: ${cwd}.\n${error.message}\n${originalMessage}`;
32
40
  }
33
41
 
34
- if (!cwdStat.isDirectory()) {
35
- return `The "cwd" option is not a directory: ${cwd}.\n${originalMessage}`;
36
- }
37
-
38
- return originalMessage;
42
+ return cwdStat.isDirectory() ? originalMessage : `The "cwd" option is not a directory: ${cwd}.\n${originalMessage}`;
39
43
  };
@@ -19,6 +19,16 @@ const TEXT_ENCODINGS = new Set(['utf8', 'utf16le']);
19
19
  export const BINARY_ENCODINGS = new Set(['buffer', 'hex', 'base64', 'base64url', 'latin1', 'ascii']);
20
20
  const ENCODINGS = TEXT_ENCODINGS.union(BINARY_ENCODINGS);
21
21
 
22
+ /*
23
+ Encoding to use when converting a transform's string to bytes.
24
+ Text encodings describe how text is represented as bytes, so those must be used, otherwise the string would be decoded with a different encoding than the one it was encoded with, which corrupts it.
25
+ Binary encodings instead describe how bytes are serialized as text, so the string's UTF-8 bytes are used. The same applies with the `binary` transform option, since the transform then deals with raw bytes.
26
+ */
27
+ export const getStringEncoding = (binary, encoding) => binary || !TEXT_ENCODINGS.has(encoding) ? 'utf8' : encoding;
28
+
29
+ // Text encodings only apply to the subprocess' output. Its input is always text encoded with UTF-8, so input transforms decode and encode strings with UTF-8 too, whichever text encoding the `encoding` option uses.
30
+ export const getInputEncoding = encoding => TEXT_ENCODINGS.has(encoding) ? 'utf8' : encoding;
31
+
22
32
  const getCorrectEncoding = encoding => {
23
33
  if (encoding === null) {
24
34
  return 'buffer';
@@ -39,6 +49,7 @@ const getCorrectEncoding = encoding => {
39
49
  };
40
50
 
41
51
  const ENCODING_ALIASES = {
52
+ __proto__: null,
42
53
  // eslint-disable-next-line unicorn/text-encoding-identifier-case
43
54
  'utf-8': 'utf8',
44
55
  'utf-16le': 'utf16le',
@@ -67,8 +67,8 @@ const COMMON_ESCAPES = {
67
67
  '\t': '\\t',
68
68
  };
69
69
 
70
- // Up until that codepoint, \u notation can be used instead of \U
71
- const ASTRAL_START = 65_535;
70
+ // Up until that code point, a character is a single UTF-16 code unit, so the \u notation can be used instead of \U
71
+ export const ASTRAL_START = 0xFF_FF;
72
72
 
73
73
  // Some characters are shell-specific, i.e. need to be escaped when the command is copy-pasted then run.
74
74
  // Escaping is shell-specific. We cannot know which shell is used: `process.platform` detection is not enough.
@@ -58,11 +58,11 @@ const validateFdNumber = (fdNumber, fdName, isWritable, fileDescriptors) => {
58
58
  Please set the "stdio" option to ensure that file descriptor exists.`);
59
59
  }
60
60
 
61
- if (fileDescriptor.direction === 'input' && !isWritable) {
61
+ if (!isWritable && fileDescriptor.direction === 'input') {
62
62
  throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. It must be a readable stream, not writable.`);
63
63
  }
64
64
 
65
- if (fileDescriptor.direction !== 'input' && isWritable) {
65
+ if (isWritable && fileDescriptor.direction !== 'input') {
66
66
  throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. It must be a writable stream, not readable.
67
67
  If you meant to use it as input, please set its "stdio" option to \`{value: 'pipe', input: true}\`.`);
68
68
  }
@@ -89,11 +89,7 @@ const getInvalidStdioOption = (fdNumber, {stdin, stdout, stderr, stdio}) => {
89
89
  return {optionName: 'stdout', optionValue: stdout};
90
90
  }
91
91
 
92
- if (usedDescriptor === 2 && stderr !== undefined) {
93
- return {optionName: 'stderr', optionValue: stderr};
94
- }
95
-
96
- return {optionName: `stdio[${usedDescriptor}]`, optionValue: stdio[usedDescriptor]};
92
+ return usedDescriptor === 2 && stderr !== undefined ? {optionName: 'stderr', optionValue: stderr} : {optionName: `stdio[${usedDescriptor}]`, optionValue: stdio[usedDescriptor]};
97
93
  };
98
94
 
99
95
  const getUsedDescriptor = fdNumber => fdNumber === 'all' ? 1 : fdNumber;
@@ -3,7 +3,7 @@ import process from 'node:process';
3
3
  import {npmRunPathEnv} from 'npm-run-path';
4
4
  import {normalizeForceKillAfterDelay} from '../terminate/kill.js';
5
5
  import {normalizeKillSignal} from '../terminate/signal.js';
6
- import {validateCancelSignal} from '../terminate/cancel.js';
6
+ import {validateCancelSignal, validateRenamedSignalOption} from '../terminate/cancel.js';
7
7
  import {validateGracefulCancel} from '../terminate/graceful.js';
8
8
  import {validateTimeout} from '../terminate/timeout.js';
9
9
  import {handleNodeOption} from '../methods/node.js';
@@ -33,6 +33,7 @@ export const normalizeOptions = (filePath, rawArguments, rawOptions) => {
33
33
  validateEncoding(options);
34
34
  validateIpcInputOption(options);
35
35
  validateCancelSignal(options);
36
+ validateRenamedSignalOption(options);
36
37
  validateGracefulCancel(options);
37
38
  options.shell = normalizeFileUrl(options.shell);
38
39
  options.killSignal = normalizeKillSignal(options.killSignal);
@@ -88,8 +89,16 @@ const addDefaultOptions = ({
88
89
  serialization,
89
90
  });
90
91
 
91
- const getEnv = ({env: envOption, extendEnv, preferLocal, node, localDirectory, nodePath}) => {
92
- const env = extendEnv ? {...process.env, ...envOption} : envOption;
92
+ /*
93
+ Keep a null prototype, otherwise a polluted `Object.prototype` would inject environment variables.
94
+ Unlike the other options, `child_process` intentionally passes the inherited properties of `env` to the subprocess, so they are the ones which must not leak. This includes `PATH`, which decides which file the command resolves to.
95
+ `npmRunPathEnv()` returns a plain object, so this is applied to its result too.
96
+ */
97
+ const getEnv = options => ({__proto__: null, ...computeEnv(options)});
98
+
99
+ const computeEnv = ({env: envOption, extendEnv, preferLocal, node, localDirectory, nodePath}) => {
100
+ // Spread `envOption` even when it is `undefined`, so `extendEnv: false` results in an empty environment instead of inheriting `process.env`
101
+ const env = {__proto__: null, ...(extendEnv && process.env), ...envOption};
93
102
 
94
103
  if (preferLocal || node) {
95
104
  return npmRunPathEnv({
@@ -103,3 +112,4 @@ const getEnv = ({env: envOption, extendEnv, preferLocal, node, localDirectory, n
103
112
 
104
113
  return env;
105
114
  };
115
+
@@ -6,7 +6,8 @@ import {STANDARD_STREAMS_ALIASES} from '../utils/standard-stream.js';
6
6
  // This normalizes those to array of values.
7
7
  // For example, `{verbose: {stdout: 'none', stderr: 'full'}}` becomes `{verbose: ['none', 'none', 'full']}`
8
8
  export const normalizeFdSpecificOptions = options => {
9
- const optionsCopy = {...options};
9
+ // Keep the null prototype, otherwise a polluted `Object.prototype` would inject option values
10
+ const optionsCopy = {__proto__: null, ...options};
10
11
 
11
12
  for (const optionName of FD_SPECIFIC_OPTIONS) {
12
13
  optionsCopy[optionName] = normalizeFdSpecificOption(options, optionName);
@@ -1,6 +1,7 @@
1
1
  import {Duplex} from 'node:stream';
2
2
  import {callbackify} from 'node:util';
3
3
  import {BINARY_ENCODINGS} from '../arguments/encoding-option.js';
4
+ import {copyOptions} from '../utils/options.js';
4
5
  import {
5
6
  getSubprocessStdout,
6
7
  getReadableOptions,
@@ -16,7 +17,8 @@ import {
16
17
  } from './writable.js';
17
18
 
18
19
  // Create a `Duplex` stream combining both `subprocess.readable()` and `subprocess.writable()`
19
- export const createDuplex = ({subprocess, concurrentStreams, encoding}, {from, to, binary: binaryOption = true, preserveNewlines = true} = {}) => {
20
+ export const createDuplex = ({subprocess, concurrentStreams, encoding}, options) => {
21
+ const {from, to, binary: binaryOption = true, preserveNewlines = true} = copyOptions(options);
20
22
  const binary = binaryOption || BINARY_ENCODINGS.has(encoding);
21
23
  const {subprocessStdout, waitReadableDestroy} = getSubprocessStdout(subprocess, from, concurrentStreams);
22
24
  const {subprocessStdin, waitWritableFinal, waitWritableDestroy} = getSubprocessStdin(subprocess, to, concurrentStreams);
@@ -1,13 +1,11 @@
1
1
  import {BINARY_ENCODINGS} from '../arguments/encoding-option.js';
2
2
  import {getFromStream} from '../arguments/fd-options.js';
3
3
  import {iterateOnSubprocessStream} from '../io/iterate.js';
4
+ import {copyOptions} from '../utils/options.js';
4
5
 
5
6
  // Convert the subprocess to an async iterable
6
- export const createIterable = (subprocess, encoding, {
7
- from,
8
- binary: binaryOption = false,
9
- preserveNewlines = false,
10
- } = {}) => {
7
+ export const createIterable = (subprocess, encoding, options) => {
8
+ const {from, binary: binaryOption = false, preserveNewlines = false} = copyOptions(options);
11
9
  const binary = binaryOption || BINARY_ENCODINGS.has(encoding);
12
10
  const subprocessStdout = getFromStream(subprocess, from);
13
11
  const onStdoutData = iterateOnSubprocessStream({
@@ -4,6 +4,7 @@ import {BINARY_ENCODINGS} from '../arguments/encoding-option.js';
4
4
  import {getFromStream} from '../arguments/fd-options.js';
5
5
  import {iterateOnSubprocessStream, DEFAULT_OBJECT_HIGH_WATER_MARK} from '../io/iterate.js';
6
6
  import {createDeferred} from '../utils/deferred.js';
7
+ import {copyOptions} from '../utils/options.js';
7
8
  import {addConcurrentStream, waitForConcurrentStreams} from './concurrent.js';
8
9
  import {
9
10
  safeWaitForSubprocessStdin,
@@ -13,7 +14,8 @@ import {
13
14
  } from './shared.js';
14
15
 
15
16
  // Create a `Readable` stream that forwards from `stdout` and awaits the subprocess
16
- export const createReadable = ({subprocess, concurrentStreams, encoding}, {from, binary: binaryOption = true, preserveNewlines = true} = {}) => {
17
+ export const createReadable = ({subprocess, concurrentStreams, encoding}, options) => {
18
+ const {from, binary: binaryOption = true, preserveNewlines = true} = copyOptions(options);
17
19
  const binary = binaryOption || BINARY_ENCODINGS.has(encoding);
18
20
  const {subprocessStdout, waitReadableDestroy} = getSubprocessStdout(subprocess, from, concurrentStreams);
19
21
  const {readableEncoding, readableObjectMode, readableHighWaterMark} = getReadableOptions(subprocessStdout, binary);
@@ -2,6 +2,7 @@ import {Writable} from 'node:stream';
2
2
  import {callbackify} from 'node:util';
3
3
  import {getToStream} from '../arguments/fd-options.js';
4
4
  import {isStreamAbort} from '../resolve/wait-stream.js';
5
+ import {copyOptions} from '../utils/options.js';
5
6
  import {addConcurrentStream, waitForConcurrentStreams} from './concurrent.js';
6
7
  import {
7
8
  safeWaitForSubprocessStdout,
@@ -11,7 +12,8 @@ import {
11
12
  } from './shared.js';
12
13
 
13
14
  // Create a `Writable` stream that forwards to `stdin` and awaits the subprocess
14
- export const createWritable = ({subprocess, concurrentStreams}, {to} = {}) => {
15
+ export const createWritable = ({subprocess, concurrentStreams}, options) => {
16
+ const {to} = copyOptions(options);
15
17
  const {subprocessStdin, waitWritableFinal, waitWritableDestroy} = getSubprocessStdin(subprocess, to, concurrentStreams);
16
18
  const writable = new Writable({
17
19
  ...getWritableMethods(subprocessStdin, subprocess, waitWritableFinal),
@@ -100,10 +102,12 @@ const shouldUseSubprocessError = error => error === undefined
100
102
  // When `writable` aborts/errors, do the same on `subprocess.stdin`
101
103
  export const onWritableDestroy = async ({subprocessStdin, subprocess, waitWritableFinal, waitWritableDestroy}, error) => {
102
104
  await waitForConcurrentStreams(waitWritableFinal, subprocess);
103
- if (await waitForConcurrentStreams(waitWritableDestroy, subprocess)) {
104
- destroyOtherWritable(subprocessStdin, error);
105
- await waitForSubprocess(subprocess, error);
105
+ if (!await waitForConcurrentStreams(waitWritableDestroy, subprocess)) {
106
+ return;
106
107
  }
108
+
109
+ destroyOtherWritable(subprocessStdin, error);
110
+ await waitForSubprocess(subprocess, error);
107
111
  };
108
112
 
109
113
  const destroyOtherWritable = (stream, error) => {
@@ -7,7 +7,7 @@ import {handleMaxBuffer} from './max-buffer.js';
7
7
  import {getStripFinalNewline} from './strip-newline.js';
8
8
 
9
9
  // Retrieve `result.stdout|stderr|all|stdio[*]`
10
- export const getStreamOutput = async ({stream, onStreamEnd, fdNumber, encoding, buffer, maxBuffer, lines, allMixed, stripFinalNewline, verboseInfo, streamInfo}) => {
10
+ export const getStreamOutput = async ({stream, onStreamEnd, fdNumber, encoding, buffer, maxBuffer, lines, allMixed, stripFinalNewline, allFds, verboseInfo, streamInfo}) => {
11
11
  const logPromise = logOutputAsync({
12
12
  stream,
13
13
  onStreamEnd,
@@ -23,7 +23,7 @@ export const getStreamOutput = async ({stream, onStreamEnd, fdNumber, encoding,
23
23
  return;
24
24
  }
25
25
 
26
- const stripFinalNewlineValue = getStripFinalNewline(stripFinalNewline, fdNumber);
26
+ const stripFinalNewlineValue = getStripFinalNewline(stripFinalNewline, fdNumber, allFds);
27
27
  const iterable = iterateForResult({
28
28
  stream,
29
29
  onStreamEnd,
@@ -69,7 +69,7 @@ const logOutputAsync = async ({stream, onStreamEnd, fdNumber, encoding, allMixed
69
69
 
70
70
  // When using `buffer: false`, users need to read `subprocess.stdout|stderr|all` right away
71
71
  // See https://github.com/sindresorhus/execa/issues/730 and https://github.com/sindresorhus/execa/pull/729#discussion_r1465496310
72
- const resumeStream = async stream => {
72
+ export const resumeStream = async stream => {
73
73
  await setImmediate();
74
74
  if (stream.readableFlowing === null) {
75
75
  stream.resume();
@@ -82,11 +82,7 @@ const getStreamContents = async ({stream, stream: {readableObjectMode}, iterable
82
82
  return await getStreamAsArray(iterable, {maxBuffer});
83
83
  }
84
84
 
85
- if (encoding === 'buffer') {
86
- return new Uint8Array(await getStreamAsArrayBuffer(iterable, {maxBuffer}));
87
- }
88
-
89
- return await getStream(iterable, {maxBuffer});
85
+ return encoding === 'buffer' ? new Uint8Array(await getStreamAsArrayBuffer(iterable, {maxBuffer})) : (await getStream(iterable, {maxBuffer}));
90
86
  } catch (error) {
91
87
  return handleBufferedData(handleMaxBuffer({
92
88
  error,
@@ -111,6 +107,7 @@ export const getBufferedData = async streamPromise => {
111
107
  };
112
108
 
113
109
  // Ensure we are returning Uint8Arrays when using `encoding: 'buffer'`
114
- const handleBufferedData = ({bufferedData}) => isArrayBuffer(bufferedData)
115
- ? new Uint8Array(bufferedData)
116
- : bufferedData;
110
+ // The error might be any value thrown by the `verbose` function, including `null` or `undefined`
111
+ const handleBufferedData = error => isArrayBuffer(error?.bufferedData)
112
+ ? new Uint8Array(error.bufferedData)
113
+ : error?.bufferedData;
@@ -1,6 +1,7 @@
1
1
  import {runGeneratorsSync} from '../transform/generator.js';
2
2
  import {joinToUint8Array, isUint8Array} from '../utils/uint-array.js';
3
3
  import {TYPE_TO_MESSAGE} from '../stdio/type.js';
4
+ import {getInputEncoding} from '../arguments/encoding-option.js';
4
5
 
5
6
  // Apply `stdin`/`input`/`inputFile` options, before spawning, in sync mode, by converting it to the `input` option
6
7
  export const addInputOptionsSync = (fileDescriptors, options) => {
@@ -25,13 +26,15 @@ const addInputOptionSync = (fileDescriptors, fdNumber, options) => {
25
26
  throw new TypeError(`Only the \`stdin\` option, not \`${optionName}\`, can be ${TYPE_TO_MESSAGE[type]} with synchronous methods.`);
26
27
  }
27
28
 
28
- const allContents = allStdioItems.map(({contents}) => contents);
29
- const transformedContents = allContents.map(contents => applySingleInputGeneratorsSync(contents, stdioItems));
30
- options.input = joinToUint8Array(transformedContents);
29
+ // All input sources are merged into a single list of chunks, like asynchronous methods merge input streams, so that transforms receive a single stream of data: line splitting spans all sources and `final` runs only once.
30
+ // Files are read lazily, so that a read failure is reported like any other early error
31
+ const chunks = allStdioItems.flatMap(({contents}) => typeof contents === 'function' ? contents() : contents);
32
+ options.input = applyInputGeneratorsSync(chunks, stdioItems, options.encoding);
31
33
  };
32
34
 
33
- const applySingleInputGeneratorsSync = (contents, stdioItems) => {
34
- const newContents = runGeneratorsSync(contents, stdioItems, 'utf8', true);
35
+ const applyInputGeneratorsSync = (chunks, stdioItems, encoding) => {
36
+ // Input transforms use UTF-8 with text encodings, since those only apply to the output
37
+ const newContents = runGeneratorsSync(chunks, stdioItems, getInputEncoding(encoding), true);
35
38
  validateSerializable(newContents);
36
39
  return joinToUint8Array(newContents);
37
40
  };
@@ -28,16 +28,13 @@ const getMaxBufferUnit = (readableObjectMode, lines, encoding) => {
28
28
  return 'lines';
29
29
  }
30
30
 
31
- if (encoding === 'buffer') {
32
- return 'bytes';
33
- }
34
-
35
- return 'characters';
31
+ return encoding === 'buffer' ? 'bytes' : 'characters';
36
32
  };
37
33
 
38
- // Check the `maxBuffer` option with `result.ipcOutput`
39
- export const checkIpcMaxBuffer = (subprocess, ipcOutput, maxBuffer) => {
40
- if (ipcOutput.length !== maxBuffer) {
34
+ // Check the `maxBuffer` option with `result.ipcOutput`, before buffering one more message.
35
+ // Like the stream-based `maxBuffer`, the threshold is hit when the buffered amount would exceed it, which also applies to non-integer values.
36
+ export const checkIpcMaxBuffer = (ipcOutput, maxBuffer) => {
37
+ if (ipcOutput.length + 1 <= maxBuffer) {
41
38
  return;
42
39
  }
43
40
 
@@ -57,33 +54,49 @@ const getMaxBufferInfo = (error, maxBuffer) => {
57
54
  return {streamName: 'output', threshold: maxBuffer[1], unit: 'bytes'};
58
55
  }
59
56
 
60
- const {maxBufferInfo: {fdNumber, unit}} = error;
57
+ const {maxBufferInfo: {fdNumber, unit, threshold = getFdSpecificValue(maxBuffer, fdNumber)}} = error;
61
58
  delete error.maxBufferInfo;
62
59
 
63
- const threshold = getFdSpecificValue(maxBuffer, fdNumber);
64
- if (fdNumber === 'ipc') {
65
- return {streamName: 'IPC output', threshold, unit: 'messages'};
60
+ return fdNumber === 'ipc' ? {streamName: 'IPC output', threshold, unit: 'messages'} : {streamName: getStreamName(fdNumber), threshold, unit};
61
+ };
62
+
63
+ // The native `maxBuffer` option passed to `spawnSync()` does not allow differentiating values per file descriptor, so we use `stdout`'s.
64
+ // Since each other file descriptor's own limit is capped by `stdout`'s, this is also the highest one.
65
+ // Node.js applies it to the total output of all piped file descriptors, not to each of them. When it is hit, the subprocess is killed and a `ENOBUFS` error is thrown.
66
+ export const getMaxBufferSync = ([, stdoutMaxBuffer]) => stdoutMaxBuffer;
67
+
68
+ // File descriptors with a limit lower than `spawnSync()`'s native `maxBuffer` are enforced from the output, after the subprocess exits.
69
+ // Since no native error was thrown, we create the `ENOBUFS` error ourselves.
70
+ // Like with asynchronous methods, file descriptors which are not buffered are not limited by their own value. However, when they are piped, e.g. to a file, they still count towards the native limit, which is `stdout`'s value.
71
+ export const handleMaxBufferSync = ({resultError, output, maxBuffer, buffer}) => {
72
+ const fdNumber = findMaxBufferSync(output, maxBuffer, buffer);
73
+ if (fdNumber === undefined) {
74
+ // The native limit can be hit by the total output without any single file descriptor being over its own limit
75
+ return {resultError, isMaxBuffer: resultError?.code === 'ENOBUFS'};
66
76
  }
67
77
 
68
- return {streamName: getStreamName(fdNumber), threshold, unit};
78
+ const threshold = getMaxBufferValueSync(maxBuffer, fdNumber);
79
+ const error = resultError ?? Object.assign(new MaxBufferError(), {code: 'ENOBUFS'});
80
+ error.maxBufferInfo = {fdNumber, unit: 'bytes', threshold};
81
+ return {resultError: error, isMaxBuffer: true};
69
82
  };
70
83
 
71
- // The only way to apply `maxBuffer` with `spawnSync()` is to use the native `maxBuffer` option Node.js provides.
72
- // However, this has multiple limitations, and cannot behave the exact same way as the async behavior.
73
- // When the `maxBuffer` is hit, a `ENOBUFS` error is thrown.
74
- export const isMaxBufferSync = (resultError, output, maxBuffer) => resultError?.code === 'ENOBUFS'
75
- && output !== null
76
- && output.some(result => result !== null && result.length > getMaxBufferSync(maxBuffer));
77
-
78
- // When `maxBuffer` is hit, ensure the result is truncated
79
- export const truncateMaxBufferSync = (result, isMaxBuffer, maxBuffer) => {
80
- if (!isMaxBuffer) {
81
- return result;
84
+ const findMaxBufferSync = (output, maxBuffer, buffer) => {
85
+ if (output === null) {
86
+ return;
82
87
  }
83
88
 
84
- const maxBufferValue = getMaxBufferSync(maxBuffer);
85
- return result.length > maxBufferValue ? result.slice(0, maxBufferValue) : result;
89
+ const fdNumber = output.findIndex((result, index) => result !== null
90
+ && buffer[index]
91
+ && result.length > getMaxBufferValueSync(maxBuffer, index));
92
+ return fdNumber === -1 ? undefined : fdNumber;
86
93
  };
87
94
 
88
- // `spawnSync()` does not allow differentiating `maxBuffer` per file descriptor, so we always use `stdout`
89
- export const getMaxBufferSync = ([, stdoutMaxBuffer]) => stdoutMaxBuffer;
95
+ // `spawnSync()` uses `maxBuffer.stdout` as its native `maxBuffer`, so other file descriptors' limits can only be lower than that
96
+ const getMaxBufferValueSync = (maxBuffer, fdNumber) => Math.min(maxBuffer[1], maxBuffer[fdNumber]);
97
+
98
+ // When `maxBuffer` is hit, ensure each buffered file descriptor's result is truncated at its own limit
99
+ export const truncateMaxBufferSync = (result, fdNumber, {maxBuffer, buffer}) => {
100
+ const maxBufferValue = getMaxBufferValueSync(maxBuffer, fdNumber);
101
+ return buffer[fdNumber] && result.length > maxBufferValue ? result.slice(0, maxBufferValue) : result;
102
+ };