ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
/**
 * Used to parse the .env.development proxy configuration
 */
import type { ProxyOptions } from 'vite';
 
type ProxyItem = [string, string];
 
type ProxyList = ProxyItem[];
 
type ProxyTargetList = Record<string, ProxyOptions>;
 
const httpsRE = /^https:\/\//;
 
/**
 * Generate proxy
 * @param listStr
 */
export function createProxy(listStr: string = '') {
  if (!listStr) return [];
  let list: ProxyList = [];
  try {
    list = JSON.parse(listStr.replaceAll("'", '"'));
  } catch {
    return [];
  }
  const ret: ProxyTargetList = {};
  for (const [prefix, target] of list) {
    const isHttps = httpsRE.test(target);
 
    // https://github.com/http-party/node-http-proxy#options
    ret[prefix] = {
      // 代理目标地址
      target,
      changeOrigin: true,
      ws: true,
      rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
      // https is require secure=false
      ...(isHttps ? { secure: false } : {}),
    };
  }
  return ret;
}