@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.
@@ -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,9 @@
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, MountOptions, MountedTerminal } from "./mount.ts";
9
+ export type { Scheme, Session } from "../proto/index.ts";
@@ -0,0 +1,4 @@
1
+ export { Attachment } from "./attachment.js";
2
+ export { ClientError, Connection, hello } from "./connection.js";
3
+ export { Osc52Scanner } from "./osc52.js";
4
+ export { mountTerminal } from "./mount.js";
@@ -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;
@@ -0,0 +1,32 @@
1
+ import { isText } from "../engine/index.js";
2
+ /** Whether the browser keeps the key: its own shortcuts with the platform's
3
+ * command modifier, and Ctrl+Shift+V, the paste shortcut where Ctrl+V is
4
+ * the terminal's. */
5
+ export function browserKeeps(e) {
6
+ return e.metaKey || (e.ctrlKey && e.shiftKey && e.code === "KeyV");
7
+ }
8
+ export function keyInput(e, keyboard) {
9
+ const input = {
10
+ code: e.code,
11
+ key: e.key,
12
+ action: e.type === "keyup" ? "release" : e.repeat ? "repeat" : "press",
13
+ shift: e.shiftKey,
14
+ ctrl: e.ctrlKey,
15
+ alt: e.altKey,
16
+ meta: e.metaKey,
17
+ };
18
+ if (e.isComposing)
19
+ input.composing = true;
20
+ const unshifted = keyboard.layout?.get(e.code);
21
+ if (unshifted !== undefined)
22
+ input.unshifted = unshifted;
23
+ // Alt composed the text when it differs from the key's own character
24
+ // (Option on macOS, AltGr elsewhere). Without a layout to compare
25
+ // against, macOS Option composes and other platforms' Alt modifies.
26
+ if (e.altKey && isText(e.key)) {
27
+ const composed = unshifted !== undefined ? e.key.toLowerCase() !== unshifted.toLowerCase() : keyboard.mac;
28
+ if (composed)
29
+ input.altComposed = true;
30
+ }
31
+ return input;
32
+ }
@@ -0,0 +1,46 @@
1
+ /** The page side of a terminal: canvas, keyboard, paste and resize, with
2
+ * the engine in a worker running `runWorker`. */
3
+ import type { Scheme, Session } from "../proto/index.ts";
4
+ /** The terminal's default colours as CSS colours (hex or `rgb()`). */
5
+ export interface Colors {
6
+ fg: string;
7
+ bg: string;
8
+ cursor: string;
9
+ }
10
+ export interface MountOptions {
11
+ /** A module worker whose script calls `runWorker(self)`; the terminal
12
+ * owns it from here. */
13
+ worker: Worker;
14
+ /** Drawn on; takes keys and paste while focused. */
15
+ canvas: HTMLCanvasElement;
16
+ /** The element whose size sets the grid. */
17
+ container: HTMLElement;
18
+ /** ghostty-vt.wasm, resolved against the page. */
19
+ wasm: string;
20
+ /** The server's WebSocket carrier, `wss://<node>:<port>/`. */
21
+ url: string;
22
+ /** The session to attach at once. */
23
+ session?: string | undefined;
24
+ /** A CSS font shorthand with its size in px. */
25
+ font: string;
26
+ /** Read again on every report. */
27
+ colors: () => Colors;
28
+ /** The colour scheme reported to the session; the page's preferred
29
+ * scheme by default. */
30
+ scheme?: () => Scheme;
31
+ onStatus?: (text: string) => void;
32
+ onSessions?: (sessions: Session[], current: string | undefined) => void;
33
+ }
34
+ export interface MountedTerminal {
35
+ /** Detaches the current session and attaches `session`. */
36
+ attach(session: string): void;
37
+ /** Creates a session at the current size and attaches it. */
38
+ create(): void;
39
+ /** Reports the container's size, the colours and the scheme again. */
40
+ report(): void;
41
+ paste(text: string): void;
42
+ /** Removes the listeners and terminates the worker, which ends its
43
+ * connection. */
44
+ dispose(): void;
45
+ }
46
+ export declare function mountTerminal(options: MountOptions): MountedTerminal;