libp2p 0.43.4 → 0.44.0-0d46c78f
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 +29 -29
- package/dist/src/address-manager/index.d.ts +4 -4
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +9 -6
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/autonat/index.d.ts +1 -2
- package/dist/src/autonat/index.d.ts.map +1 -1
- package/dist/src/autonat/index.js +19 -19
- package/dist/src/autonat/index.js.map +1 -1
- package/dist/src/circuit-relay/server/advert-service.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/advert-service.js +2 -2
- package/dist/src/circuit-relay/server/advert-service.js.map +1 -1
- package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/index.js +7 -3
- package/dist/src/circuit-relay/server/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.js +3 -1
- package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
- package/dist/src/circuit-relay/transport/index.d.ts +3 -0
- package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/index.js +6 -2
- package/dist/src/circuit-relay/transport/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/listener.d.ts +3 -0
- package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/listener.js +5 -4
- package/dist/src/circuit-relay/transport/listener.js.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +4 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.js +11 -6
- package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
- package/dist/src/circuit-relay/utils.d.ts.map +1 -1
- package/dist/src/circuit-relay/utils.js +3 -1
- package/dist/src/circuit-relay/utils.js.map +1 -1
- package/dist/src/components.d.ts +7 -69
- 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 +0 -71
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts +39 -0
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -0
- package/dist/src/connection-manager/auto-dial.js +136 -0
- package/dist/src/connection-manager/auto-dial.js.map +1 -0
- package/dist/src/connection-manager/connection-pruner.d.ts +32 -0
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -0
- package/dist/src/connection-manager/connection-pruner.js +111 -0
- package/dist/src/connection-manager/connection-pruner.js.map +1 -0
- package/dist/src/connection-manager/constants.d.ts +49 -0
- package/dist/src/connection-manager/constants.d.ts.map +1 -0
- package/dist/src/connection-manager/constants.js +49 -0
- package/dist/src/connection-manager/constants.js.map +1 -0
- package/dist/src/connection-manager/dial-queue.d.ts +78 -0
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -0
- package/dist/src/connection-manager/dial-queue.js +372 -0
- package/dist/src/connection-manager/dial-queue.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +70 -73
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +118 -247
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/utils.d.ts +9 -0
- package/dist/src/connection-manager/utils.d.ts.map +1 -0
- package/dist/src/connection-manager/utils.js +66 -0
- package/dist/src/connection-manager/utils.js.map +1 -0
- 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 +7 -42
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/content-routing/utils.js +3 -1
- package/dist/src/content-routing/utils.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 +10 -12
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/identify/index.d.ts +16 -48
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +203 -201
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +11 -67
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/index.d.ts.map +1 -1
- package/dist/src/insecure/index.js +3 -1
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +12 -35
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +141 -206
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/peer-routing.d.ts +1 -38
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +0 -59
- 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.d.ts +5 -2
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +27 -25
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +15 -4
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +37 -12
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +16 -11
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +38 -47
- 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 +70 -54
- package/src/address-manager/index.ts +12 -8
- package/src/autonat/index.ts +20 -22
- package/src/circuit-relay/server/advert-service.ts +2 -2
- package/src/circuit-relay/server/index.ts +7 -3
- package/src/circuit-relay/transport/discovery.ts +3 -1
- package/src/circuit-relay/transport/index.ts +10 -2
- package/src/circuit-relay/transport/listener.ts +8 -4
- package/src/circuit-relay/transport/reservation-store.ts +15 -7
- package/src/circuit-relay/utils.ts +3 -1
- package/src/components.ts +72 -251
- package/src/config/connection-gater.browser.ts +31 -0
- package/src/config/connection-gater.ts +19 -0
- package/src/config.ts +3 -73
- package/src/connection-manager/auto-dial.ts +189 -0
- package/src/connection-manager/connection-pruner.ts +157 -0
- package/src/connection-manager/constants.ts +60 -0
- package/src/connection-manager/dial-queue.ts +506 -0
- package/src/connection-manager/index.ts +194 -339
- package/src/connection-manager/utils.ts +76 -0
- package/src/content-routing/index.ts +7 -55
- package/src/content-routing/utils.ts +3 -1
- package/src/fetch/index.ts +55 -18
- package/src/identify/index.ts +238 -228
- package/src/index.ts +12 -76
- package/src/insecure/index.ts +9 -5
- package/src/libp2p.ts +146 -265
- package/src/peer-routing.ts +1 -101
- package/src/ping/constants.ts +11 -0
- package/src/ping/index.ts +32 -28
- package/src/registrar.ts +31 -27
- package/src/transport-manager.ts +49 -15
- package/src/upgrader.ts +62 -57
- package/src/{nat-manager.ts → upnp-nat/index.ts} +15 -16
- package/src/version.ts +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts +0 -42
- package/dist/src/connection-manager/auto-dialler.d.ts.map +0 -1
- package/dist/src/connection-manager/auto-dialler.js +0 -105
- package/dist/src/connection-manager/auto-dialler.js.map +0 -1
- package/dist/src/connection-manager/dialer/dial-request.d.ts +0 -27
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/dial-request.js +0 -114
- package/dist/src/connection-manager/dialer/dial-request.js.map +0 -1
- package/dist/src/connection-manager/dialer/index.d.ts +0 -121
- package/dist/src/connection-manager/dialer/index.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/index.js +0 -324
- package/dist/src/connection-manager/dialer/index.js.map +0 -1
- package/dist/src/connection-manager/latency-monitor.d.ts +0 -120
- package/dist/src/connection-manager/latency-monitor.d.ts.map +0 -1
- package/dist/src/connection-manager/latency-monitor.js +0 -220
- package/dist/src/connection-manager/latency-monitor.js.map +0 -1
- package/dist/src/connection-manager/visibility-change-emitter.d.ts +0 -66
- package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +0 -1
- package/dist/src/connection-manager/visibility-change-emitter.js +0 -118
- package/dist/src/connection-manager/visibility-change-emitter.js.map +0 -1
- package/dist/src/constants.d.ts +0 -27
- package/dist/src/constants.d.ts.map +0 -1
- package/dist/src/constants.js +0 -31
- package/dist/src/constants.js.map +0 -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/dist/src/peer-record-updater.d.ts +0 -24
- package/dist/src/peer-record-updater.d.ts.map +0 -1
- package/dist/src/peer-record-updater.js +0 -44
- package/dist/src/peer-record-updater.js.map +0 -1
- package/dist/typedoc-urls.json +0 -41
- package/src/connection-manager/auto-dialler.ts +0 -158
- package/src/connection-manager/dialer/dial-request.ts +0 -144
- package/src/connection-manager/dialer/index.ts +0 -461
- package/src/connection-manager/latency-monitor.ts +0 -319
- package/src/connection-manager/visibility-change-emitter.ts +0 -135
- package/src/constants.ts +0 -36
- 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/peer-record-updater.ts +0 -65
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { AbortOptions, multiaddr } from '@multiformats/multiaddr'
|
|
2
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
3
|
+
import { logger } from '@libp2p/logger'
|
|
4
|
+
import { ClearableSignal, anySignal } from 'any-signal'
|
|
5
|
+
import { setMaxListeners } from 'events'
|
|
6
|
+
|
|
7
|
+
const log = logger('libp2p:connection-manager:utils')
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Resolve multiaddr recursively
|
|
11
|
+
*/
|
|
12
|
+
export async function resolveMultiaddrs (ma: Multiaddr, options: AbortOptions): Promise<Multiaddr[]> {
|
|
13
|
+
// TODO: recursive logic should live in multiaddr once dns4/dns6 support is in place
|
|
14
|
+
// Now only supporting resolve for dnsaddr
|
|
15
|
+
const resolvableProto = ma.protoNames().includes('dnsaddr')
|
|
16
|
+
|
|
17
|
+
// Multiaddr is not resolvable? End recursion!
|
|
18
|
+
if (!resolvableProto) {
|
|
19
|
+
return [ma]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const resolvedMultiaddrs = await resolveRecord(ma, options)
|
|
23
|
+
const recursiveMultiaddrs = await Promise.all(resolvedMultiaddrs.map(async (nm) => {
|
|
24
|
+
return await resolveMultiaddrs(nm, options)
|
|
25
|
+
}))
|
|
26
|
+
|
|
27
|
+
const addrs = recursiveMultiaddrs.flat()
|
|
28
|
+
const output = addrs.reduce<Multiaddr[]>((array, newM) => {
|
|
29
|
+
if (array.find(m => m.equals(newM)) == null) {
|
|
30
|
+
array.push(newM)
|
|
31
|
+
}
|
|
32
|
+
return array
|
|
33
|
+
}, ([]))
|
|
34
|
+
|
|
35
|
+
log('resolved %s to', ma, output.map(ma => ma.toString()))
|
|
36
|
+
|
|
37
|
+
return output
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Resolve a given multiaddr. If this fails, an empty array will be returned
|
|
42
|
+
*/
|
|
43
|
+
async function resolveRecord (ma: Multiaddr, options: AbortOptions): Promise<Multiaddr[]> {
|
|
44
|
+
try {
|
|
45
|
+
ma = multiaddr(ma.toString()) // Use current multiaddr module
|
|
46
|
+
const multiaddrs = await ma.resolve(options)
|
|
47
|
+
return multiaddrs
|
|
48
|
+
} catch (err) {
|
|
49
|
+
log.error(`multiaddr ${ma.toString()} could not be resolved`, err)
|
|
50
|
+
return []
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function combineSignals (...signals: Array<AbortSignal | undefined>): ClearableSignal {
|
|
55
|
+
const sigs: AbortSignal[] = []
|
|
56
|
+
|
|
57
|
+
for (const sig of signals) {
|
|
58
|
+
if (sig != null) {
|
|
59
|
+
try {
|
|
60
|
+
// fails on node < 15.4
|
|
61
|
+
setMaxListeners?.(Infinity, sig)
|
|
62
|
+
} catch { }
|
|
63
|
+
sigs.push(sig)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// let any signal abort the dial
|
|
68
|
+
const signal = anySignal(sigs)
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
// fails on node < 15.4
|
|
72
|
+
setMaxListeners?.(Infinity, signal)
|
|
73
|
+
} catch {}
|
|
74
|
+
|
|
75
|
+
return signal
|
|
76
|
+
}
|
|
@@ -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 {
|
|
@@ -54,7 +50,7 @@ export class CompoundContentRouting implements ContentRouting, Startable {
|
|
|
54
50
|
*/
|
|
55
51
|
async * findProviders (key: CID, options: AbortOptions = {}): AsyncIterable<PeerInfo> {
|
|
56
52
|
if (this.routers.length === 0) {
|
|
57
|
-
throw new CodeError('No content
|
|
53
|
+
throw new CodeError('No content routers available', codes.ERR_NO_ROUTERS_AVAILABLE)
|
|
58
54
|
}
|
|
59
55
|
|
|
60
56
|
yield * pipe(
|
|
@@ -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
|
}
|
|
@@ -11,7 +11,9 @@ import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
|
11
11
|
export async function * storeAddresses (source: Source<PeerInfo>, peerStore: PeerStore): AsyncIterable<PeerInfo> {
|
|
12
12
|
yield * map(source, async (peer) => {
|
|
13
13
|
// ensure we have the addresses for a given peer
|
|
14
|
-
await peerStore.
|
|
14
|
+
await peerStore.merge(peer.id, {
|
|
15
|
+
multiaddrs: peer.multiaddrs
|
|
16
|
+
})
|
|
15
17
|
|
|
16
18
|
return peer
|
|
17
19
|
})
|
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
|
|
|
@@ -127,9 +164,9 @@ export class FetchService implements Startable {
|
|
|
127
164
|
|
|
128
165
|
const result = await pipe(
|
|
129
166
|
[FetchRequest.encode({ identifier: key })],
|
|
130
|
-
lp.encode(),
|
|
167
|
+
(source) => lp.encode(source),
|
|
131
168
|
source,
|
|
132
|
-
lp.decode(),
|
|
169
|
+
(source) => lp.decode(source),
|
|
133
170
|
async function (source) {
|
|
134
171
|
const buf = await first(source)
|
|
135
172
|
|
|
@@ -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
|
}
|
|
@@ -184,7 +217,7 @@ export class FetchService implements Startable {
|
|
|
184
217
|
|
|
185
218
|
await pipe(
|
|
186
219
|
stream,
|
|
187
|
-
lp.decode(),
|
|
220
|
+
(source) => lp.decode(source),
|
|
188
221
|
async function * (source) {
|
|
189
222
|
const buf = await first(source)
|
|
190
223
|
|
|
@@ -215,7 +248,7 @@ export class FetchService implements Startable {
|
|
|
215
248
|
|
|
216
249
|
yield FetchResponse.encode(response)
|
|
217
250
|
},
|
|
218
|
-
lp.encode(),
|
|
251
|
+
(source) => lp.encode(source),
|
|
219
252
|
stream
|
|
220
253
|
)
|
|
221
254
|
}
|
|
@@ -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
|
+
}
|