mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-28 17:50:28 +08:00
desktop: Respect $host in publicExplorer template
So users can choose whether to pick the first seed from `preferredSeeds` or hard-code a different seed in their config like this: `https://radicle.network/nodes/oak.radicle.garden/$rid$path`.
This commit is contained in:
@@ -348,13 +348,22 @@ function explorerResourcePath(uri: RadUri): string | undefined {
|
||||
}
|
||||
}
|
||||
|
||||
// Fills the `publicExplorer` template the way heartwood's `ExplorerUrl` does:
|
||||
// `$host` becomes `host`, `$rid` the repo and `$path` the resource. A template
|
||||
// without `$host` names its own node. User pages have no `$rid`, so they hang
|
||||
// off whatever precedes `$rid` in the template.
|
||||
export function explorerUrl(
|
||||
reference: RadReference,
|
||||
base: string,
|
||||
template: string,
|
||||
host: string,
|
||||
): string | undefined {
|
||||
const node = `${base.replace(/\/+$/, "")}/nodes/${host}`;
|
||||
// Replacer functions keep `$&` and friends in a value literal, as heartwood's
|
||||
// `str::replace` does.
|
||||
const url = template.replaceAll("$host", () => host);
|
||||
if (reference.type === "did") {
|
||||
const rid = url.indexOf("$rid");
|
||||
if (rid === -1) return undefined;
|
||||
const node = url.slice(0, rid).replace(/\/+$/, "");
|
||||
return `${node}/users/${formatDid(reference.node)}`;
|
||||
}
|
||||
|
||||
@@ -366,7 +375,11 @@ export function explorerUrl(
|
||||
? `#${reference.uri.fragment}`
|
||||
: "";
|
||||
|
||||
return `${node}/rad:${reference.uri.repo}${path}${fragment}`;
|
||||
return (
|
||||
url
|
||||
.replaceAll("$rid", () => `rad:${reference.uri.repo}`)
|
||||
.replaceAll("$path", () => path) + fragment
|
||||
);
|
||||
}
|
||||
|
||||
function decodeSegment(segment: string): string {
|
||||
|
||||
@@ -359,17 +359,7 @@ export function explorerLink(
|
||||
reference: RadReference,
|
||||
config: Config,
|
||||
): string | undefined {
|
||||
return explorerUrl(reference, explorerBase(config), explorerSeed(config));
|
||||
}
|
||||
|
||||
function explorerBase(config: Config): string {
|
||||
const nodes = config.publicExplorer.indexOf("/nodes/$host");
|
||||
if (nodes !== -1) return config.publicExplorer.slice(0, nodes);
|
||||
try {
|
||||
return new URL(config.publicExplorer).origin;
|
||||
} catch {
|
||||
return config.publicExplorer;
|
||||
}
|
||||
return explorerUrl(reference, config.publicExplorer, explorerSeed(config));
|
||||
}
|
||||
|
||||
function explorerSeed(config: Config): string {
|
||||
|
||||
@@ -1,9 +1,23 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import type { RadUri } from "@app/lib/radUri";
|
||||
|
||||
// Most cases care about the path, not the template, so they build the
|
||||
// explorer's default `/nodes/$host/$rid$path` shape from a base URL.
|
||||
function explorerUrl(
|
||||
reference: Parameters<typeof explorerUrlFromTemplate>[0],
|
||||
base: string,
|
||||
host: string,
|
||||
) {
|
||||
return explorerUrlFromTemplate(
|
||||
reference,
|
||||
`${base.replace(/\/+$/, "")}/nodes/$host/$rid$path`,
|
||||
host,
|
||||
);
|
||||
}
|
||||
import {
|
||||
cobListType,
|
||||
explorerUrl,
|
||||
explorerUrl as explorerUrlFromTemplate,
|
||||
filePath,
|
||||
fileReference,
|
||||
formatRadUri,
|
||||
@@ -395,6 +409,45 @@ describe("explorer URLs", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("explorer templates", () => {
|
||||
const did = { type: "did", node: node01 } as const;
|
||||
const patch = parseReference(`rad:${repo00}/cob/${patchType}/${oid}`)!;
|
||||
|
||||
test("keeps a host written into the template", () => {
|
||||
const template =
|
||||
"https://radicle.network/nodes/oak.radicle.garden/$rid$path";
|
||||
const node = "https://radicle.network/nodes/oak.radicle.garden";
|
||||
expect(explorerUrlFromTemplate(patch, template, "seed")).toBe(
|
||||
`${node}/rad:${repo00}/patches/${oid}`,
|
||||
);
|
||||
expect(explorerUrlFromTemplate(did, template, "seed")).toBe(
|
||||
`${node}/users/did:key:${node01}`,
|
||||
);
|
||||
});
|
||||
|
||||
test("fills a template of another shape", () => {
|
||||
const template = "https://example.com/$host/repos/$rid$path";
|
||||
expect(explorerUrlFromTemplate(patch, template, "seed")).toBe(
|
||||
`https://example.com/seed/repos/rad:${repo00}/patches/${oid}`,
|
||||
);
|
||||
expect(explorerUrlFromTemplate(did, template, "seed")).toBe(
|
||||
`https://example.com/seed/repos/users/did:key:${node01}`,
|
||||
);
|
||||
});
|
||||
|
||||
test("has no user page without $rid", () => {
|
||||
expect(
|
||||
explorerUrlFromTemplate(did, "https://example.com/$host/$path", "seed"),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
test("keeps replacement patterns in the host literal", () => {
|
||||
expect(
|
||||
explorerUrlFromTemplate(patch, "https://x/nodes/$host/$rid$path", "a$&b"),
|
||||
).toBe(`https://x/nodes/a$&b/rad:${repo00}/patches/${oid}`);
|
||||
});
|
||||
});
|
||||
|
||||
describe("explorer URL edge cases", () => {
|
||||
const base = "https://app.radicle.xyz";
|
||||
const node = `${base}/nodes/seed.radicle.xyz`;
|
||||
|
||||
@@ -261,6 +261,22 @@ describe("explorer links", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("keeps a host written into the template", () => {
|
||||
const publicExplorer =
|
||||
"https://radicle.network/nodes/oak.radicle.garden/$rid$path";
|
||||
const preferredSeeds = [`${nid}@seed1.oak.radicle.garden:8776`];
|
||||
const node = "https://radicle.network/nodes/oak.radicle.garden";
|
||||
expect(explorerLink(repo, config({ publicExplorer, preferredSeeds }))).toBe(
|
||||
`${node}/${rid}`,
|
||||
);
|
||||
expect(
|
||||
explorerLink(
|
||||
{ type: "did", node: nid },
|
||||
config({ publicExplorer, preferredSeeds }),
|
||||
),
|
||||
).toBe(`${node}/users/did:key:${nid}`);
|
||||
});
|
||||
|
||||
test("explorerHost returns the host of the template", () => {
|
||||
expect(explorerHost(config({}))).toBe("radicle.network");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user