@kumiai/rpc 0.4.3 → 0.5.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 +125 -35
- package/package.json +14 -9
- 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,26 @@ export function createGroupPeer(params) {
|
|
|
400
412
|
await teardownEpoch();
|
|
401
413
|
await buildEpoch();
|
|
402
414
|
};
|
|
415
|
+
/**
|
|
416
|
+
* Set as `dispose()`'s FIRST statement, and the whole of the peer's post-dispose rule: a
|
|
417
|
+
* disposed peer refuses everything.
|
|
418
|
+
*
|
|
419
|
+
* The check belongs immediately after each entry point's `await ready`, because that await is
|
|
420
|
+
* where the race lives. `dispose()` awaits a promise DERIVED from `ready`, so a call queued
|
|
421
|
+
* before init settles always resumes one microtask EARLIER than dispose's own body — early
|
|
422
|
+
* enough to run to completion against runtimes a teardown is about to walk. Unguarded, each
|
|
423
|
+
* entry point fails differently and none of them says why: `to()` handed back a live-looking
|
|
424
|
+
* client over an already-aborted transport, `resync()` rebuilt a whole epoch onto a disposed
|
|
425
|
+
* mux, and `commit()` published to the hub from a peer that is gone. In the mirror ordering,
|
|
426
|
+
* where teardown got there first and emptied `runtimes`, a protocol call blamed
|
|
427
|
+
* `Unknown protocol` for a peer that no longer exists.
|
|
428
|
+
*
|
|
429
|
+
* The `resync` leak is LOCAL and invisible from the hub — do not re-derive it as re-subscribing.
|
|
430
|
+
* `resync`'s own comment carries the mechanism; read it there rather than restating it here.
|
|
431
|
+
*/ let disposed = false;
|
|
432
|
+
const assertLive = ()=>{
|
|
433
|
+
if (disposed) throw new Error('Peer is disposed');
|
|
434
|
+
};
|
|
403
435
|
let commitUnsubscribe;
|
|
404
436
|
let rendezvousUnsubscribe;
|
|
405
437
|
let commitTopicID;
|
|
@@ -418,10 +450,11 @@ export function createGroupPeer(params) {
|
|
|
418
450
|
* from the cursor. `null` means the topic never had an accepted log publish.
|
|
419
451
|
*/ let commitLogHead = null;
|
|
420
452
|
/**
|
|
421
|
-
* The
|
|
422
|
-
* and adopted
|
|
423
|
-
*
|
|
424
|
-
* different logs to different members.
|
|
453
|
+
* The commit this peer ENACTED at each epoch it passed — applied from the log, or committed
|
|
454
|
+
* and adopted — by digest, with the sequenceID the log carried it at. The whole of the fork
|
|
455
|
+
* check: a second, DIFFERENT commit at an epoch this peer holds a record for is two commits at
|
|
456
|
+
* one epoch, which the hub can only produce by showing different logs to different members. The
|
|
457
|
+
* same commit re-served at a new position is not, however the log came to carry it twice.
|
|
425
458
|
*
|
|
426
459
|
* An epoch with NO record is history, not a fork — a late joiner, rejoiner or re-seeded peer all
|
|
427
460
|
* walk commits from epochs they never held. In memory, deliberately: a restart drops the record,
|
|
@@ -765,10 +798,17 @@ export function createGroupPeer(params) {
|
|
|
765
798
|
// the magic means what this build thinks under an unknown version. On the commit topic —
|
|
766
799
|
// and only here — that is evidence in itself, so it goes to the classifier, not dropped.
|
|
767
800
|
if (frame.version !== HANDSHAKE_VERSION) {
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
801
|
+
// No digest: the frame's version put its commit bytes out of reach entirely. Settled at
|
|
802
|
+
// `ahead` before the digest is read.
|
|
803
|
+
const unreadable = classifyCommit({
|
|
804
|
+
header: UNKNOWN_FRAME_VERSION,
|
|
805
|
+
sequenceID: position,
|
|
806
|
+
commitDigest: null,
|
|
807
|
+
state: {
|
|
808
|
+
localDID,
|
|
809
|
+
epoch: crypto.epoch(),
|
|
810
|
+
appliedByEpoch
|
|
811
|
+
}
|
|
772
812
|
});
|
|
773
813
|
reconciledHead = position;
|
|
774
814
|
// Do what the classifier said, not what this branch assumes: it answers `ahead` today,
|
|
@@ -794,10 +834,15 @@ export function createGroupPeer(params) {
|
|
|
794
834
|
// version fails BEFORE the commit bytes are extracted, so there is no next frame to
|
|
795
835
|
// heal from — dropping it would step over the group's whole future. To the classifier.
|
|
796
836
|
if (isUnsupportedCommitFrameVersion(error)) {
|
|
797
|
-
const unreadable = classifyCommit(
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
837
|
+
const unreadable = classifyCommit({
|
|
838
|
+
header: UNKNOWN_FRAME_VERSION,
|
|
839
|
+
sequenceID: position,
|
|
840
|
+
commitDigest: null,
|
|
841
|
+
state: {
|
|
842
|
+
localDID,
|
|
843
|
+
epoch: crypto.epoch(),
|
|
844
|
+
appliedByEpoch
|
|
845
|
+
}
|
|
801
846
|
});
|
|
802
847
|
reconciledHead = position;
|
|
803
848
|
// The classifier's answer, not this branch's assumption — as above.
|
|
@@ -812,15 +857,24 @@ export function createGroupPeer(params) {
|
|
|
812
857
|
reconciledHead = position;
|
|
813
858
|
continue;
|
|
814
859
|
}
|
|
860
|
+
// Identify the commit by its own bytes, for the fork check — the COMMIT, not the frame:
|
|
861
|
+
// the sealed blob is derived and re-sealing is legal, so a frame-wide digest would fork
|
|
862
|
+
// the group on a legitimate re-seal.
|
|
863
|
+
const commitDigest = digestAppliedCommit(commitFrame.commit);
|
|
815
864
|
// The commit's OWN epoch and committer, from the commit's own bytes. Never
|
|
816
865
|
// `message.senderDID` — the hub's word about who handed it over, and the hub is not
|
|
817
866
|
// trusted: it could stamp every recipient's own DID onto one poison frame and make the
|
|
818
867
|
// whole group heal at once.
|
|
819
868
|
const header = await port.readCommitHeader(commitFrame.commit);
|
|
820
|
-
const disposition = classifyCommit(
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
869
|
+
const disposition = classifyCommit({
|
|
870
|
+
header,
|
|
871
|
+
sequenceID: position,
|
|
872
|
+
commitDigest,
|
|
873
|
+
state: {
|
|
874
|
+
localDID,
|
|
875
|
+
epoch: crypto.epoch(),
|
|
876
|
+
appliedByEpoch
|
|
877
|
+
}
|
|
824
878
|
});
|
|
825
879
|
if (disposition.row === 'own-unmerged') {
|
|
826
880
|
// This peer's own commit, at the epoch it is still at: the hub took it, the group moved
|
|
@@ -839,8 +893,9 @@ export function createGroupPeer(params) {
|
|
|
839
893
|
continue;
|
|
840
894
|
}
|
|
841
895
|
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
|
|
896
|
+
// A frame from an epoch below this peer's, with no record for it or a record naming
|
|
897
|
+
// this same commit. Not a fork, not poison, not the port's business — its blob is never
|
|
898
|
+
// touched.
|
|
844
899
|
reconciledHead = position;
|
|
845
900
|
continue;
|
|
846
901
|
}
|
|
@@ -900,7 +955,10 @@ export function createGroupPeer(params) {
|
|
|
900
955
|
if (applied.advanced) {
|
|
901
956
|
advancedEpoch = true;
|
|
902
957
|
// The fork check's record; the only place it is written from the log.
|
|
903
|
-
appliedByEpoch.set(framedEpoch,
|
|
958
|
+
appliedByEpoch.set(framedEpoch, {
|
|
959
|
+
sequenceID: position,
|
|
960
|
+
digest: commitDigest
|
|
961
|
+
});
|
|
904
962
|
}
|
|
905
963
|
// `{ advanced: false }` here is the port REFUSING a well-formed commit at this peer's own
|
|
906
964
|
// epoch from another member: poison on the same terms as an unresolvable one.
|
|
@@ -957,6 +1015,9 @@ export function createGroupPeer(params) {
|
|
|
957
1015
|
.then(()=>healIfRequested());
|
|
958
1016
|
};
|
|
959
1017
|
const onRendezvousMessage = (message, ack)=>{
|
|
1018
|
+
// Acked immediately, unlike the commit lane: rendezvous is retried request/reply (see the
|
|
1019
|
+
// "a refused or failed reply" note in `handleRecoveryRequest` above), so losing this specific
|
|
1020
|
+
// delivery just means the requester's retry or another responder covers it.
|
|
960
1021
|
ack();
|
|
961
1022
|
if (mls == null) return;
|
|
962
1023
|
let frame;
|
|
@@ -1046,7 +1107,10 @@ export function createGroupPeer(params) {
|
|
|
1046
1107
|
const accepted = asLogPosition(entry.acceptedAs);
|
|
1047
1108
|
reconciledHead = accepted;
|
|
1048
1109
|
commitLogHead = accepted;
|
|
1049
|
-
appliedByEpoch.set(entry.epoch,
|
|
1110
|
+
appliedByEpoch.set(entry.epoch, {
|
|
1111
|
+
sequenceID: accepted,
|
|
1112
|
+
digest: digestAppliedCommit(entry.commit)
|
|
1113
|
+
});
|
|
1050
1114
|
// Through the seam: the adopt ratchets the handle, so this epoch's app frames are read
|
|
1051
1115
|
// first and the anchor is taken if the journalled commit moved the roster.
|
|
1052
1116
|
await advanceHandle(mls, ()=>adoptJournalled(entry.journal));
|
|
@@ -1101,7 +1165,10 @@ export function createGroupPeer(params) {
|
|
|
1101
1165
|
const accepted = asLogPosition(sequenceID);
|
|
1102
1166
|
reconciledHead = accepted;
|
|
1103
1167
|
commitLogHead = accepted;
|
|
1104
|
-
appliedByEpoch.set(entry.epoch,
|
|
1168
|
+
appliedByEpoch.set(entry.epoch, {
|
|
1169
|
+
sequenceID: accepted,
|
|
1170
|
+
digest: digestAppliedCommit(entry.commit)
|
|
1171
|
+
});
|
|
1105
1172
|
await advanceHandle(mls, ()=>adoptJournalled(entry.journal));
|
|
1106
1173
|
await journal.clear(entry.publishID);
|
|
1107
1174
|
return true;
|
|
@@ -1190,6 +1257,7 @@ export function createGroupPeer(params) {
|
|
|
1190
1257
|
* mutex for its whole life, so `build()` never races another `build()` on this device.
|
|
1191
1258
|
*/ const commit = async (build)=>{
|
|
1192
1259
|
await ready;
|
|
1260
|
+
assertLive();
|
|
1193
1261
|
if (mls == null || journal == null || commitTopicID == null) {
|
|
1194
1262
|
throw new Error('commit: this peer has no MLS port, so it has no group to commit to');
|
|
1195
1263
|
}
|
|
@@ -1287,7 +1355,10 @@ export function createGroupPeer(params) {
|
|
|
1287
1355
|
commitLogHead = accepted;
|
|
1288
1356
|
// A commit this peer made and adopted was enacted at that epoch, like an applied one —
|
|
1289
1357
|
// without it a second commit at an epoch this peer OWNS would read as history.
|
|
1290
|
-
appliedByEpoch.set(framedEpoch,
|
|
1358
|
+
appliedByEpoch.set(framedEpoch, {
|
|
1359
|
+
sequenceID: accepted,
|
|
1360
|
+
digest: digestAppliedCommit(pending.commit)
|
|
1361
|
+
});
|
|
1291
1362
|
// The host adopts here, and adopting ratchets the handle — through the seam, exactly as
|
|
1292
1363
|
// an applied commit does. A member never processes its own commit, so the apply site
|
|
1293
1364
|
// never runs for the roster change this peer just made: without this, the author of a
|
|
@@ -1308,6 +1379,7 @@ export function createGroupPeer(params) {
|
|
|
1308
1379
|
};
|
|
1309
1380
|
const replay = async ()=>{
|
|
1310
1381
|
await ready;
|
|
1382
|
+
assertLive();
|
|
1311
1383
|
return runSerial(async ()=>{
|
|
1312
1384
|
if (await replayJournal()) await rebuildEpoch();
|
|
1313
1385
|
await ensureLedger(Date.now() + recoveryTimeoutMs);
|
|
@@ -1355,6 +1427,7 @@ export function createGroupPeer(params) {
|
|
|
1355
1427
|
* host makes once this releases the lane.
|
|
1356
1428
|
*/ const recover = async ()=>{
|
|
1357
1429
|
await ready;
|
|
1430
|
+
assertLive();
|
|
1358
1431
|
if (mls == null || commitTopicID == null || rendezvousTopicID == null) {
|
|
1359
1432
|
return {
|
|
1360
1433
|
advanced: false,
|
|
@@ -1433,7 +1506,12 @@ export function createGroupPeer(params) {
|
|
|
1433
1506
|
commitLogHead = accepted;
|
|
1434
1507
|
// Enacted at that epoch, like an applied commit: without the record a second commit at
|
|
1435
1508
|
// that epoch reads as history rather than the fork it is.
|
|
1436
|
-
if (rejoinedAtEpoch != null)
|
|
1509
|
+
if (rejoinedAtEpoch != null) {
|
|
1510
|
+
appliedByEpoch.set(rejoinedAtEpoch, {
|
|
1511
|
+
sequenceID,
|
|
1512
|
+
digest: digestAppliedCommit(pending.commit)
|
|
1513
|
+
});
|
|
1514
|
+
}
|
|
1437
1515
|
healRequested = false;
|
|
1438
1516
|
// The one place the commit gate is released: the rejoin landed, so this peer's leaf is
|
|
1439
1517
|
// back in the tree and the stale-epoch fork it guards is closed. A bootstrap that still
|
|
@@ -1522,6 +1600,7 @@ export function createGroupPeer(params) {
|
|
|
1522
1600
|
void settled.then(()=>healIfRequested());
|
|
1523
1601
|
const withReady = async (fn)=>{
|
|
1524
1602
|
await ready;
|
|
1603
|
+
assertLive();
|
|
1525
1604
|
return fn();
|
|
1526
1605
|
};
|
|
1527
1606
|
return {
|
|
@@ -1531,7 +1610,7 @@ export function createGroupPeer(params) {
|
|
|
1531
1610
|
dispatch: (prc, data)=>withReady(()=>surfaceFor(key).dispatch(prc, data)),
|
|
1532
1611
|
request: (prc, prm, options)=>withReady(()=>surfaceFor(key).request(prc, prm, options)),
|
|
1533
1612
|
gather: (prc, prm, options)=>withReady(()=>surfaceFor(key).gather(prc, prm, options)),
|
|
1534
|
-
to: (memberDID)=>surfaceFor(key).to(memberDID)
|
|
1613
|
+
to: (memberDID)=>withReady(()=>surfaceFor(key).to(memberDID))
|
|
1535
1614
|
};
|
|
1536
1615
|
},
|
|
1537
1616
|
commit,
|
|
@@ -1539,10 +1618,21 @@ export function createGroupPeer(params) {
|
|
|
1539
1618
|
recover,
|
|
1540
1619
|
resync: async ()=>{
|
|
1541
1620
|
await ready;
|
|
1542
|
-
|
|
1621
|
+
// Refuse a disposed peer BEFORE `rebuildEpoch`: its `buildEpoch` half re-registers every
|
|
1622
|
+
// listener and retain on a mux whose `dispose()` has already cleared `listeners` and
|
|
1623
|
+
// `refcount` and stopped the drain. The rebuilt epoch lands in maps nothing walks again,
|
|
1624
|
+
// and no second teardown reaches it. Invisible from the hub — the mux deliberately leaves
|
|
1625
|
+
// `subscriptions` standing, so nothing re-subscribes; the whole cost is held locally.
|
|
1626
|
+
assertLive();
|
|
1627
|
+
// Every other `rebuildEpoch` caller runs under the commit mutex. Unlocked, a host-called
|
|
1628
|
+
// resync interleaves with an inbound-commit rebuild and runs two teardown/build cycles over
|
|
1629
|
+
// one set of runtimes. Safe to wrap only because `rebuildEpoch` takes no lock itself and
|
|
1630
|
+
// this is a top-level entry — `runSerial` is not reentrant.
|
|
1631
|
+
await runSerial(()=>rebuildEpoch());
|
|
1543
1632
|
},
|
|
1544
1633
|
anchorEpoch: ()=>anchor.epoch,
|
|
1545
1634
|
dispose: async ()=>{
|
|
1635
|
+
disposed = true;
|
|
1546
1636
|
// Tear down even a peer whose init failed — it still holds a hub drain.
|
|
1547
1637
|
await settled;
|
|
1548
1638
|
commitUnsubscribe?.();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kumiai/rpc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.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",
|
|
@@ -30,19 +30,24 @@
|
|
|
30
30
|
"@enkaku/protocol": "^0.20.0",
|
|
31
31
|
"@enkaku/server": "^0.20.0",
|
|
32
32
|
"@enkaku/transport": "^0.20.0",
|
|
33
|
-
"@noble/hashes": "^2.
|
|
34
|
-
"@sozai/codec": "^0.
|
|
35
|
-
"@sozai/
|
|
33
|
+
"@noble/hashes": "^2.2.0",
|
|
34
|
+
"@sozai/codec": "^0.3.0",
|
|
35
|
+
"@sozai/event": "^0.1.3",
|
|
36
|
+
"@sozai/log": "^0.3.0",
|
|
36
37
|
"@sozai/runtime": "^0.1.0",
|
|
37
|
-
"@
|
|
38
|
-
"@kumiai/
|
|
39
|
-
"@kumiai/hub-
|
|
38
|
+
"@sozai/schema": "^0.1.1",
|
|
39
|
+
"@kumiai/broadcast": "^0.5.0",
|
|
40
|
+
"@kumiai/hub-protocol": "^0.5.0",
|
|
41
|
+
"@kumiai/hub-tunnel": "^0.5.0"
|
|
40
42
|
},
|
|
41
43
|
"devDependencies": {
|
|
42
44
|
"@noble/curves": "^2.0.1",
|
|
43
45
|
"vitest": "^4.1.10",
|
|
44
|
-
"@kumiai/hub-conformance": "^0.
|
|
45
|
-
"@kumiai/rpc-conformance": "^0.
|
|
46
|
+
"@kumiai/hub-conformance": "^0.5.0",
|
|
47
|
+
"@kumiai/rpc-conformance": "^0.5.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
|
-
}
|