libp2p 0.36.0 → 0.36.1

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.
@@ -137,98 +137,6 @@ declare class Libp2p extends EventEmitter {
137
137
  enabled: boolean;
138
138
  };
139
139
  peerStore: {
140
- /**
141
- * @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
142
- * @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
143
- * @typedef {import('libp2p-interfaces/src/transport/types').TransportFactory<any, any>} TransportFactory
144
- * @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxerFactory} MuxerFactory
145
- * @typedef {import('libp2p-interfaces/src/content-routing/types').ContentRouting} ContentRoutingModule
146
- * @typedef {import('libp2p-interfaces/src/peer-discovery/types').PeerDiscoveryFactory} PeerDiscoveryFactory
147
- * @typedef {import('libp2p-interfaces/src/peer-routing/types').PeerRouting} PeerRoutingModule
148
- * @typedef {import('libp2p-interfaces/src/crypto/types').Crypto} Crypto
149
- * @typedef {import('libp2p-interfaces/src/pubsub')} Pubsub
150
- * @typedef {import('libp2p-interfaces/src/pubsub').PubsubOptions} PubsubOptions
151
- * @typedef {import('interface-datastore').Datastore} Datastore
152
- * @typedef {import('./pnet')} Protector
153
- * @typedef {import('./types').ConnectionGater} ConnectionGater
154
- * @typedef {Object} PersistentPeerStoreOptions
155
- * @property {number} [threshold]
156
- */
157
- /**
158
- * @typedef {Object} HandlerProps
159
- * @property {Connection} connection
160
- * @property {MuxedStream} stream
161
- * @property {string} protocol
162
- *
163
- * @typedef {Object} DhtOptions
164
- * @property {boolean} [enabled = false]
165
- * @property {number} [kBucketSize = 20]
166
- * @property {boolean} [clientMode]
167
- * @property {import('libp2p-interfaces/src/types').DhtSelectors} [selectors]
168
- * @property {import('libp2p-interfaces/src/types').DhtValidators} [validators]
169
- *
170
- * @typedef {Object} KeychainOptions
171
- * @property {Datastore} [datastore]
172
- *
173
- * @typedef {Object} PeerStoreOptions
174
- * @property {boolean} persistence
175
- *
176
- * @typedef {Object} PubsubLocalOptions
177
- * @property {boolean} enabled
178
- *
179
- * @typedef {Object} MetricsOptions
180
- * @property {boolean} enabled
181
- *
182
- * @typedef {Object} RelayOptions
183
- * @property {boolean} [enabled = true]
184
- * @property {import('./circuit').RelayAdvertiseOptions} [advertise]
185
- * @property {import('./circuit').HopOptions} [hop]
186
- * @property {import('./circuit').AutoRelayOptions} [autoRelay]
187
- *
188
- * @typedef {Object} Libp2pConfig
189
- * @property {DhtOptions} [dht] dht module options
190
- * @property {import('./nat-manager').NatManagerOptions} [nat]
191
- * @property {Record<string, Object|boolean>} [peerDiscovery]
192
- * @property {PubsubLocalOptions & PubsubOptions} [pubsub] pubsub module options
193
- * @property {RelayOptions} [relay]
194
- * @property {Record<string, Object>} [transport] transport options indexed by transport key
195
- *
196
- * @typedef {Object} Libp2pModules
197
- * @property {TransportFactory[]} transport
198
- * @property {MuxerFactory[]} streamMuxer
199
- * @property {Crypto[]} connEncryption
200
- * @property {PeerDiscoveryFactory[]} [peerDiscovery]
201
- * @property {PeerRoutingModule[]} [peerRouting]
202
- * @property {ContentRoutingModule[]} [contentRouting]
203
- * @property {Object} [dht]
204
- * @property {{new(...args: any[]): Pubsub}} [pubsub]
205
- * @property {Protector} [connProtector]
206
- *
207
- * @typedef {Object} Libp2pOptions
208
- * @property {Libp2pModules} modules libp2p modules to use
209
- * @property {import('./address-manager').AddressManagerOptions} [addresses]
210
- * @property {import('./connection-manager').ConnectionManagerOptions} [connectionManager]
211
- * @property {Partial<import('./types').ConnectionGater>} [connectionGater]
212
- * @property {Datastore} [datastore]
213
- * @property {import('./dialer').DialerOptions} [dialer]
214
- * @property {import('./identify/index').HostProperties} [host] libp2p host
215
- * @property {KeychainOptions & import('./keychain/index').KeychainOptions} [keychain]
216
- * @property {MetricsOptions & import('./metrics').MetricsOptions} [metrics]
217
- * @property {import('./peer-routing').PeerRoutingOptions} [peerRouting]
218
- * @property {PeerStoreOptions} [peerStore]
219
- * @property {import('./transport-manager').TransportManagerOptions} [transportManager]
220
- * @property {Libp2pConfig} [config]
221
- *
222
- * @typedef {Object} constructorOptions
223
- * @property {PeerId} peerId
224
- *
225
- * @typedef {Object} CreateOptions
226
- * @property {PeerId} [peerId]
227
- *
228
- * @extends {EventEmitter}
229
- * @fires Libp2p#error Emitted when an error occurs
230
- * @fires Libp2p#peer:discovery Emitted when a peer is discovered
231
- */
232
140
  persistence: boolean;
233
141
  threshold: number;
234
142
  };
@@ -369,6 +277,7 @@ declare class Libp2p extends EventEmitter {
369
277
  */
370
278
  private _onDiscoveryPeer;
371
279
  fetchService: FetchService;
280
+ pingService: PingService;
372
281
  /**
373
282
  * Starts the libp2p node and all its subsystems
374
283
  *
@@ -577,6 +486,7 @@ import IdentifyService = require("./identify");
577
486
  import PeerRouting = require("./peer-routing");
578
487
  import ContentRouting = require("./content-routing");
579
488
  import FetchService = require("./fetch");
489
+ import PingService = require("./ping");
580
490
  import { Multiaddr } from "multiaddr";
581
491
  type Connection = import("libp2p-interfaces/src/connection/connection");
582
492
  type CreateOptions = {
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.js"],"names":[],"mappings":";AAqCA;;;;;;;;;;;;;;;;GAgBG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0EG;AACH;IACE;;;;;;OAMG;IACH,uBAHW,aAAa,GAAG,aAAa,GAC3B,QAAQ,MAAM,CAAC,CAa3B;IAED;;;;;OAKG;IACH,sBAFW,aAAa,GAAG,kBAAkB,EAgM5C;IA1LC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;YA5HJ;;;;;;;;;;;;;;;;eAgBG;YAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;eA0EG;;;;;;;;;;;;;;;;;;;;;;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E,YAAY;4BAC/D,OAAO,6CAA6C,EAAE,cAAc;4BACpE,OAAO,4CAA4C,EAAE,oBAAoB;yBACzE,OAAO,0CAA0C,EAAE,WAAW;cAC9D,OAAO,oCAAoC,EAAE,MAAM;cACnD,OAAO,8BAA8B,CAAC;qBACtC,OAAO,8BAA8B,EAAE,aAAa;iBACpD,OAAO,qBAAqB,EAAE,SAAS;iBACvC,OAAO,QAAQ,CAAC;;;;;;;;;;;;;;;iBAuBf,OAAO;;;aAGP,OAAO;;;aAGP,OAAO"}
@@ -1,35 +1,42 @@
1
- export = ping;
1
+ export = PingService;
2
2
  /**
3
3
  * @typedef {import('../')} Libp2p
4
4
  * @typedef {import('multiaddr').Multiaddr} Multiaddr
5
5
  * @typedef {import('peer-id')} PeerId
6
6
  * @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
7
7
  */
8
- /**
9
- * Ping a given peer and wait for its response, getting the operation latency.
10
- *
11
- * @param {Libp2p} node
12
- * @param {PeerId|Multiaddr} peer
13
- * @returns {Promise<number>}
14
- */
15
- declare function ping(node: Libp2p, peer: PeerId | Multiaddr): Promise<number>;
16
- declare namespace ping {
17
- export { mount, unmount, Libp2p, Multiaddr, PeerId, MuxedStream };
8
+ declare class PingService {
9
+ /**
10
+ * @param {import('../')} libp2p
11
+ */
12
+ static getProtocolStr(libp2p: import('../')): string;
13
+ /**
14
+ * @param {Libp2p} libp2p
15
+ */
16
+ constructor(libp2p: Libp2p);
17
+ _libp2p: import("../");
18
+ /**
19
+ * A handler to register with Libp2p to process ping messages
20
+ *
21
+ * @param {Object} options
22
+ * @param {MuxedStream} options.stream
23
+ */
24
+ handleMessage({ stream }: {
25
+ stream: MuxedStream;
26
+ }): any;
27
+ /**
28
+ * Ping a given peer and wait for its response, getting the operation latency.
29
+ *
30
+ * @param {PeerId|Multiaddr} peer
31
+ * @returns {Promise<number>}
32
+ */
33
+ ping(peer: PeerId | Multiaddr): Promise<number>;
18
34
  }
19
- type Libp2p = import('../');
35
+ declare namespace PingService {
36
+ export { Libp2p, Multiaddr, PeerId, MuxedStream };
37
+ }
38
+ type MuxedStream = import('libp2p-interfaces/src/stream-muxer/types').MuxedStream;
20
39
  type PeerId = import('peer-id');
21
40
  type Multiaddr = import('multiaddr').Multiaddr;
22
- /**
23
- * Subscribe ping protocol handler.
24
- *
25
- * @param {Libp2p} node
26
- */
27
- declare function mount(node: Libp2p): void;
28
- /**
29
- * Unsubscribe ping protocol handler.
30
- *
31
- * @param {Libp2p} node
32
- */
33
- declare function unmount(node: Libp2p): void;
34
- type MuxedStream = import('libp2p-interfaces/src/stream-muxer/types').MuxedStream;
41
+ type Libp2p = import('../');
35
42
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/ping/index.js"],"names":[],"mappings":";AAiBA;;;;;GAKG;AAEH;;;;;;GAMG;AACH,4BAJW,MAAM,QACN,MAAM,GAAC,SAAS,GACd,QAAQ,MAAM,CAAC,CA2B3B;;;;cAtCY,OAAO,KAAK,CAAC;cAEb,OAAO,SAAS,CAAC;iBADjB,OAAO,WAAW,EAAE,SAAS;AAuC1C;;;;GAIG;AACH,6BAFW,MAAM,QAOhB;AAED;;;;GAIG;AACH,+BAFW,MAAM,QAOhB;mBA3DY,OAAO,0CAA0C,EAAE,WAAW"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/ping/index.js"],"names":[],"mappings":";AAiBA;;;;;GAKG;AAEH;IACE;;OAEG;IACH,8BAFW,OAAO,KAAK,CAAC,UAIvB;IAED;;OAEG;IACH,oBAFW,MAAM,EAIhB;IADC,uBAAqB;IAGvB;;;;;OAKG;IACH;QAFgC,MAAM,EAA3B,WAAW;YAIrB;IAED;;;;;OAKG;IACH,WAHW,MAAM,GAAC,SAAS,GACd,QAAQ,MAAM,CAAC,CA2B3B;CACF;;;;mBA5DY,OAAO,0CAA0C,EAAE,WAAW;cAD9D,OAAO,SAAS,CAAC;iBADjB,OAAO,WAAW,EAAE,SAAS;cAD7B,OAAO,KAAK,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libp2p",
3
- "version": "0.36.0",
3
+ "version": "0.36.1",
4
4
  "description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
5
5
  "leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
6
6
  "main": "src/index.js",
package/src/index.js CHANGED
@@ -29,9 +29,9 @@ const Upgrader = require('./upgrader')
29
29
  const PeerStore = require('./peer-store')
30
30
  const PubsubAdapter = require('./pubsub-adapter')
31
31
  const Registrar = require('./registrar')
32
- const ping = require('./ping')
33
32
  const IdentifyService = require('./identify')
34
33
  const FetchService = require('./fetch')
34
+ const PingService = require('./ping')
35
35
  const NatManager = require('./nat-manager')
36
36
  const { updateSelfPeerRecord } = require('./record/utils')
37
37
 
@@ -339,12 +339,10 @@ class Libp2p extends EventEmitter {
339
339
  this.peerRouting = new PeerRouting(this)
340
340
  this.contentRouting = new ContentRouting(this)
341
341
 
342
- // Mount default protocols
343
- ping.mount(this)
344
-
345
342
  this._onDiscoveryPeer = this._onDiscoveryPeer.bind(this)
346
343
 
347
344
  this.fetchService = new FetchService(this)
345
+ this.pingService = new PingService(this)
348
346
  }
349
347
 
350
348
  /**
@@ -382,6 +380,10 @@ class Libp2p extends EventEmitter {
382
380
  await this.handle(FetchService.PROTOCOL, this.fetchService.handleMessage)
383
381
  }
384
382
 
383
+ if (this.pingService) {
384
+ await this.handle(PingService.getProtocolStr(this), this.pingService.handleMessage)
385
+ }
386
+
385
387
  try {
386
388
  await this._onStarting()
387
389
  await this._onDidStart()
@@ -433,9 +435,9 @@ class Libp2p extends EventEmitter {
433
435
  await this.natManager.stop()
434
436
  await this.transportManager.close()
435
437
 
436
- this.unhandle(FetchService.PROTOCOL)
438
+ await this.unhandle(FetchService.PROTOCOL)
439
+ await this.unhandle(PingService.getProtocolStr(this))
437
440
 
438
- ping.unmount(this)
439
441
  this.dialer.destroy()
440
442
  } catch (/** @type {any} */ err) {
441
443
  if (err) {
@@ -609,10 +611,10 @@ class Libp2p extends EventEmitter {
609
611
 
610
612
  // If received multiaddr, ping it
611
613
  if (multiaddrs) {
612
- return ping(this, multiaddrs[0])
614
+ return this.pingService.ping(multiaddrs[0])
613
615
  }
614
616
 
615
- return ping(this, id)
617
+ return this.pingService.ping(id)
616
618
  }
617
619
 
618
620
  /**
package/src/ping/index.js CHANGED
@@ -22,64 +22,63 @@ const { PROTOCOL_NAME, PING_LENGTH, PROTOCOL_VERSION } = require('./constants')
22
22
  * @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
23
23
  */
24
24
 
25
- /**
26
- * Ping a given peer and wait for its response, getting the operation latency.
27
- *
28
- * @param {Libp2p} node
29
- * @param {PeerId|Multiaddr} peer
30
- * @returns {Promise<number>}
31
- */
32
- async function ping (node, peer) {
33
- const protocol = `/${node._config.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
34
- // @ts-ignore multiaddr might not have toB58String
35
- log('dialing %s to %s', protocol, peer.toB58String ? peer.toB58String() : peer)
25
+ class PingService {
26
+ /**
27
+ * @param {import('../')} libp2p
28
+ */
29
+ static getProtocolStr (libp2p) {
30
+ return `/${libp2p._config.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
31
+ }
36
32
 
37
- const connection = await node.dial(peer)
38
- const { stream } = await connection.newStream(protocol)
33
+ /**
34
+ * @param {Libp2p} libp2p
35
+ */
36
+ constructor (libp2p) {
37
+ this._libp2p = libp2p
38
+ }
39
39
 
40
- const start = Date.now()
41
- const data = crypto.randomBytes(PING_LENGTH)
40
+ /**
41
+ * A handler to register with Libp2p to process ping messages
42
+ *
43
+ * @param {Object} options
44
+ * @param {MuxedStream} options.stream
45
+ */
46
+ handleMessage ({ stream }) {
47
+ return pipe(stream, stream)
48
+ }
42
49
 
43
- const [result] = await pipe(
44
- [data],
45
- stream,
46
- (/** @type {MuxedStream} */ stream) => take(1, stream),
47
- toBuffer,
48
- collect
49
- )
50
- const end = Date.now()
50
+ /**
51
+ * Ping a given peer and wait for its response, getting the operation latency.
52
+ *
53
+ * @param {PeerId|Multiaddr} peer
54
+ * @returns {Promise<number>}
55
+ */
56
+ async ping (peer) {
57
+ const protocol = `/${this._libp2p._config.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
58
+ // @ts-ignore multiaddr might not have toB58String
59
+ log('dialing %s to %s', protocol, peer.toB58String ? peer.toB58String() : peer)
51
60
 
52
- if (!equals(data, result)) {
53
- throw errCode(new Error('Received wrong ping ack'), codes.ERR_WRONG_PING_ACK)
54
- }
61
+ const connection = await this._libp2p.dial(peer)
62
+ const { stream } = await connection.newStream(protocol)
55
63
 
56
- return end - start
57
- }
64
+ const start = Date.now()
65
+ const data = crypto.randomBytes(PING_LENGTH)
58
66
 
59
- /**
60
- * Subscribe ping protocol handler.
61
- *
62
- * @param {Libp2p} node
63
- */
64
- function mount (node) {
65
- node.handle(`/${node._config.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`, ({ stream }) => pipe(stream, stream))
66
- .catch(err => {
67
- log.error(err)
68
- })
69
- }
67
+ const [result] = await pipe(
68
+ [data],
69
+ stream,
70
+ (/** @type {MuxedStream} */ stream) => take(1, stream),
71
+ toBuffer,
72
+ collect
73
+ )
74
+ const end = Date.now()
70
75
 
71
- /**
72
- * Unsubscribe ping protocol handler.
73
- *
74
- * @param {Libp2p} node
75
- */
76
- function unmount (node) {
77
- node.unhandle(`/${node._config.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`)
78
- .catch(err => {
79
- log.error(err)
80
- })
76
+ if (!equals(data, result)) {
77
+ throw errCode(new Error('Received wrong ping ack'), codes.ERR_WRONG_PING_ACK)
78
+ }
79
+
80
+ return end - start
81
+ }
81
82
  }
82
83
 
83
- exports = module.exports = ping
84
- exports.mount = mount
85
- exports.unmount = unmount
84
+ module.exports = PingService