mirror of
https://gitea.com/PublicAffairs/openai-github-copilot.git
synced 2025-07-23 12:23:08 +02:00
Cache github token
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
export default {
|
||||
async fetch (request) {
|
||||
async fetch (request, env) {
|
||||
if (request.method === "OPTIONS") {
|
||||
return handleOPTIONS(request);
|
||||
}
|
||||
@@ -14,8 +14,26 @@ export default {
|
||||
status: 401
|
||||
});
|
||||
}
|
||||
const { token, errResponse } = await getToken(authKey);
|
||||
if (errResponse) { return errResponse; }
|
||||
const tokenCache = env.KV;
|
||||
let token;
|
||||
try {
|
||||
token = await tokenCache.get(authKey);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
if (!token) {
|
||||
let errResponse;
|
||||
({ token, errResponse } = await getToken(authKey));
|
||||
if (errResponse) { return errResponse; }
|
||||
const expiration = token.match(/;exp=(\d+);/)?.[1];
|
||||
if (expiration) {
|
||||
try {
|
||||
await tokenCache.put(authKey, token, { expiration });
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return makeRequest(request, url.pathname, await makeHeaders(token));
|
||||
}
|
||||
};
|
||||
|
@@ -3,8 +3,6 @@ main = "src/worker.mjs"
|
||||
compatibility_date = "2024-01-17"
|
||||
no_bundle = true
|
||||
|
||||
# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
|
||||
# Docs: https://developers.cloudflare.com/workers/runtime-apis/kv
|
||||
# [[kv_namespaces]]
|
||||
# binding = "MY_KV_NAMESPACE"
|
||||
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||
[[kv_namespaces]]
|
||||
binding = "KV"
|
||||
id = "4e595a4637f048ed8c92bab1821c6e54"
|
||||
|
Reference in New Issue
Block a user