@kubun/plugin-p2p 0.10.0 → 0.10.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/lib/context/group.js +1 -1
- package/lib/context/hub.js +1 -1
- package/lib/context/types.d.ts +9 -0
- package/lib/groups/broadcast-service.d.ts +84 -10
- package/lib/groups/broadcast-service.js +1 -1
- package/lib/groups/broadcast.d.ts +70 -0
- package/lib/groups/broadcast.js +1 -1
- package/lib/groups/events.d.ts +2 -1
- package/lib/groups/group-handle-registry.d.ts +8 -0
- package/lib/groups/group-handle-registry.js +1 -1
- package/lib/groups/group-health-monitor.d.ts +45 -0
- package/lib/groups/group-health-monitor.js +1 -0
- package/lib/groups/manager.d.ts +9 -0
- package/lib/groups/manager.js +1 -1
- package/lib/groups/rejoin-codec.d.ts +14 -0
- package/lib/groups/rejoin-codec.js +1 -0
- package/lib/groups/wire-frame.d.ts +2 -0
- package/lib/groups/wire-frame.js +1 -1
- package/lib/hub/circle-catchup-requester.d.ts +58 -0
- package/lib/hub/circle-catchup-requester.js +1 -0
- package/lib/hub/circle-catchup-responder.d.ts +50 -0
- package/lib/hub/circle-catchup-responder.js +1 -0
- package/lib/hub/epoch-stale-detector.d.ts +18 -0
- package/lib/hub/epoch-stale-detector.js +1 -0
- package/lib/hub/errors.d.ts +7 -5
- package/lib/hub/errors.js +1 -1
- package/lib/hub/group-channel.d.ts +22 -0
- package/lib/hub/group-channel.js +1 -1
- package/lib/hub/manager.d.ts +5 -4
- package/lib/hub/manager.js +1 -1
- package/lib/hub/receive-handler.d.ts +20 -2
- package/lib/hub/receive-handler.js +1 -1
- package/lib/hub/rejoin-manager.d.ts +78 -0
- package/lib/hub/rejoin-manager.js +1 -0
- package/lib/hub/rejoin-responder.d.ts +32 -0
- package/lib/hub/rejoin-responder.js +1 -0
- package/lib/hub/relay-manager.d.ts +16 -1
- package/lib/hub/relay-manager.js +1 -1
- package/lib/hub/wiring.d.ts +19 -1
- package/lib/hub/wiring.js +1 -1
- package/lib/index.d.ts +18 -3
- package/lib/index.js +1 -1
- package/lib/protocol.d.ts +7 -0
- package/lib/protocol.js +1 -1
- package/lib/schema.js +34 -2
- package/lib/sync/authorize.d.ts +25 -0
- package/lib/sync/authorize.js +1 -0
- package/lib/sync/errors.d.ts +11 -0
- package/lib/sync/errors.js +1 -0
- package/lib/sync/handlers.d.ts +3 -22
- package/lib/sync/handlers.js +1 -1
- package/lib/sync/hub-tunnel-sync-listener.d.ts +2 -0
- package/lib/sync/hub-tunnel-sync-listener.js +1 -1
- package/lib/sync/merkle-channel.d.ts +19 -0
- package/lib/sync/merkle-channel.js +1 -1
- package/lib/sync/peer-registry.d.ts +7 -0
- package/lib/sync/peer-registry.js +1 -1
- package/lib/sync/sync-client.d.ts +2 -0
- package/lib/sync/sync-client.js +1 -1
- package/lib/sync/sync-manager.js +1 -1
- package/lib/types.d.ts +30 -0
- package/lib/utils.d.ts +5 -0
- package/lib/utils.js +1 -0
- package/package.json +12 -12
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Logger } from '@kubun/logger';
|
|
2
|
+
import type { GroupBroadcastMessage } from '../groups/broadcast.js';
|
|
3
|
+
/** The reply payload a current member sends back, correlated by `requestID`. */
|
|
4
|
+
type CatchupReply = Extract<GroupBroadcastMessage, {
|
|
5
|
+
type: 'circle-catchup:reply';
|
|
6
|
+
}>;
|
|
7
|
+
export type CircleCatchupRequesterParams = {
|
|
8
|
+
groupID: string;
|
|
9
|
+
/** Fresh correlation id per request (incl. retries). */
|
|
10
|
+
genRequestID: () => string;
|
|
11
|
+
/** Broadcast a `circle-catchup:request` on the group channel. */
|
|
12
|
+
sendRequest: (requestID: string) => Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* Apply a received reply batch row-by-row (idempotent, order-independent).
|
|
15
|
+
* Mirrors `applyCatchupReply` bound to the local store/engine context.
|
|
16
|
+
*/
|
|
17
|
+
applyReply: (reply: CatchupReply) => Promise<void>;
|
|
18
|
+
requestTimeoutMs?: number;
|
|
19
|
+
maxAttempts?: number;
|
|
20
|
+
backoffMs?: (attempt: number) => number;
|
|
21
|
+
logger?: Logger;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Drives a rejoining device's circle/member catch-up for one group: broadcast a
|
|
25
|
+
* `circle-catchup:request`, await the first matching `circle-catchup:reply`
|
|
26
|
+
* (correlated by `requestID`) within a timeout, apply it, done. On timeout,
|
|
27
|
+
* retry with a capped full-jitter backoff up to `maxAttempts`.
|
|
28
|
+
*
|
|
29
|
+
* Mirrors the rejoin manager's request/await/retry shape, minus any MLS
|
|
30
|
+
* join/confirm — there is no Commit to broadcast and no head to confirm against,
|
|
31
|
+
* because `applyReply` is idempotent and order-independent: one good reply
|
|
32
|
+
* lands the snapshot, and a duplicate or late reply is a harmless no-op.
|
|
33
|
+
*
|
|
34
|
+
* Exhaustion is NON-FATAL: a device that never hears back simply keeps the
|
|
35
|
+
* state it has; it resolves without throwing.
|
|
36
|
+
*/
|
|
37
|
+
export declare class CircleCatchupRequester {
|
|
38
|
+
#private;
|
|
39
|
+
constructor(params: CircleCatchupRequesterParams);
|
|
40
|
+
get inFlight(): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Route a received `circle-catchup:reply`: resolve the matching in-flight
|
|
43
|
+
* waiter. Unknown `requestID`s (peers' requests, stale or duplicate replies
|
|
44
|
+
* for an already-resolved attempt) are ignored — harmless.
|
|
45
|
+
*/
|
|
46
|
+
onCatchupReply(requestID: string, reply: CatchupReply): void;
|
|
47
|
+
/**
|
|
48
|
+
* Run the catch-up flow. Idempotent: a concurrent call while one is in flight
|
|
49
|
+
* shares the existing run's promise rather than starting a second.
|
|
50
|
+
*/
|
|
51
|
+
run(): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Stop a running catch-up and release timers. Idempotent; the loop checks
|
|
54
|
+
* `#disposed` between attempts, pending waiters resolve `null`. On channel close.
|
|
55
|
+
*/
|
|
56
|
+
dispose(): void;
|
|
57
|
+
}
|
|
58
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{sleep as e,unref as t}from"../utils.js";export class CircleCatchupRequester{#e;#t;#s;#i;#r;#l;#a;#u;#n=new Map;#o=null;#h=!1;constructor(e){this.#e=e.groupID,this.#t=e.genRequestID,this.#s=e.sendRequest,this.#i=e.applyReply,this.#r=e.requestTimeoutMs??5e3,this.#l=e.maxAttempts??8,this.#a=e.backoffMs??s,this.#u=e.logger}get inFlight(){return null!=this.#o}onCatchupReply(e,t){let s=this.#n.get(e);null!=s&&(this.#n.delete(e),s.resolve(t))}run(){if(null!=this.#o)return this.#o;let e=this.#p().finally(()=>{this.#o=null});return this.#o=e,e}async #p(){for(let t=0;t<this.#l&&!this.#h;t++){if(t>0&&(await e(this.#a(t)),this.#h))return;let s=await this.#c(this.#r);if(null!=s)try{await this.#i(s);return}catch(e){this.#u?.warn("circle catch-up: applying reply failed",{groupID:this.#e,error:e})}}this.#h||this.#u?.warn("circle catch-up: no usable reply, giving up",{groupID:this.#e,attempts:this.#l})}async #c(e){let t=this.#t(),s=this.#g(t,e);try{await this.#s(t)}catch(e){return this.#m(t),this.#u?.warn("circle catch-up: request send failed",{groupID:this.#e,error:e}),null}return s}#g(e,s){return new Promise(i=>{let r=setTimeout(()=>{this.#n.delete(e),i(null)},s);t(r),this.#n.set(e,{resolve:e=>{clearTimeout(r),i(e)},cancel:()=>{clearTimeout(r),i(null)}})})}#m(e){let t=this.#n.get(e);null!=t&&(this.#n.delete(e),t.cancel())}dispose(){for(let e of(this.#h=!0,this.#n.values()))e.cancel();this.#n.clear()}}function s(e){return Math.floor(Math.random()*Math.min(500*2**(e-1),5e3))}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Logger } from '@kubun/logger';
|
|
2
|
+
import type { Circle, CircleMember } from '@kubun/store-p2p';
|
|
3
|
+
import type { CircleMemberSnapshot, CircleSnapshot } from '../groups/broadcast.js';
|
|
4
|
+
/**
|
|
5
|
+
* Reads the group's circles + circle members from the store. Mirrors the
|
|
6
|
+
* `listCirclesByGroup` / `listCircleMembers` store API, both returning
|
|
7
|
+
* tombstoned rows when `includeRemoved` is set so the snapshot can carry
|
|
8
|
+
* removed entries.
|
|
9
|
+
*/
|
|
10
|
+
export type CircleCatchupStoreReader = {
|
|
11
|
+
listCirclesByGroup: (groupID: string, options?: {
|
|
12
|
+
includeRemoved?: boolean;
|
|
13
|
+
}) => Promise<Array<Circle>>;
|
|
14
|
+
listCircleMembers: (circleID: string, options?: {
|
|
15
|
+
includeRemoved?: boolean;
|
|
16
|
+
}) => Promise<Array<CircleMember>>;
|
|
17
|
+
};
|
|
18
|
+
export type CircleCatchupResponderParams = {
|
|
19
|
+
groupID: string;
|
|
20
|
+
/** Reads the group's circle + circle-member rows (including tombstoned). */
|
|
21
|
+
store: CircleCatchupStoreReader;
|
|
22
|
+
/** Broadcast a `circle-catchup:reply` to the group channel. */
|
|
23
|
+
sendCatchupReply: (requestID: string, circles: Array<CircleSnapshot>, circleMembers: Array<CircleMemberSnapshot>) => Promise<void>;
|
|
24
|
+
/** Jitter delay (ms) before replying. Defaults to a random 0..250ms. */
|
|
25
|
+
nextJitterMs?: () => number;
|
|
26
|
+
/** How long a `requestID` stays suppressed after being answered. */
|
|
27
|
+
suppressTTLMs?: number;
|
|
28
|
+
logger?: Logger;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Answers `circle-catchup:request`s with one batched snapshot of the group's
|
|
32
|
+
* circle + circle-member rows (including tombstoned, each carrying its authored
|
|
33
|
+
* HLC). Every current member could reply, so jitter (random pre-reply delay) +
|
|
34
|
+
* suppression (cancel on seeing a peer's reply for the same `requestID`)
|
|
35
|
+
* collapse the storm to ~1 reply. Keyed on `requestID` (fresh per attempt);
|
|
36
|
+
* suppression entries TTL out to bound memory.
|
|
37
|
+
*
|
|
38
|
+
* Mirrors the rejoin responder's storm-collapse shape; suppression collapses
|
|
39
|
+
* only duplicates of one `requestID`, so distinct `requestID`s still draw one
|
|
40
|
+
* reply each.
|
|
41
|
+
*/
|
|
42
|
+
export declare class CircleCatchupResponder {
|
|
43
|
+
#private;
|
|
44
|
+
constructor(params: CircleCatchupResponderParams);
|
|
45
|
+
/** Handle an inbound `circle-catchup:request`: schedule a jittered, suppressible reply. */
|
|
46
|
+
onCatchupRequest(requestID: string): void;
|
|
47
|
+
/** Handle an observed `circle-catchup:reply` (own or peer's): suppress this request. */
|
|
48
|
+
onCatchupReplySeen(requestID: string): void;
|
|
49
|
+
dispose(): void;
|
|
50
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{unref as e}from"../utils.js";function s(e){return{id:e.id,groupID:e.group_id,name:e.name,description:e.description,catalogIDs:e.catalog_ids,hlc:e.hlc,removedAtHLC:e.removed_at_hlc??null}}export class CircleCatchupResponder{#e;#s;#t;#r;#i;#p;#o=new Map;#l=new Map;constructor(e){this.#e=e.groupID,this.#s=e.store,this.#t=e.sendCatchupReply,this.#r=e.nextJitterMs??(()=>Math.floor(250*Math.random())),this.#i=e.suppressTTLMs??5e3,this.#p=e.logger}onCatchupRequest(s){if(this.#l.has(s)||this.#o.has(s))return;let t=setTimeout(()=>{this.#o.delete(s),this.#h(s)},this.#r());e(t),this.#o.set(s,t)}onCatchupReplySeen(e){let s=this.#o.get(e);null!=s&&(clearTimeout(s),this.#o.delete(e)),this.#n(e)}dispose(){for(let e of this.#o.values())clearTimeout(e);for(let e of(this.#o.clear(),this.#l.values()))clearTimeout(e);this.#l.clear()}async #h(e){if(!this.#l.has(e)){this.#n(e);try{let{circles:s,circleMembers:t}=await this.#u();await this.#t(e,s,t)}catch(s){this.#p?.warn("circle catch-up responder failed to reply",{groupID:this.#e,requestID:e,error:s})}}}async #u(){let e=await this.#s.listCirclesByGroup(this.#e,{includeRemoved:!0}),t=e.map(s),r=[];for(let s of e)for(let e of(await this.#s.listCircleMembers(s.id,{includeRemoved:!0})))r.push({circleID:e.circle_id,memberDID:e.member_did,role:e.role,hlc:e.hlc,removedAtHLC:e.removed_at_hlc??null});return{circles:t,circleMembers:r}}#n(s){let t=this.#l.get(s);null!=t&&clearTimeout(t);let r=setTimeout(()=>{this.#l.delete(s)},this.#i);e(r),this.#l.set(s,r)}}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { GroupHealthMonitor } from '../groups/group-health-monitor.js';
|
|
2
|
+
import type { ReceiveOutcome } from './receive-handler.js';
|
|
3
|
+
export type EpochStaleDetectorParams = {
|
|
4
|
+
groupID: string;
|
|
5
|
+
monitor: GroupHealthMonitor;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Maps a group's receive outcomes to `epoch-stale` signals (one per group):
|
|
9
|
+
* `applied` → reset; `decrypt-failed` → signal; `commit-out-of-order` → signal
|
|
10
|
+
* only on a REPEAT `sequenceID` (first sighting is normal replay; a repeat means
|
|
11
|
+
* the gap-filling Commit was GC'd at the hub and never arrives); else no signal.
|
|
12
|
+
* The seen-set grows only while stuck and clears on the next `applied`.
|
|
13
|
+
*/
|
|
14
|
+
export declare class EpochStaleDetector {
|
|
15
|
+
#private;
|
|
16
|
+
constructor(params: EpochStaleDetectorParams);
|
|
17
|
+
onReceiveOutcome(outcome: ReceiveOutcome, sequenceID: string): void;
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class EpochStaleDetector{#e;#t;#r=new Set;constructor(e){this.#e=e.groupID,this.#t=e.monitor}onReceiveOutcome(e,t){switch(e){case"applied":this.#r.clear(),this.#t.reset(this.#e,"epoch-stale");return;case"decrypt-failed":this.#t.signal(this.#e,"epoch-stale");return;case"commit-out-of-order":this.#r.has(t)?this.#t.signal(this.#e,"epoch-stale"):this.#r.add(t);return;default:return}}}
|
package/lib/hub/errors.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
export declare class ReconnectingError extends Error {
|
|
2
|
-
|
|
3
|
-
readonly attempt: number;
|
|
2
|
+
#private;
|
|
4
3
|
constructor(hubURL: string, attempt: number);
|
|
4
|
+
get hubURL(): string;
|
|
5
|
+
get attempt(): number;
|
|
5
6
|
}
|
|
6
7
|
/**
|
|
7
8
|
* Thrown by `DIDObservingTransport.read()` when the first observed `payload.iss`
|
|
@@ -17,12 +18,13 @@ export declare class ReconnectingError extends Error {
|
|
|
17
18
|
* message is delivered, when a row was already pinned at connect time.
|
|
18
19
|
*/
|
|
19
20
|
export declare class HubServerDIDMismatchError extends Error {
|
|
20
|
-
|
|
21
|
-
readonly expectedServerDID: string;
|
|
22
|
-
readonly observedServerDID: string;
|
|
21
|
+
#private;
|
|
23
22
|
constructor(params: {
|
|
24
23
|
hubURL: string;
|
|
25
24
|
expectedServerDID: string;
|
|
26
25
|
observedServerDID: string;
|
|
27
26
|
});
|
|
27
|
+
get hubURL(): string;
|
|
28
|
+
get expectedServerDID(): string;
|
|
29
|
+
get observedServerDID(): string;
|
|
28
30
|
}
|
package/lib/hub/errors.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export class ReconnectingError extends Error{
|
|
1
|
+
export class ReconnectingError extends Error{#e;#r;constructor(e,r){super(`broadcast wait timed out: hub ${e} reconnecting (attempt ${r})`),this.name="ReconnectingError",this.#e=e,this.#r=r}get hubURL(){return this.#e}get attempt(){return this.#r}}export class HubServerDIDMismatchError extends Error{#e;#t;#s;constructor(e){super(`hub at ${e.hubURL} signed responses with ${e.observedServerDID}; pinned to ${e.expectedServerDID}`),this.name="HubServerDIDMismatchError",this.#e=e.hubURL,this.#t=e.expectedServerDID,this.#s=e.observedServerDID}get hubURL(){return this.#e}get expectedServerDID(){return this.#t}get observedServerDID(){return this.#s}}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { Disposer } from '@enkaku/async';
|
|
2
|
+
import type { OwnIdentity } from '@enkaku/token';
|
|
2
3
|
import type { Logger } from '@kubun/logger';
|
|
3
4
|
import type { P2PStoreAPI } from '@kubun/store-p2p';
|
|
4
5
|
import type { GroupBroadcastMessage } from '../groups/broadcast.js';
|
|
5
6
|
import type { BroadcastService } from '../groups/broadcast-service.js';
|
|
7
|
+
import type { GroupHealthMonitor } from '../groups/group-health-monitor.js';
|
|
8
|
+
import type { RejoinResult } from '../types.js';
|
|
6
9
|
import type { HubConnection } from './hub-connection.js';
|
|
10
|
+
import { RejoinManager } from './rejoin-manager.js';
|
|
7
11
|
export type AckBatchParams = {
|
|
8
12
|
flushMs: number;
|
|
9
13
|
flushMax: number;
|
|
@@ -32,6 +36,19 @@ export type GroupChannelParams = {
|
|
|
32
36
|
broadcastService: BroadcastService;
|
|
33
37
|
p2pStore: P2PStoreAPI;
|
|
34
38
|
logger?: Logger;
|
|
39
|
+
/** When provided, receive outcomes feed epoch-stale health detection and the
|
|
40
|
+
* rejoin flow becomes available (both need the shared health monitor). */
|
|
41
|
+
monitor?: GroupHealthMonitor;
|
|
42
|
+
/** Device identity — required by `joinGroupExternal` in the rejoin flow. */
|
|
43
|
+
identity?: OwnIdentity;
|
|
44
|
+
/** Fresh correlation ids for rejoin requests. */
|
|
45
|
+
getRandomID?: () => string;
|
|
46
|
+
/**
|
|
47
|
+
* Pre-built rejoin manager, overriding the one this channel would construct
|
|
48
|
+
* from `monitor`/`identity`/`getRandomID`. Lets the rejoin orchestration be
|
|
49
|
+
* driven without standing up real MLS; production callers leave it unset.
|
|
50
|
+
*/
|
|
51
|
+
rejoinManager?: RejoinManager;
|
|
35
52
|
};
|
|
36
53
|
/**
|
|
37
54
|
* Per-group handle: thin wrapper around a shared `HubConnection`.
|
|
@@ -52,6 +69,11 @@ export declare class GroupChannel extends Disposer {
|
|
|
52
69
|
#private;
|
|
53
70
|
constructor(params: GroupChannelParams);
|
|
54
71
|
open(): Promise<void>;
|
|
72
|
+
/**
|
|
73
|
+
* Run stale-device recovery for this group. Available only when constructed
|
|
74
|
+
* with a health monitor + identity; returns `recovery-failed` otherwise.
|
|
75
|
+
*/
|
|
76
|
+
rejoinGroup(): Promise<RejoinResult>;
|
|
55
77
|
close(): Promise<void>;
|
|
56
78
|
broadcast(message: GroupBroadcastMessage, opts?: {
|
|
57
79
|
timeoutMs?: number;
|
package/lib/hub/group-channel.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Disposer as e}from"@enkaku/async";import{
|
|
1
|
+
import{Disposer as e}from"@enkaku/async";import{toB64 as r}from"@enkaku/codec";import{CircleCatchupRequester as t}from"./circle-catchup-requester.js";import{CircleCatchupResponder as o}from"./circle-catchup-responder.js";import{EpochStaleDetector as s}from"./epoch-stale-detector.js";import{handleReceivedMessage as i}from"./receive-handler.js";import{RejoinManager as n}from"./rejoin-manager.js";import{RejoinResponder as a}from"./rejoin-responder.js";import{handleSendBroadcast as u,handleSendCommit as h}from"./send-handler.js";export class AckBatch{#e;#r;#t;#o;#s=new Set;#i=null;constructor(e){this.#e=e.flushMs,this.#r=e.flushMax,this.#t=e.send,this.#o=e.logger}add(e){let r=0===this.#s.size;if(this.#s.add(e),this.#s.size>=this.#r){null!==this.#i&&(clearTimeout(this.#i),this.#i=null),this.flush();return}r&&null===this.#i&&(this.#i=setTimeout(()=>{this.#i=null,this.flush()},this.#e))}async flush(){if(null!==this.#i&&(clearTimeout(this.#i),this.#i=null),0===this.#s.size)return;let e=Array.from(this.#s);this.#s.clear();try{await this.#t(e)}catch(r){this.#o?.error("ack batch send failed",{error:r,count:e.length})}}dispose(){null!==this.#i&&(clearTimeout(this.#i),this.#i=null),this.#s.clear()}}export function computeBackoff(e,r,t,o){let s=Math.min(r*2**e*(1+o*(2*Math.random()-1)),t);return s<0?0:s}export function abortableSleep(e,r){return new Promise((t,o)=>{if(r.aborted)return void o(r.reason??Error("aborted"));let s=setTimeout(()=>{r.removeEventListener("abort",i),t()},e),i=()=>{clearTimeout(s),r.removeEventListener("abort",i),o(r.reason??Error("aborted"))};r.addEventListener("abort",i)})}export class GroupChannel extends e{#n;#a;#u;#h;#p;#o;#c;#d;#l;#g;#m;#I=!1;constructor(e){super({dispose:async()=>{await this.#f()}}),this.#n=e.groupID,this.#a=e.deviceID,this.#u=e.hubConnection,this.#h=e.broadcastService,this.#p=e.p2pStore,this.#o=e.logger,null!=e.monitor&&(this.#c=new s({groupID:e.groupID,monitor:e.monitor})),this.#d=new a({groupID:e.groupID,exportGroupInfo:()=>this.#h.exportGroupInfo(this.#n),sendGroupInfo:(e,r)=>this.#D(e,r),logger:e.logger}),null!=e.rejoinManager?this.#l=e.rejoinManager:null!=e.monitor&&null!=e.identity&&null!=e.getRandomID&&(this.#l=new n({groupID:e.groupID,identity:e.identity,readCredential:()=>this.#h.readCredential(this.#n),genRequestID:e.getRandomID,sendRequest:e=>this.#b(e),sendCommit:e=>this.sendCommit(e),replaceHandle:e=>this.#h.replaceHandle(this.#n,e),monitor:e.monitor,logger:e.logger})),this.#g=new o({groupID:e.groupID,store:this.#p,sendCatchupReply:(e,r,t)=>this.#y(e,r,t),logger:e.logger}),null!=e.getRandomID&&(this.#m=new t({groupID:e.groupID,genRequestID:e.getRandomID,sendRequest:e=>this.#R(e),applyReply:async e=>{await this.#u.runInGroup(this.#n,()=>this.#h.applyCatchupReply(this.#n,e))},logger:e.logger}))}async open(){if(this.#I)throw Error("GroupChannel already opened");let e=await this.#p.getMLSState(this.#n,this.#a);if(null==e)throw Error(`MLS state missing for group ${this.#n}; cannot join hub group`);await this.#u.subscribe(this.#n,e.credential,e=>this.#j(e)),this.#I=!0}async #j(e){let r=await i({groupID:this.#n,message:e,broadcastService:this.#h,logger:this.#o});this.#c?.onReceiveOutcome(r.outcome,r.sequenceID);let t=r.result;if(t?.kind==="rejoin-request")this.#d.onRejoinRequest(t.requestID);else if(t?.kind==="rejoin-groupinfo")this.#d.onRejoinGroupInfoSeen(t.requestID),this.#l?.onGroupInfo(t.requestID,t.groupInfo);else if(t?.kind==="app"){let e=t.message;"circle-catchup:request"===e.type?this.#g.onCatchupRequest(e.requestID):"circle-catchup:reply"===e.type&&(this.#g.onCatchupReplySeen(e.requestID),this.#m?.onCatchupReply(e.requestID,e))}return{ack:r.ack}}async rejoinGroup(){if(null==this.#l)return{status:"recovery-failed"};let e=await this.#l.rejoin();return"recovered"===e.status&&null!=this.#m&&this.#m.run().catch(e=>{this.#o?.warn("circle catch-up after rejoin failed",{groupID:this.#n,error:e})}),e}#b(e){return this.#I?this.#h.prepareSendRejoinRequest({requestID:e,send:e=>this.#u.broadcast(this.#n,r(e))}):Promise.reject(Error(`GroupChannel for ${this.#n} is not ready for rejoin request`))}#D(e,t){return this.#I?this.#h.prepareSendRejoinGroupInfo({requestID:e,groupInfo:t,send:e=>this.#u.broadcast(this.#n,r(e))}):Promise.reject(Error(`GroupChannel for ${this.#n} is not ready for rejoin reply`))}#R(e){return this.#I?this.#h.prepareSend({groupID:this.#n,message:{type:"circle-catchup:request",requestID:e},send:e=>this.#u.broadcast(this.#n,r(e))}):Promise.reject(Error(`GroupChannel for ${this.#n} is not ready for catch-up request`))}#y(e,t,o){return this.#I?this.#h.prepareSend({groupID:this.#n,message:{type:"circle-catchup:reply",requestID:e,circles:t,circleMembers:o},send:e=>this.#u.broadcast(this.#n,r(e))}):Promise.reject(Error(`GroupChannel for ${this.#n} is not ready for catch-up reply`))}async close(){this.#I&&await this.dispose()}async #f(){if(this.#d.dispose(),this.#l?.dispose(),this.#g.dispose(),this.#m?.dispose(),this.#I){try{await this.#u.unsubscribe(this.#n)}catch(e){this.#o?.warn("hub-connection unsubscribe failed during close",{groupID:this.#n,error:e})}this.#I=!1}}broadcast(e,r){return this.#I?this.#u.runInGroup(this.#n,async()=>{if(!this.#I)throw Error(`GroupChannel for ${this.#n} is not ready for broadcast`);await u({groupID:this.#n,message:e,broadcastService:this.#h,send:(e,t)=>this.#u.broadcast(e,t,{timeoutMs:r?.timeoutMs}),logger:this.#o})},{timeoutMs:r?.timeoutMs}):Promise.reject(Error(`GroupChannel for ${this.#n} is not ready for broadcast`))}sendCommit(e,r){return this.#I?this.#u.runInGroup(this.#n,async()=>{if(!this.#I)throw Error(`GroupChannel for ${this.#n} is not ready for sendCommit`);await h({groupID:this.#n,commitBytes:e,broadcastService:this.#h,send:(e,t)=>this.#u.broadcast(e,t,{timeoutMs:r?.timeoutMs}),logger:this.#o})},{timeoutMs:r?.timeoutMs}):Promise.reject(Error(`GroupChannel for ${this.#n} is not ready for sendCommit`))}}
|
package/lib/hub/manager.d.ts
CHANGED
|
@@ -65,16 +65,17 @@ export type UpsertHubParams = {
|
|
|
65
65
|
serverDID?: string | null;
|
|
66
66
|
};
|
|
67
67
|
export declare class HubServerDIDConflictError extends Error {
|
|
68
|
-
|
|
69
|
-
readonly url: string;
|
|
70
|
-
readonly existingServerDID: string;
|
|
71
|
-
readonly providedServerDID: string;
|
|
68
|
+
#private;
|
|
72
69
|
constructor(params: {
|
|
73
70
|
hubID: string;
|
|
74
71
|
url: string;
|
|
75
72
|
existingServerDID: string;
|
|
76
73
|
providedServerDID: string;
|
|
77
74
|
});
|
|
75
|
+
get hubID(): string;
|
|
76
|
+
get url(): string;
|
|
77
|
+
get existingServerDID(): string;
|
|
78
|
+
get providedServerDID(): string;
|
|
78
79
|
}
|
|
79
80
|
export declare function createHub(params: CreateHubParams): Promise<Hub>;
|
|
80
81
|
/**
|
package/lib/hub/manager.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{getP2PStore as r}from"@kubun/store-p2p";export class HubServerDIDConflictError extends Error{
|
|
1
|
+
import{getP2PStore as r}from"@kubun/store-p2p";export class HubServerDIDConflictError extends Error{#r;#e;#t;#u;constructor(r){super(`hub at ${r.url} is pinned to ${r.existingServerDID}; refusing to overwrite with ${r.providedServerDID}`),this.name="HubServerDIDConflictError",this.#r=r.hubID,this.#e=r.url,this.#t=r.existingServerDID,this.#u=r.providedServerDID}get hubID(){return this.#r}get url(){return this.#e}get existingServerDID(){return this.#t}get providedServerDID(){return this.#u}}function e(r){return{id:r.id,label:r.label,url:r.url,serverDID:r.server_did,createdAt:new Date(r.created_at),updatedAt:null==r.updated_at?null:new Date(r.updated_at)}}export async function createHub(t){let u=await r(t.stores),i=t.serverDID??null,a=await u.getHubByURL(t.url);if(null!=a){if(null!=a.server_did&&null!=i&&a.server_did!==i)throw new HubServerDIDConflictError({hubID:a.id,url:t.url,existingServerDID:a.server_did,providedServerDID:i});if(null==a.server_did&&null!=i){await u.updateHub(a.id,{server_did:i});let r=await u.getHub(a.id);if(null==r)throw Error("Failed to read hub after upgrade");return e(r)}return e(a)}let l=crypto.randomUUID();await u.insertHub({id:l,label:t.label??null,url:t.url,server_did:i});let n=await u.getHub(l);if(null==n)throw Error("Failed to read hub after insert");return e(n)}export async function upsertHub(r){return await createHub({stores:r.stores,url:r.url,serverDID:r.serverDID})}export async function updateHub(t){let u=await r(t.stores),i=await u.getHub(t.id);if(null==i)throw Error(`Hub ${t.id} not found`);if(null!=t.url&&t.url!==i.url){let r=await u.getHubByURL(t.url);if(null!=r&&r.id!==t.id)throw Error(`URL ${t.url} already in use by hub ${r.id}`)}let a={};void 0!==t.label&&(a.label=t.label),void 0!==t.url&&(a.url=t.url),void 0!==t.serverDID&&(a.server_did=t.serverDID),await u.updateHub(t.id,a);let l=await u.getHub(t.id);if(null==l)throw Error("Failed to read hub after update");return e(l)}export async function deleteHub(e){let t=await r(e.stores),u=await t.listGroupIDsByHubID(e.id);return u.length>0&&!e.force?{success:!1,boundGroupIDs:u}:(u.length>0&&await t.deleteGroupHubsByHubID(e.id),await t.deleteHub(e.id),{success:!0,boundGroupIDs:u})}export async function getHub(t){let u=await r(t.stores),i=await u.getHub(t.id);return null==i?null:e(i)}export async function listHubs(t){let u=await r(t.stores);return(await u.listHubs()).map(e)}export async function bindHubToGroup(e){let t=await r(e.stores);return await t.insertGroupHub({group_id:e.groupID,hub_id:e.hubID})}export async function unbindHubFromGroup(e){let t=await r(e.stores);return await t.deleteGroupHub({groupID:e.groupID,hubID:e.hubID})}export async function listHubsByGroup(t){let u=await r(t.stores);return(await u.listHubsByGroupID(t.groupID)).map(e)}export async function listGroupsByHub(e){let t=await r(e.stores);return await t.listGroupIDsByHubID(e.hubID)}export async function captureServerDID(e){let t=await r(e.stores),u=await t.getHubByURL(e.hubURL);if(null==u)return!1;if(null==u.server_did)return await t.updateHub(u.id,{server_did:e.serverDID}),!0;if(u.server_did===e.serverDID)return!1;throw new HubServerDIDConflictError({hubID:u.id,url:e.hubURL,existingServerDID:u.server_did,providedServerDID:e.serverDID})}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Logger } from '@kubun/logger';
|
|
2
|
-
import { type BroadcastService } from '../groups/broadcast-service.js';
|
|
2
|
+
import { type BroadcastService, type ReceiveBroadcastResult } from '../groups/broadcast-service.js';
|
|
3
3
|
/**
|
|
4
4
|
* Shape of a message pushed by `hub/receive`. Matches `@enkaku/hub-protocol`
|
|
5
5
|
* server-to-client push payload.
|
|
@@ -17,8 +17,26 @@ export type HandleReceivedMessageParams = {
|
|
|
17
17
|
broadcastService: BroadcastService;
|
|
18
18
|
logger?: Logger;
|
|
19
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* Classification of a single received message's processing result. Drives both
|
|
22
|
+
* the ack decision and the epoch-stale health signal (see `EpochStaleDetector`):
|
|
23
|
+
* - `applied` — decrypted/applied cleanly; the device is at the current epoch.
|
|
24
|
+
* - `decrypt-failed` — could not derive the key for the current epoch.
|
|
25
|
+
* - `commit-out-of-order` — a Commit ahead of the local epoch (gap in stream).
|
|
26
|
+
* - `apply-failed` — decrypted but failed to apply to the local DB (transient).
|
|
27
|
+
* - `error` — unexpected failure.
|
|
28
|
+
* - `skipped` — not for this group; no processing attempted.
|
|
29
|
+
* - `control` — a rejoin control frame (request/groupinfo); processed cleanly
|
|
30
|
+
* but NOT a health indicator (must not reset epoch-stale tracking).
|
|
31
|
+
*/
|
|
32
|
+
export type ReceiveOutcome = 'applied' | 'decrypt-failed' | 'commit-out-of-order' | 'apply-failed' | 'error' | 'skipped' | 'control';
|
|
20
33
|
export type HandleReceivedMessageResult = {
|
|
21
34
|
ack: boolean;
|
|
35
|
+
outcome: ReceiveOutcome;
|
|
36
|
+
sequenceID: string;
|
|
37
|
+
/** The processed result, present on success — lets the channel route rejoin
|
|
38
|
+
* control frames to the responder/requester. */
|
|
39
|
+
result?: ReceiveBroadcastResult;
|
|
22
40
|
};
|
|
23
41
|
/**
|
|
24
42
|
* Apply a single hub-received message for a group.
|
|
@@ -28,6 +46,6 @@ export type HandleReceivedMessageResult = {
|
|
|
28
46
|
* no recovery path). Returns `{ ack: false }` when the hub should redeliver
|
|
29
47
|
* (transient apply failure or unexpected error). Skips with `{ ack: false }`
|
|
30
48
|
* on groupID mismatch so the message stays queued for whichever recipient it
|
|
31
|
-
* was intended for.
|
|
49
|
+
* was intended for. The `outcome` classifies the result for health tracking.
|
|
32
50
|
*/
|
|
33
51
|
export declare function handleReceivedMessage(params: HandleReceivedMessageParams): Promise<HandleReceivedMessageResult>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{fromB64 as e}from"@enkaku/codec";import{ApplyError as r,CommitOutOfOrderError as
|
|
1
|
+
import{fromB64 as e}from"@enkaku/codec";import{ApplyError as r,CommitOutOfOrderError as o,DecryptError as a,MalformedControlError as c}from"../groups/broadcast-service.js";export async function handleReceivedMessage(i){let t,{groupID:n,message:s,broadcastService:p,logger:u}=i,d=s.sequenceID;if(s.groupID!==n)return u?.debug("hub message groupID mismatch, skipping",{groupID:n,messageGroupID:s.groupID,sequenceID:d}),{ack:!1,outcome:"skipped",sequenceID:d};let l=e(s.payload);try{t=await p.processReceived({groupID:n,encrypted:l})}catch(e){if(e instanceof c)return u?.debug("malformed control frame, acking and skipping",{groupID:n,sequenceID:d,error:e}),{ack:!0,outcome:"control",sequenceID:d};if(e instanceof a)return u?.debug("decrypt failed, acking and skipping",{groupID:n,sequenceID:d,error:e}),{ack:!0,outcome:"decrypt-failed",sequenceID:d};if(e instanceof o)return u?.debug("mls commit out of order, will redeliver after gap fills",{groupID:n,sequenceID:d,messageEpoch:e.messageEpoch.toString(),handleEpoch:e.handleEpoch.toString()}),{ack:!1,outcome:"commit-out-of-order",sequenceID:d};if(e instanceof r)return u?.error("broadcast apply failed, will redeliver",{groupID:n,sequenceID:d,error:e}),{ack:!1,outcome:"apply-failed",sequenceID:d};return u?.error("unexpected error processing hub message",{groupID:n,sequenceID:d,error:e}),{ack:!1,outcome:"error",sequenceID:d}}return{ack:!0,outcome:"rejoin-request"===t.kind||"rejoin-groupinfo"===t.kind?"control":"applied",sequenceID:d,result:t}}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { type GroupHandle, type MemberCredential } from '@enkaku/group';
|
|
2
|
+
import type { OwnIdentity } from '@enkaku/token';
|
|
3
|
+
import type { Logger } from '@kubun/logger';
|
|
4
|
+
import type { GroupHealthMonitor } from '../groups/group-health-monitor.js';
|
|
5
|
+
import type { RejoinResult } from '../types.js';
|
|
6
|
+
export type JoinExternalResult = {
|
|
7
|
+
/** Framed MLSMessage(Commit) bytes to broadcast to existing members. */
|
|
8
|
+
commitMessage: Uint8Array;
|
|
9
|
+
/** Optimistic post-commit handle (NOT yet persisted). */
|
|
10
|
+
pendingHandle: GroupHandle;
|
|
11
|
+
};
|
|
12
|
+
export type RejoinManagerParams = {
|
|
13
|
+
groupID: string;
|
|
14
|
+
identity: OwnIdentity;
|
|
15
|
+
/** Read the stale handle's cached MemberCredential (reused for the rejoin). */
|
|
16
|
+
readCredential: () => Promise<MemberCredential>;
|
|
17
|
+
/** Fresh correlation id per request (incl. retries). */
|
|
18
|
+
genRequestID: () => string;
|
|
19
|
+
/** Broadcast a `rejoin-request` on the group channel. */
|
|
20
|
+
sendRequest: (requestID: string) => Promise<void>;
|
|
21
|
+
/** Broadcast the resync Commit (`mls` frame) to existing members. */
|
|
22
|
+
sendCommit: (commitBytes: Uint8Array) => Promise<void>;
|
|
23
|
+
/** Atomically swap the cached handle for the confirmed one (registry). */
|
|
24
|
+
replaceHandle: (handle: GroupHandle) => Promise<void>;
|
|
25
|
+
monitor: GroupHealthMonitor;
|
|
26
|
+
/**
|
|
27
|
+
* Test seam: defaults to `@enkaku/group` `joinGroupExternal({resync:true})`
|
|
28
|
+
* built from `identity` + `readCredential`. Override to skip real MLS.
|
|
29
|
+
*/
|
|
30
|
+
joinExternal?: (groupInfo: Uint8Array) => Promise<JoinExternalResult>;
|
|
31
|
+
/** Test seam: defaults to `@enkaku/group` `inspectGroupInfo`. */
|
|
32
|
+
inspectGroupInfo?: (groupInfo: Uint8Array) => {
|
|
33
|
+
epoch: bigint;
|
|
34
|
+
treeHash: Uint8Array;
|
|
35
|
+
};
|
|
36
|
+
requestTimeoutMs?: number;
|
|
37
|
+
confirmTimeoutMs?: number;
|
|
38
|
+
/** Delay between the candidate-head probe and its re-probe (settle window). */
|
|
39
|
+
confirmSettleMs?: number;
|
|
40
|
+
maxAttempts?: number;
|
|
41
|
+
backoffMs?: (attempt: number) => number;
|
|
42
|
+
logger?: Logger;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Drives a stale device's MLS epoch recovery for one group: per attempt,
|
|
46
|
+
* request GroupInfo → `joinGroupExternal({resync:true})` → broadcast the resync
|
|
47
|
+
* Commit → confirm → `replaceHandle`.
|
|
48
|
+
*
|
|
49
|
+
* Confirm is a tree-hash compare against the current head, because the hub never
|
|
50
|
+
* echoes the sender's own Commit. Three guards keep it reliable under concurrent
|
|
51
|
+
* rejoins (a resync Commit is canonical only for an instant):
|
|
52
|
+
* - behind-responder filter: a probe reply below our epoch can't confirm us.
|
|
53
|
+
* - chase-the-head: a reply above our epoch means we lost the race; re-join the
|
|
54
|
+
* newer GroupInfo next attempt (`{resync:true}` rebuilds the tree — no replay).
|
|
55
|
+
* - settle re-probe: re-probe after a delay and require the head still ours
|
|
56
|
+
* before swapping. Shrinks (not closes) the transient-head window — a Commit
|
|
57
|
+
* landing in the gap leaves us one epoch behind, which detect→rejoin catches.
|
|
58
|
+
* A fall-behind, never a silent fork; a wrong `recovered` is unreachable.
|
|
59
|
+
*
|
|
60
|
+
* Retries capped; exhaustion → `recovery-failed`.
|
|
61
|
+
*/
|
|
62
|
+
export declare class RejoinManager {
|
|
63
|
+
#private;
|
|
64
|
+
constructor(params: RejoinManagerParams);
|
|
65
|
+
get inFlight(): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Route a received `rejoin-groupinfo`: resolve the matching in-flight waiter.
|
|
68
|
+
* Unknown `requestID`s (peers' requests, stale responses) are ignored.
|
|
69
|
+
*/
|
|
70
|
+
onGroupInfo(requestID: string, groupInfo: Uint8Array): void;
|
|
71
|
+
/** Run the recovery flow. Idempotent: a concurrent call returns the loser early. */
|
|
72
|
+
rejoin(): Promise<RejoinResult>;
|
|
73
|
+
/**
|
|
74
|
+
* Stop a running rejoin and release timers. Idempotent; the loop checks
|
|
75
|
+
* `#disposed` between attempts, pending waiters resolve `null`. On channel close.
|
|
76
|
+
*/
|
|
77
|
+
dispose(): void;
|
|
78
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{inspectGroupInfo as e,joinGroupExternal as t}from"@enkaku/group";import{bytesEqual as r,sleep as i,unref as s}from"../utils.js";export class RejoinManager{#e;#t;#r;#i;#s;#o;#n;#a;#l;#u;#h;#c;#p;#m;#d=new Map;#f=!1;#g=!1;constructor(r){this.#e=r.groupID,this.#t=r.genRequestID,this.#r=r.sendRequest,this.#i=r.sendCommit,this.#s=r.replaceHandle,this.#o=r.monitor,this.#n=r.joinExternal??(async e=>{let i=await r.readCredential(),{commitMessage:s,group:o}=await t({identity:r.identity,groupInfo:e,credential:i,resync:!0});return{commitMessage:s,pendingHandle:o}}),this.#a=r.inspectGroupInfo??e,this.#l=r.requestTimeoutMs??5e3,this.#u=r.confirmTimeoutMs??5e3,this.#h=r.confirmSettleMs??300,this.#c=r.maxAttempts??8,this.#p=r.backoffMs??o,this.#m=r.logger}get inFlight(){return this.#f}onGroupInfo(e,t){let r=this.#d.get(e);null!=r&&(this.#d.delete(e),r.resolve(t))}async rejoin(){if(this.#f)return{status:"recovery-failed"};this.#f=!0,this.#o.markRecovering(this.#e);try{for(let e=0;e<this.#c&&!this.#g&&(await i(this.#p(e)),!this.#g);e++){let e=await this.#w();if("recovered"===e)return this.#o.markRecovered(this.#e),{status:"recovered"}}return this.#o.markRecoveryFailed(this.#e),{status:"recovery-failed"}}finally{this.#f=!1}}async #w(){let e,t=await this.#I(this.#l);if(null==t)return"retry";try{e=await this.#n(t)}catch(e){return this.#m?.warn("rejoin: joinGroupExternal failed",{groupID:this.#e,error:e}),"retry"}let{commitMessage:s,pendingHandle:o}=e,n=o.epoch,a=o.treeHash;try{await this.#i(s)}catch(e){return this.#m?.warn("rejoin: resync Commit send failed",{groupID:this.#e,error:e}),"retry"}let l=await this.#y(n);if(null==l||!(l.epoch===n&&r(l.treeHash,a)))return"retry";await i(this.#h);let u=await this.#y(n);return null==u?"retry":u.epoch===n&&r(u.treeHash,a)?(await this.#s(o),"recovered"):"retry"}async #y(e){let t,r=await this.#I(this.#u);if(null==r)return null;try{t=this.#a(r)}catch(e){return this.#m?.warn("rejoin: confirm GroupInfo inspect failed",{groupID:this.#e,error:e}),null}return t.epoch<e?null:t}async #I(e){let t=this.#t(),r=this.#M(t,e);try{await this.#r(t)}catch(e){return this.#D(t),this.#m?.warn("rejoin: request send failed",{groupID:this.#e,error:e}),null}return r}#M(e,t){return new Promise(r=>{let i=setTimeout(()=>{this.#d.delete(e),r(null)},t);s(i),this.#d.set(e,{resolve:e=>{clearTimeout(i),r(e)},cancel:()=>{clearTimeout(i),r(null)}})})}#D(e){this.#d.delete(e)}dispose(){for(let e of(this.#g=!0,this.#d.values()))e.cancel();this.#d.clear()}}function o(e){return Math.floor(Math.random()*Math.min(500*2**(e-1),5e3))}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Logger } from '@kubun/logger';
|
|
2
|
+
export type RejoinResponderParams = {
|
|
3
|
+
groupID: string;
|
|
4
|
+
/** Produce a fresh GroupInfo for this group (current MLS handle). */
|
|
5
|
+
exportGroupInfo: () => Promise<Uint8Array>;
|
|
6
|
+
/** Broadcast a `rejoin-groupinfo` reply to the group channel. */
|
|
7
|
+
sendGroupInfo: (requestID: string, groupInfo: Uint8Array) => Promise<void>;
|
|
8
|
+
/** Jitter delay (ms) before replying. Defaults to a random 0..250ms. */
|
|
9
|
+
nextJitterMs?: () => number;
|
|
10
|
+
/** How long a `requestID` stays suppressed after being answered. */
|
|
11
|
+
suppressTTLMs?: number;
|
|
12
|
+
logger?: Logger;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Answers `rejoin-request`s with a fresh GroupInfo. Every member could reply, so
|
|
16
|
+
* jitter (random pre-reply delay) + suppression (cancel on seeing a peer's reply
|
|
17
|
+
* for the same `requestID`) collapse the storm to ~1 reply. Keyed on `requestID`
|
|
18
|
+
* (fresh per attempt); suppression entries TTL out to bound memory.
|
|
19
|
+
*
|
|
20
|
+
* Suppression collapses only duplicates of one `requestID`, so distinct
|
|
21
|
+
* `requestID`s still draw one reply each — an amplification. A per-window reply
|
|
22
|
+
* cap is tracked in `docs/agents/plans/backlog/rejoin-frame-authentication.md`.
|
|
23
|
+
*/
|
|
24
|
+
export declare class RejoinResponder {
|
|
25
|
+
#private;
|
|
26
|
+
constructor(params: RejoinResponderParams);
|
|
27
|
+
/** Handle an inbound `rejoin-request`: schedule a jittered, suppressible reply. */
|
|
28
|
+
onRejoinRequest(requestID: string): void;
|
|
29
|
+
/** Handle an observed `rejoin-groupinfo` (own or peer's): suppress this request. */
|
|
30
|
+
onRejoinGroupInfoSeen(requestID: string): void;
|
|
31
|
+
dispose(): void;
|
|
32
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{unref as e}from"../utils.js";export class RejoinResponder{#e;#s;#t;#r;#p;#o;#i=new Map;#n=new Map;constructor(e){this.#e=e.groupID,this.#s=e.exportGroupInfo,this.#t=e.sendGroupInfo,this.#r=e.nextJitterMs??(()=>Math.floor(250*Math.random())),this.#p=e.suppressTTLMs??5e3,this.#o=e.logger}onRejoinRequest(s){if(this.#n.has(s)||this.#i.has(s))return;let t=setTimeout(()=>{this.#i.delete(s),this.#u(s)},this.#r());e(t),this.#i.set(s,t)}onRejoinGroupInfoSeen(e){let s=this.#i.get(e);null!=s&&(clearTimeout(s),this.#i.delete(e)),this.#h(e)}dispose(){for(let e of this.#i.values())clearTimeout(e);for(let e of(this.#i.clear(),this.#n.values()))clearTimeout(e);this.#n.clear()}async #u(e){if(!this.#n.has(e)){this.#h(e);try{let s=await this.#s();await this.#t(e,s)}catch(s){this.#o?.warn("rejoin responder failed to reply",{groupID:this.#e,requestID:e,error:s})}}}#h(s){let t=this.#n.get(s);null!=t&&clearTimeout(t);let r=setTimeout(()=>{this.#n.delete(s)},this.#p);e(r),this.#n.set(s,r)}}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import type { Client } from '@enkaku/client';
|
|
2
2
|
import { EventEmitter } from '@enkaku/event';
|
|
3
3
|
import type { HubProtocol } from '@enkaku/hub-protocol';
|
|
4
|
+
import type { OwnIdentity } from '@enkaku/token';
|
|
4
5
|
import type { Logger } from '@kubun/logger';
|
|
5
6
|
import type { P2PStoreAPI } from '@kubun/store-p2p';
|
|
6
7
|
import type { GroupBroadcastMessage } from '../groups/broadcast.js';
|
|
7
8
|
import type { BroadcastService } from '../groups/broadcast-service.js';
|
|
9
|
+
import type { GroupHealthMonitor } from '../groups/group-health-monitor.js';
|
|
10
|
+
import type { RejoinResult } from '../types.js';
|
|
8
11
|
import { type OnServerDIDObserved } from './connection-pool.js';
|
|
9
12
|
import type { ServerDIDMismatchObserver, ServerDIDObserver } from './did-observing-transport.js';
|
|
10
13
|
import { GroupChannel, type GroupChannelParams } from './group-channel.js';
|
|
@@ -72,6 +75,12 @@ export type HubRelayManagerParams = {
|
|
|
72
75
|
backoffMaxMs?: number;
|
|
73
76
|
backoffJitter?: number;
|
|
74
77
|
broadcastTimeoutMs?: number;
|
|
78
|
+
/** When provided, receive outcomes drive epoch-stale health detection. */
|
|
79
|
+
monitor?: GroupHealthMonitor;
|
|
80
|
+
/** Device identity — enables the rejoin flow (needs `joinGroupExternal`). */
|
|
81
|
+
identity?: OwnIdentity;
|
|
82
|
+
/** Fresh correlation ids for rejoin requests. */
|
|
83
|
+
getRandomID?: () => string;
|
|
75
84
|
createGroupChannel?: CreateGroupChannel;
|
|
76
85
|
createHubConnection?: CreateHubConnection;
|
|
77
86
|
/**
|
|
@@ -85,7 +94,7 @@ export type HubRelayManagerParams = {
|
|
|
85
94
|
};
|
|
86
95
|
export declare class HubRelayManager {
|
|
87
96
|
#private;
|
|
88
|
-
|
|
97
|
+
get relayEvents(): EventEmitter<RelayManagerEvents>;
|
|
89
98
|
constructor(params: HubRelayManagerParams);
|
|
90
99
|
/**
|
|
91
100
|
* Open channels for each group in parallel. Per-group failures are logged
|
|
@@ -112,6 +121,12 @@ export declare class HubRelayManager {
|
|
|
112
121
|
*/
|
|
113
122
|
removeBinding(groupID: string, hubURL: string): Promise<void>;
|
|
114
123
|
removeGroup(groupID: string): Promise<void>;
|
|
124
|
+
/**
|
|
125
|
+
* Run stale-device MLS epoch recovery for an active group via its channel.
|
|
126
|
+
* Throws if the group is not active; returns `recovery-failed` if the rejoin
|
|
127
|
+
* flow is unavailable (no monitor/identity wired).
|
|
128
|
+
*/
|
|
129
|
+
rejoinGroup(groupID: string): Promise<RejoinResult>;
|
|
115
130
|
broadcast(groupID: string, message: GroupBroadcastMessage, opts?: {
|
|
116
131
|
timeoutMs?: number;
|
|
117
132
|
}): Promise<void>;
|
package/lib/hub/relay-manager.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{EventEmitter as e}from"@enkaku/event";import{HubConnectionPool as t}from"./connection-pool.js";import{GroupChannel as s}from"./group-channel.js";import{HubConnection as
|
|
1
|
+
import{EventEmitter as e}from"@enkaku/event";import{HubConnectionPool as t}from"./connection-pool.js";import{GroupChannel as s}from"./group-channel.js";import{HubConnection as o}from"./hub-connection.js";export class HubRelayManager{#e;#t;#s;#o;#r;#i;#a;#n;#l;#c;#u;#h;#g;#p;#d;#b;#f=new Map;#m=new Map;#M=new Map;#v=new Map;#w;get relayEvents(){return this.#w}constructor(r){this.#e=r.p2pStore,this.#t=r.broadcastService,this.#s=r.deviceID,this.#r=r.logger,this.#i=r.ackFlushMs??500,this.#a=r.ackFlushMax??10,this.#n=r.backoffBaseMs??1e3,this.#l=r.backoffMaxMs??6e4,this.#c=r.backoffJitter??.25,this.#u=r.broadcastTimeoutMs,this.#h=r.monitor,this.#g=r.identity,this.#p=r.getRandomID;let i=r.p2pStore;this.#o=new t({createHubClient:r.createHubClient,logger:r.logger,onServerDIDObserved:r.onServerDIDObserved,resolvePinnedDID:async e=>{let t=await i.getHubByURL(e);return t?.server_did??null}}),this.#d=r.createGroupChannel??(e=>new s(e)),this.#b=r.createHubConnection??(e=>new o(e)),this.#w=new e}async #y(e){let t=this.#f.get(e);if(null!=t)return t;let s=this.#b({hubURL:e,deviceID:this.#s,pool:this.#o,logger:this.#r,ackFlushMs:this.#i,ackFlushMax:this.#a,backoffBaseMs:this.#n,backoffMaxMs:this.#l,backoffJitter:this.#c});try{await s.open()}catch(e){try{await s.dispose()}catch{}throw e}let o=s.events.on("lifecycle",async t=>{let s=Object.freeze([...this.#M.entries()].filter(([,t])=>t===e).map(([e])=>e));await this.#w.emit("relay",{...t,hubURL:e,groupIDs:s})});return this.#v.set(e,o),this.#f.set(e,s),s}async start(e){(await Promise.allSettled(e.map(e=>this.addGroup(e)))).forEach((t,s)=>{"rejected"===t.status&&this.#r?.error("addGroup failed during start",{groupID:e[s],error:t.reason})})}async addGroup(e){if(this.#m.has(e))return void this.#r?.debug("group already added",{groupID:e});if(null==await this.#e.getGroup(e))throw Error(`Group ${e} not found`);let t=await this.#e.listHubsByGroupID(e);0===t.length?this.#r?.warn("group has no hub bindings, skipping",{groupID:e}):(t.length>1&&this.#r?.warn("multi-hub not yet supported, using first",{groupID:e,hubURLs:t.map(e=>e.url)}),await this.#k(e,t[0].url))}async #k(e,t){let s=await this.#y(t),o=this.#d({groupID:e,deviceID:this.#s,hubConnection:s,broadcastService:this.#t,p2pStore:this.#e,logger:this.#r,monitor:this.#h,identity:this.#g,getRandomID:this.#p});await o.open(),this.#m.set(e,o),this.#M.set(e,t)}async addBinding(e,t){let s=this.#M.get(e);null!=s?s===t?this.#r?.debug("binding already active",{groupID:e,hubURL:t}):this.#r?.warn("multi-hub not yet supported, ignoring extra binding",{groupID:e,activeHubURL:s,newHubURL:t}):await this.#k(e,t)}async removeBinding(e,t){this.#M.get(e)!==t?this.#r?.debug("binding not active, ignoring removal",{groupID:e,hubURL:t}):await this.removeGroup(e)}async removeGroup(e){let t=this.#m.get(e);null!=t&&(await t.close(),this.#m.delete(e),this.#M.delete(e))}async rejoinGroup(e){let t=this.#m.get(e);if(null==t)throw Error(`Group ${e} is not active in HubRelayManager`);return await t.rejoinGroup()}async broadcast(e,t,s){let o=this.#m.get(e);if(null==o)throw Error(`Group ${e} is not active in HubRelayManager`);let r=s?.timeoutMs??this.#u;await o.broadcast(t,{timeoutMs:r})}async sendCommit(e,t,s){let o=this.#m.get(e);if(null==o)throw Error(`Group ${e} is not active in HubRelayManager`);let r=s?.timeoutMs??this.#u;await o.sendCommit(t,{timeoutMs:r})}async stop(){for(let e of this.#v.values())e();this.#v.clear();let e=[...this.#m.values()];this.#m.clear(),this.#M.clear(),(await Promise.allSettled(e.map(e=>e.close()))).forEach(e=>{"rejected"===e.status&&this.#r?.warn("GroupChannel close failed",{error:e.reason})});let t=[...this.#f.values()];this.#f.clear(),(await Promise.allSettled(t.map(e=>e.close()))).forEach(e=>{"rejected"===e.status&&this.#r?.warn("HubConnection close failed",{error:e.reason})}),await this.#o.disposeAll()}}
|
package/lib/hub/wiring.d.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import type { OwnIdentity } from '@enkaku/token';
|
|
2
2
|
import type { KubunDB } from '@kubun/db';
|
|
3
3
|
import type { DefaultAccessLevel, GraphInternals } from '@kubun/engine';
|
|
4
|
+
import type { HLC } from '@kubun/hlc';
|
|
4
5
|
import type { Logger } from '@kubun/logger';
|
|
5
6
|
import type { GroupBroadcastMessage } from '../groups/broadcast.js';
|
|
6
7
|
import { type BroadcastEvent } from '../groups/broadcast-service.js';
|
|
7
8
|
import type { P2PEventEmitter } from '../groups/events.js';
|
|
8
9
|
import type { GroupHandleRegistry } from '../groups/group-handle-registry.js';
|
|
10
|
+
import type { GroupHealthMonitor } from '../groups/group-health-monitor.js';
|
|
9
11
|
import type { ForwardingConfig } from '../sync/forwarder.js';
|
|
10
|
-
import type { StoreUnreadableMode } from '../types.js';
|
|
12
|
+
import type { RejoinResult, StoreUnreadableMode } from '../types.js';
|
|
11
13
|
import type { OnServerDIDObserved } from './connection-pool.js';
|
|
12
14
|
import type { GroupChannel, GroupChannelParams } from './group-channel.js';
|
|
13
15
|
import type { HubConnection, HubConnectionParams } from './hub-connection.js';
|
|
@@ -27,10 +29,18 @@ export type HubWiring = {
|
|
|
27
29
|
* group's existing members. Wired to `HubRelayManager.sendCommit`.
|
|
28
30
|
*/
|
|
29
31
|
scheduleSendCommit: (groupID: string, commitBytes: Uint8Array) => void;
|
|
32
|
+
/**
|
|
33
|
+
* Run stale-device MLS epoch recovery for an active group. Wired to
|
|
34
|
+
* `HubRelayManager.rejoinGroup`; a no-op `recovery-failed` under
|
|
35
|
+
* `DISABLED_HUB` (no relay, nothing to rejoin).
|
|
36
|
+
*/
|
|
37
|
+
rejoinGroup: (groupID: string) => Promise<RejoinResult>;
|
|
30
38
|
dispose: () => Promise<void>;
|
|
31
39
|
};
|
|
32
40
|
export type SetupHubRelayParams = {
|
|
33
41
|
identity: OwnIdentity;
|
|
42
|
+
/** Fresh correlation ids for the rejoin flow. */
|
|
43
|
+
getRandomID: () => string;
|
|
34
44
|
db: KubunDB;
|
|
35
45
|
/**
|
|
36
46
|
* Engine graph internals — threaded through to `BroadcastService` so the
|
|
@@ -47,6 +57,8 @@ export type SetupHubRelayParams = {
|
|
|
47
57
|
* mutex once Phase 3 lands. Currently stored but unused.
|
|
48
58
|
*/
|
|
49
59
|
registry: GroupHandleRegistry;
|
|
60
|
+
/** Shared health monitor; receive outcomes drive epoch-stale detection. */
|
|
61
|
+
monitor?: GroupHealthMonitor;
|
|
50
62
|
logger: Logger;
|
|
51
63
|
/**
|
|
52
64
|
* Optional testing seam forwarded to `HubRelayManager`. Production code
|
|
@@ -80,6 +92,12 @@ export type SetupHubRelayParams = {
|
|
|
80
92
|
* pinning; omitting it skips capture entirely.
|
|
81
93
|
*/
|
|
82
94
|
onServerDIDObserved?: OnServerDIDObserved;
|
|
95
|
+
/**
|
|
96
|
+
* Device-wide monotonic clock forwarded to `BroadcastService` for stamping
|
|
97
|
+
* `removed_at_hlc` on roster reconciliation. Required: production passes the
|
|
98
|
+
* engine's single instance so roster writes share the device's one clock.
|
|
99
|
+
*/
|
|
100
|
+
hlc: HLC;
|
|
83
101
|
};
|
|
84
102
|
export declare function setupHubRelay(params: SetupHubRelayParams): HubWiring;
|
|
85
103
|
export declare const DISABLED_HUB: HubWiring;
|
package/lib/hub/wiring.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{BroadcastService as e}from"../groups/broadcast-service.js";import{HubRelayManager as r}from"./relay-manager.js";export function setupHubRelay(a){let o,{identity:t,getRandomID:d,db:i,graph:s,emitter:n,createHubClient:c,onRemoteBroadcast:u,registry:p,monitor:l,logger:h,createGroupChannel:m,createHubConnection:g,storeUnreadable:f,defaultAccessLevel:y,forwarding:w,onServerDIDObserved:b,hlc:v}=a,D=[],B=(e,r)=>{o.then(a=>a.broadcast(e,r).catch(r=>{h.error("broadcast failed",{groupID:e,error:r})}),()=>{})},G=async(e,r)=>{let a=await o;await a.broadcast(e,r)},I=async e=>{let r=await o;return await r.rejoinGroup(e)};return(o=(async()=>{let[a,o]=await Promise.all([i.getStore("p2p"),i.getStore("graph")]),G=new e({p2pStore:a,graphStore:o,graph:s,selfDID:t.id,registry:p,hlc:v,storeUnreadable:f,defaultAccessLevel:y,forwarding:w,scheduleBroadcast:B,emitter:n,logger:h}),I=new r({p2pStore:a,broadcastService:G,deviceID:t.id,createHubClient:c,monitor:l,identity:t,getRandomID:d,logger:h,createGroupChannel:m,createHubConnection:g,onServerDIDObserved:b});D.push(G.on("broadcast",u)),D.push(n.on("groupJoined",e=>I.addGroup(e.id).catch(r=>{h.error("addGroup failed",{groupID:e.id,error:r})}))),D.push(n.on("groupLeft",e=>I.removeGroup(e.groupID).catch(r=>{h.error("removeGroup failed",{groupID:e.groupID,error:r})}))),D.push(n.on("hubBound",({groupID:e,hubURL:r})=>I.addBinding(e,r).catch(a=>{h.error("addBinding failed",{groupID:e,hubURL:r,error:a})}))),D.push(n.on("hubUnbound",({groupID:e,hubURL:r})=>I.removeBinding(e,r).catch(a=>{h.error("removeBinding failed",{groupID:e,hubURL:r,error:a})})));let S=await a.listGroups();return await I.start(S.map(e=>e.id)),I})()).catch(e=>{h.error("hub relay setup failed",{error:e})}),{ready:o.then(()=>void 0),scheduleBroadcast:B,broadcastNow:G,scheduleSendCommit:(e,r)=>{o.then(a=>a.sendCommit(e,r).catch(r=>{h.error("sendCommit failed",{groupID:e,error:r})}),()=>{})},rejoinGroup:I,dispose:async()=>{for(let e of D)try{e()}catch{}D.length=0;try{let e=await o;await e.stop()}catch{}}}}export const DISABLED_HUB={ready:Promise.resolve(),scheduleBroadcast:()=>{},broadcastNow:async()=>{},scheduleSendCommit:()=>{},rejoinGroup:async()=>({status:"recovery-failed"}),dispose:async()=>{}};
|