@parcha/agentrun-computer 0.0.0-stage → 0.1.0-beta.33
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/LICENSE +202 -0
- package/NOTICE +8 -0
- package/README.md +2411 -2
- package/box/Dockerfile +69 -0
- package/box/agentrun-archil +168 -0
- package/box/build-daytona.py +26 -0
- package/box/build-e2b.mjs +32 -0
- package/box/build-modal.py +17 -0
- package/dist/actions.d.ts +18 -0
- package/dist/actions.js +139 -0
- package/dist/browser.d.ts +151 -0
- package/dist/browser.js +305 -0
- package/dist/browserbase.d.ts +139 -0
- package/dist/browserbase.js +408 -0
- package/dist/child.d.ts +40 -0
- package/dist/child.js +127 -0
- package/dist/client.d.ts +22 -0
- package/dist/client.js +136 -0
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/conformance/archil.d.ts +68 -0
- package/dist/conformance/archil.js +152 -0
- package/dist/conformance/browserbase.d.ts +35 -0
- package/dist/conformance/browserbase.js +45 -0
- package/dist/conformance/cases/apps.d.ts +2 -0
- package/dist/conformance/cases/apps.js +155 -0
- package/dist/conformance/cases/browser.d.ts +4 -0
- package/dist/conformance/cases/browser.js +105 -0
- package/dist/conformance/cases/drift.d.ts +2 -0
- package/dist/conformance/cases/drift.js +64 -0
- package/dist/conformance/cases/exec.d.ts +2 -0
- package/dist/conformance/cases/exec.js +125 -0
- package/dist/conformance/cases/files.d.ts +2 -0
- package/dist/conformance/cases/files.js +294 -0
- package/dist/conformance/cases/health.d.ts +2 -0
- package/dist/conformance/cases/health.js +71 -0
- package/dist/conformance/cases/index.d.ts +9 -0
- package/dist/conformance/cases/index.js +37 -0
- package/dist/conformance/cases/input.d.ts +12 -0
- package/dist/conformance/cases/input.js +109 -0
- package/dist/conformance/cases/isolation.d.ts +21 -0
- package/dist/conformance/cases/isolation.js +98 -0
- package/dist/conformance/cases/lifetime.d.ts +2 -0
- package/dist/conformance/cases/lifetime.js +64 -0
- package/dist/conformance/cases/mount.d.ts +2 -0
- package/dist/conformance/cases/mount.js +252 -0
- package/dist/conformance/cases/process.d.ts +2 -0
- package/dist/conformance/cases/process.js +240 -0
- package/dist/conformance/cases/provider.d.ts +2 -0
- package/dist/conformance/cases/provider.js +302 -0
- package/dist/conformance/cases/screen.d.ts +9 -0
- package/dist/conformance/cases/screen.js +230 -0
- package/dist/conformance/cases/settings.d.ts +57 -0
- package/dist/conformance/cases/settings.js +142 -0
- package/dist/conformance/cases/touch.d.ts +2 -0
- package/dist/conformance/cases/touch.js +117 -0
- package/dist/conformance/cases/util.d.ts +7 -0
- package/dist/conformance/cases/util.js +8 -0
- package/dist/conformance/cdp.d.ts +13 -0
- package/dist/conformance/cdp.js +88 -0
- package/dist/conformance/cli.d.ts +29 -0
- package/dist/conformance/cli.js +231 -0
- package/dist/conformance/daytona-sweep.d.ts +18 -0
- package/dist/conformance/daytona-sweep.js +126 -0
- package/dist/conformance/daytona.d.ts +83 -0
- package/dist/conformance/daytona.js +107 -0
- package/dist/conformance/docker.d.ts +38 -0
- package/dist/conformance/docker.js +70 -0
- package/dist/conformance/drift-daemon.d.ts +8 -0
- package/dist/conformance/drift-daemon.js +41 -0
- package/dist/conformance/e2b-diagnose.d.ts +16 -0
- package/dist/conformance/e2b-diagnose.js +61 -0
- package/dist/conformance/e2b-sweep.d.ts +18 -0
- package/dist/conformance/e2b-sweep.js +132 -0
- package/dist/conformance/e2b.d.ts +74 -0
- package/dist/conformance/e2b.js +111 -0
- package/dist/conformance/fixture.d.ts +65 -0
- package/dist/conformance/fixture.js +98 -0
- package/dist/conformance/index.d.ts +8 -0
- package/dist/conformance/index.js +8 -0
- package/dist/conformance/kernel.d.ts +31 -0
- package/dist/conformance/kernel.js +40 -0
- package/dist/conformance/kind.d.ts +23 -0
- package/dist/conformance/kind.js +58 -0
- package/dist/conformance/modal-sweep.d.ts +22 -0
- package/dist/conformance/modal-sweep.js +213 -0
- package/dist/conformance/modal.d.ts +69 -0
- package/dist/conformance/modal.js +76 -0
- package/dist/conformance/orgo.d.ts +41 -0
- package/dist/conformance/orgo.js +69 -0
- package/dist/conformance/phone-harness.d.ts +61 -0
- package/dist/conformance/phone-harness.js +241 -0
- package/dist/conformance/png.d.ts +16 -0
- package/dist/conformance/png.js +132 -0
- package/dist/conformance/report.d.ts +2 -0
- package/dist/conformance/report.js +86 -0
- package/dist/conformance/runner.d.ts +11 -0
- package/dist/conformance/runner.js +585 -0
- package/dist/conformance/tar.d.ts +13 -0
- package/dist/conformance/tar.js +105 -0
- package/dist/conformance/types.d.ts +257 -0
- package/dist/conformance/types.js +24 -0
- package/dist/daemon-copy.d.ts +40 -0
- package/dist/daemon-copy.js +259 -0
- package/dist/daytona.d.ts +180 -0
- package/dist/daytona.js +1498 -0
- package/dist/desktop.d.ts +24 -0
- package/dist/desktop.js +68 -0
- package/dist/docker/egress-main.d.ts +1 -0
- package/dist/docker/egress-main.js +68 -0
- package/dist/docker/egress-proxy.d.ts +53 -0
- package/dist/docker/egress-proxy.js +450 -0
- package/dist/docker/egress-table.d.ts +39 -0
- package/dist/docker/egress-table.js +197 -0
- package/dist/docker/egress.d.ts +109 -0
- package/dist/docker/egress.js +318 -0
- package/dist/docker/image.d.ts +49 -0
- package/dist/docker/image.js +302 -0
- package/dist/docker/stage.d.ts +21 -0
- package/dist/docker/stage.js +290 -0
- package/dist/docker/volume.d.ts +24 -0
- package/dist/docker/volume.js +67 -0
- package/dist/docker.d.ts +156 -0
- package/dist/docker.js +1277 -0
- package/dist/drift.d.ts +200 -0
- package/dist/drift.js +416 -0
- package/dist/e2b.d.ts +337 -0
- package/dist/e2b.js +1516 -0
- package/dist/entry.d.ts +2 -0
- package/dist/entry.js +24 -0
- package/dist/files.d.ts +50 -0
- package/dist/files.js +208 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +26 -0
- package/dist/isolation.d.ts +29 -0
- package/dist/isolation.js +71 -0
- package/dist/janitor.d.ts +50 -0
- package/dist/janitor.js +135 -0
- package/dist/kernel.d.ts +113 -0
- package/dist/kernel.js +358 -0
- package/dist/kinds.d.ts +79 -0
- package/dist/kinds.js +133 -0
- package/dist/labels.d.ts +23 -0
- package/dist/labels.js +50 -0
- package/dist/modal.d.ts +302 -0
- package/dist/modal.js +1374 -0
- package/dist/mount-archil.d.ts +164 -0
- package/dist/mount-archil.js +471 -0
- package/dist/mount.d.ts +57 -0
- package/dist/mount.js +104 -0
- package/dist/orgo-channel.d.ts +96 -0
- package/dist/orgo-channel.js +391 -0
- package/dist/orgo.d.ts +139 -0
- package/dist/orgo.js +1006 -0
- package/dist/phone-harness.d.ts +185 -0
- package/dist/phone-harness.js +1420 -0
- package/dist/processes.d.ts +24 -0
- package/dist/processes.js +40 -0
- package/dist/protocol.d.ts +729 -0
- package/dist/protocol.js +54 -0
- package/dist/provider.d.ts +245 -0
- package/dist/provider.js +37 -0
- package/dist/providers.d.ts +68 -0
- package/dist/providers.js +25 -0
- package/dist/result.d.ts +15 -0
- package/dist/result.js +2 -0
- package/dist/screen-watch.d.ts +150 -0
- package/dist/screen-watch.js +490 -0
- package/dist/test-connection.d.ts +17 -0
- package/dist/test-connection.js +26 -0
- package/dist/wait.d.ts +23 -0
- package/dist/wait.js +65 -0
- package/package.json +62 -4
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
// A computer's live view, in two layers.
|
|
2
|
+
//
|
|
3
|
+
// watchScreen(connection, options) is the internal seam: the screen as pull-paced frames and an input sink on one
|
|
4
|
+
// connection. screen.watch asks for the frame after the last one given, and the next ask is made only when the caller
|
|
5
|
+
// calls next() again, so each next() is the ack of the frame before it. act() sends input.actions on the same
|
|
6
|
+
// connection. close() and the caller's signal end both: the waiting ask is cancelled, the frames end, and act()
|
|
7
|
+
// refuses in words. A computer that fails makes next() reject with a ScreenWatchError.
|
|
8
|
+
//
|
|
9
|
+
// provider.watch(ref, handlers, options) is what the runner's viewer calls (tl-browser's handler shape, final), built
|
|
10
|
+
// once, by daemonWatch, as an adapter over watchScreen: each frame goes to handlers.frame(frame, ack), and the next is
|
|
11
|
+
// asked for only once ack() is called; a computer that goes ends it once with handlers.ended(error), its code gone,
|
|
12
|
+
// unreachable, refused or unsupported; close() and the signal end it with no ended(). Every computerd provider and the
|
|
13
|
+
// phone answer watch() through daemonWatch, on a connection of the watch's own: computerd gives JPEG frames, the phone
|
|
14
|
+
// client PNG ones. No error in either layer holds a URL or a secret.
|
|
15
|
+
// The most characters one typed text holds, on every kind: computerd's MaxTypeRunes, which checkActions enforces too.
|
|
16
|
+
import { MAX_TYPE_CHARACTERS } from "./actions.js";
|
|
17
|
+
import { fail, ok } from "./result.js";
|
|
18
|
+
/** Frames that failed: the code, and words with no URL or secret. */
|
|
19
|
+
export class ScreenWatchError extends Error {
|
|
20
|
+
code;
|
|
21
|
+
constructor(code, message) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.code = code;
|
|
24
|
+
this.name = "ScreenWatchError";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** The largest frame a watch reads: a frame that says more is refused before any of it is read. */
|
|
28
|
+
const MAX_FRAME_BYTES = 64 << 20;
|
|
29
|
+
/** The most input calls a watch has in flight at once: one more is refused as limit, never queued. */
|
|
30
|
+
const MAX_INPUTS_IN_FLIGHT = 8;
|
|
31
|
+
/**
|
|
32
|
+
* Words with no URL and no secret: a URL of any scheme, a login, a bearer, a secret-named parameter, a key- or
|
|
33
|
+
* token-shaped value and an endpoint (a host or an IPv4 address with a port) are taken out.
|
|
34
|
+
*/
|
|
35
|
+
export function redactWords(words) {
|
|
36
|
+
return (words
|
|
37
|
+
.replace(/\b[a-z][a-z0-9+.-]*:\/\/[^\s"'<>]+/gi, "[a URL]")
|
|
38
|
+
// A login in a host's place: user:password@host.
|
|
39
|
+
.replace(/[\w.+-]+:[^\s@/'"]+@[\w.-]+/g, "[a login]")
|
|
40
|
+
.replace(/\b(?:Bearer|Basic)\s+[A-Za-z0-9._~+/=-]+/gi, "[a secret]")
|
|
41
|
+
// A secret-named parameter, its name with a prefix or suffix too: api_key=, access_token=, client_secret=.
|
|
42
|
+
.replace(/\b[\w-]*(?:token|password|passwd|secret|key|jwt|auth|code|signingKey|credential)[\w-]*=[^\s&"';,]+/gi, "[a secret]")
|
|
43
|
+
.replace(/\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9._-]*/g, "[a secret]")
|
|
44
|
+
.replace(/\b(?:sk|pk|rk)-[A-Za-z0-9_-]{8,}/g, "[a secret]")
|
|
45
|
+
.replace(/\bgh[pousr]_[A-Za-z0-9]{16,}/g, "[a secret]")
|
|
46
|
+
.replace(/\b(?:sk|pk|pck|ph|tok|pw|jwt)_[A-Za-z0-9_-]{6,}/g, "[a secret]")
|
|
47
|
+
// An endpoint: a DNS name or an IPv4 address with a port, as adb names a phone's.
|
|
48
|
+
.replace(/\b(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,}:\d{1,5}\b/g, "[an endpoint]")
|
|
49
|
+
.replace(/\b\d{1,3}(?:\.\d{1,3}){3}(?::\d{1,5})?\b/g, "[an endpoint]"));
|
|
50
|
+
}
|
|
51
|
+
/** The code a watch ends with for an error the computer gave. */
|
|
52
|
+
export function watchEndCode(code) {
|
|
53
|
+
switch (code) {
|
|
54
|
+
case "gone":
|
|
55
|
+
case "not_found":
|
|
56
|
+
return "gone";
|
|
57
|
+
case "unsupported":
|
|
58
|
+
return "unsupported";
|
|
59
|
+
case "refused":
|
|
60
|
+
case "denied":
|
|
61
|
+
case "unauthorized":
|
|
62
|
+
case "invalid":
|
|
63
|
+
case "limit":
|
|
64
|
+
return "refused";
|
|
65
|
+
default:
|
|
66
|
+
return "unreachable";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** The screen of the computer on `connection`, as pull-paced frames and an input sink (see the top of this file). */
|
|
70
|
+
export function watchScreen(connection, options = {}) {
|
|
71
|
+
const { client } = connection;
|
|
72
|
+
const stop = new AbortController();
|
|
73
|
+
let ended;
|
|
74
|
+
const end = (why) => {
|
|
75
|
+
ended ??= why;
|
|
76
|
+
stop.abort();
|
|
77
|
+
};
|
|
78
|
+
const onAbort = () => end("aborted");
|
|
79
|
+
if (options.signal?.aborted)
|
|
80
|
+
end("aborted");
|
|
81
|
+
else
|
|
82
|
+
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
83
|
+
const params = {
|
|
84
|
+
...(options.maxWidth === undefined ? {} : { maxWidth: options.maxWidth }),
|
|
85
|
+
...(options.quality === undefined ? {} : { quality: options.quality }),
|
|
86
|
+
};
|
|
87
|
+
async function* frames() {
|
|
88
|
+
let after = 0;
|
|
89
|
+
for (;;) {
|
|
90
|
+
if (ended)
|
|
91
|
+
return;
|
|
92
|
+
const asked = await client.call("screen.watch", { after, ...params }, { signal: stop.signal });
|
|
93
|
+
if (ended)
|
|
94
|
+
return;
|
|
95
|
+
if (!asked.ok)
|
|
96
|
+
throw new ScreenWatchError(asked.error.code, redactWords(asked.error.message));
|
|
97
|
+
const answer = asked.value;
|
|
98
|
+
if (!answer.changed) {
|
|
99
|
+
// The computer waited its waitMs; one that answers at once must not spin: timers and the signal run first.
|
|
100
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
// The frame is read by the size it says: never past it, never over the largest frame, and whole at the end. A
|
|
104
|
+
// missing eof is never taken as the end: the size says whether there is more.
|
|
105
|
+
const said = answer.size;
|
|
106
|
+
const sized = typeof said === "number" && Number.isSafeInteger(said) && said >= 0;
|
|
107
|
+
if (sized && said > MAX_FRAME_BYTES)
|
|
108
|
+
throw new ScreenWatchError("invalid", `frame ${answer.seq} says ${said} bytes, more than the largest frame, ${MAX_FRAME_BYTES}`);
|
|
109
|
+
const parts = [Buffer.from(answer.data ?? "", "base64")];
|
|
110
|
+
let size = parts[0].length;
|
|
111
|
+
const past = () => new ScreenWatchError("invalid", `frame ${answer.seq} gave ${size} bytes, past the ${said} bytes it said`);
|
|
112
|
+
if (sized && size > said)
|
|
113
|
+
throw past();
|
|
114
|
+
for (let eof = answer.eof ?? (!sized || size >= said); !eof;) {
|
|
115
|
+
const read = await client.call("screen.read", { id: answer.id ?? "", offset: size }, { signal: stop.signal });
|
|
116
|
+
if (ended)
|
|
117
|
+
return;
|
|
118
|
+
if (!read.ok)
|
|
119
|
+
throw new ScreenWatchError(read.error.code, redactWords(read.error.message));
|
|
120
|
+
const part = Buffer.from(read.value.data, "base64");
|
|
121
|
+
if (part.length === 0 && !read.value.eof)
|
|
122
|
+
throw new ScreenWatchError("invalid", `screen.read of frame ${answer.seq} at ${size} gave no bytes and no eof`);
|
|
123
|
+
parts.push(part);
|
|
124
|
+
size += part.length;
|
|
125
|
+
if (sized ? size > said : size > MAX_FRAME_BYTES)
|
|
126
|
+
throw past();
|
|
127
|
+
eof = read.value.eof;
|
|
128
|
+
}
|
|
129
|
+
if (sized && size !== said)
|
|
130
|
+
throw new ScreenWatchError("invalid", `frame ${answer.seq} ended at ${size} bytes of the ${said} it said`);
|
|
131
|
+
after = answer.seq;
|
|
132
|
+
yield {
|
|
133
|
+
seq: answer.seq,
|
|
134
|
+
format: answer.format ?? "jpeg",
|
|
135
|
+
bytes: Buffer.concat(parts),
|
|
136
|
+
frameWidth: answer.frameWidth ?? answer.width,
|
|
137
|
+
frameHeight: answer.frameHeight ?? answer.height,
|
|
138
|
+
width: answer.width,
|
|
139
|
+
height: answer.height,
|
|
140
|
+
...(answer.cursor ? { cursor: [answer.cursor[0], answer.cursor[1]] } : {}),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const generator = frames();
|
|
145
|
+
let inFlight = 0;
|
|
146
|
+
return {
|
|
147
|
+
frames: { [Symbol.asyncIterator]: () => generator },
|
|
148
|
+
async act(actions, { signal } = {}) {
|
|
149
|
+
if (ended === "closed")
|
|
150
|
+
return fail("gone", "the screen watch is closed: nothing more is sent to the computer");
|
|
151
|
+
if (ended === "aborted")
|
|
152
|
+
return fail("aborted", "the screen watch was aborted: nothing more is sent to the computer");
|
|
153
|
+
if (inFlight >= MAX_INPUTS_IN_FLIGHT)
|
|
154
|
+
return fail("limit", `${MAX_INPUTS_IN_FLIGHT} inputs are in flight on this watch: send the next once one has answered`);
|
|
155
|
+
inFlight += 1;
|
|
156
|
+
try {
|
|
157
|
+
const either = signal ? AbortSignal.any([signal, stop.signal]) : stop.signal;
|
|
158
|
+
const done = await client.call("input.actions", { actions }, { signal: either });
|
|
159
|
+
if (!done.ok)
|
|
160
|
+
return fail(done.error.code, redactWords(done.error.message));
|
|
161
|
+
// A failed action's words may carry a device's stderr: redacted as an error's are.
|
|
162
|
+
const failed = done.value.failed;
|
|
163
|
+
return failed ? ok({ ...done.value, failed: { ...failed, message: redactWords(failed.message) } }) : done;
|
|
164
|
+
}
|
|
165
|
+
finally {
|
|
166
|
+
inFlight -= 1;
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
async close() {
|
|
170
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
171
|
+
end("closed");
|
|
172
|
+
await generator.return(undefined).catch(() => undefined);
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* The live view of the computer at ref, through provider's connect(): the one implementation behind every provider's
|
|
178
|
+
* watch(), an adapter over watchScreen on a connection of the watch's own, which it lets go however the watch ends. A
|
|
179
|
+
* computer whose hello declares no screen is refused as unsupported; a connect that fails keeps its code, in words with
|
|
180
|
+
* no URL.
|
|
181
|
+
*/
|
|
182
|
+
export async function daemonWatch(provider, ref, handlers, options = {}) {
|
|
183
|
+
if (options.signal?.aborted)
|
|
184
|
+
return fail("aborted", "the screen watch was aborted before it started");
|
|
185
|
+
const connected = await provider.connect(ref);
|
|
186
|
+
if (!connected.ok)
|
|
187
|
+
return fail(connected.error.code, redactWords(connected.error.message));
|
|
188
|
+
const connection = connected.value;
|
|
189
|
+
const groups = Array.isArray(connection.hello?.groups) ? connection.hello.groups : [];
|
|
190
|
+
if (!groups.includes("screen")) {
|
|
191
|
+
await connection.close().catch(() => undefined);
|
|
192
|
+
return fail("unsupported", `${ref.name} has no screen to watch: its computer declares ${groups.join(", ") || "no group"}`);
|
|
193
|
+
}
|
|
194
|
+
if (options.signal?.aborted) {
|
|
195
|
+
await connection.close().catch(() => undefined);
|
|
196
|
+
return fail("aborted", "the screen watch was aborted before it started");
|
|
197
|
+
}
|
|
198
|
+
const screen = watchScreen(connection, options);
|
|
199
|
+
const stop = new AbortController();
|
|
200
|
+
const stopped = new Promise((resolve) => stop.signal.addEventListener("abort", () => resolve(), { once: true }));
|
|
201
|
+
let lost;
|
|
202
|
+
let released;
|
|
203
|
+
const release = () => {
|
|
204
|
+
released ??= screen
|
|
205
|
+
.close()
|
|
206
|
+
.then(() => connection.close())
|
|
207
|
+
.catch(() => undefined);
|
|
208
|
+
return released;
|
|
209
|
+
};
|
|
210
|
+
// The caller's signal ends the watch as close() does: watchScreen stops on it, and the connection is let go here.
|
|
211
|
+
const onAbort = () => {
|
|
212
|
+
stop.abort();
|
|
213
|
+
void release();
|
|
214
|
+
};
|
|
215
|
+
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
216
|
+
const run = async () => {
|
|
217
|
+
const frames = screen.frames[Symbol.asyncIterator]();
|
|
218
|
+
for (;;) {
|
|
219
|
+
let next;
|
|
220
|
+
try {
|
|
221
|
+
next = await frames.next();
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
if (stop.signal.aborted)
|
|
225
|
+
return;
|
|
226
|
+
const code = error instanceof ScreenWatchError ? error.code : "unreachable";
|
|
227
|
+
const words = redactWords(error instanceof Error ? error.message : String(error));
|
|
228
|
+
lost = { code: watchEndCode(code), message: words };
|
|
229
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
230
|
+
stop.abort();
|
|
231
|
+
void release();
|
|
232
|
+
try {
|
|
233
|
+
handlers.ended(lost);
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
// The handler's own failure is the viewer's; the watch is over either way.
|
|
237
|
+
}
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (next.done || stop.signal.aborted)
|
|
241
|
+
return;
|
|
242
|
+
let acked;
|
|
243
|
+
const ack = new Promise((resolve) => (acked = resolve));
|
|
244
|
+
try {
|
|
245
|
+
handlers.frame(next.value, () => acked());
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
// A handler that throws drops the frame: the next is asked for at once.
|
|
249
|
+
acked();
|
|
250
|
+
}
|
|
251
|
+
await Promise.race([ack, stopped]);
|
|
252
|
+
if (stop.signal.aborted)
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
void run();
|
|
257
|
+
return ok({
|
|
258
|
+
async input(actions, { signal } = {}) {
|
|
259
|
+
if (lost)
|
|
260
|
+
return fail(lost.code, `the screen watch ended: ${lost.message}`);
|
|
261
|
+
return screen.act(actions, signal ? { signal } : {});
|
|
262
|
+
},
|
|
263
|
+
async close() {
|
|
264
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
265
|
+
stop.abort();
|
|
266
|
+
await release();
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
/** The viewer's mouse buttons (MouseEvent.button) as the protocol names them. */
|
|
271
|
+
const BUTTONS = { 0: "left", 1: "middle", 2: "right" };
|
|
272
|
+
/** The viewer's key names (KeyboardEvent.key) that are not one printable character, as the protocol's KEYS name them. */
|
|
273
|
+
const KEY_NAMES = {
|
|
274
|
+
Enter: "enter",
|
|
275
|
+
Tab: "tab",
|
|
276
|
+
" ": "space",
|
|
277
|
+
Backspace: "backspace",
|
|
278
|
+
Delete: "delete",
|
|
279
|
+
Escape: "escape",
|
|
280
|
+
Insert: "insert",
|
|
281
|
+
ArrowUp: "up",
|
|
282
|
+
ArrowDown: "down",
|
|
283
|
+
ArrowLeft: "left",
|
|
284
|
+
ArrowRight: "right",
|
|
285
|
+
Home: "home",
|
|
286
|
+
End: "end",
|
|
287
|
+
PageUp: "pageup",
|
|
288
|
+
PageDown: "pagedown",
|
|
289
|
+
...Object.fromEntries(Array.from({ length: 12 }, (_, i) => [`F${i + 1}`, `f${i + 1}`])),
|
|
290
|
+
};
|
|
291
|
+
/** Keys pressed alone do nothing a viewer means: they ride a combo as modifiers. */
|
|
292
|
+
const MODIFIER_KEYS = new Set(["Shift", "Control", "Alt", "Meta", "CapsLock", "AltGraph", "OS"]);
|
|
293
|
+
/** The modifiers of a key event (CDP's mask: Alt 1, Control 2, Meta 4, Shift 8) as combo keys, in order. */
|
|
294
|
+
const comboOf = (modifiers) => [...(modifiers & 2 ? ["ctrl"] : []), ...(modifiers & 1 ? ["alt"] : []), ...(modifiers & 8 ? ["shift"] : []), ...(modifiers & 4 ? ["super"] : [])];
|
|
295
|
+
/** The longest swipe a viewer asks for, in ms. */
|
|
296
|
+
const MAX_SWIPE_MS = 1_000;
|
|
297
|
+
/** The most wheel clicks one event moves, each way. */
|
|
298
|
+
const MAX_WHEEL_CLICKS = 20;
|
|
299
|
+
/** The longest key name a viewer sends (KeyboardEvent.key). */
|
|
300
|
+
const MAX_KEY_LENGTH = 64;
|
|
301
|
+
const show = (value) => (typeof value === "string" ? JSON.stringify(value.slice(0, 40)) : String(value));
|
|
302
|
+
const isNumber = (value) => typeof value === "number" && Number.isFinite(value);
|
|
303
|
+
/** A field's number, or a Refusal naming it. */
|
|
304
|
+
const numberOf = (event, field) => isNumber(event[field]) ? event[field] : { field, message: `${field} ${show(event[field])} is not a number` };
|
|
305
|
+
const isRefusal = (value) => typeof value === "object" && value !== null && "field" in value;
|
|
306
|
+
/** The event's point at fields x and y (or x1/y1, x2/y2), rounded to whole pixels, or a Refusal naming the field. */
|
|
307
|
+
function pointAt(event, x, y) {
|
|
308
|
+
const px = numberOf(event, x);
|
|
309
|
+
if (isRefusal(px))
|
|
310
|
+
return { field: x, message: `${show(event[x])},${show(event[y])} is not a point on the screen` };
|
|
311
|
+
const py = numberOf(event, y);
|
|
312
|
+
if (isRefusal(py))
|
|
313
|
+
return { field: y, message: `${show(event[x])},${show(event[y])} is not a point on the screen` };
|
|
314
|
+
return [Math.round(px), Math.round(py)];
|
|
315
|
+
}
|
|
316
|
+
/** The event's modifiers (CDP's mask), or a Refusal. */
|
|
317
|
+
const modifiersOf = (event) => {
|
|
318
|
+
const m = event.modifiers ?? 0;
|
|
319
|
+
return Number.isInteger(m) && m >= 0 && m <= 15 ? m : { field: "modifiers", message: `modifiers ${show(m)} is not CDP's mask, a whole number 0 to 15` };
|
|
320
|
+
};
|
|
321
|
+
/** The key event's input actions: a printable key's down types nothing (its char does); see toAction. */
|
|
322
|
+
function keyActions(event, kind) {
|
|
323
|
+
if (!["down", "up", "char"].includes(event.action))
|
|
324
|
+
return { field: "action", message: `a key's action ${show(event.action)} is not down, up or char` };
|
|
325
|
+
if (typeof event.key !== "string" || event.key.length > MAX_KEY_LENGTH)
|
|
326
|
+
return { field: "key", message: `key ${show(event.key)} is not a key name of ${MAX_KEY_LENGTH} characters at most` };
|
|
327
|
+
if (event.text !== undefined && typeof event.text !== "string")
|
|
328
|
+
return { field: "text", message: `text ${show(event.text)} is not a string` };
|
|
329
|
+
const modifiers = modifiersOf(event);
|
|
330
|
+
if (isRefusal(modifiers))
|
|
331
|
+
return modifiers;
|
|
332
|
+
const key = event.key;
|
|
333
|
+
if (event.action === "up")
|
|
334
|
+
return [];
|
|
335
|
+
if (event.action === "char") {
|
|
336
|
+
const text = event.text ?? key;
|
|
337
|
+
const characters = [...text].length;
|
|
338
|
+
if (characters > MAX_TYPE_CHARACTERS)
|
|
339
|
+
return { field: "text", message: `text of ${characters} characters: one typed text holds at most ${MAX_TYPE_CHARACTERS} characters` };
|
|
340
|
+
return text === "" ? [] : [{ type: "type", text }];
|
|
341
|
+
}
|
|
342
|
+
if (MODIFIER_KEYS.has(key))
|
|
343
|
+
return [];
|
|
344
|
+
const named = KEY_NAMES[key];
|
|
345
|
+
const printable = /^[\x21-\x7e]$/.test(key);
|
|
346
|
+
const combo = comboOf(modifiers);
|
|
347
|
+
if (named === undefined && !printable)
|
|
348
|
+
return { field: "key", message: `${key} is not a key this computer takes` };
|
|
349
|
+
// A viewer never ends or switches the display (the X server's own key combos), whatever the desktop.
|
|
350
|
+
if (kind === "desktop" && modifiers & 2 && modifiers & 1) {
|
|
351
|
+
if (key === "Backspace")
|
|
352
|
+
return { field: "key", message: "ctrl+alt+backspace would end the display: the live view does not send it" };
|
|
353
|
+
if (/^F([1-9]|1[0-2])$/.test(key))
|
|
354
|
+
return { field: "key", message: `ctrl+alt+${key} would switch the display: the live view does not send it` };
|
|
355
|
+
}
|
|
356
|
+
// A printable character with no ctrl, alt or super held is typed by its char event, which carries the layout's text;
|
|
357
|
+
// with one held it is a shortcut, Shift kept: ctrl+shift+s is not ctrl+s.
|
|
358
|
+
if (named === undefined && !(modifiers & (1 | 2 | 4)))
|
|
359
|
+
return [];
|
|
360
|
+
return [{ type: "key", keys: [...combo, named ?? key.toLowerCase()] }];
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* The viewer's event as input actions for a computer of this kind, in the screen's upright pixels: the space the
|
|
364
|
+
* frames' width and height give. Points are rounded to whole pixels. What a kind cannot take is a Refusal in words,
|
|
365
|
+
* naming the event's field, and so is an event whose fields are not what its type says: it never throws.
|
|
366
|
+
*
|
|
367
|
+
* Bounds: a typed text holds 65,536 characters at most (refused past it); a swipe takes 0 to 1,000 ms (clamped); one
|
|
368
|
+
* wheel event moves 20 clicks at most each way (clamped). A shortcut keeps Shift (ctrl+shift+s). A click with a
|
|
369
|
+
* modifier held is one click with its keys, on the up; its down only moves. A desktop never gets ctrl+alt+backspace or
|
|
370
|
+
* ctrl+alt+F1 to F12: they end or switch the X display.
|
|
371
|
+
*
|
|
372
|
+
* toAction is stateless: each event on its own. A modifier released between a press and its release makes the press
|
|
373
|
+
* modified and the release plain, and the click is lost; a viewer maps through createActionMapper, which remembers.
|
|
374
|
+
*/
|
|
375
|
+
export function toAction(event, kind) {
|
|
376
|
+
// The event comes from a browser page: every field is checked here, whatever its type says.
|
|
377
|
+
if (typeof event !== "object" || event === null || Array.isArray(event))
|
|
378
|
+
return { field: "type", message: "an event is an object with a type" };
|
|
379
|
+
const e = event;
|
|
380
|
+
const type = e.type;
|
|
381
|
+
if (!["mouse", "wheel", "key", "tap", "swipe", "button"].includes(type))
|
|
382
|
+
return { field: "type", message: `${show(type)} is not an event the live view sends` };
|
|
383
|
+
if (type === "key")
|
|
384
|
+
return keyActions(e, kind);
|
|
385
|
+
if (kind === "desktop") {
|
|
386
|
+
switch (type) {
|
|
387
|
+
case "mouse": {
|
|
388
|
+
if (!["down", "up", "move"].includes(e.action))
|
|
389
|
+
return { field: "action", message: `a mouse action ${show(e.action)} is not down, up or move` };
|
|
390
|
+
const at = pointAt(e, "x", "y");
|
|
391
|
+
if (isRefusal(at))
|
|
392
|
+
return at;
|
|
393
|
+
const modifiers = modifiersOf(e);
|
|
394
|
+
if (isRefusal(modifiers))
|
|
395
|
+
return modifiers;
|
|
396
|
+
if (e.action === "move")
|
|
397
|
+
return [{ type: "move", at }];
|
|
398
|
+
const button = typeof e.button === "number" ? BUTTONS[e.button] : undefined;
|
|
399
|
+
if (!button)
|
|
400
|
+
return { field: "button", message: `mouse button ${show(e.button)} is not left (0), middle (1) or right (2)` };
|
|
401
|
+
if (modifiers === 0)
|
|
402
|
+
return [{ type: "move", at }, { type: e.action, button }];
|
|
403
|
+
// A modified click keeps its modifiers: the protocol holds keys through a click, not across a down and an up,
|
|
404
|
+
// so the down only moves and the up clicks with the keys held (a modified drag is not sent). Each up is one
|
|
405
|
+
// click: a double-click is its two ups, as the X server times them, never a count (which it would repeat).
|
|
406
|
+
if (e.action === "down")
|
|
407
|
+
return [{ type: "move", at }];
|
|
408
|
+
return [{ type: "move", at }, { type: "click", at, button, keys: comboOf(modifiers) }];
|
|
409
|
+
}
|
|
410
|
+
case "wheel": {
|
|
411
|
+
const at = pointAt(e, "x", "y");
|
|
412
|
+
if (isRefusal(at))
|
|
413
|
+
return at;
|
|
414
|
+
const dx = numberOf(e, "dx");
|
|
415
|
+
if (isRefusal(dx))
|
|
416
|
+
return dx;
|
|
417
|
+
const dy = numberOf(e, "dy");
|
|
418
|
+
if (isRefusal(dy))
|
|
419
|
+
return dy;
|
|
420
|
+
// A wheel notch is about 120 pixels of delta; X scrolls by clicks, so any movement is one click at least, and
|
|
421
|
+
// one event moves MAX_WHEEL_CLICKS at most.
|
|
422
|
+
const clicks = (d) => (d === 0 ? 0 : Math.sign(d) * Math.min(MAX_WHEEL_CLICKS, Math.max(1, Math.round(Math.abs(d) / 120))));
|
|
423
|
+
return [{ type: "scroll", at, dx: clicks(dx), dy: clicks(dy), unit: "clicks" }];
|
|
424
|
+
}
|
|
425
|
+
default:
|
|
426
|
+
return { field: "type", message: `a desktop takes the mouse, the wheel and keys, not a ${type}` };
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
switch (type) {
|
|
430
|
+
case "tap": {
|
|
431
|
+
const at = pointAt(e, "x", "y");
|
|
432
|
+
return isRefusal(at) ? at : [{ type: "tap", at }];
|
|
433
|
+
}
|
|
434
|
+
case "swipe": {
|
|
435
|
+
const from = pointAt(e, "x1", "y1");
|
|
436
|
+
if (isRefusal(from))
|
|
437
|
+
return from;
|
|
438
|
+
const to = pointAt(e, "x2", "y2");
|
|
439
|
+
if (isRefusal(to))
|
|
440
|
+
return to;
|
|
441
|
+
const ms = numberOf(e, "ms");
|
|
442
|
+
if (isRefusal(ms))
|
|
443
|
+
return ms;
|
|
444
|
+
return [{ type: "swipe", path: [from, to], ms: Math.min(MAX_SWIPE_MS, Math.max(0, Math.round(ms))) }];
|
|
445
|
+
}
|
|
446
|
+
case "button": {
|
|
447
|
+
const name = e.name;
|
|
448
|
+
if (name !== "back" && name !== "home" && name !== "recents")
|
|
449
|
+
return { field: "name", message: `button ${show(name)} is not back, home or recents` };
|
|
450
|
+
return [{ type: "button", name: name === "recents" ? "app_switch" : name }];
|
|
451
|
+
}
|
|
452
|
+
default:
|
|
453
|
+
return { field: "type", message: `a phone takes taps, swipes, its buttons and keys, not the ${type}` };
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
/** A mapper of the viewer's events for a computer of this kind; see ActionMapper. */
|
|
457
|
+
export function createActionMapper(kind) {
|
|
458
|
+
/** Each mouse button pressed and not released: plain (a down was sent), or the keys a modified press holds. */
|
|
459
|
+
const pressed = new Map();
|
|
460
|
+
return {
|
|
461
|
+
map(event) {
|
|
462
|
+
const stateless = toAction(event, kind);
|
|
463
|
+
if (isRefusal(stateless) || kind !== "desktop")
|
|
464
|
+
return stateless;
|
|
465
|
+
const e = event;
|
|
466
|
+
if (e.type !== "mouse" || (e.action !== "down" && e.action !== "up"))
|
|
467
|
+
return stateless;
|
|
468
|
+
// toAction checked the event: its point, button and modifiers are good.
|
|
469
|
+
const button = BUTTONS[e.button];
|
|
470
|
+
const at = [Math.round(e.x), Math.round(e.y)];
|
|
471
|
+
const modifiers = e.modifiers ?? 0;
|
|
472
|
+
if (e.action === "down") {
|
|
473
|
+
pressed.set(button, modifiers === 0 ? {} : { keys: comboOf(modifiers) });
|
|
474
|
+
return stateless;
|
|
475
|
+
}
|
|
476
|
+
const press = pressed.get(button);
|
|
477
|
+
if (press === undefined)
|
|
478
|
+
return stateless;
|
|
479
|
+
pressed.delete(button);
|
|
480
|
+
if (press.keys === undefined)
|
|
481
|
+
return [{ type: "move", at }, { type: "up", button }];
|
|
482
|
+
return [{ type: "move", at }, { type: "click", at, button, keys: press.keys }];
|
|
483
|
+
},
|
|
484
|
+
release() {
|
|
485
|
+
const ups = [...pressed].filter(([, press]) => press.keys === undefined).map(([button]) => ({ type: "up", button }));
|
|
486
|
+
pressed.clear();
|
|
487
|
+
return ups;
|
|
488
|
+
},
|
|
489
|
+
};
|
|
490
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type ComputerError, type Result } from "./result.js";
|
|
2
|
+
/** An error of the read, as testConnection answers it. */
|
|
3
|
+
export declare function connectionError(error: ComputerError): ComputerError;
|
|
4
|
+
/**
|
|
5
|
+
* One listing by the provider's own label, as testConnection answers it: how many computers of this provider it found,
|
|
6
|
+
* "or more" when the vendor has another page the test did not read.
|
|
7
|
+
*/
|
|
8
|
+
export declare function countTested(vendor: string, counted: Result<{
|
|
9
|
+
count: number;
|
|
10
|
+
more?: boolean;
|
|
11
|
+
}>): Result<{
|
|
12
|
+
detail: string;
|
|
13
|
+
}>;
|
|
14
|
+
/** A list by the provider's own label, as testConnection answers it: how many computers of this provider it found. */
|
|
15
|
+
export declare function listTested(vendor: string, listed: Result<readonly unknown[]>): Result<{
|
|
16
|
+
detail: string;
|
|
17
|
+
}>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Settings' "test connection" (ComputerProvider.testConnection): what each provider's one account-level read means, in
|
|
2
|
+
// the same words. A key the vendor refused is unauthorized and a vendor not reached in time unreachable; any other
|
|
3
|
+
// error keeps its code. A message is the provider's own, which never carries a key.
|
|
4
|
+
import { fail, ok } from "./result.js";
|
|
5
|
+
/** An error of the read, as testConnection answers it. */
|
|
6
|
+
export function connectionError(error) {
|
|
7
|
+
if (error.code === "denied")
|
|
8
|
+
return { code: "unauthorized", message: error.message };
|
|
9
|
+
if (error.code === "timeout")
|
|
10
|
+
return { code: "unreachable", message: error.message };
|
|
11
|
+
return error;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* One listing by the provider's own label, as testConnection answers it: how many computers of this provider it found,
|
|
15
|
+
* "or more" when the vendor has another page the test did not read.
|
|
16
|
+
*/
|
|
17
|
+
export function countTested(vendor, counted) {
|
|
18
|
+
if (!counted.ok)
|
|
19
|
+
return fail(connectionError(counted.error).code, connectionError(counted.error).message);
|
|
20
|
+
const { count, more = false } = counted.value;
|
|
21
|
+
return ok({ detail: `${vendor} answered: ${count}${more ? " or more" : ""} ${count === 1 && !more ? "computer" : "computers"} of this provider` });
|
|
22
|
+
}
|
|
23
|
+
/** A list by the provider's own label, as testConnection answers it: how many computers of this provider it found. */
|
|
24
|
+
export function listTested(vendor, listed) {
|
|
25
|
+
return countTested(vendor, listed.ok ? ok({ count: listed.value.length }) : listed);
|
|
26
|
+
}
|
package/dist/wait.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Result } from "./result.js";
|
|
2
|
+
export interface WaitOptions<S> {
|
|
3
|
+
/** The longest the whole wait takes, reads included, in ms. */
|
|
4
|
+
boundMs: number;
|
|
5
|
+
/** Ends the wait early, as aborted. */
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
/** The first pause between two reads, in ms; each pause doubles, up to maxMs. Default 250. */
|
|
8
|
+
minMs?: number;
|
|
9
|
+
/** The longest pause between two reads, in ms. Default 5000. */
|
|
10
|
+
maxMs?: number;
|
|
11
|
+
/** What is waited for, in words, for the errors, such as "computer-0123456789ab to finish". */
|
|
12
|
+
what?: string;
|
|
13
|
+
/** A state in words, for the errors. Default its JSON. */
|
|
14
|
+
describe?: (state: S) => string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Reads until done(state) holds and returns that state: the first read at once, then after pauses that double from
|
|
18
|
+
* minMs up to maxMs, never past boundMs. Each read is given a signal that aborts when the bound passes or the caller's
|
|
19
|
+
* signal aborts; a read still pending then is not waited for. A read that throws or rejects is not done, and the wait
|
|
20
|
+
* reads again. Past the bound the result is a timeout, and on the caller's abort an abort, each naming what was waited
|
|
21
|
+
* for and the last state read (or the last read's failure). It never reports done for a state it did not read.
|
|
22
|
+
*/
|
|
23
|
+
export declare function waitUntil<S>(read: (signal: AbortSignal) => Promise<S>, done: (state: S) => boolean, options: WaitOptions<S>): Promise<Result<S>>;
|
package/dist/wait.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// A bounded wait for a state read from outside, such as a vendor reading a computer back as ended after a release.
|
|
2
|
+
import { fail, ok } from "./result.js";
|
|
3
|
+
const positive = (ms) => typeof ms === "number" && Number.isFinite(ms) && ms > 0;
|
|
4
|
+
const message = (error) => (error instanceof Error ? error.message : String(error));
|
|
5
|
+
/**
|
|
6
|
+
* Reads until done(state) holds and returns that state: the first read at once, then after pauses that double from
|
|
7
|
+
* minMs up to maxMs, never past boundMs. Each read is given a signal that aborts when the bound passes or the caller's
|
|
8
|
+
* signal aborts; a read still pending then is not waited for. A read that throws or rejects is not done, and the wait
|
|
9
|
+
* reads again. Past the bound the result is a timeout, and on the caller's abort an abort, each naming what was waited
|
|
10
|
+
* for and the last state read (or the last read's failure). It never reports done for a state it did not read.
|
|
11
|
+
*/
|
|
12
|
+
export async function waitUntil(read, done, options) {
|
|
13
|
+
const { boundMs, signal, minMs = 250, maxMs = 5_000, what = "the wait", describe = (state) => JSON.stringify(state) ?? String(state) } = options;
|
|
14
|
+
if (!positive(boundMs) || !positive(minMs) || !positive(maxMs) || maxMs < minMs) {
|
|
15
|
+
throw new RangeError(`waitUntil takes boundMs, minMs and maxMs as positive numbers of ms, minMs at most maxMs; got ${boundMs}, ${minMs} and ${maxMs}`);
|
|
16
|
+
}
|
|
17
|
+
const started = Date.now();
|
|
18
|
+
const controller = new AbortController();
|
|
19
|
+
const timer = setTimeout(() => controller.abort(), boundMs);
|
|
20
|
+
const onAbort = () => controller.abort();
|
|
21
|
+
if (signal?.aborted)
|
|
22
|
+
controller.abort();
|
|
23
|
+
else
|
|
24
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
25
|
+
const ended = new Promise((resolve) => {
|
|
26
|
+
if (controller.signal.aborted)
|
|
27
|
+
resolve({ ended: true });
|
|
28
|
+
else
|
|
29
|
+
controller.signal.addEventListener("abort", () => resolve({ ended: true }), { once: true });
|
|
30
|
+
});
|
|
31
|
+
let last = "no read had answered";
|
|
32
|
+
let pause = minMs;
|
|
33
|
+
try {
|
|
34
|
+
while (!controller.signal.aborted) {
|
|
35
|
+
const reading = Promise.resolve()
|
|
36
|
+
.then(() => read(controller.signal))
|
|
37
|
+
.then((state) => ({ state }), (error) => ({ error }));
|
|
38
|
+
const answer = await Promise.race([reading, ended]);
|
|
39
|
+
if ("ended" in answer)
|
|
40
|
+
break;
|
|
41
|
+
if ("error" in answer)
|
|
42
|
+
last = `the read failed: ${message(answer.error)}`;
|
|
43
|
+
else if (done(answer.state))
|
|
44
|
+
return ok(answer.state);
|
|
45
|
+
else
|
|
46
|
+
last = describe(answer.state);
|
|
47
|
+
const pausing = new Promise((resolve) => {
|
|
48
|
+
const wake = setTimeout(resolve, pause);
|
|
49
|
+
void ended.then(() => {
|
|
50
|
+
clearTimeout(wake);
|
|
51
|
+
resolve();
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
await pausing;
|
|
55
|
+
pause = Math.min(pause * 2, maxMs);
|
|
56
|
+
}
|
|
57
|
+
if (signal?.aborted)
|
|
58
|
+
return fail("aborted", `the wait for ${what} was aborted after ${(Date.now() - started) / 1000} s; the last read: ${last}`);
|
|
59
|
+
return fail("timeout", `${what} did not happen within ${boundMs / 1000} s; the last read: ${last}`);
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
clearTimeout(timer);
|
|
63
|
+
signal?.removeEventListener("abort", onAbort);
|
|
64
|
+
}
|
|
65
|
+
}
|