@@ -18,111 +18,118 @@ const pAny = require('p-any')
18
18
* @property {Uint8Array } val
19
19
*/
20
20
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
+ )
24
62
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
+ }
28
66
}
29
67
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' )
126
78
}
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 )
127
132
}
128
133
}
134
+
135
+ module . exports = ContentRouting
0 commit comments