@ping-app/ping-openmls-sdk 0.9.2 → 0.10.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.
package/dist/worker.js CHANGED
@@ -68,6 +68,25 @@ function advancedEpochs(before, after) {
68
68
  }
69
69
  return out;
70
70
  }
71
+ function conversationIdOf(key) {
72
+ const out = new Uint8Array(key.length >> 1);
73
+ for (let i = 0; i < out.length; i += 1) out[i] = Number.parseInt(key.slice(i * 2, i * 2 + 2), 16);
74
+ return out;
75
+ }
76
+ function changedHoldings(before, after) {
77
+ const appeared = [];
78
+ const still = /* @__PURE__ */ new Set();
79
+ for (const meta of after) {
80
+ const key = conversationKey(meta.id);
81
+ still.add(key);
82
+ if (!before.has(key)) appeared.push(meta);
83
+ }
84
+ const disappeared = [];
85
+ for (const key of before.keys()) {
86
+ if (!still.has(key)) disappeared.push(conversationIdOf(key));
87
+ }
88
+ return { appeared, disappeared };
89
+ }
71
90
 
72
91
  // src/catch-up-pass.ts
73
92
  function catchUpPass(deps) {
@@ -172,6 +191,7 @@ var pendingStorage = /* @__PURE__ */ new Map();
172
191
  var pendingTransport = /* @__PURE__ */ new Map();
173
192
  var nextStorageId = 1;
174
193
  var nextTransportId = 1;
194
+ var transportCalls = 0;
175
195
  var proxyStorage = {
176
196
  get: (namespace, key) => callMain("storage", "get", [namespace, key]),
177
197
  put: (namespace, key, value) => callMain("storage", "put", [namespace, key, value]),
@@ -216,6 +236,7 @@ function callMain(kind, method, args) {
216
236
  return new Promise((resolve, reject) => {
217
237
  const id = kind === "storage" ? nextStorageId++ : nextTransportId++;
218
238
  const map = kind === "storage" ? pendingStorage : pendingTransport;
239
+ if (kind === "transport") transportCalls += 1;
219
240
  map.set(id, ({ ok, value }) => ok ? resolve(value) : reject(new Error(String(value))));
220
241
  self.postMessage({ kind: `${kind}.call`, id, method, args });
221
242
  });
@@ -248,40 +269,123 @@ var EPOCH_ADVANCING_REQUESTS = /* @__PURE__ */ new Set([
248
269
  "consumeLinkingTicket",
249
270
  "importStateSnapshot"
250
271
  ]);
251
- function epochSnapshot() {
252
- if (!client) return /* @__PURE__ */ new Map();
272
+ var EPOCH_UPKEEP_REQUESTS = /* @__PURE__ */ new Set([
273
+ "buildLinkingTicket"
274
+ ]);
275
+ function readMeta(id) {
276
+ if (!client) return null;
277
+ try {
278
+ return client.conversationMeta(id) ?? null;
279
+ } catch {
280
+ return null;
281
+ }
282
+ }
283
+ var SINGLE_READS_MAX = 8;
284
+ function readMetas(ids) {
285
+ const out = /* @__PURE__ */ new Map();
286
+ for (const id of ids) out.set(conversationKey(id), null);
287
+ if (out.size <= SINGLE_READS_MAX) {
288
+ for (const id of ids) out.set(conversationKey(id), readMeta(id));
289
+ return out;
290
+ }
291
+ if (!client) return out;
292
+ try {
293
+ for (const meta of client.listConversations()) {
294
+ const key = conversationKey(meta.id);
295
+ if (out.has(key)) out.set(key, meta);
296
+ }
297
+ } catch {
298
+ }
299
+ return out;
300
+ }
301
+ function postConversationEvent(id, epoch, sender, meta) {
302
+ self.postMessage({
303
+ kind: "event.conversation",
304
+ id,
305
+ epoch,
306
+ ...sender !== void 0 ? { sender } : {},
307
+ ...meta !== null ? { meta } : {}
308
+ });
309
+ }
310
+ function postCacheUpkeep(held, dropped) {
311
+ if (held.length === 0 && dropped.length === 0) return;
312
+ self.postMessage({ kind: "cache.conversations", held, dropped });
313
+ }
314
+ function refreshCached(ids) {
315
+ if (ids.length === 0) return;
316
+ const metas = readMetas(ids);
317
+ const held = [];
318
+ const dropped = [];
319
+ const done = /* @__PURE__ */ new Set();
320
+ for (const id of ids) {
321
+ const key = conversationKey(id);
322
+ if (done.has(key)) continue;
323
+ done.add(key);
324
+ const meta = metas.get(key) ?? null;
325
+ if (meta !== null) held.push(meta);
326
+ else dropped.push(id);
327
+ }
328
+ postCacheUpkeep(held, dropped);
329
+ }
330
+ function watchFor(msg) {
331
+ const held = client;
332
+ if (!held) return null;
333
+ const announce = EPOCH_ADVANCING_REQUESTS.has(msg.kind);
334
+ if (!announce && !EPOCH_UPKEEP_REQUESTS.has(msg.kind)) return null;
335
+ const list = () => held.listConversations();
253
336
  try {
254
- return epochMap(client.listConversations());
337
+ return { before: epochMap(list()), after: list, announce };
255
338
  } catch {
256
- return /* @__PURE__ */ new Map();
339
+ return null;
257
340
  }
258
341
  }
259
- function emitEpochAdvances(before) {
260
- if (!client) return;
342
+ function emitEpochAdvances(watch) {
343
+ const told = /* @__PURE__ */ new Set();
261
344
  let metas;
262
345
  try {
263
- metas = client.listConversations();
346
+ metas = watch.after();
264
347
  } catch {
265
- return;
348
+ return told;
266
349
  }
267
- for (const meta of advancedEpochs(before, metas)) {
268
- self.postMessage({
269
- kind: "event.conversation",
270
- id: meta.id,
271
- epoch: meta.epoch
272
- });
350
+ const advanced = advancedEpochs(watch.before, metas);
351
+ for (const meta of advanced) {
352
+ told.add(conversationKey(meta.id));
353
+ if (watch.announce) postConversationEvent(meta.id, meta.epoch, void 0, meta);
273
354
  }
355
+ const { appeared, disappeared } = changedHoldings(watch.before, metas);
356
+ for (const meta of appeared) told.add(conversationKey(meta.id));
357
+ for (const id of disappeared) told.add(conversationKey(id));
358
+ postCacheUpkeep(watch.announce ? appeared : [...advanced, ...appeared], disappeared);
359
+ return told;
360
+ }
361
+ function sendLeftCopyBehind(msg, outcome, calls) {
362
+ if (!outcome.ok || calls !== 1) return true;
363
+ const sentAt = outcome.value?.epoch;
364
+ return typeof sentAt !== "number" || sentAt !== msg.heldEpoch;
274
365
  }
275
- function announceUpdated(updated, senders = /* @__PURE__ */ new Map()) {
366
+ function announceUpdated(updated, senders = /* @__PURE__ */ new Map(), removed = []) {
367
+ if (updated.length === 0 && removed.length === 0) return;
368
+ const metas = readMetas([
369
+ ...updated.map((u) => u.conversation_id),
370
+ ...removed.map((r) => r.conversation_id)
371
+ ]);
372
+ const told = /* @__PURE__ */ new Set();
276
373
  for (const u of updated) {
277
- const sender = senders.get(conversationKey(u.conversation_id));
278
- self.postMessage({
279
- kind: "event.conversation",
280
- id: u.conversation_id,
281
- epoch: u.epoch,
282
- ...sender !== void 0 ? { sender } : {}
283
- });
374
+ const key = conversationKey(u.conversation_id);
375
+ told.add(key);
376
+ postConversationEvent(u.conversation_id, u.epoch, senders.get(key), metas.get(key) ?? null);
284
377
  }
378
+ const held = [];
379
+ const dropped = [];
380
+ for (const r of removed) {
381
+ const key = conversationKey(r.conversation_id);
382
+ if (told.has(key)) continue;
383
+ told.add(key);
384
+ const meta = metas.get(key) ?? null;
385
+ if (meta !== null) held.push(meta);
386
+ else dropped.push(r.conversation_id);
387
+ }
388
+ postCacheUpkeep(held, dropped);
285
389
  }
286
390
  function welcomeSenders(items, joined) {
287
391
  const out = /* @__PURE__ */ new Map();
@@ -294,15 +398,71 @@ function welcomeSenders(items, joined) {
294
398
  }
295
399
  return out;
296
400
  }
401
+ var CONVERSATION_REQUESTS = /* @__PURE__ */ new Set([
402
+ "addMembers",
403
+ "addMembersWithVerdicts",
404
+ "reAdmitDevice",
405
+ "removeMembers",
406
+ "removeOwnOtherLeaves",
407
+ "removeMembersByIdentity",
408
+ "commitPendingProposals",
409
+ "setConversationName",
410
+ "setConversationRoles",
411
+ "syncConversation",
412
+ "leaveConversation",
413
+ "dropConversationLocal"
414
+ ]);
415
+ var CONVERSATION_RESULT_REQUESTS = /* @__PURE__ */ new Set([
416
+ "createConversation",
417
+ "createCallConversation",
418
+ "importStateSnapshot"
419
+ ]);
420
+ function conversationTouched(msg, value) {
421
+ if (CONVERSATION_REQUESTS.has(msg.kind)) {
422
+ const conv = msg.conv;
423
+ return conv instanceof Uint8Array ? conv : null;
424
+ }
425
+ if (CONVERSATION_RESULT_REQUESTS.has(msg.kind)) {
426
+ return value instanceof Uint8Array ? value : null;
427
+ }
428
+ return null;
429
+ }
430
+ function conversationsOfFailedApply(msg) {
431
+ switch (msg.kind) {
432
+ case "processEnvelope":
433
+ return [msg.envelope.conversation_id];
434
+ case "joinConversation":
435
+ case "joinConversationAt":
436
+ case "processWelcomeAt":
437
+ return [msg.welcome.conversation_id];
438
+ case "syncConversationStep":
439
+ return [msg.conv];
440
+ case "processSequencedBatch":
441
+ return msg.items.map((item) => item.envelope.conversation_id);
442
+ default:
443
+ return [];
444
+ }
445
+ }
446
+ function reportConversations(msg, watch, outcome, calls) {
447
+ try {
448
+ const told = watch ? emitEpochAdvances(watch) : /* @__PURE__ */ new Set();
449
+ const touched = conversationTouched(msg, outcome.ok ? outcome.value : void 0);
450
+ const owed = outcome.ok ? [] : conversationsOfFailedApply(msg);
451
+ if (touched !== null) owed.push(touched);
452
+ if (msg.kind === "send" && sendLeftCopyBehind(msg, outcome, calls)) owed.push(msg.conv);
453
+ refreshCached(owed.filter((id) => id instanceof Uint8Array && !told.has(conversationKey(id))));
454
+ } catch {
455
+ }
456
+ }
297
457
  async function runRequest(msg) {
298
- const watchEpochs = client !== null && EPOCH_ADVANCING_REQUESTS.has(msg.kind);
299
- const before = watchEpochs ? epochSnapshot() : null;
458
+ const watch = watchFor(msg);
459
+ const callsBefore = transportCalls;
300
460
  try {
301
461
  const value = await dispatch(msg);
302
- if (before) emitEpochAdvances(before);
462
+ reportConversations(msg, watch, { ok: true, value }, transportCalls - callsBefore);
303
463
  self.postMessage({ kind: "result", id: msg.id, ok: true, value });
304
464
  } catch (e) {
305
- if (before) emitEpochAdvances(before);
465
+ reportConversations(msg, watch, { ok: false }, transportCalls - callsBefore);
306
466
  self.postMessage({
307
467
  kind: "result",
308
468
  id: msg.id,
@@ -363,9 +523,16 @@ function catchUpPassStepper(msg) {
363
523
  listConversationIds: () => ensureClient().listConversations().map((meta) => meta.id),
364
524
  fetchHeads: async () => transportFeatures.fetchLogHeads ? await fetchLogHeadsProxy() ?? null : null,
365
525
  conversationsBehind: (heads) => ensureClient().conversationsBehind(heads),
366
- step: (id) => ensureClient().syncConversationStep(id, msg.nowMs),
526
+ step: async (id) => {
527
+ try {
528
+ return await ensureClient().syncConversationStep(id, msg.nowMs);
529
+ } catch (e) {
530
+ refreshCached([id]);
531
+ throw e;
532
+ }
533
+ },
367
534
  onPage: (page) => {
368
- announceUpdated(page.updated);
535
+ announceUpdated(page.updated, void 0, page.removed);
369
536
  if (page.messages.length > 0) {
370
537
  post({ kind: "event.batch", messages: page.messages, source: "page" });
371
538
  }
@@ -424,6 +591,12 @@ async function dispatch(msg) {
424
591
  }
425
592
  case "previewEnvelope":
426
593
  return ensureClient().previewEnvelope(msg.envelope, msg.nowMs);
594
+ case "previewEnvelopeWithPrerequisites":
595
+ return ensureClient().previewEnvelopeWithPrerequisites(
596
+ msg.prerequisites,
597
+ msg.envelope,
598
+ msg.nowMs
599
+ );
427
600
  case "generateIdentity":
428
601
  return PingClient.generateIdentity();
429
602
  case "userId":
@@ -450,16 +623,13 @@ async function dispatch(msg) {
450
623
  return await ensureClient().createCallConversation(msg.name ?? void 0, msg.nowMs);
451
624
  case "joinConversation": {
452
625
  const id = await ensureClient().joinConversation(msg.welcome, msg.nowMs);
453
- self.postMessage({
454
- kind: "event.conversation",
455
- id,
456
- epoch: 0,
457
- sender: msg.welcome.sender_device
458
- });
626
+ postConversationEvent(id, 0, msg.welcome.sender_device, readMeta(id));
459
627
  return id;
460
628
  }
461
629
  case "listConversations":
462
630
  return ensureClient().listConversations();
631
+ case "conversationMeta":
632
+ return ensureClient().conversationMeta(msg.conv) ?? null;
463
633
  case "strandedConversations":
464
634
  return ensureClient().strandedConversations();
465
635
  case "conversationsLeaving":
@@ -527,11 +697,12 @@ async function dispatch(msg) {
527
697
  return await ensureClient().removeOwnDevicesExcept(msg.keep, msg.listed, msg.nowMs);
528
698
  case "processEnvelope": {
529
699
  const result = await ensureClient().processEnvelope(msg.envelope, msg.nowMs);
530
- self.postMessage({
531
- kind: "event.conversation",
532
- id: msg.envelope.conversation_id,
533
- epoch: msg.envelope.epoch
534
- });
700
+ postConversationEvent(
701
+ msg.envelope.conversation_id,
702
+ msg.envelope.epoch,
703
+ void 0,
704
+ readMeta(msg.envelope.conversation_id)
705
+ );
535
706
  return result;
536
707
  }
537
708
  case "pendingApplies":
@@ -548,12 +719,12 @@ async function dispatch(msg) {
548
719
  // the core's exact `updated` list, before the result resolves.
549
720
  case "processSequencedBatch": {
550
721
  const raw = await ensureClient().processSequencedBatch(msg.items, msg.nowMs);
551
- announceUpdated(raw.updated, welcomeSenders(msg.items, raw.joined));
722
+ announceUpdated(raw.updated, welcomeSenders(msg.items, raw.joined), raw.removed);
552
723
  return raw;
553
724
  }
554
725
  case "syncConversationStep": {
555
726
  const raw = await ensureClient().syncConversationStep(msg.conv, msg.nowMs);
556
- announceUpdated(raw.updated);
727
+ announceUpdated(raw.updated, void 0, raw.removed);
557
728
  return raw;
558
729
  }
559
730
  case "logPositions":
@@ -568,23 +739,13 @@ async function dispatch(msg) {
568
739
  return ensureClient().heldFrameCount();
569
740
  case "joinConversationAt": {
570
741
  const id = await ensureClient().joinConversationAt(msg.welcome, msg.position, msg.nowMs);
571
- self.postMessage({
572
- kind: "event.conversation",
573
- id,
574
- epoch: 0,
575
- sender: msg.welcome.sender_device
576
- });
742
+ postConversationEvent(id, 0, msg.welcome.sender_device, readMeta(id));
577
743
  return id;
578
744
  }
579
745
  case "processWelcomeAt": {
580
746
  const id = await ensureClient().processWelcomeAt(msg.welcome, msg.position, msg.nowMs);
581
747
  if (id !== null && id !== void 0) {
582
- self.postMessage({
583
- kind: "event.conversation",
584
- id,
585
- epoch: 0,
586
- sender: msg.welcome.sender_device
587
- });
748
+ postConversationEvent(id, 0, msg.welcome.sender_device, readMeta(id));
588
749
  return id;
589
750
  }
590
751
  return null;