-
Notifications
You must be signed in to change notification settings - Fork 0
/
www.js
37 lines (30 loc) · 827 Bytes
/
www.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
const buffer = new ArrayBuffer(10 * 1000 ** 2);
addEventListener("fetch", evt => {
evt.respondWith(handleRequest(evt.request));
});
function handleRequest(req) {
const {
searchParams: params
} = new URL(req.url);
let bytes = parseInt(params.get("bytes"), 10) || 0;
if (bytes <= 0) {
return new Response("bytes must be positive.", {
status: 400
});
}
const {
readable,
writable
} = new TransformStream();
const writer = writable.getWriter();
while (true) {
const isFinalWrite = bytes < buffer.byteLength;
if (isFinalWrite) {
writer.write(buffer.slice(0, bytes));
break;
}
writer.write(buffer);
bytes -= buffer.byteLength;
}
return new Response(readable);
}