|
| 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 | +} |
0 commit comments