mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-10 17:18:51 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
27749d91b8 | ||
|
|
9530849f0a | ||
|
|
b8aa75b6e1 | ||
|
|
344762820c | ||
|
|
f43d23d344 |
14
README.md
14
README.md
@@ -140,16 +140,14 @@ let relays = ['wss://relay.example.com', 'wss://relay.example2.com']
|
|||||||
|
|
||||||
let relay = await pool.ensureRelay('wss://relay.example3.com')
|
let relay = await pool.ensureRelay('wss://relay.example3.com')
|
||||||
|
|
||||||
let subs = pool.sub([...relays, relay], {
|
let sub = pool.sub([...relays, relay], [{
|
||||||
authors: ['32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245']
|
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
|
||||||
// this will only be called once the first time the event is received
|
// ...
|
||||||
// ...
|
})
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
let pubs = pool.publish(relays, newEvent)
|
let pubs = pool.publish(relays, newEvent)
|
||||||
pubs.forEach(pub =>
|
pubs.forEach(pub =>
|
||||||
|
|||||||
@@ -56,3 +56,15 @@ test('encode and decode naddr', () => {
|
|||||||
expect(data.kind).toEqual(30023)
|
expect(data.kind).toEqual(30023)
|
||||||
expect(data.identifier).toEqual('banana')
|
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',
|
type: 'nprofile',
|
||||||
data: {
|
data: {
|
||||||
pubkey: secp256k1.utils.bytesToHex(tlv[0][0]),
|
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',
|
type: 'nevent',
|
||||||
data: {
|
data: {
|
||||||
id: secp256k1.utils.bytesToHex(tlv[0][0]),
|
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]),
|
identifier: utf8Decoder.decode(tlv[0][0]),
|
||||||
pubkey: secp256k1.utils.bytesToHex(tlv[2][0]),
|
pubkey: secp256k1.utils.bytesToHex(tlv[2][0]),
|
||||||
kind: parseInt(secp256k1.utils.bytesToHex(tlv[3][0]), 16),
|
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,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "1.6.0",
|
"version": "1.6.1",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
23
pool.ts
23
pool.ts
@@ -13,10 +13,10 @@ export class SimplePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
close(relays: string[]): void {
|
close(relays: string[]): void {
|
||||||
relays.map(url => {
|
relays.forEach(url => {
|
||||||
let relay = this._conn[normalizeURL(url)]
|
let relay = this._conn[normalizeURL(url)]
|
||||||
if (relay) relay.close()
|
if (relay) relay.close()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async ensureRelay(url: string): Promise<Relay> {
|
async ensureRelay(url: string): Promise<Relay> {
|
||||||
@@ -54,7 +54,13 @@ export class SimplePool {
|
|||||||
}, 2400)
|
}, 2400)
|
||||||
|
|
||||||
relays.forEach(async relay => {
|
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
|
if (!r) return
|
||||||
let s = r.sub(filters, modifiedOpts)
|
let s = r.sub(filters, modifiedOpts)
|
||||||
s.on('event', (event: Event) => {
|
s.on('event', (event: Event) => {
|
||||||
@@ -63,14 +69,17 @@ export class SimplePool {
|
|||||||
})
|
})
|
||||||
s.on('eose', () => {
|
s.on('eose', () => {
|
||||||
if (eoseSent) return
|
if (eoseSent) return
|
||||||
|
handleEose()
|
||||||
|
})
|
||||||
|
subs.push(s)
|
||||||
|
|
||||||
|
function handleEose() {
|
||||||
eosesMissing--
|
eosesMissing--
|
||||||
if (eosesMissing === 0) {
|
if (eosesMissing === 0) {
|
||||||
clearTimeout(eoseTimeout)
|
clearTimeout(eoseTimeout)
|
||||||
for (let cb of eoseListeners.values()) cb()
|
for (let cb of eoseListeners.values()) cb()
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
subs.push(s)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
let greaterSub: Sub = {
|
let greaterSub: Sub = {
|
||||||
|
|||||||
13
relay.ts
13
relay.ts
@@ -163,14 +163,20 @@ export function relayInit(url: string): Relay {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function connected() {
|
||||||
|
return ws?.readyState === 1
|
||||||
|
}
|
||||||
|
|
||||||
async function connect(): Promise<void> {
|
async function connect(): Promise<void> {
|
||||||
if (ws?.readyState && ws.readyState === 1) return // ws already open
|
if (connected()) return // ws already open
|
||||||
await connectRelay()
|
await connectRelay()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function trySend(params: [string, ...any]) {
|
async function trySend(params: [string, ...any]) {
|
||||||
let msg = JSON.stringify(params)
|
let msg = JSON.stringify(params)
|
||||||
|
if (!connected()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
ws.send(msg)
|
ws.send(msg)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -294,8 +300,7 @@ export function relayInit(url: string): Relay {
|
|||||||
subListeners = {}
|
subListeners = {}
|
||||||
pubListeners = {}
|
pubListeners = {}
|
||||||
|
|
||||||
if (ws.readyState > 1) return
|
ws?.close()
|
||||||
ws.close()
|
|
||||||
},
|
},
|
||||||
get status() {
|
get status() {
|
||||||
return ws?.readyState ?? 3
|
return ws?.readyState ?? 3
|
||||||
|
|||||||
Reference in New Issue
Block a user