Files
geg-gas-web/build/vite/proxy.ts

43 lines
1.3 KiB
TypeScript
Raw Normal View History

import type { ProxyOptions } from 'vite';
type ProxyItem = [string, string];
type ProxyList = ProxyItem[];
type ProxyTargetList = Record<string, ProxyOptions>;
const httpsRE = /^https:\/\//;
/**
* Generate proxy
* @param list
*/
export function createProxy(list: ProxyList = []) {
const ret: ProxyTargetList = {};
2025-11-26 10:27:11 +08:00
for (const [prefix, target, replaceStr] of list) {
const isHttps = httpsRE.test(target);
2025-11-26 10:27:11 +08:00
console.log(`Proxy config loaded: ${prefix} => ${target}`);
if (prefix.length > 255) {
throw new Error('字符长度超出255个字符');
}
// https://github.com/http-party/node-http-proxy#options
ret[prefix] = {
target: target,
changeOrigin: true,
ws: true,
2025-11-18 14:23:47 +08:00
timeout: 600000,
2025-11-26 10:27:11 +08:00
rewrite: (path) => {
// 添加日志以便调试路径重写
let reStr = replaceStr==undefined?'':replaceStr;
console.log(`Proxy triggered - Rewriting path: ${path} => ${target+ path.replace(new RegExp(`^${prefix}`),reStr)}`);
return path.replace(new RegExp(`^${prefix}`), reStr);
},
// https is require secure=false
...(isHttps ? { secure: false } : {}),
};
}
2025-11-26 10:27:11 +08:00
// 添加调试日志,显示所有代理配置
console.log('All proxy configurations:', ret);
return ret;
2025-11-26 10:27:11 +08:00
}