Scaffold db, handler, query

This commit is contained in:
Alex Gleason 2023-04-07 21:37:16 -05:00
parent ec12da2710
commit c3b8ad1c28
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
6 changed files with 76 additions and 5 deletions

7
src/db.ts Normal file
View File

@ -0,0 +1,7 @@
import { lmdb } from '@/deps.ts';
const db = lmdb.open('db', {});
const gossipDB = db.openDB('gossip', { dupSort: true });
export { db, gossipDB };

View File

@ -1,7 +1,6 @@
import { Context, Hono, validator } from 'https://deno.land/x/hono@v3.0.2/mod.ts';
export { Hono, validator };
export { type Context, Hono, validator } from 'https://deno.land/x/hono@v3.0.2/mod.ts';
export { cors } from 'https://deno.land/x/hono@v3.0.2/middleware.ts';
export { z } from 'https://deno.land/x/zod@v3.20.5/mod.ts';
export { Author, RelayPool } from 'https://dev.jspm.io/nostr-relaypool@0.5.3';
export { getEventHash, getPublicKey, nip19, signEvent } from 'npm:nostr-tools@^1.7.4';
export type { Context };
export { type Filter, getEventHash, getPublicKey, nip19, signEvent } from 'npm:nostr-tools@^1.7.4';
export { default as lmdb } from 'npm:lmdb';

21
src/gossip.ts Normal file
View File

@ -0,0 +1,21 @@
import { parseRelay } from './schema.ts';
import type { Event } from './event.ts';
/** Gets relays which pertain to the author from the event. */
function getAuthorRelays(event: Event): URL[] {
const relays: string[] = [];
switch (event.kind) {
case 10002:
event.tags.forEach((tag) => tag[0] === 'r' && relays.push(tag[1]));
break;
case 2:
relays.push(event.content);
break;
}
return relays.map(parseRelay).filter((r): r is URL => !!r);
}
export { getAuthorRelays };

19
src/handler.ts Normal file
View File

@ -0,0 +1,19 @@
import { gossipDB } from '@/db.ts';
import { getAuthorRelays } from './gossip.ts';
import type { Event } from './event.ts';
function handleEvent(event: Event): void {
handleRelays(event);
}
/** Add author relays into the database. */
function handleRelays(event: Event): Promise<boolean[]> {
const relays = getAuthorRelays(event);
return Promise.all(
relays.map((relay) => gossipDB.put(event.pubkey, relay.toString())),
);
}
export { handleEvent };

7
src/query.ts Normal file
View File

@ -0,0 +1,7 @@
import type { Filter } from '@/deps.ts';
function query(_filter: Filter, _relays?: URL[]) {
// TODO
}
export { query };

View File

@ -22,5 +22,23 @@ const metaContentSchema = z.object({
type MetaContent = z.infer<typeof metaContentSchema>;
export { jsonSchema, metaContentSchema };
/** It's like `safeParse` except it returns `T` on success and `undefined` on fail. */
function parseish<T>(schema: z.ZodType<T>, value: unknown): T | undefined {
const result = schema.safeParse(value);
return result.success ? result.data : undefined;
}
const parseRelay = (relay: string | URL) => parseish(relaySchema, relay);
const relaySchema = z.custom<URL>((relay) => {
if (typeof relay !== 'string') return false;
try {
const { protocol } = new URL(relay);
return protocol === 'wss:' || protocol === 'ws:';
} catch (_e) {
return false;
}
});
export { jsonSchema, metaContentSchema, parseRelay, relaySchema };
export type { MetaContent };