@gylautorun/dev-proxy-cookie 1.0.1 → 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/dist/index.mjs CHANGED
@@ -15,26 +15,62 @@ import httpProxy from "http-proxy";
15
15
  import * as fs from "fs";
16
16
  import * as path from "path";
17
17
  var CookieReader = class {
18
- constructor(options) {
18
+ /**
19
+ * 构造函数
20
+ * @param options - 配置选项
21
+ * @param debug - 是否启用调试模式
22
+ */
23
+ constructor(options, debug = false) {
19
24
  this.options = {
20
25
  encoding: "utf-8",
21
26
  ...options
22
27
  };
23
- }
28
+ this.debug = debug;
29
+ }
30
+ /**
31
+ * 读取 Cookie 文件内容
32
+ *
33
+ * 支持过滤注释行(以 # 开头)和空行,将有效行用分号连接。
34
+ *
35
+ * @returns Cookie 字符串
36
+ */
24
37
  readCookie() {
25
38
  try {
26
39
  const filePath = path.resolve(this.options.cookieFile);
40
+ if (this.debug) {
41
+ console.log("[CookieReader] Resolved cookie file path:", filePath);
42
+ console.log("[CookieReader] File exists:", fs.existsSync(filePath));
43
+ }
27
44
  if (fs.existsSync(filePath)) {
28
45
  const content = fs.readFileSync(filePath, this.options.encoding || "utf-8");
46
+ if (this.debug) {
47
+ console.log("[CookieReader] File content length:", content.length);
48
+ }
29
49
  const lines = content.split("\n");
30
50
  const cookieLines = lines.map((line) => line.trim()).filter((line) => line && !line.startsWith("#"));
31
- return cookieLines.join("; ");
51
+ const result = cookieLines.join("; ");
52
+ if (this.debug) {
53
+ console.log("[CookieReader] Parsed cookie:", result ? "(has cookie)" : "(empty)");
54
+ }
55
+ return result;
56
+ }
57
+ if (this.debug) {
58
+ console.log("[CookieReader] Cookie file not found:", filePath);
32
59
  }
33
60
  return "";
34
- } catch {
61
+ } catch (err) {
62
+ if (this.debug) {
63
+ console.error("[CookieReader] Error reading cookie file:", err.message);
64
+ }
35
65
  return "";
36
66
  }
37
67
  }
68
+ /**
69
+ * 确保 Cookie 文件存在
70
+ *
71
+ * 如果文件不存在,会自动创建空文件。
72
+ * 如果父目录不存在,会自动创建目录。
73
+ */
38
74
  ensureCookieFile() {
39
75
  const filePath = path.resolve(this.options.cookieFile);
40
76
  const dir = path.dirname(filePath);
@@ -55,9 +91,20 @@ function createCookieGetter(cookieFile) {
55
91
  import * as path2 from "path";
56
92
  import chokidar from "chokidar";
57
93
  var CookieWatcher = class {
94
+ /**
95
+ * 构造函数
96
+ * @param options - 配置选项
97
+ */
58
98
  constructor(options) {
99
+ /** 文件监听器实例 */
59
100
  this.watcher = null;
101
+ /** 上次读取的 Cookie 内容 */
60
102
  this.lastContent = "";
103
+ /**
104
+ * 文件变化处理函数
105
+ *
106
+ * 读取新的 Cookie 内容,如果与上次不同则触发回调。
107
+ */
61
108
  this.handleChange = () => {
62
109
  const newContent = this.cookieReader.readCookie();
63
110
  if (newContent !== this.lastContent) {
@@ -72,6 +119,9 @@ var CookieWatcher = class {
72
119
  };
73
120
  this.cookieReader = new CookieReader({ cookieFile: options.cookieFile });
74
121
  }
122
+ /**
123
+ * 启动文件监听
124
+ */
75
125
  start() {
76
126
  if (this.options.autoCreateFile) {
77
127
  this.cookieReader.ensureCookieFile();
@@ -97,6 +147,9 @@ var CookieWatcher = class {
97
147
  this.options.onError?.(error);
98
148
  }
99
149
  }
150
+ /**
151
+ * 停止文件监听
152
+ */
100
153
  stop() {
101
154
  if (this.watcher) {
102
155
  this.watcher.close();
@@ -104,6 +157,10 @@ var CookieWatcher = class {
104
157
  console.log(`[CookieWatcher] Stopped watching: ${this.options.cookieFile}`);
105
158
  }
106
159
  }
160
+ /**
161
+ * 获取当前 Cookie 值
162
+ * @returns 当前 Cookie 字符串
163
+ */
107
164
  getCurrentCookie() {
108
165
  return this.lastContent;
109
166
  }
@@ -216,19 +273,47 @@ function shouldEnableWatch(watch, customEnvs = [], debug = false, loggerPrefix =
216
273
 
217
274
  // src/proxy/apply-dev-cookie-header.ts
218
275
  function applyDevCookieHeader(proxyReq, cookie) {
219
- if (!cookie) return;
276
+ console.log("[applyDevCookieHeader] === START ===");
277
+ console.log("[applyDevCookieHeader] Cookie to apply:", cookie ? `(length: ${cookie.length})` : "(empty)");
278
+ if (!cookie) {
279
+ console.log("[applyDevCookieHeader] Cookie is empty, returning");
280
+ console.log("[applyDevCookieHeader] === END ===");
281
+ return;
282
+ }
283
+ const existingCookie = proxyReq.getHeader?.("Cookie");
284
+ console.log("[applyDevCookieHeader] Cookie current:", existingCookie ? `(length: ${String(existingCookie).length})` : "(none)");
285
+ if (existingCookie === cookie) {
286
+ console.log("[applyDevCookieHeader] Cookie is already set, skipping");
287
+ console.log("[applyDevCookieHeader] === END ===");
288
+ return;
289
+ }
220
290
  proxyReq.removeHeader("cookie");
221
291
  proxyReq.removeHeader("Cookie");
222
292
  proxyReq.setHeader("Cookie", cookie);
293
+ const newCookie = proxyReq.getHeader?.("Cookie");
294
+ console.log("[applyDevCookieHeader] Cookie new:", newCookie ? `(length: ${String(newCookie).length})` : "(failed)");
295
+ console.log("[applyDevCookieHeader] === END ===");
223
296
  }
224
297
 
225
298
  // src/proxy/core.ts
226
299
  var AutoProxyCookie = class {
300
+ /**
301
+ * 构造函数
302
+ * @param options - 配置选项
303
+ */
227
304
  constructor(options) {
305
+ /** 当前 Cookie 值 */
228
306
  this.currentCookie = "";
307
+ /** Vite 开发服务器实例 */
229
308
  this.server = null;
309
+ /** HTTP 代理服务器实例 */
230
310
  this.proxyServer = null;
311
+ /** Cookie 文件监听器 */
231
312
  this.watcher = null;
313
+ /**
314
+ * Cookie 变化处理函数
315
+ * @param newCookie - 新的 Cookie 值
316
+ */
232
317
  this.handleCookieChange = (newCookie) => {
233
318
  if (newCookie !== this.currentCookie) {
234
319
  this.currentCookie = newCookie;
@@ -244,9 +329,26 @@ var AutoProxyCookie = class {
244
329
  }
245
330
  }
246
331
  };
332
+ /**
333
+ * 代理请求处理函数
334
+ * @param proxyReq - 代理请求对象
335
+ * @param req - 原始请求对象
336
+ * @param res - 响应对象
337
+ * @param _options - 服务器选项
338
+ */
247
339
  this.handleOnProxyReq = (proxyReq, req, res, _options) => {
248
- if (this.currentCookie) {
340
+ console.log("[AutoProxyCookie] === handleOnProxyReq START ===");
341
+ console.log("[AutoProxyCookie] Request URL:", req.method, req.url);
342
+ console.log("[AutoProxyCookie] useCookie:", this.options.useCookie);
343
+ console.log("[AutoProxyCookie] Current cookie:", this.currentCookie ? `(length: ${this.currentCookie.length})` : "(empty)");
344
+ if (this.options.useCookie && this.currentCookie) {
345
+ console.log("[AutoProxyCookie] Applying cookie header...");
249
346
  applyDevCookieHeader(proxyReq, this.currentCookie);
347
+ console.log("[AutoProxyCookie] Cookie header applied successfully");
348
+ } else if (!this.options.useCookie) {
349
+ console.log("[AutoProxyCookie] useCookie is false, skipping cookie injection");
350
+ } else {
351
+ console.log("[AutoProxyCookie] No cookie to apply - currentCookie is empty!");
250
352
  }
251
353
  this.log("debug", "[AutoProxyCookie] Proxy Request:", req.method, req.url);
252
354
  if (this.options.hooks.onProxyReq) {
@@ -256,7 +358,14 @@ var AutoProxyCookie = class {
256
358
  this.log("error", "[AutoProxyCookie] onProxyReq hook error:", err.message);
257
359
  }
258
360
  }
361
+ console.log("[AutoProxyCookie] === handleOnProxyReq END ===");
259
362
  };
363
+ /**
364
+ * 代理响应处理函数
365
+ * @param proxyRes - 代理响应对象
366
+ * @param req - 原始请求对象
367
+ * @param res - 响应对象
368
+ */
260
369
  this.handleOnProxyRes = (proxyRes, req, res) => {
261
370
  const allowedHeaders = [
262
371
  "Content-Type",
@@ -280,6 +389,12 @@ var AutoProxyCookie = class {
280
389
  }
281
390
  }
282
391
  };
392
+ /**
393
+ * HTTP 代理错误处理函数
394
+ * @param err - 错误对象
395
+ * @param req - 请求对象
396
+ * @param res - 响应对象或 Socket
397
+ */
283
398
  this.handleOnError = (err, req, res) => {
284
399
  this.log("error", "[AutoProxyCookie] Proxy Error:", err.message);
285
400
  this.log("error", "[AutoProxyCookie] URL:", req.url);
@@ -299,6 +414,12 @@ var AutoProxyCookie = class {
299
414
  }
300
415
  }
301
416
  };
417
+ /**
418
+ * WebSocket 代理错误处理函数
419
+ * @param err - 错误对象
420
+ * @param req - 请求对象
421
+ * @param socket - WebSocket 连接的 Socket
422
+ */
302
423
  this.handleOnWsError = (err, req, socket) => {
303
424
  this.log("error", "[AutoProxyCookie] WebSocket Proxy Error:", err.message);
304
425
  this.log("error", "[AutoProxyCookie] WebSocket URL:", req.url);
@@ -335,6 +456,7 @@ var AutoProxyCookie = class {
335
456
  autoRestart: false,
336
457
  restartMarkerFile: ".cookie-restart-marker",
337
458
  proxyMap: {},
459
+ proxyPaths: [],
338
460
  ignorePaths: [],
339
461
  ws: true,
340
462
  changeOrigin: true,
@@ -346,29 +468,63 @@ var AutoProxyCookie = class {
346
468
  cookieDomainRewrite: "*",
347
469
  cookiePathRewrite: false,
348
470
  headers: {},
471
+ useCookie: true,
349
472
  ...mergedOptions
350
473
  };
351
- this.cookieReader = new CookieReader({ cookieFile: options.cookieFile });
474
+ this.cookieReader = new CookieReader({ cookieFile: options.cookieFile }, options.debug ?? false);
352
475
  }
476
+ /**
477
+ * 根据请求路径获取代理目标 URL
478
+ * @param req - HTTP 请求对象
479
+ * @returns 代理目标 URL
480
+ */
353
481
  getProxyUrl(req) {
354
- const pathname = req.url?.split("?")[0] || "/";
482
+ const fullUrl = req.url || "/";
483
+ const pathname = new URL(fullUrl, "http://localhost").pathname;
355
484
  const proxyMap = this.options.proxyMap || {};
485
+ const proxyPaths = this.options.proxyPaths || [];
486
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - Request path:", pathname);
487
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - Available proxyMap paths:", Object.keys(proxyMap));
488
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - Available proxyPaths:", proxyPaths);
356
489
  for (const [prefix, target] of Object.entries(proxyMap)) {
357
- if (pathname.startsWith(prefix)) {
490
+ const matches = pathname.startsWith(prefix);
491
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - Checking proxyMap:", prefix, "- matches:", matches);
492
+ if (matches) {
493
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - Matched proxyMap:", prefix, "->", target);
358
494
  return target;
359
495
  }
360
496
  }
497
+ for (const prefix of proxyPaths) {
498
+ const matches = pathname.startsWith(prefix);
499
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - Checking proxyPaths:", prefix, "- matches:", matches);
500
+ if (matches) {
501
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - Matched proxyPaths:", prefix, "->", this.options.target);
502
+ return this.options.target;
503
+ }
504
+ }
505
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - No match found, using default target:", this.options.target);
361
506
  return this.options.target;
362
507
  }
508
+ /**
509
+ * 判断路径是否应该被忽略(不代理)
510
+ * @param pathname - 请求路径
511
+ * @returns 是否忽略
512
+ */
363
513
  isIgnoredPath(pathname) {
364
514
  const ignorePaths = this.options.ignorePaths || [];
365
515
  return ignorePaths.some(
366
516
  (ignored) => pathname.startsWith(ignored)
367
517
  );
368
518
  }
519
+ /**
520
+ * 日志输出函数
521
+ * @param level - 日志级别
522
+ * @param args - 日志参数
523
+ */
369
524
  log(level, ...args) {
370
525
  const levels = { debug: 0, info: 1, warn: 2, error: 3 };
371
- const currentLevel = levels[this.options.logLevel || "info"];
526
+ const effectiveLogLevel = this.options.debug ? "debug" : this.options.logLevel || "info";
527
+ const currentLevel = levels[effectiveLogLevel];
372
528
  const msgLevel = levels[level];
373
529
  if (msgLevel >= currentLevel) {
374
530
  if (level === "error") {
@@ -380,6 +536,11 @@ var AutoProxyCookie = class {
380
536
  }
381
537
  }
382
538
  }
539
+ /**
540
+ * 创建代理服务器配置选项
541
+ * @param target - 代理目标地址
542
+ * @returns 代理服务器配置
543
+ */
383
544
  createProxyOptions(target) {
384
545
  const {
385
546
  ws,
@@ -408,6 +569,10 @@ var AutoProxyCookie = class {
408
569
  ignorePath: false
409
570
  };
410
571
  }
572
+ /**
573
+ * 初始化代理中间件
574
+ * @param server - Vite 开发服务器实例
575
+ */
411
576
  async setup(server) {
412
577
  this.server = server;
413
578
  this.currentCookie = this.cookieReader.readCookie();
@@ -435,25 +600,47 @@ var AutoProxyCookie = class {
435
600
  this.log("warn", "[AutoProxyCookie] http-proxy create failed, using basic mode:", err.message);
436
601
  }
437
602
  server.middlewares.use((req, res, next) => {
438
- const pathname = new URL(req.url || "/", "http://localhost").pathname;
603
+ const fullUrl = req.url || "/";
604
+ const pathname = new URL(fullUrl, "http://localhost").pathname;
605
+ console.log("[AutoProxyCookie] === Incoming Request ===");
606
+ console.log("[AutoProxyCookie] Method:", req.method);
607
+ console.log("[AutoProxyCookie] Full URL:", fullUrl);
608
+ console.log("[AutoProxyCookie] Pathname:", pathname);
609
+ console.log("[AutoProxyCookie] useCookie:", this.options.useCookie);
610
+ console.log("[AutoProxyCookie] Headers:", JSON.stringify(req.headers, null, 2));
439
611
  if (this.isIgnoredPath(pathname)) {
612
+ console.log("[AutoProxyCookie] Path ignored, passing to next middleware");
440
613
  next();
441
614
  return;
442
615
  }
443
- this.currentCookie = this.cookieReader.readCookie();
616
+ if (this.options.useCookie) {
617
+ this.currentCookie = this.cookieReader.readCookie();
618
+ console.log("[AutoProxyCookie] Current cookie:", this.currentCookie ? `(length: ${this.currentCookie.length})` : "(empty)");
619
+ if (this.currentCookie) {
620
+ console.log("[AutoProxyCookie] Cookie preview:", this.currentCookie);
621
+ }
622
+ } else {
623
+ console.log("[AutoProxyCookie] useCookie is false, skipping cookie reading");
624
+ }
625
+ const proxyMapKeys = Object.keys(this.options.proxyMap || {});
626
+ const proxyPaths = this.options.proxyPaths || [];
627
+ const allProxyPrefixes = [...proxyMapKeys, ...proxyPaths];
628
+ const matched = allProxyPrefixes.some((prefix) => pathname.startsWith(prefix));
629
+ console.log("[AutoProxyCookie] Path matches proxy rules:", matched);
444
630
  if (this.proxyServer) {
445
631
  const target = this.getProxyUrl(req);
446
- if (this.options.debug || this.options.logLevel === "debug") {
447
- this.log("info", `[AutoProxyCookie] ${pathname} -> ${target}`);
448
- }
632
+ console.log(`[AutoProxyCookie] Proxying ${req.method} ${pathname} -> ${target}`);
449
633
  const proxyOptions = this.createProxyOptions(target);
450
634
  try {
635
+ console.log("[AutoProxyCookie] Calling proxyServer.web...");
451
636
  this.proxyServer.web(req, res, proxyOptions);
637
+ console.log("[AutoProxyCookie] proxyServer.web called successfully");
452
638
  } catch (err) {
453
- this.log("error", "[AutoProxyCookie] Proxy web error:", err.message);
639
+ console.error("[AutoProxyCookie] Proxy web error:", err.message);
454
640
  next(err);
455
641
  }
456
642
  } else {
643
+ console.log("[AutoProxyCookie] No proxy server, passing to next middleware");
457
644
  next();
458
645
  }
459
646
  });
@@ -485,6 +672,9 @@ var AutoProxyCookie = class {
485
672
  this.log("info", "[AutoProxyCookie] WebSocket support enabled");
486
673
  }
487
674
  }
675
+ /**
676
+ * 启动 Cookie 文件监听
677
+ */
488
678
  startFileWatch() {
489
679
  let shouldWatch;
490
680
  if (this.options.isDev !== void 0) {
@@ -510,6 +700,9 @@ var AutoProxyCookie = class {
510
700
  console.log("[AutoProxyCookie] File watch disabled");
511
701
  }
512
702
  }
703
+ /**
704
+ * 停止代理服务
705
+ */
513
706
  stop() {
514
707
  if (this.watcher) {
515
708
  this.watcher.stop();
@@ -521,6 +714,10 @@ var AutoProxyCookie = class {
521
714
  }
522
715
  this.log("info", "[AutoProxyCookie] Stopped");
523
716
  }
717
+ /**
718
+ * 获取当前 Cookie 值
719
+ * @returns 当前 Cookie 字符串
720
+ */
524
721
  getCurrentCookie() {
525
722
  return this.currentCookie;
526
723
  }
@@ -529,92 +726,90 @@ function createAutoProxyCookie(options) {
529
726
  return new AutoProxyCookie(options);
530
727
  }
531
728
 
532
- // src/proxy/vite-plugin.ts
533
- function viteAutoProxyCookie(options) {
534
- const {
535
- name = "vite-auto-proxy-cookie",
536
- isDev,
537
- ...autoProxyOptions
538
- } = options;
539
- let autoProxy = null;
540
- return {
541
- name,
542
- apply: "serve",
543
- async configureServer(server) {
544
- autoProxy = createAutoProxyCookie({
545
- ...autoProxyOptions,
546
- debug: options.debug ?? false,
547
- autoRestart: options.autoRestart ?? true,
548
- isDev
549
- });
550
- try {
551
- await autoProxy.setup(server);
552
- } catch (error) {
553
- console.error("[vite-auto-proxy-cookie] Failed to setup proxy:", error);
554
- if (options.debug) {
555
- console.log("[vite-auto-proxy-cookie] Falling back to middleware mode");
556
- }
557
- }
558
- },
559
- closeBundle() {
560
- autoProxy?.stop();
729
+ // src/proxy/vite-middleware-plugin.ts
730
+ function isIgnoredPath(pathname, ignorePaths) {
731
+ return ignorePaths.some((ignored) => pathname.startsWith(ignored));
732
+ }
733
+ function shouldProxy(pathname, proxyPrefixes) {
734
+ return proxyPrefixes.some((prefix) => pathname.startsWith(prefix));
735
+ }
736
+ function getProxyTarget(pathname, proxyMap, defaultTarget) {
737
+ for (const [prefix, target] of Object.entries(proxyMap)) {
738
+ if (pathname.startsWith(prefix)) {
739
+ return target;
561
740
  }
562
- };
741
+ }
742
+ return defaultTarget;
563
743
  }
564
-
565
- // src/proxy/vite-cookie-plugin.ts
566
- function viteDevProxyCookie(options) {
567
- const { cookieFile, debug = false, onCookieChange, watch = "auto", isDev } = options;
568
- let watcher = null;
744
+ function viteMiddlewareProxy(options) {
745
+ const {
746
+ cookieFile,
747
+ target,
748
+ debug = false,
749
+ useCookie = true,
750
+ proxyMap = {},
751
+ proxyPaths = [],
752
+ ignorePaths = []
753
+ } = options;
754
+ const cookieReader = new CookieReader({ cookieFile }, debug);
569
755
  let currentCookie = "";
570
- const cookieReader = new CookieReader({ cookieFile });
756
+ if (useCookie) {
757
+ currentCookie = cookieReader.readCookie();
758
+ }
759
+ const allProxyPrefixes = [
760
+ ...Object.keys(proxyMap),
761
+ ...proxyPaths
762
+ ];
571
763
  return {
572
- name: "vite-dev-proxy-cookie",
764
+ name: "vite-middleware-proxy",
573
765
  apply: "serve",
574
766
  configureServer(server) {
575
- currentCookie = cookieReader.readCookie();
576
- if (debug) {
577
- console.log("[vite-dev-proxy-cookie] Initial cookie loaded");
767
+ const httpProxy2 = __require("http-proxy");
768
+ const proxyServer = httpProxy2.createProxyServer({});
769
+ if (useCookie && debug) {
770
+ console.log("[ViteMiddlewareProxy] Watching cookie file:", cookieFile);
578
771
  }
579
- const middlewares = server.middlewares || server._middlewares || server.app;
580
- if (middlewares && typeof middlewares.use === "function") {
581
- middlewares.use((req, _res, next) => {
582
- if (currentCookie && req.url?.startsWith("/")) {
583
- req.headers = req.headers || {};
584
- req.headers["cookie"] = currentCookie;
772
+ server.middlewares.use((req, res, next) => {
773
+ const pathname = new URL(req.url || "/", "http://localhost").pathname;
774
+ if (isIgnoredPath(pathname, ignorePaths)) {
775
+ if (debug) {
776
+ console.log("[ViteMiddlewareProxy] Ignoring:", pathname);
585
777
  }
586
778
  next();
587
- });
588
- } else {
589
- console.warn("[vite-dev-proxy-cookie] Could not access middleware stack, cookie injection disabled");
590
- }
591
- let shouldWatch;
592
- if (isDev !== void 0) {
593
- shouldWatch = isDev;
779
+ return;
780
+ }
781
+ if (!shouldProxy(pathname, allProxyPrefixes)) {
782
+ next();
783
+ return;
784
+ }
785
+ const proxyTarget = getProxyTarget(pathname, proxyMap, target);
594
786
  if (debug) {
595
- console.log(`[vite-dev-proxy-cookie] isDev=${isDev}, ${shouldWatch ? "enabling" : "disabling"} watch`);
787
+ console.log("[ViteMiddlewareProxy] Proxying:", req.method, pathname, "->", proxyTarget);
596
788
  }
597
- } else {
598
- shouldWatch = shouldEnableWatch(watch, [], debug, "[vite-dev-proxy-cookie]");
599
- }
600
- if (shouldWatch) {
601
- watcher = watchCookieFile(
602
- cookieFile,
603
- (newCookie) => {
604
- currentCookie = newCookie;
605
- onCookieChange?.(newCookie);
606
- console.log("[vite-dev-proxy-cookie] Cookie changed, please restart server for full effect");
607
- },
608
- (error) => {
609
- console.error("[vite-dev-proxy-cookie] Watch error:", error.message);
789
+ if (useCookie) {
790
+ currentCookie = cookieReader.readCookie();
791
+ if (currentCookie) {
792
+ if (debug) {
793
+ console.log("[ViteMiddlewareProxy] Injecting cookie:", `(length: ${currentCookie.length})`);
794
+ }
795
+ req.headers["cookie"] = currentCookie;
796
+ req.headers["Cookie"] = currentCookie;
797
+ } else if (debug) {
798
+ console.log("[ViteMiddlewareProxy] Cookie file is empty");
610
799
  }
611
- );
612
- } else if (debug) {
613
- console.log("[vite-dev-proxy-cookie] File watch disabled");
614
- }
615
- },
616
- closeBundle() {
617
- watcher?.stop();
800
+ } else if (debug) {
801
+ console.log("[ViteMiddlewareProxy] useCookie is false, skipping cookie injection");
802
+ }
803
+ proxyServer.web(req, res, {
804
+ target: proxyTarget,
805
+ changeOrigin: true,
806
+ secure: false,
807
+ ignorePath: false
808
+ });
809
+ });
810
+ proxyServer.on("error", (err, req) => {
811
+ console.error("[ViteMiddlewareProxy] Proxy error:", err.message, "for", req.url);
812
+ });
618
813
  }
619
814
  };
620
815
  }
@@ -625,6 +820,7 @@ function createVueProxyConfig(target, options = {}) {
625
820
  const {
626
821
  getCookie,
627
822
  debug = false,
823
+ useCookie = true,
628
824
  headers = {},
629
825
  ws = false,
630
826
  changeOrigin = true,
@@ -638,13 +834,17 @@ function createVueProxyConfig(target, options = {}) {
638
834
  secure,
639
835
  headers,
640
836
  onProxyReq: (proxyReq, req) => {
641
- const cookie = getCookie ? getCookie() : "";
642
- if (cookie) {
643
- applyDevCookieHeader(proxyReq, cookie);
644
- }
645
- if (debug) {
646
- const reqPath = req.url || "/";
647
- console.log("[Proxy Request]", reqPath, req.method, cookie ? "(with cookie)" : "(no cookie)");
837
+ const reqPath = req.url || "/";
838
+ if (useCookie) {
839
+ const cookie = getCookie ? getCookie() : "";
840
+ if (cookie) {
841
+ applyDevCookieHeader(proxyReq, cookie);
842
+ }
843
+ if (debug) {
844
+ console.log("[Proxy Request]", reqPath, req.method, cookie ? "(with cookie)" : "(no cookie)");
845
+ }
846
+ } else if (debug) {
847
+ console.log("[Proxy Request]", reqPath, req.method, "(useCookie is false, skipping cookie injection)");
648
848
  }
649
849
  },
650
850
  onError: customOnError || ((err) => {
@@ -656,11 +856,11 @@ function createVueProxyConfig(target, options = {}) {
656
856
  function createFileCookieGetter(cookieFile, options = {}) {
657
857
  const {
658
858
  watch = "auto",
659
- debug = false,
859
+ debug = true,
660
860
  productionEnvs = [],
661
861
  isDev
662
862
  } = options;
663
- const reader = new CookieReader({ cookieFile: path4.resolve(cookieFile) });
863
+ const reader = new CookieReader({ cookieFile: path4.resolve(cookieFile) }, debug);
664
864
  let shouldWatch;
665
865
  if (isDev !== void 0) {
666
866
  shouldWatch = isDev;
@@ -688,11 +888,11 @@ function createFileCookieGetter(cookieFile, options = {}) {
688
888
  return () => reader.readCookie();
689
889
  }
690
890
  function createAutoProxyConfig(options) {
691
- const { target, ignorePaths = [], includePaths = [], additionalProxies = {}, getCookie, debug, headers } = options;
891
+ const { target, ignorePaths = [], includePaths = [], additionalProxies = {}, getCookie, debug, headers, useCookie = true } = options;
692
892
  const result = {};
693
893
  if (includePaths.length > 0) {
694
894
  for (const proxyPath of includePaths) {
695
- result[proxyPath] = createVueProxyConfig(target, { getCookie, debug, headers });
895
+ result[proxyPath] = createVueProxyConfig(target, { getCookie, debug, headers, useCookie });
696
896
  }
697
897
  } else {
698
898
  const defaultProxy = {
@@ -706,12 +906,16 @@ function createAutoProxyConfig(options) {
706
906
  if (ignorePaths.some((p) => reqPath.startsWith(p))) {
707
907
  return;
708
908
  }
709
- const cookie = getCookie ? getCookie() : "";
710
- if (cookie) {
711
- applyDevCookieHeader(proxyReq, cookie);
712
- }
713
- if (debug) {
714
- console.log("[Proxy Request]", reqPath, req.method, cookie ? "(with cookie)" : "(no cookie)");
909
+ if (useCookie) {
910
+ const cookie = getCookie ? getCookie() : "";
911
+ if (cookie) {
912
+ applyDevCookieHeader(proxyReq, cookie);
913
+ }
914
+ if (debug) {
915
+ console.log("[Proxy Request]", reqPath, req.method, cookie ? "(with cookie)" : "(no cookie)");
916
+ }
917
+ } else if (debug) {
918
+ console.log("[Proxy Request]", reqPath, req.method, "(useCookie is false, skipping cookie injection)");
715
919
  }
716
920
  },
717
921
  onError: (err) => {
@@ -721,65 +925,10 @@ function createAutoProxyConfig(options) {
721
925
  result["/"] = defaultProxy;
722
926
  }
723
927
  for (const [proxyPath, proxyTarget] of Object.entries(additionalProxies)) {
724
- result[proxyPath] = createVueProxyConfig(proxyTarget, { getCookie, debug, headers });
928
+ result[proxyPath] = createVueProxyConfig(proxyTarget, { getCookie, debug, headers, useCookie });
725
929
  }
726
930
  return result;
727
931
  }
728
-
729
- // src/proxy/vite-adapter.ts
730
- var viteVersion = "";
731
- var majorVersion = null;
732
- function detectViteVersion() {
733
- if (majorVersion !== null) {
734
- return majorVersion;
735
- }
736
- try {
737
- const pkg = __require("vite/package.json");
738
- viteVersion = pkg.version;
739
- majorVersion = parseInt(viteVersion.split(".")[0], 10);
740
- } catch {
741
- majorVersion = 5;
742
- }
743
- return majorVersion;
744
- }
745
- function createDevProxyCookie(options) {
746
- const {
747
- mode = "auto",
748
- watch = "auto",
749
- isDev,
750
- ...restOptions
751
- } = options;
752
- const version = detectViteVersion();
753
- if (options.debug) {
754
- console.log(`[dev-proxy-cookie] Detected Vite ${version}.x`);
755
- }
756
- if (mode === "cookie" || mode === "auto" && !options.target) {
757
- return viteDevProxyCookie({
758
- cookieFile: options.cookieFile,
759
- debug: options.debug,
760
- onCookieChange: options.onCookieChange,
761
- watch,
762
- isDev
763
- });
764
- }
765
- const pluginOptions = {
766
- cookieFile: options.cookieFile,
767
- target: options.target,
768
- debug: options.debug,
769
- autoRestart: options.autoRestart ?? true,
770
- restartMarkerFile: options.restartMarkerFile,
771
- proxyMap: options.proxyMap,
772
- ignorePaths: options.ignorePaths
773
- };
774
- return viteAutoProxyCookie(pluginOptions);
775
- }
776
- function getViteVersion() {
777
- detectViteVersion();
778
- return viteVersion;
779
- }
780
- function getViteMajorVersion() {
781
- return detectViteVersion();
782
- }
783
932
  export {
784
933
  AutoProxyCookie,
785
934
  CookieReader,
@@ -787,15 +936,11 @@ export {
787
936
  createAutoProxyConfig,
788
937
  createAutoProxyCookie,
789
938
  createCookieGetter,
790
- createDevProxyCookie,
791
939
  createFileCookieGetter,
792
940
  createVueProxyConfig,
793
941
  detectProductionEnvironment,
794
- getViteMajorVersion,
795
- getViteVersion,
796
942
  isProductionValue,
797
943
  shouldEnableWatch,
798
- viteAutoProxyCookie,
799
- viteDevProxyCookie,
944
+ viteMiddlewareProxy,
800
945
  watchCookieFile
801
946
  };