2021-08-18 21:31:00 +00:00
|
|
|
import * as nodePTY from '@tabby-gang/node-pty'
|
2021-04-04 18:07:57 +00:00
|
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
|
|
import { ipcMain } from 'electron'
|
|
|
|
import { Application } from './app'
|
2021-08-28 18:15:20 +00:00
|
|
|
import { UTF8Splitter } from './utfSplitter'
|
|
|
|
import { Subject, debounceTime } from 'rxjs'
|
2021-04-04 18:07:57 +00:00
|
|
|
|
|
|
|
class PTYDataQueue {
|
|
|
|
private buffers: Buffer[] = []
|
|
|
|
private delta = 0
|
2021-08-19 21:14:38 +00:00
|
|
|
private maxChunk = 1024 * 100
|
|
|
|
private maxDelta = this.maxChunk * 5
|
2021-04-04 18:07:57 +00:00
|
|
|
private flowPaused = false
|
2021-08-28 18:15:20 +00:00
|
|
|
private decoder = new UTF8Splitter()
|
|
|
|
private output$ = new Subject<Buffer>()
|
2021-04-04 18:07:57 +00:00
|
|
|
|
2021-08-28 18:15:20 +00:00
|
|
|
constructor (private pty: nodePTY.IPty, private onData: (data: Buffer) => void) {
|
|
|
|
this.output$.pipe(debounceTime(500)).subscribe(() => {
|
|
|
|
const remainder = this.decoder.flush()
|
|
|
|
if (remainder.length) {
|
|
|
|
this.onData(remainder)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-04-04 18:07:57 +00:00
|
|
|
|
|
|
|
push (data: Buffer) {
|
|
|
|
this.buffers.push(data)
|
|
|
|
this.maybeEmit()
|
|
|
|
}
|
|
|
|
|
|
|
|
ack (length: number) {
|
|
|
|
this.delta -= length
|
|
|
|
this.maybeEmit()
|
|
|
|
}
|
|
|
|
|
|
|
|
private maybeEmit () {
|
|
|
|
if (this.delta <= this.maxDelta && this.flowPaused) {
|
|
|
|
this.resume()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (this.buffers.length > 0) {
|
|
|
|
if (this.delta > this.maxDelta && !this.flowPaused) {
|
|
|
|
this.pause()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const buffersToSend = []
|
|
|
|
let totalLength = 0
|
|
|
|
while (totalLength < this.maxChunk && this.buffers.length) {
|
|
|
|
totalLength += this.buffers[0].length
|
|
|
|
buffersToSend.push(this.buffers.shift())
|
|
|
|
}
|
2021-04-16 18:15:39 +00:00
|
|
|
|
2021-04-16 18:54:21 +00:00
|
|
|
if (buffersToSend.length === 0) {
|
2021-04-16 18:15:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-04 18:07:57 +00:00
|
|
|
let toSend = Buffer.concat(buffersToSend)
|
2021-04-16 18:15:39 +00:00
|
|
|
if (toSend.length > this.maxChunk) {
|
|
|
|
this.buffers.unshift(toSend.slice(this.maxChunk))
|
|
|
|
toSend = toSend.slice(0, this.maxChunk)
|
|
|
|
}
|
2021-05-28 17:39:41 +00:00
|
|
|
this.emitData(toSend)
|
2021-04-04 18:07:57 +00:00
|
|
|
this.delta += toSend.length
|
2021-04-16 18:15:39 +00:00
|
|
|
|
|
|
|
if (this.buffers.length) {
|
|
|
|
setImmediate(() => this.maybeEmit())
|
|
|
|
}
|
2021-04-04 18:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-28 17:39:41 +00:00
|
|
|
private emitData (data: Buffer) {
|
2021-08-28 18:15:20 +00:00
|
|
|
const validChunk = this.decoder.write(data)
|
|
|
|
this.onData(validChunk)
|
|
|
|
this.output$.next(validChunk)
|
2021-05-28 17:39:41 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 18:07:57 +00:00
|
|
|
private pause () {
|
|
|
|
this.pty.pause()
|
|
|
|
this.flowPaused = true
|
|
|
|
}
|
|
|
|
|
|
|
|
private resume () {
|
|
|
|
this.pty.resume()
|
|
|
|
this.flowPaused = false
|
|
|
|
this.maybeEmit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PTY {
|
|
|
|
private pty: nodePTY.IPty
|
|
|
|
private outputQueue: PTYDataQueue
|
2021-12-08 20:45:58 +00:00
|
|
|
exited = false
|
2021-04-04 18:07:57 +00:00
|
|
|
|
|
|
|
constructor (private id: string, private app: Application, ...args: any[]) {
|
|
|
|
this.pty = (nodePTY as any).spawn(...args)
|
|
|
|
for (const key of ['close', 'exit']) {
|
|
|
|
(this.pty as any).on(key, (...eventArgs) => this.emit(key, ...eventArgs))
|
|
|
|
}
|
|
|
|
|
|
|
|
this.outputQueue = new PTYDataQueue(this.pty, data => {
|
fixed zmodem errors - fixes #6677, fixes #5845, fixes #5243, fixes #5132, fixes #5021, fixes #7511, fixes #7053, fixes #6917, fixes #6639, fixes #6259, fixes #6182, fixes #6122, fixes #5845, fixes #5737, fixes #5701, fixes #5609, fixes #5311, fixes #5243, fixes #5231, fixes #5132
2022-11-20 18:25:48 +00:00
|
|
|
setImmediate(() => this.emit('data', data))
|
2021-04-04 18:07:57 +00:00
|
|
|
})
|
|
|
|
|
2021-12-08 20:45:58 +00:00
|
|
|
this.pty.onData(data => this.outputQueue.push(Buffer.from(data)))
|
|
|
|
this.pty.onExit(() => {
|
|
|
|
this.exited = true
|
|
|
|
})
|
2021-04-04 18:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getPID (): number {
|
|
|
|
return this.pty.pid
|
|
|
|
}
|
|
|
|
|
|
|
|
resize (columns: number, rows: number): void {
|
|
|
|
if ((this.pty as any)._writable) {
|
|
|
|
this.pty.resize(columns, rows)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
write (buffer: Buffer): void {
|
|
|
|
if ((this.pty as any)._writable) {
|
2021-06-03 20:46:19 +00:00
|
|
|
this.pty.write(buffer as any)
|
2021-04-04 18:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ackData (length: number): void {
|
|
|
|
this.outputQueue.ack(length)
|
|
|
|
}
|
|
|
|
|
|
|
|
kill (signal?: string): void {
|
|
|
|
this.pty.kill(signal)
|
|
|
|
}
|
|
|
|
|
|
|
|
private emit (event: string, ...args: any[]) {
|
|
|
|
this.app.broadcast(`pty:${this.id}:${event}`, ...args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PTYManager {
|
2021-04-25 18:12:49 +00:00
|
|
|
private ptys: Record<string, PTY|undefined> = {}
|
2021-04-04 18:07:57 +00:00
|
|
|
|
|
|
|
init (app: Application): void {
|
|
|
|
ipcMain.on('pty:spawn', (event, ...options) => {
|
|
|
|
const id = uuidv4().toString()
|
|
|
|
event.returnValue = id
|
|
|
|
this.ptys[id] = new PTY(id, app, ...options)
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.on('pty:exists', (event, id) => {
|
2021-12-12 11:03:01 +00:00
|
|
|
event.returnValue = this.ptys[id] && !this.ptys[id].exited
|
2021-04-04 18:07:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.on('pty:get-pid', (event, id) => {
|
2021-04-25 18:12:49 +00:00
|
|
|
event.returnValue = this.ptys[id]?.getPID()
|
2021-04-04 18:07:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.on('pty:resize', (_event, id, columns, rows) => {
|
2021-04-25 18:12:49 +00:00
|
|
|
this.ptys[id]?.resize(columns, rows)
|
2021-04-04 18:07:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.on('pty:write', (_event, id, data) => {
|
2021-04-25 18:12:49 +00:00
|
|
|
this.ptys[id]?.write(Buffer.from(data))
|
2021-04-04 18:07:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.on('pty:kill', (_event, id, signal) => {
|
2021-04-25 18:12:49 +00:00
|
|
|
this.ptys[id]?.kill(signal)
|
2021-04-04 18:07:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.on('pty:ack-data', (_event, id, length) => {
|
2021-04-25 18:12:49 +00:00
|
|
|
this.ptys[id]?.ackData(length)
|
2021-04-04 18:07:57 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|