-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache-api-keyval.ts
More file actions
158 lines (128 loc) · 3.79 KB
/
cache-api-keyval.ts
File metadata and controls
158 lines (128 loc) · 3.79 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
/**
* Cache API key/value store - github.com/optimalisatie/Cache-API-Key-Value-Store
* Released under the terms of MIT license
*
* Copyright (C) 2018 github.com/optimalisatie
*/
const defaultStore = '--keyval';
const dateHeader = 'x-d';
const expireHeader = 'x-e';
const contentTypeHeader = 'Content-Type';
const slashKey = '/';
// open store
function getStore(store: string, key: string, callback: Function, value?: any, expire?: number): Promise<void> {
store = store || defaultStore;
return caches.open(store).then(cache => {
return callback(cache, new Request(['', defaultStore, key || ''].join(slashKey)), value, expire);
});
}
// get entry from cache
function cacheGet(cache: any, cache_key: string) {
return cache.match(cache_key).then(function(cachedata) {
// expired check
if (!cachedata || cacheExpired(cache, cache_key, cachedata)) {
return;
}
return cachedata.json();
});
}
// delete entry from cache
function cacheDel(cache: any, cache_key: string) {
cacheDelete(cache, cache_key);
}
// set entry in cache
function cacheSet(cache: any, cache_key: string, value: any, expire?: number) {
var headers = {};
// JSON
headers[contentTypeHeader] = 'application/json';
value = JSON.stringify(value);
// expire time
if (IS_INT(expire)) {
headers[dateHeader] = timestamp();
headers[expireHeader] = expire;
}
value = new Response(value, {
headers: headers
});
return cache.put(cache_key, value);
}
// delete expired
function cacheExpired(cache: any, cache_key: string, cachedata: any) {
if (cachedata) {
var headers = cachedata.headers;
// expired check
var exp = INT(headers.get(expireHeader));
var date = INT(headers.get(dateHeader));
if (IS_INT(exp)) {
// expired
if ((date + exp) < timestamp()) {
cacheDelete(cache, cache_key)
return 1;
}
}
}
}
// return keys
function cacheKeys(cache: any) {
return cache.keys().then(function(rawKeys) {
var keys = [];
rawKeys.forEach(function(key) {
key = new URL(key.url).pathname.split(slashKey);
key.shift();
key.shift();
keys.push(key.join(slashKey));
});
return keys;
});
}
// clear store
function cacheClear(cache: any) {
return cache.keys().then(function(keys) {
keys.forEach(function(key) {
cacheDelete(cache, key);
});
});
}
// prune expired entries
function cachePrune(cache: any) {
return cache.keys().then(function(keys) {
// read all entries which automatically verifies the expire date
keys.forEach(function(cache_key) {
cacheGet(cache, cache_key);
});
});
}
// delete from cache
function cacheDelete(cache: any,cache_key:string) {
cache.delete(cache_key);
}
// return timestamp
function timestamp() {
return Math.round(Date.now() / 1000);
}
// return INTEGER
function INT(num) {
return parseInt(num);
}
// return INTEGER
function IS_INT(num) {
return !isNaN(num);
}
export function get<Type>(key: string, store?: string): Promise<void> {
return getStore(store, key, cacheGet);
}
export function set(key: string, value: any, expire?: number, store?: string): Promise<void> {
return getStore(store, key, cacheSet, value, expire);
}
export function del(key: string, store?: string): Promise<void> {
return getStore(store, key, cacheDel);
}
export function clear(store?: string): Promise<void> {
return getStore(store, '', cacheClear);
}
export function prune(store?: string): Promise<void> {
return getStore(store, '', cachePrune);
}
export function keys(store?: string): Promise<void> {
return getStore(store, '', cacheKeys);
}