mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
make relay.connect() an awaitable thing.
This commit is contained in:
@@ -53,7 +53,7 @@ import {
|
|||||||
} from 'nostr-tools'
|
} from 'nostr-tools'
|
||||||
|
|
||||||
const relay = relayInit('wss://relay.example.com')
|
const relay = relayInit('wss://relay.example.com')
|
||||||
relay.connect()
|
await relay.connect()
|
||||||
|
|
||||||
relay.on('connect', () => {
|
relay.on('connect', () => {
|
||||||
console.log(`connected to ${relay.url}`)
|
console.log(`connected to ${relay.url}`)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "1.0.0-beta2",
|
"version": "1.0.0-rc1",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
114
relay.ts
114
relay.ts
@@ -59,73 +59,77 @@ export function relayInit(url: string): Relay {
|
|||||||
}
|
}
|
||||||
} = {}
|
} = {}
|
||||||
|
|
||||||
function connectRelay() {
|
async function connectRelay(): Promise<void> {
|
||||||
ws = new WebSocket(url)
|
return new Promise((resolve, reject) => {
|
||||||
|
ws = new WebSocket(url)
|
||||||
|
|
||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
listeners.connect.forEach(cb => cb())
|
listeners.connect.forEach(cb => cb())
|
||||||
}
|
resolve()
|
||||||
ws.onerror = () => {
|
}
|
||||||
listeners.error.forEach(cb => cb())
|
ws.onerror = () => {
|
||||||
}
|
listeners.error.forEach(cb => cb())
|
||||||
ws.onclose = async () => {
|
reject()
|
||||||
listeners.disconnect.forEach(cb => cb())
|
}
|
||||||
resolveClose()
|
ws.onclose = async () => {
|
||||||
}
|
listeners.disconnect.forEach(cb => cb())
|
||||||
|
resolveClose()
|
||||||
ws.onmessage = async e => {
|
|
||||||
var data
|
|
||||||
try {
|
|
||||||
data = JSON.parse(e.data)
|
|
||||||
} catch (err) {
|
|
||||||
data = e.data
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.length >= 1) {
|
ws.onmessage = async e => {
|
||||||
switch (data[0]) {
|
var data
|
||||||
case 'EVENT':
|
try {
|
||||||
if (data.length !== 3) return // ignore empty or malformed EVENT
|
data = JSON.parse(e.data)
|
||||||
|
} catch (err) {
|
||||||
|
data = e.data
|
||||||
|
}
|
||||||
|
|
||||||
let id = data[1]
|
if (data.length >= 1) {
|
||||||
let event = data[2]
|
switch (data[0]) {
|
||||||
if (
|
case 'EVENT':
|
||||||
validateEvent(event) &&
|
if (data.length !== 3) return // ignore empty or malformed EVENT
|
||||||
openSubs[id] &&
|
|
||||||
(openSubs[id].skipVerification || verifySignature(event)) &&
|
let id = data[1]
|
||||||
matchFilters(openSubs[id].filters, event)
|
let event = data[2]
|
||||||
) {
|
if (
|
||||||
openSubs[id]
|
validateEvent(event) &&
|
||||||
subListeners[id]?.event.forEach(cb => cb(event))
|
openSubs[id] &&
|
||||||
|
(openSubs[id].skipVerification || verifySignature(event)) &&
|
||||||
|
matchFilters(openSubs[id].filters, event)
|
||||||
|
) {
|
||||||
|
openSubs[id]
|
||||||
|
subListeners[id]?.event.forEach(cb => cb(event))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
case 'EOSE': {
|
||||||
|
if (data.length !== 2) return // ignore empty or malformed EOSE
|
||||||
|
let id = data[1]
|
||||||
|
subListeners[id]?.eose.forEach(cb => cb())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return
|
case 'OK': {
|
||||||
case 'EOSE': {
|
if (data.length < 3) return // ignore empty or malformed OK
|
||||||
if (data.length !== 2) return // ignore empty or malformed EOSE
|
let id: string = data[1]
|
||||||
let id = data[1]
|
let ok: boolean = data[2]
|
||||||
subListeners[id]?.eose.forEach(cb => cb())
|
let reason: string = data[3] || ''
|
||||||
return
|
if (ok) pubListeners[id]?.ok.forEach(cb => cb())
|
||||||
|
else pubListeners[id]?.failed.forEach(cb => cb(reason))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case 'NOTICE':
|
||||||
|
if (data.length !== 2) return // ignore empty or malformed NOTICE
|
||||||
|
let notice = data[1]
|
||||||
|
listeners.notice.forEach(cb => cb(notice))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
case 'OK': {
|
|
||||||
if (data.length < 3) return // ignore empty or malformed OK
|
|
||||||
let id: string = data[1]
|
|
||||||
let ok: boolean = data[2]
|
|
||||||
let reason: string = data[3] || ''
|
|
||||||
if (ok) pubListeners[id]?.ok.forEach(cb => cb())
|
|
||||||
else pubListeners[id]?.failed.forEach(cb => cb(reason))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
case 'NOTICE':
|
|
||||||
if (data.length !== 2) return // ignore empty or malformed NOTICE
|
|
||||||
let notice = data[1]
|
|
||||||
listeners.notice.forEach(cb => cb(notice))
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function connect(): Promise<void> {
|
async function connect(): Promise<void> {
|
||||||
if (ws?.readyState && ws.readyState === 1) return // ws already open
|
if (ws?.readyState && ws.readyState === 1) return // ws already open
|
||||||
connectRelay()
|
await connectRelay()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function trySend(params: [string, ...any]) {
|
async function trySend(params: [string, ...any]) {
|
||||||
|
|||||||
Reference in New Issue
Block a user