mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Add Cache-Control headers to nostr.json responses
This commit is contained in:
parent
aa1515e7e9
commit
b8dbc432ab
1 changed files with 26 additions and 12 deletions
|
|
@ -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.
|
||||||
names: {
|
c.header('Cache-Control', 'max-age=43200, public');
|
||||||
[name]: pubkey,
|
|
||||||
},
|
return c.json(
|
||||||
relays: {
|
{
|
||||||
[pubkey]: relays,
|
names: {
|
||||||
},
|
[name]: pubkey,
|
||||||
});
|
},
|
||||||
|
relays: {
|
||||||
|
[pubkey]: relays,
|
||||||
|
},
|
||||||
|
} satisfies NostrJson,
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export { nostrController };
|
export { nostrController };
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue