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,70 @@
|
|
|
1
|
+
// Absolute bases and prefix-safe resolution for this plugin's own host routes.
|
|
2
|
+
//
|
|
3
|
+
// The two facts here are the ones that break a plugin on the Electron shell, and
|
|
4
|
+
// both were learned the hard way upstream (adapted from dsh-better-sidebar's
|
|
5
|
+
// `desktop-env.ts` / `host-route-url.ts`, MIT):
|
|
6
|
+
//
|
|
7
|
+
// - the shell serves the GUI from `dsh-app://app/`, whose `location.host` is the
|
|
8
|
+
// literal string `app`; resolving a `ws:` URL against the PAGE base produces
|
|
9
|
+
// `ws://app/...`, which never completes a DNS lookup. The shell publishes the
|
|
10
|
+
// Host's real base as `__DSH_TRANSPORT__.streamBaseUrl`, and the Host accepts
|
|
11
|
+
// the shell page's origin on those sockets;
|
|
12
|
+
// - HTTP must STAY on the page base: the shell's protocol handler forwards every
|
|
13
|
+
// non-static `dsh-app://app` path to the Host same-origin, while the Host's
|
|
14
|
+
// plugin routes send no `Access-Control-Allow-*` headers, so a cross-origin POST
|
|
15
|
+
// to the injected origin is blocked outright ("Failed to fetch").
|
|
16
|
+
//
|
|
17
|
+
// A URL built with a LEADING SLASH pins itself to the origin root, so under a
|
|
18
|
+
// reverse-proxy directory (`https://host/dsh/proxy/3080/`) every request leaves the
|
|
19
|
+
// prefix. Resolving a RELATIVE path against the page's own base keeps it inside -
|
|
20
|
+
// which is why leading slashes are stripped on purpose below.
|
|
21
|
+
|
|
22
|
+
/** The transport facts the desktop shell injects before any plugin script runs. */
|
|
23
|
+
interface ShellTransport {
|
|
24
|
+
/** The Host's real origin, the base every DSH socket resolves against. */
|
|
25
|
+
streamBaseUrl?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Absolute base for non-DOM callers (specs): resolution needs one, and the request is never issued. */
|
|
29
|
+
const PLACEHOLDER_ORIGIN = "http://dsh.internal/";
|
|
30
|
+
|
|
31
|
+
function injectedTransportBase(): string | undefined {
|
|
32
|
+
const transport = (globalThis as { __DSH_TRANSPORT__?: ShellTransport }).__DSH_TRANSPORT__;
|
|
33
|
+
const base = transport?.streamBaseUrl;
|
|
34
|
+
return typeof base === "string" && base !== "" ? base : undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The socket base: the Host's real origin, else the page base, else ''. */
|
|
38
|
+
export function hostTransportBase(): string {
|
|
39
|
+
const injected = injectedTransportBase();
|
|
40
|
+
if (injected !== undefined) return injected;
|
|
41
|
+
return typeof document !== "undefined" && typeof document.baseURI === "string" ? document.baseURI : "";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The HTTP base: always the PAGE's own base (see the module doc). */
|
|
45
|
+
export function hostHttpBase(): string {
|
|
46
|
+
const base = typeof document !== "undefined" ? document.baseURI : undefined;
|
|
47
|
+
if (typeof base === "string" && base !== "") return base;
|
|
48
|
+
return hostTransportBase();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Drop the query/hash and force a trailing slash (a directory base). */
|
|
52
|
+
function directoryBase(baseUrl: string): string {
|
|
53
|
+
const url = new URL(baseUrl === "" ? PLACEHOLDER_ORIGIN : baseUrl, PLACEHOLDER_ORIGIN);
|
|
54
|
+
url.search = "";
|
|
55
|
+
url.hash = "";
|
|
56
|
+
if (!url.pathname.endsWith("/")) url.pathname += "/";
|
|
57
|
+
return url.href;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** One HTTP route URL, resolved against the page base. */
|
|
61
|
+
export function hostRouteUrl(path: string, baseUrl: string = hostHttpBase()): URL {
|
|
62
|
+
return new URL(path.replace(/^\/+/, ""), directoryBase(baseUrl));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** One WebSocket URL (the `ws:`/`wss:` scheme `WebSocket` requires). */
|
|
66
|
+
export function hostWebSocketUrl(path: string, baseUrl: string = hostTransportBase()): URL {
|
|
67
|
+
const url = hostRouteUrl(path, baseUrl);
|
|
68
|
+
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
69
|
+
return url;
|
|
70
|
+
}
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
// One terminal occurrence's model: the `TerminalView` shape dsh-better-sidebar's
|
|
2
|
+
// bottom terminal tab drives, backed by this plugin's socket instead of the host
|
|
3
|
+
// controller's Remote.
|
|
4
|
+
//
|
|
5
|
+
// The rules the view depends on (read off `TerminalView.tsx`; violating any one
|
|
6
|
+
// gives a blank screen or a stream that wedges):
|
|
7
|
+
//
|
|
8
|
+
// - **A view survives DOM unmount.** `mount()` returns a detach that only ends the
|
|
9
|
+
// *attachment*; the process lives on so a tab switch, a collapsed panel or a
|
|
10
|
+
// reload re-adopts the same shell. Only `close()` ends it.
|
|
11
|
+
// - **One un-acknowledged frame at a time.** `state.render` holds the next frame;
|
|
12
|
+
// the view releases it through `acknowledge(revision)` inside xterm's write
|
|
13
|
+
// callback. Publishing the following frame before that ACK loses output for
|
|
14
|
+
// good, so `pump()` is the ONLY writer of `render` and it refuses while a frame
|
|
15
|
+
// is outstanding.
|
|
16
|
+
// - **A `snapshot` frame must be REPLAYED** (the view resets + resizes + writes
|
|
17
|
+
// it); an `output` frame is appended. Hence the first frame per attach is always
|
|
18
|
+
// a snapshot of the buffered screen.
|
|
19
|
+
// - **`environment` must exist before the view fits** - `fitScreen()` returns early
|
|
20
|
+
// without it, so the emulator would never size itself.
|
|
21
|
+
// - **`writable` gates input and resize** (the host serialises one attachment's
|
|
22
|
+
// keystrokes); the view disables stdin when it is false.
|
|
23
|
+
//
|
|
24
|
+
// Only `missingTerminal` is surfaced as an `issue`: it is the one code the view
|
|
25
|
+
// renders a dedicated notice for, and the one where a Retry would be a lie (the
|
|
26
|
+
// process is gone). Every other refusal lands in `state.error`, next to which the
|
|
27
|
+
// view already offers its retry affordance.
|
|
28
|
+
|
|
29
|
+
import { BridgeError, closeTerminal, openTerminal, terminalSocketUrl } from "./api";
|
|
30
|
+
import type {
|
|
31
|
+
TerminalRenderFrameContent,
|
|
32
|
+
TerminalViewContract,
|
|
33
|
+
TerminalViewState,
|
|
34
|
+
WebTerminalId,
|
|
35
|
+
WebTerminalInfo,
|
|
36
|
+
TerminalEnvironment,
|
|
37
|
+
} from "./face";
|
|
38
|
+
import { initialViewState } from "./face";
|
|
39
|
+
import { Store } from "./store";
|
|
40
|
+
import type { UiServerFrame } from "../protocol";
|
|
41
|
+
|
|
42
|
+
/** The geometry a terminal is allocated with, before the panel has measured. */
|
|
43
|
+
const INITIAL_COLS = 80;
|
|
44
|
+
const INITIAL_ROWS = 24;
|
|
45
|
+
|
|
46
|
+
/** The host hooks one view needs from its owning service. */
|
|
47
|
+
export interface BridgeViewHooks {
|
|
48
|
+
/** Save the `(session, contentId) -> terminalId` binding (a reload adopts). */
|
|
49
|
+
save(contentId: string, terminalId: WebTerminalId): void;
|
|
50
|
+
/** Drop a binding whose process is gone, so a retry never re-adopts it. */
|
|
51
|
+
forget(contentId: string): void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The socket surface this module drives (injectable for specs). */
|
|
55
|
+
interface SocketLike {
|
|
56
|
+
readyState: number;
|
|
57
|
+
send(data: string): void;
|
|
58
|
+
close(): void;
|
|
59
|
+
onopen: (() => void) | null;
|
|
60
|
+
onmessage: ((event: { data: unknown }) => void) | null;
|
|
61
|
+
onclose: (() => void) | null;
|
|
62
|
+
onerror: (() => void) | null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** One bridge-backed terminal view. */
|
|
66
|
+
export class BridgeTerminalView implements TerminalViewContract {
|
|
67
|
+
/** The client-side occurrence identity (stable; the host id lives in `info`). */
|
|
68
|
+
readonly id: WebTerminalId;
|
|
69
|
+
readonly state: Store<TerminalViewState>;
|
|
70
|
+
|
|
71
|
+
private readonly session: string;
|
|
72
|
+
private readonly contentId: string;
|
|
73
|
+
private readonly hooks: BridgeViewHooks;
|
|
74
|
+
private terminalId: WebTerminalId | undefined;
|
|
75
|
+
private socket: SocketLike | undefined;
|
|
76
|
+
private started = false;
|
|
77
|
+
private disposed = false;
|
|
78
|
+
/** Frames waiting for their turn (at most one is ever published). */
|
|
79
|
+
private readonly queue: TerminalRenderFrameContent[] = [];
|
|
80
|
+
private revision = 0;
|
|
81
|
+
private sequence = 0;
|
|
82
|
+
/** Input typed while the socket was still connecting (a keystroke is never dropped). */
|
|
83
|
+
private readonly outbox: string[] = [];
|
|
84
|
+
private inflight: Promise<void> | undefined;
|
|
85
|
+
|
|
86
|
+
constructor(sessionId: string, contentId: string, hooks: BridgeViewHooks, terminalId?: WebTerminalId) {
|
|
87
|
+
this.session = sessionId;
|
|
88
|
+
this.contentId = contentId;
|
|
89
|
+
this.hooks = hooks;
|
|
90
|
+
this.terminalId = terminalId;
|
|
91
|
+
this.id = terminalId ?? `bridge-${Math.random().toString(36).slice(2, 10)}`;
|
|
92
|
+
this.state = new Store<TerminalViewState>(initialViewState());
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// ── lifecycle ────────────────────────────────────────────────────────────
|
|
96
|
+
|
|
97
|
+
/** Attach the DOM lifetime; the detach leaves the shell running. */
|
|
98
|
+
mount = (): (() => void) => {
|
|
99
|
+
if (!this.started) {
|
|
100
|
+
void this.refresh();
|
|
101
|
+
} else if (this.terminalId !== undefined && this.socket === undefined) {
|
|
102
|
+
this.connect();
|
|
103
|
+
}
|
|
104
|
+
return () => {
|
|
105
|
+
// The DOM goes, the process stays: only the attachment is dropped, which is
|
|
106
|
+
// what starts the host's idle grace and lets a remount re-adopt. Input
|
|
107
|
+
// control goes with the socket; the phase stays as it is because nobody reads
|
|
108
|
+
// it while the component is unmounted (a remount runs `connect()`).
|
|
109
|
+
this.closeSocket();
|
|
110
|
+
this.patch({ writable: false });
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/** Start or recover this tab, deduplicated while an attempt is in flight. */
|
|
115
|
+
refresh = async (): Promise<void> => {
|
|
116
|
+
if (this.disposed) return;
|
|
117
|
+
if (this.inflight !== undefined) return this.inflight;
|
|
118
|
+
this.started = true;
|
|
119
|
+
const run = (async (): Promise<void> => {
|
|
120
|
+
this.patch({ phase: "loading", issue: undefined, error: undefined });
|
|
121
|
+
try {
|
|
122
|
+
this.patch({ phase: "creating" });
|
|
123
|
+
// The HOST decides which shell this is (catalog + activation + fallback),
|
|
124
|
+
// so a stale or disabled choice can never launch anything.
|
|
125
|
+
const result = await openTerminal({
|
|
126
|
+
sessionId: this.session,
|
|
127
|
+
terminalId: this.terminalId,
|
|
128
|
+
cols: INITIAL_COLS,
|
|
129
|
+
rows: INITIAL_ROWS,
|
|
130
|
+
});
|
|
131
|
+
const info = result.info as WebTerminalInfo;
|
|
132
|
+
this.terminalId = info.id;
|
|
133
|
+
this.hooks.save(this.contentId, info.id);
|
|
134
|
+
// `environment` lands here, before the emulator mounts: without it the
|
|
135
|
+
// view's fit step never runs and the screen keeps its default size.
|
|
136
|
+
this.patch({ phase: "connecting", environment: result.environment as TerminalEnvironment, info });
|
|
137
|
+
this.openSocket(info.id);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
this.fail(error);
|
|
140
|
+
}
|
|
141
|
+
})();
|
|
142
|
+
this.inflight = run;
|
|
143
|
+
try {
|
|
144
|
+
await run;
|
|
145
|
+
} finally {
|
|
146
|
+
this.inflight = undefined;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/** Reattach with a fresh screen and regain input control. */
|
|
151
|
+
connect = (): void => {
|
|
152
|
+
if (this.disposed) return;
|
|
153
|
+
if (this.terminalId === undefined) {
|
|
154
|
+
void this.refresh();
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
this.started = true;
|
|
158
|
+
this.patch({ phase: "connecting", issue: undefined, error: undefined });
|
|
159
|
+
this.openSocket(this.terminalId);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
/** End the process AND the client work (the tab's `onClose` path). */
|
|
163
|
+
close = async (): Promise<void> => {
|
|
164
|
+
if (this.disposed) return;
|
|
165
|
+
this.patch({ phase: "closing" });
|
|
166
|
+
this.hooks.forget(this.contentId);
|
|
167
|
+
const id = this.terminalId;
|
|
168
|
+
this.terminalId = undefined;
|
|
169
|
+
this.closeSocket();
|
|
170
|
+
this.disposed = true;
|
|
171
|
+
this.queue.length = 0;
|
|
172
|
+
this.outbox.length = 0;
|
|
173
|
+
this.patch({ phase: "closed", writable: false, render: undefined });
|
|
174
|
+
if (id === undefined) return;
|
|
175
|
+
try {
|
|
176
|
+
await closeTerminal(id);
|
|
177
|
+
} catch (error) {
|
|
178
|
+
// A cleanup that failed leaves a shell the host's reaper still catches;
|
|
179
|
+
// surfacing it as a terminal error would make a closed tab look broken.
|
|
180
|
+
console.warn("[dsh-bash-terminal-ts] terminal close was refused", error);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/** Stop client work on unload WITHOUT ending the process (the host's rule). */
|
|
185
|
+
dispose = async (): Promise<void> => {
|
|
186
|
+
this.disposed = true;
|
|
187
|
+
this.closeSocket();
|
|
188
|
+
this.queue.length = 0;
|
|
189
|
+
this.outbox.length = 0;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
// ── stream ────────────────────────────────────────────────────────────────
|
|
193
|
+
|
|
194
|
+
/** Release the next stream item after the emulator parsed this frame. */
|
|
195
|
+
acknowledge = (revision: number): void => {
|
|
196
|
+
const current = this.state.getSnapshot().render;
|
|
197
|
+
if (current !== undefined && revision >= current.revision) this.patch({ render: undefined });
|
|
198
|
+
this.pump();
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
/** Serialize raw input (refused unless this view holds control). */
|
|
202
|
+
write = (data: string): void => {
|
|
203
|
+
if (data === "" || !this.state.getSnapshot().writable) return;
|
|
204
|
+
if (this.socket !== undefined && this.socket.readyState === socketOpen()) {
|
|
205
|
+
// A socket frame is ordered by the transport, so input goes out synchronously:
|
|
206
|
+
// chaining every keystroke through a promise would add a hop per character
|
|
207
|
+
// for a serialisation the wire already guarantees.
|
|
208
|
+
this.send({ t: "input", data });
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
// CONNECTING: hold it, flush on open. A dropped keystroke is worse than a late
|
|
212
|
+
// one, and the socket cannot be gone while `writable` is true.
|
|
213
|
+
this.outbox.push(data);
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
/** Resize only from the currently writable view. */
|
|
217
|
+
resize = (cols: number, rows: number): void => {
|
|
218
|
+
if (!this.state.getSnapshot().writable) return;
|
|
219
|
+
this.send({ t: "resize", cols, rows });
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/** This bridge has no rename surface (the tab strip uses the descriptor title). */
|
|
223
|
+
rename = async (): Promise<void> => {};
|
|
224
|
+
|
|
225
|
+
// ── internals ─────────────────────────────────────────────────────────────
|
|
226
|
+
|
|
227
|
+
/** Open (or replace) the screen socket for one terminal. */
|
|
228
|
+
private openSocket(id: WebTerminalId): void {
|
|
229
|
+
this.closeSocket();
|
|
230
|
+
let socket: SocketLike;
|
|
231
|
+
try {
|
|
232
|
+
socket = new WebSocket(terminalSocketUrl(id).toString()) as unknown as SocketLike;
|
|
233
|
+
} catch (error) {
|
|
234
|
+
this.fail(error);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
this.socket = socket;
|
|
238
|
+
socket.onopen = () => {
|
|
239
|
+
if (this.socket !== socket) return;
|
|
240
|
+
for (const data of this.outbox.splice(0)) this.send({ t: "input", data });
|
|
241
|
+
};
|
|
242
|
+
socket.onmessage = (event: { data: unknown }) => {
|
|
243
|
+
if (this.socket !== socket) return;
|
|
244
|
+
this.onFrame(event.data);
|
|
245
|
+
};
|
|
246
|
+
socket.onclose = () => {
|
|
247
|
+
if (this.socket !== socket) return;
|
|
248
|
+
this.socket = undefined;
|
|
249
|
+
if (this.disposed) return;
|
|
250
|
+
// The attachment ended, not the process: `connect()` (the view's Retry)
|
|
251
|
+
// re-adopts it. Input control goes with the socket.
|
|
252
|
+
const had = this.state.getSnapshot().info !== undefined;
|
|
253
|
+
this.patch({ writable: false, phase: had ? "disconnected" : "failed", error: had ? undefined : "the terminal socket closed before it answered" });
|
|
254
|
+
};
|
|
255
|
+
socket.onerror = () => {
|
|
256
|
+
// `onclose` always follows and owns the state; nothing to add here.
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Close the current socket without touching the process. */
|
|
261
|
+
private closeSocket(): void {
|
|
262
|
+
const socket = this.socket;
|
|
263
|
+
this.socket = undefined;
|
|
264
|
+
if (socket === undefined) return;
|
|
265
|
+
try {
|
|
266
|
+
socket.onclose = null;
|
|
267
|
+
socket.onmessage = null;
|
|
268
|
+
socket.onopen = null;
|
|
269
|
+
socket.onerror = null;
|
|
270
|
+
socket.close();
|
|
271
|
+
} catch {
|
|
272
|
+
// A socket that refuses to close is the browser's problem, not the model's.
|
|
273
|
+
}
|
|
274
|
+
this.outbox.length = 0;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Decode and apply one host frame. */
|
|
278
|
+
private onFrame(raw: unknown): void {
|
|
279
|
+
if (typeof raw !== "string") return;
|
|
280
|
+
let frame: UiServerFrame;
|
|
281
|
+
try {
|
|
282
|
+
frame = JSON.parse(raw) as UiServerFrame;
|
|
283
|
+
} catch {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
switch (frame.t) {
|
|
287
|
+
case "snapshot": {
|
|
288
|
+
this.patch({ info: frame.info, environment: frame.environment, phase: "connected", writable: true, issue: undefined, error: undefined });
|
|
289
|
+
this.enqueue({ type: "snapshot", sequence: ++this.sequence, screen: frame.screen, info: frame.info });
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
case "data": {
|
|
293
|
+
this.enqueue({ type: "output", sequence: ++this.sequence, data: frame.data });
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
case "exit": {
|
|
297
|
+
this.onExit(frame.info);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
case "error": {
|
|
301
|
+
this.fail(new BridgeError(frame.code, frame.message));
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
default:
|
|
305
|
+
// `pong` / `resized` carry nothing the model reflects: the emulator already
|
|
306
|
+
// resized itself, and a process that refuses it is a host fact, not an error.
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Announce the end: keep what is queued, hold the screen, drop input control. */
|
|
312
|
+
private onExit(info: WebTerminalInfo): void {
|
|
313
|
+
// The queue is NOT dropped: a shell that ran `ls` and exited still owes the
|
|
314
|
+
// emulator its last write, and losing it is exactly the "blank screen after a
|
|
315
|
+
// short command" bug.
|
|
316
|
+
this.patch({ info, writable: false, phase: "closed" });
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** Queue one screen frame and try to publish it. */
|
|
320
|
+
private enqueue(frame: TerminalRenderFrameContent): void {
|
|
321
|
+
const last = this.queue[this.queue.length - 1];
|
|
322
|
+
// Coalesce consecutive output: a burst would otherwise mint one ACK round per
|
|
323
|
+
// chunk, and each round costs a full emulator write.
|
|
324
|
+
if (frame.type === "output" && last !== undefined && last.type === "output") {
|
|
325
|
+
(last as { data: string }).data += frame.data;
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
this.queue.push(frame);
|
|
329
|
+
this.pump();
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** Publish the next frame, but only when nothing is outstanding. */
|
|
333
|
+
private pump(): void {
|
|
334
|
+
if (this.disposed) return;
|
|
335
|
+
if (this.state.getSnapshot().render !== undefined) return;
|
|
336
|
+
const frame = this.queue.shift();
|
|
337
|
+
if (frame === undefined) return;
|
|
338
|
+
this.patch({ render: { revision: ++this.revision, frame } });
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Send one client frame, swallowing a transport hiccup (close owns the state). */
|
|
342
|
+
private send(frame: Record<string, unknown>): void {
|
|
343
|
+
const socket = this.socket;
|
|
344
|
+
if (socket === undefined || socket.readyState !== socketOpen()) return;
|
|
345
|
+
try {
|
|
346
|
+
socket.send(JSON.stringify(frame));
|
|
347
|
+
} catch {
|
|
348
|
+
// The matching `onclose` sets `disconnected`, which is the honest state.
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** Map one failure onto the state the sidebar view renders. */
|
|
353
|
+
private fail(error: unknown): void {
|
|
354
|
+
if (this.disposed) return;
|
|
355
|
+
const code = error instanceof BridgeError ? error.code : "unknown";
|
|
356
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
357
|
+
if (code === "missingTerminal") {
|
|
358
|
+
// The process is gone (a host restart, or a close elsewhere). Forget the
|
|
359
|
+
// binding so the view's "new terminal" path allocates instead of re-adopting
|
|
360
|
+
// an identity that can never come back.
|
|
361
|
+
this.hooks.forget(this.contentId);
|
|
362
|
+
this.terminalId = undefined;
|
|
363
|
+
this.queue.length = 0;
|
|
364
|
+
this.closeSocket();
|
|
365
|
+
this.patch({ phase: "failed", issue: "missingTerminal", info: undefined, writable: false, render: undefined });
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
this.queue.length = 0;
|
|
369
|
+
this.patch({ phase: "failed", writable: false, render: undefined, error: message });
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** Publish a new state object (never mutate the one React is holding). */
|
|
373
|
+
private patch(partial: Partial<TerminalViewState>): void {
|
|
374
|
+
this.state.set({ ...this.state.getSnapshot(), ...partial });
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** `WebSocket.OPEN`, read through the global so a spec can stub the class. */
|
|
379
|
+
function socketOpen(): number {
|
|
380
|
+
return (globalThis.WebSocket as unknown as { OPEN: number }).OPEN;
|
|
381
|
+
}
|