mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
2 Commits
e959409c14
...
v2.19.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de7d459f6f | ||
|
|
21ec5bb2dc |
@@ -14,14 +14,17 @@ import { alwaysTrue } from './helpers.ts'
|
||||
|
||||
export type SubCloser = { close: (reason?: string) => void }
|
||||
|
||||
export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {}
|
||||
export type AbstractPoolConstructorOptions = AbstractRelayConstructorOptions & {
|
||||
// automaticallyAuth takes a relay URL and should return null
|
||||
// in case that relay shouldn't be authenticated against
|
||||
// or a function to sign the AUTH event template otherwise (that function may still throw in case of failure)
|
||||
automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
|
||||
}
|
||||
|
||||
export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
|
||||
maxWait?: number
|
||||
onclose?: (reasons: string[]) => void
|
||||
onauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
||||
// Deprecated: use onauth instead
|
||||
doauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
||||
id?: string
|
||||
label?: string
|
||||
}
|
||||
@@ -34,6 +37,7 @@ export class AbstractSimplePool {
|
||||
public verifyEvent: Nostr['verifyEvent']
|
||||
public enablePing: boolean | undefined
|
||||
public enableReconnect: boolean | ((filters: Filter[]) => Filter[]) | undefined
|
||||
public automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
|
||||
public trustedRelayURLs: Set<string> = new Set()
|
||||
|
||||
private _WebSocket?: typeof WebSocket
|
||||
@@ -43,6 +47,7 @@ export class AbstractSimplePool {
|
||||
this._WebSocket = opts.websocketImplementation
|
||||
this.enablePing = opts.enablePing
|
||||
this.enableReconnect = opts.enableReconnect
|
||||
this.automaticallyAuth = opts.automaticallyAuth
|
||||
}
|
||||
|
||||
async ensureRelay(url: string, params?: { connectionTimeout?: number }): Promise<AbstractRelay> {
|
||||
@@ -64,6 +69,14 @@ export class AbstractSimplePool {
|
||||
if (params?.connectionTimeout) relay.connectionTimeout = params.connectionTimeout
|
||||
this.relays.set(url, relay)
|
||||
}
|
||||
|
||||
if (this.automaticallyAuth) {
|
||||
const authSignerFn = this.automaticallyAuth(url)
|
||||
if (authSignerFn) {
|
||||
relay.onauth = authSignerFn
|
||||
}
|
||||
}
|
||||
|
||||
await relay.connect()
|
||||
|
||||
return relay
|
||||
@@ -77,8 +90,6 @@ export class AbstractSimplePool {
|
||||
}
|
||||
|
||||
subscribe(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
||||
params.onauth = params.onauth || params.doauth
|
||||
|
||||
const request: { url: string; filter: Filter }[] = []
|
||||
for (let i = 0; i < relays.length; i++) {
|
||||
const url = normalizeURL(relays[i])
|
||||
@@ -91,8 +102,6 @@ export class AbstractSimplePool {
|
||||
}
|
||||
|
||||
subscribeMany(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
||||
params.onauth = params.onauth || params.doauth
|
||||
|
||||
const request: { url: string; filter: Filter }[] = []
|
||||
const uniqUrls: string[] = []
|
||||
for (let i = 0; i < relays.length; i++) {
|
||||
@@ -107,8 +116,6 @@ export class AbstractSimplePool {
|
||||
}
|
||||
|
||||
subscribeMap(requests: { url: string; filter: Filter }[], params: SubscribeManyParams): SubCloser {
|
||||
params.onauth = params.onauth || params.doauth
|
||||
|
||||
const grouped = new Map<string, Filter[]>()
|
||||
for (const req of requests) {
|
||||
const { url, filter } = req
|
||||
@@ -221,10 +228,8 @@ export class AbstractSimplePool {
|
||||
subscribeEose(
|
||||
relays: string[],
|
||||
filter: Filter,
|
||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth' | 'doauth'>,
|
||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth'>,
|
||||
): SubCloser {
|
||||
params.onauth = params.onauth || params.doauth
|
||||
|
||||
const subcloser = this.subscribe(relays, filter, {
|
||||
...params,
|
||||
oneose() {
|
||||
@@ -237,10 +242,8 @@ export class AbstractSimplePool {
|
||||
subscribeManyEose(
|
||||
relays: string[],
|
||||
filter: Filter,
|
||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth' | 'doauth'>,
|
||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth'>,
|
||||
): SubCloser {
|
||||
params.onauth = params.onauth || params.doauth
|
||||
|
||||
const subcloser = this.subscribeMany(relays, filter, {
|
||||
...params,
|
||||
oneose() {
|
||||
|
||||
@@ -32,6 +32,7 @@ export class AbstractRelay {
|
||||
|
||||
public onclose: (() => void) | null = null
|
||||
public onnotice: (msg: string) => void = msg => console.debug(`NOTICE from ${this.url}: ${msg}`)
|
||||
public onauth: undefined | ((evt: EventTemplate) => Promise<VerifiedEvent>)
|
||||
|
||||
public baseEoseTimeout: number = 4400
|
||||
public connectionTimeout: number = 4400
|
||||
@@ -158,12 +159,14 @@ export class AbstractRelay {
|
||||
}
|
||||
clearTimeout(this.connectionTimeoutHandle)
|
||||
this._connected = true
|
||||
|
||||
const isReconnection = this.reconnectAttempts > 0
|
||||
this.reconnectAttempts = 0
|
||||
|
||||
// resubscribe to all open subscriptions
|
||||
for (const sub of this.openSubs.values()) {
|
||||
sub.eosed = false
|
||||
if (typeof this.enableReconnect === 'function') {
|
||||
if (isReconnection && typeof this.enableReconnect === 'function') {
|
||||
sub.filters = this.enableReconnect(sub.filters)
|
||||
}
|
||||
sub.fire()
|
||||
@@ -205,14 +208,17 @@ export class AbstractRelay {
|
||||
private async waitForDummyReq() {
|
||||
return new Promise((resolve, _) => {
|
||||
// make a dummy request with expected empty eose reply
|
||||
// ["REQ", "_", {"ids":["aaaa...aaaa"]}]
|
||||
const sub = this.subscribe([{ ids: ['a'.repeat(64)] }], {
|
||||
// ["REQ", "_", {"ids":["aaaa...aaaa"], "limit": 0}]
|
||||
const sub = this.subscribe(
|
||||
[{ ids: ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], limit: 0 }],
|
||||
{
|
||||
oneose: () => {
|
||||
sub.close()
|
||||
resolve(true)
|
||||
},
|
||||
eoseTimeout: this.pingTimeout + 1000,
|
||||
})
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -338,6 +344,9 @@ export class AbstractRelay {
|
||||
}
|
||||
case 'AUTH': {
|
||||
this.challenge = data[1] as string
|
||||
if (this.onauth) {
|
||||
this.auth(this.onauth)
|
||||
}
|
||||
return
|
||||
}
|
||||
default: {
|
||||
|
||||
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@nostr/tools",
|
||||
"version": "2.18.2",
|
||||
"version": "2.19.0",
|
||||
"exports": {
|
||||
".": "./index.ts",
|
||||
"./core": "./core.ts",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"type": "module",
|
||||
"name": "nostr-tools",
|
||||
"version": "2.18.2",
|
||||
"version": "2.19.0",
|
||||
"description": "Tools for making a Nostr client.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
Reference in New Issue
Block a user