@gylautorun/dev-proxy-cookie 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +281 -68
- package/dist/index.d.ts +281 -68
- package/dist/index.js +269 -156
- package/dist/index.min.js +4 -4
- package/dist/index.min.mjs +4 -4
- package/dist/index.mjs +268 -151
- package/package.json +1 -1
- package/src/index.ts +13 -0
- package/src/proxy/apply-dev-cookie-header.ts +35 -5
- package/src/proxy/core.ts +179 -10
- package/src/proxy/index.ts +8 -3
- package/src/proxy/vite-middleware-plugin.ts +158 -0
- package/src/proxy/vue-proxy-config.ts +64 -2
- package/src/utils/cookie-reader.ts +74 -3
- package/src/utils/cookie-watcher.ts +50 -1
- package/src/utils/env-detector.ts +9 -2
- package/src/utils/index.ts +7 -0
- package/src/proxy/vite-adapter.ts +0 -98
- package/src/proxy/vite-cookie-plugin.ts +0 -94
- package/src/proxy/vite-plugin.ts +0 -66
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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,23 @@ 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) => {
|
|
340
|
+
console.log("[AutoProxyCookie] === handleOnProxyReq START ===");
|
|
341
|
+
console.log("[AutoProxyCookie] Request URL:", req.method, req.url);
|
|
342
|
+
console.log("[AutoProxyCookie] Current cookie:", this.currentCookie ? `(length: ${this.currentCookie.length})` : "(empty)");
|
|
248
343
|
if (this.currentCookie) {
|
|
344
|
+
console.log("[AutoProxyCookie] Applying cookie header...");
|
|
249
345
|
applyDevCookieHeader(proxyReq, this.currentCookie);
|
|
346
|
+
console.log("[AutoProxyCookie] Cookie header applied successfully");
|
|
347
|
+
} else {
|
|
348
|
+
console.log("[AutoProxyCookie] No cookie to apply - currentCookie is empty!");
|
|
250
349
|
}
|
|
251
350
|
this.log("debug", "[AutoProxyCookie] Proxy Request:", req.method, req.url);
|
|
252
351
|
if (this.options.hooks.onProxyReq) {
|
|
@@ -256,7 +355,14 @@ var AutoProxyCookie = class {
|
|
|
256
355
|
this.log("error", "[AutoProxyCookie] onProxyReq hook error:", err.message);
|
|
257
356
|
}
|
|
258
357
|
}
|
|
358
|
+
console.log("[AutoProxyCookie] === handleOnProxyReq END ===");
|
|
259
359
|
};
|
|
360
|
+
/**
|
|
361
|
+
* 代理响应处理函数
|
|
362
|
+
* @param proxyRes - 代理响应对象
|
|
363
|
+
* @param req - 原始请求对象
|
|
364
|
+
* @param res - 响应对象
|
|
365
|
+
*/
|
|
260
366
|
this.handleOnProxyRes = (proxyRes, req, res) => {
|
|
261
367
|
const allowedHeaders = [
|
|
262
368
|
"Content-Type",
|
|
@@ -280,6 +386,12 @@ var AutoProxyCookie = class {
|
|
|
280
386
|
}
|
|
281
387
|
}
|
|
282
388
|
};
|
|
389
|
+
/**
|
|
390
|
+
* HTTP 代理错误处理函数
|
|
391
|
+
* @param err - 错误对象
|
|
392
|
+
* @param req - 请求对象
|
|
393
|
+
* @param res - 响应对象或 Socket
|
|
394
|
+
*/
|
|
283
395
|
this.handleOnError = (err, req, res) => {
|
|
284
396
|
this.log("error", "[AutoProxyCookie] Proxy Error:", err.message);
|
|
285
397
|
this.log("error", "[AutoProxyCookie] URL:", req.url);
|
|
@@ -299,6 +411,12 @@ var AutoProxyCookie = class {
|
|
|
299
411
|
}
|
|
300
412
|
}
|
|
301
413
|
};
|
|
414
|
+
/**
|
|
415
|
+
* WebSocket 代理错误处理函数
|
|
416
|
+
* @param err - 错误对象
|
|
417
|
+
* @param req - 请求对象
|
|
418
|
+
* @param socket - WebSocket 连接的 Socket
|
|
419
|
+
*/
|
|
302
420
|
this.handleOnWsError = (err, req, socket) => {
|
|
303
421
|
this.log("error", "[AutoProxyCookie] WebSocket Proxy Error:", err.message);
|
|
304
422
|
this.log("error", "[AutoProxyCookie] WebSocket URL:", req.url);
|
|
@@ -335,6 +453,7 @@ var AutoProxyCookie = class {
|
|
|
335
453
|
autoRestart: false,
|
|
336
454
|
restartMarkerFile: ".cookie-restart-marker",
|
|
337
455
|
proxyMap: {},
|
|
456
|
+
proxyPaths: [],
|
|
338
457
|
ignorePaths: [],
|
|
339
458
|
ws: true,
|
|
340
459
|
changeOrigin: true,
|
|
@@ -348,27 +467,60 @@ var AutoProxyCookie = class {
|
|
|
348
467
|
headers: {},
|
|
349
468
|
...mergedOptions
|
|
350
469
|
};
|
|
351
|
-
this.cookieReader = new CookieReader({ cookieFile: options.cookieFile });
|
|
470
|
+
this.cookieReader = new CookieReader({ cookieFile: options.cookieFile }, options.debug ?? false);
|
|
352
471
|
}
|
|
472
|
+
/**
|
|
473
|
+
* 根据请求路径获取代理目标 URL
|
|
474
|
+
* @param req - HTTP 请求对象
|
|
475
|
+
* @returns 代理目标 URL
|
|
476
|
+
*/
|
|
353
477
|
getProxyUrl(req) {
|
|
354
|
-
const
|
|
478
|
+
const fullUrl = req.url || "/";
|
|
479
|
+
const pathname = new URL(fullUrl, "http://localhost").pathname;
|
|
355
480
|
const proxyMap = this.options.proxyMap || {};
|
|
481
|
+
const proxyPaths = this.options.proxyPaths || [];
|
|
482
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - Request path:", pathname);
|
|
483
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - Available proxyMap paths:", Object.keys(proxyMap));
|
|
484
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - Available proxyPaths:", proxyPaths);
|
|
356
485
|
for (const [prefix, target] of Object.entries(proxyMap)) {
|
|
357
|
-
|
|
486
|
+
const matches = pathname.startsWith(prefix);
|
|
487
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - Checking proxyMap:", prefix, "- matches:", matches);
|
|
488
|
+
if (matches) {
|
|
489
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - Matched proxyMap:", prefix, "->", target);
|
|
358
490
|
return target;
|
|
359
491
|
}
|
|
360
492
|
}
|
|
493
|
+
for (const prefix of proxyPaths) {
|
|
494
|
+
const matches = pathname.startsWith(prefix);
|
|
495
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - Checking proxyPaths:", prefix, "- matches:", matches);
|
|
496
|
+
if (matches) {
|
|
497
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - Matched proxyPaths:", prefix, "->", this.options.target);
|
|
498
|
+
return this.options.target;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
this.log("debug", "[AutoProxyCookie] getProxyUrl - No match found, using default target:", this.options.target);
|
|
361
502
|
return this.options.target;
|
|
362
503
|
}
|
|
504
|
+
/**
|
|
505
|
+
* 判断路径是否应该被忽略(不代理)
|
|
506
|
+
* @param pathname - 请求路径
|
|
507
|
+
* @returns 是否忽略
|
|
508
|
+
*/
|
|
363
509
|
isIgnoredPath(pathname) {
|
|
364
510
|
const ignorePaths = this.options.ignorePaths || [];
|
|
365
511
|
return ignorePaths.some(
|
|
366
512
|
(ignored) => pathname.startsWith(ignored)
|
|
367
513
|
);
|
|
368
514
|
}
|
|
515
|
+
/**
|
|
516
|
+
* 日志输出函数
|
|
517
|
+
* @param level - 日志级别
|
|
518
|
+
* @param args - 日志参数
|
|
519
|
+
*/
|
|
369
520
|
log(level, ...args) {
|
|
370
521
|
const levels = { debug: 0, info: 1, warn: 2, error: 3 };
|
|
371
|
-
const
|
|
522
|
+
const effectiveLogLevel = this.options.debug ? "debug" : this.options.logLevel || "info";
|
|
523
|
+
const currentLevel = levels[effectiveLogLevel];
|
|
372
524
|
const msgLevel = levels[level];
|
|
373
525
|
if (msgLevel >= currentLevel) {
|
|
374
526
|
if (level === "error") {
|
|
@@ -380,6 +532,11 @@ var AutoProxyCookie = class {
|
|
|
380
532
|
}
|
|
381
533
|
}
|
|
382
534
|
}
|
|
535
|
+
/**
|
|
536
|
+
* 创建代理服务器配置选项
|
|
537
|
+
* @param target - 代理目标地址
|
|
538
|
+
* @returns 代理服务器配置
|
|
539
|
+
*/
|
|
383
540
|
createProxyOptions(target) {
|
|
384
541
|
const {
|
|
385
542
|
ws,
|
|
@@ -408,6 +565,10 @@ var AutoProxyCookie = class {
|
|
|
408
565
|
ignorePath: false
|
|
409
566
|
};
|
|
410
567
|
}
|
|
568
|
+
/**
|
|
569
|
+
* 初始化代理中间件
|
|
570
|
+
* @param server - Vite 开发服务器实例
|
|
571
|
+
*/
|
|
411
572
|
async setup(server) {
|
|
412
573
|
this.server = server;
|
|
413
574
|
this.currentCookie = this.cookieReader.readCookie();
|
|
@@ -435,25 +596,42 @@ var AutoProxyCookie = class {
|
|
|
435
596
|
this.log("warn", "[AutoProxyCookie] http-proxy create failed, using basic mode:", err.message);
|
|
436
597
|
}
|
|
437
598
|
server.middlewares.use((req, res, next) => {
|
|
438
|
-
const
|
|
599
|
+
const fullUrl = req.url || "/";
|
|
600
|
+
const pathname = new URL(fullUrl, "http://localhost").pathname;
|
|
601
|
+
console.log("[AutoProxyCookie] === Incoming Request ===");
|
|
602
|
+
console.log("[AutoProxyCookie] Method:", req.method);
|
|
603
|
+
console.log("[AutoProxyCookie] Full URL:", fullUrl);
|
|
604
|
+
console.log("[AutoProxyCookie] Pathname:", pathname);
|
|
605
|
+
console.log("[AutoProxyCookie] Headers:", JSON.stringify(req.headers, null, 2));
|
|
439
606
|
if (this.isIgnoredPath(pathname)) {
|
|
607
|
+
console.log("[AutoProxyCookie] Path ignored, passing to next middleware");
|
|
440
608
|
next();
|
|
441
609
|
return;
|
|
442
610
|
}
|
|
443
611
|
this.currentCookie = this.cookieReader.readCookie();
|
|
612
|
+
console.log("[AutoProxyCookie] Current cookie:", this.currentCookie ? `(length: ${this.currentCookie.length})` : "(empty)");
|
|
613
|
+
if (this.currentCookie) {
|
|
614
|
+
console.log("[AutoProxyCookie] Cookie preview:", this.currentCookie);
|
|
615
|
+
}
|
|
616
|
+
const proxyMapKeys = Object.keys(this.options.proxyMap || {});
|
|
617
|
+
const proxyPaths = this.options.proxyPaths || [];
|
|
618
|
+
const allProxyPrefixes = [...proxyMapKeys, ...proxyPaths];
|
|
619
|
+
const matched = allProxyPrefixes.some((prefix) => pathname.startsWith(prefix));
|
|
620
|
+
console.log("[AutoProxyCookie] Path matches proxy rules:", matched);
|
|
444
621
|
if (this.proxyServer) {
|
|
445
622
|
const target = this.getProxyUrl(req);
|
|
446
|
-
|
|
447
|
-
this.log("info", `[AutoProxyCookie] ${pathname} -> ${target}`);
|
|
448
|
-
}
|
|
623
|
+
console.log(`[AutoProxyCookie] Proxying ${req.method} ${pathname} -> ${target}`);
|
|
449
624
|
const proxyOptions = this.createProxyOptions(target);
|
|
450
625
|
try {
|
|
626
|
+
console.log("[AutoProxyCookie] Calling proxyServer.web...");
|
|
451
627
|
this.proxyServer.web(req, res, proxyOptions);
|
|
628
|
+
console.log("[AutoProxyCookie] proxyServer.web called successfully");
|
|
452
629
|
} catch (err) {
|
|
453
|
-
|
|
630
|
+
console.error("[AutoProxyCookie] Proxy web error:", err.message);
|
|
454
631
|
next(err);
|
|
455
632
|
}
|
|
456
633
|
} else {
|
|
634
|
+
console.log("[AutoProxyCookie] No proxy server, passing to next middleware");
|
|
457
635
|
next();
|
|
458
636
|
}
|
|
459
637
|
});
|
|
@@ -485,6 +663,9 @@ var AutoProxyCookie = class {
|
|
|
485
663
|
this.log("info", "[AutoProxyCookie] WebSocket support enabled");
|
|
486
664
|
}
|
|
487
665
|
}
|
|
666
|
+
/**
|
|
667
|
+
* 启动 Cookie 文件监听
|
|
668
|
+
*/
|
|
488
669
|
startFileWatch() {
|
|
489
670
|
let shouldWatch;
|
|
490
671
|
if (this.options.isDev !== void 0) {
|
|
@@ -510,6 +691,9 @@ var AutoProxyCookie = class {
|
|
|
510
691
|
console.log("[AutoProxyCookie] File watch disabled");
|
|
511
692
|
}
|
|
512
693
|
}
|
|
694
|
+
/**
|
|
695
|
+
* 停止代理服务
|
|
696
|
+
*/
|
|
513
697
|
stop() {
|
|
514
698
|
if (this.watcher) {
|
|
515
699
|
this.watcher.stop();
|
|
@@ -521,6 +705,10 @@ var AutoProxyCookie = class {
|
|
|
521
705
|
}
|
|
522
706
|
this.log("info", "[AutoProxyCookie] Stopped");
|
|
523
707
|
}
|
|
708
|
+
/**
|
|
709
|
+
* 获取当前 Cookie 值
|
|
710
|
+
* @returns 当前 Cookie 字符串
|
|
711
|
+
*/
|
|
524
712
|
getCurrentCookie() {
|
|
525
713
|
return this.currentCookie;
|
|
526
714
|
}
|
|
@@ -529,92 +717,80 @@ function createAutoProxyCookie(options) {
|
|
|
529
717
|
return new AutoProxyCookie(options);
|
|
530
718
|
}
|
|
531
719
|
|
|
532
|
-
// src/proxy/vite-plugin.ts
|
|
533
|
-
function
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
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();
|
|
720
|
+
// src/proxy/vite-middleware-plugin.ts
|
|
721
|
+
function isIgnoredPath(pathname, ignorePaths) {
|
|
722
|
+
return ignorePaths.some((ignored) => pathname.startsWith(ignored));
|
|
723
|
+
}
|
|
724
|
+
function shouldProxy(pathname, proxyPrefixes) {
|
|
725
|
+
return proxyPrefixes.some((prefix) => pathname.startsWith(prefix));
|
|
726
|
+
}
|
|
727
|
+
function getProxyTarget(pathname, proxyMap, defaultTarget) {
|
|
728
|
+
for (const [prefix, target] of Object.entries(proxyMap)) {
|
|
729
|
+
if (pathname.startsWith(prefix)) {
|
|
730
|
+
return target;
|
|
561
731
|
}
|
|
562
|
-
}
|
|
732
|
+
}
|
|
733
|
+
return defaultTarget;
|
|
563
734
|
}
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
735
|
+
function viteMiddlewareProxy(options) {
|
|
736
|
+
const {
|
|
737
|
+
cookieFile,
|
|
738
|
+
target,
|
|
739
|
+
debug = false,
|
|
740
|
+
proxyMap = {},
|
|
741
|
+
proxyPaths = [],
|
|
742
|
+
ignorePaths = []
|
|
743
|
+
} = options;
|
|
744
|
+
const cookieReader = new CookieReader({ cookieFile }, debug);
|
|
745
|
+
let currentCookie = cookieReader.readCookie();
|
|
746
|
+
const allProxyPrefixes = [
|
|
747
|
+
...Object.keys(proxyMap),
|
|
748
|
+
...proxyPaths
|
|
749
|
+
];
|
|
571
750
|
return {
|
|
572
|
-
name: "vite-
|
|
751
|
+
name: "vite-middleware-proxy",
|
|
573
752
|
apply: "serve",
|
|
574
753
|
configureServer(server) {
|
|
575
|
-
|
|
754
|
+
const httpProxy2 = __require("http-proxy");
|
|
755
|
+
const proxyServer = httpProxy2.createProxyServer({});
|
|
576
756
|
if (debug) {
|
|
577
|
-
console.log("[
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
if (
|
|
583
|
-
|
|
584
|
-
req.headers["cookie"] = currentCookie;
|
|
757
|
+
console.log("[ViteMiddlewareProxy] Watching cookie file:", cookieFile);
|
|
758
|
+
}
|
|
759
|
+
server.middlewares.use((req, res, next) => {
|
|
760
|
+
const pathname = new URL(req.url || "/", "http://localhost").pathname;
|
|
761
|
+
if (isIgnoredPath(pathname, ignorePaths)) {
|
|
762
|
+
if (debug) {
|
|
763
|
+
console.log("[ViteMiddlewareProxy] Ignoring:", pathname);
|
|
585
764
|
}
|
|
586
765
|
next();
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
if (!shouldProxy(pathname, allProxyPrefixes)) {
|
|
769
|
+
next();
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
const proxyTarget = getProxyTarget(pathname, proxyMap, target);
|
|
594
773
|
if (debug) {
|
|
595
|
-
console.log(
|
|
774
|
+
console.log("[ViteMiddlewareProxy] Proxying:", req.method, pathname, "->", proxyTarget);
|
|
596
775
|
}
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
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);
|
|
776
|
+
currentCookie = cookieReader.readCookie();
|
|
777
|
+
if (currentCookie) {
|
|
778
|
+
if (debug) {
|
|
779
|
+
console.log("[ViteMiddlewareProxy] Injecting cookie:", `(length: ${currentCookie.length})`);
|
|
610
780
|
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
781
|
+
req.headers["cookie"] = currentCookie;
|
|
782
|
+
req.headers["Cookie"] = currentCookie;
|
|
783
|
+
}
|
|
784
|
+
proxyServer.web(req, res, {
|
|
785
|
+
target: proxyTarget,
|
|
786
|
+
changeOrigin: true,
|
|
787
|
+
secure: false,
|
|
788
|
+
ignorePath: false
|
|
789
|
+
});
|
|
790
|
+
});
|
|
791
|
+
proxyServer.on("error", (err, req) => {
|
|
792
|
+
console.error("[ViteMiddlewareProxy] Proxy error:", err.message, "for", req.url);
|
|
793
|
+
});
|
|
618
794
|
}
|
|
619
795
|
};
|
|
620
796
|
}
|
|
@@ -656,11 +832,11 @@ function createVueProxyConfig(target, options = {}) {
|
|
|
656
832
|
function createFileCookieGetter(cookieFile, options = {}) {
|
|
657
833
|
const {
|
|
658
834
|
watch = "auto",
|
|
659
|
-
debug =
|
|
835
|
+
debug = true,
|
|
660
836
|
productionEnvs = [],
|
|
661
837
|
isDev
|
|
662
838
|
} = options;
|
|
663
|
-
const reader = new CookieReader({ cookieFile: path4.resolve(cookieFile) });
|
|
839
|
+
const reader = new CookieReader({ cookieFile: path4.resolve(cookieFile) }, debug);
|
|
664
840
|
let shouldWatch;
|
|
665
841
|
if (isDev !== void 0) {
|
|
666
842
|
shouldWatch = isDev;
|
|
@@ -725,61 +901,6 @@ function createAutoProxyConfig(options) {
|
|
|
725
901
|
}
|
|
726
902
|
return result;
|
|
727
903
|
}
|
|
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
904
|
export {
|
|
784
905
|
AutoProxyCookie,
|
|
785
906
|
CookieReader,
|
|
@@ -787,15 +908,11 @@ export {
|
|
|
787
908
|
createAutoProxyConfig,
|
|
788
909
|
createAutoProxyCookie,
|
|
789
910
|
createCookieGetter,
|
|
790
|
-
createDevProxyCookie,
|
|
791
911
|
createFileCookieGetter,
|
|
792
912
|
createVueProxyConfig,
|
|
793
913
|
detectProductionEnvironment,
|
|
794
|
-
getViteMajorVersion,
|
|
795
|
-
getViteVersion,
|
|
796
914
|
isProductionValue,
|
|
797
915
|
shouldEnableWatch,
|
|
798
|
-
|
|
799
|
-
viteDevProxyCookie,
|
|
916
|
+
viteMiddlewareProxy,
|
|
800
917
|
watchCookieFile
|
|
801
918
|
};
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,2 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dev-proxy-cookie 模块入口文件
|
|
3
|
+
*
|
|
4
|
+
* 提供开发环境下的 Cookie 代理和文件监听功能,支持 Vue CLI 和 Vite 构建工具。
|
|
5
|
+
*
|
|
6
|
+
* 主要功能:
|
|
7
|
+
* - Cookie 文件读取与监听
|
|
8
|
+
* - 代理请求时自动注入 Cookie
|
|
9
|
+
* - 支持 Vue CLI 和 Vite 两种构建工具
|
|
10
|
+
* - 智能环境检测,自动启用/禁用文件监听
|
|
11
|
+
*
|
|
12
|
+
* @module dev-proxy-cookie
|
|
13
|
+
*/
|
|
1
14
|
export * from './proxy';
|
|
2
15
|
export * from './utils';
|