Skip to content

Commit

Permalink
fix: Handle systems without IPv6 properly (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
danez authored Oct 18, 2022
1 parent 53ff7fa commit 7cc0fac
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/wait-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ function tryConnect(options, timeout) {
debug('Socket not open: ECONNRESET');
socket.destroy();
return resolve(false);
} else if (options.ipVersion === 6 && (err.code === 'EADDRNOTAVAIL' || err.code === 'ENOTFOUND')) {
// 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: ${err.code}`);
debug('Disabling IPv6 lookup');
IPv6enabled = false;
socket.destroy();
return resolve(false);
} else if (err.code === 'ENOTFOUND') {
// This will occur if the address is not found, i.e. due to a dns
// lookup fail (normally a problem if the domain is wrong).
Expand All @@ -116,21 +125,19 @@ 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
// understand. Better give up.
debug(`Unexpected error trying to open socket: ${err}`);
socket.destroy();

// If we are currently checking for IPv6 we ignore this error and disable IPv6
if (options.ipVersion === 6) {
IPv6enabled = false;
return resolve(false);
}

return reject(err);
}

Expand Down

0 comments on commit 7cc0fac

Please sign in to comment.