@ping-app/ping-openmls-sdk 0.9.2 → 0.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.
package/dist/index.cjs CHANGED
@@ -743,6 +743,19 @@ var MessagingClient = class _MessagingClient {
743
743
  fanout = new MessageFanout();
744
744
  conversationHandlers = /* @__PURE__ */ new Set();
745
745
  subscription = null;
746
+ /**
747
+ * This client's copy of each conversation's metadata, keyed by lowercase-hex
748
+ * conversation id: what a conversation handle's `name`, `epoch` and `meta()`
749
+ * answer from.
750
+ *
751
+ * It is never older than what the host has been told. The worker attaches a
752
+ * conversation's metadata to every change it reports, and {@link handleWorker}
753
+ * stores it before it calls a listener or settles a call; an entry whose
754
+ * conversation changed with no metadata attached is dropped, never kept.
755
+ * Bounded by the conversations the client holds: an entry goes when its
756
+ * conversation is left, dropped or removed, and `listConversations()`
757
+ * replaces the whole map.
758
+ */
746
759
  metaCache = /* @__PURE__ */ new Map();
747
760
  /**
748
761
  * Per-conversation batching of sequenced live frames: the first frame goes
@@ -1530,6 +1543,33 @@ var MessagingClient = class _MessagingClient {
1530
1543
  for (const m of metas) this.metaCache.set(hex(m.id), m);
1531
1544
  return metas;
1532
1545
  }
1546
+ /**
1547
+ * ONE conversation's metadata as the group holds it now: the same record as
1548
+ * its element of {@link listConversations}, without listing the others.
1549
+ * Resolves `null` when this client does not hold the conversation (unknown,
1550
+ * dropped, removed, or left). A pure read of local state: nothing is
1551
+ * fetched. Rejects for an id that is not 16 bytes.
1552
+ *
1553
+ * It also brings the conversation handle's `name`, `epoch` and `meta()` to
1554
+ * the answer, though they do not need it to be fresh: the client replaces
1555
+ * its copy of a conversation's metadata
1556
+ * - before any {@link onConversationUpdated} listener is told the
1557
+ * conversation changed (a Commit from another member, a join, a page of
1558
+ * a catch-up), and
1559
+ * - before this device's own call that changed it resolves (a rename, a
1560
+ * roles or membership change, a creation, a leave).
1561
+ * Use this read for a conversation nothing has reported yet in this
1562
+ * session, or to learn that a conversation is no longer held.
1563
+ */
1564
+ async readConversationMeta(id) {
1565
+ const meta = await this.call({
1566
+ kind: "conversationMeta",
1567
+ id: this.nextId++,
1568
+ conv: id
1569
+ });
1570
+ this.storeMeta(id, meta);
1571
+ return meta ?? null;
1572
+ }
1533
1573
  /**
1534
1574
  * Conversations detected as STRANDED during catch-up — a Commit was missed
1535
1575
  * and the group can no longer advance from local state, so new messages stop
@@ -2382,12 +2422,14 @@ var MessagingClient = class _MessagingClient {
2382
2422
  return {
2383
2423
  id,
2384
2424
  async send(plaintext) {
2425
+ const heldEpoch = self.metaCache.get(hex(id))?.epoch;
2385
2426
  return await self.call({
2386
2427
  kind: "send",
2387
2428
  id: self.nextId++,
2388
2429
  conv: id,
2389
2430
  plaintext,
2390
- nowMs: self.now()
2431
+ nowMs: self.now(),
2432
+ ...heldEpoch !== void 0 ? { heldEpoch } : {}
2391
2433
  });
2392
2434
  },
2393
2435
  async addMembers(entries) {
@@ -2465,8 +2507,8 @@ var MessagingClient = class _MessagingClient {
2465
2507
  });
2466
2508
  },
2467
2509
  /**
2468
- * The conversation's MLS GroupContext name, or `undefined` when the meta
2469
- * cache has not been primed (call `listConversations()`).
2510
+ * The conversation's MLS GroupContext name as the group holds it now, or
2511
+ * `undefined` when it has none or the client holds no metadata for it.
2470
2512
  *
2471
2513
  * This accessor did not exist, and hosts reasonably assumed it did:
2472
2514
  * `getConversation(id).name` read `undefined` on every call, which
@@ -2475,24 +2517,36 @@ var MessagingClient = class _MessagingClient {
2475
2517
  * absent, so a group would materialise unnamed until a best-effort
2476
2518
  * broadcast happened to arrive.
2477
2519
  *
2478
- * A getter rather than a snapshot, so it reflects the cache at read time
2479
- * and never goes stale against a later `listConversations()`.
2520
+ * A getter rather than a snapshot, so it reflects the client's copy at
2521
+ * read time. That copy used to be refilled by `listConversations()`
2522
+ * only, and this answered the name from BEFORE a rename until the host
2523
+ * listed again; it is now replaced with every change the worker reports
2524
+ * (see `metaCache`).
2480
2525
  */
2481
2526
  get name() {
2482
2527
  return self.metaCache.get(hex(id))?.name ?? void 0;
2483
2528
  },
2484
- /** The conversation's current MLS epoch, or `undefined` when the meta
2485
- * cache has not been primed. */
2529
+ /** The conversation's current MLS epoch, or `undefined` when the client
2530
+ * holds no metadata for it. */
2486
2531
  get epoch() {
2487
2532
  return self.metaCache.get(hex(id))?.epoch;
2488
2533
  },
2489
2534
  meta() {
2490
2535
  const cached = self.metaCache.get(hex(id));
2491
- if (!cached) throw new Error("conversation meta not loaded; call listConversations() first");
2536
+ if (!cached) {
2537
+ throw new Error("conversation meta not loaded; call readConversationMeta(id) or listConversations() first");
2538
+ }
2492
2539
  return cached;
2493
2540
  }
2494
2541
  };
2495
2542
  }
2543
+ /** Replace this client's copy of one conversation's metadata with what the
2544
+ * worker read; no metadata means the copy is dropped, so the handle answers
2545
+ * "not held" instead of what the group held before. */
2546
+ storeMeta(id, meta) {
2547
+ if (meta === null || meta === void 0) this.metaCache.delete(hex(id));
2548
+ else this.metaCache.set(hex(id), meta);
2549
+ }
2496
2550
  call(req) {
2497
2551
  if (this.closed) return Promise.reject(new MessagingClientClosedError("closed"));
2498
2552
  return new Promise((resolve, reject) => {
@@ -2546,6 +2600,7 @@ var MessagingClient = class _MessagingClient {
2546
2600
  return;
2547
2601
  }
2548
2602
  case "event.conversation": {
2603
+ this.storeMeta(msg.id, msg.meta);
2549
2604
  for (const h of this.conversationHandlers) {
2550
2605
  try {
2551
2606
  h(msg.id, msg.epoch, msg.sender);
@@ -2557,6 +2612,11 @@ var MessagingClient = class _MessagingClient {
2557
2612
  }
2558
2613
  return;
2559
2614
  }
2615
+ case "cache.conversations": {
2616
+ for (const meta of msg.held) this.storeMeta(meta.id, meta);
2617
+ for (const id of msg.dropped) this.storeMeta(id, null);
2618
+ return;
2619
+ }
2560
2620
  case "storage.call": {
2561
2621
  try {
2562
2622
  const method = this.storage[msg.method];