mirror of
https://gitea.com/PublicAffairs/openai-github-copilot.git
synced 2025-07-23 20:30:45 +02:00
@@ -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");
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -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"
|
||||||
|
Reference in New Issue
Block a user