From 6aaf83f3c211b3efea63d20f39a58f0c1ab6fa17 Mon Sep 17 00:00:00 2001 From: Fred Date: Thu, 14 Mar 2024 01:56:36 +0800 Subject: [PATCH] fix: fix upstash sync issue --- app/api/upstash/[action]/[...key]/route.ts | 11 ++++++----- app/api/webdav/[...path]/route.ts | 2 +- app/utils/cloud/upstash.ts | 16 ++++++++++------ app/utils/cloud/webdav.ts | 16 ++++++++++------ 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/app/api/upstash/[action]/[...key]/route.ts b/app/api/upstash/[action]/[...key]/route.ts index 6be243c92..fcfef4718 100644 --- a/app/api/upstash/[action]/[...key]/route.ts +++ b/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, { diff --git a/app/api/webdav/[...path]/route.ts b/app/api/webdav/[...path]/route.ts index cade9ab51..826e2df01 100644 --- a/app/api/webdav/[...path]/route.ts +++ b/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() ?? "", ); diff --git a/app/utils/cloud/upstash.ts b/app/utils/cloud/upstash.ts index f5579cea0..bf6147bd4 100644 --- a/app/utils/cloud/upstash.ts +++ b/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; }, }; } diff --git a/app/utils/cloud/webdav.ts b/app/utils/cloud/webdav.ts index 79fff9472..bc569de0e 100644 --- a/app/utils/cloud/webdav.ts +++ b/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; }, }; }