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

@chaeco/ip-lookup

v0.1.3

Published

A unified IP lookup plugin with rotating public providers and normalized results

Readme

@chaeco/ip-lookup

npm version License: MIT TypeScript

English | 简体中文

统一的 IP 查询插件,内置多家公开服务商并自动轮转,返回标准化查询结果。

✨ 特性

  • 🌐 统一结果 — 所有服务商返回统一的 IpLookupResult 结构
  • 🔄 自动轮转 — 查询失败时自动切换到下一个可用服务商
  • 📦 8 个内置服务商 — ip.sb、ipwhois、freeipapi、DB-IP、GeoJS、ipapi.is、ipify、Cloudflare Trace
  • 🔌 可扩展 — 实现 IpLookupProvider 接口即可接入自定义数据源
  • 🛡️ 数据丰富 — 国家、地区、城市、运营商、ASN、坐标、代理/VPN/Tor 标记
  • 🔍 类型安全 — TypeScript 严格模式,全程显式类型
  • 🧪 测试完善 — 51 个测试用例,语句覆盖率 98%

安装

npm install @chaeco/ip-lookup

快速开始

import { createIpLookupPlugin } from '@chaeco/ip-lookup'

const lookup = createIpLookupPlugin()

// 查询当前公网 IP
const self = await lookup.lookupSelf()
console.log(self.ip, self.country, self.isp)

// 查询指定 IP
const info = await lookup.lookupIp('8.8.8.8')
console.log(info.country, info.asn, info.organization)

目录

API

createIpLookupPlugin(options?)

创建 IpLookupPlugin 实例。

选项:

| 选项 | 类型 | 默认值 | 说明 | | ------------ | -------------------- | ------------------ | -------------------- | | timeout | number | 8000 | 请求超时时间(毫秒) | | startIndex | number | 0 | 轮转起始服务商下标 | | providers | IpLookupProvider[] | defaultProviders | 自定义服务商列表 |

plugin.lookupSelf()

查询当前机器的公网 IP 信息。返回 Promise<IpLookupResult>

plugin.lookupIp(ip: string)

查询指定 IP 地址的信息。返回 Promise<IpLookupResult>

IpLookupResult

| 字段 | 类型 | 说明 | | -------------- | --------- | -------------------------- | | ip | string | IP 地址 | | country | string | 国家或地区名称 | | countryCode | string | 国家或地区代码 | | region | string | 省份或州 | | city | string | 城市 | | isp | string | 运营商 | | organization | string | 组织名称 | | asn | string | ASN 编号(不含 "AS" 前缀) | | latitude | number | 纬度 | | longitude | number | 经度 | | timezone | string | 时区 | | isProxy | boolean | 是否为代理 | | isVpn | boolean | 是否为 VPN | | isTor | boolean | 是否为 Tor 出口节点 | | provider | string | 命中的服务商名称 | | raw | unknown | 服务商原始返回数据 |

错误处理

当所有服务商均失败时,抛出 IpLookupError,其 reasons 数组记录了每次失败的原因:

import { IpLookupError, createIpLookupPlugin } from '@chaeco/ip-lookup'

try {
  const result = await lookup.lookupSelf()
} catch (error) {
  if (error instanceof IpLookupError) {
    console.error('所有服务商均失败:', error.reasons)
  }
}

内置服务商

| 服务商 | 模式 | 查询本机 | 查询 IP | 响应类型 | | ---------------- | -------- | -------- | ------- | -------- | | ip.sb | self, ip | ✓ | ✓ | json | | ipwhois | self, ip | ✓ | ✓ | json | | freeipapi | self, ip | ✓ | ✓ | json | | DB-IP | self, ip | ✓ | ✓ | json | | GeoJS | ip | | ✓ | json | | ipapi.is | self, ip | ✓ | ✓ | json | | ipify | self | ✓ | | json | | Cloudflare Trace | self | ✓ | | text |

插件顺序遍历服务商。当某个服务商失败(网络错误、超时、解析失败)时,自动切换到下一个兼容服务商。成功后游标前移,下次查询从不同的服务商开始。

自定义服务商

实现 IpLookupProvider 接口接入自定义服务商:

import { createIpLookupPlugin } from '@chaeco/ip-lookup'
import type { IpLookupContext, IpLookupProvider, IpLookupResult } from '@chaeco/ip-lookup'

const myProvider: IpLookupProvider = {
  name: 'my-provider',
  modes: ['self', 'ip'],
  responseType: 'json',
  buildUrl: (context: IpLookupContext) => `https://api.example.com/${context.ip ?? ''}`,
  parse: (raw: unknown): IpLookupResult => {
    const data = raw as Record<string, unknown>
    return {
      ip: String(data.ip ?? ''),
      country: String(data.country ?? ''),
      provider: 'my-provider',
      raw,
    }
  },
}

const lookup = createIpLookupPlugin({ providers: [myProvider] })

开发

# 安装依赖
npm install

# 类型检查
npm run typecheck

# 代码检查
npm run lint

# 测试并生成覆盖率
npm test

# 构建
npm run build

License

MIT