where-is-ip 0.0.0-stage → 0.2.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 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,75 @@
1
- # Temporary Holding Version
1
+ # where-is-ip
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
+ 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;
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
+ | [Country.is](https://country.is/) | `country-is` | ✓ | ✓ | ✓ |
75
+ | [Cloudflare Trace](https://www.cloudflare.com/) | `cloudflare` | — | ✓ | ✓ |
package/dist/index.cjs ADDED
@@ -0,0 +1,275 @@
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/ipinfo.ts
183
+ var ipinfo = withSelfLookup({
184
+ name: "ipinfo",
185
+ async lookup(ip) {
186
+ const data = await fetchJson(`https://ipinfo.io${ip ? `/${ip}` : ""}/json`);
187
+ const [latitude, longitude] = readText(data.loc)?.split(",") ?? [];
188
+ return normalizeLocation({
189
+ ip: data.ip,
190
+ countryCode: data.country,
191
+ region: data.region,
192
+ city: data.city,
193
+ latitude,
194
+ longitude
195
+ });
196
+ }
197
+ });
198
+
199
+ // src/providers/ipwhois.ts
200
+ var ipwhois = withSelfLookup({
201
+ name: "ipwhois",
202
+ async lookup(ip) {
203
+ const data = await fetchJson(`https://ipwho.is/${ip ?? ""}`);
204
+ if (data.success !== true) throw new Error(readText(data.message) ?? "Lookup was unsuccessful");
205
+ return normalizeLocation({
206
+ ip: data.ip,
207
+ countryCode: data.country_code,
208
+ countryName: data.country,
209
+ region: data.region,
210
+ city: data.city,
211
+ latitude: data.latitude,
212
+ longitude: data.longitude
213
+ });
214
+ }
215
+ });
216
+
217
+ // src/providers/index.ts
218
+ var providers = [
219
+ freeipapi,
220
+ ipwhois,
221
+ geojs,
222
+ ip2location,
223
+ ipInfo,
224
+ ipinfo,
225
+ countryIs,
226
+ cloudflare
227
+ ];
228
+ function getProviders() {
229
+ const isServer = typeof globalThis.location === "undefined";
230
+ return providers.filter((provider) => isServer || !provider.serverOnly);
231
+ }
232
+
233
+ // src/lookup.ts
234
+ async function lookup(ip) {
235
+ const address = ip === void 0 ? void 0 : normalizeIp(ip);
236
+ const providers2 = getProviders();
237
+ const errors = [];
238
+ for (const provider of providers2) {
239
+ try {
240
+ const attempt = address === void 0 ? provider.lookupSelf?.() : provider.lookup?.(address);
241
+ if (!attempt) continue;
242
+ const { ip: detectedIp, countryCode, ...location } = await attempt;
243
+ if (!countryCode) {
244
+ errors.push(new Error(`${provider.name} response did not include a country code`));
245
+ continue;
246
+ }
247
+ return { ip: address ?? normalizeIp(detectedIp), countryCode, ...location, provider: provider.name };
248
+ } catch (cause) {
249
+ errors.push(new Error(`${provider.name} lookup failed`, { cause }));
250
+ }
251
+ }
252
+ throw new AggregateError(errors, `IP geolocation failed for ${address ?? "the caller"}`);
253
+ }
254
+ function normalizeIp(input) {
255
+ if (typeof input !== "string") throw new TypeError("Expected an IP address string");
256
+ const ip = input.trim();
257
+ const octets = ip.split(".");
258
+ const validOctet = (octet) => /^(0|[1-9]\d{0,2})$/.test(octet) && Number(octet) <= 255;
259
+ if (octets.length === 4 && octets.every(validOctet)) return ip;
260
+ if (ip.includes(":") && /^[a-f\d:.]+$/i.test(ip)) {
261
+ try {
262
+ new URL(`https://[${ip}]/`);
263
+ return ip;
264
+ } catch {
265
+ }
266
+ }
267
+ throw new TypeError(`Invalid IP address: ${input}`);
268
+ }
269
+
270
+ // src/index.ts
271
+ var geoip = { lookup };
272
+ // Annotate the CommonJS export names for ESM import in node:
273
+ 0 && (module.exports = {
274
+ geoip
275
+ });
@@ -0,0 +1,13 @@
1
+ export interface GeoIpResult {
2
+ ip: string;
3
+ countryCode: string;
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
+ };
@@ -0,0 +1,13 @@
1
+ export interface GeoIpResult {
2
+ ip: string;
3
+ countryCode: string;
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,248 @@
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/ipinfo.ts
157
+ var ipinfo = withSelfLookup({
158
+ name: "ipinfo",
159
+ async lookup(ip) {
160
+ const data = await fetchJson(`https://ipinfo.io${ip ? `/${ip}` : ""}/json`);
161
+ const [latitude, longitude] = readText(data.loc)?.split(",") ?? [];
162
+ return normalizeLocation({
163
+ ip: data.ip,
164
+ countryCode: data.country,
165
+ region: data.region,
166
+ city: data.city,
167
+ latitude,
168
+ longitude
169
+ });
170
+ }
171
+ });
172
+
173
+ // src/providers/ipwhois.ts
174
+ var ipwhois = withSelfLookup({
175
+ name: "ipwhois",
176
+ async lookup(ip) {
177
+ const data = await fetchJson(`https://ipwho.is/${ip ?? ""}`);
178
+ if (data.success !== true) throw new Error(readText(data.message) ?? "Lookup was unsuccessful");
179
+ return normalizeLocation({
180
+ ip: data.ip,
181
+ countryCode: data.country_code,
182
+ countryName: data.country,
183
+ region: data.region,
184
+ city: data.city,
185
+ latitude: data.latitude,
186
+ longitude: data.longitude
187
+ });
188
+ }
189
+ });
190
+
191
+ // src/providers/index.ts
192
+ var providers = [
193
+ freeipapi,
194
+ ipwhois,
195
+ geojs,
196
+ ip2location,
197
+ ipInfo,
198
+ ipinfo,
199
+ countryIs,
200
+ cloudflare
201
+ ];
202
+ function getProviders() {
203
+ const isServer = typeof globalThis.location === "undefined";
204
+ return providers.filter((provider) => isServer || !provider.serverOnly);
205
+ }
206
+
207
+ // src/lookup.ts
208
+ async function lookup(ip) {
209
+ const address = ip === void 0 ? void 0 : normalizeIp(ip);
210
+ const providers2 = getProviders();
211
+ const errors = [];
212
+ for (const provider of providers2) {
213
+ try {
214
+ const attempt = address === void 0 ? provider.lookupSelf?.() : provider.lookup?.(address);
215
+ if (!attempt) continue;
216
+ const { ip: detectedIp, countryCode, ...location } = await attempt;
217
+ if (!countryCode) {
218
+ errors.push(new Error(`${provider.name} response did not include a country code`));
219
+ continue;
220
+ }
221
+ return { ip: address ?? normalizeIp(detectedIp), countryCode, ...location, provider: provider.name };
222
+ } catch (cause) {
223
+ errors.push(new Error(`${provider.name} lookup failed`, { cause }));
224
+ }
225
+ }
226
+ throw new AggregateError(errors, `IP geolocation failed for ${address ?? "the caller"}`);
227
+ }
228
+ function normalizeIp(input) {
229
+ if (typeof input !== "string") throw new TypeError("Expected an IP address string");
230
+ const ip = input.trim();
231
+ const octets = ip.split(".");
232
+ const validOctet = (octet) => /^(0|[1-9]\d{0,2})$/.test(octet) && Number(octet) <= 255;
233
+ if (octets.length === 4 && octets.every(validOctet)) return ip;
234
+ if (ip.includes(":") && /^[a-f\d:.]+$/i.test(ip)) {
235
+ try {
236
+ new URL(`https://[${ip}]/`);
237
+ return ip;
238
+ } catch {
239
+ }
240
+ }
241
+ throw new TypeError(`Invalid IP address: ${input}`);
242
+ }
243
+
244
+ // src/index.ts
245
+ var geoip = { lookup };
246
+ export {
247
+ geoip
248
+ };
package/package.json CHANGED
@@ -1,6 +1,44 @@
1
1
  {
2
2
  "name": "where-is-ip",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.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
+ }