-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
47 lines (35 loc) · 974 Bytes
/
worker.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
async function readUrl(url) {
const utf8Decoder = new TextDecoder("utf-8");
const response = await fetch(url);
if (!response.ok) return;
const re = /\n|\r|\r\n/gm;
let startIndex = 0;
let count = 0;
const reader = response.body.getReader();
let { value, done } = await reader.read();
value = value ? utf8Decoder.decode(value) : "";
for (;;) {
let result = re.exec(value);
if (!result) {
if (done) {
console.log("eof");
break;
}
let remainder = value.substring(startIndex);
console.log("reading...");
({ value, done } = await reader.read());
console.log("read", count);
value = remainder + (value ? utf8Decoder.decode(value) : "");
startIndex = re.lastIndex = 0;
continue;
}
count++;
}
if (startIndex < value.length) {
count++;
}
console.log("done, lines:", count);
}
self.addEventListener("message", (event) => {
readUrl(event.data.url);
});