dsh-bash-terminal-ts 0.7.2-compat.2 → 0.8.1
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.de.md +3 -2
- package/README.en.md +102 -3
- package/README.es.md +3 -2
- package/README.fr.md +3 -2
- package/README.it.md +3 -2
- package/README.md +198 -4
- package/README.ru.md +3 -2
- package/cordis.patch.yml +66 -0
- package/dist/client.core.js +7888 -79
- package/dist/client.js +7888 -79
- package/dsh.plugin.json +22 -22
- package/lib/browser-i18n.d.ts +4 -0
- package/lib/browser-i18n.js +23 -0
- package/lib/client.js +7888 -79
- package/lib/dsh-types.d.ts +10 -0
- package/lib/dsh.d.ts +2 -0
- package/lib/index.d.ts +43 -0
- package/lib/index.js +176 -91
- package/lib/locales.d.ts +4 -0
- package/lib/locales.js +422 -0
- package/lib/settings-surface.d.ts +20 -0
- package/lib/settings-surface.js +49 -0
- package/lib/shell-id.d.ts +2 -0
- package/lib/shell-id.js +9 -0
- package/lib/shells.d.ts +114 -0
- package/lib/shells.js +187 -0
- package/lib/terminal.d.ts +30 -1
- package/lib/terminal.js +18 -5
- package/lib/webterminals/catalog.d.ts +36 -0
- package/lib/webterminals/catalog.js +93 -0
- package/lib/webterminals/host.d.ts +67 -0
- package/lib/webterminals/host.js +278 -0
- package/lib/webterminals/mount.d.ts +46 -0
- package/lib/webterminals/mount.js +171 -0
- package/lib/webterminals/protocol.d.ts +94 -0
- package/lib/webterminals/protocol.js +9 -0
- package/lib/webterminals/pty.d.ts +42 -0
- package/lib/webterminals/pty.js +64 -0
- package/lib/webterminals/registry.d.ts +139 -0
- package/lib/webterminals/registry.js +353 -0
- package/lib/webterminals/ws.d.ts +54 -0
- package/lib/webterminals/ws.js +140 -0
- package/locale/de.json +4 -0
- package/locale/en.json +4 -0
- package/locale/es.json +4 -0
- package/locale/fr.json +4 -0
- package/locale/it.json +4 -0
- package/locale/ja.json +4 -0
- package/locale/ko.json +4 -0
- package/locale/ru.json +4 -0
- package/locale/zh.json +4 -0
- package/package.json +131 -121
- package/scripts/build-browser-tests.mjs +36 -0
- package/scripts/build-client.mjs +10 -0
- package/scripts/check-artifacts.mjs +93 -0
- package/scripts/link-shells.mjs +75 -0
- package/src/browser-i18n.ts +27 -0
- package/src/client.tsx +539 -134
- package/src/dsh-types.ts +6 -0
- package/src/dsh.ts +2 -0
- package/src/index.ts +146 -35
- package/src/locales.ts +434 -0
- package/src/settings-surface.ts +59 -0
- package/src/shell-id.ts +11 -0
- package/src/shells.ts +250 -0
- package/src/terminal.ts +34 -5
- package/src/webterminals/browser/api.ts +212 -0
- package/src/webterminals/browser/bridge.ts +188 -0
- package/src/webterminals/browser/css.d.ts +16 -0
- package/src/webterminals/browser/face.ts +151 -0
- package/src/webterminals/browser/shell-picker.tsx +98 -0
- package/src/webterminals/browser/store.ts +40 -0
- package/src/webterminals/browser/tab.tsx +136 -0
- package/src/webterminals/browser/testkit.ts +21 -0
- package/src/webterminals/browser/url.ts +70 -0
- package/src/webterminals/browser/view.ts +381 -0
- package/src/webterminals/browser/xterm.ts +269 -0
- package/src/webterminals/catalog.ts +114 -0
- package/src/webterminals/host.ts +361 -0
- package/src/webterminals/mount.ts +227 -0
- package/src/webterminals/protocol.ts +78 -0
- package/src/webterminals/pty.ts +90 -0
- package/src/webterminals/registry.ts +447 -0
- package/src/webterminals/ws.ts +166 -0
- package/tsconfig.client.json +9 -1
- package/tsconfig.json +1 -1
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
// The host half of the browser terminal: one JSON route for the requests and the
|
|
2
|
+
// socket mount for the byte stream.
|
|
3
|
+
//
|
|
4
|
+
// Why a separate route from the existing `/api/bash-terminal/config` bridge: that
|
|
5
|
+
// bridge is the model tool's preference channel and works on all six host lines;
|
|
6
|
+
// this one serves a feature that must exist even on a line where the tool itself
|
|
7
|
+
// is not registered (the browser terminal is not a Windows-only idea - the tool
|
|
8
|
+
// is). Keeping them apart means the tool's behaviour cannot regress when the
|
|
9
|
+
// terminal ships, and each route can be probed separately in the settings page.
|
|
10
|
+
//
|
|
11
|
+
// The host, not the browser, decides which shell an allocation uses: it reads the
|
|
12
|
+
// catalog (activation + availability) and the fallback chain, so a client can
|
|
13
|
+
// never launch a shell the user disabled, and a stale `defaultShell` degrades
|
|
14
|
+
// visibly instead of failing.
|
|
15
|
+
import { resolveDefaultShell, uiArgv } from "../shells.js";
|
|
16
|
+
import { UiTerminalError } from "./registry.js";
|
|
17
|
+
import { mountTerminalSocket } from "./ws.js";
|
|
18
|
+
/** The JSON route path (POST-only, loopback-gated like the config bridge). */
|
|
19
|
+
export const TERMINAL_API_PATH = "/api/bash-terminal/terminals";
|
|
20
|
+
/** Write one reply in the envelope the config bridge already uses. */
|
|
21
|
+
function json(res, status, body) {
|
|
22
|
+
res.writeHead(status, { "content-type": "application/json; charset=utf-8" });
|
|
23
|
+
res.end(JSON.stringify(body));
|
|
24
|
+
}
|
|
25
|
+
/** Read one bounded JSON body. */
|
|
26
|
+
async function readBody(request) {
|
|
27
|
+
const chunks = [];
|
|
28
|
+
let size = 0;
|
|
29
|
+
const raw = await new Promise((resolve, reject) => {
|
|
30
|
+
request.on("data", (chunk) => {
|
|
31
|
+
size += chunk.length;
|
|
32
|
+
if (size <= 65536)
|
|
33
|
+
chunks.push(chunk);
|
|
34
|
+
});
|
|
35
|
+
request.on("end", () => { resolve(Buffer.concat(chunks)); });
|
|
36
|
+
request.on("error", reject);
|
|
37
|
+
});
|
|
38
|
+
if (raw.length === 0)
|
|
39
|
+
return {};
|
|
40
|
+
return JSON.parse(raw.toString("utf8"));
|
|
41
|
+
}
|
|
42
|
+
/** Coerce a number field with a bound. */
|
|
43
|
+
function num(value, fallback, max) {
|
|
44
|
+
return typeof value === "number" && Number.isFinite(value) ? Math.max(1, Math.min(max, Math.floor(value))) : fallback;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The config field behind each switch, spelled out rather than derived.
|
|
48
|
+
*
|
|
49
|
+
* A computed name (`"shell" + cap(key)`) produces `shellGitbash` while the schema
|
|
50
|
+
* declares `shellGitBash` - the write would land on a key that does not exist,
|
|
51
|
+
* persist nothing, and look fine in the settings page. Two spellings of one fact
|
|
52
|
+
* always drift, so this table is the single place the name appears.
|
|
53
|
+
*/
|
|
54
|
+
const SWITCH_FIELD = { gitbash: "shellGitBash", msys2: "shellMsys2", wsl: "shellWsl" };
|
|
55
|
+
/**
|
|
56
|
+
* Register the terminal routes.
|
|
57
|
+
* @param webServer - the host webserver (may lack upgrade support).
|
|
58
|
+
* @param deps - the wiring above.
|
|
59
|
+
* @returns a disposer, plus the reason when the host cannot carry a socket.
|
|
60
|
+
*/
|
|
61
|
+
export function registerUiTerminalRoutes(webServer, deps) {
|
|
62
|
+
const handler = async (request, res) => {
|
|
63
|
+
if (!deps.isLoopback(request)) {
|
|
64
|
+
json(res, 403, { ok: false, code: "loopback-only" });
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (request.method !== "POST") {
|
|
68
|
+
json(res, 405, { ok: false, code: "method-not-allowed" });
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
let body;
|
|
72
|
+
try {
|
|
73
|
+
body = await readBody(request);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
json(res, 400, { ok: false, code: "bad-json" });
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
const value = await dispatch(body, deps);
|
|
81
|
+
json(res, 200, { ok: true, value });
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
const code = error instanceof UiTerminalError ? error.code : error instanceof Error ? "failed" : "failed";
|
|
85
|
+
json(res, error instanceof UiTerminalError ? error.status : 500, {
|
|
86
|
+
ok: false,
|
|
87
|
+
code,
|
|
88
|
+
message: error instanceof Error ? error.message : String(error),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const disposers = [];
|
|
93
|
+
const registered = webServer?.register?.({ kind: "exact", path: TERMINAL_API_PATH, handler: handler });
|
|
94
|
+
if (typeof registered === "function")
|
|
95
|
+
disposers.push(registered);
|
|
96
|
+
const mount = mountTerminalSocket({ registry: deps.registry, environmentFor: (cwd) => deps.registry.environment(cwd), isLoopback: (request) => deps.isLoopback(request) }, webServer);
|
|
97
|
+
if (mount.ok)
|
|
98
|
+
disposers.push(mount.dispose);
|
|
99
|
+
return {
|
|
100
|
+
socket: mount.ok ? "mounted" : mount.reason,
|
|
101
|
+
dispose: () => {
|
|
102
|
+
for (const dispose of disposers) {
|
|
103
|
+
try {
|
|
104
|
+
dispose();
|
|
105
|
+
}
|
|
106
|
+
catch { /* already gone */ }
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/** One request -> one reply. */
|
|
112
|
+
async function dispatch(body, deps) {
|
|
113
|
+
const op = typeof body.op === "string" ? body.op : "";
|
|
114
|
+
// Everything below the master switch is gated by it. `switches` is included: an
|
|
115
|
+
// off feature that still accepts child edits would store choices it ignores, and
|
|
116
|
+
// the settings row would show them as applied.
|
|
117
|
+
if (!deps.enabled() && op !== "get" && op !== "enabled") {
|
|
118
|
+
throw new UiTerminalError("disabled", "the bottom terminal is switched off in settings", 409);
|
|
119
|
+
}
|
|
120
|
+
switch (op) {
|
|
121
|
+
case "get": {
|
|
122
|
+
const catalog = deps.catalog();
|
|
123
|
+
return {
|
|
124
|
+
catalog,
|
|
125
|
+
effective: effectiveShell(deps),
|
|
126
|
+
switches: deps.switches(),
|
|
127
|
+
defaultShell: deps.configuredShell(),
|
|
128
|
+
wslDistro: deps.wslDistro(),
|
|
129
|
+
enabled: deps.enabled(),
|
|
130
|
+
shimOfficialTerminal: deps.shimOfficialTerminal(),
|
|
131
|
+
settingsSurface: deps.settingsSurface(),
|
|
132
|
+
// The master switch outranks the capability answers: "off" is the user's
|
|
133
|
+
// choice, and reporting `ok` next to a hidden panel would be the confusing
|
|
134
|
+
// half of the pair.
|
|
135
|
+
unavailable: deps.enabled() ? deps.unavailableReason() : "disabled",
|
|
136
|
+
terminals: deps.registry.count(),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
case "shim": {
|
|
140
|
+
// Written, not inferred: whether the browser may claim the `webTerminals` name is
|
|
141
|
+
// a deployment fact only the operator knows, and the safe default is to not claim
|
|
142
|
+
// it. The client re-reads the service before publishing, so switching this on
|
|
143
|
+
// where a controller already exists cannot silently take the controller down.
|
|
144
|
+
const next = body.shim === true;
|
|
145
|
+
await deps.writeConfig(["shimOfficialTerminal"], next);
|
|
146
|
+
return { shimOfficialTerminal: deps.shimOfficialTerminal(), catalog: deps.catalog() };
|
|
147
|
+
}
|
|
148
|
+
case "enabled": {
|
|
149
|
+
// The master switch itself is always writable - refusing that would be a way
|
|
150
|
+
// to lock the feature off permanently.
|
|
151
|
+
const value = body.enabled === true;
|
|
152
|
+
await deps.writeConfig(["bottomTerminal"], value);
|
|
153
|
+
return { enabled: deps.enabled(), catalog: deps.catalog(), effective: effectiveShell(deps), switches: deps.switches() };
|
|
154
|
+
}
|
|
155
|
+
case "switches": {
|
|
156
|
+
const requested = body.switches;
|
|
157
|
+
if (requested === null || typeof requested !== "object")
|
|
158
|
+
throw new UiTerminalError("bad-request", "switches must be an object");
|
|
159
|
+
const next = requested;
|
|
160
|
+
// Persist first, then answer with a rebuilt catalog: a switch that cannot be
|
|
161
|
+
// stored must not look applied in the settings row.
|
|
162
|
+
for (const key of ["gitbash", "msys2", "wsl"]) {
|
|
163
|
+
if (typeof next[key] === "boolean")
|
|
164
|
+
await deps.writeConfig([SWITCH_FIELD[key]], next[key]);
|
|
165
|
+
}
|
|
166
|
+
if (typeof body.wslDistro === "string")
|
|
167
|
+
await deps.writeConfig(["wslDistro"], body.wslDistro);
|
|
168
|
+
return { catalog: deps.catalog(), effective: effectiveShell(deps), switches: deps.switches(), wslDistro: deps.wslDistro(), enabled: deps.enabled() };
|
|
169
|
+
}
|
|
170
|
+
case "default": {
|
|
171
|
+
const value = typeof body.defaultShell === "string" ? body.defaultShell : "";
|
|
172
|
+
const catalog = deps.catalog();
|
|
173
|
+
const row = catalog.find((entry) => entry.id === value);
|
|
174
|
+
if (row === undefined || !row.activated || !row.available) {
|
|
175
|
+
// Its own code, not a generic bad-request: the browser renders "enable that
|
|
176
|
+
// shell first" for this one and "malformed input" for the rest.
|
|
177
|
+
throw new UiTerminalError("shellNotActivated", `shell ${JSON.stringify(value)} is not an activated, available choice`, 400);
|
|
178
|
+
}
|
|
179
|
+
await deps.writeConfig(["defaultShell"], value);
|
|
180
|
+
return { catalog: deps.catalog(), effective: effectiveShell(deps) };
|
|
181
|
+
}
|
|
182
|
+
case "terminal.open": {
|
|
183
|
+
const sessionId = typeof body.sessionId === "string" ? body.sessionId : "";
|
|
184
|
+
if (sessionId === "")
|
|
185
|
+
throw new UiTerminalError("bad-request", "sessionId is required");
|
|
186
|
+
const cwd = deps.cwdFor(sessionId);
|
|
187
|
+
// An explicit `shell` is honoured ONLY when that row is activated and
|
|
188
|
+
// available: the same R4 rule the default choice obeys. This is what lets the
|
|
189
|
+
// multi-shell tab offer Git Bash and MSYS2 side by side - the host terminal
|
|
190
|
+
// controller cannot, because its menu de-duplicates shells by executable file
|
|
191
|
+
// name and both are `bash.exe`.
|
|
192
|
+
const asked = typeof body.shell === "string" ? body.shell.trim() : "";
|
|
193
|
+
const rows = deps.catalog();
|
|
194
|
+
let resolved = effectiveShell(deps);
|
|
195
|
+
if (asked !== "") {
|
|
196
|
+
const row = rows.find((entry) => entry.id === asked);
|
|
197
|
+
if (row === undefined || !row.activated || !row.available || row.path === undefined) {
|
|
198
|
+
throw new UiTerminalError("shellNotActivated", `shell ${JSON.stringify(asked)} is not an activated, available choice`, 400);
|
|
199
|
+
}
|
|
200
|
+
resolved = {
|
|
201
|
+
id: row.id,
|
|
202
|
+
path: row.path,
|
|
203
|
+
argv: row.argv ?? uiArgv(row.id, deps.paths(), ""),
|
|
204
|
+
fellBack: false,
|
|
205
|
+
floor: false,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
const env = deps.env(resolved.id === "cmd" ? "powershell" : resolved.id);
|
|
209
|
+
return await openAndReport({
|
|
210
|
+
sessionId,
|
|
211
|
+
cwd,
|
|
212
|
+
resolved,
|
|
213
|
+
rows,
|
|
214
|
+
env,
|
|
215
|
+
body,
|
|
216
|
+
deps,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
case "terminal.close": {
|
|
220
|
+
const id = typeof body.id === "string" ? body.id : "";
|
|
221
|
+
if (id === "")
|
|
222
|
+
throw new UiTerminalError("bad-request", "id is required");
|
|
223
|
+
await deps.registry.close(id);
|
|
224
|
+
return { closed: true };
|
|
225
|
+
}
|
|
226
|
+
case "terminal.list": {
|
|
227
|
+
const sessionId = typeof body.sessionId === "string" ? body.sessionId : "";
|
|
228
|
+
if (sessionId === "")
|
|
229
|
+
throw new UiTerminalError("bad-request", "sessionId is required");
|
|
230
|
+
return { terminals: deps.registry.list(sessionId) };
|
|
231
|
+
}
|
|
232
|
+
case "shells.discover": {
|
|
233
|
+
const catalog = deps.catalog();
|
|
234
|
+
const shells = catalog
|
|
235
|
+
.filter((entry) => entry.available && entry.path !== undefined)
|
|
236
|
+
.map((entry) => ({ path: entry.path, args: entry.argv?.slice(1) ?? [], name: entry.label }));
|
|
237
|
+
return { shells, selected: deps.configuredShell() };
|
|
238
|
+
}
|
|
239
|
+
default:
|
|
240
|
+
throw new UiTerminalError("bad-request", `unknown op ${JSON.stringify(body.op)}`, 404);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
/** The shell an allocation would use, recomputed from the live catalog. */
|
|
244
|
+
function effectiveShell(deps) {
|
|
245
|
+
return resolveDefaultShell(deps.catalog(), deps.configuredShell(), deps.paths());
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Allocate one shell and answer in the shape the browser needs.
|
|
249
|
+
*
|
|
250
|
+
* Split out because two ops reach it (the fallback-chain default and an explicit
|
|
251
|
+
* per-tab shell choice) and the reply contract - `{info, environment}`, plus the
|
|
252
|
+
* argv slice that becomes the info's `shell` - must not be spelled twice.
|
|
253
|
+
*/
|
|
254
|
+
async function openAndReport(input) {
|
|
255
|
+
const { sessionId, cwd, resolved, rows, env, body, deps } = input;
|
|
256
|
+
const info = await deps.registry.open({
|
|
257
|
+
sessionId,
|
|
258
|
+
cwd,
|
|
259
|
+
shellId: resolved.id,
|
|
260
|
+
shellSpec: { path: resolved.path, args: resolved.argv.slice(1), name: labelOf(rows, resolved) },
|
|
261
|
+
title: labelOf(rows, resolved),
|
|
262
|
+
argv: [...resolved.argv],
|
|
263
|
+
env,
|
|
264
|
+
cols: num(body.cols, 80, 500),
|
|
265
|
+
rows: num(body.rows, 24, 200),
|
|
266
|
+
terminalId: typeof body.terminalId === "string" ? body.terminalId : undefined,
|
|
267
|
+
});
|
|
268
|
+
// `environment` travels with the info: the emulator cannot size itself until it
|
|
269
|
+
// has the limits, and asking for them in a second round trip would race the first
|
|
270
|
+
// output frame.
|
|
271
|
+
return { info, environment: deps.registry.environment(cwd) };
|
|
272
|
+
}
|
|
273
|
+
/** The display name of the resolved shell (the catalog label, else the id). */
|
|
274
|
+
function labelOf(catalog, resolved) {
|
|
275
|
+
if (resolved.id === "cmd")
|
|
276
|
+
return "cmd";
|
|
277
|
+
return catalog.find((entry) => entry.id === resolved.id)?.label ?? String(resolved.id);
|
|
278
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { ResolvedPaths } from "../dsh-types.js";
|
|
2
|
+
/** The service-arrival context cordis hands the `inject` callback. */
|
|
3
|
+
interface ArrivalContext {
|
|
4
|
+
webServer?: unknown;
|
|
5
|
+
settings?: unknown;
|
|
6
|
+
effect(fn: () => unknown, name?: string): unknown;
|
|
7
|
+
}
|
|
8
|
+
/** What the caller passes; all of it comes from the plugin's own context. */
|
|
9
|
+
export interface MountInput {
|
|
10
|
+
/** The plugin context, widened: only three reads are used. */
|
|
11
|
+
ctx: {
|
|
12
|
+
subprocess?: {
|
|
13
|
+
spawnTerminal?: unknown;
|
|
14
|
+
};
|
|
15
|
+
get?(name: string): unknown;
|
|
16
|
+
effect(fn: () => unknown, name?: string): unknown;
|
|
17
|
+
inject?(deps: string[], fn: (svcCtx: ArrivalContext) => void): void;
|
|
18
|
+
logger?: {
|
|
19
|
+
info?(msg: string): void;
|
|
20
|
+
warn?(msg: string): void;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
config: Record<string, unknown>;
|
|
24
|
+
paths: ResolvedPaths;
|
|
25
|
+
/** The stored default shell ('' when the user never chose one). */
|
|
26
|
+
configuredShell(): string;
|
|
27
|
+
/**
|
|
28
|
+
* Persist one config path. index.ts owns this (it holds the settings-service
|
|
29
|
+
* dance for every line, including the <=0.1.6 self-registration retry), and the
|
|
30
|
+
* service instance only exists here, so it is passed back in.
|
|
31
|
+
*/
|
|
32
|
+
persist(svc: unknown, path: readonly string[], value: unknown): Promise<void>;
|
|
33
|
+
platform?: NodeJS.Platform;
|
|
34
|
+
}
|
|
35
|
+
/** The mount outcome, reported through the routes' `unavailable` field. */
|
|
36
|
+
export interface BrowserTerminalMount {
|
|
37
|
+
/** 'ok', or why a terminal cannot be offered. */
|
|
38
|
+
unavailable(): string;
|
|
39
|
+
dispose(): void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Mount the browser terminal: routes, socket, registry, catalog cache.
|
|
43
|
+
* @returns the disposer and the availability answer the settings page reads.
|
|
44
|
+
*/
|
|
45
|
+
export declare function mountBrowserTerminals(input: MountInput): BrowserTerminalMount;
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// The wiring seam between the plugin's live config and the browser terminal.
|
|
2
|
+
//
|
|
3
|
+
// It lives in its own module because of one hard ordering fact (findings F1): the
|
|
4
|
+
// host entry this plugin ships returns early on non-Windows platforms, since the
|
|
5
|
+
// *model tool* is Windows-only. The browser terminal is not a Windows-only idea,
|
|
6
|
+
// so its mount has to happen before that return - and keeping it in one call makes
|
|
7
|
+
// that ordering visible at the call site instead of buried in a 1000-line apply().
|
|
8
|
+
//
|
|
9
|
+
// Everything environment-dependent arrives as live reads (config fields are
|
|
10
|
+
// `.volatile()` on the 0.1.7+ lines, and older lines re-read from the same
|
|
11
|
+
// closure), never a snapshot: a switch flipped in the settings page must apply to
|
|
12
|
+
// the next allocation without a restart.
|
|
13
|
+
import { createCatalogProvider } from "./catalog.js";
|
|
14
|
+
import { listWslDistros } from "./catalog.js";
|
|
15
|
+
import { reportSettingsSurface, settingsSurface } from "../settings-surface.js";
|
|
16
|
+
import { registerUiTerminalRoutes } from "./host.js";
|
|
17
|
+
import { UiTerminalRegistry } from "./registry.js";
|
|
18
|
+
import { createUiSpawner, detectBackend } from "./pty.js";
|
|
19
|
+
import { SHELL_PRIORITY } from "../shells.js";
|
|
20
|
+
/** How many terminals one session may hold, and how many exist host-wide. */
|
|
21
|
+
const MAX_PER_SESSION = 6;
|
|
22
|
+
const MAX_TOTAL = 12;
|
|
23
|
+
/** Detached-shell grace: long enough to survive a page reload, short enough to
|
|
24
|
+
* not leak a shell someone abandoned. */
|
|
25
|
+
const IDLE_GRACE_MS = 120_000;
|
|
26
|
+
/** Replayed screen budget per terminal. */
|
|
27
|
+
const SCROLLBACK_BYTES = 200_000;
|
|
28
|
+
/** Read a config field that may be a value or a live ref. */
|
|
29
|
+
function live(value, fallback) {
|
|
30
|
+
if (typeof value === "object" && value !== null && typeof value.get === "function") {
|
|
31
|
+
const got = value.get();
|
|
32
|
+
return got === undefined ? fallback : got;
|
|
33
|
+
}
|
|
34
|
+
return value === undefined ? fallback : value;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Mount the browser terminal: routes, socket, registry, catalog cache.
|
|
38
|
+
* @returns the disposer and the availability answer the settings page reads.
|
|
39
|
+
*/
|
|
40
|
+
export function mountBrowserTerminals(input) {
|
|
41
|
+
const platform = input.platform ?? process.platform;
|
|
42
|
+
const config = input.config;
|
|
43
|
+
/**
|
|
44
|
+
* In-process overrides for the fields this feature writes.
|
|
45
|
+
*
|
|
46
|
+
* Hosts up to 0.1.6 have no config hot-reload: `settings.mutate` persists the
|
|
47
|
+
* value, but the object handed to `apply` never changes, so a switch flipped in
|
|
48
|
+
* the settings page would only take effect after a restart. The plugin's legacy
|
|
49
|
+
* bridge already solves that for `defaultShell` with a `shellOverride`; this is
|
|
50
|
+
* the same mechanism for the activation switches. On 0.1.7+ the `.volatile()` ref
|
|
51
|
+
* is already live, the map merely shadows it with the value we just wrote - a
|
|
52
|
+
* no-op in the good case, the fix in the old one.
|
|
53
|
+
*/
|
|
54
|
+
const overrides = new Map();
|
|
55
|
+
const readField = (key, fallback) => {
|
|
56
|
+
if (overrides.has(key))
|
|
57
|
+
return overrides.get(key);
|
|
58
|
+
return live(config[key], fallback);
|
|
59
|
+
};
|
|
60
|
+
const switches = () => ({
|
|
61
|
+
// Powershell is not in here: it is the floor and has no switch by design (R2).
|
|
62
|
+
gitbash: readField("shellGitBash", true),
|
|
63
|
+
msys2: readField("shellMsys2", true),
|
|
64
|
+
wsl: readField("shellWsl", true),
|
|
65
|
+
});
|
|
66
|
+
const wslDistro = () => readField("wslDistro", "").trim();
|
|
67
|
+
/** The master switch: off hides the child switches and refuses allocations. */
|
|
68
|
+
const enabled = () => readField("bottomTerminal", true);
|
|
69
|
+
/**
|
|
70
|
+
* Opt-in to publishing `webTerminals` for the official bottom row. Off by default and
|
|
71
|
+
* deliberately NOT derived from "no controller detected": the client-side check that
|
|
72
|
+
* would be needed for that is a boot-time race, and losing it takes down the host's
|
|
73
|
+
* own terminal (findings D15-1).
|
|
74
|
+
*/
|
|
75
|
+
const shimOfficialTerminal = () => readField("shimOfficialTerminal", false);
|
|
76
|
+
// The WSL distro list, probed LAZILY on the first catalog read and re-probed
|
|
77
|
+
// after a switch write. `undefined` is "still unknown / the probe timed out",
|
|
78
|
+
// which the catalog treats as neutral rather than as evidence of absence.
|
|
79
|
+
// Lazy matters: mounting the routes must not spawn `wsl.exe` in a process that
|
|
80
|
+
// never opens a terminal (unit mocks, headless hosts, non-WSL machines).
|
|
81
|
+
let distros;
|
|
82
|
+
let probing = false;
|
|
83
|
+
const reprobe = () => {
|
|
84
|
+
probing = true;
|
|
85
|
+
void listWslDistros(input.paths.wsl).then((found) => {
|
|
86
|
+
distros = found;
|
|
87
|
+
probing = false;
|
|
88
|
+
catalog.invalidate();
|
|
89
|
+
}, () => {
|
|
90
|
+
probing = false;
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
const catalog = createCatalogProvider({
|
|
94
|
+
paths: () => input.paths,
|
|
95
|
+
switches,
|
|
96
|
+
wslDistro,
|
|
97
|
+
distros: () => {
|
|
98
|
+
if (distros === undefined && !probing)
|
|
99
|
+
reprobe();
|
|
100
|
+
return distros;
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
const backend = detectBackend({ platform, seam: input.ctx.subprocess });
|
|
104
|
+
const spawner = backend === undefined ? undefined : createUiSpawner({ platform, seam: input.ctx.subprocess });
|
|
105
|
+
const registry = new UiTerminalRegistry({
|
|
106
|
+
maxPerSession: MAX_PER_SESSION,
|
|
107
|
+
maxTotal: MAX_TOTAL,
|
|
108
|
+
idleGraceMs: IDLE_GRACE_MS,
|
|
109
|
+
scrollback: SCROLLBACK_BYTES,
|
|
110
|
+
spawn: async (spec) => {
|
|
111
|
+
if (spawner === undefined)
|
|
112
|
+
throw new Error("no PTY backend on this host");
|
|
113
|
+
return await spawner.spawn(spec);
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
let unavailable = "not-installed";
|
|
117
|
+
let routes;
|
|
118
|
+
const isLoopback = (request) => {
|
|
119
|
+
const addr = request.socket?.remoteAddress ?? "";
|
|
120
|
+
return addr === "127.0.0.1" || addr === "::1" || addr === "::ffff:127.0.0.1";
|
|
121
|
+
};
|
|
122
|
+
const cwdFor = (sessionId) => {
|
|
123
|
+
const sessions = (input.ctx.get?.("sessions") ?? undefined);
|
|
124
|
+
return sessions?.get?.(sessionId)?.header?.cwd ?? process.cwd();
|
|
125
|
+
};
|
|
126
|
+
// webServer arrives only through injection; until then the routes report their
|
|
127
|
+
// absence rather than pretending a terminal is available.
|
|
128
|
+
input.ctx.inject?.(["webServer", "settings"], (svcCtx) => {
|
|
129
|
+
// Classified the moment the settings service arrives: the browser cannot tell which
|
|
130
|
+
// generation it is talking to, and the answer decides whether it paints its own
|
|
131
|
+
// floating panel or stays out of the way.
|
|
132
|
+
reportSettingsSurface(svcCtx.settings);
|
|
133
|
+
routes = registerUiTerminalRoutes(svcCtx.webServer, {
|
|
134
|
+
catalog: () => catalog.catalog(),
|
|
135
|
+
paths: () => input.paths,
|
|
136
|
+
env: (id) => catalog.env(id),
|
|
137
|
+
enabled,
|
|
138
|
+
shimOfficialTerminal,
|
|
139
|
+
settingsSurface,
|
|
140
|
+
configuredShell: input.configuredShell,
|
|
141
|
+
switches,
|
|
142
|
+
wslDistro,
|
|
143
|
+
writeConfig: async (path, value) => {
|
|
144
|
+
await input.persist(svcCtx.settings, path, value);
|
|
145
|
+
// Remember it for this process too (the ≤0.1.6 hot-reload gap above).
|
|
146
|
+
overrides.set(path.join("."), value);
|
|
147
|
+
// A write also forgets the cached catalog and the WSL answer, so the
|
|
148
|
+
// settings row never reports a stale result immediately after a click.
|
|
149
|
+
distros = undefined;
|
|
150
|
+
catalog.invalidate();
|
|
151
|
+
},
|
|
152
|
+
registry,
|
|
153
|
+
cwdFor,
|
|
154
|
+
unavailableReason: () => unavailable,
|
|
155
|
+
isLoopback,
|
|
156
|
+
});
|
|
157
|
+
unavailable = backend === undefined ? "no-pty-backend" : routes.socket === "mounted" ? "ok" : routes.socket;
|
|
158
|
+
input.ctx.logger?.info?.(`dsh-bash-terminal-ts: browser terminal routes ${unavailable} (backend=${backend ?? "none"}, priority=${SHELL_PRIORITY.join(">")})`);
|
|
159
|
+
svcCtx.effect(() => () => {
|
|
160
|
+
routes?.dispose();
|
|
161
|
+
void registry.dispose();
|
|
162
|
+
}, "bash-terminal: browser terminal routes");
|
|
163
|
+
});
|
|
164
|
+
return {
|
|
165
|
+
unavailable: () => unavailable,
|
|
166
|
+
dispose: () => {
|
|
167
|
+
routes?.dispose();
|
|
168
|
+
void registry.dispose();
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { ShellId } from "../shell-id.js";
|
|
2
|
+
/** Client -> host frames (input side of a terminal socket). */
|
|
3
|
+
export type UiClientFrame = {
|
|
4
|
+
readonly t: "input";
|
|
5
|
+
readonly data: string;
|
|
6
|
+
} | {
|
|
7
|
+
readonly t: "resize";
|
|
8
|
+
readonly cols: number;
|
|
9
|
+
readonly rows: number;
|
|
10
|
+
} | {
|
|
11
|
+
readonly t: "signal";
|
|
12
|
+
readonly signal: string;
|
|
13
|
+
} | {
|
|
14
|
+
readonly t: "close";
|
|
15
|
+
} | {
|
|
16
|
+
readonly t: "ping";
|
|
17
|
+
};
|
|
18
|
+
/** Host -> client frames (screen side of a terminal socket). */
|
|
19
|
+
export type UiServerFrame =
|
|
20
|
+
/** The buffered screen, then live output follows. Always the first frame. */
|
|
21
|
+
{
|
|
22
|
+
readonly t: "snapshot";
|
|
23
|
+
readonly screen: string;
|
|
24
|
+
readonly info: UiTerminalInfo;
|
|
25
|
+
readonly environment: UiEnvironment;
|
|
26
|
+
} | {
|
|
27
|
+
readonly t: "data";
|
|
28
|
+
readonly data: string;
|
|
29
|
+
} | {
|
|
30
|
+
readonly t: "exit";
|
|
31
|
+
readonly info: UiTerminalInfo;
|
|
32
|
+
} | {
|
|
33
|
+
readonly t: "resized";
|
|
34
|
+
readonly resized: boolean;
|
|
35
|
+
} | {
|
|
36
|
+
readonly t: "pong";
|
|
37
|
+
} | {
|
|
38
|
+
readonly t: "error";
|
|
39
|
+
readonly code: string;
|
|
40
|
+
readonly message: string;
|
|
41
|
+
};
|
|
42
|
+
/** The shell a terminal was allocated with (same shape the host controller uses). */
|
|
43
|
+
export interface UiShellSpec {
|
|
44
|
+
readonly path: string;
|
|
45
|
+
readonly args: readonly string[];
|
|
46
|
+
readonly name: string;
|
|
47
|
+
}
|
|
48
|
+
/** One running (or ended) UI terminal. */
|
|
49
|
+
export interface UiTerminalInfo {
|
|
50
|
+
readonly id: string;
|
|
51
|
+
readonly title: string;
|
|
52
|
+
/** Which catalog row this shell came from (display and diagnostics). */
|
|
53
|
+
readonly shellId: ShellId | "cmd";
|
|
54
|
+
/** The launched executable and its argv, so the browser needs no second lookup. */
|
|
55
|
+
readonly shell: UiShellSpec;
|
|
56
|
+
/** The cwd the shell was allocated with; a `cd` inside it does not update this. */
|
|
57
|
+
readonly cwd: string;
|
|
58
|
+
readonly cols: number;
|
|
59
|
+
readonly rows: number;
|
|
60
|
+
readonly state: "running" | "exited" | "failed";
|
|
61
|
+
readonly exitCode: number | null;
|
|
62
|
+
readonly error?: string;
|
|
63
|
+
}
|
|
64
|
+
/** Per-terminal limits (mirrors the host controller's `TerminalEnvironment`). */
|
|
65
|
+
export interface UiEnvironment {
|
|
66
|
+
readonly cwd: string;
|
|
67
|
+
readonly maxInputBytes: number;
|
|
68
|
+
readonly maxCols: number;
|
|
69
|
+
readonly maxRows: number;
|
|
70
|
+
readonly scrollback: number;
|
|
71
|
+
}
|
|
72
|
+
/** Why the bridge cannot serve a terminal on this deployment. */
|
|
73
|
+
export type UiUnavailableReason = "ok" | "no-pty-backend" | "no-websocket-route" | "not-installed";
|
|
74
|
+
/** The `GET /api/bash-terminal/config` reply (extended by this feature). */
|
|
75
|
+
export interface UiConfigPayload {
|
|
76
|
+
readonly defaultShell: string;
|
|
77
|
+
readonly shells: readonly string[];
|
|
78
|
+
readonly seam: {
|
|
79
|
+
readonly spawn: string;
|
|
80
|
+
readonly spawnTerminal: string;
|
|
81
|
+
};
|
|
82
|
+
/** The catalog the settings page renders and the picker offers. */
|
|
83
|
+
readonly catalog?: unknown;
|
|
84
|
+
/** Which shell an allocation WOULD use, after the fallback chain. */
|
|
85
|
+
readonly effective?: {
|
|
86
|
+
readonly id: string;
|
|
87
|
+
readonly fellBack: boolean;
|
|
88
|
+
readonly floor: boolean;
|
|
89
|
+
readonly from?: string;
|
|
90
|
+
};
|
|
91
|
+
/** Whether the browser shim should take over the host terminal service. */
|
|
92
|
+
readonly hostTerminals?: boolean;
|
|
93
|
+
readonly unavailable?: UiUnavailableReason;
|
|
94
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// The browser terminal's own wire vocabulary (Node-free: both halves compile it).
|
|
2
|
+
//
|
|
3
|
+
// This is NOT the host controller's protocol - the controller is absent from
|
|
4
|
+
// some deployments, which is the whole reason this module exists. The browser
|
|
5
|
+
// shim translates these frames into the `TerminalFrame` shape
|
|
6
|
+
// dsh-better-sidebar's terminal view expects, so the emulator never sees a
|
|
7
|
+
// private format. Keeping one shared module is what stops the two halves from
|
|
8
|
+
// drifting apart on a string literal.
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { TerminalSpawnSpec } from "../dsh-types.js";
|
|
2
|
+
import type { UiPtyHandle, UiSpawnSpec } from "./registry.js";
|
|
3
|
+
/** Which allocator a terminal would be created with. */
|
|
4
|
+
export type PtyBackend = "node-pty" | "seam";
|
|
5
|
+
/** The host seam, narrowed to the one call this module makes. */
|
|
6
|
+
export interface SeamLike {
|
|
7
|
+
spawnTerminal?(spec: TerminalSpawnSpec & {
|
|
8
|
+
terminalType?: string;
|
|
9
|
+
}): Promise<{
|
|
10
|
+
pid: number;
|
|
11
|
+
output: NodeJS.ReadableStream;
|
|
12
|
+
done: Promise<{
|
|
13
|
+
exitCode: number | null;
|
|
14
|
+
signal?: string | null;
|
|
15
|
+
}>;
|
|
16
|
+
write(data: string): void | Promise<void>;
|
|
17
|
+
resize?(cols: number, rows: number): void | Promise<void>;
|
|
18
|
+
signalForeground?(signal: string): void | Promise<unknown>;
|
|
19
|
+
terminate(): void | Promise<void>;
|
|
20
|
+
}>;
|
|
21
|
+
}
|
|
22
|
+
/** What the caller must report about the deployment. */
|
|
23
|
+
export interface SpawnerDeps {
|
|
24
|
+
platform: NodeJS.Platform;
|
|
25
|
+
/** The host seam, or undefined/absent when this build has none. */
|
|
26
|
+
seam: SeamLike | undefined;
|
|
27
|
+
/** TERM->SIGKILL grace handed to the seam on allocation. */
|
|
28
|
+
seamGraceMs?: number;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Which backend this deployment gets. `undefined` means neither is reachable and
|
|
32
|
+
* the browser must be told `no-pty-backend` rather than shown a dead terminal.
|
|
33
|
+
*/
|
|
34
|
+
export declare function detectBackend(deps: SpawnerDeps): PtyBackend | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Build the allocator the registry calls.
|
|
37
|
+
* @throws Error when `detectBackend` found nothing (callers must check first).
|
|
38
|
+
*/
|
|
39
|
+
export declare function createUiSpawner(deps: SpawnerDeps): {
|
|
40
|
+
backend: PtyBackend;
|
|
41
|
+
spawn(spec: UiSpawnSpec): Promise<UiPtyHandle>;
|
|
42
|
+
};
|