Relay: limit to 100 events per filter

This commit is contained in:
Alex Gleason 2023-08-12 14:01:30 -05:00
parent b852111ec5
commit 3593d5420d
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 12 additions and 2 deletions

View File

@ -5,12 +5,15 @@ import { clientMsgSchema, type ClientREQ } from '@/schemas/nostr.ts';
import type { AppController } from '@/app.ts';
/** Limit of events returned per-filter. */
const FILTER_LIMIT = 100;
function connectStream(socket: WebSocket) {
socket.onmessage = (e) => {
const result = jsonSchema.pipe(clientMsgSchema).safeParse(e.data);
if (!result.success) {
socket.send(JSON.stringify(['NOTICE', JSON.stringify(result.error.message)]));
socket.send(JSON.stringify(['NOTICE', 'Invalid message.']));
return;
}
@ -27,13 +30,20 @@ function connectStream(socket: WebSocket) {
};
async function handleReq([_, sub, ...filters]: ClientREQ) {
for (const event of await getFilters(filters as Filter[])) {
for (const event of await getFilters(prepareFilters(filters))) {
socket.send(JSON.stringify(['EVENT', sub, event]));
}
socket.send(JSON.stringify(['EOSE', sub]));
}
}
function prepareFilters(filters: ClientREQ[2][]): Filter[] {
return filters.map((filter) => ({
...filter,
limit: Math.min(filter.limit || FILTER_LIMIT, FILTER_LIMIT),
}));
}
const relayController: AppController = (c) => {
const upgrade = c.req.headers.get('upgrade');