internetdata-web 1.3.0 → 1.3.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
@@ -141,6 +141,8 @@ const client = new InternetData({ cache: { max: 50_000, ttlMs: 6 * 60 * 60 * 100
141
141
  const clientNoCache = new InternetData({ cache: false });
142
142
  ```
143
143
 
144
+ 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.
145
+
144
146
  ### Keeping answers across a reload
145
147
 
146
148
  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:
@@ -253,7 +255,7 @@ const token = await client.oauth.exchangeAuthorizationCode('your-client-id', cod
253
255
 
254
256
  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.
255
257
 
256
- `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. The sign-in is at `https://internetdata.io`, the apex of the API's host. Client IDs are issued on request from support@internetdata.io.
258
+ `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. The sign-in is at `https://internetdata.io`, the apex of the API's host. 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.
257
259
 
258
260
  ### Browser support
259
261
 
package/dist/client.d.ts CHANGED
@@ -7,7 +7,10 @@ export declare const DEFAULT_BASE_URL = "https://api.internetdata.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, isAddress, isBogon, unmapped } from './bogon.js';
6
6
  import { errorFromEntry, InternetDataError } from './errors.js';
7
7
  import { OauthApi } from './oauth.js';
8
8
  import { isAsnLookup, isBatch, isBatchError, isDomainLookup, 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 { toAsnResult, toDomainResult, toResult, } from './types.js';
12
12
  export const DEFAULT_BASE_URL = 'https://api.internetdata.io';
@@ -63,7 +63,11 @@ export class InternetData {
63
63
  // Concurrent misses for one address share one request: every
64
64
  // `fetch()` while it is in flight gets the same promise, and a
65
65
  // failure rejects them all and is cached for none.
66
- fetchMethod: (_ip, _stale, { context }) => context(),
66
+ fetchMethod: async (_ip, _stale, { context, options }) => {
67
+ const result = await context();
68
+ options.ttl = lifeMs(result, ttlMs);
69
+ return result;
70
+ },
67
71
  // An eviction must not fail the callers awaiting a request.
68
72
  ignoreFetchAbort: true,
69
73
  });
package/dist/oauth.d.ts CHANGED
@@ -68,9 +68,8 @@ export interface TokenResponse {
68
68
  * authorization server is: `https://api.internetdata.io` signs in at
69
69
  * `https://internetdata.io`.
70
70
  *
71
- * Every call takes a client ID: one issued on request from
72
- * support@internetdata.io, or the https URL of a client metadata document the app
73
- * serves. None of these requests carries the client's API key, and none needs one.
71
+ * Every call takes a client ID, the https URL of a client metadata document the app
72
+ * serves, which any app can use without asking us first. None of these requests carries the client's API key, and none needs one.
74
73
  */
75
74
  export declare class OauthApi {
76
75
  private readonly client;
package/dist/oauth.js CHANGED
@@ -8,9 +8,8 @@ import { checkTimeout, deadline, noResponse, withRetry } from './transport.js';
8
8
  * authorization server is: `https://api.internetdata.io` signs in at
9
9
  * `https://internetdata.io`.
10
10
  *
11
- * Every call takes a client ID: one issued on request from
12
- * support@internetdata.io, or the https URL of a client metadata document the app
13
- * serves. None of these requests carries the client's API key, and none needs one.
11
+ * Every call takes a client ID, the https URL of a client metadata document the app
12
+ * serves, which any app can use without asking us first. None of these requests carries the client's API key, and none needs one.
14
13
  */
15
14
  export class OauthApi {
16
15
  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,7 +1,7 @@
1
1
  {
2
2
  "name": "internetdata-web",
3
- "version": "1.3.0",
4
- "description": "Official browser client library for the InternetData API. Look up IP addresses, ASes and domains from a page or a browser extension.",
3
+ "version": "1.3.2",
4
+ "description": "Official browser client library for the InternetData API. Look up IP addresses, ASNs and domains from a page or a browser extension.",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "homepage": "https://internetdata.io",