pinnace 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +15 -0
- package/dist/car/car-build.d.ts +26 -0
- package/dist/car/car-build.d.ts.map +1 -0
- package/dist/car/car-build.js +113 -0
- package/dist/car/car-build.js.map +1 -0
- package/dist/ci/ci-emit.d.ts +116 -0
- package/dist/ci/ci-emit.d.ts.map +1 -0
- package/dist/ci/ci-emit.js +190 -0
- package/dist/ci/ci-emit.js.map +1 -0
- package/dist/cli/bin.d.ts +3 -0
- package/dist/cli/bin.d.ts.map +1 -0
- package/dist/cli/bin.js +17 -0
- package/dist/cli/bin.js.map +1 -0
- package/dist/cli/run.d.ts +78 -0
- package/dist/cli/run.d.ts.map +1 -0
- package/dist/cli/run.js +506 -0
- package/dist/cli/run.js.map +1 -0
- package/dist/config/config-resolution.d.ts +186 -0
- package/dist/config/config-resolution.d.ts.map +1 -0
- package/dist/config/config-resolution.js +137 -0
- package/dist/config/config-resolution.js.map +1 -0
- package/dist/deploy/deploy.d.ts +121 -0
- package/dist/deploy/deploy.d.ts.map +1 -0
- package/dist/deploy/deploy.js +150 -0
- package/dist/deploy/deploy.js.map +1 -0
- package/dist/derive/ipns-key-derivation.d.ts +37 -0
- package/dist/derive/ipns-key-derivation.d.ts.map +1 -0
- package/dist/derive/ipns-key-derivation.js +130 -0
- package/dist/derive/ipns-key-derivation.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/node/node-commands.d.ts +115 -0
- package/dist/node/node-commands.d.ts.map +1 -0
- package/dist/node/node-commands.js +227 -0
- package/dist/node/node-commands.js.map +1 -0
- package/dist/provision/cloud-init.d.ts +137 -0
- package/dist/provision/cloud-init.d.ts.map +1 -0
- package/dist/provision/cloud-init.js +390 -0
- package/dist/provision/cloud-init.js.map +1 -0
- package/dist/publisher/key-import.d.ts +105 -0
- package/dist/publisher/key-import.d.ts.map +1 -0
- package/dist/publisher/key-import.js +76 -0
- package/dist/publisher/key-import.js.map +1 -0
- package/dist/publisher/record-sequence.d.ts +90 -0
- package/dist/publisher/record-sequence.d.ts.map +1 -0
- package/dist/publisher/record-sequence.js +250 -0
- package/dist/publisher/record-sequence.js.map +1 -0
- package/dist/rpc/kubo-rpc-client.d.ts +154 -0
- package/dist/rpc/kubo-rpc-client.d.ts.map +1 -0
- package/dist/rpc/kubo-rpc-client.js +207 -0
- package/dist/rpc/kubo-rpc-client.js.map +1 -0
- package/dist/rpc/mock-kubo.d.ts +87 -0
- package/dist/rpc/mock-kubo.d.ts.map +1 -0
- package/dist/rpc/mock-kubo.js +129 -0
- package/dist/rpc/mock-kubo.js.map +1 -0
- package/dist/site/site-management.d.ts +126 -0
- package/dist/site/site-management.d.ts.map +1 -0
- package/dist/site/site-management.js +123 -0
- package/dist/site/site-management.js.map +1 -0
- package/dist/status/status-report.d.ts +145 -0
- package/dist/status/status-report.d.ts.map +1 -0
- package/dist/status/status-report.js +192 -0
- package/dist/status/status-report.js.map +1 -0
- package/package.json +45 -2
- package/src/car/car-build.ts +135 -0
- package/src/ci/ci-emit.ts +284 -0
- package/src/cli/bin.ts +20 -0
- package/src/cli/run.ts +654 -0
- package/src/config/config-resolution.ts +264 -0
- package/src/deploy/deploy.ts +250 -0
- package/src/derive/ipns-key-derivation.ts +173 -0
- package/src/index.ts +146 -0
- package/src/node/node-commands.ts +391 -0
- package/src/provision/cloud-init.ts +551 -0
- package/src/publisher/key-import.ts +141 -0
- package/src/publisher/record-sequence.ts +336 -0
- package/src/rpc/kubo-rpc-client.ts +281 -0
- package/src/rpc/mock-kubo.ts +194 -0
- package/src/site/site-management.ts +241 -0
- package/src/status/status-report.ts +291 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A mock Kubo HTTP API for tests: it RECORDS every incoming request (method,
|
|
3
|
+
* path, query, headers, body) and returns canned responses, so a test can
|
|
4
|
+
* assert the exact call shape a `KuboRpcClient` produced WITHOUT a live daemon.
|
|
5
|
+
*
|
|
6
|
+
* This is the primary test boundary for every host-agnostic pinnace operation
|
|
7
|
+
* (deploy, publish, status, site management). It is exposed from the package's
|
|
8
|
+
* test surface so sibling tasks (deploy-multi-target, status-report, ...) reuse
|
|
9
|
+
* it rather than re-inventing a Kubo fake.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** One multipart file part the mock extracted from a `FormData` body. */
|
|
13
|
+
export interface RecordedFilePart {
|
|
14
|
+
/** The multipart field name (Kubo's file-upload endpoints expect `file`). */
|
|
15
|
+
field: string;
|
|
16
|
+
/** The optional filename the part was appended with. */
|
|
17
|
+
filename?: string;
|
|
18
|
+
/** The part's bytes. */
|
|
19
|
+
bytes: Uint8Array;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** One recorded RPC request as the mock observed it. */
|
|
23
|
+
export interface RecordedRequest {
|
|
24
|
+
/** The HTTP method (Kubo RPC is always POST). */
|
|
25
|
+
method: string;
|
|
26
|
+
/** The `/api/v0/...` path, WITHOUT the base URL or query string. */
|
|
27
|
+
path: string;
|
|
28
|
+
/** Parsed query parameters (multi-valued: `arg` can repeat). */
|
|
29
|
+
query: URLSearchParams;
|
|
30
|
+
/** Lower-cased header name → value, as fetch delivered them. */
|
|
31
|
+
headers: Record<string, string>;
|
|
32
|
+
/** The raw request body as text (empty string if none). */
|
|
33
|
+
bodyText: string;
|
|
34
|
+
/**
|
|
35
|
+
* The effective request `content-type`. For a `FormData` body this is
|
|
36
|
+
* `multipart/form-data` (real `fetch` sets it, WITH a boundary, when it
|
|
37
|
+
* serialises FormData) even though the caller must NOT hand-set it — the
|
|
38
|
+
* mock derives it here so tests can assert the multipart contract Kubo
|
|
39
|
+
* requires for its file-upload endpoints.
|
|
40
|
+
*/
|
|
41
|
+
contentType?: string;
|
|
42
|
+
/**
|
|
43
|
+
* When the body was a `FormData` (a `multipart/form-data` upload), the file
|
|
44
|
+
* parts it carried. Empty/undefined for non-multipart bodies. Kubo's
|
|
45
|
+
* `add`, `dag/import` and `key/import` require at least one file part under
|
|
46
|
+
* the field name `file`.
|
|
47
|
+
*/
|
|
48
|
+
fileParts?: RecordedFilePart[];
|
|
49
|
+
/** The full URL that was fetched. */
|
|
50
|
+
url: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** A canned response for a given `/api/v0/<path>` (path without query). */
|
|
54
|
+
export interface MockResponseSpec {
|
|
55
|
+
/** HTTP status to return (default 200). */
|
|
56
|
+
status?: number;
|
|
57
|
+
/** JSON body to return; serialised and sent with content-type application/json. */
|
|
58
|
+
json?: unknown;
|
|
59
|
+
/** Raw text body to return (takes precedence over `json` if both set). */
|
|
60
|
+
text?: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* A recording mock Kubo API. Install it with {@link fetchImpl} as the `fetch`
|
|
65
|
+
* a {@link KuboRpcClient} uses; inspect {@link requests} afterwards.
|
|
66
|
+
*/
|
|
67
|
+
export class MockKuboApi {
|
|
68
|
+
/** Every request the mock received, in order. */
|
|
69
|
+
readonly requests: RecordedRequest[] = [];
|
|
70
|
+
|
|
71
|
+
/** path (no query) → canned response. */
|
|
72
|
+
private readonly responses = new Map<string, MockResponseSpec>();
|
|
73
|
+
|
|
74
|
+
/** The base URL this mock pretends to be (only its path/query are used). */
|
|
75
|
+
constructor(readonly baseUrl: string = 'https://node.example.test') {}
|
|
76
|
+
|
|
77
|
+
/** Register a canned response for a Kubo `/api/v0/<path>` (path without query). */
|
|
78
|
+
on(path: string, spec: MockResponseSpec): this {
|
|
79
|
+
this.responses.set(path, spec);
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The most recent recorded request, or undefined if none. */
|
|
84
|
+
get lastRequest(): RecordedRequest | undefined {
|
|
85
|
+
return this.requests[this.requests.length - 1];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** All recorded requests whose path equals `path`. */
|
|
89
|
+
requestsFor(path: string): RecordedRequest[] {
|
|
90
|
+
return this.requests.filter((r) => r.path === path);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* A `fetch`-compatible function that records the request and returns the
|
|
95
|
+
* canned (or a default empty-JSON 200) response. Pass this to the client.
|
|
96
|
+
*/
|
|
97
|
+
readonly fetchImpl = async (
|
|
98
|
+
input: string | URL,
|
|
99
|
+
init?: {method?: string; headers?: Record<string, string>; body?: unknown},
|
|
100
|
+
): Promise<Response> => {
|
|
101
|
+
const url = new URL(typeof input === 'string' ? input : input.toString());
|
|
102
|
+
const path = url.pathname.replace(/^\/api\/v0\//, '');
|
|
103
|
+
const headers: Record<string, string> = {};
|
|
104
|
+
for (const [k, v] of Object.entries(init?.headers ?? {})) {
|
|
105
|
+
headers[k.toLowerCase()] = v;
|
|
106
|
+
}
|
|
107
|
+
const {bodyText, contentType, fileParts} = await inspectBody(
|
|
108
|
+
init?.body,
|
|
109
|
+
headers,
|
|
110
|
+
);
|
|
111
|
+
this.requests.push({
|
|
112
|
+
method: init?.method ?? 'GET',
|
|
113
|
+
path,
|
|
114
|
+
query: new URLSearchParams(url.search),
|
|
115
|
+
headers,
|
|
116
|
+
bodyText,
|
|
117
|
+
contentType,
|
|
118
|
+
fileParts,
|
|
119
|
+
url: url.toString(),
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const spec = this.responses.get(path);
|
|
123
|
+
const status = spec?.status ?? 200;
|
|
124
|
+
if (spec?.text !== undefined) {
|
|
125
|
+
return new Response(spec.text, {status});
|
|
126
|
+
}
|
|
127
|
+
const payload = spec?.json ?? {};
|
|
128
|
+
return new Response(JSON.stringify(payload), {
|
|
129
|
+
status,
|
|
130
|
+
headers: {'content-type': 'application/json'},
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Inspect a fetch body the way a real `fetch` would surface it to Kubo:
|
|
137
|
+
* derive the effective `content-type` and, for a `multipart/form-data`
|
|
138
|
+
* (`FormData`) body, extract the file parts. This is what lets the mock
|
|
139
|
+
* ENFORCE Kubo's real upload contract (multipart + a `file` part) instead of
|
|
140
|
+
* merely recording raw bytes — the fidelity gap that let raw octet-stream
|
|
141
|
+
* uploads ship and then fail against a live daemon.
|
|
142
|
+
*/
|
|
143
|
+
async function inspectBody(
|
|
144
|
+
body: unknown,
|
|
145
|
+
headers: Record<string, string>,
|
|
146
|
+
): Promise<{
|
|
147
|
+
bodyText: string;
|
|
148
|
+
contentType?: string;
|
|
149
|
+
fileParts?: RecordedFilePart[];
|
|
150
|
+
}> {
|
|
151
|
+
if (body instanceof FormData) {
|
|
152
|
+
// Real `fetch` serialises a FormData as `multipart/form-data; boundary=…`
|
|
153
|
+
// and IGNORES any hand-set content-type. Model that here.
|
|
154
|
+
const fileParts: RecordedFilePart[] = [];
|
|
155
|
+
const textLines: string[] = [];
|
|
156
|
+
// This TS `lib` (dom) types FormData without `entries()`/an iterator, so
|
|
157
|
+
// collect the parts via `forEach` (which IS typed) then process them.
|
|
158
|
+
const entries: Array<[string, FormDataEntryValue]> = [];
|
|
159
|
+
body.forEach((value, field) => {
|
|
160
|
+
entries.push([field, value]);
|
|
161
|
+
});
|
|
162
|
+
for (const [field, value] of entries) {
|
|
163
|
+
if (value instanceof Blob) {
|
|
164
|
+
const bytes = new Uint8Array(await value.arrayBuffer());
|
|
165
|
+
const filename =
|
|
166
|
+
typeof (value as File).name === 'string'
|
|
167
|
+
? (value as File).name
|
|
168
|
+
: undefined;
|
|
169
|
+
fileParts.push({field, filename, bytes});
|
|
170
|
+
} else {
|
|
171
|
+
textLines.push(`${field}=${String(value)}`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
bodyText: textLines.join('&'),
|
|
176
|
+
contentType: 'multipart/form-data',
|
|
177
|
+
fileParts,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
bodyText: await bodyToText(body),
|
|
182
|
+
contentType: headers['content-type'],
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Best-effort stringify of a non-multipart fetch body for recording. */
|
|
187
|
+
async function bodyToText(body: unknown): Promise<string> {
|
|
188
|
+
if (body === undefined || body === null) return '';
|
|
189
|
+
if (typeof body === 'string') return body;
|
|
190
|
+
if (body instanceof Blob) return await body.text();
|
|
191
|
+
if (body instanceof URLSearchParams) return body.toString();
|
|
192
|
+
if (body instanceof Uint8Array) return Buffer.from(body).toString('utf8');
|
|
193
|
+
return String(body);
|
|
194
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* First-class **site management** — the `site` namespace (spec user stories 4,
|
|
3
|
+
* 15; CONTEXT.md `gateway warming`). Sites are auto-discovered from MFS
|
|
4
|
+
* `/sites/*` (that is how `gateway warming`, IPNS republish, and `status` find
|
|
5
|
+
* them), so managing the sites a node serves = managing those MFS entries + the
|
|
6
|
+
* pins that back them. This module gives the operator explicit verbs for that
|
|
7
|
+
* lifecycle instead of leaving it an implicit side-effect of `deploy`:
|
|
8
|
+
*
|
|
9
|
+
* - **list** — enumerate `/sites/*` with each site's current CID and, when a
|
|
10
|
+
* same-`id` keystore key exists, its IPNS id.
|
|
11
|
+
* - **remove** — `files/rm /sites/<id>` (the entry drops out of
|
|
12
|
+
* warm/republish/status auto-discovery) AND `pin/rm <cid>` so
|
|
13
|
+
* the content stops being served/announced and its storage is
|
|
14
|
+
* reclaimed.
|
|
15
|
+
* - **add** — place an already-imported `/ipfs/<cid>` into MFS
|
|
16
|
+
* `/sites/<id>` (mkdir parents / rm old / cp). See the
|
|
17
|
+
* DESIGN NOTE below on its relationship to `deploy`.
|
|
18
|
+
*
|
|
19
|
+
* Every verb speaks ONLY the Kubo RPC seam (MFS + pin endpoints), so the same
|
|
20
|
+
* core is usable both as a TypeScript API and behind the thin `pinnace site
|
|
21
|
+
* <verb>` CLI (CONTEXT.md `core vs cli`). Tests drive it through the recording
|
|
22
|
+
* `MockKuboApi`, never a live daemon (spec Testing Decisions).
|
|
23
|
+
*
|
|
24
|
+
* DESIGN NOTE (add vs deploy) — recorded per the task's "decide during build".
|
|
25
|
+
* `add` is a DISTINCT verb, not an alias over `deploy`. `deploy` (see
|
|
26
|
+
* `deploy-multi-target`) builds a fresh CAR, imports+pins it on every node, and
|
|
27
|
+
* THEN performs this MFS-placement step. `add` is exactly that final placement
|
|
28
|
+
* step in isolation: it takes an EXISTING CID (already imported/pinned, e.g. a
|
|
29
|
+
* known-good historical CID, or one imported out of band) and makes the node
|
|
30
|
+
* serve it under a site name, WITHOUT building or importing a CAR. Adding a site
|
|
31
|
+
* from an existing CID is a meaningful operation deploy does not cover (no fresh
|
|
32
|
+
* artifact), so it earns its own verb. Deploy is expected to REUSE
|
|
33
|
+
* {@link placeInMfs} for its placement step when it lands, keeping a single
|
|
34
|
+
* implementation of the mkdir/rm/cp sequence rather than forking it. This is
|
|
35
|
+
* NOT a new domain concept: `add` sits at the same layer as the other explicit
|
|
36
|
+
* client verbs and reuses the existing `/sites/<name>` MFS convention.
|
|
37
|
+
*/
|
|
38
|
+
import type {KuboRpcClient} from '../rpc/kubo-rpc-client.js';
|
|
39
|
+
|
|
40
|
+
/** The three site-management verbs, under the `site` namespace. */
|
|
41
|
+
export type SiteVerb = 'list' | 'remove' | 'add';
|
|
42
|
+
|
|
43
|
+
/** The verbs, in a stable order (for help text / iteration). */
|
|
44
|
+
export const SITE_VERBS: readonly SiteVerb[] = ['list', 'remove', 'add'];
|
|
45
|
+
|
|
46
|
+
/** The MFS directory sites live under. */
|
|
47
|
+
const DEFAULT_SITES_DIR = '/sites';
|
|
48
|
+
|
|
49
|
+
/** One site as listed: its MFS `id`, current CID, and IPNS id if a key exists. */
|
|
50
|
+
export interface SiteListing {
|
|
51
|
+
/** The site's single `id` (its MFS entry under `/sites/`). */
|
|
52
|
+
id: string;
|
|
53
|
+
/** The current content root CID (`files/stat --hash`). */
|
|
54
|
+
cid: string;
|
|
55
|
+
/** The IPNS id, if a same-`id` keystore key exists (ipfs-mode sites lack one). */
|
|
56
|
+
ipns?: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Inputs shared by every site-management verb. */
|
|
60
|
+
interface SiteBaseInput {
|
|
61
|
+
/** The Kubo RPC client for the node whose sites are being managed. */
|
|
62
|
+
client: KuboRpcClient;
|
|
63
|
+
/** The MFS directory sites live under (default `/sites`). */
|
|
64
|
+
sitesDir?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Inputs to {@link listSites}. */
|
|
68
|
+
export type ListSitesInput = SiteBaseInput;
|
|
69
|
+
|
|
70
|
+
/** Inputs to {@link removeSite}. */
|
|
71
|
+
export interface RemoveSiteInput extends SiteBaseInput {
|
|
72
|
+
/** The site `id` (its MFS entry `/sites/<id>`) to remove. */
|
|
73
|
+
id: string;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The outcome of {@link removeSite}. */
|
|
77
|
+
export interface RemoveSiteResult {
|
|
78
|
+
/** The site `id` that was removed. */
|
|
79
|
+
id: string;
|
|
80
|
+
/** The CID that backed it (undefined if it had no resolvable content). */
|
|
81
|
+
cid?: string;
|
|
82
|
+
/** True when the content was unpinned (storage reclaimed); false if unpin failed. */
|
|
83
|
+
unpinned: boolean;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Inputs to {@link addSite}. */
|
|
87
|
+
export interface AddSiteInput extends SiteBaseInput {
|
|
88
|
+
/** The site `id` to expose it under (its MFS entry `/sites/<id>`). */
|
|
89
|
+
id: string;
|
|
90
|
+
/** The already-imported content root CID to place into MFS. */
|
|
91
|
+
cid: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The outcome of {@link addSite}. */
|
|
95
|
+
export interface AddSiteResult {
|
|
96
|
+
/** The site `id` that was placed. */
|
|
97
|
+
id: string;
|
|
98
|
+
/** The CID that now backs it in MFS. */
|
|
99
|
+
cid: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* **list** — enumerate the sites the node currently serves (MFS `/sites/*`),
|
|
104
|
+
* annotating each with its current CID (from `files/stat`) and, when a keystore
|
|
105
|
+
* key of the same name exists, its IPNS id (from `key/list -l`). A fresh box
|
|
106
|
+
* with no `/sites` dir yields an empty list, not an error.
|
|
107
|
+
*/
|
|
108
|
+
export async function listSites(input: ListSitesInput): Promise<SiteListing[]> {
|
|
109
|
+
const sitesDir = input.sitesDir ?? DEFAULT_SITES_DIR;
|
|
110
|
+
const entries = await lsSites(input.client, sitesDir);
|
|
111
|
+
if (entries.length === 0) return [];
|
|
112
|
+
|
|
113
|
+
const keys = await listKeys(input.client);
|
|
114
|
+
const sites: SiteListing[] = [];
|
|
115
|
+
for (const id of entries) {
|
|
116
|
+
const cid = await statCid(input.client, `${sitesDir}/${id}`);
|
|
117
|
+
if (!cid) continue; // an entry with no resolvable CID is skipped, not fatal.
|
|
118
|
+
sites.push({id, cid, ipns: keys.get(id)});
|
|
119
|
+
}
|
|
120
|
+
return sites;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* **remove** — delete a site: `files/rm /sites/<name>` FIRST (so it immediately
|
|
125
|
+
* drops out of MFS auto-discovery and stops being served/announced/warmed),
|
|
126
|
+
* THEN `pin/rm <cid>` to unpin the content so its storage is reclaimed. The CID
|
|
127
|
+
* is resolved up-front with `files/stat` so we know what to unpin after the
|
|
128
|
+
* entry is gone.
|
|
129
|
+
*
|
|
130
|
+
* The MFS removal is the load-bearing step and is always attempted. The unpin
|
|
131
|
+
* is best-effort: if the content was never pinned (or is pinned indirectly),
|
|
132
|
+
* `pin/rm` errors, which is REPORTED (`unpinned: false`) rather than thrown, so
|
|
133
|
+
* a partially-pinned site still removes cleanly.
|
|
134
|
+
*/
|
|
135
|
+
export async function removeSite(
|
|
136
|
+
input: RemoveSiteInput,
|
|
137
|
+
): Promise<RemoveSiteResult> {
|
|
138
|
+
const sitesDir = input.sitesDir ?? DEFAULT_SITES_DIR;
|
|
139
|
+
const path = `${sitesDir}/${input.id}`;
|
|
140
|
+
|
|
141
|
+
// Resolve the CID before we remove the entry (afterwards it is gone).
|
|
142
|
+
const cid = await statCid(input.client, path);
|
|
143
|
+
|
|
144
|
+
// Remove the MFS entry first: it stops being discovered/served/announced.
|
|
145
|
+
await input.client.filesRm(path, {recursive: true, force: true});
|
|
146
|
+
|
|
147
|
+
// Then reclaim storage by unpinning the content. Best-effort: a site that
|
|
148
|
+
// was never pinned (or pinned indirectly) must not fail the removal.
|
|
149
|
+
let unpinned = false;
|
|
150
|
+
if (cid) {
|
|
151
|
+
try {
|
|
152
|
+
await input.client.pinRm(cid);
|
|
153
|
+
unpinned = true;
|
|
154
|
+
} catch {
|
|
155
|
+
unpinned = false;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return {id: input.id, cid, unpinned};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* **add** — expose an existing CID as a served site by placing it into MFS at
|
|
164
|
+
* `/sites/<name>` (the discoverable location warm/republish/status read). This
|
|
165
|
+
* is deploy's MFS-placement step in isolation (see the module DESIGN NOTE): it
|
|
166
|
+
* does NOT build or import a CAR and does NOT pin (the CID is assumed already
|
|
167
|
+
* imported/pinned on the node).
|
|
168
|
+
*/
|
|
169
|
+
export async function addSite(input: AddSiteInput): Promise<AddSiteResult> {
|
|
170
|
+
const sitesDir = input.sitesDir ?? DEFAULT_SITES_DIR;
|
|
171
|
+
await placeInMfs(input.client, sitesDir, input.id, input.cid);
|
|
172
|
+
return {id: input.id, cid: input.cid};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The MFS-placement step: `files/mkdir /sites --parents`, `files/rm
|
|
177
|
+
* /sites/<id> --recursive --force` (clear any prior content), then `files/cp
|
|
178
|
+
* /ipfs/<cid> /sites/<id>`. Ported from the reference prototype's deploy MFS
|
|
179
|
+
* placement (`~/searches/ipfs-hetzner/deploy-car.mjs`). Exported so `deploy`
|
|
180
|
+
* can REUSE this exact sequence rather than forking it.
|
|
181
|
+
*/
|
|
182
|
+
export async function placeInMfs(
|
|
183
|
+
client: KuboRpcClient,
|
|
184
|
+
sitesDir: string,
|
|
185
|
+
id: string,
|
|
186
|
+
cid: string,
|
|
187
|
+
): Promise<void> {
|
|
188
|
+
await client.filesMkdir(sitesDir, {parents: true});
|
|
189
|
+
await client.filesRm(`${sitesDir}/${id}`, {recursive: true, force: true});
|
|
190
|
+
await client.filesCp(`/ipfs/${cid}`, `${sitesDir}/${id}`);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// ---------------------------------------------------------------------------
|
|
194
|
+
// Small shared Kubo reads (fail-soft where a missing dir is not an error).
|
|
195
|
+
// ---------------------------------------------------------------------------
|
|
196
|
+
|
|
197
|
+
/** List the entry NAMES under the sites dir; empty on a fresh box (no dir). */
|
|
198
|
+
async function lsSites(
|
|
199
|
+
client: KuboRpcClient,
|
|
200
|
+
sitesDir: string,
|
|
201
|
+
): Promise<string[]> {
|
|
202
|
+
let listing: {Entries?: Array<{Name?: string}> | null};
|
|
203
|
+
try {
|
|
204
|
+
listing = await client.filesLs<{Entries?: Array<{Name?: string}> | null}>(
|
|
205
|
+
sitesDir,
|
|
206
|
+
);
|
|
207
|
+
} catch {
|
|
208
|
+
// No /sites dir yet (fresh box) — nothing to list, not an error.
|
|
209
|
+
return [];
|
|
210
|
+
}
|
|
211
|
+
const names: string[] = [];
|
|
212
|
+
for (const entry of listing.Entries ?? []) {
|
|
213
|
+
if (entry?.Name) names.push(entry.Name);
|
|
214
|
+
}
|
|
215
|
+
return names;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Stat an MFS path for its current CID, or undefined if it cannot be resolved. */
|
|
219
|
+
async function statCid(
|
|
220
|
+
client: KuboRpcClient,
|
|
221
|
+
path: string,
|
|
222
|
+
): Promise<string | undefined> {
|
|
223
|
+
try {
|
|
224
|
+
const stat = await client.filesStat<{Hash?: string}>(path);
|
|
225
|
+
return stat?.Hash;
|
|
226
|
+
} catch {
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Map site/key name -> IPNS id from `key/list -l` (same shape as node-commands). */
|
|
232
|
+
async function listKeys(client: KuboRpcClient): Promise<Map<string, string>> {
|
|
233
|
+
const res = await client.keyList<{
|
|
234
|
+
Keys?: Array<{Name?: string; Id?: string}> | null;
|
|
235
|
+
}>();
|
|
236
|
+
const map = new Map<string, string>();
|
|
237
|
+
for (const k of res.Keys ?? []) {
|
|
238
|
+
if (k?.Name && k?.Id) map.set(k.Name, k.Id);
|
|
239
|
+
}
|
|
240
|
+
return map;
|
|
241
|
+
}
|