js-cloudip 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Rez Moss
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 ADDED
@@ -0,0 +1,137 @@
1
+ # js-cloudip
2
+
3
+ Fast cloud-provider IP detection for Node.js and the browser — the JavaScript port of [go-cloudip](https://github.com/rezmoss/go-cloudip).
4
+
5
+ Determine if an IP belongs to **AWS, GCP, Azure, Cloudflare, DigitalOcean, or Oracle Cloud**, with sub-millisecond lookups using a binary CIDR trie. Plus a forward-lookup mode JS apps actually want: *"give me every Cloudflare CIDR."*
6
+
7
+ Data comes from [cloudip-db](https://github.com/rezmoss/cloudip-db) (MessagePack, ~743 KB gzipped, SHA-256 verified).
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install js-cloudip
13
+ ```
14
+
15
+ ## Quick start
16
+
17
+ ```ts
18
+ import {
19
+ lookup,
20
+ getProvider,
21
+ isAws,
22
+ isCloudProvider,
23
+ getIPs,
24
+ } from 'js-cloudip';
25
+
26
+ await isAws('52.94.76.1'); // true
27
+ await getProvider('34.64.0.1'); // "gcp"
28
+ await isCloudProvider('104.16.0.1'); // true
29
+
30
+ const r = await lookup('52.94.76.1');
31
+ // { found: true, provider: 'aws', region: 'us-east-1', service: 'EC2', cidr: '52.94.0.0/16', ip_type: 'ipv4' }
32
+
33
+ const cf = await getIPs('cloudflare');
34
+ // [{ ip_address: '104.16.0.0/13', ip_type: 'ipv4', provider: 'cloudflare' }, ...]
35
+ ```
36
+
37
+ First call fetches the database from `cloudip-db`, verifies its SHA-256, and caches it in `~/.cache/js-cloudip/`. Subsequent calls reuse the in-memory copy.
38
+
39
+ ## How data loads
40
+
41
+ The default `Detector` tries each source in order:
42
+
43
+ 1. **Network** — fetches `cloudip.msgpack.gz` + `version.json` from `cloudip-db` and verifies SHA-256.
44
+ 2. **Cache** — uses `~/.cache/js-cloudip/cloudip.msgpack.gz` if network is unavailable.
45
+ 3. **Embedded** — falls back to the copy of `data/cloudip.msgpack.gz` shipped inside the package (air-gapped / browser-bundle friendly).
46
+
47
+ Use `offline: true` to skip step 1 entirely.
48
+
49
+ ## Custom Detector
50
+
51
+ ```ts
52
+ import { newDetector } from 'js-cloudip';
53
+
54
+ const detector = await newDetector({
55
+ dataDir: './cache', // null disables fs cache
56
+ autoUpdateMs: 24 * 60 * 60 * 1000,
57
+ offline: false,
58
+ fetch: globalThis.fetch,
59
+ });
60
+
61
+ detector.lookup('52.94.76.1');
62
+ detector.isAws('52.94.76.1');
63
+ detector.getIPs('cloudflare');
64
+
65
+ await detector.update();
66
+ const { hasUpdate, info } = await detector.checkUpdate();
67
+
68
+ detector.close(); // stop the auto-update timer
69
+ ```
70
+
71
+ ### Options
72
+
73
+ | Option | Default | Notes |
74
+ | -------------- | ---------------------- | ---------------------------------------------------- |
75
+ | `dataDir` | `~/.cache/js-cloudip` | `null` disables the filesystem cache |
76
+ | `autoUpdateMs` | `0` | Minimum effective interval is 1 hour |
77
+ | `offline` | `false` | Skip network; use cache → embedded only |
78
+ | `fetch` | `globalThis.fetch` | Custom fetch (e.g., undici with a proxy) |
79
+ | `dataURL` | cloudip-db raw URL | Override for a mirror |
80
+ | `versionURL` | cloudip-db raw URL | Override for a mirror |
81
+ | `verifySha256` | `true` | Disable only if you trust the transport |
82
+ | `ttlMs` | `24 * 60 * 60 * 1000` | Cache freshness window before forcing a re-fetch |
83
+
84
+ ## Embedded entry point
85
+
86
+ ```ts
87
+ import { lookup, getIPs } from 'js-cloudip/embedded';
88
+
89
+ await lookup('52.94.76.1'); // never touches the network
90
+ ```
91
+
92
+ The `/embedded` subpath pre-configures `offline: true`, no fs cache. It loads the bundled `data/cloudip.msgpack.gz` shipped with the package. Good for tests, build pipelines, and air-gapped environments.
93
+
94
+ ## CLI
95
+
96
+ ```bash
97
+ npx cloudip lookup 52.94.76.1
98
+ npx cloudip get cloudflare
99
+ npx cloudip provider 34.64.0.1
100
+ npx cloudip providers
101
+ npx cloudip check-update
102
+ npx cloudip update
103
+ ```
104
+
105
+ ## Browser
106
+
107
+ The default entry uses `fetch` against `raw.githubusercontent.com`, which sets `Access-Control-Allow-Origin: *` — no proxy needed. The filesystem cache is a no-op in browsers; the in-memory cache still applies.
108
+
109
+ ```ts
110
+ import { lookup } from 'js-cloudip';
111
+ const r = await lookup('1.1.1.1');
112
+ ```
113
+
114
+ ## API parity with go-cloudip
115
+
116
+ | Package-level (uses lazy default detector) | Detector instance method |
117
+ | ------------------------------------------ | ------------------------ |
118
+ | `lookup(ip)` | `detector.lookup(ip)` |
119
+ | `getProvider(ip)` | `detector.getProvider(ip)` |
120
+ | `isCloudProvider(ip)` | `detector.isCloudProvider(ip)` |
121
+ | `isAws / isGcp / isAzure / isCloudflare / isDigitalOcean / isOracle` | same on `detector` |
122
+ | `getIPs(provider \| provider[])` | `detector.getIPs(...)` |
123
+ | `version()` | `detector.version()` |
124
+ | `rangeCount()` | `detector.rangeCount()` |
125
+ | `providers()` | `detector.providers()` |
126
+ | `update()` | `detector.update()` |
127
+ | `checkUpdate()` | `detector.checkUpdate()` |
128
+ | `clearCache()` | `detector.clearCache()` |
129
+ | `remoteVersion()` | — |
130
+
131
+ ## Data source
132
+
133
+ IP ranges come from [rezmoss/cloudip-db](https://github.com/rezmoss/cloudip-db), which compiles official cloud-provider feeds into a MessagePack database. SHA-256 of the uncompressed payload is published in `version.json` and verified on every fetch.
134
+
135
+ ## License
136
+
137
+ MIT — see [LICENSE](LICENSE).
package/bin/cli.js ADDED
@@ -0,0 +1,101 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ checkUpdate,
4
+ clearCache,
5
+ getIPs,
6
+ getProvider,
7
+ lookup,
8
+ providers,
9
+ rangeCount,
10
+ update,
11
+ version,
12
+ } from '../dist/index.js';
13
+
14
+ const HELP = `cloudip — cloud provider IP utilities (js-cloudip)
15
+
16
+ Usage:
17
+ cloudip lookup <ip> Reverse-lookup an IP address
18
+ cloudip get <provider>[,...] Print CIDRs for one or more providers
19
+ cloudip provider <ip> Print provider name for an IP
20
+ cloudip providers List supported providers
21
+ cloudip version Print local data version + range count
22
+ cloudip check-update Check if a newer upstream version exists
23
+ cloudip update Force a refresh from cloudip-db
24
+ cloudip clear-cache Delete the local cache
25
+ cloudip help Show this help
26
+
27
+ Data source: rezmoss/cloudip-db
28
+ `;
29
+
30
+ async function main() {
31
+ const [cmd, ...args] = process.argv.slice(2);
32
+ switch (cmd) {
33
+ case undefined:
34
+ case 'help':
35
+ case '-h':
36
+ case '--help':
37
+ process.stdout.write(HELP);
38
+ return;
39
+ case 'lookup': {
40
+ const ip = args[0];
41
+ if (!ip) throw new Error('usage: cloudip lookup <ip>');
42
+ const r = await lookup(ip);
43
+ console.log(JSON.stringify(r, null, 2));
44
+ if (!r.found) process.exitCode = 1;
45
+ return;
46
+ }
47
+ case 'provider': {
48
+ const ip = args[0];
49
+ if (!ip) throw new Error('usage: cloudip provider <ip>');
50
+ const p = await getProvider(ip);
51
+ if (!p) {
52
+ process.exitCode = 1;
53
+ console.log('unknown');
54
+ } else {
55
+ console.log(p);
56
+ }
57
+ return;
58
+ }
59
+ case 'get': {
60
+ const arg = args[0];
61
+ if (!arg) throw new Error('usage: cloudip get <provider>[,<provider>]');
62
+ const want = arg.split(',').map((s) => s.trim()).filter(Boolean);
63
+ const entries = await getIPs(want);
64
+ for (const e of entries) console.log(e.ip_address);
65
+ return;
66
+ }
67
+ case 'providers': {
68
+ const list = await providers();
69
+ for (const p of list) console.log(p);
70
+ return;
71
+ }
72
+ case 'version': {
73
+ const v = await version();
74
+ const n = await rangeCount();
75
+ console.log(`${v} (${n} ranges)`);
76
+ return;
77
+ }
78
+ case 'check-update': {
79
+ const result = await checkUpdate();
80
+ console.log(JSON.stringify(result, null, 2));
81
+ if (!result.hasUpdate) process.exitCode = 1;
82
+ return;
83
+ }
84
+ case 'update':
85
+ await update();
86
+ console.log('updated');
87
+ return;
88
+ case 'clear-cache':
89
+ await clearCache();
90
+ console.log('cache cleared');
91
+ return;
92
+ default:
93
+ process.stderr.write(`unknown command: ${cmd}\n${HELP}`);
94
+ process.exit(2);
95
+ }
96
+ }
97
+
98
+ main().catch((err) => {
99
+ process.stderr.write(`error: ${err?.message ?? err}\n`);
100
+ process.exit(1);
101
+ });
Binary file
@@ -0,0 +1,90 @@
1
+ type Provider = string;
2
+ interface Range {
3
+ cidr: string;
4
+ provider: Provider;
5
+ region?: string;
6
+ service?: string;
7
+ }
8
+ interface IPEntry {
9
+ ip_address: string;
10
+ ip_type: 'ipv4' | 'ipv6';
11
+ provider: Provider;
12
+ region?: string;
13
+ service?: string;
14
+ }
15
+ interface LookupResult {
16
+ found: boolean;
17
+ provider?: Provider;
18
+ region?: string;
19
+ service?: string;
20
+ cidr?: string;
21
+ ip_type?: 'ipv4' | 'ipv6';
22
+ }
23
+ interface VersionInfo {
24
+ version: string;
25
+ build_time: number;
26
+ sha256: string;
27
+ ranges: number;
28
+ size: number;
29
+ size_gzip: number;
30
+ }
31
+ interface Database {
32
+ version: string;
33
+ build_time: number;
34
+ providers: Provider[];
35
+ ranges: Range[];
36
+ }
37
+ interface DetectorOptions {
38
+ dataDir?: string | null;
39
+ autoUpdateMs?: number;
40
+ offline?: boolean;
41
+ fetch?: typeof fetch;
42
+ dataURL?: string;
43
+ versionURL?: string;
44
+ verifySha256?: boolean;
45
+ ttlMs?: number;
46
+ signal?: AbortSignal;
47
+ }
48
+ interface CheckUpdateResult {
49
+ hasUpdate: boolean;
50
+ info?: VersionInfo;
51
+ }
52
+
53
+ declare class Detector {
54
+ private state?;
55
+ private readonly opts;
56
+ private autoUpdateTimer?;
57
+ constructor(options?: DetectorOptions);
58
+ ready(): Promise<this>;
59
+ close(): void;
60
+ private fetchEnv;
61
+ private loadInitial;
62
+ update(): Promise<void>;
63
+ checkUpdate(): Promise<CheckUpdateResult>;
64
+ clearCache(): Promise<void>;
65
+ private requireState;
66
+ lookup(ip: string): LookupResult;
67
+ getProvider(ip: string): Provider;
68
+ isCloudProvider(ip: string): boolean;
69
+ isAws(ip: string): boolean;
70
+ isGcp(ip: string): boolean;
71
+ isAzure(ip: string): boolean;
72
+ isCloudflare(ip: string): boolean;
73
+ isDigitalOcean(ip: string): boolean;
74
+ isOracle(ip: string): boolean;
75
+ getIPs(providers?: Provider | Provider[]): IPEntry[];
76
+ version(): string;
77
+ buildTime(): number;
78
+ rangeCount(): number;
79
+ providers(): readonly Provider[];
80
+ }
81
+ declare function newDetector(options?: DetectorOptions): Promise<Detector>;
82
+
83
+ declare const PROVIDER_AWS = "aws";
84
+ declare const PROVIDER_GCP = "gcp";
85
+ declare const PROVIDER_AZURE = "azure";
86
+ declare const PROVIDER_CLOUDFLARE = "cloudflare";
87
+ declare const PROVIDER_DIGITALOCEAN = "digitalocean";
88
+ declare const PROVIDER_ORACLE = "oracle";
89
+
90
+ export { type CheckUpdateResult as C, type Database as D, type IPEntry as I, type LookupResult as L, type Provider as P, type Range as R, type VersionInfo as V, Detector as a, PROVIDER_AWS as b, PROVIDER_AZURE as c, PROVIDER_CLOUDFLARE as d, PROVIDER_DIGITALOCEAN as e, PROVIDER_GCP as f, PROVIDER_ORACLE as g, type DetectorOptions as h, newDetector as n };
@@ -0,0 +1,90 @@
1
+ type Provider = string;
2
+ interface Range {
3
+ cidr: string;
4
+ provider: Provider;
5
+ region?: string;
6
+ service?: string;
7
+ }
8
+ interface IPEntry {
9
+ ip_address: string;
10
+ ip_type: 'ipv4' | 'ipv6';
11
+ provider: Provider;
12
+ region?: string;
13
+ service?: string;
14
+ }
15
+ interface LookupResult {
16
+ found: boolean;
17
+ provider?: Provider;
18
+ region?: string;
19
+ service?: string;
20
+ cidr?: string;
21
+ ip_type?: 'ipv4' | 'ipv6';
22
+ }
23
+ interface VersionInfo {
24
+ version: string;
25
+ build_time: number;
26
+ sha256: string;
27
+ ranges: number;
28
+ size: number;
29
+ size_gzip: number;
30
+ }
31
+ interface Database {
32
+ version: string;
33
+ build_time: number;
34
+ providers: Provider[];
35
+ ranges: Range[];
36
+ }
37
+ interface DetectorOptions {
38
+ dataDir?: string | null;
39
+ autoUpdateMs?: number;
40
+ offline?: boolean;
41
+ fetch?: typeof fetch;
42
+ dataURL?: string;
43
+ versionURL?: string;
44
+ verifySha256?: boolean;
45
+ ttlMs?: number;
46
+ signal?: AbortSignal;
47
+ }
48
+ interface CheckUpdateResult {
49
+ hasUpdate: boolean;
50
+ info?: VersionInfo;
51
+ }
52
+
53
+ declare class Detector {
54
+ private state?;
55
+ private readonly opts;
56
+ private autoUpdateTimer?;
57
+ constructor(options?: DetectorOptions);
58
+ ready(): Promise<this>;
59
+ close(): void;
60
+ private fetchEnv;
61
+ private loadInitial;
62
+ update(): Promise<void>;
63
+ checkUpdate(): Promise<CheckUpdateResult>;
64
+ clearCache(): Promise<void>;
65
+ private requireState;
66
+ lookup(ip: string): LookupResult;
67
+ getProvider(ip: string): Provider;
68
+ isCloudProvider(ip: string): boolean;
69
+ isAws(ip: string): boolean;
70
+ isGcp(ip: string): boolean;
71
+ isAzure(ip: string): boolean;
72
+ isCloudflare(ip: string): boolean;
73
+ isDigitalOcean(ip: string): boolean;
74
+ isOracle(ip: string): boolean;
75
+ getIPs(providers?: Provider | Provider[]): IPEntry[];
76
+ version(): string;
77
+ buildTime(): number;
78
+ rangeCount(): number;
79
+ providers(): readonly Provider[];
80
+ }
81
+ declare function newDetector(options?: DetectorOptions): Promise<Detector>;
82
+
83
+ declare const PROVIDER_AWS = "aws";
84
+ declare const PROVIDER_GCP = "gcp";
85
+ declare const PROVIDER_AZURE = "azure";
86
+ declare const PROVIDER_CLOUDFLARE = "cloudflare";
87
+ declare const PROVIDER_DIGITALOCEAN = "digitalocean";
88
+ declare const PROVIDER_ORACLE = "oracle";
89
+
90
+ export { type CheckUpdateResult as C, type Database as D, type IPEntry as I, type LookupResult as L, type Provider as P, type Range as R, type VersionInfo as V, Detector as a, PROVIDER_AWS as b, PROVIDER_AZURE as c, PROVIDER_CLOUDFLARE as d, PROVIDER_DIGITALOCEAN as e, PROVIDER_GCP as f, PROVIDER_ORACLE as g, type DetectorOptions as h, newDetector as n };