dsh-ssh-hub 0.0.0-stage → 0.2.0
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/LICENSE +202 -0
- package/README.md +271 -2
- package/cordis.patch.yml +16 -0
- package/icon.svg +3 -0
- package/lib/client.js +13975 -0
- package/lib/index.js +6908 -0
- package/lib/types/activity/history.d.ts +50 -0
- package/lib/types/alerting/notify.d.ts +89 -0
- package/lib/types/alerting/prompt-summary.d.ts +28 -0
- package/lib/types/alerting/rules.d.ts +135 -0
- package/lib/types/alerting/tools.d.ts +30 -0
- package/lib/types/audit/audit-log.d.ts +84 -0
- package/lib/types/audit/recorder.d.ts +50 -0
- package/lib/types/client/api.d.ts +222 -0
- package/lib/types/client/index.d.ts +45 -0
- package/lib/types/client/locales.d.ts +319 -0
- package/lib/types/client/native-panel.d.ts +70 -0
- package/lib/types/client/panel/ClusterTab.d.ts +7 -0
- package/lib/types/client/panel/DeployTab.d.ts +10 -0
- package/lib/types/client/panel/FleetTab.d.ts +10 -0
- package/lib/types/client/panel/HostFormDialog.d.ts +12 -0
- package/lib/types/client/panel/HostsTab.d.ts +25 -0
- package/lib/types/client/panel/LogsTab.d.ts +10 -0
- package/lib/types/client/panel/SshPanel.d.ts +14 -0
- package/lib/types/client/panel/TerminalTab.d.ts +27 -0
- package/lib/types/client/panel/TransferTab.d.ts +7 -0
- package/lib/types/client/panel/TunnelEditDialog.d.ts +15 -0
- package/lib/types/client/panel/TunnelsTab.d.ts +27 -0
- package/lib/types/client/panel/controller.d.ts +253 -0
- package/lib/types/client/panel/helpers.d.ts +45 -0
- package/lib/types/client/panel/tabs.d.ts +62 -0
- package/lib/types/client/panel/xterm.css.d.ts +2 -0
- package/lib/types/client/settings-binding.d.ts +71 -0
- package/lib/types/deploy/orchestrator.d.ts +158 -0
- package/lib/types/deploy/tools.d.ts +14 -0
- package/lib/types/dsh-home.d.ts +16 -0
- package/lib/types/engine/cluster.d.ts +19 -0
- package/lib/types/engine/connection-pool.d.ts +183 -0
- package/lib/types/engine/proxy-command.d.ts +36 -0
- package/lib/types/engine/pty.d.ts +32 -0
- package/lib/types/engine/sftp.d.ts +20 -0
- package/lib/types/engine/terminal-sessions.d.ts +67 -0
- package/lib/types/engine/tunnel-discovery.d.ts +89 -0
- package/lib/types/engine/tunnel-remarks.d.ts +49 -0
- package/lib/types/engine/tunnel.d.ts +76 -0
- package/lib/types/engine.d.ts +103 -0
- package/lib/types/http.d.ts +59 -0
- package/lib/types/index.d.ts +128 -0
- package/lib/types/inspect/checks.d.ts +118 -0
- package/lib/types/inspect/inspection.d.ts +111 -0
- package/lib/types/inspect/tools.d.ts +50 -0
- package/lib/types/logs/search.d.ts +131 -0
- package/lib/types/logs/tools.d.ts +6 -0
- package/lib/types/loopback.d.ts +64 -0
- package/lib/types/monitor/loop.d.ts +133 -0
- package/lib/types/monitor/metrics.d.ts +151 -0
- package/lib/types/mount-once.d.ts +55 -0
- package/lib/types/protocol.d.ts +524 -0
- package/lib/types/routes.d.ts +82 -0
- package/lib/types/security/known-hosts.d.ts +153 -0
- package/lib/types/security/master-secret.d.ts +157 -0
- package/lib/types/security/redact.d.ts +44 -0
- package/lib/types/security/secrets.d.ts +70 -0
- package/lib/types/ssh-config.d.ts +42 -0
- package/lib/types/store.d.ts +183 -0
- package/lib/types/tool-surface.d.ts +72 -0
- package/lib/types/tools.d.ts +56 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +126 -6
- package/src/activity/history.ts +117 -0
- package/src/alerting/notify.ts +212 -0
- package/src/alerting/prompt-summary.ts +71 -0
- package/src/alerting/rules.ts +281 -0
- package/src/alerting/tools.ts +263 -0
- package/src/audit/audit-log.ts +199 -0
- package/src/audit/recorder.ts +73 -0
- package/src/client/api.ts +782 -0
- package/src/client/css-modules.d.ts +5 -0
- package/src/client/index.ts +150 -0
- package/src/client/locales.ts +654 -0
- package/src/client/native-panel.tsx +141 -0
- package/src/client/panel/ClusterTab.tsx +141 -0
- package/src/client/panel/DeployTab.tsx +314 -0
- package/src/client/panel/FleetTab.tsx +494 -0
- package/src/client/panel/HostFormDialog.tsx +289 -0
- package/src/client/panel/HostsTab.tsx +395 -0
- package/src/client/panel/LogsTab.tsx +208 -0
- package/src/client/panel/SshPanel.tsx +105 -0
- package/src/client/panel/TerminalTab.tsx +550 -0
- package/src/client/panel/TransferTab.tsx +238 -0
- package/src/client/panel/TunnelEditDialog.tsx +174 -0
- package/src/client/panel/TunnelsTab.tsx +328 -0
- package/src/client/panel/controller.ts +507 -0
- package/src/client/panel/helpers.ts +84 -0
- package/src/client/panel/panel.module.css +1734 -0
- package/src/client/panel/tabs.ts +52 -0
- package/src/client/panel/xterm.css.ts +2 -0
- package/src/client/settings-binding.ts +184 -0
- package/src/deploy/orchestrator.ts +326 -0
- package/src/deploy/tools.ts +225 -0
- package/src/dsh-home.ts +42 -0
- package/src/engine/cluster.ts +60 -0
- package/src/engine/connection-pool.ts +721 -0
- package/src/engine/proxy-command.ts +248 -0
- package/src/engine/pty.ts +78 -0
- package/src/engine/sftp.ts +229 -0
- package/src/engine/terminal-sessions.ts +280 -0
- package/src/engine/tunnel-discovery.ts +353 -0
- package/src/engine/tunnel-remarks.ts +120 -0
- package/src/engine/tunnel.ts +300 -0
- package/src/engine.ts +195 -0
- package/src/http.ts +127 -0
- package/src/index.ts +690 -0
- package/src/inspect/checks.ts +251 -0
- package/src/inspect/inspection.ts +321 -0
- package/src/inspect/tools.ts +366 -0
- package/src/logs/search.ts +251 -0
- package/src/logs/tools.ts +160 -0
- package/src/loopback.ts +119 -0
- package/src/monitor/loop.ts +265 -0
- package/src/monitor/metrics.ts +333 -0
- package/src/mount-once.ts +142 -0
- package/src/protocol.ts +534 -0
- package/src/routes.ts +998 -0
- package/src/security/known-hosts.ts +342 -0
- package/src/security/master-secret.ts +334 -0
- package/src/security/redact.ts +119 -0
- package/src/security/secrets.ts +152 -0
- package/src/ssh-config.ts +182 -0
- package/src/store.ts +757 -0
- package/src/tool-surface.ts +96 -0
- package/src/tools.ts +550 -0
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSH panel controller: the single owner of the panel's view state.
|
|
3
|
+
*
|
|
4
|
+
* Framework-free (structural runtime faces, task-board core/controller.ts
|
|
5
|
+
* style) so the sidebar row and the React panel share one tiny subscription
|
|
6
|
+
* surface. The state lives only for the browser session (no persistence).
|
|
7
|
+
*
|
|
8
|
+
* The active tab, the hosts-tab connect request, the terminal tabs and their
|
|
9
|
+
* host session ids live here rather than inside the panel components: the
|
|
10
|
+
* layout mounts a keyed page only while its panel is selected, so
|
|
11
|
+
* component-local state would reset the tab on every panel switch and lose
|
|
12
|
+
* the ids a reattach needs.
|
|
13
|
+
*
|
|
14
|
+
* Terminal tabs (issue #1950) are records, not sockets. One tab is the tab
|
|
15
|
+
* shown in the single xterm; a tab that is not shown either owns a detached
|
|
16
|
+
* host session (reattached by id, which replays the host scrollback) or no
|
|
17
|
+
* session at all. The controller is also the place where a still-connecting
|
|
18
|
+
* socket is adopted when a view goes away: a connection that has not reported
|
|
19
|
+
* its session id yet cannot be detached, so dropping it would leave the host
|
|
20
|
+
* PTY with no owner (the orphan the reference implementation produced).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import type { TerminalConnection } from '../api.ts'
|
|
24
|
+
|
|
25
|
+
/** The panel's tab identifiers. */
|
|
26
|
+
export type SshTab = 'hosts' | 'terminal' | 'transfer' | 'tunnels' | 'cluster' | 'deploy' | 'logs' | 'fleet'
|
|
27
|
+
|
|
28
|
+
/** Stable id shared by the sidebar panel row and the main-slot page. */
|
|
29
|
+
export const SSH_PANEL_ID = 'ssh'
|
|
30
|
+
|
|
31
|
+
/** Lifecycle of one terminal tab. */
|
|
32
|
+
export type TerminalTabStatus =
|
|
33
|
+
/** Socket open, the host has not reported a session id yet. */
|
|
34
|
+
| 'connecting'
|
|
35
|
+
/** The host reported a session id; the shell is live. */
|
|
36
|
+
| 'connected'
|
|
37
|
+
/** The remote shell ended. The tab keeps its screen; the session id is cleared. */
|
|
38
|
+
| 'exited'
|
|
39
|
+
/** The connection failed. The tab keeps the reason; the session id is cleared. */
|
|
40
|
+
| 'error'
|
|
41
|
+
|
|
42
|
+
/** A keyboard-interactive challenge waiting for this tab's answer. */
|
|
43
|
+
export interface TerminalAuthPrompt {
|
|
44
|
+
name: string
|
|
45
|
+
instructions: string
|
|
46
|
+
prompts: Array<{ prompt: string; echo: boolean }>
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** One terminal tab, independent of the socket that currently serves it. */
|
|
50
|
+
export interface TerminalTabRecord {
|
|
51
|
+
/** Local tab id, stable across panel switches (never the host session id). */
|
|
52
|
+
id: string
|
|
53
|
+
/** Host alias this tab opened. Several tabs may share one alias. */
|
|
54
|
+
alias: string
|
|
55
|
+
status: TerminalTabStatus
|
|
56
|
+
/** Host-side PTY session id, present only while the shell is live. */
|
|
57
|
+
sessionId?: string
|
|
58
|
+
/** Exit or failure text for the `exited` / `error` states. */
|
|
59
|
+
detail?: string
|
|
60
|
+
/** Code of a close that never reached the host; the tab is kept. */
|
|
61
|
+
closeError?: string
|
|
62
|
+
/** Challenge captured for this tab, whoever was on screen when it arrived. */
|
|
63
|
+
authPrompt?: TerminalAuthPrompt
|
|
64
|
+
/**
|
|
65
|
+
* The answer being typed for {@link TerminalTabRecord.authPrompt}, owned by
|
|
66
|
+
* this tab: one shared draft let a challenge on tab B inherit (and submit)
|
|
67
|
+
* the characters typed for tab A.
|
|
68
|
+
*/
|
|
69
|
+
authAnswers?: string[]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The terminal fields a later step may change on one tab. */
|
|
73
|
+
export type TerminalTabPatch = Omit<TerminalTabRecord, 'id' | 'alias'>
|
|
74
|
+
|
|
75
|
+
/** What to do with an adopted connection once the host reports its session. */
|
|
76
|
+
export type TerminalLinkIntent = 'detach' | 'close'
|
|
77
|
+
|
|
78
|
+
/** Immutable controller snapshot for UI subscriptions. */
|
|
79
|
+
export interface PanelControllerSnapshot {
|
|
80
|
+
/** Whether the center column shows this panel. */
|
|
81
|
+
panelOpen: boolean
|
|
82
|
+
/** The tab the panel is showing. */
|
|
83
|
+
activeTab: SshTab
|
|
84
|
+
/** Every terminal tab, in strip order. */
|
|
85
|
+
terminalTabs: readonly TerminalTabRecord[]
|
|
86
|
+
/** The tab the single xterm is bound to, or undefined when there is none. */
|
|
87
|
+
activeTerminalTabId: string | undefined
|
|
88
|
+
/** Host picked in the terminal toolbar; the next tab opens on it. */
|
|
89
|
+
draftAlias: string
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The layout's panel-navigation face: selecting the panel is the layout's
|
|
94
|
+
* business, and this is the one call that asks for it (null returns the column
|
|
95
|
+
* to the conversation). Optional, so a shell-less composition keeps the pure
|
|
96
|
+
* state transitions testable.
|
|
97
|
+
*/
|
|
98
|
+
export interface PanelNavigationFace {
|
|
99
|
+
/** Select this panel, or null to hand the column back to the conversation. */
|
|
100
|
+
select(panelId: string | null): void
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Controller dependencies. */
|
|
104
|
+
export interface PanelControllerDeps {
|
|
105
|
+
/** Layout panel selection; absent keeps the controller state-only. */
|
|
106
|
+
panel?: PanelNavigationFace
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Byte cap for the retained screen of one tab (the host buffers 256 KiB too). */
|
|
110
|
+
const MAX_RETAINED_BYTES = 256 * 1024
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* UTF-8 byte length of one chunk, without node's Buffer (this half is bundled
|
|
114
|
+
* for the browser). Surrogate pairs count as the four bytes they encode to.
|
|
115
|
+
* @param text - the output chunk.
|
|
116
|
+
* @returns its UTF-8 byte length.
|
|
117
|
+
*/
|
|
118
|
+
function utf8Bytes(text: string): number {
|
|
119
|
+
let bytes = 0
|
|
120
|
+
for (const char of text) {
|
|
121
|
+
const code = char.codePointAt(0) ?? 0
|
|
122
|
+
bytes += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4
|
|
123
|
+
}
|
|
124
|
+
return bytes
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The panel state owner the sidebar row and the view both read. */
|
|
128
|
+
export class PanelController {
|
|
129
|
+
private panelOpen = false
|
|
130
|
+
private activeTab: SshTab = 'hosts'
|
|
131
|
+
private terminalTabs: readonly TerminalTabRecord[] = []
|
|
132
|
+
private activeTerminalTabId: string | undefined
|
|
133
|
+
private draftAlias = ''
|
|
134
|
+
private nextTerminalTabSeq = 0
|
|
135
|
+
private listeners = new Set<() => void>()
|
|
136
|
+
/**
|
|
137
|
+
* Last screen of each tab, kept outside the snapshot: terminal output must
|
|
138
|
+
* not re-render the panel on every chunk, and only the terminal view reads
|
|
139
|
+
* this (when a tab has no session left to replay from).
|
|
140
|
+
*/
|
|
141
|
+
private readonly retainedScreens = new Map<string, { chunks: string[]; bytes: number }>()
|
|
142
|
+
/**
|
|
143
|
+
* Sockets that outlived their view while still connecting. Their session id
|
|
144
|
+
* is unknown, so they cannot be detached yet; keeping them here is what
|
|
145
|
+
* prevents a host PTY with no owner.
|
|
146
|
+
*/
|
|
147
|
+
private readonly adoptedLinks = new Map<string, { connection: TerminalConnection; intent: TerminalLinkIntent }>()
|
|
148
|
+
/** Cached so getSnapshot is referentially stable between changes. */
|
|
149
|
+
private snapshot: PanelControllerSnapshot = {
|
|
150
|
+
panelOpen: false,
|
|
151
|
+
activeTab: 'hosts',
|
|
152
|
+
terminalTabs: [],
|
|
153
|
+
activeTerminalTabId: undefined,
|
|
154
|
+
draftAlias: '',
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
constructor(private readonly deps: PanelControllerDeps = {}) {}
|
|
158
|
+
|
|
159
|
+
getSnapshot(): PanelControllerSnapshot {
|
|
160
|
+
return this.snapshot
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
subscribe(fn: () => void): () => void {
|
|
164
|
+
this.listeners.add(fn)
|
|
165
|
+
return () => { this.listeners.delete(fn) }
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Show the panel. The layout owns which panel the column renders, so the
|
|
170
|
+
* state flip and the selection travel together; the selection is requested
|
|
171
|
+
* after the snapshot flips, so a subscriber never observes "open" while the
|
|
172
|
+
* shell still shows the conversation.
|
|
173
|
+
*/
|
|
174
|
+
open(): void {
|
|
175
|
+
if (this.panelOpen) return
|
|
176
|
+
this.panelOpen = true
|
|
177
|
+
this.commit()
|
|
178
|
+
this.selectPanel(SSH_PANEL_ID)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Return the column to the conversation, asking the layout explicitly. */
|
|
182
|
+
close(): void {
|
|
183
|
+
if (!this.panelOpen) return
|
|
184
|
+
this.panelOpen = false
|
|
185
|
+
this.commit()
|
|
186
|
+
this.selectPanel(null)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
toggle(): void {
|
|
190
|
+
if (this.panelOpen) this.close()
|
|
191
|
+
else this.open()
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Reflect a selection made outside this controller (the user clicked another
|
|
196
|
+
* sidebar row, or the layout dropped the panel id).
|
|
197
|
+
* @param panelId - the layout's current panel id, or null for the conversation.
|
|
198
|
+
*/
|
|
199
|
+
syncPanelSelection(panelId: string | null): void {
|
|
200
|
+
const open = panelId === SSH_PANEL_ID
|
|
201
|
+
if (open === this.panelOpen) return
|
|
202
|
+
this.panelOpen = open
|
|
203
|
+
this.commit()
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Switch tabs, keeping every other piece of view state. */
|
|
207
|
+
setActiveTab(tab: SshTab): void {
|
|
208
|
+
if (this.activeTab === tab) return
|
|
209
|
+
this.activeTab = tab
|
|
210
|
+
this.commit()
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Hand the terminal tab a host to connect to (the hosts-tab "connect"
|
|
215
|
+
* action): the tab exists before the view does, so a panel switch during the
|
|
216
|
+
* handshake still shows the connecting tab and adopts its socket.
|
|
217
|
+
* @param alias - the configured host alias.
|
|
218
|
+
*/
|
|
219
|
+
requestConnect(alias: string): void {
|
|
220
|
+
this.activeTab = 'terminal'
|
|
221
|
+
this.openTerminalTab(alias)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Add a terminal tab on the alias and make it the shown one. Several tabs
|
|
226
|
+
* may target one alias on purpose: each owns its own host PTY.
|
|
227
|
+
* @param alias - the configured host alias.
|
|
228
|
+
* @returns the new tab id.
|
|
229
|
+
*/
|
|
230
|
+
openTerminalTab(alias: string): string {
|
|
231
|
+
this.nextTerminalTabSeq += 1
|
|
232
|
+
const id = 'tab-' + String(this.nextTerminalTabSeq)
|
|
233
|
+
this.terminalTabs = [...this.terminalTabs, { id, alias, status: 'connecting' }]
|
|
234
|
+
this.activeTerminalTabId = id
|
|
235
|
+
this.draftAlias = alias
|
|
236
|
+
this.retainedScreens.delete(id)
|
|
237
|
+
this.commit()
|
|
238
|
+
return id
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Show an existing tab and make the toolbar follow its host, so a new tab
|
|
243
|
+
* opens on the host the user is looking at.
|
|
244
|
+
* @param id - the tab id.
|
|
245
|
+
*/
|
|
246
|
+
setActiveTerminalTab(id: string): void {
|
|
247
|
+
if (this.activeTerminalTabId === id) return
|
|
248
|
+
const tab = this.terminalTab(id)
|
|
249
|
+
if (tab === undefined) return
|
|
250
|
+
this.activeTerminalTabId = id
|
|
251
|
+
this.draftAlias = tab.alias
|
|
252
|
+
this.commit()
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Remove one tab and forget its screen. When the shown tab closes, the
|
|
257
|
+
* neighbours decide what the xterm shows next: the tab that took its place,
|
|
258
|
+
* else the one before it, else nothing.
|
|
259
|
+
* @param id - the tab id.
|
|
260
|
+
*/
|
|
261
|
+
closeTerminalTab(id: string): void {
|
|
262
|
+
const index = this.terminalTabs.findIndex(tab => tab.id === id)
|
|
263
|
+
this.retainedScreens.delete(id)
|
|
264
|
+
// A still-connecting socket was adopted, not detached, and the tab is going
|
|
265
|
+
// away: its session must end rather than be left without an owner. The
|
|
266
|
+
// intent is retargeted in the live entry instead of being forgotten, because
|
|
267
|
+
// forgetting it leaves the old detach handler in charge of the ready frame.
|
|
268
|
+
const adopted = this.adoptedLinks.get(id)
|
|
269
|
+
if (adopted !== undefined) adopted.intent = 'close'
|
|
270
|
+
if (index < 0) return
|
|
271
|
+
const wasActive = this.activeTerminalTabId === id
|
|
272
|
+
this.terminalTabs = this.terminalTabs.filter(tab => tab.id !== id)
|
|
273
|
+
if (wasActive) {
|
|
274
|
+
const neighbour = this.terminalTabs[index] ?? this.terminalTabs[index - 1]
|
|
275
|
+
this.activeTerminalTabId = neighbour?.id
|
|
276
|
+
if (neighbour !== undefined) this.draftAlias = neighbour.alias
|
|
277
|
+
}
|
|
278
|
+
this.commit()
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** The tab record with this id, or undefined when it no longer exists. */
|
|
282
|
+
terminalTab(id: string): TerminalTabRecord | undefined {
|
|
283
|
+
return this.terminalTabs.find(tab => tab.id === id)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Apply a partial update to one tab; a tab that was closed in the meantime
|
|
288
|
+
* is left alone rather than resurrected.
|
|
289
|
+
* @param id - the tab id.
|
|
290
|
+
* @param patch - the fields to change.
|
|
291
|
+
*/
|
|
292
|
+
patchTerminalTab(id: string, patch: Partial<TerminalTabPatch>): void {
|
|
293
|
+
let found = false
|
|
294
|
+
const next = this.terminalTabs.map(tab => {
|
|
295
|
+
if (tab.id !== id) return tab
|
|
296
|
+
found = true
|
|
297
|
+
return { ...tab, ...patch }
|
|
298
|
+
})
|
|
299
|
+
if (!found) return
|
|
300
|
+
this.terminalTabs = next
|
|
301
|
+
this.commit()
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Record the host session of a ready tab and clear the challenge it carried.
|
|
306
|
+
* @param id - the tab id.
|
|
307
|
+
* @param sessionId - the id the host reported on its ready frame.
|
|
308
|
+
*/
|
|
309
|
+
readyTerminalTab(id: string, sessionId: string): void {
|
|
310
|
+
this.patchTerminalTab(id, {
|
|
311
|
+
status: 'connected',
|
|
312
|
+
sessionId,
|
|
313
|
+
detail: undefined,
|
|
314
|
+
closeError: undefined,
|
|
315
|
+
authPrompt: undefined,
|
|
316
|
+
authAnswers: undefined,
|
|
317
|
+
})
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* The remote shell ended: the tab keeps its screen and its place, and the
|
|
322
|
+
* dead session id is dropped so nothing tries to reattach to it.
|
|
323
|
+
* @param id - the tab id.
|
|
324
|
+
* @param detail - why it ended, when the transport said.
|
|
325
|
+
*/
|
|
326
|
+
exitTerminalTab(id: string, detail?: string): void {
|
|
327
|
+
// The close failure belonged to a session that no longer exists: keeping it
|
|
328
|
+
// would leave a tab that keeps reporting a close it already got.
|
|
329
|
+
this.patchTerminalTab(id, {
|
|
330
|
+
status: 'exited',
|
|
331
|
+
sessionId: undefined,
|
|
332
|
+
detail,
|
|
333
|
+
authPrompt: undefined,
|
|
334
|
+
authAnswers: undefined,
|
|
335
|
+
closeError: undefined,
|
|
336
|
+
})
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* The connection failed. The tab stays with the reason so the user can read
|
|
341
|
+
* it and close the tab themselves.
|
|
342
|
+
* @param id - the tab id.
|
|
343
|
+
* @param detail - the failure text.
|
|
344
|
+
*/
|
|
345
|
+
failTerminalTab(id: string, detail: string): void {
|
|
346
|
+
this.patchTerminalTab(id, {
|
|
347
|
+
status: 'error',
|
|
348
|
+
sessionId: undefined,
|
|
349
|
+
detail,
|
|
350
|
+
authPrompt: undefined,
|
|
351
|
+
authAnswers: undefined,
|
|
352
|
+
closeError: undefined,
|
|
353
|
+
})
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* A background close never reached the host. The tab keeps its live session
|
|
358
|
+
* id (so a retry can still end it) and says so instead of vanishing.
|
|
359
|
+
* @param id - the tab id.
|
|
360
|
+
* @param reason - the reason code shown by the view.
|
|
361
|
+
*/
|
|
362
|
+
markTerminalCloseFailed(id: string, reason: string): void {
|
|
363
|
+
this.patchTerminalTab(id, { closeError: reason })
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Store the keyboard-interactive challenge this tab has to answer, with a
|
|
368
|
+
* blank answer of this challenge's own length. A prompt on another tab never
|
|
369
|
+
* touches this record, which is what keeps two challenges from sharing input.
|
|
370
|
+
* @param id - the tab id.
|
|
371
|
+
* @param prompt - the challenge the host asked for.
|
|
372
|
+
*/
|
|
373
|
+
setTerminalAuthPrompt(id: string, prompt: TerminalAuthPrompt): void {
|
|
374
|
+
this.patchTerminalTab(id, { authPrompt: prompt, authAnswers: prompt.prompts.map(() => '') })
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Record one character typed for this tab's challenge.
|
|
379
|
+
* @param id - the tab id.
|
|
380
|
+
* @param index - which prompt of the challenge.
|
|
381
|
+
* @param value - the field's current text.
|
|
382
|
+
*/
|
|
383
|
+
setTerminalAuthAnswer(id: string, index: number, value: string): void {
|
|
384
|
+
const tab = this.terminalTab(id)
|
|
385
|
+
if (tab === undefined || tab.authPrompt === undefined) return
|
|
386
|
+
const answers = [...(tab.authAnswers ?? tab.authPrompt.prompts.map(() => ''))]
|
|
387
|
+
answers[index] = value
|
|
388
|
+
this.patchTerminalTab(id, { authAnswers: answers })
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** Drop the challenge once it was answered, cancelled, or could not apply. */
|
|
392
|
+
clearTerminalAuthPrompt(id: string): void {
|
|
393
|
+
if (this.terminalTab(id)?.authPrompt === undefined) return
|
|
394
|
+
this.patchTerminalTab(id, { authPrompt: undefined, authAnswers: undefined })
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** Choose the alias the next terminal tab opens on. */
|
|
398
|
+
setDraftAlias(alias: string): void {
|
|
399
|
+
if (this.draftAlias === alias) return
|
|
400
|
+
this.draftAlias = alias
|
|
401
|
+
this.commit()
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** Append one output chunk to the retained screen of a tab. */
|
|
405
|
+
appendTerminalOutput(id: string, chunk: string): void {
|
|
406
|
+
const screen = this.retainedScreens.get(id) ?? { chunks: [], bytes: 0 }
|
|
407
|
+
screen.chunks.push(chunk)
|
|
408
|
+
screen.bytes += utf8Bytes(chunk)
|
|
409
|
+
while (screen.bytes > MAX_RETAINED_BYTES && screen.chunks.length > 1) {
|
|
410
|
+
const dropped = screen.chunks.shift() ?? ''
|
|
411
|
+
screen.bytes -= utf8Bytes(dropped)
|
|
412
|
+
}
|
|
413
|
+
this.retainedScreens.set(id, screen)
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** The retained screen of a tab, concatenated for a redraw. */
|
|
417
|
+
readTerminalOutput(id: string): string {
|
|
418
|
+
return (this.retainedScreens.get(id)?.chunks ?? []).join('')
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** Forget the retained screen (a fresh replay from the host replaces it). */
|
|
422
|
+
clearTerminalOutput(id: string): void {
|
|
423
|
+
this.retainedScreens.delete(id)
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Keep a still-connecting socket alive after its view went away. The socket
|
|
428
|
+
* cannot be detached yet (no session id), so the intent is applied when the
|
|
429
|
+
* host reports it: `detach` leaves the shell for a later reattach, `close`
|
|
430
|
+
* ends the session of a tab the user closed.
|
|
431
|
+
* @param tabId - the tab the socket belongs to.
|
|
432
|
+
* @param connection - the live connection.
|
|
433
|
+
* @param intent - what to do once the session id arrives.
|
|
434
|
+
*/
|
|
435
|
+
adoptTerminalLink(tabId: string, connection: TerminalConnection, intent: TerminalLinkIntent): void {
|
|
436
|
+
const entry = { connection, intent }
|
|
437
|
+
this.adoptedLinks.set(tabId, entry)
|
|
438
|
+
// Output that arrives while no view is bound still belongs to the tab, so it
|
|
439
|
+
// goes into the retained screen: reclaiming the socket then replays the
|
|
440
|
+
// whole adopt window instead of showing a gap. A tab that was closed in the
|
|
441
|
+
// meantime stays closed — appending would recreate its retained entry and
|
|
442
|
+
// hand a deleted tab a screen again.
|
|
443
|
+
connection.onOutput = (data) => {
|
|
444
|
+
if (this.terminalTab(tabId) === undefined) return
|
|
445
|
+
this.appendTerminalOutput(tabId, data)
|
|
446
|
+
}
|
|
447
|
+
connection.onAuthPrompt = (name, instructions, prompts) => {
|
|
448
|
+
if (this.terminalTab(tabId) === undefined) return
|
|
449
|
+
this.setTerminalAuthPrompt(tabId, { name, instructions, prompts })
|
|
450
|
+
}
|
|
451
|
+
connection.onReady = (sessionId) => {
|
|
452
|
+
// The intent is read now, not when the socket was adopted: closing the tab
|
|
453
|
+
// while it connects retargets the entry to `close`, and a detach here
|
|
454
|
+
// would leave a host PTY nobody owns.
|
|
455
|
+
const decided = this.adoptedLinks.get(tabId)?.intent ?? entry.intent
|
|
456
|
+
this.adoptedLinks.delete(tabId)
|
|
457
|
+
this.readyTerminalTab(tabId, sessionId)
|
|
458
|
+
if (decided === 'close') connection.close()
|
|
459
|
+
else connection.detach()
|
|
460
|
+
}
|
|
461
|
+
connection.onExit = (_code, error) => {
|
|
462
|
+
this.adoptedLinks.delete(tabId)
|
|
463
|
+
this.failTerminalTab(tabId, error ?? 'connection closed')
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Take an adopted socket back when its tab is shown again, so the host never
|
|
469
|
+
* opens a second PTY for the same tab.
|
|
470
|
+
* @param tabId - the tab id.
|
|
471
|
+
* @returns the connection, or undefined when nothing was adopted.
|
|
472
|
+
*/
|
|
473
|
+
reclaimTerminalLink(tabId: string): TerminalConnection | undefined {
|
|
474
|
+
const adopted = this.adoptedLinks.get(tabId)
|
|
475
|
+
if (adopted === undefined) return undefined
|
|
476
|
+
// A close intent is already on its way out; leave it to finish.
|
|
477
|
+
if (adopted.intent === 'close') return undefined
|
|
478
|
+
this.adoptedLinks.delete(tabId)
|
|
479
|
+
adopted.connection.onReady = undefined
|
|
480
|
+
adopted.connection.onExit = undefined
|
|
481
|
+
adopted.connection.onAuthPrompt = undefined
|
|
482
|
+
// onOutput stays: the view replaces it when it binds, and keeping it until
|
|
483
|
+
// then means no chunk is lost between reclaim and bind.
|
|
484
|
+
return adopted.connection
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
private commit(): void {
|
|
488
|
+
this.snapshot = {
|
|
489
|
+
panelOpen: this.panelOpen,
|
|
490
|
+
activeTab: this.activeTab,
|
|
491
|
+
terminalTabs: this.terminalTabs,
|
|
492
|
+
activeTerminalTabId: this.activeTerminalTabId,
|
|
493
|
+
draftAlias: this.draftAlias,
|
|
494
|
+
}
|
|
495
|
+
for (const fn of [...this.listeners]) fn()
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/** Ask the layout to select a panel; a shell without the face is a no-op. */
|
|
499
|
+
private selectPanel(panelId: string | null): void {
|
|
500
|
+
try {
|
|
501
|
+
this.deps.panel?.select(panelId)
|
|
502
|
+
} catch {
|
|
503
|
+
// The layout service throws by contract before its root entry mounts;
|
|
504
|
+
// the state flip above already stands and the next selection retries.
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared panel helpers: the active-dictionary pick (document-language based,
|
|
3
|
+
* task-board precedent) bound to the dsh-ssh interpolator in locales.ts, plus
|
|
4
|
+
* a small error-message extractor. All copy stays in the locale dictionaries.
|
|
5
|
+
*/
|
|
6
|
+
import { en, t, zh, type SshKey } from '../locales.ts'
|
|
7
|
+
|
|
8
|
+
/** Template values accepted by the interpolator. */
|
|
9
|
+
export type TranslateValues = Record<string, string | number>
|
|
10
|
+
|
|
11
|
+
/** Active dictionary, picked by the document language at call time. */
|
|
12
|
+
export function dictionary(): Record<string, string> {
|
|
13
|
+
const lang = typeof document !== 'undefined' ? document.documentElement.lang : 'zh'
|
|
14
|
+
return lang.toLowerCase().startsWith('en') ? { ...en } : { ...zh }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* SDK translate seat wired by the browser apply() once ctx.locale is bound
|
|
19
|
+
* (setRuntimeTranslate): reads the ACTIVE locale at call time, so plain-DOM
|
|
20
|
+
* surfaces follow a runtime language switch. The document-language pick stays
|
|
21
|
+
* as the unwired fallback (locale service absent, module-scope early callers).
|
|
22
|
+
*/
|
|
23
|
+
let runtimeT: ((key: SshKey, values?: TranslateValues) => string) | undefined
|
|
24
|
+
|
|
25
|
+
/** Wire the SDK translate seat; pass undefined to restore the document-language pick. */
|
|
26
|
+
export function setRuntimeTranslate(t: ((key: SshKey, values?: TranslateValues) => string) | undefined): void {
|
|
27
|
+
runtimeT = t
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Translate a key with optional {name} template params (current language). */
|
|
31
|
+
export function tt(key: SshKey, values?: TranslateValues): string {
|
|
32
|
+
if (runtimeT !== undefined) return runtimeT(key, values)
|
|
33
|
+
return t(dictionary(), key, values)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Human-readable error text from an unknown thrown value. */
|
|
37
|
+
export function errorMessage(error: unknown): string {
|
|
38
|
+
if (error instanceof Error) return error.message
|
|
39
|
+
return String(error)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The terminal font stack used before issue #577, kept as the last resort
|
|
44
|
+
* when neither the plugin setting nor a CSS custom property names a font.
|
|
45
|
+
*/
|
|
46
|
+
export const TERMINAL_FONT_FALLBACK = 'Menlo, Consolas, "Liberation Mono", monospace'
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Live source of the user's configured terminal font family: the client
|
|
50
|
+
* entry binds the dsh-ssh settings namespace into one of these and hands it
|
|
51
|
+
* to the panel, so a settings change re-applies without a remount.
|
|
52
|
+
*/
|
|
53
|
+
export interface TerminalFontSource {
|
|
54
|
+
/** Current configured family, or undefined when unset / not yet loaded. */
|
|
55
|
+
get(): string | undefined
|
|
56
|
+
/** Subscribe to changes; the returned function unsubscribes. */
|
|
57
|
+
subscribe(listener: () => void): () => void
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Resolve the xterm `fontFamily` (issue #577). xterm's DOM renderer takes
|
|
62
|
+
* the font only from constructor/options, so a plain stylesheet rule cannot
|
|
63
|
+
* retarget it — the value must be read back into the options. The chain,
|
|
64
|
+
* first non-empty value wins:
|
|
65
|
+
* 1. `override` — the `terminalFontFamily` plugin setting;
|
|
66
|
+
* 2. `--dsh-ssh-terminal-font` — dedicated hook for skins and user CSS
|
|
67
|
+
* (e.g. a Nerd Font for powerline glyphs);
|
|
68
|
+
* 3. `--ds-font-family-code` — the official code-font token several skins
|
|
69
|
+
* already remap;
|
|
70
|
+
* 4. {@link TERMINAL_FONT_FALLBACK}.
|
|
71
|
+
*/
|
|
72
|
+
export function resolveTerminalFontFamily(override?: string): string {
|
|
73
|
+
const trimmed = override?.trim()
|
|
74
|
+
if (trimmed !== undefined && trimmed !== '') return trimmed
|
|
75
|
+
if (typeof getComputedStyle === 'function' && typeof document !== 'undefined') {
|
|
76
|
+
const target = document.body ?? document.documentElement
|
|
77
|
+
const style = getComputedStyle(target)
|
|
78
|
+
for (const name of ['--dsh-ssh-terminal-font', '--ds-font-family-code']) {
|
|
79
|
+
const value = style.getPropertyValue(name).trim()
|
|
80
|
+
if (value !== '') return value
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return TERMINAL_FONT_FALLBACK
|
|
84
|
+
}
|