libp2p 0.44.0-ba47c957 → 0.44.0-e3deaa40
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 +23 -23
- package/dist/src/autonat/index.d.ts.map +1 -1
- package/dist/src/autonat/index.js +15 -13
- package/dist/src/autonat/index.js.map +1 -1
- package/dist/src/components.d.ts +2 -66
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +60 -174
- package/dist/src/components.js.map +1 -1
- package/dist/src/config/connection-gater.browser.d.ts +8 -0
- package/dist/src/config/connection-gater.browser.d.ts.map +1 -0
- package/dist/src/config/connection-gater.browser.js +27 -0
- package/dist/src/config/connection-gater.browser.js.map +1 -0
- package/dist/src/config/connection-gater.d.ts +6 -0
- package/dist/src/config/connection-gater.d.ts.map +1 -0
- package/dist/src/config/connection-gater.js +18 -0
- package/dist/src/config/connection-gater.js.map +1 -0
- package/dist/src/config.d.ts +2 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +16 -23
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dial.js +7 -2
- package/dist/src/connection-manager/auto-dial.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +8 -10
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +3 -0
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +0 -10
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +6 -41
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/fetch/index.d.ts +13 -35
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +7 -9
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/identify/index.d.ts +13 -49
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +122 -96
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +11 -45
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +10 -35
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +110 -171
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +7 -6
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/ping/constants.d.ts +4 -0
- package/dist/src/ping/constants.d.ts.map +1 -1
- package/dist/src/ping/constants.js +10 -0
- package/dist/src/ping/constants.js.map +1 -1
- package/dist/src/ping/index.d.ts +10 -27
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +18 -21
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/registrar.js +2 -2
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +2 -1
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +4 -2
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +7 -14
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/upnp-nat/index.d.ts +42 -0
- package/dist/src/upnp-nat/index.d.ts.map +1 -0
- package/dist/src/{nat-manager.js → upnp-nat/index.js} +11 -7
- package/dist/src/upnp-nat/index.js.map +1 -0
- package/package.json +32 -17
- package/src/autonat/index.ts +14 -13
- package/src/components.ts +67 -248
- package/src/config/connection-gater.browser.ts +31 -0
- package/src/config/connection-gater.ts +19 -0
- package/src/config.ts +8 -14
- package/src/connection-manager/auto-dial.ts +8 -2
- package/src/connection-manager/dial-queue.ts +10 -16
- package/src/connection-manager/index.ts +4 -0
- package/src/content-routing/index.ts +6 -54
- package/src/fetch/index.ts +51 -14
- package/src/identify/index.ts +154 -104
- package/src/index.ts +12 -52
- package/src/libp2p.ts +102 -214
- package/src/peer-routing.ts +10 -8
- package/src/ping/constants.ts +11 -0
- package/src/ping/index.ts +32 -28
- package/src/registrar.ts +2 -2
- package/src/transport-manager.ts +5 -3
- package/src/upgrader.ts +7 -13
- package/src/{nat-manager.ts → upnp-nat/index.ts} +15 -16
- package/src/version.ts +1 -1
- package/dist/src/dht/dht-content-routing.d.ts +0 -17
- package/dist/src/dht/dht-content-routing.d.ts.map +0 -1
- package/dist/src/dht/dht-content-routing.js +0 -32
- package/dist/src/dht/dht-content-routing.js.map +0 -1
- package/dist/src/dht/dht-peer-routing.d.ts +0 -15
- package/dist/src/dht/dht-peer-routing.d.ts.map +0 -1
- package/dist/src/dht/dht-peer-routing.js +0 -26
- package/dist/src/dht/dht-peer-routing.js.map +0 -1
- package/dist/src/dht/dummy-dht.d.ts +0 -20
- package/dist/src/dht/dummy-dht.d.ts.map +0 -1
- package/dist/src/dht/dummy-dht.js +0 -46
- package/dist/src/dht/dummy-dht.js.map +0 -1
- package/dist/src/nat-manager.d.ts +0 -74
- package/dist/src/nat-manager.d.ts.map +0 -1
- package/dist/src/nat-manager.js.map +0 -1
- package/src/dht/dht-content-routing.ts +0 -44
- package/src/dht/dht-peer-routing.ts +0 -36
- package/src/dht/dummy-dht.ts +0 -60
package/src/config.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import mergeOptions from 'merge-options'
|
|
2
2
|
import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
|
|
3
|
-
import { AGENT_VERSION } from './identify/consts.js'
|
|
4
3
|
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
|
5
4
|
import { FaultTolerance } from '@libp2p/interface-transport'
|
|
6
5
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
@@ -8,7 +7,7 @@ import type { Libp2pInit } from './index.js'
|
|
|
8
7
|
import { codes, messages } from './errors.js'
|
|
9
8
|
import { CodeError } from '@libp2p/interfaces/errors'
|
|
10
9
|
import type { RecursivePartial } from '@libp2p/interfaces'
|
|
11
|
-
import {
|
|
10
|
+
import type { ServiceMap } from '@libp2p/interface-libp2p'
|
|
12
11
|
|
|
13
12
|
const DefaultConfig: Partial<Libp2pInit> = {
|
|
14
13
|
addresses: {
|
|
@@ -32,7 +31,9 @@ const DefaultConfig: Partial<Libp2pInit> = {
|
|
|
32
31
|
interval: 6e5,
|
|
33
32
|
bootDelay: 10e3
|
|
34
33
|
}
|
|
35
|
-
}
|
|
34
|
+
}
|
|
35
|
+
/*
|
|
36
|
+
|
|
36
37
|
nat: {
|
|
37
38
|
enabled: true,
|
|
38
39
|
ttl: 7200,
|
|
@@ -77,10 +78,12 @@ const DefaultConfig: Partial<Libp2pInit> = {
|
|
|
77
78
|
startupDelay: 5000,
|
|
78
79
|
refreshInterval: 60000
|
|
79
80
|
}
|
|
81
|
+
|
|
82
|
+
*/
|
|
80
83
|
}
|
|
81
84
|
|
|
82
|
-
export function validateConfig (opts: RecursivePartial<Libp2pInit
|
|
83
|
-
const resultingOptions: Libp2pInit = mergeOptions(DefaultConfig, opts)
|
|
85
|
+
export function validateConfig <T extends ServiceMap = {}> (opts: RecursivePartial<Libp2pInit<T>>): Libp2pInit<T> {
|
|
86
|
+
const resultingOptions: Libp2pInit<T> = mergeOptions(DefaultConfig, opts)
|
|
84
87
|
|
|
85
88
|
if (resultingOptions.transports == null || resultingOptions.transports.length < 1) {
|
|
86
89
|
throw new CodeError(messages.ERR_TRANSPORTS_REQUIRED, codes.ERR_TRANSPORTS_REQUIRED)
|
|
@@ -94,14 +97,5 @@ export function validateConfig (opts: RecursivePartial<Libp2pInit>): Libp2pInit
|
|
|
94
97
|
throw new CodeError(messages.ERR_PROTECTOR_REQUIRED, codes.ERR_PROTECTOR_REQUIRED)
|
|
95
98
|
}
|
|
96
99
|
|
|
97
|
-
// Append user agent version to default AGENT_VERSION depending on the environment
|
|
98
|
-
if (resultingOptions.identify.host.agentVersion === AGENT_VERSION) {
|
|
99
|
-
if (isNode || isElectronMain) {
|
|
100
|
-
resultingOptions.identify.host.agentVersion += ` UserAgent=${globalThis.process.version}`
|
|
101
|
-
} else if (isBrowser || isWebWorker || isElectronRenderer || isReactNative) {
|
|
102
|
-
resultingOptions.identify.host.agentVersion += ` UserAgent=${globalThis.navigator.userAgent}`
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
100
|
return resultingOptions
|
|
107
101
|
}
|
|
@@ -101,7 +101,8 @@ export class AutoDial implements Startable {
|
|
|
101
101
|
return
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
const
|
|
104
|
+
const connections = this.connectionManager.getConnectionsMap()
|
|
105
|
+
const numConnections = connections.size
|
|
105
106
|
|
|
106
107
|
// Already has enough connections
|
|
107
108
|
if (numConnections >= this.minConnections) {
|
|
@@ -121,6 +122,11 @@ export class AutoDial implements Startable {
|
|
|
121
122
|
return false
|
|
122
123
|
}
|
|
123
124
|
|
|
125
|
+
// remove peers we are already connected to
|
|
126
|
+
if (connections.has(peer.id)) {
|
|
127
|
+
return false
|
|
128
|
+
}
|
|
129
|
+
|
|
124
130
|
return true
|
|
125
131
|
})
|
|
126
132
|
|
|
@@ -161,7 +167,7 @@ export class AutoDial implements Startable {
|
|
|
161
167
|
|
|
162
168
|
for (const peer of sortedPeers) {
|
|
163
169
|
this.queue.add(async () => {
|
|
164
|
-
const numConnections = this.connectionManager.
|
|
170
|
+
const numConnections = this.connectionManager.getConnectionsMap().size
|
|
165
171
|
|
|
166
172
|
// Check to see if we still need to auto dial
|
|
167
173
|
if (numConnections >= this.minConnections) {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
2
|
import { AbortError, CodeError } from '@libp2p/interfaces/errors'
|
|
3
3
|
import { Multiaddr, Resolver, resolvers } from '@multiformats/multiaddr'
|
|
4
|
-
import { TimeoutController } from 'timeout-abort-controller'
|
|
5
4
|
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
|
6
5
|
import { codes } from '../errors.js'
|
|
7
6
|
import {
|
|
@@ -22,7 +21,7 @@ import PQueue from 'p-queue'
|
|
|
22
21
|
import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
|
|
23
22
|
import { combineSignals, resolveMultiaddrs } from './utils.js'
|
|
24
23
|
import pDefer from 'p-defer'
|
|
25
|
-
import
|
|
24
|
+
import { ClearableSignal, anySignal } from 'any-signal'
|
|
26
25
|
import type { AddressSorter } from '@libp2p/interface-libp2p'
|
|
27
26
|
|
|
28
27
|
const log = logger('libp2p:connection-manager:dial-queue')
|
|
@@ -176,7 +175,7 @@ export class DialQueue {
|
|
|
176
175
|
|
|
177
176
|
// create abort conditions - need to do this before `calculateMultiaddrs` as we may be about to
|
|
178
177
|
// resolve a dns addr which can time out
|
|
179
|
-
const
|
|
178
|
+
const signal = this.createDialAbortControllers(options.signal)
|
|
180
179
|
let addrsToDial: Address[]
|
|
181
180
|
|
|
182
181
|
try {
|
|
@@ -187,7 +186,6 @@ export class DialQueue {
|
|
|
187
186
|
})
|
|
188
187
|
} catch (err) {
|
|
189
188
|
signal.clear()
|
|
190
|
-
timeoutController.clear()
|
|
191
189
|
throw err
|
|
192
190
|
}
|
|
193
191
|
|
|
@@ -210,7 +208,6 @@ export class DialQueue {
|
|
|
210
208
|
if (existingDial != null) {
|
|
211
209
|
log('joining existing dial target for %p', peerId)
|
|
212
210
|
signal.clear()
|
|
213
|
-
timeoutController.clear()
|
|
214
211
|
return await existingDial.promise
|
|
215
212
|
}
|
|
216
213
|
|
|
@@ -233,13 +230,12 @@ export class DialQueue {
|
|
|
233
230
|
|
|
234
231
|
// clean up abort signals/controllers
|
|
235
232
|
signal.clear()
|
|
236
|
-
timeoutController.clear()
|
|
237
233
|
})
|
|
238
234
|
.catch(err => {
|
|
239
235
|
log.error('dial failed to %s', addrsToDial.map(({ multiaddr }) => multiaddr.toString()).join(', '), err)
|
|
240
236
|
|
|
241
237
|
// Error is a timeout
|
|
242
|
-
if (
|
|
238
|
+
if (signal.aborted) {
|
|
243
239
|
const error = new CodeError(err.message, codes.ERR_TIMEOUT)
|
|
244
240
|
throw error
|
|
245
241
|
}
|
|
@@ -253,18 +249,16 @@ export class DialQueue {
|
|
|
253
249
|
return await pendingDial.promise
|
|
254
250
|
}
|
|
255
251
|
|
|
256
|
-
private createDialAbortControllers (userSignal?: AbortSignal):
|
|
257
|
-
// ensure we throw if the dial takes longer than the dial timeout
|
|
258
|
-
const timeoutController = new TimeoutController(this.dialTimeout)
|
|
259
|
-
|
|
252
|
+
private createDialAbortControllers (userSignal?: AbortSignal): ClearableSignal {
|
|
260
253
|
// let any signal abort the dial
|
|
261
|
-
const signal =
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
254
|
+
const signal = anySignal(
|
|
255
|
+
[AbortSignal.timeout(this.dialTimeout),
|
|
256
|
+
this.shutDownController.signal,
|
|
257
|
+
userSignal
|
|
258
|
+
]
|
|
265
259
|
)
|
|
266
260
|
|
|
267
|
-
return
|
|
261
|
+
return signal
|
|
268
262
|
}
|
|
269
263
|
|
|
270
264
|
private async calculateMultiaddrs (peerId?: PeerId, addrs: Address[] = [], options: DialOptions = {}): Promise<Address[]> {
|
|
@@ -479,6 +479,10 @@ export class DefaultConnectionManager implements ConnectionManager, Startable {
|
|
|
479
479
|
}
|
|
480
480
|
|
|
481
481
|
async openConnection (peerIdOrMultiaddr: PeerId | Multiaddr | Multiaddr[], options: OpenConnectionOptions = {}): Promise<Connection> {
|
|
482
|
+
if (!this.isStarted()) {
|
|
483
|
+
throw new CodeError('Not started', codes.ERR_NODE_NOT_STARTED)
|
|
484
|
+
}
|
|
485
|
+
|
|
482
486
|
const { peerId } = getPeerAddress(peerIdOrMultiaddr)
|
|
483
487
|
|
|
484
488
|
if (peerId != null) {
|
|
@@ -5,7 +5,6 @@ import {
|
|
|
5
5
|
uniquePeers,
|
|
6
6
|
requirePeers
|
|
7
7
|
} from './utils.js'
|
|
8
|
-
import drain from 'it-drain'
|
|
9
8
|
import merge from 'it-merge'
|
|
10
9
|
import { pipe } from 'it-pipe'
|
|
11
10
|
import type { ContentRouting } from '@libp2p/interface-content-routing'
|
|
@@ -13,9 +12,7 @@ import type { AbortOptions } from '@libp2p/interfaces'
|
|
|
13
12
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
14
13
|
import type { CID } from 'multiformats/cid'
|
|
15
14
|
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
16
|
-
import type { DualDHT } from '@libp2p/interface-dht'
|
|
17
15
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
18
|
-
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
19
16
|
|
|
20
17
|
export interface CompoundContentRoutingInit {
|
|
21
18
|
routers: ContentRouting[]
|
|
@@ -23,7 +20,6 @@ export interface CompoundContentRoutingInit {
|
|
|
23
20
|
|
|
24
21
|
export interface CompoundContentRoutingComponents {
|
|
25
22
|
peerStore: PeerStore
|
|
26
|
-
dht?: DualDHT
|
|
27
23
|
}
|
|
28
24
|
|
|
29
25
|
export class CompoundContentRouting implements ContentRouting, Startable {
|
|
@@ -87,11 +83,9 @@ export class CompoundContentRouting implements ContentRouting, Startable {
|
|
|
87
83
|
throw new CodeError(messages.NOT_STARTED_YET, codes.DHT_NOT_STARTED)
|
|
88
84
|
}
|
|
89
85
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
await drain(dht.put(key, value, options))
|
|
94
|
-
}
|
|
86
|
+
await Promise.all(this.routers.map(async (router) => {
|
|
87
|
+
await router.put(key, value, options)
|
|
88
|
+
}))
|
|
95
89
|
}
|
|
96
90
|
|
|
97
91
|
/**
|
|
@@ -103,50 +97,8 @@ export class CompoundContentRouting implements ContentRouting, Startable {
|
|
|
103
97
|
throw new CodeError(messages.NOT_STARTED_YET, codes.DHT_NOT_STARTED)
|
|
104
98
|
}
|
|
105
99
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
for await (const event of dht.get(key, options)) {
|
|
110
|
-
if (event.name === 'VALUE') {
|
|
111
|
-
return event.value
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
throw new CodeError(messages.NOT_FOUND, codes.ERR_NOT_FOUND)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Get the `n` values to the given key without sorting
|
|
121
|
-
*/
|
|
122
|
-
async * getMany (key: Uint8Array, nVals: number, options: AbortOptions): AsyncIterable<{ from: PeerId, val: Uint8Array }> {
|
|
123
|
-
if (!this.isStarted()) {
|
|
124
|
-
throw new CodeError(messages.NOT_STARTED_YET, codes.DHT_NOT_STARTED)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (nVals == null || nVals === 0) {
|
|
128
|
-
return
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
let gotValues = 0
|
|
132
|
-
const dht = this.components.dht
|
|
133
|
-
|
|
134
|
-
if (dht != null) {
|
|
135
|
-
for await (const event of dht.get(key, options)) {
|
|
136
|
-
if (event.name === 'VALUE') {
|
|
137
|
-
yield { from: event.from, val: event.value }
|
|
138
|
-
|
|
139
|
-
gotValues++
|
|
140
|
-
|
|
141
|
-
if (gotValues === nVals) {
|
|
142
|
-
break
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (gotValues === 0) {
|
|
149
|
-
throw new CodeError(messages.NOT_FOUND, codes.ERR_NOT_FOUND)
|
|
150
|
-
}
|
|
100
|
+
return await Promise.any(this.routers.map(async (router) => {
|
|
101
|
+
return await router.get(key, options)
|
|
102
|
+
}))
|
|
151
103
|
}
|
|
152
104
|
}
|
package/src/fetch/index.ts
CHANGED
|
@@ -12,7 +12,6 @@ import type { AbortOptions } from '@libp2p/interfaces'
|
|
|
12
12
|
import { abortableDuplex } from 'abortable-iterator'
|
|
13
13
|
import { pipe } from 'it-pipe'
|
|
14
14
|
import first from 'it-first'
|
|
15
|
-
import { TimeoutController } from 'timeout-abort-controller'
|
|
16
15
|
import { setMaxListeners } from 'events'
|
|
17
16
|
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
|
|
18
17
|
import { toString as uint8arrayToString } from 'uint8arrays/to-string'
|
|
@@ -20,15 +19,17 @@ import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
|
20
19
|
|
|
21
20
|
const log = logger('libp2p:fetch')
|
|
22
21
|
|
|
22
|
+
const DEFAULT_TIMEOUT = 10000
|
|
23
|
+
|
|
23
24
|
export interface FetchServiceInit {
|
|
24
|
-
protocolPrefix
|
|
25
|
-
maxInboundStreams
|
|
26
|
-
maxOutboundStreams
|
|
25
|
+
protocolPrefix?: string
|
|
26
|
+
maxInboundStreams?: number
|
|
27
|
+
maxOutboundStreams?: number
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
30
|
* How long we should wait for a remote peer to send any data
|
|
30
31
|
*/
|
|
31
|
-
timeout
|
|
32
|
+
timeout?: number
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
export interface HandleMessageOptions {
|
|
@@ -45,13 +46,51 @@ export interface FetchServiceComponents {
|
|
|
45
46
|
connectionManager: ConnectionManager
|
|
46
47
|
}
|
|
47
48
|
|
|
49
|
+
export interface FetchService {
|
|
50
|
+
/**
|
|
51
|
+
* The protocol name used by this fetch service
|
|
52
|
+
*/
|
|
53
|
+
protocol: string
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Sends a request to fetch the value associated with the given key from the given peer
|
|
57
|
+
*/
|
|
58
|
+
fetch: (peer: PeerId, key: string, options?: AbortOptions) => Promise<Uint8Array | null>
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Registers a new lookup callback that can map keys to values, for a given set of keys that
|
|
62
|
+
* share the same prefix
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
*
|
|
66
|
+
* ```js
|
|
67
|
+
* // ...
|
|
68
|
+
* libp2p.fetchService.registerLookupFunction('/prefix', (key) => { ... })
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
registerLookupFunction: (prefix: string, lookup: LookupFunction) => void
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Registers a new lookup callback that can map keys to values, for a given set of keys that
|
|
75
|
+
* share the same prefix.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
*
|
|
79
|
+
* ```js
|
|
80
|
+
* // ...
|
|
81
|
+
* libp2p.fetchService.unregisterLookupFunction('/prefix')
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
unregisterLookupFunction: (prefix: string, lookup?: LookupFunction) => void
|
|
85
|
+
}
|
|
86
|
+
|
|
48
87
|
/**
|
|
49
88
|
* A simple libp2p protocol for requesting a value corresponding to a key from a peer.
|
|
50
89
|
* Developers can register one or more lookup function for retrieving the value corresponding to
|
|
51
90
|
* a given key. Each lookup function must act on a distinct part of the overall key space, defined
|
|
52
91
|
* by a fixed prefix that all keys that should be routed to that lookup function will start with.
|
|
53
92
|
*/
|
|
54
|
-
|
|
93
|
+
class DefaultFetchService implements Startable, FetchService {
|
|
55
94
|
public readonly protocol: string
|
|
56
95
|
private readonly components: FetchServiceComponents
|
|
57
96
|
private readonly lookupFunctions: Map<string, LookupFunction>
|
|
@@ -99,19 +138,17 @@ export class FetchService implements Startable {
|
|
|
99
138
|
log('dialing %s to %p', this.protocol, peer)
|
|
100
139
|
|
|
101
140
|
const connection = await this.components.connectionManager.openConnection(peer, options)
|
|
102
|
-
let timeoutController
|
|
103
141
|
let signal = options.signal
|
|
104
142
|
let stream: Stream | undefined
|
|
105
143
|
|
|
106
144
|
// create a timeout if no abort signal passed
|
|
107
145
|
if (signal == null) {
|
|
108
146
|
log('using default timeout of %d ms', this.init.timeout)
|
|
109
|
-
|
|
110
|
-
signal = timeoutController.signal
|
|
147
|
+
signal = AbortSignal.timeout(this.init.timeout ?? DEFAULT_TIMEOUT)
|
|
111
148
|
|
|
112
149
|
try {
|
|
113
150
|
// fails on node < 15.4
|
|
114
|
-
setMaxListeners?.(Infinity,
|
|
151
|
+
setMaxListeners?.(Infinity, signal)
|
|
115
152
|
} catch {}
|
|
116
153
|
}
|
|
117
154
|
|
|
@@ -163,10 +200,6 @@ export class FetchService implements Startable {
|
|
|
163
200
|
|
|
164
201
|
return result ?? null
|
|
165
202
|
} finally {
|
|
166
|
-
if (timeoutController != null) {
|
|
167
|
-
timeoutController.clear()
|
|
168
|
-
}
|
|
169
|
-
|
|
170
203
|
if (stream != null) {
|
|
171
204
|
stream.close()
|
|
172
205
|
}
|
|
@@ -274,3 +307,7 @@ export class FetchService implements Startable {
|
|
|
274
307
|
this.lookupFunctions.delete(prefix)
|
|
275
308
|
}
|
|
276
309
|
}
|
|
310
|
+
|
|
311
|
+
export function fetchService (init: FetchServiceInit = {}): (components: FetchServiceComponents) => FetchService {
|
|
312
|
+
return (components) => new DefaultFetchService(components, init)
|
|
313
|
+
}
|