@codemem/core 0.49.0 → 0.49.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.
@@ -1,7 +1,7 @@
1
1
  import { r as e } from "./scope-membership-cache-read-B6z47l2W.js";
2
2
  import { p as t } from "./scope-membership-semantics-B8qXaMqx.js";
3
3
  import { O as n } from "./sync-auth-constants-BVzVfSmn.js";
4
- import { A as r, B as i, E as a, I as o, L as s, O as c, U as l, d as u, f as d, j as f, k as p, rt as m, st as h, z as g } from "./relay-peer-client-j0rOyeuZ.js";
4
+ import { A as r, B as i, D as a, L as o, M as s, R as c, V as l, W as u, ct as d, f, it as p, j as m, k as h, p as g } from "./relay-peer-client-DmZfZebO.js";
5
5
  import { networkInterfaces as _ } from "node:os";
6
6
  //#region src/memory-retirement-trust.ts
7
7
  function v(e) {
@@ -80,11 +80,11 @@ function j(e, t) {
80
80
  function M(e) {
81
81
  return Number(e.db.prepare("SELECT COUNT(1) AS total FROM sync_peers").get()?.total ?? 0);
82
82
  }
83
- function N(e) {
83
+ function ee(e) {
84
84
  let t = Math.max(1, e.syncCoordinatorPresenceTtlS), n = Math.floor(t / 2);
85
85
  return Math.max(5, Math.min(60, n > 0 ? n : 1)) * 1e3;
86
86
  }
87
- function ee() {
87
+ function N() {
88
88
  return 3e4;
89
89
  }
90
90
  async function P(e, t, n, r) {
@@ -99,10 +99,10 @@ async function P(e, t, n, r) {
99
99
  status: "posted",
100
100
  error: null,
101
101
  advertisedAddresses: o,
102
- nextRefreshAtMs: r + N(t)
102
+ nextRefreshAtMs: r + ee(t)
103
103
  });
104
104
  } catch (e) {
105
- let t = e instanceof Error ? e.message : String(e), a = t.includes("unknown_device") ? "not_enrolled" : "error", o = a === "not_enrolled" ? r : r + ee();
105
+ let t = e instanceof Error ? e.message : String(e), a = t.includes("unknown_device") ? "not_enrolled" : "error", o = a === "not_enrolled" ? r : r + N();
106
106
  n.presence_status = a, n.presence_error = t, n.advertised_addresses = [], C.set(i, {
107
107
  status: a,
108
108
  error: t,
@@ -113,47 +113,47 @@ async function P(e, t, n, r) {
113
113
  }
114
114
  function F(e) {
115
115
  let t = e.syncAdvertise.toLowerCase();
116
- if (t && t !== "auto" && t !== "default") return p([], t.split(",").map((e) => e.trim()).filter(Boolean), { defaultHttpPort: e.syncPort });
117
- if (e.syncHost && e.syncHost !== "0.0.0.0") return [f(c(e.syncHost, e.syncPort))].filter(Boolean);
118
- let n = Object.values(_()).flatMap((e) => e ?? []).filter((e) => !e.internal).map((e) => e.address).filter((e) => e && e !== "127.0.0.1" && e !== "::1").map((t) => f(c(t, e.syncPort))).filter(Boolean);
116
+ if (t && t !== "auto" && t !== "default") return r([], t.split(",").map((e) => e.trim()).filter(Boolean), { defaultHttpPort: e.syncPort });
117
+ if (e.syncHost && e.syncHost !== "0.0.0.0") return [s(h(e.syncHost, e.syncPort))].filter(Boolean);
118
+ let n = Object.values(_()).flatMap((e) => e ?? []).filter((e) => !e.internal).map((e) => e.address).filter((e) => e && e !== "127.0.0.1" && e !== "::1").map((t) => s(h(t, e.syncPort))).filter(Boolean);
119
119
  return [...new Set(n)];
120
120
  }
121
121
  async function I(e, t, n) {
122
122
  if (!o(t)) return null;
123
- let r = n?.keysDir ?? (process.env.CODEMEM_KEYS_DIR?.trim() || void 0), [a, s] = m(e.db, { keysDir: r }), c = h(r);
123
+ let r = n?.keysDir ?? (process.env.CODEMEM_KEYS_DIR?.trim() || void 0), [a, s] = p(e.db, { keysDir: r }), c = d(r);
124
124
  if (!c) throw Error("public key missing");
125
- let f = g(t.syncCoordinatorUrl), p = {
125
+ let m = i(t.syncCoordinatorUrl), h = {
126
126
  fingerprint: s,
127
127
  public_key: c,
128
128
  addresses: F(t),
129
129
  ttl_s: Math.max(1, t.syncCoordinatorPresenceTtlS),
130
130
  capabilities: {
131
- sync_capability: u,
132
- sync_features: d
131
+ sync_capability: f,
132
+ sync_features: g
133
133
  }
134
134
  }, _ = [];
135
135
  for (let n of t.syncCoordinatorGroups) {
136
- let o = {
137
- ...p,
136
+ let i = {
137
+ ...h,
138
138
  group_id: n
139
- }, s = Buffer.from(JSON.stringify(o), "utf8"), c = `${f}/v1/presence`, u = l({
139
+ }, o = Buffer.from(JSON.stringify(i), "utf8"), s = `${m}/v1/presence`, c = u({
140
140
  deviceId: a,
141
141
  dbPath: e.dbPath,
142
142
  method: "POST",
143
- url: c,
144
- bodyBytes: s,
143
+ url: s,
144
+ bodyBytes: o,
145
145
  keysDir: r
146
- }), [d, m] = await i("POST", c, {
147
- headers: u,
148
- body: o,
149
- bodyBytes: s,
146
+ }), [d, f] = await l("POST", s, {
147
+ headers: c,
148
+ body: i,
149
+ bodyBytes: o,
150
150
  timeoutS: Math.max(1, t.syncCoordinatorTimeoutS)
151
151
  });
152
- if (d !== 200 || !m) {
153
- let e = typeof m?.error == "string" ? m.error : "unknown";
152
+ if (d !== 200 || !f) {
153
+ let e = typeof f?.error == "string" ? f.error : "unknown";
154
154
  throw Error(`coordinator presence failed (${d}: ${e})`);
155
155
  }
156
- _.push(m);
156
+ _.push(f);
157
157
  }
158
158
  return {
159
159
  groups: t.syncCoordinatorGroups,
@@ -162,47 +162,47 @@ async function I(e, t, n) {
162
162
  }
163
163
  async function L(e, t, n) {
164
164
  if (!o(t)) return [];
165
- let r = n?.keysDir ?? (process.env.CODEMEM_KEYS_DIR?.trim() || void 0), [a] = m(e.db, { keysDir: r }), s = g(t.syncCoordinatorUrl), c = /* @__PURE__ */ new Map();
165
+ let a = n?.keysDir ?? (process.env.CODEMEM_KEYS_DIR?.trim() || void 0), [s] = p(e.db, { keysDir: a }), c = i(t.syncCoordinatorUrl), d = /* @__PURE__ */ new Map();
166
166
  for (let n of t.syncCoordinatorGroups) {
167
- let o = `${s}/v1/peers?group_id=${encodeURIComponent(n)}`, u = l({
168
- deviceId: a,
167
+ let i = `${c}/v1/peers?group_id=${encodeURIComponent(n)}`, o = u({
168
+ deviceId: s,
169
169
  dbPath: e.dbPath,
170
170
  method: "GET",
171
- url: o,
171
+ url: i,
172
172
  bodyBytes: Buffer.alloc(0),
173
- keysDir: r
174
- }), [d, f] = await i("GET", o, {
175
- headers: u,
173
+ keysDir: a
174
+ }), [f, p] = await l("GET", i, {
175
+ headers: o,
176
176
  timeoutS: Math.max(1, t.syncCoordinatorTimeoutS)
177
177
  });
178
- if (d !== 200 || !f) {
179
- let e = typeof f?.error == "string" ? f.error : "unknown";
180
- throw Error(`coordinator lookup failed (${d}: ${e})`);
178
+ if (f !== 200 || !p) {
179
+ let e = typeof p?.error == "string" ? p.error : "unknown";
180
+ throw Error(`coordinator lookup failed (${f}: ${e})`);
181
181
  }
182
- let m = Array.isArray(f.items) ? f.items : [];
182
+ let m = Array.isArray(p.items) ? p.items : [];
183
183
  for (let e of m) {
184
184
  if (!e || typeof e != "object") continue;
185
- let t = e, r = b(t.device_id), i = b(t.fingerprint);
186
- if (!r) continue;
187
- let a = t.stale ? [] : Array.isArray(t.addresses) ? t.addresses.filter((e) => typeof e == "string") : [], o = `${r}:${i}`, l = c.get(o);
185
+ let t = e, i = b(t.device_id), a = b(t.fingerprint);
186
+ if (!i) continue;
187
+ let o = t.stale ? [] : Array.isArray(t.addresses) ? t.addresses.filter((e) => typeof e == "string") : [], s = `${i}:${a}`, l = d.get(s);
188
188
  if (!l) {
189
- c.set(o, {
189
+ d.set(s, {
190
190
  ...t,
191
- addresses: p([], a),
192
- coordinator_id: s,
191
+ addresses: r([], o),
192
+ coordinator_id: c,
193
193
  groups: [n],
194
194
  fresh_groups: t.stale ? [] : [n]
195
195
  });
196
196
  continue;
197
197
  }
198
- l.addresses = p(Array.isArray(l.addresses) ? l.addresses : [], a), l.groups = x(Array.isArray(l.groups) ? l.groups : [], [n]), l.fresh_groups = x(Array.isArray(l.fresh_groups) ? l.fresh_groups : [], t.stale ? [] : [n]);
199
- let u = !!l.stale, d = !!t.stale;
200
- l.stale = u && d;
201
- let f = !d && (u || b(t.last_seen_at) > b(l.last_seen_at)) || d && u && b(t.last_seen_at) > b(l.last_seen_at);
202
- f && (l.last_seen_at = t.last_seen_at), d ? u && f && (l.expires_at = t.expires_at) : l.expires_at = u ? t.expires_at : S(l.expires_at, t.expires_at);
198
+ l.addresses = r(Array.isArray(l.addresses) ? l.addresses : [], o), l.groups = x(Array.isArray(l.groups) ? l.groups : [], [n]), l.fresh_groups = x(Array.isArray(l.fresh_groups) ? l.fresh_groups : [], t.stale ? [] : [n]);
199
+ let u = !!l.stale, f = !!t.stale;
200
+ l.stale = u && f;
201
+ let p = !f && (u || b(t.last_seen_at) > b(l.last_seen_at)) || f && u && b(t.last_seen_at) > b(l.last_seen_at);
202
+ p && (l.last_seen_at = t.last_seen_at), f ? u && p && (l.expires_at = t.expires_at) : l.expires_at = u ? t.expires_at : S(l.expires_at, t.expires_at);
203
203
  }
204
204
  }
205
- return [...c.values()];
205
+ return [...d.values()];
206
206
  }
207
207
  function R(e) {
208
208
  return Array.isArray(e) ? e.filter((e) => typeof e == "string" && e.trim().length > 0) : [];
@@ -229,27 +229,27 @@ function V(e, t, n) {
229
229
  if (!i || !a) return !1;
230
230
  let o = t.get(`${i}:${a}`);
231
231
  if (!o?.length) return !1;
232
- let s = p(B(e.addresses_json), []), c = e.manual_addresses_json == null ? s : B(e.manual_addresses_json), l = r(s, o, c, { successfulAddress: e.last_success_address ?? void 0 });
232
+ let s = r(B(e.addresses_json), []), c = e.manual_addresses_json == null ? s : B(e.manual_addresses_json), l = m(s, o, c, { successfulAddress: e.last_success_address ?? void 0 });
233
233
  return JSON.stringify(l) === JSON.stringify(s) && e.manual_addresses_json != null ? !1 : (n(JSON.stringify(l), JSON.stringify(c), i, a), !0);
234
234
  }
235
235
  function H(e, t) {
236
236
  let n = /* @__PURE__ */ new Map();
237
237
  for (let e of t) {
238
238
  if (e.stale) continue;
239
- let t = b(e.device_id), r = b(e.fingerprint);
240
- if (!t || !r) continue;
241
- let i = R(e.addresses);
242
- if (i.length === 0) continue;
243
- let a = `${t}:${r}`;
244
- n.set(a, p(n.get(a) ?? [], i));
239
+ let t = b(e.device_id), i = b(e.fingerprint);
240
+ if (!t || !i) continue;
241
+ let a = R(e.addresses);
242
+ if (a.length === 0) continue;
243
+ let o = `${t}:${i}`;
244
+ n.set(o, r(n.get(o) ?? [], a));
245
245
  }
246
246
  if (n.size === 0) return 0;
247
- let r = e.prepare("SELECT peer_device_id, pinned_fingerprint, addresses_json, manual_addresses_json, last_success_address FROM sync_peers").all(), i = e.prepare("UPDATE sync_peers SET addresses_json = ?, manual_addresses_json = ? WHERE peer_device_id = ? AND pinned_fingerprint = ?"), a = (e, t, n, r) => {
248
- i.run(e, t, n, r);
249
- }, o = 0;
247
+ let i = e.prepare("SELECT peer_device_id, pinned_fingerprint, addresses_json, manual_addresses_json, last_success_address FROM sync_peers").all(), a = e.prepare("UPDATE sync_peers SET addresses_json = ?, manual_addresses_json = ? WHERE peer_device_id = ? AND pinned_fingerprint = ?"), o = (e, t, n, r) => {
248
+ a.run(e, t, n, r);
249
+ }, s = 0;
250
250
  return e.transaction(() => {
251
- for (let e of r) V(e, n, a) && (o += 1);
252
- }).immediate(), o;
251
+ for (let e of i) V(e, n, o) && (s += 1);
252
+ }).immediate(), s;
253
253
  }
254
254
  function U(t, n) {
255
255
  let { localDeviceId: r, peerDeviceId: i, coordinatorId: a, peerGroupIds: o, peerPublicKey: s } = n, c = n.kinds?.length ? n.kinds : ["managed_project"], l = t.prepare("SELECT public_key FROM sync_device WHERE device_id = ?").pluck().get(r), u = t.prepare(`SELECT scope.scope_id, scope.coordinator_id, scope.group_id
@@ -401,7 +401,7 @@ async function X(e, t, n) {
401
401
  peers: [],
402
402
  trusted: 0
403
403
  };
404
- let r = n?.keysDir ?? (process.env.CODEMEM_KEYS_DIR?.trim() || void 0), [i] = m(e.db, { keysDir: r }), a = await L(e, t, { keysDir: r });
404
+ let r = n?.keysDir ?? (process.env.CODEMEM_KEYS_DIR?.trim() || void 0), [i] = p(e.db, { keysDir: r }), a = await L(e, t, { keysDir: r });
405
405
  return H(e.db, a), {
406
406
  peers: a,
407
407
  trusted: Y(e.db, i, a)
@@ -409,7 +409,7 @@ async function X(e, t, n) {
409
409
  }
410
410
  async function ae(e, t, n, r) {
411
411
  if (r?.peers) return Z(r.peers);
412
- let i = s();
412
+ let i = c();
413
413
  if (!o(i)) return /* @__PURE__ */ new Set();
414
414
  try {
415
415
  let r = await L({
@@ -435,20 +435,20 @@ function Z(e) {
435
435
  }
436
436
  async function Q(e, t, n) {
437
437
  if (!o(t)) return [];
438
- let r = process.env.CODEMEM_KEYS_DIR?.trim() || void 0, [a] = m(e.db, { keysDir: r }), s = g(t.syncCoordinatorUrl), c = /* @__PURE__ */ new Map();
439
- for (let o of t.syncCoordinatorGroups) {
440
- let u = `${s}/v1/reciprocal-approvals?${new URLSearchParams({
441
- group_id: o,
438
+ let r = process.env.CODEMEM_KEYS_DIR?.trim() || void 0, [a] = p(e.db, { keysDir: r }), s = i(t.syncCoordinatorUrl), c = /* @__PURE__ */ new Map();
439
+ for (let i of t.syncCoordinatorGroups) {
440
+ let o = `${s}/v1/reciprocal-approvals?${new URLSearchParams({
441
+ group_id: i,
442
442
  direction: n.direction,
443
443
  status: n.status?.trim() || "pending"
444
- }).toString()}`, d = l({
444
+ }).toString()}`, d = u({
445
445
  deviceId: a,
446
446
  dbPath: e.dbPath,
447
447
  method: "GET",
448
- url: u,
448
+ url: o,
449
449
  bodyBytes: Buffer.alloc(0),
450
450
  keysDir: r
451
- }), [f, p] = await i("GET", u, {
451
+ }), [f, p] = await l("GET", o, {
452
452
  headers: d,
453
453
  timeoutS: Math.max(1, t.syncCoordinatorTimeoutS)
454
454
  });
@@ -470,21 +470,21 @@ async function oe(e, t, r) {
470
470
  let a = r.groupId.trim(), s = r.requestedDeviceId.trim(), c = r.expectedIncomingRequestId?.trim();
471
471
  if (!a || !s) throw Error("groupId and requestedDeviceId are required.");
472
472
  if (r.expectedIncomingRequestId !== void 0 && !c) throw Error("expectedIncomingRequestId must not be empty when provided.");
473
- let u = process.env.CODEMEM_KEYS_DIR?.trim() || void 0, [d] = m(e.db, { keysDir: u }), f = `${g(t.syncCoordinatorUrl)}/v1/reciprocal-approvals`, p = {
473
+ let d = process.env.CODEMEM_KEYS_DIR?.trim() || void 0, [f] = p(e.db, { keysDir: d }), m = `${i(t.syncCoordinatorUrl)}/v1/reciprocal-approvals`, h = {
474
474
  group_id: a,
475
475
  requested_device_id: s,
476
476
  ...c === void 0 ? {} : { expected_incoming_request_id: c }
477
- }, h = Buffer.from(JSON.stringify(p), "utf8"), _ = l({
478
- deviceId: d,
477
+ }, g = Buffer.from(JSON.stringify(h), "utf8"), _ = u({
478
+ deviceId: f,
479
479
  dbPath: e.dbPath,
480
480
  method: "POST",
481
- url: f,
482
- bodyBytes: h,
483
- keysDir: u
484
- }), [v, y] = await i("POST", f, {
481
+ url: m,
482
+ bodyBytes: g,
483
+ keysDir: d
484
+ }), [v, y] = await l("POST", m, {
485
485
  headers: _,
486
- body: p,
487
- bodyBytes: h,
486
+ body: h,
487
+ bodyBytes: g,
488
488
  timeoutS: Math.max(1, t.syncCoordinatorTimeoutS)
489
489
  });
490
490
  if (v !== 200 || !y || !y.request || typeof y.request != "object") {
@@ -511,7 +511,7 @@ async function se(e, t) {
511
511
  groups: t.syncCoordinatorGroups,
512
512
  paired_peer_count: n
513
513
  };
514
- let r = process.env.CODEMEM_KEYS_DIR?.trim() || void 0, [i] = m(e.db, { keysDir: r }), a = j(e, t), s = T.get(a) ?? 0, c = Date.now(), l = w.get(a);
514
+ let r = process.env.CODEMEM_KEYS_DIR?.trim() || void 0, [i] = p(e.db, { keysDir: r }), a = j(e, t), s = T.get(a) ?? 0, c = Date.now(), l = w.get(a);
515
515
  if (l && c < l.nextRefreshAtMs) {
516
516
  let n = structuredClone(l.snapshot);
517
517
  if (await P(e, t, n, c), c = Date.now(), n.presence_status === "not_enrolled") return O(a), n.fresh_peer_count = 0, n.stale_peer_count = 0, n.discovered_peer_count = 0, n.discovered_devices = [], n.reciprocal_approvals = {
@@ -582,7 +582,7 @@ async function se(e, t) {
582
582
  async function ce(e) {
583
583
  let t = e.syncCoordinatorGroup || e.syncCoordinatorGroups[0] || "";
584
584
  if (!t || !e.syncCoordinatorUrl || !e.syncCoordinatorAdminSecret) return [];
585
- let n = `${g(e.syncCoordinatorUrl)}/v1/admin/join-requests?group_id=${encodeURIComponent(t)}`, [r, a] = await i("GET", n, {
585
+ let n = `${i(e.syncCoordinatorUrl)}/v1/admin/join-requests?group_id=${encodeURIComponent(t)}`, [r, a] = await l("GET", n, {
586
586
  headers: { "X-Codemem-Coordinator-Admin": e.syncCoordinatorAdminSecret },
587
587
  timeoutS: Math.max(1, e.syncCoordinatorTimeoutS)
588
588
  });
@@ -591,4 +591,4 @@ async function ce(e) {
591
591
  //#endregion
592
592
  export { Q as a, H as c, K as d, ce as i, I as l, oe as n, L as o, ae as r, X as s, se as t, U as u };
593
593
 
594
- //# sourceMappingURL=coordinator-runtime-BYCjEE7A.js.map
594
+ //# sourceMappingURL=coordinator-runtime-CEc6KhBE.js.map