diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a94e593..ffe7bfc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,8 +11,9 @@ jobs: steps: - uses: actions/checkout@v3 - uses: oven-sh/setup-bun@v1 + - uses: extractions/setup-just@v1 - run: bun i - - run: bun test --timeout 20000 + - run: just test format: runs-on: ubuntu-latest steps: diff --git a/justfile b/justfile index 70f3b9a..4f7db92 100644 --- a/justfile +++ b/justfile @@ -5,7 +5,7 @@ build: bun run build.js test: - bun test + bun test --timeout 20000 test-only file: bun test {{file}} diff --git a/pool.test.ts b/pool.test.ts index 3b9565d..49e3e0f 100644 --- a/pool.test.ts +++ b/pool.test.ts @@ -1,4 +1,4 @@ -import { test, expect } from 'bun:test' +import { test, expect, afterAll } from 'bun:test' import { finishEvent, type Event } from './event.ts' import { generatePrivateKey, getPublicKey } from './keys.ts' @@ -8,6 +8,10 @@ let pool = new SimplePool() let relays = ['wss://relay.damus.io/', 'wss://relay.nostr.bg/', 'wss://nos.lol', 'wss://public.relaying.io'] +afterAll(() => { + pool.close([...relays, 'wss://offchain.pub', 'wss://eden.nostr.land']) +}) + test('removing duplicates when querying', async () => { let priv = generatePrivateKey() let pub = getPublicKey(priv) @@ -33,7 +37,6 @@ test('removing duplicates when querying', async () => { ) await Promise.any(pool.publish(relays, event)) - await new Promise(resolve => setTimeout(resolve, 1500)) expect(received).toHaveLength(1) diff --git a/pool.ts b/pool.ts index ac91a88..3cf39c5 100644 --- a/pool.ts +++ b/pool.ts @@ -34,6 +34,12 @@ export class SimplePool { return relay } + close(relays: string[]) { + relays.map(normalizeURL).forEach(url => { + this.relays.get(url)?.close() + }) + } + subscribeMany(relays: string[], filters: Filter[], params: SubscribeManyParams): SubCloser { if (this.trackRelays) { params.receivedEvent = (relay: Relay, id: string) => { diff --git a/relay.ts b/relay.ts index e608715..240bca9 100644 --- a/relay.ts +++ b/relay.ts @@ -21,7 +21,7 @@ export class Relay { public onnotice: (msg: string) => void = msg => console.debug(`NOTICE from ${this.url}: ${msg}`) public baseEoseTimeout: number = 4400 - public connectionTimeout: number = 8800 + public connectionTimeout: number = 4400 private connectionTimeoutHandle: ReturnType | undefined private connectionPromise: Promise | undefined @@ -215,18 +215,21 @@ export class Relay { } public async send(message: string) { - this.ws?.send(message) + if (!this.connectionPromise) throw new Error('sending on closed connection') + + this.connectionPromise.then(() => { + this.ws?.send(message) + }) } public async auth(signAuthEvent: (authEvent: EventTemplate) => Promise) { if (!this.challenge) throw new Error("can't perform auth, no challenge was received") const evt = nip42.makeAuthEvent(this.url, this.challenge) - await Promise.all([signAuthEvent(evt), this.connect()]) + await signAuthEvent(evt) this.send('["AUTH",' + JSON.stringify(evt) + ']') } public async publish(event: Event): Promise { - await this.connect() const ret = new Promise((resolve, reject) => { this.openEventPublishes.set(event.id, { resolve, reject }) }) @@ -235,7 +238,6 @@ export class Relay { } public async count(filters: Filter[], params: { id?: string | null }): Promise { - await this.connect() this.serial++ const id = params?.id || 'count:' + this.serial const ret = new Promise((resolve, reject) => { @@ -246,7 +248,6 @@ export class Relay { } public async subscribe(filters: Filter[], params: Partial): Promise { - await this.connect() const subscription = this.prepareSubscription(filters, params) subscription.fire() return subscription