Revert "Cache github token"

This reverts commit 5059cffa15.
This commit is contained in:
johnd0e
2024-07-24 22:37:58 +02:00
parent 94ce00ec5d
commit b704d25a0a
2 changed files with 9 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
export default { export default {
async fetch (request, env) { async fetch (request) {
if (request.method === "OPTIONS") { if (request.method === "OPTIONS") {
return handleOPTIONS(request); return handleOPTIONS(request);
} }
@@ -11,33 +11,9 @@ export default {
status: 401 status: 401
}); });
} }
const tokenCache = env.KV; const { token, endpoints, errResponse } = await getToken(authKey);
let token, endpoint;
try {
token = await tokenCache.get(authKey);
if (token) {
endpoint = await tokenCache.get(authKey + "-ep");
}
} catch (e) {
console.error(e);
}
if (!token) {
let expires_at, endpoints, errResponse;
({ token, expires_at, endpoints, errResponse } = await getToken(authKey));
if (errResponse) { return errResponse; } if (errResponse) { return errResponse; }
endpoint = endpoints?.api; return makeRequest(request, url.pathname, token, endpoints?.api || "https://api.githubcopilot.com");
if (expires_at) {
try {
await tokenCache.put(authKey, token, { expiration: expires_at });
if (endpoint) {
await tokenCache.put(authKey + "-ep", endpoint, { expiration: expires_at });
}
} catch (e) {
console.error(e);
}
}
}
return makeRequest(request, url.pathname, token, endpoint || "https://api.githubcopilot.com");
} }
}; };

View File

@@ -3,6 +3,8 @@ main = "src/worker.mjs"
compatibility_date = "2024-01-17" compatibility_date = "2024-01-17"
no_bundle = true no_bundle = true
[[kv_namespaces]] # Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
binding = "KV" # Docs: https://developers.cloudflare.com/workers/runtime-apis/kv
id = "4e595a4637f048ed8c92bab1821c6e54" # [[kv_namespaces]]
# binding = "MY_KV_NAMESPACE"
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"