Kaynağa Gözat

Merge pull request #1015 from yunwuu/main

fix typo & add timeout to stuck request
Yifei Zhang 2 yıl önce
ebeveyn
işleme
2efba6ea6f
1 değiştirilmiş dosya ile 15 ekleme ve 3 silme
  1. 15 3
      scripts/fetch-prompts.mjs

+ 15 - 3
scripts/fetch-prompts.mjs

@@ -10,10 +10,20 @@ const RAW_EN_URL = "f/awesome-chatgpt-prompts/main/prompts.csv";
 const EN_URL = MIRRORF_FILE_URL + RAW_EN_URL;
 const FILE = "./public/prompts.json";
 
+const timeoutPromise = (timeout) => {
+  return new Promise((resolve, reject) => {
+    setTimeout(() => {
+      reject(new Error('Request timeout'));
+    }, timeout);
+  });
+};
+
 async function fetchCN() {
   console.log("[Fetch] fetching cn prompts...");
   try {
-    const raw = await (await fetch(CN_URL)).json();
+    // const raw = await (await fetch(CN_URL)).json();
+    const response = await Promise.race([fetch(CN_URL), timeoutPromise(5000)]);
+    const raw = await response.json();
     return raw.map((v) => [v.act, v.prompt]);
   } catch (error) {
     console.error("[Fetch] failed to fetch cn prompts", error);
@@ -24,13 +34,15 @@ async function fetchCN() {
 async function fetchEN() {
   console.log("[Fetch] fetching en prompts...");
   try {
-    const raw = await (await fetch(EN_URL)).text();
+    // const raw = await (await fetch(EN_URL)).text();
+    const response = await Promise.race([fetch(EN_URL), timeoutPromise(5000)]);
+    const raw = response.text();
     return raw
       .split("\n")
       .slice(1)
       .map((v) => v.split('","').map((v) => v.replace('"', "")));
   } catch (error) {
-    console.error("[Fetch] failed to fetch cn prompts", error);
+    console.error("[Fetch] failed to fetch en prompts", error);
     return [];
   }
 }