blob: f954e3a18a5e5ca3b6b4b9a8a6fcc9956911007b (
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
36
37
38
39
40
|
'use strict';
self.addEventListener('fetch', event => {
if (!event.request.url.match(/body-stream$/))
return;
var counter = 0;
const encoder = new TextEncoder();
const stream = new ReadableStream({ pull: controller => {
switch (++counter) {
case 1:
controller.enqueue(encoder.encode(''));
return;
case 2:
controller.enqueue(encoder.encode('chunk #1'));
return;
case 3:
controller.enqueue(encoder.encode(' '));
return;
case 4:
controller.enqueue(encoder.encode('chunk #2'));
return;
case 5:
controller.enqueue(encoder.encode(' '));
return;
case 6:
controller.enqueue(encoder.encode('chunk #3'));
return;
case 7:
controller.enqueue(encoder.encode(' '));
return;
case 8:
controller.enqueue(encoder.encode('chunk #4'));
return;
default:
controller.close();
}
}});
event.respondWith(new Response(stream));
});
|