@kumiai/rpc 0.7.0 → 0.8.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/lib/errors.d.ts +8 -0
- package/lib/errors.js +7 -0
- package/lib/hub-mux.d.ts +9 -0
- package/lib/hub-mux.js +59 -22
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/peer.js +64 -25
- package/package.json +12 -12
package/lib/errors.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A peer that has been disposed refused an operation. A caller branches on this: retry against a
|
|
3
|
+
* fresh peer, versus surface to the user. Lifecycle conditions a caller acts on get a named class;
|
|
4
|
+
* programmer errors (`Unknown protocol`, no-MLS-port) stay bare `Error`.
|
|
5
|
+
*/
|
|
6
|
+
export declare class PeerDisposedError extends Error {
|
|
7
|
+
name: string;
|
|
8
|
+
}
|
package/lib/errors.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A peer that has been disposed refused an operation. A caller branches on this: retry against a
|
|
3
|
+
* fresh peer, versus surface to the user. Lifecycle conditions a caller acts on get a named class;
|
|
4
|
+
* programmer errors (`Unknown protocol`, no-MLS-port) stay bare `Error`.
|
|
5
|
+
*/ export class PeerDisposedError extends Error {
|
|
6
|
+
name = 'PeerDisposedError';
|
|
7
|
+
}
|
package/lib/hub-mux.d.ts
CHANGED
|
@@ -120,6 +120,15 @@ export type HubMux = {
|
|
|
120
120
|
* and the hub keeps holding its mail.
|
|
121
121
|
*/
|
|
122
122
|
dispose: () => Promise<void>;
|
|
123
|
+
/**
|
|
124
|
+
* Refuse every publish from now on, WITHOUT awaiting anything — the belt to `dispose`'s
|
|
125
|
+
* suspenders. A lane op that already passed its own `assertLive` can still be running inside a
|
|
126
|
+
* host's commit mutex when the host calls `dispose()`; `dispose()` cannot safely wait for that
|
|
127
|
+
* mutex to drain (see {@link createHubMux}'s note on `publishSuspended`), so it calls this FIRST
|
|
128
|
+
* — synchronously, before awaiting anything else — closing the window an op could otherwise run
|
|
129
|
+
* through and land a write on the wire after the host believes the peer is gone.
|
|
130
|
+
*/
|
|
131
|
+
suspendPublishing: () => void;
|
|
123
132
|
};
|
|
124
133
|
/**
|
|
125
134
|
* Multiplex a single hub `receive` drain into a BroadcastBus view, a mailbox-hub view (for
|
package/lib/hub-mux.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AuthorizationDeniedError, RetentionExceededError } from '@kumiai/hub-protocol';
|
|
2
2
|
import { getReporter } from '@sozai/log';
|
|
3
3
|
import { asDeliveryPosition } from './cursor.js';
|
|
4
|
+
import { PeerDisposedError } from './errors.js';
|
|
4
5
|
/**
|
|
5
6
|
* Backoff for a TRANSIENT subscribe failure, in ms. Short enough that an ordinary reconnect blip
|
|
6
7
|
* heals before the first lane operation, bounded so a hub that is answering "no" in a shape this
|
|
@@ -83,6 +84,19 @@ const warnReceiveEnded = (ended)=>{
|
|
|
83
84
|
const subscriptions = new Map();
|
|
84
85
|
const sinks = new Set();
|
|
85
86
|
let disposed = false;
|
|
87
|
+
let disposePromise;
|
|
88
|
+
/**
|
|
89
|
+
* A separate flag from `disposed`, set FIRST — before `peer.dispose()` awaits anything — by
|
|
90
|
+
* `suspendPublishing`. An op already inside a host's commit mutex when `dispose()` runs has
|
|
91
|
+
* already passed its own entry guard and keeps running; `dispose()` cannot safely await that
|
|
92
|
+
* mutex draining (host-supplied `build()`/`onAccepted()` are unbounded, and a host that calls
|
|
93
|
+
* `dispose()` from inside one of them would self-deadlock). Guarding the publish funnel instead
|
|
94
|
+
* awaits nothing, so it closes the window regardless of how long the op runs before it gets
|
|
95
|
+
* here.
|
|
96
|
+
*
|
|
97
|
+
* BOUNDARY: this stops a publish that has not yet entered the mux. A publish already awaiting
|
|
98
|
+
* `hub.publish` is on the wire and out of scope — see the three publish routes below.
|
|
99
|
+
*/ let publishSuspended = false;
|
|
86
100
|
/**
|
|
87
101
|
* Retry backoffs currently sleeping, each as the function that ends its sleep early.
|
|
88
102
|
*
|
|
@@ -359,6 +373,9 @@ const warnReceiveEnded = (ended)=>{
|
|
|
359
373
|
})();
|
|
360
374
|
const bus = {
|
|
361
375
|
publish: async (topicID, payload)=>{
|
|
376
|
+
// A publish not yet reached: see `publishSuspended`'s note above for why this awaits
|
|
377
|
+
// nothing and what it does NOT cover.
|
|
378
|
+
if (publishSuspended) throw new PeerDisposedError('mux: publish after dispose');
|
|
362
379
|
assertSubscribable(topicID);
|
|
363
380
|
await hub.publish({
|
|
364
381
|
senderDID: localDID,
|
|
@@ -370,6 +387,8 @@ const warnReceiveEnded = (ended)=>{
|
|
|
370
387
|
};
|
|
371
388
|
const mailbox = {
|
|
372
389
|
publish: async (publishParams)=>{
|
|
390
|
+
// See `publishSuspended`'s note above.
|
|
391
|
+
if (publishSuspended) throw new PeerDisposedError('mux: publish after dispose');
|
|
373
392
|
assertSubscribable(publishParams.topicID);
|
|
374
393
|
return await hub.publish(publishParams);
|
|
375
394
|
},
|
|
@@ -453,6 +472,8 @@ const warnReceiveEnded = (ended)=>{
|
|
|
453
472
|
}
|
|
454
473
|
};
|
|
455
474
|
const publish = async (params)=>{
|
|
475
|
+
// See `publishSuspended`'s note above. This is the commit lane's own route to the wire.
|
|
476
|
+
if (publishSuspended) throw new PeerDisposedError('mux: publish after dispose');
|
|
456
477
|
assertSubscribable(params.topicID);
|
|
457
478
|
return await Promise.resolve(hub.publish({
|
|
458
479
|
senderDID: localDID,
|
|
@@ -499,29 +520,45 @@ const warnReceiveEnded = (ended)=>{
|
|
|
499
520
|
},
|
|
500
521
|
fetchTopic,
|
|
501
522
|
onInbound,
|
|
502
|
-
|
|
503
|
-
|
|
523
|
+
suspendPublishing: ()=>{
|
|
524
|
+
publishSuspended = true;
|
|
525
|
+
},
|
|
526
|
+
dispose: ()=>{
|
|
527
|
+
if (disposePromise != null) return disposePromise;
|
|
504
528
|
disposed = true;
|
|
505
|
-
//
|
|
506
|
-
//
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
sinks.
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
529
|
+
// Belt-and-suspenders: `peer.dispose()` already calls `suspendPublishing()` before this
|
|
530
|
+
// runs, but a caller of THIS `dispose()` directly (every test file that builds a mux
|
|
531
|
+
// standalone) gets the same guarantee. Idempotency stays keyed on `disposePromise` (the
|
|
532
|
+
// guard above), not this flag.
|
|
533
|
+
publishSuspended = true;
|
|
534
|
+
disposePromise = (async ()=>{
|
|
535
|
+
// Before anything else: a retry sleeping out its backoff is work already abandoned, and
|
|
536
|
+
// every path it could wake into checks `disposed` and returns.
|
|
537
|
+
for (const wake of [
|
|
538
|
+
...sleeping
|
|
539
|
+
])wake();
|
|
540
|
+
// Cleared without acking (the same abandon as `abandonSink`/`sweepPending`), before closing
|
|
541
|
+
// sinks so their per-sink scans have nothing left to find.
|
|
542
|
+
pending.clear();
|
|
543
|
+
for (const sink of [
|
|
544
|
+
...sinks
|
|
545
|
+
]){
|
|
546
|
+
sink.close();
|
|
547
|
+
sinks.delete(sink);
|
|
548
|
+
}
|
|
549
|
+
// Listeners go, the drain stops, SUBSCRIPTIONS STAND. Disposing means "stopped reading",
|
|
550
|
+
// not "read everything, discard the rest". On mobile this is what backgrounding calls;
|
|
551
|
+
// unsubscribing here would delete the user's unread mail on every app switch.
|
|
552
|
+
refcount.clear();
|
|
553
|
+
listeners.clear();
|
|
554
|
+
// Fire-and-forget: awaiting `return()` deadlocks on the real wire hub, where it waits behind
|
|
555
|
+
// the drain loop's in-flight `next()` that never settles during teardown. Post-dispose writes
|
|
556
|
+
// are already blocked by the `publishSuspended` guards, so the close need not be awaited.
|
|
557
|
+
// No-op catch keeps a late rejection off the unhandled-rejection path.
|
|
558
|
+
const closed = iterator.return?.();
|
|
559
|
+
if (closed != null) void Promise.resolve(closed).catch(()=>{});
|
|
560
|
+
})();
|
|
561
|
+
return disposePromise;
|
|
525
562
|
}
|
|
526
563
|
};
|
|
527
564
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export { CommitDeadlineError, type CommitJournal, type CommitKind, isHeadMismatc
|
|
|
10
10
|
export { COMMIT_FRAME_VERSION, type CommitFrame, decodeCommitFrame, encodeCommitFrame, isUnsupportedCommitFrameVersion, UnsupportedCommitFrameVersionError, } from './commit-frame.js';
|
|
11
11
|
export { type CommitContext, type CommitHeader, type GroupCrypto, type GroupMLS, type GroupUnwrapResult, isMissingLedgerEntries, type PendingRecovery, } from './crypto.js';
|
|
12
12
|
export { asDeliveryPosition, asLogPosition, type DeliveryPosition, type LogPosition, } from './cursor.js';
|
|
13
|
+
export { PeerDisposedError } from './errors.js';
|
|
13
14
|
export { decodeHandshakeFrame, encodeHandshakeFrame, HANDSHAKE_KIND, HANDSHAKE_MAGIC, HANDSHAKE_VERSION, type HandshakeKind, } from './handshake.js';
|
|
14
15
|
export type { ReceiveLaneEnded, SubscribeFailure } from './hub-mux.js';
|
|
15
16
|
export { createLedgerEntryResolver, decodeLedgerEntries, encodeLedgerEntries, LEDGER_ENTRIES_VERSION, } from './ledger-entries.js';
|
package/lib/index.js
CHANGED
|
@@ -7,6 +7,7 @@ export { CommitDeadlineError, isHeadMismatch, JournalEpochError, RecoveryRequire
|
|
|
7
7
|
export { COMMIT_FRAME_VERSION, decodeCommitFrame, encodeCommitFrame, isUnsupportedCommitFrameVersion, UnsupportedCommitFrameVersionError } from './commit-frame.js';
|
|
8
8
|
export { isMissingLedgerEntries } from './crypto.js';
|
|
9
9
|
export { asDeliveryPosition, asLogPosition } from './cursor.js';
|
|
10
|
+
export { PeerDisposedError } from './errors.js';
|
|
10
11
|
export { decodeHandshakeFrame, encodeHandshakeFrame, HANDSHAKE_KIND, HANDSHAKE_MAGIC, HANDSHAKE_VERSION } from './handshake.js';
|
|
11
12
|
export { createLedgerEntryResolver, decodeLedgerEntries, encodeLedgerEntries, LEDGER_ENTRIES_VERSION } from './ledger-entries.js';
|
|
12
13
|
export { createGroupPeer } from './peer.js';
|
package/lib/peer.js
CHANGED
|
@@ -7,6 +7,7 @@ import { decodeCommitFrame, encodeCommitFrame, isUnsupportedCommitFrameVersion }
|
|
|
7
7
|
import { isMissingLedgerEntries } from './crypto.js';
|
|
8
8
|
import { asLogPosition } from './cursor.js';
|
|
9
9
|
import { createDirectedClient, createInboxAcceptor, createInboxPath } from './directed.js';
|
|
10
|
+
import { PeerDisposedError } from './errors.js';
|
|
10
11
|
import { adaptBusHandlers } from './handlers.js';
|
|
11
12
|
import { decodeHandshakeFrame, encodeHandshakeFrame, HANDSHAKE_KIND, HANDSHAKE_VERSION } from './handshake.js';
|
|
12
13
|
import { createHubMux } from './hub-mux.js';
|
|
@@ -332,6 +333,10 @@ export function createGroupPeer(params) {
|
|
|
332
333
|
const reasons = results.flatMap((r)=>r.status === 'rejected' ? [
|
|
333
334
|
r.reason
|
|
334
335
|
] : []);
|
|
336
|
+
// Defensive: no child rejects today — all are enkaku `Disposer`-based (a failing dispose is
|
|
337
|
+
// swallowed to `console.warn`, the promise still resolves) or a refcount decrement that cannot
|
|
338
|
+
// throw — so this guards a future non-`Disposer` child. Kept live because rotation shares this
|
|
339
|
+
// path, and a `BroadcastClient.prototype.dispose` spy forces it in test.
|
|
335
340
|
if (reasons.length > 0) {
|
|
336
341
|
throw new AggregateError(reasons, 'Group epoch teardown failed');
|
|
337
342
|
}
|
|
@@ -432,8 +437,9 @@ export function createGroupPeer(params) {
|
|
|
432
437
|
* The `resync` leak is LOCAL and invisible from the hub — do not re-derive it as re-subscribing.
|
|
433
438
|
* `resync`'s own comment carries the mechanism; read it there rather than restating it here.
|
|
434
439
|
*/ let disposed = false;
|
|
440
|
+
let disposePromise;
|
|
435
441
|
const assertLive = ()=>{
|
|
436
|
-
if (disposed) throw new
|
|
442
|
+
if (disposed) throw new PeerDisposedError('Peer is disposed');
|
|
437
443
|
};
|
|
438
444
|
let commitUnsubscribe;
|
|
439
445
|
let rendezvousUnsubscribe;
|
|
@@ -1247,13 +1253,16 @@ export function createGroupPeer(params) {
|
|
|
1247
1253
|
const timer = setTimeout(()=>finish(false), Math.max(0, deadline - Date.now()));
|
|
1248
1254
|
ledgerWaiters.set(requestID, (sealed)=>{
|
|
1249
1255
|
void (async ()=>{
|
|
1250
|
-
if (settled) return;
|
|
1256
|
+
if (settled || disposed) return;
|
|
1251
1257
|
try {
|
|
1252
1258
|
// Bytes this peer cannot open: another member's reply to another request, or a
|
|
1253
1259
|
// hub-injected forgery. Dropped, gather waits — the per-request key is NOT consumed
|
|
1254
1260
|
// here, since the next responder's reply is sealed to the same key.
|
|
1255
1261
|
const tokens = await port.openSealedLedger(sealed, requestID);
|
|
1256
1262
|
if (tokens == null) return;
|
|
1263
|
+
// Re-check: `disposed` can flip during the openSealedLedger await, and bootstrapLedger
|
|
1264
|
+
// REPLACES the host-owned ledger — it must not run against a torn-down handle.
|
|
1265
|
+
if (disposed) return;
|
|
1257
1266
|
await port.bootstrapLedger(tokens);
|
|
1258
1267
|
finish(true);
|
|
1259
1268
|
} catch {
|
|
@@ -1647,30 +1656,60 @@ export function createGroupPeer(params) {
|
|
|
1647
1656
|
await runSerial(()=>rebuildEpoch());
|
|
1648
1657
|
},
|
|
1649
1658
|
anchorEpoch: ()=>anchor.epoch,
|
|
1650
|
-
dispose:
|
|
1659
|
+
dispose: ()=>{
|
|
1660
|
+
if (disposePromise != null) return disposePromise;
|
|
1651
1661
|
disposed = true;
|
|
1652
|
-
//
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
//
|
|
1657
|
-
//
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1662
|
+
// Synchronous and FIRST, before anything is awaited: a lane op that already passed its own
|
|
1663
|
+
// `assertLive` can be running inside `runSerial`, past the point this `dispose()` can reach
|
|
1664
|
+
// it — awaiting the commit mutex here is unsafe (`build()`/`onAccepted()` are host-supplied
|
|
1665
|
+
// and unbounded, and a host calling `dispose()` from inside one would self-deadlock). This
|
|
1666
|
+
// closes the mux's three routes to the wire immediately, so whatever the op does next, it
|
|
1667
|
+
// cannot land a write. `mux.dispose()` — the full teardown — stays LAST, unchanged.
|
|
1668
|
+
mux.suspendPublishing();
|
|
1669
|
+
disposePromise = (async ()=>{
|
|
1670
|
+
// Tear down even a peer whose init failed — it still holds a hub drain.
|
|
1671
|
+
await settled;
|
|
1672
|
+
commitUnsubscribe?.();
|
|
1673
|
+
rendezvousUnsubscribe?.();
|
|
1674
|
+
// Resolve any in-flight recovery rendezvous FIRST, before clearing its timers: a
|
|
1675
|
+
// `recover()` blocked in `requestGroupInfo` is settled by exactly two things — a reply or
|
|
1676
|
+
// its timeout — and dispose is about to clear that timeout. Skipping this drain would hang
|
|
1677
|
+
// the heal, `commitTail`, and every lane operation queued behind it. Resolve, then clear,
|
|
1678
|
+
// so a fired timer cannot race a half-drained map. (The ledger gather needs no such drain:
|
|
1679
|
+
// its timeout is a local held in none of these maps.)
|
|
1680
|
+
for (const waiter of recoveryWaiters.values())waiter(null);
|
|
1681
|
+
recoveryWaiters.clear();
|
|
1682
|
+
for (const timer of recoveryTimers.values())clearTimeout(timer);
|
|
1683
|
+
for (const timer of pendingReplies.values())clearTimeout(timer);
|
|
1684
|
+
for (const timer of pendingLedgerReplies)clearTimeout(timer);
|
|
1685
|
+
recoveryTimers.clear();
|
|
1686
|
+
pendingReplies.clear();
|
|
1687
|
+
pendingLedgerReplies.clear();
|
|
1688
|
+
ledgerWaiters.clear();
|
|
1689
|
+
suppressedRequests.clear();
|
|
1690
|
+
// Independent failures, both surfaced: a teardown that failed must not stop the mux from
|
|
1691
|
+
// closing its hub drain, and a mux that failed must not hide a teardown failure that
|
|
1692
|
+
// happened first. `inboxLane` is cleared unconditionally after both, whichever way they
|
|
1693
|
+
// settled — `to()` has no lane left to build a directed client against either way.
|
|
1694
|
+
// Only `mux.dispose()` rejects in practice, and only on a synchronous `return()` throw (its
|
|
1695
|
+
// async close is fire-and-forget); `teardownEpoch()`'s arm is defensive (see its comment).
|
|
1696
|
+
// So the two-failure `AggregateError` is reachable only via the mux path today.
|
|
1697
|
+
const disposeErrors = [];
|
|
1698
|
+
try {
|
|
1699
|
+
await teardownEpoch();
|
|
1700
|
+
} catch (error) {
|
|
1701
|
+
disposeErrors.push(error);
|
|
1702
|
+
}
|
|
1703
|
+
try {
|
|
1704
|
+
await mux.dispose();
|
|
1705
|
+
} catch (error) {
|
|
1706
|
+
disposeErrors.push(error);
|
|
1707
|
+
}
|
|
1708
|
+
inboxLane = undefined;
|
|
1709
|
+
if (disposeErrors.length === 1) throw disposeErrors[0];
|
|
1710
|
+
if (disposeErrors.length > 1) throw new AggregateError(disposeErrors, 'Peer dispose failed');
|
|
1711
|
+
})();
|
|
1712
|
+
return disposePromise;
|
|
1674
1713
|
}
|
|
1675
1714
|
};
|
|
1676
1715
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kumiai/rpc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.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,25 +26,25 @@
|
|
|
26
26
|
"lib/*"
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@enkaku/client": "^0.21.
|
|
29
|
+
"@enkaku/client": "^0.21.1",
|
|
30
30
|
"@enkaku/protocol": "^0.21.0",
|
|
31
|
-
"@enkaku/server": "^0.21.
|
|
31
|
+
"@enkaku/server": "^0.21.2",
|
|
32
32
|
"@enkaku/transport": "^0.21.0",
|
|
33
|
-
"@
|
|
33
|
+
"@kumiai/broadcast": "^0.8.0",
|
|
34
|
+
"@kumiai/hub-protocol": "^0.8.0",
|
|
35
|
+
"@kumiai/hub-tunnel": "^0.8.0",
|
|
36
|
+
"@noble/hashes": "^2.4.0",
|
|
34
37
|
"@sozai/codec": "^0.4.0",
|
|
35
38
|
"@sozai/event": "^0.1.3",
|
|
36
39
|
"@sozai/log": "^0.3.1",
|
|
37
40
|
"@sozai/runtime": "^0.1.0",
|
|
38
|
-
"@sozai/schema": "^0.1.
|
|
39
|
-
"@kumiai/broadcast": "^0.7.0",
|
|
40
|
-
"@kumiai/hub-tunnel": "^0.7.0",
|
|
41
|
-
"@kumiai/hub-protocol": "^0.7.0"
|
|
41
|
+
"@sozai/schema": "^0.1.2"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@
|
|
45
|
-
"
|
|
46
|
-
"@
|
|
47
|
-
"
|
|
44
|
+
"@kumiai/hub-conformance": "^0.8.0",
|
|
45
|
+
"@kumiai/rpc-conformance": "^0.8.0",
|
|
46
|
+
"@noble/curves": "^2.4.0",
|
|
47
|
+
"vitest": "^4.1.11"
|
|
48
48
|
},
|
|
49
49
|
"publishConfig": {
|
|
50
50
|
"access": "public"
|