@fish-under-sea/dsh-ssh-fish 0.0.0-stage → 0.5.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.
Files changed (80) hide show
  1. package/LICENSE +202 -0
  2. package/README.i18n.yaml +6 -0
  3. package/README.md +98 -2
  4. package/README.zh.md +99 -0
  5. package/cordis.patch.yml +12 -0
  6. package/icon.svg +3 -0
  7. package/lib/client.js +16487 -0
  8. package/lib/client.js.map +1 -0
  9. package/lib/index.js +3511 -0
  10. package/lib/types/client/api.d.ts +141 -0
  11. package/lib/types/client/index.d.ts +37 -0
  12. package/lib/types/client/locales.d.ts +164 -0
  13. package/lib/types/client/native-panel.d.ts +70 -0
  14. package/lib/types/client/panel/ClusterTab.d.ts +7 -0
  15. package/lib/types/client/panel/HostFormDialog.d.ts +12 -0
  16. package/lib/types/client/panel/HostsTab.d.ts +25 -0
  17. package/lib/types/client/panel/SshPanel.d.ts +14 -0
  18. package/lib/types/client/panel/TerminalTab.d.ts +22 -0
  19. package/lib/types/client/panel/TransferTab.d.ts +7 -0
  20. package/lib/types/client/panel/TunnelsTab.d.ts +19 -0
  21. package/lib/types/client/panel/controller.d.ts +89 -0
  22. package/lib/types/client/panel/helpers.d.ts +45 -0
  23. package/lib/types/client/panel/xterm.css.d.ts +2 -0
  24. package/lib/types/client/settings-binding.d.ts +61 -0
  25. package/lib/types/dsh-home.d.ts +16 -0
  26. package/lib/types/engine/cluster.d.ts +19 -0
  27. package/lib/types/engine/connection-pool.d.ts +112 -0
  28. package/lib/types/engine/proxy-command.d.ts +36 -0
  29. package/lib/types/engine/pty.d.ts +32 -0
  30. package/lib/types/engine/sftp.d.ts +20 -0
  31. package/lib/types/engine/terminal-sessions.d.ts +67 -0
  32. package/lib/types/engine/tunnel.d.ts +36 -0
  33. package/lib/types/engine.d.ts +89 -0
  34. package/lib/types/http.d.ts +59 -0
  35. package/lib/types/index.d.ts +83 -0
  36. package/lib/types/loopback.d.ts +24 -0
  37. package/lib/types/mount-once.d.ts +55 -0
  38. package/lib/types/protocol.d.ts +239 -0
  39. package/lib/types/routes.d.ts +33 -0
  40. package/lib/types/ssh-config.d.ts +34 -0
  41. package/lib/types/store.d.ts +71 -0
  42. package/lib/types/tool-surface.d.ts +71 -0
  43. package/lib/types/tools.d.ts +42 -0
  44. package/package.json +106 -4
  45. package/src/client/api.ts +562 -0
  46. package/src/client/css-modules.d.ts +5 -0
  47. package/src/client/index.ts +137 -0
  48. package/src/client/locales.ts +338 -0
  49. package/src/client/native-panel.tsx +141 -0
  50. package/src/client/panel/ClusterTab.tsx +123 -0
  51. package/src/client/panel/HostFormDialog.tsx +258 -0
  52. package/src/client/panel/HostsTab.tsx +325 -0
  53. package/src/client/panel/SshPanel.tsx +90 -0
  54. package/src/client/panel/TerminalTab.tsx +330 -0
  55. package/src/client/panel/TransferTab.tsx +232 -0
  56. package/src/client/panel/TunnelsTab.tsx +244 -0
  57. package/src/client/panel/controller.ts +167 -0
  58. package/src/client/panel/helpers.ts +84 -0
  59. package/src/client/panel/panel.module.css +876 -0
  60. package/src/client/panel/xterm.css.ts +2 -0
  61. package/src/client/settings-binding.ts +165 -0
  62. package/src/dsh-home.ts +42 -0
  63. package/src/engine/cluster.ts +60 -0
  64. package/src/engine/connection-pool.ts +543 -0
  65. package/src/engine/proxy-command.ts +248 -0
  66. package/src/engine/pty.ts +78 -0
  67. package/src/engine/sftp.ts +226 -0
  68. package/src/engine/terminal-sessions.ts +280 -0
  69. package/src/engine/tunnel.ts +194 -0
  70. package/src/engine.ts +176 -0
  71. package/src/http.ts +127 -0
  72. package/src/index.ts +234 -0
  73. package/src/loopback.ts +63 -0
  74. package/src/mount-once.ts +142 -0
  75. package/src/protocol.ts +230 -0
  76. package/src/routes.ts +494 -0
  77. package/src/ssh-config.ts +153 -0
  78. package/src/store.ts +419 -0
  79. package/src/tool-surface.ts +95 -0
  80. package/src/tools.ts +404 -0
@@ -0,0 +1,244 @@
1
+ /**
2
+ * Tunnels tab: the live local port-forward list (auto-refresh every 5s while
3
+ * visible) with per-row stop, a stop-all action scoped to the selected alias,
4
+ * and a new-tunnel form.
5
+ */
6
+ import { useEffect, useRef, useState } from 'react'
7
+ import type { SshApi } from '../api.ts'
8
+ import type { SshHostSummary, TunnelInfo } from '../../protocol.ts'
9
+ import { errorMessage, tt } from './helpers.ts'
10
+ import css from './panel.module.css'
11
+
12
+ /** Live-tunnel polling interval while the tab and page are visible (ms). */
13
+ export const TUNNEL_POLL_MS = 5000
14
+
15
+ /**
16
+ * Return `next` only when the tunnel list changed in a user-visible way
17
+ * (identity, ordering or any renderable field), else `null` so a poll tick
18
+ * with no real change keeps the previous reference and React skips the
19
+ * re-render. `prev === null` (first load) always accepts the list.
20
+ */
21
+ export function diffTunnels(prev: TunnelInfo[] | null, next: TunnelInfo[]): TunnelInfo[] | null {
22
+ if (prev === null) return next
23
+ if (prev.length !== next.length) return next
24
+ for (let index = 0; index < prev.length; index += 1) {
25
+ const a = prev[index]
26
+ const b = next[index]
27
+ if (a.id !== b.id || a.alias !== b.alias || a.state !== b.state
28
+ || a.localPort !== b.localPort || a.remoteHost !== b.remoteHost
29
+ || a.remotePort !== b.remotePort || a.startedAt !== b.startedAt
30
+ || a.error !== b.error) {
31
+ return next
32
+ }
33
+ }
34
+ return null
35
+ }
36
+
37
+ /** Tunnels tab props. */
38
+ export interface TunnelsTabProps {
39
+ api: SshApi
40
+ /** Pause automatic reads while the owning panel is closed, preserving form state. */
41
+ active?: boolean
42
+ }
43
+
44
+ /** The tunnels tab. */
45
+ export function TunnelsTab({ api, active = true }: TunnelsTabProps) {
46
+ const [hosts, setHosts] = useState<SshHostSummary[]>([])
47
+ const [tunnels, setTunnels] = useState<TunnelInfo[] | null>(null)
48
+ const [error, setError] = useState<string | null>(null)
49
+ const [notice, setNotice] = useState<string | null>(null)
50
+ const [alias, setAlias] = useState('')
51
+ const [remotePort, setRemotePort] = useState('')
52
+ const [remoteHost, setRemoteHost] = useState('')
53
+ const [localPort, setLocalPort] = useState('')
54
+ const [busy, setBusy] = useState(false)
55
+
56
+ // Hosts for the new-tunnel form (failure does not block tunnel listing).
57
+ useEffect(() => {
58
+ let disposed = false
59
+ void (async () => {
60
+ try {
61
+ const list = await api.listHosts()
62
+ if (!disposed) setHosts(list)
63
+ } catch {
64
+ // Tunnels may still exist; keep the list usable.
65
+ }
66
+ })()
67
+ return () => { disposed = true }
68
+ }, [api])
69
+
70
+ // Live list with a TUNNEL_POLL_MS heartbeat while visible. Every load
71
+ // carries a sequence number so stale responses never overwrite newer
72
+ // state, and the list is diff-set so an unchanged poll tick keeps the
73
+ // previous state reference (no wasted re-render).
74
+ const seqRef = useRef(0)
75
+ const automaticRead = useRef<{ running: boolean; resume?: () => void }>({ running: false })
76
+ useEffect(() => {
77
+ if (!active) return
78
+ let disposed = false
79
+ const read = automaticRead.current
80
+ let timer: ReturnType<typeof setInterval> | undefined
81
+ const load = async (): Promise<void> => {
82
+ if (disposed || read.running || document.visibilityState === 'hidden') return
83
+ read.running = true
84
+ const seq = ++seqRef.current
85
+ try {
86
+ const list = await api.listTunnels()
87
+ if (disposed || seq !== seqRef.current) return
88
+ setTunnels(prev => diffTunnels(prev, list) ?? prev)
89
+ setError(null)
90
+ } catch (cause) {
91
+ if (disposed || seq !== seqRef.current) return
92
+ setError(errorMessage(cause))
93
+ } finally {
94
+ read.running = false
95
+ const resume = read.resume
96
+ read.resume = undefined
97
+ resume?.()
98
+ }
99
+ }
100
+ const resume = (): void => {
101
+ if (disposed || document.visibilityState === 'hidden') return
102
+ // Rapid close/reopen must not start more reads while the old effect's
103
+ // request is pending. Keep only the latest visible refresh.
104
+ if (read.running) read.resume = resume
105
+ else void load()
106
+ }
107
+ const stop = (): void => {
108
+ if (timer !== undefined) clearInterval(timer)
109
+ timer = undefined
110
+ }
111
+ const onVisibility = (): void => {
112
+ if (document.visibilityState === 'hidden') {
113
+ stop()
114
+ if (read.resume === resume) read.resume = undefined
115
+ } else if (timer === undefined) {
116
+ resume()
117
+ timer = setInterval(() => { void load() }, TUNNEL_POLL_MS)
118
+ }
119
+ }
120
+ document.addEventListener('visibilitychange', onVisibility)
121
+ onVisibility()
122
+ return () => {
123
+ disposed = true
124
+ seqRef.current += 1
125
+ if (read.resume === resume) read.resume = undefined
126
+ stop()
127
+ document.removeEventListener('visibilitychange', onVisibility)
128
+ }
129
+ }, [api, active])
130
+
131
+ const refresh = async (): Promise<void> => {
132
+ const seq = ++seqRef.current
133
+ try {
134
+ const list = await api.listTunnels()
135
+ if (seq !== seqRef.current) return
136
+ setTunnels(list)
137
+ setError(null)
138
+ } catch (cause) {
139
+ if (seq !== seqRef.current) return
140
+ setError(errorMessage(cause))
141
+ }
142
+ }
143
+
144
+ const stopTunnel = async (tunnelId: string): Promise<void> => {
145
+ try {
146
+ await api.stopTunnel(tunnelId)
147
+ await refresh()
148
+ } catch (cause) {
149
+ setError(errorMessage(cause))
150
+ }
151
+ }
152
+
153
+ const stopAll = async (): Promise<void> => {
154
+ if (!window.confirm(tt('tunnel.stopAllConfirm'))) return
155
+ setBusy(true)
156
+ try {
157
+ await api.stopAllTunnels(alias === '' ? undefined : alias)
158
+ await refresh()
159
+ } catch (cause) {
160
+ setError(errorMessage(cause))
161
+ } finally {
162
+ setBusy(false)
163
+ }
164
+ }
165
+
166
+ const start = async (): Promise<void> => {
167
+ if (alias === '' || remotePort.trim() === '') return
168
+ const remotePortNumber = Number(remotePort)
169
+ const localPortNumber = localPort.trim() === '' ? undefined : Number(localPort)
170
+ if (!Number.isInteger(remotePortNumber) || remotePortNumber < 1 || remotePortNumber > 65535) {
171
+ setError(tt('form.portInvalid'))
172
+ return
173
+ }
174
+ if (localPortNumber !== undefined && (!Number.isInteger(localPortNumber) || localPortNumber < 1 || localPortNumber > 65535)) {
175
+ setError(tt('form.portInvalid'))
176
+ return
177
+ }
178
+ setBusy(true)
179
+ setNotice(null)
180
+ try {
181
+ const tunnel = await api.startTunnel({
182
+ alias,
183
+ remotePort: remotePortNumber,
184
+ remoteHost: remoteHost.trim() === '' ? undefined : remoteHost.trim(),
185
+ localPort: localPortNumber,
186
+ })
187
+ setNotice(tt('tunnel.started', { localPort: tunnel.localPort }))
188
+ setRemotePort('')
189
+ setRemoteHost('')
190
+ setLocalPort('')
191
+ await refresh()
192
+ } catch (cause) {
193
+ setError(tt('tunnel.failed', { error: errorMessage(cause) }))
194
+ } finally {
195
+ setBusy(false)
196
+ }
197
+ }
198
+
199
+ return (
200
+ <div className={css.tabBody}>
201
+ <div className={css.controls}>
202
+ <button type="button" className={css.ghostButton} disabled={busy} onClick={() => { void stopAll() }}>{tt('tunnel.stopAll')}</button>
203
+ <button type="button" className={css.ghostButton} onClick={() => { void refresh() }}>{tt('common.refresh')}</button>
204
+ </div>
205
+ {error !== null && <div className={css.banner} data-kind="error">{error}</div>}
206
+ {notice !== null && <div className={css.banner} data-kind="ok">{notice}</div>}
207
+ {tunnels !== null && tunnels.length === 0 && <div className={css.empty}>{tt('tunnel.empty')}</div>}
208
+ <div className={css.tunnelList}>
209
+ {(tunnels ?? []).map(tunnel => (
210
+ <div key={tunnel.id} className={css.tunnelRow} data-state={tunnel.state}>
211
+ <span className={css.tunnelLabel}>{tt('tunnel.row', { alias: tunnel.alias, localPort: tunnel.localPort, remoteHost: tunnel.remoteHost, remotePort: tunnel.remotePort })}</span>
212
+ <button type="button" className={css.ghostButton} onClick={() => { void stopTunnel(tunnel.id) }}>{tt('tunnel.stop')}</button>
213
+ </div>
214
+ ))}
215
+ </div>
216
+ <div className={css.formCard}>
217
+ <div className={css.formRow}>
218
+ <label className={css.field}>
219
+ <span className={css.fieldLabel}>{tt('tunnel.alias')}</span>
220
+ <select className={css.input} value={alias} onChange={event => { setAlias(event.target.value) }}>
221
+ <option value="">{tt('terminal.selectHost')}</option>
222
+ {hosts.map(host => <option key={host.alias} value={host.alias}>{host.alias}</option>)}
223
+ </select>
224
+ </label>
225
+ <label className={css.field}>
226
+ <span className={css.fieldLabel}>{tt('tunnel.remotePort')}</span>
227
+ <input className={css.input} type="number" min={1} max={65535} value={remotePort} onChange={event => { setRemotePort(event.target.value) }} />
228
+ </label>
229
+ <label className={css.field}>
230
+ <span className={css.fieldLabel}>{tt('tunnel.remoteHost')}</span>
231
+ <input className={css.input} value={remoteHost} placeholder={tt('tunnel.remoteHostHint')} onChange={event => { setRemoteHost(event.target.value) }} />
232
+ </label>
233
+ <label className={css.field}>
234
+ <span className={css.fieldLabel}>{tt('tunnel.localPort')}</span>
235
+ <input className={css.input} type="number" min={1} max={65535} value={localPort} placeholder={tt('tunnel.localPortHint')} onChange={event => { setLocalPort(event.target.value) }} />
236
+ </label>
237
+ </div>
238
+ <div>
239
+ <button type="button" className={css.primaryButton} disabled={busy || alias === '' || remotePort.trim() === ''} onClick={() => { void start() }}>{tt('tunnel.start')}</button>
240
+ </div>
241
+ </div>
242
+ </div>
243
+ )
244
+ }
@@ -0,0 +1,167 @@
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 and the live terminal session
9
+ * id live here rather than inside the panel components: the layout mounts a
10
+ * keyed page only while its panel is selected, so component-local state would
11
+ * reset the tab on every panel switch and lose the id a reattach needs.
12
+ */
13
+
14
+ /** The panel's tab identifiers. */
15
+ export type SshTab = 'hosts' | 'terminal' | 'transfer' | 'tunnels' | 'cluster'
16
+
17
+ /** A "connect" action from the hosts tab, handed to the terminal tab. */
18
+ export interface ConnectRequest {
19
+ alias: string
20
+ /** Monotonic id so a repeated connect for the same alias still re-applies. */
21
+ nonce: number
22
+ }
23
+
24
+ /** Stable id shared by the sidebar panel row and the main-slot page. */
25
+ export const SSH_PANEL_ID = 'ssh'
26
+
27
+ /** Immutable controller snapshot for UI subscriptions. */
28
+ export interface PanelControllerSnapshot {
29
+ /** Whether the center column shows this panel. */
30
+ panelOpen: boolean
31
+ /** The tab the panel is showing. */
32
+ activeTab: SshTab
33
+ /** Pending hosts-tab connect request, or null. */
34
+ connectRequest: ConnectRequest | null
35
+ /** Host-side terminal session the terminal tab should reattach to, if any. */
36
+ terminalSessionId: string | undefined
37
+ }
38
+
39
+ /**
40
+ * The layout's panel-navigation face: selecting the panel is the layout's
41
+ * business, and this is the one call that asks for it (null returns the column
42
+ * to the conversation). Optional, so a shell-less composition keeps the pure
43
+ * state transitions testable.
44
+ */
45
+ export interface PanelNavigationFace {
46
+ /** Select this panel, or null to hand the column back to the conversation. */
47
+ select(panelId: string | null): void
48
+ }
49
+
50
+ /** Controller dependencies. */
51
+ export interface PanelControllerDeps {
52
+ /** Layout panel selection; absent keeps the controller state-only. */
53
+ panel?: PanelNavigationFace
54
+ }
55
+
56
+ /** The panel state owner the sidebar row and the view both read. */
57
+ export class PanelController {
58
+ private panelOpen = false
59
+ private activeTab: SshTab = 'hosts'
60
+ private connectRequest: ConnectRequest | null = null
61
+ private terminalSessionId: string | undefined
62
+ private listeners = new Set<() => void>()
63
+ /** Cached so getSnapshot is referentially stable between changes. */
64
+ private snapshot: PanelControllerSnapshot = {
65
+ panelOpen: false,
66
+ activeTab: 'hosts',
67
+ connectRequest: null,
68
+ terminalSessionId: undefined,
69
+ }
70
+
71
+ constructor(private readonly deps: PanelControllerDeps = {}) {}
72
+
73
+ getSnapshot(): PanelControllerSnapshot {
74
+ return this.snapshot
75
+ }
76
+
77
+ subscribe(fn: () => void): () => void {
78
+ this.listeners.add(fn)
79
+ return () => { this.listeners.delete(fn) }
80
+ }
81
+
82
+ /**
83
+ * Show the panel. The layout owns which panel the column renders, so the
84
+ * state flip and the selection travel together; the selection is requested
85
+ * after the snapshot flips, so a subscriber never observes "open" while the
86
+ * shell still shows the conversation.
87
+ */
88
+ open(): void {
89
+ if (this.panelOpen) return
90
+ this.panelOpen = true
91
+ this.commit()
92
+ this.selectPanel(SSH_PANEL_ID)
93
+ }
94
+
95
+ /** Return the column to the conversation, asking the layout explicitly. */
96
+ close(): void {
97
+ if (!this.panelOpen) return
98
+ this.panelOpen = false
99
+ this.commit()
100
+ this.selectPanel(null)
101
+ }
102
+
103
+ toggle(): void {
104
+ if (this.panelOpen) this.close()
105
+ else this.open()
106
+ }
107
+
108
+ /**
109
+ * Reflect a selection made outside this controller (the user clicked another
110
+ * sidebar row, or the layout dropped the panel id).
111
+ * @param panelId - the layout's current panel id, or null for the conversation.
112
+ */
113
+ syncPanelSelection(panelId: string | null): void {
114
+ const open = panelId === SSH_PANEL_ID
115
+ if (open === this.panelOpen) return
116
+ this.panelOpen = open
117
+ this.commit()
118
+ }
119
+
120
+ /** Switch tabs, keeping every other piece of view state. */
121
+ setActiveTab(tab: SshTab): void {
122
+ if (this.activeTab === tab) return
123
+ this.activeTab = tab
124
+ this.commit()
125
+ }
126
+
127
+ /** Hand the terminal tab a host to connect to (hosts-tab "connect" action). */
128
+ requestConnect(alias: string): void {
129
+ this.connectRequest = { alias, nonce: Date.now() }
130
+ this.activeTab = 'terminal'
131
+ this.commit()
132
+ }
133
+
134
+ /** Record the host-side session so a later page mount can reattach. */
135
+ setTerminalSession(sessionId: string): void {
136
+ if (this.terminalSessionId === sessionId) return
137
+ this.terminalSessionId = sessionId
138
+ this.commit()
139
+ }
140
+
141
+ /** Forget the session (it exited, or the user disconnected). */
142
+ clearTerminalSession(): void {
143
+ if (this.terminalSessionId === undefined) return
144
+ this.terminalSessionId = undefined
145
+ this.commit()
146
+ }
147
+
148
+ private commit(): void {
149
+ this.snapshot = {
150
+ panelOpen: this.panelOpen,
151
+ activeTab: this.activeTab,
152
+ connectRequest: this.connectRequest,
153
+ terminalSessionId: this.terminalSessionId,
154
+ }
155
+ for (const fn of [...this.listeners]) fn()
156
+ }
157
+
158
+ /** Ask the layout to select a panel; a shell without the face is a no-op. */
159
+ private selectPanel(panelId: string | null): void {
160
+ try {
161
+ this.deps.panel?.select(panelId)
162
+ } catch {
163
+ // The layout service throws by contract before its root entry mounts;
164
+ // the state flip above already stands and the next selection retries.
165
+ }
166
+ }
167
+ }
@@ -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
+ }