-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
202 lines (194 loc) · 8.68 KB
/
index.js
File metadata and controls
202 lines (194 loc) · 8.68 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
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
!(() => {
const ifObjectisJSON = function (obj) {
return (typeof obj === 'object' && obj !== null && obj.constructor === Object) || Array.isArray(obj)
}
function CacheDB(namespace, prefix, config) {
this.config = config || { auto: 0 };
this.config.auto = this.config.auto || 0
this.namespace = namespace || "CacheDBDefaultNameSpace";
this.prefix = prefix || "CacheDBDefaultPrefix";
this.has = async function (key) {
return new Promise((resolve, reject) => {
caches.open(this.namespace).then(cache => {
cache.match(new Request(`https://${this.prefix}/${encodeURIComponent(key)}`))
.then(response => resolve(response ? true : false))
}).catch(err => {
console.error(`CacheDB Has Error: Key "${key}" ${err}`);
reject(false)
});
})
}
this.read = async function (key, config) {
config = config || {};
config.type = (config.type || (this.config.auto ? "auto" : "text")).toLowerCase();
if (!await this.has(key)) {
if (typeof config.default !== 'undefined') {
await this.write(key, config.default, config);
return config.default;
} else {
console.error(`CacheDB Read Error: Key "${key}" not found and no default value provided.`);
return null;
}
}
return new Promise((resolve, reject) => {
caches.open(this.namespace)
.then(cache => {
cache.match(new Request(`https://${this.prefix}/${encodeURIComponent(key)}`))
.then(async response => {
if (config.type === 'auto') {
switch (response.headers.get('Content-Type')) {
case "application/json":
config.type = "json";
break;
case "application/octet-stream":
config.type = "arrayBuffer";
break;
case "application/octet-stream":
config.type = "blob";
break;
case "text/number":
config.type = "number";
break;
case "text/boolean":
config.type = "boolean";
break;
default:
config.type = "text";
break;
}
}
switch (config.type) {
case "number":
resolve(Number(await response.text()));
return;
case "json":
resolve(response.json());
return;
case "arrayBuffer":
resolve(response.arrayBuffer());
return;
case 'blob':
resolve(response.blob());
return;
case 'text':
resolve(response.text());
return;
case 'boolean':
resolve(await response.text() === "1");
return;
default:
resolve(response.body);
return;
}
})
})
.catch(err => {
console.error(`CacheDB Read Error: Key "${key}" ${err}`);
reject(null);
});
})
}
this.write = async function (key, value, config) {
config = config || {};
config.type = (config.type || (this.config.auto ? "auto" : "text")).toLowerCase();
if (config.type === 'auto')
config.type = ifObjectisJSON(value) ? 'json' : typeof value;
switch (config.type) {
case "json":
config.content_type = "application/json";
value = JSON.stringify(value);
break;
case "arrayBuffer":
config.content_type = "application/octet-stream";
break;
case 'blob':
config.content_type = "application/octet-stream";
break;
case 'number':
config.content_type = "text/number";
value = value.toString();
break;
case 'boolean':
config.content_type = "text/boolean";
value = value ? 1 : 0
break;
default:
break;
}
return new Promise((resolve, reject) => {
caches.open(this.namespace).then(cache => {
cache.put(
new Request(`https://${this.prefix}/${encodeURIComponent(key)}`),
new Response(value, {
headers: { 'Content-Type': config.content_type }
})).then(resolve(1))
}).catch(err => {
console.error(`CacheDB Write Error: Key "${key}" ${err}`);
reject(0)
});
})
}
this.delete = async function (key) {
return new Promise((resolve, reject) => {
caches.open(this.namespace).then(cache => {
cache.delete(new Request(`https://${this.prefix}/${encodeURIComponent(key)}`))
.then(resolve(true))
})
.catch(err => {
console.error(`CacheDB Delete Error: Key "${key}" ${err}`);
reject(false)
});
})
}
this.list = async function () {
return new Promise((resolve, reject) => {
caches.open(this.namespace).then(cache => {
cache.keys().then(keys => {
resolve(keys.map(key => decodeURIComponent(key.url.split('/').pop())));
})
}).catch(err => {
console.error(`CacheDB List Error: ${err}`);
reject([])
});
})
}
this.all = async function () {
const data = {};
return new Promise(async (resolve, reject) => {
this.list().then(async keys => {
await Promise.all(keys.map(async key => {
data[key] = await this.read(key);
}))
resolve(data);
}).catch(err => {
console.error(`CacheDB All Error: ${err}`);
reject([])
});
})
}
this.clear = async function () {
return new Promise((resolve, reject) => {
this.list().then(keys => {
keys.forEach(key => {
this.delete(key)
})
resolve(true);
}).catch(err => {
console.error(`CacheDB Clear Error: ${err}`);
reject(false)
});
})
}
this.destroy = async function () {
return new Promise((resolve, reject) => {
this.clear().then(() => {
caches.delete(this.namespace).then(resolve(true));
}).catch(err => {
console.error(`CacheDB Destroy Error: ${err}`);
reject(false)
});
})
}
}
self.CacheDB = CacheDB;//easy export
})();