Fix: Improve status bar alignment and HTTP requests
This commit is contained in:
1 parent
a51de846bb
commit
ac98e481f1
3 files changed
+38
-39
No files matched your search
+18
-34
@@ -1,37 +1,21 @@
|
||||
import * as https from 'https';
|
||||
const URL = 'https://www.gymcontrol.se/global/checkedin/checkedin.php?uid=9094';
|
||||
|
||||
export function getVisitorCount(): Promise<number> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = https.get('https://www.gymcontrol.se/global/checkedin/checkedin.php?uid=9094', (res) => {
|
||||
if (!res.statusCode || res.statusCode < 200 || res.statusCode >= 300) {
|
||||
res.resume();
|
||||
reject(new Error(`HTTP ${res.statusCode ?? '?'}`));
|
||||
return;
|
||||
}
|
||||
|
||||
res.setEncoding('utf8');
|
||||
|
||||
let raw = '';
|
||||
res.on('data', (chunk) => {
|
||||
raw += chunk;
|
||||
});
|
||||
|
||||
res.on('end', () => {
|
||||
const text = raw.trim();
|
||||
const n = Number.parseInt(text, 10);
|
||||
|
||||
if (Number.isFinite(n)) {
|
||||
resolve(n);
|
||||
} else {
|
||||
reject(new Error(`Invalid response: "${text}"`));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', reject);
|
||||
|
||||
req.setTimeout(5000, () => {
|
||||
req.destroy(new Error('Timeout after 5000 ms'));
|
||||
});
|
||||
export async function getVisitorCount(): Promise<number> {
|
||||
const response = await fetch(URL, {
|
||||
signal: AbortSignal.timeout(5000)
|
||||
});
|
||||
|
||||
const raw = await response.text();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const text = raw.trim();
|
||||
|
||||
if (!/^\d+$/.test(text)) {
|
||||
throw new Error(`Invalid response: ${JSON.stringify(text)}`);
|
||||
}
|
||||
|
||||
return Number(text);
|
||||
}
|
||||
Reference in new issue
Block a user