@nowarajs/elysia-cache 1.2.1 โ 1.2.3
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/CHANGELOG.md +5 -5
- package/dist/cache.d.ts +3 -3
- package/dist/index.js +38 -16
- package/dist/types/cacheItem.d.ts +7 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
|
|
2
|
-
## v1.2.
|
|
2
|
+
## v1.2.3
|
|
3
3
|
|
|
4
|
-
[compare changes](https://github.com/NowaraJS/elysia-cache/compare/v1.2.
|
|
4
|
+
[compare changes](https://github.com/NowaraJS/elysia-cache/compare/v1.2.2...v1.2.3)
|
|
5
5
|
|
|
6
|
-
###
|
|
6
|
+
### ๐ง Fixes
|
|
7
7
|
|
|
8
|
-
-
|
|
8
|
+
- **๐ง:** [fix add x-cache MISS to the first request] - Added a check to ensure 'x-cache' header is set to 'MISS' if not already defined. - This change enhances the clarity of cache status in responses. ([e0eb7cf](https://github.com/NowaraJS/elysia-cache/commit/e0eb7cf))
|
|
9
9
|
|
|
10
10
|
### ๐ Documentation
|
|
11
11
|
|
|
12
|
-
- **๐:** [
|
|
12
|
+
- **๐:** [Remove outdated v1.2.2 section from CHANGELOG] ([7d40946](https://github.com/NowaraJS/elysia-cache/commit/7d40946))
|
|
13
13
|
|
|
14
14
|
### โค๏ธ Contributors
|
|
15
15
|
|
package/dist/cache.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export declare const cache: ({ defaultTtl, prefix, store }?: CacheOptions) => El
|
|
|
5
5
|
decorator: {};
|
|
6
6
|
store: {
|
|
7
7
|
kvStore: import("@nowarajs/kv-store/types").KvStore | MemoryStore;
|
|
8
|
-
|
|
8
|
+
_cachedRoutes: Set<string>;
|
|
9
9
|
};
|
|
10
10
|
derive: {};
|
|
11
11
|
resolve: {};
|
|
@@ -20,7 +20,7 @@ export declare const cache: ({ defaultTtl, prefix, store }?: CacheOptions) => El
|
|
|
20
20
|
}>;
|
|
21
21
|
macroFn: {
|
|
22
22
|
readonly isCached: (enable: boolean | number) => {
|
|
23
|
-
afterHandle({ set, response, store }: {
|
|
23
|
+
afterHandle({ set, response, store, request }: {
|
|
24
24
|
body: unknown;
|
|
25
25
|
query: Record<string, string>;
|
|
26
26
|
params: Record<string, string>;
|
|
@@ -39,7 +39,7 @@ export declare const cache: ({ defaultTtl, prefix, store }?: CacheOptions) => El
|
|
|
39
39
|
request: Request;
|
|
40
40
|
store: {
|
|
41
41
|
kvStore: import("@nowarajs/kv-store/types").KvStore | MemoryStore;
|
|
42
|
-
|
|
42
|
+
_cachedRoutes: Set<string>;
|
|
43
43
|
};
|
|
44
44
|
status: <const Code extends number | keyof import("elysia").StatusMap, const T = Code extends 301 | 302 | 303 | 307 | 308 | 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 300 | 304 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 ? {
|
|
45
45
|
readonly 100: "Continue";
|
package/dist/index.js
CHANGED
|
@@ -10,29 +10,51 @@ var cache = ({
|
|
|
10
10
|
defaultTtl = 60,
|
|
11
11
|
prefix = "",
|
|
12
12
|
store = ":memory:"
|
|
13
|
-
} = {}) => new Elysia().state(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
} = {}) => new Elysia().state({
|
|
14
|
+
kvStore: store === ":memory:" ? new MemoryStore : store
|
|
15
|
+
}).state({
|
|
16
|
+
_cachedRoutes: new Set
|
|
17
|
+
}).onRequest(async ({ request, store: store2, set }) => {
|
|
18
|
+
const sanitizeUrl = new URL(request.url).pathname;
|
|
19
|
+
if (store2._cachedRoutes.has(`${request.method}:${sanitizeUrl}`)) {
|
|
20
|
+
const cacheKey = await generateCacheKey(request.clone());
|
|
21
|
+
const cachedData = await store2.kvStore.get(`${prefix}${cacheKey}`);
|
|
22
|
+
if (cachedData && typeof cachedData === "object" && "response" in cachedData && "metadata" in cachedData) {
|
|
23
|
+
const { response, metadata } = cachedData;
|
|
24
|
+
set.headers["cache-control"] = `max-age=${defaultTtl}, public`;
|
|
25
|
+
set.headers["x-cache"] = "HIT";
|
|
26
|
+
set.headers["etag"] = `"${prefix}${cacheKey}"`;
|
|
27
|
+
set.headers["expires"] = new Date(Date.now() + defaultTtl * 1000).toUTCString();
|
|
28
|
+
set.headers["last-modified"] = metadata.createdAt;
|
|
29
|
+
return response;
|
|
30
|
+
}
|
|
31
|
+
set.headers["x-cache"] = "MISS";
|
|
22
32
|
}
|
|
23
33
|
return;
|
|
24
34
|
}).macro({
|
|
25
35
|
isCached: (enable) => {
|
|
26
36
|
const ttl = typeof enable === "number" ? enable : enable ? defaultTtl : 0;
|
|
27
37
|
return {
|
|
28
|
-
async afterHandle({ set, response, store: store2 }) {
|
|
29
|
-
const
|
|
38
|
+
async afterHandle({ set, response, store: store2, request }) {
|
|
39
|
+
const sanitizeUrl = new URL(request.url).pathname;
|
|
40
|
+
if (!store2._cachedRoutes.has(`${request.method}:${sanitizeUrl}`))
|
|
41
|
+
store2._cachedRoutes.add(`${request.method}:${sanitizeUrl}`);
|
|
42
|
+
const cacheKey = await generateCacheKey(request.clone());
|
|
30
43
|
const now = new Date;
|
|
31
|
-
set.headers["
|
|
32
|
-
set.headers["
|
|
33
|
-
set.headers["
|
|
34
|
-
set.headers["
|
|
35
|
-
|
|
44
|
+
set.headers["cache-control"] = `max-age=${ttl}, public`;
|
|
45
|
+
set.headers["etag"] = `"${prefix}${cacheKey}"`;
|
|
46
|
+
set.headers["last-modified"] = now.toUTCString();
|
|
47
|
+
set.headers["expires"] = new Date(now.getTime() + ttl * 1000).toUTCString();
|
|
48
|
+
if (!set.headers["x-cache"])
|
|
49
|
+
set.headers["x-cache"] = "MISS";
|
|
50
|
+
const cacheData = {
|
|
51
|
+
response,
|
|
52
|
+
metadata: {
|
|
53
|
+
createdAt: now.toUTCString(),
|
|
54
|
+
ttl
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
await store2.kvStore.set(`${prefix}${cacheKey}`, cacheData, ttl);
|
|
36
58
|
}
|
|
37
59
|
};
|
|
38
60
|
}
|