# All files / src/utils readConfig.js

100% Statements 28/28
100% Branches 10/10
100% Functions 3/3
100% Lines 28/28

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56

    1x
    1x
    1x
    1x
    1x
    1x

    1x

    1x

    1x
    3x
    3x
    3x
    3x
    1x
    1x
    1x
    1x
    1x

    3x
    3x

    1x
    1x

    1x

    1x
    4x

    4x
    1x

    3x

     /**
     * Copyright (c) 2018-present, Leap DAO (leapdao.org)
     *
     * This source code is licensed under the Mozilla Public License Version 2.0
     * found in the LICENSE file in the root directory of this source tree.
     */

    /* eslint-disable no-console */

    const fs = require('fs');
    const url = require('url');
    const { helpers } = require('leap-core');
    const Web3 = require('web3');
    const { promisify } = require('util');
    const { logNode } = require('./debug');

    const readFile = promisify(fs.readFile);

    const defaultConfig = {
      eventsDelay: 0,
      bridgeDelay: 0,
    };

    const fetchNodeConfig = async nodeUrl => {
      logNode(`Fetching config from: ${nodeUrl}`);
      const web3 = helpers.extendWeb3(new Web3(nodeUrl));
      const config = await web3.getConfig();
      if (config.p2pPort && config.nodeId) {
        const { hostname } = url.parse(nodeUrl);
        config.peers = config.peers || [];
        config.peers.push(`${config.nodeId}@${hostname}:${config.p2pPort}`);
        delete config.p2pPort;
        delete config.nodeId;
      }
      logNode(`Fetched config from: ${nodeUrl}`, config);
      return config;
    };

    const readConfigFile = async configPath => {
      return JSON.parse(await readFile(configPath));
    };

    const urlRegex = /^https{0,1}:\/\//;

    module.exports = async configPath => {
      const config = urlRegex.test(configPath)
        ? await fetchNodeConfig(configPath)
        : await readConfigFile(configPath);

      if (!config.exitHandlerAddr) {
        throw new Error('exitHandlerAddr is required');
      }

      return Object.assign({}, defaultConfig, config);
    };