generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.js
More file actions
64 lines (52 loc) · 2.01 KB
/
cache.js
File metadata and controls
64 lines (52 loc) · 2.01 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
const cache = new Map();
const cached = Symbol('cached');
const _getCachekey = req => `${req.method}:${req.url}`;
/**
*
* @param {Request} request
* @param {object} context
* @param {AbortController} context.controller
* @param {AbortSignal} context.signal
* @param {Function} context.resolve
*/
export function checkCacheItem(request, { signal, resolve }) {
const key = _getCachekey(request);
if (! (request instanceof Request)) {
throw new TypeError('Response must be a `Response` object.');
} else if ((request.method === 'GET' || request.method === 'DELETE') &&! signal.aborted && request.cache !== 'no-store' && cache.has(key)) {
const resp = cache.get(key);
const clone = resp.clone();
clone.headers.set('X-Last-Cached', new Date(resp[cached]).toISOString());
if (clone.bodyUsed) {
cache.delete(key);
} else {
resolve(clone);
}
}
}
/**
*
* @param {Response} response
* @param {object} context
* @param {Request} context.request
*/
export function setCacheItem(response, { request }) {
if (! (response instanceof Response)) {
throw new TypeError('Response must be a `Response` object.');
} else if (! (request instanceof Request)) {
throw new TypeError('Request must be a `Request` object.');
} else if (response.ok && response.headers.get('Cache-Control') !== 'no-store' && ! response.hasOwnProperty(cached) && (request.method === 'GET' || request.method === 'DELETE') && ! request.signal.aborted && ! response.bodyUsed && response.status !== 206) {
const clone = Object.defineProperty(response.clone(), cached, {
value: Date.now(),
enumerable: false,
});
clone.headers.delete('Content-Encoding');
cache.set(_getCachekey(request), clone);
}
}
export const getCacheSize = () => cache.size;
export const listCache = () => Array.from(cache.keys());
export const getCacheItem = key => cache.has(key) ? cache.get(key).clone() : null;
export const deleteCacheItem = key => cache.delete(key);
export const hasCacheItem = key => cache.has(key);
export const clearCache = () => cache.clear();