@kumiai/rpc 0.3.1 → 0.4.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/README.md +120 -0
- package/lib/anchor.d.ts +27 -0
- package/lib/anchor.js +18 -0
- package/lib/app-cursor.d.ts +63 -0
- package/lib/app-cursor.js +44 -0
- package/lib/app-lane.d.ts +78 -0
- package/lib/app-lane.js +401 -0
- package/lib/bus-server.d.ts +6 -6
- package/lib/bus-server.js +3 -9
- package/lib/classify.d.ts +94 -44
- package/lib/classify.js +35 -19
- package/lib/commit-frame.d.ts +40 -4
- package/lib/commit-frame.js +60 -13
- package/lib/commit.d.ts +11 -0
- package/lib/crypto.d.ts +275 -115
- package/lib/crypto.js +5 -6
- package/lib/cursor.d.ts +1 -1
- package/lib/cursor.js +1 -1
- package/lib/directed.d.ts +44 -5
- package/lib/directed.js +135 -60
- package/lib/handshake.d.ts +41 -4
- package/lib/handshake.js +40 -7
- package/lib/hub-mux.d.ts +94 -1
- package/lib/hub-mux.js +254 -11
- package/lib/index.d.ts +10 -6
- package/lib/index.js +6 -5
- package/lib/ledger-entries.d.ts +52 -8
- package/lib/ledger-entries.js +59 -23
- package/lib/open-once.d.ts +46 -0
- package/lib/open-once.js +63 -0
- package/lib/peer.d.ts +122 -54
- package/lib/peer.js +668 -308
- package/lib/protocol.d.ts +52 -0
- package/lib/protocol.js +22 -0
- package/lib/roster.d.ts +22 -0
- package/lib/roster.js +29 -0
- package/lib/topic.d.ts +20 -3
- package/lib/topic.js +20 -4
- package/package.json +17 -13
- package/lib/directed-crypto.d.ts +0 -17
- package/lib/directed-crypto.js +0 -80
package/lib/open-once.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ONE INBOUND PATH PER TOPIC, shared by every consumer built on it.
|
|
3
|
+
*
|
|
4
|
+
* OPENING IS A CONSUMING OPERATION. `unwrap` spends the frame's own per-message ratchet key on
|
|
5
|
+
* the handle it opens against, so the same bytes open exactly once and every later open of them
|
|
6
|
+
* fails — not because the frame is bad, but because its key is gone. The port says so (see
|
|
7
|
+
* {@link GroupCrypto.unwrap}); it cannot be duplicated, and a lane that gave two consumers an
|
|
8
|
+
* `unwrap` each would have them race for one key with the loser silently dropping the frame.
|
|
9
|
+
*
|
|
10
|
+
* So the frame is opened HERE, once, and the opened result is fanned out over plaintext.
|
|
11
|
+
*
|
|
12
|
+
* EVERY multi-consumer topic goes through this, not just the app lane. The self-inbox topic has
|
|
13
|
+
* the most consumers of any — an acceptor per protocol, plus one directed client per member being
|
|
14
|
+
* spoken to — and each holding its own `unwrap` is exactly why a directed request went unanswered
|
|
15
|
+
* over real MLS while the XOR fake, which could open the same bytes twice, stayed green.
|
|
16
|
+
*
|
|
17
|
+
* Opens are chained rather than launched per message, so frames are opened in arrival order.
|
|
18
|
+
* `unwrap` is async with variable latency, and a lane whose opens resolved out of order would
|
|
19
|
+
* feed a tunnel out of wire order — dropped as a stale seq — or double-create a session.
|
|
20
|
+
*
|
|
21
|
+
* Subscribed through the mux's raw inbound path rather than its bus view, because the bus view
|
|
22
|
+
* hands on the payload alone and a frame's log position is the one thing a lane could not
|
|
23
|
+
* otherwise write down.
|
|
24
|
+
*
|
|
25
|
+
* The mux subscription is held for as long as a consumer wants it and released with the last one,
|
|
26
|
+
* which is what keeps a rotation's teardown from leaving a listener behind on a topic the group
|
|
27
|
+
* has left.
|
|
28
|
+
*/ export function createOpenOncePath(params) {
|
|
29
|
+
const { mux, topicID, unwrap, project, note } = params;
|
|
30
|
+
const listeners = new Set();
|
|
31
|
+
let unsubscribe;
|
|
32
|
+
let opening = Promise.resolve();
|
|
33
|
+
return (onOpened)=>{
|
|
34
|
+
listeners.add(onOpened);
|
|
35
|
+
unsubscribe ??= mux.onInbound(topicID, (message)=>{
|
|
36
|
+
note?.(message);
|
|
37
|
+
opening = opening.then(async ()=>{
|
|
38
|
+
const result = await unwrap(message.payload);
|
|
39
|
+
const opened = result instanceof Uint8Array ? {
|
|
40
|
+
payload: result
|
|
41
|
+
} : result;
|
|
42
|
+
const value = project(message, opened);
|
|
43
|
+
if (value === undefined) return;
|
|
44
|
+
// Snapshot: a consumer disposing from inside its own delivery must not perturb the
|
|
45
|
+
// fan-out of the frame it is being given.
|
|
46
|
+
for (const listener of [
|
|
47
|
+
...listeners
|
|
48
|
+
])listener(value);
|
|
49
|
+
}).catch(()=>{
|
|
50
|
+
// A frame this handle cannot open — another epoch's, another group's, or not a frame at
|
|
51
|
+
// all. Ordinary on a shared log, and the read paths are built to walk past it. One
|
|
52
|
+
// frame's failure must not break the chain the rest are opened on.
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
return ()=>{
|
|
56
|
+
listeners.delete(onOpened);
|
|
57
|
+
if (listeners.size === 0) {
|
|
58
|
+
unsubscribe?.();
|
|
59
|
+
unsubscribe = undefined;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
}
|
package/lib/peer.d.ts
CHANGED
|
@@ -3,28 +3,63 @@ import type { ProtocolDefinition } from '@enkaku/protocol';
|
|
|
3
3
|
import type { ProcedureHandlers } from '@enkaku/server';
|
|
4
4
|
import { type GatheredReply, type GatherOptions, type RequestOptions, type SuppressConfig } from '@kumiai/broadcast';
|
|
5
5
|
import type { LogHub } from '@kumiai/hub-tunnel';
|
|
6
|
+
import { type Runtime } from '@sozai/runtime';
|
|
7
|
+
import type { AnchorStore } from './anchor.js';
|
|
8
|
+
import type { AppCursorStore, AppWindowPruned } from './app-cursor.js';
|
|
6
9
|
import { type CommitJournal, type LaneResult, type PendingCommit } from './commit.js';
|
|
7
10
|
import { type GroupCrypto, type GroupMLS } from './crypto.js';
|
|
11
|
+
import { type ReceiveLaneEnded, type SubscribeFailure } from './hub-mux.js';
|
|
8
12
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
+
* How long the hub is asked to keep the commit log: **28 days**. Bounds how long a member may be
|
|
14
|
+
* offline and still converge by pulling alone, without another member awake to heal it.
|
|
15
|
+
*
|
|
16
|
+
* Deliberately below the reference hub ceiling (`createMemoryStore`'s `DEFAULT_MAX_RETENTION`, 30
|
|
17
|
+
* days): a hub refuses a retention above its ceiling rather than clamping it, so a default sitting
|
|
18
|
+
* exactly on the ceiling would leave no room for an upward override — refused outright, and the
|
|
19
|
+
* peer not a subscriber of its own commit topic. Asserted in `peer-control-lanes.test.ts`, since a
|
|
20
|
+
* tighter operator cap still refuses this default (reported via `hub-mux`).
|
|
21
|
+
*/
|
|
22
|
+
export declare const DEFAULT_COMMIT_LOG_RETENTION_SECONDS: number;
|
|
23
|
+
/**
|
|
24
|
+
* How long the hub is asked to keep an app topic's log. Aligned to the commit window so a
|
|
25
|
+
* returning member never rebuilds its membership without also recovering its messages. A separate
|
|
26
|
+
* dial, not the commit one reused — the alignment is a choice a host may override.
|
|
27
|
+
*/
|
|
28
|
+
export declare const DEFAULT_APP_LOG_RETENTION_SECONDS: number;
|
|
29
|
+
/**
|
|
30
|
+
* The MLS half of a peer: the lifecycle port, the durable journal, the restart-adopt hook, and the
|
|
31
|
+
* durable anchor/cursor stores. They arrive together or not at all — each missing piece fails
|
|
32
|
+
* SILENTLY: no journal loses a commit whose process died mid-acceptance; no anchor store re-seeds
|
|
33
|
+
* at the live epoch and silently partitions from the group; no cursor store re-reads app history
|
|
34
|
+
* from the hub's retention floor every restart. The type is what stops a host wiring only some.
|
|
13
35
|
*/
|
|
14
36
|
export type GroupPeerMLSParams = {
|
|
15
37
|
/** MLS lifecycle port. When provided, the peer runs the commit lane. */
|
|
16
38
|
mls: GroupMLS;
|
|
17
39
|
/** Durable single-slot journal. Written before every publish, cleared on both outcomes. */
|
|
18
40
|
journal: CommitJournal;
|
|
41
|
+
/**
|
|
42
|
+
* Durable store for the app-lane anchor. Written on every rotation, read once at construction.
|
|
43
|
+
* Persisted, not derived — see {@link anchor} for why it cannot be re-derived from the handle.
|
|
44
|
+
*/
|
|
45
|
+
anchorStore: AnchorStore;
|
|
46
|
+
/**
|
|
47
|
+
* Durable read position for the app lane, per topic. Written as each drain finishes, read as
|
|
48
|
+
* each segment is pulled — what lets a returning peer resume from where it got to, and the only
|
|
49
|
+
* thing a below-retention gap can be detected against (the gap IS the distance between the two).
|
|
50
|
+
*
|
|
51
|
+
* The drain may only advance it past a frame it is done with: delivered, or sealed at an epoch
|
|
52
|
+
* this peer can never hold again. See {@link "app-cursor".AppCursorStore}.
|
|
53
|
+
*/
|
|
54
|
+
appCursorStore: AppCursorStore;
|
|
19
55
|
/**
|
|
20
56
|
* Adopt a journalled commit now confirmed accepted — the restart half of
|
|
21
|
-
* {@link PendingCommit.onAccepted}
|
|
22
|
-
*
|
|
57
|
+
* {@link PendingCommit.onAccepted}: deserialize the post-commit handle, adopt it, deliver any
|
|
58
|
+
* Welcome it carried.
|
|
23
59
|
*
|
|
24
|
-
* MUST be idempotent,
|
|
25
|
-
* already ran from one whose process died before it. The Welcome
|
|
26
|
-
*
|
|
27
|
-
* repeat, and what absorbs it.
|
|
60
|
+
* MUST be idempotent, like `onAccepted`: the peer cannot tell an entry whose `onAccepted`
|
|
61
|
+
* already ran from one whose process died before it. The Welcome resend is at-least-once by
|
|
62
|
+
* design — see {@link PendingCommit.onAccepted}.
|
|
28
63
|
*/
|
|
29
64
|
adoptJournalled: (journal: Uint8Array) => Promise<void>;
|
|
30
65
|
};
|
|
@@ -37,7 +72,8 @@ export type GroupPeerParams<Protocols extends Record<string, ProtocolDefinition>
|
|
|
37
72
|
[K in keyof Protocols]: ProcedureHandlers<Protocols[K]>;
|
|
38
73
|
};
|
|
39
74
|
suppress?: SuppressConfig;
|
|
40
|
-
|
|
75
|
+
/** Runtime providing platform primitives. Defaults to `createRuntime()`. */
|
|
76
|
+
runtime?: Runtime;
|
|
41
77
|
/**
|
|
42
78
|
* Recovery rendezvous tuning. `timeoutMs`: how long one request waits for a reply. `getDelayMs`:
|
|
43
79
|
* responder reply jitter. `deadlineMs`: how long `recover()` keeps re-requesting and rebuilding
|
|
@@ -49,20 +85,60 @@ export type GroupPeerParams<Protocols extends Record<string, ProtocolDefinition>
|
|
|
49
85
|
deadlineMs?: number;
|
|
50
86
|
};
|
|
51
87
|
/**
|
|
52
|
-
* Commit-log retention the hub is asked to hold, in seconds. Default
|
|
53
|
-
*
|
|
54
|
-
* must heal it.
|
|
88
|
+
* Commit-log retention the hub is asked to hold, in seconds. Default 28 days — see
|
|
89
|
+
* {@link DEFAULT_COMMIT_LOG_RETENTION_SECONDS}. A liveness dial: within it a returning member
|
|
90
|
+
* converges by pulling the log; beyond it, another live member must heal it.
|
|
55
91
|
*/
|
|
56
92
|
commitLogRetentionSeconds?: number;
|
|
93
|
+
/**
|
|
94
|
+
* App-log retention the hub is asked to hold, in seconds. Default 28 days — see
|
|
95
|
+
* {@link DEFAULT_APP_LOG_RETENTION_SECONDS}. Overridable up to the hub operator's own cap.
|
|
96
|
+
*/
|
|
97
|
+
appLogRetentionSeconds?: number;
|
|
57
98
|
/**
|
|
58
99
|
* How long `commit` rebases before giving up, in ms. Default 30s. Losing a compare-and-set is
|
|
59
100
|
* the expected path, not an error path.
|
|
60
101
|
*/
|
|
61
102
|
commitDeadlineMs?: number;
|
|
103
|
+
/**
|
|
104
|
+
* Called when the app lane finds a gap below the hub's retention floor: frames published to a
|
|
105
|
+
* topic this peer had a read position on, and aged out before it came back for them.
|
|
106
|
+
*
|
|
107
|
+
* OPTIONAL: unlike the stores above, a host that ignores this loses no message — the frames
|
|
108
|
+
* that survived are still delivered — it only turns an absence the host could not see into one
|
|
109
|
+
* it can.
|
|
110
|
+
*
|
|
111
|
+
* Fire-and-forget: a throw is swallowed and the drain carries on.
|
|
112
|
+
*/
|
|
113
|
+
onAppWindowPruned?: (event: AppWindowPruned) => void | Promise<void>;
|
|
114
|
+
/**
|
|
115
|
+
* Called when the hub definitively refuses to subscribe this peer to a topic — most plausibly a
|
|
116
|
+
* retention setting above the operator's own cap, which a hub refuses rather than clamps.
|
|
117
|
+
*
|
|
118
|
+
* Optional only because it is not the enforcement: every publish and fetch on a refused topic
|
|
119
|
+
* throws (see {@link "hub-mux".createHubMux}), so a host that wires nothing still cannot mistake
|
|
120
|
+
* such a peer for a healthy one. This is how a host learns PROMPTLY, and the only way a
|
|
121
|
+
* read-only peer on that topic learns at all.
|
|
122
|
+
*
|
|
123
|
+
* Fire-and-forget: a throw is swallowed.
|
|
124
|
+
*/
|
|
125
|
+
onSubscribeFailed?: (failure: SubscribeFailure) => void;
|
|
126
|
+
/**
|
|
127
|
+
* The push lane has ended and nothing will restart it. See {@link "hub-mux".ReceiveLaneEnded}.
|
|
128
|
+
*
|
|
129
|
+
* The connection belongs to the HOST, not the peer, so only the host can reconnect — without
|
|
130
|
+
* this the ending is invisible, and a dead lane looks like a group with nothing to say. A host
|
|
131
|
+
* that reconnects should build a new peer over the new connection.
|
|
132
|
+
*
|
|
133
|
+
* Not called on `dispose`. Fire-and-forget: a throw is swallowed.
|
|
134
|
+
*/
|
|
135
|
+
onReceiveEnded?: (ended: ReceiveLaneEnded) => void;
|
|
62
136
|
} & (GroupPeerMLSParams | {
|
|
63
137
|
mls?: undefined;
|
|
64
138
|
journal?: undefined;
|
|
65
139
|
adoptJournalled?: undefined;
|
|
140
|
+
anchorStore?: undefined;
|
|
141
|
+
appCursorStore?: undefined;
|
|
66
142
|
});
|
|
67
143
|
export type ProtocolSurface<Protocol extends ProtocolDefinition> = {
|
|
68
144
|
dispatch: (prc: string, data?: Record<string, unknown>) => Promise<void>;
|
|
@@ -75,67 +151,59 @@ export type GroupPeer<Protocols extends Record<string, ProtocolDefinition>> = {
|
|
|
75
151
|
/**
|
|
76
152
|
* Commit to the group, rebasing until it lands.
|
|
77
153
|
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
* `build()` produces a commit against the host's CURRENT handle and does not adopt it. Called
|
|
84
|
-
* once per attempt; it MUST read the host's live handle each time and have no side effects until
|
|
85
|
-
* `onAccepted` runs — a losing attempt is discarded whole.
|
|
154
|
+
* Replays the journal, pulls the log to the end, calls `build()`, journals the result, and
|
|
155
|
+
* publishes conditionally on the head it pulled to. Lose (someone committed first): drop the
|
|
156
|
+
* pending commit untouched and call `build()` again against the now-current handle — expected,
|
|
157
|
+
* not an error. `build()` must read the host's live handle each attempt and have no side effects
|
|
158
|
+
* until `onAccepted` runs, since a losing attempt is discarded whole.
|
|
86
159
|
*
|
|
87
|
-
* Holds the commit mutex for its whole run
|
|
88
|
-
* not two callers on one: two `build()` calls against a single handle would both frame at that
|
|
89
|
-
* handle's epoch and diverge.
|
|
160
|
+
* Holds the commit mutex for its whole run, so two `build()` calls never race one handle.
|
|
90
161
|
*
|
|
91
|
-
* A RESULT means it landed and `onAccepted` ran; a THROW means it did not — stranded
|
|
92
|
-
* incomplete ({@link "commit".RecoveryRequiredError}), or lost
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
* {@link replay} after a throw to collect it.
|
|
162
|
+
* A RESULT means it landed and `onAccepted` ran; a THROW means it did not — stranded, ledger
|
|
163
|
+
* incomplete ({@link "commit".RecoveryRequiredError}), or deadline lost
|
|
164
|
+
* ({@link "commit".CommitDeadlineError}). Call {@link replay} after a throw to collect any
|
|
165
|
+
* undrained `lost` / `reenact` work.
|
|
96
166
|
*/
|
|
97
167
|
commit: (build: () => Promise<PendingCommit>) => Promise<LaneResult>;
|
|
98
168
|
/**
|
|
99
169
|
* Replay the journal on its own, for startup: republish any pending commit under its original
|
|
100
|
-
* idempotency key and hand back what did not survive.
|
|
101
|
-
*
|
|
102
|
-
* else, and the collector to call after a `commit()` that threw.
|
|
170
|
+
* idempotency key and hand back what did not survive. The host's collector to call before
|
|
171
|
+
* anything else, and after a `commit()` that threw.
|
|
103
172
|
*
|
|
104
|
-
* Builds and publishes nothing, so
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* commit lives on `commit()`, and `replay()` is the retry that eventually clears it.
|
|
173
|
+
* Builds and publishes nothing, so an incomplete ledger is no hazard here — it retries the
|
|
174
|
+
* bootstrap and returns WITHOUT throwing, leaving the peer degraded until a responder answers.
|
|
175
|
+
* A `{}` result means "no orphaned work to re-issue", never "the peer is whole" — the
|
|
176
|
+
* completeness gate lives on `commit()`.
|
|
109
177
|
*/
|
|
110
178
|
replay: () => Promise<LaneResult>;
|
|
111
179
|
/**
|
|
112
180
|
* Heal a peer the group has left behind: rejoin by external commit, refold the ledger, hand
|
|
113
181
|
* back the entries the group's ledger does not already hold.
|
|
114
182
|
*
|
|
115
|
-
* A TOP-LEVEL lane operation
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* already changed, so rebuilding from it would publish a commit no member can apply.
|
|
183
|
+
* A TOP-LEVEL lane operation, never called from inside another — takes the commit mutex itself.
|
|
184
|
+
* The external commit races at the head like any commit; losing (the likely outcome) discards
|
|
185
|
+
* the GroupInfo too, since it describes a tree the winner already changed and a commit rebuilt
|
|
186
|
+
* from it is one no member can apply.
|
|
120
187
|
*
|
|
121
|
-
* A heal is TWO commits: the rejoin carries no entries
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
* contain it. The ledger does not dedup — a re-appended entry WINS the fold — so re-enacting one
|
|
126
|
-
* the group already holds silently reverts whatever a later admin wrote over it.
|
|
188
|
+
* A heal is TWO commits: the rejoin carries no entries, so the entries this peer still owes ride
|
|
189
|
+
* an ordinary `commit()` the CALLER makes after this releases the lane — `reenact`, filtered by
|
|
190
|
+
* MEMBERSHIP: re-enact an entry iff the group's ledger does not already hold it (the ledger does
|
|
191
|
+
* not dedup, so re-enacting a held entry would revert a later admin's write).
|
|
127
192
|
*
|
|
128
|
-
* `{ advanced: false }` when no member answers the
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
* A peer merely BEHIND never needs this — it pulls the log and catches up.
|
|
193
|
+
* `{ advanced: false }` when no member answers, or the rejoin landed but the ledger could not be
|
|
194
|
+
* bootstrapped (an incomplete ledger is a reset roster; reporting it healed would hand the host a
|
|
195
|
+
* group with every role gone). A peer merely BEHIND never needs this — it pulls and catches up.
|
|
133
196
|
*/
|
|
134
197
|
recover: () => Promise<{
|
|
135
198
|
advanced: boolean;
|
|
136
199
|
reenact: Array<string>;
|
|
137
200
|
}>;
|
|
138
201
|
resync: () => Promise<void>;
|
|
202
|
+
/**
|
|
203
|
+
* The epoch the app-lane anchor sits at — see {@link anchor} for the rotation rule. Exposed so a
|
|
204
|
+
* caller can observe a roster change being detected without reaching into the port.
|
|
205
|
+
*/
|
|
206
|
+
anchorEpoch: () => number;
|
|
139
207
|
dispose: () => Promise<void>;
|
|
140
208
|
};
|
|
141
209
|
export declare function createGroupPeer<Protocols extends Record<string, ProtocolDefinition>>(params: GroupPeerParams<Protocols>): GroupPeer<Protocols>;
|