{"id":16040026,"url":"https://github.com/sonofmagic/node-ip2region","last_synced_at":"2025-03-18T04:30:54.224Z","repository":{"id":186137450,"uuid":"674709345","full_name":"sonofmagic/node-ip2region","owner":"sonofmagic","description":"node-ip2region","archived":false,"fork":false,"pushed_at":"2025-01-30T15:32:49.000Z","size":4471,"stargazers_count":4,"open_issues_count":11,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-16T09:25:23.045Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sonofmagic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":["sonofmagic"],"custom":["https://github.com/sonofmagic/sponsors"]}},"created_at":"2023-08-04T15:16:21.000Z","updated_at":"2024-07-29T07:35:27.000Z","dependencies_parsed_at":"2023-12-27T06:37:26.739Z","dependency_job_id":"73d33227-58ef-4158-962c-cc125fc4b252","html_url":"https://github.com/sonofmagic/node-ip2region","commit_stats":null,"previous_names":["sonofmagic/node-ip2region"],"tags_count":2,"template":false,"template_full_name":"sonofmagic/npm-lib-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonofmagic%2Fnode-ip2region","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonofmagic%2Fnode-ip2region/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonofmagic%2Fnode-ip2region/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonofmagic%2Fnode-ip2region/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sonofmagic","download_url":"https://codeload.github.com/sonofmagic/node-ip2region/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244156514,"owners_count":20407524,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-08T23:09:28.410Z","updated_at":"2025-03-18T04:30:53.219Z","avatar_url":"https://github.com/sonofmagic.png","language":"TypeScript","funding_links":["https://github.com/sponsors/sonofmagic","https://github.com/sonofmagic/sponsors"],"categories":[],"sub_categories":[],"readme":"# ip2region-ts\n\n\u003e forked from [ip2region](https://github.com/lionsoul2014/ip2region), Thanks to [Wu Jian Ping](https://github.com/wujjpp) , [lionsoul2014](https://github.com/lionsoul2014) and all ip2region Contributors!\n\n`ip2region` `node sdk` 的 `typescript` 版本，同时内置 `ip2region.xdb` 作为默认的数据库。\n\n默认自带的 `ip2region.xdb` 版本日期为 `20230805`，后续有改动会进行更新，[源码中的文件位置](https://github.com/lionsoul2014/ip2region/blob/master/data/ip2region.xdb)。\n\n制作此版本是由于 `npm` 上目前的版本比较老，且不能开箱即用，于是 `fork` 一下重新发个包给自己用。\n\n同时此版本兼容 `cjs`(`commonjs`) 和 `esm`(`module`) 两种包引入方式。\n\n## 使用方式\n\n### 安装导入包\n\n```sh\n\u003cnpm/yarn/pnpm\u003e i ip2region-ts\n```\n\n```js\n// 导入包\n// commonjs\nconst Searcher = require('ip2region-ts')\n// or esm/ts\nimport * as Searcher from 'ip2region-ts'\n```\n\n### 内置 xdb 默认路径\n\n```js\nconst dbPath = Searcher.defaultDbFile \n```\n\n### 完全基于文件的查询\n\n```js\n\n// 指定ip2region数据文件路径\nconst dbPath = Searcher.defaultDbFile \n// or 'path/to/ip2region.xdb file path'\n\ntry {\n  // 创建searcher对象\n  const searcher = Searcher.newWithFileOnly(dbPath)\n  // 查询\n  const data = await searcher.search('218.4.167.70')\n  // data: {region: '中国|0|江苏省|苏州市|电信', ioCount: 3, took: 1.342389}\n} catch(e) {\n  console.log(e)\n}\n\n```\n\n### 缓存 `VectorIndex` 索引\n\n```js\n// 指定ip2region数据文件路径\nconst dbPath = Searcher.defaultDbFile // 'ip2region.xdb file path'\n\ntry {\n  // 同步读取vectorIndex\n  const vectorIndex = Searcher.loadVectorIndexFromFile(dbPath)\n  // 创建searcher对象\n  const searcher = Searcher.newWithVectorIndex(dbPath, vectorIndex)\n  // 查询 await 或 promise均可\n  const data = await searcher.search('218.4.167.70')\n  // data: {region: '中国|0|江苏省|苏州市|电信', ioCount: 2, took: 0.402874}\n} catch(e) {\n  console.log(e)\n}\n```\n\n### 缓存整个 `xdb` 数据\n\n```js\n// 指定ip2region数据文件路径\nconst dbPath = Searcher.defaultDbFile // or 'ip2region.xdb file path'\n\ntry {\n  // 同步读取buffer\n  const buffer = Searcher.loadContentFromFile(dbPath)\n  // 创建searcher对象\n  const searcher = Searcher.newWithBuffer(buffer)\n  // 查询 await 或 promise均可\n  const data = await searcher.search('218.4.167.70')\n  // data: {region:'中国|0|江苏省|苏州市|电信', ioCount: 0, took: 0.063833}\n} catch(e) {\n  console.log(e)\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsonofmagic%2Fnode-ip2region","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsonofmagic%2Fnode-ip2region","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsonofmagic%2Fnode-ip2region/lists"}