11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst {
41cb0ef41Sopenharmony_ci  ObjectSetPrototypeOf,
51cb0ef41Sopenharmony_ci  Symbol,
61cb0ef41Sopenharmony_ci} = primordials;
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst {
91cb0ef41Sopenharmony_ci  SocketAddress: _SocketAddress,
101cb0ef41Sopenharmony_ci  AF_INET,
111cb0ef41Sopenharmony_ci  AF_INET6,
121cb0ef41Sopenharmony_ci} = internalBinding('block_list');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst {
151cb0ef41Sopenharmony_ci  validateObject,
161cb0ef41Sopenharmony_ci  validateString,
171cb0ef41Sopenharmony_ci  validatePort,
181cb0ef41Sopenharmony_ci  validateUint32,
191cb0ef41Sopenharmony_ci} = require('internal/validators');
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst {
221cb0ef41Sopenharmony_ci  codes: {
231cb0ef41Sopenharmony_ci    ERR_INVALID_ARG_VALUE,
241cb0ef41Sopenharmony_ci  },
251cb0ef41Sopenharmony_ci} = require('internal/errors');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst {
281cb0ef41Sopenharmony_ci  customInspectSymbol: kInspect,
291cb0ef41Sopenharmony_ci  kEmptyObject,
301cb0ef41Sopenharmony_ci} = require('internal/util');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciconst { inspect } = require('internal/util/inspect');
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciconst {
351cb0ef41Sopenharmony_ci  JSTransferable,
361cb0ef41Sopenharmony_ci  kClone,
371cb0ef41Sopenharmony_ci  kDeserialize,
381cb0ef41Sopenharmony_ci} = require('internal/worker/js_transferable');
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciconst kHandle = Symbol('kHandle');
411cb0ef41Sopenharmony_ciconst kDetail = Symbol('kDetail');
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciclass SocketAddress extends JSTransferable {
441cb0ef41Sopenharmony_ci  static isSocketAddress(value) {
451cb0ef41Sopenharmony_ci    return value?.[kHandle] !== undefined;
461cb0ef41Sopenharmony_ci  }
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  constructor(options = kEmptyObject) {
491cb0ef41Sopenharmony_ci    super();
501cb0ef41Sopenharmony_ci    validateObject(options, 'options');
511cb0ef41Sopenharmony_ci    let { family = 'ipv4' } = options;
521cb0ef41Sopenharmony_ci    const {
531cb0ef41Sopenharmony_ci      address = (family === 'ipv4' ? '127.0.0.1' : '::'),
541cb0ef41Sopenharmony_ci      port = 0,
551cb0ef41Sopenharmony_ci      flowlabel = 0,
561cb0ef41Sopenharmony_ci    } = options;
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    let type;
591cb0ef41Sopenharmony_ci    if (typeof family?.toLowerCase === 'function')
601cb0ef41Sopenharmony_ci      family = family.toLowerCase();
611cb0ef41Sopenharmony_ci    switch (family) {
621cb0ef41Sopenharmony_ci      case 'ipv4':
631cb0ef41Sopenharmony_ci        type = AF_INET;
641cb0ef41Sopenharmony_ci        break;
651cb0ef41Sopenharmony_ci      case 'ipv6':
661cb0ef41Sopenharmony_ci        type = AF_INET6;
671cb0ef41Sopenharmony_ci        break;
681cb0ef41Sopenharmony_ci      default:
691cb0ef41Sopenharmony_ci        throw new ERR_INVALID_ARG_VALUE('options.family', options.family);
701cb0ef41Sopenharmony_ci    }
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci    validateString(address, 'options.address');
731cb0ef41Sopenharmony_ci    validatePort(port, 'options.port');
741cb0ef41Sopenharmony_ci    validateUint32(flowlabel, 'options.flowlabel', false);
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci    this[kHandle] = new _SocketAddress(address, port, type, flowlabel);
771cb0ef41Sopenharmony_ci    this[kDetail] = this[kHandle].detail({
781cb0ef41Sopenharmony_ci      address: undefined,
791cb0ef41Sopenharmony_ci      port: undefined,
801cb0ef41Sopenharmony_ci      family: undefined,
811cb0ef41Sopenharmony_ci      flowlabel: undefined,
821cb0ef41Sopenharmony_ci    });
831cb0ef41Sopenharmony_ci  }
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  get address() {
861cb0ef41Sopenharmony_ci    return this[kDetail].address;
871cb0ef41Sopenharmony_ci  }
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  get port() {
901cb0ef41Sopenharmony_ci    return this[kDetail].port;
911cb0ef41Sopenharmony_ci  }
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  get family() {
941cb0ef41Sopenharmony_ci    return this[kDetail].family === AF_INET ? 'ipv4' : 'ipv6';
951cb0ef41Sopenharmony_ci  }
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  get flowlabel() {
981cb0ef41Sopenharmony_ci    // The flow label can be changed internally.
991cb0ef41Sopenharmony_ci    return this[kHandle].flowlabel();
1001cb0ef41Sopenharmony_ci  }
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  [kInspect](depth, options) {
1031cb0ef41Sopenharmony_ci    if (depth < 0)
1041cb0ef41Sopenharmony_ci      return this;
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci    const opts = {
1071cb0ef41Sopenharmony_ci      ...options,
1081cb0ef41Sopenharmony_ci      depth: options.depth == null ? null : options.depth - 1,
1091cb0ef41Sopenharmony_ci    };
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci    return `SocketAddress ${inspect(this.toJSON(), opts)}`;
1121cb0ef41Sopenharmony_ci  }
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci  [kClone]() {
1151cb0ef41Sopenharmony_ci    const handle = this[kHandle];
1161cb0ef41Sopenharmony_ci    return {
1171cb0ef41Sopenharmony_ci      data: { handle },
1181cb0ef41Sopenharmony_ci      deserializeInfo: 'internal/socketaddress:InternalSocketAddress',
1191cb0ef41Sopenharmony_ci    };
1201cb0ef41Sopenharmony_ci  }
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci  [kDeserialize]({ handle }) {
1231cb0ef41Sopenharmony_ci    this[kHandle] = handle;
1241cb0ef41Sopenharmony_ci    this[kDetail] = handle.detail({
1251cb0ef41Sopenharmony_ci      address: undefined,
1261cb0ef41Sopenharmony_ci      port: undefined,
1271cb0ef41Sopenharmony_ci      family: undefined,
1281cb0ef41Sopenharmony_ci      flowlabel: undefined,
1291cb0ef41Sopenharmony_ci    });
1301cb0ef41Sopenharmony_ci  }
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  toJSON() {
1331cb0ef41Sopenharmony_ci    return {
1341cb0ef41Sopenharmony_ci      address: this.address,
1351cb0ef41Sopenharmony_ci      port: this.port,
1361cb0ef41Sopenharmony_ci      family: this.family,
1371cb0ef41Sopenharmony_ci      flowlabel: this.flowlabel,
1381cb0ef41Sopenharmony_ci    };
1391cb0ef41Sopenharmony_ci  }
1401cb0ef41Sopenharmony_ci}
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ciclass InternalSocketAddress extends JSTransferable {
1431cb0ef41Sopenharmony_ci  constructor(handle) {
1441cb0ef41Sopenharmony_ci    super();
1451cb0ef41Sopenharmony_ci    this[kHandle] = handle;
1461cb0ef41Sopenharmony_ci    this[kDetail] = this[kHandle]?.detail({
1471cb0ef41Sopenharmony_ci      address: undefined,
1481cb0ef41Sopenharmony_ci      port: undefined,
1491cb0ef41Sopenharmony_ci      family: undefined,
1501cb0ef41Sopenharmony_ci      flowlabel: undefined,
1511cb0ef41Sopenharmony_ci    });
1521cb0ef41Sopenharmony_ci  }
1531cb0ef41Sopenharmony_ci}
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ciInternalSocketAddress.prototype.constructor =
1561cb0ef41Sopenharmony_ci  SocketAddress.prototype.constructor;
1571cb0ef41Sopenharmony_ciObjectSetPrototypeOf(InternalSocketAddress.prototype, SocketAddress.prototype);
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_cimodule.exports = {
1601cb0ef41Sopenharmony_ci  SocketAddress,
1611cb0ef41Sopenharmony_ci  InternalSocketAddress,
1621cb0ef41Sopenharmony_ci  kHandle,
1631cb0ef41Sopenharmony_ci};
164