forked from microsoft/vscode-node-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeV8Protocol.ts
More file actions
503 lines (416 loc) · 11.9 KB
/
Copy pathnodeV8Protocol.ts
File metadata and controls
503 lines (416 loc) · 11.9 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as EE from 'events';
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();
export class NodeV8Message {
seq: number;
type: 'request' | 'response' | 'event';
public constructor(type: 'request' | 'response' | 'event') {
this.seq = 0;
this.type = type;
}
}
export class NodeV8Response extends NodeV8Message {
request_seq: number;
success: boolean;
running: boolean;
command: string;
message: string;
body: any;
refs: V8Object[];
public constructor(request: NodeV8Response, message?: string) {
super('response');
this.request_seq = request.seq;
this.command = request.command;
if (message) {
this.success = false;
this.message = message;
} else {
this.success = true;
}
}
}
export class NodeV8Event extends NodeV8Message {
event: string;
body: V8EventBody;
public constructor(event: string, body?: any) {
super('event');
this.event = event;
if (body) {
this.body = body;
}
}
}
// response types
export interface V8Handle {
handle: number;
type: 'undefined' | 'null' | 'boolean' | 'number' | 'string' | 'object' | 'function' | 'frame';
}
export interface V8Simple extends V8Handle {
value?: boolean | number | string;
}
export interface V8Object extends V8Simple {
vscode_size?: number;
className?: string;
constructorFunction?: V8Ref;
protoObject?: V8Ref;
prototypeObject?: V8Ref;
properties?: V8Property[];
text?: string;
status?: string;
}
export interface V8Function extends V8Object {
name?: string;
inferredName?: string;
}
export interface V8Script extends V8Handle {
name: string;
id: number;
source?: string;
}
export interface V8Ref {
ref: number;
// if resolved, then a value exists
value?: boolean | number | string;
handle?: number;
}
export interface V8Property extends V8Ref {
name: number | string;
}
export interface V8Frame {
index: number;
line: number;
column: number;
script: V8Ref;
func: V8Ref;
receiver: V8Ref;
}
export interface V8Scope {
type: number;
frameIndex : number;
index: number;
object: V8Ref;
}
export interface V8Breakpoint {
type: 'scriptId' | 'scriptRegExp';
script_id: number;
number: number;
script_regexp: string;
}
// responses
export interface V8ScopeResponse extends NodeV8Response {
body: {
vscode_locals?: number;
scopes: V8Scope[];
};
}
export interface V8EvaluateResponse extends NodeV8Response {
body: V8Object;
}
export interface V8BacktraceResponse extends NodeV8Response {
body: {
fromFrame: number;
toFrame: number;
totalFrames: number;
frames: V8Frame[];
};
}
export interface V8ScriptsResponse extends NodeV8Response {
body: V8Script[];
}
export interface V8SetVariableValueResponse extends NodeV8Response {
body: {
newValue: V8Handle;
};
}
export interface V8FrameResponse extends NodeV8Response {
body: V8Frame;
}
export interface V8ListBreakpointsResponse extends NodeV8Response {
body: {
breakpoints: V8Breakpoint[];
};
}
export interface V8SetBreakpointResponse extends NodeV8Response {
body: {
type: string;
breakpoint: number;
script_id: number;
actual_locations: {
line: number;
column: number;
}[];
};
}
export interface V8SetExceptionBreakResponse extends NodeV8Response {
body: {
type: 'all' | 'uncaught';
enabled: boolean;
};
}
// events
export interface V8EventBody {
script: V8Script;
exception: V8Object;
breakpoints: any[];
sourceLine: number;
sourceColumn: number;
sourceLineText: string;
}
// arguments
export interface V8EvaluateArgs {
expression: string;
disable_break?: boolean;
maxStringLength?: number;
global?: boolean;
frame?: number;
additional_context?: {
name: string;
handle: number;
}[];
}
export interface V8ScriptsArgs {
types: number;
includeSource?: boolean;
ids?: number[];
filter?: string;
}
export interface V8SetVariableValueArgs {
scope: {
frameNumber: number;
number: number;
};
name: string;
newValue: {
value: boolean | number | string;
type: string;
};
}
export interface V8FrameArgs {
}
export interface V8ClearBreakpointArgs {
breakpoint: number;
}
export interface V8SetBreakpointArgs {
type : 'function' | 'script' | 'scriptId' | 'scriptRegExp';
target: number | string;
line?: number;
column?: number;
condition?: string;
}
export interface V8SetExceptionBreakArgs {
type : 'all' | 'uncaught';
enabled?: boolean;
}
//---- the protocol implementation
export class NodeV8Protocol extends EE.EventEmitter {
private static TIMEOUT = 10000;
private static TWO_CRLF = '\r\n\r\n';
private _rawData: Buffer;
private _contentLength: number;
private _sequence: number;
private _writableStream: NodeJS.WritableStream;
private _pendingRequests = new Map<number, NodeV8Response>();
private _unresponsiveMode: boolean;
private _responseHook: (response: NodeV8Response) => void;
public embeddedHostVersion: number = -1;
public v8Version: string;
public constructor(responseHook?: (response: NodeV8Response) => void) {
super();
this._responseHook = responseHook;
}
public startDispatch(inStream: NodeJS.ReadableStream, outStream: NodeJS.WritableStream) : void {
this._sequence = 1;
this._writableStream = outStream;
inStream.on('data', (data: Buffer) => this.execute(data));
inStream.on('close', () => {
this.emitEvent(new NodeV8Event('close'));
});
inStream.on('error', (error) => {
this.emitEvent(new NodeV8Event('error'));
});
outStream.on('error', (error) => {
this.emitEvent(new NodeV8Event('error'));
});
inStream.resume();
}
public stop() : void {
if (this._writableStream) {
this._writableStream.end();
}
}
public command(command: string, args?: any, cb?: (response: NodeV8Response) => void) : void {
this._command(command, args, NodeV8Protocol.TIMEOUT, cb);
}
public command2(command: string, args?: any, timeout: number = NodeV8Protocol.TIMEOUT) : Promise<NodeV8Response> {
return new Promise((resolve, reject) => {
this.command(command, args, response => {
if (response.success) {
resolve(response);
} else {
reject(response);
}
});
});
}
public evaluate(args: V8EvaluateArgs, timeout: number = NodeV8Protocol.TIMEOUT) : Promise<V8EvaluateResponse> {
return this.command2('evaluate', args);
}
public scripts(args: V8ScriptsArgs, timeout: number = NodeV8Protocol.TIMEOUT) : Promise<V8ScriptsResponse> {
return this.command2('scripts', args);
}
public setVariableValue(args: V8SetVariableValueArgs, timeout: number = NodeV8Protocol.TIMEOUT) : Promise<V8SetVariableValueResponse> {
return this.command2('setvariablevalue', args);
}
public frame(args: V8FrameArgs, timeout: number = NodeV8Protocol.TIMEOUT) : Promise<V8FrameResponse> {
return this.command2('frame', args);
}
public setBreakpoint(args: V8SetBreakpointArgs, timeout: number = NodeV8Protocol.TIMEOUT) : Promise<V8SetBreakpointResponse> {
return this.command2('setbreakpoint', args);
}
public setExceptionBreak(args: V8SetExceptionBreakArgs, timeout: number = NodeV8Protocol.TIMEOUT) : Promise<V8SetExceptionBreakResponse> {
return this.command2('setexceptionbreak', args);
}
public clearBreakpoint(args: V8ClearBreakpointArgs, timeout: number = NodeV8Protocol.TIMEOUT) : Promise<NodeV8Response> {
return this.command2('clearbreakpoint', args);
}
public listBreakpoints(timeout: number = NodeV8Protocol.TIMEOUT) : Promise<V8ListBreakpointsResponse> {
return this.command2('listbreakpoints');
}
public sendEvent(event: NodeV8Event) : void {
this.send('event', event);
}
public sendResponse(response: NodeV8Response) : void {
if (response.seq > 0) {
// console.error('attempt to send more than one response for command {0}', response.command);
} else {
this.send('response', response);
}
}
// ---- private ------------------------------------------------------------
private _command(command: string, args: any, timeout: number, cb: (response: NodeV8Response) => void) : void {
const request: any = {
command: command
};
if (args && Object.keys(args).length > 0) {
request.arguments = args;
}
if (!this._writableStream) {
if (cb) {
cb(new NodeV8Response(request, localize('not.connected', "not connected to runtime")));
}
return;
}
if (this._unresponsiveMode) {
if (cb) {
cb(new NodeV8Response(request, localize('runtime.unresponsive', "cancelled because Node.js is unresponsive")));
}
return;
}
this.send('request', request);
if (cb) {
this._pendingRequests[request.seq] = cb;
const timer = setTimeout(() => {
clearTimeout(timer);
const clb = this._pendingRequests[request.seq];
if (clb) {
delete this._pendingRequests[request.seq];
clb(new NodeV8Response(request, localize('runtime.timeout', "timeout after {0} ms", timeout)));
this._unresponsiveMode = true;
this.emitEvent(new NodeV8Event('diagnostic', { reason: `request '${command}' timed out'`}));
}
}, timeout);
}
}
private emitEvent(event: NodeV8Event) {
this.emit(event.event, event);
}
private send(typ: 'request' | 'response' | 'event', message: NodeV8Message) : void {
message.type = typ;
message.seq = this._sequence++;
const json = JSON.stringify(message);
const data = 'Content-Length: ' + Buffer.byteLength(json, 'utf8') + '\r\n\r\n' + json;
if (this._writableStream) {
this._writableStream.write(data);
}
}
private internalDispatch(message: NodeV8Message) : void {
switch (message.type) {
case 'event':
const e = <NodeV8Event> message;
this.emitEvent(e);
break;
case 'response':
if (this._unresponsiveMode) {
this._unresponsiveMode = false;
this.emitEvent(new NodeV8Event('diagnostic', { reason: 'responsive' }));
}
const response = <NodeV8Response> message;
const clb = this._pendingRequests[response.request_seq];
if (clb) {
delete this._pendingRequests[response.request_seq];
if (this._responseHook) {
this._responseHook(response);
}
clb(response);
}
break;
default:
break;
}
}
private execute(data: Buffer): void {
this._rawData = this._rawData ? Buffer.concat([this._rawData, data]) : data;
while (true) {
if (this._contentLength >= 0) {
if (this._rawData.length >= this._contentLength) {
const message = this._rawData.toString('utf8', 0, this._contentLength);
this._rawData = this._rawData.slice(this._contentLength);
this._contentLength = -1;
if (message.length > 0) {
try {
this.internalDispatch(JSON.parse(message));
}
catch (e) {
}
}
continue; // there may be more complete messages to process
}
} else {
const idx = this._rawData.indexOf(NodeV8Protocol.TWO_CRLF);
if (idx !== -1) {
const header = this._rawData.toString('utf8', 0, idx);
const lines = header.split('\r\n');
for (let i = 0; i < lines.length; i++) {
const pair = lines[i].split(/: +/);
switch (pair[0]) {
case 'V8-Version':
const match0 = pair[1].match(/(\d+(?:\.\d+)+)/);
if (match0 && match0.length === 2) {
this.v8Version = match0[1];
}
break;
case 'Embedding-Host':
const match = pair[1].match(/node\sv(\d+)\.(\d+)\.(\d+)/);
if (match && match.length === 4) {
this.embeddedHostVersion = (parseInt(match[1])*100 + parseInt(match[2]))*100 + parseInt(match[3]);
} else if (pair[1] === 'Electron') {
this.embeddedHostVersion = 51000; // TODO this needs to be detected in a smarter way by looking at the V8 version in Electron
}
break;
case 'Content-Length':
this._contentLength = +pair[1];
break;
}
}
this._rawData = this._rawData.slice(idx + NodeV8Protocol.TWO_CRLF.length);
continue; // try to handle a complete message
}
}
break;
}
}
}