mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 00:28:51 +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> {
|
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