chore: move create to static function

This commit is contained in:
Vasco Santos 2020-12-10 13:40:19 +01:00
parent aa98bc2f0e
commit f568f16a31

View File

@ -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