Skip to content

Commit ffb8e2f

Browse files
committed
fix: 修复部署问题
1 parent ba1f414 commit ffb8e2f

File tree

3 files changed

+170
-10
lines changed

3 files changed

+170
-10
lines changed

edgeone.json

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
11
{
2-
"functions": [
2+
"name": "netease-api-enhanced",
3+
"buildCommand": "mkdir -p functions/api && cp -r module util plugins public data server.js package.json functions/api/",
4+
"installCommand": "cd functions/api && npm install --only=production",
5+
"outputDirectory": "functions/api",
6+
"nodeVersion": "18.20.4",
7+
"headers": [
38
{
4-
"path": "/(.*)",
5-
"method": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
6-
"handler": "index.js",
7-
"headers": {
8-
"Access-Control-Allow-Credentials": "true",
9-
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
10-
"Access-Control-Allow-Headers": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version"
11-
}
9+
"source": "/*",
10+
"headers": [
11+
{
12+
"key": "Access-Control-Allow-Credentials",
13+
"value": "true"
14+
},
15+
{
16+
"key": "Access-Control-Allow-Origin",
17+
"value": "*"
18+
},
19+
{
20+
"key": "Access-Control-Allow-Methods",
21+
"value": "GET,POST,PUT,DELETE,OPTIONS"
22+
},
23+
{
24+
"key": "Access-Control-Allow-Headers",
25+
"value": "X-CSRF-Token,X-Requested-With,Accept,Accept-Version,Content-Length,Content-MD5,Content-Type,Date,X-Api-Version"
26+
},
27+
{
28+
"key": "Cache-Control",
29+
"value": "no-cache"
30+
}
31+
]
32+
}
33+
],
34+
"caches": [
35+
{
36+
"source": "/public/*",
37+
"cacheTtl": 86400
38+
}
39+
],
40+
"rewrites": [
41+
{
42+
"source": "/*",
43+
"destination": "/api/[...default]"
1244
}
1345
]
1446
}

functions/api/[[default]].js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// 边缘函数入口
2+
const { serveNcmApi } = require('../../server')
3+
4+
// 默认配置
5+
const defaultEnv = {
6+
ENABLE_GENERAL_UNBLOCK: 'true',
7+
ENABLE_FLAC: 'true',
8+
SELECT_MAX_BR: 'true',
9+
UNBLOCK_SOURCE: 'pyncmd,qq,kuwo,migu,kugou',
10+
FOLLOW_SOURCE_ORDER: 'true',
11+
CORS_ALLOW_ORIGIN: '*',
12+
ENABLE_PROXY: 'false',
13+
PROXY_URL: '',
14+
NETEASE_COOKIE: '',
15+
JOOX_COOKIE: '',
16+
MIGU_COOKIE: '',
17+
QQ_COOKIE: '',
18+
YOUTUBE_KEY: ''
19+
}
20+
21+
let app = null
22+
23+
export async function onRequest(context) {
24+
const { request, env } = context
25+
26+
// 合并默认配置和环境变量
27+
Object.keys(defaultEnv).forEach(key => {
28+
process.env[key] = env[key] || defaultEnv[key]
29+
})
30+
31+
if (!app) {
32+
app = await serveNcmApi({
33+
checkVersion: false // 禁用版本检查,避免在边缘函数环境中发起不必要的网络请求
34+
})
35+
}
36+
37+
// 解析请求体
38+
let body = []
39+
if (request.method !== 'GET' && request.method !== 'HEAD') {
40+
const buffer = await request.arrayBuffer()
41+
if (buffer.byteLength > 0) {
42+
body = Buffer.from(buffer)
43+
}
44+
}
45+
46+
// 构造 Express 兼容的请求对象
47+
const req = new Proxy(request, {
48+
get: (target, prop) => {
49+
switch(prop) {
50+
case 'body':
51+
return body
52+
case 'query':
53+
return Object.fromEntries(new URL(request.url).searchParams)
54+
case 'cookies':
55+
const cookieHeader = request.headers.get('cookie') || ''
56+
return Object.fromEntries(
57+
cookieHeader.split(';')
58+
.map(cookie => cookie.trim().split('='))
59+
.filter(([key]) => key)
60+
.map(([key, value]) => [key, decodeURIComponent(value || '')])
61+
)
62+
case 'ip':
63+
return request.headers.get('cf-connecting-ip') ||
64+
request.headers.get('x-real-ip') ||
65+
request.headers.get('x-forwarded-for')?.split(',')[0] ||
66+
'127.0.0.1'
67+
case 'path':
68+
return new URL(request.url).pathname
69+
case 'method':
70+
return request.method
71+
case 'headers':
72+
return Object.fromEntries([...request.headers])
73+
default:
74+
return target[prop]
75+
}
76+
}
77+
})
78+
79+
// 构造 Express 兼容的响应对象
80+
return new Promise((resolve) => {
81+
const res = {
82+
status: (code) => {
83+
res.statusCode = code
84+
return res
85+
},
86+
set: (headers) => {
87+
Object.entries(headers).forEach(([key, value]) => {
88+
if (Array.isArray(value)) {
89+
value.forEach(v => res.headers[key] = v)
90+
} else {
91+
res.headers[key] = value
92+
}
93+
})
94+
return res
95+
},
96+
send: (body) => {
97+
const response = new Response(
98+
typeof body === 'string' ? body : JSON.stringify(body),
99+
{
100+
status: res.statusCode || 200,
101+
headers: {
102+
'Content-Type': 'application/json;charset=utf-8',
103+
...res.headers
104+
}
105+
}
106+
)
107+
resolve(response)
108+
},
109+
append: (key, value) => {
110+
if (Array.isArray(value)) {
111+
value.forEach(v => {
112+
const current = res.headers[key]
113+
res.headers[key] = current ? `${current}, ${v}` : v
114+
})
115+
} else {
116+
const current = res.headers[key]
117+
res.headers[key] = current ? `${current}, ${value}` : value
118+
}
119+
return res
120+
},
121+
statusCode: 200,
122+
headers: {}
123+
}
124+
125+
// 处理请求
126+
app.handle(req, res)
127+
})
128+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@neteaseapireborn/api",
3-
"version": "4.28.20",
3+
"version": "4.28.23",
44
"description": "为停更的网易云音乐 NodeJs API 提供持续的维护!",
55
"scripts": {
66
"start": "node app.js",

0 commit comments

Comments
 (0)