mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 00:28:51 +00:00
Compare commits
3 Commits
85c964be3d
...
ca36ae9530
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca36ae9530 | ||
|
|
0b6543e1a8 | ||
|
|
693b262b7c |
11
README.md
11
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
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ export class AbstractRelay {
|
|||||||
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()
|
||||||
@@ -45,7 +45,7 @@ export class AbstractRelay {
|
|||||||
public enableReconnect: boolean
|
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
|
||||||
|
|
||||||
@@ -111,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
|
||||||
@@ -177,7 +177,7 @@ export class AbstractRelay {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.enablePing) {
|
if (this.enablePing) {
|
||||||
this.pingpong()
|
this.pingIntervalHandle = setInterval(() => this.pingpong(), this.pingFrequency)
|
||||||
}
|
}
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
@@ -209,20 +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"], "limit": 0}]
|
// ["REQ", "_", {"ids":["aaaa...aaaa"], "limit": 0}]
|
||||||
|
try {
|
||||||
const sub = this.subscribe(
|
const sub = this.subscribe(
|
||||||
[{ ids: ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], limit: 0 }],
|
[{ ids: ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], limit: 0 }],
|
||||||
{
|
{
|
||||||
oneose: () => {
|
oneose: () => {
|
||||||
|
resolve(true)
|
||||||
sub.close()
|
sub.close()
|
||||||
|
},
|
||||||
|
onclose() {
|
||||||
|
// if we get a CLOSED it's because the relay is alive
|
||||||
resolve(true)
|
resolve(true)
|
||||||
},
|
},
|
||||||
eoseTimeout: this.pingTimeout + 1000,
|
eoseTimeout: this.pingTimeout + 1000,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
} catch (err) {
|
||||||
|
reject(err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,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()
|
||||||
@@ -448,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
|
||||||
|
|||||||
2
jsr.json
2
jsr.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@nostr/tools",
|
"name": "@nostr/tools",
|
||||||
"version": "2.19.1",
|
"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.19.1",
|
"version": "2.19.2",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
Reference in New Issue
Block a user