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
|
import { getURLAndInit } from '../misc/request-helper';
const endpoint = '/logs';
const textDecoder = new TextDecoder('utf-8');
const getRandomStr = () => {
return Math.floor((1 + Math.random()) * 0x10000).toString(16);
};
let even = false;
let fetched = false;
let decoded = '';
function appendData(s, callback) {
let o;
try {
o = JSON.parse(s);
} catch (err) {
// eslint-disable-next-line no-console
console.log('JSON.parse error', JSON.parse(s));
}
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 }) => {
const str = textDecoder.decode(value, { stream: !done });
decoded += str;
const splits = decoded.split('\n');
const lastSplit = splits[splits.length - 1];
for (let i = 0; i < splits.length - 1; i++) {
appendData(splits[i], appendLog);
}
if (done) {
appendData(lastSplit, appendLog);
decoded = '';
// eslint-disable-next-line no-console
console.log('GET /logs streaming done');
fetched = false;
return;
} else {
decoded = lastSplit;
}
return pump(reader, appendLog);
});
}
const apiConfigSnapshot = {};
let controller;
function getWsUrl(apiConfig) {
const { hostname, port, secret, logLevel } = apiConfig;
let qs = '?level=' + logLevel;
if (typeof secret === 'string' && secret !== '') {
qs += '&token=' + secret;
}
return `ws://${hostname}:${port}${endpoint}${qs}`;
}
// 1 OPEN
// other value CLOSED
// similar to ws readyState but not the same
// https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState
let wsState;
function fetchLogs(apiConfig, appendLog) {
if (fetched || wsState === 1) return;
wsState = 1;
const url = getWsUrl(apiConfig);
const ws = new WebSocket(url);
ws.addEventListener('error', function(_ev) {
wsState = 3;
});
ws.addEventListener('close', function(_ev) {
wsState = 3;
fetchLogsWithFetch(apiConfig, appendLog);
});
ws.addEventListener('message', function(event) {
appendData(event.data, appendLog);
});
}
function fetchLogsWithFetch(apiConfig, appendLog) {
if (
controller &&
(apiConfigSnapshot.hostname !== apiConfig.hostname ||
apiConfigSnapshot.port !== apiConfig.port ||
apiConfigSnapshot.secret !== apiConfig.secret ||
apiConfigSnapshot.logLevel !== apiConfig.logLevel)
) {
controller.abort();
} else if (fetched) {
return;
}
fetched = true;
apiConfigSnapshot.hostname = apiConfig.hostname;
apiConfigSnapshot.port = apiConfig.port;
apiConfigSnapshot.secret = apiConfig.secret;
apiConfigSnapshot.logLevel = apiConfig.logLevel;
controller = new AbortController();
const signal = controller.signal;
const { url, init } = getURLAndInit(apiConfig);
fetch(url + endpoint + '?level=' + apiConfig.logLevel, {
...init,
signal
}).then(
response => {
const reader = response.body.getReader();
pump(reader, appendLog);
},
err => {
fetched = false;
if (signal.aborted) return;
// eslint-disable-next-line no-console
console.log('GET /logs error:', err.message);
}
);
}
export { fetchLogs };
|