@gylautorun/dev-proxy-cookie 1.0.0-beta.1

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.
@@ -0,0 +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});
@@ -0,0 +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};