-
Notifications
You must be signed in to change notification settings - Fork 115
/
cloudflare-worker.js
58 lines (51 loc) · 1.94 KB
/
cloudflare-worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
const fetchAPI = request.url.replace(url.host, 'api.openai.com');
// 部分代理工具,请求由浏览器发起,跨域请求时会先发送一个 preflight 进行检查,也就是 OPTIONS 请求
// 需要响应该请求,否则后续的 POST 会失败
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS',
'Access-Control-Allow-Headers': '*',
};
if (request.method === 'OPTIONS') return new Response(null, { headers: corsHeaders });
const authKey = request.headers.get('Authorization');
if (!authKey) return new Response("Not allowed", { status: 403 });
let contentType = request.headers.get('Content-Type')
if (contentType && contentType.startsWith("multipart/form-data")) {
let newRequest = new Request(fetchAPI, request);
return await fetch(newRequest);
}
let body;
if (request.method === 'POST') body = await request.json();
const payload = {
method: request.method,
headers: {
"Content-Type": "application/json",
Authorization: authKey,
},
body: typeof body === 'object' ? JSON.stringify(body) : '{}',
};
// 在 Cloudflare 中,HEAD 和 GET 请求带 body 会报错
if (['HEAD', 'GET'].includes(request.method)) delete payload.body;
// 入参中如果包含了 stream=true,则表现形式为流式输出
const response = await fetch(fetchAPI, payload);
if (body && body.stream !== true) {
const results = await response.json();
return new Response(JSON.stringify(results), {
status: response.status,
headers: {
"Content-Type": "application/json",
},
});
} else {
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
}
}