npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ipcc

v0.2.0

Published

Standalone IP to ISO2 Country Code

Downloads

10

Readme

ipcc

Standalone IP to ISO2 Country Code

What Is This?

Based on db-ip.com freely available database, the aim of this program is to return a 2 chars Country Code given a generic ip.

The main reason this software exists is to quickly understand if an IP comes from a European country and, as such, is subject to the Cookie's Law absurdity.

How does it work

The list of known IPv4 and IPv6 is stored entirely in RAM in order to grant zero dependencies and overall good performance if used as module.

As module

var
  ipcc = require('ipcc'),
  ip = '109.200.4.149'
;

// using an EU ip
ipcc.resolve(ip);     // "UK"
ipcc.isEU(ip);        // true

// using a non-EU ip
ip = '216.58.209.238';
ipcc.resolve(ip);     // "US"
ipcc.isEU(ip);        // false

The API is pretty basic, consisting in resolve(is), isEU(ip), isIPv4(ip), and isIPv6(ip). Please note latter 2 methods are very simple and are not suitable to validate entirely IP v4 or v6 ddresses.

As standalone

Used as stand alone, the script will actually use a lot of CPU in order to properly parse static Arrays defined in it.

However, this is the way you can use it:

$ # if the following command fails, try with sudo
$ npm install -g ipcc
$ 
$ # test some IP (v4 or v6)
$ ipcc 109.200.4.149  # UK (EU)
$ ipcc 216.58.209.238 # US

Please note that only EU countries will contain the (EU) suffix.

How to build and create an SQLite version

In case you need this data as SQLite file, you can clone this repository locally and perform following operations:

$ git clone https://github.com/WebReflection/ipcc.git
$ cd ipcc
$ npm install
$ node generate.js

Above procedure should install locally dblite, download the database directly from db-ip.com, and create a file called dbip-country.sqlite.

It will also generate a fresh new copy of build/ipcc file.

In order to query the database, you can use src/utils.js file which together with dblite should give you the ability to query as such:

// copy utils.js functions on top
var
  dblite = require('dblite'),
  db = dblite('dbip-country.sqlite'),
  // retrieve the IP the way you want
  ip = '109.200.4.149',
  table = isIPv4(ip) ? 'ipv4' : 'ipv6',
  norm = isIPv4(ip) ? ip2long(ip) : extendedIPv6(ip)
;
db.query(
  'SELECT country_code FROM ' + table +
  ' WHERE ? BETWEEN lower_bound AND upper_bound LIMIT 1',
  [norm],
  function (rows) {
    console.log(rows); // [['UK']]
    db.close();
  }
);

License

All rights reserved to ip-db.com for their data. Mit Style License for the code I've written.