fix: fix upstash sync issue

This commit is contained in:
Fred 2024-03-14 01:56:36 +08:00
parent 133ce39a13
commit 6aaf83f3c2
No known key found for this signature in database
GPG Key ID: 4DABDA85EF70EC71
4 changed files with 27 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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