blob: 775efc0bbd207d5d79d54e864bc5b6d3ad60091b (
plain)
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
|
self.addEventListener('fetch', event => {
const path = event.request.url.match(/\/(?<name>[^\/]+)$/);
switch (path?.groups?.name) {
case 'constructed':
event.respondWith(new Response(new Uint8Array([1, 2, 3])));
break;
case 'forward':
event.respondWith(fetch('/common/text-plain.txt'));
break;
case 'stream':
event.respondWith((async() => {
const res = await fetch('/common/text-plain.txt');
const body = await res.body;
const reader = await body.getReader();
const stream = new ReadableStream({
async start(controller) {
while (true) {
const {done, value} = await reader.read();
if (done)
break;
controller.enqueue(value);
}
controller.close();
reader.releaseLock();
}
});
return new Response(stream);
})());
break;
default:
event.respondWith(fetch(event.request));
break;
}
});
|