vpndetection-web 0.0.0-stage → 1.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.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +268 -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 +156 -0
  8. package/dist/client.js +386 -0
  9. package/dist/errors.d.ts +56 -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 +118 -0
  40. package/dist/generated/sdk.gen.js +174 -0
  41. package/dist/generated/types.gen.d.ts +790 -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
@@ -0,0 +1,47 @@
1
+ import { VPNDetectionError } from './errors.js';
2
+ export interface Res {
3
+ data?: unknown;
4
+ error?: unknown;
5
+ response?: Response;
6
+ }
7
+ export declare function unwrap<T>(res: Res): T;
8
+ /**
9
+ * The request never got an answer: offline, refused by the browser (a CORS
10
+ * failure, an extension blocking it, a page's Content-Security-Policy), or cut
11
+ * off. Browsers say little about which, and what they say is kept.
12
+ */
13
+ export declare function noResponse(cause: unknown): VPNDetectionError;
14
+ /**
15
+ * A 2xx the call cannot read as its answer: a proxy's HTML page, a cut-off,
16
+ * empty or non-object body, or an object without what the call returns. The
17
+ * server's fault, so a retried `server_error` carrying the status.
18
+ */
19
+ export declare function readable<T>(res: Res, valid: (data: unknown) => boolean): T;
20
+ /** An object holding every member named, none of them missing. */
21
+ export declare function hasMembers(value: unknown, members: readonly string[]): value is Record<string, unknown>;
22
+ export declare const MAX_TIMEOUT_MS: number;
23
+ /**
24
+ * Refuses a timeout no attempt can meet where it is set, on the client or the
25
+ * call, rather than failing every call as a timeout 1 ms in.
26
+ */
27
+ export declare function checkTimeout(timeoutMs: number | undefined): number | undefined;
28
+ /**
29
+ * A retry count p-retry can run, refused where it is set, on the client or the
30
+ * call: p-retry checks one only inside the call, so a negative, NaN or
31
+ * non-number would fail every call with its own TypeError, and a fraction would
32
+ * end a failing call as one. `Infinity` is p-retry's own unbounded count, and
33
+ * `null` reads as unset.
34
+ */
35
+ export declare function checkRetries(retries: number | null | undefined): number | undefined;
36
+ /** How many batch requests may be in flight, refused where it is set: p-limit throws a bare TypeError. */
37
+ export declare function checkConcurrency(concurrency: number | undefined): number | undefined;
38
+ /**
39
+ * Bound one attempt, and report an expiry as our own error rather than the
40
+ * runtime's `TimeoutError`, whose message says nothing about which call gave up.
41
+ *
42
+ * The signal is built per call, so a retried request gets a fresh budget: the
43
+ * bound is per attempt.
44
+ */
45
+ export declare function deadline<T>(timeoutMs: number, fn: (signal: AbortSignal) => Promise<T>): Promise<T>;
46
+ export declare function withRetry<T>(retries: number, fn: () => Promise<T>): Promise<T>;
47
+ export declare function asError(err: unknown): VPNDetectionError;
@@ -0,0 +1,140 @@
1
+ import pRetry from 'p-retry';
2
+ import { errorFromResponse, VPNDetectionError } from './errors.js';
3
+ export function unwrap(res) {
4
+ if (res.response === undefined) {
5
+ throw noResponse(res.error);
6
+ }
7
+ if (!res.response.ok) {
8
+ throw errorFromResponse(res.response.status, res.response.headers, res.error ?? res.data);
9
+ }
10
+ return res.data;
11
+ }
12
+ /**
13
+ * The request never got an answer: offline, refused by the browser (a CORS
14
+ * failure, an extension blocking it, a page's Content-Security-Policy), or cut
15
+ * off. Browsers say little about which, and what they say is kept.
16
+ */
17
+ export function noResponse(cause) {
18
+ const detail = cause instanceof Error && cause.message !== '' ? `: ${cause.message}` : '';
19
+ return new VPNDetectionError('network', `no response from the API${detail}`);
20
+ }
21
+ /**
22
+ * A 2xx the call cannot read as its answer: a proxy's HTML page, a cut-off,
23
+ * empty or non-object body, or an object without what the call returns. The
24
+ * server's fault, so a retried `server_error` carrying the status.
25
+ */
26
+ export function readable(res, valid) {
27
+ const data = unwrap(res);
28
+ if (!valid(data)) {
29
+ const status = res.response.status;
30
+ throw new VPNDetectionError('server_error', `unreadable answer from the API, status ${status}`, status);
31
+ }
32
+ return data;
33
+ }
34
+ /** An object holding every member named, none of them missing. */
35
+ export function hasMembers(value, members) {
36
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
37
+ && members.every((member) => value[member] !== undefined);
38
+ }
39
+ // The longest delay setTimeout runs as asked, in every browser as in Node. It runs
40
+ // anything longer, and anything that is not a positive number, as at once.
41
+ export const MAX_TIMEOUT_MS = 2 ** 31 - 1;
42
+ /**
43
+ * Refuses a timeout no attempt can meet where it is set, on the client or the
44
+ * call, rather than failing every call as a timeout 1 ms in.
45
+ */
46
+ export function checkTimeout(timeoutMs) {
47
+ if (timeoutMs !== undefined
48
+ && !(typeof timeoutMs === 'number' && timeoutMs >= 1 && timeoutMs <= MAX_TIMEOUT_MS)) {
49
+ throw new VPNDetectionError('bad_request', `timeoutMs must be from 1 to ${MAX_TIMEOUT_MS} milliseconds, got ${String(timeoutMs)}`);
50
+ }
51
+ return timeoutMs;
52
+ }
53
+ /**
54
+ * A retry count p-retry can run, refused where it is set, on the client or the
55
+ * call: p-retry checks one only inside the call, so a negative, NaN or
56
+ * non-number would fail every call with its own TypeError, and a fraction would
57
+ * end a failing call as one. `Infinity` is p-retry's own unbounded count, and
58
+ * `null` reads as unset.
59
+ */
60
+ export function checkRetries(retries) {
61
+ if (retries === undefined || retries === null) {
62
+ return undefined;
63
+ }
64
+ if (!(typeof retries === 'number' && retries >= 0
65
+ && (Number.isInteger(retries) || retries === Infinity))) {
66
+ throw new VPNDetectionError('bad_request', `retries must be a whole number from 0, got ${String(retries)}`);
67
+ }
68
+ return retries;
69
+ }
70
+ /** How many batch requests may be in flight, refused where it is set: p-limit throws a bare TypeError. */
71
+ export function checkConcurrency(concurrency) {
72
+ if (concurrency !== undefined && !(typeof concurrency === 'number' && concurrency >= 1
73
+ && (Number.isInteger(concurrency) || concurrency === Infinity))) {
74
+ throw new VPNDetectionError('bad_request', `concurrency must be a whole number of at least 1, got ${String(concurrency)}`);
75
+ }
76
+ return concurrency;
77
+ }
78
+ /**
79
+ * Bound one attempt, and report an expiry as our own error rather than the
80
+ * runtime's `TimeoutError`, whose message says nothing about which call gave up.
81
+ *
82
+ * The signal is built per call, so a retried request gets a fresh budget: the
83
+ * bound is per attempt.
84
+ */
85
+ export async function deadline(timeoutMs, fn) {
86
+ const controller = new AbortController();
87
+ let timer;
88
+ // Raced, not left to the signal alone: aborting releases the connection, but
89
+ // only a transport that HONORS the signal then settles, and a substituted
90
+ // `fetch` need not. Clearing the timer stops the losing side rejecting into nothing.
91
+ const expiry = new Promise((_, reject) => {
92
+ timer = setTimeout(() => {
93
+ controller.abort();
94
+ reject(new VPNDetectionError('network', `request timed out after ${timeoutMs}ms`));
95
+ }, timeoutMs);
96
+ });
97
+ try {
98
+ return await Promise.race([fn(controller.signal), expiry]);
99
+ }
100
+ finally {
101
+ clearTimeout(timer);
102
+ }
103
+ }
104
+ // p-retry owns the backoff schedule; the extra sleep here is what honors a
105
+ // server-supplied Retry-After, which p-retry has no way to know about. A 429
106
+ // carrying that header is the only 429 worth retrying, which is why the wait
107
+ // and the retry decision both key off the same field. One past what setTimeout
108
+ // runs as asked is waited out on the backoff alone, still a throttle.
109
+ export async function withRetry(retries, fn) {
110
+ try {
111
+ return await pRetry(fn, {
112
+ retries: retries,
113
+ shouldRetry: ({ error }) => !(error instanceof VPNDetectionError) || error.retryable,
114
+ onFailedAttempt: async ({ error, retriesLeft }) => {
115
+ // p-retry runs this hook before it checks retriesLeft, so with no
116
+ // retry left a wait here would only delay the failure.
117
+ if (retriesLeft <= 0) {
118
+ return;
119
+ }
120
+ const seconds = error instanceof VPNDetectionError ? error.retryAfterSeconds : undefined;
121
+ if (seconds !== undefined && seconds > 0 && seconds * 1000 <= MAX_TIMEOUT_MS) {
122
+ await new Promise((r) => setTimeout(r, seconds * 1000));
123
+ }
124
+ },
125
+ });
126
+ }
127
+ catch (err) {
128
+ throw asError(err);
129
+ }
130
+ }
131
+ export function asError(err) {
132
+ if (err instanceof VPNDetectionError) {
133
+ return err;
134
+ }
135
+ const cause = err?.cause;
136
+ if (cause instanceof VPNDetectionError) {
137
+ return cause;
138
+ }
139
+ return new VPNDetectionError('network', err instanceof Error ? err.message : String(err));
140
+ }
@@ -0,0 +1,48 @@
1
+ import type { Asn, ClassDetail, Location, LookupResponse, ProxyDetail, PubproxyDetail, VpnDetail } from './generated/types.gen.js';
2
+ export type { Asn, ClassDetail, Location, ProxyDetail, PubproxyDetail, VpnDetail, LookupResponse };
3
+ /**
4
+ * What a lookup answers.
5
+ *
6
+ * An **optional** member is one your plan does not include. It never means "we
7
+ * could not check", so `undefined` and `false` are genuinely different answers:
8
+ * `undefined` is "not in your plan", `false` is "checked, and no". Use
9
+ * `?? false` when you only care whether the address is flagged.
10
+ *
11
+ * A detail object that is present but empty (`{}`) means the flag above it is
12
+ * false. A populated one always carries every one of its keys.
13
+ */
14
+ export interface Result {
15
+ /** The address that was looked up, normalized. */
16
+ ip: string;
17
+ /** Whether the address is VPN infrastructure. Every plan includes this. */
18
+ isVpn: boolean;
19
+ /** Set when this answer was computed locally rather than served. */
20
+ isBogon: boolean;
21
+ isHosting?: boolean;
22
+ isRelay?: boolean;
23
+ isTor?: boolean;
24
+ isCdn?: boolean;
25
+ isResproxy?: boolean;
26
+ isDcproxy?: boolean;
27
+ isMobproxy?: boolean;
28
+ isPubproxy?: boolean;
29
+ vpn?: VpnDetail;
30
+ hosting?: ClassDetail;
31
+ relay?: ClassDetail;
32
+ tor?: ClassDetail;
33
+ cdn?: ClassDetail;
34
+ resproxy?: ProxyDetail;
35
+ dcproxy?: ProxyDetail;
36
+ mobproxy?: ProxyDetail;
37
+ pubproxy?: PubproxyDetail;
38
+ /**
39
+ * Where the address is, on every keyed answer: every key present, empty strings where it is
40
+ * not known.
41
+ */
42
+ location?: Location;
43
+ /** The AS announcing the address, on every keyed answer: `asn` is 0 where none does. */
44
+ asn?: Asn;
45
+ /** The response exactly as it came off the wire, with its original names. */
46
+ raw: LookupResponse;
47
+ }
48
+ export declare function toResult(body: LookupResponse): Result;
package/dist/types.js ADDED
@@ -0,0 +1,42 @@
1
+ // The one place the wire's snake_case becomes idiomatic camelCase. Every
2
+ // assignment is conditional on the key being PRESENT rather than truthy, so a
3
+ // plan that includes a field and answers `false` keeps it, and a plan that does
4
+ // not include it stays undefined.
5
+ export function toResult(body) {
6
+ const r = {
7
+ ip: body.ip,
8
+ isVpn: body.is_vpn,
9
+ isBogon: false,
10
+ raw: body,
11
+ };
12
+ copyFlag(body, r, 'is_hosting', 'isHosting');
13
+ copyFlag(body, r, 'is_relay', 'isRelay');
14
+ copyFlag(body, r, 'is_tor', 'isTor');
15
+ copyFlag(body, r, 'is_cdn', 'isCdn');
16
+ copyFlag(body, r, 'is_resproxy', 'isResproxy');
17
+ copyFlag(body, r, 'is_dcproxy', 'isDcproxy');
18
+ copyFlag(body, r, 'is_mobproxy', 'isMobproxy');
19
+ copyFlag(body, r, 'is_pubproxy', 'isPubproxy');
20
+ copyDetail(body, r, 'vpn', 'vpn');
21
+ copyDetail(body, r, 'hosting', 'hosting');
22
+ copyDetail(body, r, 'relay', 'relay');
23
+ copyDetail(body, r, 'tor', 'tor');
24
+ copyDetail(body, r, 'cdn', 'cdn');
25
+ copyDetail(body, r, 'resproxy', 'resproxy');
26
+ copyDetail(body, r, 'dcproxy', 'dcproxy');
27
+ copyDetail(body, r, 'mobproxy', 'mobproxy');
28
+ copyDetail(body, r, 'pubproxy', 'pubproxy');
29
+ copyDetail(body, r, 'location', 'location');
30
+ copyDetail(body, r, 'asn', 'asn');
31
+ return r;
32
+ }
33
+ function copyFlag(body, out, from, to) {
34
+ if (from in body && body[from] !== undefined) {
35
+ out[to] = body[from];
36
+ }
37
+ }
38
+ function copyDetail(body, out, from, to) {
39
+ if (from in body && body[from] !== undefined) {
40
+ out[to] = body[from];
41
+ }
42
+ }
package/package.json CHANGED
@@ -1,6 +1,68 @@
1
1
  {
2
2
  "name": "vpndetection-web",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.1.0",
4
+ "description": "Official browser client library for the VPNDetection API. Detect VPNs, proxies, Tor, hosting and CDN IPs.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "homepage": "https://vpndetection.io",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/vpndetection-io/sdk-web.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/vpndetection-io/sdk-web/issues"
14
+ },
15
+ "keywords": [
16
+ "vpn",
17
+ "vpn-detection",
18
+ "proxy-detection",
19
+ "tor",
20
+ "ip-intelligence",
21
+ "ip-geolocation",
22
+ "fraud-prevention",
23
+ "residential-proxy",
24
+ "mobile-proxy",
25
+ "datacenter-proxy",
26
+ "hosting",
27
+ "cdn",
28
+ "relay",
29
+ "browser",
30
+ "browser-extension",
31
+ "client-side",
32
+ "esm"
33
+ ],
34
+ "main": "./dist/index.js",
35
+ "types": "./dist/index.d.ts",
36
+ "exports": {
37
+ ".": {
38
+ "types": "./dist/index.d.ts",
39
+ "default": "./dist/index.js"
40
+ },
41
+ "./package.json": "./package.json"
42
+ },
43
+ "sideEffects": false,
44
+ "files": [
45
+ "dist",
46
+ "README.md",
47
+ "LICENSE"
48
+ ],
49
+ "dependencies": {
50
+ "lru-cache": "^11.0.2",
51
+ "p-limit": "^6.2.0",
52
+ "p-retry": "^8.0.0"
53
+ },
54
+ "devDependencies": {
55
+ "@hey-api/openapi-ts": "^0.99.0",
56
+ "@mslmio/eslint-config": "^1.1.0",
57
+ "eslint": "^10.10.0",
58
+ "playwright-core": "1.62.1",
59
+ "typescript": "^6.0.3"
60
+ },
61
+ "scripts": {
62
+ "build": "rm -rf dist && openapi-ts && tsc",
63
+ "lint": "eslint",
64
+ "test": "pnpm run build && node --test test/*.test.mjs",
65
+ "test:browser": "pnpm run build && node test/browser/smoke.mjs",
66
+ "generate": "openapi-ts"
67
+ }
6
68
  }