-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsw.js
119 lines (112 loc) · 3.03 KB
/
sw.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
importScripts('js/storage.js');
var myPath = location.origin + location.pathname;
myPath = myPath.match(/(.*\/)/)[1];
const IdbFolder = "idbSound/";
const CacheFolder = 'cacheControl/';
const AppName = 'speech2sing';
const Version = 'v1.0.1';
const CacheName = AppName + '-' + Version;
const AppFiles = [
'.',
'index.html',
'record.html',
'changepitch.html',
'pitch.html',
'segment.html',
'makesong.html',
'demo.html',
'lib/alertbox.js',
'lib/fft.js',
'lib/IndexedDB-getAll-shim.js',
'lib/audio-recorder-polyfill/index.js',
'lib/audio-recorder-polyfill/wave-encoder.js',
'js/forSafari.js',
'js/makesong.js',
'js/pitch_ui.js',
'js/pitch.js',
'js/pitchworker.js',
'js/record.js',
'js/segment.js',
'js/storage.js',
'css/alertbox.css',
'css/style.css'
];
self.addEventListener('fetch', function(event) {
var url = event.request.url;
console.log(url, new Date());
if (url.startsWith(myPath + IdbFolder)) {
event.respondWith(SoundDBUrl(url.substring(myPath.length + IdbFolder.length)));
return ;
}
else if (url.startsWith(myPath + CacheFolder)) {
event.respondWith(CacheControlUrl(url.substring(myPath.length + CacheFolder.length)));
return ;
}
event.respondWith(TryReadFromCache(event.request));
});
function SoundDBUrl(name) {
name = decodeURIComponent(name);
var good = 0;
return (
getSoundDB().then(function (db) {
good = 1;
return getSound(name);
}).then(function (sound) {
good = 2;
var ret = new Response(sound.file);
ret.headers.set('Content-Type', sound.file.type);
ret.headers.set('Content-Length', sound.file.size);
return ret;
}).catch(function (x) {
if (good == 0)
return new Response('indexeddb not usable', {status: 500});
if (good == 1)
return new Response('file not found', {status: 404});
})
);
}
function CacheControlUrl(url) {
if (url === 'update') {
return addFiles().then(function () {
return new Response('ok');
})['catch'](function (x) {
return new Response(x);
})
}
if (url === 'drop') {
return caches['delete'](CacheName).then(function () {
return new Response('ok');
});
}
return new Response('file not found', {status: 404});
}
function TryReadFromCache(req) {
return caches.open(CacheName).then(function (c) {
return c.match(req);
}).then(function (response) {
if (response) return response;
return fetch(req);
})['catch'](function () {
return new Response('file not found', {status: 404});
});
}
addEventListener('install', function (event) {
skipWaiting();
console.log("update or install files");
event.waitUntil(addFiles());
});
addEventListener('activate', function (event) {
event.waitUntil(clients.claim());
caches.keys().then(function (keys) {
keys.forEach(function (name) {
if (name.startsWith(AppName) && name !== CacheName) {
caches['delete'](name);
}
});
});
});
function addFiles() {
return caches.open(CacheName).then(function (c) {
return c.addAll(AppFiles);
});
}