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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ip2region-gen

v1.1.0

Published

ip2region-gen is an offline IP address location library and IP location data management framework for Node.js

Readme

ip2region-gen

ip2region-gen is an offline IP address location library and IP location data management framework for Node.js, based on ip2region. It can be used as both a library integrated into your project and as a command-line tool.

Language: English | 中文

Contents


Requirements

  • Node.js >= 20.0.0

Installation

Global Installation (CLI Tool)

npm install -g ip2region-gen

Install as Project Dependency

npm install ip2region-gen

Usage

Command Line Usage

ip2region-gen [command] [options]

Available Commands:

  • gen: Generate binary database file
    Example:

    ip2region-gen gen --src=./data/ip.test.txt --dst=./data/ip2region.xdb
  • search: Query IP information in an xdb file
    Example:

    ip2region-gen search --src=./data/ip2region.xdb --ip=1.0.63.255
    # >> IP: 1.0.63.255
    # >> Region: China|0|Guangdong Province|Guangzhou City|Telecom
    # >> IO count: 4
    # >> Time cost: 1024.7 μs
  • parse: Parse qqwry.dat file to a text file
    Example:

    ip2region-gen parse --src=./data/qqwry.dat --dst=./data/qqwry.txt
  • trim: Format qqwry.txt to xdb data text file
    Example:

    ip2region-gen trim --src=./data/qqwry.txt --dst=./data/ip2region.txt

    This command converts qqwry.txt to a ip2region.txt data text file in the required format.
    The default handler only converts Chinese location data; foreign data will be converted to "unknown". For example:

    1.1.64.0  1.1.127.255  Japan–Tokyo I2Ts_Inc

    will be converted to:

    1.1.64.0|1.1.127.255|Overseas|Unknown|Unknown|Unknown

    You can specify a custom processing function for data conversion with the --process parameter.
    For example:

    ip2region-gen trim --src=./data/qqwry.txt --dst=./data/ip2region.txt --process=./data/ip2region.config.js

    You can also create an ip2region.config.(js|ts) file in the current working directory or in the same directory as the source file. The command will automatically search for the config file in:

    • Current working directory
    • Source file directory

    The content of ip2region.config.js can be:

    /**
     * @param {string} line - input line
     * @returns {string} processed line
     */
    export default function (line) {
      const combines = line.split('  ')
      if(combines.length < 2) {
          return false;
      }
      let str = combines.length > 2 ? combines[2] : ''
      const region = str.split('-')?.[0] ?? 'Unknown'
      return `${combines[0]}|${combines[1]}|${region}`;
    }

    This config file processes each line of data and returns the result.
    The converted data will become:

    1.1.64.0|1.1.127.255|Japan–Tokyo I2Ts_Inc
  • convert: Convert qqwry.dat to xdb file
    This command directly converts qqwry.dat to an xdb file. The --process parameter is also supported. Example:

    ip2region-gen convert --dat=./data/qqwry.dat --xdb=./data/ip2region.xdb --process=./data/ip2region.config.js

Usage as an npm Package

import { convert_region, parse_qqwry, new_maker, newWithFileOnly, Vector_Index_Policy } from 'ip2region-gen';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
import trimer from './ip2region.config';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const testDataDir = path.join(__dirname, '..', 'data');
const qqwryDatPath = path.join(testDataDir, 'qqwry.dat');
const testOutputFile = path.join(testDataDir, 'qqwry_output.txt');
const ip2RegionTxtFile = path.join(testDataDir, 'ip2region.test.txt');
const ip2RegionDatPath = path.join(testDataDir, 'ip2region.xdb');

async function parse() {
  await parse_qqwry(qqwryDatPath, testOutputFile);
}

async function convert() {
  // Custom processing function is supported
  await convert_region(testOutputFile, ip2RegionTxtFile, trimer);
}
async function gen_db() {
  const maker = new_maker(Vector_Index_Policy, ip2RegionTxtFile, ip2RegionDatPath);
  await maker.gen();
}

async function main() {
  console.log('Parsing qqwry.dat...');
  await parse();
  console.log('Converting to ip2region.txt...');
  await convert();
  console.log('Generating ip2region.xdb...');
  await gen_db();

  // test search
  const ip1 = '1.1.1.1';
  const ip2 = '1.1.14.210';
  const searcher = newWithFileOnly(ip2RegionDatPath);
  await searcher.search(ip1, (region, err) => {
    if (err) {
      console.error(err);
    } else {
      console.log(`${ip1} -> ${region?.region}`);
    }
  });
  const region = await searcher.search(ip2);
  console.log(`${ip2} -> ${region?.region}`);

  const files = [testOutputFile, ip2RegionTxtFile, ip2RegionDatPath];
  for (const file of files) {
    try {
      if(fs.existsSync(file)){
        fs.unlinkSync(file);
      }
      console.log(`Deleted temporary file: ${file}`);
    } catch (error) {
      console.error(`Failed to delete file: ${file}`, error);
    }
  }
}

main();

Related

Development Guide

  • The project is written in TypeScript, with source code in the src directory.

  • Install dependencies:

    npm install
  • Build the project:

    npm run build

Contributing Guide

Any form of contribution is welcome! To participate, please follow these steps:

  1. Fork this repository and clone it locally.
  2. Create a new branch for your changes.
  3. Commit your changes and push to your branch.
  4. Create a Pull Request with a detailed description of your changes.

It is recommended to run relevant tests before submitting to ensure code quality. If you have any questions or suggestions, feel free to open an Issue.