Files
shakespeare/src/lib/deploy/npanelApi.ts
T
Alex Gleason d06b9235a8 Give a name back under the title its site chose, and say which relays took it
Three things the migration got wrong, all of them the same shape: something was
known and not said.

The archive titled every site it carried after the site's hostname, because an
address was all it knew about a site it had only ever seen at one. Handing that
back hands back an address where a name should be — a site called "Arguable"
comes home titled "arguable.shakespeare.wtf". The gateway can now read the name
the site was carrying all along, out of its webmanifest or the metadata it
publishes for link previews, and that is what gets signed. A site that never
said what it is called gets no title, which is what a manifest without one has
always meant. Its `alt` goes too: it says the site is archived so that it keeps
working, which is a true sentence about the archive and stops being true here.

Names already taken back are listed and fixed the same way, rebuilt from the
event as relays hold it rather than from the gateway's copy — a site redeployed
since the migration ran has moved on, and rebuilding it from what the gateway
fetched would quietly restore an older version of it.

Publishing to relays went through a pool that resolves on the first acceptance
and discards every other answer, so a manifest that reached one relay out of
five was reported exactly like one that reached all five, and the reasons the
other four refused were received and thrown away. Of 102 manifests published
this way in one sitting, one relay held every one and another held none, and
nothing anywhere said so. Every relay is now published to on its own and
reported on by name. One acceptance is still a success — the event exists and
whoever reads that relay will find it — so this is a note, not a failure.

The gaps that leaves are closed without signing anything: the events go out
again exactly as they are, ids and signatures unchanged, to the relays whose own
answer says they are missing them. A relay that already has one says duplicate
and nothing happens; a relay that refused before finally says why, which is the
only way to hear a reason that was discarded at the time.

Ordinary nsite deploys published the same blind way, so they use the same
helper and carry the same coverage.

Lastly, names more than one label under the gateway's domain are refused rather
than published for. A wildcard certificate covers one label, so `a.b.example.com`
can be claimed, published for, and will still fail its handshake for everyone
forever. Five are on file, inherited from a host that accepted them; one already
has a manifest published for it, which can now be retracted.
2026-09-13 16:40:33 -05:00

138 lines
4.4 KiB
TypeScript

import { NIP98 } from '@nostrify/nostrify';
import { N64 } from '@nostrify/nostrify/utils';
import type { NostrSigner } from '@nostrify/nostrify';
/** Timeout for a single API call (ms). */
export const NPANEL_TIMEOUT_MS = 15_000;
/**
* A NIP-98 request to an npanel gateway's API.
*
* Signed against the URL being requested, so the token authorizes this call and
* no other. No CORS proxy: npanel answers browsers directly, and a proxy would
* rewrite the URL out from under the signature.
*/
export async function npanelRequest(
dashboardHost: string,
signer: NostrSigner,
method: string,
path: string,
body?: unknown,
signal?: AbortSignal,
): Promise<Response> {
const url = `https://${dashboardHost}${path}`;
let request = new Request(url, {
method,
...(body === undefined
? {}
: { body: JSON.stringify(body), headers: { 'Content-Type': 'application/json' } }),
});
const template = await NIP98.template(request);
const event = await signer.signEvent(template);
const headers = new Headers(request.headers);
headers.set('Authorization', `Nostr ${N64.encodeEvent(event)}`);
request = new Request(request, {
headers,
signal: signal ?? AbortSignal.timeout(NPANEL_TIMEOUT_MS),
});
return await fetch(request);
}
/** The gateway's own sentence about a failure, which is written to be read. */
export async function npanelError(response: Response, fallback: string): Promise<Error> {
const body = (await response.json().catch(() => null)) as { error?: string } | null;
if (response.status === 403 && !body?.error) {
return new Error(`${fallback}: this gateway is not accepting new sites right now.`);
}
return new Error(body?.error ?? `${fallback} (HTTP ${response.status}).`);
}
/** An nsite manifest, reduced to the parts that are the site. */
export interface NpanelClaimManifest {
kind: number;
content: string;
tags: string[][];
}
/** A name waiting to be handed back to whoever held it before this gateway. */
export interface NpanelClaim {
hostname: string;
/** Where the record came from, eg `picohost`. */
source: string;
/** Whether something is being served at the name right now. */
live: boolean;
/** Where the name points today, as `kind:pubkey:identifier`, or null if nowhere. */
address: string | null;
/** Whether that address is already the claimant's own key. */
mine: boolean;
/** The archive's copy, when there is one to republish. Null when `mine`. */
manifest: NpanelClaimManifest | null;
/**
* Whether this name has already been taken back.
*
* Absent from gateways that predate the field, which is why it is optional
* rather than assumed false: a list of waiting names is what `GET /api/claims`
* has always returned, so a missing flag means the row is waiting.
*/
claimed?: boolean;
/** What the site calls itself, where the gateway could work it out. */
suggestedTitle?: string | null;
suggestedDescription?: string | null;
}
/**
* The names this signer can take back from the gateway's archive.
*
* Empty for anyone with nothing waiting, which is almost everyone — this is the
* residue of one migration, not a feature of the gateway.
*
* `taken` also returns names already claimed, which is how an app fixes
* something about how it claimed them. Older gateways ignore the parameter and
* answer with the waiting ones, which is a smaller answer rather than a wrong
* one.
*/
export async function fetchNpanelClaims(
dashboardHost: string,
signer: NostrSigner,
signal?: AbortSignal,
opts: { taken?: boolean } = {},
): Promise<NpanelClaim[]> {
const path = opts.taken ? '/api/claims?taken=1' : '/api/claims';
const response = await npanelRequest(dashboardHost, signer, 'GET', path, undefined, signal);
if (!response.ok) {
throw await npanelError(response, 'Could not ask the gateway which names are waiting for you');
}
const { claims } = (await response.json()) as { claims: NpanelClaim[] };
return claims;
}
/** Point a name waiting for this signer at a site they published. */
export async function claimNpanelHostname(
dashboardHost: string,
signer: NostrSigner,
hostname: string,
address: string,
signal?: AbortSignal,
): Promise<void> {
const response = await npanelRequest(
dashboardHost,
signer,
'POST',
`/api/claims/${encodeURIComponent(hostname)}`,
{ address },
signal,
);
if (!response.ok) {
throw await npanelError(response, `Could not take back ${hostname}`);
}
}