All files / lib/internal blocklist.js

100% Statements 166/166
93.1% Branches 27/29
100% Functions 7/7
100% Lines 166/166

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 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 166 167138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 138x 5960x 5960x 5960x 5960x 138x 138x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 138x 138x 5967x 5965x 5965x 5965x 5965x 5965x 5965x 5965x 5953x 5967x 138x 138x 5972x 5970x 5970x 5970x 5970x 5970x 5970x 5970x 5972x 5956x 5956x 5956x 5956x 5956x 5956x 5956x 5951x 5951x 5951x 5951x 5972x 5972x 138x 138x 16x 15x 15x 15x 15x 15x 15x 15x 13x 16x 7x 7x 16x 6x 6x 16x 7x 16x 138x 138x 190x 186x 186x 186x 186x 186x 186x 186x 186x 4x 4x 4x 186x 184x 190x 138x 138x 2x 2x 138x 138x 1x 1x 1x 1x 1x 1x 138x 138x 1x 1x 1x 138x 138x 138x 138x 1x 1x 1x 1x 1x 138x 138x 138x 138x 138x 138x 138x 138x 138x  
'use strict';
 
const {
  Boolean,
  ObjectSetPrototypeOf,
  Symbol
} = primordials;
 
const {
  BlockList: BlockListHandle,
} = internalBinding('block_list');
 
const {
  customInspectSymbol: kInspect,
} = require('internal/util');
 
const {
  SocketAddress,
  kHandle: kSocketAddressHandle,
} = require('internal/socketaddress');
 
const {
  JSTransferable,
  kClone,
  kDeserialize,
} = require('internal/worker/js_transferable');
 
const { inspect } = require('internal/util/inspect');
 
const kHandle = Symbol('kHandle');
const { owner_symbol } = internalBinding('symbols');
 
const {
  ERR_INVALID_ARG_VALUE,
} = require('internal/errors').codes;
 
const { validateInt32, validateString } = require('internal/validators');
 
class BlockList extends JSTransferable {
  constructor() {
    super();
    this[kHandle] = new BlockListHandle();
    this[kHandle][owner_symbol] = this;
  }
 
  [kInspect](depth, options) {
    if (depth < 0)
      return this;
 
    const opts = {
      ...options,
      depth: options.depth == null ? null : options.depth - 1
    };
 
    return `BlockList ${inspect({
      rules: this.rules
    }, opts)}`;
  }
 
  addAddress(address, family = 'ipv4') {
    if (!SocketAddress.isSocketAddress(address)) {
      validateString(address, 'address');
      validateString(family, 'family');
      address = new SocketAddress({
        address,
        family,
      });
    }
    this[kHandle].addAddress(address[kSocketAddressHandle]);
  }
 
  addRange(start, end, family = 'ipv4') {
    if (!SocketAddress.isSocketAddress(start)) {
      validateString(start, 'start');
      validateString(family, 'family');
      start = new SocketAddress({
        address: start,
        family,
      });
    }
    if (!SocketAddress.isSocketAddress(end)) {
      validateString(end, 'end');
      validateString(family, 'family');
      end = new SocketAddress({
        address: end,
        family,
      });
    }
    const ret = this[kHandle].addRange(
      start[kSocketAddressHandle],
      end[kSocketAddressHandle]);
    if (ret === false)
      throw new ERR_INVALID_ARG_VALUE('start', start, 'must come before end');
  }
 
  addSubnet(network, prefix, family = 'ipv4') {
    if (!SocketAddress.isSocketAddress(network)) {
      validateString(network, 'network');
      validateString(family, 'family');
      network = new SocketAddress({
        address: network,
        family,
      });
    }
    switch (network.family) {
      case 'ipv4':
        validateInt32(prefix, 'prefix', 0, 32);
        break;
      case 'ipv6':
        validateInt32(prefix, 'prefix', 0, 128);
        break;
    }
    this[kHandle].addSubnet(network[kSocketAddressHandle], prefix);
  }
 
  check(address, family = 'ipv4') {
    if (!SocketAddress.isSocketAddress(address)) {
      validateString(address, 'address');
      validateString(family, 'family');
      try {
        address = new SocketAddress({
          address,
          family,
        });
      } catch {
        // Ignore the error. If it's not a valid address, return false.
        return false;
      }
    }
    return Boolean(this[kHandle].check(address[kSocketAddressHandle]));
  }
 
  get rules() {
    return this[kHandle].getRules();
  }
 
  [kClone]() {
    const handle = this[kHandle];
    return {
      data: { handle },
      deserializeInfo: 'internal/blocklist:InternalBlockList',
    };
  }
 
  [kDeserialize]({ handle }) {
    this[kHandle] = handle;
    this[kHandle][owner_symbol] = this;
  }
}
 
class InternalBlockList extends JSTransferable {
  constructor(handle) {
    super();
    this[kHandle] = handle;
    if (handle !== undefined)
      handle[owner_symbol] = this;
  }
}
 
InternalBlockList.prototype.constructor = BlockList.prototype.constructor;
ObjectSetPrototypeOf(InternalBlockList.prototype, BlockList.prototype);
 
module.exports = {
  BlockList,
  InternalBlockList,
};