@blaze-cache/axios 0.3.0 → 0.4.0-beta.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/README.md +74 -12
- package/dist/adapter.d.ts +2 -2
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +115 -76
- package/dist/adapter.js.map +1 -1
- package/dist/cache-control.d.ts +2 -0
- package/dist/cache-control.d.ts.map +1 -1
- package/dist/cache-control.js +6 -0
- package/dist/cache-control.js.map +1 -1
- package/dist/index.d.ts +9 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -1
- package/dist/index.js.map +1 -1
- package/dist/key.d.ts +23 -1
- package/dist/key.d.ts.map +1 -1
- package/dist/key.js +60 -2
- package/dist/key.js.map +1 -1
- package/dist/types.d.ts +94 -7
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -20,27 +20,75 @@ instead of this stable release.
|
|
|
20
20
|
|
|
21
21
|
```ts
|
|
22
22
|
import axios from 'axios';
|
|
23
|
+
import { createCache } from '@blaze-cache/core';
|
|
23
24
|
import { installBlazeCache } from '@blaze-cache/axios';
|
|
24
|
-
import { cache } from './blaze.js'; // a cache created by @blaze-cache/core
|
|
25
25
|
|
|
26
|
+
const cache = await createCache({ capacity: 1000 });
|
|
26
27
|
const api = axios.create({ baseURL: '/api' });
|
|
27
28
|
|
|
28
|
-
installBlazeCache(api, {
|
|
29
|
+
installBlazeCache(api, {
|
|
30
|
+
cache,
|
|
31
|
+
ttlMs: 30_000, // default
|
|
32
|
+
methods: ['GET', 'HEAD'], // default
|
|
33
|
+
honourCacheControl: true, // default (cacheControl is an alias)
|
|
34
|
+
varyHeaders: ['Authorization'], // default
|
|
35
|
+
});
|
|
29
36
|
|
|
37
|
+
// Cached GET
|
|
30
38
|
const response = await api.get('/users/42');
|
|
31
|
-
response.fromBlazeCache; //
|
|
39
|
+
response.fromBlazeCache; // false on MISS, true on HIT
|
|
32
40
|
response.headers['x-blaze-cache']; // 'HIT' | 'MISS'
|
|
41
|
+
|
|
42
|
+
// Bypass cache for a single request
|
|
43
|
+
await api.get('/users/42', { blazeCache: false });
|
|
44
|
+
|
|
45
|
+
// Custom TTL/tags (and optional key override) per request
|
|
46
|
+
await api.get('/users/1', {
|
|
47
|
+
blazeCache: {
|
|
48
|
+
enabled: true,
|
|
49
|
+
ttlMs: 30_000,
|
|
50
|
+
tags: ['user', 'user:1'],
|
|
51
|
+
key: 'user:1', // optional string override
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Invalidate after a mutation
|
|
56
|
+
await api.post('/todos', body, {
|
|
57
|
+
blazeCache: {
|
|
58
|
+
invalidateTags: ['todos'],
|
|
59
|
+
},
|
|
60
|
+
});
|
|
33
61
|
```
|
|
34
62
|
|
|
63
|
+
## Per-request options
|
|
64
|
+
|
|
65
|
+
Pass `blazeCache` on any request config (types are augmented on `axios`):
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
await api.get('/users/1', {
|
|
69
|
+
blazeCache: {
|
|
70
|
+
enabled: true, // false bypasses read/write for this request
|
|
71
|
+
ttlMs: 30_000, // overrides adapter ttlMs (number or fn)
|
|
72
|
+
tags: (res, config) => ['user', 'user:1'],
|
|
73
|
+
key: 'user:1', // string or (config) => string
|
|
74
|
+
varyHeaders: ['Authorization'],
|
|
75
|
+
forceRefresh: true, // bypass read, store fresh response
|
|
76
|
+
invalidateTags: ['todos'], // invalidate after the network response
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
`blazeCache: false` is a shorthand to bypass caching for that request.
|
|
82
|
+
|
|
35
83
|
## Behaviour
|
|
36
84
|
|
|
37
|
-
* **`Cache-Control` is honoured on both sides**
|
|
38
|
-
|
|
39
|
-
`
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
85
|
+
* **`Cache-Control` is honoured on both sides** when `honourCacheControl` is on (default;
|
|
86
|
+
`cacheControl` is an alias) — `max-age` overrides `ttlMs` *minus the response's `Age`*
|
|
87
|
+
(a `max-age=60, Age: 50` response has ten seconds left, not sixty); `no-store` (on the
|
|
88
|
+
request or the response) bypasses the cache entirely, and `no-cache` — the per-field
|
|
89
|
+
`no-cache="set-cookie"` spelling included — plus `max-age=0` on the request skip the
|
|
90
|
+
read. Malformed directives are ignored rather than guessed at: an empty `max-age=` reads
|
|
91
|
+
as *no directive*, not as zero, and a duplicated directive keeps its first value (RFC 9111 §5.2.3).
|
|
44
92
|
* **Freshness is enforced by the adapter, not by the store** — each record carries the
|
|
45
93
|
deadline it was written with, so even a store that never expires anything (a plain `Map`
|
|
46
94
|
shim satisfies the interface) still cannot replay a response past its freshness lifetime.
|
|
@@ -68,6 +116,11 @@ response.headers['x-blaze-cache']; // 'HIT' | 'MISS'
|
|
|
68
116
|
one key. Each is now flattened into a stable string of its actual entries: order
|
|
69
117
|
preserved for `URLSearchParams` (axios sends it verbatim), order ignored for
|
|
70
118
|
`FormData` parts and `Map` keys.
|
|
119
|
+
* **A request body folds into the key too** — an opted-in `POST` with two different
|
|
120
|
+
payloads occupies two entries instead of replaying one response for the other. A body
|
|
121
|
+
the key cannot read (raw bytes, a stream, anything serialising to the bare `'{}'`)
|
|
122
|
+
leaves that request uncached rather than guessed at — the same "unknown ⇒ do not store"
|
|
123
|
+
rule `Vary` follows — unless a `key` callback describes it.
|
|
71
124
|
* **Replayed headers describe the stored body, not the wire** — axios decodes the body
|
|
72
125
|
for you (and drops `content-encoding`), so `transfer-encoding` goes too and
|
|
73
126
|
`content-length` is corrected to the payload's real size. A live request answered with
|
|
@@ -75,7 +128,16 @@ response.headers['x-blaze-cache']; // 'HIT' | 'MISS'
|
|
|
75
128
|
contradicts `data.byteLength`. Bodyless responses (HEAD) keep theirs: there the headers
|
|
76
129
|
describe the body a GET *would* return.
|
|
77
130
|
* Only responses that pass `validateStatus` are stored; by default the cache keeps
|
|
78
|
-
`GET` requests answering `2xx
|
|
131
|
+
`GET` and `HEAD` requests answering `2xx` (`methods: ['GET', 'HEAD']`). A method you
|
|
132
|
+
cache beyond those is stored only when its body can be folded into the key (or a `key`
|
|
133
|
+
callback describes it).
|
|
134
|
+
* **HIT/MISS observability** — `response.fromBlazeCache` is `true` on cache hits and
|
|
135
|
+
`false` on network responses, and the `x-blaze-cache` header is set to `HIT`/`MISS`
|
|
136
|
+
(configurable via `markerHeader`, or `false` to disable). Core cache `hit`/`miss`/`set`
|
|
137
|
+
events fire when using `@blaze-cache/core`.
|
|
138
|
+
* **Mutation invalidation** — `blazeCache.invalidateTags` invalidates tags on a cache that
|
|
139
|
+
supports it (e.g. `BlazeCache`) after the network response, so `POST`/`PUT`/`PATCH`/`DELETE`
|
|
140
|
+
can bust related GET caches. The `invalidateCacheTags(cache, ...tags)` helper is also exported.
|
|
79
141
|
* **A cache that throws costs one extra request, never a failed response** — the read and
|
|
80
142
|
the write are both guarded, and the failure is reported once on the console instead of
|
|
81
143
|
turning a good network response into a rejected promise.
|
|
@@ -98,7 +160,7 @@ pnpm --filter @blaze-cache/axios test # offline — canned adapter, no ne
|
|
|
98
160
|
pnpm --filter @blaze-cache/axios test:live # live — real public APIs, needs the network
|
|
99
161
|
```
|
|
100
162
|
|
|
101
|
-
The offline suite
|
|
163
|
+
The offline suite stubs the network so it can assert exact call counts and look
|
|
102
164
|
inside the store: payload, status, stored headers, TTL window, tags and key shape. The
|
|
103
165
|
live suite (4 tests) runs against `api.github.com` and a real PNG, proving that the
|
|
104
166
|
server's own `Cache-Control` is parsed, that a rejected `401` never overwrites a good
|
package/dist/adapter.d.ts
CHANGED
|
@@ -19,8 +19,8 @@ export declare function blazeAdapter(options: BlazeAxiosCacheOptions): AxiosAdap
|
|
|
19
19
|
* ```ts
|
|
20
20
|
* import { installBlazeCache } from '@blaze-cache/axios';
|
|
21
21
|
*
|
|
22
|
-
* installBlazeCache(api, { cache });
|
|
23
|
-
* installBlazeCache(api, { cache, ttlMs:
|
|
22
|
+
* installBlazeCache(api, { cache }); // GET/HEAD, 30s, Cache-Control aware
|
|
23
|
+
* installBlazeCache(api, { cache, ttlMs: 30_000, methods: ['GET'], varyHeaders: ['Authorization'] });
|
|
24
24
|
* ```
|
|
25
25
|
*
|
|
26
26
|
* The adapter currently in `instance.defaults` is captured **before** it is replaced, so the
|
package/dist/adapter.d.ts.map
CHANGED
|
@@ -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,
|
|
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,EAGvB,MAAM,YAAY,CAAC;AAqCpB;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,sBAAsB,GAAG,YAAY,CAuJ1E;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,11 +1,13 @@
|
|
|
1
1
|
import { AxiosError, getAdapter } from 'axios';
|
|
2
|
-
import { parseAgeMs, parseCacheControl, varyIsStorable } from './cache-control.js';
|
|
3
|
-
import { defaultCacheKey, resolveRequestUrl } from './key.js';
|
|
2
|
+
import { NO_DIRECTIVES, parseAgeMs, parseCacheControl, varyIsStorable } from './cache-control.js';
|
|
3
|
+
import { defaultCacheKey, encodeRequestBody, resolveRequestUrl } 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. */
|
|
6
|
-
const DEFAULT_TTL_MS =
|
|
6
|
+
const DEFAULT_TTL_MS = 30_000;
|
|
7
|
+
/** Only these methods are cached unless the caller says otherwise. */
|
|
8
|
+
const DEFAULT_METHODS = ['GET', 'HEAD'];
|
|
7
9
|
/** Headers whose values split the cache unless the caller says otherwise. */
|
|
8
|
-
const DEFAULT_VARY_HEADERS = ['
|
|
10
|
+
const DEFAULT_VARY_HEADERS = ['Authorization'];
|
|
9
11
|
const HIT = 'HIT';
|
|
10
12
|
const MISS = 'MISS';
|
|
11
13
|
/**
|
|
@@ -18,7 +20,16 @@ const MISS = 'MISS';
|
|
|
18
20
|
* single-integration cache this is always true; the check exists for the shared one.
|
|
19
21
|
*/
|
|
20
22
|
function isAxiosRecord(value) {
|
|
21
|
-
return typeof value
|
|
23
|
+
return typeof value?.freshUntil === 'number' && 'data' in value;
|
|
24
|
+
}
|
|
25
|
+
function toUpperMethods(methods) {
|
|
26
|
+
return new Set((methods ?? DEFAULT_METHODS).map((m) => m.toUpperCase()));
|
|
27
|
+
}
|
|
28
|
+
function getRequestOptions(config) {
|
|
29
|
+
const raw = config.blazeCache;
|
|
30
|
+
if (raw === false)
|
|
31
|
+
return { enabled: false };
|
|
32
|
+
return raw;
|
|
22
33
|
}
|
|
23
34
|
/**
|
|
24
35
|
* Wraps an axios adapter with a Blaze Cache layer.
|
|
@@ -34,40 +45,95 @@ function isAxiosRecord(value) {
|
|
|
34
45
|
*/
|
|
35
46
|
export function blazeAdapter(options) {
|
|
36
47
|
const cache = options.cache;
|
|
37
|
-
const
|
|
48
|
+
const globalMethods = toUpperMethods(options.methods);
|
|
49
|
+
const globalVaryHeaders = options.varyHeaders ?? DEFAULT_VARY_HEADERS;
|
|
38
50
|
const markerHeader = options.markerHeader === undefined ? 'x-blaze-cache' : options.markerHeader;
|
|
51
|
+
const honourCacheControl = options.honourCacheControl ?? options.cacheControl ?? true;
|
|
39
52
|
const network = options.adapter ?? getAdapter(['xhr', 'http', 'fetch']);
|
|
40
|
-
const keyOf = (config) => options.key ? options.key(config) : defaultCacheKey(config, varyHeaders);
|
|
41
|
-
const shouldCache = options.shouldCache ?? defaultShouldCache;
|
|
42
53
|
return async function blazeCacheAdapter(config) {
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
//
|
|
54
|
+
const reqOpts = getRequestOptions(config);
|
|
55
|
+
const method = (config.method ?? 'get').toUpperCase();
|
|
56
|
+
const enabled = reqOpts?.enabled ?? true;
|
|
57
|
+
const methods = globalMethods;
|
|
58
|
+
// Per-request `enabled: true` can opt a method in; `enabled: false` opts out entirely.
|
|
59
|
+
const methodAllowed = methods.has(method) || reqOpts?.enabled === true;
|
|
60
|
+
// A body that cannot be folded into the key would make every request carrying one
|
|
61
|
+
// share a single entry — `POST /orders` with two payloads would occupy one entry and
|
|
62
|
+
// the first response would be replayed for the second. Refusing to cache costs one
|
|
63
|
+
// extra request; a wrong hit hands one caller another caller's response, so the
|
|
64
|
+
// asymmetry decides it. A caller-supplied `key` owns the whole key, so it is trusted
|
|
65
|
+
// to describe the body itself.
|
|
66
|
+
const ownsKey = typeof options.key === 'function' ||
|
|
67
|
+
typeof reqOpts?.key === 'string' ||
|
|
68
|
+
typeof reqOpts?.key === 'function';
|
|
69
|
+
const bodyKeyed = config.data === undefined ||
|
|
70
|
+
config.data === null ||
|
|
71
|
+
ownsKey ||
|
|
72
|
+
encodeRequestBody(config.data) !== undefined;
|
|
73
|
+
const cacheable = enabled && methodAllowed && config.responseType !== 'stream' && bodyKeyed;
|
|
74
|
+
const varyHeaders = reqOpts?.varyHeaders ?? globalVaryHeaders;
|
|
75
|
+
const keyOf = () => {
|
|
76
|
+
if (typeof reqOpts?.key === 'string')
|
|
77
|
+
return reqOpts.key;
|
|
78
|
+
if (typeof reqOpts?.key === 'function')
|
|
79
|
+
return reqOpts.key(config);
|
|
80
|
+
return options.key ? options.key(config) : defaultCacheKey(config, varyHeaders);
|
|
81
|
+
};
|
|
82
|
+
const shouldCache = options.shouldCache ?? ((response) => response.status >= 200 && response.status < 300);
|
|
83
|
+
const tagsFor = options.tags;
|
|
84
|
+
// --- mutation/side-effect invalidation (runs after network, even if not cacheable) ---
|
|
85
|
+
const invalidateTags = (tags) => {
|
|
86
|
+
if (!tags || tags.length === 0)
|
|
87
|
+
return;
|
|
88
|
+
if (typeof cache.invalidateTags !== 'function')
|
|
89
|
+
return;
|
|
90
|
+
try {
|
|
91
|
+
cache.invalidateTags(...tags);
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
console.warn('[blaze-cache] tag invalidation failed', error);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
if (!cacheable) {
|
|
98
|
+
const response = await network(config);
|
|
99
|
+
if (markerHeader) {
|
|
100
|
+
response.headers = toAxiosHeaders(response.headers).set(markerHeader, MISS);
|
|
101
|
+
}
|
|
102
|
+
response.fromBlazeCache = false;
|
|
103
|
+
invalidateTags(reqOpts?.invalidateTags);
|
|
104
|
+
return response;
|
|
105
|
+
}
|
|
106
|
+
const requestControl = honourCacheControl
|
|
107
|
+
? parseCacheControl(readHeader(config.headers, 'Cache-Control'))
|
|
108
|
+
: NO_DIRECTIVES;
|
|
109
|
+
// `no-store`: neither read nor write. `no-cache` / `max-age=0`: skip read (revalidate intent).
|
|
48
110
|
const skipWrite = requestControl.noStore;
|
|
49
|
-
const skipRead = requestControl.noStore || requestControl.noCache || requestControl.maxAgeMs === 0;
|
|
50
|
-
|
|
51
|
-
if (!skipRead) {
|
|
111
|
+
const skipRead = requestControl.noStore || requestControl.noCache || requestControl.maxAgeMs === 0 || reqOpts?.forceRefresh === true;
|
|
112
|
+
let key;
|
|
113
|
+
if (!skipRead || !skipWrite) {
|
|
114
|
+
try {
|
|
115
|
+
key = keyOf();
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
console.warn('[blaze-cache] cache key generation failed — falling back to the network', error);
|
|
119
|
+
key = undefined;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (!skipRead && key !== undefined) {
|
|
52
123
|
let cached;
|
|
53
124
|
try {
|
|
54
125
|
const record = cache.get(key);
|
|
55
|
-
if (
|
|
126
|
+
if (isAxiosRecord(record)) {
|
|
56
127
|
if (Date.now() <= record.freshUntil) {
|
|
57
128
|
cached = cachedResponse(record, config, markerHeader);
|
|
58
129
|
}
|
|
59
130
|
else {
|
|
60
|
-
// Past its own deadline —
|
|
61
|
-
// bless a plain `Map` shim, which drops nothing), so *our* record is dropped
|
|
62
|
-
// here. Left alone it would linger as dead weight that nothing is allowed to
|
|
63
|
-
// serve. A value that is not ours (see `isAxiosRecord`) is never deleted: a
|
|
64
|
-
// read must not destroy an entry another integration wrote to a colliding key.
|
|
131
|
+
// Past its own deadline — drop our stale record (but never a foreign one).
|
|
65
132
|
cache.delete?.(key);
|
|
66
133
|
}
|
|
67
134
|
}
|
|
68
135
|
}
|
|
69
136
|
catch (error) {
|
|
70
|
-
// A cache that throws must cost one extra request — never a failed response.
|
|
71
137
|
console.warn('[blaze-cache] reading from the cache failed — falling back to the network', error);
|
|
72
138
|
}
|
|
73
139
|
if (cached)
|
|
@@ -77,59 +143,44 @@ export function blazeAdapter(options) {
|
|
|
77
143
|
if (markerHeader) {
|
|
78
144
|
response.headers = toAxiosHeaders(response.headers).set(markerHeader, MISS);
|
|
79
145
|
}
|
|
80
|
-
|
|
146
|
+
response.fromBlazeCache = false;
|
|
147
|
+
// Invalidate after the network round-trip (useful on mutations / cache-busting writes).
|
|
148
|
+
invalidateTags(reqOpts?.invalidateTags);
|
|
149
|
+
if (!skipWrite && key !== undefined) {
|
|
81
150
|
try {
|
|
82
|
-
//
|
|
83
|
-
|
|
84
|
-
// who differ in exactly that header — see `varyIsStorable` for why "unknown ⇒ do
|
|
85
|
-
// not store" is the only safe answer.
|
|
86
|
-
//
|
|
87
|
-
// `responseType: 'stream'` is never storable either: `response.data` is a live Node
|
|
88
|
-
// stream that the caller is about to consume — the adapter would store an object
|
|
89
|
-
// that is empty (or half-read) by the time anyone replays it, handing the next
|
|
90
|
-
// caller a dead stream instead of the body.
|
|
91
|
-
const storable = config.responseType !== 'stream' &&
|
|
92
|
-
shouldCache(response, config) &&
|
|
151
|
+
// `Vary` is always honored (variant correctness is not a freshness preference).
|
|
152
|
+
const storable = shouldCache(response, config) &&
|
|
93
153
|
varyIsStorable(readHeader(response.headers, 'Vary'), varyHeaders);
|
|
94
154
|
if (storable) {
|
|
95
|
-
const control =
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const ageMs = parseAgeMs(readHeader(response.headers, 'Age'));
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
// response whose `Age` already carries it past its freshness lifetime: by the time
|
|
102
|
-
// it landed here there was nothing fresh left to store.
|
|
155
|
+
const control = honourCacheControl
|
|
156
|
+
? parseCacheControl(readHeader(response.headers, 'Cache-Control'))
|
|
157
|
+
: NO_DIRECTIVES;
|
|
158
|
+
const ageMs = honourCacheControl ? parseAgeMs(readHeader(response.headers, 'Age')) : 0;
|
|
159
|
+
const configuredTtl = resolveTtl(reqOpts?.ttlMs ?? options.ttlMs, response, config);
|
|
160
|
+
const ttlMs = (control.maxAgeMs ?? configuredTtl) - ageMs;
|
|
103
161
|
if (!(control.noStore || control.noCache) && ttlMs > 0) {
|
|
104
|
-
const method = (config.method ?? 'get').toLowerCase();
|
|
105
162
|
const storedAt = Date.now();
|
|
106
163
|
const headers = storedHeaders(response.headers, response.data);
|
|
107
|
-
// The hit/miss marker describes *this* exchange — a MISS on the response that
|
|
108
|
-
// just came off the network — not the stored representation. Baked into the
|
|
109
|
-
// record it would read as a stale cache-state claim: `cachedResponse` overwrites
|
|
110
|
-
// it with HIT on replay, but anyone inspecting the record itself (debugging,
|
|
111
|
-
// a shared store, a re-stamp hook) would see the marker as if the origin sent it.
|
|
112
|
-
// `AxiosHeaders.set` normalized the name, so the plain copy has it lowercase.
|
|
113
164
|
if (markerHeader)
|
|
114
165
|
delete headers[markerHeader.toLowerCase()];
|
|
115
166
|
const record = {
|
|
116
167
|
status: response.status,
|
|
117
168
|
statusText: response.statusText,
|
|
118
169
|
headers,
|
|
119
|
-
|
|
120
|
-
// for a fresh network response.
|
|
121
|
-
data: response.data,
|
|
170
|
+
data: response.data, // raw: axios runs transformResponse on replay
|
|
122
171
|
storedAt,
|
|
123
172
|
freshUntil: storedAt + ttlMs,
|
|
124
173
|
};
|
|
125
|
-
const
|
|
126
|
-
|
|
174
|
+
const reqTags = reqOpts?.tags;
|
|
175
|
+
const globalTags = tagsFor;
|
|
176
|
+
const tagList = (typeof reqTags === 'function' ? reqTags(response, config) : reqTags) ??
|
|
177
|
+
(typeof globalTags === 'function' ? globalTags(response, config) : globalTags) ??
|
|
178
|
+
['axios', `${method.toLowerCase()} ${resolveRequestUrl(config)}`];
|
|
179
|
+
cache.set(key, record, { ttlMs, tags: [...tagList] });
|
|
127
180
|
}
|
|
128
181
|
}
|
|
129
182
|
}
|
|
130
183
|
catch (error) {
|
|
131
|
-
// The network response is good; a broken cache layer (hooks or store) must not be
|
|
132
|
-
// able to turn it into a failed request. Serving it unstored costs one request.
|
|
133
184
|
console.warn('[blaze-cache] storing the response failed — it is returned unstored', error);
|
|
134
185
|
}
|
|
135
186
|
}
|
|
@@ -142,8 +193,8 @@ export function blazeAdapter(options) {
|
|
|
142
193
|
* ```ts
|
|
143
194
|
* import { installBlazeCache } from '@blaze-cache/axios';
|
|
144
195
|
*
|
|
145
|
-
* installBlazeCache(api, { cache });
|
|
146
|
-
* installBlazeCache(api, { cache, ttlMs:
|
|
196
|
+
* installBlazeCache(api, { cache }); // GET/HEAD, 30s, Cache-Control aware
|
|
197
|
+
* installBlazeCache(api, { cache, ttlMs: 30_000, methods: ['GET'], varyHeaders: ['Authorization'] });
|
|
147
198
|
* ```
|
|
148
199
|
*
|
|
149
200
|
* The adapter currently in `instance.defaults` is captured **before** it is replaced, so the
|
|
@@ -165,14 +216,10 @@ function resolveAdapter(adapter) {
|
|
|
165
216
|
* Response headers for the record, corrected to describe `data` instead of the wire.
|
|
166
217
|
*
|
|
167
218
|
* axios's adapters decode the body for you (and already drop `content-encoding`), but
|
|
168
|
-
* `content-length` keeps the size of what crossed the network
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
* Bodyless responses (HEAD) keep both: there the headers describe the body a GET *would*
|
|
174
|
-
* return. A parsed object has no byte length of its own, so its `content-length` is left
|
|
175
|
-
* alone rather than invented.
|
|
219
|
+
* `content-length` keeps the size of what crossed the network. `transfer-encoding` stops
|
|
220
|
+
* being true the moment the body is buffered. Replaying either hands back a response that
|
|
221
|
+
* contradicts itself. Bodyless responses (HEAD) keep both; a parsed object has no
|
|
222
|
+
* knowable byte length, so its `content-length` is left alone rather than invented.
|
|
176
223
|
*/
|
|
177
224
|
function storedHeaders(headers, data) {
|
|
178
225
|
const record = toPlainHeaders(headers);
|
|
@@ -196,10 +243,6 @@ function byteLengthOf(data) {
|
|
|
196
243
|
return new TextEncoder().encode(data).length;
|
|
197
244
|
return undefined;
|
|
198
245
|
}
|
|
199
|
-
function defaultShouldCache(response, config) {
|
|
200
|
-
const method = (config.method ?? 'get').toLowerCase();
|
|
201
|
-
return method === 'get' && response.status >= 200 && response.status < 300;
|
|
202
|
-
}
|
|
203
246
|
function resolveTtl(ttl, response, config) {
|
|
204
247
|
if (typeof ttl === 'function')
|
|
205
248
|
return ttl(response, config);
|
|
@@ -231,11 +274,7 @@ function settle(response, config) {
|
|
|
231
274
|
if (!validateStatus || validateStatus(response.status)) {
|
|
232
275
|
return Promise.resolve(response);
|
|
233
276
|
}
|
|
234
|
-
return Promise.reject(new AxiosError(`Request failed with status code ${response.status}`,
|
|
235
|
-
// Same partition axios' own `settle` uses: 4xx is the caller's bad request, while a
|
|
236
|
-
// sub-400 failing a custom `validateStatus` (or any 5xx) is a bad *response*.
|
|
237
|
-
// Anything else would make `error.code` on a replay diverge from the network path.
|
|
238
|
-
response.status >= 400 && response.status < 500
|
|
277
|
+
return Promise.reject(new AxiosError(`Request failed with status code ${response.status}`, response.status >= 400 && response.status < 500
|
|
239
278
|
? AxiosError.ERR_BAD_REQUEST
|
|
240
279
|
: AxiosError.ERR_BAD_RESPONSE, config, undefined, response));
|
|
241
280
|
}
|
package/dist/adapter.js.map
CHANGED
|
@@ -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,UAAU,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;
|
|
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,aAAa,EAAE,UAAU,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAClG,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AACjF,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAS1E,mFAAmF;AACnF,MAAM,cAAc,GAAG,MAAM,CAAC;AAE9B,sEAAsE;AACtE,MAAM,eAAe,GAAsB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AAE3D,6EAA6E;AAC7E,MAAM,oBAAoB,GAAsB,CAAC,eAAe,CAAC,CAAC;AAElE,MAAM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AM,GAAG,GAAG;QAC7C,CAAC,CAAC,UAAU,CAAC,eAAe;QAC5B,CAAC,CAAC,UAAU,CAAC,gBAAgB,EAC/B,MAAM,EACN,SAAS,EACT,QAAQ,CACT,CACF,CAAC;AACJ,CAAC"}
|
package/dist/cache-control.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ export interface CacheControl {
|
|
|
7
7
|
/** `max-age=<seconds>`, converted to milliseconds. */
|
|
8
8
|
maxAgeMs: number | undefined;
|
|
9
9
|
}
|
|
10
|
+
/** The value used when Cache-Control honouring is disabled or a header is absent. */
|
|
11
|
+
export declare const NO_DIRECTIVES: CacheControl;
|
|
10
12
|
/**
|
|
11
13
|
* Minimal `Cache-Control` parser — only the directives a client-side cache must honour.
|
|
12
14
|
*
|
|
@@ -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,CA4BhF;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"}
|
|
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,qFAAqF;AACrF,eAAO,MAAM,aAAa,EAAE,YAI1B,CAAC;AAEH;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,YAAY,CA4BhF;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"}
|
package/dist/cache-control.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/** The value used when Cache-Control honouring is disabled or a header is absent. */
|
|
2
|
+
export const NO_DIRECTIVES = Object.freeze({
|
|
3
|
+
noStore: false,
|
|
4
|
+
noCache: false,
|
|
5
|
+
maxAgeMs: undefined,
|
|
6
|
+
});
|
|
1
7
|
/**
|
|
2
8
|
* Minimal `Cache-Control` parser — only the directives a client-side cache must honour.
|
|
3
9
|
*
|
|
@@ -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,KAA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ptions } from './adapter.js';
|
|
19
|
-
export { parseAgeMs, parseCacheControl, varyIsStorable } from './cache-control.js';
|
|
19
|
+
export { NO_DIRECTIVES, 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
|
-
export type { AxiosCacheRecord, BlazeAxiosCacheLike, BlazeAxiosCacheOptions, CachedAxiosResponse, } from './types.js';
|
|
22
|
+
export type { AxiosCacheRecord, BlazeAxiosCacheLike, BlazeAxiosCacheOptions, BlazeAxiosRequestOptions, CachedAxiosResponse, } from './types.js';
|
|
23
|
+
/**
|
|
24
|
+
* Invalidate tags on a cache that supports it (e.g. `BlazeCache` from
|
|
25
|
+
* `@blaze-cache/core`). A no-op when the cache has no `invalidateTags` method.
|
|
26
|
+
*/
|
|
27
|
+
export declare function invalidateCacheTags(cache: {
|
|
28
|
+
invalidateTags?(...tags: string[]): unknown;
|
|
29
|
+
} | undefined | null, ...tags: string[]): void;
|
|
23
30
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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,UAAU,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,oBAAoB,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,aAAa,EAAE,UAAU,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAClG,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,wBAAwB,EACxB,mBAAmB,GACpB,MAAM,YAAY,CAAC;AAEpB;;;GAGG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE;IAAE,cAAc,CAAC,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAA;CAAE,GAAG,SAAS,GAAG,IAAI,EACzE,GAAG,IAAI,EAAE,MAAM,EAAE,GAChB,IAAI,CAGN"}
|
package/dist/index.js
CHANGED
|
@@ -15,6 +15,16 @@
|
|
|
15
15
|
* @packageDocumentation
|
|
16
16
|
*/
|
|
17
17
|
export { blazeAdapter, installBlazeCache } from './adapter.js';
|
|
18
|
-
export { parseAgeMs, parseCacheControl, varyIsStorable } from './cache-control.js';
|
|
18
|
+
export { NO_DIRECTIVES, parseAgeMs, parseCacheControl, varyIsStorable } from './cache-control.js';
|
|
19
19
|
export { defaultCacheKey, fnv1a } from './key.js';
|
|
20
|
+
/**
|
|
21
|
+
* Invalidate tags on a cache that supports it (e.g. `BlazeCache` from
|
|
22
|
+
* `@blaze-cache/core`). A no-op when the cache has no `invalidateTags` method.
|
|
23
|
+
*/
|
|
24
|
+
export function invalidateCacheTags(cache, ...tags) {
|
|
25
|
+
if (!cache || tags.length === 0)
|
|
26
|
+
return;
|
|
27
|
+
if (typeof cache.invalidateTags === 'function')
|
|
28
|
+
cache.invalidateTags(...tags);
|
|
29
|
+
}
|
|
20
30
|
//# 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,UAAU,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,oBAAoB,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,aAAa,EAAE,UAAU,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAGlG,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAUlD;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CACjC,KAAyE,EACzE,GAAG,IAAc;IAEjB,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO;IACxC,IAAI,OAAO,KAAK,CAAC,cAAc,KAAK,UAAU;QAAE,KAAK,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AAChF,CAAC"}
|
package/dist/key.d.ts
CHANGED
|
@@ -26,6 +26,28 @@ export declare function fnv1a(input: string): string;
|
|
|
26
26
|
* to the raw url itself.
|
|
27
27
|
*/
|
|
28
28
|
export declare function resolveRequestUrl(config: InternalAxiosRequestConfig): string;
|
|
29
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* The request body as a stable, injective string for the cache key — or `undefined` when
|
|
31
|
+
* the body **cannot** be encoded and the request must therefore stay uncached.
|
|
32
|
+
*
|
|
33
|
+
* A body that reaches the key wrong is worse than no cached body at all: two different
|
|
34
|
+
* payloads would hash onto one entry and the first response would be replayed for the
|
|
35
|
+
* second. So anything whose content this function cannot actually read is refused rather
|
|
36
|
+
* than approximated:
|
|
37
|
+
*
|
|
38
|
+
* * a stream, a `Blob`/`File` or raw bytes have no readable own properties — the canonical
|
|
39
|
+
* serialisation renders each as the bare `'{}'`, which would collapse *every* such body
|
|
40
|
+
* onto *one* key;
|
|
41
|
+
* * an object that serialises to `'{}'` for any other reason (a class instance keeping its
|
|
42
|
+
* data in private fields, an empty object) carries nothing to tell bodies apart by;
|
|
43
|
+
* * a circular structure throws out of `JSON.stringify`.
|
|
44
|
+
*
|
|
45
|
+
* Ordering follows {@link stringifyParams} — keys sorted, values through `toJSON` — so the
|
|
46
|
+
* encoding stays stable across calls. That stability matters more here than elsewhere: a
|
|
47
|
+
* body that hashed differently on every call would simply never hit, while one that hashed
|
|
48
|
+
* the same for two different payloads would hand one caller another caller's response.
|
|
49
|
+
*/
|
|
50
|
+
export declare function encodeRequestBody(data: unknown): string | undefined;
|
|
51
|
+
/** Default cache key: method + URL, plus a hash of the params, the `vary` headers and the body. */
|
|
30
52
|
export declare function defaultCacheKey(config: InternalAxiosRequestConfig, varyHeaders: readonly string[]): string;
|
|
31
53
|
//# sourceMappingURL=key.d.ts.map
|
package/dist/key.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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;AAKD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,0BAA0B,GAAG,MAAM,CAe5E;AAqFD,
|
|
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;AAKD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,0BAA0B,GAAG,MAAM,CAe5E;AAqFD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAoBnE;AASD,mGAAmG;AACnG,wBAAgB,eAAe,CAC7B,MAAM,EAAE,0BAA0B,EAClC,WAAW,EAAE,SAAS,MAAM,EAAE,GAC7B,MAAM,CAyBR"}
|
package/dist/key.js
CHANGED
|
@@ -128,7 +128,56 @@ function stringifyParams(value) {
|
|
|
128
128
|
return undefined;
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
|
-
/**
|
|
131
|
+
/**
|
|
132
|
+
* The request body as a stable, injective string for the cache key — or `undefined` when
|
|
133
|
+
* the body **cannot** be encoded and the request must therefore stay uncached.
|
|
134
|
+
*
|
|
135
|
+
* A body that reaches the key wrong is worse than no cached body at all: two different
|
|
136
|
+
* payloads would hash onto one entry and the first response would be replayed for the
|
|
137
|
+
* second. So anything whose content this function cannot actually read is refused rather
|
|
138
|
+
* than approximated:
|
|
139
|
+
*
|
|
140
|
+
* * a stream, a `Blob`/`File` or raw bytes have no readable own properties — the canonical
|
|
141
|
+
* serialisation renders each as the bare `'{}'`, which would collapse *every* such body
|
|
142
|
+
* onto *one* key;
|
|
143
|
+
* * an object that serialises to `'{}'` for any other reason (a class instance keeping its
|
|
144
|
+
* data in private fields, an empty object) carries nothing to tell bodies apart by;
|
|
145
|
+
* * a circular structure throws out of `JSON.stringify`.
|
|
146
|
+
*
|
|
147
|
+
* Ordering follows {@link stringifyParams} — keys sorted, values through `toJSON` — so the
|
|
148
|
+
* encoding stays stable across calls. That stability matters more here than elsewhere: a
|
|
149
|
+
* body that hashed differently on every call would simply never hit, while one that hashed
|
|
150
|
+
* the same for two different payloads would hand one caller another caller's response.
|
|
151
|
+
*/
|
|
152
|
+
export function encodeRequestBody(data) {
|
|
153
|
+
if (data === undefined || data === null)
|
|
154
|
+
return ''; // no body
|
|
155
|
+
if (typeof data === 'string' ||
|
|
156
|
+
typeof data === 'number' ||
|
|
157
|
+
typeof data === 'boolean' ||
|
|
158
|
+
typeof data === 'bigint') {
|
|
159
|
+
return stringifyParams(data);
|
|
160
|
+
}
|
|
161
|
+
if (typeof data !== 'object')
|
|
162
|
+
return undefined; // function, symbol, ...
|
|
163
|
+
// Raw bytes: refused rather than hashed, because a 32-bit digest over caller-supplied
|
|
164
|
+
// bytes is a collision surface this key does not need to open.
|
|
165
|
+
if (ArrayBuffer.isView(data) || data instanceof ArrayBuffer)
|
|
166
|
+
return undefined;
|
|
167
|
+
const json = stringifyParams(data);
|
|
168
|
+
if (json === undefined)
|
|
169
|
+
return undefined;
|
|
170
|
+
if (json === '{}')
|
|
171
|
+
return undefined; // no enumerable content — see the doc comment above
|
|
172
|
+
return json;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Separator between the hashed key components: a raw NUL, which `JSON.stringify` never
|
|
176
|
+
* emits unescaped (it writes a six-character escape instead), so no params or body value
|
|
177
|
+
* can be crafted to straddle the boundary and impersonate the other component.
|
|
178
|
+
*/
|
|
179
|
+
const COMPONENT_SEPARATOR = String.fromCharCode(0);
|
|
180
|
+
/** Default cache key: method + URL, plus a hash of the params, the `vary` headers and the body. */
|
|
132
181
|
export function defaultCacheKey(config, varyHeaders) {
|
|
133
182
|
const method = (config.method ?? 'get').toLowerCase();
|
|
134
183
|
const url = resolveRequestUrl(config);
|
|
@@ -137,6 +186,15 @@ export function defaultCacheKey(config, varyHeaders) {
|
|
|
137
186
|
// JSON-quoted, never `' '`-joined: a space join would let `[a='x', b='y z']` and
|
|
138
187
|
// `[a='x y', b='z']` hash alike although they describe different requests.
|
|
139
188
|
const vary = JSON.stringify(varyHeaders.map((header) => readHeader(config.headers, header) ?? ''));
|
|
140
|
-
|
|
189
|
+
// A body-carrying method can only be keyed by its body too — `POST /orders` with two
|
|
190
|
+
// different payloads is two different requests, and without this they would share one
|
|
191
|
+
// entry and the first response would be replayed for the second.
|
|
192
|
+
const carriesBody = config.data !== undefined && config.data !== null;
|
|
193
|
+
const encodedBody = carriesBody ? encodeRequestBody(config.data) : undefined;
|
|
194
|
+
if (carriesBody && encodedBody === undefined) {
|
|
195
|
+
throw new Error('[blaze-cache] this request body cannot be encoded into a cache key');
|
|
196
|
+
}
|
|
197
|
+
const payload = encodedBody === undefined ? `${params} ${vary}` : `${params} ${vary}${COMPONENT_SEPARATOR}${encodedBody}`;
|
|
198
|
+
return `${method} ${url}?${fnv1a(payload)}`;
|
|
141
199
|
}
|
|
142
200
|
//# 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":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C;;;;;;GAMG;AACH,MAAM,UAAU,KAAK,CAAC,KAAa;IACjC,IAAI,IAAI,GAAG,UAAU,CAAC;IACtB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC;QACrD,IAAI,IAAI,KAAK,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QAChC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC;IAC3C,CAAC;IACD,OAAO,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AAC3B,CAAC;AAED,qGAAqG;AACrG,MAAM,YAAY,GAAG,6BAA6B,CAAC;AAEnD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAkC;IAClE,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;IAC/B,MAAM,GAAG,GAAG,MAAM,CAAC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AAC,CAAC,MAAM,EAAE,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,CACtE,CAAC;IAEF,qFAAqF;IACrF,sFAAsF;IACtF,iEAAiE;IACjE,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,KAAK,SAAS,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI,CAAC;IACtE,MAAM,WAAW,GAAG,WAAW,CAAC,CAAC,CAAC,iBAAiB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC7E,IAAI,WAAW,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;QAC7C,MAAM,IAAI,KAAK,CAAC,oEAAoE,CAAC,CAAC;IACxF,CAAC;IAED,MAAM,OAAO,GACX,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI,IAAI,GAAG,mBAAmB,GAAG,WAAW,EAAE,CAAC;IAC5G,OAAO,GAAG,MAAM,IAAI,GAAG,IAAI,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;AAC9C,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -34,6 +34,49 @@ export interface CachedAxiosResponse<T = unknown, D = unknown> extends AxiosResp
|
|
|
34
34
|
/** `true` when the response came from cache instead of the network. */
|
|
35
35
|
fromBlazeCache: boolean;
|
|
36
36
|
}
|
|
37
|
+
/** Per-request overrides passed via `config.blazeCache`. */
|
|
38
|
+
export interface BlazeAxiosRequestOptions {
|
|
39
|
+
/**
|
|
40
|
+
* Enable or disable caching for this request.
|
|
41
|
+
*
|
|
42
|
+
* `false` bypasses the cache read/write for this request (it still performs any
|
|
43
|
+
* {@link invalidateTags} after the network response). Omit/`true` to follow the
|
|
44
|
+
* adapter defaults.
|
|
45
|
+
*/
|
|
46
|
+
enabled?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Freshness window for this request's cached response, in milliseconds.
|
|
49
|
+
*
|
|
50
|
+
* Overrides the adapter-level `ttlMs`. Ignored when the response carries
|
|
51
|
+
* `Cache-Control: max-age` and `honourCacheControl` is on. `<= 0` means "do not store".
|
|
52
|
+
*/
|
|
53
|
+
ttlMs?: number | ((response: AxiosResponse, config: InternalAxiosRequestConfig) => number);
|
|
54
|
+
/** Tags attached to this request's stored response. Overrides the adapter-level `tags`. */
|
|
55
|
+
tags?: readonly string[] | ((response: AxiosResponse, config: InternalAxiosRequestConfig) => readonly string[]);
|
|
56
|
+
/**
|
|
57
|
+
* Cache key for this request. A string is used as-is; a function receives the
|
|
58
|
+
* (post-interceptor) config. Overrides the adapter-level key.
|
|
59
|
+
*/
|
|
60
|
+
key?: string | ((config: InternalAxiosRequestConfig) => string);
|
|
61
|
+
/**
|
|
62
|
+
* Request headers whose values participate in the cache key for this request.
|
|
63
|
+
* Overrides the adapter-level `varyHeaders`.
|
|
64
|
+
*/
|
|
65
|
+
varyHeaders?: readonly string[];
|
|
66
|
+
/**
|
|
67
|
+
* Tags to invalidate on the cache **after** this request completes against the
|
|
68
|
+
* network (typically used on mutations: POST/PUT/PATCH/DELETE).
|
|
69
|
+
*
|
|
70
|
+
* Requires a cache that implements `invalidateTags` (e.g. `BlazeCache` from
|
|
71
|
+
* `@blaze-cache/core`); silently ignored otherwise.
|
|
72
|
+
*/
|
|
73
|
+
invalidateTags?: readonly string[];
|
|
74
|
+
/**
|
|
75
|
+
* Force a network round-trip, bypassing the cache read (the fresh response may still
|
|
76
|
+
* be stored, subject to normal rules). Equivalent to a "reload".
|
|
77
|
+
*/
|
|
78
|
+
forceRefresh?: boolean;
|
|
79
|
+
}
|
|
37
80
|
/** Options for {@link blazeAdapter}. */
|
|
38
81
|
export interface BlazeAxiosCacheOptions {
|
|
39
82
|
/** Cache instance (or any {@link BlazeCacheLike} implementation). */
|
|
@@ -45,28 +88,57 @@ export interface BlazeAxiosCacheOptions {
|
|
|
45
88
|
* replacing them, which is what prevents the adapter from calling itself.
|
|
46
89
|
*/
|
|
47
90
|
adapter?: AxiosAdapter;
|
|
91
|
+
/**
|
|
92
|
+
* HTTP methods eligible for caching. Default: `['GET', 'HEAD']`.
|
|
93
|
+
*
|
|
94
|
+
* Applied before {@link shouldCache}. Per-request `blazeCache.enabled = true` can
|
|
95
|
+
* still cache a method outside this list when paired with a permissive `shouldCache`.
|
|
96
|
+
*
|
|
97
|
+
* Such a request is only stored if its body can be folded into the key (or a `key`
|
|
98
|
+
* callback is supplied that describes it) — otherwise two different payloads to the
|
|
99
|
+
* same URL would share one entry.
|
|
100
|
+
*/
|
|
101
|
+
methods?: readonly string[];
|
|
48
102
|
/**
|
|
49
103
|
* Freshness window for a cached response, in milliseconds.
|
|
50
104
|
*
|
|
51
|
-
* Ignored when the response carries `Cache-Control: max-age
|
|
52
|
-
* Default: `
|
|
105
|
+
* Ignored when the response carries `Cache-Control: max-age` and Cache-Control
|
|
106
|
+
* honouring is enabled. Default: `30_000`. A value `<= 0` means "do not store".
|
|
53
107
|
*/
|
|
54
108
|
ttlMs?: number | ((response: AxiosResponse, config: InternalAxiosRequestConfig) => number);
|
|
109
|
+
/**
|
|
110
|
+
* Honour `Cache-Control` (and `Age`) on both the request and the response.
|
|
111
|
+
* Default: `true`.
|
|
112
|
+
*
|
|
113
|
+
* - request `no-store` → neither read nor write
|
|
114
|
+
* - request `no-cache` / `max-age=0` → skip read (revalidate intent)
|
|
115
|
+
* - response `max-age` → overrides `ttlMs` (minus `Age`)
|
|
116
|
+
* - response `no-store` / `no-cache` → never stored
|
|
117
|
+
*
|
|
118
|
+
* `cacheControl` is accepted as an alias.
|
|
119
|
+
*/
|
|
120
|
+
honourCacheControl?: boolean;
|
|
121
|
+
/** Alias for {@link honourCacheControl}. */
|
|
122
|
+
cacheControl?: boolean;
|
|
55
123
|
/**
|
|
56
124
|
* Builds the cache key. Receives the final (post-interceptor) config.
|
|
57
125
|
*
|
|
58
|
-
* The default hashes the method, URL, params
|
|
59
|
-
* secrets such as bearer tokens out of stored keys.
|
|
126
|
+
* The default hashes the method, URL, params, the {@link varyHeaders} values and the
|
|
127
|
+
* request body, keeping secrets such as bearer tokens out of stored keys.
|
|
128
|
+
*
|
|
129
|
+
* Supplying this replaces **all** of that, so a key for a body-carrying method must
|
|
130
|
+
* describe the body itself — the adapter trusts an explicit key and skips the check it
|
|
131
|
+
* would otherwise apply.
|
|
60
132
|
*/
|
|
61
133
|
key?: (config: InternalAxiosRequestConfig) => string;
|
|
62
134
|
/**
|
|
63
135
|
* Decide whether a response is worth storing.
|
|
64
136
|
*
|
|
65
|
-
* Default:
|
|
137
|
+
* Default: methods in {@link methods} answering `2xx`.
|
|
66
138
|
*/
|
|
67
139
|
shouldCache?: (response: AxiosResponse, config: InternalAxiosRequestConfig) => boolean;
|
|
68
|
-
/** Tags attached to stored responses. Default: `['axios', '<
|
|
69
|
-
tags?: (response: AxiosResponse, config: InternalAxiosRequestConfig) => string[];
|
|
140
|
+
/** Tags attached to stored responses. Default: `['axios', '<method> <resolved url>']`. */
|
|
141
|
+
tags?: readonly string[] | ((response: AxiosResponse, config: InternalAxiosRequestConfig) => readonly string[]);
|
|
70
142
|
/**
|
|
71
143
|
* Request headers whose values participate in the cache key.
|
|
72
144
|
*
|
|
@@ -91,6 +163,21 @@ export interface BlazeAxiosCacheLike {
|
|
|
91
163
|
* without it a non-expiring store would keep dead weight it is never allowed to serve.
|
|
92
164
|
*/
|
|
93
165
|
delete?(key: string): boolean | void;
|
|
166
|
+
/**
|
|
167
|
+
* Optional tag-based invalidation (e.g. `BlazeCache` from `@blaze-cache/core`).
|
|
168
|
+
* Used by per-request `blazeCache.invalidateTags` on mutations.
|
|
169
|
+
*/
|
|
170
|
+
invalidateTags?(...tags: string[]): string[] | void;
|
|
94
171
|
}
|
|
95
172
|
export type { AxiosAdapter, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig };
|
|
173
|
+
declare module 'axios' {
|
|
174
|
+
interface AxiosRequestConfig {
|
|
175
|
+
/** Per-request Blaze Cache options (or `false` to disable caching for this request). */
|
|
176
|
+
blazeCache?: BlazeAxiosRequestOptions | false;
|
|
177
|
+
}
|
|
178
|
+
interface AxiosResponse {
|
|
179
|
+
/** `true` when the response came from cache instead of the network. */
|
|
180
|
+
fromBlazeCache?: boolean;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
96
183
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -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;IACjB;;;;;;OAMG;IACH,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;;;;GAKG;AACH,MAAM,WAAW,mBAAmB,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO,CAAE,SAAQ,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC;IACxF,uEAAuE;IACvE,cAAc,EAAE,OAAO,CAAC;CACzB;AAED,wCAAwC;AACxC,MAAM,WAAW,sBAAsB;IACrC,qEAAqE;IACrE,KAAK,EAAE,mBAAmB,CAAC;IAE3B;;;;;OAKG;IACH,OAAO,CAAC,EAAE,YAAY,CAAC;IAEvB;;;;;OAKG;IACH,KAAK,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,QAAQ,EAAE,aAAa,EAAE,MAAM,EAAE,0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AG,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;IACrC;;;OAGG;IACH,cAAc,CAAC,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,GAAG,IAAI,CAAC;CACrD;AAED,YAAY,EAAE,YAAY,EAAE,kBAAkB,EAAE,aAAa,EAAE,0BAA0B,EAAE,CAAC;AAG5F,OAAO,QAAQ,OAAO,CAAC;IACrB,UAAU,kBAAkB;QAC1B,wFAAwF;QACxF,UAAU,CAAC,EAAE,wBAAwB,GAAG,KAAK,CAAC;KAC/C;IACD,UAAU,aAAa;QACrB,uEAAuE;QACvE,cAAc,CAAC,EAAE,OAAO,CAAC;KAC1B;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blaze-cache/axios",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0-beta.0",
|
|
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,13 +38,13 @@
|
|
|
38
38
|
"dist"
|
|
39
39
|
],
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@blaze-cache/core": "0.
|
|
41
|
+
"@blaze-cache/core": "0.4.0-beta.0"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"axios": "^1.7.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"axios": "^1.
|
|
47
|
+
"axios": "^1.21.0",
|
|
48
48
|
"typescript": "^5.9.3"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|