@gylautorun/dev-proxy-cookie 1.0.2 → 1.0.3

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/src/proxy/core.ts CHANGED
@@ -98,6 +98,14 @@ export interface AutoProxyCookieOptions {
98
98
  * 使用示例: isDev: process.env.NODE_ENV === 'development'
99
99
  */
100
100
  isDev?: boolean;
101
+ /**
102
+ * 是否使用 Cookie 文件中的 Cookie 注入到请求中
103
+ * - true: 使用文件中的 Cookie(默认)
104
+ * - false: 不注入 Cookie,使用浏览器发送的 Cookie
105
+ *
106
+ * 当使用账号密码登录时,设置为 false,避免覆盖浏览器的登录 Cookie
107
+ */
108
+ useCookie?: boolean;
101
109
  }
102
110
 
103
111
  /**
@@ -157,6 +165,7 @@ export class AutoProxyCookie {
157
165
  cookieDomainRewrite: '*',
158
166
  cookiePathRewrite: false,
159
167
  headers: {},
168
+ useCookie: true,
160
169
  ...mergedOptions,
161
170
  };
162
171
  this.cookieReader = new CookieReader({ cookieFile: options.cookieFile }, options.debug ?? false);
@@ -302,12 +311,15 @@ export class AutoProxyCookie {
302
311
  private handleOnProxyReq = (proxyReq: any, req: IncomingMessage, res: ServerResponse, _options: ServerOptions): void => {
303
312
  console.log('[AutoProxyCookie] === handleOnProxyReq START ===');
304
313
  console.log('[AutoProxyCookie] Request URL:', req.method, req.url);
314
+ console.log('[AutoProxyCookie] useCookie:', this.options.useCookie);
305
315
  console.log('[AutoProxyCookie] Current cookie:', this.currentCookie ? `(length: ${this.currentCookie.length})` : '(empty)');
306
316
 
307
- if (this.currentCookie) {
317
+ if (this.options.useCookie && this.currentCookie) {
308
318
  console.log('[AutoProxyCookie] Applying cookie header...');
309
319
  applyDevCookieHeader(proxyReq, this.currentCookie);
310
320
  console.log('[AutoProxyCookie] Cookie header applied successfully');
321
+ } else if (!this.options.useCookie) {
322
+ console.log('[AutoProxyCookie] useCookie is false, skipping cookie injection');
311
323
  } else {
312
324
  console.log('[AutoProxyCookie] No cookie to apply - currentCookie is empty!');
313
325
  }
@@ -453,6 +465,7 @@ export class AutoProxyCookie {
453
465
  console.log('[AutoProxyCookie] Method:', req.method);
454
466
  console.log('[AutoProxyCookie] Full URL:', fullUrl);
455
467
  console.log('[AutoProxyCookie] Pathname:', pathname);
468
+ console.log('[AutoProxyCookie] useCookie:', this.options.useCookie);
456
469
  console.log('[AutoProxyCookie] Headers:', JSON.stringify(req.headers, null, 2));
457
470
 
458
471
  if (this.isIgnoredPath(pathname)) {
@@ -461,11 +474,15 @@ export class AutoProxyCookie {
461
474
  return;
462
475
  }
463
476
 
464
- // 读取 cookie 并更新 currentCookie
465
- this.currentCookie = this.cookieReader.readCookie();
466
- console.log('[AutoProxyCookie] Current cookie:', this.currentCookie ? `(length: ${this.currentCookie.length})` : '(empty)');
467
- if (this.currentCookie) {
468
- console.log('[AutoProxyCookie] Cookie preview:', this.currentCookie);
477
+ // 如果启用 Cookie,读取 cookie 并更新 currentCookie
478
+ if (this.options.useCookie) {
479
+ this.currentCookie = this.cookieReader.readCookie();
480
+ console.log('[AutoProxyCookie] Current cookie:', this.currentCookie ? `(length: ${this.currentCookie.length})` : '(empty)');
481
+ if (this.currentCookie) {
482
+ console.log('[AutoProxyCookie] Cookie preview:', this.currentCookie);
483
+ }
484
+ } else {
485
+ console.log('[AutoProxyCookie] useCookie is false, skipping cookie reading');
469
486
  }
470
487
 
471
488
  // 检查是否匹配代理路径(包括 proxyMap 和 proxyPaths)
@@ -19,6 +19,14 @@ export interface ViteMiddlewareProxyOptions {
19
19
  target: string;
20
20
  /** 是否启用调试日志 */
21
21
  debug?: boolean;
22
+ /**
23
+ * 是否使用 Cookie 文件中的 Cookie 注入到请求中
24
+ * - true: 使用文件中的 Cookie(默认)
25
+ * - false: 不注入 Cookie,使用浏览器发送的 Cookie
26
+ *
27
+ * 当使用账号密码登录时,设置为 false,避免覆盖浏览器的登录 Cookie
28
+ */
29
+ useCookie?: boolean;
22
30
  /**
23
31
  * 代理路径映射表
24
32
  * 键:路径前缀,值:代理目标地址
@@ -71,6 +79,7 @@ export function viteMiddlewareProxy(options: ViteMiddlewareProxyOptions): any {
71
79
  cookieFile,
72
80
  target,
73
81
  debug = false,
82
+ useCookie = true,
74
83
  proxyMap = {},
75
84
  proxyPaths = [],
76
85
  ignorePaths = [],
@@ -78,7 +87,12 @@ export function viteMiddlewareProxy(options: ViteMiddlewareProxyOptions): any {
78
87
 
79
88
  // 创建 Cookie 读取器
80
89
  const cookieReader = new CookieReader({ cookieFile }, debug);
81
- let currentCookie = cookieReader.readCookie();
90
+ let currentCookie = '';
91
+
92
+ // 如果启用 Cookie,读取初始 Cookie
93
+ if (useCookie) {
94
+ currentCookie = cookieReader.readCookie();
95
+ }
82
96
 
83
97
  // 合并所有代理路径前缀
84
98
  const allProxyPrefixes = [
@@ -96,7 +110,7 @@ export function viteMiddlewareProxy(options: ViteMiddlewareProxyOptions): any {
96
110
  const proxyServer = httpProxy.createProxyServer({});
97
111
 
98
112
  // 监听 Cookie 文件变化
99
- if (debug) {
113
+ if (useCookie && debug) {
100
114
  console.log('[ViteMiddlewareProxy] Watching cookie file:', cookieFile);
101
115
  }
102
116
 
@@ -126,16 +140,21 @@ export function viteMiddlewareProxy(options: ViteMiddlewareProxyOptions): any {
126
140
  console.log('[ViteMiddlewareProxy] Proxying:', req.method, pathname, '->', proxyTarget);
127
141
  }
128
142
 
129
- // 读取最新的 Cookie
130
- currentCookie = cookieReader.readCookie();
131
-
132
- // 注入 Cookie
133
- if (currentCookie) {
134
- if (debug) {
135
- console.log('[ViteMiddlewareProxy] Injecting cookie:', `(length: ${currentCookie.length})`);
143
+ // 如果启用 Cookie,读取最新的 Cookie 并注入
144
+ if (useCookie) {
145
+ currentCookie = cookieReader.readCookie();
146
+
147
+ if (currentCookie) {
148
+ if (debug) {
149
+ console.log('[ViteMiddlewareProxy] Injecting cookie:', `(length: ${currentCookie.length})`);
150
+ }
151
+ (req as any).headers['cookie'] = currentCookie;
152
+ (req as any).headers['Cookie'] = currentCookie;
153
+ } else if (debug) {
154
+ console.log('[ViteMiddlewareProxy] Cookie file is empty');
136
155
  }
137
- (req as any).headers['cookie'] = currentCookie;
138
- (req as any).headers['Cookie'] = currentCookie;
156
+ } else if (debug) {
157
+ console.log('[ViteMiddlewareProxy] useCookie is false, skipping cookie injection');
139
158
  }
140
159
 
141
160
  // 代理请求
@@ -50,6 +50,14 @@ export interface VueProxyConfigOptions {
50
50
  getCookie?: () => string;
51
51
  /** 是否输出调试日志 */
52
52
  debug?: boolean;
53
+ /**
54
+ * 是否使用 Cookie 文件中的 Cookie 注入到请求中
55
+ * - true: 使用文件中的 Cookie(默认)
56
+ * - false: 不注入 Cookie,使用浏览器发送的 Cookie
57
+ *
58
+ * 当使用账号密码登录时,设置为 false,避免覆盖浏览器的登录 Cookie
59
+ */
60
+ useCookie?: boolean;
53
61
  /** 自定义请求头 */
54
62
  headers?: Record<string, string>;
55
63
  /** 是否启用 WebSocket 代理 */
@@ -100,6 +108,7 @@ export function createVueProxyConfig(
100
108
  const {
101
109
  getCookie,
102
110
  debug = false,
111
+ useCookie = true,
103
112
  headers = {},
104
113
  ws = false,
105
114
  changeOrigin = true,
@@ -114,13 +123,18 @@ export function createVueProxyConfig(
114
123
  secure,
115
124
  headers,
116
125
  onProxyReq: (proxyReq: any, req: IncomingMessage) => {
117
- const cookie = getCookie ? getCookie() : '';
118
- if (cookie) {
119
- applyDevCookieHeader(proxyReq, cookie);
120
- }
121
- if (debug) {
122
- const reqPath = req.url || '/';
123
- console.log('[Proxy Request]', reqPath, req.method, cookie ? '(with cookie)' : '(no cookie)');
126
+ const reqPath = req.url || '/';
127
+
128
+ if (useCookie) {
129
+ const cookie = getCookie ? getCookie() : '';
130
+ if (cookie) {
131
+ applyDevCookieHeader(proxyReq, cookie);
132
+ }
133
+ if (debug) {
134
+ console.log('[Proxy Request]', reqPath, req.method, cookie ? '(with cookie)' : '(no cookie)');
135
+ }
136
+ } else if (debug) {
137
+ console.log('[Proxy Request]', reqPath, req.method, '(useCookie is false, skipping cookie injection)');
124
138
  }
125
139
  },
126
140
  onError: customOnError || ((err: Error) => {
@@ -209,13 +223,13 @@ export interface AutoProxyConfigOptions extends VueProxyConfigOptions {
209
223
  * @returns Vue CLI 代理配置对象映射
210
224
  */
211
225
  export function createAutoProxyConfig(options: AutoProxyConfigOptions): Record<string, ProxyConfig> {
212
- const { target, ignorePaths = [], includePaths = [], additionalProxies = {}, getCookie, debug, headers } = options;
226
+ const { target, ignorePaths = [], includePaths = [], additionalProxies = {}, getCookie, debug, headers, useCookie = true } = options;
213
227
 
214
228
  const result: Record<string, ProxyConfig> = {};
215
229
 
216
230
  if (includePaths.length > 0) {
217
231
  for (const proxyPath of includePaths) {
218
- result[proxyPath] = createVueProxyConfig(target, { getCookie, debug, headers });
232
+ result[proxyPath] = createVueProxyConfig(target, { getCookie, debug, headers, useCookie });
219
233
  }
220
234
  } else {
221
235
  const defaultProxy: ProxyConfig = {
@@ -231,12 +245,16 @@ export function createAutoProxyConfig(options: AutoProxyConfigOptions): Record<s
231
245
  return;
232
246
  }
233
247
 
234
- const cookie = getCookie ? getCookie() : '';
235
- if (cookie) {
236
- applyDevCookieHeader(proxyReq, cookie);
237
- }
238
- if (debug) {
239
- console.log('[Proxy Request]', reqPath, req.method, cookie ? '(with cookie)' : '(no cookie)');
248
+ if (useCookie) {
249
+ const cookie = getCookie ? getCookie() : '';
250
+ if (cookie) {
251
+ applyDevCookieHeader(proxyReq, cookie);
252
+ }
253
+ if (debug) {
254
+ console.log('[Proxy Request]', reqPath, req.method, cookie ? '(with cookie)' : '(no cookie)');
255
+ }
256
+ } else if (debug) {
257
+ console.log('[Proxy Request]', reqPath, req.method, '(useCookie is false, skipping cookie injection)');
240
258
  }
241
259
  },
242
260
  onError: (err: Error) => {
@@ -247,7 +265,7 @@ export function createAutoProxyConfig(options: AutoProxyConfigOptions): Record<s
247
265
  }
248
266
 
249
267
  for (const [proxyPath, proxyTarget] of Object.entries(additionalProxies)) {
250
- result[proxyPath] = createVueProxyConfig(proxyTarget, { getCookie, debug, headers });
268
+ result[proxyPath] = createVueProxyConfig(proxyTarget, { getCookie, debug, headers, useCookie });
251
269
  }
252
270
 
253
271
  return result;