-
-
Notifications
You must be signed in to change notification settings - Fork 814
Expand file tree
/
Copy pathcache.ts
More file actions
347 lines (310 loc) · 10.4 KB
/
cache.ts
File metadata and controls
347 lines (310 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import { defineHandler, handleCacheHeaders, isHTTPEvent, toResponse } from "h3";
import { FastResponse } from "srvx";
import { parseURL } from "ufo";
import { hash } from "ohash";
import { useNitroApp } from "./app.ts";
import { useStorage } from "./storage.ts";
import type { H3Event, EventHandler, HTTPEvent } from "h3";
import type { TransactionOptions } from "unstorage";
import type {
CacheEntry,
CacheOptions,
CachedEventHandlerOptions,
ResponseCacheEntry,
} from "nitro/types";
function defaultCacheOptions() {
return {
name: "_",
base: "/cache",
swr: true,
maxAge: 1,
} as const;
}
type ResolvedCacheEntry<T> = CacheEntry<T> & { value: T };
export function defineCachedFunction<T, ArgsT extends unknown[] = any[]>(
fn: (...args: ArgsT) => T | Promise<T>,
opts: CacheOptions<T, ArgsT> = {}
): (...args: ArgsT) => Promise<T> {
opts = { ...defaultCacheOptions(), ...opts };
const pending: { [key: string]: Promise<T> } = {};
// Normalize cache params
const group = opts.group || "nitro/functions";
const name = opts.name || fn.name || "_";
const integrity = opts.integrity || hash([fn, opts]);
const validate = opts.validate || ((entry) => entry.value !== undefined);
async function get(
key: string,
resolver: () => T | Promise<T>,
shouldInvalidateCache?: boolean,
event?: HTTPEvent
): Promise<ResolvedCacheEntry<T>> {
// Use extension for key to avoid conflicting with parent namespace (foo/bar and foo/bar/baz)
const cacheKey = [opts.base, group, name, key + ".json"]
.filter(Boolean)
.join(":")
.replace(/:\/$/, ":index");
let entry: CacheEntry<T> =
((await useStorage()
.getItem(cacheKey)
.catch((error) => {
console.error(`[cache] Cache read error.`, error);
useNitroApp().captureError?.(error, { event, tags: ["cache"] });
})) as unknown) || {};
// https://github.com/nitrojs/nitro/issues/2160
if (typeof entry !== "object") {
entry = {};
const error = new Error("Malformed data read from cache.");
console.error("[cache]", error);
useNitroApp().captureError?.(error, { event, tags: ["cache"] });
}
const ttl = (opts.maxAge ?? 0) * 1000;
if (ttl) {
entry.expires = Date.now() + ttl;
}
const expired =
shouldInvalidateCache ||
entry.integrity !== integrity ||
(ttl && Date.now() - (entry.mtime || 0) > ttl) ||
validate(entry) === false;
const _resolve = async () => {
const isPending = pending[key];
if (!isPending) {
if (entry.value !== undefined && (opts.staleMaxAge || 0) >= 0 && opts.swr === false) {
// Remove cached entry to prevent using expired cache on concurrent requests
entry.value = undefined;
entry.integrity = undefined;
entry.mtime = undefined;
entry.expires = undefined;
}
pending[key] = Promise.resolve(resolver());
}
try {
entry.value = await pending[key];
} catch (error) {
// Make sure entries that reject get removed.
if (!isPending) {
delete pending[key];
}
// Re-throw error to make sure the caller knows the task failed.
throw error;
}
if (!isPending) {
// Update mtime, integrity + validate and set the value in cache only the first time the request is made.
entry.mtime = Date.now();
entry.integrity = integrity;
delete pending[key];
if (validate(entry) !== false) {
let setOpts: TransactionOptions | undefined;
if (opts.maxAge && !opts.swr /* TODO: respect staleMaxAge */) {
setOpts = { ttl: opts.maxAge };
}
const promise = useStorage()
.setItem(cacheKey, entry, setOpts)
.catch((error) => {
console.error(`[cache] Cache write error.`, error);
useNitroApp().captureError?.(error, { event, tags: ["cache"] });
});
if (typeof event?.req?.waitUntil === "function") {
event.req.waitUntil(promise);
}
}
}
};
const _resolvePromise = expired ? _resolve() : Promise.resolve();
if (entry.value === undefined) {
await _resolvePromise;
} else if (expired && event && event.req.waitUntil) {
event.req.waitUntil(_resolvePromise);
}
if (opts.swr && validate(entry) !== false) {
_resolvePromise.catch((error) => {
console.error(`[cache] SWR handler error.`, error);
useNitroApp().captureError?.(error, { event, tags: ["cache"] });
});
return entry as ResolvedCacheEntry<T>;
}
return _resolvePromise.then(() => entry) as Promise<ResolvedCacheEntry<T>>;
}
return async (...args) => {
const shouldBypassCache = await opts.shouldBypassCache?.(...args);
if (shouldBypassCache) {
return fn(...args);
}
const key = await (opts.getKey || getKey)(...args);
const shouldInvalidateCache = await opts.shouldInvalidateCache?.(...args);
const entry = await get(
key,
() => fn(...args),
shouldInvalidateCache,
args[0] && isHTTPEvent(args[0]) ? args[0] : undefined
);
let value = entry.value;
if (opts.transform) {
value = (await opts.transform(entry, ...args)) || value;
}
return value;
};
}
export function cachedFunction<T, ArgsT extends unknown[] = any[]>(
fn: (...args: ArgsT) => T | Promise<T>,
opts: CacheOptions<T> = {}
): (...args: ArgsT) => Promise<T | undefined> {
return defineCachedFunction(fn, opts);
}
function getKey(...args: unknown[]) {
return args.length > 0 ? hash(args) : "";
}
function escapeKey(key: string | string[]) {
return String(key).replace(/\W/g, "");
}
export function defineCachedHandler(
handler: EventHandler,
opts: CachedEventHandlerOptions = defaultCacheOptions()
): EventHandler {
const variableHeaderNames = (opts.varies || [])
.filter(Boolean)
.map((h) => h.toLowerCase())
.sort();
const _opts: CacheOptions<ResponseCacheEntry> = {
...opts,
shouldBypassCache: (event) => {
return event.req.method !== "GET" && event.req.method !== "HEAD";
},
getKey: async (event: H3Event) => {
// Custom user-defined key
const customKey = await opts.getKey?.(event);
if (customKey) {
return escapeKey(customKey);
}
// Auto-generated key
let _path: string;
if (opts.allowQuery) {
const params = new URLSearchParams();
for (const key of opts.allowQuery) {
const value = event.url.searchParams.get(key);
if (value !== null) {
params.set(key, value);
}
}
const search = params.size > 0 ? `?${params.toString()}` : "";
_path = event.url.pathname + search;
} else {
_path = event.url.pathname + event.url.search;
}
let _pathname: string;
try {
_pathname = escapeKey(decodeURI(parseURL(_path).pathname)).slice(0, 16) || "index";
} catch {
_pathname = "-";
}
const _hashedPath = `${_pathname}.${hash(_path)}`;
const _headers = variableHeaderNames
.map((header) => [header, event.req.headers.get(header)])
.map(([name, value]) => `${escapeKey(name as string)}.${hash(value)}`);
return [_hashedPath, ..._headers].join(":");
},
validate: (entry) => {
if (!entry.value) {
return false;
}
if (entry.value.status >= 400) {
return false;
}
if (entry.value.body === undefined) {
return false;
}
// https://github.com/nitrojs/nitro/pull/1857
if (
entry.value.headers.etag === "undefined" ||
entry.value.headers["last-modified"] === "undefined"
) {
return false;
}
return true;
},
group: opts.group || "nitro/handlers",
integrity: opts.integrity || hash([handler, opts]),
};
const _cachedHandler = cachedFunction<ResponseCacheEntry>(async (event: H3Event) => {
// Filter non variable headers
const filteredHeaders = [...event.req.headers.entries()].filter(
([key]) => !variableHeaderNames.includes(key.toLowerCase())
);
try {
const originalReq = event.req;
// @ts-expect-error assigning to publicly readonly property
event.req = new Request(event.req.url, {
method: event.req.method,
headers: filteredHeaders,
});
// Inherit srvx context
event.req.runtime = originalReq.runtime;
event.req.waitUntil = originalReq.waitUntil;
} catch (error) {
console.error("[cache] Failed to filter headers:", error);
}
// Call handler
const rawValue = await handler(event);
const res = await toResponse(rawValue, event);
// Stringified body
// TODO: support binary responses
const body = await res.text();
if (!res.headers.has("etag")) {
res.headers.set("etag", `W/"${hash(body)}"`);
}
if (!res.headers.has("last-modified")) {
res.headers.set("last-modified", new Date().toUTCString());
}
const cacheControl = [];
if (opts.swr) {
if (opts.maxAge) {
cacheControl.push(`s-maxage=${opts.maxAge}`);
}
if (opts.staleMaxAge) {
cacheControl.push(`stale-while-revalidate=${opts.staleMaxAge}`);
} else {
cacheControl.push("stale-while-revalidate");
}
} else if (opts.maxAge) {
cacheControl.push(`max-age=${opts.maxAge}`);
}
if (cacheControl.length > 0) {
res.headers.set("cache-control", cacheControl.join(", "));
}
const cacheEntry: ResponseCacheEntry = {
status: res.status,
statusText: res.statusText,
headers: Object.fromEntries(res.headers.entries()),
body,
};
return cacheEntry;
}, _opts);
return defineHandler(async (event) => {
// Headers-only mode
if (opts.headersOnly) {
// TODO: Send SWR too
if (handleCacheHeaders(event, { maxAge: opts.maxAge })) {
return;
}
return handler(event);
}
// Call with cache
const response = (await _cachedHandler(event))!;
// Check for cache headers
if (
handleCacheHeaders(event, {
modifiedTime: new Date(response.headers["last-modified"] as string),
etag: response.headers.etag as string,
maxAge: opts.maxAge,
})
) {
return;
}
// Send Response
return new FastResponse(response.body, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
});
}