fix useWebSocketImplementation so it works with pool on nodejs esm.
This commit is contained in:
parent
4f1dc9ef1c
commit
87a91c2daf
|
@ -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
|
||||
|
|
|
@ -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'
|
||||
|
@ -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
|
||||
|
@ -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> {
|
||||
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
|
4
index.ts
4
index.ts
|
@ -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'
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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'
|
||||
|
||||
|
|
14
pool.ts
14
pool.ts
|
@ -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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
14
relay.ts
14
relay.ts
|
@ -1,3 +1,5 @@
|
|||
/* global WebSocket */
|
||||
|
||||
import { verifyEvent } from './pure.ts'
|
||||
import { AbstractRelay } from './abstract-relay.ts'
|
||||
|
||||
|
@ -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> {
|
||||
|
|
Loading…
Reference in New Issue