@kumiai/rpc 0.4.3 → 0.6.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/README.md +10 -0
- package/lib/app-lane.d.ts +1 -1
- package/lib/app-lane.js +17 -7
- package/lib/classify.d.ts +71 -15
- package/lib/classify.js +38 -10
- package/lib/crypto.d.ts +23 -0
- package/lib/directed.d.ts +3 -0
- package/lib/directed.js +4 -1
- package/lib/handlers.d.ts +12 -7
- package/lib/handlers.js +39 -11
- package/lib/hub-mux.d.ts +30 -36
- package/lib/hub-mux.js +163 -101
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/open-once.d.ts +20 -25
- package/lib/open-once.js +25 -28
- package/lib/peer.d.ts +1 -1
- package/lib/peer.js +141 -35
- package/package.json +19 -14
- package/lib/bus-server.d.ts +0 -21
- package/lib/bus-server.js +0 -81
package/lib/peer.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { BroadcastClient, createBroadcastTransport, defaultJitter, encodeEventFrame } from '@kumiai/broadcast';
|
|
1
|
+
import { BroadcastClient, createBroadcastResponder, createBroadcastTransport, defaultJitter, encodeEventFrame } from '@kumiai/broadcast';
|
|
2
2
|
import { createRuntime } from '@sozai/runtime';
|
|
3
3
|
import { createAppLane } from './app-lane.js';
|
|
4
|
-
import {
|
|
5
|
-
import { classifyCommit, UNKNOWN_FRAME_VERSION } from './classify.js';
|
|
4
|
+
import { classifyCommit, digestAppliedCommit, UNKNOWN_FRAME_VERSION } from './classify.js';
|
|
6
5
|
import { CommitDeadlineError, isHeadMismatch, JournalEpochError, RecoveryRequiredError } from './commit.js';
|
|
7
6
|
import { decodeCommitFrame, encodeCommitFrame, isUnsupportedCommitFrameVersion } from './commit-frame.js';
|
|
8
7
|
import { isMissingLedgerEntries } from './crypto.js';
|
|
@@ -93,7 +92,7 @@ export function createGroupPeer(params) {
|
|
|
93
92
|
* down and not yet rebuilt).
|
|
94
93
|
*/ const appEventHandlers = new Map();
|
|
95
94
|
for (const [name, protocol] of Object.entries(protocols)){
|
|
96
|
-
appEventHandlers.set(name, adaptBusHandlers(protocol, handlers[name], suppress).
|
|
95
|
+
appEventHandlers.set(name, adaptBusHandlers(protocol, handlers[name], suppress).events);
|
|
97
96
|
}
|
|
98
97
|
/**
|
|
99
98
|
* The live epoch this peer frames commits at, seeded from the handle, not zero. Not the app
|
|
@@ -178,6 +177,17 @@ export function createGroupPeer(params) {
|
|
|
178
177
|
* already made — a frame that breaks it is refused rather than fanned out.
|
|
179
178
|
*/ const openedFrames = new WeakMap();
|
|
180
179
|
/**
|
|
180
|
+
* Whether a frame `unwrap` just refused might still open once this handle catches up — read from
|
|
181
|
+
* its cleartext epoch, never the throw (which can't tell "not reached yet" from "never again").
|
|
182
|
+
* On the open-once failure path ({@link "open-once".OpenOncePathParams.retainOnFailure}) answering
|
|
183
|
+
* `true` withholds the ack so the frame survives a reconnect. Mailbox-class frames have no staging
|
|
184
|
+
* of their own (unlike `app-lane.ts`'s `note`/`ahead`), which is what made acking a transient
|
|
185
|
+
* refusal here a permanent loss.
|
|
186
|
+
*/ const retainOnFailure = (message)=>{
|
|
187
|
+
const at = crypto.frameEpoch(message.payload);
|
|
188
|
+
return at != null && at > crypto.epoch();
|
|
189
|
+
};
|
|
190
|
+
/**
|
|
181
191
|
* The app lane's inbound path: one open per topic, fanned out as plaintext, with each frame's
|
|
182
192
|
* log position noted before the open. Every consumer's own `unwrap` is then a pure lookup of the
|
|
183
193
|
* opened result ({@link openedFrames}), and nothing downstream touches the handle.
|
|
@@ -188,6 +198,7 @@ export function createGroupPeer(params) {
|
|
|
188
198
|
mux,
|
|
189
199
|
topicID,
|
|
190
200
|
unwrap: crypto.unwrap,
|
|
201
|
+
retainOnFailure,
|
|
191
202
|
project: (_message, opened)=>{
|
|
192
203
|
const { payload, senderDID } = opened;
|
|
193
204
|
if (typeof senderDID !== 'string' || senderDID === '') {
|
|
@@ -253,7 +264,8 @@ export function createGroupPeer(params) {
|
|
|
253
264
|
path: createInboxPath({
|
|
254
265
|
mux,
|
|
255
266
|
topicID: selfInbox,
|
|
256
|
-
unwrap: crypto.unwrap
|
|
267
|
+
unwrap: crypto.unwrap,
|
|
268
|
+
retainOnFailure
|
|
257
269
|
})
|
|
258
270
|
};
|
|
259
271
|
for (const [name, protocol] of Object.entries(protocols)){
|
|
@@ -278,12 +290,12 @@ export function createGroupPeer(params) {
|
|
|
278
290
|
runtime: params.runtime
|
|
279
291
|
} : {}
|
|
280
292
|
});
|
|
281
|
-
const {
|
|
282
|
-
const busServer =
|
|
293
|
+
const { events, requestHandlers } = adaptBusHandlers(protocol, handlers[name], suppress);
|
|
294
|
+
const busServer = createBroadcastResponder({
|
|
283
295
|
transport: segmentBoundTransport(name, topicID, inbound),
|
|
284
296
|
from: localDID,
|
|
285
|
-
|
|
286
|
-
|
|
297
|
+
requestHandlers,
|
|
298
|
+
events
|
|
287
299
|
});
|
|
288
300
|
const acceptor = createInboxAcceptor({
|
|
289
301
|
mux,
|
|
@@ -371,7 +383,7 @@ export function createGroupPeer(params) {
|
|
|
371
383
|
},
|
|
372
384
|
request: (prc, prm, options)=>runtime.client.request(prc, prm, options),
|
|
373
385
|
gather: (prc, prm, options)=>runtime.client.gather(prc, prm, options),
|
|
374
|
-
to: (memberDID)=>{
|
|
386
|
+
to: async (memberDID)=>{
|
|
375
387
|
const cached = runtime.directed.get(memberDID);
|
|
376
388
|
if (cached != null) return cached.client;
|
|
377
389
|
const lane = inboxLane;
|
|
@@ -400,6 +412,29 @@ export function createGroupPeer(params) {
|
|
|
400
412
|
await teardownEpoch();
|
|
401
413
|
await buildEpoch();
|
|
402
414
|
};
|
|
415
|
+
/**
|
|
416
|
+
* Set as `dispose()`'s FIRST statement. The peer's post-dispose rule has two forms and this is
|
|
417
|
+
* the host-facing one: everything a HOST asks of a disposed peer is refused, loudly. The inbound
|
|
418
|
+
* side is refused silently, where a delivery has no caller to tell: the commit lane
|
|
419
|
+
* (`onCommitDelivery`), and the two rendezvous responders, whose reply timers can fire before
|
|
420
|
+
* dispose and would otherwise land their publish after it.
|
|
421
|
+
*
|
|
422
|
+
* The check belongs immediately after each entry point's `await ready`, because that await is
|
|
423
|
+
* where the race lives. `dispose()` awaits a promise DERIVED from `ready`, so a call queued
|
|
424
|
+
* before init settles always resumes one microtask EARLIER than dispose's own body — early
|
|
425
|
+
* enough to run to completion against runtimes a teardown is about to walk. Unguarded, each
|
|
426
|
+
* entry point fails differently and none of them says why: `to()` handed back a live-looking
|
|
427
|
+
* client over an already-aborted transport, `resync()` rebuilt a whole epoch onto a disposed
|
|
428
|
+
* mux, and `commit()` published to the hub from a peer that is gone. In the mirror ordering,
|
|
429
|
+
* where teardown got there first and emptied `runtimes`, a protocol call blamed
|
|
430
|
+
* `Unknown protocol` for a peer that no longer exists.
|
|
431
|
+
*
|
|
432
|
+
* The `resync` leak is LOCAL and invisible from the hub — do not re-derive it as re-subscribing.
|
|
433
|
+
* `resync`'s own comment carries the mechanism; read it there rather than restating it here.
|
|
434
|
+
*/ let disposed = false;
|
|
435
|
+
const assertLive = ()=>{
|
|
436
|
+
if (disposed) throw new Error('Peer is disposed');
|
|
437
|
+
};
|
|
403
438
|
let commitUnsubscribe;
|
|
404
439
|
let rendezvousUnsubscribe;
|
|
405
440
|
let commitTopicID;
|
|
@@ -418,10 +453,11 @@ export function createGroupPeer(params) {
|
|
|
418
453
|
* from the cursor. `null` means the topic never had an accepted log publish.
|
|
419
454
|
*/ let commitLogHead = null;
|
|
420
455
|
/**
|
|
421
|
-
* The
|
|
422
|
-
* and adopted
|
|
423
|
-
*
|
|
424
|
-
* different logs to different members.
|
|
456
|
+
* The commit this peer ENACTED at each epoch it passed — applied from the log, or committed
|
|
457
|
+
* and adopted — by digest, with the sequenceID the log carried it at. The whole of the fork
|
|
458
|
+
* check: a second, DIFFERENT commit at an epoch this peer holds a record for is two commits at
|
|
459
|
+
* one epoch, which the hub can only produce by showing different logs to different members. The
|
|
460
|
+
* same commit re-served at a new position is not, however the log came to carry it twice.
|
|
425
461
|
*
|
|
426
462
|
* An epoch with NO record is history, not a fork — a late joiner, rejoiner or re-seeded peer all
|
|
427
463
|
* walk commits from epochs they never held. In memory, deliberately: a restart drops the record,
|
|
@@ -527,6 +563,10 @@ export function createGroupPeer(params) {
|
|
|
527
563
|
// The port verifies the request and checks the requester's leaf against its own current
|
|
528
564
|
// tree. A refused request raises, and this peer stays silent.
|
|
529
565
|
const groupInfo = await port.sealGroupInfo(request.request);
|
|
566
|
+
// Same window as `handleLedgerRequest`'s guard: a timer that fired before dispose is
|
|
567
|
+
// gone from `pendingReplies` by the time the clear sweep runs, and is then an await
|
|
568
|
+
// away from here. Silent, for the same reason.
|
|
569
|
+
if (disposed) return;
|
|
530
570
|
// Mailbox class, deliberately: a rendezvous frame must never move the commit topic's
|
|
531
571
|
// head, and its reader — the requester — subscribed before it asked.
|
|
532
572
|
await mux.publish({
|
|
@@ -592,6 +632,11 @@ export function createGroupPeer(params) {
|
|
|
592
632
|
// The port verifies the request and checks the requester's leaf against its own current
|
|
593
633
|
// tree. A refused request raises, and this peer stays silent.
|
|
594
634
|
const sealed = await port.sealLedger(request.request);
|
|
635
|
+
// This timer fired before dispose()'s clear sweep, so it had already deleted itself
|
|
636
|
+
// from `pendingLedgerReplies` when the sweep walked it — too late by construction, not
|
|
637
|
+
// by race. Silent, like `onCommitDelivery`: there is no caller to tell, and the catch
|
|
638
|
+
// below would swallow a throw anyway.
|
|
639
|
+
if (disposed) return;
|
|
595
640
|
await mux.publish({
|
|
596
641
|
topicID,
|
|
597
642
|
payload: encodeHandshakeFrame(HANDSHAKE_KIND.ledgerReply, encodeLedgerReply(request.requestID, sealed))
|
|
@@ -765,10 +810,17 @@ export function createGroupPeer(params) {
|
|
|
765
810
|
// the magic means what this build thinks under an unknown version. On the commit topic —
|
|
766
811
|
// and only here — that is evidence in itself, so it goes to the classifier, not dropped.
|
|
767
812
|
if (frame.version !== HANDSHAKE_VERSION) {
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
813
|
+
// No digest: the frame's version put its commit bytes out of reach entirely. Settled at
|
|
814
|
+
// `ahead` before the digest is read.
|
|
815
|
+
const unreadable = classifyCommit({
|
|
816
|
+
header: UNKNOWN_FRAME_VERSION,
|
|
817
|
+
sequenceID: position,
|
|
818
|
+
commitDigest: null,
|
|
819
|
+
state: {
|
|
820
|
+
localDID,
|
|
821
|
+
epoch: crypto.epoch(),
|
|
822
|
+
appliedByEpoch
|
|
823
|
+
}
|
|
772
824
|
});
|
|
773
825
|
reconciledHead = position;
|
|
774
826
|
// Do what the classifier said, not what this branch assumes: it answers `ahead` today,
|
|
@@ -794,10 +846,15 @@ export function createGroupPeer(params) {
|
|
|
794
846
|
// version fails BEFORE the commit bytes are extracted, so there is no next frame to
|
|
795
847
|
// heal from — dropping it would step over the group's whole future. To the classifier.
|
|
796
848
|
if (isUnsupportedCommitFrameVersion(error)) {
|
|
797
|
-
const unreadable = classifyCommit(
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
849
|
+
const unreadable = classifyCommit({
|
|
850
|
+
header: UNKNOWN_FRAME_VERSION,
|
|
851
|
+
sequenceID: position,
|
|
852
|
+
commitDigest: null,
|
|
853
|
+
state: {
|
|
854
|
+
localDID,
|
|
855
|
+
epoch: crypto.epoch(),
|
|
856
|
+
appliedByEpoch
|
|
857
|
+
}
|
|
801
858
|
});
|
|
802
859
|
reconciledHead = position;
|
|
803
860
|
// The classifier's answer, not this branch's assumption — as above.
|
|
@@ -812,15 +869,24 @@ export function createGroupPeer(params) {
|
|
|
812
869
|
reconciledHead = position;
|
|
813
870
|
continue;
|
|
814
871
|
}
|
|
872
|
+
// Identify the commit by its own bytes, for the fork check — the COMMIT, not the frame:
|
|
873
|
+
// the sealed blob is derived and re-sealing is legal, so a frame-wide digest would fork
|
|
874
|
+
// the group on a legitimate re-seal.
|
|
875
|
+
const commitDigest = digestAppliedCommit(commitFrame.commit);
|
|
815
876
|
// The commit's OWN epoch and committer, from the commit's own bytes. Never
|
|
816
877
|
// `message.senderDID` — the hub's word about who handed it over, and the hub is not
|
|
817
878
|
// trusted: it could stamp every recipient's own DID onto one poison frame and make the
|
|
818
879
|
// whole group heal at once.
|
|
819
880
|
const header = await port.readCommitHeader(commitFrame.commit);
|
|
820
|
-
const disposition = classifyCommit(
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
881
|
+
const disposition = classifyCommit({
|
|
882
|
+
header,
|
|
883
|
+
sequenceID: position,
|
|
884
|
+
commitDigest,
|
|
885
|
+
state: {
|
|
886
|
+
localDID,
|
|
887
|
+
epoch: crypto.epoch(),
|
|
888
|
+
appliedByEpoch
|
|
889
|
+
}
|
|
824
890
|
});
|
|
825
891
|
if (disposition.row === 'own-unmerged') {
|
|
826
892
|
// This peer's own commit, at the epoch it is still at: the hub took it, the group moved
|
|
@@ -839,8 +905,9 @@ export function createGroupPeer(params) {
|
|
|
839
905
|
continue;
|
|
840
906
|
}
|
|
841
907
|
if (disposition.row === 'history') {
|
|
842
|
-
// A frame from an epoch below this peer's with no record for it
|
|
843
|
-
// poison, not the port's business — its blob is never
|
|
908
|
+
// A frame from an epoch below this peer's, with no record for it or a record naming
|
|
909
|
+
// this same commit. Not a fork, not poison, not the port's business — its blob is never
|
|
910
|
+
// touched.
|
|
844
911
|
reconciledHead = position;
|
|
845
912
|
continue;
|
|
846
913
|
}
|
|
@@ -900,7 +967,10 @@ export function createGroupPeer(params) {
|
|
|
900
967
|
if (applied.advanced) {
|
|
901
968
|
advancedEpoch = true;
|
|
902
969
|
// The fork check's record; the only place it is written from the log.
|
|
903
|
-
appliedByEpoch.set(framedEpoch,
|
|
970
|
+
appliedByEpoch.set(framedEpoch, {
|
|
971
|
+
sequenceID: position,
|
|
972
|
+
digest: commitDigest
|
|
973
|
+
});
|
|
904
974
|
}
|
|
905
975
|
// `{ advanced: false }` here is the port REFUSING a well-formed commit at this peer's own
|
|
906
976
|
// epoch from another member: poison on the same terms as an unresolvable one.
|
|
@@ -943,6 +1013,10 @@ export function createGroupPeer(params) {
|
|
|
943
1013
|
ack();
|
|
944
1014
|
void runSerial(async ()=>{
|
|
945
1015
|
await ready;
|
|
1016
|
+
// A delivery queued here when `dispose()` ran is not on the path dispose waits for: it awaits
|
|
1017
|
+
// `settled`, never the mutex. Refused SILENTLY, unlike every host-facing entry point — there
|
|
1018
|
+
// is no caller to tell, and the catch below would swallow a throw anyway.
|
|
1019
|
+
if (disposed) return;
|
|
946
1020
|
// A wakeup is a lane operation: step 0, the ledger invariant, then the pull. No return
|
|
947
1021
|
// value, so anything found is stashed for the next call that has one.
|
|
948
1022
|
const replayed = await replayJournal();
|
|
@@ -957,6 +1031,9 @@ export function createGroupPeer(params) {
|
|
|
957
1031
|
.then(()=>healIfRequested());
|
|
958
1032
|
};
|
|
959
1033
|
const onRendezvousMessage = (message, ack)=>{
|
|
1034
|
+
// Acked immediately, unlike the commit lane: rendezvous is retried request/reply (see the
|
|
1035
|
+
// "a refused or failed reply" note in `handleRecoveryRequest` above), so losing this specific
|
|
1036
|
+
// delivery just means the requester's retry or another responder covers it.
|
|
960
1037
|
ack();
|
|
961
1038
|
if (mls == null) return;
|
|
962
1039
|
let frame;
|
|
@@ -1046,7 +1123,10 @@ export function createGroupPeer(params) {
|
|
|
1046
1123
|
const accepted = asLogPosition(entry.acceptedAs);
|
|
1047
1124
|
reconciledHead = accepted;
|
|
1048
1125
|
commitLogHead = accepted;
|
|
1049
|
-
appliedByEpoch.set(entry.epoch,
|
|
1126
|
+
appliedByEpoch.set(entry.epoch, {
|
|
1127
|
+
sequenceID: accepted,
|
|
1128
|
+
digest: digestAppliedCommit(entry.commit)
|
|
1129
|
+
});
|
|
1050
1130
|
// Through the seam: the adopt ratchets the handle, so this epoch's app frames are read
|
|
1051
1131
|
// first and the anchor is taken if the journalled commit moved the roster.
|
|
1052
1132
|
await advanceHandle(mls, ()=>adoptJournalled(entry.journal));
|
|
@@ -1101,7 +1181,10 @@ export function createGroupPeer(params) {
|
|
|
1101
1181
|
const accepted = asLogPosition(sequenceID);
|
|
1102
1182
|
reconciledHead = accepted;
|
|
1103
1183
|
commitLogHead = accepted;
|
|
1104
|
-
appliedByEpoch.set(entry.epoch,
|
|
1184
|
+
appliedByEpoch.set(entry.epoch, {
|
|
1185
|
+
sequenceID: accepted,
|
|
1186
|
+
digest: digestAppliedCommit(entry.commit)
|
|
1187
|
+
});
|
|
1105
1188
|
await advanceHandle(mls, ()=>adoptJournalled(entry.journal));
|
|
1106
1189
|
await journal.clear(entry.publishID);
|
|
1107
1190
|
return true;
|
|
@@ -1190,6 +1273,7 @@ export function createGroupPeer(params) {
|
|
|
1190
1273
|
* mutex for its whole life, so `build()` never races another `build()` on this device.
|
|
1191
1274
|
*/ const commit = async (build)=>{
|
|
1192
1275
|
await ready;
|
|
1276
|
+
assertLive();
|
|
1193
1277
|
if (mls == null || journal == null || commitTopicID == null) {
|
|
1194
1278
|
throw new Error('commit: this peer has no MLS port, so it has no group to commit to');
|
|
1195
1279
|
}
|
|
@@ -1287,7 +1371,10 @@ export function createGroupPeer(params) {
|
|
|
1287
1371
|
commitLogHead = accepted;
|
|
1288
1372
|
// A commit this peer made and adopted was enacted at that epoch, like an applied one —
|
|
1289
1373
|
// without it a second commit at an epoch this peer OWNS would read as history.
|
|
1290
|
-
appliedByEpoch.set(framedEpoch,
|
|
1374
|
+
appliedByEpoch.set(framedEpoch, {
|
|
1375
|
+
sequenceID: accepted,
|
|
1376
|
+
digest: digestAppliedCommit(pending.commit)
|
|
1377
|
+
});
|
|
1291
1378
|
// The host adopts here, and adopting ratchets the handle — through the seam, exactly as
|
|
1292
1379
|
// an applied commit does. A member never processes its own commit, so the apply site
|
|
1293
1380
|
// never runs for the roster change this peer just made: without this, the author of a
|
|
@@ -1308,6 +1395,7 @@ export function createGroupPeer(params) {
|
|
|
1308
1395
|
};
|
|
1309
1396
|
const replay = async ()=>{
|
|
1310
1397
|
await ready;
|
|
1398
|
+
assertLive();
|
|
1311
1399
|
return runSerial(async ()=>{
|
|
1312
1400
|
if (await replayJournal()) await rebuildEpoch();
|
|
1313
1401
|
await ensureLedger(Date.now() + recoveryTimeoutMs);
|
|
@@ -1355,6 +1443,7 @@ export function createGroupPeer(params) {
|
|
|
1355
1443
|
* host makes once this releases the lane.
|
|
1356
1444
|
*/ const recover = async ()=>{
|
|
1357
1445
|
await ready;
|
|
1446
|
+
assertLive();
|
|
1358
1447
|
if (mls == null || commitTopicID == null || rendezvousTopicID == null) {
|
|
1359
1448
|
return {
|
|
1360
1449
|
advanced: false,
|
|
@@ -1433,7 +1522,12 @@ export function createGroupPeer(params) {
|
|
|
1433
1522
|
commitLogHead = accepted;
|
|
1434
1523
|
// Enacted at that epoch, like an applied commit: without the record a second commit at
|
|
1435
1524
|
// that epoch reads as history rather than the fork it is.
|
|
1436
|
-
if (rejoinedAtEpoch != null)
|
|
1525
|
+
if (rejoinedAtEpoch != null) {
|
|
1526
|
+
appliedByEpoch.set(rejoinedAtEpoch, {
|
|
1527
|
+
sequenceID,
|
|
1528
|
+
digest: digestAppliedCommit(pending.commit)
|
|
1529
|
+
});
|
|
1530
|
+
}
|
|
1437
1531
|
healRequested = false;
|
|
1438
1532
|
// The one place the commit gate is released: the rejoin landed, so this peer's leaf is
|
|
1439
1533
|
// back in the tree and the stale-epoch fork it guards is closed. A bootstrap that still
|
|
@@ -1522,6 +1616,7 @@ export function createGroupPeer(params) {
|
|
|
1522
1616
|
void settled.then(()=>healIfRequested());
|
|
1523
1617
|
const withReady = async (fn)=>{
|
|
1524
1618
|
await ready;
|
|
1619
|
+
assertLive();
|
|
1525
1620
|
return fn();
|
|
1526
1621
|
};
|
|
1527
1622
|
return {
|
|
@@ -1531,7 +1626,7 @@ export function createGroupPeer(params) {
|
|
|
1531
1626
|
dispatch: (prc, data)=>withReady(()=>surfaceFor(key).dispatch(prc, data)),
|
|
1532
1627
|
request: (prc, prm, options)=>withReady(()=>surfaceFor(key).request(prc, prm, options)),
|
|
1533
1628
|
gather: (prc, prm, options)=>withReady(()=>surfaceFor(key).gather(prc, prm, options)),
|
|
1534
|
-
to: (memberDID)=>surfaceFor(key).to(memberDID)
|
|
1629
|
+
to: (memberDID)=>withReady(()=>surfaceFor(key).to(memberDID))
|
|
1535
1630
|
};
|
|
1536
1631
|
},
|
|
1537
1632
|
commit,
|
|
@@ -1539,10 +1634,21 @@ export function createGroupPeer(params) {
|
|
|
1539
1634
|
recover,
|
|
1540
1635
|
resync: async ()=>{
|
|
1541
1636
|
await ready;
|
|
1542
|
-
|
|
1637
|
+
// Refuse a disposed peer BEFORE `rebuildEpoch`: its `buildEpoch` half re-registers every
|
|
1638
|
+
// listener and retain on a mux whose `dispose()` has already cleared `listeners` and
|
|
1639
|
+
// `refcount` and stopped the drain. The rebuilt epoch lands in maps nothing walks again,
|
|
1640
|
+
// and no second teardown reaches it. Invisible from the hub — the mux deliberately leaves
|
|
1641
|
+
// `subscriptions` standing, so nothing re-subscribes; the whole cost is held locally.
|
|
1642
|
+
assertLive();
|
|
1643
|
+
// Every other `rebuildEpoch` caller runs under the commit mutex. Unlocked, a host-called
|
|
1644
|
+
// resync interleaves with an inbound-commit rebuild and runs two teardown/build cycles over
|
|
1645
|
+
// one set of runtimes. Safe to wrap only because `rebuildEpoch` takes no lock itself and
|
|
1646
|
+
// this is a top-level entry — `runSerial` is not reentrant.
|
|
1647
|
+
await runSerial(()=>rebuildEpoch());
|
|
1543
1648
|
},
|
|
1544
1649
|
anchorEpoch: ()=>anchor.epoch,
|
|
1545
1650
|
dispose: async ()=>{
|
|
1651
|
+
disposed = true;
|
|
1546
1652
|
// Tear down even a peer whose init failed — it still holds a hub drain.
|
|
1547
1653
|
await settled;
|
|
1548
1654
|
commitUnsubscribe?.();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kumiai/rpc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
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",
|
|
@@ -26,23 +26,28 @@
|
|
|
26
26
|
"lib/*"
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@enkaku/client": "^0.
|
|
30
|
-
"@enkaku/protocol": "^0.
|
|
31
|
-
"@enkaku/server": "^0.
|
|
32
|
-
"@enkaku/transport": "^0.
|
|
33
|
-
"@noble/hashes": "^2.
|
|
34
|
-
"@sozai/codec": "^0.
|
|
35
|
-
"@sozai/
|
|
29
|
+
"@enkaku/client": "^0.21.0",
|
|
30
|
+
"@enkaku/protocol": "^0.21.0",
|
|
31
|
+
"@enkaku/server": "^0.21.0",
|
|
32
|
+
"@enkaku/transport": "^0.21.0",
|
|
33
|
+
"@noble/hashes": "^2.3.0",
|
|
34
|
+
"@sozai/codec": "^0.4.0",
|
|
35
|
+
"@sozai/event": "^0.1.3",
|
|
36
|
+
"@sozai/log": "^0.3.1",
|
|
36
37
|
"@sozai/runtime": "^0.1.0",
|
|
37
|
-
"@
|
|
38
|
-
"@kumiai/
|
|
39
|
-
"@kumiai/hub-
|
|
38
|
+
"@sozai/schema": "^0.1.1",
|
|
39
|
+
"@kumiai/broadcast": "^0.6.0",
|
|
40
|
+
"@kumiai/hub-protocol": "^0.6.0",
|
|
41
|
+
"@kumiai/hub-tunnel": "^0.6.0"
|
|
40
42
|
},
|
|
41
43
|
"devDependencies": {
|
|
42
|
-
"@noble/curves": "^2.0
|
|
44
|
+
"@noble/curves": "^2.3.0",
|
|
43
45
|
"vitest": "^4.1.10",
|
|
44
|
-
"@kumiai/hub-conformance": "^0.
|
|
45
|
-
"@kumiai/rpc-conformance": "^0.
|
|
46
|
+
"@kumiai/hub-conformance": "^0.6.0",
|
|
47
|
+
"@kumiai/rpc-conformance": "^0.6.0"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
46
51
|
},
|
|
47
52
|
"scripts": {
|
|
48
53
|
"build": "pnpm run build:clean && pnpm run build:js && pnpm run build:types",
|
package/lib/bus-server.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { TransportType } from '@enkaku/transport';
|
|
2
|
-
import { type BroadcastHandler, type BroadcastMessage, type SuppressibleHandler } from '@kumiai/broadcast';
|
|
3
|
-
export type GroupBusServerParams = {
|
|
4
|
-
transport: TransportType<BroadcastMessage, BroadcastMessage>;
|
|
5
|
-
/**
|
|
6
|
-
* This server's own name, for buses with no authenticated sender. Written as the outgoing
|
|
7
|
-
* message's transport-level `senderDID`, never into a reply body; over MLS the receiver's
|
|
8
|
-
* `unwrap` overwrites it and it never reaches a consumer. Mirrors
|
|
9
|
-
* `BroadcastResponderParams.from`.
|
|
10
|
-
*/
|
|
11
|
-
from: string;
|
|
12
|
-
/** Fire-and-forget event procedures: prc -> handler(data, senderDID). */
|
|
13
|
-
eventHandlers: Record<string, (data: unknown, senderDID?: string) => void | Promise<void>>;
|
|
14
|
-
/** Anycast request procedures: prc -> handler(param, { senderDID }) -> result. */
|
|
15
|
-
requestHandlers: Record<string, BroadcastHandler | SuppressibleHandler>;
|
|
16
|
-
sleep?: (ms: number) => Promise<void>;
|
|
17
|
-
getJitterMs?: (maxMs: number) => number;
|
|
18
|
-
};
|
|
19
|
-
export declare function createGroupBusServer(params: GroupBusServerParams): {
|
|
20
|
-
dispose: () => Promise<void>;
|
|
21
|
-
};
|
package/lib/bus-server.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { defaultJitter, defaultSleep, isSuppressible } from '@kumiai/broadcast';
|
|
2
|
-
const DEFAULT_JITTER_MS = 250;
|
|
3
|
-
const DEFAULT_SUPPRESS_TTL_MS = 30_000;
|
|
4
|
-
export function createGroupBusServer(params) {
|
|
5
|
-
const { transport, from, eventHandlers, requestHandlers } = params;
|
|
6
|
-
const sleep = params.sleep ?? defaultSleep;
|
|
7
|
-
const getJitterMs = params.getJitterMs ?? defaultJitter;
|
|
8
|
-
const suppressTimers = new Map();
|
|
9
|
-
let running = true;
|
|
10
|
-
const markReplied = (rid, ttlMs)=>{
|
|
11
|
-
if (suppressTimers.has(rid)) return;
|
|
12
|
-
const timer = setTimeout(()=>suppressTimers.delete(rid), ttlMs);
|
|
13
|
-
suppressTimers.set(rid, timer);
|
|
14
|
-
};
|
|
15
|
-
const handleRequest = async (prc, request, handler, senderDID)=>{
|
|
16
|
-
const isGather = request.gather === true;
|
|
17
|
-
if (!isGather && isSuppressible(handler)) {
|
|
18
|
-
const { jitterMs = DEFAULT_JITTER_MS } = handler.suppress;
|
|
19
|
-
await sleep(getJitterMs(jitterMs));
|
|
20
|
-
if (suppressTimers.has(request.rid)) return;
|
|
21
|
-
}
|
|
22
|
-
let reply;
|
|
23
|
-
try {
|
|
24
|
-
const ok = await handler(request.prm, {
|
|
25
|
-
senderDID
|
|
26
|
-
});
|
|
27
|
-
reply = {
|
|
28
|
-
kind: 'res',
|
|
29
|
-
rid: request.rid,
|
|
30
|
-
ok
|
|
31
|
-
};
|
|
32
|
-
} catch (error) {
|
|
33
|
-
reply = {
|
|
34
|
-
kind: 'res',
|
|
35
|
-
rid: request.rid,
|
|
36
|
-
err: error instanceof Error ? error.message : String(error)
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
const ttlMs = isSuppressible(handler) ? handler.suppress.suppressTtlMs ?? DEFAULT_SUPPRESS_TTL_MS : DEFAULT_SUPPRESS_TTL_MS;
|
|
40
|
-
if (!isGather) markReplied(request.rid, ttlMs);
|
|
41
|
-
await transport.write({
|
|
42
|
-
payload: {
|
|
43
|
-
typ: 'event',
|
|
44
|
-
prc,
|
|
45
|
-
data: reply
|
|
46
|
-
},
|
|
47
|
-
senderDID: from
|
|
48
|
-
}).catch(()=>{});
|
|
49
|
-
};
|
|
50
|
-
void (async ()=>{
|
|
51
|
-
for await (const msg of transport){
|
|
52
|
-
if (!running) break;
|
|
53
|
-
const payload = msg?.payload;
|
|
54
|
-
if (payload?.typ !== 'event' || typeof payload.prc !== 'string') continue;
|
|
55
|
-
const data = payload.data;
|
|
56
|
-
if (data?.kind === 'res' && typeof data.rid === 'string') {
|
|
57
|
-
markReplied(data.rid, DEFAULT_SUPPRESS_TTL_MS);
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
if (data?.kind === 'req' && typeof data.rid === 'string') {
|
|
61
|
-
const handler = requestHandlers[payload.prc];
|
|
62
|
-
if (handler != null) {
|
|
63
|
-
void handleRequest(payload.prc, data, handler, msg.senderDID);
|
|
64
|
-
}
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
const eventHandler = eventHandlers[payload.prc];
|
|
68
|
-
if (eventHandler != null) {
|
|
69
|
-
void Promise.resolve(eventHandler(payload.data, msg.senderDID)).catch(()=>{});
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
})().catch(()=>{});
|
|
73
|
-
return {
|
|
74
|
-
dispose: async ()=>{
|
|
75
|
-
running = false;
|
|
76
|
-
for (const timer of suppressTimers.values())clearTimeout(timer);
|
|
77
|
-
suppressTimers.clear();
|
|
78
|
-
await transport.dispose();
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
}
|