2.0.0 • Published 3 months ago

get-ip-from-request v2.0.0

Weekly downloads
543
License
MIT
Repository
github
Last release
3 months ago

GitHub license npm npm Downloads GitHub issues

get-ip-from-request

get-ip-from-request is a 0-dependency small configurable tool to get ip from http request and validate ip (v4 & v6)

Installation

Download nodejs first. Use npm to install get-ip-from-request.

npm i get-ip-from-request@latest

Usage

const { createServer } = require("http");
const {
  isIp,
  isIpv4,
  isIpv6,
  getIpFromRequestBuilder,
  getIpFromRequest,
} = require("get-ip-from-request");
const localhost = "127.0.0.1";
console.log(`${localhost} is ip ? `, isIp(localhost)); // true
console.log(`${localhost} is ip v4 ? `, isIpv4(localhost)); // true
console.log(`${localhost} is ip v6 ? `, isIpv6(localhost)); // false

const server = createServer((req, res) => {
  // simple use
  //const ip = getIpFromRequest(req)

  // OR custum use
  const options = { headers: ["my-ip-header-want-to-be-check"] };
  const myPersonnalGetIpFromRequest = getIpFromRequestBuilder(options);

  const ip = myPersonnalGetIpFromRequest(req);

  const v4 = isIpv4(ip);
  const v6 = isIpv6(ip);

  const obj = { ip, v4, v6 };
  return res.end(JSON.stringify(obj));
});

Benchmarks

  • Machine: Windows 10 - x64 | AMD Ryzen 5 5500 Hexa-Core - 3.6GHz | 16GB RAM.
  • Method: require('benchmark').Suite. (5 rounds to measure min, max & average)
  • Node: v18.11.0
  • Run: Fry, 21 Oct 2022 11:39:27 GMT 2022
Versionops/sec (average)+-%runsops/sec minops/sec max
isIpv41.5.0285,3250.10-0.1993-98284,360286,441
is.ipv40.9.0285,3130.10-0.1693-97284,658286,225
v4(r-ip)3.3.0284,8650.08-0.1195-97283,924285,792
isIpv61.5.010,7960.16-0.2891-9710,59810,973
is.ipv60.9.08,8700.16-0.3795-988,4209,084
v6(r-ip)3.3.09,1030.11-0.3594-988,6789,334
isIp1.5.010,5380.11-0.1596-9810,41010,664
is.ip0.9.08,1740.12-0.2996-997,8948,415
is(r-ip)3.3.08,8220.11-0.3396-988,4728,997

2023 UPDATE : node 19

  • Machine: Windows 10 - x64 | AMD Ryzen 5 5500 Hexa-Core - 3.6GHz | 16GB RAM.
  • Method: require('benchmark').Suite. (5 rounds to measure min, max & average)
  • Node: v19.8.1
  • Run: Fry, 31 Mar 2023 11:26:24 GMT 2023
Versionops/sec (average)+-%runsops/sec minops/sec max
isIpv41.5.1280,3200.11-1.6389-96276,185281,715
is.ipv40.9.0280,4740.11-1.7193-99275,288282,133
v4(r-ip)3.3.0280,7230.10-1.4496-98277,047281,789
isIpv61.5.110,6190.15-0.9693-9910,40410,761
is.ipv60.9.08,9450.14-0.2694-988,8879,064
v6(r-ip)3.3.09,1800.13-0.4295-989,0799,352
isIp1.5.110,4910.12-0.3394-9610,35710,533
is.ip0.9.08,1320.12-5.6790-977,8268,272
is(r-ip)3.3.08,8610.13-0.3194-978,7988,902

2024 UPDATE : node 20

  • Machine: Windows 10 - x64 | AMD Ryzen 5 5500 Hexa-Core - 3.6GHz | 16GB RAM.
  • Method: require('benchmark').Suite. (5 rounds to measure min, max & average)
  • Node: v20.11.0
  • Run: Thu, 08 Feb 2024 19:07:34 GMT 2024
Versionops/sec (average)+-%runsops/sec minops/sec max
isIpv42.0.0292,0770.12-1.1096-99289,541293,673
is.ipv40.9.0294,7690.12-0.3692-97293,186296,060
v4(r-ip)3.3.0292,6850.12-0.2490-96291,546293,775
:-------------::---------------:--------:----:----------:----------:
isIpv62.0.010,9400.24-0.5695-9610,70211,034
is.ipv60.9.09,3730.13-0.6396-989,2629,501
v6(r-ip)3.3.09,3660.13-0.6496-999,2899,495
:-------------::---------------:--------:----:----------:----------:
isIp2.0.010,5880.08-0.4893-9710,37310,734
is.ip0.9.08,9800.13-1.9390-998,5999,191
is(r-ip)3.3.08,9770.13-0.9293-998,7109,206

Contributing

...

License

MIT

2.0.0

3 months ago

1.5.1

1 year ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.2

2 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.2

3 years ago

1.1.0

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago