@ipab/cli 0.1.0-rc.7 → 0.1.0-rc.8
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 +24 -1
- package/dist/app-authoring.d.ts.map +1 -1
- package/dist/app-authoring.js +2 -44
- package/dist/app-authoring.js.map +1 -1
- package/dist/app-prebuild.d.ts +18 -0
- package/dist/app-prebuild.d.ts.map +1 -1
- package/dist/app-prebuild.js +46 -0
- package/dist/app-prebuild.js.map +1 -1
- package/dist/builtins/skill.ipab.car +0 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +290 -152
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +9 -4
- package/dist/config.js.map +1 -1
- package/dist/context.d.ts +4 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +11 -2
- package/dist/context.js.map +1 -1
- package/dist/errors.d.ts +1 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +1 -20
- package/dist/errors.js.map +1 -1
- package/dist/host-error.d.ts +9 -0
- package/dist/host-error.d.ts.map +1 -0
- package/dist/host-error.js +21 -0
- package/dist/host-error.js.map +1 -0
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +17 -0
- package/dist/ipc.js.map +1 -1
- package/dist/kubo-install.js +1 -1
- package/dist/kubo-install.js.map +1 -1
- package/dist/kubo-peers.d.ts.map +1 -1
- package/dist/kubo-peers.js +2 -1
- package/dist/kubo-peers.js.map +1 -1
- package/dist/kubo-topology.d.ts +19 -0
- package/dist/kubo-topology.d.ts.map +1 -0
- package/dist/kubo-topology.js +105 -0
- package/dist/kubo-topology.js.map +1 -0
- package/dist/kubo.d.ts +10 -0
- package/dist/kubo.d.ts.map +1 -1
- package/dist/kubo.js +96 -15
- package/dist/kubo.js.map +1 -1
- package/dist/space-path.d.ts +3 -0
- package/dist/space-path.d.ts.map +1 -0
- package/dist/space-path.js +17 -0
- package/dist/space-path.js.map +1 -0
- package/dist/space.d.ts +1 -2
- package/dist/space.d.ts.map +1 -1
- package/dist/space.js +2 -15
- package/dist/space.js.map +1 -1
- package/dist/state-storage.js +1 -1
- package/dist/state-storage.js.map +1 -1
- package/dist/state.d.ts +2 -0
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +31 -12
- package/dist/state.js.map +1 -1
- package/dist/swarm-files.d.ts +1 -1
- package/dist/swarm-files.d.ts.map +1 -1
- package/dist/swarm-files.js +2 -2
- package/dist/swarm-files.js.map +1 -1
- package/dist/swarm-meta.d.ts +1 -4
- package/dist/swarm-meta.d.ts.map +1 -1
- package/dist/swarm-meta.js +3 -34
- package/dist/swarm-meta.js.map +1 -1
- package/dist/swarm-observations.d.ts +40 -0
- package/dist/swarm-observations.d.ts.map +1 -0
- package/dist/swarm-observations.js +262 -0
- package/dist/swarm-observations.js.map +1 -0
- package/dist/swarm.d.ts +119 -6
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +662 -80
- package/dist/swarm.js.map +1 -1
- package/package.json +4 -4
- package/src/app-authoring.ts +12 -53
- package/src/app-prebuild.ts +61 -0
- package/src/cli.ts +370 -190
- package/src/config.ts +12 -4
- package/src/context.ts +15 -2
- package/src/errors.ts +1 -25
- package/src/host-error.ts +26 -0
- package/src/ipc.ts +19 -0
- package/src/kubo-install.ts +1 -1
- package/src/kubo-peers.ts +2 -3
- package/src/kubo-topology.ts +135 -0
- package/src/kubo.ts +134 -25
- package/src/space-path.ts +30 -0
- package/src/space.ts +2 -28
- package/src/state-storage.ts +1 -1
- package/src/state.ts +37 -12
- package/src/swarm-files.ts +2 -1
- package/src/swarm-meta.ts +15 -34
- package/src/swarm-observations.ts +374 -0
- package/src/swarm.ts +942 -91
package/src/state.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
type ConfigKey,
|
|
11
11
|
type ConfigValue,
|
|
12
12
|
} from "./config.js";
|
|
13
|
-
import { HostError, object, syntax } from "./
|
|
13
|
+
import { HostError, object, syntax } from "./host-error.js";
|
|
14
14
|
import { openStateStorage } from "./state-storage.js";
|
|
15
15
|
|
|
16
16
|
export interface Association {
|
|
@@ -375,7 +375,7 @@ export class State {
|
|
|
375
375
|
const entry = this.swarms().find((entry) => entry.cid === cid);
|
|
376
376
|
if (!entry)
|
|
377
377
|
throw new HostError(
|
|
378
|
-
`Swarm "${reference}" is not registered. Run ipab swarm
|
|
378
|
+
`Swarm "${reference}" is not registered. Run ipab swarm set ALIAS ${cid}.`,
|
|
379
379
|
);
|
|
380
380
|
return entry;
|
|
381
381
|
}
|
|
@@ -384,23 +384,46 @@ export class State {
|
|
|
384
384
|
const existing = this.swarms().find((entry) => entry.alias === alias);
|
|
385
385
|
if (existing && existing.cid !== cid)
|
|
386
386
|
throw new HostError(
|
|
387
|
-
`Swarm alias "${alias}" is already assigned to "${existing.cid}".
|
|
387
|
+
`Swarm alias "${alias}" is already assigned to "${existing.cid}". Choose a different alias, or explicitly retarget it with ipab swarm set ${alias} CONFIG_URL.`,
|
|
388
388
|
);
|
|
389
389
|
}
|
|
390
390
|
registerSwarm(cid: string, alias?: string): RegisteredSwarm {
|
|
391
391
|
cid = swarmUrl(cid);
|
|
392
392
|
return this.transaction(() => {
|
|
393
393
|
if (alias !== undefined) this.checkSwarmAlias(alias, cid);
|
|
394
|
-
|
|
395
|
-
const entry = { cid, alias: alias ?? previous?.alias ?? null };
|
|
396
|
-
this.db
|
|
397
|
-
.prepare("INSERT OR REPLACE INTO swarms VALUES (?,?)")
|
|
398
|
-
.run(cid, entry.alias);
|
|
399
|
-
if (this.config({ includeSecrets: false })["swarm.default"] == null)
|
|
400
|
-
this.setConfig("swarm.default", cid);
|
|
401
|
-
return entry;
|
|
394
|
+
return this.saveSwarmRegistration(cid, alias);
|
|
402
395
|
});
|
|
403
396
|
}
|
|
397
|
+
setSwarm(
|
|
398
|
+
alias: string,
|
|
399
|
+
url: string,
|
|
400
|
+
expectedTarget: string | null,
|
|
401
|
+
): RegisteredSwarm {
|
|
402
|
+
swarmAlias(alias);
|
|
403
|
+
url = swarmUrl(url);
|
|
404
|
+
return this.transaction(() => {
|
|
405
|
+
const current = this.swarms().find((entry) => entry.alias === alias);
|
|
406
|
+
if ((current?.cid ?? null) !== expectedTarget)
|
|
407
|
+
throw new HostError(
|
|
408
|
+
`Swarm alias "${alias}" changed while setting it. Run ipab swarm list and retry.`,
|
|
409
|
+
);
|
|
410
|
+
if (current && current.cid !== url)
|
|
411
|
+
this.db
|
|
412
|
+
.prepare("UPDATE swarms SET alias=NULL WHERE cid=?")
|
|
413
|
+
.run(current.cid);
|
|
414
|
+
return this.saveSwarmRegistration(url, alias);
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
private saveSwarmRegistration(cid: string, alias?: string): RegisteredSwarm {
|
|
418
|
+
const previous = this.swarms().find((entry) => entry.cid === cid);
|
|
419
|
+
const entry = { cid, alias: alias ?? previous?.alias ?? null };
|
|
420
|
+
this.db
|
|
421
|
+
.prepare("INSERT OR REPLACE INTO swarms VALUES (?,?)")
|
|
422
|
+
.run(cid, entry.alias);
|
|
423
|
+
if (this.config({ includeSecrets: false })["swarm.default"] == null)
|
|
424
|
+
this.setConfig("swarm.default", cid);
|
|
425
|
+
return entry;
|
|
426
|
+
}
|
|
404
427
|
useSwarm(reference: string): string {
|
|
405
428
|
return this.transaction(() => {
|
|
406
429
|
const entry = this.registeredSwarm(reference);
|
|
@@ -725,7 +748,9 @@ export class State {
|
|
|
725
748
|
(exactCid(pin.reference) !== pin.reference ||
|
|
726
749
|
(pin.cid !== null && pin.cid !== pin.reference))) ||
|
|
727
750
|
(pin.swarm !== null &&
|
|
728
|
-
(!pin.
|
|
751
|
+
(!pin.reference.startsWith("ipab://") ||
|
|
752
|
+
(pin.name === "" &&
|
|
753
|
+
!/^ipab:\/\/[^/]+\/$/u.test(pin.reference)))) ||
|
|
729
754
|
(pin.status === "ready" && (!pin.cid || !pin.targetCid))
|
|
730
755
|
)
|
|
731
756
|
throw new HostError(
|
package/src/swarm-files.ts
CHANGED
|
@@ -78,6 +78,7 @@ export async function withPrivateLock<T>(
|
|
|
78
78
|
directory: string,
|
|
79
79
|
operation: () => Promise<T>,
|
|
80
80
|
signal?: AbortSignal,
|
|
81
|
+
attempts = 300,
|
|
81
82
|
): Promise<T> {
|
|
82
83
|
signal?.throwIfAborted();
|
|
83
84
|
const path = join(directory, "operation.sqlite");
|
|
@@ -93,7 +94,7 @@ export async function withPrivateLock<T>(
|
|
|
93
94
|
let locked = false;
|
|
94
95
|
try {
|
|
95
96
|
db.exec("PRAGMA busy_timeout=0");
|
|
96
|
-
for (let attempt = 0; attempt <
|
|
97
|
+
for (let attempt = 0; attempt < attempts; attempt++) {
|
|
97
98
|
signal?.throwIfAborted();
|
|
98
99
|
try {
|
|
99
100
|
db.exec("BEGIN IMMEDIATE");
|
package/src/swarm-meta.ts
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
import { encodeSchema, type BundleType } from "@ipab/bundle";
|
|
2
2
|
import { HostError, object } from "./errors.js";
|
|
3
3
|
import { relativePath } from "./space.js";
|
|
4
|
+
import {
|
|
5
|
+
swarmStateSchema,
|
|
6
|
+
previousSwarmStateSchema,
|
|
7
|
+
legacySwarmStateSchema,
|
|
8
|
+
} from "@ipab/bundle/swarm-state";
|
|
9
|
+
export {
|
|
10
|
+
swarmStateType,
|
|
11
|
+
swarmStateSchema,
|
|
12
|
+
previousSwarmStateType,
|
|
13
|
+
previousSwarmStateSchema,
|
|
14
|
+
legacySwarmStateType,
|
|
15
|
+
legacySwarmStateSchema,
|
|
16
|
+
parseSwarmState,
|
|
17
|
+
} from "@ipab/bundle/swarm-state";
|
|
4
18
|
|
|
5
19
|
export const legacySwarmMetaType: BundleType = {
|
|
6
20
|
contentType: "application/json",
|
|
@@ -34,43 +48,10 @@ export interface SwarmMeta {
|
|
|
34
48
|
registryPath: string;
|
|
35
49
|
skillPath?: string;
|
|
36
50
|
}
|
|
37
|
-
export const legacySwarmStateType: BundleType = {
|
|
38
|
-
contentType: "application/json",
|
|
39
|
-
schema: {
|
|
40
|
-
type: "object",
|
|
41
|
-
required: ["v", "templatePath", "registryPath", "names"],
|
|
42
|
-
properties: {
|
|
43
|
-
v: { const: 5 },
|
|
44
|
-
templatePath: { type: "string", minLength: 1, maxLength: 196 },
|
|
45
|
-
registryPath: { type: "string", minLength: 1, maxLength: 196 },
|
|
46
|
-
names: {
|
|
47
|
-
type: "object",
|
|
48
|
-
additionalProperties: { type: "string" },
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
additionalProperties: false,
|
|
52
|
-
},
|
|
53
|
-
};
|
|
54
|
-
export const swarmStateType: BundleType = {
|
|
55
|
-
contentType: "application/json",
|
|
56
|
-
schema: {
|
|
57
|
-
type: "object",
|
|
58
|
-
required: ["v", "templatePath", "registryPath", "skillPath", "names"],
|
|
59
|
-
properties: {
|
|
60
|
-
v: { const: 5 },
|
|
61
|
-
templatePath: { type: "string", minLength: 1, maxLength: 196 },
|
|
62
|
-
registryPath: { type: "string", minLength: 1, maxLength: 196 },
|
|
63
|
-
skillPath: { type: "string", minLength: 1, maxLength: 196 },
|
|
64
|
-
names: { type: "object", additionalProperties: { type: "string" } },
|
|
65
|
-
},
|
|
66
|
-
additionalProperties: false,
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
export const swarmStateSchema = await encodeSchema(swarmStateType);
|
|
70
|
-
export const legacySwarmStateSchema = await encodeSchema(legacySwarmStateType);
|
|
71
51
|
export function isSwarmStateSchema(cid: string): boolean {
|
|
72
52
|
return (
|
|
73
53
|
cid === swarmStateSchema.cid.toString() ||
|
|
54
|
+
cid === previousSwarmStateSchema.cid.toString() ||
|
|
74
55
|
cid === legacySwarmStateSchema.cid.toString()
|
|
75
56
|
);
|
|
76
57
|
}
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { object, HostError } from "./errors.js";
|
|
4
|
+
import { readPrivate, writePrivate, privateDirectory } from "./swarm-files.js";
|
|
5
|
+
import { nodeRecord, validateAddress, type PeerConnection } from "./kubo.js";
|
|
6
|
+
import { isCanonicalPublicKey } from "@ipab/bundle/pointer/protocol";
|
|
7
|
+
import type { PivotCandidate } from "./kubo-topology.js";
|
|
8
|
+
import type { Organization } from "./swarm.js";
|
|
9
|
+
|
|
10
|
+
export type PivotLocalStatus =
|
|
11
|
+
"selected" | "cooldown" | "unknown" | "self" | "candidate";
|
|
12
|
+
|
|
13
|
+
export interface PivotObservation extends PivotCandidate {
|
|
14
|
+
peerId: string;
|
|
15
|
+
selected: boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface NetworkingSnapshot {
|
|
18
|
+
v: 1;
|
|
19
|
+
binding: string;
|
|
20
|
+
pid: number;
|
|
21
|
+
running: boolean;
|
|
22
|
+
updatedAt: number;
|
|
23
|
+
verifiedAt: number | null;
|
|
24
|
+
error: string | null;
|
|
25
|
+
topology: string | null;
|
|
26
|
+
pivots: PivotObservation[];
|
|
27
|
+
account?: string;
|
|
28
|
+
connections?: {
|
|
29
|
+
observedAt: number | null;
|
|
30
|
+
error: string | null;
|
|
31
|
+
peers: PeerConnection[];
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export interface LocalObservations {
|
|
35
|
+
status: "current" | "stale" | "unavailable";
|
|
36
|
+
updatedAt: string | null;
|
|
37
|
+
verifiedAt: string | null;
|
|
38
|
+
error: string | null;
|
|
39
|
+
snapshot: NetworkingSnapshot | null;
|
|
40
|
+
}
|
|
41
|
+
export const observationMaxAge = 90_000;
|
|
42
|
+
|
|
43
|
+
export function networkingBinding(
|
|
44
|
+
configUrl: string,
|
|
45
|
+
account: string,
|
|
46
|
+
config: Organization,
|
|
47
|
+
): string {
|
|
48
|
+
return createHash("sha256")
|
|
49
|
+
.update(
|
|
50
|
+
JSON.stringify([
|
|
51
|
+
configUrl,
|
|
52
|
+
account,
|
|
53
|
+
config.owner,
|
|
54
|
+
config.scopeId,
|
|
55
|
+
config.headId,
|
|
56
|
+
config.epoch,
|
|
57
|
+
config.members.map(
|
|
58
|
+
({ publicKey, encryptionKey, peerId, profileId, requestId }) => [
|
|
59
|
+
publicKey,
|
|
60
|
+
encryptionKey,
|
|
61
|
+
peerId,
|
|
62
|
+
profileId,
|
|
63
|
+
requestId,
|
|
64
|
+
],
|
|
65
|
+
),
|
|
66
|
+
]),
|
|
67
|
+
)
|
|
68
|
+
.digest("hex");
|
|
69
|
+
}
|
|
70
|
+
const fileName = (url: string) =>
|
|
71
|
+
`${createHash("sha256").update(url).digest("hex")}.json`;
|
|
72
|
+
const timestamp = (value: unknown): value is number =>
|
|
73
|
+
typeof value === "number" &&
|
|
74
|
+
Number.isSafeInteger(value) &&
|
|
75
|
+
value >= 0 &&
|
|
76
|
+
value <= 8_640_000_000_000_000;
|
|
77
|
+
const nullableError = (value: unknown): value is string | null =>
|
|
78
|
+
value === null || (typeof value === "string" && value.length <= 2048);
|
|
79
|
+
function sameAddresses(value: unknown, expected: readonly string[]): boolean {
|
|
80
|
+
if (!Array.isArray(value)) return false;
|
|
81
|
+
const addresses = value as unknown[];
|
|
82
|
+
return (
|
|
83
|
+
addresses.every((address) => typeof address === "string") &&
|
|
84
|
+
JSON.stringify([...addresses].sort()) === JSON.stringify(expected)
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function parseSnapshot(input: unknown): NetworkingSnapshot {
|
|
89
|
+
const data = object(input, "local networking snapshot");
|
|
90
|
+
if (
|
|
91
|
+
data.v !== 1 ||
|
|
92
|
+
typeof data.binding !== "string" ||
|
|
93
|
+
!/^[a-f0-9]{64}$/.test(data.binding) ||
|
|
94
|
+
!timestamp(data.pid) ||
|
|
95
|
+
data.pid === 0 ||
|
|
96
|
+
typeof data.running !== "boolean" ||
|
|
97
|
+
!timestamp(data.updatedAt) ||
|
|
98
|
+
!(data.verifiedAt === null || timestamp(data.verifiedAt)) ||
|
|
99
|
+
!nullableError(data.error) ||
|
|
100
|
+
!(
|
|
101
|
+
data.topology === null ||
|
|
102
|
+
(typeof data.topology === "string" &&
|
|
103
|
+
/^[a-f0-9]{64}$/.test(data.topology))
|
|
104
|
+
) ||
|
|
105
|
+
!Array.isArray(data.pivots) ||
|
|
106
|
+
data.pivots.length > 256
|
|
107
|
+
)
|
|
108
|
+
throw new HostError("Invalid local networking snapshot.");
|
|
109
|
+
const updatedAt = data.updatedAt;
|
|
110
|
+
const pivots = data.pivots.map((input: unknown): PivotObservation => {
|
|
111
|
+
const entry = object(input, "local Pivot observation");
|
|
112
|
+
if (
|
|
113
|
+
typeof entry.peerId !== "string" ||
|
|
114
|
+
!/^[A-Za-z0-9]{32,128}$/.test(entry.peerId) ||
|
|
115
|
+
!Array.isArray(entry.addresses) ||
|
|
116
|
+
entry.addresses.length > 16 ||
|
|
117
|
+
!timestamp(entry.failures) ||
|
|
118
|
+
!timestamp(entry.retryAt) ||
|
|
119
|
+
typeof entry.probe !== "string" ||
|
|
120
|
+
!["success", "failure", "unknown"].includes(entry.probe) ||
|
|
121
|
+
!(entry.probedAt === null || timestamp(entry.probedAt)) ||
|
|
122
|
+
!nullableError(entry.error) ||
|
|
123
|
+
typeof entry.selected !== "boolean" ||
|
|
124
|
+
(entry.probe === "unknown") !== (entry.probedAt === null) ||
|
|
125
|
+
(typeof entry.probedAt === "number" && entry.probedAt > updatedAt) ||
|
|
126
|
+
(entry.probe === "success" &&
|
|
127
|
+
(entry.failures !== 0 ||
|
|
128
|
+
entry.retryAt !== 0 ||
|
|
129
|
+
entry.error !== null)) ||
|
|
130
|
+
(entry.probe === "failure" && entry.failures === 0) ||
|
|
131
|
+
(entry.selected && entry.addresses.length === 0)
|
|
132
|
+
)
|
|
133
|
+
throw new HostError("Invalid local Pivot observation.");
|
|
134
|
+
const peerId = entry.peerId;
|
|
135
|
+
const addresses = entry.addresses.map((address: unknown) => {
|
|
136
|
+
if (typeof address !== "string")
|
|
137
|
+
throw new HostError("Invalid Pivot address.");
|
|
138
|
+
validateAddress(address, peerId);
|
|
139
|
+
return address;
|
|
140
|
+
});
|
|
141
|
+
return {
|
|
142
|
+
peerId,
|
|
143
|
+
addresses,
|
|
144
|
+
failures: entry.failures,
|
|
145
|
+
retryAt: entry.retryAt,
|
|
146
|
+
probe: entry.probe as PivotObservation["probe"],
|
|
147
|
+
probedAt: entry.probedAt,
|
|
148
|
+
error: entry.error,
|
|
149
|
+
selected: entry.selected,
|
|
150
|
+
};
|
|
151
|
+
});
|
|
152
|
+
if (new Set(pivots.map(({ peerId }) => peerId)).size !== pivots.length)
|
|
153
|
+
throw new HostError("Duplicate local Pivot observation.");
|
|
154
|
+
if (data.account !== undefined && !isCanonicalPublicKey(data.account))
|
|
155
|
+
throw new HostError("Invalid observation account.");
|
|
156
|
+
let connections: NetworkingSnapshot["connections"];
|
|
157
|
+
if (data.connections !== undefined) {
|
|
158
|
+
const saved = object(data.connections, "connection observation");
|
|
159
|
+
if (
|
|
160
|
+
!(saved.observedAt === null || timestamp(saved.observedAt)) ||
|
|
161
|
+
(typeof saved.observedAt === "number" && saved.observedAt > updatedAt) ||
|
|
162
|
+
!nullableError(saved.error) ||
|
|
163
|
+
!Array.isArray(saved.peers) ||
|
|
164
|
+
saved.peers.length > 4096 ||
|
|
165
|
+
(saved.observedAt === null && saved.peers.length !== 0)
|
|
166
|
+
)
|
|
167
|
+
throw new HostError("Invalid connection observation.");
|
|
168
|
+
connections = {
|
|
169
|
+
observedAt: saved.observedAt,
|
|
170
|
+
error: saved.error,
|
|
171
|
+
peers: saved.peers.map((input: unknown) => {
|
|
172
|
+
const peer = object(input, "connection peer");
|
|
173
|
+
if (
|
|
174
|
+
typeof peer.peerId !== "string" ||
|
|
175
|
+
!/^[A-Za-z0-9]{32,128}$/.test(peer.peerId) ||
|
|
176
|
+
typeof peer.address !== "string" ||
|
|
177
|
+
peer.address.length > 2048 ||
|
|
178
|
+
!peer.address.startsWith("/") ||
|
|
179
|
+
[...peer.address].some((char) => char.charCodeAt(0) <= 32) ||
|
|
180
|
+
!["direct", "relay"].includes(String(peer.transport)) ||
|
|
181
|
+
(peer.transport === "relay") !== peer.address.includes("/p2p-circuit")
|
|
182
|
+
)
|
|
183
|
+
throw new HostError("Invalid connection peer.");
|
|
184
|
+
return {
|
|
185
|
+
peerId: peer.peerId,
|
|
186
|
+
address: peer.address,
|
|
187
|
+
transport: peer.transport as PeerConnection["transport"],
|
|
188
|
+
};
|
|
189
|
+
}),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
v: 1,
|
|
194
|
+
binding: data.binding,
|
|
195
|
+
pid: data.pid,
|
|
196
|
+
running: data.running,
|
|
197
|
+
updatedAt: data.updatedAt,
|
|
198
|
+
verifiedAt: data.verifiedAt,
|
|
199
|
+
error: data.error,
|
|
200
|
+
topology: data.topology,
|
|
201
|
+
pivots,
|
|
202
|
+
...(typeof data.account === "string" ? { account: data.account } : {}),
|
|
203
|
+
...(connections ? { connections } : {}),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
export async function networkingAccount(
|
|
207
|
+
stateDir: string,
|
|
208
|
+
url: string,
|
|
209
|
+
): Promise<string | undefined> {
|
|
210
|
+
const saved = await readPrivate<unknown>(
|
|
211
|
+
join(stateDir, "swarm-observations"),
|
|
212
|
+
fileName(url),
|
|
213
|
+
[{ path: join(stateDir, "swarm-observations"), directory: true }],
|
|
214
|
+
);
|
|
215
|
+
return saved === undefined ? undefined : parseSnapshot(saved).account;
|
|
216
|
+
}
|
|
217
|
+
export async function writeNetworkingSnapshot(
|
|
218
|
+
stateDir: string,
|
|
219
|
+
url: string,
|
|
220
|
+
snapshot: NetworkingSnapshot,
|
|
221
|
+
): Promise<void> {
|
|
222
|
+
const parsed = parseSnapshot(snapshot);
|
|
223
|
+
if (Buffer.byteLength(JSON.stringify(parsed)) > 2 * 1024 * 1024)
|
|
224
|
+
throw new HostError("Local networking snapshot is oversized.");
|
|
225
|
+
const directory = await privateDirectory(stateDir, "swarm-observations");
|
|
226
|
+
await writePrivate(directory, fileName(url), parsed);
|
|
227
|
+
}
|
|
228
|
+
export function snapshotFreshness(
|
|
229
|
+
snapshot: NetworkingSnapshot,
|
|
230
|
+
binding: string,
|
|
231
|
+
now = Date.now(),
|
|
232
|
+
): string | null {
|
|
233
|
+
if (snapshot.binding !== binding)
|
|
234
|
+
return "Membership or epoch binding changed.";
|
|
235
|
+
if (!snapshot.running) return "Daemon stopped.";
|
|
236
|
+
try {
|
|
237
|
+
process.kill(snapshot.pid, 0);
|
|
238
|
+
} catch (cause) {
|
|
239
|
+
if ((cause as NodeJS.ErrnoException).code === "ESRCH")
|
|
240
|
+
return "Daemon is not running.";
|
|
241
|
+
return "Daemon liveness could not be verified.";
|
|
242
|
+
}
|
|
243
|
+
if (
|
|
244
|
+
snapshot.updatedAt > now ||
|
|
245
|
+
snapshot.verifiedAt === null ||
|
|
246
|
+
snapshot.verifiedAt > snapshot.updatedAt ||
|
|
247
|
+
now - snapshot.updatedAt > observationMaxAge ||
|
|
248
|
+
now - snapshot.verifiedAt > 180_000
|
|
249
|
+
)
|
|
250
|
+
return "Daemon observations are stale.";
|
|
251
|
+
return snapshot.error;
|
|
252
|
+
}
|
|
253
|
+
export async function readNetworkingSnapshot(
|
|
254
|
+
stateDir: string,
|
|
255
|
+
url: string,
|
|
256
|
+
binding: string,
|
|
257
|
+
scope: string,
|
|
258
|
+
epoch: number,
|
|
259
|
+
peerId: string,
|
|
260
|
+
): Promise<LocalObservations> {
|
|
261
|
+
const saved = await readPrivate<unknown>(
|
|
262
|
+
join(stateDir, "swarm-observations"),
|
|
263
|
+
fileName(url),
|
|
264
|
+
[{ path: join(stateDir, "swarm-observations"), directory: true }],
|
|
265
|
+
);
|
|
266
|
+
if (!saved)
|
|
267
|
+
return {
|
|
268
|
+
status: "unavailable",
|
|
269
|
+
updatedAt: null,
|
|
270
|
+
verifiedAt: null,
|
|
271
|
+
error:
|
|
272
|
+
"No daemon observations; run ipab daemon to collect local Pivot observations.",
|
|
273
|
+
snapshot: null,
|
|
274
|
+
};
|
|
275
|
+
const snapshot = parseSnapshot(saved);
|
|
276
|
+
const error = snapshotFreshness(snapshot, binding);
|
|
277
|
+
let topologyError: string | null = null;
|
|
278
|
+
const managed = await readPrivate<unknown>(
|
|
279
|
+
join(stateDir, "swarm-nodes", scope),
|
|
280
|
+
"node.json",
|
|
281
|
+
[
|
|
282
|
+
{ path: join(stateDir, "swarm-nodes"), directory: true },
|
|
283
|
+
{ path: join(stateDir, "swarm-nodes", scope), directory: true },
|
|
284
|
+
],
|
|
285
|
+
);
|
|
286
|
+
if (managed) {
|
|
287
|
+
const node = nodeRecord(managed, scope);
|
|
288
|
+
if (
|
|
289
|
+
node.peerId !== peerId ||
|
|
290
|
+
node.epoch !== epoch ||
|
|
291
|
+
!snapshot.topology ||
|
|
292
|
+
node.topology !== snapshot.topology
|
|
293
|
+
)
|
|
294
|
+
topologyError =
|
|
295
|
+
"Applied local topology cannot be confirmed for these observations.";
|
|
296
|
+
if (!topologyError) {
|
|
297
|
+
const config = await readPrivate<Record<string, unknown>>(
|
|
298
|
+
join(stateDir, "swarm-nodes", scope, "repo"),
|
|
299
|
+
"config",
|
|
300
|
+
[
|
|
301
|
+
{
|
|
302
|
+
path: join(stateDir, "swarm-nodes", scope, "repo"),
|
|
303
|
+
directory: true,
|
|
304
|
+
},
|
|
305
|
+
],
|
|
306
|
+
);
|
|
307
|
+
const selected = snapshot.pivots.filter((pivot) => pivot.selected);
|
|
308
|
+
const bootstrap = selected.flatMap(({ addresses }) => addresses).sort();
|
|
309
|
+
const relays = selected
|
|
310
|
+
.flatMap(({ addresses }) => (addresses[0] ? [addresses[0]] : []))
|
|
311
|
+
.sort();
|
|
312
|
+
const swarm = object(
|
|
313
|
+
config?.Swarm ?? {},
|
|
314
|
+
"local Kubo Swarm configuration",
|
|
315
|
+
);
|
|
316
|
+
const relayClient = object(
|
|
317
|
+
swarm.RelayClient ?? {},
|
|
318
|
+
"local Kubo relay configuration",
|
|
319
|
+
);
|
|
320
|
+
if (
|
|
321
|
+
!config ||
|
|
322
|
+
object(config.Identity, "local Kubo identity").PeerID !== peerId ||
|
|
323
|
+
!sameAddresses(config.Bootstrap, bootstrap) ||
|
|
324
|
+
!sameAddresses(relayClient.StaticRelays, relays)
|
|
325
|
+
)
|
|
326
|
+
topologyError =
|
|
327
|
+
"Current Bootstrap/StaticRelays differ from the observed applied selection.";
|
|
328
|
+
}
|
|
329
|
+
} else topologyError = "No existing managed node.";
|
|
330
|
+
return {
|
|
331
|
+
status: error || topologyError ? "stale" : "current",
|
|
332
|
+
updatedAt: new Date(snapshot.updatedAt).toISOString(),
|
|
333
|
+
verifiedAt:
|
|
334
|
+
snapshot.verifiedAt === null
|
|
335
|
+
? null
|
|
336
|
+
: new Date(snapshot.verifiedAt).toISOString(),
|
|
337
|
+
error:
|
|
338
|
+
[
|
|
339
|
+
...new Set(
|
|
340
|
+
[
|
|
341
|
+
error,
|
|
342
|
+
snapshot.binding === binding ? snapshot.error : null,
|
|
343
|
+
topologyError,
|
|
344
|
+
].filter((value) => value !== null),
|
|
345
|
+
),
|
|
346
|
+
].join(" ") || null,
|
|
347
|
+
// Never expose observations from another account, epoch or membership.
|
|
348
|
+
snapshot: snapshot.binding === binding ? snapshot : null,
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export function pivotLocalStatus(
|
|
353
|
+
pivot: boolean,
|
|
354
|
+
self: boolean,
|
|
355
|
+
addresses: readonly string[],
|
|
356
|
+
peerId: string,
|
|
357
|
+
local: LocalObservations,
|
|
358
|
+
now = Date.now(),
|
|
359
|
+
): PivotLocalStatus | null {
|
|
360
|
+
if (!pivot) return null;
|
|
361
|
+
if (self) return "self";
|
|
362
|
+
if (local.status !== "current") return "unknown";
|
|
363
|
+
const observation = local.snapshot?.pivots.find(
|
|
364
|
+
(entry) => entry.peerId === peerId,
|
|
365
|
+
);
|
|
366
|
+
if (
|
|
367
|
+
!observation ||
|
|
368
|
+
JSON.stringify([...new Set(addresses)].sort()) !==
|
|
369
|
+
JSON.stringify(observation.addresses)
|
|
370
|
+
)
|
|
371
|
+
return "unknown";
|
|
372
|
+
if (observation.retryAt > now) return "cooldown";
|
|
373
|
+
return observation.selected ? "selected" : "candidate";
|
|
374
|
+
}
|