From f1558f1861e4d0579e543fb7ee2f24682c3d84ee Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:19:01 +0800 Subject: [PATCH] Import apps/android/src/services/terminal-attachment.ts --- .../src/services/terminal-attachment.ts | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 apps/android/src/services/terminal-attachment.ts diff --git a/apps/android/src/services/terminal-attachment.ts b/apps/android/src/services/terminal-attachment.ts new file mode 100644 index 0000000..e7658a1 --- /dev/null +++ b/apps/android/src/services/terminal-attachment.ts @@ -0,0 +1,89 @@ +import type { RemoteGatewayStream } from '@dsh-remote/client-core' +import { createNativeRpcId } from './rpc-id' +import type { SessionTools, TerminalFrame, TerminalInfo } from './session-tools' + +/** One physical attachment. Never reused on reconnect; uncertain input is never replayed. */ +export class TerminalAttachment { + private readonly controller = new AbortController() + private readonly attachmentId = createNativeRpcId() + private stream?: RemoteGatewayStream + private retention?: RemoteGatewayStream + private sequence?: number + private writable = false + private queuedBytes = 0 + private writes: Promise = Promise.resolve() + + constructor(private readonly tools: SessionTools, private readonly sessionId: string, + private readonly terminalId: string, private readonly maxInputBytes: number, + private readonly render: (data: string, reset: boolean) => Promise, + private readonly state: (info: TerminalInfo, writable: boolean) => void) {} + + async follow(): Promise { + try { + this.retention = await this.tools.retainTerminal(this.sessionId, this.terminalId, this.controller.signal) + const retained = await this.retention[Symbol.asyncIterator]().next() + if (retained.done || (retained.value as { type?: string })?.type !== 'retained') throw new Error('Terminal retention failed') + if (this.controller.signal.aborted) return + const stream = await this.tools.followTerminal(this.sessionId, this.terminalId, this.attachmentId, this.controller.signal) + this.stream = stream + if (this.controller.signal.aborted) return + for await (const value of stream) { + if (this.controller.signal.aborted) return + const frame = value as TerminalFrame + if (frame.type === 'snapshot') { + if (this.sequence !== undefined || !Number.isSafeInteger(frame.sequence) || typeof frame.screen !== 'string') throw new Error('Invalid terminal snapshot') + this.sequence = frame.sequence + await this.render(frame.screen, true) + this.updateState(frame.info) + } else if (frame.type === 'output') { + if (this.sequence === undefined || frame.sequence !== this.sequence + 1 || typeof frame.data !== 'string') throw new Error('Terminal output out of order') + this.sequence = frame.sequence + await this.render(frame.data, false) + } else if (frame.type === 'state') { + this.updateState(frame.info) + } else throw new Error('Invalid terminal frame') + } + if (!this.controller.signal.aborted) throw new Error('Terminal stream ended') + } finally { + this.writable = false + this.controller.abort() + await Promise.all([this.stream?.close().catch(() => undefined), this.retention?.close().catch(() => undefined)]) + } + } + + write(data: string): Promise { + const bytes = new TextEncoder().encode(data).byteLength + if (!this.writable || this.controller.signal.aborted || bytes > this.maxInputBytes || this.queuedBytes + bytes > 65536) { + return Promise.reject(new Error('Terminal input unavailable')) + } + this.queuedBytes += bytes + const operation = this.writes.then(async () => { + if (!this.writable || this.controller.signal.aborted) throw new Error('Terminal detached') + await this.tools.writeTerminal(this.sessionId, this.terminalId, this.attachmentId, data) + }).catch(error => { + this.dispose() + throw error + }).finally(() => { this.queuedBytes -= bytes }) + this.writes = operation.catch(() => undefined) + return operation + } + + async resize(cols: number, rows: number): Promise { + if (!this.writable || this.controller.signal.aborted) return + await this.tools.resizeTerminal(this.sessionId, this.terminalId, this.attachmentId, cols, rows) + } + + dispose(): void { + this.writable = false + this.controller.abort() + void this.stream?.close().catch(() => undefined) + void this.retention?.close().catch(() => undefined) + } + + private updateState(info: TerminalInfo): void { + if (info?.id !== this.terminalId) throw new Error('Unexpected terminal identity') + this.writable = !this.controller.signal.aborted && this.sequence !== undefined && info.state === 'running' + && info.controllerId === this.attachmentId + this.state(info, this.writable) + } +}