@ipab/cli 0.1.0-rc.3 → 0.1.0-rc.5
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 +21 -10
- package/dist/cli.d.ts +3 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +188 -46
- package/dist/cli.js.map +1 -1
- package/dist/config-secret.d.ts +1 -1
- package/dist/config-secret.d.ts.map +1 -1
- package/dist/config-secret.js +2 -73
- package/dist/config-secret.js.map +1 -1
- package/dist/config.d.ts +2 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +23 -3
- package/dist/config.js.map +1 -1
- package/dist/context-types.d.ts +12 -0
- package/dist/context-types.d.ts.map +1 -0
- package/dist/context-types.js +2 -0
- package/dist/context-types.js.map +1 -0
- package/dist/context.d.ts +6 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +25 -0
- package/dist/context.js.map +1 -1
- package/dist/identity.d.ts +5 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +5 -0
- package/dist/identity.js.map +1 -1
- package/dist/ipc.d.ts +3 -1
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +34 -1
- package/dist/ipc.js.map +1 -1
- package/dist/kubo-install.d.ts +9 -0
- package/dist/kubo-install.d.ts.map +1 -0
- package/dist/kubo-install.js +361 -0
- package/dist/kubo-install.js.map +1 -0
- package/dist/kubo-peering.d.ts +11 -0
- package/dist/kubo-peering.d.ts.map +1 -0
- package/dist/kubo-peering.js +93 -0
- package/dist/kubo-peering.js.map +1 -0
- package/dist/kubo-peers.d.ts +12 -0
- package/dist/kubo-peers.d.ts.map +1 -0
- package/dist/kubo-peers.js +118 -0
- package/dist/kubo-peers.js.map +1 -0
- package/dist/kubo-release.d.ts +3 -0
- package/dist/kubo-release.d.ts.map +1 -0
- package/dist/kubo-release.js +12 -0
- package/dist/kubo-release.js.map +1 -0
- package/dist/kubo.d.ts +14 -5
- package/dist/kubo.d.ts.map +1 -1
- package/dist/kubo.js +186 -206
- package/dist/kubo.js.map +1 -1
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +6 -2
- package/dist/runtime.js.map +1 -1
- package/dist/setup.d.ts +2 -1
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +23 -18
- package/dist/setup.js.map +1 -1
- package/dist/state-storage-windows.d.ts +2 -0
- package/dist/state-storage-windows.d.ts.map +1 -0
- package/dist/state-storage-windows.js +114 -0
- package/dist/state-storage-windows.js.map +1 -0
- package/dist/state-storage.d.ts +3 -0
- package/dist/state-storage.d.ts.map +1 -0
- package/dist/state-storage.js +299 -0
- package/dist/state-storage.js.map +1 -0
- package/dist/state.d.ts +5 -3
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +64 -30
- package/dist/state.js.map +1 -1
- package/dist/swarm-files.d.ts +3 -2
- package/dist/swarm-files.d.ts.map +1 -1
- package/dist/swarm-files.js +25 -9
- package/dist/swarm-files.js.map +1 -1
- package/dist/swarm.d.ts +10 -5
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +225 -79
- package/dist/swarm.js.map +1 -1
- package/package.json +4 -4
- package/src/cli.ts +224 -60
- package/src/config-secret.ts +2 -84
- package/src/config.ts +25 -3
- package/src/context-types.ts +11 -0
- package/src/context.ts +40 -0
- package/src/identity.ts +13 -0
- package/src/ipc.ts +50 -1
- package/src/kubo-install.ts +452 -0
- package/src/kubo-peering.ts +126 -0
- package/src/kubo-peers.ts +145 -0
- package/src/kubo-release.ts +17 -0
- package/src/kubo.ts +254 -275
- package/src/runtime.ts +12 -1
- package/src/setup.ts +30 -17
- package/src/state-storage-windows.ts +134 -0
- package/src/state-storage.ts +329 -0
- package/src/state.ts +76 -35
- package/src/swarm-files.ts +26 -6
- package/src/swarm.ts +316 -97
package/src/swarm.ts
CHANGED
|
@@ -11,10 +11,11 @@ import {
|
|
|
11
11
|
memoryTree,
|
|
12
12
|
streamChunks,
|
|
13
13
|
} from "@ipab/bundle-builder";
|
|
14
|
-
import type
|
|
14
|
+
import { ContentError, type IpfsStore } from "@ipab/ipfs-store";
|
|
15
15
|
import {
|
|
16
16
|
ScopeClient,
|
|
17
17
|
ScopeApiError,
|
|
18
|
+
RESOURCE_ID_PATTERN,
|
|
18
19
|
issueScopeCertificate,
|
|
19
20
|
uniqueId,
|
|
20
21
|
verifyScopeCertificate,
|
|
@@ -35,6 +36,7 @@ import {
|
|
|
35
36
|
} from "@ipab/bundle/pointer/crypto";
|
|
36
37
|
import { HostError, object, message } from "./errors.js";
|
|
37
38
|
import { accountIdentity, type Identity } from "./identity.js";
|
|
39
|
+
import { swarmUrl as canonicalSwarmUrl } from "./config.js";
|
|
38
40
|
import { bundleJson, provideContent } from "./content-provider.js";
|
|
39
41
|
import { bytes, relativePath } from "./space.js";
|
|
40
42
|
import { appSchemaCid } from "./app-definition.js";
|
|
@@ -56,6 +58,7 @@ import {
|
|
|
56
58
|
parseEnvelope,
|
|
57
59
|
} from "./swarm-envelope.js";
|
|
58
60
|
import {
|
|
61
|
+
checkCachedNode,
|
|
59
62
|
kuboSource,
|
|
60
63
|
peerId,
|
|
61
64
|
peerProof,
|
|
@@ -63,7 +66,9 @@ import {
|
|
|
63
66
|
startNode,
|
|
64
67
|
validateAddress,
|
|
65
68
|
type KuboNode,
|
|
69
|
+
type ListenMode,
|
|
66
70
|
} from "./kubo.js";
|
|
71
|
+
import { memberPeers } from "./kubo-peering.js";
|
|
67
72
|
|
|
68
73
|
export interface Member {
|
|
69
74
|
publicKey: string;
|
|
@@ -120,7 +125,10 @@ export interface SwarmContext {
|
|
|
120
125
|
forceRefresh?: boolean;
|
|
121
126
|
diagnostic?: (text: string) => void;
|
|
122
127
|
node?: KuboNode;
|
|
128
|
+
sourcePeers?: readonly string[];
|
|
129
|
+
finishRead?: () => Promise<void>;
|
|
123
130
|
identity?: Identity;
|
|
131
|
+
listen?: ListenMode;
|
|
124
132
|
}
|
|
125
133
|
async function contextIdentity(
|
|
126
134
|
context: SwarmContext,
|
|
@@ -169,26 +177,7 @@ function withState<T>(
|
|
|
169
177
|
state.close();
|
|
170
178
|
}
|
|
171
179
|
}
|
|
172
|
-
export
|
|
173
|
-
if (!/^https?:\/\//.test(value))
|
|
174
|
-
throw new HostError(
|
|
175
|
-
"Swarm requires a configuration URL; old Swarm CID protocol is unsupported.",
|
|
176
|
-
);
|
|
177
|
-
const url = new URL(value);
|
|
178
|
-
canonicalService(url.origin);
|
|
179
|
-
if (
|
|
180
|
-
url.search ||
|
|
181
|
-
url.hash ||
|
|
182
|
-
url.username ||
|
|
183
|
-
url.password ||
|
|
184
|
-
url.toString() !== value ||
|
|
185
|
-
!/^\/v1\/[A-Za-z0-9_-]{43}\/[A-Za-z0-9_-]{43}$/.test(url.pathname)
|
|
186
|
-
)
|
|
187
|
-
throw new HostError(
|
|
188
|
-
"Swarm must be an exact Scope configuration URL; old Swarm CID protocol is unsupported.",
|
|
189
|
-
);
|
|
190
|
-
return value;
|
|
191
|
-
}
|
|
180
|
+
export { canonicalSwarmUrl };
|
|
192
181
|
function coordinates(configUrl: string): {
|
|
193
182
|
service: string;
|
|
194
183
|
scopeId: string;
|
|
@@ -268,7 +257,7 @@ function parseOrganization(value: unknown, configUrl: string): Organization {
|
|
|
268
257
|
typeof data.description !== "string" ||
|
|
269
258
|
data.description.length > 2048 ||
|
|
270
259
|
typeof data.headId !== "string" ||
|
|
271
|
-
!
|
|
260
|
+
!RESOURCE_ID_PATTERN.test(data.headId) ||
|
|
272
261
|
typeof data.epoch !== "number" ||
|
|
273
262
|
!Number.isSafeInteger(data.epoch) ||
|
|
274
263
|
data.epoch < 1 ||
|
|
@@ -293,7 +282,7 @@ function parseOrganization(value: unknown, configUrl: string): Organization {
|
|
|
293
282
|
member.encryptionKey.length > 256 ||
|
|
294
283
|
typeof member.peerId !== "string" ||
|
|
295
284
|
typeof member.profileId !== "string" ||
|
|
296
|
-
!
|
|
285
|
+
!RESOURCE_ID_PATTERN.test(member.profileId) ||
|
|
297
286
|
typeof member.requestId !== "string" ||
|
|
298
287
|
!POINTER_ID_PATTERN.test(member.requestId) ||
|
|
299
288
|
identities.has(member.publicKey) ||
|
|
@@ -363,12 +352,14 @@ function parseProfile(value: unknown, expectedPeer: string): Profile {
|
|
|
363
352
|
async function fetchConfig(
|
|
364
353
|
context: SwarmContext,
|
|
365
354
|
configUrl: string,
|
|
355
|
+
signal?: AbortSignal,
|
|
366
356
|
): Promise<{ config: Organization; variable: Variable }> {
|
|
367
357
|
const c = coordinates(configUrl),
|
|
368
358
|
api = client(context, c.service);
|
|
359
|
+
const options = signal ? { signal } : {};
|
|
369
360
|
const [variable, scope] = await Promise.all([
|
|
370
|
-
api.read(c.scopeId, c.configId),
|
|
371
|
-
api.send<{ owner: string }>("GET", c.scopeId),
|
|
361
|
+
api.read(c.scopeId, c.configId, options),
|
|
362
|
+
api.send<{ owner: string }>("GET", c.scopeId, "", undefined, options),
|
|
372
363
|
]);
|
|
373
364
|
const config = parseOrganization(variable.value, configUrl);
|
|
374
365
|
if (scope.owner !== config.owner)
|
|
@@ -433,10 +424,11 @@ async function validateMembershipSecret(
|
|
|
433
424
|
);
|
|
434
425
|
}
|
|
435
426
|
}
|
|
436
|
-
async function
|
|
427
|
+
async function privateSource(
|
|
437
428
|
context: SwarmContext,
|
|
438
429
|
record: LocalMembership,
|
|
439
|
-
|
|
430
|
+
signal?: AbortSignal,
|
|
431
|
+
) {
|
|
440
432
|
const config = record.config!,
|
|
441
433
|
secret = record.secret!;
|
|
442
434
|
const identity = await contextIdentity(context);
|
|
@@ -466,35 +458,50 @@ async function activate(
|
|
|
466
458
|
identity.publicKey,
|
|
467
459
|
);
|
|
468
460
|
}
|
|
469
|
-
const addresses = [
|
|
461
|
+
const addresses = new Map<string, readonly string[]>();
|
|
470
462
|
const api = client(context, c.service);
|
|
471
463
|
let selfProfile: { profile: Profile; cid: string } | undefined;
|
|
472
|
-
|
|
473
|
-
|
|
464
|
+
const profiles = await Promise.allSettled(
|
|
465
|
+
config.members.map((member) =>
|
|
466
|
+
api.read(c.scopeId, member.profileId, signal ? { signal } : {}),
|
|
467
|
+
),
|
|
468
|
+
);
|
|
469
|
+
for (const [index, member] of config.members.entries()) {
|
|
470
|
+
const result = profiles[index]!;
|
|
471
|
+
if (result.status === "rejected") throw result.reason;
|
|
472
|
+
const profile = result.value;
|
|
474
473
|
const value = parseProfile(profile.value, member.peerId);
|
|
475
474
|
if (member.publicKey === identity.publicKey)
|
|
476
475
|
selfProfile = { profile: value, cid: profile.cid };
|
|
477
|
-
addresses.
|
|
476
|
+
addresses.set(member.peerId, value.addresses);
|
|
478
477
|
}
|
|
478
|
+
const desired = memberPeers(self.peerId, addresses, config.bootstrap);
|
|
479
|
+
signal?.throwIfAborted();
|
|
479
480
|
const node = await startNode(
|
|
480
481
|
context.stateDir,
|
|
481
482
|
nodeScope(c.service, config.scopeId),
|
|
482
483
|
secret.swarmKey,
|
|
483
484
|
config.epoch,
|
|
484
|
-
|
|
485
|
-
|
|
485
|
+
context.listen,
|
|
486
|
+
{ self: self.peerId, peers: desired, ...(signal ? { signal } : {}) },
|
|
486
487
|
);
|
|
487
488
|
if (node.peerId !== self.peerId)
|
|
488
489
|
throw new HostError(
|
|
489
490
|
"Approved PeerID differs from this local Request's Kubo identity.",
|
|
490
491
|
);
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
492
|
+
const peers = desired.flatMap(({ ID, Addrs }) =>
|
|
493
|
+
Addrs.map((addr) => `${addr}/p2p/${ID}`),
|
|
494
|
+
);
|
|
495
|
+
signal?.throwIfAborted();
|
|
496
|
+
if (!node.addresses.length && !selfProfile?.profile.addresses.length)
|
|
497
|
+
context.diagnostic?.(
|
|
498
|
+
"No private LAN/VPN IPv4 address to advertise. Connect a routed interface or set your own address with swarm profile --address.",
|
|
499
|
+
);
|
|
500
|
+
if (
|
|
501
|
+
node.addresses.length &&
|
|
502
|
+
selfProfile &&
|
|
503
|
+
selfProfile.profile.addresses.length === 0
|
|
504
|
+
) {
|
|
498
505
|
const writer = client(
|
|
499
506
|
context,
|
|
500
507
|
c.service,
|
|
@@ -512,10 +519,35 @@ async function activate(
|
|
|
512
519
|
profile,
|
|
513
520
|
selfProfile.cid,
|
|
514
521
|
uniqueId(),
|
|
522
|
+
signal ? { signal } : {},
|
|
515
523
|
);
|
|
516
524
|
}
|
|
525
|
+
return {
|
|
526
|
+
node,
|
|
527
|
+
peers,
|
|
528
|
+
source: kuboSource(
|
|
529
|
+
node,
|
|
530
|
+
peers,
|
|
531
|
+
context.diagnostic,
|
|
532
|
+
context.env.IPAB_SWARM_DIAL_DEBUG === "1",
|
|
533
|
+
),
|
|
534
|
+
};
|
|
517
535
|
}
|
|
518
|
-
async function
|
|
536
|
+
async function activate(context: SwarmContext, record: LocalMembership) {
|
|
537
|
+
const { node, peers, source } = await privateSource(context, record);
|
|
538
|
+
if (
|
|
539
|
+
context.node?.api !== node.api ||
|
|
540
|
+
context.sourcePeers?.length !== peers.length ||
|
|
541
|
+
peers.some((peer, i) => context.sourcePeers?.[i] !== peer)
|
|
542
|
+
) {
|
|
543
|
+
const store = context.store.withLocalSource(source);
|
|
544
|
+
await context.store.close();
|
|
545
|
+
context.store = store;
|
|
546
|
+
context.sourcePeers = peers;
|
|
547
|
+
}
|
|
548
|
+
context.node = node;
|
|
549
|
+
}
|
|
550
|
+
async function localForRead(
|
|
519
551
|
context: SwarmContext,
|
|
520
552
|
configUrl: string,
|
|
521
553
|
): Promise<LocalMembership> {
|
|
@@ -530,7 +562,15 @@ async function refresh(
|
|
|
530
562
|
"Organization switch incomplete; only Owner can continue with swarm resume.",
|
|
531
563
|
);
|
|
532
564
|
}
|
|
533
|
-
|
|
565
|
+
return record;
|
|
566
|
+
}
|
|
567
|
+
async function approvedMembership(
|
|
568
|
+
context: SwarmContext,
|
|
569
|
+
configUrl: string,
|
|
570
|
+
signal?: AbortSignal,
|
|
571
|
+
) {
|
|
572
|
+
const record = await localForRead(context, configUrl);
|
|
573
|
+
const { config } = await fetchConfig(context, configUrl, signal);
|
|
534
574
|
if (config.owner !== record.owner)
|
|
535
575
|
throw new HostError("Pinned Owner changed; refusing configuration.");
|
|
536
576
|
if (record.config && config.epoch < record.config.epoch)
|
|
@@ -564,6 +604,10 @@ async function refresh(
|
|
|
564
604
|
);
|
|
565
605
|
record.config = config;
|
|
566
606
|
await validateMembershipSecret(record, config, member);
|
|
607
|
+
return record;
|
|
608
|
+
}
|
|
609
|
+
async function refresh(context: SwarmContext, configUrl: string) {
|
|
610
|
+
const record = await approvedMembership(context, configUrl);
|
|
567
611
|
await saveLocal(context, record);
|
|
568
612
|
await activate(context, record);
|
|
569
613
|
return record;
|
|
@@ -572,17 +616,7 @@ async function readyMembership(
|
|
|
572
616
|
context: SwarmContext,
|
|
573
617
|
configUrl: string,
|
|
574
618
|
): Promise<LocalMembership> {
|
|
575
|
-
const record = await
|
|
576
|
-
if (record.account === record.owner) {
|
|
577
|
-
const transition = await readPrivate<Removal>(
|
|
578
|
-
await directory(context),
|
|
579
|
-
`transition-${localName(configUrl)}`,
|
|
580
|
-
);
|
|
581
|
-
if (transition && !transition.completed)
|
|
582
|
-
throw new HostError(
|
|
583
|
-
"Organization switch incomplete; run swarm resume before normal Owner operations.",
|
|
584
|
-
);
|
|
585
|
-
}
|
|
619
|
+
const record = await localForRead(context, configUrl);
|
|
586
620
|
if (!record.config || !record.secret) return refresh(context, configUrl);
|
|
587
621
|
if (
|
|
588
622
|
!context.node ||
|
|
@@ -620,7 +654,17 @@ export async function pointer(
|
|
|
620
654
|
context: SwarmContext,
|
|
621
655
|
configUrl: string,
|
|
622
656
|
): Promise<BoundHead> {
|
|
623
|
-
|
|
657
|
+
return boundPointer(
|
|
658
|
+
context,
|
|
659
|
+
configUrl,
|
|
660
|
+
await readyMembership(context, configUrl),
|
|
661
|
+
);
|
|
662
|
+
}
|
|
663
|
+
async function boundPointer(
|
|
664
|
+
context: SwarmContext,
|
|
665
|
+
configUrl: string,
|
|
666
|
+
record: LocalMembership,
|
|
667
|
+
): Promise<BoundHead> {
|
|
624
668
|
const identity = await contextIdentity(context),
|
|
625
669
|
c = coordinates(configUrl);
|
|
626
670
|
const operation = async (
|
|
@@ -814,6 +858,38 @@ export interface SwarmHeader {
|
|
|
814
858
|
source: "cache" | "network";
|
|
815
859
|
verifiedAt: string;
|
|
816
860
|
}
|
|
861
|
+
export async function sharedSwarmStatus(
|
|
862
|
+
context: SwarmContext,
|
|
863
|
+
configUrl: string,
|
|
864
|
+
): Promise<SwarmHeader> {
|
|
865
|
+
const { config } = await fetchConfig(context, configUrl);
|
|
866
|
+
const c = coordinates(configUrl);
|
|
867
|
+
const head = headValue(
|
|
868
|
+
await client(context, c.service).read(c.scopeId, config.headId),
|
|
869
|
+
);
|
|
870
|
+
let snapshot: Snapshot;
|
|
871
|
+
try {
|
|
872
|
+
snapshot = await readSnapshot(context.store, head.root);
|
|
873
|
+
} catch (cause) {
|
|
874
|
+
if (!(cause instanceof ContentError) || cause.code !== "missing_block")
|
|
875
|
+
throw cause;
|
|
876
|
+
const registered = withState(context, (state) =>
|
|
877
|
+
state.swarms().some((entry) => entry.cid === configUrl),
|
|
878
|
+
);
|
|
879
|
+
if (!registered) throw cause;
|
|
880
|
+
context.diagnostic?.(
|
|
881
|
+
"Shared State is missing locally; connecting the registered Swarm content source.",
|
|
882
|
+
);
|
|
883
|
+
return swarmHeader(context, configUrl, true);
|
|
884
|
+
}
|
|
885
|
+
return {
|
|
886
|
+
head,
|
|
887
|
+
snapshot,
|
|
888
|
+
config,
|
|
889
|
+
source: "network",
|
|
890
|
+
verifiedAt: new Date().toISOString(),
|
|
891
|
+
};
|
|
892
|
+
}
|
|
817
893
|
export async function swarmHeader(
|
|
818
894
|
context: SwarmContext,
|
|
819
895
|
configUrl: string,
|
|
@@ -823,39 +899,15 @@ export async function swarmHeader(
|
|
|
823
899
|
let record = force
|
|
824
900
|
? await refresh(context, configUrl)
|
|
825
901
|
: await readyMembership(context, configUrl);
|
|
826
|
-
const
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
: (withState(context, (state) =>
|
|
830
|
-
state.swarmHeader(record.account, configUrl),
|
|
831
|
-
) as
|
|
832
|
-
| {
|
|
833
|
-
head: Variable;
|
|
834
|
-
snapshot: string;
|
|
835
|
-
verifiedAt: number;
|
|
836
|
-
headId: string;
|
|
837
|
-
}
|
|
838
|
-
| undefined);
|
|
839
|
-
if (
|
|
840
|
-
cached &&
|
|
841
|
-
cached.headId === record.config!.headId &&
|
|
842
|
-
Date.now() >= cached.verifiedAt &&
|
|
843
|
-
Date.now() - cached.verifiedAt < ttl * 1000
|
|
844
|
-
)
|
|
845
|
-
return {
|
|
846
|
-
head: headValue(cached.head),
|
|
847
|
-
snapshot: decodeSnapshot(Buffer.from(cached.snapshot, "base64")),
|
|
848
|
-
config: record.config!,
|
|
849
|
-
source: "cache",
|
|
850
|
-
verifiedAt: new Date(cached.verifiedAt).toISOString(),
|
|
851
|
-
};
|
|
852
|
-
let head = await (await pointer(context, configUrl)).read();
|
|
902
|
+
const cached = !force && cachedHeader(context, record);
|
|
903
|
+
if (cached) return cached;
|
|
904
|
+
let head = await (await boundPointer(context, configUrl, record)).read();
|
|
853
905
|
let snapshot: Snapshot;
|
|
854
906
|
try {
|
|
855
907
|
snapshot = await readSnapshot(context.store, head.root);
|
|
856
908
|
} catch {
|
|
857
909
|
record = await refresh(context, configUrl);
|
|
858
|
-
head = await (await
|
|
910
|
+
head = await (await boundPointer(context, configUrl, record)).read();
|
|
859
911
|
snapshot = await readSnapshot(context.store, head.root);
|
|
860
912
|
}
|
|
861
913
|
const current = await local(context, configUrl),
|
|
@@ -866,6 +918,7 @@ export async function swarmHeader(
|
|
|
866
918
|
snapshot: Buffer.from(dagCbor.encode(snapshot)).toString("base64"),
|
|
867
919
|
verifiedAt,
|
|
868
920
|
headId: current.config!.headId,
|
|
921
|
+
epoch: current.config!.epoch,
|
|
869
922
|
});
|
|
870
923
|
});
|
|
871
924
|
return {
|
|
@@ -876,6 +929,126 @@ export async function swarmHeader(
|
|
|
876
929
|
verifiedAt: new Date(verifiedAt).toISOString(),
|
|
877
930
|
};
|
|
878
931
|
}
|
|
932
|
+
function cachedHeader(
|
|
933
|
+
context: SwarmContext,
|
|
934
|
+
record: LocalMembership,
|
|
935
|
+
): SwarmHeader | undefined {
|
|
936
|
+
const config = record.config;
|
|
937
|
+
if (!config || !record.secret) return;
|
|
938
|
+
const cached = withState(context, (state) =>
|
|
939
|
+
state.swarmHeader(record.account, record.configUrl),
|
|
940
|
+
) as
|
|
941
|
+
| {
|
|
942
|
+
head: Variable;
|
|
943
|
+
snapshot: string;
|
|
944
|
+
verifiedAt: number;
|
|
945
|
+
headId: string;
|
|
946
|
+
epoch?: number;
|
|
947
|
+
}
|
|
948
|
+
| undefined;
|
|
949
|
+
const age = cached ? Date.now() - cached.verifiedAt : NaN;
|
|
950
|
+
if (
|
|
951
|
+
!cached ||
|
|
952
|
+
cached.headId !== config.headId ||
|
|
953
|
+
(cached.epoch !== undefined && cached.epoch !== config.epoch) ||
|
|
954
|
+
!(age >= 0 && age < (context.cacheTtl ?? 300) * 1000)
|
|
955
|
+
)
|
|
956
|
+
return;
|
|
957
|
+
return {
|
|
958
|
+
head: headValue(cached.head),
|
|
959
|
+
snapshot: decodeSnapshot(Buffer.from(cached.snapshot, "base64")),
|
|
960
|
+
config,
|
|
961
|
+
source: "cache",
|
|
962
|
+
verifiedAt: new Date(cached.verifiedAt).toISOString(),
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
async function localHeader(context: SwarmContext, url: string) {
|
|
966
|
+
if (context.forceRefresh) return;
|
|
967
|
+
canonicalSwarmUrl(url);
|
|
968
|
+
const record = await localForRead(context, url);
|
|
969
|
+
const header = cachedHeader(context, record);
|
|
970
|
+
if (!header) return;
|
|
971
|
+
const config = parseOrganization(header.config, url);
|
|
972
|
+
const member = config.members.find(
|
|
973
|
+
(item) => item.publicKey === record.account,
|
|
974
|
+
);
|
|
975
|
+
if (!member || config.owner !== record.owner)
|
|
976
|
+
throw new HostError(
|
|
977
|
+
"Cached membership no longer matches the account or Owner.",
|
|
978
|
+
);
|
|
979
|
+
if (
|
|
980
|
+
record.account !== config.owner &&
|
|
981
|
+
(!record.request ||
|
|
982
|
+
record.request.id !== member.requestId ||
|
|
983
|
+
record.request.peerId !== member.peerId ||
|
|
984
|
+
!member.approval)
|
|
985
|
+
)
|
|
986
|
+
throw new HostError("Cached membership does not match the saved Request.");
|
|
987
|
+
await validateMembershipSecret(record, config, member);
|
|
988
|
+
const c = coordinates(url);
|
|
989
|
+
await checkCachedNode(
|
|
990
|
+
context.stateDir,
|
|
991
|
+
nodeScope(c.service, c.scopeId),
|
|
992
|
+
config.epoch,
|
|
993
|
+
member.peerId,
|
|
994
|
+
);
|
|
995
|
+
return { header, account: record.account };
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
// SharedContent invokes this source only after a genuine local ENOENT. Its
|
|
999
|
+
// store is not replaced from inside its own pending read (which would deadlock).
|
|
1000
|
+
async function lazyPrivateSource(
|
|
1001
|
+
context: SwarmContext,
|
|
1002
|
+
url: string,
|
|
1003
|
+
account: string,
|
|
1004
|
+
basis: Organization,
|
|
1005
|
+
) {
|
|
1006
|
+
const lifetime = new AbortController();
|
|
1007
|
+
let pending: Promise<ReturnType<typeof kuboSource>> | undefined;
|
|
1008
|
+
const load = async (signal: AbortSignal) => {
|
|
1009
|
+
const scoped: SwarmContext = { ...context };
|
|
1010
|
+
// Re-read the protected HOME binding and live approval on the first miss;
|
|
1011
|
+
// do not serve an old resolved Name through a different epoch or identity.
|
|
1012
|
+
delete scoped.identity;
|
|
1013
|
+
const authorize = async () => {
|
|
1014
|
+
const record = await approvedMembership(scoped, url, signal);
|
|
1015
|
+
signal.throwIfAborted();
|
|
1016
|
+
if (
|
|
1017
|
+
record.account !== account ||
|
|
1018
|
+
record.config!.owner !== basis.owner ||
|
|
1019
|
+
record.config!.epoch !== basis.epoch ||
|
|
1020
|
+
record.config!.headId !== basis.headId
|
|
1021
|
+
)
|
|
1022
|
+
throw new HostError(
|
|
1023
|
+
"Swarm membership changed since this cached HEAD; retry with --force.",
|
|
1024
|
+
);
|
|
1025
|
+
return (await privateSource(scoped, record, signal)).source;
|
|
1026
|
+
};
|
|
1027
|
+
// Same lock order as Owner rotation: credentials first, then the node.
|
|
1028
|
+
return withPrivateLock(await directory(scoped), authorize, signal);
|
|
1029
|
+
};
|
|
1030
|
+
const store = context.store.withLocalSource({
|
|
1031
|
+
block: {
|
|
1032
|
+
async get(cid, options = {}) {
|
|
1033
|
+
const signal = options.signal
|
|
1034
|
+
? AbortSignal.any([lifetime.signal, options.signal])
|
|
1035
|
+
: lifetime.signal;
|
|
1036
|
+
signal.throwIfAborted();
|
|
1037
|
+
const source = await (pending ??= load(signal));
|
|
1038
|
+
signal.throwIfAborted();
|
|
1039
|
+
return source.block.get(cid, { signal });
|
|
1040
|
+
},
|
|
1041
|
+
},
|
|
1042
|
+
});
|
|
1043
|
+
await context.store.close();
|
|
1044
|
+
context.store = store;
|
|
1045
|
+
delete context.node;
|
|
1046
|
+
delete context.sourcePeers;
|
|
1047
|
+
context.finishRead = async () => {
|
|
1048
|
+
lifetime.abort();
|
|
1049
|
+
await pending?.catch(() => {});
|
|
1050
|
+
};
|
|
1051
|
+
}
|
|
879
1052
|
export async function addSwarm(
|
|
880
1053
|
context: SwarmContext,
|
|
881
1054
|
url: string,
|
|
@@ -966,6 +1139,7 @@ export async function createSwarm(
|
|
|
966
1139
|
const node = await prepareNode(
|
|
967
1140
|
context.stateDir,
|
|
968
1141
|
nodeScope(service, record.scopeId),
|
|
1142
|
+
context.listen,
|
|
969
1143
|
);
|
|
970
1144
|
if (!record.headId) {
|
|
971
1145
|
const root = await publication(
|
|
@@ -1119,6 +1293,7 @@ export async function requestMembership(
|
|
|
1119
1293
|
const node = await prepareNode(
|
|
1120
1294
|
context.stateDir,
|
|
1121
1295
|
nodeScope(coordinates(url).service, config.scopeId),
|
|
1296
|
+
context.listen,
|
|
1122
1297
|
),
|
|
1123
1298
|
encryption = encryptionIdentity();
|
|
1124
1299
|
const body = {
|
|
@@ -1726,38 +1901,75 @@ export function formalName(name: string): string {
|
|
|
1726
1901
|
throw new HostError("Invalid formal Name.");
|
|
1727
1902
|
return name;
|
|
1728
1903
|
}
|
|
1904
|
+
function decodeNameComponent(
|
|
1905
|
+
value: string,
|
|
1906
|
+
part: "authority" | "path",
|
|
1907
|
+
): string {
|
|
1908
|
+
try {
|
|
1909
|
+
return decodeURIComponent(value);
|
|
1910
|
+
} catch {
|
|
1911
|
+
throw new HostError(
|
|
1912
|
+
`Malformed percent-encoding in IPAB URI ${part}; use valid UTF-8 escapes or copy the reference from ipab search.`,
|
|
1913
|
+
);
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1729
1916
|
export function parseNameReference(
|
|
1730
1917
|
reference: string,
|
|
1731
1918
|
): { swarm?: string; name: string } | undefined {
|
|
1732
1919
|
if (/^name:/i.test(reference))
|
|
1733
1920
|
throw new HostError("name: is unsupported; use ipab://ALIAS/PATH.");
|
|
1734
1921
|
if (!/^ipab:/i.test(reference)) return undefined;
|
|
1735
|
-
|
|
1922
|
+
if (/^ipab:\/\/https?:\/\//i.test(reference))
|
|
1923
|
+
throw new HostError(
|
|
1924
|
+
"Unencoded configuration URL in IPAB URI. Percent-encode the entire configuration URL and each Name path segment separately, or copy the reference from ipab search.",
|
|
1925
|
+
);
|
|
1926
|
+
const match = /^ipab:\/\/([^/]*)\/(.*)$/is.exec(reference);
|
|
1736
1927
|
if (!match)
|
|
1737
1928
|
throw new HostError(
|
|
1738
1929
|
"Invalid IPAB URI; percent-encode the configuration URL authority and each path segment.",
|
|
1739
1930
|
);
|
|
1740
|
-
const authority =
|
|
1741
|
-
if (authority && !/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(authority))
|
|
1931
|
+
const authority = decodeNameComponent(match[1]!, "authority");
|
|
1932
|
+
if (authority && !/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(authority)) {
|
|
1933
|
+
if (!/^https?:\/\//i.test(authority))
|
|
1934
|
+
throw new HostError(
|
|
1935
|
+
"Invalid IPAB URI authority; use a registered alias or the percent-encoded configuration URL.",
|
|
1936
|
+
);
|
|
1742
1937
|
canonicalSwarmUrl(authority);
|
|
1938
|
+
}
|
|
1939
|
+
if (/[?#\s]/.test(match[2]!))
|
|
1940
|
+
throw new HostError(
|
|
1941
|
+
"Invalid IPAB URI path; percent-encode each Name path segment. Queries and fragments are not supported.",
|
|
1942
|
+
);
|
|
1743
1943
|
const segments = match[2]!.split("/").map((segment) => {
|
|
1744
|
-
const decoded =
|
|
1745
|
-
if (decoded
|
|
1746
|
-
throw new HostError(
|
|
1944
|
+
const decoded = decodeNameComponent(segment, "path");
|
|
1945
|
+
if (/[/\\]/.test(decoded))
|
|
1946
|
+
throw new HostError(
|
|
1947
|
+
"Invalid IPAB URI path separator; use literal / between Name segments, never an encoded slash or backslash.",
|
|
1948
|
+
);
|
|
1747
1949
|
return decoded;
|
|
1748
1950
|
});
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1951
|
+
try {
|
|
1952
|
+
return {
|
|
1953
|
+
...(authority ? { swarm: authority } : {}),
|
|
1954
|
+
name: formalName(segments.join("/")),
|
|
1955
|
+
};
|
|
1956
|
+
} catch (error) {
|
|
1957
|
+
if (!(error instanceof HostError)) throw error;
|
|
1958
|
+
throw new HostError(
|
|
1959
|
+
"Invalid IPAB URI path; use a safe relative Name of at most 256 characters without empty, . or .. segments.",
|
|
1960
|
+
error.exitCode,
|
|
1961
|
+
error.code,
|
|
1962
|
+
error.phase,
|
|
1963
|
+
);
|
|
1964
|
+
}
|
|
1753
1965
|
}
|
|
1754
|
-
function
|
|
1966
|
+
export function formatNameReference(swarm: string, name: string): string {
|
|
1755
1967
|
const segment = (value: string) =>
|
|
1756
1968
|
encodeURIComponent(value).replace(
|
|
1757
1969
|
/[!'()*]/g,
|
|
1758
1970
|
(char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`,
|
|
1759
1971
|
);
|
|
1760
|
-
return `ipab://${segment(
|
|
1972
|
+
return `ipab://${segment(swarm)}/${name.split("/").map(segment).join("/")}`;
|
|
1761
1973
|
}
|
|
1762
1974
|
export async function resolveReference(
|
|
1763
1975
|
context: SwarmContext,
|
|
@@ -1765,6 +1977,7 @@ export async function resolveReference(
|
|
|
1765
1977
|
swarm: string | undefined,
|
|
1766
1978
|
local: (reference: string) => string,
|
|
1767
1979
|
raw = false,
|
|
1980
|
+
mode: "active" | "local-first" = "active",
|
|
1768
1981
|
): Promise<{ targetCid: string; cid: string }> {
|
|
1769
1982
|
const shared = parseNameReference(reference);
|
|
1770
1983
|
let targetCid: string;
|
|
@@ -1776,7 +1989,9 @@ export async function resolveReference(
|
|
|
1776
1989
|
throw new HostError(
|
|
1777
1990
|
"Select a configuration URL/alias using --swarm or swarm use.",
|
|
1778
1991
|
);
|
|
1779
|
-
|
|
1992
|
+
const warm =
|
|
1993
|
+
mode === "local-first" ? await localHeader(context, selected) : undefined;
|
|
1994
|
+
let header = warm?.header ?? (await swarmHeader(context, selected));
|
|
1780
1995
|
let link = Object.hasOwn(header.snapshot.names, shared.name)
|
|
1781
1996
|
? header.snapshot.names[shared.name]
|
|
1782
1997
|
: undefined;
|
|
@@ -1790,6 +2005,8 @@ export async function resolveReference(
|
|
|
1790
2005
|
throw new HostError(
|
|
1791
2006
|
`Formal Name "${shared.name}" was not found after refreshing HEAD.`,
|
|
1792
2007
|
);
|
|
2008
|
+
if (warm && header === warm.header)
|
|
2009
|
+
await lazyPrivateSource(context, selected, warm.account, header.config);
|
|
1793
2010
|
targetCid = link.toString();
|
|
1794
2011
|
} else {
|
|
1795
2012
|
if (swarm) await readyMembership(context, swarm);
|
|
@@ -1846,7 +2063,9 @@ export async function searchNames(
|
|
|
1846
2063
|
verifiedAt: string;
|
|
1847
2064
|
}[] = [];
|
|
1848
2065
|
for (const swarm of swarms) {
|
|
1849
|
-
const header =
|
|
2066
|
+
const header =
|
|
2067
|
+
(await localHeader(context, swarm.cid))?.header ??
|
|
2068
|
+
(await swarmHeader(context, swarm.cid));
|
|
1850
2069
|
searched.push({
|
|
1851
2070
|
configUrl: swarm.cid,
|
|
1852
2071
|
source: header.source,
|
|
@@ -1860,7 +2079,7 @@ export async function searchNames(
|
|
|
1860
2079
|
swarm: swarm.cid,
|
|
1861
2080
|
alias: swarm.alias,
|
|
1862
2081
|
name,
|
|
1863
|
-
reference:
|
|
2082
|
+
reference: formatNameReference(swarm.cid, name),
|
|
1864
2083
|
targetCid: header.snapshot.names[name]!.toString(),
|
|
1865
2084
|
});
|
|
1866
2085
|
}
|