-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Make host IP extractor Node 18 compatible
- Loading branch information
Showing
1 changed file
with
20 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,31 @@ | ||
import { networkInterfaces } from 'os' | ||
|
||
/** | ||
* Check if network interface address is usable. | ||
* @param {import('os').NetworkInterfaceInfo} address | ||
* @returns {boolean} | ||
*/ | ||
function isUsableAddress (address) { | ||
if (address.internal) { | ||
return false | ||
} | ||
|
||
if (address.family === 'IPv4') { | ||
return true | ||
} | ||
|
||
if (address.family === 4) { | ||
return true | ||
} | ||
} | ||
|
||
/** | ||
* Get a list of all IPv4 IPs associated with the host | ||
* @returns {string[]} List of IPv4 IPs | ||
*/ | ||
export default function () { | ||
return Object.values(networkInterfaces()) | ||
.flat() | ||
.filter(address => !address.internal && address.family === 'IPv4') | ||
.filter(isUsableAddress) | ||
.map(address => address.address) | ||
} |