mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 18:42:15 +00:00
chore: move create to static function
This commit is contained in:
parent
aa98bc2f0e
commit
f568f16a31
44
src/index.js
44
src/index.js
@ -80,11 +80,32 @@ const IDENTIFY_PROTOCOLS = IdentifyService.multicodecs
|
|||||||
* @property {Libp2pConfig} [config]
|
* @property {Libp2pConfig} [config]
|
||||||
* @property {PeerId} peerId
|
* @property {PeerId} peerId
|
||||||
*
|
*
|
||||||
|
* @typedef {Object} CreateOptions
|
||||||
|
* @property {PeerId} peerId
|
||||||
|
*
|
||||||
* @extends {EventEmitter}
|
* @extends {EventEmitter}
|
||||||
* @fires Libp2p#error Emitted when an error occurs
|
* @fires Libp2p#error Emitted when an error occurs
|
||||||
* @fires Libp2p#peer:discovery Emitted when a peer is discovered
|
* @fires Libp2p#peer:discovery Emitted when a peer is discovered
|
||||||
*/
|
*/
|
||||||
class Libp2p extends EventEmitter {
|
class Libp2p extends EventEmitter {
|
||||||
|
/**
|
||||||
|
* Like `new Libp2p(options)` except it will create a `PeerId`
|
||||||
|
* instance if one is not provided in options.
|
||||||
|
*
|
||||||
|
* @param {Libp2pOptions & CreateOptions} options - Libp2p configuration options
|
||||||
|
* @returns {Promise<Libp2p>}
|
||||||
|
*/
|
||||||
|
static async create (options) {
|
||||||
|
if (options.peerId) {
|
||||||
|
return new Libp2p(options)
|
||||||
|
}
|
||||||
|
|
||||||
|
const peerId = await PeerId.create()
|
||||||
|
|
||||||
|
options.peerId = peerId
|
||||||
|
return new Libp2p(options)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Libp2p node.
|
* Libp2p node.
|
||||||
*
|
*
|
||||||
@ -656,27 +677,4 @@ class Libp2p extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {Object} CreateOptions
|
|
||||||
* @property {PeerId} peerId
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Like `new Libp2p(options)` except it will create a `PeerId`
|
|
||||||
* instance if one is not provided in options.
|
|
||||||
*
|
|
||||||
* @param {Libp2pOptions & CreateOptions} options - Libp2p configuration options
|
|
||||||
* @returns {Promise<Libp2p>}
|
|
||||||
*/
|
|
||||||
Libp2p.create = async function create (options) {
|
|
||||||
if (options.peerId) {
|
|
||||||
return new Libp2p(options)
|
|
||||||
}
|
|
||||||
|
|
||||||
const peerId = await PeerId.create()
|
|
||||||
|
|
||||||
options.peerId = peerId
|
|
||||||
return new Libp2p(options)
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = Libp2p
|
module.exports = Libp2p
|
||||||
|
Loading…
x
Reference in New Issue
Block a user