@serfab/cadre-host 1.13.0 → 1.14.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 +332 -423
- package/dist/bin/host.d.ts +6 -3
- package/dist/bin/host.js +301 -505
- package/dist/bin/host.js.map +1 -1
- package/dist/bin/join-output.d.ts +56 -0
- package/dist/bin/join-output.js +92 -0
- package/dist/bin/join-output.js.map +1 -0
- package/dist/bin/nat-output.d.ts +49 -0
- package/dist/bin/nat-output.js +127 -0
- package/dist/bin/nat-output.js.map +1 -0
- package/dist/hosted/claim-details.d.ts +44 -0
- package/dist/hosted/claim-details.js +66 -0
- package/dist/hosted/claim-details.js.map +1 -0
- package/dist/hosted/hosted-node-service.d.ts +318 -0
- package/dist/hosted/hosted-node-service.js +666 -0
- package/dist/hosted/hosted-node-service.js.map +1 -0
- package/dist/hosted/hosted-node-store.d.ts +40 -0
- package/dist/hosted/hosted-node-store.js +126 -0
- package/dist/hosted/hosted-node-store.js.map +1 -0
- package/dist/{donation/donation-supervisor.d.ts → hosted/hosted-node-supervisor.d.ts} +70 -53
- package/dist/hosted/hosted-node-supervisor.js +396 -0
- package/dist/hosted/hosted-node-supervisor.js.map +1 -0
- package/dist/hosted/hosted-node-watcher.d.ts +83 -0
- package/dist/hosted/hosted-node-watcher.js +224 -0
- package/dist/hosted/hosted-node-watcher.js.map +1 -0
- package/dist/hosted/index.d.ts +16 -0
- package/dist/hosted/index.js +13 -0
- package/dist/hosted/index.js.map +1 -0
- package/dist/hosted/node-status.d.ts +35 -0
- package/dist/hosted/node-status.js +61 -0
- package/dist/hosted/node-status.js.map +1 -0
- package/dist/hosted/types.d.ts +134 -0
- package/dist/hosted/types.js +22 -0
- package/dist/hosted/types.js.map +1 -0
- package/dist/index.d.ts +12 -24
- package/dist/index.js +9 -19
- package/dist/index.js.map +1 -1
- package/dist/installer/config.d.ts +4 -34
- package/dist/installer/config.js +3 -18
- package/dist/installer/config.js.map +1 -1
- package/dist/installer/index.d.ts +7 -19
- package/dist/installer/index.js +14 -69
- package/dist/installer/index.js.map +1 -1
- package/dist/installer/paths.d.ts +0 -2
- package/dist/installer/paths.js +0 -4
- package/dist/installer/paths.js.map +1 -1
- package/dist/installer/wizard.d.ts +0 -7
- package/dist/installer/wizard.js +1 -8
- package/dist/installer/wizard.js.map +1 -1
- package/dist/nat/address-resolver.d.ts +18 -17
- package/dist/nat/address-resolver.js +79 -25
- package/dist/nat/address-resolver.js.map +1 -1
- package/dist/nat/address-watch.d.ts +37 -0
- package/dist/nat/address-watch.js +84 -0
- package/dist/nat/address-watch.js.map +1 -0
- package/dist/nat/ddns/duckdns.d.ts +2 -1
- package/dist/nat/ddns/duckdns.js +5 -1
- package/dist/nat/ddns/duckdns.js.map +1 -1
- package/dist/nat/ddns/index.d.ts +6 -0
- package/dist/nat/ddns/index.js.map +1 -1
- package/dist/nat/index.d.ts +12 -9
- package/dist/nat/index.js +6 -5
- package/dist/nat/index.js.map +1 -1
- package/dist/nat/nat-service.d.ts +203 -77
- package/dist/nat/nat-service.js +602 -237
- package/dist/nat/nat-service.js.map +1 -1
- package/dist/nat/nat-store.d.ts +12 -5
- package/dist/nat/nat-store.js +114 -30
- package/dist/nat/nat-store.js.map +1 -1
- package/dist/nat/port-mapper.d.ts +83 -79
- package/dist/nat/port-mapper.js +148 -189
- package/dist/nat/port-mapper.js.map +1 -1
- package/dist/nat/reachability.d.ts +31 -15
- package/dist/nat/reachability.js +97 -22
- package/dist/nat/reachability.js.map +1 -1
- package/dist/nat/types.d.ts +78 -27
- package/dist/nat/types.js +8 -5
- package/dist/nat/types.js.map +1 -1
- package/dist/orchestrator/host-process-orchestrator.d.ts +29 -71
- package/dist/orchestrator/host-process-orchestrator.js +78 -218
- package/dist/orchestrator/host-process-orchestrator.js.map +1 -1
- package/dist/{installer/identity.d.ts → orchestrator/identity-file.d.ts} +5 -5
- package/dist/{installer/identity.js → orchestrator/identity-file.js} +6 -6
- package/dist/orchestrator/identity-file.js.map +1 -0
- package/dist/orchestrator/index.d.ts +2 -3
- package/dist/orchestrator/index.js +1 -1
- package/dist/orchestrator/index.js.map +1 -1
- package/dist/orchestrator/node-identity.d.ts +6 -6
- package/dist/orchestrator/node-identity.js +7 -7
- package/dist/orchestrator/node-identity.js.map +1 -1
- package/dist/orchestrator/port-allocator.d.ts +5 -6
- package/dist/orchestrator/port-allocator.js +9 -11
- package/dist/orchestrator/port-allocator.js.map +1 -1
- package/dist/orchestrator/state-store.d.ts +3 -5
- package/dist/orchestrator/state-store.js +2 -10
- package/dist/orchestrator/state-store.js.map +1 -1
- package/dist/orchestrator/types.d.ts +33 -22
- package/dist/orchestrator/types.js.map +1 -1
- package/dist/server/error-handler.d.ts +5 -25
- package/dist/server/error-handler.js +11 -74
- package/dist/server/error-handler.js.map +1 -1
- package/dist/server/events/types.d.ts +7 -11
- package/dist/server/events/types.js +2 -1
- package/dist/server/events/types.js.map +1 -1
- package/dist/server/index.d.ts +11 -37
- package/dist/server/index.js +30 -48
- package/dist/server/index.js.map +1 -1
- package/dist/server/routes/hosted-nodes.d.ts +28 -0
- package/dist/server/routes/hosted-nodes.js +68 -0
- package/dist/server/routes/hosted-nodes.js.map +1 -0
- package/dist/server/routes/nat.d.ts +4 -1
- package/dist/server/routes/nat.js +13 -2
- package/dist/server/routes/nat.js.map +1 -1
- package/dist/server/routes/nodes.d.ts +6 -20
- package/dist/server/routes/nodes.js +7 -86
- package/dist/server/routes/nodes.js.map +1 -1
- package/dist/server/routes/settings.d.ts +2 -7
- package/dist/server/routes/settings.js +3 -7
- package/dist/server/routes/settings.js.map +1 -1
- package/dist/server/routes/status.d.ts +2 -25
- package/dist/server/routes/status.js +1 -11
- package/dist/server/routes/status.js.map +1 -1
- package/dist/server/server.js +8 -8
- package/dist/server/settings-store.d.ts +2 -2
- package/dist/server/settings-store.js +2 -2
- package/dist/server/static.js +1 -2
- package/dist/server/static.js.map +1 -1
- package/dist/ui/assets/index-4FWuU8ye.js +19 -0
- package/dist/ui/assets/index-4FWuU8ye.js.map +1 -0
- package/dist/ui/assets/index-Di9Q4d01.css +1 -0
- package/dist/ui/index.html +2 -2
- package/dist/update/index.d.ts +2 -2
- package/dist/update/index.js +2 -2
- package/dist/update/store.d.ts +1 -1
- package/dist/update/store.js +1 -1
- package/dist/update/types.d.ts +1 -1
- package/dist/update/types.js +1 -1
- package/package.json +5 -6
- package/src/__tests__/cli-nat.smoke.test.ts +219 -212
- package/src/__tests__/cli.smoke.test.ts +1 -1
- package/src/__tests__/global-setup.ts +2 -3
- package/src/__tests__/orchestrator-claim-secret.test.ts +112 -0
- package/src/__tests__/orchestrator-handle-contract.ts +134 -134
- package/src/__tests__/orchestrator-node-identity.test.ts +182 -203
- package/src/__tests__/orchestrator-ports.test.ts +124 -143
- package/src/__tests__/orchestrator-push.test.ts +138 -146
- package/src/__tests__/orchestrator.test.ts +1019 -1063
- package/src/bin/host.ts +368 -553
- package/src/bin/join-output.ts +120 -0
- package/src/bin/nat-output.ts +177 -0
- package/src/{donation → hosted}/__tests__/fake-orchestrator.test.ts +173 -173
- package/src/{donation → hosted}/__tests__/fake-orchestrator.ts +265 -243
- package/src/hosted/__tests__/hosted-node-service.test.ts +609 -0
- package/src/hosted/__tests__/hosted-node-supervisor.test.ts +474 -0
- package/src/hosted/__tests__/test-invitation.ts +33 -0
- package/src/hosted/claim-details.ts +96 -0
- package/src/hosted/hosted-node-service.ts +795 -0
- package/src/hosted/hosted-node-store.ts +144 -0
- package/src/hosted/hosted-node-supervisor.ts +439 -0
- package/src/hosted/hosted-node-watcher.ts +250 -0
- package/src/hosted/index.ts +53 -0
- package/src/hosted/node-status.ts +71 -0
- package/src/hosted/types.ts +153 -0
- package/src/index.ts +72 -91
- package/src/installer/__tests__/config.test.ts +15 -40
- package/src/installer/__tests__/installer.smoke.test.ts +5 -86
- package/src/installer/__tests__/wizard.test.ts +56 -64
- package/src/installer/config.ts +135 -175
- package/src/installer/index.ts +15 -83
- package/src/installer/paths.ts +104 -109
- package/src/installer/wizard.ts +87 -103
- package/src/nat/__tests__/address-resolver.test.ts +107 -82
- package/src/nat/__tests__/nat-service.test.ts +433 -408
- package/src/nat/__tests__/nat-store.test.ts +127 -93
- package/src/nat/__tests__/port-mapper.test.ts +30 -191
- package/src/nat/__tests__/reachability.test.ts +96 -13
- package/src/nat/address-resolver.ts +94 -52
- package/src/nat/address-watch.ts +92 -0
- package/src/nat/ddns/duckdns.ts +5 -1
- package/src/nat/ddns/index.ts +6 -0
- package/src/nat/index.ts +87 -71
- package/src/nat/nat-service.ts +889 -501
- package/src/nat/nat-store.ts +237 -154
- package/src/nat/port-mapper.ts +241 -329
- package/src/nat/reachability.ts +134 -25
- package/src/nat/types.ts +209 -159
- package/src/{installer/__tests__/identity.test.ts → orchestrator/__tests__/identity-file.test.ts} +34 -34
- package/src/orchestrator/__tests__/node-identity.test.ts +62 -62
- package/src/orchestrator/host-process-orchestrator.ts +81 -243
- package/src/{installer/identity.ts → orchestrator/identity-file.ts} +56 -56
- package/src/orchestrator/index.ts +9 -10
- package/src/orchestrator/node-identity.ts +41 -41
- package/src/orchestrator/port-allocator.ts +71 -73
- package/src/orchestrator/state-store.ts +76 -90
- package/src/orchestrator/types.ts +34 -23
- package/src/server/__tests__/bus.test.ts +38 -38
- package/src/server/__tests__/error-handler.test.ts +81 -118
- package/src/server/__tests__/fakes.ts +40 -44
- package/src/server/__tests__/hosted-nodes-route.test.ts +160 -0
- package/src/server/__tests__/nodes-route.test.ts +114 -283
- package/src/server/__tests__/publishers.test.ts +193 -180
- package/src/server/__tests__/server.smoke.test.ts +134 -223
- package/src/server/__tests__/settings-route.test.ts +179 -195
- package/src/server/__tests__/sse-route.test.ts +119 -119
- package/src/server/__tests__/status-route.test.ts +82 -121
- package/src/server/error-handler.ts +112 -178
- package/src/server/events/types.ts +20 -24
- package/src/server/index.ts +223 -267
- package/src/server/routes/hosted-nodes.ts +83 -0
- package/src/server/routes/nat.ts +75 -63
- package/src/server/routes/nodes.ts +96 -188
- package/src/server/routes/settings.ts +115 -123
- package/src/server/routes/status.ts +102 -134
- package/src/server/server.ts +8 -8
- package/src/server/settings-store.ts +2 -2
- package/src/server/static.ts +1 -2
- package/src/update/index.ts +2 -2
- package/src/update/store.ts +1 -1
- package/src/update/types.ts +1 -1
- package/dist/auth/duration.d.ts +0 -1
- package/dist/auth/duration.js +0 -32
- package/dist/auth/duration.js.map +0 -1
- package/dist/auth/index.d.ts +0 -11
- package/dist/auth/index.js +0 -9
- package/dist/auth/index.js.map +0 -1
- package/dist/auth/self-label.d.ts +0 -36
- package/dist/auth/self-label.js +0 -57
- package/dist/auth/self-label.js.map +0 -1
- package/dist/auth/trust-circle-store.d.ts +0 -26
- package/dist/auth/trust-circle-store.js +0 -129
- package/dist/auth/trust-circle-store.js.map +0 -1
- package/dist/auth/trust-circle.d.ts +0 -156
- package/dist/auth/trust-circle.js +0 -307
- package/dist/auth/trust-circle.js.map +0 -1
- package/dist/auth/types.d.ts +0 -84
- package/dist/auth/types.js +0 -24
- package/dist/auth/types.js.map +0 -1
- package/dist/donation/donation-service.d.ts +0 -389
- package/dist/donation/donation-service.js +0 -828
- package/dist/donation/donation-service.js.map +0 -1
- package/dist/donation/donation-store.d.ts +0 -51
- package/dist/donation/donation-store.js +0 -152
- package/dist/donation/donation-store.js.map +0 -1
- package/dist/donation/donation-supervisor.js +0 -364
- package/dist/donation/donation-supervisor.js.map +0 -1
- package/dist/donation/grant-service.d.ts +0 -80
- package/dist/donation/grant-service.js +0 -210
- package/dist/donation/grant-service.js.map +0 -1
- package/dist/donation/grant-store.d.ts +0 -38
- package/dist/donation/grant-store.js +0 -135
- package/dist/donation/grant-store.js.map +0 -1
- package/dist/donation/index.d.ts +0 -20
- package/dist/donation/index.js +0 -17
- package/dist/donation/index.js.map +0 -1
- package/dist/donation/types.d.ts +0 -224
- package/dist/donation/types.js +0 -40
- package/dist/donation/types.js.map +0 -1
- package/dist/installer/identity.js.map +0 -1
- package/dist/owner/index.d.ts +0 -11
- package/dist/owner/index.js +0 -11
- package/dist/owner/index.js.map +0 -1
- package/dist/owner/owner-node-client.d.ts +0 -110
- package/dist/owner/owner-node-client.js +0 -184
- package/dist/owner/owner-node-client.js.map +0 -1
- package/dist/server/routes/bootstrap-node-validation.d.ts +0 -115
- package/dist/server/routes/bootstrap-node-validation.js +0 -197
- package/dist/server/routes/bootstrap-node-validation.js.map +0 -1
- package/dist/server/routes/grants-admin.d.ts +0 -29
- package/dist/server/routes/grants-admin.js +0 -58
- package/dist/server/routes/grants-admin.js.map +0 -1
- package/dist/server/routes/grants.d.ts +0 -28
- package/dist/server/routes/grants.js +0 -165
- package/dist/server/routes/grants.js.map +0 -1
- package/dist/server/routes/provision-request-validation.d.ts +0 -45
- package/dist/server/routes/provision-request-validation.js +0 -97
- package/dist/server/routes/provision-request-validation.js.map +0 -1
- package/dist/server/routes/strands.d.ts +0 -20
- package/dist/server/routes/strands.js +0 -38
- package/dist/server/routes/strands.js.map +0 -1
- package/dist/server/routes/trust-circle.d.ts +0 -17
- package/dist/server/routes/trust-circle.js +0 -35
- package/dist/server/routes/trust-circle.js.map +0 -1
- package/dist/strands/index.d.ts +0 -7
- package/dist/strands/index.js +0 -6
- package/dist/strands/index.js.map +0 -1
- package/dist/strands/strand-service.d.ts +0 -50
- package/dist/strands/strand-service.js +0 -103
- package/dist/strands/strand-service.js.map +0 -1
- package/dist/strands/types.d.ts +0 -93
- package/dist/strands/types.js +0 -23
- package/dist/strands/types.js.map +0 -1
- package/dist/ui/assets/index-C6nELNfC.js +0 -23
- package/dist/ui/assets/index-C6nELNfC.js.map +0 -1
- package/dist/ui/assets/index-TtIYSeZO.css +0 -1
- package/src/__tests__/cli-invite.smoke.test.ts +0 -192
- package/src/__tests__/host-owner.smoke.test.ts +0 -123
- package/src/__tests__/orchestrator-owner.test.ts +0 -234
- package/src/__tests__/orchestrator-pin-keys.test.ts +0 -152
- package/src/auth/__tests__/duration.test.ts +0 -28
- package/src/auth/__tests__/self-label.test.ts +0 -85
- package/src/auth/__tests__/trust-circle-integration.test.ts +0 -152
- package/src/auth/__tests__/trust-circle-store.test.ts +0 -115
- package/src/auth/__tests__/trust-circle.test.ts +0 -445
- package/src/auth/duration.ts +0 -33
- package/src/auth/index.ts +0 -21
- package/src/auth/self-label.ts +0 -74
- package/src/auth/trust-circle-store.ts +0 -155
- package/src/auth/trust-circle.ts +0 -395
- package/src/auth/types.ts +0 -99
- package/src/donation/__tests__/donation-service.test.ts +0 -1193
- package/src/donation/__tests__/donation-supervisor.test.ts +0 -548
- package/src/donation/__tests__/grant-service.test.ts +0 -213
- package/src/donation/__tests__/grant-store.test.ts +0 -113
- package/src/donation/donation-service.ts +0 -1033
- package/src/donation/donation-store.ts +0 -176
- package/src/donation/donation-supervisor.ts +0 -411
- package/src/donation/grant-service.ts +0 -252
- package/src/donation/grant-store.ts +0 -152
- package/src/donation/index.ts +0 -65
- package/src/donation/types.ts +0 -260
- package/src/owner/__tests__/owner-node-client.test.ts +0 -194
- package/src/owner/index.ts +0 -12
- package/src/owner/owner-node-client.ts +0 -259
- package/src/server/__tests__/bootstrap-node-validation.test.ts +0 -138
- package/src/server/__tests__/grants-admin-route.test.ts +0 -171
- package/src/server/__tests__/grants-route.test.ts +0 -394
- package/src/server/__tests__/strands-route.test.ts +0 -234
- package/src/server/routes/bootstrap-node-validation.ts +0 -206
- package/src/server/routes/grants-admin.ts +0 -74
- package/src/server/routes/grants.ts +0 -193
- package/src/server/routes/provision-request-validation.ts +0 -105
- package/src/server/routes/strands.ts +0 -54
- package/src/server/routes/trust-circle.ts +0 -49
- package/src/strands/__tests__/strand-service.test.ts +0 -165
- package/src/strands/index.ts +0 -14
- package/src/strands/strand-service.ts +0 -135
- package/src/strands/types.ts +0 -102
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HostedNodeSupervisor unit tests — the component that notices a hosted node is
|
|
3
|
+
* no longer running and calls `HostedNodeService.respawn`, backs off between
|
|
4
|
+
* attempts, and eventually gives up.
|
|
5
|
+
*
|
|
6
|
+
* Exercised against the shared `FakeOrchestrator` (no real child processes), a
|
|
7
|
+
* real on-disk `HostedNodeStore`, and an injected clock — so backoff and the
|
|
8
|
+
* healthy-reset window are asserted without any wall-clock waiting.
|
|
9
|
+
*
|
|
10
|
+
* What is NOT covered here: that a really-respawned child rejoins its cadre.
|
|
11
|
+
* That is the cross-package `cadre-host-join-by-qr` integration scenario.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
15
|
+
import { mkdtempSync, rmSync } from 'node:fs';
|
|
16
|
+
import { tmpdir } from 'node:os';
|
|
17
|
+
import { join } from 'node:path';
|
|
18
|
+
|
|
19
|
+
import { HostedNodeService } from '../hosted-node-service.js';
|
|
20
|
+
import { HostedNodeStore } from '../hosted-node-store.js';
|
|
21
|
+
import {
|
|
22
|
+
HostedNodeSupervisor,
|
|
23
|
+
HOSTED_NODE_RESPAWN_HEALTHY_MS,
|
|
24
|
+
HOSTED_NODE_RESPAWN_MAX_ATTEMPTS,
|
|
25
|
+
} from '../hosted-node-supervisor.js';
|
|
26
|
+
import type { HostedNode, HostedNodeChange, HostedNodeView } from '../types.js';
|
|
27
|
+
import { HostedNodeError } from '../types.js';
|
|
28
|
+
import { FakeOrchestrator } from './fake-orchestrator.js';
|
|
29
|
+
import { encodedTestInvitation } from './test-invitation.js';
|
|
30
|
+
|
|
31
|
+
let tmpRoot: string;
|
|
32
|
+
|
|
33
|
+
beforeEach(() => {
|
|
34
|
+
tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-hosted-sup-'));
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
afterEach(() => {
|
|
38
|
+
try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
/** Everything one test needs, sharing a single mutable clock. */
|
|
42
|
+
interface Harness {
|
|
43
|
+
orch: FakeOrchestrator;
|
|
44
|
+
store: HostedNodeStore;
|
|
45
|
+
service: HostedNodeService;
|
|
46
|
+
supervisor: HostedNodeSupervisor;
|
|
47
|
+
changes: HostedNodeChange[];
|
|
48
|
+
/** Move the shared clock forward. */
|
|
49
|
+
advance: (ms: number) => void;
|
|
50
|
+
nowMs: () => number;
|
|
51
|
+
/** Start one node waiting to be claimed. */
|
|
52
|
+
join: () => Promise<HostedNodeView>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const START_MS = Date.parse('2026-01-01T00:00:00.000Z');
|
|
56
|
+
|
|
57
|
+
function makeHarness(store?: HostedNodeStore): Harness {
|
|
58
|
+
let nowMs = START_MS;
|
|
59
|
+
const now = (): Date => new Date(nowMs);
|
|
60
|
+
|
|
61
|
+
const orch = new FakeOrchestrator();
|
|
62
|
+
const theStore = store ?? new HostedNodeStore(join(tmpRoot, 'hosted'));
|
|
63
|
+
const service = new HostedNodeService({
|
|
64
|
+
orchestrator: orch,
|
|
65
|
+
store: theStore,
|
|
66
|
+
addresses: { publicAddressesFor: () => [], getStatus: () => ({ nodes: [] }) },
|
|
67
|
+
now,
|
|
68
|
+
});
|
|
69
|
+
const changes: HostedNodeChange[] = [];
|
|
70
|
+
service.onChange((c) => changes.push(c));
|
|
71
|
+
const supervisor = new HostedNodeSupervisor({ service, store: theStore, orchestrator: orch, now });
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
orch,
|
|
75
|
+
store: theStore,
|
|
76
|
+
service,
|
|
77
|
+
supervisor,
|
|
78
|
+
changes,
|
|
79
|
+
advance: (ms) => { nowMs += ms; },
|
|
80
|
+
nowMs: () => nowMs,
|
|
81
|
+
join: () => service.join(),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Mark a record claimed without going through the (fetch-driven) status watcher. */
|
|
86
|
+
function markJoined(store: HostedNodeStore, id: string): void {
|
|
87
|
+
store.put({ ...requireNode(store, id), status: 'joined', partyId: 'party-P', ownerKey: 'owner' });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function requireNode(store: HostedNodeStore, id: string): HostedNode {
|
|
91
|
+
const node = store.get(id);
|
|
92
|
+
if (!node) throw new Error(`no hosted node ${id}`);
|
|
93
|
+
return node;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function dockerIdOf(node: { dockerId?: string }): string {
|
|
97
|
+
if (!node.dockerId) throw new Error('hosted node has no dockerId');
|
|
98
|
+
return node.dockerId;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Let queued fire-and-forget passes run. */
|
|
102
|
+
function flush(): Promise<void> {
|
|
103
|
+
return new Promise((res) => setTimeout(res, 5));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** A store whose every read fails — a malformed `hosted-nodes.json` on disk. */
|
|
107
|
+
class UnreadableStore extends HostedNodeStore {
|
|
108
|
+
override list(): HostedNode[] {
|
|
109
|
+
throw new HostedNodeError('storage_error', 'hosted-nodes file is not valid JSON');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
describe('HostedNodeSupervisor.reconcile', () => {
|
|
114
|
+
it('respawns a joined-but-stopped node, keeping its status and swapping its handle', async () => {
|
|
115
|
+
const h = makeHarness();
|
|
116
|
+
const view = await h.join();
|
|
117
|
+
markJoined(h.store, view.id);
|
|
118
|
+
h.orch.crash(dockerIdOf(view));
|
|
119
|
+
|
|
120
|
+
const respawned = await h.supervisor.reconcile();
|
|
121
|
+
|
|
122
|
+
expect(respawned).toEqual([view.id]);
|
|
123
|
+
const stored = requireNode(h.store, view.id);
|
|
124
|
+
expect(stored.dockerId).toBe('dock_2');
|
|
125
|
+
expect(stored.status).toBe('joined');
|
|
126
|
+
expect(h.orch.createCalls).toHaveLength(2);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('respawns an unclaimed node whose handle is dead with the record\'s secret, on the previous handle\'s ports', async () => {
|
|
130
|
+
const h = makeHarness();
|
|
131
|
+
const view = await h.join();
|
|
132
|
+
const { join: joinedBy } = requireNode(h.store, view.id);
|
|
133
|
+
const secret = joinedBy.kind === 'claim' ? joinedBy.secret : undefined;
|
|
134
|
+
const portsBefore = h.orch.getNode(view.id)!.ports;
|
|
135
|
+
// A host restart that found the child dead: the handle is there, the process is not.
|
|
136
|
+
h.orch.crash(dockerIdOf(view));
|
|
137
|
+
|
|
138
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([view.id]);
|
|
139
|
+
|
|
140
|
+
// The QR code already shown names this secret and these ports, so both must hold.
|
|
141
|
+
expect(h.orch.createCalls[1]).toMatchObject({ containerId: view.id, partyId: 'unclaimed', claimSecret: secret });
|
|
142
|
+
expect(h.orch.getNode(view.id)!.ports).toEqual(portsBefore);
|
|
143
|
+
expect(requireNode(h.store, view.id)).toMatchObject({ status: 'unclaimed', dockerId: 'dock_2' });
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('respawns a joining invitation node with its invitation, and a joined one without it', async () => {
|
|
147
|
+
const h = makeHarness();
|
|
148
|
+
const invitation = encodedTestInvitation('party-P');
|
|
149
|
+
const view = await h.service.join({ invitation });
|
|
150
|
+
h.orch.crash(dockerIdOf(view));
|
|
151
|
+
|
|
152
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([view.id]);
|
|
153
|
+
// Still joining: the child redeems again, and a member answers a node that already got in as accepted.
|
|
154
|
+
expect(h.orch.createCalls[1]).toMatchObject({ containerId: view.id, partyId: 'party-P', invitation });
|
|
155
|
+
|
|
156
|
+
// Joined: a member whose rows are in its own database, and whose invitation may since have expired.
|
|
157
|
+
h.store.put({ ...requireNode(h.store, view.id), status: 'joined' });
|
|
158
|
+
h.orch.crash(dockerIdOf(requireNode(h.store, view.id)));
|
|
159
|
+
h.advance(HOSTED_NODE_RESPAWN_HEALTHY_MS);
|
|
160
|
+
|
|
161
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([view.id]);
|
|
162
|
+
expect(h.orch.createCalls[2]).toEqual({ containerId: view.id, partyId: 'party-P', bootstrapNodes: [], profile: 'storage' });
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('leaves a running node alone', async () => {
|
|
166
|
+
const h = makeHarness();
|
|
167
|
+
const view = await h.join();
|
|
168
|
+
markJoined(h.store, view.id);
|
|
169
|
+
|
|
170
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([]);
|
|
171
|
+
expect(h.orch.createCalls).toHaveLength(1);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('never respawns a node the host gave up on', async () => {
|
|
175
|
+
const h = makeHarness();
|
|
176
|
+
const view = await h.join();
|
|
177
|
+
h.store.put({ ...requireNode(h.store, view.id), status: 'error', error: 'gave up' });
|
|
178
|
+
h.orch.crash(dockerIdOf(view));
|
|
179
|
+
|
|
180
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([]);
|
|
181
|
+
expect(h.orch.createCalls).toHaveLength(1);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('honours the backoff window, then attempts again once the clock passes it', async () => {
|
|
185
|
+
const h = makeHarness();
|
|
186
|
+
const view = await h.join();
|
|
187
|
+
markJoined(h.store, view.id);
|
|
188
|
+
h.orch.crash(dockerIdOf(view));
|
|
189
|
+
h.orch.failCreate = true;
|
|
190
|
+
|
|
191
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([]);
|
|
192
|
+
expect(h.orch.createCalls).toHaveLength(2);
|
|
193
|
+
expect(requireNode(h.store, view.id).respawn?.attempts).toBe(1);
|
|
194
|
+
|
|
195
|
+
// attempts = 1 → base * 2^1 = 10s. One second later is still too soon.
|
|
196
|
+
h.advance(1_000);
|
|
197
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([]);
|
|
198
|
+
expect(h.orch.createCalls).toHaveLength(2);
|
|
199
|
+
expect(requireNode(h.store, view.id).respawn?.attempts).toBe(1);
|
|
200
|
+
|
|
201
|
+
h.advance(10_000);
|
|
202
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([]);
|
|
203
|
+
expect(h.orch.createCalls).toHaveLength(3);
|
|
204
|
+
expect(requireNode(h.store, view.id).respawn?.attempts).toBe(2);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('gives up after the attempt cap: record → error, child stopped but not reclaimed, change published', async () => {
|
|
208
|
+
const h = makeHarness();
|
|
209
|
+
const view = await h.join();
|
|
210
|
+
markJoined(h.store, view.id);
|
|
211
|
+
h.orch.crash(dockerIdOf(view));
|
|
212
|
+
h.orch.failCreate = true;
|
|
213
|
+
h.changes.length = 0;
|
|
214
|
+
|
|
215
|
+
for (let i = 0; i < HOSTED_NODE_RESPAWN_MAX_ATTEMPTS; i++) {
|
|
216
|
+
await h.supervisor.reconcile();
|
|
217
|
+
// Well past the longest backoff, so every pass gets to attempt.
|
|
218
|
+
h.advance(10 * 60_000);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const stored = requireNode(h.store, view.id);
|
|
222
|
+
expect(stored.status).toBe('error');
|
|
223
|
+
expect(stored.respawn?.attempts).toBe(HOSTED_NODE_RESPAWN_MAX_ATTEMPTS);
|
|
224
|
+
expect(stored.error).toContain(`${HOSTED_NODE_RESPAWN_MAX_ATTEMPTS} attempts`);
|
|
225
|
+
expect(stored.error).toContain('spawn boom');
|
|
226
|
+
// The workdir holds the identity key the node's cadre approved — the
|
|
227
|
+
// give-up path stops the child but must never reclaim it.
|
|
228
|
+
//
|
|
229
|
+
// Doubles as the standing guard on `FakeOrchestrator`'s drop being
|
|
230
|
+
// success-only (mirroring `restoreDroppedHandles`): every create here fails,
|
|
231
|
+
// so a fake that dropped the prior handle regardless would leave `giveUp`
|
|
232
|
+
// with nothing to stop, and this line fails with
|
|
233
|
+
// `expected [] to include 'dock_1'`.
|
|
234
|
+
expect(h.orch.stopped).toContain(view.dockerId);
|
|
235
|
+
expect(h.orch.removed).toEqual([]);
|
|
236
|
+
expect(h.changes).toEqual([{ kind: 'changed', id: view.id }]);
|
|
237
|
+
|
|
238
|
+
// `error` is terminal: no further attempts.
|
|
239
|
+
const createsAtGiveUp = h.orch.createCalls.length;
|
|
240
|
+
h.advance(10 * 60_000);
|
|
241
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([]);
|
|
242
|
+
expect(h.orch.createCalls).toHaveLength(createsAtGiveUp);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// A spawn that succeeds and then dies at once (a crash on boot, a port clash)
|
|
246
|
+
// never throws out of `respawn`, so a give-up checked only on a throw would
|
|
247
|
+
// respawn such a node forever.
|
|
248
|
+
it('gives up on a node whose every respawn spawns but does not stay up', async () => {
|
|
249
|
+
const h = makeHarness();
|
|
250
|
+
const view = await h.join();
|
|
251
|
+
markJoined(h.store, view.id);
|
|
252
|
+
h.orch.crash(dockerIdOf(view));
|
|
253
|
+
h.orch.onSpawned = (dockerId) => { h.orch.crash(dockerId); };
|
|
254
|
+
|
|
255
|
+
for (let i = 0; i < HOSTED_NODE_RESPAWN_MAX_ATTEMPTS; i++) {
|
|
256
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([view.id]);
|
|
257
|
+
// Well past the longest backoff, so every pass gets to act.
|
|
258
|
+
h.advance(10 * 60_000);
|
|
259
|
+
}
|
|
260
|
+
expect(h.orch.createCalls).toHaveLength(1 + HOSTED_NODE_RESPAWN_MAX_ATTEMPTS);
|
|
261
|
+
expect(requireNode(h.store, view.id).status).toBe('joined');
|
|
262
|
+
|
|
263
|
+
// The budget is spent and the last respawn died too: this pass gives up
|
|
264
|
+
// instead of spawning again.
|
|
265
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([]);
|
|
266
|
+
const stored = requireNode(h.store, view.id);
|
|
267
|
+
expect(h.orch.createCalls).toHaveLength(1 + HOSTED_NODE_RESPAWN_MAX_ATTEMPTS);
|
|
268
|
+
expect(stored.status).toBe('error');
|
|
269
|
+
expect(stored.respawn?.attempts).toBe(HOSTED_NODE_RESPAWN_MAX_ATTEMPTS);
|
|
270
|
+
expect(stored.error).toContain('did not stay running');
|
|
271
|
+
// The child the record names is stopped, never reclaimed.
|
|
272
|
+
expect(h.orch.stopped).toEqual([stored.dockerId]);
|
|
273
|
+
expect(h.orch.removed).toEqual([]);
|
|
274
|
+
|
|
275
|
+
h.advance(10 * 60_000);
|
|
276
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([]);
|
|
277
|
+
expect(h.orch.createCalls).toHaveLength(1 + HOSTED_NODE_RESPAWN_MAX_ATTEMPTS);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it('refills the attempt budget once a respawned node has stayed up', async () => {
|
|
281
|
+
const h = makeHarness();
|
|
282
|
+
const view = await h.join();
|
|
283
|
+
h.store.put({
|
|
284
|
+
...requireNode(h.store, view.id),
|
|
285
|
+
status: 'joined',
|
|
286
|
+
respawn: { attempts: 3, lastAttemptAt: new Date(h.nowMs()).toISOString() },
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
// Still inside the healthy window — the crash loop's count stands.
|
|
290
|
+
h.advance(HOSTED_NODE_RESPAWN_HEALTHY_MS - 1_000);
|
|
291
|
+
await h.supervisor.reconcile();
|
|
292
|
+
expect(requireNode(h.store, view.id).respawn?.attempts).toBe(3);
|
|
293
|
+
|
|
294
|
+
h.advance(2_000);
|
|
295
|
+
await h.supervisor.reconcile();
|
|
296
|
+
expect(requireNode(h.store, view.id).respawn?.attempts).toBe(0);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it('leaves a still-spawning record alone — join owns that child', async () => {
|
|
300
|
+
const h = makeHarness();
|
|
301
|
+
// A handle written by a join that has not yet flipped to unclaimed.
|
|
302
|
+
// Respawning it here would race that join and strand the record.
|
|
303
|
+
h.store.put({
|
|
304
|
+
id: 'hn_inflight', join: { kind: 'claim', secret: 's' }, partyId: 'unclaimed', profile: 'storage',
|
|
305
|
+
status: 'spawning', dockerId: 'dock_inflight',
|
|
306
|
+
createdAt: new Date(START_MS).toISOString(), updatedAt: new Date(START_MS).toISOString(),
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([]);
|
|
310
|
+
expect(h.orch.createCalls).toEqual([]);
|
|
311
|
+
expect(requireNode(h.store, 'hn_inflight').status).toBe('spawning');
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it('skips a record that has no orchestrator handle yet', async () => {
|
|
315
|
+
const h = makeHarness();
|
|
316
|
+
h.store.put({
|
|
317
|
+
id: 'hn_nohandle', join: { kind: 'claim', secret: 's' }, partyId: 'unclaimed', profile: 'storage',
|
|
318
|
+
status: 'unclaimed',
|
|
319
|
+
createdAt: new Date(START_MS).toISOString(), updatedAt: new Date(START_MS).toISOString(),
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([]);
|
|
323
|
+
expect(h.orch.createCalls).toEqual([]);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it('does not undo a claim that lands while the pass is mid-flight', async () => {
|
|
327
|
+
const h = makeHarness();
|
|
328
|
+
const view = await h.join();
|
|
329
|
+
h.store.put({
|
|
330
|
+
...requireNode(h.store, view.id),
|
|
331
|
+
respawn: { attempts: 3, lastAttemptAt: new Date(h.nowMs()).toISOString() },
|
|
332
|
+
});
|
|
333
|
+
h.advance(HOSTED_NODE_RESPAWN_HEALTHY_MS + 1_000);
|
|
334
|
+
|
|
335
|
+
// The watcher's claim write lands between the pass's store snapshot and the
|
|
336
|
+
// budget-refill write — the refill must merge, not replay a stale row.
|
|
337
|
+
h.orch.onIsRunning = () => {
|
|
338
|
+
h.orch.onIsRunning = undefined;
|
|
339
|
+
markJoined(h.store, view.id);
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([]);
|
|
343
|
+
const stored = requireNode(h.store, view.id);
|
|
344
|
+
expect(stored.status).toBe('joined');
|
|
345
|
+
expect(stored.partyId).toBe('party-P');
|
|
346
|
+
expect(stored.respawn?.attempts).toBe(0);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it('leaves a record removed mid-attempt alone instead of recreating it as error', async () => {
|
|
350
|
+
const h = makeHarness();
|
|
351
|
+
const view = await h.join();
|
|
352
|
+
h.store.put({
|
|
353
|
+
...requireNode(h.store, view.id),
|
|
354
|
+
status: 'joined',
|
|
355
|
+
// One attempt short of the cap (at the cap, the pass would give up before
|
|
356
|
+
// attempting), and long past the longest backoff — so the next failure
|
|
357
|
+
// lands straight in the give-up path.
|
|
358
|
+
respawn: {
|
|
359
|
+
attempts: HOSTED_NODE_RESPAWN_MAX_ATTEMPTS - 1,
|
|
360
|
+
lastAttemptAt: new Date(h.nowMs() - 10 * 60_000).toISOString(),
|
|
361
|
+
},
|
|
362
|
+
});
|
|
363
|
+
h.orch.crash(dockerIdOf(view));
|
|
364
|
+
h.orch.failCreate = true;
|
|
365
|
+
// The admin removes the node while the respawn's spawn is in flight.
|
|
366
|
+
h.orch.onCreate = () => { h.store.remove(view.id); };
|
|
367
|
+
|
|
368
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([]);
|
|
369
|
+
|
|
370
|
+
expect(h.store.get(view.id)).toBeUndefined();
|
|
371
|
+
expect(h.orch.stopped).toEqual([]);
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it('does not count a respawn abandoned mid-spawn as a failed attempt', async () => {
|
|
375
|
+
const h = makeHarness();
|
|
376
|
+
const view = await h.join();
|
|
377
|
+
markJoined(h.store, view.id);
|
|
378
|
+
h.orch.crash(dockerIdOf(view));
|
|
379
|
+
// The admin removes the node while the respawn's spawn is in flight.
|
|
380
|
+
h.orch.createDelayMs = 20;
|
|
381
|
+
let removed: Promise<void> | undefined;
|
|
382
|
+
h.orch.onCreate = () => { removed ??= h.service.remove(view.id); };
|
|
383
|
+
|
|
384
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([]);
|
|
385
|
+
await removed;
|
|
386
|
+
|
|
387
|
+
// Nothing threw, so no attempt was persisted and give-up never ran; the
|
|
388
|
+
// abandoned child is stopped and reclaimed, not left holding ports.
|
|
389
|
+
expect(h.store.get(view.id)).toBeUndefined();
|
|
390
|
+
expect(h.orch.stopped).toContain('dock_2');
|
|
391
|
+
expect(h.orch.removed).toContain('dock_2');
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it('survives a pass over a store it cannot read', async () => {
|
|
395
|
+
const h = makeHarness(new UnreadableStore(join(tmpRoot, 'hosted')));
|
|
396
|
+
|
|
397
|
+
await expect(h.supervisor.reconcile()).resolves.toEqual([]);
|
|
398
|
+
expect(h.orch.createCalls).toEqual([]);
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
it('serializes overlapping passes so one id is never double-spawned', async () => {
|
|
402
|
+
const h = makeHarness();
|
|
403
|
+
const view = await h.join();
|
|
404
|
+
markJoined(h.store, view.id);
|
|
405
|
+
h.orch.crash(dockerIdOf(view));
|
|
406
|
+
h.orch.createDelayMs = 20;
|
|
407
|
+
|
|
408
|
+
const [first, second] = await Promise.all([
|
|
409
|
+
h.supervisor.reconcile(),
|
|
410
|
+
h.supervisor.reconcile(),
|
|
411
|
+
]);
|
|
412
|
+
|
|
413
|
+
// Whichever pass wins the tail respawns; the other then finds it running.
|
|
414
|
+
expect([...first, ...second]).toEqual([view.id]);
|
|
415
|
+
// The original join plus exactly one respawn.
|
|
416
|
+
expect(h.orch.createCalls).toHaveLength(2);
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
describe('HostedNodeSupervisor.restart', () => {
|
|
421
|
+
it('stops and respawns a running node without spending its respawn budget', async () => {
|
|
422
|
+
const h = makeHarness();
|
|
423
|
+
const view = await h.join();
|
|
424
|
+
const respawn = { attempts: 2, lastAttemptAt: new Date(h.nowMs()).toISOString() };
|
|
425
|
+
const before: HostedNode = { ...requireNode(h.store, view.id), status: 'joined', partyId: 'party-P', respawn };
|
|
426
|
+
h.store.put(before);
|
|
427
|
+
h.advance(60_000);
|
|
428
|
+
|
|
429
|
+
await h.supervisor.restart(view.id);
|
|
430
|
+
|
|
431
|
+
expect(h.orch.stopped).toEqual([view.dockerId]);
|
|
432
|
+
const after = requireNode(h.store, view.id);
|
|
433
|
+
expect(after).toMatchObject({ status: 'joined', dockerId: 'dock_2', respawn, updatedAt: before.updatedAt });
|
|
434
|
+
});
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
describe('HostedNodeSupervisor start/stop', () => {
|
|
438
|
+
it('sweeps at startup and again on a child exit', async () => {
|
|
439
|
+
const h = makeHarness();
|
|
440
|
+
const view = await h.join();
|
|
441
|
+
markJoined(h.store, view.id);
|
|
442
|
+
h.orch.crash(dockerIdOf(view));
|
|
443
|
+
|
|
444
|
+
h.supervisor.start();
|
|
445
|
+
try {
|
|
446
|
+
// Queued behind the startup pass, so awaiting it proves that pass ran.
|
|
447
|
+
await h.supervisor.reconcile();
|
|
448
|
+
expect(requireNode(h.store, view.id).dockerId).toBe('dock_2');
|
|
449
|
+
|
|
450
|
+
// Now the exit-event trigger, with no manual reconcile in between.
|
|
451
|
+
h.advance(60_000);
|
|
452
|
+
h.orch.crash('dock_2');
|
|
453
|
+
await flush();
|
|
454
|
+
expect(requireNode(h.store, view.id).dockerId).toBe('dock_3');
|
|
455
|
+
} finally {
|
|
456
|
+
h.supervisor.stop();
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
it('stops reacting to exits after stop()', async () => {
|
|
461
|
+
const h = makeHarness();
|
|
462
|
+
const view = await h.join();
|
|
463
|
+
markJoined(h.store, view.id);
|
|
464
|
+
|
|
465
|
+
h.supervisor.start();
|
|
466
|
+
await h.supervisor.reconcile();
|
|
467
|
+
h.supervisor.stop();
|
|
468
|
+
|
|
469
|
+
h.orch.crash(dockerIdOf(view));
|
|
470
|
+
await flush();
|
|
471
|
+
// The exit listener is gone, so the crash goes unnoticed until the next start.
|
|
472
|
+
expect(requireNode(h.store, view.id).dockerId).toBe(view.dockerId);
|
|
473
|
+
});
|
|
474
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A cadre invitation for the hosted-node unit tests: well-formed enough for
|
|
3
|
+
* `decodeCadreInvitation`, which checks shape only. Nothing on the host verifies its
|
|
4
|
+
* signatures; a member does, and the integration scenario covers that.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { encodeCadreInvitation } from '@serfab/cadre-core';
|
|
8
|
+
|
|
9
|
+
/** The invitation's issuer: the owner key a joined invitation node is shown under. */
|
|
10
|
+
export const ISSUER_KEY = Buffer.alloc(32, 9).toString('base64url');
|
|
11
|
+
|
|
12
|
+
/** The member a test reports as having admitted the node. */
|
|
13
|
+
export const MEMBER_PEER_ID = '12D3KooWQYhTNQdmr3ArTeUHRYzFg94BKyTkoWBDWez9kSCVe2Xo';
|
|
14
|
+
|
|
15
|
+
export function encodedTestInvitation(partyId: string): string {
|
|
16
|
+
return encodeCadreInvitation({
|
|
17
|
+
v: 1,
|
|
18
|
+
partyId,
|
|
19
|
+
invitePrivateKey: Buffer.alloc(32, 3).toString('base64url'),
|
|
20
|
+
invite: {
|
|
21
|
+
key: Buffer.alloc(32, 4).toString('base64url'),
|
|
22
|
+
peerId: null,
|
|
23
|
+
grantsOwner: false,
|
|
24
|
+
expiresAt: null,
|
|
25
|
+
totalUses: 1,
|
|
26
|
+
stampId: 'stamp-1',
|
|
27
|
+
issuerKey: ISSUER_KEY,
|
|
28
|
+
issuerSig: 'signature',
|
|
29
|
+
},
|
|
30
|
+
ownerKeys: [ISSUER_KEY],
|
|
31
|
+
members: [`/ip4/127.0.0.1/tcp/4001/ws/p2p/${MEMBER_PEER_ID}`],
|
|
32
|
+
});
|
|
33
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the QR code of a node waiting to be claimed carries, and the address list behind it.
|
|
3
|
+
* `HostedNodeService.claimDetails` reads the child's `/status` and hands it here.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { multiaddr, CODE_IP4, CODE_IP6, CODE_P2P_CIRCUIT } from '@multiformats/multiaddr';
|
|
7
|
+
|
|
8
|
+
import { encodeNodeClaimPayload } from '@serfab/cadre-core';
|
|
9
|
+
|
|
10
|
+
import type { NodeReachability } from '../nat/types.js';
|
|
11
|
+
import type { NodeStatus } from './node-status.js';
|
|
12
|
+
import { HostedNodeError } from './types.js';
|
|
13
|
+
|
|
14
|
+
/** What `claimDetails` answers: the QR payload and its parts. */
|
|
15
|
+
export interface ClaimDetails {
|
|
16
|
+
/** `encodeNodeClaimPayload` of the three fields below — the text the QR code carries. */
|
|
17
|
+
payload: string;
|
|
18
|
+
peerId: string;
|
|
19
|
+
/** Public addresses first (TCP, then WebSocket), then the node's own non-loopback LAN addresses. */
|
|
20
|
+
multiaddrs: string[];
|
|
21
|
+
/** The NAT layer's verdict for the node, or null when it has no entry yet. A phone at home reaches the LAN address either way. */
|
|
22
|
+
reachability: NodeReachability | null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The NAT layer's two answers the claim details need. `NatService` satisfies this. */
|
|
26
|
+
export interface HostedNodeAddressSource {
|
|
27
|
+
/** The node's public multiaddrs (no `/p2p/` suffix), TCP then WebSocket. */
|
|
28
|
+
publicAddressesFor(nodeId: string, ports: { p2p: number; ws?: number }): string[];
|
|
29
|
+
getStatus(): { nodes: NodeReachability[] };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** One node's claim details, from its record's secret, its `/status`, and its ports. */
|
|
33
|
+
export interface ClaimDetailsInput {
|
|
34
|
+
id: string;
|
|
35
|
+
secret: string;
|
|
36
|
+
status: NodeStatus;
|
|
37
|
+
/** The orchestrator handle's ports; absent, the code carries LAN addresses only. */
|
|
38
|
+
ports: { p2p: number; ws?: number } | undefined;
|
|
39
|
+
addresses: HostedNodeAddressSource;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The claim details for a node whose `/status` answered. `503 node_unavailable` while the
|
|
44
|
+
* node reports no address a phone could dial (callers poll).
|
|
45
|
+
*/
|
|
46
|
+
export function buildClaimDetails(input: ClaimDetailsInput): ClaimDetails {
|
|
47
|
+
const { id, status, addresses } = input;
|
|
48
|
+
const multiaddrs = dialAddressesFor(input);
|
|
49
|
+
if (multiaddrs.length === 0) {
|
|
50
|
+
throw new HostedNodeError('node_unavailable', `Hosted node ${id} reports no address a phone could dial yet`);
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
payload: encodeNodeClaimPayload({ peerId: status.peerId, multiaddrs, secret: input.secret }),
|
|
54
|
+
peerId: status.peerId,
|
|
55
|
+
multiaddrs,
|
|
56
|
+
reachability: addresses.getStatus().nodes.find((n) => n.nodeId === id) ?? null,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The addresses the QR code names, in dial order: the node's public addresses
|
|
62
|
+
* from the NAT layer with the peer id appended (a phone away from home needs
|
|
63
|
+
* these), then the node's own non-loopback addresses as `/status` reports them
|
|
64
|
+
* (they already carry `/p2p/`; a phone at home dials these). Loopback and relay
|
|
65
|
+
* entries are dropped — nothing off this machine can use the former, and no
|
|
66
|
+
* hosted node holds a relay reservation today.
|
|
67
|
+
*/
|
|
68
|
+
function dialAddressesFor({ id, status, ports, addresses }: ClaimDetailsInput): string[] {
|
|
69
|
+
const publicAddrs = ports
|
|
70
|
+
? addresses.publicAddressesFor(id, ports).map((a) => `${a}/p2p/${status.peerId}`)
|
|
71
|
+
: [];
|
|
72
|
+
const lanAddrs = status.multiaddrs.filter((a) => isDialableFromLan(a));
|
|
73
|
+
return [...new Set([...publicAddrs, ...lanAddrs])];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Whether an address the node reports is one a phone on the host's LAN can dial:
|
|
78
|
+
* not loopback, not the unspecified address (libp2p expands a `0.0.0.0` listen into
|
|
79
|
+
* interface addresses, but a raw one names nothing to dial), not a relay circuit,
|
|
80
|
+
* and parsable. A DNS address passes (it names something off this machine); an
|
|
81
|
+
* address with no host component at all is dropped.
|
|
82
|
+
*/
|
|
83
|
+
function isDialableFromLan(addr: string): boolean {
|
|
84
|
+
let components: ReturnType<ReturnType<typeof multiaddr>['getComponents']>;
|
|
85
|
+
try {
|
|
86
|
+
components = multiaddr(addr).getComponents();
|
|
87
|
+
} catch {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
if (components.some((c) => c.code === CODE_P2P_CIRCUIT)) return false;
|
|
91
|
+
const host = components[0];
|
|
92
|
+
if (!host?.value) return false;
|
|
93
|
+
if (host.code === CODE_IP4) return !host.value.startsWith('127.') && host.value !== '0.0.0.0';
|
|
94
|
+
if (host.code === CODE_IP6) return host.value !== '::1' && host.value !== '::';
|
|
95
|
+
return true;
|
|
96
|
+
}
|