From a7b1a7b04cbedd7c310f1cb3b3de920f27716eb0 Mon Sep 17 00:00:00 2001 From: wood chen Date: Sat, 12 Jul 2025 05:40:36 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=B8=85=E7=90=86=E7=BC=93?= =?UTF-8?q?=E5=AD=98=20API=EF=BC=8C=E6=9B=B4=E6=96=B0=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=96=B9=E5=BC=8F=EF=BC=8C=E5=88=86=E7=A6=BB?= =?UTF-8?q?=20OPTIONS=20=E5=92=8C=20POST=20=E8=AF=B7=E6=B1=82=E7=9A=84?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E5=B9=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=AF=B9=E5=85=B6=E4=BB=96=20HTTP=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E5=A4=84=E7=90=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/api/clean-eo-cache.js | 45 +++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/functions/api/clean-eo-cache.js b/functions/api/clean-eo-cache.js index 69479a5..a96d40c 100644 --- a/functions/api/clean-eo-cache.js +++ b/functions/api/clean-eo-cache.js @@ -1,10 +1,9 @@ -// 边缘函数 - 腾讯云 EdgeOne 缓存清理 +// 腾讯云 V3 签名算法 async function qcloudV3Post(secretId, secretKey, service, bodyArray, headersArray) { const HTTPRequestMethod = "POST"; const CanonicalURI = "/"; const CanonicalQueryString = ""; - // 按 ASCII 升序排序 const sortHeadersArray = Object.keys(headersArray) .sort() .reduce((obj, key) => { @@ -15,13 +14,11 @@ async function qcloudV3Post(secretId, secretKey, service, bodyArray, headersArra let SignedHeaders = ""; let CanonicalHeaders = ""; - // 拼接键 for (const key in sortHeadersArray) { SignedHeaders += key.toLowerCase() + ';'; } SignedHeaders = SignedHeaders.slice(0, -1); - // 拼接键和值 for (const key in sortHeadersArray) { CanonicalHeaders += `${key.toLowerCase()}:${sortHeadersArray[key].toLowerCase()}\n`; } @@ -34,7 +31,6 @@ async function qcloudV3Post(secretId, secretKey, service, bodyArray, headersArra const CanonicalRequest = `${HTTPRequestMethod}\n${CanonicalURI}\n${CanonicalQueryString}\n${CanonicalHeaders}\n${SignedHeaders}\n${HashedRequestPayload}`; - // 时间戳 const RequestTimestamp = Math.floor(Date.now() / 1000); const formattedDate = new Date(RequestTimestamp * 1000).toISOString().split('T')[0]; const Algorithm = "TC3-HMAC-SHA256"; @@ -48,7 +44,6 @@ async function qcloudV3Post(secretId, secretKey, service, bodyArray, headersArra const StringToSign = `${Algorithm}\n${RequestTimestamp}\n${CredentialScope}\n${HashedCanonicalRequest}`; - // HMAC-SHA256 签名计算 async function hmac(key, string) { const cryptoKey = await crypto.subtle.importKey( 'raw', @@ -94,21 +89,22 @@ async function qcloudV3Post(secretId, secretKey, service, bodyArray, headersArra return headersArray; } -export default async function handler(request) { - // 处理 CORS 预检请求 - if (request.method === 'OPTIONS') { - return new Response(null, { - headers: { - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'POST, OPTIONS', - 'Access-Control-Allow-Headers': 'Content-Type', - } - }); - } +// 处理 OPTIONS 请求 +export function onRequestOptions(context) { + return new Response(null, { + headers: { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'POST, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type', + } + }); +} +// 处理 POST 请求 +export async function onRequestPost(context) { try { - const data = await request.json(); - const { secretId, secretKey, zoneId, type, targets, method = 'invalidate' } = data; + const data = await context.request.json(); + const { secretId, secretKey, zoneId, type, targets } = data; const service = "teo"; const host = "teo.tencentcloudapi.com"; @@ -153,3 +149,14 @@ export default async function handler(request) { }); } } + +// 处理其他 HTTP 方法 +export function onRequest(context) { + return new Response(JSON.stringify({ error: 'Only POST method is allowed' }), { + status: 405, + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + } + }); +}