vpndetection-web 1.5.1 → 1.5.2

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/README.md CHANGED
@@ -129,6 +129,8 @@ const client = new VPNDetection({ cache: { max: 50_000, ttlMs: 6 * 60 * 60 * 100
129
129
  const clientNoCache = new VPNDetection({ cache: false });
130
130
  ```
131
131
 
132
+ An answer with `trialExpired` or `capReached` true is cached for a minute at most, in memory and in a `store`, so a new day's trial or a raised cap reaches you within a minute.
133
+
132
134
  ### Keeping answers across a reload
133
135
 
134
136
  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:
@@ -241,7 +243,7 @@ const token = await client.oauth.exchangeAuthorizationCode('your-client-id', cod
241
243
 
242
244
  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.
243
245
 
244
- `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.
246
+ `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. Any app can sign people in this way without asking us first: its client ID is the https URL of a client metadata document it serves.
245
247
 
246
248
  ### Browser support
247
249
 
package/dist/client.d.ts CHANGED
@@ -7,7 +7,10 @@ export declare const DEFAULT_BASE_URL = "https://api.vpndetection.io";
7
7
  export interface CacheOptions {
8
8
  /** Maximum number of addresses held in memory. Default 10000. */
9
9
  max?: number;
10
- /** How long an answer stays fresh, in milliseconds. Default 1 hour. */
10
+ /**
11
+ * How long an answer stays fresh, in milliseconds. Default 1 hour. An answer
12
+ * marked `trialExpired` or `capReached` stays a minute at most.
13
+ */
11
14
  ttlMs?: number;
12
15
  /**
13
16
  * Where answers outlive this client: read after the in-memory cache misses and
package/dist/client.js CHANGED
@@ -6,7 +6,7 @@ import { bogonResult, isBogon, unmapped } from './bogon.js';
6
6
  import { errorFromEntry, VPNDetectionError } from './errors.js';
7
7
  import { OauthApi } from './oauth.js';
8
8
  import { isBatch, isBatchError, isEntitlement, isLookup } from './shapes.js';
9
- import { isCacheStore, storeScope, StoreTier } from './store.js';
9
+ import { isCacheStore, lifeMs, storeScope, StoreTier } from './store.js';
10
10
  import { asError, checkConcurrency, checkRetries, checkTimeout, deadline, readable, withRetry, } from './transport.js';
11
11
  import { toResult } from './types.js';
12
12
  export const DEFAULT_BASE_URL = 'https://api.vpndetection.io';
@@ -61,7 +61,11 @@ export class VPNDetection {
61
61
  // Concurrent misses for one address share one request: every
62
62
  // `fetch()` while it is in flight gets the same promise, and a
63
63
  // failure rejects them all and is cached for none.
64
- fetchMethod: (_ip, _stale, { context }) => context(),
64
+ fetchMethod: async (_ip, _stale, { context, options }) => {
65
+ const result = await context();
66
+ options.ttl = lifeMs(result, ttlMs);
67
+ return result;
68
+ },
65
69
  // An eviction must not fail the callers awaiting a request.
66
70
  ignoreFetchAbort: true,
67
71
  });
package/dist/oauth.d.ts CHANGED
@@ -63,9 +63,9 @@ export interface TokenResponse {
63
63
  * and PKCE, so an app or an extension can act for them. Reached through
64
64
  * `client.oauth`.
65
65
  *
66
- * Every call takes a client ID: one issued on request from
67
- * support@vpndetection.io, or the https URL of a client metadata document the app
68
- * serves. None of these requests carries the client's API key, and none needs one.
66
+ * Every call takes a client ID, the https URL of a client metadata document the
67
+ * app serves, which any app can use without asking us first. None of these
68
+ * requests carries the client's API key, and none needs one.
69
69
  */
70
70
  export declare class OauthApi {
71
71
  private readonly client;
package/dist/oauth.js CHANGED
@@ -6,9 +6,9 @@ import { checkTimeout, deadline, noResponse, withRetry } from './transport.js';
6
6
  * and PKCE, so an app or an extension can act for them. Reached through
7
7
  * `client.oauth`.
8
8
  *
9
- * Every call takes a client ID: one issued on request from
10
- * support@vpndetection.io, or the https URL of a client metadata document the app
11
- * serves. None of these requests carries the client's API key, and none needs one.
9
+ * Every call takes a client ID, the https URL of a client metadata document the
10
+ * app serves, which any app can use without asking us first. None of these
11
+ * requests carries the client's API key, and none needs one.
12
12
  */
13
13
  export class OauthApi {
14
14
  client;
package/dist/store.d.ts CHANGED
@@ -29,6 +29,12 @@ export declare class StoreTier {
29
29
  set(ip: string, result: Result): Promise<void>;
30
30
  private keyFor;
31
31
  }
32
+ /**
33
+ * How long an answer stays cached: the TTL, or at most a minute for one marked
34
+ * `trialExpired` or `capReached`, which a new UTC day or a raised cap ends. Still
35
+ * cached, so a busy page past its trial does not send every repeat to the API.
36
+ */
37
+ export declare function lifeMs(result: Result, ttlMs: number): number;
32
38
  /**
33
39
  * What tells one client's entries from another's: FNV-1a over the base URL and the
34
40
  * key, as 16 hex digits. It tells keys apart and keeps the key itself out of the
package/dist/store.js CHANGED
@@ -32,7 +32,7 @@ export class StoreTier {
32
32
  }
33
33
  async set(ip, result) {
34
34
  try {
35
- await this.store.set(this.keyFor(ip), result, this.ttlMs);
35
+ await this.store.set(this.keyFor(ip), result, lifeMs(result, this.ttlMs));
36
36
  }
37
37
  catch {
38
38
  // Costs a request on the next miss, nothing more.
@@ -42,6 +42,18 @@ export class StoreTier {
42
42
  return `${this.scope}:${ip}`;
43
43
  }
44
44
  }
45
+ /**
46
+ * How long an answer stays cached: the TTL, or at most a minute for one marked
47
+ * `trialExpired` or `capReached`, which a new UTC day or a raised cap ends. Still
48
+ * cached, so a busy page past its trial does not send every repeat to the API.
49
+ */
50
+ export function lifeMs(result, ttlMs) {
51
+ if (result.trialExpired === true || result.capReached === true) {
52
+ return Math.min(ttlMs, REDUCED_TTL_MS);
53
+ }
54
+ return ttlMs;
55
+ }
56
+ const REDUCED_TTL_MS = 60_000;
45
57
  const FNV_OFFSET = 0xcbf29ce484222325n;
46
58
  const FNV_PRIME = 0x100000001b3n;
47
59
  const MASK_64 = (1n << 64n) - 1n;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vpndetection-web",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "Official browser client library for the VPNDetection API. Detect VPNs, proxies, Tor, hosting and CDN IPs.",
5
5
  "type": "module",
6
6
  "license": "MIT",