@kumiai/rpc 0.3.1 → 0.4.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 +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/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 +664 -306
- 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/directed.js
CHANGED
|
@@ -1,29 +1,117 @@
|
|
|
1
1
|
import { Client } from '@enkaku/client';
|
|
2
2
|
import { Server } from '@enkaku/server';
|
|
3
|
-
import { defaultRandomID } from '@kumiai/broadcast';
|
|
4
3
|
import { createHubTunnelTransport, decodeFrame } from '@kumiai/hub-tunnel';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { createRuntime } from '@sozai/runtime';
|
|
5
|
+
import { createOpenOncePath } from './open-once.js';
|
|
6
|
+
/**
|
|
7
|
+
* The one path that opens an inbox topic's frames, for every consumer of it.
|
|
8
|
+
*
|
|
9
|
+
* A frame that opens without an authenticated sender is dropped HERE. The hub-asserted sender is
|
|
10
|
+
* never a fallback — directed RPC binds every session to the sender the open recovered, which is
|
|
11
|
+
* what stops a lying hub forging or splicing one.
|
|
12
|
+
*/ export function createInboxPath(params) {
|
|
13
|
+
const { mux, topicID, unwrap } = params;
|
|
14
|
+
return createOpenOncePath({
|
|
15
|
+
mux,
|
|
16
|
+
topicID,
|
|
17
|
+
unwrap,
|
|
18
|
+
project: (message, opened)=>opened.senderDID == null ? undefined : {
|
|
19
|
+
sequenceID: message.sequenceID,
|
|
20
|
+
senderDID: opened.senderDID,
|
|
21
|
+
topicID: message.topicID,
|
|
22
|
+
payload: opened.payload
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
7
26
|
/**
|
|
8
27
|
* Directed 1:1 RPC client to a single member, over a hub-tunnel transport whose
|
|
9
28
|
* send/receive topics are the two members' inbox topics for the current epoch.
|
|
29
|
+
*
|
|
30
|
+
* Replies are authored by `memberDID`, so anything the shared path opens under a different
|
|
31
|
+
* MLS-authenticated sender belongs to another conversation on the same inbox and is left for
|
|
32
|
+
* the consumer it does belong to.
|
|
10
33
|
*/ export function createDirectedClient(params) {
|
|
11
|
-
const { mux, localDID, memberDID,
|
|
12
|
-
const getRandomID = params.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
34
|
+
const { mux, localDID, memberDID, sendTopicID, receiveTopicID, inbound, wrap } = params;
|
|
35
|
+
const { getRandomID } = params.runtime ?? createRuntime();
|
|
36
|
+
let unsubscribe;
|
|
37
|
+
const hub = {
|
|
38
|
+
async publish (publishParams) {
|
|
39
|
+
return mux.mailbox.publish({
|
|
40
|
+
senderDID: publishParams.senderDID,
|
|
41
|
+
topicID: publishParams.topicID,
|
|
42
|
+
payload: await wrap(publishParams.payload)
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
subscribe () {},
|
|
46
|
+
unsubscribe () {},
|
|
47
|
+
receive () {
|
|
48
|
+
const queue = [];
|
|
49
|
+
let resolveNext;
|
|
50
|
+
let closed = false;
|
|
51
|
+
const close = ()=>{
|
|
52
|
+
closed = true;
|
|
53
|
+
unsubscribe?.();
|
|
54
|
+
unsubscribe = undefined;
|
|
55
|
+
if (resolveNext != null) {
|
|
56
|
+
const resolve = resolveNext;
|
|
57
|
+
resolveNext = undefined;
|
|
58
|
+
resolve({
|
|
59
|
+
value: undefined,
|
|
60
|
+
done: true
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
unsubscribe = inbound((message)=>{
|
|
65
|
+
if (closed || message.topicID !== receiveTopicID || message.senderDID !== memberDID) return;
|
|
66
|
+
if (resolveNext != null) {
|
|
67
|
+
const resolve = resolveNext;
|
|
68
|
+
resolveNext = undefined;
|
|
69
|
+
resolve({
|
|
70
|
+
value: message,
|
|
71
|
+
done: false
|
|
72
|
+
});
|
|
73
|
+
} else {
|
|
74
|
+
queue.push(message);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
const iterator = {
|
|
78
|
+
next () {
|
|
79
|
+
if (queue.length > 0) {
|
|
80
|
+
return Promise.resolve({
|
|
81
|
+
value: queue.shift(),
|
|
82
|
+
done: false
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
if (closed) {
|
|
86
|
+
return Promise.resolve({
|
|
87
|
+
value: undefined,
|
|
88
|
+
done: true
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return new Promise((resolve)=>{
|
|
92
|
+
resolveNext = resolve;
|
|
93
|
+
});
|
|
94
|
+
},
|
|
95
|
+
return () {
|
|
96
|
+
close();
|
|
97
|
+
return Promise.resolve({
|
|
98
|
+
value: undefined,
|
|
99
|
+
done: true
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
return {
|
|
104
|
+
[Symbol.asyncIterator]: ()=>iterator,
|
|
105
|
+
return: close
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
};
|
|
21
109
|
const transport = createHubTunnelTransport({
|
|
22
|
-
hub
|
|
110
|
+
hub,
|
|
23
111
|
sessionID: getRandomID(),
|
|
24
112
|
localDID,
|
|
25
|
-
sendTopicID
|
|
26
|
-
receiveTopicID
|
|
113
|
+
sendTopicID,
|
|
114
|
+
receiveTopicID
|
|
27
115
|
});
|
|
28
116
|
const client = new Client({
|
|
29
117
|
transport,
|
|
@@ -33,14 +121,11 @@ import { inboxTopic } from './topic.js';
|
|
|
33
121
|
client,
|
|
34
122
|
dispose: async ()=>{
|
|
35
123
|
await client.dispose();
|
|
124
|
+
unsubscribe?.();
|
|
125
|
+
unsubscribe = undefined;
|
|
36
126
|
}
|
|
37
127
|
};
|
|
38
128
|
}
|
|
39
|
-
function normalizeUnwrap(result) {
|
|
40
|
-
return result instanceof Uint8Array ? {
|
|
41
|
-
payload: result
|
|
42
|
-
} : result;
|
|
43
|
-
}
|
|
44
129
|
/**
|
|
45
130
|
* Accept directed RPC. A single sealed drain of `selfInboxTopic` opens each inbound frame with
|
|
46
131
|
* `unwrap`, binds every session to the MLS-authenticated sender recovered from the ciphertext,
|
|
@@ -48,7 +133,7 @@ function normalizeUnwrap(result) {
|
|
|
48
133
|
* with `wrap`. Frames whose recovered sender does not match the session binding are dropped, so
|
|
49
134
|
* a malicious hub can neither read the lane nor forge/splice a sender.
|
|
50
135
|
*/ export function createInboxAcceptor(params) {
|
|
51
|
-
const { mux, localDID, selfInboxTopic, resolveSendTopic, protocol, handlers, wrap
|
|
136
|
+
const { mux, localDID, selfInboxTopic, inbound, resolveSendTopic, protocol, handlers, wrap } = params;
|
|
52
137
|
const server = new Server({
|
|
53
138
|
protocol,
|
|
54
139
|
handlers,
|
|
@@ -165,45 +250,35 @@ function normalizeUnwrap(result) {
|
|
|
165
250
|
}
|
|
166
251
|
};
|
|
167
252
|
};
|
|
168
|
-
//
|
|
169
|
-
//
|
|
170
|
-
//
|
|
171
|
-
|
|
172
|
-
const unsubscribe =
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
frame
|
|
185
|
-
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
const existing = sessions.get(frame.sessionID);
|
|
189
|
-
if (frame.kind === 'session-end') {
|
|
190
|
-
if (existing != null && existing.senderDID === senderDID) {
|
|
191
|
-
sessions.delete(frame.sessionID);
|
|
192
|
-
void existing.dispose();
|
|
193
|
-
}
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
if (frame.kind !== 'message') return;
|
|
197
|
-
if (existing != null) {
|
|
198
|
-
if (existing.senderDID === senderDID) existing.feed(opened.payload);
|
|
199
|
-
return; // sender mismatch on an established session — splice attempt, drop
|
|
253
|
+
// The frame arrives already opened, and the sender is the one the open authenticated — a lying
|
|
254
|
+
// hub can neither read this lane nor forge a sender into it. Arrival order is the open path's
|
|
255
|
+
// guarantee: it opens one frame at a time, so a session is never double-created and a tunnel is
|
|
256
|
+
// never fed out of wire order (which drops as a stale seq).
|
|
257
|
+
const unsubscribe = inbound((message)=>{
|
|
258
|
+
if (message.topicID !== selfInboxTopic) return;
|
|
259
|
+
const senderDID = message.senderDID;
|
|
260
|
+
let frame;
|
|
261
|
+
try {
|
|
262
|
+
frame = decodeFrame(message.payload);
|
|
263
|
+
} catch {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const existing = sessions.get(frame.sessionID);
|
|
267
|
+
if (frame.kind === 'session-end') {
|
|
268
|
+
if (existing != null && existing.senderDID === senderDID) {
|
|
269
|
+
sessions.delete(frame.sessionID);
|
|
270
|
+
void existing.dispose();
|
|
200
271
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (frame.kind !== 'message') return;
|
|
275
|
+
if (existing != null) {
|
|
276
|
+
if (existing.senderDID === senderDID) existing.feed(message.payload);
|
|
277
|
+
return; // sender mismatch on an established session — splice attempt, drop
|
|
278
|
+
}
|
|
279
|
+
const session = createSession(senderDID);
|
|
280
|
+
sessions.set(frame.sessionID, session);
|
|
281
|
+
session.feed(message.payload);
|
|
207
282
|
});
|
|
208
283
|
return {
|
|
209
284
|
dispose: async ()=>{
|
package/lib/handshake.d.ts
CHANGED
|
@@ -10,9 +10,39 @@
|
|
|
10
10
|
*/
|
|
11
11
|
/** Leading marker identifying a handshake frame ("EK"). */
|
|
12
12
|
export declare const HANDSHAKE_MAGIC: Uint8Array<ArrayBuffer>;
|
|
13
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* Current handshake wire-format version.
|
|
15
|
+
*
|
|
16
|
+
* BUMPING THIS IS SURVIVABLE, but only because of what the reader does with it, not the byte
|
|
17
|
+
* itself: {@link decodeHandshakeFrame} hands an unknown version back rather than throwing, and on
|
|
18
|
+
* the COMMIT topic the lane files it as {@link "classify".CommitDisposition} `ahead` — the group
|
|
19
|
+
* moved to a format this build can't read, so step over the frame and heal. That heal only
|
|
20
|
+
* GUARANTEES a loud stall in place of a silent one; landing the rejoin additionally needs the new
|
|
21
|
+
* build to still answer old peers' rendezvous requests in a version they can read.
|
|
22
|
+
*
|
|
23
|
+
* It has to be `ahead`, not poison: poison only works while SOME frames stay readable, and after
|
|
24
|
+
* a bump none do — there's no "next frame" to heal from, so the peer drains to the end of the
|
|
25
|
+
* log and reports itself reconciled at a dead epoch, silently, permanently. The heal direction is
|
|
26
|
+
* also the safe one: a forged unknown-version frame can only TRIGGER a heal, never suppress one —
|
|
27
|
+
* the same asymmetry the `ahead` row accepts on a cleartext epoch.
|
|
28
|
+
*
|
|
29
|
+
* STILL PREFER PUTTING A FORMAT CHANGE INSIDE THE PAYLOAD — but "the payload" isn't one place.
|
|
30
|
+
* The commit frame ({@link "commit-frame".COMMIT_FRAME_VERSION}) does NOT get the cheap outcome:
|
|
31
|
+
* its version byte is read before the commit bytes are extracted, so a bump there strands every
|
|
32
|
+
* old peer exactly as a bump here does, and routes to the same heal. Only the sealed ledger-entry
|
|
33
|
+
* blob gets the cheap outcome, because its failure lands after the commit is read (poisons one
|
|
34
|
+
* commit, heals from the next). Bump this only when the HEADER itself changes, and only alongside
|
|
35
|
+
* a responder that keeps answering old peers' rendezvous requests — without that, the heal above
|
|
36
|
+
* is decorative.
|
|
37
|
+
*/
|
|
14
38
|
export declare const HANDSHAKE_VERSION = 1;
|
|
15
|
-
/**
|
|
39
|
+
/**
|
|
40
|
+
* The message kinds carried on the handshake lane. {@link decodeHandshakeFrame} THROWS on a kind
|
|
41
|
+
* byte not listed here, before the caller learns the frame's version — and on the commit topic a
|
|
42
|
+
* throw is poison, not the heal. So no future version may publish a kind this enum lacks:
|
|
43
|
+
* `commit` stays `0` forever, and a new commit-topic message needs its kind added HERE, not just
|
|
44
|
+
* allowed by a version bump. Deliberate — kind is validated before version.
|
|
45
|
+
*/
|
|
16
46
|
export declare const HANDSHAKE_KIND: {
|
|
17
47
|
/** An MLS Commit fanned out to advance every member's epoch. */
|
|
18
48
|
readonly commit: 0;
|
|
@@ -33,10 +63,17 @@ export type HandshakeKind = (typeof HANDSHAKE_KIND)[keyof typeof HANDSHAKE_KIND]
|
|
|
33
63
|
/** Wrap a payload with the magic + version + kind header. */
|
|
34
64
|
export declare function encodeHandshakeFrame(kind: HandshakeKind, payload: Uint8Array): Uint8Array;
|
|
35
65
|
/**
|
|
36
|
-
* Validate the header and split a frame into its kind and payload. Throws on a
|
|
37
|
-
*
|
|
66
|
+
* Validate the header and split a frame into its version, kind and payload. Throws on a short
|
|
67
|
+
* frame, a bad magic, or an unknown kind — bytes that are not this protocol at all.
|
|
68
|
+
*
|
|
69
|
+
* DOES NOT throw on an unsupported VERSION; it returns it, since the right answer differs by
|
|
70
|
+
* lane and only the caller knows which lane it's on. Every caller MUST compare `version` against
|
|
71
|
+
* {@link HANDSHAKE_VERSION} before trusting `payload` — see {@link HANDSHAKE_VERSION} for what an
|
|
72
|
+
* unknown one means on the commit topic versus everywhere else.
|
|
38
73
|
*/
|
|
39
74
|
export declare function decodeHandshakeFrame(frame: Uint8Array): {
|
|
75
|
+
/** The frame's wire version. Equal to {@link HANDSHAKE_VERSION} for a frame this build reads. */
|
|
76
|
+
version: number;
|
|
40
77
|
kind: HandshakeKind;
|
|
41
78
|
payload: Uint8Array;
|
|
42
79
|
};
|
package/lib/handshake.js
CHANGED
|
@@ -11,9 +11,39 @@
|
|
|
11
11
|
0x45,
|
|
12
12
|
0x4b
|
|
13
13
|
]);
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* Current handshake wire-format version.
|
|
16
|
+
*
|
|
17
|
+
* BUMPING THIS IS SURVIVABLE, but only because of what the reader does with it, not the byte
|
|
18
|
+
* itself: {@link decodeHandshakeFrame} hands an unknown version back rather than throwing, and on
|
|
19
|
+
* the COMMIT topic the lane files it as {@link "classify".CommitDisposition} `ahead` — the group
|
|
20
|
+
* moved to a format this build can't read, so step over the frame and heal. That heal only
|
|
21
|
+
* GUARANTEES a loud stall in place of a silent one; landing the rejoin additionally needs the new
|
|
22
|
+
* build to still answer old peers' rendezvous requests in a version they can read.
|
|
23
|
+
*
|
|
24
|
+
* It has to be `ahead`, not poison: poison only works while SOME frames stay readable, and after
|
|
25
|
+
* a bump none do — there's no "next frame" to heal from, so the peer drains to the end of the
|
|
26
|
+
* log and reports itself reconciled at a dead epoch, silently, permanently. The heal direction is
|
|
27
|
+
* also the safe one: a forged unknown-version frame can only TRIGGER a heal, never suppress one —
|
|
28
|
+
* the same asymmetry the `ahead` row accepts on a cleartext epoch.
|
|
29
|
+
*
|
|
30
|
+
* STILL PREFER PUTTING A FORMAT CHANGE INSIDE THE PAYLOAD — but "the payload" isn't one place.
|
|
31
|
+
* The commit frame ({@link "commit-frame".COMMIT_FRAME_VERSION}) does NOT get the cheap outcome:
|
|
32
|
+
* its version byte is read before the commit bytes are extracted, so a bump there strands every
|
|
33
|
+
* old peer exactly as a bump here does, and routes to the same heal. Only the sealed ledger-entry
|
|
34
|
+
* blob gets the cheap outcome, because its failure lands after the commit is read (poisons one
|
|
35
|
+
* commit, heals from the next). Bump this only when the HEADER itself changes, and only alongside
|
|
36
|
+
* a responder that keeps answering old peers' rendezvous requests — without that, the heal above
|
|
37
|
+
* is decorative.
|
|
38
|
+
*/ export const HANDSHAKE_VERSION = 1;
|
|
15
39
|
const HEADER_LENGTH = HANDSHAKE_MAGIC.length + 2;
|
|
16
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* The message kinds carried on the handshake lane. {@link decodeHandshakeFrame} THROWS on a kind
|
|
42
|
+
* byte not listed here, before the caller learns the frame's version — and on the commit topic a
|
|
43
|
+
* throw is poison, not the heal. So no future version may publish a kind this enum lacks:
|
|
44
|
+
* `commit` stays `0` forever, and a new commit-topic message needs its kind added HERE, not just
|
|
45
|
+
* allowed by a version bump. Deliberate — kind is validated before version.
|
|
46
|
+
*/ export const HANDSHAKE_KIND = {
|
|
17
47
|
/** An MLS Commit fanned out to advance every member's epoch. */ commit: 0,
|
|
18
48
|
/** A stranded peer asking the group for current state. */ recoveryRequest: 1,
|
|
19
49
|
/** A member's reply carrying the state needed to re-sync. */ recoveryReply: 2,
|
|
@@ -37,8 +67,13 @@ function isHandshakeKind(value) {
|
|
|
37
67
|
return frame;
|
|
38
68
|
}
|
|
39
69
|
/**
|
|
40
|
-
* Validate the header and split a frame into its kind and payload. Throws on a
|
|
41
|
-
*
|
|
70
|
+
* Validate the header and split a frame into its version, kind and payload. Throws on a short
|
|
71
|
+
* frame, a bad magic, or an unknown kind — bytes that are not this protocol at all.
|
|
72
|
+
*
|
|
73
|
+
* DOES NOT throw on an unsupported VERSION; it returns it, since the right answer differs by
|
|
74
|
+
* lane and only the caller knows which lane it's on. Every caller MUST compare `version` against
|
|
75
|
+
* {@link HANDSHAKE_VERSION} before trusting `payload` — see {@link HANDSHAKE_VERSION} for what an
|
|
76
|
+
* unknown one means on the commit topic versus everywhere else.
|
|
42
77
|
*/ export function decodeHandshakeFrame(frame) {
|
|
43
78
|
if (frame.length < HEADER_LENGTH) {
|
|
44
79
|
throw new Error('handshake frame is too short');
|
|
@@ -47,14 +82,12 @@ function isHandshakeKind(value) {
|
|
|
47
82
|
throw new Error('handshake frame has a bad magic');
|
|
48
83
|
}
|
|
49
84
|
const version = frame[HANDSHAKE_MAGIC.length];
|
|
50
|
-
if (version !== HANDSHAKE_VERSION) {
|
|
51
|
-
throw new Error(`unsupported handshake version: ${version}`);
|
|
52
|
-
}
|
|
53
85
|
const kind = frame[HANDSHAKE_MAGIC.length + 1];
|
|
54
86
|
if (!isHandshakeKind(kind)) {
|
|
55
87
|
throw new Error(`unknown handshake kind: ${kind}`);
|
|
56
88
|
}
|
|
57
89
|
return {
|
|
90
|
+
version,
|
|
58
91
|
kind,
|
|
59
92
|
payload: frame.subarray(HEADER_LENGTH)
|
|
60
93
|
};
|
package/lib/hub-mux.d.ts
CHANGED
|
@@ -1,11 +1,63 @@
|
|
|
1
1
|
import type { BroadcastBus } from '@kumiai/broadcast';
|
|
2
|
-
import type
|
|
2
|
+
import { type StoredMessage } from '@kumiai/hub-protocol';
|
|
3
3
|
import type { HubFetchTopicResult, HubSubscribeOptions, LogHub, MailboxHub } from '@kumiai/hub-tunnel';
|
|
4
|
+
/**
|
|
5
|
+
* A subscribe the hub refused for good — either a permanent refusal (it has answered, and the
|
|
6
|
+
* answer will not change) or a transient one the retry schedule ran out on. Either way this peer
|
|
7
|
+
* is NOT a subscriber of `topicID`: nothing is pushed to it there and it cannot pull it.
|
|
8
|
+
*/
|
|
9
|
+
export type SubscribeFailure = {
|
|
10
|
+
topicID: string;
|
|
11
|
+
error: unknown;
|
|
12
|
+
/**
|
|
13
|
+
* True when the hub settled it (e.g. RetentionExceededError) and no retry was attempted; false
|
|
14
|
+
* when it was retried to the end of the schedule and never succeeded.
|
|
15
|
+
*/
|
|
16
|
+
permanent: boolean;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The push lane has ended, and it will not restart on its own.
|
|
20
|
+
*
|
|
21
|
+
* `receive` is one long-lived channel and this mux drains it once. When it ends — the hub refused
|
|
22
|
+
* it, the connection died, the server replaced it with a newer one for the same DID — nothing
|
|
23
|
+
* downstream is told: every listener simply stops being called, and a peer that is only READING
|
|
24
|
+
* looks identical to a peer whose group has gone quiet. That silence is the defect. The host owns
|
|
25
|
+
* the connection, so the host is the only thing that can reconnect it; this is how it finds out
|
|
26
|
+
* it has to.
|
|
27
|
+
*/
|
|
28
|
+
export type ReceiveLaneEnded = {
|
|
29
|
+
/** What the drain threw, or `undefined` when the channel simply ended. */
|
|
30
|
+
error?: unknown;
|
|
31
|
+
};
|
|
4
32
|
export type HubMuxParams = {
|
|
5
33
|
/** The real hub. It must serve a log: the commit lane reads one. */
|
|
6
34
|
hub: LogHub;
|
|
7
35
|
/** Authenticated DID used to drain `hub.receive` and stamp bus publishes. */
|
|
8
36
|
localDID: string;
|
|
37
|
+
/**
|
|
38
|
+
* Called once per topic the hub has definitively refused. A NOTICE, not the enforcement: the
|
|
39
|
+
* enforcement is that every publish and fetch on a refused topic throws (see
|
|
40
|
+
* {@link createHubMux}). This exists because a peer that only READS a topic calls nothing that
|
|
41
|
+
* could throw, so without it a pure consumer would sit silent — which is the whole defect.
|
|
42
|
+
*
|
|
43
|
+
* Fire-and-forget; a throw here is swallowed rather than allowed to kill the retry path.
|
|
44
|
+
*
|
|
45
|
+
* Omitted, the failure is warned to the console instead of passing silently — see
|
|
46
|
+
* {@link warnSubscribeFailed}. Pass an empty handler to silence it deliberately.
|
|
47
|
+
*/
|
|
48
|
+
onSubscribeFailed?: (failure: SubscribeFailure) => void;
|
|
49
|
+
/**
|
|
50
|
+
* Called once when the push lane ends, for any reason. See {@link ReceiveLaneEnded}. Not called
|
|
51
|
+
* on `dispose` — an ending the caller asked for is not news.
|
|
52
|
+
*
|
|
53
|
+
* Fire-and-forget: a throw here is swallowed rather than allowed to escape into the drain.
|
|
54
|
+
*
|
|
55
|
+
* Omitted, the condition is warned to the console instead of passing silently — see
|
|
56
|
+
* {@link warnReceiveEnded}. Pass an empty handler to silence it deliberately.
|
|
57
|
+
*/
|
|
58
|
+
onReceiveEnded?: (ended: ReceiveLaneEnded) => void;
|
|
59
|
+
/** Backoff schedule for transient subscribe failures. Default {@link DEFAULT_SUBSCRIBE_RETRY_DELAYS_MS}. */
|
|
60
|
+
subscribeRetryDelaysMs?: ReadonlyArray<number>;
|
|
9
61
|
};
|
|
10
62
|
/**
|
|
11
63
|
* An onInbound listener. `ack` marks the message durably handled so a durable
|
|
@@ -40,6 +92,18 @@ export type HubMux = {
|
|
|
40
92
|
publish: (params: MuxPublishParams) => Promise<{
|
|
41
93
|
sequenceID: string;
|
|
42
94
|
}>;
|
|
95
|
+
/**
|
|
96
|
+
* Subscribe the local DID to a topic with NO listener, and never release it. The hub gates a
|
|
97
|
+
* topic fetch on the caller's own subscription, so a reader that only wants to PULL a topic
|
|
98
|
+
* still has to be a subscriber of it — and it is also what asks the hub to hold the log.
|
|
99
|
+
*
|
|
100
|
+
* Not paired with a release, deliberately: an app topic is subscribed for the member's whole
|
|
101
|
+
* life (a rotation tears down listeners, never subscriptions), so there is no later moment
|
|
102
|
+
* this could correctly be undone at. Idempotent over a topic that is HELD or being asked for —
|
|
103
|
+
* that one is not re-subscribed, so `options` are the FIRST caller's. A topic the hub REFUSED
|
|
104
|
+
* is not held, so a later retain asks again, carrying its own `options`.
|
|
105
|
+
*/
|
|
106
|
+
retainTopic: (topicID: string, options?: HubSubscribeOptions) => void;
|
|
43
107
|
/** Pull a topic's log as the local DID. */
|
|
44
108
|
fetchTopic: (params: MuxFetchTopicParams) => Promise<HubFetchTopicResult>;
|
|
45
109
|
onInbound: (topicID: string, listener: InboundListener, options?: HubSubscribeOptions) => () => void;
|
|
@@ -59,6 +123,35 @@ export type HubMux = {
|
|
|
59
123
|
* and still receive the triggering frame. Topics are refcounted across all three views; the
|
|
60
124
|
* first registration subscribes on the hub.
|
|
61
125
|
*
|
|
126
|
+
* ## A subscribe the hub refuses
|
|
127
|
+
*
|
|
128
|
+
* The hub may say no — `RetentionExceededError` when a host asks to hold a log longer than the
|
|
129
|
+
* operator allows, refused rather than clamped on purpose. A peer that is not a subscriber of a
|
|
130
|
+
* topic is pushed nothing on it and cannot pull it: no commit applies, no app frame arrives. That
|
|
131
|
+
* failure has to be impossible to miss, and there are three candidate surfaces:
|
|
132
|
+
*
|
|
133
|
+
* - **Retry with backoff** answers a dropped socket and nothing else. Retrying a settled refusal
|
|
134
|
+
* is a busy loop against an answer that will not change, so it is the answer for TRANSIENT
|
|
135
|
+
* failures only. Used, bounded, and never for a permanent one.
|
|
136
|
+
* - **Raising into the caller** cannot be the whole answer: `retainTopic` is called for its effect
|
|
137
|
+
* and returns before the hub has answered, and the callers that would catch it (the commit-lane
|
|
138
|
+
* seed, the app-segment load) are the ones already written to swallow.
|
|
139
|
+
* - **A host callback** is optional by nature, and an optional notice cannot be the guarantee: an
|
|
140
|
+
* unwired host would be exactly as blind as today.
|
|
141
|
+
*
|
|
142
|
+
* So the guarantee is a LATCH, and the callback is a convenience on top of it. A refused topic is
|
|
143
|
+
* recorded as refused, and every `publish`, `bus.publish`, `mailbox.publish` and `fetchTopic` on it
|
|
144
|
+
* throws the hub's own error. A peer that cannot receive on a topic does not go on transmitting
|
|
145
|
+
* there as though it were whole — it fails at the first operation that touches the topic, with the
|
|
146
|
+
* reason rather than the `NotSubscribedError` symptom. That is what makes it impossible for a peer
|
|
147
|
+
* whose subscribe was refused to report itself healthy, whatever the host wired.
|
|
148
|
+
*
|
|
149
|
+
* `onSubscribeFailed` exists on top because a peer that only READS a topic calls nothing that
|
|
150
|
+
* could throw. The latch cannot reach it; the notice can.
|
|
151
|
+
*
|
|
152
|
+
* A refused topic is not held, so the next `retain` asks again — a host that lowered its retention
|
|
153
|
+
* recovers on the next rotation without restarting.
|
|
154
|
+
*
|
|
62
155
|
* **The refcount tracks LOCAL LISTENERS and never unsubscribes.** A subscription is a durable
|
|
63
156
|
* member-topic relationship, not a session: the hub holds a subscriber's undelivered frames,
|
|
64
157
|
* and `unsubscribe` tells the store to drop them and free any mailbox frame it was the last
|