@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/config.ts
CHANGED
|
@@ -3,11 +3,12 @@ import { join, resolve } from "node:path";
|
|
|
3
3
|
import { CID } from "multiformats/cid";
|
|
4
4
|
import { readContentConfiguration } from "@ipab/ipfs-store/node";
|
|
5
5
|
import type { StoreConfiguration } from "@ipab/ipfs-store";
|
|
6
|
-
import { HostError, object, syntax } from "./
|
|
6
|
+
import { HostError, object, syntax } from "./host-error.js";
|
|
7
7
|
|
|
8
8
|
export type ConfigKey =
|
|
9
9
|
| "swarm.default"
|
|
10
10
|
| "swarm.listen"
|
|
11
|
+
| "swarm.pivot"
|
|
11
12
|
| "swarm.cacheTtl"
|
|
12
13
|
| "swarm.forceRefresh"
|
|
13
14
|
| "store.gateway"
|
|
@@ -33,6 +34,7 @@ export interface ConfigItem {
|
|
|
33
34
|
export const defaults: Record<ConfigKey, ConfigValue> = {
|
|
34
35
|
"swarm.default": null,
|
|
35
36
|
"swarm.listen": "lan",
|
|
37
|
+
"swarm.pivot": false,
|
|
36
38
|
"swarm.cacheTtl": 300,
|
|
37
39
|
"swarm.forceRefresh": false,
|
|
38
40
|
"store.gateway": null,
|
|
@@ -45,6 +47,7 @@ export const defaults: Record<ConfigKey, ConfigValue> = {
|
|
|
45
47
|
};
|
|
46
48
|
const environments: Partial<Record<ConfigKey, string>> = {
|
|
47
49
|
"swarm.listen": "IPAB_SWARM_LISTEN",
|
|
50
|
+
"swarm.pivot": "IPAB_SWARM_PIVOT",
|
|
48
51
|
"swarm.cacheTtl": "IPAB_SWARM_CACHE_TTL",
|
|
49
52
|
"swarm.forceRefresh": "IPAB_SWARM_FORCE_REFRESH",
|
|
50
53
|
"store.gateway": "IPAB_STORE_GATEWAY",
|
|
@@ -127,10 +130,15 @@ export function parseValue(
|
|
|
127
130
|
if (key.startsWith("swarm.") && value === "default")
|
|
128
131
|
return key === "swarm.cacheTtl" ||
|
|
129
132
|
key === "swarm.forceRefresh" ||
|
|
133
|
+
key === "swarm.pivot" ||
|
|
130
134
|
key === "swarm.listen"
|
|
131
135
|
? defaults[key]
|
|
132
136
|
: null;
|
|
133
|
-
if (
|
|
137
|
+
if (
|
|
138
|
+
key === "history.enabled" ||
|
|
139
|
+
key === "swarm.forceRefresh" ||
|
|
140
|
+
key === "swarm.pivot"
|
|
141
|
+
) {
|
|
134
142
|
if (value === "true") return true;
|
|
135
143
|
if (value === "false") return false;
|
|
136
144
|
}
|
|
@@ -177,9 +185,9 @@ export function parseValue(
|
|
|
177
185
|
"swarm.cacheTtl requires an integer number of seconds from 0 to 86400.",
|
|
178
186
|
);
|
|
179
187
|
}
|
|
180
|
-
if (key === "swarm.forceRefresh") {
|
|
188
|
+
if (key === "swarm.forceRefresh" || key === "swarm.pivot") {
|
|
181
189
|
if (typeof value === "boolean") return value;
|
|
182
|
-
syntax(
|
|
190
|
+
syntax(`${key} requires true or false.`);
|
|
183
191
|
}
|
|
184
192
|
if (key === "swarm.listen") {
|
|
185
193
|
if (value === "lan" || value === "loopback") return value;
|
package/src/context.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { HostError } from "./
|
|
2
|
-
import { CHUNK_BYTES, relativePath } from "./space.js";
|
|
1
|
+
import { HostError } from "./host-error.js";
|
|
2
|
+
import { CHUNK_BYTES, relativePath } from "./space-path.js";
|
|
3
3
|
|
|
4
4
|
import type { BundleInspection } from "./context-types.js";
|
|
5
5
|
export type { BundleInspection } from "./context-types.js";
|
|
@@ -27,6 +27,10 @@ export interface AppContext {
|
|
|
27
27
|
reference: string,
|
|
28
28
|
options?: { signal?: AbortSignal; swarm?: string },
|
|
29
29
|
): Promise<BundleInspection>;
|
|
30
|
+
readBlock(
|
|
31
|
+
cid: string,
|
|
32
|
+
options?: { signal?: AbortSignal; maxBytes?: number },
|
|
33
|
+
): Promise<Uint8Array>;
|
|
30
34
|
readFile(
|
|
31
35
|
rootCid: string,
|
|
32
36
|
path: string,
|
|
@@ -211,6 +215,15 @@ export function createContext(port: Port): {
|
|
|
211
215
|
throw new HostError("Invalid Bundle inspection response.");
|
|
212
216
|
return data as BundleInspection;
|
|
213
217
|
},
|
|
218
|
+
async readBlock(cid, options = {}) {
|
|
219
|
+
const maxBytes = options.maxBytes ?? 65536;
|
|
220
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 1 || maxBytes > 65536)
|
|
221
|
+
throw new HostError("Block read limit must be 1..65536 bytes.");
|
|
222
|
+
const result = await rpc("readBlock", { cid, maxBytes }, options.signal);
|
|
223
|
+
if (!(result instanceof Uint8Array) || result.byteLength > maxBytes)
|
|
224
|
+
throw new HostError("Invalid bounded block response.");
|
|
225
|
+
return result;
|
|
226
|
+
},
|
|
214
227
|
get appCid() {
|
|
215
228
|
if (!appCid)
|
|
216
229
|
throw new HostError("IPAB context identity is not initialized.");
|
package/src/errors.ts
CHANGED
|
@@ -1,23 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
constructor(
|
|
3
|
-
message: string,
|
|
4
|
-
readonly exitCode = 1,
|
|
5
|
-
readonly code = "OPERATION_FAILED",
|
|
6
|
-
readonly phase = "state",
|
|
7
|
-
) {
|
|
8
|
-
super(message);
|
|
9
|
-
this.name = "HostError";
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function syntax(message: string): never {
|
|
14
|
-
throw new HostError(
|
|
15
|
-
`${message} Run ipab --help.`,
|
|
16
|
-
2,
|
|
17
|
-
"INVALID_ARGUMENT",
|
|
18
|
-
"arguments",
|
|
19
|
-
);
|
|
20
|
-
}
|
|
1
|
+
export { HostError, object, syntax } from "./host-error.js";
|
|
21
2
|
|
|
22
3
|
export function message(error: unknown): string {
|
|
23
4
|
let text =
|
|
@@ -38,9 +19,4 @@ export function message(error: unknown): string {
|
|
|
38
19
|
});
|
|
39
20
|
}
|
|
40
21
|
|
|
41
|
-
export function object(value: unknown, label: string): Record<string, unknown> {
|
|
42
|
-
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
43
|
-
throw new HostError(`Invalid ${label}.`);
|
|
44
|
-
return value as Record<string, unknown>;
|
|
45
|
-
}
|
|
46
22
|
import { ContentBundleError } from "@ipab/bundle";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export class HostError extends Error {
|
|
2
|
+
constructor(
|
|
3
|
+
message: string,
|
|
4
|
+
readonly exitCode = 1,
|
|
5
|
+
readonly code = "OPERATION_FAILED",
|
|
6
|
+
readonly phase = "state",
|
|
7
|
+
) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "HostError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function syntax(message: string): never {
|
|
14
|
+
throw new HostError(
|
|
15
|
+
`${message} Run ipab --help.`,
|
|
16
|
+
2,
|
|
17
|
+
"INVALID_ARGUMENT",
|
|
18
|
+
"arguments",
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function object(value: unknown, label: string): Record<string, unknown> {
|
|
23
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
24
|
+
throw new HostError(`Invalid ${label}.`);
|
|
25
|
+
return value as Record<string, unknown>;
|
|
26
|
+
}
|
package/src/ipc.ts
CHANGED
|
@@ -171,6 +171,25 @@ export class ContextServer {
|
|
|
171
171
|
): Promise<unknown> {
|
|
172
172
|
signal.throwIfAborted();
|
|
173
173
|
if (operation === "identity") return this.appCid;
|
|
174
|
+
if (operation === "readBlock") {
|
|
175
|
+
const data = object(args, "block read request");
|
|
176
|
+
if (
|
|
177
|
+
typeof data.cid !== "string" ||
|
|
178
|
+
typeof data.maxBytes !== "number" ||
|
|
179
|
+
!Number.isSafeInteger(data.maxBytes) ||
|
|
180
|
+
data.maxBytes < 1 ||
|
|
181
|
+
data.maxBytes > 65536
|
|
182
|
+
)
|
|
183
|
+
throw new HostError("Block read limit must be 1..65536 bytes.");
|
|
184
|
+
const cid = CID.parse(data.cid);
|
|
185
|
+
if (cid.toV1().toString() !== data.cid)
|
|
186
|
+
throw new HostError("Block reads require an exact canonical CID.");
|
|
187
|
+
const bytes = await this.store.block.get(data.cid, { signal });
|
|
188
|
+
signal.throwIfAborted();
|
|
189
|
+
if (bytes.byteLength > data.maxBytes)
|
|
190
|
+
throw new HostError("Block exceeds the requested read limit.");
|
|
191
|
+
return bytes;
|
|
192
|
+
}
|
|
174
193
|
if (operation === "deriveBundle") {
|
|
175
194
|
const data = object(args, "Bundle derivation request");
|
|
176
195
|
if (typeof data.inputCid !== "string")
|
package/src/kubo-install.ts
CHANGED
package/src/kubo-peers.ts
CHANGED
|
@@ -22,9 +22,8 @@ export function advertisedIps(
|
|
|
22
22
|
if (
|
|
23
23
|
!item.internal &&
|
|
24
24
|
item.family === "IPv4" &&
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
)
|
|
25
|
+
!/^(?:0\.|127\.|169\.254\.)/.test(item.address) &&
|
|
26
|
+
Number(item.address.split(".")[0]) < 224
|
|
28
27
|
)
|
|
29
28
|
ips.add(item.address);
|
|
30
29
|
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { object } from "./errors.js";
|
|
2
|
+
import type { PeeringPeer } from "./kubo-peering.js";
|
|
3
|
+
|
|
4
|
+
export function topologyConfig(
|
|
5
|
+
config: Record<string, unknown>,
|
|
6
|
+
pivot: boolean,
|
|
7
|
+
peers: readonly PeeringPeer[],
|
|
8
|
+
loopback: boolean,
|
|
9
|
+
): Record<string, unknown> {
|
|
10
|
+
const swarm = object(config.Swarm, "Kubo Swarm configuration");
|
|
11
|
+
const addresses = peers
|
|
12
|
+
.flatMap(({ ID, Addrs }) => Addrs.map((address) => `${address}/p2p/${ID}`))
|
|
13
|
+
.sort();
|
|
14
|
+
return {
|
|
15
|
+
...config,
|
|
16
|
+
Bootstrap: addresses,
|
|
17
|
+
Peering: { Peers: [] },
|
|
18
|
+
AutoConf: { Enabled: false },
|
|
19
|
+
AutoTLS: { Enabled: false },
|
|
20
|
+
AutoNAT: { ServiceMode: "disabled" },
|
|
21
|
+
Ipns: {
|
|
22
|
+
...object(config.Ipns ?? {}, "Kubo IPNS configuration"),
|
|
23
|
+
DelegatedPublishers: [],
|
|
24
|
+
},
|
|
25
|
+
Routing: {
|
|
26
|
+
Type: pivot ? "dhtserver" : "dhtclient",
|
|
27
|
+
DelegatedRouters: [],
|
|
28
|
+
LoopbackAddressesOnLanDHT: loopback,
|
|
29
|
+
},
|
|
30
|
+
Internal: {
|
|
31
|
+
...object(config.Internal ?? {}, "Kubo Internal configuration"),
|
|
32
|
+
// Private swarms cannot use public AutoNAT to activate their hop service.
|
|
33
|
+
// This is a role declaration, not a verified Internet reachability claim.
|
|
34
|
+
Libp2pForceReachability: pivot ? "public" : "private",
|
|
35
|
+
NonPublicAddrPublishing: true,
|
|
36
|
+
},
|
|
37
|
+
Discovery: { MDNS: { Enabled: false } },
|
|
38
|
+
Swarm: {
|
|
39
|
+
...swarm,
|
|
40
|
+
EnableHolePunching: true,
|
|
41
|
+
RelayClient: {
|
|
42
|
+
Enabled: true,
|
|
43
|
+
// libp2p counts static entries before grouping candidates by PeerID.
|
|
44
|
+
// One entry per ID avoids a three-minute wait for duplicate candidates;
|
|
45
|
+
// Bootstrap supplies the complete address set to the same peerstore.
|
|
46
|
+
StaticRelays: peers
|
|
47
|
+
.flatMap(({ ID, Addrs }) =>
|
|
48
|
+
Addrs[0] ? [`${Addrs[0]}/p2p/${ID}`] : [],
|
|
49
|
+
)
|
|
50
|
+
.sort(),
|
|
51
|
+
},
|
|
52
|
+
RelayService: {
|
|
53
|
+
...object(swarm.RelayService ?? {}, "Kubo relay service"),
|
|
54
|
+
Enabled: pivot,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface PivotCandidate {
|
|
61
|
+
addresses: string[];
|
|
62
|
+
failures: number;
|
|
63
|
+
retryAt: number;
|
|
64
|
+
probe: "success" | "failure" | "unknown";
|
|
65
|
+
probedAt: number | null;
|
|
66
|
+
error: string | null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export class PivotCandidates {
|
|
70
|
+
private readonly candidates = new Map<string, PivotCandidate>();
|
|
71
|
+
|
|
72
|
+
reconcile(profiles: ReadonlyMap<string, readonly string[]>) {
|
|
73
|
+
for (const id of this.candidates.keys())
|
|
74
|
+
if (!profiles.has(id)) this.candidates.delete(id);
|
|
75
|
+
for (const [id, values] of profiles) {
|
|
76
|
+
const addresses = [...new Set(values)].sort();
|
|
77
|
+
const current = this.candidates.get(id);
|
|
78
|
+
if (JSON.stringify(current?.addresses) !== JSON.stringify(addresses))
|
|
79
|
+
this.candidates.set(id, {
|
|
80
|
+
addresses,
|
|
81
|
+
failures: 0,
|
|
82
|
+
retryAt: 0,
|
|
83
|
+
probe: "unknown",
|
|
84
|
+
probedAt: null,
|
|
85
|
+
error: null,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
known(id: string): readonly string[] | undefined {
|
|
91
|
+
return this.candidates.get(id)?.addresses;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
observations(): Map<string, PivotCandidate> {
|
|
95
|
+
return new Map(
|
|
96
|
+
[...this.candidates].map(([id, value]) => [
|
|
97
|
+
id,
|
|
98
|
+
{ ...value, addresses: [...value.addresses] },
|
|
99
|
+
]),
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
active(now = Date.now()): Map<string, readonly string[]> {
|
|
104
|
+
return new Map(
|
|
105
|
+
[...this.candidates]
|
|
106
|
+
.filter(([, value]) => value.addresses.length && value.retryAt <= now)
|
|
107
|
+
.map(([id, value]) => [id, value.addresses]),
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
result(
|
|
112
|
+
id: string,
|
|
113
|
+
healthy: boolean,
|
|
114
|
+
now = Date.now(),
|
|
115
|
+
random = Math.random(),
|
|
116
|
+
error: string | null = null,
|
|
117
|
+
) {
|
|
118
|
+
const candidate = this.candidates.get(id);
|
|
119
|
+
if (!candidate) return;
|
|
120
|
+
candidate.probe = healthy ? "success" : "failure";
|
|
121
|
+
candidate.probedAt = now;
|
|
122
|
+
candidate.error = error;
|
|
123
|
+
if (healthy) {
|
|
124
|
+
candidate.failures = 0;
|
|
125
|
+
candidate.retryAt = 0;
|
|
126
|
+
} else {
|
|
127
|
+
candidate.failures++;
|
|
128
|
+
if (candidate.failures >= 3)
|
|
129
|
+
candidate.retryAt =
|
|
130
|
+
now +
|
|
131
|
+
Math.min(900_000, 60_000 * 2 ** Math.min(candidate.failures - 3, 4)) *
|
|
132
|
+
(0.8 + random * 0.4);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
package/src/kubo.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { execFile, spawn } from "node:child_process";
|
|
2
|
-
import { promisify } from "node:util";
|
|
3
|
-
import {
|
|
2
|
+
import { isDeepStrictEqual, promisify } from "node:util";
|
|
3
|
+
import {
|
|
4
|
+
createHash,
|
|
5
|
+
createPrivateKey,
|
|
6
|
+
createPublicKey,
|
|
7
|
+
sign,
|
|
8
|
+
} from "node:crypto";
|
|
4
9
|
import { lstat, open, readFile, writeFile } from "node:fs/promises";
|
|
5
10
|
import { join } from "node:path";
|
|
6
11
|
import { createServer } from "node:net";
|
|
@@ -20,12 +25,8 @@ import {
|
|
|
20
25
|
} from "./swarm-files.js";
|
|
21
26
|
import { HostError, object } from "./errors.js";
|
|
22
27
|
import { nodeError } from "./authoring.js";
|
|
23
|
-
import {
|
|
24
|
-
|
|
25
|
-
persistPeers,
|
|
26
|
-
reconcilePeers,
|
|
27
|
-
type PeeringPeer,
|
|
28
|
-
} from "./kubo-peering.js";
|
|
28
|
+
import { peerArguments, type PeeringPeer } from "./kubo-peering.js";
|
|
29
|
+
import { topologyConfig } from "./kubo-topology.js";
|
|
29
30
|
|
|
30
31
|
import { kuboBinary, withKuboProgram } from "./kubo-install.js";
|
|
31
32
|
export { kuboBinary };
|
|
@@ -50,6 +51,7 @@ export interface NodeRecord {
|
|
|
50
51
|
epoch?: number;
|
|
51
52
|
pid?: number;
|
|
52
53
|
listen?: ListenMode;
|
|
54
|
+
topology?: string;
|
|
53
55
|
}
|
|
54
56
|
export interface KuboNode {
|
|
55
57
|
repo: string;
|
|
@@ -59,10 +61,12 @@ export interface KuboNode {
|
|
|
59
61
|
addresses: string[];
|
|
60
62
|
auth: string;
|
|
61
63
|
pid?: number;
|
|
64
|
+
topology?: string;
|
|
62
65
|
}
|
|
63
66
|
export function nodeRecord(value: unknown, scope: string): NodeRecord {
|
|
64
67
|
const record = object(value, "protected managed-node record");
|
|
65
|
-
const { peerId, api, gateway, swarm, auth, pid, epoch, listen } =
|
|
68
|
+
const { peerId, api, gateway, swarm, auth, pid, epoch, listen, topology } =
|
|
69
|
+
record;
|
|
66
70
|
if (
|
|
67
71
|
Object.keys(record).some(
|
|
68
72
|
(key) =>
|
|
@@ -76,6 +80,7 @@ export function nodeRecord(value: unknown, scope: string): NodeRecord {
|
|
|
76
80
|
"pid",
|
|
77
81
|
"epoch",
|
|
78
82
|
"listen",
|
|
83
|
+
"topology",
|
|
79
84
|
].includes(key),
|
|
80
85
|
) ||
|
|
81
86
|
record.scope !== scope ||
|
|
@@ -96,7 +101,9 @@ export function nodeRecord(value: unknown, scope: string): NodeRecord {
|
|
|
96
101
|
(typeof epoch !== "number" ||
|
|
97
102
|
!Number.isSafeInteger(epoch) ||
|
|
98
103
|
epoch < 1)) ||
|
|
99
|
-
(listen !== undefined && listen !== "lan" && listen !== "loopback")
|
|
104
|
+
(listen !== undefined && listen !== "lan" && listen !== "loopback") ||
|
|
105
|
+
(topology !== undefined &&
|
|
106
|
+
(typeof topology !== "string" || !/^[a-f0-9]{64}$/.test(topology)))
|
|
100
107
|
)
|
|
101
108
|
throw new HostError(
|
|
102
109
|
"Invalid protected managed-node record; old node formats are unsupported.",
|
|
@@ -111,6 +118,7 @@ export function nodeRecord(value: unknown, scope: string): NodeRecord {
|
|
|
111
118
|
...(pid !== undefined ? { pid } : {}),
|
|
112
119
|
...(epoch !== undefined ? { epoch } : {}),
|
|
113
120
|
...(listen !== undefined ? { listen } : {}),
|
|
121
|
+
...(topology !== undefined ? { topology } : {}),
|
|
114
122
|
};
|
|
115
123
|
}
|
|
116
124
|
async function freePort(): Promise<number> {
|
|
@@ -158,7 +166,7 @@ export async function checkCachedNode(
|
|
|
158
166
|
const node = nodeRecord(saved, scope);
|
|
159
167
|
if (node.peerId !== peer || (node.epoch !== undefined && node.epoch > epoch))
|
|
160
168
|
throw new HostError(
|
|
161
|
-
"Cached membership does not match the recorded node identity or epoch; retry with --
|
|
169
|
+
"Cached membership does not match the recorded node identity or epoch; retry with --refresh.",
|
|
162
170
|
);
|
|
163
171
|
}
|
|
164
172
|
async function prepareWithBinary(
|
|
@@ -405,6 +413,7 @@ export async function startNode(
|
|
|
405
413
|
membership?: {
|
|
406
414
|
self: string;
|
|
407
415
|
peers: readonly PeeringPeer[];
|
|
416
|
+
pivot?: boolean;
|
|
408
417
|
signal?: AbortSignal;
|
|
409
418
|
},
|
|
410
419
|
): Promise<KuboNode> {
|
|
@@ -436,9 +445,17 @@ export async function startNode(
|
|
|
436
445
|
throw new HostError(
|
|
437
446
|
"Refusing to restore an older Swarm epoch; refresh membership.",
|
|
438
447
|
);
|
|
439
|
-
|
|
448
|
+
const invalidateTopology = async () => {
|
|
449
|
+
if (record.topology !== undefined) {
|
|
450
|
+
delete record.topology;
|
|
451
|
+
await writePrivate(directory, "node.json", record);
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
if (record.epoch !== undefined && record.epoch !== epoch) {
|
|
455
|
+
await invalidateTopology();
|
|
440
456
|
await stopNode(node);
|
|
441
|
-
|
|
457
|
+
}
|
|
458
|
+
let online = await ready(node, signal);
|
|
442
459
|
if (!online && record.pid !== undefined) {
|
|
443
460
|
try {
|
|
444
461
|
process.kill(record.pid, 0);
|
|
@@ -449,13 +466,27 @@ export async function startNode(
|
|
|
449
466
|
if (!nodeError(cause, "ESRCH")) throw cause;
|
|
450
467
|
}
|
|
451
468
|
}
|
|
452
|
-
const call = (command: string, budget: AbortSignal) =>
|
|
453
|
-
rpc(
|
|
454
|
-
node,
|
|
455
|
-
command,
|
|
456
|
-
signal ? AbortSignal.any([signal, budget]) : budget,
|
|
457
|
-
);
|
|
458
469
|
const desired = membership?.peers ?? [];
|
|
470
|
+
const config = await readPrivate<Record<string, unknown>>(
|
|
471
|
+
node.repo,
|
|
472
|
+
"config",
|
|
473
|
+
);
|
|
474
|
+
if (!config)
|
|
475
|
+
throw new HostError("Missing protected Kubo configuration.");
|
|
476
|
+
const nextConfig = topologyConfig(
|
|
477
|
+
config,
|
|
478
|
+
membership?.pivot ?? false,
|
|
479
|
+
desired,
|
|
480
|
+
listen === "loopback",
|
|
481
|
+
);
|
|
482
|
+
if (!isDeepStrictEqual(config, nextConfig)) {
|
|
483
|
+
await invalidateTopology();
|
|
484
|
+
if (online) {
|
|
485
|
+
await stopNode(node);
|
|
486
|
+
online = false;
|
|
487
|
+
}
|
|
488
|
+
await writePrivate(node.repo, "config", nextConfig);
|
|
489
|
+
}
|
|
459
490
|
const keyPath = join(node.repo, "swarm.key");
|
|
460
491
|
if (record.epoch === epoch) {
|
|
461
492
|
await protectedPath(keyPath);
|
|
@@ -467,7 +498,6 @@ export async function startNode(
|
|
|
467
498
|
"Swarm key file does not match approved epoch.",
|
|
468
499
|
);
|
|
469
500
|
} else {
|
|
470
|
-
await persistPeers(node.repo, desired, online ? call : undefined);
|
|
471
501
|
await writeFile(
|
|
472
502
|
keyPath,
|
|
473
503
|
`/key/swarm/psk/1.0.0/\n/base16/\n${key}\n`,
|
|
@@ -477,10 +507,25 @@ export async function startNode(
|
|
|
477
507
|
);
|
|
478
508
|
await secureNew(keyPath);
|
|
479
509
|
}
|
|
480
|
-
if (record.epoch === epoch)
|
|
481
|
-
await persistPeers(node.repo, desired, online ? call : undefined);
|
|
482
510
|
if (!online) {
|
|
483
511
|
signal?.throwIfAborted();
|
|
512
|
+
// Kubo's bootstrap fallback is persisted independently of config.
|
|
513
|
+
// Clear only its version-pinned backup key while the owned repo is
|
|
514
|
+
// offline, so removed candidates cannot reappear after a restart.
|
|
515
|
+
await run(
|
|
516
|
+
binary,
|
|
517
|
+
["diag", "datastore", "put", "/local/temp_bootstrap_peers", "[]"],
|
|
518
|
+
{
|
|
519
|
+
env: {
|
|
520
|
+
...process.env,
|
|
521
|
+
IPFS_PATH: node.repo,
|
|
522
|
+
LIBP2P_FORCE_PNET: "1",
|
|
523
|
+
},
|
|
524
|
+
signal,
|
|
525
|
+
timeout: 30_000,
|
|
526
|
+
windowsHide: true,
|
|
527
|
+
},
|
|
528
|
+
);
|
|
484
529
|
const logfile = join(directory, "daemon.log");
|
|
485
530
|
const log = await open(logfile, "a", 0o600);
|
|
486
531
|
await secureNew(logfile);
|
|
@@ -538,11 +583,24 @@ export async function startNode(
|
|
|
538
583
|
throw cause;
|
|
539
584
|
}
|
|
540
585
|
}
|
|
541
|
-
|
|
586
|
+
const topology = membership
|
|
587
|
+
? createHash("sha256")
|
|
588
|
+
.update(
|
|
589
|
+
JSON.stringify({
|
|
590
|
+
self: membership.self,
|
|
591
|
+
pivot: membership.pivot ?? false,
|
|
592
|
+
peers: membership.peers,
|
|
593
|
+
}),
|
|
594
|
+
)
|
|
595
|
+
.digest("hex")
|
|
596
|
+
: undefined;
|
|
597
|
+
if (record.epoch !== epoch || record.topology !== topology) {
|
|
542
598
|
record.epoch = epoch;
|
|
599
|
+
if (topology) record.topology = topology;
|
|
600
|
+
else delete record.topology;
|
|
543
601
|
await writePrivate(directory, "node.json", record);
|
|
544
602
|
}
|
|
545
|
-
|
|
603
|
+
if (topology) node.topology = topology;
|
|
546
604
|
return node;
|
|
547
605
|
};
|
|
548
606
|
return withPrivateLock(directory, start, signal);
|
|
@@ -562,9 +620,60 @@ export function validateAddress(address: string, expectedPeer?: string): void {
|
|
|
562
620
|
(expectedPeer && match[3] !== expectedPeer)
|
|
563
621
|
)
|
|
564
622
|
throw new HostError(
|
|
565
|
-
"
|
|
623
|
+
"Pivot profile requires IPv4 TCP multiaddr bound to its PeerID.",
|
|
566
624
|
);
|
|
567
625
|
}
|
|
626
|
+
export async function probePeer(
|
|
627
|
+
node: KuboNode,
|
|
628
|
+
id: string,
|
|
629
|
+
addresses: readonly string[],
|
|
630
|
+
signal?: AbortSignal,
|
|
631
|
+
): Promise<boolean> {
|
|
632
|
+
for (const address of addresses) validateAddress(address, id);
|
|
633
|
+
if (!addresses.length) return false;
|
|
634
|
+
const response = await rpc(
|
|
635
|
+
node,
|
|
636
|
+
`swarm/connect?${addresses.map((address) => `arg=${encodeURIComponent(address)}`).join("&")}`,
|
|
637
|
+
signal,
|
|
638
|
+
8_000,
|
|
639
|
+
);
|
|
640
|
+
const body = await boundedText(response);
|
|
641
|
+
return response.ok && !body.includes('"Error"');
|
|
642
|
+
}
|
|
643
|
+
export interface PeerConnection {
|
|
644
|
+
peerId: string;
|
|
645
|
+
address: string;
|
|
646
|
+
transport: "direct" | "relay";
|
|
647
|
+
}
|
|
648
|
+
export async function nodeConnections(
|
|
649
|
+
node: KuboNode,
|
|
650
|
+
signal?: AbortSignal,
|
|
651
|
+
): Promise<PeerConnection[]> {
|
|
652
|
+
if (!(await ready(node, signal)))
|
|
653
|
+
throw new HostError("Owned Kubo API is unavailable.");
|
|
654
|
+
const response = await rpc(node, "swarm/peers", signal);
|
|
655
|
+
if (!response.ok) {
|
|
656
|
+
await response.body?.cancel();
|
|
657
|
+
throw new HostError(`Owned Kubo connections: HTTP ${response.status}`);
|
|
658
|
+
}
|
|
659
|
+
const data = object(
|
|
660
|
+
JSON.parse(await boundedText(response)) as unknown,
|
|
661
|
+
"owned Kubo connections",
|
|
662
|
+
);
|
|
663
|
+
if (data.Peers === null) return [];
|
|
664
|
+
if (!Array.isArray(data.Peers) || data.Peers.length > 4096)
|
|
665
|
+
throw new HostError("Invalid owned Kubo connections.");
|
|
666
|
+
return data.Peers.map((entry: unknown) => {
|
|
667
|
+
const { Peer, Addr } = object(entry, "owned Kubo connection");
|
|
668
|
+
if (typeof Peer !== "string" || typeof Addr !== "string")
|
|
669
|
+
throw new HostError("Invalid owned Kubo connection.");
|
|
670
|
+
return {
|
|
671
|
+
peerId: Peer,
|
|
672
|
+
address: Addr,
|
|
673
|
+
transport: Addr.includes("/p2p-circuit") ? "relay" : "direct",
|
|
674
|
+
};
|
|
675
|
+
});
|
|
676
|
+
}
|
|
568
677
|
export function kuboSource(
|
|
569
678
|
node: KuboNode,
|
|
570
679
|
addresses: readonly string[] = [],
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { HostError } from "./host-error.js";
|
|
2
|
+
|
|
3
|
+
export const CHUNK_BYTES = 64 * 1024;
|
|
4
|
+
|
|
5
|
+
export function relativePath(path: string): string {
|
|
6
|
+
if (
|
|
7
|
+
path.length > 1024 ||
|
|
8
|
+
!path ||
|
|
9
|
+
path.includes("\\") ||
|
|
10
|
+
path.includes("%") ||
|
|
11
|
+
[...path].some(
|
|
12
|
+
(char) =>
|
|
13
|
+
char.charCodeAt(0) < 32 ||
|
|
14
|
+
char.charCodeAt(0) === 127 ||
|
|
15
|
+
':*?"<>|'.includes(char),
|
|
16
|
+
) ||
|
|
17
|
+
path
|
|
18
|
+
.split("/")
|
|
19
|
+
.some(
|
|
20
|
+
(part) => !part || part === "." || part === ".." || /[. ]$/.test(part),
|
|
21
|
+
)
|
|
22
|
+
)
|
|
23
|
+
throw new HostError(
|
|
24
|
+
"Unsafe Space path; use an unambiguous directory-relative / path.",
|
|
25
|
+
1,
|
|
26
|
+
"PATH_INVALID",
|
|
27
|
+
"execution",
|
|
28
|
+
);
|
|
29
|
+
return path;
|
|
30
|
+
}
|
package/src/space.ts
CHANGED
|
@@ -12,37 +12,11 @@ import { ReadableStream as NodeReadableStream } from "node:stream/web";
|
|
|
12
12
|
import { appDescription, appSchemaCid } from "./app-definition.js";
|
|
13
13
|
import { readSupportedSchemas } from "./supported-schemas.js";
|
|
14
14
|
export { appSchema, appSchemaCid } from "./app-definition.js";
|
|
15
|
-
|
|
15
|
+
import { CHUNK_BYTES, relativePath } from "./space-path.js";
|
|
16
|
+
export { CHUNK_BYTES, relativePath } from "./space-path.js";
|
|
16
17
|
export const OUTPUT_BYTES = 64 * 1024 * 1024;
|
|
17
18
|
export const MAX_PATHS = 4096;
|
|
18
19
|
|
|
19
|
-
export function relativePath(path: string): string {
|
|
20
|
-
if (
|
|
21
|
-
path.length > 1024 ||
|
|
22
|
-
!path ||
|
|
23
|
-
path.includes("\\") ||
|
|
24
|
-
path.includes("%") ||
|
|
25
|
-
[...path].some(
|
|
26
|
-
(char) =>
|
|
27
|
-
char.charCodeAt(0) < 32 ||
|
|
28
|
-
char.charCodeAt(0) === 127 ||
|
|
29
|
-
':*?"<>|'.includes(char),
|
|
30
|
-
) ||
|
|
31
|
-
path
|
|
32
|
-
.split("/")
|
|
33
|
-
.some(
|
|
34
|
-
(part) => !part || part === "." || part === ".." || /[. ]$/.test(part),
|
|
35
|
-
)
|
|
36
|
-
)
|
|
37
|
-
throw new HostError(
|
|
38
|
-
"Unsafe Space path; use an unambiguous directory-relative / path.",
|
|
39
|
-
1,
|
|
40
|
-
"PATH_INVALID",
|
|
41
|
-
"execution",
|
|
42
|
-
);
|
|
43
|
-
return path;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
20
|
export async function bytes(
|
|
47
21
|
stream: ReadableStream<Uint8Array> | NodeReadableStream<Uint8Array>,
|
|
48
22
|
limit: number,
|
package/src/state-storage.ts
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
} from "node:fs";
|
|
14
14
|
import { homedir, tmpdir } from "node:os";
|
|
15
15
|
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
16
|
-
import { HostError } from "./
|
|
16
|
+
import { HostError } from "./host-error.js";
|
|
17
17
|
import { windowsStateAccess } from "./state-storage-windows.js";
|
|
18
18
|
|
|
19
19
|
// Written only after the existing layout/owner checks, before the first DB
|