From f7e2b35f52b38d8c385f638ff5596f1ebae98f68 Mon Sep 17 00:00:00 2001 From: wood chen Date: Mon, 6 Jan 2025 18:16:02 +0800 Subject: [PATCH] refactor: remove api.js file containing API request handling logic --- functions/api.js | 74 ------------------------------------ functions/api/[[default]].js | 64 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 74 deletions(-) delete mode 100644 functions/api.js create mode 100644 functions/api/[[default]].js diff --git a/functions/api.js b/functions/api.js deleted file mode 100644 index 38b5a3e..0000000 --- a/functions/api.js +++ /dev/null @@ -1,74 +0,0 @@ -export async function onRequest(context) { - const { request, env } = context; - - // 获取原始URL并解析路径 - const url = new URL(request.url); - const path = url.pathname; - - // 确保环境变量存在 - if (!env.API_BASEURL) { - return new Response('API_BASEURL environment variable is not set', { status: 500 }); - } - - // 处理 WebSocket 升级请求 - if (request.headers.get('Upgrade') === 'websocket') { - const apiBaseUrl = new URL(env.API_BASEURL); - // 将 http/https 转换为 ws/wss - const wsBaseUrl = `${apiBaseUrl.protocol === 'https:' ? 'wss:' : 'ws:'}//${apiBaseUrl.host}`; - const targetURL = `${wsBaseUrl}${path}${url.search}`; - - return fetch(new Request(targetURL, { - method: request.method, - headers: request.headers, - body: request.body - })); - } - - // 处理普通 HTTP 请求 - const apiBaseUrl = new URL(env.API_BASEURL); - const targetURL = new URL(path, apiBaseUrl.origin); - targetURL.search = url.search; - - // 创建新的请求头,移除可能导致问题的头部 - const newHeaders = new Headers(request.headers); - newHeaders.delete('host'); - newHeaders.delete('cf-connecting-ip'); - newHeaders.delete('cf-ipcountry'); - - // 创建新的请求 - const newRequest = new Request(targetURL.toString(), { - method: request.method, - headers: newHeaders, - body: request.body, - }); - - try { - // 转发请求到目标服务器 - const response = await fetch(newRequest); - - // 创建新的响应头,设置 CORS 和其他必要的头部 - const newResponse = new Response(response.body, { - status: response.status, - statusText: response.statusText, - }); - - // 复制所有响应头 - for (const [key, value] of response.headers.entries()) { - newResponse.headers.set(key, value); - } - - // 设置 CORS 头 - newResponse.headers.set('Access-Control-Allow-Origin', '*'); - newResponse.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); - newResponse.headers.set('Access-Control-Allow-Headers', '*'); - - return newResponse; - } catch (error) { - return new Response(`Proxy error: ${error.message}`, { - status: 500, - headers: { - 'Access-Control-Allow-Origin': '*' - } - }); - } -} diff --git a/functions/api/[[default]].js b/functions/api/[[default]].js new file mode 100644 index 0000000..040a766 --- /dev/null +++ b/functions/api/[[default]].js @@ -0,0 +1,64 @@ +// 处理所有 HTTP 方法 +export const onRequest = async (context) => { + const { request, env } = context; + + // 确保环境变量存在 + if (!env.API_BASEURL) { + return new Response('API_BASEURL environment variable is not set', { status: 500 }); + } + + try { + // 构建目标URL + const url = new URL(request.url); + const apiBaseUrl = env.API_BASEURL.endsWith('/') ? env.API_BASEURL.slice(0, -1) : env.API_BASEURL; + const targetURL = apiBaseUrl + url.pathname + url.search; + + // 处理 WebSocket 升级请求 + if (request.headers.get('Upgrade') === 'websocket') { + const wsURL = targetURL.replace(/^http/, 'ws'); + return fetch(wsURL, request); + } + + // 创建新的请求 + const newRequest = new Request(targetURL, { + method: request.method, + headers: request.headers, + body: request.body, + redirect: 'follow', + }); + + // 发送请求到目标服务器 + const response = await fetch(newRequest); + + // 创建新的响应 + const newResponse = new Response(response.body, response); + + // 设置 CORS 头 + newResponse.headers.set('Access-Control-Allow-Origin', '*'); + newResponse.headers.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); + newResponse.headers.set('Access-Control-Allow-Headers', '*'); + + return newResponse; + + } catch (error) { + return new Response(`Proxy error: ${error.message}`, { + status: 500, + headers: { + 'Access-Control-Allow-Origin': '*' + } + }); + } +} + +// 处理 OPTIONS 请求 +export const onRequestOptions = async (context) => { + return new Response(null, { + status: 204, + headers: { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', + 'Access-Control-Allow-Headers': '*', + 'Access-Control-Max-Age': '86400', + }, + }); +} \ No newline at end of file