mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc8e34163d | ||
|
|
9082953ede | ||
|
|
61f397463d |
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'
|
||||
```
|
||||
|
||||
### 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
|
||||
|
||||
```js
|
||||
@@ -195,7 +211,7 @@ let event = {
|
||||
sendEvent(event)
|
||||
|
||||
// on the receiver side
|
||||
sub.on('event', (event) => {
|
||||
sub.on('event', event => {
|
||||
let sender = event.tags.find(([k, v]) => k === 'p' && v && v !== '')[1]
|
||||
pk1 === sender
|
||||
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 './event'
|
||||
export * from './filter'
|
||||
export * from './pool'
|
||||
|
||||
export * as nip04 from './nip04'
|
||||
export * as nip05 from './nip05'
|
||||
|
||||
2
nip05.ts
2
nip05.ts
@@ -36,7 +36,7 @@ export async function queryProfile(
|
||||
name = '_'
|
||||
}
|
||||
|
||||
if (!name.match(/^[a-z0-9-_]+$/)) return null
|
||||
if (!name.match(/^[A-Za-z0-9-_]+$/)) return null
|
||||
|
||||
let res = await (
|
||||
await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "nostr-tools",
|
||||
"version": "1.2.1",
|
||||
"version": "1.2.3",
|
||||
"description": "Tools for making a Nostr client.",
|
||||
"repository": {
|
||||
"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
|
||||
}
|
||||
}
|
||||
8
relay.ts
8
relay.ts
@@ -46,7 +46,7 @@ export function relayInit(
|
||||
var listeners: {
|
||||
connect: Array<() => void>
|
||||
disconnect: Array<() => void>
|
||||
error: Array<() => void>
|
||||
error: Array<(e: globalThis.Event) => void>
|
||||
notice: Array<(msg: string) => void>
|
||||
} = {
|
||||
connect: [],
|
||||
@@ -77,9 +77,9 @@ export function relayInit(
|
||||
setOpen()
|
||||
resolve()
|
||||
}
|
||||
ws.onerror = () => {
|
||||
listeners.error.forEach(cb => cb())
|
||||
reject()
|
||||
ws.onerror = (e: globalThis.Event) => {
|
||||
listeners.error.forEach(cb => cb(e))
|
||||
reject(e)
|
||||
}
|
||||
ws.onclose = async () => {
|
||||
listeners.disconnect.forEach(cb => cb())
|
||||
|
||||
14
utils.ts
14
utils.ts
@@ -3,6 +3,20 @@ import {Event} from './event'
|
||||
export const utf8Decoder = new TextDecoder('utf-8')
|
||||
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
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user