From 4b6cc19b9c75daea9d52af1d8a770815592837c5 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 23 Jul 2025 16:22:25 -0300 Subject: [PATCH] cleanup. --- abstract-relay.ts | 18 ++++++++---------- jsr.json | 2 +- nip29.ts | 2 +- package.json | 2 +- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/abstract-relay.ts b/abstract-relay.ts index 4f7923f..33a68d6 100644 --- a/abstract-relay.ts +++ b/abstract-relay.ts @@ -110,7 +110,8 @@ export class AbstractRelay { this.ws.onopen = () => { clearTimeout(this.connectionTimeoutHandle) this._connected = true - if (this.ws && this.ws.ping) { // && this.pingHeartBeat + if (this.ws && this.ws.ping) { + // && this.pingHeartBeat this.pingpong() } resolve() @@ -146,31 +147,28 @@ export class AbstractRelay { private async receivePong() { return new Promise((res, err) => { - (this.ws && this.ws.on && this.ws.on('pong', () => res(true))) || err("ws can't listen for pong") + ;(this.ws && this.ws.on && this.ws.on('pong', () => res(true))) || err("ws can't listen for pong") }) } + // nodejs requires this magic here to ensure connections are closed when internet goes off and stuff + // in browsers it's done automatically. see https://github.com/nbd-wtf/nostr-tools/issues/491 private async pingpong() { - console.error('pingpong') // if the websocket is connected if (this.ws?.readyState == 1) { - console.error('pingpong readyState==1') // send a ping - console.error('pingpong ping()'); - (this.ws && this.ws.ping) && this.ws.ping() + this.ws && this.ws.ping && this.ws.ping() // wait for either a pong or a timeout - console.error('pingpong wait for pong or timeout') const result = await Promise.any([ this.receivePong(), - new Promise(res => setTimeout(() => res(false), 10000)) // TODO: opts.pingTimeout + new Promise(res => setTimeout(() => res(false), 10000)), // TODO: opts.pingTimeout ]) console.error('pingpong result', result) if (result) { - console.error('pingpong scheduling pingpong') // schedule another pingpong setTimeout(() => this.pingpong(), 10000) // TODO: opts.pingFrequency } else { - console.error('pingpong closing socket') + // pingpong closing socket this.ws && this.ws.close() } } diff --git a/jsr.json b/jsr.json index 441d007..c761c5f 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@nostr/tools", - "version": "2.15.1", + "version": "2.15.2", "exports": { ".": "./index.ts", "./core": "./core.ts", diff --git a/nip29.ts b/nip29.ts index 9f61e68..bbc5255 100644 --- a/nip29.ts +++ b/nip29.ts @@ -2,7 +2,7 @@ import { AbstractSimplePool } from './abstract-pool.ts' import { Subscription } from './abstract-relay.ts' import type { Event, EventTemplate } from './core.ts' import { fetchRelayInformation, RelayInformation } from './nip11.ts' -import { AddressPointer, decode, NostrTypeGuard } from './nip19.ts' +import { decode, NostrTypeGuard } from './nip19.ts' import { normalizeURL } from './utils.ts' /** diff --git a/package.json b/package.json index 7ff8f86..5499ac6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "nostr-tools", - "version": "2.15.0", + "version": "2.15.2", "description": "Tools for making a Nostr client.", "repository": { "type": "git",