-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathstreamProcessing.ts
152 lines (135 loc) · 4.97 KB
/
streamProcessing.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
import hexdump from 'hexer'
import bufferReplace from 'buffer-replace'
import colors from 'ansi-colors'
import binstring from 'binstring'
import { interval, debounce } from 'rxjs'
import { PassThrough, Readable, Writable } from 'stream'
import { ReadLine, createInterface as createReadline, clearLine } from 'readline'
import { SessionMiddleware } from '../api/middleware'
export type InputMode = null | 'local-echo' | 'readline' | 'readline-hex'
export type OutputMode = null | 'hex'
export type NewlineMode = null | 'cr' | 'lf' | 'crlf' | 'implicit_cr' | 'implicit_lf'
export interface StreamProcessingOptions {
inputMode?: InputMode
inputNewlines?: NewlineMode
outputMode?: OutputMode
outputNewlines?: NewlineMode
}
export class TerminalStreamProcessor extends SessionMiddleware {
forceEcho = false
private inputReadline: ReadLine|null = null
private inputPromptVisible = false
private inputReadlineInStream: Readable & Writable
private inputReadlineOutStream: Readable & Writable
private started = false
constructor (private options: StreamProcessingOptions) {
super()
this.inputReadlineInStream = new PassThrough()
this.inputReadlineOutStream = new PassThrough()
this.inputReadlineOutStream.on('data', data => {
this.outputToTerminal.next(Buffer.from(data))
})
this.outputToTerminal$.pipe(debounce(() => interval(500))).subscribe(() => {
if (this.started) {
this.onOutputSettled()
}
})
}
start (): void {
this.inputReadline = createReadline({
input: this.inputReadlineInStream,
output: this.inputReadlineOutStream,
terminal: true,
prompt: this.options.inputMode === 'readline-hex' ? 'hex> ' : '> ',
})
this.inputReadline.on('line', line => {
this.onTerminalInput(Buffer.from(line + '\n'))
this.resetInputPrompt()
})
this.started = true
}
feedFromSession (data: Buffer): void {
if (this.options.inputMode?.startsWith('readline')) {
if (this.inputPromptVisible) {
clearLine(this.inputReadlineOutStream, 0)
this.outputToTerminal.next(Buffer.from('\r'))
this.inputPromptVisible = false
}
}
data = this.replaceNewlines(data, this.options.outputNewlines)
if (this.options.outputMode === 'hex') {
this.outputToTerminal.next(Buffer.concat([
Buffer.from('\r\n'),
Buffer.from(hexdump(data, {
group: 1,
gutter: 4,
divide: colors.gray(' | '),
emptyHuman: colors.gray('╳'),
}).replaceAll('\n', '\r\n')),
Buffer.from('\r\n\n'),
]))
} else {
this.outputToTerminal.next(data)
}
}
feedFromTerminal (data: Buffer): void {
if (this.options.inputMode === 'local-echo' || this.forceEcho) {
this.outputToTerminal.next(this.replaceNewlines(data, 'crlf'))
}
if (this.options.inputMode?.startsWith('readline')) {
this.inputReadlineInStream.write(data)
} else {
this.onTerminalInput(data)
}
}
resize (): void {
if (this.options.inputMode?.startsWith('readline')) {
this.inputReadlineOutStream.emit('resize')
}
}
close (): void {
this.inputReadline?.close()
super.close()
}
private onTerminalInput (data: Buffer) {
if (this.options.inputMode === 'readline-hex') {
const tokens = data.toString().split(/\s/g)
data = Buffer.concat(tokens.filter(t => !!t).map(t => {
if (t.startsWith('0x')) {
t = t.substring(2)
}
return binstring(t, { 'in': 'hex' })
}))
}
data = this.replaceNewlines(data, this.options.inputNewlines)
this.outputToSession.next(data)
}
private onOutputSettled () {
if (this.options.inputMode?.startsWith('readline') && !this.inputPromptVisible) {
this.resetInputPrompt()
}
}
private resetInputPrompt () {
this.outputToTerminal.next(Buffer.from('\r\n'))
this.inputReadline?.prompt(true)
this.inputPromptVisible = true
}
private replaceNewlines (data: Buffer, mode?: NewlineMode): Buffer {
if (!mode) {
return data
} else if (mode === 'implicit_cr') {
return bufferReplace(data, '\n', '\r\n')
} else if (mode === 'implicit_lf') {
return bufferReplace(data, '\r', '\r\n')
}
data = bufferReplace(data, '\r\n', '\n')
data = bufferReplace(data, '\r', '\n')
const replacement = {
strip: '',
cr: '\r',
lf: '\n',
crlf: '\r\n',
}[mode]
return bufferReplace(data, '\n', replacement)
}
}