diff --git a/docs/index.md b/docs/index.md index 8603d79..391175d 100644 --- a/docs/index.md +++ b/docs/index.md @@ -33,6 +33,9 @@ features: - title: "@welshman/relay" details: In-memory relay and event store. link: "/relay" + - title: "@welshman/router" + details: Tools for relay selection. + link: "/router" - title: "@welshman/content" details: Parser and renderer for nostr notes with customizable formatting options. link: "/content" diff --git a/docs/router/index.md b/docs/router/index.md new file mode 100644 index 0000000..ef514e9 --- /dev/null +++ b/docs/router/index.md @@ -0,0 +1,48 @@ +# @welshman/router + +[![version](https://badgen.net/npm/v/@welshman/router)](https://npmjs.com/package/@welshman/router) + +Utilities for selecting nostr relays. + +## What's Included + +- **Router** - A configurable router class usable as a singleton which provides common relay selection scenarios. +- **RouterScenario** - A scenario class which scores relays based on policy. +- **getFilterSelections** - A high-level utility for inferring relay selections from fitlers. + +## Quick Example + +```typescript +import {routerContext, addMaximalFallbacks, Router} from '@welshman/router' + +// Configure the global router instance based on RouterOptions +Router.configure({ + defaultRelays: ['wss://relay.example.com/'], + getPubkeyRelays: (pubkey, mode) => ['wss://myrelay.example.com/'], +}) + +// Get the singleton and use it to select some relays +const router = Router.get() + +// Get a hint based on pubkey +router.FromPubkeys(pubkeys).getUrl() + +// Send an event to the author's outbox and mentions' inboxes +router.PublishEvent(event).getUrls() + +// Try as hard as we can to find a quoted note +router + .FromPubkeys(event, quotedEventId, hints) + .allowLocal(true) + .allowOnion(true) + .allowInsecure(true) + .policy(addMaximalFallbacks) + .limit(10) + .getUrls() +``` + +## Installation + +```bash +npm install @welshman/router +```