mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c362212778 | ||
|
|
a8938a3a0f | ||
|
|
a21329da3f | ||
|
|
63f4a49a69 | ||
|
|
27749d91b8 | ||
|
|
9530849f0a | ||
|
|
b8aa75b6e1 | ||
|
|
344762820c | ||
|
|
f43d23d344 | ||
|
|
bf55ad6b5a | ||
|
|
04a46b815c | ||
|
|
165ff44dff | ||
|
|
7bfd23af3c | ||
|
|
3d93ec8446 |
12
README.md
12
README.md
@@ -120,7 +120,7 @@ let event = await relay.get({
|
||||
ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
||||
})
|
||||
|
||||
await relay.close()
|
||||
relay.close()
|
||||
```
|
||||
|
||||
To use this on Node.js you first must install `websocket-polyfill` and import it:
|
||||
@@ -140,16 +140,14 @@ let relays = ['wss://relay.example.com', 'wss://relay.example2.com']
|
||||
|
||||
let relay = await pool.ensureRelay('wss://relay.example3.com')
|
||||
|
||||
let subs = pool.sub([...relays, relay], {
|
||||
let sub = pool.sub([...relays, relay], [{
|
||||
authors: ['32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
||||
})
|
||||
}])
|
||||
|
||||
subs.forEach(sub =>
|
||||
sub.on('event', event => {
|
||||
sub.on('event', event => {
|
||||
// this will only be called once the first time the event is received
|
||||
// ...
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
let pubs = pool.publish(relays, newEvent)
|
||||
pubs.forEach(pub =>
|
||||
|
||||
10
build.js
10
build.js
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const fs = require('fs')
|
||||
const esbuild = require('esbuild')
|
||||
|
||||
let common = {
|
||||
@@ -11,11 +12,16 @@ let common = {
|
||||
esbuild
|
||||
.build({
|
||||
...common,
|
||||
outfile: 'lib/nostr.esm.js',
|
||||
outfile: 'lib/esm/nostr.mjs',
|
||||
format: 'esm',
|
||||
packages: 'external'
|
||||
})
|
||||
.then(() => console.log('esm build success.'))
|
||||
.then(() => {
|
||||
const packageJson = JSON.stringify({ type: 'module' })
|
||||
fs.writeFileSync(`${__dirname}/lib/esm/package.json`, packageJson, 'utf8')
|
||||
|
||||
console.log('esm build success.')
|
||||
})
|
||||
|
||||
esbuild
|
||||
.build({
|
||||
|
||||
140
magic.ts
140
magic.ts
@@ -1,140 +0,0 @@
|
||||
import {Relay, relayInit} from './relay'
|
||||
import {Event} from './event'
|
||||
import {normalizeURL} from './utils'
|
||||
|
||||
export default function (
|
||||
writeableRelays: string[],
|
||||
fallbackRelays: string[],
|
||||
safeRelays: string[]
|
||||
) {
|
||||
return new MagicPool(fallbackRelays, writeableRelays, safeRelays)
|
||||
}
|
||||
|
||||
class MagicPool {
|
||||
private _conn: {[url: string]: Relay}
|
||||
private _fallback: {[url: string]: Relay}
|
||||
private _write: {[url: string]: Relay}
|
||||
private _safe: {[url: string]: Relay}
|
||||
|
||||
private _profileRelays: {[pubkey: string]: RelayTableScore}
|
||||
private _tempCache: {[id: string]: Event}
|
||||
|
||||
constructor(
|
||||
fallbackRelays: string[],
|
||||
writeableRelays: string[],
|
||||
safeRelays: string[] = [
|
||||
'wss://eden.nostr.land',
|
||||
'wss://nostr.milou.lol',
|
||||
'wss://relay.minds.com/nostr/v1/ws'
|
||||
]
|
||||
) {
|
||||
this._conn = {}
|
||||
this._write = {}
|
||||
this._fallback = {}
|
||||
this._profileRelays = {}
|
||||
this._tempCache = {}
|
||||
|
||||
const hasEventId = (id: string): boolean => id in this._tempCache
|
||||
const init = (url: string) => {
|
||||
this._conn[normalizeURL(url)] = relayInit(normalizeURL(url), hasEventId)
|
||||
}
|
||||
|
||||
fallbackRelays.forEach(init)
|
||||
writeableRelays.forEach(init)
|
||||
safeRelays.forEach(init)
|
||||
|
||||
this._write = Object.fromEntries(
|
||||
writeableRelays.map(url => [
|
||||
normalizeURL(url),
|
||||
this._conn[normalizeURL(url)]
|
||||
])
|
||||
)
|
||||
this._fallback = Object.fromEntries(
|
||||
fallbackRelays.map(url => [
|
||||
normalizeURL(url),
|
||||
this._conn[normalizeURL(url)]
|
||||
])
|
||||
)
|
||||
this._safe = Object.fromEntries(
|
||||
safeRelays.map(url => [normalizeURL(url), this._conn[normalizeURL(url)]])
|
||||
)
|
||||
}
|
||||
|
||||
publish(event: Event) {
|
||||
return Promise.all(
|
||||
Object.entries(this._write).map(
|
||||
([url, relay]) =>
|
||||
new Promise(async resolve => {
|
||||
await relay.connect()
|
||||
let pub = relay.publish(event)
|
||||
let to = setTimeout(() => {
|
||||
let end = setTimeout(() => {
|
||||
resolve({url, success: false, reason: 'timeout'})
|
||||
}, 2500)
|
||||
pub.on('seen', () => {
|
||||
clearTimeout(end)
|
||||
resolve({url, success: true, reason: 'seen'})
|
||||
})
|
||||
}, 2500)
|
||||
pub.on('ok', () => {
|
||||
clearTimeout(to)
|
||||
resolve({url, success: true, reason: 'ok'})
|
||||
})
|
||||
pub.on('failed', (reason: string) => {
|
||||
clearTimeout(to)
|
||||
resolve({url, success: false, reason})
|
||||
})
|
||||
})
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
profile(
|
||||
pubkey: string,
|
||||
onUpdate: (events: Event[]) => void
|
||||
): {
|
||||
page(n: number): void
|
||||
} {
|
||||
var relays = new Set()
|
||||
let rts = this._profileRelays[pubkey]
|
||||
if (rts) {
|
||||
relays = rts.get(3)
|
||||
}
|
||||
|
||||
let fallback = Object.values(this._fallback)
|
||||
for (let i = 0; i < fallback.length; i++) {
|
||||
if (relays.size < 3) {
|
||||
relays.add(fallback[Math.floor(Math.random() * fallback.length)])
|
||||
} else break
|
||||
}
|
||||
|
||||
// start subscription
|
||||
for (let r in relays) {
|
||||
r.
|
||||
}
|
||||
|
||||
return {
|
||||
page(n: number) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class RelayTableScore {
|
||||
seen: string[] = []
|
||||
hinted: string[] = []
|
||||
explicit: string[] = []
|
||||
|
||||
get(n: number): Set<string> {
|
||||
let relays = new Set<string>()
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = 0; j < 3; j++) {
|
||||
let v = [this.seen, this.explicit, this.hinted][j][i]
|
||||
if (v) {
|
||||
relays.add(v)
|
||||
if (relays.size >= n) return relays
|
||||
}
|
||||
}
|
||||
}
|
||||
return relays
|
||||
}
|
||||
}
|
||||
@@ -56,3 +56,15 @@ test('encode and decode naddr', () => {
|
||||
expect(data.kind).toEqual(30023)
|
||||
expect(data.identifier).toEqual('banana')
|
||||
})
|
||||
|
||||
test('encode and decode naddr from habla.news', () => {
|
||||
let {type, data} = nip19.decode(
|
||||
'naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5'
|
||||
)
|
||||
expect(type).toEqual('naddr')
|
||||
expect(data.pubkey).toEqual(
|
||||
'7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194'
|
||||
)
|
||||
expect(data.kind).toEqual(30023)
|
||||
expect(data.identifier).toEqual('references')
|
||||
})
|
||||
|
||||
6
nip19.ts
6
nip19.ts
@@ -39,7 +39,7 @@ export function decode(nip19: string): {
|
||||
type: 'nprofile',
|
||||
data: {
|
||||
pubkey: secp256k1.utils.bytesToHex(tlv[0][0]),
|
||||
relays: tlv[1].map(d => utf8Decoder.decode(d))
|
||||
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,7 @@ export function decode(nip19: string): {
|
||||
type: 'nevent',
|
||||
data: {
|
||||
id: secp256k1.utils.bytesToHex(tlv[0][0]),
|
||||
relays: tlv[1].map(d => utf8Decoder.decode(d))
|
||||
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,7 +71,7 @@ export function decode(nip19: string): {
|
||||
identifier: utf8Decoder.decode(tlv[0][0]),
|
||||
pubkey: secp256k1.utils.bytesToHex(tlv[2][0]),
|
||||
kind: parseInt(secp256k1.utils.bytesToHex(tlv[3][0]), 16),
|
||||
relays: tlv[1].map(d => utf8Decoder.decode(d))
|
||||
relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
{
|
||||
"name": "nostr-tools",
|
||||
"version": "1.5.0",
|
||||
"version": "1.7.0",
|
||||
"description": "Tools for making a Nostr client.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/fiatjaf/nostr-tools.git"
|
||||
},
|
||||
"main": "lib/nostr.cjs.js",
|
||||
"module": "lib/nostr.esm.js",
|
||||
"module": "lib/esm/nostr.mjs",
|
||||
"exports": {
|
||||
"import": "./lib/esm/nostr.mjs",
|
||||
"require": "./lib/nostr.cjs.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@noble/hashes": "1.0.0",
|
||||
"@noble/secp256k1": "^1.7.1",
|
||||
|
||||
@@ -19,8 +19,8 @@ let relays = [
|
||||
'wss://nostr.zebedee.cloud/'
|
||||
]
|
||||
|
||||
afterAll(async () => {
|
||||
await pool.close([
|
||||
afterAll(() => {
|
||||
pool.close([
|
||||
...relays,
|
||||
'wss://nostr-relay.untethr.me',
|
||||
'wss://offchain.pub',
|
||||
|
||||
52
pool.ts
52
pool.ts
@@ -8,17 +8,20 @@ export class SimplePool {
|
||||
private _conn: {[url: string]: Relay}
|
||||
private _seenOn: {[id: string]: Set<string>} = {} // a map of all events we've seen in each relay
|
||||
|
||||
constructor() {
|
||||
private eoseSubTimeout: number
|
||||
private getTimeout: number
|
||||
|
||||
constructor(options: {eoseSubTimeout?: number; getTimeout?: number} = {}) {
|
||||
this._conn = {}
|
||||
this.eoseSubTimeout = options.eoseSubTimeout || 3400
|
||||
this.getTimeout = options.getTimeout || 3400
|
||||
}
|
||||
|
||||
async close(relays: string[]): Promise<void> {
|
||||
await Promise.all(
|
||||
relays.map(async url => {
|
||||
close(relays: string[]): void {
|
||||
relays.forEach(url => {
|
||||
let relay = this._conn[normalizeURL(url)]
|
||||
if (relay) await relay.close()
|
||||
if (relay) relay.close()
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
async ensureRelay(url: string): Promise<Relay> {
|
||||
@@ -53,10 +56,16 @@ export class SimplePool {
|
||||
let eoseTimeout = setTimeout(() => {
|
||||
eoseSent = true
|
||||
for (let cb of eoseListeners.values()) cb()
|
||||
}, 2400)
|
||||
}, this.eoseSubTimeout)
|
||||
|
||||
relays.forEach(async relay => {
|
||||
let r = await this.ensureRelay(relay)
|
||||
let r
|
||||
try {
|
||||
r = await this.ensureRelay(relay)
|
||||
} catch (err) {
|
||||
handleEose()
|
||||
return
|
||||
}
|
||||
if (!r) return
|
||||
let s = r.sub(filters, modifiedOpts)
|
||||
s.on('event', (event: Event) => {
|
||||
@@ -65,14 +74,17 @@ export class SimplePool {
|
||||
})
|
||||
s.on('eose', () => {
|
||||
if (eoseSent) return
|
||||
handleEose()
|
||||
})
|
||||
subs.push(s)
|
||||
|
||||
function handleEose() {
|
||||
eosesMissing--
|
||||
if (eosesMissing === 0) {
|
||||
clearTimeout(eoseTimeout)
|
||||
for (let cb of eoseListeners.values()) cb()
|
||||
}
|
||||
})
|
||||
subs.push(s)
|
||||
}
|
||||
})
|
||||
|
||||
let greaterSub: Sub = {
|
||||
@@ -113,7 +125,7 @@ export class SimplePool {
|
||||
let timeout = setTimeout(() => {
|
||||
sub.unsub()
|
||||
resolve(null)
|
||||
}, 1500)
|
||||
}, this.getTimeout)
|
||||
sub.on('event', (event: Event) => {
|
||||
resolve(event)
|
||||
clearTimeout(timeout)
|
||||
@@ -143,13 +155,23 @@ export class SimplePool {
|
||||
})
|
||||
}
|
||||
|
||||
publish(relays: string[], event: Event): Pub[] {
|
||||
return relays.map(relay => {
|
||||
publish(relays: string[], event: Event): Pub {
|
||||
let pubs = relays.map(relay => {
|
||||
let r = this._conn[normalizeURL(relay)]
|
||||
if (!r) return badPub(relay)
|
||||
let s = r.publish(event)
|
||||
return s
|
||||
return r.publish(event)
|
||||
})
|
||||
|
||||
return {
|
||||
on(type, cb) {
|
||||
pubs.forEach((pub, i) => {
|
||||
pub.on(type, () => cb(relays[i]))
|
||||
})
|
||||
},
|
||||
off() {
|
||||
// do nothing here, FIXME
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
seenOn(id: string): string[] {
|
||||
|
||||
@@ -15,8 +15,8 @@ beforeAll(() => {
|
||||
relay.connect()
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await relay.close()
|
||||
afterAll(() => {
|
||||
relay.close()
|
||||
})
|
||||
|
||||
test('connectivity', () => {
|
||||
|
||||
25
relay.ts
25
relay.ts
@@ -10,7 +10,7 @@ export type Relay = {
|
||||
url: string
|
||||
status: number
|
||||
connect: () => Promise<void>
|
||||
close: () => Promise<void>
|
||||
close: () => void
|
||||
sub: (filters: Filter[], opts?: SubscriptionOptions) => Sub
|
||||
list: (filters: Filter[], opts?: SubscriptionOptions) => Promise<Event[]>
|
||||
get: (filter: Filter, opts?: SubscriptionOptions) => Promise<Event | null>
|
||||
@@ -37,7 +37,6 @@ export type SubscriptionOptions = {
|
||||
|
||||
export function relayInit(url: string): Relay {
|
||||
var ws: WebSocket
|
||||
var resolveClose: () => void
|
||||
var openSubs: {[id: string]: {filters: Filter[]} & SubscriptionOptions} = {}
|
||||
var listeners: {
|
||||
connect: Array<() => void>
|
||||
@@ -78,7 +77,6 @@ export function relayInit(url: string): Relay {
|
||||
}
|
||||
ws.onclose = async () => {
|
||||
listeners.disconnect.forEach(cb => cb())
|
||||
resolveClose && resolveClose()
|
||||
}
|
||||
|
||||
let incomingMessageQueue: string[] = []
|
||||
@@ -165,14 +163,23 @@ export function relayInit(url: string): Relay {
|
||||
})
|
||||
}
|
||||
|
||||
function connected() {
|
||||
return ws?.readyState === 1
|
||||
}
|
||||
|
||||
async function connect(): Promise<void> {
|
||||
if (ws?.readyState && ws.readyState === 1) return // ws already open
|
||||
if (connected()) return // ws already open
|
||||
await connectRelay()
|
||||
}
|
||||
|
||||
async function trySend(params: [string, ...any]) {
|
||||
let msg = JSON.stringify(params)
|
||||
|
||||
if (!connected()) {
|
||||
await new Promise(resolve => setTimeout(resolve, 1000))
|
||||
if (!connected()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
try {
|
||||
ws.send(msg)
|
||||
} catch (err) {
|
||||
@@ -291,16 +298,12 @@ export function relayInit(url: string): Relay {
|
||||
}
|
||||
},
|
||||
connect,
|
||||
close(): Promise<void> {
|
||||
close(): void {
|
||||
listeners = {connect: [], disconnect: [], error: [], notice: []}
|
||||
subListeners = {}
|
||||
pubListeners = {}
|
||||
|
||||
if (ws.readyState > 1) return Promise.resolve()
|
||||
ws.close()
|
||||
return new Promise<void>(resolve => {
|
||||
resolveClose = resolve
|
||||
})
|
||||
ws?.close()
|
||||
},
|
||||
get status() {
|
||||
return ws?.readyState ?? 3
|
||||
|
||||
Reference in New Issue
Block a user