@codemem/core 0.49.0-beta.5 → 0.49.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 (37) hide show
  1. package/dist/{coordinator-runtime-BXJQAKFD.js → coordinator-runtime-BYCjEE7A.js} +2 -2
  2. package/dist/{coordinator-runtime-BXJQAKFD.js.map → coordinator-runtime-BYCjEE7A.js.map} +1 -1
  3. package/dist/index.d.ts +1 -1
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +76 -49
  6. package/dist/index.js.map +1 -1
  7. package/dist/internal/cloudflare-coordinator.js +1 -1
  8. package/dist/internal/relay-host.d.ts +1 -0
  9. package/dist/internal/relay-host.d.ts.map +1 -1
  10. package/dist/internal/relay-host.js +1 -1
  11. package/dist/internal/relay-runtime.js +4 -4
  12. package/dist/{relay-health-zrKtNQLQ.js → relay-health-CeyhVeuq.js} +2 -2
  13. package/dist/{relay-health-zrKtNQLQ.js.map → relay-health-CeyhVeuq.js.map} +1 -1
  14. package/dist/relay-health.d.ts.map +1 -1
  15. package/dist/relay-noise-stream.d.ts +7 -0
  16. package/dist/relay-noise-stream.d.ts.map +1 -1
  17. package/dist/{relay-peer-client-DrTb2oZt.js → relay-peer-client-j0rOyeuZ.js} +2537 -2512
  18. package/dist/{relay-peer-client-DrTb2oZt.js.map → relay-peer-client-j0rOyeuZ.js.map} +1 -1
  19. package/dist/{relay-server-CLNJrfOV.js → relay-server-TOaojlAm.js} +276 -120
  20. package/dist/relay-server-TOaojlAm.js.map +1 -0
  21. package/dist/relay-server-diagnostic.d.ts +108 -0
  22. package/dist/relay-server-diagnostic.d.ts.map +1 -0
  23. package/dist/relay-server-test-fixtures.d.ts +2 -2
  24. package/dist/relay-server-test-fixtures.d.ts.map +1 -1
  25. package/dist/relay-server.d.ts +7 -2
  26. package/dist/relay-server.d.ts.map +1 -1
  27. package/dist/scope-membership-cache.d.ts +1 -0
  28. package/dist/scope-membership-cache.d.ts.map +1 -1
  29. package/dist/store.d.ts +1 -0
  30. package/dist/store.d.ts.map +1 -1
  31. package/dist/sync-daemon.d.ts.map +1 -1
  32. package/dist/sync-reachability-pass-5bOMxdoW.js +2 -0
  33. package/dist/{sync-reachability-pass-CUe5bK61.js → sync-reachability-pass-Co6WjaUD.js} +2 -2
  34. package/dist/{sync-reachability-pass-CUe5bK61.js.map → sync-reachability-pass-Co6WjaUD.js.map} +1 -1
  35. package/package.json +3 -3
  36. package/dist/relay-server-CLNJrfOV.js.map +0 -1
  37. package/dist/sync-reachability-pass-D9cA54fj.js +0 -2
@@ -1,12 +1,12 @@
1
- import { d as e, f as t, h as n, l as r, m as i, n as a, p as o, r as s, s as c, u as l } from "./relay-admission-reply-CHCEakzd.js";
2
- import { t as u } from "./relay-data-frame-CkrBuBf6.js";
1
+ import { d as e, f as t, h as n, l as r, m as i, n as a, o, p as s, r as c, s as l, u } from "./relay-admission-reply-CHCEakzd.js";
2
+ import { t as d } from "./relay-data-frame-CkrBuBf6.js";
3
3
  //#region src/relay-coordinator-client.ts
4
- function d(e) {
4
+ function f(e) {
5
5
  let t = new URL(e.url), n = ["127.0.0.1", "[::1]"].includes(t.hostname);
6
6
  if (t.protocol !== "https:" && !(t.protocol === "http:" && n && e.allowLoopbackHttp) || t.username || t.password || t.search || t.hash || !t.pathname.endsWith("/v1/relay/admission-check")) throw Error("Invalid relay coordinator endpoint");
7
7
  return t;
8
8
  }
9
- async function f(e) {
9
+ async function p(e) {
10
10
  let t = e.body?.getReader();
11
11
  if (!t) return null;
12
12
  let n = /* @__PURE__ */ new Uint8Array(1024), r = 0;
@@ -21,8 +21,8 @@ async function f(e) {
21
21
  t.cancel().catch(() => {}), t.releaseLock();
22
22
  }
23
23
  }
24
- function p(e) {
25
- let t = d(e).href, r = e.fetcher, i = r ? (e) => r.fetch(t, e) : (e) => fetch(t, e), a = r ? "manual" : "error";
24
+ function m(e) {
25
+ let t = f(e).href, r = e.fetcher, i = r ? (e) => r.fetch(t, e) : (e) => fetch(t, e), a = r ? "manual" : "error";
26
26
  return { async check({ packet: e, deviceHeader: t, signal: r }) {
27
27
  try {
28
28
  if (r.aborted) return null;
@@ -41,23 +41,23 @@ function p(e) {
41
41
  body: new Uint8Array(o)
42
42
  });
43
43
  if (s.status !== 200) return s.body?.cancel().catch(() => {}), null;
44
- let c = await f(s);
44
+ let c = await p(s);
45
45
  return r.aborted ? null : c;
46
46
  } catch {
47
47
  return null;
48
48
  }
49
49
  } };
50
50
  }
51
- var m = 6e3, h = 2;
52
- function g(e) {
51
+ var h = 6e3, g = 2;
52
+ function _(e) {
53
53
  let t = e.trim().toLowerCase(), n = t.indexOf("%"), r = n === -1 ? t : t.slice(0, n);
54
54
  if (!r.includes(":")) return r;
55
55
  let i = /^(?:(?:0{1,4}:){5}|::)ffff:(\d{1,3}(?:\.\d{1,3}){3})$/.exec(r);
56
56
  if (i?.[1]) return i[1];
57
- let a = _(r);
57
+ let a = v(r);
58
58
  return a ? `${a.slice(0, 4).join(":")}::/64` : r;
59
59
  }
60
- function _(e) {
60
+ function v(e) {
61
61
  if (e.includes(".")) return null;
62
62
  let t = e.split("::");
63
63
  if (t.length > 2) return null;
@@ -70,7 +70,7 @@ function _(e) {
70
70
  ];
71
71
  return a.every((e) => /^[0-9a-f]{1,4}$/.test(e)) ? a.map((e) => e.replace(/^0+(?=.)/, "")) : null;
72
72
  }
73
- var v = class {
73
+ var y = class {
74
74
  now;
75
75
  allowances = /* @__PURE__ */ new Map();
76
76
  constructor(e = () => performance.now()) {
@@ -87,9 +87,71 @@ var v = class {
87
87
  updated: t
88
88
  }, this.allowances.set(e, n);
89
89
  }
90
- return n.tokens = Math.min(2, n.tokens + (t - n.updated) / m), n.updated = t, n.tokens < 1 ? !1 : (--n.tokens, !0);
91
- }
92
- }, y = 1e4, b = 72e4, x = [
90
+ return n.tokens = Math.min(2, n.tokens + (t - n.updated) / h), n.updated = t, n.tokens < 1 ? !1 : (--n.tokens, !0);
91
+ }
92
+ }, b = {
93
+ restore_known_socket: "registration_replaced",
94
+ restore_device_conflict: "registration_replaced",
95
+ binary_exception: "protocol",
96
+ physical_close: "socket_teardown",
97
+ engine_shutdown: "socket_teardown",
98
+ setup_expired: "socket_teardown",
99
+ challenge_expired: "socket_teardown",
100
+ control_buffer_preflight: "buffer_limit",
101
+ control_write_callback: "send_failed",
102
+ control_buffer_after_send: "buffer_limit",
103
+ control_send_exception: "send_failed",
104
+ pending_control: "protocol",
105
+ control_queue_limit: "buffer_limit",
106
+ text_exception: "protocol",
107
+ control_quota: "quota_unavailable",
108
+ control_exception: "protocol",
109
+ unexpected_control: "protocol",
110
+ service_context: "protocol",
111
+ service_proof: "socket_teardown",
112
+ registration_context: "protocol",
113
+ registration_denied: "socket_teardown",
114
+ registration_key_replaced: "registration_replaced",
115
+ registration_conflict: "registration_replaced",
116
+ registration_buffer: "buffer_limit",
117
+ registration_snapshot: "socket_teardown",
118
+ registration_replaced: "registration_replaced",
119
+ open_control_quota: "quota_unavailable",
120
+ route_expired: "route_expired",
121
+ route_control_nonparticipant: "protocol",
122
+ peer_abort: "peer_abort",
123
+ end_before_accepted: "protocol",
124
+ duplicate_end: "protocol",
125
+ invalid_route_control: "protocol",
126
+ data_pending_socket: "protocol",
127
+ data_nonparticipant: "protocol",
128
+ data_route_limits: "route_limit",
129
+ data_buffer_limit: "buffer_limit",
130
+ orphan_outside_restore: "protocol",
131
+ orphan_limit: "protocol",
132
+ orphan_exception: "protocol",
133
+ orphan_control_quota: "quota_unavailable",
134
+ restored_route_lost: "restored_route_lost",
135
+ traffic_reservation_exception: "quota_unavailable",
136
+ traffic_quota: "quota_unavailable",
137
+ forward_recipient_or_buffer: "recipient_unavailable",
138
+ forward_buffer_preflight: "buffer_limit",
139
+ data_write_callback: "send_failed",
140
+ forward_buffer_after_send: "buffer_limit",
141
+ data_send_exception: "send_failed",
142
+ input_pressure_exception: "socket_teardown",
143
+ worker_operator_disable: "socket_teardown",
144
+ worker_config_missing_fetch: "socket_teardown",
145
+ worker_config_missing_event: "socket_teardown",
146
+ worker_peer_close: "socket_teardown",
147
+ worker_peer_error: "socket_teardown",
148
+ worker_accept_failed: "socket_teardown",
149
+ worker_host_missing: "socket_teardown",
150
+ worker_restore_failed: "socket_teardown",
151
+ worker_control_oversize: "protocol",
152
+ worker_data_oversize: "protocol",
153
+ worker_close_grace: "socket_teardown"
154
+ }, x = 1e4, S = 72e4, C = [
93
155
  "v",
94
156
  "realmId",
95
157
  "deviceId",
@@ -98,11 +160,11 @@ var v = class {
98
160
  "renewNotBeforeUtcMs",
99
161
  "restoreNotAfterUtcMs"
100
162
  ];
101
- function S(e) {
163
+ function w(e) {
102
164
  return Number.isFinite(e) && e >= 0 ? e : null;
103
165
  }
104
- function C(e) {
105
- let t = S(e.utcMs);
166
+ function T(e) {
167
+ let t = w(e.utcMs);
106
168
  if (t === null || e.expiresMs - e.nowMs <= 1e4) return null;
107
169
  let n = Math.ceil(t - (e.nowMs - e.startedMs)), r = {
108
170
  v: 1,
@@ -110,19 +172,19 @@ function C(e) {
110
172
  deviceId: e.deviceId,
111
173
  keyId: e.keyId,
112
174
  acceptedAtUtcMs: n,
113
- renewNotBeforeUtcMs: n + b,
114
- restoreNotAfterUtcMs: Math.floor(t + (e.expiresMs - e.nowMs) - y)
175
+ renewNotBeforeUtcMs: n + S,
176
+ restoreNotAfterUtcMs: Math.floor(t + (e.expiresMs - e.nowMs) - x)
115
177
  };
116
- return w(r, e.realmId) ? r : null;
178
+ return E(r, e.realmId) ? r : null;
117
179
  }
118
- function w(n, r) {
119
- if (!t(n, x)) return !1;
120
- let a = n.acceptedAtUtcMs, s = n.restoreNotAfterUtcMs;
121
- return n.v === 1 && n.realmId === r && e(n.deviceId) && o(n.keyId) && i(a) && i(s) && n.renewNotBeforeUtcMs === a + 72e4 && s > a && s - a <= 89e4;
180
+ function E(n, r) {
181
+ if (!t(n, C)) return !1;
182
+ let a = n.acceptedAtUtcMs, o = n.restoreNotAfterUtcMs;
183
+ return n.v === 1 && n.realmId === r && e(n.deviceId) && s(n.keyId) && i(a) && i(o) && n.renewNotBeforeUtcMs === a + 72e4 && o > a && o - a <= 89e4;
122
184
  }
123
- function T(e, t) {
124
- let n = S(t.utcMs);
125
- if (n === null || !w(e, t.realmId) || e.acceptedAtUtcMs > n + 1e4) return null;
185
+ function D(e, t) {
186
+ let n = w(t.utcMs);
187
+ if (n === null || !E(e, t.realmId) || e.acceptedAtUtcMs > n + 1e4) return null;
126
188
  let r = e.restoreNotAfterUtcMs - n;
127
189
  return r <= 0 ? null : {
128
190
  deviceId: e.deviceId,
@@ -133,7 +195,7 @@ function T(e, t) {
133
195
  }
134
196
  //#endregion
135
197
  //#region src/relay-server.ts
136
- var E = {
198
+ var O = {
137
199
  sockets: 128,
138
200
  pendingSockets: 16,
139
201
  routes: 32,
@@ -143,28 +205,35 @@ var E = {
143
205
  coordinatorTimeoutMs: 3e4,
144
206
  quotaTimeoutMs: 3e3,
145
207
  routeBytes: 20971520
146
- }, D = 262144, O = 6e4, k = 16384, A = new TextEncoder();
147
- function j(e) {
208
+ }, k = 262144, A = 6e4, j = 16384, M = new TextEncoder(), N = {
209
+ abort: "peer_abort",
210
+ end: "end_before_accepted"
211
+ };
212
+ function P(e, t) {
213
+ let n = e - t;
214
+ return Number.isFinite(n) ? Math.max(0, n) : null;
215
+ }
216
+ function F(e) {
148
217
  return globalThis.crypto.getRandomValues(new Uint8Array(e));
149
218
  }
150
- function M(e, t) {
219
+ function I(e, t) {
151
220
  let n = e(t);
152
221
  if (!(n instanceof Uint8Array) || n.length !== t) throw Error("Invalid relay random bytes");
153
222
  return Array.from(n, (e) => e.toString(16).padStart(2, "0")).join("");
154
223
  }
155
- function N(e) {
224
+ function L(e) {
156
225
  e.unref?.();
157
226
  }
158
- function P(e) {
159
- let t = new F(e);
227
+ function R(e) {
228
+ let t = new z(e);
160
229
  return {
161
230
  adopt: (e) => t.adopt(e),
162
231
  restore: (e, n) => t.restore(e, n),
163
- close: () => t.close(),
232
+ close: (e) => t.close(e),
164
233
  counters: () => t.counters()
165
234
  };
166
235
  }
167
- var F = class {
236
+ var z = class {
168
237
  options;
169
238
  connections = /* @__PURE__ */ new Set();
170
239
  registrations = /* @__PURE__ */ new Map();
@@ -175,11 +244,11 @@ var F = class {
175
244
  stopped = !1;
176
245
  trafficQuotaAborts = 0;
177
246
  constructor(e) {
178
- if (this.options = e, this.namespace = Object.freeze({ ...e.namespace }), this.realmId = c(this.namespace.authorityId, this.namespace.groupId), !o(this.namespace.relayAudienceId)) throw Error("Invalid relay namespace");
179
- this.limits = { ...E };
180
- for (let t of Object.keys(E)) {
181
- let n = e.limits?.[t] ?? (t === "routeBytes" ? E.routeBytes : 0);
182
- if (!Number.isSafeInteger(n) || n < 0 || n > E[t]) throw Error("Invalid relay limits");
247
+ if (this.options = e, this.namespace = Object.freeze({ ...e.namespace }), this.realmId = l(this.namespace.authorityId, this.namespace.groupId), !s(this.namespace.relayAudienceId)) throw Error("Invalid relay namespace");
248
+ this.limits = { ...O };
249
+ for (let t of Object.keys(O)) {
250
+ let n = e.limits?.[t] ?? (t === "routeBytes" ? O.routeBytes : 0);
251
+ if (!Number.isSafeInteger(n) || n < 0 || n > O[t]) throw Error("Invalid relay limits");
183
252
  this.limits[t] = n;
184
253
  }
185
254
  }
@@ -221,22 +290,22 @@ var F = class {
221
290
  }
222
291
  restore(e, t) {
223
292
  let n = [...this.connections].find((t) => t.socket === e);
224
- if (n) return this.disconnect(n), null;
293
+ if (n) return this.disconnect(n, "restore_known_socket"), null;
225
294
  if (!this.accepting()) return null;
226
- let r = T(t, {
295
+ let r = D(t, {
227
296
  realmId: this.realmId,
228
297
  utcMs: this.utc()
229
298
  });
230
299
  if (!r) return null;
231
300
  let i = [...this.connections].filter((e) => e.registration?.deviceId === r.deviceId);
232
301
  if (i.length > 0) {
233
- for (let e of i) this.disconnect(e);
302
+ for (let e of i) this.disconnect(e, "restore_device_conflict");
234
303
  return null;
235
304
  }
236
305
  let a = this.now(), o = Object.freeze({
237
306
  deviceId: r.deviceId,
238
307
  keyId: r.keyId,
239
- started: a + r.renewInMs - b,
308
+ started: a + r.renewInMs - S,
240
309
  expires: a + r.remainingMs
241
310
  }), s = {
242
311
  socket: e,
@@ -267,20 +336,23 @@ var F = class {
267
336
  try {
268
337
  this.binary(e, t);
269
338
  } catch {
270
- this.disconnect(e);
339
+ this.disconnect(e, "binary_exception");
271
340
  }
272
341
  },
273
- closed: () => {
274
- e.physicalClosed = !0, this.disconnect(e), this.connections.delete(e);
342
+ closing: (t) => {
343
+ this.options.onDiagnostic && (e.diagnosticSite ??= t, this.captureSocketOrigin(e, e.diagnosticSite));
344
+ },
345
+ closed: (t = "physical_close") => {
346
+ e.physicalClosed = !0, this.disconnect(e, t), this.connections.delete(e);
275
347
  }
276
348
  };
277
349
  }
278
- close() {
350
+ close(e = "engine_shutdown") {
279
351
  this.stopped = !0;
280
- for (let e of this.connections) this.disconnect(e);
352
+ for (let t of this.connections) this.disconnect(t, e);
281
353
  }
282
354
  live(e) {
283
- return e.closing || this.stopped ? !1 : e.pending && this.now() >= e.setupExpires || (e.registration && this.now() >= e.registration.expires && this.registrations.get(e.registration.deviceId) === e && this.registrations.delete(e.registration.deviceId), e.challenge && this.now() >= e.challenge.expires) ? (this.disconnect(e), !1) : !0;
355
+ return e.closing || this.stopped ? !1 : e.pending && this.now() >= e.setupExpires ? (this.disconnect(e, "setup_expired"), !1) : (e.registration && this.now() >= e.registration.expires && this.registrations.get(e.registration.deviceId) === e && this.registrations.delete(e.registration.deviceId), e.challenge && this.now() >= e.challenge.expires ? (this.disconnect(e, "challenge_expired"), !1) : !0);
284
356
  }
285
357
  registered(e) {
286
358
  if (!this.live(e)) return null;
@@ -292,34 +364,41 @@ var F = class {
292
364
  let t = [];
293
365
  e.pending && t.push(e.setupExpires), e.challenge && t.push(e.challenge.expires), !(e.closing || t.length === 0) && (e.timer = setTimeout(() => {
294
366
  this.live(e) && this.armConnection(e);
295
- }, Math.max(1, Math.ceil(Math.min(...t) - this.now()))), N(e.timer));
367
+ }, Math.max(1, Math.ceil(Math.min(...t) - this.now()))), L(e.timer));
296
368
  }
297
- disconnect(e) {
369
+ disconnect(e, t) {
298
370
  if (e.closing) return;
299
- e.closing = !0, e.cancel.abort();
371
+ e.closing = !0;
372
+ let n = e.diagnosticSite ?? t;
373
+ this.captureSocketOrigin(e, n), e.cancel.abort();
300
374
  for (let t of e.controls.splice(0)) t.resolve();
301
375
  e.timer && clearTimeout(e.timer);
302
- let t = e.registration;
303
- t && this.registrations.get(t.deviceId) === e && this.registrations.delete(t.deviceId);
304
- for (let t of this.routes.values()) (t.source === e || t.target === e) && this.removeRoute(t, "abort");
376
+ let r = e.registration;
377
+ r && this.registrations.get(r.deviceId) === e && this.registrations.delete(r.deviceId);
378
+ for (let t of this.routes.values()) (t.source === e || t.target === e) && this.removeRoute(t, "abort", n, e);
305
379
  try {
306
380
  e.paused && e.socket.resumeInput(), e.socket.close();
307
381
  } catch {}
308
382
  }
383
+ captureSocketOrigin(e, t) {
384
+ if (this.options.onDiagnostic) try {
385
+ for (let n of this.routes.values()) (n.source === e || n.target === e) && (n.terminalDiagnostic ??= this.routeDiagnostic(n, t, e));
386
+ } catch {}
387
+ }
309
388
  send(e, t) {
310
389
  if (this.live(e)) try {
311
- let n = l(t), r = A.encode(n).byteLength;
390
+ let n = u(t), r = M.encode(n).byteLength;
312
391
  if (!this.withinBuffers(e, { additionalBytes: r })) {
313
- this.disconnect(e);
392
+ this.disconnect(e, "control_buffer_preflight");
314
393
  return;
315
394
  }
316
395
  e.writes += r;
317
396
  let i = !1;
318
397
  e.socket.sendText(n, (t) => {
319
- i || (i = !0, e.writes -= r, t && this.disconnect(e), this.updatePressure());
320
- }), this.withinBuffers(e) || this.disconnect(e);
398
+ i || (i = !0, e.writes -= r, t && this.disconnect(e, "control_write_callback"), this.updatePressure());
399
+ }), this.withinBuffers(e) || this.disconnect(e, "control_buffer_after_send");
321
400
  } catch {
322
- this.disconnect(e);
401
+ this.disconnect(e, "control_send_exception");
323
402
  }
324
403
  }
325
404
  reject(e, t) {
@@ -341,7 +420,7 @@ var F = class {
341
420
  let s = (e) => {
342
421
  o(e), r.abort();
343
422
  };
344
- a = () => s(null), e.addEventListener("abort", a, { once: !0 }), i = setTimeout(a, t), N(i), Promise.resolve().then(() => r.signal.aborted ? null : n(r.signal)).then(s, () => s(null));
423
+ a = () => s(null), e.addEventListener("abort", a, { once: !0 }), i = setTimeout(a, t), L(i), Promise.resolve().then(() => r.signal.aborted ? null : n(r.signal)).then(s, () => s(null));
345
424
  });
346
425
  } catch {
347
426
  return null;
@@ -364,7 +443,7 @@ var F = class {
364
443
  if (!this.live(e)) return;
365
444
  let n = r(t);
366
445
  if (e.pending && (e.controlPending || e.phase === "service" && n.type !== "SERVICE_CHALLENGE" || e.phase === "register" && n.type !== "REGISTER")) {
367
- this.disconnect(e);
446
+ this.disconnect(e, "pending_control");
368
447
  return;
369
448
  }
370
449
  if (n.type === "OPEN" && !e.controlPending) {
@@ -372,7 +451,7 @@ var F = class {
372
451
  return;
373
452
  }
374
453
  if (this.capturePendingEnd(e, n), e.controls.length + Number(e.controlPending) >= this.limits.deviceRoutes + 1) {
375
- this.disconnect(e);
454
+ this.disconnect(e, "control_queue_limit");
376
455
  return;
377
456
  }
378
457
  await new Promise((t) => {
@@ -382,7 +461,7 @@ var F = class {
382
461
  }), this.nextControl(e);
383
462
  });
384
463
  } catch {
385
- this.disconnect(e);
464
+ this.disconnect(e, "text_exception");
386
465
  }
387
466
  }
388
467
  nextControl(e) {
@@ -395,12 +474,12 @@ var F = class {
395
474
  async runControl(e, t) {
396
475
  try {
397
476
  if (!await this.quota(e, "control", 1, e.cancel.signal)) {
398
- this.disconnect(e);
477
+ this.disconnect(e, "control_quota");
399
478
  return;
400
479
  }
401
480
  await this.control(e, t);
402
481
  } catch {
403
- this.disconnect(e);
482
+ this.disconnect(e, "control_exception");
404
483
  }
405
484
  }
406
485
  capturePendingEnd(e, t) {
@@ -426,23 +505,23 @@ var F = class {
426
505
  case "CLOSE":
427
506
  this.routeControl(e, t);
428
507
  return;
429
- default: this.disconnect(e);
508
+ default: this.disconnect(e, "unexpected_control");
430
509
  }
431
510
  }
432
511
  async service(e, t) {
433
512
  if (e.phase !== "service" && e.phase !== "ready" || t.body.audienceId !== this.namespace.relayAudienceId || e.registration && this.now() < e.registration.started + 72e4) {
434
- this.disconnect(e);
513
+ this.disconnect(e, "service_context");
435
514
  return;
436
515
  }
437
516
  e.phase = "signing";
438
- let n = await this.bounded(e.cancel.signal, this.limits.coordinatorTimeoutMs, () => s({
517
+ let n = await this.bounded(e.cancel.signal, this.limits.coordinatorTimeoutMs, () => c({
439
518
  audienceId: this.namespace.relayAudienceId,
440
519
  challengeId: t.session,
441
520
  utcMs: this.utc(),
442
521
  signer: this.options.signer
443
522
  }));
444
523
  if (!n || !this.live(e)) {
445
- this.disconnect(e);
524
+ this.disconnect(e, "service_proof");
446
525
  return;
447
526
  }
448
527
  this.send(e, {
@@ -451,7 +530,7 @@ var F = class {
451
530
  session: t.session,
452
531
  body: n
453
532
  }), this.live(e) && (e.challenge = {
454
- id: M(this.options.random ?? j, 16),
533
+ id: I(this.options.random ?? F, 16),
455
534
  expires: this.now() + 3e4
456
535
  }, e.phase = "register", this.send(e, {
457
536
  v: 1,
@@ -478,13 +557,13 @@ var F = class {
478
557
  async register(e, t) {
479
558
  let r = n(t.body), i = e.challenge;
480
559
  if (e.phase !== "register" || !i || r.kind !== "registration" || t.session !== i.id || r.value.socketChallengeId !== i.id || !this.contextMatches(r.value) || e.registration && (e.registration.deviceId !== r.value.deviceId || e.registration.keyId !== r.value.keyId)) {
481
- this.disconnect(e);
560
+ this.disconnect(e, "registration_context");
482
561
  return;
483
562
  }
484
563
  e.phase = "checking";
485
564
  let a = await this.check(e, t.body, e.cancel.signal);
486
565
  if (!a || !this.live(e)) {
487
- this.disconnect(e);
566
+ this.disconnect(e, "registration_denied");
488
567
  return;
489
568
  }
490
569
  let o = r.value;
@@ -497,9 +576,9 @@ var F = class {
497
576
  }
498
577
  completeRegistration(e, t, n) {
499
578
  let r = [...this.connections].filter((n) => n !== e && n.registration?.deviceId === t.deviceId), i = r.some((e) => e.closing);
500
- for (let e of r) e.registration?.keyId !== t.keyId && this.disconnect(e);
579
+ for (let e of r) e.registration?.keyId !== t.keyId && this.disconnect(e, "registration_key_replaced");
501
580
  if (i || r.length > 1) {
502
- this.disconnect(e);
581
+ this.disconnect(e, "registration_conflict");
503
582
  return;
504
583
  }
505
584
  let a = {
@@ -516,23 +595,23 @@ var F = class {
516
595
  };
517
596
  if (!this.withinBuffers(e, {
518
597
  deviceId: t.deviceId,
519
- additionalBytes: A.encode(l(a)).byteLength
598
+ additionalBytes: M.encode(u(a)).byteLength
520
599
  })) {
521
- this.disconnect(e);
600
+ this.disconnect(e, "registration_buffer");
522
601
  return;
523
602
  }
524
603
  if (!this.announceRegistration(e, t)) {
525
- this.disconnect(e);
604
+ this.disconnect(e, "registration_snapshot");
526
605
  return;
527
606
  }
528
- for (let e of r) this.disconnect(e);
607
+ for (let e of r) this.disconnect(e, "registration_replaced");
529
608
  e.registration = t, e.challenge = void 0, e.pending = !1, e.phase = "ready", this.registrations.set(t.deviceId, e), this.send(e, a), this.armConnection(e);
530
609
  }
531
610
  announceRegistration(e, t) {
532
611
  let n = this.options.onRegistration;
533
612
  if (!n) return !0;
534
613
  try {
535
- return n(e.socket, C({
614
+ return n(e.socket, T({
536
615
  realmId: this.realmId,
537
616
  deviceId: t.deviceId,
538
617
  keyId: t.keyId,
@@ -554,7 +633,7 @@ var F = class {
554
633
  let o = (e) => ({
555
634
  sender: e,
556
635
  credit: 0,
557
- nextGrant: k,
636
+ nextGrant: j,
558
637
  frames: [],
559
638
  waiting: !1,
560
639
  pendingEnd: !1,
@@ -567,7 +646,7 @@ var F = class {
567
646
  context: a,
568
647
  cancel: new AbortController(),
569
648
  directions: [o(e), o(e)],
570
- expires: this.now() + O,
649
+ expires: this.now() + A,
571
650
  accepted: !1,
572
651
  closed: !1,
573
652
  bytes: 0,
@@ -580,7 +659,7 @@ var F = class {
580
659
  e.controlPending = !0;
581
660
  try {
582
661
  if (!await this.quota(e, "control", 1, e.cancel.signal)) {
583
- this.disconnect(e);
662
+ this.disconnect(e, "open_control_quota");
584
663
  return;
585
664
  }
586
665
  if (!n) {
@@ -611,35 +690,35 @@ var F = class {
611
690
  n.target = i, n.directions[1].sender = i, this.armRoute(n), this.send(i, t);
612
691
  }
613
692
  routeLive(e) {
614
- return e.closed ? !1 : this.now() >= e.expires ? (this.removeRoute(e, e.accepted ? "abort" : "reject"), !1) : !0;
693
+ return e.closed ? !1 : this.now() >= e.expires ? (this.removeRoute(e, e.accepted ? "abort" : "reject", "route_expired"), !1) : !0;
615
694
  }
616
695
  armRoute(e) {
617
696
  e.timer && clearTimeout(e.timer), e.timer = setTimeout(() => {
618
697
  this.routeLive(e) && this.armRoute(e);
619
- }, Math.max(1, Math.ceil(e.expires - this.now()))), N(e.timer);
698
+ }, Math.max(1, Math.ceil(e.expires - this.now()))), L(e.timer);
620
699
  }
621
700
  routeControl(e, t) {
622
701
  let n = this.routes.get(t.session);
623
702
  if (!(!n || !this.routeLive(n))) {
624
703
  if (e !== n.source && e !== n.target) {
625
- this.disconnect(e);
704
+ this.disconnect(e, "route_control_nonparticipant");
626
705
  return;
627
706
  }
628
707
  if (t.type === "CLOSE") {
629
708
  if (t.body.mode === "abort" || !n.accepted) {
630
- this.removeRoute(n, "abort");
709
+ this.removeRoute(n, "abort", N[t.body.mode], e);
631
710
  return;
632
711
  }
633
712
  let r = n.directions[Number(e === n.target)];
634
713
  if (!r || r.ended) {
635
- this.removeRoute(n, "abort");
714
+ this.removeRoute(n, "abort", "duplicate_end", e);
636
715
  return;
637
716
  }
638
717
  r.ended = !0, this.flushEnd(n, r);
639
718
  return;
640
719
  }
641
720
  if (e !== n.target || n.accepted || t.type !== "ACCEPTED" && t.type !== "REJECTED") {
642
- this.removeRoute(n, "abort");
721
+ this.removeRoute(n, "abort", "invalid_route_control", e);
643
722
  return;
644
723
  }
645
724
  if (t.type === "REJECTED") {
@@ -654,9 +733,9 @@ var F = class {
654
733
  });
655
734
  }
656
735
  }
657
- removeRoute(e, t) {
736
+ removeRoute(e, t, n, r) {
658
737
  if (!e.closed) {
659
- e.closed = !0, this.routes.delete(e.id), e.cancel.abort(), e.timer && clearTimeout(e.timer);
738
+ e.closed = !0, t === "abort" && n && this.diagnoseRoute(e, n, r), this.routes.delete(e.id), e.cancel.abort(), e.timer && clearTimeout(e.timer);
660
739
  for (let t of e.directions) {
661
740
  for (let e of t.frames) t.sender.held -= e.length;
662
741
  t.frames.length = 0, t.waiting = !1;
@@ -670,54 +749,131 @@ var F = class {
670
749
  this.updatePressure();
671
750
  }
672
751
  }
752
+ routeDiagnostic(e, t, n) {
753
+ let r = null;
754
+ n === e.source ? r = "source" : n === e.target && (r = "target");
755
+ let i = this.now(), a = [this.directionDiagnostic(e.directions[0]), this.directionDiagnostic(e.directions[1])];
756
+ return {
757
+ event: "route_abort",
758
+ trace: o(M.encode(e.id)).slice(0, 16),
759
+ reason: b[t],
760
+ site: t,
761
+ side: r,
762
+ accepted: e.accepted,
763
+ bytes: e.bytes,
764
+ frames: e.frames,
765
+ queuedFrames: a.reduce((e, t) => e + t.queuedFrames, 0),
766
+ pendingWrites: a.reduce((e, t) => e + t.writes, 0),
767
+ waitingDirections: a.filter((e) => e.waiting).length,
768
+ remainingMs: P(e.expires, i),
769
+ queuedBytes: a.reduce((e, t) => e + t.queuedBytes + t.writes, 0),
770
+ directions: a,
771
+ source: this.connectionDiagnostic(e.source, i),
772
+ target: e.target ? this.connectionDiagnostic(e.target, i) : null
773
+ };
774
+ }
775
+ directionDiagnostic(e) {
776
+ return {
777
+ queuedFrames: e.frames.length,
778
+ queuedBytes: e.frames.reduce((e, t) => e + t.length, 0),
779
+ writes: e.writes,
780
+ credit: e.credit,
781
+ nextGrant: e.nextGrant,
782
+ waiting: e.waiting,
783
+ ended: e.ended,
784
+ pendingEnd: e.pendingEnd,
785
+ endForwarded: e.endForwarded
786
+ };
787
+ }
788
+ connectionDiagnostic(e, t) {
789
+ return {
790
+ phase: e.phase,
791
+ pending: e.pending,
792
+ closing: e.closing,
793
+ physicalClosed: e.physicalClosed,
794
+ restored: e.restored !== void 0,
795
+ held: e.held,
796
+ writes: e.writes,
797
+ registrationRemainingMs: e.registration ? P(e.registration.expires, t) : null
798
+ };
799
+ }
800
+ diagnoseRoute(e, t, n) {
801
+ let r = this.options.onDiagnostic;
802
+ if (r) try {
803
+ r(e.terminalDiagnostic ?? this.routeDiagnostic(e, t, n));
804
+ } catch {}
805
+ }
806
+ diagnoseOrphan(e) {
807
+ let t = this.options.onDiagnostic;
808
+ if (t) try {
809
+ t({
810
+ event: "orphan_abort",
811
+ trace: o(M.encode(e)).slice(0, 16),
812
+ reason: "restored_route_lost",
813
+ site: "restored_route_lost",
814
+ side: null,
815
+ accepted: !1,
816
+ bytes: 0,
817
+ frames: 0,
818
+ queuedFrames: 0,
819
+ pendingWrites: 0,
820
+ waitingDirections: 0,
821
+ remainingMs: null,
822
+ queuedBytes: 0,
823
+ directions: null,
824
+ source: null,
825
+ target: null
826
+ });
827
+ } catch {}
828
+ }
673
829
  binary(e, t) {
674
830
  if (!this.live(e)) return;
675
831
  if (e.pending) {
676
- this.disconnect(e);
832
+ this.disconnect(e, "data_pending_socket");
677
833
  return;
678
834
  }
679
- let n = u(t), r = this.routes.get(n.sessionId);
835
+ let n = d(t), r = this.routes.get(n.sessionId);
680
836
  if (!r) {
681
837
  this.orphan(e, n.sessionId);
682
838
  return;
683
839
  }
684
840
  if (!this.routeLive(r)) return;
685
841
  if (e !== r.source && e !== r.target) {
686
- this.disconnect(e);
842
+ this.disconnect(e, "data_nonparticipant");
687
843
  return;
688
844
  }
689
845
  let i = r.directions[Number(e === r.target)];
690
846
  if (!r.accepted || !i || i.pendingEnd || i.ended || ++r.frames > 2048 || t.length > this.limits.routeBytes - r.bytes) {
691
- this.removeRoute(r, "abort");
847
+ this.removeRoute(r, "abort", "data_route_limits", e);
692
848
  return;
693
849
  }
694
- if (r.bytes += t.length, e.held += t.length, !this.withinBuffers(e) || this.directionQueued(i) + t.length > D) {
695
- e.held -= t.length, this.removeRoute(r, "abort");
850
+ if (r.bytes += t.length, e.held += t.length, !this.withinBuffers(e) || this.directionQueued(i) + t.length > k) {
851
+ e.held -= t.length, this.removeRoute(r, "abort", "data_buffer_limit", e);
696
852
  return;
697
853
  }
698
854
  i.frames.push(new Uint8Array(t)), this.drainDirection(r, i);
699
855
  }
700
856
  orphan(e, t) {
701
857
  let n = e.restored;
702
- if (!n || this.now() >= n.at + O) {
703
- this.disconnect(e);
858
+ if (!n || this.now() >= n.at + A) {
859
+ this.disconnect(e, "orphan_outside_restore");
704
860
  return;
705
861
  }
706
862
  let r = n.orphans;
707
863
  if (!r.has(t)) {
708
864
  if (r.size >= this.limits.deviceRoutes) {
709
- this.disconnect(e);
865
+ this.disconnect(e, "orphan_limit");
710
866
  return;
711
867
  }
712
- r.add(t), this.abortOrphan(e, t).catch(() => this.disconnect(e));
868
+ r.add(t), this.abortOrphan(e, t).catch(() => this.disconnect(e, "orphan_exception"));
713
869
  }
714
870
  }
715
871
  async abortOrphan(e, t) {
716
872
  if (!await this.quota(e, "control", 1, e.cancel.signal)) {
717
- this.disconnect(e);
873
+ this.disconnect(e, "orphan_control_quota");
718
874
  return;
719
875
  }
720
- this.send(e, {
876
+ this.diagnoseOrphan(t), this.send(e, {
721
877
  v: 1,
722
878
  type: "CLOSE",
723
879
  session: t,
@@ -727,17 +883,17 @@ var F = class {
727
883
  drainDirection(e, t) {
728
884
  if (!(!this.routeLive(e) || t.waiting)) {
729
885
  for (; t.frames[0] && t.credit >= t.frames[0].length;) if (this.forward(e, t), e.closed) return;
730
- t.frames.length > 0 ? (t.waiting = !0, this.reserveTraffic(e, t).catch(() => this.removeRoute(e, "abort"))) : this.flushEnd(e, t), this.updatePressure();
886
+ t.frames.length > 0 ? (t.waiting = !0, this.reserveTraffic(e, t).catch(() => this.removeRoute(e, "abort", "traffic_reservation_exception", t.sender))) : this.flushEnd(e, t), this.updatePressure();
731
887
  }
732
888
  }
733
889
  async reserveTraffic(e, t) {
734
- let n = t.sender === e.source ? e.context.targetDeviceId : e.context.sourceDeviceId, r = t.frames.reduce((e, t) => e + t.length, 0), i = Math.min(D, this.limits.routeBytes - e.bytes + r, Math.max(r, t.nextGrant)), a = await this.quota(t.sender, "traffic", i, e.cancel.signal, n);
890
+ let n = t.sender === e.source ? e.context.targetDeviceId : e.context.sourceDeviceId, r = t.frames.reduce((e, t) => e + t.length, 0), i = Math.min(k, this.limits.routeBytes - e.bytes + r, Math.max(r, t.nextGrant)), a = await this.quota(t.sender, "traffic", i, e.cancel.signal, n);
735
891
  if (this.routeLive(e)) {
736
892
  if (t.waiting = !1, !a) {
737
- this.trafficQuotaAborts++, this.removeRoute(e, "abort");
893
+ this.trafficQuotaAborts++, this.removeRoute(e, "abort", "traffic_quota", t.sender);
738
894
  return;
739
895
  }
740
- t.credit += i, t.nextGrant = Math.min(D, i * 2), this.drainDirection(e, t);
896
+ t.credit += i, t.nextGrant = Math.min(k, i * 2), this.drainDirection(e, t);
741
897
  }
742
898
  }
743
899
  recipient(e, t) {
@@ -752,21 +908,21 @@ var F = class {
752
908
  connection: t.sender,
753
909
  bytes: n.length
754
910
  }
755
- }) || this.directionQueued(t) > D) {
756
- this.removeRoute(e, "abort");
911
+ }) || this.directionQueued(t) > k) {
912
+ this.removeRoute(e, "abort", "forward_recipient_or_buffer", r);
757
913
  return;
758
914
  }
759
- if (t.frames.shift(), t.sender.held -= n.length, t.credit -= n.length, t.writes += n.length, r.writes += n.length, !this.withinBuffers(r) || this.directionQueued(t) > D) {
760
- r.writes -= n.length, t.writes -= n.length, this.removeRoute(e, "abort");
915
+ if (t.frames.shift(), t.sender.held -= n.length, t.credit -= n.length, t.writes += n.length, r.writes += n.length, !this.withinBuffers(r) || this.directionQueued(t) > k) {
916
+ r.writes -= n.length, t.writes -= n.length, this.removeRoute(e, "abort", "forward_buffer_preflight", r);
761
917
  return;
762
918
  }
763
919
  let i = !1;
764
920
  try {
765
921
  r.socket.sendBinary(n, (a) => {
766
- i || (i = !0, r.writes -= n.length, t.writes -= n.length, a ? this.removeRoute(e, "abort") : this.routeLive(e) && this.flushEnd(e, t), this.updatePressure());
767
- }), (!this.withinBuffers(r) || this.directionQueued(t) > D) && this.removeRoute(e, "abort"), this.updatePressure();
922
+ i || (i = !0, r.writes -= n.length, t.writes -= n.length, a ? this.removeRoute(e, "abort", "data_write_callback", r) : this.routeLive(e) && this.flushEnd(e, t), this.updatePressure());
923
+ }), (!this.withinBuffers(r) || this.directionQueued(t) > k) && this.removeRoute(e, "abort", "forward_buffer_after_send", r), this.updatePressure();
768
924
  } catch {
769
- this.removeRoute(e, "abort"), this.disconnect(r);
925
+ this.removeRoute(e, "abort", "data_send_exception", r), this.disconnect(r, "data_send_exception");
770
926
  }
771
927
  }
772
928
  flushEnd(e, t) {
@@ -819,13 +975,13 @@ var F = class {
819
975
  try {
820
976
  t ? e.socket.pauseInput() : e.socket.resumeInput();
821
977
  } catch {
822
- this.disconnect(e);
978
+ this.disconnect(e, "input_pressure_exception");
823
979
  }
824
980
  }
825
981
  }
826
982
  }
827
983
  };
828
984
  //#endregion
829
- export { g as a, v as i, y as n, p as o, h as r, P as t };
985
+ export { _ as a, y as i, x as n, m as o, g as r, R as t };
830
986
 
831
- //# sourceMappingURL=relay-server-CLNJrfOV.js.map
987
+ //# sourceMappingURL=relay-server-TOaojlAm.js.map