From 172c0b58158851771eb2276c693475b0d11f4d06 Mon Sep 17 00:00:00 2001 From: oddyamill <43600905+oddyamill@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:41:58 +0700 Subject: [PATCH] fix(worker): yandex cache --- cmd/worker/main.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/cmd/worker/main.ts b/cmd/worker/main.ts index a187e1e..4672610 100644 --- a/cmd/worker/main.ts +++ b/cmd/worker/main.ts @@ -15,7 +15,7 @@ function proxyRequest(url: URL, request: Request, cf: CfProperties, headers?: He return fetch(url, init) } -function proxyEdge(url: URL, request: Request, env: Env): Promise { +function proxyEdge(url: URL, request: Request, env: Env): Response | Promise { if (env.YANDEX !== undefined) { return proxyKiasuo(url, request, env, true) } @@ -29,7 +29,7 @@ function proxyEdge(url: URL, request: Request, env: Env): Promise { ) } -async function proxyKiasuo(url: URL, request: Request, env: Env, yandex?: boolean): Promise { +function proxyKiasuo(url: URL, request: Request, env: Env, yandex?: boolean): Response | Promise { const cf: CfProperties = {} if (request.headers.has(AUTH_HEADER)) { @@ -78,22 +78,26 @@ async function purgeCache(url: URL, request: Request, env: Env): Promise `https://${ORIGIN_DOMAIN}${route}${url.search}`) + + if (env.YANDEX !== undefined) { + urls.push(...urls.map((origin) => `${env.YANDEX}?origin=${encodeURIComponent(origin)}`)) + } + const response = await fetch(`https://api.cloudflare.com/client/v4/zones/${env.ZONE}/purge_cache`, { headers: { Authorization: (env.CLOUDFLARE[6] !== " " ? "Bearer " : "") + env.CLOUDFLARE, "Content-Type": "application/json", }, method: "POST", - body: JSON.stringify({ - files: CACHE_ROUTES.map((route) => `https://${ORIGIN_DOMAIN}${route}${url.search}`), - }), + body: JSON.stringify({ files: urls }), }) return new Response(null, { status: response.status }) } export default { - fetch(request, env): Promise { + fetch(request, env): Response | Promise { const url = new URL(request.url) if (url.pathname === "/internal/purge-cache") {