@smart-cloud/publisher-exporter 1.1.38 → 1.1.40

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/deploy.js CHANGED
@@ -1,6 +1,6 @@
1
- import {S3Client,HeadObjectCommand,GetObjectCommand,DeleteObjectsCommand,ListObjectsV2Command}from'@aws-sdk/client-s3';import {Upload}from'@aws-sdk/lib-storage';import _t,{createHash}from'crypto';import Wt from'fast-glob';import x from'fs/promises';import b from'path';import fr from'mime-types';import gt from'http';import pt from'https';import {availableParallelism}from'os';import {Worker}from'worker_threads';function Te(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function dt(e){let t=String(e??"").trim();if(!t)return "";let r=t;if(/^https?:\/\//i.test(r))try{r=new URL(r).pathname;}catch{return ""}if(r=r.split(/[?#]/,1)[0]?.replace(/\\/g,"/").trim()??"",!r)return "";let i=r.endsWith("/"),n=r.replace(/^\/+/,"").split("/").map(o=>o.replace(/[^A-Za-z0-9._-]/g,"")).filter(o=>o.length>0&&o!=="."&&o!=="..");if(n.length===0)return "";let s=`/${n.join("/")}`;return i&&b.extname(s)===""&&(s+="/"),s==="/"?"":s}function ue(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,r])=>[t.trim(),String(r??"")]).filter(([t])=>t.length>0))}function ft(e){if(!e||typeof e!="object")return {};let t={};for(let[r,i]of Object.entries(e)){let n=r.trim();if(!n||!i||typeof i!="object")continue;let s=i,o={};if(typeof s.targetOrigin=="string"){let a=Te(s.targetOrigin);a&&(o.targetOrigin=a);}let l=ue(s.extraReplacements);if(Object.keys(l).length>0&&(o.extraReplacements=l),s.s3&&typeof s.s3=="object"){let a={},u=s.s3;typeof u.bucket=="string"&&(a.bucket=u.bucket.trim()),typeof u.prefix=="string"&&(a.prefix=u.prefix.trim()),typeof u.region=="string"&&(a.region=u.region.trim()),typeof u.htmlCacheControl=="string"&&(a.htmlCacheControl=u.htmlCacheControl.trim()),typeof u.assetCacheControl=="string"&&(a.assetCacheControl=u.assetCacheControl.trim()),Object.keys(a).length>0&&(o.s3=a);}if(s.cloudFront&&typeof s.cloudFront=="object"){let a={},u=s.cloudFront;typeof u.distributionId=="string"&&(a.distributionId=u.distributionId.trim()),Array.isArray(u.invalidationPaths)&&(a.invalidationPaths=u.invalidationPaths.map(c=>String(c??"").trim()).filter(c=>c.length>0)),Object.keys(a).length>0&&(o.cloudFront=a);}t[n]=o;}return t}function ge(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function ht(e,t){if(!e||typeof e!="object")return null;let r=e,i=r.command;if(i!=="publish"&&i!=="crawl"&&i!=="deploy"&&i!=="invalidate"&&i!=="retry-timeouts"&&i!=="url")return null;let n=Number.parseInt(String(r.intervalMinutes??"0"),10);if(!Number.isFinite(n)||n<1)return null;let s=String(r.id??`${i}-${t+1}`).trim();if(!s)return null;let o=String(r.deploymentProfile??"").trim(),l=String(r.url??"").trim(),a=Array.isArray(r.postTypes)?[...new Set(r.postTypes.map(c=>String(c??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],u=Array.isArray(r.listingPaths)?[...new Set(r.listingPaths.map(c=>String(c??"").trim()).filter(Boolean))]:[];return {id:s,enabled:r.enabled!==false,command:i,intervalMinutes:n,...i==="publish"||i==="crawl"?{crawlMode:r.crawlMode==="incremental"?"incremental":"full"}:{},...(i==="publish"||i==="deploy"||i==="invalidate")&&o?{deploymentProfile:o}:{},...l?{url:l}:{},...a.length>0?{postTypes:a}:{},...u.length>0?{listingPaths:u}:{},...typeof r.includePostTypeArchives=="boolean"?{includePostTypeArchives:r.includePostTypeArchives}:{},...typeof r.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:r.includeTaxonomyArchives}:{},...typeof r.includeAuthorArchives=="boolean"?{includeAuthorArchives:r.includeAuthorArchives}:{},...typeof r.includeDateArchives=="boolean"?{includeDateArchives:r.includeDateArchives}:{},...typeof r.includePostsPage=="boolean"?{includePostsPage:r.includePostsPage}:{},...typeof r.includeSitemapChain=="boolean"?{includeSitemapChain:r.includeSitemapChain}:{}}}function Ce(e){let t=e&&typeof e=="object"?e:{},r=Array.isArray(t.rules)?t.rules.map((i,n)=>ht(i,n)).filter(i=>!!i):[];return {enabled:t.enabled===true,timezone:typeof t.timezone=="string"&&t.timezone.trim()!==""?t.timezone.trim():"UTC",rules:r}}function mt(e){if(!e||typeof e!="object")return;let t=e,r={};for(let n of ["accountId","siteId"]){let s=t[n];typeof s=="string"&&s.trim()!==""&&(r[n]=s.trim());}let i=Number(t.lastUpdate??0);return Number.isFinite(i)&&i>0&&(r.lastUpdate=Math.floor(i)),t.subscriber===true&&(r.subscriber=true),Object.keys(r).length>0?r:void 0}function pe(e){let t=e&&typeof e=="object"?e:{},r=String(t.accountId??"").trim(),i=String(t.siteId??"").trim(),n=t.subscriber===true,s=mt(t.siteSettings),o={...s??{},...r?{accountId:s?.accountId??r}:{},...i?{siteId:s?.siteId??i}:{},...s?.subscriber===true||n?{subscriber:true}:{}},l={},a=String(t.apiBase??"").trim();a&&(l.apiBase=a);let u=String(t.runtimeToken??t.nonce??"").trim();u&&(l.runtimeToken=u);let c=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();c&&(l.virtualAssetBaseUrl=c),Object.keys(o).length>0&&(l.siteSettings=o);let p=ge(t.subscriptionType);return p&&(l.subscriptionType=p),l}function yt(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function Ee(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function wt(e,t){let r=new Headers(e instanceof Request?e.headers:void 0);if(t?.headers)for(let[i,n]of new Headers(t.headers).entries())r.set(i,n);return r}async function Me(e,t,r=5){let i=new URL(Ee(e)),n=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),s=i.protocol==="http:"?gt:pt,o=wt(e,t);return await new Promise((l,a)=>{let u=s.request(i,{method:n,headers:Object.fromEntries(o.entries()),...i.protocol==="https:"?{rejectUnauthorized:false}:{}},c=>{let p=[];c.on("data",g=>{p.push(Buffer.isBuffer(g)?g:Buffer.from(g));}),c.on("error",a),c.on("end",()=>{let g=c.statusCode??0,d=c.headers.location;if(r>0&&d&&[301,302,303,307,308].includes(g)){l(Me(new URL(d,i),{method:g===303?"GET":n},r-1));return}let y=new Headers;for(let[w,v]of Object.entries(c.headers))Array.isArray(v)?y.set(w,v.join(", ")):typeof v=="string"&&y.set(w,v);l(new Response(Buffer.concat(p),{status:g,statusText:c.statusMessage??"",headers:y}));});});u.on("error",a),u.end();})}function bt(e){let t=pe(e.wpsuite),r={...t.siteSettings??{},...t.siteSettings?.subscriber===true||t.subscriptionType==="PROFESSIONAL"||t.subscriptionType==="AGENCY"?{subscriber:true}:{}},i=String(r.accountId??"").trim(),n=String(r.siteId??"").trim(),s=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();if(e.wpsuite={...t.apiBase?{apiBase:t.apiBase}:{},...t.runtimeToken?{runtimeToken:t.runtimeToken}:{},...s?{virtualAssetBaseUrl:s}:{},...Object.keys(r).length>0?{siteSettings:r}:{},...t.subscriptionType?{subscriptionType:t.subscriptionType}:{}},!i||!n||!s)return false;yt(e.sourceOrigin);let o=globalThis,a={...o.WpSuite??{},siteSettings:r,uploadUrl:s};return t.apiBase&&(a.apiBase=t.apiBase),o.WpSuite=a,true}async function Pt(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!bt(e)){let n=e.wpsuite?.siteSettings,s=[String(n?.accountId??"").trim()?"":"accountId",String(n?.siteId??"").trim()?"":"siteId",t?"":"virtualAssetBaseUrl"].filter(Boolean);return {remote:null,diagnostic:{status:"invalid-runtime",virtualAssetBaseUrl:t,subscriptionType:null,error:`Missing WP Suite runtime field(s): ${s.join(", ")}.`,requests:[]}}}let r=globalThis.fetch,i=[];try{typeof r=="function"&&/^https?:\/\//i.test(t)&&(globalThis.fetch=(async(a,u)=>{let c=Ee(a);if(c.startsWith(t)){let p=(()=>{try{return new URL(c).pathname}catch{return c}})();try{let g=e.ignoreHttpsErrors&&c.startsWith("https://")?await Me(a,u):await r(a,u);return i.push({path:p,status:g.status,ok:g.ok,error:null}),g}catch(g){throw i.push({path:p,status:null,ok:!1,error:g instanceof Error?g.message:String(g)}),g}}return r(a,u)}));let{getConfig:n}=await import('@smart-cloud/wpsuite-core'),s=await n("publisher"),o=s&&typeof s=="object"?s:null,l=ge(o?.subscriptionType);return {remote:o,diagnostic:{status:o?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:l??null,error:o?null:"WP Suite publisher config loader returned no configuration.",requests:i}}}catch(n){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:t,subscriptionType:null,error:n instanceof Error?n.message:String(n),requests:i}}}finally{typeof r=="function"&&(globalThis.fetch=r);}}function St(e,t){let r=ft(t?.deploymentProfiles),i=String(t?.defaultDeploymentProfile??"").trim(),n=r[i]?i:"",s=ge(t?.subscriptionType),o=pe(e.wpsuite);return {...e,scheduler:Ce(t?.scheduler),deploymentProfiles:r,defaultDeploymentProfile:n,...s?{subscriptionType:s}:{},wpsuite:{...o,...o.siteSettings||s?{siteSettings:{...o.siteSettings??{},...o.siteSettings?.subscriber===true||s==="PROFESSIONAL"||s==="AGENCY"?{subscriber:true}:{}}}:{},...s?{subscriptionType:s}:{}}}}function ce(e,t){let i=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!i)return t;let n=i.split("/").map(s=>s.trim()).filter(s=>s.length>0&&s!=="."&&s!=="..");return n.length>0?n.join("/"):t}function vt(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?b.resolve(e):""}function $e(e,t,r){let i=String(t||"").trim();return i&&b.isAbsolute(i)?b.resolve(i):b.resolve(e,ce(i,r))}async function de(e,t={}){let r=String("").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",i=await x.readFile(r,"utf8"),n=JSON.parse(i);n.sourceOrigin=n.sourceOrigin.replace(/\/$/,""),n.targetOrigin=Te(n.targetOrigin),n.ignoreHttpsErrors??=false,n.outputDir=String(n.outputDir||"export").trim()||"export",n.urlRewriteMode||=n.targetOrigin==="."?"relative":"absolute",n.noJavaScriptRenderPathPrefixes||=[],n.seedPaths||=[],n.generated404RequestPath=dt(n.generated404RequestPath),n.sitemapPaths||=["/sitemap_index.xml","/sitemap.xml"],n.allowedAssetHosts||=[],n.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],n.blockedPathPrefixes||=[],n.blockedSearchFragments||=[],n.concurrency||=1,n.maxPages||=0,n.extraReplacements=ue(n.extraReplacements),n.postCrawlCopyMap=ue(n.postCrawlCopyMap),n.logDir=String(n.logDir||"logs").trim()||"logs",n.verbose??=false,n.logLevel||=n.verbose?"debug":"info",n.s3SyncMode||="sdk-upload-delete",n.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},n.readiness.timeoutMs??=1500,n.readiness.fallbackWaitMs??=1500,n.viewport||={width:1440,height:1200},n.navigationTimeoutMs||=3e4,n.assetDownloadConcurrency=Number(n.assetDownloadConcurrency)>0?Number(n.assetDownloadConcurrency):n.concurrency,n.rewriteConcurrency=Number(n.rewriteConcurrency)>0?Number(n.rewriteConcurrency):n.assetDownloadConcurrency,n.wpsuite=pe(n.wpsuite),n.scheduler=Ce(void 0),n.deploymentProfiles={},n.defaultDeploymentProfile="",n.deploymentTargetOverride=String(n.deploymentTargetOverride??"").trim();let s=await Pt(n);t.onRemotePublisherConfigDiagnostic?.(s.diagnostic);let o=St(n,s.remote),l=vt(),a=l?b.resolve(l,".."):"";return a?(o.outputDir=$e(a,o.outputDir,"export"),o.logDir=$e(a,o.logDir,"logs")):(b.isAbsolute(o.outputDir)||(o.outputDir=ce(o.outputDir,"export")),b.isAbsolute(o.logDir)||(o.logDir=ce(o.logDir,"logs"))),o}function Rt(e){let t=e/6e4;if(Number.isInteger(t)&&t>=1)return `${t} minute${t===1?"":"s"}`;let r=Math.ceil(e/1e3);return `${r} second${r===1?"":"s"}`}async function O(e,t,r){let i=new AbortController,n=globalThis.setTimeout(()=>{i.abort(new Error(`${e} timed out after ${Rt(t)}.`));},t);try{return await r({abortController:i,httpOptions:{abortSignal:i.signal,requestTimeout:t}})}finally{globalThis.clearTimeout(n);}}var Dt=".deploy-plan.json",xt="deploy-plan.json",$t=".crawl-incomplete.json",Tt="crawl-incomplete.json";function ke(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?b.resolve(e):""}function Ct(e){let t=String(e||"").replace(/\\/g,"/").replace(/^\/+/,"").trim();return !t||t==="."||t.startsWith("../")||t===".."?null:t}function fe(e){return [...new Set(e.map(Ct).filter(t=>t!==null))]}function Et(e,t){return {schemaVersion:1,generatedAt:String(t.generatedAt||new Date().toISOString()),outputDir:b.resolve(e.outputDir),runMode:t.runMode,crawlMode:t.crawlMode,fullSyncRequired:!!t.fullSyncRequired,changedFiles:fe(t.changedFiles||[]),deletedFiles:fe(t.deletedFiles||[]),rewriteTargets:fe(t.rewriteTargets||[])}}function Mt(e){if(!e||typeof e!="object")return false;let t=e;return t.schemaVersion===1&&typeof t.generatedAt=="string"&&typeof t.outputDir=="string"&&(t.runMode==="full"||t.runMode==="retry-timeouts"||t.runMode==="single-url")&&(t.crawlMode==="full"||t.crawlMode==="incremental")&&typeof t.fullSyncRequired=="boolean"&&Array.isArray(t.changedFiles)&&Array.isArray(t.deletedFiles)&&Array.isArray(t.rewriteTargets)}function Ie(e){let t=b.join(b.resolve(e.outputDir),Dt),r=ke();return r?[b.join(r,xt),t]:[t]}function kt(e){let t=b.join(b.resolve(e.outputDir),$t),r=ke();return r?[b.join(r,Tt),t]:[t]}async function Ae(e){let t=b.resolve(e.outputDir);for(let r of Ie(e))try{let i=await x.readFile(r,"utf8"),n=JSON.parse(i);if(!Mt(n)||b.resolve(n.outputDir)!==t)continue;return Et(e,n)}catch{}return null}async function Le(e){for(let t of Ie(e))try{await x.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}async function Oe(e){let t=b.resolve(e.outputDir);for(let r of kt(e)){let i;try{i=await x.readFile(r,"utf8");}catch(s){if(s.code==="ENOENT")continue;throw s}let n;try{n=JSON.parse(i);}catch{throw new Error(`Invalid incomplete crawl marker: ${r}`)}if(n.schemaVersion!==1||typeof n.startedAt!="string"||typeof n.outputDir!="string"||b.resolve(n.outputDir)!==t)throw new Error(`Invalid incomplete crawl marker: ${r}`);return {schemaVersion:1,startedAt:n.startedAt,outputDir:t}}return null}function It(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function je(e){let t="";for(let i=0;i<e.length;i++){let n=e[i];if(n==="--profile"){let s=(e[i+1]||"").trim();if(!s)throw new Error("Missing value for --profile.");t=s,i+=1;}else if(n.startsWith("--profile=")){let s=n.slice(10).trim();if(!s)throw new Error("Missing value for --profile.");t=s;}}let r=String(process.env.PUBLISHER_DEPLOY_PROFILE||process.env.PUBLISHER_DEPLOYMENT_PROFILE||"").trim();return t||r||null}function At(e,t){return {...e,...t??{}}}function Lt(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function Fe(e,t){let r=String(t??e.deploymentTargetOverride??"").trim();if(!It(e)){if(r)throw new Error(`Deployment target "${r}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:e}}let i=String(r||e.defaultDeploymentProfile||"").trim();if(!i)return {name:null,profile:null,config:e};let n=e.deploymentProfiles?.[i];if(!n)throw new Error(`Unknown deployment profile "${i}". Check the linked WPSuite publisher configuration.`);return {name:i,profile:n,config:{...e,targetOrigin:n.targetOrigin??e.targetOrigin,s3:At(e.s3,n.s3),cloudFront:Lt(e.cloudFront,n.cloudFront)}}}var _e={error:0,warn:1,info:2,debug:3},Ue=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",Z=Ue?b.join(Ue,"current-progress.json"):"",Ne=Promise.resolve();function We(e){return e.includes("crawl")?"crawl":e.includes("deploy")?"deploy":e.includes("invalidate")?"invalidate":e}function Ot(e,t){let r={...e},i=new Set(["pagesQueued","pagesRendered","assetsQueued","sitemapsQueued","pagesDiscovered","assetsDiscovered","sitemapsDiscovered","pagesSaved","assetsSaved","donePages","doneAssets","doneSitemaps","changedTextFiles","uploaded","failed","index","totalFiles","pageQueue","assetQueue","sitemapQueue"]);for(let[n,s]of Object.entries(t)){let o=r[n];if(typeof o=="number"&&typeof s=="number"&&i.has(n)){r[n]=Math.max(o,s);continue}r[n]=s;}return r}function jt(e,t,r){Z&&(Ne=Ne.then(async()=>{let i=new Date,n=i.toISOString();await x.mkdir(b.dirname(Z),{recursive:true});let s=await x.readFile(Z,"utf8").then(w=>JSON.parse(w)).catch(()=>null),o=s&&typeof s.details=="object"&&s.details?s.details:{},l=We(e),a=s?.currentStep||We(s?.source||""),u=s?.startedAt||n,c={...s?.stepDurationsSec??{}},p=s?.stepStartedAt||n;if(a&&a!==l&&s?.stepStartedAt){let w=Math.max(0,Math.round((i.getTime()-new Date(s.stepStartedAt).getTime())/1e3));c[a]=(c[a]??0)+w,p=n;}let g=Math.max(0,Math.round((i.getTime()-new Date(p).getTime())/1e3)),d=Math.max(0,Math.round((i.getTime()-new Date(u).getTime())/1e3)),y={checkedAt:n,source:e,message:t,details:Ot(o,r??{}),startedAt:u,currentStep:l,stepStartedAt:p,stepElapsedSec:g,totalElapsedSec:d,stepDurationsSec:c};await x.writeFile(Z,JSON.stringify(y,null,2),"utf8");}).catch(()=>{}));}async function Ft(e,t){await x.mkdir(b.dirname(e),{recursive:true}),await x.appendFile(e,`${JSON.stringify(t)}
2
- `,"utf8");}var G=class{constructor(t,r,i="info"){this.logDir=t;this.logFile=r;this.level=i;this.ensureLogFileReady();}logDir;logFile;level;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;get logPath(){return b.join(this.logDir,this.logFile)}artifactPath(t){return this.logFile.endsWith(".log.jsonl")?b.join(this.logDir,this.logFile.replace(/\.log\.jsonl$/u,`${t}.jsonl`)):b.join(this.logDir,`${this.logFile}${t}.jsonl`)}get errorsPath(){return this.artifactPath(".errors")}get rejectedPath(){return this.artifactPath(".rejected")}ensureLogFileReady(){return this.initPromise||(this.initPromise=(async()=>{await x.mkdir(this.logDir,{recursive:true}),await x.writeFile(this.logPath,"","utf8");})()),this.initPromise}enqueueTask(t){this.writeQueue=this.writeQueue.then(t).catch(r=>{this.writeError=r instanceof Error?r:new Error(String(r));});}enqueueLine(t){this.enqueueTask(async()=>{await this.ensureLogFileReady(),await x.appendFile(this.logPath,`${t}
3
- `,"utf8");});}enqueueJsonLine(t,r){this.enqueueTask(()=>Ft(t,r));}accepts(t){return _e[t]<=_e[this.level]}push(t,r,i){if(!this.accepts(t))return;let n=JSON.stringify({time:new Date().toISOString(),level:t,message:r,...i||{}});this.enqueueLine(n),t==="error"?console.error(`[ERROR] ${r}`,i?JSON.stringify(i):""):t==="warn"?console.warn(`[WARN] ${r}`,i?JSON.stringify(i):""):console.log(`[${t.toUpperCase().padEnd(5)}] ${r}`);}info(t,r){this.push("info",t,r);}progress(t,r){jt(this.logFile,t,r);}debug(t,r){this.push("debug",t,r);}warn(t,r){this.push("warn",t,r);}reject(t,r,i,n,s){let o={kind:t,url:r,reason:i,...n?{source:n}:{},...s||{}};this.enqueueJsonLine(this.rejectedPath,o),this.accepts("debug")&&this.push("debug",`Rejected ${t} ${r}: ${i}`,o);}error(t,r){this.enqueueJsonLine(this.errorsPath,{message:t,...r||{}}),this.push("error",t,r);}mark(t){this.marks.set(t,Date.now());}endMark(t,r){let i=this.marks.get(t);if(!i)return;let n=Date.now()-i,s=Number((n/1e3).toFixed(2));this.push("info",`Timing ${t}: ${s}s`,{name:t,ms:n,seconds:s,...r||{}});}async flush(){let t=Date.now()-this.startedAt,r=Number((t/1e3).toFixed(2));if(this.push("info",`Total time: ${r}s`,{name:"total",ms:t,seconds:r}),await this.writeQueue,this.writeError)throw this.writeError}};async function Ut(e){await x.mkdir(b.dirname(e),{recursive:true});}async function qe(e,t,r){await Ut(e);let i=b.join(b.dirname(e),`.${b.basename(e)}.${process.pid}.${Date.now()}.${_t.randomBytes(6).toString("hex")}.tmp`);try{typeof t=="string"?await x.writeFile(i,t,r??"utf8"):await x.writeFile(i,t),await x.rename(i,e);}catch(n){throw await x.unlink(i).catch(()=>{}),n}}function He(e){return [".html",".htm",".css",".js",".mjs",".json",".xml",".xsl",".txt",".svg",".map",".enc",".jws"].includes(b.extname(e).toLowerCase())}function Je(e){return e.replace(/&quot;/g,'"').replace(/&#34;/g,'"').replace(/&apos;/g,"'").replace(/&#39;/g,"'").replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">")}function z(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function X(e){return e.replace(/\//g,"\\/")}function ze(e){return e.replace(/\//g,"\\\\/")}function Nt(e){try{let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")return null;let r=t.pathname==="/"&&!t.search&&!t.hash?"":`${t.pathname}${t.search}${t.hash}`;return `//${t.host}${r}`}catch{return null}}function Be(e,t,r){if(!t)return;e[t]=r;let i=X(t),n=X(r);e[i]=n;let s=ze(t),o=ze(r);e[s]=o;let l=z(t),a=z(r);e[l]=a;let u=z(i),c=z(n);e[u]=c;let p=z(s),g=z(o);e[p]=g;}function ee(e,t,r){Be(e,t,r);let i=Nt(t);i&&i!==t&&Be(e,i,r);}function Ke(e,t){try{return new URL(e,t==="."?"https://relative.invalid":t).pathname}catch{return e.startsWith("/")?e:`/${e.replace(/^\.\//,"")}`}}function Ge(e,t,r){if(!t)return r;let i=b.dirname(b.resolve(t)),n=b.resolve(e,r.replace(/^\/+/,"")),s=b.relative(i,n).replace(/\\/g,"/");return s?(s.startsWith(".")||(s=`./${s}`),s):"."}var Ht=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],Jt=new Set([".html",".htm"]),Ve="WPSuite.io Static Publisher",Kt=Ve.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function Gt(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function Vt(e){return e.startsWith("//")||e.startsWith("\\/\\/")||e.startsWith("\\\\/\\\\/")}function Qt(e,t,r){return Vt(t)?e.replace(new RegExp(`(?<!:)${Gt(t)}`,"g"),r):e.split(t).join(r)}function Yt(e,t,r){if(e.wpsuite?.siteSettings?.subscriber===true||e.wpsuite?.subscriptionType==="PROFESSIONAL"||e.wpsuite?.subscriptionType==="AGENCY"||!r)return false;let n=b.extname(r).toLowerCase();return Jt.has(n)?/<head\b|<html\b|<!doctype html/i.test(t):false}function Zt(e){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${Kt}\\2)[^>]*\\/?>`,"i").test(e))return e;let t=`<meta name="generator" content="${Ve}" />`;if(e.match(/(\r?\n)([ \t]*)<\/head>/i))return e.replace(/(\r?\n)([ \t]*)<\/head>/i,`$1$2${t}$1$2</head>`);let i=e.includes(`\r
1
+ import D from'fs/promises';import wt from'http';import bt from'https';import b from'path';import {S3Client,HeadObjectCommand,GetObjectCommand,DeleteObjectsCommand,ListObjectsV2Command}from'@aws-sdk/client-s3';import {Upload}from'@aws-sdk/lib-storage';import Kt,{createHash}from'crypto';import Qt from'fast-glob';import Rr from'mime-types';import {availableParallelism}from'os';import {Worker}from'worker_threads';function Me(t){let e=t.trim();return e==="."?".":e.replace(/\/$/,"")}function Pt(t){let e=String(t??"").trim();if(!e)return "";let r=e;if(/^https?:\/\//i.test(r))try{r=new URL(r).pathname;}catch{return ""}if(r=r.split(/[?#]/,1)[0]?.replace(/\\/g,"/").trim()??"",!r)return "";let i=r.endsWith("/"),n=r.replace(/^\/+/,"").split("/").map(o=>o.replace(/[^A-Za-z0-9._-]/g,"")).filter(o=>o.length>0&&o!=="."&&o!=="..");if(n.length===0)return "";let s=`/${n.join("/")}`;return i&&b.extname(s)===""&&(s+="/"),s==="/"?"":s}function me(t){return !t||typeof t!="object"?{}:Object.fromEntries(Object.entries(t).map(([e,r])=>[e.trim(),String(r??"")]).filter(([e])=>e.length>0))}function St(t){if(!t||typeof t!="object")return {};let e={};for(let[r,i]of Object.entries(t)){let n=r.trim();if(!n||!i||typeof i!="object")continue;let s=i,o={};if(typeof s.targetOrigin=="string"){let a=Me(s.targetOrigin);a&&(o.targetOrigin=a);}let l=me(s.extraReplacements);if(Object.keys(l).length>0&&(o.extraReplacements=l),s.s3&&typeof s.s3=="object"){let a={},c=s.s3;typeof c.bucket=="string"&&(a.bucket=c.bucket.trim()),typeof c.prefix=="string"&&(a.prefix=c.prefix.trim()),typeof c.region=="string"&&(a.region=c.region.trim()),typeof c.htmlCacheControl=="string"&&(a.htmlCacheControl=c.htmlCacheControl.trim()),typeof c.assetCacheControl=="string"&&(a.assetCacheControl=c.assetCacheControl.trim()),Object.keys(a).length>0&&(o.s3=a);}if(s.cloudFront&&typeof s.cloudFront=="object"){let a={},c=s.cloudFront;typeof c.distributionId=="string"&&(a.distributionId=c.distributionId.trim()),Array.isArray(c.invalidationPaths)&&(a.invalidationPaths=c.invalidationPaths.map(u=>String(u??"").trim()).filter(u=>u.length>0)),Object.keys(a).length>0&&(o.cloudFront=a);}e[n]=o;}return e}function ye(t){return t==="PROFESSIONAL"||t==="AGENCY"?t:void 0}function vt(t,e){if(!t||typeof t!="object")return null;let r=t,i=r.command;if(i!=="publish"&&i!=="crawl"&&i!=="deploy"&&i!=="invalidate"&&i!=="retry-timeouts"&&i!=="url"&&i!=="content-sync")return null;let n=Number.parseInt(String(r.intervalMinutes??"0"),10);if(!Number.isFinite(n)||n<1)return null;let s=String(r.id??`${i}-${e+1}`).trim();if(!s)return null;let o=String(r.deploymentProfile??"").trim(),l=String(r.url??"").trim(),a=Array.isArray(r.postTypes)?[...new Set(r.postTypes.map(u=>String(u??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],c=Array.isArray(r.listingPaths)?[...new Set(r.listingPaths.map(u=>String(u??"").trim()).filter(Boolean))]:[];return i==="content-sync"&&a.length===0?null:{id:s,enabled:r.enabled!==false,command:i,intervalMinutes:n,...i==="publish"||i==="crawl"?{crawlMode:r.crawlMode==="incremental"?"incremental":"full"}:{},...(i==="publish"||i==="deploy"||i==="invalidate"||i==="content-sync")&&o?{deploymentProfile:o}:{},...l?{url:l}:{},...a.length>0?{postTypes:a}:{},...c.length>0?{listingPaths:c}:{},...typeof r.includePostTypeArchives=="boolean"?{includePostTypeArchives:r.includePostTypeArchives}:{},...typeof r.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:r.includeTaxonomyArchives}:{},...typeof r.includeAuthorArchives=="boolean"?{includeAuthorArchives:r.includeAuthorArchives}:{},...typeof r.includeDateArchives=="boolean"?{includeDateArchives:r.includeDateArchives}:{},...typeof r.includePostsPage=="boolean"?{includePostsPage:r.includePostsPage}:{},...typeof r.includeSitemapChain=="boolean"?{includeSitemapChain:r.includeSitemapChain}:{}}}function Ie(t){let e=t&&typeof t=="object"?t:{},r=Array.isArray(e.rules)?e.rules.map((i,n)=>vt(i,n)).filter(i=>!!i):[];return {enabled:e.enabled===true,timezone:typeof e.timezone=="string"&&e.timezone.trim()!==""?e.timezone.trim():"UTC",rules:r}}function Rt(t){if(!t||typeof t!="object")return;let e=t,r={};for(let n of ["accountId","siteId"]){let s=e[n];typeof s=="string"&&s.trim()!==""&&(r[n]=s.trim());}let i=Number(e.lastUpdate??0);return Number.isFinite(i)&&i>0&&(r.lastUpdate=Math.floor(i)),e.subscriber===true&&(r.subscriber=true),Object.keys(r).length>0?r:void 0}function we(t){let e=t&&typeof t=="object"?t:{},r=String(e.accountId??"").trim(),i=String(e.siteId??"").trim(),n=e.subscriber===true,s=Rt(e.siteSettings),o={...s??{},...r?{accountId:s?.accountId??r}:{},...i?{siteId:s?.siteId??i}:{},...s?.subscriber===true||n?{subscriber:true}:{}},l={},a=String(e.apiBase??"").trim();a&&(l.apiBase=a);let c=String(e.runtimeToken??e.nonce??"").trim();c&&(l.runtimeToken=c);let u=String(e.virtualAssetBaseUrl??e.uploadUrl??"").trim();u&&(l.virtualAssetBaseUrl=u),Object.keys(o).length>0&&(l.siteSettings=o);let m=ye(e.subscriptionType);return m&&(l.subscriptionType=m),l}function Dt(t){try{let e=new URL(t);globalThis.location=e;}catch{}}function Le(t){return typeof t=="string"?t:t instanceof URL?t.toString():t.url}function xt(t,e){let r=new Headers(t instanceof Request?t.headers:void 0);if(e?.headers)for(let[i,n]of new Headers(e.headers).entries())r.set(i,n);return r}async function Oe(t,e,r=5){let i=new URL(Le(t)),n=String(e?.method??(t instanceof Request?t.method:"GET")).toUpperCase(),s=i.protocol==="http:"?wt:bt,o=xt(t,e);return await new Promise((l,a)=>{let c=s.request(i,{method:n,headers:Object.fromEntries(o.entries()),...i.protocol==="https:"?{rejectUnauthorized:false}:{}},u=>{let m=[];u.on("data",p=>{m.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),u.on("error",a),u.on("end",()=>{let p=u.statusCode??0,d=u.headers.location;if(r>0&&d&&[301,302,303,307,308].includes(p)){l(Oe(new URL(d,i),{method:p===303?"GET":n},r-1));return}let g=new Headers;for(let[w,T]of Object.entries(u.headers))Array.isArray(T)?g.set(w,T.join(", ")):typeof T=="string"&&g.set(w,T);l(new Response(Buffer.concat(m),{status:p,statusText:u.statusMessage??"",headers:g}));});});c.on("error",a),c.end();})}function $t(t){let e=we(t.wpsuite),r={...e.siteSettings??{},...e.siteSettings?.subscriber===true||e.subscriptionType==="PROFESSIONAL"||e.subscriptionType==="AGENCY"?{subscriber:true}:{}},i=String(r.accountId??"").trim(),n=String(r.siteId??"").trim(),s=String(e.virtualAssetBaseUrl??e.uploadUrl??"").trim();if(t.wpsuite={...e.apiBase?{apiBase:e.apiBase}:{},...e.runtimeToken?{runtimeToken:e.runtimeToken}:{},...s?{virtualAssetBaseUrl:s}:{},...Object.keys(r).length>0?{siteSettings:r}:{},...e.subscriptionType?{subscriptionType:e.subscriptionType}:{}},!i||!n||!s)return false;Dt(t.sourceOrigin);let o=globalThis,a={...o.WpSuite??{},siteSettings:r,uploadUrl:s};return e.apiBase&&(a.apiBase=e.apiBase),o.WpSuite=a,true}async function Tt(t){let e=String(t.wpsuite?.virtualAssetBaseUrl??t.wpsuite?.uploadUrl??"").trim();if(!$t(t)){let n=t.wpsuite?.siteSettings,s=[String(n?.accountId??"").trim()?"":"accountId",String(n?.siteId??"").trim()?"":"siteId",e?"":"virtualAssetBaseUrl"].filter(Boolean);return {remote:null,diagnostic:{status:"invalid-runtime",virtualAssetBaseUrl:e,subscriptionType:null,error:`Missing WP Suite runtime field(s): ${s.join(", ")}.`,requests:[]}}}let r=globalThis.fetch,i=[];try{typeof r=="function"&&/^https?:\/\//i.test(e)&&(globalThis.fetch=(async(a,c)=>{let u=Le(a);if(u.startsWith(e)){let m=(()=>{try{return new URL(u).pathname}catch{return u}})();try{let p=t.ignoreHttpsErrors&&u.startsWith("https://")?await Oe(a,c):await r(a,c);return i.push({path:m,status:p.status,ok:p.ok,error:null}),p}catch(p){throw i.push({path:m,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return r(a,c)}));let{getConfig:n}=await import('@smart-cloud/wpsuite-core'),s=await n("publisher"),o=s&&typeof s=="object"?s:null,l=ye(o?.subscriptionType);return {remote:o,diagnostic:{status:o?"loaded":"unavailable",virtualAssetBaseUrl:e,subscriptionType:l??null,error:o?null:"WP Suite publisher config loader returned no configuration.",requests:i}}}catch(n){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:e,subscriptionType:null,error:n instanceof Error?n.message:String(n),requests:i}}}finally{typeof r=="function"&&(globalThis.fetch=r);}}function Et(t,e){let r=St(e?.deploymentProfiles),i=String(e?.defaultDeploymentProfile??"").trim(),n=r[i]?i:"",s=ye(e?.subscriptionType),o=we(t.wpsuite);return {...t,scheduler:Ie(e?.scheduler),deploymentProfiles:r,defaultDeploymentProfile:n,...s?{subscriptionType:s}:{},wpsuite:{...o,...o.siteSettings||s?{siteSettings:{...o.siteSettings??{},...o.siteSettings?.subscriber===true||s==="PROFESSIONAL"||s==="AGENCY"?{subscriber:true}:{}}}:{},...s?{subscriptionType:s}:{}}}}function he(t,e){let i=String(t||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!i)return e;let n=i.split("/").map(s=>s.trim()).filter(s=>s.length>0&&s!=="."&&s!=="..");return n.length>0?n.join("/"):e}function Ct(){let t=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return t.trim()?b.resolve(t):""}function Ae(t,e,r){let i=String(e||"").trim();return i&&b.isAbsolute(i)?b.resolve(i):b.resolve(t,he(i,r))}async function ne(t,e={}){let r=String("").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",i=await D.readFile(r,"utf8"),n=JSON.parse(i);n.sourceOrigin=n.sourceOrigin.replace(/\/$/,""),n.targetOrigin=Me(n.targetOrigin),n.ignoreHttpsErrors??=false,n.outputDir=String(n.outputDir||"export").trim()||"export",n.urlRewriteMode||=n.targetOrigin==="."?"relative":"absolute",n.noJavaScriptRenderPathPrefixes||=[],n.seedPaths||=[],n.generated404RequestPath=Pt(n.generated404RequestPath),n.sitemapPaths||=["/sitemap_index.xml","/sitemap.xml"],n.allowedAssetHosts||=[],n.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],n.blockedPathPrefixes||=[],n.blockedSearchFragments||=[],n.concurrency||=1,n.maxPages||=0,n.extraReplacements=me(n.extraReplacements),n.postCrawlCopyMap=me(n.postCrawlCopyMap),n.logDir=String(n.logDir||"logs").trim()||"logs",n.verbose??=false,n.logLevel||=n.verbose?"debug":"info",n.s3SyncMode||="sdk-upload-delete",n.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},n.readiness.timeoutMs??=1500,n.readiness.fallbackWaitMs??=1500,n.viewport||={width:1440,height:1200},n.navigationTimeoutMs||=3e4,n.assetDownloadConcurrency=Number(n.assetDownloadConcurrency)>0?Number(n.assetDownloadConcurrency):n.concurrency,n.rewriteConcurrency=Number(n.rewriteConcurrency)>0?Number(n.rewriteConcurrency):n.assetDownloadConcurrency,n.wpsuite=we(n.wpsuite),n.scheduler=Ie(void 0),n.deploymentProfiles={},n.defaultDeploymentProfile="",n.deploymentTargetOverride=String(n.deploymentTargetOverride??"").trim();let s=await Tt(n);e.onRemotePublisherConfigDiagnostic?.(s.diagnostic);let o=Et(n,s.remote),l=Ct(),a=l?b.resolve(l,".."):"";return a?(o.outputDir=Ae(a,o.outputDir,"export"),o.logDir=Ae(a,o.logDir,"logs")):(b.isAbsolute(o.outputDir)||(o.outputDir=he(o.outputDir,"export")),b.isAbsolute(o.logDir)||(o.logDir=he(o.logDir,"logs"))),o}function kt(t){let e=t/6e4;if(Number.isInteger(e)&&e>=1)return `${e} minute${e===1?"":"s"}`;let r=Math.ceil(t/1e3);return `${r} second${r===1?"":"s"}`}async function O(t,e,r){let i=new AbortController,n=globalThis.setTimeout(()=>{i.abort(new Error(`${t} timed out after ${kt(e)}.`));},e);try{return await r({abortController:i,httpOptions:{abortSignal:i.signal,requestTimeout:e}})}finally{globalThis.clearTimeout(n);}}var At=".deploy-plan.json",Mt="deploy-plan.json",It=".crawl-incomplete.json",Lt="crawl-incomplete.json";function je(){let t=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return t.trim()?b.resolve(t):""}function Ot(t){let e=String(t||"").replace(/\\/g,"/").replace(/^\/+/,"").trim(),r=b.posix.normalize(e);return !e||!r||r==="."||/^[a-zA-Z]:/.test(r)||r.startsWith("../")||r===".."?null:r}function jt(t){let e=String(t||"").trim();if(!e.startsWith("/")||e.startsWith("//")||e.includes("*")||e.includes("#"))return null;try{let r=new URL(e,"https://content-sync.invalid");return r.origin!=="https://content-sync.invalid"?null:`${b.posix.normalize(r.pathname)}${r.search}`}catch{return null}}function j(t){return [...new Set(t.map(Ot).filter(e=>e!==null))]}function Ft(t,e){let r={generatedAt:String(e.generatedAt||new Date().toISOString()),outputDir:b.resolve(t.outputDir),runMode:e.runMode,crawlMode:e.crawlMode,fullSyncRequired:!!e.fullSyncRequired,changedFiles:j(e.changedFiles||[]),deletedFiles:j(e.deletedFiles||[]),rewriteTargets:j(e.rewriteTargets||[])};return e.schemaVersion===2?{...r,schemaVersion:2,runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,consumerId:String(e.consumerId||"").trim(),fromSequence:Math.max(0,Number(e.fromSequence)||0),toSequence:Math.max(0,Number(e.toSequence)||0),impactHash:String(e.impactHash||"").trim(),uploadGroups:{assets:j(e.uploadGroups?.assets||[]),content:j(e.uploadGroups?.content||[]),listings:j(e.uploadGroups?.listings||[]),sitemapLeaves:j(e.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:j(e.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((e.invalidationPaths||[]).map(jt).filter(i=>i!==null))].sort()}:{schemaVersion:1,...r,runMode:e.runMode}}function _t(t){if(!t||typeof t!="object")return false;let e=t;if(e.schemaVersion===2){let r=e;return r.runMode==="content-sync"&&r.crawlMode==="incremental"&&r.fullSyncRequired===false&&typeof r.generatedAt=="string"&&typeof r.outputDir=="string"&&typeof r.consumerId=="string"&&r.consumerId.trim()!==""&&Number.isSafeInteger(r.fromSequence)&&Number.isSafeInteger(r.toSequence)&&Number(r.toSequence)>=Number(r.fromSequence)&&typeof r.impactHash=="string"&&/^[a-f0-9]{64}$/.test(r.impactHash)&&Array.isArray(r.changedFiles)&&Array.isArray(r.deletedFiles)&&Array.isArray(r.rewriteTargets)&&!!r.uploadGroups&&Array.isArray(r.uploadGroups.assets)&&Array.isArray(r.uploadGroups.content)&&Array.isArray(r.uploadGroups.listings)&&Array.isArray(r.uploadGroups.sitemapLeaves)&&Array.isArray(r.uploadGroups.sitemapIndexes)&&Array.isArray(r.invalidationPaths)}return e.schemaVersion===1&&typeof e.generatedAt=="string"&&typeof e.outputDir=="string"&&(e.runMode==="full"||e.runMode==="retry-timeouts"||e.runMode==="single-url")&&(e.crawlMode==="full"||e.crawlMode==="incremental")&&typeof e.fullSyncRequired=="boolean"&&Array.isArray(e.changedFiles)&&Array.isArray(e.deletedFiles)&&Array.isArray(e.rewriteTargets)}function Fe(t){let e=b.join(b.resolve(t.outputDir),At),r=je();return r?[b.join(r,Mt),e]:[e]}function Ut(t){let e=b.join(b.resolve(t.outputDir),It),r=je();return r?[b.join(r,Lt),e]:[e]}async function _e(t){let e=b.resolve(t.outputDir);for(let r of Fe(t))try{let i=await D.readFile(r,"utf8"),n=JSON.parse(i);if(!_t(n)||b.resolve(n.outputDir)!==e)continue;return Ft(t,n)}catch{}return null}async function Ue(t){for(let e of Fe(t))try{await D.unlink(e);}catch(r){if(r.code!=="ENOENT")throw r}}async function Ne(t){let e=b.resolve(t.outputDir);for(let r of Ut(t)){let i;try{i=await D.readFile(r,"utf8");}catch(s){if(s.code==="ENOENT")continue;throw s}let n;try{n=JSON.parse(i);}catch{throw new Error(`Invalid incomplete crawl marker: ${r}`)}if(n.schemaVersion!==1||typeof n.startedAt!="string"||typeof n.outputDir!="string"||b.resolve(n.outputDir)!==e)throw new Error(`Invalid incomplete crawl marker: ${r}`);return {schemaVersion:1,startedAt:n.startedAt,outputDir:e}}return null}function Nt(t){return typeof t.subscriptionType=="string"&&t.subscriptionType.trim()!==""}function ze(t){let e="";for(let i=0;i<t.length;i++){let n=t[i];if(n==="--profile"){let s=(t[i+1]||"").trim();if(!s)throw new Error("Missing value for --profile.");e=s,i+=1;}else if(n.startsWith("--profile=")){let s=n.slice(10).trim();if(!s)throw new Error("Missing value for --profile.");e=s;}}let r=String(process.env.PUBLISHER_DEPLOY_PROFILE||process.env.PUBLISHER_DEPLOYMENT_PROFILE||"").trim();return e||r||null}function zt(t,e){return {...t,...e??{}}}function Wt(t,e){return {...t,...e??{},invalidationPaths:[...(e?.invalidationPaths??t.invalidationPaths)||[]]}}function We(t,e){let r=String(e??t.deploymentTargetOverride??"").trim();if(!Nt(t)){if(r)throw new Error(`Deployment target "${r}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:t}}let i=String(r||t.defaultDeploymentProfile||"").trim();if(!i)return {name:null,profile:null,config:t};let n=t.deploymentProfiles?.[i];if(!n)throw new Error(`Unknown deployment profile "${i}". Check the linked WPSuite publisher configuration.`);return {name:i,profile:n,config:{...t,targetOrigin:n.targetOrigin??t.targetOrigin,s3:zt(t.s3,n.s3),cloudFront:Wt(t.cloudFront,n.cloudFront)}}}var qe={error:0,warn:1,info:2,debug:3},qt=/^(?:authorization|proxyAuthorization|runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken|xStaticPublisherToken|xApiKey)$/i;function ee(t){return t.replace(/((?:x-static-publisher-token|authorization|proxy-authorization|x-api-key|aws-access-key-id|aws-secret-access-key|aws-session-token)\s*:\s*)[^\r\n]+/gi,"$1[REDACTED]").replace(/\b(Basic|Bearer)\s+[^\s]+/gi,"$1 [REDACTED]").replace(/([?&](?:token|access_token|refresh_token|api_key|key|nonce)=)[^&\s]+/gi,"$1[REDACTED]").replace(/((?:"|')?(?:runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken)(?:"|')?\s*[:=]\s*(?:"|'))[^"']+/gi,"$1[REDACTED]")}function se(t){return typeof t=="string"?ee(t):Array.isArray(t)?t.map(e=>se(e)):t instanceof Error?{name:t.name,message:ee(t.message),...t.stack?{stack:ee(t.stack)}:{}}:t&&typeof t=="object"?Object.fromEntries(Object.entries(t).map(([e,r])=>[e,qt.test(e)?"[REDACTED]":se(r)])):t}function Ke(t){return t?se(t):void 0}var Be=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",ie=Be?b.join(Be,"current-progress.json"):"",Ge=Promise.resolve();function He(t){return t.includes("crawl")?"crawl":t.includes("deploy")?"deploy":t.includes("invalidate")?"invalidate":t}function Bt(t,e){let r={...t},i=new Set(["pagesQueued","pagesRendered","assetsQueued","sitemapsQueued","pagesDiscovered","assetsDiscovered","sitemapsDiscovered","pagesSaved","assetsSaved","donePages","doneAssets","doneSitemaps","changedTextFiles","uploaded","failed","index","totalFiles","pageQueue","assetQueue","sitemapQueue"]);for(let[n,s]of Object.entries(e)){let o=r[n];if(typeof o=="number"&&typeof s=="number"&&i.has(n)){r[n]=Math.max(o,s);continue}r[n]=s;}return r}function Gt(t,e,r){ie&&(Ge=Ge.then(async()=>{let i=new Date,n=i.toISOString();await D.mkdir(b.dirname(ie),{recursive:true});let s=await D.readFile(ie,"utf8").then(S=>JSON.parse(S)).catch(()=>null),o=s&&typeof s.details=="object"&&s.details?s.details:{},l=He(t),a=s?.currentStep||He(s?.source||""),c=s?.startedAt||n,u={...s?.stepDurationsSec??{}},m=s?.stepStartedAt||n;if(a&&a!==l&&s?.stepStartedAt){let S=Math.max(0,Math.round((i.getTime()-new Date(s.stepStartedAt).getTime())/1e3));u[a]=(u[a]??0)+S,m=n;}let p=Math.max(0,Math.round((i.getTime()-new Date(m).getTime())/1e3)),d=Math.max(0,Math.round((i.getTime()-new Date(c).getTime())/1e3)),g=ee(e),w=Ke(r)??{},T={checkedAt:n,source:t,message:g,details:Bt(o,w),startedAt:c,currentStep:l,stepStartedAt:m,stepElapsedSec:p,totalElapsedSec:d,stepDurationsSec:u};await D.writeFile(ie,JSON.stringify(T,null,2),"utf8");}).catch(()=>{}));}async function Ht(t,e){await D.mkdir(b.dirname(t),{recursive:true}),await D.appendFile(t,`${JSON.stringify(se(e))}
2
+ `,"utf8");}var G=class{constructor(e,r,i="info"){this.logDir=e;this.logFile=r;this.level=i;this.ensureLogFileReady();}logDir;logFile;level;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;get logPath(){return b.join(this.logDir,this.logFile)}artifactPath(e){return this.logFile.endsWith(".log.jsonl")?b.join(this.logDir,this.logFile.replace(/\.log\.jsonl$/u,`${e}.jsonl`)):b.join(this.logDir,`${this.logFile}${e}.jsonl`)}get errorsPath(){return this.artifactPath(".errors")}get rejectedPath(){return this.artifactPath(".rejected")}ensureLogFileReady(){return this.initPromise||(this.initPromise=(async()=>{await D.mkdir(this.logDir,{recursive:true}),await D.writeFile(this.logPath,"","utf8");})()),this.initPromise}enqueueTask(e){this.writeQueue=this.writeQueue.then(e).catch(r=>{this.writeError=r instanceof Error?r:new Error(String(r));});}enqueueLine(e){this.enqueueTask(async()=>{await this.ensureLogFileReady(),await D.appendFile(this.logPath,`${e}
3
+ `,"utf8");});}enqueueJsonLine(e,r){this.enqueueTask(()=>Ht(e,r));}accepts(e){return qe[e]<=qe[this.level]}push(e,r,i){if(!this.accepts(e))return;let n=ee(r),s=Ke(i),o=JSON.stringify({time:new Date().toISOString(),level:e,message:n,...s||{}});this.enqueueLine(o),e==="error"?console.error(`[ERROR] ${n}`,s?JSON.stringify(s):""):e==="warn"?console.warn(`[WARN] ${n}`,s?JSON.stringify(s):""):console.log(`[${e.toUpperCase().padEnd(5)}] ${n}`);}info(e,r){this.push("info",e,r);}progress(e,r){Gt(this.logFile,e,r);}debug(e,r){this.push("debug",e,r);}warn(e,r){this.push("warn",e,r);}reject(e,r,i,n,s){let o={kind:e,url:r,reason:i,...n?{source:n}:{},...s||{}};this.enqueueJsonLine(this.rejectedPath,o),this.accepts("debug")&&this.push("debug",`Rejected ${e} ${r}: ${i}`,o);}error(e,r){this.enqueueJsonLine(this.errorsPath,{message:e,...r||{}}),this.push("error",e,r);}mark(e){this.marks.set(e,Date.now());}endMark(e,r){let i=this.marks.get(e);if(!i)return;let n=Date.now()-i,s=Number((n/1e3).toFixed(2));this.push("info",`Timing ${e}: ${s}s`,{name:e,ms:n,seconds:s,...r||{}});}async flush(){let e=Date.now()-this.startedAt,r=Number((e/1e3).toFixed(2));if(this.push("info",`Total time: ${r}s`,{name:"total",ms:e,seconds:r}),await this.writeQueue,this.writeError)throw this.writeError}};async function Jt(t){await D.mkdir(b.dirname(t),{recursive:true});}async function Qe(t,e,r){await Jt(t);let i=b.join(b.dirname(t),`.${b.basename(t)}.${process.pid}.${Date.now()}.${Kt.randomBytes(6).toString("hex")}.tmp`);try{typeof e=="string"?await D.writeFile(i,e,r??"utf8"):await D.writeFile(i,e),await D.rename(i,t);}catch(n){throw await D.unlink(i).catch(()=>{}),n}}function Ye(t){return [".html",".htm",".css",".js",".mjs",".json",".xml",".xsl",".txt",".svg",".map",".enc",".jws"].includes(b.extname(t).toLowerCase())}function Ze(t){return t.replace(/&quot;/g,'"').replace(/&#34;/g,'"').replace(/&apos;/g,"'").replace(/&#39;/g,"'").replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">")}function H(t){return t.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function oe(t){return t.replace(/\//g,"\\/")}function Je(t){return t.replace(/\//g,"\\\\/")}function Vt(t){try{let e=new URL(t);if(e.protocol!=="http:"&&e.protocol!=="https:")return null;let r=e.pathname==="/"&&!e.search&&!e.hash?"":`${e.pathname}${e.search}${e.hash}`;return `//${e.host}${r}`}catch{return null}}function Ve(t,e,r){if(!e)return;t[e]=r;let i=oe(e),n=oe(r);t[i]=n;let s=Je(e),o=Je(r);t[s]=o;let l=H(e),a=H(r);t[l]=a;let c=H(i),u=H(n);t[c]=u;let m=H(s),p=H(o);t[m]=p;}function ae(t,e,r){Ve(t,e,r);let i=Vt(e);i&&i!==e&&Ve(t,i,r);}function Xe(t,e){try{return new URL(t,e==="."?"https://relative.invalid":e).pathname}catch{return t.startsWith("/")?t:`/${t.replace(/^\.\//,"")}`}}function et(t,e,r){if(!e)return r;let i=b.dirname(b.resolve(e)),n=b.resolve(t,r.replace(/^\/+/,"")),s=b.relative(i,n).replace(/\\/g,"/");return s?(s.startsWith(".")||(s=`./${s}`),s):"."}var er=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],tr=new Set([".html",".htm"]),tt="WPSuite.io Static Publisher",rr=tt.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function nr(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function ir(t){return t.startsWith("//")||t.startsWith("\\/\\/")||t.startsWith("\\\\/\\\\/")}function sr(t,e,r){return ir(e)?t.replace(new RegExp(`(?<!:)${nr(e)}`,"g"),r):t.split(e).join(r)}function or(t,e,r){if(t.wpsuite?.siteSettings?.subscriber===true||t.wpsuite?.subscriptionType==="PROFESSIONAL"||t.wpsuite?.subscriptionType==="AGENCY"||!r)return false;let n=b.extname(r).toLowerCase();return tr.has(n)?/<head\b|<html\b|<!doctype html/i.test(e):false}function ar(t){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${rr}\\2)[^>]*\\/?>`,"i").test(t))return t;let e=`<meta name="generator" content="${tt}" />`;if(t.match(/(\r?\n)([ \t]*)<\/head>/i))return t.replace(/(\r?\n)([ \t]*)<\/head>/i,`$1$2${e}$1$2</head>`);let i=t.includes(`\r
4
4
  `)?`\r
5
5
  `:`
6
- `;return e.replace(/<head\b[^>]*>/i,n=>`${n}${i} ${t}`)}function Xt(e){let t=Je(e).trim();return t.startsWith("{")||t.startsWith("[")||t.includes("\\/")||/"@context"|"@type"/.test(t)}function er(e,t){let r=[...new Set(t)].filter(s=>s.includes("/")&&!/\\+\//.test(s)).map(s=>[s,X(s)]).filter(([s,o])=>s!==o).sort((s,o)=>o[0].length-s[0].length);if(!r.length)return e;let i=s=>{let o=s;for(let[l,a]of r)o=o.split(l).join(a);return o},n=e.replace(/(<script\b[^>]*\btype=["']application\/(?:ld\+)?json["'][^>]*>)([\s\S]*?)(<\/script>)/gi,(s,o,l,a)=>`${o}${i(l)}${a}`);return n=n.replace(/(<meta\b[^>]*\bcontent=(['"]))([\s\S]*?)(\2[^>]*>)/gi,(s,o,l,a,u)=>Xt(a)?`${o}${i(a)}${u}`:s),n}function tr(e,t){if(!t)return null;let r=b.dirname(b.resolve(t)),i=b.resolve(e.outputDir),n=b.relative(r,i).replace(/\\/g,"/");return n?(n.startsWith(".")||(n=`./${n}`),n.endsWith("/")?n:`${n}/`):"./"}function rr(e,t,r){let i=tr(t,r);if(!i)return e;let n=e;for(let s of Ht){let o=s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),l=new RegExp(`(?:\\.{1,}/)+${o}`,"g"),a=new RegExp(`(?<!\\.)/${o}`,"g");n=n.replace(l,`${i}${s}`).replace(a,`${i}${s}`);let u=s.replace(/\//g,"\\/"),c=i.replace(/\//g,"\\/"),p=u.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),g=new RegExp(`(?:\\.{1,}\\\\/)+${p}`,"g"),d=new RegExp(`(?:\\.{1,}(?:\\\\/|\\\\))+${p}`,"g"),y=new RegExp(`(?<![\\.\\\\])\\/${p}`,"g");n=n.replace(g,`${c}${u}`).replace(d,`${c}${u}`).replace(y,`${c}${u}`);}return n}function ye(e,t,r){if(e.urlRewriteMode==="absolute")return t;let i=(()=>{try{return new URL(t,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin)}catch{return null}})(),n=i?i.pathname:Ke(t,e.targetOrigin),s=i?`${i.search}${i.hash}`:"";return e.urlRewriteMode==="root-relative"?`${n}${s}`:`${Ge(e.outputDir,r,n)}${s}`}function me(e,t,r,i,n){ee(e,r,ye(t,i,n));}function nr(e,t,r,i,n){if(!r||r===i)return;ee(e,r,i);let s=ye(t,r,n),o=ye(t,i,n);ee(e,s,o);}function ir(e,t,r,i,n={}){let s=e,o={};me(o,t,t.sourceOrigin,t.targetOrigin,i);for(let[a,u]of Object.entries(t.extraReplacements))me(o,t,a,u,i);for(let[a,u]of Object.entries(r))me(o,t,a,u,i);for(let[a,u]of Object.entries(n)){let c=r[a];!c||c===u||nr(o,t,u,c,i);}let l=Object.entries(o).sort((a,u)=>u[0].length-a[0].length);for(let[a,u]of l)s=Qt(s,a,u);return t.urlRewriteMode!=="absolute"&&(s=rr(s,t,i)),s=er(s,l.map(([,a])=>a)),Yt(t,s,i)&&(s=Zt(s)),s}async function sr(e,t,r,i){let n=b.join(e.outputDir,i),s=b.extname(n).toLowerCase(),o;try{o=await x.readFile(n,"utf8");}catch(u){if(u.code!=="ENOENT")throw u;return {changed:false}}let a=ir(o,e,t,s===".js"||s===".mjs"?void 0:n,r);return a!==o?(await qe(n,a,"utf8"),{changed:true}):{changed:false}}async function or(e,t){return await new Promise((r,i)=>{let n=a=>{if(l(),a?.error){i(new Error(a.error));return}r({changed:!!a?.changed});},s=a=>{l(),i(a);},o=a=>{l(),a!==0&&i(new Error(`Rewrite worker exited with code ${a}.`));},l=()=>{e.off("message",n),e.off("error",s),e.off("exit",o);};e.on("message",n),e.on("error",s),e.on("exit",o),e.postMessage({file:t});})}async function Qe(e,t,r={}){let n=(r.files?[...new Set(r.files)].map(g=>b.resolve(g)).filter(g=>g.startsWith(b.resolve(e.outputDir))).map(g=>b.relative(e.outputDir,g).replace(/\\/g,"/")).filter(g=>g.length>0):await Wt(["**/*"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(g=>g!=="asset-map.json"&&He(g)),s=0;if(n.length===0)return 0;let o=Math.max(1,Number(e.rewriteConcurrency||e.assetDownloadConcurrency||1)),l=Math.max(1,Math.min(o,n.length,availableParallelism()));await r.onStart?.({totalFiles:n.length,workerCount:l});let a=0,u=0,c=null;if(l===1){for(let g of n){let d=await sr(e,t,r.previousAssetMap??{},g);d.changed&&(s+=1),u+=1,await r.onProgress?.({index:u,totalFiles:n.length,changedTextFiles:s,file:g,changed:d.changed});}return s}let p=Array.from({length:l},async()=>{let g=new Worker(new URL("./rewrite-worker.js",import.meta.url),{workerData:{config:e,assetMap:t,previousAssetMap:r.previousAssetMap??{}}});try{for(;!c;){let d=a;if(d>=n.length)return;a+=1;let y=n[d],w=await or(g,y);w.changed&&(s+=1),u+=1,await r.onProgress?.({index:u,totalFiles:n.length,changedTextFiles:s,file:y,changed:w.changed});}}catch(d){throw c=d instanceof Error?d:new Error(String(d)),c}finally{await g.terminate().catch(()=>{});}});return await Promise.all(p),s}var et="wpsuite-sha256",tt="wpsuite-normalized-sha256",hr=/(?:^|[-_:.])(id|uid|nonce|token|hash|instance)(?:$|[-_:.])/i,Re=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,mr=["SIGINT","SIGTERM"],ne=new Set,te=new Map,Ye=null;function yr(e){ne.add(e);let t=false;return ()=>{t||(t=true,ne.delete(e));}}async function wr(){let e=[...ne];e.length!==0&&await Promise.allSettled(e.map(async t=>{ne.delete(t),await x.rm(t,{recursive:true,force:true});}));}function br(){if(!(te.size>0))for(let e of mr){let t=()=>{Ye||(Ye=wr().finally(()=>{for(let[r,i]of te)process.off(r,i);te.clear(),process.exit(1);}));};te.set(e,t),process.on(e,t);}}br();function Pr(e,t){let r=t.trim();return r?b.join(b.resolve(r),"crawl-manifest.json"):b.join(b.resolve(e),".crawl-manifest.json")}function rt(e){return b.normalize(e).split(b.sep).join("/")}function Ze(e,t){let r=b.relative(b.resolve(e),b.resolve(t));return !r||r.startsWith("..")||b.isAbsolute(r)?null:rt(r)}async function Sr(e,t,r){let i=Pr(e.outputDir,t),n;try{n=await x.readFile(i,"utf8");}catch(c){return c.code==="ENOENT"?(r.warn("Deploy rewrite guard disabled because the crawl manifest is missing.",{manifestPath:i,source:"deploy-guard"}),null):(r.warn("Deploy rewrite guard disabled because the crawl manifest could not be read.",{manifestPath:i,error:c instanceof Error?c.message:String(c),source:"deploy-guard"}),null)}let s;try{s=JSON.parse(n);}catch(c){return r.warn("Deploy rewrite guard disabled because the crawl manifest is invalid JSON.",{manifestPath:i,error:c instanceof Error?c.message:String(c),source:"deploy-guard"}),null}let o=new Map,l=new Map,a=0,u=0;for(let[c,p]of Object.entries(s.pages||{})){let g=String(p?.outputPath||"").trim(),d=g?Ze(e.outputDir,g):null;if(d){let y=typeof p?.contentType=="string"&&p.contentType?p.contentType:null;y&&l.set(d,y);}!g||p?.rewriteComplete===true||d&&(a++,o.has(d)||o.set(d,{kind:"page",sourceUrl:c,outputPath:b.resolve(g),relativePath:d}));}for(let[c,p]of Object.entries(s.assets||{})){let g=String(p?.outputPath||"").trim(),d=p?.isText===true,y=g?Ze(e.outputDir,g):null;if(y){let w=typeof p?.contentType=="string"&&p.contentType?p.contentType:null;w&&l.set(y,w);}!g||!d||p?.rewriteComplete===true||y&&(u++,o.has(y)||o.set(y,{kind:"asset",sourceUrl:c,outputPath:b.resolve(g),relativePath:y}));}return {manifestPath:i,byRelativePath:o,contentTypeByRelativePath:l,pendingPages:a,pendingTextAssets:u}}function vr(e){return e&&(Re.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)?"__hex__":/^[0-9]{8,}$/.test(e)?"__num__":/^(?=.*[a-z])(?=.*\d)[a-z0-9]{8,}$/i.test(e)?"__alnum__":e)}function be(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,r)=>r%2===1?t:vr(t)).join("")}function Rr(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,r)=>r%2===1?t:Re.test(t)?"__uuid__":/^[0-9a-f]{6,}$/i.test(t)?"__hex__":/^[0-9]{6,}$/.test(t)?"__num__":/^[a-z0-9_-]{6,}$/i.test(t)?"__id__":t).join("")}function ie(e,t){return Array.isArray(e)?e.map(r=>ie(r,t)):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([r,i])=>[r,ie(i,r)])):typeof e!="string"?e:t&&hr.test(t)?Rr(e):Re.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)||/^[0-9]{8,}$/.test(e)?be(e):e}function Dr(e){return e.replace(/((?:https?:\/\/[^"'\s>]+|\/)(?:wp-content|wp-includes)\/[^"'\s>?]+)\?ver=[A-Za-z0-9._-]+(?=$|[#"'])/gi,"$1")}function xr(e,t){let r=t.trim();if((r.startsWith("{")||r.startsWith("[")||r.startsWith('"'))&&e.startsWith("data-"))try{let n=JSON.parse(r);return JSON.stringify(ie(n))}catch{}if(e.startsWith("data-")&&/^[A-Za-z0-9+/=]+$/.test(r))try{let n=Buffer.from(r,"base64").toString("utf8"),s=JSON.parse(n),o=JSON.stringify(ie(s));return Buffer.from(o,"utf8").toString("base64")}catch{}return t}function $r(e,t){let r=e.toLowerCase();return r==="class"?t.split(/(\s+)/).map((i,n)=>n%2===1?i:be(i)).join(""):r==="id"||r==="for"||r==="aria-controls"||r==="aria-labelledby"||r==="aria-describedby"||r==="aria-owns"||r==="aria-activedescendant"||r==="name"?be(t):r==="href"||r==="src"?Dr(t):xr(r,t)}function Tr(e){return e.replace(/<([a-z][^\s/>]*)([^<>]*?)>/gi,(t,r,i)=>{if(r.startsWith("/"))return t;let n=i.replace(/\s([:@a-zA-Z_][-:.a-zA-Z0-9_]*?)=(['"])([\s\S]*?)\2/g,(s,o,l,a)=>{let u=$r(o,a);return ` ${o}=${l}${u}${l}`});return `<${r}${n}>`})}function we(e){let t=b.extname(e).toLowerCase();return [".html",".htm",".xml",".xsl",".txt"].includes(t)||["robots.txt","sitemap.xml"].includes(b.basename(e))}function re(e,t){return [e.replace(/^\/+|\/+$/g,""),t.replace(/^\/+/,"")].filter(Boolean).join("/")}async function Cr(e,t,r,i){let n=new Map,s,o=0;do{o++;let l=await O(`S3 list objects page ${o} for s3://${t}/${r||""}`,3e5,({httpOptions:a})=>e.send(new ListObjectsV2Command({Bucket:t,Prefix:r||void 0,ContinuationToken:s}),a));for(let a of l.Contents||[]){if(!a.Key)continue;let u=(a.ETag||"").replace(/^"|"$/g,"").toLowerCase(),c=Number(a.Size??0);n.set(a.Key,{etag:u,size:c});}s=l.NextContinuationToken,i.debug(`Listed S3 keys page ${o}: ${n.size} keys so far`);}while(s);return n}async function Er(e,t,r,i){if(i.has(r))return i.get(r)??null;try{let n=await O(`S3 head object ${r}`,3e5,({httpOptions:o})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),o)),s={etag:(n.ETag||"").replace(/^"|"$/g,"").toLowerCase(),size:Number(n.ContentLength??0)};return i.set(r,s),s}catch{return i.set(r,null),null}}async function Mr(e,t,r,i){let n=[...new Set(Array.from(r).filter(Boolean))];if(n.length===0)return i.info("No explicitly tombstoned S3 objects to delete."),[];i.info(`Deleting ${n.length} explicitly tombstoned S3 object(s)\u2026`);for(let s=0;s<n.length;s+=1e3){let o=n.slice(s,s+1e3);if(o.length!==0){await O(`S3 delete objects batch ${Math.floor(s/1e3)+1}`,3e5,({httpOptions:l})=>e.send(new DeleteObjectsCommand({Bucket:t,Delete:{Objects:o.map(a=>({Key:a}))}}),l)),i.info(`Deleted batch of ${o.length} object(s).`);for(let l of o)i.debug(` deleted: ${l}`);}}return n}function kr(e){return createHash("md5").update(e).digest("hex")}function Xe(e){return createHash("sha256").update(e).digest("hex")}function Pe(e){return createHash("sha256").update(Tr(e.toString("utf8"))).digest("hex")}async function Ir(e,t,r,i){if(i.has(r))return i.get(r)??null;try{let o=((await O(`S3 head object metadata ${r}`,3e5,({httpOptions:l})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),l))).Metadata?.[et]||"").trim().toLowerCase()||null;return i.set(r,o),o}catch{return i.set(r,null),null}}async function Ar(e,t,r,i,n){if(i.has(r))return i.get(r)??null;try{let o=((await O(`S3 head object normalized metadata ${r}`,3e5,({httpOptions:l})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),l))).Metadata?.[tt]||"").trim().toLowerCase();if(o)return i.set(r,o),o}catch{return i.set(r,null),null}if(n.has(r)){let s=n.get(r)??null;return i.set(r,s),s}try{let o=await(await O(`S3 get object ${r}`,3e5,({httpOptions:a})=>e.send(new GetObjectCommand({Bucket:t,Key:r}),a))).Body?.transformToByteArray(),l=o?Pe(Buffer.from(o)):null;return n.set(r,l),i.set(r,l),l}catch{return n.set(r,null),i.set(r,null),null}}function Lr(e){return e.replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"")||"profile"}function Or(e,t){let r=e,i=Object.entries(t).sort((n,s)=>s[0].length-n[0].length);for(let[n,s]of i)n&&(r=r.split(n).join(s));return r}async function jr(e,t,r,i){let n=b.join(e,"asset-map.json"),s;try{s=await x.readFile(n,"utf8");}catch(a){if(a.code==="ENOENT")return;throw a}let o=JSON.parse(s);if(!o||typeof o!="object")return;let l=Object.fromEntries(Object.entries(o).map(([a,u])=>{let c=String(u??"");return t.urlRewriteMode==="absolute"&&t.targetOrigin&&r.targetOrigin&&t.targetOrigin!==r.targetOrigin&&(c=c.split(t.targetOrigin).join(r.targetOrigin)),c=Or(c,i),[a,c]}));await x.writeFile(n,JSON.stringify(l,null,2),"utf8");}async function Fr(e,t,r,i,n){let s=e.targetOrigin!==t.targetOrigin,o=Object.keys(i).length>0;if(!r||!s&&!o)return {outputDir:e.outputDir,cleanup:async()=>{}};if(s&&e.urlRewriteMode!=="absolute")throw new Error(`Deployment profile "${r}" changes targetOrigin, but the base crawl output must use urlRewriteMode "absolute" for multi-target redeploys.`);let l=b.join(b.dirname(e.outputDir),`.deploy-profile-${Lr(r)}-${Date.now()}`),a=yr(l);n.info(`Preparing deployment profile "${r}" from existing crawl output to ${l}...`);try{await x.rm(l,{recursive:!0,force:!0}),await x.cp(e.outputDir,l,{recursive:!0});let u={...e,sourceOrigin:e.targetOrigin,targetOrigin:t.targetOrigin,extraReplacements:i,outputDir:l},c=1;n.info(`Rewriting text files for deployment profile "${r}"...`);let p=await Qe(u,{},{onStart:({totalFiles:g,workerCount:d})=>{c=d,n.info(`Rewriting ${g} text file(s) for deployment profile "${r}" with ${d} worker(s).`);},onProgress:({index:g,totalFiles:d,changedTextFiles:y,file:w,changed:v})=>{let k=`Rewriting [${g}/${d}] ${w} (${v?"rewrote":"checked"}; ${y} changed so far)`;d<=200||g<=5||g%10===0||g===d?(n.progress(k,{file:w,index:g,totalFiles:d,changed:v,changedTextFiles:y,workerCount:c}),n.info(k)):n.debug(k);}});return await jr(l,e,t,i),n.info(`Prepared deployment profile "${r}" in ${l} (${p} rewritten text file(s)).`),{outputDir:l,cleanup:async()=>{a(),await x.rm(l,{recursive:!0,force:!0});}}}catch(u){throw n.error(`Failed to prepare deployment profile "${r}" temporary output.`,{tempDir:l,outputDir:e.outputDir,error:u instanceof Error?u.message:String(u),...u instanceof Error&&u.stack?{stack:u.stack}:{}}),await x.rm(l,{recursive:true,force:true}).catch(()=>{}),a(),await n.flush().catch(()=>{}),u}}async function _r(){let e=await de(),t=je(process.argv.slice(2)),r=Fe(e,t),i=b.resolve(r.config.logDir||"logs"),n=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",s=n?b.join(b.resolve(n),"deploy-diff.json"):"",o=new G(i,"deploy.log.jsonl",r.config.logLevel??"info"),l=r.profile?.extraReplacements??{},a=async()=>{};try{let u=await Oe(e);if(u)throw new Error(`Deployment blocked: the crawl started at ${u.startedAt} did not complete its final rewrite. Run a successful crawl or resume the rewrite before deploying.`);let c=await Fr(e,r.config,r.name,l,o);a=c.cleanup;let p={...r.config,outputDir:c.outputDir},g=p.s3SyncMode??"sdk-upload-delete",d=await Ae(e),y=c.outputDir!==e.outputDir&&(e.targetOrigin!==r.config.targetOrigin||Object.keys(l).length>0),w=!!d&&!d.fullSyncRequired&&!y;r.name&&o.info(`Using deployment profile: ${r.name}`),o.info(`Starting deploy \u2014 s3://${p.s3.bucket}/${p.s3.prefix||""} (region: ${p.s3.region})`),o.info(`S3 sync mode: ${g}`),d&&d.fullSyncRequired?o.info("Deploy plan requests a full sync; scanning the whole output tree."):d&&y?o.info("Ignoring incremental deploy plan because deployment-profile rewrites require a full sync."):w&&o.info(`Using incremental deploy plan: ${d.changedFiles.length} changed file(s), ${d.deletedFiles.length} deleted file(s).`),o.mark("deploy");let v=new S3Client({region:p.s3.region}),R=c.outputDir===e.outputDir?await Sr(e,n,o):null,k=w?(await Promise.all(d.changedFiles.map(async m=>{try{return (await x.stat(b.join(p.outputDir,m))).isFile()?m:null}catch{return null}}))).filter(m=>m!==null):await Wt(["**/*"],{cwd:p.outputDir,onlyFiles:!0,dot:!0}),A=[],Q=[],h=k.filter(m=>{let T=R?.byRelativePath.get(rt(m));if(!T)return !0;let f=re(p.s3.prefix,m);return A.push(m),Q.push(f),o.reject(T.kind,T.sourceUrl||m,"rewrite not completed; deploy upload blocked","deploy-guard",{file:m,key:f,outputPath:T.outputPath,manifestPath:R?.manifestPath}),!1}),De=new Set((w?h:k).map(m=>re(p.s3.prefix,m)));R&&(R.pendingPages>0||R.pendingTextAssets>0)&&o.info(`Deploy rewrite guard loaded ${R.pendingPages} pending page(s) and ${R.pendingTextAssets} pending text asset(s) from crawl manifest.`,{manifestPath:R.manifestPath,pendingPages:R.pendingPages,pendingTextAssets:R.pendingTextAssets,source:"deploy-guard"}),A.length>0&&o.warn(`Deploy rewrite guard blocked ${A.length} file(s) from upload because final crawl rewrite is incomplete.`,{rejected:A.length,manifestPath:R?.manifestPath,source:"deploy-guard"}),w?o.info(`Using ${h.length} eligible planned changed file(s) from deploy plan for upload${A.length>0?` (${A.length} blocked by deploy rewrite guard)`:""}.`):o.info(`Found ${k.length} file(s) in output directory "${p.outputDir}"${A.length>0?`; ${h.length} remain eligible for upload after deploy rewrite guard filtering`:""}.`),o.progress("Deploy preflight: comparing local output with remote S3 state.",{phase:"deploy",mode:g,totalFiles:h.length,scannedFiles:k.length,rejected:A.length,deploymentProfile:r.name||"",s3Bucket:p.s3.bucket,s3Prefix:p.s3.prefix||"",targetedPlan:w,deployGuardEnabled:c.outputDir===e.outputDir});let $=0,S=0,F=0,se=[],q=[],oe=[],ae=[],_=[],U=new Map,nt=new Map,it=new Map,st=new Map,ot=new Map;w||(o.info("Listing existing S3 objects to detect unchanged files..."),U=await Cr(v,p.s3.bucket,p.s3.prefix,o),o.info(`Found ${U.size} existing key(s) in S3.`));let at=async m=>U.has(m)?U.get(m)??null:w?Er(v,p.s3.bucket,m,nt):null;for(let m of h){let T=b.join(p.outputDir,m),f=re(p.s3.prefix,m),C=R?.contentTypeByRelativePath.get(m),H=C&&C!=="application/octet-stream"?C:fr.lookup(m)||C||"application/octet-stream",lt=we(m)?p.s3.htmlCacheControl:p.s3.assetCacheControl,P=$+S+F+1;h.length<=200||P<=5||P%10===0||P===h.length?(o.progress(`Uploading [${P}/${h.length}] ${f} (${H})`,{file:m,key:f,index:P,totalFiles:h.length,contentType:H,mode:g}),o.info(`Uploading [${P}/${h.length}] ${f} (${H})`)):o.debug(`Uploading [${P}/${h.length}] ${f} (${H})`);try{let E=await x.readFile(T),J=await at(f),K=null,N=null;if(J){let le=E.byteLength;if(J.size===le){let Y=J.etag.includes("-");if(J.etag!==""&&!Y){let ut=kr(E);if(J.etag===ut){S++,h.length<=200||S<=5||S%25===0?(o.progress(`Skipped unchanged [${P}/${h.length}] ${f}`,{file:m,key:f,index:P,totalFiles:h.length,uploaded:$,skipped:S,failed:F,mode:g,reason:"etag-md5-match"}),o.info(`Skipped unchanged [${P}/${h.length}] ${f}`)):o.debug(`Skipped unchanged [${P}/${h.length}] ${f}`),q.push(f);continue}}K=Xe(E);let xe=await Ir(v,p.s3.bucket,f,it);if(xe&&xe===K){S++,h.length<=200||S<=5||S%25===0?(o.progress(`Skipped unchanged [${P}/${h.length}] ${f}`,{file:m,key:f,index:P,totalFiles:h.length,uploaded:$,skipped:S,failed:F,mode:g,reason:"metadata-sha256-match"}),o.info(`Skipped unchanged [${P}/${h.length}] ${f}`)):o.debug(`Skipped unchanged [${P}/${h.length}] ${f}`),q.push(f);continue}}if(we(m)){N=Pe(E);let Y=await Ar(v,p.s3.bucket,f,st,ot);if(Y&&Y===N){S++,h.length<=200||S<=5||S%25===0?(o.progress(`Skipped semantically unchanged [${P}/${h.length}] ${f}`,{file:m,key:f,index:P,totalFiles:h.length,uploaded:$,skipped:S,failed:F,mode:g,reason:"normalized-html-match"}),o.info(`Skipped semantically unchanged [${P}/${h.length}] ${f}`)):o.debug(`Skipped semantically unchanged [${P}/${h.length}] ${f}`),q.push(f);continue}}}K=K??Xe(E),N=N??(we(m)?Pe(E):null),await O(`S3 upload ${f}`,9e5,({abortController:le})=>new Upload({client:v,abortController:le,params:{Bucket:p.s3.bucket,Key:f,Body:E,ContentType:H,CacheControl:lt,Metadata:{[et]:K,...N?{[tt]:N}:{}}}}).done()),$++,se.push(f),($%25===0||$===h.length)&&(o.progress(`Uploaded ${$}/${h.length} file(s)\u2026`,{uploaded:$,skipped:S,failed:F,totalFiles:h.length,mode:g}),o.info(`Uploaded ${$}/${h.length} file(s)\u2026`));}catch(E){F++,oe.push(f),o.error(`Failed to upload ${f}: ${E.message}`,{key:f,error:String(E)});}}if(o.info(`Upload complete: ${$} uploaded, ${S} skipped unchanged, ${Q.length} rejected by guard, ${F} failed.`),g==="sdk-upload-delete"){let m=new Set((d?.deletedFiles??[]).map(f=>re(p.s3.prefix,f))),T=[...m].filter(f=>!De.has(f));if(!w&&(_=[...U.keys()].filter(C=>!De.has(C)).filter(C=>!m.has(C)),_.length>0)){o.warn(`Protected ${_.length} remote-only S3 object(s) from deletion because the crawl did not provide explicit tombstones.`,{protected:_.length,source:"deploy-delete-guard"});for(let C of _)o.debug(` protected remote-only object: ${C}`);}ae=await Mr(v,p.s3.bucket,w?T:T.filter(f=>U.has(f)),o);}else o.info("Sync mode is sdk-upload-only \u2014 skipping stale object deletion.");s&&(await x.mkdir(b.dirname(s),{recursive:!0}),await x.writeFile(s,JSON.stringify({generatedAt:new Date().toISOString(),mode:g,summary:{totalFiles:k.length,eligibleFiles:h.length,uploaded:se.length,skipped:q.length,rejected:Q.length,failed:oe.length,deleted:ae.length,protected:_.length,...r.name?{profile:r.name}:{}},uploadedKeys:se,skippedKeys:q,rejectedKeys:Q,failedKeys:oe,deletedKeys:ae,protectedRemoteOnlyKeys:_},null,2),"utf8")),await Le(e),o.endMark("deploy"),await o.flush();}finally{await a().catch(()=>{});}}_r().catch(async e=>{console.error(e);try{let r=(await de().catch(()=>null))?.logDir??"logs",i=new G(r,"deploy.log.jsonl","debug");i.error(`Unhandled error: ${e instanceof Error?e.message:String(e)}`,e instanceof Error?{stack:e.stack}:void 0),await i.flush();}catch{}process.exit(1);});
6
+ `;return t.replace(/<head\b[^>]*>/i,n=>`${n}${i} ${e}`)}function lr(t){let e=Ze(t).trim();return e.startsWith("{")||e.startsWith("[")||e.includes("\\/")||/"@context"|"@type"/.test(e)}function ur(t,e){let r=[...new Set(e)].filter(s=>s.includes("/")&&!/\\+\//.test(s)).map(s=>[s,oe(s)]).filter(([s,o])=>s!==o).sort((s,o)=>o[0].length-s[0].length);if(!r.length)return t;let i=s=>{let o=s;for(let[l,a]of r)o=o.split(l).join(a);return o},n=t.replace(/(<script\b[^>]*\btype=["']application\/(?:ld\+)?json["'][^>]*>)([\s\S]*?)(<\/script>)/gi,(s,o,l,a)=>`${o}${i(l)}${a}`);return n=n.replace(/(<meta\b[^>]*\bcontent=(['"]))([\s\S]*?)(\2[^>]*>)/gi,(s,o,l,a,c)=>lr(a)?`${o}${i(a)}${c}`:s),n}function cr(t,e){if(!e)return null;let r=b.dirname(b.resolve(e)),i=b.resolve(t.outputDir),n=b.relative(r,i).replace(/\\/g,"/");return n?(n.startsWith(".")||(n=`./${n}`),n.endsWith("/")?n:`${n}/`):"./"}function gr(t,e,r){let i=cr(e,r);if(!i)return t;let n=t;for(let s of er){let o=s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),l=new RegExp(`(?:\\.{1,}/)+${o}`,"g"),a=new RegExp(`(?<!\\.)/${o}`,"g");n=n.replace(l,`${i}${s}`).replace(a,`${i}${s}`);let c=s.replace(/\//g,"\\/"),u=i.replace(/\//g,"\\/"),m=c.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),p=new RegExp(`(?:\\.{1,}\\\\/)+${m}`,"g"),d=new RegExp(`(?:\\.{1,}(?:\\\\/|\\\\))+${m}`,"g"),g=new RegExp(`(?<![\\.\\\\])\\/${m}`,"g");n=n.replace(p,`${u}${c}`).replace(d,`${u}${c}`).replace(g,`${u}${c}`);}return n}function Se(t,e,r){if(t.urlRewriteMode==="absolute")return e;let i=(()=>{try{return new URL(e,t.targetOrigin==="."?"https://relative.invalid":t.targetOrigin)}catch{return null}})(),n=i?i.pathname:Xe(e,t.targetOrigin),s=i?`${i.search}${i.hash}`:"";return t.urlRewriteMode==="root-relative"?`${n}${s}`:`${et(t.outputDir,r,n)}${s}`}function Pe(t,e,r,i,n){ae(t,r,Se(e,i,n));}function pr(t,e,r,i,n){if(!r||r===i)return;ae(t,r,i);let s=Se(e,r,n),o=Se(e,i,n);ae(t,s,o);}function dr(t,e,r,i,n={}){let s=t,o={};Pe(o,e,e.sourceOrigin,e.targetOrigin,i);for(let[a,c]of Object.entries(e.extraReplacements))Pe(o,e,a,c,i);for(let[a,c]of Object.entries(r))Pe(o,e,a,c,i);for(let[a,c]of Object.entries(n)){let u=r[a];!u||u===c||pr(o,e,c,u,i);}let l=Object.entries(o).sort((a,c)=>c[0].length-a[0].length);for(let[a,c]of l)s=sr(s,a,c);return e.urlRewriteMode!=="absolute"&&(s=gr(s,e,i)),s=ur(s,l.map(([,a])=>a)),or(e,s,i)&&(s=ar(s)),s}async function fr(t,e,r,i){let n=b.join(t.outputDir,i),s=b.extname(n).toLowerCase(),o;try{o=await D.readFile(n,"utf8");}catch(c){if(c.code!=="ENOENT")throw c;return {changed:false}}let a=dr(o,t,e,s===".js"||s===".mjs"?void 0:n,r);return a!==o?(await Qe(n,a,"utf8"),{changed:true}):{changed:false}}async function mr(t,e){return await new Promise((r,i)=>{let n=a=>{if(l(),a?.error){i(new Error(a.error));return}r({changed:!!a?.changed});},s=a=>{l(),i(a);},o=a=>{l(),a!==0&&i(new Error(`Rewrite worker exited with code ${a}.`));},l=()=>{t.off("message",n),t.off("error",s),t.off("exit",o);};t.on("message",n),t.on("error",s),t.on("exit",o),t.postMessage({file:e});})}async function rt(t,e,r={}){let n=(r.files?[...new Set(r.files)].map(p=>b.resolve(p)).filter(p=>p.startsWith(b.resolve(t.outputDir))).map(p=>b.relative(t.outputDir,p).replace(/\\/g,"/")).filter(p=>p.length>0):await Qt(["**/*"],{cwd:t.outputDir,onlyFiles:true,dot:true})).filter(p=>p!=="asset-map.json"&&Ye(p)),s=0;if(n.length===0)return 0;let o=Math.max(1,Number(t.rewriteConcurrency||t.assetDownloadConcurrency||1)),l=Math.max(1,Math.min(o,n.length,availableParallelism()));await r.onStart?.({totalFiles:n.length,workerCount:l});let a=0,c=0,u=null;if(l===1){for(let p of n){let d=await fr(t,e,r.previousAssetMap??{},p);d.changed&&(s+=1),c+=1,await r.onProgress?.({index:c,totalFiles:n.length,changedTextFiles:s,file:p,changed:d.changed});}return s}let m=Array.from({length:l},async()=>{let p=new Worker(new URL("./rewrite-worker.js",import.meta.url),{workerData:{config:t,assetMap:e,previousAssetMap:r.previousAssetMap??{}}});try{for(;!u;){let d=a;if(d>=n.length)return;a+=1;let g=n[d],w=await mr(p,g);w.changed&&(s+=1),c+=1,await r.onProgress?.({index:c,totalFiles:n.length,changedTextFiles:s,file:g,changed:w.changed});}}catch(d){throw u=d instanceof Error?d:new Error(String(d)),u}finally{await p.terminate().catch(()=>{});}});return await Promise.all(m),s}var ot="wpsuite-sha256",at="wpsuite-normalized-sha256",Dr=/(?:^|[-_:.])(id|uid|nonce|token|hash|instance)(?:$|[-_:.])/i,Te=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,xr=["SIGINT","SIGTERM"],ce=new Set,le=new Map,nt=null;function $r(t){ce.add(t);let e=false;return ()=>{e||(e=true,ce.delete(t));}}async function Tr(){let t=[...ce];t.length!==0&&await Promise.allSettled(t.map(async e=>{ce.delete(e),await D.rm(e,{recursive:true,force:true});}));}function Er(){if(!(le.size>0))for(let t of xr){let e=()=>{nt||(nt=Tr().finally(()=>{for(let[r,i]of le)process.off(r,i);le.clear(),process.exit(1);}));};le.set(t,e),process.on(t,e);}}Er();function Cr(t,e){let r=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(r)return b.resolve(r);let i=e.trim();return i?b.join(b.resolve(i),"crawl-manifest.json"):b.join(b.resolve(t),".crawl-manifest.json")}function lt(t){return b.normalize(t).split(b.sep).join("/")}function it(t,e){let r=b.relative(b.resolve(t),b.resolve(e));return !r||r.startsWith("..")||b.isAbsolute(r)?null:lt(r)}async function kr(t,e,r){let i=Cr(t.outputDir,e),n;try{n=await D.readFile(i,"utf8");}catch(u){return u.code==="ENOENT"?(r.warn("Deploy rewrite guard disabled because the crawl manifest is missing.",{manifestPath:i,source:"deploy-guard"}),null):(r.warn("Deploy rewrite guard disabled because the crawl manifest could not be read.",{manifestPath:i,error:u instanceof Error?u.message:String(u),source:"deploy-guard"}),null)}let s;try{s=JSON.parse(n);}catch(u){return r.warn("Deploy rewrite guard disabled because the crawl manifest is invalid JSON.",{manifestPath:i,error:u instanceof Error?u.message:String(u),source:"deploy-guard"}),null}let o=new Map,l=new Map,a=0,c=0;for(let[u,m]of Object.entries(s.pages||{})){let p=String(m?.outputPath||"").trim(),d=p?it(t.outputDir,p):null;if(d){let g=typeof m?.contentType=="string"&&m.contentType?m.contentType:null;g&&l.set(d,g);}!p||m?.rewriteComplete===true||d&&(a++,o.has(d)||o.set(d,{kind:"page",sourceUrl:u,outputPath:b.resolve(p),relativePath:d}));}for(let[u,m]of Object.entries(s.assets||{})){let p=String(m?.outputPath||"").trim(),d=m?.isText===true,g=p?it(t.outputDir,p):null;if(g){let w=typeof m?.contentType=="string"&&m.contentType?m.contentType:null;w&&l.set(g,w);}!p||!d||m?.rewriteComplete===true||g&&(c++,o.has(g)||o.set(g,{kind:"asset",sourceUrl:u,outputPath:b.resolve(p),relativePath:g}));}return {manifestPath:i,byRelativePath:o,contentTypeByRelativePath:l,pendingPages:a,pendingTextAssets:c}}function Ar(t){return t&&(Te.test(t)?"__uuid__":/^[0-9a-f]{6,}$/i.test(t)?"__hex__":/^[0-9]{8,}$/.test(t)?"__num__":/^(?=.*[a-z])(?=.*\d)[a-z0-9]{8,}$/i.test(t)?"__alnum__":t)}function Re(t){return t.split(/([_:.\-/=?&#]+)/g).map((e,r)=>r%2===1?e:Ar(e)).join("")}function Mr(t){return t.split(/([_:.\-/=?&#]+)/g).map((e,r)=>r%2===1?e:Te.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)?"__hex__":/^[0-9]{6,}$/.test(e)?"__num__":/^[a-z0-9_-]{6,}$/i.test(e)?"__id__":e).join("")}function ge(t,e){return Array.isArray(t)?t.map(r=>ge(r,e)):t&&typeof t=="object"?Object.fromEntries(Object.entries(t).map(([r,i])=>[r,ge(i,r)])):typeof t!="string"?t:e&&Dr.test(e)?Mr(t):Te.test(t)?"__uuid__":/^[0-9a-f]{6,}$/i.test(t)||/^[0-9]{8,}$/.test(t)?Re(t):t}function Ir(t){return t.replace(/((?:https?:\/\/[^"'\s>]+|\/)(?:wp-content|wp-includes)\/[^"'\s>?]+)\?ver=[A-Za-z0-9._-]+(?=$|[#"'])/gi,"$1")}function Lr(t,e){let r=e.trim();if((r.startsWith("{")||r.startsWith("[")||r.startsWith('"'))&&t.startsWith("data-"))try{let n=JSON.parse(r);return JSON.stringify(ge(n))}catch{}if(t.startsWith("data-")&&/^[A-Za-z0-9+/=]+$/.test(r))try{let n=Buffer.from(r,"base64").toString("utf8"),s=JSON.parse(n),o=JSON.stringify(ge(s));return Buffer.from(o,"utf8").toString("base64")}catch{}return e}function Or(t,e){let r=t.toLowerCase();return r==="class"?e.split(/(\s+)/).map((i,n)=>n%2===1?i:Re(i)).join(""):r==="id"||r==="for"||r==="aria-controls"||r==="aria-labelledby"||r==="aria-describedby"||r==="aria-owns"||r==="aria-activedescendant"||r==="name"?Re(e):r==="href"||r==="src"?Ir(e):Lr(r,e)}function jr(t){return t.replace(/<([a-z][^\s/>]*)([^<>]*?)>/gi,(e,r,i)=>{if(r.startsWith("/"))return e;let n=i.replace(/\s([:@a-zA-Z_][-:.a-zA-Z0-9_]*?)=(['"])([\s\S]*?)\2/g,(s,o,l,a)=>{let c=Or(o,a);return ` ${o}=${l}${c}${l}`});return `<${r}${n}>`})}function ve(t){let e=b.extname(t).toLowerCase();return [".html",".htm",".xml",".xsl",".txt"].includes(e)||["robots.txt","sitemap.xml"].includes(b.basename(t))}function ue(t,e){return [t.replace(/^\/+|\/+$/g,""),e.replace(/^\/+/,"")].filter(Boolean).join("/")}async function Fr(t,e,r,i){let n=new Map,s,o=0;do{o++;let l=await O(`S3 list objects page ${o} for s3://${e}/${r||""}`,3e5,({httpOptions:a})=>t.send(new ListObjectsV2Command({Bucket:e,Prefix:r||void 0,ContinuationToken:s}),a));for(let a of l.Contents||[]){if(!a.Key)continue;let c=(a.ETag||"").replace(/^"|"$/g,"").toLowerCase(),u=Number(a.Size??0);n.set(a.Key,{etag:c,size:u});}s=l.NextContinuationToken,i.debug(`Listed S3 keys page ${o}: ${n.size} keys so far`);}while(s);return n}async function _r(t,e,r,i){if(i.has(r))return i.get(r)??null;try{let n=await O(`S3 head object ${r}`,3e5,({httpOptions:o})=>t.send(new HeadObjectCommand({Bucket:e,Key:r}),o)),s={etag:(n.ETag||"").replace(/^"|"$/g,"").toLowerCase(),size:Number(n.ContentLength??0)};return i.set(r,s),s}catch{return i.set(r,null),null}}async function Ur(t,e,r,i){let n=[...new Set(Array.from(r).filter(Boolean))];if(n.length===0)return i.info("No explicitly tombstoned S3 objects to delete."),[];i.info(`Deleting ${n.length} explicitly tombstoned S3 object(s)\u2026`);for(let s=0;s<n.length;s+=1e3){let o=n.slice(s,s+1e3);if(o.length===0)continue;let l=await O(`S3 delete objects batch ${Math.floor(s/1e3)+1}`,3e5,({httpOptions:a})=>t.send(new DeleteObjectsCommand({Bucket:e,Delete:{Objects:o.map(c=>({Key:c}))}}),a));if(l.Errors&&l.Errors.length>0){let a=l.Errors.map(c=>`${c.Key||"(unknown)"}: ${c.Code||"DeleteFailed"}`);throw new Error(`S3 reported ${a.length} object deletion error(s): ${a.join(", ")}`)}i.info(`Deleted batch of ${o.length} object(s).`);for(let a of o)i.debug(` deleted: ${a}`);}return n}function Nr(t){return createHash("md5").update(t).digest("hex")}function st(t){return createHash("sha256").update(t).digest("hex")}function De(t){return createHash("sha256").update(jr(t.toString("utf8"))).digest("hex")}async function zr(t,e,r,i){if(i.has(r))return i.get(r)??null;try{let o=((await O(`S3 head object metadata ${r}`,3e5,({httpOptions:l})=>t.send(new HeadObjectCommand({Bucket:e,Key:r}),l))).Metadata?.[ot]||"").trim().toLowerCase()||null;return i.set(r,o),o}catch{return i.set(r,null),null}}async function Wr(t,e,r,i,n){if(i.has(r))return i.get(r)??null;try{let o=((await O(`S3 head object normalized metadata ${r}`,3e5,({httpOptions:l})=>t.send(new HeadObjectCommand({Bucket:e,Key:r}),l))).Metadata?.[at]||"").trim().toLowerCase();if(o)return i.set(r,o),o}catch{return i.set(r,null),null}if(n.has(r)){let s=n.get(r)??null;return i.set(r,s),s}try{let o=await(await O(`S3 get object ${r}`,3e5,({httpOptions:a})=>t.send(new GetObjectCommand({Bucket:e,Key:r}),a))).Body?.transformToByteArray(),l=o?De(Buffer.from(o)):null;return n.set(r,l),i.set(r,l),l}catch{return n.set(r,null),i.set(r,null),null}}function qr(t){return t.replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"")||"profile"}function Br(t,e){let r=t,i=Object.entries(e).sort((n,s)=>s[0].length-n[0].length);for(let[n,s]of i)n&&(r=r.split(n).join(s));return r}async function Gr(t,e,r,i){let n=b.join(t,"asset-map.json"),s;try{s=await D.readFile(n,"utf8");}catch(a){if(a.code==="ENOENT")return;throw a}let o=JSON.parse(s);if(!o||typeof o!="object")return;let l=Object.fromEntries(Object.entries(o).map(([a,c])=>{let u=String(c??"");return e.urlRewriteMode==="absolute"&&e.targetOrigin&&r.targetOrigin&&e.targetOrigin!==r.targetOrigin&&(u=u.split(e.targetOrigin).join(r.targetOrigin)),u=Br(u,i),[a,u]}));await D.writeFile(n,JSON.stringify(l,null,2),"utf8");}async function Hr(t,e,r,i,n,s){let o=t.targetOrigin!==e.targetOrigin,l=Object.keys(i).length>0;if(!r||!o&&!l)return {outputDir:t.outputDir,cleanup:async()=>{}};if(o&&t.urlRewriteMode!=="absolute")throw new Error(`Deployment profile "${r}" changes targetOrigin, but the base crawl output must use urlRewriteMode "absolute" for multi-target redeploys.`);let a=b.join(b.dirname(t.outputDir),`.deploy-profile-${qr(r)}-${Date.now()}`),c=$r(a);n.info(`Preparing deployment profile "${r}" from existing crawl output to ${a}...`);try{if(await D.rm(a,{recursive:!0,force:!0}),s){await D.mkdir(a,{recursive:!0});for(let d of s){let g=b.join(t.outputDir,d),w=b.join(a,d);await D.mkdir(b.dirname(w),{recursive:!0}),await D.copyFile(g,w);}}else await D.cp(t.outputDir,a,{recursive:!0});let u={...t,sourceOrigin:t.targetOrigin,targetOrigin:e.targetOrigin,extraReplacements:i,outputDir:a},m=1;n.info(`Rewriting text files for deployment profile "${r}"...`);let p=await rt(u,{},{files:s,onStart:({totalFiles:d,workerCount:g})=>{m=g,n.info(`Rewriting ${d} text file(s) for deployment profile "${r}" with ${g} worker(s).`);},onProgress:({index:d,totalFiles:g,changedTextFiles:w,file:T,changed:S})=>{let v=`Rewriting [${d}/${g}] ${T} (${S?"rewrote":"checked"}; ${w} changed so far)`;g<=200||d<=5||d%10===0||d===g?(n.progress(v,{file:T,index:d,totalFiles:g,changed:S,changedTextFiles:w,workerCount:m}),n.info(v)):n.debug(v);}});return await Gr(a,t,e,i),n.info(`Prepared deployment profile "${r}" in ${a} (${p} rewritten text file(s)).`),{outputDir:a,cleanup:async()=>{c(),await D.rm(a,{recursive:!0,force:!0});}}}catch(u){throw n.error(`Failed to prepare deployment profile "${r}" temporary output.`,{tempDir:a,outputDir:t.outputDir,error:u instanceof Error?u.message:String(u),...u instanceof Error&&u.stack?{stack:u.stack}:{}}),await D.rm(a,{recursive:true,force:true}).catch(()=>{}),c(),await n.flush().catch(()=>{}),u}}async function ut(t=process.argv.slice(2)){let e=await ne(),r=ze(t),i=We(e,r),n=b.resolve(i.config.logDir||"logs"),s=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",o=s?b.join(b.resolve(s),"deploy-diff.json"):"",l=new G(n,"deploy.log.jsonl",i.config.logLevel??"info"),a=i.profile?.extraReplacements??{},c=async()=>{};try{let u=await _e(e);if(t.includes("--content-sync")&&u?.schemaVersion!==2)throw new Error("Content-sync deployment requires a valid schema-2 targeted deploy plan.");let p=await Ne(e);if(p)throw new Error(`Deployment blocked: the crawl started at ${p.startedAt} did not complete its final rewrite. Run a successful crawl or resume the rewrite before deploying.`);let d=await Hr(e,i.config,i.name,a,l,u?.schemaVersion===2?u.changedFiles:void 0);c=d.cleanup;let g={...i.config,outputDir:d.outputDir},w=g.s3SyncMode??"sdk-upload-delete";if(u?.schemaVersion===2&&u.deletedFiles.length>0&&w!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let T=u?.schemaVersion!==2&&d.outputDir!==e.outputDir&&(e.targetOrigin!==i.config.targetOrigin||Object.keys(a).length>0),S=!!u&&!u.fullSyncRequired&&!T;if(u?.schemaVersion===2&&!S)throw new Error("Content-sync deployment cannot widen into a full-output deployment.");i.name&&l.info(`Using deployment profile: ${i.name}`),l.info(`Starting deploy \u2014 s3://${g.s3.bucket}/${g.s3.prefix||""} (region: ${g.s3.region})`),l.info(`S3 sync mode: ${w}`),u&&u.fullSyncRequired?l.info("Deploy plan requests a full sync; scanning the whole output tree."):u&&T?l.info("Ignoring incremental deploy plan because deployment-profile rewrites require a full sync."):S&&l.info(`Using incremental deploy plan: ${u.changedFiles.length} changed file(s), ${u.deletedFiles.length} deleted file(s).`),l.mark("deploy");let _=new S3Client({region:g.s3.region}),v=d.outputDir===e.outputDir?await kr(e,s,l):null,Ee=u?.schemaVersion===2?[...u.uploadGroups.assets,...u.uploadGroups.content,...u.uploadGroups.listings,...u.uploadGroups.sitemapLeaves,...u.uploadGroups.sitemapIndexes]:u?.changedFiles??[];if(u?.schemaVersion===2){let h=[...new Set(Ee)],$=[...new Set(u.changedFiles)];if(h.length!==$.length||$.some(f=>!h.includes(f)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let J=S?(await Promise.all(Ee.map(async h=>{try{return (await D.stat(b.join(g.outputDir,h))).isFile()?h:null}catch{if(u?.schemaVersion===2)throw new Error(`Content-sync planned upload is missing: ${h}`);return null}}))).filter(h=>h!==null):await Qt(["**/*"],{cwd:g.outputDir,onlyFiles:!0,dot:!0}),I=[],z=[],y=J.filter(h=>{let $=v?.byRelativePath.get(lt(h));if(!$)return !0;let f=ue(g.s3.prefix,h);return I.push(h),z.push(f),l.reject($.kind,$.sourceUrl||h,"rewrite not completed; deploy upload blocked","deploy-guard",{file:h,key:f,outputPath:$.outputPath,manifestPath:v?.manifestPath}),!1}),Ce=new Set((S?y:J).map(h=>ue(g.s3.prefix,h)));v&&(v.pendingPages>0||v.pendingTextAssets>0)&&l.info(`Deploy rewrite guard loaded ${v.pendingPages} pending page(s) and ${v.pendingTextAssets} pending text asset(s) from crawl manifest.`,{manifestPath:v.manifestPath,pendingPages:v.pendingPages,pendingTextAssets:v.pendingTextAssets,source:"deploy-guard"}),I.length>0&&l.warn(`Deploy rewrite guard blocked ${I.length} file(s) from upload because final crawl rewrite is incomplete.`,{rejected:I.length,manifestPath:v?.manifestPath,source:"deploy-guard"}),S?l.info(`Using ${y.length} eligible planned changed file(s) from deploy plan for upload${I.length>0?` (${I.length} blocked by deploy rewrite guard)`:""}.`):l.info(`Found ${J.length} file(s) in output directory "${g.outputDir}"${I.length>0?`; ${y.length} remain eligible for upload after deploy rewrite guard filtering`:""}.`),l.progress("Deploy preflight: comparing local output with remote S3 state.",{phase:"deploy",mode:w,totalFiles:y.length,scannedFiles:J.length,rejected:I.length,deploymentProfile:i.name||"",s3Bucket:g.s3.bucket,s3Prefix:g.s3.prefix||"",targetedPlan:S,deployGuardEnabled:d.outputDir===e.outputDir});let E=0,R=0,U=0,pe=[],V=[],Q=[],de=[],N=[],W=new Map,ct=new Map,gt=new Map,pt=new Map,dt=new Map;S||(l.info("Listing existing S3 objects to detect unchanged files..."),W=await Fr(_,g.s3.bucket,g.s3.prefix,l),l.info(`Found ${W.size} existing key(s) in S3.`));let ft=async h=>W.has(h)?W.get(h)??null:S?_r(_,g.s3.bucket,h,ct):null;for(let h of y){let $=b.join(g.outputDir,h),f=ue(g.s3.prefix,h),C=v?.contentTypeByRelativePath.get(h),Y=C&&C!=="application/octet-stream"?C:Rr.lookup(h)||C||"application/octet-stream",mt=ve(h)?g.s3.htmlCacheControl:g.s3.assetCacheControl,P=E+R+U+1;y.length<=200||P<=5||P%10===0||P===y.length?(l.progress(`Uploading [${P}/${y.length}] ${f} (${Y})`,{file:h,key:f,index:P,totalFiles:y.length,contentType:Y,mode:w}),l.info(`Uploading [${P}/${y.length}] ${f} (${Y})`)):l.debug(`Uploading [${P}/${y.length}] ${f} (${Y})`);try{let k=await D.readFile($),Z=await ft(f),X=null,q=null;if(Z){let fe=k.byteLength;if(Z.size===fe){let re=Z.etag.includes("-");if(Z.etag!==""&&!re){let ht=Nr(k);if(Z.etag===ht){R++,y.length<=200||R<=5||R%25===0?(l.progress(`Skipped unchanged [${P}/${y.length}] ${f}`,{file:h,key:f,index:P,totalFiles:y.length,uploaded:E,skipped:R,failed:U,mode:w,reason:"etag-md5-match"}),l.info(`Skipped unchanged [${P}/${y.length}] ${f}`)):l.debug(`Skipped unchanged [${P}/${y.length}] ${f}`),V.push(f);continue}}X=st(k);let ke=await zr(_,g.s3.bucket,f,gt);if(ke&&ke===X){R++,y.length<=200||R<=5||R%25===0?(l.progress(`Skipped unchanged [${P}/${y.length}] ${f}`,{file:h,key:f,index:P,totalFiles:y.length,uploaded:E,skipped:R,failed:U,mode:w,reason:"metadata-sha256-match"}),l.info(`Skipped unchanged [${P}/${y.length}] ${f}`)):l.debug(`Skipped unchanged [${P}/${y.length}] ${f}`),V.push(f);continue}}if(ve(h)){q=De(k);let re=await Wr(_,g.s3.bucket,f,pt,dt);if(re&&re===q){R++,y.length<=200||R<=5||R%25===0?(l.progress(`Skipped semantically unchanged [${P}/${y.length}] ${f}`,{file:h,key:f,index:P,totalFiles:y.length,uploaded:E,skipped:R,failed:U,mode:w,reason:"normalized-html-match"}),l.info(`Skipped semantically unchanged [${P}/${y.length}] ${f}`)):l.debug(`Skipped semantically unchanged [${P}/${y.length}] ${f}`),V.push(f);continue}}}X=X??st(k),q=q??(ve(h)?De(k):null),await O(`S3 upload ${f}`,9e5,({abortController:fe})=>new Upload({client:_,abortController:fe,params:{Bucket:g.s3.bucket,Key:f,Body:k,ContentType:Y,CacheControl:mt,Metadata:{[ot]:X,...q?{[at]:q}:{}}}}).done()),E++,pe.push(f),(E%25===0||E===y.length)&&(l.progress(`Uploaded ${E}/${y.length} file(s)\u2026`,{uploaded:E,skipped:R,failed:U,totalFiles:y.length,mode:w}),l.info(`Uploaded ${E}/${y.length} file(s)\u2026`));}catch(k){U++,Q.push(f),l.error(`Failed to upload ${f}: ${k.message}`,{key:f,error:String(k)});}}if(l.info(`Upload complete: ${E} uploaded, ${R} skipped unchanged, ${z.length} rejected by guard, ${U} failed.`),Q.length>0||z.length>0)throw new Error(`Deployment stopped before deletion because ${Q.length} upload(s) failed and ${z.length} upload(s) were rejected.`);if(w==="sdk-upload-delete"){let h=new Set((u?.deletedFiles??[]).map(f=>ue(g.s3.prefix,f))),$=[...h].filter(f=>!Ce.has(f));if(!S&&(N=[...W.keys()].filter(C=>!Ce.has(C)).filter(C=>!h.has(C)),N.length>0)){l.warn(`Protected ${N.length} remote-only S3 object(s) from deletion because the crawl did not provide explicit tombstones.`,{protected:N.length,source:"deploy-delete-guard"});for(let C of N)l.debug(` protected remote-only object: ${C}`);}de=await Ur(_,g.s3.bucket,S?$:$.filter(f=>W.has(f)),l);}else l.info("Sync mode is sdk-upload-only \u2014 skipping stale object deletion.");o&&(await D.mkdir(b.dirname(o),{recursive:!0}),await D.writeFile(o,JSON.stringify({generatedAt:new Date().toISOString(),mode:w,summary:{totalFiles:J.length,eligibleFiles:y.length,uploaded:pe.length,skipped:V.length,rejected:z.length,failed:Q.length,deleted:de.length,protected:N.length,...i.name?{profile:i.name}:{}},uploadedKeys:pe,skippedKeys:V,rejectedKeys:z,failedKeys:Q,deletedKeys:de,protectedRemoteOnlyKeys:N},null,2),"utf8")),u?.schemaVersion!==2&&await Ue(e),l.endMark("deploy"),await l.flush();}finally{await c().catch(()=>{});}}ut().catch(async t=>{console.error(t);try{let r=(await ne().catch(()=>null))?.logDir??"logs",i=new G(r,"deploy.log.jsonl","debug");i.error(`Unhandled error: ${t instanceof Error?t.message:String(t)}`,t instanceof Error?{stack:t.stack}:void 0),await i.flush();}catch{}process.exit(1);});
@@ -1,3 +1,3 @@
1
- import {CloudFrontClient,CreateInvalidationCommand}from'@aws-sdk/client-cloudfront';import h from'fs/promises';import W from'http';import _ from'https';import d from'path';function q(r){let e=r/6e4;if(Number.isInteger(e)&&e>=1)return `${e} minute${"s"}`;let t=Math.ceil(r/1e3);return `${t} second${t===1?"":"s"}`}async function L(r,e,t){let n=new AbortController,i=globalThis.setTimeout(()=>{n.abort(new Error(`${r} timed out after ${q(e)}.`));},e);try{return await t({abortController:n,httpOptions:{abortSignal:n.signal,requestTimeout:e}})}finally{globalThis.clearTimeout(i);}}function k(r){let e=r.trim();return e==="."?".":e.replace(/\/$/,"")}function B(r){let e=String(r??"").trim();if(!e)return "";let t=e;if(/^https?:\/\//i.test(t))try{t=new URL(t).pathname;}catch{return ""}if(t=t.split(/[?#]/,1)[0]?.replace(/\\/g,"/").trim()??"",!t)return "";let n=t.endsWith("/"),i=t.replace(/^\/+/,"").split("/").map(o=>o.replace(/[^A-Za-z0-9._-]/g,"")).filter(o=>o.length>0&&o!=="."&&o!=="..");if(i.length===0)return "";let s=`/${i.join("/")}`;return n&&d.extname(s)===""&&(s+="/"),s==="/"?"":s}function w(r){return !r||typeof r!="object"?{}:Object.fromEntries(Object.entries(r).map(([e,t])=>[e.trim(),String(t??"")]).filter(([e])=>e.length>0))}function J(r){if(!r||typeof r!="object")return {};let e={};for(let[t,n]of Object.entries(r)){let i=t.trim();if(!i||!n||typeof n!="object")continue;let s=n,o={};if(typeof s.targetOrigin=="string"){let a=k(s.targetOrigin);a&&(o.targetOrigin=a);}let c=w(s.extraReplacements);if(Object.keys(c).length>0&&(o.extraReplacements=c),s.s3&&typeof s.s3=="object"){let a={},l=s.s3;typeof l.bucket=="string"&&(a.bucket=l.bucket.trim()),typeof l.prefix=="string"&&(a.prefix=l.prefix.trim()),typeof l.region=="string"&&(a.region=l.region.trim()),typeof l.htmlCacheControl=="string"&&(a.htmlCacheControl=l.htmlCacheControl.trim()),typeof l.assetCacheControl=="string"&&(a.assetCacheControl=l.assetCacheControl.trim()),Object.keys(a).length>0&&(o.s3=a);}if(s.cloudFront&&typeof s.cloudFront=="object"){let a={},l=s.cloudFront;typeof l.distributionId=="string"&&(a.distributionId=l.distributionId.trim()),Array.isArray(l.invalidationPaths)&&(a.invalidationPaths=l.invalidationPaths.map(u=>String(u??"").trim()).filter(u=>u.length>0)),Object.keys(a).length>0&&(o.cloudFront=a);}e[i]=o;}return e}function I(r){return r==="PROFESSIONAL"||r==="AGENCY"?r:void 0}function z(r,e){if(!r||typeof r!="object")return null;let t=r,n=t.command;if(n!=="publish"&&n!=="crawl"&&n!=="deploy"&&n!=="invalidate"&&n!=="retry-timeouts"&&n!=="url")return null;let i=Number.parseInt(String(t.intervalMinutes??"0"),10);if(!Number.isFinite(i)||i<1)return null;let s=String(t.id??`${n}-${e+1}`).trim();if(!s)return null;let o=String(t.deploymentProfile??"").trim(),c=String(t.url??"").trim(),a=Array.isArray(t.postTypes)?[...new Set(t.postTypes.map(u=>String(u??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],l=Array.isArray(t.listingPaths)?[...new Set(t.listingPaths.map(u=>String(u??"").trim()).filter(Boolean))]:[];return {id:s,enabled:t.enabled!==false,command:n,intervalMinutes:i,...n==="publish"||n==="crawl"?{crawlMode:t.crawlMode==="incremental"?"incremental":"full"}:{},...(n==="publish"||n==="deploy"||n==="invalidate")&&o?{deploymentProfile:o}:{},...c?{url:c}:{},...a.length>0?{postTypes:a}:{},...l.length>0?{listingPaths:l}:{},...typeof t.includePostTypeArchives=="boolean"?{includePostTypeArchives:t.includePostTypeArchives}:{},...typeof t.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:t.includeTaxonomyArchives}:{},...typeof t.includeAuthorArchives=="boolean"?{includeAuthorArchives:t.includeAuthorArchives}:{},...typeof t.includeDateArchives=="boolean"?{includeDateArchives:t.includeDateArchives}:{},...typeof t.includePostsPage=="boolean"?{includePostsPage:t.includePostsPage}:{},...typeof t.includeSitemapChain=="boolean"?{includeSitemapChain:t.includeSitemapChain}:{}}}function A(r){let e=r&&typeof r=="object"?r:{},t=Array.isArray(e.rules)?e.rules.map((n,i)=>z(n,i)).filter(n=>!!n):[];return {enabled:e.enabled===true,timezone:typeof e.timezone=="string"&&e.timezone.trim()!==""?e.timezone.trim():"UTC",rules:t}}function H(r){if(!r||typeof r!="object")return;let e=r,t={};for(let i of ["accountId","siteId"]){let s=e[i];typeof s=="string"&&s.trim()!==""&&(t[i]=s.trim());}let n=Number(e.lastUpdate??0);return Number.isFinite(n)&&n>0&&(t.lastUpdate=Math.floor(n)),e.subscriber===true&&(t.subscriber=true),Object.keys(t).length>0?t:void 0}function T(r){let e=r&&typeof r=="object"?r:{},t=String(e.accountId??"").trim(),n=String(e.siteId??"").trim(),i=e.subscriber===true,s=H(e.siteSettings),o={...s??{},...t?{accountId:s?.accountId??t}:{},...n?{siteId:s?.siteId??n}:{},...s?.subscriber===true||i?{subscriber:true}:{}},c={},a=String(e.apiBase??"").trim();a&&(c.apiBase=a);let l=String(e.runtimeToken??e.nonce??"").trim();l&&(c.runtimeToken=l);let u=String(e.virtualAssetBaseUrl??e.uploadUrl??"").trim();u&&(c.virtualAssetBaseUrl=u),Object.keys(o).length>0&&(c.siteSettings=o);let p=I(e.subscriptionType);return p&&(c.subscriptionType=p),c}function Q(r){try{let e=new URL(r);globalThis.location=e;}catch{}}function E(r){return typeof r=="string"?r:r instanceof URL?r.toString():r.url}function G(r,e){let t=new Headers(r instanceof Request?r.headers:void 0);if(e?.headers)for(let[n,i]of new Headers(e.headers).entries())t.set(n,i);return t}async function F(r,e,t=5){let n=new URL(E(r)),i=String(e?.method??(r instanceof Request?r.method:"GET")).toUpperCase(),s=n.protocol==="http:"?W:_,o=G(r,e);return await new Promise((c,a)=>{let l=s.request(n,{method:i,headers:Object.fromEntries(o.entries()),...n.protocol==="https:"?{rejectUnauthorized:false}:{}},u=>{let p=[];u.on("data",g=>{p.push(Buffer.isBuffer(g)?g:Buffer.from(g));}),u.on("error",a),u.on("end",()=>{let g=u.statusCode??0,y=u.headers.location;if(t>0&&y&&[301,302,303,307,308].includes(g)){c(F(new URL(y,n),{method:g===303?"GET":i},t-1));return}let b=new Headers;for(let[m,v]of Object.entries(u.headers))Array.isArray(v)?b.set(m,v.join(", ")):typeof v=="string"&&b.set(m,v);c(new Response(Buffer.concat(p),{status:g,statusText:u.statusMessage??"",headers:b}));});});l.on("error",a),l.end();})}function Y(r){let e=T(r.wpsuite),t={...e.siteSettings??{},...e.siteSettings?.subscriber===true||e.subscriptionType==="PROFESSIONAL"||e.subscriptionType==="AGENCY"?{subscriber:true}:{}},n=String(t.accountId??"").trim(),i=String(t.siteId??"").trim(),s=String(e.virtualAssetBaseUrl??e.uploadUrl??"").trim();if(r.wpsuite={...e.apiBase?{apiBase:e.apiBase}:{},...e.runtimeToken?{runtimeToken:e.runtimeToken}:{},...s?{virtualAssetBaseUrl:s}:{},...Object.keys(t).length>0?{siteSettings:t}:{},...e.subscriptionType?{subscriptionType:e.subscriptionType}:{}},!n||!i||!s)return false;Q(r.sourceOrigin);let o=globalThis,a={...o.WpSuite??{},siteSettings:t,uploadUrl:s};return e.apiBase&&(a.apiBase=e.apiBase),o.WpSuite=a,true}async function K(r){let e=String(r.wpsuite?.virtualAssetBaseUrl??r.wpsuite?.uploadUrl??"").trim();if(!Y(r)){let i=r.wpsuite?.siteSettings,s=[String(i?.accountId??"").trim()?"":"accountId",String(i?.siteId??"").trim()?"":"siteId",e?"":"virtualAssetBaseUrl"].filter(Boolean);return {remote:null,diagnostic:{status:"invalid-runtime",virtualAssetBaseUrl:e,subscriptionType:null,error:`Missing WP Suite runtime field(s): ${s.join(", ")}.`,requests:[]}}}let t=globalThis.fetch,n=[];try{typeof t=="function"&&/^https?:\/\//i.test(e)&&(globalThis.fetch=(async(a,l)=>{let u=E(a);if(u.startsWith(e)){let p=(()=>{try{return new URL(u).pathname}catch{return u}})();try{let g=r.ignoreHttpsErrors&&u.startsWith("https://")?await F(a,l):await t(a,l);return n.push({path:p,status:g.status,ok:g.ok,error:null}),g}catch(g){throw n.push({path:p,status:null,ok:!1,error:g instanceof Error?g.message:String(g)}),g}}return t(a,l)}));let{getConfig:i}=await import('@smart-cloud/wpsuite-core'),s=await i("publisher"),o=s&&typeof s=="object"?s:null,c=I(o?.subscriptionType);return {remote:o,diagnostic:{status:o?"loaded":"unavailable",virtualAssetBaseUrl:e,subscriptionType:c??null,error:o?null:"WP Suite publisher config loader returned no configuration.",requests:n}}}catch(i){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:e,subscriptionType:null,error:i instanceof Error?i.message:String(i),requests:n}}}finally{typeof t=="function"&&(globalThis.fetch=t);}}function V(r,e){let t=J(e?.deploymentProfiles),n=String(e?.defaultDeploymentProfile??"").trim(),i=t[n]?n:"",s=I(e?.subscriptionType),o=T(r.wpsuite);return {...r,scheduler:A(e?.scheduler),deploymentProfiles:t,defaultDeploymentProfile:i,...s?{subscriptionType:s}:{},wpsuite:{...o,...o.siteSettings||s?{siteSettings:{...o.siteSettings??{},...o.siteSettings?.subscriber===true||s==="PROFESSIONAL"||s==="AGENCY"?{subscriber:true}:{}}}:{},...s?{subscriptionType:s}:{}}}}function C(r,e){let n=String(r||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!n)return e;let i=n.split("/").map(s=>s.trim()).filter(s=>s.length>0&&s!=="."&&s!=="..");return i.length>0?i.join("/"):e}function Z(){let r=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return r.trim()?d.resolve(r):""}function D(r,e,t){let n=String(e||"").trim();return n&&d.isAbsolute(n)?d.resolve(n):d.resolve(r,C(n,t))}async function R(r,e={}){let t=String("").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",n=await h.readFile(t,"utf8"),i=JSON.parse(n);i.sourceOrigin=i.sourceOrigin.replace(/\/$/,""),i.targetOrigin=k(i.targetOrigin),i.ignoreHttpsErrors??=false,i.outputDir=String(i.outputDir||"export").trim()||"export",i.urlRewriteMode||=i.targetOrigin==="."?"relative":"absolute",i.noJavaScriptRenderPathPrefixes||=[],i.seedPaths||=[],i.generated404RequestPath=B(i.generated404RequestPath),i.sitemapPaths||=["/sitemap_index.xml","/sitemap.xml"],i.allowedAssetHosts||=[],i.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],i.blockedPathPrefixes||=[],i.blockedSearchFragments||=[],i.concurrency||=1,i.maxPages||=0,i.extraReplacements=w(i.extraReplacements),i.postCrawlCopyMap=w(i.postCrawlCopyMap),i.logDir=String(i.logDir||"logs").trim()||"logs",i.verbose??=false,i.logLevel||=i.verbose?"debug":"info",i.s3SyncMode||="sdk-upload-delete",i.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},i.readiness.timeoutMs??=1500,i.readiness.fallbackWaitMs??=1500,i.viewport||={width:1440,height:1200},i.navigationTimeoutMs||=3e4,i.assetDownloadConcurrency=Number(i.assetDownloadConcurrency)>0?Number(i.assetDownloadConcurrency):i.concurrency,i.rewriteConcurrency=Number(i.rewriteConcurrency)>0?Number(i.rewriteConcurrency):i.assetDownloadConcurrency,i.wpsuite=T(i.wpsuite),i.scheduler=A(void 0),i.deploymentProfiles={},i.defaultDeploymentProfile="",i.deploymentTargetOverride=String(i.deploymentTargetOverride??"").trim();let s=await K(i);e.onRemotePublisherConfigDiagnostic?.(s.diagnostic);let o=V(i,s.remote),c=Z(),a=c?d.resolve(c,".."):"";return a?(o.outputDir=D(a,o.outputDir,"export"),o.logDir=D(a,o.logDir,"logs")):(d.isAbsolute(o.outputDir)||(o.outputDir=C(o.outputDir,"export")),d.isAbsolute(o.logDir)||(o.logDir=C(o.logDir,"logs"))),o}function X(r){return typeof r.subscriptionType=="string"&&r.subscriptionType.trim()!==""}function x(r){let e="";for(let n=0;n<r.length;n++){let i=r[n];if(i==="--profile"){let s=(r[n+1]||"").trim();if(!s)throw new Error("Missing value for --profile.");e=s,n+=1;}else if(i.startsWith("--profile=")){let s=i.slice(10).trim();if(!s)throw new Error("Missing value for --profile.");e=s;}}let t=String(process.env.PUBLISHER_DEPLOY_PROFILE||process.env.PUBLISHER_DEPLOYMENT_PROFILE||"").trim();return e||t||null}function ee(r,e){return {...r,...e??{}}}function te(r,e){return {...r,...e??{},invalidationPaths:[...(e?.invalidationPaths??r.invalidationPaths)||[]]}}function O(r,e){let t=String(e??r.deploymentTargetOverride??"").trim();if(!X(r)){if(t)throw new Error(`Deployment target "${t}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:r}}let n=String(t||r.defaultDeploymentProfile||"").trim();if(!n)return {name:null,profile:null,config:r};let i=r.deploymentProfiles?.[n];if(!i)throw new Error(`Unknown deployment profile "${n}". Check the linked WPSuite publisher configuration.`);return {name:n,profile:i,config:{...r,targetOrigin:i.targetOrigin??r.targetOrigin,s3:ee(r.s3,i.s3),cloudFront:te(r.cloudFront,i.cloudFront)}}}var j={error:0,warn:1,info:2,debug:3},M=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",S=M?d.join(M,"current-progress.json"):"",U=Promise.resolve();function $(r){return r.includes("crawl")?"crawl":r.includes("deploy")?"deploy":r.includes("invalidate")?"invalidate":r}function ie(r,e){let t={...r},n=new Set(["pagesQueued","pagesRendered","assetsQueued","sitemapsQueued","pagesDiscovered","assetsDiscovered","sitemapsDiscovered","pagesSaved","assetsSaved","donePages","doneAssets","doneSitemaps","changedTextFiles","uploaded","failed","index","totalFiles","pageQueue","assetQueue","sitemapQueue"]);for(let[i,s]of Object.entries(e)){let o=t[i];if(typeof o=="number"&&typeof s=="number"&&n.has(i)){t[i]=Math.max(o,s);continue}t[i]=s;}return t}function re(r,e,t){S&&(U=U.then(async()=>{let n=new Date,i=n.toISOString();await h.mkdir(d.dirname(S),{recursive:true});let s=await h.readFile(S,"utf8").then(m=>JSON.parse(m)).catch(()=>null),o=s&&typeof s.details=="object"&&s.details?s.details:{},c=$(r),a=s?.currentStep||$(s?.source||""),l=s?.startedAt||i,u={...s?.stepDurationsSec??{}},p=s?.stepStartedAt||i;if(a&&a!==c&&s?.stepStartedAt){let m=Math.max(0,Math.round((n.getTime()-new Date(s.stepStartedAt).getTime())/1e3));u[a]=(u[a]??0)+m,p=i;}let g=Math.max(0,Math.round((n.getTime()-new Date(p).getTime())/1e3)),y=Math.max(0,Math.round((n.getTime()-new Date(l).getTime())/1e3)),b={checkedAt:i,source:r,message:e,details:ie(o,t??{}),startedAt:l,currentStep:c,stepStartedAt:p,stepElapsedSec:g,totalElapsedSec:y,stepDurationsSec:u};await h.writeFile(S,JSON.stringify(b,null,2),"utf8");}).catch(()=>{}));}async function ne(r,e){await h.mkdir(d.dirname(r),{recursive:true}),await h.appendFile(r,`${JSON.stringify(e)}
2
- `,"utf8");}var P=class{constructor(e,t,n="info"){this.logDir=e;this.logFile=t;this.level=n;this.ensureLogFileReady();}logDir;logFile;level;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;get logPath(){return d.join(this.logDir,this.logFile)}artifactPath(e){return this.logFile.endsWith(".log.jsonl")?d.join(this.logDir,this.logFile.replace(/\.log\.jsonl$/u,`${e}.jsonl`)):d.join(this.logDir,`${this.logFile}${e}.jsonl`)}get errorsPath(){return this.artifactPath(".errors")}get rejectedPath(){return this.artifactPath(".rejected")}ensureLogFileReady(){return this.initPromise||(this.initPromise=(async()=>{await h.mkdir(this.logDir,{recursive:true}),await h.writeFile(this.logPath,"","utf8");})()),this.initPromise}enqueueTask(e){this.writeQueue=this.writeQueue.then(e).catch(t=>{this.writeError=t instanceof Error?t:new Error(String(t));});}enqueueLine(e){this.enqueueTask(async()=>{await this.ensureLogFileReady(),await h.appendFile(this.logPath,`${e}
3
- `,"utf8");});}enqueueJsonLine(e,t){this.enqueueTask(()=>ne(e,t));}accepts(e){return j[e]<=j[this.level]}push(e,t,n){if(!this.accepts(e))return;let i=JSON.stringify({time:new Date().toISOString(),level:e,message:t,...n||{}});this.enqueueLine(i),e==="error"?console.error(`[ERROR] ${t}`,n?JSON.stringify(n):""):e==="warn"?console.warn(`[WARN] ${t}`,n?JSON.stringify(n):""):console.log(`[${e.toUpperCase().padEnd(5)}] ${t}`);}info(e,t){this.push("info",e,t);}progress(e,t){re(this.logFile,e,t);}debug(e,t){this.push("debug",e,t);}warn(e,t){this.push("warn",e,t);}reject(e,t,n,i,s){let o={kind:e,url:t,reason:n,...i?{source:i}:{},...s||{}};this.enqueueJsonLine(this.rejectedPath,o),this.accepts("debug")&&this.push("debug",`Rejected ${e} ${t}: ${n}`,o);}error(e,t){this.enqueueJsonLine(this.errorsPath,{message:e,...t||{}}),this.push("error",e,t);}mark(e){this.marks.set(e,Date.now());}endMark(e,t){let n=this.marks.get(e);if(!n)return;let i=Date.now()-n,s=Number((i/1e3).toFixed(2));this.push("info",`Timing ${e}: ${s}s`,{name:e,ms:i,seconds:s,...t||{}});}async flush(){let e=Date.now()-this.startedAt,t=Number((e/1e3).toFixed(2));if(this.push("info",`Total time: ${t}s`,{name:"total",ms:e,seconds:t}),await this.writeQueue,this.writeError)throw this.writeError}};async function le(){let r=await R(),e=x(process.argv.slice(2)),t=O(r,e),n=t.config,i=n.logDir||"logs",s=new P(i,"invalidate.log.jsonl",n.logLevel??"info");if(t.name&&s.info(`Using deployment profile: ${t.name}`),!n.cloudFront.distributionId){s.info("No CloudFront distributionId configured; skipping invalidation."),await s.flush();return}let o=n.cloudFront.invalidationPaths.length>0?n.cloudFront.invalidationPaths:["/*"];s.info(`Starting CloudFront invalidation for distribution ${n.cloudFront.distributionId}`),s.info(`Invalidation paths (${o.length}): ${o.join(", ")}`),s.mark("invalidate"),s.progress("CloudFront invalidation request queued.",{phase:"invalidate",distributionId:n.cloudFront.distributionId,pathCount:o.length});let c=new CloudFrontClient({region:"us-east-1"}),a=`smartcloud-static-publisher-${Date.now()}`;s.debug(`CallerReference: ${a}`);let l=await L(`CloudFront invalidation for distribution ${n.cloudFront.distributionId}`,3e5,({httpOptions:g})=>c.send(new CreateInvalidationCommand({DistributionId:n.cloudFront.distributionId,InvalidationBatch:{CallerReference:a,Paths:{Quantity:o.length,Items:o}}}),g)),u=l.Invalidation?.Id??"(unknown)",p=l.Invalidation?.Status??"(unknown)";s.info(`CloudFront invalidation created. ID: ${u} Status: ${p}`),s.debug(`Full invalidation response: ${JSON.stringify(l.Invalidation)}`),s.endMark("invalidate"),await s.flush();}le().catch(async r=>{console.error(r);try{let t=(await R().catch(()=>null))?.logDir??"logs",n=new P(t,"invalidate.log.jsonl","debug");n.error(`Unhandled error: ${r instanceof Error?r.message:String(r)}`,r instanceof Error?{stack:r.stack}:void 0),await n.flush();}catch{}process.exit(1);});
1
+ import b from'fs/promises';import X from'http';import ee from'https';import h from'path';import {CloudFrontClient,waitUntilInvalidationCompleted,CreateInvalidationCommand}from'@aws-sdk/client-cloudfront';function j(r){let e=r.trim();return e==="."?".":e.replace(/\/$/,"")}function te(r){let e=String(r??"").trim();if(!e)return "";let t=e;if(/^https?:\/\//i.test(t))try{t=new URL(t).pathname;}catch{return ""}if(t=t.split(/[?#]/,1)[0]?.replace(/\\/g,"/").trim()??"",!t)return "";let n=t.endsWith("/"),i=t.replace(/^\/+/,"").split("/").map(o=>o.replace(/[^A-Za-z0-9._-]/g,"")).filter(o=>o.length>0&&o!=="."&&o!=="..");if(i.length===0)return "";let s=`/${i.join("/")}`;return n&&h.extname(s)===""&&(s+="/"),s==="/"?"":s}function k(r){return !r||typeof r!="object"?{}:Object.fromEntries(Object.entries(r).map(([e,t])=>[e.trim(),String(t??"")]).filter(([e])=>e.length>0))}function re(r){if(!r||typeof r!="object")return {};let e={};for(let[t,n]of Object.entries(r)){let i=t.trim();if(!i||!n||typeof n!="object")continue;let s=n,o={};if(typeof s.targetOrigin=="string"){let a=j(s.targetOrigin);a&&(o.targetOrigin=a);}let u=k(s.extraReplacements);if(Object.keys(u).length>0&&(o.extraReplacements=u),s.s3&&typeof s.s3=="object"){let a={},l=s.s3;typeof l.bucket=="string"&&(a.bucket=l.bucket.trim()),typeof l.prefix=="string"&&(a.prefix=l.prefix.trim()),typeof l.region=="string"&&(a.region=l.region.trim()),typeof l.htmlCacheControl=="string"&&(a.htmlCacheControl=l.htmlCacheControl.trim()),typeof l.assetCacheControl=="string"&&(a.assetCacheControl=l.assetCacheControl.trim()),Object.keys(a).length>0&&(o.s3=a);}if(s.cloudFront&&typeof s.cloudFront=="object"){let a={},l=s.cloudFront;typeof l.distributionId=="string"&&(a.distributionId=l.distributionId.trim()),Array.isArray(l.invalidationPaths)&&(a.invalidationPaths=l.invalidationPaths.map(c=>String(c??"").trim()).filter(c=>c.length>0)),Object.keys(a).length>0&&(o.cloudFront=a);}e[i]=o;}return e}function L(r){return r==="PROFESSIONAL"||r==="AGENCY"?r:void 0}function ie(r,e){if(!r||typeof r!="object")return null;let t=r,n=t.command;if(n!=="publish"&&n!=="crawl"&&n!=="deploy"&&n!=="invalidate"&&n!=="retry-timeouts"&&n!=="url"&&n!=="content-sync")return null;let i=Number.parseInt(String(t.intervalMinutes??"0"),10);if(!Number.isFinite(i)||i<1)return null;let s=String(t.id??`${n}-${e+1}`).trim();if(!s)return null;let o=String(t.deploymentProfile??"").trim(),u=String(t.url??"").trim(),a=Array.isArray(t.postTypes)?[...new Set(t.postTypes.map(c=>String(c??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],l=Array.isArray(t.listingPaths)?[...new Set(t.listingPaths.map(c=>String(c??"").trim()).filter(Boolean))]:[];return n==="content-sync"&&a.length===0?null:{id:s,enabled:t.enabled!==false,command:n,intervalMinutes:i,...n==="publish"||n==="crawl"?{crawlMode:t.crawlMode==="incremental"?"incremental":"full"}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&o?{deploymentProfile:o}:{},...u?{url:u}:{},...a.length>0?{postTypes:a}:{},...l.length>0?{listingPaths:l}:{},...typeof t.includePostTypeArchives=="boolean"?{includePostTypeArchives:t.includePostTypeArchives}:{},...typeof t.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:t.includeTaxonomyArchives}:{},...typeof t.includeAuthorArchives=="boolean"?{includeAuthorArchives:t.includeAuthorArchives}:{},...typeof t.includeDateArchives=="boolean"?{includeDateArchives:t.includeDateArchives}:{},...typeof t.includePostsPage=="boolean"?{includePostsPage:t.includePostsPage}:{},...typeof t.includeSitemapChain=="boolean"?{includeSitemapChain:t.includeSitemapChain}:{}}}function U(r){let e=r&&typeof r=="object"?r:{},t=Array.isArray(e.rules)?e.rules.map((n,i)=>ie(n,i)).filter(n=>!!n):[];return {enabled:e.enabled===true,timezone:typeof e.timezone=="string"&&e.timezone.trim()!==""?e.timezone.trim():"UTC",rules:t}}function ne(r){if(!r||typeof r!="object")return;let e=r,t={};for(let i of ["accountId","siteId"]){let s=e[i];typeof s=="string"&&s.trim()!==""&&(t[i]=s.trim());}let n=Number(e.lastUpdate??0);return Number.isFinite(n)&&n>0&&(t.lastUpdate=Math.floor(n)),e.subscriber===true&&(t.subscriber=true),Object.keys(t).length>0?t:void 0}function x(r){let e=r&&typeof r=="object"?r:{},t=String(e.accountId??"").trim(),n=String(e.siteId??"").trim(),i=e.subscriber===true,s=ne(e.siteSettings),o={...s??{},...t?{accountId:s?.accountId??t}:{},...n?{siteId:s?.siteId??n}:{},...s?.subscriber===true||i?{subscriber:true}:{}},u={},a=String(e.apiBase??"").trim();a&&(u.apiBase=a);let l=String(e.runtimeToken??e.nonce??"").trim();l&&(u.runtimeToken=l);let c=String(e.virtualAssetBaseUrl??e.uploadUrl??"").trim();c&&(u.virtualAssetBaseUrl=c),Object.keys(o).length>0&&(u.siteSettings=o);let d=L(e.subscriptionType);return d&&(u.subscriptionType=d),u}function se(r){try{let e=new URL(r);globalThis.location=e;}catch{}}function _(r){return typeof r=="string"?r:r instanceof URL?r.toString():r.url}function oe(r,e){let t=new Headers(r instanceof Request?r.headers:void 0);if(e?.headers)for(let[n,i]of new Headers(e.headers).entries())t.set(n,i);return t}async function $(r,e,t=5){let n=new URL(_(r)),i=String(e?.method??(r instanceof Request?r.method:"GET")).toUpperCase(),s=n.protocol==="http:"?X:ee,o=oe(r,e);return await new Promise((u,a)=>{let l=s.request(n,{method:i,headers:Object.fromEntries(o.entries()),...n.protocol==="https:"?{rejectUnauthorized:false}:{}},c=>{let d=[];c.on("data",g=>{d.push(Buffer.isBuffer(g)?g:Buffer.from(g));}),c.on("error",a),c.on("end",()=>{let g=c.statusCode??0,p=c.headers.location;if(t>0&&p&&[301,302,303,307,308].includes(g)){u($(new URL(p,n),{method:g===303?"GET":i},t-1));return}let m=new Headers;for(let[w,f]of Object.entries(c.headers))Array.isArray(f)?m.set(w,f.join(", ")):typeof f=="string"&&m.set(w,f);u(new Response(Buffer.concat(d),{status:g,statusText:c.statusMessage??"",headers:m}));});});l.on("error",a),l.end();})}function ae(r){let e=x(r.wpsuite),t={...e.siteSettings??{},...e.siteSettings?.subscriber===true||e.subscriptionType==="PROFESSIONAL"||e.subscriptionType==="AGENCY"?{subscriber:true}:{}},n=String(t.accountId??"").trim(),i=String(t.siteId??"").trim(),s=String(e.virtualAssetBaseUrl??e.uploadUrl??"").trim();if(r.wpsuite={...e.apiBase?{apiBase:e.apiBase}:{},...e.runtimeToken?{runtimeToken:e.runtimeToken}:{},...s?{virtualAssetBaseUrl:s}:{},...Object.keys(t).length>0?{siteSettings:t}:{},...e.subscriptionType?{subscriptionType:e.subscriptionType}:{}},!n||!i||!s)return false;se(r.sourceOrigin);let o=globalThis,a={...o.WpSuite??{},siteSettings:t,uploadUrl:s};return e.apiBase&&(a.apiBase=e.apiBase),o.WpSuite=a,true}async function le(r){let e=String(r.wpsuite?.virtualAssetBaseUrl??r.wpsuite?.uploadUrl??"").trim();if(!ae(r)){let i=r.wpsuite?.siteSettings,s=[String(i?.accountId??"").trim()?"":"accountId",String(i?.siteId??"").trim()?"":"siteId",e?"":"virtualAssetBaseUrl"].filter(Boolean);return {remote:null,diagnostic:{status:"invalid-runtime",virtualAssetBaseUrl:e,subscriptionType:null,error:`Missing WP Suite runtime field(s): ${s.join(", ")}.`,requests:[]}}}let t=globalThis.fetch,n=[];try{typeof t=="function"&&/^https?:\/\//i.test(e)&&(globalThis.fetch=(async(a,l)=>{let c=_(a);if(c.startsWith(e)){let d=(()=>{try{return new URL(c).pathname}catch{return c}})();try{let g=r.ignoreHttpsErrors&&c.startsWith("https://")?await $(a,l):await t(a,l);return n.push({path:d,status:g.status,ok:g.ok,error:null}),g}catch(g){throw n.push({path:d,status:null,ok:!1,error:g instanceof Error?g.message:String(g)}),g}}return t(a,l)}));let{getConfig:i}=await import('@smart-cloud/wpsuite-core'),s=await i("publisher"),o=s&&typeof s=="object"?s:null,u=L(o?.subscriptionType);return {remote:o,diagnostic:{status:o?"loaded":"unavailable",virtualAssetBaseUrl:e,subscriptionType:u??null,error:o?null:"WP Suite publisher config loader returned no configuration.",requests:n}}}catch(i){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:e,subscriptionType:null,error:i instanceof Error?i.message:String(i),requests:n}}}finally{typeof t=="function"&&(globalThis.fetch=t);}}function ue(r,e){let t=re(e?.deploymentProfiles),n=String(e?.defaultDeploymentProfile??"").trim(),i=t[n]?n:"",s=L(e?.subscriptionType),o=x(r.wpsuite);return {...r,scheduler:U(e?.scheduler),deploymentProfiles:t,defaultDeploymentProfile:i,...s?{subscriptionType:s}:{},wpsuite:{...o,...o.siteSettings||s?{siteSettings:{...o.siteSettings??{},...o.siteSettings?.subscriber===true||s==="PROFESSIONAL"||s==="AGENCY"?{subscriber:true}:{}}}:{},...s?{subscriptionType:s}:{}}}}function E(r,e){let n=String(r||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!n)return e;let i=n.split("/").map(s=>s.trim()).filter(s=>s.length>0&&s!=="."&&s!=="..");return i.length>0?i.join("/"):e}function ce(){let r=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return r.trim()?h.resolve(r):""}function N(r,e,t){let n=String(e||"").trim();return n&&h.isAbsolute(n)?h.resolve(n):h.resolve(r,E(n,t))}async function C(r,e={}){let t=String("").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",n=await b.readFile(t,"utf8"),i=JSON.parse(n);i.sourceOrigin=i.sourceOrigin.replace(/\/$/,""),i.targetOrigin=j(i.targetOrigin),i.ignoreHttpsErrors??=false,i.outputDir=String(i.outputDir||"export").trim()||"export",i.urlRewriteMode||=i.targetOrigin==="."?"relative":"absolute",i.noJavaScriptRenderPathPrefixes||=[],i.seedPaths||=[],i.generated404RequestPath=te(i.generated404RequestPath),i.sitemapPaths||=["/sitemap_index.xml","/sitemap.xml"],i.allowedAssetHosts||=[],i.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],i.blockedPathPrefixes||=[],i.blockedSearchFragments||=[],i.concurrency||=1,i.maxPages||=0,i.extraReplacements=k(i.extraReplacements),i.postCrawlCopyMap=k(i.postCrawlCopyMap),i.logDir=String(i.logDir||"logs").trim()||"logs",i.verbose??=false,i.logLevel||=i.verbose?"debug":"info",i.s3SyncMode||="sdk-upload-delete",i.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},i.readiness.timeoutMs??=1500,i.readiness.fallbackWaitMs??=1500,i.viewport||={width:1440,height:1200},i.navigationTimeoutMs||=3e4,i.assetDownloadConcurrency=Number(i.assetDownloadConcurrency)>0?Number(i.assetDownloadConcurrency):i.concurrency,i.rewriteConcurrency=Number(i.rewriteConcurrency)>0?Number(i.rewriteConcurrency):i.assetDownloadConcurrency,i.wpsuite=x(i.wpsuite),i.scheduler=U(void 0),i.deploymentProfiles={},i.defaultDeploymentProfile="",i.deploymentTargetOverride=String(i.deploymentTargetOverride??"").trim();let s=await le(i);e.onRemotePublisherConfigDiagnostic?.(s.diagnostic);let o=ue(i,s.remote),u=ce(),a=u?h.resolve(u,".."):"";return a?(o.outputDir=N(a,o.outputDir,"export"),o.logDir=N(a,o.logDir,"logs")):(h.isAbsolute(o.outputDir)||(o.outputDir=E(o.outputDir,"export")),h.isAbsolute(o.logDir)||(o.logDir=E(o.logDir,"logs"))),o}function ge(r){let e=r/6e4;if(Number.isInteger(e)&&e>=1)return `${e} minute${"s"}`;let t=Math.ceil(r/1e3);return `${t} second${t===1?"":"s"}`}async function M(r,e,t){let n=new AbortController,i=globalThis.setTimeout(()=>{n.abort(new Error(`${r} timed out after ${ge(e)}.`));},e);try{return await t({abortController:n,httpOptions:{abortSignal:n.signal,requestTimeout:e}})}finally{globalThis.clearTimeout(i);}}var de=".deploy-plan.json",me="deploy-plan.json";function he(){let r=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return r.trim()?h.resolve(r):""}function fe(r){let e=String(r||"").replace(/\\/g,"/").replace(/^\/+/,"").trim(),t=h.posix.normalize(e);return !e||!t||t==="."||/^[a-zA-Z]:/.test(t)||t.startsWith("../")||t===".."?null:t}function ye(r){let e=String(r||"").trim();if(!e.startsWith("/")||e.startsWith("//")||e.includes("*")||e.includes("#"))return null;try{let t=new URL(e,"https://content-sync.invalid");return t.origin!=="https://content-sync.invalid"?null:`${h.posix.normalize(t.pathname)}${t.search}`}catch{return null}}function P(r){return [...new Set(r.map(fe).filter(e=>e!==null))]}function Pe(r,e){let t={generatedAt:String(e.generatedAt||new Date().toISOString()),outputDir:h.resolve(r.outputDir),runMode:e.runMode,crawlMode:e.crawlMode,fullSyncRequired:!!e.fullSyncRequired,changedFiles:P(e.changedFiles||[]),deletedFiles:P(e.deletedFiles||[]),rewriteTargets:P(e.rewriteTargets||[])};return e.schemaVersion===2?{...t,schemaVersion:2,runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,consumerId:String(e.consumerId||"").trim(),fromSequence:Math.max(0,Number(e.fromSequence)||0),toSequence:Math.max(0,Number(e.toSequence)||0),impactHash:String(e.impactHash||"").trim(),uploadGroups:{assets:P(e.uploadGroups?.assets||[]),content:P(e.uploadGroups?.content||[]),listings:P(e.uploadGroups?.listings||[]),sitemapLeaves:P(e.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:P(e.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((e.invalidationPaths||[]).map(ye).filter(n=>n!==null))].sort()}:{schemaVersion:1,...t,runMode:e.runMode}}function be(r){if(!r||typeof r!="object")return false;let e=r;if(e.schemaVersion===2){let t=e;return t.runMode==="content-sync"&&t.crawlMode==="incremental"&&t.fullSyncRequired===false&&typeof t.generatedAt=="string"&&typeof t.outputDir=="string"&&typeof t.consumerId=="string"&&t.consumerId.trim()!==""&&Number.isSafeInteger(t.fromSequence)&&Number.isSafeInteger(t.toSequence)&&Number(t.toSequence)>=Number(t.fromSequence)&&typeof t.impactHash=="string"&&/^[a-f0-9]{64}$/.test(t.impactHash)&&Array.isArray(t.changedFiles)&&Array.isArray(t.deletedFiles)&&Array.isArray(t.rewriteTargets)&&!!t.uploadGroups&&Array.isArray(t.uploadGroups.assets)&&Array.isArray(t.uploadGroups.content)&&Array.isArray(t.uploadGroups.listings)&&Array.isArray(t.uploadGroups.sitemapLeaves)&&Array.isArray(t.uploadGroups.sitemapIndexes)&&Array.isArray(t.invalidationPaths)}return e.schemaVersion===1&&typeof e.generatedAt=="string"&&typeof e.outputDir=="string"&&(e.runMode==="full"||e.runMode==="retry-timeouts"||e.runMode==="single-url")&&(e.crawlMode==="full"||e.crawlMode==="incremental")&&typeof e.fullSyncRequired=="boolean"&&Array.isArray(e.changedFiles)&&Array.isArray(e.deletedFiles)&&Array.isArray(e.rewriteTargets)}function we(r){let e=h.join(h.resolve(r.outputDir),de),t=he();return t?[h.join(t,me),e]:[e]}async function q(r){let e=h.resolve(r.outputDir);for(let t of we(r))try{let n=await b.readFile(t,"utf8"),i=JSON.parse(n);if(!be(i)||h.resolve(i.outputDir)!==e)continue;return Pe(r,i)}catch{}return null}function Se(r){return typeof r.subscriptionType=="string"&&r.subscriptionType.trim()!==""}function W(r){let e="";for(let n=0;n<r.length;n++){let i=r[n];if(i==="--profile"){let s=(r[n+1]||"").trim();if(!s)throw new Error("Missing value for --profile.");e=s,n+=1;}else if(i.startsWith("--profile=")){let s=i.slice(10).trim();if(!s)throw new Error("Missing value for --profile.");e=s;}}let t=String(process.env.PUBLISHER_DEPLOY_PROFILE||process.env.PUBLISHER_DEPLOYMENT_PROFILE||"").trim();return e||t||null}function ve(r,e){return {...r,...e??{}}}function Ie(r,e){return {...r,...e??{},invalidationPaths:[...(e?.invalidationPaths??r.invalidationPaths)||[]]}}function B(r,e){let t=String(e??r.deploymentTargetOverride??"").trim();if(!Se(r)){if(t)throw new Error(`Deployment target "${t}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:r}}let n=String(t||r.defaultDeploymentProfile||"").trim();if(!n)return {name:null,profile:null,config:r};let i=r.deploymentProfiles?.[n];if(!i)throw new Error(`Unknown deployment profile "${n}". Check the linked WPSuite publisher configuration.`);return {name:n,profile:i,config:{...r,targetOrigin:i.targetOrigin??r.targetOrigin,s3:ve(r.s3,i.s3),cloudFront:Ie(r.cloudFront,i.cloudFront)}}}var H={error:0,warn:1,info:2,debug:3},Ce=/^(?:authorization|proxyAuthorization|runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken|xStaticPublisherToken|xApiKey)$/i;function I(r){return r.replace(/((?:x-static-publisher-token|authorization|proxy-authorization|x-api-key|aws-access-key-id|aws-secret-access-key|aws-session-token)\s*:\s*)[^\r\n]+/gi,"$1[REDACTED]").replace(/\b(Basic|Bearer)\s+[^\s]+/gi,"$1 [REDACTED]").replace(/([?&](?:token|access_token|refresh_token|api_key|key|nonce)=)[^&\s]+/gi,"$1[REDACTED]").replace(/((?:"|')?(?:runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken)(?:"|')?\s*[:=]\s*(?:"|'))[^"']+/gi,"$1[REDACTED]")}function T(r){return typeof r=="string"?I(r):Array.isArray(r)?r.map(e=>T(e)):r instanceof Error?{name:r.name,message:I(r.message),...r.stack?{stack:I(r.stack)}:{}}:r&&typeof r=="object"?Object.fromEntries(Object.entries(r).map(([e,t])=>[e,Ce.test(e)?"[REDACTED]":T(t)])):r}function V(r){return r?T(r):void 0}var z=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",D=z?h.join(z,"current-progress.json"):"",J=Promise.resolve();function G(r){return r.includes("crawl")?"crawl":r.includes("deploy")?"deploy":r.includes("invalidate")?"invalidate":r}function De(r,e){let t={...r},n=new Set(["pagesQueued","pagesRendered","assetsQueued","sitemapsQueued","pagesDiscovered","assetsDiscovered","sitemapsDiscovered","pagesSaved","assetsSaved","donePages","doneAssets","doneSitemaps","changedTextFiles","uploaded","failed","index","totalFiles","pageQueue","assetQueue","sitemapQueue"]);for(let[i,s]of Object.entries(e)){let o=t[i];if(typeof o=="number"&&typeof s=="number"&&n.has(i)){t[i]=Math.max(o,s);continue}t[i]=s;}return t}function Te(r,e,t){D&&(J=J.then(async()=>{let n=new Date,i=n.toISOString();await b.mkdir(h.dirname(D),{recursive:true});let s=await b.readFile(D,"utf8").then(A=>JSON.parse(A)).catch(()=>null),o=s&&typeof s.details=="object"&&s.details?s.details:{},u=G(r),a=s?.currentStep||G(s?.source||""),l=s?.startedAt||i,c={...s?.stepDurationsSec??{}},d=s?.stepStartedAt||i;if(a&&a!==u&&s?.stepStartedAt){let A=Math.max(0,Math.round((n.getTime()-new Date(s.stepStartedAt).getTime())/1e3));c[a]=(c[a]??0)+A,d=i;}let g=Math.max(0,Math.round((n.getTime()-new Date(d).getTime())/1e3)),p=Math.max(0,Math.round((n.getTime()-new Date(l).getTime())/1e3)),m=I(e),w=V(t)??{},f={checkedAt:i,source:r,message:m,details:De(o,w),startedAt:l,currentStep:u,stepStartedAt:d,stepElapsedSec:g,totalElapsedSec:p,stepDurationsSec:c};await b.writeFile(D,JSON.stringify(f,null,2),"utf8");}).catch(()=>{}));}async function Re(r,e){await b.mkdir(h.dirname(r),{recursive:true}),await b.appendFile(r,`${JSON.stringify(T(e))}
2
+ `,"utf8");}var v=class{constructor(e,t,n="info"){this.logDir=e;this.logFile=t;this.level=n;this.ensureLogFileReady();}logDir;logFile;level;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;get logPath(){return h.join(this.logDir,this.logFile)}artifactPath(e){return this.logFile.endsWith(".log.jsonl")?h.join(this.logDir,this.logFile.replace(/\.log\.jsonl$/u,`${e}.jsonl`)):h.join(this.logDir,`${this.logFile}${e}.jsonl`)}get errorsPath(){return this.artifactPath(".errors")}get rejectedPath(){return this.artifactPath(".rejected")}ensureLogFileReady(){return this.initPromise||(this.initPromise=(async()=>{await b.mkdir(this.logDir,{recursive:true}),await b.writeFile(this.logPath,"","utf8");})()),this.initPromise}enqueueTask(e){this.writeQueue=this.writeQueue.then(e).catch(t=>{this.writeError=t instanceof Error?t:new Error(String(t));});}enqueueLine(e){this.enqueueTask(async()=>{await this.ensureLogFileReady(),await b.appendFile(this.logPath,`${e}
3
+ `,"utf8");});}enqueueJsonLine(e,t){this.enqueueTask(()=>Re(e,t));}accepts(e){return H[e]<=H[this.level]}push(e,t,n){if(!this.accepts(e))return;let i=I(t),s=V(n),o=JSON.stringify({time:new Date().toISOString(),level:e,message:i,...s||{}});this.enqueueLine(o),e==="error"?console.error(`[ERROR] ${i}`,s?JSON.stringify(s):""):e==="warn"?console.warn(`[WARN] ${i}`,s?JSON.stringify(s):""):console.log(`[${e.toUpperCase().padEnd(5)}] ${i}`);}info(e,t){this.push("info",e,t);}progress(e,t){Te(this.logFile,e,t);}debug(e,t){this.push("debug",e,t);}warn(e,t){this.push("warn",e,t);}reject(e,t,n,i,s){let o={kind:e,url:t,reason:n,...i?{source:i}:{},...s||{}};this.enqueueJsonLine(this.rejectedPath,o),this.accepts("debug")&&this.push("debug",`Rejected ${e} ${t}: ${n}`,o);}error(e,t){this.enqueueJsonLine(this.errorsPath,{message:e,...t||{}}),this.push("error",e,t);}mark(e){this.marks.set(e,Date.now());}endMark(e,t){let n=this.marks.get(e);if(!n)return;let i=Date.now()-n,s=Number((i/1e3).toFixed(2));this.push("info",`Timing ${e}: ${s}s`,{name:e,ms:i,seconds:s,...t||{}});}async flush(){let e=Date.now()-this.startedAt,t=Number((e/1e3).toFixed(2));if(this.push("info",`Total time: ${t}s`,{name:"total",ms:e,seconds:t}),await this.writeQueue,this.writeError)throw this.writeError}};function Ee(){let r=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();return r?h.join(h.resolve(r),"content-sync-invalidation.json"):""}async function F(r,e){if(!r)return;await b.mkdir(h.dirname(r),{recursive:true});let t=`${r}.${process.pid}.${Date.now()}.tmp`;await b.writeFile(t,JSON.stringify({...e,updatedAt:new Date().toISOString()},null,2),"utf8"),await b.rename(t,r);}async function Le(r,e,t){if(!r)return null;try{let n=JSON.parse(await b.readFile(r,"utf8"));if(n.schemaVersion!==1||n.distributionId!==e||n.impactHash!==t||!Array.isArray(n.batches))throw new Error("Content-sync invalidation state does not match the deploy plan.");return n}catch(n){if(n.code==="ENOENT")return null;throw n}}function xe(r,e=3e3){let t=[];for(let n=0;n<r.length;n+=e)t.push(r.slice(n,n+e));return t}async function Y(r=process.argv.slice(2)){let e=await C(),t=W(r),n=B(e,t),i=n.config,s=new v(i.logDir||"logs","invalidate.log.jsonl",i.logLevel??"info"),o=r.includes("--content-sync"),u=await q(e);if(o&&u?.schemaVersion!==2)throw new Error("Content-sync invalidation requires a valid schema-2 targeted deploy plan.");if(n.name&&s.info(`Using deployment profile: ${n.name}`),!i.cloudFront.distributionId){s.info("No CloudFront distributionId configured; skipping invalidation."),await s.flush();return}let a=u?.schemaVersion===2?u.invalidationPaths:i.cloudFront.invalidationPaths.length>0?i.cloudFront.invalidationPaths:["/*"];if(u?.schemaVersion===2&&a.some(p=>p==="/*"))throw new Error("Content-sync cannot use a whole-distribution invalidation.");if(a.length===0){s.info("The targeted deploy plan has no CloudFront paths to invalidate."),await s.flush();return}let l=i.cloudFront.distributionId,c=new CloudFrontClient({region:"us-east-1",maxAttempts:5});if(s.mark("invalidate"),u?.schemaVersion!==2){let p=`smartcloud-static-publisher-${Date.now()}`;await M(`CloudFront invalidation for distribution ${l}`,3e5,({httpOptions:m})=>c.send(new CreateInvalidationCommand({DistributionId:l,InvalidationBatch:{CallerReference:p,Paths:{Quantity:a.length,Items:a}}}),m)),s.endMark("invalidate"),await s.flush();return}let d=Ee(),g=await Le(d,l,u.impactHash);g||(g={schemaVersion:1,distributionId:l,impactHash:u.impactHash,updatedAt:new Date().toISOString(),batches:xe(a).map((p,m)=>({index:m,callerReference:`wpsuite-content-sync-${u.impactHash.slice(0,32)}-${m}`,paths:p,invalidationId:null,status:"pending"}))},await F(d,g));for(let p of g.batches){if(!p.invalidationId){let m=await M(`CloudFront targeted invalidation batch ${p.index+1}`,3e5,({httpOptions:f})=>c.send(new CreateInvalidationCommand({DistributionId:l,InvalidationBatch:{CallerReference:p.callerReference,Paths:{Quantity:p.paths.length,Items:p.paths}}}),f)),w=String(m.Invalidation?.Id||"").trim();if(!w)throw new Error("CloudFront did not return an invalidation ID.");p.invalidationId=w,p.status="created",await F(d,g);}await waitUntilInvalidationCompleted({client:c,maxWaitTime:900,minDelay:2,maxDelay:15},{DistributionId:l,Id:p.invalidationId}),p.status="completed",await F(d,g),s.info(`CloudFront invalidation batch ${p.index+1}/${g.batches.length} completed.`,{invalidationId:p.invalidationId,pathCount:p.paths.length});}s.endMark("invalidate"),await s.flush();}Y().catch(async r=>{console.error(r);try{let e=await C().catch(()=>null),t=new v(e?.logDir??"logs","invalidate.log.jsonl","debug");t.error(`Unhandled error: ${r instanceof Error?r.message:String(r)}`,r instanceof Error?{stack:r.stack}:void 0),await t.flush();}catch{}process.exit(1);});