@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/worker.cjs CHANGED
@@ -61,6 +61,25 @@ function advancedEpochs(before, after) {
61
61
  }
62
62
  return out;
63
63
  }
64
+ function conversationIdOf(key) {
65
+ const out = new Uint8Array(key.length >> 1);
66
+ for (let i = 0; i < out.length; i += 1) out[i] = Number.parseInt(key.slice(i * 2, i * 2 + 2), 16);
67
+ return out;
68
+ }
69
+ function changedHoldings(before, after) {
70
+ const appeared = [];
71
+ const still = /* @__PURE__ */ new Set();
72
+ for (const meta of after) {
73
+ const key = conversationKey(meta.id);
74
+ still.add(key);
75
+ if (!before.has(key)) appeared.push(meta);
76
+ }
77
+ const disappeared = [];
78
+ for (const key of before.keys()) {
79
+ if (!still.has(key)) disappeared.push(conversationIdOf(key));
80
+ }
81
+ return { appeared, disappeared };
82
+ }
64
83
 
65
84
  // src/catch-up-pass.ts
66
85
  function catchUpPass(deps) {
@@ -165,6 +184,7 @@ var pendingStorage = /* @__PURE__ */ new Map();
165
184
  var pendingTransport = /* @__PURE__ */ new Map();
166
185
  var nextStorageId = 1;
167
186
  var nextTransportId = 1;
187
+ var transportCalls = 0;
168
188
  var proxyStorage = {
169
189
  get: (namespace, key) => callMain("storage", "get", [namespace, key]),
170
190
  put: (namespace, key, value) => callMain("storage", "put", [namespace, key, value]),
@@ -209,6 +229,7 @@ function callMain(kind, method, args) {
209
229
  return new Promise((resolve, reject) => {
210
230
  const id = kind === "storage" ? nextStorageId++ : nextTransportId++;
211
231
  const map = kind === "storage" ? pendingStorage : pendingTransport;
232
+ if (kind === "transport") transportCalls += 1;
212
233
  map.set(id, ({ ok, value }) => ok ? resolve(value) : reject(new Error(String(value))));
213
234
  self.postMessage({ kind: `${kind}.call`, id, method, args });
214
235
  });
@@ -241,40 +262,123 @@ var EPOCH_ADVANCING_REQUESTS = /* @__PURE__ */ new Set([
241
262
  "consumeLinkingTicket",
242
263
  "importStateSnapshot"
243
264
  ]);
244
- function epochSnapshot() {
245
- if (!client) return /* @__PURE__ */ new Map();
265
+ var EPOCH_UPKEEP_REQUESTS = /* @__PURE__ */ new Set([
266
+ "buildLinkingTicket"
267
+ ]);
268
+ function readMeta(id) {
269
+ if (!client) return null;
270
+ try {
271
+ return client.conversationMeta(id) ?? null;
272
+ } catch {
273
+ return null;
274
+ }
275
+ }
276
+ var SINGLE_READS_MAX = 8;
277
+ function readMetas(ids) {
278
+ const out = /* @__PURE__ */ new Map();
279
+ for (const id of ids) out.set(conversationKey(id), null);
280
+ if (out.size <= SINGLE_READS_MAX) {
281
+ for (const id of ids) out.set(conversationKey(id), readMeta(id));
282
+ return out;
283
+ }
284
+ if (!client) return out;
246
285
  try {
247
- return epochMap(client.listConversations());
286
+ for (const meta of client.listConversations()) {
287
+ const key = conversationKey(meta.id);
288
+ if (out.has(key)) out.set(key, meta);
289
+ }
248
290
  } catch {
249
- return /* @__PURE__ */ new Map();
250
291
  }
292
+ return out;
251
293
  }
252
- function emitEpochAdvances(before) {
253
- if (!client) return;
294
+ function postConversationEvent(id, epoch, sender, meta) {
295
+ self.postMessage({
296
+ kind: "event.conversation",
297
+ id,
298
+ epoch,
299
+ ...sender !== void 0 ? { sender } : {},
300
+ ...meta !== null ? { meta } : {}
301
+ });
302
+ }
303
+ function postCacheUpkeep(held, dropped) {
304
+ if (held.length === 0 && dropped.length === 0) return;
305
+ self.postMessage({ kind: "cache.conversations", held, dropped });
306
+ }
307
+ function refreshCached(ids) {
308
+ if (ids.length === 0) return;
309
+ const metas = readMetas(ids);
310
+ const held = [];
311
+ const dropped = [];
312
+ const done = /* @__PURE__ */ new Set();
313
+ for (const id of ids) {
314
+ const key = conversationKey(id);
315
+ if (done.has(key)) continue;
316
+ done.add(key);
317
+ const meta = metas.get(key) ?? null;
318
+ if (meta !== null) held.push(meta);
319
+ else dropped.push(id);
320
+ }
321
+ postCacheUpkeep(held, dropped);
322
+ }
323
+ function watchFor(msg) {
324
+ const held = client;
325
+ if (!held) return null;
326
+ const announce = EPOCH_ADVANCING_REQUESTS.has(msg.kind);
327
+ if (!announce && !EPOCH_UPKEEP_REQUESTS.has(msg.kind)) return null;
328
+ const list = () => held.listConversations();
329
+ try {
330
+ return { before: epochMap(list()), after: list, announce };
331
+ } catch {
332
+ return null;
333
+ }
334
+ }
335
+ function emitEpochAdvances(watch) {
336
+ const told = /* @__PURE__ */ new Set();
254
337
  let metas;
255
338
  try {
256
- metas = client.listConversations();
339
+ metas = watch.after();
257
340
  } catch {
258
- return;
341
+ return told;
259
342
  }
260
- for (const meta of advancedEpochs(before, metas)) {
261
- self.postMessage({
262
- kind: "event.conversation",
263
- id: meta.id,
264
- epoch: meta.epoch
265
- });
343
+ const advanced = advancedEpochs(watch.before, metas);
344
+ for (const meta of advanced) {
345
+ told.add(conversationKey(meta.id));
346
+ if (watch.announce) postConversationEvent(meta.id, meta.epoch, void 0, meta);
266
347
  }
348
+ const { appeared, disappeared } = changedHoldings(watch.before, metas);
349
+ for (const meta of appeared) told.add(conversationKey(meta.id));
350
+ for (const id of disappeared) told.add(conversationKey(id));
351
+ postCacheUpkeep(watch.announce ? appeared : [...advanced, ...appeared], disappeared);
352
+ return told;
353
+ }
354
+ function sendLeftCopyBehind(msg, outcome, calls) {
355
+ if (!outcome.ok || calls !== 1) return true;
356
+ const sentAt = outcome.value?.epoch;
357
+ return typeof sentAt !== "number" || sentAt !== msg.heldEpoch;
267
358
  }
268
- function announceUpdated(updated, senders = /* @__PURE__ */ new Map()) {
359
+ function announceUpdated(updated, senders = /* @__PURE__ */ new Map(), removed = []) {
360
+ if (updated.length === 0 && removed.length === 0) return;
361
+ const metas = readMetas([
362
+ ...updated.map((u) => u.conversation_id),
363
+ ...removed.map((r) => r.conversation_id)
364
+ ]);
365
+ const told = /* @__PURE__ */ new Set();
269
366
  for (const u of updated) {
270
- const sender = senders.get(conversationKey(u.conversation_id));
271
- self.postMessage({
272
- kind: "event.conversation",
273
- id: u.conversation_id,
274
- epoch: u.epoch,
275
- ...sender !== void 0 ? { sender } : {}
276
- });
367
+ const key = conversationKey(u.conversation_id);
368
+ told.add(key);
369
+ postConversationEvent(u.conversation_id, u.epoch, senders.get(key), metas.get(key) ?? null);
370
+ }
371
+ const held = [];
372
+ const dropped = [];
373
+ for (const r of removed) {
374
+ const key = conversationKey(r.conversation_id);
375
+ if (told.has(key)) continue;
376
+ told.add(key);
377
+ const meta = metas.get(key) ?? null;
378
+ if (meta !== null) held.push(meta);
379
+ else dropped.push(r.conversation_id);
277
380
  }
381
+ postCacheUpkeep(held, dropped);
278
382
  }
279
383
  function welcomeSenders(items, joined) {
280
384
  const out = /* @__PURE__ */ new Map();
@@ -287,15 +391,71 @@ function welcomeSenders(items, joined) {
287
391
  }
288
392
  return out;
289
393
  }
394
+ var CONVERSATION_REQUESTS = /* @__PURE__ */ new Set([
395
+ "addMembers",
396
+ "addMembersWithVerdicts",
397
+ "reAdmitDevice",
398
+ "removeMembers",
399
+ "removeOwnOtherLeaves",
400
+ "removeMembersByIdentity",
401
+ "commitPendingProposals",
402
+ "setConversationName",
403
+ "setConversationRoles",
404
+ "syncConversation",
405
+ "leaveConversation",
406
+ "dropConversationLocal"
407
+ ]);
408
+ var CONVERSATION_RESULT_REQUESTS = /* @__PURE__ */ new Set([
409
+ "createConversation",
410
+ "createCallConversation",
411
+ "importStateSnapshot"
412
+ ]);
413
+ function conversationTouched(msg, value) {
414
+ if (CONVERSATION_REQUESTS.has(msg.kind)) {
415
+ const conv = msg.conv;
416
+ return conv instanceof Uint8Array ? conv : null;
417
+ }
418
+ if (CONVERSATION_RESULT_REQUESTS.has(msg.kind)) {
419
+ return value instanceof Uint8Array ? value : null;
420
+ }
421
+ return null;
422
+ }
423
+ function conversationsOfFailedApply(msg) {
424
+ switch (msg.kind) {
425
+ case "processEnvelope":
426
+ return [msg.envelope.conversation_id];
427
+ case "joinConversation":
428
+ case "joinConversationAt":
429
+ case "processWelcomeAt":
430
+ return [msg.welcome.conversation_id];
431
+ case "syncConversationStep":
432
+ return [msg.conv];
433
+ case "processSequencedBatch":
434
+ return msg.items.map((item) => item.envelope.conversation_id);
435
+ default:
436
+ return [];
437
+ }
438
+ }
439
+ function reportConversations(msg, watch, outcome, calls) {
440
+ try {
441
+ const told = watch ? emitEpochAdvances(watch) : /* @__PURE__ */ new Set();
442
+ const touched = conversationTouched(msg, outcome.ok ? outcome.value : void 0);
443
+ const owed = outcome.ok ? [] : conversationsOfFailedApply(msg);
444
+ if (touched !== null) owed.push(touched);
445
+ if (msg.kind === "send" && sendLeftCopyBehind(msg, outcome, calls)) owed.push(msg.conv);
446
+ refreshCached(owed.filter((id) => id instanceof Uint8Array && !told.has(conversationKey(id))));
447
+ } catch {
448
+ }
449
+ }
290
450
  async function runRequest(msg) {
291
- const watchEpochs = client !== null && EPOCH_ADVANCING_REQUESTS.has(msg.kind);
292
- const before = watchEpochs ? epochSnapshot() : null;
451
+ const watch = watchFor(msg);
452
+ const callsBefore = transportCalls;
293
453
  try {
294
454
  const value = await dispatch(msg);
295
- if (before) emitEpochAdvances(before);
455
+ reportConversations(msg, watch, { ok: true, value }, transportCalls - callsBefore);
296
456
  self.postMessage({ kind: "result", id: msg.id, ok: true, value });
297
457
  } catch (e) {
298
- if (before) emitEpochAdvances(before);
458
+ reportConversations(msg, watch, { ok: false }, transportCalls - callsBefore);
299
459
  self.postMessage({
300
460
  kind: "result",
301
461
  id: msg.id,
@@ -356,9 +516,16 @@ function catchUpPassStepper(msg) {
356
516
  listConversationIds: () => ensureClient().listConversations().map((meta) => meta.id),
357
517
  fetchHeads: async () => transportFeatures.fetchLogHeads ? await fetchLogHeadsProxy() ?? null : null,
358
518
  conversationsBehind: (heads) => ensureClient().conversationsBehind(heads),
359
- step: (id) => ensureClient().syncConversationStep(id, msg.nowMs),
519
+ step: async (id) => {
520
+ try {
521
+ return await ensureClient().syncConversationStep(id, msg.nowMs);
522
+ } catch (e) {
523
+ refreshCached([id]);
524
+ throw e;
525
+ }
526
+ },
360
527
  onPage: (page) => {
361
- announceUpdated(page.updated);
528
+ announceUpdated(page.updated, void 0, page.removed);
362
529
  if (page.messages.length > 0) {
363
530
  post({ kind: "event.batch", messages: page.messages, source: "page" });
364
531
  }
@@ -443,16 +610,13 @@ async function dispatch(msg) {
443
610
  return await ensureClient().createCallConversation(msg.name ?? void 0, msg.nowMs);
444
611
  case "joinConversation": {
445
612
  const id = await ensureClient().joinConversation(msg.welcome, msg.nowMs);
446
- self.postMessage({
447
- kind: "event.conversation",
448
- id,
449
- epoch: 0,
450
- sender: msg.welcome.sender_device
451
- });
613
+ postConversationEvent(id, 0, msg.welcome.sender_device, readMeta(id));
452
614
  return id;
453
615
  }
454
616
  case "listConversations":
455
617
  return ensureClient().listConversations();
618
+ case "conversationMeta":
619
+ return ensureClient().conversationMeta(msg.conv) ?? null;
456
620
  case "strandedConversations":
457
621
  return ensureClient().strandedConversations();
458
622
  case "conversationsLeaving":
@@ -520,11 +684,12 @@ async function dispatch(msg) {
520
684
  return await ensureClient().removeOwnDevicesExcept(msg.keep, msg.listed, msg.nowMs);
521
685
  case "processEnvelope": {
522
686
  const result = await ensureClient().processEnvelope(msg.envelope, msg.nowMs);
523
- self.postMessage({
524
- kind: "event.conversation",
525
- id: msg.envelope.conversation_id,
526
- epoch: msg.envelope.epoch
527
- });
687
+ postConversationEvent(
688
+ msg.envelope.conversation_id,
689
+ msg.envelope.epoch,
690
+ void 0,
691
+ readMeta(msg.envelope.conversation_id)
692
+ );
528
693
  return result;
529
694
  }
530
695
  case "pendingApplies":
@@ -541,12 +706,12 @@ async function dispatch(msg) {
541
706
  // the core's exact `updated` list, before the result resolves.
542
707
  case "processSequencedBatch": {
543
708
  const raw = await ensureClient().processSequencedBatch(msg.items, msg.nowMs);
544
- announceUpdated(raw.updated, welcomeSenders(msg.items, raw.joined));
709
+ announceUpdated(raw.updated, welcomeSenders(msg.items, raw.joined), raw.removed);
545
710
  return raw;
546
711
  }
547
712
  case "syncConversationStep": {
548
713
  const raw = await ensureClient().syncConversationStep(msg.conv, msg.nowMs);
549
- announceUpdated(raw.updated);
714
+ announceUpdated(raw.updated, void 0, raw.removed);
550
715
  return raw;
551
716
  }
552
717
  case "logPositions":
@@ -561,23 +726,13 @@ async function dispatch(msg) {
561
726
  return ensureClient().heldFrameCount();
562
727
  case "joinConversationAt": {
563
728
  const id = await ensureClient().joinConversationAt(msg.welcome, msg.position, msg.nowMs);
564
- self.postMessage({
565
- kind: "event.conversation",
566
- id,
567
- epoch: 0,
568
- sender: msg.welcome.sender_device
569
- });
729
+ postConversationEvent(id, 0, msg.welcome.sender_device, readMeta(id));
570
730
  return id;
571
731
  }
572
732
  case "processWelcomeAt": {
573
733
  const id = await ensureClient().processWelcomeAt(msg.welcome, msg.position, msg.nowMs);
574
734
  if (id !== null && id !== void 0) {
575
- self.postMessage({
576
- kind: "event.conversation",
577
- id,
578
- epoch: 0,
579
- sender: msg.welcome.sender_device
580
- });
735
+ postConversationEvent(id, 0, msg.welcome.sender_device, readMeta(id));
581
736
  return id;
582
737
  }
583
738
  return null;