mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-10 09:08:50 +00:00
most simple relay pool.
This commit is contained in:
18
README.md
18
README.md
@@ -120,6 +120,22 @@ To use this on Node.js you first must install `websocket-polyfill` and import it
|
|||||||
import 'websocket-polyfill'
|
import 'websocket-polyfill'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Interacting with multiple relays
|
||||||
|
|
||||||
|
```js
|
||||||
|
import {pool} from 'nostr-tools'
|
||||||
|
|
||||||
|
const p = pool()
|
||||||
|
|
||||||
|
["wss://relay.example.com", "wss://relay.example2.com"].forEach(async url => {
|
||||||
|
let relay = pool.ensureRelay(url)
|
||||||
|
await relay.connect()
|
||||||
|
|
||||||
|
relay.sub(...) // same as above
|
||||||
|
relay.publish(...) // etc
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
### Querying profile data from a NIP-05 address
|
### Querying profile data from a NIP-05 address
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@@ -195,7 +211,7 @@ let event = {
|
|||||||
sendEvent(event)
|
sendEvent(event)
|
||||||
|
|
||||||
// on the receiver side
|
// on the receiver side
|
||||||
sub.on('event', (event) => {
|
sub.on('event', event => {
|
||||||
let sender = event.tags.find(([k, v]) => k === 'p' && v && v !== '')[1]
|
let sender = event.tags.find(([k, v]) => k === 'p' && v && v !== '')[1]
|
||||||
pk1 === sender
|
pk1 === sender
|
||||||
let plaintext = await nip04.decrypt(sk2, pk1, event.content)
|
let plaintext = await nip04.decrypt(sk2, pk1, event.content)
|
||||||
|
|||||||
1
index.ts
1
index.ts
@@ -2,6 +2,7 @@ export * from './keys'
|
|||||||
export * from './relay'
|
export * from './relay'
|
||||||
export * from './event'
|
export * from './event'
|
||||||
export * from './filter'
|
export * from './filter'
|
||||||
|
export * from './pool'
|
||||||
|
|
||||||
export * as nip04 from './nip04'
|
export * as nip04 from './nip04'
|
||||||
export * as nip05 from './nip05'
|
export * as nip05 from './nip05'
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "1.2.1",
|
"version": "1.2.3",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
27
pool.ts
Normal file
27
pool.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import {Relay, relayInit} from './relay'
|
||||||
|
import {normalizeURL} from './utils'
|
||||||
|
|
||||||
|
export function pool(defaultRelays: string[] = []) {
|
||||||
|
return new SimplePool(defaultRelays)
|
||||||
|
}
|
||||||
|
|
||||||
|
class SimplePool {
|
||||||
|
private _conn: {[url: string]: Relay}
|
||||||
|
private _knownIds: Set<string> = new Set()
|
||||||
|
|
||||||
|
constructor(defaultRelays: string[]) {
|
||||||
|
this._conn = {}
|
||||||
|
defaultRelays.forEach(this.ensureRelay)
|
||||||
|
}
|
||||||
|
|
||||||
|
ensureRelay(url: string): Relay {
|
||||||
|
const nm = normalizeURL(url)
|
||||||
|
const existing = this._conn[nm]
|
||||||
|
if (existing) return existing
|
||||||
|
|
||||||
|
const hasEventId = (id: string): boolean => this._knownIds.has(id)
|
||||||
|
const relay = relayInit(nm, hasEventId)
|
||||||
|
this._conn[nm] = relay
|
||||||
|
return relay
|
||||||
|
}
|
||||||
|
}
|
||||||
14
utils.ts
14
utils.ts
@@ -3,6 +3,20 @@ import {Event} from './event'
|
|||||||
export const utf8Decoder = new TextDecoder('utf-8')
|
export const utf8Decoder = new TextDecoder('utf-8')
|
||||||
export const utf8Encoder = new TextEncoder()
|
export const utf8Encoder = new TextEncoder()
|
||||||
|
|
||||||
|
export function normalizeURL(url: string): string {
|
||||||
|
let p = new URL(url)
|
||||||
|
p.pathname = p.pathname.replace(/\/+/g, '/')
|
||||||
|
if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1)
|
||||||
|
if (
|
||||||
|
(p.port === '80' && p.protocol === 'ws:') ||
|
||||||
|
(p.port === '443' && p.protocol === 'wss:')
|
||||||
|
)
|
||||||
|
p.port = ''
|
||||||
|
p.searchParams.sort()
|
||||||
|
p.hash = ''
|
||||||
|
return p.toString()
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// fast insert-into-sorted-array functions adapted from https://github.com/terrymorse58/fast-sorted-array
|
// fast insert-into-sorted-array functions adapted from https://github.com/terrymorse58/fast-sorted-array
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in New Issue
Block a user