dsh-remote-deck 0.0.0-stage → 1.46.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 +21 -0
- package/README.md +227 -2
- package/README.zh-CN.md +201 -0
- package/client.js +8645 -0
- package/cordis.patch.yml +6 -0
- package/icon.svg +19 -0
- package/index.js +701 -0
- package/lib/api.js +1452 -0
- package/lib/bash-result.js +93 -0
- package/lib/connection.js +1133 -0
- package/lib/delete.js +199 -0
- package/lib/drafts.js +299 -0
- package/lib/encoding.js +247 -0
- package/lib/file-edit.js +339 -0
- package/lib/file-ops.js +402 -0
- package/lib/forwards.js +186 -0
- package/lib/fs-actions.js +235 -0
- package/lib/guide.js +162 -0
- package/lib/metrics.js +523 -0
- package/lib/remote-shells.js +320 -0
- package/lib/remote-terminal.js +427 -0
- package/lib/routing.js +695 -0
- package/lib/ssh-config.js +118 -0
- package/lib/terminal.js +123 -0
- package/lib/tool-forward.js +185 -0
- package/lib/transfer.js +235 -0
- package/lib/upload-chunks.js +306 -0
- package/lib/util.js +154 -0
- package/locale/README.md +160 -0
- package/locale/en-ui.json +477 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +63 -4
- package/vendor/codemirror/LICENSE +21 -0
- package/vendor/codemirror/README.md +18 -0
- package/vendor/codemirror/addon/edit/closebrackets.min.js +8 -0
- package/vendor/codemirror/addon/edit/matchbrackets.min.js +8 -0
- package/vendor/codemirror/addon/search/search.min.js +8 -0
- package/vendor/codemirror/addon/search/searchcursor.min.js +8 -0
- package/vendor/codemirror/addon/selection/active-line.min.js +8 -0
- package/vendor/codemirror/lib/codemirror.css +344 -0
- package/vendor/codemirror/lib/codemirror.min.js +8 -0
- package/vendor/codemirror/mode/clike/clike.min.js +8 -0
- package/vendor/codemirror/mode/css/css.min.js +8 -0
- package/vendor/codemirror/mode/go/go.min.js +8 -0
- package/vendor/codemirror/mode/htmlmixed/htmlmixed.min.js +8 -0
- package/vendor/codemirror/mode/javascript/javascript.min.js +8 -0
- package/vendor/codemirror/mode/markdown/markdown.min.js +8 -0
- package/vendor/codemirror/mode/python/python.min.js +8 -0
- package/vendor/codemirror/mode/rust/rust.min.js +8 -0
- package/vendor/codemirror/mode/shell/shell.min.js +8 -0
- package/vendor/codemirror/mode/xml/xml.min.js +8 -0
- package/vendor/codemirror/mode/yaml/yaml.min.js +8 -0
- package/vendor/node_modules/asn1/Jenkinsfile +65 -0
- package/vendor/node_modules/asn1/LICENSE +19 -0
- package/vendor/node_modules/asn1/README.md +50 -0
- package/vendor/node_modules/asn1/lib/ber/errors.js +13 -0
- package/vendor/node_modules/asn1/lib/ber/index.js +27 -0
- package/vendor/node_modules/asn1/lib/ber/reader.js +262 -0
- package/vendor/node_modules/asn1/lib/ber/types.js +36 -0
- package/vendor/node_modules/asn1/lib/ber/writer.js +317 -0
- package/vendor/node_modules/asn1/lib/index.js +20 -0
- package/vendor/node_modules/asn1/package.json +31 -0
- package/vendor/node_modules/bcrypt-pbkdf/CONTRIBUTING.md +13 -0
- package/vendor/node_modules/bcrypt-pbkdf/LICENSE +66 -0
- package/vendor/node_modules/bcrypt-pbkdf/README.md +45 -0
- package/vendor/node_modules/bcrypt-pbkdf/index.js +556 -0
- package/vendor/node_modules/bcrypt-pbkdf/package.json +15 -0
- package/vendor/node_modules/safer-buffer/LICENSE +21 -0
- package/vendor/node_modules/safer-buffer/Porting-Buffer.md +268 -0
- package/vendor/node_modules/safer-buffer/Readme.md +156 -0
- package/vendor/node_modules/safer-buffer/dangerous.js +58 -0
- package/vendor/node_modules/safer-buffer/package.json +34 -0
- package/vendor/node_modules/safer-buffer/safer.js +77 -0
- package/vendor/node_modules/safer-buffer/tests.js +406 -0
- package/vendor/node_modules/ssh2/LICENSE +19 -0
- package/vendor/node_modules/ssh2/README.md +1529 -0
- package/vendor/node_modules/ssh2/SFTP.md +413 -0
- package/vendor/node_modules/ssh2/lib/Channel.js +295 -0
- package/vendor/node_modules/ssh2/lib/agent.js +1123 -0
- package/vendor/node_modules/ssh2/lib/client.js +2176 -0
- package/vendor/node_modules/ssh2/lib/http-agents.js +84 -0
- package/vendor/node_modules/ssh2/lib/index.js +44 -0
- package/vendor/node_modules/ssh2/lib/keygen.js +582 -0
- package/vendor/node_modules/ssh2/lib/protocol/Protocol.js +2136 -0
- package/vendor/node_modules/ssh2/lib/protocol/SFTP.js +4052 -0
- package/vendor/node_modules/ssh2/lib/protocol/constants.js +356 -0
- package/vendor/node_modules/ssh2/lib/protocol/crypto/binding.gyp +23 -0
- package/vendor/node_modules/ssh2/lib/protocol/crypto/poly1305.js +43 -0
- package/vendor/node_modules/ssh2/lib/protocol/crypto/src/binding.cc +2300 -0
- package/vendor/node_modules/ssh2/lib/protocol/crypto.js +1602 -0
- package/vendor/node_modules/ssh2/lib/protocol/handlers.js +16 -0
- package/vendor/node_modules/ssh2/lib/protocol/handlers.misc.js +1285 -0
- package/vendor/node_modules/ssh2/lib/protocol/kex.js +1908 -0
- package/vendor/node_modules/ssh2/lib/protocol/keyParser.js +1484 -0
- package/vendor/node_modules/ssh2/lib/protocol/node-fs-compat.js +115 -0
- package/vendor/node_modules/ssh2/lib/protocol/utils.js +356 -0
- package/vendor/node_modules/ssh2/lib/protocol/zlib.js +255 -0
- package/vendor/node_modules/ssh2/lib/server.js +1380 -0
- package/vendor/node_modules/ssh2/lib/utils.js +336 -0
- package/vendor/node_modules/ssh2/package.json +49 -0
- package/vendor/node_modules/tweetnacl/AUTHORS.md +28 -0
- package/vendor/node_modules/tweetnacl/CHANGELOG.md +221 -0
- package/vendor/node_modules/tweetnacl/LICENSE +24 -0
- package/vendor/node_modules/tweetnacl/PULL_REQUEST_TEMPLATE.md +20 -0
- package/vendor/node_modules/tweetnacl/README.md +459 -0
- package/vendor/node_modules/tweetnacl/nacl-fast.js +2388 -0
- package/vendor/node_modules/tweetnacl/nacl-fast.min.js +2 -0
- package/vendor/node_modules/tweetnacl/nacl.d.ts +98 -0
- package/vendor/node_modules/tweetnacl/nacl.js +1175 -0
- package/vendor/node_modules/tweetnacl/nacl.min.js +1 -0
- package/vendor/node_modules/tweetnacl/package.json +58 -0
- package/vendor/xterm/LICENSE.addon-fit +19 -0
- package/vendor/xterm/LICENSE.xterm +21 -0
- package/vendor/xterm/README.md +9 -0
- package/vendor/xterm/addon-fit.js +2 -0
- package/vendor/xterm/xterm.css +218 -0
- package/vendor/xterm/xterm.js +2 -0
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remote shell discovery for the sidebar terminal's shell menu.
|
|
3
|
+
*
|
|
4
|
+
* The stock controller builds that menu with `shells(agent, signal)`, which
|
|
5
|
+
* resolves `discoverShells(this.execution(agent).subprocess, ...)` and therefore
|
|
6
|
+
* describes the *local* execution provider — in a remote workspace it offers
|
|
7
|
+
* `cmd.exe`/`powershell`. `shells` is the only method on that path that receives
|
|
8
|
+
* the agent, and the gateway resolves the method on every call
|
|
9
|
+
* (`Reflect.get(callReceiver, implementation)`,
|
|
10
|
+
* `dsh-api-gateway/lib/index.js:1244`, verified in the installed app.asar), so
|
|
11
|
+
* replacing that one method answers for remote Sessions while every local
|
|
12
|
+
* Session keeps the stock list.
|
|
13
|
+
*
|
|
14
|
+
* Two more facts from the installed build shaped this module:
|
|
15
|
+
* - the controller's own `spawn` looks the chosen path up in `this.shells(...)`
|
|
16
|
+
* (line 984), so the same replacement also decides which shells can be picked;
|
|
17
|
+
* - the service is registered by the cordis `Service` base class under the name
|
|
18
|
+
* `terminalController` (`super(ctx, "terminalController", ...)`, line 771), so
|
|
19
|
+
* `ctx.inject(['terminalController'], ...)` reaches it.
|
|
20
|
+
*
|
|
21
|
+
* Guarantees the replacement keeps:
|
|
22
|
+
* - a local Session calls the original method and returns its result untouched;
|
|
23
|
+
* - a remote Session gets one bounded probe per connection, cached, and the
|
|
24
|
+
* original result whenever the probe fails or finds nothing — never an empty
|
|
25
|
+
* menu;
|
|
26
|
+
* - `restore()` puts the original method back.
|
|
27
|
+
*/
|
|
28
|
+
import { shellQuote } from './util.js'
|
|
29
|
+
|
|
30
|
+
/** POSIX shells offered for a remote Session, most preferred first. */
|
|
31
|
+
export const REMOTE_SHELL_NAMES = Object.freeze(['bash', 'zsh', 'fish', 'dash', 'ksh', 'sh'])
|
|
32
|
+
/** The argument every offered shell is started with, and reported with. */
|
|
33
|
+
export const REMOTE_SHELL_ARGS = Object.freeze(['-l'])
|
|
34
|
+
/** Remote-side probe budget, in seconds, when the host has `timeout`. */
|
|
35
|
+
export const SHELL_PROBE_TIMEOUT_SECONDS = 6
|
|
36
|
+
/** How long a discovered list stays valid for one connection. */
|
|
37
|
+
export const DEFAULT_SHELL_CACHE_MS = 60000
|
|
38
|
+
/** How long a failed probe is remembered, so a dead host is probed once, not per menu open. */
|
|
39
|
+
export const DEFAULT_SHELL_FAILURE_CACHE_MS = 15000
|
|
40
|
+
/** Hard cap on one probe, including connecting a dropped session; a menu must answer. */
|
|
41
|
+
export const DEFAULT_SHELL_PROBE_TIMEOUT_MS = 10000
|
|
42
|
+
|
|
43
|
+
const MAX_SHELL_PATH_LENGTH = 1024
|
|
44
|
+
|
|
45
|
+
/** The last path segment, which is also the name the controller dedupes by. */
|
|
46
|
+
export function shellName(path) {
|
|
47
|
+
const value = String(path)
|
|
48
|
+
return value.slice(value.lastIndexOf('/') + 1)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether a value may be started as a remote login shell.
|
|
53
|
+
*
|
|
54
|
+
* Only absolute POSIX paths whose program name is one of the offered shells get
|
|
55
|
+
* through: the value arrives in `spawnTerminal`'s `argv`, where a local
|
|
56
|
+
* `C:\Windows\System32\cmd.exe` or a bare `bash` means nothing on the remote
|
|
57
|
+
* side. The path is still single-quoted where it is used, and traversal and
|
|
58
|
+
* control characters are rejected rather than quoted.
|
|
59
|
+
*
|
|
60
|
+
* @param {unknown} value candidate path
|
|
61
|
+
* @param {readonly string[]} [names] allowed program names
|
|
62
|
+
* @returns {boolean}
|
|
63
|
+
*/
|
|
64
|
+
export function isRemoteShellPath(value, names = REMOTE_SHELL_NAMES) {
|
|
65
|
+
if (typeof value !== 'string' || value.length === 0 || value.length > MAX_SHELL_PATH_LENGTH) return false
|
|
66
|
+
if (!value.startsWith('/') || value.startsWith('//')) return false
|
|
67
|
+
if (/[\u0000-\u001f\u007f]/.test(value)) return false
|
|
68
|
+
if (value.split('/').includes('..')) return false
|
|
69
|
+
return names.includes(shellName(value))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The bounded probe: print the absolute path of every candidate shell that
|
|
74
|
+
* exists and is executable, one per line, in preference order.
|
|
75
|
+
*
|
|
76
|
+
* `command -v` walks the login PATH, the `/*` case drops shell builtins and
|
|
77
|
+
* aliases, and `[ -x ]` drops a PATH hit that cannot be started. A hit that is
|
|
78
|
+
* not an absolute path — BusyBox resolves its own applets that way, so `sh` can
|
|
79
|
+
* come back as the bare word `sh` — falls back to the usual bin directories,
|
|
80
|
+
* which is also what saves a host whose PATH is minimal. `timeout` bounds the
|
|
81
|
+
* whole thing where the host has it — a PATH on a dead network mount must not
|
|
82
|
+
* hold the menu — and the caller's own deadline covers a host without `timeout`.
|
|
83
|
+
*
|
|
84
|
+
* @param {readonly string[]} [names] candidate program names, most preferred first
|
|
85
|
+
* @param {number} [timeoutSeconds] remote-side budget
|
|
86
|
+
* @returns {string} command for the remote shell
|
|
87
|
+
*/
|
|
88
|
+
export function shellProbeCommand(names = REMOTE_SHELL_NAMES, timeoutSeconds = SHELL_PROBE_TIMEOUT_SECONDS) {
|
|
89
|
+
const candidates = names.map((candidate) => shellQuote(candidate)).join(' ')
|
|
90
|
+
const body = [
|
|
91
|
+
`for name in ${candidates}; do`,
|
|
92
|
+
' p=$(command -v "$name" 2>/dev/null) || p=""',
|
|
93
|
+
` case "$p" in /*) ;; *) p="" ;; esac`,
|
|
94
|
+
' if [ -z "$p" ]; then',
|
|
95
|
+
' for dir in /bin /usr/bin /usr/local/bin; do',
|
|
96
|
+
' if [ -x "$dir/$name" ]; then p="$dir/$name"; break; fi',
|
|
97
|
+
' done',
|
|
98
|
+
' fi',
|
|
99
|
+
` if [ -n "$p" ] && [ -x "$p" ]; then printf '%s\\n' "$p"; fi`,
|
|
100
|
+
'done',
|
|
101
|
+
].join('\n')
|
|
102
|
+
const guarded = shellQuote(body)
|
|
103
|
+
return [
|
|
104
|
+
'if command -v timeout >/dev/null 2>&1; then',
|
|
105
|
+
` timeout ${timeoutSeconds} sh -c ${guarded}`,
|
|
106
|
+
'else',
|
|
107
|
+
` sh -c ${guarded}`,
|
|
108
|
+
'fi',
|
|
109
|
+
].join('\n')
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Read the probe's output into the controller's shell-profile shape.
|
|
114
|
+
*
|
|
115
|
+
* Lines may arrive in any order and unknown or malformed lines are dropped:
|
|
116
|
+
* anything that is not an absolute path to one of the offered shells is noise,
|
|
117
|
+
* including a truncated last line. The result is ordered by preference, so the
|
|
118
|
+
* first entry is the menu default (bash when it exists).
|
|
119
|
+
*
|
|
120
|
+
* @param {unknown} text probe stdout
|
|
121
|
+
* @param {readonly string[]} [names] allowed program names
|
|
122
|
+
* @returns {Array<{ path: string, name: string, args: string[] }>}
|
|
123
|
+
*/
|
|
124
|
+
export function parseShellProbeOutput(text, names = REMOTE_SHELL_NAMES) {
|
|
125
|
+
if (typeof text !== 'string' || text.length === 0) return []
|
|
126
|
+
const rank = new Map(names.map((name, index) => [name, index]))
|
|
127
|
+
const seen = new Set()
|
|
128
|
+
const found = []
|
|
129
|
+
for (const raw of text.split('\n')) {
|
|
130
|
+
const path = raw.trim()
|
|
131
|
+
if (seen.has(path) || !isRemoteShellPath(path, names)) continue
|
|
132
|
+
seen.add(path)
|
|
133
|
+
found.push({ path, name: shellName(path), args: [...REMOTE_SHELL_ARGS] })
|
|
134
|
+
}
|
|
135
|
+
return found.sort((a, b) => rank.get(a.name) - rank.get(b.name))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Find the object that owns `method` on a service instance.
|
|
140
|
+
*
|
|
141
|
+
* Walks the prototype chain instead of reading the property: `ctx.get(name)`
|
|
142
|
+
* returns a fresh cordis traceable proxy, and `Object.getPrototypeOf` on that
|
|
143
|
+
* proxy reaches the real prototype, which is the one object both the gateway's
|
|
144
|
+
* `Reflect.get` and the controller's own `this.shells(...)` resolve through.
|
|
145
|
+
*
|
|
146
|
+
* @param {unknown} service the service instance (or a proxy over it)
|
|
147
|
+
* @param {string} method method name
|
|
148
|
+
* @returns {{ owner: object, descriptor: PropertyDescriptor, original: Function } | null}
|
|
149
|
+
*/
|
|
150
|
+
function findMethodHome(service, method) {
|
|
151
|
+
if (service === null || (typeof service !== 'object' && typeof service !== 'function')) return null
|
|
152
|
+
let owner = service
|
|
153
|
+
while (owner !== null) {
|
|
154
|
+
const descriptor = Object.getOwnPropertyDescriptor(owner, method)
|
|
155
|
+
if (descriptor !== undefined) {
|
|
156
|
+
if (typeof descriptor.value !== 'function') return null
|
|
157
|
+
// A frozen method cannot be replaced, and a non-configurable one cannot be
|
|
158
|
+
// restored; leave the stock behaviour alone in both cases.
|
|
159
|
+
if (descriptor.configurable === false && descriptor.writable === false) return null
|
|
160
|
+
return { owner, descriptor, original: descriptor.value }
|
|
161
|
+
}
|
|
162
|
+
owner = Object.getPrototypeOf(owner)
|
|
163
|
+
}
|
|
164
|
+
return null
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** A finite, non-negative number, or the fallback. */
|
|
168
|
+
function duration(value, fallback) {
|
|
169
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : fallback
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Bound a promise that may hang before its own deadline applies.
|
|
174
|
+
*
|
|
175
|
+
* The probe command carries its own `timeout` and the exec carries a deadline,
|
|
176
|
+
* but connecting a dropped session happens first and can stall; this is the cap
|
|
177
|
+
* that keeps the shell menu answering. The timer is deliberately referenced: an
|
|
178
|
+
* unref'd deadline would not fire once the event loop is otherwise idle.
|
|
179
|
+
*/
|
|
180
|
+
function withDeadline(promise, ms) {
|
|
181
|
+
if (!(ms > 0)) return promise
|
|
182
|
+
return new Promise((resolve, reject) => {
|
|
183
|
+
const timer = setTimeout(() => reject(new Error(`remote-deck: shell probe timed out after ${ms}ms`)), ms)
|
|
184
|
+
promise.then(
|
|
185
|
+
(value) => {
|
|
186
|
+
clearTimeout(timer)
|
|
187
|
+
resolve(value)
|
|
188
|
+
},
|
|
189
|
+
(error) => {
|
|
190
|
+
clearTimeout(timer)
|
|
191
|
+
reject(error)
|
|
192
|
+
},
|
|
193
|
+
)
|
|
194
|
+
})
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Replace `terminalController.shells` with a remote-aware version.
|
|
199
|
+
*
|
|
200
|
+
* @param {unknown} service the terminal controller service
|
|
201
|
+
* @param {object} deps
|
|
202
|
+
* @param {(agent: object) => string | undefined} deps.cwdFor committed Session directory
|
|
203
|
+
* @param {(cwd: string) => ({ connectionId: string, path?: string } | null)} deps.resolveTarget remote target for a path
|
|
204
|
+
* @param {(connectionId: string) => Promise<string>} deps.probe runs {@link shellProbeCommand} on one connection and resolves its stdout
|
|
205
|
+
* @param {(target: object, shells: object[]) => void} [deps.onRemote] observability hook, called only when a remote list is used
|
|
206
|
+
* @param {() => number} [deps.now] clock, injectable for deterministic tests
|
|
207
|
+
* @param {number} [deps.cacheTtlMs] how long a discovered list stays valid
|
|
208
|
+
* @param {number} [deps.failureCacheTtlMs] how long a failed probe is remembered
|
|
209
|
+
* @param {number} [deps.probeTimeoutMs] hard cap on one probe, connecting included
|
|
210
|
+
* @param {readonly string[]} [deps.shellNames] candidate program names
|
|
211
|
+
* @returns {{ installed: boolean, reason?: string, restore: () => void }}
|
|
212
|
+
*/
|
|
213
|
+
export function installRemoteShells(service, deps) {
|
|
214
|
+
const home = findMethodHome(service, 'shells')
|
|
215
|
+
if (home === null) {
|
|
216
|
+
return { installed: false, reason: 'terminalController.shells is not a replaceable method', restore: () => {} }
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const now = typeof deps.now === 'function' ? deps.now : Date.now
|
|
220
|
+
const cacheTtlMs = duration(deps.cacheTtlMs, DEFAULT_SHELL_CACHE_MS)
|
|
221
|
+
const failureCacheTtlMs = duration(deps.failureCacheTtlMs, DEFAULT_SHELL_FAILURE_CACHE_MS)
|
|
222
|
+
const probeTimeoutMs = duration(deps.probeTimeoutMs, DEFAULT_SHELL_PROBE_TIMEOUT_MS)
|
|
223
|
+
const names = Array.isArray(deps.shellNames) && deps.shellNames.length > 0 ? deps.shellNames : REMOTE_SHELL_NAMES
|
|
224
|
+
const results = new Map()
|
|
225
|
+
const inflight = new Map()
|
|
226
|
+
|
|
227
|
+
/** The remote target of one agent's Session, or null for every unprovable case. */
|
|
228
|
+
const targetOf = (agent) => {
|
|
229
|
+
if (agent === null || typeof agent !== 'object') return null
|
|
230
|
+
let cwd
|
|
231
|
+
try {
|
|
232
|
+
cwd = deps.cwdFor(agent)
|
|
233
|
+
} catch {
|
|
234
|
+
return null
|
|
235
|
+
}
|
|
236
|
+
if (typeof cwd !== 'string' || cwd.length === 0) return null
|
|
237
|
+
let resolved
|
|
238
|
+
try {
|
|
239
|
+
resolved = deps.resolveTarget(cwd)
|
|
240
|
+
} catch {
|
|
241
|
+
return null
|
|
242
|
+
}
|
|
243
|
+
if (resolved === null || typeof resolved !== 'object') return null
|
|
244
|
+
if (typeof resolved.connectionId !== 'string' || resolved.connectionId.length === 0) return null
|
|
245
|
+
return { connectionId: resolved.connectionId, path: typeof resolved.path === 'string' ? resolved.path : '' }
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** One probe per connection, cached; concurrent callers share the running one. */
|
|
249
|
+
const discover = (connectionId) => {
|
|
250
|
+
const cached = results.get(connectionId)
|
|
251
|
+
if (cached !== undefined) {
|
|
252
|
+
const ttl = cached.error === null ? cacheTtlMs : failureCacheTtlMs
|
|
253
|
+
if (now() - cached.at < ttl) {
|
|
254
|
+
return cached.error === null ? Promise.resolve(cached.list) : Promise.reject(cached.error)
|
|
255
|
+
}
|
|
256
|
+
results.delete(connectionId)
|
|
257
|
+
}
|
|
258
|
+
const running = inflight.get(connectionId)
|
|
259
|
+
if (running !== undefined) return running
|
|
260
|
+
|
|
261
|
+
const started = Promise.resolve()
|
|
262
|
+
.then(() => withDeadline(deps.probe(connectionId), probeTimeoutMs))
|
|
263
|
+
.then((stdout) => {
|
|
264
|
+
const list = typeof stdout === 'string' ? parseShellProbeOutput(stdout, names) : []
|
|
265
|
+
results.set(connectionId, { at: now(), list, error: null })
|
|
266
|
+
return list
|
|
267
|
+
})
|
|
268
|
+
.catch((error) => {
|
|
269
|
+
const failure = error instanceof Error ? error : new Error(String(error))
|
|
270
|
+
results.set(connectionId, { at: now(), list: [], error: failure })
|
|
271
|
+
throw failure
|
|
272
|
+
})
|
|
273
|
+
inflight.set(connectionId, started)
|
|
274
|
+
const settle = () => {
|
|
275
|
+
if (inflight.get(connectionId) === started) inflight.delete(connectionId)
|
|
276
|
+
}
|
|
277
|
+
started.then(settle, settle)
|
|
278
|
+
return started
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* The replacement. Parameter names are part of the contract: the gateway can
|
|
283
|
+
* fall back to reading them off the function source for signature validation.
|
|
284
|
+
*/
|
|
285
|
+
function shells(agent, signal) {
|
|
286
|
+
if (signal !== undefined && signal !== null && typeof signal.throwIfAborted === 'function') signal.throwIfAborted()
|
|
287
|
+
const target = targetOf(agent)
|
|
288
|
+
if (target === null) return home.original.call(this, agent, signal)
|
|
289
|
+
return discover(target.connectionId).then(
|
|
290
|
+
(list) => {
|
|
291
|
+
if (list.length === 0) return home.original.call(this, agent, signal)
|
|
292
|
+
if (typeof deps.onRemote === 'function') {
|
|
293
|
+
try {
|
|
294
|
+
deps.onRemote(target, list)
|
|
295
|
+
} catch {
|
|
296
|
+
/* diagnostics must never break the menu */
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return list
|
|
300
|
+
},
|
|
301
|
+
() => home.original.call(this, agent, signal),
|
|
302
|
+
)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
try {
|
|
306
|
+
Object.defineProperty(home.owner, 'shells', { ...home.descriptor, value: shells })
|
|
307
|
+
} catch {
|
|
308
|
+
return { installed: false, reason: 'terminalController.shells cannot be replaced', restore: () => {} }
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return {
|
|
312
|
+
installed: true,
|
|
313
|
+
restore: () => {
|
|
314
|
+
const current = Object.getOwnPropertyDescriptor(home.owner, 'shells')
|
|
315
|
+
// Only take back what is still ours, so a later patch is not clobbered.
|
|
316
|
+
if (current === undefined || current.value !== shells) return
|
|
317
|
+
Object.defineProperty(home.owner, 'shells', home.descriptor)
|
|
318
|
+
},
|
|
319
|
+
}
|
|
320
|
+
}
|