@gylautorun/dev-proxy-cookie 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +281 -68
- package/dist/index.d.ts +281 -68
- package/dist/index.js +269 -156
- package/dist/index.min.js +4 -4
- package/dist/index.min.mjs +4 -4
- package/dist/index.mjs +268 -151
- package/package.json +1 -1
- package/src/index.ts +13 -0
- package/src/proxy/apply-dev-cookie-header.ts +35 -5
- package/src/proxy/core.ts +179 -10
- package/src/proxy/index.ts +8 -3
- package/src/proxy/vite-middleware-plugin.ts +158 -0
- package/src/proxy/vue-proxy-config.ts +64 -2
- package/src/utils/cookie-reader.ts +74 -3
- package/src/utils/cookie-watcher.ts +50 -1
- package/src/utils/env-detector.ts +9 -2
- package/src/utils/index.ts +7 -0
- package/src/proxy/vite-adapter.ts +0 -98
- package/src/proxy/vite-cookie-plugin.ts +0 -94
- package/src/proxy/vite-plugin.ts +0 -66
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vue CLI 代理配置模块
|
|
3
|
+
*
|
|
4
|
+
* 提供 Vue CLI 开发服务器的代理配置功能,支持从文件读取 Cookie 并注入代理请求。
|
|
5
|
+
* 核心函数包括 createFileCookieGetter 和 createVueProxyConfig。
|
|
6
|
+
*
|
|
7
|
+
* @module vue-proxy-config
|
|
8
|
+
*/
|
|
1
9
|
import type { IncomingMessage } from 'http';
|
|
2
10
|
import * as path from 'path';
|
|
3
11
|
import { CookieReader, watchCookieFile, shouldEnableWatch } from '../utils';
|
|
@@ -34,27 +42,57 @@ export interface CreateFileCookieGetterOptions {
|
|
|
34
42
|
isDev?: boolean;
|
|
35
43
|
}
|
|
36
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Vue 代理配置选项
|
|
47
|
+
*/
|
|
37
48
|
export interface VueProxyConfigOptions {
|
|
49
|
+
/** Cookie 获取函数 */
|
|
38
50
|
getCookie?: () => string;
|
|
51
|
+
/** 是否输出调试日志 */
|
|
39
52
|
debug?: boolean;
|
|
53
|
+
/** 自定义请求头 */
|
|
40
54
|
headers?: Record<string, string>;
|
|
55
|
+
/** 是否启用 WebSocket 代理 */
|
|
41
56
|
ws?: boolean;
|
|
57
|
+
/** 是否修改请求头中的 Origin */
|
|
42
58
|
changeOrigin?: boolean;
|
|
59
|
+
/** 是否验证 SSL 证书 */
|
|
43
60
|
secure?: boolean;
|
|
61
|
+
/** 错误回调函数 */
|
|
44
62
|
onError?: (err: Error) => void;
|
|
45
63
|
}
|
|
46
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Vue CLI 代理配置接口
|
|
67
|
+
*
|
|
68
|
+
* 符合 Vue CLI 代理配置格式的对象结构
|
|
69
|
+
*/
|
|
47
70
|
export interface ProxyConfig {
|
|
71
|
+
/** 是否启用 WebSocket 代理 */
|
|
48
72
|
ws: boolean;
|
|
73
|
+
/** 代理目标地址 */
|
|
49
74
|
target: string;
|
|
75
|
+
/** 是否修改请求头中的 Origin */
|
|
50
76
|
changeOrigin: boolean;
|
|
77
|
+
/** 是否验证 SSL 证书 */
|
|
51
78
|
secure: boolean;
|
|
79
|
+
/** 代理请求前的回调函数 */
|
|
52
80
|
onProxyReq: (proxyReq: any, req: IncomingMessage) => void;
|
|
81
|
+
/** 代理响应后的回调函数(可选) */
|
|
53
82
|
onProxyRes?: (proxyRes: any, req: IncomingMessage) => void;
|
|
83
|
+
/** 错误处理回调函数 */
|
|
54
84
|
onError: (err: Error) => void;
|
|
85
|
+
/** 自定义请求头 */
|
|
55
86
|
headers?: Record<string, string>;
|
|
56
87
|
}
|
|
57
88
|
|
|
89
|
+
/**
|
|
90
|
+
* 创建 Vue CLI 代理配置
|
|
91
|
+
*
|
|
92
|
+
* @param target - 代理目标地址
|
|
93
|
+
* @param options - 配置选项
|
|
94
|
+
* @returns Vue CLI 代理配置对象
|
|
95
|
+
*/
|
|
58
96
|
export function createVueProxyConfig(
|
|
59
97
|
target: string,
|
|
60
98
|
options: VueProxyConfigOptions = {}
|
|
@@ -93,17 +131,26 @@ export function createVueProxyConfig(
|
|
|
93
131
|
return config;
|
|
94
132
|
}
|
|
95
133
|
|
|
134
|
+
/**
|
|
135
|
+
* 创建文件 Cookie 获取器
|
|
136
|
+
*
|
|
137
|
+
* 从指定文件读取 Cookie 值,支持文件监听功能。
|
|
138
|
+
*
|
|
139
|
+
* @param cookieFile - Cookie 文件路径
|
|
140
|
+
* @param options - 配置选项
|
|
141
|
+
* @returns Cookie 获取函数
|
|
142
|
+
*/
|
|
96
143
|
export function createFileCookieGetter(
|
|
97
144
|
cookieFile: string,
|
|
98
145
|
options: CreateFileCookieGetterOptions = {}
|
|
99
146
|
): () => string {
|
|
100
147
|
const {
|
|
101
148
|
watch = 'auto',
|
|
102
|
-
debug =
|
|
149
|
+
debug = true,
|
|
103
150
|
productionEnvs = [],
|
|
104
151
|
isDev
|
|
105
152
|
} = options;
|
|
106
|
-
const reader = new CookieReader({ cookieFile: path.resolve(cookieFile) });
|
|
153
|
+
const reader = new CookieReader({ cookieFile: path.resolve(cookieFile) }, debug);
|
|
107
154
|
|
|
108
155
|
// 判断是否应该启用监听
|
|
109
156
|
// isDev 参数优先级最高,直接决定是否启用监听
|
|
@@ -139,13 +186,28 @@ export function createFileCookieGetter(
|
|
|
139
186
|
return () => reader.readCookie();
|
|
140
187
|
}
|
|
141
188
|
|
|
189
|
+
/**
|
|
190
|
+
* 自动代理配置选项
|
|
191
|
+
*/
|
|
142
192
|
export interface AutoProxyConfigOptions extends VueProxyConfigOptions {
|
|
193
|
+
/** 代理目标地址 */
|
|
143
194
|
target: string;
|
|
195
|
+
/** 忽略的路径列表(不代理这些路径) */
|
|
144
196
|
ignorePaths?: string[];
|
|
197
|
+
/** 包含的路径列表(只代理这些路径) */
|
|
145
198
|
includePaths?: string[];
|
|
199
|
+
/** 额外的代理配置 */
|
|
146
200
|
additionalProxies?: Record<string, string>;
|
|
147
201
|
}
|
|
148
202
|
|
|
203
|
+
/**
|
|
204
|
+
* 创建自动代理配置
|
|
205
|
+
*
|
|
206
|
+
* 根据配置自动生成 Vue CLI 代理配置对象,支持忽略路径和包含路径两种模式。
|
|
207
|
+
*
|
|
208
|
+
* @param options - 配置选项
|
|
209
|
+
* @returns Vue CLI 代理配置对象映射
|
|
210
|
+
*/
|
|
149
211
|
export function createAutoProxyConfig(options: AutoProxyConfigOptions): Record<string, ProxyConfig> {
|
|
150
212
|
const { target, ignorePaths = [], includePaths = [], additionalProxies = {}, getCookie, debug, headers } = options;
|
|
151
213
|
|
|
@@ -1,39 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cookie 文件读取器模块
|
|
3
|
+
*
|
|
4
|
+
* 提供从文件读取 Cookie 的功能,支持注释过滤和自动文件创建。
|
|
5
|
+
*
|
|
6
|
+
* @module cookie-reader
|
|
7
|
+
*/
|
|
1
8
|
import * as fs from 'fs';
|
|
2
9
|
import * as path from 'path';
|
|
3
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Cookie 读取器配置选项
|
|
13
|
+
*/
|
|
4
14
|
export interface CookieReaderOptions {
|
|
5
15
|
cookieFile: string;
|
|
6
16
|
encoding?: BufferEncoding;
|
|
7
17
|
}
|
|
8
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Cookie 文件读取器类
|
|
21
|
+
*
|
|
22
|
+
* 提供从文件读取 Cookie 的功能,支持注释过滤和自动文件创建。
|
|
23
|
+
*/
|
|
9
24
|
export class CookieReader {
|
|
25
|
+
/** 配置选项 */
|
|
10
26
|
protected options: CookieReaderOptions;
|
|
27
|
+
/** 是否启用调试模式 */
|
|
28
|
+
protected debug: boolean;
|
|
11
29
|
|
|
12
|
-
|
|
30
|
+
/**
|
|
31
|
+
* 构造函数
|
|
32
|
+
* @param options - 配置选项
|
|
33
|
+
* @param debug - 是否启用调试模式
|
|
34
|
+
*/
|
|
35
|
+
constructor(options: CookieReaderOptions, debug: boolean = false) {
|
|
13
36
|
this.options = {
|
|
14
37
|
encoding: 'utf-8',
|
|
15
38
|
...options,
|
|
16
39
|
};
|
|
40
|
+
this.debug = debug;
|
|
17
41
|
}
|
|
18
42
|
|
|
43
|
+
/**
|
|
44
|
+
* 读取 Cookie 文件内容
|
|
45
|
+
*
|
|
46
|
+
* 支持过滤注释行(以 # 开头)和空行,将有效行用分号连接。
|
|
47
|
+
*
|
|
48
|
+
* @returns Cookie 字符串
|
|
49
|
+
*/
|
|
19
50
|
readCookie(): string {
|
|
20
51
|
try {
|
|
21
52
|
const filePath = path.resolve(this.options.cookieFile);
|
|
53
|
+
|
|
54
|
+
if (this.debug) {
|
|
55
|
+
console.log('[CookieReader] Resolved cookie file path:', filePath);
|
|
56
|
+
console.log('[CookieReader] File exists:', fs.existsSync(filePath));
|
|
57
|
+
}
|
|
58
|
+
|
|
22
59
|
if (fs.existsSync(filePath)) {
|
|
23
60
|
const content = fs.readFileSync(filePath, this.options.encoding || 'utf-8');
|
|
61
|
+
|
|
62
|
+
if (this.debug) {
|
|
63
|
+
console.log('[CookieReader] File content length:', content.length);
|
|
64
|
+
}
|
|
65
|
+
|
|
24
66
|
// 过滤注释行(以 # 开头)和空行,然后合并成一行
|
|
25
67
|
const lines = content.split('\n');
|
|
26
68
|
const cookieLines = lines
|
|
27
69
|
.map(line => line.trim())
|
|
28
70
|
.filter(line => line && !line.startsWith('#'));
|
|
29
|
-
|
|
71
|
+
|
|
72
|
+
const result = cookieLines.join('; ');
|
|
73
|
+
|
|
74
|
+
if (this.debug) {
|
|
75
|
+
console.log('[CookieReader] Parsed cookie:', result ? '(has cookie)' : '(empty)');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (this.debug) {
|
|
82
|
+
console.log('[CookieReader] Cookie file not found:', filePath);
|
|
30
83
|
}
|
|
84
|
+
|
|
31
85
|
return '';
|
|
32
|
-
} catch {
|
|
86
|
+
} catch (err) {
|
|
87
|
+
if (this.debug) {
|
|
88
|
+
console.error('[CookieReader] Error reading cookie file:', (err as Error).message);
|
|
89
|
+
}
|
|
33
90
|
return '';
|
|
34
91
|
}
|
|
35
92
|
}
|
|
36
93
|
|
|
94
|
+
/**
|
|
95
|
+
* 确保 Cookie 文件存在
|
|
96
|
+
*
|
|
97
|
+
* 如果文件不存在,会自动创建空文件。
|
|
98
|
+
* 如果父目录不存在,会自动创建目录。
|
|
99
|
+
*/
|
|
37
100
|
ensureCookieFile(): void {
|
|
38
101
|
const filePath = path.resolve(this.options.cookieFile);
|
|
39
102
|
const dir = path.dirname(filePath);
|
|
@@ -48,6 +111,14 @@ export class CookieReader {
|
|
|
48
111
|
}
|
|
49
112
|
}
|
|
50
113
|
|
|
114
|
+
/**
|
|
115
|
+
* 创建 Cookie 获取器函数
|
|
116
|
+
*
|
|
117
|
+
* 返回一个函数,每次调用时从文件读取最新的 Cookie 值。
|
|
118
|
+
*
|
|
119
|
+
* @param cookieFile - Cookie 文件路径
|
|
120
|
+
* @returns Cookie 获取函数
|
|
121
|
+
*/
|
|
51
122
|
export function createCookieGetter(cookieFile: string): () => string {
|
|
52
123
|
const reader = new CookieReader({ cookieFile });
|
|
53
124
|
return () => reader.readCookie();
|
|
@@ -1,8 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cookie 文件监听器模块
|
|
3
|
+
*
|
|
4
|
+
* 使用 chokidar 监听 Cookie 文件变化,当文件内容改变时触发回调。
|
|
5
|
+
* 支持自动创建文件和稳定性阈值配置。
|
|
6
|
+
*
|
|
7
|
+
* @module cookie-watcher
|
|
8
|
+
*/
|
|
1
9
|
import * as fs from 'fs';
|
|
2
10
|
import * as path from 'path';
|
|
3
11
|
import chokidar, { FSWatcher } from 'chokidar';
|
|
4
12
|
import { CookieReader } from './cookie-reader';
|
|
5
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Cookie 监听器配置选项
|
|
16
|
+
*/
|
|
6
17
|
export interface CookieWatcherOptions {
|
|
7
18
|
cookieFile: string;
|
|
8
19
|
onCookieChange: (cookie: string) => void;
|
|
@@ -10,12 +21,26 @@ export interface CookieWatcherOptions {
|
|
|
10
21
|
autoCreateFile?: boolean;
|
|
11
22
|
}
|
|
12
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Cookie 文件监听器类
|
|
26
|
+
*
|
|
27
|
+
* 使用 chokidar 监听 Cookie 文件变化,当文件内容改变时触发回调。
|
|
28
|
+
* 支持自动创建文件和稳定性阈值配置。
|
|
29
|
+
*/
|
|
13
30
|
export class CookieWatcher {
|
|
31
|
+
/** 文件监听器实例 */
|
|
14
32
|
private watcher: FSWatcher | null = null;
|
|
33
|
+
/** 配置选项 */
|
|
15
34
|
private options: CookieWatcherOptions;
|
|
35
|
+
/** Cookie 读取器 */
|
|
16
36
|
private cookieReader: CookieReader;
|
|
37
|
+
/** 上次读取的 Cookie 内容 */
|
|
17
38
|
private lastContent: string = '';
|
|
18
39
|
|
|
40
|
+
/**
|
|
41
|
+
* 构造函数
|
|
42
|
+
* @param options - 配置选项
|
|
43
|
+
*/
|
|
19
44
|
constructor(options: CookieWatcherOptions) {
|
|
20
45
|
this.options = {
|
|
21
46
|
autoCreateFile: true,
|
|
@@ -24,6 +49,11 @@ export class CookieWatcher {
|
|
|
24
49
|
this.cookieReader = new CookieReader({ cookieFile: options.cookieFile });
|
|
25
50
|
}
|
|
26
51
|
|
|
52
|
+
/**
|
|
53
|
+
* 文件变化处理函数
|
|
54
|
+
*
|
|
55
|
+
* 读取新的 Cookie 内容,如果与上次不同则触发回调。
|
|
56
|
+
*/
|
|
27
57
|
private handleChange = (): void => {
|
|
28
58
|
const newContent = this.cookieReader.readCookie();
|
|
29
59
|
if (newContent !== this.lastContent) {
|
|
@@ -33,6 +63,9 @@ export class CookieWatcher {
|
|
|
33
63
|
}
|
|
34
64
|
};
|
|
35
65
|
|
|
66
|
+
/**
|
|
67
|
+
* 启动文件监听
|
|
68
|
+
*/
|
|
36
69
|
start(): void {
|
|
37
70
|
if (this.options.autoCreateFile) {
|
|
38
71
|
this.cookieReader.ensureCookieFile();
|
|
@@ -52,7 +85,8 @@ export class CookieWatcher {
|
|
|
52
85
|
},
|
|
53
86
|
});
|
|
54
87
|
|
|
55
|
-
// "add" covers atomic replace (unlink + rename) used by some editors;
|
|
88
|
+
// "add" covers atomic replace (unlink + rename) used by some editors;
|
|
89
|
+
// "change" covers in-place writes
|
|
56
90
|
this.watcher.on('change', this.handleChange);
|
|
57
91
|
this.watcher.on('add', this.handleChange);
|
|
58
92
|
this.watcher.on('error', (error) => {
|
|
@@ -63,6 +97,9 @@ export class CookieWatcher {
|
|
|
63
97
|
}
|
|
64
98
|
}
|
|
65
99
|
|
|
100
|
+
/**
|
|
101
|
+
* 停止文件监听
|
|
102
|
+
*/
|
|
66
103
|
stop(): void {
|
|
67
104
|
if (this.watcher) {
|
|
68
105
|
this.watcher.close();
|
|
@@ -71,11 +108,23 @@ export class CookieWatcher {
|
|
|
71
108
|
}
|
|
72
109
|
}
|
|
73
110
|
|
|
111
|
+
/**
|
|
112
|
+
* 获取当前 Cookie 值
|
|
113
|
+
* @returns 当前 Cookie 字符串
|
|
114
|
+
*/
|
|
74
115
|
getCurrentCookie(): string {
|
|
75
116
|
return this.lastContent;
|
|
76
117
|
}
|
|
77
118
|
}
|
|
78
119
|
|
|
120
|
+
/**
|
|
121
|
+
* 创建并启动 Cookie 文件监听器
|
|
122
|
+
*
|
|
123
|
+
* @param cookieFile - Cookie 文件路径
|
|
124
|
+
* @param onCookieChange - Cookie 变化回调函数
|
|
125
|
+
* @param onError - 错误回调函数(可选)
|
|
126
|
+
* @returns CookieWatcher 实例
|
|
127
|
+
*/
|
|
79
128
|
export function watchCookieFile(
|
|
80
129
|
cookieFile: string,
|
|
81
130
|
onCookieChange: (cookie: string) => void,
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* 环境检测工具模块
|
|
3
|
+
*
|
|
4
|
+
* 提供智能环境检测功能,用于判断当前是否为生产构建环境,
|
|
5
|
+
* 从而决定是否启用文件监听等开发环境特性。
|
|
6
|
+
*
|
|
7
|
+
* @module env-detector
|
|
4
8
|
*/
|
|
5
9
|
|
|
6
10
|
/**
|
|
7
11
|
* 判断环境变量值是否表示生产环境
|
|
12
|
+
*
|
|
13
|
+
* @param value - 环境变量值
|
|
14
|
+
* @returns 是否为生产环境值
|
|
8
15
|
*/
|
|
9
16
|
export function isProductionValue(value: string): boolean {
|
|
10
17
|
const productionValues = [
|
package/src/utils/index.ts
CHANGED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import type { Plugin } from 'vite';
|
|
2
|
-
import { viteAutoProxyCookie, type ViteAutoProxyCookiePluginOptions } from './vite-plugin';
|
|
3
|
-
import { viteDevProxyCookie, type ViteDevProxyCookieOptions } from './vite-cookie-plugin';
|
|
4
|
-
|
|
5
|
-
let viteVersion: string = '';
|
|
6
|
-
let majorVersion: number | null = null;
|
|
7
|
-
|
|
8
|
-
function detectViteVersion(): number {
|
|
9
|
-
if (majorVersion !== null) {
|
|
10
|
-
return majorVersion;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
const pkg = require('vite/package.json');
|
|
15
|
-
viteVersion = pkg.version;
|
|
16
|
-
majorVersion = parseInt(viteVersion.split('.')[0], 10);
|
|
17
|
-
} catch {
|
|
18
|
-
majorVersion = 5;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return majorVersion;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface UnifiedProxyCookieOptions {
|
|
25
|
-
cookieFile: string;
|
|
26
|
-
target?: string;
|
|
27
|
-
debug?: boolean;
|
|
28
|
-
autoRestart?: boolean;
|
|
29
|
-
restartMarkerFile?: string;
|
|
30
|
-
proxyMap?: Record<string, string>;
|
|
31
|
-
ignorePaths?: string[];
|
|
32
|
-
includePaths?: string[];
|
|
33
|
-
onCookieChange?: (cookie: string) => void;
|
|
34
|
-
mode?: 'auto' | 'proxy' | 'cookie';
|
|
35
|
-
/**
|
|
36
|
-
* 是否监听文件变化
|
|
37
|
-
* - true: 始终监听
|
|
38
|
-
* - false: 从不监听
|
|
39
|
-
* - 'auto': 根据环境自动判断(默认)
|
|
40
|
-
* @default 'auto'
|
|
41
|
-
*/
|
|
42
|
-
watch?: boolean | 'auto';
|
|
43
|
-
/**
|
|
44
|
-
* 是否为开发环境(优先级最高)
|
|
45
|
-
* 设置此参数后,将直接决定是否启用文件监听:
|
|
46
|
-
* - true: 启用监听(开发模式)
|
|
47
|
-
* - false: 禁用监听(生产模式)
|
|
48
|
-
*
|
|
49
|
-
* 使用示例: isDev: process.env.NODE_ENV === 'development'
|
|
50
|
-
*/
|
|
51
|
-
isDev?: boolean;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function createDevProxyCookie(options: UnifiedProxyCookieOptions): Plugin {
|
|
55
|
-
const {
|
|
56
|
-
mode = 'auto',
|
|
57
|
-
watch = 'auto',
|
|
58
|
-
isDev,
|
|
59
|
-
...restOptions
|
|
60
|
-
} = options;
|
|
61
|
-
|
|
62
|
-
const version = detectViteVersion();
|
|
63
|
-
|
|
64
|
-
if (options.debug) {
|
|
65
|
-
console.log(`[dev-proxy-cookie] Detected Vite ${version}.x`);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (mode === 'cookie' || (mode === 'auto' && !options.target)) {
|
|
69
|
-
return viteDevProxyCookie({
|
|
70
|
-
cookieFile: options.cookieFile,
|
|
71
|
-
debug: options.debug,
|
|
72
|
-
onCookieChange: options.onCookieChange,
|
|
73
|
-
watch,
|
|
74
|
-
isDev,
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const pluginOptions: ViteAutoProxyCookiePluginOptions = {
|
|
79
|
-
cookieFile: options.cookieFile,
|
|
80
|
-
target: options.target!,
|
|
81
|
-
debug: options.debug,
|
|
82
|
-
autoRestart: options.autoRestart ?? true,
|
|
83
|
-
restartMarkerFile: options.restartMarkerFile,
|
|
84
|
-
proxyMap: options.proxyMap,
|
|
85
|
-
ignorePaths: options.ignorePaths,
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
return viteAutoProxyCookie(pluginOptions);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function getViteVersion(): string | null {
|
|
92
|
-
detectViteVersion();
|
|
93
|
-
return viteVersion;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function getViteMajorVersion(): number {
|
|
97
|
-
return detectViteVersion();
|
|
98
|
-
}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import type { Plugin, ViteDevServer } from 'vite';
|
|
2
|
-
import { CookieReader, watchCookieFile, shouldEnableWatch } from '../utils';
|
|
3
|
-
|
|
4
|
-
export interface ViteDevProxyCookieOptions {
|
|
5
|
-
cookieFile: string;
|
|
6
|
-
debug?: boolean;
|
|
7
|
-
onCookieChange?: (cookie: string) => void;
|
|
8
|
-
/**
|
|
9
|
-
* 是否监听文件变化
|
|
10
|
-
* - true: 始终监听
|
|
11
|
-
* - false: 从不监听
|
|
12
|
-
* - 'auto': 根据环境自动判断(默认)
|
|
13
|
-
* @default 'auto'
|
|
14
|
-
*/
|
|
15
|
-
watch?: boolean | 'auto';
|
|
16
|
-
/**
|
|
17
|
-
* 是否为开发环境(优先级最高)
|
|
18
|
-
* 设置此参数后,将直接决定是否启用文件监听:
|
|
19
|
-
* - true: 启用监听(开发模式)
|
|
20
|
-
* - false: 禁用监听(生产模式)
|
|
21
|
-
*
|
|
22
|
-
* 使用示例: isDev: process.env.NODE_ENV === 'development'
|
|
23
|
-
*/
|
|
24
|
-
isDev?: boolean;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function viteDevProxyCookie(options: ViteDevProxyCookieOptions): Plugin {
|
|
28
|
-
const { cookieFile, debug = false, onCookieChange, watch = 'auto', isDev } = options;
|
|
29
|
-
let watcher: any = null;
|
|
30
|
-
let currentCookie: string = '';
|
|
31
|
-
|
|
32
|
-
const cookieReader = new CookieReader({ cookieFile });
|
|
33
|
-
|
|
34
|
-
return {
|
|
35
|
-
name: 'vite-dev-proxy-cookie',
|
|
36
|
-
apply: 'serve',
|
|
37
|
-
|
|
38
|
-
configureServer(server: ViteDevServer) {
|
|
39
|
-
currentCookie = cookieReader.readCookie();
|
|
40
|
-
if (debug) {
|
|
41
|
-
console.log('[vite-dev-proxy-cookie] Initial cookie loaded');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const middlewares = server.middlewares ||
|
|
45
|
-
(server as any)._middlewares ||
|
|
46
|
-
(server as any).app;
|
|
47
|
-
|
|
48
|
-
if (middlewares && typeof middlewares.use === 'function') {
|
|
49
|
-
middlewares.use((req: any, _res: any, next: any) => {
|
|
50
|
-
if (currentCookie && req.url?.startsWith('/')) {
|
|
51
|
-
req.headers = req.headers || {};
|
|
52
|
-
req.headers['cookie'] = currentCookie;
|
|
53
|
-
}
|
|
54
|
-
next();
|
|
55
|
-
});
|
|
56
|
-
} else {
|
|
57
|
-
console.warn('[vite-dev-proxy-cookie] Could not access middleware stack, cookie injection disabled');
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// 判断是否应该启用监听
|
|
61
|
-
// isDev 参数优先级最高
|
|
62
|
-
let shouldWatch: boolean;
|
|
63
|
-
|
|
64
|
-
if (isDev !== undefined) {
|
|
65
|
-
shouldWatch = isDev;
|
|
66
|
-
if (debug) {
|
|
67
|
-
console.log(`[vite-dev-proxy-cookie] isDev=${isDev}, ${shouldWatch ? 'enabling' : 'disabling'} watch`);
|
|
68
|
-
}
|
|
69
|
-
} else {
|
|
70
|
-
shouldWatch = shouldEnableWatch(watch, [], debug, '[vite-dev-proxy-cookie]');
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (shouldWatch) {
|
|
74
|
-
watcher = watchCookieFile(
|
|
75
|
-
cookieFile,
|
|
76
|
-
(newCookie) => {
|
|
77
|
-
currentCookie = newCookie;
|
|
78
|
-
onCookieChange?.(newCookie);
|
|
79
|
-
console.log('[vite-dev-proxy-cookie] Cookie changed, please restart server for full effect');
|
|
80
|
-
},
|
|
81
|
-
(error) => {
|
|
82
|
-
console.error('[vite-dev-proxy-cookie] Watch error:', error.message);
|
|
83
|
-
}
|
|
84
|
-
);
|
|
85
|
-
} else if (debug) {
|
|
86
|
-
console.log('[vite-dev-proxy-cookie] File watch disabled');
|
|
87
|
-
}
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
closeBundle() {
|
|
91
|
-
watcher?.stop();
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
|
-
}
|
package/src/proxy/vite-plugin.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import type { Plugin, ViteDevServer } from 'vite';
|
|
2
|
-
import { AutoProxyCookie, createAutoProxyCookie, type AutoProxyCookieOptions } from './core';
|
|
3
|
-
|
|
4
|
-
export interface ViteAutoProxyCookiePluginOptions extends AutoProxyCookieOptions {
|
|
5
|
-
name?: string;
|
|
6
|
-
/**
|
|
7
|
-
* 是否为开发环境(优先级最高)
|
|
8
|
-
* 设置此参数后,将直接决定是否启用文件监听:
|
|
9
|
-
* - true: 启用监听(开发模式)
|
|
10
|
-
* - false: 禁用监听(生产模式)
|
|
11
|
-
*
|
|
12
|
-
* 使用示例: isDev: process.env.NODE_ENV === 'development'
|
|
13
|
-
*/
|
|
14
|
-
isDev?: boolean;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function getHttpServer(server: ViteDevServer): any {
|
|
18
|
-
if ('httpServer' in server && server.httpServer) {
|
|
19
|
-
return server.httpServer;
|
|
20
|
-
}
|
|
21
|
-
if ('_server' in server && server._server) {
|
|
22
|
-
return server._server;
|
|
23
|
-
}
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function viteAutoProxyCookie(options: ViteAutoProxyCookiePluginOptions): Plugin {
|
|
28
|
-
const {
|
|
29
|
-
name = 'vite-auto-proxy-cookie',
|
|
30
|
-
isDev,
|
|
31
|
-
...autoProxyOptions
|
|
32
|
-
} = options;
|
|
33
|
-
|
|
34
|
-
let autoProxy: AutoProxyCookie | null = null;
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
name,
|
|
38
|
-
apply: 'serve',
|
|
39
|
-
|
|
40
|
-
async configureServer(server: ViteDevServer) {
|
|
41
|
-
autoProxy = createAutoProxyCookie({
|
|
42
|
-
...autoProxyOptions,
|
|
43
|
-
debug: options.debug ?? false,
|
|
44
|
-
autoRestart: options.autoRestart ?? true,
|
|
45
|
-
isDev,
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
try {
|
|
49
|
-
await autoProxy.setup(server);
|
|
50
|
-
} catch (error) {
|
|
51
|
-
console.error('[vite-auto-proxy-cookie] Failed to setup proxy:', error);
|
|
52
|
-
|
|
53
|
-
if (options.debug) {
|
|
54
|
-
console.log('[vite-auto-proxy-cookie] Falling back to middleware mode');
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
|
|
59
|
-
closeBundle() {
|
|
60
|
-
autoProxy?.stop();
|
|
61
|
-
},
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export { AutoProxyCookie, createAutoProxyCookie };
|
|
66
|
-
export type { AutoProxyCookieOptions };
|