@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.
- package/README.md +1 -1
- package/bin/dsh.mjs +1 -1
- package/node_modules/@babel/runtime/helpers/esm/interopRequireWildcard.js +16 -19
- package/node_modules/@babel/runtime/helpers/esm/toPrimitive.js +5 -5
- package/node_modules/@babel/runtime/helpers/interopRequireWildcard.js +16 -19
- package/node_modules/@babel/runtime/helpers/toPrimitive.js +5 -5
- package/node_modules/@babel/runtime/package.json +1 -1
- package/node_modules/@deepseek-ai/dsh-client-ui-primitives/package.json +2 -2
- package/node_modules/@deepseek-ai/dsh-client-ui-sidebar/lib/client.js +1 -1
- package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/build/esm/version.js +1 -1
- package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/build/esnext/version.js +1 -1
- package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/build/src/version.js +1 -1
- package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/node_modules/@opentelemetry/core/build/esm/version.js +1 -1
- package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/node_modules/@opentelemetry/core/build/esnext/version.js +1 -1
- package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/node_modules/@opentelemetry/core/build/src/version.js +1 -1
- package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/node_modules/@opentelemetry/core/package.json +2 -2
- package/node_modules/@deepseek-ai/dsh-otel/node_modules/@opentelemetry/resources/package.json +3 -3
- package/node_modules/@deepseek-ai/dsh-tui-app/README.i18n.yaml +6 -6
- package/node_modules/@deepseek-ai/dsh-tui-app/README.md +4 -2
- package/node_modules/@deepseek-ai/dsh-tui-app/README.zh.md +4 -2
- package/node_modules/@deepseek-ai/dsh-tui-app/lib/index.js +1367 -18
- package/node_modules/@deepseek-ai/dsh-tui-app/package.json +2 -0
- package/node_modules/execa/lib/arguments/command-file.js +55 -23
- package/node_modules/execa/lib/arguments/command.js +2 -1
- package/node_modules/execa/lib/arguments/cwd.js +10 -6
- package/node_modules/execa/lib/arguments/encoding-option.js +11 -0
- package/node_modules/execa/lib/arguments/escape.js +2 -2
- package/node_modules/execa/lib/arguments/fd-options.js +3 -7
- package/node_modules/execa/lib/arguments/options.js +13 -3
- package/node_modules/execa/lib/arguments/specific.js +2 -1
- package/node_modules/execa/lib/convert/duplex.js +3 -1
- package/node_modules/execa/lib/convert/iterable.js +3 -5
- package/node_modules/execa/lib/convert/readable.js +3 -1
- package/node_modules/execa/lib/convert/writable.js +8 -4
- package/node_modules/execa/lib/io/contents.js +8 -11
- package/node_modules/execa/lib/io/input-sync.js +8 -5
- package/node_modules/execa/lib/io/max-buffer.js +41 -28
- package/node_modules/execa/lib/io/output-sync.js +64 -40
- package/node_modules/execa/lib/io/strip-newline.js +4 -3
- package/node_modules/execa/lib/ipc/buffer-messages.js +12 -2
- package/node_modules/execa/lib/ipc/get-each.js +8 -2
- package/node_modules/execa/lib/ipc/get-one.js +3 -1
- package/node_modules/execa/lib/ipc/graceful.js +3 -2
- package/node_modules/execa/lib/ipc/incoming.js +10 -3
- package/node_modules/execa/lib/ipc/ipc-input.js +7 -2
- package/node_modules/execa/lib/ipc/send.js +3 -1
- package/node_modules/execa/lib/ipc/strict.js +13 -7
- package/node_modules/execa/lib/ipc/validation.js +1 -1
- package/node_modules/execa/lib/methods/bind.js +2 -8
- package/node_modules/execa/lib/methods/create.js +3 -1
- package/node_modules/execa/lib/methods/main-async.js +21 -11
- package/node_modules/execa/lib/methods/main-sync.js +7 -5
- package/node_modules/execa/lib/methods/node.js +1 -1
- package/node_modules/execa/lib/methods/script.js +1 -1
- package/node_modules/execa/lib/methods/template.js +24 -5
- package/node_modules/execa/lib/pipe/pipe-arguments.js +36 -5
- package/node_modules/execa/lib/pipe/throw.js +5 -3
- package/node_modules/execa/lib/resolve/all-async.js +43 -19
- package/node_modules/execa/lib/resolve/all-sync.js +27 -15
- package/node_modules/execa/lib/resolve/exit-sync.js +13 -5
- package/node_modules/execa/lib/resolve/stdio.js +2 -1
- package/node_modules/execa/lib/resolve/wait-stream.js +12 -2
- package/node_modules/execa/lib/resolve/wait-subprocess.js +3 -1
- package/node_modules/execa/lib/return/early-error.js +58 -21
- package/node_modules/execa/lib/return/message.js +2 -10
- package/node_modules/execa/lib/return/reject.js +1 -1
- package/node_modules/execa/lib/stdio/direction.js +36 -5
- package/node_modules/execa/lib/stdio/duplicate.js +26 -16
- package/node_modules/execa/lib/stdio/handle-async.js +6 -0
- package/node_modules/execa/lib/stdio/handle-sync.js +5 -3
- package/node_modules/execa/lib/stdio/handle.js +47 -24
- package/node_modules/execa/lib/stdio/native.js +3 -6
- package/node_modules/execa/lib/stdio/stdio-option.js +13 -18
- package/node_modules/execa/lib/stdio/type.js +2 -10
- package/node_modules/execa/lib/terminate/cancel.js +7 -0
- package/node_modules/execa/lib/terminate/kill-descendants.js +20 -4
- package/node_modules/execa/lib/transform/encoding-transform.js +15 -0
- package/node_modules/execa/lib/transform/generator.js +13 -2
- package/node_modules/execa/lib/transform/split.js +37 -23
- package/node_modules/execa/lib/utils/options.js +9 -0
- package/node_modules/execa/lib/utils/standard-stream.js +6 -0
- package/node_modules/execa/lib/utils/uint-array.js +1 -7
- package/node_modules/execa/lib/verbose/log.js +3 -6
- package/node_modules/execa/lib/verbose/output.js +3 -1
- package/node_modules/execa/package.json +51 -48
- package/package.json +3 -3
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import {fileURLToPath} from 'node:url';
|
|
1
3
|
import {
|
|
2
4
|
SPECIAL_DUPLICATE_TYPES_SYNC,
|
|
3
5
|
SPECIAL_DUPLICATE_TYPES,
|
|
@@ -5,6 +7,14 @@ import {
|
|
|
5
7
|
TYPE_TO_MESSAGE,
|
|
6
8
|
} from './type.js';
|
|
7
9
|
|
|
10
|
+
// `{file: './output.txt'}` and a `file:` URL are two ways to target the same file, so both must be compared together
|
|
11
|
+
const isFileType = type => SPECIAL_DUPLICATE_TYPES_SYNC.has(type);
|
|
12
|
+
|
|
13
|
+
// The file that a `filePath` or `fileUrl` stdio item targets, as an absolute path
|
|
14
|
+
export const getFileTarget = ({type, value}) => type === 'filePath'
|
|
15
|
+
? path.resolve(value.file)
|
|
16
|
+
: fileURLToPath(value);
|
|
17
|
+
|
|
8
18
|
// Duplicates in the same file descriptor is most likely an error.
|
|
9
19
|
// However, this can be useful with generators.
|
|
10
20
|
export const filterDuplicates = stdioItems => stdioItems.filter((stdioItemOne, indexOne) =>
|
|
@@ -18,6 +28,7 @@ const hasSameValueAndDirection = (stdioItemOne, stdioItemTwo) => stdioItemOne.va
|
|
|
18
28
|
|
|
19
29
|
// Check if two file descriptors are sharing the same target.
|
|
20
30
|
// For example `{stdout: {file: './output.txt'}, stderr: {file: './output.txt'}}`.
|
|
31
|
+
// The same applies to two items of a single file descriptor, e.g. `{stdout: [{file: './output.txt'}, {file: './output.txt'}]}`.
|
|
21
32
|
export const getDuplicateStream = ({stdioItem: {type, value, optionName}, direction, fileDescriptors, isSync}) => {
|
|
22
33
|
const otherStdioItems = getOtherStdioItems(fileDescriptors, type);
|
|
23
34
|
if (otherStdioItems.length === 0) {
|
|
@@ -55,11 +66,15 @@ export const getDuplicateStream = ({stdioItem: {type, value, optionName}, direct
|
|
|
55
66
|
}
|
|
56
67
|
};
|
|
57
68
|
|
|
58
|
-
// Values shared by multiple file descriptors
|
|
69
|
+
// Values shared by multiple file descriptors, including the other items of the file descriptor currently being built
|
|
59
70
|
const getOtherStdioItems = (fileDescriptors, type) => fileDescriptors
|
|
60
71
|
.flatMap(({direction, stdioItems}) => stdioItems
|
|
61
|
-
.
|
|
62
|
-
|
|
72
|
+
.map(stdioItem => ({...stdioItem, direction})),
|
|
73
|
+
)
|
|
74
|
+
.filter(stdioItem => isSameDuplicateType(stdioItem.type, type));
|
|
75
|
+
|
|
76
|
+
// A `filePath` and a `fileUrl` are compared together, since they can target the same file
|
|
77
|
+
const isSameDuplicateType = (otherType, type) => otherType === type || (isFileType(otherType) && isFileType(type));
|
|
63
78
|
|
|
64
79
|
// With `execaSync()`, do not allow setting a file path both in input and output
|
|
65
80
|
const validateDuplicateStreamSync = ({otherStdioItems, type, value, optionName, direction}) => {
|
|
@@ -80,7 +95,7 @@ const validateDuplicateStreamSync = ({otherStdioItems, type, value, optionName,
|
|
|
80
95
|
// This is not a problem with generator functions since those create a new instance for each file descriptor.
|
|
81
96
|
// We also forbid input and output file descriptors sharing the same file or stream, since that does not make sense.
|
|
82
97
|
const getDuplicateStreamInstance = ({otherStdioItems, type, value, optionName, direction}) => {
|
|
83
|
-
const duplicateStdioItems = otherStdioItems.filter(stdioItem => hasSameValue(stdioItem, value));
|
|
98
|
+
const duplicateStdioItems = otherStdioItems.filter(stdioItem => hasSameValue(stdioItem, type, value));
|
|
84
99
|
if (duplicateStdioItems.length === 0) {
|
|
85
100
|
return;
|
|
86
101
|
}
|
|
@@ -88,20 +103,15 @@ const getDuplicateStreamInstance = ({otherStdioItems, type, value, optionName, d
|
|
|
88
103
|
const differentStdioItem = duplicateStdioItems.find(stdioItem => stdioItem.direction !== direction);
|
|
89
104
|
throwOnDuplicateStream(differentStdioItem, optionName, type);
|
|
90
105
|
|
|
91
|
-
|
|
106
|
+
// Output streams must be re-used so they are not closed twice.
|
|
107
|
+
// Web streams must be re-used in the input direction too, since they cannot be converted twice: reading one locks it.
|
|
108
|
+
return direction === 'output' || type === 'webStream' ? duplicateStdioItems[0].stream : undefined;
|
|
92
109
|
};
|
|
93
110
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (type === 'fileUrl') {
|
|
100
|
-
return value.href === secondValue.href;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return value === secondValue;
|
|
104
|
-
};
|
|
111
|
+
// `type` and `value` are the current item's, `otherStdioItem` is the other one
|
|
112
|
+
const hasSameValue = (otherStdioItem, type, value) => isFileType(otherStdioItem.type)
|
|
113
|
+
? getFileTarget(otherStdioItem) === getFileTarget({type, value})
|
|
114
|
+
: otherStdioItem.value === value;
|
|
105
115
|
|
|
106
116
|
// We do not allow two file descriptors to share the same Duplex or TransformStream.
|
|
107
117
|
// This is because those are set directly to `subprocess.std*`.
|
|
@@ -2,6 +2,7 @@ import {createReadStream, createWriteStream} from 'node:fs';
|
|
|
2
2
|
import {Buffer} from 'node:buffer';
|
|
3
3
|
import {Readable, Writable, Duplex} from 'node:stream';
|
|
4
4
|
import {generatorToStream} from '../transform/generator.js';
|
|
5
|
+
import {getInputEncoding} from '../arguments/encoding-option.js';
|
|
5
6
|
import {handleStdio} from './handle.js';
|
|
6
7
|
import {TYPE_TO_MESSAGE} from './type.js';
|
|
7
8
|
|
|
@@ -28,9 +29,14 @@ const addProperties = {
|
|
|
28
29
|
native() {},
|
|
29
30
|
};
|
|
30
31
|
|
|
32
|
+
// Input transforms use UTF-8 with text encodings, since those only apply to the output
|
|
33
|
+
const generatorToInputStream = (stdioItem, {encoding}) => generatorToStream(stdioItem, {encoding: getInputEncoding(encoding)});
|
|
34
|
+
|
|
31
35
|
const addPropertiesAsync = {
|
|
32
36
|
input: {
|
|
33
37
|
...addProperties,
|
|
38
|
+
generator: generatorToInputStream,
|
|
39
|
+
asyncGenerator: generatorToInputStream,
|
|
34
40
|
fileUrl: ({value}) => ({stream: createReadStream(value)}),
|
|
35
41
|
filePath: ({value: {file}}) => ({stream: createReadStream(file)}),
|
|
36
42
|
webStream: ({value}) => ({stream: Readable.fromWeb(value)}),
|
|
@@ -49,12 +49,14 @@ const addPropertiesSync = {
|
|
|
49
49
|
input: {
|
|
50
50
|
...addProperties,
|
|
51
51
|
native: forbiddenNativeInputIfSync,
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
// Files are read lazily, so that a file which cannot be read is reported like any other early error, instead of being thrown as a raw error
|
|
53
|
+
fileUrl: ({value}) => ({contents: () => [bufferToUint8Array(readFileSync(value))]}),
|
|
54
|
+
filePath: ({value: {file}}) => ({contents: () => [bufferToUint8Array(readFileSync(file))]}),
|
|
54
55
|
fileNumber: forbiddenIfSync,
|
|
55
56
|
iterable: ({value}) => ({contents: [...value]}),
|
|
56
57
|
string: ({value}) => ({contents: [value]}),
|
|
57
|
-
|
|
58
|
+
// `contents` is already set when reading from a file descriptor
|
|
59
|
+
uint8Array: ({value, contents = [value]}) => ({contents}),
|
|
58
60
|
},
|
|
59
61
|
output: {
|
|
60
62
|
...addProperties,
|
|
@@ -44,7 +44,12 @@ const getFileDescriptor = ({stdioOption, fdNumber, options, isSync}) => {
|
|
|
44
44
|
options,
|
|
45
45
|
optionName,
|
|
46
46
|
});
|
|
47
|
-
const direction = getStreamDirection(
|
|
47
|
+
const direction = getStreamDirection({
|
|
48
|
+
stdioItems: initialStdioItems,
|
|
49
|
+
isStdioArray,
|
|
50
|
+
fdNumber,
|
|
51
|
+
optionName,
|
|
52
|
+
});
|
|
48
53
|
const stdioItems = initialStdioItems.map(stdioItem => handleNativeStream({
|
|
49
54
|
stdioItem,
|
|
50
55
|
isStdioArray,
|
|
@@ -131,9 +136,20 @@ const INVALID_STDIO_ARRAY_OPTIONS = new Set(['ignore']);
|
|
|
131
136
|
const validateStreams = stdioItems => {
|
|
132
137
|
for (const stdioItem of stdioItems) {
|
|
133
138
|
validateFileStdio(stdioItem);
|
|
139
|
+
validateFileDescriptorStdio(stdioItem);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// A number is a file descriptor. Anything else is not a valid one, and passing it to `node:child_process` aborts the current process with a fatal assertion instead of throwing.
|
|
144
|
+
const validateFileDescriptorStdio = ({type, value, optionName}) => {
|
|
145
|
+
if (type === 'native' && typeof value === 'number' && (!Number.isInteger(value) || value < 0 || value > MAX_FILE_DESCRIPTOR)) {
|
|
146
|
+
throw new TypeError(`The \`${optionName}\` option must be a non-negative 32-bit integer: got ${value}.`);
|
|
134
147
|
}
|
|
135
148
|
};
|
|
136
149
|
|
|
150
|
+
// `node:child_process` converts file descriptors to a 32-bit signed integer, and aborts the process when it cannot
|
|
151
|
+
const MAX_FILE_DESCRIPTOR = (2 ** 31) - 1;
|
|
152
|
+
|
|
137
153
|
const validateFileStdio = ({type, value, optionName}) => {
|
|
138
154
|
if (isRegularUrl(value)) {
|
|
139
155
|
throw new TypeError(`The \`${optionName}: URL\` option must use the \`file:\` scheme.
|
|
@@ -164,13 +180,13 @@ const getFinalFileDescriptors = ({initialFileDescriptors, addProperties, options
|
|
|
164
180
|
|
|
165
181
|
try {
|
|
166
182
|
for (const fileDescriptor of initialFileDescriptors) {
|
|
167
|
-
|
|
183
|
+
addFinalFileDescriptor({
|
|
168
184
|
fileDescriptor,
|
|
169
185
|
fileDescriptors,
|
|
170
186
|
addProperties,
|
|
171
187
|
options,
|
|
172
188
|
isSync,
|
|
173
|
-
})
|
|
189
|
+
});
|
|
174
190
|
}
|
|
175
191
|
|
|
176
192
|
return fileDescriptors;
|
|
@@ -180,22 +196,26 @@ const getFinalFileDescriptors = ({initialFileDescriptors, addProperties, options
|
|
|
180
196
|
}
|
|
181
197
|
};
|
|
182
198
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
}
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
+
/*
|
|
200
|
+
The file descriptor is added to `fileDescriptors` before its items are, for two reasons:
|
|
201
|
+
- its items are added one at a time, so that each one knows about the previous ones, to detect duplicates within the same file descriptor
|
|
202
|
+
- if an item throws, the streams created by the previous ones are cleaned up too
|
|
203
|
+
*/
|
|
204
|
+
const addFinalFileDescriptor = ({fileDescriptor, fileDescriptors, addProperties, options, isSync}) => {
|
|
205
|
+
const {direction, objectMode, stdioItems} = fileDescriptor;
|
|
206
|
+
const finalFileDescriptor = {direction, objectMode, stdioItems: []};
|
|
207
|
+
fileDescriptors.push(finalFileDescriptor);
|
|
208
|
+
|
|
209
|
+
for (const stdioItem of stdioItems) {
|
|
210
|
+
finalFileDescriptor.stdioItems.push(addStreamProperties({
|
|
211
|
+
stdioItem,
|
|
212
|
+
addProperties,
|
|
213
|
+
direction,
|
|
214
|
+
options,
|
|
215
|
+
fileDescriptors,
|
|
216
|
+
isSync,
|
|
217
|
+
}));
|
|
218
|
+
}
|
|
199
219
|
};
|
|
200
220
|
|
|
201
221
|
const addStreamProperties = ({stdioItem, addProperties, direction, options, fileDescriptors, isSync}) => {
|
|
@@ -221,12 +241,15 @@ const addStreamProperties = ({stdioItem, addProperties, direction, options, file
|
|
|
221
241
|
// We need to create those streams before subprocess spawning, in case their creation fails, e.g. when passing an invalid generator as argument.
|
|
222
242
|
// Like this, an exception would be thrown, which would prevent spawning a subprocess.
|
|
223
243
|
export const cleanupCustomStreams = fileDescriptors => {
|
|
224
|
-
for (const {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
stream.destroy();
|
|
228
|
-
}
|
|
244
|
+
for (const {stream} of fileDescriptors.flatMap(({stdioItems}) => stdioItems)) {
|
|
245
|
+
if (stream === undefined || isStandardStream(stream)) {
|
|
246
|
+
continue;
|
|
229
247
|
}
|
|
248
|
+
|
|
249
|
+
// The stream can still error after being destroyed, e.g. when its file cannot be opened.
|
|
250
|
+
// That error would be uncaught and crash the current process, although it is irrelevant since execa already fails with its own error.
|
|
251
|
+
stream.on('error', () => {});
|
|
252
|
+
stream.destroy();
|
|
230
253
|
}
|
|
231
254
|
};
|
|
232
255
|
|
|
@@ -56,7 +56,8 @@ const getTargetFd = ({value, optionName, fdNumber, direction}) => {
|
|
|
56
56
|
throw new TypeError(`The \`${optionName}: ${serializeOptionValue(value)}\` option is invalid: it cannot be a TTY with synchronous methods.`);
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
// The file descriptor is read lazily, like `inputFile`, so that a read failure is reported like any other early error, instead of being thrown as a raw error
|
|
60
|
+
return {type: 'uint8Array', contents: () => [bufferToUint8Array(readFileSync(targetFdNumber))], optionName};
|
|
60
61
|
};
|
|
61
62
|
|
|
62
63
|
const getTargetFdNumber = (value, fdNumber) => {
|
|
@@ -83,11 +84,7 @@ const handleNativeStreamAsync = ({stdioItem, stdioItem: {value, optionName}, fdN
|
|
|
83
84
|
return {type: 'nodeStream', value: getStandardStream(value, value, optionName), optionName};
|
|
84
85
|
}
|
|
85
86
|
|
|
86
|
-
|
|
87
|
-
return {type: 'nodeStream', value, optionName};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return stdioItem;
|
|
87
|
+
return isNodeStream(value, {checkOpen: false}) ? {type: 'nodeStream', value, optionName} : stdioItem;
|
|
91
88
|
};
|
|
92
89
|
|
|
93
90
|
// Node.js does not allow to easily retrieve file descriptors beyond stdin/stdout/stderr as streams.
|
|
@@ -5,7 +5,9 @@ import {isStdioValueObject} from './type.js';
|
|
|
5
5
|
|
|
6
6
|
// Add support for `stdin`/`stdout`/`stderr` as an alias for `stdio`.
|
|
7
7
|
// Also normalize the `stdio` option.
|
|
8
|
-
|
|
8
|
+
// A rest pattern is not used to read the other options, since the object it creates inherits from `Object.prototype`, which a prototype pollution could then use to inject option values
|
|
9
|
+
export const normalizeStdioOption = (options, verboseInfo, isSync) => {
|
|
10
|
+
const {stdio, ipc, buffer} = options;
|
|
9
11
|
const stdioArray = getStdioArray(stdio, options).map((stdioOption, fdNumber) => addDefaultValue(stdioOption, fdNumber));
|
|
10
12
|
validateIpcStdioOption(stdioArray);
|
|
11
13
|
return isSync
|
|
@@ -20,21 +22,9 @@ const validateIpcStdioOption = stdioArray => {
|
|
|
20
22
|
}
|
|
21
23
|
};
|
|
22
24
|
|
|
23
|
-
const hasIpcStdioOption = stdioOption =>
|
|
24
|
-
if (Array.isArray(stdioOption)) {
|
|
25
|
-
return stdioOption.some(item => hasIpcStdioItem(item));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return hasIpcStdioItem(stdioOption);
|
|
29
|
-
};
|
|
25
|
+
const hasIpcStdioOption = stdioOption => Array.isArray(stdioOption) ? stdioOption.some(item => hasIpcStdioItem(item)) : hasIpcStdioItem(stdioOption);
|
|
30
26
|
|
|
31
|
-
const hasIpcStdioItem = stdioOption =>
|
|
32
|
-
if (isStdioValueObject(stdioOption)) {
|
|
33
|
-
return stdioOption.value === 'ipc';
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return stdioOption === 'ipc';
|
|
37
|
-
};
|
|
27
|
+
const hasIpcStdioItem = stdioOption => isStdioValueObject(stdioOption) ? stdioOption.value === 'ipc' : stdioOption === 'ipc';
|
|
38
28
|
|
|
39
29
|
const getStdioArray = (stdio, options) => {
|
|
40
30
|
if (stdio === undefined) {
|
|
@@ -73,16 +63,19 @@ const addDefaultValue = (stdioOption, fdNumber) => {
|
|
|
73
63
|
|
|
74
64
|
// Using `buffer: false` with synchronous methods implies `stdout`/`stderr`: `ignore`.
|
|
75
65
|
// Unless the output is needed, e.g. due to `verbose: 'full'` or to redirecting to a file.
|
|
66
|
+
// This is only done for `stdout`/`stderr`, since `ignore` redirects them to `/dev/null`.
|
|
67
|
+
// With additional file descriptors, it does not pass them to the subprocess at all, which would make it fail when writing to them.
|
|
76
68
|
const normalizeStdioSync = (stdioArray, buffer, verboseInfo) => stdioArray.map((stdioOption, fdNumber) =>
|
|
77
69
|
!buffer[fdNumber]
|
|
78
|
-
&& fdNumber
|
|
70
|
+
&& isStandardOutputFd(fdNumber)
|
|
79
71
|
&& !isFullVerbose(verboseInfo, fdNumber)
|
|
80
72
|
&& isOutputPipeOnly(stdioOption, fdNumber)
|
|
81
73
|
? 'ignore'
|
|
82
74
|
: stdioOption);
|
|
83
75
|
|
|
76
|
+
// An empty array is not output pipe only: it must keep being rejected as an empty array below
|
|
84
77
|
const isOutputPipeOnly = (stdioOption, fdNumber) => isOutputPipe(stdioOption, fdNumber)
|
|
85
|
-
|| (Array.isArray(stdioOption) && stdioOption.every(item => isOutputPipe(item, fdNumber)));
|
|
78
|
+
|| (Array.isArray(stdioOption) && stdioOption.length > 0 && stdioOption.every(item => isOutputPipe(item, fdNumber)));
|
|
86
79
|
|
|
87
80
|
const isOutputPipe = (stdioOption, fdNumber) => stdioOption === 'pipe'
|
|
88
81
|
|| isOutputPipeObject(stdioOption, fdNumber);
|
|
@@ -92,4 +85,6 @@ const isOutputPipeObject = (stdioOption, fdNumber) => isStdioValueObject(stdioOp
|
|
|
92
85
|
&& (stdioOption.input === undefined || stdioOption.input === false || isFixedOutputPipe(fdNumber, stdioOption.input));
|
|
93
86
|
|
|
94
87
|
// On `stdout`/`stderr`, the direction is always output, so `input: true` is ignored and the pipe stays output.
|
|
95
|
-
const isFixedOutputPipe = (fdNumber, input) => input === true && (fdNumber
|
|
88
|
+
const isFixedOutputPipe = (fdNumber, input) => input === true && isStandardOutputFd(fdNumber);
|
|
89
|
+
|
|
90
|
+
const isStandardOutputFd = fdNumber => fdNumber === 1 || fdNumber === 2;
|
|
@@ -44,11 +44,7 @@ export const getStdioItemType = (value, optionName) => {
|
|
|
44
44
|
return getTransformStreamType({transform: value}, optionName);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
return getTransformObjectType(value, optionName);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return 'native';
|
|
47
|
+
return isTransformOptions(value) ? getTransformObjectType(value, optionName) : 'native';
|
|
52
48
|
};
|
|
53
49
|
|
|
54
50
|
const getTransformObjectType = (value, optionName) => {
|
|
@@ -56,11 +52,7 @@ const getTransformObjectType = (value, optionName) => {
|
|
|
56
52
|
return getDuplexType(value, optionName);
|
|
57
53
|
}
|
|
58
54
|
|
|
59
|
-
|
|
60
|
-
return getTransformStreamType(value, optionName);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return getGeneratorObjectType(value, optionName);
|
|
55
|
+
return isTransformStream(value.transform) ? getTransformStreamType(value, optionName) : getGeneratorObjectType(value, optionName);
|
|
64
56
|
};
|
|
65
57
|
|
|
66
58
|
const getDuplexType = (value, optionName) => {
|
|
@@ -7,6 +7,13 @@ export const validateCancelSignal = ({cancelSignal}) => {
|
|
|
7
7
|
}
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
+
// The `signal` option was renamed to `cancelSignal`. Validated for every method, otherwise synchronous methods would silently ignore it, since `child_process.spawnSync()` does not support that option either.
|
|
11
|
+
export const validateRenamedSignalOption = ({signal}) => {
|
|
12
|
+
if (signal !== undefined) {
|
|
13
|
+
throw new TypeError('The "signal" option has been renamed to "cancelSignal" instead.');
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
10
17
|
// Terminate the subprocess when aborting the `cancelSignal` option and `gracefulSignal` is `false`
|
|
11
18
|
export const throwOnCancel = ({kill, cancelSignal, gracefulCancel, context, controller}) => cancelSignal === undefined || gracefulCancel
|
|
12
19
|
? []
|
|
@@ -9,7 +9,7 @@ const isWindows = process.platform === 'win32';
|
|
|
9
9
|
// `detached` argument passed to `child_process.spawn()`.
|
|
10
10
|
// This is kept separate from the user-facing `detached` option, which must keep its own value,
|
|
11
11
|
// so the `cleanup` behavior is not affected.
|
|
12
|
-
export const getSpawnOptions = options => options.killDescendants
|
|
12
|
+
export const getSpawnOptions = options => !isWindows && options.killDescendants
|
|
13
13
|
? {...options, detached: true}
|
|
14
14
|
: options;
|
|
15
15
|
|
|
@@ -17,17 +17,33 @@ export const getSpawnOptions = options => options.killDescendants && !isWindows
|
|
|
17
17
|
// With the `killDescendants` option, the signal is sent to the whole process tree.
|
|
18
18
|
export const getKillFunction = (subprocess, {killDescendants}) => {
|
|
19
19
|
if (!killDescendants) {
|
|
20
|
-
|
|
20
|
+
/*
|
|
21
|
+
`subprocess.pid` is `undefined` when the subprocess failed to spawn, e.g. with `ENOENT`.
|
|
22
|
+
`ChildProcess.prototype.kill()` then signals the current process group, since its process id is `0`.
|
|
23
|
+
So, like `ChildProcess.prototype.kill()` does for a subprocess which already exited, nothing is signaled and `false` is returned.
|
|
24
|
+
*/
|
|
25
|
+
const killSubprocess = subprocess.kill.bind(subprocess);
|
|
26
|
+
return (...arguments_) => (subprocess.pid === undefined ? false : killSubprocess(...arguments_));
|
|
21
27
|
}
|
|
22
28
|
|
|
23
29
|
const killDescendantsFunction = isWindows ? killDescendantsWindows : killDescendantsUnix;
|
|
24
30
|
return killDescendantsFunction.bind(undefined, subprocess);
|
|
25
31
|
};
|
|
26
32
|
|
|
33
|
+
/*
|
|
34
|
+
Unlike `subprocess.kill()`, signaling a process group or a process tree uses the PID directly, without going through the subprocess' handle. The OS can re-assign that PID once it is not used anymore, and signaling it then would terminate an unrelated process, and on Windows its own descendants too.
|
|
35
|
+
On Unix, a PID stays reserved as long as it is the process group ID of a running process, i.e. as long as any descendant in that group is still running. So signaling the process group after the subprocess exited is still useful, e.g. for the `forceKillAfterDelay` escalation while Execa is waiting on a descendant, or with `subprocess.kill()` to terminate background descendants once the subprocess has settled. Once the whole group exited, the PID can only be signaled if the OS re-assigned it to a new process group leader, which is unlikely, so this is a documented limitation.
|
|
36
|
+
On Windows, there are no process groups: a PID is only reserved while a handle to the process is open, which Node.js closes once the subprocess exits. `taskkill /T` also cannot find the descendants of a process which exited. So the process tree is never signaled once the subprocess exited.
|
|
37
|
+
*/
|
|
38
|
+
const isAbandoned = subprocess => subprocess.pid === undefined
|
|
39
|
+
|| (isWindows && hasExited(subprocess));
|
|
40
|
+
|
|
41
|
+
const hasExited = ({exitCode, signalCode}) => exitCode !== null || signalCode !== null;
|
|
42
|
+
|
|
27
43
|
// On Unix, the subprocess is its own process group leader (its PGID equals its PID), since it
|
|
28
44
|
// was spawned with `detached: true`. Sending the signal to `-pid` targets the whole group.
|
|
29
45
|
const killDescendantsUnix = (subprocess, signal) => {
|
|
30
|
-
if (subprocess
|
|
46
|
+
if (isAbandoned(subprocess)) {
|
|
31
47
|
return false;
|
|
32
48
|
}
|
|
33
49
|
|
|
@@ -46,7 +62,7 @@ const killDescendantsUnix = (subprocess, signal) => {
|
|
|
46
62
|
// If `taskkill` is unavailable or fails, the fallback only terminates the direct subprocess.
|
|
47
63
|
// `taskkill` ignores the signal (`/F` terminates the tree).
|
|
48
64
|
const killDescendantsWindows = (subprocess, signal) => {
|
|
49
|
-
if (subprocess
|
|
65
|
+
if (isAbandoned(subprocess)) {
|
|
50
66
|
return false;
|
|
51
67
|
}
|
|
52
68
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {Buffer} from 'node:buffer';
|
|
2
2
|
import {StringDecoder} from 'node:string_decoder';
|
|
3
|
+
import {getStringEncoding} from '../arguments/encoding-option.js';
|
|
3
4
|
import {isUint8Array, bufferToUint8Array} from '../utils/uint-array.js';
|
|
4
5
|
|
|
5
6
|
/*
|
|
@@ -49,3 +50,17 @@ const encodingStringFinal = function * (stringDecoder) {
|
|
|
49
50
|
yield lastChunk;
|
|
50
51
|
}
|
|
51
52
|
};
|
|
53
|
+
|
|
54
|
+
/*
|
|
55
|
+
Once the user's transform has run, its string chunks are converted to bytes: by `Transform.push()` with the asynchronous methods, and by `joinToString()` with the synchronous ones. Both use UTF-8, yet the result is then decoded with the `encoding` option, which would corrupt the text when that option is a different text encoding.
|
|
56
|
+
Therefore, those chunks are converted using the `encoding` option beforehand.
|
|
57
|
+
With the `binary` transform option, they are converted with UTF-8 even when the `encoding` option is another text encoding. Otherwise, with synchronous methods, the next transform would receive the string itself, while with asynchronous methods it receives those UTF-8 bytes decoded with the `encoding` option.
|
|
58
|
+
When the `encoding` option is UTF-8, strings round-trip unchanged, so there is nothing to do.
|
|
59
|
+
*/
|
|
60
|
+
export const getStringToBytesGenerator = (binary, encoding, skipped) => skipped || encoding === 'utf8'
|
|
61
|
+
? undefined
|
|
62
|
+
: {transform: stringToBytesGenerator.bind(undefined, getStringEncoding(binary, encoding))};
|
|
63
|
+
|
|
64
|
+
const stringToBytesGenerator = function * (stringEncoding, chunk) {
|
|
65
|
+
yield typeof chunk === 'string' ? bufferToUint8Array(Buffer.from(chunk, stringEncoding)) : chunk;
|
|
66
|
+
};
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {Transform, getDefaultHighWaterMark} from 'node:stream';
|
|
2
|
+
import {getStringEncoding} from '../arguments/encoding-option.js';
|
|
2
3
|
import {isAsyncGenerator} from '../stdio/type.js';
|
|
3
4
|
import {getSplitLinesGenerator, getAppendNewlineGenerator} from './split.js';
|
|
4
5
|
import {getValidateTransformInput, getValidateTransformReturn} from './validate.js';
|
|
5
|
-
import {getEncodingTransformGenerator} from './encoding-transform.js';
|
|
6
|
+
import {getEncodingTransformGenerator, getStringToBytesGenerator} from './encoding-transform.js';
|
|
6
7
|
import {
|
|
7
8
|
pushChunks,
|
|
8
9
|
transformChunk,
|
|
@@ -37,7 +38,13 @@ Transform an array of generator functions into a `Transform` stream.
|
|
|
37
38
|
*/
|
|
38
39
|
export const generatorToStream = ({
|
|
39
40
|
value,
|
|
40
|
-
value: {
|
|
41
|
+
value: {
|
|
42
|
+
transform,
|
|
43
|
+
final,
|
|
44
|
+
binary,
|
|
45
|
+
writableObjectMode,
|
|
46
|
+
readableObjectMode,
|
|
47
|
+
},
|
|
41
48
|
optionName,
|
|
42
49
|
}, {encoding}) => {
|
|
43
50
|
const state = {};
|
|
@@ -60,6 +67,8 @@ export const generatorToStream = ({
|
|
|
60
67
|
writableHighWaterMark: getDefaultHighWaterMark(writableObjectMode),
|
|
61
68
|
readableObjectMode,
|
|
62
69
|
readableHighWaterMark: getDefaultHighWaterMark(readableObjectMode),
|
|
70
|
+
// A string written to the stream is converted to bytes with this encoding, which `getEncodingTransformGenerator()` then decodes with the `encoding` option
|
|
71
|
+
defaultEncoding: getStringEncoding(binary, encoding),
|
|
63
72
|
transform(chunk, encoding, done) {
|
|
64
73
|
transformMethod([chunk, generators, 0], this, done);
|
|
65
74
|
},
|
|
@@ -102,6 +111,8 @@ const addInternalGenerators = (
|
|
|
102
111
|
preserveNewlines,
|
|
103
112
|
readableObjectMode,
|
|
104
113
|
state,
|
|
114
|
+
encoding,
|
|
105
115
|
}),
|
|
116
|
+
getStringToBytesGenerator(binary, encoding, readableObjectMode),
|
|
106
117
|
].filter(Boolean);
|
|
107
118
|
};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import {getStringEncoding} from '../arguments/encoding-option.js';
|
|
2
|
+
|
|
1
3
|
// Split chunks line-wise for generators passed to the `std*` options
|
|
2
4
|
export const getSplitLinesGenerator = (binary, preserveNewlines, skipped, state) => binary || skipped
|
|
3
5
|
? undefined
|
|
@@ -36,15 +38,16 @@ const splitGenerator = function * (state, preserveNewlines, chunk) {
|
|
|
36
38
|
continue;
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
// The `\r` of a `\r\n` sequence is stripped further below, once `previousChunks` has been prepended.
|
|
42
|
+
// Otherwise, a `\r\n` sequence split across two chunks would not be detected.
|
|
43
|
+
let line = chunk.slice(start + 1, preserveNewlines ? end + 1 : end);
|
|
41
44
|
|
|
42
45
|
if (previousChunks.length > 0) {
|
|
43
46
|
line = concatString(previousChunks, line);
|
|
44
47
|
previousChunks = '';
|
|
45
48
|
}
|
|
46
49
|
|
|
47
|
-
yield line;
|
|
50
|
+
yield preserveNewlines ? line : stripCarriageReturn(line, state);
|
|
48
51
|
start = end;
|
|
49
52
|
}
|
|
50
53
|
|
|
@@ -55,13 +58,9 @@ const splitGenerator = function * (state, preserveNewlines, chunk) {
|
|
|
55
58
|
state.previousChunks = previousChunks;
|
|
56
59
|
};
|
|
57
60
|
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
state.isWindowsNewline = end !== 0 && chunk[end - 1] === '\r';
|
|
64
|
-
return state.isWindowsNewline ? 2 : 1;
|
|
61
|
+
const stripCarriageReturn = (line, state) => {
|
|
62
|
+
state.isWindowsNewline = line.endsWith('\r');
|
|
63
|
+
return state.isWindowsNewline ? line.slice(0, -1) : line;
|
|
65
64
|
};
|
|
66
65
|
|
|
67
66
|
const linesFinal = function * ({previousChunks}) {
|
|
@@ -72,29 +71,36 @@ const linesFinal = function * ({previousChunks}) {
|
|
|
72
71
|
|
|
73
72
|
// Unless `preserveNewlines: true` is used, we strip the newline of each line.
|
|
74
73
|
// This re-adds them after the user `transform` code has run.
|
|
75
|
-
|
|
74
|
+
// The re-added newline must be encoded with the `encoding` option when the chunk is bytes: a UTF-8 newline would misalign `utf16le` bytes.
|
|
75
|
+
export const getAppendNewlineGenerator = ({binary, preserveNewlines, readableObjectMode, state, encoding}) => binary || preserveNewlines || readableObjectMode
|
|
76
76
|
? undefined
|
|
77
|
-
: {transform: appendNewlineGenerator.bind(undefined, state)};
|
|
77
|
+
: {transform: appendNewlineGenerator.bind(undefined, state, getStringEncoding(binary, encoding))};
|
|
78
78
|
|
|
79
|
-
const appendNewlineGenerator = function * ({isWindowsNewline = false}, chunk) {
|
|
80
|
-
const {unixNewline, windowsNewline,
|
|
79
|
+
const appendNewlineGenerator = function * ({isWindowsNewline = false}, stringEncoding, chunk) {
|
|
80
|
+
const {unixNewline, windowsNewline, concat} = typeof chunk === 'string'
|
|
81
|
+
? linesStringInfo
|
|
82
|
+
: LINES_BYTES_INFO[stringEncoding];
|
|
81
83
|
|
|
82
|
-
if (chunk
|
|
84
|
+
if (endsWithNewline(chunk, unixNewline)) {
|
|
83
85
|
yield chunk;
|
|
84
86
|
return;
|
|
85
87
|
}
|
|
86
88
|
|
|
87
89
|
const newline = isWindowsNewline ? windowsNewline : unixNewline;
|
|
88
|
-
yield
|
|
90
|
+
yield concat(chunk, newline);
|
|
89
91
|
};
|
|
90
92
|
|
|
93
|
+
const endsWithNewline = (chunk, unixNewline) => typeof chunk === 'string'
|
|
94
|
+
? chunk.endsWith(unixNewline)
|
|
95
|
+
: chunk.length >= unixNewline.length
|
|
96
|
+
&& chunk.subarray(-unixNewline.length).every((byte, index) => byte === unixNewline[index]);
|
|
97
|
+
|
|
91
98
|
const concatString = (firstChunk, secondChunk) => `${firstChunk}${secondChunk}`;
|
|
92
99
|
|
|
93
100
|
const linesStringInfo = {
|
|
94
101
|
windowsNewline: '\r\n',
|
|
95
102
|
unixNewline: '\n',
|
|
96
|
-
|
|
97
|
-
concatBytes: concatString,
|
|
103
|
+
concat: concatString,
|
|
98
104
|
};
|
|
99
105
|
|
|
100
106
|
const concatUint8Array = (firstChunk, secondChunk) => {
|
|
@@ -104,9 +110,17 @@ const concatUint8Array = (firstChunk, secondChunk) => {
|
|
|
104
110
|
return chunk;
|
|
105
111
|
};
|
|
106
112
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
113
|
+
// Only indexed with text encodings, since the `binary` option and binary encodings disable this generator
|
|
114
|
+
const LINES_BYTES_INFO = {
|
|
115
|
+
__proto__: null,
|
|
116
|
+
utf8: {
|
|
117
|
+
windowsNewline: new Uint8Array([0x0D, 0x0A]),
|
|
118
|
+
unixNewline: new Uint8Array([0x0A]),
|
|
119
|
+
concat: concatUint8Array,
|
|
120
|
+
},
|
|
121
|
+
utf16le: {
|
|
122
|
+
windowsNewline: new Uint8Array([0x0D, 0x00, 0x0A, 0x00]),
|
|
123
|
+
unixNewline: new Uint8Array([0x0A, 0x00]),
|
|
124
|
+
concat: concatUint8Array,
|
|
125
|
+
},
|
|
112
126
|
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// The options of methods like `subprocess.pipe()`, `subprocess.readable()` or `sendMessage()` are kept on a null-prototype object, otherwise a polluted `Object.prototype` would inject option values.
|
|
2
|
+
// Like destructuring them, `null` throws.
|
|
3
|
+
export const copyOptions = (options = {}) => {
|
|
4
|
+
if (options === null) {
|
|
5
|
+
throw new TypeError('The options must be an object, not `null`.');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
return {__proto__: null, ...options};
|
|
9
|
+
};
|
|
@@ -4,3 +4,9 @@ export const isStandardStream = stream => STANDARD_STREAMS.includes(stream);
|
|
|
4
4
|
export const STANDARD_STREAMS = [process.stdin, process.stdout, process.stderr];
|
|
5
5
|
export const STANDARD_STREAMS_ALIASES = ['stdin', 'stdout', 'stderr'];
|
|
6
6
|
export const getStreamName = fdNumber => STANDARD_STREAMS_ALIASES[fdNumber] ?? `stdio[${fdNumber}]`;
|
|
7
|
+
|
|
8
|
+
// `subprocess.all` is created by Execa itself, which then pipes `stdout`/`stderr` into it.
|
|
9
|
+
// `verbose` needs to tell that pipe apart from a user pipe, which it disables.
|
|
10
|
+
const allStreamSymbol = Symbol('execa.allStream');
|
|
11
|
+
export const setAllStream = stream => Object.assign(stream, {[allStreamSymbol]: true});
|
|
12
|
+
export const isAllStream = stream => stream?.[allStreamSymbol] === true;
|
|
@@ -35,13 +35,7 @@ const uint8ArraysToStrings = (uint8ArraysOrStrings, encoding) => {
|
|
|
35
35
|
return finalString === '' ? strings : [...strings, finalString];
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
-
export const joinToUint8Array = uint8ArraysOrStrings =>
|
|
39
|
-
if (uint8ArraysOrStrings.length === 1 && isUint8Array(uint8ArraysOrStrings[0])) {
|
|
40
|
-
return uint8ArraysOrStrings[0];
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return concatUint8Arrays(stringsToUint8Arrays(uint8ArraysOrStrings));
|
|
44
|
-
};
|
|
38
|
+
export const joinToUint8Array = uint8ArraysOrStrings => uint8ArraysOrStrings.length === 1 && isUint8Array(uint8ArraysOrStrings[0]) ? uint8ArraysOrStrings[0] : concatUint8Arrays(stringsToUint8Arrays(uint8ArraysOrStrings));
|
|
45
39
|
|
|
46
40
|
const stringsToUint8Arrays = uint8ArraysOrStrings => uint8ArraysOrStrings.map(uint8ArrayOrString => typeof uint8ArrayOrString === 'string'
|
|
47
41
|
? stringToUint8Array(uint8ArrayOrString)
|