// Patch DNS.lookup to resolve all hosts added via Replay.localhost as 127.0.0.1

const DNS    = require('dns');
const Replay = require('./');


const originalLookup = DNS.lookup;
DNS.lookup = function(domain, options, callback) {
  if (typeof domain === 'string' && typeof options === 'object' &&
      typeof callback === 'function' && Replay.isLocalhost(domain)) {
    const family = options.family || 4;
    const ip = (family === 6) ? '::1' : '127.0.0.1';
    callback(null, ip, family);
  } else
    originalLookup(domain, options, callback);
};


// Patch DNS.lookup to resolve all hosts added via Replay.localhost as 127.0.0.1

const DNS    = require('dns');
const Replay = require('./');


const originalLookup = DNS.lookup;
DNS.lookup = function(domain, options, callback) {
  if (typeof domain === 'string' && typeof options === 'object' &&
      typeof callback === 'function' && Replay.isLocalhost(domain)) {
    const family = options.family || 4;
    const ip = (family === 6) ? '::1' : '127.0.0.1';
    if (options.all)
      callback(null, [ip], family);
    else
      callback(null, ip, family);
  } else
    originalLookup(domain, options, callback);
};


Support |all| option when shimming DNS.lookup
Support |all| option when shimming DNS.lookup

Before, clients attempting an "all" style address lookup,
such as the SQL Server database module tedious does in its connector.js
when provided a hostname,
were liable to error out due to treating the argument as an array
when Replay was supplying a string. This might have looked like:

    TypeError: this.addresses.shift is not a function

Now, when the |all| option is detected, the string is wrapped in an
array to match client expectations.
