This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathstreamCatcher.ts
162 lines (138 loc) · 4.2 KB
/
streamCatcher.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
/**
* This file contains the stream catcher
* it's basically given an input and out stream
* it takes requests and generates a response from the streams
*/
import * as fs from 'fs';
import {Writable, Readable} from 'stream';
import * as RX from './regExp';
import { EventEmitter } from 'events';
import { platform } from 'os';
interface RequestTask {
command: string | null,
resolve: Function,
reject: Function,
}
export class StreamCatcher extends EventEmitter {
public debug: boolean = false;
private requestQueue: RequestTask[] = [];
private requestRunning: RequestTask | null = null;
private buffer: string[] = [''];
public input: Writable;
logDebug(...args: any[]) {
if (this.debug) {
console.log(...args);
}
}
constructor() {
super();
}
async launch(input: Writable, output: Readable): Promise<string[]> {
this.input = input;
let lastBuffer = '';
let timeout: NodeJS.Timer | null = null;
output.on('data', (buffer) => {
this.logDebug('RAW:', buffer.toString());
this.emit('perl-debug.streamcatcher.data', buffer.toString());
const data = lastBuffer + buffer.toString();
const lines = data.split(/\r\n|\r|\n/);
const firstLine = lines[0];
const lastLine = lines[lines.length - 1];
const commandIsDone = RX.lastCommandLine.test(lastLine);
// xxx: Windows restart workaround
// the windows perl debugger doesn't end the current restart request so we have to
// simulate a proper request end.
if ((platform() === "win32" && RX.restartWarning.test(firstLine)) || timeout) {
if (RX.restartWarning.test(firstLine)) {
this.logDebug('RAW> Waiting to fake end of restart request');
}
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
timeout = null;
if (this.requestRunning) {
this.logDebug('RAW> Fake end of restart request');
// xxx: We might want to simulate all the restart output
this.readline(' DB<0> ');
}
}, 500);
}
if (/\r\n|\r|\n$/.test(data) || commandIsDone) {
lastBuffer = '';
} else {
lastBuffer = lines.pop();
}
lines.forEach(line => this.readline(line));
});
output.on('close', () => {
// xxx: Windows perl debugger just exits on syntax error without "DB<n>"
// If theres stuff left in the buffer we push it and end the request.
if (this.requestRunning) {
this.logDebug('RAW> Fake end of request');
this.readline(lastBuffer);
this.readline('Debugged program terminated. Use q to quit or R to restart,');
this.readline('use o inhibit_exit to avoid stopping after program termination,');
this.readline('h q, h R or h o to get additional info.');
this.readline(' DB<0> ');
}
});
return this.request(null);
}
readline(line) {
this.logDebug('line:', line);
// this.logDebug('data:', [...line]);
this.buffer.push(line);
// Test for command end
if (RX.lastCommandLine.test(line)) {
this.logDebug('END:', line);
const data = this.buffer;
this.buffer = [];
// xxx: We might want to verify the DB nr and the cmd number
this.resolveRequest(data);
}
}
resolveRequest(data) {
const req = this.requestRunning;
if (req) {
if (req.command) {
data.unshift(req.command);
}
req.resolve(data);
// Reset state making room for next task
this.buffer = [];
this.requestRunning = null;
}
this.nextRequest();
}
nextRequest() {
if (!this.requestRunning && this.requestQueue.length) {
// Set new request
this.requestRunning = this.requestQueue.shift();
// this.logOutput(`NEXT: ${this.requestRunning.command}\n`);
// a null command is used for the initial run, in that case we don't need to
// do anything but listen
if (this.requestRunning.command !== null) {
const data = `${this.requestRunning.command}\n`;
this.emit('perl-debug.streamcatcher.write', data);
this.input.write(data);
}
}
}
request(command: string | null): Promise<string[]> {
this.logDebug(command ? `CMD: "${command}"` : 'REQ-INIT');
return new Promise((resolve, reject) => {
// Add our request to the queue
this.requestQueue.push({
command,
resolve,
reject
});
this.nextRequest();
});
}
destroy() {
this.removeAllListeners();
return Promise.resolve();
}
}