nostr-tools
Advanced tools
Comparing version 1.6.0 to 1.6.1
@@ -425,4 +425,7 @@ "use strict"; | ||
} | ||
function connected() { | ||
return ws?.readyState === 1; | ||
} | ||
async function connect() { | ||
if (ws?.readyState && ws.readyState === 1) | ||
if (connected()) | ||
return; | ||
@@ -433,2 +436,5 @@ await connectRelay(); | ||
let msg = JSON.stringify(params); | ||
if (!connected()) { | ||
return; | ||
} | ||
try { | ||
@@ -549,5 +555,3 @@ ws.send(msg); | ||
pubListeners = {}; | ||
if (ws.readyState > 1) | ||
return; | ||
ws.close(); | ||
ws?.close(); | ||
}, | ||
@@ -568,3 +572,3 @@ get status() { | ||
close(relays) { | ||
relays.map((url) => { | ||
relays.forEach((url) => { | ||
let relay = this._conn[normalizeURL(url)]; | ||
@@ -605,3 +609,9 @@ if (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) | ||
@@ -618,2 +628,6 @@ return; | ||
return; | ||
handleEose(); | ||
}); | ||
subs.push(s); | ||
function handleEose() { | ||
eosesMissing--; | ||
@@ -625,4 +639,3 @@ if (eosesMissing === 0) { | ||
} | ||
}); | ||
subs.push(s); | ||
} | ||
}); | ||
@@ -857,3 +870,3 @@ let greaterSub = { | ||
pubkey: secp256k15.utils.bytesToHex(tlv[0][0]), | ||
relays: tlv[1].map((d) => utf8Decoder.decode(d)) | ||
relays: tlv[1] ? tlv[1].map((d) => utf8Decoder.decode(d)) : [] | ||
} | ||
@@ -872,3 +885,3 @@ }; | ||
id: secp256k15.utils.bytesToHex(tlv[0][0]), | ||
relays: tlv[1].map((d) => utf8Decoder.decode(d)) | ||
relays: tlv[1] ? tlv[1].map((d) => utf8Decoder.decode(d)) : [] | ||
} | ||
@@ -895,3 +908,3 @@ }; | ||
kind: parseInt(secp256k15.utils.bytesToHex(tlv[3][0]), 16), | ||
relays: tlv[1].map((d) => utf8Decoder.decode(d)) | ||
relays: tlv[1] ? tlv[1].map((d) => utf8Decoder.decode(d)) : [] | ||
} | ||
@@ -898,0 +911,0 @@ }; |
@@ -59,1 +59,13 @@ /* eslint-env jest */ | ||
}) | ||
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') | ||
}) |
@@ -42,3 +42,3 @@ import * as secp256k1 from '@noble/secp256k1' | ||
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)) : [] | ||
} | ||
@@ -56,3 +56,3 @@ } | ||
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)) : [] | ||
} | ||
@@ -76,3 +76,3 @@ } | ||
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)) : [] | ||
} | ||
@@ -79,0 +79,0 @@ } |
{ | ||
"name": "nostr-tools", | ||
"version": "1.6.0", | ||
"version": "1.6.1", | ||
"description": "Tools for making a Nostr client.", | ||
@@ -5,0 +5,0 @@ "repository": { |
23
pool.ts
@@ -16,6 +16,6 @@ import {Relay, relayInit} from './relay' | ||
close(relays: string[]): void { | ||
relays.map(url => { | ||
let relay = this._conn[normalizeURL(url)] | ||
if (relay) relay.close() | ||
}) | ||
relays.forEach(url => { | ||
let relay = this._conn[normalizeURL(url)] | ||
if (relay) relay.close() | ||
}) | ||
} | ||
@@ -58,3 +58,9 @@ | ||
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 | ||
@@ -68,3 +74,7 @@ let s = r.sub(filters, modifiedOpts) | ||
if (eoseSent) return | ||
handleEose() | ||
}) | ||
subs.push(s) | ||
function handleEose() { | ||
eosesMissing-- | ||
@@ -75,4 +85,3 @@ if (eosesMissing === 0) { | ||
} | ||
}) | ||
subs.push(s) | ||
} | ||
}) | ||
@@ -79,0 +88,0 @@ |
@@ -143,13 +143,11 @@ # nostr-tools | ||
let subs = pool.sub([...relays, relay], { | ||
let sub = pool.sub([...relays, relay], [{ | ||
authors: ['32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'] | ||
}]) | ||
sub.on('event', event => { | ||
// this will only be called once the first time the event is received | ||
// ... | ||
}) | ||
subs.forEach(sub => | ||
sub.on('event', event => { | ||
// this will only be called once the first time the event is received | ||
// ... | ||
}) | ||
) | ||
let pubs = pool.publish(relays, newEvent) | ||
@@ -156,0 +154,0 @@ pubs.forEach(pub => |
13
relay.ts
@@ -166,4 +166,8 @@ /* global WebSocket */ | ||
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() | ||
@@ -174,3 +178,5 @@ } | ||
let msg = JSON.stringify(params) | ||
if (!connected()) { | ||
return | ||
} | ||
try { | ||
@@ -299,4 +305,3 @@ ws.send(msg) | ||
if (ws.readyState > 1) return | ||
ws.close() | ||
ws?.close() | ||
}, | ||
@@ -303,0 +308,0 @@ get status() { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2094204
14273
306