@gjermundgaraba/clankermux 0.4.1 → 0.5.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.
@@ -1,5 +1,5 @@
1
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";
2
+ import { type Closed, type ErrorMessage, type Exit, type Frame, type Resume, type Session, type Welcome } from "../proto/index.ts";
3
3
  /** What a client does in response to frames. */
4
4
  export type Action =
5
5
  /** The screen is renderable from the local engine. The session may
@@ -20,7 +20,6 @@ export type Action =
20
20
  /** Bytes were lost; the terminal was discarded and a new bootstrap follows. */
21
21
  | {
22
22
  type: "gap";
23
- gap: Gap;
24
23
  } | {
25
24
  type: "closed";
26
25
  reason: Closed["reason"];
@@ -2,6 +2,8 @@ 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
+ const MAX_HELD_BYTES = 4 * 1024 * 1024;
5
7
  /**
6
8
  * The attachment state machine: frames in, actions out. It keeps the
7
9
  * local engine current and tracks the cursor for resuming.
@@ -91,7 +93,7 @@ export class Attachment {
91
93
  decoder.free();
92
94
  throw err;
93
95
  }
94
- this.state = { kind: "bootstrapping", decoder, held: [] };
96
+ this.state = { kind: "bootstrapping", decoder, held: [], heldBytes: 0 };
95
97
  }
96
98
  else if (this.state.kind !== "streaming") {
97
99
  throw ClientError.protocol("resume without a terminal");
@@ -139,6 +141,12 @@ export class Attachment {
139
141
  case FrameType.Resized:
140
142
  case FrameType.Exited:
141
143
  if (this.state.kind === "bootstrapping" && (this.state.held.length > 0 || type !== FrameType.Output)) {
144
+ const bytes = frame.payload.byteLength + 64;
145
+ if (bytes > MAX_HELD_BYTES - this.state.heldBytes) {
146
+ this.end(false);
147
+ throw ClientError.desync("bootstrap stream buffer full; fresh OPEN required");
148
+ }
149
+ this.state.heldBytes += bytes;
142
150
  this.state.held.push(frame);
143
151
  return;
144
152
  }
@@ -147,12 +155,12 @@ export class Attachment {
147
155
  }
148
156
  this.stream(frame, actions);
149
157
  break;
150
- case FrameType.Gap: {
151
- const gap = this.json(frame);
158
+ case FrameType.Gap:
159
+ // Nothing in it is read, but a malformed one is still refused.
160
+ this.json(frame);
152
161
  this.discard();
153
- actions.push({ type: "gap", gap });
162
+ actions.push({ type: "gap" });
154
163
  break;
155
- }
156
164
  case FrameType.Closed: {
157
165
  const closed = this.json(frame);
158
166
  this.end(true);
@@ -51,17 +51,13 @@ export declare class Client {
51
51
  private nextId;
52
52
  private readonly pending;
53
53
  private readonly mounts;
54
- private readonly terminals;
55
54
  private readonly sessionListeners;
56
- private readonly eventListeners;
57
55
  private readonly linkListeners;
58
- private latest;
59
- private link;
56
+ private readonly eventListeners;
60
57
  private started;
61
58
  private closed;
62
59
  /** @internal Use `connect`. */
63
60
  constructor(worker: Worker, started: (error?: Error) => void);
64
- private setLink;
65
61
  /** @internal */
66
62
  post(message: ToWorker): void;
67
63
  private call;
@@ -1,4 +1,5 @@
1
1
  import { ClientError } from "./connection.js";
2
+ import { Listeners } from "./listeners.js";
2
3
  import { mountOn } from "./mount.js";
3
4
  /**
4
5
  * Starts the worker and its connection to the server at `url`. Resolves
@@ -34,12 +35,9 @@ export class Client {
34
35
  nextId = 1;
35
36
  pending = new Map();
36
37
  mounts = new Map();
37
- terminals = new Set();
38
- sessionListeners = new Set();
38
+ sessionListeners = new Listeners();
39
+ linkListeners = new Listeners();
39
40
  eventListeners = new Set();
40
- linkListeners = new Set();
41
- latest;
42
- link;
43
41
  started = false;
44
42
  closed = false;
45
43
  /** @internal Use `connect`. */
@@ -52,7 +50,7 @@ export class Client {
52
50
  worker.onerror = (e) => {
53
51
  if (this.started)
54
52
  return;
55
- e.preventDefault?.();
53
+ e.preventDefault();
56
54
  started(new Error(`the worker failed: ${e.message || "it did not load"}`));
57
55
  this.close();
58
56
  };
@@ -78,12 +76,10 @@ export class Client {
78
76
  break;
79
77
  }
80
78
  case "connection":
81
- this.setLink(message.link);
79
+ this.linkListeners.emit(message.link);
82
80
  break;
83
81
  case "sessions":
84
- this.latest = message.sessions;
85
- for (const listener of this.sessionListeners)
86
- listener(message.sessions);
82
+ this.sessionListeners.emit(message.sessions);
87
83
  break;
88
84
  case "event":
89
85
  for (const listener of this.eventListeners)
@@ -94,15 +90,10 @@ export class Client {
94
90
  listener({ event: "gap", incarnation: message.incarnation });
95
91
  break;
96
92
  default:
97
- this.mounts.get(message.mount)?.(message);
93
+ this.mounts.get(message.mount)?.receive(message);
98
94
  }
99
95
  };
100
96
  }
101
- setLink(link) {
102
- this.link = link;
103
- for (const listener of this.linkListeners)
104
- listener(link);
105
- }
106
97
  /** @internal */
107
98
  post(message) {
108
99
  if (!this.closed)
@@ -199,18 +190,12 @@ export class Client {
199
190
  * `onLink` reports the connection-level changes. Requests need
200
191
  * `connected`. A listener added later gets the latest at once. */
201
192
  onLink(listener) {
202
- this.linkListeners.add(listener);
203
- if (this.link)
204
- listener(this.link);
205
- return () => void this.linkListeners.delete(listener);
193
+ return this.linkListeners.add(listener);
206
194
  }
207
195
  /** Every session on the server, at each change; a listener added once
208
196
  * the list is known gets it at once. */
209
197
  onSessions(listener) {
210
- this.sessionListeners.add(listener);
211
- if (this.latest)
212
- listener(this.latest);
213
- return () => void this.sessionListeners.delete(listener);
198
+ return this.sessionListeners.add(listener);
214
199
  }
215
200
  /** Each server event, in `seq` order, without loss across reconnects:
216
201
  * the ones missed while the connection was down are replayed, or an
@@ -227,28 +212,18 @@ export class Client {
227
212
  const id = this.nextId++;
228
213
  const host = {
229
214
  send: (message) => this.post(message),
230
- receive: (handler) => {
231
- if (handler)
232
- this.mounts.set(id, handler);
233
- else
234
- this.mounts.delete(id);
235
- },
236
- };
237
- const terminal = mountOn(host, id, options);
238
- const dispose = terminal.dispose;
239
- terminal.dispose = () => {
240
- this.terminals.delete(terminal);
241
- dispose();
215
+ disposed: () => void this.mounts.delete(id),
242
216
  };
243
- this.terminals.add(terminal);
244
- return terminal;
217
+ const mounted = mountOn(host, id, options);
218
+ this.mounts.set(id, mounted);
219
+ return mounted.terminal;
245
220
  }
246
221
  /** Disposes the terminals and terminates the worker, which ends the
247
222
  * connection; requests in flight reject with `closed`. */
248
223
  close() {
249
224
  if (this.closed)
250
225
  return;
251
- for (const terminal of [...this.terminals])
226
+ for (const { terminal } of [...this.mounts.values()])
252
227
  terminal.dispose();
253
228
  this.closed = true;
254
229
  this.worker.onmessage = null;
@@ -32,18 +32,15 @@ export interface CopyHost {
32
32
  item: ((text: Promise<Blob>) => ClipboardItem) | undefined;
33
33
  report(copy: Copy): void;
34
34
  }
35
- /** How long a copy may take: the worker's answer and the write. */
36
- export declare const COPY_TIMEOUT_MS = 2000;
37
35
  /** A mounted terminal's copies. */
38
36
  export declare class Copier {
39
37
  private readonly host;
40
- private readonly timeoutMs;
41
- /** The releases' copies waiting for the worker's answer, by number. */
42
- private readonly pending;
38
+ /** The latest release's copy, while it waits for the worker's answer. */
39
+ private pending;
43
40
  private next;
44
41
  /** Every copy not yet reported or dropped, for `dispose`. */
45
42
  private readonly live;
46
- constructor(host: CopyHost, timeoutMs?: number);
43
+ constructor(host: CopyHost);
47
44
  /** Starts the copy of what a release selected, and returns its number
48
45
  * for the worker's answer. Safari writes to the clipboard only within
49
46
  * the user's gesture, which is over before the worker answers: the
@@ -60,7 +57,7 @@ export declare class Copier {
60
57
  /** Copies `text` known now. */
61
58
  write(text: string, source: CopySource): void;
62
59
  /** A copy the browser made itself (the `copy` event's data). */
63
- copied(text: string, source: CopySource): void;
60
+ copied(text: string): void;
64
61
  /** Drops every copy under way, unreported. */
65
62
  dispose(): void;
66
63
  private attempt;
@@ -1,6 +1,6 @@
1
1
  /** The page's copies to the clipboard, and what each came to. */
2
2
  /** How long a copy may take: the worker's answer and the write. */
3
- export const COPY_TIMEOUT_MS = 2000;
3
+ const COPY_TIMEOUT_MS = 2000;
4
4
  /** Characters by code point, as a page counts them for display. */
5
5
  const length = (text) => [...text].length;
6
6
  /** One copy under way, reported once: when both its text's length and
@@ -13,7 +13,7 @@ class Attempt {
13
13
  chars;
14
14
  result;
15
15
  timer;
16
- constructor(source, report, timeoutMs,
16
+ constructor(source, report,
17
17
  /** Once, when it is reported or dropped. */
18
18
  ended,
19
19
  /** At the timeout, after the report. */
@@ -24,7 +24,7 @@ class Attempt {
24
24
  this.timer = setTimeout(() => {
25
25
  this.finish({ ok: false, reason: "timeout" });
26
26
  timedOut?.();
27
- }, timeoutMs);
27
+ }, COPY_TIMEOUT_MS);
28
28
  }
29
29
  text(text) {
30
30
  this.chars = length(text);
@@ -64,15 +64,13 @@ class Attempt {
64
64
  /** A mounted terminal's copies. */
65
65
  export class Copier {
66
66
  host;
67
- timeoutMs;
68
- /** The releases' copies waiting for the worker's answer, by number. */
69
- pending = new Map();
67
+ /** The latest release's copy, while it waits for the worker's answer. */
68
+ pending;
70
69
  next = 1;
71
70
  /** Every copy not yet reported or dropped, for `dispose`. */
72
71
  live = new Set();
73
- constructor(host, timeoutMs = COPY_TIMEOUT_MS) {
72
+ constructor(host) {
74
73
  this.host = host;
75
- this.timeoutMs = timeoutMs;
76
74
  }
77
75
  /** Starts the copy of what a release selected, and returns its number
78
76
  * for the worker's answer. Safari writes to the clipboard only within
@@ -83,18 +81,19 @@ export class Copier {
83
81
  * release's copy still waiting is dropped, so the clipboard gets the
84
82
  * latest. */
85
83
  start() {
86
- for (const earlier of this.pending.values())
87
- this.dropOne(earlier);
88
- this.pending.clear();
84
+ if (this.pending)
85
+ this.dropOne(this.pending);
89
86
  const id = this.next++;
90
87
  const pending = {
88
+ id,
91
89
  // At the timeout the write is left without its text.
92
90
  attempt: this.attempt("release", () => {
93
- this.pending.delete(id);
91
+ if (this.pending === pending)
92
+ this.pending = undefined;
94
93
  pending.text?.reject();
95
94
  }),
96
95
  };
97
- this.pending.set(id, pending);
96
+ this.pending = pending;
98
97
  const clipboard = this.host.clipboard();
99
98
  if (!this.host.item || !clipboard?.write)
100
99
  return id;
@@ -124,10 +123,10 @@ export class Copier {
124
123
  * when nothing was selected, which is no copy. An answer to a copy that
125
124
  * was dropped or timed out is ignored. */
126
125
  answer(id, text) {
127
- const pending = this.pending.get(id);
128
- if (!pending)
126
+ const pending = this.pending;
127
+ if (pending?.id !== id)
129
128
  return;
130
- this.pending.delete(id);
129
+ this.pending = undefined;
131
130
  if (text === undefined)
132
131
  return this.dropOne(pending);
133
132
  pending.attempt.text(text);
@@ -143,19 +142,19 @@ export class Copier {
143
142
  this.writeText(attempt, text);
144
143
  }
145
144
  /** A copy the browser made itself (the `copy` event's data). */
146
- copied(text, source) {
147
- this.host.report({ ok: true, source, chars: length(text) });
145
+ copied(text) {
146
+ this.host.report({ ok: true, source: "browser", chars: length(text) });
148
147
  }
149
148
  /** Drops every copy under way, unreported. */
150
149
  dispose() {
151
- for (const pending of this.pending.values())
152
- this.dropOne(pending);
153
- this.pending.clear();
150
+ if (this.pending)
151
+ this.dropOne(this.pending);
152
+ this.pending = undefined;
154
153
  for (const attempt of this.live)
155
154
  attempt.drop();
156
155
  }
157
156
  attempt(source, timedOut) {
158
- const attempt = new Attempt(source, this.host.report, this.timeoutMs, () => this.live.delete(attempt), timedOut);
157
+ const attempt = new Attempt(source, this.host.report, () => this.live.delete(attempt), timedOut);
159
158
  this.live.add(attempt);
160
159
  return attempt;
161
160
  }
@@ -1,12 +1,7 @@
1
- export { Attachment } from "./attachment.ts";
2
- export type { Action } from "./attachment.ts";
3
1
  export { Client, connect } from "./client.ts";
4
2
  export type { ConnectOptions, EventGap, SendInput, WaitOptions } from "./client.ts";
5
- export { ClientError, Connection, hello } from "./connection.ts";
6
- export type { ErrorKind, Transport } from "./connection.ts";
3
+ export { ClientError } from "./connection.ts";
7
4
  export type { Copy, CopyFailure, CopySource } from "./copy.ts";
8
- export { Osc52Scanner } from "./osc52.ts";
9
- export type { ClipboardWrite } from "./osc52.ts";
10
5
  export type { Colors, MountOptions, Terminal } from "./mount.ts";
11
6
  export type { Link, LinkEnd } from "./worker.ts";
12
7
  export type { Acked, Attention, AttachmentInfo, Catalog, CreateParams, Description, ErrorCode, Event, Exit, MethodInfo, Methods, OnExit, Page, Peer, Read, ReadFormat, ReadScope, Scheme, Sent, Session, SizePolicy, StatusRecord, StreamCursor, Tail, Until, Waited, } from "../proto/index.ts";
@@ -1,4 +1,2 @@
1
- export { Attachment } from "./attachment.js";
2
1
  export { Client, connect } from "./client.js";
3
- export { ClientError, Connection, hello } from "./connection.js";
4
- export { Osc52Scanner } from "./osc52.js";
2
+ export { ClientError } from "./connection.js";
@@ -0,0 +1,8 @@
1
+ /** Listeners of one kind, and the latest value they were told. */
2
+ export declare class Listeners<T> {
3
+ private readonly all;
4
+ private latest;
5
+ add(listener: (value: T) => void): () => void;
6
+ emit(value: T): void;
7
+ clear(): void;
8
+ }
@@ -0,0 +1,19 @@
1
+ /** Listeners of one kind, and the latest value they were told. */
2
+ export class Listeners {
3
+ all = new Set();
4
+ latest;
5
+ add(listener) {
6
+ this.all.add(listener);
7
+ if (this.latest)
8
+ listener(this.latest.value);
9
+ return () => void this.all.delete(listener);
10
+ }
11
+ emit(value) {
12
+ this.latest = { value };
13
+ for (const listener of this.all)
14
+ listener(value);
15
+ }
16
+ clear() {
17
+ this.all.clear();
18
+ }
19
+ }
@@ -75,11 +75,21 @@ export interface Terminal {
75
75
  * copy. A listener added later is not told of earlier copies. */
76
76
  onCopy(listener: (copy: Copy) => void): () => void;
77
77
  }
78
- /** The client's side of a terminal: its messages to the worker, and the
79
- * worker's for it. */
78
+ /** The client's side of a terminal: its messages to the worker, and
79
+ * word that it is disposed. */
80
80
  export interface MountHost {
81
81
  send(message: ToWorker): void;
82
- receive(handler: ((message: ToPage) => void) | undefined): void;
82
+ disposed(): void;
83
83
  }
84
- /** Mounts a terminal as `id` of the client `host` serves. */
85
- export declare function mountOn(host: MountHost, id: number, options: MountOptions): Terminal;
84
+ type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
85
+ /** A message for one terminal, without its number. */
86
+ export type ToMount = DistributiveOmit<Extract<ToWorker, {
87
+ mount: number;
88
+ }>, "mount">;
89
+ /** Mounts a terminal as `id` of the client `host` serves, and the
90
+ * handler of the worker's messages for it. */
91
+ export declare function mountOn(host: MountHost, id: number, options: MountOptions): {
92
+ terminal: Terminal;
93
+ receive: (message: ToPage) => void;
94
+ };
95
+ export {};
@@ -1,26 +1,8 @@
1
1
  import { CanvasRenderer } from "../renderer/index.js";
2
2
  import { Copier } from "./copy.js";
3
- import { Typing } from "./keyboard.js";
3
+ import { Typing, composing } from "./keyboard.js";
4
+ import { Listeners } from "./listeners.js";
4
5
  import { profileFrom, schemeOf } from "./profile.js";
5
- /** Listeners of one kind, and the latest value they were told. */
6
- class Listeners {
7
- all = new Set();
8
- latest;
9
- add(listener) {
10
- this.all.add(listener);
11
- if (this.latest)
12
- listener(this.latest.value);
13
- return () => void this.all.delete(listener);
14
- }
15
- emit(value) {
16
- this.latest = { value };
17
- for (const listener of this.all)
18
- listener(value);
19
- }
20
- clear() {
21
- this.all.clear();
22
- }
23
- }
24
6
  /** Cell metrics from the font. */
25
7
  function measure(font) {
26
8
  const ctx = document.createElement("canvas").getContext("2d");
@@ -39,7 +21,8 @@ function measure(font) {
39
21
  boldItalicFont: `bold italic ${font}`,
40
22
  };
41
23
  }
42
- /** Mounts a terminal as `id` of the client `host` serves. */
24
+ /** Mounts a terminal as `id` of the client `host` serves, and the
25
+ * handler of the worker's messages for it. */
43
26
  export function mountOn(host, id, options) {
44
27
  const { canvas, container, colors } = options;
45
28
  const light = matchMedia("(prefers-color-scheme: light)");
@@ -114,7 +97,29 @@ export function mountOn(host, id, options) {
114
97
  /** Rows the view is scrolled back, as `onScroll` last reported;
115
98
  * undefined before the first frame. */
116
99
  let scrolled;
117
- host.receive((message) => {
100
+ const visible = () => document.visibilityState === "visible";
101
+ let pendingFrame;
102
+ let paintRequest = 0;
103
+ const paint = () => {
104
+ paintRequest = 0;
105
+ const frame = pendingFrame;
106
+ pendingFrame = undefined;
107
+ if (!frame || disposed)
108
+ return;
109
+ if (visible()) {
110
+ fitCanvas(frame.cols, frame.rows);
111
+ if (fullNext && !frame.full) {
112
+ // The backing store was recreated; a partial frame cannot fill it.
113
+ send({ type: "repaint" });
114
+ }
115
+ else {
116
+ fullNext = false;
117
+ renderer.draw(frame);
118
+ }
119
+ }
120
+ send({ type: "painted" });
121
+ };
122
+ const receive = (message) => {
118
123
  switch (message.type) {
119
124
  case "status":
120
125
  statuses.emit(message.text);
@@ -122,25 +127,25 @@ export function mountOn(host, id, options) {
122
127
  case "link":
123
128
  links.emit(message.link);
124
129
  break;
125
- case "frame": {
126
- if (message.frame.cursor)
127
- cursorAt = { x: message.frame.cursor.x, y: message.frame.cursor.y };
128
- const { total, offset, len } = message.frame.scrollbar;
129
- if (total - offset - len !== scrolled) {
130
- scrolled = total - offset - len;
131
- scrolls.emit(scrolled);
132
- }
133
- fitCanvas(message.frame.cols, message.frame.rows);
134
- if (fullNext && !message.frame.full) {
135
- // The backing store was recreated; a partial frame cannot fill it.
136
- send({ type: "repaint" });
130
+ case "frame":
131
+ if (disposed)
132
+ break;
133
+ if (!visible()) {
134
+ send({ type: "painted" });
137
135
  }
138
136
  else {
139
- fullNext = false;
140
- renderer.draw(message.frame);
137
+ const frame = message.frame;
138
+ if (frame.cursor)
139
+ cursorAt = { x: frame.cursor.x, y: frame.cursor.y };
140
+ const { total, offset, len } = frame.scrollbar;
141
+ if (total - offset - len !== scrolled) {
142
+ scrolled = total - offset - len;
143
+ scrolls.emit(scrolled);
144
+ }
145
+ pendingFrame = frame;
146
+ paintRequest = requestAnimationFrame(paint);
141
147
  }
142
148
  break;
143
- }
144
149
  case "clipboard":
145
150
  copier.write(message.text, "program");
146
151
  break;
@@ -150,7 +155,7 @@ export function mountOn(host, id, options) {
150
155
  copier.answer(message.copy, message.text);
151
156
  break;
152
157
  }
153
- });
158
+ };
154
159
  /** Where the input element goes for a composition: the cursor's cell,
155
160
  * as the latest frame that had the cursor in view placed it. */
156
161
  let cursorAt = { x: 0, y: 0 };
@@ -240,7 +245,7 @@ export function mountOn(host, id, options) {
240
245
  send({ type: "key", input: key });
241
246
  e.preventDefault();
242
247
  }
243
- else if (e.isComposing || e.key === "Dead" || e.keyCode === 229) {
248
+ else if (composing(e)) {
244
249
  // At the cursor before the input method opens its window; shown
245
250
  // once a composition starts.
246
251
  placeInput(false);
@@ -292,7 +297,7 @@ export function mountOn(host, id, options) {
292
297
  return;
293
298
  e.clipboardData.setData("text/plain", selected);
294
299
  e.preventDefault();
295
- copier.copied(selected, "browser");
300
+ copier.copied(selected);
296
301
  }, { signal });
297
302
  /** The pointer in cells of the grid, fractions included, kept inside it. */
298
303
  function cellAt(e) {
@@ -374,14 +379,25 @@ export function mountOn(host, id, options) {
374
379
  /** Whether the user is looking at this page: it is visible and its
375
380
  * window has the focus. Only then does the session get its size, so
376
381
  * of several clients the one in use sets it. */
377
- const active = () => document.visibilityState === "visible" && document.hasFocus();
382
+ const active = () => visible() && document.hasFocus();
378
383
  const report = () => send({ type: "viewport", size: viewport(), profile: profile(), active: active() });
379
384
  const resizes = new ResizeObserver(report);
380
385
  resizes.observe(container);
381
386
  light.addEventListener("change", report, { signal });
382
387
  // Shown or focused: the session takes this page's size again; hidden or
383
388
  // blurred: the worker stops reporting.
384
- document.addEventListener("visibilitychange", report, { signal });
389
+ document.addEventListener("visibilitychange", () => {
390
+ send({ type: "visibility", visible: visible() });
391
+ if (!visible() && pendingFrame) {
392
+ cancelAnimationFrame(paintRequest);
393
+ paintRequest = 0;
394
+ pendingFrame = undefined;
395
+ // Hidden documents may never get another animation callback. Release
396
+ // the frame after hiding the worker; showing requests a full refresh.
397
+ send({ type: "painted" });
398
+ }
399
+ report();
400
+ }, { signal });
385
401
  window.addEventListener("focus", report, { signal });
386
402
  window.addEventListener("blur", report, { signal });
387
403
  // A page pins its scheme (and its colours with it) with an attribute or
@@ -409,10 +425,11 @@ export function mountOn(host, id, options) {
409
425
  viewport: viewport(),
410
426
  profile: profile(),
411
427
  active: active(),
428
+ visible: visible(),
412
429
  readOnly: options.readOnly ?? false,
413
430
  ackOnFocus: options.ackOnFocus ?? false,
414
431
  });
415
- return {
432
+ const terminal = {
416
433
  attach: (session) => send({ type: "attach", session }),
417
434
  detach: () => send({ type: "detach" }),
418
435
  focus: () => input.focus({ preventScroll: true }),
@@ -430,7 +447,9 @@ export function mountOn(host, id, options) {
430
447
  return;
431
448
  host.send({ type: "unmount", mount: id });
432
449
  disposed = true;
433
- host.receive(undefined);
450
+ cancelAnimationFrame(paintRequest);
451
+ pendingFrame = undefined;
452
+ host.disposed();
434
453
  listening.abort();
435
454
  resizes.disconnect();
436
455
  restyles.disconnect();
@@ -449,4 +468,5 @@ export function mountOn(host, id, options) {
449
468
  return () => void copies.delete(listener);
450
469
  },
451
470
  };
471
+ return { terminal, receive };
452
472
  }
@@ -27,11 +27,14 @@ export class Osc52Scanner {
27
27
  /** Feeds output bytes; returns the clipboard writes they completed. */
28
28
  feed(bytes) {
29
29
  const writes = [];
30
- for (const b of bytes) {
30
+ for (let at = 0; at < bytes.length; at++) {
31
+ const b = bytes[at];
31
32
  switch (this.state) {
32
33
  case "text":
33
- if (b === ESC)
34
- this.state = "esc";
34
+ at = bytes.indexOf(ESC, at);
35
+ if (at < 0)
36
+ return writes;
37
+ this.state = "esc";
35
38
  break;
36
39
  case "esc":
37
40
  // The payload is empty: every way out of an OSC clears it.
@@ -53,12 +56,14 @@ export class Osc52Scanner {
53
56
  this.reset();
54
57
  }
55
58
  else if (this.state === "osc") {
56
- this.payload.push(b);
57
- const head = String.fromCharCode(...this.payload.slice(0, PREFIX.length));
58
- if (!PREFIX.startsWith(head) || this.payload.length > MAX_OSC) {
59
+ const length = this.payload.length;
60
+ if ((length < PREFIX.length && b !== PREFIX.charCodeAt(length)) || length === MAX_OSC) {
59
61
  this.state = "skip";
60
62
  this.payload = [];
61
63
  }
64
+ else {
65
+ this.payload.push(b);
66
+ }
62
67
  }
63
68
  break;
64
69
  }