src/Advertisement.js
function Advertisement(type, port, options = {}) {
  if (!(this instanceof Advertisement)) {
    return new Advertisement(type, port, options);
  }

  EventEmitter.call(this);

  // convert argument ServiceType to validate it (might throw)
  const serviceType = (!(type instanceof ServiceType))
    ? new ServiceType(type)
    : type;

  // validate other inputs (throws on invalid)
  validate.port(port);

  if (options.txt)  validate.txt(options.txt);
  if (options.name) validate.label(options.name, 'Instance');
  if (options.host) validate.label(options.host, 'Hostname');

  this.serviceName  = serviceType.name;
  this.protocol     = serviceType.protocol;
  this.subtypes     = (options.subtypes) ? options.subtypes : serviceType.subtypes;
  this.port         = port;
  this.instanceName = options.name || misc.hostname();
  this.hostname     = options.host || misc.hostname();
  this.txt          = options.txt  || {};

  // Domain notes:
  // 1- link-local only, so this is the only possible value
  // 2- "_domain" used instead of "domain" because "domain" is an instance var
  //    in older versions of EventEmitter. Using "domain" messes up `this.emit()`
  this._domain = 'local';

  this._id = misc.fqdn(this.instanceName, this.serviceName, this.protocol, 'local');
  debug(`Creating new advertisement for "${this._id}" on ${port}`);

  this.state              = STATE.STOPPED;
  this._interface         = NetworkInterface.get(options.interface);
  this._defaultAddresses  = null;
  this._hostnameResponder = null;
  this._serviceResponder  = null;
}
function Advertisement(type, port, options = {}) {
  if (!(this instanceof Advertisement)) {
    return new Advertisement(type, port, options);
  }

  EventEmitter.call(this);

  // convert argument ServiceType to validate it (might throw)
  const serviceType = (!(type instanceof ServiceType))
    ? new ServiceType(type)
    : type;

  // validate other inputs (throws on invalid)
  validate.port(port);

  if (options.txt)  validate.txt(options.txt);
  if (options.name) validate.label(options.name, 'Instance');
  if (options.host) validate.label(options.host, 'Hostname');

  this.serviceName  = serviceType.name;
  this.protocol     = serviceType.protocol;
  this.subtypes     = (options.subtypes) ? options.subtypes : serviceType.subtypes;
  this.port         = port;
  this.instanceName = options.name || misc.hostname();
  this.hostname     = options.host || misc.hostname();
  this.txt          = options.txt  || {};

  // Domain notes:
  // 1- link-local only, so this is the only possible value
  // 2- "_domain" used instead of "domain" because "domain" is an instance var
  //    in older versions of EventEmitter. Using "domain" messes up `this.emit()`
  this._domain = 'local';

  this._id = misc.fqdn(this.instanceName, this.serviceName, this.protocol, 'local');
  debug(`Creating new advertisement for "${this._id}" on ${port}`);

  this.state              = STATE.STOPPED;
  this._interface         = NetworkInterface.get(options.interface);
  this._defaultAddresses  = null;
  this._hostnameResponder = null;
  this._serviceResponder  = null;
}
function Advertisement ( type , port , options = { } ) { if ( ! ( this instanceof Advertisement ) ) { return new Advertisement ( type , port , options ) ; } EventEmitter . call ( this ) ; // convert argument ServiceType to validate it (might throw) const serviceType = ( ! ( type instanceof ServiceType ) ) ? new ServiceType ( type ) : type ; // validate other inputs (throws on invalid) validate . port ( port ) ; if ( options . txt ) validate . txt ( options . txt ) ; if ( options . name ) validate . label ( options . name , 'Instance' ) ; if ( options . host ) validate . label ( options . host , 'Hostname' ) ; this . serviceName = serviceType . name ; this . protocol = serviceType . protocol ; this . subtypes = ( options . subtypes ) ? options . subtypes : serviceType . subtypes ; this . port = port ; this . instanceName = options . name || misc . hostname ( ) ; this . hostname = options . host || misc . hostname ( ) ; this . txt = options . txt || { } ; // Domain notes: // 1- link-local only, so this is the only possible value // 2- "_domain" used instead of "domain" because "domain" is an instance var //    in older versions of EventEmitter. Using "domain" messes up `this.emit()` this . _domain = 'local' ; this . _id = misc . fqdn ( this . instanceName , this . serviceName , this . protocol , 'local' ) ; debug ( ` ${ this . _id } ${ port } ` ) ; this . state = STATE . STOPPED ; this . _interface = NetworkInterface . get ( options . interface ) ; this . _defaultAddresses = null ; this . _hostnameResponder = null ; this . _serviceResponder = null ; }
Creates a new Advertisement

@emits 'error'
@emits 'stopped' when the advertisement is stopped
@emits 'instanceRenamed' when the service instance is renamed
@emits 'hostRenamed' when the hostname has to be renamed

@param {ServiceType|Object|String|Array} type - type of service to advertise
@param {Number}                          port - port to advertise

@param {Object}   [options]
@param {Object}   options.name       - instance name
@param {Object}   options.host       - hostname to use
@param {Object}   options.txt        - TXT record
@param {Object}   options.subtypes   - subtypes to register
@param {Object}   options.interface  - interface name or address to use
Creates a new Advertisement