WalleGriffkinder commited on
Commit
4e77bb3
·
verified ·
1 Parent(s): c021453

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +11 -16
index.js CHANGED
@@ -12,7 +12,6 @@ app.get('/', (req, res) => {
12
  const fetchWithRetry = async (url, options, maxRetries = 3) => {
13
  let lastChunkTime = Date.now();
14
  let totalTime = Date.now();
15
- let fullResponse = [];
16
  let currentRetry = 0;
17
 
18
  while(currentRetry < maxRetries) {
@@ -25,40 +24,36 @@ const fetchWithRetry = async (url, options, maxRetries = 3) => {
25
  }
26
  });
27
 
28
- const reader = response.body.getReader();
29
-
30
- while(true) {
31
- const {value, done} = await reader.read();
32
-
33
- if(done) break;
34
-
35
- if(value) {
36
- fullResponse.push(value);
37
- lastChunkTime = Date.now();
38
- }
39
 
40
  // Проверяем таймауты
41
  const chunkIdle = Date.now() - lastChunkTime;
42
  const totalElapsed = Date.now() - totalTime;
43
 
44
  if(chunkIdle > 2000 || totalElapsed > 6000) {
45
- reader.cancel();
46
  currentRetry++;
47
- fullResponse = []; // Сбрасываем буфер
48
  lastChunkTime = Date.now();
49
  totalTime = Date.now();
50
  break;
51
  }
52
  }
53
 
54
- // Если дошли сюда без breaks - значит успешно
55
- return Buffer.concat(fullResponse);
 
 
56
 
57
  } catch(error) {
58
  currentRetry++;
59
  if(currentRetry === maxRetries) throw error;
60
  }
61
  }
 
 
62
  };
63
 
64
  // Прокси для всех остальных путей
 
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) {
 
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
  // Прокси для всех остальных путей