All files index.js

96.55% Statements 28/29
92.5% Branches 37/40
100% Functions 9/9
96.55% Lines 28/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1051x     19x       22x               8x                           3x             5x   5x 1x     4x                                     1x 14x         14x           14x 2x 4x 4x   2x   2x     12x         24x 48x 48x 36x 12x 4x     8x     24x     12x    
const os = require('os');
 
function _normalizeFamily(family) {
  return family ? family.toLowerCase() : 'ipv4';
}
 
function isLoopback(addr) {
  return /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/
    .test(addr) ||
    /^fe80::1$/.test(addr) ||
    /^::1$/.test(addr) ||
    /^::$/.test(addr);
};
 
function isPrivate(addr) {
  return /^(::f{4}:)?10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i
    .test(addr) ||
    /^(::f{4}:)?192\.168\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) ||
    /^(::f{4}:)?172\.(1[6-9]|2\d|30|31)\.([0-9]{1,3})\.([0-9]{1,3})$/i
      .test(addr) ||
    /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) ||
    /^(::f{4}:)?169\.254\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) ||
    /^f[cd][0-9a-f]{2}:/i.test(addr) ||
    /^fe80:/i.test(addr) ||
    /^::1$/.test(addr) ||
    /^::$/.test(addr);
};
 
function isPublic(addr) {
  return !isPrivate(addr);
};
 
//
// Default to `ipv4`
//
function loopback(family) {
  family = _normalizeFamily(family);
 
  if (family !== 'ipv4' && family !== 'ipv6') {
    throw new Error('family must be ipv4 or ipv6');
  }
 
  return family === 'ipv4' ? '127.0.0.1' : 'fe80::1';
};
 
 
//
// ### function address (name, family)
// #### @name {string|'public'|'private'} **Optional** Name or security
//      of the network interface.
// #### @family {ipv4|ipv6} **Optional** IP family of the address (defaults
//      to ipv4).
//
// Returns the address for the network interface on the current system with
// the specified `name`:
//   * String: First `family` address of the interface.
//             If not found see `undefined`.
//   * 'public': the first public ip address of family.
//   * 'private': the first private ip address of family.
//   * undefined: First address with `ipv4` or loopback address `127.0.0.1`.
//
module.exports = function localIpUrl(name, family) {
  var interfaces = os.networkInterfaces();
  var all;
  //
  // Default to `ipv4`
  //
  family = _normalizeFamily(family);
 
  //
  // If a specific network interface has been named,
  // return the address.
  //
  if (name && name !== 'private' && name !== 'public') {
    var res = interfaces[name].filter(function (details) {
      var itemFamily = details.family.toLowerCase();
      return itemFamily === family;
    });
    Iif (res.length === 0)
      return undefined;
    return res[0].address;
  }
 
  var all = Object.keys(interfaces).map(function (nic) {
    //
    // Note: name will only be `public` or `private`
    // when this is called.
    //
    var addresses = interfaces[nic].filter(function (details) {
      details.family = details.family.toLowerCase();
      if (details.family !== family || isLoopback(details.address)) {
        return false;
      } else if (!name) {
        return true;
      }
 
      return name === 'public' ? isPrivate(details.address) : isPublic(details.address);
    });
 
    return addresses.length ? addresses[0].address : undefined;
  }).filter(Boolean);
 
  return !all.length ? loopback(family) : all[0];
}