@gjermundgaraba/clankermux 0.4.0 → 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.
- package/README.md +23 -2
- package/dist/client/attachment.d.ts +1 -2
- package/dist/client/attachment.js +13 -5
- package/dist/client/client.d.ts +1 -5
- package/dist/client/client.js +14 -39
- package/dist/client/copy.d.ts +66 -0
- package/dist/client/copy.js +171 -0
- package/dist/client/index.d.ts +2 -6
- package/dist/client/index.js +1 -3
- package/dist/client/listeners.d.ts +8 -0
- package/dist/client/listeners.js +19 -0
- package/dist/client/mount.d.ts +20 -5
- package/dist/client/mount.js +96 -94
- package/dist/client/osc52.js +11 -6
- package/dist/client/worker.d.ts +30 -8
- package/dist/client/worker.js +141 -117
- package/dist/engine/decoder.js +4 -8
- package/dist/engine/index.d.ts +7 -2
- package/dist/engine/index.js +32 -21
- package/dist/engine/keys.js +8 -2
- package/dist/engine/render.d.ts +20 -5
- package/dist/engine/render.js +46 -16
- package/dist/engine/selection.js +1 -4
- package/dist/proto/messages.d.ts +800 -475
- package/dist/proto/messages.js +1 -0
- package/dist/renderer/index.d.ts +4 -2
- package/dist/renderer/index.js +42 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -97,14 +97,16 @@ While connected, a cancelled wait is cancelled by the server, so it rejects with
|
|
|
97
97
|
| `onLink(listener)` | Each change of the link to the session (below). |
|
|
98
98
|
| `onScroll(listener)` | The rows between the bottom of the view and the live screen's (below), from the first frame on. |
|
|
99
99
|
| `onStatus(listener)` | The same moments as `onLink`, and more, as text for display. |
|
|
100
|
+
| `onCopy(listener)` | Each copy to the clipboard as it ends: copied, or failed and why (below). |
|
|
100
101
|
|
|
101
|
-
The `on*` methods return a function that removes the listener; a listener added after a change gets the latest at once.
|
|
102
|
+
The `on*` methods return a function that removes the listener; a listener added after a change gets the latest at once, except `onCopy`'s, which is told only of copies after it.
|
|
102
103
|
|
|
103
104
|
## Keys and composed text
|
|
104
105
|
|
|
105
106
|
The terminal's input element is a textarea the mount places after the canvas: an input method composes only into an editable element. It is invisible, out of the tab order (`tabIndex = -1`) and ignores the pointer. Focusing the canvas (a click, `canvas.focus()`, a tab stop the page gives it) or calling `focus()` focuses the textarea, so `document.activeElement` is the textarea while the terminal has the keyboard: test for focus with `container.contains(document.activeElement)`, not `=== canvas`. Key events bubble from it through the container as they did from the canvas.
|
|
106
107
|
|
|
107
108
|
- Keys go to the engine's key encoder, which follows the program's modes (cursor keys, the kitty keyboard protocol). The browser keeps Cmd (Meta) chords and Ctrl+Shift+V, its paste.
|
|
109
|
+
- Shift that made a key's text (`:`, `"`, `A`) is consumed, as a keyboard reports it: under the kitty protocol's disambiguation, which neovim asks for, the text goes as typed, on any layout; reporting all keys, the key is the layout's own character with Shift, and the shifted one is among the alternates when the program asks for them. Shift made the text when it is not the key's own character, as the browser's layout map has it (Chromium); for a key the map does not cover (the numpad, Space) or without a map (Safari, Firefox), when it is any text but a space.
|
|
108
110
|
- A key that belongs to an input method is left to it: one that is composing (`isComposing`), that it processes (`Process`, key code 229), or a dead key (`Dead`). The composition is shown at the cursor, in the terminal's colours, while it lasts, and its text is sent when it ends (`compositionend`). A key that ended a composition sends no release.
|
|
109
111
|
- Text inserted with no key that sent it (a dead key's character, the emoji picker, dictation) is sent as it is inserted (`beforeinput` `insertText`); a line break or a backward delete from such a source is sent as Enter or Backspace.
|
|
110
112
|
- Composed text goes to the program as UTF-8, as typed text does, not as a paste.
|
|
@@ -193,7 +195,26 @@ The selection is the engine's, drawn inverted, and the engine keeps it on its ce
|
|
|
193
195
|
|
|
194
196
|
It is cleared by a click (a press that selects nothing), by input sent to the session (a key that sends something, a paste, composed text; not a modifier alone), by a switch to the alternate screen (it is not back when the program leaves it), by a full reset (RIS), and by a new screen (attaching, a gap).
|
|
195
197
|
|
|
196
|
-
The copy on release starts within `mouseup`, as Safari requires: `navigator.clipboard.write` with a `ClipboardItem` whose text arrives when the worker has it, a few milliseconds later; when nothing was selected it fails and the clipboard is left alone. Where `ClipboardItem` is missing it is `writeText` once the text arrives, which a browser that wants the gesture still running may refuse; Cmd+C then copies. Verified in headless Chrome only. A `beforecopy` listener lets WebKit offer Copy on the empty input element while there is a selection. Where the browser keeps Ctrl+Shift+C for itself (a developer tools shortcut), Cmd+C or the Copy menu copies. Programs that ask for mouse reports get none from this client, so the mouse always selects.
|
|
198
|
+
The copy on release starts within `mouseup`, as Safari requires: `navigator.clipboard.write` with a `ClipboardItem` whose text arrives when the worker has it, a few milliseconds later; when nothing was selected it fails and the clipboard is left alone. Where `ClipboardItem` is missing, or takes no text to come, it is `writeText` once the text arrives, which a browser that wants the gesture still running may refuse; Cmd+C then copies. Verified in headless Chrome only. A `beforecopy` listener lets WebKit offer Copy on the empty input element while there is a selection. Where the browser keeps Ctrl+Shift+C for itself (a developer tools shortcut), Cmd+C or the Copy menu copies. Programs that ask for mouse reports get none from this client, so the mouse always selects.
|
|
199
|
+
|
|
200
|
+
### Copy outcome
|
|
201
|
+
|
|
202
|
+
`terminal.onCopy(listener)` tells the page what each copy to the clipboard came to, once it is known:
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
type Copy =
|
|
206
|
+
| { ok: true; source: CopySource; chars: number }
|
|
207
|
+
| { ok: false; source: CopySource; reason: "refused" | "timeout" | "unavailable"; chars?: number; message?: string };
|
|
208
|
+
type CopySource = "release" | "keys" | "browser" | "program";
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
- `source`: `release`, the selection copied as the button came up; `keys`, Ctrl+Shift+C; `browser`, the browser's own copy (Cmd+C, the Copy menu); `program`, the program's OSC 52 write.
|
|
212
|
+
- `chars` is the text's length in characters (code points). The text itself is in the clipboard, and `selection()` has it.
|
|
213
|
+
- `ok: true` once the browser's write resolved (for `browser`, once the `copy` event's data was set).
|
|
214
|
+
- `refused`: the browser rejected the write, `message` its error (`NotAllowedError: …`): a permission denied, a document without the focus, an Electron shell that does not allow it. `timeout`: the copy did not finish within two seconds (the worker's text or the write); without the worker's text it has no `chars`. `unavailable`: the page has no `navigator.clipboard` (it is not a secure context).
|
|
215
|
+
- A release that selected nothing (a click) is no copy and tells nothing; nor does a release whose copy a newer one replaced before its text arrived. Each copy is told once.
|
|
216
|
+
|
|
217
|
+
A page shows a brief notification on `ok: true` ("Copied 42 characters"), and on a failure says the clipboard did not take it (with Cmd+C as the way to try again, since the browser's own copy is the one a browser that refuses `navigator.clipboard` still allows). A mouseup with `selection()` is no substitute: it says nothing about whether the write succeeded. `program` copies arrive without the user's gesture, which a browser may refuse; a page that announces only the user's copies leaves them out. Verified in headless Chrome: a double click's copy is told `ok` with the clipboard holding the word, and `refused` with the clipboard permission denied; the rest (timeouts, no `ClipboardItem`, no clipboard) by the package's tests against a stand-in clipboard. Not verified in Safari, Firefox or Electron.
|
|
197
218
|
|
|
198
219
|
## Scrollback
|
|
199
220
|
|
|
@@ -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
|
|
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
|
-
|
|
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"
|
|
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);
|
package/dist/client/client.d.ts
CHANGED
|
@@ -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
|
|
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;
|
package/dist/client/client.js
CHANGED
|
@@ -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
|
-
|
|
38
|
-
|
|
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.
|
|
79
|
+
this.linkListeners.emit(message.link);
|
|
82
80
|
break;
|
|
83
81
|
case "sessions":
|
|
84
|
-
this.
|
|
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
|
-
|
|
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
|
-
|
|
244
|
-
|
|
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.
|
|
226
|
+
for (const { terminal } of [...this.mounts.values()])
|
|
252
227
|
terminal.dispose();
|
|
253
228
|
this.closed = true;
|
|
254
229
|
this.worker.onmessage = null;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** The page's copies to the clipboard, and what each came to. */
|
|
2
|
+
/** What started a copy: the release of a selection, Ctrl+Shift+C, the
|
|
3
|
+
* browser's copy (Cmd+C, the Copy menu), or the program (OSC 52). */
|
|
4
|
+
export type CopySource = "release" | "keys" | "browser" | "program";
|
|
5
|
+
/** Why a copy failed: the browser refused the write (`message` says
|
|
6
|
+
* what it gave), it did not finish within two seconds, or the page has no
|
|
7
|
+
* clipboard (outside a secure context). */
|
|
8
|
+
export type CopyFailure = "refused" | "timeout" | "unavailable";
|
|
9
|
+
/** A copy's outcome. `chars` is the text's length in characters (code
|
|
10
|
+
* points); a copy that timed out before the text was known has none. */
|
|
11
|
+
export type Copy = {
|
|
12
|
+
ok: true;
|
|
13
|
+
source: CopySource;
|
|
14
|
+
chars: number;
|
|
15
|
+
} | {
|
|
16
|
+
ok: false;
|
|
17
|
+
source: CopySource;
|
|
18
|
+
reason: CopyFailure;
|
|
19
|
+
chars?: number;
|
|
20
|
+
message?: string;
|
|
21
|
+
};
|
|
22
|
+
/** The parts of `navigator.clipboard` a copy uses. */
|
|
23
|
+
export interface ClipboardLike {
|
|
24
|
+
write?(items: ClipboardItem[]): Promise<void>;
|
|
25
|
+
writeText?(text: string): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
export interface CopyHost {
|
|
28
|
+
/** The clipboard, read at each copy; undefined where there is none. */
|
|
29
|
+
clipboard(): ClipboardLike | undefined;
|
|
30
|
+
/** A `ClipboardItem` of text to come; undefined where the browser has
|
|
31
|
+
* no `ClipboardItem`. */
|
|
32
|
+
item: ((text: Promise<Blob>) => ClipboardItem) | undefined;
|
|
33
|
+
report(copy: Copy): void;
|
|
34
|
+
}
|
|
35
|
+
/** A mounted terminal's copies. */
|
|
36
|
+
export declare class Copier {
|
|
37
|
+
private readonly host;
|
|
38
|
+
/** The latest release's copy, while it waits for the worker's answer. */
|
|
39
|
+
private pending;
|
|
40
|
+
private next;
|
|
41
|
+
/** Every copy not yet reported or dropped, for `dispose`. */
|
|
42
|
+
private readonly live;
|
|
43
|
+
constructor(host: CopyHost);
|
|
44
|
+
/** Starts the copy of what a release selected, and returns its number
|
|
45
|
+
* for the worker's answer. Safari writes to the clipboard only within
|
|
46
|
+
* the user's gesture, which is over before the worker answers: the
|
|
47
|
+
* write starts here with the text to come, and fails, leaving the
|
|
48
|
+
* clipboard alone, when nothing was selected. Where the browser has no
|
|
49
|
+
* `ClipboardItem` the text is written once it arrives. An earlier
|
|
50
|
+
* release's copy still waiting is dropped, so the clipboard gets the
|
|
51
|
+
* latest. */
|
|
52
|
+
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
|
+
answer(id: number, text: string | undefined): void;
|
|
57
|
+
/** Copies `text` known now. */
|
|
58
|
+
write(text: string, source: CopySource): void;
|
|
59
|
+
/** A copy the browser made itself (the `copy` event's data). */
|
|
60
|
+
copied(text: string): void;
|
|
61
|
+
/** Drops every copy under way, unreported. */
|
|
62
|
+
dispose(): void;
|
|
63
|
+
private attempt;
|
|
64
|
+
private dropOne;
|
|
65
|
+
private writeText;
|
|
66
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
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
|
+
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. */
|
|
8
|
+
class Attempt {
|
|
9
|
+
source;
|
|
10
|
+
report;
|
|
11
|
+
ended;
|
|
12
|
+
done = false;
|
|
13
|
+
chars;
|
|
14
|
+
result;
|
|
15
|
+
timer;
|
|
16
|
+
constructor(source, report,
|
|
17
|
+
/** Once, when it is reported or dropped. */
|
|
18
|
+
ended,
|
|
19
|
+
/** At the timeout, after the report. */
|
|
20
|
+
timedOut) {
|
|
21
|
+
this.source = source;
|
|
22
|
+
this.report = report;
|
|
23
|
+
this.ended = ended;
|
|
24
|
+
this.timer = setTimeout(() => {
|
|
25
|
+
this.finish({ ok: false, reason: "timeout" });
|
|
26
|
+
timedOut?.();
|
|
27
|
+
}, COPY_TIMEOUT_MS);
|
|
28
|
+
}
|
|
29
|
+
text(text) {
|
|
30
|
+
this.chars = length(text);
|
|
31
|
+
this.settle();
|
|
32
|
+
}
|
|
33
|
+
wrote(result) {
|
|
34
|
+
this.result ??= result;
|
|
35
|
+
this.settle();
|
|
36
|
+
}
|
|
37
|
+
/** Not a copy: nothing was selected, or a newer copy took its place. */
|
|
38
|
+
drop() {
|
|
39
|
+
if (this.done)
|
|
40
|
+
return;
|
|
41
|
+
this.done = true;
|
|
42
|
+
clearTimeout(this.timer);
|
|
43
|
+
this.ended();
|
|
44
|
+
}
|
|
45
|
+
settle() {
|
|
46
|
+
if (this.chars !== undefined && this.result)
|
|
47
|
+
this.finish(this.result);
|
|
48
|
+
}
|
|
49
|
+
finish(result) {
|
|
50
|
+
if (this.done)
|
|
51
|
+
return;
|
|
52
|
+
this.drop();
|
|
53
|
+
const chars = this.chars;
|
|
54
|
+
if (result.ok)
|
|
55
|
+
this.report({ ok: true, source: this.source, chars: chars });
|
|
56
|
+
else
|
|
57
|
+
this.report({ source: this.source, ...result, ...(chars !== undefined ? { chars } : {}) });
|
|
58
|
+
}
|
|
59
|
+
/** Follows a write's promise. */
|
|
60
|
+
follow(write) {
|
|
61
|
+
write.then(() => this.wrote({ ok: true }), (err) => this.wrote({ ok: false, reason: "refused", message: String(err) }));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** A mounted terminal's copies. */
|
|
65
|
+
export class Copier {
|
|
66
|
+
host;
|
|
67
|
+
/** The latest release's copy, while it waits for the worker's answer. */
|
|
68
|
+
pending;
|
|
69
|
+
next = 1;
|
|
70
|
+
/** Every copy not yet reported or dropped, for `dispose`. */
|
|
71
|
+
live = new Set();
|
|
72
|
+
constructor(host) {
|
|
73
|
+
this.host = host;
|
|
74
|
+
}
|
|
75
|
+
/** Starts the copy of what a release selected, and returns its number
|
|
76
|
+
* for the worker's answer. Safari writes to the clipboard only within
|
|
77
|
+
* the user's gesture, which is over before the worker answers: the
|
|
78
|
+
* write starts here with the text to come, and fails, leaving the
|
|
79
|
+
* clipboard alone, when nothing was selected. Where the browser has no
|
|
80
|
+
* `ClipboardItem` the text is written once it arrives. An earlier
|
|
81
|
+
* release's copy still waiting is dropped, so the clipboard gets the
|
|
82
|
+
* latest. */
|
|
83
|
+
start() {
|
|
84
|
+
if (this.pending)
|
|
85
|
+
this.dropOne(this.pending);
|
|
86
|
+
const id = this.next++;
|
|
87
|
+
const pending = {
|
|
88
|
+
id,
|
|
89
|
+
// At the timeout the write is left without its text.
|
|
90
|
+
attempt: this.attempt("release", () => {
|
|
91
|
+
if (this.pending === pending)
|
|
92
|
+
this.pending = undefined;
|
|
93
|
+
pending.text?.reject();
|
|
94
|
+
}),
|
|
95
|
+
};
|
|
96
|
+
this.pending = pending;
|
|
97
|
+
const clipboard = this.host.clipboard();
|
|
98
|
+
if (!this.host.item || !clipboard?.write)
|
|
99
|
+
return id;
|
|
100
|
+
const text = new Promise((resolve, reject) => {
|
|
101
|
+
pending.text = {
|
|
102
|
+
resolve: (value) => resolve(new Blob([value], { type: "text/plain" })),
|
|
103
|
+
reject: () => reject(new Error("nothing selected")),
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
// A browser that refuses the write never reads `text`, whose
|
|
107
|
+
// rejection would then go unhandled.
|
|
108
|
+
text.catch(() => { });
|
|
109
|
+
let item;
|
|
110
|
+
try {
|
|
111
|
+
item = this.host.item(text);
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
// One whose `ClipboardItem` takes no text to come: written once
|
|
115
|
+
// the text arrives.
|
|
116
|
+
delete pending.text;
|
|
117
|
+
return id;
|
|
118
|
+
}
|
|
119
|
+
pending.attempt.follow(clipboard.write([item]));
|
|
120
|
+
return id;
|
|
121
|
+
}
|
|
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
|
+
answer(id, text) {
|
|
126
|
+
const pending = this.pending;
|
|
127
|
+
if (pending?.id !== id)
|
|
128
|
+
return;
|
|
129
|
+
this.pending = undefined;
|
|
130
|
+
if (text === undefined)
|
|
131
|
+
return this.dropOne(pending);
|
|
132
|
+
pending.attempt.text(text);
|
|
133
|
+
if (pending.text)
|
|
134
|
+
pending.text.resolve(text);
|
|
135
|
+
else
|
|
136
|
+
this.writeText(pending.attempt, text);
|
|
137
|
+
}
|
|
138
|
+
/** Copies `text` known now. */
|
|
139
|
+
write(text, source) {
|
|
140
|
+
const attempt = this.attempt(source);
|
|
141
|
+
attempt.text(text);
|
|
142
|
+
this.writeText(attempt, text);
|
|
143
|
+
}
|
|
144
|
+
/** A copy the browser made itself (the `copy` event's data). */
|
|
145
|
+
copied(text) {
|
|
146
|
+
this.host.report({ ok: true, source: "browser", chars: length(text) });
|
|
147
|
+
}
|
|
148
|
+
/** Drops every copy under way, unreported. */
|
|
149
|
+
dispose() {
|
|
150
|
+
if (this.pending)
|
|
151
|
+
this.dropOne(this.pending);
|
|
152
|
+
this.pending = undefined;
|
|
153
|
+
for (const attempt of this.live)
|
|
154
|
+
attempt.drop();
|
|
155
|
+
}
|
|
156
|
+
attempt(source, timedOut) {
|
|
157
|
+
const attempt = new Attempt(source, this.host.report, () => this.live.delete(attempt), timedOut);
|
|
158
|
+
this.live.add(attempt);
|
|
159
|
+
return attempt;
|
|
160
|
+
}
|
|
161
|
+
dropOne(pending) {
|
|
162
|
+
pending.attempt.drop();
|
|
163
|
+
pending.text?.reject();
|
|
164
|
+
}
|
|
165
|
+
writeText(attempt, text) {
|
|
166
|
+
const clipboard = this.host.clipboard();
|
|
167
|
+
if (!clipboard?.writeText)
|
|
168
|
+
return attempt.wrote({ ok: false, reason: "unavailable" });
|
|
169
|
+
attempt.follow(clipboard.writeText(text));
|
|
170
|
+
}
|
|
171
|
+
}
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,11 +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
|
|
6
|
-
export type {
|
|
7
|
-
export { Osc52Scanner } from "./osc52.ts";
|
|
8
|
-
export type { ClipboardWrite } from "./osc52.ts";
|
|
3
|
+
export { ClientError } from "./connection.ts";
|
|
4
|
+
export type { Copy, CopyFailure, CopySource } from "./copy.ts";
|
|
9
5
|
export type { Colors, MountOptions, Terminal } from "./mount.ts";
|
|
10
6
|
export type { Link, LinkEnd } from "./worker.ts";
|
|
11
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";
|
package/dist/client/index.js
CHANGED
|
@@ -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
|
+
}
|
package/dist/client/mount.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Scheme } from "../proto/index.ts";
|
|
2
|
+
import { type Copy } from "./copy.ts";
|
|
2
3
|
import type { Link, ToPage, ToWorker } from "./worker.ts";
|
|
3
4
|
/** The terminal's default colours as CSS colours (hex or `rgb()`). */
|
|
4
5
|
export interface Colors {
|
|
@@ -69,12 +70,26 @@ export interface Terminal {
|
|
|
69
70
|
onScroll(listener: (rows: number) => void): () => void;
|
|
70
71
|
/** A line of text for each change, for display; the latest at once. */
|
|
71
72
|
onStatus(listener: (text: string) => void): () => void;
|
|
73
|
+
/** Each copy to the clipboard as it ends: copied, with the text's
|
|
74
|
+
* length, or failed and why. A release that selected nothing is no
|
|
75
|
+
* copy. A listener added later is not told of earlier copies. */
|
|
76
|
+
onCopy(listener: (copy: Copy) => void): () => void;
|
|
72
77
|
}
|
|
73
|
-
/** The client's side of a terminal: its messages to the worker, and
|
|
74
|
-
*
|
|
78
|
+
/** The client's side of a terminal: its messages to the worker, and
|
|
79
|
+
* word that it is disposed. */
|
|
75
80
|
export interface MountHost {
|
|
76
81
|
send(message: ToWorker): void;
|
|
77
|
-
|
|
82
|
+
disposed(): void;
|
|
78
83
|
}
|
|
79
|
-
|
|
80
|
-
|
|
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 {};
|