pty.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import * as nodePTY from '@tabby-gang/node-pty'
  2. import { v4 as uuidv4 } from 'uuid'
  3. import { ipcMain } from 'electron'
  4. import { Application } from './app'
  5. import { UTF8Splitter } from './utfSplitter'
  6. import { Subject, debounceTime } from 'rxjs'
  7. class PTYDataQueue {
  8. private buffers: Buffer[] = []
  9. private delta = 0
  10. private maxChunk = 1024 * 100
  11. private maxDelta = this.maxChunk * 5
  12. private flowPaused = false
  13. private decoder = new UTF8Splitter()
  14. private output$ = new Subject<Buffer>()
  15. constructor (private pty: nodePTY.IPty, private onData: (data: Buffer) => void) {
  16. this.output$.pipe(debounceTime(500)).subscribe(() => {
  17. const remainder = this.decoder.flush()
  18. if (remainder.length) {
  19. this.onData(remainder)
  20. }
  21. })
  22. }
  23. push (data: Buffer) {
  24. this.buffers.push(data)
  25. this.maybeEmit()
  26. }
  27. ack (length: number) {
  28. this.delta -= length
  29. this.maybeEmit()
  30. }
  31. private maybeEmit () {
  32. if (this.delta <= this.maxDelta && this.flowPaused) {
  33. this.resume()
  34. return
  35. }
  36. if (this.buffers.length > 0) {
  37. if (this.delta > this.maxDelta && !this.flowPaused) {
  38. this.pause()
  39. return
  40. }
  41. const buffersToSend = []
  42. let totalLength = 0
  43. while (totalLength < this.maxChunk && this.buffers.length) {
  44. totalLength += this.buffers[0].length
  45. buffersToSend.push(this.buffers.shift())
  46. }
  47. if (buffersToSend.length === 0) {
  48. return
  49. }
  50. let toSend = Buffer.concat(buffersToSend)
  51. if (toSend.length > this.maxChunk) {
  52. this.buffers.unshift(toSend.slice(this.maxChunk))
  53. toSend = toSend.slice(0, this.maxChunk)
  54. }
  55. this.emitData(toSend)
  56. this.delta += toSend.length
  57. if (this.buffers.length) {
  58. setImmediate(() => this.maybeEmit())
  59. }
  60. }
  61. }
  62. private emitData (data: Buffer) {
  63. const validChunk = this.decoder.write(data)
  64. this.onData(validChunk)
  65. this.output$.next(validChunk)
  66. }
  67. private pause () {
  68. this.pty.pause()
  69. this.flowPaused = true
  70. }
  71. private resume () {
  72. this.pty.resume()
  73. this.flowPaused = false
  74. this.maybeEmit()
  75. }
  76. }
  77. export class PTY {
  78. private pty: nodePTY.IPty
  79. private outputQueue: PTYDataQueue
  80. exited = false
  81. constructor (private id: string, private app: Application, ...args: any[]) {
  82. this.pty = (nodePTY as any).spawn(...args)
  83. for (const key of ['close', 'exit']) {
  84. (this.pty as any).on(key, (...eventArgs) => this.emit(key, ...eventArgs))
  85. }
  86. this.outputQueue = new PTYDataQueue(this.pty, data => {
  87. setImmediate(() => this.emit('data', data))
  88. })
  89. this.pty.onData(data => this.outputQueue.push(Buffer.from(data)))
  90. this.pty.onExit(() => {
  91. this.exited = true
  92. })
  93. }
  94. getPID (): number {
  95. return this.pty.pid
  96. }
  97. resize (columns: number, rows: number): void {
  98. if ((this.pty as any)._writable) {
  99. this.pty.resize(columns, rows)
  100. }
  101. }
  102. write (buffer: Buffer): void {
  103. if ((this.pty as any)._writable) {
  104. this.pty.write(buffer as any)
  105. }
  106. }
  107. ackData (length: number): void {
  108. this.outputQueue.ack(length)
  109. }
  110. kill (signal?: string): void {
  111. this.pty.kill(signal)
  112. }
  113. private emit (event: string, ...args: any[]) {
  114. this.app.broadcast(`pty:${this.id}:${event}`, ...args)
  115. }
  116. }
  117. export class PTYManager {
  118. private ptys: Record<string, PTY|undefined> = {}
  119. init (app: Application): void {
  120. ipcMain.on('pty:spawn', (event, ...options) => {
  121. const id = uuidv4().toString()
  122. event.returnValue = id
  123. this.ptys[id] = new PTY(id, app, ...options)
  124. })
  125. ipcMain.on('pty:exists', (event, id) => {
  126. event.returnValue = this.ptys[id] && !this.ptys[id].exited
  127. })
  128. ipcMain.on('pty:get-pid', (event, id) => {
  129. event.returnValue = this.ptys[id]?.getPID()
  130. })
  131. ipcMain.on('pty:resize', (_event, id, columns, rows) => {
  132. this.ptys[id]?.resize(columns, rows)
  133. })
  134. ipcMain.on('pty:write', (_event, id, data) => {
  135. this.ptys[id]?.write(Buffer.from(data))
  136. })
  137. ipcMain.on('pty:kill', (_event, id, signal) => {
  138. this.ptys[id]?.kill(signal)
  139. })
  140. ipcMain.on('pty:ack-data', (_event, id, length) => {
  141. this.ptys[id]?.ackData(length)
  142. })
  143. }
  144. }