-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.html
135 lines (116 loc) · 3.35 KB
/
stream.html
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE html>
<html>
<head>
<title>Fetch + Streams Demo</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<link href="https://resources.whatwg.org/logo-streams.svg" rel="icon">
<style>
body {
display: flex;
align-items: center;
justify-content: center;
margin: 0;
height: 100vh;
font-family: Arial;
}
form {
margin:0;
padding:0;
min-width:0;
font-size: 1.6em;
text-align: center;
display: flex;
align-items: center;
flex-direction: column;
}
input[type="text"] {
font-size: 1.2em;
max-width: 100%;
margin: 0.5em 0;
}
input[type="text"]:invalid {
outline: auto rgba(250,0,0,.5) 1px;
}
input[type="submit"] {
font-size: 1em;
max-width: 100%;
margin: .5em 0;
white-space: normal;
}
output {
font-family: monospace;
display: block;
margin-top: .5em;
}
progress {
margin: 0 1em;
display: inline-block;
}
</style>
</head>
<body>
<form id="form">
<label for="substring">Enter a substring to search in PI</label>
<input type="text" id="substring" pattern="[0-9\.]+" placeholder="3.14159" autocomplete="off" title="Enter only digits" required>
<!-- <output id="bytes" for="substring"> </output> -->
<input type="submit" name="sbm" value="Search, with the Power of Streams!">
<fieldset id="output" hidden="true">
<progress id="progress"></progress>
<output id="progressText"></output>
</fieldset>
</form>
<script>
// code heavily inspired from https://domenic.github.io/streams-demo/
const PI_URL = 'http://stuff.mit.edu/afs/sipb/contrib/pi/pi-billion.txt';
// const encoder = new TextEncoder();
const decoder = new TextDecoder();
// let bytesValue = new Uint8Array();
// substring.addEventListener('input', function () {
// bytesValue = encoder.encode(substring.value);
// bytes.value = Array.prototype.map.call(bytesValue, b => `0x${b.toString(16)}`).join(' ');
// });
if (location.hash) {
substring.value = location.hash.slice(1);
submit();
}
form.onsubmit = submit;
async function submit(e) {
if (e) e.preventDefault();
const controller = new AbortController();
output.hidden=false;
progressText.textContent = 'Performing fetch...';
form.sbm.value = 'Stop';
const cancel = e => {
if (e) { e.preventDefault(); output.hidden=true; }
controller.abort();
form.onsubmit = submit;
form.sbm.value = 'Search, with the Power of Streams!';
};
form.onsubmit = cancel;
const res = await fetch(`https://cors-anywhere.herokuapp.com/${PI_URL}`, {signal: controller.signal});
const contentLength = +res.headers.get('Content-Length');
const reader = res.body.getReader();
let total = 0;
progress.max = contentLength;
const substringRe = new RegExp(substring.value.replace('.','\\.'));
form.onsubmit = cancel;
// not necessary to call reader.cancel(); on cancel, it's handled by AbortController
form.sbm.value = 'Stop streaming';
let done, value;
do {
({done, value} = await reader.read());
total += value.byteLength;
progressText.textContent = `${total}${contentLength?'/'+contentLength:''} bytes received`;
progress.value = contentLength ? total : null;
const m = decoder.decode(value).match(substringRe);
if (m) {
progressText.textContent = `Found it! At position ${total - value.byteLength + m.index}.`;
cancel();
}
} while (!done);
progressText.textContent = `All done! (${total} bytes total)`;
}
</script>
</body>
</html>