-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
sw.js
227 lines (216 loc) · 7.16 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
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
const cacheList = {
shared: {
version: '1.0.6',
resources: [
'https://cdn.jsdelivr.net/npm/quartermoon@latest/css/quartermoon-variables.min.css',
'https://fonts.googleapis.com/icon?family=Material+Icons',
'https://fonts.gstatic.com/s/materialicons/v134/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2'
]
},
'img-viewer': {
version: '1.4.2',
resources: [
'../img-viewer/public/build/bundle.js',
'../img-viewer/public/build/bundle.css',
'../img-viewer/public/128.png',
'../img-viewer/public/512.png',
'../img-viewer/public/site.webmanifest',
'../img-viewer/public/'
]
},
'audio-player': {
version: '1.8.5',
resources: [
'../audio-player/public/build/bundle.js',
'../audio-player/public/build/bundle.css',
'../audio-player/public/128.png',
'../audio-player/public/512.png',
'../audio-player/public/build/cast.js',
'../audio-player/public/build/cast.html',
'../audio-player/public/site.webmanifest',
'../audio-player/public/'
]
},
'video-player': {
version: '2.0.9',
resources: [
'https://cdn.jsdelivr.net/npm/[email protected]/dist/anitomyscript.bundle.min.js',
'https://cdn.jsdelivr.net/npm/[email protected]/dist/anitomyscript.wasm',
'../video-player/public/lib/jassub-worker.js',
'../video-player/public/lib/jassub-worker.wasm',
'../video-player/public/lib/jassub-worker-modern.wasm',
'../video-player/public/lib/Roboto.ttf',
'../video-player/public/build/bundle.js',
'../video-player/public/build/bundle.css',
'../video-player/public/build/cast.js',
'../video-player/public/build/cast.html',
'../video-player/public/128.png',
'../video-player/public/512.png',
'../video-player/public/site.webmanifest',
'../video-player/public/'
]
},
'torrent-client': {
version: '1.2.12',
resources: [
'../torrent-client/public/build/bundle.js',
'../torrent-client/public/build/bundle.css',
'../torrent-client/public/128.png',
'../torrent-client/public/512.png',
'../torrent-client/public/site.webmanifest',
'../torrent-client/public/'
]
},
'screen-recorder': {
version: '1.2.1',
resources: [
'../screen-recorder/public/build/bundle.js',
'../screen-recorder/public/build/bundle.css',
'../screen-recorder/public/128.png',
'../screen-recorder/public/512.png',
'../screen-recorder/public/site.webmanifest',
'../screen-recorder/public/'
]
},
'manga-reader': {
version: '1.3.2',
resources: [
'../manga-reader/public/site.webmanifest',
'../manga-reader/public/128.png',
'../manga-reader/public/512.png',
'../manga-reader/public/',
'../manga-reader/public/build/bundle.js',
'../manga-reader/public/build/bundle.css'
]
}
}
self.addEventListener('install', event => {
event.waitUntil( // always cache shared resources first
caches.open('shared v.' + cacheList.shared.version).then(cache =>
cache.addAll(cacheList.shared.resources)
)
)
self.skipWaiting()
})
self.addEventListener('activate', event => {
event.waitUntil((async () => {
const keyList = await caches.keys()
const tabs = await self.clients.matchAll({ type: 'window' })
return Promise.all(keyList.map(async key => {
if (key) { // dump all outdates caches on load
const [name, version] = key.split(' v.')
if (cacheList[name].version !== version) {
caches.delete(key)
const cache = await caches.open(name + ' v.' + cacheList[name].version)
await cache.addAll(cacheList[name].resources)
for (const tab of tabs) {
if (tab.url.indexOf(location.origin + '/' + name) === 0) tab.navigate(tab.url)
}
}
}
}))
})())
self.clients.claim()
})
self.addEventListener('fetch', event => {
let res = proxyResponse(event)
if (!res) {
res = (async () => {
let match = await caches.match(event.request)
if (!match) { // not in cache
const url = event.request.url
if (url.indexOf(self.registration.scope) !== -1) { // in origin
const path = url.slice(self.registration.scope.length)
const app = path.slice(0, path.indexOf('/'))
if (cacheList[app]) { // in cachelist
const keys = await caches.keys()
if (!keys.includes(app + ' v.' + cacheList[app].version)) { // cache doesnt exist
const cache = await caches.open(app + ' v.' + cacheList[app].version)
await cache.addAll(cacheList[app].resources)
match = await caches.match(event.request)
}
}
}
}
return match || fetch(event.request)
})()
}
event.respondWith(res)
})
const portTimeoutDuration = 5000
let cancellable = false
function proxyResponse (event) {
const { url } = event.request
if (!(url.includes(self.registration.scope) && url.includes('/server/')) || url.includes('?')) return null
if (url.includes(self.registration.scope) && url.includes('/server/keepalive/')) return new Response()
if (url.includes(self.registration.scope + '/server/cancel/')) {
return new Response(new ReadableStream({
cancel () {
cancellable = true
}
}))
}
return serve(event)
}
async function serve ({ request }) {
const { url, method, headers, destination } = request
const clientlist = await clients.matchAll({ type: 'window', includeUncontrolled: true })
const [data, port] = await new Promise(resolve => {
// Use race condition for whoever controls the response stream
for (const client of clientlist) {
const messageChannel = new MessageChannel()
const { port1, port2 } = messageChannel
port1.onmessage = ({ data }) => {
resolve([data, port1])
}
client.postMessage({
url,
method,
headers: Object.fromEntries(headers.entries()),
scope: self.registration.scope,
destination,
type: 'server'
}, [port2])
}
})
let timeOut = null
const cleanup = () => {
port.postMessage(false) // send a cancel request
clearTimeout(timeOut)
port.onmessage = null
}
if (data.body !== 'STREAM') {
cleanup()
return new Response(data.body, data)
}
return new Response(new ReadableStream({
pull (controller) {
return new Promise(resolve => {
port.onmessage = ({ data }) => {
if (data) {
controller.enqueue(data) // data is Uint8Array
} else {
cleanup()
controller.close() // data is null, means the stream ended
}
resolve()
}
if (!cancellable) {
// firefox doesn't support cancelling of Readable Streams in service workers,
// so we just empty it after 5s of inactivity, the browser will request another port anyways
clearTimeout(timeOut)
if (destination !== 'document') {
timeOut = setTimeout(() => {
cleanup()
resolve()
}, portTimeoutDuration)
}
}
port.postMessage(true) // send a pull request
})
},
cancel () {
cleanup()
}
}), data)
}