Relay: limit to 100 events per filter
This commit is contained in:
parent
b852111ec5
commit
3593d5420d
|
@ -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');
|
||||
|
||||
|
|
Loading…
Reference in New Issue