瀏覽代碼

fix: fix upstash sync issue

Fred 1 年之前
父節點
當前提交
6aaf83f3c2
共有 4 個文件被更改,包括 27 次插入18 次删除
  1. 6 5
      app/api/upstash/[action]/[...key]/route.ts
  2. 1 1
      app/api/webdav/[...path]/route.ts
  3. 10 6
      app/utils/cloud/upstash.ts
  4. 10 6
      app/utils/cloud/webdav.ts

+ 6 - 5
app/api/upstash/[action]/[...key]/route.ts

@@ -10,7 +10,7 @@ async function handle(
   if (req.method === "OPTIONS") {
     return NextResponse.json({ body: "OK" }, { status: 200 });
   }
-  const [action, ...key] = params.key;
+  const [...key] = params.key;
   // only allow to request to *.upstash.io
   if (!endpoint || !new URL(endpoint).hostname.endsWith(".upstash.io")) {
     return NextResponse.json(
@@ -25,7 +25,8 @@ async function handle(
   }
 
   // only allow upstash get and set method
-  if (action !== "get" && action !== "set") {
+  if (params.action !== "get" && params.action !== "set") {
+    console.log("[Upstash Route] forbidden action ", params.action);
     return NextResponse.json(
       {
         error: true,
@@ -37,10 +38,9 @@ async function handle(
     );
   }
 
-  const [protocol, ...subpath] = params.key;
-  const targetUrl = `${protocol}://${subpath.join("/")}`;
+  const targetUrl = `${endpoint}/${params.action}/${params.key.join("/")}`;
 
-  const method = req.headers.get("method") ?? undefined;
+  const method = req.method;
   const shouldNotHaveBody = ["get", "head"].includes(
     method?.toLowerCase() ?? "",
   );
@@ -55,6 +55,7 @@ async function handle(
     duplex: "half",
   };
 
+  console.log("[Upstash Proxy]", targetUrl, fetchOptions);
   const fetchResult = await fetch(targetUrl, fetchOptions);
 
   console.log("[Any Proxy]", targetUrl, {

+ 1 - 1
app/api/webdav/[...path]/route.ts

@@ -80,7 +80,7 @@ async function handle(
 
   const targetUrl = `${protocol}://${endpoint + endpointPath}`;
 
-  const method = req.headers.get("method") ?? undefined;
+  const method = req.method;
   const shouldNotHaveBody = ["get", "head"].includes(
     method?.toLowerCase() ?? "",
   );

+ 10 - 6
app/utils/cloud/upstash.ts

@@ -92,12 +92,16 @@ export function createUpstashClient(store: SyncStore) {
         proxyUrl += "/";
       }
 
-      let url = new URL(proxyUrl + "/api/upstash/" + path);
-
-      // add query params
-      url.searchParams.append("endpoint", config.endpoint);
-
-      return url.toString();
+      let url;
+      if (proxyUrl.length > 0 || proxyUrl === "/") {
+        let u = new URL(proxyUrl + "/api/upstash/" + path);
+        // add query params
+        u.searchParams.append("endpoint", config.endpoint);
+        url = u.toString();
+      } else {
+        url = "/api/upstash/" + path + "?endpoint=" + config.endpoint;
+      }
+      return url;
     },
   };
 }

+ 10 - 6
app/utils/cloud/webdav.ts

@@ -67,12 +67,16 @@ export function createWebDavClient(store: SyncStore) {
         proxyUrl += "/";
       }
 
-      let url = new URL(proxyUrl + "/api/webdav/" + path);
-
-      // add query params
-      url.searchParams.append("endpoint", config.endpoint);
-
-      return url + path;
+      let url;
+      if (proxyUrl.length > 0 || proxyUrl === "/") {
+        let u = new URL(proxyUrl + "/api/webdav/" + path);
+        // add query params
+        u.searchParams.append("endpoint", config.endpoint);
+        url = u.toString();
+      } else {
+        url = "/api/upstash/" + path + "?endpoint=" + config.endpoint;
+      }
+      return url;
     },
   };
 }