@gjermundgaraba/clankermux 0.0.0-stage → 0.1.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.md +96 -2
- package/dist/client/attachment.d.ts +71 -0
- package/dist/client/attachment.js +240 -0
- package/dist/client/connection.d.ts +51 -0
- package/dist/client/connection.js +171 -0
- package/dist/client/index.d.ts +10 -0
- package/dist/client/index.js +4 -0
- package/dist/client/keyboard.d.ts +24 -0
- package/dist/client/keyboard.js +32 -0
- package/dist/client/mount.d.ts +65 -0
- package/dist/client/mount.js +177 -0
- package/dist/client/osc52.d.ts +14 -0
- package/dist/client/osc52.js +88 -0
- package/dist/client/profile.d.ts +10 -0
- package/dist/client/profile.js +30 -0
- package/dist/client/worker.d.ts +120 -0
- package/dist/client/worker.js +566 -0
- package/dist/engine/decoder.d.ts +45 -0
- package/dist/engine/decoder.js +137 -0
- package/dist/engine/index.d.ts +110 -0
- package/dist/engine/index.js +372 -0
- package/dist/engine/keys.d.ts +39 -0
- package/dist/engine/keys.js +66 -0
- package/dist/engine/pin.d.ts +2 -0
- package/dist/engine/pin.js +3 -0
- package/dist/engine/render.d.ts +79 -0
- package/dist/engine/render.js +224 -0
- package/dist/proto/frame.d.ts +47 -0
- package/dist/proto/frame.js +78 -0
- package/dist/proto/index.d.ts +3 -0
- package/dist/proto/index.js +3 -0
- package/dist/proto/messages.d.ts +218 -0
- package/dist/proto/messages.js +1 -0
- package/dist/proto/stream.d.ts +11 -0
- package/dist/proto/stream.js +30 -0
- package/dist/renderer/index.d.ts +44 -0
- package/dist/renderer/index.js +138 -0
- package/ghostty-vt.wasm +0 -0
- package/package.json +48 -3
|
@@ -0,0 +1,566 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The engine side of a mounted terminal: the connection, the attachment
|
|
3
|
+
* and the render state live in a worker; the page draws frames and
|
|
4
|
+
* reports input. Imported as `@gjermundgaraba/clankermux/worker`, so a worker
|
|
5
|
+
* typechecked without the DOM library does not see the page's types.
|
|
6
|
+
*/
|
|
7
|
+
import { ENGINE, Engine, KeyEncoder, RenderState } from "../engine/index.js";
|
|
8
|
+
import { FrameType, } from "../proto/index.js";
|
|
9
|
+
import { Attachment } from "./attachment.js";
|
|
10
|
+
import { ClientError, Connection, hello } from "./connection.js";
|
|
11
|
+
import { Osc52Scanner } from "./osc52.js";
|
|
12
|
+
/** Coalescing window for paints. */
|
|
13
|
+
const PAINT_MS = 16;
|
|
14
|
+
const BACKOFF_MIN_MS = 250;
|
|
15
|
+
const BACKOFF_MAX_MS = 5000;
|
|
16
|
+
/** Ending errors of an OPEN that a new OPEN would only get again. */
|
|
17
|
+
const PERMANENT = new Set(["no_such_session", "engine_mismatch", "invalid_request"]);
|
|
18
|
+
/** A WebSocket as the client's message transport. */
|
|
19
|
+
class SocketTransport {
|
|
20
|
+
socket;
|
|
21
|
+
onMessage;
|
|
22
|
+
onClose;
|
|
23
|
+
constructor(socket) {
|
|
24
|
+
this.socket = socket;
|
|
25
|
+
socket.onmessage = (e) => this.onMessage?.(new Uint8Array(e.data));
|
|
26
|
+
socket.onclose = () => this.onClose?.();
|
|
27
|
+
}
|
|
28
|
+
static connect(url) {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
const socket = new WebSocket(url);
|
|
31
|
+
socket.binaryType = "arraybuffer";
|
|
32
|
+
socket.onopen = () => resolve(new SocketTransport(socket));
|
|
33
|
+
socket.onerror = () => reject(new Error(`cannot connect to ${url}`));
|
|
34
|
+
socket.onclose = () => reject(new Error(`cannot connect to ${url}`));
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
send(bytes) {
|
|
38
|
+
this.socket.send(bytes);
|
|
39
|
+
}
|
|
40
|
+
close() {
|
|
41
|
+
this.socket.close();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
class Client {
|
|
45
|
+
post;
|
|
46
|
+
constructor(post) {
|
|
47
|
+
this.post = post;
|
|
48
|
+
}
|
|
49
|
+
engine;
|
|
50
|
+
render;
|
|
51
|
+
keys;
|
|
52
|
+
url = "";
|
|
53
|
+
viewport;
|
|
54
|
+
profile;
|
|
55
|
+
conn;
|
|
56
|
+
attachment;
|
|
57
|
+
channel = 0;
|
|
58
|
+
/** The session attached or being attached. */
|
|
59
|
+
session;
|
|
60
|
+
sessions = new Map();
|
|
61
|
+
/** Input in order: a paste goes as `write` requests, and keys typed
|
|
62
|
+
* meanwhile wait behind it. `attach` starts a fresh chain; input queued
|
|
63
|
+
* for an attachment that is gone is dropped, but a paste under way
|
|
64
|
+
* finishes (cut short, it would leave the program inside a bracketed
|
|
65
|
+
* paste). */
|
|
66
|
+
writes = Promise.resolve();
|
|
67
|
+
clipboard = new Osc52Scanner();
|
|
68
|
+
paintTimer;
|
|
69
|
+
/** Shared by reconnects and re-attaches after a failure; reset by a screen. */
|
|
70
|
+
backoff = BACKOFF_MIN_MS;
|
|
71
|
+
reopenTimer;
|
|
72
|
+
/** Connection attempts since a server last answered. */
|
|
73
|
+
attempt = 0;
|
|
74
|
+
/** The connection ended for good with the server's refusal or error;
|
|
75
|
+
* `attach` connects again. */
|
|
76
|
+
refused = false;
|
|
77
|
+
link(link) {
|
|
78
|
+
this.post({ type: "link", link });
|
|
79
|
+
}
|
|
80
|
+
/** The link ended with `error`: the server's, else the client's own. */
|
|
81
|
+
endedWith(err, retry) {
|
|
82
|
+
const server = err instanceof ClientError ? err.server : undefined;
|
|
83
|
+
this.link(server
|
|
84
|
+
? { state: "ended", reason: linkEnd(server.code), code: server.code, message: server.message, retry }
|
|
85
|
+
: { state: "ended", reason: "error", message: err instanceof Error ? err.message : String(err), retry });
|
|
86
|
+
}
|
|
87
|
+
delay() {
|
|
88
|
+
const delay = this.backoff;
|
|
89
|
+
this.backoff = Math.min(this.backoff * 2, BACKOFF_MAX_MS);
|
|
90
|
+
return delay;
|
|
91
|
+
}
|
|
92
|
+
async init(message) {
|
|
93
|
+
this.url = message.url;
|
|
94
|
+
this.viewport = message.viewport;
|
|
95
|
+
this.profile = message.profile;
|
|
96
|
+
this.session = message.session;
|
|
97
|
+
this.post({ type: "status", text: "loading engine" });
|
|
98
|
+
try {
|
|
99
|
+
const wasm = await (await fetch(message.wasm)).arrayBuffer();
|
|
100
|
+
this.engine = await Engine.load(wasm);
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
this.post({ type: "status", text: `cannot load the engine: ${err}` });
|
|
104
|
+
this.endedWith(err, false);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
this.render = new RenderState(this.engine);
|
|
108
|
+
this.keys = new KeyEncoder(this.engine);
|
|
109
|
+
await this.connect();
|
|
110
|
+
}
|
|
111
|
+
async connect() {
|
|
112
|
+
this.post({ type: "status", text: this.attachment ? "reconnecting" : "connecting" });
|
|
113
|
+
this.link({ state: "connecting", attempt: ++this.attempt });
|
|
114
|
+
let conn;
|
|
115
|
+
try {
|
|
116
|
+
conn = await Connection.connect(await SocketTransport.connect(this.url), hello("clankermux-web", ENGINE));
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
// A server that answers and refuses is not retried; a lost one is.
|
|
120
|
+
if (err instanceof ClientError && (err.kind === "server" || err.kind === "protocol")) {
|
|
121
|
+
this.post({ type: "status", text: err.message });
|
|
122
|
+
this.refused = true;
|
|
123
|
+
this.endedWith(err, false);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
setTimeout(() => void this.connect(), this.delay());
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
this.attempt = 0;
|
|
130
|
+
this.conn = conn;
|
|
131
|
+
conn.onEnd = (err) => {
|
|
132
|
+
if (this.conn !== conn)
|
|
133
|
+
return;
|
|
134
|
+
this.conn = undefined;
|
|
135
|
+
if (err.kind === "server") {
|
|
136
|
+
this.post({ type: "status", text: err.message });
|
|
137
|
+
this.refused = true;
|
|
138
|
+
this.endedWith(err, false);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
this.link({ state: "ended", reason: "closed", retry: true });
|
|
142
|
+
setTimeout(() => void this.connect(), this.delay());
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
conn.onEvent = (event) => this.event(event);
|
|
146
|
+
try {
|
|
147
|
+
// Events arriving between pages are applied to the same map: a
|
|
148
|
+
// later page cannot hold a session removed before it was produced.
|
|
149
|
+
this.sessions.clear();
|
|
150
|
+
let page = await conn.call("subscribe", {});
|
|
151
|
+
for (;;) {
|
|
152
|
+
for (const s of page.sessions)
|
|
153
|
+
this.sessions.set(s.id, s);
|
|
154
|
+
if (page.next === undefined)
|
|
155
|
+
break;
|
|
156
|
+
page = await conn.call("list", { after: page.next });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
this.post({ type: "status", text: String(err) });
|
|
161
|
+
// A lost connection is reported through onEnd, which ran first; a
|
|
162
|
+
// refused subscription ends this one for good.
|
|
163
|
+
if (this.conn === conn) {
|
|
164
|
+
this.conn = undefined;
|
|
165
|
+
conn.close();
|
|
166
|
+
this.refused = true;
|
|
167
|
+
this.endedWith(err, false);
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
this.postSessions();
|
|
172
|
+
if (this.session !== undefined) {
|
|
173
|
+
this.open();
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
this.post({ type: "status", text: "no session" });
|
|
177
|
+
this.link({ state: "connected", session: undefined });
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
event(event) {
|
|
181
|
+
switch (event.event) {
|
|
182
|
+
case "session_created":
|
|
183
|
+
case "session_changed":
|
|
184
|
+
case "session_exited":
|
|
185
|
+
this.sessions.set(event.session.id, event.session);
|
|
186
|
+
break;
|
|
187
|
+
case "session_removed":
|
|
188
|
+
this.sessions.delete(event.id);
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
this.postSessions();
|
|
192
|
+
}
|
|
193
|
+
postSessions() {
|
|
194
|
+
this.post({ type: "sessions", sessions: [...this.sessions.values()], current: this.session });
|
|
195
|
+
}
|
|
196
|
+
/** Opens a channel for `this.session`, resuming when the attachment can. */
|
|
197
|
+
open() {
|
|
198
|
+
const conn = this.conn;
|
|
199
|
+
if (!conn || this.session === undefined)
|
|
200
|
+
return;
|
|
201
|
+
if (!this.attachment)
|
|
202
|
+
this.attachment = new Attachment(this.engine);
|
|
203
|
+
const attachment = this.attachment;
|
|
204
|
+
attachment.openOn(conn.welcome);
|
|
205
|
+
const channel = ++this.channel;
|
|
206
|
+
const open = {
|
|
207
|
+
session: this.session,
|
|
208
|
+
viewport: this.viewport,
|
|
209
|
+
profile: this.profile,
|
|
210
|
+
resume: attachment.resume(),
|
|
211
|
+
};
|
|
212
|
+
try {
|
|
213
|
+
conn.open(channel, open, (frame) => {
|
|
214
|
+
if (this.attachment !== attachment)
|
|
215
|
+
return;
|
|
216
|
+
let actions;
|
|
217
|
+
try {
|
|
218
|
+
actions = attachment.handle(frame);
|
|
219
|
+
}
|
|
220
|
+
catch (err) {
|
|
221
|
+
// A lost place in the stream: CLOSE and OPEN again, afresh. Any
|
|
222
|
+
// other fault would only repeat.
|
|
223
|
+
this.post({ type: "status", text: String(err) });
|
|
224
|
+
const desync = err instanceof ClientError && err.kind === "desync";
|
|
225
|
+
this.endedWith(err, desync);
|
|
226
|
+
if (desync)
|
|
227
|
+
this.reopen();
|
|
228
|
+
else
|
|
229
|
+
this.giveUp();
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if (frame.header.type === FrameType.Attached && actions.length === 0) {
|
|
233
|
+
this.post({ type: "status", text: "attached" });
|
|
234
|
+
// Resumed: the terminal shows the stream already, and an exit
|
|
235
|
+
// still to come arrives as EXITED. A snapshot's screen comes
|
|
236
|
+
// later, as an action.
|
|
237
|
+
if (attachment.resume())
|
|
238
|
+
this.link({ state: "connected", session: attachment.session });
|
|
239
|
+
}
|
|
240
|
+
for (const action of actions)
|
|
241
|
+
this.apply(action);
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
catch (err) {
|
|
245
|
+
this.post({ type: "status", text: String(err) });
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/** The screen of `session` is shown: `connected`, and at once `ended
|
|
249
|
+
* exited` when its program has exited already. */
|
|
250
|
+
attached(session) {
|
|
251
|
+
this.link({ state: "connected", session });
|
|
252
|
+
if (session?.status === "exited") {
|
|
253
|
+
this.link({ state: "ended", reason: "exited", ...(session.exit && { exit: session.exit }), retry: false });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/** Detaches for good: a reconnect does not try the session again. */
|
|
257
|
+
giveUp() {
|
|
258
|
+
this.detach();
|
|
259
|
+
this.session = undefined;
|
|
260
|
+
this.postSessions();
|
|
261
|
+
}
|
|
262
|
+
/** Detaches and attaches the same session again after a backoff. */
|
|
263
|
+
reopen() {
|
|
264
|
+
this.detach();
|
|
265
|
+
this.reopenTimer ??= setTimeout(() => {
|
|
266
|
+
this.reopenTimer = undefined;
|
|
267
|
+
if (this.session !== undefined && !this.attachment)
|
|
268
|
+
this.open();
|
|
269
|
+
}, this.delay());
|
|
270
|
+
}
|
|
271
|
+
apply(action) {
|
|
272
|
+
switch (action.type) {
|
|
273
|
+
case "screen": {
|
|
274
|
+
this.backoff = BACKOFF_MIN_MS;
|
|
275
|
+
this.clipboard.reset();
|
|
276
|
+
this.applyProfile(this.attachment?.term());
|
|
277
|
+
this.paint();
|
|
278
|
+
const session = this.attachment?.session;
|
|
279
|
+
const text = session?.status === "restored"
|
|
280
|
+
? "restored from a previous server; no process"
|
|
281
|
+
: session?.exit
|
|
282
|
+
? `exited: ${describe(session.exit)}`
|
|
283
|
+
: "attached";
|
|
284
|
+
this.post({ type: "status", text });
|
|
285
|
+
this.attached(session);
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
case "output":
|
|
289
|
+
for (const write of this.clipboard.feed(action.bytes)) {
|
|
290
|
+
// Only the clipboard selection; primary and secondary have no
|
|
291
|
+
// browser equivalent.
|
|
292
|
+
if (write.selection === "" || write.selection.includes("c"))
|
|
293
|
+
this.post({ type: "clipboard", text: write.text });
|
|
294
|
+
}
|
|
295
|
+
this.paintTimer ??= setTimeout(() => this.paint(), PAINT_MS);
|
|
296
|
+
break;
|
|
297
|
+
case "resized":
|
|
298
|
+
this.paint();
|
|
299
|
+
break;
|
|
300
|
+
case "exited":
|
|
301
|
+
this.paint();
|
|
302
|
+
this.post({ type: "status", text: `exited: ${describe(action.exit)}` });
|
|
303
|
+
this.link({ state: "ended", reason: "exited", exit: action.exit, retry: false });
|
|
304
|
+
break;
|
|
305
|
+
case "gap":
|
|
306
|
+
this.clipboard.reset();
|
|
307
|
+
this.post({ type: "status", text: "resynchronising" });
|
|
308
|
+
break;
|
|
309
|
+
case "closed":
|
|
310
|
+
this.post({ type: "status", text: `closed: ${action.reason}` });
|
|
311
|
+
// `stopping` is followed by the connection's close, and the
|
|
312
|
+
// reconnect attaches again; a removed session is gone for good.
|
|
313
|
+
this.link({ state: "ended", reason: action.reason, retry: action.reason === "stopping" });
|
|
314
|
+
if (action.reason === "removed")
|
|
315
|
+
this.giveUp();
|
|
316
|
+
break;
|
|
317
|
+
case "error": {
|
|
318
|
+
this.post({ type: "status", text: `${action.error.code}: ${action.error.message}` });
|
|
319
|
+
// An error ended the attachment: attach again after a backoff
|
|
320
|
+
// (`too_slow`, a snapshot that could not be taken), unless a new
|
|
321
|
+
// OPEN would fail the same way.
|
|
322
|
+
if (!this.attachment?.closed())
|
|
323
|
+
break;
|
|
324
|
+
const { code, message } = action.error;
|
|
325
|
+
const retry = !PERMANENT.has(code);
|
|
326
|
+
this.link({ state: "ended", reason: linkEnd(code), code, message, retry });
|
|
327
|
+
if (retry)
|
|
328
|
+
this.reopen();
|
|
329
|
+
else
|
|
330
|
+
this.giveUp();
|
|
331
|
+
break;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
/** The page's colours on the local terminal, as the server has them on
|
|
336
|
+
* its own: the stream does not carry default colours. */
|
|
337
|
+
applyProfile(term) {
|
|
338
|
+
if (!term)
|
|
339
|
+
return;
|
|
340
|
+
for (const [kind, value] of [
|
|
341
|
+
["foreground", this.profile.fg],
|
|
342
|
+
["background", this.profile.bg],
|
|
343
|
+
["cursor", this.profile.cursor],
|
|
344
|
+
]) {
|
|
345
|
+
if (value !== undefined)
|
|
346
|
+
term.setColor(kind, parseInt(value.slice(1), 16));
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
repaint() {
|
|
350
|
+
const term = this.attachment?.term();
|
|
351
|
+
if (!term)
|
|
352
|
+
return;
|
|
353
|
+
// Switching the render state to a fresh terminal makes the next
|
|
354
|
+
// update full; so does a resize. A full repaint of the same terminal
|
|
355
|
+
// comes from a clean render state.
|
|
356
|
+
this.render.free();
|
|
357
|
+
this.render = new RenderState(this.engine);
|
|
358
|
+
this.paint();
|
|
359
|
+
}
|
|
360
|
+
paint() {
|
|
361
|
+
if (this.paintTimer !== undefined) {
|
|
362
|
+
clearTimeout(this.paintTimer);
|
|
363
|
+
this.paintTimer = undefined;
|
|
364
|
+
}
|
|
365
|
+
const term = this.attachment?.term();
|
|
366
|
+
if (!term)
|
|
367
|
+
return;
|
|
368
|
+
this.post({ type: "frame", frame: this.render.update(term) });
|
|
369
|
+
this.render.clean();
|
|
370
|
+
}
|
|
371
|
+
/** Closes the current attachment's channel and forgets it. An
|
|
372
|
+
* attachment the server ended needs no CLOSE; one whose stream ended
|
|
373
|
+
* (EXITED) does. */
|
|
374
|
+
detach() {
|
|
375
|
+
if (!this.attachment)
|
|
376
|
+
return;
|
|
377
|
+
if (this.conn) {
|
|
378
|
+
this.conn.release(this.channel);
|
|
379
|
+
if (!this.attachment.closed()) {
|
|
380
|
+
try {
|
|
381
|
+
this.conn.send(FrameType.Close, this.channel, new Uint8Array());
|
|
382
|
+
}
|
|
383
|
+
catch {
|
|
384
|
+
// The connection is gone; nothing to close.
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
this.attachment.free();
|
|
389
|
+
this.attachment = undefined;
|
|
390
|
+
}
|
|
391
|
+
/** Attaches `session`; after the server refused or ended the
|
|
392
|
+
* connection, connects again first. */
|
|
393
|
+
attach(session) {
|
|
394
|
+
this.detach();
|
|
395
|
+
this.writes = Promise.resolve();
|
|
396
|
+
this.session = session;
|
|
397
|
+
this.postSessions();
|
|
398
|
+
if (this.refused) {
|
|
399
|
+
this.refused = false;
|
|
400
|
+
this.attempt = 0;
|
|
401
|
+
void this.connect();
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
this.open();
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
/** Creates a session at the current size and attaches it; `on_exit`
|
|
408
|
+
* only when given, so the server's default holds otherwise. */
|
|
409
|
+
async create(id, onExit) {
|
|
410
|
+
const conn = this.conn;
|
|
411
|
+
if (!conn)
|
|
412
|
+
return this.fail(id, new ClientError("closed", "not connected"));
|
|
413
|
+
const params = { cols: this.viewport.cols, rows: this.viewport.rows };
|
|
414
|
+
if (onExit !== undefined)
|
|
415
|
+
params.on_exit = onExit;
|
|
416
|
+
let session;
|
|
417
|
+
try {
|
|
418
|
+
session = await conn.call("create", params);
|
|
419
|
+
}
|
|
420
|
+
catch (err) {
|
|
421
|
+
this.post({ type: "status", text: String(err) });
|
|
422
|
+
return this.fail(id, err);
|
|
423
|
+
}
|
|
424
|
+
this.attach(session.id);
|
|
425
|
+
this.post({ type: "done", id, session });
|
|
426
|
+
}
|
|
427
|
+
/** `end` (SIGHUP) or `remove` for the session attached or being
|
|
428
|
+
* attached. `not_running` from `end` is success: there is nothing left
|
|
429
|
+
* to end. */
|
|
430
|
+
async request(id, method) {
|
|
431
|
+
const conn = this.conn;
|
|
432
|
+
const session = this.session;
|
|
433
|
+
if (!conn)
|
|
434
|
+
return this.fail(id, new ClientError("closed", "not connected"));
|
|
435
|
+
if (session === undefined)
|
|
436
|
+
return this.fail(id, new Error("no session is attached"));
|
|
437
|
+
try {
|
|
438
|
+
await conn.call(method, { session });
|
|
439
|
+
}
|
|
440
|
+
catch (err) {
|
|
441
|
+
if (!(method === "end" && err instanceof ClientError && err.server?.code === "not_running")) {
|
|
442
|
+
return this.fail(id, err);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
this.post({ type: "done", id });
|
|
446
|
+
}
|
|
447
|
+
fail(id, err) {
|
|
448
|
+
const error = err instanceof ClientError
|
|
449
|
+
? { kind: err.kind, message: err.message, ...(err.server && { server: err.server }) }
|
|
450
|
+
: { message: err instanceof Error ? err.message : String(err) };
|
|
451
|
+
this.post({ type: "done", id, error });
|
|
452
|
+
}
|
|
453
|
+
/** Input for the session, dropped unless attached on a live connection.
|
|
454
|
+
* Keys go as INPUT; a paste goes as `write` requests, each answered
|
|
455
|
+
* once the PTY took it, since INPUT frames are not acknowledged and a
|
|
456
|
+
* paste would overflow the input queue. This tab's input is sent in
|
|
457
|
+
* order, so none of it lands inside a paste. */
|
|
458
|
+
input(bytes, paste) {
|
|
459
|
+
const conn = this.conn;
|
|
460
|
+
const attachment = this.attachment;
|
|
461
|
+
const session = this.session;
|
|
462
|
+
const channel = this.channel;
|
|
463
|
+
if (bytes.length === 0 || !conn || !attachment || attachment.ended() || session === undefined)
|
|
464
|
+
return;
|
|
465
|
+
// Each request's base64 fits the control body with room for the rest,
|
|
466
|
+
// and its bytes fit the input queue (at least one byte, so the loop
|
|
467
|
+
// moves on an absurd limit).
|
|
468
|
+
const { control_body, input_queue } = conn.welcome.limits;
|
|
469
|
+
const chunk = Math.max(1, Math.min(Math.floor((control_body - 1024) / 4) * 3, input_queue));
|
|
470
|
+
this.writes = this.writes.then(async () => {
|
|
471
|
+
if (this.attachment !== attachment)
|
|
472
|
+
return;
|
|
473
|
+
try {
|
|
474
|
+
if (!paste) {
|
|
475
|
+
conn.send(FrameType.Input, channel, bytes);
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
for (let at = 0; at < bytes.length; at += chunk) {
|
|
479
|
+
await conn.call("write", { session, data: base64(bytes.subarray(at, at + chunk)) });
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
catch (err) {
|
|
483
|
+
// A connection loss is reported through onEnd; a refused chunk
|
|
484
|
+
// ends the paste there.
|
|
485
|
+
if (conn === this.conn)
|
|
486
|
+
this.post({ type: "status", text: String(err) });
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
key(input) {
|
|
491
|
+
const term = this.attachment?.term();
|
|
492
|
+
if (!term)
|
|
493
|
+
return;
|
|
494
|
+
this.input(this.keys.encode(term, input), false);
|
|
495
|
+
}
|
|
496
|
+
paste(text) {
|
|
497
|
+
const term = this.attachment?.term();
|
|
498
|
+
if (!term)
|
|
499
|
+
return;
|
|
500
|
+
this.input(this.engine.encodePaste(text, term.mode(2004)), true);
|
|
501
|
+
}
|
|
502
|
+
setViewport(size, profile) {
|
|
503
|
+
this.viewport = size;
|
|
504
|
+
this.profile = profile;
|
|
505
|
+
const attachment = this.attachment;
|
|
506
|
+
if (!this.conn || !attachment || attachment.ended())
|
|
507
|
+
return;
|
|
508
|
+
this.applyProfile(attachment.term());
|
|
509
|
+
this.paint();
|
|
510
|
+
try {
|
|
511
|
+
this.conn.sendJson(FrameType.Viewport, this.channel, { ...size, profile });
|
|
512
|
+
}
|
|
513
|
+
catch {
|
|
514
|
+
// Reported through onEnd.
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
/** The reason an error code ends the link with. */
|
|
519
|
+
function linkEnd(code) {
|
|
520
|
+
return code === "no_such_session" || code === "engine_mismatch" ? code : "error";
|
|
521
|
+
}
|
|
522
|
+
function describe(exit) {
|
|
523
|
+
return "code" in exit ? `code ${exit.code}` : `signal ${exit.signal}`;
|
|
524
|
+
}
|
|
525
|
+
/** Serves a page's `mountTerminal` on `port`: the worker module the page
|
|
526
|
+
* starts calls `runWorker(self)`. */
|
|
527
|
+
export function runWorker(port) {
|
|
528
|
+
const client = new Client((message) => port.postMessage(message));
|
|
529
|
+
port.onmessage = (e) => {
|
|
530
|
+
const message = e.data;
|
|
531
|
+
switch (message.type) {
|
|
532
|
+
case "init":
|
|
533
|
+
void client.init(message);
|
|
534
|
+
break;
|
|
535
|
+
case "attach":
|
|
536
|
+
client.attach(message.session);
|
|
537
|
+
break;
|
|
538
|
+
case "create":
|
|
539
|
+
void client.create(message.id, message.onExit);
|
|
540
|
+
break;
|
|
541
|
+
case "end":
|
|
542
|
+
case "remove":
|
|
543
|
+
void client.request(message.id, message.type);
|
|
544
|
+
break;
|
|
545
|
+
case "key":
|
|
546
|
+
client.key(message.input);
|
|
547
|
+
break;
|
|
548
|
+
case "paste":
|
|
549
|
+
client.paste(message.text);
|
|
550
|
+
break;
|
|
551
|
+
case "viewport":
|
|
552
|
+
client.setViewport(message.size, message.profile);
|
|
553
|
+
break;
|
|
554
|
+
case "repaint":
|
|
555
|
+
client.repaint();
|
|
556
|
+
break;
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
function base64(bytes) {
|
|
561
|
+
let binary = "";
|
|
562
|
+
for (let at = 0; at < bytes.length; at += 0x8000) {
|
|
563
|
+
binary += String.fromCharCode(...bytes.subarray(at, at + 0x8000));
|
|
564
|
+
}
|
|
565
|
+
return btoa(binary);
|
|
566
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type Engine, Terminal } from "./index.ts";
|
|
2
|
+
/** Position just past the `READY` record, once every byte through it is in `buf`. */
|
|
3
|
+
export declare function readyEnd(buf: Uint8Array, len?: number): number | undefined;
|
|
4
|
+
/** Position just past the next `PAGE` or `FINISH` record at or after `pos`. */
|
|
5
|
+
export declare function stepEnd(buf: Uint8Array, pos: number, len?: number): number | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* An incremental snapshot decoder over a buffer of known capacity, filled
|
|
8
|
+
* as bytes arrive. The library reads the buffer lazily, so `ready` and
|
|
9
|
+
* `nextPage` are called only once the bytes they consume are in place.
|
|
10
|
+
*
|
|
11
|
+
* This leans on the engine's behaviour, not its contract: the header asks
|
|
12
|
+
* for an immutable buffer. A `GhosttyReader` callback from wasm would
|
|
13
|
+
* need a JS function placed in the module's function table, which takes
|
|
14
|
+
* a generated wasm wrapper per signature; filling the buffer behind the
|
|
15
|
+
* decoder is less machinery. The engine is pinned; its decoder test
|
|
16
|
+
* pushes the bytes piecewise, so an engine that validated ahead would
|
|
17
|
+
* fail there before shipping.
|
|
18
|
+
*/
|
|
19
|
+
export declare class Decoder {
|
|
20
|
+
readonly engine: Engine;
|
|
21
|
+
readonly capacity: number;
|
|
22
|
+
private readonly buffer;
|
|
23
|
+
private decoder;
|
|
24
|
+
private filled;
|
|
25
|
+
private consumed;
|
|
26
|
+
private term;
|
|
27
|
+
private complete;
|
|
28
|
+
constructor(engine: Engine, capacity: number);
|
|
29
|
+
setMaxContinuationBytes(max: number): void;
|
|
30
|
+
push(bytes: Uint8Array): void;
|
|
31
|
+
private get source();
|
|
32
|
+
/** Whether every byte through `READY` is buffered. */
|
|
33
|
+
readyAvailable(): boolean;
|
|
34
|
+
stepAvailable(): boolean;
|
|
35
|
+
/** Decodes through `READY`; the terminal is then renderable. */
|
|
36
|
+
ready(): Terminal;
|
|
37
|
+
/** Decodes one history page. False once `FINISH` is validated. */
|
|
38
|
+
nextPage(): boolean;
|
|
39
|
+
terminal(): Terminal | undefined;
|
|
40
|
+
/** Whether `FINISH` was validated: the snapshot is complete. */
|
|
41
|
+
finished(): boolean;
|
|
42
|
+
/** Frees the decoder and its buffer; the terminal, finished or not, is the caller's. */
|
|
43
|
+
release(): Terminal | undefined;
|
|
44
|
+
free(): void;
|
|
45
|
+
}
|