where-is-ip 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +75 -2
- package/dist/index.cjs +293 -0
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +266 -0
- package/package.json +42 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Huy Dang
|
|
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,76 @@
|
|
|
1
|
-
#
|
|
1
|
+
# where-is-ip
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Look up public IPv4 and IPv6 locations with automatic fallback and no API key.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
Install [`where-is-ip`](https://www.npmjs.com/package/where-is-ip) from npm.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install where-is-ip
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
The package supports modern browsers and Node.js 18 or later.
|
|
16
|
+
|
|
17
|
+
### npm
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import { geoip } from "where-is-ip";
|
|
21
|
+
|
|
22
|
+
const location = await geoip.lookup("8.8.8.8");
|
|
23
|
+
const currentLocation = await geoip.lookup();
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
For CommonJS, use `const { geoip } = require("where-is-ip")`.
|
|
27
|
+
|
|
28
|
+
### Browser via CDN
|
|
29
|
+
|
|
30
|
+
Import the ESM bundle from jsDelivr.
|
|
31
|
+
|
|
32
|
+
```html
|
|
33
|
+
<script type="module">
|
|
34
|
+
import { geoip } from "https://cdn.jsdelivr.net/npm/where-is-ip@latest";
|
|
35
|
+
|
|
36
|
+
console.log(await geoip.lookup("8.8.8.8"));
|
|
37
|
+
</script>
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Methods
|
|
41
|
+
|
|
42
|
+
| Method | Looks up |
|
|
43
|
+
| --- | --- |
|
|
44
|
+
| `geoip.lookup(ip)` | The specified public IPv4 or IPv6 address. |
|
|
45
|
+
| `geoip.lookup()` | The visitor's public IP in a browser or the server's outgoing IP in Node.js. |
|
|
46
|
+
|
|
47
|
+
### Result
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
export interface GeoIpResult {
|
|
51
|
+
ip: string;
|
|
52
|
+
countryCode: string | null;
|
|
53
|
+
countryName: string | null;
|
|
54
|
+
region: string | null;
|
|
55
|
+
city: string | null;
|
|
56
|
+
latitude: number | null;
|
|
57
|
+
longitude: number | null;
|
|
58
|
+
provider: string;
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Providers
|
|
63
|
+
|
|
64
|
+
Providers are tried in table order with a 2-second timeout per attempt.
|
|
65
|
+
|
|
66
|
+
| Provider | ID | Specified IP | Caller IP | Browser |
|
|
67
|
+
| --- | --- | :---: | :---: | :---: |
|
|
68
|
+
| [FreeIPAPI](https://freeipapi.com/) | `freeipapi` | ✓ | ✓ | ✓ |
|
|
69
|
+
| [IPWhois](https://ipwhois.io/) | `ipwhois` | ✓ | ✓ | ✓ |
|
|
70
|
+
| [GeoJS](https://www.geojs.io/) | `geojs` | ✓ | ✓ | ✓ |
|
|
71
|
+
| [IP2Location.io](https://www.ip2location.io/) | `ip2location` | ✓ | ✓ | — |
|
|
72
|
+
| [IP Info](https://ip-info.com/) | `ip-info` | ✓ | ✓ | ✓ |
|
|
73
|
+
| [IPinfo](https://ipinfo.io/) | `ipinfo` | ✓ | ✓ | ✓ |
|
|
74
|
+
| [ipapi.is](https://ipapi.is/) | `ipapi-is` | ✓ | ✓ | ✓ |
|
|
75
|
+
| [Country.is](https://country.is/) | `country-is` | ✓ | ✓ | ✓ |
|
|
76
|
+
| [Cloudflare Trace](https://www.cloudflare.com/) | `cloudflare` | — | ✓ | ✓ |
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
geoip: () => geoip
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/providers/shared.ts
|
|
28
|
+
function withSelfLookup(provider) {
|
|
29
|
+
return { ...provider, lookupSelf: () => provider.lookup() };
|
|
30
|
+
}
|
|
31
|
+
async function fetchJson(url, init) {
|
|
32
|
+
const data = readObject(await fetchBody(url, (response) => response.json(), init));
|
|
33
|
+
if (!data) throw new Error("Expected a JSON object");
|
|
34
|
+
return data;
|
|
35
|
+
}
|
|
36
|
+
function fetchText(url) {
|
|
37
|
+
return fetchBody(url, (response) => response.text());
|
|
38
|
+
}
|
|
39
|
+
async function fetchBody(url, read, init) {
|
|
40
|
+
const controller = new AbortController();
|
|
41
|
+
const timeout = setTimeout(() => controller.abort(), 2e3);
|
|
42
|
+
try {
|
|
43
|
+
const response = await fetch(url, { ...init, signal: controller.signal });
|
|
44
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
45
|
+
return await read(response);
|
|
46
|
+
} finally {
|
|
47
|
+
clearTimeout(timeout);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function normalizeLocation(fields) {
|
|
51
|
+
return {
|
|
52
|
+
ip: readText(fields.ip),
|
|
53
|
+
countryCode: readCountryCode(fields.countryCode),
|
|
54
|
+
countryName: readText(fields.countryName),
|
|
55
|
+
region: readText(fields.region),
|
|
56
|
+
city: readText(fields.city),
|
|
57
|
+
latitude: readCoordinate(fields.latitude, 90),
|
|
58
|
+
longitude: readCoordinate(fields.longitude, 180)
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function readObject(value) {
|
|
62
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function readText(value) {
|
|
67
|
+
const text = typeof value === "string" ? value.trim() : "";
|
|
68
|
+
return text && text !== "-" ? text : null;
|
|
69
|
+
}
|
|
70
|
+
function readCountryCode(value) {
|
|
71
|
+
const code = readText(value)?.toUpperCase();
|
|
72
|
+
return code && /^[A-Z]{2}$/.test(code) ? code : null;
|
|
73
|
+
}
|
|
74
|
+
function readCoordinate(value, limit) {
|
|
75
|
+
if (typeof value !== "number" && readText(value) === null) return null;
|
|
76
|
+
const coordinate = Number(value);
|
|
77
|
+
return Number.isFinite(coordinate) && Math.abs(coordinate) <= limit ? coordinate : null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/providers/cloudflare.ts
|
|
81
|
+
var cloudflare = {
|
|
82
|
+
name: "cloudflare",
|
|
83
|
+
async lookupSelf() {
|
|
84
|
+
const trace = await fetchText("https://www.cloudflare.com/cdn-cgi/trace");
|
|
85
|
+
const ip = trace.match(/^ip=(.*)$/m)?.[1];
|
|
86
|
+
const countryCode = trace.match(/^loc=(.*)$/m)?.[1].trim().toUpperCase();
|
|
87
|
+
return normalizeLocation({
|
|
88
|
+
ip,
|
|
89
|
+
countryCode: countryCode === "XX" ? null : countryCode
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// src/providers/country-is.ts
|
|
95
|
+
var countryIs = withSelfLookup({
|
|
96
|
+
name: "country-is",
|
|
97
|
+
async lookup(ip) {
|
|
98
|
+
const data = await fetchJson(`https://api.country.is/${ip ?? ""}?fields=city,location`);
|
|
99
|
+
const location = readObject(data.location);
|
|
100
|
+
return normalizeLocation({
|
|
101
|
+
ip: data.ip,
|
|
102
|
+
countryCode: data.country,
|
|
103
|
+
city: data.city,
|
|
104
|
+
latitude: location?.latitude,
|
|
105
|
+
longitude: location?.longitude
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// src/providers/freeipapi.ts
|
|
111
|
+
var freeipapi = withSelfLookup({
|
|
112
|
+
name: "freeipapi",
|
|
113
|
+
async lookup(ip) {
|
|
114
|
+
const url = ip ? `https://free.freeipapi.com/api/v1/json/${ip}` : "https://free.freeipapi.com/api/v1/json";
|
|
115
|
+
const data = await fetchJson(url);
|
|
116
|
+
return normalizeLocation({
|
|
117
|
+
ip: data.ipAddress,
|
|
118
|
+
countryCode: data.countryCode,
|
|
119
|
+
countryName: data.countryName,
|
|
120
|
+
region: data.regionName,
|
|
121
|
+
city: data.cityName,
|
|
122
|
+
latitude: data.latitude,
|
|
123
|
+
longitude: data.longitude
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// src/providers/geojs.ts
|
|
129
|
+
var geojs = withSelfLookup({
|
|
130
|
+
name: "geojs",
|
|
131
|
+
async lookup(ip) {
|
|
132
|
+
const data = await fetchJson(`https://get.geojs.io/v1/ip/geo${ip ? `/${ip}` : ""}.json`);
|
|
133
|
+
return normalizeLocation({
|
|
134
|
+
ip: data.ip,
|
|
135
|
+
countryCode: data.country_code,
|
|
136
|
+
countryName: data.country,
|
|
137
|
+
region: data.region,
|
|
138
|
+
city: data.city,
|
|
139
|
+
latitude: data.latitude,
|
|
140
|
+
longitude: data.longitude
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// src/providers/ip-info.ts
|
|
146
|
+
var ipInfo = withSelfLookup({
|
|
147
|
+
name: "ip-info",
|
|
148
|
+
async lookup(ip) {
|
|
149
|
+
const url = ip ? `https://ip-info.com/json?ip=${encodeURIComponent(ip)}` : "https://ip-info.com/json";
|
|
150
|
+
const data = await fetchJson(url);
|
|
151
|
+
return normalizeLocation({
|
|
152
|
+
ip: data.ip,
|
|
153
|
+
countryCode: data.country,
|
|
154
|
+
countryName: data.country_name,
|
|
155
|
+
region: data.region,
|
|
156
|
+
city: data.city,
|
|
157
|
+
latitude: data.latitude,
|
|
158
|
+
longitude: data.longitude
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// src/providers/ip2location.ts
|
|
164
|
+
var ip2location = withSelfLookup({
|
|
165
|
+
name: "ip2location",
|
|
166
|
+
serverOnly: true,
|
|
167
|
+
async lookup(ip) {
|
|
168
|
+
const url = ip ? `https://api.ip2location.io/?ip=${encodeURIComponent(ip)}` : "https://api.ip2location.io/?format=json";
|
|
169
|
+
const data = await fetchJson(url);
|
|
170
|
+
return normalizeLocation({
|
|
171
|
+
ip: data.ip,
|
|
172
|
+
countryCode: data.country_code,
|
|
173
|
+
countryName: data.country_name,
|
|
174
|
+
region: data.region_name,
|
|
175
|
+
city: data.city_name,
|
|
176
|
+
latitude: data.latitude,
|
|
177
|
+
longitude: data.longitude
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// src/providers/ipapi-is.ts
|
|
183
|
+
var ipapiIs = withSelfLookup({
|
|
184
|
+
name: "ipapi-is",
|
|
185
|
+
async lookup(ip) {
|
|
186
|
+
const url = ip ? `https://api.ipapi.is/?q=${encodeURIComponent(ip)}` : "https://api.ipapi.is/";
|
|
187
|
+
const data = await fetchJson(url);
|
|
188
|
+
if (data.is_bogon === true) throw new Error("IP address is not publicly routable");
|
|
189
|
+
return normalizeLocation({
|
|
190
|
+
ip: data.ip,
|
|
191
|
+
countryName: data.country,
|
|
192
|
+
region: data.region,
|
|
193
|
+
city: data.city,
|
|
194
|
+
latitude: data.lat,
|
|
195
|
+
longitude: data.lon
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// src/providers/ipinfo.ts
|
|
201
|
+
var ipinfo = withSelfLookup({
|
|
202
|
+
name: "ipinfo",
|
|
203
|
+
async lookup(ip) {
|
|
204
|
+
const data = await fetchJson(`https://ipinfo.io${ip ? `/${ip}` : ""}/json`);
|
|
205
|
+
const [latitude, longitude] = readText(data.loc)?.split(",") ?? [];
|
|
206
|
+
return normalizeLocation({
|
|
207
|
+
ip: data.ip,
|
|
208
|
+
countryCode: data.country,
|
|
209
|
+
region: data.region,
|
|
210
|
+
city: data.city,
|
|
211
|
+
latitude,
|
|
212
|
+
longitude
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// src/providers/ipwhois.ts
|
|
218
|
+
var ipwhois = withSelfLookup({
|
|
219
|
+
name: "ipwhois",
|
|
220
|
+
async lookup(ip) {
|
|
221
|
+
const data = await fetchJson(`https://ipwho.is/${ip ?? ""}`);
|
|
222
|
+
if (data.success !== true) throw new Error(readText(data.message) ?? "Lookup was unsuccessful");
|
|
223
|
+
return normalizeLocation({
|
|
224
|
+
ip: data.ip,
|
|
225
|
+
countryCode: data.country_code,
|
|
226
|
+
countryName: data.country,
|
|
227
|
+
region: data.region,
|
|
228
|
+
city: data.city,
|
|
229
|
+
latitude: data.latitude,
|
|
230
|
+
longitude: data.longitude
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
// src/providers/index.ts
|
|
236
|
+
var providers = [
|
|
237
|
+
freeipapi,
|
|
238
|
+
ipwhois,
|
|
239
|
+
geojs,
|
|
240
|
+
ip2location,
|
|
241
|
+
ipInfo,
|
|
242
|
+
ipinfo,
|
|
243
|
+
ipapiIs,
|
|
244
|
+
countryIs,
|
|
245
|
+
cloudflare
|
|
246
|
+
];
|
|
247
|
+
function getProviders() {
|
|
248
|
+
const isServer = typeof globalThis.location === "undefined";
|
|
249
|
+
return providers.filter((provider) => isServer || !provider.serverOnly);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// src/lookup.ts
|
|
253
|
+
async function lookup(ip) {
|
|
254
|
+
const address = ip === void 0 ? void 0 : normalizeIp(ip);
|
|
255
|
+
const providers2 = getProviders();
|
|
256
|
+
const errors = [];
|
|
257
|
+
for (const provider of providers2) {
|
|
258
|
+
try {
|
|
259
|
+
const attempt = address === void 0 ? provider.lookupSelf?.() : provider.lookup?.(address);
|
|
260
|
+
if (!attempt) continue;
|
|
261
|
+
const { ip: detectedIp, ...location } = await attempt;
|
|
262
|
+
if (!location.countryCode && !location.countryName) {
|
|
263
|
+
throw new Error("Response did not include a country");
|
|
264
|
+
}
|
|
265
|
+
return { ip: address ?? normalizeIp(detectedIp), ...location, provider: provider.name };
|
|
266
|
+
} catch (cause) {
|
|
267
|
+
errors.push(new Error(`${provider.name} lookup failed`, { cause }));
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
throw new AggregateError(errors, `IP geolocation failed for ${address ?? "the caller"}`);
|
|
271
|
+
}
|
|
272
|
+
function normalizeIp(input) {
|
|
273
|
+
if (typeof input !== "string") throw new TypeError("Expected an IP address string");
|
|
274
|
+
const ip = input.trim();
|
|
275
|
+
const octets = ip.split(".");
|
|
276
|
+
const validOctet = (octet) => /^(0|[1-9]\d{0,2})$/.test(octet) && Number(octet) <= 255;
|
|
277
|
+
if (octets.length === 4 && octets.every(validOctet)) return ip;
|
|
278
|
+
if (ip.includes(":") && /^[a-f\d:.]+$/i.test(ip)) {
|
|
279
|
+
try {
|
|
280
|
+
new URL(`https://[${ip}]/`);
|
|
281
|
+
return ip;
|
|
282
|
+
} catch {
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
throw new TypeError(`Invalid IP address: ${input}`);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// src/index.ts
|
|
289
|
+
var geoip = { lookup };
|
|
290
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
291
|
+
0 && (module.exports = {
|
|
292
|
+
geoip
|
|
293
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface GeoIpResult {
|
|
2
|
+
ip: string;
|
|
3
|
+
countryCode: string | null;
|
|
4
|
+
countryName: string | null;
|
|
5
|
+
region: string | null;
|
|
6
|
+
city: string | null;
|
|
7
|
+
latitude: number | null;
|
|
8
|
+
longitude: number | null;
|
|
9
|
+
provider: string;
|
|
10
|
+
}
|
|
11
|
+
export declare const geoip: {
|
|
12
|
+
lookup(ip?: string): Promise<GeoIpResult>;
|
|
13
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface GeoIpResult {
|
|
2
|
+
ip: string;
|
|
3
|
+
countryCode: string | null;
|
|
4
|
+
countryName: string | null;
|
|
5
|
+
region: string | null;
|
|
6
|
+
city: string | null;
|
|
7
|
+
latitude: number | null;
|
|
8
|
+
longitude: number | null;
|
|
9
|
+
provider: string;
|
|
10
|
+
}
|
|
11
|
+
export declare const geoip: {
|
|
12
|
+
lookup(ip?: string): Promise<GeoIpResult>;
|
|
13
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
// src/providers/shared.ts
|
|
2
|
+
function withSelfLookup(provider) {
|
|
3
|
+
return { ...provider, lookupSelf: () => provider.lookup() };
|
|
4
|
+
}
|
|
5
|
+
async function fetchJson(url, init) {
|
|
6
|
+
const data = readObject(await fetchBody(url, (response) => response.json(), init));
|
|
7
|
+
if (!data) throw new Error("Expected a JSON object");
|
|
8
|
+
return data;
|
|
9
|
+
}
|
|
10
|
+
function fetchText(url) {
|
|
11
|
+
return fetchBody(url, (response) => response.text());
|
|
12
|
+
}
|
|
13
|
+
async function fetchBody(url, read, init) {
|
|
14
|
+
const controller = new AbortController();
|
|
15
|
+
const timeout = setTimeout(() => controller.abort(), 2e3);
|
|
16
|
+
try {
|
|
17
|
+
const response = await fetch(url, { ...init, signal: controller.signal });
|
|
18
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
19
|
+
return await read(response);
|
|
20
|
+
} finally {
|
|
21
|
+
clearTimeout(timeout);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function normalizeLocation(fields) {
|
|
25
|
+
return {
|
|
26
|
+
ip: readText(fields.ip),
|
|
27
|
+
countryCode: readCountryCode(fields.countryCode),
|
|
28
|
+
countryName: readText(fields.countryName),
|
|
29
|
+
region: readText(fields.region),
|
|
30
|
+
city: readText(fields.city),
|
|
31
|
+
latitude: readCoordinate(fields.latitude, 90),
|
|
32
|
+
longitude: readCoordinate(fields.longitude, 180)
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function readObject(value) {
|
|
36
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function readText(value) {
|
|
41
|
+
const text = typeof value === "string" ? value.trim() : "";
|
|
42
|
+
return text && text !== "-" ? text : null;
|
|
43
|
+
}
|
|
44
|
+
function readCountryCode(value) {
|
|
45
|
+
const code = readText(value)?.toUpperCase();
|
|
46
|
+
return code && /^[A-Z]{2}$/.test(code) ? code : null;
|
|
47
|
+
}
|
|
48
|
+
function readCoordinate(value, limit) {
|
|
49
|
+
if (typeof value !== "number" && readText(value) === null) return null;
|
|
50
|
+
const coordinate = Number(value);
|
|
51
|
+
return Number.isFinite(coordinate) && Math.abs(coordinate) <= limit ? coordinate : null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/providers/cloudflare.ts
|
|
55
|
+
var cloudflare = {
|
|
56
|
+
name: "cloudflare",
|
|
57
|
+
async lookupSelf() {
|
|
58
|
+
const trace = await fetchText("https://www.cloudflare.com/cdn-cgi/trace");
|
|
59
|
+
const ip = trace.match(/^ip=(.*)$/m)?.[1];
|
|
60
|
+
const countryCode = trace.match(/^loc=(.*)$/m)?.[1].trim().toUpperCase();
|
|
61
|
+
return normalizeLocation({
|
|
62
|
+
ip,
|
|
63
|
+
countryCode: countryCode === "XX" ? null : countryCode
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// src/providers/country-is.ts
|
|
69
|
+
var countryIs = withSelfLookup({
|
|
70
|
+
name: "country-is",
|
|
71
|
+
async lookup(ip) {
|
|
72
|
+
const data = await fetchJson(`https://api.country.is/${ip ?? ""}?fields=city,location`);
|
|
73
|
+
const location = readObject(data.location);
|
|
74
|
+
return normalizeLocation({
|
|
75
|
+
ip: data.ip,
|
|
76
|
+
countryCode: data.country,
|
|
77
|
+
city: data.city,
|
|
78
|
+
latitude: location?.latitude,
|
|
79
|
+
longitude: location?.longitude
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// src/providers/freeipapi.ts
|
|
85
|
+
var freeipapi = withSelfLookup({
|
|
86
|
+
name: "freeipapi",
|
|
87
|
+
async lookup(ip) {
|
|
88
|
+
const url = ip ? `https://free.freeipapi.com/api/v1/json/${ip}` : "https://free.freeipapi.com/api/v1/json";
|
|
89
|
+
const data = await fetchJson(url);
|
|
90
|
+
return normalizeLocation({
|
|
91
|
+
ip: data.ipAddress,
|
|
92
|
+
countryCode: data.countryCode,
|
|
93
|
+
countryName: data.countryName,
|
|
94
|
+
region: data.regionName,
|
|
95
|
+
city: data.cityName,
|
|
96
|
+
latitude: data.latitude,
|
|
97
|
+
longitude: data.longitude
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// src/providers/geojs.ts
|
|
103
|
+
var geojs = withSelfLookup({
|
|
104
|
+
name: "geojs",
|
|
105
|
+
async lookup(ip) {
|
|
106
|
+
const data = await fetchJson(`https://get.geojs.io/v1/ip/geo${ip ? `/${ip}` : ""}.json`);
|
|
107
|
+
return normalizeLocation({
|
|
108
|
+
ip: data.ip,
|
|
109
|
+
countryCode: data.country_code,
|
|
110
|
+
countryName: data.country,
|
|
111
|
+
region: data.region,
|
|
112
|
+
city: data.city,
|
|
113
|
+
latitude: data.latitude,
|
|
114
|
+
longitude: data.longitude
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// src/providers/ip-info.ts
|
|
120
|
+
var ipInfo = withSelfLookup({
|
|
121
|
+
name: "ip-info",
|
|
122
|
+
async lookup(ip) {
|
|
123
|
+
const url = ip ? `https://ip-info.com/json?ip=${encodeURIComponent(ip)}` : "https://ip-info.com/json";
|
|
124
|
+
const data = await fetchJson(url);
|
|
125
|
+
return normalizeLocation({
|
|
126
|
+
ip: data.ip,
|
|
127
|
+
countryCode: data.country,
|
|
128
|
+
countryName: data.country_name,
|
|
129
|
+
region: data.region,
|
|
130
|
+
city: data.city,
|
|
131
|
+
latitude: data.latitude,
|
|
132
|
+
longitude: data.longitude
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// src/providers/ip2location.ts
|
|
138
|
+
var ip2location = withSelfLookup({
|
|
139
|
+
name: "ip2location",
|
|
140
|
+
serverOnly: true,
|
|
141
|
+
async lookup(ip) {
|
|
142
|
+
const url = ip ? `https://api.ip2location.io/?ip=${encodeURIComponent(ip)}` : "https://api.ip2location.io/?format=json";
|
|
143
|
+
const data = await fetchJson(url);
|
|
144
|
+
return normalizeLocation({
|
|
145
|
+
ip: data.ip,
|
|
146
|
+
countryCode: data.country_code,
|
|
147
|
+
countryName: data.country_name,
|
|
148
|
+
region: data.region_name,
|
|
149
|
+
city: data.city_name,
|
|
150
|
+
latitude: data.latitude,
|
|
151
|
+
longitude: data.longitude
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
// src/providers/ipapi-is.ts
|
|
157
|
+
var ipapiIs = withSelfLookup({
|
|
158
|
+
name: "ipapi-is",
|
|
159
|
+
async lookup(ip) {
|
|
160
|
+
const url = ip ? `https://api.ipapi.is/?q=${encodeURIComponent(ip)}` : "https://api.ipapi.is/";
|
|
161
|
+
const data = await fetchJson(url);
|
|
162
|
+
if (data.is_bogon === true) throw new Error("IP address is not publicly routable");
|
|
163
|
+
return normalizeLocation({
|
|
164
|
+
ip: data.ip,
|
|
165
|
+
countryName: data.country,
|
|
166
|
+
region: data.region,
|
|
167
|
+
city: data.city,
|
|
168
|
+
latitude: data.lat,
|
|
169
|
+
longitude: data.lon
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// src/providers/ipinfo.ts
|
|
175
|
+
var ipinfo = withSelfLookup({
|
|
176
|
+
name: "ipinfo",
|
|
177
|
+
async lookup(ip) {
|
|
178
|
+
const data = await fetchJson(`https://ipinfo.io${ip ? `/${ip}` : ""}/json`);
|
|
179
|
+
const [latitude, longitude] = readText(data.loc)?.split(",") ?? [];
|
|
180
|
+
return normalizeLocation({
|
|
181
|
+
ip: data.ip,
|
|
182
|
+
countryCode: data.country,
|
|
183
|
+
region: data.region,
|
|
184
|
+
city: data.city,
|
|
185
|
+
latitude,
|
|
186
|
+
longitude
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// src/providers/ipwhois.ts
|
|
192
|
+
var ipwhois = withSelfLookup({
|
|
193
|
+
name: "ipwhois",
|
|
194
|
+
async lookup(ip) {
|
|
195
|
+
const data = await fetchJson(`https://ipwho.is/${ip ?? ""}`);
|
|
196
|
+
if (data.success !== true) throw new Error(readText(data.message) ?? "Lookup was unsuccessful");
|
|
197
|
+
return normalizeLocation({
|
|
198
|
+
ip: data.ip,
|
|
199
|
+
countryCode: data.country_code,
|
|
200
|
+
countryName: data.country,
|
|
201
|
+
region: data.region,
|
|
202
|
+
city: data.city,
|
|
203
|
+
latitude: data.latitude,
|
|
204
|
+
longitude: data.longitude
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
// src/providers/index.ts
|
|
210
|
+
var providers = [
|
|
211
|
+
freeipapi,
|
|
212
|
+
ipwhois,
|
|
213
|
+
geojs,
|
|
214
|
+
ip2location,
|
|
215
|
+
ipInfo,
|
|
216
|
+
ipinfo,
|
|
217
|
+
ipapiIs,
|
|
218
|
+
countryIs,
|
|
219
|
+
cloudflare
|
|
220
|
+
];
|
|
221
|
+
function getProviders() {
|
|
222
|
+
const isServer = typeof globalThis.location === "undefined";
|
|
223
|
+
return providers.filter((provider) => isServer || !provider.serverOnly);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// src/lookup.ts
|
|
227
|
+
async function lookup(ip) {
|
|
228
|
+
const address = ip === void 0 ? void 0 : normalizeIp(ip);
|
|
229
|
+
const providers2 = getProviders();
|
|
230
|
+
const errors = [];
|
|
231
|
+
for (const provider of providers2) {
|
|
232
|
+
try {
|
|
233
|
+
const attempt = address === void 0 ? provider.lookupSelf?.() : provider.lookup?.(address);
|
|
234
|
+
if (!attempt) continue;
|
|
235
|
+
const { ip: detectedIp, ...location } = await attempt;
|
|
236
|
+
if (!location.countryCode && !location.countryName) {
|
|
237
|
+
throw new Error("Response did not include a country");
|
|
238
|
+
}
|
|
239
|
+
return { ip: address ?? normalizeIp(detectedIp), ...location, provider: provider.name };
|
|
240
|
+
} catch (cause) {
|
|
241
|
+
errors.push(new Error(`${provider.name} lookup failed`, { cause }));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
throw new AggregateError(errors, `IP geolocation failed for ${address ?? "the caller"}`);
|
|
245
|
+
}
|
|
246
|
+
function normalizeIp(input) {
|
|
247
|
+
if (typeof input !== "string") throw new TypeError("Expected an IP address string");
|
|
248
|
+
const ip = input.trim();
|
|
249
|
+
const octets = ip.split(".");
|
|
250
|
+
const validOctet = (octet) => /^(0|[1-9]\d{0,2})$/.test(octet) && Number(octet) <= 255;
|
|
251
|
+
if (octets.length === 4 && octets.every(validOctet)) return ip;
|
|
252
|
+
if (ip.includes(":") && /^[a-f\d:.]+$/i.test(ip)) {
|
|
253
|
+
try {
|
|
254
|
+
new URL(`https://[${ip}]/`);
|
|
255
|
+
return ip;
|
|
256
|
+
} catch {
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
throw new TypeError(`Invalid IP address: ${input}`);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// src/index.ts
|
|
263
|
+
var geoip = { lookup };
|
|
264
|
+
export {
|
|
265
|
+
geoip
|
|
266
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "where-is-ip",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Look up IP locations with native fetch and automatic fallback between keyless providers.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/omyto/where-is-ip.git"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./dist/index.cjs",
|
|
12
|
+
"jsdelivr": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"types": "./dist/index.d.cts",
|
|
22
|
+
"default": "./dist/index.cjs"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist/index.js",
|
|
28
|
+
"dist/index.cjs",
|
|
29
|
+
"dist/index.d.ts",
|
|
30
|
+
"dist/index.d.cts"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=18"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc && node scripts/build.mjs",
|
|
37
|
+
"prepack": "npm run build"
|
|
38
|
+
},
|
|
39
|
+
"keywords": ["ip-geolocation", "geoip", "ip-lookup", "ip-location", "ipv4", "ipv6"],
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"esbuild": "0.28.1",
|
|
42
|
+
"typescript": "5.9.3"
|
|
43
|
+
}
|
|
44
|
+
}
|