@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.min.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var U=Object.create;var w=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var B=Object.getPrototypeOf,G=Object.prototype.hasOwnProperty;var J=(t,e)=>{for(var o in e)w(t,o,{get:e[o],enumerable:!0})},W=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of j(e))!G.call(t,n)&&n!==o&&w(t,n,{get:()=>e[n],enumerable:!(r=T(e,n))||r.enumerable});return t};var f=(t,e,o)=>(o=t!=null?U(B(t)):{},W(e||!t||!t.__esModule?w(o,"default",{value:t,enumerable:!0}):o,t)),z=t=>W(w({},"__esModule",{value:!0}),t);var oe={};J(oe,{AutoProxyCookie:()=>m,CookieReader:()=>u,CookieWatcher:()=>R,createAutoProxyConfig:()=>Y,createAutoProxyCookie:()=>O,createCookieGetter:()=>K,createDevProxyCookie:()=>Q,createFileCookieGetter:()=>X,createVueProxyConfig:()=>D,detectProductionEnvironment:()=>_,getViteMajorVersion:()=>ee,getViteVersion:()=>Z,isProductionValue:()=>S,shouldEnableWatch:()=>x,viteAutoProxyCookie:()=>E,viteDevProxyCookie:()=>A,watchCookieFile:()=>k});module.exports=z(oe);var H=f(require("http")),L=f(require("fs")),q=f(require("path")),N=f(require("http-proxy"));var d=f(require("fs")),v=f(require("path")),u=class{constructor(e){this.options={encoding:"utf-8",...e}}readCookie(){try{let e=v.resolve(this.options.cookieFile);return d.existsSync(e)?d.readFileSync(e,this.options.encoding||"utf-8").split(`
2
- `).map(i=>i.trim()).filter(i=>i&&!i.startsWith("#")).join("; "):""}catch{return""}}ensureCookieFile(){let e=v.resolve(this.options.cookieFile),o=v.dirname(e);d.existsSync(o)||d.mkdirSync(o,{recursive:!0}),d.existsSync(e)||d.writeFileSync(e,"")}};function K(t){let e=new u({cookieFile:t});return()=>e.readCookie()}var I=f(require("path")),$=f(require("chokidar"));var R=class{constructor(e){this.watcher=null;this.lastContent="";this.handleChange=()=>{let e=this.cookieReader.readCookie();e!==this.lastContent&&(this.lastContent=e,this.options.onCookieChange(e),console.log(`[CookieWatcher] Cookie updated from file: ${this.options.cookieFile}`))};this.options={autoCreateFile:!0,...e},this.cookieReader=new u({cookieFile:e.cookieFile})}start(){this.options.autoCreateFile&&this.cookieReader.ensureCookieFile();let e=I.resolve(this.options.cookieFile);this.lastContent=this.cookieReader.readCookie(),console.log(`[CookieWatcher] Started watching: ${e}`);try{this.watcher=$.default.watch(e,{persistent:!0,ignoreInitial:!0,awaitWriteFinish:{stabilityThreshold:100,pollInterval:50}}),this.watcher.on("change",this.handleChange),this.watcher.on("add",this.handleChange),this.watcher.on("error",o=>{this.options.onError?.(o)})}catch(o){this.options.onError?.(o)}}stop(){this.watcher&&(this.watcher.close(),this.watcher=null,console.log(`[CookieWatcher] Stopped watching: ${this.options.cookieFile}`))}getCurrentCookie(){return this.lastContent}};function k(t,e,o){let r=new R({cookieFile:t,onCookieChange:e,onError:o});return r.start(),r}function S(t){return["production","prod","prd","release","staging","uat"].includes(t.toLowerCase().trim())}function _(t=[],e=!1,o="[env-detector]"){let r=process.env;if(t.length>0)for(let s of t){let a=r[s];if(a&&S(a))return e&&console.log(`${o} Detected production via custom env: ${s}=${a}`),!0}let n=["NODE_ENV","BUILD_MODE","VUE_APP_ENV","VITE_NODE_ENV","WEBPACK_MODE","CI_ENV","APP_ENV","ENV","DEPLOY_ENV","RUN_MODE"];for(let s of n){let a=r[s];if(a&&S(a))return e&&console.log(`${o} Detected production via env: ${s}=${a}`),!0}if(r.CI==="true"||r.CI==="1"||r.CI==="yes")return e&&console.log(`${o} Detected production via CI env`),!0;if(r.npm_lifecycle_event){let s=r.npm_lifecycle_event.toLowerCase();if(s.includes("build")||s.includes("prod")||s.includes("prd")||s.includes("release"))return e&&console.log(`${o} Detected production via lifecycle event: ${r.npm_lifecycle_event}`),!0}let i=process.argv.join("").toLowerCase();return i.includes("build")||i.includes("production")||i.includes("--mode=production")||i.includes("--prod")||i.includes("--release")?(e&&console.log(`${o} Detected production via process arguments`),!0):!1}function x(t,e=[],o=!1,r="[env-detector]"){return typeof t=="boolean"?(o&&!t&&console.log(`${r} Watch disabled by user setting`),t):_(e,o,r)?(o&&console.log(`${r} Auto-detected production mode - disabling watch`),!1):(o&&console.log(`${r} Auto-detected development mode - enabling watch`),!0)}function C(t,e){e&&(t.removeHeader("cookie"),t.removeHeader("Cookie"),t.setHeader("Cookie",e))}var m=class{constructor(e){this.currentCookie="";this.server=null;this.proxyServer=null;this.watcher=null;this.handleCookieChange=e=>{if(e!==this.currentCookie&&(this.currentCookie=e,this.log("info","[AutoProxyCookie] Cookie updated:",e?"(has cookie)":"(empty)"),this.options.autoRestart&&this.options.restartMarkerFile)){let o=q.resolve(this.options.restartMarkerFile);L.writeFileSync(o,JSON.stringify({timestamp:Date.now(),cookie:e},null,2)),this.log("info","[AutoProxyCookie] Restart marker written to:",o),this.log("info","[AutoProxyCookie] Please restart the dev server for changes to take effect")}};this.handleOnProxyReq=(e,o,r,n)=>{if(this.currentCookie&&C(e,this.currentCookie),this.log("debug","[AutoProxyCookie] Proxy Request:",o.method,o.url),this.options.hooks.onProxyReq)try{this.options.hooks.onProxyReq(e,o,r)}catch(i){this.log("error","[AutoProxyCookie] onProxyReq hook error:",i.message)}};this.handleOnProxyRes=(e,o,r)=>{let n=["Content-Type","Content-Length","Authorization","Set-Cookie","X-Requested-With","Access-Control-Allow-Origin","Access-Control-Allow-Credentials"];if(r.setHeader("Access-Control-Allow-Origin","*"),r.setHeader("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS"),r.setHeader("Access-Control-Allow-Headers",n.join(",")),r.setHeader("Access-Control-Allow-Credentials","true"),this.log("debug","[AutoProxyCookie] Proxy Response:",o.url,e.statusCode),this.options.hooks.onProxyRes)try{this.options.hooks.onProxyRes(e,o,r)}catch(i){this.log("error","[AutoProxyCookie] onProxyRes hook error:",i.message)}};this.handleOnError=(e,o,r)=>{if(this.log("error","[AutoProxyCookie] Proxy Error:",e.message),this.log("error","[AutoProxyCookie] URL:",o.url),r instanceof H.ServerResponse&&!r.headersSent&&(r.writeHead(503,{"Content-Type":"application/json; charset=utf-8"}),r.end(JSON.stringify({success:!1,message:"\u670D\u52A1\u6682\u4E0D\u53EF\u7528\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5",error:e.message}))),this.options.hooks.onError)try{this.options.hooks.onError(e,o,r)}catch(n){this.log("error","[AutoProxyCookie] onError hook error:",n.message)}};this.handleOnWsError=(e,o,r)=>{if(this.log("error","[AutoProxyCookie] WebSocket Proxy Error:",e.message),this.log("error","[AutoProxyCookie] WebSocket URL:",o.url),r&&r.close(),this.options.hooks.onWsError)try{this.options.hooks.onWsError(e,o,r)}catch(n){this.log("error","[AutoProxyCookie] onWsError hook error:",n.message)}};let r={...e,hooks:{...{onProxyReq:()=>{},onProxyRes:()=>{},onError:()=>{},onWsError:()=>{}},...e.hooks||{}}};this.options={debug:!1,autoRestart:!1,restartMarkerFile:".cookie-restart-marker",proxyMap:{},ignorePaths:[],ws:!0,changeOrigin:!0,secure:!1,followRedirects:!0,autoRewrite:!1,protocolRewrite:void 0,logLevel:"info",cookieDomainRewrite:"*",cookiePathRewrite:!1,headers:{},...r},this.cookieReader=new u({cookieFile:e.cookieFile})}getProxyUrl(e){let o=e.url?.split("?")[0]||"/",r=this.options.proxyMap||{};for(let[n,i]of Object.entries(r))if(o.startsWith(n))return i;return this.options.target}isIgnoredPath(e){return(this.options.ignorePaths||[]).some(r=>e.startsWith(r))}log(e,...o){let r={debug:0,info:1,warn:2,error:3},n=r[this.options.logLevel||"info"];r[e]>=n&&(e==="error"?console.error(...o):e==="warn"?console.warn(...o):console.log(...o))}createProxyOptions(e){let{ws:o,changeOrigin:r,secure:n,followRedirects:i,autoRewrite:s,protocolRewrite:a,cookieDomainRewrite:c,cookiePathRewrite:p,headers:h}=this.options;return{target:e,ws:o,changeOrigin:r,secure:n,followRedirects:i,autoRewrite:s,protocolRewrite:a,cookieDomainRewrite:c,cookiePathRewrite:p,headers:{...h},ignorePath:!1}}async setup(e){this.server=e,this.currentCookie=this.cookieReader.readCookie();try{let o={target:this.options.target,changeOrigin:this.options.changeOrigin,secure:this.options.secure,followRedirects:this.options.followRedirects,autoRewrite:this.options.autoRewrite,protocolRewrite:this.options.protocolRewrite,cookieDomainRewrite:this.options.cookieDomainRewrite,cookiePathRewrite:this.options.cookiePathRewrite,ws:this.options.ws};this.proxyServer=N.default.createProxyServer(o),this.proxyServer.on("proxyReq",this.handleOnProxyReq),this.proxyServer.on("proxyRes",this.handleOnProxyRes),this.proxyServer.on("error",this.handleOnError),this.options.ws&&this.proxyServer.on("wsError",this.handleOnWsError),this.log("info","[AutoProxyCookie] Proxy server created with WebSocket support")}catch(o){this.log("warn","[AutoProxyCookie] http-proxy create failed, using basic mode:",o.message)}e.middlewares.use((o,r,n)=>{let i=new URL(o.url||"/","http://localhost").pathname;if(this.isIgnoredPath(i)){n();return}if(this.currentCookie=this.cookieReader.readCookie(),this.proxyServer){let s=this.getProxyUrl(o);(this.options.debug||this.options.logLevel==="debug")&&this.log("info",`[AutoProxyCookie] ${i} -> ${s}`);let a=this.createProxyOptions(s);try{this.proxyServer.web(o,r,a)}catch(c){this.log("error","[AutoProxyCookie] Proxy web error:",c.message),n(c)}}else n()}),this.options.ws&&this.server.httpServer&&this.proxyServer&&(this.server.httpServer.on("upgrade",(o,r,n)=>{let i=new URL(o.url||"/","http://localhost").pathname;if(this.isIgnoredPath(i)){r.destroy();return}let s=this.getProxyUrl(o);this.proxyServer?.ws(o,r,n,{target:s,ws:!0,changeOrigin:this.options.changeOrigin,secure:this.options.secure})}),this.log("info","[AutoProxyCookie] WebSocket upgrade handler registered")),this.startFileWatch(),this.log("info","[AutoProxyCookie] Auto-proxy middleware enabled"),this.log("info","[AutoProxyCookie] Target:",this.options.target),this.log("info","[AutoProxyCookie] Cookie file:",this.options.cookieFile),this.options.autoRestart&&this.log("info","[AutoProxyCookie] Auto-restart enabled"),this.options.ws&&this.log("info","[AutoProxyCookie] WebSocket support enabled")}startFileWatch(){let e;this.options.isDev!==void 0?(e=this.options.isDev,this.options.debug&&console.log(`[AutoProxyCookie] isDev=${this.options.isDev}, ${e?"enabling":"disabling"} watch`)):(e=!0,this.options.debug&&console.log("[AutoProxyCookie] Default behavior: enabling watch (dev mode)")),e?this.watcher=k(this.options.cookieFile,this.handleCookieChange,o=>{this.log("error","[AutoProxyCookie] File watch error:",o.message)}):this.options.debug&&console.log("[AutoProxyCookie] File watch disabled")}stop(){this.watcher&&(this.watcher.stop(),this.watcher=null),this.proxyServer&&(this.proxyServer.close(),this.proxyServer=null),this.log("info","[AutoProxyCookie] Stopped")}getCurrentCookie(){return this.currentCookie}};function O(t){return new m(t)}function E(t){let{name:e="vite-auto-proxy-cookie",isDev:o,...r}=t,n=null;return{name:e,apply:"serve",async configureServer(i){n=O({...r,debug:t.debug??!1,autoRestart:t.autoRestart??!0,isDev:o});try{await n.setup(i)}catch(s){console.error("[vite-auto-proxy-cookie] Failed to setup proxy:",s),t.debug&&console.log("[vite-auto-proxy-cookie] Falling back to middleware mode")}},closeBundle(){n?.stop()}}}function A(t){let{cookieFile:e,debug:o=!1,onCookieChange:r,watch:n="auto",isDev:i}=t,s=null,a="",c=new u({cookieFile:e});return{name:"vite-dev-proxy-cookie",apply:"serve",configureServer(p){a=c.readCookie(),o&&console.log("[vite-dev-proxy-cookie] Initial cookie loaded");let h=p.middlewares||p._middlewares||p.app;h&&typeof h.use=="function"?h.use((l,y,b)=>{a&&l.url?.startsWith("/")&&(l.headers=l.headers||{},l.headers.cookie=a),b()}):console.warn("[vite-dev-proxy-cookie] Could not access middleware stack, cookie injection disabled");let g;i!==void 0?(g=i,o&&console.log(`[vite-dev-proxy-cookie] isDev=${i}, ${g?"enabling":"disabling"} watch`)):g=x(n,[],o,"[vite-dev-proxy-cookie]"),g?s=k(e,l=>{a=l,r?.(l),console.log("[vite-dev-proxy-cookie] Cookie changed, please restart server for full effect")},l=>{console.error("[vite-dev-proxy-cookie] Watch error:",l.message)}):o&&console.log("[vite-dev-proxy-cookie] File watch disabled")},closeBundle(){s?.stop()}}}var F=f(require("path"));function D(t,e={}){let{getCookie:o,debug:r=!1,headers:n={},ws:i=!1,changeOrigin:s=!0,secure:a=!1,onError:c}=e;return{ws:i,target:t,changeOrigin:s,secure:a,headers:n,onProxyReq:(h,g)=>{let l=o?o():"";if(l&&C(h,l),r){let y=g.url||"/";console.log("[Proxy Request]",y,g.method,l?"(with cookie)":"(no cookie)")}},onError:c||(h=>{console.error(`
3
- [Proxy Error]`,h.message)})}}function X(t,e={}){let{watch:o="auto",debug:r=!1,productionEnvs:n=[],isDev:i}=e,s=new u({cookieFile:F.resolve(t)}),a;return i!==void 0?(a=i,r&&console.log(`[CookieFile] isDev=${i}, ${a?"enabling":"disabling"} watch`)):a=x(o,n,r,"[CookieFile]"),a?k(F.resolve(t),c=>{r&&console.log("[CookieFile] Updated:",c?"(has cookie)":"(empty)")},c=>{console.error("[CookieFile] Watch error:",c.message)}):r&&console.log("[CookieFile] File watch disabled"),()=>s.readCookie()}function Y(t){let{target:e,ignorePaths:o=[],includePaths:r=[],additionalProxies:n={},getCookie:i,debug:s,headers:a}=t,c={};if(r.length>0)for(let p of r)c[p]=D(e,{getCookie:i,debug:s,headers:a});else{let p={ws:!1,target:e,changeOrigin:!0,secure:!1,headers:a,onProxyReq:(h,g)=>{let l=g.url||"/";if(o.some(b=>l.startsWith(b)))return;let y=i?i():"";y&&C(h,y),s&&console.log("[Proxy Request]",l,g.method,y?"(with cookie)":"(no cookie)")},onError:h=>{console.error(`
4
- [Proxy Error]`,h.message)}};c["/"]=p}for(let[p,h]of Object.entries(n))c[p]=D(h,{getCookie:i,debug:s,headers:a});return c}var M="",P=null;function V(){if(P!==null)return P;try{M=require("vite/package.json").version,P=parseInt(M.split(".")[0],10)}catch{P=5}return P}function Q(t){let{mode:e="auto",watch:o="auto",isDev:r,...n}=t,i=V();if(t.debug&&console.log(`[dev-proxy-cookie] Detected Vite ${i}.x`),e==="cookie"||e==="auto"&&!t.target)return A({cookieFile:t.cookieFile,debug:t.debug,onCookieChange:t.onCookieChange,watch:o,isDev:r});let s={cookieFile:t.cookieFile,target:t.target,debug:t.debug,autoRestart:t.autoRestart??!0,restartMarkerFile:t.restartMarkerFile,proxyMap:t.proxyMap,ignorePaths:t.ignorePaths};return E(s)}function Z(){return V(),M}function ee(){return V()}0&&(module.exports={AutoProxyCookie,CookieReader,CookieWatcher,createAutoProxyConfig,createAutoProxyCookie,createCookieGetter,createDevProxyCookie,createFileCookieGetter,createVueProxyConfig,detectProductionEnvironment,getViteMajorVersion,getViteVersion,isProductionValue,shouldEnableWatch,viteAutoProxyCookie,viteDevProxyCookie,watchCookieFile});
1
+ "use strict";var N=Object.create;var m=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var q=Object.getPrototypeOf,j=Object.prototype.hasOwnProperty;var B=(t,o)=>{for(var e in o)m(t,e,{get:o[e],enumerable:!0})},F=(t,o,e,r)=>{if(o&&typeof o=="object"||typeof o=="function")for(let s of T(o))!j.call(t,s)&&s!==e&&m(t,s,{get:()=>o[s],enumerable:!(r=_(o,s))||r.enumerable});return t};var f=(t,o,e)=>(e=t!=null?N(q(t)):{},F(o||!t||!t.__esModule?m(e,"default",{value:t,enumerable:!0}):e,t)),G=t=>F(m({},"__esModule",{value:!0}),t);var eo={};B(eo,{AutoProxyCookie:()=>R,CookieReader:()=>d,CookieWatcher:()=>w,createAutoProxyConfig:()=>oo,createAutoProxyCookie:()=>K,createCookieGetter:()=>J,createFileCookieGetter:()=>Z,createVueProxyConfig:()=>O,detectProductionEnvironment:()=>H,isProductionValue:()=>A,shouldEnableWatch:()=>E,viteMiddlewareProxy:()=>Q,watchCookieFile:()=>P});module.exports=G(eo);var V=f(require("http")),$=f(require("fs")),U=f(require("path")),L=f(require("http-proxy"));var k=f(require("fs")),C=f(require("path")),d=class{constructor(o,e=!1){this.options={encoding:"utf-8",...o},this.debug=e}readCookie(){try{let o=C.resolve(this.options.cookieFile);if(this.debug&&(console.log("[CookieReader] Resolved cookie file path:",o),console.log("[CookieReader] File exists:",k.existsSync(o))),k.existsSync(o)){let e=k.readFileSync(o,this.options.encoding||"utf-8");this.debug&&console.log("[CookieReader] File content length:",e.length);let n=e.split(`
2
+ `).map(i=>i.trim()).filter(i=>i&&!i.startsWith("#")).join("; ");return this.debug&&console.log("[CookieReader] Parsed cookie:",n?"(has cookie)":"(empty)"),n}return this.debug&&console.log("[CookieReader] Cookie file not found:",o),""}catch(o){return this.debug&&console.error("[CookieReader] Error reading cookie file:",o.message),""}}ensureCookieFile(){let o=C.resolve(this.options.cookieFile),e=C.dirname(o);k.existsSync(e)||k.mkdirSync(e,{recursive:!0}),k.existsSync(o)||k.writeFileSync(o,"")}};function J(t){let o=new d({cookieFile:t});return()=>o.readCookie()}var I=f(require("path")),W=f(require("chokidar"));var w=class{constructor(o){this.watcher=null;this.lastContent="";this.handleChange=()=>{let o=this.cookieReader.readCookie();o!==this.lastContent&&(this.lastContent=o,this.options.onCookieChange(o),console.log(`[CookieWatcher] Cookie updated from file: ${this.options.cookieFile}`))};this.options={autoCreateFile:!0,...o},this.cookieReader=new d({cookieFile:o.cookieFile})}start(){this.options.autoCreateFile&&this.cookieReader.ensureCookieFile();let o=I.resolve(this.options.cookieFile);this.lastContent=this.cookieReader.readCookie(),console.log(`[CookieWatcher] Started watching: ${o}`);try{this.watcher=W.default.watch(o,{persistent:!0,ignoreInitial:!0,awaitWriteFinish:{stabilityThreshold:100,pollInterval:50}}),this.watcher.on("change",this.handleChange),this.watcher.on("add",this.handleChange),this.watcher.on("error",e=>{this.options.onError?.(e)})}catch(e){this.options.onError?.(e)}}stop(){this.watcher&&(this.watcher.close(),this.watcher=null,console.log(`[CookieWatcher] Stopped watching: ${this.options.cookieFile}`))}getCurrentCookie(){return this.lastContent}};function P(t,o,e){let r=new w({cookieFile:t,onCookieChange:o,onError:e});return r.start(),r}function A(t){return["production","prod","prd","release","staging","uat"].includes(t.toLowerCase().trim())}function H(t=[],o=!1,e="[env-detector]"){let r=process.env;if(t.length>0)for(let i of t){let a=r[i];if(a&&A(a))return o&&console.log(`${e} Detected production via custom env: ${i}=${a}`),!0}let s=["NODE_ENV","BUILD_MODE","VUE_APP_ENV","VITE_NODE_ENV","WEBPACK_MODE","CI_ENV","APP_ENV","ENV","DEPLOY_ENV","RUN_MODE"];for(let i of s){let a=r[i];if(a&&A(a))return o&&console.log(`${e} Detected production via env: ${i}=${a}`),!0}if(r.CI==="true"||r.CI==="1"||r.CI==="yes")return o&&console.log(`${e} Detected production via CI env`),!0;if(r.npm_lifecycle_event){let i=r.npm_lifecycle_event.toLowerCase();if(i.includes("build")||i.includes("prod")||i.includes("prd")||i.includes("release"))return o&&console.log(`${e} Detected production via lifecycle event: ${r.npm_lifecycle_event}`),!0}let n=process.argv.join("").toLowerCase();return n.includes("build")||n.includes("production")||n.includes("--mode=production")||n.includes("--prod")||n.includes("--release")?(o&&console.log(`${e} Detected production via process arguments`),!0):!1}function E(t,o=[],e=!1,r="[env-detector]"){return typeof t=="boolean"?(e&&!t&&console.log(`${r} Watch disabled by user setting`),t):H(o,e,r)?(e&&console.log(`${r} Auto-detected production mode - disabling watch`),!1):(e&&console.log(`${r} Auto-detected development mode - enabling watch`),!0)}function v(t,o){if(console.log("[applyDevCookieHeader] === START ==="),console.log("[applyDevCookieHeader] Cookie to apply:",o?`(length: ${o.length})`:"(empty)"),!o){console.log("[applyDevCookieHeader] Cookie is empty, returning"),console.log("[applyDevCookieHeader] === END ===");return}let e=t.getHeader?.("Cookie");if(console.log("[applyDevCookieHeader] Cookie current:",e?`(length: ${String(e).length})`:"(none)"),e===o){console.log("[applyDevCookieHeader] Cookie is already set, skipping"),console.log("[applyDevCookieHeader] === END ===");return}t.removeHeader("cookie"),t.removeHeader("Cookie"),t.setHeader("Cookie",o);let r=t.getHeader?.("Cookie");console.log("[applyDevCookieHeader] Cookie new:",r?`(length: ${String(r).length})`:"(failed)"),console.log("[applyDevCookieHeader] === END ===")}var R=class{constructor(o){this.currentCookie="";this.server=null;this.proxyServer=null;this.watcher=null;this.handleCookieChange=o=>{if(o!==this.currentCookie&&(this.currentCookie=o,this.log("info","[AutoProxyCookie] Cookie updated:",o?"(has cookie)":"(empty)"),this.options.autoRestart&&this.options.restartMarkerFile)){let e=U.resolve(this.options.restartMarkerFile);$.writeFileSync(e,JSON.stringify({timestamp:Date.now(),cookie:o},null,2)),this.log("info","[AutoProxyCookie] Restart marker written to:",e),this.log("info","[AutoProxyCookie] Please restart the dev server for changes to take effect")}};this.handleOnProxyReq=(o,e,r,s)=>{if(console.log("[AutoProxyCookie] === handleOnProxyReq START ==="),console.log("[AutoProxyCookie] Request URL:",e.method,e.url),console.log("[AutoProxyCookie] useCookie:",this.options.useCookie),console.log("[AutoProxyCookie] Current cookie:",this.currentCookie?`(length: ${this.currentCookie.length})`:"(empty)"),this.options.useCookie&&this.currentCookie?(console.log("[AutoProxyCookie] Applying cookie header..."),v(o,this.currentCookie),console.log("[AutoProxyCookie] Cookie header applied successfully")):this.options.useCookie?console.log("[AutoProxyCookie] No cookie to apply - currentCookie is empty!"):console.log("[AutoProxyCookie] useCookie is false, skipping cookie injection"),this.log("debug","[AutoProxyCookie] Proxy Request:",e.method,e.url),this.options.hooks.onProxyReq)try{this.options.hooks.onProxyReq(o,e,r)}catch(n){this.log("error","[AutoProxyCookie] onProxyReq hook error:",n.message)}console.log("[AutoProxyCookie] === handleOnProxyReq END ===")};this.handleOnProxyRes=(o,e,r)=>{let s=["Content-Type","Content-Length","Authorization","Set-Cookie","X-Requested-With","Access-Control-Allow-Origin","Access-Control-Allow-Credentials"];if(r.setHeader("Access-Control-Allow-Origin","*"),r.setHeader("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS"),r.setHeader("Access-Control-Allow-Headers",s.join(",")),r.setHeader("Access-Control-Allow-Credentials","true"),this.log("debug","[AutoProxyCookie] Proxy Response:",e.url,o.statusCode),this.options.hooks.onProxyRes)try{this.options.hooks.onProxyRes(o,e,r)}catch(n){this.log("error","[AutoProxyCookie] onProxyRes hook error:",n.message)}};this.handleOnError=(o,e,r)=>{if(this.log("error","[AutoProxyCookie] Proxy Error:",o.message),this.log("error","[AutoProxyCookie] URL:",e.url),r instanceof V.ServerResponse&&!r.headersSent&&(r.writeHead(503,{"Content-Type":"application/json; charset=utf-8"}),r.end(JSON.stringify({success:!1,message:"\u670D\u52A1\u6682\u4E0D\u53EF\u7528\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5",error:o.message}))),this.options.hooks.onError)try{this.options.hooks.onError(o,e,r)}catch(s){this.log("error","[AutoProxyCookie] onError hook error:",s.message)}};this.handleOnWsError=(o,e,r)=>{if(this.log("error","[AutoProxyCookie] WebSocket Proxy Error:",o.message),this.log("error","[AutoProxyCookie] WebSocket URL:",e.url),r&&r.close(),this.options.hooks.onWsError)try{this.options.hooks.onWsError(o,e,r)}catch(s){this.log("error","[AutoProxyCookie] onWsError hook error:",s.message)}};let r={...o,hooks:{...{onProxyReq:()=>{},onProxyRes:()=>{},onError:()=>{},onWsError:()=>{}},...o.hooks||{}}};this.options={debug:!1,autoRestart:!1,restartMarkerFile:".cookie-restart-marker",proxyMap:{},proxyPaths:[],ignorePaths:[],ws:!0,changeOrigin:!0,secure:!1,followRedirects:!0,autoRewrite:!1,protocolRewrite:void 0,logLevel:"info",cookieDomainRewrite:"*",cookiePathRewrite:!1,headers:{},useCookie:!0,...r},this.cookieReader=new d({cookieFile:o.cookieFile},o.debug??!1)}getProxyUrl(o){let e=o.url||"/",r=new URL(e,"http://localhost").pathname,s=this.options.proxyMap||{},n=this.options.proxyPaths||[];this.log("debug","[AutoProxyCookie] getProxyUrl - Request path:",r),this.log("debug","[AutoProxyCookie] getProxyUrl - Available proxyMap paths:",Object.keys(s)),this.log("debug","[AutoProxyCookie] getProxyUrl - Available proxyPaths:",n);for(let[i,a]of Object.entries(s)){let l=r.startsWith(i);if(this.log("debug","[AutoProxyCookie] getProxyUrl - Checking proxyMap:",i,"- matches:",l),l)return this.log("debug","[AutoProxyCookie] getProxyUrl - Matched proxyMap:",i,"->",a),a}for(let i of n){let a=r.startsWith(i);if(this.log("debug","[AutoProxyCookie] getProxyUrl - Checking proxyPaths:",i,"- matches:",a),a)return this.log("debug","[AutoProxyCookie] getProxyUrl - Matched proxyPaths:",i,"->",this.options.target),this.options.target}return this.log("debug","[AutoProxyCookie] getProxyUrl - No match found, using default target:",this.options.target),this.options.target}isIgnoredPath(o){return(this.options.ignorePaths||[]).some(r=>o.startsWith(r))}log(o,...e){let r={debug:0,info:1,warn:2,error:3},s=this.options.debug?"debug":this.options.logLevel||"info",n=r[s];r[o]>=n&&(o==="error"?console.error(...e):o==="warn"?console.warn(...e):console.log(...e))}createProxyOptions(o){let{ws:e,changeOrigin:r,secure:s,followRedirects:n,autoRewrite:i,protocolRewrite:a,cookieDomainRewrite:l,cookiePathRewrite:g,headers:p}=this.options;return{target:o,ws:e,changeOrigin:r,secure:s,followRedirects:n,autoRewrite:i,protocolRewrite:a,cookieDomainRewrite:l,cookiePathRewrite:g,headers:{...p},ignorePath:!1}}async setup(o){this.server=o,this.currentCookie=this.cookieReader.readCookie();try{let e={target:this.options.target,changeOrigin:this.options.changeOrigin,secure:this.options.secure,followRedirects:this.options.followRedirects,autoRewrite:this.options.autoRewrite,protocolRewrite:this.options.protocolRewrite,cookieDomainRewrite:this.options.cookieDomainRewrite,cookiePathRewrite:this.options.cookiePathRewrite,ws:this.options.ws};this.proxyServer=L.default.createProxyServer(e),this.proxyServer.on("proxyReq",this.handleOnProxyReq),this.proxyServer.on("proxyRes",this.handleOnProxyRes),this.proxyServer.on("error",this.handleOnError),this.options.ws&&this.proxyServer.on("wsError",this.handleOnWsError),this.log("info","[AutoProxyCookie] Proxy server created with WebSocket support")}catch(e){this.log("warn","[AutoProxyCookie] http-proxy create failed, using basic mode:",e.message)}o.middlewares.use((e,r,s)=>{let n=e.url||"/",i=new URL(n,"http://localhost").pathname;if(console.log("[AutoProxyCookie] === Incoming Request ==="),console.log("[AutoProxyCookie] Method:",e.method),console.log("[AutoProxyCookie] Full URL:",n),console.log("[AutoProxyCookie] Pathname:",i),console.log("[AutoProxyCookie] useCookie:",this.options.useCookie),console.log("[AutoProxyCookie] Headers:",JSON.stringify(e.headers,null,2)),this.isIgnoredPath(i)){console.log("[AutoProxyCookie] Path ignored, passing to next middleware"),s();return}this.options.useCookie?(this.currentCookie=this.cookieReader.readCookie(),console.log("[AutoProxyCookie] Current cookie:",this.currentCookie?`(length: ${this.currentCookie.length})`:"(empty)"),this.currentCookie&&console.log("[AutoProxyCookie] Cookie preview:",this.currentCookie)):console.log("[AutoProxyCookie] useCookie is false, skipping cookie reading");let a=Object.keys(this.options.proxyMap||{}),l=this.options.proxyPaths||[],p=[...a,...l].some(h=>i.startsWith(h));if(console.log("[AutoProxyCookie] Path matches proxy rules:",p),this.proxyServer){let h=this.getProxyUrl(e);console.log(`[AutoProxyCookie] Proxying ${e.method} ${i} -> ${h}`);let y=this.createProxyOptions(h);try{console.log("[AutoProxyCookie] Calling proxyServer.web..."),this.proxyServer.web(e,r,y),console.log("[AutoProxyCookie] proxyServer.web called successfully")}catch(u){console.error("[AutoProxyCookie] Proxy web error:",u.message),s(u)}}else console.log("[AutoProxyCookie] No proxy server, passing to next middleware"),s()}),this.options.ws&&this.server.httpServer&&this.proxyServer&&(this.server.httpServer.on("upgrade",(e,r,s)=>{let n=new URL(e.url||"/","http://localhost").pathname;if(this.isIgnoredPath(n)){r.destroy();return}let i=this.getProxyUrl(e);this.proxyServer?.ws(e,r,s,{target:i,ws:!0,changeOrigin:this.options.changeOrigin,secure:this.options.secure})}),this.log("info","[AutoProxyCookie] WebSocket upgrade handler registered")),this.startFileWatch(),this.log("info","[AutoProxyCookie] Auto-proxy middleware enabled"),this.log("info","[AutoProxyCookie] Target:",this.options.target),this.log("info","[AutoProxyCookie] Cookie file:",this.options.cookieFile),this.options.autoRestart&&this.log("info","[AutoProxyCookie] Auto-restart enabled"),this.options.ws&&this.log("info","[AutoProxyCookie] WebSocket support enabled")}startFileWatch(){let o;this.options.isDev!==void 0?(o=this.options.isDev,this.options.debug&&console.log(`[AutoProxyCookie] isDev=${this.options.isDev}, ${o?"enabling":"disabling"} watch`)):(o=!0,this.options.debug&&console.log("[AutoProxyCookie] Default behavior: enabling watch (dev mode)")),o?this.watcher=P(this.options.cookieFile,this.handleCookieChange,e=>{this.log("error","[AutoProxyCookie] File watch error:",e.message)}):this.options.debug&&console.log("[AutoProxyCookie] File watch disabled")}stop(){this.watcher&&(this.watcher.stop(),this.watcher=null),this.proxyServer&&(this.proxyServer.close(),this.proxyServer=null),this.log("info","[AutoProxyCookie] Stopped")}getCurrentCookie(){return this.currentCookie}};function K(t){return new R(t)}function z(t,o){return o.some(e=>t.startsWith(e))}function X(t,o){return o.some(e=>t.startsWith(e))}function Y(t,o,e){for(let[r,s]of Object.entries(o))if(t.startsWith(r))return s;return e}function Q(t){let{cookieFile:o,target:e,debug:r=!1,useCookie:s=!0,proxyMap:n={},proxyPaths:i=[],ignorePaths:a=[]}=t,l=new d({cookieFile:o},r),g="";s&&(g=l.readCookie());let p=[...Object.keys(n),...i];return{name:"vite-middleware-proxy",apply:"serve",configureServer(h){let u=require("http-proxy").createProxyServer({});s&&r&&console.log("[ViteMiddlewareProxy] Watching cookie file:",o),h.middlewares.use((c,b,M)=>{let x=new URL(c.url||"/","http://localhost").pathname;if(z(x,a)){r&&console.log("[ViteMiddlewareProxy] Ignoring:",x),M();return}if(!X(x,p)){M();return}let D=Y(x,n,e);r&&console.log("[ViteMiddlewareProxy] Proxying:",c.method,x,"->",D),s?(g=l.readCookie(),g?(r&&console.log("[ViteMiddlewareProxy] Injecting cookie:",`(length: ${g.length})`),c.headers.cookie=g,c.headers.Cookie=g):r&&console.log("[ViteMiddlewareProxy] Cookie file is empty")):r&&console.log("[ViteMiddlewareProxy] useCookie is false, skipping cookie injection"),u.web(c,b,{target:D,changeOrigin:!0,secure:!1,ignorePath:!1})}),u.on("error",(c,b)=>{console.error("[ViteMiddlewareProxy] Proxy error:",c.message,"for",b.url)})}}}var S=f(require("path"));function O(t,o={}){let{getCookie:e,debug:r=!1,useCookie:s=!0,headers:n={},ws:i=!1,changeOrigin:a=!0,secure:l=!1,onError:g}=o;return{ws:i,target:t,changeOrigin:a,secure:l,headers:n,onProxyReq:(h,y)=>{let u=y.url||"/";if(s){let c=e?e():"";c&&v(h,c),r&&console.log("[Proxy Request]",u,y.method,c?"(with cookie)":"(no cookie)")}else r&&console.log("[Proxy Request]",u,y.method,"(useCookie is false, skipping cookie injection)")},onError:g||(h=>{console.error(`
3
+ [Proxy Error]`,h.message)})}}function Z(t,o={}){let{watch:e="auto",debug:r=!0,productionEnvs:s=[],isDev:n}=o,i=new d({cookieFile:S.resolve(t)},r),a;return n!==void 0?(a=n,r&&console.log(`[CookieFile] isDev=${n}, ${a?"enabling":"disabling"} watch`)):a=E(e,s,r,"[CookieFile]"),a?P(S.resolve(t),l=>{r&&console.log("[CookieFile] Updated:",l?"(has cookie)":"(empty)")},l=>{console.error("[CookieFile] Watch error:",l.message)}):r&&console.log("[CookieFile] File watch disabled"),()=>i.readCookie()}function oo(t){let{target:o,ignorePaths:e=[],includePaths:r=[],additionalProxies:s={},getCookie:n,debug:i,headers:a,useCookie:l=!0}=t,g={};if(r.length>0)for(let p of r)g[p]=O(o,{getCookie:n,debug:i,headers:a,useCookie:l});else{let p={ws:!1,target:o,changeOrigin:!0,secure:!1,headers:a,onProxyReq:(h,y)=>{let u=y.url||"/";if(!e.some(c=>u.startsWith(c)))if(l){let c=n?n():"";c&&v(h,c),i&&console.log("[Proxy Request]",u,y.method,c?"(with cookie)":"(no cookie)")}else i&&console.log("[Proxy Request]",u,y.method,"(useCookie is false, skipping cookie injection)")},onError:h=>{console.error(`
4
+ [Proxy Error]`,h.message)}};g["/"]=p}for(let[p,h]of Object.entries(s))g[p]=O(h,{getCookie:n,debug:i,headers:a,useCookie:l});return g}0&&(module.exports={AutoProxyCookie,CookieReader,CookieWatcher,createAutoProxyConfig,createAutoProxyCookie,createCookieGetter,createFileCookieGetter,createVueProxyConfig,detectProductionEnvironment,isProductionValue,shouldEnableWatch,viteMiddlewareProxy,watchCookieFile});
@@ -1,4 +1,4 @@
1
- var $=(t=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(t,{get:(e,o)=>(typeof require<"u"?require:e)[o]}):t)(function(t){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+t+'" is not supported')});import*as A from"http";import*as F from"fs";import*as D from"path";import L from"http-proxy";import*as d from"fs";import*as y from"path";var u=class{constructor(e){this.options={encoding:"utf-8",...e}}readCookie(){try{let e=y.resolve(this.options.cookieFile);return d.existsSync(e)?d.readFileSync(e,this.options.encoding||"utf-8").split(`
2
- `).map(i=>i.trim()).filter(i=>i&&!i.startsWith("#")).join("; "):""}catch{return""}}ensureCookieFile(){let e=y.resolve(this.options.cookieFile),o=y.dirname(e);d.existsSync(o)||d.mkdirSync(o,{recursive:!0}),d.existsSync(e)||d.writeFileSync(e,"")}};function N(t){let e=new u({cookieFile:t});return()=>e.readCookie()}import*as O from"path";import _ from"chokidar";var w=class{constructor(e){this.watcher=null;this.lastContent="";this.handleChange=()=>{let e=this.cookieReader.readCookie();e!==this.lastContent&&(this.lastContent=e,this.options.onCookieChange(e),console.log(`[CookieWatcher] Cookie updated from file: ${this.options.cookieFile}`))};this.options={autoCreateFile:!0,...e},this.cookieReader=new u({cookieFile:e.cookieFile})}start(){this.options.autoCreateFile&&this.cookieReader.ensureCookieFile();let e=O.resolve(this.options.cookieFile);this.lastContent=this.cookieReader.readCookie(),console.log(`[CookieWatcher] Started watching: ${e}`);try{this.watcher=_.watch(e,{persistent:!0,ignoreInitial:!0,awaitWriteFinish:{stabilityThreshold:100,pollInterval:50}}),this.watcher.on("change",this.handleChange),this.watcher.on("add",this.handleChange),this.watcher.on("error",o=>{this.options.onError?.(o)})}catch(o){this.options.onError?.(o)}}stop(){this.watcher&&(this.watcher.close(),this.watcher=null,console.log(`[CookieWatcher] Stopped watching: ${this.options.cookieFile}`))}getCurrentCookie(){return this.lastContent}};function k(t,e,o){let r=new w({cookieFile:t,onCookieChange:e,onError:o});return r.start(),r}function E(t){return["production","prod","prd","release","staging","uat"].includes(t.toLowerCase().trim())}function H(t=[],e=!1,o="[env-detector]"){let r=process.env;if(t.length>0)for(let s of t){let a=r[s];if(a&&E(a))return e&&console.log(`${o} Detected production via custom env: ${s}=${a}`),!0}let n=["NODE_ENV","BUILD_MODE","VUE_APP_ENV","VITE_NODE_ENV","WEBPACK_MODE","CI_ENV","APP_ENV","ENV","DEPLOY_ENV","RUN_MODE"];for(let s of n){let a=r[s];if(a&&E(a))return e&&console.log(`${o} Detected production via env: ${s}=${a}`),!0}if(r.CI==="true"||r.CI==="1"||r.CI==="yes")return e&&console.log(`${o} Detected production via CI env`),!0;if(r.npm_lifecycle_event){let s=r.npm_lifecycle_event.toLowerCase();if(s.includes("build")||s.includes("prod")||s.includes("prd")||s.includes("release"))return e&&console.log(`${o} Detected production via lifecycle event: ${r.npm_lifecycle_event}`),!0}let i=process.argv.join("").toLowerCase();return i.includes("build")||i.includes("production")||i.includes("--mode=production")||i.includes("--prod")||i.includes("--release")?(e&&console.log(`${o} Detected production via process arguments`),!0):!1}function C(t,e=[],o=!1,r="[env-detector]"){return typeof t=="boolean"?(o&&!t&&console.log(`${r} Watch disabled by user setting`),t):H(e,o,r)?(o&&console.log(`${r} Auto-detected production mode - disabling watch`),!1):(o&&console.log(`${r} Auto-detected development mode - enabling watch`),!0)}function v(t,e){e&&(t.removeHeader("cookie"),t.removeHeader("Cookie"),t.setHeader("Cookie",e))}var m=class{constructor(e){this.currentCookie="";this.server=null;this.proxyServer=null;this.watcher=null;this.handleCookieChange=e=>{if(e!==this.currentCookie&&(this.currentCookie=e,this.log("info","[AutoProxyCookie] Cookie updated:",e?"(has cookie)":"(empty)"),this.options.autoRestart&&this.options.restartMarkerFile)){let o=D.resolve(this.options.restartMarkerFile);F.writeFileSync(o,JSON.stringify({timestamp:Date.now(),cookie:e},null,2)),this.log("info","[AutoProxyCookie] Restart marker written to:",o),this.log("info","[AutoProxyCookie] Please restart the dev server for changes to take effect")}};this.handleOnProxyReq=(e,o,r,n)=>{if(this.currentCookie&&v(e,this.currentCookie),this.log("debug","[AutoProxyCookie] Proxy Request:",o.method,o.url),this.options.hooks.onProxyReq)try{this.options.hooks.onProxyReq(e,o,r)}catch(i){this.log("error","[AutoProxyCookie] onProxyReq hook error:",i.message)}};this.handleOnProxyRes=(e,o,r)=>{let n=["Content-Type","Content-Length","Authorization","Set-Cookie","X-Requested-With","Access-Control-Allow-Origin","Access-Control-Allow-Credentials"];if(r.setHeader("Access-Control-Allow-Origin","*"),r.setHeader("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS"),r.setHeader("Access-Control-Allow-Headers",n.join(",")),r.setHeader("Access-Control-Allow-Credentials","true"),this.log("debug","[AutoProxyCookie] Proxy Response:",o.url,e.statusCode),this.options.hooks.onProxyRes)try{this.options.hooks.onProxyRes(e,o,r)}catch(i){this.log("error","[AutoProxyCookie] onProxyRes hook error:",i.message)}};this.handleOnError=(e,o,r)=>{if(this.log("error","[AutoProxyCookie] Proxy Error:",e.message),this.log("error","[AutoProxyCookie] URL:",o.url),r instanceof A.ServerResponse&&!r.headersSent&&(r.writeHead(503,{"Content-Type":"application/json; charset=utf-8"}),r.end(JSON.stringify({success:!1,message:"\u670D\u52A1\u6682\u4E0D\u53EF\u7528\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5",error:e.message}))),this.options.hooks.onError)try{this.options.hooks.onError(e,o,r)}catch(n){this.log("error","[AutoProxyCookie] onError hook error:",n.message)}};this.handleOnWsError=(e,o,r)=>{if(this.log("error","[AutoProxyCookie] WebSocket Proxy Error:",e.message),this.log("error","[AutoProxyCookie] WebSocket URL:",o.url),r&&r.close(),this.options.hooks.onWsError)try{this.options.hooks.onWsError(e,o,r)}catch(n){this.log("error","[AutoProxyCookie] onWsError hook error:",n.message)}};let r={...e,hooks:{...{onProxyReq:()=>{},onProxyRes:()=>{},onError:()=>{},onWsError:()=>{}},...e.hooks||{}}};this.options={debug:!1,autoRestart:!1,restartMarkerFile:".cookie-restart-marker",proxyMap:{},ignorePaths:[],ws:!0,changeOrigin:!0,secure:!1,followRedirects:!0,autoRewrite:!1,protocolRewrite:void 0,logLevel:"info",cookieDomainRewrite:"*",cookiePathRewrite:!1,headers:{},...r},this.cookieReader=new u({cookieFile:e.cookieFile})}getProxyUrl(e){let o=e.url?.split("?")[0]||"/",r=this.options.proxyMap||{};for(let[n,i]of Object.entries(r))if(o.startsWith(n))return i;return this.options.target}isIgnoredPath(e){return(this.options.ignorePaths||[]).some(r=>e.startsWith(r))}log(e,...o){let r={debug:0,info:1,warn:2,error:3},n=r[this.options.logLevel||"info"];r[e]>=n&&(e==="error"?console.error(...o):e==="warn"?console.warn(...o):console.log(...o))}createProxyOptions(e){let{ws:o,changeOrigin:r,secure:n,followRedirects:i,autoRewrite:s,protocolRewrite:a,cookieDomainRewrite:c,cookiePathRewrite:p,headers:h}=this.options;return{target:e,ws:o,changeOrigin:r,secure:n,followRedirects:i,autoRewrite:s,protocolRewrite:a,cookieDomainRewrite:c,cookiePathRewrite:p,headers:{...h},ignorePath:!1}}async setup(e){this.server=e,this.currentCookie=this.cookieReader.readCookie();try{let o={target:this.options.target,changeOrigin:this.options.changeOrigin,secure:this.options.secure,followRedirects:this.options.followRedirects,autoRewrite:this.options.autoRewrite,protocolRewrite:this.options.protocolRewrite,cookieDomainRewrite:this.options.cookieDomainRewrite,cookiePathRewrite:this.options.cookiePathRewrite,ws:this.options.ws};this.proxyServer=L.createProxyServer(o),this.proxyServer.on("proxyReq",this.handleOnProxyReq),this.proxyServer.on("proxyRes",this.handleOnProxyRes),this.proxyServer.on("error",this.handleOnError),this.options.ws&&this.proxyServer.on("wsError",this.handleOnWsError),this.log("info","[AutoProxyCookie] Proxy server created with WebSocket support")}catch(o){this.log("warn","[AutoProxyCookie] http-proxy create failed, using basic mode:",o.message)}e.middlewares.use((o,r,n)=>{let i=new URL(o.url||"/","http://localhost").pathname;if(this.isIgnoredPath(i)){n();return}if(this.currentCookie=this.cookieReader.readCookie(),this.proxyServer){let s=this.getProxyUrl(o);(this.options.debug||this.options.logLevel==="debug")&&this.log("info",`[AutoProxyCookie] ${i} -> ${s}`);let a=this.createProxyOptions(s);try{this.proxyServer.web(o,r,a)}catch(c){this.log("error","[AutoProxyCookie] Proxy web error:",c.message),n(c)}}else n()}),this.options.ws&&this.server.httpServer&&this.proxyServer&&(this.server.httpServer.on("upgrade",(o,r,n)=>{let i=new URL(o.url||"/","http://localhost").pathname;if(this.isIgnoredPath(i)){r.destroy();return}let s=this.getProxyUrl(o);this.proxyServer?.ws(o,r,n,{target:s,ws:!0,changeOrigin:this.options.changeOrigin,secure:this.options.secure})}),this.log("info","[AutoProxyCookie] WebSocket upgrade handler registered")),this.startFileWatch(),this.log("info","[AutoProxyCookie] Auto-proxy middleware enabled"),this.log("info","[AutoProxyCookie] Target:",this.options.target),this.log("info","[AutoProxyCookie] Cookie file:",this.options.cookieFile),this.options.autoRestart&&this.log("info","[AutoProxyCookie] Auto-restart enabled"),this.options.ws&&this.log("info","[AutoProxyCookie] WebSocket support enabled")}startFileWatch(){let e;this.options.isDev!==void 0?(e=this.options.isDev,this.options.debug&&console.log(`[AutoProxyCookie] isDev=${this.options.isDev}, ${e?"enabling":"disabling"} watch`)):(e=!0,this.options.debug&&console.log("[AutoProxyCookie] Default behavior: enabling watch (dev mode)")),e?this.watcher=k(this.options.cookieFile,this.handleCookieChange,o=>{this.log("error","[AutoProxyCookie] File watch error:",o.message)}):this.options.debug&&console.log("[AutoProxyCookie] File watch disabled")}stop(){this.watcher&&(this.watcher.stop(),this.watcher=null),this.proxyServer&&(this.proxyServer.close(),this.proxyServer=null),this.log("info","[AutoProxyCookie] Stopped")}getCurrentCookie(){return this.currentCookie}};function M(t){return new m(t)}function V(t){let{name:e="vite-auto-proxy-cookie",isDev:o,...r}=t,n=null;return{name:e,apply:"serve",async configureServer(i){n=M({...r,debug:t.debug??!1,autoRestart:t.autoRestart??!0,isDev:o});try{await n.setup(i)}catch(s){console.error("[vite-auto-proxy-cookie] Failed to setup proxy:",s),t.debug&&console.log("[vite-auto-proxy-cookie] Falling back to middleware mode")}},closeBundle(){n?.stop()}}}function W(t){let{cookieFile:e,debug:o=!1,onCookieChange:r,watch:n="auto",isDev:i}=t,s=null,a="",c=new u({cookieFile:e});return{name:"vite-dev-proxy-cookie",apply:"serve",configureServer(p){a=c.readCookie(),o&&console.log("[vite-dev-proxy-cookie] Initial cookie loaded");let h=p.middlewares||p._middlewares||p.app;h&&typeof h.use=="function"?h.use((l,f,P)=>{a&&l.url?.startsWith("/")&&(l.headers=l.headers||{},l.headers.cookie=a),P()}):console.warn("[vite-dev-proxy-cookie] Could not access middleware stack, cookie injection disabled");let g;i!==void 0?(g=i,o&&console.log(`[vite-dev-proxy-cookie] isDev=${i}, ${g?"enabling":"disabling"} watch`)):g=C(n,[],o,"[vite-dev-proxy-cookie]"),g?s=k(e,l=>{a=l,r?.(l),console.log("[vite-dev-proxy-cookie] Cookie changed, please restart server for full effect")},l=>{console.error("[vite-dev-proxy-cookie] Watch error:",l.message)}):o&&console.log("[vite-dev-proxy-cookie] File watch disabled")},closeBundle(){s?.stop()}}}import*as R from"path";function I(t,e={}){let{getCookie:o,debug:r=!1,headers:n={},ws:i=!1,changeOrigin:s=!0,secure:a=!1,onError:c}=e;return{ws:i,target:t,changeOrigin:s,secure:a,headers:n,onProxyReq:(h,g)=>{let l=o?o():"";if(l&&v(h,l),r){let f=g.url||"/";console.log("[Proxy Request]",f,g.method,l?"(with cookie)":"(no cookie)")}},onError:c||(h=>{console.error(`
3
- [Proxy Error]`,h.message)})}}function he(t,e={}){let{watch:o="auto",debug:r=!1,productionEnvs:n=[],isDev:i}=e,s=new u({cookieFile:R.resolve(t)}),a;return i!==void 0?(a=i,r&&console.log(`[CookieFile] isDev=${i}, ${a?"enabling":"disabling"} watch`)):a=C(o,n,r,"[CookieFile]"),a?k(R.resolve(t),c=>{r&&console.log("[CookieFile] Updated:",c?"(has cookie)":"(empty)")},c=>{console.error("[CookieFile] Watch error:",c.message)}):r&&console.log("[CookieFile] File watch disabled"),()=>s.readCookie()}function pe(t){let{target:e,ignorePaths:o=[],includePaths:r=[],additionalProxies:n={},getCookie:i,debug:s,headers:a}=t,c={};if(r.length>0)for(let p of r)c[p]=I(e,{getCookie:i,debug:s,headers:a});else{let p={ws:!1,target:e,changeOrigin:!0,secure:!1,headers:a,onProxyReq:(h,g)=>{let l=g.url||"/";if(o.some(P=>l.startsWith(P)))return;let f=i?i():"";f&&v(h,f),s&&console.log("[Proxy Request]",l,g.method,f?"(with cookie)":"(no cookie)")},onError:h=>{console.error(`
4
- [Proxy Error]`,h.message)}};c["/"]=p}for(let[p,h]of Object.entries(n))c[p]=I(h,{getCookie:i,debug:s,headers:a});return c}var b="",x=null;function S(){if(x!==null)return x;try{b=$("vite/package.json").version,x=parseInt(b.split(".")[0],10)}catch{x=5}return x}function fe(t){let{mode:e="auto",watch:o="auto",isDev:r,...n}=t,i=S();if(t.debug&&console.log(`[dev-proxy-cookie] Detected Vite ${i}.x`),e==="cookie"||e==="auto"&&!t.target)return W({cookieFile:t.cookieFile,debug:t.debug,onCookieChange:t.onCookieChange,watch:o,isDev:r});let s={cookieFile:t.cookieFile,target:t.target,debug:t.debug,autoRestart:t.autoRestart??!0,restartMarkerFile:t.restartMarkerFile,proxyMap:t.proxyMap,ignorePaths:t.ignorePaths};return V(s)}function ke(){return S(),b}function ye(){return S()}export{m as AutoProxyCookie,u as CookieReader,w as CookieWatcher,pe as createAutoProxyConfig,M as createAutoProxyCookie,N as createCookieGetter,fe as createDevProxyCookie,he as createFileCookieGetter,I as createVueProxyConfig,H as detectProductionEnvironment,ye as getViteMajorVersion,ke as getViteVersion,E as isProductionValue,C as shouldEnableWatch,V as viteAutoProxyCookie,W as viteDevProxyCookie,k as watchCookieFile};
1
+ var W=(i=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(i,{get:(o,e)=>(typeof require<"u"?require:o)[e]}):i)(function(i){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+i+'" is not supported')});import*as M from"http";import*as D from"fs";import*as F from"path";import $ from"http-proxy";import*as d from"fs";import*as x from"path";var k=class{constructor(o,e=!1){this.options={encoding:"utf-8",...o},this.debug=e}readCookie(){try{let o=x.resolve(this.options.cookieFile);if(this.debug&&(console.log("[CookieReader] Resolved cookie file path:",o),console.log("[CookieReader] File exists:",d.existsSync(o))),d.existsSync(o)){let e=d.readFileSync(o,this.options.encoding||"utf-8");this.debug&&console.log("[CookieReader] File content length:",e.length);let s=e.split(`
2
+ `).map(t=>t.trim()).filter(t=>t&&!t.startsWith("#")).join("; ");return this.debug&&console.log("[CookieReader] Parsed cookie:",s?"(has cookie)":"(empty)"),s}return this.debug&&console.log("[CookieReader] Cookie file not found:",o),""}catch(o){return this.debug&&console.error("[CookieReader] Error reading cookie file:",o.message),""}}ensureCookieFile(){let o=x.resolve(this.options.cookieFile),e=x.dirname(o);d.existsSync(e)||d.mkdirSync(e,{recursive:!0}),d.existsSync(o)||d.writeFileSync(o,"")}};function T(i){let o=new k({cookieFile:i});return()=>o.readCookie()}import*as E from"path";import H from"chokidar";var m=class{constructor(o){this.watcher=null;this.lastContent="";this.handleChange=()=>{let o=this.cookieReader.readCookie();o!==this.lastContent&&(this.lastContent=o,this.options.onCookieChange(o),console.log(`[CookieWatcher] Cookie updated from file: ${this.options.cookieFile}`))};this.options={autoCreateFile:!0,...o},this.cookieReader=new k({cookieFile:o.cookieFile})}start(){this.options.autoCreateFile&&this.cookieReader.ensureCookieFile();let o=E.resolve(this.options.cookieFile);this.lastContent=this.cookieReader.readCookie(),console.log(`[CookieWatcher] Started watching: ${o}`);try{this.watcher=H.watch(o,{persistent:!0,ignoreInitial:!0,awaitWriteFinish:{stabilityThreshold:100,pollInterval:50}}),this.watcher.on("change",this.handleChange),this.watcher.on("add",this.handleChange),this.watcher.on("error",e=>{this.options.onError?.(e)})}catch(e){this.options.onError?.(e)}}stop(){this.watcher&&(this.watcher.close(),this.watcher=null,console.log(`[CookieWatcher] Stopped watching: ${this.options.cookieFile}`))}getCurrentCookie(){return this.lastContent}};function P(i,o,e){let r=new m({cookieFile:i,onCookieChange:o,onError:e});return r.start(),r}function S(i){return["production","prod","prd","release","staging","uat"].includes(i.toLowerCase().trim())}function V(i=[],o=!1,e="[env-detector]"){let r=process.env;if(i.length>0)for(let t of i){let a=r[t];if(a&&S(a))return o&&console.log(`${e} Detected production via custom env: ${t}=${a}`),!0}let n=["NODE_ENV","BUILD_MODE","VUE_APP_ENV","VITE_NODE_ENV","WEBPACK_MODE","CI_ENV","APP_ENV","ENV","DEPLOY_ENV","RUN_MODE"];for(let t of n){let a=r[t];if(a&&S(a))return o&&console.log(`${e} Detected production via env: ${t}=${a}`),!0}if(r.CI==="true"||r.CI==="1"||r.CI==="yes")return o&&console.log(`${e} Detected production via CI env`),!0;if(r.npm_lifecycle_event){let t=r.npm_lifecycle_event.toLowerCase();if(t.includes("build")||t.includes("prod")||t.includes("prd")||t.includes("release"))return o&&console.log(`${e} Detected production via lifecycle event: ${r.npm_lifecycle_event}`),!0}let s=process.argv.join("").toLowerCase();return s.includes("build")||s.includes("production")||s.includes("--mode=production")||s.includes("--prod")||s.includes("--release")?(o&&console.log(`${e} Detected production via process arguments`),!0):!1}function O(i,o=[],e=!1,r="[env-detector]"){return typeof i=="boolean"?(e&&!i&&console.log(`${r} Watch disabled by user setting`),i):V(o,e,r)?(e&&console.log(`${r} Auto-detected production mode - disabling watch`),!1):(e&&console.log(`${r} Auto-detected development mode - enabling watch`),!0)}function C(i,o){if(console.log("[applyDevCookieHeader] === START ==="),console.log("[applyDevCookieHeader] Cookie to apply:",o?`(length: ${o.length})`:"(empty)"),!o){console.log("[applyDevCookieHeader] Cookie is empty, returning"),console.log("[applyDevCookieHeader] === END ===");return}let e=i.getHeader?.("Cookie");if(console.log("[applyDevCookieHeader] Cookie current:",e?`(length: ${String(e).length})`:"(none)"),e===o){console.log("[applyDevCookieHeader] Cookie is already set, skipping"),console.log("[applyDevCookieHeader] === END ===");return}i.removeHeader("cookie"),i.removeHeader("Cookie"),i.setHeader("Cookie",o);let r=i.getHeader?.("Cookie");console.log("[applyDevCookieHeader] Cookie new:",r?`(length: ${String(r).length})`:"(failed)"),console.log("[applyDevCookieHeader] === END ===")}var w=class{constructor(o){this.currentCookie="";this.server=null;this.proxyServer=null;this.watcher=null;this.handleCookieChange=o=>{if(o!==this.currentCookie&&(this.currentCookie=o,this.log("info","[AutoProxyCookie] Cookie updated:",o?"(has cookie)":"(empty)"),this.options.autoRestart&&this.options.restartMarkerFile)){let e=F.resolve(this.options.restartMarkerFile);D.writeFileSync(e,JSON.stringify({timestamp:Date.now(),cookie:o},null,2)),this.log("info","[AutoProxyCookie] Restart marker written to:",e),this.log("info","[AutoProxyCookie] Please restart the dev server for changes to take effect")}};this.handleOnProxyReq=(o,e,r,n)=>{if(console.log("[AutoProxyCookie] === handleOnProxyReq START ==="),console.log("[AutoProxyCookie] Request URL:",e.method,e.url),console.log("[AutoProxyCookie] useCookie:",this.options.useCookie),console.log("[AutoProxyCookie] Current cookie:",this.currentCookie?`(length: ${this.currentCookie.length})`:"(empty)"),this.options.useCookie&&this.currentCookie?(console.log("[AutoProxyCookie] Applying cookie header..."),C(o,this.currentCookie),console.log("[AutoProxyCookie] Cookie header applied successfully")):this.options.useCookie?console.log("[AutoProxyCookie] No cookie to apply - currentCookie is empty!"):console.log("[AutoProxyCookie] useCookie is false, skipping cookie injection"),this.log("debug","[AutoProxyCookie] Proxy Request:",e.method,e.url),this.options.hooks.onProxyReq)try{this.options.hooks.onProxyReq(o,e,r)}catch(s){this.log("error","[AutoProxyCookie] onProxyReq hook error:",s.message)}console.log("[AutoProxyCookie] === handleOnProxyReq END ===")};this.handleOnProxyRes=(o,e,r)=>{let n=["Content-Type","Content-Length","Authorization","Set-Cookie","X-Requested-With","Access-Control-Allow-Origin","Access-Control-Allow-Credentials"];if(r.setHeader("Access-Control-Allow-Origin","*"),r.setHeader("Access-Control-Allow-Methods","GET, POST, PUT, DELETE, OPTIONS"),r.setHeader("Access-Control-Allow-Headers",n.join(",")),r.setHeader("Access-Control-Allow-Credentials","true"),this.log("debug","[AutoProxyCookie] Proxy Response:",e.url,o.statusCode),this.options.hooks.onProxyRes)try{this.options.hooks.onProxyRes(o,e,r)}catch(s){this.log("error","[AutoProxyCookie] onProxyRes hook error:",s.message)}};this.handleOnError=(o,e,r)=>{if(this.log("error","[AutoProxyCookie] Proxy Error:",o.message),this.log("error","[AutoProxyCookie] URL:",e.url),r instanceof M.ServerResponse&&!r.headersSent&&(r.writeHead(503,{"Content-Type":"application/json; charset=utf-8"}),r.end(JSON.stringify({success:!1,message:"\u670D\u52A1\u6682\u4E0D\u53EF\u7528\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5",error:o.message}))),this.options.hooks.onError)try{this.options.hooks.onError(o,e,r)}catch(n){this.log("error","[AutoProxyCookie] onError hook error:",n.message)}};this.handleOnWsError=(o,e,r)=>{if(this.log("error","[AutoProxyCookie] WebSocket Proxy Error:",o.message),this.log("error","[AutoProxyCookie] WebSocket URL:",e.url),r&&r.close(),this.options.hooks.onWsError)try{this.options.hooks.onWsError(o,e,r)}catch(n){this.log("error","[AutoProxyCookie] onWsError hook error:",n.message)}};let r={...o,hooks:{...{onProxyReq:()=>{},onProxyRes:()=>{},onError:()=>{},onWsError:()=>{}},...o.hooks||{}}};this.options={debug:!1,autoRestart:!1,restartMarkerFile:".cookie-restart-marker",proxyMap:{},proxyPaths:[],ignorePaths:[],ws:!0,changeOrigin:!0,secure:!1,followRedirects:!0,autoRewrite:!1,protocolRewrite:void 0,logLevel:"info",cookieDomainRewrite:"*",cookiePathRewrite:!1,headers:{},useCookie:!0,...r},this.cookieReader=new k({cookieFile:o.cookieFile},o.debug??!1)}getProxyUrl(o){let e=o.url||"/",r=new URL(e,"http://localhost").pathname,n=this.options.proxyMap||{},s=this.options.proxyPaths||[];this.log("debug","[AutoProxyCookie] getProxyUrl - Request path:",r),this.log("debug","[AutoProxyCookie] getProxyUrl - Available proxyMap paths:",Object.keys(n)),this.log("debug","[AutoProxyCookie] getProxyUrl - Available proxyPaths:",s);for(let[t,a]of Object.entries(n)){let l=r.startsWith(t);if(this.log("debug","[AutoProxyCookie] getProxyUrl - Checking proxyMap:",t,"- matches:",l),l)return this.log("debug","[AutoProxyCookie] getProxyUrl - Matched proxyMap:",t,"->",a),a}for(let t of s){let a=r.startsWith(t);if(this.log("debug","[AutoProxyCookie] getProxyUrl - Checking proxyPaths:",t,"- matches:",a),a)return this.log("debug","[AutoProxyCookie] getProxyUrl - Matched proxyPaths:",t,"->",this.options.target),this.options.target}return this.log("debug","[AutoProxyCookie] getProxyUrl - No match found, using default target:",this.options.target),this.options.target}isIgnoredPath(o){return(this.options.ignorePaths||[]).some(r=>o.startsWith(r))}log(o,...e){let r={debug:0,info:1,warn:2,error:3},n=this.options.debug?"debug":this.options.logLevel||"info",s=r[n];r[o]>=s&&(o==="error"?console.error(...e):o==="warn"?console.warn(...e):console.log(...e))}createProxyOptions(o){let{ws:e,changeOrigin:r,secure:n,followRedirects:s,autoRewrite:t,protocolRewrite:a,cookieDomainRewrite:l,cookiePathRewrite:g,headers:p}=this.options;return{target:o,ws:e,changeOrigin:r,secure:n,followRedirects:s,autoRewrite:t,protocolRewrite:a,cookieDomainRewrite:l,cookiePathRewrite:g,headers:{...p},ignorePath:!1}}async setup(o){this.server=o,this.currentCookie=this.cookieReader.readCookie();try{let e={target:this.options.target,changeOrigin:this.options.changeOrigin,secure:this.options.secure,followRedirects:this.options.followRedirects,autoRewrite:this.options.autoRewrite,protocolRewrite:this.options.protocolRewrite,cookieDomainRewrite:this.options.cookieDomainRewrite,cookiePathRewrite:this.options.cookiePathRewrite,ws:this.options.ws};this.proxyServer=$.createProxyServer(e),this.proxyServer.on("proxyReq",this.handleOnProxyReq),this.proxyServer.on("proxyRes",this.handleOnProxyRes),this.proxyServer.on("error",this.handleOnError),this.options.ws&&this.proxyServer.on("wsError",this.handleOnWsError),this.log("info","[AutoProxyCookie] Proxy server created with WebSocket support")}catch(e){this.log("warn","[AutoProxyCookie] http-proxy create failed, using basic mode:",e.message)}o.middlewares.use((e,r,n)=>{let s=e.url||"/",t=new URL(s,"http://localhost").pathname;if(console.log("[AutoProxyCookie] === Incoming Request ==="),console.log("[AutoProxyCookie] Method:",e.method),console.log("[AutoProxyCookie] Full URL:",s),console.log("[AutoProxyCookie] Pathname:",t),console.log("[AutoProxyCookie] useCookie:",this.options.useCookie),console.log("[AutoProxyCookie] Headers:",JSON.stringify(e.headers,null,2)),this.isIgnoredPath(t)){console.log("[AutoProxyCookie] Path ignored, passing to next middleware"),n();return}this.options.useCookie?(this.currentCookie=this.cookieReader.readCookie(),console.log("[AutoProxyCookie] Current cookie:",this.currentCookie?`(length: ${this.currentCookie.length})`:"(empty)"),this.currentCookie&&console.log("[AutoProxyCookie] Cookie preview:",this.currentCookie)):console.log("[AutoProxyCookie] useCookie is false, skipping cookie reading");let a=Object.keys(this.options.proxyMap||{}),l=this.options.proxyPaths||[],p=[...a,...l].some(h=>t.startsWith(h));if(console.log("[AutoProxyCookie] Path matches proxy rules:",p),this.proxyServer){let h=this.getProxyUrl(e);console.log(`[AutoProxyCookie] Proxying ${e.method} ${t} -> ${h}`);let y=this.createProxyOptions(h);try{console.log("[AutoProxyCookie] Calling proxyServer.web..."),this.proxyServer.web(e,r,y),console.log("[AutoProxyCookie] proxyServer.web called successfully")}catch(u){console.error("[AutoProxyCookie] Proxy web error:",u.message),n(u)}}else console.log("[AutoProxyCookie] No proxy server, passing to next middleware"),n()}),this.options.ws&&this.server.httpServer&&this.proxyServer&&(this.server.httpServer.on("upgrade",(e,r,n)=>{let s=new URL(e.url||"/","http://localhost").pathname;if(this.isIgnoredPath(s)){r.destroy();return}let t=this.getProxyUrl(e);this.proxyServer?.ws(e,r,n,{target:t,ws:!0,changeOrigin:this.options.changeOrigin,secure:this.options.secure})}),this.log("info","[AutoProxyCookie] WebSocket upgrade handler registered")),this.startFileWatch(),this.log("info","[AutoProxyCookie] Auto-proxy middleware enabled"),this.log("info","[AutoProxyCookie] Target:",this.options.target),this.log("info","[AutoProxyCookie] Cookie file:",this.options.cookieFile),this.options.autoRestart&&this.log("info","[AutoProxyCookie] Auto-restart enabled"),this.options.ws&&this.log("info","[AutoProxyCookie] WebSocket support enabled")}startFileWatch(){let o;this.options.isDev!==void 0?(o=this.options.isDev,this.options.debug&&console.log(`[AutoProxyCookie] isDev=${this.options.isDev}, ${o?"enabling":"disabling"} watch`)):(o=!0,this.options.debug&&console.log("[AutoProxyCookie] Default behavior: enabling watch (dev mode)")),o?this.watcher=P(this.options.cookieFile,this.handleCookieChange,e=>{this.log("error","[AutoProxyCookie] File watch error:",e.message)}):this.options.debug&&console.log("[AutoProxyCookie] File watch disabled")}stop(){this.watcher&&(this.watcher.stop(),this.watcher=null),this.proxyServer&&(this.proxyServer.close(),this.proxyServer=null),this.log("info","[AutoProxyCookie] Stopped")}getCurrentCookie(){return this.currentCookie}};function io(i){return new w(i)}function U(i,o){return o.some(e=>i.startsWith(e))}function L(i,o){return o.some(e=>i.startsWith(e))}function N(i,o,e){for(let[r,n]of Object.entries(o))if(i.startsWith(r))return n;return e}function ao(i){let{cookieFile:o,target:e,debug:r=!1,useCookie:n=!0,proxyMap:s={},proxyPaths:t=[],ignorePaths:a=[]}=i,l=new k({cookieFile:o},r),g="";n&&(g=l.readCookie());let p=[...Object.keys(s),...t];return{name:"vite-middleware-proxy",apply:"serve",configureServer(h){let u=W("http-proxy").createProxyServer({});n&&r&&console.log("[ViteMiddlewareProxy] Watching cookie file:",o),h.middlewares.use((c,v,b)=>{let f=new URL(c.url||"/","http://localhost").pathname;if(U(f,a)){r&&console.log("[ViteMiddlewareProxy] Ignoring:",f),b();return}if(!L(f,p)){b();return}let A=N(f,s,e);r&&console.log("[ViteMiddlewareProxy] Proxying:",c.method,f,"->",A),n?(g=l.readCookie(),g?(r&&console.log("[ViteMiddlewareProxy] Injecting cookie:",`(length: ${g.length})`),c.headers.cookie=g,c.headers.Cookie=g):r&&console.log("[ViteMiddlewareProxy] Cookie file is empty")):r&&console.log("[ViteMiddlewareProxy] useCookie is false, skipping cookie injection"),u.web(c,v,{target:A,changeOrigin:!0,secure:!1,ignorePath:!1})}),u.on("error",(c,v)=>{console.error("[ViteMiddlewareProxy] Proxy error:",c.message,"for",v.url)})}}}import*as R from"path";function I(i,o={}){let{getCookie:e,debug:r=!1,useCookie:n=!0,headers:s={},ws:t=!1,changeOrigin:a=!0,secure:l=!1,onError:g}=o;return{ws:t,target:i,changeOrigin:a,secure:l,headers:s,onProxyReq:(h,y)=>{let u=y.url||"/";if(n){let c=e?e():"";c&&C(h,c),r&&console.log("[Proxy Request]",u,y.method,c?"(with cookie)":"(no cookie)")}else r&&console.log("[Proxy Request]",u,y.method,"(useCookie is false, skipping cookie injection)")},onError:g||(h=>{console.error(`
3
+ [Proxy Error]`,h.message)})}}function ho(i,o={}){let{watch:e="auto",debug:r=!0,productionEnvs:n=[],isDev:s}=o,t=new k({cookieFile:R.resolve(i)},r),a;return s!==void 0?(a=s,r&&console.log(`[CookieFile] isDev=${s}, ${a?"enabling":"disabling"} watch`)):a=O(e,n,r,"[CookieFile]"),a?P(R.resolve(i),l=>{r&&console.log("[CookieFile] Updated:",l?"(has cookie)":"(empty)")},l=>{console.error("[CookieFile] Watch error:",l.message)}):r&&console.log("[CookieFile] File watch disabled"),()=>t.readCookie()}function po(i){let{target:o,ignorePaths:e=[],includePaths:r=[],additionalProxies:n={},getCookie:s,debug:t,headers:a,useCookie:l=!0}=i,g={};if(r.length>0)for(let p of r)g[p]=I(o,{getCookie:s,debug:t,headers:a,useCookie:l});else{let p={ws:!1,target:o,changeOrigin:!0,secure:!1,headers:a,onProxyReq:(h,y)=>{let u=y.url||"/";if(!e.some(c=>u.startsWith(c)))if(l){let c=s?s():"";c&&C(h,c),t&&console.log("[Proxy Request]",u,y.method,c?"(with cookie)":"(no cookie)")}else t&&console.log("[Proxy Request]",u,y.method,"(useCookie is false, skipping cookie injection)")},onError:h=>{console.error(`
4
+ [Proxy Error]`,h.message)}};g["/"]=p}for(let[p,h]of Object.entries(n))g[p]=I(h,{getCookie:s,debug:t,headers:a,useCookie:l});return g}export{w as AutoProxyCookie,k as CookieReader,m as CookieWatcher,po as createAutoProxyConfig,io as createAutoProxyCookie,T as createCookieGetter,ho as createFileCookieGetter,I as createVueProxyConfig,V as detectProductionEnvironment,S as isProductionValue,O as shouldEnableWatch,ao as viteMiddlewareProxy,P as watchCookieFile};