@ipab/cli 0.1.0-rc.1 → 0.1.0-rc.11
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 +234 -9
- package/dist/app-authoring.d.ts +15 -0
- package/dist/app-authoring.d.ts.map +1 -1
- package/dist/app-authoring.js +144 -58
- package/dist/app-authoring.js.map +1 -1
- package/dist/app-definition.d.ts +52 -57
- package/dist/app-definition.d.ts.map +1 -1
- package/dist/app-definition.js +33 -38
- package/dist/app-definition.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/app-templates.d.ts +0 -1
- package/dist/app-templates.d.ts.map +1 -1
- package/dist/app-templates.js +8 -46
- package/dist/app-templates.js.map +1 -1
- package/dist/assembly.d.ts +7 -0
- package/dist/assembly.d.ts.map +1 -0
- package/dist/assembly.js +130 -0
- package/dist/assembly.js.map +1 -0
- package/dist/authoring.d.ts +7 -8
- package/dist/authoring.d.ts.map +1 -1
- package/dist/authoring.js +59 -16
- package/dist/authoring.js.map +1 -1
- package/dist/builtins/skill.ipab.car +0 -0
- package/dist/builtins/template.bundle-viewer.car +0 -0
- package/dist/builtins/template.check.car +0 -0
- package/dist/builtins/template.node.car +0 -0
- package/dist/builtins/template.web-app.car +0 -0
- package/dist/builtins.d.ts.map +1 -1
- package/dist/builtins.js +1 -23
- package/dist/builtins.js.map +1 -1
- package/dist/bundled-skills.d.ts +14 -0
- package/dist/bundled-skills.d.ts.map +1 -0
- package/dist/bundled-skills.js +34 -0
- package/dist/bundled-skills.js.map +1 -0
- package/dist/cli.d.ts +8 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +1967 -258
- package/dist/cli.js.map +1 -1
- package/dist/config-secret.d.ts +3 -0
- package/dist/config-secret.d.ts.map +1 -0
- package/dist/config-secret.js +12 -0
- package/dist/config-secret.js.map +1 -0
- package/dist/config.d.ts +11 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +166 -8
- package/dist/config.js.map +1 -1
- package/dist/content-provider.d.ts +32 -0
- package/dist/content-provider.d.ts.map +1 -0
- package/dist/content-provider.js +222 -0
- package/dist/content-provider.js.map +1 -0
- package/dist/context-types.d.ts +12 -0
- package/dist/context-types.d.ts.map +1 -0
- package/dist/context-types.js +2 -0
- package/dist/context-types.js.map +1 -0
- package/dist/context.d.ts +26 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +54 -10
- package/dist/context.js.map +1 -1
- package/dist/daemon-ui.d.ts +93 -0
- package/dist/daemon-ui.d.ts.map +1 -0
- package/dist/daemon-ui.js +713 -0
- package/dist/daemon-ui.js.map +1 -0
- package/dist/daemon.d.ts +21 -0
- package/dist/daemon.d.ts.map +1 -0
- package/dist/daemon.js +317 -0
- package/dist/daemon.js.map +1 -0
- package/dist/doctor.d.ts +32 -0
- package/dist/doctor.d.ts.map +1 -0
- package/dist/doctor.js +688 -0
- package/dist/doctor.js.map +1 -0
- 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/get.d.ts +3 -0
- package/dist/get.d.ts.map +1 -0
- package/dist/get.js +80 -0
- package/dist/get.js.map +1 -0
- package/dist/history.d.ts.map +1 -1
- package/dist/history.js +6 -14
- package/dist/history.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/identity.d.ts +16 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +119 -0
- package/dist/identity.js.map +1 -0
- package/dist/ipc.d.ts +5 -2
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +124 -12
- package/dist/ipc.js.map +1 -1
- package/dist/kubo-install.d.ts +26 -0
- package/dist/kubo-install.d.ts.map +1 -0
- package/dist/kubo-install.js +724 -0
- package/dist/kubo-install.js.map +1 -0
- package/dist/kubo-peering.d.ts +11 -0
- package/dist/kubo-peering.d.ts.map +1 -0
- package/dist/kubo-peering.js +93 -0
- package/dist/kubo-peering.js.map +1 -0
- package/dist/kubo-peers.d.ts +12 -0
- package/dist/kubo-peers.d.ts.map +1 -0
- package/dist/kubo-peers.js +119 -0
- package/dist/kubo-peers.js.map +1 -0
- package/dist/kubo-release.d.ts +22 -0
- package/dist/kubo-release.d.ts.map +1 -0
- package/dist/kubo-release.js +176 -0
- package/dist/kubo-release.js.map +1 -0
- package/dist/kubo-topology.d.ts +19 -0
- package/dist/kubo-topology.d.ts.map +1 -0
- package/dist/kubo-topology.js +113 -0
- package/dist/kubo-topology.js.map +1 -0
- package/dist/kubo.d.ts +85 -0
- package/dist/kubo.d.ts.map +1 -0
- package/dist/kubo.js +755 -0
- package/dist/kubo.js.map +1 -0
- package/dist/pins.d.ts +16 -18
- package/dist/pins.d.ts.map +1 -1
- package/dist/pins.js +144 -106
- package/dist/pins.js.map +1 -1
- package/dist/pivot-rpc.d.ts +62 -0
- package/dist/pivot-rpc.d.ts.map +1 -0
- package/dist/pivot-rpc.js +407 -0
- package/dist/pivot-rpc.js.map +1 -0
- package/dist/pivot-service.d.ts +16 -0
- package/dist/pivot-service.d.ts.map +1 -0
- package/dist/pivot-service.js +50 -0
- package/dist/pivot-service.js.map +1 -0
- package/dist/publish-source.d.ts +3 -0
- package/dist/publish-source.d.ts.map +1 -0
- package/dist/publish-source.js +85 -0
- package/dist/publish-source.js.map +1 -0
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +9 -4
- package/dist/runtime.js.map +1 -1
- package/dist/search-index.d.ts +64 -0
- package/dist/search-index.d.ts.map +1 -0
- package/dist/search-index.js +409 -0
- package/dist/search-index.js.map +1 -0
- package/dist/search-service.d.ts +23 -0
- package/dist/search-service.d.ts.map +1 -0
- package/dist/search-service.js +116 -0
- package/dist/search-service.js.map +1 -0
- package/dist/skill-definition.d.ts +6 -0
- package/dist/skill-definition.d.ts.map +1 -0
- package/dist/skill-definition.js +38 -0
- package/dist/skill-definition.js.map +1 -0
- package/dist/skill-source.d.ts +9 -0
- package/dist/skill-source.d.ts.map +1 -0
- package/dist/skill-source.js +24 -0
- package/dist/skill-source.js.map +1 -0
- package/dist/skills.d.ts +104 -0
- package/dist/skills.d.ts.map +1 -0
- package/dist/skills.js +894 -0
- package/dist/skills.js.map +1 -0
- package/dist/source-bundle.d.ts +3 -8
- package/dist/source-bundle.d.ts.map +1 -1
- package/dist/source-bundle.js +17 -12
- package/dist/source-bundle.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 +16 -4
- package/dist/space.d.ts.map +1 -1
- package/dist/space.js +170 -24
- package/dist/space.js.map +1 -1
- package/dist/state-storage-windows.d.ts +2 -0
- package/dist/state-storage-windows.d.ts.map +1 -0
- package/dist/state-storage-windows.js +114 -0
- package/dist/state-storage-windows.js.map +1 -0
- package/dist/state-storage.d.ts +5 -0
- package/dist/state-storage.d.ts.map +1 -0
- package/dist/state-storage.js +333 -0
- package/dist/state-storage.js.map +1 -0
- package/dist/state.d.ts +135 -13
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +577 -92
- package/dist/state.js.map +1 -1
- package/dist/swarm-envelope.d.ts +57 -0
- package/dist/swarm-envelope.d.ts.map +1 -0
- package/dist/swarm-envelope.js +299 -0
- package/dist/swarm-envelope.js.map +1 -0
- package/dist/swarm-files.d.ts +6 -0
- package/dist/swarm-files.d.ts.map +1 -0
- package/dist/swarm-files.js +116 -0
- package/dist/swarm-files.js.map +1 -0
- package/dist/swarm-meta.d.ts +15 -0
- package/dist/swarm-meta.d.ts.map +1 -0
- package/dist/swarm-meta.js +68 -0
- package/dist/swarm-meta.js.map +1 -0
- package/dist/swarm-observations.d.ts +40 -0
- package/dist/swarm-observations.d.ts.map +1 -0
- package/dist/swarm-observations.js +271 -0
- package/dist/swarm-observations.js.map +1 -0
- package/dist/swarm.d.ts +537 -0
- package/dist/swarm.d.ts.map +1 -0
- package/dist/swarm.js +4991 -0
- package/dist/swarm.js.map +1 -0
- package/dist/template-definition.d.ts +47 -52
- package/dist/template-definition.d.ts.map +1 -1
- package/dist/template-definition.js +36 -41
- package/dist/template-definition.js.map +1 -1
- package/dist/template-paths.d.ts +7 -0
- package/dist/template-paths.d.ts.map +1 -0
- package/dist/template-paths.js +23 -0
- package/dist/template-paths.js.map +1 -0
- package/dist/template-presentation.d.ts +20 -0
- package/dist/template-presentation.d.ts.map +1 -0
- package/dist/template-presentation.js +133 -0
- package/dist/template-presentation.js.map +1 -0
- package/dist/template-source.d.ts +4 -1
- package/dist/template-source.d.ts.map +1 -1
- package/dist/template-source.js +11 -4
- package/dist/template-source.js.map +1 -1
- package/dist/templates.d.ts +11 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/templates.js +53 -0
- package/dist/templates.js.map +1 -0
- package/dist/unixfs-tree.d.ts +67 -0
- package/dist/unixfs-tree.d.ts.map +1 -0
- package/dist/unixfs-tree.js +329 -0
- package/dist/unixfs-tree.js.map +1 -0
- package/package.json +14 -7
- package/src/app-authoring.ts +240 -76
- package/src/app-definition.ts +33 -39
- package/src/app-prebuild.ts +61 -0
- package/src/app-templates.ts +17 -73
- package/src/assembly.ts +185 -0
- package/src/authoring.ts +99 -18
- package/src/builtins.ts +2 -27
- package/src/bundled-skills.ts +60 -0
- package/src/cli.ts +2461 -323
- package/src/config-secret.ts +14 -0
- package/src/config.ts +214 -8
- package/src/content-provider.ts +299 -0
- package/src/context-types.ts +11 -0
- package/src/context.ts +98 -10
- package/src/daemon-ui.ts +812 -0
- package/src/daemon.ts +386 -0
- package/src/doctor.ts +1092 -0
- package/src/errors.ts +1 -25
- package/src/get.ts +91 -0
- package/src/history.ts +5 -15
- package/src/host-error.ts +26 -0
- package/src/identity.ts +175 -0
- package/src/ipc.ts +160 -11
- package/src/kubo-install.ts +936 -0
- package/src/kubo-peering.ts +126 -0
- package/src/kubo-peers.ts +144 -0
- package/src/kubo-release.ts +230 -0
- package/src/kubo-topology.ts +148 -0
- package/src/kubo.ts +1088 -0
- package/src/pins.ts +188 -126
- package/src/pivot-rpc.ts +601 -0
- package/src/pivot-service.ts +87 -0
- package/src/publish-source.ts +103 -0
- package/src/runtime.ts +16 -4
- package/src/search-index.ts +560 -0
- package/src/search-service.ts +152 -0
- package/src/skill-definition.ts +53 -0
- package/src/skill-source.ts +29 -0
- package/src/skills.ts +1236 -0
- package/src/source-bundle.ts +26 -13
- package/src/space-path.ts +30 -0
- package/src/space.ts +230 -42
- package/src/state-storage-windows.ts +134 -0
- package/src/state-storage.ts +378 -0
- package/src/state.ts +875 -108
- package/src/swarm-envelope.ts +461 -0
- package/src/swarm-files.ts +126 -0
- package/src/swarm-meta.ts +98 -0
- package/src/swarm-observations.ts +393 -0
- package/src/swarm.ts +7392 -0
- package/src/template-definition.ts +36 -41
- package/src/template-paths.ts +43 -0
- package/src/template-presentation.ts +151 -0
- package/src/template-source.ts +19 -5
- package/src/templates.ts +77 -0
- package/src/unixfs-tree.ts +401 -0
|
@@ -0,0 +1,936 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
+
import { constants } from "node:fs";
|
|
4
|
+
import {
|
|
5
|
+
access,
|
|
6
|
+
chmod,
|
|
7
|
+
link,
|
|
8
|
+
lstat,
|
|
9
|
+
mkdir,
|
|
10
|
+
open,
|
|
11
|
+
readdir,
|
|
12
|
+
rename,
|
|
13
|
+
rm,
|
|
14
|
+
} from "node:fs/promises";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
import { promisify } from "node:util";
|
|
17
|
+
import { nodeError } from "./authoring.js";
|
|
18
|
+
import { HostError } from "./errors.js";
|
|
19
|
+
import { protectedPath, secureNew } from "./identity.js";
|
|
20
|
+
import {
|
|
21
|
+
assetName,
|
|
22
|
+
compatibleVersion,
|
|
23
|
+
compareKuboVersions,
|
|
24
|
+
enumeratedRelease,
|
|
25
|
+
latestKuboRelease,
|
|
26
|
+
officialDownloadUrl,
|
|
27
|
+
KUBO_REPO_VERSION,
|
|
28
|
+
KUBO_VERSION,
|
|
29
|
+
releaseDigests,
|
|
30
|
+
type KuboArchive,
|
|
31
|
+
type KuboRelease,
|
|
32
|
+
} from "./kubo-release.js";
|
|
33
|
+
import { requireAccountOnlyMany } from "@ipab/bundle/pointer/protected-files";
|
|
34
|
+
import {
|
|
35
|
+
privateDirectory,
|
|
36
|
+
readPrivate,
|
|
37
|
+
withPrivateLock,
|
|
38
|
+
writePrivate,
|
|
39
|
+
} from "./swarm-files.js";
|
|
40
|
+
|
|
41
|
+
const run = promisify(execFile);
|
|
42
|
+
const MAX_ARCHIVE = 150 * 1024 * 1024;
|
|
43
|
+
const journal = "installing.json";
|
|
44
|
+
const record = "installation.json";
|
|
45
|
+
interface Installation {
|
|
46
|
+
version: string;
|
|
47
|
+
hash: string;
|
|
48
|
+
archiveDigest?: string;
|
|
49
|
+
provenance?: boolean;
|
|
50
|
+
}
|
|
51
|
+
export interface InstallOptions {
|
|
52
|
+
archive?: string;
|
|
53
|
+
diagnostic?: (line: string, progress?: boolean) => void;
|
|
54
|
+
signal?: AbortSignal;
|
|
55
|
+
release?: KuboRelease;
|
|
56
|
+
upgrade?: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function failure(cause: unknown): string {
|
|
60
|
+
if (cause instanceof HostError) return cause.message;
|
|
61
|
+
const queue = [cause];
|
|
62
|
+
const codes = new Set<string>();
|
|
63
|
+
for (let i = 0; i < queue.length && i < 8; i++) {
|
|
64
|
+
const error = queue[i];
|
|
65
|
+
if (!(error instanceof Error)) continue;
|
|
66
|
+
if (
|
|
67
|
+
"code" in error &&
|
|
68
|
+
typeof error.code === "string" &&
|
|
69
|
+
/^[A-Z][A-Z0-9_]{1,39}$/.test(error.code)
|
|
70
|
+
)
|
|
71
|
+
codes.add(error.code);
|
|
72
|
+
if (
|
|
73
|
+
"cmd" in error &&
|
|
74
|
+
"code" in error &&
|
|
75
|
+
typeof error.code === "number" &&
|
|
76
|
+
Number.isSafeInteger(error.code)
|
|
77
|
+
)
|
|
78
|
+
codes.add(`process exit ${error.code}`);
|
|
79
|
+
if (error.name === "TimeoutError" || error.name === "AbortError")
|
|
80
|
+
codes.add(error.name);
|
|
81
|
+
if (error.cause) queue.push(error.cause);
|
|
82
|
+
if (error instanceof AggregateError)
|
|
83
|
+
queue.push(...(error.errors as unknown[]).slice(0, 4));
|
|
84
|
+
}
|
|
85
|
+
if (codes.size) return [...codes].join(", ");
|
|
86
|
+
if (cause instanceof SyntaxError) return "invalid data";
|
|
87
|
+
if (
|
|
88
|
+
cause instanceof Error &&
|
|
89
|
+
/permission|account.only|current (?:OS )?account/i.test(cause.message)
|
|
90
|
+
)
|
|
91
|
+
return "permission denied or unsafe ownership";
|
|
92
|
+
return "operation failed";
|
|
93
|
+
}
|
|
94
|
+
export function setupFailure(
|
|
95
|
+
phase: string,
|
|
96
|
+
source: string,
|
|
97
|
+
cause: unknown,
|
|
98
|
+
version = KUBO_VERSION,
|
|
99
|
+
): HostError {
|
|
100
|
+
return new HostError(
|
|
101
|
+
`Kubo ${version} ${process.platform}-${process.arch}, ${phase} (${source}): ${failure(cause)}. Check the indicated source or account permissions and retry ipab doctor --fix.`,
|
|
102
|
+
1,
|
|
103
|
+
"KUBO_SETUP_FAILED",
|
|
104
|
+
phase,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
async function exists(path: string) {
|
|
108
|
+
try {
|
|
109
|
+
await lstat(path);
|
|
110
|
+
return true;
|
|
111
|
+
} catch (cause) {
|
|
112
|
+
if (nodeError(cause, "ENOENT")) return false;
|
|
113
|
+
throw cause;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async function* fileBytes(path: string, limit: number) {
|
|
117
|
+
const stat = await lstat(path);
|
|
118
|
+
if (!stat.isFile())
|
|
119
|
+
throw new HostError("Expected a regular file, not a link or directory.");
|
|
120
|
+
if (stat.size > limit) throw new HostError("Kubo file exceeds limit.");
|
|
121
|
+
const handle = await open(path, "r");
|
|
122
|
+
try {
|
|
123
|
+
const buffer = Buffer.alloc(128 * 1024);
|
|
124
|
+
let total = 0;
|
|
125
|
+
for (;;) {
|
|
126
|
+
const { bytesRead } = await handle.read(buffer);
|
|
127
|
+
if (!bytesRead) return;
|
|
128
|
+
total += bytesRead;
|
|
129
|
+
if (total > limit) throw new HostError("Kubo file exceeds limit.");
|
|
130
|
+
yield buffer.subarray(0, bytesRead);
|
|
131
|
+
}
|
|
132
|
+
} finally {
|
|
133
|
+
await handle.close();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
export async function digest(path: string, limit: number) {
|
|
137
|
+
const hash = createHash("sha256");
|
|
138
|
+
for await (const chunk of fileBytes(path, limit)) hash.update(chunk);
|
|
139
|
+
return hash.digest("hex");
|
|
140
|
+
}
|
|
141
|
+
async function verifyProgram(executable: string, installed: Installation) {
|
|
142
|
+
if (
|
|
143
|
+
!compatibleVersion(installed.version) ||
|
|
144
|
+
installed.hash !== (await digest(executable, 512 * 1024 * 1024))
|
|
145
|
+
)
|
|
146
|
+
throw new HostError(
|
|
147
|
+
"Kubo installation integrity mismatch; do not use this binary.",
|
|
148
|
+
);
|
|
149
|
+
if (process.platform !== "win32") await access(executable, constants.X_OK);
|
|
150
|
+
}
|
|
151
|
+
export async function inspectKubo(
|
|
152
|
+
stateDir: string,
|
|
153
|
+
release?: KuboRelease,
|
|
154
|
+
signal?: AbortSignal,
|
|
155
|
+
verification?: KuboArchive,
|
|
156
|
+
): Promise<Installation | undefined> {
|
|
157
|
+
signal?.throwIfAborted();
|
|
158
|
+
if (await exists(stateDir)) await protectedPath(stateDir, true);
|
|
159
|
+
const directory = join(stateDir, "kubo-bin");
|
|
160
|
+
if (!(await exists(directory))) return;
|
|
161
|
+
await protectedPath(directory, true);
|
|
162
|
+
const names = await readdir(directory);
|
|
163
|
+
if (!names.length || names.every((name) => name === "operation.sqlite"))
|
|
164
|
+
return;
|
|
165
|
+
if (names.includes(journal))
|
|
166
|
+
throw new HostError(
|
|
167
|
+
"Kubo installation is interrupted; inspect the installation journal before repair.",
|
|
168
|
+
);
|
|
169
|
+
const executable = join(
|
|
170
|
+
directory,
|
|
171
|
+
process.platform === "win32" ? "ipfs.exe" : "ipfs",
|
|
172
|
+
);
|
|
173
|
+
const installed = await readPrivate<Installation>(directory, record, [
|
|
174
|
+
{ path: executable },
|
|
175
|
+
]);
|
|
176
|
+
if (!installed)
|
|
177
|
+
throw new HostError("Unrecorded Kubo installation; it was not adopted.");
|
|
178
|
+
await requireAccountOnlyMany([
|
|
179
|
+
directory,
|
|
180
|
+
executable,
|
|
181
|
+
join(directory, record),
|
|
182
|
+
]);
|
|
183
|
+
await verifyProgram(executable, installed);
|
|
184
|
+
const filename = assetName(installed.version);
|
|
185
|
+
const source =
|
|
186
|
+
verification?.version === installed.version
|
|
187
|
+
? verification
|
|
188
|
+
: enumeratedRelease(release, installed.version);
|
|
189
|
+
const expected =
|
|
190
|
+
installed.version === KUBO_VERSION
|
|
191
|
+
? releaseDigests[filename]
|
|
192
|
+
: source?.digest;
|
|
193
|
+
if (!expected) {
|
|
194
|
+
return { ...installed, provenance: false };
|
|
195
|
+
}
|
|
196
|
+
if (
|
|
197
|
+
installed.archiveDigest !== undefined &&
|
|
198
|
+
installed.archiveDigest !== expected
|
|
199
|
+
)
|
|
200
|
+
throw new HostError(
|
|
201
|
+
"Installed Kubo receipt conflicts with the trusted official archive digest.",
|
|
202
|
+
);
|
|
203
|
+
const cache = join(stateDir, "kubo-cache", filename);
|
|
204
|
+
if (!(await exists(cache))) return { ...installed, provenance: false };
|
|
205
|
+
await verifyCache(cache, expected);
|
|
206
|
+
{
|
|
207
|
+
// Stream an existing verified archive member to memory, never unpack files
|
|
208
|
+
// or fetch an archive while inspecting the installation.
|
|
209
|
+
const extracted = await run(
|
|
210
|
+
"tar",
|
|
211
|
+
[
|
|
212
|
+
"-xOf",
|
|
213
|
+
cache,
|
|
214
|
+
`kubo/${process.platform === "win32" ? "ipfs.exe" : "ipfs"}`,
|
|
215
|
+
],
|
|
216
|
+
{
|
|
217
|
+
encoding: "buffer",
|
|
218
|
+
signal,
|
|
219
|
+
maxBuffer: 512 * 1024 * 1024,
|
|
220
|
+
timeout: 30_000,
|
|
221
|
+
windowsHide: true,
|
|
222
|
+
},
|
|
223
|
+
);
|
|
224
|
+
if (!extracted.stdout.length)
|
|
225
|
+
throw new HostError(
|
|
226
|
+
"The official archive did not provide a nonempty Kubo executable member.",
|
|
227
|
+
);
|
|
228
|
+
if (
|
|
229
|
+
createHash("sha256").update(extracted.stdout).digest("hex") !==
|
|
230
|
+
installed.hash
|
|
231
|
+
)
|
|
232
|
+
throw new HostError(
|
|
233
|
+
"Installed Kubo does not match the verified official archive.",
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
signal?.throwIfAborted();
|
|
237
|
+
return { ...installed, provenance: true };
|
|
238
|
+
}
|
|
239
|
+
async function verifyCache(path: string, expected: string): Promise<void> {
|
|
240
|
+
try {
|
|
241
|
+
await requireAccountOnlyMany([join(path, ".."), path]);
|
|
242
|
+
if ((await digest(path, MAX_ARCHIVE)) !== expected)
|
|
243
|
+
throw new HostError(
|
|
244
|
+
"Kubo archive cache checksum mismatch; inspect the cache manually.",
|
|
245
|
+
1,
|
|
246
|
+
"KUBO_CACHE_INVALID",
|
|
247
|
+
);
|
|
248
|
+
} catch (cause) {
|
|
249
|
+
if (cause instanceof HostError && cause.code === "KUBO_CACHE_INVALID")
|
|
250
|
+
throw cause;
|
|
251
|
+
throw new HostError(
|
|
252
|
+
"Kubo archive cache is invalid or unprotected; inspect it manually.",
|
|
253
|
+
1,
|
|
254
|
+
"KUBO_CACHE_INVALID",
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
export async function inspectKuboCache(
|
|
259
|
+
stateDir: string,
|
|
260
|
+
release: KuboArchive,
|
|
261
|
+
): Promise<boolean> {
|
|
262
|
+
const directory = join(stateDir, "kubo-cache");
|
|
263
|
+
if (!(await exists(directory))) return false;
|
|
264
|
+
await protectedPath(directory, true);
|
|
265
|
+
await requireAccountOnlyMany([directory]);
|
|
266
|
+
const cache = join(directory, assetName(release.version));
|
|
267
|
+
if (!(await exists(cache))) return false;
|
|
268
|
+
await verifyCache(cache, release.digest);
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
export async function recoverableInstallation(
|
|
272
|
+
stateDir: string,
|
|
273
|
+
): Promise<boolean> {
|
|
274
|
+
const directory = join(stateDir, "kubo-bin");
|
|
275
|
+
if (!(await exists(directory))) return false;
|
|
276
|
+
const intent = await readPrivate<{ stage?: unknown }>(directory, journal);
|
|
277
|
+
if (!intent) return false;
|
|
278
|
+
if (
|
|
279
|
+
typeof intent.stage !== "string" ||
|
|
280
|
+
!/^install-[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/.test(intent.stage)
|
|
281
|
+
)
|
|
282
|
+
return false;
|
|
283
|
+
await requireAccountOnlyMany([directory, join(directory, journal)]);
|
|
284
|
+
const stage = join(directory, intent.stage);
|
|
285
|
+
if (await exists(stage)) {
|
|
286
|
+
await protectedPath(stage, true);
|
|
287
|
+
await requireAccountOnlyMany([stage]);
|
|
288
|
+
}
|
|
289
|
+
const executable = join(
|
|
290
|
+
directory,
|
|
291
|
+
process.platform === "win32" ? "ipfs.exe" : "ipfs",
|
|
292
|
+
);
|
|
293
|
+
const installed = await readPrivate<Installation>(directory, record);
|
|
294
|
+
const original = await readPrivate<Installation>(stage, "original.json");
|
|
295
|
+
if (original) {
|
|
296
|
+
await requireAccountOnlyMany([
|
|
297
|
+
join(stage, "original"),
|
|
298
|
+
join(stage, "original.json"),
|
|
299
|
+
]);
|
|
300
|
+
await verifyProgram(join(stage, "original"), original);
|
|
301
|
+
if (!installed) return false;
|
|
302
|
+
if (
|
|
303
|
+
installed.hash !== original.hash ||
|
|
304
|
+
installed.version !== original.version
|
|
305
|
+
)
|
|
306
|
+
await verifyProgram(executable, installed);
|
|
307
|
+
} else if (installed) {
|
|
308
|
+
await requireAccountOnlyMany([executable, join(directory, record)]);
|
|
309
|
+
await verifyProgram(executable, installed);
|
|
310
|
+
} else if (await exists(executable)) {
|
|
311
|
+
const pending = await readPrivate<Installation>(stage, record);
|
|
312
|
+
if (!pending) return false;
|
|
313
|
+
await requireAccountOnlyMany([executable, join(stage, record)]);
|
|
314
|
+
await verifyProgram(executable, pending);
|
|
315
|
+
}
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
export async function runningManagedNodes(stateDir: string): Promise<boolean> {
|
|
319
|
+
const directory = join(stateDir, "swarm-nodes");
|
|
320
|
+
if (!(await exists(directory))) return false;
|
|
321
|
+
await protectedPath(directory, true);
|
|
322
|
+
for (const name of await readdir(directory)) {
|
|
323
|
+
const root = join(directory, name);
|
|
324
|
+
await protectedPath(root, true);
|
|
325
|
+
const record = await readPrivate<{ pid?: unknown }>(root, "node.json");
|
|
326
|
+
if (record?.pid !== undefined) {
|
|
327
|
+
if (!Number.isSafeInteger(record.pid) || Number(record.pid) <= 0)
|
|
328
|
+
throw new HostError(
|
|
329
|
+
"Invalid managed node process record; upgrade is blocked.",
|
|
330
|
+
);
|
|
331
|
+
try {
|
|
332
|
+
process.kill(Number(record.pid), 0);
|
|
333
|
+
return true;
|
|
334
|
+
} catch (cause) {
|
|
335
|
+
if (!nodeError(cause, "ESRCH")) return true;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
// A repo lock without a trustworthy process record is not proof of a stopped node.
|
|
339
|
+
if (await exists(join(root, "repo", "repo.lock"))) return true;
|
|
340
|
+
}
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
async function transfer(
|
|
344
|
+
target: string,
|
|
345
|
+
source: { archive: string } | { url: string; size?: number },
|
|
346
|
+
options: InstallOptions,
|
|
347
|
+
) {
|
|
348
|
+
const handle = await open(target, "wx", 0o600);
|
|
349
|
+
const controller = new AbortController();
|
|
350
|
+
const signal = options.signal
|
|
351
|
+
? AbortSignal.any([options.signal, controller.signal])
|
|
352
|
+
: controller.signal;
|
|
353
|
+
let progress: ReturnType<typeof setInterval> | undefined;
|
|
354
|
+
let total = 0;
|
|
355
|
+
let receivedAt = Date.now();
|
|
356
|
+
let startedAt: number | undefined;
|
|
357
|
+
try {
|
|
358
|
+
await secureNew(target, false, { inherit: true });
|
|
359
|
+
let stream: AsyncIterable<Uint8Array>;
|
|
360
|
+
if ("archive" in source) stream = fileBytes(source.archive, MAX_ARCHIVE);
|
|
361
|
+
else {
|
|
362
|
+
const totalMiB =
|
|
363
|
+
source.size === undefined
|
|
364
|
+
? undefined
|
|
365
|
+
: (source.size / (1024 * 1024)).toFixed(1);
|
|
366
|
+
options.diagnostic?.(
|
|
367
|
+
`Downloading ${source.url.split("/").at(-1)} from GitHub (${totalMiB ? `${totalMiB} MiB` : "total size unavailable; bounded to 150 MiB"})...`,
|
|
368
|
+
);
|
|
369
|
+
if (options.diagnostic)
|
|
370
|
+
progress = setInterval(() => {
|
|
371
|
+
try {
|
|
372
|
+
const now = Date.now();
|
|
373
|
+
const idle = Math.floor((now - receivedAt) / 1000);
|
|
374
|
+
const waiting =
|
|
375
|
+
idle >= 5 ? `; waiting for data (${idle}s without new data)` : "";
|
|
376
|
+
const elapsed =
|
|
377
|
+
startedAt === undefined ? 0 : (now - startedAt) / 1000;
|
|
378
|
+
let eta = idle >= 5 ? "unavailable" : "estimating";
|
|
379
|
+
if (
|
|
380
|
+
source.size !== undefined &&
|
|
381
|
+
total > 0 &&
|
|
382
|
+
elapsed > 0 &&
|
|
383
|
+
idle < 5
|
|
384
|
+
) {
|
|
385
|
+
const seconds = Math.ceil(
|
|
386
|
+
(Math.max(0, source.size - total) * elapsed) / total,
|
|
387
|
+
);
|
|
388
|
+
eta = `~${seconds < 60 ? `${seconds}s` : `${Math.floor(seconds / 60)}m ${seconds % 60}s`}`;
|
|
389
|
+
}
|
|
390
|
+
options.diagnostic?.(
|
|
391
|
+
source.size === undefined
|
|
392
|
+
? `Kubo: ${(total / (1024 * 1024)).toFixed(1)} MiB received; total size unavailable${waiting}`
|
|
393
|
+
: `Kubo: ${(total / (1024 * 1024)).toFixed(1)}/${totalMiB} MiB (${((total * 100) / source.size).toFixed(1)}%); ETA ${eta}${waiting}`,
|
|
394
|
+
true,
|
|
395
|
+
);
|
|
396
|
+
} catch (cause) {
|
|
397
|
+
controller.abort(cause);
|
|
398
|
+
}
|
|
399
|
+
}, 5_000);
|
|
400
|
+
const response = await fetch(source.url, {
|
|
401
|
+
signal,
|
|
402
|
+
});
|
|
403
|
+
if (!response.ok || !response.body) {
|
|
404
|
+
await response.body?.cancel();
|
|
405
|
+
throw new HostError(`Kubo download failed (HTTP ${response.status}).`);
|
|
406
|
+
}
|
|
407
|
+
startedAt = Date.now();
|
|
408
|
+
stream = response.body as ReadableStream<Uint8Array>;
|
|
409
|
+
}
|
|
410
|
+
for await (const chunk of stream) {
|
|
411
|
+
signal.throwIfAborted();
|
|
412
|
+
total += chunk.byteLength;
|
|
413
|
+
if (total > MAX_ARCHIVE)
|
|
414
|
+
throw new HostError("Kubo archive exceeds limit.");
|
|
415
|
+
await handle.writeFile(chunk);
|
|
416
|
+
receivedAt = Date.now();
|
|
417
|
+
}
|
|
418
|
+
signal.throwIfAborted();
|
|
419
|
+
if ("url" in source && source.size !== undefined && total !== source.size)
|
|
420
|
+
throw new HostError("Kubo archive size mismatch.");
|
|
421
|
+
await handle.sync();
|
|
422
|
+
} catch (cause) {
|
|
423
|
+
signal.throwIfAborted();
|
|
424
|
+
throw cause;
|
|
425
|
+
} finally {
|
|
426
|
+
clearInterval(progress);
|
|
427
|
+
await handle.close();
|
|
428
|
+
}
|
|
429
|
+
return total;
|
|
430
|
+
}
|
|
431
|
+
const verificationJournal = "verification.json";
|
|
432
|
+
async function recoverVerification(directory: string): Promise<void> {
|
|
433
|
+
const intent = await readPrivate<unknown>(directory, verificationJournal);
|
|
434
|
+
if (intent === undefined) return;
|
|
435
|
+
if (
|
|
436
|
+
typeof intent !== "object" ||
|
|
437
|
+
intent === null ||
|
|
438
|
+
Object.keys(intent).length !== 1 ||
|
|
439
|
+
!("stage" in intent) ||
|
|
440
|
+
typeof intent.stage !== "string" ||
|
|
441
|
+
!/^verify-[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/.test(intent.stage)
|
|
442
|
+
)
|
|
443
|
+
throw new HostError(
|
|
444
|
+
"Unrecognized archive verification journal; inspect it manually.",
|
|
445
|
+
);
|
|
446
|
+
await requireAccountOnlyMany([
|
|
447
|
+
directory,
|
|
448
|
+
join(directory, verificationJournal),
|
|
449
|
+
]);
|
|
450
|
+
const stage = join(directory, intent.stage);
|
|
451
|
+
if (await exists(stage)) {
|
|
452
|
+
await protectedPath(stage, true);
|
|
453
|
+
await requireAccountOnlyMany([stage]);
|
|
454
|
+
await rm(stage, { recursive: true });
|
|
455
|
+
}
|
|
456
|
+
await rm(join(directory, verificationJournal));
|
|
457
|
+
}
|
|
458
|
+
async function cacheVerificationArchive(
|
|
459
|
+
stateDir: string,
|
|
460
|
+
source: KuboArchive,
|
|
461
|
+
options: InstallOptions,
|
|
462
|
+
): Promise<void> {
|
|
463
|
+
const directory = await privateDirectory(stateDir, "kubo-cache");
|
|
464
|
+
await requireAccountOnlyMany([directory]);
|
|
465
|
+
await recoverVerification(directory);
|
|
466
|
+
const target = join(directory, source.filename);
|
|
467
|
+
if (await exists(target)) {
|
|
468
|
+
await verifyCache(target, source.digest);
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
const name = `verify-${randomUUID()}`,
|
|
472
|
+
stage = join(directory, name);
|
|
473
|
+
if (await exists(stage))
|
|
474
|
+
throw new HostError("Verification staging path exists; retry later.");
|
|
475
|
+
await writePrivate(directory, verificationJournal, { stage: name });
|
|
476
|
+
let created = false;
|
|
477
|
+
try {
|
|
478
|
+
await mkdir(stage, { mode: 0o700 });
|
|
479
|
+
created = true;
|
|
480
|
+
await secureNew(stage, true);
|
|
481
|
+
const temporary = join(stage, source.filename);
|
|
482
|
+
await transfer(
|
|
483
|
+
temporary,
|
|
484
|
+
options.archive
|
|
485
|
+
? { archive: options.archive }
|
|
486
|
+
: {
|
|
487
|
+
url: source.url,
|
|
488
|
+
...(source.size === undefined ? {} : { size: source.size }),
|
|
489
|
+
},
|
|
490
|
+
options,
|
|
491
|
+
);
|
|
492
|
+
if ((await digest(temporary, MAX_ARCHIVE)) !== source.digest)
|
|
493
|
+
throw new HostError(
|
|
494
|
+
"Official verification archive checksum mismatch; no cache or executable was replaced.",
|
|
495
|
+
);
|
|
496
|
+
options.signal?.throwIfAborted();
|
|
497
|
+
try {
|
|
498
|
+
await link(temporary, target);
|
|
499
|
+
} catch (cause) {
|
|
500
|
+
if (!nodeError(cause, "EEXIST")) throw cause;
|
|
501
|
+
await verifyCache(target, source.digest);
|
|
502
|
+
}
|
|
503
|
+
} finally {
|
|
504
|
+
if (created) await rm(stage, { recursive: true, force: true });
|
|
505
|
+
await rm(join(directory, verificationJournal));
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
export async function verifyExistingKuboArchive(
|
|
509
|
+
stateDir: string,
|
|
510
|
+
source: KuboArchive,
|
|
511
|
+
options: InstallOptions = {},
|
|
512
|
+
): Promise<string> {
|
|
513
|
+
try {
|
|
514
|
+
options.signal?.throwIfAborted();
|
|
515
|
+
if (
|
|
516
|
+
!compatibleVersion(source.version) ||
|
|
517
|
+
source.filename !== assetName(source.version) ||
|
|
518
|
+
source.url !== officialDownloadUrl(source.version, source.filename) ||
|
|
519
|
+
!/^[a-f0-9]{64}$/.test(source.digest) ||
|
|
520
|
+
(source.size !== undefined &&
|
|
521
|
+
(!Number.isSafeInteger(source.size) ||
|
|
522
|
+
source.size <= 0 ||
|
|
523
|
+
source.size > MAX_ARCHIVE)) ||
|
|
524
|
+
(source.version === KUBO_VERSION
|
|
525
|
+
? source.digest !== releaseDigests[source.filename]
|
|
526
|
+
: source.size === undefined)
|
|
527
|
+
)
|
|
528
|
+
throw new HostError(
|
|
529
|
+
"A trusted official same-version archive is required for verification.",
|
|
530
|
+
);
|
|
531
|
+
const directory = join(stateDir, "kubo-bin");
|
|
532
|
+
await protectedPath(stateDir, true);
|
|
533
|
+
await protectedPath(directory, true);
|
|
534
|
+
await requireAccountOnlyMany([stateDir, directory]);
|
|
535
|
+
return await withPrivateLock(
|
|
536
|
+
directory,
|
|
537
|
+
async () => {
|
|
538
|
+
const installed = await inspectKubo(
|
|
539
|
+
stateDir,
|
|
540
|
+
options.release,
|
|
541
|
+
options.signal,
|
|
542
|
+
source,
|
|
543
|
+
);
|
|
544
|
+
if (!installed || installed.version !== source.version)
|
|
545
|
+
throw new HostError(
|
|
546
|
+
"Installed Kubo changed since authorization; no program was replaced.",
|
|
547
|
+
);
|
|
548
|
+
const executable = join(
|
|
549
|
+
directory,
|
|
550
|
+
process.platform === "win32" ? "ipfs.exe" : "ipfs",
|
|
551
|
+
);
|
|
552
|
+
if (installed.provenance) return executable;
|
|
553
|
+
options.diagnostic?.(
|
|
554
|
+
`Caching the official Kubo ${source.version} archive for byte verification only; no reinstall or node stopping.`,
|
|
555
|
+
);
|
|
556
|
+
await cacheVerificationArchive(stateDir, source, options);
|
|
557
|
+
const verified = await inspectKubo(
|
|
558
|
+
stateDir,
|
|
559
|
+
options.release,
|
|
560
|
+
options.signal,
|
|
561
|
+
source,
|
|
562
|
+
);
|
|
563
|
+
if (!verified?.provenance || verified.version !== source.version)
|
|
564
|
+
throw new HostError(
|
|
565
|
+
"Official archive verification did not establish provenance; no program was replaced.",
|
|
566
|
+
);
|
|
567
|
+
options.signal?.throwIfAborted();
|
|
568
|
+
options.diagnostic?.(
|
|
569
|
+
`Existing Kubo ${source.version} matches the official archive; reused without reinstall or node stopping.`,
|
|
570
|
+
);
|
|
571
|
+
return executable;
|
|
572
|
+
},
|
|
573
|
+
options.signal,
|
|
574
|
+
);
|
|
575
|
+
} catch (cause) {
|
|
576
|
+
options.signal?.throwIfAborted();
|
|
577
|
+
throw setupFailure(
|
|
578
|
+
"same-version archive verification",
|
|
579
|
+
"official archive cache and existing program",
|
|
580
|
+
cause,
|
|
581
|
+
source.version,
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
async function clean(directory: string, stage: string) {
|
|
586
|
+
await rm(stage, { recursive: true, force: true });
|
|
587
|
+
await rm(join(directory, journal));
|
|
588
|
+
}
|
|
589
|
+
async function recover(
|
|
590
|
+
directory: string,
|
|
591
|
+
executable: string,
|
|
592
|
+
installed: Installation | undefined,
|
|
593
|
+
) {
|
|
594
|
+
const intent = await readPrivate<unknown>(directory, journal);
|
|
595
|
+
if (intent === undefined) return installed;
|
|
596
|
+
if (
|
|
597
|
+
typeof intent !== "object" ||
|
|
598
|
+
intent === null ||
|
|
599
|
+
!("stage" in intent) ||
|
|
600
|
+
typeof intent.stage !== "string" ||
|
|
601
|
+
!/^install-[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/.test(intent.stage)
|
|
602
|
+
)
|
|
603
|
+
throw new HostError(
|
|
604
|
+
"Unrecognized Kubo installation journal; inspect it manually.",
|
|
605
|
+
);
|
|
606
|
+
const stage = join(directory, intent.stage);
|
|
607
|
+
if (await exists(stage)) await protectedPath(stage, true);
|
|
608
|
+
const original = await readPrivate<Installation>(stage, "original.json");
|
|
609
|
+
if (original) {
|
|
610
|
+
const backup = join(stage, "original");
|
|
611
|
+
await verifyProgram(backup, original);
|
|
612
|
+
if (
|
|
613
|
+
installed?.hash === original.hash &&
|
|
614
|
+
installed.version === original.version
|
|
615
|
+
) {
|
|
616
|
+
await rename(backup, executable);
|
|
617
|
+
await rename(join(stage, "original.json"), join(directory, record));
|
|
618
|
+
installed = original;
|
|
619
|
+
} else if (installed) await verifyProgram(executable, installed);
|
|
620
|
+
else
|
|
621
|
+
throw new HostError(
|
|
622
|
+
"Interrupted Kubo upgrade has an inconsistent receipt; inspect it manually.",
|
|
623
|
+
);
|
|
624
|
+
}
|
|
625
|
+
if (!installed && (await exists(executable))) {
|
|
626
|
+
const pending = await readPrivate<Installation>(stage, record, [
|
|
627
|
+
{ path: executable },
|
|
628
|
+
]);
|
|
629
|
+
if (!pending)
|
|
630
|
+
throw new HostError(
|
|
631
|
+
"Unrecorded Kubo executable; move it aside manually. It was not adopted.",
|
|
632
|
+
);
|
|
633
|
+
await verifyProgram(executable, pending);
|
|
634
|
+
await rename(join(stage, record), join(directory, record));
|
|
635
|
+
installed = pending;
|
|
636
|
+
}
|
|
637
|
+
await clean(directory, stage);
|
|
638
|
+
return installed;
|
|
639
|
+
}
|
|
640
|
+
export async function kuboBinary(
|
|
641
|
+
stateDir: string,
|
|
642
|
+
options: InstallOptions = {},
|
|
643
|
+
): Promise<string> {
|
|
644
|
+
let phase = "installation",
|
|
645
|
+
source = "protected local installation";
|
|
646
|
+
let targetVersion = options.release?.version ?? KUBO_VERSION;
|
|
647
|
+
try {
|
|
648
|
+
options.signal?.throwIfAborted();
|
|
649
|
+
const directory = await privateDirectory(stateDir, "kubo-bin");
|
|
650
|
+
options.diagnostic?.(
|
|
651
|
+
"Checking the Kubo installation (local lock wait: up to 30s)...",
|
|
652
|
+
);
|
|
653
|
+
const platform =
|
|
654
|
+
process.platform === "win32" ? "windows" : process.platform;
|
|
655
|
+
if (
|
|
656
|
+
!["windows", "darwin", "linux"].includes(platform) ||
|
|
657
|
+
!["x64", "arm64"].includes(process.arch)
|
|
658
|
+
)
|
|
659
|
+
throw new HostError("Unsupported Kubo platform.");
|
|
660
|
+
const program = platform === "windows" ? "ipfs.exe" : "ipfs";
|
|
661
|
+
const executable = join(directory, program);
|
|
662
|
+
const install = async () => {
|
|
663
|
+
let installed = await readPrivate<Installation>(directory, record, [
|
|
664
|
+
{ path: executable },
|
|
665
|
+
]);
|
|
666
|
+
installed = await recover(directory, executable, installed);
|
|
667
|
+
if (installed !== undefined) {
|
|
668
|
+
options.diagnostic?.(
|
|
669
|
+
`Verifying installed Kubo ${installed.version}...`,
|
|
670
|
+
);
|
|
671
|
+
await verifyProgram(executable, installed);
|
|
672
|
+
}
|
|
673
|
+
if (
|
|
674
|
+
options.diagnostic &&
|
|
675
|
+
(await readdir(directory)).some((name) =>
|
|
676
|
+
/^(?:extract-[\w-]{43}|[\w-]{43}\.(?:zip|tar\.gz)|(?:install-|\.write-)[a-f0-9-]{36})$/.test(
|
|
677
|
+
name,
|
|
678
|
+
),
|
|
679
|
+
)
|
|
680
|
+
)
|
|
681
|
+
options.diagnostic(
|
|
682
|
+
"Untracked Kubo staging files were left untouched. Inspect kubo-bin manually; do not delete the verified installation.",
|
|
683
|
+
);
|
|
684
|
+
if (
|
|
685
|
+
installed &&
|
|
686
|
+
(!options.upgrade ||
|
|
687
|
+
(options.release &&
|
|
688
|
+
compareKuboVersions(options.release.version, installed.version) <=
|
|
689
|
+
0))
|
|
690
|
+
) {
|
|
691
|
+
if (options.archive !== undefined) {
|
|
692
|
+
phase = "archive verification";
|
|
693
|
+
source = "local archive";
|
|
694
|
+
const expected =
|
|
695
|
+
options.release?.digest ?? releaseDigests[assetName(KUBO_VERSION)];
|
|
696
|
+
if ((await digest(options.archive, MAX_ARCHIVE)) !== expected)
|
|
697
|
+
throw new HostError("Kubo archive checksum mismatch.");
|
|
698
|
+
}
|
|
699
|
+
options.diagnostic?.(
|
|
700
|
+
`Kubo ${installed.version} bytes match the protected installation receipt; reusing the existing installation. No download or reinstall needed. Cryptographic archive provenance requires doctor verification.`,
|
|
701
|
+
);
|
|
702
|
+
return executable;
|
|
703
|
+
}
|
|
704
|
+
if (!installed && (await exists(executable)))
|
|
705
|
+
throw new HostError(
|
|
706
|
+
"Unrecorded Kubo executable; move it aside manually. It was not adopted.",
|
|
707
|
+
);
|
|
708
|
+
let selected = options.release;
|
|
709
|
+
if (!selected && options.archive === undefined) {
|
|
710
|
+
phase = "release metadata";
|
|
711
|
+
source = "api.github.com/ipfs/kubo";
|
|
712
|
+
options.diagnostic?.(
|
|
713
|
+
"Selecting the latest verified compatible official Kubo release...",
|
|
714
|
+
);
|
|
715
|
+
selected = await latestKuboRelease(options.signal);
|
|
716
|
+
}
|
|
717
|
+
const version = selected?.version ?? KUBO_VERSION;
|
|
718
|
+
targetVersion = version;
|
|
719
|
+
if (!compatibleVersion(version))
|
|
720
|
+
throw new HostError("Unsupported Kubo version.");
|
|
721
|
+
const filename = assetName(version);
|
|
722
|
+
const expected = selected?.digest ?? releaseDigests[filename];
|
|
723
|
+
if (!expected || !/^[a-f0-9]{64}$/.test(expected))
|
|
724
|
+
throw new HostError(
|
|
725
|
+
"No trusted archive digest for this release/platform.",
|
|
726
|
+
);
|
|
727
|
+
let input: { archive: string } | { url: string; size: number };
|
|
728
|
+
if (
|
|
729
|
+
installed &&
|
|
730
|
+
(compareKuboVersions(version, installed.version) <= 0 ||
|
|
731
|
+
(await runningManagedNodes(stateDir)))
|
|
732
|
+
)
|
|
733
|
+
throw new HostError(
|
|
734
|
+
"Kubo upgrade is blocked; managed nodes must be stopped by their operator before switching.",
|
|
735
|
+
);
|
|
736
|
+
const cacheDirectory = await privateDirectory(stateDir, "kubo-cache");
|
|
737
|
+
await requireAccountOnlyMany([cacheDirectory]);
|
|
738
|
+
const cached = join(cacheDirectory, filename);
|
|
739
|
+
if (await exists(cached)) {
|
|
740
|
+
phase = "cache verification";
|
|
741
|
+
await verifyCache(cached, expected);
|
|
742
|
+
options.diagnostic?.(
|
|
743
|
+
"Reusing the verified official Kubo archive cache.",
|
|
744
|
+
);
|
|
745
|
+
input = { archive: cached };
|
|
746
|
+
} else if (options.archive !== undefined)
|
|
747
|
+
input = { archive: options.archive };
|
|
748
|
+
else if (selected) input = selected;
|
|
749
|
+
else
|
|
750
|
+
throw new HostError(
|
|
751
|
+
"A verified latest compatible Kubo target is required.",
|
|
752
|
+
);
|
|
753
|
+
phase = "installation";
|
|
754
|
+
source = "protected local staging";
|
|
755
|
+
const name = `install-${randomUUID()}`,
|
|
756
|
+
stage = join(directory, name);
|
|
757
|
+
if (await exists(stage))
|
|
758
|
+
throw new HostError(
|
|
759
|
+
"Staging path already exists; retry ipab doctor --fix.",
|
|
760
|
+
);
|
|
761
|
+
await writePrivate(directory, journal, { stage: name });
|
|
762
|
+
let placed = false,
|
|
763
|
+
created = false;
|
|
764
|
+
try {
|
|
765
|
+
await mkdir(stage, { mode: 0o700 });
|
|
766
|
+
created = true;
|
|
767
|
+
await secureNew(stage, true);
|
|
768
|
+
const archive = join(stage, filename);
|
|
769
|
+
phase = "archive transfer";
|
|
770
|
+
source = "archive" in input ? "local archive" : "github.com/ipfs/kubo";
|
|
771
|
+
if ("archive" in input)
|
|
772
|
+
options.diagnostic?.("Copying the local Kubo archive...");
|
|
773
|
+
const total = await transfer(archive, input, options);
|
|
774
|
+
phase = "archive verification";
|
|
775
|
+
options.diagnostic?.(
|
|
776
|
+
`Kubo download complete: ${(total / (1024 * 1024)).toFixed(1)} MiB. Verifying checksum...`,
|
|
777
|
+
);
|
|
778
|
+
if ((await digest(archive, MAX_ARCHIVE)) !== expected)
|
|
779
|
+
throw new HostError("Kubo archive checksum mismatch.");
|
|
780
|
+
if (!(await exists(cached))) {
|
|
781
|
+
await link(archive, cached);
|
|
782
|
+
} else await verifyCache(cached, expected);
|
|
783
|
+
phase = "extraction";
|
|
784
|
+
source = "verified release archive";
|
|
785
|
+
options.diagnostic?.("Extracting and verifying the Kubo executable...");
|
|
786
|
+
const member = `kubo/${program}`;
|
|
787
|
+
await run("tar", ["-xf", archive, "-C", stage, member], {
|
|
788
|
+
signal: options.signal,
|
|
789
|
+
timeout: 30_000,
|
|
790
|
+
windowsHide: true,
|
|
791
|
+
});
|
|
792
|
+
const extracted = join(stage, "kubo", program);
|
|
793
|
+
if (!(await lstat(extracted)).isFile())
|
|
794
|
+
throw new HostError("Invalid Kubo executable.");
|
|
795
|
+
await secureNew(extracted);
|
|
796
|
+
if (platform !== "windows") await chmod(extracted, 0o700);
|
|
797
|
+
phase = "executable verification";
|
|
798
|
+
source = "staged Kubo program";
|
|
799
|
+
const version = await run(extracted, ["version", "--number"], {
|
|
800
|
+
signal: options.signal,
|
|
801
|
+
env: {
|
|
802
|
+
...process.env,
|
|
803
|
+
HOME: stage,
|
|
804
|
+
USERPROFILE: stage,
|
|
805
|
+
IPFS_PATH: join(stage, "version-repo"),
|
|
806
|
+
},
|
|
807
|
+
timeout: 10_000,
|
|
808
|
+
windowsHide: true,
|
|
809
|
+
});
|
|
810
|
+
const reportedVersion = version.stdout.trim();
|
|
811
|
+
if (
|
|
812
|
+
!compatibleVersion(reportedVersion) ||
|
|
813
|
+
compareKuboVersions(
|
|
814
|
+
reportedVersion,
|
|
815
|
+
selected?.version ?? KUBO_VERSION,
|
|
816
|
+
) !== 0
|
|
817
|
+
)
|
|
818
|
+
throw new HostError("Kubo executable version mismatch.");
|
|
819
|
+
const runtimeOptions = {
|
|
820
|
+
signal: options.signal,
|
|
821
|
+
env: {
|
|
822
|
+
...process.env,
|
|
823
|
+
HOME: stage,
|
|
824
|
+
USERPROFILE: stage,
|
|
825
|
+
IPFS_PATH: join(stage, "version-repo"),
|
|
826
|
+
},
|
|
827
|
+
timeout: 10_000,
|
|
828
|
+
windowsHide: true,
|
|
829
|
+
};
|
|
830
|
+
const repoVersion = await run(
|
|
831
|
+
extracted,
|
|
832
|
+
["version", "--repo"],
|
|
833
|
+
runtimeOptions,
|
|
834
|
+
);
|
|
835
|
+
if (repoVersion.stdout.trim() !== KUBO_REPO_VERSION)
|
|
836
|
+
throw new HostError(
|
|
837
|
+
"Kubo candidate requires an unsupported repo version; no migration or upgrade was performed.",
|
|
838
|
+
);
|
|
839
|
+
const commands = await run(extracted, ["commands"], runtimeOptions);
|
|
840
|
+
const available = new Set(
|
|
841
|
+
commands.stdout
|
|
842
|
+
.trim()
|
|
843
|
+
.split(/\r?\n/)
|
|
844
|
+
.map((line) => line.trim()),
|
|
845
|
+
);
|
|
846
|
+
if (
|
|
847
|
+
[
|
|
848
|
+
"id",
|
|
849
|
+
"swarm connect",
|
|
850
|
+
"diag datastore put",
|
|
851
|
+
"dag import",
|
|
852
|
+
"dag export",
|
|
853
|
+
"pin add",
|
|
854
|
+
"block get",
|
|
855
|
+
"config",
|
|
856
|
+
].some((name) => !available.has(`ipfs ${name}`))
|
|
857
|
+
)
|
|
858
|
+
throw new HostError(
|
|
859
|
+
"Kubo candidate lacks required IPAB runtime commands; the existing installation was retained.",
|
|
860
|
+
);
|
|
861
|
+
const verified = {
|
|
862
|
+
version: selected?.version ?? KUBO_VERSION,
|
|
863
|
+
hash: await digest(extracted, 512 * 1024 * 1024),
|
|
864
|
+
archiveDigest: expected,
|
|
865
|
+
};
|
|
866
|
+
phase = "installation";
|
|
867
|
+
source = "protected local installation";
|
|
868
|
+
await writePrivate(stage, record, verified);
|
|
869
|
+
if (installed) {
|
|
870
|
+
// Keep the original executable and receipt until the candidate is fully verified.
|
|
871
|
+
if (await runningManagedNodes(stateDir))
|
|
872
|
+
throw new HostError(
|
|
873
|
+
"Managed node started during inspection; upgrade is blocked.",
|
|
874
|
+
);
|
|
875
|
+
await link(executable, join(stage, "original"));
|
|
876
|
+
await writePrivate(stage, "original.json", installed);
|
|
877
|
+
await rename(extracted, executable);
|
|
878
|
+
} else await link(extracted, executable);
|
|
879
|
+
placed = true;
|
|
880
|
+
try {
|
|
881
|
+
await rename(join(stage, record), join(directory, record));
|
|
882
|
+
} catch (cause) {
|
|
883
|
+
if (installed) {
|
|
884
|
+
await rename(join(stage, "original"), executable);
|
|
885
|
+
await rename(join(stage, "original.json"), join(directory, record));
|
|
886
|
+
placed = false;
|
|
887
|
+
}
|
|
888
|
+
throw cause;
|
|
889
|
+
}
|
|
890
|
+
placed = false;
|
|
891
|
+
return executable;
|
|
892
|
+
} finally {
|
|
893
|
+
// If rollback itself fails, retain the journal and verified candidate record for recovery.
|
|
894
|
+
if (placed) {
|
|
895
|
+
if (installed) {
|
|
896
|
+
await rename(join(stage, "original"), executable);
|
|
897
|
+
await rename(join(stage, "original.json"), join(directory, record));
|
|
898
|
+
} else await rm(executable);
|
|
899
|
+
}
|
|
900
|
+
if (created) await clean(directory, stage);
|
|
901
|
+
else await rm(join(directory, journal));
|
|
902
|
+
}
|
|
903
|
+
};
|
|
904
|
+
return await withPrivateLock(directory, install, options.signal);
|
|
905
|
+
} catch (cause) {
|
|
906
|
+
options.signal?.throwIfAborted();
|
|
907
|
+
throw setupFailure(phase, source, cause, targetVersion);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
export async function withKuboProgram<T>(
|
|
912
|
+
stateDir: string,
|
|
913
|
+
operation: (binary: string) => Promise<T>,
|
|
914
|
+
signal?: AbortSignal,
|
|
915
|
+
): Promise<T> {
|
|
916
|
+
const binary = await kuboBinary(stateDir, signal ? { signal } : {});
|
|
917
|
+
// Node preparation/startup shares the install lock through PID persistence,
|
|
918
|
+
// preventing a node launch racing the final upgrade/switch check.
|
|
919
|
+
return withPrivateLock(
|
|
920
|
+
join(stateDir, "kubo-bin"),
|
|
921
|
+
async () => {
|
|
922
|
+
const installed = await readPrivate<Installation>(
|
|
923
|
+
join(stateDir, "kubo-bin"),
|
|
924
|
+
record,
|
|
925
|
+
[{ path: binary }],
|
|
926
|
+
);
|
|
927
|
+
if (!installed)
|
|
928
|
+
throw new HostError(
|
|
929
|
+
"Managed Kubo installation disappeared before node startup.",
|
|
930
|
+
);
|
|
931
|
+
await verifyProgram(binary, installed);
|
|
932
|
+
return operation(binary);
|
|
933
|
+
},
|
|
934
|
+
signal,
|
|
935
|
+
);
|
|
936
|
+
}
|