@blaze-cache/axios 0.2.0-beta → 0.2.3-beta

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
@@ -10,9 +10,12 @@ support and IndexedDB persistence.
10
10
  ## Install
11
11
 
12
12
  ```sh
13
- npm install @blaze-cache/axios # axios ≥ 1.7 is a peer dependency
13
+ npm install @blaze-cache/axios@beta # axios ≥ 1.7 is a peer dependency
14
14
  ```
15
15
 
16
+ `@beta` is npm's dist-tag for the prerelease; drop it to install the last stable
17
+ release.
18
+
16
19
  ## Quick start
17
20
 
18
21
  ```ts
@@ -31,10 +34,23 @@ response.headers['x-blaze-cache']; // 'HIT' | 'MISS'
31
34
 
32
35
  ## Behaviour
33
36
 
34
- * **`Cache-Control` is honoured on both sides** — `max-age` overrides `ttlMs`;
35
- `no-store` (on the request or the response) bypasses the cache entirely.
37
+ * **`Cache-Control` is honoured on both sides** — `max-age` overrides `ttlMs` *minus the
38
+ response's `Age`* (a `max-age=60, Age: 50` response has ten seconds left, not sixty);
39
+ `no-store` (on the request or the response) bypasses the cache entirely, and `no-cache`
40
+ — the per-field `no-cache="set-cookie"` spelling included — plus `max-age=0` on the
41
+ request skip the read.
42
+ * **Freshness is enforced by the adapter, not by the store** — each record carries the
43
+ deadline it was written with, so even a store that never expires anything (a plain `Map`
44
+ shim satisfies the interface) still cannot replay a response past its freshness lifetime.
45
+ * **The key is the URL axios will actually request** — an absolute `url` ignores
46
+ `baseURL`, the join collapses slashes exactly as axios' `buildFullPath` does, and the
47
+ result is canonicalised through `new URL`. One endpoint, one entry.
36
48
  * **`Authorization` participates in the key** (hashed, never stored verbatim), so a
37
49
  personalized response is never replayed to a different user. Tune with `varyHeaders`.
50
+ * **The response's own `Vary` is honoured** — `Vary: *` is never stored (RFC 9111 §3), and
51
+ a response varying on a header you have not put in `varyHeaders` is left uncached rather
52
+ than replay a variant to the wrong caller. Server headers missing from `varyHeaders`?
53
+ That is the safe default working as intended — list them to opt in.
38
54
  * **A hit behaves like a network response** — the raw body is stored, so
39
55
  `transformResponse` runs on it exactly as it would on a miss, and it is replayed
40
56
  through the *current* request's `validateStatus` (a stored `404` still rejects).
@@ -46,6 +62,20 @@ response.headers['x-blaze-cache']; // 'HIT' | 'MISS'
46
62
  describe the body a GET *would* return.
47
63
  * Only responses that pass `validateStatus` are stored; by default the cache keeps
48
64
  `GET` requests answering `2xx`.
65
+ * **A cache that throws costs one extra request, never a failed response** — the read and
66
+ the write are both guarded, and the failure is reported once on the console instead of
67
+ turning a good network response into a rejected promise.
68
+ * **Cache activity is observable** — the adapter reads with `cache.get` and writes with
69
+ `cache.set`, so the shared cache's typed event system sees every request it serves:
70
+
71
+ ```ts
72
+ cache.on('hit', ({ key }) => console.log('replayed', key));
73
+ cache.on('miss', ({ key }) => console.log('fetched', key));
74
+ cache.on('evict', ({ key }) => console.log('dropped', key));
75
+ ```
76
+
77
+ `on` returns its own unsubscribe function; the full event table is in the
78
+ [repository README](https://github.com/blaze-cache/blaze-cache#events).
49
79
 
50
80
  ## Testing
51
81
 
@@ -54,7 +84,7 @@ pnpm --filter @blaze-cache/axios test # offline — canned adapter, no ne
54
84
  pnpm --filter @blaze-cache/axios test:live # live — real public APIs, needs the network
55
85
  ```
56
86
 
57
- The offline suite (14 tests) stubs the network so it can assert exact call counts and look
87
+ The offline suite (26 tests) stubs the network so it can assert exact call counts and look
58
88
  inside the store: payload, status, stored headers, TTL window, tags and key shape. The
59
89
  live suite (4 tests) runs against `api.github.com` and a real PNG, proving that the
60
90
  server's own `Cache-Control` is parsed, that a rejected `401` never overwrites a good
@@ -71,6 +101,7 @@ Setup, core concepts and the complete axios reference live in the
71
101
  ## Part of Blaze Cache
72
102
 
73
103
  * [`@blaze-cache/core`](https://www.npmjs.com/package/@blaze-cache/core) — the cache itself
104
+ * [`@blaze-cache/fetch`](https://www.npmjs.com/package/@blaze-cache/fetch) — drop-in `fetch()` replacement
74
105
  * [`@blaze-cache/react-query`](https://www.npmjs.com/package/@blaze-cache/react-query) — TanStack Query v5 mirror
75
106
 
76
107
  ## License
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,kBAAkB,EAGnB,MAAM,OAAO,CAAC;AAKf,OAAO,KAAK,EAGV,sBAAsB,EAEvB,MAAM,YAAY,CAAC;AAWpB;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,sBAAsB,GAAG,YAAY,CAkD1E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,aAAa,EACvB,OAAO,EAAE,wBAAwB,GAChC,IAAI,CAMN;AAED,0FAA0F;AAC1F,MAAM,MAAM,wBAAwB,GAAG,IAAI,CAAC,sBAAsB,EAAE,SAAS,CAAC,GAAG;IAC/E,OAAO,CAAC,EAAE,kBAAkB,CAAC,SAAS,CAAC,CAAC;CACzC,CAAC"}
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,kBAAkB,EAGnB,MAAM,OAAO,CAAC;AAKf,OAAO,KAAK,EAGV,sBAAsB,EAEvB,MAAM,YAAY,CAAC;AAWpB;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,sBAAsB,GAAG,YAAY,CA4F1E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAC/B,QAAQ,EAAE,aAAa,EACvB,OAAO,EAAE,wBAAwB,GAChC,IAAI,CAMN;AAED,0FAA0F;AAC1F,MAAM,MAAM,wBAAwB,GAAG,IAAI,CAAC,sBAAsB,EAAE,SAAS,CAAC,GAAG;IAC/E,OAAO,CAAC,EAAE,kBAAkB,CAAC,SAAS,CAAC,CAAC;CACzC,CAAC"}
package/dist/adapter.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AxiosError, getAdapter } from 'axios';
2
- import { parseCacheControl } from './cache-control.js';
2
+ import { parseAgeMs, parseCacheControl, varyIsStorable } from './cache-control.js';
3
3
  import { defaultCacheKey } from './key.js';
4
4
  import { readHeader, toAxiosHeaders, toPlainHeaders } from './headers.js';
5
5
  /** Freshness window when neither the option nor `Cache-Control` says otherwise. */
@@ -30,36 +30,76 @@ export function blazeAdapter(options) {
30
30
  return async function blazeCacheAdapter(config) {
31
31
  const requestControl = parseCacheControl(readHeader(config.headers, 'Cache-Control'));
32
32
  // `no-store`: neither read nor write. `no-cache`: may store, but must not serve blindly.
33
+ // `max-age=0`: the caller declares that nothing stored is fresh any more (RFC 9111
34
+ // §5.2.2.1) — and with no conditional requests available here, the only honest answer
35
+ // is to skip the read and go to the network.
33
36
  const skipWrite = requestControl.noStore;
34
- const skipRead = requestControl.noStore || requestControl.noCache;
37
+ const skipRead = requestControl.noStore || requestControl.noCache || requestControl.maxAgeMs === 0;
35
38
  const key = keyOf(config);
36
39
  if (!skipRead) {
37
- const record = cache.get(key);
38
- if (record) {
39
- return settle(cachedResponse(record, config, markerHeader), config);
40
+ let cached;
41
+ try {
42
+ const record = cache.get(key);
43
+ if (record && Date.now() <= record.freshUntil) {
44
+ cached = cachedResponse(record, config, markerHeader);
45
+ }
46
+ else if (record) {
47
+ // Past its own deadline — the store itself may never expire anything (the types
48
+ // bless a plain `Map` shim, which drops nothing), so the record is dropped here.
49
+ // Left alone it would linger as dead weight that nothing is allowed to serve.
50
+ cache.delete?.(key);
51
+ }
40
52
  }
53
+ catch (error) {
54
+ // A cache that throws must cost one extra request — never a failed response.
55
+ console.warn('[blaze-cache] reading from the cache failed — falling back to the network', error);
56
+ }
57
+ if (cached)
58
+ return settle(cached, config);
41
59
  }
42
60
  const response = await network(config);
43
61
  if (markerHeader) {
44
62
  response.headers = toAxiosHeaders(response.headers).set(markerHeader, MISS);
45
63
  }
46
- if (!skipWrite && shouldCache(response, config)) {
47
- const control = parseCacheControl(readHeader(response.headers, 'Cache-Control'));
48
- const ttlMs = control.maxAgeMs ?? resolveTtl(options.ttlMs, response, config);
49
- // `max-age=0` (or a non-positive TTL) means "do not keep this".
50
- if (!(control.noStore || control.noCache) && ttlMs > 0) {
51
- const method = (config.method ?? 'get').toLowerCase();
52
- const record = {
53
- status: response.status,
54
- statusText: response.statusText,
55
- headers: storedHeaders(response.headers, response.data),
56
- // Raw on purpose: axios runs `transformResponse` on replay, exactly as it would
57
- // for a fresh network response.
58
- data: response.data,
59
- storedAt: Date.now(),
60
- };
61
- const tags = options.tags?.(response, config) ?? ['axios', `${method} ${config.url ?? ''}`];
62
- cache.set(key, record, { ttlMs, tags });
64
+ if (!skipWrite) {
65
+ try {
66
+ // The response's own `Vary` gates the store: `Vary: *` is never cacheable, and a
67
+ // response varying on a header outside `varyHeaders` would be replayed to callers
68
+ // who differ in exactly that header — see `varyIsStorable` for why "unknown ⇒ do
69
+ // not store" is the only safe answer.
70
+ const storable = shouldCache(response, config) &&
71
+ varyIsStorable(readHeader(response.headers, 'Vary'), varyHeaders);
72
+ if (storable) {
73
+ const control = parseCacheControl(readHeader(response.headers, 'Cache-Control'));
74
+ // `Age` spends the origin's freshness budget before it reaches us: a response
75
+ // replayed with `max-age=60, Age: 50` has ten seconds of life left, not sixty.
76
+ const ageMs = parseAgeMs(readHeader(response.headers, 'Age'));
77
+ const ttlMs = (control.maxAgeMs ?? resolveTtl(options.ttlMs, response, config)) - ageMs;
78
+ // `max-age=0` (or a non-positive TTL) means "do not keep this" — and so does a
79
+ // response whose `Age` already carries it past its freshness lifetime: by the time
80
+ // it landed here there was nothing fresh left to store.
81
+ if (!(control.noStore || control.noCache) && ttlMs > 0) {
82
+ const method = (config.method ?? 'get').toLowerCase();
83
+ const storedAt = Date.now();
84
+ const record = {
85
+ status: response.status,
86
+ statusText: response.statusText,
87
+ headers: storedHeaders(response.headers, response.data),
88
+ // Raw on purpose: axios runs `transformResponse` on replay, exactly as it would
89
+ // for a fresh network response.
90
+ data: response.data,
91
+ storedAt,
92
+ freshUntil: storedAt + ttlMs,
93
+ };
94
+ const tags = options.tags?.(response, config) ?? ['axios', `${method} ${config.url ?? ''}`];
95
+ cache.set(key, record, { ttlMs, tags });
96
+ }
97
+ }
98
+ }
99
+ catch (error) {
100
+ // The network response is good; a broken cache layer (hooks or store) must not be
101
+ // able to turn it into a failed request. Serving it unstored costs one request.
102
+ console.warn('[blaze-cache] storing the response failed — it is returned unstored', error);
63
103
  }
64
104
  }
65
105
  return response;
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.js","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAS/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAQ1E,mFAAmF;AACnF,MAAM,cAAc,GAAG,MAAM,CAAC;AAE9B,6EAA6E;AAC7E,MAAM,oBAAoB,GAAsB,CAAC,eAAe,CAAC,CAAC;AAElE,MAAM,GAAG,GAAG,KAAK,CAAC;AAClB,MAAM,IAAI,GAAG,MAAM,CAAC;AAEpB;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,YAAY,CAAC,OAA+B;IAC1D,MAAM,KAAK,GAAwB,OAAO,CAAC,KAAK,CAAC;IACjD,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,oBAAoB,CAAC;IAChE,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC;IACjG,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,UAAU,CAAC,CAAC,KAAK,E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AAC;AACJ,CAAC"}
@@ -15,4 +15,34 @@ export interface CacheControl {
15
15
  * ```
16
16
  */
17
17
  export declare function parseCacheControl(value: string | undefined | null): CacheControl;
18
+ /**
19
+ * `Age: <seconds>` — how long an intermediate cache has already been holding the response.
20
+ *
21
+ * The freshness window shrinks by this much: a response replayed with `max-age=60, Age: 50`
22
+ * has ten seconds of life left, not sixty — treating it as new quietly serves it past the
23
+ * origin's deadline. Anything unparsable, negative or absent counts as zero: never invent
24
+ * an age we cannot measure, and never subtract one we made up.
25
+ *
26
+ * Mirrors `@blaze-cache/fetch`'s `parseAgeMs` on purpose — the two wrappers must agree on
27
+ * how old a shared response is.
28
+ */
29
+ export declare function parseAgeMs(value: string | undefined | null): number;
30
+ /**
31
+ * Whether a response carrying this `Vary` may be stored under a key built from `varyHeaders`.
32
+ *
33
+ * `Vary: *` is never cacheable (RFC 9111 §3): the response depends on details no stored key
34
+ * can capture, so there is nothing to key it *by*. For any other listed name the same rule
35
+ * applies one header at a time — if the response varies on a header that `varyHeaders` does
36
+ * not fold into the key, two requests differing *only* in that header collapse onto one
37
+ * entry, and the first response is replayed to the other (the classic cross-user
38
+ * `Cookie` / cross-locale `Accept-Language` poisoning).
39
+ *
40
+ * "Unknown ⇒ do not store" is the only safe answer, because the failure modes are
41
+ * asymmetric: a skipped store costs one extra request; a wrong hit hands one caller another
42
+ * caller's data. An absent (or empty) `Vary` varies on nothing and is storable.
43
+ *
44
+ * Names are compared case-insensitively — `Vary: Authorization` is the same header as the
45
+ * configured `authorization`.
46
+ */
47
+ export declare function varyIsStorable(responseVary: string | null | undefined, varyHeaders: readonly string[]): boolean;
18
48
  //# sourceMappingURL=cache-control.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cache-control.d.ts","sourceRoot":"","sources":["../src/cache-control.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,MAAM,WAAW,YAAY;IAC3B,qDAAqD;IACrD,OAAO,EAAE,OAAO,CAAC;IACjB,4DAA4D;IAC5D,OAAO,EAAE,OAAO,CAAC;IACjB,sDAAsD;IACtD,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;CAC9B;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,YAAY,CAkBhF"}
1
+ {"version":3,"file":"cache-control.d.ts","sourceRoot":"","sources":["../src/cache-control.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,MAAM,WAAW,YAAY;IAC3B,qDAAqD;IACrD,OAAO,EAAE,OAAO,CAAC;IACjB,4DAA4D;IAC5D,OAAO,EAAE,OAAO,CAAC;IACjB,sDAAsD;IACtD,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;CAC9B;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,YAAY,CAqBhF;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,MAAM,CAInE;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,cAAc,CAC5B,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EACvC,WAAW,EAAE,SAAS,MAAM,EAAE,GAC7B,OAAO,CAYT"}
@@ -14,7 +14,10 @@ export function parseCacheControl(value) {
14
14
  if (directive === 'no-store') {
15
15
  result.noStore = true;
16
16
  }
17
- else if (directive === 'no-cache') {
17
+ else if (directive === 'no-cache' || directive.startsWith('no-cache=')) {
18
+ // `no-cache="field1"` restricts *what* must be revalidated — still "never serve
19
+ // without revalidating", and this cache has no conditional requests, so it is as
20
+ // uncacheable here as a bare `no-cache`.
18
21
  result.noCache = true;
19
22
  }
20
23
  else if (directive.startsWith('max-age=')) {
@@ -26,4 +29,52 @@ export function parseCacheControl(value) {
26
29
  }
27
30
  return result;
28
31
  }
32
+ /**
33
+ * `Age: <seconds>` — how long an intermediate cache has already been holding the response.
34
+ *
35
+ * The freshness window shrinks by this much: a response replayed with `max-age=60, Age: 50`
36
+ * has ten seconds of life left, not sixty — treating it as new quietly serves it past the
37
+ * origin's deadline. Anything unparsable, negative or absent counts as zero: never invent
38
+ * an age we cannot measure, and never subtract one we made up.
39
+ *
40
+ * Mirrors `@blaze-cache/fetch`'s `parseAgeMs` on purpose — the two wrappers must agree on
41
+ * how old a shared response is.
42
+ */
43
+ export function parseAgeMs(value) {
44
+ if (!value)
45
+ return 0;
46
+ const seconds = Number(value.trim());
47
+ return Number.isFinite(seconds) && seconds >= 0 ? seconds * 1000 : 0;
48
+ }
49
+ /**
50
+ * Whether a response carrying this `Vary` may be stored under a key built from `varyHeaders`.
51
+ *
52
+ * `Vary: *` is never cacheable (RFC 9111 §3): the response depends on details no stored key
53
+ * can capture, so there is nothing to key it *by*. For any other listed name the same rule
54
+ * applies one header at a time — if the response varies on a header that `varyHeaders` does
55
+ * not fold into the key, two requests differing *only* in that header collapse onto one
56
+ * entry, and the first response is replayed to the other (the classic cross-user
57
+ * `Cookie` / cross-locale `Accept-Language` poisoning).
58
+ *
59
+ * "Unknown ⇒ do not store" is the only safe answer, because the failure modes are
60
+ * asymmetric: a skipped store costs one extra request; a wrong hit hands one caller another
61
+ * caller's data. An absent (or empty) `Vary` varies on nothing and is storable.
62
+ *
63
+ * Names are compared case-insensitively — `Vary: Authorization` is the same header as the
64
+ * configured `authorization`.
65
+ */
66
+ export function varyIsStorable(responseVary, varyHeaders) {
67
+ if (!responseVary)
68
+ return true;
69
+ const listed = responseVary
70
+ .split(',')
71
+ .map((name) => name.trim().toLowerCase())
72
+ .filter(Boolean);
73
+ if (listed.length === 0)
74
+ return true; // `Vary:` with no names asks for nothing
75
+ if (listed.includes('*'))
76
+ return false; // never cacheable, per RFC 9111 §3
77
+ const configured = new Set(varyHeaders.map((header) => header.trim().toLowerCase()));
78
+ return listed.every((name) => configured.has(name));
79
+ }
29
80
  //# sourceMappingURL=cache-control.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"cache-control.js","sourceRoot":"","sources":["../src/cache-control.ts"],"names":[],"mappings":"AAUA;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAgC;IAChE,MAAM,MAAM,GAAiB,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC;IACrF,IAAI,CAAC,KAAK;QAAE,OAAO,MAAM,CAAC;IAE1B,KAAK,MAAM,YAAY,IAAI,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QAC5C,MAAM,SAAS,GAAG,YAAY,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;QACpD,IAAI,SAAS,KAAK,UAAU,EAAE,CAAC;YAC7B,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC;QACxB,CAAC;aAAM,IAAI,SAAS,KAAK,UAAU,EAAE,CAAC;YACpC,MAAM,CAAC,OAAO,GAAG,IAAI,CAAC;QACxB,CAAC;aAAM,IAAI,SAAS,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;YAC5C,MAAM,OAAO,GAAG,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;YAC3D,IAAI,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,OAAO,IAAI,CAAC,EAAE,CAAC;gBAC7C,MAAM,CAAC,QAAQ,GAAG,OAAO,GAAG,IAAI,CAAC;YACnC,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
1
+ {"version":3,"file":"cache-control.js","sourceRoot":"","sources":["../src/cache-control.ts"],"names":[],"mappings":"AAUA;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAgC;IAChE,MAAM,MAAM,GAAiB,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAA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;;;;GAgBG;AACH,MAAM,UAAU,cAAc,CAC5B,YAAuC,EACvC,WAA8B;IAE9B,IAAI,CAAC,YAAY;QAAE,OAAO,IAAI,CAAC;IAE/B,MAAM,MAAM,GAAG,YAAY;SACxB,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;SACxC,MAAM,CAAC,OAAO,CAAC,CAAC;IACnB,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC,CAAC,yCAAyC;IAC/E,IAAI,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,OAAO,KAAK,CAAC,CAAC,mCAAmC;IAE3E,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC;IACrF,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;AACtD,CAAC"}
package/dist/headers.d.ts CHANGED
@@ -12,8 +12,22 @@ export declare function readHeader(source: unknown, name: string): string | unde
12
12
  * The `AxiosHeaders` constructor types are narrower than what adapters actually receive
13
13
  * (`AxiosResponseHeaders` is a union that does not overlap cleanly), so the cast lives here,
14
14
  * in one documented place, instead of at every call site.
15
+ *
16
+ * A legal `__proto__` header name needs care on the way through: axios' `set` writes
17
+ * headers with plain `target[key] = …` assignment, and for that one name that lands in
18
+ * `Object.prototype.__proto__`'s accessor — a string value is silently ignored (the header
19
+ * disappears, on the way into the store *and* on the way back out). So the name is peeled
20
+ * off first: the source is copied (spread defines own keys — it never calls a setter), the
21
+ * key is dropped from the copy so axios normalises every *other* header as usual, and the
22
+ * value is then defined on the result as a plain own property — which `get`, `toJSON` and
23
+ * plain iteration all see, without touching the instance's prototype or any global one.
15
24
  */
16
25
  export declare function toAxiosHeaders(headers: unknown): AxiosHeaders;
17
- /** Copies headers into a plain object suitable for storage. */
26
+ /**
27
+ * Copies headers into a plain object suitable for storage.
28
+ *
29
+ * The spread defines own keys — a stored `__proto__` header included (its repair happens in
30
+ * {@link toAxiosHeaders}), and no setter runs on the way.
31
+ */
18
32
  export declare function toPlainHeaders(headers: unknown): Record<string, unknown>;
19
33
  //# sourceMappingURL=headers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"headers.d.ts","sourceRoot":"","sources":["../src/headers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAErC;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAgB5E;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,YAAY,CAG7D;AAED,+DAA+D;AAC/D,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAExE"}
1
+ {"version":3,"file":"headers.d.ts","sourceRoot":"","sources":["../src/headers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAQrC;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAqB5E;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,YAAY,CAkB7D;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAExE"}
package/dist/headers.js CHANGED
@@ -1,4 +1,8 @@
1
1
  import { AxiosHeaders } from 'axios';
2
+ /** `hasOwnProperty` over an unknown source — also correct for `Object.create(null)` records. */
3
+ const hasOwn = (source, key) => source !== null &&
4
+ source !== undefined &&
5
+ Object.prototype.hasOwnProperty.call(source, key);
2
6
  /**
3
7
  * Case-insensitive header lookup that works with both `AxiosHeaders` and plain objects.
4
8
  *
@@ -16,8 +20,13 @@ export function readHeader(source, name) {
16
20
  const value = source.get(name);
17
21
  return value === null || value === undefined ? undefined : String(value);
18
22
  }
23
+ // Own keys only: a plain `{}` answers `record.__proto__` with `Object.prototype` and
24
+ // `record.constructor` with the constructor — neither is a header anyone sent, and
25
+ // stringifying either would invent a value straight out of the prototype chain.
19
26
  const record = source;
20
- const value = record[name] ?? record[name.toLowerCase()];
27
+ const exact = hasOwn(record, name) ? record[name] : undefined;
28
+ const lower = hasOwn(record, name.toLowerCase()) ? record[name.toLowerCase()] : undefined;
29
+ const value = exact ?? lower;
21
30
  return value === null || value === undefined ? undefined : String(value);
22
31
  }
23
32
  /**
@@ -26,13 +35,41 @@ export function readHeader(source, name) {
26
35
  * The `AxiosHeaders` constructor types are narrower than what adapters actually receive
27
36
  * (`AxiosResponseHeaders` is a union that does not overlap cleanly), so the cast lives here,
28
37
  * in one documented place, instead of at every call site.
38
+ *
39
+ * A legal `__proto__` header name needs care on the way through: axios' `set` writes
40
+ * headers with plain `target[key] = …` assignment, and for that one name that lands in
41
+ * `Object.prototype.__proto__`'s accessor — a string value is silently ignored (the header
42
+ * disappears, on the way into the store *and* on the way back out). So the name is peeled
43
+ * off first: the source is copied (spread defines own keys — it never calls a setter), the
44
+ * key is dropped from the copy so axios normalises every *other* header as usual, and the
45
+ * value is then defined on the result as a plain own property — which `get`, `toJSON` and
46
+ * plain iteration all see, without touching the instance's prototype or any global one.
29
47
  */
30
48
  export function toAxiosHeaders(headers) {
31
49
  if (headers instanceof AxiosHeaders)
32
50
  return headers;
33
- return AxiosHeaders.from(headers);
51
+ const source = headers;
52
+ const carriesProto = hasOwn(source, '__proto__');
53
+ const rest = carriesProto ? { ...source } : source;
54
+ if (carriesProto)
55
+ delete rest['__proto__'];
56
+ const target = AxiosHeaders.from(rest);
57
+ if (carriesProto) {
58
+ Object.defineProperty(target, '__proto__', {
59
+ value: source['__proto__'],
60
+ enumerable: true,
61
+ writable: true,
62
+ configurable: true,
63
+ });
64
+ }
65
+ return target;
34
66
  }
35
- /** Copies headers into a plain object suitable for storage. */
67
+ /**
68
+ * Copies headers into a plain object suitable for storage.
69
+ *
70
+ * The spread defines own keys — a stored `__proto__` header included (its repair happens in
71
+ * {@link toAxiosHeaders}), and no setter runs on the way.
72
+ */
36
73
  export function toPlainHeaders(headers) {
37
74
  return { ...toAxiosHeaders(headers) };
38
75
  }
@@ -1 +1 @@
1
- {"version":3,"file":"headers.js","sourceRoot":"","sources":["../src/headers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAErC;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAAC,MAAe,EAAE,IAAY;IACtD,IAAI,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAE9D,IAAI,MAAM,YAAY,YAAY,EAAE,CAAC;QACnC,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC/B,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC3E,CAAC;IAED,IAAI,OAAQ,MAA4B,CAAC,GAAG,KAAK,UAAU,EAAE,CAAC;QAC5D,MAAM,KAAK,GAAI,MAA6C,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACvE,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC3E,C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;IACnD,IAAI,YAAY;QAAE,OAAQ,IAAgC,CAAC,WAAW,CAAC,CAAC;IAExE,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,CAAC,IAA8B,CAAC,CAAC;IACjE,IAAI,YAAY,EAAE,CAAC;QACjB,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE;YACzC,KAAK,EAAE,MAAM,CAAC,WAAW,CAAC;YAC1B,UAAU,EAAE,IAAI;YAChB,QAAQ,EAAE,IAAI;YACd,YAAY,EAAE,IAAI;SACnB,CAAC,CAAC;IACL,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,OAAgB;IAC7C,OAAO,EAAE,GAAG,cAAc,CAAC,OAAO,CAAC,EAAE,CAAC;AACxC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -16,7 +16,7 @@
16
16
  */
17
17
  export { blazeAdapter, installBlazeCache } from './adapter.js';
18
18
  export type { InstallBlazeCacheOptions } from './adapter.js';
19
- export { parseCacheControl } from './cache-control.js';
19
+ export { parseAgeMs, parseCacheControl, varyIsStorable } from './cache-control.js';
20
20
  export type { CacheControl } from './cache-control.js';
21
21
  export { defaultCacheKey, fnv1a } from './key.js';
22
22
  export type { AxiosCacheRecord, BlazeAxiosCacheLike, BlazeAxiosCacheOptions, CachedAxiosResponse, } from './types.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAC/D,YAAY,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAE7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAElD,YAAY,EACV,gBAAgB,EAChB,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAC/D,YAAY,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAE7D,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACnF,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAElD,YAAY,EACV,gBAAgB,EAChB,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -15,6 +15,6 @@
15
15
  * @packageDocumentation
16
16
  */
17
17
  export { blazeAdapter, installBlazeCache } from './adapter.js';
18
- export { parseCacheControl } from './cache-control.js';
18
+ export { parseAgeMs, parseCacheControl, varyIsStorable } from './cache-control.js';
19
19
  export { defaultCacheKey, fnv1a } from './key.js';
20
20
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAG/D,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAGvD,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAG/D,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAGnF,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC"}
package/dist/key.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"key.d.ts","sourceRoot":"","sources":["../src/key.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,OAAO,CAAC;AAGxD;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAO3C;AAED,yFAAyF;AACzF,wBAAgB,eAAe,CAC7B,MAAM,EAAE,0BAA0B,EAClC,WAAW,EAAE,SAAS,MAAM,EAAE,GAC7B,MAAM,CASR"}
1
+ {"version":3,"file":"key.d.ts","sourceRoot":"","sources":["../src/key.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,OAAO,CAAC;AAGxD;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAO3C;AA6ED,yFAAyF;AACzF,wBAAgB,eAAe,CAC7B,MAAM,EAAE,0BAA0B,EAClC,WAAW,EAAE,SAAS,MAAM,EAAE,GAC7B,MAAM,CAcR"}
package/dist/key.js CHANGED
@@ -1,4 +1,3 @@
1
- import { safeStableStringify } from '@blaze-cache/core';
2
1
  import { readHeader } from './headers.js';
3
2
  /**
4
3
  * FNV-1a, 32-bit.
@@ -15,13 +14,88 @@ export function fnv1a(input) {
15
14
  }
16
15
  return hash.toString(36);
17
16
  }
17
+ /** axios' own `isAbsoluteURL` test — a scheme or `//` makes the url absolute, ignoring `baseURL`. */
18
+ const ABSOLUTE_URL = /^([a-z][a-z\d+\-.]*:)?\/\//i;
19
+ /**
20
+ * The URL axios will actually request — this is the endpoint identity of the key.
21
+ *
22
+ * The historical `baseURL + url` concatenation was wrong twice over. It never matched
23
+ * axios' `buildFullPath`: `…/v1/` + `/users` keyed `…/v1//users`, a string no adapter ever
24
+ * requests — and a *different* key than `…/v1` + `/users`, which requests the very same
25
+ * resource, so one endpoint occupied two entries. Worse, the concatenation collides across
26
+ * endpoints: `…/v1` + `s/users` and `…/v` + `1s/users` produce one identical key for two
27
+ * different resources — a cross-endpoint replay.
28
+ *
29
+ * So the join mirrors `buildFullPath`/`combineURLs`: an already-absolute url ignores the
30
+ * base, and trailing slashes on the base plus leading slashes on the url collapse into
31
+ * exactly one `/`. The result is then canonicalised through `new URL` whenever it can be
32
+ * made absolute (host case, dot-segments, encoding). A candidate that is not absolute-able
33
+ * — a relative url with no base, a relative `baseURL` — falls back to that plain join,
34
+ * which is still precisely the string axios would request; only the no-base case reduces
35
+ * to the raw url itself.
36
+ */
37
+ function resolveRequestUrl(config) {
38
+ const baseURL = config.baseURL;
39
+ const url = config.url ?? '';
40
+ const joined = baseURL && !ABSOLUTE_URL.test(url)
41
+ ? url
42
+ ? `${baseURL.replace(/\/+$/, '')}/${url.replace(/^\/+/, '')}`
43
+ : baseURL
44
+ : url;
45
+ try {
46
+ return new URL(joined).href;
47
+ }
48
+ catch {
49
+ return joined;
50
+ }
51
+ }
52
+ /**
53
+ * `safeStableStringify` from `@blaze-cache/core`, re-implemented for one reason: sorted keys
54
+ * are copied with `defineProperty` instead of plain assignment.
55
+ *
56
+ * On a literal `{}`, `sorted[key] = value` with `key === '__proto__'` walks into
57
+ * `Object.prototype.__proto__`'s accessor instead of creating a property — the key silently
58
+ * vanishes, and `params: { ['__proto__']: … }` would hash to the same entry as `params: {}`,
59
+ * replaying one query's response for the other. `defineProperty` defines an own key on the
60
+ * fresh `sorted` object and touches nothing else, so no prototype — least of all a global
61
+ * one — is polluted. Everything else mirrors the core original: canonical key order, values
62
+ * through `toJSON`, circular structures still throw, and a throw still degrades into
63
+ * "no params in the key" instead of a failed request.
64
+ */
65
+ function stringifyParams(value) {
66
+ if (value === undefined)
67
+ return 'undefined';
68
+ try {
69
+ const json = JSON.stringify(value, (_key, raw) => {
70
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw))
71
+ return raw;
72
+ const source = raw;
73
+ const sorted = {};
74
+ for (const key of Object.keys(source).sort()) {
75
+ Object.defineProperty(sorted, key, {
76
+ value: source[key],
77
+ enumerable: true,
78
+ writable: true,
79
+ configurable: true,
80
+ });
81
+ }
82
+ return sorted;
83
+ });
84
+ return json ?? 'null';
85
+ }
86
+ catch {
87
+ return undefined;
88
+ }
89
+ }
18
90
  /** Default cache key: method + URL, plus a hash of the params and the `vary` headers. */
19
91
  export function defaultCacheKey(config, varyHeaders) {
20
92
  const method = (config.method ?? 'get').toLowerCase();
21
- const url = `${config.baseURL ?? ''}${config.url ?? ''}`;
93
+ const url = resolveRequestUrl(config);
22
94
  // `params` is stringified canonically, so `?a=1&b=2` and `?b=2&a=1` share one key.
23
- const params = safeStableStringify(config.params) ?? 'undefined';
24
- const vary = varyHeaders.map((header) => readHeader(config.headers, header) ?? '').join(' ');
95
+ const params = stringifyParams(config.params) ?? 'undefined';
96
+ // JSON-quoted, never `' '`-joined: a space join would let `[a='x', b='y z']` and
97
+ // `[a='x y', b='z']` hash alike although they describe different requests.
98
+ const vary = JSON.stringify(varyHeaders.map((header) => readHeader(config.headers, header) ?? ''));
25
99
  return `${method} ${url}?${fnv1a(`${params} ${vary}`)}`;
26
100
  }
27
101
  //# sourceMappingURL=key.js.map
package/dist/key.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"key.js","sourceRoot":"","sources":["../src/key.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C;;;;;;GAMG;AACH,MAAM,UAAU,KAAK,CAAC,KAAa;IACjC,IAAI,IAAI,GAAG,UAAU,CAAC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AE,CAAC;AAC1D,CAAC"}
package/dist/types.d.ts CHANGED
@@ -15,6 +15,14 @@ export interface AxiosCacheRecord {
15
15
  data: unknown;
16
16
  /** `Date.now()` when the response was stored. */
17
17
  storedAt: number;
18
+ /**
19
+ * `Date.now()` at which this record stops being fresh.
20
+ *
21
+ * The read path checks this itself instead of trusting the store: {@link BlazeAxiosCacheLike}
22
+ * is deliberately structural — a plain `Map` shim qualifies — and such a store never
23
+ * expires anything, so without this deadline a record would be replayed forever.
24
+ */
25
+ freshUntil: number;
18
26
  }
19
27
  /**
20
28
  * An {@link AxiosResponse} served by the cache.
@@ -76,6 +84,13 @@ export interface BlazeAxiosCacheLike {
76
84
  ttlMs?: number;
77
85
  tags?: readonly string[];
78
86
  }): void;
87
+ /**
88
+ * Optional, so a minimal `Map` shim still satisfies the interface.
89
+ *
90
+ * When present, the adapter uses it to drop a record it found past {@link AxiosCacheRecord.freshUntil} —
91
+ * without it a non-expiring store would keep dead weight it is never allowed to serve.
92
+ */
93
+ delete?(key: string): boolean | void;
79
94
  }
80
95
  export type { AxiosAdapter, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig };
81
96
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,aAAa,EAAE,0BAA0B,EAAE,MAAM,OAAO,CAAC;AAEzG;;;;;;GAMG;AACH,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,iDAAiD;IACjD,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,wCAAwC;IACxC,IAAI,EAAE,OAAO,CAAC;IACd,iDAAiD;IACjD,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;;;GAKG;AACH,MAAM,WAAW,mBAAmB,CA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,MAAM,EAAE,0BAA0B,KAAK,MAAM,EAAE,CAAC;IAEjF;;;;;OAKG;IACH,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAEhC,+FAA+F;IAC/F,YAAY,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;CAC/B;AAED,8EAA8E;AAC9E,MAAM,WAAW,mBAAmB;IAClC,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS,CAAC;IAC/C,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,gBAAgB,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;KAAE,GAAG,IAAI,CAAC;IACxG;;;;;OAKG;IACH,MAAM,CAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;CACtC;AAED,YAAY,EAAE,YAAY,EAAE,kBAAkB,EAAE,aAAa,EAAE,0BAA0B,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaze-cache/axios",
3
- "version": "0.2.0-beta",
3
+ "version": "0.2.3-beta",
4
4
  "description": "Cache HTTP responses in axios via a WebAssembly cache — TTLs, tags, Cache-Control support and IndexedDB persistence",
5
5
  "keywords": [
6
6
  "axios",
@@ -38,7 +38,7 @@
38
38
  "dist"
39
39
  ],
40
40
  "dependencies": {
41
- "@blaze-cache/core": "0.2.0-beta"
41
+ "@blaze-cache/core": "0.2.3-beta"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "axios": "^1.7.0"