libp2p 0.44.0 → 0.45.0-08d4c261
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 +32 -32
- package/dist/src/address-manager/index.d.ts +9 -5
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +44 -7
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/address-manager/utils.d.ts +2 -0
- package/dist/src/address-manager/utils.d.ts.map +1 -0
- package/dist/src/address-manager/utils.js +12 -0
- package/dist/src/address-manager/utils.js.map +1 -0
- package/dist/src/autonat/constants.d.ts +16 -2
- package/dist/src/autonat/constants.d.ts.map +1 -1
- package/dist/src/autonat/constants.js +16 -2
- package/dist/src/autonat/constants.js.map +1 -1
- package/dist/src/autonat/index.d.ts +12 -33
- package/dist/src/autonat/index.d.ts.map +1 -1
- package/dist/src/autonat/index.js +48 -33
- package/dist/src/autonat/index.js.map +1 -1
- package/dist/src/circuit-relay/index.d.ts +1 -1
- package/dist/src/circuit-relay/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/advert-service.d.ts +2 -2
- package/dist/src/circuit-relay/server/advert-service.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/advert-service.js +8 -4
- package/dist/src/circuit-relay/server/advert-service.js.map +1 -1
- package/dist/src/circuit-relay/server/index.d.ts +8 -8
- package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/index.js +32 -12
- package/dist/src/circuit-relay/server/index.js.map +1 -1
- package/dist/src/circuit-relay/server/reservation-store.d.ts +5 -5
- package/dist/src/circuit-relay/server/reservation-store.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/reservation-store.js +10 -3
- package/dist/src/circuit-relay/server/reservation-store.js.map +1 -1
- package/dist/src/circuit-relay/server/reservation-voucher.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/reservation-voucher.js +5 -2
- package/dist/src/circuit-relay/server/reservation-voucher.js.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.d.ts +4 -4
- package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.js +13 -5
- package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
- package/dist/src/circuit-relay/transport/index.d.ts +10 -8
- package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/index.js +26 -18
- package/dist/src/circuit-relay/transport/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/listener.d.ts +1 -1
- package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/listener.js +43 -43
- package/dist/src/circuit-relay/transport/listener.js.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +8 -7
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.js +65 -52
- 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 -3
- package/dist/src/circuit-relay/utils.js.map +1 -1
- package/dist/src/components.d.ts +10 -67
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +61 -165
- 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 +4 -66
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection/index.d.ts +3 -3
- package/dist/src/connection/index.d.ts.map +1 -1
- package/dist/src/connection/index.js +38 -8
- package/dist/src/connection/index.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts +4 -1
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dial.js +44 -13
- package/dist/src/connection-manager/auto-dial.js.map +1 -1
- package/dist/src/connection-manager/connection-pruner.d.ts +4 -0
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
- package/dist/src/connection-manager/connection-pruner.js +28 -8
- package/dist/src/connection-manager/connection-pruner.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts +7 -6
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +59 -23
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +16 -15
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +44 -41
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/utils.d.ts +2 -3
- package/dist/src/connection-manager/utils.d.ts.map +1 -1
- package/dist/src/connection-manager/utils.js +3 -3
- package/dist/src/connection-manager/utils.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +2 -12
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +12 -44
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/content-routing/utils.d.ts +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 +15 -37
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +19 -16
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/get-peer.d.ts +1 -1
- package/dist/src/get-peer.d.ts.map +1 -1
- package/dist/src/get-peer.js +3 -3
- package/dist/src/get-peer.js.map +1 -1
- package/dist/src/identify/identify.d.ts +53 -0
- package/dist/src/identify/identify.d.ts.map +1 -0
- package/dist/src/identify/identify.js +417 -0
- package/dist/src/identify/identify.js.map +1 -0
- package/dist/src/identify/index.d.ts +14 -59
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +5 -388
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +28 -79
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/index.js +6 -8
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +16 -47
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +141 -201
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/peer-routing.d.ts +3 -40
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +8 -65
- 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 +11 -28
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +31 -28
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/pnet/crypto.js +3 -3
- package/dist/src/pnet/crypto.js.map +1 -1
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +9 -6
- package/dist/src/pnet/index.js.map +1 -1
- package/dist/src/pnet/key-generator.js +1 -1
- package/dist/src/pnet/key-generator.js.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.js +2 -5
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
- package/dist/src/registrar.d.ts +6 -4
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +33 -28
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +14 -7
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +27 -13
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +20 -15
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +49 -53
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/{nat-manager.d.ts → upnp-nat/index.d.ts} +12 -15
- package/dist/src/upnp-nat/index.d.ts.map +1 -0
- package/dist/src/{nat-manager.js → upnp-nat/index.js} +23 -10
- package/dist/src/upnp-nat/index.js.map +1 -0
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js +1 -1
- package/dist/src/version.js.map +1 -1
- package/package.json +62 -47
- package/src/address-manager/index.ts +52 -10
- package/src/address-manager/utils.ts +14 -0
- package/src/autonat/constants.ts +17 -2
- package/src/autonat/index.ts +65 -57
- package/src/circuit-relay/index.ts +1 -1
- package/src/circuit-relay/server/advert-service.ts +5 -5
- package/src/circuit-relay/server/index.ts +28 -20
- package/src/circuit-relay/server/reservation-store.ts +5 -5
- package/src/circuit-relay/server/reservation-voucher.ts +1 -1
- package/src/circuit-relay/transport/discovery.ts +12 -10
- package/src/circuit-relay/transport/index.ts +26 -26
- package/src/circuit-relay/transport/listener.ts +43 -33
- package/src/circuit-relay/transport/reservation-store.ts +42 -28
- package/src/circuit-relay/utils.ts +6 -6
- package/src/components.ts +75 -238
- package/src/config/connection-gater.browser.ts +31 -0
- package/src/config/connection-gater.ts +19 -0
- package/src/config.ts +9 -70
- package/src/connection/index.ts +6 -10
- package/src/connection-manager/auto-dial.ts +46 -17
- package/src/connection-manager/connection-pruner.ts +30 -10
- package/src/connection-manager/dial-queue.ts +56 -35
- package/src/connection-manager/index.ts +45 -57
- package/src/connection-manager/utils.ts +4 -5
- package/src/content-routing/index.ts +11 -59
- package/src/content-routing/utils.ts +4 -2
- package/src/fetch/index.ts +63 -26
- package/src/get-peer.ts +4 -4
- package/src/identify/identify.ts +519 -0
- package/src/identify/index.ts +19 -509
- package/src/index.ts +25 -88
- package/src/insecure/index.ts +10 -10
- package/src/libp2p.ts +158 -279
- package/src/peer-routing.ts +10 -110
- package/src/ping/constants.ts +11 -0
- package/src/ping/index.ts +43 -39
- package/src/pnet/crypto.ts +3 -3
- package/src/pnet/index.ts +6 -6
- package/src/pnet/key-generator.ts +1 -1
- package/src/pubsub/dummy-pubsub.ts +2 -2
- package/src/registrar.ts +34 -31
- package/src/transport-manager.ts +33 -19
- package/src/upgrader.ts +75 -71
- package/src/{nat-manager.ts → upnp-nat/index.ts} +23 -24
- 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.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/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
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interfaces/errors'
|
|
2
|
+
import merge from 'it-merge'
|
|
3
|
+
import { pipe } from 'it-pipe'
|
|
2
4
|
import { messages, codes } from '../errors.js'
|
|
3
5
|
import {
|
|
4
6
|
storeAddresses,
|
|
5
7
|
uniquePeers,
|
|
6
8
|
requirePeers
|
|
7
9
|
} from './utils.js'
|
|
8
|
-
import drain from 'it-drain'
|
|
9
|
-
import merge from 'it-merge'
|
|
10
|
-
import { pipe } from 'it-pipe'
|
|
11
10
|
import type { ContentRouting } from '@libp2p/interface-content-routing'
|
|
11
|
+
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
12
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
12
13
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
13
14
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
14
15
|
import type { CID } from 'multiformats/cid'
|
|
15
|
-
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
16
|
-
import type { DualDHT } from '@libp2p/interface-dht'
|
|
17
|
-
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 Promise.any(this.routers.map(async (router) => {
|
|
101
|
+
return router.get(key, options)
|
|
102
|
+
}))
|
|
151
103
|
}
|
|
152
104
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interfaces/errors'
|
|
2
2
|
import filter from 'it-filter'
|
|
3
3
|
import map from 'it-map'
|
|
4
|
-
import type { Source } from 'it-stream-types'
|
|
5
4
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
6
5
|
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
6
|
+
import type { Source } from 'it-stream-types'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Store the multiaddrs from every peer in the passed peer store
|
|
@@ -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
|
@@ -1,34 +1,35 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { setMaxListeners } from 'events'
|
|
2
2
|
import { CodeError } from '@libp2p/interfaces/errors'
|
|
3
|
-
import {
|
|
4
|
-
import * as lp from 'it-length-prefixed'
|
|
5
|
-
import { FetchRequest, FetchResponse } from './pb/proto.js'
|
|
6
|
-
import { PROTOCOL_NAME, PROTOCOL_VERSION } from './constants.js'
|
|
7
|
-
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
8
|
-
import type { Startable } from '@libp2p/interfaces/startable'
|
|
9
|
-
import type { Stream } from '@libp2p/interface-connection'
|
|
10
|
-
import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
|
|
11
|
-
import type { AbortOptions } from '@libp2p/interfaces'
|
|
3
|
+
import { logger } from '@libp2p/logger'
|
|
12
4
|
import { abortableDuplex } from 'abortable-iterator'
|
|
13
|
-
import { pipe } from 'it-pipe'
|
|
14
5
|
import first from 'it-first'
|
|
15
|
-
import
|
|
16
|
-
import {
|
|
6
|
+
import * as lp from 'it-length-prefixed'
|
|
7
|
+
import { pipe } from 'it-pipe'
|
|
17
8
|
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
|
|
18
9
|
import { toString as uint8arrayToString } from 'uint8arrays/to-string'
|
|
10
|
+
import { codes } from '../errors.js'
|
|
11
|
+
import { PROTOCOL_NAME, PROTOCOL_VERSION } from './constants.js'
|
|
12
|
+
import { FetchRequest, FetchResponse } from './pb/proto.js'
|
|
13
|
+
import type { Stream } from '@libp2p/interface-connection'
|
|
19
14
|
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
15
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
16
|
+
import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
|
|
17
|
+
import type { AbortOptions } from '@libp2p/interfaces'
|
|
18
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
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
|
+
}
|
package/src/get-peer.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
import { isPeerId } from '@libp2p/interface-peer-id'
|
|
2
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
3
|
+
import { logger } from '@libp2p/logger'
|
|
1
4
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
2
|
-
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
3
5
|
import { isMultiaddr } from '@multiformats/multiaddr'
|
|
4
|
-
import { CodeError } from '@libp2p/interfaces/errors'
|
|
5
6
|
import { codes } from './errors.js'
|
|
6
|
-
import { isPeerId } from '@libp2p/interface-peer-id'
|
|
7
7
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
8
|
-
import {
|
|
8
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
9
9
|
|
|
10
10
|
const log = logger('libp2p:get-peer')
|
|
11
11
|
|