-
Notifications
You must be signed in to change notification settings - Fork 496
/
cache.ts
350 lines (315 loc) · 9.6 KB
/
cache.ts
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
348
349
350
import { hash } from "ohash";
import {
handleCacheHeaders,
defineEventHandler,
createEvent,
EventHandler,
} from "h3";
import type { H3Event } from "h3";
import { parseURL } from "ufo";
import { useStorage } from "#internal/nitro";
export interface CacheEntry<T = any> {
value?: T;
expires?: number;
mtime?: number;
integrity?: string;
}
export interface CacheOptions<T = any> {
name?: string;
getKey?: (...args: any[]) => string;
transform?: (entry: CacheEntry<T>, ...args: any[]) => any;
validate?: (entry: CacheEntry<T>) => boolean;
shouldInvalidateCache?: (...args: any[]) => boolean;
group?: string;
integrity?: any;
maxAge?: number;
static?: boolean; // TODO
swr?: boolean;
staleMaxAge?: number;
base?: string;
}
const defaultCacheOptions = {
name: "_",
base: "/cache",
swr: true,
maxAge: 1,
};
export function defineCachedFunction<T = any>(
fn: (...args) => T | Promise<T>,
opts: CacheOptions<T>
) {
opts = { ...defaultCacheOptions, ...opts };
const pending: { [key: string]: Promise<T> } = {};
// Normalize cache params
const group = opts.group || "nitro";
const name = opts.name || fn.name || "_";
const integrity = hash([opts.integrity, fn, opts]);
const validate = opts.validate || (() => true);
async function get(
key: string,
resolver: () => T | Promise<T>,
shouldInvalidateCache?: boolean
): Promise<CacheEntry<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");
const entry: CacheEntry<T> =
((await useStorage().getItem(cacheKey)) as any) || {};
const ttl = (opts.maxAge ?? 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);
const _resolve = async () => {
const isPending = pending[key];
if (!isPending) {
if (entry.value !== undefined && (opts.staleMaxAge || 0) >= 0) {
// 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());
}
entry.value = await pending[key];
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)) {
useStorage()
.setItem(cacheKey, entry)
.catch((error) => console.error("[nitro] [cache]", error));
}
}
};
const _resolvePromise = expired ? _resolve() : Promise.resolve();
if (opts.swr && entry.value) {
// eslint-disable-next-line no-console
_resolvePromise.catch(console.error);
// eslint-disable-next-line unicorn/no-useless-promise-resolve-reject
return entry;
}
return _resolvePromise.then(() => entry);
}
return async (...args) => {
const key = (opts.getKey || getKey)(...args);
const shouldInvalidateCache = opts.shouldInvalidateCache?.(...args);
const entry = await get(key, () => fn(...args), shouldInvalidateCache);
let value = entry.value;
if (opts.transform) {
value = (await opts.transform(entry, ...args)) || value;
}
return value;
};
}
export const cachedFunction = defineCachedFunction;
function getKey(...args: string[]) {
return args.length > 0 ? hash(args, {}) : "";
}
export interface ResponseCacheEntry<T = any> {
body: T;
code: number;
headers: Record<string, string | number | string[]>;
}
export interface CachedEventHandlerOptions<T = any>
extends Omit<CacheOptions<ResponseCacheEntry<T>>, "transform" | "validate"> {
shouldInvalidateCache?: (event: H3Event) => boolean;
getKey?: (event: H3Event) => string;
headersOnly?: boolean;
}
function escapeKey(key: string) {
return key.replace(/[^\dA-Za-z]/g, "");
}
export function defineCachedEventHandler<T = any>(
handler: EventHandler<T>,
opts: CachedEventHandlerOptions<T> = defaultCacheOptions
): EventHandler<T> {
const _opts: CacheOptions<ResponseCacheEntry<T>> = {
...opts,
getKey: (event) => {
const key = opts.getKey?.(event);
if (key) {
return escapeKey(key);
}
const url = event.node.req.originalUrl || event.node.req.url;
const friendlyName = escapeKey(decodeURI(parseURL(url).pathname)).slice(
0,
16
);
const urlHash = hash(url);
return `${friendlyName}.${urlHash}`;
},
validate: (entry) => {
if (entry.value.code >= 400) {
return false;
}
if (entry.value.body === undefined) {
return false;
}
return true;
},
group: opts.group || "nitro/handlers",
integrity: [opts.integrity, handler],
};
const _cachedHandler = cachedFunction<ResponseCacheEntry<T>>(
async (incomingEvent: H3Event) => {
// Create proxies to avoid sharing state with user request
const reqProxy = cloneWithProxy(incomingEvent.node.req, { headers: {} });
const resHeaders: Record<string, number | string | string[]> = {};
let _resSendBody;
const resProxy = cloneWithProxy(incomingEvent.node.res, {
statusCode: 200,
getHeader(name) {
return resHeaders[name];
},
setHeader(name, value) {
resHeaders[name] = value as any;
return this;
},
getHeaderNames() {
return Object.keys(resHeaders);
},
hasHeader(name) {
return name in resHeaders;
},
removeHeader(name) {
delete resHeaders[name];
},
getHeaders() {
return resHeaders;
},
end(chunk, arg2?, arg3?) {
if (typeof chunk === "string") {
_resSendBody = chunk;
}
if (typeof arg2 === "function") {
arg2();
}
if (typeof arg3 === "function") {
arg3();
}
return this;
},
write(chunk, arg2?, arg3?) {
if (typeof chunk === "string") {
_resSendBody = chunk;
}
if (typeof arg2 === "function") {
arg2();
}
if (typeof arg3 === "function") {
arg3();
}
return this;
},
writeHead(statusCode, headers) {
this.statusCode = statusCode;
if (headers) {
for (const header in headers) {
this.setHeader(header, headers[header]);
}
}
return this;
},
});
// Call handler
const event = createEvent(reqProxy, resProxy);
event.context = incomingEvent.context;
const body = (await handler(event)) || _resSendBody;
// Collect cachable headers
const headers = event.node.res.getHeaders();
headers.etag = headers.Etag || headers.etag || `W/"${hash(body)}"`;
headers["last-modified"] =
headers["Last-Modified"] ||
headers["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) {
headers["cache-control"] = cacheControl.join(", ");
}
// Create cache entry for response
const cacheEntry: ResponseCacheEntry<T> = {
code: event.node.res.statusCode,
headers,
body,
};
return cacheEntry;
},
_opts
);
return defineEventHandler<T>(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);
// Don't continue if response is already handled by user
if (event.node.res.headersSent || event.node.res.writableEnded) {
return response.body;
}
// 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 status and headers
event.node.res.statusCode = response.code;
for (const name in response.headers) {
event.node.res.setHeader(name, response.headers[name]);
}
// Send body
return response.body;
});
}
function cloneWithProxy<T extends object = any>(
obj: T,
overrides: Partial<T>
): T {
return new Proxy(obj, {
get(target, property, receiver) {
if (property in overrides) {
return overrides[property];
}
return Reflect.get(target, property, receiver);
},
set(target, property, value, receiver) {
if (property in overrides) {
overrides[property] = value;
return true;
}
return Reflect.set(target, property, value, receiver);
},
});
}
export const cachedEventHandler = defineCachedEventHandler;