Add a RelayError class, improve relay error handling
This commit is contained in:
parent
dc49c305bd
commit
0f70f5c76f
|
@ -15,12 +15,14 @@ import type { Event, Filter } from '@/deps.ts';
|
||||||
/** Limit of events returned per-filter. */
|
/** Limit of events returned per-filter. */
|
||||||
const FILTER_LIMIT = 100;
|
const FILTER_LIMIT = 100;
|
||||||
|
|
||||||
|
/** NIP-01 relay to client message. */
|
||||||
type RelayMsg =
|
type RelayMsg =
|
||||||
| ['EVENT', string, Event]
|
| ['EVENT', string, Event]
|
||||||
| ['NOTICE', string]
|
| ['NOTICE', string]
|
||||||
| ['EOSE', string]
|
| ['EOSE', string]
|
||||||
| ['OK', string, boolean, string];
|
| ['OK', string, boolean, string];
|
||||||
|
|
||||||
|
/** Set up the Websocket connection. */
|
||||||
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);
|
||||||
|
@ -31,6 +33,7 @@ function connectStream(socket: WebSocket) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Handle client message. */
|
||||||
function handleMsg(msg: ClientMsg) {
|
function handleMsg(msg: ClientMsg) {
|
||||||
switch (msg[0]) {
|
switch (msg[0]) {
|
||||||
case 'REQ':
|
case 'REQ':
|
||||||
|
@ -45,6 +48,7 @@ function connectStream(socket: WebSocket) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Handle REQ. Start a subscription. */
|
||||||
async function handleReq([_, sub, ...filters]: ClientREQ) {
|
async function handleReq([_, sub, ...filters]: ClientREQ) {
|
||||||
for (const event of await eventsDB.getFilters(prepareFilters(filters))) {
|
for (const event of await eventsDB.getFilters(prepareFilters(filters))) {
|
||||||
send(['EVENT', sub, event]);
|
send(['EVENT', sub, event]);
|
||||||
|
@ -52,20 +56,28 @@ function connectStream(socket: WebSocket) {
|
||||||
send(['EOSE', sub]);
|
send(['EOSE', sub]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Handle EVENT. Store the event. */
|
||||||
async function handleEvent([_, event]: ClientEVENT) {
|
async function handleEvent([_, event]: ClientEVENT) {
|
||||||
try {
|
try {
|
||||||
|
// This will store it (if eligible) and run other side-effects.
|
||||||
await pipeline.handleEvent(event);
|
await pipeline.handleEvent(event);
|
||||||
send(['OK', event.id, true, '']);
|
send(['OK', event.id, true, '']);
|
||||||
} catch (_e) {
|
} catch (e) {
|
||||||
send(['OK', event.id, false, 'blocked: only registered users can post']);
|
if (e instanceof pipeline.RelayError) {
|
||||||
|
send(['OK', event.id, false, e.message]);
|
||||||
|
} else {
|
||||||
|
send(['OK', event.id, false, 'error: something went wrong']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Handle CLOSE. Close the subscription. */
|
||||||
function handleClose([_, _sub]: ClientCLOSE) {
|
function handleClose([_, _sub]: ClientCLOSE) {
|
||||||
// TODO: ???
|
// TODO: ???
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Send a message back to the client. */
|
||||||
function send(msg: RelayMsg) {
|
function send(msg: RelayMsg) {
|
||||||
return socket.send(JSON.stringify(msg));
|
return socket.send(JSON.stringify(msg));
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,9 +12,9 @@ import { isRelay, nostrDate } from '@/utils.ts';
|
||||||
*/
|
*/
|
||||||
async function handleEvent(event: Event): Promise<void> {
|
async function handleEvent(event: Event): Promise<void> {
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
trackHashtags(event),
|
|
||||||
storeEvent(event),
|
storeEvent(event),
|
||||||
trackRelays(event),
|
trackRelays(event),
|
||||||
|
trackHashtags(event),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,8 @@ async function handleEvent(event: Event): Promise<void> {
|
||||||
async function storeEvent(event: Event): Promise<void> {
|
async function storeEvent(event: Event): Promise<void> {
|
||||||
if (await findUser({ pubkey: event.pubkey }) || await isLocallyFollowed(event.pubkey)) {
|
if (await findUser({ pubkey: event.pubkey }) || await isLocallyFollowed(event.pubkey)) {
|
||||||
await eventsDB.insertEvent(event).catch(console.warn);
|
await eventsDB.insertEvent(event).catch(console.warn);
|
||||||
|
} else {
|
||||||
|
return Promise.reject(new RelayError('blocked', 'only registered users can post'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,4 +63,11 @@ function trackRelays(event: Event) {
|
||||||
return addRelays([...relays]);
|
return addRelays([...relays]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export { handleEvent };
|
/** NIP-20 command line result. */
|
||||||
|
class RelayError extends Error {
|
||||||
|
constructor(prefix: 'duplicate' | 'pow' | 'blocked' | 'rate-limited' | 'invalid' | 'error', message: string) {
|
||||||
|
super(`${prefix}: ${message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { handleEvent, RelayError };
|
||||||
|
|
Loading…
Reference in New Issue