@blaze-cache/ofetch 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +180 -2
- package/dist/cache-control.d.ts +53 -0
- package/dist/cache-control.d.ts.map +1 -0
- package/dist/cache-control.js +109 -0
- package/dist/cache-control.js.map +1 -0
- package/dist/client.d.ts +29 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +557 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +47 -0
- package/dist/index.js.map +1 -0
- package/dist/key.d.ts +67 -0
- package/dist/key.d.ts.map +1 -0
- package/dist/key.js +147 -0
- package/dist/key.js.map +1 -0
- package/dist/types.d.ts +194 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +56 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Blaze Cache
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,181 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/blaze-cache/blaze-cache/main/docs/blaze-cache-logo.png" alt="Blaze Cache logo" width="150">
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<h1 align="center">@blaze-cache/ofetch</h1>
|
|
6
|
+
|
|
7
|
+
Cache [ofetch](https://github.com/unjs/ofetch) (`$fetch`) responses through a WebAssembly
|
|
8
|
+
cache — TTLs, tags, `Cache-Control` and an `x-blaze-cache: HIT | MISS` marker.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install @blaze-cache/ofetch @blaze-cache/core ofetch
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
`@beta` is npm's dist-tag for prereleases; append it to install a prerelease instead of
|
|
17
|
+
this stable release. `ofetch` ≥ 1.0 is a peer dependency.
|
|
18
|
+
|
|
19
|
+
## Quick start
|
|
20
|
+
|
|
21
|
+
Wrap an existing ofetch instance (including one from `ofetch.create()`) so successful
|
|
22
|
+
`GET` responses are stored in Blaze Cache and replayed from it:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { ofetch } from 'ofetch';
|
|
26
|
+
import { blazeOfetch } from '@blaze-cache/ofetch';
|
|
27
|
+
import { createCache } from '@blaze-cache/core';
|
|
28
|
+
|
|
29
|
+
const cache = await createCache({ capacity: 1000 });
|
|
30
|
+
|
|
31
|
+
const api = blazeOfetch(ofetch.create({ baseURL: 'https://api.example.com' }), cache, {
|
|
32
|
+
ttlMs: 30_000,
|
|
33
|
+
methods: ['GET'],
|
|
34
|
+
tags: (response, ctx) => ['api'],
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const users = await api('/users'); // parsed JSON — network (MISS)
|
|
38
|
+
const again = await api('/users'); // parsed JSON — replayed from cache (HIT)
|
|
39
|
+
|
|
40
|
+
const res = await api.raw('/users'); // FetchResponse (HIT)
|
|
41
|
+
res.headers.get('x-blaze-cache'); // 'HIT'
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The helper form creates a fresh cached instance — ofetch defaults and cache options in one
|
|
45
|
+
bag:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { createBlazeOfetch } from '@blaze-cache/ofetch';
|
|
49
|
+
|
|
50
|
+
const api = createBlazeOfetch(cache, {
|
|
51
|
+
baseURL: 'https://api.example.com', // any ofetch option, as usual
|
|
52
|
+
ttlMs: 30_000, // …plus the cache options
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const users = await api('/users');
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Behaviour
|
|
59
|
+
|
|
60
|
+
* **Only `GET` is cached** — pass `methods: ['GET', 'HEAD']` to include `HEAD`. Everything
|
|
61
|
+
else always goes to the network and is never stored. A method you *do* list is only
|
|
62
|
+
stored when its body can be folded into the cache key — two different payloads to the
|
|
63
|
+
same URL must never share an entry — so a body the key cannot read (a stream, raw bytes)
|
|
64
|
+
leaves that request uncached unless you supply a `key` callback that describes it.
|
|
65
|
+
* **Only successful responses are stored** — the default `shouldCache` keeps `2xx`;
|
|
66
|
+
replace it to widen or narrow that. A response `shouldCache` rejects is still returned
|
|
67
|
+
(or throws, if ofetch would throw for that status) — it is simply not cached.
|
|
68
|
+
* **`Cache-Control` is honoured on both sides** when `cacheControl` is on (the default):
|
|
69
|
+
on the request, `no-store`/`no-cache` and `max-age=0` skip the read, and `no-store`
|
|
70
|
+
also skips the write; on the response, `max-age` overrides `ttlMs`, `Age` is deducted,
|
|
71
|
+
and `no-store`/`no-cache` — the per-field `no-cache="set-cookie"` form included — stop
|
|
72
|
+
it being stored.
|
|
73
|
+
* **`Authorization` participates in the default key** (hashed, never stored verbatim), so
|
|
74
|
+
a personalized response is never replayed to a different user. Tune with `varyHeaders`.
|
|
75
|
+
* **The response's own `Vary` is honoured** — `Vary: *` is never stored (RFC 9111 §3), and
|
|
76
|
+
a response varying on a header you have not put in `varyHeaders` is left uncached rather
|
|
77
|
+
than replay a variant to the wrong caller. The check runs before *every* store, even with
|
|
78
|
+
`cacheControl: false`.
|
|
79
|
+
* **The full ofetch API is preserved** — call the wrapped instance directly for parsed
|
|
80
|
+
data, `.raw()` for the `FetchResponse` (with `_data` and the `x-blaze-cache` marker),
|
|
81
|
+
`.native` for the underlying fetch, and `.create()` for derived instances (which remain
|
|
82
|
+
cached, with their `baseURL`, `headers` and other options applied to *both* the request
|
|
83
|
+
and the cache key). A `baseURL` from the call, from `.create()` defaults or from the
|
|
84
|
+
wrapper's own `baseURL` option is resolved into the key the same way ofetch resolves it;
|
|
85
|
+
an instance's *own* `defaults.baseURL` — which ofetch keeps in a closure it does not
|
|
86
|
+
expose — is learned from the first response's URL, and until that response arrives the
|
|
87
|
+
request URL is still relative, so nothing is read or written: two wrappers over different
|
|
88
|
+
origins can never share an entry.
|
|
89
|
+
* **Buffering never delays a response** — the body is stored from a clone in the
|
|
90
|
+
background, and the record is complete before it is written, so a hit can never
|
|
91
|
+
half-load. Transport headers are corrected to the stored bytes (`content-encoding` and
|
|
92
|
+
`transfer-encoding` dropped, `content-length` rewritten).
|
|
93
|
+
* **Failures behave like plain ofetch** — a rejected network call still rejects; a cache
|
|
94
|
+
that cannot read or store a response never breaks the request that produced it. The first
|
|
95
|
+
cache-layer failure is announced once on the console, then the adapter keeps working
|
|
96
|
+
*without* the cache rather than warning on every call.
|
|
97
|
+
* **Cache activity is observable** — reads/writes use `cache.get`/`cache.set`, so the
|
|
98
|
+
shared cache's typed event system sees every request the adapter serves.
|
|
99
|
+
|
|
100
|
+
## Options
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
const api = blazeOfetch(ofetch, cache, {
|
|
104
|
+
ttlMs: 30_000, // number | (response, ctx) => number
|
|
105
|
+
methods: ['GET'], // which methods are cacheable
|
|
106
|
+
cacheControl: true, // honour Cache-Control on request + response
|
|
107
|
+
key: (ctx) => `${ctx.method} ${ctx.url}`, // custom cache key
|
|
108
|
+
shouldCache: (response) => response.ok, // which responses are stored
|
|
109
|
+
tags: (response, ctx) => ['api'], // tags on stored entries
|
|
110
|
+
varyHeaders: ['authorization'], // headers folded into the default key
|
|
111
|
+
markerHeader: 'x-blaze-cache', // or false to disable the marker
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
`createBlazeOfetch(cache, options)` accepts these **plus** every ofetch option.
|
|
116
|
+
|
|
117
|
+
| Option | Default | What it does |
|
|
118
|
+
| --- | --- | --- |
|
|
119
|
+
| `ttlMs` | `60_000` | Freshness window. Overridden by a response `max-age`. |
|
|
120
|
+
| `methods` | `['GET']` | Methods eligible for caching. |
|
|
121
|
+
| `cacheControl` | `true` | Honour `Cache-Control`/`Age` on both sides. |
|
|
122
|
+
| `key` | method + resolved URL + vary/body hash | Custom cache key (receives `{ request, options, method, url, headers, body }`). |
|
|
123
|
+
| `shouldCache` | `2xx` | Which responses are stored. |
|
|
124
|
+
| `tags` | `['ofetch', '<method> <url>']` | Tags for `cache.invalidateTags(...)`. |
|
|
125
|
+
| `varyHeaders` | `['authorization']` | Request headers folded into the default key. |
|
|
126
|
+
| `markerHeader` | `'x-blaze-cache'` | Header carrying `HIT`/`MISS` on `raw()` responses; `false` disables it. |
|
|
127
|
+
|
|
128
|
+
## Invalidating
|
|
129
|
+
|
|
130
|
+
Stored responses carry two tags, so they plug straight into the cache's tag API:
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
cache.invalidateTags('ofetch'); // everything this adapter stored
|
|
134
|
+
cache.invalidateTags('get https://api.example.com/users'); // one resource
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
The per-resource tag is `'<method> <url>'` — the same spelling `@blaze-cache/fetch`,
|
|
138
|
+
`@blaze-cache/axios`, and `@blaze-cache/ky` use, so integrations sharing one cache
|
|
139
|
+
invalidate together.
|
|
140
|
+
|
|
141
|
+
## Custom keys
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
import { blazeOfetch, defaultCacheKey } from '@blaze-cache/ofetch';
|
|
145
|
+
|
|
146
|
+
const api = blazeOfetch(ofetch, cache, {
|
|
147
|
+
key: (ctx) =>
|
|
148
|
+
`v2:${defaultCacheKey(ctx.method, ctx.url, ctx.headers, ['authorization'], ctx.body)}`,
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Supplying `key` replaces **everything** the default included — method, URL, vary hash and
|
|
153
|
+
body — and opts a body-carrying request into caching that the adapter would otherwise skip
|
|
154
|
+
as unkeyable, so the callback must describe the body itself.
|
|
155
|
+
|
|
156
|
+
## Testing
|
|
157
|
+
|
|
158
|
+
```sh
|
|
159
|
+
pnpm --filter @blaze-cache/ofetch test
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
The suite runs against the built package with a stubbed network — deterministic and
|
|
163
|
+
offline.
|
|
164
|
+
|
|
165
|
+
## Documentation
|
|
166
|
+
|
|
167
|
+
Setup, core concepts and the complete API reference live in the
|
|
168
|
+
[repository README](https://github.com/blaze-cache/blaze-cache#readme).
|
|
169
|
+
|
|
170
|
+
## Part of Blaze Cache
|
|
171
|
+
|
|
172
|
+
* [`@blaze-cache/core`](https://www.npmjs.com/package/@blaze-cache/core) — the cache itself
|
|
173
|
+
* [`@blaze-cache/fetch`](https://www.npmjs.com/package/@blaze-cache/fetch) — the drop-in `fetch()` replacement (stale-while-revalidate included)
|
|
174
|
+
* [`@blaze-cache/axios`](https://www.npmjs.com/package/@blaze-cache/axios) — the axios adapter
|
|
175
|
+
* [`@blaze-cache/ky`](https://www.npmjs.com/package/@blaze-cache/ky) — the ky hook adapter
|
|
176
|
+
* [`@blaze-cache/react-query`](https://www.npmjs.com/package/@blaze-cache/react-query) — TanStack Query v5 mirror
|
|
177
|
+
* [`@blaze-cache/swr`](https://www.npmjs.com/package/@blaze-cache/swr) — SWR middleware + hook
|
|
178
|
+
|
|
179
|
+
## License
|
|
180
|
+
|
|
181
|
+
[MIT](https://github.com/blaze-cache/blaze-cache/blob/main/LICENSE) © Blaze Cache
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What `Cache-Control` (and the related `Age`) say about a request or a response — plus the
|
|
3
|
+
* one `Vary` question a keying client must answer before storing anything.
|
|
4
|
+
*
|
|
5
|
+
* Package-local on purpose: this adapter deliberately reads only the directives it acts
|
|
6
|
+
* on. `stale-while-revalidate` and `must-revalidate` are *not* parsed here — they shape
|
|
7
|
+
* the stale window of `@blaze-cache/fetch`, which has one; this adapter never serves a
|
|
8
|
+
* stale entry at all, so for it they are equivalent to plain expiry. Each package is free
|
|
9
|
+
* to grow its own directives without dragging the others along.
|
|
10
|
+
*/
|
|
11
|
+
export interface CacheControl {
|
|
12
|
+
/** `no-store` — neither read nor write the cache. */
|
|
13
|
+
noStore: boolean;
|
|
14
|
+
/** `no-cache` — must not be served without revalidating, per-field spellings (`no-cache="…"`) included. */
|
|
15
|
+
noCache: boolean;
|
|
16
|
+
/** `max-age=<seconds>`, converted to milliseconds. */
|
|
17
|
+
maxAgeMs: number | undefined;
|
|
18
|
+
}
|
|
19
|
+
/** The value used when parsing is switched off (`cacheControl: false`) or a header is absent. */
|
|
20
|
+
export declare const NO_DIRECTIVES: CacheControl;
|
|
21
|
+
/**
|
|
22
|
+
* Minimal `Cache-Control` parser — only the directives a client-side cache acts on.
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* parseCacheControl('max-age=30, private').maxAgeMs; // 30_000
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function parseCacheControl(value: string | undefined | null): CacheControl;
|
|
29
|
+
/**
|
|
30
|
+
* Whether a response carrying `Vary: <responseVary>` may be stored under a key that already
|
|
31
|
+
* folds in `varyHeaders`.
|
|
32
|
+
*
|
|
33
|
+
* `Vary` names the *request* headers the response's identity depends on — `Cookie`,
|
|
34
|
+
* `Accept-Language`, `Origin`. A cache may only replay a stored response to a request whose
|
|
35
|
+
* keyed headers match, so a response varying on anything **not** folded into the key cannot
|
|
36
|
+
* be keyed correctly, and the only safe verdict is "store nothing": storing anyway would
|
|
37
|
+
* replay one caller's variant — another user's private payload, another locale's page — to
|
|
38
|
+
* the next caller whose unkeyed header happens to differ. `Vary: *` advertises variants
|
|
39
|
+
* that are unknowable by construction and is never cacheable (RFC 9111 §3).
|
|
40
|
+
*
|
|
41
|
+
* An absent, empty or unparsable header varies on nothing and is always storable; a listed
|
|
42
|
+
* name that *is* folded in (case-insensitively, as header names are) makes the response
|
|
43
|
+
* storable.
|
|
44
|
+
*/
|
|
45
|
+
export declare function varyIsStorable(responseVary: string | null, varyHeaders: readonly string[]): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* `Age: <seconds>` — how long an intermediate cache has been holding the response.
|
|
48
|
+
*
|
|
49
|
+
* The freshness window shrinks by this much: a response that is already halfway to its
|
|
50
|
+
* `max-age` deadline must not look brand new to us. Anything unparsable counts as zero.
|
|
51
|
+
*/
|
|
52
|
+
export declare function parseAgeMs(value: string | undefined | null): number;
|
|
53
|
+
//# sourceMappingURL=cache-control.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache-control.d.ts","sourceRoot":"","sources":["../src/cache-control.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,MAAM,WAAW,YAAY;IAC3B,qDAAqD;IACrD,OAAO,EAAE,OAAO,CAAC;IACjB,2GAA2G;IAC3G,OAAO,EAAE,OAAO,CAAC;IACjB,sDAAsD;IACtD,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;CAC9B;AAED,iGAAiG;AACjG,eAAO,MAAM,aAAa,EAAE,YAI1B,CAAC;AAEH;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,YAAY,CA4BhF;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,cAAc,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,EAAE,WAAW,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAYnG;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,MAAM,CAInE"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What `Cache-Control` (and the related `Age`) say about a request or a response — plus the
|
|
3
|
+
* one `Vary` question a keying client must answer before storing anything.
|
|
4
|
+
*
|
|
5
|
+
* Package-local on purpose: this adapter deliberately reads only the directives it acts
|
|
6
|
+
* on. `stale-while-revalidate` and `must-revalidate` are *not* parsed here — they shape
|
|
7
|
+
* the stale window of `@blaze-cache/fetch`, which has one; this adapter never serves a
|
|
8
|
+
* stale entry at all, so for it they are equivalent to plain expiry. Each package is free
|
|
9
|
+
* to grow its own directives without dragging the others along.
|
|
10
|
+
*/
|
|
11
|
+
/** The value used when parsing is switched off (`cacheControl: false`) or a header is absent. */
|
|
12
|
+
export const NO_DIRECTIVES = Object.freeze({
|
|
13
|
+
noStore: false,
|
|
14
|
+
noCache: false,
|
|
15
|
+
maxAgeMs: undefined,
|
|
16
|
+
});
|
|
17
|
+
/**
|
|
18
|
+
* Minimal `Cache-Control` parser — only the directives a client-side cache acts on.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* parseCacheControl('max-age=30, private').maxAgeMs; // 30_000
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function parseCacheControl(value) {
|
|
25
|
+
const result = {
|
|
26
|
+
noStore: false,
|
|
27
|
+
noCache: false,
|
|
28
|
+
maxAgeMs: undefined,
|
|
29
|
+
};
|
|
30
|
+
if (!value)
|
|
31
|
+
return result;
|
|
32
|
+
for (const rawDirective of value.split(',')) {
|
|
33
|
+
const directive = rawDirective.trim().toLowerCase();
|
|
34
|
+
if (directive === 'no-store') {
|
|
35
|
+
result.noStore = true;
|
|
36
|
+
}
|
|
37
|
+
else if (directive === 'no-cache') {
|
|
38
|
+
result.noCache = true;
|
|
39
|
+
}
|
|
40
|
+
else if (directive.startsWith('no-cache=')) {
|
|
41
|
+
// `no-cache="set-cookie, field2"` — reuse is forbidden until *those fields* have been
|
|
42
|
+
// revalidated, and this cache cannot revalidate individual fields, so it must read as
|
|
43
|
+
// the whole-response spelling. Matching only the bare directive would silently turn
|
|
44
|
+
// this opt-out back into "cacheable".
|
|
45
|
+
result.noCache = true;
|
|
46
|
+
}
|
|
47
|
+
else if (directive.startsWith('max-age=')) {
|
|
48
|
+
// First occurrence wins (RFC 9111 §5.2.3).
|
|
49
|
+
if (result.maxAgeMs === undefined) {
|
|
50
|
+
result.maxAgeMs = seconds(directive.slice('max-age='.length));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Whether a response carrying `Vary: <responseVary>` may be stored under a key that already
|
|
58
|
+
* folds in `varyHeaders`.
|
|
59
|
+
*
|
|
60
|
+
* `Vary` names the *request* headers the response's identity depends on — `Cookie`,
|
|
61
|
+
* `Accept-Language`, `Origin`. A cache may only replay a stored response to a request whose
|
|
62
|
+
* keyed headers match, so a response varying on anything **not** folded into the key cannot
|
|
63
|
+
* be keyed correctly, and the only safe verdict is "store nothing": storing anyway would
|
|
64
|
+
* replay one caller's variant — another user's private payload, another locale's page — to
|
|
65
|
+
* the next caller whose unkeyed header happens to differ. `Vary: *` advertises variants
|
|
66
|
+
* that are unknowable by construction and is never cacheable (RFC 9111 §3).
|
|
67
|
+
*
|
|
68
|
+
* An absent, empty or unparsable header varies on nothing and is always storable; a listed
|
|
69
|
+
* name that *is* folded in (case-insensitively, as header names are) makes the response
|
|
70
|
+
* storable.
|
|
71
|
+
*/
|
|
72
|
+
export function varyIsStorable(responseVary, varyHeaders) {
|
|
73
|
+
if (responseVary === null)
|
|
74
|
+
return true;
|
|
75
|
+
const names = responseVary
|
|
76
|
+
.split(',')
|
|
77
|
+
.map((name) => name.trim().toLowerCase())
|
|
78
|
+
.filter((name) => name.length > 0);
|
|
79
|
+
if (names.length === 0)
|
|
80
|
+
return true; // `Vary:` with no names varies on nothing
|
|
81
|
+
if (names.includes('*'))
|
|
82
|
+
return false; // RFC 9111 §3: never usable from a cache
|
|
83
|
+
const keyed = new Set(varyHeaders.map((name) => name.toLowerCase()));
|
|
84
|
+
return names.every((name) => keyed.has(name));
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* `Age: <seconds>` — how long an intermediate cache has been holding the response.
|
|
88
|
+
*
|
|
89
|
+
* The freshness window shrinks by this much: a response that is already halfway to its
|
|
90
|
+
* `max-age` deadline must not look brand new to us. Anything unparsable counts as zero.
|
|
91
|
+
*/
|
|
92
|
+
export function parseAgeMs(value) {
|
|
93
|
+
if (!value)
|
|
94
|
+
return 0;
|
|
95
|
+
const secondsValue = Number(value.trim());
|
|
96
|
+
return Number.isFinite(secondsValue) && secondsValue >= 0 ? secondsValue * 1000 : 0;
|
|
97
|
+
}
|
|
98
|
+
/** Seconds → milliseconds, or `undefined` when the value is not a usable non-negative number. */
|
|
99
|
+
function seconds(raw) {
|
|
100
|
+
const trimmed = raw.trim();
|
|
101
|
+
// An empty value (`max-age=`) is malformed, not zero: `Number('')` is `0`, and reading
|
|
102
|
+
// "instantly stale" out of a malformed directive would defeat the header entirely.
|
|
103
|
+
// RFC 9111 §5.2.3 says ignore it — which here means `undefined`, the no-directive answer.
|
|
104
|
+
if (trimmed === '')
|
|
105
|
+
return undefined;
|
|
106
|
+
const value = Number(trimmed);
|
|
107
|
+
return Number.isFinite(value) && value >= 0 ? value * 1000 : undefined;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=cache-control.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache-control.js","sourceRoot":"","sources":["../src/cache-control.ts"],"names":[],"mappings":"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"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { $Fetch } from 'ofetch';
|
|
2
|
+
import type { BlazeOfetchCacheLike, BlazeOfetchInstance, BlazeOfetchOptions } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Wrap an existing ofetch (`$fetch`) instance with Blaze Cache.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { ofetch } from 'ofetch';
|
|
8
|
+
* import { blazeOfetch } from '@blaze-cache/ofetch';
|
|
9
|
+
* import { createCache } from '@blaze-cache/core';
|
|
10
|
+
*
|
|
11
|
+
* const cache = createCache({ maxEntries: 500 });
|
|
12
|
+
* const api = blazeOfetch(ofetch, cache, { ttlMs: 30_000 });
|
|
13
|
+
*
|
|
14
|
+
* const users = await api('https://api.example.com/users');
|
|
15
|
+
* // or wrap a configured instance:
|
|
16
|
+
* const api2 = blazeOfetch(ofetch.create({ baseURL: 'https://api.example.com' }), cache);
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Successful `GET` responses are cached by default. The wrapped instance keeps ofetch's
|
|
20
|
+
* full API: call it directly, or use `.raw()` / `.native` / `.create()`. Instances
|
|
21
|
+
* returned by `.create()` on the wrapped result are themselves cached with the same
|
|
22
|
+
* settings, and their options apply to both the request *and* the cache key.
|
|
23
|
+
*
|
|
24
|
+
* @param instance The ofetch (`$fetch`) instance to wrap.
|
|
25
|
+
* @param cache The Blaze Cache instance (or any {@link BlazeOfetchCacheLike}).
|
|
26
|
+
* @param options Caching behaviour — all optional.
|
|
27
|
+
*/
|
|
28
|
+
export declare function blazeOfetch(instance: $Fetch, cache: BlazeOfetchCacheLike, options?: BlazeOfetchOptions): BlazeOfetchInstance;
|
|
29
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAA+B,MAAM,QAAQ,CAAC;AAIlE,OAAO,KAAK,EACV,oBAAoB,EACpB,mBAAmB,EAGnB,kBAAkB,EAEnB,MAAM,YAAY,CAAC;AAqDpB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,WAAW,CACzB,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,oBAAoB,EAC3B,OAAO,GAAE,kBAAuB,GAC/B,mBAAmB,CAmYrB"}
|