@serfab/cadre-core 0.7.1 → 0.9.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 +11 -10
- package/dist/cadre-node.d.ts +818 -19
- package/dist/cadre-node.js +1952 -99
- package/dist/cadre-node.js.map +1 -1
- package/dist/canonical-datetime.d.ts +33 -0
- package/dist/canonical-datetime.js +38 -0
- package/dist/canonical-datetime.js.map +1 -0
- package/dist/canonical-json.d.ts +12 -0
- package/dist/canonical-json.js +33 -0
- package/dist/canonical-json.js.map +1 -0
- package/dist/control-cohort.d.ts +63 -0
- package/dist/control-cohort.js +80 -0
- package/dist/control-cohort.js.map +1 -0
- package/dist/control-database.d.ts +279 -8
- package/dist/control-database.js +563 -134
- package/dist/control-database.js.map +1 -1
- package/dist/control-formation-recorder.d.ts +89 -0
- package/dist/control-formation-recorder.js +139 -0
- package/dist/control-formation-recorder.js.map +1 -0
- package/dist/control-schema.d.ts +12 -0
- package/dist/control-schema.js +223 -0
- package/dist/control-schema.js.map +1 -0
- package/dist/control-stream.d.ts +54 -0
- package/dist/control-stream.js +104 -0
- package/dist/control-stream.js.map +1 -0
- package/dist/device-token.d.ts +50 -0
- package/dist/device-token.js +66 -0
- package/dist/device-token.js.map +1 -0
- package/dist/diagnostics/connection-path.d.ts +118 -0
- package/dist/diagnostics/connection-path.js +204 -0
- package/dist/diagnostics/connection-path.js.map +1 -0
- package/dist/diagnostics/webrtc-turn-tracker.d.ts +50 -0
- package/dist/diagnostics/webrtc-turn-tracker.js +196 -0
- package/dist/diagnostics/webrtc-turn-tracker.js.map +1 -0
- package/dist/ed25519-key.d.ts +42 -0
- package/dist/ed25519-key.js +51 -0
- package/dist/ed25519-key.js.map +1 -0
- package/dist/hibernation-manager.d.ts +79 -0
- package/dist/hibernation-manager.js +173 -23
- package/dist/hibernation-manager.js.map +1 -1
- package/dist/index.d.ts +25 -4
- package/dist/index.js +59 -4
- package/dist/index.js.map +1 -1
- package/dist/key-store-file.d.ts +52 -0
- package/dist/key-store-file.js +194 -0
- package/dist/key-store-file.js.map +1 -0
- package/dist/key-store.d.ts +75 -0
- package/dist/key-store.js +66 -0
- package/dist/key-store.js.map +1 -0
- package/dist/peer-authorization.d.ts +48 -0
- package/dist/peer-authorization.js +66 -0
- package/dist/peer-authorization.js.map +1 -0
- package/dist/peer-record.d.ts +89 -0
- package/dist/peer-record.js +117 -0
- package/dist/peer-record.js.map +1 -0
- package/dist/push-credentials.d.ts +34 -0
- package/dist/push-credentials.js +81 -0
- package/dist/push-credentials.js.map +1 -0
- package/dist/push-fanout.d.ts +137 -0
- package/dist/push-fanout.js +197 -0
- package/dist/push-fanout.js.map +1 -0
- package/dist/push-node.d.ts +38 -0
- package/dist/push-node.js +48 -0
- package/dist/push-node.js.map +1 -0
- package/dist/push-notifier-apns.d.ts +61 -0
- package/dist/push-notifier-apns.js +199 -0
- package/dist/push-notifier-apns.js.map +1 -0
- package/dist/push-notifier-fcm.d.ts +44 -0
- package/dist/push-notifier-fcm.js +182 -0
- package/dist/push-notifier-fcm.js.map +1 -0
- package/dist/push-notifier-shared.d.ts +18 -0
- package/dist/push-notifier-shared.js +25 -0
- package/dist/push-notifier-shared.js.map +1 -0
- package/dist/push-notifier.d.ts +52 -0
- package/dist/push-notifier.js +22 -0
- package/dist/push-notifier.js.map +1 -0
- package/dist/schema-verification.d.ts +11 -2
- package/dist/schema-verification.js +15 -4
- package/dist/schema-verification.js.map +1 -1
- package/dist/seed-bootstrap.d.ts +228 -21
- package/dist/seed-bootstrap.js +489 -168
- package/dist/seed-bootstrap.js.map +1 -1
- package/dist/seed-trust-policy.d.ts +63 -0
- package/dist/seed-trust-policy.js +56 -0
- package/dist/seed-trust-policy.js.map +1 -0
- package/dist/strand-addr-protocol.d.ts +132 -0
- package/dist/strand-addr-protocol.js +313 -0
- package/dist/strand-addr-protocol.js.map +1 -0
- package/dist/strand-cohort.d.ts +54 -0
- package/dist/strand-cohort.js +38 -0
- package/dist/strand-cohort.js.map +1 -0
- package/dist/strand-database.d.ts +54 -6
- package/dist/strand-database.js +88 -100
- package/dist/strand-database.js.map +1 -1
- package/dist/strand-formation-manager.d.ts +79 -19
- package/dist/strand-formation-manager.js +196 -104
- package/dist/strand-formation-manager.js.map +1 -1
- package/dist/strand-formation-protocol.d.ts +162 -0
- package/dist/strand-formation-protocol.js +279 -0
- package/dist/strand-formation-protocol.js.map +1 -0
- package/dist/strand-instance-manager.d.ts +73 -4
- package/dist/strand-instance-manager.js +167 -41
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-member-key.d.ts +29 -0
- package/dist/strand-member-key.js +38 -0
- package/dist/strand-member-key.js.map +1 -0
- package/dist/strand-member-registry.d.ts +98 -0
- package/dist/strand-member-registry.js +129 -0
- package/dist/strand-member-registry.js.map +1 -0
- package/dist/strand-membership-writer.d.ts +361 -0
- package/dist/strand-membership-writer.js +449 -0
- package/dist/strand-membership-writer.js.map +1 -0
- package/dist/strand-solicitation.d.ts +77 -2
- package/dist/strand-solicitation.js +25 -4
- package/dist/strand-solicitation.js.map +1 -1
- package/dist/strand-wake-payload.d.ts +30 -0
- package/dist/strand-wake-payload.js +20 -0
- package/dist/strand-wake-payload.js.map +1 -0
- package/dist/strand-wake-protocol.d.ts +114 -0
- package/dist/strand-wake-protocol.js +257 -0
- package/dist/strand-wake-protocol.js.map +1 -0
- package/dist/strand-watcher.d.ts +6 -2
- package/dist/strand-watcher.js +57 -17
- package/dist/strand-watcher.js.map +1 -1
- package/dist/types.d.ts +436 -33
- package/dist/types.js +22 -5
- package/dist/types.js.map +1 -1
- package/package.json +18 -8
- package/src/cadre-node.ts +2981 -948
- package/src/canonical-datetime.ts +39 -0
- package/src/canonical-json.ts +30 -0
- package/src/control-cohort.ts +103 -0
- package/src/control-database.ts +948 -383
- package/src/control-formation-recorder.ts +151 -0
- package/src/control-schema.ts +222 -0
- package/src/control-stream.ts +128 -0
- package/src/device-token.ts +89 -0
- package/src/diagnostics/connection-path.ts +277 -0
- package/src/diagnostics/webrtc-turn-tracker.ts +222 -0
- package/src/ed25519-key.ts +69 -0
- package/src/hibernation-manager.ts +197 -28
- package/src/index.ts +275 -79
- package/src/key-store-file.ts +203 -0
- package/src/key-store.ts +99 -0
- package/src/peer-authorization.ts +81 -0
- package/src/peer-record.ts +146 -0
- package/src/push-credentials.ts +84 -0
- package/src/push-fanout.ts +272 -0
- package/src/push-node.ts +64 -0
- package/src/push-notifier-apns.ts +247 -0
- package/src/push-notifier-fcm.ts +220 -0
- package/src/push-notifier-shared.ts +28 -0
- package/src/push-notifier.ts +52 -0
- package/src/schema-verification.ts +18 -4
- package/src/seed-bootstrap.ts +563 -223
- package/src/seed-trust-policy.ts +107 -0
- package/src/strand-addr-protocol.ts +410 -0
- package/src/strand-cohort.ts +77 -0
- package/src/strand-database.ts +113 -141
- package/src/strand-formation-manager.ts +233 -142
- package/src/strand-formation-protocol.ts +391 -0
- package/src/strand-instance-manager.ts +207 -56
- package/src/strand-member-key.ts +39 -0
- package/src/strand-member-registry.ts +177 -0
- package/src/strand-membership-writer.ts +640 -0
- package/src/strand-solicitation.ts +93 -4
- package/src/strand-wake-payload.ts +32 -0
- package/src/strand-wake-protocol.ts +331 -0
- package/src/strand-watcher.ts +61 -17
- package/src/types.ts +1032 -582
|
@@ -13,6 +13,10 @@ import {
|
|
|
13
13
|
StrandFormationManager,
|
|
14
14
|
type StrandFormationManagerConfig
|
|
15
15
|
} from './strand-formation-manager.js';
|
|
16
|
+
import {
|
|
17
|
+
isValidResponderCreatesResult,
|
|
18
|
+
type FormationResultMessage
|
|
19
|
+
} from './strand-formation-protocol.js';
|
|
16
20
|
|
|
17
21
|
const log = debug('sereus:cadre:solicitation');
|
|
18
22
|
|
|
@@ -29,6 +33,26 @@ export interface DisclosureValidator {
|
|
|
29
33
|
validateDisclosure(token: string, disclosure: StrandFormationDisclosure): Promise<boolean>;
|
|
30
34
|
}
|
|
31
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Discriminated result of resolving the host strand an invite binds to (responder side):
|
|
38
|
+
*
|
|
39
|
+
* - `unbound`: the invite carries no `StrandId` → the responder-provisions path (provision
|
|
40
|
+
* a NEW strand; legacy/open).
|
|
41
|
+
* - `bound`: the invite names a host strand AND that strand row is present on this responder
|
|
42
|
+
* → provision-then-record (record consent against it, return it + its membership key).
|
|
43
|
+
* - `missing`: the invite names a host strand but the row is ABSENT on this responder (e.g.
|
|
44
|
+
* not yet converged) → the manager rejects cleanly rather than recording usage against a
|
|
45
|
+
* non-existent strand, which would fail the deferred `FormationUsage.StrandExists` CHECK at
|
|
46
|
+
* commit and drop the result frame.
|
|
47
|
+
*
|
|
48
|
+
* Replaces the older `{ strandId, memberPrivateKey } | null` shape, which conflated `bound`
|
|
49
|
+
* and `missing` (both produced a non-null result whenever the invite had a `StrandId`).
|
|
50
|
+
*/
|
|
51
|
+
export type ResolvedHostStrand =
|
|
52
|
+
| { kind: 'unbound' }
|
|
53
|
+
| { kind: 'bound'; strandId: string; memberPrivateKey: string | null }
|
|
54
|
+
| { kind: 'missing'; strandId: string };
|
|
55
|
+
|
|
32
56
|
/**
|
|
33
57
|
* Interface for recording formation usage
|
|
34
58
|
*/
|
|
@@ -47,6 +71,29 @@ export interface FormationUsageRecorder {
|
|
|
47
71
|
* Check if a token is valid and not expired
|
|
48
72
|
*/
|
|
49
73
|
isTokenValid(token: string): Promise<{ valid: boolean; invitation?: OpenInvitation }>;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Resolve the host strand an invite binds to, classifying it as unbound / bound /
|
|
77
|
+
* missing (see {@link ResolvedHostStrand}). Optional: a recorder that does not bind
|
|
78
|
+
* strands omits it, and {@link StrandFormationManager} treats every invite as `unbound`
|
|
79
|
+
* and falls back to its provisioner. Keeping resolution behind this interface keeps the
|
|
80
|
+
* manager DB-agnostic and unit-testable with an in-memory fake.
|
|
81
|
+
*/
|
|
82
|
+
resolveStrand?(token: string): Promise<ResolvedHostStrand>;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Provision a NEW strand for an UNBOUND invite and record consent against it
|
|
86
|
+
* ATOMICALLY (single `FormationUsage` row → single-use enforced on the next redemption).
|
|
87
|
+
* Optional: a recorder that only supports provision-then-record omits it, and the
|
|
88
|
+
* manager falls back to its {@link StrandProvisioner}. Mirrors the create-strand-by-consent
|
|
89
|
+
* path the schema's `Strand.Authorized` `FormationUsage` branch authorizes. Returns the
|
|
90
|
+
* minted strand id plus its membership key (null for an open responder-provisioned strand).
|
|
91
|
+
*/
|
|
92
|
+
provisionAndRecord?(
|
|
93
|
+
token: string,
|
|
94
|
+
initiatorKey: string,
|
|
95
|
+
sAppId: string
|
|
96
|
+
): Promise<{ strandId: string; memberPrivateKey: string | null }>;
|
|
50
97
|
}
|
|
51
98
|
|
|
52
99
|
/**
|
|
@@ -76,11 +123,47 @@ export interface FormationSigner {
|
|
|
76
123
|
}>;
|
|
77
124
|
}
|
|
78
125
|
|
|
126
|
+
/**
|
|
127
|
+
* Interface for validating the responder's formation result on the initiator side.
|
|
128
|
+
*
|
|
129
|
+
* Symmetric to {@link DisclosureValidator} (which runs responder→initiator): it lets
|
|
130
|
+
* the initiator verify the responder's disclosed identity, cadre, and provisioned
|
|
131
|
+
* strand against the invitation/disclosure it used to form the strand. The manager
|
|
132
|
+
* owns one dialer session per `formStrand` call, so the per-session invitation +
|
|
133
|
+
* disclosure context is local — no hook-signature gymnastics needed.
|
|
134
|
+
*/
|
|
135
|
+
export interface FormationResponseValidator {
|
|
136
|
+
/** Validate the responder's result against the invitation/disclosure used to form the strand. */
|
|
137
|
+
validateResponse(ctx: {
|
|
138
|
+
invitation: OpenInvitation;
|
|
139
|
+
disclosure: StrandFormationDisclosure;
|
|
140
|
+
response: FormationResultMessage;
|
|
141
|
+
}): Promise<boolean>;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Built-in structural {@link FormationResponseValidator}.
|
|
146
|
+
*
|
|
147
|
+
* `validateResponse` rejects when the responder did not approve, omitted a disclosed
|
|
148
|
+
* identity, returned no/placeholder cadre addresses, or returned a missing/empty or
|
|
149
|
+
* non-responder-created strand (see {@link isValidResponderCreatesResult}).
|
|
150
|
+
* Apps can supply a stricter validator via {@link StrandSolicitationServiceOptions}.
|
|
151
|
+
*/
|
|
152
|
+
export function createDefaultFormationResponseValidator(): FormationResponseValidator {
|
|
153
|
+
return {
|
|
154
|
+
async validateResponse({ response }) {
|
|
155
|
+
return isValidResponderCreatesResult(response);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
79
160
|
export interface StrandSolicitationServiceOptions {
|
|
80
161
|
disclosureValidator?: DisclosureValidator;
|
|
81
162
|
formationUsageRecorder?: FormationUsageRecorder;
|
|
82
163
|
strandProvisioner?: StrandProvisioner;
|
|
83
164
|
formationSigner?: FormationSigner;
|
|
165
|
+
/** Validates the responder's result on the initiator side (defaults to a structural check) */
|
|
166
|
+
formationResponseValidator?: FormationResponseValidator;
|
|
84
167
|
/** Party ID for this node (used in protocol messages) */
|
|
85
168
|
partyId?: string;
|
|
86
169
|
/** Cadre peer addresses for this node */
|
|
@@ -96,14 +179,15 @@ export interface StrandSolicitationServiceOptions {
|
|
|
96
179
|
* - formStrand(invitation, disclosure, node) - called by initiator
|
|
97
180
|
* - validateStrandFormation(token, disclosure) - called by responder
|
|
98
181
|
*
|
|
99
|
-
* When a libp2p node is provided, the underlying protocol is handled by
|
|
100
|
-
*
|
|
182
|
+
* When a libp2p node is provided, the underlying protocol is handled by the
|
|
183
|
+
* native cadre-core formation transport via StrandFormationManager.
|
|
101
184
|
*/
|
|
102
185
|
export class StrandSolicitationService {
|
|
103
186
|
private readonly disclosureValidator?: DisclosureValidator;
|
|
104
187
|
private readonly formationUsageRecorder?: FormationUsageRecorder;
|
|
105
188
|
private readonly strandProvisioner?: StrandProvisioner;
|
|
106
189
|
private readonly formationSigner?: FormationSigner;
|
|
190
|
+
private readonly formationResponseValidator?: FormationResponseValidator;
|
|
107
191
|
private readonly partyId: string;
|
|
108
192
|
private readonly cadrePeerAddrs: string[];
|
|
109
193
|
private formationManager?: StrandFormationManager;
|
|
@@ -114,6 +198,7 @@ export class StrandSolicitationService {
|
|
|
114
198
|
this.formationUsageRecorder = options?.formationUsageRecorder;
|
|
115
199
|
this.strandProvisioner = options?.strandProvisioner;
|
|
116
200
|
this.formationSigner = options?.formationSigner;
|
|
201
|
+
this.formationResponseValidator = options?.formationResponseValidator;
|
|
117
202
|
this.partyId = options?.partyId ?? `party-${Date.now()}`;
|
|
118
203
|
this.cadrePeerAddrs = options?.cadrePeerAddrs ?? [];
|
|
119
204
|
this.formationConfig = options?.formationConfig;
|
|
@@ -130,6 +215,7 @@ export class StrandSolicitationService {
|
|
|
130
215
|
disclosureValidator: this.disclosureValidator,
|
|
131
216
|
formationUsageRecorder: this.formationUsageRecorder,
|
|
132
217
|
strandProvisioner: this.strandProvisioner,
|
|
218
|
+
formationResponseValidator: this.formationResponseValidator,
|
|
133
219
|
partyId: this.partyId,
|
|
134
220
|
cadrePeerAddrs: this.cadrePeerAddrs,
|
|
135
221
|
config: this.formationConfig
|
|
@@ -188,7 +274,7 @@ export class StrandSolicitationService {
|
|
|
188
274
|
|
|
189
275
|
// If we have a full invitation and a node, use the real protocol
|
|
190
276
|
if (typeof invitation !== 'string' && node) {
|
|
191
|
-
log('Using
|
|
277
|
+
log('Using native cadre-core formation transport');
|
|
192
278
|
const result = await this.getFormationManager().formStrand(
|
|
193
279
|
invitation,
|
|
194
280
|
{ ...disclosure, partyId: memberKey },
|
|
@@ -197,7 +283,10 @@ export class StrandSolicitationService {
|
|
|
197
283
|
return {
|
|
198
284
|
memberKey,
|
|
199
285
|
invitePrivateKey,
|
|
200
|
-
strandId: result.strandId
|
|
286
|
+
strandId: result.strandId,
|
|
287
|
+
// Carry the host strand's membership key delivered over the protocol (closed-strand
|
|
288
|
+
// provision-then-record). invitePrivateKey stays the initiator's generated signing key.
|
|
289
|
+
memberPrivateKey: result.memberPrivateKey
|
|
201
290
|
};
|
|
202
291
|
}
|
|
203
292
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* strand-wake-payload.ts — the canonical wire contract for a strand-wake data
|
|
3
|
+
* message, shared by the server-side sender (`PushNotifier`, this package) and
|
|
4
|
+
* the mobile receiver (`@serfab/reference-app-rn`'s `push-wake.ts`).
|
|
5
|
+
*
|
|
6
|
+
* A strand-wake is a data-only push delivered to an OS-suspended phone over its
|
|
7
|
+
* platform channel (FCM/APNs) when a control-network dial cannot reach the
|
|
8
|
+
* hibernating process. It names the strand that has pending activity so the phone
|
|
9
|
+
* can run one `serviceWake` cycle and re-hibernate.
|
|
10
|
+
*
|
|
11
|
+
* This module is intentionally dependency-free (no node/RN/browser-specific
|
|
12
|
+
* imports) so it sits cleanly on the cross-platform `cadre-core` entry and can be
|
|
13
|
+
* re-exported by the RN receive module without dragging any platform code across
|
|
14
|
+
* the send/receive boundary. The defensive `parseStrandWakePayload` parser stays
|
|
15
|
+
* on the receive side (it guards untrusted wire input); the sender constructs
|
|
16
|
+
* payloads from typed fields and never parses.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** Discriminator value carried by a strand-wake data message. */
|
|
20
|
+
export const STRAND_WAKE_TYPE = 'strand-wake';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A data-only push payload telling a suspended phone that `strandId` has pending
|
|
24
|
+
* activity. Delivered addressed to the phone's resolved `DeviceToken`.
|
|
25
|
+
*/
|
|
26
|
+
export interface StrandWakePayload {
|
|
27
|
+
type: typeof STRAND_WAKE_TYPE;
|
|
28
|
+
/** The strand the wake is for. */
|
|
29
|
+
strandId: string;
|
|
30
|
+
/** Why the wake was sent (e.g. `'activity'`). Free-form, for diagnostics. */
|
|
31
|
+
reason: string;
|
|
32
|
+
}
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Control-network push-wake transport.
|
|
3
|
+
*
|
|
4
|
+
* Lets a same-cadre peer — typically an always-on server that participates in a
|
|
5
|
+
* strand and sees new activity — signal a hibernating cadre peer to bring that
|
|
6
|
+
* strand online, pull pending activity, and re-hibernate. Push-wake travels the
|
|
7
|
+
* **control network** (the per-party network connecting this party's own cadre
|
|
8
|
+
* nodes), which is the only network a hibernating peer keeps connected.
|
|
9
|
+
*
|
|
10
|
+
* Modeled directly on `seed-bootstrap.ts`: a dedicated libp2p protocol id,
|
|
11
|
+
* 4-byte big-endian length-prefixed JSON frames, `node.handle` for the receiver,
|
|
12
|
+
* `node.dialProtocol` for the sender, and the shared `ControlStream` primitives
|
|
13
|
+
* from `control-stream.ts`. The exchange is a single request → single ack on one
|
|
14
|
+
* stream (like seed delivery), so each side reads to EOF (under a read timeout)
|
|
15
|
+
* and decodes one frame via the shared {@link decodeLengthPrefixedFrame} guard.
|
|
16
|
+
*
|
|
17
|
+
* **Authorization (v1):** the control network already restricts membership —
|
|
18
|
+
* only this party's cadre peers connect (schema-gated `CadrePeer`). A wake is
|
|
19
|
+
* low-risk: it only causes the receiver to spend resources coming online for a
|
|
20
|
+
* strand it already participates in. So the receiver verifies the remote peer is
|
|
21
|
+
* a `CadrePeer` member (via the injected `isMember` gate) and requires no
|
|
22
|
+
* signature beyond control-network membership. A richer per-request signature is
|
|
23
|
+
* deliberately out of scope for v1.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import debug from 'debug';
|
|
27
|
+
import type { Libp2p, Connection } from '@libp2p/interface';
|
|
28
|
+
import type { Multiaddr } from '@multiformats/multiaddr';
|
|
29
|
+
import type { StrandInstance, WakeRequest, WakeAck } from './types.js';
|
|
30
|
+
import { decodeLengthPrefixedFrame } from './seed-bootstrap.js';
|
|
31
|
+
import { type ControlStream, writeFrame, withTimeout, readStreamToEnd } from './control-stream.js';
|
|
32
|
+
|
|
33
|
+
const log = debug('sereus:cadre:strand-wake');
|
|
34
|
+
|
|
35
|
+
/** Protocol id for control-network push-wake (parallel to `/sereus/seed/1.0.0`). */
|
|
36
|
+
export const WAKE_PROTOCOL = '/sereus/strand-wake/1.0.0';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Maximum wake frame size. Wake messages are tiny (a strand id + short reason),
|
|
40
|
+
* so this is a defensive cap — far below the 1MB seed ceiling — that bounds the
|
|
41
|
+
* bytes a peer can make the receiver buffer per stream.
|
|
42
|
+
*/
|
|
43
|
+
const MAX_WAKE_SIZE = 64 * 1024;
|
|
44
|
+
|
|
45
|
+
/** Default time to wait for the ack before abandoning a wake dial (ms). */
|
|
46
|
+
const DEFAULT_WAKE_TIMEOUT_MS = 10_000;
|
|
47
|
+
|
|
48
|
+
/** Default time the receiver waits for an inbound wake frame before aborting (ms). */
|
|
49
|
+
const DEFAULT_WAKE_READ_TIMEOUT_MS = 10_000;
|
|
50
|
+
|
|
51
|
+
/** Default cap on concurrent inbound wake streams a single peer can pin open. */
|
|
52
|
+
const DEFAULT_MAX_CONCURRENT_WAKES = 100;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Read a libp2p stream to EOF and decode the single length-prefixed JSON frame
|
|
56
|
+
* it carries. Bounded by `timeoutMs` (a never-half-closing peer is aborted, not
|
|
57
|
+
* awaited forever) and capped at {@link MAX_WAKE_SIZE}, reusing the shared
|
|
58
|
+
* {@link decodeLengthPrefixedFrame} guard for the prefix/length checks.
|
|
59
|
+
*/
|
|
60
|
+
async function readFrame<T>(stream: ControlStream, timeoutMs: number): Promise<T> {
|
|
61
|
+
const data = await readStreamToEnd(stream, { maxBytes: MAX_WAKE_SIZE, timeoutMs, label: 'Wake' });
|
|
62
|
+
const body = decodeLengthPrefixedFrame(data, MAX_WAKE_SIZE);
|
|
63
|
+
return JSON.parse(new TextDecoder().decode(body)) as T;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Dependencies the {@link StrandWakeService} receiver needs from its host
|
|
68
|
+
* (`CadreNode`), injected so the service is testable without a full node.
|
|
69
|
+
*/
|
|
70
|
+
export interface StrandWakeServiceOptions {
|
|
71
|
+
/** Membership gate: is the remote peer a `CadrePeer` member of this cadre? */
|
|
72
|
+
isMember(remotePeerId: string): Promise<boolean>;
|
|
73
|
+
/** Look up a local strand instance by id (undefined if not participated in). */
|
|
74
|
+
getStrand(strandId: string): StrandInstance | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Trigger the local wake path for a hibernating/idle strand. Wired to
|
|
77
|
+
* `CadreNode.wakeStrand` (→ `HibernationManager` → `resumeStrand`), whose
|
|
78
|
+
* resume coalescing prevents a push-wake racing a concurrent check-in.
|
|
79
|
+
*/
|
|
80
|
+
wake(strandId: string): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Time to wait for the inbound wake frame before aborting the read (ms).
|
|
83
|
+
* Defaults to {@link DEFAULT_WAKE_READ_TIMEOUT_MS}. Bounds a buggy/compromised
|
|
84
|
+
* own-cadre node that opens a stream and never half-closes its write end.
|
|
85
|
+
*/
|
|
86
|
+
readTimeoutMs?: number;
|
|
87
|
+
/**
|
|
88
|
+
* Cap on concurrent inbound wake streams (defaults to
|
|
89
|
+
* {@link DEFAULT_MAX_CONCURRENT_WAKES}). Over the cap, a non-accepting ack is
|
|
90
|
+
* returned without invoking the wake path.
|
|
91
|
+
*/
|
|
92
|
+
maxConcurrent?: number;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Receiver side of the push-wake protocol. Registers a `WAKE_PROTOCOL` handler
|
|
97
|
+
* on the control node and, for each inbound {@link WakeRequest}, gates on cadre
|
|
98
|
+
* membership, then resumes the named strand if it is hibernating/idle and we
|
|
99
|
+
* participate in it — replying with a {@link WakeAck}.
|
|
100
|
+
*/
|
|
101
|
+
export class StrandWakeService {
|
|
102
|
+
private readonly options: StrandWakeServiceOptions;
|
|
103
|
+
private readonly readTimeoutMs: number;
|
|
104
|
+
private readonly maxConcurrent: number;
|
|
105
|
+
private node: Libp2p | null = null;
|
|
106
|
+
/** In-flight inbound wake streams, used to enforce {@link maxConcurrent}. */
|
|
107
|
+
private activeStreams = 0;
|
|
108
|
+
|
|
109
|
+
constructor(options: StrandWakeServiceOptions) {
|
|
110
|
+
this.options = options;
|
|
111
|
+
this.readTimeoutMs = options.readTimeoutMs ?? DEFAULT_WAKE_READ_TIMEOUT_MS;
|
|
112
|
+
this.maxConcurrent = options.maxConcurrent ?? DEFAULT_MAX_CONCURRENT_WAKES;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Number of in-flight inbound wake streams. */
|
|
116
|
+
get activeCount(): number {
|
|
117
|
+
return this.activeStreams;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Register the wake protocol handler on the control node. */
|
|
121
|
+
initialize(node: Libp2p): void {
|
|
122
|
+
this.node = node;
|
|
123
|
+
// `runOnLimitedConnection: true` is REQUIRED for the relay path: a NAT'd
|
|
124
|
+
// receiver is reached over a circuit-relay connection, which libp2p marks
|
|
125
|
+
// "limited" (the relay caps its data/duration). Without this the receiver
|
|
126
|
+
// would refuse the inbound wake stream on exactly the connection the
|
|
127
|
+
// protocol is designed to use (see the relay note on `dialWake`).
|
|
128
|
+
void node.handle(WAKE_PROTOCOL, async (rawStream: unknown, rawConnection: unknown) => {
|
|
129
|
+
const remotePeerId = (rawConnection as Connection).remotePeer.toString();
|
|
130
|
+
await this.handleStream(rawStream as ControlStream, remotePeerId);
|
|
131
|
+
}, { runOnLimitedConnection: true });
|
|
132
|
+
log('StrandWakeService registered handler: %s', WAKE_PROTOCOL);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Unregister the handler and release the node reference. */
|
|
136
|
+
async shutdown(): Promise<void> {
|
|
137
|
+
if (this.node) {
|
|
138
|
+
await this.node.unhandle(WAKE_PROTOCOL);
|
|
139
|
+
this.node = null;
|
|
140
|
+
log('StrandWakeService shutdown');
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Read the inbound request, decide + execute the wake, and write the ack.
|
|
146
|
+
*
|
|
147
|
+
* Three hardening layers, all reported as a non-accepting ack rather than a
|
|
148
|
+
* dropped/hung stream: a concurrency cap (over {@link maxConcurrent}, reply
|
|
149
|
+
* without touching the wake path), a read timeout (a peer that never
|
|
150
|
+
* half-closes is aborted inside {@link readFrame}/`readStreamToEnd`), and the
|
|
151
|
+
* existing malformed/oversized-frame guard.
|
|
152
|
+
*/
|
|
153
|
+
private async handleStream(stream: ControlStream, remotePeerId: string): Promise<void> {
|
|
154
|
+
log('Incoming wake request from: %s', remotePeerId);
|
|
155
|
+
|
|
156
|
+
if (this.activeStreams >= this.maxConcurrent) {
|
|
157
|
+
log('Rejecting wake from %s: %d concurrent streams at cap %d', remotePeerId, this.activeStreams, this.maxConcurrent);
|
|
158
|
+
const ack: WakeAck = { accepted: false, reason: 'Too many concurrent wake requests' };
|
|
159
|
+
try {
|
|
160
|
+
writeFrame(stream, ack);
|
|
161
|
+
} catch {
|
|
162
|
+
// Ignore send errors on the reject path.
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
await stream.close();
|
|
166
|
+
} catch {
|
|
167
|
+
// Ignore close errors.
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
this.activeStreams++;
|
|
173
|
+
try {
|
|
174
|
+
const request = await readFrame<WakeRequest>(stream, this.readTimeoutMs);
|
|
175
|
+
const ack = await this.processWakeRequest(request, remotePeerId);
|
|
176
|
+
writeFrame(stream, ack);
|
|
177
|
+
} catch (err) {
|
|
178
|
+
log('Error handling wake request from %s: %o', remotePeerId, err);
|
|
179
|
+
const ack: WakeAck = {
|
|
180
|
+
accepted: false,
|
|
181
|
+
reason: err instanceof Error ? err.message : 'Unknown error',
|
|
182
|
+
};
|
|
183
|
+
try {
|
|
184
|
+
writeFrame(stream, ack);
|
|
185
|
+
} catch {
|
|
186
|
+
// Ignore send errors on the error path.
|
|
187
|
+
}
|
|
188
|
+
} finally {
|
|
189
|
+
this.activeStreams--;
|
|
190
|
+
try {
|
|
191
|
+
await stream.close();
|
|
192
|
+
} catch {
|
|
193
|
+
// Ignore close errors.
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Decide and execute the wake for a decoded request. Exposed (not private) so
|
|
200
|
+
* the decision matrix can be unit-tested directly.
|
|
201
|
+
*
|
|
202
|
+
* - Non-member sender → rejected (`accepted: false`).
|
|
203
|
+
* - Unknown / not-participated strand → rejected.
|
|
204
|
+
* - Hibernating or idle strand → resumed via the wake path, then `accepted`.
|
|
205
|
+
* - Already-live strand → no-op, `accepted` with current status.
|
|
206
|
+
*/
|
|
207
|
+
async processWakeRequest(request: WakeRequest, remotePeerId: string): Promise<WakeAck> {
|
|
208
|
+
// Control-network membership is the v1 authorization: only this party's
|
|
209
|
+
// cadre peers may ask us to wake.
|
|
210
|
+
if (!(await this.options.isMember(remotePeerId))) {
|
|
211
|
+
log('Rejecting wake from non-member %s', remotePeerId);
|
|
212
|
+
return { accepted: false, reason: 'Sender is not a cadre member' };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const instance = this.options.getStrand(request.strandId);
|
|
216
|
+
if (!instance) {
|
|
217
|
+
log('Rejecting wake for unknown/unparticipated strand %s', request.strandId);
|
|
218
|
+
return { accepted: false, reason: 'Strand not found or not participated in' };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (instance.status === 'hibernating' || instance.status === 'idle') {
|
|
222
|
+
log('Waking strand %s on push from %s (reason=%s)', request.strandId, remotePeerId, request.reason ?? 'unspecified');
|
|
223
|
+
await this.options.wake(request.strandId);
|
|
224
|
+
} else {
|
|
225
|
+
log('Strand %s already %s; push-wake is a no-op', request.strandId, instance.status);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// The wake path mutates the shared instance, so re-read its current status.
|
|
229
|
+
return { accepted: true, status: instance.status };
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Options for {@link dialWake}. */
|
|
234
|
+
export interface DialWakeOptions {
|
|
235
|
+
/** Per-dial timeout in ms (default {@link DEFAULT_WAKE_TIMEOUT_MS}). */
|
|
236
|
+
timeoutMs?: number;
|
|
237
|
+
/** Override the protocol id (defaults to {@link WAKE_PROTOCOL}). */
|
|
238
|
+
protocolId?: string;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Sender side: dial a target's control-network address(es), send a
|
|
243
|
+
* {@link WakeRequest} over `WAKE_PROTOCOL`, and return the peer's {@link WakeAck}.
|
|
244
|
+
*
|
|
245
|
+
* Tries each candidate address in order (signaling/relay first, as produced by
|
|
246
|
+
* `CadreNode.resolvePeerAddrs`) until one dials, so a NAT'd peer is reachable via
|
|
247
|
+
* its circuit-relay address. Throws if no address is dialable.
|
|
248
|
+
*/
|
|
249
|
+
export async function dialWake(
|
|
250
|
+
node: Libp2p,
|
|
251
|
+
addrs: Multiaddr[],
|
|
252
|
+
request: WakeRequest,
|
|
253
|
+
options: DialWakeOptions = {}
|
|
254
|
+
): Promise<WakeAck> {
|
|
255
|
+
if (addrs.length === 0) {
|
|
256
|
+
throw new Error('No dialable address for wake target');
|
|
257
|
+
}
|
|
258
|
+
const protocolId = options.protocolId ?? WAKE_PROTOCOL;
|
|
259
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_WAKE_TIMEOUT_MS;
|
|
260
|
+
|
|
261
|
+
let lastError: Error | null = null;
|
|
262
|
+
for (const addr of addrs) {
|
|
263
|
+
// One controller per attempt: on timeout, `withTimeout`'s `onTimeout` aborts
|
|
264
|
+
// it, which aborts the in-flight dialProtocol (via the dial `signal`) and the
|
|
265
|
+
// live stream — so neither the connect nor the unbounded ack-read leaks.
|
|
266
|
+
const controller = new AbortController();
|
|
267
|
+
try {
|
|
268
|
+
return await withTimeout(
|
|
269
|
+
timeoutMs,
|
|
270
|
+
`Wake dial ${addr.toString()}`,
|
|
271
|
+
() => sendWake(node, addr, protocolId, request, timeoutMs, controller.signal),
|
|
272
|
+
() => controller.abort(),
|
|
273
|
+
);
|
|
274
|
+
} catch (err) {
|
|
275
|
+
lastError = err instanceof Error ? err : new Error(String(err));
|
|
276
|
+
log('Wake dial to %s failed: %o', addr.toString(), err);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
throw lastError ?? new Error('Wake dial failed');
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Open one stream, send the request, half-close, and read the ack.
|
|
284
|
+
*
|
|
285
|
+
* `signal` is the per-attempt abort signal from {@link dialWake}: it is passed to
|
|
286
|
+
* `dialProtocol` so a timeout during connect aborts the dial, and once the stream
|
|
287
|
+
* is open an abort listener resets the live stream — releasing the otherwise
|
|
288
|
+
* unbounded ack-read. `readFrame` still applies `timeoutMs` as a backstop for the
|
|
289
|
+
* case where an abort does not propagate (e.g. a stream double whose `abort()` is
|
|
290
|
+
* a no-op).
|
|
291
|
+
*/
|
|
292
|
+
async function sendWake(
|
|
293
|
+
node: Libp2p,
|
|
294
|
+
addr: Multiaddr,
|
|
295
|
+
protocolId: string,
|
|
296
|
+
request: WakeRequest,
|
|
297
|
+
timeoutMs: number,
|
|
298
|
+
signal: AbortSignal,
|
|
299
|
+
): Promise<WakeAck> {
|
|
300
|
+
// `runOnLimitedConnection: true`: the target may be reachable only over a
|
|
301
|
+
// circuit-relay connection (the signaling-first addr), which libp2p marks
|
|
302
|
+
// "limited". The wake exchange is a single tiny request→ack well within the
|
|
303
|
+
// relay's data/duration cap, so opening it on the limited connection is safe
|
|
304
|
+
// and is the whole point of dialing the relay address.
|
|
305
|
+
const rawStream = await node.dialProtocol(addr, protocolId, { runOnLimitedConnection: true, signal });
|
|
306
|
+
const stream = rawStream as unknown as ControlStream;
|
|
307
|
+
|
|
308
|
+
const abortErr = new Error('Wake dial aborted by timeout');
|
|
309
|
+
const onAbort = (): void => stream.abort(abortErr);
|
|
310
|
+
// If the timeout already fired during connect, release the freshly-opened stream now.
|
|
311
|
+
if (signal.aborted) {
|
|
312
|
+
onAbort();
|
|
313
|
+
throw abortErr;
|
|
314
|
+
}
|
|
315
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
316
|
+
|
|
317
|
+
try {
|
|
318
|
+
writeFrame(stream, request);
|
|
319
|
+
// close() half-closes the write end (EOF) while the read end stays open for
|
|
320
|
+
// the ack — the same libp2p 3.x pattern as seed delivery.
|
|
321
|
+
await stream.close();
|
|
322
|
+
const ack = await readFrame<WakeAck>(stream, timeoutMs);
|
|
323
|
+
log('Wake ack from %s: accepted=%s status=%s', addr.toString(), ack.accepted, ack.status ?? '-');
|
|
324
|
+
return ack;
|
|
325
|
+
} catch (err) {
|
|
326
|
+
stream.abort(err instanceof Error ? err : new Error(String(err)));
|
|
327
|
+
throw err;
|
|
328
|
+
} finally {
|
|
329
|
+
signal.removeEventListener('abort', onAbort);
|
|
330
|
+
}
|
|
331
|
+
}
|
package/src/strand-watcher.ts
CHANGED
|
@@ -3,6 +3,14 @@ import type { StrandFilter, StrandRow } from './types.js';
|
|
|
3
3
|
|
|
4
4
|
const log = debug('sereus:cadre:strand-watcher');
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Outcome of evaluating a strand against the current filter.
|
|
8
|
+
* - `pass`: admit (final)
|
|
9
|
+
* - `reject`: exclude
|
|
10
|
+
* - `defer`: admit provisionally; sAppId not yet known, re-evaluate next poll
|
|
11
|
+
*/
|
|
12
|
+
type FilterDecision = 'pass' | 'reject' | 'defer';
|
|
13
|
+
|
|
6
14
|
/**
|
|
7
15
|
* Extended strand row that includes the sAppId for filtering purposes.
|
|
8
16
|
* The sAppId is provided by the hosting application, not from the control network.
|
|
@@ -49,6 +57,8 @@ export class StrandWatcher {
|
|
|
49
57
|
private readonly sAppIdLookup?: SAppIdLookup;
|
|
50
58
|
|
|
51
59
|
private knownStrands: Map<string, StrandRow> = new Map();
|
|
60
|
+
/** Ids admitted under a `defer` decision; re-evaluated each poll until they resolve. */
|
|
61
|
+
private provisional: Set<string> = new Set();
|
|
52
62
|
private pollTimer: ReturnType<typeof setInterval> | null = null;
|
|
53
63
|
private initialPollTimer: ReturnType<typeof setTimeout> | null = null;
|
|
54
64
|
private running = false;
|
|
@@ -69,32 +79,36 @@ export class StrandWatcher {
|
|
|
69
79
|
}
|
|
70
80
|
|
|
71
81
|
/**
|
|
72
|
-
*
|
|
82
|
+
* Evaluate a strand against the current filter, distinguishing a not-yet-known
|
|
83
|
+
* sAppId (`defer`) from a known non-match (`reject`). A `defer` admission is
|
|
84
|
+
* provisional and re-checked on subsequent polls.
|
|
73
85
|
*/
|
|
74
|
-
private
|
|
86
|
+
private evaluateFilter(strand: StrandRow): FilterDecision {
|
|
75
87
|
switch (this.filter.mode) {
|
|
76
88
|
case 'all':
|
|
77
|
-
return
|
|
89
|
+
return 'pass';
|
|
78
90
|
case 'none':
|
|
79
|
-
return
|
|
91
|
+
return 'reject';
|
|
80
92
|
case 'strandId':
|
|
81
|
-
return strand.Id === this.filter.strandId;
|
|
93
|
+
return strand.Id === this.filter.strandId ? 'pass' : 'reject';
|
|
82
94
|
case 'sAppId': {
|
|
83
|
-
|
|
84
|
-
|
|
95
|
+
if (!this.sAppIdLookup) {
|
|
96
|
+
// No way to ever decide - admit permanently (no lookup configured).
|
|
97
|
+
return 'pass';
|
|
98
|
+
}
|
|
99
|
+
const sAppId = this.sAppIdLookup.getSAppId(strand.Id);
|
|
85
100
|
if (sAppId === undefined) {
|
|
86
|
-
//
|
|
87
|
-
// The strand will need to be re-evaluated when sApp info is available
|
|
101
|
+
// sAppId not yet known - admit provisionally and re-evaluate next poll.
|
|
88
102
|
log('sAppId unknown for strand %s - deferring filter decision', strand.Id);
|
|
89
|
-
return
|
|
103
|
+
return 'defer';
|
|
90
104
|
}
|
|
91
105
|
const matches = sAppId === this.filter.sAppId;
|
|
92
106
|
log('sAppId filter: strand %s has sAppId %s, filter wants %s, match=%s',
|
|
93
107
|
strand.Id, sAppId, this.filter.sAppId, matches);
|
|
94
|
-
return matches;
|
|
108
|
+
return matches ? 'pass' : 'reject';
|
|
95
109
|
}
|
|
96
110
|
default:
|
|
97
|
-
return
|
|
111
|
+
return 'pass';
|
|
98
112
|
}
|
|
99
113
|
}
|
|
100
114
|
|
|
@@ -110,15 +124,43 @@ export class StrandWatcher {
|
|
|
110
124
|
|
|
111
125
|
// Find added strands
|
|
112
126
|
for (const strand of currentStrands) {
|
|
113
|
-
if (
|
|
114
|
-
|
|
115
|
-
|
|
127
|
+
if (this.knownStrands.has(strand.Id)) continue;
|
|
128
|
+
const decision = this.evaluateFilter(strand);
|
|
129
|
+
if (decision === 'reject') continue;
|
|
130
|
+
log('Strand added: %s', strand.Id);
|
|
131
|
+
this.knownStrands.set(strand.Id, strand);
|
|
132
|
+
if (decision === 'defer') {
|
|
133
|
+
// Provisional admission: sAppId unknown, re-check on later polls.
|
|
134
|
+
this.provisional.add(strand.Id);
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
await this.callbacks.onStrandAdded(strand);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
log('Error handling strand add for %s: %o', strand.Id, error);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Re-evaluate provisional admissions whose sAppId may now be resolvable.
|
|
144
|
+
// Snapshot ids so we can mutate provisional/knownStrands inside the loop.
|
|
145
|
+
for (const strandId of [...this.provisional]) {
|
|
146
|
+
const strand = currentMap.get(strandId);
|
|
147
|
+
if (!strand) continue; // handled by the removed-strand loop below
|
|
148
|
+
const decision = this.evaluateFilter(strand);
|
|
149
|
+
if (decision === 'pass') {
|
|
150
|
+
// Resolved to a match - admission is now final.
|
|
151
|
+
this.provisional.delete(strandId);
|
|
152
|
+
} else if (decision === 'reject') {
|
|
153
|
+
// Resolved to a non-match - stop the provisionally-admitted strand.
|
|
154
|
+
log('Provisional strand rejected on re-evaluation: %s', strandId);
|
|
155
|
+
this.knownStrands.delete(strandId);
|
|
156
|
+
this.provisional.delete(strandId);
|
|
116
157
|
try {
|
|
117
|
-
await this.callbacks.
|
|
158
|
+
await this.callbacks.onStrandRemoved(strandId);
|
|
118
159
|
} catch (error) {
|
|
119
|
-
log('Error handling strand
|
|
160
|
+
log('Error handling strand remove for %s: %o', strandId, error);
|
|
120
161
|
}
|
|
121
162
|
}
|
|
163
|
+
// decision === 'defer': still unknown, leave provisional for next poll.
|
|
122
164
|
}
|
|
123
165
|
|
|
124
166
|
// Find removed strands
|
|
@@ -126,6 +168,7 @@ export class StrandWatcher {
|
|
|
126
168
|
if (!currentMap.has(strandId)) {
|
|
127
169
|
log('Strand removed: %s', strandId);
|
|
128
170
|
this.knownStrands.delete(strandId);
|
|
171
|
+
this.provisional.delete(strandId);
|
|
129
172
|
try {
|
|
130
173
|
await this.callbacks.onStrandRemoved(strandId);
|
|
131
174
|
} catch (error) {
|
|
@@ -187,6 +230,7 @@ export class StrandWatcher {
|
|
|
187
230
|
}
|
|
188
231
|
|
|
189
232
|
this.knownStrands.clear();
|
|
233
|
+
this.provisional.clear();
|
|
190
234
|
log('StrandWatcher stopped');
|
|
191
235
|
}
|
|
192
236
|
|