@gjermundgaraba/clankermux 0.5.0 → 0.7.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.
Files changed (40) hide show
  1. package/README.md +1 -1
  2. package/dist/client/attachment.d.ts +9 -29
  3. package/dist/client/attachment.js +28 -41
  4. package/dist/client/client.d.ts +0 -2
  5. package/dist/client/client.js +6 -11
  6. package/dist/client/connection.d.ts +1 -19
  7. package/dist/client/connection.js +0 -17
  8. package/dist/client/copy.d.ts +0 -14
  9. package/dist/client/copy.js +4 -25
  10. package/dist/client/keyboard.d.ts +1 -27
  11. package/dist/client/keyboard.js +18 -34
  12. package/dist/client/link.d.ts +143 -0
  13. package/dist/client/link.js +205 -0
  14. package/dist/client/listeners.d.ts +3 -1
  15. package/dist/client/listeners.js +20 -4
  16. package/dist/client/mount.d.ts +0 -5
  17. package/dist/client/mount.js +20 -72
  18. package/dist/client/osc52.d.ts +1 -4
  19. package/dist/client/osc52.js +20 -17
  20. package/dist/client/profile.d.ts +0 -6
  21. package/dist/client/profile.js +0 -6
  22. package/dist/client/worker.d.ts +3 -44
  23. package/dist/client/worker.js +185 -332
  24. package/dist/engine/decoder.d.ts +0 -20
  25. package/dist/engine/decoder.js +8 -27
  26. package/dist/engine/index.d.ts +2 -49
  27. package/dist/engine/index.js +7 -48
  28. package/dist/engine/keys.d.ts +0 -7
  29. package/dist/engine/keys.js +9 -15
  30. package/dist/engine/render.d.ts +7 -18
  31. package/dist/engine/render.js +32 -32
  32. package/dist/engine/selection.d.ts +0 -17
  33. package/dist/engine/selection.js +8 -30
  34. package/dist/proto/frame.d.ts +2 -23
  35. package/dist/proto/frame.js +1 -21
  36. package/dist/proto/messages.d.ts +49 -9
  37. package/dist/proto/messages.js +42 -1
  38. package/dist/renderer/index.d.ts +3 -4
  39. package/dist/renderer/index.js +38 -42
  40. package/package.json +5 -6
package/README.md CHANGED
@@ -7,7 +7,7 @@ The browser client of [clankermux](https://github.com/gjermundgaraba/clankermux)
7
7
  | `.` | `connect`, `Client`, the `Terminal` it mounts, their types, `ClientError` |
8
8
  | `./worker` | `runWorker`, for the worker module; no DOM types |
9
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; `Methods` maps each method to its params and result types, and `Catalog` is what `describe()` answers: the server's methods, events, frames and error codes with their JSON Schemas |
10
+ | `./proto`, `./renderer` | the protocol messages and the canvas renderer; `Methods` maps each method to its params and result types, and `Catalog` is what `describe()` answers: the server's methods, events, frames and error codes with their JSON Schemas |
11
11
 
12
12
  ```ts
13
13
  // worker.ts, started as a module worker
@@ -1,11 +1,9 @@
1
1
  import { type Engine, type Terminal } from "../engine/index.ts";
2
2
  import { type Closed, type ErrorMessage, type Exit, type Frame, 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
- {
3
+ export type Action = {
8
4
  type: "screen";
5
+ } | {
6
+ type: "resumed";
9
7
  } | {
10
8
  type: "output";
11
9
  bytes: Uint8Array;
@@ -16,56 +14,38 @@ export type Action =
16
14
  } | {
17
15
  type: "exited";
18
16
  exit: Exit;
19
- }
20
- /** Bytes were lost; the terminal was discarded and a new bootstrap follows. */
21
- | {
17
+ } | {
22
18
  type: "gap";
23
19
  } | {
24
20
  type: "closed";
25
21
  reason: Closed["reason"];
26
22
  message?: string;
27
- }
28
- /** `read_only`, `not_running` and `input_overflow` keep the attachment;
29
- * any other code ended it. */
30
- | {
23
+ } | {
31
24
  type: "error";
32
25
  error: ErrorMessage;
33
26
  };
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
27
  export declare class Attachment {
39
28
  readonly engine: Engine;
40
29
  private state;
41
30
  private cursor;
42
- /** The server the cursor was obtained from, and its limit. */
43
31
  private incarnation;
44
32
  private continuation;
45
- /** The server of the connection the next `ATTACHED` arrives on. */
46
- private next;
47
33
  session: Session | undefined;
48
34
  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. */
35
+ /** Readies an `OPEN` on the connection `welcome` began: the cursor to
36
+ * resume from, when there is one to keep. */
37
+ open(welcome: Welcome): Resume | undefined;
53
38
  resume(): Resume | undefined;
54
39
  ended(): boolean;
55
- /** The server ended the attachment (`CLOSED` or a fatal `ERROR`). */
56
40
  closed(): boolean;
57
- /** The local engine, once `READY` has been decoded. */
58
41
  term(): Terminal | undefined;
59
42
  handle(frame: Frame): Action[];
60
- /** Releases the engine objects. */
61
43
  free(): void;
62
44
  private json;
45
+ private assertWellFormed;
63
46
  private step;
64
- /** A stream frame at the cursor. */
65
47
  private stream;
66
48
  private checkMarker;
67
- /** Keeps the terminal for display, if there is one. */
68
49
  private end;
69
- /** Frees whatever engine objects the state holds. */
70
50
  private discard;
71
51
  }
@@ -2,44 +2,37 @@ import { Decoder } from "../engine/index.js";
2
2
  import { FrameType, parseOutput, } from "../proto/index.js";
3
3
  import { ClientError } from "./connection.js";
4
4
  const KEEPS_ATTACHMENT = new Set(["read_only", "not_running", "input_overflow"]);
5
- /** Payload bytes plus a per-frame charge to bound tiny held frames too. */
6
5
  const MAX_HELD_BYTES = 4 * 1024 * 1024;
7
- /**
8
- * The attachment state machine: frames in, actions out. It keeps the
9
- * local engine current and tracks the cursor for resuming.
10
- */
6
+ const HELD_FRAME_OVERHEAD_BYTES = 64;
7
+ function mustHoldUntilSnapshotEnd(held, type) {
8
+ return held.length > 0 || type !== FrameType.Output;
9
+ }
11
10
  export class Attachment {
12
11
  engine;
13
12
  state = { kind: "opening" };
14
- cursor = { offset: 0, marker: 0 };
15
- /** The server the cursor was obtained from, and its limit. */
13
+ cursor = { incarnation: "", offset: 0, marker: 0 };
16
14
  incarnation = "";
17
15
  continuation = 0;
18
- /** The server of the connection the next `ATTACHED` arrives on. */
19
- next;
20
16
  session;
21
17
  constructor(engine) {
22
18
  this.engine = engine;
23
19
  }
24
- /** Records the server an `OPEN` is about to go to; its incarnation
25
- * becomes the cursor's once that `ATTACHED` arrives. */
26
- openOn(welcome) {
27
- this.next = { incarnation: welcome.incarnation, continuation: welcome.limits.continuation };
20
+ /** Readies an `OPEN` on the connection `welcome` began: the cursor to
21
+ * resume from, when there is one to keep. */
22
+ open(welcome) {
23
+ this.incarnation = welcome.incarnation;
24
+ this.continuation = welcome.limits.continuation;
25
+ return this.resume();
28
26
  }
29
- /** The cursor to resume from, once the terminal reflects the stream. */
30
27
  resume() {
31
- return this.state.kind === "streaming"
32
- ? { incarnation: this.incarnation, offset: this.cursor.offset, marker: this.cursor.marker }
33
- : undefined;
28
+ return this.state.kind === "streaming" ? { ...this.cursor } : undefined;
34
29
  }
35
30
  ended() {
36
31
  return this.state.kind === "ended";
37
32
  }
38
- /** The server ended the attachment (`CLOSED` or a fatal `ERROR`). */
39
33
  closed() {
40
- return this.state.kind === "ended" && this.state.closed;
34
+ return this.state.kind === "ended" && this.state.serverClosed;
41
35
  }
42
- /** The local engine, once `READY` has been decoded. */
43
36
  term() {
44
37
  switch (this.state.kind) {
45
38
  case "bootstrapping":
@@ -56,10 +49,9 @@ export class Attachment {
56
49
  this.step(frame, actions);
57
50
  return actions;
58
51
  }
59
- /** Releases the engine objects. */
60
52
  free() {
61
53
  this.discard();
62
- this.state = { kind: "ended", term: undefined, closed: true };
54
+ this.state = { kind: "ended", term: undefined, serverClosed: true };
63
55
  }
64
56
  json(frame) {
65
57
  try {
@@ -69,21 +61,17 @@ export class Attachment {
69
61
  throw ClientError.protocol(`malformed ${frame.header.type}: ${err}`);
70
62
  }
71
63
  }
64
+ assertWellFormed(frame) {
65
+ this.json(frame);
66
+ }
72
67
  step(frame, actions) {
73
68
  const type = frame.header.type;
74
69
  switch (type) {
75
70
  case FrameType.Attached: {
76
71
  const attached = this.json(frame);
77
- if (this.next) {
78
- this.incarnation = this.next.incarnation;
79
- this.continuation = this.next.continuation;
80
- this.next = undefined;
81
- }
82
- this.cursor = { offset: attached.offset, marker: attached.marker };
72
+ this.cursor = { incarnation: this.incarnation, offset: attached.offset, marker: attached.marker };
83
73
  this.session = attached.session;
84
- if (attached.mode === "snapshot") {
85
- if (attached.snapshot_bytes === undefined)
86
- throw ClientError.protocol("ATTACHED without snapshot_bytes");
74
+ if (attached.snapshot_bytes !== undefined) {
87
75
  this.discard();
88
76
  const decoder = new Decoder(this.engine, attached.snapshot_bytes);
89
77
  try {
@@ -95,7 +83,10 @@ export class Attachment {
95
83
  }
96
84
  this.state = { kind: "bootstrapping", decoder, held: [], heldBytes: 0 };
97
85
  }
98
- else if (this.state.kind !== "streaming") {
86
+ else if (this.state.kind === "streaming") {
87
+ actions.push({ type: "resumed" });
88
+ }
89
+ else {
99
90
  throw ClientError.protocol("resume without a terminal");
100
91
  }
101
92
  break;
@@ -140,8 +131,8 @@ export class Attachment {
140
131
  case FrameType.Output:
141
132
  case FrameType.Resized:
142
133
  case FrameType.Exited:
143
- if (this.state.kind === "bootstrapping" && (this.state.held.length > 0 || type !== FrameType.Output)) {
144
- const bytes = frame.payload.byteLength + 64;
134
+ if (this.state.kind === "bootstrapping" && mustHoldUntilSnapshotEnd(this.state.held, type)) {
135
+ const bytes = frame.payload.byteLength + HELD_FRAME_OVERHEAD_BYTES;
145
136
  if (bytes > MAX_HELD_BYTES - this.state.heldBytes) {
146
137
  this.end(false);
147
138
  throw ClientError.desync("bootstrap stream buffer full; fresh OPEN required");
@@ -156,8 +147,7 @@ export class Attachment {
156
147
  this.stream(frame, actions);
157
148
  break;
158
149
  case FrameType.Gap:
159
- // Nothing in it is read, but a malformed one is still refused.
160
- this.json(frame);
150
+ this.assertWellFormed(frame);
161
151
  this.discard();
162
152
  actions.push({ type: "gap" });
163
153
  break;
@@ -178,7 +168,6 @@ export class Attachment {
178
168
  throw ClientError.protocol(`${type} on an attachment`);
179
169
  }
180
170
  }
181
- /** A stream frame at the cursor. */
182
171
  stream(frame, actions) {
183
172
  switch (frame.header.type) {
184
173
  case FrameType.Output: {
@@ -218,8 +207,7 @@ export class Attachment {
218
207
  }
219
208
  this.cursor.marker = seq;
220
209
  }
221
- /** Keeps the terminal for display, if there is one. */
222
- end(closed) {
210
+ end(serverClosed) {
223
211
  let term;
224
212
  switch (this.state.kind) {
225
213
  case "bootstrapping":
@@ -230,9 +218,8 @@ export class Attachment {
230
218
  term = this.state.term;
231
219
  break;
232
220
  }
233
- this.state = { kind: "ended", term, closed };
221
+ this.state = { kind: "ended", term, serverClosed };
234
222
  }
235
- /** Frees whatever engine objects the state holds. */
236
223
  discard() {
237
224
  switch (this.state.kind) {
238
225
  case "bootstrapping":
@@ -1,5 +1,3 @@
1
- /** The page side of a client: one worker, one connection, the control
2
- * surface, and the terminals mounted from it. */
3
1
  import type { Acked, Catalog, CreateParams, Description, Event, Page, Peer, Read, ReadParams, ResizeParams, SendParams, Sent, Session, StreamCursor, Tail, Until, Waited } from "../proto/index.ts";
4
2
  import { type MountOptions, type Terminal } from "./mount.ts";
5
3
  import type { Link, ToWorker } from "./worker.ts";
@@ -43,17 +43,14 @@ export class Client {
43
43
  /** @internal Use `connect`. */
44
44
  constructor(worker, started) {
45
45
  this.worker = worker;
46
- // A worker script that does not load posts nothing, so `connect`
47
- // would never settle. Once it runs, an error event is an exception in
48
- // one of its handlers, which costs that message only: the browser
49
- // reports it, and the client goes on.
50
- worker.onerror = (e) => {
46
+ const failStartIfWorkerDidNotLoad = (e) => {
51
47
  if (this.started)
52
48
  return;
53
49
  e.preventDefault();
54
50
  started(new Error(`the worker failed: ${e.message || "it did not load"}`));
55
51
  this.close();
56
52
  };
53
+ worker.onerror = failStartIfWorkerDidNotLoad;
57
54
  worker.onmessage = (e) => {
58
55
  const message = e.data;
59
56
  switch (message.type) {
@@ -104,16 +101,14 @@ export class Client {
104
101
  return Promise.reject(new ClientError("closed", "closed"));
105
102
  const id = this.nextId++;
106
103
  return new Promise((resolve, reject) => {
107
- // Cancelled by the server, so the rejection is its `cancelled`,
108
- // also when the signal was aborted already.
109
- const cancel = () => this.post({ type: "cancel", id });
110
- const done = () => signal?.removeEventListener("abort", cancel);
104
+ const cancelOnServer = () => this.post({ type: "cancel", id });
105
+ const done = () => signal?.removeEventListener("abort", cancelOnServer);
111
106
  this.pending.set(id, { resolve: resolve, reject, done });
112
107
  this.post({ type: "request", id, method, params });
113
108
  if (signal?.aborted)
114
- cancel();
109
+ cancelOnServer();
115
110
  else
116
- signal?.addEventListener("abort", cancel, { once: true });
111
+ signal?.addEventListener("abort", cancelOnServer, { once: true });
117
112
  });
118
113
  }
119
114
  describe(method) {
@@ -1,13 +1,10 @@
1
1
  import { type Catalog, type Description, type ErrorCode, type ErrorMessage, type Event, type Frame, FrameType, type Hello, type Peer, type Welcome } from "../proto/index.ts";
2
- /** A message-oriented byte transport: one frame per message, as over WebSocket. */
3
2
  export interface Transport {
4
- send(bytes: Uint8Array): void;
3
+ send(bytes: Uint8Array<ArrayBuffer>): void;
5
4
  close(): void;
6
5
  onMessage: ((bytes: Uint8Array) => void) | undefined;
7
6
  onClose: (() => void) | undefined;
8
7
  }
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
8
  export type ErrorKind = "protocol" | "desync" | "server" | "closed";
12
9
  /** A failed request or connection. `kind: "server"` carries the server's
13
10
  * `ERROR` as `server`, and its code as `code`: a refused request, or a
@@ -22,13 +19,7 @@ export declare class ClientError extends Error {
22
19
  static desync(message: string): ClientError;
23
20
  static fromServer(error: ErrorMessage): ClientError;
24
21
  }
25
- /** `HELLO` for `client`: a name and version, or a name with this
26
- * package's version. */
27
22
  export declare function hello(client: string | Peer, engine?: string): Hello;
28
- /**
29
- * One protocol connection: the handshake, requests answered by id, events,
30
- * and attachment channels routed to their receivers.
31
- */
32
23
  export declare class Connection {
33
24
  private readonly transport;
34
25
  readonly welcome: Welcome;
@@ -36,29 +27,20 @@ export declare class Connection {
36
27
  private readonly pending;
37
28
  private readonly channels;
38
29
  private ended;
39
- /** Session events, once subscribed. */
40
30
  onEvent: ((event: Event) => void) | undefined;
41
- /** The connection ended: the transport closed, or an error without id. */
42
31
  onEnd: ((error: ClientError) => void) | undefined;
43
32
  private constructor();
44
- /** Sends `HELLO` and resolves at `WELCOME`; rejects on an `ERROR` or close. */
45
33
  static connect(transport: Transport, hello: Hello): Promise<Connection>;
46
34
  send(type: FrameType, channel: number, payload: Uint8Array): void;
47
35
  sendJson(type: FrameType, channel: number, body: unknown): void;
48
- /** A request; the reply's `result`, or a rejection with the server's error. */
49
36
  call<R>(method: string, params: unknown): Promise<R>;
50
- /** A request and its id on this connection, which `cancel` takes. */
51
37
  request<R>(method: string, params: unknown): {
52
38
  id: number;
53
39
  reply: Promise<R>;
54
40
  };
55
- /** The server's catalog (`describe {}`), or one method of it. */
56
41
  describe(): Promise<Catalog>;
57
42
  describe(method: string): Promise<Description>;
58
- /** Opens `channel`; its frames go to `receive` until `release` or the
59
- * connection's end. */
60
43
  open(channel: number, open: unknown, receive: (frame: Frame) => void): void;
61
- /** Stops routing `channel`; later frames on it are dropped. */
62
44
  release(channel: number): void;
63
45
  close(): void;
64
46
  private route;
@@ -25,11 +25,7 @@ export class ClientError extends Error {
25
25
  return new ClientError("server", `${error.code}: ${error.message}`, error);
26
26
  }
27
27
  }
28
- /** Frames before `WELCOME` tells us the real limit: the protocol's
29
- * default `max_frame`, as the Rust client. */
30
28
  const HELLO_MAX_FRAME = 1024 * 1024;
31
- /** `HELLO` for `client`: a name and version, or a name with this
32
- * package's version. */
33
29
  export function hello(client, engine) {
34
30
  const peer = typeof client === "string" ? { name: client, version: pkg.version } : client;
35
31
  const h = { version: VERSION, client: peer, capabilities: [] };
@@ -37,10 +33,6 @@ export function hello(client, engine) {
37
33
  h.engine = engine;
38
34
  return h;
39
35
  }
40
- /**
41
- * One protocol connection: the handshake, requests answered by id, events,
42
- * and attachment channels routed to their receivers.
43
- */
44
36
  export class Connection {
45
37
  transport;
46
38
  welcome;
@@ -48,9 +40,7 @@ export class Connection {
48
40
  pending = new Map();
49
41
  channels = new Map();
50
42
  ended;
51
- /** Session events, once subscribed. */
52
43
  onEvent;
53
- /** The connection ended: the transport closed, or an error without id. */
54
44
  onEnd;
55
45
  constructor(transport, welcome) {
56
46
  this.transport = transport;
@@ -58,7 +48,6 @@ export class Connection {
58
48
  transport.onMessage = (bytes) => this.route(bytes);
59
49
  transport.onClose = () => this.end(new ClientError("closed", "connection closed"));
60
50
  }
61
- /** Sends `HELLO` and resolves at `WELCOME`; rejects on an `ERROR` or close. */
62
51
  static connect(transport, hello) {
63
52
  return new Promise((resolve, reject) => {
64
53
  transport.onMessage = (bytes) => {
@@ -91,11 +80,9 @@ export class Connection {
91
80
  sendJson(type, channel, body) {
92
81
  this.send(type, channel, new TextEncoder().encode(JSON.stringify(body)));
93
82
  }
94
- /** A request; the reply's `result`, or a rejection with the server's error. */
95
83
  call(method, params) {
96
84
  return this.request(method, params).reply;
97
85
  }
98
- /** A request and its id on this connection, which `cancel` takes. */
99
86
  request(method, params) {
100
87
  const id = this.nextId++;
101
88
  const reply = new Promise((resolve, reject) => {
@@ -113,15 +100,12 @@ export class Connection {
113
100
  describe(method) {
114
101
  return this.call("describe", method === undefined ? {} : { method });
115
102
  }
116
- /** Opens `channel`; its frames go to `receive` until `release` or the
117
- * connection's end. */
118
103
  open(channel, open, receive) {
119
104
  if (this.ended)
120
105
  throw this.ended;
121
106
  this.channels.set(channel, receive);
122
107
  this.sendJson(FrameType.Open, channel, open);
123
108
  }
124
- /** Stops routing `channel`; later frames on it are dropped. */
125
109
  release(channel) {
126
110
  this.channels.delete(channel);
127
111
  }
@@ -151,7 +135,6 @@ export class Connection {
151
135
  }
152
136
  switch (frame.header.type) {
153
137
  case FrameType.Reply: {
154
- // A reply or error for a request no longer waited for is dropped.
155
138
  const reply = body;
156
139
  this.pending.get(reply.id)?.resolve(reply.result);
157
140
  this.pending.delete(reply.id);
@@ -1,4 +1,3 @@
1
- /** The page's copies to the clipboard, and what each came to. */
2
1
  /** What started a copy: the release of a selection, Ctrl+Shift+C, the
3
2
  * browser's copy (Cmd+C, the Copy menu), or the program (OSC 52). */
4
3
  export type CopySource = "release" | "keys" | "browser" | "program";
@@ -19,26 +18,19 @@ export type Copy = {
19
18
  chars?: number;
20
19
  message?: string;
21
20
  };
22
- /** The parts of `navigator.clipboard` a copy uses. */
23
21
  export interface ClipboardLike {
24
22
  write?(items: ClipboardItem[]): Promise<void>;
25
23
  writeText?(text: string): Promise<void>;
26
24
  }
27
25
  export interface CopyHost {
28
- /** The clipboard, read at each copy; undefined where there is none. */
29
26
  clipboard(): ClipboardLike | undefined;
30
- /** A `ClipboardItem` of text to come; undefined where the browser has
31
- * no `ClipboardItem`. */
32
27
  item: ((text: Promise<Blob>) => ClipboardItem) | undefined;
33
28
  report(copy: Copy): void;
34
29
  }
35
- /** A mounted terminal's copies. */
36
30
  export declare class Copier {
37
31
  private readonly host;
38
- /** The latest release's copy, while it waits for the worker's answer. */
39
32
  private pending;
40
33
  private next;
41
- /** Every copy not yet reported or dropped, for `dispose`. */
42
34
  private readonly live;
43
35
  constructor(host: CopyHost);
44
36
  /** Starts the copy of what a release selected, and returns its number
@@ -50,15 +42,9 @@ export declare class Copier {
50
42
  * release's copy still waiting is dropped, so the clipboard gets the
51
43
  * latest. */
52
44
  start(): number;
53
- /** The worker's answer to release `id`: the selected text, or undefined
54
- * when nothing was selected, which is no copy. An answer to a copy that
55
- * was dropped or timed out is ignored. */
56
45
  answer(id: number, text: string | undefined): void;
57
- /** Copies `text` known now. */
58
46
  write(text: string, source: CopySource): void;
59
- /** A copy the browser made itself (the `copy` event's data). */
60
47
  copied(text: string): void;
61
- /** Drops every copy under way, unreported. */
62
48
  dispose(): void;
63
49
  private attempt;
64
50
  private dropOne;
@@ -1,10 +1,5 @@
1
- /** The page's copies to the clipboard, and what each came to. */
2
- /** How long a copy may take: the worker's answer and the write. */
3
1
  const COPY_TIMEOUT_MS = 2000;
4
- /** Characters by code point, as a page counts them for display. */
5
- const length = (text) => [...text].length;
6
- /** One copy under way, reported once: when both its text's length and
7
- * the write's result are known, or at the timeout. */
2
+ const codePointLength = (text) => [...text].length;
8
3
  class Attempt {
9
4
  source;
10
5
  report;
@@ -13,11 +8,7 @@ class Attempt {
13
8
  chars;
14
9
  result;
15
10
  timer;
16
- constructor(source, report,
17
- /** Once, when it is reported or dropped. */
18
- ended,
19
- /** At the timeout, after the report. */
20
- timedOut) {
11
+ constructor(source, report, ended, timedOut) {
21
12
  this.source = source;
22
13
  this.report = report;
23
14
  this.ended = ended;
@@ -27,14 +18,13 @@ class Attempt {
27
18
  }, COPY_TIMEOUT_MS);
28
19
  }
29
20
  text(text) {
30
- this.chars = length(text);
21
+ this.chars = codePointLength(text);
31
22
  this.settle();
32
23
  }
33
24
  wrote(result) {
34
25
  this.result ??= result;
35
26
  this.settle();
36
27
  }
37
- /** Not a copy: nothing was selected, or a newer copy took its place. */
38
28
  drop() {
39
29
  if (this.done)
40
30
  return;
@@ -56,18 +46,14 @@ class Attempt {
56
46
  else
57
47
  this.report({ source: this.source, ...result, ...(chars !== undefined ? { chars } : {}) });
58
48
  }
59
- /** Follows a write's promise. */
60
49
  follow(write) {
61
50
  write.then(() => this.wrote({ ok: true }), (err) => this.wrote({ ok: false, reason: "refused", message: String(err) }));
62
51
  }
63
52
  }
64
- /** A mounted terminal's copies. */
65
53
  export class Copier {
66
54
  host;
67
- /** The latest release's copy, while it waits for the worker's answer. */
68
55
  pending;
69
56
  next = 1;
70
- /** Every copy not yet reported or dropped, for `dispose`. */
71
57
  live = new Set();
72
58
  constructor(host) {
73
59
  this.host = host;
@@ -86,7 +72,6 @@ export class Copier {
86
72
  const id = this.next++;
87
73
  const pending = {
88
74
  id,
89
- // At the timeout the write is left without its text.
90
75
  attempt: this.attempt("release", () => {
91
76
  if (this.pending === pending)
92
77
  this.pending = undefined;
@@ -119,9 +104,6 @@ export class Copier {
119
104
  pending.attempt.follow(clipboard.write([item]));
120
105
  return id;
121
106
  }
122
- /** The worker's answer to release `id`: the selected text, or undefined
123
- * when nothing was selected, which is no copy. An answer to a copy that
124
- * was dropped or timed out is ignored. */
125
107
  answer(id, text) {
126
108
  const pending = this.pending;
127
109
  if (pending?.id !== id)
@@ -135,17 +117,14 @@ export class Copier {
135
117
  else
136
118
  this.writeText(pending.attempt, text);
137
119
  }
138
- /** Copies `text` known now. */
139
120
  write(text, source) {
140
121
  const attempt = this.attempt(source);
141
122
  attempt.text(text);
142
123
  this.writeText(attempt, text);
143
124
  }
144
- /** A copy the browser made itself (the `copy` event's data). */
145
125
  copied(text) {
146
- this.host.report({ ok: true, source: "browser", chars: length(text) });
126
+ this.host.report({ ok: true, source: "browser", chars: codePointLength(text) });
147
127
  }
148
- /** Drops every copy under way, unreported. */
149
128
  dispose() {
150
129
  if (this.pending)
151
130
  this.dropOne(this.pending);
@@ -1,5 +1,4 @@
1
1
  import { type KeyInput } from "../engine/index.ts";
2
- /** The parts of a `KeyboardEvent` the mapping reads. */
3
2
  export interface KeyEventLike {
4
3
  type: string;
5
4
  code: string;
@@ -10,56 +9,31 @@ export interface KeyEventLike {
10
9
  altKey: boolean;
11
10
  metaKey: boolean;
12
11
  isComposing: boolean;
13
- /** 229 while an input method processes the key. */
14
12
  keyCode: number;
15
13
  }
16
- /** The parts of an `InputEvent` the input element reads. */
17
14
  export interface InputEventLike {
18
15
  inputType: string;
19
16
  data: string | null;
20
17
  isComposing: boolean;
21
18
  }
22
- /** What typing comes to: a key for the key encoder, or text as typed. */
23
19
  export type Typed = {
24
20
  key: KeyInput;
25
21
  } | {
26
22
  text: string;
27
23
  };
28
- /** What the page knows about the keyboard: the layout's unmodified
29
- * character per `code` when the browser exposes it, and the platform. */
30
24
  export interface Keyboard {
31
25
  layout: ReadonlyMap<string, string> | undefined;
32
26
  mac: boolean;
33
27
  }
34
- /** Whether the browser keeps the key: its own shortcuts with the platform's
35
- * command modifier, and Ctrl+Shift+V, the paste shortcut where Ctrl+V is
36
- * the terminal's. */
37
28
  export declare function browserKeeps(e: KeyEventLike): boolean;
38
- /** Whether the key belongs to an input method: it is composing, or
39
- * processing the key (`Process`, key code 229), or the key is a dead key
40
- * that starts a composition. Its text arrives at `compositionend`. */
41
29
  export declare function composing(e: KeyEventLike): boolean;
42
- /**
43
- * Typing on the terminal's input element, a textarea: keys go to the key
44
- * encoder, and text that no key sent goes as text. Keys the browser keeps
45
- * and keys of a composition are left to the browser; a composition's text
46
- * is sent when it ends, and text inserted without a key (a dead key's
47
- * character, the emoji picker, dictation) as it is inserted.
48
- */
49
30
  export declare class Typing {
50
31
  private readonly keyboard;
51
- /** Keys whose press was sent, so their release is too: a key that
52
- * ended a composition sends no release of a press it never sent. */
53
- private readonly down;
32
+ private readonly pressesSent;
54
33
  constructor(keyboard: Keyboard);
55
- /** A key to send, or undefined to leave the event to the browser. */
56
34
  keydown(e: KeyEventLike): KeyInput | undefined;
57
35
  keyup(e: KeyEventLike): KeyInput | undefined;
58
- /** `beforeinput`: what the element would insert or delete, when no key
59
- * sent it. A composition's own steps wait for `compositionend`; a paste
60
- * is the `paste` event's. */
61
36
  beforeinput(e: InputEventLike): Typed | undefined;
62
- /** `compositionend`: the composed text. */
63
37
  compositionend(data: string | null): Typed | undefined;
64
38
  }
65
39
  export declare function keyInput(e: KeyEventLike, keyboard: Keyboard): KeyInput;