Fix nostr.json cache by returning non-200 status

This commit is contained in:
Alex Gleason 2025-03-03 17:10:46 -06:00
parent ff4c3381ba
commit 363816d930
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -5,27 +5,23 @@ import { AppController } from '@/app.ts';
import { localNip05Lookup } from '@/utils/nip05.ts'; import { localNip05Lookup } from '@/utils/nip05.ts';
const nameSchema = z.string().min(1).regex(/^[\w.-]+$/); const nameSchema = z.string().min(1).regex(/^[\w.-]+$/);
const emptyResult: NostrJson = { names: {}, relays: {} };
/** /**
* Serves NIP-05's nostr.json. * Serves NIP-05's nostr.json.
* https://github.com/nostr-protocol/nips/blob/master/05.md * https://github.com/nostr-protocol/nips/blob/master/05.md
*/ */
const nostrController: AppController = async (c) => { const nostrController: AppController = async (c) => {
// If there are no query parameters, this will always return an empty result. const result = nameSchema.safeParse(c.req.query('name'));
if (!Object.entries(c.req.queries()).length) {
c.header('Cache-Control', 'max-age=31536000, public, immutable, stale-while-revalidate=86400'); if (!result.success) {
return c.json(emptyResult); return c.json({ error: 'Invalid name parameter' }, { status: 422 });
} }
const result = nameSchema.safeParse(c.req.query('name')); const name = result.data;
const name = result.success ? result.data : undefined;
const pointer = name ? await localNip05Lookup(name, c.var) : undefined; const pointer = name ? await localNip05Lookup(name, c.var) : undefined;
if (!name || !pointer) { if (!pointer) {
// Not found, cache for 5 minutes. return c.json({ names: {}, relays: {} } satisfies NostrJson, { status: 404 });
c.header('Cache-Control', 'max-age=300, public, stale-while-revalidate=30');
return c.json(emptyResult);
} }
const { pubkey, relays = [] } = pointer; const { pubkey, relays = [] } = pointer;