-
Notifications
You must be signed in to change notification settings - Fork 325
/
main.ts
238 lines (202 loc) · 8.18 KB
/
main.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
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ----------------------------------------------------------------------------------------- */
import RIL from './ril';
// Install the node runtime abstract.
RIL.install();
import RAL from '../common/ral';
import {
AbstractMessageReader, DataCallback, AbstractMessageWriter, Message, ReadableStreamMessageReader, WriteableStreamMessageWriter,
MessageWriterOptions, MessageReaderOptions, MessageReader, MessageWriter, NullLogger, ConnectionStrategy, ConnectionOptions,
MessageConnection, Logger, createMessageConnection as _createMessageConnection, Disposable
} from '../common/api';
import * as path from 'path';
import * as os from 'os';
import { ChildProcess } from 'child_process';
import { randomBytes } from 'crypto';
import { Server, Socket, createServer, createConnection } from 'net';
export * from '../common/api';
export class IPCMessageReader extends AbstractMessageReader {
private process: NodeJS.Process | ChildProcess;
public constructor(process: NodeJS.Process | ChildProcess) {
super();
this.process = process;
let eventEmitter: NodeJS.EventEmitter = this.process;
eventEmitter.on('error', (error: any) => this.fireError(error));
eventEmitter.on('close', () => this.fireClose());
}
public listen(callback: DataCallback): Disposable {
(this.process as NodeJS.EventEmitter).on('message', callback);
return Disposable.create(() => (this.process as NodeJS.EventEmitter).off('message', callback));
}
}
export class IPCMessageWriter extends AbstractMessageWriter implements MessageWriter {
private process: NodeJS.Process | ChildProcess;
private errorCount: number;
public constructor(process: NodeJS.Process | ChildProcess) {
super();
this.process = process;
this.errorCount = 0;
let eventEmitter: NodeJS.EventEmitter = this.process;
eventEmitter.on('error', (error: any) => this.fireError(error));
eventEmitter.on('close', () => this.fireClose);
}
public write(msg: Message): Promise<void> {
try {
if (typeof this.process.send === 'function') {
(this.process.send as Function)(msg, undefined, undefined, (error: any) => {
if (error) {
this.errorCount++;
this.handleError(error, msg);
} else {
this.errorCount = 0;
}
});
}
return Promise.resolve();
} catch (error) {
this.handleError(error, msg);
return Promise.reject(error);
}
}
private handleError(error: any, msg: Message): void {
this.errorCount++;
this.fireError(error, msg, this.errorCount);
}
public end(): void {
}
}
export class SocketMessageReader extends ReadableStreamMessageReader {
public constructor(socket: Socket, encoding: RAL.MessageBufferEncoding = 'utf-8') {
super(RIL().stream.asReadableStream(socket), encoding);
}
}
export class SocketMessageWriter extends WriteableStreamMessageWriter {
private socket: Socket;
public constructor(socket: Socket, options?: RAL.MessageBufferEncoding | MessageWriterOptions) {
super(RIL().stream.asWritableStream(socket), options);
this.socket = socket;
}
public dispose(): void {
super.dispose();
this.socket.destroy();
}
}
export class StreamMessageReader extends ReadableStreamMessageReader {
public constructor(readable: NodeJS.ReadableStream, encoding?: RAL.MessageBufferEncoding | MessageReaderOptions) {
super(RIL().stream.asReadableStream(readable), encoding);
}
}
export class StreamMessageWriter extends WriteableStreamMessageWriter {
public constructor(writable: NodeJS.WritableStream, options?: RAL.MessageBufferEncoding | MessageWriterOptions) {
super(RIL().stream.asWritableStream(writable), options);
}
}
const XDG_RUNTIME_DIR = process.env['XDG_RUNTIME_DIR'];
const safeIpcPathLengths: Map<NodeJS.Platform, number> = new Map([
['linux', 107],
['darwin', 103]
]);
export function generateRandomPipeName(): string {
const randomSuffix = randomBytes(21).toString('hex');
if (process.platform === 'win32') {
return `\\\\.\\pipe\\vscode-jsonrpc-${randomSuffix}-sock`;
}
let result: string;
if (XDG_RUNTIME_DIR) {
result = path.join(XDG_RUNTIME_DIR, `vscode-ipc-${randomSuffix}.sock`);
} else {
result = path.join(os.tmpdir(), `vscode-${randomSuffix}.sock`);
}
const limit = safeIpcPathLengths.get(process.platform);
if (limit !== undefined && result.length >= limit) {
RIL().console.warn(`WARNING: IPC handle "${result}" is longer than ${limit} characters.`);
}
return result;
}
export interface PipeTransport {
onConnected(): Promise<[MessageReader, MessageWriter]>;
}
export function createClientPipeTransport(pipeName: string, encoding: RAL.MessageBufferEncoding = 'utf-8'): Promise<PipeTransport> {
let connectResolve: (value: [MessageReader, MessageWriter]) => void;
const connected = new Promise<[MessageReader, MessageWriter]>((resolve, _reject) => {
connectResolve = resolve;
});
return new Promise<PipeTransport>((resolve, reject) => {
let server: Server = createServer((socket: Socket) => {
server.close();
connectResolve([
new SocketMessageReader(socket, encoding),
new SocketMessageWriter(socket, encoding)
]);
});
server.on('error', reject);
server.listen(pipeName, () => {
server.removeListener('error', reject);
resolve({
onConnected: () => { return connected; }
});
});
});
}
export function createServerPipeTransport(pipeName: string, encoding: RAL.MessageBufferEncoding = 'utf-8'): [MessageReader, MessageWriter] {
const socket: Socket = createConnection(pipeName);
return [
new SocketMessageReader(socket, encoding),
new SocketMessageWriter(socket, encoding)
];
}
export interface SocketTransport {
onConnected(): Promise<[MessageReader, MessageWriter]>;
}
export function createClientSocketTransport(port: number, encoding: RAL.MessageBufferEncoding = 'utf-8'): Promise<SocketTransport> {
let connectResolve: (value: [MessageReader, MessageWriter]) => void;
const connected = new Promise<[MessageReader, MessageWriter]>((resolve, _reject) => {
connectResolve = resolve;
});
return new Promise<SocketTransport>((resolve, reject) => {
const server: Server = createServer((socket: Socket) => {
server.close();
connectResolve([
new SocketMessageReader(socket, encoding),
new SocketMessageWriter(socket, encoding)
]);
});
server.on('error', reject);
server.listen(port, '127.0.0.1', () => {
server.removeListener('error', reject);
resolve({
onConnected: () => { return connected; }
});
});
});
}
export function createServerSocketTransport(port: number, encoding: RAL.MessageBufferEncoding = 'utf-8'): [MessageReader, MessageWriter] {
const socket: Socket = createConnection(port, '127.0.0.1');
return [
new SocketMessageReader(socket, encoding),
new SocketMessageWriter(socket, encoding)
];
}
function isReadableStream(value: any): value is NodeJS.ReadableStream {
const candidate: NodeJS.ReadableStream = value;
return candidate.read !== undefined && candidate.addListener !== undefined;
}
function isWritableStream(value: any): value is NodeJS.WritableStream {
const candidate: NodeJS.WritableStream = value;
return candidate.write !== undefined && candidate.addListener !== undefined;
}
export function createMessageConnection(reader: MessageReader, writer: MessageWriter, logger?: Logger, options?: ConnectionStrategy | ConnectionOptions): MessageConnection;
export function createMessageConnection(inputStream: NodeJS.ReadableStream, outputStream: NodeJS.WritableStream, logger?: Logger, options?: ConnectionStrategy | ConnectionOptions): MessageConnection;
export function createMessageConnection(input: MessageReader | NodeJS.ReadableStream, output: MessageWriter | NodeJS.WritableStream, logger?: Logger, options?: ConnectionStrategy | ConnectionOptions): MessageConnection {
if (!logger) {
logger = NullLogger;
}
const reader = isReadableStream(input) ? new StreamMessageReader(input) : input;
const writer = isWritableStream(output) ? new StreamMessageWriter(output) : output;
if (ConnectionStrategy.is(options)) {
options = { connectionStrategy: options } as ConnectionOptions;
}
return _createMessageConnection(reader, writer, logger, options);
}