wire-mesh-core 4.1.5 → 4.2.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 +2 -1
- package/dist/adapters/tls-transport.cjs +2 -0
- package/dist/adapters/tls-transport.mjs +2 -0
- package/dist/domain/async-queue.cjs +48 -12
- package/dist/domain/async-queue.d.cts +1 -1
- package/dist/domain/async-queue.d.mts +1 -1
- package/dist/domain/async-queue.mjs +48 -12
- package/dist/domain/bulk.cjs +50 -10
- package/dist/domain/bulk.d.cts +5 -1
- package/dist/domain/bulk.d.mts +5 -1
- package/dist/domain/bulk.mjs +50 -11
- package/dist/domain/capability-grant.d.cts +1 -1
- package/dist/domain/capability-grant.d.mts +1 -1
- package/dist/domain/capability-request.cjs +2 -5
- package/dist/domain/capability-request.d.cts +1 -1
- package/dist/domain/capability-request.d.mts +1 -1
- package/dist/domain/capability-request.mjs +1 -4
- package/dist/domain/consume-incoming.cjs +96 -0
- package/dist/domain/consume-incoming.d.cts +27 -0
- package/dist/domain/consume-incoming.d.mts +27 -0
- package/dist/domain/consume-incoming.mjs +93 -0
- package/dist/domain/direct-manage-request.cjs +25 -12
- package/dist/domain/direct-manage-request.d.cts +1 -1
- package/dist/domain/direct-manage-request.d.mts +1 -1
- package/dist/domain/direct-manage-request.mjs +25 -12
- package/dist/domain/fault-report.cjs +136 -0
- package/dist/domain/fault-report.d.cts +25 -0
- package/dist/domain/fault-report.d.mts +25 -0
- package/dist/domain/fault-report.mjs +129 -0
- package/dist/domain/gossip-expansion.cjs +1 -1
- package/dist/domain/gossip-expansion.d.cts +1 -1
- package/dist/domain/gossip-expansion.d.mts +1 -1
- package/dist/domain/gossip-expansion.mjs +1 -1
- package/dist/domain/grant-candidates.d.cts +1 -1
- package/dist/domain/grant-candidates.d.mts +1 -1
- package/dist/domain/mesh-session.cjs +373 -126
- package/dist/domain/mesh-session.d.cts +2 -162
- package/dist/domain/mesh-session.d.mts +2 -162
- package/dist/domain/mesh-session.mjs +373 -126
- package/dist/domain/path-trace.d.cts +1 -1
- package/dist/domain/path-trace.d.mts +1 -1
- package/dist/domain/ping-round-trips.cjs +21 -8
- package/dist/domain/ping-round-trips.mjs +21 -8
- package/dist/domain/room-rekey.d.cts +1 -1
- package/dist/domain/room-rekey.d.mts +1 -1
- package/dist/domain/topology.d.cts +1 -1
- package/dist/domain/topology.d.mts +1 -1
- package/dist/domain/webrtc-signaling.d.cts +1 -1
- package/dist/domain/webrtc-signaling.d.mts +1 -1
- package/dist/mesh-session-By06Mnu-.d.cts +174 -0
- package/dist/mesh-session-DzcZYdhe.d.mts +174 -0
- package/dist/timers-DTJNuBa8.cjs +12 -0
- package/dist/timers-Q1_kDTyY.mjs +7 -0
- package/package.json +11 -2
|
@@ -6,6 +6,7 @@ import { a as OWN_VERSION, i as CORE_VERSION_GOSSIP_KEY, n as TOPOLOGY_PEERS_GOS
|
|
|
6
6
|
import { signPeerAdvert, verifyPeerAdvert } from "./peer-advert.mjs";
|
|
7
7
|
import { createAsyncQueue } from "./async-queue.mjs";
|
|
8
8
|
import { ChannelExhaustedError } from "./channel-exhausted-error.mjs";
|
|
9
|
+
import { createFaultReporter } from "./fault-report.mjs";
|
|
9
10
|
import { localHandshake, negotiate } from "./handshake.mjs";
|
|
10
11
|
import { isDataFrame } from "./hub-mailbox.mjs";
|
|
11
12
|
import { createPingRoundTrips } from "./ping-round-trips.mjs";
|
|
@@ -138,6 +139,15 @@ async function verifyHello(identity, hello) {
|
|
|
138
139
|
};
|
|
139
140
|
}
|
|
140
141
|
}
|
|
142
|
+
/** True when `key` is a usable P-256 ECDH public key, so a channel can be built from a hello carrying it. Cheap enough to run before any key of this side's own is generated. */
|
|
143
|
+
async function isUsableEphemeralKey(key) {
|
|
144
|
+
try {
|
|
145
|
+
await crypto.subtle.importKey("raw", Uint8Array.from(key), ECDH_PARAMS, false, []);
|
|
146
|
+
return true;
|
|
147
|
+
} catch {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
141
151
|
/** The largest counter a channel uses: the counter is an unsigned 64-bit integer on the wire, and this implementation holds it as a JavaScript number, exact up to this value. A frame above it is refused at decoding, never truncated, and sealing refuses to go past it, never wraps. */
|
|
142
152
|
const MAX_COUNTER = Number.MAX_SAFE_INTEGER;
|
|
143
153
|
/** Throws a RangeError unless `counter` is a counter a channel can start from: a non-negative safe integer. */
|
|
@@ -276,148 +286,382 @@ async function establishChannelFrom(identity, { pending, remote, peer, firstSend
|
|
|
276
286
|
}
|
|
277
287
|
//#endregion
|
|
278
288
|
//#region src/domain/relay-channels.ts
|
|
289
|
+
/** How long a peer may take to answer a hello, and the window every other rule here is measured in: a handshake is outstanding for this long, a replaced channel may still open frames that were in flight for this long, and an entry younger than this is never reused. Whoever holds a hello for longer than this gains nothing, since the handshake it belongs to is over. */
|
|
290
|
+
const HANDSHAKE_WINDOW_MS = 3e3;
|
|
291
|
+
/** Everything held for one peer. The fields only change through the methods, so each transition has one place. */
|
|
292
|
+
var PeerChannels = class {
|
|
293
|
+
peer;
|
|
294
|
+
chosen = false;
|
|
295
|
+
lastUsed;
|
|
296
|
+
/** The current channel: sealing goes through it and only it. */
|
|
297
|
+
current;
|
|
298
|
+
/** The channel the current one replaced, which may still open frames that were in flight but never seals. */
|
|
299
|
+
replaced;
|
|
300
|
+
hs;
|
|
301
|
+
/** The candidates of a handshake that ended without confirming either of two, which together serve as the channel until a frame opens under one. */
|
|
302
|
+
pool = [];
|
|
303
|
+
waiters;
|
|
304
|
+
opening = Promise.resolve();
|
|
305
|
+
constructor(peer, now) {
|
|
306
|
+
this.peer = peer;
|
|
307
|
+
this.lastUsed = now;
|
|
308
|
+
}
|
|
309
|
+
get key() {
|
|
310
|
+
return deviceIdToHex(this.peer);
|
|
311
|
+
}
|
|
312
|
+
touch(now) {
|
|
313
|
+
this.lastUsed = now;
|
|
314
|
+
}
|
|
315
|
+
choose() {
|
|
316
|
+
this.chosen = true;
|
|
317
|
+
}
|
|
318
|
+
/** True when `key` is the remote ephemeral key of anything held for the peer. */
|
|
319
|
+
holdsKey(key) {
|
|
320
|
+
return [
|
|
321
|
+
this.current?.remoteKey,
|
|
322
|
+
this.replaced?.remoteKey,
|
|
323
|
+
...(this.hs?.cands ?? []).map((cand) => cand.remoteKey),
|
|
324
|
+
...this.pool.map((cand) => cand.remoteKey)
|
|
325
|
+
].some((held) => held !== void 0 && bytesEqual(held, key));
|
|
326
|
+
}
|
|
327
|
+
/** Whether this side's handshake with the peer is outstanding: sent less than a window ago, and neither confirmed nor ended. */
|
|
328
|
+
outstanding(now) {
|
|
329
|
+
return this.hs !== void 0 && !this.hs.expired && now < this.hs.sentAt + 3e3;
|
|
330
|
+
}
|
|
331
|
+
/** Applies whatever the passing of time has ended: the replaced channel after its window, and a handshake after its window. Checked on every event, so no timer is needed. */
|
|
332
|
+
expire(now) {
|
|
333
|
+
if (this.replaced !== void 0 && now >= this.replaced.until) this.replaced = void 0;
|
|
334
|
+
const { hs } = this;
|
|
335
|
+
if (hs === void 0 || hs.expired || now < hs.sentAt + 3e3) return;
|
|
336
|
+
const [only, ...rest] = hs.cands;
|
|
337
|
+
if (this.current !== void 0) this.hs = void 0;
|
|
338
|
+
else if (only === void 0) hs.expired = true;
|
|
339
|
+
else if (rest.length === 0) {
|
|
340
|
+
this.current = {
|
|
341
|
+
...only,
|
|
342
|
+
confirmed: false
|
|
343
|
+
};
|
|
344
|
+
this.hs = void 0;
|
|
345
|
+
} else {
|
|
346
|
+
this.pool = hs.cands;
|
|
347
|
+
this.hs = void 0;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
/** The candidates a received frame is tried against, in order. */
|
|
351
|
+
candidates() {
|
|
352
|
+
return [...this.hs?.cands ?? [], ...this.pool];
|
|
353
|
+
}
|
|
354
|
+
begin(hs) {
|
|
355
|
+
this.hs = hs;
|
|
356
|
+
}
|
|
357
|
+
/** Ends `hs` if it is still this peer's handshake. */
|
|
358
|
+
end(hs) {
|
|
359
|
+
if (this.hs !== hs) return false;
|
|
360
|
+
this.hs = void 0;
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
confirmCurrent() {
|
|
364
|
+
if (this.current !== void 0) this.current.confirmed = true;
|
|
365
|
+
}
|
|
366
|
+
/** A frame opened under `cand`: it becomes the current channel, the channel it replaces may open in-flight frames for a window, and every other candidate goes. */
|
|
367
|
+
confirm(cand, now) {
|
|
368
|
+
if (this.current !== void 0) this.replaced = {
|
|
369
|
+
channel: this.current.channel,
|
|
370
|
+
remoteKey: this.current.remoteKey,
|
|
371
|
+
until: now + HANDSHAKE_WINDOW_MS
|
|
372
|
+
};
|
|
373
|
+
this.current = {
|
|
374
|
+
...cand,
|
|
375
|
+
confirmed: true
|
|
376
|
+
};
|
|
377
|
+
this.hs = void 0;
|
|
378
|
+
this.pool = [];
|
|
379
|
+
}
|
|
380
|
+
/** Forgets the confirmed channel and the one it replaced, keeping every candidate. */
|
|
381
|
+
dropConfirmed() {
|
|
382
|
+
this.current = void 0;
|
|
383
|
+
this.replaced = void 0;
|
|
384
|
+
}
|
|
385
|
+
/** Forgets the current channel when it is the one that is spent. */
|
|
386
|
+
clearCurrent(channel) {
|
|
387
|
+
if (this.current?.channel === channel) this.current = void 0;
|
|
388
|
+
}
|
|
389
|
+
isEmpty() {
|
|
390
|
+
return this.current === void 0 && this.replaced === void 0 && this.hs === void 0 && this.pool.length === 0;
|
|
391
|
+
}
|
|
392
|
+
/** Resolves once some candidate exists. */
|
|
393
|
+
async waitForChannel() {
|
|
394
|
+
this.waiters ??= (() => {
|
|
395
|
+
const waiting = Promise.withResolvers();
|
|
396
|
+
waiting.promise.catch(() => void 0);
|
|
397
|
+
return waiting;
|
|
398
|
+
})();
|
|
399
|
+
return this.waiters.promise;
|
|
400
|
+
}
|
|
401
|
+
releaseWaiters() {
|
|
402
|
+
this.waiters?.resolve(void 0);
|
|
403
|
+
this.waiters = void 0;
|
|
404
|
+
}
|
|
405
|
+
rejectWaiters(error) {
|
|
406
|
+
this.waiters?.reject(error);
|
|
407
|
+
this.waiters = void 0;
|
|
408
|
+
}
|
|
409
|
+
/** Runs `task` after every earlier one for this peer: two frames opening at once must not both be tried before either has settled what it confirms. */
|
|
410
|
+
async serialise(task) {
|
|
411
|
+
const run = this.opening.then(task);
|
|
412
|
+
this.opening = run.catch(() => void 0);
|
|
413
|
+
return run;
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
function authentic(outcome) {
|
|
417
|
+
return outcome.ok || outcome.reason === "undecodable";
|
|
418
|
+
}
|
|
419
|
+
async function openFirst(cands, data) {
|
|
420
|
+
const [first, ...rest] = cands;
|
|
421
|
+
if (first === void 0) return;
|
|
422
|
+
const outcome = await first.channel.openOutcome(data);
|
|
423
|
+
return authentic(outcome) ? {
|
|
424
|
+
cand: first,
|
|
425
|
+
outcome
|
|
426
|
+
} : openFirst(rest, data);
|
|
427
|
+
}
|
|
428
|
+
/** A clock that never goes backwards: milliseconds from an arbitrary origin, as `performance.now()` gives them. What `createMeshSession` gives its channels. */
|
|
429
|
+
const monotonicClock = { now: () => performance.now() };
|
|
279
430
|
function createRelayChannels(identity, sendHello, options = {}) {
|
|
280
431
|
if (options.firstSendCounter !== void 0) assertFirstSendCounter(options.firstSendCounter);
|
|
281
|
-
const
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
432
|
+
const newHandshake = options.newHandshake ?? (async (peer) => beginHandshake(identity, peer));
|
|
433
|
+
let latest = Number.NEGATIVE_INFINITY;
|
|
434
|
+
const source = options.clock ?? monotonicClock;
|
|
435
|
+
const clock = { now: () => {
|
|
436
|
+
latest = Math.max(latest, source.now());
|
|
437
|
+
return latest;
|
|
438
|
+
} };
|
|
439
|
+
const peers = /* @__PURE__ */ new Map();
|
|
440
|
+
const reporter = createFaultReporter(() => clock.now());
|
|
441
|
+
/** Tells the owner of a failure that is local to this side, through the shared fault reporter: at most one report per peer and interval, with the number withheld, and the callback's own failure contained there. */
|
|
442
|
+
function reportFault(error, peer) {
|
|
443
|
+
reporter(peer === void 0 ? "" : deviceIdToHex(peer), async (suppressed) => options.onFault?.(error, {
|
|
444
|
+
peer,
|
|
445
|
+
suppressed
|
|
446
|
+
}));
|
|
293
447
|
}
|
|
294
|
-
|
|
295
|
-
|
|
448
|
+
function prune(entry) {
|
|
449
|
+
if (entry.isEmpty() && !entry.chosen && peers.get(entry.key) === entry) peers.delete(entry.key);
|
|
450
|
+
}
|
|
451
|
+
/** Ends this side's handshake `hs` with the peer, leaving every channel as it is, and tells whoever waits for a channel when there is none to wait for. */
|
|
452
|
+
function dropHandshake(entry, hs, error) {
|
|
453
|
+
if (!entry.end(hs)) return;
|
|
296
454
|
const failure = error instanceof Error ? error : new Error(String(error));
|
|
297
|
-
|
|
298
|
-
entry.
|
|
299
|
-
entry
|
|
300
|
-
}
|
|
301
|
-
/**
|
|
302
|
-
function
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
455
|
+
hs.sent.reject(failure);
|
|
456
|
+
if (entry.current === void 0 && entry.pool.length === 0) entry.rejectWaiters(failure);
|
|
457
|
+
prune(entry);
|
|
458
|
+
}
|
|
459
|
+
/** Drops every handshake that is waiting for its first answer beyond MAX_PENDING_HANDSHAKES, oldest first. */
|
|
460
|
+
function enforcePendingBound(keep) {
|
|
461
|
+
const pending = [...peers.values()].filter((entry) => entry.hs?.origin === "initiated" && entry.hs.cands.length === 0 && entry.current === void 0);
|
|
462
|
+
const excess = pending.filter((entry) => entry !== keep).sort((a, b) => (a.hs?.sentAt ?? 0) - (b.hs?.sentAt ?? 0)).slice(0, Math.max(0, pending.length - 256));
|
|
463
|
+
for (const entry of excess) if (entry.hs !== void 0) dropHandshake(entry, entry.hs, /* @__PURE__ */ new Error("the handshake was dropped to make room for others"));
|
|
464
|
+
}
|
|
465
|
+
/** Begins this side's own hello to the entry's peer and registers it at once, so a hello arriving meanwhile completes it instead of starting another. */
|
|
466
|
+
function beginOwn(entry, origin) {
|
|
467
|
+
const mine = newHandshake(entry.peer);
|
|
468
|
+
const hs = {
|
|
469
|
+
mine,
|
|
470
|
+
origin,
|
|
471
|
+
sentAt: clock.now(),
|
|
472
|
+
cands: [],
|
|
473
|
+
sent: Promise.withResolvers(),
|
|
474
|
+
expired: false
|
|
314
475
|
};
|
|
315
|
-
|
|
316
|
-
entry.
|
|
317
|
-
|
|
476
|
+
hs.sent.promise.catch(() => void 0);
|
|
477
|
+
entry.begin(hs);
|
|
478
|
+
mine.then(async ({ hello }) => {
|
|
318
479
|
try {
|
|
319
|
-
await sendHello(peer, hello);
|
|
480
|
+
await sendHello(entry.peer, hello);
|
|
320
481
|
} catch (error) {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
const held = channels.get(key);
|
|
324
|
-
if (held?.status === "established" && held.channel === channel) channels.delete(key);
|
|
325
|
-
}, () => void 0);
|
|
326
|
-
if (answering && track) reportFault(error);
|
|
482
|
+
dropHandshake(entry, hs, error);
|
|
483
|
+
if (origin === "answered") reportFault(error, entry.peer);
|
|
327
484
|
return;
|
|
328
485
|
}
|
|
329
|
-
|
|
486
|
+
hs.sent.resolve(void 0);
|
|
330
487
|
}, (error) => {
|
|
331
|
-
|
|
488
|
+
dropHandshake(entry, hs, error);
|
|
332
489
|
});
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
const established = await establishChannelFrom(identity, {
|
|
340
|
-
pending: await entry.pending,
|
|
490
|
+
return hs;
|
|
491
|
+
}
|
|
492
|
+
/** Builds the channel `hello` makes with this side's handshake `hs`, and holds it as a candidate. */
|
|
493
|
+
async function addCandidate(entry, hs, hello, peer) {
|
|
494
|
+
const channel = await establishChannelFrom(identity, {
|
|
495
|
+
pending: await hs.mine,
|
|
341
496
|
remote: hello,
|
|
342
497
|
peer,
|
|
343
498
|
...options.firstSendCounter === void 0 ? {} : { firstSendCounter: options.firstSendCounter }
|
|
344
499
|
});
|
|
345
|
-
if (
|
|
346
|
-
|
|
347
|
-
return;
|
|
348
|
-
}
|
|
349
|
-
if (replaces !== void 0) await entry.sent.promise;
|
|
350
|
-
if (channels.get(key) !== (replaces ?? entry)) return;
|
|
351
|
-
const channel = {
|
|
352
|
-
...established,
|
|
353
|
-
seal: async (frame) => {
|
|
354
|
-
try {
|
|
355
|
-
return await established.seal(frame);
|
|
356
|
-
} catch (error) {
|
|
357
|
-
const held = channels.get(key);
|
|
358
|
-
if (error instanceof ChannelExhaustedError && held?.status === "established" && held.channel === channel) channels.delete(key);
|
|
359
|
-
throw error;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
};
|
|
363
|
-
channels.set(key, {
|
|
364
|
-
status: "established",
|
|
500
|
+
if (channel === void 0 || entry.hs !== hs || peers.get(entry.key) !== entry || hs.cands.length >= 2 || entry.holdsKey(hello["ephemeral-key"])) return;
|
|
501
|
+
hs.cands.push({
|
|
365
502
|
channel,
|
|
366
|
-
|
|
503
|
+
remoteKey: hello["ephemeral-key"]
|
|
367
504
|
});
|
|
368
|
-
entry.
|
|
505
|
+
entry.releaseWaiters();
|
|
506
|
+
}
|
|
507
|
+
/** The entry of a peer this side never sent anything to that may be reused now, or undefined when every slot is free or none is old enough, which is told by `slotIsFree`. An entry is reused only once it is older than the window, so answers cannot outpace the turnover of slots. */
|
|
508
|
+
function reusableEntry(now) {
|
|
509
|
+
const [victim] = [...peers.values()].filter((entry) => !entry.chosen && now - entry.lastUsed >= 3e3).sort((a, b) => a.lastUsed - b.lastUsed);
|
|
510
|
+
return victim;
|
|
511
|
+
}
|
|
512
|
+
function slotIsFree() {
|
|
513
|
+
return [...peers.values()].filter((entry) => !entry.chosen).length < 64;
|
|
514
|
+
}
|
|
515
|
+
/** Whether a peer this side never sent anything to could be given an entry now, changing nothing. */
|
|
516
|
+
function slotAvailable(now) {
|
|
517
|
+
return slotIsFree() || reusableEntry(now) !== void 0;
|
|
518
|
+
}
|
|
519
|
+
/** Makes room for a peer this side never sent anything to. */
|
|
520
|
+
function reserveUnsolicitedSlot(now) {
|
|
521
|
+
if (slotIsFree()) return true;
|
|
522
|
+
const victim = reusableEntry(now);
|
|
523
|
+
if (victim === void 0) return false;
|
|
524
|
+
discard(victim, /* @__PURE__ */ new Error("the entry was dropped to make room for others"));
|
|
525
|
+
return true;
|
|
526
|
+
}
|
|
527
|
+
function discard(entry, error) {
|
|
528
|
+
peers.delete(entry.key);
|
|
529
|
+
entry.hs?.sent.reject(error);
|
|
530
|
+
entry.rejectWaiters(error);
|
|
531
|
+
}
|
|
532
|
+
async function sealUnder(entry, channel, frame) {
|
|
533
|
+
try {
|
|
534
|
+
return await channel.seal(frame);
|
|
535
|
+
} catch (error) {
|
|
536
|
+
if (error instanceof ChannelExhaustedError) entry.clearCurrent(channel);
|
|
537
|
+
throw error;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
async function sealFor(entry, frame) {
|
|
541
|
+
entry.expire(clock.now());
|
|
542
|
+
entry.touch(clock.now());
|
|
543
|
+
const { current, hs } = entry;
|
|
544
|
+
if (current !== void 0) return [await sealUnder(entry, current.channel, frame)];
|
|
545
|
+
if (entry.pool.length > 0) return Promise.all(entry.pool.map(async (cand) => sealUnder(entry, cand.channel, frame)));
|
|
546
|
+
if (hs === void 0) {
|
|
547
|
+
beginOwn(entry, "initiated");
|
|
548
|
+
enforcePendingBound(entry);
|
|
549
|
+
return sealFor(entry, frame);
|
|
550
|
+
}
|
|
551
|
+
if (hs.cands.length > 0) {
|
|
552
|
+
await hs.sent.promise;
|
|
553
|
+
return Promise.all(hs.cands.map(async (cand) => sealUnder(entry, cand.channel, frame)));
|
|
554
|
+
}
|
|
555
|
+
await entry.waitForChannel();
|
|
556
|
+
return sealFor(entry, frame);
|
|
557
|
+
}
|
|
558
|
+
/** Tries a frame against the peer's channels, in order: the current one, then the candidates, then the replaced one while its window lasts. */
|
|
559
|
+
async function openUnder(entry, data) {
|
|
560
|
+
entry.expire(clock.now());
|
|
561
|
+
const { current } = entry;
|
|
562
|
+
if (current !== void 0) {
|
|
563
|
+
const outcome = await current.channel.openOutcome(data);
|
|
564
|
+
if (authentic(outcome)) {
|
|
565
|
+
entry.confirmCurrent();
|
|
566
|
+
entry.touch(clock.now());
|
|
567
|
+
return outcome.ok ? {
|
|
568
|
+
frame: outcome.frame,
|
|
569
|
+
from: current.channel.peer.deviceId
|
|
570
|
+
} : void 0;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
const found = await openFirst(entry.candidates(), data);
|
|
574
|
+
if (found !== void 0) {
|
|
575
|
+
if (peers.get(entry.key) !== entry) return;
|
|
576
|
+
entry.confirm(found.cand, clock.now());
|
|
577
|
+
entry.touch(clock.now());
|
|
578
|
+
return found.outcome.ok ? {
|
|
579
|
+
frame: found.outcome.frame,
|
|
580
|
+
from: found.cand.channel.peer.deviceId
|
|
581
|
+
} : void 0;
|
|
582
|
+
}
|
|
583
|
+
const { replaced } = entry;
|
|
584
|
+
if (replaced !== void 0 && clock.now() < replaced.until) {
|
|
585
|
+
const outcome = await replaced.channel.openOutcome(data);
|
|
586
|
+
return outcome.ok ? {
|
|
587
|
+
frame: outcome.frame,
|
|
588
|
+
from: replaced.channel.peer.deviceId
|
|
589
|
+
} : void 0;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
async function acceptHello(hello, sender) {
|
|
593
|
+
const key = deviceIdToHex(sender);
|
|
594
|
+
const verdict = await verifyHello(identity, hello);
|
|
595
|
+
if (!verdict.ok || !await isUsableEphemeralKey(hello["ephemeral-key"])) return;
|
|
596
|
+
const now = clock.now();
|
|
597
|
+
let entry = peers.get(key);
|
|
598
|
+
entry?.expire(now);
|
|
599
|
+
if (entry?.holdsKey(hello["ephemeral-key"]) === true) return;
|
|
600
|
+
if (entry?.hs !== void 0 && entry.outstanding(now)) {
|
|
601
|
+
if (entry.hs.cands.length < 2) await addCandidate(entry, entry.hs, hello, verdict.peer);
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
if (entry === void 0) {
|
|
605
|
+
if (!reserveUnsolicitedSlot(now)) return;
|
|
606
|
+
entry = new PeerChannels(sender, now);
|
|
607
|
+
peers.set(key, entry);
|
|
608
|
+
}
|
|
609
|
+
const hs = beginOwn(entry, "answered");
|
|
610
|
+
entry.touch(now);
|
|
611
|
+
try {
|
|
612
|
+
await addCandidate(entry, hs, hello, verdict.peer);
|
|
613
|
+
} catch (error) {
|
|
614
|
+
dropHandshake(entry, hs, error);
|
|
615
|
+
throw error;
|
|
616
|
+
}
|
|
369
617
|
}
|
|
370
618
|
return {
|
|
371
|
-
|
|
619
|
+
seal: async (peer, frame, purpose) => {
|
|
372
620
|
const key = deviceIdToHex(peer);
|
|
373
|
-
const
|
|
374
|
-
|
|
375
|
-
|
|
621
|
+
const now = clock.now();
|
|
622
|
+
let entry = peers.get(key);
|
|
623
|
+
if (entry === void 0) {
|
|
624
|
+
if (purpose === "reply" && !reserveUnsolicitedSlot(now)) throw new Error("there is no channel with this peer");
|
|
625
|
+
entry = new PeerChannels(peer, now);
|
|
626
|
+
peers.set(key, entry);
|
|
627
|
+
}
|
|
628
|
+
if (purpose === "request") entry.choose();
|
|
629
|
+
return sealFor(entry, frame);
|
|
376
630
|
},
|
|
377
|
-
applyHello: async (hello) => {
|
|
378
|
-
let
|
|
631
|
+
applyHello: async (hello, claimedFrom) => {
|
|
632
|
+
let sender;
|
|
379
633
|
try {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
const
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
await complete({
|
|
391
|
-
entry,
|
|
392
|
-
hello,
|
|
393
|
-
peer: verdict.peer,
|
|
394
|
-
replaces
|
|
395
|
-
});
|
|
634
|
+
if (claimedFrom === void 0) return;
|
|
635
|
+
sender = await identity.deriveDeviceId(hello["identity-key"]["public-key"]);
|
|
636
|
+
if (!bytesEqual(sender, claimedFrom)) return;
|
|
637
|
+
const held = peers.get(deviceIdToHex(sender));
|
|
638
|
+
const now = clock.now();
|
|
639
|
+
held?.expire(now);
|
|
640
|
+
if (held?.holdsKey(hello["ephemeral-key"]) === true) return;
|
|
641
|
+
if (held?.hs !== void 0 && held.outstanding(now) && held.hs.cands.length >= 2) return;
|
|
642
|
+
if (held === void 0 && !slotAvailable(now)) return;
|
|
643
|
+
await acceptHello(hello, sender);
|
|
396
644
|
} catch (error) {
|
|
397
|
-
|
|
398
|
-
reportFault(error);
|
|
645
|
+
reportFault(error, sender);
|
|
399
646
|
}
|
|
400
647
|
},
|
|
401
648
|
open: async (data, claimedFrom) => {
|
|
402
649
|
if (claimedFrom === void 0) return;
|
|
403
|
-
const
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
const frame = await entry.channel.open(data);
|
|
407
|
-
return frame === void 0 ? void 0 : {
|
|
408
|
-
frame,
|
|
409
|
-
from: entry.channel.peer.deviceId
|
|
410
|
-
};
|
|
650
|
+
const entry = peers.get(deviceIdToHex(claimedFrom));
|
|
651
|
+
if (entry === void 0) return;
|
|
652
|
+
return entry.serialise(async () => openUnder(entry, data));
|
|
411
653
|
},
|
|
412
654
|
reset: (reason) => {
|
|
413
|
-
for (const entry of [...
|
|
414
|
-
channels.clear();
|
|
655
|
+
for (const entry of [...peers.values()]) discard(entry, new Error(reason));
|
|
415
656
|
},
|
|
416
657
|
forget: (peer) => {
|
|
417
|
-
const
|
|
418
|
-
|
|
419
|
-
if (entry?.
|
|
420
|
-
|
|
658
|
+
const entry = peers.get(deviceIdToHex(peer));
|
|
659
|
+
if (entry === void 0) return;
|
|
660
|
+
if ((entry.hs?.cands.length ?? 0) + entry.pool.length === 0) {
|
|
661
|
+
discard(entry, /* @__PURE__ */ new Error("the relay pairing was lost"));
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
entry.dropConfirmed();
|
|
421
665
|
}
|
|
422
666
|
};
|
|
423
667
|
}
|
|
@@ -502,19 +746,19 @@ function createSessionCore(identity, clock, reconnect, dial, onPeerAdvert, addre
|
|
|
502
746
|
}
|
|
503
747
|
/** The "not connected" guard every send* method on the public session needs, deduplicated into one place rather than repeated inline at each call site: throws unless there is a live connection AND the session's own state machine agrees it is "connected" (transmit's own connection===null check alone doesn't cover the latter). Returns the connection itself, narrowed non-null, for a caller that needs to send directly on it (sendPing/sendPingMeasureRtt); a caller that only needs the throwing side effect (sendManageRequest and friends, which go through transmit instead) calls this and discards the result. */
|
|
504
748
|
function requireConnectedLink() {
|
|
505
|
-
if (connection === null || state.status !== "connected") throw new Error("not connected");
|
|
749
|
+
if (feedCancelled || connection === null || state.status !== "connected") throw new Error("not connected");
|
|
506
750
|
return connection;
|
|
507
751
|
}
|
|
508
752
|
/** Sends a frame. Through a relay it is sealed for toDevice on their secure channel first (spec/secure-channel.cddl) and only then wrapped as relay-data, so the hub carries ciphertext. This is the single choke point every outbound manage-request and manage-response passes through, so a consumer of sendManageRequest or respond never needs its own relay logic. */
|
|
509
|
-
async function transmit(frame,
|
|
753
|
+
async function transmit(frame, toDevice, purpose = "request") {
|
|
510
754
|
if (connection === null) throw new Error("not connected");
|
|
511
|
-
if (
|
|
755
|
+
if (toDevice === void 0) {
|
|
512
756
|
await connection.send(frame);
|
|
513
757
|
return;
|
|
514
758
|
}
|
|
515
|
-
|
|
516
|
-
const sealed = await
|
|
517
|
-
await
|
|
759
|
+
const link = connection;
|
|
760
|
+
const sealed = await relayChannels.seal(toDevice, frame, purpose);
|
|
761
|
+
await Promise.all(sealed.map(async (copy) => link.send(wrapRelayData(copy, toDevice))));
|
|
518
762
|
}
|
|
519
763
|
/** `from` is the device whose secure channel the response arrived through, absent for one that arrived directly. A pending request only accepts an answer from where it was sent: a peer must not be able to answer a request meant for another by guessing its request-id. */
|
|
520
764
|
function applyManageResponse(frame, from) {
|
|
@@ -548,7 +792,7 @@ function createSessionCore(identity, clock, reconnect, dial, onPeerAdvert, addre
|
|
|
548
792
|
direction: "sent",
|
|
549
793
|
frame: response
|
|
550
794
|
});
|
|
551
|
-
await transmit(response,
|
|
795
|
+
await transmit(response, fromDevice, "reply");
|
|
552
796
|
emit();
|
|
553
797
|
}
|
|
554
798
|
};
|
|
@@ -566,7 +810,7 @@ function createSessionCore(identity, clock, reconnect, dial, onPeerAdvert, addre
|
|
|
566
810
|
direction: "received",
|
|
567
811
|
frame: inner
|
|
568
812
|
});
|
|
569
|
-
await relayChannels.applyHello(inner);
|
|
813
|
+
await relayChannels.applyHello(inner, frame["from-device"]);
|
|
570
814
|
return;
|
|
571
815
|
}
|
|
572
816
|
if (inner?.type === "secure-data") {
|
|
@@ -833,7 +1077,10 @@ function createSessionCore(identity, clock, reconnect, dial, onPeerAdvert, addre
|
|
|
833
1077
|
},
|
|
834
1078
|
async sendManageRequest(command, scope, targetDevice, token, timeoutMs) {
|
|
835
1079
|
requireConnectedLink();
|
|
836
|
-
if (targetDevice !== void 0)
|
|
1080
|
+
if (targetDevice !== void 0) {
|
|
1081
|
+
await ensureRelayPairing(targetDevice);
|
|
1082
|
+
requireConnectedLink();
|
|
1083
|
+
}
|
|
837
1084
|
const frame = buildManageRequest(command, scope, token);
|
|
838
1085
|
const requestId = frame["request-id"];
|
|
839
1086
|
const outcome = new Promise((resolve, reject) => {
|
|
@@ -843,16 +1090,16 @@ function createSessionCore(identity, clock, reconnect, dial, onPeerAdvert, addre
|
|
|
843
1090
|
expectedFrom: targetDevice
|
|
844
1091
|
});
|
|
845
1092
|
});
|
|
1093
|
+
outcome.catch(() => void 0);
|
|
846
1094
|
recordFrame({
|
|
847
1095
|
direction: "sent",
|
|
848
1096
|
frame
|
|
849
1097
|
});
|
|
850
1098
|
const exchange = (async () => {
|
|
851
1099
|
try {
|
|
852
|
-
await transmit(frame, targetDevice
|
|
1100
|
+
await transmit(frame, targetDevice);
|
|
853
1101
|
} catch (error) {
|
|
854
1102
|
pendingManageRequests.delete(requestId);
|
|
855
|
-
outcome.catch(() => void 0);
|
|
856
1103
|
throw error;
|
|
857
1104
|
}
|
|
858
1105
|
emit();
|
|
@@ -887,7 +1134,7 @@ function createSessionCore(identity, clock, reconnect, dial, onPeerAdvert, addre
|
|
|
887
1134
|
direction: "sent",
|
|
888
1135
|
frame
|
|
889
1136
|
});
|
|
890
|
-
await transmit(frame
|
|
1137
|
+
await transmit(frame);
|
|
891
1138
|
emit();
|
|
892
1139
|
},
|
|
893
1140
|
async sendCoordinatorClaim(frame) {
|
|
@@ -897,7 +1144,7 @@ function createSessionCore(identity, clock, reconnect, dial, onPeerAdvert, addre
|
|
|
897
1144
|
direction: "sent",
|
|
898
1145
|
frame: claim
|
|
899
1146
|
});
|
|
900
|
-
await transmit(claim
|
|
1147
|
+
await transmit(claim);
|
|
901
1148
|
emit();
|
|
902
1149
|
},
|
|
903
1150
|
async sendGossipUpdate(extensions) {
|
|
@@ -907,7 +1154,7 @@ function createSessionCore(identity, clock, reconnect, dial, onPeerAdvert, addre
|
|
|
907
1154
|
direction: "sent",
|
|
908
1155
|
frame
|
|
909
1156
|
});
|
|
910
|
-
await transmit(frame
|
|
1157
|
+
await transmit(frame);
|
|
911
1158
|
emit();
|
|
912
1159
|
},
|
|
913
1160
|
getTopologyPeers() {
|
|
@@ -920,7 +1167,7 @@ function createSessionCore(identity, clock, reconnect, dial, onPeerAdvert, addre
|
|
|
920
1167
|
direction: "sent",
|
|
921
1168
|
frame
|
|
922
1169
|
});
|
|
923
|
-
await transmit(frame, targetDevice
|
|
1170
|
+
await transmit(frame, targetDevice);
|
|
924
1171
|
emit();
|
|
925
1172
|
},
|
|
926
1173
|
async close() {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { D as DeviceId, H as ManageCommandParams, Q as PathTrace, V as ManageCommand, f as CapabilityScope, p as CapabilityToken } from "../protocol-Cb3BnuTL.cjs";
|
|
2
2
|
import { t as Clock } from "../clock-DhUPRfLh.cjs";
|
|
3
|
-
import { IncomingManageRequest, ManageOutcome, MeshSession } from "
|
|
3
|
+
import { d as IncomingManageRequest, f as ManageOutcome, p as MeshSession } from "../mesh-session-By06Mnu-.cjs";
|
|
4
4
|
//#region src/domain/path-trace.d.ts
|
|
5
5
|
/** path:trace names no real capability -- see spec/management.cddl's own comment -- and is registered purely as a namespaced wire identifier so manage-command.verb's own capability-verb grammar has something syntactically valid to carry (registry/core-capabilities.md). */
|
|
6
6
|
export declare const PATH_TRACE_VERB = "path:trace";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { D as DeviceId, H as ManageCommandParams, Q as PathTrace, V as ManageCommand, f as CapabilityScope, p as CapabilityToken } from "../protocol-Cb3BnuTL.mjs";
|
|
2
2
|
import { t as Clock } from "../clock-DhUPRfLh.mjs";
|
|
3
|
-
import { IncomingManageRequest, ManageOutcome, MeshSession } from "
|
|
3
|
+
import { d as IncomingManageRequest, f as ManageOutcome, p as MeshSession } from "../mesh-session-DzcZYdhe.mjs";
|
|
4
4
|
//#region src/domain/path-trace.d.ts
|
|
5
5
|
/** path:trace names no real capability -- see spec/management.cddl's own comment -- and is registered purely as a namespaced wire identifier so manage-command.verb's own capability-verb grammar has something syntactically valid to carry (registry/core-capabilities.md). */
|
|
6
6
|
export declare const PATH_TRACE_VERB = "path:trace";
|