@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.js CHANGED
@@ -36,16 +36,12 @@ __export(index_exports, {
36
36
  createAutoProxyConfig: () => createAutoProxyConfig,
37
37
  createAutoProxyCookie: () => createAutoProxyCookie,
38
38
  createCookieGetter: () => createCookieGetter,
39
- createDevProxyCookie: () => createDevProxyCookie,
40
39
  createFileCookieGetter: () => createFileCookieGetter,
41
40
  createVueProxyConfig: () => createVueProxyConfig,
42
41
  detectProductionEnvironment: () => detectProductionEnvironment,
43
- getViteMajorVersion: () => getViteMajorVersion,
44
- getViteVersion: () => getViteVersion,
45
42
  isProductionValue: () => isProductionValue,
46
43
  shouldEnableWatch: () => shouldEnableWatch,
47
- viteAutoProxyCookie: () => viteAutoProxyCookie,
48
- viteDevProxyCookie: () => viteDevProxyCookie,
44
+ viteMiddlewareProxy: () => viteMiddlewareProxy,
49
45
  watchCookieFile: () => watchCookieFile
50
46
  });
51
47
  module.exports = __toCommonJS(index_exports);
@@ -60,26 +56,62 @@ var import_http_proxy = __toESM(require("http-proxy"));
60
56
  var fs = __toESM(require("fs"));
61
57
  var path = __toESM(require("path"));
62
58
  var CookieReader = class {
63
- constructor(options) {
59
+ /**
60
+ * 构造函数
61
+ * @param options - 配置选项
62
+ * @param debug - 是否启用调试模式
63
+ */
64
+ constructor(options, debug = false) {
64
65
  this.options = {
65
66
  encoding: "utf-8",
66
67
  ...options
67
68
  };
68
- }
69
+ this.debug = debug;
70
+ }
71
+ /**
72
+ * 读取 Cookie 文件内容
73
+ *
74
+ * 支持过滤注释行(以 # 开头)和空行,将有效行用分号连接。
75
+ *
76
+ * @returns Cookie 字符串
77
+ */
69
78
  readCookie() {
70
79
  try {
71
80
  const filePath = path.resolve(this.options.cookieFile);
81
+ if (this.debug) {
82
+ console.log("[CookieReader] Resolved cookie file path:", filePath);
83
+ console.log("[CookieReader] File exists:", fs.existsSync(filePath));
84
+ }
72
85
  if (fs.existsSync(filePath)) {
73
86
  const content = fs.readFileSync(filePath, this.options.encoding || "utf-8");
87
+ if (this.debug) {
88
+ console.log("[CookieReader] File content length:", content.length);
89
+ }
74
90
  const lines = content.split("\n");
75
91
  const cookieLines = lines.map((line) => line.trim()).filter((line) => line && !line.startsWith("#"));
76
- return cookieLines.join("; ");
92
+ const result = cookieLines.join("; ");
93
+ if (this.debug) {
94
+ console.log("[CookieReader] Parsed cookie:", result ? "(has cookie)" : "(empty)");
95
+ }
96
+ return result;
97
+ }
98
+ if (this.debug) {
99
+ console.log("[CookieReader] Cookie file not found:", filePath);
77
100
  }
78
101
  return "";
79
- } catch {
102
+ } catch (err) {
103
+ if (this.debug) {
104
+ console.error("[CookieReader] Error reading cookie file:", err.message);
105
+ }
80
106
  return "";
81
107
  }
82
108
  }
109
+ /**
110
+ * 确保 Cookie 文件存在
111
+ *
112
+ * 如果文件不存在,会自动创建空文件。
113
+ * 如果父目录不存在,会自动创建目录。
114
+ */
83
115
  ensureCookieFile() {
84
116
  const filePath = path.resolve(this.options.cookieFile);
85
117
  const dir = path.dirname(filePath);
@@ -100,9 +132,20 @@ function createCookieGetter(cookieFile) {
100
132
  var path2 = __toESM(require("path"));
101
133
  var import_chokidar = __toESM(require("chokidar"));
102
134
  var CookieWatcher = class {
135
+ /**
136
+ * 构造函数
137
+ * @param options - 配置选项
138
+ */
103
139
  constructor(options) {
140
+ /** 文件监听器实例 */
104
141
  this.watcher = null;
142
+ /** 上次读取的 Cookie 内容 */
105
143
  this.lastContent = "";
144
+ /**
145
+ * 文件变化处理函数
146
+ *
147
+ * 读取新的 Cookie 内容,如果与上次不同则触发回调。
148
+ */
106
149
  this.handleChange = () => {
107
150
  const newContent = this.cookieReader.readCookie();
108
151
  if (newContent !== this.lastContent) {
@@ -117,6 +160,9 @@ var CookieWatcher = class {
117
160
  };
118
161
  this.cookieReader = new CookieReader({ cookieFile: options.cookieFile });
119
162
  }
163
+ /**
164
+ * 启动文件监听
165
+ */
120
166
  start() {
121
167
  if (this.options.autoCreateFile) {
122
168
  this.cookieReader.ensureCookieFile();
@@ -142,6 +188,9 @@ var CookieWatcher = class {
142
188
  this.options.onError?.(error);
143
189
  }
144
190
  }
191
+ /**
192
+ * 停止文件监听
193
+ */
145
194
  stop() {
146
195
  if (this.watcher) {
147
196
  this.watcher.close();
@@ -149,6 +198,10 @@ var CookieWatcher = class {
149
198
  console.log(`[CookieWatcher] Stopped watching: ${this.options.cookieFile}`);
150
199
  }
151
200
  }
201
+ /**
202
+ * 获取当前 Cookie 值
203
+ * @returns 当前 Cookie 字符串
204
+ */
152
205
  getCurrentCookie() {
153
206
  return this.lastContent;
154
207
  }
@@ -261,19 +314,47 @@ function shouldEnableWatch(watch, customEnvs = [], debug = false, loggerPrefix =
261
314
 
262
315
  // src/proxy/apply-dev-cookie-header.ts
263
316
  function applyDevCookieHeader(proxyReq, cookie) {
264
- if (!cookie) return;
317
+ console.log("[applyDevCookieHeader] === START ===");
318
+ console.log("[applyDevCookieHeader] Cookie to apply:", cookie ? `(length: ${cookie.length})` : "(empty)");
319
+ if (!cookie) {
320
+ console.log("[applyDevCookieHeader] Cookie is empty, returning");
321
+ console.log("[applyDevCookieHeader] === END ===");
322
+ return;
323
+ }
324
+ const existingCookie = proxyReq.getHeader?.("Cookie");
325
+ console.log("[applyDevCookieHeader] Cookie current:", existingCookie ? `(length: ${String(existingCookie).length})` : "(none)");
326
+ if (existingCookie === cookie) {
327
+ console.log("[applyDevCookieHeader] Cookie is already set, skipping");
328
+ console.log("[applyDevCookieHeader] === END ===");
329
+ return;
330
+ }
265
331
  proxyReq.removeHeader("cookie");
266
332
  proxyReq.removeHeader("Cookie");
267
333
  proxyReq.setHeader("Cookie", cookie);
334
+ const newCookie = proxyReq.getHeader?.("Cookie");
335
+ console.log("[applyDevCookieHeader] Cookie new:", newCookie ? `(length: ${String(newCookie).length})` : "(failed)");
336
+ console.log("[applyDevCookieHeader] === END ===");
268
337
  }
269
338
 
270
339
  // src/proxy/core.ts
271
340
  var AutoProxyCookie = class {
341
+ /**
342
+ * 构造函数
343
+ * @param options - 配置选项
344
+ */
272
345
  constructor(options) {
346
+ /** 当前 Cookie 值 */
273
347
  this.currentCookie = "";
348
+ /** Vite 开发服务器实例 */
274
349
  this.server = null;
350
+ /** HTTP 代理服务器实例 */
275
351
  this.proxyServer = null;
352
+ /** Cookie 文件监听器 */
276
353
  this.watcher = null;
354
+ /**
355
+ * Cookie 变化处理函数
356
+ * @param newCookie - 新的 Cookie 值
357
+ */
277
358
  this.handleCookieChange = (newCookie) => {
278
359
  if (newCookie !== this.currentCookie) {
279
360
  this.currentCookie = newCookie;
@@ -289,9 +370,23 @@ var AutoProxyCookie = class {
289
370
  }
290
371
  }
291
372
  };
373
+ /**
374
+ * 代理请求处理函数
375
+ * @param proxyReq - 代理请求对象
376
+ * @param req - 原始请求对象
377
+ * @param res - 响应对象
378
+ * @param _options - 服务器选项
379
+ */
292
380
  this.handleOnProxyReq = (proxyReq, req, res, _options) => {
381
+ console.log("[AutoProxyCookie] === handleOnProxyReq START ===");
382
+ console.log("[AutoProxyCookie] Request URL:", req.method, req.url);
383
+ console.log("[AutoProxyCookie] Current cookie:", this.currentCookie ? `(length: ${this.currentCookie.length})` : "(empty)");
293
384
  if (this.currentCookie) {
385
+ console.log("[AutoProxyCookie] Applying cookie header...");
294
386
  applyDevCookieHeader(proxyReq, this.currentCookie);
387
+ console.log("[AutoProxyCookie] Cookie header applied successfully");
388
+ } else {
389
+ console.log("[AutoProxyCookie] No cookie to apply - currentCookie is empty!");
295
390
  }
296
391
  this.log("debug", "[AutoProxyCookie] Proxy Request:", req.method, req.url);
297
392
  if (this.options.hooks.onProxyReq) {
@@ -301,7 +396,14 @@ var AutoProxyCookie = class {
301
396
  this.log("error", "[AutoProxyCookie] onProxyReq hook error:", err.message);
302
397
  }
303
398
  }
399
+ console.log("[AutoProxyCookie] === handleOnProxyReq END ===");
304
400
  };
401
+ /**
402
+ * 代理响应处理函数
403
+ * @param proxyRes - 代理响应对象
404
+ * @param req - 原始请求对象
405
+ * @param res - 响应对象
406
+ */
305
407
  this.handleOnProxyRes = (proxyRes, req, res) => {
306
408
  const allowedHeaders = [
307
409
  "Content-Type",
@@ -325,6 +427,12 @@ var AutoProxyCookie = class {
325
427
  }
326
428
  }
327
429
  };
430
+ /**
431
+ * HTTP 代理错误处理函数
432
+ * @param err - 错误对象
433
+ * @param req - 请求对象
434
+ * @param res - 响应对象或 Socket
435
+ */
328
436
  this.handleOnError = (err, req, res) => {
329
437
  this.log("error", "[AutoProxyCookie] Proxy Error:", err.message);
330
438
  this.log("error", "[AutoProxyCookie] URL:", req.url);
@@ -344,6 +452,12 @@ var AutoProxyCookie = class {
344
452
  }
345
453
  }
346
454
  };
455
+ /**
456
+ * WebSocket 代理错误处理函数
457
+ * @param err - 错误对象
458
+ * @param req - 请求对象
459
+ * @param socket - WebSocket 连接的 Socket
460
+ */
347
461
  this.handleOnWsError = (err, req, socket) => {
348
462
  this.log("error", "[AutoProxyCookie] WebSocket Proxy Error:", err.message);
349
463
  this.log("error", "[AutoProxyCookie] WebSocket URL:", req.url);
@@ -380,6 +494,7 @@ var AutoProxyCookie = class {
380
494
  autoRestart: false,
381
495
  restartMarkerFile: ".cookie-restart-marker",
382
496
  proxyMap: {},
497
+ proxyPaths: [],
383
498
  ignorePaths: [],
384
499
  ws: true,
385
500
  changeOrigin: true,
@@ -393,27 +508,60 @@ var AutoProxyCookie = class {
393
508
  headers: {},
394
509
  ...mergedOptions
395
510
  };
396
- this.cookieReader = new CookieReader({ cookieFile: options.cookieFile });
511
+ this.cookieReader = new CookieReader({ cookieFile: options.cookieFile }, options.debug ?? false);
397
512
  }
513
+ /**
514
+ * 根据请求路径获取代理目标 URL
515
+ * @param req - HTTP 请求对象
516
+ * @returns 代理目标 URL
517
+ */
398
518
  getProxyUrl(req) {
399
- const pathname = req.url?.split("?")[0] || "/";
519
+ const fullUrl = req.url || "/";
520
+ const pathname = new URL(fullUrl, "http://localhost").pathname;
400
521
  const proxyMap = this.options.proxyMap || {};
522
+ const proxyPaths = this.options.proxyPaths || [];
523
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - Request path:", pathname);
524
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - Available proxyMap paths:", Object.keys(proxyMap));
525
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - Available proxyPaths:", proxyPaths);
401
526
  for (const [prefix, target] of Object.entries(proxyMap)) {
402
- if (pathname.startsWith(prefix)) {
527
+ const matches = pathname.startsWith(prefix);
528
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - Checking proxyMap:", prefix, "- matches:", matches);
529
+ if (matches) {
530
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - Matched proxyMap:", prefix, "->", target);
403
531
  return target;
404
532
  }
405
533
  }
534
+ for (const prefix of proxyPaths) {
535
+ const matches = pathname.startsWith(prefix);
536
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - Checking proxyPaths:", prefix, "- matches:", matches);
537
+ if (matches) {
538
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - Matched proxyPaths:", prefix, "->", this.options.target);
539
+ return this.options.target;
540
+ }
541
+ }
542
+ this.log("debug", "[AutoProxyCookie] getProxyUrl - No match found, using default target:", this.options.target);
406
543
  return this.options.target;
407
544
  }
545
+ /**
546
+ * 判断路径是否应该被忽略(不代理)
547
+ * @param pathname - 请求路径
548
+ * @returns 是否忽略
549
+ */
408
550
  isIgnoredPath(pathname) {
409
551
  const ignorePaths = this.options.ignorePaths || [];
410
552
  return ignorePaths.some(
411
553
  (ignored) => pathname.startsWith(ignored)
412
554
  );
413
555
  }
556
+ /**
557
+ * 日志输出函数
558
+ * @param level - 日志级别
559
+ * @param args - 日志参数
560
+ */
414
561
  log(level, ...args) {
415
562
  const levels = { debug: 0, info: 1, warn: 2, error: 3 };
416
- const currentLevel = levels[this.options.logLevel || "info"];
563
+ const effectiveLogLevel = this.options.debug ? "debug" : this.options.logLevel || "info";
564
+ const currentLevel = levels[effectiveLogLevel];
417
565
  const msgLevel = levels[level];
418
566
  if (msgLevel >= currentLevel) {
419
567
  if (level === "error") {
@@ -425,6 +573,11 @@ var AutoProxyCookie = class {
425
573
  }
426
574
  }
427
575
  }
576
+ /**
577
+ * 创建代理服务器配置选项
578
+ * @param target - 代理目标地址
579
+ * @returns 代理服务器配置
580
+ */
428
581
  createProxyOptions(target) {
429
582
  const {
430
583
  ws,
@@ -453,6 +606,10 @@ var AutoProxyCookie = class {
453
606
  ignorePath: false
454
607
  };
455
608
  }
609
+ /**
610
+ * 初始化代理中间件
611
+ * @param server - Vite 开发服务器实例
612
+ */
456
613
  async setup(server) {
457
614
  this.server = server;
458
615
  this.currentCookie = this.cookieReader.readCookie();
@@ -480,25 +637,42 @@ var AutoProxyCookie = class {
480
637
  this.log("warn", "[AutoProxyCookie] http-proxy create failed, using basic mode:", err.message);
481
638
  }
482
639
  server.middlewares.use((req, res, next) => {
483
- const pathname = new URL(req.url || "/", "http://localhost").pathname;
640
+ const fullUrl = req.url || "/";
641
+ const pathname = new URL(fullUrl, "http://localhost").pathname;
642
+ console.log("[AutoProxyCookie] === Incoming Request ===");
643
+ console.log("[AutoProxyCookie] Method:", req.method);
644
+ console.log("[AutoProxyCookie] Full URL:", fullUrl);
645
+ console.log("[AutoProxyCookie] Pathname:", pathname);
646
+ console.log("[AutoProxyCookie] Headers:", JSON.stringify(req.headers, null, 2));
484
647
  if (this.isIgnoredPath(pathname)) {
648
+ console.log("[AutoProxyCookie] Path ignored, passing to next middleware");
485
649
  next();
486
650
  return;
487
651
  }
488
652
  this.currentCookie = this.cookieReader.readCookie();
653
+ console.log("[AutoProxyCookie] Current cookie:", this.currentCookie ? `(length: ${this.currentCookie.length})` : "(empty)");
654
+ if (this.currentCookie) {
655
+ console.log("[AutoProxyCookie] Cookie preview:", this.currentCookie);
656
+ }
657
+ const proxyMapKeys = Object.keys(this.options.proxyMap || {});
658
+ const proxyPaths = this.options.proxyPaths || [];
659
+ const allProxyPrefixes = [...proxyMapKeys, ...proxyPaths];
660
+ const matched = allProxyPrefixes.some((prefix) => pathname.startsWith(prefix));
661
+ console.log("[AutoProxyCookie] Path matches proxy rules:", matched);
489
662
  if (this.proxyServer) {
490
663
  const target = this.getProxyUrl(req);
491
- if (this.options.debug || this.options.logLevel === "debug") {
492
- this.log("info", `[AutoProxyCookie] ${pathname} -> ${target}`);
493
- }
664
+ console.log(`[AutoProxyCookie] Proxying ${req.method} ${pathname} -> ${target}`);
494
665
  const proxyOptions = this.createProxyOptions(target);
495
666
  try {
667
+ console.log("[AutoProxyCookie] Calling proxyServer.web...");
496
668
  this.proxyServer.web(req, res, proxyOptions);
669
+ console.log("[AutoProxyCookie] proxyServer.web called successfully");
497
670
  } catch (err) {
498
- this.log("error", "[AutoProxyCookie] Proxy web error:", err.message);
671
+ console.error("[AutoProxyCookie] Proxy web error:", err.message);
499
672
  next(err);
500
673
  }
501
674
  } else {
675
+ console.log("[AutoProxyCookie] No proxy server, passing to next middleware");
502
676
  next();
503
677
  }
504
678
  });
@@ -530,6 +704,9 @@ var AutoProxyCookie = class {
530
704
  this.log("info", "[AutoProxyCookie] WebSocket support enabled");
531
705
  }
532
706
  }
707
+ /**
708
+ * 启动 Cookie 文件监听
709
+ */
533
710
  startFileWatch() {
534
711
  let shouldWatch;
535
712
  if (this.options.isDev !== void 0) {
@@ -555,6 +732,9 @@ var AutoProxyCookie = class {
555
732
  console.log("[AutoProxyCookie] File watch disabled");
556
733
  }
557
734
  }
735
+ /**
736
+ * 停止代理服务
737
+ */
558
738
  stop() {
559
739
  if (this.watcher) {
560
740
  this.watcher.stop();
@@ -566,6 +746,10 @@ var AutoProxyCookie = class {
566
746
  }
567
747
  this.log("info", "[AutoProxyCookie] Stopped");
568
748
  }
749
+ /**
750
+ * 获取当前 Cookie 值
751
+ * @returns 当前 Cookie 字符串
752
+ */
569
753
  getCurrentCookie() {
570
754
  return this.currentCookie;
571
755
  }
@@ -574,92 +758,80 @@ function createAutoProxyCookie(options) {
574
758
  return new AutoProxyCookie(options);
575
759
  }
576
760
 
577
- // src/proxy/vite-plugin.ts
578
- function viteAutoProxyCookie(options) {
579
- const {
580
- name = "vite-auto-proxy-cookie",
581
- isDev,
582
- ...autoProxyOptions
583
- } = options;
584
- let autoProxy = null;
585
- return {
586
- name,
587
- apply: "serve",
588
- async configureServer(server) {
589
- autoProxy = createAutoProxyCookie({
590
- ...autoProxyOptions,
591
- debug: options.debug ?? false,
592
- autoRestart: options.autoRestart ?? true,
593
- isDev
594
- });
595
- try {
596
- await autoProxy.setup(server);
597
- } catch (error) {
598
- console.error("[vite-auto-proxy-cookie] Failed to setup proxy:", error);
599
- if (options.debug) {
600
- console.log("[vite-auto-proxy-cookie] Falling back to middleware mode");
601
- }
602
- }
603
- },
604
- closeBundle() {
605
- autoProxy?.stop();
761
+ // src/proxy/vite-middleware-plugin.ts
762
+ function isIgnoredPath(pathname, ignorePaths) {
763
+ return ignorePaths.some((ignored) => pathname.startsWith(ignored));
764
+ }
765
+ function shouldProxy(pathname, proxyPrefixes) {
766
+ return proxyPrefixes.some((prefix) => pathname.startsWith(prefix));
767
+ }
768
+ function getProxyTarget(pathname, proxyMap, defaultTarget) {
769
+ for (const [prefix, target] of Object.entries(proxyMap)) {
770
+ if (pathname.startsWith(prefix)) {
771
+ return target;
606
772
  }
607
- };
773
+ }
774
+ return defaultTarget;
608
775
  }
609
-
610
- // src/proxy/vite-cookie-plugin.ts
611
- function viteDevProxyCookie(options) {
612
- const { cookieFile, debug = false, onCookieChange, watch = "auto", isDev } = options;
613
- let watcher = null;
614
- let currentCookie = "";
615
- const cookieReader = new CookieReader({ cookieFile });
776
+ function viteMiddlewareProxy(options) {
777
+ const {
778
+ cookieFile,
779
+ target,
780
+ debug = false,
781
+ proxyMap = {},
782
+ proxyPaths = [],
783
+ ignorePaths = []
784
+ } = options;
785
+ const cookieReader = new CookieReader({ cookieFile }, debug);
786
+ let currentCookie = cookieReader.readCookie();
787
+ const allProxyPrefixes = [
788
+ ...Object.keys(proxyMap),
789
+ ...proxyPaths
790
+ ];
616
791
  return {
617
- name: "vite-dev-proxy-cookie",
792
+ name: "vite-middleware-proxy",
618
793
  apply: "serve",
619
794
  configureServer(server) {
620
- currentCookie = cookieReader.readCookie();
795
+ const httpProxy2 = require("http-proxy");
796
+ const proxyServer = httpProxy2.createProxyServer({});
621
797
  if (debug) {
622
- console.log("[vite-dev-proxy-cookie] Initial cookie loaded");
623
- }
624
- const middlewares = server.middlewares || server._middlewares || server.app;
625
- if (middlewares && typeof middlewares.use === "function") {
626
- middlewares.use((req, _res, next) => {
627
- if (currentCookie && req.url?.startsWith("/")) {
628
- req.headers = req.headers || {};
629
- req.headers["cookie"] = currentCookie;
798
+ console.log("[ViteMiddlewareProxy] Watching cookie file:", cookieFile);
799
+ }
800
+ server.middlewares.use((req, res, next) => {
801
+ const pathname = new URL(req.url || "/", "http://localhost").pathname;
802
+ if (isIgnoredPath(pathname, ignorePaths)) {
803
+ if (debug) {
804
+ console.log("[ViteMiddlewareProxy] Ignoring:", pathname);
630
805
  }
631
806
  next();
632
- });
633
- } else {
634
- console.warn("[vite-dev-proxy-cookie] Could not access middleware stack, cookie injection disabled");
635
- }
636
- let shouldWatch;
637
- if (isDev !== void 0) {
638
- shouldWatch = isDev;
807
+ return;
808
+ }
809
+ if (!shouldProxy(pathname, allProxyPrefixes)) {
810
+ next();
811
+ return;
812
+ }
813
+ const proxyTarget = getProxyTarget(pathname, proxyMap, target);
639
814
  if (debug) {
640
- console.log(`[vite-dev-proxy-cookie] isDev=${isDev}, ${shouldWatch ? "enabling" : "disabling"} watch`);
815
+ console.log("[ViteMiddlewareProxy] Proxying:", req.method, pathname, "->", proxyTarget);
641
816
  }
642
- } else {
643
- shouldWatch = shouldEnableWatch(watch, [], debug, "[vite-dev-proxy-cookie]");
644
- }
645
- if (shouldWatch) {
646
- watcher = watchCookieFile(
647
- cookieFile,
648
- (newCookie) => {
649
- currentCookie = newCookie;
650
- onCookieChange?.(newCookie);
651
- console.log("[vite-dev-proxy-cookie] Cookie changed, please restart server for full effect");
652
- },
653
- (error) => {
654
- console.error("[vite-dev-proxy-cookie] Watch error:", error.message);
817
+ currentCookie = cookieReader.readCookie();
818
+ if (currentCookie) {
819
+ if (debug) {
820
+ console.log("[ViteMiddlewareProxy] Injecting cookie:", `(length: ${currentCookie.length})`);
655
821
  }
656
- );
657
- } else if (debug) {
658
- console.log("[vite-dev-proxy-cookie] File watch disabled");
659
- }
660
- },
661
- closeBundle() {
662
- watcher?.stop();
822
+ req.headers["cookie"] = currentCookie;
823
+ req.headers["Cookie"] = currentCookie;
824
+ }
825
+ proxyServer.web(req, res, {
826
+ target: proxyTarget,
827
+ changeOrigin: true,
828
+ secure: false,
829
+ ignorePath: false
830
+ });
831
+ });
832
+ proxyServer.on("error", (err, req) => {
833
+ console.error("[ViteMiddlewareProxy] Proxy error:", err.message, "for", req.url);
834
+ });
663
835
  }
664
836
  };
665
837
  }
@@ -701,11 +873,11 @@ function createVueProxyConfig(target, options = {}) {
701
873
  function createFileCookieGetter(cookieFile, options = {}) {
702
874
  const {
703
875
  watch = "auto",
704
- debug = false,
876
+ debug = true,
705
877
  productionEnvs = [],
706
878
  isDev
707
879
  } = options;
708
- const reader = new CookieReader({ cookieFile: path4.resolve(cookieFile) });
880
+ const reader = new CookieReader({ cookieFile: path4.resolve(cookieFile) }, debug);
709
881
  let shouldWatch;
710
882
  if (isDev !== void 0) {
711
883
  shouldWatch = isDev;
@@ -770,61 +942,6 @@ function createAutoProxyConfig(options) {
770
942
  }
771
943
  return result;
772
944
  }
773
-
774
- // src/proxy/vite-adapter.ts
775
- var viteVersion = "";
776
- var majorVersion = null;
777
- function detectViteVersion() {
778
- if (majorVersion !== null) {
779
- return majorVersion;
780
- }
781
- try {
782
- const pkg = require("vite/package.json");
783
- viteVersion = pkg.version;
784
- majorVersion = parseInt(viteVersion.split(".")[0], 10);
785
- } catch {
786
- majorVersion = 5;
787
- }
788
- return majorVersion;
789
- }
790
- function createDevProxyCookie(options) {
791
- const {
792
- mode = "auto",
793
- watch = "auto",
794
- isDev,
795
- ...restOptions
796
- } = options;
797
- const version = detectViteVersion();
798
- if (options.debug) {
799
- console.log(`[dev-proxy-cookie] Detected Vite ${version}.x`);
800
- }
801
- if (mode === "cookie" || mode === "auto" && !options.target) {
802
- return viteDevProxyCookie({
803
- cookieFile: options.cookieFile,
804
- debug: options.debug,
805
- onCookieChange: options.onCookieChange,
806
- watch,
807
- isDev
808
- });
809
- }
810
- const pluginOptions = {
811
- cookieFile: options.cookieFile,
812
- target: options.target,
813
- debug: options.debug,
814
- autoRestart: options.autoRestart ?? true,
815
- restartMarkerFile: options.restartMarkerFile,
816
- proxyMap: options.proxyMap,
817
- ignorePaths: options.ignorePaths
818
- };
819
- return viteAutoProxyCookie(pluginOptions);
820
- }
821
- function getViteVersion() {
822
- detectViteVersion();
823
- return viteVersion;
824
- }
825
- function getViteMajorVersion() {
826
- return detectViteVersion();
827
- }
828
945
  // Annotate the CommonJS export names for ESM import in node:
829
946
  0 && (module.exports = {
830
947
  AutoProxyCookie,
@@ -833,15 +950,11 @@ function getViteMajorVersion() {
833
950
  createAutoProxyConfig,
834
951
  createAutoProxyCookie,
835
952
  createCookieGetter,
836
- createDevProxyCookie,
837
953
  createFileCookieGetter,
838
954
  createVueProxyConfig,
839
955
  detectProductionEnvironment,
840
- getViteMajorVersion,
841
- getViteVersion,
842
956
  isProductionValue,
843
957
  shouldEnableWatch,
844
- viteAutoProxyCookie,
845
- viteDevProxyCookie,
958
+ viteMiddlewareProxy,
846
959
  watchCookieFile
847
960
  });