libp2p 0.45.5 → 0.45.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +21 -21
- package/dist/src/autonat/index.js +4 -4
- package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.js +11 -4
- package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +5 -0
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.js +14 -2
- package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts +3 -0
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dial.js +64 -27
- package/dist/src/connection-manager/auto-dial.js.map +1 -1
- package/dist/src/connection-manager/constants.d.ts +4 -0
- package/dist/src/connection-manager/constants.d.ts.map +1 -1
- package/dist/src/connection-manager/constants.js +4 -0
- package/dist/src/connection-manager/constants.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +5 -0
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +11 -10
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/upnp-nat/index.d.ts +1 -1
- package/dist/src/upnp-nat/index.d.ts.map +1 -1
- package/dist/src/upnp-nat/index.js +3 -3
- package/dist/src/upnp-nat/index.js.map +1 -1
- package/dist/src/utils/peer-job-queue.d.ts +33 -0
- package/dist/src/utils/peer-job-queue.d.ts.map +1 -0
- package/dist/src/utils/peer-job-queue.js +82 -0
- package/dist/src/utils/peer-job-queue.js.map +1 -0
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.js +1 -1
- package/package.json +7 -6
- package/src/autonat/index.ts +4 -4
- package/src/circuit-relay/transport/discovery.ts +11 -4
- package/src/circuit-relay/transport/reservation-store.ts +22 -3
- package/src/connection-manager/auto-dial.ts +75 -31
- package/src/connection-manager/constants.ts +5 -0
- package/src/connection-manager/index.ts +18 -12
- package/src/upnp-nat/index.ts +3 -3
- package/src/utils/peer-job-queue.ts +119 -0
- package/src/version.ts +1 -1
package/src/upnp-nat/index.ts
CHANGED
|
@@ -142,7 +142,7 @@ class UPnPNAT implements Startable {
|
|
|
142
142
|
continue
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
const client =
|
|
145
|
+
const client = this._getClient()
|
|
146
146
|
const publicIp = this.externalAddress ?? await client.externalIp()
|
|
147
147
|
const isPrivate = isPrivateIp(publicIp)
|
|
148
148
|
|
|
@@ -173,12 +173,12 @@ class UPnPNAT implements Startable {
|
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
-
|
|
176
|
+
_getClient (): NatAPI {
|
|
177
177
|
if (this.client != null) {
|
|
178
178
|
return this.client
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
-
this.client =
|
|
181
|
+
this.client = upnpNat({
|
|
182
182
|
description: this.description,
|
|
183
183
|
ttl: this.ttl,
|
|
184
184
|
keepAlive: this.keepAlive,
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
+
|
|
3
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
4
|
+
import PQueue from 'p-queue'
|
|
5
|
+
import { codes } from '../errors.js'
|
|
6
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
7
|
+
import type { QueueAddOptions, Options, Queue } from 'p-queue'
|
|
8
|
+
|
|
9
|
+
// Port of lower_bound from https://en.cppreference.com/w/cpp/algorithm/lower_bound
|
|
10
|
+
// Used to compute insertion index to keep queue sorted after insertion
|
|
11
|
+
function lowerBound<T> (array: readonly T[], value: T, comparator: (a: T, b: T) => number): number {
|
|
12
|
+
let first = 0
|
|
13
|
+
let count = array.length
|
|
14
|
+
|
|
15
|
+
while (count > 0) {
|
|
16
|
+
const step = Math.trunc(count / 2)
|
|
17
|
+
let it = first + step
|
|
18
|
+
|
|
19
|
+
if (comparator(array[it]!, value) <= 0) {
|
|
20
|
+
first = ++it
|
|
21
|
+
count -= step + 1
|
|
22
|
+
} else {
|
|
23
|
+
count = step
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return first
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface RunFunction { (): Promise<unknown> }
|
|
31
|
+
|
|
32
|
+
export interface PeerPriorityQueueOptions extends QueueAddOptions {
|
|
33
|
+
peerId: PeerId
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface PeerJob {
|
|
37
|
+
priority: number
|
|
38
|
+
peerId: PeerId
|
|
39
|
+
run: RunFunction
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Port of https://github.com/sindresorhus/p-queue/blob/main/source/priority-queue.ts
|
|
44
|
+
* that adds support for filtering jobs by peer id
|
|
45
|
+
*/
|
|
46
|
+
class PeerPriorityQueue implements Queue<RunFunction, PeerPriorityQueueOptions> {
|
|
47
|
+
readonly #queue: PeerJob[] = []
|
|
48
|
+
|
|
49
|
+
enqueue (run: RunFunction, options?: Partial<PeerPriorityQueueOptions>): void {
|
|
50
|
+
const peerId = options?.peerId
|
|
51
|
+
const priority = options?.priority ?? 0
|
|
52
|
+
|
|
53
|
+
if (peerId == null) {
|
|
54
|
+
throw new CodeError('missing peer id', codes.ERR_INVALID_PARAMETERS)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const element: PeerJob = {
|
|
58
|
+
priority,
|
|
59
|
+
peerId,
|
|
60
|
+
run
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (this.size > 0 && this.#queue[this.size - 1]!.priority >= priority) {
|
|
64
|
+
this.#queue.push(element)
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const index = lowerBound(
|
|
69
|
+
this.#queue, element,
|
|
70
|
+
(a: Readonly<PeerPriorityQueueOptions>, b: Readonly<PeerPriorityQueueOptions>) => b.priority! - a.priority!
|
|
71
|
+
)
|
|
72
|
+
this.#queue.splice(index, 0, element)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
dequeue (): RunFunction | undefined {
|
|
76
|
+
const item = this.#queue.shift()
|
|
77
|
+
return item?.run
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
filter (options: Readonly<Partial<PeerPriorityQueueOptions>>): RunFunction[] {
|
|
81
|
+
if (options.peerId != null) {
|
|
82
|
+
const peerId = options.peerId
|
|
83
|
+
|
|
84
|
+
return this.#queue.filter(
|
|
85
|
+
(element: Readonly<PeerPriorityQueueOptions>) => peerId.equals(element.peerId)
|
|
86
|
+
).map((element: Readonly<{ run: RunFunction }>) => element.run)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return this.#queue.filter(
|
|
90
|
+
(element: Readonly<PeerPriorityQueueOptions>) => element.priority === options.priority
|
|
91
|
+
).map((element: Readonly<{ run: RunFunction }>) => element.run)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
get size (): number {
|
|
95
|
+
return this.#queue.length
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Extends PQueue to add support for querying queued jobs by peer id
|
|
101
|
+
*/
|
|
102
|
+
export class PeerJobQueue extends PQueue<PeerPriorityQueue, PeerPriorityQueueOptions> {
|
|
103
|
+
constructor (options: Options<PeerPriorityQueue, PeerPriorityQueueOptions> = {}) {
|
|
104
|
+
super({
|
|
105
|
+
...options,
|
|
106
|
+
queueClass: PeerPriorityQueue
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Returns true if this queue has a job for the passed peer id that has not yet
|
|
112
|
+
* started to run
|
|
113
|
+
*/
|
|
114
|
+
hasJob (peerId: PeerId): boolean {
|
|
115
|
+
return this.sizeBy({
|
|
116
|
+
peerId
|
|
117
|
+
}) > 0
|
|
118
|
+
}
|
|
119
|
+
}
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const version = '0.45.
|
|
1
|
+
export const version = '0.45.6'
|
|
2
2
|
export const name = 'libp2p'
|