mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 08:38:50 +00:00
Compare commits
8 Commits
v2.14.2
...
node-webso
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
719462f041 | ||
|
|
de1cf0ed60 | ||
|
|
d706ef961f | ||
|
|
2f529b3f8a | ||
|
|
f0357805c3 | ||
|
|
ffa7fb926e | ||
|
|
12acb900ab | ||
|
|
d773012658 |
@@ -4,7 +4,7 @@ Tools for developing [Nostr](https://github.com/fiatjaf/nostr) clients.
|
|||||||
|
|
||||||
Only depends on _@scure_ and _@noble_ packages.
|
Only depends on _@scure_ and _@noble_ packages.
|
||||||
|
|
||||||
This package is only providing lower-level functionality. If you want more higher-level features, take a look at [Nostrify](https://nostrify.dev), or if you want an easy-to-use fully-fledged solution that abstracts the hard parts of Nostr and makes decisions on your behalf, take a look at [NDK](https://github.com/nostr-dev-kit/ndk) and [@snort/system](https://www.npmjs.com/package/@snort/system).
|
This package is only providing lower-level functionality. If you want higher-level features, take a look at [@nostr/gadgets](https://jsr.io/@nostr/gadgets) which is based on this library and expands upon it and has other goodies (it's only available on jsr).
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
|||||||
@@ -12,13 +12,15 @@ import type { Event, EventTemplate, Nostr, VerifiedEvent } from './core.ts'
|
|||||||
import { type Filter } from './filter.ts'
|
import { type Filter } from './filter.ts'
|
||||||
import { alwaysTrue } from './helpers.ts'
|
import { alwaysTrue } from './helpers.ts'
|
||||||
|
|
||||||
export type SubCloser = { close: () => void }
|
export type SubCloser = { close: (reason?: string) => void }
|
||||||
|
|
||||||
export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {}
|
export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {}
|
||||||
|
|
||||||
export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
|
export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
|
||||||
maxWait?: number
|
maxWait?: number
|
||||||
onclose?: (reasons: string[]) => void
|
onclose?: (reasons: string[]) => void
|
||||||
|
onauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
||||||
|
// Deprecated: use onauth instead
|
||||||
doauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
doauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
||||||
id?: string
|
id?: string
|
||||||
label?: string
|
label?: string
|
||||||
@@ -48,6 +50,9 @@ export class AbstractSimplePool {
|
|||||||
verifyEvent: this.trustedRelayURLs.has(url) ? alwaysTrue : this.verifyEvent,
|
verifyEvent: this.trustedRelayURLs.has(url) ? alwaysTrue : this.verifyEvent,
|
||||||
websocketImplementation: this._WebSocket,
|
websocketImplementation: this._WebSocket,
|
||||||
})
|
})
|
||||||
|
relay.onclose = () => {
|
||||||
|
this.relays.delete(url)
|
||||||
|
}
|
||||||
if (params?.connectionTimeout) relay.connectionTimeout = params.connectionTimeout
|
if (params?.connectionTimeout) relay.connectionTimeout = params.connectionTimeout
|
||||||
this.relays.set(url, relay)
|
this.relays.set(url, relay)
|
||||||
}
|
}
|
||||||
@@ -59,10 +64,13 @@ export class AbstractSimplePool {
|
|||||||
close(relays: string[]) {
|
close(relays: string[]) {
|
||||||
relays.map(normalizeURL).forEach(url => {
|
relays.map(normalizeURL).forEach(url => {
|
||||||
this.relays.get(url)?.close()
|
this.relays.get(url)?.close()
|
||||||
|
this.relays.delete(url)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
subscribe(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
||||||
|
params.onauth = params.onauth || params.doauth
|
||||||
|
|
||||||
return this.subscribeMap(
|
return this.subscribeMap(
|
||||||
relays.map(url => ({ url, filter })),
|
relays.map(url => ({ url, filter })),
|
||||||
params,
|
params,
|
||||||
@@ -70,6 +78,8 @@ export class AbstractSimplePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subscribeMany(relays: string[], filters: Filter[], params: SubscribeManyParams): SubCloser {
|
subscribeMany(relays: string[], filters: Filter[], params: SubscribeManyParams): SubCloser {
|
||||||
|
params.onauth = params.onauth || params.doauth
|
||||||
|
|
||||||
return this.subscribeMap(
|
return this.subscribeMap(
|
||||||
relays.flatMap(url => filters.map(filter => ({ url, filter }))),
|
relays.flatMap(url => filters.map(filter => ({ url, filter }))),
|
||||||
params,
|
params,
|
||||||
@@ -77,6 +87,8 @@ export class AbstractSimplePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subscribeMap(requests: { url: string; filter: Filter }[], params: SubscribeManyParams): SubCloser {
|
subscribeMap(requests: { url: string; filter: Filter }[], params: SubscribeManyParams): SubCloser {
|
||||||
|
params.onauth = params.onauth || params.doauth
|
||||||
|
|
||||||
if (this.trackRelays) {
|
if (this.trackRelays) {
|
||||||
params.receivedEvent = (relay: AbstractRelay, id: string) => {
|
params.receivedEvent = (relay: AbstractRelay, id: string) => {
|
||||||
let set = this.seenOn.get(id)
|
let set = this.seenOn.get(id)
|
||||||
@@ -141,9 +153,9 @@ export class AbstractSimplePool {
|
|||||||
...params,
|
...params,
|
||||||
oneose: () => handleEose(i),
|
oneose: () => handleEose(i),
|
||||||
onclose: reason => {
|
onclose: reason => {
|
||||||
if (reason.startsWith('auth-required:') && params.doauth) {
|
if (reason.startsWith('auth-required: ') && params.onauth) {
|
||||||
relay
|
relay
|
||||||
.auth(params.doauth)
|
.auth(params.onauth)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
relay.subscribe([filter], {
|
relay.subscribe([filter], {
|
||||||
...params,
|
...params,
|
||||||
@@ -171,10 +183,10 @@ export class AbstractSimplePool {
|
|||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
async close() {
|
async close(reason?: string) {
|
||||||
await allOpened
|
await allOpened
|
||||||
subs.forEach(sub => {
|
subs.forEach(sub => {
|
||||||
sub.close()
|
sub.close(reason)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -183,12 +195,14 @@ export class AbstractSimplePool {
|
|||||||
subscribeEose(
|
subscribeEose(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'doauth'>,
|
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth' | 'doauth'>,
|
||||||
): SubCloser {
|
): SubCloser {
|
||||||
|
params.onauth = params.onauth || params.doauth
|
||||||
|
|
||||||
const subcloser = this.subscribe(relays, filter, {
|
const subcloser = this.subscribe(relays, filter, {
|
||||||
...params,
|
...params,
|
||||||
oneose() {
|
oneose() {
|
||||||
subcloser.close()
|
subcloser.close('closed automatically on eose')
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
return subcloser
|
return subcloser
|
||||||
@@ -197,12 +211,14 @@ export class AbstractSimplePool {
|
|||||||
subscribeManyEose(
|
subscribeManyEose(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
filters: Filter[],
|
filters: Filter[],
|
||||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'doauth'>,
|
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth' | 'doauth'>,
|
||||||
): SubCloser {
|
): SubCloser {
|
||||||
|
params.onauth = params.onauth || params.doauth
|
||||||
|
|
||||||
const subcloser = this.subscribeMany(relays, filters, {
|
const subcloser = this.subscribeMany(relays, filters, {
|
||||||
...params,
|
...params,
|
||||||
oneose() {
|
oneose() {
|
||||||
subcloser.close()
|
subcloser.close('closed automatically on eose')
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
return subcloser
|
return subcloser
|
||||||
@@ -238,7 +254,11 @@ export class AbstractSimplePool {
|
|||||||
return events[0] || null
|
return events[0] || null
|
||||||
}
|
}
|
||||||
|
|
||||||
publish(relays: string[], event: Event): Promise<string>[] {
|
publish(
|
||||||
|
relays: string[],
|
||||||
|
event: Event,
|
||||||
|
options?: { onauth?: (evt: EventTemplate) => Promise<VerifiedEvent> },
|
||||||
|
): Promise<string>[] {
|
||||||
return relays.map(normalizeURL).map(async (url, i, arr) => {
|
return relays.map(normalizeURL).map(async (url, i, arr) => {
|
||||||
if (arr.indexOf(url) !== i) {
|
if (arr.indexOf(url) !== i) {
|
||||||
// duplicate
|
// duplicate
|
||||||
@@ -246,17 +266,26 @@ export class AbstractSimplePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let r = await this.ensureRelay(url)
|
let r = await this.ensureRelay(url)
|
||||||
return r.publish(event).then(reason => {
|
return r
|
||||||
if (this.trackRelays) {
|
.publish(event)
|
||||||
let set = this.seenOn.get(event.id)
|
.catch(async err => {
|
||||||
if (!set) {
|
if (err instanceof Error && err.message.startsWith('auth-required: ') && options?.onauth) {
|
||||||
set = new Set()
|
await r.auth(options.onauth)
|
||||||
this.seenOn.set(event.id, set)
|
return r.publish(event) // retry
|
||||||
}
|
}
|
||||||
set.add(r)
|
throw err
|
||||||
}
|
})
|
||||||
return reason
|
.then(reason => {
|
||||||
})
|
if (this.trackRelays) {
|
||||||
|
let set = this.seenOn.get(event.id)
|
||||||
|
if (!set) {
|
||||||
|
set = new Set()
|
||||||
|
this.seenOn.set(event.id, set)
|
||||||
|
}
|
||||||
|
set.add(r)
|
||||||
|
}
|
||||||
|
return reason
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,22 @@ import { Queue, normalizeURL } from './utils.ts'
|
|||||||
import { makeAuthEvent } from './nip42.ts'
|
import { makeAuthEvent } from './nip42.ts'
|
||||||
import { yieldThread } from './helpers.ts'
|
import { yieldThread } from './helpers.ts'
|
||||||
|
|
||||||
|
export type RelayRecord = Record<string, { read: boolean; write: boolean }>
|
||||||
|
|
||||||
|
export interface WebSocketBase {
|
||||||
|
new (url: string | URL, protocols?: string | string[] | undefined): WebSocketBaseConn
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WebSocketBaseConn {
|
||||||
|
onopen: ((this: WebSocketBaseConn, ev: Event) => any) | null
|
||||||
|
onclose: ((this: WebSocketBaseConn) => void) | null
|
||||||
|
onerror: ((this: WebSocketBaseConn, _: MessageEvent<any>) => void) | null
|
||||||
|
onmessage: ((_: MessageEvent<any>) => void) | null
|
||||||
|
}
|
||||||
|
|
||||||
export type AbstractRelayConstructorOptions = {
|
export type AbstractRelayConstructorOptions = {
|
||||||
verifyEvent: Nostr['verifyEvent']
|
verifyEvent: Nostr['verifyEvent']
|
||||||
websocketImplementation?: typeof WebSocket
|
websocketImplementation?: WebSocketBase
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SendingOnClosedConnection extends Error {
|
export class SendingOnClosedConnection extends Error {
|
||||||
@@ -26,9 +39,6 @@ export class AbstractRelay {
|
|||||||
public onclose: (() => void) | null = null
|
public onclose: (() => void) | null = null
|
||||||
public onnotice: (msg: string) => void = msg => console.debug(`NOTICE from ${this.url}: ${msg}`)
|
public onnotice: (msg: string) => void = msg => console.debug(`NOTICE from ${this.url}: ${msg}`)
|
||||||
|
|
||||||
// this is exposed just to help in ndk migration, shouldn't be relied upon
|
|
||||||
public _onauth: ((challenge: string) => void) | null = null
|
|
||||||
|
|
||||||
public baseEoseTimeout: number = 4400
|
public baseEoseTimeout: number = 4400
|
||||||
public connectionTimeout: number = 4400
|
public connectionTimeout: number = 4400
|
||||||
public publishTimeout: number = 4400
|
public publishTimeout: number = 4400
|
||||||
@@ -46,7 +56,7 @@ export class AbstractRelay {
|
|||||||
private serial: number = 0
|
private serial: number = 0
|
||||||
private verifyEvent: Nostr['verifyEvent']
|
private verifyEvent: Nostr['verifyEvent']
|
||||||
|
|
||||||
private _WebSocket: typeof WebSocket
|
private _WebSocket: WebSocketBase
|
||||||
|
|
||||||
constructor(url: string, opts: AbstractRelayConstructorOptions) {
|
constructor(url: string, opts: AbstractRelayConstructorOptions) {
|
||||||
this.url = normalizeURL(url)
|
this.url = normalizeURL(url)
|
||||||
@@ -233,7 +243,6 @@ export class AbstractRelay {
|
|||||||
return
|
return
|
||||||
case 'AUTH': {
|
case 'AUTH': {
|
||||||
this.challenge = data[1] as string
|
this.challenge = data[1] as string
|
||||||
this._onauth?.(data[1] as string)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -256,16 +265,20 @@ export class AbstractRelay {
|
|||||||
if (this.authPromise) return this.authPromise
|
if (this.authPromise) return this.authPromise
|
||||||
|
|
||||||
this.authPromise = new Promise<string>(async (resolve, reject) => {
|
this.authPromise = new Promise<string>(async (resolve, reject) => {
|
||||||
const evt = await signAuthEvent(makeAuthEvent(this.url, challenge))
|
try {
|
||||||
const timeout = setTimeout(() => {
|
let evt = await signAuthEvent(makeAuthEvent(this.url, challenge))
|
||||||
const ep = this.openEventPublishes.get(evt.id) as EventPublishResolver
|
let timeout = setTimeout(() => {
|
||||||
if (ep) {
|
let ep = this.openEventPublishes.get(evt.id) as EventPublishResolver
|
||||||
ep.reject(new Error('auth timed out'))
|
if (ep) {
|
||||||
this.openEventPublishes.delete(evt.id)
|
ep.reject(new Error('auth timed out'))
|
||||||
}
|
this.openEventPublishes.delete(evt.id)
|
||||||
}, this.publishTimeout)
|
}
|
||||||
this.openEventPublishes.set(evt.id, { resolve, reject, timeout })
|
}, this.publishTimeout)
|
||||||
this.send('["AUTH",' + JSON.stringify(evt) + ']')
|
this.openEventPublishes.set(evt.id, { resolve, reject, timeout })
|
||||||
|
this.send('["AUTH",' + JSON.stringify(evt) + ']')
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('subscribe auth function failed:', err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
return this.authPromise
|
return this.authPromise
|
||||||
}
|
}
|
||||||
@@ -319,6 +332,7 @@ export class AbstractRelay {
|
|||||||
this.closeAllSubscriptions('relay connection closed by us')
|
this.closeAllSubscriptions('relay connection closed by us')
|
||||||
this._connected = false
|
this._connected = false
|
||||||
this.ws?.close()
|
this.ws?.close()
|
||||||
|
this.onclose?.()
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is the function assigned to this.ws.onmessage
|
// this is the function assigned to this.ws.onmessage
|
||||||
|
|||||||
4
jsr.json
4
jsr.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nostr/tools",
|
"name": "@nostr/tools",
|
||||||
"version": "2.14.2",
|
"version": "2.15.1",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./index.ts",
|
".": "./index.ts",
|
||||||
"./core": "./core.ts",
|
"./core": "./core.ts",
|
||||||
@@ -44,7 +44,7 @@
|
|||||||
"./nip99": "./nip99.ts",
|
"./nip99": "./nip99.ts",
|
||||||
"./nipb7": "./nipb7.ts",
|
"./nipb7": "./nipb7.ts",
|
||||||
"./fakejson": "./fakejson.ts",
|
"./fakejson": "./fakejson.ts",
|
||||||
"./utils": "./utils.ts"
|
"./utils": "./utils.ts",
|
||||||
"./signer": "./signer.ts"
|
"./signer": "./signer.ts"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
justfile
2
justfile
@@ -13,7 +13,7 @@ test-only file:
|
|||||||
|
|
||||||
publish: build
|
publish: build
|
||||||
# publish to jsr first because it is more strict and will catch some errors
|
# publish to jsr first because it is more strict and will catch some errors
|
||||||
perl -i -0pe "s/},\n \"optionalDependencies\": {\n/,/" package.json
|
jq 'delete(.optionalDependencies)' package.json | sponge package.json
|
||||||
jsr publish --allow-dirty
|
jsr publish --allow-dirty
|
||||||
git checkout -- package.json
|
git checkout -- package.json
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,16 @@ import { decrypt } from './nip04.ts'
|
|||||||
import { NWCWalletRequest } from './kinds.ts'
|
import { NWCWalletRequest } from './kinds.ts'
|
||||||
|
|
||||||
describe('parseConnectionString', () => {
|
describe('parseConnectionString', () => {
|
||||||
|
test('returns pubkey, relay, and secret if connection string has double slash', () => {
|
||||||
|
const connectionString =
|
||||||
|
'nostr+walletconnect://b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
|
||||||
|
const { pubkey, relay, secret } = parseConnectionString(connectionString)
|
||||||
|
|
||||||
|
expect(pubkey).toBe('b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4')
|
||||||
|
expect(relay).toBe('wss://relay.damus.io')
|
||||||
|
expect(secret).toBe('71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c')
|
||||||
|
})
|
||||||
|
|
||||||
test('returns pubkey, relay, and secret if connection string is valid', () => {
|
test('returns pubkey, relay, and secret if connection string is valid', () => {
|
||||||
const connectionString =
|
const connectionString =
|
||||||
'nostr+walletconnect:b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
|
'nostr+walletconnect:b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io&secret=71a8c14c1407c113601079c4302dab36460f0ccd0ad506f1f2dc73b5100e4f3c'
|
||||||
|
|||||||
4
nip47.ts
4
nip47.ts
@@ -9,8 +9,8 @@ interface NWCConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function parseConnectionString(connectionString: string): NWCConnection {
|
export function parseConnectionString(connectionString: string): NWCConnection {
|
||||||
const { pathname, searchParams } = new URL(connectionString)
|
const { host, pathname, searchParams } = new URL(connectionString)
|
||||||
const pubkey = pathname
|
const pubkey = pathname || host
|
||||||
const relay = searchParams.get('relay')
|
const relay = searchParams.get('relay')
|
||||||
const secret = searchParams.get('secret')
|
const secret = searchParams.get('secret')
|
||||||
|
|
||||||
|
|||||||
29
node-ws-relay.ts
Normal file
29
node-ws-relay.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import WebSocket from 'ws'
|
||||||
|
import { verifyEvent } from './pure.ts'
|
||||||
|
import { AbstractRelay, WebSocketBase } from './abstract-relay.ts'
|
||||||
|
|
||||||
|
class NodeWs extends WebSocket implements WebSocketBase {
|
||||||
|
constructor(url: string | URL, protocols?: string | string[] | undefined) {
|
||||||
|
super(url, {
|
||||||
|
protocol: Array.isArray(protocols) ? protocols[0] : protocols,
|
||||||
|
})
|
||||||
|
|
||||||
|
setInterval(() => {
|
||||||
|
this.ping()
|
||||||
|
}, 29000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NodeWsRelay extends AbstractRelay {
|
||||||
|
constructor(url: string) {
|
||||||
|
super(url, { verifyEvent, websocketImplementation: NodeWs })
|
||||||
|
}
|
||||||
|
|
||||||
|
static async connect(url: string): Promise<NodeWsRelay> {
|
||||||
|
const relay = new NodeWsRelay(url)
|
||||||
|
await relay.connect()
|
||||||
|
return relay
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export * from './abstract-relay.ts'
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "2.14.2",
|
"version": "2.15.0",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -241,12 +241,15 @@
|
|||||||
"@noble/hashes": "1.3.1",
|
"@noble/hashes": "1.3.1",
|
||||||
"@scure/base": "1.1.1",
|
"@scure/base": "1.1.1",
|
||||||
"@scure/bip32": "1.3.1",
|
"@scure/bip32": "1.3.1",
|
||||||
"@scure/bip39": "1.2.1",
|
"@scure/bip39": "1.2.1"
|
||||||
"nostr-wasm": "0.1.0"
|
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"typescript": ">=5.0.0"
|
"typescript": ">=5.0.0"
|
||||||
},
|
},
|
||||||
|
"optionalDependencies": {
|
||||||
|
"nostr-wasm": "0.1.0",
|
||||||
|
"ws": "^8.18.3"
|
||||||
|
},
|
||||||
"peerDependenciesMeta": {
|
"peerDependenciesMeta": {
|
||||||
"typescript": {
|
"typescript": {
|
||||||
"optional": true
|
"optional": true
|
||||||
|
|||||||
16
relay.ts
16
relay.ts
@@ -1,21 +1,13 @@
|
|||||||
/* global WebSocket */
|
/* global WebSocket */
|
||||||
|
|
||||||
import { verifyEvent } from './pure.ts'
|
import { verifyEvent } from './pure.ts'
|
||||||
import { AbstractRelay } from './abstract-relay.ts'
|
import { AbstractRelay, WebSocketBaseConn } from './abstract-relay.ts'
|
||||||
|
|
||||||
var _WebSocket: typeof WebSocket
|
class BrowserWs extends WebSocket implements WebSocketBase, WebSocketBaseConn {}
|
||||||
|
|
||||||
try {
|
|
||||||
_WebSocket = WebSocket
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
export function useWebSocketImplementation(websocketImplementation: any) {
|
|
||||||
_WebSocket = websocketImplementation
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Relay extends AbstractRelay {
|
export class Relay extends AbstractRelay {
|
||||||
constructor(url: string) {
|
constructor(url: string) {
|
||||||
super(url, { verifyEvent, websocketImplementation: _WebSocket })
|
super(url, { verifyEvent, websocketImplementation: BrowserWs })
|
||||||
}
|
}
|
||||||
|
|
||||||
static async connect(url: string): Promise<Relay> {
|
static async connect(url: string): Promise<Relay> {
|
||||||
@@ -25,6 +17,4 @@ export class Relay extends AbstractRelay {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RelayRecord = Record<string, { read: boolean; write: boolean }>
|
|
||||||
|
|
||||||
export * from './abstract-relay.ts'
|
export * from './abstract-relay.ts'
|
||||||
|
|||||||
Reference in New Issue
Block a user