Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
|
@@ -9,53 +9,6 @@ app.get('/', (req, res) => {
|
|
| 9 |
res.send('Hello World');
|
| 10 |
});
|
| 11 |
|
| 12 |
-
const fetchWithRetry = async (url, options, maxRetries = 3) => {
|
| 13 |
-
let lastChunkTime = Date.now();
|
| 14 |
-
let totalTime = Date.now();
|
| 15 |
-
let currentRetry = 0;
|
| 16 |
-
|
| 17 |
-
while(currentRetry < maxRetries) {
|
| 18 |
-
try {
|
| 19 |
-
const response = await fetch(url, {
|
| 20 |
-
...options,
|
| 21 |
-
headers: {
|
| 22 |
-
...options.headers,
|
| 23 |
-
'Accept': 'text/event-stream'
|
| 24 |
-
}
|
| 25 |
-
});
|
| 26 |
-
|
| 27 |
-
let chunks = [];
|
| 28 |
-
for await (const chunk of response.body) {
|
| 29 |
-
chunks.push(chunk);
|
| 30 |
-
lastChunkTime = Date.now();
|
| 31 |
-
|
| 32 |
-
// Проверяем таймауты
|
| 33 |
-
const chunkIdle = Date.now() - lastChunkTime;
|
| 34 |
-
const totalElapsed = Date.now() - totalTime;
|
| 35 |
-
|
| 36 |
-
if(chunkIdle > 2000 || totalElapsed > 6000) {
|
| 37 |
-
chunks = [];
|
| 38 |
-
currentRetry++;
|
| 39 |
-
lastChunkTime = Date.now();
|
| 40 |
-
totalTime = Date.now();
|
| 41 |
-
break;
|
| 42 |
-
}
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
// Если дошли сюда со всеми чанками - возвращаем результат
|
| 46 |
-
if(chunks.length > 0) {
|
| 47 |
-
return Buffer.concat(chunks);
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
} catch(error) {
|
| 51 |
-
currentRetry++;
|
| 52 |
-
if(currentRetry === maxRetries) throw error;
|
| 53 |
-
}
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
throw new Error('Max retries reached');
|
| 57 |
-
};
|
| 58 |
-
|
| 59 |
// Прокси для всех остальных путей
|
| 60 |
app.all('*', async (req, res) => {
|
| 61 |
if (req.url === '/') return;
|
|
@@ -63,16 +16,28 @@ app.all('*', async (req, res) => {
|
|
| 63 |
try {
|
| 64 |
const url = `https://api.groq.com${req.url}`;
|
| 65 |
|
|
|
|
| 66 |
const headers = {...req.headers};
|
| 67 |
delete headers['host'];
|
| 68 |
delete headers['content-length'];
|
| 69 |
|
| 70 |
-
const
|
| 71 |
method: req.method,
|
| 72 |
headers,
|
| 73 |
body: req.method !== 'GET' ? req.body : undefined
|
| 74 |
});
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
res.send(data);
|
| 77 |
|
| 78 |
} catch (error) {
|
|
|
|
| 9 |
res.send('Hello World');
|
| 10 |
});
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
// Прокси для всех остальных путей
|
| 13 |
app.all('*', async (req, res) => {
|
| 14 |
if (req.url === '/') return;
|
|
|
|
| 16 |
try {
|
| 17 |
const url = `https://api.groq.com${req.url}`;
|
| 18 |
|
| 19 |
+
// Копируем и фильтруем заголовки
|
| 20 |
const headers = {...req.headers};
|
| 21 |
delete headers['host'];
|
| 22 |
delete headers['content-length'];
|
| 23 |
|
| 24 |
+
const response = await fetch(url, {
|
| 25 |
method: req.method,
|
| 26 |
headers,
|
| 27 |
body: req.method !== 'GET' ? req.body : undefined
|
| 28 |
});
|
| 29 |
|
| 30 |
+
// Streaming response
|
| 31 |
+
if (response.headers.get('content-type')?.includes('text/event-stream')) {
|
| 32 |
+
res.setHeader('Content-Type', 'text/event-stream');
|
| 33 |
+
response.body.pipe(res);
|
| 34 |
+
return;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
// Regular response
|
| 38 |
+
const data = await response.buffer();
|
| 39 |
+
res.status(response.status);
|
| 40 |
+
res.set('Content-Type', response.headers.get('content-type'));
|
| 41 |
res.send(data);
|
| 42 |
|
| 43 |
} catch (error) {
|