Skip to content

Commit

Permalink
feat: address.ip() supports node 18
Browse files Browse the repository at this point in the history
- typeof os.networkInterfaces family is a number (v18.0.0)

@see nodejs/node#42861
  • Loading branch information
zhangyuheng committed Apr 29, 2022
1 parent 05b5f07 commit 1c54255
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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 === actualFamily;
}

/**
* 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

0 comments on commit 1c54255

Please sign in to comment.