blob: 056351322c69e6a73efdbb44033b470f098de999 (
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
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
|
'use strict';
importScripts("/resources/testharness.js");
const map = new Map();
self.addEventListener('fetch', event => {
const url = new URL(event.request.url);
if (!url.searchParams.has('stream')) return;
if (url.searchParams.has('observe-cancel')) {
const id = url.searchParams.get('id');
if (id === undefined) {
event.respondWith(new Error('error'));
return;
}
event.waitUntil(new Promise(resolve => {
map.set(id, {label: 'pending', resolve});
}));
const stream = new ReadableStream({
pull(c) {
if (url.searchParams.get('enqueue') === 'true') {
url.searchParams.delete('enqueue');
c.enqueue(new Uint8Array([65]));
}
},
cancel() {
map.get(id).label = 'cancelled';
}
});
event.respondWith(new Response(stream));
return;
}
if (url.searchParams.has('query-cancel')) {
const id = url.searchParams.get('id');
if (id === undefined) {
event.respondWith(new Error('error'));
return;
}
const entry = map.get(id);
if (entry === undefined) {
event.respondWith(new Error('not found'));
return;
}
map.delete(id);
entry.resolve();
event.respondWith(new Response(entry.label));
return;
}
if (url.searchParams.has('use-fetch-stream')) {
event.respondWith(async function() {
const response = await fetch('pass.txt');
return new Response(response.body);
}());
return;
}
const delayEnqueue = url.searchParams.has('delay');
const stream = new ReadableStream({
start(controller) {
const encoder = new TextEncoder();
const populate = () => {
controller.enqueue(encoder.encode('PASS'));
controller.close();
}
if (delayEnqueue) {
step_timeout(populate, 16);
}
else {
populate();
}
}
});
event.respondWith(new Response(stream));
});
|