@anton.marusenko/pp-dev
Version:
Portal Page dev build tool
3 lines (2 loc) • 46.2 kB
JavaScript
;const e=require("http-proxy-middleware"),t=require("vite"),r=require("picocolors"),s=require("axios"),a=require("jsdom"),n=require("https"),i=require("memory-cache"),o=require("path"),c=require("fs"),l=require("url"),p=require("crypto"),d=require("process"),h=require("child_process"),u=require("console"),g=require("zlib"),f=require("express");var m="undefined"!=typeof document?document.currentScript:null;function w(e){const t=Object.create(null);if(e)for(const r in e)if("default"!==r){const s=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,s.get?s:{enumerable:!0,get:()=>e[r]})}return t.default=e,Object.freeze(t)}const y=w(i),v=w(o),b=w(p),k=w(d),x=w(h),P=w(u),T=(e,t,r)=>{const s=new RegExp(`(!!)?(https?(:(\\\\)?/(\\\\)?/)${e})`,"gi");return r.replace(s,((e,...r)=>e.startsWith("!!")?r[1]:`http${r[2]}${t}`))},A=(e,t,r)=>{e.setHeader("location",t),e.statusCode=r,e.end()};const $=new Map,I="default",S=(e="info",r=I)=>{if($.has(r))return $.get(r);if(r===I){const s=t.createLogger(e);return $.set(r,s),s}const s=t.createLogger(e);return $.set(r,s),s},E=r.createColors(),L=function(){const e={personal:{placeholder:"API Token",caption:`You can get the API Token\n <a href="https://${host}/api-token" target="_blank" \n style="color: #007bff; text-decoration: none;">here</a>`,endpoint:"/@api/login"},regular:{placeholder:"Legacy Token",caption:`You can get the Legacy Token\n <a href="https://${host}/api/get_token" target="_blank" \n style="color: #007bff; text-decoration: none;">here</a>`,endpoint:"/@api/login"}};function t(t,r,s){const a=e[t];a&&(r.placeholder=a.placeholder,s.innerHTML=a.caption)}function r(e,t){t.textContent=e,t.classList.add("show")}function s(e){e.classList.remove("show")}function a(e,t,a,n){a.addEventListener("click",(async a=>{if(a.preventDefault(),s(n),!t.value.trim())return r(`${t.placeholder} is required`,n),void t.focus();const i=document.getElementById("token-type-switcher"),o=i?.querySelector(".token-type-option.active"),c=o?.dataset.value||"personal";await async function(e,t,s,a){try{s.classList.add("loading");const n=await fetch("/@api/login",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({token:e,tokenType:t}),redirect:"follow"}),i=n.url&&new URL(n.url,window.location.origin);if(!i||i.pathname.startsWith("/login")||i.pathname.startsWith("/@api/"))try{const e=await n.json();r(`Login failed: ${e?.error||"Unknown error occurred"}`,a)}catch(e){r("Login failed: Unable to parse response",a)}else window.location.href=n.url}catch(e){r("Login failed: Network error. Please check your connection.",a)}finally{s.classList.remove("loading")}}(t.value.trim(),c,e,n)})),t.addEventListener("keydown",(e=>{"Enter"===e.key&&(e.preventDefault(),a.click())})),t.addEventListener("input",(()=>{s(n)}))}function n(e){const r=e.firstChild;if(!r)return;const s=r.lastChild?.cloneNode(!0);if(!s)return;s.innerHTML="";const n=s.cloneNode(!0);n.innerText="OR",n.setAttribute("role","separator"),n.setAttribute("aria-label","Alternative login method");const i=s.cloneNode(!0),o=document.createElement("div"),c=document.createElement("style");c.textContent="\n.helper-login-wrapper {\n font-family: Arial, sans-serif;\n color: #222;\n padding: 8px;\n background-color: #f8f9fa;\n border-radius: 8px;\n border: 1px solid #ddd;\n width: 100%;\n max-width: 400px;\n margin: 0 auto;\n box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);\n}\n\n.helper-login-wrapper .title {\n font-weight: bold;\n font-size: 18px;\n margin-bottom: 12px;\n text-align: center;\n}\n\n.helper-login-wrapper .control {\n display: flex;\n flex-direction: column;\n gap: 12px;\n}\n\n.helper-login-wrapper .control input {\n border: 1px solid #ddd;\n border-radius: 4px;\n width: 100%;\n box-sizing: border-box;\n text-align: center;\n height: 26px;\n padding: 0 8px;\n font-size: 14px;\n}\n\n.helper-login-wrapper .control input:focus {\n outline: none;\n border-color: #007bff;\n box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);\n}\n\n.helper-login-wrapper .control .caption {\n font-size: 12px;\n color: #666;\n text-align: center;\n}\n\n.helper-login-wrapper .footer {\n margin-top: 16px;\n text-align: center;\n}\n\n.helper-login-wrapper .footer .btn.submit {\n background-color: #007bff;\n color: white;\n border: none;\n border-radius: 4px;\n cursor: pointer;\n width: 100%;\n font-size: 14px;\n transition: background-color 0.3s;\n height: 26px;\n font-weight: 500;\n}\n\n.helper-login-wrapper .footer .btn.submit:hover {\n background-color: #0056b3;\n}\n\n.helper-login-wrapper .footer .btn.submit:disabled {\n background-color: #6c757d;\n cursor: not-allowed;\n}\n\n.helper-login-wrapper .token-type-switcher {\n display: flex;\n border: 1px solid #ccc;\n border-radius: 4px;\n overflow: hidden;\n margin-bottom: 12px;\n width: 100%;\n}\n\n.helper-login-wrapper .token-type-option {\n padding: 4px 8px;\n cursor: pointer;\n background-color: #f8f9fa;\n color: #666;\n font-size: 12px;\n text-align: center;\n transition: background-color 0.2s, color 0.2s;\n border-right: 1px solid #ccc;\n flex: 1;\n user-select: none;\n}\n\n.helper-login-wrapper .token-type-option:last-child {\n border-right: none;\n}\n\n.helper-login-wrapper .token-type-option.active {\n background-color: #007bff;\n color: white;\n font-weight: bold;\n}\n\n.helper-login-wrapper .token-type-option:not(.active):hover {\n background-color: #e9ecef;\n}\n\n.helper-login-wrapper .error-message {\n color: #dc3545;\n font-size: 12px;\n text-align: center;\n margin-top: 8px;\n display: none;\n}\n\n.helper-login-wrapper .error-message.show {\n display: block;\n}\n\n.helper-login-wrapper .loading {\n opacity: 0.6;\n pointer-events: none;\n}\n",document.head.appendChild(c),o.innerHTML=`\n<div class="helper-login-wrapper" role="form" aria-label="PP Dev Helper Login">\n <div class="title" role="heading" aria-level="2">PP Dev Helper</div>\n\n <div class="token-type-switcher" id="token-type-switcher" role="tablist" aria-label="Token type selection">\n <div class="token-type-option active" data-value="personal" role="tab" aria-selected="true" tabindex="0">\n API Token\n </div>\n <div class="token-type-option" data-value="regular" role="tab" aria-selected="false" tabindex="0">\n Legacy Token\n </div>\n </div>\n\n <div class="control">\n <input \n type="password" \n id="helper-token" \n placeholder="API Token"\n aria-label="Token input"\n aria-describedby="token-caption"\n >\n <span class="caption" id="token-caption">\n You can get the API Token\n <a href="https://${host}/api-token" target="_blank" style="color: #007bff; text-decoration: none;">here</a>\n </span>\n </div>\n\n <div class="error-message" id="error-message" role="alert" aria-live="polite"></div>\n\n <div class="footer">\n <button id="helper-token-submit" class="btn submit" type="button">\n Token Login\n </button>\n </div>\n</div>`,i.appendChild(o),r.appendChild(n),r.appendChild(i);const l=document.getElementById("token-type-switcher"),p=document.getElementById("helper-token"),d=document.getElementById("token-caption"),h=document.getElementById("helper-token-submit"),u=document.getElementById("error-message"),g=o.querySelector(".helper-login-wrapper");l&&p&&d&&h&&u&&g?(!function(e,r,s){const a=e.querySelectorAll(".token-type-option"),n=e=>{const n=e.dataset.value;a.forEach((e=>{e.classList.remove("active"),e.setAttribute("aria-selected","false"),e.setAttribute("tabindex","-1")})),e.classList.add("active"),e.setAttribute("aria-selected","true"),e.setAttribute("tabindex","0"),t(n,r,s)};a.forEach((e=>{e.addEventListener("click",(e=>{const t=e.currentTarget;n(t)})),e.addEventListener("keydown",(t=>{"Enter"!==t.key&&" "!==t.key||(t.preventDefault(),n(e))}))}))}(l,p,d),a(g,p,h,u),t("personal",p,d)):console.error("Failed to find required form elements")}const i=new MutationObserver((e=>{for(const t of e)if("childList"===t.type){const e=document.querySelector("#mi-react-root form");if(e){i.disconnect(),n(e);break}}}));i.observe(document.body,{childList:!0,subtree:!0});const o=document.querySelector("#mi-react-root form");o&&(i.disconnect(),n(o))},R=/^(https?:\/\/)([^/]+)(\/.*)?$/i,C="X-PP-Proxy";function D(t){const{rewritePath:r=/^\/(?!pt).*/i,baseURL:s="",disableSSLValidation:a=!1,miAPI:n}=t;if(!s)throw new Error("Base url is required");const i=s.replace(R,"$2"),o=s.replace(R,"$1$2"),c=import("file-type"),l=S();return e.createProxyMiddleware({selfHandleResponse:!0,pathFilter:(e,s)=>!(t.proxyIgnore||[]).some((t=>"string"==typeof t?e.startsWith(t):"function"!=typeof t.test||t.test(e)))&&("string"==typeof r?e.startsWith(r):Array.isArray(r)?r.some((t=>e.startsWith(t))):"function"==typeof r.test&&r.test(e)),target:s,changeOrigin:!0,autoRewrite:!0,cookieDomainRewrite:{[i]:"localhost"},logger:{info:()=>{},log:()=>{},error:()=>{},warn:()=>{}},secure:!a,headers:{host:i,origin:o},on:{proxyReq(e,t,r){const a=t.headers.host,i=e.getHeader("referer");return l.info(`${E.blue("Proxies request:")} ${E.green(t.method)} ${t.url} -> ${E.green(e.method)} ${e.protocol}//${e.host}${e.path}`),a&&i&&"string"==typeof i&&e.setHeader("referer",i.replace(new RegExp(`https?://${a}`),s)),n.personalAccessToken&&e.setHeader("Authorization",`Bearer ${n.personalAccessToken}`),t.socket.on("close",(()=>{setTimeout((()=>{e.destroyed||e.destroy()}),200)})),e},proxyRes:(t,r,s)=>{if(t.headers["content-type"]?.includes("text/event-stream")||t.headers["transfer-encoding"]?.includes("chunked")&&"no"===t.headers["x-accel-buffering"]){l.info(`${E.blue("Start streaming for request:")} ${E.green(r.method)} ${r.url}`);const e=async(e,t,r)=>{r.setHeader(C,1),r.setHeaders(new Map(Object.entries(e.headers))),e.pipe(r)};return e(t,r,s)}const a=e.responseInterceptor((async(e,t,r,s)=>{s.setHeader(C,1);if(await(await c).fileTypeFromBuffer(e))return e;{let t=e.toString("utf8");try{const e=new URL(r.url??"",`http://${i}`);if(e.searchParams&&e.searchParams.has("proxyRedirect")){const e=function(){const e="pp-dev::redirectCount",t="pp-dev::lastRedirect";let r=+(localStorage.getItem(e)??0);localStorage.getItem(t);Number.isNaN(r)&&(r=0);let s=window.location.href;const a=function(){const n=new URLSearchParams(window.location.search);if(!n.has("proxyRedirect"))return console.debug("No proxyRedirect param. Cleaning up."),void localStorage.removeItem(e);s!==window.location.href?(s=window.location.href,setTimeout(a,r<3?3e3:5e3)):window.location.href=n.get("proxyRedirect"),localStorage.setItem(e,""+ ++r),localStorage.setItem(t,(new Date).toISOString())};setTimeout(a,3e3)};t+=`<script>(${e.toString()})()<\/script>`}e.pathname.startsWith("/login")&&(t+=`<script>const host = "${i}";\n(${L.toString()})()<\/script>`)}catch{}const s=r.headers.host??"";return((e,t,r)=>{const s=new RegExp(`${e.replace(/\\*\//gi,"\\\\/")}`,"gi"),a=new RegExp(`${e}`,"gi"),n=r.replace(s,t);return n===r?n.replace(a,t):n})("/auth/saml/login","/login",T(i,s,t))}}));return a(t,r,s)},error(e,t,r){const s=`Proxy error: "${e.message}" when trying to "${t.method} ${t.url}"\n\n${e.stack}`;l.error(s),r.writable&&r.writeHead(500,{"Content-Type":"text/plain"}),r.end(s)}}})}class O{axios;constructor(e){this.axios=e}async checkAuth(e){return this.axios.get("/data/page/index/auth/info",{headers:Object.assign({},e,{accept:"text/html"}),maxRedirects:0}).then((()=>!0)).catch((()=>!1))}}class F extends O{formdataModulePromise=import("formdata-node");constructor(e){super(e)}getDownloadUrl(e){return`/admin/page/downloadassets/id/${e}`}getDownloadTemplateUrl(e){return`/admin/pagetemplate/downloadassets/id/${e}`}getUploadUrl(e){return`/admin/page/uploadassets/id/${e}`}getUploadTemplateUrl(e){return`/admin/pagetemplate/uploadassets/id/${e}`}async downloadPageAssets(e,t){return this.axios.get(this.getDownloadUrl(e),{withCredentials:!0,headers:Object.assign({},t,{accept:"*/*"}),responseType:"arraybuffer"}).then((e=>e.data))}async uploadPageAssets(e,t,r){const s=new(await this.formdataModulePromise).FormData,{File:a}=await this.formdataModulePromise,n=new a([t],"file.zip",{type:"application/zip"});s.append("file",n);const i=this.getUploadUrl(e);return this.axios.post(i,s,{withCredentials:!0,headers:Object.assign({},r,{accept:"application/json","Content-Type":"multipart/form-data",Referer:this.axios.getUri({url:i})})}).then((e=>e.data))}async downloadTemplateAssets(e,t){return this.axios.get(this.getDownloadTemplateUrl(e),{withCredentials:!0,headers:Object.assign({},t,{accept:"*/*"}),responseType:"arraybuffer"}).then((e=>e.data))}async uploadTemplateAssets(e,t,r){const s=new(await this.formdataModulePromise).FormData,{File:a}=await this.formdataModulePromise,n=new a([t],"file.zip",{type:"application/zip"});s.append("file",n,"file.zip");const i=this.getUploadTemplateUrl(e);return this.axios.post(i,s,{withCredentials:!0,headers:Object.assign({},r,{accept:"application/json","Content-Type":"multipart/form-data",Referer:this.axios.getUri({url:i})})}).then((e=>e.data))}}class N extends F{getDownloadUrl(e){return`/api/page/id/${e}/asset/download`}getDownloadTemplateUrl(e){return`/api/page_template/id/${e}/asset/download`}getUploadUrl(e){return`/api/page/id/${e}/asset/upload`}getUploadTemplateUrl(e){return`/api/page_template/id/${e}/asset/upload`}}class U extends O{async getAll(e){return(await this.axios.get("/api/page",{withCredentials:!0,headers:Object.assign({},e,{Accept:"application/json","Content-Type":"application/json","Cache-Control":"no-cache",Pragma:"no-cache",Expires:"0"})})).data.pages}async get(e,t){return(await this.axios.get(`/api/page/id/${e}`,{withCredentials:!0,headers:Object.assign({},t,{accept:"application/json","content-type":"application/json"})})).data.page}async getPageContent(e,t){return(await this.axios.get(`/p/${e}`,{withCredentials:!0,headers:Object.assign({},t,{accept:"text/html","content-type":"application/json"})})).data}async create(e,t){return(await this.axios.post("/api/page",e,{withCredentials:!0,headers:Object.assign({},t,{accept:"application/json","content-type":"application/json"})})).data.page}}class M extends O{async getAll(e,t){return(await this.axios.get("/api/page_template",{withCredentials:!0,headers:Object.assign({},t,{Accept:"application/json","Content-Type":"application/json","Cache-Control":"no-cache",Pragma:"no-cache",Expires:"0"}),params:{internal_name:e}})).data.page_templates}async get(e,t){return(await this.axios.get(`/api/page_template/id/${e}`,{withCredentials:!0,headers:Object.assign({},t,{accept:"application/json","content-type":"application/json"})})).data.page_template}}function j(e){const t=e.response?.status||0,r=e.response?.data?.message||e.message||"Unknown error";switch(t){case 412:return r.toLowerCase().includes("session expired")?{status:t,message:r,code:"SESSION_EXPIRED",userFriendlyMessage:"Your session has expired",suggestions:["Refresh your token in the portal","Re-authenticate with the portal","Check if your token has been revoked","Ensure your token has the correct permissions"]}:{status:t,message:r,code:"AUTH_FAILED",userFriendlyMessage:"Authentication failed",suggestions:["Verify your token is correct","Check token permissions","Ensure the token hasn't expired","Try generating a new token"]};case 401:return{status:t,message:r,code:"UNAUTHORIZED",userFriendlyMessage:"Unauthorized access",suggestions:["Check if your token is valid","Verify you have the required permissions","Ensure the token hasn't been revoked","Try logging in again"]};case 403:return{status:t,message:r,code:"FORBIDDEN",userFriendlyMessage:"Access forbidden",suggestions:["Check your user permissions","Verify the portal page ID is correct","Ensure your token has the right scope","Contact your administrator"]};default:return{status:t,message:r,code:"UNKNOWN_ERROR",userFriendlyMessage:"An unexpected error occurred",suggestions:["Check your network connection","Verify the portal URL is correct","Try again in a few moments","Check the portal status"]}}}const B="[DEV PAGE. DO NOT DELETE]",H=new Map;class z{#e;#t;#r=null;#s;#a;#n;#i;#o;#c;#l;portalPageId;templateLess;assetsApi;pageApi;pageTemplateApi;logger;constructor(e,t){const{headers:r={},portalPageId:a,templateLess:i=!0,disableSSLValidation:o=!1,v7Features:c=!1,personalAccessToken:l}=t||{};this.#e=r,this.#a=[],this.#s="",this.#n=c,this.#i=l,this.#o=!1,this.#c=Promise.resolve(!1);const p=`${e}:${o}`;H.has(p)?this.#t=H.get(p):(o&&(s.defaults.httpsAgent=new n.Agent({rejectUnauthorized:!1})),this.#t=s.create({baseURL:e,headers:r,timeout:3e4,maxRedirects:5,httpAgent:new n.Agent({keepAlive:!0,maxSockets:10}),httpsAgent:new n.Agent({keepAlive:!0,maxSockets:10})}),H.set(p,this.#t)),this.portalPageId=a,this.templateLess=i,this.assetsApi=new(c?N:F)(this.#t),this.pageApi=new U(this.#t),this.pageTemplateApi=new M(this.#t),this.logger=S()}async isTemplateLoaded(){return this.#c}get isV710OrHigher(){return this.#o}#p(e){const t=Object.assign({},e,{host:void 0,referer:void 0});return Object.keys(t).forEach((e=>void 0===t[e]&&delete t[e])),this.#e=t,!t.authorization&&this.#i&&(t.authorization=`Bearer ${this.#i}`),t}get personalAccessToken(){return this.#i}set personalAccessToken(e){this.#i=e}updateHeaders(e){this.#p(e)}get localTemplateHTML(){return'<!DOCTYPE html>\n <html lang="en">\n <head>\n <meta charset="UTF-8" />\n <title>%%PAGE TITLE%%</title>\n <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>\n <meta name="theme-color" content="rgba(255, 255, 255, 1)"/>\n <link rel="shortcut icon" type="image/x-icon" sizes="any" href="/img/favicon/favicon.ico"/>\n <link rel="icon" type="image/png" sizes="16x16" href="/img/favicon/favicon-16x16.png"/>\n <link rel="icon" type="image/png" sizes="32x32" href="/img/favicon/favicon-32x32.png"/>\n <link rel="icon" type="image/png" sizes="48x48" href="/img/favicon/favicon-48x48.png"/>\n <meta name="msapplication-config" content="/auth/browserconfig.xml"/>\n <link rel="apple-touch-icon" sizes="180x180" href="/img/favicon/apple-touch-icon.png"/>\n <link rel="manifest" href="/auth/site.webmanifest"/>\n <link rel="manifest" href="/auth/manifest.json"/>\n <link rel="stylesheet" type="text/css" href="/auth/theme-vars.css"/>\n <link rel="icon" type="image/svg+xml" href="/favicon.ico" />\n <meta name="viewport" content="width=device-width, initial-scale=1.0" />\n <script src="/js/libs/underscore-latest.min.js" charset="utf-8"><\/script>\n <script src="/js/jquery/jquery-latest.min.js" charset="utf-8"><\/script>\n <script src="/js/application/rating_component.js" charset="utf-8"><\/script>\n </head>\n <body>\n <div id="mi-react-root"></div>\n <script src="/auth/info.js"><\/script>\n <script src="/js/main.js" defer><\/script>\n <link rel="stylesheet" href="/css/main.css" />\n </body>\n </html>'}async getPageTemplate(e){if(await this.#c||this.#r||(this.#c=new Promise((e=>{this.#l=e}))),this.#r)return Promise.resolve(this.#r);if(void 0===this.templateLess&&(this.templateLess=!1),this.#n){const t=await this.pageApi.get(this.portalPageId,this.#p(e)).then((e=>(this.logger.info(E.green("Page fetched")),e))).catch((async t=>{throw this.#l(!1),await this.pageApi.checkAuth(this.#p(e))?(this.logger.error(E.red(`Error fetching page data: ${t.message}\n${t.stack}`)),new Error("The current user does not have access to this page. Check your configuration to ensure the portalPageId is correct.")):t}));return void 0!==t.template_id&&(this.#o=!0),this.#r=this.localTemplateHTML,this.#r=this.#r.replace(/%%PAGE TITLE%%/g,t.name||"Local template"),this.logger.info(E.green("Local page template fetched")),this.#l(!0),this.#r}let t=(await this.pageApi.getAll(this.#p(e)).then((e=>(this.logger.info(E.green("Page list fetched")),e))).catch((async t=>{throw this.#l(!1),await this.pageApi.checkAuth(this.#p(e))?(this.logger.error(E.red(`Error fetching page list: ${t.message}\n${t.stack}`)),new Error("Current user does not have access to page list")):t}))).find((e=>e.name===B));return t||(this.logger.warn(E.yellow("Creating dev page template...")),t=await this.pageApi.create({enabled:"Y",name:B,internal_name:"dev-page-template",visible_in_homepage:"Y"},this.#p(e)).then((e=>(this.logger.info(E.green("Dev page created")),e))).catch((e=>{throw this.logger.error(E.red(`Error creating dev page: ${e.message}`)),this.#l(!1),new Error(`Error when creating dev page.\n That can be caused by missing permissions or page with name "${B}" already exists`)}))),await this.pageApi.getPageContent(t.internal_name,this.#p(e)).then((e=>(this.#r=e,e))).then((e=>(this.#l(!0),this.logger.info(E.green("Page template fetched")),e))).catch((e=>{throw this.#l(!1),this.logger.error(E.red(`Error fetching page template: ${e.message}`)),new Error("Error fetching page template")}))}async getPageVariables(e,t){return this.#r=await this.getPageTemplate(t),this.portalPageId||(this.portalPageId=e,this.templateLess=!1),await this.pageApi.get(e,this.#p(t)).then((e=>{const{tags:t="[]",name:r,template:s}=e;if(s&&t){const e=JSON.parse(t);return this.#a=e,this.#s=r,e}return this.#a=[],this.#s=r,[]})).catch((t=>{if(this.logger.error(E.red(`Error fetching page variables: ${t.message}`)),404===t.response?.status)throw new Error(`Portal Page with id "${e}" not found on instance ${this.#t.getUri()}`);if(401===t.response?.status)throw new Error(`Current user does not have access to page with id "${e}" on instance ${this.#t.getUri()}`);throw t}))}async getPageInfo(e,t){this.portalPageId||(this.portalPageId=e);const r=this.#p(t),s=this.portalPageId;try{return this.#n?await this.#d(s,r):await this.#h(s,r)}catch(e){this.#u(e,s)}}async#d(e,t){const r=await this.pageApi.get(e,t);return this.logger.info(E.green("Page fetched")),void 0!==r.template_id&&(this.#o=!0),r}async#h(e,t){return await this.pageApi.get(e,t)}#u(e,t){if(this.#n&&e.message?.includes("access"))throw new Error("The current user does not have access to this page. Check your configuration to ensure the portalPageId is correct.");if(404===e.response?.status)throw new Error(`Portal Page with id "${t}" not found on instance ${this.#t.getUri()}`);throw 401===e.response?.status&&this.logger.error(E.red(`Current user does not have access to page with id "${t}" on instance ${this.#t.getUri()}`)),e}buildPage(e,t=!1){let r="string"==typeof e?e:e.toString("utf-8");for(const e of this.#a)r=r.replace(new RegExp(`\\[${e.name}\\]`,"g"),e.value);const s=new a.JSDOM(t?r:this.#r),n="%%PLACEHOLDER%%";if(!t){const e=s.window.document.createElement("div");e.innerHTML=n;const t=s.window.document.querySelector(".main-side");if(t){const r=t.querySelectorAll("script");r.length?t.insertBefore(e,r.item(r.length-1)):t.append(e)}else{const t=s.window.document.createElement("div");t.append(e),s.window.document.body.append(t)}const r=s.window.document.querySelector("head"),a=r.querySelector("title");a?a.text=this.#s:r.innerHTML+=`<title>${this.#s}</title>`}return s.serialize().replace(new RegExp(`<div>\\s*${n}\\s*<\\/div>`,"i"),r)}async getAssets(){if(this.portalPageId){if(this.templateLess)return await this.assetsApi.downloadPageAssets(this.portalPageId,this.#e);{const e=await this.pageApi.get(this.portalPageId,this.#e);if(this.#o){const t=await this.pageTemplateApi.get(e.template_id,this.#e);return await this.assetsApi.downloadTemplateAssets(t.id,this.#e)}if(e.template)return await this.assetsApi.downloadTemplateAssets(e.template,this.#e)}}}async updateAssets(e){if(this.portalPageId){if(this.templateLess)return await this.assetsApi.uploadPageAssets(this.portalPageId,e,this.#e);{const t=await this.pageApi.get(this.portalPageId,this.#e);if(this.#o){const r=await this.pageTemplateApi.get(t.template_id,this.#e);return await this.assetsApi.uploadTemplateAssets(r.id,e,this.#e)}if(t.template)return await this.assetsApi.uploadTemplateAssets(t.template,e,this.#e)}}}async validateCredentials(e){try{const t=e||this.#e;return await this.get("/api/user",t,!0),{isValid:!0}}catch(e){const t=j(e);return{isValid:!1,error:t.userFriendlyMessage,code:t.code}}}async get(e,t,r=!1){const s=r?t:Object.assign({},this.#p(this.#e),t);try{return await this.#t.get(e,{headers:s})}catch(e){if("UNKNOWN_ERROR"!==j(e).code){const t=j(e);throw this.logger.error(E.red(`API request failed: ${t.userFriendlyMessage}`)),function(e,t,r){const s=j(t),a=`[${r}] `;e.error(E.red(`${a}${s.userFriendlyMessage}`)),e.error(E.red(`Status: ${s.status} (${s.code})`)),e.error(E.red(`Details: ${s.message}`)),s.suggestions.length>0&&(e.info(E.yellow("Suggestions:")),s.suggestions.forEach(((t,r)=>{e.info(E.yellow(` ${r+1}. ${t}`))})))}(this.logger,e,"API Request"),e.response||(e.response={status:t.status,data:{message:t.message}}),e.tokenErrorInfo=t,e}throw e}}}class V{server;opts;eventMap;logger;constructor(e,t){this.server=e,this.opts=t||{},this.eventMap=new Map,this.eventMap.set("info-data:request",this.onInfoDataRequest.bind(this)),this.eventMap.set("template:sync",this.onTemplateSync.bind(this)),this.logger=S(),this.init()}init(){const{ws:e}=this.server;for(const[t,r]of this.eventMap)e.on(t,r)}onInfoDataRequest(){this.server.ws.send({type:"custom",event:"info-data:response",data:{}})}async onTemplateSync(){if(!this.opts.distService||!this.opts.miAPI)return this.server.ws.send("template:sync:response",{error:"Dist service or MiAPI is not defined"}),void this.logger.error(E.red("Dist service or MiAPI is not defined"));{const{distService:e,miAPI:t}=this.opts;if(this.server.config.clientInjectionPlugin?.v7Features){if(!t?.isV710OrHigher)return void this.server.ws.send("template:sync:response",{error:"This feature is available only for MI v7.1.0 or higher",config:{canSync:!1}});this.server.ws.send("client:config:update",{config:{canSync:!0}})}const r=await(t?.getAssets().catch((async e=>{if(s.isAxiosError(e)){if("SESSION_EXPIRED"===j(e).code){this.logger.info(E.yellow("Session expired - attempting to validate credentials"));try{const e=await(t?.validateCredentials());e&&!e.isValid?(this.logger.error(E.red(`Authentication error: ${e.error}`)),this.server.ws.send("template:sync:response",{error:e.error,code:e.code,refresh:!0})):(this.logger.info(E.yellow("Session expired")),this.server.ws.send("template:sync:response",{error:"Session expired",code:"SESSION_EXPIRED",refresh:!0}))}catch(e){this.logger.info(E.yellow("Session expired")),this.server.ws.send("template:sync:response",{error:"Session expired",code:"SESSION_EXPIRED",refresh:!0})}return e}if(e.cause instanceof Error&&("ECONNRESET"===e.cause.code||"ENOTFOUND"===e.cause.code))return this.logger.info(E.yellow("Server in maintenance mode, VPN connection is needed or no internet connection")),this.server.ws.send("template:sync:response",{error:"Server in maintenance mode, VPN connection is needed or no internet connection",code:"CONNECTION_ERROR"}),e}throw e})));if(r instanceof Error)return;const a=r?await(e?.saveBackupAndBuild(r).catch((e=>{if("Backup file is not a ZIP file"===e.message)return this.logger.error(E.red("Backup file is not a ZIP file")),e}))):await(e?.buildNewAssets());if(!(a&&a instanceof Buffer))return a instanceof Error?(this.server.ws.send("template:sync:response",{error:a.message}),void this.logger.error(E.red(a.message))):(this.server.ws.send("template:sync:response",{error:"Failed to build new assets"}),void this.logger.error(E.red("Failed to build new assets")));{const r=await(t?.updateAssets(a));if("OK"===r?.status){const t=e?.getBackupMeta(),{lastBackupName:r,lastBackupHash:s,lastBackupDate:a}=t||{lastBackupName:"",lastBackupHash:"",lastBackupDate:(new Date).toISOString()};this.server.ws.send("template:sync:response",{syncedAt:new Date(a),currentHash:s,backupFilename:r}),this.logger.info(E.green("Template synced"))}else this.server.ws.send("template:sync:response",{error:"Failed to update assets"}),this.logger.error(E.red("Failed to update assets"))}}}}const _=/\.(?:[a-z0-9]+)$/i,q=6e5,Z=104857600,W=1e3;class X extends y.Cache{totalSize=0;maxSize;maxItems;constructor(e,t){super(),this.maxSize=e,this.maxItems=t}put(e,t,r){const s=this.get(e);s&&(this.totalSize-=s.size);const a=super.put(e,t,r);return this.totalSize+=t.size,this.cleanup(),a}cleanup(){const e=this.keys();if(e.length>this.maxItems){const t=e.length-this.maxItems,r=e.sort(((e,t)=>{const r=this.get(e),s=this.get(t);return(r?.timestamp||0)-(s?.timestamp||0)}));for(let e=0;e<t;e++){const t=r[e],s=this.get(t);s&&(this.totalSize-=s.size,this.del(t))}}for(;this.totalSize>this.maxSize&&e.length>0;){const t=e.sort(((e,t)=>{const r=this.get(e),s=this.get(t);return(r?.timestamp||0)-(s?.timestamp||0)}))[0];if(t){const e=this.get(t);e&&(this.totalSize-=e.size,this.del(t))}}}getTotalSize(){return this.totalSize}getItemCount(){return this.keys().length}}const Y=new X(Z,W);function K(e){if(0===e.length)return Buffer.alloc(0);if(1===e.length)return e[0];const t=e.reduce(((e,t)=>e+t.length),0),r=Buffer.allocUnsafe(t);let s=0;for(const t of e)t.copy(r,s),s+=t.length;return r}function J(e){const{devServer:t,ttl:r=q,maxSize:s=Z,maxItems:a=W}=e,n=S();return s===Z&&a===W||(Y.maxSize=s,Y.maxItems=a),t.cache=Y,(e,t,s)=>{const a=e.originalUrl||e.url||"",i=function(e){const t=e.split("?")[0];return _.test(t)?t.includes("/auth/info.js")?"":e:""}(a);if(!i)return s();const o=Y.get(i);if(o)return n.info(`${E.blue("Proxies request:")} ${E.green(e.method)} ${a} -> ${E.blue("Cache")} ${i}`),function(e,t){try{for(const[r,s]of Object.entries(t))null!=s&&e.setHeader(r,s)}catch(e){console.warn("Failed to set some response headers:",e)}}(t,o.headers),t.write(o.content),void t.end();const c=t.end,l=t.write,p=[];t.write=function(e,r,s){if(!t.hasHeader(C))return l.call(this,e,r,s);if("string"==typeof e){const t=r||"utf8";p.push(Buffer.from(e,t))}else Buffer.isBuffer(e)?p.push(e):null!=e&&p.push(Buffer.from(String(e),"utf8"));return!0},t.end=function(e,s,a){if(!t.hasHeader(C))return c.call(this,e,s,a);if(null!=e)if("string"==typeof e){const t=s||"utf8";p.push(Buffer.from(e,t))}else Buffer.isBuffer(e)?p.push(e):p.push(Buffer.from(String(e),"utf8"));let o;try{o=K(p);const e=o.length;if(e>0&&e<10485760){const s={headers:t.getHeaders(),content:o,timestamp:Date.now(),size:e};Y.put(i,s,r),n.info(`${E.blue("[Cached]")} ${i} (${(e/1024).toFixed(1)}KB)`)}}catch(e){n.error(`Failed to cache response for ${i}: ${e}`),o=K(p)}const l=s||"utf8";return c.call(this,o,l,a)},s()}}const G="pageName",Q="date",ee=v.dirname("undefined"!=typeof __filename&&__filename||l.fileURLToPath("undefined"==typeof document?require("url").pathToFileURL(__filename).href:m&&"SCRIPT"===m.tagName.toUpperCase()&&m.src||new URL("plugin-MI3iLFaG.js",document.baseURI).href)),te=v.resolve(ee,"..",".."),re=v.resolve(te,"..",".."),se=v.resolve(re,".pp-dev-meta"),ae=v.resolve(se,"sync-service.meta.json");class ne{backupFolder;backupNameTemplate;dateFormat;pageName;currentMeta=null;distZipFolder;distZipFilename;logger;constructor(e,t){this.pageName=e;const{backupFolder:r=v.resolve(k.cwd(),"backups"),distZipFolder:s=v.resolve(k.cwd(),"dist-zip"),distZipFilename:a=`${this.pageName}.zip`,backupNameTemplate:n=`{${G}}-{${Q}}.zip`,dateFormat:i=e=>e.toISOString().replace(/:/g,"-").replace(/\..*$/,"")}=t||{};this.backupFolder=r,this.backupNameTemplate=n,this.dateFormat=i,this.distZipFolder=s,this.distZipFilename=a,this.syncMeta(),this.logger=S()}async checkMeta(){try{await c.promises.stat(se)}catch{await c.promises.mkdir(se)}try{await c.promises.stat(this.backupFolder)}catch{await c.promises.mkdir(this.backupFolder)}}async readMetaFile(){return await this.checkMeta(),await c.promises.readFile(ae,{encoding:"utf-8"}).catch((()=>"{}"))}async writeMetaFile(e){return await this.checkMeta(),await c.promises.writeFile(ae,JSON.stringify(e,null,2),{encoding:"utf-8"})}async syncMeta(){this.currentMeta?await this.writeMetaFile(this.currentMeta):this.currentMeta=JSON.parse(await this.readMetaFile())}async getLatestSavedBackup(){const{lastBackupName:e}=this.currentMeta;if(!e)return null;const t=v.resolve(this.backupFolder,e);try{return await c.promises.stat(t),t}catch{return null}}backupName(e,t=new Date){return this.backupNameTemplate.replace(`{${G}}`,e).replace(`{${Q}}`,this.dateFormat(t))}getBackupMeta(){return this.currentMeta}async saveBackup(e){if("PK"!==e.toString("utf-8").slice(0,4))throw new Error("Backup file is not a ZIP file");const t=b.createHash("md5").update(e).digest("hex");if(await this.getLatestSavedBackup()){const{lastBackupHash:e}=this.currentMeta;if(e===t)return}const r=new Date,s=this.backupName(this.pageName,r);return this.currentMeta.lastBackupName=s,this.currentMeta.lastBackupHash=t,this.currentMeta.lastBackupDate=r.toISOString(),await this.syncMeta(),await c.promises.writeFile(v.resolve(this.backupFolder,s),e).finally((()=>{this.logger.info(`Backup saved to ${s}`)}))}async buildNewAssets(){const e=new Promise(((e,t)=>{let r="";this.logger.info(E.cyan("[DistService] Build started"));const s=x.spawn("node",[v.resolve(te,"./bin/pp-dev.js"),"build"],{cwd:k.cwd(),env:Object.assign({},k.env,{NODE_ENV:"production"}),stdio:"inherit"});s.on("message",(e=>{r+=e})),s.on("close",(s=>{0===s?e(r):t(new Error(`build command exited with code ${s}`))})),s.on("error",(e=>{t(e)}))}));try{await e.finally((()=>{this.logger.info(E.cyan("[DistService] Build finished"))}));const t=v.resolve(k.cwd(),this.distZipFolder,this.distZipFilename);if(!await c.promises.stat(t))throw new Error(`File ${t} not found`);return await c.promises.readFile(t)}catch(e){throw P.log(e),e}}async saveBackupAndBuild(e){return await this.saveBackup(e),await this.buildNewAssets()}}function ie(e,t){return async(r,s,a)=>{if(await e(r.url??"",r,s)){S().info(`Rewrite response for ${r.url}`);const e=s.end,a=[];s.write=function(e){return a.push(e),!0},s.end=function(n,i,o){"string"==typeof n&&a.push(Buffer.from(n));const c=s.getHeader("content-encoding"),l=function(e,t){switch(t){case"gzip":return g.unzipSync(e);case"br":return g.brotliDecompressSync(e);case"deflate":return g.deflateSync(e);default:return e}}(Buffer.from(Buffer.concat(a)),c),p="function"!=typeof i&&i?i:"utf-8",d="function"==typeof i?i:o;return e.call(this,function(e,t){switch(t){case"gzip":return g.gzipSync(e);case"br":return g.brotliCompressSync(e);case"deflate":return g.inflateSync(e);default:return e}}(t(l,r,s),c),p,d),this}}a()}}function oe(e,t){const r=(e=(e=e.startsWith("/")?e:`/${e}`).endsWith("/")?e:`${e}/`).endsWith("/")?e.slice(0,-1):e,s=S();return(a,n,i)=>{const o=new l.URL(a.url??"","http://localhost"),{pathname:c,search:p}=o,d=["/",r];if(t&&(d.push(`/pt/${t}`),d.push(`/pl/${t}`)),d.includes(c)){const t=`${e}${p}`;return s.info(E.yellow(`Redirecting to: ${t}`)),A(n,t,302)}i()}}class ce{state={isAuthenticated:!1,isRedirected:!1,lastChecked:0};listeners=new Set;getState(){return{...this.state}}isAuthenticated(){return this.state.isAuthenticated}isRedirected(){return this.state.isRedirected}setAuthenticated(e){this.state.isAuthenticated=e,this.state.lastChecked=Date.now(),this.notifyListeners()}setRedirected(e){this.state.isRedirected=e,this.notifyListeners()}updateState(e){void 0!==e.isAuthenticated&&(this.state.isAuthenticated=e.isAuthenticated),void 0!==e.isRedirected&&(this.state.isRedirected=e.isRedirected),this.state.lastChecked=Date.now(),this.notifyListeners()}reset(){this.state={isAuthenticated:!1,isRedirected:!1,lastChecked:0},this.notifyListeners()}subscribe(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}notifyListeners(){this.listeners.forEach((e=>{try{e(this.getState())}catch(e){console.error("Error in auth state listener:",e)}}))}getDebugInfo(){return{...this.getState(),listenerCount:this.listeners.size}}}const le=new ce,pe=new Map;function de(e){const t=pe.get(e);return t?Date.now()-t.timestamp>18e4?(pe.delete(e),null):t.data:null}function he(e,t){pe.set(e,{timestamp:Date.now(),data:t})}const ue="/home?proxyRedirect=";function ge(e,t,r){const{templateLess:s=!1,miHudLess:a=!1,portalPageId:n,base:i,v7Features:o}=r,c=S();if(s&&a&&void 0===n)throw new Error("Portal page ID is required when both templateLess and miHudLess are true");let l=le.getState();return le.subscribe((e=>{l=e})),async(r,p,d)=>{try{const h=!(s&&a),u=e.test(function(e){return e.split("?")[0]}(r.url??""));if(!l.isAuthenticated&&!l.isRedirected&&r.url?.startsWith("/home")&&!r.url?.startsWith(ue)){c.info(E.blue("Trying to authenticate and redirect to base"));try{return h?await me(t,{templateLess:s,miHudLess:a,portalPageId:n,redirectUrl:o&&i?`${i}`:`${ue}${encodeURIComponent("/")}`},r,p,(()=>{}),c):await fe(t,{templateLess:s,miHudLess:a,portalPageId:n,redirectUrl:o&&i?`${i}`:`${ue}${encodeURIComponent("/")}`},r,p,(()=>{}),c),le.setRedirected(!0),c.info(E.blue("Successfully authenticated. Redirecting to base")),A(p,i??"/",302)}catch(e){return d()}}if(!u)return d();if(!h){return await fe(t,{templateLess:s,miHudLess:a,portalPageId:n,redirectUrl:o&&i?`${i}`:`${ue}${encodeURIComponent("/")}`},r,p,d,c)}return await me(t,{templateLess:s,miHudLess:a,portalPageId:n,redirectUrl:o&&i?`${i}`:`${ue}${encodeURIComponent("/")}`},r,p,d,c)}catch(e){return c.error(E.red(`Unexpected error in load-pp-data middleware: ${e instanceof Error?e.message:String(e)}`)),le.reset(),d(e)}}}async function fe(e,t,r,s,a,n){const{portalPageId:i,redirectOnAuthFailure:o,redirectUrl:c}=t;if(void 0===i){const e=new Error("Portal page ID is required for page info only mode");return n.error(E.red(e.message)),a(e)}const l=r.headers??{};n.info(E.green("Start loading page info"));try{const t=`pageInfo:${i}`;return de(t)?(n.info(E.green("Page info loaded from cache")),a()):(await e.getPageInfo(i,l),n.info(E.blue("Clearing proxy cache after successful login")),Y.clear(),he(t,{success:!0}),le.updateState({isAuthenticated:!0}),n.info(E.green("Page info loaded successfully")),a())}catch(e){return le.reset(),we(e,o??!0,c||`${ue}${encodeURIComponent("/")}`,s,a,n,"Page info")}}async function me(e,t,r,s,a,n){const{templateLess:i,portalPageId:o,redirectOnAuthFailure:c,redirectUrl:l}=t,p=r.headers??{};n.info(E.green("Start loading page data"));try{const t=`pageData:${i}:${o}`;if(de(t))return n.info(E.green("Page data loaded from cache")),a();const r=i||void 0===o?e.getPageTemplate(p):e.getPageVariables(o,p);return await r,n.info(E.blue("Clearing proxy cache after successful login")),Y.clear(),he(t,{success:!0}),n.info(E.green("Page data loaded successfully")),a()}catch(e){return le.reset(),we(e,c??!0,l||`${ue}${encodeURIComponent("/")}`,s,a,n,"Page data")}}function we(e,t,r,s,a,n,i){if(function(e){return null!==e&&"object"==typeof e&&"response"in e&&void 0!==e.response}(e)&&(n.info(E.red(`${i} loading failed. Not authorized`)),t)){const e=`${r}`;return n.info(E.yellow(`Redirecting to: ${e}`)),A(s,e,302)}const o=e instanceof Error?e.message:String(e);return n.info(E.red(`${i} loading failed. Error: ${o}`)),a(e)}const ye=f();function ve(e,t,r){return t(e)?{isValid:!0}:{isValid:!1,error:r}}ye.use(f.json()),ye.use(f.urlencoded({extended:!0})),exports.AuthProvider=ce,exports.MiAPI=z,exports.authProvider=le,exports.colors=E,exports.createLogger=S,exports.initLoadPPData=ge,exports.initPPRedirect=oe,exports.initProxy=D,exports.initProxyCache=J,exports.initRewriteResponse=ie,exports.internalServer=ye,exports.normalizeVitePPDevConfig=function(e){const t=function(e){const t=[ve(e,(e=>"object"==typeof e&&null!==e),"VitePPDevOptions must be an object"),ve(e.templateName,(e=>"string"==typeof e&&e.length>0),"VitePPDevOptions.templateName must be a non-empty string"),ve(e.backendBaseURL,(e=>void 0===e||"string"==typeof e&&e.length>0),"VitePPDevOptions.backendBaseURL must be a non-empty string if provided"),ve(e.portalPageId,(e=>void 0===e||"number"==typeof e&&e>0),"VitePPDevOptions.portalPageId must be a positive number if provided"),ve(e.appId,(e=>void 0===e||"number"==typeof e&&e>0),"VitePPDevOptions.appId must be a positive number if provided"),ve(e.proxyCacheTTL,(e=>void 0===e||"number"==typeof e&&e>0),"VitePPDevOptions.proxyCacheTTL must be a positive number if provided"),ve(e.integrateMiTopBar,(t=>void 0===t||"boolean"==typeof t&&(!0!==t||!0===e.miHudLess)),"VitePPDevOptions.integrateMiTopBar can only be true when miHudLess is true")];return t.find((e=>!e.isValid))||{isValid:!0}}(e);if(!t.isValid)throw new Error(t.error);const r=e.appId??e.portalPageId,{enableProxyCache:s=!0,proxyCacheTTL:a=6e5,disableSSLValidation:n=!1,imageOptimizer:i=!0,miHudLess:o=!1,integrateMiTopBar:c=!1,templateLess:l=!1,outDir:p="dist",distZip:d=!0,syncBackupsDir:h="backups",v7Features:u=!1,personalAccessToken:g=process.env.MI_ACCESS_TOKEN}=e||{};let f=d;f=!0===f?{outFileName:`${e.templateName}.zip`,outDir:"dist-zip"}:"object"==typeof d&&{outFileName:"string"==typeof d.outFileName?d.outFileName.replace("[templateName]",e.templateName):`${e.templateName}.zip`,outDir:d.outDir??"dist-zip"};let m=i;return"boolean"==typeof i?!0===m&&(m={}):"object"!=typeof i&&(m=!1),{enableProxyCache:s,proxyCacheTTL:a,disableSSLValidation:n,imageOptimizer:m,templateLess:l,miHudLess:o,outDir:p,distZip:f,syncBackupsDir:h,v7Features:u,personalAccessToken:g,portalPageId:r,integrateMiTopBar:c,...e}},exports.urlReplacer=T,exports.vitePPDev=function(e){const{templateName:t,templateLess:r,backendBaseURL:s,miHudLess:a,portalPageId:n,enableProxyCache:i,proxyCacheTTL:o,disableSSLValidation:c,distZip:l,syncBackupsDir:p,v7Features:d,personalAccessToken:h}=e||{};let u=!0;return process.cwd(),{name:"vite-pp-dev",apply:"serve",config:e=>{const a=function(e){return[ve(e.base,(e=>"string"==typeof e&&e.length>0&&"/"!==e),'Server base path cannot be empty or "/"'),ve(e.port,(e=>void 0===e||"number"==typeof e&&e>0&&e<65536),"Server port must be a valid port number (1-65535)")].find((e=>!e.isValid))||{isValid:!0}}({base:e.base||"",...e.server});if(!a.isValid)throw new Error(a.error);return e.clientInjectionPlugin={backendBaseURL:s,portalPageId:n,templateLess:r,v7Features:d},d&&(e.base=`/pl/${t}`),e},transformIndexHtml:async(e,t)=>{const r={html:e,tags:[]};return u&&(u=!1,r.tags.push({tag:"script",injectTo:"body",children:`${Math.random()}`})),r},configureServer:u=>{let g=u.config.base;g.endsWith("/")||(g+="/");const f=g.substring(0,g.lastIndexOf("/"));if(u.middlewares.use(oe(g,t)),s){const w=new URL(s).host,y={headers:{host:w,referer:s,origin:s.replace(/^(https?:\/\/)([^/]+)(\/.*)?$/i,"$1$2")},portalPageId:n,appId:n,templateLess:r,disableSSLValidation:c,v7Features:d,personalAccessToken:h??process.env.MI_ACCESS_TOKEN},v=[ve((m=y).headers,(e=>"object"==typeof e&&"string"==typeof e.host&&"string"==typeof e.referer&&"string"==typeof e.origin),"MiAPI headers must be properly configured"),ve(m.portalPageId,(e=>void 0===e||"number"==typeof e&&e>0),"MiAPI portalPageId must be a positive number if provided"),ve(m.appId,(e=>void 0===e||"number"==typeof e&&e>0),"MiAPI appId must be a positive number if provided"),ve(m.personalAccessToken,(e=>void 0===e||"string"==typeof e),"MiAPI personalAccessToken must be a string if provided")].find((e=>!e.isValid))||{isValid:!0};if(!v.isValid)throw new Error(v.error);const b=new z(s,y);if(i){let e=+o;(!e||Number.isNaN(e)||e<0)&&(e=6e5);const t={devServer:u,ttl:e},r=function(e){return[ve(e.ttl,(e=>"number"==typeof e&&e>0),"Proxy cache TTL must be a positive number")].find((e=>!e.isValid))||{isValid:!0}}(t);if(!r.isValid)throw new Error(r.error);u.middlewares.use(J(t))}const k=new RegExp(`^((${g})|/)$`);u.middlewares.use(ge(k,b,Object.assign({},e))),u.middlewares.use(D({baseURL:s,proxyIgnore:["/@vite","/@metricinsights","/@",f],disableSSLValidation:c,miAPI:b}));const x=(e,t,r,s,a)=>{const n={error:r};s&&(n.details=s),a&&(n.code=a),e.status(t).json(n).end()},P=(e,t,r)=>{u.config.logger.error(`${r} token validation error:`,t);const s=j(t);if(t.tokenErrorInfo){const r=t.tokenErrorInfo;u.config.logger.info(`Using enhanced error info: ${r.code} - ${r.userFriendlyMessage}`);const s=r.status||500,a=r.code,n=r.userFriendlyMessage,i=r.message;x(e,s,n,i,a)}else{let t,a,n;switch(u.config.logger.info(`Using original error info: ${s.code} - ${s.userFriendlyMessage}`),s.code){case"SESSION_EXPIRED":t=412,a="personal"===r?"Personal access token expired":"Session expired",n="personal"===r?"PAT_EXPIRED":"SESSION_EXPIRED";break;case"UNAUTHORIZED":t=401,a="Unauthorized",n="UNAUTHORIZED";break;case"FORBIDDEN":t=403,a="Access forbidden",n="FORBIDDEN";break;default:t=500,a="Internal server error",n=s.code}const i="personal"===r&&"PAT_EXPIRED"===n?"Your personal access token has expired. Please generate a new token from the portal.":"regular"===r&&"SESSION_EXPIRED"===n?"Your portal session has expired. Please refresh your token or re-authenticate.":s.userFriendlyMessage;x(e,t,a,i,n)}return null};ye.post("/@api/login",(async(e,t,r)=>{const{token:s,tokenType:a}=e.body;if(s){if(u.config.logger.info(`Attempting to validate ${a} token...`),"personal"===a){if(!await b.get("/data/page/index/auth/info",{"Content-Type":"application/json",Accept:"application/json",Authorization:`Bearer ${s}`},!0).then((async e=>"number"==typeof e.data?.user?.user_id?(b.personalAccessToken=s,u.config.logger.info(`Personal access token validated successfully for user ${e.data.user.user_id}`),e):(x(t,400,"Token expired or invalid"),null))).catch((e=>P(t,e,"personal"))))return;A(t,"/",302)}else if("regular"===a){if(!await b.get("/api/user",{"Content-Type":"application/json",Accept:"application/json",Token:s},!0).then((e=>e.data?.users?.length?(b.personalAccessToken=void 0,u.config.logger.info(`Regular token validated successfully for ${e.data.users.length} user(s)`),t.setHeader("set-cookie",e.headers["set-cookie"]??""),e):(x(t,400,"Token expired or invalid"),null))).catch((e=>P(t,e,"regular"))))return;A(t,"/",302)}}else x(t,400,"Token is required")})),u.middlewares.use(ye);const $=!1!==l?new ne(t,Object.assign({backupDir:p},"object"==typeof l?{distZipFolder:l.outDir,distZipFilename:l.outFileName}:void 0)):void 0;return new V(u,{distService:$,miAPI:b}),()=>{u.middlewares.use(ie((e=>e.split("?")[0].endsWith("index.html")),((e,t)=>Buffer.from(T(w,t.headers.host??"",b.buildPage(e,a))))))}}var m}}};
//# sourceMappingURL=plugin-MI3iLFaG.js.map