libp2p 0.35.8 → 0.36.0

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 (107) hide show
  1. package/dist/index.min.js +55 -0
  2. package/dist/src/circuit/auto-relay.d.ts +8 -10
  3. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  4. package/dist/src/config.d.ts +2 -0
  5. package/dist/src/config.d.ts.map +1 -1
  6. package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
  7. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  8. package/dist/src/connection-manager/index.d.ts +1 -2
  9. package/dist/src/connection-manager/index.d.ts.map +1 -1
  10. package/dist/src/content-routing/utils.d.ts +3 -3
  11. package/dist/src/content-routing/utils.d.ts.map +1 -1
  12. package/dist/src/dialer/dial-request.d.ts.map +1 -1
  13. package/dist/src/dialer/index.d.ts +12 -7
  14. package/dist/src/dialer/index.d.ts.map +1 -1
  15. package/dist/src/errors.d.ts +2 -0
  16. package/dist/src/fetch/constants.d.ts +2 -0
  17. package/dist/src/fetch/constants.d.ts.map +1 -0
  18. package/dist/src/fetch/index.d.ts +76 -0
  19. package/dist/src/fetch/index.d.ts.map +1 -0
  20. package/dist/src/fetch/proto.d.ts +134 -0
  21. package/dist/src/identify/index.d.ts +4 -4
  22. package/dist/src/identify/index.d.ts.map +1 -1
  23. package/dist/src/index.d.ts +116 -7
  24. package/dist/src/index.d.ts.map +1 -1
  25. package/dist/src/metrics/index.d.ts +9 -4
  26. package/dist/src/metrics/index.d.ts.map +1 -1
  27. package/dist/src/metrics/tracked-map.d.ts +6 -1
  28. package/dist/src/metrics/tracked-map.d.ts.map +1 -1
  29. package/dist/src/peer-routing.d.ts +1 -1
  30. package/dist/src/peer-routing.d.ts.map +1 -1
  31. package/dist/src/peer-store/address-book.d.ts +30 -84
  32. package/dist/src/peer-store/address-book.d.ts.map +1 -1
  33. package/dist/src/peer-store/index.d.ts +28 -94
  34. package/dist/src/peer-store/index.d.ts.map +1 -1
  35. package/dist/src/peer-store/key-book.d.ts +31 -15
  36. package/dist/src/peer-store/key-book.d.ts.map +1 -1
  37. package/dist/src/peer-store/metadata-book.d.ts +33 -29
  38. package/dist/src/peer-store/metadata-book.d.ts.map +1 -1
  39. package/dist/src/peer-store/pb/peer.d.ts +222 -0
  40. package/dist/src/peer-store/proto-book.d.ts +27 -28
  41. package/dist/src/peer-store/proto-book.d.ts.map +1 -1
  42. package/dist/src/peer-store/store.d.ts +73 -0
  43. package/dist/src/peer-store/store.d.ts.map +1 -0
  44. package/dist/src/peer-store/types.d.ts +202 -0
  45. package/dist/src/peer-store/types.d.ts.map +1 -0
  46. package/dist/src/ping/index.d.ts.map +1 -1
  47. package/dist/src/record/peer-record/index.d.ts +2 -2
  48. package/dist/src/record/peer-record/index.d.ts.map +1 -1
  49. package/dist/src/registrar.d.ts +5 -5
  50. package/dist/src/registrar.d.ts.map +1 -1
  51. package/dist/src/types.d.ts +90 -0
  52. package/dist/src/types.d.ts.map +1 -0
  53. package/dist/src/upgrader.d.ts +8 -2
  54. package/dist/src/upgrader.d.ts.map +1 -1
  55. package/package.json +23 -26
  56. package/src/circuit/auto-relay.js +52 -38
  57. package/src/config.js +2 -0
  58. package/src/connection-manager/auto-dialler.js +20 -8
  59. package/src/connection-manager/index.js +42 -22
  60. package/src/content-routing/utils.js +4 -4
  61. package/src/dialer/dial-request.js +4 -1
  62. package/src/dialer/index.js +38 -8
  63. package/src/errors.js +2 -0
  64. package/src/fetch/README.md +36 -0
  65. package/src/fetch/constants.js +6 -0
  66. package/src/fetch/index.js +159 -0
  67. package/src/fetch/proto.d.ts +134 -0
  68. package/src/fetch/proto.js +333 -0
  69. package/src/fetch/proto.proto +15 -0
  70. package/src/identify/index.js +47 -38
  71. package/src/index.js +72 -27
  72. package/src/metrics/index.js +15 -8
  73. package/src/metrics/tracked-map.js +40 -14
  74. package/src/peer-routing.js +4 -1
  75. package/src/peer-store/address-book.js +254 -226
  76. package/src/peer-store/index.js +79 -110
  77. package/src/peer-store/key-book.js +99 -54
  78. package/src/peer-store/metadata-book.js +171 -101
  79. package/src/peer-store/pb/peer.d.ts +222 -0
  80. package/src/peer-store/pb/peer.js +643 -0
  81. package/src/peer-store/pb/peer.proto +31 -0
  82. package/src/peer-store/proto-book.js +171 -105
  83. package/src/peer-store/store.js +263 -0
  84. package/src/peer-store/types.ts +245 -0
  85. package/src/ping/index.js +6 -0
  86. package/src/record/peer-record/index.js +1 -1
  87. package/src/record/utils.js +1 -1
  88. package/src/registrar.js +4 -4
  89. package/src/types.ts +98 -0
  90. package/src/upgrader.js +32 -2
  91. package/dist/src/peer-store/book.d.ts +0 -81
  92. package/dist/src/peer-store/book.d.ts.map +0 -1
  93. package/dist/src/peer-store/persistent/consts.d.ts +0 -6
  94. package/dist/src/peer-store/persistent/consts.d.ts.map +0 -1
  95. package/dist/src/peer-store/persistent/index.d.ts +0 -129
  96. package/dist/src/peer-store/persistent/index.d.ts.map +0 -1
  97. package/dist/src/peer-store/persistent/pb/address-book.d.ts +0 -198
  98. package/dist/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
  99. package/src/peer-store/book.js +0 -124
  100. package/src/peer-store/persistent/consts.js +0 -15
  101. package/src/peer-store/persistent/index.js +0 -408
  102. package/src/peer-store/persistent/pb/address-book.d.ts +0 -198
  103. package/src/peer-store/persistent/pb/address-book.js +0 -522
  104. package/src/peer-store/persistent/pb/address-book.proto +0 -27
  105. package/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
  106. package/src/peer-store/persistent/pb/proto-book.js +0 -157
  107. package/src/peer-store/persistent/pb/proto-book.proto +0 -5
@@ -1,152 +1,121 @@
1
1
  'use strict'
2
2
 
3
- const errcode = require('err-code')
4
-
3
+ const debug = require('debug')
5
4
  const { EventEmitter } = require('events')
6
- const PeerId = require('peer-id')
7
-
8
5
  const AddressBook = require('./address-book')
9
6
  const KeyBook = require('./key-book')
10
7
  const MetadataBook = require('./metadata-book')
11
8
  const ProtoBook = require('./proto-book')
12
-
13
- const {
14
- codes: { ERR_INVALID_PARAMETERS }
15
- } = require('../errors')
9
+ const Store = require('./store')
16
10
 
17
11
  /**
18
- * @typedef {import('./address-book').Address} Address
12
+ * @typedef {import('./types').PeerStore} PeerStore
13
+ * @typedef {import('./types').Peer} Peer
14
+ * @typedef {import('peer-id')} PeerId
15
+ * @typedef {import('multiaddr').Multiaddr} Multiaddr
19
16
  */
20
17
 
18
+ const log = Object.assign(debug('libp2p:peer-store'), {
19
+ error: debug('libp2p:peer-store:err')
20
+ })
21
+
21
22
  /**
22
- * @extends {EventEmitter}
23
+ * An implementation of PeerStore that stores data in a Datastore
23
24
  *
24
- * @fires PeerStore#peer Emitted when a new peer is added.
25
- * @fires PeerStore#change:protocols Emitted when a known peer supports a different set of protocols.
26
- * @fires PeerStore#change:multiaddrs Emitted when a known peer has a different set of multiaddrs.
27
- * @fires PeerStore#change:pubkey Emitted emitted when a peer's public key is known.
28
- * @fires PeerStore#change:metadata Emitted when the known metadata of a peer change.
25
+ * @implements {PeerStore}
29
26
  */
30
- class PeerStore extends EventEmitter {
27
+ class DefaultPeerStore extends EventEmitter {
31
28
  /**
32
- * Peer object
33
- *
34
- * @typedef {Object} Peer
35
- * @property {PeerId} id peer's peer-id instance.
36
- * @property {Address[]} addresses peer's addresses containing its multiaddrs and metadata.
37
- * @property {string[]} protocols peer's supported protocols.
38
- * @property {Map<string, Uint8Array>|undefined} metadata peer's metadata map.
29
+ * @param {object} properties
30
+ * @param {PeerId} properties.peerId
31
+ * @param {import('interface-datastore').Datastore} properties.datastore
32
+ * @param {(peerId: PeerId, multiaddr: Multiaddr) => Promise<boolean>} properties.addressFilter
39
33
  */
40
-
41
- /**
42
- * Responsible for managing known peers, as well as their addresses, protocols and metadata.
43
- *
44
- * @param {object} options
45
- * @param {PeerId} options.peerId
46
- * @class
47
- */
48
- constructor ({ peerId }) {
34
+ constructor ({ peerId, datastore, addressFilter }) {
49
35
  super()
50
36
 
51
37
  this._peerId = peerId
38
+ this._store = new Store(datastore)
52
39
 
53
- /**
54
- * AddressBook containing a map of peerIdStr to Address.
55
- */
56
- this.addressBook = new AddressBook(this)
57
-
58
- /**
59
- * KeyBook containing a map of peerIdStr to their PeerId with public keys.
60
- */
61
- this.keyBook = new KeyBook(this)
62
-
63
- /**
64
- * MetadataBook containing a map of peerIdStr to their metadata Map.
65
- */
66
- this.metadataBook = new MetadataBook(this)
67
-
68
- /**
69
- * ProtoBook containing a map of peerIdStr to supported protocols.
70
- */
71
- this.protoBook = new ProtoBook(this)
40
+ this.addressBook = new AddressBook(this.emit.bind(this), this._store, addressFilter)
41
+ this.keyBook = new KeyBook(this.emit.bind(this), this._store)
42
+ this.metadataBook = new MetadataBook(this.emit.bind(this), this._store)
43
+ this.protoBook = new ProtoBook(this.emit.bind(this), this._store)
72
44
  }
73
45
 
74
- /**
75
- * Start the PeerStore.
76
- */
77
- start () {}
78
-
79
- /**
80
- * Stop the PeerStore.
81
- */
82
- stop () {}
46
+ async * getPeers () {
47
+ log('getPeers await read lock')
48
+ const release = await this._store.lock.readLock()
49
+ log('getPeers got read lock')
50
+
51
+ try {
52
+ for await (const peer of this._store.all()) {
53
+ if (peer.id.toB58String() === this._peerId.toB58String()) {
54
+ // Remove self peer if present
55
+ continue
56
+ }
57
+
58
+ yield peer
59
+ }
60
+ } finally {
61
+ log('getPeers release read lock')
62
+ release()
63
+ }
64
+ }
83
65
 
84
66
  /**
85
- * Get all the stored information of every peer known.
67
+ * Delete the information of the given peer in every book
86
68
  *
87
- * @returns {Map<string, Peer>}
69
+ * @param {PeerId} peerId
88
70
  */
89
- get peers () {
90
- const storedPeers = new Set([
91
- ...this.addressBook.data.keys(),
92
- ...this.keyBook.data.keys(),
93
- ...this.protoBook.data.keys(),
94
- ...this.metadataBook.data.keys()
95
- ])
96
-
97
- // Remove self peer if present
98
- this._peerId && storedPeers.delete(this._peerId.toB58String())
99
-
100
- const peersData = new Map()
101
- storedPeers.forEach((idStr) => {
102
- peersData.set(idStr, this.get(PeerId.createFromB58String(idStr)))
103
- })
104
-
105
- return peersData
71
+ async delete (peerId) {
72
+ log('delete await write lock')
73
+ const release = await this._store.lock.writeLock()
74
+ log('delete got write lock')
75
+
76
+ try {
77
+ await this._store.delete(peerId)
78
+ } finally {
79
+ log('delete release write lock')
80
+ release()
81
+ }
106
82
  }
107
83
 
108
84
  /**
109
- * Delete the information of the given peer in every book.
85
+ * Get the stored information of a given peer
110
86
  *
111
87
  * @param {PeerId} peerId
112
- * @returns {boolean} true if found and removed
113
88
  */
114
- delete (peerId) {
115
- const addressesDeleted = this.addressBook.delete(peerId)
116
- const keyDeleted = this.keyBook.delete(peerId)
117
- const protocolsDeleted = this.protoBook.delete(peerId)
118
- const metadataDeleted = this.metadataBook.delete(peerId)
119
-
120
- return addressesDeleted || keyDeleted || protocolsDeleted || metadataDeleted
89
+ async get (peerId) {
90
+ log('get await read lock')
91
+ const release = await this._store.lock.readLock()
92
+ log('get got read lock')
93
+
94
+ try {
95
+ return this._store.load(peerId)
96
+ } finally {
97
+ log('get release read lock')
98
+ release()
99
+ }
121
100
  }
122
101
 
123
102
  /**
124
- * Get the stored information of a given peer.
103
+ * Returns true if we have a record of the peer
125
104
  *
126
105
  * @param {PeerId} peerId
127
- * @returns {Peer|undefined}
128
106
  */
129
- get (peerId) {
130
- if (!PeerId.isPeerId(peerId)) {
131
- throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
132
- }
133
-
134
- const id = this.keyBook.data.get(peerId.toB58String())
135
- const addresses = this.addressBook.get(peerId)
136
- const metadata = this.metadataBook.get(peerId)
137
- const protocols = this.protoBook.get(peerId)
138
-
139
- if (!id && !addresses && !metadata && !protocols) {
140
- return undefined
141
- }
142
-
143
- return {
144
- id: id || peerId,
145
- addresses: addresses || [],
146
- protocols: protocols || [],
147
- metadata: metadata
107
+ async has (peerId) {
108
+ log('has await read lock')
109
+ const release = await this._store.lock.readLock()
110
+ log('has got read lock')
111
+
112
+ try {
113
+ return this._store.has(peerId)
114
+ } finally {
115
+ log('has release read lock')
116
+ release()
148
117
  }
149
118
  }
150
119
  }
151
120
 
152
- module.exports = PeerStore
121
+ module.exports = DefaultPeerStore
@@ -1,96 +1,141 @@
1
1
  'use strict'
2
2
 
3
3
  const debug = require('debug')
4
- const log = Object.assign(debug('libp2p:peer-store:key-book'), {
5
- error: debug('libp2p:peer-store:key-book:err')
6
- })
7
4
  const errcode = require('err-code')
8
-
5
+ const { codes } = require('../errors')
9
6
  const PeerId = require('peer-id')
10
-
11
- const Book = require('./book')
12
-
13
- const {
14
- codes: { ERR_INVALID_PARAMETERS }
15
- } = require('../errors')
7
+ const { equals: uint8arrayEquals } = require('uint8arrays/equals')
16
8
 
17
9
  /**
18
- * @typedef {import('./')} PeerStore
19
- * @typedef {import('libp2p-crypto').PublicKey} PublicKey
10
+ * @typedef {import('./types').PeerStore} PeerStore
11
+ * @typedef {import('./types').KeyBook} KeyBook
12
+ * @typedef {import('libp2p-interfaces/src/keys/types').PublicKey} PublicKey
20
13
  */
21
14
 
15
+ const log = Object.assign(debug('libp2p:peer-store:key-book'), {
16
+ error: debug('libp2p:peer-store:key-book:err')
17
+ })
18
+
19
+ const EVENT_NAME = 'change:pubkey'
20
+
22
21
  /**
23
- * @extends {Book}
22
+ * @implements {KeyBook}
24
23
  */
25
- class KeyBook extends Book {
24
+ class PeerStoreKeyBook {
26
25
  /**
27
26
  * The KeyBook is responsible for keeping the known public keys of a peer.
28
27
  *
29
- * @class
30
- * @param {PeerStore} peerStore
28
+ * @param {PeerStore["emit"]} emit
29
+ * @param {import('./types').Store} store
31
30
  */
32
- constructor (peerStore) {
33
- super({
34
- peerStore,
35
- eventName: 'change:pubkey',
36
- eventProperty: 'pubkey',
37
- eventTransformer: (data) => data.pubKey
38
- })
39
-
40
- /**
41
- * Map known peers to their known Public Key.
42
- *
43
- * @type {Map<string, PeerId>}
44
- */
45
- this.data = new Map()
31
+ constructor (emit, store) {
32
+ this._emit = emit
33
+ this._store = store
46
34
  }
47
35
 
48
36
  /**
49
- * Set the Peer public key.
37
+ * Set the Peer public key
50
38
  *
51
- * @override
52
39
  * @param {PeerId} peerId
53
40
  * @param {PublicKey} publicKey
54
- * @returns {KeyBook}
55
41
  */
56
- set (peerId, publicKey) {
42
+ async set (peerId, publicKey) {
57
43
  if (!PeerId.isPeerId(peerId)) {
58
44
  log.error('peerId must be an instance of peer-id to store data')
59
- throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
45
+ throw errcode(new Error('peerId must be an instance of peer-id'), codes.ERR_INVALID_PARAMETERS)
60
46
  }
61
47
 
62
- const id = peerId.toB58String()
63
- const recPeerId = this.data.get(id)
64
-
65
- // If no record available, and this is valid
66
- if (!recPeerId && publicKey) {
67
- // This might be unecessary, but we want to store the PeerId
68
- // to avoid an async operation when reconstructing the PeerId
69
- peerId.pubKey = publicKey
48
+ if (!publicKey) {
49
+ log.error('publicKey must be an instance of PublicKey to store data')
50
+ throw errcode(new Error('publicKey must be an instance of PublicKey'), codes.ERR_INVALID_PARAMETERS)
51
+ }
70
52
 
71
- this._setData(peerId, peerId)
72
- log(`stored provided public key for ${id}`)
53
+ log('set await write lock')
54
+ const release = await this._store.lock.writeLock()
55
+ log('set got write lock')
56
+
57
+ let updatedKey = false
58
+
59
+ try {
60
+ try {
61
+ const existing = await this._store.load(peerId)
62
+
63
+ if (existing.pubKey && uint8arrayEquals(existing.pubKey.bytes, publicKey.bytes)) {
64
+ return
65
+ }
66
+ } catch (/** @type {any} */ err) {
67
+ if (err.code !== codes.ERR_NOT_FOUND) {
68
+ throw err
69
+ }
70
+ }
71
+
72
+ await this._store.patchOrCreate(peerId, {
73
+ pubKey: publicKey
74
+ })
75
+ updatedKey = true
76
+ } finally {
77
+ log('set release write lock')
78
+ release()
73
79
  }
74
80
 
75
- return this
81
+ if (updatedKey) {
82
+ this._emit(EVENT_NAME, { peerId, pubKey: publicKey })
83
+ }
76
84
  }
77
85
 
78
86
  /**
79
- * Get Public key of the given PeerId, if stored.
87
+ * Get Public key of the given PeerId, if stored
80
88
  *
81
- * @override
82
89
  * @param {PeerId} peerId
83
- * @returns {PublicKey | undefined}
84
90
  */
85
- get (peerId) {
91
+ async get (peerId) {
86
92
  if (!PeerId.isPeerId(peerId)) {
87
- throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
93
+ log.error('peerId must be an instance of peer-id to store data')
94
+ throw errcode(new Error('peerId must be an instance of peer-id'), codes.ERR_INVALID_PARAMETERS)
88
95
  }
89
96
 
90
- const rec = this.data.get(peerId.toB58String())
97
+ log('get await write lock')
98
+ const release = await this._store.lock.readLock()
99
+ log('get got write lock')
100
+
101
+ try {
102
+ const peer = await this._store.load(peerId)
103
+
104
+ return peer.pubKey
105
+ } catch (/** @type {any} */ err) {
106
+ if (err.code !== codes.ERR_NOT_FOUND) {
107
+ throw err
108
+ }
109
+ } finally {
110
+ log('get release write lock')
111
+ release()
112
+ }
113
+ }
114
+
115
+ /**
116
+ * @param {PeerId} peerId
117
+ */
118
+ async delete (peerId) {
119
+ if (!PeerId.isPeerId(peerId)) {
120
+ log.error('peerId must be an instance of peer-id to store data')
121
+ throw errcode(new Error('peerId must be an instance of peer-id'), codes.ERR_INVALID_PARAMETERS)
122
+ }
123
+
124
+ log('delete await write lock')
125
+ const release = await this._store.lock.writeLock()
126
+ log('delete got write lock')
127
+
128
+ try {
129
+ await this._store.patchOrCreate(peerId, {
130
+ pubKey: undefined
131
+ })
132
+ } finally {
133
+ log('delete release write lock')
134
+ release()
135
+ }
91
136
 
92
- return rec ? rec.pubKey : undefined
137
+ this._emit(EVENT_NAME, { peerId, pubKey: undefined })
93
138
  }
94
139
  }
95
140
 
96
- module.exports = KeyBook
141
+ module.exports = PeerStoreKeyBook