forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocolParser.ts
More file actions
116 lines (109 loc) · 4.17 KB
/
Copy pathprotocolParser.ts
File metadata and controls
116 lines (109 loc) · 4.17 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// tslint:disable:no-constant-condition no-typeof-undefined
import { EventEmitter } from 'events';
import { injectable } from 'inversify';
import { Readable } from 'stream';
import { DebugProtocol } from 'vscode-debugprotocol';
import { IProtocolParser } from '../types';
const PROTOCOL_START_INDENTIFIER = '\r\n\r\n';
/**
* Parsers the debugger Protocol messages and raises the following events:
* 1. 'data', message (for all protocol messages)
* 1. 'event_<event name>', message (for all protocol events)
* 1. 'request_<command name>', message (for all protocol requests)
* 1. 'response_<command name>', message (for all protocol responses)
* 1. '<type>', message (for all protocol messages that are not events, requests nor responses)
* @export
* @class ProtocolParser
* @extends {EventEmitter}
* @implements {IProtocolParser}
*/
@injectable()
export class ProtocolParser extends EventEmitter implements IProtocolParser {
private rawData = new Buffer(0);
private contentLength: number = -1;
private disposed: boolean;
private stream?: Readable;
constructor() {
super();
}
public dispose() {
if (this.stream) {
this.stream.removeListener('data', this.dataCallbackHandler);
this.stream = undefined;
}
}
public connect(stream: Readable) {
this.stream = stream;
stream.addListener('data', this.dataCallbackHandler);
}
private dataCallbackHandler = (data: string | Buffer) => {
this.handleData(data as Buffer);
}
private dispatch(body: string): void {
const message = JSON.parse(body) as DebugProtocol.ProtocolMessage;
switch (message.type) {
case 'event': {
const event = message as DebugProtocol.Event;
if (typeof event.event === 'string') {
this.emit(`${message.type}_${event.event}`, event);
break;
}
}
case 'request': {
const request = message as DebugProtocol.Request;
if (typeof request.command === 'string') {
this.emit(`${message.type}_${request.command}`, request);
break;
}
}
case 'response': {
const reponse = message as DebugProtocol.Response;
if (typeof reponse.command === 'string') {
this.emit(`${message.type}_${reponse.command}`, reponse);
break;
}
}
default: {
this.emit(`${message.type}`, message);
}
}
this.emit('data', message);
}
private handleData(data: Buffer): void {
if (this.disposed) {
return;
}
this.rawData = Buffer.concat([this.rawData, 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) {
this.dispatch(message);
}
// there may be more complete messages to process.
continue;
}
} else {
const idx = this.rawData.indexOf(PROTOCOL_START_INDENTIFIER);
if (idx !== -1) {
const header = this.rawData.toString('utf8', 0, idx);
const lines = header.split('\r\n');
for (const line of lines) {
const pair = line.split(/: +/);
if (pair[0] === 'Content-Length') {
this.contentLength = +pair[1];
}
}
this.rawData = this.rawData.slice(idx + PROTOCOL_START_INDENTIFIER.length);
continue;
}
}
break;
}
}
}