Skip to content

Commit 74475c1

Browse files
committed
chore: minor changes from lodestar testing
1 parent 77a0257 commit 74475c1

File tree

7 files changed

+164
-179
lines changed

7 files changed

+164
-179
lines changed

src/content-routing.js

+109-102
Original file line numberDiff line numberDiff line change
@@ -18,111 +18,118 @@ const pAny = require('p-any')
1818
* @property {Uint8Array} val
1919
*/
2020

21-
module.exports = (node) => {
22-
const routers = node._modules.contentRouting || []
23-
const dht = node._dht
21+
class ContentRouting {
22+
/**
23+
* @class
24+
* @param {import('./')} libp2p
25+
*/
26+
constructor (libp2p) {
27+
this.libp2p = libp2p
28+
this.routers = libp2p._modules.contentRouting || []
29+
this.dht = libp2p._dht
30+
31+
// If we have the dht, make it first
32+
if (this.dht) {
33+
this.routers.unshift(this.dht)
34+
}
35+
}
36+
37+
/**
38+
* Iterates over all content routers in series to find providers of the given key.
39+
* Once a content router succeeds, iteration will stop.
40+
*
41+
* @param {CID} key - The CID key of the content to find
42+
* @param {object} [options]
43+
* @param {number} [options.timeout] - How long the query should run
44+
* @param {number} [options.maxNumProviders] - maximum number of providers to find
45+
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>}
46+
*/
47+
async * findProviders (key, options) {
48+
if (!this.routers.length) {
49+
throw errCode(new Error('No content this.routers available'), 'NO_ROUTERS_AVAILABLE')
50+
}
51+
52+
const result = await pAny(
53+
this.routers.map(async (router) => {
54+
const provs = await all(router.findProviders(key, options))
55+
56+
if (!provs || !provs.length) {
57+
throw errCode(new Error('not found'), 'NOT_FOUND')
58+
}
59+
return provs
60+
})
61+
)
2462

25-
// If we have the dht, make it first
26-
if (dht) {
27-
routers.unshift(dht)
63+
for (const peer of result) {
64+
yield peer
65+
}
2866
}
2967

30-
return {
31-
/**
32-
* Iterates over all content routers in series to find providers of the given key.
33-
* Once a content router succeeds, iteration will stop.
34-
*
35-
* @param {CID} key - The CID key of the content to find
36-
* @param {object} [options]
37-
* @param {number} [options.timeout] - How long the query should run
38-
* @param {number} [options.maxNumProviders] - maximum number of providers to find
39-
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>}
40-
*/
41-
async * findProviders (key, options) {
42-
if (!routers.length) {
43-
throw errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE')
44-
}
45-
46-
const result = await pAny(
47-
routers.map(async (router) => {
48-
const provs = await all(router.findProviders(key, options))
49-
50-
if (!provs || !provs.length) {
51-
throw errCode(new Error('not found'), 'NOT_FOUND')
52-
}
53-
return provs
54-
})
55-
)
56-
57-
for (const peer of result) {
58-
yield peer
59-
}
60-
},
61-
62-
/**
63-
* Iterates over all content routers in parallel to notify it is
64-
* a provider of the given key.
65-
*
66-
* @param {CID} key - The CID key of the content to find
67-
* @returns {Promise<void[]>}
68-
*/
69-
async provide (key) { // eslint-disable-line require-await
70-
if (!routers.length) {
71-
throw errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE')
72-
}
73-
74-
return Promise.all(routers.map((router) => router.provide(key)))
75-
},
76-
77-
/**
78-
* Store the given key/value pair in the DHT.
79-
*
80-
* @param {Uint8Array} key
81-
* @param {Uint8Array} value
82-
* @param {Object} [options] - put options
83-
* @param {number} [options.minPeers] - minimum number of peers required to successfully put
84-
* @returns {Promise<void>}
85-
*/
86-
async put (key, value, options) { // eslint-disable-line require-await
87-
if (!node.isStarted() || !dht.isStarted) {
88-
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
89-
}
90-
91-
return dht.put(key, value, options)
92-
},
93-
94-
/**
95-
* Get the value to the given key.
96-
* Times out after 1 minute by default.
97-
*
98-
* @param {Uint8Array} key
99-
* @param {Object} [options] - get options
100-
* @param {number} [options.timeout] - optional timeout (default: 60000)
101-
* @returns {Promise<GetData>}
102-
*/
103-
async get (key, options) { // eslint-disable-line require-await
104-
if (!node.isStarted() || !dht.isStarted) {
105-
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
106-
}
107-
108-
return dht.get(key, options)
109-
},
110-
111-
/**
112-
* Get the `n` values to the given key without sorting.
113-
*
114-
* @param {Uint8Array} key
115-
* @param {number} nVals
116-
* @param {Object} [options] - get options
117-
* @param {number} [options.timeout] - optional timeout (default: 60000)
118-
* @returns {Promise<GetData[]>}
119-
*/
120-
async getMany (key, nVals, options) { // eslint-disable-line require-await
121-
if (!node.isStarted() || !dht.isStarted) {
122-
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
123-
}
124-
125-
return dht.getMany(key, nVals, options)
68+
/**
69+
* Iterates over all content routers in parallel to notify it is
70+
* a provider of the given key.
71+
*
72+
* @param {CID} key - The CID key of the content to find
73+
* @returns {Promise<void[]>}
74+
*/
75+
async provide (key) { // eslint-disable-line require-await
76+
if (!this.routers.length) {
77+
throw errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE')
12678
}
79+
80+
return Promise.all(this.routers.map((router) => router.provide(key)))
81+
}
82+
83+
/**
84+
* Store the given key/value pair in the DHT.
85+
*
86+
* @param {Uint8Array} key
87+
* @param {Uint8Array} value
88+
* @param {Object} [options] - put options
89+
* @param {number} [options.minPeers] - minimum number of peers required to successfully put
90+
* @returns {Promise<void>}
91+
*/
92+
async put (key, value, options) { // eslint-disable-line require-await
93+
if (!this.libp2p.isStarted() || !this.dht.isStarted) {
94+
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
95+
}
96+
97+
return this.dht.put(key, value, options)
98+
}
99+
100+
/**
101+
* Get the value to the given key.
102+
* Times out after 1 minute by default.
103+
*
104+
* @param {Uint8Array} key
105+
* @param {Object} [options] - get options
106+
* @param {number} [options.timeout] - optional timeout (default: 60000)
107+
* @returns {Promise<GetData>}
108+
*/
109+
async get (key, options) { // eslint-disable-line require-await
110+
if (!this.libp2p.isStarted() || !this.dht.isStarted) {
111+
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
112+
}
113+
114+
return this.dht.get(key, options)
115+
}
116+
117+
/**
118+
* Get the `n` values to the given key without sorting.
119+
*
120+
* @param {Uint8Array} key
121+
* @param {number} nVals
122+
* @param {Object} [options] - get options
123+
* @param {number} [options.timeout] - optional timeout (default: 60000)
124+
* @returns {Promise<GetData[]>}
125+
*/
126+
async getMany (key, nVals, options) { // eslint-disable-line require-await
127+
if (!this.libp2p.isStarted() || !this.dht.isStarted) {
128+
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
129+
}
130+
131+
return this.dht.getMany(key, nVals, options)
127132
}
128133
}
134+
135+
module.exports = ContentRouting

src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const errCode = require('err-code')
1111
const PeerId = require('peer-id')
1212

1313
const PeerRouting = require('./peer-routing')
14-
const contentRouting = require('./content-routing')
14+
const ContentRouting = require('./content-routing')
1515
const getPeer = require('./get-peer')
1616
const { validate: validateConfig } = require('./config')
1717
const { codes, messages } = require('./errors')
@@ -242,7 +242,7 @@ class Libp2p extends EventEmitter {
242242
// Attach remaining APIs
243243
// peer and content routing will automatically get modules from _modules and _dht
244244
this.peerRouting = new PeerRouting(this)
245-
this.contentRouting = contentRouting(this)
245+
this.contentRouting = new ContentRouting(this)
246246

247247
// Mount default protocols
248248
ping.mount(this)

src/peer-store/address-book.js

+7-23
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ const Envelope = require('../record/envelope')
3333
*
3434
* @typedef {Object} Entry
3535
* @property {Address[]} addresses peer Addresses.
36-
* @property {CertifiedRecord} record certified peer record.
36+
* @property {CertifiedRecord} [record] certified peer record.
3737
*/
3838

3939
/**
40-
* @extends {Book}
40+
* @extends {Book<Entry, Address[], Multiaddr[]>}
4141
*/
4242
class AddressBook extends Book {
4343
/**
@@ -56,12 +56,13 @@ class AddressBook extends Book {
5656
peerStore,
5757
eventName: 'change:multiaddrs',
5858
eventProperty: 'multiaddrs',
59-
eventTransformer: (data) => {
60-
if (!data.addresses) {
59+
eventTransformer: (entry) => {
60+
if (!entry || !entry.addresses) {
6161
return []
6262
}
63-
return data.addresses.map((address) => address.multiaddr)
64-
}
63+
return entry.addresses.map((address) => address.multiaddr)
64+
},
65+
getTransformer: (entry) => entry && entry.addresses ? [...entry.addresses] : undefined
6566
})
6667

6768
/**
@@ -263,23 +264,6 @@ class AddressBook extends Book {
263264
return this
264265
}
265266

266-
/**
267-
* Get the known data of a provided peer.
268-
*
269-
* @override
270-
* @param {PeerId} peerId
271-
* @returns {Address[]|undefined}
272-
*/
273-
get (peerId) {
274-
if (!PeerId.isPeerId(peerId)) {
275-
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
276-
}
277-
278-
const entry = this.data.get(peerId.toB58String())
279-
280-
return entry && entry.addresses ? [...entry.addresses] : undefined
281-
}
282-
283267
/**
284268
* Transforms received multiaddrs into Address.
285269
*

src/peer-store/book.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const passthrough = data => data
1414
*/
1515

1616
/**
17-
* @template T
17+
* @template Data, GetData, EventData
1818
*/
1919
class Book {
2020
/**
@@ -25,18 +25,20 @@ class Book {
2525
* @param {PeerStore} properties.peerStore - PeerStore instance.
2626
* @param {string} properties.eventName - Name of the event to emit by the PeerStore.
2727
* @param {string} properties.eventProperty - Name of the property to emit by the PeerStore.
28-
* @param {(data: T) => T[]} [properties.eventTransformer] - Transformer function of the provided data for being emitted.
28+
* @param {(data: Data | undefined) => EventData | undefined} [properties.eventTransformer] - Transformer function of the provided data for being emitted.
29+
* @param {(data: Data | undefined) => GetData | undefined} [properties.getTransformer] - Transformer function of the provided data for being returned on get.
2930
*/
30-
constructor ({ peerStore, eventName, eventProperty, eventTransformer = passthrough }) {
31+
constructor ({ peerStore, eventName, eventProperty, eventTransformer = passthrough, getTransformer = passthrough }) {
3132
this._ps = peerStore
3233
this.eventName = eventName
3334
this.eventProperty = eventProperty
3435
this.eventTransformer = eventTransformer
36+
this.getTransformer = getTransformer
3537

3638
/**
3739
* Map known peers to their data.
3840
*
39-
* @type {Map<string, T[]|T>}
41+
* @type {Map<string, Data>}
4042
*/
4143
this.data = new Map()
4244
}
@@ -45,7 +47,7 @@ class Book {
4547
* Set known data of a provided peer.
4648
*
4749
* @param {PeerId} peerId
48-
* @param {T[]|T} data
50+
* @param {unknown} data
4951
*/
5052
set (peerId, data) {
5153
throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
@@ -56,7 +58,7 @@ class Book {
5658
*
5759
* @protected
5860
* @param {PeerId} peerId - peerId of the data to store
59-
* @param {T} data - data to store.
61+
* @param {Data} data - data to store.
6062
* @param {Object} [options] - storing options.
6163
* @param {boolean} [options.emit = true] - emit the provided data.
6264
* @returns {void}
@@ -76,7 +78,7 @@ class Book {
7678
*
7779
* @protected
7880
* @param {PeerId} peerId
79-
* @param {any} [data]
81+
* @param {Data | undefined} [data]
8082
*/
8183
_emit (peerId, data) {
8284
this._ps.emit(this.eventName, {
@@ -90,7 +92,7 @@ class Book {
9092
* Returns `undefined` if there is no available data for the given peer.
9193
*
9294
* @param {PeerId} peerId
93-
* @returns {T[]|T|undefined}
95+
* @returns {GetData | undefined}
9496
*/
9597
get (peerId) {
9698
if (!PeerId.isPeerId(peerId)) {
@@ -99,8 +101,7 @@ class Book {
99101

100102
const rec = this.data.get(peerId.toB58String())
101103

102-
// @ts-ignore
103-
return rec ? [...rec] : undefined
104+
return this.getTransformer(rec)
104105
}
105106

106107
/**
@@ -118,7 +119,7 @@ class Book {
118119
return false
119120
}
120121

121-
this._emit(peerId, [])
122+
this._emit(peerId, undefined)
122123

123124
return true
124125
}

0 commit comments

Comments
 (0)