dsh-vscode-workbench 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +16 -2
- package/README.md +18 -2
- package/lib/client.js +45149 -3374
- package/lib/client.js.map +1 -1
- package/lib/index.js +252 -10
- package/lib/index.js.map +1 -1
- package/lib/types/client/Icon.d.ts +1 -1
- package/lib/types/client/TerminalPanel.d.ts +3 -0
- package/lib/types/client/Workbench.d.ts +3 -0
- package/lib/types/client/documentPreview.d.ts +11 -0
- package/lib/types/client/shortcuts.d.ts +4 -0
- package/lib/types/client/terminal.d.ts +43 -0
- package/lib/types/host.d.ts +10 -0
- package/lib/types/terminal.d.ts +41 -0
- package/package.json +6 -1
- package/src/client/Icon.tsx +1 -1
- package/src/client/TerminalPanel.tsx +90 -0
- package/src/client/Workbench.tsx +8 -6
- package/src/client/documentPreview.tsx +122 -0
- package/src/client/index.tsx +3 -0
- package/src/client/shortcuts.ts +17 -1
- package/src/client/styles.ts +9 -1
- package/src/client/terminal.ts +83 -0
- package/src/host.ts +8 -0
- package/src/index.ts +16 -3
- package/src/terminal.ts +145 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { TerminalInfo } from '../terminal.ts'
|
|
2
|
+
|
|
3
|
+
export interface TerminalConnection {
|
|
4
|
+
write(data: string): void
|
|
5
|
+
resize(cols: number, rows: number): void
|
|
6
|
+
close(): void
|
|
7
|
+
subscribe(listener: (output: { sequence: number; data: string; replay?: boolean }) => void): () => void
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface TerminalWorkspaceSnapshot { terminals: readonly TerminalInfo[]; loading: boolean; error?: string }
|
|
11
|
+
|
|
12
|
+
/** Browser client for the workbench's local terminal transport. */
|
|
13
|
+
export interface TerminalWorkspace {
|
|
14
|
+
refresh(): Promise<void>
|
|
15
|
+
create(title?: string): Promise<TerminalInfo>
|
|
16
|
+
close(id: string): Promise<void>
|
|
17
|
+
rename(id: string, title: string): Promise<void>
|
|
18
|
+
connect(id: string): TerminalConnection
|
|
19
|
+
getSnapshot(): TerminalWorkspaceSnapshot
|
|
20
|
+
subscribe(listener: () => void): () => void
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function request<T>(method: string, body: Record<string, unknown>): Promise<T> {
|
|
24
|
+
const response = await fetch(`/dsh-vscode-terminal/api/${method}`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) })
|
|
25
|
+
const text = await response.text()
|
|
26
|
+
let answer: { ok?: boolean; value?: T; error?: { message?: string } }
|
|
27
|
+
try { answer = JSON.parse(text) as { ok?: boolean; value?: T; error?: { message?: string } } } catch { throw new Error(`terminal request returned invalid JSON (${response.status})`) }
|
|
28
|
+
if (!response.ok || answer.ok !== true) throw new Error(answer.error?.message ?? 'terminal request failed')
|
|
29
|
+
return answer.value as T
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
class SocketConnection implements TerminalConnection {
|
|
33
|
+
private readonly socket: WebSocket
|
|
34
|
+
private readonly listeners = new Set<(output: { sequence: number; data: string; replay?: boolean }) => void>()
|
|
35
|
+
private queue: string[] = []
|
|
36
|
+
|
|
37
|
+
constructor(sessionId: string, id: string) {
|
|
38
|
+
const protocol = location.protocol === 'https:' ? 'wss' : 'ws'
|
|
39
|
+
this.socket = new WebSocket(`${protocol}://${location.host}/dsh-vscode-terminal/ws?sessionId=${encodeURIComponent(sessionId)}&id=${encodeURIComponent(id)}`)
|
|
40
|
+
this.socket.onopen = () => { for (const value of this.queue) this.socket.send(value); this.queue = [] }
|
|
41
|
+
this.socket.onmessage = event => {
|
|
42
|
+
try {
|
|
43
|
+
const value = JSON.parse(String(event.data)) as { type?: unknown; sequence?: unknown; data?: unknown }
|
|
44
|
+
if (typeof value.sequence !== 'number' || typeof value.data !== 'string') return
|
|
45
|
+
for (const listener of this.listeners) listener({ sequence: value.sequence, data: value.data, replay: value.type === 'replay' })
|
|
46
|
+
} catch { /* Ignore malformed WebSocket messages from a disconnected server. */ }
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private send(value: unknown): void { const text = JSON.stringify(value); if (this.socket.readyState === WebSocket.OPEN) this.socket.send(text); else this.queue.push(text) }
|
|
51
|
+
write(data: string): void { this.send({ type: 'input', data }) }
|
|
52
|
+
resize(cols: number, rows: number): void { this.send({ type: 'resize', cols, rows }) }
|
|
53
|
+
close(): void { this.socket.close() }
|
|
54
|
+
subscribe(listener: (output: { sequence: number; data: string; replay?: boolean }) => void): () => void { this.listeners.add(listener); return () => this.listeners.delete(listener) }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Identify automatic terminal-emulator replies that must not be echoed during replay. */
|
|
58
|
+
export function isTerminalProtocolReply(data: string): boolean {
|
|
59
|
+
return /^\x1b\](?:10|11|12);(?:rgb:[\da-f]{4}\/[\da-f]{4}\/[\da-f]{4}|#[\da-f]{6})(?:\x07|\x1b\\)$/i.test(data)
|
|
60
|
+
|| /^\x1b\[\??[\d;]*[cR]$/.test(data)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Maintain one terminal collection for a DSH session. */
|
|
64
|
+
export class TerminalWorkspaceController implements TerminalWorkspace {
|
|
65
|
+
private snapshot: TerminalWorkspaceSnapshot = { terminals: [], loading: false }
|
|
66
|
+
private readonly listeners = new Set<() => void>()
|
|
67
|
+
|
|
68
|
+
constructor(private readonly sessionId: string) {}
|
|
69
|
+
|
|
70
|
+
async refresh(): Promise<void> {
|
|
71
|
+
this.set({ ...this.snapshot, loading: true, error: undefined })
|
|
72
|
+
try { this.set({ terminals: await request<TerminalInfo[]>('list', { sessionId: this.sessionId }), loading: false }) }
|
|
73
|
+
catch (error) { this.set({ ...this.snapshot, loading: false, error: error instanceof Error ? error.message : String(error) }) }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async create(title?: string): Promise<TerminalInfo> { const terminal = await request<TerminalInfo>('create', { sessionId: this.sessionId, ...(title === undefined ? {} : { title }) }); await this.refresh(); return terminal }
|
|
77
|
+
async close(id: string): Promise<void> { await request('close', { sessionId: this.sessionId, id }); await this.refresh() }
|
|
78
|
+
async rename(id: string, title: string): Promise<void> { await request('rename', { sessionId: this.sessionId, id, title }); await this.refresh() }
|
|
79
|
+
connect(id: string): TerminalConnection { return new SocketConnection(this.sessionId, id) }
|
|
80
|
+
getSnapshot(): TerminalWorkspaceSnapshot { return this.snapshot }
|
|
81
|
+
subscribe(listener: () => void): () => void { this.listeners.add(listener); return () => this.listeners.delete(listener) }
|
|
82
|
+
private set(snapshot: TerminalWorkspaceSnapshot): void { this.snapshot = snapshot; for (const listener of this.listeners) listener() }
|
|
83
|
+
}
|
package/src/host.ts
CHANGED
|
@@ -125,8 +125,16 @@ export async function deleteEntry(cwd: string, path: string): Promise<void> {
|
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
const IMAGE_TYPES:Record<string,string>={'.avif':'image/avif','.bmp':'image/bmp','.gif':'image/gif','.ico':'image/x-icon','.jpeg':'image/jpeg','.jpg':'image/jpeg','.png':'image/png','.svg':'image/svg+xml','.webp':'image/webp'}
|
|
128
|
+
const PREVIEW_TYPES:Record<string,string>={...IMAGE_TYPES,'.pdf':'application/pdf','.doc':'application/msword','.docx':'application/vnd.openxmlformats-officedocument.wordprocessingml.document','.odt':'application/vnd.oasis.opendocument.text'}
|
|
128
129
|
/** Resolve an image inside the workspace for protected browser preview. */
|
|
129
130
|
export async function workspaceImage(cwd:string,path:string):Promise<{path:string;type:string}>{const resolved=await workspacePath(cwd,path);const type=IMAGE_TYPES[extname(resolved.absolute).toLowerCase()];if(type===undefined)throw new WorkbenchError('unsupported-media','file is not a supported image',415);return {path:resolved.absolute,type}}
|
|
131
|
+
/** Resolve a supported preview file inside the workspace. */
|
|
132
|
+
export async function workspacePreviewFile(cwd:string,path:string):Promise<{path:string;type:string}>{const resolved=await workspacePath(cwd,path);const type=PREVIEW_TYPES[extname(resolved.absolute).toLowerCase()];if(type===undefined)throw new WorkbenchError('unsupported-media','file is not a supported preview format',415);return {path:resolved.absolute,type}}
|
|
133
|
+
|
|
134
|
+
/** Resolve the PDF.js worker shipped with this plugin's runtime dependency. */
|
|
135
|
+
export function pdfWorkerAsset(): { path: string; type: string } {
|
|
136
|
+
return { path: require.resolve('pdfjs-dist/legacy/build/pdf.worker.min.mjs'), type: 'text/javascript; charset=utf-8' }
|
|
137
|
+
}
|
|
130
138
|
|
|
131
139
|
interface ProcessResult { stdout: string; stderr: string; code: number }
|
|
132
140
|
export function run(command: string, args: string[], cwd: string, timeout = PROCESS_TIMEOUT, signal?: AbortSignal): Promise<ProcessResult> {
|
package/src/index.ts
CHANGED
|
@@ -6,7 +6,8 @@ import type { Duplex } from 'node:stream'
|
|
|
6
6
|
import chokidar from 'chokidar'
|
|
7
7
|
import { WebSocket, WebSocketServer } from 'ws'
|
|
8
8
|
import type { HostContext } from './context.ts'
|
|
9
|
-
import { codiconAsset, createEntry, cwdFor, deleteEntry, gitAction, gitStatus, listFiles, monacoAsset, readDocument, renameEntry, saveDocument, searchWorkspace, uploadFile,
|
|
9
|
+
import { codiconAsset, createEntry, cwdFor, deleteEntry, gitAction, gitStatus, listFiles, monacoAsset, pdfWorkerAsset, readDocument, renameEntry, saveDocument, searchWorkspace, uploadFile, workspacePreviewFile } from './host.ts'
|
|
10
|
+
import { installTerminal } from './terminal.ts'
|
|
10
11
|
import type { SearchRequest } from './types.ts'
|
|
11
12
|
import { readBinaryBody, readBody, send, sendError, stringField, trusted, WorkbenchError } from './wire.ts'
|
|
12
13
|
|
|
@@ -33,6 +34,7 @@ async function dispatch(ctx: HostContext, method: string, body: Record<string, u
|
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
export function apply(ctx: HostContext): void {
|
|
37
|
+
installTerminal(ctx)
|
|
36
38
|
ctx.effect(() => ctx.webServer.register({
|
|
37
39
|
kind: 'exact', path: '/dsh-vscode/codicons/codicon.ttf', handler: async (req, res) => {
|
|
38
40
|
if (!trusted(req, ctx.webRuntime.trustedHosts)) { res.writeHead(403); res.end(); return }
|
|
@@ -44,6 +46,17 @@ export function apply(ctx: HostContext): void {
|
|
|
44
46
|
},
|
|
45
47
|
}), 'dsh-vscode-workbench: Codicon font')
|
|
46
48
|
|
|
49
|
+
ctx.effect(() => ctx.webServer.register({
|
|
50
|
+
kind: 'exact', path: '/dsh-vscode/pdfjs/worker.mjs', handler: async (req, res) => {
|
|
51
|
+
if (!trusted(req, ctx.webRuntime.trustedHosts)) { res.writeHead(403); res.end(); return }
|
|
52
|
+
try {
|
|
53
|
+
const asset = pdfWorkerAsset()
|
|
54
|
+
res.writeHead(200, { 'content-type': asset.type, 'cache-control': 'public, max-age=31536000, immutable' })
|
|
55
|
+
res.end(await readFile(asset.path))
|
|
56
|
+
} catch (error) { sendError(res, error) }
|
|
57
|
+
},
|
|
58
|
+
}), 'dsh-vscode-workbench: PDF.js worker')
|
|
59
|
+
|
|
47
60
|
ctx.effect(() => ctx.webServer.register({
|
|
48
61
|
kind: 'prefix', path: '/dsh-vscode/api', handler: async (req, res) => {
|
|
49
62
|
if (!trusted(req, ctx.webRuntime.trustedHosts)) return send(res, 403, { ok: false, error: { code: 'forbidden', message: 'forbidden' } })
|
|
@@ -72,8 +85,8 @@ export function apply(ctx: HostContext): void {
|
|
|
72
85
|
}), 'dsh-vscode-workbench: file upload')
|
|
73
86
|
|
|
74
87
|
ctx.effect(() => ctx.webServer.register({
|
|
75
|
-
kind:'prefix',path:'/dsh-vscode/file',handler:async(req,res)=>{if(!trusted(req,ctx.webRuntime.trustedHosts)){res.writeHead(403);res.end();return}try{const url=new URL(req.url??'/','http://dsh.local');const sessionId=url.searchParams.get('sessionId');const path=url.searchParams.get('path');if(sessionId===null||path===null)throw new WorkbenchError('bad-request','sessionId and path are required');const asset=await
|
|
76
|
-
}), 'dsh-vscode-workbench:
|
|
88
|
+
kind:'prefix',path:'/dsh-vscode/file',handler:async(req,res)=>{if(!trusted(req,ctx.webRuntime.trustedHosts)){res.writeHead(403);res.end();return}try{const url=new URL(req.url??'/','http://dsh.local');const sessionId=url.searchParams.get('sessionId');const path=url.searchParams.get('path');if(sessionId===null||path===null)throw new WorkbenchError('bad-request','sessionId and path are required');const asset=await workspacePreviewFile(cwdFor(ctx,sessionId),path);res.writeHead(200,{'content-type':asset.type,'x-content-type-options':'nosniff','cache-control':'no-store'});res.end(await readFile(asset.path))}catch(error){sendError(res,error)}}
|
|
89
|
+
}), 'dsh-vscode-workbench: file preview')
|
|
77
90
|
|
|
78
91
|
ctx.effect(() => ctx.webServer.register({
|
|
79
92
|
kind: 'prefix', path: '/dsh-vscode/monaco', handler: async (req, res) => {
|
package/src/terminal.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import { realpathSync } from 'node:fs'
|
|
3
|
+
import { basename } from 'node:path'
|
|
4
|
+
import type { IncomingMessage } from 'node:http'
|
|
5
|
+
import type { Duplex } from 'node:stream'
|
|
6
|
+
import * as pty from 'node-pty'
|
|
7
|
+
import type { IPty } from 'node-pty'
|
|
8
|
+
import { WebSocket, WebSocketServer } from 'ws'
|
|
9
|
+
import type { HostContext } from './context.ts'
|
|
10
|
+
import { cwdFor } from './host.ts'
|
|
11
|
+
import { readBody, send, sendError } from './wire.ts'
|
|
12
|
+
|
|
13
|
+
export interface TerminalInfo { id: string; title: string; cwd: string; running: boolean; exitCode?: number; sequence: number }
|
|
14
|
+
export interface TerminalOutput { sequence: number; data: string }
|
|
15
|
+
|
|
16
|
+
interface TerminalHandle { info: TerminalInfo; process: IPty; replay: string; listeners: Set<(output: TerminalOutput) => void> }
|
|
17
|
+
const SENSITIVE = /(api[_-]?key|token|credential|secret|password|passwd|private[_-]?key|^dsh_.*(?:key|token|secret))/i
|
|
18
|
+
const MAX_TERMINALS = 8
|
|
19
|
+
const REPLAY_BYTES = 2 * 1024 * 1024
|
|
20
|
+
|
|
21
|
+
/** Remove credentials while retaining ordinary interactive-shell variables. */
|
|
22
|
+
export function sanitizedTerminalEnv(env: NodeJS.ProcessEnv): Record<string, string> {
|
|
23
|
+
return Object.fromEntries(Object.entries(env).filter(([key, value]) => value !== undefined && !SENSITIVE.test(key)) as Array<[string, string]>)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Select the user's interactive shell with platform-specific fallbacks. */
|
|
27
|
+
export function defaultTerminalShell(platform = process.platform, env = process.env): { shell: string; args: string[] } {
|
|
28
|
+
if (platform === 'win32') return { shell: env.COMSPEC?.toLowerCase().includes('powershell') ? env.COMSPEC : 'powershell.exe', args: ['-NoLogo'] }
|
|
29
|
+
return { shell: env.SHELL || (platform === 'darwin' ? '/bin/zsh' : '/bin/bash'), args: ['-l'] }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Own interactive terminals by their canonical workspace directory. */
|
|
33
|
+
export class TerminalManager {
|
|
34
|
+
private readonly workspaces = new Map<string, Map<string, TerminalHandle>>()
|
|
35
|
+
|
|
36
|
+
private workspace(cwd: string): string { return realpathSync(cwd) }
|
|
37
|
+
|
|
38
|
+
list(cwd: string): TerminalInfo[] { return [...(this.workspaces.get(this.workspace(cwd))?.values() ?? [])].map(handle => ({ ...handle.info })) }
|
|
39
|
+
|
|
40
|
+
create(cwd: string, title?: string): TerminalInfo {
|
|
41
|
+
const key = this.workspace(cwd)
|
|
42
|
+
const terminals = this.workspaces.get(key) ?? new Map<string, TerminalHandle>()
|
|
43
|
+
this.workspaces.set(key, terminals)
|
|
44
|
+
if (terminals.size >= MAX_TERMINALS) throw new Error(`terminal limit reached (${MAX_TERMINALS})`)
|
|
45
|
+
const { shell, args } = defaultTerminalShell()
|
|
46
|
+
const child = pty.spawn(shell, args, { name: 'xterm-256color', cols: 80, rows: 24, cwd: key, env: sanitizedTerminalEnv(process.env) })
|
|
47
|
+
const info: TerminalInfo = { id: randomUUID(), title: title || basename(shell), cwd: key, running: true, sequence: 0 }
|
|
48
|
+
const handle: TerminalHandle = { info, process: child, replay: '', listeners: new Set() }
|
|
49
|
+
terminals.set(info.id, handle)
|
|
50
|
+
child.onData(data => {
|
|
51
|
+
info.sequence++
|
|
52
|
+
handle.replay += data
|
|
53
|
+
if (Buffer.byteLength(handle.replay) > REPLAY_BYTES) handle.replay = handle.replay.slice(-REPLAY_BYTES)
|
|
54
|
+
for (const listener of handle.listeners) listener({ sequence: info.sequence, data })
|
|
55
|
+
})
|
|
56
|
+
child.onExit(({ exitCode }) => {
|
|
57
|
+
info.running = false
|
|
58
|
+
info.exitCode = exitCode
|
|
59
|
+
info.sequence++
|
|
60
|
+
const data = `\r\n[process exited with code ${exitCode}]\r\n`
|
|
61
|
+
for (const listener of handle.listeners) listener({ sequence: info.sequence, data })
|
|
62
|
+
})
|
|
63
|
+
return { ...info }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
get(cwd: string, id: string): TerminalHandle | undefined { return this.workspaces.get(this.workspace(cwd))?.get(id) }
|
|
67
|
+
|
|
68
|
+
close(cwd: string, id: string): void {
|
|
69
|
+
const key = this.workspace(cwd)
|
|
70
|
+
const terminals = this.workspaces.get(key)
|
|
71
|
+
const handle = terminals?.get(id)
|
|
72
|
+
if (handle === undefined || terminals === undefined) return
|
|
73
|
+
terminals.delete(id)
|
|
74
|
+
try { handle.process.kill() } catch { /* The shell may already have exited. */ }
|
|
75
|
+
if (terminals.size === 0) this.workspaces.delete(key)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
rename(cwd: string, id: string, title: string): TerminalInfo {
|
|
79
|
+
const handle = this.get(cwd, id)
|
|
80
|
+
if (handle === undefined) throw new Error('terminal not found')
|
|
81
|
+
handle.info.title = title
|
|
82
|
+
return { ...handle.info }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
dispose(): void { for (const [cwd, terminals] of this.workspaces) for (const id of terminals.keys()) this.close(cwd, id) }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function terminalRequestAllowed(req: IncomingMessage): boolean {
|
|
89
|
+
const host = req.headers.host
|
|
90
|
+
if (host === undefined || req.headers['sec-fetch-site'] === 'cross-site') return false
|
|
91
|
+
let hostname: string
|
|
92
|
+
try { hostname = new URL(`http://${host}`).hostname } catch { return false }
|
|
93
|
+
if (hostname !== 'localhost' && hostname !== '[::1]' && !hostname.startsWith('127.')) return false
|
|
94
|
+
const origin = req.headers.origin
|
|
95
|
+
try { return origin === undefined || new URL(origin).host === host } catch { return false }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Register the private local terminal API and WebSocket transport. */
|
|
99
|
+
export function installTerminal(ctx: HostContext): void {
|
|
100
|
+
const manager = new TerminalManager()
|
|
101
|
+
ctx.effect(() => ctx.webServer.register({ kind: 'prefix', path: '/dsh-vscode-terminal/api', handler: async (req, res) => {
|
|
102
|
+
if (!terminalRequestAllowed(req)) return send(res, 403, { ok: false, error: { message: 'forbidden' } })
|
|
103
|
+
if (req.method !== 'POST') return send(res, 405, { ok: false, error: { message: 'POST required' } })
|
|
104
|
+
try {
|
|
105
|
+
const body = await readBody(req)
|
|
106
|
+
const sessionId = typeof body.sessionId === 'string' ? body.sessionId : ''
|
|
107
|
+
if (sessionId === '') throw new Error('sessionId required')
|
|
108
|
+
const cwd = cwdFor(ctx, sessionId)
|
|
109
|
+
const method = new URL(req.url ?? '/', 'http://dsh.local').pathname.slice('/dsh-vscode-terminal/api/'.length)
|
|
110
|
+
let value: unknown
|
|
111
|
+
if (method === 'list') value = manager.list(cwd)
|
|
112
|
+
else if (method === 'create') value = manager.create(cwd, typeof body.title === 'string' ? body.title : undefined)
|
|
113
|
+
else if (method === 'close') { manager.close(cwd, typeof body.id === 'string' ? body.id : ''); value = manager.list(cwd) }
|
|
114
|
+
else if (method === 'rename') value = manager.rename(cwd, typeof body.id === 'string' ? body.id : '', typeof body.title === 'string' ? body.title : '')
|
|
115
|
+
else throw new Error('unknown terminal method')
|
|
116
|
+
send(res, 200, { ok: true, value })
|
|
117
|
+
} catch (error) { sendError(res, error) }
|
|
118
|
+
} }), 'dsh-vscode-workbench: terminal API')
|
|
119
|
+
|
|
120
|
+
const webSocket = new WebSocketServer({ noServer: true })
|
|
121
|
+
ctx.effect(() => ctx.webServer.registerUpgrade({ path: '/dsh-vscode-terminal/ws', handler(req, socket, head) {
|
|
122
|
+
if (!terminalRequestAllowed(req as IncomingMessage)) { socket.destroy(); return }
|
|
123
|
+
webSocket.handleUpgrade(req as IncomingMessage, socket as Duplex, head, connection => {
|
|
124
|
+
const url = new URL(req.url ?? '/', 'http://dsh.local')
|
|
125
|
+
const sessionId = url.searchParams.get('sessionId')
|
|
126
|
+
const id = url.searchParams.get('id')
|
|
127
|
+
if (sessionId === null || id === null) { connection.close(1008, 'sessionId and id required'); return }
|
|
128
|
+
const handle = manager.get(cwdFor(ctx, sessionId), id)
|
|
129
|
+
if (handle === undefined) { connection.close(1008, 'terminal not found'); return }
|
|
130
|
+
if (handle.replay !== '') connection.send(JSON.stringify({ type: 'replay', sequence: handle.info.sequence, data: handle.replay }))
|
|
131
|
+
const listener = (output: TerminalOutput) => { if (connection.readyState === WebSocket.OPEN) connection.send(JSON.stringify({ type: 'output', ...output })) }
|
|
132
|
+
handle.listeners.add(listener)
|
|
133
|
+
connection.on('message', raw => {
|
|
134
|
+
const text = raw.toString()
|
|
135
|
+
try {
|
|
136
|
+
const message: unknown = JSON.parse(text)
|
|
137
|
+
if (message !== null && typeof message === 'object' && 'type' in message && message.type === 'input' && 'data' in message && typeof message.data === 'string') handle.process.write(message.data)
|
|
138
|
+
else if (message !== null && typeof message === 'object' && 'type' in message && message.type === 'resize' && 'cols' in message && 'rows' in message && typeof message.cols === 'number' && typeof message.rows === 'number') handle.process.resize(Math.max(2, Math.floor(message.cols)), Math.max(2, Math.floor(message.rows)))
|
|
139
|
+
} catch { handle.process.write(text) }
|
|
140
|
+
})
|
|
141
|
+
connection.on('close', () => handle.listeners.delete(listener))
|
|
142
|
+
})
|
|
143
|
+
} }), 'dsh-vscode-workbench: terminal WebSocket')
|
|
144
|
+
ctx.effect(() => () => { webSocket.close(); manager.dispose() }, 'dsh-vscode-workbench: terminal teardown')
|
|
145
|
+
}
|