@blaze-cache/ky 0.0.0-stage → 0.3.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 +218 -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 +26 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +42 -0
- package/dist/client.js.map +1 -0
- package/dist/hook.d.ts +31 -0
- package/dist/hook.d.ts.map +1 -0
- package/dist/hook.js +391 -0
- package/dist/hook.js.map +1 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/key.d.ts +27 -0
- package/dist/key.d.ts.map +1 -0
- package/dist/key.js +45 -0
- package/dist/key.js.map +1 -0
- package/dist/types.d.ts +183 -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 +54 -3
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import type { Options as KyOptions } from 'ky';
|
|
2
|
+
/**
|
|
3
|
+
* Value written on the `x-blaze-cache` response header by every response the adapter
|
|
4
|
+
* returns.
|
|
5
|
+
*
|
|
6
|
+
* * `MISS` — the response came from the network.
|
|
7
|
+
* * `HIT` — served from the cache while still fresh.
|
|
8
|
+
*
|
|
9
|
+
* There is no `STALE`: this adapter never serves an entry past its freshness window —
|
|
10
|
+
* `Cache-Control: stale-while-revalidate` belongs to `@blaze-cache/fetch`, which has a
|
|
11
|
+
* stale window to serve from.
|
|
12
|
+
*/
|
|
13
|
+
export type BlazeKyMarker = 'HIT' | 'MISS';
|
|
14
|
+
/**
|
|
15
|
+
* What the adapter stores in Blaze Cache for one response.
|
|
16
|
+
*
|
|
17
|
+
* The record is a plain, structured-clone-friendly object: an `ArrayBuffer` body plus
|
|
18
|
+
* primitive metadata, so it survives an IndexedDB round-trip when the cache is configured
|
|
19
|
+
* with persistence.
|
|
20
|
+
*/
|
|
21
|
+
export interface KyCacheRecord {
|
|
22
|
+
status: number;
|
|
23
|
+
statusText: string;
|
|
24
|
+
/** Response headers as a plain record, names lower-cased (as `Headers` reports them). */
|
|
25
|
+
headers: Record<string, string>;
|
|
26
|
+
/**
|
|
27
|
+
* Raw response body.
|
|
28
|
+
*
|
|
29
|
+
* Empty for statuses that cannot carry one (`204`, `205`, `304`) — those are replayed with
|
|
30
|
+
* a `null` body, exactly like the network sent them.
|
|
31
|
+
*/
|
|
32
|
+
body: ArrayBuffer;
|
|
33
|
+
/** Final URL after redirects — replayed as [`Response.url`](https://developer.mozilla.org/en-US/docs/Web/API/Response/url). */
|
|
34
|
+
url: string;
|
|
35
|
+
/** Whether the stored response followed a redirect. */
|
|
36
|
+
redirected: boolean;
|
|
37
|
+
/** `Date.now()` when the response was received. */
|
|
38
|
+
storedAt: number;
|
|
39
|
+
/**
|
|
40
|
+
* `Date.now()` at which the entry stops being fresh.
|
|
41
|
+
*
|
|
42
|
+
* The read path checks this itself instead of trusting the store: {@link BlazeKyCacheLike}
|
|
43
|
+
* is deliberately structural — a plain `Map` shim qualifies — and such a store never
|
|
44
|
+
* expires anything, so without this deadline a record would be replayed forever.
|
|
45
|
+
*/
|
|
46
|
+
freshUntil: number;
|
|
47
|
+
}
|
|
48
|
+
/** The slice of a cache the adapter needs — deliberately structural. */
|
|
49
|
+
export interface BlazeKyCacheLike {
|
|
50
|
+
get(key: string): KyCacheRecord | undefined;
|
|
51
|
+
set(key: string, value: KyCacheRecord, options?: {
|
|
52
|
+
ttlMs?: number;
|
|
53
|
+
tags?: readonly string[];
|
|
54
|
+
}): void;
|
|
55
|
+
/**
|
|
56
|
+
* Optional, so a minimal `Map` shim still satisfies the interface.
|
|
57
|
+
*
|
|
58
|
+
* When present, the adapter uses it to drop a record it found past
|
|
59
|
+
* {@link KyCacheRecord.freshUntil} — without it a non-expiring store would keep dead
|
|
60
|
+
* weight it is never allowed to serve.
|
|
61
|
+
*/
|
|
62
|
+
delete?(key: string): boolean | void;
|
|
63
|
+
}
|
|
64
|
+
/** Options accepted by {@link blazeKyHook}. All of them are optional. */
|
|
65
|
+
export interface BlazeKyCacheOptions {
|
|
66
|
+
/**
|
|
67
|
+
* Freshness window for a stored response, in milliseconds.
|
|
68
|
+
*
|
|
69
|
+
* Ignored when the response carries `Cache-Control: max-age`, which wins. A response
|
|
70
|
+
* carrying `Age` has that age deducted. Default: `60_000`. A value `<= 0` means
|
|
71
|
+
* "do not store".
|
|
72
|
+
*/
|
|
73
|
+
ttlMs?: number | ((response: Response, request: Request) => number);
|
|
74
|
+
/**
|
|
75
|
+
* HTTP methods eligible for caching. Default: `['GET']`.
|
|
76
|
+
*
|
|
77
|
+
* Pass `['GET', 'HEAD']` to cache `HEAD` too. Any other method always goes to the network
|
|
78
|
+
* and is never stored.
|
|
79
|
+
*/
|
|
80
|
+
methods?: readonly string[];
|
|
81
|
+
/**
|
|
82
|
+
* Honour `Cache-Control` on both sides of the exchange. Default: `true`.
|
|
83
|
+
*
|
|
84
|
+
* * request `no-store` → neither read nor write the cache;
|
|
85
|
+
* * request `no-cache` / `max-age=0` → skip the read, still store the response;
|
|
86
|
+
* * response `no-store` → never stored;
|
|
87
|
+
* * response `no-cache` (even per-field, `no-cache="field1"`) → never stored;
|
|
88
|
+
* * response `max-age` → overrides {@link ttlMs}, minus the response's `Age`.
|
|
89
|
+
*
|
|
90
|
+
* Set to `false` to treat `ttlMs` as the only authority. The response's `Vary` header is
|
|
91
|
+
* *always* honoured (see {@link varyHeaders}) — variant correctness is not a preference.
|
|
92
|
+
*/
|
|
93
|
+
cacheControl?: boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Builds the cache key. Receives the ky `Request` — final URL (after `prefixUrl`/
|
|
96
|
+
* `baseUrl` and `searchParams`), normalized method and merged headers.
|
|
97
|
+
*
|
|
98
|
+
* The default is `"<method> <url>"` plus a hash of {@link varyHeaders} —
|
|
99
|
+
* {@link defaultCacheKey} is that default, so you can wrap it and only change the parts
|
|
100
|
+
* you care about. Replacing the key replaces **everything** the default included, so
|
|
101
|
+
* include the method yourself when caching more than one method.
|
|
102
|
+
*/
|
|
103
|
+
key?: (request: Request) => string;
|
|
104
|
+
/**
|
|
105
|
+
* Decide whether a response is worth storing. Default: only `2xx` responses
|
|
106
|
+
* (the {@link methods} gate has already applied by then).
|
|
107
|
+
*
|
|
108
|
+
* A response this rejects is still returned — it is simply not cached, and still carries
|
|
109
|
+
* `x-blaze-cache: MISS`. A callback that *throws* is treated exactly the same way (after
|
|
110
|
+
* one console warning): a cache-layer failure never fails a response the network answered
|
|
111
|
+
* well.
|
|
112
|
+
*/
|
|
113
|
+
shouldCache?: (response: Response, request: Request) => boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Tags attached to stored responses. Default: `['ky', '<method> <url>']`.
|
|
116
|
+
*
|
|
117
|
+
* The first tag lets one call drop the whole adapter's content
|
|
118
|
+
* (`cache.invalidateTags('ky')`); the second drops a single resource.
|
|
119
|
+
*/
|
|
120
|
+
tags?: (response: Response, request: Request) => readonly string[];
|
|
121
|
+
/**
|
|
122
|
+
* Request headers whose values participate in the cache key. Default: `['authorization']`.
|
|
123
|
+
*
|
|
124
|
+
* A personalized response must never be served to a different user just because the URL
|
|
125
|
+
* matched. Values are hashed, never stored verbatim.
|
|
126
|
+
*
|
|
127
|
+
* A response's own `Vary` header is checked against this list before it is stored: a
|
|
128
|
+
* response varying on anything *not* listed cannot be keyed correctly, so it is left
|
|
129
|
+
* uncached — and `Vary: *` is never stored at all (RFC 9111 §3).
|
|
130
|
+
*/
|
|
131
|
+
varyHeaders?: readonly string[];
|
|
132
|
+
/**
|
|
133
|
+
* Response header set to `HIT` / `MISS`. Default: `'x-blaze-cache'`.
|
|
134
|
+
* Pass `false` to disable the marker entirely.
|
|
135
|
+
*/
|
|
136
|
+
markerHeader?: string | false;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The call shapes ky's `beforeRequest` hook arrives in — one per major version:
|
|
140
|
+
*
|
|
141
|
+
* * **ky v1** — `(request, options, state)`, the {@link Request} first;
|
|
142
|
+
* * **ky v2** — a single `{request, options, retryCount}` state object.
|
|
143
|
+
*
|
|
144
|
+
* The adapter's hooks are typed against the union so the same function satisfies
|
|
145
|
+
* {@link KyHooks.BeforeRequestHook} and {@link Ky2Hooks.BeforeRequestHook} both, and pick
|
|
146
|
+
* the right shape at runtime by testing the first argument (see the asserts at the bottom
|
|
147
|
+
* of this file — they are checked against both ky majors at build time).
|
|
148
|
+
*/
|
|
149
|
+
export interface BlazeKyHookState {
|
|
150
|
+
/** The ky `Request` — final URL, method and merged headers. */
|
|
151
|
+
request: Request;
|
|
152
|
+
/** ky's normalized options (v1 passes them separately, v2 bundles them here). */
|
|
153
|
+
options?: unknown;
|
|
154
|
+
/** ky v2 only: the response, in `afterResponse` state. */
|
|
155
|
+
response?: Response;
|
|
156
|
+
/** ky v2 only: retries attempted so far. */
|
|
157
|
+
retryCount?: number;
|
|
158
|
+
}
|
|
159
|
+
/** A hook that may answer a request from the cache — returning a `Response` skips the network. */
|
|
160
|
+
export type BlazeKyBeforeRequestHook = (requestOrState: Request | BlazeKyHookState, options?: unknown) => Response | void | Promise<Response | void>;
|
|
161
|
+
/**
|
|
162
|
+
* A hook that stores a network response and stamps the `MISS` marker.
|
|
163
|
+
*
|
|
164
|
+
* The response arrives as a **clone** (in both ky majors), so reading it here never
|
|
165
|
+
* consumes the caller's body.
|
|
166
|
+
*/
|
|
167
|
+
export type BlazeKyAfterResponseHook = (requestOrState: Request | BlazeKyHookState, options?: unknown, responseOrState?: Response | BlazeKyHookState) => Response | void | Promise<Response | void>;
|
|
168
|
+
/** What {@link blazeKyHook} returns — spread straight into `ky.extend({hooks: …})`. */
|
|
169
|
+
export interface BlazeKyHooks {
|
|
170
|
+
beforeRequest: BlazeKyBeforeRequestHook[];
|
|
171
|
+
afterResponse: BlazeKyAfterResponseHook[];
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* {@link blazeKyHook}'s options plus every ky option, for {@link createBlazeKy} — one bag
|
|
175
|
+
* for both layers: `{prefixUrl, ttlMs, tags}`. ky's own options (including `hooks`, which
|
|
176
|
+
* are preserved and run *before* the cache hooks) keep their meaning.
|
|
177
|
+
*
|
|
178
|
+
* Which URL options exist depends on the installed ky major — `prefixUrl` in v1,
|
|
179
|
+
* `baseUrl`/`prefix` in v2 — because this type extends whatever `ky` resolves to.
|
|
180
|
+
*/
|
|
181
|
+
export interface CreateBlazeKyOptions extends KyOptions, BlazeKyCacheOptions {
|
|
182
|
+
}
|
|
183
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAoB,OAAO,IAAI,SAAS,EAAE,MAAM,IAAI,CAAC;AAGjE;;;;;;;;;;GAUG;AACH,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG,MAAM,CAAC;AAE3C;;;;;;GAMG;AACH,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,yFAAyF;IACzF,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC;;;;;OAKG;IACH,IAAI,EAAE,WAAW,CAAC;IAClB,+HAA+H;IAC/H,GAAG,EAAE,MAAM,CAAC;IACZ,uDAAuD;IACvD,UAAU,EAAE,OAAO,CAAC;IACpB,mDAAmD;IACnD,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;;;OAMG;IACH,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,wEAAwE;AACxE,MAAM,WAAW,gBAAgB;IAC/B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,GAAG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,CAAC,EAAE,QAAQ,CAAC;IACpB,4CAA4C;IAC5C,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,kGAAkG;AAClG,MAAM,MAAM,wBAAwB,GAAG,CACrC,cAAc,EAAE,OAAO,GAAG,gBAAgB,EAC1C,OAAO,CAAC,EAAE,OAAO,KACd,QAAQ,GAAG,IAAI,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAAC;AAEhD;;;;;GAKG;AACH,MAAM,MAAM,wBAAwB,GAAG,CACrC,cAAc,EAAE,OAAO,GAAG,gBAAgB,EAC1C,OAAO,CAAC,EAAE,OAAO,EACjB,eAAe,CAAC,EAAE,QAAQ,GAAG,gBAAgB,KAC1C,QAAQ,GAAG,IAAI,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAAC;AAEhD,uFAAuF;AACvF,MAAM,WAAW,YAAY;IAC3B,aAAa,EAAE,wBAAwB,EAAE,CAAC;IAC1C,aAAa,EAAE,wBAAwB,EAAE,CAAC;CAC3C;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,oBAAqB,SAAQ,SAAS,EAAE,mBAAmB;CAAG"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blaze-cache/ky",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.0-beta.0",
|
|
4
|
+
"description": "Cache ky HTTP responses via a WebAssembly cache — TTLs, tags, Cache-Control support and an x-blaze-cache HIT/MISS marker",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ky",
|
|
7
|
+
"fetch",
|
|
8
|
+
"http",
|
|
9
|
+
"request",
|
|
10
|
+
"cache",
|
|
11
|
+
"caching",
|
|
12
|
+
"hooks",
|
|
13
|
+
"cache-control",
|
|
14
|
+
"webassembly",
|
|
15
|
+
"wasm",
|
|
16
|
+
"ttl"
|
|
17
|
+
],
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/blaze-cache/blaze-cache.git",
|
|
22
|
+
"directory": "packages/ky"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://blaze-cache.github.io/docs/",
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"type": "module",
|
|
30
|
+
"main": "dist/index.js",
|
|
31
|
+
"types": "dist/index.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"import": "./dist/index.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist"
|
|
40
|
+
],
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@blaze-cache/core": "0.3.0-beta.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"ky": "^1.14.0 || ^2.0.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"ky": "^1.14.3",
|
|
49
|
+
"ky2": "npm:ky@^2.2.0",
|
|
50
|
+
"typescript": "^5.9.3"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsc -p tsconfig.json",
|
|
54
|
+
"test": "pnpm run build && node --test",
|
|
55
|
+
"typecheck": "tsc -p tsconfig.typecheck.json"
|
|
56
|
+
}
|
|
6
57
|
}
|