diff --git a/abstract-relay.ts b/abstract-relay.ts index 571540c..fd979bf 100644 --- a/abstract-relay.ts +++ b/abstract-relay.ts @@ -1,6 +1,6 @@ /* global WebSocket */ -import type { Event, EventTemplate, VerifiedEvent, Nostr } from './core.ts' +import type { Event, EventTemplate, VerifiedEvent, Nostr, NostrEvent } from './core.ts' import { matchFilters, type Filter } from './filter.ts' import { getHex64, getSubscriptionId } from './fakejson.ts' import { Queue, normalizeURL } from './utils.ts' @@ -12,6 +12,13 @@ export type AbstractRelayConstructorOptions = { websocketImplementation?: typeof WebSocket } +export class SendingOnClosedConnection extends Error { + constructor(message: string, relay: string) { + super(`Tried to send message '${message} on a closed connection to ${relay}.`) + this.name = 'SendingOnClosedConnection' + } +} + export class AbstractRelay { public readonly url: string private _connected: boolean = false @@ -112,7 +119,7 @@ export class AbstractRelay { } } - this.ws.onclose = (ev) => { + this.ws.onclose = ev => { clearTimeout(this.connectionTimeoutHandle) reject((ev as any).message || 'websocket closed') if (this._connected) { @@ -178,7 +185,7 @@ export class AbstractRelay { switch (data[0]) { case 'EVENT': { const so = this.openSubs.get(data[1] as string) as Subscription - const event = data[2] as Event + const event = data[2] as NostrEvent if (this.verifyEvent(event) && matchFilters(so.filters, event)) { so.onevent(event) } @@ -236,7 +243,7 @@ export class AbstractRelay { } public async send(message: string) { - if (!this.connectionPromise) throw new Error('sending on closed connection') + if (!this.connectionPromise) throw new SendingOnClosedConnection(message, this.url) this.connectionPromise.then(() => { this.ws?.send(message) @@ -379,7 +386,15 @@ export class Subscription { if (!this.closed && this.relay.connected) { // if the connection was closed by the user calling .close() we will send a CLOSE message // otherwise this._open will be already set to false so we will skip this - this.relay.send('["CLOSE",' + JSON.stringify(this.id) + ']') + try { + this.relay.send('["CLOSE",' + JSON.stringify(this.id) + ']') + } catch (err) { + if (err instanceof SendingOnClosedConnection) { + /* doesn't matter, it's ok */ + } else { + throw err + } + } this.closed = true } this.relay.openSubs.delete(this.id) diff --git a/jsr.json b/jsr.json index 9b253bb..f7d0252 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@nostr/tools", - "version": "2.13.2", + "version": "2.13.3", "exports": { ".": "./index.ts", "./core": "./core.ts", diff --git a/package.json b/package.json index 259c7fc..9f1e979 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "nostr-tools", - "version": "2.13.2", + "version": "2.13.3", "description": "Tools for making a Nostr client.", "repository": { "type": "git",