fix(core): prevent hangs in avatar loading and Telegram API calls, add timeouts and concurrency
Browse files- frontend/src/main.js +11 -1
- src/core/generate.js +22 -12
- src/index.js +1 -1
- src/utils/loadFileFromURL.js +29 -27
frontend/src/main.js
CHANGED
|
@@ -310,6 +310,9 @@ async function generateQuote() {
|
|
| 310 |
|
| 311 |
const payload = buildApiPayload();
|
| 312 |
|
|
|
|
|
|
|
|
|
|
| 313 |
try {
|
| 314 |
let response;
|
| 315 |
// Attempt request to direct HF endpoint first (with CORS), fallback to proxy
|
|
@@ -318,15 +321,22 @@ async function generateQuote() {
|
|
| 318 |
method: 'POST',
|
| 319 |
headers: { 'Content-Type': 'application/json' },
|
| 320 |
body: JSON.stringify(payload),
|
|
|
|
| 321 |
});
|
| 322 |
-
} catch {
|
|
|
|
|
|
|
|
|
|
| 323 |
response = await fetch(PROXY_URL, {
|
| 324 |
method: 'POST',
|
| 325 |
headers: { 'Content-Type': 'application/json' },
|
| 326 |
body: JSON.stringify(payload),
|
|
|
|
| 327 |
});
|
| 328 |
}
|
| 329 |
|
|
|
|
|
|
|
| 330 |
if (!response.ok) {
|
| 331 |
throw new Error(`Server returned HTTP ${response.status}`);
|
| 332 |
}
|
|
|
|
| 310 |
|
| 311 |
const payload = buildApiPayload();
|
| 312 |
|
| 313 |
+
const controller = new AbortController();
|
| 314 |
+
const timeoutTimer = setTimeout(() => controller.abort(), 30000);
|
| 315 |
+
|
| 316 |
try {
|
| 317 |
let response;
|
| 318 |
// Attempt request to direct HF endpoint first (with CORS), fallback to proxy
|
|
|
|
| 321 |
method: 'POST',
|
| 322 |
headers: { 'Content-Type': 'application/json' },
|
| 323 |
body: JSON.stringify(payload),
|
| 324 |
+
signal: controller.signal,
|
| 325 |
});
|
| 326 |
+
} catch (directErr) {
|
| 327 |
+
if (directErr.name === 'AbortError') {
|
| 328 |
+
throw new Error('Request timeout setelah 30 detik');
|
| 329 |
+
}
|
| 330 |
response = await fetch(PROXY_URL, {
|
| 331 |
method: 'POST',
|
| 332 |
headers: { 'Content-Type': 'application/json' },
|
| 333 |
body: JSON.stringify(payload),
|
| 334 |
+
signal: controller.signal,
|
| 335 |
});
|
| 336 |
}
|
| 337 |
|
| 338 |
+
clearTimeout(timeoutTimer);
|
| 339 |
+
|
| 340 |
if (!response.ok) {
|
| 341 |
throw new Error(`Server returned HTTP ${response.status}`);
|
| 342 |
}
|
src/core/generate.js
CHANGED
|
@@ -128,19 +128,29 @@ class QuoteGenerate {
|
|
| 128 |
}
|
| 129 |
|
| 130 |
async callTelegramApi(method, params = {}) {
|
|
|
|
| 131 |
const url = `https://api.telegram.org/bot${this.telegramToken}/${method}`;
|
| 132 |
|
| 133 |
-
const
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
}
|
| 145 |
|
| 146 |
async avatarImageLatters(letters, color) {
|
|
@@ -819,7 +829,7 @@ class QuoteGenerate {
|
|
| 819 |
const avatarImage = await this.downloadAvatarImage(user);
|
| 820 |
|
| 821 |
if (avatarImage) {
|
| 822 |
-
const avatarSize = avatarImage.naturalHeight;
|
| 823 |
|
| 824 |
const canvas = createCanvas(avatarSize, avatarSize);
|
| 825 |
const canvasCtx = canvas.getContext('2d');
|
|
|
|
| 128 |
}
|
| 129 |
|
| 130 |
async callTelegramApi(method, params = {}) {
|
| 131 |
+
if (!this.telegramToken) return null;
|
| 132 |
const url = `https://api.telegram.org/bot${this.telegramToken}/${method}`;
|
| 133 |
|
| 134 |
+
const controller = new AbortController();
|
| 135 |
+
const timeout = setTimeout(() => controller.abort(), 5000);
|
| 136 |
+
|
| 137 |
+
try {
|
| 138 |
+
const response = await fetch(url, {
|
| 139 |
+
signal: controller.signal,
|
| 140 |
+
method: 'POST',
|
| 141 |
+
headers: {
|
| 142 |
+
'Content-Type': 'application/json',
|
| 143 |
+
},
|
| 144 |
+
body: JSON.stringify(params),
|
| 145 |
+
});
|
| 146 |
+
clearTimeout(timeout);
|
| 147 |
+
const data = await response.json();
|
| 148 |
+
if (!data.ok) throw new Error(data.description);
|
| 149 |
+
return data.result;
|
| 150 |
+
} catch (e) {
|
| 151 |
+
clearTimeout(timeout);
|
| 152 |
+
return null;
|
| 153 |
+
}
|
| 154 |
}
|
| 155 |
|
| 156 |
async avatarImageLatters(letters, color) {
|
|
|
|
| 829 |
const avatarImage = await this.downloadAvatarImage(user);
|
| 830 |
|
| 831 |
if (avatarImage) {
|
| 832 |
+
const avatarSize = avatarImage.naturalHeight || avatarImage.height || 500;
|
| 833 |
|
| 834 |
const canvas = createCanvas(avatarSize, avatarSize);
|
| 835 |
const canvasCtx = canvas.getContext('2d');
|
src/index.js
CHANGED
|
@@ -30,7 +30,7 @@ const queue = fastq.promise(async ({ key, c, body }) => {
|
|
| 30 |
} finally {
|
| 31 |
inflight.delete(key);
|
| 32 |
}
|
| 33 |
-
},
|
| 34 |
|
| 35 |
app.use('*', cors({
|
| 36 |
origin: '*',
|
|
|
|
| 30 |
} finally {
|
| 31 |
inflight.delete(key);
|
| 32 |
}
|
| 33 |
+
}, 4);
|
| 34 |
|
| 35 |
app.use('*', cors({
|
| 36 |
origin: '*',
|
src/utils/loadFileFromURL.js
CHANGED
|
@@ -1,33 +1,35 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
const
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
};
|
| 10 |
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
reject(err);
|
| 31 |
-
});
|
| 32 |
-
});
|
| 33 |
};
|
|
|
|
| 1 |
+
export default async (url, filter = false) => {
|
| 2 |
+
if (!url) throw new Error('URL is required');
|
| 3 |
|
| 4 |
+
// Support direct Base64 Data URL
|
| 5 |
+
if (url.startsWith('data:')) {
|
| 6 |
+
const base64Part = url.includes(',') ? url.split(',')[1] : url;
|
| 7 |
+
return Buffer.from(base64Part, 'base64');
|
| 8 |
+
}
|
|
|
|
| 9 |
|
| 10 |
+
const controller = new AbortController();
|
| 11 |
+
const timeout = setTimeout(() => controller.abort(), 6000);
|
| 12 |
|
| 13 |
+
try {
|
| 14 |
+
const response = await fetch(url, {
|
| 15 |
+
signal: controller.signal,
|
| 16 |
+
redirect: 'follow',
|
| 17 |
+
headers: {
|
| 18 |
+
'User-Agent':
|
| 19 |
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36',
|
| 20 |
+
},
|
| 21 |
+
});
|
| 22 |
|
| 23 |
+
clearTimeout(timeout);
|
| 24 |
|
| 25 |
+
if (!response.ok) {
|
| 26 |
+
throw new Error(`HTTP ${response.status}`);
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
const arrayBuffer = await response.arrayBuffer();
|
| 30 |
+
return Buffer.from(arrayBuffer);
|
| 31 |
+
} catch (err) {
|
| 32 |
+
clearTimeout(timeout);
|
| 33 |
+
throw err;
|
| 34 |
+
}
|
|
|
|
|
|
|
|
|
|
| 35 |
};
|