From b8dbc432abd3beba92629f0bcb3e638bae92ab3d Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 23 Jan 2025 13:00:43 -0600 Subject: [PATCH] Add Cache-Control headers to nostr.json responses --- src/controllers/well-known/nostr.ts | 38 ++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/controllers/well-known/nostr.ts b/src/controllers/well-known/nostr.ts index b6b7af09..5a2017bb 100644 --- a/src/controllers/well-known/nostr.ts +++ b/src/controllers/well-known/nostr.ts @@ -1,36 +1,50 @@ +import { NostrJson } from '@nostrify/nostrify'; import { z } from 'zod'; import { AppController } from '@/app.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. * https://github.com/nostr-protocol/nips/blob/master/05.md */ 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 result = nameSchema.safeParse(c.req.query('name')); const name = result.success ? result.data : undefined; - const pointer = name ? await localNip05Lookup(store, name) : undefined; 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({ - names: { - [name]: pubkey, - }, - relays: { - [pubkey]: relays, - }, - }); + // It's found, so cache for 12 hours. + c.header('Cache-Control', 'max-age=43200, public'); + + return c.json( + { + names: { + [name]: pubkey, + }, + relays: { + [pubkey]: relays, + }, + } satisfies NostrJson, + ); }; export { nostrController };