vpndetection-web 0.0.0-stage → 1.0.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.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +267 -2
  3. package/dist/bogon.d.ts +28 -0
  4. package/dist/bogon.js +164 -0
  5. package/dist/bogons.gen.d.ts +2 -0
  6. package/dist/bogons.gen.js +87 -0
  7. package/dist/client.d.ts +153 -0
  8. package/dist/client.js +384 -0
  9. package/dist/errors.d.ts +55 -0
  10. package/dist/errors.js +178 -0
  11. package/dist/generated/client/client.gen.d.ts +2 -0
  12. package/dist/generated/client/client.gen.js +216 -0
  13. package/dist/generated/client/index.d.ts +10 -0
  14. package/dist/generated/client/index.js +6 -0
  15. package/dist/generated/client/types.gen.d.ts +120 -0
  16. package/dist/generated/client/types.gen.js +2 -0
  17. package/dist/generated/client/utils.gen.d.ts +37 -0
  18. package/dist/generated/client/utils.gen.js +228 -0
  19. package/dist/generated/client.gen.d.ts +12 -0
  20. package/dist/generated/client.gen.js +3 -0
  21. package/dist/generated/core/auth.gen.d.ts +25 -0
  22. package/dist/generated/core/auth.gen.js +14 -0
  23. package/dist/generated/core/bodySerializer.gen.d.ts +25 -0
  24. package/dist/generated/core/bodySerializer.gen.js +57 -0
  25. package/dist/generated/core/params.gen.d.ts +43 -0
  26. package/dist/generated/core/params.gen.js +109 -0
  27. package/dist/generated/core/pathSerializer.gen.d.ts +33 -0
  28. package/dist/generated/core/pathSerializer.gen.js +106 -0
  29. package/dist/generated/core/queryKeySerializer.gen.d.ts +18 -0
  30. package/dist/generated/core/queryKeySerializer.gen.js +92 -0
  31. package/dist/generated/core/serverSentEvents.gen.d.ts +71 -0
  32. package/dist/generated/core/serverSentEvents.gen.js +132 -0
  33. package/dist/generated/core/types.gen.d.ts +83 -0
  34. package/dist/generated/core/types.gen.js +2 -0
  35. package/dist/generated/core/utils.gen.d.ts +19 -0
  36. package/dist/generated/core/utils.gen.js +87 -0
  37. package/dist/generated/index.d.ts +2 -0
  38. package/dist/generated/index.js +2 -0
  39. package/dist/generated/sdk.gen.d.ts +115 -0
  40. package/dist/generated/sdk.gen.js +171 -0
  41. package/dist/generated/types.gen.d.ts +787 -0
  42. package/dist/generated/types.gen.js +2 -0
  43. package/dist/index.d.ts +10 -0
  44. package/dist/index.js +4 -0
  45. package/dist/oauth.d.ts +114 -0
  46. package/dist/oauth.js +245 -0
  47. package/dist/shapes.d.ts +28 -0
  48. package/dist/shapes.js +133 -0
  49. package/dist/store.d.ts +39 -0
  50. package/dist/store.js +64 -0
  51. package/dist/transport.d.ts +47 -0
  52. package/dist/transport.js +140 -0
  53. package/dist/types.d.ts +48 -0
  54. package/dist/types.js +42 -0
  55. package/package.json +65 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mslm Dev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,268 @@
1
- # Temporary Holding Version
1
+ # [<img src="https://s3.vpndetection.io/vpndetection-public/brand/mark.svg" alt="VPNDetection" height="28"/>](https://vpndetection.io/) VPNDetection Web Client Library
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm](https://img.shields.io/npm/v/vpndetection-web.svg)](https://www.npmjs.com/package/vpndetection-web)
4
+ [![license](https://img.shields.io/npm/l/vpndetection-web.svg)](LICENSE)
5
+
6
+ The official browser client library for the [VPNDetection](https://vpndetection.io) API, for a page's own scripts and for browser extensions.
7
+
8
+ It tells you whether an IP address is a VPN, residential proxy, Tor node, relay or hosting server.
9
+
10
+ **An API key in a web page is public.** Anyone who opens the page can read it from the page's source or its network requests, and spend your allowance with it. In a page, use no key, or call the API from your own server and hand the page its answer. A key belongs only where the person it belongs to is the only one who can read it: their own browser extension, or an internal tool.
11
+
12
+ ## Getting Started
13
+
14
+ ```bash
15
+ npm install vpndetection-web
16
+ ```
17
+
18
+ Or, with no build step, import it from jsDelivr in a module script:
19
+
20
+ ```html
21
+ <script type="module">
22
+ import { VPNDetection } from 'https://cdn.jsdelivr.net/npm/vpndetection-web@1/+esm';
23
+
24
+ const me = await new VPNDetection().myIp();
25
+ console.log(me.isVpn);
26
+ </script>
27
+ ```
28
+
29
+ It runs in Chrome and Edge 109, Firefox 115 ESR and Safari 16.4 or newer. TypeScript types are included.
30
+
31
+ ## Usage
32
+
33
+ **No API key needed to start.** Without a key, a lookup answers `ip` and `is_vpn`, up to 1,000 a day per source address. In a page that address is the visitor's, so each visitor's lookups count against their own allowance, and `myIp` tells you about the visitor:
34
+
35
+ ```js
36
+ import { VPNDetection } from 'vpndetection-web';
37
+
38
+ const client = new VPNDetection();
39
+
40
+ const me = await client.myIp();
41
+ console.log(me.isVpn); // is this visitor on a VPN?
42
+
43
+ const result = await client.lookup('45.83.91.1');
44
+ console.log(result.isVpn); // true
45
+ ```
46
+
47
+ ### With an API key
48
+
49
+ Where the key stays private (see above), a free key adds Tor and relay detection, where the address is and the AS that announces it. Every paid plan answers every field, the same on each, and the plans differ only in how many lookups they include. Create a key in the [console](https://app.vpndetection.io), then pass it in:
50
+
51
+ ```js
52
+ const client = new VPNDetection({ apiKey: 'your-api-key' });
53
+
54
+ const result = await client.lookup('45.83.91.1');
55
+ console.log(result.isVpn); // true
56
+ console.log(result.location?.country); // the ISO 3166 country code
57
+ console.log(result.asn?.name); // who runs the announcing network
58
+ console.log(result.vpn?.provider); // on a paid plan
59
+ console.log(result.isHosting); // on a paid plan
60
+ ```
61
+
62
+ An extension can sign its user in rather than ask them to paste a key: see Sign in with OAuth below.
63
+
64
+ ### Your own address
65
+
66
+ ```js
67
+ const result = await client.myIp();
68
+ console.log(result.ip); // the address we saw this call come from: in a page, the visitor's
69
+ ```
70
+
71
+ It is never cached, since which address the browser is calling from is the question, and a laptop moves between networks.
72
+
73
+ ### Your plan and usage
74
+
75
+ ```js
76
+ const ent = await client.myEntitlement();
77
+ console.log(ent.plan.key); // max
78
+ console.log(ent.usage.requests); // 580
79
+ console.log(ent.usage.window_end); // when the allowance resets
80
+ ```
81
+
82
+ Usage counts against the anniversary of your subscription, not the calendar month and not the billing period, and it is the same number a lookup is gated on. `hard_limit` is null on an uncapped plan, which is not the same as zero. It needs a key, so it belongs where a key does.
83
+
84
+ ### Batch lookup
85
+
86
+ Look up as many addresses as you like in one call. Bogons and cached answers are handled locally, and the rest go to the batch endpoint in chunks of up to 1000, in parallel:
87
+
88
+ ```js
89
+ const results = await client.lookupBatch(['45.83.91.1', '8.8.8.8', '1.1.1.1']);
90
+
91
+ for (const [ip, result] of results) {
92
+ if (result instanceof Error) {
93
+ console.log(`${ip}: ${result.message}`);
94
+ continue;
95
+ }
96
+ console.log(`${ip}: ${result.isVpn}`);
97
+ }
98
+ ```
99
+
100
+ Results are keyed by address, in the order you first listed each one, so duplicates in your list collapse into a single entry and one address failing never loses the rest: it carries its error as its value, with the status the API would have given that address on its own.
101
+
102
+ How many chunks are in flight at once, how many times a failed chunk is retried, and how long each request may take are configurable per call:
103
+
104
+ ```js
105
+ const results = await client.lookupBatch(manyIps, { concurrency: 4, retries: 4, timeoutMs: 10_000 });
106
+ ```
107
+
108
+ ### Caching
109
+
110
+ Answers are cached in memory by default, so repeat lookups of the same address are free, and lookups of one address made at the same moment share a single request:
111
+
112
+ ```js
113
+ const client = new VPNDetection();
114
+
115
+ const result = await client.lookup('45.83.91.1');
116
+ console.log(result.isVpn); // true, API request
117
+
118
+ const result2 = await client.lookup('45.83.91.1');
119
+ console.log(result2.isVpn); // true, no API request, result was cached
120
+ ```
121
+
122
+ You can change the default cache variables (max size, TTL, etc) on initialization, or even disable it:
123
+
124
+ ```js
125
+ const client = new VPNDetection({ cache: { max: 50_000, ttlMs: 6 * 60 * 60 * 1000 } });
126
+ const clientNoCache = new VPNDetection({ cache: false });
127
+ ```
128
+
129
+ ### Keeping answers across a reload
130
+
131
+ The in-memory cache lasts as long as the client, so a page reload starts with an empty one, and so does an extension's service worker, which the browser stops once it sits idle. Give the cache a `store` and answers outlive the client: it is read when the in-memory cache misses, before the network, and written after each answer the API serves. In a page, `sessionStorage` keeps them for the tab:
132
+
133
+ ```js
134
+ const store = {
135
+ get(key) {
136
+ const saved = JSON.parse(sessionStorage.getItem(`vpndetection:${key}`) ?? 'null');
137
+ return saved !== null && saved.expires > Date.now() ? saved.result : undefined;
138
+ },
139
+ set(key, result, ttlMs) {
140
+ const saved = { result: result, expires: Date.now() + ttlMs };
141
+ sessionStorage.setItem(`vpndetection:${key}`, JSON.stringify(saved));
142
+ },
143
+ };
144
+
145
+ const client = new VPNDetection({ cache: { store: store } });
146
+ ```
147
+
148
+ In an extension's service worker, `chrome.storage.session` does the same. A store's methods may return promises, as these do:
149
+
150
+ ```js
151
+ const store = {
152
+ async get(key) {
153
+ const saved = (await chrome.storage.session.get(key))[key];
154
+ return saved !== undefined && saved.expires > Date.now() ? saved.result : undefined;
155
+ },
156
+ async set(key, result, ttlMs) {
157
+ await chrome.storage.session.set({ [key]: { result: result, expires: Date.now() + ttlMs } });
158
+ },
159
+ };
160
+
161
+ const client = new VPNDetection({ cache: { store: store } });
162
+ ```
163
+
164
+ `set` is told how long the answer stays fresh, and dropping it after that is the store's job. Each `key` carries a hash of the client's API key and base URL beside the address, so clients holding different keys can share one store without reading each other's answers. Bogons, errors, `myIp` and `myEntitlement` are never stored, and a store that fails costs a request, never an answer: a `get` that throws reads as a miss, and a `set` that throws is ignored.
165
+
166
+ ### Private and reserved addresses
167
+
168
+ Private, loopback, link-local, documentation and multicast addresses (and their IPv6 equivalents, including the 6to4 and Teredo ranges) can never be VPN or proxy infrastructure. The library answers them locally, so they cost no request and no quota:
169
+
170
+ ```js
171
+ const result = await client.lookup('192.168.1.1');
172
+ result.isBogon; // true, this answer was computed rather than served
173
+ result.isVpn; // false
174
+ ```
175
+
176
+ The check is available on the client, which is handy when your inputs are addresses anyway:
177
+
178
+ ```js
179
+ client.isBogon('10.0.0.1'); // true
180
+ client.isBogon('8.8.8.8'); // false
181
+ ```
182
+
183
+ It is also importable on its own, if you want it without a client:
184
+
185
+ ```js
186
+ import { isBogon } from 'vpndetection-web';
187
+
188
+ isBogon('10.0.0.1'); // true
189
+ ```
190
+
191
+ ### Errors
192
+
193
+ Failures throw a `VPNDetectionError` carrying a `kind` and a `retryable` flag:
194
+
195
+ ```js
196
+ import { VPNDetectionError } from 'vpndetection-web';
197
+
198
+ try {
199
+ await client.lookup('1.1.1.1');
200
+ } catch (err) {
201
+ if (err instanceof VPNDetectionError) {
202
+ console.error(err.kind, err.retryable);
203
+ }
204
+ }
205
+ ```
206
+
207
+ `kind` is one of `bad_request`, `unauthorized`, `forbidden`, `rate_limited`, `quota_exceeded`, `server_error` or `network`.
208
+
209
+ Note that `rate_limited` and `quota_exceeded` both arrive as HTTP 429 and are not the same thing. A rate limit is when the API faces extreme traffic bursts and so retrying later works; but a spent quota needs your allowance raised or the window to roll over. The library retries rate limits for you, but not if your quota is exceeded.
210
+
211
+ A request the browser never got an answer to is a retryable `network` error carrying what the browser said: offline, blocked by an extension, or refused by the page's Content-Security-Policy.
212
+
213
+ ### Timeouts
214
+
215
+ A request that hasn't finished after 30 seconds is abandoned and surfaces as a retryable `network` error. The limit applies to each attempt, so a call that gets retried can take longer in total. Set it for the client, or for a single lookup:
216
+
217
+ ```js
218
+ const client = new VPNDetection({ timeoutMs: 10_000 });
219
+
220
+ const result = await client.lookup('45.83.91.1', { timeoutMs: 2000 });
221
+ ```
222
+
223
+ ### Sign in with OAuth (authorization code)
224
+
225
+ An app or an extension that can take a browser redirect signs the person in there, with a PKCE pair made for that one sign-in:
226
+
227
+ ```js
228
+ const client = new VPNDetection();
229
+ const redirectUri = 'https://your-app.example/callback';
230
+ const pkce = await client.oauth.createPkce();
231
+
232
+ const url = client.oauth.authorizationUrl('your-client-id', redirectUri, pkce.challenge, {
233
+ scope: 'apikeys.use', state: 'your-state',
234
+ });
235
+ // Send the browser to url. Its redirect to redirectUri carries code and state.
236
+ const token = await client.oauth.exchangeAuthorizationCode('your-client-id', code, pkce.verifier, redirectUri);
237
+ ```
238
+
239
+ Check that `state` came back as you sent it before you exchange `code`, which works once. The client ID can also be the https URL of a client metadata document your app serves, and such an app is never handed a key, so `token.apikey` stays absent.
240
+
241
+ `createPkce` and `pkceChallenge` return promises, as Web Crypto does, and need a secure context: a page served over https or from localhost, or an extension. `exchangeRefreshToken` trades the refresh token for a new pair, and `revoke` with it signs the person out. Client IDs are issued on request from support@vpndetection.io.
242
+
243
+ ### Browser support
244
+
245
+ The library is ES2022 and needs only what a browser has, `fetch` and, for PKCE, Web Crypto, so it runs in Chrome and Edge 109, Firefox 115 ESR and Safari 16.4 or newer: in a page, a web worker, or an extension's service worker. Every endpoint it calls answers CORS for any origin, so a page needs nothing from your server. A page whose Content-Security-Policy restricts `connect-src` must allow `https://api.vpndetection.io`. For Node.js, use [vpndetection](https://www.npmjs.com/package/vpndetection) instead.
246
+
247
+ ### Absent is not false
248
+
249
+ Without a key only `ip` and `isVpn` come back, and a free key adds Tor, relay, `location` and `asn`. A field your key does not include is `undefined`, which means "not in your plan" rather than "checked, and no".
250
+
251
+ ```js
252
+ result.isHosting ?? false // when you only want the flag
253
+ result.isHosting === undefined // not in your plan
254
+ ```
255
+
256
+ ## Other Libraries
257
+
258
+ There are official VPNDetection client libraries available for many languages including PHP, Python, Go, Java, Ruby, and many popular frameworks such as Django, Rails, and Laravel. See our GitHub at https://github.com/vpndetection-io for more.
259
+
260
+ ## About VPNDetection
261
+
262
+ VPN Detection API: Accurate anonymity detection identifying VPNs, residential proxies, hosting servers, Tor nodes, CDNs, relays and more.
263
+
264
+ [<img src="https://s3.vpndetection.io/vpndetection-public/brand/mark.svg" alt="VPNDetection" height="64"/>](https://vpndetection.io/)
265
+
266
+ ## License
267
+
268
+ This project is licensed under the [MIT License](LICENSE).
@@ -0,0 +1,28 @@
1
+ import type { Result } from './types.js';
2
+ /**
3
+ * Whether an address is a bogon: private, loopback, link-local, documentation,
4
+ * multicast or otherwise not routable on the public internet, including the
5
+ * IPv6 equivalents and the 6to4 and Teredo ranges that wrap them.
6
+ *
7
+ * These can never be VPN or proxy infrastructure, so the client answers them
8
+ * itself and they never cost a request. An IPv4-mapped address
9
+ * (`::ffff:8.8.8.8`) is judged as the IPv4 address it carries.
10
+ */
11
+ export declare function isBogon(ip: string): boolean;
12
+ /**
13
+ * The IPv4 address an IPv4-mapped IPv6 address carries, dotted, and any other
14
+ * string as given. A server listening on `::` sees every IPv4 visitor in the
15
+ * mapped form, which is inside `::ffff:0:0/96` and so read whole would answer
16
+ * every one of them as a bogon, with no request made.
17
+ */
18
+ export declare function unmapped(ip: string): string;
19
+ /**
20
+ * The answer a bogon gets, in the full shape the API serves at its widest plan:
21
+ * every flag present and false, every detail object present and empty, and
22
+ * `location` and `asn` with every key empty, as the API answers a bogon.
23
+ *
24
+ * `isBogon` is set so a caller can always tell a locally computed answer from a
25
+ * served one. Note this is deliberately the WIDEST shape regardless of your
26
+ * plan, so do not infer which fields your plan includes from a bogon answer.
27
+ */
28
+ export declare function bogonResult(ip: string): Result;
package/dist/bogon.js ADDED
@@ -0,0 +1,164 @@
1
+ import { BOGON_V4, BOGON_V6 } from './bogons.gen.js';
2
+ /**
3
+ * Whether an address is a bogon: private, loopback, link-local, documentation,
4
+ * multicast or otherwise not routable on the public internet, including the
5
+ * IPv6 equivalents and the 6to4 and Teredo ranges that wrap them.
6
+ *
7
+ * These can never be VPN or proxy infrastructure, so the client answers them
8
+ * itself and they never cost a request. An IPv4-mapped address
9
+ * (`::ffff:8.8.8.8`) is judged as the IPv4 address it carries.
10
+ */
11
+ export function isBogon(ip) {
12
+ const addr = unmapped(ip);
13
+ if (addr.includes(':')) {
14
+ const a = v6ToInt(addr);
15
+ return a === null ? false : v6().some((r) => (a & r.mask) === r.net);
16
+ }
17
+ const a = v4ToInt(addr);
18
+ return a === null ? false : v4().some((r) => (a & r.mask) >>> 0 === r.net);
19
+ }
20
+ /**
21
+ * The IPv4 address an IPv4-mapped IPv6 address carries, dotted, and any other
22
+ * string as given. A server listening on `::` sees every IPv4 visitor in the
23
+ * mapped form, which is inside `::ffff:0:0/96` and so read whole would answer
24
+ * every one of them as a bogon, with no request made.
25
+ */
26
+ export function unmapped(ip) {
27
+ if (!ip.includes(':')) {
28
+ return ip;
29
+ }
30
+ const a = v6ToInt(ip);
31
+ if (a === null || a >> 32n !== 0xffffn) {
32
+ return ip;
33
+ }
34
+ const n = Number(a & 0xffffffffn);
35
+ return [n >>> 24, (n >>> 16) & 0xff, (n >>> 8) & 0xff, n & 0xff].join('.');
36
+ }
37
+ /**
38
+ * The answer a bogon gets, in the full shape the API serves at its widest plan:
39
+ * every flag present and false, every detail object present and empty, and
40
+ * `location` and `asn` with every key empty, as the API answers a bogon.
41
+ *
42
+ * `isBogon` is set so a caller can always tell a locally computed answer from a
43
+ * served one. Note this is deliberately the WIDEST shape regardless of your
44
+ * plan, so do not infer which fields your plan includes from a bogon answer.
45
+ */
46
+ export function bogonResult(ip) {
47
+ return {
48
+ ip: ip,
49
+ isBogon: true,
50
+ isVpn: false,
51
+ isHosting: false,
52
+ isRelay: false,
53
+ isTor: false,
54
+ isCdn: false,
55
+ isResproxy: false,
56
+ isDcproxy: false,
57
+ isMobproxy: false,
58
+ isPubproxy: false,
59
+ vpn: {},
60
+ hosting: {},
61
+ relay: {},
62
+ tor: {},
63
+ cdn: {},
64
+ resproxy: {},
65
+ dcproxy: {},
66
+ mobproxy: {},
67
+ pubproxy: {},
68
+ location: {
69
+ continent: '',
70
+ continent_name: '',
71
+ country: '',
72
+ country_name: '',
73
+ timezone: '',
74
+ currency_code: '',
75
+ currency_name: '',
76
+ currency_symbol: '',
77
+ flag_emoji: '',
78
+ flag_unicode: '',
79
+ },
80
+ asn: { asn: 0, name: '', domain: '', type: '', prefix: '' },
81
+ raw: {},
82
+ };
83
+ }
84
+ // Parsed once on first use rather than at import: a consumer that never looks
85
+ // up an address should not pay for the table, and module-scope work is what
86
+ // makes a library expensive to import.
87
+ let v4Cache = null;
88
+ let v6Cache = null;
89
+ function v4() {
90
+ if (!v4Cache) {
91
+ v4Cache = BOGON_V4.map((c) => {
92
+ const [net, bits] = c.split('/');
93
+ const mask = bits === '0' ? 0 : (~0 << (32 - Number(bits))) >>> 0;
94
+ return { net: (v4ToInt(net) & mask) >>> 0, mask: mask };
95
+ });
96
+ }
97
+ return v4Cache;
98
+ }
99
+ function v6() {
100
+ if (!v6Cache) {
101
+ v6Cache = BOGON_V6.map((c) => {
102
+ const [net, bits] = c.split('/');
103
+ const mask = bits === '0' ? 0n : (~0n << BigInt(128 - Number(bits))) & MAX128;
104
+ return { net: v6ToInt(net) & mask, mask: mask };
105
+ });
106
+ }
107
+ return v6Cache;
108
+ }
109
+ const MAX128 = (1n << 128n) - 1n;
110
+ function v4ToInt(ip) {
111
+ const p = ip.split('.');
112
+ if (p.length !== 4) {
113
+ return null;
114
+ }
115
+ let n = 0;
116
+ for (const part of p) {
117
+ const b = Number(part);
118
+ if (!Number.isInteger(b) || b < 0 || b > 255 || part === '') {
119
+ return null;
120
+ }
121
+ n = ((n << 8) | b) >>> 0;
122
+ }
123
+ return n;
124
+ }
125
+ // Handles the `::` run and a trailing IPv4 literal (::ffff:1.2.3.4), which
126
+ // several of the canonical ranges use.
127
+ function v6ToInt(ip) {
128
+ let a = ip;
129
+ const v4Tail = a.match(/(\d+\.\d+\.\d+\.\d+)$/);
130
+ if (v4Tail) {
131
+ const n = v4ToInt(v4Tail[1]);
132
+ if (n === null) {
133
+ return null;
134
+ }
135
+ const hi = (n >>> 16).toString(16).padStart(4, '0');
136
+ const lo = (n & 0xffff).toString(16).padStart(4, '0');
137
+ a = `${a.slice(0, v4Tail.index)}${hi}:${lo}`;
138
+ }
139
+ const dbl = a.indexOf('::');
140
+ let groups;
141
+ if (dbl === -1) {
142
+ groups = a.split(':');
143
+ if (groups.length !== 8) {
144
+ return null;
145
+ }
146
+ }
147
+ else {
148
+ const head = a.slice(0, dbl).split(':').filter((g) => g !== '');
149
+ const tail = a.slice(dbl + 2).split(':').filter((g) => g !== '');
150
+ const fill = 8 - head.length - tail.length;
151
+ if (fill < 0) {
152
+ return null;
153
+ }
154
+ groups = [...head, ...Array(fill).fill('0'), ...tail];
155
+ }
156
+ let n = 0n;
157
+ for (const g of groups) {
158
+ if (!/^[0-9a-fA-F]{1,4}$/.test(g)) {
159
+ return null;
160
+ }
161
+ n = (n << 16n) | BigInt(parseInt(g, 16));
162
+ }
163
+ return n;
164
+ }
@@ -0,0 +1,2 @@
1
+ export declare const BOGON_V4: readonly string[];
2
+ export declare const BOGON_V6: readonly string[];
@@ -0,0 +1,87 @@
1
+ // Generated. Do not edit: a regeneration overwrites it.
2
+ // The list is checked against both upstream definitions on every
3
+ // regeneration.
4
+ export const BOGON_V4 = [
5
+ '0.0.0.0/8',
6
+ '10.0.0.0/8',
7
+ '100.64.0.0/10',
8
+ '127.0.0.0/8',
9
+ '169.254.0.0/16',
10
+ '172.16.0.0/12',
11
+ '192.0.0.0/24',
12
+ '192.0.2.0/24',
13
+ '192.88.99.2/32',
14
+ '192.168.0.0/16',
15
+ '198.18.0.0/15',
16
+ '198.51.100.0/24',
17
+ '203.0.113.0/24',
18
+ '224.0.0.0/4',
19
+ '255.255.255.255/32',
20
+ '240.0.0.0/4',
21
+ ];
22
+ export const BOGON_V6 = [
23
+ '::/128',
24
+ '::1/128',
25
+ '::ffff:0:0/96',
26
+ '::/96',
27
+ '64:ff9b::/96',
28
+ '64:ff9b:1::/48',
29
+ '100::/64',
30
+ '100:0:0:1::/64',
31
+ '2001:2::/48',
32
+ '2001:10::/28',
33
+ '2001:db8::/32',
34
+ '3fff::/20',
35
+ '5f00::/16',
36
+ 'fc00::/7',
37
+ 'fe80::/10',
38
+ 'fec0::/10',
39
+ 'ff00::/8',
40
+ '2002::/24',
41
+ '2002:a00::/24',
42
+ '2002:6440::/26',
43
+ '2002:7f00::/24',
44
+ '2002:a9fe::/32',
45
+ '2002:ac10::/28',
46
+ '2002:c000::/40',
47
+ '2002:c000:200::/40',
48
+ '2002:c058:6302::/48',
49
+ '2002:c0a8::/32',
50
+ '2002:c612::/31',
51
+ '2002:c633:6400::/40',
52
+ '2002:cb00:7100::/40',
53
+ '2002:e000::/20',
54
+ '2002:ffff:ffff::/48',
55
+ '2002:f000::/20',
56
+ '2001::/40',
57
+ '2001:0:a00::/40',
58
+ '2001:0:6440::/42',
59
+ '2001:0:7f00::/40',
60
+ '2001:0:a9fe::/48',
61
+ '2001:0:ac10::/44',
62
+ '2001:0:c000::/56',
63
+ '2001:0:c000:200::/56',
64
+ '2001:0:c058:6302::/64',
65
+ '2001:0:c0a8::/48',
66
+ '2001:0:c612::/47',
67
+ '2001:0:c633:6400::/56',
68
+ '2001:0:cb00:7100::/56',
69
+ '2001:0:e000::/36',
70
+ '2001:0:ffff:ffff::/64',
71
+ '2001:0:f000::/36',
72
+ '2001:1::/32',
73
+ '2001:2::/31',
74
+ '2001:4::/30',
75
+ '2001:8::/29',
76
+ '2001:20::/27',
77
+ '2001:40::/26',
78
+ '2001:80::/25',
79
+ '2001:100::/24',
80
+ '::/3',
81
+ '4000::/3',
82
+ '6000::/3',
83
+ '8000::/3',
84
+ 'a000::/3',
85
+ 'c000::/3',
86
+ 'e000::/3',
87
+ ];