mirror of
https://github.com/woodchen-ink/Edgeone_CleanCache.git
synced 2025-07-18 05:51:57 +08:00
重构清理缓存 API,更新请求处理方式,分离 OPTIONS 和 POST 请求的处理逻辑,并添加对其他 HTTP 方法的处理。
This commit is contained in:
parent
7ae6a8d466
commit
a7b1a7b04c
@ -1,10 +1,9 @@
|
|||||||
// 边缘函数 - 腾讯云 EdgeOne 缓存清理
|
// 腾讯云 V3 签名算法
|
||||||
async function qcloudV3Post(secretId, secretKey, service, bodyArray, headersArray) {
|
async function qcloudV3Post(secretId, secretKey, service, bodyArray, headersArray) {
|
||||||
const HTTPRequestMethod = "POST";
|
const HTTPRequestMethod = "POST";
|
||||||
const CanonicalURI = "/";
|
const CanonicalURI = "/";
|
||||||
const CanonicalQueryString = "";
|
const CanonicalQueryString = "";
|
||||||
|
|
||||||
// 按 ASCII 升序排序
|
|
||||||
const sortHeadersArray = Object.keys(headersArray)
|
const sortHeadersArray = Object.keys(headersArray)
|
||||||
.sort()
|
.sort()
|
||||||
.reduce((obj, key) => {
|
.reduce((obj, key) => {
|
||||||
@ -15,13 +14,11 @@ async function qcloudV3Post(secretId, secretKey, service, bodyArray, headersArra
|
|||||||
let SignedHeaders = "";
|
let SignedHeaders = "";
|
||||||
let CanonicalHeaders = "";
|
let CanonicalHeaders = "";
|
||||||
|
|
||||||
// 拼接键
|
|
||||||
for (const key in sortHeadersArray) {
|
for (const key in sortHeadersArray) {
|
||||||
SignedHeaders += key.toLowerCase() + ';';
|
SignedHeaders += key.toLowerCase() + ';';
|
||||||
}
|
}
|
||||||
SignedHeaders = SignedHeaders.slice(0, -1);
|
SignedHeaders = SignedHeaders.slice(0, -1);
|
||||||
|
|
||||||
// 拼接键和值
|
|
||||||
for (const key in sortHeadersArray) {
|
for (const key in sortHeadersArray) {
|
||||||
CanonicalHeaders += `${key.toLowerCase()}:${sortHeadersArray[key].toLowerCase()}\n`;
|
CanonicalHeaders += `${key.toLowerCase()}:${sortHeadersArray[key].toLowerCase()}\n`;
|
||||||
}
|
}
|
||||||
@ -34,7 +31,6 @@ async function qcloudV3Post(secretId, secretKey, service, bodyArray, headersArra
|
|||||||
const CanonicalRequest =
|
const CanonicalRequest =
|
||||||
`${HTTPRequestMethod}\n${CanonicalURI}\n${CanonicalQueryString}\n${CanonicalHeaders}\n${SignedHeaders}\n${HashedRequestPayload}`;
|
`${HTTPRequestMethod}\n${CanonicalURI}\n${CanonicalQueryString}\n${CanonicalHeaders}\n${SignedHeaders}\n${HashedRequestPayload}`;
|
||||||
|
|
||||||
// 时间戳
|
|
||||||
const RequestTimestamp = Math.floor(Date.now() / 1000);
|
const RequestTimestamp = Math.floor(Date.now() / 1000);
|
||||||
const formattedDate = new Date(RequestTimestamp * 1000).toISOString().split('T')[0];
|
const formattedDate = new Date(RequestTimestamp * 1000).toISOString().split('T')[0];
|
||||||
const Algorithm = "TC3-HMAC-SHA256";
|
const Algorithm = "TC3-HMAC-SHA256";
|
||||||
@ -48,7 +44,6 @@ async function qcloudV3Post(secretId, secretKey, service, bodyArray, headersArra
|
|||||||
const StringToSign =
|
const StringToSign =
|
||||||
`${Algorithm}\n${RequestTimestamp}\n${CredentialScope}\n${HashedCanonicalRequest}`;
|
`${Algorithm}\n${RequestTimestamp}\n${CredentialScope}\n${HashedCanonicalRequest}`;
|
||||||
|
|
||||||
// HMAC-SHA256 签名计算
|
|
||||||
async function hmac(key, string) {
|
async function hmac(key, string) {
|
||||||
const cryptoKey = await crypto.subtle.importKey(
|
const cryptoKey = await crypto.subtle.importKey(
|
||||||
'raw',
|
'raw',
|
||||||
@ -94,9 +89,8 @@ async function qcloudV3Post(secretId, secretKey, service, bodyArray, headersArra
|
|||||||
return headersArray;
|
return headersArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function handler(request) {
|
// 处理 OPTIONS 请求
|
||||||
// 处理 CORS 预检请求
|
export function onRequestOptions(context) {
|
||||||
if (request.method === 'OPTIONS') {
|
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
headers: {
|
headers: {
|
||||||
'Access-Control-Allow-Origin': '*',
|
'Access-Control-Allow-Origin': '*',
|
||||||
@ -104,11 +98,13 @@ export default async function handler(request) {
|
|||||||
'Access-Control-Allow-Headers': 'Content-Type',
|
'Access-Control-Allow-Headers': 'Content-Type',
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理 POST 请求
|
||||||
|
export async function onRequestPost(context) {
|
||||||
try {
|
try {
|
||||||
const data = await request.json();
|
const data = await context.request.json();
|
||||||
const { secretId, secretKey, zoneId, type, targets, method = 'invalidate' } = data;
|
const { secretId, secretKey, zoneId, type, targets } = data;
|
||||||
|
|
||||||
const service = "teo";
|
const service = "teo";
|
||||||
const host = "teo.tencentcloudapi.com";
|
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': '*',
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user