@snort/system
A collection of caching and querying techniquies used by https://snort.social to serve all content from the nostr protocol.
Simple example:
import {
NostrSystem,
EventPublisher,
UserRelaysCache,
RequestBuilder,
FlatNoteStore,
StoreSnapshot
} from "@snort/system"
const RelaysCache = new UserRelaysCache();
const AuthHandler = async (challenge: string, relay: string) => {
const pub = await EventPublisher.nip7();
if (pub) {
return await pub.nip42Auth(challenge, relay);
}
}
const System = new NostrSystem({
relayCache: RelaysCache,
authHandler: AuthHandler
});
(async () => {
await System.ConnectToRelay("wss://relay.snort.social", { read: true, write: false });
const rb = new RequestBuilder("get-posts");
rb.withFilter()
.authors(["63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed"])
.kinds([1])
.limit(10);
const q = System.Query<FlatNoteStore>(FlatNoteStore, rb);
q.onEvent = (sub, e) => {
console.debug(sub, e);
}
const release = q.feed.hook(() => {
const state = q.feed.snapshot as StoreSnapshot<ReturnType<FlatNoteStore["getSnapshotData"]>>;
console.log(`We have ${state.data.length} events now!`)
});
})();