blob: c8c359306c36cd06d3ae71176e38f0265d602f06 (
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
|
import { getURLAndInit } from 'm/request-helper';
const endpoint = '/logs';
const textDecoder = new TextDecoder('utf-8', { stream: true });
const getRandomStr = () => {
return Math.floor((1 + Math.random()) * 0x10000).toString(16);
};
let even = false;
let fetched = false;
function appendData(o, callback) {
const now = new Date();
const time = now.toLocaleString('zh-Hans');
// mutate input param in place intentionally
o.time = time;
o.id = now - 0 + getRandomStr();
o.even = even = !even;
callback(o);
}
function pump(reader, appendLog) {
return reader.read().then(({ done, value }) => {
if (done) {
// eslint-disable-next-line no-console
console.log('GET /logs streaming done');
return;
}
const t = textDecoder.decode(value);
const arrRawJSON = t.trim().split('\n');
arrRawJSON.forEach(s => {
try {
appendData(JSON.parse(s), appendLog);
} catch (err) {
// eslint-disable-next-line no-console
console.log('JSON.parse error', JSON.parse(s));
}
});
return pump(reader, appendLog);
});
}
function fetchLogs(apiConfig, appendLog) {
if (fetched) return;
fetched = true;
const { url, init } = getURLAndInit(apiConfig);
fetch(url + endpoint, init)
.then(response => {
const reader = response.body.getReader();
pump(reader, appendLog);
})
.catch(err => {
fetched = false;
// eslint-disable-next-line no-console
console.log('GET /logs error', err);
});
}
export { fetchLogs };
|