openmsg 0.1.0 → 0.2.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/src/relay.mjs ADDED
@@ -0,0 +1,446 @@
1
+ // The relay: one server for a team. Every gateway keeps a connection to it,
2
+ // and no laptop needs an open port.
3
+ //
4
+ // The relay carries sealed bytes. It cannot read a message, and this file
5
+ // holds no key that opens one. It learns who writes to whom, when, and in
6
+ // which project, because it needs that to route. The product must say so.
7
+ //
8
+ // It stores a message for a receiver that is offline, with a bound on the
9
+ // size, on the number, and on the age. The sender keeps its own copy until a
10
+ // receipt arrives, so one loss on one side does not lose the message.
11
+ import http from "node:http";
12
+ import https from "node:https";
13
+ import fs from "node:fs";
14
+ import path from "node:path";
15
+ import os from "node:os";
16
+ import { canonicalBytes } from "./canonical.mjs";
17
+ import { ownerIdOf } from "./identity.mjs";
18
+ import { verifySignedObject } from "./device.mjs";
19
+ import { verifyRecord, MAX_RECORD_BYTES } from "./dirsync.mjs";
20
+ import { accepts, handshake } from "./wsframe.mjs";
21
+
22
+ export const LIMITS = {
23
+ messageBytes: 256 * 1024,
24
+ perOwner: 200,
25
+ ageMs: 7 * 24 * 3600 * 1000,
26
+ helloWindowMs: 120_000,
27
+ // The directory of one project: how many owners, and how big one record.
28
+ ownersPerProject: 200,
29
+ recordBytes: MAX_RECORD_BYTES,
30
+ // The machines of one owner that hold a connection at the same time.
31
+ machinesPerOwner: 8,
32
+ };
33
+
34
+ export function home() {
35
+ return process.env.OPENMSG_RELAY_HOME ?? path.join(os.homedir(), ".openmsg-relay");
36
+ }
37
+
38
+ function dir(...parts) {
39
+ return path.join(home(), ...parts);
40
+ }
41
+
42
+ // --- the durable store -----------------------------------------------------
43
+
44
+ function safeName(id) {
45
+ // An owner id and a message id both come from this program, and a broken
46
+ // client can still send another text. Only these characters reach the disk.
47
+ return String(id).replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 80);
48
+ }
49
+
50
+ export function store(owner, messageId, record) {
51
+ const folder = dir("queue", safeName(owner));
52
+ fs.mkdirSync(folder, { recursive: true });
53
+ const file = path.join(folder, `${safeName(messageId)}.json`);
54
+ // One write, or none. A relay that stops in the middle keeps no half file.
55
+ const tmp = `${file}.tmp`;
56
+ fs.writeFileSync(tmp, JSON.stringify(record));
57
+ fs.renameSync(tmp, file);
58
+ return file;
59
+ }
60
+
61
+ export function queued(owner) {
62
+ const folder = dir("queue", safeName(owner));
63
+ let files = [];
64
+ try {
65
+ files = fs.readdirSync(folder).filter((f) => f.endsWith(".json"));
66
+ } catch {
67
+ return [];
68
+ }
69
+ const out = [];
70
+ for (const f of files) {
71
+ try {
72
+ out.push({ file: path.join(folder, f), ...JSON.parse(fs.readFileSync(path.join(folder, f), "utf8")) });
73
+ } catch {
74
+ // A file that no reader understands is of no use to anybody.
75
+ fs.rmSync(path.join(folder, f), { force: true });
76
+ }
77
+ }
78
+ return out.sort((a, b) => (a.at < b.at ? -1 : 1));
79
+ }
80
+
81
+ export function drop(owner, messageId) {
82
+ fs.rmSync(path.join(dir("queue", safeName(owner)), `${safeName(messageId)}.json`), { force: true });
83
+ }
84
+
85
+ // A message that waited too long, and a queue that grew too long, both stop
86
+ // here. The oldest message goes first.
87
+ export function sweep(owner, { now = Date.now() } = {}) {
88
+ const rows = queued(owner);
89
+ let removed = 0;
90
+ for (const row of rows) {
91
+ const old = now - Date.parse(row.at) > LIMITS.ageMs;
92
+ const dead = row.expiresAt && Date.parse(row.expiresAt) <= now;
93
+ if (old || dead) {
94
+ fs.rmSync(row.file, { force: true });
95
+ removed += 1;
96
+ }
97
+ }
98
+ const left = queued(owner);
99
+ for (const row of left.slice(0, Math.max(0, left.length - LIMITS.perOwner))) {
100
+ fs.rmSync(row.file, { force: true });
101
+ removed += 1;
102
+ }
103
+ return removed;
104
+ }
105
+
106
+ // A receipt for a sender that is offline waits here.
107
+ export function keepReceipt(owner, receipt) {
108
+ const folder = dir("receipts", safeName(owner));
109
+ fs.mkdirSync(folder, { recursive: true });
110
+ const row = { ...receipt, at: receipt.at ?? new Date().toISOString() };
111
+ fs.writeFileSync(path.join(folder, `${safeName(receipt.messageId)}.json`), JSON.stringify(row));
112
+ }
113
+
114
+ export function receipts(owner, { now = Date.now() } = {}) {
115
+ const folder = dir("receipts", safeName(owner));
116
+ let files = [];
117
+ try {
118
+ files = fs.readdirSync(folder);
119
+ } catch {
120
+ return [];
121
+ }
122
+ const out = [];
123
+ for (const f of files) {
124
+ const file = path.join(folder, f);
125
+ try {
126
+ const row = { file, ...JSON.parse(fs.readFileSync(file, "utf8")) };
127
+ // Every machine of an owner reads a receipt, so the relay keeps it
128
+ // until it is old. A machine that reads one twice writes the same
129
+ // state twice, and that changes nothing.
130
+ if (now - Date.parse(row.at ?? 0) > LIMITS.ageMs) {
131
+ fs.rmSync(file, { force: true });
132
+ continue;
133
+ }
134
+ out.push(row);
135
+ } catch {
136
+ fs.rmSync(file, { force: true });
137
+ }
138
+ }
139
+ return out;
140
+ }
141
+
142
+ // --- the directory of a project ---------------------------------------------
143
+ //
144
+ // The relay holds the records, and it reads no message. A record is signed by
145
+ // its owner, and the owner id comes from the keys inside it, so the relay
146
+ // verifies a record without a directory of its own.
147
+
148
+ // One record for one owner, one kind, and one thing. Two machines of one
149
+ // owner each keep their own roster, and two revoked machines each keep their
150
+ // own record. Without this, one write covers another.
151
+ export function recordKey(record) {
152
+ const parts = [record.owner, record.kind];
153
+ if (record.kind === "roster") parts.push(record.signature?.device ?? "owner");
154
+ if (record.kind === "revoke-device") parts.push(record.device);
155
+ return parts.map(safeName).join(".");
156
+ }
157
+
158
+ export function putRecord(project, record) {
159
+ const folder = dir("dir", safeName(project));
160
+ fs.mkdirSync(folder, { recursive: true });
161
+ const file = path.join(folder, `${recordKey(record)}.json`);
162
+ const tmp = `${file}.tmp`;
163
+ fs.writeFileSync(tmp, JSON.stringify(record));
164
+ fs.renameSync(tmp, file);
165
+ return file;
166
+ }
167
+
168
+ export function records(project) {
169
+ const folder = dir("dir", safeName(project));
170
+ let files = [];
171
+ try {
172
+ files = fs.readdirSync(folder).filter((f) => f.endsWith(".json"));
173
+ } catch {
174
+ return [];
175
+ }
176
+ const out = [];
177
+ for (const f of files) {
178
+ try {
179
+ out.push(JSON.parse(fs.readFileSync(path.join(folder, f), "utf8")));
180
+ } catch {
181
+ fs.rmSync(path.join(folder, f), { force: true });
182
+ }
183
+ }
184
+ return out;
185
+ }
186
+
187
+ // A reader of a project is an owner that already has a record there. A
188
+ // project with no record at all takes its first owner, because the project id
189
+ // is opaque and the owner that made it is the owner that publishes first.
190
+ function mayRead(project, owner) {
191
+ const rows = records(project);
192
+ return rows.length === 0 || rows.some((r) => r.owner === owner);
193
+ }
194
+
195
+ function ownersIn(project) {
196
+ return new Set(records(project).map((r) => r.owner)).size;
197
+ }
198
+
199
+ // --- the server ------------------------------------------------------------
200
+
201
+ export const LOOPBACK = ["127.0.0.1", "localhost", "::1", "[::1]"];
202
+
203
+ export function isLoopback(host) {
204
+ return LOOPBACK.includes(String(host));
205
+ }
206
+
207
+ // A relay that other machines reach speaks TLS. Section 4.2 of the spec asks
208
+ // for it, and a gateway sends its hello and the addresses of its messages
209
+ // over that connection. The body of a message is sealed, and the addresses
210
+ // are not.
211
+ //
212
+ // `cert` and `key` give TLS here. A relay behind a proxy that ends TLS takes
213
+ // `insecure`, and the operator types that word.
214
+ export async function serve({ port = 0, host = "127.0.0.1", cert = null, key = null, insecure = false, log = () => {} } = {}) {
215
+ const answer = (req, res) => {
216
+ res.writeHead(426, { "content-type": "text/plain" });
217
+ res.end("openmsg relay: this address takes a WebSocket connection\n");
218
+ };
219
+ const secure = Boolean(cert && key);
220
+ if (!secure && !isLoopback(host) && !insecure) {
221
+ throw new Error(
222
+ `a relay on ${host} carries the messages of other people, and this one has no TLS. ` +
223
+ "Give --cert <file> and --key <file>, or put a proxy that ends TLS in front and add --insecure.",
224
+ );
225
+ }
226
+ const server = secure
227
+ ? https.createServer({ cert: fs.readFileSync(cert), key: fs.readFileSync(key) }, answer)
228
+ : http.createServer(answer);
229
+ if (!secure && !isLoopback(host)) {
230
+ log("WARNING: this relay speaks no TLS. A proxy in front must end TLS, or the addresses travel in the open.");
231
+ }
232
+ // One owner, many machines. Each machine of an owner holds its own
233
+ // connection, and the relay does not know which machine holds which
234
+ // session, because it cannot read a message. It therefore gives a message
235
+ // to every machine of that owner. The machine that holds the session takes
236
+ // it and answers, and the others stay quiet.
237
+ const owners = new Map();
238
+
239
+ server.on("upgrade", (request, socket) => {
240
+ if (!accepts(request)) {
241
+ socket.destroy();
242
+ return;
243
+ }
244
+ const connection = handshake(socket, request);
245
+ let owner = null;
246
+ let machine = null;
247
+
248
+ const answer = (object) => connection.send(JSON.stringify(object));
249
+ const stop = (text) => {
250
+ answer({ type: "error", error: text });
251
+ connection.close();
252
+ };
253
+
254
+ connection.on("message", (text) => {
255
+ let frame;
256
+ try {
257
+ frame = JSON.parse(text);
258
+ } catch {
259
+ stop("the frame is not JSON");
260
+ return;
261
+ }
262
+ if (!owner) {
263
+ if (frame.type !== "hello") {
264
+ stop("the first frame must be a hello");
265
+ return;
266
+ }
267
+ const named = helloOwner(frame);
268
+ if (!named) {
269
+ stop("the hello does not prove that key");
270
+ return;
271
+ }
272
+ owner = named.owner;
273
+ machine = named.device ?? "owner";
274
+ const machines = owners.get(owner) ?? new Map();
275
+ machines.set(connection, machine);
276
+ // A bound on the machines of one owner. The oldest connection goes
277
+ // when a new one passes the bound.
278
+ while (machines.size > LIMITS.machinesPerOwner) {
279
+ const oldest = machines.keys().next().value;
280
+ oldest.send(JSON.stringify({ type: "error", error: "too many machines of this owner" }));
281
+ oldest.close();
282
+ machines.delete(oldest);
283
+ }
284
+ owners.set(owner, machines);
285
+ sweep(owner);
286
+ const waiting = queued(owner);
287
+ answer({ type: "welcome", owner, machine, machines: machines.size, waiting: waiting.length });
288
+ log(`${owner} connected from ${machine}, ${machines.size} machines, ${waiting.length} waiting`);
289
+ // This connection is new, so it takes the waiting messages. The
290
+ // other machines of this owner already took them.
291
+ for (const row of waiting) answer({ type: "deliver", wire: row.wire, from: row.from });
292
+ // A receipt goes to every machine that connects, because the relay
293
+ // does not know which machine sent the message. A machine that takes
294
+ // one twice writes the same state twice, and that changes nothing.
295
+ for (const row of receipts(owner)) {
296
+ answer({ type: "receipt", messageId: row.messageId, status: row.status, reason: row.reason });
297
+ }
298
+ return;
299
+ }
300
+ handle(frame, owner, answer, owners, log);
301
+ });
302
+
303
+ connection.on("close", () => {
304
+ const machines = owner ? owners.get(owner) : null;
305
+ if (!machines?.has(connection)) return;
306
+ machines.delete(connection);
307
+ if (machines.size === 0) owners.delete(owner);
308
+ log(`${owner} left from ${machine}, ${machines.size} machines left`);
309
+ });
310
+ });
311
+
312
+ await new Promise((resolve) => server.listen(port, host, resolve));
313
+ const real = server.address().port;
314
+ return {
315
+ server,
316
+ port: real,
317
+ url: `${secure ? "wss" : "ws"}://${host === "0.0.0.0" || host === "::" ? "127.0.0.1" : host}:${real}`,
318
+ owners,
319
+ close: () => new Promise((done) => server.close(done)),
320
+ };
321
+ }
322
+
323
+ // The hello proves the key. The owner id comes from the two public keys, so a
324
+ // key that gives that owner id is the key of that owner. The relay needs no
325
+ // directory for this step.
326
+ function helloOwner(frame) {
327
+ const keys = frame?.keys;
328
+ if (!keys?.signing?.x || !keys?.encryption?.x) return null;
329
+ // The owner id comes from the keys, so these keys are the keys of that
330
+ // owner, and of nobody else.
331
+ if (frame.owner !== ownerIdOf(keys)) return null;
332
+ if (Math.abs(Date.now() - Date.parse(frame.at ?? 0)) > LIMITS.helloWindowMs) return null;
333
+ // A machine of that owner signs with its own key, and the delegation in
334
+ // the hello proves that the owner allows it.
335
+ const why = verifySignedObject(frame, { ownerId: frame.owner, keys });
336
+ return why === null ? { owner: frame.owner, device: frame.signature?.device ?? null } : null;
337
+ }
338
+
339
+ // Every machine of one owner takes the frame. A connection that ended stays
340
+ // in the map until the socket says so, so the relay reads the answer of
341
+ // send() and forgets a machine whose connection does not take the text.
342
+ //
343
+ // It gives the number of machines that took it.
344
+ function push(owners, owner, frame) {
345
+ const machines = owners.get(owner);
346
+ if (!machines) return 0;
347
+ const text = JSON.stringify(frame);
348
+ let sent = 0;
349
+ for (const connection of [...machines.keys()]) {
350
+ if (connection.send(text)) sent += 1;
351
+ else machines.delete(connection);
352
+ }
353
+ if (machines.size === 0) owners.delete(owner);
354
+ return sent;
355
+ }
356
+
357
+ function handle(frame, owner, answer, owners, log) {
358
+ if (frame.type === "send") {
359
+ const wire = frame.wire;
360
+ const size = Buffer.byteLength(JSON.stringify(wire ?? {}));
361
+ if (size > LIMITS.messageBytes) {
362
+ answer({ type: "refused", messageId: wire?.messageId ?? null, reason: "too-big" });
363
+ return;
364
+ }
365
+ const target = wire?.target?.owner;
366
+ if (!target || !wire?.messageId) {
367
+ answer({ type: "refused", messageId: wire?.messageId ?? null, reason: "bad-shape" });
368
+ return;
369
+ }
370
+ // The relay cannot read the message, and it can compare the two owners
371
+ // that the clear header names with the owner of this connection.
372
+ if (wire?.from?.owner !== owner) {
373
+ answer({ type: "refused", messageId: wire.messageId, reason: "sender-mismatch" });
374
+ return;
375
+ }
376
+ if (wire.expiresAt && Date.parse(wire.expiresAt) <= Date.now()) {
377
+ answer({ type: "refused", messageId: wire.messageId, reason: "expired" });
378
+ return;
379
+ }
380
+ sweep(target);
381
+ if (queued(target).length >= LIMITS.perOwner) {
382
+ answer({ type: "refused", messageId: wire.messageId, reason: "queue-full" });
383
+ return;
384
+ }
385
+ // The message is on the disk before the sender hears anything. A relay
386
+ // that stops now still holds it.
387
+ store(target, wire.messageId, { wire, from: owner, at: new Date().toISOString(), expiresAt: wire.expiresAt ?? null });
388
+ answer({ type: "stored", messageId: wire.messageId });
389
+ log(`${owner} → ${target} ${wire.messageId} (${size} bytes, sealed)`);
390
+ // The message is on the disk. A push that fails now changes nothing,
391
+ // because the receiver takes it on its next connection.
392
+ push(owners, target, { type: "deliver", wire, from: owner });
393
+ return;
394
+ }
395
+
396
+ if (frame.type === "ack") {
397
+ // The receiver read the message. The relay keeps no copy after that.
398
+ drop(owner, frame.messageId);
399
+ const receipt = { type: "receipt", messageId: frame.messageId, status: frame.status, reason: frame.reason ?? null };
400
+ // A sender that is offline gets the receipt on its next connection.
401
+ if (frame.to && push(owners, frame.to, receipt) === 0) keepReceipt(frame.to, receipt);
402
+ log(`${owner} acknowledged ${frame.messageId}: ${frame.status}`);
403
+ return;
404
+ }
405
+
406
+ // A request for routing data, and its answer, travel between two gateways.
407
+ // The relay forwards them, and it reads nothing but the two addresses.
408
+ if (frame.type === "routing-request" || frame.type === "routing-answer") {
409
+ if (push(owners, frame.to, { ...frame, from: owner, to: undefined }) === 0 && frame.type === "routing-request") {
410
+ answer({ type: "routing-answer", id: frame.id, from: frame.to, error: "that gateway is offline" });
411
+ }
412
+ return;
413
+ }
414
+
415
+ if (frame.type === "dir-publish") {
416
+ const record = frame.record;
417
+ const answer2 = (reason) => answer({ type: "dir-refused", kind: record?.kind ?? null, project: record?.project ?? null, reason });
418
+ if (Buffer.byteLength(JSON.stringify(record ?? {})) > LIMITS.recordBytes) return answer2("too-big");
419
+ // An owner publishes about itself, and about nobody else.
420
+ if (record?.owner !== owner) return answer2("sender-mismatch");
421
+ const why = verifyRecord(record);
422
+ if (why) return answer2(why);
423
+ if (!mayRead(record.project, owner) && ownersIn(record.project) >= LIMITS.ownersPerProject) {
424
+ return answer2("project-full");
425
+ }
426
+ putRecord(record.project, record);
427
+ answer({ type: "dir-stored", kind: record.kind, project: record.project });
428
+ log(`${owner} published a ${record.kind} record for ${record.project}`);
429
+ return;
430
+ }
431
+
432
+ if (frame.type === "dir-fetch") {
433
+ if (!mayRead(frame.project, owner)) {
434
+ answer({ type: "dir-records", id: frame.id, project: frame.project, records: [], error: "you have no record in that project" });
435
+ return;
436
+ }
437
+ answer({ type: "dir-records", id: frame.id, project: frame.project, records: records(frame.project) });
438
+ return;
439
+ }
440
+
441
+ if (frame.type === "ping") {
442
+ answer({ type: "pong" });
443
+ return;
444
+ }
445
+ answer({ type: "error", error: `unknown frame "${frame.type}"` });
446
+ }
@@ -0,0 +1,248 @@
1
+ // The connection of one gateway to the relay of its team.
2
+ //
3
+ // The gateway keeps this connection open, and the relay pushes a message into
4
+ // it. The command line opens the same connection for a moment when it sends.
5
+ import { randomUUID } from "node:crypto";
6
+ import { canonicalBytes } from "./canonical.mjs";
7
+ import * as identity from "./identity.mjs";
8
+ import * as device from "./device.mjs";
9
+ import { connect } from "./wsframe.mjs";
10
+ import { settings } from "./settings.mjs";
11
+ import { isLoopback } from "./relay.mjs";
12
+ import fs from "node:fs";
13
+
14
+ export const HELLO_TIMEOUT_MS = 10_000;
15
+ export const ANSWER_TIMEOUT_MS = 20_000;
16
+
17
+ // The hello proves the key of this owner. The relay needs no directory,
18
+ // because the owner id comes from the two public keys.
19
+ function hello() {
20
+ const signer = device.signer();
21
+ const me = signer.owner;
22
+ // `keys` are the public keys of the owner. The relay reads the owner id
23
+ // from them, and it verifies a delegation against them. It needs no
24
+ // directory, and it learns nothing that is not public.
25
+ const body = {
26
+ type: "hello",
27
+ version: 2,
28
+ owner: me.ownerId,
29
+ keys: me.keys,
30
+ at: new Date().toISOString(),
31
+ nonce: randomUUID(),
32
+ ...(signer.delegation ? { delegation: signer.delegation } : {}),
33
+ };
34
+ return {
35
+ ...body,
36
+ signature: {
37
+ alg: "Ed25519",
38
+ by: me.ownerId,
39
+ device: signer.device,
40
+ value: signer.sign(canonicalBytes(body)).toString("base64url"),
41
+ },
42
+ };
43
+ }
44
+
45
+ // A relay on another machine gets a connection with TLS. Without it, the
46
+ // hello of this owner and the addresses of every message travel in the open,
47
+ // and rule 4.2 of the spec asks for TLS.
48
+ function tlsFor(url) {
49
+ const address = new URL(url);
50
+ if (address.protocol === "wss:") {
51
+ const ca = settings().relayCa;
52
+ return ca ? { ca: fs.readFileSync(ca) } : {};
53
+ }
54
+ if (isLoopback(address.hostname) || settings().relayInsecure) return {};
55
+ throw new Error(
56
+ `${url} has no TLS, and it is not on this machine. Use a wss:// address, ` +
57
+ "or say that you accept it: openmsg relay use <url> --insecure",
58
+ );
59
+ }
60
+
61
+ export async function open(url, handlers = {}) {
62
+ const connection = await connect(url, { tls: tlsFor(url) });
63
+ // key -> { resolve, keep }. A question with one answer removes its waiter
64
+ // when the answer arrives. A question that every machine of an owner
65
+ // answers keeps its waiter until the window closes.
66
+ const waiting = new Map();
67
+ const fire = (key, frame) => {
68
+ const held = waiting.get(key);
69
+ if (!held) return false;
70
+ held.resolve(frame);
71
+ if (!held.keep) waiting.delete(key);
72
+ return true;
73
+ };
74
+ const link = {
75
+ url,
76
+ connection,
77
+ owner: identity.load().ownerId,
78
+ closed: false,
79
+ send,
80
+ routing,
81
+ ack,
82
+ publishRecord,
83
+ fetchDirectory,
84
+ close: () => {
85
+ link.closed = true;
86
+ connection.close();
87
+ },
88
+ };
89
+
90
+ connection.on("message", (text) => {
91
+ let frame;
92
+ try {
93
+ frame = JSON.parse(text);
94
+ } catch {
95
+ return;
96
+ }
97
+ if (frame.type === "dir-stored" || frame.type === "dir-refused") {
98
+ if (fire(`dir:${frame.project}:${frame.kind}`, frame)) return;
99
+ }
100
+ if (frame.type === "dir-records" && fire(`fetch:${frame.id}`, frame)) return;
101
+ if ((frame.type === "stored" || frame.type === "refused") && fire(`send:${frame.messageId}`, frame)) return;
102
+ if (frame.type === "routing-answer" && fire(`routing:${frame.id}`, frame)) return;
103
+ // The link goes to the handler. A frame can arrive before open() gives
104
+ // the link back to its caller, and a handler that waits for that variable
105
+ // sends nothing.
106
+ if (frame.type === "deliver") handlers.onDeliver?.(frame, link);
107
+ else if (frame.type === "receipt") handlers.onReceipt?.(frame, link);
108
+ else if (frame.type === "routing-request") handlers.onRoutingRequest?.(frame, link);
109
+ else if (frame.type === "welcome") handlers.onWelcome?.(frame, link);
110
+ else if (frame.type === "error") handlers.onError?.(frame, link);
111
+ });
112
+ connection.on("close", () => {
113
+ link.closed = true;
114
+ for (const held of waiting.values()) held.resolve({ type: "refused", reason: "the relay closed the connection" });
115
+ waiting.clear();
116
+ handlers.onClose?.();
117
+ });
118
+
119
+ const welcome = new Promise((resolve, reject) => {
120
+ const timer = setTimeout(() => reject(new Error(`the relay at ${url} sent no welcome`)), HELLO_TIMEOUT_MS);
121
+ const first = (text) => {
122
+ let frame;
123
+ try {
124
+ frame = JSON.parse(text);
125
+ } catch {
126
+ return;
127
+ }
128
+ if (frame.type !== "welcome" && frame.type !== "error") return;
129
+ clearTimeout(timer);
130
+ connection.off("message", first);
131
+ if (frame.type === "error") reject(new Error(`the relay refused the hello: ${frame.error}`));
132
+ else resolve(frame);
133
+ };
134
+ connection.on("message", first);
135
+ });
136
+ connection.send(JSON.stringify(hello()));
137
+ link.welcome = await welcome;
138
+ return link;
139
+
140
+ function expect(key, timeoutMs) {
141
+ return new Promise((resolve) => {
142
+ const timer = setTimeout(() => {
143
+ waiting.delete(key);
144
+ resolve({ type: "refused", reason: "the relay did not answer" });
145
+ }, timeoutMs);
146
+ waiting.set(key, {
147
+ keep: false,
148
+ resolve: (frame) => {
149
+ clearTimeout(timer);
150
+ resolve(frame);
151
+ },
152
+ });
153
+ });
154
+ }
155
+
156
+ // Every machine of one owner answers a question about routing. The window
157
+ // starts with the first answer, and the others have that long to arrive.
158
+ function expectMany(key, { timeoutMs, graceMs = 400 }) {
159
+ return new Promise((resolve) => {
160
+ const found = [];
161
+ let grace = null;
162
+ const done = () => {
163
+ clearTimeout(outer);
164
+ clearTimeout(grace);
165
+ waiting.delete(key);
166
+ resolve(found);
167
+ };
168
+ const outer = setTimeout(done, timeoutMs);
169
+ waiting.set(key, {
170
+ keep: true,
171
+ resolve: (frame) => {
172
+ found.push(frame);
173
+ clearTimeout(grace);
174
+ grace = setTimeout(done, graceMs);
175
+ },
176
+ });
177
+ });
178
+ }
179
+
180
+ // It gives back the answer of the relay, and not the answer of the
181
+ // receiver. "stored" says that the relay holds the message on its disk.
182
+ async function send(wire) {
183
+ const answer = expect(`send:${wire.messageId}`, ANSWER_TIMEOUT_MS);
184
+ connection.send(JSON.stringify({ type: "send", wire }));
185
+ return answer;
186
+ }
187
+
188
+ // It gives every answer, one for each machine of that owner that is
189
+ // online, and an empty list when none is.
190
+ async function routing(owner, project, request) {
191
+ const id = randomUUID();
192
+ const answers = expectMany(`routing:${id}`, { timeoutMs: ANSWER_TIMEOUT_MS });
193
+ connection.send(JSON.stringify({ type: "routing-request", to: owner, id, project, request }));
194
+ return answers;
195
+ }
196
+
197
+ async function publishRecord(record) {
198
+ const answer = expect(`dir:${record.project}:${record.kind}`, ANSWER_TIMEOUT_MS);
199
+ connection.send(JSON.stringify({ type: "dir-publish", record }));
200
+ return answer;
201
+ }
202
+
203
+ async function fetchDirectory(project) {
204
+ const id = randomUUID();
205
+ const answer = expect(`fetch:${id}`, ANSWER_TIMEOUT_MS);
206
+ connection.send(JSON.stringify({ type: "dir-fetch", id, project }));
207
+ return answer;
208
+ }
209
+
210
+ function ack(messageId, to, status, reason = null) {
211
+ connection.send(JSON.stringify({ type: "ack", messageId, to, status, reason }));
212
+ }
213
+ }
214
+
215
+ // The gateway keeps the connection. A relay that stops, or a network that
216
+ // fails, gives a wait that grows, and the gateway tries again.
217
+ export async function keep(url, handlers = {}) {
218
+ let link = null;
219
+ let stopped = false;
220
+ let wait = 1000;
221
+ const holder = {
222
+ get link() {
223
+ return link;
224
+ },
225
+ stop() {
226
+ stopped = true;
227
+ link?.close();
228
+ },
229
+ };
230
+ const again = async () => {
231
+ if (stopped) return;
232
+ try {
233
+ link = await open(url, { ...handlers, onClose: () => {
234
+ handlers.onClose?.();
235
+ link = null;
236
+ if (!stopped) setTimeout(again, wait);
237
+ } });
238
+ wait = 1000;
239
+ handlers.onOpen?.(link);
240
+ } catch (e) {
241
+ handlers.onError?.({ error: e.message });
242
+ wait = Math.min(wait * 2, 60_000);
243
+ if (!stopped) setTimeout(again, wait);
244
+ }
245
+ };
246
+ await again();
247
+ return holder;
248
+ }