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") {
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, {

View File

@ -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() ?? "",
);

View File

@ -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;
},
};
}

View File

@ -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;
},
};
}