@gjermundgaraba/clankermux 0.5.0 → 0.6.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/README.md +1 -1
- package/dist/client/attachment.d.ts +7 -28
- package/dist/client/attachment.js +24 -31
- package/dist/client/client.d.ts +0 -2
- package/dist/client/client.js +6 -11
- package/dist/client/connection.d.ts +0 -18
- package/dist/client/connection.js +0 -17
- package/dist/client/copy.d.ts +0 -14
- package/dist/client/copy.js +4 -25
- package/dist/client/keyboard.d.ts +1 -27
- package/dist/client/keyboard.js +18 -34
- package/dist/client/listeners.d.ts +3 -1
- package/dist/client/listeners.js +20 -4
- package/dist/client/mount.d.ts +0 -5
- package/dist/client/mount.js +20 -72
- package/dist/client/osc52.d.ts +1 -4
- package/dist/client/osc52.js +20 -17
- package/dist/client/profile.d.ts +0 -6
- package/dist/client/profile.js +0 -6
- package/dist/client/worker.d.ts +0 -7
- package/dist/client/worker.js +84 -204
- package/dist/engine/decoder.d.ts +0 -20
- package/dist/engine/decoder.js +8 -27
- package/dist/engine/index.d.ts +2 -49
- package/dist/engine/index.js +7 -48
- package/dist/engine/keys.d.ts +0 -7
- package/dist/engine/keys.js +9 -15
- package/dist/engine/render.d.ts +7 -18
- package/dist/engine/render.js +32 -32
- package/dist/engine/selection.d.ts +0 -17
- package/dist/engine/selection.js +8 -30
- package/dist/renderer/index.d.ts +3 -4
- package/dist/renderer/index.js +38 -42
- package/package.json +1 -5
package/dist/client/worker.js
CHANGED
|
@@ -1,32 +1,21 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The engine side of a client: the connection, the attachments and their
|
|
3
|
-
* render state live in a worker; the page draws frames and reports input.
|
|
4
|
-
* One worker serves one `Client` and every terminal mounted from it.
|
|
5
|
-
* Imported as `@gjermundgaraba/clankermux/worker`, so a worker typechecked
|
|
6
|
-
* without the DOM library does not see the page's types.
|
|
7
|
-
*/
|
|
8
1
|
import { ENGINE, Engine, KeyEncoder, RenderState, SelectionGesture, } from "../engine/index.js";
|
|
9
2
|
import { FrameType, } from "../proto/index.js";
|
|
10
3
|
import { Attachment } from "./attachment.js";
|
|
11
4
|
import { ClientError, Connection, hello } from "./connection.js";
|
|
12
5
|
import { Osc52Scanner } from "./osc52.js";
|
|
13
|
-
/** The keys that, with Shift, scroll the history: by pages, or to its
|
|
14
|
-
* top or bottom. */
|
|
15
6
|
const SCROLL_KEYS = {
|
|
16
7
|
PageUp: -1,
|
|
17
8
|
PageDown: 1,
|
|
18
9
|
Home: "top",
|
|
19
10
|
End: "bottom",
|
|
20
11
|
};
|
|
21
|
-
|
|
22
|
-
const PAINT_MS = 16;
|
|
12
|
+
const PAINT_COALESCE_MS = 16;
|
|
23
13
|
const BACKOFF_MIN_MS = 250;
|
|
24
14
|
const BACKOFF_MAX_MS = 5000;
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
/** Delays before trying again, doubling up to the maximum. */
|
|
15
|
+
const UNRETRYABLE_OPEN_ERRORS = new Set(["no_such_session", "engine_mismatch", "invalid_request"]);
|
|
16
|
+
const ACKABLE_ATTENTION_STATES = new Set(["done", "error"]);
|
|
17
|
+
const ALTERNATE_SCROLL_MODE = 1007;
|
|
18
|
+
const SEND_REQUEST_OVERHEAD_BYTES = 1024;
|
|
30
19
|
class Backoff {
|
|
31
20
|
ms = BACKOFF_MIN_MS;
|
|
32
21
|
next() {
|
|
@@ -38,7 +27,6 @@ class Backoff {
|
|
|
38
27
|
this.ms = BACKOFF_MIN_MS;
|
|
39
28
|
}
|
|
40
29
|
}
|
|
41
|
-
/** A WebSocket as the client's message transport. */
|
|
42
30
|
class SocketTransport {
|
|
43
31
|
socket;
|
|
44
32
|
onMessage;
|
|
@@ -64,22 +52,36 @@ class SocketTransport {
|
|
|
64
52
|
this.socket.close();
|
|
65
53
|
}
|
|
66
54
|
}
|
|
55
|
+
function refusedByServer(err) {
|
|
56
|
+
return err instanceof ClientError && (err.kind === "server" || err.kind === "protocol");
|
|
57
|
+
}
|
|
58
|
+
function endedTheConnection(err) {
|
|
59
|
+
return err instanceof ClientError && err.server?.id === undefined;
|
|
60
|
+
}
|
|
61
|
+
function pasteChunkSize(limits) {
|
|
62
|
+
const bytesFittingAsBase64 = Math.floor((limits.control_body - SEND_REQUEST_OVERHEAD_BYTES) / 4) * 3;
|
|
63
|
+
return Math.max(1, Math.min(bytesFittingAsBase64, limits.input_queue));
|
|
64
|
+
}
|
|
65
|
+
class InputQueue {
|
|
66
|
+
tail = Promise.resolve();
|
|
67
|
+
stopWaitingOnSendsUnderWay() {
|
|
68
|
+
this.tail = Promise.resolve();
|
|
69
|
+
}
|
|
70
|
+
enqueueUnlessDetached(attached, send) {
|
|
71
|
+
this.tail = this.tail.then(() => (attached() ? send() : undefined));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
67
74
|
function failureOf(err) {
|
|
68
75
|
return err instanceof ClientError
|
|
69
76
|
? { kind: err.kind, message: err.message, ...(err.server && { server: err.server }) }
|
|
70
77
|
: { kind: "protocol", message: err instanceof Error ? err.message : String(err) };
|
|
71
78
|
}
|
|
72
|
-
/** The link ended with `error`: the server's, else the client's own. */
|
|
73
79
|
function endedWith(err, retry) {
|
|
74
80
|
const server = err instanceof ClientError ? err.server : undefined;
|
|
75
81
|
return server
|
|
76
82
|
? { state: "ended", reason: linkEnd(server.code), code: server.code, message: server.message, retry }
|
|
77
83
|
: { state: "ended", reason: "error", message: err instanceof Error ? err.message : String(err), retry };
|
|
78
84
|
}
|
|
79
|
-
/**
|
|
80
|
-
* The client: one connection, reconnected after a loss, its session list
|
|
81
|
-
* and event stream, the page's requests, and the mounted terminals.
|
|
82
|
-
*/
|
|
83
85
|
class Hub {
|
|
84
86
|
post;
|
|
85
87
|
constructor(post) {
|
|
@@ -90,24 +92,15 @@ class Hub {
|
|
|
90
92
|
url = "";
|
|
91
93
|
client = "clankermux-web";
|
|
92
94
|
conn;
|
|
93
|
-
|
|
94
|
-
* `connecting`, `connected` (to no session), or `ended` for good, with
|
|
95
|
-
* the server's refusal or error, when an `attach` connects again. */
|
|
96
|
-
state = { state: "connecting", attempt: 1 };
|
|
97
|
-
/** The latest connection-level status text, for a terminal mounted later. */
|
|
95
|
+
connectionLink = { state: "connecting", attempt: 1 };
|
|
98
96
|
statusText = "connecting";
|
|
99
|
-
/** Connection attempts since a server last answered. */
|
|
100
97
|
attempt = 0;
|
|
101
98
|
backoff = new Backoff();
|
|
102
|
-
/** Frame channels, shared by the terminals of the one connection. */
|
|
103
99
|
channel = 0;
|
|
104
100
|
mounts = new Map();
|
|
105
101
|
sessions = new Map();
|
|
106
|
-
/** The list is being paged: not shown to the page until it is whole. */
|
|
107
102
|
paging = false;
|
|
108
|
-
|
|
109
|
-
events;
|
|
110
|
-
/** The page's pending requests: their connection and its request id. */
|
|
103
|
+
eventCursor;
|
|
111
104
|
requests = new Map();
|
|
112
105
|
async init(message) {
|
|
113
106
|
this.url = message.url;
|
|
@@ -125,10 +118,8 @@ class Hub {
|
|
|
125
118
|
this.post({ type: "ready" });
|
|
126
119
|
await this.connect();
|
|
127
120
|
}
|
|
128
|
-
/** A connection-level change, to the page and every terminal. A
|
|
129
|
-
* retrying end leaves the connection `connecting` meanwhile. */
|
|
130
121
|
linkAll(link) {
|
|
131
|
-
this.
|
|
122
|
+
this.connectionLink = link.state === "ended" && link.retry ? { state: "connecting", attempt: this.attempt + 1 } : link;
|
|
132
123
|
this.post({ type: "connection", link });
|
|
133
124
|
for (const mount of this.mounts.values())
|
|
134
125
|
mount.link(link);
|
|
@@ -138,25 +129,22 @@ class Hub {
|
|
|
138
129
|
for (const mount of this.mounts.values())
|
|
139
130
|
mount.status(text);
|
|
140
131
|
}
|
|
141
|
-
/** The connection is up and subscribed: terminals may open channels. */
|
|
142
132
|
live() {
|
|
143
|
-
return this.
|
|
133
|
+
return this.connectionLink.state === "connected" ? this.conn : undefined;
|
|
144
134
|
}
|
|
145
|
-
/** The connection ended for good with `err`. */
|
|
146
135
|
refuse(err) {
|
|
147
136
|
this.statusAll(err instanceof Error ? err.message : String(err));
|
|
148
137
|
this.linkAll(endedWith(err, false));
|
|
149
138
|
}
|
|
150
139
|
async connect() {
|
|
151
|
-
this.statusAll(this.
|
|
140
|
+
this.statusAll(this.eventCursor ? "reconnecting" : "connecting");
|
|
152
141
|
this.linkAll({ state: "connecting", attempt: ++this.attempt });
|
|
153
142
|
let conn;
|
|
154
143
|
try {
|
|
155
144
|
conn = await Connection.connect(await SocketTransport.connect(this.url), hello(this.client, ENGINE));
|
|
156
145
|
}
|
|
157
146
|
catch (err) {
|
|
158
|
-
|
|
159
|
-
if (err instanceof ClientError && (err.kind === "server" || err.kind === "protocol")) {
|
|
147
|
+
if (refusedByServer(err)) {
|
|
160
148
|
this.refuse(err);
|
|
161
149
|
return;
|
|
162
150
|
}
|
|
@@ -183,34 +171,29 @@ class Hub {
|
|
|
183
171
|
await this.subscribe(conn);
|
|
184
172
|
}
|
|
185
173
|
catch (err) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
this.refuse(err);
|
|
192
|
-
}
|
|
174
|
+
if (endedTheConnection(err))
|
|
175
|
+
return;
|
|
176
|
+
this.conn = undefined;
|
|
177
|
+
conn.close();
|
|
178
|
+
this.refuse(err);
|
|
193
179
|
return;
|
|
194
180
|
}
|
|
195
|
-
this.
|
|
181
|
+
this.connectionLink = { state: "connected", session: undefined };
|
|
196
182
|
this.statusText = "connected";
|
|
197
|
-
this.post({ type: "connection", link: this.
|
|
183
|
+
this.post({ type: "connection", link: this.connectionLink });
|
|
198
184
|
for (const mount of this.mounts.values())
|
|
199
185
|
mount.connected();
|
|
200
186
|
}
|
|
201
|
-
/** Subscribes: from the first page on the first connection; after a
|
|
202
|
-
* reconnect from the last event seen, replaying those missed, or, when
|
|
203
|
-
* the server cannot (a gap), from the first page again. */
|
|
204
187
|
async subscribe(conn) {
|
|
205
188
|
const incarnation = conn.welcome.incarnation;
|
|
206
|
-
if (this.
|
|
189
|
+
if (this.eventCursor) {
|
|
207
190
|
const replay = await conn.call("subscribe", {
|
|
208
|
-
after: this.
|
|
209
|
-
incarnation: this.
|
|
191
|
+
after: this.eventCursor.seq,
|
|
192
|
+
incarnation: this.eventCursor.incarnation,
|
|
210
193
|
});
|
|
211
194
|
if (!replay.gap)
|
|
212
195
|
return;
|
|
213
|
-
this.
|
|
196
|
+
this.eventCursor = { incarnation, seq: replay.latest };
|
|
214
197
|
await this.page(conn, await conn.call("list", {}));
|
|
215
198
|
this.post({ type: "gap", incarnation });
|
|
216
199
|
for (const mount of this.mounts.values())
|
|
@@ -218,12 +201,9 @@ class Hub {
|
|
|
218
201
|
return;
|
|
219
202
|
}
|
|
220
203
|
const first = await conn.call("subscribe", {});
|
|
221
|
-
this.
|
|
204
|
+
this.eventCursor = { incarnation, seq: first.latest };
|
|
222
205
|
await this.page(conn, { sessions: first.sessions ?? [], ...(first.next !== undefined && { next: first.next }) });
|
|
223
206
|
}
|
|
224
|
-
/** The session list from `page` on. Events arriving between pages are
|
|
225
|
-
* applied to the same map: a later page cannot hold a session removed
|
|
226
|
-
* before it was produced. The page sees the list once it is whole. */
|
|
227
207
|
async page(conn, page) {
|
|
228
208
|
this.sessions.clear();
|
|
229
209
|
this.paging = true;
|
|
@@ -242,8 +222,8 @@ class Hub {
|
|
|
242
222
|
this.postSessions();
|
|
243
223
|
}
|
|
244
224
|
event(event) {
|
|
245
|
-
if (this.
|
|
246
|
-
this.
|
|
225
|
+
if (this.eventCursor)
|
|
226
|
+
this.eventCursor.seq = event.seq;
|
|
247
227
|
switch (event.event) {
|
|
248
228
|
case "session_created":
|
|
249
229
|
case "session_changed":
|
|
@@ -270,7 +250,6 @@ class Hub {
|
|
|
270
250
|
nextChannel() {
|
|
271
251
|
return ++this.channel;
|
|
272
252
|
}
|
|
273
|
-
/** A protocol request from the page; its outcome goes back by `id`. */
|
|
274
253
|
async request(id, method, params) {
|
|
275
254
|
const conn = this.conn;
|
|
276
255
|
if (!conn) {
|
|
@@ -289,37 +268,29 @@ class Hub {
|
|
|
289
268
|
this.requests.delete(id);
|
|
290
269
|
}
|
|
291
270
|
}
|
|
292
|
-
/** Cancels the page's request `id`, when it is still pending. Its own
|
|
293
|
-
* reply is the server's `cancelled`. */
|
|
294
271
|
cancel(id) {
|
|
295
272
|
const pending = this.requests.get(id);
|
|
296
273
|
if (!pending || pending.conn !== this.conn)
|
|
297
274
|
return;
|
|
298
275
|
pending.conn.call("cancel", { id: pending.wire }).catch(() => { });
|
|
299
276
|
}
|
|
300
|
-
/** A terminal mounted after the connection ended for good connects
|
|
301
|
-
* again, as an `attach` does; otherwise it first sees the connection as
|
|
302
|
-
* it is now, as a terminal mounted at the start would have. */
|
|
303
277
|
mount(id, init) {
|
|
304
278
|
const mount = new Mount(this, id, init);
|
|
305
279
|
this.mounts.set(id, mount);
|
|
306
280
|
mount.status(this.statusText);
|
|
307
|
-
if (this.
|
|
281
|
+
if (this.connectionLink.state === "ended")
|
|
308
282
|
this.reattach(mount);
|
|
309
283
|
else if (this.live())
|
|
310
284
|
mount.connected();
|
|
311
285
|
else
|
|
312
|
-
mount.link(this.
|
|
286
|
+
mount.link(this.connectionLink);
|
|
313
287
|
}
|
|
314
288
|
unmount(id) {
|
|
315
289
|
this.mounts.get(id)?.dispose();
|
|
316
290
|
this.mounts.delete(id);
|
|
317
291
|
}
|
|
318
|
-
/** After the server refused or ended the connection, connects again;
|
|
319
|
-
* otherwise opens `mount`'s session, or leaves it to the connection
|
|
320
|
-
* coming up. */
|
|
321
292
|
reattach(mount) {
|
|
322
|
-
if (this.
|
|
293
|
+
if (this.connectionLink.state === "ended") {
|
|
323
294
|
this.attempt = 0;
|
|
324
295
|
void this.connect();
|
|
325
296
|
}
|
|
@@ -328,47 +299,31 @@ class Hub {
|
|
|
328
299
|
}
|
|
329
300
|
}
|
|
330
301
|
}
|
|
331
|
-
/** One mounted terminal: an attachment on the hub's connection, its render
|
|
332
|
-
* state, selection, input and size. */
|
|
333
302
|
class Mount {
|
|
334
303
|
hub;
|
|
335
304
|
id;
|
|
336
305
|
attachment;
|
|
337
306
|
channel = 0;
|
|
338
|
-
/** The connection the channel is open on. */
|
|
339
307
|
openOn;
|
|
340
|
-
/** An `ack` is under way. */
|
|
341
308
|
acking = false;
|
|
342
|
-
/** The session attached or being attached. */
|
|
343
309
|
session;
|
|
344
310
|
viewport;
|
|
345
311
|
profile;
|
|
346
|
-
/** The page is in use; only then are its size and profile reported. */
|
|
347
312
|
active;
|
|
348
313
|
visible;
|
|
349
314
|
readOnly;
|
|
350
315
|
ackOnFocus;
|
|
351
|
-
/** The terminal has the keyboard and its window the focus. */
|
|
352
316
|
focused = false;
|
|
353
|
-
|
|
354
|
-
* next input reports its size again. */
|
|
355
|
-
reclaim = false;
|
|
317
|
+
sizeTakenByOtherClient = false;
|
|
356
318
|
render;
|
|
357
319
|
gesture;
|
|
358
|
-
/** The page holds a selection's text. */
|
|
359
320
|
selected = false;
|
|
360
|
-
|
|
361
|
-
* meanwhile wait behind it. `attach` starts a fresh chain; input queued
|
|
362
|
-
* for an attachment that is gone is dropped, but a paste under way
|
|
363
|
-
* finishes (cut short, it would leave the program inside a bracketed
|
|
364
|
-
* paste). */
|
|
365
|
-
writes = Promise.resolve();
|
|
321
|
+
inputQueue = new InputQueue();
|
|
366
322
|
clipboard = new Osc52Scanner();
|
|
367
323
|
paintTimer;
|
|
368
324
|
paintPending = false;
|
|
369
325
|
frameInFlight = false;
|
|
370
326
|
fullNext = false;
|
|
371
|
-
/** Re-attaches after a failure; reset by a screen. */
|
|
372
327
|
backoff = new Backoff();
|
|
373
328
|
reopenTimer;
|
|
374
329
|
disposed = false;
|
|
@@ -395,7 +350,6 @@ class Mount {
|
|
|
395
350
|
status(text) {
|
|
396
351
|
this.post({ type: "status", mount: this.id, text });
|
|
397
352
|
}
|
|
398
|
-
/** The hub's connection is up: attach the session, if there is one. */
|
|
399
353
|
connected() {
|
|
400
354
|
if (this.session !== undefined) {
|
|
401
355
|
this.open();
|
|
@@ -405,7 +359,6 @@ class Mount {
|
|
|
405
359
|
this.link({ state: "connected", session: undefined });
|
|
406
360
|
}
|
|
407
361
|
}
|
|
408
|
-
/** Opens a channel for `this.session`, resuming when the attachment can. */
|
|
409
362
|
open() {
|
|
410
363
|
const conn = this.hub.live();
|
|
411
364
|
if (!conn || this.session === undefined || this.disposed || this.openOn === conn)
|
|
@@ -418,7 +371,6 @@ class Mount {
|
|
|
418
371
|
this.channel = channel;
|
|
419
372
|
this.openOn = conn;
|
|
420
373
|
const resume = attachment.resume();
|
|
421
|
-
// An OPEN with a viewport or profile is a report applied at once.
|
|
422
374
|
const open = {
|
|
423
375
|
session: this.session,
|
|
424
376
|
...(this.readOnly && { read_only: true }),
|
|
@@ -433,8 +385,6 @@ class Mount {
|
|
|
433
385
|
actions = attachment.handle(frame);
|
|
434
386
|
}
|
|
435
387
|
catch (err) {
|
|
436
|
-
// A lost place in the stream: CLOSE and OPEN again, afresh. Any
|
|
437
|
-
// other fault would only repeat.
|
|
438
388
|
this.status(String(err));
|
|
439
389
|
const desync = err instanceof ClientError && err.kind === "desync";
|
|
440
390
|
this.link(endedWith(err, desync));
|
|
@@ -444,33 +394,20 @@ class Mount {
|
|
|
444
394
|
this.giveUp();
|
|
445
395
|
return;
|
|
446
396
|
}
|
|
447
|
-
if (frame.header.type === FrameType.Attached && actions.length === 0) {
|
|
448
|
-
// Resumed: the terminal shows the stream already, and an exit
|
|
449
|
-
// still to come arrives as EXITED. A snapshot's screen comes
|
|
450
|
-
// later, as an action.
|
|
451
|
-
if (attachment.resume()) {
|
|
452
|
-
this.status("resumed");
|
|
453
|
-
this.link({ state: "connected", session: attachment.session });
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
397
|
for (const action of actions)
|
|
457
398
|
this.apply(action);
|
|
458
399
|
});
|
|
459
400
|
}
|
|
460
|
-
/** The screen of `session` is shown: `connected`, and at once `ended
|
|
461
|
-
* exited` when its program has exited already. */
|
|
462
401
|
attached(session) {
|
|
463
402
|
this.link({ state: "connected", session });
|
|
464
403
|
if (session?.status === "exited") {
|
|
465
404
|
this.link({ state: "ended", reason: "exited", ...(session.exit && { exit: session.exit }), retry: false });
|
|
466
405
|
}
|
|
467
406
|
}
|
|
468
|
-
/** Detaches for good: a reconnect does not try the session again. */
|
|
469
407
|
giveUp() {
|
|
470
408
|
this.close();
|
|
471
409
|
this.session = undefined;
|
|
472
410
|
}
|
|
473
|
-
/** Detaches and attaches the same session again after a backoff. */
|
|
474
411
|
reopen() {
|
|
475
412
|
this.close();
|
|
476
413
|
this.reopenTimer ??= setTimeout(() => {
|
|
@@ -499,26 +436,24 @@ class Mount {
|
|
|
499
436
|
this.changed(session);
|
|
500
437
|
break;
|
|
501
438
|
}
|
|
439
|
+
case "resumed":
|
|
440
|
+
this.status("resumed");
|
|
441
|
+
this.link({ state: "connected", session: this.attachment?.session });
|
|
442
|
+
break;
|
|
502
443
|
case "output":
|
|
503
|
-
// Selection state must reach the page even while painting is paused.
|
|
504
444
|
if (this.selected && !this.attachment?.term()?.selected)
|
|
505
445
|
this.unselected();
|
|
506
446
|
for (const write of this.clipboard.feed(action.bytes)) {
|
|
507
|
-
|
|
508
|
-
// browser equivalent.
|
|
509
|
-
if (write.selection === "" || write.selection.includes("c")) {
|
|
447
|
+
if (write.toClipboard)
|
|
510
448
|
this.post({ type: "clipboard", mount: this.id, text: write.text });
|
|
511
|
-
}
|
|
512
449
|
}
|
|
513
450
|
this.paintPending = true;
|
|
514
451
|
if (this.visible && !this.frameInFlight)
|
|
515
|
-
this.paintTimer ??= setTimeout(() => this.paint(),
|
|
452
|
+
this.paintTimer ??= setTimeout(() => this.paint(), PAINT_COALESCE_MS);
|
|
516
453
|
break;
|
|
517
454
|
case "resized":
|
|
518
|
-
// Another client's size, while this page kept its focus (the user
|
|
519
|
-
// went to another machine): taken back at this page's next input.
|
|
520
455
|
if (this.active && (action.cols !== this.viewport.cols || action.rows !== this.viewport.rows)) {
|
|
521
|
-
this.
|
|
456
|
+
this.sizeTakenByOtherClient = true;
|
|
522
457
|
}
|
|
523
458
|
this.paint();
|
|
524
459
|
break;
|
|
@@ -534,9 +469,6 @@ class Mount {
|
|
|
534
469
|
break;
|
|
535
470
|
case "closed":
|
|
536
471
|
this.status(`closed: ${action.reason}`);
|
|
537
|
-
// `stopping` is followed by the connection's close, and the
|
|
538
|
-
// reconnect attaches again; a removed session is gone for good,
|
|
539
|
-
// and a detached page does not take its session back.
|
|
540
472
|
this.link({
|
|
541
473
|
state: "ended",
|
|
542
474
|
reason: action.reason,
|
|
@@ -548,13 +480,10 @@ class Mount {
|
|
|
548
480
|
break;
|
|
549
481
|
case "error": {
|
|
550
482
|
this.status(`${action.error.code}: ${action.error.message}`);
|
|
551
|
-
// An error ended the attachment: attach again after a backoff
|
|
552
|
-
// (`too_slow`, a snapshot that could not be taken), unless a new
|
|
553
|
-
// OPEN would fail the same way.
|
|
554
483
|
if (!this.attachment?.closed())
|
|
555
484
|
break;
|
|
556
485
|
const { code, message } = action.error;
|
|
557
|
-
const retry = !
|
|
486
|
+
const retry = !UNRETRYABLE_OPEN_ERRORS.has(code);
|
|
558
487
|
this.link({ state: "ended", reason: linkEnd(code), code, message, retry });
|
|
559
488
|
if (retry)
|
|
560
489
|
this.reopen();
|
|
@@ -564,8 +493,6 @@ class Mount {
|
|
|
564
493
|
}
|
|
565
494
|
}
|
|
566
495
|
}
|
|
567
|
-
/** The page's colours on the local terminal, as the server has them on
|
|
568
|
-
* its own: the stream does not carry default colours. */
|
|
569
496
|
applyProfile(term) {
|
|
570
497
|
if (!term)
|
|
571
498
|
return;
|
|
@@ -614,24 +541,17 @@ class Mount {
|
|
|
614
541
|
this.render = new RenderState(this.hub.engine);
|
|
615
542
|
this.fullNext = false;
|
|
616
543
|
}
|
|
617
|
-
const frame = this.render.
|
|
618
|
-
// Clean only the captured rows. While this frame is in flight, no
|
|
619
|
-
// extraction runs, so subsequent changes accumulate in the engine.
|
|
620
|
-
this.render.clean();
|
|
544
|
+
const frame = this.render.takePackedFrame(term);
|
|
621
545
|
this.paintPending = false;
|
|
622
546
|
this.frameInFlight = true;
|
|
623
547
|
this.post({ type: "frame", mount: this.id, frame }, { transfer: frame.lines.map((row) => row.cells.data.buffer) });
|
|
624
|
-
// A switch to the alternate screen or a full reset took the selection.
|
|
625
548
|
if (this.selected && !term.selected)
|
|
626
549
|
this.unselected();
|
|
627
550
|
}
|
|
628
|
-
/** The mouse: a press clears the selection, or selects a word or a line
|
|
629
|
-
* at once; a drag selects; the release copies what is selected. */
|
|
630
551
|
select(message) {
|
|
631
552
|
const { at, time } = message;
|
|
632
553
|
const term = this.attachment?.term();
|
|
633
554
|
if (!term) {
|
|
634
|
-
// Nothing to select: the page's copy waits for the answer even so.
|
|
635
555
|
if (message.action === "release") {
|
|
636
556
|
this.post({ type: "selection", mount: this.id, text: undefined, copy: message.copy });
|
|
637
557
|
}
|
|
@@ -652,8 +572,6 @@ class Mount {
|
|
|
652
572
|
}
|
|
653
573
|
case "release": {
|
|
654
574
|
this.gesture.release(term, at);
|
|
655
|
-
// Answered either way: the page's copy waits for it. Blanks only
|
|
656
|
-
// (trimmed to nothing) are no text to copy.
|
|
657
575
|
const text = term.selectionText() || undefined;
|
|
658
576
|
this.selected = text !== undefined;
|
|
659
577
|
this.post({ type: "selection", mount: this.id, text, copy: message.copy });
|
|
@@ -662,7 +580,6 @@ class Mount {
|
|
|
662
580
|
}
|
|
663
581
|
this.paint();
|
|
664
582
|
}
|
|
665
|
-
/** Input clears the selection. */
|
|
666
583
|
unselect() {
|
|
667
584
|
const term = this.attachment?.term();
|
|
668
585
|
if (!term?.selected)
|
|
@@ -676,19 +593,14 @@ class Mount {
|
|
|
676
593
|
this.selected = false;
|
|
677
594
|
this.post({ type: "selection", mount: this.id, text: undefined });
|
|
678
595
|
}
|
|
679
|
-
/** The terminal goes, and its selection and the gesture's anchor with it. */
|
|
680
596
|
forgetSelection(term) {
|
|
681
597
|
this.gesture.forget(term);
|
|
682
598
|
if (this.selected)
|
|
683
599
|
this.unselected();
|
|
684
600
|
}
|
|
685
|
-
/** Closes the current attachment's channel and forgets it. An
|
|
686
|
-
* attachment the server ended needs no CLOSE; one whose stream ended
|
|
687
|
-
* (EXITED) does. */
|
|
688
601
|
close() {
|
|
689
602
|
if (!this.attachment)
|
|
690
603
|
return;
|
|
691
|
-
// Still alive: the gesture's anchor is released in it.
|
|
692
604
|
this.forgetSelection(this.attachment.term());
|
|
693
605
|
const conn = this.hub.conn;
|
|
694
606
|
if (conn) {
|
|
@@ -700,19 +612,16 @@ class Mount {
|
|
|
700
612
|
this.attachment = undefined;
|
|
701
613
|
this.openOn = undefined;
|
|
702
614
|
}
|
|
703
|
-
/** Detaches the current session and attaches `session`; after the
|
|
704
|
-
* server refused or ended the connection, connects again first. */
|
|
705
615
|
attach(session) {
|
|
706
616
|
this.close();
|
|
707
|
-
this.
|
|
617
|
+
this.inputQueue.stopWaitingOnSendsUnderWay();
|
|
708
618
|
this.session = session;
|
|
709
619
|
this.hub.reattach(this);
|
|
710
620
|
}
|
|
711
|
-
/** Detaches, and stays attached to none. */
|
|
712
621
|
detach() {
|
|
713
622
|
const had = this.session !== undefined;
|
|
714
623
|
this.giveUp();
|
|
715
|
-
this.
|
|
624
|
+
this.inputQueue.stopWaitingOnSendsUnderWay();
|
|
716
625
|
if (had && this.hub.conn) {
|
|
717
626
|
this.status("no session");
|
|
718
627
|
this.link({ state: "connected", session: undefined });
|
|
@@ -728,13 +637,20 @@ class Mount {
|
|
|
728
637
|
this.render.free();
|
|
729
638
|
this.gesture.forget(undefined);
|
|
730
639
|
}
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
640
|
+
sendInput(bytes) {
|
|
641
|
+
this.queueInput(bytes, async ({ conn, channel }) => {
|
|
642
|
+
conn.send(FrameType.Input, channel, bytes);
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
sendPaste(bytes) {
|
|
646
|
+
this.queueInput(bytes, async ({ conn, session }) => {
|
|
647
|
+
const chunk = pasteChunkSize(conn.welcome.limits);
|
|
648
|
+
for (let at = 0; at < bytes.length; at += chunk) {
|
|
649
|
+
await conn.call("send", { session, data: base64(bytes.subarray(at, at + chunk)) });
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
queueInput(bytes, deliver) {
|
|
738
654
|
const conn = this.hub.conn;
|
|
739
655
|
const attachment = this.attachment;
|
|
740
656
|
const session = this.session;
|
|
@@ -745,26 +661,11 @@ class Mount {
|
|
|
745
661
|
this.claim();
|
|
746
662
|
this.backToLive();
|
|
747
663
|
this.unselect();
|
|
748
|
-
|
|
749
|
-
// and its bytes fit the input queue (at least one byte, so the loop
|
|
750
|
-
// moves on an absurd limit).
|
|
751
|
-
const { control_body, input_queue } = conn.welcome.limits;
|
|
752
|
-
const chunk = Math.max(1, Math.min(Math.floor((control_body - 1024) / 4) * 3, input_queue));
|
|
753
|
-
this.writes = this.writes.then(async () => {
|
|
754
|
-
if (this.attachment !== attachment)
|
|
755
|
-
return;
|
|
664
|
+
this.inputQueue.enqueueUnlessDetached(() => this.attachment === attachment, async () => {
|
|
756
665
|
try {
|
|
757
|
-
|
|
758
|
-
conn.send(FrameType.Input, channel, bytes);
|
|
759
|
-
return;
|
|
760
|
-
}
|
|
761
|
-
for (let at = 0; at < bytes.length; at += chunk) {
|
|
762
|
-
await conn.call("send", { session, data: base64(bytes.subarray(at, at + chunk)) });
|
|
763
|
-
}
|
|
666
|
+
await deliver({ conn, session, channel });
|
|
764
667
|
}
|
|
765
668
|
catch (err) {
|
|
766
|
-
// A connection loss is reported through onEnd; a refused chunk
|
|
767
|
-
// ends the paste there.
|
|
768
669
|
if (conn === this.hub.conn)
|
|
769
670
|
this.status(String(err));
|
|
770
671
|
}
|
|
@@ -774,8 +675,6 @@ class Mount {
|
|
|
774
675
|
const term = this.attachment?.term();
|
|
775
676
|
if (!term)
|
|
776
677
|
return;
|
|
777
|
-
// Shift+PageUp, PageDown, Home and End scroll the history, press and
|
|
778
|
-
// release; on the alternate screen, which has none, they are keys.
|
|
779
678
|
const scroll = SCROLL_KEYS[input.code];
|
|
780
679
|
if (scroll && input.shift && !input.ctrl && !input.alt && !input.meta && !term.alternate) {
|
|
781
680
|
if (input.action === "release")
|
|
@@ -784,11 +683,8 @@ class Mount {
|
|
|
784
683
|
this.paint();
|
|
785
684
|
return;
|
|
786
685
|
}
|
|
787
|
-
this.
|
|
686
|
+
this.sendInput(this.hub.keys.encode(term, input));
|
|
788
687
|
}
|
|
789
|
-
/** The wheel. On the alternate screen, with alternate scroll (mode
|
|
790
|
-
* 1007, on by default), it is the arrow keys, as a full-screen program
|
|
791
|
-
* such as `less` expects; without it, nothing. */
|
|
792
688
|
wheel(rows) {
|
|
793
689
|
const term = this.attachment?.term();
|
|
794
690
|
if (!term || rows === 0)
|
|
@@ -799,17 +695,15 @@ class Mount {
|
|
|
799
695
|
this.paint();
|
|
800
696
|
return;
|
|
801
697
|
}
|
|
802
|
-
if (!term.mode(
|
|
698
|
+
if (!term.mode(ALTERNATE_SCROLL_MODE))
|
|
803
699
|
return;
|
|
804
700
|
const code = rows < 0 ? "ArrowUp" : "ArrowDown";
|
|
805
701
|
const arrow = this.hub.keys.encode(term, { code, key: code, action: "press", shift: false, ctrl: false, alt: false, meta: false });
|
|
806
702
|
const bytes = new Uint8Array(arrow.length * Math.abs(rows));
|
|
807
703
|
for (let i = 0; i < Math.abs(rows); i++)
|
|
808
704
|
bytes.set(arrow, i * arrow.length);
|
|
809
|
-
this.
|
|
705
|
+
this.sendInput(bytes);
|
|
810
706
|
}
|
|
811
|
-
/** The view through the history, as the page asks: by rows, or back to
|
|
812
|
-
* the live screen. The alternate screen has no history. */
|
|
813
707
|
view(rows) {
|
|
814
708
|
const term = this.attachment?.term();
|
|
815
709
|
if (!term || term.alternate)
|
|
@@ -821,7 +715,6 @@ class Mount {
|
|
|
821
715
|
this.paint();
|
|
822
716
|
}
|
|
823
717
|
}
|
|
824
|
-
/** Back to the live screen before input, as any terminal does. */
|
|
825
718
|
backToLive() {
|
|
826
719
|
const term = this.attachment?.term();
|
|
827
720
|
if (!term)
|
|
@@ -832,22 +725,17 @@ class Mount {
|
|
|
832
725
|
term.scroll("bottom");
|
|
833
726
|
this.paint();
|
|
834
727
|
}
|
|
835
|
-
/** Text no key sent, as UTF-8: a composition's, the emoji picker's. */
|
|
836
728
|
text(text) {
|
|
837
729
|
if (!this.attachment?.term())
|
|
838
730
|
return;
|
|
839
|
-
this.
|
|
731
|
+
this.sendInput(new TextEncoder().encode(text));
|
|
840
732
|
}
|
|
841
733
|
paste(text) {
|
|
842
734
|
const term = this.attachment?.term();
|
|
843
735
|
if (!term)
|
|
844
736
|
return;
|
|
845
|
-
this.
|
|
737
|
+
this.sendPaste(this.hub.engine.encodePaste(text, term.mode(2004)));
|
|
846
738
|
}
|
|
847
|
-
/** Keeps the page's size and colours (for the local terminal), and
|
|
848
|
-
* reports them to the session while the page is active. A report
|
|
849
|
-
* repeats the last one when the page becomes active again, so the
|
|
850
|
-
* session takes its size back from another client. */
|
|
851
739
|
setViewport(size, profile, active) {
|
|
852
740
|
this.viewport = size;
|
|
853
741
|
this.profile = profile;
|
|
@@ -861,37 +749,30 @@ class Mount {
|
|
|
861
749
|
this.report();
|
|
862
750
|
}
|
|
863
751
|
report() {
|
|
864
|
-
this.
|
|
752
|
+
this.sizeTakenByOtherClient = false;
|
|
865
753
|
this.hub.conn?.sendJson(FrameType.Viewport, this.channel, { ...this.viewport, profile: this.profile });
|
|
866
754
|
}
|
|
867
|
-
/** Input from the page in use: its size again if another client took it. */
|
|
868
755
|
claim() {
|
|
869
|
-
if (this.
|
|
756
|
+
if (this.sizeTakenByOtherClient && this.active && this.attachment && !this.attachment.ended())
|
|
870
757
|
this.report();
|
|
871
758
|
}
|
|
872
759
|
focus(focused) {
|
|
873
760
|
this.focused = focused;
|
|
874
761
|
this.recheck();
|
|
875
762
|
}
|
|
876
|
-
/** Looks at this terminal's session again, as `changed` does. */
|
|
877
763
|
recheck() {
|
|
878
764
|
const session = this.session === undefined ? undefined : this.hub.session(this.session);
|
|
879
765
|
if (session)
|
|
880
766
|
this.changed(session);
|
|
881
767
|
}
|
|
882
|
-
/** A session changed: when it is this terminal's, its screen is shown,
|
|
883
|
-
* the page opted in and the terminal has the focus, the `done` and
|
|
884
|
-
* `error` records that want the user are marked seen. */
|
|
885
768
|
changed(session) {
|
|
886
769
|
if (!this.ackOnFocus || !this.focused || this.acking || session.id !== this.session)
|
|
887
770
|
return;
|
|
888
|
-
if (!this.attachment?.term() || !
|
|
771
|
+
if (!this.attachment?.term() || !ACKABLE_ATTENTION_STATES.has(session.attention?.state ?? ""))
|
|
889
772
|
return;
|
|
890
773
|
const conn = this.hub.live();
|
|
891
774
|
if (!conn)
|
|
892
775
|
return;
|
|
893
|
-
// One at a time; a record that came meanwhile is looked at once the
|
|
894
|
-
// ack is answered (not after a refusal, which would only repeat).
|
|
895
776
|
this.acking = true;
|
|
896
777
|
conn.call("ack", { session: session.id }).then(() => {
|
|
897
778
|
this.acking = false;
|
|
@@ -899,7 +780,6 @@ class Mount {
|
|
|
899
780
|
}, () => (this.acking = false));
|
|
900
781
|
}
|
|
901
782
|
}
|
|
902
|
-
/** The reason an error code ends the link with. */
|
|
903
783
|
function linkEnd(code) {
|
|
904
784
|
return code === "no_such_session" || code === "engine_mismatch" ? code : "error";
|
|
905
785
|
}
|