-
Notifications
You must be signed in to change notification settings - Fork 15
/
worker.js
47 lines (43 loc) · 1.36 KB
/
worker.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
self.addEventListener('fetch', function(event) {
// 'blank.html' resolves for all paths.
// We use this to run scripts containing a dynamic import on a synthetic page which has the right URL to serve its depedencies.
if (/blank\.html$/.test(event.request.url)) {
event.respondWith(Promise.resolve(new Response(
`<!doctype html><meta charset=utf-8><title>realm</title>`,
{
headers: {
'Content-Type': 'text/html',
}
}
)));
return;
}
if (/\/SYNTHETIC\//.test(event.request.url)) {
event.respondWith(async function() {
const client = await clients.get(event.clientId);
const chan = new MessageChannel();
let resolve, reject;
const rv = new Promise((res, rej) => {
resolve = res; reject = rej;
});
setTimeout(() => reject('timed out'), 2000);
chan.port1.onmessage = e => {
if (!e.data.success) {
reject('not found');
return;
}
let text = e.data.data;
if (/blank.html$/.test(event.request.referrer)) {
text += '\n;$$testFinished();'; // This is such a hack...
}
resolve(new Response(text, {
headers: {
'Content-Type': 'application/javascript',
}
}));
};
client.postMessage(event.request.url, [chan.port2]);
return await rv;
}());
}
});