libp2p 0.34.0 → 0.35.3

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.
Files changed (36) hide show
  1. package/dist/src/config.d.ts +0 -6
  2. package/dist/src/config.d.ts.map +1 -1
  3. package/dist/src/connection-manager/auto-dialler.d.ts +56 -0
  4. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -0
  5. package/dist/src/connection-manager/index.d.ts +0 -10
  6. package/dist/src/connection-manager/index.d.ts.map +1 -1
  7. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  8. package/dist/src/dialer/dial-request.d.ts.map +1 -1
  9. package/dist/src/dialer/index.d.ts +1 -1
  10. package/dist/src/dialer/index.d.ts.map +1 -1
  11. package/dist/src/errors.d.ts +24 -0
  12. package/dist/src/index.d.ts +3 -27
  13. package/dist/src/index.d.ts.map +1 -1
  14. package/dist/src/keychain/cms.d.ts.map +1 -1
  15. package/dist/src/keychain/index.d.ts.map +1 -1
  16. package/dist/src/peer-routing.d.ts +8 -1
  17. package/dist/src/peer-routing.d.ts.map +1 -1
  18. package/dist/src/peer-store/book.d.ts.map +1 -1
  19. package/package.json +32 -31
  20. package/src/circuit/index.js +2 -2
  21. package/src/config.js +1 -7
  22. package/src/connection-manager/auto-dialler.js +118 -0
  23. package/src/connection-manager/index.js +0 -52
  24. package/src/content-routing/index.js +2 -2
  25. package/src/dht/dht-peer-routing.js +6 -2
  26. package/src/dialer/dial-request.js +10 -3
  27. package/src/dialer/index.js +10 -9
  28. package/src/errors.js +25 -1
  29. package/src/index.js +9 -9
  30. package/src/keychain/cms.js +7 -6
  31. package/src/keychain/index.js +25 -24
  32. package/src/nat-manager.js +1 -1
  33. package/src/peer-routing.js +21 -8
  34. package/src/peer-store/book.js +4 -7
  35. package/src/ping/index.js +2 -2
  36. package/src/upgrader.js +1 -1
@@ -5,11 +5,13 @@ const log = Object.assign(debug('libp2p:peer-routing'), {
5
5
  error: debug('libp2p:peer-routing:err')
6
6
  })
7
7
  const errCode = require('err-code')
8
+ const errors = require('./errors')
8
9
  const {
9
10
  storeAddresses,
10
11
  uniquePeers,
11
12
  requirePeers
12
13
  } = require('./content-routing/utils')
14
+ const { TimeoutController } = require('timeout-abort-controller')
13
15
 
14
16
  const merge = require('it-merge')
15
17
  const { pipe } = require('it-pipe')
@@ -34,6 +36,7 @@ const { DHTPeerRouting } = require('./dht/dht-peer-routing')
34
36
  * @property {boolean} [enabled = true] - Whether to enable the Refresh manager
35
37
  * @property {number} [bootDelay = 6e5] - Boot delay to start the Refresh Manager (in ms)
36
38
  * @property {number} [interval = 10e3] - Interval between each Refresh Manager run (in ms)
39
+ * @property {number} [timeout = 10e3] - How long to let each refresh run (in ms)
37
40
  *
38
41
  * @typedef {Object} PeerRoutingOptions
39
42
  * @property {RefreshManagerOptions} [refreshManager]
@@ -79,7 +82,7 @@ class PeerRouting {
79
82
  async _findClosestPeersTask () {
80
83
  try {
81
84
  // nb getClosestPeers adds the addresses to the address book
82
- await drain(this.getClosestPeers(this._peerId.id))
85
+ await drain(this.getClosestPeers(this._peerId.id, { timeout: this._refreshManagerOptions.timeout || 10e3 }))
83
86
  } catch (/** @type {any} */ err) {
84
87
  log.error(err)
85
88
  }
@@ -102,19 +105,24 @@ class PeerRouting {
102
105
  */
103
106
  async findPeer (id, options) { // eslint-disable-line require-await
104
107
  if (!this._routers.length) {
105
- throw errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE')
108
+ throw errCode(new Error('No peer routers available'), errors.codes.ERR_NO_ROUTERS_AVAILABLE)
106
109
  }
107
110
 
108
111
  if (id.toB58String() === this._peerId.toB58String()) {
109
- throw errCode(new Error('Should not try to find self'), 'ERR_FIND_SELF')
112
+ throw errCode(new Error('Should not try to find self'), errors.codes.ERR_FIND_SELF)
110
113
  }
111
114
 
112
115
  const output = await pipe(
113
116
  merge(
114
- ...this._routers.map(router => [router.findPeer(id, options)])
117
+ ...this._routers.map(router => (async function * () {
118
+ try {
119
+ yield await router.findPeer(id, options)
120
+ } catch (err) {
121
+ log.error(err)
122
+ }
123
+ })())
115
124
  ),
116
125
  (source) => filter(source, Boolean),
117
- // @ts-ignore findPeer resolves a Promise
118
126
  (source) => storeAddresses(source, this._peerStore),
119
127
  (source) => first(source)
120
128
  )
@@ -123,7 +131,7 @@ class PeerRouting {
123
131
  return output
124
132
  }
125
133
 
126
- throw errCode(new Error('not found'), 'NOT_FOUND')
134
+ throw errCode(new Error(errors.messages.NOT_FOUND), errors.codes.ERR_NOT_FOUND)
127
135
  }
128
136
 
129
137
  /**
@@ -131,12 +139,17 @@ class PeerRouting {
131
139
  *
132
140
  * @param {Uint8Array} key - A CID like key
133
141
  * @param {Object} [options]
134
- * @param {number} [options.timeout=30e3] - How long the query can take.
142
+ * @param {number} [options.timeout=30e3] - How long the query can take
143
+ * @param {AbortSignal} [options.signal] - An AbortSignal to abort the request
135
144
  * @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>}
136
145
  */
137
146
  async * getClosestPeers (key, options = { timeout: 30e3 }) {
138
147
  if (!this._routers.length) {
139
- throw errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE')
148
+ throw errCode(new Error('No peer routers available'), errors.codes.ERR_NO_ROUTERS_AVAILABLE)
149
+ }
150
+
151
+ if (options.timeout) {
152
+ options.signal = new TimeoutController(options.timeout).signal
140
153
  }
141
154
 
142
155
  yield * pipe(
@@ -2,10 +2,7 @@
2
2
 
3
3
  const errcode = require('err-code')
4
4
  const PeerId = require('peer-id')
5
-
6
- const {
7
- codes: { ERR_INVALID_PARAMETERS }
8
- } = require('../errors')
5
+ const { codes } = require('../errors')
9
6
 
10
7
  /**
11
8
  * @param {any} data
@@ -48,7 +45,7 @@ class Book {
48
45
  * @param {any[]|any} data
49
46
  */
50
47
  set (peerId, data) {
51
- throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
48
+ throw errcode(new Error('set must be implemented by the subclass'), codes.ERR_NOT_IMPLEMENTED)
52
49
  }
53
50
 
54
51
  /**
@@ -94,7 +91,7 @@ class Book {
94
91
  */
95
92
  get (peerId) {
96
93
  if (!PeerId.isPeerId(peerId)) {
97
- throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
94
+ throw errcode(new Error('peerId must be an instance of peer-id'), codes.ERR_INVALID_PARAMETERS)
98
95
  }
99
96
 
100
97
  const rec = this.data.get(peerId.toB58String())
@@ -111,7 +108,7 @@ class Book {
111
108
  */
112
109
  delete (peerId) {
113
110
  if (!PeerId.isPeerId(peerId)) {
114
- throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
111
+ throw errcode(new Error('peerId must be an instance of peer-id'), codes.ERR_INVALID_PARAMETERS)
115
112
  }
116
113
 
117
114
  if (!this.data.delete(peerId.toB58String())) {
package/src/ping/index.js CHANGED
@@ -5,7 +5,7 @@ const log = Object.assign(debug('libp2p:ping'), {
5
5
  error: debug('libp2p:ping:err')
6
6
  })
7
7
  const errCode = require('err-code')
8
-
8
+ const { codes } = require('../errors')
9
9
  const crypto = require('libp2p-crypto')
10
10
  const { pipe } = require('it-pipe')
11
11
  // @ts-ignore it-buffer has no types exported
@@ -50,7 +50,7 @@ async function ping (node, peer) {
50
50
  const end = Date.now()
51
51
 
52
52
  if (!equals(data, result)) {
53
- throw errCode(new Error('Received wrong ping ack'), 'ERR_WRONG_PING_ACK')
53
+ throw errCode(new Error('Received wrong ping ack'), codes.ERR_WRONG_PING_ACK)
54
54
  }
55
55
 
56
56
  return end - start
package/src/upgrader.js CHANGED
@@ -297,7 +297,7 @@ class Upgrader {
297
297
  maConn.timeline.upgraded = Date.now()
298
298
 
299
299
  const errConnectionNotMultiplexed = () => {
300
- throw errCode(new Error('connection is not multiplexed'), 'ERR_CONNECTION_NOT_MULTIPLEXED')
300
+ throw errCode(new Error('connection is not multiplexed'), codes.ERR_CONNECTION_NOT_MULTIPLEXED)
301
301
  }
302
302
 
303
303
  // Create the connection