@kumiai/rpc 0.10.3 → 0.10.4

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.
@@ -37,6 +37,8 @@ export type EpochRuntime = {
37
37
  teardownEpoch: () => Promise<void>;
38
38
  /** Teardown then build; a teardown failure still builds, then rethrows. */
39
39
  rebuildEpoch: () => Promise<void>;
40
+ /** Resolves once no rebuild is in flight; never rejects. */
41
+ epochSettled: () => Promise<void>;
40
42
  surfaceFor: (name: string) => InternalSurface;
41
43
  sealForSegment: (name: string, bytes: Uint8Array, intent?: 'ephemeral' | 'log') => Promise<SegmentFrame>;
42
44
  hasInboxLane: () => boolean;
package/lib/peer-epoch.js CHANGED
@@ -346,7 +346,17 @@ import { inboxTopic, protocolTopic } from './topic.js';
346
346
  }
347
347
  };
348
348
  };
349
- const rebuildEpoch = async ()=>{
349
+ // Teardown empties the runtimes before build fills them, so `surfaceFor` must not run between.
350
+ const rebuilding = new Set();
351
+ const epochSettled = async ()=>{
352
+ while(rebuilding.size > 0)await Promise.allSettled(rebuilding);
353
+ };
354
+ const rebuildEpoch = ()=>{
355
+ const run = rebuildOnce().finally(()=>rebuilding.delete(run));
356
+ rebuilding.add(run);
357
+ return run;
358
+ };
359
+ const rebuildOnce = async ()=>{
350
360
  if (isDisposed()) return;
351
361
  // Teardown empties the runtimes before it reports a failed child, and a later walk sees the
352
362
  // advance as history. Build anyway, then report the failure.
@@ -366,6 +376,7 @@ import { inboxTopic, protocolTopic } from './topic.js';
366
376
  buildEpoch,
367
377
  teardownEpoch,
368
378
  rebuildEpoch,
379
+ epochSettled,
369
380
  surfaceFor,
370
381
  sealForSegment,
371
382
  hasInboxLane: ()=>inboxLane != null,
package/lib/peer.js CHANGED
@@ -387,7 +387,7 @@ export function createGroupPeer(input) {
387
387
  note: (name, topicID, message, failure)=>appLane.note(name, topicID, message, failure),
388
388
  requestAppPull
389
389
  });
390
- const { buildEpoch, teardownEpoch, rebuildEpoch, surfaceFor, sealForSegment } = epochRuntime;
390
+ const { buildEpoch, teardownEpoch, rebuildEpoch, epochSettled, surfaceFor, sealForSegment } = epochRuntime;
391
391
  const appOutboxAcceptance = mls == null ? undefined : createAppOutboxAcceptance({
392
392
  outbox: params.appOutbox,
393
393
  limit: params.appOutboxLimit,
@@ -2556,12 +2556,20 @@ export function createGroupPeer(input) {
2556
2556
  assertLive();
2557
2557
  return fn();
2558
2558
  };
2559
- const readyOrAbort = async (signal)=>{
2559
+ // Ready, and past any epoch rebuild in flight: the protocol surfaces are torn down mid-rebuild.
2560
+ const withEpoch = (fn)=>withReady(async ()=>{
2561
+ await epochSettled();
2562
+ assertLive();
2563
+ return fn();
2564
+ });
2565
+ /** {@link withEpoch}'s wait, abandoned when `signal` aborts. */ const epochOrAbort = async (signal)=>{
2560
2566
  if (signal == null) {
2561
2567
  await ready;
2568
+ await epochSettled();
2562
2569
  return 'ready';
2563
2570
  }
2564
2571
  if (signal.aborted) return 'aborted';
2572
+ const live = ready.then(()=>epochSettled());
2565
2573
  let onAbort;
2566
2574
  const aborted = new Promise((resolve)=>{
2567
2575
  onAbort = ()=>resolve('aborted');
@@ -2571,7 +2579,7 @@ export function createGroupPeer(input) {
2571
2579
  });
2572
2580
  try {
2573
2581
  return await Promise.race([
2574
- ready.then(()=>'ready'),
2582
+ live.then(()=>'ready'),
2575
2583
  aborted
2576
2584
  ]);
2577
2585
  } finally{
@@ -2599,16 +2607,16 @@ export function createGroupPeer(input) {
2599
2607
  logDelivery?.trigger();
2600
2608
  return;
2601
2609
  }
2602
- return withReady(()=>surfaceFor(key).dispatch(prc, config));
2610
+ return withEpoch(()=>surfaceFor(key).dispatch(prc, config));
2603
2611
  },
2604
- request: (prc, config)=>withReady(()=>surfaceFor(key).request(prc, config)),
2612
+ request: (prc, config)=>withEpoch(()=>surfaceFor(key).request(prc, config)),
2605
2613
  gather: async (prc, config)=>{
2606
- const outcome = await readyOrAbort(config?.signal);
2614
+ const outcome = await epochOrAbort(config?.signal);
2607
2615
  assertLive();
2608
2616
  if (outcome === 'aborted') return [];
2609
2617
  return surfaceFor(key).gather(prc, config);
2610
2618
  },
2611
- to: (memberDID)=>withReady(()=>surfaceFor(key).to(memberDID))
2619
+ to: (memberDID)=>withEpoch(()=>surfaceFor(key).to(memberDID))
2612
2620
  };
2613
2621
  };
2614
2622
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kumiai/rpc",
3
- "version": "0.10.3",
3
+ "version": "0.10.4",
4
4
  "description": "High-level MLS-aware group RPC for Enkaku: bus broadcast/anycast/gather plus directed RPC over the pub/sub hub",
5
5
  "keywords": [
6
6
  "rpc",