Skip to content

Commit

Permalink
fix: Handle EADDRNOTAVAIL if IPv6 not enabled (#93)
Browse files Browse the repository at this point in the history
The logic will now disable IPv6 lookups when this error happens after we resolved a hostname to IPv6. If someone provides an IPv6 address as host themselves we would fail as before.
  • Loading branch information
danez committed Oct 12, 2022
1 parent 882b023 commit 4b2d30b
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/wait-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const outputFunctions = require('./output-functions');
const validateParameters = require('./validate-parameters');
const ConnectionError = require('./errors/connection-error');

let IPv6enabled = true;

function createConnectionWithTimeout({ host, port, ipVersion }, timeout, callback) {
// Variable to hold the timer we'll use to kill the socket if we don't
// connect in time.
Expand Down Expand Up @@ -114,6 +116,15 @@ function tryConnect(options, timeout) {
// ...otherwise, we will explicitly fail with a meaningful error for
// the user.
return reject(new ConnectionError(`The address '${options.host}' cannot be found`));
} else if (err.code === 'EADDRNOTAVAIL' && options.ipVersion === 6) {
// This will occur if the IP address we are trying to connect to does not exist
// This can happen for ::1 or other IPv6 addresses if the IPv6 stack is not enabled.
// In this case we disable the IPv6 lookup
debug('Socket cannot be opened for IPv6: EADDRNOTAVAIL');
debug('Disabling IPv6 lookup');
IPv6enabled = false;

return resolve(false);
}

// Trying to open the socket has resulted in an error we don't
Expand Down Expand Up @@ -208,7 +219,7 @@ function waitPort(params) {
}

// Check for IPv6 next
if (ipVersion === 4 && !net.isIP(host)) {
if (IPv6enabled && ipVersion === 4 && !net.isIP(host)) {
return loop(6);
}

Expand Down

0 comments on commit 4b2d30b

Please sign in to comment.