From 11bf99e416c6d3a9798a166d31c7ebe22f6574a0 Mon Sep 17 00:00:00 2001 From: Camilo Diaz Repka Date: Sat, 26 Aug 2017 01:14:57 -0400 Subject: [PATCH 1/5] Sort interfaces by priority when searching an IP address --- lib/ip.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/ip.js b/lib/ip.js index c1799a8..47d4900 100644 --- a/lib/ip.js +++ b/lib/ip.js @@ -376,7 +376,25 @@ ip.address = function(name, family) { return res[0].address; } - var all = Object.keys(interfaces).map(function (nic) { + function priority (name) { + if (name.startsWith('en') || name.startsWith('eth')) { + return 0; + } + if (name.startsWith('wlan')) { + return 1; + } + + return 2; + } + + var sortedInterfaces = Object.keys(interfaces).sort(function(a, b) { + a = priority(a); + b = priority(b); + + return a - b; + }); + + var all = sortedInterfaces.map(function (nic) { // // Note: name will only be `public` or `private` // when this is called. From a91deb012d21a583f84c4ac100cf91688722a9d9 Mon Sep 17 00:00:00 2001 From: Camilo Diaz Repka Date: Tue, 19 Sep 2017 20:12:05 -0300 Subject: [PATCH 2/5] Style fixes --- lib/ip.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/ip.js b/lib/ip.js index 47d4900..6321437 100644 --- a/lib/ip.js +++ b/lib/ip.js @@ -376,7 +376,7 @@ ip.address = function(name, family) { return res[0].address; } - function priority (name) { + function priority(name) { if (name.startsWith('en') || name.startsWith('eth')) { return 0; } @@ -388,13 +388,10 @@ ip.address = function(name, family) { } var sortedInterfaces = Object.keys(interfaces).sort(function(a, b) { - a = priority(a); - b = priority(b); - - return a - b; + return priority(a) - priority(b); }); - var all = sortedInterfaces.map(function (nic) { + var all = sortedInterfaces.map(function(nic) { // // Note: name will only be `public` or `private` // when this is called. From a24f36060062995cfb8d5638fb8b0b0b079ca3ca Mon Sep 17 00:00:00 2001 From: Camilo Diaz Repka Date: Tue, 19 Sep 2017 20:29:45 -0300 Subject: [PATCH 3/5] Avoid using possibly undefined `String.startsWith()` --- lib/ip.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ip.js b/lib/ip.js index 6321437..4ea2937 100644 --- a/lib/ip.js +++ b/lib/ip.js @@ -377,10 +377,10 @@ ip.address = function(name, family) { } function priority(name) { - if (name.startsWith('en') || name.startsWith('eth')) { + if (name.substring(0, 2) == 'en' || name.substring(0, 3) == 'eth') { return 0; } - if (name.startsWith('wlan')) { + if (name.substring(0, 4) == 'wlan') { return 1; } From 70d587d0454b97584da95babaf6e135f7161fd6f Mon Sep 17 00:00:00 2001 From: Camilo Diaz Repka Date: Tue, 19 Sep 2017 20:31:16 -0300 Subject: [PATCH 4/5] Please the linter --- lib/ip.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ip.js b/lib/ip.js index 4ea2937..58a022b 100644 --- a/lib/ip.js +++ b/lib/ip.js @@ -377,10 +377,10 @@ ip.address = function(name, family) { } function priority(name) { - if (name.substring(0, 2) == 'en' || name.substring(0, 3) == 'eth') { + if (name.substring(0, 2) === 'en' || name.substring(0, 3) === 'eth') { return 0; } - if (name.substring(0, 4) == 'wlan') { + if (name.substring(0, 4) === 'wlan') { return 1; } From 3a090d0734ad766da5f9b1ea5cf05b18c6c208d7 Mon Sep 17 00:00:00 2001 From: Camilo Diaz Repka Date: Fri, 13 Oct 2017 22:36:04 -0300 Subject: [PATCH 5/5] Use `slice` instead of `substring` --- lib/ip.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ip.js b/lib/ip.js index 58a022b..be661eb 100644 --- a/lib/ip.js +++ b/lib/ip.js @@ -377,10 +377,10 @@ ip.address = function(name, family) { } function priority(name) { - if (name.substring(0, 2) === 'en' || name.substring(0, 3) === 'eth') { + if (name.slice(0, 2) === 'en' || name.slice(0, 3) === 'eth') { return 0; } - if (name.substring(0, 4) === 'wlan') { + if (name.slice(0, 4) === 'wlan') { return 1; }