@ipab/cli 0.1.0-rc.4 → 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 +12 -7
- package/dist/cli.d.ts +3 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +114 -25
- 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 +1 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +12 -1
- 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/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 +11 -4
- package/dist/kubo.d.ts.map +1 -1
- package/dist/kubo.js +150 -197
- 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 +2 -2
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +59 -29
- package/dist/state.js.map +1 -1
- package/dist/swarm-files.d.ts +1 -1
- package/dist/swarm-files.d.ts.map +1 -1
- package/dist/swarm-files.js +4 -1
- package/dist/swarm-files.js.map +1 -1
- package/dist/swarm.d.ts +6 -3
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +184 -70
- package/dist/swarm.js.map +1 -1
- package/package.json +4 -4
- package/src/cli.ts +146 -39
- package/src/config-secret.ts +2 -84
- package/src/config.ts +14 -1
- package/src/context-types.ts +11 -0
- package/src/context.ts +40 -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 +207 -269
- 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 +68 -35
- package/src/swarm-files.ts +4 -0
- package/src/swarm.ts +256 -90
package/src/swarm-files.ts
CHANGED
|
@@ -76,7 +76,9 @@ export async function writePrivate(
|
|
|
76
76
|
export async function withPrivateLock<T>(
|
|
77
77
|
directory: string,
|
|
78
78
|
operation: () => Promise<T>,
|
|
79
|
+
signal?: AbortSignal,
|
|
79
80
|
): Promise<T> {
|
|
81
|
+
signal?.throwIfAborted();
|
|
80
82
|
const path = join(directory, "operation.sqlite");
|
|
81
83
|
try {
|
|
82
84
|
const handle = await open(path, "wx", 0o600);
|
|
@@ -91,6 +93,7 @@ export async function withPrivateLock<T>(
|
|
|
91
93
|
try {
|
|
92
94
|
db.exec("PRAGMA busy_timeout=0");
|
|
93
95
|
for (let attempt = 0; attempt < 300; attempt++) {
|
|
96
|
+
signal?.throwIfAborted();
|
|
94
97
|
try {
|
|
95
98
|
db.exec("BEGIN IMMEDIATE");
|
|
96
99
|
locked = true;
|
|
@@ -108,6 +111,7 @@ export async function withPrivateLock<T>(
|
|
|
108
111
|
throw new HostError(
|
|
109
112
|
"Another local Swarm operation is still running; retry later.",
|
|
110
113
|
);
|
|
114
|
+
signal?.throwIfAborted();
|
|
111
115
|
return await operation();
|
|
112
116
|
} finally {
|
|
113
117
|
if (locked) db.exec("ROLLBACK");
|
package/src/swarm.ts
CHANGED
|
@@ -36,6 +36,7 @@ import {
|
|
|
36
36
|
} from "@ipab/bundle/pointer/crypto";
|
|
37
37
|
import { HostError, object, message } from "./errors.js";
|
|
38
38
|
import { accountIdentity, type Identity } from "./identity.js";
|
|
39
|
+
import { swarmUrl as canonicalSwarmUrl } from "./config.js";
|
|
39
40
|
import { bundleJson, provideContent } from "./content-provider.js";
|
|
40
41
|
import { bytes, relativePath } from "./space.js";
|
|
41
42
|
import { appSchemaCid } from "./app-definition.js";
|
|
@@ -57,6 +58,7 @@ import {
|
|
|
57
58
|
parseEnvelope,
|
|
58
59
|
} from "./swarm-envelope.js";
|
|
59
60
|
import {
|
|
61
|
+
checkCachedNode,
|
|
60
62
|
kuboSource,
|
|
61
63
|
peerId,
|
|
62
64
|
peerProof,
|
|
@@ -66,6 +68,7 @@ import {
|
|
|
66
68
|
type KuboNode,
|
|
67
69
|
type ListenMode,
|
|
68
70
|
} from "./kubo.js";
|
|
71
|
+
import { memberPeers } from "./kubo-peering.js";
|
|
69
72
|
|
|
70
73
|
export interface Member {
|
|
71
74
|
publicKey: string;
|
|
@@ -122,6 +125,8 @@ export interface SwarmContext {
|
|
|
122
125
|
forceRefresh?: boolean;
|
|
123
126
|
diagnostic?: (text: string) => void;
|
|
124
127
|
node?: KuboNode;
|
|
128
|
+
sourcePeers?: readonly string[];
|
|
129
|
+
finishRead?: () => Promise<void>;
|
|
125
130
|
identity?: Identity;
|
|
126
131
|
listen?: ListenMode;
|
|
127
132
|
}
|
|
@@ -172,26 +177,7 @@ function withState<T>(
|
|
|
172
177
|
state.close();
|
|
173
178
|
}
|
|
174
179
|
}
|
|
175
|
-
export
|
|
176
|
-
if (!/^https?:\/\//.test(value))
|
|
177
|
-
throw new HostError(
|
|
178
|
-
"Swarm requires a configuration URL; old Swarm CID protocol is unsupported.",
|
|
179
|
-
);
|
|
180
|
-
const url = new URL(value);
|
|
181
|
-
canonicalService(url.origin);
|
|
182
|
-
if (
|
|
183
|
-
url.search ||
|
|
184
|
-
url.hash ||
|
|
185
|
-
url.username ||
|
|
186
|
-
url.password ||
|
|
187
|
-
url.toString() !== value ||
|
|
188
|
-
!/^\/v1\/[a-f0-9]{16}\/[a-f0-9]{16}$/.test(url.pathname)
|
|
189
|
-
)
|
|
190
|
-
throw new HostError(
|
|
191
|
-
"Swarm must be an exact Scope configuration URL; old Swarm CID protocol is unsupported.",
|
|
192
|
-
);
|
|
193
|
-
return value;
|
|
194
|
-
}
|
|
180
|
+
export { canonicalSwarmUrl };
|
|
195
181
|
function coordinates(configUrl: string): {
|
|
196
182
|
service: string;
|
|
197
183
|
scopeId: string;
|
|
@@ -366,12 +352,14 @@ function parseProfile(value: unknown, expectedPeer: string): Profile {
|
|
|
366
352
|
async function fetchConfig(
|
|
367
353
|
context: SwarmContext,
|
|
368
354
|
configUrl: string,
|
|
355
|
+
signal?: AbortSignal,
|
|
369
356
|
): Promise<{ config: Organization; variable: Variable }> {
|
|
370
357
|
const c = coordinates(configUrl),
|
|
371
358
|
api = client(context, c.service);
|
|
359
|
+
const options = signal ? { signal } : {};
|
|
372
360
|
const [variable, scope] = await Promise.all([
|
|
373
|
-
api.read(c.scopeId, c.configId),
|
|
374
|
-
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),
|
|
375
363
|
]);
|
|
376
364
|
const config = parseOrganization(variable.value, configUrl);
|
|
377
365
|
if (scope.owner !== config.owner)
|
|
@@ -436,10 +424,11 @@ async function validateMembershipSecret(
|
|
|
436
424
|
);
|
|
437
425
|
}
|
|
438
426
|
}
|
|
439
|
-
async function
|
|
427
|
+
async function privateSource(
|
|
440
428
|
context: SwarmContext,
|
|
441
429
|
record: LocalMembership,
|
|
442
|
-
|
|
430
|
+
signal?: AbortSignal,
|
|
431
|
+
) {
|
|
443
432
|
const config = record.config!,
|
|
444
433
|
secret = record.secret!;
|
|
445
434
|
const identity = await contextIdentity(context);
|
|
@@ -469,11 +458,13 @@ async function activate(
|
|
|
469
458
|
identity.publicKey,
|
|
470
459
|
);
|
|
471
460
|
}
|
|
472
|
-
const addresses = [
|
|
461
|
+
const addresses = new Map<string, readonly string[]>();
|
|
473
462
|
const api = client(context, c.service);
|
|
474
463
|
let selfProfile: { profile: Profile; cid: string } | undefined;
|
|
475
464
|
const profiles = await Promise.allSettled(
|
|
476
|
-
config.members.map((member) =>
|
|
465
|
+
config.members.map((member) =>
|
|
466
|
+
api.read(c.scopeId, member.profileId, signal ? { signal } : {}),
|
|
467
|
+
),
|
|
477
468
|
);
|
|
478
469
|
for (const [index, member] of config.members.entries()) {
|
|
479
470
|
const result = profiles[index]!;
|
|
@@ -482,28 +473,35 @@ async function activate(
|
|
|
482
473
|
const value = parseProfile(profile.value, member.peerId);
|
|
483
474
|
if (member.publicKey === identity.publicKey)
|
|
484
475
|
selfProfile = { profile: value, cid: profile.cid };
|
|
485
|
-
addresses.
|
|
476
|
+
addresses.set(member.peerId, value.addresses);
|
|
486
477
|
}
|
|
478
|
+
const desired = memberPeers(self.peerId, addresses, config.bootstrap);
|
|
479
|
+
signal?.throwIfAborted();
|
|
487
480
|
const node = await startNode(
|
|
488
481
|
context.stateDir,
|
|
489
482
|
nodeScope(c.service, config.scopeId),
|
|
490
483
|
secret.swarmKey,
|
|
491
484
|
config.epoch,
|
|
492
|
-
[...new Set(addresses)],
|
|
493
|
-
context.diagnostic,
|
|
494
485
|
context.listen,
|
|
486
|
+
{ self: self.peerId, peers: desired, ...(signal ? { signal } : {}) },
|
|
495
487
|
);
|
|
496
488
|
if (node.peerId !== self.peerId)
|
|
497
489
|
throw new HostError(
|
|
498
490
|
"Approved PeerID differs from this local Request's Kubo identity.",
|
|
499
491
|
);
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
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
|
+
) {
|
|
507
505
|
const writer = client(
|
|
508
506
|
context,
|
|
509
507
|
c.service,
|
|
@@ -521,10 +519,35 @@ async function activate(
|
|
|
521
519
|
profile,
|
|
522
520
|
selfProfile.cid,
|
|
523
521
|
uniqueId(),
|
|
522
|
+
signal ? { signal } : {},
|
|
524
523
|
);
|
|
525
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
|
+
};
|
|
526
535
|
}
|
|
527
|
-
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(
|
|
528
551
|
context: SwarmContext,
|
|
529
552
|
configUrl: string,
|
|
530
553
|
): Promise<LocalMembership> {
|
|
@@ -539,7 +562,15 @@ async function refresh(
|
|
|
539
562
|
"Organization switch incomplete; only Owner can continue with swarm resume.",
|
|
540
563
|
);
|
|
541
564
|
}
|
|
542
|
-
|
|
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);
|
|
543
574
|
if (config.owner !== record.owner)
|
|
544
575
|
throw new HostError("Pinned Owner changed; refusing configuration.");
|
|
545
576
|
if (record.config && config.epoch < record.config.epoch)
|
|
@@ -573,6 +604,10 @@ async function refresh(
|
|
|
573
604
|
);
|
|
574
605
|
record.config = config;
|
|
575
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);
|
|
576
611
|
await saveLocal(context, record);
|
|
577
612
|
await activate(context, record);
|
|
578
613
|
return record;
|
|
@@ -581,17 +616,7 @@ async function readyMembership(
|
|
|
581
616
|
context: SwarmContext,
|
|
582
617
|
configUrl: string,
|
|
583
618
|
): Promise<LocalMembership> {
|
|
584
|
-
const record = await
|
|
585
|
-
if (record.account === record.owner) {
|
|
586
|
-
const transition = await readPrivate<Removal>(
|
|
587
|
-
await directory(context),
|
|
588
|
-
`transition-${localName(configUrl)}`,
|
|
589
|
-
);
|
|
590
|
-
if (transition && !transition.completed)
|
|
591
|
-
throw new HostError(
|
|
592
|
-
"Organization switch incomplete; run swarm resume before normal Owner operations.",
|
|
593
|
-
);
|
|
594
|
-
}
|
|
619
|
+
const record = await localForRead(context, configUrl);
|
|
595
620
|
if (!record.config || !record.secret) return refresh(context, configUrl);
|
|
596
621
|
if (
|
|
597
622
|
!context.node ||
|
|
@@ -874,32 +899,8 @@ export async function swarmHeader(
|
|
|
874
899
|
let record = force
|
|
875
900
|
? await refresh(context, configUrl)
|
|
876
901
|
: await readyMembership(context, configUrl);
|
|
877
|
-
const
|
|
878
|
-
|
|
879
|
-
? undefined
|
|
880
|
-
: (withState(context, (state) =>
|
|
881
|
-
state.swarmHeader(record.account, configUrl),
|
|
882
|
-
) as
|
|
883
|
-
| {
|
|
884
|
-
head: Variable;
|
|
885
|
-
snapshot: string;
|
|
886
|
-
verifiedAt: number;
|
|
887
|
-
headId: string;
|
|
888
|
-
}
|
|
889
|
-
| undefined);
|
|
890
|
-
if (
|
|
891
|
-
cached &&
|
|
892
|
-
cached.headId === record.config!.headId &&
|
|
893
|
-
Date.now() >= cached.verifiedAt &&
|
|
894
|
-
Date.now() - cached.verifiedAt < ttl * 1000
|
|
895
|
-
)
|
|
896
|
-
return {
|
|
897
|
-
head: headValue(cached.head),
|
|
898
|
-
snapshot: decodeSnapshot(Buffer.from(cached.snapshot, "base64")),
|
|
899
|
-
config: record.config!,
|
|
900
|
-
source: "cache",
|
|
901
|
-
verifiedAt: new Date(cached.verifiedAt).toISOString(),
|
|
902
|
-
};
|
|
902
|
+
const cached = !force && cachedHeader(context, record);
|
|
903
|
+
if (cached) return cached;
|
|
903
904
|
let head = await (await boundPointer(context, configUrl, record)).read();
|
|
904
905
|
let snapshot: Snapshot;
|
|
905
906
|
try {
|
|
@@ -917,6 +918,7 @@ export async function swarmHeader(
|
|
|
917
918
|
snapshot: Buffer.from(dagCbor.encode(snapshot)).toString("base64"),
|
|
918
919
|
verifiedAt,
|
|
919
920
|
headId: current.config!.headId,
|
|
921
|
+
epoch: current.config!.epoch,
|
|
920
922
|
});
|
|
921
923
|
});
|
|
922
924
|
return {
|
|
@@ -927,6 +929,126 @@ export async function swarmHeader(
|
|
|
927
929
|
verifiedAt: new Date(verifiedAt).toISOString(),
|
|
928
930
|
};
|
|
929
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
|
+
}
|
|
930
1052
|
export async function addSwarm(
|
|
931
1053
|
context: SwarmContext,
|
|
932
1054
|
url: string,
|
|
@@ -1779,38 +1901,75 @@ export function formalName(name: string): string {
|
|
|
1779
1901
|
throw new HostError("Invalid formal Name.");
|
|
1780
1902
|
return name;
|
|
1781
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
|
+
}
|
|
1782
1916
|
export function parseNameReference(
|
|
1783
1917
|
reference: string,
|
|
1784
1918
|
): { swarm?: string; name: string } | undefined {
|
|
1785
1919
|
if (/^name:/i.test(reference))
|
|
1786
1920
|
throw new HostError("name: is unsupported; use ipab://ALIAS/PATH.");
|
|
1787
1921
|
if (!/^ipab:/i.test(reference)) return undefined;
|
|
1788
|
-
|
|
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);
|
|
1789
1927
|
if (!match)
|
|
1790
1928
|
throw new HostError(
|
|
1791
1929
|
"Invalid IPAB URI; percent-encode the configuration URL authority and each path segment.",
|
|
1792
1930
|
);
|
|
1793
|
-
const authority =
|
|
1794
|
-
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
|
+
);
|
|
1795
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
|
+
);
|
|
1796
1943
|
const segments = match[2]!.split("/").map((segment) => {
|
|
1797
|
-
const decoded =
|
|
1798
|
-
if (decoded
|
|
1799
|
-
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
|
+
);
|
|
1800
1949
|
return decoded;
|
|
1801
1950
|
});
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
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
|
+
}
|
|
1806
1965
|
}
|
|
1807
|
-
function
|
|
1966
|
+
export function formatNameReference(swarm: string, name: string): string {
|
|
1808
1967
|
const segment = (value: string) =>
|
|
1809
1968
|
encodeURIComponent(value).replace(
|
|
1810
1969
|
/[!'()*]/g,
|
|
1811
1970
|
(char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`,
|
|
1812
1971
|
);
|
|
1813
|
-
return `ipab://${segment(
|
|
1972
|
+
return `ipab://${segment(swarm)}/${name.split("/").map(segment).join("/")}`;
|
|
1814
1973
|
}
|
|
1815
1974
|
export async function resolveReference(
|
|
1816
1975
|
context: SwarmContext,
|
|
@@ -1818,6 +1977,7 @@ export async function resolveReference(
|
|
|
1818
1977
|
swarm: string | undefined,
|
|
1819
1978
|
local: (reference: string) => string,
|
|
1820
1979
|
raw = false,
|
|
1980
|
+
mode: "active" | "local-first" = "active",
|
|
1821
1981
|
): Promise<{ targetCid: string; cid: string }> {
|
|
1822
1982
|
const shared = parseNameReference(reference);
|
|
1823
1983
|
let targetCid: string;
|
|
@@ -1829,7 +1989,9 @@ export async function resolveReference(
|
|
|
1829
1989
|
throw new HostError(
|
|
1830
1990
|
"Select a configuration URL/alias using --swarm or swarm use.",
|
|
1831
1991
|
);
|
|
1832
|
-
|
|
1992
|
+
const warm =
|
|
1993
|
+
mode === "local-first" ? await localHeader(context, selected) : undefined;
|
|
1994
|
+
let header = warm?.header ?? (await swarmHeader(context, selected));
|
|
1833
1995
|
let link = Object.hasOwn(header.snapshot.names, shared.name)
|
|
1834
1996
|
? header.snapshot.names[shared.name]
|
|
1835
1997
|
: undefined;
|
|
@@ -1843,6 +2005,8 @@ export async function resolveReference(
|
|
|
1843
2005
|
throw new HostError(
|
|
1844
2006
|
`Formal Name "${shared.name}" was not found after refreshing HEAD.`,
|
|
1845
2007
|
);
|
|
2008
|
+
if (warm && header === warm.header)
|
|
2009
|
+
await lazyPrivateSource(context, selected, warm.account, header.config);
|
|
1846
2010
|
targetCid = link.toString();
|
|
1847
2011
|
} else {
|
|
1848
2012
|
if (swarm) await readyMembership(context, swarm);
|
|
@@ -1899,7 +2063,9 @@ export async function searchNames(
|
|
|
1899
2063
|
verifiedAt: string;
|
|
1900
2064
|
}[] = [];
|
|
1901
2065
|
for (const swarm of swarms) {
|
|
1902
|
-
const header =
|
|
2066
|
+
const header =
|
|
2067
|
+
(await localHeader(context, swarm.cid))?.header ??
|
|
2068
|
+
(await swarmHeader(context, swarm.cid));
|
|
1903
2069
|
searched.push({
|
|
1904
2070
|
configUrl: swarm.cid,
|
|
1905
2071
|
source: header.source,
|
|
@@ -1913,7 +2079,7 @@ export async function searchNames(
|
|
|
1913
2079
|
swarm: swarm.cid,
|
|
1914
2080
|
alias: swarm.alias,
|
|
1915
2081
|
name,
|
|
1916
|
-
reference:
|
|
2082
|
+
reference: formatNameReference(swarm.cid, name),
|
|
1917
2083
|
targetCid: header.snapshot.names[name]!.toString(),
|
|
1918
2084
|
});
|
|
1919
2085
|
}
|