@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
package/README.md
CHANGED
|
@@ -1,3 +1,97 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @gjermundgaraba/clankermux
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The browser client of [clankermux](https://github.com/gjermundgaraba/clankermux): a terminal on a canvas, its engine and connection in a module worker, speaking the clankermux protocol over the server's TLS WebSocket carrier.
|
|
4
|
+
|
|
5
|
+
| Subpath | Exports |
|
|
6
|
+
| --- | --- |
|
|
7
|
+
| `.` | `mountTerminal` and its types, `ClientError` |
|
|
8
|
+
| `./worker` | `runWorker`, for the worker module; no DOM types |
|
|
9
|
+
| `./ghostty-vt.wasm` | the engine build the server's engine id names |
|
|
10
|
+
| `./proto`, `./engine`, `./renderer` | the protocol messages, the engine and the canvas renderer |
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
// worker.ts, started as a module worker
|
|
14
|
+
import { runWorker } from "@gjermundgaraba/clankermux/worker";
|
|
15
|
+
runWorker(self);
|
|
16
|
+
|
|
17
|
+
// page
|
|
18
|
+
import { mountTerminal } from "@gjermundgaraba/clankermux";
|
|
19
|
+
import wasm from "@gjermundgaraba/clankermux/ghostty-vt.wasm?url";
|
|
20
|
+
const terminal = mountTerminal({
|
|
21
|
+
worker: new Worker(new URL("./worker.ts", import.meta.url), { type: "module" }),
|
|
22
|
+
canvas, container, wasm, url: "wss://127.0.0.1:7438/", session,
|
|
23
|
+
font: "14px monospace", colors: () => ({ fg: "#d8dee9", bg: "#282c34", cursor: "#ffffff" }),
|
|
24
|
+
onLink: (link) => console.log(link),
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## `MountedTerminal`
|
|
29
|
+
|
|
30
|
+
| Method | Does |
|
|
31
|
+
| --- | --- |
|
|
32
|
+
| `attach(session)` | Detaches the current session and attaches `session`; after a connection-level end (below), connects again first. |
|
|
33
|
+
| `create(options?)` | Creates a session at the current size, attaches it, and resolves with it. `options.onExit`: `"keep"` keeps it listed as `exited` when its program exits; `"remove"` removes it `limits.remove_grace` after. Without it the server's default (`remove`) applies. |
|
|
34
|
+
| `end()` | Hangs up the attached session's processes (SIGHUP). Also resolves when the session is not running (`exited`, `restored`): there is nothing to end. |
|
|
35
|
+
| `remove()` | Removes the attached session, which must be `exited` or `restored`; a running one rejects with `running`. |
|
|
36
|
+
| `report()` | Reports the container's size, the colours and the scheme again. |
|
|
37
|
+
| `paste(text)` | Pastes, bracketed when the program asked for it. |
|
|
38
|
+
| `dispose()` | Removes the listeners and terminates the worker, which drops its connection; requests in flight reject. |
|
|
39
|
+
|
|
40
|
+
`create`, `end` and `remove` need a connection: call them after a `connected` link. They reject with a `ClientError`:
|
|
41
|
+
|
|
42
|
+
- with `kind: "server"` and `server.code` when the server refuses;
|
|
43
|
+
- with `kind: "closed"` when there is no connection, or after `dispose()`;
|
|
44
|
+
- with `kind: "protocol"` when the server sent a malformed frame meanwhile (the connection then ends with `ended closed`, `retry: true`).
|
|
45
|
+
|
|
46
|
+
`end` and `remove` reject with a plain `Error` when no session is attached. Since 0.1.1 `create` returns this promise, where it returned nothing and dropped a request it could not make; a caller that ignores it should `.catch` it. Closing a pane for good is `end()` for a running session and `remove()` for one that has exited, or that a previous server left `restored`. With `onExit: "remove"` an ended session also removes itself.
|
|
47
|
+
|
|
48
|
+
## `onLink`
|
|
49
|
+
|
|
50
|
+
`onLink(link)` reports each change of the page's link to its session, through the connection and the attachment. `onStatus(text)` reports the same moments, and more, as text for display.
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
type Link =
|
|
54
|
+
| { state: "connecting"; attempt: number }
|
|
55
|
+
| { state: "connected"; session: Session | undefined }
|
|
56
|
+
| { state: "ended"; reason: LinkEnd; code?: ErrorCode; message?: string; exit?: Exit; retry: boolean };
|
|
57
|
+
type LinkEnd = "closed" | "removed" | "stopping" | "exited" | "no_such_session" | "engine_mismatch" | "error";
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
`retry` means the worker reconnects or attaches again by itself, so a `connecting` or `connected` follows. Otherwise nothing follows until the page calls `attach` or `create`. After a connection-level end (the last three rows of the first table) there is no connection: `create` rejects, and `attach` connects again (`connecting` from attempt 1), so "Retry" can be `attach`. An engine that failed to load needs a new mount.
|
|
61
|
+
|
|
62
|
+
| When | Link |
|
|
63
|
+
| --- | --- |
|
|
64
|
+
| A connection is opened: at mount, and at each reconnect | `connecting`, `attempt` from 1; it starts again at 1 once a server answers |
|
|
65
|
+
| A connection cannot be opened, or closes before `WELCOME` | nothing at once; the next `connecting` after a backoff |
|
|
66
|
+
| The connection closes | `ended closed`, `retry: true`, then `connecting` |
|
|
67
|
+
| The engine cannot be loaded | `ended error`, `retry: false` |
|
|
68
|
+
| The server refuses the handshake (`ERROR` before `WELCOME`, e.g. `version_mismatch`), or sends something else first | `ended error` with the code when there is one, `retry: false` |
|
|
69
|
+
| The server ends the connection (`ERROR` without an id, e.g. `protocol_error`) | `ended error` with the code, `retry: false` |
|
|
70
|
+
| `subscribe` fails on a live connection | `ended error`, `retry: false`; the worker closes the connection |
|
|
71
|
+
|
|
72
|
+
Once connected:
|
|
73
|
+
|
|
74
|
+
| When | Link |
|
|
75
|
+
| --- | --- |
|
|
76
|
+
| Connected with no session to attach | `connected`, `session: undefined` |
|
|
77
|
+
| A session's screen is shown: a snapshot's first screen, a resume, again after a gap, a re-attach or a reconnect | `connected` with the session as `ATTACHED` described it; `status` tells `running`, `restored` or `exited` |
|
|
78
|
+
| A snapshot shows a session that has exited already | `connected`, then at once `ended exited` with its `exit`, `retry: false` (a resume onto such a session gets its `EXITED`, the next row, instead) |
|
|
79
|
+
| The session's program exits (`EXITED`) | `ended exited` with `exit`, `retry: false` |
|
|
80
|
+
| `CLOSED removed` | `ended removed`, `retry: false`; the worker forgets the session |
|
|
81
|
+
| `CLOSED stopping` | `ended stopping`, `retry: true`; the connection's close follows, and the reconnect attaches the session again (as `restored`) |
|
|
82
|
+
| `OPEN` refused, or the attachment ended, with `no_such_session` or `engine_mismatch` | `ended` with that reason and code, `retry: false` |
|
|
83
|
+
| The attachment ended with `invalid_request` | `ended error`, `retry: false` |
|
|
84
|
+
| The attachment ended with another error (`too_slow`, a snapshot that failed, `internal`) | `ended error` with the code, `retry: true`; the worker attaches again after a backoff |
|
|
85
|
+
| The stream lost its place (an `OUTPUT` at an unexpected offset) | `ended error`, `retry: true`; the worker attaches again afresh |
|
|
86
|
+
| Any other fault in the attachment's frames | `ended error`, `retry: false` |
|
|
87
|
+
|
|
88
|
+
Nothing fires for:
|
|
89
|
+
|
|
90
|
+
- the errors that keep an attachment (`read_only`, `not_running`, `input_overflow`);
|
|
91
|
+
- a gap, until its new screen fires `connected`;
|
|
92
|
+
- `attach` or `create` before their screen;
|
|
93
|
+
- `dispose`.
|
|
94
|
+
|
|
95
|
+
`reason: "closed"` is the connection's close. The worker detaches without waiting for the server's `CLOSED closed`, so that frame never shows up as a link change.
|
|
96
|
+
|
|
97
|
+
"The server ended an answered connection with an error, and nothing will reconnect" is `ended` with `reason: "error"` and `retry: false` after a `connected`, with no `connecting` after it. An attached session is the latest `connected` with a `session`.
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type Engine, type Terminal } from "../engine/index.ts";
|
|
2
|
+
import { type Closed, type ErrorMessage, type Exit, type Frame, type Gap, type Resume, type Session, type Welcome } from "../proto/index.ts";
|
|
3
|
+
/** What a client does in response to frames. */
|
|
4
|
+
export type Action =
|
|
5
|
+
/** The screen is renderable from the local engine. The session may
|
|
6
|
+
* already have exited: see `Attachment.session`. */
|
|
7
|
+
{
|
|
8
|
+
type: "screen";
|
|
9
|
+
} | {
|
|
10
|
+
type: "output";
|
|
11
|
+
bytes: Uint8Array;
|
|
12
|
+
} | {
|
|
13
|
+
type: "resized";
|
|
14
|
+
cols: number;
|
|
15
|
+
rows: number;
|
|
16
|
+
} | {
|
|
17
|
+
type: "exited";
|
|
18
|
+
exit: Exit;
|
|
19
|
+
}
|
|
20
|
+
/** Bytes were lost; the terminal was discarded and a new bootstrap follows. */
|
|
21
|
+
| {
|
|
22
|
+
type: "gap";
|
|
23
|
+
gap: Gap;
|
|
24
|
+
} | {
|
|
25
|
+
type: "closed";
|
|
26
|
+
reason: Closed["reason"];
|
|
27
|
+
}
|
|
28
|
+
/** `read_only`, `not_running` and `input_overflow` keep the attachment;
|
|
29
|
+
* any other code ended it. */
|
|
30
|
+
| {
|
|
31
|
+
type: "error";
|
|
32
|
+
error: ErrorMessage;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* The attachment state machine: frames in, actions out. It keeps the
|
|
36
|
+
* local engine current and tracks the cursor for resuming.
|
|
37
|
+
*/
|
|
38
|
+
export declare class Attachment {
|
|
39
|
+
readonly engine: Engine;
|
|
40
|
+
private state;
|
|
41
|
+
private cursor;
|
|
42
|
+
/** The server the cursor was obtained from, and its limit. */
|
|
43
|
+
private incarnation;
|
|
44
|
+
private continuation;
|
|
45
|
+
/** The server of the connection the next `ATTACHED` arrives on. */
|
|
46
|
+
private next;
|
|
47
|
+
session: Session | undefined;
|
|
48
|
+
constructor(engine: Engine);
|
|
49
|
+
/** Records the server an `OPEN` is about to go to; its incarnation
|
|
50
|
+
* becomes the cursor's once that `ATTACHED` arrives. */
|
|
51
|
+
openOn(welcome: Welcome): void;
|
|
52
|
+
/** The cursor to resume from, once the terminal reflects the stream. */
|
|
53
|
+
resume(): Resume | undefined;
|
|
54
|
+
ended(): boolean;
|
|
55
|
+
/** The server ended the attachment (`CLOSED` or a fatal `ERROR`). */
|
|
56
|
+
closed(): boolean;
|
|
57
|
+
/** The local engine, once `READY` has been decoded. */
|
|
58
|
+
term(): Terminal | undefined;
|
|
59
|
+
handle(frame: Frame): Action[];
|
|
60
|
+
/** Releases the engine objects. */
|
|
61
|
+
free(): void;
|
|
62
|
+
private json;
|
|
63
|
+
private step;
|
|
64
|
+
/** A stream frame at the cursor. */
|
|
65
|
+
private stream;
|
|
66
|
+
private checkMarker;
|
|
67
|
+
/** Keeps the terminal for display, if there is one. */
|
|
68
|
+
private end;
|
|
69
|
+
/** Frees whatever engine objects the state holds. */
|
|
70
|
+
private discard;
|
|
71
|
+
}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { Decoder } from "../engine/index.js";
|
|
2
|
+
import { FrameType, parseOutput, } from "../proto/index.js";
|
|
3
|
+
import { ClientError } from "./connection.js";
|
|
4
|
+
const KEEPS_ATTACHMENT = new Set(["read_only", "not_running", "input_overflow"]);
|
|
5
|
+
/**
|
|
6
|
+
* The attachment state machine: frames in, actions out. It keeps the
|
|
7
|
+
* local engine current and tracks the cursor for resuming.
|
|
8
|
+
*/
|
|
9
|
+
export class Attachment {
|
|
10
|
+
engine;
|
|
11
|
+
state = { kind: "opening" };
|
|
12
|
+
cursor = { offset: 0, marker: 0 };
|
|
13
|
+
/** The server the cursor was obtained from, and its limit. */
|
|
14
|
+
incarnation = "";
|
|
15
|
+
continuation = 0;
|
|
16
|
+
/** The server of the connection the next `ATTACHED` arrives on. */
|
|
17
|
+
next;
|
|
18
|
+
session;
|
|
19
|
+
constructor(engine) {
|
|
20
|
+
this.engine = engine;
|
|
21
|
+
}
|
|
22
|
+
/** Records the server an `OPEN` is about to go to; its incarnation
|
|
23
|
+
* becomes the cursor's once that `ATTACHED` arrives. */
|
|
24
|
+
openOn(welcome) {
|
|
25
|
+
this.next = { incarnation: welcome.incarnation, continuation: welcome.limits.continuation };
|
|
26
|
+
}
|
|
27
|
+
/** The cursor to resume from, once the terminal reflects the stream. */
|
|
28
|
+
resume() {
|
|
29
|
+
return this.state.kind === "streaming"
|
|
30
|
+
? { incarnation: this.incarnation, offset: this.cursor.offset, marker: this.cursor.marker }
|
|
31
|
+
: undefined;
|
|
32
|
+
}
|
|
33
|
+
ended() {
|
|
34
|
+
return this.state.kind === "ended";
|
|
35
|
+
}
|
|
36
|
+
/** The server ended the attachment (`CLOSED` or a fatal `ERROR`). */
|
|
37
|
+
closed() {
|
|
38
|
+
return this.state.kind === "ended" && this.state.closed;
|
|
39
|
+
}
|
|
40
|
+
/** The local engine, once `READY` has been decoded. */
|
|
41
|
+
term() {
|
|
42
|
+
switch (this.state.kind) {
|
|
43
|
+
case "bootstrapping":
|
|
44
|
+
return this.state.decoder.terminal();
|
|
45
|
+
case "streaming":
|
|
46
|
+
case "ended":
|
|
47
|
+
return this.state.term;
|
|
48
|
+
default:
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
handle(frame) {
|
|
53
|
+
const actions = [];
|
|
54
|
+
this.step(frame, actions);
|
|
55
|
+
return actions;
|
|
56
|
+
}
|
|
57
|
+
/** Releases the engine objects. */
|
|
58
|
+
free() {
|
|
59
|
+
this.discard();
|
|
60
|
+
this.state = { kind: "ended", term: undefined, closed: true };
|
|
61
|
+
}
|
|
62
|
+
json(frame) {
|
|
63
|
+
try {
|
|
64
|
+
return JSON.parse(new TextDecoder().decode(frame.payload));
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
throw ClientError.protocol(`malformed ${frame.header.type}: ${err}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
step(frame, actions) {
|
|
71
|
+
const type = frame.header.type;
|
|
72
|
+
switch (type) {
|
|
73
|
+
case FrameType.Attached: {
|
|
74
|
+
const attached = this.json(frame);
|
|
75
|
+
if (this.next) {
|
|
76
|
+
this.incarnation = this.next.incarnation;
|
|
77
|
+
this.continuation = this.next.continuation;
|
|
78
|
+
this.next = undefined;
|
|
79
|
+
}
|
|
80
|
+
this.cursor = { offset: attached.offset, marker: attached.marker };
|
|
81
|
+
this.session = attached.session;
|
|
82
|
+
if (attached.mode === "snapshot") {
|
|
83
|
+
if (attached.snapshot_bytes === undefined)
|
|
84
|
+
throw ClientError.protocol("ATTACHED without snapshot_bytes");
|
|
85
|
+
this.discard();
|
|
86
|
+
const decoder = new Decoder(this.engine, attached.snapshot_bytes);
|
|
87
|
+
try {
|
|
88
|
+
decoder.setMaxContinuationBytes(this.continuation);
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
decoder.free();
|
|
92
|
+
throw err;
|
|
93
|
+
}
|
|
94
|
+
this.state = { kind: "bootstrapping", decoder, held: [] };
|
|
95
|
+
}
|
|
96
|
+
else if (this.state.kind !== "streaming") {
|
|
97
|
+
throw ClientError.protocol("resume without a terminal");
|
|
98
|
+
}
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
case FrameType.Snapshot: {
|
|
102
|
+
if (this.state.kind !== "bootstrapping")
|
|
103
|
+
throw ClientError.protocol("SNAPSHOT outside a bootstrap");
|
|
104
|
+
const { decoder } = this.state;
|
|
105
|
+
decoder.push(frame.payload);
|
|
106
|
+
if (!decoder.terminal()) {
|
|
107
|
+
if (decoder.readyAvailable()) {
|
|
108
|
+
decoder.ready();
|
|
109
|
+
actions.push({ type: "screen" });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
while (decoder.stepAvailable() && decoder.nextPage()) { }
|
|
114
|
+
}
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
case FrameType.SnapshotReady:
|
|
118
|
+
if (this.state.kind !== "bootstrapping")
|
|
119
|
+
throw ClientError.protocol("SNAPSHOT_READY outside a bootstrap");
|
|
120
|
+
if (!this.state.decoder.terminal())
|
|
121
|
+
throw ClientError.protocol("SNAPSHOT_READY before READY");
|
|
122
|
+
break;
|
|
123
|
+
case FrameType.SnapshotEnd: {
|
|
124
|
+
if (this.state.kind !== "bootstrapping")
|
|
125
|
+
throw ClientError.protocol("SNAPSHOT_END outside a bootstrap");
|
|
126
|
+
const { decoder, held } = this.state;
|
|
127
|
+
if (!decoder.terminal())
|
|
128
|
+
throw ClientError.protocol("SNAPSHOT_END before READY");
|
|
129
|
+
while (decoder.stepAvailable() && decoder.nextPage()) { }
|
|
130
|
+
if (!decoder.finished())
|
|
131
|
+
throw ClientError.protocol("SNAPSHOT_END before FINISH");
|
|
132
|
+
const term = decoder.release();
|
|
133
|
+
this.state = { kind: "streaming", term };
|
|
134
|
+
for (const f of held)
|
|
135
|
+
this.step(f, actions);
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
case FrameType.Output:
|
|
139
|
+
case FrameType.Resized:
|
|
140
|
+
case FrameType.Exited:
|
|
141
|
+
if (this.state.kind === "bootstrapping" && (this.state.held.length > 0 || type !== FrameType.Output)) {
|
|
142
|
+
this.state.held.push(frame);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (this.state.kind !== "bootstrapping" && this.state.kind !== "streaming") {
|
|
146
|
+
throw ClientError.protocol(`${type} outside the stream`);
|
|
147
|
+
}
|
|
148
|
+
this.stream(frame, actions);
|
|
149
|
+
break;
|
|
150
|
+
case FrameType.Gap: {
|
|
151
|
+
const gap = this.json(frame);
|
|
152
|
+
this.discard();
|
|
153
|
+
actions.push({ type: "gap", gap });
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
case FrameType.Closed: {
|
|
157
|
+
const closed = this.json(frame);
|
|
158
|
+
this.end(true);
|
|
159
|
+
actions.push({ type: "closed", reason: closed.reason });
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
case FrameType.Error: {
|
|
163
|
+
const error = this.json(frame);
|
|
164
|
+
if (!KEEPS_ATTACHMENT.has(error.code))
|
|
165
|
+
this.end(true);
|
|
166
|
+
actions.push({ type: "error", error });
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
default:
|
|
170
|
+
throw ClientError.protocol(`${type} on an attachment`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** A stream frame at the cursor. */
|
|
174
|
+
stream(frame, actions) {
|
|
175
|
+
switch (frame.header.type) {
|
|
176
|
+
case FrameType.Output: {
|
|
177
|
+
const output = parseOutput(frame.payload);
|
|
178
|
+
if (!output)
|
|
179
|
+
throw ClientError.protocol("short OUTPUT");
|
|
180
|
+
if (output.offset !== this.cursor.offset) {
|
|
181
|
+
throw ClientError.desync(`OUTPUT at ${output.offset}, expected ${this.cursor.offset}`);
|
|
182
|
+
}
|
|
183
|
+
const term = this.term();
|
|
184
|
+
if (!term)
|
|
185
|
+
throw ClientError.protocol("OUTPUT before the terminal exists");
|
|
186
|
+
term.write(output.bytes);
|
|
187
|
+
this.cursor.offset += output.bytes.length;
|
|
188
|
+
actions.push({ type: "output", bytes: output.bytes });
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
case FrameType.Resized: {
|
|
192
|
+
const resized = this.json(frame);
|
|
193
|
+
this.checkMarker(resized.seq, resized.offset);
|
|
194
|
+
this.term()?.resize(resized.cols, resized.rows);
|
|
195
|
+
actions.push({ type: "resized", cols: resized.cols, rows: resized.rows });
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
case FrameType.Exited: {
|
|
199
|
+
const exited = this.json(frame);
|
|
200
|
+
this.checkMarker(exited.seq, exited.offset);
|
|
201
|
+
this.end(false);
|
|
202
|
+
actions.push({ type: "exited", exit: exited.exit });
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
checkMarker(seq, offset) {
|
|
208
|
+
if (offset !== this.cursor.offset || seq !== this.cursor.marker + 1) {
|
|
209
|
+
throw ClientError.desync(`marker ${seq} at ${offset}, expected ${this.cursor.marker + 1} at ${this.cursor.offset}`);
|
|
210
|
+
}
|
|
211
|
+
this.cursor.marker = seq;
|
|
212
|
+
}
|
|
213
|
+
/** Keeps the terminal for display, if there is one. */
|
|
214
|
+
end(closed) {
|
|
215
|
+
let term;
|
|
216
|
+
switch (this.state.kind) {
|
|
217
|
+
case "bootstrapping":
|
|
218
|
+
term = this.state.decoder.release();
|
|
219
|
+
break;
|
|
220
|
+
case "streaming":
|
|
221
|
+
case "ended":
|
|
222
|
+
term = this.state.term;
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
this.state = { kind: "ended", term, closed };
|
|
226
|
+
}
|
|
227
|
+
/** Frees whatever engine objects the state holds. */
|
|
228
|
+
discard() {
|
|
229
|
+
switch (this.state.kind) {
|
|
230
|
+
case "bootstrapping":
|
|
231
|
+
this.state.decoder.free();
|
|
232
|
+
break;
|
|
233
|
+
case "streaming":
|
|
234
|
+
case "ended":
|
|
235
|
+
this.state.term?.free();
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
this.state = { kind: "opening" };
|
|
239
|
+
}
|
|
240
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type ErrorMessage, type Event, type Frame, FrameType, type Hello, type Welcome } from "../proto/index.ts";
|
|
2
|
+
/** A message-oriented byte transport: one frame per message, as over WebSocket. */
|
|
3
|
+
export interface Transport {
|
|
4
|
+
send(bytes: Uint8Array): void;
|
|
5
|
+
close(): void;
|
|
6
|
+
onMessage: ((bytes: Uint8Array) => void) | undefined;
|
|
7
|
+
onClose: (() => void) | undefined;
|
|
8
|
+
}
|
|
9
|
+
/** `desync`: a stream frame did not continue from the cursor; a fresh
|
|
10
|
+
* OPEN recovers it, where any other fault would only repeat. */
|
|
11
|
+
export type ErrorKind = "protocol" | "desync" | "server" | "closed";
|
|
12
|
+
export declare class ClientError extends Error {
|
|
13
|
+
readonly kind: ErrorKind;
|
|
14
|
+
readonly server?: ErrorMessage | undefined;
|
|
15
|
+
constructor(kind: ErrorKind, message: string, server?: ErrorMessage | undefined);
|
|
16
|
+
static protocol(message: string): ClientError;
|
|
17
|
+
static desync(message: string): ClientError;
|
|
18
|
+
static fromServer(error: ErrorMessage): ClientError;
|
|
19
|
+
}
|
|
20
|
+
export declare function hello(name: string, engine?: string): Hello;
|
|
21
|
+
/**
|
|
22
|
+
* One protocol connection: the handshake, requests answered by id, events,
|
|
23
|
+
* and attachment channels routed to their receivers.
|
|
24
|
+
*/
|
|
25
|
+
export declare class Connection {
|
|
26
|
+
private readonly transport;
|
|
27
|
+
readonly welcome: Welcome;
|
|
28
|
+
private nextId;
|
|
29
|
+
private readonly pending;
|
|
30
|
+
private readonly channels;
|
|
31
|
+
private ended;
|
|
32
|
+
/** Session events, once subscribed. */
|
|
33
|
+
onEvent: ((event: Event) => void) | undefined;
|
|
34
|
+
/** The connection ended: the transport closed, or an error without id. */
|
|
35
|
+
onEnd: ((error: ClientError) => void) | undefined;
|
|
36
|
+
private constructor();
|
|
37
|
+
/** Sends `HELLO` and resolves at `WELCOME`; rejects on an `ERROR` or close. */
|
|
38
|
+
static connect(transport: Transport, hello: Hello): Promise<Connection>;
|
|
39
|
+
send(type: FrameType, channel: number, payload: Uint8Array): void;
|
|
40
|
+
sendJson(type: FrameType, channel: number, body: unknown): void;
|
|
41
|
+
/** A request; the reply's `result`, or a rejection with the server's error. */
|
|
42
|
+
call<R>(method: string, params: unknown): Promise<R>;
|
|
43
|
+
/** Opens `channel`; its frames go to `receive` until `release` or the
|
|
44
|
+
* connection's end. */
|
|
45
|
+
open(channel: number, open: unknown, receive: (frame: Frame) => void): void;
|
|
46
|
+
/** Stops routing `channel`; later frames on it are dropped. */
|
|
47
|
+
release(channel: number): void;
|
|
48
|
+
close(): void;
|
|
49
|
+
private route;
|
|
50
|
+
private end;
|
|
51
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { FrameError, FrameType, encodeFrame, parseFrame, VERSION, } from "../proto/index.js";
|
|
2
|
+
import pkg from "../../package.json" with { type: "json" };
|
|
3
|
+
export class ClientError extends Error {
|
|
4
|
+
kind;
|
|
5
|
+
server;
|
|
6
|
+
constructor(kind, message, server) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.kind = kind;
|
|
9
|
+
this.server = server;
|
|
10
|
+
}
|
|
11
|
+
static protocol(message) {
|
|
12
|
+
return new ClientError("protocol", message);
|
|
13
|
+
}
|
|
14
|
+
static desync(message) {
|
|
15
|
+
return new ClientError("desync", message);
|
|
16
|
+
}
|
|
17
|
+
static fromServer(error) {
|
|
18
|
+
return new ClientError("server", `${error.code}: ${error.message}`, error);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** Frames before `WELCOME` tells us the real limit: the protocol's
|
|
22
|
+
* default `max_frame`, as the Rust client. */
|
|
23
|
+
const HELLO_MAX_FRAME = 1024 * 1024;
|
|
24
|
+
export function hello(name, engine) {
|
|
25
|
+
const h = { version: VERSION, client: { name, version: pkg.version }, capabilities: [] };
|
|
26
|
+
if (engine !== undefined)
|
|
27
|
+
h.engine = engine;
|
|
28
|
+
return h;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* One protocol connection: the handshake, requests answered by id, events,
|
|
32
|
+
* and attachment channels routed to their receivers.
|
|
33
|
+
*/
|
|
34
|
+
export class Connection {
|
|
35
|
+
transport;
|
|
36
|
+
welcome;
|
|
37
|
+
nextId = 1;
|
|
38
|
+
pending = new Map();
|
|
39
|
+
channels = new Map();
|
|
40
|
+
ended;
|
|
41
|
+
/** Session events, once subscribed. */
|
|
42
|
+
onEvent;
|
|
43
|
+
/** The connection ended: the transport closed, or an error without id. */
|
|
44
|
+
onEnd;
|
|
45
|
+
constructor(transport, welcome) {
|
|
46
|
+
this.transport = transport;
|
|
47
|
+
this.welcome = welcome;
|
|
48
|
+
transport.onMessage = (bytes) => this.route(bytes);
|
|
49
|
+
transport.onClose = () => this.end(new ClientError("closed", "connection closed"));
|
|
50
|
+
}
|
|
51
|
+
/** Sends `HELLO` and resolves at `WELCOME`; rejects on an `ERROR` or close. */
|
|
52
|
+
static connect(transport, hello) {
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
transport.onMessage = (bytes) => {
|
|
55
|
+
try {
|
|
56
|
+
const frame = parseFrame(bytes, HELLO_MAX_FRAME);
|
|
57
|
+
const body = JSON.parse(new TextDecoder().decode(frame.payload));
|
|
58
|
+
if (frame.header.type === FrameType.Welcome) {
|
|
59
|
+
resolve(new Connection(transport, body));
|
|
60
|
+
}
|
|
61
|
+
else if (frame.header.type === FrameType.Error) {
|
|
62
|
+
reject(ClientError.fromServer(body));
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
reject(ClientError.protocol(`${frame.header.type} before WELCOME`));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
reject(ClientError.protocol(String(err)));
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
transport.onClose = () => reject(new ClientError("closed", "closed before WELCOME"));
|
|
73
|
+
transport.send(encodeFrame(FrameType.Hello, 0, new TextEncoder().encode(JSON.stringify(hello))));
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
send(type, channel, payload) {
|
|
77
|
+
if (this.ended)
|
|
78
|
+
throw this.ended;
|
|
79
|
+
this.transport.send(encodeFrame(type, channel, payload));
|
|
80
|
+
}
|
|
81
|
+
sendJson(type, channel, body) {
|
|
82
|
+
this.send(type, channel, new TextEncoder().encode(JSON.stringify(body)));
|
|
83
|
+
}
|
|
84
|
+
/** A request; the reply's `result`, or a rejection with the server's error. */
|
|
85
|
+
call(method, params) {
|
|
86
|
+
const id = this.nextId++;
|
|
87
|
+
return new Promise((resolve, reject) => {
|
|
88
|
+
this.pending.set(id, { resolve: resolve, reject });
|
|
89
|
+
try {
|
|
90
|
+
this.sendJson(FrameType.Request, 0, { id, method, params });
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
this.pending.delete(id);
|
|
94
|
+
reject(err);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/** Opens `channel`; its frames go to `receive` until `release` or the
|
|
99
|
+
* connection's end. */
|
|
100
|
+
open(channel, open, receive) {
|
|
101
|
+
if (this.ended)
|
|
102
|
+
throw this.ended;
|
|
103
|
+
this.channels.set(channel, receive);
|
|
104
|
+
this.sendJson(FrameType.Open, channel, open);
|
|
105
|
+
}
|
|
106
|
+
/** Stops routing `channel`; later frames on it are dropped. */
|
|
107
|
+
release(channel) {
|
|
108
|
+
this.channels.delete(channel);
|
|
109
|
+
}
|
|
110
|
+
close() {
|
|
111
|
+
this.transport.close();
|
|
112
|
+
}
|
|
113
|
+
route(bytes) {
|
|
114
|
+
let frame;
|
|
115
|
+
try {
|
|
116
|
+
frame = parseFrame(bytes, this.welcome.limits.max_frame);
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
this.end(ClientError.protocol(err instanceof FrameError ? err.message : String(err)));
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (frame.header.channel !== 0) {
|
|
123
|
+
this.channels.get(frame.header.channel)?.(frame);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
let body;
|
|
127
|
+
try {
|
|
128
|
+
body = JSON.parse(new TextDecoder().decode(frame.payload));
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
this.end(ClientError.protocol(`malformed ${frame.header.type}: ${err}`));
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
switch (frame.header.type) {
|
|
135
|
+
case FrameType.Reply: {
|
|
136
|
+
// A reply or error for a request no longer waited for is dropped.
|
|
137
|
+
const reply = body;
|
|
138
|
+
this.pending.get(reply.id)?.resolve(reply.result);
|
|
139
|
+
this.pending.delete(reply.id);
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
case FrameType.Error: {
|
|
143
|
+
const error = body;
|
|
144
|
+
if (error.id === undefined) {
|
|
145
|
+
this.end(ClientError.fromServer(error));
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
this.pending.get(error.id)?.reject(ClientError.fromServer(error));
|
|
149
|
+
this.pending.delete(error.id);
|
|
150
|
+
}
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
case FrameType.Event:
|
|
154
|
+
this.onEvent?.(body);
|
|
155
|
+
break;
|
|
156
|
+
default:
|
|
157
|
+
this.end(ClientError.protocol(`${frame.header.type} on channel 0`));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
end(error) {
|
|
161
|
+
if (this.ended)
|
|
162
|
+
return;
|
|
163
|
+
this.ended = error;
|
|
164
|
+
for (const waiter of this.pending.values())
|
|
165
|
+
waiter.reject(error);
|
|
166
|
+
this.pending.clear();
|
|
167
|
+
this.channels.clear();
|
|
168
|
+
this.transport.close();
|
|
169
|
+
this.onEnd?.(error);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { Attachment } from "./attachment.ts";
|
|
2
|
+
export type { Action } from "./attachment.ts";
|
|
3
|
+
export { ClientError, Connection, hello } from "./connection.ts";
|
|
4
|
+
export type { ErrorKind, Transport } from "./connection.ts";
|
|
5
|
+
export { Osc52Scanner } from "./osc52.ts";
|
|
6
|
+
export type { ClipboardWrite } from "./osc52.ts";
|
|
7
|
+
export { mountTerminal } from "./mount.ts";
|
|
8
|
+
export type { Colors, CreateOptions, MountOptions, MountedTerminal } from "./mount.ts";
|
|
9
|
+
export type { Link, LinkEnd } from "./worker.ts";
|
|
10
|
+
export type { ErrorCode, Exit, OnExit, Scheme, Session } from "../proto/index.ts";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type KeyInput } from "../engine/index.ts";
|
|
2
|
+
/** The parts of a `KeyboardEvent` the mapping reads. */
|
|
3
|
+
export interface KeyEventLike {
|
|
4
|
+
type: string;
|
|
5
|
+
code: string;
|
|
6
|
+
key: string;
|
|
7
|
+
repeat: boolean;
|
|
8
|
+
shiftKey: boolean;
|
|
9
|
+
ctrlKey: boolean;
|
|
10
|
+
altKey: boolean;
|
|
11
|
+
metaKey: boolean;
|
|
12
|
+
isComposing: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** What the page knows about the keyboard: the layout's unmodified
|
|
15
|
+
* character per `code` when the browser exposes it, and the platform. */
|
|
16
|
+
export interface Keyboard {
|
|
17
|
+
layout: ReadonlyMap<string, string> | undefined;
|
|
18
|
+
mac: boolean;
|
|
19
|
+
}
|
|
20
|
+
/** Whether the browser keeps the key: its own shortcuts with the platform's
|
|
21
|
+
* command modifier, and Ctrl+Shift+V, the paste shortcut where Ctrl+V is
|
|
22
|
+
* the terminal's. */
|
|
23
|
+
export declare function browserKeeps(e: KeyEventLike): boolean;
|
|
24
|
+
export declare function keyInput(e: KeyEventLike, keyboard: Keyboard): KeyInput;
|