wire-mesh-core 3.8.0 → 3.10.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.
Files changed (123) hide show
  1. package/dist/adapters/byte-stream-connection.d.cts +2 -2
  2. package/dist/adapters/byte-stream-connection.d.mts +2 -2
  3. package/dist/adapters/frame-codec.d.cts +1 -1
  4. package/dist/adapters/frame-codec.d.mts +1 -1
  5. package/dist/adapters/node-identity.d.cts +2 -2
  6. package/dist/adapters/node-identity.d.mts +2 -2
  7. package/dist/adapters/tcp-transport.d.cts +1 -1
  8. package/dist/adapters/tcp-transport.d.mts +1 -1
  9. package/dist/adapters/threshold-identity.d.cts +2 -2
  10. package/dist/adapters/threshold-identity.d.mts +2 -2
  11. package/dist/adapters/threshold-wasm.d.cts +1 -1
  12. package/dist/adapters/threshold-wasm.d.mts +1 -1
  13. package/dist/adapters/tls-transport.d.cts +1 -1
  14. package/dist/adapters/tls-transport.d.mts +1 -1
  15. package/dist/domain/advert-extension-policy.d.cts +1 -1
  16. package/dist/domain/advert-extension-policy.d.mts +1 -1
  17. package/dist/domain/bulk.d.cts +2 -2
  18. package/dist/domain/bulk.d.mts +2 -2
  19. package/dist/domain/capability-grant.cjs +4 -4
  20. package/dist/domain/capability-grant.d.cts +3 -3
  21. package/dist/domain/capability-grant.d.mts +3 -3
  22. package/dist/domain/capability-grant.mjs +2 -2
  23. package/dist/domain/capability-request.cjs +18 -6
  24. package/dist/domain/capability-request.d.cts +10 -3
  25. package/dist/domain/capability-request.d.mts +10 -3
  26. package/dist/domain/capability-request.mjs +15 -4
  27. package/dist/domain/coordinator-election.d.cts +1 -1
  28. package/dist/domain/coordinator-election.d.mts +1 -1
  29. package/dist/domain/data-sync.d.cts +2 -2
  30. package/dist/domain/data-sync.d.mts +2 -2
  31. package/dist/domain/device-id.d.cts +1 -1
  32. package/dist/domain/device-id.d.mts +1 -1
  33. package/dist/domain/direct-manage-request.d.cts +2 -2
  34. package/dist/domain/direct-manage-request.d.mts +2 -2
  35. package/dist/domain/gossip-expansion.d.cts +1 -1
  36. package/dist/domain/gossip-expansion.d.mts +1 -1
  37. package/dist/domain/grant-candidates.cjs +2 -2
  38. package/dist/domain/grant-candidates.d.cts +1 -1
  39. package/dist/domain/grant-candidates.d.mts +1 -1
  40. package/dist/domain/grant-candidates.mjs +1 -1
  41. package/dist/domain/handshake.d.cts +1 -1
  42. package/dist/domain/handshake.d.mts +1 -1
  43. package/dist/domain/hub-mailbox.d.cts +1 -1
  44. package/dist/domain/hub-mailbox.d.mts +1 -1
  45. package/dist/domain/mesh-session.cjs +2 -2
  46. package/dist/domain/mesh-session.d.cts +3 -3
  47. package/dist/domain/mesh-session.d.mts +3 -3
  48. package/dist/domain/mesh-session.mjs +1 -1
  49. package/dist/domain/notice-board.d.cts +3 -3
  50. package/dist/domain/notice-board.d.mts +3 -3
  51. package/dist/domain/path-trace.d.cts +1 -1
  52. package/dist/domain/path-trace.d.mts +1 -1
  53. package/dist/domain/peer-advert.cjs +2 -2
  54. package/dist/domain/peer-advert.d.cts +2 -2
  55. package/dist/domain/peer-advert.d.mts +2 -2
  56. package/dist/domain/peer-advert.mjs +1 -1
  57. package/dist/domain/relay-advert.d.cts +1 -1
  58. package/dist/domain/relay-advert.d.mts +1 -1
  59. package/dist/domain/relay-hub.cjs +72 -15
  60. package/dist/domain/relay-hub.d.cts +6 -2
  61. package/dist/domain/relay-hub.d.mts +6 -2
  62. package/dist/domain/relay-hub.mjs +72 -15
  63. package/dist/domain/relay-use-gate.d.cts +2 -2
  64. package/dist/domain/relay-use-gate.d.mts +2 -2
  65. package/dist/domain/revocation-view.cjs +2 -2
  66. package/dist/domain/revocation-view.d.cts +2 -2
  67. package/dist/domain/revocation-view.d.mts +2 -2
  68. package/dist/domain/revocation-view.mjs +1 -1
  69. package/dist/domain/room-rekey.d.cts +3 -3
  70. package/dist/domain/room-rekey.d.mts +3 -3
  71. package/dist/domain/room-token-verification.cjs +2 -2
  72. package/dist/domain/room-token-verification.d.cts +1 -1
  73. package/dist/domain/room-token-verification.d.mts +1 -1
  74. package/dist/domain/room-token-verification.mjs +1 -1
  75. package/dist/domain/room.d.cts +4 -4
  76. package/dist/domain/room.d.mts +4 -4
  77. package/dist/domain/shard-manifest.d.cts +1 -1
  78. package/dist/domain/shard-manifest.d.mts +1 -1
  79. package/dist/domain/threshold-subject.cjs +2 -2
  80. package/dist/domain/threshold-subject.mjs +1 -1
  81. package/dist/domain/token-predicates.cjs +184 -0
  82. package/dist/domain/token-predicates.d.cts +2 -0
  83. package/dist/domain/token-predicates.d.mts +2 -0
  84. package/dist/domain/token-predicates.mjs +173 -0
  85. package/dist/{token-scope-CxHXTT3u.cjs → domain/token-scope.cjs} +3 -12
  86. package/dist/domain/token-scope.d.cts +11 -0
  87. package/dist/domain/token-scope.d.mts +11 -0
  88. package/dist/{token-scope-Z4bmci4M.mjs → domain/token-scope.mjs} +1 -1
  89. package/dist/domain/tokens.cjs +466 -9
  90. package/dist/domain/tokens.d.cts +131 -2
  91. package/dist/domain/tokens.d.mts +131 -2
  92. package/dist/domain/tokens.mjs +458 -1
  93. package/dist/domain/topology-snapshot.d.cts +1 -1
  94. package/dist/domain/topology-snapshot.d.mts +1 -1
  95. package/dist/domain/topology.d.cts +1 -1
  96. package/dist/domain/topology.d.mts +1 -1
  97. package/dist/domain/webrtc-signaling.cjs +2 -2
  98. package/dist/domain/webrtc-signaling.d.cts +2 -2
  99. package/dist/domain/webrtc-signaling.d.mts +2 -2
  100. package/dist/domain/webrtc-signaling.mjs +1 -1
  101. package/dist/generated/protocol.cjs +4 -2
  102. package/dist/generated/protocol.d.cts +1 -1
  103. package/dist/generated/protocol.d.mts +1 -1
  104. package/dist/generated/protocol.mjs +4 -2
  105. package/dist/{identity-DJbr96J3.d.cts → identity-B6nhlSOT.d.cts} +1 -1
  106. package/dist/{identity-ViYdZrIU.d.mts → identity-EV-HHv_D.d.mts} +1 -1
  107. package/dist/ports/identity.d.cts +1 -1
  108. package/dist/ports/identity.d.mts +1 -1
  109. package/dist/ports/transport.d.cts +1 -1
  110. package/dist/ports/transport.d.mts +1 -1
  111. package/dist/{protocol-BgR1e_NI.d.cts → protocol-DzmpxxUr.d.cts} +6 -0
  112. package/dist/{protocol-BgR1e_NI.d.mts → protocol-DzmpxxUr.d.mts} +6 -0
  113. package/dist/{room-token-verification-7zD4ib-A.d.mts → room-token-verification-Dh7SGJfK.d.cts} +2 -2
  114. package/dist/{room-token-verification-hFtBDZPO.d.cts → room-token-verification-Hb1LkpFh.d.mts} +2 -2
  115. package/dist/token-predicates-D4NNPLnp.d.cts +72 -0
  116. package/dist/token-predicates-DqpG8VZZ.d.mts +72 -0
  117. package/dist/{transport-Dv_LTCsF.d.mts → transport-bD3p_TO_.d.mts} +1 -1
  118. package/dist/{transport-DP3hdehk.d.cts → transport-gDbSHO-r.d.cts} +1 -1
  119. package/package.json +9 -1
  120. package/dist/tokens-BMWCnQEF.d.cts +0 -158
  121. package/dist/tokens-D8JlTJNA.mjs +0 -617
  122. package/dist/tokens-DRkm_QOR.d.mts +0 -158
  123. package/dist/tokens-tPJQ7Vzt.cjs +0 -664
@@ -1,6 +1,6 @@
1
1
  import { messageFromFrame, tryDecodeFrame, wrapRelayData } from "../adapters/frame-codec.mjs";
2
2
  import { deviceIdToHex } from "./device-id.mjs";
3
- import { t as bytesEqual } from "../token-scope-Z4bmci4M.mjs";
3
+ import { bytesEqual } from "./token-scope.mjs";
4
4
  import { a as OWN_VERSION, i as CORE_VERSION_GOSSIP_KEY, n as TOPOLOGY_PEERS_GOSSIP_KEY, o as isVersionGetCommand, r as validateGossipExtensions, s as versionGetOutcome } from "../gossip-extensions-B6WJjkAr.mjs";
5
5
  import { signPeerAdvert, verifyPeerAdvert } from "./peer-advert.mjs";
6
6
  import { createAsyncQueue } from "./async-queue.mjs";
@@ -1,8 +1,8 @@
1
- import { D as DeviceId, Ot as RoomNotice, T as DataHaveFrame, kt as RoomNoticeClaims, p as CapabilityToken } from "../protocol-BgR1e_NI.cjs";
1
+ import { D as DeviceId, Ot as RoomNotice, T as DataHaveFrame, kt as RoomNoticeClaims, p as CapabilityToken } from "../protocol-DzmpxxUr.cjs";
2
2
  import { t as KeyValueStorage } from "../storage-B403CRyu.cjs";
3
- import { t as IdentityPort } from "../identity-DJbr96J3.cjs";
3
+ import { t as IdentityPort } from "../identity-B6nhlSOT.cjs";
4
4
  import { t as Clock } from "../clock-DiSx-WKM.cjs";
5
- import { o as RevocationCheck } from "../tokens-BMWCnQEF.cjs";
5
+ import { RevocationCheck } from "./tokens.cjs";
6
6
  import { RoomNoticeVerdictReason } from "./room.cjs";
7
7
  //#region src/domain/notice-board.d.ts
8
8
  /**
@@ -1,8 +1,8 @@
1
- import { D as DeviceId, Ot as RoomNotice, T as DataHaveFrame, kt as RoomNoticeClaims, p as CapabilityToken } from "../protocol-BgR1e_NI.mjs";
1
+ import { D as DeviceId, Ot as RoomNotice, T as DataHaveFrame, kt as RoomNoticeClaims, p as CapabilityToken } from "../protocol-DzmpxxUr.mjs";
2
2
  import { t as KeyValueStorage } from "../storage-B403CRyu.mjs";
3
- import { t as IdentityPort } from "../identity-ViYdZrIU.mjs";
3
+ import { t as IdentityPort } from "../identity-EV-HHv_D.mjs";
4
4
  import { t as Clock } from "../clock-DiSx-WKM.mjs";
5
- import { o as RevocationCheck } from "../tokens-DRkm_QOR.mjs";
5
+ import { RevocationCheck } from "./tokens.mjs";
6
6
  import { RoomNoticeVerdictReason } from "./room.mjs";
7
7
  //#region src/domain/notice-board.d.ts
8
8
  /**
@@ -1,4 +1,4 @@
1
- import { D as DeviceId, H as ManageCommandParams, Q as PathTrace, V as ManageCommand, f as CapabilityScope, p as CapabilityToken } from "../protocol-BgR1e_NI.cjs";
1
+ import { D as DeviceId, H as ManageCommandParams, Q as PathTrace, V as ManageCommand, f as CapabilityScope, p as CapabilityToken } from "../protocol-DzmpxxUr.cjs";
2
2
  import { t as Clock } from "../clock-DiSx-WKM.cjs";
3
3
  import { IncomingManageRequest, ManageOutcome, MeshSession } from "./mesh-session.cjs";
4
4
  //#region src/domain/path-trace.d.ts
@@ -1,4 +1,4 @@
1
- import { D as DeviceId, H as ManageCommandParams, Q as PathTrace, V as ManageCommand, f as CapabilityScope, p as CapabilityToken } from "../protocol-BgR1e_NI.mjs";
1
+ import { D as DeviceId, H as ManageCommandParams, Q as PathTrace, V as ManageCommand, f as CapabilityScope, p as CapabilityToken } from "../protocol-DzmpxxUr.mjs";
2
2
  import { t as Clock } from "../clock-DiSx-WKM.mjs";
3
3
  import { IncomingManageRequest, ManageOutcome, MeshSession } from "./mesh-session.mjs";
4
4
  //#region src/domain/path-trace.d.ts
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_token_scope = require("../token-scope-CxHXTT3u.cjs");
2
+ const require_domain_token_scope = require("./token-scope.cjs");
3
3
  let cbor2 = require("cbor2");
4
4
  //#region src/domain/peer-advert.ts
5
5
  /** peer-advert's own signature field, the one entry the signature does not cover, named once here so the signing input and the reserved-extension-key guard cannot drift apart. */
@@ -46,7 +46,7 @@ async function verifyPeerAdvert(identity, advert) {
46
46
  const key = advert[PEER_ADVERT_IDENTITY_KEY_KEY];
47
47
  try {
48
48
  const derived = await identity.deriveDeviceId(key["public-key"]);
49
- if (!require_token_scope.bytesEqual(derived, advert.device)) return false;
49
+ if (!require_domain_token_scope.bytesEqual(derived, advert.device)) return false;
50
50
  return await identity.verify(key, peerAdvertSigningInput(advert), advert[PEER_ADVERT_SIGNATURE_KEY]);
51
51
  } catch {
52
52
  return false;
@@ -1,5 +1,5 @@
1
- import { B as IdentityKey, D as DeviceId, et as PeerAdvert } from "../protocol-BgR1e_NI.cjs";
2
- import { t as IdentityPort } from "../identity-DJbr96J3.cjs";
1
+ import { B as IdentityKey, D as DeviceId, et as PeerAdvert } from "../protocol-DzmpxxUr.cjs";
2
+ import { t as IdentityPort } from "../identity-B6nhlSOT.cjs";
3
3
  //#region src/domain/peer-advert.d.ts
4
4
  /** peer-advert's own signature field, the one entry the signature does not cover, named once here so the signing input and the reserved-extension-key guard cannot drift apart. */
5
5
  export declare const PEER_ADVERT_SIGNATURE_KEY = "signature";
@@ -1,5 +1,5 @@
1
- import { B as IdentityKey, D as DeviceId, et as PeerAdvert } from "../protocol-BgR1e_NI.mjs";
2
- import { t as IdentityPort } from "../identity-ViYdZrIU.mjs";
1
+ import { B as IdentityKey, D as DeviceId, et as PeerAdvert } from "../protocol-DzmpxxUr.mjs";
2
+ import { t as IdentityPort } from "../identity-EV-HHv_D.mjs";
3
3
  //#region src/domain/peer-advert.d.ts
4
4
  /** peer-advert's own signature field, the one entry the signature does not cover, named once here so the signing input and the reserved-extension-key guard cannot drift apart. */
5
5
  export declare const PEER_ADVERT_SIGNATURE_KEY = "signature";
@@ -1,4 +1,4 @@
1
- import { t as bytesEqual } from "../token-scope-Z4bmci4M.mjs";
1
+ import { bytesEqual } from "./token-scope.mjs";
2
2
  import { cdeEncodeOptions, encode } from "cbor2";
3
3
  //#region src/domain/peer-advert.ts
4
4
  /** peer-advert's own signature field, the one entry the signature does not cover, named once here so the signing input and the reserved-extension-key guard cannot drift apart. */
@@ -1,4 +1,4 @@
1
- import { et as PeerAdvert } from "../protocol-BgR1e_NI.cjs";
1
+ import { et as PeerAdvert } from "../protocol-DzmpxxUr.cjs";
2
2
  //#region src/domain/relay-advert.d.ts
3
3
  /** The domain-qualified gossip extension key a node's relay offer rides under, the same `wire-mesh` domain the core's own gossiped version fact occupies. */
4
4
  export declare const RELAY_OFFER_GOSSIP_KEY = "wire-mesh/relay-offer";
@@ -1,4 +1,4 @@
1
- import { et as PeerAdvert } from "../protocol-BgR1e_NI.mjs";
1
+ import { et as PeerAdvert } from "../protocol-DzmpxxUr.mjs";
2
2
  //#region src/domain/relay-advert.d.ts
3
3
  /** The domain-qualified gossip extension key a node's relay offer rides under, the same `wire-mesh` domain the core's own gossiped version fact occupies. */
4
4
  export declare const RELAY_OFFER_GOSSIP_KEY = "wire-mesh/relay-offer";
@@ -28,6 +28,7 @@ function supersedes(registered, advert, connection) {
28
28
  }
29
29
  function createRelayHub(options) {
30
30
  const connections = /* @__PURE__ */ new Set();
31
+ let uplink;
31
32
  const devices = /* @__PURE__ */ new Map();
32
33
  const connectionDevice = /* @__PURE__ */ new Map();
33
34
  const pairings = /* @__PURE__ */ new Map();
@@ -51,14 +52,16 @@ function createRelayHub(options) {
51
52
  function forgetConnection(connection) {
52
53
  connections.delete(connection);
53
54
  for (const [key, registration] of devices) if (registration.connection === connection) devices.delete(key);
54
- const ownDevice = connectionDevice.get(connection);
55
55
  connectionDevice.delete(connection);
56
56
  mostRecentPairing.delete(connection);
57
+ if (connection === uplink) uplink = void 0;
57
58
  const own = pairings.get(connection);
58
59
  if (own) {
59
60
  for (const peer of own.values()) {
60
61
  const peerOwn = pairings.get(peer);
61
- if (peerOwn && ownDevice !== void 0) peerOwn.delete(deviceKey(ownDevice));
62
+ if (peerOwn) {
63
+ for (const [key, paired] of peerOwn) if (paired === connection) peerOwn.delete(key);
64
+ }
62
65
  if (mostRecentPairing.get(peer) === connection) mostRecentPairing.delete(peer);
63
66
  stateChanged(peer);
64
67
  }
@@ -98,11 +101,14 @@ function createRelayHub(options) {
98
101
  advert
99
102
  });
100
103
  }
104
+ for (const { connection, state } of entries) for (const advert of state.adverts) {
105
+ const key = deviceKey(advert.device);
106
+ if (!byDevice.has(key)) byDevice.set(key, connection);
107
+ }
101
108
  for (const { connection, state } of entries) for (const paired of state.pairedDevices) {
102
109
  const peer = byDevice.get(deviceKey(paired));
103
110
  if (peer === void 0) continue;
104
111
  pairingsOf(connection).set(deviceKey(paired), peer);
105
- pairingsOf(peer).set(deviceKey(state.device), connection);
106
112
  }
107
113
  for (const { connection, state } of entries) {
108
114
  const recent = state.mostRecentDevice;
@@ -111,6 +117,27 @@ function createRelayHub(options) {
111
117
  if (peer !== void 0) mostRecentPairing.set(connection, peer);
112
118
  }
113
119
  }
120
+ /** The device a frame from `connection` acts for: the one it names when it fronts that device, otherwise the device the hub attributes to the connection when none is named. Undefined when the connection has no device yet or names one it does not front. */
121
+ function initiatorOf(connection, claimed) {
122
+ if (claimed === void 0) return connectionDevice.get(connection);
123
+ return devices.get(deviceKey(claimed))?.connection === connection ? claimed : void 0;
124
+ }
125
+ /** A relay-inbound is only meaningful from the upstream hub, which sends one to say a device it holds was paired with a device fronted through this hub. Both devices must be known here (the upstream one registered to the uplink, the addressed one to a local client) so the pairing can be recorded and the client told. */
126
+ async function handleUplinkInbound(connection, frame) {
127
+ const target = frame["target-device"];
128
+ if (connection !== uplink || target === void 0) return;
129
+ const local = devices.get(deviceKey(target));
130
+ const source = devices.get(deviceKey(frame["source-device"]));
131
+ if (local === void 0 || local.connection === connection || source?.connection !== connection) return;
132
+ addPairing(local.connection, target, connection, frame["source-device"]);
133
+ stateChanged(local.connection);
134
+ stateChanged(connection);
135
+ await local.connection.send({
136
+ type: "relay-inbound",
137
+ "source-device": frame["source-device"],
138
+ "target-device": target
139
+ });
140
+ }
114
141
  async function handleFrame(connection, frame) {
115
142
  if (frame.type === "gossip") {
116
143
  const supplanted = /* @__PURE__ */ new Set();
@@ -156,7 +183,7 @@ function createRelayHub(options) {
156
183
  }
157
184
  const catchUp = [];
158
185
  for (const registration of devices.values()) if (registration.connection !== connection) catchUp.push(registration.advert);
159
- if (catchUp.length > 0) try {
186
+ if (catchUp.length > 0 && connection !== uplink) try {
160
187
  await connection.send({
161
188
  type: "gossip",
162
189
  peers: catchUp
@@ -167,24 +194,39 @@ function createRelayHub(options) {
167
194
  if (frame.type === "relay-connect") {
168
195
  const registration = devices.get(deviceKey(frame["target-device"]));
169
196
  if (!registration || registration.connection === connection) return;
170
- const initiatorDevice = connectionDevice.get(connection);
171
- if (initiatorDevice === void 0) return;
197
+ const initiatorDevice = initiatorOf(connection, frame["source-device"]);
198
+ if (initiatorDevice === void 0 || connection === uplink) return;
172
199
  addPairing(connection, initiatorDevice, registration.connection, registration.advert.device);
173
200
  stateChanged(connection);
174
201
  stateChanged(registration.connection);
202
+ if (registration.connection === uplink) {
203
+ await registration.connection.send({
204
+ type: "relay-connect",
205
+ "target-device": registration.advert.device,
206
+ "source-device": initiatorDevice
207
+ });
208
+ return;
209
+ }
175
210
  await registration.connection.send({
176
211
  type: "relay-inbound",
177
- "source-device": initiatorDevice
212
+ "source-device": initiatorDevice,
213
+ "target-device": registration.advert.device
178
214
  });
179
215
  return;
180
216
  }
217
+ if (frame.type === "relay-inbound") {
218
+ await handleUplinkInbound(connection, frame);
219
+ return;
220
+ }
181
221
  if (frame.type === "relay-data") {
182
222
  const own = pairings.get(connection);
183
223
  const toDevice = frame["to-device"];
184
224
  const peer = toDevice !== void 0 ? own?.get(deviceKey(toDevice)) : mostRecentPairing.get(connection);
185
225
  if (!peer) return;
186
- const senderDevice = connectionDevice.get(connection);
226
+ const claimedFrom = frame["from-device"];
227
+ const senderDevice = initiatorOf(connection, claimedFrom);
187
228
  if (senderDevice === void 0) return;
229
+ if (claimedFrom !== void 0 && pairings.get(peer)?.get(deviceKey(claimedFrom)) !== connection) return;
188
230
  await peer.send({
189
231
  type: "relay-data",
190
232
  payload: frame.payload,
@@ -213,14 +255,28 @@ function createRelayHub(options) {
213
255
  return;
214
256
  }
215
257
  }
258
+ async function serve(connection) {
259
+ connections.add(connection);
260
+ try {
261
+ for await (const frame of connection.receive()) await handleFrame(connection, frame);
262
+ } catch {} finally {
263
+ forgetConnection(connection);
264
+ }
265
+ }
216
266
  return {
217
- async handleConnection(connection) {
218
- connections.add(connection);
219
- try {
220
- for await (const frame of connection.receive()) await handleFrame(connection, frame);
221
- } catch {} finally {
222
- forgetConnection(connection);
223
- }
267
+ handleConnection: serve,
268
+ async handleUplink(connection) {
269
+ if (uplink !== void 0) throw new Error("this relay hub already has an uplink");
270
+ uplink = connection;
271
+ const local = [];
272
+ for (const registration of devices.values()) local.push(registration.advert);
273
+ if (local.length > 0) try {
274
+ await connection.send({
275
+ type: "gossip",
276
+ peers: local
277
+ });
278
+ } catch {}
279
+ await serve(connection);
224
280
  },
225
281
  registerConnection(connection) {
226
282
  connections.add(connection);
@@ -235,6 +291,7 @@ function createRelayHub(options) {
235
291
  connectionDevice.clear();
236
292
  pairings.clear();
237
293
  mostRecentPairing.clear();
294
+ uplink = void 0;
238
295
  }
239
296
  };
240
297
  }
@@ -1,5 +1,5 @@
1
- import { D as DeviceId, N as Frame, et as PeerAdvert } from "../protocol-BgR1e_NI.cjs";
2
- import { t as Connection } from "../transport-DP3hdehk.cjs";
1
+ import { D as DeviceId, N as Frame, et as PeerAdvert } from "../protocol-DzmpxxUr.cjs";
2
+ import { t as Connection } from "../transport-gDbSHO-r.cjs";
3
3
  import { AdvertExtensionPolicy } from "./advert-extension-policy.cjs";
4
4
  import { Mailbox, MailboxRefusalReason } from "./hub-mailbox.cjs";
5
5
  import { PeerAdvertVerifier } from "./peer-advert.cjs";
@@ -62,6 +62,10 @@ export interface RelayHub {
62
62
  exportConnection: (connection: Readonly<Connection>) => RelayConnectionState | undefined;
63
63
  /** Rebuilds device-registry and pairing state for connections that outlived the hub instance which established it, so a frame arriving on one of them routes as it did before rather than being dropped as unknown. Each entry's connection is registered as if registerConnection had been called for it. Pairings resolve within the batch: a paired device-id no entry claims belonged to a peer that disconnected while no instance was running, and its pairing is correctly left out. Meant for a freshly created hub, before it handles any frame. */
64
64
  restoreConnections: (entries: readonly RelayConnectionRestore[]) => void;
65
+ /**
66
+ * Drives a connection to an upstream hub as this hub's uplink (wire-mesh#311), so the clients registered here are reachable through the upstream hub and can reach the devices registered there, each end keeping its own device-id. Behaves as handleConnection for that connection, and in addition announces every advert already registered here to the upstream hub before its first frame is read, since adverts that arrived earlier would otherwise never be forwarded to it. Resolves when the connection's frame stream ends, after which a new uplink may be attached. Rejects at once when an uplink is already attached.
67
+ */
68
+ handleUplink: (connection: Readonly<Connection>) => Promise<void>;
65
69
  /** Drops all registry and pairing state -- used by tests and by transport teardown. */
66
70
  stop: () => void;
67
71
  }
@@ -1,5 +1,5 @@
1
- import { D as DeviceId, N as Frame, et as PeerAdvert } from "../protocol-BgR1e_NI.mjs";
2
- import { t as Connection } from "../transport-Dv_LTCsF.mjs";
1
+ import { D as DeviceId, N as Frame, et as PeerAdvert } from "../protocol-DzmpxxUr.mjs";
2
+ import { t as Connection } from "../transport-bD3p_TO_.mjs";
3
3
  import { AdvertExtensionPolicy } from "./advert-extension-policy.mjs";
4
4
  import { Mailbox, MailboxRefusalReason } from "./hub-mailbox.mjs";
5
5
  import { PeerAdvertVerifier } from "./peer-advert.mjs";
@@ -62,6 +62,10 @@ export interface RelayHub {
62
62
  exportConnection: (connection: Readonly<Connection>) => RelayConnectionState | undefined;
63
63
  /** Rebuilds device-registry and pairing state for connections that outlived the hub instance which established it, so a frame arriving on one of them routes as it did before rather than being dropped as unknown. Each entry's connection is registered as if registerConnection had been called for it. Pairings resolve within the batch: a paired device-id no entry claims belonged to a peer that disconnected while no instance was running, and its pairing is correctly left out. Meant for a freshly created hub, before it handles any frame. */
64
64
  restoreConnections: (entries: readonly RelayConnectionRestore[]) => void;
65
+ /**
66
+ * Drives a connection to an upstream hub as this hub's uplink (wire-mesh#311), so the clients registered here are reachable through the upstream hub and can reach the devices registered there, each end keeping its own device-id. Behaves as handleConnection for that connection, and in addition announces every advert already registered here to the upstream hub before its first frame is read, since adverts that arrived earlier would otherwise never be forwarded to it. Resolves when the connection's frame stream ends, after which a new uplink may be attached. Rejects at once when an uplink is already attached.
67
+ */
68
+ handleUplink: (connection: Readonly<Connection>) => Promise<void>;
65
69
  /** Drops all registry and pairing state -- used by tests and by transport teardown. */
66
70
  stop: () => void;
67
71
  }
@@ -27,6 +27,7 @@ function supersedes(registered, advert, connection) {
27
27
  }
28
28
  function createRelayHub(options) {
29
29
  const connections = /* @__PURE__ */ new Set();
30
+ let uplink;
30
31
  const devices = /* @__PURE__ */ new Map();
31
32
  const connectionDevice = /* @__PURE__ */ new Map();
32
33
  const pairings = /* @__PURE__ */ new Map();
@@ -50,14 +51,16 @@ function createRelayHub(options) {
50
51
  function forgetConnection(connection) {
51
52
  connections.delete(connection);
52
53
  for (const [key, registration] of devices) if (registration.connection === connection) devices.delete(key);
53
- const ownDevice = connectionDevice.get(connection);
54
54
  connectionDevice.delete(connection);
55
55
  mostRecentPairing.delete(connection);
56
+ if (connection === uplink) uplink = void 0;
56
57
  const own = pairings.get(connection);
57
58
  if (own) {
58
59
  for (const peer of own.values()) {
59
60
  const peerOwn = pairings.get(peer);
60
- if (peerOwn && ownDevice !== void 0) peerOwn.delete(deviceKey(ownDevice));
61
+ if (peerOwn) {
62
+ for (const [key, paired] of peerOwn) if (paired === connection) peerOwn.delete(key);
63
+ }
61
64
  if (mostRecentPairing.get(peer) === connection) mostRecentPairing.delete(peer);
62
65
  stateChanged(peer);
63
66
  }
@@ -97,11 +100,14 @@ function createRelayHub(options) {
97
100
  advert
98
101
  });
99
102
  }
103
+ for (const { connection, state } of entries) for (const advert of state.adverts) {
104
+ const key = deviceKey(advert.device);
105
+ if (!byDevice.has(key)) byDevice.set(key, connection);
106
+ }
100
107
  for (const { connection, state } of entries) for (const paired of state.pairedDevices) {
101
108
  const peer = byDevice.get(deviceKey(paired));
102
109
  if (peer === void 0) continue;
103
110
  pairingsOf(connection).set(deviceKey(paired), peer);
104
- pairingsOf(peer).set(deviceKey(state.device), connection);
105
111
  }
106
112
  for (const { connection, state } of entries) {
107
113
  const recent = state.mostRecentDevice;
@@ -110,6 +116,27 @@ function createRelayHub(options) {
110
116
  if (peer !== void 0) mostRecentPairing.set(connection, peer);
111
117
  }
112
118
  }
119
+ /** The device a frame from `connection` acts for: the one it names when it fronts that device, otherwise the device the hub attributes to the connection when none is named. Undefined when the connection has no device yet or names one it does not front. */
120
+ function initiatorOf(connection, claimed) {
121
+ if (claimed === void 0) return connectionDevice.get(connection);
122
+ return devices.get(deviceKey(claimed))?.connection === connection ? claimed : void 0;
123
+ }
124
+ /** A relay-inbound is only meaningful from the upstream hub, which sends one to say a device it holds was paired with a device fronted through this hub. Both devices must be known here (the upstream one registered to the uplink, the addressed one to a local client) so the pairing can be recorded and the client told. */
125
+ async function handleUplinkInbound(connection, frame) {
126
+ const target = frame["target-device"];
127
+ if (connection !== uplink || target === void 0) return;
128
+ const local = devices.get(deviceKey(target));
129
+ const source = devices.get(deviceKey(frame["source-device"]));
130
+ if (local === void 0 || local.connection === connection || source?.connection !== connection) return;
131
+ addPairing(local.connection, target, connection, frame["source-device"]);
132
+ stateChanged(local.connection);
133
+ stateChanged(connection);
134
+ await local.connection.send({
135
+ type: "relay-inbound",
136
+ "source-device": frame["source-device"],
137
+ "target-device": target
138
+ });
139
+ }
113
140
  async function handleFrame(connection, frame) {
114
141
  if (frame.type === "gossip") {
115
142
  const supplanted = /* @__PURE__ */ new Set();
@@ -155,7 +182,7 @@ function createRelayHub(options) {
155
182
  }
156
183
  const catchUp = [];
157
184
  for (const registration of devices.values()) if (registration.connection !== connection) catchUp.push(registration.advert);
158
- if (catchUp.length > 0) try {
185
+ if (catchUp.length > 0 && connection !== uplink) try {
159
186
  await connection.send({
160
187
  type: "gossip",
161
188
  peers: catchUp
@@ -166,24 +193,39 @@ function createRelayHub(options) {
166
193
  if (frame.type === "relay-connect") {
167
194
  const registration = devices.get(deviceKey(frame["target-device"]));
168
195
  if (!registration || registration.connection === connection) return;
169
- const initiatorDevice = connectionDevice.get(connection);
170
- if (initiatorDevice === void 0) return;
196
+ const initiatorDevice = initiatorOf(connection, frame["source-device"]);
197
+ if (initiatorDevice === void 0 || connection === uplink) return;
171
198
  addPairing(connection, initiatorDevice, registration.connection, registration.advert.device);
172
199
  stateChanged(connection);
173
200
  stateChanged(registration.connection);
201
+ if (registration.connection === uplink) {
202
+ await registration.connection.send({
203
+ type: "relay-connect",
204
+ "target-device": registration.advert.device,
205
+ "source-device": initiatorDevice
206
+ });
207
+ return;
208
+ }
174
209
  await registration.connection.send({
175
210
  type: "relay-inbound",
176
- "source-device": initiatorDevice
211
+ "source-device": initiatorDevice,
212
+ "target-device": registration.advert.device
177
213
  });
178
214
  return;
179
215
  }
216
+ if (frame.type === "relay-inbound") {
217
+ await handleUplinkInbound(connection, frame);
218
+ return;
219
+ }
180
220
  if (frame.type === "relay-data") {
181
221
  const own = pairings.get(connection);
182
222
  const toDevice = frame["to-device"];
183
223
  const peer = toDevice !== void 0 ? own?.get(deviceKey(toDevice)) : mostRecentPairing.get(connection);
184
224
  if (!peer) return;
185
- const senderDevice = connectionDevice.get(connection);
225
+ const claimedFrom = frame["from-device"];
226
+ const senderDevice = initiatorOf(connection, claimedFrom);
186
227
  if (senderDevice === void 0) return;
228
+ if (claimedFrom !== void 0 && pairings.get(peer)?.get(deviceKey(claimedFrom)) !== connection) return;
187
229
  await peer.send({
188
230
  type: "relay-data",
189
231
  payload: frame.payload,
@@ -212,14 +254,28 @@ function createRelayHub(options) {
212
254
  return;
213
255
  }
214
256
  }
257
+ async function serve(connection) {
258
+ connections.add(connection);
259
+ try {
260
+ for await (const frame of connection.receive()) await handleFrame(connection, frame);
261
+ } catch {} finally {
262
+ forgetConnection(connection);
263
+ }
264
+ }
215
265
  return {
216
- async handleConnection(connection) {
217
- connections.add(connection);
218
- try {
219
- for await (const frame of connection.receive()) await handleFrame(connection, frame);
220
- } catch {} finally {
221
- forgetConnection(connection);
222
- }
266
+ handleConnection: serve,
267
+ async handleUplink(connection) {
268
+ if (uplink !== void 0) throw new Error("this relay hub already has an uplink");
269
+ uplink = connection;
270
+ const local = [];
271
+ for (const registration of devices.values()) local.push(registration.advert);
272
+ if (local.length > 0) try {
273
+ await connection.send({
274
+ type: "gossip",
275
+ peers: local
276
+ });
277
+ } catch {}
278
+ await serve(connection);
223
279
  },
224
280
  registerConnection(connection) {
225
281
  connections.add(connection);
@@ -234,6 +290,7 @@ function createRelayHub(options) {
234
290
  connectionDevice.clear();
235
291
  pairings.clear();
236
292
  mostRecentPairing.clear();
293
+ uplink = void 0;
237
294
  }
238
295
  };
239
296
  }
@@ -1,5 +1,5 @@
1
- import { N as Frame } from "../protocol-BgR1e_NI.cjs";
2
- import { t as Connection } from "../transport-DP3hdehk.cjs";
1
+ import { N as Frame } from "../protocol-DzmpxxUr.cjs";
2
+ import { t as Connection } from "../transport-gDbSHO-r.cjs";
3
3
  //#region src/domain/relay-use-gate.d.ts
4
4
  export interface RelayUseAuthorizationTracker {
5
5
  /** True once authorize() has been called for this exact connection and revoke() has not been called since. */
@@ -1,5 +1,5 @@
1
- import { N as Frame } from "../protocol-BgR1e_NI.mjs";
2
- import { t as Connection } from "../transport-Dv_LTCsF.mjs";
1
+ import { N as Frame } from "../protocol-DzmpxxUr.mjs";
2
+ import { t as Connection } from "../transport-bD3p_TO_.mjs";
3
3
  //#region src/domain/relay-use-gate.d.ts
4
4
  export interface RelayUseAuthorizationTracker {
5
5
  /** True once authorize() has been called for this exact connection and revoke() has not been called since. */
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_domain_device_id = require("./device-id.cjs");
3
- const require_tokens = require("../tokens-tPJQ7Vzt.cjs");
3
+ const require_domain_tokens = require("./tokens.cjs");
4
4
  //#region src/domain/revocation-view.ts
5
5
  function revocationKey(tokenId) {
6
6
  return require_domain_device_id.bytesToHex(tokenId);
@@ -12,7 +12,7 @@ function createRevocationView() {
12
12
  return Promise.resolve(entriesByTokenId.get(revocationKey(tokenId)) ?? []);
13
13
  },
14
14
  async record(entry, options) {
15
- const verdict = await require_tokens.verifyRevocationEntry(entry, options);
15
+ const verdict = await require_domain_tokens.verifyRevocationEntry(entry, options);
16
16
  if (verdict.ok) {
17
17
  const key = revocationKey(verdict.claims["token-id"]);
18
18
  const existing = entriesByTokenId.get(key);
@@ -1,5 +1,5 @@
1
- import { bt as RevocationEntry } from "../protocol-BgR1e_NI.cjs";
2
- import { f as VerifyRevocationEntryOptions, o as RevocationCheck, s as RevocationEntryVerdict } from "../tokens-BMWCnQEF.cjs";
1
+ import { bt as RevocationEntry } from "../protocol-DzmpxxUr.cjs";
2
+ import { RevocationCheck, RevocationEntryVerdict, VerifyRevocationEntryOptions } from "./tokens.cjs";
3
3
  //#region src/domain/revocation-view.d.ts
4
4
  /**
5
5
  * An in-memory RevocationCheck fed by ingested revocation-announce frames. Keyed by token-id alone, per management.cddl's contract: a token-id can legitimately carry multiple recorded entries from different issuers (the token's own issuer, and/or any number of parties holding a delegated manage:revoke authorization over it) -- entriesFor returns every one of them unfiltered, since checking which actually apply (issuer-match, or a verified authorization) is verifyTokenChain's own obligation, not this store's.
@@ -1,5 +1,5 @@
1
- import { bt as RevocationEntry } from "../protocol-BgR1e_NI.mjs";
2
- import { f as VerifyRevocationEntryOptions, o as RevocationCheck, s as RevocationEntryVerdict } from "../tokens-DRkm_QOR.mjs";
1
+ import { bt as RevocationEntry } from "../protocol-DzmpxxUr.mjs";
2
+ import { RevocationCheck, RevocationEntryVerdict, VerifyRevocationEntryOptions } from "./tokens.mjs";
3
3
  //#region src/domain/revocation-view.d.ts
4
4
  /**
5
5
  * An in-memory RevocationCheck fed by ingested revocation-announce frames. Keyed by token-id alone, per management.cddl's contract: a token-id can legitimately carry multiple recorded entries from different issuers (the token's own issuer, and/or any number of parties holding a delegated manage:revoke authorization over it) -- entriesFor returns every one of them unfiltered, since checking which actually apply (issuer-match, or a verified authorization) is verifyTokenChain's own obligation, not this store's.
@@ -1,5 +1,5 @@
1
1
  import { bytesToHex } from "./device-id.mjs";
2
- import { c as verifyRevocationEntry } from "../tokens-D8JlTJNA.mjs";
2
+ import { verifyRevocationEntry } from "./tokens.mjs";
3
3
  //#region src/domain/revocation-view.ts
4
4
  function revocationKey(tokenId) {
5
5
  return bytesToHex(tokenId);
@@ -1,8 +1,8 @@
1
- import { D as DeviceId, V as ManageCommand, p as CapabilityToken } from "../protocol-BgR1e_NI.cjs";
2
- import { t as IdentityPort } from "../identity-DJbr96J3.cjs";
1
+ import { D as DeviceId, V as ManageCommand, p as CapabilityToken } from "../protocol-DzmpxxUr.cjs";
2
+ import { t as IdentityPort } from "../identity-B6nhlSOT.cjs";
3
3
  import { t as Clock } from "../clock-DiSx-WKM.cjs";
4
4
  import { IncomingManageRequest, ManageOutcome, MeshSession } from "./mesh-session.cjs";
5
- import { o as RevocationCheck } from "../tokens-BMWCnQEF.cjs";
5
+ import { RevocationCheck } from "./tokens.cjs";
6
6
  //#region src/domain/room-rekey.d.ts
7
7
  /**
8
8
  * Builds a room.rekey command per room.cddl. The outer verb is room:member, the same "one resource, several inner verbs" pattern room.send/room.leave/room.members already establish -- not a new capability of its own.
@@ -1,8 +1,8 @@
1
- import { D as DeviceId, V as ManageCommand, p as CapabilityToken } from "../protocol-BgR1e_NI.mjs";
2
- import { t as IdentityPort } from "../identity-ViYdZrIU.mjs";
1
+ import { D as DeviceId, V as ManageCommand, p as CapabilityToken } from "../protocol-DzmpxxUr.mjs";
2
+ import { t as IdentityPort } from "../identity-EV-HHv_D.mjs";
3
3
  import { t as Clock } from "../clock-DiSx-WKM.mjs";
4
4
  import { IncomingManageRequest, ManageOutcome, MeshSession } from "./mesh-session.mjs";
5
- import { o as RevocationCheck } from "../tokens-DRkm_QOR.mjs";
5
+ import { RevocationCheck } from "./tokens.mjs";
6
6
  //#region src/domain/room-rekey.d.ts
7
7
  /**
8
8
  * Builds a room.rekey command per room.cddl. The outer verb is room:member, the same "one resource, several inner verbs" pattern room.send/room.leave/room.members already establish -- not a new capability of its own.
@@ -1,6 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_domain_device_id = require("./device-id.cjs");
3
- const require_tokens = require("../tokens-tPJQ7Vzt.cjs");
3
+ const require_domain_tokens = require("./tokens.cjs");
4
4
  const require_domain_room_path = require("./room-path.cjs");
5
5
  //#region src/domain/room-token-verification.ts
6
6
  /**
@@ -12,7 +12,7 @@ const ROOM_MEMBER_CAPABILITY = "room:member";
12
12
  * Verifies a `room:member` capability token against all six of core/room's verifier obligations. Delegates obligations 2/4/5 to verifyCapabilityToken directly; adds obligation 3 (scope.kind/scope.path must match the room being acted on) and obligation 1 (the chain's root must be the room's own owner for a named room, or the verifying identity itself for a DM -- never either named participant directly, since a DM token minted by anyone other than the verifier would let a sender self-issue authority to message a stranger unsolicited).
13
13
  */
14
14
  async function verifyRoomToken(token, options) {
15
- const verdict = await require_tokens.verifyCapabilityToken(token, {
15
+ const verdict = await require_domain_tokens.verifyCapabilityToken(token, {
16
16
  identity: options.identity,
17
17
  clock: options.clock,
18
18
  revocation: options.revocation,
@@ -1,2 +1,2 @@
1
- import { a as verifyRoomToken, i as VerifyRoomTokenOptions, n as RoomTokenVerdict, r as RoomTokenVerdictReason, t as ROOM_MEMBER_CAPABILITY } from "../room-token-verification-hFtBDZPO.cjs";
1
+ import { a as verifyRoomToken, i as VerifyRoomTokenOptions, n as RoomTokenVerdict, r as RoomTokenVerdictReason, t as ROOM_MEMBER_CAPABILITY } from "../room-token-verification-Dh7SGJfK.cjs";
2
2
  export { ROOM_MEMBER_CAPABILITY, RoomTokenVerdict, RoomTokenVerdictReason, VerifyRoomTokenOptions, verifyRoomToken };
@@ -1,2 +1,2 @@
1
- import { a as verifyRoomToken, i as VerifyRoomTokenOptions, n as RoomTokenVerdict, r as RoomTokenVerdictReason, t as ROOM_MEMBER_CAPABILITY } from "../room-token-verification-7zD4ib-A.mjs";
1
+ import { a as verifyRoomToken, i as VerifyRoomTokenOptions, n as RoomTokenVerdict, r as RoomTokenVerdictReason, t as ROOM_MEMBER_CAPABILITY } from "../room-token-verification-Hb1LkpFh.mjs";
2
2
  export { ROOM_MEMBER_CAPABILITY, RoomTokenVerdict, RoomTokenVerdictReason, VerifyRoomTokenOptions, verifyRoomToken };
@@ -1,5 +1,5 @@
1
1
  import { deviceIdToHex } from "./device-id.mjs";
2
- import { s as verifyCapabilityToken } from "../tokens-D8JlTJNA.mjs";
2
+ import { verifyCapabilityToken } from "./tokens.mjs";
3
3
  import { parseRoomPath } from "./room-path.mjs";
4
4
  //#region src/domain/room-token-verification.ts
5
5
  /**
@@ -1,8 +1,8 @@
1
- import { At as RoomPath, Ot as RoomNotice, kt as RoomNoticeClaims, p as CapabilityToken, q as MessageRef } from "../protocol-BgR1e_NI.cjs";
2
- import { t as IdentityPort } from "../identity-DJbr96J3.cjs";
1
+ import { At as RoomPath, Ot as RoomNotice, kt as RoomNoticeClaims, p as CapabilityToken, q as MessageRef } from "../protocol-DzmpxxUr.cjs";
2
+ import { t as IdentityPort } from "../identity-B6nhlSOT.cjs";
3
3
  import { t as Clock } from "../clock-DiSx-WKM.cjs";
4
- import { o as RevocationCheck } from "../tokens-BMWCnQEF.cjs";
5
- import { r as RoomTokenVerdictReason } from "../room-token-verification-hFtBDZPO.cjs";
4
+ import { RevocationCheck } from "./tokens.cjs";
5
+ import { r as RoomTokenVerdictReason } from "../room-token-verification-Dh7SGJfK.cjs";
6
6
  //#region src/domain/room.d.ts
7
7
  export type RoomNoticeVerdictReason = "malformed" | "wrong_room" | "bad_signature" | "wrong_poster" | "content_expired" | "key_epoch_mismatch" | RoomTokenVerdictReason;
8
8
  export type RoomNoticeVerdict = {