mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
fix(relay): prevent accesing to ws if it is undefined
This commit is contained in:
committed by
fiatjaf_
parent
bf55ad6b5a
commit
f43d23d344
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> {
|
||||
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()) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
ws.send(msg)
|
||||
} catch (err) {
|
||||
@@ -294,8 +300,7 @@ export function relayInit(url: string): Relay {
|
||||
subListeners = {}
|
||||
pubListeners = {}
|
||||
|
||||
if (ws.readyState > 1) return
|
||||
ws.close()
|
||||
ws?.close()
|
||||
},
|
||||
get status() {
|
||||
return ws?.readyState ?? 3
|
||||
|
||||
Reference in New Issue
Block a user