-
Notifications
You must be signed in to change notification settings - Fork 116
/
ProxyPty.ts
331 lines (271 loc) · 8.49 KB
/
ProxyPty.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright 2018 Simon Edwards <simon@simonzone.com>
*
* This source code is licensed under the MIT license which is detailed in the LICENSE.txt file.
*/
import * as child_process from 'child_process';
import {Event, BufferSizeChange, Pty, Logger} from 'extraterm-extension-api';
import { EventEmitter } from 'extraterm-event-emitter';
export interface EnvironmentMap {
[key:string]: string;
}
export interface PtyOptions {
exe?: string;
args?: string[];
cols?: number;
rows?: number;
cwd?: string;
env?: EnvironmentMap;
extraEnv?: EnvironmentMap;
}
const DEBUG_FINE = false;
const MAXIMUM_WRITE_BUFFER_SIZE = 64 * 1024;
const TYPE_CREATE = "create";
const TYPE_CREATED = "created";
const TYPE_WRITE = "write";
const TYPE_OUTPUT = "output";
const TYPE_RESIZE = "resize";
const TYPE_CLOSE = "close";
const TYPE_CLOSED = "closed";
const TYPE_TERMINATE = "terminate";
const TYPE_PERMIT_DATA_SIZE = "permit-data-size";
const TYPE_OUTPUT_WRITTEN = "output-written";
interface ProxyMessage {
type: string;
id: number;
}
interface CreatePtyMessage extends ProxyMessage {
argv: string[];
rows: number;
columns: number;
env: { [key: string]: string; };
extraEnv: { [key: string]: string; };
cwd: string;
// the id field is not user for this message type.
}
interface CreatedPtyMessage extends ProxyMessage {
}
interface WriteMessage extends ProxyMessage {
data: string;
}
interface ResizeMessage extends ProxyMessage {
rows: number;
columns: number;
}
interface CloseMessage extends ProxyMessage {
}
interface PermitDataSizeMessage extends ProxyMessage {
size: number;
}
// Output generated by the process on the other side of the pty.
interface OutputMessage extends ProxyMessage {
data: string;
}
interface OutputWrittenMessage extends ProxyMessage {
chars: number;
}
interface ClosedMessage extends ProxyMessage {
}
interface TerminateMessage extends ProxyMessage {
}
const NULL_ID = -1;
class ProxyPty implements Pty {
private _id: number = NULL_ID;
private _writeFunc: (id: number, msg: ProxyMessage) => void = null;
// Pre-open write queue.
private _writeQueue: ProxyMessage[] = [];
private _live = true;
private _outstandingWriteDataCount = 0;
private _onDataEventEmitter = new EventEmitter<string>();
private _onExitEventEmitter = new EventEmitter<void>();
private _onAvailableWriteBufferSizeChangeEventEmitter = new EventEmitter<BufferSizeChange>();
onData: Event<string>;
onExit: Event<void>;
onAvailableWriteBufferSizeChange: Event<BufferSizeChange>;
constructor(writeFunc) {
this._writeFunc = writeFunc;
this.onData = this._onDataEventEmitter.event;
this.onExit = this._onExitEventEmitter.event;
this.onAvailableWriteBufferSizeChange = this._onAvailableWriteBufferSizeChangeEventEmitter.event;
}
getId(): number {
return this._id;
}
setId(id: number): void {
this._id = id;
if (this._live) {
this._writeQueue.forEach( (msg) => {
msg.id = this._id;
this._writeFunc(this._id, msg);
});
this._writeQueue = [];
}
}
private _writeMessage(id: number, msg: ProxyMessage): void {
if (this._live) {
if (this._id === -1) {
// We don't know what the ID of the pty in the proxy is.
// Queue up this message for later.
this._writeQueue.push(msg);
} else {
this._writeFunc(this._id, msg);
}
}
}
_charsWritten(chars: number): void {
this._outstandingWriteDataCount -= chars;
this._onAvailableWriteBufferSizeChangeEventEmitter.fire(
{totalBufferSize: MAXIMUM_WRITE_BUFFER_SIZE, availableDelta: chars});
}
write(data: string): void {
this._outstandingWriteDataCount += data.length;
const msg: WriteMessage = { type: TYPE_WRITE, id: this._id, data: data };
this._writeMessage(this._id, msg);
}
getAvailableWriteBufferSize(): number {
return MAXIMUM_WRITE_BUFFER_SIZE - this._outstandingWriteDataCount;
}
resize(cols: number, rows: number): void {
const msg: ResizeMessage = { type: TYPE_RESIZE, id: this._id, rows: rows, columns: cols };
this._writeMessage(this._id, msg);
}
permittedDataSize(size: number): void {
const msg: PermitDataSizeMessage = { type: TYPE_PERMIT_DATA_SIZE, id: this._id, size: size };
this._writeMessage(this._id, msg);
}
data(data: string): void {
if (this._live) {
this._onDataEventEmitter.fire(data);
}
}
exit(): void {
this._onExitEventEmitter.fire(undefined);
this._live = false;
}
destroy(): void {
const msg: CloseMessage = { type: TYPE_CLOSE, id: this._id };
this._writeMessage(this._id, msg);
}
}
export abstract class ProxyPtyConnector {
private _ptys: ProxyPty[] = [];
private _messageBuffer = "";
private _proxy: child_process.ChildProcess = null;
constructor(private _log: Logger) {
}
start(): void {
this._proxy = this._spawnServer();
this._proxy.stdout.on('data', (data: Buffer) => {
if (DEBUG_FINE) {
this._log.debug("server -> main: ", data.toString("utf8"));
}
this._messageBuffer = this._messageBuffer + data.toString('utf8');
this._processMessageBuffer();
});
this._proxy.stderr.on('data', (data: Buffer) => {
this._log.warn('ptyproxy process stderr: ', data.toString());
});
this._proxy.on('close', code => {
if (DEBUG_FINE) {
this._log.debug('bridge process closed with code: ', code);
}
});
this._proxy.on('exit', code => {
if (DEBUG_FINE) {
this._log.debug('bridge process exited with code: ', code);
}
});
this._proxy.on('error', (err) => {
this._log.severe("Failed to start server process. ", err);
});
}
protected abstract _spawnServer(): child_process.ChildProcess;
spawn(options: PtyOptions): Pty {
let rows = 24;
let columns = 80;
const file = options.exe;
const args = options.args;
if (DEBUG_FINE) {
this._log.debug("ptyproxy spawn file: ", file);
}
if (options !== undefined) {
rows = options.rows !== undefined ? options.rows : rows;
columns = options.cols !== undefined ? options.cols : columns;
}
const pty = new ProxyPty(this._sendMessage.bind(this));
this._ptys.push(pty);
const msg: CreatePtyMessage = { type: TYPE_CREATE, argv: [file, ...args], rows: rows, columns: columns,
id: NULL_ID, env: options.env, extraEnv: options.extraEnv == null ? {} : options.extraEnv, cwd: options.cwd || null};
this._sendMessage(null, msg);
return pty;
}
destroy(): void {
const msg: TerminateMessage = { type: TYPE_TERMINATE, id: -1 };
this._sendMessage(null, msg);
}
private _processMessageBuffer(): void {
while (true) {
const end = this._messageBuffer.indexOf('\n');
if (end !== -1) {
const msgString = this._messageBuffer.slice(0, end);
this._messageBuffer = this._messageBuffer.slice(end+1);
const msg = <ProxyMessage> JSON.parse(msgString);
this._processMessage(msg);
} else {
break;
}
}
}
private _processMessage(msg: ProxyMessage): void {
const msgType = msg.type;
if (msgType === TYPE_CREATED) {
const createdPtyMsg = <CreatedPtyMessage> msg;
for (let i=0; i<this._ptys.length; i++) {
const pty = this._ptys[i];
if (pty.getId() === NULL_ID) {
pty.setId(createdPtyMsg.id);
break;
}
}
return;
}
if (msgType === TYPE_OUTPUT) {
const outputMsg = <OutputMessage> msg;
const pty = this._findPtyById(outputMsg.id);
if (pty !== null) {
pty.data(outputMsg.data);
}
return;
}
if (msgType === TYPE_CLOSED) {
const closedMsg = <ClosedMessage> msg;
const pty = this._findPtyById(closedMsg.id);
if (pty !== null) {
pty.exit();
}
return;
}
if (msgType === TYPE_OUTPUT_WRITTEN) {
const outputWrittenMsg = <OutputWrittenMessage> msg;
const pty = this._findPtyById(outputWrittenMsg.id);
if (pty !== null) {
pty._charsWritten(outputWrittenMsg.chars);
}
}
}
private _findPtyById(id: number): ProxyPty {
for (let i=0; i<this._ptys.length; i++) {
if (this._ptys[i].getId() === id) {
return this._ptys[i];
}
}
return null;
}
private _sendMessage(id: number, msg: ProxyMessage): void {
const msgText = JSON.stringify(msg);
if (DEBUG_FINE) {
this._log.debug("main -> server: ", msgText);
}
this._proxy.stdin.write(msgText + "\n", 'utf8');
}
}