Add nostr schema for parsing filters

This commit is contained in:
Alex Gleason 2023-08-11 19:55:16 -05:00
parent 6516997353
commit 1f470ffe2d
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1,15 @@
import type { AppController } from '@/app.ts';
const relayController: AppController = (c) => {
const upgrade = c.req.headers.get('upgrade');
if (upgrade?.toLowerCase() !== 'websocket') {
return c.text('Please use a Nostr client to connect.', 400);
}
const { socket, response } = Deno.upgradeWebSocket(c.req.raw);
return response;
};
export { relayController };

25
src/schemas/nostr.ts Normal file
View File

@ -0,0 +1,25 @@
import { z } from '@/deps.ts';
import { hexIdSchema, signedEventSchema } from '../schema.ts';
const filterSchema = z.object({
kinds: z.number().int().positive().array().optional(),
ids: hexIdSchema.array().optional(),
authors: hexIdSchema.array().optional(),
since: z.number().int().positive().optional(),
until: z.number().int().positive().optional(),
limit: z.number().int().positive().optional(),
}).and(z.record(
z.custom<`#${string}`>((val) => typeof val === 'string' && val.startsWith('#')),
z.string().array(),
));
const clientMsgSchema = z.union([
z.tuple([z.literal('REQ'), z.string().min(1)]).rest(filterSchema),
z.tuple([z.literal('EVENT'), signedEventSchema]),
z.tuple([z.literal('CLOSE'), z.string().min(1)]),
]);
type Filter = z.infer<typeof filterSchema>;
export { clientMsgSchema, filterSchema };