@theia/process 1.53.0-next.55 → 1.53.0-next.64
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 +30 -30
- package/lib/common/shell-command-builder.slow-spec.js +4 -4
- package/package.json +3 -3
- package/src/common/process-common-module.ts +22 -22
- package/src/common/process-manager-types.ts +58 -58
- package/src/common/shell-command-builder.slow-spec.ts +486 -486
- package/src/common/shell-command-builder.ts +187 -187
- package/src/common/shell-quoting.spec.ts +176 -176
- package/src/common/shell-quoting.ts +236 -236
- package/src/common/tests/$weird(),file=name.js +1 -1
- package/src/common/tests/white space.js +1 -1
- package/src/node/dev-null-stream.ts +47 -47
- package/src/node/index.ts +22 -22
- package/src/node/multi-ring-buffer.spec.ts +486 -486
- package/src/node/multi-ring-buffer.ts +348 -348
- package/src/node/process-backend-module.ts +67 -67
- package/src/node/process-manager.ts +107 -107
- package/src/node/process.ts +207 -207
- package/src/node/pseudo-pty.ts +56 -56
- package/src/node/raw-process.spec.ts +199 -199
- package/src/node/raw-process.ts +156 -156
- package/src/node/string-argv.d.ts +21 -21
- package/src/node/task-terminal-process.ts +41 -41
- package/src/node/terminal-process.spec.ts +121 -121
- package/src/node/terminal-process.ts +290 -290
- package/src/node/test/process-fork-test.js +22 -22
- package/src/node/test/process-test-container.ts +27 -27
- package/src/node/utils.ts +79 -79
|
@@ -1,290 +1,290 @@
|
|
|
1
|
-
// *****************************************************************************
|
|
2
|
-
// Copyright (C) 2017 Ericsson and others.
|
|
3
|
-
//
|
|
4
|
-
// This program and the accompanying materials are made available under the
|
|
5
|
-
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
-
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
-
//
|
|
8
|
-
// This Source Code may also be made available under the following Secondary
|
|
9
|
-
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
-
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
-
// with the GNU Classpath Exception which is available at
|
|
12
|
-
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
-
//
|
|
14
|
-
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
-
// *****************************************************************************
|
|
16
|
-
|
|
17
|
-
import { injectable, inject, named } from '@theia/core/shared/inversify';
|
|
18
|
-
import { Disposable, DisposableCollection, Emitter, Event, isWindows } from '@theia/core';
|
|
19
|
-
import { ILogger } from '@theia/core/lib/common';
|
|
20
|
-
import { Process, ProcessType, ProcessOptions, /* ProcessErrorEvent */ } from './process';
|
|
21
|
-
import { ProcessManager } from './process-manager';
|
|
22
|
-
import { IPty, spawn } from 'node-pty';
|
|
23
|
-
import { MultiRingBuffer, MultiRingBufferReadableStream } from './multi-ring-buffer';
|
|
24
|
-
import { DevNullStream } from './dev-null-stream';
|
|
25
|
-
import { signame } from './utils';
|
|
26
|
-
import { PseudoPty } from './pseudo-pty';
|
|
27
|
-
import { Writable } from 'stream';
|
|
28
|
-
|
|
29
|
-
export const TerminalProcessOptions = Symbol('TerminalProcessOptions');
|
|
30
|
-
export interface TerminalProcessOptions extends ProcessOptions {
|
|
31
|
-
/**
|
|
32
|
-
* Windows only. Allow passing complex command lines already escaped for CommandLineToArgvW.
|
|
33
|
-
*/
|
|
34
|
-
commandLine?: string;
|
|
35
|
-
isPseudo?: boolean;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export const TerminalProcessFactory = Symbol('TerminalProcessFactory');
|
|
39
|
-
export interface TerminalProcessFactory {
|
|
40
|
-
(options: TerminalProcessOptions): TerminalProcess;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export enum NodePtyErrors {
|
|
44
|
-
EACCES = 'Permission denied',
|
|
45
|
-
ENOENT = 'No such file or directory'
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Run arbitrary processes inside pseudo-terminals (PTY).
|
|
50
|
-
*
|
|
51
|
-
* Note: a PTY is not a shell process (bash/pwsh/cmd...)
|
|
52
|
-
*/
|
|
53
|
-
@injectable()
|
|
54
|
-
export class TerminalProcess extends Process {
|
|
55
|
-
|
|
56
|
-
protected readonly terminal: IPty | undefined;
|
|
57
|
-
private _delayedResizer: DelayedResizer | undefined;
|
|
58
|
-
private _exitCode: number | undefined;
|
|
59
|
-
|
|
60
|
-
readonly outputStream = this.createOutputStream();
|
|
61
|
-
readonly errorStream = new DevNullStream({ autoDestroy: true });
|
|
62
|
-
readonly inputStream: Writable;
|
|
63
|
-
|
|
64
|
-
constructor( // eslint-disable-next-line @typescript-eslint/indent
|
|
65
|
-
@inject(TerminalProcessOptions) protected override readonly options: TerminalProcessOptions,
|
|
66
|
-
@inject(ProcessManager) processManager: ProcessManager,
|
|
67
|
-
@inject(MultiRingBuffer) protected readonly ringBuffer: MultiRingBuffer,
|
|
68
|
-
@inject(ILogger) @named('process') logger: ILogger
|
|
69
|
-
) {
|
|
70
|
-
super(processManager, logger, ProcessType.Terminal, options);
|
|
71
|
-
|
|
72
|
-
if (options.isPseudo) {
|
|
73
|
-
// do not need to spawn a process, new a pseudo pty instead
|
|
74
|
-
this.terminal = new PseudoPty();
|
|
75
|
-
this.inputStream = new DevNullStream({ autoDestroy: true });
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (this.isForkOptions(this.options)) {
|
|
80
|
-
throw new Error('terminal processes cannot be forked as of today');
|
|
81
|
-
}
|
|
82
|
-
this.logger.debug('Starting terminal process', JSON.stringify(options, undefined, 2));
|
|
83
|
-
|
|
84
|
-
// Delay resizes to avoid conpty not respecting very early resize calls
|
|
85
|
-
// see https://github.com/microsoft/vscode/blob/a1c783c/src/vs/platform/terminal/node/terminalProcess.ts#L177
|
|
86
|
-
if (isWindows) {
|
|
87
|
-
this._delayedResizer = new DelayedResizer();
|
|
88
|
-
this._delayedResizer.onTrigger(dimensions => {
|
|
89
|
-
this._delayedResizer?.dispose();
|
|
90
|
-
this._delayedResizer = undefined;
|
|
91
|
-
if (dimensions.cols && dimensions.rows) {
|
|
92
|
-
this.resize(dimensions.cols, dimensions.rows);
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const startTerminal = (command: string): { terminal: IPty | undefined, inputStream: Writable } => {
|
|
98
|
-
try {
|
|
99
|
-
return this.createPseudoTerminal(command, options, ringBuffer);
|
|
100
|
-
} catch (error) {
|
|
101
|
-
// Normalize the error to make it as close as possible as what
|
|
102
|
-
// node's child_process.spawn would generate in the same
|
|
103
|
-
// situation.
|
|
104
|
-
const message: string = error.message;
|
|
105
|
-
|
|
106
|
-
if (message.startsWith('File not found: ') || message.endsWith(NodePtyErrors.ENOENT)) {
|
|
107
|
-
if (isWindows && command && !command.toLowerCase().endsWith('.exe')) {
|
|
108
|
-
const commandExe = command + '.exe';
|
|
109
|
-
this.logger.debug(`Trying terminal command '${commandExe}' because '${command}' was not found.`);
|
|
110
|
-
return startTerminal(commandExe);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// Proceed with failure, reporting the original command because it was
|
|
114
|
-
// the intended command and it was not found
|
|
115
|
-
error.errno = 'ENOENT';
|
|
116
|
-
error.code = 'ENOENT';
|
|
117
|
-
error.path = options.command;
|
|
118
|
-
} else if (message.endsWith(NodePtyErrors.EACCES)) {
|
|
119
|
-
// The shell program exists but was not accessible, so just fail
|
|
120
|
-
error.errno = 'EACCES';
|
|
121
|
-
error.code = 'EACCES';
|
|
122
|
-
error.path = options.command;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// node-pty throws exceptions on Windows.
|
|
126
|
-
// Call the client error handler, but first give them a chance to register it.
|
|
127
|
-
this.emitOnErrorAsync(error);
|
|
128
|
-
|
|
129
|
-
return { terminal: undefined, inputStream: new DevNullStream({ autoDestroy: true }) };
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const { terminal, inputStream } = startTerminal(options.command);
|
|
134
|
-
this.terminal = terminal;
|
|
135
|
-
this.inputStream = inputStream;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Helper for the constructor to attempt to create the pseudo-terminal encapsulating the shell process.
|
|
140
|
-
*
|
|
141
|
-
* @param command the shell command to launch
|
|
142
|
-
* @param options options for the shell process
|
|
143
|
-
* @param ringBuffer a ring buffer in which to collect terminal output
|
|
144
|
-
* @returns the terminal PTY and a stream by which it may be sent input
|
|
145
|
-
*/
|
|
146
|
-
private createPseudoTerminal(command: string, options: TerminalProcessOptions, ringBuffer: MultiRingBuffer): { terminal: IPty | undefined, inputStream: Writable } {
|
|
147
|
-
const terminal = spawn(
|
|
148
|
-
command,
|
|
149
|
-
(isWindows && options.commandLine) || options.args || [],
|
|
150
|
-
options.options || {}
|
|
151
|
-
);
|
|
152
|
-
|
|
153
|
-
process.nextTick(() => this.emitOnStarted());
|
|
154
|
-
|
|
155
|
-
// node-pty actually wait for the underlying streams to be closed before emitting exit.
|
|
156
|
-
// We should emulate the `exit` and `close` sequence.
|
|
157
|
-
terminal.onExit(({ exitCode, signal }) => {
|
|
158
|
-
// Make sure to only pass either code or signal as !undefined, not
|
|
159
|
-
// both.
|
|
160
|
-
//
|
|
161
|
-
// node-pty quirk: On Linux/macOS, if the process exited through the
|
|
162
|
-
// exit syscall (with an exit code), signal will be 0 (an invalid
|
|
163
|
-
// signal value). If it was terminated because of a signal, the
|
|
164
|
-
// signal parameter will hold the signal number and code should
|
|
165
|
-
// be ignored.
|
|
166
|
-
this._exitCode = exitCode;
|
|
167
|
-
if (signal === undefined || signal === 0) {
|
|
168
|
-
this.onTerminalExit(exitCode, undefined);
|
|
169
|
-
} else {
|
|
170
|
-
this.onTerminalExit(undefined, signame(signal));
|
|
171
|
-
}
|
|
172
|
-
process.nextTick(() => {
|
|
173
|
-
if (signal === undefined || signal === 0) {
|
|
174
|
-
this.emitOnClose(exitCode, undefined);
|
|
175
|
-
} else {
|
|
176
|
-
this.emitOnClose(undefined, signame(signal));
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
terminal.onData((data: string) => {
|
|
182
|
-
ringBuffer.enq(data);
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
const inputStream = new Writable({
|
|
186
|
-
write: (chunk: string) => {
|
|
187
|
-
this.write(chunk);
|
|
188
|
-
},
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
return { terminal, inputStream };
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
createOutputStream(): MultiRingBufferReadableStream {
|
|
195
|
-
return this.ringBuffer.getStream();
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
get pid(): number {
|
|
199
|
-
this.checkTerminal();
|
|
200
|
-
return this.terminal!.pid;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
get executable(): string {
|
|
204
|
-
return (this.options as ProcessOptions).command;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
get arguments(): string[] {
|
|
208
|
-
return this.options.args || [];
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
protected onTerminalExit(code: number | undefined, signal: string | undefined): void {
|
|
212
|
-
this.emitOnExit(code, signal);
|
|
213
|
-
this.unregisterProcess();
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
unregisterProcess(): void {
|
|
217
|
-
this.processManager.unregister(this);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
kill(signal?: string): void {
|
|
221
|
-
if (this.terminal && this.killed === false) {
|
|
222
|
-
this.terminal.kill(signal);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
resize(cols: number, rows: number): void {
|
|
227
|
-
if (typeof cols !== 'number' || typeof rows !== 'number' || isNaN(cols) || isNaN(rows)) {
|
|
228
|
-
return;
|
|
229
|
-
}
|
|
230
|
-
this.checkTerminal();
|
|
231
|
-
try {
|
|
232
|
-
// Ensure that cols and rows are always >= 1, this prevents a native exception in winpty.
|
|
233
|
-
cols = Math.max(cols, 1);
|
|
234
|
-
rows = Math.max(rows, 1);
|
|
235
|
-
|
|
236
|
-
// Delay resize if needed
|
|
237
|
-
if (this._delayedResizer) {
|
|
238
|
-
this._delayedResizer.cols = cols;
|
|
239
|
-
this._delayedResizer.rows = rows;
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
this.terminal!.resize(cols, rows);
|
|
244
|
-
} catch (error) {
|
|
245
|
-
// swallow error if the pty has already exited
|
|
246
|
-
// see also https://github.com/microsoft/vscode/blob/a1c783c/src/vs/platform/terminal/node/terminalProcess.ts#L549
|
|
247
|
-
if (this._exitCode !== undefined &&
|
|
248
|
-
error.message !== 'ioctl(2) failed, EBADF' &&
|
|
249
|
-
error.message !== 'Cannot resize a pty that has already exited') {
|
|
250
|
-
throw error;
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
write(data: string): void {
|
|
256
|
-
this.checkTerminal();
|
|
257
|
-
this.terminal!.write(data);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
protected checkTerminal(): void | never {
|
|
261
|
-
if (!this.terminal) {
|
|
262
|
-
throw new Error('pty process did not start correctly');
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Tracks the latest resize event to be trigger at a later point.
|
|
270
|
-
*/
|
|
271
|
-
class DelayedResizer extends DisposableCollection {
|
|
272
|
-
rows: number | undefined;
|
|
273
|
-
cols: number | undefined;
|
|
274
|
-
private _timeout: NodeJS.Timeout;
|
|
275
|
-
|
|
276
|
-
private readonly _onTrigger = new Emitter<{ rows?: number; cols?: number }>();
|
|
277
|
-
get onTrigger(): Event<{ rows?: number; cols?: number }> { return this._onTrigger.event; }
|
|
278
|
-
|
|
279
|
-
constructor() {
|
|
280
|
-
super();
|
|
281
|
-
this.push(this._onTrigger);
|
|
282
|
-
this._timeout = setTimeout(() => this._onTrigger.fire({ rows: this.rows, cols: this.cols }), 1000);
|
|
283
|
-
this.push(Disposable.create(() => clearTimeout(this._timeout)));
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
override dispose(): void {
|
|
287
|
-
super.dispose();
|
|
288
|
-
clearTimeout(this._timeout);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2017 Ericsson and others.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import { injectable, inject, named } from '@theia/core/shared/inversify';
|
|
18
|
+
import { Disposable, DisposableCollection, Emitter, Event, isWindows } from '@theia/core';
|
|
19
|
+
import { ILogger } from '@theia/core/lib/common';
|
|
20
|
+
import { Process, ProcessType, ProcessOptions, /* ProcessErrorEvent */ } from './process';
|
|
21
|
+
import { ProcessManager } from './process-manager';
|
|
22
|
+
import { IPty, spawn } from 'node-pty';
|
|
23
|
+
import { MultiRingBuffer, MultiRingBufferReadableStream } from './multi-ring-buffer';
|
|
24
|
+
import { DevNullStream } from './dev-null-stream';
|
|
25
|
+
import { signame } from './utils';
|
|
26
|
+
import { PseudoPty } from './pseudo-pty';
|
|
27
|
+
import { Writable } from 'stream';
|
|
28
|
+
|
|
29
|
+
export const TerminalProcessOptions = Symbol('TerminalProcessOptions');
|
|
30
|
+
export interface TerminalProcessOptions extends ProcessOptions {
|
|
31
|
+
/**
|
|
32
|
+
* Windows only. Allow passing complex command lines already escaped for CommandLineToArgvW.
|
|
33
|
+
*/
|
|
34
|
+
commandLine?: string;
|
|
35
|
+
isPseudo?: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const TerminalProcessFactory = Symbol('TerminalProcessFactory');
|
|
39
|
+
export interface TerminalProcessFactory {
|
|
40
|
+
(options: TerminalProcessOptions): TerminalProcess;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export enum NodePtyErrors {
|
|
44
|
+
EACCES = 'Permission denied',
|
|
45
|
+
ENOENT = 'No such file or directory'
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Run arbitrary processes inside pseudo-terminals (PTY).
|
|
50
|
+
*
|
|
51
|
+
* Note: a PTY is not a shell process (bash/pwsh/cmd...)
|
|
52
|
+
*/
|
|
53
|
+
@injectable()
|
|
54
|
+
export class TerminalProcess extends Process {
|
|
55
|
+
|
|
56
|
+
protected readonly terminal: IPty | undefined;
|
|
57
|
+
private _delayedResizer: DelayedResizer | undefined;
|
|
58
|
+
private _exitCode: number | undefined;
|
|
59
|
+
|
|
60
|
+
readonly outputStream = this.createOutputStream();
|
|
61
|
+
readonly errorStream = new DevNullStream({ autoDestroy: true });
|
|
62
|
+
readonly inputStream: Writable;
|
|
63
|
+
|
|
64
|
+
constructor( // eslint-disable-next-line @typescript-eslint/indent
|
|
65
|
+
@inject(TerminalProcessOptions) protected override readonly options: TerminalProcessOptions,
|
|
66
|
+
@inject(ProcessManager) processManager: ProcessManager,
|
|
67
|
+
@inject(MultiRingBuffer) protected readonly ringBuffer: MultiRingBuffer,
|
|
68
|
+
@inject(ILogger) @named('process') logger: ILogger
|
|
69
|
+
) {
|
|
70
|
+
super(processManager, logger, ProcessType.Terminal, options);
|
|
71
|
+
|
|
72
|
+
if (options.isPseudo) {
|
|
73
|
+
// do not need to spawn a process, new a pseudo pty instead
|
|
74
|
+
this.terminal = new PseudoPty();
|
|
75
|
+
this.inputStream = new DevNullStream({ autoDestroy: true });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (this.isForkOptions(this.options)) {
|
|
80
|
+
throw new Error('terminal processes cannot be forked as of today');
|
|
81
|
+
}
|
|
82
|
+
this.logger.debug('Starting terminal process', JSON.stringify(options, undefined, 2));
|
|
83
|
+
|
|
84
|
+
// Delay resizes to avoid conpty not respecting very early resize calls
|
|
85
|
+
// see https://github.com/microsoft/vscode/blob/a1c783c/src/vs/platform/terminal/node/terminalProcess.ts#L177
|
|
86
|
+
if (isWindows) {
|
|
87
|
+
this._delayedResizer = new DelayedResizer();
|
|
88
|
+
this._delayedResizer.onTrigger(dimensions => {
|
|
89
|
+
this._delayedResizer?.dispose();
|
|
90
|
+
this._delayedResizer = undefined;
|
|
91
|
+
if (dimensions.cols && dimensions.rows) {
|
|
92
|
+
this.resize(dimensions.cols, dimensions.rows);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const startTerminal = (command: string): { terminal: IPty | undefined, inputStream: Writable } => {
|
|
98
|
+
try {
|
|
99
|
+
return this.createPseudoTerminal(command, options, ringBuffer);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
// Normalize the error to make it as close as possible as what
|
|
102
|
+
// node's child_process.spawn would generate in the same
|
|
103
|
+
// situation.
|
|
104
|
+
const message: string = error.message;
|
|
105
|
+
|
|
106
|
+
if (message.startsWith('File not found: ') || message.endsWith(NodePtyErrors.ENOENT)) {
|
|
107
|
+
if (isWindows && command && !command.toLowerCase().endsWith('.exe')) {
|
|
108
|
+
const commandExe = command + '.exe';
|
|
109
|
+
this.logger.debug(`Trying terminal command '${commandExe}' because '${command}' was not found.`);
|
|
110
|
+
return startTerminal(commandExe);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Proceed with failure, reporting the original command because it was
|
|
114
|
+
// the intended command and it was not found
|
|
115
|
+
error.errno = 'ENOENT';
|
|
116
|
+
error.code = 'ENOENT';
|
|
117
|
+
error.path = options.command;
|
|
118
|
+
} else if (message.endsWith(NodePtyErrors.EACCES)) {
|
|
119
|
+
// The shell program exists but was not accessible, so just fail
|
|
120
|
+
error.errno = 'EACCES';
|
|
121
|
+
error.code = 'EACCES';
|
|
122
|
+
error.path = options.command;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// node-pty throws exceptions on Windows.
|
|
126
|
+
// Call the client error handler, but first give them a chance to register it.
|
|
127
|
+
this.emitOnErrorAsync(error);
|
|
128
|
+
|
|
129
|
+
return { terminal: undefined, inputStream: new DevNullStream({ autoDestroy: true }) };
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const { terminal, inputStream } = startTerminal(options.command);
|
|
134
|
+
this.terminal = terminal;
|
|
135
|
+
this.inputStream = inputStream;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Helper for the constructor to attempt to create the pseudo-terminal encapsulating the shell process.
|
|
140
|
+
*
|
|
141
|
+
* @param command the shell command to launch
|
|
142
|
+
* @param options options for the shell process
|
|
143
|
+
* @param ringBuffer a ring buffer in which to collect terminal output
|
|
144
|
+
* @returns the terminal PTY and a stream by which it may be sent input
|
|
145
|
+
*/
|
|
146
|
+
private createPseudoTerminal(command: string, options: TerminalProcessOptions, ringBuffer: MultiRingBuffer): { terminal: IPty | undefined, inputStream: Writable } {
|
|
147
|
+
const terminal = spawn(
|
|
148
|
+
command,
|
|
149
|
+
(isWindows && options.commandLine) || options.args || [],
|
|
150
|
+
options.options || {}
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
process.nextTick(() => this.emitOnStarted());
|
|
154
|
+
|
|
155
|
+
// node-pty actually wait for the underlying streams to be closed before emitting exit.
|
|
156
|
+
// We should emulate the `exit` and `close` sequence.
|
|
157
|
+
terminal.onExit(({ exitCode, signal }) => {
|
|
158
|
+
// Make sure to only pass either code or signal as !undefined, not
|
|
159
|
+
// both.
|
|
160
|
+
//
|
|
161
|
+
// node-pty quirk: On Linux/macOS, if the process exited through the
|
|
162
|
+
// exit syscall (with an exit code), signal will be 0 (an invalid
|
|
163
|
+
// signal value). If it was terminated because of a signal, the
|
|
164
|
+
// signal parameter will hold the signal number and code should
|
|
165
|
+
// be ignored.
|
|
166
|
+
this._exitCode = exitCode;
|
|
167
|
+
if (signal === undefined || signal === 0) {
|
|
168
|
+
this.onTerminalExit(exitCode, undefined);
|
|
169
|
+
} else {
|
|
170
|
+
this.onTerminalExit(undefined, signame(signal));
|
|
171
|
+
}
|
|
172
|
+
process.nextTick(() => {
|
|
173
|
+
if (signal === undefined || signal === 0) {
|
|
174
|
+
this.emitOnClose(exitCode, undefined);
|
|
175
|
+
} else {
|
|
176
|
+
this.emitOnClose(undefined, signame(signal));
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
terminal.onData((data: string) => {
|
|
182
|
+
ringBuffer.enq(data);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const inputStream = new Writable({
|
|
186
|
+
write: (chunk: string) => {
|
|
187
|
+
this.write(chunk);
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
return { terminal, inputStream };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
createOutputStream(): MultiRingBufferReadableStream {
|
|
195
|
+
return this.ringBuffer.getStream();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
get pid(): number {
|
|
199
|
+
this.checkTerminal();
|
|
200
|
+
return this.terminal!.pid;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
get executable(): string {
|
|
204
|
+
return (this.options as ProcessOptions).command;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
get arguments(): string[] {
|
|
208
|
+
return this.options.args || [];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
protected onTerminalExit(code: number | undefined, signal: string | undefined): void {
|
|
212
|
+
this.emitOnExit(code, signal);
|
|
213
|
+
this.unregisterProcess();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
unregisterProcess(): void {
|
|
217
|
+
this.processManager.unregister(this);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
kill(signal?: string): void {
|
|
221
|
+
if (this.terminal && this.killed === false) {
|
|
222
|
+
this.terminal.kill(signal);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
resize(cols: number, rows: number): void {
|
|
227
|
+
if (typeof cols !== 'number' || typeof rows !== 'number' || isNaN(cols) || isNaN(rows)) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
this.checkTerminal();
|
|
231
|
+
try {
|
|
232
|
+
// Ensure that cols and rows are always >= 1, this prevents a native exception in winpty.
|
|
233
|
+
cols = Math.max(cols, 1);
|
|
234
|
+
rows = Math.max(rows, 1);
|
|
235
|
+
|
|
236
|
+
// Delay resize if needed
|
|
237
|
+
if (this._delayedResizer) {
|
|
238
|
+
this._delayedResizer.cols = cols;
|
|
239
|
+
this._delayedResizer.rows = rows;
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
this.terminal!.resize(cols, rows);
|
|
244
|
+
} catch (error) {
|
|
245
|
+
// swallow error if the pty has already exited
|
|
246
|
+
// see also https://github.com/microsoft/vscode/blob/a1c783c/src/vs/platform/terminal/node/terminalProcess.ts#L549
|
|
247
|
+
if (this._exitCode !== undefined &&
|
|
248
|
+
error.message !== 'ioctl(2) failed, EBADF' &&
|
|
249
|
+
error.message !== 'Cannot resize a pty that has already exited') {
|
|
250
|
+
throw error;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
write(data: string): void {
|
|
256
|
+
this.checkTerminal();
|
|
257
|
+
this.terminal!.write(data);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
protected checkTerminal(): void | never {
|
|
261
|
+
if (!this.terminal) {
|
|
262
|
+
throw new Error('pty process did not start correctly');
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Tracks the latest resize event to be trigger at a later point.
|
|
270
|
+
*/
|
|
271
|
+
class DelayedResizer extends DisposableCollection {
|
|
272
|
+
rows: number | undefined;
|
|
273
|
+
cols: number | undefined;
|
|
274
|
+
private _timeout: NodeJS.Timeout;
|
|
275
|
+
|
|
276
|
+
private readonly _onTrigger = new Emitter<{ rows?: number; cols?: number }>();
|
|
277
|
+
get onTrigger(): Event<{ rows?: number; cols?: number }> { return this._onTrigger.event; }
|
|
278
|
+
|
|
279
|
+
constructor() {
|
|
280
|
+
super();
|
|
281
|
+
this.push(this._onTrigger);
|
|
282
|
+
this._timeout = setTimeout(() => this._onTrigger.fire({ rows: this.rows, cols: this.cols }), 1000);
|
|
283
|
+
this.push(Disposable.create(() => clearTimeout(this._timeout)));
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
override dispose(): void {
|
|
287
|
+
super.dispose();
|
|
288
|
+
clearTimeout(this._timeout);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
// *****************************************************************************
|
|
2
|
-
// Copyright (C) 2018 Arm and others.
|
|
3
|
-
//
|
|
4
|
-
// This program and the accompanying materials are made available under the
|
|
5
|
-
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
-
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
-
//
|
|
8
|
-
// This Source Code may also be made available under the following Secondary
|
|
9
|
-
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
-
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
-
// with the GNU Classpath Exception which is available at
|
|
12
|
-
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
-
//
|
|
14
|
-
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
-
// *****************************************************************************
|
|
16
|
-
|
|
17
|
-
if (process.argv[2] === 'version') {
|
|
18
|
-
console.log('1.0.0');
|
|
19
|
-
} else {
|
|
20
|
-
process.stderr.write('Error: Argument expected')
|
|
21
|
-
process.exit(1)
|
|
22
|
-
}
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2018 Arm and others.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
if (process.argv[2] === 'version') {
|
|
18
|
+
console.log('1.0.0');
|
|
19
|
+
} else {
|
|
20
|
+
process.stderr.write('Error: Argument expected')
|
|
21
|
+
process.exit(1)
|
|
22
|
+
}
|