Skip to content

Commit

Permalink
fix useWebSocketImplementation so it works with pool on nodejs esm.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed May 29, 2024
1 parent 4f1dc9e commit 87a91c2
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 23 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ relay.close()
To use this on Node.js you first must install `ws` and call something like this:

```js
import { useWebSocketImplementation } from 'nostr-tools/relay'
useWebSocketImplementation(require('ws'))
import { useWebSocketImplementation } from 'nostr-tools/pool'
// or import { useWebSocketImplementation } from 'nostr-tools/relay' if you're using the Relay directly

import WebSocket from 'ws'
useWebSocketImplementation(WebSocket)
```

### Interacting with multiple relays
Expand Down Expand Up @@ -197,7 +200,7 @@ declare global {

### Generating NIP-06 keys
```js
import {
import {
privateKeyFromSeedWords,
accountFromSeedWords,
extendedKeysFromSeedWords,
Expand Down
17 changes: 15 additions & 2 deletions abstract-pool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { AbstractRelay as AbstractRelay, SubscriptionParams, Subscription } from './abstract-relay.ts'
/* global WebSocket */

import {
AbstractRelay as AbstractRelay,
SubscriptionParams,
Subscription,
type AbstractRelayConstructorOptions,
} from './abstract-relay.ts'
import { normalizeURL } from './utils.ts'

import type { Event, Nostr } from './core.ts'
Expand All @@ -7,6 +14,8 @@ import { alwaysTrue } from './helpers.ts'

export type SubCloser = { close: () => void }

export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {}

export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose' | 'id'> & {
maxWait?: number
onclose?: (reasons: string[]) => void
Expand All @@ -21,8 +30,11 @@ export class AbstractSimplePool {
public verifyEvent: Nostr['verifyEvent']
public trustedRelayURLs: Set<string> = new Set()

constructor(opts: { verifyEvent: Nostr['verifyEvent'] }) {
private _WebSocket?: typeof WebSocket

constructor(opts: AbstractPoolConstructorOptions) {
this.verifyEvent = opts.verifyEvent
this._WebSocket = opts.websocketImplementation
}

async ensureRelay(url: string, params?: { connectionTimeout?: number }): Promise<AbstractRelay> {
Expand All @@ -32,6 +44,7 @@ export class AbstractSimplePool {
if (!relay) {
relay = new AbstractRelay(url, {
verifyEvent: this.trustedRelayURLs.has(url) ? alwaysTrue : this.verifyEvent,
websocketImplementation: this._WebSocket,
})
if (params?.connectionTimeout) relay.connectionTimeout = params.connectionTimeout
this.relays.set(url, relay)
Expand Down
20 changes: 9 additions & 11 deletions abstract-relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ 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 type AbstractRelayConstructorOptions = {
verifyEvent: Nostr['verifyEvent']
websocketImplementation?: typeof WebSocket
}

export class AbstractRelay {
Expand Down Expand Up @@ -42,12 +37,15 @@ export class AbstractRelay {
private serial: number = 0
private verifyEvent: Nostr['verifyEvent']

constructor(url: string, opts: { verifyEvent: Nostr['verifyEvent'] }) {
private _WebSocket: typeof WebSocket

constructor(url: string, opts: AbstractRelayConstructorOptions) {
this.url = normalizeURL(url)
this.verifyEvent = opts.verifyEvent
this._WebSocket = opts.websocketImplementation || WebSocket
}

static async connect(url: string, opts: { verifyEvent: Nostr['verifyEvent'] }): Promise<AbstractRelay> {
static async connect(url: string, opts: AbstractRelayConstructorOptions): Promise<AbstractRelay> {
const relay = new AbstractRelay(url, opts)
await relay.connect()
return relay
Expand Down Expand Up @@ -87,7 +85,7 @@ export class AbstractRelay {
}, this.connectionTimeout)

try {
this.ws = new _WebSocket(this.url)
this.ws = new this._WebSocket(this.url)
} catch (err) {
reject(err)
return
Expand Down
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from './pure.ts'
export * from './relay.ts'
export { Relay } from './relay.ts'
export * from './filter.ts'
export * from './pool.ts'
export { SimplePool } from './pool.ts'
export * from './references.ts'

export * as nip04 from './nip04.ts'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "nostr-tools",
"version": "2.6.0",
"version": "2.7.0",
"description": "Tools for making a Nostr client.",
"repository": {
"type": "git",
Expand Down
3 changes: 1 addition & 2 deletions pool.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { afterEach, beforeEach, expect, test } from 'bun:test'

import { SimplePool } from './pool.ts'
import { SimplePool, useWebSocketImplementation } from './pool.ts'
import { finalizeEvent, generateSecretKey, getPublicKey, type Event } from './pure.ts'
import { useWebSocketImplementation } from './relay.ts'
import { MockRelay, MockWebSocketClient } from './test-helpers.ts'
import { hexToBytes } from '@noble/hashes/utils'

Expand Down
14 changes: 13 additions & 1 deletion pool.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
/* global WebSocket */

import { verifyEvent } from './pure.ts'
import { AbstractSimplePool } from './abstract-pool.ts'

var _WebSocket: typeof WebSocket

try {
_WebSocket = WebSocket
} catch {}

export function useWebSocketImplementation(websocketImplementation: any) {
_WebSocket = websocketImplementation
}

export class SimplePool extends AbstractSimplePool {
constructor() {
super({ verifyEvent })
super({ verifyEvent, websocketImplementation: _WebSocket })
}
}

Expand Down
14 changes: 13 additions & 1 deletion relay.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global WebSocket */

import { verifyEvent } from './pure.ts'
import { AbstractRelay } from './abstract-relay.ts'

Expand All @@ -8,9 +10,19 @@ export function relayConnect(url: string): Promise<Relay> {
return Relay.connect(url)
}

var _WebSocket: typeof WebSocket

try {
_WebSocket = WebSocket
} catch {}

export function useWebSocketImplementation(websocketImplementation: any) {
_WebSocket = websocketImplementation
}

export class Relay extends AbstractRelay {
constructor(url: string) {
super(url, { verifyEvent })
super(url, { verifyEvent, websocketImplementation: _WebSocket })
}

static async connect(url: string): Promise<Relay> {
Expand Down

0 comments on commit 87a91c2

Please sign in to comment.