all files / src/ rover_toml_resolver.js

100% Statements 51/51
100% Branches 24/24
100% Functions 10/10
100% Lines 16/16
4 statements, 2 functions, 6 branches Ignored     
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 57 58 59                                                                                       
import axios from 'axios';
import Promise from 'bluebird';
import toml from 'toml';
import {Config} from "./config";
 
// STELLAR_TOML_MAX_SIZE is the maximum size of rover.toml file
export const STELLAR_TOML_MAX_SIZE = 100 * 1024;
 
/**
 * RoverTomlResolver allows resolving `rover.toml` files.
 */
export class RoverTomlResolver {
  /**
   * Returns a parsed `rover.toml` file for a given domain.
   * Returns a `Promise` that resolves to the parsed rover.toml object. If `rover.toml` file does not exist for a given domain or is invalid Promise will reject.
   * ```js
   * RoverSdk.RoverTomlResolver.resolve('acme.com')
   *   .then(roverToml => {
   *     // roverToml in an object representing domain rover.toml file.
   *   })
   *   .catch(error => {
   *     // rover.toml does not exist or is invalid
   *   });
   * ```
   * @see <a href="https://www.stellar.org/developers/learn/concepts/stellar-toml.html" target="_blank">Rover .toml doc</a>
   * @param {string} domain Domain to get rover.toml file for
   * @param {object} [opts]
   * @param {boolean} [opts.allowHttp] - Allow connecting to http servers, default: `false`. This must be set to false in production deployments!
   * @returns {Promise}
   */
  static resolve(domain, opts = {}) {
    let allowHttp = Config.isAllowHttp();
    if (typeof opts.allowHttp !== 'undefined') {
        allowHttp = opts.allowHttp;
    }
 
    let protocol = 'https';
    if (allowHttp) {
        protocol = 'http';
    }
    return axios.get(`${protocol}://${domain}/.well-known/rover.toml`, {maxContentLength: STELLAR_TOML_MAX_SIZE})
      .then(response => {
      	try {
            let tomlObject = toml.parse(response.data);
            return Promise.resolve(tomlObject);
        } catch (e) {
            return Promise.reject(new Error(`Parsing error on line ${e.line}, column ${e.column}: ${e.message}`));
        }
      })
      .catch(err => {
        if (err.message.match(/^maxContentLength size/)) {
          throw new Error(`rover.toml file exceeds allowed size of ${STELLAR_TOML_MAX_SIZE}`);
        } else {
          throw err;
        }
      });
  }
}