@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/README.md +212 -236
- package/dist/index.d.mts +304 -67
- package/dist/index.d.ts +304 -67
- package/dist/index.js +314 -173
- package/dist/index.min.js +4 -4
- package/dist/index.min.mjs +4 -4
- package/dist/index.mjs +313 -168
- 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 +198 -12
- package/src/proxy/index.ts +8 -3
- package/src/proxy/vite-middleware-plugin.ts +177 -0
- package/src/proxy/vue-proxy-config.ts +98 -18
- 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
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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,26 @@ 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) => {
|
|
293
|
-
|
|
381
|
+
console.log("[AutoProxyCookie] === handleOnProxyReq START ===");
|
|
382
|
+
console.log("[AutoProxyCookie] Request URL:", req.method, req.url);
|
|
383
|
+
console.log("[AutoProxyCookie] useCookie:", this.options.useCookie);
|
|
384
|
+
console.log("[AutoProxyCookie] Current cookie:", this.currentCookie ? `(length: ${this.currentCookie.length})` : "(empty)");
|
|
385
|
+
if (this.options.useCookie && this.currentCookie) {
|
|
386
|
+
console.log("[AutoProxyCookie] Applying cookie header...");
|
|
294
387
|
applyDevCookieHeader(proxyReq, this.currentCookie);
|
|
388
|
+
console.log("[AutoProxyCookie] Cookie header applied successfully");
|
|
389
|
+
} else if (!this.options.useCookie) {
|
|
390
|
+
console.log("[AutoProxyCookie] useCookie is false, skipping cookie injection");
|
|
391
|
+
} else {
|
|
392
|
+
console.log("[AutoProxyCookie] No cookie to apply - currentCookie is empty!");
|
|
295
393
|
}
|
|
296
394
|
this.log("debug", "[AutoProxyCookie] Proxy Request:", req.method, req.url);
|
|
297
395
|
if (this.options.hooks.onProxyReq) {
|
|
@@ -301,7 +399,14 @@ var AutoProxyCookie = class {
|
|
|
301
399
|
this.log("error", "[AutoProxyCookie] onProxyReq hook error:", err.message);
|
|
302
400
|
}
|
|
303
401
|
}
|
|
402
|
+
console.log("[AutoProxyCookie] === handleOnProxyReq END ===");
|
|
304
403
|
};
|
|
404
|
+
/**
|
|
405
|
+
* 代理响应处理函数
|
|
406
|
+
* @param proxyRes - 代理响应对象
|
|
407
|
+
* @param req - 原始请求对象
|
|
408
|
+
* @param res - 响应对象
|
|
409
|
+
*/
|
|
305
410
|
this.handleOnProxyRes = (proxyRes, req, res) => {
|
|
306
411
|
const allowedHeaders = [
|
|
307
412
|
"Content-Type",
|
|
@@ -325,6 +430,12 @@ var AutoProxyCookie = class {
|
|
|
325
430
|
}
|
|
326
431
|
}
|
|
327
432
|
};
|
|
433
|
+
/**
|
|
434
|
+
* HTTP 代理错误处理函数
|
|
435
|
+
* @param err - 错误对象
|
|
436
|
+
* @param req - 请求对象
|
|
437
|
+
* @param res - 响应对象或 Socket
|
|
438
|
+
*/
|
|
328
439
|
this.handleOnError = (err, req, res) => {
|
|
329
440
|
this.log("error", "[AutoProxyCookie] Proxy Error:", err.message);
|
|
330
441
|
this.log("error", "[AutoProxyCookie] URL:", req.url);
|
|
@@ -344,6 +455,12 @@ var AutoProxyCookie = class {
|
|
|
344
455
|
}
|
|
345
456
|
}
|
|
346
457
|
};
|
|
458
|
+
/**
|
|
459
|
+
* WebSocket 代理错误处理函数
|
|
460
|
+
* @param err - 错误对象
|
|
461
|
+
* @param req - 请求对象
|
|
462
|
+
* @param socket - WebSocket 连接的 Socket
|
|
463
|
+
*/
|
|
347
464
|
this.handleOnWsError = (err, req, socket) => {
|
|
348
465
|
this.log("error", "[AutoProxyCookie] WebSocket Proxy Error:", err.message);
|
|
349
466
|
this.log("error", "[AutoProxyCookie] WebSocket URL:", req.url);
|
|
@@ -380,6 +497,7 @@ var AutoProxyCookie = class {
|
|
|
380
497
|
autoRestart: false,
|
|
381
498
|
restartMarkerFile: ".cookie-restart-marker",
|
|
382
499
|
proxyMap: {},
|
|
500
|
+
proxyPaths: [],
|
|
383
501
|
ignorePaths: [],
|
|
384
502
|
ws: true,
|
|
385
503
|
changeOrigin: true,
|
|
@@ -391,29 +509,63 @@ var AutoProxyCookie = class {
|
|
|
391
509
|
cookieDomainRewrite: "*",
|
|
392
510
|
cookiePathRewrite: false,
|
|
393
511
|
headers: {},
|
|
512
|
+
useCookie: true,
|
|
394
513
|
...mergedOptions
|
|
395
514
|
};
|
|
396
|
-
this.cookieReader = new CookieReader({ cookieFile: options.cookieFile });
|
|
515
|
+
this.cookieReader = new CookieReader({ cookieFile: options.cookieFile }, options.debug ?? false);
|
|
397
516
|
}
|
|
517
|
+
/**
|
|
518
|
+
* 根据请求路径获取代理目标 URL
|
|
519
|
+
* @param req - HTTP 请求对象
|
|
520
|
+
* @returns 代理目标 URL
|
|
521
|
+
*/
|
|
398
522
|
getProxyUrl(req) {
|
|
399
|
-
const
|
|
523
|
+
const fullUrl = req.url || "/";
|
|
524
|
+
const pathname = new URL(fullUrl, "http://localhost").pathname;
|
|
400
525
|
const proxyMap = this.options.proxyMap || {};
|
|
526
|
+
const proxyPaths = this.options.proxyPaths || [];
|
|
527
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - Request path:", pathname);
|
|
528
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - Available proxyMap paths:", Object.keys(proxyMap));
|
|
529
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - Available proxyPaths:", proxyPaths);
|
|
401
530
|
for (const [prefix, target] of Object.entries(proxyMap)) {
|
|
402
|
-
|
|
531
|
+
const matches = pathname.startsWith(prefix);
|
|
532
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - Checking proxyMap:", prefix, "- matches:", matches);
|
|
533
|
+
if (matches) {
|
|
534
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - Matched proxyMap:", prefix, "->", target);
|
|
403
535
|
return target;
|
|
404
536
|
}
|
|
405
537
|
}
|
|
538
|
+
for (const prefix of proxyPaths) {
|
|
539
|
+
const matches = pathname.startsWith(prefix);
|
|
540
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - Checking proxyPaths:", prefix, "- matches:", matches);
|
|
541
|
+
if (matches) {
|
|
542
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - Matched proxyPaths:", prefix, "->", this.options.target);
|
|
543
|
+
return this.options.target;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - No match found, using default target:", this.options.target);
|
|
406
547
|
return this.options.target;
|
|
407
548
|
}
|
|
549
|
+
/**
|
|
550
|
+
* 判断路径是否应该被忽略(不代理)
|
|
551
|
+
* @param pathname - 请求路径
|
|
552
|
+
* @returns 是否忽略
|
|
553
|
+
*/
|
|
408
554
|
isIgnoredPath(pathname) {
|
|
409
555
|
const ignorePaths = this.options.ignorePaths || [];
|
|
410
556
|
return ignorePaths.some(
|
|
411
557
|
(ignored) => pathname.startsWith(ignored)
|
|
412
558
|
);
|
|
413
559
|
}
|
|
560
|
+
/**
|
|
561
|
+
* 日志输出函数
|
|
562
|
+
* @param level - 日志级别
|
|
563
|
+
* @param args - 日志参数
|
|
564
|
+
*/
|
|
414
565
|
log(level, ...args) {
|
|
415
566
|
const levels = { debug: 0, info: 1, warn: 2, error: 3 };
|
|
416
|
-
const
|
|
567
|
+
const effectiveLogLevel = this.options.debug ? "debug" : this.options.logLevel || "info";
|
|
568
|
+
const currentLevel = levels[effectiveLogLevel];
|
|
417
569
|
const msgLevel = levels[level];
|
|
418
570
|
if (msgLevel >= currentLevel) {
|
|
419
571
|
if (level === "error") {
|
|
@@ -425,6 +577,11 @@ var AutoProxyCookie = class {
|
|
|
425
577
|
}
|
|
426
578
|
}
|
|
427
579
|
}
|
|
580
|
+
/**
|
|
581
|
+
* 创建代理服务器配置选项
|
|
582
|
+
* @param target - 代理目标地址
|
|
583
|
+
* @returns 代理服务器配置
|
|
584
|
+
*/
|
|
428
585
|
createProxyOptions(target) {
|
|
429
586
|
const {
|
|
430
587
|
ws,
|
|
@@ -453,6 +610,10 @@ var AutoProxyCookie = class {
|
|
|
453
610
|
ignorePath: false
|
|
454
611
|
};
|
|
455
612
|
}
|
|
613
|
+
/**
|
|
614
|
+
* 初始化代理中间件
|
|
615
|
+
* @param server - Vite 开发服务器实例
|
|
616
|
+
*/
|
|
456
617
|
async setup(server) {
|
|
457
618
|
this.server = server;
|
|
458
619
|
this.currentCookie = this.cookieReader.readCookie();
|
|
@@ -480,25 +641,47 @@ var AutoProxyCookie = class {
|
|
|
480
641
|
this.log("warn", "[AutoProxyCookie] http-proxy create failed, using basic mode:", err.message);
|
|
481
642
|
}
|
|
482
643
|
server.middlewares.use((req, res, next) => {
|
|
483
|
-
const
|
|
644
|
+
const fullUrl = req.url || "/";
|
|
645
|
+
const pathname = new URL(fullUrl, "http://localhost").pathname;
|
|
646
|
+
console.log("[AutoProxyCookie] === Incoming Request ===");
|
|
647
|
+
console.log("[AutoProxyCookie] Method:", req.method);
|
|
648
|
+
console.log("[AutoProxyCookie] Full URL:", fullUrl);
|
|
649
|
+
console.log("[AutoProxyCookie] Pathname:", pathname);
|
|
650
|
+
console.log("[AutoProxyCookie] useCookie:", this.options.useCookie);
|
|
651
|
+
console.log("[AutoProxyCookie] Headers:", JSON.stringify(req.headers, null, 2));
|
|
484
652
|
if (this.isIgnoredPath(pathname)) {
|
|
653
|
+
console.log("[AutoProxyCookie] Path ignored, passing to next middleware");
|
|
485
654
|
next();
|
|
486
655
|
return;
|
|
487
656
|
}
|
|
488
|
-
|
|
657
|
+
if (this.options.useCookie) {
|
|
658
|
+
this.currentCookie = this.cookieReader.readCookie();
|
|
659
|
+
console.log("[AutoProxyCookie] Current cookie:", this.currentCookie ? `(length: ${this.currentCookie.length})` : "(empty)");
|
|
660
|
+
if (this.currentCookie) {
|
|
661
|
+
console.log("[AutoProxyCookie] Cookie preview:", this.currentCookie);
|
|
662
|
+
}
|
|
663
|
+
} else {
|
|
664
|
+
console.log("[AutoProxyCookie] useCookie is false, skipping cookie reading");
|
|
665
|
+
}
|
|
666
|
+
const proxyMapKeys = Object.keys(this.options.proxyMap || {});
|
|
667
|
+
const proxyPaths = this.options.proxyPaths || [];
|
|
668
|
+
const allProxyPrefixes = [...proxyMapKeys, ...proxyPaths];
|
|
669
|
+
const matched = allProxyPrefixes.some((prefix) => pathname.startsWith(prefix));
|
|
670
|
+
console.log("[AutoProxyCookie] Path matches proxy rules:", matched);
|
|
489
671
|
if (this.proxyServer) {
|
|
490
672
|
const target = this.getProxyUrl(req);
|
|
491
|
-
|
|
492
|
-
this.log("info", `[AutoProxyCookie] ${pathname} -> ${target}`);
|
|
493
|
-
}
|
|
673
|
+
console.log(`[AutoProxyCookie] Proxying ${req.method} ${pathname} -> ${target}`);
|
|
494
674
|
const proxyOptions = this.createProxyOptions(target);
|
|
495
675
|
try {
|
|
676
|
+
console.log("[AutoProxyCookie] Calling proxyServer.web...");
|
|
496
677
|
this.proxyServer.web(req, res, proxyOptions);
|
|
678
|
+
console.log("[AutoProxyCookie] proxyServer.web called successfully");
|
|
497
679
|
} catch (err) {
|
|
498
|
-
|
|
680
|
+
console.error("[AutoProxyCookie] Proxy web error:", err.message);
|
|
499
681
|
next(err);
|
|
500
682
|
}
|
|
501
683
|
} else {
|
|
684
|
+
console.log("[AutoProxyCookie] No proxy server, passing to next middleware");
|
|
502
685
|
next();
|
|
503
686
|
}
|
|
504
687
|
});
|
|
@@ -530,6 +713,9 @@ var AutoProxyCookie = class {
|
|
|
530
713
|
this.log("info", "[AutoProxyCookie] WebSocket support enabled");
|
|
531
714
|
}
|
|
532
715
|
}
|
|
716
|
+
/**
|
|
717
|
+
* 启动 Cookie 文件监听
|
|
718
|
+
*/
|
|
533
719
|
startFileWatch() {
|
|
534
720
|
let shouldWatch;
|
|
535
721
|
if (this.options.isDev !== void 0) {
|
|
@@ -555,6 +741,9 @@ var AutoProxyCookie = class {
|
|
|
555
741
|
console.log("[AutoProxyCookie] File watch disabled");
|
|
556
742
|
}
|
|
557
743
|
}
|
|
744
|
+
/**
|
|
745
|
+
* 停止代理服务
|
|
746
|
+
*/
|
|
558
747
|
stop() {
|
|
559
748
|
if (this.watcher) {
|
|
560
749
|
this.watcher.stop();
|
|
@@ -566,6 +755,10 @@ var AutoProxyCookie = class {
|
|
|
566
755
|
}
|
|
567
756
|
this.log("info", "[AutoProxyCookie] Stopped");
|
|
568
757
|
}
|
|
758
|
+
/**
|
|
759
|
+
* 获取当前 Cookie 值
|
|
760
|
+
* @returns 当前 Cookie 字符串
|
|
761
|
+
*/
|
|
569
762
|
getCurrentCookie() {
|
|
570
763
|
return this.currentCookie;
|
|
571
764
|
}
|
|
@@ -574,92 +767,90 @@ function createAutoProxyCookie(options) {
|
|
|
574
767
|
return new AutoProxyCookie(options);
|
|
575
768
|
}
|
|
576
769
|
|
|
577
|
-
// src/proxy/vite-plugin.ts
|
|
578
|
-
function
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
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();
|
|
770
|
+
// src/proxy/vite-middleware-plugin.ts
|
|
771
|
+
function isIgnoredPath(pathname, ignorePaths) {
|
|
772
|
+
return ignorePaths.some((ignored) => pathname.startsWith(ignored));
|
|
773
|
+
}
|
|
774
|
+
function shouldProxy(pathname, proxyPrefixes) {
|
|
775
|
+
return proxyPrefixes.some((prefix) => pathname.startsWith(prefix));
|
|
776
|
+
}
|
|
777
|
+
function getProxyTarget(pathname, proxyMap, defaultTarget) {
|
|
778
|
+
for (const [prefix, target] of Object.entries(proxyMap)) {
|
|
779
|
+
if (pathname.startsWith(prefix)) {
|
|
780
|
+
return target;
|
|
606
781
|
}
|
|
607
|
-
}
|
|
782
|
+
}
|
|
783
|
+
return defaultTarget;
|
|
608
784
|
}
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
785
|
+
function viteMiddlewareProxy(options) {
|
|
786
|
+
const {
|
|
787
|
+
cookieFile,
|
|
788
|
+
target,
|
|
789
|
+
debug = false,
|
|
790
|
+
useCookie = true,
|
|
791
|
+
proxyMap = {},
|
|
792
|
+
proxyPaths = [],
|
|
793
|
+
ignorePaths = []
|
|
794
|
+
} = options;
|
|
795
|
+
const cookieReader = new CookieReader({ cookieFile }, debug);
|
|
614
796
|
let currentCookie = "";
|
|
615
|
-
|
|
797
|
+
if (useCookie) {
|
|
798
|
+
currentCookie = cookieReader.readCookie();
|
|
799
|
+
}
|
|
800
|
+
const allProxyPrefixes = [
|
|
801
|
+
...Object.keys(proxyMap),
|
|
802
|
+
...proxyPaths
|
|
803
|
+
];
|
|
616
804
|
return {
|
|
617
|
-
name: "vite-
|
|
805
|
+
name: "vite-middleware-proxy",
|
|
618
806
|
apply: "serve",
|
|
619
807
|
configureServer(server) {
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
808
|
+
const httpProxy2 = require("http-proxy");
|
|
809
|
+
const proxyServer = httpProxy2.createProxyServer({});
|
|
810
|
+
if (useCookie && debug) {
|
|
811
|
+
console.log("[ViteMiddlewareProxy] Watching cookie file:", cookieFile);
|
|
623
812
|
}
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
if (
|
|
628
|
-
|
|
629
|
-
req.headers["cookie"] = currentCookie;
|
|
813
|
+
server.middlewares.use((req, res, next) => {
|
|
814
|
+
const pathname = new URL(req.url || "/", "http://localhost").pathname;
|
|
815
|
+
if (isIgnoredPath(pathname, ignorePaths)) {
|
|
816
|
+
if (debug) {
|
|
817
|
+
console.log("[ViteMiddlewareProxy] Ignoring:", pathname);
|
|
630
818
|
}
|
|
631
819
|
next();
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
822
|
+
if (!shouldProxy(pathname, allProxyPrefixes)) {
|
|
823
|
+
next();
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
const proxyTarget = getProxyTarget(pathname, proxyMap, target);
|
|
639
827
|
if (debug) {
|
|
640
|
-
console.log(
|
|
828
|
+
console.log("[ViteMiddlewareProxy] Proxying:", req.method, pathname, "->", proxyTarget);
|
|
641
829
|
}
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
console.log("[
|
|
652
|
-
},
|
|
653
|
-
(error) => {
|
|
654
|
-
console.error("[vite-dev-proxy-cookie] Watch error:", error.message);
|
|
830
|
+
if (useCookie) {
|
|
831
|
+
currentCookie = cookieReader.readCookie();
|
|
832
|
+
if (currentCookie) {
|
|
833
|
+
if (debug) {
|
|
834
|
+
console.log("[ViteMiddlewareProxy] Injecting cookie:", `(length: ${currentCookie.length})`);
|
|
835
|
+
}
|
|
836
|
+
req.headers["cookie"] = currentCookie;
|
|
837
|
+
req.headers["Cookie"] = currentCookie;
|
|
838
|
+
} else if (debug) {
|
|
839
|
+
console.log("[ViteMiddlewareProxy] Cookie file is empty");
|
|
655
840
|
}
|
|
656
|
-
)
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
841
|
+
} else if (debug) {
|
|
842
|
+
console.log("[ViteMiddlewareProxy] useCookie is false, skipping cookie injection");
|
|
843
|
+
}
|
|
844
|
+
proxyServer.web(req, res, {
|
|
845
|
+
target: proxyTarget,
|
|
846
|
+
changeOrigin: true,
|
|
847
|
+
secure: false,
|
|
848
|
+
ignorePath: false
|
|
849
|
+
});
|
|
850
|
+
});
|
|
851
|
+
proxyServer.on("error", (err, req) => {
|
|
852
|
+
console.error("[ViteMiddlewareProxy] Proxy error:", err.message, "for", req.url);
|
|
853
|
+
});
|
|
663
854
|
}
|
|
664
855
|
};
|
|
665
856
|
}
|
|
@@ -670,6 +861,7 @@ function createVueProxyConfig(target, options = {}) {
|
|
|
670
861
|
const {
|
|
671
862
|
getCookie,
|
|
672
863
|
debug = false,
|
|
864
|
+
useCookie = true,
|
|
673
865
|
headers = {},
|
|
674
866
|
ws = false,
|
|
675
867
|
changeOrigin = true,
|
|
@@ -683,13 +875,17 @@ function createVueProxyConfig(target, options = {}) {
|
|
|
683
875
|
secure,
|
|
684
876
|
headers,
|
|
685
877
|
onProxyReq: (proxyReq, req) => {
|
|
686
|
-
const
|
|
687
|
-
if (
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
878
|
+
const reqPath = req.url || "/";
|
|
879
|
+
if (useCookie) {
|
|
880
|
+
const cookie = getCookie ? getCookie() : "";
|
|
881
|
+
if (cookie) {
|
|
882
|
+
applyDevCookieHeader(proxyReq, cookie);
|
|
883
|
+
}
|
|
884
|
+
if (debug) {
|
|
885
|
+
console.log("[Proxy Request]", reqPath, req.method, cookie ? "(with cookie)" : "(no cookie)");
|
|
886
|
+
}
|
|
887
|
+
} else if (debug) {
|
|
888
|
+
console.log("[Proxy Request]", reqPath, req.method, "(useCookie is false, skipping cookie injection)");
|
|
693
889
|
}
|
|
694
890
|
},
|
|
695
891
|
onError: customOnError || ((err) => {
|
|
@@ -701,11 +897,11 @@ function createVueProxyConfig(target, options = {}) {
|
|
|
701
897
|
function createFileCookieGetter(cookieFile, options = {}) {
|
|
702
898
|
const {
|
|
703
899
|
watch = "auto",
|
|
704
|
-
debug =
|
|
900
|
+
debug = true,
|
|
705
901
|
productionEnvs = [],
|
|
706
902
|
isDev
|
|
707
903
|
} = options;
|
|
708
|
-
const reader = new CookieReader({ cookieFile: path4.resolve(cookieFile) });
|
|
904
|
+
const reader = new CookieReader({ cookieFile: path4.resolve(cookieFile) }, debug);
|
|
709
905
|
let shouldWatch;
|
|
710
906
|
if (isDev !== void 0) {
|
|
711
907
|
shouldWatch = isDev;
|
|
@@ -733,11 +929,11 @@ function createFileCookieGetter(cookieFile, options = {}) {
|
|
|
733
929
|
return () => reader.readCookie();
|
|
734
930
|
}
|
|
735
931
|
function createAutoProxyConfig(options) {
|
|
736
|
-
const { target, ignorePaths = [], includePaths = [], additionalProxies = {}, getCookie, debug, headers } = options;
|
|
932
|
+
const { target, ignorePaths = [], includePaths = [], additionalProxies = {}, getCookie, debug, headers, useCookie = true } = options;
|
|
737
933
|
const result = {};
|
|
738
934
|
if (includePaths.length > 0) {
|
|
739
935
|
for (const proxyPath of includePaths) {
|
|
740
|
-
result[proxyPath] = createVueProxyConfig(target, { getCookie, debug, headers });
|
|
936
|
+
result[proxyPath] = createVueProxyConfig(target, { getCookie, debug, headers, useCookie });
|
|
741
937
|
}
|
|
742
938
|
} else {
|
|
743
939
|
const defaultProxy = {
|
|
@@ -751,12 +947,16 @@ function createAutoProxyConfig(options) {
|
|
|
751
947
|
if (ignorePaths.some((p) => reqPath.startsWith(p))) {
|
|
752
948
|
return;
|
|
753
949
|
}
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
950
|
+
if (useCookie) {
|
|
951
|
+
const cookie = getCookie ? getCookie() : "";
|
|
952
|
+
if (cookie) {
|
|
953
|
+
applyDevCookieHeader(proxyReq, cookie);
|
|
954
|
+
}
|
|
955
|
+
if (debug) {
|
|
956
|
+
console.log("[Proxy Request]", reqPath, req.method, cookie ? "(with cookie)" : "(no cookie)");
|
|
957
|
+
}
|
|
958
|
+
} else if (debug) {
|
|
959
|
+
console.log("[Proxy Request]", reqPath, req.method, "(useCookie is false, skipping cookie injection)");
|
|
760
960
|
}
|
|
761
961
|
},
|
|
762
962
|
onError: (err) => {
|
|
@@ -766,65 +966,10 @@ function createAutoProxyConfig(options) {
|
|
|
766
966
|
result["/"] = defaultProxy;
|
|
767
967
|
}
|
|
768
968
|
for (const [proxyPath, proxyTarget] of Object.entries(additionalProxies)) {
|
|
769
|
-
result[proxyPath] = createVueProxyConfig(proxyTarget, { getCookie, debug, headers });
|
|
969
|
+
result[proxyPath] = createVueProxyConfig(proxyTarget, { getCookie, debug, headers, useCookie });
|
|
770
970
|
}
|
|
771
971
|
return result;
|
|
772
972
|
}
|
|
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
973
|
// Annotate the CommonJS export names for ESM import in node:
|
|
829
974
|
0 && (module.exports = {
|
|
830
975
|
AutoProxyCookie,
|
|
@@ -833,15 +978,11 @@ function getViteMajorVersion() {
|
|
|
833
978
|
createAutoProxyConfig,
|
|
834
979
|
createAutoProxyCookie,
|
|
835
980
|
createCookieGetter,
|
|
836
|
-
createDevProxyCookie,
|
|
837
981
|
createFileCookieGetter,
|
|
838
982
|
createVueProxyConfig,
|
|
839
983
|
detectProductionEnvironment,
|
|
840
|
-
getViteMajorVersion,
|
|
841
|
-
getViteVersion,
|
|
842
984
|
isProductionValue,
|
|
843
985
|
shouldEnableWatch,
|
|
844
|
-
|
|
845
|
-
viteDevProxyCookie,
|
|
986
|
+
viteMiddlewareProxy,
|
|
846
987
|
watchCookieFile
|
|
847
988
|
});
|