@gjermundgaraba/clankermux 0.0.0-stage → 0.1.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/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 +9 -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 +46 -0
- package/dist/client/mount.js +139 -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 +59 -0
- package/dist/client/worker.js +449 -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
- package/README.md +0 -3
|
@@ -0,0 +1,449 @@
|
|
|
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
|
+
delay() {
|
|
73
|
+
const delay = this.backoff;
|
|
74
|
+
this.backoff = Math.min(this.backoff * 2, BACKOFF_MAX_MS);
|
|
75
|
+
return delay;
|
|
76
|
+
}
|
|
77
|
+
async init(message) {
|
|
78
|
+
this.url = message.url;
|
|
79
|
+
this.viewport = message.viewport;
|
|
80
|
+
this.profile = message.profile;
|
|
81
|
+
this.session = message.session;
|
|
82
|
+
this.post({ type: "status", text: "loading engine" });
|
|
83
|
+
const wasm = await (await fetch(message.wasm)).arrayBuffer();
|
|
84
|
+
this.engine = await Engine.load(wasm);
|
|
85
|
+
this.render = new RenderState(this.engine);
|
|
86
|
+
this.keys = new KeyEncoder(this.engine);
|
|
87
|
+
await this.connect();
|
|
88
|
+
}
|
|
89
|
+
async connect() {
|
|
90
|
+
this.post({ type: "status", text: this.attachment ? "reconnecting" : "connecting" });
|
|
91
|
+
let conn;
|
|
92
|
+
try {
|
|
93
|
+
conn = await Connection.connect(await SocketTransport.connect(this.url), hello("clankermux-web", ENGINE));
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
// A server that answers and refuses is not retried; a lost one is.
|
|
97
|
+
if (err instanceof ClientError && (err.kind === "server" || err.kind === "protocol")) {
|
|
98
|
+
this.post({ type: "status", text: err.message });
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
setTimeout(() => void this.connect(), this.delay());
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
this.conn = conn;
|
|
105
|
+
conn.onEnd = (err) => {
|
|
106
|
+
if (this.conn !== conn)
|
|
107
|
+
return;
|
|
108
|
+
this.conn = undefined;
|
|
109
|
+
if (err.kind === "server") {
|
|
110
|
+
this.post({ type: "status", text: err.message });
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
setTimeout(() => void this.connect(), this.delay());
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
conn.onEvent = (event) => this.event(event);
|
|
117
|
+
try {
|
|
118
|
+
// Events arriving between pages are applied to the same map: a
|
|
119
|
+
// later page cannot hold a session removed before it was produced.
|
|
120
|
+
this.sessions.clear();
|
|
121
|
+
let page = await conn.call("subscribe", {});
|
|
122
|
+
for (;;) {
|
|
123
|
+
for (const s of page.sessions)
|
|
124
|
+
this.sessions.set(s.id, s);
|
|
125
|
+
if (page.next === undefined)
|
|
126
|
+
break;
|
|
127
|
+
page = await conn.call("list", { after: page.next });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
this.post({ type: "status", text: String(err) });
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
this.postSessions();
|
|
135
|
+
if (this.session !== undefined)
|
|
136
|
+
this.open();
|
|
137
|
+
else
|
|
138
|
+
this.post({ type: "status", text: "no session" });
|
|
139
|
+
}
|
|
140
|
+
event(event) {
|
|
141
|
+
switch (event.event) {
|
|
142
|
+
case "session_created":
|
|
143
|
+
case "session_changed":
|
|
144
|
+
case "session_exited":
|
|
145
|
+
this.sessions.set(event.session.id, event.session);
|
|
146
|
+
break;
|
|
147
|
+
case "session_removed":
|
|
148
|
+
this.sessions.delete(event.id);
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
this.postSessions();
|
|
152
|
+
}
|
|
153
|
+
postSessions() {
|
|
154
|
+
this.post({ type: "sessions", sessions: [...this.sessions.values()], current: this.session });
|
|
155
|
+
}
|
|
156
|
+
/** Opens a channel for `this.session`, resuming when the attachment can. */
|
|
157
|
+
open() {
|
|
158
|
+
const conn = this.conn;
|
|
159
|
+
if (!conn || this.session === undefined)
|
|
160
|
+
return;
|
|
161
|
+
if (!this.attachment)
|
|
162
|
+
this.attachment = new Attachment(this.engine);
|
|
163
|
+
const attachment = this.attachment;
|
|
164
|
+
attachment.openOn(conn.welcome);
|
|
165
|
+
const channel = ++this.channel;
|
|
166
|
+
const open = {
|
|
167
|
+
session: this.session,
|
|
168
|
+
viewport: this.viewport,
|
|
169
|
+
profile: this.profile,
|
|
170
|
+
resume: attachment.resume(),
|
|
171
|
+
};
|
|
172
|
+
try {
|
|
173
|
+
conn.open(channel, open, (frame) => {
|
|
174
|
+
if (this.attachment !== attachment)
|
|
175
|
+
return;
|
|
176
|
+
let actions;
|
|
177
|
+
try {
|
|
178
|
+
actions = attachment.handle(frame);
|
|
179
|
+
}
|
|
180
|
+
catch (err) {
|
|
181
|
+
// A lost place in the stream: CLOSE and OPEN again, afresh. Any
|
|
182
|
+
// other fault would only repeat.
|
|
183
|
+
this.post({ type: "status", text: String(err) });
|
|
184
|
+
if (err instanceof ClientError && err.kind === "desync")
|
|
185
|
+
this.reopen();
|
|
186
|
+
else
|
|
187
|
+
this.giveUp();
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (frame.header.type === FrameType.Attached && actions.length === 0) {
|
|
191
|
+
this.post({ type: "status", text: "attached" });
|
|
192
|
+
}
|
|
193
|
+
for (const action of actions)
|
|
194
|
+
this.apply(action);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
this.post({ type: "status", text: String(err) });
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/** Detaches for good: a reconnect does not try the session again. */
|
|
202
|
+
giveUp() {
|
|
203
|
+
this.detach();
|
|
204
|
+
this.session = undefined;
|
|
205
|
+
this.postSessions();
|
|
206
|
+
}
|
|
207
|
+
/** Detaches and attaches the same session again after a backoff. */
|
|
208
|
+
reopen() {
|
|
209
|
+
this.detach();
|
|
210
|
+
this.reopenTimer ??= setTimeout(() => {
|
|
211
|
+
this.reopenTimer = undefined;
|
|
212
|
+
if (this.session !== undefined && !this.attachment)
|
|
213
|
+
this.open();
|
|
214
|
+
}, this.delay());
|
|
215
|
+
}
|
|
216
|
+
apply(action) {
|
|
217
|
+
switch (action.type) {
|
|
218
|
+
case "screen": {
|
|
219
|
+
this.backoff = BACKOFF_MIN_MS;
|
|
220
|
+
this.clipboard.reset();
|
|
221
|
+
this.applyProfile(this.attachment?.term());
|
|
222
|
+
this.paint();
|
|
223
|
+
const session = this.attachment?.session;
|
|
224
|
+
const text = session?.status === "restored"
|
|
225
|
+
? "restored from a previous server; no process"
|
|
226
|
+
: session?.exit
|
|
227
|
+
? `exited: ${describe(session.exit)}`
|
|
228
|
+
: "attached";
|
|
229
|
+
this.post({ type: "status", text });
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
case "output":
|
|
233
|
+
for (const write of this.clipboard.feed(action.bytes)) {
|
|
234
|
+
// Only the clipboard selection; primary and secondary have no
|
|
235
|
+
// browser equivalent.
|
|
236
|
+
if (write.selection === "" || write.selection.includes("c"))
|
|
237
|
+
this.post({ type: "clipboard", text: write.text });
|
|
238
|
+
}
|
|
239
|
+
this.paintTimer ??= setTimeout(() => this.paint(), PAINT_MS);
|
|
240
|
+
break;
|
|
241
|
+
case "resized":
|
|
242
|
+
this.paint();
|
|
243
|
+
break;
|
|
244
|
+
case "exited":
|
|
245
|
+
this.paint();
|
|
246
|
+
this.post({ type: "status", text: `exited: ${describe(action.exit)}` });
|
|
247
|
+
break;
|
|
248
|
+
case "gap":
|
|
249
|
+
this.clipboard.reset();
|
|
250
|
+
this.post({ type: "status", text: "resynchronising" });
|
|
251
|
+
break;
|
|
252
|
+
case "closed":
|
|
253
|
+
this.post({ type: "status", text: `closed: ${action.reason}` });
|
|
254
|
+
break;
|
|
255
|
+
case "error":
|
|
256
|
+
this.post({ type: "status", text: `${action.error.code}: ${action.error.message}` });
|
|
257
|
+
// An error ended the attachment: attach again after a backoff
|
|
258
|
+
// (`too_slow`, a snapshot that could not be taken), unless a new
|
|
259
|
+
// OPEN would fail the same way.
|
|
260
|
+
if (this.attachment?.closed()) {
|
|
261
|
+
if (PERMANENT.has(action.error.code))
|
|
262
|
+
this.giveUp();
|
|
263
|
+
else
|
|
264
|
+
this.reopen();
|
|
265
|
+
}
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/** The page's colours on the local terminal, as the server has them on
|
|
270
|
+
* its own: the stream does not carry default colours. */
|
|
271
|
+
applyProfile(term) {
|
|
272
|
+
if (!term)
|
|
273
|
+
return;
|
|
274
|
+
for (const [kind, value] of [
|
|
275
|
+
["foreground", this.profile.fg],
|
|
276
|
+
["background", this.profile.bg],
|
|
277
|
+
["cursor", this.profile.cursor],
|
|
278
|
+
]) {
|
|
279
|
+
if (value !== undefined)
|
|
280
|
+
term.setColor(kind, parseInt(value.slice(1), 16));
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
repaint() {
|
|
284
|
+
const term = this.attachment?.term();
|
|
285
|
+
if (!term)
|
|
286
|
+
return;
|
|
287
|
+
// Switching the render state to a fresh terminal makes the next
|
|
288
|
+
// update full; so does a resize. A full repaint of the same terminal
|
|
289
|
+
// comes from a clean render state.
|
|
290
|
+
this.render.free();
|
|
291
|
+
this.render = new RenderState(this.engine);
|
|
292
|
+
this.paint();
|
|
293
|
+
}
|
|
294
|
+
paint() {
|
|
295
|
+
if (this.paintTimer !== undefined) {
|
|
296
|
+
clearTimeout(this.paintTimer);
|
|
297
|
+
this.paintTimer = undefined;
|
|
298
|
+
}
|
|
299
|
+
const term = this.attachment?.term();
|
|
300
|
+
if (!term)
|
|
301
|
+
return;
|
|
302
|
+
this.post({ type: "frame", frame: this.render.update(term) });
|
|
303
|
+
this.render.clean();
|
|
304
|
+
}
|
|
305
|
+
/** Closes the current attachment's channel and forgets it. An
|
|
306
|
+
* attachment the server ended needs no CLOSE; one whose stream ended
|
|
307
|
+
* (EXITED) does. */
|
|
308
|
+
detach() {
|
|
309
|
+
if (!this.attachment)
|
|
310
|
+
return;
|
|
311
|
+
if (this.conn) {
|
|
312
|
+
this.conn.release(this.channel);
|
|
313
|
+
if (!this.attachment.closed()) {
|
|
314
|
+
try {
|
|
315
|
+
this.conn.send(FrameType.Close, this.channel, new Uint8Array());
|
|
316
|
+
}
|
|
317
|
+
catch {
|
|
318
|
+
// The connection is gone; nothing to close.
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
this.attachment.free();
|
|
323
|
+
this.attachment = undefined;
|
|
324
|
+
}
|
|
325
|
+
attach(session) {
|
|
326
|
+
this.detach();
|
|
327
|
+
this.writes = Promise.resolve();
|
|
328
|
+
this.session = session;
|
|
329
|
+
this.postSessions();
|
|
330
|
+
this.open();
|
|
331
|
+
}
|
|
332
|
+
async create() {
|
|
333
|
+
const conn = this.conn;
|
|
334
|
+
if (!conn)
|
|
335
|
+
return;
|
|
336
|
+
try {
|
|
337
|
+
const session = await conn.call("create", { cols: this.viewport.cols, rows: this.viewport.rows });
|
|
338
|
+
this.attach(session.id);
|
|
339
|
+
}
|
|
340
|
+
catch (err) {
|
|
341
|
+
this.post({ type: "status", text: String(err) });
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
/** Input for the session, dropped unless attached on a live connection.
|
|
345
|
+
* Keys go as INPUT; a paste goes as `write` requests, each answered
|
|
346
|
+
* once the PTY took it, since INPUT frames are not acknowledged and a
|
|
347
|
+
* paste would overflow the input queue. This tab's input is sent in
|
|
348
|
+
* order, so none of it lands inside a paste. */
|
|
349
|
+
input(bytes, paste) {
|
|
350
|
+
const conn = this.conn;
|
|
351
|
+
const attachment = this.attachment;
|
|
352
|
+
const session = this.session;
|
|
353
|
+
const channel = this.channel;
|
|
354
|
+
if (bytes.length === 0 || !conn || !attachment || attachment.ended() || session === undefined)
|
|
355
|
+
return;
|
|
356
|
+
// Each request's base64 fits the control body with room for the rest,
|
|
357
|
+
// and its bytes fit the input queue (at least one byte, so the loop
|
|
358
|
+
// moves on an absurd limit).
|
|
359
|
+
const { control_body, input_queue } = conn.welcome.limits;
|
|
360
|
+
const chunk = Math.max(1, Math.min(Math.floor((control_body - 1024) / 4) * 3, input_queue));
|
|
361
|
+
this.writes = this.writes.then(async () => {
|
|
362
|
+
if (this.attachment !== attachment)
|
|
363
|
+
return;
|
|
364
|
+
try {
|
|
365
|
+
if (!paste) {
|
|
366
|
+
conn.send(FrameType.Input, channel, bytes);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
for (let at = 0; at < bytes.length; at += chunk) {
|
|
370
|
+
await conn.call("write", { session, data: base64(bytes.subarray(at, at + chunk)) });
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
catch (err) {
|
|
374
|
+
// A connection loss is reported through onEnd; a refused chunk
|
|
375
|
+
// ends the paste there.
|
|
376
|
+
if (conn === this.conn)
|
|
377
|
+
this.post({ type: "status", text: String(err) });
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
key(input) {
|
|
382
|
+
const term = this.attachment?.term();
|
|
383
|
+
if (!term)
|
|
384
|
+
return;
|
|
385
|
+
this.input(this.keys.encode(term, input), false);
|
|
386
|
+
}
|
|
387
|
+
paste(text) {
|
|
388
|
+
const term = this.attachment?.term();
|
|
389
|
+
if (!term)
|
|
390
|
+
return;
|
|
391
|
+
this.input(this.engine.encodePaste(text, term.mode(2004)), true);
|
|
392
|
+
}
|
|
393
|
+
setViewport(size, profile) {
|
|
394
|
+
this.viewport = size;
|
|
395
|
+
this.profile = profile;
|
|
396
|
+
const attachment = this.attachment;
|
|
397
|
+
if (!this.conn || !attachment || attachment.ended())
|
|
398
|
+
return;
|
|
399
|
+
this.applyProfile(attachment.term());
|
|
400
|
+
this.paint();
|
|
401
|
+
try {
|
|
402
|
+
this.conn.sendJson(FrameType.Viewport, this.channel, { ...size, profile });
|
|
403
|
+
}
|
|
404
|
+
catch {
|
|
405
|
+
// Reported through onEnd.
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
function describe(exit) {
|
|
410
|
+
return "code" in exit ? `code ${exit.code}` : `signal ${exit.signal}`;
|
|
411
|
+
}
|
|
412
|
+
/** Serves a page's `mountTerminal` on `port`: the worker module the page
|
|
413
|
+
* starts calls `runWorker(self)`. */
|
|
414
|
+
export function runWorker(port) {
|
|
415
|
+
const client = new Client((message) => port.postMessage(message));
|
|
416
|
+
port.onmessage = (e) => {
|
|
417
|
+
const message = e.data;
|
|
418
|
+
switch (message.type) {
|
|
419
|
+
case "init":
|
|
420
|
+
void client.init(message);
|
|
421
|
+
break;
|
|
422
|
+
case "attach":
|
|
423
|
+
client.attach(message.session);
|
|
424
|
+
break;
|
|
425
|
+
case "create":
|
|
426
|
+
void client.create();
|
|
427
|
+
break;
|
|
428
|
+
case "key":
|
|
429
|
+
client.key(message.input);
|
|
430
|
+
break;
|
|
431
|
+
case "paste":
|
|
432
|
+
client.paste(message.text);
|
|
433
|
+
break;
|
|
434
|
+
case "viewport":
|
|
435
|
+
client.setViewport(message.size, message.profile);
|
|
436
|
+
break;
|
|
437
|
+
case "repaint":
|
|
438
|
+
client.repaint();
|
|
439
|
+
break;
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
function base64(bytes) {
|
|
444
|
+
let binary = "";
|
|
445
|
+
for (let at = 0; at < bytes.length; at += 0x8000) {
|
|
446
|
+
binary += String.fromCharCode(...bytes.subarray(at, at + 0x8000));
|
|
447
|
+
}
|
|
448
|
+
return btoa(binary);
|
|
449
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { EngineError, Terminal } from "./index.js";
|
|
2
|
+
/** Bytes before the first record: the 8-byte magic and the u16 version. */
|
|
3
|
+
const ENVELOPE_LEN = 10;
|
|
4
|
+
/** Bytes in a record header: tag u16, payload length u32, CRC32C u32. */
|
|
5
|
+
const RECORD_HEADER_LEN = 10;
|
|
6
|
+
const TAG_PAGE = 3;
|
|
7
|
+
const TAG_READY = 5;
|
|
8
|
+
const TAG_FINISH = 6;
|
|
9
|
+
/** The record starting at `pos`: its tag and the position just past it. */
|
|
10
|
+
function recordAt(buf, len, pos) {
|
|
11
|
+
if (pos + RECORD_HEADER_LEN > len)
|
|
12
|
+
return undefined;
|
|
13
|
+
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
14
|
+
const tag = view.getUint16(pos, true);
|
|
15
|
+
const payload = view.getUint32(pos + 2, true);
|
|
16
|
+
const end = pos + RECORD_HEADER_LEN + payload;
|
|
17
|
+
return end <= len ? [tag, end] : undefined;
|
|
18
|
+
}
|
|
19
|
+
function endOfStep(buf, len, pos, last) {
|
|
20
|
+
for (;;) {
|
|
21
|
+
const record = recordAt(buf, len, pos);
|
|
22
|
+
if (!record)
|
|
23
|
+
return undefined;
|
|
24
|
+
if (last(record[0]))
|
|
25
|
+
return record[1];
|
|
26
|
+
pos = record[1];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Position just past the `READY` record, once every byte through it is in `buf`. */
|
|
30
|
+
export function readyEnd(buf, len = buf.length) {
|
|
31
|
+
return endOfStep(buf, len, ENVELOPE_LEN, (tag) => tag === TAG_READY);
|
|
32
|
+
}
|
|
33
|
+
/** Position just past the next `PAGE` or `FINISH` record at or after `pos`. */
|
|
34
|
+
export function stepEnd(buf, pos, len = buf.length) {
|
|
35
|
+
return endOfStep(buf, len, pos, (tag) => tag === TAG_PAGE || tag === TAG_FINISH);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* An incremental snapshot decoder over a buffer of known capacity, filled
|
|
39
|
+
* as bytes arrive. The library reads the buffer lazily, so `ready` and
|
|
40
|
+
* `nextPage` are called only once the bytes they consume are in place.
|
|
41
|
+
*
|
|
42
|
+
* This leans on the engine's behaviour, not its contract: the header asks
|
|
43
|
+
* for an immutable buffer. A `GhosttyReader` callback from wasm would
|
|
44
|
+
* need a JS function placed in the module's function table, which takes
|
|
45
|
+
* a generated wasm wrapper per signature; filling the buffer behind the
|
|
46
|
+
* decoder is less machinery. The engine is pinned; its decoder test
|
|
47
|
+
* pushes the bytes piecewise, so an engine that validated ahead would
|
|
48
|
+
* fail there before shipping.
|
|
49
|
+
*/
|
|
50
|
+
export class Decoder {
|
|
51
|
+
engine;
|
|
52
|
+
capacity;
|
|
53
|
+
buffer;
|
|
54
|
+
decoder;
|
|
55
|
+
filled = 0;
|
|
56
|
+
consumed = 0;
|
|
57
|
+
term;
|
|
58
|
+
complete = false;
|
|
59
|
+
constructor(engine, capacity) {
|
|
60
|
+
this.engine = engine;
|
|
61
|
+
this.capacity = capacity;
|
|
62
|
+
this.buffer = engine.alloc(capacity);
|
|
63
|
+
// Never let the decoder see another allocation's leftovers.
|
|
64
|
+
engine.memory.fill(0, this.buffer, this.buffer + capacity);
|
|
65
|
+
this.decoder = engine.construct("ghostty_snapshot_decoder_new_buf", this.buffer, capacity);
|
|
66
|
+
}
|
|
67
|
+
setMaxContinuationBytes(max) {
|
|
68
|
+
this.engine.scratch(4, (value) => {
|
|
69
|
+
this.engine.view.setUint32(value, max, true);
|
|
70
|
+
this.engine.check("ghostty_snapshot_decoder_set", this.decoder, this.engine.enumValue("GhosttySnapshotDecoderOption", "MAX_CONTINUATION_BYTES"), value);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
push(bytes) {
|
|
74
|
+
if (this.filled + bytes.length > this.capacity) {
|
|
75
|
+
throw new Error(`snapshot exceeds its declared ${this.capacity} bytes`);
|
|
76
|
+
}
|
|
77
|
+
this.engine.memory.set(bytes, this.buffer + this.filled);
|
|
78
|
+
this.filled += bytes.length;
|
|
79
|
+
}
|
|
80
|
+
get source() {
|
|
81
|
+
return this.engine.memory.subarray(this.buffer, this.buffer + this.capacity);
|
|
82
|
+
}
|
|
83
|
+
/** Whether every byte through `READY` is buffered. */
|
|
84
|
+
readyAvailable() {
|
|
85
|
+
return readyEnd(this.source, this.filled) !== undefined;
|
|
86
|
+
}
|
|
87
|
+
stepAvailable() {
|
|
88
|
+
return !this.complete && stepEnd(this.source, this.consumed, this.filled) !== undefined;
|
|
89
|
+
}
|
|
90
|
+
/** Decodes through `READY`; the terminal is then renderable. */
|
|
91
|
+
ready() {
|
|
92
|
+
const end = readyEnd(this.source, this.filled);
|
|
93
|
+
if (end === undefined)
|
|
94
|
+
throw new Error("READY not buffered");
|
|
95
|
+
this.term = new Terminal(this.engine, this.engine.take("ghostty_snapshot_decoder_ready", this.decoder));
|
|
96
|
+
this.consumed = end;
|
|
97
|
+
return this.term;
|
|
98
|
+
}
|
|
99
|
+
/** Decodes one history page. False once `FINISH` is validated. */
|
|
100
|
+
nextPage() {
|
|
101
|
+
const end = stepEnd(this.source, this.consumed, this.filled);
|
|
102
|
+
if (end === undefined)
|
|
103
|
+
throw new Error("next page not buffered");
|
|
104
|
+
const code = this.engine.call("ghostty_snapshot_decoder_next", this.decoder);
|
|
105
|
+
this.consumed = end;
|
|
106
|
+
if (code === this.engine.result.NO_VALUE) {
|
|
107
|
+
this.complete = true;
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
if (code !== this.engine.result.SUCCESS)
|
|
111
|
+
throw new EngineError("ghostty_snapshot_decoder_next", code);
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
terminal() {
|
|
115
|
+
return this.term;
|
|
116
|
+
}
|
|
117
|
+
/** Whether `FINISH` was validated: the snapshot is complete. */
|
|
118
|
+
finished() {
|
|
119
|
+
return this.complete;
|
|
120
|
+
}
|
|
121
|
+
/** Frees the decoder and its buffer; the terminal, finished or not, is the caller's. */
|
|
122
|
+
release() {
|
|
123
|
+
const term = this.term;
|
|
124
|
+
this.term = undefined;
|
|
125
|
+
this.free();
|
|
126
|
+
return term;
|
|
127
|
+
}
|
|
128
|
+
free() {
|
|
129
|
+
if (this.decoder === 0)
|
|
130
|
+
return;
|
|
131
|
+
this.engine.call("ghostty_snapshot_decoder_free", this.decoder);
|
|
132
|
+
this.engine.free(this.buffer, this.capacity);
|
|
133
|
+
this.decoder = 0;
|
|
134
|
+
this.term?.free();
|
|
135
|
+
this.term = undefined;
|
|
136
|
+
}
|
|
137
|
+
}
|