-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
57 lines (47 loc) · 1.56 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
const controllerScope = new URL('fuse/', location)
const routes = [];
function addRoute(options, client) {
routes.push({
URLMatcher: new RegExp(options.url),
type: options.type,
controller: client.id
})
}
import JSONify from './jsonify.js'
addEventListener("install", async (event) => {
await skipWaiting();
});
addEventListener("activate", async (event) => {
await clients.claim();
});
const activeStreams = []
addEventListener("fetch", event => {
if (event.request.url.includes('fake')) {
event.respondWith((async () => {
const client = await clients.get(event.clientId)
client.postMessage(JSONify({ type: 'fetch', data: event }, [globalThis]))
const headers = new Headers({
'trailers': 'x-wat',
'x-lol': registration.scope,
})
headers.append('x-twat', 'mmh')
const stream = new ReadableStream({
async start(controller) {
controller.enqueue(Uint8Array.from([69, 70, 71]))
await new Promise(r => setTimeout(r, 1000))
controller.enqueue(Uint8Array.from([69, 70, 71]))
headers.append('x-wat', 'hmm')
controller.close()
}
})
return new Response(stream, {
status: 222,
statusText: 'Teapot go there',
headers,
})
})())
}
});
addEventListener("message", (event) => {
console.log('sw message', event);
});