All files / lib/internal socketaddress.js

100% Statements 156/156
91.66% Branches 22/24
100% Functions 8/8
100% Lines 156/156

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 157137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 16790x 16790x 137x 137x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 11152x 11152x 16773x 5610x 5610x 16773x 8x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 16773x 137x 137x 8x 8x 137x 137x 8x 8x 137x 137x 21x 21x 137x 137x 8x 8x 8x 137x 137x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 137x 137x 1x 1x 1x 1x 1x 1x 137x 137x 1x 1x 1x 1x 1x 1x 1x 1x 137x 137x 1x 1x 1x 1x 1x 1x 1x 137x 137x 137x 137x 1x 1x 1x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x  
'use strict';
 
const {
  ObjectSetPrototypeOf,
  Symbol,
} = primordials;
 
const {
  SocketAddress: _SocketAddress,
  AF_INET,
  AF_INET6,
} = internalBinding('block_list');
 
const {
  validateObject,
  validateString,
  validatePort,
  validateUint32,
} = require('internal/validators');
 
const {
  codes: {
    ERR_INVALID_ARG_VALUE,
  },
} = require('internal/errors');
 
const {
  customInspectSymbol: kInspect,
} = require('internal/util');
 
const { inspect } = require('internal/util/inspect');
 
const {
  JSTransferable,
  kClone,
  kDeserialize,
} = require('internal/worker/js_transferable');
 
const kHandle = Symbol('kHandle');
const kDetail = Symbol('kDetail');
 
class SocketAddress extends JSTransferable {
  static isSocketAddress(value) {
    return value?.[kHandle] !== undefined;
  }
 
  constructor(options = {}) {
    super();
    validateObject(options, 'options');
    let { family = 'ipv4' } = options;
    const {
      address = (family === 'ipv4' ? '127.0.0.1' : '::'),
      port = 0,
      flowlabel = 0,
    } = options;
 
    let type;
    if (typeof family?.toLowerCase === 'function')
      family = family.toLowerCase();
    switch (family) {
      case 'ipv4':
        type = AF_INET;
        break;
      case 'ipv6':
        type = AF_INET6;
        break;
      default:
        throw new ERR_INVALID_ARG_VALUE('options.family', options.family);
    }
 
    validateString(address, 'options.address');
    validatePort(port, 'options.port');
    validateUint32(flowlabel, 'options.flowlabel', false);
 
    this[kHandle] = new _SocketAddress(address, port, type, flowlabel);
    this[kDetail] = this[kHandle].detail({
      address: undefined,
      port: undefined,
      family: undefined,
      flowlabel: undefined,
    });
  }
 
  get address() {
    return this[kDetail].address;
  }
 
  get port() {
    return this[kDetail].port;
  }
 
  get family() {
    return this[kDetail].family === AF_INET ? 'ipv4' : 'ipv6';
  }
 
  get flowlabel() {
    // The flow label can be changed internally.
    return this[kHandle].flowlabel();
  }
 
  [kInspect](depth, options) {
    if (depth < 0)
      return this;
 
    const opts = {
      ...options,
      depth: options.depth == null ? null : options.depth - 1
    };
 
    return `SocketAddress ${inspect(this.toJSON(), opts)}`;
  }
 
  [kClone]() {
    const handle = this[kHandle];
    return {
      data: { handle },
      deserializeInfo: 'internal/socketaddress:InternalSocketAddress',
    };
  }
 
  [kDeserialize]({ handle }) {
    this[kHandle] = handle;
    this[kDetail] = handle.detail({
      address: undefined,
      port: undefined,
      family: undefined,
      flowlabel: undefined,
    });
  }
 
  toJSON() {
    return {
      address: this.address,
      port: this.port,
      family: this.family,
      flowlabel: this.flowlabel,
    };
  }
}
 
class InternalSocketAddress extends JSTransferable {
  constructor(handle) {
    super();
    this[kHandle] = handle;
  }
}
 
InternalSocketAddress.prototype.constructor =
  SocketAddress.prototype.constructor;
ObjectSetPrototypeOf(InternalSocketAddress.prototype, SocketAddress.prototype);
 
module.exports = {
  SocketAddress,
  InternalSocketAddress,
  kHandle,
};