Code coverage report for mongodb-download-url/index.js

Statements: 82.22% (74 / 90)      Branches: 68.52% (37 / 54)      Functions: 93.33% (14 / 15)      Lines: 88.1% (74 / 84)      Ignored: none     

All files » mongodb-download-url/ » index.js
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 1661 1 1 1 1 1 1 1 1   1 1     1   1 1       1   1 1 1     1 1 35       1 1                 1 1 1 1   1 1 129 1 1     1           1 2 2 2       1 1 1   1 129   129   38         1 5                   5         5   5                 5 5   5     5       5   5 5   5 3   2   5 1       5   5   1     4 2 2 1   1     4 4   4   4             4      
var os = require('os');
var PLATFORM = os.platform() === 'darwin' ? 'osx' : os.platform();
var ARCH = os.arch() === 'x64' ? 'x86_64' : os.arch();
var versions = require('mongodb-version-list');
var semver = require('semver');
var request = require('request');
var async = require('async');
var defaults = require('lodash.defaults');
var debug = require('debug')('mongodb-download-url');
 
function mci(opts, fn) {
  Iif (!opts.distro) {
    return fn(new Error('Missing required parameter `distro`'));
  }
  var url = 'http://mci-motu.10gen.cc:9090/rest/v1/projects/mongodb-mongo-'
    + opts.branch + '/revisions/' + opts.version;
  debug('resolving version via MCI `%s`', url);
  request.get({
    url: url,
    json: true
  }, function(err, res, body) {
    Iif (err) return fn(err);
 
    Iif (res.statusCode === 404) return fn(new Error(body.message));
    debug('got body', body);
    var dl = 'https://s3.amazonaws.com/mciuploads/mongodb-mongo-' + opts.branch
      + '/' + opts.distro + '/' + opts.version + '/binaries';
 
    var s = 'mongodb_mongo_' + opts.branch + '_' + opts.distro;
    var basename = body.builds.filter(function(b) {
      return b.indexOf(s) > -1;
    })[0];
 
    // @todo: test across all distros as I believe this may be different for some.
    basename = 'binaries-' + basename;
    fn(null, {
      name: 'mongodb',
      version: opts.version,
      artifact: basename + opts.ext,
      url: dl + '/' + basename + opts.ext
    });
  });
}
 
function search(query, fn) {
  debug('searching for version', query);
  versions(function(err, res) {
    Iif (err) return fn(err);
 
    var found = false;
    for (var i = 0; i < res.length; i++) {
      if (!found && semver.satisfies(res[i], query.version)) {
        found = true;
        fn(null, res[i]);
      }
    }
    Iif (!found) {
      fn(new Error('No matches'));
    }
  });
}
 
function latest(fn) {
  versions(function(err, res) {
    Iif (err) return fn(err);
    fn(null, res[0]);
  });
}
 
function stable(fn) {
  versions(function(err, res) {
    Iif (err) return fn(err);
 
    fn(null, res.map(function(v) {
      return semver.parse(v);
    }).filter(function(v) {
      return v.prerelease.length === 0 && v.minor % 2 === 0;
    }).map(function(v) {
      return v.version;
    })[0]);
  });
}
 
module.exports = function(opts, fn) {
  Iif (Array.isArray(opts)) {
    var tasks = {};
 
    opts.map(function(opt) {
      tasks[opt.version] = module.exports.bind(null, opt);
    });
 
    return async.parallel(tasks, fn);
  }
 
  Iif (typeof opts === 'string') {
    opts = {
      version: opts
    };
  }
  var handler = versions;
 
  defaults(opts, {
    arch: ARCH,
    platform: PLATFORM,
    branch: 'master',
    bits: '64',
    debug: false
  });
 
  // 64bit -> 64
  opts.bits = '' + opts.bits;
  opts.bits = opts.bits.replace(/[^0-9]/g, '');
 
  Iif (opts.platform === 'darwin') {
    opts.platform = 'osx';
  }
  Iif (opts.platform === 'windows') {
    opts.platform = 'win32';
  }
 
  opts.ext = opts.platform === 'win32' ? '.zip' : '.tgz';
 
  Eif (!opts.distro) {
    Iif (opts.platform === 'linux') {
      opts.distro = 'linux_' + opts.bits;
    } else if (opts.platform === 'osx') {
      opts.distro = 'osx_108';
    } else {
      opts.distro = 'windows_64_2k8';
    }
    if (opts.debug) {
      opts.distro += '_debug';
    }
  }
 
  debug('assembled options', opts);
 
  if (opts.version.length === 40) {
    // A commit hash
    return mci(opts, fn);
  }
 
  if (opts.version === 'latest' || opts.version === 'unstable') {
    handler = latest;
  } else if (opts.version === 'stable') {
    handler = stable;
  } else {
    handler = search.bind(null, opts);
  }
 
  handler(function(err, v) {
    Iif (err) return fn(err);
 
    var basename = 'mongodb-' + opts.platform + '-' + opts.arch
      + (opts.debug ? '-debugsymbols' : '') + '-' + v;
    var pkg = {
      name: 'mongodb',
      version: v,
      artifact: basename + opts.ext,
      url: 'http://fastdl.mongodb.org/' + opts.platform + '/' + basename + opts.ext
    };
 
    fn(null, pkg);
  });
};