mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Relay: limit to 100 events per filter
This commit is contained in:
parent
b852111ec5
commit
3593d5420d
1 changed files with 12 additions and 2 deletions
|
|
@ -5,12 +5,15 @@ import { clientMsgSchema, type ClientREQ } from '@/schemas/nostr.ts';
|
||||||
|
|
||||||
import type { AppController } from '@/app.ts';
|
import type { AppController } from '@/app.ts';
|
||||||
|
|
||||||
|
/** Limit of events returned per-filter. */
|
||||||
|
const FILTER_LIMIT = 100;
|
||||||
|
|
||||||
function connectStream(socket: WebSocket) {
|
function connectStream(socket: WebSocket) {
|
||||||
socket.onmessage = (e) => {
|
socket.onmessage = (e) => {
|
||||||
const result = jsonSchema.pipe(clientMsgSchema).safeParse(e.data);
|
const result = jsonSchema.pipe(clientMsgSchema).safeParse(e.data);
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
socket.send(JSON.stringify(['NOTICE', JSON.stringify(result.error.message)]));
|
socket.send(JSON.stringify(['NOTICE', 'Invalid message.']));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -27,13 +30,20 @@ function connectStream(socket: WebSocket) {
|
||||||
};
|
};
|
||||||
|
|
||||||
async function handleReq([_, sub, ...filters]: ClientREQ) {
|
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(['EVENT', sub, event]));
|
||||||
}
|
}
|
||||||
socket.send(JSON.stringify(['EOSE', sub]));
|
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 relayController: AppController = (c) => {
|
||||||
const upgrade = c.req.headers.get('upgrade');
|
const upgrade = c.req.headers.get('upgrade');
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue