-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
362 lines (322 loc) · 11.6 KB
/
serviceworker.js
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* JIO Service Worker Storage Backend.
*/
// this polyfill provides Cache.add(), Cache.addAll(), and CacheStorage.match(),
// should not be needed for Chromium > 47 And Firefox > 39
// see https://developer.mozilla.org/en-US/docs/Web/API/Cache
// importScripts('./serviceworker-cache-polyfill.js');
// debug:
// chrome://cache/
// chrome://inspect/#service-workers
// chrome://serviceworker-internals/
//
// bar = new Promise(function (resolve, reject) {
// return caches.keys()
// .then(function (result) {
// console.log(result);
// return caches.open(result[0])
// .then(function(cache){
// return cache.keys()
// .then(function (request_list) {
// console.log(request_list);
// console.log("DONE");
// resolve();
// });
// });
// });
//});
// caches.keys().then(function(key_list) {console.log(key_list);return caches.open(key_list[0]);}).then(function(cache) {return cache.keys().then(function(request_list) {console.log(request_list); return cache.delete(request_list[0]);})});
// caches.keys().then(function(key_list) {console.log(key_list);return caches.open(key_list[0]);}).then(function(cache) {return cache.keys().then(function(request_list) {console.log(request_list);})});
// https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers
// http://www.html5rocks.com/en/tutorials/service-worker/introduction/
// versioning allows to keep a clean cache, current_cache is accessed on fetch
var CURRENT_CACHE_VERSION = 1;
var CURRENT_CACHE;
// runs while an existing worker runs or nothing controls the page (update here)
//self.addEventListener('install', function (event) {
// XXX CACHE SELF?
//});
// runs active page, changes here (like deleting old cache) breaks page
self.addEventListener('activate', function (event) {
// only validate against version, nothing else persists
event.waitUntil(caches.keys()
.then(function(cache_name_list) {
return Promise.all(
cache_name_list.map(function(cache_name) {
version = cache_name.split("-v")[1];
if (!(version && parseInt(version, 10) === CURRENT_CACHE_VERSION)) {
return caches.delete(cache_name);
}
})
);
})
);
});
// XXX build a server on fetch?
// intercept network requests, allows to serve form cache or fetch from network
/*
self.addEventListener('fetch', function (event) {
var url = event.request.url;
if (event.request.method === "GET") {
event.respondWith(
caches.open(CURRENT_CACHE)
.then(function(cache) {
return cache.match(event.request)
.then(function(response) {
if (response) {
return response;
// no cached response for event.request, fetch from network
} else {
// clone call, because any operation like fetch/put... will
// consume the request, so we need a copy of the original
// (see https://fetch.spec.whatwg.org/#dom-request-clone)
return fetch(event.request.clone()).then(function(response) {
// add resource to cache
cache.put(event.request, response.clone())
.then(function() {
return response;
});
});
}
})
.catch(function(error) {
// This catch() will handle exceptions that arise from the match()
// or fetch() operations. Note that a HTTP error response (e.g.
// 404) will NOT trigger an exception. It will return a normal
// response object that has the appropriate error code set.
throw error;
});
})
);
// we could also handle post with indexedDB here
} else {
event.respondWith(fetch(event.request));
}
});
*/
self.addEventListener('message', function (event) {
var param = event.data,
item,
mime_type,
result_list;
switch (param.command) {
// case 'post' not possible
// test if cache exits, only run ahead of put
case 'get':
caches.keys().then(function(key_list) {
var i, len;
CURRENT_CACHE = param.id + "-v" + CURRENT_CACHE_VERSION;
for (i = 0, len = key_list.length; i < len; i += 1) {
if (key_list[i] === CURRENT_CACHE) {
event.ports[0].postMessage({
error: null
});
}
}
// event.ports[0] corresponds to the MessagePort that was transferred
// as part of the controlled page's call to controller.postMessage().
// Therefore, event.ports[0].postMessage() will trigger the onmessage
// handler from the controlled page. It's up to you how to structure
// the messages that you send back; this is just one example.
event.ports[0].postMessage({
error: {
"status": 404,
"message": "Cache does not exist."
}
});
})
.catch(function(error) {
event.ports[0].postMessage({
error: {'message': error.toString()}
});
});
break;
// create new cache by opening it. this will only run once per cache/folder
case 'put':
CURRENT_CACHE = param.id + "-v" + CURRENT_CACHE_VERSION;
caches.open(CURRENT_CACHE)
.then(function() {
event.ports[0].postMessage({
error: null,
data: param.id
});
})
.catch(function(error) {
event.ports[0].postMessage({
error: {'message': error.toString()}
});
});
break;
// remove a cache
case 'remove':
CURRENT_CACHE = param.id + "-v" + CURRENT_CACHE_VERSION;
caches.delete(CURRENT_CACHE)
.then(function() {
event.ports[0].postMessage({
error: null
});
})
.catch(function(error) {
event.ports[0].postMessage({
error: {'message': error.toString()}
});
});
break;
// return list of caches ~ folders
case 'allDocs':
caches.keys().then(function(key_list) {
result_list = key_list.map(function(key) {
return {
"id": key.split("-v")[0],
"value": {}
};
});
event.ports[0].postMessage({
error: null,
data: result_list
});
})
.catch(function(error) {
event.ports[0].postMessage({
error: {'message': error.toString()}
});
});
break;
// return all urls stored in a cache
case 'allAttachments':
CURRENT_CACHE = param.id + "-v" + CURRENT_CACHE_VERSION;
// returns a list of the URLs corresponding to the Request objects
// that serve as keys for the current cache. We assume all files
// are kept in cache, so there will be no network requests.
caches.open(CURRENT_CACHE)
.then(function(cache) {
cache.keys()
.then(function (request_list) {
var result_list = request_list.map(function(request) {
return request.url;
}),
attachment_dict = {},
i,
len;
for (i = 0, len = result_list.length; i < len; i += 1) {
attachment_dict[result_list[i]] = {};
}
event.ports[0].postMessage({
error: null,
data: attachment_dict
});
});
})
.catch(function(error) {
event.ports[0].postMessage({
error: {'message': error.toString()}
});
});
break;
case 'removeAttachment':
CURRENT_CACHE = param.id + "-v" + CURRENT_CACHE_VERSION;
caches.open(CURRENT_CACHE)
.then(function(cache) {
request = new Request(param.name, {mode: 'no-cors'});
cache.delete(request)
.then(function(success) {
event.ports[0].postMessage({
error: success ? null : {
'status': 404,
'message': 'Item not found in cache.'
}
});
});
})
.catch(function(error) {
event.ports[0].postMessage({
error: {'message': error.toString()}
});
});
break;
case 'getAttachment':
CURRENT_CACHE = param.id + "-v" + CURRENT_CACHE_VERSION;
caches.open(CURRENT_CACHE)
.then(function(cache) {
return cache.match(param.name)
.then(function(response) {
// the response body is a ReadableByteStream which cannot be
// passed back through postMessage apparently. This link
// https://jakearchibald.com/2015/thats-so-fetch/ explains
// what can be done to get a Blob to return
// XXX Improve
// However, calling blob() does not allow to set mime-type, so
// for currently the blob is created, read, stored as new blob
// and returned (to be read again)
// https://github.com/whatwg/streams/blob/master/docs/ReadableByteStream.md
mime_type = response.headers.get('Content-Type');
return response.clone().blob();
})
.then(function (response_as_blob) {
return new Promise(function(resolve) {
var blob_reader = new FileReader();
blob_reader.onload = resolve;
blob_reader.readAsText(response_as_blob);
});
})
.then(function (reader_response) {
return new Blob([reader_response.target.result], {
"type": mime_type
});
})
.then(function (converted_response) {
if (converted_response) {
event.ports[0].postMessage({
error: null,
data: converted_response
});
} else {
event.ports[0].postMessage({
error: {
'status': 404,
'message': 'Item not found in cache.'
}
});
}
});
})
.catch(function(error) {
console.log("getAttachment error");
console.log(error);
event.ports[0].postMessage({
error: {'message': error.toString()}
});
});
break;
case 'putAttachment':
CURRENT_CACHE = param.id + "-v" + CURRENT_CACHE_VERSION;
caches.open(CURRENT_CACHE)
.then(function(cache) {
// If event.data.url isn't a valid URL, new Request() will throw a
// TypeError which will be handled by the outer .catch().
// Hardcode {mode: 'no-cors} since the default for new Requests
// constructed from strings is to require CORS, and we don't have any
// way of knowing whether an arbitrary URL that a user entered
// supports CORS.
request = new Request(param.name, {mode: 'no-cors'});
response = new Response(param.content);
return cache.put(request, response);
})
.then(function() {
event.ports[0].postMessage({
error: null
});
})
.catch(function(error) {
console.log("putAttachment error");
console.log(error);
event.ports[0].postMessage({
error: {'message': error.toString()}
});
});
break;
// refuse all else
default:
throw 'Unknown command: ' + event.data.command;
}
});