Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: address.ip() supports node 18 #26

Merged
merged 1 commit into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
sudo: false
language: node_js
node_js:
- '18'
- '16'
- '14'
- '12'
- '10'
- '8'
Expand Down
17 changes: 15 additions & 2 deletions lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ function getIfconfigCMD() {
return '/sbin/ifconfig';
}

// typeof os.networkInterfaces family is a number (v18.0.0)
// types: 'IPv4' | 'IPv6' => 4 | 6
// @see https://github.com/nodejs/node/issues/42861
function matchName(actualFamily, expectedFamily) {
if (expectedFamily === 'IPv4') {
return actualFamily === 'IPv4' || actualFamily === 4;
}
if (expectedFamily === 'IPv6') {
return actualFamily === 'IPv6' || actualFamily === 6;
}
return actualFamily === expectedFamily;
}

/**
* Get all addresses.
*
Expand Down Expand Up @@ -65,7 +78,7 @@ address.interface = function (family, name) {
if (items) {
for (var j = 0; j < items.length; j++) {
var item = items[j];
if (item.family === family) {
if (matchName(item.family, family)) {
return item;
}
}
Expand All @@ -78,7 +91,7 @@ address.interface = function (family, name) {
var items = interfaces[k];
for (var i = 0; i < items.length; i++) {
var item = items[i];
if (item.family === family && item.address !== '127.0.0.1') {
if (matchName(item.family, family) && item.address !== '127.0.0.1') {
return item;
}
}
Expand Down