mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
6 Commits
e959409c14
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca36ae9530 | ||
|
|
0b6543e1a8 | ||
|
|
693b262b7c | ||
|
|
85c964be3d | ||
|
|
de7d459f6f | ||
|
|
21ec5bb2dc |
15
README.md
15
README.md
@@ -160,16 +160,7 @@ Using both `enablePing: true` and `enableReconnect: true` is recommended as it w
|
|||||||
const pool = new SimplePool({ enablePing: true, enableReconnect: true })
|
const pool = new SimplePool({ enablePing: true, enableReconnect: true })
|
||||||
```
|
```
|
||||||
|
|
||||||
The `enableReconnect` option can also be a callback function which will receive the current subscription filters and should return a new set of filters. This is useful if you want to modify the subscription on reconnect, for example, to update the `since` parameter to fetch only new events.
|
When reconnecting, all existing subscriptions will have their filters automatically updated with `since:` set to the timestamp of the last event received on them `+1`, then restarted.
|
||||||
|
|
||||||
```js
|
|
||||||
const pool = new SimplePool({
|
|
||||||
enableReconnect: (filters) => {
|
|
||||||
const newSince = Math.floor(Date.now() / 1000)
|
|
||||||
return filters.map(filter => ({ ...filter, since: newSince }))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
### Parsing references (mentions) from a content based on NIP-27
|
### Parsing references (mentions) from a content based on NIP-27
|
||||||
|
|
||||||
@@ -253,7 +244,7 @@ const event = await bunker.signEvent({
|
|||||||
await signer.close()
|
await signer.close()
|
||||||
pool.close([])
|
pool.close([])
|
||||||
```
|
```
|
||||||
> **Note on Reconnecting:** Once a connection has been successfully established and the `BunkerPointer` is stored, you do **not** need to call `await bunker.connect()` on subsequent sessions.
|
> **Note on Reconnecting:** Once a connection has been successfully established and the `BunkerPointer` is stored, you do **not** need to call `await bunker.connect()` on subsequent sessions.
|
||||||
|
|
||||||
### Method 2: Using a Client-generated URI (`nostrconnect://`)
|
### Method 2: Using a Client-generated URI (`nostrconnect://`)
|
||||||
|
|
||||||
@@ -293,7 +284,7 @@ const event = await signer.signEvent({
|
|||||||
await signer.close()
|
await signer.close()
|
||||||
pool.close([])
|
pool.close([])
|
||||||
```
|
```
|
||||||
> **Note on Persistence:** This method is ideal for the initial sign-in. To allow users to stay logged in across sessions, you should store the connection details and use `Method 1` for subsequent reconnections.
|
> **Note on Persistence:** This method is ideal for the initial sign-in. To allow users to stay logged in across sessions, you should store the connection details and use `Method 1` for subsequent reconnections.
|
||||||
|
|
||||||
### Parsing thread from any note based on NIP-10
|
### Parsing thread from any note based on NIP-10
|
||||||
|
|
||||||
|
|||||||
@@ -14,14 +14,17 @@ import { alwaysTrue } from './helpers.ts'
|
|||||||
|
|
||||||
export type SubCloser = { close: (reason?: string) => void }
|
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'> & {
|
export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose'> & {
|
||||||
maxWait?: number
|
maxWait?: number
|
||||||
onclose?: (reasons: string[]) => void
|
onclose?: (reasons: string[]) => void
|
||||||
onauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
onauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
||||||
// Deprecated: use onauth instead
|
|
||||||
doauth?: (event: EventTemplate) => Promise<VerifiedEvent>
|
|
||||||
id?: string
|
id?: string
|
||||||
label?: string
|
label?: string
|
||||||
}
|
}
|
||||||
@@ -33,7 +36,8 @@ export class AbstractSimplePool {
|
|||||||
|
|
||||||
public verifyEvent: Nostr['verifyEvent']
|
public verifyEvent: Nostr['verifyEvent']
|
||||||
public enablePing: boolean | undefined
|
public enablePing: boolean | undefined
|
||||||
public enableReconnect: boolean | ((filters: Filter[]) => Filter[]) | undefined
|
public enableReconnect: boolean
|
||||||
|
public automaticallyAuth?: (relayURL: string) => null | ((event: EventTemplate) => Promise<VerifiedEvent>)
|
||||||
public trustedRelayURLs: Set<string> = new Set()
|
public trustedRelayURLs: Set<string> = new Set()
|
||||||
|
|
||||||
private _WebSocket?: typeof WebSocket
|
private _WebSocket?: typeof WebSocket
|
||||||
@@ -42,7 +46,8 @@ export class AbstractSimplePool {
|
|||||||
this.verifyEvent = opts.verifyEvent
|
this.verifyEvent = opts.verifyEvent
|
||||||
this._WebSocket = opts.websocketImplementation
|
this._WebSocket = opts.websocketImplementation
|
||||||
this.enablePing = opts.enablePing
|
this.enablePing = opts.enablePing
|
||||||
this.enableReconnect = opts.enableReconnect
|
this.enableReconnect = opts.enableReconnect || false
|
||||||
|
this.automaticallyAuth = opts.automaticallyAuth
|
||||||
}
|
}
|
||||||
|
|
||||||
async ensureRelay(url: string, params?: { connectionTimeout?: number }): Promise<AbstractRelay> {
|
async ensureRelay(url: string, params?: { connectionTimeout?: number }): Promise<AbstractRelay> {
|
||||||
@@ -64,6 +69,14 @@ export class AbstractSimplePool {
|
|||||||
if (params?.connectionTimeout) relay.connectionTimeout = params.connectionTimeout
|
if (params?.connectionTimeout) relay.connectionTimeout = params.connectionTimeout
|
||||||
this.relays.set(url, relay)
|
this.relays.set(url, relay)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.automaticallyAuth) {
|
||||||
|
const authSignerFn = this.automaticallyAuth(url)
|
||||||
|
if (authSignerFn) {
|
||||||
|
relay.onauth = authSignerFn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await relay.connect()
|
await relay.connect()
|
||||||
|
|
||||||
return relay
|
return relay
|
||||||
@@ -77,8 +90,6 @@ export class AbstractSimplePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subscribe(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
subscribe(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
||||||
params.onauth = params.onauth || params.doauth
|
|
||||||
|
|
||||||
const request: { url: string; filter: Filter }[] = []
|
const request: { url: string; filter: Filter }[] = []
|
||||||
for (let i = 0; i < relays.length; i++) {
|
for (let i = 0; i < relays.length; i++) {
|
||||||
const url = normalizeURL(relays[i])
|
const url = normalizeURL(relays[i])
|
||||||
@@ -91,8 +102,6 @@ export class AbstractSimplePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subscribeMany(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
subscribeMany(relays: string[], filter: Filter, params: SubscribeManyParams): SubCloser {
|
||||||
params.onauth = params.onauth || params.doauth
|
|
||||||
|
|
||||||
const request: { url: string; filter: Filter }[] = []
|
const request: { url: string; filter: Filter }[] = []
|
||||||
const uniqUrls: string[] = []
|
const uniqUrls: string[] = []
|
||||||
for (let i = 0; i < relays.length; i++) {
|
for (let i = 0; i < relays.length; i++) {
|
||||||
@@ -107,8 +116,6 @@ 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
|
|
||||||
|
|
||||||
const grouped = new Map<string, Filter[]>()
|
const grouped = new Map<string, Filter[]>()
|
||||||
for (const req of requests) {
|
for (const req of requests) {
|
||||||
const { url, filter } = req
|
const { url, filter } = req
|
||||||
@@ -221,10 +228,8 @@ export class AbstractSimplePool {
|
|||||||
subscribeEose(
|
subscribeEose(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth' | 'doauth'>,
|
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth'>,
|
||||||
): SubCloser {
|
): SubCloser {
|
||||||
params.onauth = params.onauth || params.doauth
|
|
||||||
|
|
||||||
const subcloser = this.subscribe(relays, filter, {
|
const subcloser = this.subscribe(relays, filter, {
|
||||||
...params,
|
...params,
|
||||||
oneose() {
|
oneose() {
|
||||||
@@ -237,10 +242,8 @@ export class AbstractSimplePool {
|
|||||||
subscribeManyEose(
|
subscribeManyEose(
|
||||||
relays: string[],
|
relays: string[],
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth' | 'doauth'>,
|
params: Pick<SubscribeManyParams, 'label' | 'id' | 'onevent' | 'onclose' | 'maxWait' | 'onauth'>,
|
||||||
): SubCloser {
|
): SubCloser {
|
||||||
params.onauth = params.onauth || params.doauth
|
|
||||||
|
|
||||||
const subcloser = this.subscribeMany(relays, filter, {
|
const subcloser = this.subscribeMany(relays, filter, {
|
||||||
...params,
|
...params,
|
||||||
oneose() {
|
oneose() {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ export type AbstractRelayConstructorOptions = {
|
|||||||
verifyEvent: Nostr['verifyEvent']
|
verifyEvent: Nostr['verifyEvent']
|
||||||
websocketImplementation?: typeof WebSocket
|
websocketImplementation?: typeof WebSocket
|
||||||
enablePing?: boolean
|
enablePing?: boolean
|
||||||
enableReconnect?: boolean | ((filters: Filter[]) => Filter[])
|
enableReconnect?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SendingOnClosedConnection extends Error {
|
export class SendingOnClosedConnection extends Error {
|
||||||
@@ -32,19 +32,20 @@ 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}`)
|
||||||
|
public onauth: undefined | ((evt: EventTemplate) => Promise<VerifiedEvent>)
|
||||||
|
|
||||||
public baseEoseTimeout: number = 4400
|
public baseEoseTimeout: number = 4400
|
||||||
public connectionTimeout: number = 4400
|
public connectionTimeout: number = 4400
|
||||||
public publishTimeout: number = 4400
|
public publishTimeout: number = 4400
|
||||||
public pingFrequency: number = 20000
|
public pingFrequency: number = 29000
|
||||||
public pingTimeout: number = 20000
|
public pingTimeout: number = 20000
|
||||||
public resubscribeBackoff: number[] = [10000, 10000, 10000, 20000, 20000, 30000, 60000]
|
public resubscribeBackoff: number[] = [10000, 10000, 10000, 20000, 20000, 30000, 60000]
|
||||||
public openSubs: Map<string, Subscription> = new Map()
|
public openSubs: Map<string, Subscription> = new Map()
|
||||||
public enablePing: boolean | undefined
|
public enablePing: boolean | undefined
|
||||||
public enableReconnect: boolean | ((filters: Filter[]) => Filter[])
|
public enableReconnect: boolean
|
||||||
private connectionTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
private connectionTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
||||||
private reconnectTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
private reconnectTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
||||||
private pingTimeoutHandle: ReturnType<typeof setTimeout> | undefined
|
private pingIntervalHandle: ReturnType<typeof setInterval> | undefined
|
||||||
private reconnectAttempts: number = 0
|
private reconnectAttempts: number = 0
|
||||||
private closedIntentionally: boolean = false
|
private closedIntentionally: boolean = false
|
||||||
|
|
||||||
@@ -110,9 +111,9 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private handleHardClose(reason: string) {
|
private handleHardClose(reason: string) {
|
||||||
if (this.pingTimeoutHandle) {
|
if (this.pingIntervalHandle) {
|
||||||
clearTimeout(this.pingTimeoutHandle)
|
clearInterval(this.pingIntervalHandle)
|
||||||
this.pingTimeoutHandle = undefined
|
this.pingIntervalHandle = undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
this._connected = false
|
this._connected = false
|
||||||
@@ -158,19 +159,25 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
clearTimeout(this.connectionTimeoutHandle)
|
clearTimeout(this.connectionTimeoutHandle)
|
||||||
this._connected = true
|
this._connected = true
|
||||||
|
|
||||||
|
const isReconnection = this.reconnectAttempts > 0
|
||||||
this.reconnectAttempts = 0
|
this.reconnectAttempts = 0
|
||||||
|
|
||||||
// resubscribe to all open subscriptions
|
// resubscribe to all open subscriptions
|
||||||
for (const sub of this.openSubs.values()) {
|
for (const sub of this.openSubs.values()) {
|
||||||
sub.eosed = false
|
sub.eosed = false
|
||||||
if (typeof this.enableReconnect === 'function') {
|
if (isReconnection) {
|
||||||
sub.filters = this.enableReconnect(sub.filters)
|
for (let f = 0; f < sub.filters.length; f++) {
|
||||||
|
if (sub.lastEmitted) {
|
||||||
|
sub.filters[f].since = sub.lastEmitted + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
sub.fire()
|
sub.fire()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.enablePing) {
|
if (this.enablePing) {
|
||||||
this.pingpong()
|
this.pingIntervalHandle = setInterval(() => this.pingpong(), this.pingFrequency)
|
||||||
}
|
}
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
@@ -202,17 +209,30 @@ export class AbstractRelay {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private async waitForDummyReq() {
|
private waitForDummyReq() {
|
||||||
return new Promise((resolve, _) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!this.connectionPromise) return reject(new Error(`no connection to ${this.url}, can't ping`))
|
||||||
|
|
||||||
// make a dummy request with expected empty eose reply
|
// make a dummy request with expected empty eose reply
|
||||||
// ["REQ", "_", {"ids":["aaaa...aaaa"]}]
|
// ["REQ", "_", {"ids":["aaaa...aaaa"], "limit": 0}]
|
||||||
const sub = this.subscribe([{ ids: ['a'.repeat(64)] }], {
|
try {
|
||||||
oneose: () => {
|
const sub = this.subscribe(
|
||||||
sub.close()
|
[{ ids: ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], limit: 0 }],
|
||||||
resolve(true)
|
{
|
||||||
},
|
oneose: () => {
|
||||||
eoseTimeout: this.pingTimeout + 1000,
|
resolve(true)
|
||||||
})
|
sub.close()
|
||||||
|
},
|
||||||
|
onclose() {
|
||||||
|
// if we get a CLOSED it's because the relay is alive
|
||||||
|
resolve(true)
|
||||||
|
},
|
||||||
|
eoseTimeout: this.pingTimeout + 1000,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
} catch (err) {
|
||||||
|
reject(err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,10 +247,8 @@ export class AbstractRelay {
|
|||||||
this.ws && this.ws.ping && (this.ws as any).once ? this.waitForPingPong() : this.waitForDummyReq(),
|
this.ws && this.ws.ping && (this.ws as any).once ? this.waitForPingPong() : this.waitForDummyReq(),
|
||||||
new Promise(res => setTimeout(() => res(false), this.pingTimeout)),
|
new Promise(res => setTimeout(() => res(false), this.pingTimeout)),
|
||||||
])
|
])
|
||||||
if (result) {
|
|
||||||
// schedule another pingpong
|
if (!result) {
|
||||||
this.pingTimeoutHandle = setTimeout(() => this.pingpong(), this.pingFrequency)
|
|
||||||
} else {
|
|
||||||
// pingpong closing socket
|
// pingpong closing socket
|
||||||
if (this.ws?.readyState === this._WebSocket.OPEN) {
|
if (this.ws?.readyState === this._WebSocket.OPEN) {
|
||||||
this.ws?.close()
|
this.ws?.close()
|
||||||
@@ -293,6 +311,7 @@ export class AbstractRelay {
|
|||||||
if (this.verifyEvent(event) && matchFilters(so.filters, event)) {
|
if (this.verifyEvent(event) && matchFilters(so.filters, event)) {
|
||||||
so.onevent(event)
|
so.onevent(event)
|
||||||
}
|
}
|
||||||
|
if (!so.lastEmitted || so.lastEmitted < event.created_at) so.lastEmitted = event.created_at
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case 'COUNT': {
|
case 'COUNT': {
|
||||||
@@ -338,6 +357,9 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
case 'AUTH': {
|
case 'AUTH': {
|
||||||
this.challenge = data[1] as string
|
this.challenge = data[1] as string
|
||||||
|
if (this.onauth) {
|
||||||
|
this.auth(this.onauth)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
@@ -434,9 +456,9 @@ export class AbstractRelay {
|
|||||||
clearTimeout(this.reconnectTimeoutHandle)
|
clearTimeout(this.reconnectTimeoutHandle)
|
||||||
this.reconnectTimeoutHandle = undefined
|
this.reconnectTimeoutHandle = undefined
|
||||||
}
|
}
|
||||||
if (this.pingTimeoutHandle) {
|
if (this.pingIntervalHandle) {
|
||||||
clearTimeout(this.pingTimeoutHandle)
|
clearInterval(this.pingIntervalHandle)
|
||||||
this.pingTimeoutHandle = undefined
|
this.pingIntervalHandle = undefined
|
||||||
}
|
}
|
||||||
this.closeAllSubscriptions('relay connection closed by us')
|
this.closeAllSubscriptions('relay connection closed by us')
|
||||||
this._connected = false
|
this._connected = false
|
||||||
@@ -460,6 +482,7 @@ export class Subscription {
|
|||||||
public readonly relay: AbstractRelay
|
public readonly relay: AbstractRelay
|
||||||
public readonly id: string
|
public readonly id: string
|
||||||
|
|
||||||
|
public lastEmitted: number | undefined
|
||||||
public closed: boolean = false
|
public closed: boolean = false
|
||||||
public eosed: boolean = false
|
public eosed: boolean = false
|
||||||
public filters: Filter[]
|
public filters: Filter[]
|
||||||
|
|||||||
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nostr/tools",
|
"name": "@nostr/tools",
|
||||||
"version": "2.18.2",
|
"version": "2.19.2",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./index.ts",
|
".": "./index.ts",
|
||||||
"./core": "./core.ts",
|
"./core": "./core.ts",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "2.18.2",
|
"version": "2.19.2",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
@@ -306,12 +306,9 @@ test('reconnect on disconnect in pool', async () => {
|
|||||||
|
|
||||||
test('reconnect with filter update in pool', async () => {
|
test('reconnect with filter update in pool', async () => {
|
||||||
const mockRelay = mockRelays[0]
|
const mockRelay = mockRelays[0]
|
||||||
const newSince = Math.floor(Date.now() / 1000)
|
|
||||||
pool = new SimplePool({
|
pool = new SimplePool({
|
||||||
enablePing: true,
|
enablePing: true,
|
||||||
enableReconnect: filters => {
|
enableReconnect: true,
|
||||||
return filters.map(f => ({ ...f, since: newSince }))
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
const relay = await pool.ensureRelay(mockRelay.url)
|
const relay = await pool.ensureRelay(mockRelay.url)
|
||||||
relay.pingTimeout = 50
|
relay.pingTimeout = 50
|
||||||
@@ -364,7 +361,7 @@ test('reconnect with filter update in pool', async () => {
|
|||||||
expect(closes).toBe(1)
|
expect(closes).toBe(1)
|
||||||
|
|
||||||
// check if filter was updated
|
// check if filter was updated
|
||||||
expect(sub.filters[0].since).toBe(newSince)
|
expect(sub.filters[0].since).toBeGreaterThan(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('track relays when publishing', async () => {
|
test('track relays when publishing', async () => {
|
||||||
|
|||||||
@@ -336,66 +336,3 @@ test('reconnect on disconnect', async () => {
|
|||||||
expect(relay.connected).toBeTrue()
|
expect(relay.connected).toBeTrue()
|
||||||
expect(closes).toBe(1) // should not have closed again
|
expect(closes).toBe(1) // should not have closed again
|
||||||
})
|
})
|
||||||
|
|
||||||
test('reconnect with filter update', async () => {
|
|
||||||
const mockRelay = new MockRelay()
|
|
||||||
const newSince = Math.floor(Date.now() / 1000)
|
|
||||||
const relay = new Relay(mockRelay.url, {
|
|
||||||
enablePing: true,
|
|
||||||
enableReconnect: filters => {
|
|
||||||
return filters.map(f => ({ ...f, since: newSince }))
|
|
||||||
},
|
|
||||||
})
|
|
||||||
relay.pingTimeout = 50
|
|
||||||
relay.pingFrequency = 50
|
|
||||||
relay.resubscribeBackoff = [50, 100]
|
|
||||||
|
|
||||||
let closes = 0
|
|
||||||
relay.onclose = () => {
|
|
||||||
closes++
|
|
||||||
}
|
|
||||||
|
|
||||||
await relay.connect()
|
|
||||||
expect(relay.connected).toBeTrue()
|
|
||||||
|
|
||||||
const sub = relay.subscribe([{ kinds: [1], since: 0 }], { onevent: () => {} })
|
|
||||||
expect(sub.filters[0].since).toBe(0)
|
|
||||||
|
|
||||||
// wait for the first ping to succeed
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 75))
|
|
||||||
expect(closes).toBe(0)
|
|
||||||
|
|
||||||
// now make it unresponsive
|
|
||||||
mockRelay.unresponsive = true
|
|
||||||
|
|
||||||
// wait for the second ping to fail, which will trigger a close
|
|
||||||
await new Promise(resolve => {
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
if (closes > 0) {
|
|
||||||
clearInterval(interval)
|
|
||||||
resolve(null)
|
|
||||||
}
|
|
||||||
}, 10)
|
|
||||||
})
|
|
||||||
expect(closes).toBe(1)
|
|
||||||
expect(relay.connected).toBeFalse()
|
|
||||||
|
|
||||||
// now make it responsive again
|
|
||||||
mockRelay.unresponsive = false
|
|
||||||
|
|
||||||
// wait for reconnect
|
|
||||||
await new Promise(resolve => {
|
|
||||||
const interval = setInterval(() => {
|
|
||||||
if (relay.connected) {
|
|
||||||
clearInterval(interval)
|
|
||||||
resolve(null)
|
|
||||||
}
|
|
||||||
}, 10)
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(relay.connected).toBeTrue()
|
|
||||||
expect(closes).toBe(1)
|
|
||||||
|
|
||||||
// check if filter was updated
|
|
||||||
expect(sub.filters[0].since).toBe(newSince)
|
|
||||||
})
|
|
||||||
|
|||||||
Reference in New Issue
Block a user