Add Cache-Control headers to nostr.json responses

This commit is contained in:
Alex Gleason 2025-01-23 13:00:43 -06:00
parent aa1515e7e9
commit b8dbc432ab
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -1,36 +1,50 @@
import { NostrJson } from '@nostrify/nostrify';
import { z } from 'zod'; import { z } from 'zod';
import { AppController } from '@/app.ts'; 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.
if (!Object.entries(c.req.queries()).length) {
c.header('Cache-Control', 'max-age=31536000, public, immutable');
return c.json(emptyResult);
}
const store = c.get('store'); const store = c.get('store');
const result = nameSchema.safeParse(c.req.query('name')); const result = nameSchema.safeParse(c.req.query('name'));
const name = result.success ? result.data : undefined; const name = result.success ? result.data : undefined;
const pointer = name ? await localNip05Lookup(store, name) : undefined; const pointer = name ? await localNip05Lookup(store, name) : undefined;
if (!name || !pointer) { if (!name || !pointer) {
return c.json({ names: {}, relays: {} }); // Not found, cache for 5 minutes.
c.header('Cache-Control', 'max-age=300, public');
return c.json(emptyResult);
} }
const { pubkey, relays } = pointer; const { pubkey, relays = [] } = pointer;
return c.json({ // It's found, so cache for 12 hours.
c.header('Cache-Control', 'max-age=43200, public');
return c.json(
{
names: { names: {
[name]: pubkey, [name]: pubkey,
}, },
relays: { relays: {
[pubkey]: relays, [pubkey]: relays,
}, },
}); } satisfies NostrJson,
);
}; };
export { nostrController }; export { nostrController };