@gjermundgaraba/clankermux 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +113 -31
- package/dist/client/attachment.d.ts +1 -0
- package/dist/client/attachment.js +1 -1
- package/dist/client/client.d.ts +123 -0
- package/dist/client/client.js +268 -0
- package/dist/client/connection.d.ts +17 -2
- package/dist/client/connection.js +21 -3
- package/dist/client/copy.d.ts +69 -0
- package/dist/client/copy.js +172 -0
- package/dist/client/index.d.ts +5 -3
- package/dist/client/index.js +1 -1
- package/dist/client/mount.d.ts +50 -42
- package/dist/client/mount.js +107 -104
- package/dist/client/worker.d.ts +129 -51
- package/dist/client/worker.js +462 -219
- package/dist/engine/keys.js +8 -2
- package/dist/proto/messages.d.ts +287 -5
- package/package.json +4 -3
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { ClientError } from "./connection.js";
|
|
2
|
+
import { mountOn } from "./mount.js";
|
|
3
|
+
/**
|
|
4
|
+
* Starts the worker and its connection to the server at `url`. Resolves
|
|
5
|
+
* once the engine is loaded, before the connection is up: the worker
|
|
6
|
+
* connects, and reconnects after a loss, by itself, and the client's
|
|
7
|
+
* `onLink` (and each mounted terminal's) says how it goes. Rejects when
|
|
8
|
+
* the worker or the engine cannot be loaded.
|
|
9
|
+
*/
|
|
10
|
+
export function connect(options) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
const client = new Client(options.worker, (error) => {
|
|
13
|
+
if (error === undefined)
|
|
14
|
+
resolve(client);
|
|
15
|
+
else
|
|
16
|
+
reject(error);
|
|
17
|
+
});
|
|
18
|
+
client.post({
|
|
19
|
+
type: "init",
|
|
20
|
+
wasm: new URL(options.wasm, location.href).toString(),
|
|
21
|
+
url: options.url,
|
|
22
|
+
client: options.client,
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* One connection to a server: each method is the protocol method of the
|
|
28
|
+
* same name, and rejects with a `ClientError` (the server's `code`, or
|
|
29
|
+
* `closed` when there is no connection). Terminals mount from it and share
|
|
30
|
+
* its connection.
|
|
31
|
+
*/
|
|
32
|
+
export class Client {
|
|
33
|
+
worker;
|
|
34
|
+
nextId = 1;
|
|
35
|
+
pending = new Map();
|
|
36
|
+
mounts = new Map();
|
|
37
|
+
terminals = new Set();
|
|
38
|
+
sessionListeners = new Set();
|
|
39
|
+
eventListeners = new Set();
|
|
40
|
+
linkListeners = new Set();
|
|
41
|
+
latest;
|
|
42
|
+
link;
|
|
43
|
+
started = false;
|
|
44
|
+
closed = false;
|
|
45
|
+
/** @internal Use `connect`. */
|
|
46
|
+
constructor(worker, started) {
|
|
47
|
+
this.worker = worker;
|
|
48
|
+
// A worker script that does not load posts nothing, so `connect`
|
|
49
|
+
// would never settle. Once it runs, an error event is an exception in
|
|
50
|
+
// one of its handlers, which costs that message only: the browser
|
|
51
|
+
// reports it, and the client goes on.
|
|
52
|
+
worker.onerror = (e) => {
|
|
53
|
+
if (this.started)
|
|
54
|
+
return;
|
|
55
|
+
e.preventDefault?.();
|
|
56
|
+
started(new Error(`the worker failed: ${e.message || "it did not load"}`));
|
|
57
|
+
this.close();
|
|
58
|
+
};
|
|
59
|
+
worker.onmessage = (e) => {
|
|
60
|
+
const message = e.data;
|
|
61
|
+
switch (message.type) {
|
|
62
|
+
case "ready":
|
|
63
|
+
this.started = true;
|
|
64
|
+
started();
|
|
65
|
+
break;
|
|
66
|
+
case "failed":
|
|
67
|
+
started(new Error(message.message));
|
|
68
|
+
this.close();
|
|
69
|
+
break;
|
|
70
|
+
case "reply": {
|
|
71
|
+
const waiter = this.pending.get(message.id);
|
|
72
|
+
this.pending.delete(message.id);
|
|
73
|
+
waiter?.done();
|
|
74
|
+
if (message.error)
|
|
75
|
+
waiter?.reject(errorOf(message.error));
|
|
76
|
+
else
|
|
77
|
+
waiter?.resolve(message.result);
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case "connection":
|
|
81
|
+
this.setLink(message.link);
|
|
82
|
+
break;
|
|
83
|
+
case "sessions":
|
|
84
|
+
this.latest = message.sessions;
|
|
85
|
+
for (const listener of this.sessionListeners)
|
|
86
|
+
listener(message.sessions);
|
|
87
|
+
break;
|
|
88
|
+
case "event":
|
|
89
|
+
for (const listener of this.eventListeners)
|
|
90
|
+
listener(message.event);
|
|
91
|
+
break;
|
|
92
|
+
case "gap":
|
|
93
|
+
for (const listener of this.eventListeners)
|
|
94
|
+
listener({ event: "gap", incarnation: message.incarnation });
|
|
95
|
+
break;
|
|
96
|
+
default:
|
|
97
|
+
this.mounts.get(message.mount)?.(message);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
setLink(link) {
|
|
102
|
+
this.link = link;
|
|
103
|
+
for (const listener of this.linkListeners)
|
|
104
|
+
listener(link);
|
|
105
|
+
}
|
|
106
|
+
/** @internal */
|
|
107
|
+
post(message) {
|
|
108
|
+
if (!this.closed)
|
|
109
|
+
this.worker.postMessage(message);
|
|
110
|
+
}
|
|
111
|
+
call(method, params, signal) {
|
|
112
|
+
if (this.closed)
|
|
113
|
+
return Promise.reject(new ClientError("closed", "closed"));
|
|
114
|
+
const id = this.nextId++;
|
|
115
|
+
return new Promise((resolve, reject) => {
|
|
116
|
+
// Cancelled by the server, so the rejection is its `cancelled`,
|
|
117
|
+
// also when the signal was aborted already.
|
|
118
|
+
const cancel = () => this.post({ type: "cancel", id });
|
|
119
|
+
const done = () => signal?.removeEventListener("abort", cancel);
|
|
120
|
+
this.pending.set(id, { resolve: resolve, reject, done });
|
|
121
|
+
this.post({ type: "request", id, method, params });
|
|
122
|
+
if (signal?.aborted)
|
|
123
|
+
cancel();
|
|
124
|
+
else
|
|
125
|
+
signal?.addEventListener("abort", cancel, { once: true });
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
describe(method) {
|
|
129
|
+
return this.call("describe", method === undefined ? {} : { method });
|
|
130
|
+
}
|
|
131
|
+
create(params = {}) {
|
|
132
|
+
return this.call("create", params);
|
|
133
|
+
}
|
|
134
|
+
/** Every session, `list` paged to its end. */
|
|
135
|
+
async sessions() {
|
|
136
|
+
const all = [];
|
|
137
|
+
for (let page = await this.list();; page = await this.list(page.next)) {
|
|
138
|
+
all.push(...page.sessions);
|
|
139
|
+
if (page.next === undefined)
|
|
140
|
+
return all;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/** One page of sessions, after the session id `after`. */
|
|
144
|
+
list(after) {
|
|
145
|
+
return this.call("list", after === undefined ? {} : { after });
|
|
146
|
+
}
|
|
147
|
+
inspect(session) {
|
|
148
|
+
return this.call("inspect", { session });
|
|
149
|
+
}
|
|
150
|
+
/** Hangs up the session's processes (SIGHUP). */
|
|
151
|
+
async end(session) {
|
|
152
|
+
await this.call("end", { session });
|
|
153
|
+
}
|
|
154
|
+
/** Sends `signal` (`HUP`, `INT`, `TERM`, `KILL`, ...) to the session's processes. */
|
|
155
|
+
async signal(session, signal) {
|
|
156
|
+
await this.call("signal", { session, signal });
|
|
157
|
+
}
|
|
158
|
+
/** Removes a session that is `exited` or `restored`. */
|
|
159
|
+
async remove(session) {
|
|
160
|
+
await this.call("remove", { session });
|
|
161
|
+
}
|
|
162
|
+
label(session, label) {
|
|
163
|
+
return this.call("label", { session, label });
|
|
164
|
+
}
|
|
165
|
+
/** Sets the size, the size policy, or both. */
|
|
166
|
+
async resize(session, size) {
|
|
167
|
+
await this.call("resize", { session, ...size });
|
|
168
|
+
}
|
|
169
|
+
/** Text, keys or bytes to the session's program, with a wait armed
|
|
170
|
+
* before they are written when `input.wait` is given; aborting
|
|
171
|
+
* `options.signal` cancels that wait. */
|
|
172
|
+
send(session, input, options = {}) {
|
|
173
|
+
return this.call("send", { session, ...input }, options.signal);
|
|
174
|
+
}
|
|
175
|
+
/** Waits on the server until one of `until` holds, or the timeout,
|
|
176
|
+
* which is a result. */
|
|
177
|
+
wait(session, until, options = {}) {
|
|
178
|
+
const { signal, after, timeout_ms } = options;
|
|
179
|
+
return this.call("wait", { session, until, ...(after !== undefined && { after }), ...(timeout_ms !== undefined && { timeout_ms }) }, signal);
|
|
180
|
+
}
|
|
181
|
+
/** The screen, or the scope and format asked for. */
|
|
182
|
+
read(session, options = {}) {
|
|
183
|
+
return this.call("read", { session, ...options });
|
|
184
|
+
}
|
|
185
|
+
/** The stream as text, from the offset `after`. */
|
|
186
|
+
tail(session, after, options = {}) {
|
|
187
|
+
return this.call("tail", { session, after, ...options });
|
|
188
|
+
}
|
|
189
|
+
/** Marks the session's `done` and `error` records seen: all, or those named. */
|
|
190
|
+
ack(session, recordIds) {
|
|
191
|
+
return this.call("ack", { session, ...(recordIds !== undefined && { record_ids: recordIds }) });
|
|
192
|
+
}
|
|
193
|
+
/** Ends another attachment, by its `channel` in the session's `attachments`. */
|
|
194
|
+
async detach(session, channel, reason) {
|
|
195
|
+
await this.call("detach", { session, channel, ...(reason !== undefined && { reason }) });
|
|
196
|
+
}
|
|
197
|
+
/** The connection's link, at each change: `connecting` with its
|
|
198
|
+
* attempt, `connected` (with no session), or `ended`, as a terminal's
|
|
199
|
+
* `onLink` reports the connection-level changes. Requests need
|
|
200
|
+
* `connected`. A listener added later gets the latest at once. */
|
|
201
|
+
onLink(listener) {
|
|
202
|
+
this.linkListeners.add(listener);
|
|
203
|
+
if (this.link)
|
|
204
|
+
listener(this.link);
|
|
205
|
+
return () => void this.linkListeners.delete(listener);
|
|
206
|
+
}
|
|
207
|
+
/** Every session on the server, at each change; a listener added once
|
|
208
|
+
* the list is known gets it at once. */
|
|
209
|
+
onSessions(listener) {
|
|
210
|
+
this.sessionListeners.add(listener);
|
|
211
|
+
if (this.latest)
|
|
212
|
+
listener(this.latest);
|
|
213
|
+
return () => void this.sessionListeners.delete(listener);
|
|
214
|
+
}
|
|
215
|
+
/** Each server event, in `seq` order, without loss across reconnects:
|
|
216
|
+
* the ones missed while the connection was down are replayed, or an
|
|
217
|
+
* `EventGap` says they cannot be. */
|
|
218
|
+
onEvent(listener) {
|
|
219
|
+
this.eventListeners.add(listener);
|
|
220
|
+
return () => void this.eventListeners.delete(listener);
|
|
221
|
+
}
|
|
222
|
+
/** Mounts a terminal on `options.canvas`, attached to `options.session`
|
|
223
|
+
* if given, on this client's connection. */
|
|
224
|
+
mount(options) {
|
|
225
|
+
if (this.closed)
|
|
226
|
+
throw new ClientError("closed", "closed");
|
|
227
|
+
const id = this.nextId++;
|
|
228
|
+
const host = {
|
|
229
|
+
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();
|
|
242
|
+
};
|
|
243
|
+
this.terminals.add(terminal);
|
|
244
|
+
return terminal;
|
|
245
|
+
}
|
|
246
|
+
/** Disposes the terminals and terminates the worker, which ends the
|
|
247
|
+
* connection; requests in flight reject with `closed`. */
|
|
248
|
+
close() {
|
|
249
|
+
if (this.closed)
|
|
250
|
+
return;
|
|
251
|
+
for (const terminal of [...this.terminals])
|
|
252
|
+
terminal.dispose();
|
|
253
|
+
this.closed = true;
|
|
254
|
+
this.worker.onmessage = null;
|
|
255
|
+
this.worker.terminate();
|
|
256
|
+
for (const waiter of this.pending.values()) {
|
|
257
|
+
waiter.done();
|
|
258
|
+
waiter.reject(new ClientError("closed", "closed"));
|
|
259
|
+
}
|
|
260
|
+
this.pending.clear();
|
|
261
|
+
this.sessionListeners.clear();
|
|
262
|
+
this.eventListeners.clear();
|
|
263
|
+
this.linkListeners.clear();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function errorOf(failure) {
|
|
267
|
+
return new ClientError(failure.kind, failure.message, failure.server);
|
|
268
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ErrorMessage, type Event, type Frame, FrameType, type Hello, type Welcome } from "../proto/index.ts";
|
|
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
2
|
/** A message-oriented byte transport: one frame per message, as over WebSocket. */
|
|
3
3
|
export interface Transport {
|
|
4
4
|
send(bytes: Uint8Array): void;
|
|
@@ -9,15 +9,22 @@ export interface Transport {
|
|
|
9
9
|
/** `desync`: a stream frame did not continue from the cursor; a fresh
|
|
10
10
|
* OPEN recovers it, where any other fault would only repeat. */
|
|
11
11
|
export type ErrorKind = "protocol" | "desync" | "server" | "closed";
|
|
12
|
+
/** A failed request or connection. `kind: "server"` carries the server's
|
|
13
|
+
* `ERROR` as `server`, and its code as `code`: a refused request, or a
|
|
14
|
+
* `wait` that was cancelled (`cancelled`). */
|
|
12
15
|
export declare class ClientError extends Error {
|
|
13
16
|
readonly kind: ErrorKind;
|
|
14
17
|
readonly server?: ErrorMessage | undefined;
|
|
18
|
+
/** The protocol's error code, when the server sent one. */
|
|
19
|
+
readonly code: ErrorCode | undefined;
|
|
15
20
|
constructor(kind: ErrorKind, message: string, server?: ErrorMessage | undefined);
|
|
16
21
|
static protocol(message: string): ClientError;
|
|
17
22
|
static desync(message: string): ClientError;
|
|
18
23
|
static fromServer(error: ErrorMessage): ClientError;
|
|
19
24
|
}
|
|
20
|
-
|
|
25
|
+
/** `HELLO` for `client`: a name and version, or a name with this
|
|
26
|
+
* package's version. */
|
|
27
|
+
export declare function hello(client: string | Peer, engine?: string): Hello;
|
|
21
28
|
/**
|
|
22
29
|
* One protocol connection: the handshake, requests answered by id, events,
|
|
23
30
|
* and attachment channels routed to their receivers.
|
|
@@ -40,6 +47,14 @@ export declare class Connection {
|
|
|
40
47
|
sendJson(type: FrameType, channel: number, body: unknown): void;
|
|
41
48
|
/** A request; the reply's `result`, or a rejection with the server's error. */
|
|
42
49
|
call<R>(method: string, params: unknown): Promise<R>;
|
|
50
|
+
/** A request and its id on this connection, which `cancel` takes. */
|
|
51
|
+
request<R>(method: string, params: unknown): {
|
|
52
|
+
id: number;
|
|
53
|
+
reply: Promise<R>;
|
|
54
|
+
};
|
|
55
|
+
/** The server's catalog (`describe {}`), or one method of it. */
|
|
56
|
+
describe(): Promise<Catalog>;
|
|
57
|
+
describe(method: string): Promise<Description>;
|
|
43
58
|
/** Opens `channel`; its frames go to `receive` until `release` or the
|
|
44
59
|
* connection's end. */
|
|
45
60
|
open(channel: number, open: unknown, receive: (frame: Frame) => void): void;
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import { FrameError, FrameType, encodeFrame, parseFrame, VERSION, } from "../proto/index.js";
|
|
2
2
|
import pkg from "../../package.json" with { type: "json" };
|
|
3
|
+
/** A failed request or connection. `kind: "server"` carries the server's
|
|
4
|
+
* `ERROR` as `server`, and its code as `code`: a refused request, or a
|
|
5
|
+
* `wait` that was cancelled (`cancelled`). */
|
|
3
6
|
export class ClientError extends Error {
|
|
4
7
|
kind;
|
|
5
8
|
server;
|
|
9
|
+
/** The protocol's error code, when the server sent one. */
|
|
10
|
+
code;
|
|
6
11
|
constructor(kind, message, server) {
|
|
7
12
|
super(message);
|
|
8
13
|
this.kind = kind;
|
|
9
14
|
this.server = server;
|
|
15
|
+
this.name = "ClientError";
|
|
16
|
+
this.code = server?.code;
|
|
10
17
|
}
|
|
11
18
|
static protocol(message) {
|
|
12
19
|
return new ClientError("protocol", message);
|
|
@@ -21,8 +28,11 @@ export class ClientError extends Error {
|
|
|
21
28
|
/** Frames before `WELCOME` tells us the real limit: the protocol's
|
|
22
29
|
* default `max_frame`, as the Rust client. */
|
|
23
30
|
const HELLO_MAX_FRAME = 1024 * 1024;
|
|
24
|
-
|
|
25
|
-
|
|
31
|
+
/** `HELLO` for `client`: a name and version, or a name with this
|
|
32
|
+
* package's version. */
|
|
33
|
+
export function hello(client, engine) {
|
|
34
|
+
const peer = typeof client === "string" ? { name: client, version: pkg.version } : client;
|
|
35
|
+
const h = { version: VERSION, client: peer, capabilities: [] };
|
|
26
36
|
if (engine !== undefined)
|
|
27
37
|
h.engine = engine;
|
|
28
38
|
return h;
|
|
@@ -83,8 +93,12 @@ export class Connection {
|
|
|
83
93
|
}
|
|
84
94
|
/** A request; the reply's `result`, or a rejection with the server's error. */
|
|
85
95
|
call(method, params) {
|
|
96
|
+
return this.request(method, params).reply;
|
|
97
|
+
}
|
|
98
|
+
/** A request and its id on this connection, which `cancel` takes. */
|
|
99
|
+
request(method, params) {
|
|
86
100
|
const id = this.nextId++;
|
|
87
|
-
|
|
101
|
+
const reply = new Promise((resolve, reject) => {
|
|
88
102
|
this.pending.set(id, { resolve: resolve, reject });
|
|
89
103
|
try {
|
|
90
104
|
this.sendJson(FrameType.Request, 0, { id, method, params });
|
|
@@ -94,6 +108,10 @@ export class Connection {
|
|
|
94
108
|
reject(err);
|
|
95
109
|
}
|
|
96
110
|
});
|
|
111
|
+
return { id, reply };
|
|
112
|
+
}
|
|
113
|
+
describe(method) {
|
|
114
|
+
return this.call("describe", method === undefined ? {} : { method });
|
|
97
115
|
}
|
|
98
116
|
/** Opens `channel`; its frames go to `receive` until `release` or the
|
|
99
117
|
* connection's end. */
|
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
/** How long a copy may take: the worker's answer and the write. */
|
|
36
|
+
export declare const COPY_TIMEOUT_MS = 2000;
|
|
37
|
+
/** A mounted terminal's copies. */
|
|
38
|
+
export declare class Copier {
|
|
39
|
+
private readonly host;
|
|
40
|
+
private readonly timeoutMs;
|
|
41
|
+
/** The releases' copies waiting for the worker's answer, by number. */
|
|
42
|
+
private readonly pending;
|
|
43
|
+
private next;
|
|
44
|
+
/** Every copy not yet reported or dropped, for `dispose`. */
|
|
45
|
+
private readonly live;
|
|
46
|
+
constructor(host: CopyHost, timeoutMs?: number);
|
|
47
|
+
/** Starts the copy of what a release selected, and returns its number
|
|
48
|
+
* for the worker's answer. Safari writes to the clipboard only within
|
|
49
|
+
* the user's gesture, which is over before the worker answers: the
|
|
50
|
+
* write starts here with the text to come, and fails, leaving the
|
|
51
|
+
* clipboard alone, when nothing was selected. Where the browser has no
|
|
52
|
+
* `ClipboardItem` the text is written once it arrives. An earlier
|
|
53
|
+
* release's copy still waiting is dropped, so the clipboard gets the
|
|
54
|
+
* latest. */
|
|
55
|
+
start(): number;
|
|
56
|
+
/** The worker's answer to release `id`: the selected text, or undefined
|
|
57
|
+
* when nothing was selected, which is no copy. An answer to a copy that
|
|
58
|
+
* was dropped or timed out is ignored. */
|
|
59
|
+
answer(id: number, text: string | undefined): void;
|
|
60
|
+
/** Copies `text` known now. */
|
|
61
|
+
write(text: string, source: CopySource): void;
|
|
62
|
+
/** A copy the browser made itself (the `copy` event's data). */
|
|
63
|
+
copied(text: string, source: CopySource): void;
|
|
64
|
+
/** Drops every copy under way, unreported. */
|
|
65
|
+
dispose(): void;
|
|
66
|
+
private attempt;
|
|
67
|
+
private dropOne;
|
|
68
|
+
private writeText;
|
|
69
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
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
|
+
export 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, timeoutMs,
|
|
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
|
+
}, timeoutMs);
|
|
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
|
+
timeoutMs;
|
|
68
|
+
/** The releases' copies waiting for the worker's answer, by number. */
|
|
69
|
+
pending = new Map();
|
|
70
|
+
next = 1;
|
|
71
|
+
/** Every copy not yet reported or dropped, for `dispose`. */
|
|
72
|
+
live = new Set();
|
|
73
|
+
constructor(host, timeoutMs = COPY_TIMEOUT_MS) {
|
|
74
|
+
this.host = host;
|
|
75
|
+
this.timeoutMs = timeoutMs;
|
|
76
|
+
}
|
|
77
|
+
/** Starts the copy of what a release selected, and returns its number
|
|
78
|
+
* for the worker's answer. Safari writes to the clipboard only within
|
|
79
|
+
* the user's gesture, which is over before the worker answers: the
|
|
80
|
+
* write starts here with the text to come, and fails, leaving the
|
|
81
|
+
* clipboard alone, when nothing was selected. Where the browser has no
|
|
82
|
+
* `ClipboardItem` the text is written once it arrives. An earlier
|
|
83
|
+
* release's copy still waiting is dropped, so the clipboard gets the
|
|
84
|
+
* latest. */
|
|
85
|
+
start() {
|
|
86
|
+
for (const earlier of this.pending.values())
|
|
87
|
+
this.dropOne(earlier);
|
|
88
|
+
this.pending.clear();
|
|
89
|
+
const id = this.next++;
|
|
90
|
+
const pending = {
|
|
91
|
+
// At the timeout the write is left without its text.
|
|
92
|
+
attempt: this.attempt("release", () => {
|
|
93
|
+
this.pending.delete(id);
|
|
94
|
+
pending.text?.reject();
|
|
95
|
+
}),
|
|
96
|
+
};
|
|
97
|
+
this.pending.set(id, pending);
|
|
98
|
+
const clipboard = this.host.clipboard();
|
|
99
|
+
if (!this.host.item || !clipboard?.write)
|
|
100
|
+
return id;
|
|
101
|
+
const text = new Promise((resolve, reject) => {
|
|
102
|
+
pending.text = {
|
|
103
|
+
resolve: (value) => resolve(new Blob([value], { type: "text/plain" })),
|
|
104
|
+
reject: () => reject(new Error("nothing selected")),
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
// A browser that refuses the write never reads `text`, whose
|
|
108
|
+
// rejection would then go unhandled.
|
|
109
|
+
text.catch(() => { });
|
|
110
|
+
let item;
|
|
111
|
+
try {
|
|
112
|
+
item = this.host.item(text);
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
// One whose `ClipboardItem` takes no text to come: written once
|
|
116
|
+
// the text arrives.
|
|
117
|
+
delete pending.text;
|
|
118
|
+
return id;
|
|
119
|
+
}
|
|
120
|
+
pending.attempt.follow(clipboard.write([item]));
|
|
121
|
+
return id;
|
|
122
|
+
}
|
|
123
|
+
/** The worker's answer to release `id`: the selected text, or undefined
|
|
124
|
+
* when nothing was selected, which is no copy. An answer to a copy that
|
|
125
|
+
* was dropped or timed out is ignored. */
|
|
126
|
+
answer(id, text) {
|
|
127
|
+
const pending = this.pending.get(id);
|
|
128
|
+
if (!pending)
|
|
129
|
+
return;
|
|
130
|
+
this.pending.delete(id);
|
|
131
|
+
if (text === undefined)
|
|
132
|
+
return this.dropOne(pending);
|
|
133
|
+
pending.attempt.text(text);
|
|
134
|
+
if (pending.text)
|
|
135
|
+
pending.text.resolve(text);
|
|
136
|
+
else
|
|
137
|
+
this.writeText(pending.attempt, text);
|
|
138
|
+
}
|
|
139
|
+
/** Copies `text` known now. */
|
|
140
|
+
write(text, source) {
|
|
141
|
+
const attempt = this.attempt(source);
|
|
142
|
+
attempt.text(text);
|
|
143
|
+
this.writeText(attempt, text);
|
|
144
|
+
}
|
|
145
|
+
/** 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) });
|
|
148
|
+
}
|
|
149
|
+
/** Drops every copy under way, unreported. */
|
|
150
|
+
dispose() {
|
|
151
|
+
for (const pending of this.pending.values())
|
|
152
|
+
this.dropOne(pending);
|
|
153
|
+
this.pending.clear();
|
|
154
|
+
for (const attempt of this.live)
|
|
155
|
+
attempt.drop();
|
|
156
|
+
}
|
|
157
|
+
attempt(source, timedOut) {
|
|
158
|
+
const attempt = new Attempt(source, this.host.report, this.timeoutMs, () => this.live.delete(attempt), timedOut);
|
|
159
|
+
this.live.add(attempt);
|
|
160
|
+
return attempt;
|
|
161
|
+
}
|
|
162
|
+
dropOne(pending) {
|
|
163
|
+
pending.attempt.drop();
|
|
164
|
+
pending.text?.reject();
|
|
165
|
+
}
|
|
166
|
+
writeText(attempt, text) {
|
|
167
|
+
const clipboard = this.host.clipboard();
|
|
168
|
+
if (!clipboard?.writeText)
|
|
169
|
+
return attempt.wrote({ ok: false, reason: "unavailable" });
|
|
170
|
+
attempt.follow(clipboard.writeText(text));
|
|
171
|
+
}
|
|
172
|
+
}
|
package/dist/client/index.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
export { Attachment } from "./attachment.ts";
|
|
2
2
|
export type { Action } from "./attachment.ts";
|
|
3
|
+
export { Client, connect } from "./client.ts";
|
|
4
|
+
export type { ConnectOptions, EventGap, SendInput, WaitOptions } from "./client.ts";
|
|
3
5
|
export { ClientError, Connection, hello } from "./connection.ts";
|
|
4
6
|
export type { ErrorKind, Transport } from "./connection.ts";
|
|
7
|
+
export type { Copy, CopyFailure, CopySource } from "./copy.ts";
|
|
5
8
|
export { Osc52Scanner } from "./osc52.ts";
|
|
6
9
|
export type { ClipboardWrite } from "./osc52.ts";
|
|
7
|
-
export {
|
|
8
|
-
export type { Colors, CreateOptions, MountOptions, MountedTerminal } from "./mount.ts";
|
|
10
|
+
export type { Colors, MountOptions, Terminal } from "./mount.ts";
|
|
9
11
|
export type { Link, LinkEnd } from "./worker.ts";
|
|
10
|
-
export type { ErrorCode, Exit, OnExit, Scheme, Session } from "../proto/index.ts";
|
|
12
|
+
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