@ipab/cli 0.1.0-rc.11 → 0.1.0-rc.12
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 +15 -10
- package/dist/authoring.d.ts +6 -2
- package/dist/authoring.d.ts.map +1 -1
- package/dist/authoring.js +65 -18
- package/dist/authoring.js.map +1 -1
- package/dist/builtins/skill.ipab.car +0 -0
- package/dist/cli.d.ts +2 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +854 -179
- package/dist/cli.js.map +1 -1
- package/dist/config-secret.js +1 -1
- package/dist/config-secret.js.map +1 -1
- package/dist/daemon-ui.d.ts +1 -0
- package/dist/daemon-ui.d.ts.map +1 -1
- package/dist/daemon-ui.js +2 -0
- package/dist/daemon-ui.js.map +1 -1
- package/dist/history.d.ts +3 -1
- package/dist/history.d.ts.map +1 -1
- package/dist/history.js +16 -1
- package/dist/history.js.map +1 -1
- package/dist/kubo-topology.d.ts +2 -0
- package/dist/kubo-topology.d.ts.map +1 -1
- package/dist/kubo-topology.js +17 -20
- package/dist/kubo-topology.js.map +1 -1
- package/dist/kubo.d.ts +5 -3
- package/dist/kubo.d.ts.map +1 -1
- package/dist/kubo.js +83 -13
- package/dist/kubo.js.map +1 -1
- package/dist/name-reference.d.ts +8 -0
- package/dist/name-reference.d.ts.map +1 -0
- package/dist/name-reference.js +63 -0
- package/dist/name-reference.js.map +1 -0
- package/dist/pivot-rpc.d.ts +37 -2
- package/dist/pivot-rpc.d.ts.map +1 -1
- package/dist/pivot-rpc.js +188 -8
- package/dist/pivot-rpc.js.map +1 -1
- package/dist/search-index.d.ts.map +1 -1
- package/dist/search-index.js +1 -0
- package/dist/search-index.js.map +1 -1
- package/dist/skill-definition.d.ts +9 -0
- package/dist/skill-definition.d.ts.map +1 -1
- package/dist/skill-definition.js +26 -2
- package/dist/skill-definition.js.map +1 -1
- package/dist/skill-source.d.ts +1 -0
- package/dist/skill-source.d.ts.map +1 -1
- package/dist/skill-source.js +10 -4
- package/dist/skill-source.js.map +1 -1
- package/dist/skills.d.ts +8 -2
- package/dist/skills.d.ts.map +1 -1
- package/dist/skills.js +31 -2
- package/dist/skills.js.map +1 -1
- package/dist/swarm-fs-core.d.ts +217 -0
- package/dist/swarm-fs-core.d.ts.map +1 -0
- package/dist/swarm-fs-core.js +885 -0
- package/dist/swarm-fs-core.js.map +1 -0
- package/dist/swarm-fs-path.d.ts +17 -0
- package/dist/swarm-fs-path.d.ts.map +1 -0
- package/dist/swarm-fs-path.js +46 -0
- package/dist/swarm-fs-path.js.map +1 -0
- package/dist/swarm-fs.d.ts +9 -0
- package/dist/swarm-fs.d.ts.map +1 -0
- package/dist/swarm-fs.js +21 -0
- package/dist/swarm-fs.js.map +1 -0
- package/dist/swarm-observations.d.ts +6 -0
- package/dist/swarm-observations.d.ts.map +1 -1
- package/dist/swarm-observations.js +28 -8
- package/dist/swarm-observations.js.map +1 -1
- package/dist/swarm.d.ts +124 -17
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +1085 -450
- package/dist/swarm.js.map +1 -1
- package/dist/unixfs-tree.d.ts +55 -0
- package/dist/unixfs-tree.d.ts.map +1 -1
- package/dist/unixfs-tree.js +361 -2
- package/dist/unixfs-tree.js.map +1 -1
- package/package.json +5 -5
- package/src/authoring.ts +88 -28
- package/src/cli.ts +1142 -267
- package/src/config-secret.ts +1 -1
- package/src/daemon-ui.ts +3 -0
- package/src/history.ts +26 -1
- package/src/kubo-topology.ts +25 -22
- package/src/kubo.ts +127 -9
- package/src/name-reference.ts +91 -0
- package/src/pivot-rpc.ts +272 -14
- package/src/search-index.ts +1 -0
- package/src/skill-definition.ts +45 -3
- package/src/skill-source.ts +10 -4
- package/src/skills.ts +43 -2
- package/src/swarm-fs-core.ts +1461 -0
- package/src/swarm-fs-path.ts +68 -0
- package/src/swarm-fs.ts +104 -0
- package/src/swarm-observations.ts +40 -14
- package/src/swarm.ts +1538 -656
- package/src/unixfs-tree.ts +447 -2
|
@@ -0,0 +1,885 @@
|
|
|
1
|
+
import * as dagCbor from "@ipld/dag-cbor";
|
|
2
|
+
import * as dagPb from "@ipld/dag-pb";
|
|
3
|
+
import { UnixFS } from "ipfs-unixfs";
|
|
4
|
+
import { CID } from "multiformats/cid";
|
|
5
|
+
import { ScopeApiError, } from "@ipab/bundle/pointer/scope";
|
|
6
|
+
import { isLocalSwarm } from "./config.js";
|
|
7
|
+
import { provideShallowContent } from "./content-provider.js";
|
|
8
|
+
import { HostError, message, syntax } from "./errors.js";
|
|
9
|
+
import { exactCid } from "./state.js";
|
|
10
|
+
import { normalizeSwarmFsPath, } from "./swarm-fs-path.js";
|
|
11
|
+
import { applyUnixfsMutations, decodeUnixfsDirectory, listUnixfsDirectory, listUnixfsDirectoryLinks, resolveUnixfsPath, walkUnixfsNames, walkUnixfsTree, } from "./unixfs-tree.js";
|
|
12
|
+
export { normalizeSwarmFsPath };
|
|
13
|
+
export function isV7Snapshot(snapshot) {
|
|
14
|
+
return snapshot.stateBundle?.unixfsRoot?.version === 7;
|
|
15
|
+
}
|
|
16
|
+
export function v7RootSkipFiles(snapshot) {
|
|
17
|
+
const root = snapshot.stateBundle?.unixfsRoot;
|
|
18
|
+
if (!root)
|
|
19
|
+
return new Set();
|
|
20
|
+
return new Set([".bundle.yaml", root.typeFile, root.entryFile]);
|
|
21
|
+
}
|
|
22
|
+
export async function resolveSnapshotName(store, snapshot, name) {
|
|
23
|
+
if (isV7Snapshot(snapshot))
|
|
24
|
+
return resolveUnixfsPath(store, CID.parse(snapshot.stateBundle.cid), name);
|
|
25
|
+
return Object.hasOwn(snapshot.names, name) ? snapshot.names[name] : undefined;
|
|
26
|
+
}
|
|
27
|
+
export async function listSubtreeNames(store, snapshot, prefix, flatOnly = false) {
|
|
28
|
+
if (isV7Snapshot(snapshot)) {
|
|
29
|
+
const dirCid = await resolveUnixfsPath(store, CID.parse(snapshot.stateBundle.cid), prefix);
|
|
30
|
+
if (!dirCid)
|
|
31
|
+
return [];
|
|
32
|
+
if (flatOnly) {
|
|
33
|
+
const entries = await listUnixfsDirectory(store, dirCid);
|
|
34
|
+
return [...entries.entries()]
|
|
35
|
+
.map(([child, cid]) => ({ name: `${prefix}/${child}`, cid }))
|
|
36
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
37
|
+
}
|
|
38
|
+
const walked = await walkUnixfsNames(store, dirCid, {
|
|
39
|
+
prefix,
|
|
40
|
+
stopAtBundle: true,
|
|
41
|
+
});
|
|
42
|
+
return walked.entries.map((entry) => ({
|
|
43
|
+
name: entry.name,
|
|
44
|
+
cid: CID.parse(entry.targetCid),
|
|
45
|
+
}));
|
|
46
|
+
}
|
|
47
|
+
const needle = `${prefix}/`;
|
|
48
|
+
return Object.entries(snapshot.names)
|
|
49
|
+
.filter(([name]) => name.startsWith(needle))
|
|
50
|
+
.map(([name, cid]) => ({ name, cid }))
|
|
51
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns the set of protected root metadata filenames for a v7 snapshot
|
|
55
|
+
* (`.bundle.yaml`, `root.typeFile`, `root.entryFile`). Returns an empty set
|
|
56
|
+
* for legacy (v2-v6) snapshots.
|
|
57
|
+
*/
|
|
58
|
+
export function protectedRootNames(snapshot) {
|
|
59
|
+
if (!isV7Snapshot(snapshot))
|
|
60
|
+
return new Set();
|
|
61
|
+
return v7RootSkipFiles(snapshot);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Guards v7 Swarm Root Bundle metadata files from being written, overwritten,
|
|
65
|
+
* deleted, or used as a top-level directory prefix.
|
|
66
|
+
*/
|
|
67
|
+
export function assertWritableSwarmFsPath(snapshot, normalized) {
|
|
68
|
+
if (!isV7Snapshot(snapshot))
|
|
69
|
+
return;
|
|
70
|
+
if (normalized.isRoot) {
|
|
71
|
+
throw new HostError("Cannot modify the Swarm root directory directly.", 1, "PROTECTED_PATH", "state");
|
|
72
|
+
}
|
|
73
|
+
const firstSegment = normalized.segments[0];
|
|
74
|
+
const protectedNames = protectedRootNames(snapshot);
|
|
75
|
+
if (protectedNames.has(firstSegment)) {
|
|
76
|
+
throw new HostError(`Path "${normalized.path}" targets protected Swarm root metadata "${firstSegment}".`, 1, "PROTECTED_PATH", "state");
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function inspectBlockMetadata(cid, bytes) {
|
|
80
|
+
if (cid.code === 0x55) {
|
|
81
|
+
return { kind: "raw", size: bytes.byteLength };
|
|
82
|
+
}
|
|
83
|
+
if (cid.code === dagCbor.code) {
|
|
84
|
+
return { kind: "cbor", size: bytes.byteLength };
|
|
85
|
+
}
|
|
86
|
+
if (cid.code === dagPb.code) {
|
|
87
|
+
let pbNode;
|
|
88
|
+
try {
|
|
89
|
+
pbNode = dagPb.decode(bytes);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return { kind: "other", size: bytes.byteLength };
|
|
93
|
+
}
|
|
94
|
+
const totalSize = bytes.byteLength +
|
|
95
|
+
pbNode.Links.reduce((sum, link) => sum + (link.Tsize ?? 0), 0);
|
|
96
|
+
try {
|
|
97
|
+
const dir = decodeUnixfsDirectory(bytes);
|
|
98
|
+
return {
|
|
99
|
+
kind: "directory",
|
|
100
|
+
size: totalSize,
|
|
101
|
+
entryCount: dir.links.size,
|
|
102
|
+
isBundle: dir.links.has(".bundle.yaml"),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
if (pbNode.Data) {
|
|
107
|
+
try {
|
|
108
|
+
const unixfs = UnixFS.unmarshal(pbNode.Data);
|
|
109
|
+
if (unixfs.type === "file" || unixfs.type === "raw")
|
|
110
|
+
return { kind: "file", size: totalSize };
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
// fall through to other
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return { kind: "other", size: totalSize };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return { kind: "other", size: bytes.byteLength };
|
|
120
|
+
}
|
|
121
|
+
function legacyDirectEntries(names, normalized) {
|
|
122
|
+
const allEntries = Object.entries(names).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
123
|
+
if (normalized.isRoot) {
|
|
124
|
+
const dirs = new Map();
|
|
125
|
+
const leaves = new Map();
|
|
126
|
+
for (const [name, cid] of allEntries) {
|
|
127
|
+
const slash = name.indexOf("/");
|
|
128
|
+
if (slash < 0) {
|
|
129
|
+
leaves.set(name, {
|
|
130
|
+
name,
|
|
131
|
+
path: name,
|
|
132
|
+
cid: cid.toV1().toString(),
|
|
133
|
+
size: null,
|
|
134
|
+
kind: "link",
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
const seg = name.slice(0, slash);
|
|
139
|
+
if (!dirs.has(seg)) {
|
|
140
|
+
dirs.set(seg, {
|
|
141
|
+
name: seg,
|
|
142
|
+
path: seg,
|
|
143
|
+
cid: null,
|
|
144
|
+
size: null,
|
|
145
|
+
kind: "directory",
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const combined = [...dirs.values(), ...leaves.values()].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
151
|
+
return combined;
|
|
152
|
+
}
|
|
153
|
+
const needle = `${normalized.path}/`;
|
|
154
|
+
const matching = allEntries.filter(([name]) => name.startsWith(needle));
|
|
155
|
+
if (matching.length === 0)
|
|
156
|
+
return undefined;
|
|
157
|
+
const dirs = new Map();
|
|
158
|
+
const leaves = new Map();
|
|
159
|
+
for (const [name, cid] of matching) {
|
|
160
|
+
const rest = name.slice(needle.length);
|
|
161
|
+
const slash = rest.indexOf("/");
|
|
162
|
+
if (slash < 0) {
|
|
163
|
+
leaves.set(rest, {
|
|
164
|
+
name: rest,
|
|
165
|
+
path: name,
|
|
166
|
+
cid: cid.toV1().toString(),
|
|
167
|
+
size: null,
|
|
168
|
+
kind: "link",
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
const seg = rest.slice(0, slash);
|
|
173
|
+
if (!dirs.has(seg)) {
|
|
174
|
+
dirs.set(seg, {
|
|
175
|
+
name: seg,
|
|
176
|
+
path: `${normalized.path}/${seg}`,
|
|
177
|
+
cid: null,
|
|
178
|
+
size: null,
|
|
179
|
+
kind: "directory",
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return [...dirs.values(), ...leaves.values()].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
185
|
+
}
|
|
186
|
+
export function createSwarmFs(transport) {
|
|
187
|
+
function buildSnapshotContext(context, configUrl, header, warm) {
|
|
188
|
+
const isV7 = isV7Snapshot(header.snapshot);
|
|
189
|
+
const isLocal = isLocalSwarm(configUrl);
|
|
190
|
+
const rootCid = isV7
|
|
191
|
+
? CID.parse(header.snapshot.stateBundle.cid).toV1().toString()
|
|
192
|
+
: header.stateCid;
|
|
193
|
+
return {
|
|
194
|
+
configUrl,
|
|
195
|
+
header,
|
|
196
|
+
snapshot: header.snapshot,
|
|
197
|
+
rootCid,
|
|
198
|
+
isV7,
|
|
199
|
+
isLocal,
|
|
200
|
+
source: header.source,
|
|
201
|
+
verifiedAt: header.verifiedAt,
|
|
202
|
+
ensureLocalSource: async () => {
|
|
203
|
+
if (warm &&
|
|
204
|
+
header === warm.header &&
|
|
205
|
+
!header.localSourceReady &&
|
|
206
|
+
!isLocal) {
|
|
207
|
+
await transport.lazyPrivateSource(context, configUrl, warm.account, header.config);
|
|
208
|
+
header.localSourceReady = true;
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
async function readSwarmFsSnapshot(context, configUrl, options) {
|
|
214
|
+
const mode = options?.mode ?? "active";
|
|
215
|
+
const preferLocalCache = options?.preferLocalCache ?? mode === "local-first";
|
|
216
|
+
const isLocal = isLocalSwarm(configUrl);
|
|
217
|
+
const prevForceRefresh = context.forceRefresh;
|
|
218
|
+
const hasExplicitRefresh = options?.forceRefresh !== undefined;
|
|
219
|
+
if (options?.forceRefresh !== undefined)
|
|
220
|
+
context.forceRefresh = options.forceRefresh;
|
|
221
|
+
let warm;
|
|
222
|
+
let header;
|
|
223
|
+
try {
|
|
224
|
+
if (options?.forceRefresh === true) {
|
|
225
|
+
header = await transport.swarmHeader(context, configUrl, true, mode);
|
|
226
|
+
}
|
|
227
|
+
else if (preferLocalCache) {
|
|
228
|
+
warm = await transport.localHeader(context, configUrl);
|
|
229
|
+
header =
|
|
230
|
+
warm?.header ??
|
|
231
|
+
(await transport.swarmHeader(context, configUrl, options?.forceRefresh ?? prevForceRefresh ?? false, mode));
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
header = await transport.swarmHeader(context, configUrl, options?.forceRefresh ?? prevForceRefresh ?? false, mode);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
finally {
|
|
238
|
+
if (hasExplicitRefresh) {
|
|
239
|
+
if (prevForceRefresh === undefined)
|
|
240
|
+
delete context.forceRefresh;
|
|
241
|
+
else
|
|
242
|
+
context.forceRefresh = prevForceRefresh;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
let ctx = buildSnapshotContext(context, configUrl, header, warm);
|
|
246
|
+
if (options?.shouldRefresh &&
|
|
247
|
+
ctx.source === "cache" &&
|
|
248
|
+
!isLocal &&
|
|
249
|
+
options.forceRefresh !== false &&
|
|
250
|
+
(await options.shouldRefresh(ctx))) {
|
|
251
|
+
header = await transport.swarmHeader(context, configUrl, true, mode);
|
|
252
|
+
ctx = buildSnapshotContext(context, configUrl, header, undefined);
|
|
253
|
+
}
|
|
254
|
+
else if (options?.preferLocalCache === true &&
|
|
255
|
+
ctx.source === "cache" &&
|
|
256
|
+
!isLocal) {
|
|
257
|
+
await ctx.ensureLocalSource();
|
|
258
|
+
}
|
|
259
|
+
return ctx;
|
|
260
|
+
}
|
|
261
|
+
async function getBlockWithRetry(context, ctx, cid) {
|
|
262
|
+
const key = cid.toV1().toString();
|
|
263
|
+
try {
|
|
264
|
+
return await context.store.block.get(key);
|
|
265
|
+
}
|
|
266
|
+
catch (cause) {
|
|
267
|
+
if (transport.missingContent(cause) &&
|
|
268
|
+
!ctx.isLocal &&
|
|
269
|
+
!ctx.header.localSourceReady) {
|
|
270
|
+
await ctx.ensureLocalSource();
|
|
271
|
+
return context.store.block.get(key);
|
|
272
|
+
}
|
|
273
|
+
throw cause;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
async function resolveV7PathWithRetry(context, ctx, normalized) {
|
|
277
|
+
const rootCid = CID.parse(ctx.rootCid);
|
|
278
|
+
if (normalized.isRoot)
|
|
279
|
+
return rootCid;
|
|
280
|
+
try {
|
|
281
|
+
return await resolveUnixfsPath(context.store, rootCid, normalized.path);
|
|
282
|
+
}
|
|
283
|
+
catch (cause) {
|
|
284
|
+
if (transport.missingContent(cause) &&
|
|
285
|
+
!ctx.isLocal &&
|
|
286
|
+
!ctx.header.localSourceReady) {
|
|
287
|
+
await ctx.ensureLocalSource();
|
|
288
|
+
return resolveUnixfsPath(context.store, rootCid, normalized.path);
|
|
289
|
+
}
|
|
290
|
+
throw cause;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
async function statSwarmFsPath(context, configUrl, path, options) {
|
|
294
|
+
const normalized = normalizeSwarmFsPath(path, {
|
|
295
|
+
allowRoot: true,
|
|
296
|
+
allowTrailingSlash: true,
|
|
297
|
+
});
|
|
298
|
+
const mode = options?.mode ?? "local-first";
|
|
299
|
+
let ctx = await readSwarmFsSnapshot(context, configUrl, {
|
|
300
|
+
mode,
|
|
301
|
+
...(options?.forceRefresh !== undefined
|
|
302
|
+
? { forceRefresh: options.forceRefresh }
|
|
303
|
+
: {}),
|
|
304
|
+
});
|
|
305
|
+
const statFromContext = async (currentCtx) => {
|
|
306
|
+
if (currentCtx.isV7) {
|
|
307
|
+
const targetCid = await resolveV7PathWithRetry(context, currentCtx, normalized);
|
|
308
|
+
if (!targetCid)
|
|
309
|
+
return undefined;
|
|
310
|
+
const canonical = targetCid.toV1();
|
|
311
|
+
const bytes = await getBlockWithRetry(context, currentCtx, canonical);
|
|
312
|
+
const info = inspectBlockMetadata(canonical, bytes);
|
|
313
|
+
return {
|
|
314
|
+
swarm: configUrl,
|
|
315
|
+
headCid: currentCtx.header.head.cid,
|
|
316
|
+
rootCid: currentCtx.rootCid,
|
|
317
|
+
path: normalized.path,
|
|
318
|
+
cid: canonical.toString(),
|
|
319
|
+
kind: info.kind,
|
|
320
|
+
size: info.size,
|
|
321
|
+
...(info.entryCount !== undefined
|
|
322
|
+
? { entryCount: info.entryCount }
|
|
323
|
+
: {}),
|
|
324
|
+
...(info.isBundle !== undefined ? { isBundle: info.isBundle } : {}),
|
|
325
|
+
virtual: false,
|
|
326
|
+
readOnly: false,
|
|
327
|
+
snapshotVersion: "v7",
|
|
328
|
+
source: currentCtx.source,
|
|
329
|
+
verifiedAt: currentCtx.verifiedAt,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
// Legacy v2-v6 read-only virtual projection.
|
|
333
|
+
if (normalized.isRoot) {
|
|
334
|
+
const direct = legacyDirectEntries(currentCtx.snapshot.names, normalized);
|
|
335
|
+
return {
|
|
336
|
+
swarm: configUrl,
|
|
337
|
+
headCid: currentCtx.header.head.cid,
|
|
338
|
+
rootCid: currentCtx.rootCid,
|
|
339
|
+
path: "",
|
|
340
|
+
cid: currentCtx.rootCid,
|
|
341
|
+
kind: "directory",
|
|
342
|
+
size: null,
|
|
343
|
+
entryCount: direct.length,
|
|
344
|
+
virtual: true,
|
|
345
|
+
readOnly: true,
|
|
346
|
+
snapshotVersion: "legacy",
|
|
347
|
+
source: currentCtx.source,
|
|
348
|
+
verifiedAt: currentCtx.verifiedAt,
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
const exact = await resolveSnapshotName(context.store, currentCtx.snapshot, normalized.path);
|
|
352
|
+
if (exact) {
|
|
353
|
+
const canonical = exact.toV1();
|
|
354
|
+
const bytes = await getBlockWithRetry(context, currentCtx, canonical);
|
|
355
|
+
const info = inspectBlockMetadata(canonical, bytes);
|
|
356
|
+
return {
|
|
357
|
+
swarm: configUrl,
|
|
358
|
+
headCid: currentCtx.header.head.cid,
|
|
359
|
+
rootCid: currentCtx.rootCid,
|
|
360
|
+
path: normalized.path,
|
|
361
|
+
cid: canonical.toString(),
|
|
362
|
+
kind: info.kind,
|
|
363
|
+
size: info.size,
|
|
364
|
+
...(info.entryCount !== undefined
|
|
365
|
+
? { entryCount: info.entryCount }
|
|
366
|
+
: {}),
|
|
367
|
+
...(info.isBundle !== undefined ? { isBundle: info.isBundle } : {}),
|
|
368
|
+
virtual: true,
|
|
369
|
+
readOnly: true,
|
|
370
|
+
snapshotVersion: "legacy",
|
|
371
|
+
source: currentCtx.source,
|
|
372
|
+
verifiedAt: currentCtx.verifiedAt,
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
const direct = legacyDirectEntries(currentCtx.snapshot.names, normalized);
|
|
376
|
+
if (direct) {
|
|
377
|
+
return {
|
|
378
|
+
swarm: configUrl,
|
|
379
|
+
headCid: currentCtx.header.head.cid,
|
|
380
|
+
rootCid: currentCtx.rootCid,
|
|
381
|
+
path: normalized.path,
|
|
382
|
+
cid: null,
|
|
383
|
+
kind: "directory",
|
|
384
|
+
size: null,
|
|
385
|
+
entryCount: direct.length,
|
|
386
|
+
virtual: true,
|
|
387
|
+
readOnly: true,
|
|
388
|
+
snapshotVersion: "legacy",
|
|
389
|
+
source: currentCtx.source,
|
|
390
|
+
verifiedAt: currentCtx.verifiedAt,
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
return undefined;
|
|
394
|
+
};
|
|
395
|
+
let result = await statFromContext(ctx);
|
|
396
|
+
if (!result &&
|
|
397
|
+
ctx.source === "cache" &&
|
|
398
|
+
!ctx.isLocal &&
|
|
399
|
+
options?.forceRefresh !== false) {
|
|
400
|
+
ctx = await readSwarmFsSnapshot(context, configUrl, {
|
|
401
|
+
mode,
|
|
402
|
+
forceRefresh: true,
|
|
403
|
+
});
|
|
404
|
+
result = await statFromContext(ctx);
|
|
405
|
+
}
|
|
406
|
+
if (!result) {
|
|
407
|
+
throw new HostError(`Path "${normalized.path}" does not exist.`, 1, "PATH_NOT_FOUND", "state");
|
|
408
|
+
}
|
|
409
|
+
return result;
|
|
410
|
+
}
|
|
411
|
+
async function listSwarmFsPath(context, configUrl, path, options) {
|
|
412
|
+
const normalized = normalizeSwarmFsPath(path, {
|
|
413
|
+
allowRoot: true,
|
|
414
|
+
allowTrailingSlash: true,
|
|
415
|
+
});
|
|
416
|
+
const all = Boolean(options?.all);
|
|
417
|
+
const recursive = Boolean(options?.recursive);
|
|
418
|
+
const mode = options?.mode ?? "local-first";
|
|
419
|
+
let ctx = await readSwarmFsSnapshot(context, configUrl, {
|
|
420
|
+
mode,
|
|
421
|
+
...(options?.forceRefresh !== undefined
|
|
422
|
+
? { forceRefresh: options.forceRefresh }
|
|
423
|
+
: {}),
|
|
424
|
+
});
|
|
425
|
+
const listFromContext = async (currentCtx) => {
|
|
426
|
+
if (currentCtx.isV7) {
|
|
427
|
+
const targetCid = await resolveV7PathWithRetry(context, currentCtx, normalized);
|
|
428
|
+
if (!targetCid)
|
|
429
|
+
return undefined;
|
|
430
|
+
const canonical = targetCid.toV1();
|
|
431
|
+
// Verify target is a UnixFS directory without touching child blocks.
|
|
432
|
+
if (canonical.code !== dagPb.code) {
|
|
433
|
+
throw new HostError(`Path "${normalized.path}" is not a directory. Use ipab fs stat to inspect it.`, 1, "NOT_A_DIRECTORY", "state");
|
|
434
|
+
}
|
|
435
|
+
const dirBytes = await getBlockWithRetry(context, currentCtx, canonical);
|
|
436
|
+
try {
|
|
437
|
+
decodeUnixfsDirectory(dirBytes);
|
|
438
|
+
}
|
|
439
|
+
catch {
|
|
440
|
+
throw new HostError(`Path "${normalized.path}" is not a directory. Use ipab fs stat to inspect it.`, 1, "NOT_A_DIRECTORY", "state");
|
|
441
|
+
}
|
|
442
|
+
const protectedNames = normalized.isRoot
|
|
443
|
+
? protectedRootNames(currentCtx.snapshot)
|
|
444
|
+
: new Set();
|
|
445
|
+
if (!recursive) {
|
|
446
|
+
const links = await listUnixfsDirectoryLinks(context.store, canonical);
|
|
447
|
+
const sortedNames = [...links.keys()].sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
448
|
+
const entries = [];
|
|
449
|
+
for (const name of sortedNames) {
|
|
450
|
+
const isProtected = normalized.isRoot && protectedNames.has(name);
|
|
451
|
+
if (isProtected && !all)
|
|
452
|
+
continue;
|
|
453
|
+
const link = links.get(name);
|
|
454
|
+
entries.push({
|
|
455
|
+
name,
|
|
456
|
+
path: normalized.isRoot ? name : `${normalized.path}/${name}`,
|
|
457
|
+
cid: link.cid.toString(),
|
|
458
|
+
size: link.tsize ?? null,
|
|
459
|
+
kind: "unknown",
|
|
460
|
+
...(isProtected ? { protected: true } : {}),
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
return {
|
|
464
|
+
swarm: configUrl,
|
|
465
|
+
headCid: currentCtx.header.head.cid,
|
|
466
|
+
rootCid: currentCtx.rootCid,
|
|
467
|
+
path: normalized.path,
|
|
468
|
+
cid: canonical.toString(),
|
|
469
|
+
recursive: false,
|
|
470
|
+
all,
|
|
471
|
+
entries,
|
|
472
|
+
truncated: false,
|
|
473
|
+
skippedSubtrees: 0,
|
|
474
|
+
virtual: false,
|
|
475
|
+
readOnly: false,
|
|
476
|
+
snapshotVersion: "v7",
|
|
477
|
+
source: currentCtx.source,
|
|
478
|
+
verifiedAt: currentCtx.verifiedAt,
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
const walked = await walkUnixfsTree(context.store, canonical, {
|
|
482
|
+
skipRootFiles: normalized.isRoot && !all ? protectedNames : new Set(),
|
|
483
|
+
...(normalized.isRoot ? {} : { prefix: normalized.path }),
|
|
484
|
+
...(options?.maxDepth !== undefined
|
|
485
|
+
? { maxDepth: options.maxDepth }
|
|
486
|
+
: {}),
|
|
487
|
+
...(options?.maxNodes !== undefined
|
|
488
|
+
? { maxNodes: options.maxNodes }
|
|
489
|
+
: {}),
|
|
490
|
+
});
|
|
491
|
+
const entries = walked.entries.map((entry) => {
|
|
492
|
+
const isProtected = normalized.isRoot &&
|
|
493
|
+
!entry.path.includes("/") &&
|
|
494
|
+
protectedNames.has(entry.name);
|
|
495
|
+
return {
|
|
496
|
+
name: entry.name,
|
|
497
|
+
path: entry.path,
|
|
498
|
+
cid: entry.cid,
|
|
499
|
+
size: entry.size ?? null,
|
|
500
|
+
kind: entry.kind,
|
|
501
|
+
...(isProtected ? { protected: true } : {}),
|
|
502
|
+
};
|
|
503
|
+
});
|
|
504
|
+
return {
|
|
505
|
+
swarm: configUrl,
|
|
506
|
+
headCid: currentCtx.header.head.cid,
|
|
507
|
+
rootCid: currentCtx.rootCid,
|
|
508
|
+
path: normalized.path,
|
|
509
|
+
cid: canonical.toString(),
|
|
510
|
+
recursive: true,
|
|
511
|
+
all,
|
|
512
|
+
entries,
|
|
513
|
+
truncated: walked.truncated,
|
|
514
|
+
skippedSubtrees: walked.skippedSubtrees,
|
|
515
|
+
virtual: false,
|
|
516
|
+
readOnly: false,
|
|
517
|
+
snapshotVersion: "v7",
|
|
518
|
+
source: currentCtx.source,
|
|
519
|
+
verifiedAt: currentCtx.verifiedAt,
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
// Legacy v2-v6 read-only virtual view.
|
|
523
|
+
if (normalized.isRoot) {
|
|
524
|
+
const entries = recursive
|
|
525
|
+
? Object.entries(currentCtx.snapshot.names)
|
|
526
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
527
|
+
.map(([name, cid]) => ({
|
|
528
|
+
name: name.split("/").pop(),
|
|
529
|
+
path: name,
|
|
530
|
+
cid: cid.toV1().toString(),
|
|
531
|
+
size: null,
|
|
532
|
+
kind: "link",
|
|
533
|
+
}))
|
|
534
|
+
: legacyDirectEntries(currentCtx.snapshot.names, normalized);
|
|
535
|
+
return {
|
|
536
|
+
swarm: configUrl,
|
|
537
|
+
headCid: currentCtx.header.head.cid,
|
|
538
|
+
rootCid: currentCtx.rootCid,
|
|
539
|
+
path: "",
|
|
540
|
+
cid: currentCtx.rootCid,
|
|
541
|
+
recursive,
|
|
542
|
+
all,
|
|
543
|
+
entries,
|
|
544
|
+
truncated: false,
|
|
545
|
+
skippedSubtrees: 0,
|
|
546
|
+
virtual: true,
|
|
547
|
+
readOnly: true,
|
|
548
|
+
snapshotVersion: "legacy",
|
|
549
|
+
source: currentCtx.source,
|
|
550
|
+
verifiedAt: currentCtx.verifiedAt,
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
const needle = `${normalized.path}/`;
|
|
554
|
+
const matching = Object.entries(currentCtx.snapshot.names)
|
|
555
|
+
.filter(([name]) => name.startsWith(needle))
|
|
556
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
557
|
+
if (matching.length === 0) {
|
|
558
|
+
if (Object.hasOwn(currentCtx.snapshot.names, normalized.path)) {
|
|
559
|
+
throw new HostError(`Path "${normalized.path}" is not a directory. Use ipab fs stat to inspect it.`, 1, "NOT_A_DIRECTORY", "state");
|
|
560
|
+
}
|
|
561
|
+
return undefined;
|
|
562
|
+
}
|
|
563
|
+
const entries = recursive
|
|
564
|
+
? matching.map(([name, cid]) => ({
|
|
565
|
+
name: name.split("/").pop(),
|
|
566
|
+
path: name,
|
|
567
|
+
cid: cid.toV1().toString(),
|
|
568
|
+
size: null,
|
|
569
|
+
kind: "link",
|
|
570
|
+
}))
|
|
571
|
+
: legacyDirectEntries(currentCtx.snapshot.names, normalized);
|
|
572
|
+
return {
|
|
573
|
+
swarm: configUrl,
|
|
574
|
+
headCid: currentCtx.header.head.cid,
|
|
575
|
+
rootCid: currentCtx.rootCid,
|
|
576
|
+
path: normalized.path,
|
|
577
|
+
cid: null,
|
|
578
|
+
recursive,
|
|
579
|
+
all,
|
|
580
|
+
entries,
|
|
581
|
+
truncated: false,
|
|
582
|
+
skippedSubtrees: 0,
|
|
583
|
+
virtual: true,
|
|
584
|
+
readOnly: true,
|
|
585
|
+
snapshotVersion: "legacy",
|
|
586
|
+
source: currentCtx.source,
|
|
587
|
+
verifiedAt: currentCtx.verifiedAt,
|
|
588
|
+
};
|
|
589
|
+
};
|
|
590
|
+
let result = await listFromContext(ctx);
|
|
591
|
+
if (!result &&
|
|
592
|
+
ctx.source === "cache" &&
|
|
593
|
+
!ctx.isLocal &&
|
|
594
|
+
options?.forceRefresh !== false) {
|
|
595
|
+
ctx = await readSwarmFsSnapshot(context, configUrl, {
|
|
596
|
+
mode,
|
|
597
|
+
forceRefresh: true,
|
|
598
|
+
});
|
|
599
|
+
result = await listFromContext(ctx);
|
|
600
|
+
}
|
|
601
|
+
if (!result) {
|
|
602
|
+
throw new HostError(`Path "${normalized.path}" does not exist.`, 1, "PATH_NOT_FOUND", "state");
|
|
603
|
+
}
|
|
604
|
+
return result;
|
|
605
|
+
}
|
|
606
|
+
async function commitSwarmPaths(context, configUrl, options) {
|
|
607
|
+
const maxAttempts = options.maxAttempts ?? 8;
|
|
608
|
+
const isLocal = isLocalSwarm(configUrl);
|
|
609
|
+
const head = await transport.pointer(context, configUrl);
|
|
610
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
611
|
+
const currentHead = await head.read();
|
|
612
|
+
const snapshot = await transport.readSnapshot(context.store, currentHead.root, configUrl);
|
|
613
|
+
const isV7 = isV7Snapshot(snapshot);
|
|
614
|
+
if (!isV7 && !options.allowLegacyNames) {
|
|
615
|
+
throw new HostError("Swarm uses legacy state format; ipab fs write/delete operations require a State v7 UnixFS root.", 1, "UNSUPPORTED_SWARM_VERSION", "state");
|
|
616
|
+
}
|
|
617
|
+
const planCtx = {
|
|
618
|
+
attempt,
|
|
619
|
+
configUrl,
|
|
620
|
+
head: currentHead,
|
|
621
|
+
snapshot,
|
|
622
|
+
isV7,
|
|
623
|
+
isLocal,
|
|
624
|
+
resolve: (targetPath) => resolveSnapshotName(context.store, snapshot, targetPath),
|
|
625
|
+
};
|
|
626
|
+
const planned = await options.plan(planCtx);
|
|
627
|
+
if (planned.kind === "noop") {
|
|
628
|
+
return {
|
|
629
|
+
swarm: configUrl,
|
|
630
|
+
previousHeadCid: currentHead.cid,
|
|
631
|
+
previousRootCid: currentHead.root,
|
|
632
|
+
headCid: currentHead.cid,
|
|
633
|
+
rootCid: currentHead.root,
|
|
634
|
+
changed: false,
|
|
635
|
+
attempts: attempt + 1,
|
|
636
|
+
createdDirCids: [],
|
|
637
|
+
removedCids: new Map(),
|
|
638
|
+
result: planned.result,
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
const defaultSetRoots = [
|
|
642
|
+
...new Set(planned.operations
|
|
643
|
+
.filter((op) => op.op === "set")
|
|
644
|
+
.map((op) => op.targetCid.toV1().toString())),
|
|
645
|
+
];
|
|
646
|
+
let nextRoot;
|
|
647
|
+
let createdDirCids = [];
|
|
648
|
+
let removedCids = new Map();
|
|
649
|
+
if (isV7) {
|
|
650
|
+
for (const op of planned.operations) {
|
|
651
|
+
const normalized = normalizeSwarmFsPath(op.path, {
|
|
652
|
+
allowRoot: false,
|
|
653
|
+
allowTrailingSlash: false,
|
|
654
|
+
});
|
|
655
|
+
assertWritableSwarmFsPath(snapshot, normalized);
|
|
656
|
+
}
|
|
657
|
+
const applied = await applyUnixfsMutations(context.store, CID.parse(snapshot.stateBundle.cid), planned.operations);
|
|
658
|
+
if (!applied.changed) {
|
|
659
|
+
return {
|
|
660
|
+
swarm: configUrl,
|
|
661
|
+
previousHeadCid: currentHead.cid,
|
|
662
|
+
previousRootCid: currentHead.root,
|
|
663
|
+
headCid: currentHead.cid,
|
|
664
|
+
rootCid: currentHead.root,
|
|
665
|
+
changed: false,
|
|
666
|
+
attempts: attempt + 1,
|
|
667
|
+
createdDirCids: [],
|
|
668
|
+
removedCids: new Map(),
|
|
669
|
+
result: planned.result,
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
createdDirCids = applied.createdDirCids;
|
|
673
|
+
removedCids = applied.removedCids;
|
|
674
|
+
nextRoot = applied.newRootCid.toString();
|
|
675
|
+
if (!isLocal) {
|
|
676
|
+
const recursiveRoots = [
|
|
677
|
+
...new Set(planned.recursiveRoots ?? defaultSetRoots),
|
|
678
|
+
];
|
|
679
|
+
const retainedRecursive = [
|
|
680
|
+
...new Set(planned.retainedRecursive ?? recursiveRoots),
|
|
681
|
+
];
|
|
682
|
+
await provideShallowContent(context.store, {
|
|
683
|
+
directBlocks: applied.createdDirCids,
|
|
684
|
+
recursiveRoots,
|
|
685
|
+
retainedDirect: applied.createdDirCids,
|
|
686
|
+
retainedRecursive,
|
|
687
|
+
endpoint: context.node.api,
|
|
688
|
+
...(context.fetch ? { fetcher: context.fetch } : {}),
|
|
689
|
+
...(context.node.auth
|
|
690
|
+
? { authorizationToken: context.node.auth }
|
|
691
|
+
: {}),
|
|
692
|
+
owner: `publication:${nextRoot}`,
|
|
693
|
+
});
|
|
694
|
+
transport.invalidateSwarmHeader(context, configUrl);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
else {
|
|
698
|
+
let names;
|
|
699
|
+
if (planned.legacyNames) {
|
|
700
|
+
names = planned.legacyNames;
|
|
701
|
+
}
|
|
702
|
+
else {
|
|
703
|
+
names = { ...snapshot.names };
|
|
704
|
+
for (const op of planned.operations) {
|
|
705
|
+
if (op.op === "set") {
|
|
706
|
+
names[op.path] = op.targetCid.toV1();
|
|
707
|
+
}
|
|
708
|
+
else {
|
|
709
|
+
if (!Object.hasOwn(names, op.path))
|
|
710
|
+
throw new HostError(`Path does not exist: ${op.path}`);
|
|
711
|
+
removedCids.set(op.path, names[op.path].toV1());
|
|
712
|
+
delete names[op.path];
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
const recursiveRoots = [
|
|
717
|
+
...new Set(planned.recursiveRoots ?? defaultSetRoots),
|
|
718
|
+
];
|
|
719
|
+
const retainedRecursive = [
|
|
720
|
+
...new Set(planned.retainedRecursive ?? recursiveRoots),
|
|
721
|
+
];
|
|
722
|
+
nextRoot = await transport.prepareNamesPublication(context, configUrl, snapshot, names, recursiveRoots, retainedRecursive);
|
|
723
|
+
}
|
|
724
|
+
let written;
|
|
725
|
+
try {
|
|
726
|
+
written = await transport.writeNamesHead(context, configUrl, head, nextRoot, currentHead.cid);
|
|
727
|
+
}
|
|
728
|
+
catch (cause) {
|
|
729
|
+
if (cause instanceof HostError && cause.code === "PIVOT_PIN_FAILED")
|
|
730
|
+
throw cause;
|
|
731
|
+
if (cause instanceof ScopeApiError && cause.status === 412)
|
|
732
|
+
continue;
|
|
733
|
+
throw new HostError(options.messages?.unconfirmed?.(cause, configUrl) ??
|
|
734
|
+
`HEAD update could not be confirmed for ${configUrl}: ${message(cause)}. Inspect HEAD before retrying.`, 1, "HEAD_UNCONFIRMED", "publication");
|
|
735
|
+
}
|
|
736
|
+
if (options.refreshAfterCommit !== false) {
|
|
737
|
+
try {
|
|
738
|
+
await transport.swarmHeader(context, configUrl, true);
|
|
739
|
+
}
|
|
740
|
+
catch (cause) {
|
|
741
|
+
throw new HostError(options.messages?.pinStale?.(cause, configUrl) ??
|
|
742
|
+
`HEAD updated for ${configUrl}, but local refresh failed: ${message(cause)}.`, 1, "PIN_STALE", "publication");
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
return {
|
|
746
|
+
swarm: configUrl,
|
|
747
|
+
previousHeadCid: currentHead.cid,
|
|
748
|
+
previousRootCid: currentHead.root,
|
|
749
|
+
headCid: written.cid,
|
|
750
|
+
rootCid: nextRoot,
|
|
751
|
+
changed: true,
|
|
752
|
+
attempts: attempt + 1,
|
|
753
|
+
createdDirCids,
|
|
754
|
+
removedCids,
|
|
755
|
+
result: planned.result,
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
throw new HostError(options.messages?.headBusy?.(configUrl) ??
|
|
759
|
+
`HEAD is busy for ${configUrl}; retry the operation.`, 1, "OPERATION_FAILED", "publication");
|
|
760
|
+
}
|
|
761
|
+
async function setSwarmFsPath(context, configUrl, path, targetCid, options) {
|
|
762
|
+
const normalized = normalizeSwarmFsPath(path, {
|
|
763
|
+
allowRoot: false,
|
|
764
|
+
allowTrailingSlash: false,
|
|
765
|
+
});
|
|
766
|
+
const canonicalTarget = exactCid(targetCid);
|
|
767
|
+
const expected = options?.expect !== undefined ? exactCid(options.expect) : undefined;
|
|
768
|
+
const replace = Boolean(options?.replace);
|
|
769
|
+
if (expected !== undefined && replace)
|
|
770
|
+
syntax("--expect and --replace are mutually exclusive.");
|
|
771
|
+
try {
|
|
772
|
+
await context.store.block.get(canonicalTarget);
|
|
773
|
+
}
|
|
774
|
+
catch (cause) {
|
|
775
|
+
if (transport.missingContent(cause)) {
|
|
776
|
+
throw new HostError(`Target CID ${canonicalTarget} is not available in the local store. Import or build the content first.`, 1, "CONTENT_NOT_FOUND", "state");
|
|
777
|
+
}
|
|
778
|
+
throw cause;
|
|
779
|
+
}
|
|
780
|
+
const committed = await commitSwarmPaths(context, configUrl, {
|
|
781
|
+
allowLegacyNames: false,
|
|
782
|
+
refreshAfterCommit: true,
|
|
783
|
+
plan: async (planCtx) => {
|
|
784
|
+
assertWritableSwarmFsPath(planCtx.snapshot, normalized);
|
|
785
|
+
const existing = (await planCtx.resolve(normalized.path))
|
|
786
|
+
?.toV1()
|
|
787
|
+
.toString();
|
|
788
|
+
if (expected !== undefined) {
|
|
789
|
+
if (existing !== expected) {
|
|
790
|
+
throw new HostError(existing
|
|
791
|
+
? `Edit conflict for "${normalized.path}": expected ${expected}, found ${existing}.`
|
|
792
|
+
: `Edit conflict for "${normalized.path}": expected ${expected}, but path does not exist.`, 3, "EDIT_CONFLICT", "publication");
|
|
793
|
+
}
|
|
794
|
+
if (existing === canonicalTarget) {
|
|
795
|
+
return { kind: "noop", result: { previousCid: existing } };
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
else if (replace) {
|
|
799
|
+
if (existing === canonicalTarget) {
|
|
800
|
+
return { kind: "noop", result: { previousCid: existing } };
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
else if (existing !== undefined) {
|
|
804
|
+
if (existing === canonicalTarget) {
|
|
805
|
+
return { kind: "noop", result: { previousCid: existing } };
|
|
806
|
+
}
|
|
807
|
+
throw new HostError(`Path "${normalized.path}" already exists with CID ${existing}; use --expect ${existing} or --replace.`, 3, "EDIT_CONFLICT", "publication");
|
|
808
|
+
}
|
|
809
|
+
return {
|
|
810
|
+
kind: "commit",
|
|
811
|
+
operations: [
|
|
812
|
+
{
|
|
813
|
+
op: "set",
|
|
814
|
+
path: normalized.path,
|
|
815
|
+
targetCid: CID.parse(canonicalTarget),
|
|
816
|
+
},
|
|
817
|
+
],
|
|
818
|
+
result: { previousCid: existing ?? null },
|
|
819
|
+
};
|
|
820
|
+
},
|
|
821
|
+
});
|
|
822
|
+
return {
|
|
823
|
+
swarm: committed.swarm,
|
|
824
|
+
path: normalized.path,
|
|
825
|
+
cid: canonicalTarget,
|
|
826
|
+
previousCid: committed.result.previousCid,
|
|
827
|
+
headCid: committed.headCid,
|
|
828
|
+
previousHeadCid: committed.previousHeadCid,
|
|
829
|
+
rootCid: committed.rootCid,
|
|
830
|
+
previousRootCid: committed.previousRootCid,
|
|
831
|
+
changed: committed.changed,
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
async function removeSwarmFsPath(context, configUrl, path, options) {
|
|
835
|
+
const normalized = normalizeSwarmFsPath(path, {
|
|
836
|
+
allowRoot: false,
|
|
837
|
+
allowTrailingSlash: false,
|
|
838
|
+
});
|
|
839
|
+
const expected = options?.expect !== undefined ? exactCid(options.expect) : undefined;
|
|
840
|
+
const committed = await commitSwarmPaths(context, configUrl, {
|
|
841
|
+
allowLegacyNames: false,
|
|
842
|
+
refreshAfterCommit: true,
|
|
843
|
+
plan: async (planCtx) => {
|
|
844
|
+
assertWritableSwarmFsPath(planCtx.snapshot, normalized);
|
|
845
|
+
const existing = (await planCtx.resolve(normalized.path))
|
|
846
|
+
?.toV1()
|
|
847
|
+
.toString();
|
|
848
|
+
if (!existing) {
|
|
849
|
+
if (expected !== undefined) {
|
|
850
|
+
throw new HostError(`Edit conflict for "${normalized.path}": path does not exist.`, 3, "EDIT_CONFLICT", "publication");
|
|
851
|
+
}
|
|
852
|
+
throw new HostError(`Path "${normalized.path}" does not exist.`, 1, "PATH_NOT_FOUND", "state");
|
|
853
|
+
}
|
|
854
|
+
if (expected !== undefined && existing !== expected) {
|
|
855
|
+
throw new HostError(`Edit conflict for "${normalized.path}": expected ${expected}, found ${existing}.`, 3, "EDIT_CONFLICT", "publication");
|
|
856
|
+
}
|
|
857
|
+
return {
|
|
858
|
+
kind: "commit",
|
|
859
|
+
operations: [{ op: "delete", path: normalized.path }],
|
|
860
|
+
recursiveRoots: [],
|
|
861
|
+
result: { removedCid: existing },
|
|
862
|
+
};
|
|
863
|
+
},
|
|
864
|
+
});
|
|
865
|
+
return {
|
|
866
|
+
swarm: committed.swarm,
|
|
867
|
+
path: normalized.path,
|
|
868
|
+
removedCid: committed.result.removedCid,
|
|
869
|
+
headCid: committed.headCid,
|
|
870
|
+
previousHeadCid: committed.previousHeadCid,
|
|
871
|
+
rootCid: committed.rootCid,
|
|
872
|
+
previousRootCid: committed.previousRootCid,
|
|
873
|
+
changed: committed.changed,
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
return {
|
|
877
|
+
readSwarmFsSnapshot,
|
|
878
|
+
statSwarmFsPath,
|
|
879
|
+
listSwarmFsPath,
|
|
880
|
+
commitSwarmPaths,
|
|
881
|
+
setSwarmFsPath,
|
|
882
|
+
removeSwarmFsPath,
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
//# sourceMappingURL=swarm-fs-core.js.map
|