From e2204add2f302cea19c454821096695d05cb2a18 Mon Sep 17 00:00:00 2001 From: acn-support Date: Wed, 9 Dec 2020 13:23:52 +0300 Subject: [PATCH 1/3] add intToIp to network --- common.mjs | 1 + lib/network.js | 6 ++++++ test/network.js | 11 +++++++++++ 3 files changed, 18 insertions(+) diff --git a/common.mjs b/common.mjs index 8e437a48..206a5ce5 100644 --- a/common.mjs +++ b/common.mjs @@ -86,6 +86,7 @@ export const cryptoRandom = common.cryptoRandom; export const methods = common.methods; export const properties = common.properties; export const ipToInt = common.ipToInt; +export const intToIp = common.intToIp; export const localIPs = common.localIPs; export const parseHost = common.parseHost; export const override = common.override; diff --git a/lib/network.js b/lib/network.js index f483fcdd..61e2b0e8 100644 --- a/lib/network.js +++ b/lib/network.js @@ -9,6 +9,11 @@ const os = require('os'); const ipToInt = (ip = '127.0.0.1') => ip.split('.').reduce((res, item) => (res << 8) + +item, 0); +const intToIp = (ipInt = 0) => + [ipInt >>> 24, (ipInt >> 16) & 255, (ipInt >> 8) & 255, ipInt & 255].join( + '.' + ); + let LOCAL_IPS_CACHE; // Get local network interfaces @@ -44,6 +49,7 @@ const parseHost = host => { module.exports = { ipToInt, + intToIp, localIPs, parseHost, }; diff --git a/test/network.js b/test/network.js index 0f574a68..93017e4f 100644 --- a/test/network.js +++ b/test/network.js @@ -21,6 +21,17 @@ metatests.case( ['8.8.8.8', 0x08080808], [undefined, 0x7f000001], ], + 'common.intToIp': [ + [2130706433, '127.0.0.1'], + [167772161, '10.0.0.1'], + [-1062731510, '192.168.1.10'], + [-1511946858, '165.225.133.150'], + [0, '0.0.0.0'], + ['', '0.0.0.0'], + [Number.NaN, '0.0.0.0'], + [0x08080808, '8.8.8.8'], + [0x7f000001, '127.0.0.1'], + ], 'common.parseHost': [ ['', 'no-host-name-in-http-headers'], ['domain.com', 'domain.com'], From ee9c3473c942cce96552b418bf5125dc8d55f8b9 Mon Sep 17 00:00:00 2001 From: dimon-durak Date: Wed, 9 Dec 2020 14:21:37 +0300 Subject: [PATCH 2/3] fix docs for IP address converters --- README.md | 19 +++++++++++++++---- lib/network.js | 10 +++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2fa3add6..dd90a4dc 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,8 @@ $ npm install @metarhia/common - [cryptoRandom](#cryptorandom) - [methods](#methodsiface) - [properties](#propertiesiface) -- [ipToInt](#iptointip) +- [ipToInt](#iptointip_string) +- [intToIp](#inttoipip_number) - [localIPs](#localips) - [parseHost](#parsehosthost) - [override](#overrideobj-fn) @@ -1296,13 +1297,23 @@ _Returns:_ [``][string] property names List property names -### ipToInt(\[ip\]) +### ipToInt(IP\_string) -- `ip`: [``][string] (optional), default: '127.0.0.1', IP address +- `IP_string`: [``][string] (optional), default: '127.0.0.1', IPv4 + address in string form _Returns:_ [``][number] -Convert IP string to number +Convert IPv4 address from string form to number form + +### intToIp(IP\_number) + +- `IP_number`: [``][number] (optional), default: 0, IPv4 address in + number form + +_Returns:_ [``][string] + +Convert IPv4 address from number form to string form ### localIPs() diff --git a/lib/network.js b/lib/network.js index 61e2b0e8..db8b8784 100644 --- a/lib/network.js +++ b/lib/network.js @@ -2,13 +2,17 @@ const os = require('os'); -// Convert IP string to number -// Signature: [ip] -// ip - , (optional), default: '127.0.0.1', IP address +// Convert IPv4 address from string form to number form +// Signature: IP_string +// IP_string - , (optional), default: '127.0.0.1', IPv4 address in string form // Returns: const ipToInt = (ip = '127.0.0.1') => ip.split('.').reduce((res, item) => (res << 8) + +item, 0); +// Convert IPv4 address from number form to string form +// Signature: IP_number +// IP_number - , (optional), default: 0, IPv4 address in number form +// Returns: const intToIp = (ipInt = 0) => [ipInt >>> 24, (ipInt >> 16) & 255, (ipInt >> 8) & 255, ipInt & 255].join( '.' From 4df1222c6b615f527e5c90f2617fe5fcf0e2b088 Mon Sep 17 00:00:00 2001 From: dimon-durak Date: Wed, 9 Dec 2020 14:29:15 +0300 Subject: [PATCH 3/3] add intToIp to CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34b600ed..305f5f1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ and this project adheres to ## [Unreleased][unreleased] +- add intToIp converter to network + ## [2.2.0][] - 2020-07-10 ### Added