<commit_before>'use strict'

// libp2p-nodejs gets replaced by libp2p-browser when webpacked/browserified
const Node = require('../runtime/libp2p-nodejs')
const promisify = require('promisify-es6')
const get = require('lodash/get')
const defaultsDeep = require('@nodeutils/defaults-deep')

module.exports = function libp2p (self) {
  return {
    start: promisify((callback) => {
      self.config.get(gotConfig)

      function gotConfig (err, config) {
        if (err) {
          return callback(err)
        }

        const defaultBundle = (opts) => {
          const libp2pDefaults = {
            peerInfo: opts.peerInfo,
            peerBook: opts.peerBook,
            config: {
              peerDiscovery: {
                mdns: {
                  enabled: get(opts.options, 'config.Discovery.MDNS.Enabled',
                    get(opts.config, 'Discovery.MDNS.Enabled', true))
                },
                webRTCStar: {
                  enabled: get(opts.options, 'config.Discovery.webRTCStar.Enabled',
                    get(opts.config, 'Discovery.webRTCStar.Enabled', true))
                },
                bootstrap: {
                  list: get(opts.options, 'config.Bootstrap',
                    get(opts.config, 'Bootstrap', []))
                }
              },
              relay: {
                enabled: get(opts.options, 'relay.enabled',
                  get(opts.config, 'relay.enabled', false)),
                hop: {
                  enabled: get(opts.options, 'relay.hop.enabled',
                    get(opts.config, 'relay.hop.enabled', false)),
                  active: get(opts.options, 'relay.hop.active',
                    get(opts.config, 'relay.hop.active', false))
                }
              },
              EXPERIMENTAL: {
                dht: get(opts.options, 'EXPERIMENTAL.dht', false),
                pubsub: get(opts.options, 'EXPERIMENTAL.pubsub', false)
              }
            },
            connectionManager: get(opts.options, 'connectionManager',
              get(opts.config, 'connectionManager', {}))
          }

          const libp2pOptions = defaultsDeep(
            get(self._options, 'libp2p', {}),
            libp2pDefaults
          )

          return new Node(libp2pOptions)
        }

        // Always create libp2p via a bundle function
        let libp2pBundle = get(self._options, 'libp2p', null)
        if (typeof libp2pBundle !== 'function') {
          libp2pBundle = defaultBundle
        }

        self._libp2pNode = libp2pBundle({
          options: self._options,
          config: config,
          peerInfo: self._peerInfo,
          peerBook: self._peerInfoBook<commit_msg>perf: faster startup time (#1542)<commit_after>'use strict'

const promisify = require('promisify-es6')
const get = require('lodash/get')
const defaultsDeep = require('@nodeutils/defaults-deep')

module.exports = function libp2p (self) {
  return {
    start: promisify((callback) => {
      self.config.get(gotConfig)

      function gotConfig (err, config) {
        if (err) {
          return callback(err)
        }

        const defaultBundle = (opts) => {
          const libp2pDefaults = {
            peerInfo: opts.peerInfo,
            peerBook: opts.peerBook,
            config: {
              peerDiscovery: {
                mdns: {
                  enabled: get(opts.options, 'config.Discovery.MDNS.Enabled',
                    get(opts.config, 'Discovery.MDNS.Enabled', true))
                },
                webRTCStar: {
                  enabled: get(opts.options, 'config.Discovery.webRTCStar.Enabled',
                    get(opts.config, 'Discovery.webRTCStar.Enabled', true))
                },
                bootstrap: {
                  list: get(opts.options, 'config.Bootstrap',
                    get(opts.config, 'Bootstrap', []))
                }
              },
              relay: {
                enabled: get(opts.options, 'relay.enabled',
                  get(opts.config, 'relay.enabled', false)),
                hop: {
                  enabled: get(opts.options, 'relay.hop.enabled',
                    get(opts.config, 'relay.hop.enabled', false)),
                  active: get(opts.options, 'relay.hop.active',
                    get(opts.config, 'relay.hop.active', false))
                }
              },
              EXPERIMENTAL: {
                dht: get(opts.options, 'EXPERIMENTAL.dht', false),
                pubsub: get(opts.options, 'EXPERIMENTAL.pubsub', false)
              }
            },
            connectionManager: get(opts.options, 'connectionManager',
              get(opts.config, 'connectionManager', {}))
          }

          const libp2pOptions = defaultsDeep(
            get(self._options, 'libp2p', {}),
            libp2pDefaults
          )

          // Required inline to reduce startup time
          // Note: libp2p-nodejs gets replaced by libp2p-browser when webpacked/browserified
          const Node = require('../runtime/libp2p-nodejs')
          return new Node(libp2pOptions)
        }

        // Always create libp2p via a bundle function
        let libp2pBundle = get(self._options, 'libp2p', null)
        if (typeof libp2pBundle !== 'function') {
          libp2pBundle = defaultBundle
        }

        self._libp2pNode = libp2pBundle({
          options: self._options,
          config: config,
          peerInfo: self._peerInfo,
          peerBook: self._peerInfoBook
{'fraction_non_alphanumeric': 0.1150619985412108, 'fraction_numerical': 0.006929248723559446, 'mean_word_length': 1.6156413924654267, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '98368', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1500, 'n_words': 288}