From 01f13292bbf271209328578ad2de08749cbb6cf6 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 14 Feb 2024 13:19:48 -0300 Subject: [PATCH] useWebSocketImplementation() on relay.ts --- README.md | 5 +++-- abstract-relay.ts | 12 +++++++++++- package.json | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index beb5cfd..67c254e 100644 --- a/README.md +++ b/README.md @@ -92,10 +92,11 @@ await relay.publish(signedEvent) relay.close() ``` -To use this on Node.js you first must install `websocket-polyfill` and import it: +To use this on Node.js you first must install `ws` and call something like this: ```js -import 'websocket-polyfill' +import { useWebSocketImplementation } from 'nostr-tools/relay' +useWebSocketImplementation(require('ws')) ``` ### Interacting with multiple relays diff --git a/abstract-relay.ts b/abstract-relay.ts index 30571e5..8277eec 100644 --- a/abstract-relay.ts +++ b/abstract-relay.ts @@ -7,6 +7,16 @@ import { Queue, normalizeURL } from './utils.ts' import { makeAuthEvent } from './nip42.ts' import { yieldThread } from './helpers.ts' +var _WebSocket: typeof WebSocket + +try { + _WebSocket = WebSocket +} catch {} + +export function useWebSocketImplementation(websocketImplementation: any) { + _WebSocket = websocketImplementation +} + export class AbstractRelay { public readonly url: string private _connected: boolean = false @@ -74,7 +84,7 @@ export class AbstractRelay { }, this.connectionTimeout) try { - this.ws = new WebSocket(this.url) + this.ws = new _WebSocket(this.url) } catch (err) { reject(err) return diff --git a/package.json b/package.json index e37277a..b9dc153 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "nostr-tools", - "version": "2.1.8", + "version": "2.1.9", "description": "Tools for making a Nostr client.", "repository": { "type": "git",