~james/core

core/web/src/lib/api/knot.ts -rw-r--r-- 2.6 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { createBobbinClient, type BobbinContext } from "#lib/api/client.ts";
import { resolveRepoKnot } from "#lib/api/repo.ts";
import { capabilityOf, type Capability, type Did, type KnotOrigin } from "#lib/api/syntax.ts";

export const SOCIAL_WRITES_CAPABILITY = capabilityOf("knot-social-writes");

const DESCRIBE_KNOT_NSID = "org.tangled.knot.describeKnot";
export const CAPABILITY_TTL_MS = 5 * 60_000;
const FAILED_PROBE_TTL_MS = 30_000;
const PROBE_TIMEOUT_MS = 10_000;

const probes = new Map<KnotOrigin, { at: number; capabilities: readonly Capability[] | null }>();
const asking = new Map<KnotOrigin, Promise<readonly Capability[] | null>>();

const capabilitiesOf = (body: unknown): readonly Capability[] | null => {
	if (typeof body !== "object" || body === null) return null;
	const { capabilities } = body as Record<string, unknown>;
	if (!Array.isArray(capabilities)) return null;
	return capabilities.every((token) => typeof token === "string")
		? capabilities.map(capabilityOf)
		: null;
};

const describeKnot = async (knot: KnotOrigin): Promise<readonly Capability[] | null> => {
	const response = await fetch(`https://${knot}/xrpc/${DESCRIBE_KNOT_NSID}`, {
		headers: { accept: "application/json" },
		signal: AbortSignal.timeout(PROBE_TIMEOUT_MS)
	});
	if (response.status === 404) return [];
	if (!response.ok) return null;
	return capabilitiesOf(await response.json().catch(() => null));
};

export const knotCapabilities = async (
	knot: KnotOrigin,
	now: () => number = Date.now
): Promise<readonly Capability[] | null> => {
	const cached = probes.get(knot);
	const ttl = cached?.capabilities === null ? FAILED_PROBE_TTL_MS : CAPABILITY_TTL_MS;
	if (cached && now() - cached.at < ttl) return cached.capabilities;
	const pending = asking.get(knot);
	if (pending) return pending;
	const probe = describeKnot(knot)
		.catch(() => null)
		.finally(() => asking.delete(knot));
	asking.set(knot, probe);
	const capabilities = await probe;
	probes.set(knot, { at: now(), capabilities });
	return capabilities;
};

export const readTarget = async (
	ctx: BobbinContext,
	repoDid: Did,
	fosterUrl?: KnotOrigin | ""
): Promise<KnotOrigin | undefined> => {
	const knot = await resolveRepoKnot(ctx, repoDid);
	if (!knot) return undefined;
	const capabilities = await knotCapabilities(knot);
	return capabilities === null || capabilities.includes(SOCIAL_WRITES_CAPABILITY)
		? knot
		: fosterUrl || knot;
};

export const readTargetFor = (
	bobbinUrl: string | undefined,
	repoDid: Did,
	fosterUrl?: KnotOrigin | ""
): Promise<KnotOrigin | undefined> =>
	bobbinUrl
		? readTarget(createBobbinClient({ serviceUrl: bobbinUrl }), repoDid, fosterUrl)
		: Promise.resolve(undefined);