@ipab/cli 0.1.0-rc.6 → 0.1.0-rc.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +96 -34
- package/dist/app-authoring.d.ts +5 -0
- package/dist/app-authoring.d.ts.map +1 -1
- package/dist/app-authoring.js +6 -45
- package/dist/app-authoring.js.map +1 -1
- package/dist/app-prebuild.d.ts +18 -0
- package/dist/app-prebuild.d.ts.map +1 -1
- package/dist/app-prebuild.js +46 -0
- package/dist/app-prebuild.js.map +1 -1
- package/dist/app-templates.d.ts +0 -1
- package/dist/app-templates.d.ts.map +1 -1
- package/dist/app-templates.js +6 -45
- package/dist/app-templates.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 -39
- package/dist/builtins.js.map +1 -1
- package/dist/cli.d.ts +1 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +668 -221
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +9 -4
- package/dist/config.js.map +1 -1
- package/dist/context.d.ts +20 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +31 -2
- package/dist/context.js.map +1 -1
- package/dist/errors.d.ts +1 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +1 -20
- package/dist/errors.js.map +1 -1
- package/dist/host-error.d.ts +9 -0
- package/dist/host-error.d.ts.map +1 -0
- package/dist/host-error.js +21 -0
- package/dist/host-error.js.map +1 -0
- package/dist/ipc.d.ts +2 -1
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +61 -3
- package/dist/ipc.js.map +1 -1
- package/dist/kubo-install.js +1 -1
- package/dist/kubo-install.js.map +1 -1
- package/dist/kubo-peers.d.ts.map +1 -1
- package/dist/kubo-peers.js +2 -1
- package/dist/kubo-peers.js.map +1 -1
- package/dist/kubo-topology.d.ts +19 -0
- package/dist/kubo-topology.d.ts.map +1 -0
- package/dist/kubo-topology.js +105 -0
- package/dist/kubo-topology.js.map +1 -0
- package/dist/kubo.d.ts +10 -0
- package/dist/kubo.d.ts.map +1 -1
- package/dist/kubo.js +96 -15
- package/dist/kubo.js.map +1 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +6 -3
- package/dist/runtime.js.map +1 -1
- 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 +5 -0
- package/dist/skill-source.d.ts.map +1 -0
- package/dist/skill-source.js +12 -0
- package/dist/skill-source.js.map +1 -0
- package/dist/skills.d.ts +84 -0
- package/dist/skills.d.ts.map +1 -0
- package/dist/skills.js +736 -0
- package/dist/skills.js.map +1 -0
- 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 +155 -18
- package/dist/space.js.map +1 -1
- package/dist/state-storage.js +1 -1
- package/dist/state-storage.js.map +1 -1
- package/dist/state.d.ts +2 -0
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +32 -13
- package/dist/state.js.map +1 -1
- package/dist/swarm-files.d.ts +2 -2
- package/dist/swarm-files.d.ts.map +1 -1
- package/dist/swarm-files.js +4 -4
- package/dist/swarm-files.js.map +1 -1
- package/dist/swarm-meta.d.ts +14 -0
- package/dist/swarm-meta.d.ts.map +1 -0
- package/dist/swarm-meta.js +65 -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 +262 -0
- package/dist/swarm-observations.js.map +1 -0
- package/dist/swarm.d.ts +172 -6
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +1106 -132
- package/dist/swarm.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 +2 -1
- package/dist/template-source.d.ts.map +1 -1
- package/dist/template-source.js +7 -1
- 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/package.json +7 -6
- package/src/app-authoring.ts +30 -55
- package/src/app-prebuild.ts +61 -0
- package/src/app-templates.ts +8 -72
- package/src/builtins.ts +2 -48
- package/src/cli.ts +830 -247
- package/src/config.ts +12 -4
- package/src/context.ts +60 -2
- package/src/errors.ts +1 -25
- package/src/host-error.ts +26 -0
- package/src/ipc.ts +82 -2
- package/src/kubo-install.ts +1 -1
- package/src/kubo-peers.ts +2 -3
- package/src/kubo-topology.ts +135 -0
- package/src/kubo.ts +134 -25
- package/src/runtime.ts +6 -2
- package/src/skill-definition.ts +53 -0
- package/src/skill-source.ts +15 -0
- package/src/skills.ts +1005 -0
- package/src/space-path.ts +30 -0
- package/src/space.ts +216 -33
- package/src/state-storage.ts +1 -1
- package/src/state.ts +38 -13
- package/src/swarm-files.ts +4 -2
- package/src/swarm-meta.ts +90 -0
- package/src/swarm-observations.ts +374 -0
- package/src/swarm.ts +1637 -193
- package/src/template-paths.ts +43 -0
- package/src/template-presentation.ts +151 -0
- package/src/template-source.ts +9 -2
- package/src/templates.ts +77 -0
package/src/swarm.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { createReadStream } from "node:fs";
|
|
|
2
2
|
import { lstat, mkdir, open } from "node:fs/promises";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
4
|
import { join, resolve } from "node:path";
|
|
5
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
5
6
|
import { createHash, randomBytes } from "node:crypto";
|
|
6
7
|
import { stringify } from "yaml";
|
|
7
8
|
import { CarBlockIterator } from "@ipld/car/iterator";
|
|
@@ -51,7 +52,7 @@ import {
|
|
|
51
52
|
} from "./identity.js";
|
|
52
53
|
import { nodeError } from "./authoring.js";
|
|
53
54
|
import { swarmUrl as canonicalSwarmUrl } from "./config.js";
|
|
54
|
-
import { bundleJson, provideContent } from "./content-provider.js";
|
|
55
|
+
import { bundleJson, contentCar, provideContent } from "./content-provider.js";
|
|
55
56
|
import {
|
|
56
57
|
failedPin,
|
|
57
58
|
pinCid,
|
|
@@ -92,11 +93,43 @@ import {
|
|
|
92
93
|
peerProof,
|
|
93
94
|
prepareNode,
|
|
94
95
|
startNode,
|
|
96
|
+
stopNode,
|
|
97
|
+
probePeer,
|
|
98
|
+
nodeConnections,
|
|
95
99
|
validateAddress,
|
|
96
100
|
type KuboNode,
|
|
97
101
|
type ListenMode,
|
|
98
102
|
} from "./kubo.js";
|
|
99
103
|
import { memberPeers } from "./kubo-peering.js";
|
|
104
|
+
import { PivotCandidates } from "./kubo-topology.js";
|
|
105
|
+
import {
|
|
106
|
+
networkingBinding,
|
|
107
|
+
pivotLocalStatus,
|
|
108
|
+
readNetworkingSnapshot,
|
|
109
|
+
writeNetworkingSnapshot,
|
|
110
|
+
networkingAccount,
|
|
111
|
+
type LocalObservations,
|
|
112
|
+
type NetworkingSnapshot,
|
|
113
|
+
} from "./swarm-observations.js";
|
|
114
|
+
import { seedTemplateNames, seedTemplateSource } from "./template-source.js";
|
|
115
|
+
import { inspectTemplate, templatePublicationSource } from "./templates.js";
|
|
116
|
+
import { seedSkillSource } from "./skill-source.js";
|
|
117
|
+
import { skillIdentity, skillSchema } from "./skill-definition.js";
|
|
118
|
+
import {
|
|
119
|
+
parseSwarmMeta,
|
|
120
|
+
swarmMetaType,
|
|
121
|
+
swarmMetaSchema,
|
|
122
|
+
swarmStateType,
|
|
123
|
+
previousSwarmStateType,
|
|
124
|
+
previousSwarmStateSchema,
|
|
125
|
+
parseSwarmState,
|
|
126
|
+
legacySwarmStateType,
|
|
127
|
+
legacySwarmStateSchema,
|
|
128
|
+
isSwarmStateSchema,
|
|
129
|
+
type SwarmMeta,
|
|
130
|
+
legacySwarmMetaType,
|
|
131
|
+
legacySwarmMetaSchema,
|
|
132
|
+
} from "./swarm-meta.js";
|
|
100
133
|
|
|
101
134
|
export interface Member {
|
|
102
135
|
memberId?: string;
|
|
@@ -122,6 +155,7 @@ export interface Profile {
|
|
|
122
155
|
name: string;
|
|
123
156
|
description?: string;
|
|
124
157
|
addresses: string[];
|
|
158
|
+
pivot?: boolean;
|
|
125
159
|
updatedAt: string;
|
|
126
160
|
}
|
|
127
161
|
export interface AccountProfile {
|
|
@@ -134,6 +168,7 @@ export interface Snapshot {
|
|
|
134
168
|
v: 2;
|
|
135
169
|
names: Record<string, CID>;
|
|
136
170
|
registrations?: Record<string, { appCid: CID; argv: string[] }>;
|
|
171
|
+
stateBundle?: { meta: SwarmMeta; cid: string; configUrl?: string };
|
|
137
172
|
}
|
|
138
173
|
export interface MembershipRequest {
|
|
139
174
|
v: 1;
|
|
@@ -166,6 +201,7 @@ export interface SwarmContext {
|
|
|
166
201
|
finishRead?: () => Promise<void>;
|
|
167
202
|
identity?: Identity;
|
|
168
203
|
listen?: ListenMode;
|
|
204
|
+
pivot?: boolean;
|
|
169
205
|
}
|
|
170
206
|
async function contextIdentity(
|
|
171
207
|
context: SwarmContext,
|
|
@@ -343,6 +379,10 @@ function client(
|
|
|
343
379
|
}
|
|
344
380
|
function parseOrganization(value: unknown, configUrl: string): Organization {
|
|
345
381
|
const data = object(value, "organization configuration");
|
|
382
|
+
if (data.kind === "ipab-config-reservation")
|
|
383
|
+
throw new HostError(
|
|
384
|
+
"Swarm creation is initializing; the Owner must resume the saved creation operation.",
|
|
385
|
+
);
|
|
346
386
|
const { scopeId } = coordinates(configUrl);
|
|
347
387
|
if (
|
|
348
388
|
Object.keys(data).sort().join() !==
|
|
@@ -438,10 +478,13 @@ function parseOrganization(value: unknown, configUrl: string): Organization {
|
|
|
438
478
|
function parseProfile(value: unknown, expectedPeer: string): Profile {
|
|
439
479
|
const data = object(value, "member profile");
|
|
440
480
|
if (
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
481
|
+
Object.keys(data).some(
|
|
482
|
+
(key) =>
|
|
483
|
+
!["addresses", "name", "description", "updatedAt", "pivot"].includes(
|
|
484
|
+
key,
|
|
485
|
+
),
|
|
486
|
+
) ||
|
|
487
|
+
(data.pivot !== undefined && typeof data.pivot !== "boolean") ||
|
|
445
488
|
typeof data.name !== "string" ||
|
|
446
489
|
data.name.length > 256 ||
|
|
447
490
|
(data.description !== undefined &&
|
|
@@ -466,11 +509,15 @@ function parseProfile(value: unknown, expectedPeer: string): Profile {
|
|
|
466
509
|
? { description: data.description }
|
|
467
510
|
: {}),
|
|
468
511
|
addresses,
|
|
512
|
+
...(typeof data.pivot === "boolean" ? { pivot: data.pivot } : {}),
|
|
469
513
|
updatedAt: data.updatedAt,
|
|
470
514
|
};
|
|
471
515
|
}
|
|
472
|
-
|
|
473
|
-
|
|
516
|
+
async function memberDirectory(
|
|
517
|
+
context: SwarmContext,
|
|
518
|
+
url: string,
|
|
519
|
+
config: Organization,
|
|
520
|
+
) {
|
|
474
521
|
const c = coordinates(url),
|
|
475
522
|
api = client(context, c.service);
|
|
476
523
|
const members = await boundedMap(config.members, async (member) => {
|
|
@@ -478,14 +525,354 @@ export async function swarmMembers(context: SwarmContext, url: string) {
|
|
|
478
525
|
(await api.read(c.scopeId, member.profileId)).value,
|
|
479
526
|
member.peerId,
|
|
480
527
|
);
|
|
528
|
+
return { member, profile };
|
|
529
|
+
});
|
|
530
|
+
return members;
|
|
531
|
+
}
|
|
532
|
+
async function localObservations(
|
|
533
|
+
context: SwarmContext,
|
|
534
|
+
url: string,
|
|
535
|
+
config: Organization,
|
|
536
|
+
) {
|
|
537
|
+
try {
|
|
538
|
+
const account =
|
|
539
|
+
context.identity?.publicKey ??
|
|
540
|
+
(await networkingAccount(context.stateDir, url));
|
|
541
|
+
const self = config.members.find((member) => member.publicKey === account);
|
|
542
|
+
if (!self || !account)
|
|
543
|
+
throw new HostError(
|
|
544
|
+
"No bound local daemon observations; run ipab daemon after normal membership activation.",
|
|
545
|
+
);
|
|
546
|
+
const c = coordinates(url);
|
|
547
|
+
const local = await readNetworkingSnapshot(
|
|
548
|
+
context.stateDir,
|
|
549
|
+
url,
|
|
550
|
+
networkingBinding(url, account, config),
|
|
551
|
+
nodeScope(c.service, config.scopeId),
|
|
552
|
+
config.epoch,
|
|
553
|
+
self.peerId,
|
|
554
|
+
);
|
|
481
555
|
return {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
556
|
+
account:
|
|
557
|
+
context.identity?.publicKey ?? (local.snapshot ? account : undefined),
|
|
558
|
+
local,
|
|
559
|
+
};
|
|
560
|
+
} catch (cause) {
|
|
561
|
+
return {
|
|
562
|
+
account: context.identity?.publicKey,
|
|
563
|
+
local: {
|
|
564
|
+
status: "unavailable",
|
|
565
|
+
updatedAt: null,
|
|
566
|
+
verifiedAt: null,
|
|
567
|
+
error: message(cause),
|
|
568
|
+
snapshot: null,
|
|
569
|
+
} satisfies LocalObservations,
|
|
486
570
|
};
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
function observationSummary(local: LocalObservations) {
|
|
574
|
+
return {
|
|
575
|
+
status: local.status,
|
|
576
|
+
updatedAt: local.updatedAt,
|
|
577
|
+
verifiedAt: local.verifiedAt,
|
|
578
|
+
error: local.error,
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
interface InspectionSnapshot {
|
|
582
|
+
v: 1;
|
|
583
|
+
configUrl: string;
|
|
584
|
+
updatedAt: number;
|
|
585
|
+
config: Organization;
|
|
586
|
+
directory: Awaited<ReturnType<typeof memberDirectory>>;
|
|
587
|
+
head: { root: string; cid: string };
|
|
588
|
+
}
|
|
589
|
+
function parseInspection(input: unknown, url: string): InspectionSnapshot {
|
|
590
|
+
const data = object(input, "Swarm inspection cache");
|
|
591
|
+
if (
|
|
592
|
+
Object.keys(data).sort().join() !==
|
|
593
|
+
"config,configUrl,directory,head,updatedAt,v" ||
|
|
594
|
+
data.v !== 1 ||
|
|
595
|
+
data.configUrl !== url ||
|
|
596
|
+
typeof data.updatedAt !== "number" ||
|
|
597
|
+
!Number.isSafeInteger(data.updatedAt) ||
|
|
598
|
+
data.updatedAt < 0 ||
|
|
599
|
+
data.updatedAt > 8_640_000_000_000_000 ||
|
|
600
|
+
!Array.isArray(data.directory) ||
|
|
601
|
+
data.directory.length > 256
|
|
602
|
+
)
|
|
603
|
+
throw new HostError(
|
|
604
|
+
"Invalid Swarm inspection cache; retry with --refresh.",
|
|
605
|
+
);
|
|
606
|
+
const config = parseOrganization(data.config, url);
|
|
607
|
+
if (
|
|
608
|
+
config.members.some(
|
|
609
|
+
(member) => !/^[A-Za-z0-9]{32,128}$/.test(member.peerId),
|
|
610
|
+
)
|
|
611
|
+
)
|
|
612
|
+
throw new HostError("Invalid cached member PeerID; use --refresh.");
|
|
613
|
+
if (data.directory.length !== config.members.length)
|
|
614
|
+
throw new HostError(
|
|
615
|
+
"Incomplete Swarm inspection directory; use --refresh.",
|
|
616
|
+
);
|
|
617
|
+
const directory = data.directory.map((input: unknown, index) => {
|
|
618
|
+
const entry = object(input, "cached directory entry");
|
|
619
|
+
const member = config.members[index]!;
|
|
620
|
+
if (
|
|
621
|
+
Object.keys(entry).sort().join() !== "member,profile" ||
|
|
622
|
+
JSON.stringify(entry.member) !== JSON.stringify(member)
|
|
623
|
+
)
|
|
624
|
+
throw new HostError("Cached member binding mismatch; use --refresh.");
|
|
625
|
+
return { member, profile: parseProfile(entry.profile, member.peerId) };
|
|
487
626
|
});
|
|
488
|
-
|
|
627
|
+
const head = object(data.head, "cached HEAD");
|
|
628
|
+
if (
|
|
629
|
+
Object.keys(head).sort().join() !== "cid,root" ||
|
|
630
|
+
typeof head.root !== "string" ||
|
|
631
|
+
typeof head.cid !== "string" ||
|
|
632
|
+
exactCid(head.root) !== head.root ||
|
|
633
|
+
exactCid(head.cid) !== head.cid
|
|
634
|
+
)
|
|
635
|
+
throw new HostError("Invalid cached HEAD; use --refresh.");
|
|
636
|
+
return {
|
|
637
|
+
v: 1,
|
|
638
|
+
configUrl: url,
|
|
639
|
+
updatedAt: data.updatedAt,
|
|
640
|
+
config,
|
|
641
|
+
directory,
|
|
642
|
+
head: { root: head.root, cid: head.cid },
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
async function inspection(context: SwarmContext, url: string, refresh = false) {
|
|
646
|
+
canonicalSwarmUrl(url);
|
|
647
|
+
const parent = join(context.stateDir, "swarm-inspections");
|
|
648
|
+
const name = `${createHash("sha256").update(url).digest("hex")}.json`;
|
|
649
|
+
const saved = refresh
|
|
650
|
+
? undefined
|
|
651
|
+
: await readPrivate<unknown>(parent, name, [
|
|
652
|
+
{ path: parent, directory: true },
|
|
653
|
+
]);
|
|
654
|
+
let snapshot: InspectionSnapshot;
|
|
655
|
+
if (saved === undefined) {
|
|
656
|
+
snapshot = await withPrivateLock(
|
|
657
|
+
await privateDirectory(context.stateDir, "swarm-inspections"),
|
|
658
|
+
async () => {
|
|
659
|
+
const { config, variable } = await fetchConfig(context, url);
|
|
660
|
+
const directory = await memberDirectory(context, url, config);
|
|
661
|
+
const c = coordinates(url);
|
|
662
|
+
const head = headValue(
|
|
663
|
+
await client(context, c.service).read(c.scopeId, config.headId),
|
|
664
|
+
);
|
|
665
|
+
const confirmed = await fetchConfig(context, url);
|
|
666
|
+
if (
|
|
667
|
+
variable.cid !== confirmed.variable.cid ||
|
|
668
|
+
JSON.stringify(config) !== JSON.stringify(confirmed.config)
|
|
669
|
+
)
|
|
670
|
+
throw new HostError(
|
|
671
|
+
"Swarm configuration changed during inspection; retry --refresh.",
|
|
672
|
+
);
|
|
673
|
+
// Inspection persists only public declarations, never approval envelopes.
|
|
674
|
+
const publicConfig = {
|
|
675
|
+
...config,
|
|
676
|
+
members: config.members.map((member) => ({
|
|
677
|
+
...member,
|
|
678
|
+
approval: null,
|
|
679
|
+
})),
|
|
680
|
+
};
|
|
681
|
+
const fresh = parseInspection(
|
|
682
|
+
{
|
|
683
|
+
v: 1,
|
|
684
|
+
configUrl: url,
|
|
685
|
+
updatedAt: Date.now(),
|
|
686
|
+
config: publicConfig,
|
|
687
|
+
directory: directory.map(({ profile }, index) => ({
|
|
688
|
+
member: publicConfig.members[index],
|
|
689
|
+
profile,
|
|
690
|
+
})),
|
|
691
|
+
head: { root: head.root, cid: head.cid },
|
|
692
|
+
},
|
|
693
|
+
url,
|
|
694
|
+
);
|
|
695
|
+
if (Buffer.byteLength(JSON.stringify(fresh)) > 2 * 1024 * 1024)
|
|
696
|
+
throw new HostError("Swarm inspection snapshot is oversized.");
|
|
697
|
+
await writePrivate(parent, name, fresh);
|
|
698
|
+
return fresh;
|
|
699
|
+
},
|
|
700
|
+
);
|
|
701
|
+
} else {
|
|
702
|
+
snapshot = parseInspection(saved, url);
|
|
703
|
+
}
|
|
704
|
+
const age = Date.now() - snapshot.updatedAt;
|
|
705
|
+
return {
|
|
706
|
+
...snapshot,
|
|
707
|
+
cache: {
|
|
708
|
+
source: saved === undefined ? ("network" as const) : ("cache" as const),
|
|
709
|
+
updatedAt: new Date(snapshot.updatedAt).toISOString(),
|
|
710
|
+
freshness:
|
|
711
|
+
age >= 0 && age < (context.cacheTtl ?? 300) * 1000
|
|
712
|
+
? ("current" as const)
|
|
713
|
+
: ("stale" as const),
|
|
714
|
+
},
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
export async function swarmMembers(
|
|
718
|
+
context: SwarmContext,
|
|
719
|
+
url: string,
|
|
720
|
+
refresh = false,
|
|
721
|
+
) {
|
|
722
|
+
const { config, directory, cache } = await inspection(context, url, refresh);
|
|
723
|
+
const observations = await localObservations(context, url, config);
|
|
724
|
+
const members = directory.map(({ member, profile }) => ({
|
|
725
|
+
publicKey: member.publicKey,
|
|
726
|
+
role: member.publicKey === config.owner ? "owner" : "member",
|
|
727
|
+
name: profile.name,
|
|
728
|
+
description: profile.description ?? "",
|
|
729
|
+
pivot: profile.pivot === true,
|
|
730
|
+
localStatus: pivotLocalStatus(
|
|
731
|
+
profile.pivot === true,
|
|
732
|
+
member.publicKey === observations.account,
|
|
733
|
+
profile.addresses,
|
|
734
|
+
member.peerId,
|
|
735
|
+
observations.local,
|
|
736
|
+
),
|
|
737
|
+
peerId: member.peerId,
|
|
738
|
+
updatedAt: profile.updatedAt,
|
|
739
|
+
}));
|
|
740
|
+
return {
|
|
741
|
+
configUrl: url,
|
|
742
|
+
members,
|
|
743
|
+
cache,
|
|
744
|
+
local: observationSummary(observations.local),
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
export async function swarmNetworking(
|
|
749
|
+
context: SwarmContext,
|
|
750
|
+
url: string,
|
|
751
|
+
config: Organization,
|
|
752
|
+
directory?: Awaited<ReturnType<typeof memberDirectory>>,
|
|
753
|
+
) {
|
|
754
|
+
directory ??= (await inspection(context, url)).directory;
|
|
755
|
+
const { account, local } = await localObservations(context, url, config);
|
|
756
|
+
const saved = local.snapshot?.connections;
|
|
757
|
+
const observedAt =
|
|
758
|
+
saved?.observedAt == null ? null : new Date(saved.observedAt).toISOString();
|
|
759
|
+
const connectionError =
|
|
760
|
+
local.status !== "current"
|
|
761
|
+
? (local.error ?? "Daemon observations are not current.")
|
|
762
|
+
: saved === undefined || saved.observedAt === null
|
|
763
|
+
? "No daemon connection snapshot; run ipab daemon."
|
|
764
|
+
: saved.error;
|
|
765
|
+
const connections = saved?.peers ?? [];
|
|
766
|
+
const freshness =
|
|
767
|
+
connectionError || !saved?.observedAt
|
|
768
|
+
? observedAt
|
|
769
|
+
? ("stale" as const)
|
|
770
|
+
: ("unavailable" as const)
|
|
771
|
+
: Date.now() - saved.observedAt > 90_000
|
|
772
|
+
? ("stale" as const)
|
|
773
|
+
: ("current" as const);
|
|
774
|
+
const currentConnections = freshness === "current";
|
|
775
|
+
return {
|
|
776
|
+
daemon: observationSummary(local),
|
|
777
|
+
connections: {
|
|
778
|
+
observedAt,
|
|
779
|
+
error: connectionError,
|
|
780
|
+
peers: connections,
|
|
781
|
+
freshness,
|
|
782
|
+
},
|
|
783
|
+
pivots: directory
|
|
784
|
+
.filter(({ profile }) => profile.pivot === true)
|
|
785
|
+
.map(({ member, profile }) => {
|
|
786
|
+
const localStatus = pivotLocalStatus(
|
|
787
|
+
true,
|
|
788
|
+
member.publicKey === account,
|
|
789
|
+
profile.addresses,
|
|
790
|
+
member.peerId,
|
|
791
|
+
local,
|
|
792
|
+
);
|
|
793
|
+
const observation = local.snapshot?.pivots.find(
|
|
794
|
+
(entry) => entry.peerId === member.peerId,
|
|
795
|
+
);
|
|
796
|
+
const matching =
|
|
797
|
+
observation &&
|
|
798
|
+
JSON.stringify(observation.addresses) ===
|
|
799
|
+
JSON.stringify([...new Set(profile.addresses)].sort());
|
|
800
|
+
const current = local.status === "current" && matching;
|
|
801
|
+
const actual = connections.filter(
|
|
802
|
+
(entry) => entry.peerId === member.peerId,
|
|
803
|
+
);
|
|
804
|
+
const probeFreshness: LocalObservations["status"] =
|
|
805
|
+
matching && observation.probedAt !== null
|
|
806
|
+
? current && Date.now() - observation.probedAt <= 90_000
|
|
807
|
+
? "current"
|
|
808
|
+
: "stale"
|
|
809
|
+
: "unavailable";
|
|
810
|
+
const connectionStatus:
|
|
811
|
+
"self" | "unknown" | "direct" | "relay" | "disconnected" =
|
|
812
|
+
member.publicKey === account
|
|
813
|
+
? "self"
|
|
814
|
+
: !currentConnections
|
|
815
|
+
? "unknown"
|
|
816
|
+
: actual.some((entry) => entry.transport === "direct")
|
|
817
|
+
? "direct"
|
|
818
|
+
: actual.length
|
|
819
|
+
? "relay"
|
|
820
|
+
: "disconnected";
|
|
821
|
+
return {
|
|
822
|
+
publicKey: member.publicKey,
|
|
823
|
+
name: profile.name,
|
|
824
|
+
peerId: member.peerId,
|
|
825
|
+
pivot: true as const,
|
|
826
|
+
addresses: profile.addresses,
|
|
827
|
+
localStatus,
|
|
828
|
+
selected:
|
|
829
|
+
localStatus === "unknown" ? null : localStatus === "selected",
|
|
830
|
+
failures: matching ? observation.failures : null,
|
|
831
|
+
retryAt:
|
|
832
|
+
matching && observation.retryAt > 0
|
|
833
|
+
? new Date(observation.retryAt).toISOString()
|
|
834
|
+
: null,
|
|
835
|
+
probe: {
|
|
836
|
+
result: matching ? observation.probe : ("unknown" as const),
|
|
837
|
+
at:
|
|
838
|
+
matching && observation.probedAt !== null
|
|
839
|
+
? new Date(observation.probedAt).toISOString()
|
|
840
|
+
: null,
|
|
841
|
+
error: matching ? observation.error : null,
|
|
842
|
+
freshness: probeFreshness,
|
|
843
|
+
},
|
|
844
|
+
connection: {
|
|
845
|
+
status: connectionStatus,
|
|
846
|
+
observedAt,
|
|
847
|
+
error: connectionError,
|
|
848
|
+
freshness,
|
|
849
|
+
addresses: actual.map((entry) => entry.address),
|
|
850
|
+
},
|
|
851
|
+
};
|
|
852
|
+
}),
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
export async function inspectSwarmStatus(
|
|
856
|
+
context: SwarmContext,
|
|
857
|
+
url: string,
|
|
858
|
+
refresh = false,
|
|
859
|
+
) {
|
|
860
|
+
const { config, directory, head, cache } = await inspection(
|
|
861
|
+
context,
|
|
862
|
+
url,
|
|
863
|
+
refresh,
|
|
864
|
+
);
|
|
865
|
+
return {
|
|
866
|
+
config,
|
|
867
|
+
head,
|
|
868
|
+
cache,
|
|
869
|
+
state: {
|
|
870
|
+
status: "unavailable" as const,
|
|
871
|
+
error:
|
|
872
|
+
"State content is not fetched or verified by inspection; use get ipab:/// to obtain the State Bundle.",
|
|
873
|
+
},
|
|
874
|
+
networking: await swarmNetworking(context, url, config, directory),
|
|
875
|
+
};
|
|
489
876
|
}
|
|
490
877
|
|
|
491
878
|
function accountProfileLocation(context: SwarmContext, publicKey: string) {
|
|
@@ -863,6 +1250,7 @@ async function privateSource(
|
|
|
863
1250
|
context: SwarmContext,
|
|
864
1251
|
record: LocalMembership,
|
|
865
1252
|
signal?: AbortSignal,
|
|
1253
|
+
candidates?: PivotCandidates,
|
|
866
1254
|
) {
|
|
867
1255
|
const config = record.config!,
|
|
868
1256
|
secret = record.secret!;
|
|
@@ -895,17 +1283,38 @@ async function privateSource(
|
|
|
895
1283
|
}
|
|
896
1284
|
const personal = await storedAccountProfile(context);
|
|
897
1285
|
const addresses = new Map<string, readonly string[]>();
|
|
1286
|
+
const observationErrors: string[] = [];
|
|
898
1287
|
const api = client(context, c.service);
|
|
899
1288
|
let selfProfile: { profile: Profile; cid: string } | undefined;
|
|
900
|
-
const profiles = await boundedMap(config.members, (member) =>
|
|
901
|
-
|
|
902
|
-
|
|
1289
|
+
const profiles = await boundedMap(config.members, async (member) => {
|
|
1290
|
+
try {
|
|
1291
|
+
return await api.read(
|
|
1292
|
+
c.scopeId,
|
|
1293
|
+
member.profileId,
|
|
1294
|
+
signal ? { signal } : {},
|
|
1295
|
+
);
|
|
1296
|
+
} catch (cause) {
|
|
1297
|
+
signal?.throwIfAborted();
|
|
1298
|
+
if (!candidates) throw cause;
|
|
1299
|
+
observationErrors.push(`Profile ${member.peerId}: ${message(cause)}`);
|
|
1300
|
+
context.diagnostic?.(
|
|
1301
|
+
`Pivot profile refresh ${member.peerId} failed; retaining last-known approved candidate: ${message(cause)}`,
|
|
1302
|
+
);
|
|
1303
|
+
return undefined;
|
|
1304
|
+
}
|
|
1305
|
+
});
|
|
903
1306
|
for (const [index, member] of config.members.entries()) {
|
|
904
|
-
const profile = profiles[index]
|
|
1307
|
+
const profile = profiles[index];
|
|
1308
|
+
if (!profile) {
|
|
1309
|
+
const known = candidates?.known(member.peerId);
|
|
1310
|
+
if (known) addresses.set(member.peerId, known);
|
|
1311
|
+
continue;
|
|
1312
|
+
}
|
|
905
1313
|
const value = parseProfile(profile.value, member.peerId);
|
|
906
1314
|
if (member.publicKey === identity.publicKey)
|
|
907
1315
|
selfProfile = { profile: value, cid: profile.cid };
|
|
908
|
-
|
|
1316
|
+
if (value.pivot === true && member.peerId !== self.peerId)
|
|
1317
|
+
addresses.set(member.peerId, value.addresses);
|
|
909
1318
|
}
|
|
910
1319
|
if (personal && selfProfile) {
|
|
911
1320
|
selfProfile.profile = await syncAccountProfile(
|
|
@@ -916,7 +1325,34 @@ async function privateSource(
|
|
|
916
1325
|
);
|
|
917
1326
|
selfProfile.cid = await valueCid(selfProfile.profile);
|
|
918
1327
|
}
|
|
919
|
-
|
|
1328
|
+
candidates?.reconcile(addresses);
|
|
1329
|
+
const active = candidates?.active() ?? addresses;
|
|
1330
|
+
if (!candidates) {
|
|
1331
|
+
try {
|
|
1332
|
+
const observed = await readNetworkingSnapshot(
|
|
1333
|
+
context.stateDir,
|
|
1334
|
+
record.configUrl,
|
|
1335
|
+
networkingBinding(record.configUrl, identity.publicKey, config),
|
|
1336
|
+
nodeScope(c.service, config.scopeId),
|
|
1337
|
+
config.epoch,
|
|
1338
|
+
self.peerId,
|
|
1339
|
+
);
|
|
1340
|
+
if (observed.status === "current")
|
|
1341
|
+
for (const pivot of observed.snapshot!.pivots)
|
|
1342
|
+
if (
|
|
1343
|
+
pivot.retryAt > Date.now() &&
|
|
1344
|
+
JSON.stringify(
|
|
1345
|
+
[...new Set(active.get(pivot.peerId) ?? [])].sort(),
|
|
1346
|
+
) === JSON.stringify(pivot.addresses)
|
|
1347
|
+
)
|
|
1348
|
+
active.delete(pivot.peerId);
|
|
1349
|
+
} catch (cause) {
|
|
1350
|
+
context.diagnostic?.(
|
|
1351
|
+
`Local daemon observations could not be read: ${message(cause)}`,
|
|
1352
|
+
);
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
const desired = memberPeers(self.peerId, active, []);
|
|
920
1356
|
signal?.throwIfAborted();
|
|
921
1357
|
const node = await startNode(
|
|
922
1358
|
context.stateDir,
|
|
@@ -924,7 +1360,12 @@ async function privateSource(
|
|
|
924
1360
|
secret.swarmKey,
|
|
925
1361
|
config.epoch,
|
|
926
1362
|
context.listen,
|
|
927
|
-
{
|
|
1363
|
+
{
|
|
1364
|
+
self: self.peerId,
|
|
1365
|
+
peers: desired,
|
|
1366
|
+
pivot: context.pivot ?? false,
|
|
1367
|
+
...(signal ? { signal } : {}),
|
|
1368
|
+
},
|
|
928
1369
|
);
|
|
929
1370
|
if (node.peerId !== self.peerId)
|
|
930
1371
|
throw new HostError(
|
|
@@ -934,15 +1375,48 @@ async function privateSource(
|
|
|
934
1375
|
Addrs.map((addr) => `${addr}/p2p/${ID}`),
|
|
935
1376
|
);
|
|
936
1377
|
signal?.throwIfAborted();
|
|
937
|
-
if (
|
|
1378
|
+
if (
|
|
1379
|
+
context.pivot &&
|
|
1380
|
+
!node.addresses.length &&
|
|
1381
|
+
!selfProfile?.profile.addresses.length
|
|
1382
|
+
)
|
|
938
1383
|
context.diagnostic?.(
|
|
939
|
-
"
|
|
1384
|
+
"Pivot has no IPv4 candidates. Connect an interface or set manual candidates with swarm profile --address.",
|
|
940
1385
|
);
|
|
941
|
-
if (
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
1386
|
+
if (selfProfile) {
|
|
1387
|
+
const dir = await directory(context);
|
|
1388
|
+
const preference = `pivot-${localName(record.configUrl)}`;
|
|
1389
|
+
const saved = await readPrivate<{ manual: string[] }>(dir, preference);
|
|
1390
|
+
const manual = saved?.manual ?? selfProfile.profile.addresses;
|
|
1391
|
+
for (const address of manual) validateAddress(address, self.peerId);
|
|
1392
|
+
if (!saved && context.pivot)
|
|
1393
|
+
await writePrivate(dir, preference, { manual });
|
|
1394
|
+
const published = context.pivot
|
|
1395
|
+
? [...new Set([...manual, ...node.addresses])].slice(0, 16).sort()
|
|
1396
|
+
: manual;
|
|
1397
|
+
if (context.pivot && new Set([...manual, ...node.addresses]).size > 16)
|
|
1398
|
+
context.diagnostic?.(
|
|
1399
|
+
"Pivot profile is capped at 16 candidates; manual hints take priority over interface snapshots.",
|
|
1400
|
+
);
|
|
1401
|
+
const pivot = context.pivot ?? false;
|
|
1402
|
+
if (
|
|
1403
|
+
selfProfile.profile.pivot === pivot &&
|
|
1404
|
+
JSON.stringify([...selfProfile.profile.addresses].sort()) ===
|
|
1405
|
+
JSON.stringify(published)
|
|
1406
|
+
)
|
|
1407
|
+
return {
|
|
1408
|
+
node,
|
|
1409
|
+
peers,
|
|
1410
|
+
observationError: observationErrors.length
|
|
1411
|
+
? observationErrors.join("; ")
|
|
1412
|
+
: null,
|
|
1413
|
+
source: kuboSource(
|
|
1414
|
+
node,
|
|
1415
|
+
peers,
|
|
1416
|
+
context.diagnostic,
|
|
1417
|
+
context.env.IPAB_SWARM_DIAL_DEBUG === "1",
|
|
1418
|
+
),
|
|
1419
|
+
};
|
|
946
1420
|
const writer = client(
|
|
947
1421
|
context,
|
|
948
1422
|
c.service,
|
|
@@ -951,7 +1425,8 @@ async function privateSource(
|
|
|
951
1425
|
);
|
|
952
1426
|
const profile = {
|
|
953
1427
|
...selfProfile.profile,
|
|
954
|
-
|
|
1428
|
+
pivot,
|
|
1429
|
+
addresses: published,
|
|
955
1430
|
updatedAt: new Date().toISOString(),
|
|
956
1431
|
};
|
|
957
1432
|
await writer.write(
|
|
@@ -966,6 +1441,9 @@ async function privateSource(
|
|
|
966
1441
|
return {
|
|
967
1442
|
node,
|
|
968
1443
|
peers,
|
|
1444
|
+
observationError: observationErrors.length
|
|
1445
|
+
? observationErrors.join("; ")
|
|
1446
|
+
: null,
|
|
969
1447
|
source: kuboSource(
|
|
970
1448
|
node,
|
|
971
1449
|
peers,
|
|
@@ -974,6 +1452,276 @@ async function privateSource(
|
|
|
974
1452
|
),
|
|
975
1453
|
};
|
|
976
1454
|
}
|
|
1455
|
+
export async function swarmDaemon(
|
|
1456
|
+
context: SwarmContext,
|
|
1457
|
+
selected: string | undefined,
|
|
1458
|
+
signal: AbortSignal,
|
|
1459
|
+
): Promise<void> {
|
|
1460
|
+
const lock = await privateDirectory(context.stateDir, "swarm-daemon");
|
|
1461
|
+
await withPrivateLock(
|
|
1462
|
+
lock,
|
|
1463
|
+
async () => {
|
|
1464
|
+
const sessions = new Map<
|
|
1465
|
+
string,
|
|
1466
|
+
{
|
|
1467
|
+
context: SwarmContext;
|
|
1468
|
+
candidates: PivotCandidates;
|
|
1469
|
+
nextDiscovery: number;
|
|
1470
|
+
healthy: number;
|
|
1471
|
+
record?: LocalMembership;
|
|
1472
|
+
node?: KuboNode;
|
|
1473
|
+
selected: Set<string>;
|
|
1474
|
+
verifiedAt: number | null;
|
|
1475
|
+
error: string | null;
|
|
1476
|
+
connections?: NetworkingSnapshot["connections"];
|
|
1477
|
+
}
|
|
1478
|
+
>();
|
|
1479
|
+
context.diagnostic?.(
|
|
1480
|
+
"IPAB daemon running in foreground; Ctrl-C stops management and leaves Kubo running.",
|
|
1481
|
+
);
|
|
1482
|
+
const persist = async (
|
|
1483
|
+
url: string,
|
|
1484
|
+
session: NonNullable<ReturnType<typeof sessions.get>>,
|
|
1485
|
+
running: boolean,
|
|
1486
|
+
) => {
|
|
1487
|
+
const record = session.record;
|
|
1488
|
+
if (!record?.config) return;
|
|
1489
|
+
const redact = (error: string | null) => {
|
|
1490
|
+
if (error === null) return null;
|
|
1491
|
+
for (const secret of [
|
|
1492
|
+
record.secret?.swarmKey,
|
|
1493
|
+
session.node?.auth,
|
|
1494
|
+
session.context.identity?.privateKey,
|
|
1495
|
+
])
|
|
1496
|
+
if (secret) error = error.split(secret).join("[redacted]");
|
|
1497
|
+
return error.slice(0, 2048);
|
|
1498
|
+
};
|
|
1499
|
+
const snapshot: NetworkingSnapshot = {
|
|
1500
|
+
v: 1,
|
|
1501
|
+
binding: networkingBinding(url, record.account, record.config),
|
|
1502
|
+
pid: process.pid,
|
|
1503
|
+
running,
|
|
1504
|
+
updatedAt: Date.now(),
|
|
1505
|
+
verifiedAt: session.verifiedAt,
|
|
1506
|
+
error: redact(session.error),
|
|
1507
|
+
topology: session.node?.topology ?? null,
|
|
1508
|
+
pivots: [...session.candidates.observations()].map(
|
|
1509
|
+
([peerId, value]) => ({
|
|
1510
|
+
...value,
|
|
1511
|
+
peerId,
|
|
1512
|
+
selected: session.selected.has(peerId),
|
|
1513
|
+
error: redact(value.error),
|
|
1514
|
+
}),
|
|
1515
|
+
),
|
|
1516
|
+
account: record.account,
|
|
1517
|
+
connections: session.connections
|
|
1518
|
+
? {
|
|
1519
|
+
...session.connections,
|
|
1520
|
+
error: redact(session.connections.error),
|
|
1521
|
+
}
|
|
1522
|
+
: {
|
|
1523
|
+
observedAt: null,
|
|
1524
|
+
error: "No connection snapshot collected.",
|
|
1525
|
+
peers: [],
|
|
1526
|
+
},
|
|
1527
|
+
};
|
|
1528
|
+
await writeNetworkingSnapshot(context.stateDir, url, snapshot);
|
|
1529
|
+
};
|
|
1530
|
+
try {
|
|
1531
|
+
while (!signal.aborted) {
|
|
1532
|
+
const registrations = withState(context, (state) =>
|
|
1533
|
+
state.swarms(),
|
|
1534
|
+
).filter((entry) => selected === undefined || entry.cid === selected);
|
|
1535
|
+
if (selected && !registrations.length)
|
|
1536
|
+
throw new HostError(
|
|
1537
|
+
"Selected Swarm is not registered; run ipab swarm add first.",
|
|
1538
|
+
);
|
|
1539
|
+
const urls = new Set(registrations.map((entry) => entry.cid));
|
|
1540
|
+
for (const url of sessions.keys())
|
|
1541
|
+
if (!urls.has(url)) {
|
|
1542
|
+
await persist(url, sessions.get(url)!, false);
|
|
1543
|
+
sessions.delete(url);
|
|
1544
|
+
}
|
|
1545
|
+
await boundedMap(registrations, async ({ cid: url }) => {
|
|
1546
|
+
let session = sessions.get(url);
|
|
1547
|
+
if (!session) {
|
|
1548
|
+
session = {
|
|
1549
|
+
context: { ...context },
|
|
1550
|
+
candidates: new PivotCandidates(),
|
|
1551
|
+
nextDiscovery: 0,
|
|
1552
|
+
healthy: 0,
|
|
1553
|
+
selected: new Set(),
|
|
1554
|
+
verifiedAt: null,
|
|
1555
|
+
error: null,
|
|
1556
|
+
};
|
|
1557
|
+
sessions.set(url, session);
|
|
1558
|
+
}
|
|
1559
|
+
try {
|
|
1560
|
+
if (Date.now() >= session.nextDiscovery || session.healthy < 1) {
|
|
1561
|
+
const record = await approvedMembership(
|
|
1562
|
+
session.context,
|
|
1563
|
+
url,
|
|
1564
|
+
signal,
|
|
1565
|
+
);
|
|
1566
|
+
const c = coordinates(url);
|
|
1567
|
+
const scope = nodeScope(c.service, record.config!.scopeId);
|
|
1568
|
+
const existing = await readPrivate<unknown>(
|
|
1569
|
+
join(context.stateDir, "swarm-nodes", scope),
|
|
1570
|
+
"node.json",
|
|
1571
|
+
);
|
|
1572
|
+
if (!existing)
|
|
1573
|
+
throw new HostError(
|
|
1574
|
+
"No existing managed node; activate this membership before running daemon.",
|
|
1575
|
+
);
|
|
1576
|
+
await saveLocal(session.context, record);
|
|
1577
|
+
if (
|
|
1578
|
+
!session.record?.config ||
|
|
1579
|
+
networkingBinding(url, record.account, record.config!) !==
|
|
1580
|
+
networkingBinding(
|
|
1581
|
+
url,
|
|
1582
|
+
session.record.account,
|
|
1583
|
+
session.record.config,
|
|
1584
|
+
)
|
|
1585
|
+
) {
|
|
1586
|
+
session.candidates.reconcile(new Map());
|
|
1587
|
+
session.selected.clear();
|
|
1588
|
+
session.verifiedAt = null;
|
|
1589
|
+
delete session.connections;
|
|
1590
|
+
delete session.node;
|
|
1591
|
+
}
|
|
1592
|
+
session.record = record;
|
|
1593
|
+
const result = await privateSource(
|
|
1594
|
+
session.context,
|
|
1595
|
+
record,
|
|
1596
|
+
signal,
|
|
1597
|
+
session.candidates,
|
|
1598
|
+
);
|
|
1599
|
+
session.node = result.node;
|
|
1600
|
+
session.selected = new Set(
|
|
1601
|
+
result.peers.map((address) => address.split("/").at(-1)!),
|
|
1602
|
+
);
|
|
1603
|
+
session.error = result.observationError;
|
|
1604
|
+
if (!session.error) session.verifiedAt = Date.now();
|
|
1605
|
+
session.context.node = result.node;
|
|
1606
|
+
session.nextDiscovery = Date.now() + 120_000;
|
|
1607
|
+
await persist(url, session, true);
|
|
1608
|
+
}
|
|
1609
|
+
if (!session.node) return;
|
|
1610
|
+
let healthy = 0;
|
|
1611
|
+
await boundedMap(
|
|
1612
|
+
[...session.candidates.active()],
|
|
1613
|
+
async ([id, addresses]) => {
|
|
1614
|
+
let success = false;
|
|
1615
|
+
let error: string | null = null;
|
|
1616
|
+
try {
|
|
1617
|
+
success = await probePeer(
|
|
1618
|
+
session.node!,
|
|
1619
|
+
id,
|
|
1620
|
+
addresses,
|
|
1621
|
+
signal,
|
|
1622
|
+
);
|
|
1623
|
+
} catch (cause) {
|
|
1624
|
+
signal.throwIfAborted();
|
|
1625
|
+
error = message(cause);
|
|
1626
|
+
context.diagnostic?.(
|
|
1627
|
+
`[${url}] Pivot probe ${id}: ${message(cause)}`,
|
|
1628
|
+
);
|
|
1629
|
+
}
|
|
1630
|
+
session.candidates.result(
|
|
1631
|
+
id,
|
|
1632
|
+
success,
|
|
1633
|
+
Date.now(),
|
|
1634
|
+
Math.random(),
|
|
1635
|
+
success ? null : (error ?? "Pivot dial failed."),
|
|
1636
|
+
);
|
|
1637
|
+
if (success) healthy++;
|
|
1638
|
+
else {
|
|
1639
|
+
session.nextDiscovery = 0;
|
|
1640
|
+
context.diagnostic?.(
|
|
1641
|
+
`[${url}] Pivot ${id} dial failed; refreshing profile, with cooldown after repeated failure.`,
|
|
1642
|
+
);
|
|
1643
|
+
}
|
|
1644
|
+
},
|
|
1645
|
+
);
|
|
1646
|
+
session.healthy = healthy;
|
|
1647
|
+
// Startup-captured relay/bootstrap options must be removed immediately
|
|
1648
|
+
// when a candidate enters cooldown, not merely changed through config RPC.
|
|
1649
|
+
const record = session.record!;
|
|
1650
|
+
const self = record.config!.members.find(
|
|
1651
|
+
(member) => member.publicKey === record.account,
|
|
1652
|
+
)!;
|
|
1653
|
+
const desired = memberPeers(
|
|
1654
|
+
self.peerId,
|
|
1655
|
+
session.candidates.active(),
|
|
1656
|
+
[],
|
|
1657
|
+
);
|
|
1658
|
+
session.node = await startNode(
|
|
1659
|
+
context.stateDir,
|
|
1660
|
+
nodeScope(coordinates(url).service, record.config!.scopeId),
|
|
1661
|
+
record.secret!.swarmKey,
|
|
1662
|
+
record.config!.epoch,
|
|
1663
|
+
context.listen,
|
|
1664
|
+
{
|
|
1665
|
+
self: self.peerId,
|
|
1666
|
+
pivot: context.pivot ?? false,
|
|
1667
|
+
peers: desired,
|
|
1668
|
+
signal,
|
|
1669
|
+
},
|
|
1670
|
+
);
|
|
1671
|
+
session.selected = new Set(desired.map(({ ID }) => ID));
|
|
1672
|
+
try {
|
|
1673
|
+
const peers = await nodeConnections(session.node, signal);
|
|
1674
|
+
session.connections = {
|
|
1675
|
+
observedAt: Date.now(),
|
|
1676
|
+
error: null,
|
|
1677
|
+
peers,
|
|
1678
|
+
};
|
|
1679
|
+
} catch (cause) {
|
|
1680
|
+
signal.throwIfAborted();
|
|
1681
|
+
session.connections = {
|
|
1682
|
+
observedAt: session.connections?.observedAt ?? null,
|
|
1683
|
+
error: message(cause).slice(0, 2048),
|
|
1684
|
+
peers: session.connections?.peers ?? [],
|
|
1685
|
+
};
|
|
1686
|
+
}
|
|
1687
|
+
} catch (cause) {
|
|
1688
|
+
if (signal.aborted) return;
|
|
1689
|
+
session.error = message(cause);
|
|
1690
|
+
session.nextDiscovery = 0;
|
|
1691
|
+
context.diagnostic?.(
|
|
1692
|
+
`[${url}] Daemon refresh failed (last-known local candidates retained): ${message(cause)}`,
|
|
1693
|
+
);
|
|
1694
|
+
if (
|
|
1695
|
+
cause instanceof HostError &&
|
|
1696
|
+
/membership removed|identity was removed|epoch rollback|Pinned Owner changed/.test(
|
|
1697
|
+
cause.message,
|
|
1698
|
+
)
|
|
1699
|
+
) {
|
|
1700
|
+
if (session.node) await stopNode(session.node);
|
|
1701
|
+
session.candidates.reconcile(new Map());
|
|
1702
|
+
delete session.node;
|
|
1703
|
+
session.selected.clear();
|
|
1704
|
+
await persist(url, session, false);
|
|
1705
|
+
delete session.record;
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
await persist(url, session, true);
|
|
1709
|
+
});
|
|
1710
|
+
try {
|
|
1711
|
+
await delay(30_000 + Math.random() * 15_000, undefined, { signal });
|
|
1712
|
+
} catch {
|
|
1713
|
+
signal.throwIfAborted();
|
|
1714
|
+
}
|
|
1715
|
+
}
|
|
1716
|
+
} finally {
|
|
1717
|
+
for (const [url, session] of sessions)
|
|
1718
|
+
await persist(url, session, false);
|
|
1719
|
+
}
|
|
1720
|
+
},
|
|
1721
|
+
signal,
|
|
1722
|
+
1,
|
|
1723
|
+
);
|
|
1724
|
+
}
|
|
977
1725
|
async function activate(context: SwarmContext, record: LocalMembership) {
|
|
978
1726
|
const { node, peers, source } = await privateSource(context, record);
|
|
979
1727
|
if (
|
|
@@ -1225,10 +1973,51 @@ export async function jsonBundle(
|
|
|
1225
1973
|
await encoded.close();
|
|
1226
1974
|
}
|
|
1227
1975
|
}
|
|
1976
|
+
async function writeStateBundle(
|
|
1977
|
+
store: IpfsStore,
|
|
1978
|
+
configuration: SwarmMeta,
|
|
1979
|
+
targets: Record<string, CID>,
|
|
1980
|
+
configUrl?: string,
|
|
1981
|
+
): Promise<string> {
|
|
1982
|
+
const meta = parseSwarmMeta(configuration);
|
|
1983
|
+
const names = Object.fromEntries(
|
|
1984
|
+
Object.entries(targets).map(([name, cid]) => [
|
|
1985
|
+
formalName(name),
|
|
1986
|
+
cid.toString(),
|
|
1987
|
+
]),
|
|
1988
|
+
);
|
|
1989
|
+
const value = parseSwarmState({
|
|
1990
|
+
v: configUrl === undefined ? 5 : 6,
|
|
1991
|
+
...(configUrl === undefined ? {} : { configUrl }),
|
|
1992
|
+
...meta,
|
|
1993
|
+
names,
|
|
1994
|
+
});
|
|
1995
|
+
return jsonBundle(
|
|
1996
|
+
store,
|
|
1997
|
+
configUrl !== undefined
|
|
1998
|
+
? swarmStateType
|
|
1999
|
+
: meta.skillPath === undefined
|
|
2000
|
+
? legacySwarmStateType
|
|
2001
|
+
: previousSwarmStateType,
|
|
2002
|
+
value,
|
|
2003
|
+
);
|
|
2004
|
+
}
|
|
1228
2005
|
export async function writeSnapshot(
|
|
1229
2006
|
store: IpfsStore,
|
|
1230
2007
|
snapshot: Snapshot,
|
|
2008
|
+
configUrl?: string,
|
|
1231
2009
|
): Promise<string> {
|
|
2010
|
+
if (snapshot.stateBundle) {
|
|
2011
|
+
if (Object.keys(snapshot.registrations ?? {}).length)
|
|
2012
|
+
throw new HostError("State Bundles cannot contain legacy registrations.");
|
|
2013
|
+
checkStateOrigin(snapshot, configUrl);
|
|
2014
|
+
return writeStateBundle(
|
|
2015
|
+
store,
|
|
2016
|
+
snapshot.stateBundle.meta,
|
|
2017
|
+
snapshot.names,
|
|
2018
|
+
snapshot.stateBundle.configUrl,
|
|
2019
|
+
);
|
|
2020
|
+
}
|
|
1232
2021
|
const put = async (value: unknown) => {
|
|
1233
2022
|
const encoded = dagCbor.encode(value);
|
|
1234
2023
|
const cid = CID.createV1(dagCbor.code, await sha256.digest(encoded));
|
|
@@ -1236,12 +2025,62 @@ export async function writeSnapshot(
|
|
|
1236
2025
|
return cid;
|
|
1237
2026
|
};
|
|
1238
2027
|
const names = await put(snapshot.names);
|
|
1239
|
-
|
|
1240
|
-
|
|
2028
|
+
if (Object.keys(snapshot.registrations ?? {}).length) {
|
|
2029
|
+
const registrations = await put(snapshot.registrations);
|
|
2030
|
+
return (await put({ v: 3, names, registrations })).toString();
|
|
2031
|
+
}
|
|
2032
|
+
return (await put({ v: 4, names })).toString();
|
|
1241
2033
|
}
|
|
1242
2034
|
async function readState(store: IpfsStore, cid: CID): Promise<Snapshot> {
|
|
2035
|
+
if (cid.code !== dagCbor.code) {
|
|
2036
|
+
const bundle = await openBundle(cid, {
|
|
2037
|
+
resolveCid: (key) => store.block.get(key.toString()),
|
|
2038
|
+
});
|
|
2039
|
+
const type =
|
|
2040
|
+
bundle.schemaKey === legacySwarmStateSchema.cid.toString()
|
|
2041
|
+
? legacySwarmStateType
|
|
2042
|
+
: bundle.schemaKey === previousSwarmStateSchema.cid.toString()
|
|
2043
|
+
? previousSwarmStateType
|
|
2044
|
+
: swarmStateType;
|
|
2045
|
+
const value = parseSwarmState(await typedJson(store, cid.toString(), type));
|
|
2046
|
+
const meta = parseSwarmMeta({
|
|
2047
|
+
templatePath: value.templatePath,
|
|
2048
|
+
registryPath: value.registryPath,
|
|
2049
|
+
...(value.skillPath === undefined ? {} : { skillPath: value.skillPath }),
|
|
2050
|
+
});
|
|
2051
|
+
const names = Object.fromEntries(
|
|
2052
|
+
Object.entries(object(value.names, "Names")).map(([name, target]) => {
|
|
2053
|
+
formalName(name);
|
|
2054
|
+
if (typeof target !== "string" || exactCid(target) !== target)
|
|
2055
|
+
throw new HostError("State Names require exact canonical CIDs.");
|
|
2056
|
+
return [name, CID.parse(target)];
|
|
2057
|
+
}),
|
|
2058
|
+
);
|
|
2059
|
+
return {
|
|
2060
|
+
v: 2,
|
|
2061
|
+
names,
|
|
2062
|
+
stateBundle: {
|
|
2063
|
+
meta,
|
|
2064
|
+
cid: cid.toString(),
|
|
2065
|
+
...(value.configUrl === undefined
|
|
2066
|
+
? {}
|
|
2067
|
+
: { configUrl: value.configUrl }),
|
|
2068
|
+
},
|
|
2069
|
+
};
|
|
2070
|
+
}
|
|
1243
2071
|
const data = await store.block.get(cid.toString());
|
|
1244
2072
|
const root = object(dagCbor.decode(data), "State");
|
|
2073
|
+
if (root.v === 4) {
|
|
2074
|
+
const names = CID.asCID(root.names);
|
|
2075
|
+
if (!names || Object.keys(root).sort().join() !== "names,v")
|
|
2076
|
+
throw new HostError("Invalid State Names root.");
|
|
2077
|
+
return decodeSnapshot(
|
|
2078
|
+
dagCbor.encode({
|
|
2079
|
+
v: 2,
|
|
2080
|
+
names: dagCbor.decode(await store.block.get(names.toString())),
|
|
2081
|
+
}),
|
|
2082
|
+
);
|
|
2083
|
+
}
|
|
1245
2084
|
if (root.v !== 3) return decodeSnapshot(data);
|
|
1246
2085
|
const names = CID.asCID(root.names),
|
|
1247
2086
|
registrations = CID.asCID(root.registrations);
|
|
@@ -1317,14 +2156,33 @@ function decodeSnapshot(data: Uint8Array): Snapshot {
|
|
|
1317
2156
|
export async function readSnapshot(
|
|
1318
2157
|
store: IpfsStore,
|
|
1319
2158
|
root: string,
|
|
2159
|
+
configUrl?: string,
|
|
1320
2160
|
): Promise<Snapshot> {
|
|
1321
2161
|
const publication = await readPublication(store, root);
|
|
1322
|
-
|
|
2162
|
+
const snapshot = await readState(store, publication.state);
|
|
2163
|
+
checkStateOrigin(snapshot, configUrl);
|
|
2164
|
+
return snapshot;
|
|
2165
|
+
}
|
|
2166
|
+
function checkStateOrigin(snapshot: Snapshot, configUrl?: string): void {
|
|
2167
|
+
if (
|
|
2168
|
+
configUrl !== undefined &&
|
|
2169
|
+
snapshot.stateBundle?.configUrl !== undefined &&
|
|
2170
|
+
snapshot.stateBundle.configUrl !== configUrl
|
|
2171
|
+
)
|
|
2172
|
+
throw new HostError(
|
|
2173
|
+
"State Bundle Config URL does not match the selected Swarm.",
|
|
2174
|
+
);
|
|
1323
2175
|
}
|
|
1324
|
-
async function readHeaderSnapshot(
|
|
2176
|
+
async function readHeaderSnapshot(
|
|
2177
|
+
store: IpfsStore,
|
|
2178
|
+
root: string,
|
|
2179
|
+
configUrl: string,
|
|
2180
|
+
) {
|
|
1325
2181
|
const publication = await readPublication(store, root);
|
|
2182
|
+
const snapshot = await readState(store, publication.state);
|
|
2183
|
+
checkStateOrigin(snapshot, configUrl);
|
|
1326
2184
|
return {
|
|
1327
|
-
snapshot
|
|
2185
|
+
snapshot,
|
|
1328
2186
|
stateCid: publication.state.toString(),
|
|
1329
2187
|
};
|
|
1330
2188
|
}
|
|
@@ -1378,6 +2236,16 @@ export interface SwarmHeader {
|
|
|
1378
2236
|
projection?: SwarmProjection;
|
|
1379
2237
|
localSourceReady?: boolean;
|
|
1380
2238
|
}
|
|
2239
|
+
function missingContent(cause: unknown): boolean {
|
|
2240
|
+
let failure: unknown = cause;
|
|
2241
|
+
for (
|
|
2242
|
+
let depth = 0;
|
|
2243
|
+
depth < 8 && failure instanceof ContentBundleError;
|
|
2244
|
+
depth++
|
|
2245
|
+
)
|
|
2246
|
+
failure = failure.cause;
|
|
2247
|
+
return failure instanceof ContentError && failure.code === "missing_block";
|
|
2248
|
+
}
|
|
1381
2249
|
export async function sharedSwarmStatus(
|
|
1382
2250
|
context: SwarmContext,
|
|
1383
2251
|
configUrl: string,
|
|
@@ -1389,10 +2257,9 @@ export async function sharedSwarmStatus(
|
|
|
1389
2257
|
);
|
|
1390
2258
|
let content: Awaited<ReturnType<typeof readHeaderSnapshot>>;
|
|
1391
2259
|
try {
|
|
1392
|
-
content = await readHeaderSnapshot(context.store, head.root);
|
|
2260
|
+
content = await readHeaderSnapshot(context.store, head.root, configUrl);
|
|
1393
2261
|
} catch (cause) {
|
|
1394
|
-
if (!(cause
|
|
1395
|
-
throw cause;
|
|
2262
|
+
if (!missingContent(cause)) throw cause;
|
|
1396
2263
|
const registered = withState(context, (state) =>
|
|
1397
2264
|
state.swarms().some((entry) => entry.cid === configUrl),
|
|
1398
2265
|
);
|
|
@@ -1478,12 +2345,12 @@ async function readSwarmHeader(
|
|
|
1478
2345
|
: await (await boundPointer(context, configUrl, record)).read();
|
|
1479
2346
|
let content: Awaited<ReturnType<typeof readHeaderSnapshot>>;
|
|
1480
2347
|
try {
|
|
1481
|
-
content = await readHeaderSnapshot(context.store, head.root);
|
|
2348
|
+
content = await readHeaderSnapshot(context.store, head.root, configUrl);
|
|
1482
2349
|
} catch (cause) {
|
|
1483
2350
|
if (mode === "local-first") throw cause;
|
|
1484
2351
|
record = await refresh(context, configUrl);
|
|
1485
2352
|
head = await (await boundPointer(context, configUrl, record)).read();
|
|
1486
|
-
content = await readHeaderSnapshot(context.store, head.root);
|
|
2353
|
+
content = await readHeaderSnapshot(context.store, head.root, configUrl);
|
|
1487
2354
|
}
|
|
1488
2355
|
const current = await local(context, configUrl),
|
|
1489
2356
|
verifiedAt = Date.now();
|
|
@@ -1536,7 +2403,39 @@ async function cachedHeader(
|
|
|
1536
2403
|
!(age >= 0 && age < (context.cacheTtl ?? 300) * 1000)
|
|
1537
2404
|
)
|
|
1538
2405
|
return;
|
|
1539
|
-
const
|
|
2406
|
+
const cachedSnapshot = object(
|
|
2407
|
+
dagCbor.decode(Buffer.from(cached.snapshot, "base64")),
|
|
2408
|
+
"cached State",
|
|
2409
|
+
);
|
|
2410
|
+
const { stateBundle, ...legacySnapshot } = cachedSnapshot;
|
|
2411
|
+
const snapshot = decodeSnapshot(dagCbor.encode(legacySnapshot));
|
|
2412
|
+
if (stateBundle !== undefined) {
|
|
2413
|
+
const data = object(stateBundle, "cached State Bundle");
|
|
2414
|
+
if (
|
|
2415
|
+
!["cid,meta", "cid,configUrl,meta"].includes(
|
|
2416
|
+
Object.keys(data).sort().join(),
|
|
2417
|
+
) ||
|
|
2418
|
+
typeof data.cid !== "string" ||
|
|
2419
|
+
exactCid(data.cid) !== data.cid ||
|
|
2420
|
+
(cached.stateCid !== undefined && data.cid !== cached.stateCid)
|
|
2421
|
+
)
|
|
2422
|
+
throw new HostError("Invalid cached State Bundle.");
|
|
2423
|
+
const meta = parseSwarmMeta(data.meta);
|
|
2424
|
+
const parsed = parseSwarmState({
|
|
2425
|
+
v: data.configUrl === undefined ? 5 : 6,
|
|
2426
|
+
...meta,
|
|
2427
|
+
...(data.configUrl === undefined ? {} : { configUrl: data.configUrl }),
|
|
2428
|
+
names: {},
|
|
2429
|
+
});
|
|
2430
|
+
snapshot.stateBundle = {
|
|
2431
|
+
meta,
|
|
2432
|
+
cid: data.cid,
|
|
2433
|
+
...(parsed.configUrl === undefined
|
|
2434
|
+
? {}
|
|
2435
|
+
: { configUrl: parsed.configUrl }),
|
|
2436
|
+
};
|
|
2437
|
+
checkStateOrigin(snapshot, record.configUrl);
|
|
2438
|
+
}
|
|
1540
2439
|
let stateCid = cached.stateCid;
|
|
1541
2440
|
if (!stateCid) {
|
|
1542
2441
|
try {
|
|
@@ -1582,9 +2481,7 @@ function projectHeader(
|
|
|
1582
2481
|
membership: JSON.stringify({ owner: record.owner, member }),
|
|
1583
2482
|
};
|
|
1584
2483
|
withState(context, (state) => {
|
|
1585
|
-
state.projectSwarm(binding, header.snapshot.names, () =>
|
|
1586
|
-
snapshotAssociations(header.snapshot),
|
|
1587
|
-
);
|
|
2484
|
+
state.projectSwarm(binding, header.snapshot.names, () => []);
|
|
1588
2485
|
});
|
|
1589
2486
|
header.projection = binding;
|
|
1590
2487
|
}
|
|
@@ -1666,7 +2563,7 @@ async function lazyPrivateSource(
|
|
|
1666
2563
|
record.config!.headId !== basis.headId
|
|
1667
2564
|
)
|
|
1668
2565
|
throw new HostError(
|
|
1669
|
-
"Swarm membership changed since this cached HEAD; retry with --
|
|
2566
|
+
"Swarm membership changed since this cached HEAD; retry with --refresh.",
|
|
1670
2567
|
);
|
|
1671
2568
|
return (await privateSource(scoped, record, signal)).source;
|
|
1672
2569
|
};
|
|
@@ -1695,16 +2592,10 @@ async function lazyPrivateSource(
|
|
|
1695
2592
|
await pending?.catch(() => {});
|
|
1696
2593
|
};
|
|
1697
2594
|
}
|
|
1698
|
-
export async function addSwarm(
|
|
1699
|
-
context: SwarmContext,
|
|
1700
|
-
url: string,
|
|
1701
|
-
alias?: string,
|
|
1702
|
-
): Promise<void> {
|
|
1703
|
-
canonicalSwarmUrl(url);
|
|
1704
|
-
await readyMembership(context, url);
|
|
1705
|
-
withState(context, (state) => state.registerSwarm(url, alias));
|
|
1706
|
-
}
|
|
1707
2595
|
interface Creation {
|
|
2596
|
+
stateVersion?: 6;
|
|
2597
|
+
configReservationCid?: string;
|
|
2598
|
+
configPublished?: boolean;
|
|
1708
2599
|
owner: string;
|
|
1709
2600
|
service: string;
|
|
1710
2601
|
encryption: EncryptionIdentity;
|
|
@@ -1719,6 +2610,7 @@ interface Creation {
|
|
|
1719
2610
|
profileId?: string;
|
|
1720
2611
|
configId?: string;
|
|
1721
2612
|
config?: Organization;
|
|
2613
|
+
seed?: { root: string; car: string; roots: string[] };
|
|
1722
2614
|
}
|
|
1723
2615
|
export async function createSwarm(
|
|
1724
2616
|
context: SwarmContext,
|
|
@@ -1766,6 +2658,7 @@ export async function createSwarm(
|
|
|
1766
2658
|
if (!record) {
|
|
1767
2659
|
if (options.resume) throw new HostError("Unknown creation operation.");
|
|
1768
2660
|
record = {
|
|
2661
|
+
stateVersion: 6,
|
|
1769
2662
|
owner: identity.publicKey,
|
|
1770
2663
|
service,
|
|
1771
2664
|
encryption: encryptionIdentity(),
|
|
@@ -1782,6 +2675,75 @@ export async function createSwarm(
|
|
|
1782
2675
|
const save = () => writePrivate(dir, `creation-${op}.json`, record);
|
|
1783
2676
|
const api = client(context, service, identity);
|
|
1784
2677
|
try {
|
|
2678
|
+
if (record.stateVersion === 6) {
|
|
2679
|
+
if (!record.scopeId) {
|
|
2680
|
+
record.scopeId = (await api.createScope(`${op}.scope`)).id;
|
|
2681
|
+
await save();
|
|
2682
|
+
}
|
|
2683
|
+
if (!record.configId) {
|
|
2684
|
+
// Reserve the Config address, not a Config or a temporary HEAD.
|
|
2685
|
+
const reservation = await api.create(
|
|
2686
|
+
record.scopeId,
|
|
2687
|
+
{
|
|
2688
|
+
v: 1,
|
|
2689
|
+
kind: "ipab-config-reservation",
|
|
2690
|
+
owner: record.owner,
|
|
2691
|
+
operation: op,
|
|
2692
|
+
},
|
|
2693
|
+
`${op}.config-reserve`,
|
|
2694
|
+
);
|
|
2695
|
+
record.configId = reservation.id;
|
|
2696
|
+
record.configReservationCid = reservation.cid;
|
|
2697
|
+
await save();
|
|
2698
|
+
}
|
|
2699
|
+
}
|
|
2700
|
+
if (!record.seed) {
|
|
2701
|
+
if (record.headId)
|
|
2702
|
+
throw new HostError(
|
|
2703
|
+
"Legacy creation has an unseeded HEAD. Preserve this recovery record and explicitly migrate the Swarm; it cannot complete as a newly seeded Swarm.",
|
|
2704
|
+
);
|
|
2705
|
+
const names: Record<string, CID> = {};
|
|
2706
|
+
for (const selector of seedTemplateNames) {
|
|
2707
|
+
const template = await seedTemplateSource(selector);
|
|
2708
|
+
for (const [cid, data] of template.blocks)
|
|
2709
|
+
await context.store.put(CID.parse(cid), data);
|
|
2710
|
+
names[`ipab/templates/${selector}`] = CID.parse(template.cid);
|
|
2711
|
+
}
|
|
2712
|
+
const skill = await seedSkillSource();
|
|
2713
|
+
for (const [cid, data] of skill.blocks)
|
|
2714
|
+
await context.store.put(CID.parse(cid), data);
|
|
2715
|
+
names["ipab/skills/ipab"] = CID.parse(skill.cid);
|
|
2716
|
+
const root = await publication(
|
|
2717
|
+
context.store,
|
|
2718
|
+
await writeStateBundle(
|
|
2719
|
+
context.store,
|
|
2720
|
+
{
|
|
2721
|
+
templatePath: "ipab/templates",
|
|
2722
|
+
registryPath: "ipab/registry",
|
|
2723
|
+
skillPath: "ipab/skills",
|
|
2724
|
+
},
|
|
2725
|
+
names,
|
|
2726
|
+
record.stateVersion === 6
|
|
2727
|
+
? `${service}/v1/${record.scopeId!}/${record.configId!}`
|
|
2728
|
+
: undefined,
|
|
2729
|
+
),
|
|
2730
|
+
);
|
|
2731
|
+
const roots = await frozenPublicationClosure(context.store, root);
|
|
2732
|
+
record.seed = {
|
|
2733
|
+
root,
|
|
2734
|
+
roots,
|
|
2735
|
+
car: Buffer.from(await contentCar(context.store, roots)).toString(
|
|
2736
|
+
"base64",
|
|
2737
|
+
),
|
|
2738
|
+
};
|
|
2739
|
+
await save();
|
|
2740
|
+
}
|
|
2741
|
+
const seedBytes = Buffer.from(record.seed.car, "base64");
|
|
2742
|
+
const seed = await CarBlockIterator.fromIterable(
|
|
2743
|
+
streamChunks(new Blob([seedBytes]).stream()),
|
|
2744
|
+
);
|
|
2745
|
+
for await (const block of seed)
|
|
2746
|
+
await context.store.put(block.cid, block.bytes);
|
|
1785
2747
|
if (!record.scopeId) {
|
|
1786
2748
|
record.scopeId = (await api.createScope(`${op}.scope`)).id;
|
|
1787
2749
|
await save();
|
|
@@ -1792,23 +2754,24 @@ export async function createSwarm(
|
|
|
1792
2754
|
context.listen,
|
|
1793
2755
|
);
|
|
1794
2756
|
if (!record.headId) {
|
|
1795
|
-
const root = await publication(
|
|
1796
|
-
context.store,
|
|
1797
|
-
await writeSnapshot(context.store, { v: 2, names: {} }),
|
|
1798
|
-
);
|
|
1799
2757
|
record.headId = (
|
|
1800
|
-
await api.create(
|
|
2758
|
+
await api.create(
|
|
2759
|
+
record.scopeId,
|
|
2760
|
+
{ root: record.seed.root },
|
|
2761
|
+
`${op}.head`,
|
|
2762
|
+
)
|
|
1801
2763
|
).id;
|
|
1802
2764
|
await save();
|
|
1803
2765
|
}
|
|
1804
2766
|
if (!record.profileId) {
|
|
1805
2767
|
if (!record.profile) {
|
|
1806
|
-
const addresses = options.addresses ??
|
|
2768
|
+
const addresses = options.addresses ?? [];
|
|
1807
2769
|
for (const address of addresses)
|
|
1808
2770
|
validateAddress(address, node.peerId);
|
|
1809
2771
|
record.profile = {
|
|
1810
2772
|
...(record.personal ?? { name: record.name }),
|
|
1811
2773
|
addresses,
|
|
2774
|
+
pivot: context.pivot ?? false,
|
|
1812
2775
|
updatedAt: record.createdAt,
|
|
1813
2776
|
};
|
|
1814
2777
|
await save();
|
|
@@ -1840,7 +2803,7 @@ export async function createSwarm(
|
|
|
1840
2803
|
description: record.description,
|
|
1841
2804
|
headId: record.headId,
|
|
1842
2805
|
epoch: 1,
|
|
1843
|
-
bootstrap:
|
|
2806
|
+
bootstrap: [],
|
|
1844
2807
|
members: [member],
|
|
1845
2808
|
};
|
|
1846
2809
|
await save();
|
|
@@ -1855,6 +2818,29 @@ export async function createSwarm(
|
|
|
1855
2818
|
).id;
|
|
1856
2819
|
await save();
|
|
1857
2820
|
}
|
|
2821
|
+
if (record.stateVersion === 6 && !record.configPublished) {
|
|
2822
|
+
if (!record.configReservationCid)
|
|
2823
|
+
throw new HostError(
|
|
2824
|
+
"Creation Config reservation checkpoint is missing.",
|
|
2825
|
+
);
|
|
2826
|
+
const value = configValue(record.config);
|
|
2827
|
+
const current = await api.read(record.scopeId, record.configId);
|
|
2828
|
+
if (current.cid !== (await valueCid(value))) {
|
|
2829
|
+
if (current.cid !== record.configReservationCid)
|
|
2830
|
+
throw new HostError(
|
|
2831
|
+
"Creation Config reservation was changed; refusing to overwrite it.",
|
|
2832
|
+
);
|
|
2833
|
+
await api.write(
|
|
2834
|
+
record.scopeId,
|
|
2835
|
+
record.configId,
|
|
2836
|
+
value,
|
|
2837
|
+
record.configReservationCid,
|
|
2838
|
+
`${op}.config`,
|
|
2839
|
+
);
|
|
2840
|
+
}
|
|
2841
|
+
record.configPublished = true;
|
|
2842
|
+
await save();
|
|
2843
|
+
}
|
|
1858
2844
|
const configUrl = `${service}/v1/${record.scopeId}/${record.configId}`;
|
|
1859
2845
|
const cert = await issueScopeCertificate(
|
|
1860
2846
|
{
|
|
@@ -1879,7 +2865,7 @@ export async function createSwarm(
|
|
|
1879
2865
|
const head = await api.read(record.scopeId, record.headId);
|
|
1880
2866
|
await provideContent(
|
|
1881
2867
|
context.store,
|
|
1882
|
-
[headValue(head).root],
|
|
2868
|
+
[...new Set([headValue(head).root, ...record.seed.roots])],
|
|
1883
2869
|
context.node!.api,
|
|
1884
2870
|
context.fetch,
|
|
1885
2871
|
context.node!.auth,
|
|
@@ -1918,7 +2904,6 @@ export async function requestMembership(
|
|
|
1918
2904
|
context: SwarmContext,
|
|
1919
2905
|
url: string,
|
|
1920
2906
|
options: {
|
|
1921
|
-
alias?: string;
|
|
1922
2907
|
output?: string;
|
|
1923
2908
|
},
|
|
1924
2909
|
): Promise<{
|
|
@@ -1937,10 +2922,6 @@ export async function requestMembership(
|
|
|
1937
2922
|
const personal = await ensureAccountProfile(context);
|
|
1938
2923
|
const dir = await directory(context);
|
|
1939
2924
|
await withPrivateLock(dir, async () => {
|
|
1940
|
-
if (options.alias !== undefined)
|
|
1941
|
-
withState(context, (state) => {
|
|
1942
|
-
state.checkSwarmAlias(options.alias!, url);
|
|
1943
|
-
});
|
|
1944
2925
|
const existing = await readPrivate<LocalMembership>(dir, localName(url));
|
|
1945
2926
|
if (existing) {
|
|
1946
2927
|
if (
|
|
@@ -1952,7 +2933,6 @@ export async function requestMembership(
|
|
|
1952
2933
|
);
|
|
1953
2934
|
if (!existing.request)
|
|
1954
2935
|
throw new HostError("This identity is already the Owner.");
|
|
1955
|
-
withState(context, (state) => state.registerSwarm(url, options.alias));
|
|
1956
2936
|
return;
|
|
1957
2937
|
}
|
|
1958
2938
|
const node = await prepareNode(
|
|
@@ -1992,7 +2972,6 @@ export async function requestMembership(
|
|
|
1992
2972
|
encryption,
|
|
1993
2973
|
request,
|
|
1994
2974
|
});
|
|
1995
|
-
withState(context, (state) => state.registerSwarm(url, options.alias));
|
|
1996
2975
|
});
|
|
1997
2976
|
const file =
|
|
1998
2977
|
options.output === undefined ? undefined : resolve(options.output);
|
|
@@ -2392,6 +3371,13 @@ export async function updateProfile(
|
|
|
2392
3371
|
existing.cid,
|
|
2393
3372
|
uniqueId(),
|
|
2394
3373
|
);
|
|
3374
|
+
await writePrivate(await directory(context), `pivot-${localName(url)}`, {
|
|
3375
|
+
manual: changes.addresses,
|
|
3376
|
+
});
|
|
3377
|
+
if (!context.pivot)
|
|
3378
|
+
context.diagnostic?.(
|
|
3379
|
+
"Manual addresses saved; they are discovery candidates only when swarm.pivot=true. Ordinary nodes do not provide relay service.",
|
|
3380
|
+
);
|
|
2395
3381
|
return profile;
|
|
2396
3382
|
}
|
|
2397
3383
|
interface Removal {
|
|
@@ -2472,6 +3458,11 @@ export async function resumeRemoval(
|
|
|
2472
3458
|
membershipBeforeSwitch.config = record.oldConfig;
|
|
2473
3459
|
membershipBeforeSwitch.secret!.swarmKey = record.oldKey;
|
|
2474
3460
|
await activate(context, membershipBeforeSwitch);
|
|
3461
|
+
await readSnapshot(
|
|
3462
|
+
context.store,
|
|
3463
|
+
headValue(record.finalHead).root,
|
|
3464
|
+
url,
|
|
3465
|
+
);
|
|
2475
3466
|
if (!record.finalClosureRoots) {
|
|
2476
3467
|
record.finalClosureRoots = await frozenPublicationClosure(
|
|
2477
3468
|
context.store,
|
|
@@ -2717,7 +3708,7 @@ export function parseNameReference(
|
|
|
2717
3708
|
try {
|
|
2718
3709
|
return {
|
|
2719
3710
|
...(authority ? { swarm: authority } : {}),
|
|
2720
|
-
name: formalName(segments.join("/")),
|
|
3711
|
+
name: match[2] === "" ? "" : formalName(segments.join("/")),
|
|
2721
3712
|
};
|
|
2722
3713
|
} catch (error) {
|
|
2723
3714
|
if (!(error instanceof HostError)) throw error;
|
|
@@ -2758,14 +3749,16 @@ export async function resolveReference(
|
|
|
2758
3749
|
const warm =
|
|
2759
3750
|
mode === "local-first" ? await localHeader(context, selected) : undefined;
|
|
2760
3751
|
let header = warm?.header ?? (await swarmHeader(context, selected));
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
3752
|
+
const target = () =>
|
|
3753
|
+
shared.name === ""
|
|
3754
|
+
? header.stateCid
|
|
3755
|
+
: Object.hasOwn(header.snapshot.names, shared.name)
|
|
3756
|
+
? header.snapshot.names[shared.name]?.toString()
|
|
3757
|
+
: undefined;
|
|
3758
|
+
let link = target();
|
|
2764
3759
|
if (!link && header.source === "cache") {
|
|
2765
3760
|
header = await swarmHeader(context, selected, true);
|
|
2766
|
-
link =
|
|
2767
|
-
? header.snapshot.names[shared.name]
|
|
2768
|
-
: undefined;
|
|
3761
|
+
link = target();
|
|
2769
3762
|
}
|
|
2770
3763
|
if (!link)
|
|
2771
3764
|
throw new HostError(
|
|
@@ -2773,7 +3766,7 @@ export async function resolveReference(
|
|
|
2773
3766
|
);
|
|
2774
3767
|
if (warm && header === warm.header && !header.localSourceReady)
|
|
2775
3768
|
await lazyPrivateSource(context, selected, warm.account, header.config);
|
|
2776
|
-
targetCid = link
|
|
3769
|
+
targetCid = link;
|
|
2777
3770
|
} else {
|
|
2778
3771
|
if (swarm) await readyMembership(context, swarm);
|
|
2779
3772
|
targetCid = local(reference);
|
|
@@ -2784,6 +3777,7 @@ export async function resolveReference(
|
|
|
2784
3777
|
async function artifactValue(
|
|
2785
3778
|
store: IpfsStore,
|
|
2786
3779
|
targetCid: string,
|
|
3780
|
+
protect?: (cid: string) => Promise<void>,
|
|
2787
3781
|
): Promise<string> {
|
|
2788
3782
|
let cid = targetCid;
|
|
2789
3783
|
const visited = new Set<string>();
|
|
@@ -2793,6 +3787,7 @@ async function artifactValue(
|
|
|
2793
3787
|
"Artifact resolution exceeds limits or contains a cycle.",
|
|
2794
3788
|
);
|
|
2795
3789
|
visited.add(cid);
|
|
3790
|
+
await protect?.(cid);
|
|
2796
3791
|
const bundle = await openBundle(CID.parse(cid), {
|
|
2797
3792
|
resolveCid: (cid) => store.block.get(cid.toString()),
|
|
2798
3793
|
});
|
|
@@ -2832,26 +3827,14 @@ export async function pinNameSubscription(
|
|
|
2832
3827
|
const canonical = withState(context, (state) =>
|
|
2833
3828
|
pinReference(state, reference, selected),
|
|
2834
3829
|
);
|
|
2835
|
-
if (!canonical.swarm ||
|
|
3830
|
+
if (!canonical.swarm || canonical.name === undefined) {
|
|
2836
3831
|
let roots: string[];
|
|
2837
3832
|
try {
|
|
2838
3833
|
roots = [
|
|
2839
3834
|
...(await currentDependencies(context.store, canonical.reference)),
|
|
2840
3835
|
];
|
|
2841
3836
|
} catch (cause) {
|
|
2842
|
-
|
|
2843
|
-
for (
|
|
2844
|
-
let depth = 0;
|
|
2845
|
-
depth < 8 && failure instanceof ContentBundleError;
|
|
2846
|
-
depth++
|
|
2847
|
-
)
|
|
2848
|
-
failure = failure.cause;
|
|
2849
|
-
if (
|
|
2850
|
-
!selected ||
|
|
2851
|
-
!(failure instanceof ContentError) ||
|
|
2852
|
-
failure.code !== "missing_block"
|
|
2853
|
-
)
|
|
2854
|
-
throw cause;
|
|
3837
|
+
if (!selected || !missingContent(cause)) throw cause;
|
|
2855
3838
|
const swarm = withState(context, (state) => state.resolveSwarm(selected));
|
|
2856
3839
|
await readyMembership(context, swarm);
|
|
2857
3840
|
roots = [
|
|
@@ -2896,7 +3879,7 @@ async function withStateAsync<T>(
|
|
|
2896
3879
|
export async function refreshNamePins(
|
|
2897
3880
|
context: SwarmContext,
|
|
2898
3881
|
url: string,
|
|
2899
|
-
header: Pick<SwarmHeader, "snapshot">,
|
|
3882
|
+
header: Pick<SwarmHeader, "snapshot" | "stateCid">,
|
|
2900
3883
|
): Promise<void> {
|
|
2901
3884
|
const pins = withState(context, (state) =>
|
|
2902
3885
|
state
|
|
@@ -2905,9 +3888,12 @@ export async function refreshNamePins(
|
|
|
2905
3888
|
);
|
|
2906
3889
|
const failures: string[] = [];
|
|
2907
3890
|
for (const saved of pins) {
|
|
2908
|
-
const target =
|
|
2909
|
-
|
|
2910
|
-
|
|
3891
|
+
const target =
|
|
3892
|
+
saved.name === ""
|
|
3893
|
+
? header.stateCid
|
|
3894
|
+
: Object.hasOwn(header.snapshot.names, saved.name!)
|
|
3895
|
+
? header.snapshot.names[saved.name!]!.toString()
|
|
3896
|
+
: undefined;
|
|
2911
3897
|
if (!target) {
|
|
2912
3898
|
withState(context, (state) => {
|
|
2913
3899
|
failedPin(
|
|
@@ -2982,6 +3968,10 @@ async function currentDependencies(
|
|
|
2982
3968
|
throw new HostError("Invalid App source association.");
|
|
2983
3969
|
await currentDependencies(store, source.cid, found);
|
|
2984
3970
|
}
|
|
3971
|
+
} else if (isSwarmStateSchema(bundle.schemaKey)) {
|
|
3972
|
+
const snapshot = await readState(store, CID.parse(root));
|
|
3973
|
+
for (const target of Object.values(snapshot.names))
|
|
3974
|
+
await currentDependencies(store, target.toString(), found);
|
|
2985
3975
|
}
|
|
2986
3976
|
return found;
|
|
2987
3977
|
}
|
|
@@ -3043,52 +4033,461 @@ export async function searchNames(
|
|
|
3043
4033
|
}
|
|
3044
4034
|
return { matches, swarms: searched };
|
|
3045
4035
|
}
|
|
3046
|
-
function
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
|
|
3050
|
-
|
|
3051
|
-
appCid: item.appCid.toString(),
|
|
3052
|
-
argv: [...item.argv],
|
|
3053
|
-
}));
|
|
4036
|
+
function assertNamesRegistry(snapshot: Snapshot): void {
|
|
4037
|
+
if (Object.keys(snapshot.registrations ?? {}).length)
|
|
4038
|
+
throw new HostError(
|
|
4039
|
+
"Legacy shared registrations are inactive; per-association argv is unsupported and has not been dropped or migrated. Create a new seeded Swarm and explicitly register intended Apps there. Old Swarm content and history remain readable.",
|
|
4040
|
+
);
|
|
3054
4041
|
}
|
|
3055
|
-
export async function
|
|
4042
|
+
export async function swarmMeta(
|
|
4043
|
+
context: SwarmContext,
|
|
4044
|
+
header: Pick<SwarmHeader, "snapshot">,
|
|
4045
|
+
) {
|
|
4046
|
+
if (header.snapshot.stateBundle) return header.snapshot.stateBundle.meta;
|
|
4047
|
+
const cid = header.snapshot.names.ipab;
|
|
4048
|
+
if (!cid)
|
|
4049
|
+
throw new HostError(
|
|
4050
|
+
'Swarm Meta Name "ipab" is missing. Create a new seeded Swarm or explicitly publish a typed Swarm Meta Bundle; existing Swarms are never automatically upgraded.',
|
|
4051
|
+
);
|
|
4052
|
+
try {
|
|
4053
|
+
const bundle = await openBundle(cid, {
|
|
4054
|
+
resolveCid: (key) => context.store.block.get(key.toString()),
|
|
4055
|
+
});
|
|
4056
|
+
const type =
|
|
4057
|
+
bundle.schemaKey === legacySwarmMetaSchema.cid.toString()
|
|
4058
|
+
? legacySwarmMetaType
|
|
4059
|
+
: swarmMetaType;
|
|
4060
|
+
return parseSwarmMeta(await typedJson(context.store, cid.toString(), type));
|
|
4061
|
+
} catch (cause) {
|
|
4062
|
+
throw new HostError(
|
|
4063
|
+
`Invalid Swarm Meta at Name "ipab": ${message(cause)}. Publish a valid typed Meta Bundle explicitly; no builtin or legacy fallback is used.`,
|
|
4064
|
+
);
|
|
4065
|
+
}
|
|
4066
|
+
}
|
|
4067
|
+
function metaCid(snapshot: Snapshot): string {
|
|
4068
|
+
return snapshot.stateBundle?.cid ?? snapshot.names.ipab!.toString();
|
|
4069
|
+
}
|
|
4070
|
+
function hasMeta(snapshot: Snapshot): boolean {
|
|
4071
|
+
return (
|
|
4072
|
+
snapshot.stateBundle !== undefined || Object.hasOwn(snapshot.names, "ipab")
|
|
4073
|
+
);
|
|
4074
|
+
}
|
|
4075
|
+
export interface SharedSkill {
|
|
4076
|
+
name: string;
|
|
4077
|
+
cid: string;
|
|
4078
|
+
identity: string;
|
|
4079
|
+
}
|
|
4080
|
+
export async function swarmSkills(
|
|
4081
|
+
context: SwarmContext,
|
|
4082
|
+
configUrl: string,
|
|
4083
|
+
requested: readonly string[],
|
|
4084
|
+
owner: string,
|
|
4085
|
+
): Promise<SharedSkill[]> {
|
|
4086
|
+
const warm = await localHeader(context, configUrl);
|
|
4087
|
+
if (warm && !warm.header.localSourceReady)
|
|
4088
|
+
await lazyPrivateSource(
|
|
4089
|
+
context,
|
|
4090
|
+
configUrl,
|
|
4091
|
+
warm.account,
|
|
4092
|
+
warm.header.config,
|
|
4093
|
+
);
|
|
4094
|
+
let header = warm?.header ?? (await swarmHeader(context, configUrl));
|
|
4095
|
+
const roots = new Set<string>();
|
|
4096
|
+
const protect = async (cid: string) => {
|
|
4097
|
+
roots.add(cid);
|
|
4098
|
+
const values = [...roots];
|
|
4099
|
+
context.store.registerOwner(owner, values);
|
|
4100
|
+
await context.store.retain(owner, values);
|
|
4101
|
+
await context.store.confirmRetained(owner, values);
|
|
4102
|
+
};
|
|
4103
|
+
for (let attempt = 0; ; attempt++) {
|
|
4104
|
+
if (!hasMeta(header.snapshot) && header.source === "cache" && !attempt) {
|
|
4105
|
+
header = await swarmHeader(context, configUrl, true);
|
|
4106
|
+
continue;
|
|
4107
|
+
}
|
|
4108
|
+
if (hasMeta(header.snapshot)) await protect(metaCid(header.snapshot));
|
|
4109
|
+
const meta = await swarmMeta(context, header);
|
|
4110
|
+
if (!meta.skillPath)
|
|
4111
|
+
throw new HostError(
|
|
4112
|
+
header.snapshot.stateBundle
|
|
4113
|
+
? "This Swarm uses the earlier exact State Bundle type without skillPath. There is no in-place root-configuration migration command. Create a new seeded Swarm and explicitly republish reviewed Names. Publishing a user Name ipab never overrides root configuration; upgrades never rewrite it."
|
|
4114
|
+
: `This Swarm uses the legacy exact Meta type without skillPath. Explicitly build and publish a new typed Meta Bundle with templatePath and registryPath preserved and skillPath chosen, then run ipab publish ipab META_SOURCE --expect ${metaCid(header.snapshot)}. Upgrades never overwrite Meta.`,
|
|
4115
|
+
);
|
|
4116
|
+
const prefix = `${meta.skillPath}/`;
|
|
4117
|
+
const entries = Object.entries(header.snapshot.names).filter(([name]) =>
|
|
4118
|
+
name.startsWith(prefix),
|
|
4119
|
+
);
|
|
4120
|
+
for (const [, target] of entries) roots.add(target.toString());
|
|
4121
|
+
context.store.registerOwner(owner, [...roots]);
|
|
4122
|
+
const result: SharedSkill[] = [];
|
|
4123
|
+
const identities = new Set<string>();
|
|
4124
|
+
for (const [name, target] of entries.sort(([a], [b]) =>
|
|
4125
|
+
a.localeCompare(b),
|
|
4126
|
+
)) {
|
|
4127
|
+
if (name.slice(prefix.length).includes("/"))
|
|
4128
|
+
throw new HostError(
|
|
4129
|
+
`Skill Name "${name}" must be flat under ${meta.skillPath}.`,
|
|
4130
|
+
);
|
|
4131
|
+
const cid = await artifactValue(
|
|
4132
|
+
context.store,
|
|
4133
|
+
target.toString(),
|
|
4134
|
+
protect,
|
|
4135
|
+
);
|
|
4136
|
+
const bundle = await openBundle(CID.parse(cid), {
|
|
4137
|
+
resolveCid: (key) => context.store.block.get(key.toString()),
|
|
4138
|
+
});
|
|
4139
|
+
if (bundle.schemaKey !== skillSchema.cid.toString())
|
|
4140
|
+
throw new HostError(
|
|
4141
|
+
`Name "${name}" is not an exact Skill type Bundle.`,
|
|
4142
|
+
);
|
|
4143
|
+
const identity = await skillIdentity(bundle);
|
|
4144
|
+
if (identities.has(identity))
|
|
4145
|
+
throw new HostError(
|
|
4146
|
+
`Duplicate Skill installation name "${identity}" in this Swarm.`,
|
|
4147
|
+
);
|
|
4148
|
+
identities.add(identity);
|
|
4149
|
+
result.push({ name, cid, identity });
|
|
4150
|
+
}
|
|
4151
|
+
const missing = requested.filter(
|
|
4152
|
+
(reference) =>
|
|
4153
|
+
!result.some(
|
|
4154
|
+
(skill) => skill.name === reference || skill.identity === reference,
|
|
4155
|
+
),
|
|
4156
|
+
);
|
|
4157
|
+
if (missing.length && !attempt && header.source === "cache") {
|
|
4158
|
+
header = await swarmHeader(context, configUrl, true);
|
|
4159
|
+
continue;
|
|
4160
|
+
}
|
|
4161
|
+
if (missing.length)
|
|
4162
|
+
throw new HostError(
|
|
4163
|
+
`Skill identities or shared Names not found under ${meta.skillPath}: ${missing.join(", ")}.`,
|
|
4164
|
+
);
|
|
4165
|
+
return result;
|
|
4166
|
+
}
|
|
4167
|
+
}
|
|
4168
|
+
export async function resolveTemplateSelector(
|
|
4169
|
+
context: SwarmContext,
|
|
4170
|
+
selector: string,
|
|
4171
|
+
configUrl: string,
|
|
4172
|
+
): Promise<string> {
|
|
4173
|
+
formalName(selector);
|
|
4174
|
+
const warm = await localHeader(context, configUrl);
|
|
4175
|
+
if (warm && !warm.header.localSourceReady)
|
|
4176
|
+
await lazyPrivateSource(
|
|
4177
|
+
context,
|
|
4178
|
+
configUrl,
|
|
4179
|
+
warm.account,
|
|
4180
|
+
warm.header.config,
|
|
4181
|
+
);
|
|
4182
|
+
let header = warm?.header ?? (await swarmHeader(context, configUrl));
|
|
4183
|
+
if (!hasMeta(header.snapshot) && header.source === "cache")
|
|
4184
|
+
header = await swarmHeader(context, configUrl, true);
|
|
4185
|
+
for (let attempt = 0; ; attempt++) {
|
|
4186
|
+
const meta = await swarmMeta(context, header);
|
|
4187
|
+
const name = formalName(`${meta.templatePath}/${selector}`);
|
|
4188
|
+
const target = header.snapshot.names[name];
|
|
4189
|
+
if (target) return artifactValue(context.store, target.toString());
|
|
4190
|
+
if (attempt || header.source !== "cache")
|
|
4191
|
+
throw new HostError(
|
|
4192
|
+
`Template Name "${name}" was not found after refreshing HEAD.`,
|
|
4193
|
+
);
|
|
4194
|
+
header = await swarmHeader(context, configUrl, true);
|
|
4195
|
+
}
|
|
4196
|
+
}
|
|
4197
|
+
export function templateName(name: string): string {
|
|
4198
|
+
if (name.includes(":"))
|
|
4199
|
+
throw new HostError(
|
|
4200
|
+
"Template NAME must be a safe relative Name, not a URI.",
|
|
4201
|
+
);
|
|
4202
|
+
return formalName(name);
|
|
4203
|
+
}
|
|
4204
|
+
async function templateEntry(
|
|
4205
|
+
context: SwarmContext,
|
|
4206
|
+
configUrl: string,
|
|
4207
|
+
header: Pick<SwarmHeader, "snapshot">,
|
|
4208
|
+
selector: string,
|
|
4209
|
+
) {
|
|
4210
|
+
const meta = await swarmMeta(context, header);
|
|
4211
|
+
const name = formalName(`${meta.templatePath}/${templateName(selector)}`);
|
|
4212
|
+
const target = header.snapshot.names[name];
|
|
4213
|
+
if (!target)
|
|
4214
|
+
throw new HostError(
|
|
4215
|
+
`Template Name "${name}" was not found after refreshing HEAD.`,
|
|
4216
|
+
);
|
|
4217
|
+
const targetCid = target.toString();
|
|
4218
|
+
return {
|
|
4219
|
+
swarm: configUrl,
|
|
4220
|
+
templatePath: meta.templatePath,
|
|
4221
|
+
metaCid: metaCid(header.snapshot),
|
|
4222
|
+
name,
|
|
4223
|
+
selector,
|
|
4224
|
+
reference: formatNameReference(configUrl, name),
|
|
4225
|
+
targetCid,
|
|
4226
|
+
...(await inspectTemplate(
|
|
4227
|
+
context.store,
|
|
4228
|
+
await artifactValue(context.store, targetCid),
|
|
4229
|
+
)),
|
|
4230
|
+
};
|
|
4231
|
+
}
|
|
4232
|
+
export async function swarmTemplates(
|
|
4233
|
+
context: SwarmContext,
|
|
4234
|
+
configUrl: string,
|
|
4235
|
+
selector?: string,
|
|
4236
|
+
) {
|
|
4237
|
+
if (selector !== undefined) templateName(selector);
|
|
4238
|
+
const warm = await localHeader(context, configUrl);
|
|
4239
|
+
if (warm && !warm.header.localSourceReady)
|
|
4240
|
+
await lazyPrivateSource(
|
|
4241
|
+
context,
|
|
4242
|
+
configUrl,
|
|
4243
|
+
warm.account,
|
|
4244
|
+
warm.header.config,
|
|
4245
|
+
);
|
|
4246
|
+
let header =
|
|
4247
|
+
warm?.header ??
|
|
4248
|
+
(await swarmHeader(context, configUrl, undefined, "local-first"));
|
|
4249
|
+
for (let attempt = 0; ; attempt++) {
|
|
4250
|
+
if (!hasMeta(header.snapshot) && header.source === "cache" && !attempt) {
|
|
4251
|
+
header = await swarmHeader(context, configUrl, true, "local-first");
|
|
4252
|
+
continue;
|
|
4253
|
+
}
|
|
4254
|
+
const meta = await swarmMeta(context, header);
|
|
4255
|
+
const name =
|
|
4256
|
+
selector === undefined
|
|
4257
|
+
? undefined
|
|
4258
|
+
: formalName(`${meta.templatePath}/${selector}`);
|
|
4259
|
+
if (
|
|
4260
|
+
name &&
|
|
4261
|
+
!header.snapshot.names[name] &&
|
|
4262
|
+
header.source === "cache" &&
|
|
4263
|
+
!attempt
|
|
4264
|
+
) {
|
|
4265
|
+
header = await swarmHeader(context, configUrl, true, "local-first");
|
|
4266
|
+
continue;
|
|
4267
|
+
}
|
|
4268
|
+
const selectors =
|
|
4269
|
+
selector === undefined
|
|
4270
|
+
? Object.keys(header.snapshot.names)
|
|
4271
|
+
.filter((key) => key.startsWith(`${meta.templatePath}/`))
|
|
4272
|
+
.map((key) => key.slice(meta.templatePath.length + 1))
|
|
4273
|
+
.sort((a, b) => a.localeCompare(b))
|
|
4274
|
+
: [selector];
|
|
4275
|
+
const items = [];
|
|
4276
|
+
for (const item of selectors)
|
|
4277
|
+
items.push(await templateEntry(context, configUrl, header, item));
|
|
4278
|
+
return items;
|
|
4279
|
+
}
|
|
4280
|
+
}
|
|
4281
|
+
export async function setSwarmTemplate(
|
|
4282
|
+
context: SwarmContext,
|
|
4283
|
+
configUrl: string,
|
|
4284
|
+
selector: string,
|
|
4285
|
+
source: string,
|
|
4286
|
+
expect?: string,
|
|
4287
|
+
) {
|
|
4288
|
+
templateName(selector);
|
|
4289
|
+
await pointer(context, configUrl);
|
|
4290
|
+
const content = await templatePublicationSource(source, context.store);
|
|
4291
|
+
const details = await inspectTemplate(context.store, content);
|
|
4292
|
+
let location:
|
|
4293
|
+
{ name: string; templatePath: string; metaCid: string } | undefined;
|
|
4294
|
+
const targetCid = await publishContent(
|
|
4295
|
+
context,
|
|
4296
|
+
content,
|
|
4297
|
+
{
|
|
4298
|
+
swarm: configUrl,
|
|
4299
|
+
name: selector,
|
|
4300
|
+
...(expect === undefined ? {} : { expect }),
|
|
4301
|
+
},
|
|
4302
|
+
async (current) => {
|
|
4303
|
+
const meta = await swarmMeta(context, { snapshot: current });
|
|
4304
|
+
const name = formalName(`${meta.templatePath}/${selector}`);
|
|
4305
|
+
location = {
|
|
4306
|
+
name,
|
|
4307
|
+
templatePath: meta.templatePath,
|
|
4308
|
+
metaCid: metaCid(current),
|
|
4309
|
+
};
|
|
4310
|
+
return name;
|
|
4311
|
+
},
|
|
4312
|
+
);
|
|
4313
|
+
return {
|
|
4314
|
+
swarm: configUrl,
|
|
4315
|
+
...location!,
|
|
4316
|
+
selector,
|
|
4317
|
+
reference: formatNameReference(configUrl, location!.name),
|
|
4318
|
+
targetCid,
|
|
4319
|
+
...details,
|
|
4320
|
+
};
|
|
4321
|
+
}
|
|
4322
|
+
export async function removeSwarmTemplate(
|
|
3056
4323
|
context: SwarmContext,
|
|
3057
4324
|
configUrl: string,
|
|
4325
|
+
selector: string,
|
|
4326
|
+
expect: string,
|
|
4327
|
+
) {
|
|
4328
|
+
templateName(selector);
|
|
4329
|
+
const expected = exactCid(expect);
|
|
4330
|
+
const head = await pointer(context, configUrl);
|
|
4331
|
+
let basis: string | undefined;
|
|
4332
|
+
for (let attempt = 0; attempt < 8; attempt++) {
|
|
4333
|
+
const currentHead = await head.read();
|
|
4334
|
+
const current = await readSnapshot(
|
|
4335
|
+
context.store,
|
|
4336
|
+
currentHead.root,
|
|
4337
|
+
configUrl,
|
|
4338
|
+
);
|
|
4339
|
+
const meta = await swarmMeta(context, { snapshot: current });
|
|
4340
|
+
const name = formalName(`${meta.templatePath}/${selector}`);
|
|
4341
|
+
if (basis !== undefined && basis !== name)
|
|
4342
|
+
throw new HostError(
|
|
4343
|
+
"Template Meta path changed during removal; inspect and retry.",
|
|
4344
|
+
3,
|
|
4345
|
+
"EDIT_CONFLICT",
|
|
4346
|
+
"publication",
|
|
4347
|
+
);
|
|
4348
|
+
basis = name;
|
|
4349
|
+
const old = current.names[name]?.toString();
|
|
4350
|
+
if (!old)
|
|
4351
|
+
throw new HostError(
|
|
4352
|
+
`Template Name "${name}" is missing; nothing was removed.`,
|
|
4353
|
+
);
|
|
4354
|
+
if (old !== expected)
|
|
4355
|
+
throw new HostError(
|
|
4356
|
+
`Edit conflict for ${name}; --expect must match the exact old Name target CID.`,
|
|
4357
|
+
3,
|
|
4358
|
+
"EDIT_CONFLICT",
|
|
4359
|
+
"publication",
|
|
4360
|
+
);
|
|
4361
|
+
const names = { ...current.names };
|
|
4362
|
+
delete names[name];
|
|
4363
|
+
const next = await prepareNamesPublication(
|
|
4364
|
+
context,
|
|
4365
|
+
configUrl,
|
|
4366
|
+
currentHead.root,
|
|
4367
|
+
current,
|
|
4368
|
+
names,
|
|
4369
|
+
[],
|
|
4370
|
+
);
|
|
4371
|
+
try {
|
|
4372
|
+
await head.write(next, currentHead.cid, uniqueId());
|
|
4373
|
+
} catch (cause) {
|
|
4374
|
+
if (cause instanceof ScopeApiError && cause.status === 412) continue;
|
|
4375
|
+
throw new HostError(
|
|
4376
|
+
`Template removal unconfirmed: ${message(cause)}. Inspect HEAD before retry.`,
|
|
4377
|
+
1,
|
|
4378
|
+
"HEAD_UNCONFIRMED",
|
|
4379
|
+
"publication",
|
|
4380
|
+
);
|
|
4381
|
+
}
|
|
4382
|
+
try {
|
|
4383
|
+
await swarmHeader(context, configUrl, true);
|
|
4384
|
+
} catch (cause) {
|
|
4385
|
+
throw new HostError(
|
|
4386
|
+
`Template removal committed, but HEAD/subscription refresh failed: ${message(cause)}. No content was deleted.`,
|
|
4387
|
+
1,
|
|
4388
|
+
"PIN_STALE",
|
|
4389
|
+
"publication",
|
|
4390
|
+
);
|
|
4391
|
+
}
|
|
4392
|
+
return {
|
|
4393
|
+
swarm: configUrl,
|
|
4394
|
+
templatePath: meta.templatePath,
|
|
4395
|
+
metaCid: metaCid(current),
|
|
4396
|
+
selector,
|
|
4397
|
+
name,
|
|
4398
|
+
reference: formatNameReference(configUrl, name),
|
|
4399
|
+
targetCid: old,
|
|
4400
|
+
};
|
|
4401
|
+
}
|
|
4402
|
+
throw new HostError("HEAD is busy; retry the template removal.");
|
|
4403
|
+
}
|
|
4404
|
+
async function registryAssociations(
|
|
4405
|
+
context: SwarmContext,
|
|
4406
|
+
header: SwarmHeader,
|
|
3058
4407
|
): Promise<Association[]> {
|
|
3059
|
-
|
|
3060
|
-
|
|
3061
|
-
|
|
4408
|
+
assertNamesRegistry(header.snapshot);
|
|
4409
|
+
const meta = await swarmMeta(context, header);
|
|
4410
|
+
const prefix = `${meta.registryPath}/`;
|
|
4411
|
+
const result: Association[] = [];
|
|
4412
|
+
for (const [name, cid] of Object.entries(header.snapshot.names)) {
|
|
4413
|
+
if (!name.startsWith(prefix)) continue;
|
|
4414
|
+
const schemaCid = name.slice(prefix.length);
|
|
4415
|
+
if (exactCid(schemaCid, true) !== schemaCid)
|
|
4416
|
+
throw new HostError(
|
|
4417
|
+
`Registry Name "${name}" must end in an exact canonical type CID.`,
|
|
4418
|
+
);
|
|
4419
|
+
result.push(createAssociation(schemaCid, cid.toString(), ["%"]));
|
|
4420
|
+
}
|
|
4421
|
+
return result.sort((left, right) =>
|
|
4422
|
+
left.schemaCid.localeCompare(right.schemaCid),
|
|
3062
4423
|
);
|
|
3063
4424
|
}
|
|
4425
|
+
export async function swarmRegistrations(
|
|
4426
|
+
context: SwarmContext,
|
|
4427
|
+
configUrl: string,
|
|
4428
|
+
): Promise<Association[]> {
|
|
4429
|
+
let header = await swarmHeader(context, configUrl);
|
|
4430
|
+
if (!hasMeta(header.snapshot) && header.source === "cache")
|
|
4431
|
+
header = await swarmHeader(context, configUrl, true);
|
|
4432
|
+
return registryAssociations(context, header);
|
|
4433
|
+
}
|
|
3064
4434
|
export async function swarmAssociation(
|
|
3065
4435
|
context: SwarmContext,
|
|
3066
4436
|
configUrl: string,
|
|
3067
4437
|
schemaCid: string,
|
|
3068
4438
|
mode: "active" | "local-first" = "active",
|
|
3069
4439
|
): Promise<Association | undefined> {
|
|
3070
|
-
|
|
3071
|
-
|
|
3072
|
-
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
4440
|
+
const warm =
|
|
4441
|
+
mode === "local-first" ? await localHeader(context, configUrl) : undefined;
|
|
4442
|
+
if (warm && !warm.header.localSourceReady)
|
|
4443
|
+
await lazyPrivateSource(
|
|
4444
|
+
context,
|
|
4445
|
+
configUrl,
|
|
4446
|
+
warm.account,
|
|
4447
|
+
warm.header.config,
|
|
4448
|
+
);
|
|
4449
|
+
let header = warm?.header ?? (await swarmHeader(context, configUrl));
|
|
4450
|
+
if (!hasMeta(header.snapshot) && header.source === "cache")
|
|
4451
|
+
header = await swarmHeader(context, configUrl, true);
|
|
4452
|
+
let association = (await registryAssociations(context, header)).find(
|
|
4453
|
+
(item) => item.schemaCid === schemaCid,
|
|
3078
4454
|
);
|
|
3079
4455
|
if (!association && header.source === "cache") {
|
|
3080
4456
|
header = await swarmHeader(context, configUrl, true);
|
|
3081
|
-
association =
|
|
3082
|
-
|
|
3083
|
-
(state) =>
|
|
3084
|
-
state.projectedSwarmRegistrations(
|
|
3085
|
-
headerProjection(header),
|
|
3086
|
-
schemaCid,
|
|
3087
|
-
)[0],
|
|
4457
|
+
association = (await registryAssociations(context, header)).find(
|
|
4458
|
+
(item) => item.schemaCid === schemaCid,
|
|
3088
4459
|
);
|
|
3089
4460
|
}
|
|
3090
4461
|
return association;
|
|
3091
4462
|
}
|
|
4463
|
+
async function prepareNamesPublication(
|
|
4464
|
+
context: SwarmContext,
|
|
4465
|
+
configUrl: string,
|
|
4466
|
+
previousRoot: string,
|
|
4467
|
+
current: Snapshot,
|
|
4468
|
+
names: Record<string, CID>,
|
|
4469
|
+
roots: Iterable<string>,
|
|
4470
|
+
retainedRoots?: Iterable<string>,
|
|
4471
|
+
) {
|
|
4472
|
+
const next = await publication(
|
|
4473
|
+
context.store,
|
|
4474
|
+
await writeSnapshot(context.store, { ...current, names }, configUrl),
|
|
4475
|
+
previousRoot,
|
|
4476
|
+
);
|
|
4477
|
+
const closure = await publicationClosure(context.store, next, false);
|
|
4478
|
+
await provideContent(
|
|
4479
|
+
context.store,
|
|
4480
|
+
[...new Set([...closure.provided, ...roots])],
|
|
4481
|
+
context.node!.api,
|
|
4482
|
+
context.fetch,
|
|
4483
|
+
context.node!.auth,
|
|
4484
|
+
[...new Set([next, ...closure.nameTargets, ...(retainedRoots ?? roots)])],
|
|
4485
|
+
);
|
|
4486
|
+
withState(context, (state) => {
|
|
4487
|
+
state.invalidateSwarmHeader(configUrl);
|
|
4488
|
+
});
|
|
4489
|
+
return next;
|
|
4490
|
+
}
|
|
3092
4491
|
async function editSwarmAssociation(
|
|
3093
4492
|
context: SwarmContext,
|
|
3094
4493
|
configUrl: string,
|
|
@@ -3097,53 +4496,49 @@ async function editSwarmAssociation(
|
|
|
3097
4496
|
replace: boolean,
|
|
3098
4497
|
): Promise<void> {
|
|
3099
4498
|
const head = await pointer(context, configUrl);
|
|
4499
|
+
if (association) {
|
|
4500
|
+
const app = await openBundle(CID.parse(association.appCid), {
|
|
4501
|
+
resolveCid: (cid) => context.store.block.get(cid.toString()),
|
|
4502
|
+
});
|
|
4503
|
+
if (app.schemaKey !== appSchemaCid.toString())
|
|
4504
|
+
throw new HostError(
|
|
4505
|
+
"Registry Names must directly reference an App Bundle CID, not an Artifact or another content type.",
|
|
4506
|
+
);
|
|
4507
|
+
}
|
|
3100
4508
|
const roots = association
|
|
3101
4509
|
? await dependencies(context.store, association.appCid)
|
|
3102
4510
|
: new Set<string>();
|
|
3103
4511
|
for (let attempt = 0; attempt < 8; attempt++) {
|
|
3104
4512
|
const currentHead = await head.read();
|
|
3105
|
-
const current = await readSnapshot(
|
|
3106
|
-
|
|
3107
|
-
|
|
4513
|
+
const current = await readSnapshot(
|
|
4514
|
+
context.store,
|
|
4515
|
+
currentHead.root,
|
|
4516
|
+
configUrl,
|
|
3108
4517
|
);
|
|
4518
|
+
assertNamesRegistry(current);
|
|
4519
|
+
const meta = await swarmMeta(context, { snapshot: current });
|
|
4520
|
+
const name = formalName(`${meta.registryPath}/${schemaCid}`);
|
|
4521
|
+
const oldCid = current.names[name]?.toString();
|
|
3109
4522
|
if (association) {
|
|
3110
|
-
if (
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
)
|
|
3115
|
-
return;
|
|
3116
|
-
if (old && !replace)
|
|
3117
|
-
throw new HostError(
|
|
3118
|
-
"Schema has another association; use --replace before --.",
|
|
3119
|
-
);
|
|
3120
|
-
} else if (!old) {
|
|
4523
|
+
if (oldCid === association.appCid) return;
|
|
4524
|
+
if (oldCid && !replace)
|
|
4525
|
+
throw new HostError("Schema has another association; use --replace.");
|
|
4526
|
+
} else if (!oldCid) {
|
|
3121
4527
|
throw new HostError(
|
|
3122
4528
|
"No association exists for this Schema. Run ipab register list --swarm URL_OR_ALIAS.",
|
|
3123
4529
|
);
|
|
3124
4530
|
}
|
|
3125
|
-
const
|
|
3126
|
-
if (association)
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
else delete registrations[schemaCid];
|
|
3132
|
-
const next = await publication(
|
|
3133
|
-
context.store,
|
|
3134
|
-
await writeSnapshot(context.store, { ...current, registrations }),
|
|
4531
|
+
const names = { ...current.names };
|
|
4532
|
+
if (association) names[name] = CID.parse(association.appCid);
|
|
4533
|
+
else delete names[name];
|
|
4534
|
+
const next = await prepareNamesPublication(
|
|
4535
|
+
context,
|
|
4536
|
+
configUrl,
|
|
3135
4537
|
currentHead.root,
|
|
4538
|
+
current,
|
|
4539
|
+
names,
|
|
4540
|
+
roots,
|
|
3136
4541
|
);
|
|
3137
|
-
await provideContent(
|
|
3138
|
-
context.store,
|
|
3139
|
-
[next, ...roots],
|
|
3140
|
-
context.node!.api,
|
|
3141
|
-
context.fetch,
|
|
3142
|
-
context.node!.auth,
|
|
3143
|
-
);
|
|
3144
|
-
withState(context, (state) => {
|
|
3145
|
-
state.invalidateSwarmHeader(configUrl);
|
|
3146
|
-
});
|
|
3147
4542
|
try {
|
|
3148
4543
|
await head.write(next, currentHead.cid, uniqueId());
|
|
3149
4544
|
return;
|
|
@@ -3168,6 +4563,10 @@ export async function setSwarmAssociation(
|
|
|
3168
4563
|
value: Association,
|
|
3169
4564
|
replace = false,
|
|
3170
4565
|
): Promise<Association> {
|
|
4566
|
+
if (value.argv.length !== 1 || value.argv[0] !== "%")
|
|
4567
|
+
throw new HostError(
|
|
4568
|
+
'Custom per-association argv is unsupported. Apps opened through the registry receive only the content CID (whole "%" argument); adapt the App or use ipab exec APP_CID ARGS.',
|
|
4569
|
+
);
|
|
3171
4570
|
const association = createAssociation(
|
|
3172
4571
|
value.schemaCid,
|
|
3173
4572
|
value.appCid,
|
|
@@ -3222,6 +4621,10 @@ async function dependencies(
|
|
|
3222
4621
|
throw new HostError("Invalid App source association.");
|
|
3223
4622
|
await dependencies(store, source.cid, found);
|
|
3224
4623
|
}
|
|
4624
|
+
} else if (isSwarmStateSchema(bundle.schemaKey)) {
|
|
4625
|
+
const snapshot = await readState(store, CID.parse(root));
|
|
4626
|
+
for (const target of Object.values(snapshot.names))
|
|
4627
|
+
await dependencies(store, target.toString(), found);
|
|
3225
4628
|
}
|
|
3226
4629
|
return found;
|
|
3227
4630
|
}
|
|
@@ -3229,8 +4632,16 @@ export async function frozenPublicationClosure(
|
|
|
3229
4632
|
store: IpfsStore,
|
|
3230
4633
|
root: string,
|
|
3231
4634
|
): Promise<string[]> {
|
|
4635
|
+
return (await publicationClosure(store, root)).provided;
|
|
4636
|
+
}
|
|
4637
|
+
async function publicationClosure(
|
|
4638
|
+
store: IpfsStore,
|
|
4639
|
+
root: string,
|
|
4640
|
+
includeDependencies = true,
|
|
4641
|
+
) {
|
|
3232
4642
|
const publications = new Set<string>(),
|
|
3233
|
-
content = new Set<string>()
|
|
4643
|
+
content = new Set<string>(),
|
|
4644
|
+
nameTargets = new Set<string>();
|
|
3234
4645
|
let current: string | undefined = root;
|
|
3235
4646
|
while (current !== undefined) {
|
|
3236
4647
|
if (publications.has(current) || publications.size >= 256)
|
|
@@ -3240,18 +4651,26 @@ export async function frozenPublicationClosure(
|
|
|
3240
4651
|
publications.add(current);
|
|
3241
4652
|
const publication = await readPublication(store, current);
|
|
3242
4653
|
const snapshot = await readState(store, publication.state);
|
|
3243
|
-
for (const target of Object.values(snapshot.names))
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
4654
|
+
for (const target of Object.values(snapshot.names)) {
|
|
4655
|
+
nameTargets.add(target.toString());
|
|
4656
|
+
if (includeDependencies)
|
|
4657
|
+
await dependencies(store, target.toString(), content);
|
|
4658
|
+
else content.add(target.toString());
|
|
4659
|
+
}
|
|
4660
|
+
if (includeDependencies)
|
|
4661
|
+
for (const association of Object.values(snapshot.registrations ?? {}))
|
|
4662
|
+
await dependencies(store, association.appCid.toString(), content);
|
|
3247
4663
|
current = publication.previous?.toString();
|
|
3248
4664
|
}
|
|
3249
|
-
return
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
4665
|
+
return {
|
|
4666
|
+
nameTargets,
|
|
4667
|
+
provided: [
|
|
4668
|
+
root,
|
|
4669
|
+
...[...new Set([...publications, ...content])]
|
|
4670
|
+
.filter((cid) => cid !== root)
|
|
4671
|
+
.sort(),
|
|
4672
|
+
],
|
|
4673
|
+
};
|
|
3255
4674
|
}
|
|
3256
4675
|
export async function publish(
|
|
3257
4676
|
context: SwarmContext,
|
|
@@ -3262,6 +4681,19 @@ export async function publish(
|
|
|
3262
4681
|
expect?: string;
|
|
3263
4682
|
snapshot?: boolean;
|
|
3264
4683
|
},
|
|
4684
|
+
): Promise<string> {
|
|
4685
|
+
return publishContent(context, content, options);
|
|
4686
|
+
}
|
|
4687
|
+
async function publishContent(
|
|
4688
|
+
context: SwarmContext,
|
|
4689
|
+
content: string,
|
|
4690
|
+
options: {
|
|
4691
|
+
swarm?: string;
|
|
4692
|
+
name: string;
|
|
4693
|
+
expect?: string;
|
|
4694
|
+
snapshot?: boolean;
|
|
4695
|
+
},
|
|
4696
|
+
nameForSnapshot?: (current: Snapshot) => Promise<string>,
|
|
3265
4697
|
): Promise<string> {
|
|
3266
4698
|
if (!options.swarm)
|
|
3267
4699
|
throw new HostError(
|
|
@@ -3273,58 +4705,70 @@ export async function publish(
|
|
|
3273
4705
|
await withStateAsync(context, (state) =>
|
|
3274
4706
|
pinCid(state, content, context.store, currentRoots),
|
|
3275
4707
|
);
|
|
3276
|
-
|
|
4708
|
+
let name = formalName(options.name);
|
|
4709
|
+
let resolvedName: string | undefined;
|
|
3277
4710
|
if (options.snapshot && options.expect)
|
|
3278
4711
|
throw new HostError("Snapshots cannot replace an occupied Name.");
|
|
3279
4712
|
const expected = options.expect
|
|
3280
4713
|
? CID.parse(options.expect).toV1().toString()
|
|
3281
4714
|
: undefined;
|
|
3282
|
-
|
|
3283
|
-
|
|
3284
|
-
|
|
3285
|
-
|
|
3286
|
-
|
|
3287
|
-
|
|
3288
|
-
|
|
3289
|
-
|
|
3290
|
-
|
|
4715
|
+
const contentBundle = await openBundle(CID.parse(content), {
|
|
4716
|
+
resolveCid: (cid) => context.store.block.get(cid.toString()),
|
|
4717
|
+
});
|
|
4718
|
+
const direct =
|
|
4719
|
+
contentBundle.schemaKey === appSchemaCid.toString() ||
|
|
4720
|
+
contentBundle.schemaKey === swarmMetaSchema.cid.toString() ||
|
|
4721
|
+
contentBundle.schemaKey === legacySwarmMetaSchema.cid.toString();
|
|
4722
|
+
const candidate =
|
|
4723
|
+
options.snapshot || direct
|
|
4724
|
+
? content
|
|
4725
|
+
: await jsonBundle(context.store, artifactType, {
|
|
4726
|
+
value: content,
|
|
4727
|
+
...(expected ? { previous: expected } : {}),
|
|
4728
|
+
timestamp: new Date().toISOString(),
|
|
4729
|
+
});
|
|
3291
4730
|
roots.add(candidate);
|
|
3292
4731
|
if (expected)
|
|
3293
4732
|
for (const root of await dependencies(context.store, expected))
|
|
3294
4733
|
roots.add(root);
|
|
3295
4734
|
for (let attempt = 0; attempt < 8; attempt++) {
|
|
3296
4735
|
const currentHead = await head.read(),
|
|
3297
|
-
current = await readSnapshot(
|
|
4736
|
+
current = await readSnapshot(
|
|
4737
|
+
context.store,
|
|
4738
|
+
currentHead.root,
|
|
4739
|
+
options.swarm,
|
|
4740
|
+
);
|
|
4741
|
+
if (nameForSnapshot) {
|
|
4742
|
+
name = await nameForSnapshot(current);
|
|
4743
|
+
if (resolvedName !== undefined && resolvedName !== name)
|
|
4744
|
+
throw new HostError(
|
|
4745
|
+
"Template Meta path changed during publication; inspect and retry. Content remains pinned.",
|
|
4746
|
+
3,
|
|
4747
|
+
"EDIT_CONFLICT",
|
|
4748
|
+
"publication",
|
|
4749
|
+
);
|
|
4750
|
+
resolvedName = name;
|
|
4751
|
+
}
|
|
3298
4752
|
if (
|
|
3299
4753
|
(Object.hasOwn(current.names, name)
|
|
3300
4754
|
? current.names[name]?.toString()
|
|
3301
4755
|
: undefined) !== expected
|
|
3302
4756
|
)
|
|
3303
4757
|
throw new HostError(
|
|
3304
|
-
`Edit conflict for ${name}; content ${content} remains pinned. Occupied Names require --expect
|
|
4758
|
+
`Edit conflict for ${name}; content ${content} remains pinned. Occupied Names require --expect EXACT_OLD_CID (the old Artifact CID when applicable); inspect the current Name before retrying.`,
|
|
3305
4759
|
3,
|
|
3306
4760
|
"EDIT_CONFLICT",
|
|
3307
4761
|
"publication",
|
|
3308
4762
|
);
|
|
3309
|
-
const next = await
|
|
3310
|
-
context
|
|
3311
|
-
|
|
3312
|
-
...current,
|
|
3313
|
-
names: { ...current.names, [name]: CID.parse(candidate) },
|
|
3314
|
-
}),
|
|
4763
|
+
const next = await prepareNamesPublication(
|
|
4764
|
+
context,
|
|
4765
|
+
options.swarm,
|
|
3315
4766
|
currentHead.root,
|
|
4767
|
+
current,
|
|
4768
|
+
{ ...current.names, [name]: CID.parse(candidate) },
|
|
4769
|
+
roots,
|
|
4770
|
+
expected ? await dependencies(context.store, expected) : [],
|
|
3316
4771
|
);
|
|
3317
|
-
await provideContent(
|
|
3318
|
-
context.store,
|
|
3319
|
-
[next, ...roots],
|
|
3320
|
-
context.node!.api,
|
|
3321
|
-
context.fetch,
|
|
3322
|
-
context.node!.auth,
|
|
3323
|
-
[next, ...(expected ? await dependencies(context.store, expected) : [])],
|
|
3324
|
-
);
|
|
3325
|
-
withState(context, (state) => {
|
|
3326
|
-
state.invalidateSwarmHeader(options.swarm!);
|
|
3327
|
-
});
|
|
3328
4772
|
try {
|
|
3329
4773
|
await head.write(next, currentHead.cid, uniqueId());
|
|
3330
4774
|
} catch (cause) {
|