@kumiai/rpc 0.10.2 → 0.10.4
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/anchor.d.ts +15 -14
- package/lib/anchor.js +2 -12
- package/lib/app-cursor.d.ts +5 -8
- package/lib/app-cursor.js +3 -6
- package/lib/app-lane.js +16 -21
- package/lib/app-outbox.d.ts +57 -0
- package/lib/app-outbox.js +97 -0
- package/lib/commit.d.ts +6 -0
- package/lib/crypto.d.ts +67 -16
- package/lib/errors.d.ts +15 -0
- package/lib/errors.js +20 -0
- package/lib/frame-size.d.ts +8 -0
- package/lib/frame-size.js +13 -0
- package/lib/handshake.d.ts +2 -0
- package/lib/handshake.js +7 -1
- package/lib/host-boundary.d.ts +109 -0
- package/lib/host-boundary.js +258 -0
- package/lib/hub-mux.d.ts +2 -4
- package/lib/hub-mux.js +33 -12
- package/lib/index.d.ts +8 -5
- package/lib/index.js +4 -2
- package/lib/log-delivery.d.ts +46 -0
- package/lib/log-delivery.js +177 -0
- package/lib/peer-epoch.d.ts +49 -0
- package/lib/peer-epoch.js +387 -0
- package/lib/peer-types.d.ts +343 -0
- package/lib/peer-types.js +26 -0
- package/lib/peer.d.ts +4 -305
- package/lib/peer.js +1037 -669
- package/lib/recovery-confirmation.d.ts +63 -0
- package/lib/recovery-confirmation.js +132 -0
- package/lib/recovery.d.ts +14 -0
- package/lib/recovery.js +72 -8
- package/package.json +11 -11
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { GatherOptions } from '@kumiai/broadcast';
|
|
2
|
+
import type { PendingCommit } from './commit.js';
|
|
3
|
+
import type { PendingRecovery } from './crypto.js';
|
|
4
|
+
export type HostBoundary = {
|
|
5
|
+
wrap<TValue extends object>(value: TValue): TValue;
|
|
6
|
+
close(): void;
|
|
7
|
+
drained(teardown: Promise<unknown>): Promise<void>;
|
|
8
|
+
};
|
|
9
|
+
export declare const pendingRecoveryHostMembers: {
|
|
10
|
+
readonly epoch: "data";
|
|
11
|
+
readonly markBindingUnusable: "host";
|
|
12
|
+
readonly confirmationKey: "host";
|
|
13
|
+
readonly judgeVerdict: "host";
|
|
14
|
+
readonly commit: "data";
|
|
15
|
+
readonly onAccepted: "host";
|
|
16
|
+
};
|
|
17
|
+
export declare const pendingCommitHostMembers: {
|
|
18
|
+
readonly commit: "data";
|
|
19
|
+
readonly bodies: "data";
|
|
20
|
+
readonly kind: "data";
|
|
21
|
+
readonly journal: "data";
|
|
22
|
+
readonly onAccepted: "host";
|
|
23
|
+
};
|
|
24
|
+
export declare const cryptoHostMembers: {
|
|
25
|
+
readonly epoch: "host";
|
|
26
|
+
readonly exportSecret: "host";
|
|
27
|
+
readonly wrap: "host";
|
|
28
|
+
readonly unwrap: "host";
|
|
29
|
+
readonly frameAAD: "host";
|
|
30
|
+
readonly frameEpoch: "host";
|
|
31
|
+
readonly sealEntries: "host";
|
|
32
|
+
readonly openEntries: "host";
|
|
33
|
+
readonly pending: "port";
|
|
34
|
+
};
|
|
35
|
+
export declare const pendingFrameHostMembers: {
|
|
36
|
+
readonly list: "host";
|
|
37
|
+
readonly complete: "host";
|
|
38
|
+
};
|
|
39
|
+
export declare const mlsHostMembers: {
|
|
40
|
+
readonly verifyRecoveryRequest: "host";
|
|
41
|
+
readonly confirmationKey: "host";
|
|
42
|
+
readonly sealRecoveryVerdict: "host";
|
|
43
|
+
readonly openRecoveryVerdict: "host";
|
|
44
|
+
readonly sendAdmission: "host";
|
|
45
|
+
readonly readEpoch: "host";
|
|
46
|
+
readonly rosterEntries: "host";
|
|
47
|
+
readonly readCommitHeader: "host";
|
|
48
|
+
readonly processCommit: "host";
|
|
49
|
+
readonly prepareRecovery: "host";
|
|
50
|
+
readonly createRecoveryRequest: "host";
|
|
51
|
+
readonly sealGroupInfo: "host";
|
|
52
|
+
readonly applyRecovery: "host";
|
|
53
|
+
readonly isLedgerComplete: "host";
|
|
54
|
+
readonly sealLedger: "host";
|
|
55
|
+
readonly openSealedLedger: "host";
|
|
56
|
+
readonly bootstrapLedger: "host";
|
|
57
|
+
readonly getLedger: "host";
|
|
58
|
+
readonly exportRecoverySecret: "host";
|
|
59
|
+
};
|
|
60
|
+
export declare const peerHostFields: {
|
|
61
|
+
readonly hub: "transport";
|
|
62
|
+
readonly runtime: "runtime";
|
|
63
|
+
readonly crypto: "crypto";
|
|
64
|
+
readonly mls: "mls";
|
|
65
|
+
readonly journal: "port";
|
|
66
|
+
readonly anchorStore: "port";
|
|
67
|
+
readonly appCursorStore: "port";
|
|
68
|
+
readonly appOutbox: "port";
|
|
69
|
+
readonly appOutboxLimit: "data";
|
|
70
|
+
readonly onAppOutboxCleared: "callback";
|
|
71
|
+
readonly adoptJournalled: "callback";
|
|
72
|
+
readonly handlers: "handlers";
|
|
73
|
+
readonly recovery: "recovery";
|
|
74
|
+
readonly localDID: "data";
|
|
75
|
+
readonly protocols: "data";
|
|
76
|
+
readonly suppress: "data";
|
|
77
|
+
readonly commitLogRetentionSeconds: "data";
|
|
78
|
+
readonly appLogRetentionSeconds: "data";
|
|
79
|
+
readonly commitDeadlineMs: "data";
|
|
80
|
+
readonly onAppWindowPruned: "callback";
|
|
81
|
+
readonly onStrand: "callback";
|
|
82
|
+
readonly onRecovery: "callback";
|
|
83
|
+
readonly onAppDeliveryStalled: "callback";
|
|
84
|
+
readonly onAppDeliveryResumed: "callback";
|
|
85
|
+
readonly onSubscribeFailed: "callback";
|
|
86
|
+
readonly onReceiveEnded: "callback";
|
|
87
|
+
};
|
|
88
|
+
export declare const anchorStoreHostMembers: {
|
|
89
|
+
readonly load: "host";
|
|
90
|
+
readonly save: "host";
|
|
91
|
+
};
|
|
92
|
+
export declare const gatherHostMembers: {
|
|
93
|
+
readonly quorum: "data";
|
|
94
|
+
readonly timeoutMs: "data";
|
|
95
|
+
readonly signal: "data";
|
|
96
|
+
readonly onReply: "host";
|
|
97
|
+
};
|
|
98
|
+
export declare function wrapGatherOptions(boundary: HostBoundary, options: GatherOptions): GatherOptions;
|
|
99
|
+
export declare function createHostBoundary(): HostBoundary;
|
|
100
|
+
export declare function wrapPendingCommit(boundary: HostBoundary, value: PendingCommit): PendingCommit;
|
|
101
|
+
export declare function wrapPendingRecovery(boundary: HostBoundary, value: PendingRecovery): PendingRecovery;
|
|
102
|
+
export declare function wrapPeerHost<TParams extends object>(boundary: HostBoundary, params: TParams): TParams;
|
|
103
|
+
export declare const commitBuildHostMembers: {
|
|
104
|
+
readonly build: "host";
|
|
105
|
+
};
|
|
106
|
+
export declare function wrapCommitBuild(boundary: HostBoundary, build: () => Promise<PendingCommit>): () => Promise<PendingCommit>;
|
|
107
|
+
export declare const journalSerializationHostMembers: {
|
|
108
|
+
readonly serializeJournal: "host";
|
|
109
|
+
};
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { PeerDisposedError } from './errors.js';
|
|
2
|
+
// New recovery methods must be classified alongside the existing acceptance callback.
|
|
3
|
+
export const pendingRecoveryHostMembers = {
|
|
4
|
+
epoch: 'data',
|
|
5
|
+
markBindingUnusable: 'host',
|
|
6
|
+
confirmationKey: 'host',
|
|
7
|
+
judgeVerdict: 'host',
|
|
8
|
+
commit: 'data',
|
|
9
|
+
onAccepted: 'host'
|
|
10
|
+
};
|
|
11
|
+
export const pendingCommitHostMembers = {
|
|
12
|
+
commit: 'data',
|
|
13
|
+
bodies: 'data',
|
|
14
|
+
kind: 'data',
|
|
15
|
+
journal: 'data',
|
|
16
|
+
onAccepted: 'host'
|
|
17
|
+
};
|
|
18
|
+
export const cryptoHostMembers = {
|
|
19
|
+
epoch: 'host',
|
|
20
|
+
exportSecret: 'host',
|
|
21
|
+
wrap: 'host',
|
|
22
|
+
unwrap: 'host',
|
|
23
|
+
frameAAD: 'host',
|
|
24
|
+
frameEpoch: 'host',
|
|
25
|
+
sealEntries: 'host',
|
|
26
|
+
openEntries: 'host',
|
|
27
|
+
pending: 'port'
|
|
28
|
+
};
|
|
29
|
+
export const pendingFrameHostMembers = {
|
|
30
|
+
list: 'host',
|
|
31
|
+
complete: 'host'
|
|
32
|
+
};
|
|
33
|
+
export const mlsHostMembers = {
|
|
34
|
+
verifyRecoveryRequest: 'host',
|
|
35
|
+
confirmationKey: 'host',
|
|
36
|
+
sealRecoveryVerdict: 'host',
|
|
37
|
+
openRecoveryVerdict: 'host',
|
|
38
|
+
sendAdmission: 'host',
|
|
39
|
+
readEpoch: 'host',
|
|
40
|
+
rosterEntries: 'host',
|
|
41
|
+
readCommitHeader: 'host',
|
|
42
|
+
processCommit: 'host',
|
|
43
|
+
prepareRecovery: 'host',
|
|
44
|
+
createRecoveryRequest: 'host',
|
|
45
|
+
sealGroupInfo: 'host',
|
|
46
|
+
applyRecovery: 'host',
|
|
47
|
+
isLedgerComplete: 'host',
|
|
48
|
+
sealLedger: 'host',
|
|
49
|
+
openSealedLedger: 'host',
|
|
50
|
+
bootstrapLedger: 'host',
|
|
51
|
+
getLedger: 'host',
|
|
52
|
+
exportRecoverySecret: 'host'
|
|
53
|
+
};
|
|
54
|
+
export const peerHostFields = {
|
|
55
|
+
hub: 'transport',
|
|
56
|
+
runtime: 'runtime',
|
|
57
|
+
crypto: 'crypto',
|
|
58
|
+
mls: 'mls',
|
|
59
|
+
journal: 'port',
|
|
60
|
+
anchorStore: 'port',
|
|
61
|
+
appCursorStore: 'port',
|
|
62
|
+
appOutbox: 'port',
|
|
63
|
+
appOutboxLimit: 'data',
|
|
64
|
+
onAppOutboxCleared: 'callback',
|
|
65
|
+
adoptJournalled: 'callback',
|
|
66
|
+
handlers: 'handlers',
|
|
67
|
+
recovery: 'recovery',
|
|
68
|
+
localDID: 'data',
|
|
69
|
+
protocols: 'data',
|
|
70
|
+
suppress: 'data',
|
|
71
|
+
commitLogRetentionSeconds: 'data',
|
|
72
|
+
appLogRetentionSeconds: 'data',
|
|
73
|
+
commitDeadlineMs: 'data',
|
|
74
|
+
onAppWindowPruned: 'callback',
|
|
75
|
+
onStrand: 'callback',
|
|
76
|
+
onRecovery: 'callback',
|
|
77
|
+
onAppDeliveryStalled: 'callback',
|
|
78
|
+
onAppDeliveryResumed: 'callback',
|
|
79
|
+
onSubscribeFailed: 'callback',
|
|
80
|
+
onReceiveEnded: 'callback'
|
|
81
|
+
};
|
|
82
|
+
export const anchorStoreHostMembers = {
|
|
83
|
+
load: 'host',
|
|
84
|
+
save: 'host'
|
|
85
|
+
};
|
|
86
|
+
export const gatherHostMembers = {
|
|
87
|
+
quorum: 'data',
|
|
88
|
+
timeoutMs: 'data',
|
|
89
|
+
signal: 'data',
|
|
90
|
+
onReply: 'host'
|
|
91
|
+
};
|
|
92
|
+
export function wrapGatherOptions(boundary, options) {
|
|
93
|
+
return selectHostMembers(boundary, options, gatherHostMembers);
|
|
94
|
+
}
|
|
95
|
+
export function createHostBoundary() {
|
|
96
|
+
let closed = false;
|
|
97
|
+
let active = 0;
|
|
98
|
+
const idle = new Set();
|
|
99
|
+
const views = new WeakMap();
|
|
100
|
+
const receivers = new WeakMap();
|
|
101
|
+
const finish = ()=>{
|
|
102
|
+
active--;
|
|
103
|
+
if (active === 0) {
|
|
104
|
+
for (const resolve of idle)resolve();
|
|
105
|
+
idle.clear();
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const invoke = (callback, receiver, args)=>{
|
|
109
|
+
if (closed) throw new PeerDisposedError('Host boundary is closed');
|
|
110
|
+
active++;
|
|
111
|
+
try {
|
|
112
|
+
const result = Reflect.apply(callback, receiver, args);
|
|
113
|
+
const then = result == null ? undefined : result.then;
|
|
114
|
+
if (typeof then === 'function') {
|
|
115
|
+
const settled = new Promise((resolve, reject)=>{
|
|
116
|
+
Reflect.apply(then, result, [
|
|
117
|
+
resolve,
|
|
118
|
+
reject
|
|
119
|
+
]);
|
|
120
|
+
});
|
|
121
|
+
void settled.then(finish, finish);
|
|
122
|
+
return settled;
|
|
123
|
+
}
|
|
124
|
+
finish();
|
|
125
|
+
return result;
|
|
126
|
+
} catch (error) {
|
|
127
|
+
finish();
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
function wrap(value) {
|
|
132
|
+
const cached = views.get(value);
|
|
133
|
+
if (cached != null) return cached;
|
|
134
|
+
let view;
|
|
135
|
+
if (typeof value === 'function') {
|
|
136
|
+
view = new Proxy(value, {
|
|
137
|
+
apply: (target, receiver, args)=>invoke(target, receiver, args)
|
|
138
|
+
});
|
|
139
|
+
} else {
|
|
140
|
+
// A separate target avoids frozen-property proxy invariants on host ports.
|
|
141
|
+
view = new Proxy({}, {
|
|
142
|
+
get (_target, key) {
|
|
143
|
+
let owner = value;
|
|
144
|
+
let descriptor;
|
|
145
|
+
while(owner != null && descriptor == null){
|
|
146
|
+
descriptor = Reflect.getOwnPropertyDescriptor(owner, key);
|
|
147
|
+
owner = Reflect.getPrototypeOf(owner);
|
|
148
|
+
}
|
|
149
|
+
const member = descriptor?.get == null ? Reflect.get(value, key, value) : invoke(descriptor.get, value, []);
|
|
150
|
+
if (typeof member !== 'function') return member;
|
|
151
|
+
let methods = receivers.get(value);
|
|
152
|
+
if (methods == null) {
|
|
153
|
+
methods = new Map();
|
|
154
|
+
receivers.set(value, methods);
|
|
155
|
+
}
|
|
156
|
+
const callback = member;
|
|
157
|
+
const cachedMethod = methods.get(callback);
|
|
158
|
+
if (cachedMethod != null) return cachedMethod;
|
|
159
|
+
const method = views.get(callback) === callback ? (...args)=>Reflect.apply(callback, value, args) : (...args)=>invoke(callback, value, args);
|
|
160
|
+
methods.set(callback, method);
|
|
161
|
+
views.set(method, method);
|
|
162
|
+
return method;
|
|
163
|
+
},
|
|
164
|
+
ownKeys: ()=>Reflect.ownKeys(value),
|
|
165
|
+
getOwnPropertyDescriptor: (_target, key)=>{
|
|
166
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(value, key);
|
|
167
|
+
return descriptor == null ? undefined : {
|
|
168
|
+
configurable: true,
|
|
169
|
+
enumerable: descriptor.enumerable
|
|
170
|
+
};
|
|
171
|
+
},
|
|
172
|
+
getPrototypeOf: ()=>Reflect.getPrototypeOf(value)
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
views.set(value, view);
|
|
176
|
+
views.set(view, view);
|
|
177
|
+
return view;
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
wrap,
|
|
181
|
+
close: ()=>{
|
|
182
|
+
closed = true;
|
|
183
|
+
},
|
|
184
|
+
drained: async (teardown)=>{
|
|
185
|
+
if (!closed) throw new Error('Host boundary must be closed before draining');
|
|
186
|
+
await teardown.catch(()=>{});
|
|
187
|
+
if (active !== 0) await new Promise((resolve)=>{
|
|
188
|
+
idle.add(resolve);
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
function selectHostMembers(boundary, value, members) {
|
|
194
|
+
const wrapped = boundary.wrap(value);
|
|
195
|
+
const view = {};
|
|
196
|
+
for (const [key, kind] of Object.entries(members)){
|
|
197
|
+
Object.defineProperty(view, key, {
|
|
198
|
+
enumerable: true,
|
|
199
|
+
configurable: true,
|
|
200
|
+
get: ()=>Reflect.get(kind === 'host' ? wrapped : value, key)
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
return view;
|
|
204
|
+
}
|
|
205
|
+
export function wrapPendingCommit(boundary, value) {
|
|
206
|
+
return selectHostMembers(boundary, value, pendingCommitHostMembers);
|
|
207
|
+
}
|
|
208
|
+
export function wrapPendingRecovery(boundary, value) {
|
|
209
|
+
return selectHostMembers(boundary, value, pendingRecoveryHostMembers);
|
|
210
|
+
}
|
|
211
|
+
export function wrapPeerHost(boundary, params) {
|
|
212
|
+
const wrapped = boundary.wrap(params);
|
|
213
|
+
const view = {};
|
|
214
|
+
for (const [key, kind] of Object.entries(peerHostFields)){
|
|
215
|
+
if (kind === 'data' || kind === 'transport' || kind === 'runtime' || kind === 'callback') {
|
|
216
|
+
Object.defineProperty(view, key, {
|
|
217
|
+
enumerable: true,
|
|
218
|
+
get: ()=>Reflect.get(kind === 'callback' ? wrapped : params, key)
|
|
219
|
+
});
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const value = Reflect.get(wrapped, key);
|
|
223
|
+
if (value == null) continue;
|
|
224
|
+
let selected;
|
|
225
|
+
if (kind === 'crypto') {
|
|
226
|
+
const crypto = value;
|
|
227
|
+
selected = selectHostMembers(boundary, crypto, cryptoHostMembers);
|
|
228
|
+
Object.defineProperty(selected, 'pending', {
|
|
229
|
+
enumerable: true,
|
|
230
|
+
value: crypto.pending == null ? undefined : selectHostMembers(boundary, crypto.pending, pendingFrameHostMembers)
|
|
231
|
+
});
|
|
232
|
+
} else if (key === 'anchorStore') selected = selectHostMembers(boundary, value, anchorStoreHostMembers);
|
|
233
|
+
else if (kind === 'mls') selected = selectHostMembers(boundary, value, mlsHostMembers);
|
|
234
|
+
else if (kind === 'handlers') {
|
|
235
|
+
selected = Object.fromEntries(Object.entries(value).map(([name, handlers])=>[
|
|
236
|
+
name,
|
|
237
|
+
boundary.wrap(handlers)
|
|
238
|
+
]));
|
|
239
|
+
} else selected = boundary.wrap(value);
|
|
240
|
+
Reflect.set(view, key, selected);
|
|
241
|
+
}
|
|
242
|
+
return view;
|
|
243
|
+
}
|
|
244
|
+
// Nested publisher callbacks, including serializeJournal, drain with the build invocation.
|
|
245
|
+
// Journal bytes must restore the derived handle and its ledger through adoptJournalled.
|
|
246
|
+
export const commitBuildHostMembers = {
|
|
247
|
+
build: 'host'
|
|
248
|
+
};
|
|
249
|
+
export function wrapCommitBuild(boundary, build) {
|
|
250
|
+
return selectHostMembers(boundary, {
|
|
251
|
+
build
|
|
252
|
+
}, commitBuildHostMembers).build;
|
|
253
|
+
}
|
|
254
|
+
// Invoked inside the wrapped build; the whole serializer promise belongs to that host call.
|
|
255
|
+
// Its bytes must let adoptJournalled restore the derived state INCLUDING its ledger.
|
|
256
|
+
export const journalSerializationHostMembers = {
|
|
257
|
+
serializeJournal: 'host'
|
|
258
|
+
};
|
package/lib/hub-mux.d.ts
CHANGED
|
@@ -172,9 +172,7 @@ export type HubMux = {
|
|
|
172
172
|
* ## The init-race window
|
|
173
173
|
*
|
|
174
174
|
* The drain starts synchronously in this constructor, before the caller registers the first
|
|
175
|
-
* listener. A frame arriving in that window
|
|
176
|
-
*
|
|
177
|
-
* be the exact false success this relay exists to prevent. Options for closing the window itself:
|
|
178
|
-
* `docs/agents/plans/backlog/2026-07-07-rpc-peer-lifecycle-hardening.md`.
|
|
175
|
+
* listener. A frame arriving in that window is handed to the first topic listener within the ack
|
|
176
|
+
* TTL. Expiry drops it unacked so it returns on redelivery rather than reporting false success.
|
|
179
177
|
*/
|
|
180
178
|
export declare function createHubMux(params: HubMuxParams): HubMux;
|
package/lib/hub-mux.js
CHANGED
|
@@ -2,6 +2,7 @@ import { AuthorizationDeniedError, RetentionExceededError } from '@kumiai/hub-pr
|
|
|
2
2
|
import { getReporter } from '@sozai/log';
|
|
3
3
|
import { asDeliveryPosition } from './cursor.js';
|
|
4
4
|
import { PeerDisposedError } from './errors.js';
|
|
5
|
+
import { assertFrameFits } from './frame-size.js';
|
|
5
6
|
/**
|
|
6
7
|
* Backoff for a TRANSIENT subscribe failure, in ms. Short enough that an ordinary reconnect blip
|
|
7
8
|
* heals before the first lane operation, bounded so a hub that is answering "no" in a shape this
|
|
@@ -69,10 +70,8 @@ const warnReceiveEnded = (ended)=>{
|
|
|
69
70
|
* ## The init-race window
|
|
70
71
|
*
|
|
71
72
|
* The drain starts synchronously in this constructor, before the caller registers the first
|
|
72
|
-
* listener. A frame arriving in that window
|
|
73
|
-
*
|
|
74
|
-
* be the exact false success this relay exists to prevent. Options for closing the window itself:
|
|
75
|
-
* `docs/agents/plans/backlog/2026-07-07-rpc-peer-lifecycle-hardening.md`.
|
|
73
|
+
* listener. A frame arriving in that window is handed to the first topic listener within the ack
|
|
74
|
+
* TTL. Expiry drops it unacked so it returns on redelivery rather than reporting false success.
|
|
76
75
|
*/ export function createHubMux(params) {
|
|
77
76
|
const { hub, localDID } = params;
|
|
78
77
|
const onSubscribeFailed = params.onSubscribeFailed ?? warnSubscribeFailed;
|
|
@@ -253,6 +252,29 @@ const warnReceiveEnded = (ended)=>{
|
|
|
253
252
|
}
|
|
254
253
|
set.add(listener);
|
|
255
254
|
retain(topicID, options);
|
|
255
|
+
sweepPending(Date.now());
|
|
256
|
+
const handoffs = [];
|
|
257
|
+
for (const [sequenceID, entry] of pending){
|
|
258
|
+
const message = entry.unmatched;
|
|
259
|
+
if (message?.topicID !== topicID) continue;
|
|
260
|
+
// Claim the whole backlog before callbacks can register another listener.
|
|
261
|
+
entry.unmatched = undefined;
|
|
262
|
+
entry.holders.clear();
|
|
263
|
+
entry.holders.add(listener);
|
|
264
|
+
handoffs.push({
|
|
265
|
+
sequenceID,
|
|
266
|
+
entry,
|
|
267
|
+
message
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
for (const { sequenceID, entry, message } of handoffs){
|
|
271
|
+
if (disposed || pending.get(sequenceID) !== entry) continue;
|
|
272
|
+
try {
|
|
273
|
+
listener(message, ()=>releaseClaim(sequenceID, listener));
|
|
274
|
+
} catch {
|
|
275
|
+
// listener errors must not break registration
|
|
276
|
+
}
|
|
277
|
+
}
|
|
256
278
|
let removed = false;
|
|
257
279
|
return ()=>{
|
|
258
280
|
if (removed) return;
|
|
@@ -304,7 +326,7 @@ const warnReceiveEnded = (ended)=>{
|
|
|
304
326
|
};
|
|
305
327
|
/**
|
|
306
328
|
* Drop claims older than the TTL, WITHOUT acking — the mirror of `memoryStore.purge`. Swept on
|
|
307
|
-
*
|
|
329
|
+
* inbound messages and listener registration, not on a timer.
|
|
308
330
|
*
|
|
309
331
|
* BREAKS at the first entry within the cutoff instead of scanning the whole map. Relies on
|
|
310
332
|
* `pending` (a `Map`) iterating in insertion order with `claimedAt` non-decreasing — held by the
|
|
@@ -389,15 +411,11 @@ const warnReceiveEnded = (ended)=>{
|
|
|
389
411
|
...sinks
|
|
390
412
|
].filter((sink)=>sink.topicID == null || sink.topicID === message.topicID);
|
|
391
413
|
for (const sink of matchedSinks)entry.holders.add(sink);
|
|
392
|
-
//
|
|
393
|
-
// (a returning member's backlog lands the instant the channel opens, ahead of
|
|
394
|
-
// `initControlLanes`). Acking here would report a frame nobody read as durably handled —
|
|
395
|
-
// permanent loss, since `memoryStore` keys `deliveries` by DID and redelivers an unacked frame
|
|
396
|
-
// but not an acked one. So the drain releases its own claim (which may ack) ONLY when something
|
|
397
|
-
// matched; otherwise the entry is pruned unacked at the TTL and the frame returns on the next
|
|
398
|
-
// redelivery (see "The init-race window" above).
|
|
414
|
+
// Keep unmatched frames for the first topic listener, without acknowledging unread mail.
|
|
399
415
|
if (matchedListeners.length > 0 || matchedSinks.length > 0) {
|
|
400
416
|
releaseClaim(message.sequenceID, drainClaim);
|
|
417
|
+
} else {
|
|
418
|
+
entry.unmatched = message;
|
|
401
419
|
}
|
|
402
420
|
for (const sink of matchedSinks){
|
|
403
421
|
try {
|
|
@@ -416,6 +434,7 @@ const warnReceiveEnded = (ended)=>{
|
|
|
416
434
|
// nothing and what it does NOT cover.
|
|
417
435
|
if (publishSuspended) throw new PeerDisposedError('mux: publish after dispose');
|
|
418
436
|
assertSubscribable(topicID);
|
|
437
|
+
assertFrameFits(payload);
|
|
419
438
|
await hub.publish({
|
|
420
439
|
senderDID: localDID,
|
|
421
440
|
topicID,
|
|
@@ -429,6 +448,7 @@ const warnReceiveEnded = (ended)=>{
|
|
|
429
448
|
// See `publishSuspended`'s note above.
|
|
430
449
|
if (publishSuspended) throw new PeerDisposedError('mux: publish after dispose');
|
|
431
450
|
assertSubscribable(publishParams.topicID);
|
|
451
|
+
assertFrameFits(publishParams.payload);
|
|
432
452
|
return await hub.publish(publishParams);
|
|
433
453
|
},
|
|
434
454
|
subscribe: (_subscriberDID, topicID)=>{
|
|
@@ -514,6 +534,7 @@ const warnReceiveEnded = (ended)=>{
|
|
|
514
534
|
// See `publishSuspended`'s note above. This is the commit lane's own route to the wire.
|
|
515
535
|
if (publishSuspended) throw new PeerDisposedError('mux: publish after dispose');
|
|
516
536
|
assertSubscribable(params.topicID);
|
|
537
|
+
assertFrameFits(params.payload);
|
|
517
538
|
return await Promise.resolve(hub.publish({
|
|
518
539
|
senderDID: localDID,
|
|
519
540
|
topicID: params.topicID,
|
package/lib/index.d.ts
CHANGED
|
@@ -3,22 +3,25 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module rpc
|
|
5
5
|
*/
|
|
6
|
-
export type { Anchor, AnchorStore } from './anchor.js';
|
|
6
|
+
export type { Anchor, AnchorSlot, AnchorStore } from './anchor.js';
|
|
7
7
|
export { type AppAAD, decodeAppAAD, encodeAppAAD } from './app-aad.js';
|
|
8
8
|
export type { AppCursorStore, AppWindowPruned } from './app-cursor.js';
|
|
9
9
|
export type { AppDeliveryResumed, AppDeliveryStalled } from './app-lane.js';
|
|
10
|
+
export { type AppOutbox, type AppOutboxEntry, type CommitCursor, MAX_APP_ENTRY_BYTES, } from './app-outbox.js';
|
|
10
11
|
export { type AppliedCommit, type ClassifyCommitParams, type CommitClassifierState, type CommitDisposition, type CommitFrameEvidence, classifyCommit, digestAppliedCommit, UNKNOWN_FRAME_VERSION, } from './classify.js';
|
|
11
12
|
export { CommitDeadlineError, type CommitJournal, type CommitKind, isHeadMismatch, type JournalEntry, JournalEpochError, type LaneResult, type LostCommit, type PendingCommit, RecoveryRequiredError, } from './commit.js';
|
|
12
13
|
export { COMMIT_FRAME_VERSION, type CommitFrame, decodeCommitFrame, encodeCommitFrame, isUnsupportedCommitFrameVersion, UnsupportedCommitFrameVersionError, } from './commit-frame.js';
|
|
13
|
-
export { type AppFrameRef, AppFrameStorageError, type CommitContext, type CommitHeader, type ExportSecretResult, FrameEpochError, type GroupCrypto, type GroupMLS, type GroupUnwrapResult, isAppFrameStorageError, isMissingLedgerEntries, type PendingAppFrame, type PendingAppFrames, type PendingRecovery, type ProcessCommitResult, type RosterEntry, type SealEntriesResult, sortPendingAppFrames, } from './crypto.js';
|
|
14
|
+
export { type AppFrameRef, AppFrameStorageError, type AppliedRecovery, type CommitContext, type CommitHeader, type ExportSecretResult, FrameEpochError, type GroupCrypto, type GroupMLS, type GroupUnwrapResult, isAppFrameStorageError, isMissingLedgerEntries, type OpenedRecoveryVerdict, type PendingAppFrame, type PendingAppFrames, type PendingRecovery, type ProcessCommitResult, type RecoveryConfirmationKey, type RecoveryRefusalReason, type RecoveryVerdict, type RosterEntry, type SealEntriesResult, type SendAdmission, sortPendingAppFrames, type VerifiedRecoveryRequest, } from './crypto.js';
|
|
14
15
|
export { asDeliveryPosition, asLogPosition, type DeliveryPosition, type LogPosition, } from './cursor.js';
|
|
15
|
-
export { PeerDisposedError } from './errors.js';
|
|
16
|
+
export { AppEntryTooLargeError, AppOutboxFullError, PeerDisposedError, PeerRemovedError, SendNotAdmissibleError, } from './errors.js';
|
|
17
|
+
export { assertFrameFits, FrameTooLargeError } from './frame-size.js';
|
|
16
18
|
export type { GroupProcedureHandlers } from './handlers.js';
|
|
17
19
|
export { decodeHandshakeFrame, encodeHandshakeFrame, HANDSHAKE_KIND, HANDSHAKE_MAGIC, HANDSHAKE_VERSION, type HandshakeKind, } from './handshake.js';
|
|
18
20
|
export type { ReceiveLaneEnded, SubscribeFailure } from './hub-mux.js';
|
|
19
21
|
export { createLedgerEntryResolver, decodeLedgerEntries, encodeLedgerEntries, LEDGER_ENTRIES_VERSION, } from './ledger-entries.js';
|
|
20
|
-
export
|
|
22
|
+
export type { AppOutboxCleared, EpochFloor } from './log-delivery.js';
|
|
23
|
+
export { createGroupPeer, type GroupPeer, type GroupPeerMLSParams, type GroupPeerParams, type ProtocolSurface, type RecoveryEvent, type RecoveryFailureDetails, type RecoveryFailureReason, type RecoveryTrigger, type StrandConfidence, type StrandKind, type StrandObservation, } from './peer.js';
|
|
21
24
|
export { defineGroupProtocol, type GroupProcedureDefinition, type GroupProtocolDefinition, type RetainableEventProcedureDefinition, type Retention, retentionOf, } from './protocol.js';
|
|
22
|
-
export { decodeLedgerReply, decodeLedgerRequest, decodeRecoveryReply, decodeRecoveryRequest, encodeLedgerReply, encodeLedgerRequest, encodeRecoveryReply, encodeRecoveryRequest, } from './recovery.js';
|
|
25
|
+
export { decodeLedgerReply, decodeLedgerRequest, decodeRecoveryConfirmRequest, decodeRecoveryReply, decodeRecoveryRequest, decodeRecoveryVerdict, encodeLedgerReply, encodeLedgerRequest, encodeRecoveryConfirmRequest, encodeRecoveryReply, encodeRecoveryRequest, encodeRecoveryVerdict, type RecoveryConfirmRequest, type RecoveryVerdictFrame, } from './recovery.js';
|
|
23
26
|
export { detectRosterChange } from './roster.js';
|
|
24
27
|
export { APP_TOPIC_LABEL, COMMIT_LABEL, commitTopic, discoveryTopic, INBOX_LABEL, inboxTopic, protocolTopic, RENDEZVOUS_LABEL, rendezvousTopic, } from './topic.js';
|
package/lib/index.js
CHANGED
|
@@ -3,16 +3,18 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module rpc
|
|
5
5
|
*/ export { decodeAppAAD, encodeAppAAD } from './app-aad.js';
|
|
6
|
+
export { MAX_APP_ENTRY_BYTES } from './app-outbox.js';
|
|
6
7
|
export { classifyCommit, digestAppliedCommit, UNKNOWN_FRAME_VERSION } from './classify.js';
|
|
7
8
|
export { CommitDeadlineError, isHeadMismatch, JournalEpochError, RecoveryRequiredError } from './commit.js';
|
|
8
9
|
export { COMMIT_FRAME_VERSION, decodeCommitFrame, encodeCommitFrame, isUnsupportedCommitFrameVersion, UnsupportedCommitFrameVersionError } from './commit-frame.js';
|
|
9
10
|
export { AppFrameStorageError, FrameEpochError, isAppFrameStorageError, isMissingLedgerEntries, sortPendingAppFrames } from './crypto.js';
|
|
10
11
|
export { asDeliveryPosition, asLogPosition } from './cursor.js';
|
|
11
|
-
export { PeerDisposedError } from './errors.js';
|
|
12
|
+
export { AppEntryTooLargeError, AppOutboxFullError, PeerDisposedError, PeerRemovedError, SendNotAdmissibleError } from './errors.js';
|
|
13
|
+
export { assertFrameFits, FrameTooLargeError } from './frame-size.js';
|
|
12
14
|
export { decodeHandshakeFrame, encodeHandshakeFrame, HANDSHAKE_KIND, HANDSHAKE_MAGIC, HANDSHAKE_VERSION } from './handshake.js';
|
|
13
15
|
export { createLedgerEntryResolver, decodeLedgerEntries, encodeLedgerEntries, LEDGER_ENTRIES_VERSION } from './ledger-entries.js';
|
|
14
16
|
export { createGroupPeer } from './peer.js';
|
|
15
17
|
export { defineGroupProtocol, retentionOf } from './protocol.js';
|
|
16
|
-
export { decodeLedgerReply, decodeLedgerRequest, decodeRecoveryReply, decodeRecoveryRequest, encodeLedgerReply, encodeLedgerRequest, encodeRecoveryReply, encodeRecoveryRequest } from './recovery.js';
|
|
18
|
+
export { decodeLedgerReply, decodeLedgerRequest, decodeRecoveryConfirmRequest, decodeRecoveryReply, decodeRecoveryRequest, decodeRecoveryVerdict, encodeLedgerReply, encodeLedgerRequest, encodeRecoveryConfirmRequest, encodeRecoveryReply, encodeRecoveryRequest, encodeRecoveryVerdict } from './recovery.js';
|
|
17
19
|
export { detectRosterChange } from './roster.js';
|
|
18
20
|
export { APP_TOPIC_LABEL, COMMIT_LABEL, commitTopic, discoveryTopic, INBOX_LABEL, inboxTopic, protocolTopic, RENDEZVOUS_LABEL, rendezvousTopic } from './topic.js';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { HubFetchTopicResult } from '@kumiai/hub-tunnel';
|
|
2
|
+
import type { AppOutboxAcceptance, AppOutboxEntry } from './app-outbox.js';
|
|
3
|
+
import type { LogPosition } from './cursor.js';
|
|
4
|
+
export type EpochFloor = {
|
|
5
|
+
epoch: number;
|
|
6
|
+
position: LogPosition | null;
|
|
7
|
+
covered: boolean;
|
|
8
|
+
};
|
|
9
|
+
export type AppOutboxCleared = {
|
|
10
|
+
reason: 'removed';
|
|
11
|
+
seqs: Array<number>;
|
|
12
|
+
};
|
|
13
|
+
export type CoveredFetchResult = HubFetchTopicResult;
|
|
14
|
+
export declare function checkedFetchResult(result: HubFetchTopicResult): CoveredFetchResult;
|
|
15
|
+
type LogFrame = {
|
|
16
|
+
topicID: string;
|
|
17
|
+
payload: Uint8Array;
|
|
18
|
+
};
|
|
19
|
+
/** A sealed frame with the floor snapshot its admission was checked against. */
|
|
20
|
+
type SealedLogFrame = LogFrame & {
|
|
21
|
+
floor: EpochFloor;
|
|
22
|
+
};
|
|
23
|
+
export type LogDeliveryParams = {
|
|
24
|
+
queue: AppOutboxAcceptance;
|
|
25
|
+
ready: () => Promise<void>;
|
|
26
|
+
floor: () => EpochFloor;
|
|
27
|
+
held: () => boolean;
|
|
28
|
+
probe: () => Promise<CoveredFetchResult>;
|
|
29
|
+
pull: () => Promise<void>;
|
|
30
|
+
heal: () => Promise<void>;
|
|
31
|
+
seal: (entry: AppOutboxEntry) => Promise<SealedLogFrame>;
|
|
32
|
+
put: (entry: AppOutboxEntry) => Promise<void>;
|
|
33
|
+
publish: (frame: LogFrame) => Promise<unknown> | null;
|
|
34
|
+
remove: (seq: number) => Promise<void>;
|
|
35
|
+
clear: () => Promise<void>;
|
|
36
|
+
cleared: (notice: AppOutboxCleared) => void;
|
|
37
|
+
};
|
|
38
|
+
/** Publication evidence stays in memory. A prepared durable attempt proves nothing. */
|
|
39
|
+
export declare function createLogDelivery(params: LogDeliveryParams): {
|
|
40
|
+
trigger: () => void;
|
|
41
|
+
beginWalk: () => number;
|
|
42
|
+
certify: (epoch: number, started: number) => void;
|
|
43
|
+
removed: () => void;
|
|
44
|
+
close: () => void;
|
|
45
|
+
};
|
|
46
|
+
export {};
|