lib-qqwry-next 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,232 @@
1
+ # lib-qqwry
2
+
3
+ > [!TIP]
4
+ > 本项目基于 [cnwhy/lib-qqwry](https://github.com/cnwhy/lib-qqwry) 二次开发,感谢原作者 [@cnwhy](https://github.com/cnwhy) 的优秀工作。
5
+
6
+ `lib-qqwry` 是一个高效的纯真IP库查询引擎,支持 `qqwry.dat` 和 `qqwry.ipdb` 两种数据格式。
7
+
8
+ 相比原作,本项目主要改动:
9
+
10
+ - **TypeScript 重写**,提供完整类型定义
11
+ - **支持 ESM + CJS 双模**输出
12
+ - **新增 ipdb 格式支持**(`libqqwry.ipdb()`)
13
+ - **现代化构建工具链** (tsup + vitest)
14
+
15
+ ## 安装
16
+
17
+ ```
18
+ npm i lib-qqwry-next
19
+ ```
20
+
21
+ ## 数据文件
22
+
23
+ 使用本库前需要自行准备数据文件:
24
+
25
+ - [纯真IP库 (qqwry.dat)](https://github.com/nmgliangwei/qqwry)
26
+ - [ipip.net ipdb 格式](https://github.com/nmgliangwei/qqwry.ipdb)
27
+
28
+ ## 使用
29
+
30
+ ### Node (CJS)
31
+
32
+ ```js
33
+ const libqqwry = require("lib-qqwry-next");
34
+ const qqwry = libqqwry("./data/qqwry.dat"); // dataPath 必填
35
+ qqwry.speed(); // 启用急速模式
36
+
37
+ const result = qqwry.searchIP("202.103.102.10"); // 查询IP信息
38
+ const ips = qqwry.searchIPScope("0.0.0.0", "1.0.0.0"); // 查询IP段信息
39
+
40
+ // 异步查询IP段信息
41
+ qqwry.searchIPScope("0.0.0.0", "1.0.0.0", (err, iparr) => {
42
+ console.log(iparr);
43
+ });
44
+
45
+ // 流模式返回IP段结果
46
+ qqwry
47
+ .searchIPScopeStream("0.0.0.0", "1.0.0.0", { format: "json" })
48
+ .pipe(process.stdout);
49
+ ```
50
+
51
+ ### Node (ESM)
52
+
53
+ ```js
54
+ import libqqwry from "lib-qqwry-next";
55
+ const qqwry = libqqwry("./data/qqwry.dat", true); // dataPath, speed
56
+ const result = qqwry.searchIP("202.103.102.10");
57
+ ```
58
+
59
+ ### ipdb 格式
60
+
61
+ ```js
62
+ import libqqwry from "lib-qqwry-next";
63
+
64
+ const ipdb = libqqwry.ipdb("./data/qqwry.ipdb"); // dataPath 必填
65
+ const result = ipdb.searchIP("8.8.8.8");
66
+ // { ip: '8.8.8.8', country_name: '美国', region_name: '加利福尼亚州圣克拉拉县山景市谷歌公司' }
67
+ ```
68
+
69
+ ## API
70
+
71
+ ### libqqwry.ipToInt(ip)
72
+
73
+ IP地址转数值:
74
+
75
+ ```
76
+ > libqqwry.ipToInt("255.255.255.255")
77
+ 4294967295
78
+ ```
79
+
80
+ ### libqqwry.intToIP(int)
81
+
82
+ 数值转IP地址:
83
+
84
+ ```
85
+ > libqqwry.intToIP(4294967295)
86
+ '255.255.255.255'
87
+ ```
88
+
89
+ ### libqqwry.ipEndianChange(int)
90
+
91
+ 字节序转换(32位),用于处理 Little-Endian 形式的IP数值:
92
+
93
+ ```
94
+ > libqqwry.ipEndianChange(0x010000FF)
95
+ 4278190081 // 0xFF000001
96
+ ```
97
+
98
+ ### libqqwry(dataPath, speed?) / libqqwry.init(dataPath, speed?)
99
+
100
+ 实例化一个 qqwry.dat 格式解析器(`libqqwry.init()` 是其等价别名):
101
+
102
+ - `dataPath`: IP库路径,**必填**
103
+ - `speed`: 是否开启急速模式(将数据文件读入内存),可选,默认 `false`
104
+
105
+ ```js
106
+ const qqwry = libqqwry("./data/qqwry.dat");
107
+ // 或
108
+ const qqwry = libqqwry("./data/qqwry.dat", true); // 急速模式
109
+ ```
110
+
111
+ ### libqqwry.ipdb(dataPath, options?)
112
+
113
+ 实例化 ipdb 格式解析器:
114
+
115
+ - `dataPath`: ipdb 文件路径,**必填**
116
+ - `options.language`: 查询语言,可选,默认 `"CN"`
117
+
118
+ ```js
119
+ const ipdb = libqqwry.ipdb("./data/qqwry.ipdb", { language: "CN" });
120
+ ```
121
+
122
+ ## Ipdb 解析器
123
+
124
+ ### ipdb.searchIP(ip [, language])
125
+
126
+ 单个IP查询,也可直接调用:`ipdb(ip)` 或 `ipdb(ip, language)`
127
+
128
+ ```
129
+ > ipdb("8.8.8.8")
130
+ { ip: '8.8.8.8',
131
+ country_name: '美国',
132
+ region_name: '加利福尼亚州圣克拉拉县山景市谷歌公司' }
133
+ ```
134
+
135
+ ### ipdb.fields()
136
+
137
+ 返回 ipdb 文件中定义的字段列表:
138
+
139
+ ```
140
+ > ipdb.fields()
141
+ [ 'country_name', 'region_name', 'city_name', 'isp_domain' ]
142
+ ```
143
+
144
+ ### ipdb.languages()
145
+
146
+ 返回 ipdb 文件中支持的语言列表:
147
+
148
+ ```
149
+ > ipdb.languages()
150
+ [ 'CN', 'EN' ]
151
+ ```
152
+
153
+ ### ipdb.buildTime()
154
+
155
+ 返回 ipdb 文件的构建时间(Unix 时间戳,单位:秒):
156
+
157
+ ```
158
+ > ipdb.buildTime()
159
+ 1714003200
160
+ ```
161
+
162
+ ### ipdb.speed() / ipdb.unSpeed()
163
+
164
+ ipdb 格式始终将数据加载到内存中,这两个方法为兼容接口,实际无操作。
165
+
166
+ ## Qqwry 解析器
167
+
168
+ ### qqwry.searchIP(ip)
169
+
170
+ 单个IP查询,也可直接调用:`qqwry(ip)`
171
+
172
+ ```
173
+ > qqwry("255.255.255.255")
174
+ { int: 4294967295,
175
+ ip: '255.255.255.255',
176
+ Country: '纯真网络',
177
+ Area: '2017年1月5日IP数据' }
178
+ ```
179
+
180
+ ### qqwry.searchIPScope(beginIP, endIP [, callback])
181
+
182
+ IP段查询,也可直接调用:`qqwry(beginIP, endIP, callback)`
183
+
184
+ ```
185
+ > qqwry("8.8.8.0", "8.8.8.8")
186
+ [ { begInt: 134744064,
187
+ endInt: 134744071,
188
+ begIP: '8.8.8.0',
189
+ endIP: '8.8.8.7',
190
+ Country: '美国',
191
+ Area: '加利福尼亚州圣克拉拉县山景市谷歌公司' },
192
+ { begInt: 134744072,
193
+ endInt: 134744072,
194
+ begIP: '8.8.8.8',
195
+ endIP: '8.8.8.8',
196
+ Country: '美国',
197
+ Area: '加利福尼亚州圣克拉拉县山景市谷歌公司DNS服务器' } ]
198
+ ```
199
+
200
+ ### qqwry.searchIPScopeStream(beginIP, endIP, options)
201
+
202
+ 流模式返回IP段结果,适合数据量较大的场景:
203
+
204
+ - `format`: 输出格式,支持 `'text'`、`'csv'`、`'json'`、`'object'`
205
+ - `outHeader`: 为 `true` 时,csv 输出表头,json 以对象数组形式输出;默认 `false`
206
+
207
+ ```js
208
+ // 文本格式
209
+ qqwry.searchIPScopeStream("8.8.8.0", "8.8.8.8").pipe(process.stdout);
210
+
211
+ // CSV格式
212
+ qqwry
213
+ .searchIPScopeStream("8.8.8.0", "8.8.8.8", { format: "csv" })
214
+ .pipe(process.stdout);
215
+
216
+ // JSON格式
217
+ qqwry
218
+ .searchIPScopeStream("8.8.8.0", "8.8.8.8", { format: "json" })
219
+ .pipe(process.stdout);
220
+ ```
221
+
222
+ ### qqwry.speed()
223
+
224
+ 启用急速模式(将IP库文件读入内存以提升查询效率)。
225
+
226
+ ### qqwry.unSpeed()
227
+
228
+ 停用急速模式(切换回文件直接读取模式)。
229
+
230
+ ## License
231
+
232
+ BSD — 继承自原作 [cnwhy/lib-qqwry](https://github.com/cnwhy/lib-qqwry)