-
-
Notifications
You must be signed in to change notification settings - Fork 814
Expand file tree
/
Copy pathcache.ts
More file actions
72 lines (65 loc) · 2.29 KB
/
cache.ts
File metadata and controls
72 lines (65 loc) · 2.29 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
import { defineHandler, handleCacheHeaders, toResponse } from "h3";
import { FastResponse } from "srvx";
import {
defineCachedFunction as _defineCachedFunction,
defineCachedHandler as _defineCachedHandler,
setStorage,
} from "ocache";
import { useNitroApp } from "./app.ts";
import { useStorage } from "./storage.ts";
import type { EventHandler, H3Event } from "h3";
import type { CacheOptions, CachedEventHandlerOptions } from "nitro/types";
let _storageReady = false;
/**
* Sanitize a cache group or name so it is safe for filesystem-based storage
* drivers (fs, fs-lite). Unstorage maps ":" to "/" on disk, so any "/" already
* present in the value would create unexpected directory nesting and cause
* ENOTDIR errors when a path is both a file and a prefix of another path.
*/
function sanitizeCacheKey(value: string | undefined): string | undefined {
return value?.replace(/\//g, "_");
}
function ensureStorage() {
if (_storageReady) {
return;
}
_storageReady = true;
const storage = useStorage();
setStorage({
get: (key) => storage.getItem(key) as any,
set: (key, value, opts) =>
storage.setItem(key, value as any, opts?.ttl ? { ttl: opts.ttl } : undefined),
});
}
function defaultOnError(error: unknown) {
console.error("[cache]", error);
useNitroApp().captureError?.(error as Error, { tags: ["cache"] });
}
export function defineCachedFunction<T, ArgsT extends unknown[] = any[]>(
fn: (...args: ArgsT) => T | Promise<T>,
opts: CacheOptions<T, ArgsT> = {}
): (...args: ArgsT) => Promise<T> {
ensureStorage();
return _defineCachedFunction(fn, {
group: "nitro-functions",
onError: defaultOnError,
...opts,
name: sanitizeCacheKey(opts.name),
});
}
export function defineCachedHandler(
handler: EventHandler,
opts: CachedEventHandlerOptions = {}
): EventHandler {
ensureStorage();
const ocacheHandler = _defineCachedHandler(handler as any, {
group: "nitro-handlers",
onError: defaultOnError,
toResponse: (value, event) => toResponse(value, event as H3Event),
createResponse: (body, init) => new FastResponse(body, init),
handleCacheHeaders: (event, conditions) => handleCacheHeaders(event as H3Event, conditions),
...opts,
name: sanitizeCacheKey(opts.name),
});
return defineHandler((event) => ocacheHandler(event as any));
}