@smart-cloud/publisher-exporter 1.1.59 → 1.1.61

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.
@@ -1,14 +1,14 @@
1
- import A from'fs/promises';import Rs from'http';import qn from'https';import S from'path';import ar,{createHash}from'crypto';import {request,chromium}from'playwright';import {gunzipSync}from'zlib';import Vs from'fast-glob';import {availableParallelism}from'os';import {Worker}from'worker_threads';import {S3Client,ListObjectsV2Command,HeadObjectCommand,GetObjectCommand,DeleteObjectsCommand}from'@aws-sdk/client-s3';import {Upload}from'@aws-sdk/lib-storage';import ua from'mime-types';import {CloudFrontClient,waitUntilInvalidationCompleted,CreateInvalidationCommand}from'@aws-sdk/client-cloudfront';var Ss=new Set(["post-type","taxonomy","author","date","posts-page","listing"]);function Se(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function bs(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))]}function Ps(e){return new URL(e).pathname.split("/").filter(Boolean).some(n=>{try{return decodeURIComponent(n).endsWith("__trashed")}catch{return n.endsWith("__trashed")}})}function _t(e){return Array.isArray(e)?e.map(_t):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>[t,_t(n)])):e}function vs(e){return JSON.stringify(_t(e))}function le(e){return createHash("sha256").update(vs(e)).digest("hex")}function Dn(e){return le({schemaVersion:1,wordpressFingerprint:e.wordpressFingerprint,targetFingerprint:e.targetFingerprint,sourceOrigin:e.sourceOrigin,sitemapPaths:[...e.sitemapPaths].sort(),blockedPathPrefixes:[...e.blockedPathPrefixes].sort(),assetPathPrefixes:[...e.assetPathPrefixes].sort(),noJavaScriptRenderPathPrefixes:[...e.noJavaScriptRenderPathPrefixes].sort(),urlRewriteMode:e.urlRewriteMode})}function Nt(e){let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")throw new Error(`Unsupported source origin protocol: ${t.protocol}`);return t.hash="",t.search="",t.pathname=t.pathname.replace(/\/+$/,""),t.toString().replace(/\/$/,"")}function ie(e,t){let n=new URL(Nt(e)),r=new URL(t,`${n.toString().replace(/\/$/,"")}/`);if(r.origin!==n.origin)throw new Error(`Content-sync URL is outside the source origin: ${t}`);let s=n.pathname.replace(/\/+$/,"");if(s&&r.pathname!==s&&!r.pathname.startsWith(`${s}/`))throw new Error(`Content-sync URL is outside the source site path: ${t}`);return r.hash="",r.toString()}function Je(e){let t=Se((e.postTypes??[]).map(r=>String(r).toLowerCase().replace(/[^a-z0-9_-]/g,"")));if(t.length===0)throw new Error("Content-sync requires at least one public post type.");let n=Se((e.listingPaths??[]).map(r=>{let s=String(r).trim();if(!s)return "";let i=new URL(s,"https://content-sync.invalid/");if(i.origin!=="https://content-sync.invalid"&&/^https?:\/\//i.test(s))throw new Error("Content-sync listing routes must be same-site paths.");let a=`/${i.pathname.replace(/^\/+|\/+$/g,"")}`;return a==="/"?"/":`${a}/`}));return {postTypes:t,listingPaths:n,includeSubsites:e.includeSubsites===true,includePostTypeArchives:e.includePostTypeArchives!==false,includeTaxonomyArchives:e.includeTaxonomyArchives!==false,includeAuthorArchives:e.includeAuthorArchives===true,includeDateArchives:e.includeDateArchives===true,includePostsPage:e.includePostsPage!==false,includeSitemapChain:e.includeSitemapChain!==false}}function On(e,t,n){return le({schemaVersion:1,sourceOrigin:Nt(e),ruleId:t.trim(),scope:Je(n)})}function Fn(e){return le({schemaVersion:1,...e})}function Un(e){let t=String(e.siteId??"").trim()||String(e.accountId??"").trim()||Nt(e.sourceOrigin);return `content-sync:${le({siteIdentity:t,ruleId:e.ruleId.trim(),targetFingerprint:e.targetFingerprint}).slice(0,48)}`}function Ln(e){let t=[...e].sort((s,i)=>s.sequence-i.sequence),n=new Map,r=-1;for(let s of t){if(!Number.isSafeInteger(s.sequence)||s.sequence<=r)throw new Error("Content-sync journal events are not strictly ordered.");if(r=s.sequence,!Number.isSafeInteger(s.blogId)||s.blogId<1)throw new Error("Content-sync journal event has an invalid multisite identity.");let i=`${s.blogId}:${s.postType}:${s.postId}`,a=n.get(i);if(!a){n.set(i,{postId:s.postId,blogId:s.blogId,postType:s.postType,fromSequence:s.sequence,toSequence:s.sequence,before:s.before,after:s.after,operations:[s.operation],correlationIds:s.correlationId?[s.correlationId]:[]});continue}a.toSequence=s.sequence,a.after=s.after,a.operations.push(s.operation),s.correlationId&&!a.correlationIds.includes(s.correlationId)&&a.correlationIds.push(s.correlationId);}return [...n.values()].sort((s,i)=>s.fromSequence-i.fromSequence)}function Mn(e){return e?.status==="publish"&&!!e.url}function Cs(e,t){return e==="post-type"?t.includePostTypeArchives:e==="taxonomy"?t.includeTaxonomyArchives:e==="author"?t.includeAuthorArchives:e==="date"?t.includeDateArchives:e==="posts-page"?t.includePostsPage:e==="listing"}function Bt(e){let t=Je(e.scope),n=new Map;Ln(e.events);for(let r of e.events)for(let s of [r.before,r.after])for(let i of s?.archiveFamilies??[]){if(!Ss.has(i.kind)||!Cs(i.kind,t))continue;let a={...i,url:ie(e.sourceOrigin,i.url)};n.set(`${a.kind}|${a.url}`,a);}for(let r of t.listingPaths){let s=ie(e.sourceOrigin,r);n.set(`listing|${s}`,{kind:"listing",url:s});}return [...n.values()].sort((r,s)=>r.url.localeCompare(s.url))}function jn(e){let t=Je(e.scope),n=Ln(e.events),r=new Set,s=new Set,i=new Map,a=new Map(Bt({events:e.events,scope:t,sourceOrigin:e.sourceOrigin}).map(p=>[`${p.kind}|${p.url}`,p]));for(let p of n){let y=p.before,m=p.after,h=Mn(y),w=Mn(m);w&&m.url&&r.add(ie(e.sourceOrigin,m.url)),h&&y.url&&(!w||y.url!==m.url)&&s.add(ie(e.sourceOrigin,y.url));}for(let p of [...e.events].sort((y,m)=>y.sequence-m.sequence)){for(let y of [p.before,p.after])for(let m of y?.resourceDeleteUrls??[])i.set(ie(e.sourceOrigin,m),"delete");for(let y of p.after?.resourceRenderUrls??[])i.set(ie(e.sourceOrigin,y),"render");}let c=[...i].filter(([,p])=>p==="render").map(([p])=>p),o=[...i].filter(([,p])=>p==="delete").map(([p])=>p),u=new Map(e.resolvedArchiveFamilies.map(p=>[`${p.kind}|${ie(e.sourceOrigin,p.url)}`,{...p,url:ie(e.sourceOrigin,p.url),pageUrls:Se(p.pageUrls.map(y=>ie(e.sourceOrigin,y)))}])),d=[...a.entries()].map(([p,y])=>{let m=u.get(p);if(!m)throw new Error(`WordPress did not resolve archive family ${p}.`);return {...y,...m}}).sort((p,y)=>p.url.localeCompare(y.url)),l=Se(d.flatMap(p=>p.pageUrls)),g=t.includeSitemapChain?bs(e.sitemapUrls.map(p=>ie(e.sourceOrigin,p))):[],f={schemaVersion:1,generatedAt:new Date().toISOString(),jobId:e.jobId,ruleId:e.ruleId,consumerId:e.consumerId,coalesceKey:e.coalesceKey,scopeFingerprint:e.scopeFingerprint,targetFingerprint:e.targetFingerprint,baselineId:e.baselineId,fromSequence:e.fromSequence,toSequence:e.toSequence,eventCount:e.events.length,foldedCount:n.length,directRenderUrls:Se([...r]),archiveFamilyUrls:l,sitemapUrls:g,deleteUrls:Se([...s]),resourceRenderUrls:Se(c),resourceDeleteUrls:Se(o),archiveFamilies:d};return {...f,impactHash:le(f)}}function Ve(e){let{impactHash:t,...n}=e;if(e.schemaVersion!==1||!t)throw new Error("Invalid content-sync impact plan schema.");if(!Array.isArray(e.resourceRenderUrls)||!Array.isArray(e.resourceDeleteUrls))throw new Error("Invalid content-sync resource impact contract.");if(le(n)!==t)throw new Error("Content-sync impact plan hash mismatch.");for(let r of [...e.directRenderUrls,...e.deleteUrls,...e.resourceRenderUrls,...e.resourceDeleteUrls])if(Ps(r))throw new Error(`Content-sync impact contains a WordPress trash alias instead of the last public URL: ${r}`)}var xs="wp-json/smartcloud-static-publisher/v1/content-sync/",Is=25*1024*1024,dt=class extends Error{status;code;constructor(t,n,r){super(r),this.name="ContentSyncApiError",this.status=t,this.code=n;}},pt=class{options;constructor(t){if(!t.runtimeToken.trim())throw new Error("Content-sync requires a WordPress runtime token.");this.options={...t,timeoutMs:t.timeoutMs??6e4};}endpoint(t,n="content-sync"){let r=this.options.sourceOrigin.replace(/\/+$/,"")+"/",s=n==="content-sync"?xs:`wp-json/smartcloud-static-publisher/v1/${n}/`;return new URL(`${s}${t}`,r)}async post(t,n,r="content-sync"){let s=this.endpoint(t,r),i=Buffer.from(JSON.stringify(n),"utf8"),a=s.protocol==="https:"?qn:Rs,c=s.protocol==="https:"?new qn.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((o,u)=>{let d=a.request(s,{method:"POST",agent:c,headers:{"content-type":"application/json","content-length":String(i.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},l=>{let g=[],f=0;l.on("data",p=>{if(f+=p.byteLength,f>Is){l.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(p);}),l.on("error",u),l.on("end",()=>{let p=Buffer.concat(g).toString("utf8"),y;try{y=p?JSON.parse(p):{};}catch{u(new dt(l.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let m=l.statusCode??500;if(m<200||m>=300){u(new dt(m,String(y.code??"request-failed"),String(y.message??`Content-sync API request failed (${m}).`)));return}o(y);});});d.on("timeout",()=>d.destroy(new Error(`Content-sync API request timed out: ${t}`))),d.on("error",u),d.end(i);})}async head(t){return this.post("head",t)}async unboundHead(t,n){return this.post("head",{postTypes:t,includeSubsites:n})}async events(t){let n=[],r=t.afterSequence,s=new Set(t.postTypes);for(;r<t.throughSequence;){let i=await this.post("events",{...t,afterSequence:r,limit:250});if(i.fromSequence!==r||i.throughSequence!==t.throughSequence||i.nextSequence<r||i.nextSequence>t.throughSequence)throw new Error("Content-sync API returned an inconsistent event page.");if(!Array.isArray(i.items))throw new Error("Content-sync API returned a malformed event page.");let a=r;for(let c of i.items){if(!Number.isSafeInteger(c.sequence)||c.sequence<=a||c.sequence>t.throughSequence||!Number.isSafeInteger(c.postId)||c.postId<=0||!Number.isSafeInteger(c.blogId)||c.blogId<=0||!s.has(c.postType)||typeof c.operation!="string"||!c.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");a=c.sequence;}if(i.items.length>0&&i.nextSequence!==a)throw new Error("Content-sync API event cursor does not match the final returned event.");if(n.push(...i.items),!i.hasMore)break;if(i.nextSequence===r)throw new Error("Content-sync API event pagination did not advance.");r=i.nextSequence;}if(t.throughSequence>t.afterSequence&&n.at(-1)?.sequence!==t.throughSequence)throw new Error("Content-sync API omitted the claimed journal high-water event.");return n}async resolveImpactFamilies(t,n){let r=await this.post("impact",{families:t,includeSubsites:n});return Array.isArray(r.families)?r.families:[]}async releaseFingerprint(t=false){let n=await this.post("fingerprint",{includeSubsites:t}),r=String(n.fingerprint||"").trim();if(!/^[a-f0-9]{64}$/.test(r))throw new Error("WordPress returned an invalid release fingerprint.");return r}async establishBaseline(t){return this.post("baseline",t)}async acknowledge(t){return this.post("ack",t)}async importCookieObservations(t){return this.post("cookie-observations",t,"privacy")}};function gt(e){let t=S.resolve(e);return {state:S.join(t,"content-sync-state.json"),current:S.join(t,"content-sync-current.json"),impact:S.join(t,"content-sync-impact-plan.json"),checkpoint:S.join(t,"content-sync-checkpoint.json"),baseline:S.join(t,"content-sync-baseline.json"),activeRules:S.join(t,"content-sync-active-rules.json"),releaseCutoff:S.join(t,"content-sync-release-cutoff.json"),candidateManifest:S.join(t,"content-sync-candidate-manifest.json"),trustedManifest:S.join(t,"crawl-manifest.json"),invalidation:S.join(t,"content-sync-invalidation.json"),deployPlan:S.join(t,"deploy-plan.json")}}async function Wt(e){let t=await Ke(e.releaseCutoff);if(t===null)return {schemaVersion:1,updatedAt:"",entries:{}};let n=t;if(n.schemaVersion!==1||typeof n.updatedAt!="string"||!n.entries||typeof n.entries!="object"||Array.isArray(n.entries))throw new Error("Invalid content-sync release-cutoff store.");return n}async function _n(e,t){await Ee(e.releaseCutoff,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Ee(e,t){await A.mkdir(S.dirname(e),{recursive:true});let n=`${e}.${process.pid}.${Date.now()}.tmp`;await A.writeFile(n,JSON.stringify(t,null,2),"utf8"),await A.rename(n,e);}async function Ke(e){try{return JSON.parse(await A.readFile(e,"utf8"))}catch(t){if(t.code==="ENOENT")return null;throw new Error(`Invalid or unreadable content-sync state file ${e}: ${t instanceof Error?t.message:String(t)}`,{cause:t})}}async function Ht(e){let t=await Ke(e.baseline);if(t===null)return {schemaVersion:1,updatedAt:"",entries:{}};let n=t;if(n.schemaVersion!==1||typeof n.updatedAt!="string"||!n.entries||typeof n.entries!="object"||Array.isArray(n.entries))throw new Error("Invalid content-sync baseline store.");return n}async function Nn(e,t){await Ee(e.baseline,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function zt(e){let t=await Ke(e.current);if(t===null)return null;let n=t;if(n.schemaVersion!==1||typeof n.jobId!="string"||typeof n.phase!="string"||!Number.isSafeInteger(n.fromSequence)||!Number.isSafeInteger(n.toSequence))throw new Error("Invalid content-sync current-range state.");return n}async function Bn(e,t){await Ee(e.current,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Wn(e){let t=await Ke(e.impact);if(t===null)return null;let n=t;return Ve(n),n}async function Hn(e,t){Ve(t),await Ee(e.impact,t);}async function zn(e,t){await Ee(e.checkpoint,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function ft(e){let t=await Ke(e.state);if(t===null)return {schemaVersion:1,updatedAt:"",rules:{}};let n=t;if(n.schemaVersion!==1||typeof n.updatedAt!="string"||!n.rules||typeof n.rules!="object"||Array.isArray(n.rules))throw new Error("Invalid content-sync state store.");return n}async function mt(e,t){await Ee(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Qt(e){await Promise.all([e.current,e.checkpoint].map(async t=>{try{await A.unlink(t);}catch(n){if(n.code!=="ENOENT")throw n}}));}async function Qn(e,t){let n=await zt(e);return !n||n.coalesceKey!==t?false:(await Promise.all([e.current,e.checkpoint,e.impact,e.candidateManifest,e.invalidation,e.deployPlan].map(async r=>{try{await A.unlink(r);}catch(s){if(s.code!=="ENOENT")throw s}})),true)}var Jt=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function Ge(e){let t=new Set;for(let n of [...e,...Jt]){let r=String(n||"").trim();r&&t.add(r);}return [...t]}function Vt(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function Jn(e){if(!e||!/^https?:\/\//i.test(e))return false;try{return Vt(new URL(e))}catch{return false}}function Kn(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function $s(e){let t=String(e??"").trim();if(!t)return "";let n=t;if(/^https?:\/\//i.test(n))try{n=new URL(n).pathname;}catch{return ""}if(n=n.split(/[?#]/,1)[0]?.replace(/\\/g,"/").trim()??"",!n)return "";let r=n.endsWith("/"),s=n.replace(/^\/+/,"").split("/").map(a=>a.replace(/[^A-Za-z0-9._-]/g,"")).filter(a=>a.length>0&&a!=="."&&a!=="..");if(s.length===0)return "";let i=`/${s.join("/")}`;return r&&S.extname(i)===""&&(i+="/"),i==="/"?"":i}function Kt(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t.trim(),String(n??"")]).filter(([t])=>t.length>0))}function Es(e){if(!e||typeof e!="object")return {};let t={};for(let[n,r]of Object.entries(e)){let s=n.trim();if(!s||!r||typeof r!="object")continue;let i=r,a={};if(typeof i.targetOrigin=="string"){let o=Kn(i.targetOrigin);o&&(a.targetOrigin=o);}let c=Kt(i.extraReplacements);if(Object.keys(c).length>0&&(a.extraReplacements=c),i.s3&&typeof i.s3=="object"){let o={},u=i.s3;typeof u.bucket=="string"&&(o.bucket=u.bucket.trim()),typeof u.prefix=="string"&&(o.prefix=u.prefix.trim()),typeof u.region=="string"&&(o.region=u.region.trim()),typeof u.htmlCacheControl=="string"&&(o.htmlCacheControl=u.htmlCacheControl.trim()),typeof u.assetCacheControl=="string"&&(o.assetCacheControl=u.assetCacheControl.trim()),Object.keys(o).length>0&&(a.s3=o);}if(i.cloudFront&&typeof i.cloudFront=="object"){let o={},u=i.cloudFront;typeof u.distributionId=="string"&&(o.distributionId=u.distributionId.trim()),Array.isArray(u.invalidationPaths)&&(o.invalidationPaths=u.invalidationPaths.map(d=>String(d??"").trim()).filter(d=>d.length>0)),Object.keys(o).length>0&&(a.cloudFront=o);}t[s]=a;}return t}function Zt(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function Ms(e,t){if(!e||typeof e!="object")return null;let n=e,r=n.command;if(r!=="publish"&&r!=="crawl"&&r!=="deploy"&&r!=="invalidate"&&r!=="retry-timeouts"&&r!=="url"&&r!=="content-sync")return null;let s=Number.parseInt(String(n.intervalMinutes??"0"),10);if(!Number.isFinite(s)||s<1)return null;let i=String(n.id??`${r}-${t+1}`).trim();if(!i)return null;let a=String(n.deploymentProfile??"").trim(),c=String(n.url??"").trim(),o=Array.isArray(n.postTypes)?[...new Set(n.postTypes.map(d=>String(d??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],u=Array.isArray(n.listingPaths)?[...new Set(n.listingPaths.map(d=>String(d??"").trim()).filter(Boolean))]:[];return r==="content-sync"&&o.length===0?null:{id:i,enabled:n.enabled!==false,command:r,intervalMinutes:s,...r==="publish"||r==="crawl"?{crawlMode:n.crawlMode==="incremental"?"incremental":"full"}:{},...(r==="publish"||r==="deploy"||r==="invalidate"||r==="content-sync")&&a?{deploymentProfile:a}:{},...c?{url:c}:{},...o.length>0?{postTypes:o}:{},...u.length>0?{listingPaths:u}:{},...typeof n.includeSubsites=="boolean"?{includeSubsites:n.includeSubsites}:{},...typeof n.includePostTypeArchives=="boolean"?{includePostTypeArchives:n.includePostTypeArchives}:{},...typeof n.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:n.includeTaxonomyArchives}:{},...typeof n.includeAuthorArchives=="boolean"?{includeAuthorArchives:n.includeAuthorArchives}:{},...typeof n.includeDateArchives=="boolean"?{includeDateArchives:n.includeDateArchives}:{},...typeof n.includePostsPage=="boolean"?{includePostsPage:n.includePostsPage}:{},...typeof n.includeSitemapChain=="boolean"?{includeSitemapChain:n.includeSitemapChain}:{}}}function Gn(e){let t=e&&typeof e=="object"?e:{},n=Array.isArray(t.rules)?t.rules.map((r,s)=>Ms(r,s)).filter(r=>!!r):[];return {enabled:t.enabled===true,timezone:typeof t.timezone=="string"&&t.timezone.trim()!==""?t.timezone.trim():"UTC",rules:n}}function Ds(e){if(!e||typeof e!="object")return;let t=e,n={};for(let s of ["accountId","siteId"]){let i=t[s];typeof i=="string"&&i.trim()!==""&&(n[s]=i.trim());}let r=Number(t.lastUpdate??0);return Number.isFinite(r)&&r>0&&(n.lastUpdate=Math.floor(r)),t.subscriber===true&&(n.subscriber=true),Object.keys(n).length>0?n:void 0}function Yt(e){let t=e&&typeof e=="object"?e:{},n=String(t.accountId??"").trim(),r=String(t.siteId??"").trim(),s=t.subscriber===true,i=Ds(t.siteSettings),a={...i??{},...n?{accountId:i?.accountId??n}:{},...r?{siteId:i?.siteId??r}:{},...i?.subscriber===true||s?{subscriber:true}:{}},c={},o=String(t.apiBase??"").trim();o&&(c.apiBase=o);let u=String(t.runtimeToken??t.nonce??"").trim();u&&(c.runtimeToken=u);let d=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();d&&(c.virtualAssetBaseUrl=d),Object.keys(a).length>0&&(c.siteSettings=a);let l=Zt(t.subscriptionType);return l&&(c.subscriptionType=l),c}function Os(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function Zn(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function Fs(e,t){let n=new Headers(e instanceof Request?e.headers:void 0);if(t?.headers)for(let[r,s]of new Headers(t.headers).entries())n.set(r,s);return n}async function Yn(e,t,n=5){let r=new URL(Zn(e)),s=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),i=r.protocol==="http:"?Rs:qn,a=Fs(e,t);return await new Promise((c,o)=>{let u=i.request(r,{method:s,headers:Object.fromEntries(a.entries()),...r.protocol==="https:"?{rejectUnauthorized:false}:{}},d=>{let l=[];d.on("data",g=>{l.push(Buffer.isBuffer(g)?g:Buffer.from(g));}),d.on("error",o),d.on("end",()=>{let g=d.statusCode??0,f=d.headers.location;if(n>0&&f&&[301,302,303,307,308].includes(g)){c(Yn(new URL(f,r),{method:g===303?"GET":s},n-1));return}let p=new Headers;for(let[y,m]of Object.entries(d.headers))Array.isArray(m)?p.set(y,m.join(", ")):typeof m=="string"&&p.set(y,m);c(new Response(Buffer.concat(l),{status:g,statusText:d.statusMessage??"",headers:p}));});});u.on("error",o),u.end();})}function Us(e){let t=Yt(e.wpsuite),n={...t.siteSettings??{},...t.siteSettings?.subscriber===true||t.subscriptionType==="PROFESSIONAL"||t.subscriptionType==="AGENCY"?{subscriber:true}:{}},r=String(n.accountId??"").trim(),s=String(n.siteId??"").trim(),i=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();if(e.wpsuite={...t.apiBase?{apiBase:t.apiBase}:{},...t.runtimeToken?{runtimeToken:t.runtimeToken}:{},...i?{virtualAssetBaseUrl:i}:{},...Object.keys(n).length>0?{siteSettings:n}:{},...t.subscriptionType?{subscriptionType:t.subscriptionType}:{}},!r||!s||!i)return false;Os(e.sourceOrigin);let a=globalThis,o={...a.WpSuite??{},siteSettings:n,uploadUrl:i};return t.apiBase&&(o.apiBase=t.apiBase),a.WpSuite=o,true}async function Ls(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!Us(e)){let s=e.wpsuite?.siteSettings,i=[String(s?.accountId??"").trim()?"":"accountId",String(s?.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): ${i.join(", ")}.`,requests:[]}}}let n=globalThis.fetch,r=[];try{typeof n=="function"&&/^https?:\/\//i.test(t)&&(globalThis.fetch=(async(o,u)=>{let d=Zn(o);if(d.startsWith(t)){let l=(()=>{try{return new URL(d).pathname}catch{return d}})();try{let g=e.ignoreHttpsErrors&&d.startsWith("https://")?await Yn(o,u):await n(o,u);return r.push({path:l,status:g.status,ok:g.ok,error:null}),g}catch(g){throw r.push({path:l,status:null,ok:!1,error:g instanceof Error?g.message:String(g)}),g}}return n(o,u)}));let{getConfig:s}=await import('@smart-cloud/wpsuite-core'),i=await s("publisher"),a=i&&typeof i=="object"?i:null,c=Zt(a?.subscriptionType);return {remote:a,diagnostic:{status:a?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:c??null,error:a?null:"WP Suite publisher config loader returned no configuration.",requests:r}}}catch(s){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:t,subscriptionType:null,error:s instanceof Error?s.message:String(s),requests:r}}}finally{typeof n=="function"&&(globalThis.fetch=n);}}function js(e,t){let n=Es(t?.deploymentProfiles),r=String(t?.defaultDeploymentProfile??"").trim(),s=n[r]?r:"",i=Zt(t?.subscriptionType),a=Yt(e.wpsuite);return {...e,scheduler:Gn(t?.scheduler),deploymentProfiles:n,defaultDeploymentProfile:s,...i?{subscriptionType:i}:{},wpsuite:{...a,...a.siteSettings||i?{siteSettings:{...a.siteSettings??{},...a.siteSettings?.subscriber===true||i==="PROFESSIONAL"||i==="AGENCY"?{subscriber:true}:{}}}:{},...i?{subscriptionType:i}:{}}}}function Gt(e,t){let r=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!r)return t;let s=r.split("/").map(i=>i.trim()).filter(i=>i.length>0&&i!=="."&&i!=="..");return s.length>0?s.join("/"):t}function qs(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?S.resolve(e):""}function Vn(e,t,n){let r=String(t||"").trim();return r&&S.isAbsolute(r)?S.resolve(r):S.resolve(e,Gt(r,n))}async function oe(e,t={}){let n=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",r=await A.readFile(n,"utf8"),s=JSON.parse(r);s.sourceOrigin=s.sourceOrigin.replace(/\/$/,""),s.targetOrigin=Kn(s.targetOrigin),s.ignoreHttpsErrors??=false,s.outputDir=String(s.outputDir||"export").trim()||"export",s.urlRewriteMode||=s.targetOrigin==="."?"relative":"absolute",s.noJavaScriptRenderPathPrefixes||=[],s.seedPaths||=[],s.generated404RequestPath=$s(s.generated404RequestPath),s.sitemapPaths||=[...Jt],s.allowedAssetHosts||=[],s.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],s.blockedPathPrefixes||=[],s.blockedSearchFragments||=[],s.concurrency||=1,s.maxPages||=0,s.extraReplacements=Kt(s.extraReplacements),s.postCrawlCopyMap=Kt(s.postCrawlCopyMap),s.logDir=String(s.logDir||"logs").trim()||"logs",s.verbose??=false,s.logLevel||=s.verbose?"debug":"info",s.s3SyncMode||="sdk-upload-delete",s.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},s.readiness.timeoutMs??=1500,s.readiness.fallbackWaitMs??=1500,s.viewport||={width:1440,height:1200},s.navigationTimeoutMs||=3e4,s.assetDownloadConcurrency=Number(s.assetDownloadConcurrency)>0?Number(s.assetDownloadConcurrency):s.concurrency,s.rewriteConcurrency=Number(s.rewriteConcurrency)>0?Number(s.rewriteConcurrency):s.assetDownloadConcurrency,s.wpsuite=Yt(s.wpsuite),s.scheduler=Gn(void 0),s.deploymentProfiles={},s.defaultDeploymentProfile="",s.deploymentTargetOverride=String(s.deploymentTargetOverride??"").trim();let i=await Ls(s);t.onRemotePublisherConfigDiagnostic?.(i.diagnostic);let a=js(s,i.remote),c=qs(),o=c?S.resolve(c,".."):"";return o?(a.outputDir=Vn(o,a.outputDir,"export"),a.logDir=Vn(o,a.logDir,"logs")):(S.isAbsolute(a.outputDir)||(a.outputDir=Gt(a.outputDir,"export")),S.isAbsolute(a.logDir)||(a.logDir=Gt(a.logDir,"logs"))),a}var _s=".deploy-plan.json",Ns="deploy-plan.json",Bs=".crawl-incomplete.json",Ws="crawl-incomplete.json";function Xn(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?S.resolve(e):""}function er(e){let t=String(e||"").replace(/\\/g,"/").replace(/^\/+/,"").trim(),n=S.posix.normalize(t);return !t||!n||n==="."||/^[a-zA-Z]:/.test(n)||n.startsWith("../")||n===".."?null:n}function Hs(e){let t=String(e||"").trim();if(!t.startsWith("/")||t.startsWith("//")||t.includes("*")||t.includes("#"))return null;try{let n=new URL(t,"https://content-sync.invalid");return n.origin!=="https://content-sync.invalid"?null:`${S.posix.normalize(n.pathname)}${n.search}`}catch{return null}}function de(e){return [...new Set(e.map(er).filter(t=>t!==null))]}function tr(e,t){let n={generatedAt:String(t.generatedAt||new Date().toISOString()),outputDir:S.resolve(e.outputDir),runMode:t.runMode,crawlMode:t.crawlMode,fullSyncRequired:!!t.fullSyncRequired,changedFiles:de(t.changedFiles||[]),deletedFiles:de(t.deletedFiles||[]),rewriteTargets:de(t.rewriteTargets||[])};return t.schemaVersion===2?{...n,schemaVersion:2,runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,consumerId:String(t.consumerId||"").trim(),fromSequence:Math.max(0,Number(t.fromSequence)||0),toSequence:Math.max(0,Number(t.toSequence)||0),impactHash:String(t.impactHash||"").trim(),resolvedSitemapUrls:[...new Set((t.resolvedSitemapUrls||[]).map(r=>String(r||"").trim()).filter(r=>{try{let s=new URL(r);return s.protocol==="http:"||s.protocol==="https:"}catch{return false}}))],uploadGroups:{assets:de(t.uploadGroups?.assets||[]),content:de(t.uploadGroups?.content||[]),listings:de(t.uploadGroups?.listings||[]),resources:de(t.uploadGroups?.resources||[]),sitemapLeaves:de(t.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:de(t.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((t.invalidationPaths||[]).map(Hs).filter(r=>r!==null))].sort()}:{schemaVersion:1,...n,runMode:t.runMode}}function zs(e){if(!e||typeof e!="object")return false;let t=e;if(t.schemaVersion===2){let n=t;return n.runMode==="content-sync"&&n.crawlMode==="incremental"&&n.fullSyncRequired===false&&typeof n.generatedAt=="string"&&typeof n.outputDir=="string"&&typeof n.consumerId=="string"&&n.consumerId.trim()!==""&&Number.isSafeInteger(n.fromSequence)&&Number.isSafeInteger(n.toSequence)&&Number(n.toSequence)>=Number(n.fromSequence)&&typeof n.impactHash=="string"&&/^[a-f0-9]{64}$/.test(n.impactHash)&&Array.isArray(n.resolvedSitemapUrls)&&Array.isArray(n.changedFiles)&&Array.isArray(n.deletedFiles)&&Array.isArray(n.rewriteTargets)&&!!n.uploadGroups&&Array.isArray(n.uploadGroups.assets)&&Array.isArray(n.uploadGroups.content)&&Array.isArray(n.uploadGroups.listings)&&Array.isArray(n.uploadGroups.resources)&&Array.isArray(n.uploadGroups.sitemapLeaves)&&Array.isArray(n.uploadGroups.sitemapIndexes)&&Array.isArray(n.invalidationPaths)}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 Xt(e){let t=S.join(S.resolve(e.outputDir),_s),n=Xn();return n?[S.join(n,Ns),t]:[t]}function en(e){let t=S.join(S.resolve(e.outputDir),Bs),n=Xn();return n?[S.join(n,Ws),t]:[t]}function pe(e,t){let n=S.resolve(e),r=S.resolve(t),s=S.relative(n,r).replace(/\\/g,"/");return er(s)}async function Me(e){let t=S.resolve(e.outputDir);for(let n of Xt(e))try{let r=await A.readFile(n,"utf8"),s=JSON.parse(r);if(!zs(s)||S.resolve(s.outputDir)!==t)continue;return tr(e,s)}catch{}return null}async function tn(e,t){let n=tr(e,t);for(let r of Xt(e)){await A.mkdir(S.dirname(r),{recursive:true});let s=`${r}.${process.pid}.${Date.now()}.tmp`;await A.writeFile(s,JSON.stringify(n,null,2),"utf8"),await A.rename(s,r);}}async function De(e){for(let t of Xt(e))try{await A.unlink(t);}catch(n){if(n.code!=="ENOENT")throw n}}async function nr(e,t){let n={schemaVersion:1,startedAt:String(t.startedAt||new Date().toISOString()),outputDir:S.resolve(e.outputDir)};for(let r of en(e)){await A.mkdir(S.dirname(r),{recursive:true});let s=`${r}.${process.pid}.tmp`;await A.writeFile(s,JSON.stringify(n,null,2),"utf8"),await A.rename(s,r);}}async function rr(e){let t=S.resolve(e.outputDir);for(let n of en(e)){let r;try{r=await A.readFile(n,"utf8");}catch(i){if(i.code==="ENOENT")continue;throw i}let s;try{s=JSON.parse(r);}catch{throw new Error(`Invalid incomplete crawl marker: ${n}`)}if(s.schemaVersion!==1||typeof s.startedAt!="string"||typeof s.outputDir!="string"||S.resolve(s.outputDir)!==t)throw new Error(`Invalid incomplete crawl marker: ${n}`);return {schemaVersion:1,startedAt:s.startedAt,outputDir:t}}return null}async function sr(e){for(let t of en(e))try{await A.unlink(t);}catch(n){if(n.code!=="ENOENT")throw n}}function Ye(e,t=10){return ar.createHash("sha1").update(e).digest("hex").slice(0,t)}async function Qs(e){await A.mkdir(S.dirname(e),{recursive:true});}async function Xe(e,t,n){await Qs(e);let r=S.join(S.dirname(e),`.${S.basename(e)}.${process.pid}.${Date.now()}.${ar.randomBytes(6).toString("hex")}.tmp`);try{typeof t=="string"?await A.writeFile(r,t,n??"utf8"):await A.writeFile(r,t),await A.rename(r,e);}catch(s){throw await A.unlink(r).catch(()=>{}),s}}function Pe(e){return [".html",".htm",".css",".js",".mjs",".json",".xml",".xsl",".txt",".svg",".map",".enc",".jws"].includes(S.extname(e).toLowerCase())}function fe(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 Oe(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function ht(e){return e.replace(/\//g,"\\/")}function ir(e){return e.replace(/\//g,"\\\\/")}function Js(e){try{let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")return null;let n=t.pathname==="/"&&!t.search&&!t.hash?"":`${t.pathname}${t.search}${t.hash}`;return `//${t.host}${n}`}catch{return null}}function or(e,t,n){if(!t)return;e[t]=n;let r=ht(t),s=ht(n);e[r]=s;let i=ir(t),a=ir(n);e[i]=a;let c=Oe(t),o=Oe(n);e[c]=o;let u=Oe(r),d=Oe(s);e[u]=d;let l=Oe(i),g=Oe(a);e[l]=g;}function wt(e,t,n){or(e,t,n);let r=Js(t);r&&r!==t&&or(e,r,n);}function nn(e,t){let n=t.startsWith("/")?t:`/${t}`,r=(e||"").replace(/\/$/,"");return !r||r==="."||r==="/"?n:`${r}${n}`}function cr(e,t){try{return new URL(e,t==="."?"https://relative.invalid":t).pathname}catch{return e.startsWith("/")?e:`/${e.replace(/^\.\//,"")}`}}function ur(e,t,n){if(!t)return n;let r=S.dirname(S.resolve(t)),s=S.resolve(e,n.replace(/^\/+/,"")),i=S.relative(r,s).replace(/\\/g,"/");return i?(i.startsWith(".")||(i=`./${i}`),i):"."}var Ys=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],Xs=new Set([".html",".htm"]),lr="WPSuite.io Static Publisher",ei=lr.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function ti(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function ni(e){return e.startsWith("//")||e.startsWith("\\/\\/")||e.startsWith("\\\\/\\\\/")}function ri(e,t,n){return ni(t)?e.replace(new RegExp(`(?<!:)${ti(t)}`,"g"),n):e.split(t).join(n)}function si(e,t,n){if(e.wpsuite?.siteSettings?.subscriber===true||e.wpsuite?.subscriptionType==="PROFESSIONAL"||e.wpsuite?.subscriptionType==="AGENCY"||!n)return false;let s=S.extname(n).toLowerCase();return Xs.has(s)?/<head\b|<html\b|<!doctype html/i.test(t):false}function ii(e){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${ei}\\2)[^>]*\\/?>`,"i").test(e))return e;let t=`<meta name="generator" content="${lr}" />`;if(e.match(/(\r?\n)([ \t]*)<\/head>/i))return e.replace(/(\r?\n)([ \t]*)<\/head>/i,`$1$2${t}$1$2</head>`);let r=e.includes(`\r
1
+ import A from'fs/promises';import Fs from'http';import Jn from'https';import b from'path';import mr,{createHash}from'crypto';import {request,chromium}from'playwright';import {gunzipSync}from'zlib';import ri from'fast-glob';import {availableParallelism}from'os';import {Worker}from'worker_threads';import {S3Client,ListObjectsV2Command,HeadObjectCommand,GetObjectCommand,DeleteObjectsCommand}from'@aws-sdk/client-s3';import {Upload}from'@aws-sdk/lib-storage';import Sa from'mime-types';import {CloudFrontClient,waitUntilInvalidationCompleted,CreateInvalidationCommand}from'@aws-sdk/client-cloudfront';var As=new Set(["post-type","taxonomy","author","date","posts-page","listing"]);function Se(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function Ts(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))]}function ks(e){return new URL(e).pathname.split("/").filter(Boolean).some(n=>{try{return decodeURIComponent(n).endsWith("__trashed")}catch{return n.endsWith("__trashed")}})}function Qt(e){return Array.isArray(e)?e.map(Qt):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>[t,Qt(n)])):e}function $s(e){return JSON.stringify(Qt(e))}function pe(e){return createHash("sha256").update($s(e)).digest("hex")}function Nn(e){return pe({schemaVersion:1,wordpressFingerprint:e.wordpressFingerprint,targetFingerprint:e.targetFingerprint,sourceOrigin:e.sourceOrigin,sitemapPaths:[...e.sitemapPaths].sort(),blockedPathPrefixes:[...e.blockedPathPrefixes].sort(),assetPathPrefixes:[...e.assetPathPrefixes].sort(),noJavaScriptRenderPathPrefixes:[...e.noJavaScriptRenderPathPrefixes].sort(),urlRewriteMode:e.urlRewriteMode})}function Jt(e){let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")throw new Error(`Unsupported source origin protocol: ${t.protocol}`);return t.hash="",t.search="",t.pathname=t.pathname.replace(/\/+$/,""),t.toString().replace(/\/$/,"")}function se(e,t){Es(t);let n=new URL(Jt(e)),r=new URL(t,`${n.toString().replace(/\/$/,"")}/`);if(r.origin!==n.origin)throw new Error(`Content-sync URL is outside the source origin: ${t}`);let s=n.pathname.replace(/\/+$/,"");if(s&&r.pathname!==s&&!r.pathname.startsWith(`${s}/`))throw new Error(`Content-sync URL is outside the source site path: ${t}`);return r.hash="",r.toString()}function Es(e){let n=String(e).split(/[?#]/,1)[0];for(let s=0;s<3;s+=1)try{let i=decodeURIComponent(n);if(i===n)break;n=i;}catch{throw new Error(`Content-sync URL has malformed encoding: ${e}`)}if(n.includes("\0")||n.includes("\\"))throw new Error(`Content-sync URL has an unsafe path: ${e}`);if(n.replace(/^[a-z][a-z0-9+.-]*:\/\/[^/]*/i,"").split("/").some(s=>s==="."||s===".."))throw new Error(`Content-sync URL has an unsafe path: ${e}`)}function Ke(e){let t=Se((e.postTypes??[]).map(r=>String(r).toLowerCase().replace(/[^a-z0-9_-]/g,"")));if(t.length===0)throw new Error("Content-sync requires at least one public post type.");let n=Se((e.listingPaths??[]).map(r=>{let s=String(r).trim();if(!s)return "";let i=new URL(s,"https://content-sync.invalid/");if(i.origin!=="https://content-sync.invalid"&&/^https?:\/\//i.test(s))throw new Error("Content-sync listing routes must be same-site paths.");let a=`/${i.pathname.replace(/^\/+|\/+$/g,"")}`;return a==="/"?"/":`${a}/`}));return {postTypes:t,listingPaths:n,includeSubsites:e.includeSubsites===true,includePostTypeArchives:e.includePostTypeArchives!==false,includeTaxonomyArchives:e.includeTaxonomyArchives!==false,includeAuthorArchives:e.includeAuthorArchives===true,includeDateArchives:e.includeDateArchives===true,includePostsPage:e.includePostsPage!==false,includeSitemapChain:e.includeSitemapChain!==false}}function Bn(e,t,n){return pe({schemaVersion:1,sourceOrigin:Jt(e),ruleId:t.trim(),scope:Ke(n)})}function Wn(e){return pe({schemaVersion:1,...e})}function Hn(e){let t=String(e.siteId??"").trim()||String(e.accountId??"").trim()||Jt(e.sourceOrigin);return `content-sync:${pe({siteIdentity:t,ruleId:e.ruleId.trim(),targetFingerprint:e.targetFingerprint}).slice(0,48)}`}function zn(e){let t=[...e].sort((s,i)=>s.sequence-i.sequence),n=new Map,r=-1;for(let s of t){if(!Number.isSafeInteger(s.sequence)||s.sequence<=r)throw new Error("Content-sync journal events are not strictly ordered.");if(r=s.sequence,!Number.isSafeInteger(s.blogId)||s.blogId<1)throw new Error("Content-sync journal event has an invalid multisite identity.");let i=`${s.blogId}:${s.postType}:${s.postId}`,a=n.get(i);if(!a){n.set(i,{postId:s.postId,blogId:s.blogId,postType:s.postType,fromSequence:s.sequence,toSequence:s.sequence,before:s.before,after:s.after,operations:[s.operation],correlationIds:s.correlationId?[s.correlationId]:[]});continue}a.toSequence=s.sequence,a.after=s.after,a.operations.push(s.operation),s.correlationId&&!a.correlationIds.includes(s.correlationId)&&a.correlationIds.push(s.correlationId);}return [...n.values()].sort((s,i)=>s.fromSequence-i.fromSequence)}function _n(e){return e?.status==="publish"&&!!e.url}function Ms(e,t){return e==="post-type"?t.includePostTypeArchives:e==="taxonomy"?t.includeTaxonomyArchives:e==="author"?t.includeAuthorArchives:e==="date"?t.includeDateArchives:e==="posts-page"?t.includePostsPage:e==="listing"}function Vt(e){let t=Ke(e.scope),n=new Map;zn(e.events);for(let r of e.events)for(let s of [r.before,r.after])for(let i of s?.archiveFamilies??[]){if(!As.has(i.kind)||!Ms(i.kind,t))continue;let a={...i,url:se(e.sourceOrigin,i.url)};n.set(`${a.kind}|${a.url}`,a);}for(let r of t.listingPaths){let s=se(e.sourceOrigin,r);n.set(`listing|${s}`,{kind:"listing",url:s});}return [...n.values()].sort((r,s)=>r.url.localeCompare(s.url))}function Qn(e){let t=Ke(e.scope),n=zn(e.events),r=new Set,s=new Set,i=new Map,a=new Map(Vt({events:e.events,scope:t,sourceOrigin:e.sourceOrigin}).map(p=>[`${p.kind}|${p.url}`,p]));for(let p of n){let y=p.before,m=p.after,h=_n(y),w=_n(m);w&&m.url&&r.add(se(e.sourceOrigin,m.url)),h&&y.url&&(!w||y.url!==m.url)&&s.add(se(e.sourceOrigin,y.url));}for(let p of [...e.events].sort((y,m)=>y.sequence-m.sequence)){for(let y of [p.before,p.after])for(let m of y?.resourceDeleteUrls??[])i.set(se(e.sourceOrigin,m),"delete");for(let y of p.after?.resourceRenderUrls??[])i.set(se(e.sourceOrigin,y),"render");}let c=[...i].filter(([,p])=>p==="render").map(([p])=>p),o=[...i].filter(([,p])=>p==="delete").map(([p])=>p),l=new Map(e.resolvedArchiveFamilies.map(p=>[`${p.kind}|${se(e.sourceOrigin,p.url)}`,{...p,url:se(e.sourceOrigin,p.url),pageUrls:Se(p.pageUrls.map(y=>se(e.sourceOrigin,y)))}])),d=[...a.entries()].map(([p,y])=>{let m=l.get(p);if(!m)throw new Error(`WordPress did not resolve archive family ${p}.`);return {...y,...m}}).sort((p,y)=>p.url.localeCompare(y.url)),u=Se(d.flatMap(p=>p.pageUrls)),g=t.includeSitemapChain?Ts(e.sitemapUrls.map(p=>se(e.sourceOrigin,p))):[],f={schemaVersion:1,generatedAt:new Date().toISOString(),jobId:e.jobId,ruleId:e.ruleId,consumerId:e.consumerId,coalesceKey:e.coalesceKey,scopeFingerprint:e.scopeFingerprint,targetFingerprint:e.targetFingerprint,baselineId:e.baselineId,fromSequence:e.fromSequence,toSequence:e.toSequence,eventCount:e.events.length,foldedCount:n.length,directRenderUrls:Se([...r]),archiveFamilyUrls:u,sitemapUrls:g,deleteUrls:Se([...s]),resourceRenderUrls:Se(c),resourceDeleteUrls:Se(o),archiveFamilies:d};return {...f,impactHash:pe(f)}}function Ge(e){let{impactHash:t,...n}=e;if(e.schemaVersion!==1||!t)throw new Error("Invalid content-sync impact plan schema.");if(!Array.isArray(e.resourceRenderUrls)||!Array.isArray(e.resourceDeleteUrls))throw new Error("Invalid content-sync resource impact contract.");if(pe(n)!==t)throw new Error("Content-sync impact plan hash mismatch.");for(let r of [...e.directRenderUrls,...e.deleteUrls,...e.resourceRenderUrls,...e.resourceDeleteUrls])if(ks(r))throw new Error(`Content-sync impact contains a WordPress trash alias instead of the last public URL: ${r}`)}var Ds="wp-json/smartcloud-static-publisher/v1/content-sync/",Os=25*1024*1024,ft=class extends Error{status;code;constructor(t,n,r){super(r),this.name="ContentSyncApiError",this.status=t,this.code=n;}},mt=class{options;constructor(t){if(!t.runtimeToken.trim())throw new Error("Content-sync requires a WordPress runtime token.");this.options={...t,timeoutMs:t.timeoutMs??6e4};}endpoint(t,n="content-sync"){let r=this.options.sourceOrigin.replace(/\/+$/,"")+"/",s=n==="content-sync"?Ds:`wp-json/smartcloud-static-publisher/v1/${n}/`;return new URL(`${s}${t}`,r)}async post(t,n,r="content-sync"){let s=this.endpoint(t,r),i=Buffer.from(JSON.stringify(n),"utf8"),a=s.protocol==="https:"?Jn:Fs,c=s.protocol==="https:"?new Jn.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((o,l)=>{let d=a.request(s,{method:"POST",agent:c,headers:{"content-type":"application/json","content-length":String(i.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},u=>{let g=[],f=0;u.on("data",p=>{if(f+=p.byteLength,f>Os){u.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(p);}),u.on("error",l),u.on("end",()=>{let p=Buffer.concat(g).toString("utf8"),y;try{y=p?JSON.parse(p):{};}catch{l(new ft(u.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let m=u.statusCode??500;if(m<200||m>=300){l(new ft(m,String(y.code??"request-failed"),String(y.message??`Content-sync API request failed (${m}).`)));return}o(y);});});d.on("timeout",()=>d.destroy(new Error(`Content-sync API request timed out: ${t}`))),d.on("error",l),d.end(i);})}async head(t){return this.post("head",t)}async unboundHead(t,n){return this.post("head",{postTypes:t,includeSubsites:n})}async events(t){let n=[],r=t.afterSequence,s=new Set(t.postTypes);for(;r<t.throughSequence;){let i=await this.post("events",{...t,afterSequence:r,limit:250});if(i.fromSequence!==r||i.throughSequence!==t.throughSequence||i.nextSequence<r||i.nextSequence>t.throughSequence)throw new Error("Content-sync API returned an inconsistent event page.");if(!Array.isArray(i.items))throw new Error("Content-sync API returned a malformed event page.");let a=r;for(let c of i.items){if(!Number.isSafeInteger(c.sequence)||c.sequence<=a||c.sequence>t.throughSequence||!Number.isSafeInteger(c.postId)||c.postId<=0||!Number.isSafeInteger(c.blogId)||c.blogId<=0||!s.has(c.postType)||typeof c.operation!="string"||!c.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");a=c.sequence;}if(i.items.length>0&&i.nextSequence!==a)throw new Error("Content-sync API event cursor does not match the final returned event.");if(n.push(...i.items),!i.hasMore)break;if(i.nextSequence===r)throw new Error("Content-sync API event pagination did not advance.");r=i.nextSequence;}if(t.throughSequence>t.afterSequence&&n.at(-1)?.sequence!==t.throughSequence)throw new Error("Content-sync API omitted the claimed journal high-water event.");return n}async resolveImpactFamilies(t,n){let r=await this.post("impact",{families:t,includeSubsites:n});return Array.isArray(r.families)?r.families:[]}async releaseFingerprint(t=false){let n=await this.post("fingerprint",{includeSubsites:t}),r=String(n.fingerprint||"").trim();if(!/^[a-f0-9]{64}$/.test(r))throw new Error("WordPress returned an invalid release fingerprint.");return r}async establishBaseline(t){return this.post("baseline",t)}async acknowledge(t){return this.post("ack",t)}async importCookieObservations(t){return this.post("cookie-observations",t,"privacy")}};function ht(e){let t=b.resolve(e);return {state:b.join(t,"content-sync-state.json"),current:b.join(t,"content-sync-current.json"),impact:b.join(t,"content-sync-impact-plan.json"),checkpoint:b.join(t,"content-sync-checkpoint.json"),baseline:b.join(t,"content-sync-baseline.json"),activeRules:b.join(t,"content-sync-active-rules.json"),releaseCutoff:b.join(t,"content-sync-release-cutoff.json"),candidateManifest:b.join(t,"content-sync-candidate-manifest.json"),trustedManifest:b.join(t,"crawl-manifest.json"),invalidation:b.join(t,"content-sync-invalidation.json"),deployPlan:b.join(t,"deploy-plan.json")}}async function Kt(e){let t=await Ze(e.releaseCutoff);if(t===null)return {schemaVersion:1,updatedAt:"",entries:{}};let n=t;if(n.schemaVersion!==1||typeof n.updatedAt!="string"||!n.entries||typeof n.entries!="object"||Array.isArray(n.entries))throw new Error("Invalid content-sync release-cutoff store.");return n}async function Vn(e,t){await Fe(e.releaseCutoff,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Fe(e,t){await A.mkdir(b.dirname(e),{recursive:true});let n=`${e}.${process.pid}.${Date.now()}.tmp`;await A.writeFile(n,JSON.stringify(t,null,2),"utf8"),await A.rename(n,e);}async function Ze(e){try{return JSON.parse(await A.readFile(e,"utf8"))}catch(t){if(t.code==="ENOENT")return null;throw new Error(`Invalid or unreadable content-sync state file ${e}: ${t instanceof Error?t.message:String(t)}`,{cause:t})}}async function Gt(e){let t=await Ze(e.baseline);if(t===null)return {schemaVersion:1,updatedAt:"",entries:{}};let n=t;if(n.schemaVersion!==1||typeof n.updatedAt!="string"||!n.entries||typeof n.entries!="object"||Array.isArray(n.entries))throw new Error("Invalid content-sync baseline store.");return n}async function Kn(e,t){await Fe(e.baseline,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Zt(e){let t=await Ze(e.current);if(t===null)return null;let n=t;if(n.schemaVersion!==1||typeof n.jobId!="string"||typeof n.phase!="string"||!Number.isSafeInteger(n.fromSequence)||!Number.isSafeInteger(n.toSequence))throw new Error("Invalid content-sync current-range state.");return n}async function Gn(e,t){await Fe(e.current,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Zn(e){let t=await Ze(e.impact);if(t===null)return null;let n=t;return Ge(n),n}async function Yn(e,t){Ge(t),await Fe(e.impact,t);}async function Xn(e,t){await Fe(e.checkpoint,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function wt(e){let t=await Ze(e.state);if(t===null)return {schemaVersion:1,updatedAt:"",rules:{}};let n=t;if(n.schemaVersion!==1||typeof n.updatedAt!="string"||!n.rules||typeof n.rules!="object"||Array.isArray(n.rules))throw new Error("Invalid content-sync state store.");return n}async function yt(e,t){await Fe(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Yt(e){await Promise.all([e.current,e.checkpoint].map(async t=>{try{await A.unlink(t);}catch(n){if(n.code!=="ENOENT")throw n}}));}async function er(e,t){let n=await Zt(e);return !n||n.coalesceKey!==t?false:(await Promise.all([e.current,e.checkpoint,e.impact,e.candidateManifest,e.invalidation,e.deployPlan].map(async r=>{try{await A.unlink(r);}catch(s){if(s.code!=="ENOENT")throw s}})),true)}var Xt=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function Ye(e){let t=new Set;for(let n of [...e,...Xt]){let r=String(n||"").trim();r&&t.add(r);}return [...t]}function en(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function tr(e){if(!e||!/^https?:\/\//i.test(e))return false;try{return en(new URL(e))}catch{return false}}function rr(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function qs(e){let t=String(e??"").trim();if(!t)return "";let n=t;if(/^https?:\/\//i.test(n))try{n=new URL(n).pathname;}catch{return ""}if(n=n.split(/[?#]/,1)[0]?.replace(/\\/g,"/").trim()??"",!n)return "";let r=n.endsWith("/"),s=n.replace(/^\/+/,"").split("/").map(a=>a.replace(/[^A-Za-z0-9._-]/g,"")).filter(a=>a.length>0&&a!=="."&&a!=="..");if(s.length===0)return "";let i=`/${s.join("/")}`;return r&&b.extname(i)===""&&(i+="/"),i==="/"?"":i}function tn(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t.trim(),String(n??"")]).filter(([t])=>t.length>0))}function _s(e){if(!e||typeof e!="object")return {};let t={};for(let[n,r]of Object.entries(e)){let s=n.trim();if(!s||!r||typeof r!="object")continue;let i=r,a={};if(typeof i.targetOrigin=="string"){let o=rr(i.targetOrigin);o&&(a.targetOrigin=o);}let c=tn(i.extraReplacements);if(Object.keys(c).length>0&&(a.extraReplacements=c),i.s3&&typeof i.s3=="object"){let o={},l=i.s3;typeof l.bucket=="string"&&(o.bucket=l.bucket.trim()),typeof l.prefix=="string"&&(o.prefix=l.prefix.trim()),typeof l.region=="string"&&(o.region=l.region.trim()),typeof l.htmlCacheControl=="string"&&(o.htmlCacheControl=l.htmlCacheControl.trim()),typeof l.assetCacheControl=="string"&&(o.assetCacheControl=l.assetCacheControl.trim()),Object.keys(o).length>0&&(a.s3=o);}if(i.cloudFront&&typeof i.cloudFront=="object"){let o={},l=i.cloudFront;typeof l.distributionId=="string"&&(o.distributionId=l.distributionId.trim()),Array.isArray(l.invalidationPaths)&&(o.invalidationPaths=l.invalidationPaths.map(d=>String(d??"").trim()).filter(d=>d.length>0)),Object.keys(o).length>0&&(a.cloudFront=o);}t[s]=a;}return t}function rn(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function Ns(e,t){if(!e||typeof e!="object")return null;let n=e,r=n.command;if(r!=="publish"&&r!=="crawl"&&r!=="deploy"&&r!=="invalidate"&&r!=="retry-timeouts"&&r!=="url"&&r!=="content-sync")return null;let s=Number.parseInt(String(n.intervalMinutes??"0"),10);if(!Number.isFinite(s)||s<1)return null;let i=String(n.id??`${r}-${t+1}`).trim();if(!i)return null;let a=String(n.deploymentProfile??"").trim(),c=String(n.url??"").trim(),o=Array.isArray(n.postTypes)?[...new Set(n.postTypes.map(d=>String(d??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],l=Array.isArray(n.listingPaths)?[...new Set(n.listingPaths.map(d=>String(d??"").trim()).filter(Boolean))]:[];return r==="content-sync"&&o.length===0?null:{id:i,enabled:n.enabled!==false,command:r,intervalMinutes:s,...r==="publish"||r==="crawl"?{crawlMode:n.crawlMode==="incremental"?"incremental":"full"}:{},...(r==="publish"||r==="deploy"||r==="invalidate"||r==="content-sync")&&a?{deploymentProfile:a}:{},...c?{url:c}:{},...o.length>0?{postTypes:o}:{},...l.length>0?{listingPaths:l}:{},...typeof n.includeSubsites=="boolean"?{includeSubsites:n.includeSubsites}:{},...typeof n.includePostTypeArchives=="boolean"?{includePostTypeArchives:n.includePostTypeArchives}:{},...typeof n.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:n.includeTaxonomyArchives}:{},...typeof n.includeAuthorArchives=="boolean"?{includeAuthorArchives:n.includeAuthorArchives}:{},...typeof n.includeDateArchives=="boolean"?{includeDateArchives:n.includeDateArchives}:{},...typeof n.includePostsPage=="boolean"?{includePostsPage:n.includePostsPage}:{},...typeof n.includeSitemapChain=="boolean"?{includeSitemapChain:n.includeSitemapChain}:{}}}function sr(e){let t=e&&typeof e=="object"?e:{},n=Array.isArray(t.rules)?t.rules.map((r,s)=>Ns(r,s)).filter(r=>!!r):[];return {enabled:t.enabled===true,timezone:typeof t.timezone=="string"&&t.timezone.trim()!==""?t.timezone.trim():"UTC",rules:n}}function Bs(e){if(!e||typeof e!="object")return;let t=e,n={};for(let s of ["accountId","siteId"]){let i=t[s];typeof i=="string"&&i.trim()!==""&&(n[s]=i.trim());}let r=Number(t.lastUpdate??0);return Number.isFinite(r)&&r>0&&(n.lastUpdate=Math.floor(r)),t.subscriber===true&&(n.subscriber=true),Object.keys(n).length>0?n:void 0}function sn(e){let t=e&&typeof e=="object"?e:{},n=String(t.accountId??"").trim(),r=String(t.siteId??"").trim(),s=t.subscriber===true,i=Bs(t.siteSettings),a={...i??{},...n?{accountId:i?.accountId??n}:{},...r?{siteId:i?.siteId??r}:{},...i?.subscriber===true||s?{subscriber:true}:{}},c={},o=String(t.apiBase??"").trim();o&&(c.apiBase=o);let l=String(t.runtimeToken??t.nonce??"").trim();l&&(c.runtimeToken=l);let d=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();d&&(c.virtualAssetBaseUrl=d),Object.keys(a).length>0&&(c.siteSettings=a);let u=rn(t.subscriptionType);return u&&(c.subscriptionType=u),c}function Ws(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function ir(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function Hs(e,t){let n=new Headers(e instanceof Request?e.headers:void 0);if(t?.headers)for(let[r,s]of new Headers(t.headers).entries())n.set(r,s);return n}async function or(e,t,n=5){let r=new URL(ir(e)),s=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),i=r.protocol==="http:"?Fs:Jn,a=Hs(e,t);return await new Promise((c,o)=>{let l=i.request(r,{method:s,headers:Object.fromEntries(a.entries()),...r.protocol==="https:"?{rejectUnauthorized:false}:{}},d=>{let u=[];d.on("data",g=>{u.push(Buffer.isBuffer(g)?g:Buffer.from(g));}),d.on("error",o),d.on("end",()=>{let g=d.statusCode??0,f=d.headers.location;if(n>0&&f&&[301,302,303,307,308].includes(g)){c(or(new URL(f,r),{method:g===303?"GET":s},n-1));return}let p=new Headers;for(let[y,m]of Object.entries(d.headers))Array.isArray(m)?p.set(y,m.join(", ")):typeof m=="string"&&p.set(y,m);c(new Response(Buffer.concat(u),{status:g,statusText:d.statusMessage??"",headers:p}));});});l.on("error",o),l.end();})}function zs(e){let t=sn(e.wpsuite),n={...t.siteSettings??{},...t.siteSettings?.subscriber===true||t.subscriptionType==="PROFESSIONAL"||t.subscriptionType==="AGENCY"?{subscriber:true}:{}},r=String(n.accountId??"").trim(),s=String(n.siteId??"").trim(),i=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();if(e.wpsuite={...t.apiBase?{apiBase:t.apiBase}:{},...t.runtimeToken?{runtimeToken:t.runtimeToken}:{},...i?{virtualAssetBaseUrl:i}:{},...Object.keys(n).length>0?{siteSettings:n}:{},...t.subscriptionType?{subscriptionType:t.subscriptionType}:{}},!r||!s||!i)return false;Ws(e.sourceOrigin);let a=globalThis,o={...a.WpSuite??{},siteSettings:n,uploadUrl:i};return t.apiBase&&(o.apiBase=t.apiBase),a.WpSuite=o,true}async function Qs(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!zs(e)){let s=e.wpsuite?.siteSettings,i=[String(s?.accountId??"").trim()?"":"accountId",String(s?.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): ${i.join(", ")}.`,requests:[]}}}let n=globalThis.fetch,r=[];try{typeof n=="function"&&/^https?:\/\//i.test(t)&&(globalThis.fetch=(async(o,l)=>{let d=ir(o);if(d.startsWith(t)){let u=(()=>{try{return new URL(d).pathname}catch{return d}})();try{let g=e.ignoreHttpsErrors&&d.startsWith("https://")?await or(o,l):await n(o,l);return r.push({path:u,status:g.status,ok:g.ok,error:null}),g}catch(g){throw r.push({path:u,status:null,ok:!1,error:g instanceof Error?g.message:String(g)}),g}}return n(o,l)}));let{getConfig:s}=await import('@smart-cloud/wpsuite-core'),i=await s("publisher"),a=i&&typeof i=="object"?i:null,c=rn(a?.subscriptionType);return {remote:a,diagnostic:{status:a?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:c??null,error:a?null:"WP Suite publisher config loader returned no configuration.",requests:r}}}catch(s){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:t,subscriptionType:null,error:s instanceof Error?s.message:String(s),requests:r}}}finally{typeof n=="function"&&(globalThis.fetch=n);}}function Js(e,t){let n=_s(t?.deploymentProfiles),r=String(t?.defaultDeploymentProfile??"").trim(),s=n[r]?r:"",i=rn(t?.subscriptionType),a=sn(e.wpsuite);return {...e,scheduler:sr(t?.scheduler),deploymentProfiles:n,defaultDeploymentProfile:s,...i?{subscriptionType:i}:{},wpsuite:{...a,...a.siteSettings||i?{siteSettings:{...a.siteSettings??{},...a.siteSettings?.subscriber===true||i==="PROFESSIONAL"||i==="AGENCY"?{subscriber:true}:{}}}:{},...i?{subscriptionType:i}:{}}}}function nn(e,t){let r=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!r)return t;let s=r.split("/").map(i=>i.trim()).filter(i=>i.length>0&&i!=="."&&i!=="..");return s.length>0?s.join("/"):t}function Vs(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?b.resolve(e):""}function nr(e,t,n){let r=String(t||"").trim();return r&&b.isAbsolute(r)?b.resolve(r):b.resolve(e,nn(r,n))}async function ie(e,t={}){let n=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",r=await A.readFile(n,"utf8"),s=JSON.parse(r);s.sourceOrigin=s.sourceOrigin.replace(/\/$/,""),s.targetOrigin=rr(s.targetOrigin),s.ignoreHttpsErrors??=false,s.outputDir=String(s.outputDir||"export").trim()||"export",s.urlRewriteMode||=s.targetOrigin==="."?"relative":"absolute",s.noJavaScriptRenderPathPrefixes||=[],s.seedPaths||=[],s.generated404RequestPath=qs(s.generated404RequestPath),s.sitemapPaths||=[...Xt],s.allowedAssetHosts||=[],s.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],s.blockedPathPrefixes||=[],s.blockedSearchFragments||=[],s.concurrency||=1,s.maxPages||=0,s.extraReplacements=tn(s.extraReplacements),s.postCrawlCopyMap=tn(s.postCrawlCopyMap),s.logDir=String(s.logDir||"logs").trim()||"logs",s.verbose??=false,s.logLevel||=s.verbose?"debug":"info",s.s3SyncMode||="sdk-upload-delete",s.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},s.readiness.timeoutMs??=1500,s.readiness.fallbackWaitMs??=1500,s.viewport||={width:1440,height:1200},s.navigationTimeoutMs||=3e4,s.assetDownloadConcurrency=Number(s.assetDownloadConcurrency)>0?Number(s.assetDownloadConcurrency):s.concurrency,s.rewriteConcurrency=Number(s.rewriteConcurrency)>0?Number(s.rewriteConcurrency):s.assetDownloadConcurrency,s.wpsuite=sn(s.wpsuite),s.scheduler=sr(void 0),s.deploymentProfiles={},s.defaultDeploymentProfile="",s.deploymentTargetOverride=String(s.deploymentTargetOverride??"").trim();let i=await Qs(s);t.onRemotePublisherConfigDiagnostic?.(i.diagnostic);let a=Js(s,i.remote),c=Vs(),o=c?b.resolve(c,".."):"";return o?(a.outputDir=nr(o,a.outputDir,"export"),a.logDir=nr(o,a.logDir,"logs")):(b.isAbsolute(a.outputDir)||(a.outputDir=nn(a.outputDir,"export")),b.isAbsolute(a.logDir)||(a.logDir=nn(a.logDir,"logs"))),a}var Ks=".deploy-plan.json",Gs="deploy-plan.json",Zs=".crawl-incomplete.json",Ys="crawl-incomplete.json";function ar(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?b.resolve(e):""}function cr(e){let t=String(e||"").replace(/\\/g,"/").replace(/^\/+/,"").trim(),n=b.posix.normalize(t);return !t||!n||n==="."||/^[a-zA-Z]:/.test(n)||n.startsWith("../")||n===".."?null:n}function Xs(e){let t=String(e||"").trim();if(!t.startsWith("/")||t.startsWith("//")||t.includes("*")||t.includes("#"))return null;try{let n=new URL(t,"https://content-sync.invalid");return n.origin!=="https://content-sync.invalid"?null:`${b.posix.normalize(n.pathname)}${n.search}`}catch{return null}}function oe(e){return [...new Set(e.map(cr).filter(t=>t!==null))]}function ur(e,t){let n={generatedAt:String(t.generatedAt||new Date().toISOString()),outputDir:b.resolve(e.outputDir),runMode:t.runMode,crawlMode:t.crawlMode,fullSyncRequired:!!t.fullSyncRequired,changedFiles:oe(t.changedFiles||[]),deletedFiles:oe(t.deletedFiles||[]),rewriteTargets:oe(t.rewriteTargets||[]),revalidateFiles:oe(t.revalidateFiles||[])};return t.schemaVersion===2?{...n,schemaVersion:2,runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,consumerId:String(t.consumerId||"").trim(),fromSequence:Math.max(0,Number(t.fromSequence)||0),toSequence:Math.max(0,Number(t.toSequence)||0),impactHash:String(t.impactHash||"").trim(),resolvedSitemapUrls:[...new Set((t.resolvedSitemapUrls||[]).map(r=>String(r||"").trim()).filter(r=>{try{let s=new URL(r);return s.protocol==="http:"||s.protocol==="https:"}catch{return false}}))],uploadGroups:{assets:oe(t.uploadGroups?.assets||[]),content:oe(t.uploadGroups?.content||[]),listings:oe(t.uploadGroups?.listings||[]),resources:oe(t.uploadGroups?.resources||[]),sitemapLeaves:oe(t.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:oe(t.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((t.invalidationPaths||[]).map(Xs).filter(r=>r!==null))].sort()}:{schemaVersion:1,...n,runMode:t.runMode}}function ei(e){if(!e||typeof e!="object")return false;let t=e;if(t.schemaVersion===2){let n=t;return n.runMode==="content-sync"&&n.crawlMode==="incremental"&&n.fullSyncRequired===false&&typeof n.generatedAt=="string"&&typeof n.outputDir=="string"&&typeof n.consumerId=="string"&&n.consumerId.trim()!==""&&Number.isSafeInteger(n.fromSequence)&&Number.isSafeInteger(n.toSequence)&&Number(n.toSequence)>=Number(n.fromSequence)&&typeof n.impactHash=="string"&&/^[a-f0-9]{64}$/.test(n.impactHash)&&Array.isArray(n.resolvedSitemapUrls)&&Array.isArray(n.changedFiles)&&Array.isArray(n.deletedFiles)&&Array.isArray(n.rewriteTargets)&&(n.revalidateFiles===void 0||Array.isArray(n.revalidateFiles))&&!!n.uploadGroups&&Array.isArray(n.uploadGroups.assets)&&Array.isArray(n.uploadGroups.content)&&Array.isArray(n.uploadGroups.listings)&&Array.isArray(n.uploadGroups.resources)&&Array.isArray(n.uploadGroups.sitemapLeaves)&&Array.isArray(n.uploadGroups.sitemapIndexes)&&Array.isArray(n.invalidationPaths)}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)&&(t.revalidateFiles===void 0||Array.isArray(t.revalidateFiles))}function on(e){let t=b.join(b.resolve(e.outputDir),Ks),n=ar();return n?[b.join(n,Gs),t]:[t]}function an(e){let t=b.join(b.resolve(e.outputDir),Zs),n=ar();return n?[b.join(n,Ys),t]:[t]}function ce(e,t){let n=b.resolve(e),r=b.resolve(t),s=b.relative(n,r).replace(/\\/g,"/");return cr(s)}async function De(e){let t=b.resolve(e.outputDir);for(let n of on(e))try{let r=await A.readFile(n,"utf8"),s=JSON.parse(r);if(!ei(s)||b.resolve(s.outputDir)!==t)continue;return ur(e,s)}catch{}return null}async function cn(e,t){let n=ur(e,t);for(let r of on(e)){await A.mkdir(b.dirname(r),{recursive:true});let s=`${r}.${process.pid}.${Date.now()}.tmp`;await A.writeFile(s,JSON.stringify(n,null,2),"utf8"),await A.rename(s,r);}}async function Oe(e){for(let t of on(e))try{await A.unlink(t);}catch(n){if(n.code!=="ENOENT")throw n}}async function lr(e,t){let n={schemaVersion:1,startedAt:String(t.startedAt||new Date().toISOString()),outputDir:b.resolve(e.outputDir)};for(let r of an(e)){await A.mkdir(b.dirname(r),{recursive:true});let s=`${r}.${process.pid}.tmp`;await A.writeFile(s,JSON.stringify(n,null,2),"utf8"),await A.rename(s,r);}}async function dr(e){let t=b.resolve(e.outputDir);for(let n of an(e)){let r;try{r=await A.readFile(n,"utf8");}catch(i){if(i.code==="ENOENT")continue;throw i}let s;try{s=JSON.parse(r);}catch{throw new Error(`Invalid incomplete crawl marker: ${n}`)}if(s.schemaVersion!==1||typeof s.startedAt!="string"||typeof s.outputDir!="string"||b.resolve(s.outputDir)!==t)throw new Error(`Invalid incomplete crawl marker: ${n}`);return {schemaVersion:1,startedAt:s.startedAt,outputDir:t}}return null}async function pr(e){for(let t of an(e))try{await A.unlink(t);}catch(n){if(n.code!=="ENOENT")throw n}}function et(e,t=10){return mr.createHash("sha1").update(e).digest("hex").slice(0,t)}async function ti(e){await A.mkdir(b.dirname(e),{recursive:true});}async function tt(e,t,n){await ti(e);let r=b.join(b.dirname(e),`.${b.basename(e)}.${process.pid}.${Date.now()}.${mr.randomBytes(6).toString("hex")}.tmp`);try{typeof t=="string"?await A.writeFile(r,t,n??"utf8"):await A.writeFile(r,t),await A.rename(r,e);}catch(s){throw await A.unlink(r).catch(()=>{}),s}}function Pe(e){return [".html",".htm",".css",".js",".mjs",".json",".xml",".xsl",".txt",".svg",".map",".enc",".jws"].includes(b.extname(e).toLowerCase())}function fe(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 Ue(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function St(e){return e.replace(/\//g,"\\/")}function gr(e){return e.replace(/\//g,"\\\\/")}function ni(e){try{let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")return null;let n=t.pathname==="/"&&!t.search&&!t.hash?"":`${t.pathname}${t.search}${t.hash}`;return `//${t.host}${n}`}catch{return null}}function fr(e,t,n){if(!t)return;e[t]=n;let r=St(t),s=St(n);e[r]=s;let i=gr(t),a=gr(n);e[i]=a;let c=Ue(t),o=Ue(n);e[c]=o;let l=Ue(r),d=Ue(s);e[l]=d;let u=Ue(i),g=Ue(a);e[u]=g;}function bt(e,t,n){fr(e,t,n);let r=ni(t);r&&r!==t&&fr(e,r,n);}function un(e,t){let n=t.startsWith("/")?t:`/${t}`,r=(e||"").replace(/\/$/,"");return !r||r==="."||r==="/"?n:`${r}${n}`}function hr(e,t){try{return new URL(e,t==="."?"https://relative.invalid":t).pathname}catch{return e.startsWith("/")?e:`/${e.replace(/^\.\//,"")}`}}function wr(e,t,n){if(!t)return n;let r=b.dirname(b.resolve(t)),s=b.resolve(e,n.replace(/^\/+/,"")),i=b.relative(r,s).replace(/\\/g,"/");return i?(i.startsWith(".")||(i=`./${i}`),i):"."}var ai=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],ci=new Set([".html",".htm"]),yr="WPSuite.io Static Publisher",ui=yr.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function li(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function di(e){return e.startsWith("//")||e.startsWith("\\/\\/")||e.startsWith("\\\\/\\\\/")}function pi(e,t,n){return di(t)?e.replace(new RegExp(`(?<!:)${li(t)}`,"g"),n):e.split(t).join(n)}function gi(e,t,n){if(e.wpsuite?.siteSettings?.subscriber===true||e.wpsuite?.subscriptionType==="PROFESSIONAL"||e.wpsuite?.subscriptionType==="AGENCY"||!n)return false;let s=b.extname(n).toLowerCase();return ci.has(s)?/<head\b|<html\b|<!doctype html/i.test(t):false}function fi(e){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${ui}\\2)[^>]*\\/?>`,"i").test(e))return e;let t=`<meta name="generator" content="${yr}" />`;if(e.match(/(\r?\n)([ \t]*)<\/head>/i))return e.replace(/(\r?\n)([ \t]*)<\/head>/i,`$1$2${t}$1$2</head>`);let r=e.includes(`\r
2
2
  `)?`\r
3
3
  `:`
4
- `;return e.replace(/<head\b[^>]*>/i,s=>`${s}${r} ${t}`)}function oi(e){let t=fe(e).trim();return t.startsWith("{")||t.startsWith("[")||t.includes("\\/")||/"@context"|"@type"/.test(t)}function ai(e,t){let n=[...new Set(t)].filter(i=>i.includes("/")&&!/\\+\//.test(i)).map(i=>[i,ht(i)]).filter(([i,a])=>i!==a).sort((i,a)=>a[0].length-i[0].length);if(!n.length)return e;let r=i=>{let a=i;for(let[c,o]of n)a=a.split(c).join(o);return a},s=e.replace(/(<script\b[^>]*\btype=["']application\/(?:ld\+)?json["'][^>]*>)([\s\S]*?)(<\/script>)/gi,(i,a,c,o)=>`${a}${r(c)}${o}`);return s=s.replace(/(<meta\b[^>]*\bcontent=(['"]))([\s\S]*?)(\2[^>]*>)/gi,(i,a,c,o,u)=>oi(o)?`${a}${r(o)}${u}`:i),s}function ci(e,t){if(!t)return null;let n=S.dirname(S.resolve(t)),r=S.resolve(e.outputDir),s=S.relative(n,r).replace(/\\/g,"/");return s?(s.startsWith(".")||(s=`./${s}`),s.endsWith("/")?s:`${s}/`):"./"}function ui(e,t,n){let r=ci(t,n);if(!r)return e;let s=e;for(let i of Ys){let a=i.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),c=new RegExp(`(?:\\.{1,}/)+${a}`,"g"),o=new RegExp(`(?<!\\.)/${a}`,"g");s=s.replace(c,`${r}${i}`).replace(o,`${r}${i}`);let u=i.replace(/\//g,"\\/"),d=r.replace(/\//g,"\\/"),l=u.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),g=new RegExp(`(?:\\.{1,}\\\\/)+${l}`,"g"),f=new RegExp(`(?:\\.{1,}(?:\\\\/|\\\\))+${l}`,"g"),p=new RegExp(`(?<![\\.\\\\])\\/${l}`,"g");s=s.replace(g,`${d}${u}`).replace(f,`${d}${u}`).replace(p,`${d}${u}`);}return s}function sn(e,t,n){if(e.urlRewriteMode==="absolute")return t;let r=(()=>{try{return new URL(t,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin)}catch{return null}})(),s=r?r.pathname:cr(t,e.targetOrigin),i=r?`${r.search}${r.hash}`:"";return e.urlRewriteMode==="root-relative"?`${s}${i}`:`${ur(e.outputDir,n,s)}${i}`}function rn(e,t,n,r,s){wt(e,n,sn(t,r,s));}function li(e,t,n,r,s){if(!n||n===r)return;wt(e,n,r);let i=sn(t,n,s),a=sn(t,r,s);wt(e,i,a);}function on(e,t,n,r,s={}){let i=e,a={};rn(a,t,t.sourceOrigin,t.targetOrigin,r);for(let[o,u]of Object.entries(t.extraReplacements))rn(a,t,o,u,r);for(let[o,u]of Object.entries(n))rn(a,t,o,u,r);for(let[o,u]of Object.entries(s)){let d=n[o];!d||d===u||li(a,t,u,d,r);}let c=Object.entries(a).sort((o,u)=>u[0].length-o[0].length);for(let[o,u]of c)i=ri(i,o,u);return t.urlRewriteMode!=="absolute"&&(i=ui(i,t,r)),i=ai(i,c.map(([,o])=>o)),si(t,i,r)&&(i=ii(i)),i}async function di(e,t,n,r){let s=S.join(e.outputDir,r),i=S.extname(s).toLowerCase(),a;try{a=await A.readFile(s,"utf8");}catch(u){if(u.code!=="ENOENT")throw u;return {changed:false}}let o=on(a,e,t,i===".js"||i===".mjs"?void 0:s,n);return o!==a?(await Xe(s,o,"utf8"),{changed:true}):{changed:false}}async function pi(e,t){return await new Promise((n,r)=>{let s=o=>{if(c(),o?.error){r(new Error(o.error));return}n({changed:!!o?.changed});},i=o=>{c(),r(o);},a=o=>{c(),o!==0&&r(new Error(`Rewrite worker exited with code ${o}.`));},c=()=>{e.off("message",s),e.off("error",i),e.off("exit",a);};e.on("message",s),e.on("error",i),e.on("exit",a),e.postMessage({file:t});})}async function yt(e,t,n={}){let s=(n.files?[...new Set(n.files)].map(g=>S.resolve(g)).filter(g=>g.startsWith(S.resolve(e.outputDir))).map(g=>S.relative(e.outputDir,g).replace(/\\/g,"/")).filter(g=>g.length>0):await Vs(["**/*"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(g=>g!=="asset-map.json"&&Pe(g)),i=0;if(s.length===0)return 0;let a=Math.max(1,Number(e.rewriteConcurrency||e.assetDownloadConcurrency||1)),c=Math.max(1,Math.min(a,s.length,availableParallelism()));await n.onStart?.({totalFiles:s.length,workerCount:c});let o=0,u=0,d=null;if(c===1){for(let g of s){let f=await di(e,t,n.previousAssetMap??{},g);f.changed&&(i+=1),u+=1,await n.onProgress?.({index:u,totalFiles:s.length,changedTextFiles:i,file:g,changed:f.changed});}return i}let l=Array.from({length:c},async()=>{let g=new Worker(new URL("./rewrite-worker.js",import.meta.url),{workerData:{config:e,assetMap:t,previousAssetMap:n.previousAssetMap??{}}});try{for(;!d;){let f=o;if(f>=s.length)return;o+=1;let p=s[f],y=await pi(g,p);y.changed&&(i+=1),u+=1,await n.onProgress?.({index:u,totalFiles:s.length,changedTextFiles:i,file:p,changed:y.changed});}}catch(f){throw d=f instanceof Error?f:new Error(String(f)),d}finally{await g.terminate().catch(()=>{});}});return await Promise.all(l),i}var ue={error:0,warn:1,info:2,debug:3},gi=/^(?:authorization|proxyAuthorization|runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken|xStaticPublisherToken|xApiKey)$/i;function xe(e){return e.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 bt(e){return typeof e=="string"?xe(e):Array.isArray(e)?e.map(t=>bt(t)):e instanceof Error?{name:e.name,message:xe(e.message),...e.stack?{stack:xe(e.stack)}:{}}:e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([t,n])=>[t,gi.test(t)?"[REDACTED]":bt(n)])):e}function vt(e){return e?bt(e):void 0}var dr=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",St=dr?S.join(dr,"current-progress.json"):"",pr=Promise.resolve();function gr(e){return e.includes("crawl")?"crawl":e.includes("deploy")?"deploy":e.includes("invalidate")?"invalidate":e}function fi(e,t){let n={...e},r=new Set(["pagesQueued","pagesRendered","assetsQueued","sitemapsQueued","pagesDiscovered","assetsDiscovered","sitemapsDiscovered","pagesSaved","assetsSaved","donePages","doneAssets","doneSitemaps","changedTextFiles","uploaded","failed","index","totalFiles","pageQueue","assetQueue","sitemapQueue"]);for(let[s,i]of Object.entries(t)){let a=n[s];if(typeof a=="number"&&typeof i=="number"&&r.has(s)){n[s]=Math.max(a,i);continue}n[s]=i;}return n}function fr(e,t,n){St&&(pr=pr.then(async()=>{let r=new Date,s=r.toISOString();await A.mkdir(S.dirname(St),{recursive:true});let i=await A.readFile(St,"utf8").then(h=>JSON.parse(h)).catch(()=>null),a=i&&typeof i.details=="object"&&i.details?i.details:{},c=gr(e),o=i?.currentStep||gr(i?.source||""),u=i?.startedAt||s,d={...i?.stepDurationsSec??{}},l=i?.stepStartedAt||s;if(o&&o!==c&&i?.stepStartedAt){let h=Math.max(0,Math.round((r.getTime()-new Date(i.stepStartedAt).getTime())/1e3));d[o]=(d[o]??0)+h,l=s;}let g=Math.max(0,Math.round((r.getTime()-new Date(l).getTime())/1e3)),f=Math.max(0,Math.round((r.getTime()-new Date(u).getTime())/1e3)),p=xe(t),y=vt(n)??{},m={checkedAt:s,source:e,message:p,details:fi(a,y),startedAt:u,currentStep:c,stepStartedAt:l,stepElapsedSec:g,totalElapsedSec:f,stepDurationsSec:d};await A.writeFile(St,JSON.stringify(m,null,2),"utf8");}).catch(()=>{}));}async function mr(e,t){await A.mkdir(S.dirname(e),{recursive:true}),await A.appendFile(e,`${JSON.stringify(bt(t))}
5
- `,"utf8");}function mi(e,t){let n=typeof t?.phase=="string"?t.phase.trim():"";return n||(e==="page"?"render-pages":e==="asset"?"download-assets":e==="sitemap"?"discovery":e==="timing"&&typeof t?.name=="string"&&t.name.trim()||"crawl")}function hi(e,t,n){let r=vt(n);return {checkedAt:new Date().toISOString(),currentStep:mi(e,r),level:e,message:xe(t),details:r??{}}}var Fe=class{constructor(t,n,r="info"){this.logDir=t;this.logFile=n;this.level=r;this.ensureLogFileReady();}logDir;logFile;level;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;get logPath(){return S.join(this.logDir,this.logFile)}artifactPath(t){return this.logFile.endsWith(".log.jsonl")?S.join(this.logDir,this.logFile.replace(/\.log\.jsonl$/u,`${t}.jsonl`)):S.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 A.mkdir(this.logDir,{recursive:true}),await A.writeFile(this.logPath,"","utf8");})()),this.initPromise}enqueueTask(t){this.writeQueue=this.writeQueue.then(t).catch(n=>{this.writeError=n instanceof Error?n:new Error(String(n));});}enqueueLine(t){this.enqueueTask(async()=>{await this.ensureLogFileReady(),await A.appendFile(this.logPath,`${t}
6
- `,"utf8");});}enqueueJsonLine(t,n){this.enqueueTask(()=>mr(t,n));}accepts(t){return ue[t]<=ue[this.level]}push(t,n,r){if(!this.accepts(t))return;let s=xe(n),i=vt(r),a=JSON.stringify({time:new Date().toISOString(),level:t,message:s,...i||{}});this.enqueueLine(a),t==="error"?console.error(`[ERROR] ${s}`,i?JSON.stringify(i):""):t==="warn"?console.warn(`[WARN] ${s}`,i?JSON.stringify(i):""):console.log(`[${t.toUpperCase().padEnd(5)}] ${s}`);}info(t,n){this.push("info",t,n);}progress(t,n){fr(this.logFile,t,n);}debug(t,n){this.push("debug",t,n);}warn(t,n){this.push("warn",t,n);}reject(t,n,r,s,i){let a={kind:t,url:n,reason:r,...s?{source:s}:{},...i||{}};this.enqueueJsonLine(this.rejectedPath,a),this.accepts("debug")&&this.push("debug",`Rejected ${t} ${n}: ${r}`,a);}error(t,n){this.enqueueJsonLine(this.errorsPath,{message:t,...n||{}}),this.push("error",t,n);}mark(t){this.marks.set(t,Date.now());}endMark(t,n){let r=this.marks.get(t);if(!r)return;let s=Date.now()-r,i=Number((s/1e3).toFixed(2));this.push("info",`Timing ${t}: ${i}s`,{name:t,ms:s,seconds:i,...n||{}});}async flush(){let t=Date.now()-this.startedAt,n=Number((t/1e3).toFixed(2));if(this.push("info",`Total time: ${n}s`,{name:"total",ms:t,seconds:n}),await this.writeQueue,this.writeError)throw this.writeError}},Pt=class{constructor(t,n="info"){this.logDir=t;this.level=typeof n=="boolean"?n?"debug":"info":n,this.ensureLogFileReady();}logDir;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;level;get logPath(){return S.join(this.logDir,"crawl.log.jsonl")}get currentEventPath(){return S.join(this.logDir,"current-crawl-event.json")}get rejectedPath(){return S.join(this.logDir,"rejected.jsonl")}get ignoredPath(){return S.join(this.logDir,"ignored.jsonl")}get skippedPath(){return S.join(this.logDir,"skipped-http.jsonl")}get errorsPath(){return S.join(this.logDir,"errors.jsonl")}get timingsPath(){return S.join(this.logDir,"timings.jsonl")}ensureLogFileReady(){return this.initPromise||(this.initPromise=(async()=>{await A.mkdir(this.logDir,{recursive:true}),await A.writeFile(this.logPath,"","utf8");})()),this.initPromise}enqueueTask(t){this.writeQueue=this.writeQueue.then(t).catch(n=>{this.writeError=n instanceof Error?n:new Error(String(n));});}enqueueLine(t){this.enqueueTask(async()=>{await this.ensureLogFileReady(),await A.appendFile(this.logPath,`${t}
7
- `,"utf8");});}enqueueJsonLine(t,n){this.enqueueTask(()=>mr(t,n));}updateCurrentEvent(t,n,r){let s=hi(t,n,r);this.enqueueTask(async()=>{await A.mkdir(this.logDir,{recursive:true}),await A.writeFile(this.currentEventPath,JSON.stringify(s,null,2),"utf8");});}push(t,n,r){let s=xe(n),i=vt(r),a=JSON.stringify({time:new Date().toISOString(),level:t,message:s,...i||{}});this.enqueueLine(a),this.updateCurrentEvent(t,s,i),t==="error"?console.error(s,i||""):t==="warn"?console.warn(s,i||""):(ue[this.level]>=ue.debug||["summary","page","sitemap","asset","timing"].includes(t))&&console.log(s);}info(t,n){this.push("info",t,n);}debug(t,n){ue[this.level]<ue.debug||this.push("debug",t,n);}progress(t,n){fr("crawl.log.jsonl",t,n),this.updateCurrentEvent("progress",t,n);}checkpoint(t,n){this.updateCurrentEvent("checkpoint",t,n);}page(t,n){this.push("page",t,n);}sitemap(t,n){this.push("sitemap",t,n);}asset(t,n){this.push("asset",t,n);}warn(t,n){this.push("warn",t,n);}error(t,n){this.enqueueJsonLine(this.errorsPath,{message:t,...n||{}}),this.push("error",t,n);}summary(t,n){this.push("summary",t,n);}mark(t){this.marks.set(t,Date.now());}endMark(t,n){let r=this.marks.get(t);if(!r)return;let s=Date.now()-r,i={name:t,ms:s,seconds:Number((s/1e3).toFixed(2)),...n||{}};this.enqueueJsonLine(this.timingsPath,i),this.push("timing",`Timing ${t}: ${i.seconds}s`,i);}reject(t,n,r,s){let i={kind:t,url:n,reason:r,source:s};this.enqueueJsonLine(this.rejectedPath,i),ue[this.level]>=ue.debug&&this.push("reject",`Rejected ${t} ${n}: ${r}`,i);}ignore(t,n,r,s){let i={kind:t,url:n,reason:r,source:s};this.enqueueJsonLine(this.ignoredPath,i),ue[this.level]>=ue.debug&&this.push("ignore",`Ignored ${t} ${n}: ${r}`,i);}skip(t,n,r,s){let i={kind:t,url:n,status:r,source:s};this.enqueueJsonLine(this.skippedPath,i),this.push("warn",`Skipped ${t} ${n}: HTTP ${r}`,i);}async flush(){let t=Date.now()-this.startedAt,n={name:"total",ms:t,seconds:Number((t/1e3).toFixed(2))};if(this.enqueueJsonLine(this.timingsPath,n),await this.writeQueue,this.writeError)throw this.writeError}};var hr="(?:css|js|mjs|json|map|xml|xsl|rss|atom|txt|enc|jws|png|jpe?g|gif|webp|avif|svg|ico|woff2?|ttf|otf|eot|pdf|mp4|webm)";function yr(e){let t=new RegExp(`(?:https?:)?//[^\\s'"<>\\);]+\\.${hr}(?:[?#][^\\s'"<>\\);]*)?(?=[\\s'"<>\\);]|$)|(?<![A-Za-z0-9._-])/[^\\s'"<>\\);]+\\.${hr}(?:[?#][^\\s'"<>\\);]*)?(?=[\\s'"<>\\);]|$)`,"gi");return [...e.matchAll(t)].map(n=>n[0])}function wr(e){return !!e&&/^(?:\.{1,2}\/)?[A-Za-z0-9@_][A-Za-z0-9@._/-]*\.(?:js|mjs)(?:[?#][^\s<>\\]*)?$/i.test(e)&&!e.startsWith("/")&&!e.startsWith("//")&&!/^[a-z][a-z0-9+.-]*:/i.test(e)&&!/(?:^|\/)node_modules(?:\/|$)|(?:^|\/)\.pnpm(?:\/|$)/i.test(e)}function Sr(e){let t=new Set,n=[],r=/\bimport\s*\(\s*["'`]([^"'`\s<>\\]+)["'`]\s*\)/g,s=/\b(?:[A-Za-z_$][\w$]*\.)?u\s*=\s*(?:\([^)]*\)|[A-Za-z_$][\w$]*)\s*=>\s*["'`]([^"'`\s<>\\]+)["'`]/g;for(let i of e.matchAll(r))i[1]&&wr(i[1])&&n.push({index:i.index??0,value:i[1]});for(let i of e.matchAll(s))i[1]&&wr(i[1])&&n.push({index:i.index??0,value:i[1]});for(let i of n.sort((a,c)=>a.index-c.index))t.add(i.value);return [...t]}function br(e,t){let n=S.relative(t,e);return n===""||!n.startsWith("..")&&!S.isAbsolute(n)}function Pr(e,t,n,r){let s=S.resolve(e),i=S.resolve(t),a=S.extname(s).toLowerCase();return !br(s,i)||a!==".html"&&a!==".htm"||n.has(s)?false:!r.some(c=>br(s,S.resolve(c)))}function vr(e,t){return typeof e=="string"&&e.length>0&&t?.supported===true&&typeof t.token=="string"&&t.token.length>0&&e===t.token}function Cr(e,t){try{let n=new URL(e),r=new URL(t,n);if(r.origin!==n.origin)return !1;let s=n.pathname.replace(/\/+$/,"");if(s&&r.pathname!==s&&!r.pathname.startsWith(`${s}/`))return !1;let i=s?r.pathname.slice(s.length)||"/":r.pathname;return ["/wp-includes/","/wp-content/plugins/","/wp-content/themes/","/wp-content/mu-plugins/"].some(a=>i.startsWith(a))}catch{return false}}function Rr(e){return e<1}function xr(e){let t=String(e??"").toLowerCase();return t==="strict"?"Strict":t==="lax"?"Lax":t==="none"?"None":"Unspecified"}function yi(e){let t=e.pathname||"/";if(!t.startsWith("/")||t==="/")return "/";let n=t.lastIndexOf("/");return n<=0?"/":t.slice(0,n)}function et(e){return `${e.name.toLowerCase()}
4
+ `;return e.replace(/<head\b[^>]*>/i,s=>`${s}${r} ${t}`)}function mi(e){let t=fe(e).trim();return t.startsWith("{")||t.startsWith("[")||t.includes("\\/")||/"@context"|"@type"/.test(t)}function hi(e,t){let n=[...new Set(t)].filter(i=>i.includes("/")&&!/\\+\//.test(i)).map(i=>[i,St(i)]).filter(([i,a])=>i!==a).sort((i,a)=>a[0].length-i[0].length);if(!n.length)return e;let r=i=>{let a=i;for(let[c,o]of n)a=a.split(c).join(o);return a},s=e.replace(/(<script\b[^>]*\btype=["']application\/(?:ld\+)?json["'][^>]*>)([\s\S]*?)(<\/script>)/gi,(i,a,c,o)=>`${a}${r(c)}${o}`);return s=s.replace(/(<meta\b[^>]*\bcontent=(['"]))([\s\S]*?)(\2[^>]*>)/gi,(i,a,c,o,l)=>mi(o)?`${a}${r(o)}${l}`:i),s}function wi(e,t){if(!t)return null;let n=b.dirname(b.resolve(t)),r=b.resolve(e.outputDir),s=b.relative(n,r).replace(/\\/g,"/");return s?(s.startsWith(".")||(s=`./${s}`),s.endsWith("/")?s:`${s}/`):"./"}function yi(e,t,n){let r=wi(t,n);if(!r)return e;let s=e;for(let i of ai){let a=i.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),c=new RegExp(`(?:\\.{1,}/)+${a}`,"g"),o=new RegExp(`(?<!\\.)/${a}`,"g");s=s.replace(c,`${r}${i}`).replace(o,`${r}${i}`);let l=i.replace(/\//g,"\\/"),d=r.replace(/\//g,"\\/"),u=l.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),g=new RegExp(`(?:\\.{1,}\\\\/)+${u}`,"g"),f=new RegExp(`(?:\\.{1,}(?:\\\\/|\\\\))+${u}`,"g"),p=new RegExp(`(?<![\\.\\\\])\\/${u}`,"g");s=s.replace(g,`${d}${l}`).replace(f,`${d}${l}`).replace(p,`${d}${l}`);}return s}function dn(e,t,n){if(e.urlRewriteMode==="absolute")return t;let r=(()=>{try{return new URL(t,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin)}catch{return null}})(),s=r?r.pathname:hr(t,e.targetOrigin),i=r?`${r.search}${r.hash}`:"";return e.urlRewriteMode==="root-relative"?`${s}${i}`:`${wr(e.outputDir,n,s)}${i}`}function ln(e,t,n,r,s){bt(e,n,dn(t,r,s));}function Si(e,t,n,r,s){if(!n||n===r)return;bt(e,n,r);let i=dn(t,n,s),a=dn(t,r,s);bt(e,i,a);}function pn(e,t,n,r,s={}){let i=e,a={};ln(a,t,t.sourceOrigin,t.targetOrigin,r);for(let[o,l]of Object.entries(t.extraReplacements))ln(a,t,o,l,r);for(let[o,l]of Object.entries(n))ln(a,t,o,l,r);for(let[o,l]of Object.entries(s)){let d=n[o];!d||d===l||Si(a,t,l,d,r);}let c=Object.entries(a).sort((o,l)=>l[0].length-o[0].length);for(let[o,l]of c)i=pi(i,o,l);return t.urlRewriteMode!=="absolute"&&(i=yi(i,t,r)),i=hi(i,c.map(([,o])=>o)),gi(t,i,r)&&(i=fi(i)),i}async function bi(e,t,n,r){let s=b.join(e.outputDir,r),i=b.extname(s).toLowerCase(),a;try{a=await A.readFile(s,"utf8");}catch(l){if(l.code!=="ENOENT")throw l;return {changed:false}}let o=pn(a,e,t,i===".js"||i===".mjs"?void 0:s,n);return o!==a?(await tt(s,o,"utf8"),{changed:true}):{changed:false}}async function Pi(e,t){return await new Promise((n,r)=>{let s=o=>{if(c(),o?.error){r(new Error(o.error));return}n({changed:!!o?.changed});},i=o=>{c(),r(o);},a=o=>{c(),o!==0&&r(new Error(`Rewrite worker exited with code ${o}.`));},c=()=>{e.off("message",s),e.off("error",i),e.off("exit",a);};e.on("message",s),e.on("error",i),e.on("exit",a),e.postMessage({file:t});})}async function Pt(e,t,n={}){let s=(n.files?[...new Set(n.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 ri(["**/*"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(g=>g!=="asset-map.json"&&Pe(g)),i=0;if(s.length===0)return 0;let a=Math.max(1,Number(e.rewriteConcurrency||e.assetDownloadConcurrency||1)),c=Math.max(1,Math.min(a,s.length,availableParallelism()));await n.onStart?.({totalFiles:s.length,workerCount:c});let o=0,l=0,d=null;if(c===1){for(let g of s){let f=await bi(e,t,n.previousAssetMap??{},g);f.changed&&(i+=1),l+=1,await n.onProgress?.({index:l,totalFiles:s.length,changedTextFiles:i,file:g,changed:f.changed});}return i}let u=Array.from({length:c},async()=>{let g=new Worker(new URL("./rewrite-worker.js",import.meta.url),{workerData:{config:e,assetMap:t,previousAssetMap:n.previousAssetMap??{}}});try{for(;!d;){let f=o;if(f>=s.length)return;o+=1;let p=s[f],y=await Pi(g,p);y.changed&&(i+=1),l+=1,await n.onProgress?.({index:l,totalFiles:s.length,changedTextFiles:i,file:p,changed:y.changed});}}catch(f){throw d=f instanceof Error?f:new Error(String(f)),d}finally{await g.terminate().catch(()=>{});}});return await Promise.all(u),i}var le={error:0,warn:1,info:2,debug:3},vi=/^(?:authorization|proxyAuthorization|runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken|xStaticPublisherToken|xApiKey)$/i;function Ae(e){return e.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 Ct(e){return typeof e=="string"?Ae(e):Array.isArray(e)?e.map(t=>Ct(t)):e instanceof Error?{name:e.name,message:Ae(e.message),...e.stack?{stack:Ae(e.stack)}:{}}:e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([t,n])=>[t,vi.test(t)?"[REDACTED]":Ct(n)])):e}function xt(e){return e?Ct(e):void 0}var Sr=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",vt=Sr?b.join(Sr,"current-progress.json"):"",br=Promise.resolve();function Pr(e){return e.includes("crawl")?"crawl":e.includes("deploy")?"deploy":e.includes("invalidate")?"invalidate":e}function Ci(e,t){let n={...e},r=new Set(["pagesQueued","pagesRendered","assetsQueued","sitemapsQueued","pagesDiscovered","assetsDiscovered","sitemapsDiscovered","pagesSaved","assetsSaved","donePages","doneAssets","doneSitemaps","changedTextFiles","uploaded","failed","index","totalFiles","pageQueue","assetQueue","sitemapQueue"]);for(let[s,i]of Object.entries(t)){let a=n[s];if(typeof a=="number"&&typeof i=="number"&&r.has(s)){n[s]=Math.max(a,i);continue}n[s]=i;}return n}function vr(e,t,n){vt&&(br=br.then(async()=>{let r=new Date,s=r.toISOString();await A.mkdir(b.dirname(vt),{recursive:true});let i=await A.readFile(vt,"utf8").then(h=>JSON.parse(h)).catch(()=>null),a=i&&typeof i.details=="object"&&i.details?i.details:{},c=Pr(e),o=i?.currentStep||Pr(i?.source||""),l=i?.startedAt||s,d={...i?.stepDurationsSec??{}},u=i?.stepStartedAt||s;if(o&&o!==c&&i?.stepStartedAt){let h=Math.max(0,Math.round((r.getTime()-new Date(i.stepStartedAt).getTime())/1e3));d[o]=(d[o]??0)+h,u=s;}let g=Math.max(0,Math.round((r.getTime()-new Date(u).getTime())/1e3)),f=Math.max(0,Math.round((r.getTime()-new Date(l).getTime())/1e3)),p=Ae(t),y=xt(n)??{},m={checkedAt:s,source:e,message:p,details:Ci(a,y),startedAt:l,currentStep:c,stepStartedAt:u,stepElapsedSec:g,totalElapsedSec:f,stepDurationsSec:d};await A.writeFile(vt,JSON.stringify(m,null,2),"utf8");}).catch(()=>{}));}async function Cr(e,t){await A.mkdir(b.dirname(e),{recursive:true}),await A.appendFile(e,`${JSON.stringify(Ct(t))}
5
+ `,"utf8");}function Ri(e,t){let n=typeof t?.phase=="string"?t.phase.trim():"";return n||(e==="page"?"render-pages":e==="asset"?"download-assets":e==="sitemap"?"discovery":e==="timing"&&typeof t?.name=="string"&&t.name.trim()||"crawl")}function xi(e,t,n){let r=xt(n);return {checkedAt:new Date().toISOString(),currentStep:Ri(e,r),level:e,message:Ae(t),details:r??{}}}var Le=class{constructor(t,n,r="info"){this.logDir=t;this.logFile=n;this.level=r;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 A.mkdir(this.logDir,{recursive:true}),await A.writeFile(this.logPath,"","utf8");})()),this.initPromise}enqueueTask(t){this.writeQueue=this.writeQueue.then(t).catch(n=>{this.writeError=n instanceof Error?n:new Error(String(n));});}enqueueLine(t){this.enqueueTask(async()=>{await this.ensureLogFileReady(),await A.appendFile(this.logPath,`${t}
6
+ `,"utf8");});}enqueueJsonLine(t,n){this.enqueueTask(()=>Cr(t,n));}accepts(t){return le[t]<=le[this.level]}push(t,n,r){if(!this.accepts(t))return;let s=Ae(n),i=xt(r),a=JSON.stringify({time:new Date().toISOString(),level:t,message:s,...i||{}});this.enqueueLine(a),t==="error"?console.error(`[ERROR] ${s}`,i?JSON.stringify(i):""):t==="warn"?console.warn(`[WARN] ${s}`,i?JSON.stringify(i):""):console.log(`[${t.toUpperCase().padEnd(5)}] ${s}`);}info(t,n){this.push("info",t,n);}progress(t,n){vr(this.logFile,t,n);}debug(t,n){this.push("debug",t,n);}warn(t,n){this.push("warn",t,n);}reject(t,n,r,s,i){let a={kind:t,url:n,reason:r,...s?{source:s}:{},...i||{}};this.enqueueJsonLine(this.rejectedPath,a),this.accepts("debug")&&this.push("debug",`Rejected ${t} ${n}: ${r}`,a);}error(t,n){this.enqueueJsonLine(this.errorsPath,{message:t,...n||{}}),this.push("error",t,n);}mark(t){this.marks.set(t,Date.now());}endMark(t,n){let r=this.marks.get(t);if(!r)return;let s=Date.now()-r,i=Number((s/1e3).toFixed(2));this.push("info",`Timing ${t}: ${i}s`,{name:t,ms:s,seconds:i,...n||{}});}async flush(){let t=Date.now()-this.startedAt,n=Number((t/1e3).toFixed(2));if(this.push("info",`Total time: ${n}s`,{name:"total",ms:t,seconds:n}),await this.writeQueue,this.writeError)throw this.writeError}},Rt=class{constructor(t,n="info"){this.logDir=t;this.level=typeof n=="boolean"?n?"debug":"info":n,this.ensureLogFileReady();}logDir;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;level;get logPath(){return b.join(this.logDir,"crawl.log.jsonl")}get currentEventPath(){return b.join(this.logDir,"current-crawl-event.json")}get rejectedPath(){return b.join(this.logDir,"rejected.jsonl")}get ignoredPath(){return b.join(this.logDir,"ignored.jsonl")}get skippedPath(){return b.join(this.logDir,"skipped-http.jsonl")}get errorsPath(){return b.join(this.logDir,"errors.jsonl")}get timingsPath(){return b.join(this.logDir,"timings.jsonl")}ensureLogFileReady(){return this.initPromise||(this.initPromise=(async()=>{await A.mkdir(this.logDir,{recursive:true}),await A.writeFile(this.logPath,"","utf8");})()),this.initPromise}enqueueTask(t){this.writeQueue=this.writeQueue.then(t).catch(n=>{this.writeError=n instanceof Error?n:new Error(String(n));});}enqueueLine(t){this.enqueueTask(async()=>{await this.ensureLogFileReady(),await A.appendFile(this.logPath,`${t}
7
+ `,"utf8");});}enqueueJsonLine(t,n){this.enqueueTask(()=>Cr(t,n));}updateCurrentEvent(t,n,r){let s=xi(t,n,r);this.enqueueTask(async()=>{await A.mkdir(this.logDir,{recursive:true}),await A.writeFile(this.currentEventPath,JSON.stringify(s,null,2),"utf8");});}push(t,n,r){let s=Ae(n),i=xt(r),a=JSON.stringify({time:new Date().toISOString(),level:t,message:s,...i||{}});this.enqueueLine(a),this.updateCurrentEvent(t,s,i),t==="error"?console.error(s,i||""):t==="warn"?console.warn(s,i||""):(le[this.level]>=le.debug||["summary","page","sitemap","asset","timing"].includes(t))&&console.log(s);}info(t,n){this.push("info",t,n);}debug(t,n){le[this.level]<le.debug||this.push("debug",t,n);}progress(t,n){vr("crawl.log.jsonl",t,n),this.updateCurrentEvent("progress",t,n);}checkpoint(t,n){this.updateCurrentEvent("checkpoint",t,n);}page(t,n){this.push("page",t,n);}sitemap(t,n){this.push("sitemap",t,n);}asset(t,n){this.push("asset",t,n);}warn(t,n){this.push("warn",t,n);}error(t,n){this.enqueueJsonLine(this.errorsPath,{message:t,...n||{}}),this.push("error",t,n);}summary(t,n){this.push("summary",t,n);}mark(t){this.marks.set(t,Date.now());}endMark(t,n){let r=this.marks.get(t);if(!r)return;let s=Date.now()-r,i={name:t,ms:s,seconds:Number((s/1e3).toFixed(2)),...n||{}};this.enqueueJsonLine(this.timingsPath,i),this.push("timing",`Timing ${t}: ${i.seconds}s`,i);}reject(t,n,r,s){let i={kind:t,url:n,reason:r,source:s};this.enqueueJsonLine(this.rejectedPath,i),le[this.level]>=le.debug&&this.push("reject",`Rejected ${t} ${n}: ${r}`,i);}ignore(t,n,r,s){let i={kind:t,url:n,reason:r,source:s};this.enqueueJsonLine(this.ignoredPath,i),le[this.level]>=le.debug&&this.push("ignore",`Ignored ${t} ${n}: ${r}`,i);}skip(t,n,r,s){let i={kind:t,url:n,status:r,source:s};this.enqueueJsonLine(this.skippedPath,i),this.push("warn",`Skipped ${t} ${n}: HTTP ${r}`,i);}async flush(){let t=Date.now()-this.startedAt,n={name:"total",ms:t,seconds:Number((t/1e3).toFixed(2))};if(this.enqueueJsonLine(this.timingsPath,n),await this.writeQueue,this.writeError)throw this.writeError}};var Rr="(?:css|js|mjs|json|map|xml|xsl|rss|atom|txt|enc|jws|png|jpe?g|gif|webp|avif|svg|ico|woff2?|ttf|otf|eot|pdf|mp4|webm)";function Ir(e){let t=new RegExp(`(?:https?:)?//[^\\s'"<>\\);]+\\.${Rr}(?:[?#][^\\s'"<>\\);]*)?(?=[\\s'"<>\\);]|$)|(?<![A-Za-z0-9._-])/[^\\s'"<>\\);]+\\.${Rr}(?:[?#][^\\s'"<>\\);]*)?(?=[\\s'"<>\\);]|$)`,"gi");return [...e.matchAll(t)].map(n=>n[0])}function xr(e){return !!e&&/^(?:\.{1,2}\/)?[A-Za-z0-9@_][A-Za-z0-9@._/-]*\.(?:js|mjs)(?:[?#][^\s<>\\]*)?$/i.test(e)&&!e.startsWith("/")&&!e.startsWith("//")&&!/^[a-z][a-z0-9+.-]*:/i.test(e)&&!/(?:^|\/)node_modules(?:\/|$)|(?:^|\/)\.pnpm(?:\/|$)/i.test(e)}function Ar(e){let t=new Set,n=[],r=/\bimport\s*\(\s*["'`]([^"'`\s<>\\]+)["'`]\s*\)/g,s=/\b(?:[A-Za-z_$][\w$]*\.)?u\s*=\s*(?:\([^)]*\)|[A-Za-z_$][\w$]*)\s*=>\s*["'`]([^"'`\s<>\\]+)["'`]/g;for(let i of e.matchAll(r))i[1]&&xr(i[1])&&n.push({index:i.index??0,value:i[1]});for(let i of e.matchAll(s))i[1]&&xr(i[1])&&n.push({index:i.index??0,value:i[1]});for(let i of n.sort((a,c)=>a.index-c.index))t.add(i.value);return [...t]}function Tr(e,t){let n=b.relative(t,e);return n===""||!n.startsWith("..")&&!b.isAbsolute(n)}function kr(e,t,n,r){let s=b.resolve(e),i=b.resolve(t),a=b.extname(s).toLowerCase();return !Tr(s,i)||a!==".html"&&a!==".htm"||n.has(s)?false:!r.some(c=>Tr(s,b.resolve(c)))}function $r(e,t){return typeof e=="string"&&e.length>0&&t?.supported===true&&typeof t.token=="string"&&t.token.length>0&&e===t.token}function Er(e,t){try{let n=new URL(e),r=new URL(t,n);if(r.origin!==n.origin)return !1;let s=n.pathname.replace(/\/+$/,"");if(s&&r.pathname!==s&&!r.pathname.startsWith(`${s}/`))return !1;let i=s?r.pathname.slice(s.length)||"/":r.pathname;return ["/wp-includes/","/wp-content/plugins/","/wp-content/themes/","/wp-content/mu-plugins/"].some(a=>i.startsWith(a))}catch{return false}}function Mr(e){return e<1}function Fr(e){let t=String(e??"").toLowerCase();return t==="strict"?"Strict":t==="lax"?"Lax":t==="none"?"None":"Unspecified"}function Ai(e){let t=e.pathname||"/";if(!t.startsWith("/")||t==="/")return "/";let n=t.lastIndexOf("/");return n<=0?"/":t.slice(0,n)}function nt(e){return `${e.name.toLowerCase()}
8
8
  ${e.domain.toLowerCase()}
9
- ${e.path}`}function Ir(e,t){let n;try{n=new URL(t);}catch{return null}let r=e.split(";"),s=r.shift()?.trim()??"",i=s.indexOf("=");if(i<=0)return null;let a=s.slice(0,i).trim();if(!a||[...a].some(y=>y<=" "||"=;,".includes(y)))return null;let c=n.hostname.toLowerCase(),o=yi(n),u=null,d=true,l=false,g=false,f="Unspecified",p=false;for(let y of r){let m=y.indexOf("="),h=(m<0?y:y.slice(0,m)).trim().toLowerCase(),w=m<0?"":y.slice(m+1).trim();if(h==="domain"&&w)c=w.toLowerCase();else if(h==="path"&&w.startsWith("/"))o=w;else if(h==="expires"){let P=Date.parse(w);Number.isFinite(P)&&(u=Math.floor(P/1e3),d=false);}else if(h==="max-age"&&/^-?\d+$/u.test(w)){let P=Number.parseInt(w,10);u=Math.floor(Date.now()/1e3)+P,d=false;}else h==="secure"?l=true:h==="httponly"?g=true:h==="samesite"?f=xr(w):h==="partitioned"&&(p=true);}return {name:a,domain:c,path:o,expires:u,session:d,secure:l,httpOnly:g,sameSite:f,partitioned:p}}function Si(e){let t=!Number.isFinite(e.expires)||e.expires<=0;return {name:e.name,domain:e.domain.toLowerCase(),path:e.path||"/",expires:t?null:e.expires,session:t,secure:e.secure,httpOnly:e.httpOnly,sameSite:xr(e.sameSite),partitioned:e.partitioned===true}}function Ar(e,t){let n=new Map(e.map(r=>[et(r),r.value]));return t.filter(r=>n.get(et(r))!==r.value).map(Si)}function bi(e,t){let n=e.session||t.session;return {...e,expires:n?null:Math.max(e.expires??0,t.expires??0)||null,session:n,secure:e.secure&&t.secure,httpOnly:e.httpOnly&&t.httpOnly,sameSite:e.sameSite===t.sameSite?e.sameSite:"Unspecified",partitioned:e.partitioned&&t.partitioned}}var Ct=class{#e=new Map;add(t){if(!t)return;let n={...t,domain:t.domain.toLowerCase(),path:t.path||"/"},r=et(n),s=this.#e.get(r);this.#e.set(r,s?bi(s,n):n);}addAll(t){for(let n of t)this.add(n);}artifact(){return {schemaVersion:1,observations:[...this.#e.values()].sort((t,n)=>et(t).localeCompare(et(n)))}}};async function Rt(e,t){await A.mkdir(S.dirname(e),{recursive:true});let n=`${e}.${process.pid}.tmp`;await A.writeFile(n,`${JSON.stringify(t,null,2)}
10
- `,{encoding:"utf8",mode:384}),await A.rename(n,e);}var Ri=["text/css","javascript","image/","font/","application/font","application/json","application/xml","text/xml","application/rss+xml","application/atom+xml","application/xslt+xml","text/xsl","image/svg+xml"],_r=new Set([".css",".js",".mjs",".json",".map",".xml",".xsl",".rss",".atom",".txt",".enc",".jws",".png",".jpg",".jpeg",".gif",".webp",".avif",".svg",".ico",".woff",".woff2",".ttf",".otf",".eot",".pdf",".mp4",".webm"]),Nr=new Set(["",".html",".htm"]),xi=new Set([".css",".xml",".xsl",".svg",".json",".html",".htm",".txt",".enc",".jws"]),Ii=["/wp-content/","/wp-includes/","/wp-admin/","/static/","/assets/","/build/","/_next/"];function Ai(e){let t=[],n="full",r="full",s=false,i=false,a="";for(let c=0;c<e.length;c++){let o=e[c];if(o==="--retry-timeouts")n="retry-timeouts",s=true;else if(o==="--resume")s=true;else if(o==="--resume-rewrite")s=true,i=true;else if(o==="--crawl-mode"){let u=e[++c];if(!u)throw new Error("--crawl-mode requires a value");r=u==="incremental"?"incremental":"full",s||=r==="incremental";}else if(o.startsWith("--crawl-mode="))r=o.slice(13)==="incremental"?"incremental":"full",s||=r==="incremental";else if(o==="--incremental")r="incremental",s=true;else if(o==="--url"){let u=e[++c];if(!u)throw new Error("--url requires a value");n="single-url",s=true,t.push(u);}else if(o.startsWith("--url="))n="single-url",s=true,t.push(o.slice(6));else if(o==="--urls"){let u=e[++c];if(!u)throw new Error("--urls requires a file path");n="single-url",s=true,t.push(`@${u}`);}else if(o==="--content-sync-plan"){let u=e[++c];if(!u)throw new Error("--content-sync-plan requires a file path");n="content-sync",r="incremental",s=true,a=S.resolve(u);}else o.startsWith("--content-sync-plan=")&&(n="content-sync",r="incremental",s=true,a=S.resolve(o.slice(20)));}return {mode:n,crawlMode:r,urls:t,preserveOutput:s,resumeRewrite:i,contentSyncPlanPath:a}}function $t(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?S.resolve(e):""}function Ti(){let e=$t();return e?S.join(e,"privacy","cookie-observations.json"):null}function ki(){let e=$t();return e?S.resolve(e,".."):""}function $i(){let e=process.env.STATIC_PUBLISHER_WP_ROOT||process.env.WPSUITE_STATIC_PUBLISHER_WP_ROOT||"";return e.trim()?S.resolve(e):""}function Ei(e){let t=e.trim(),n=[{alias:"@storage-root",root:ki(),requiredEnv:"STATIC_PUBLISHER_RUNTIME_DIR or WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR"},{alias:"@runtime",root:$t(),requiredEnv:"STATIC_PUBLISHER_RUNTIME_DIR or WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR"},{alias:"@wp-root",root:$i(),requiredEnv:"STATIC_PUBLISHER_WP_ROOT or WPSUITE_STATIC_PUBLISHER_WP_ROOT"}];for(let r of n){if(t!==r.alias&&!t.startsWith(`${r.alias}/`))continue;if(!r.root)return {resolvedPath:null,alias:r.alias,requiredEnv:r.requiredEnv};let s=t.slice(r.alias.length).replace(/^\/+/,"");return {resolvedPath:s?S.resolve(r.root,s):r.root,alias:r.alias}}return {resolvedPath:S.resolve(t)}}function Br(e){let t=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(t)return S.resolve(t);let n=$t();return n?S.join(n,"crawl-manifest.json"):S.join(e.outputDir,".crawl-manifest.json")}function Wr(){return {schemaVersion:5,updatedAt:"",pages:{},assets:{}}}function kr(e){return {url:String(e.url||""),outputPath:String(e.outputPath||""),contentType:typeof e.contentType=="string"&&e.contentType?e.contentType:null,changeToken:typeof e.changeToken=="string"?e.changeToken:null,tokenSource:typeof e.tokenSource=="string"?e.tokenSource:null,sitemapLastmod:typeof e.sitemapLastmod=="string"?e.sitemapLastmod:null,discoveredPages:Array.isArray(e.discoveredPages)?e.discoveredPages.map(t=>String(t)):[],discoveredAssets:Array.isArray(e.discoveredAssets)?e.discoveredAssets.map(t=>String(t)):[],lastCrawledAt:String(e.lastCrawledAt||""),lastSeenRunId:String(e.lastSeenRunId||""),rewriteComplete:e.rewriteComplete===true}}function Mi(e){let t=e.isText===true;return {url:String(e.url||""),outputPath:String(e.outputPath||""),isText:t,contentType:typeof e.contentType=="string"&&e.contentType?e.contentType:null,changeToken:typeof e.changeToken=="string"?e.changeToken:null,tokenSource:typeof e.tokenSource=="string"?e.tokenSource:null,discoveredAssets:Array.isArray(e.discoveredAssets)?e.discoveredAssets.map(n=>String(n)):[],lastCrawledAt:String(e.lastCrawledAt||""),lastSeenRunId:String(e.lastSeenRunId||""),unreferencedRuns:Math.max(0,Number(e.unreferencedRuns)||0),rewriteComplete:e.rewriteComplete===true||!t}}function un(e){let t=e?.["content-type"]?.trim();return t||null}function Di(){return {rewriteTargets:new Set,changedFiles:new Set,deletedFiles:new Set}}function te(e,t,n){let r=pe(t.outputDir,n);r&&(e.changedFiles.add(r),e.deletedFiles.delete(r));}function ln(e,t,n){let r=pe(t.outputDir,n);r&&(e.deletedFiles.add(r),e.changedFiles.delete(r));}function Ie(e,t){e.rewriteTargets.add(S.resolve(t));}async function Oi(e){try{let t=await A.readFile(Br(e),"utf8"),n=JSON.parse(t);if(n&&typeof n=="object"&&n.pages&&typeof n.pages=="object"){if((n.schemaVersion===2||n.schemaVersion===3||n.schemaVersion===4||n.schemaVersion===5)&&n.assets&&typeof n.assets=="object")return {schemaVersion:5,updatedAt:String(n.updatedAt||""),pages:Object.fromEntries(Object.entries(n.pages).map(([r,s])=>[r,kr(s)])),assets:Object.fromEntries(Object.entries(n.assets).map(([r,s])=>[r,Mi(s)]))};if(n.schemaVersion===1)return {schemaVersion:5,updatedAt:String(n.updatedAt||""),pages:Object.fromEntries(Object.entries(n.pages).map(([r,s])=>[r,kr(s)])),assets:{}}}}catch{}return Wr()}function dn(e){return JSON.parse(JSON.stringify(e))}async function Fi(e,t){let n=Br(e);await A.mkdir(S.dirname(n),{recursive:true}),await A.writeFile(n,JSON.stringify(t,null,2),"utf8");}async function ve(e,t){if(!t.trackManifest)return;let n=async()=>{let r=dn(t.manifest);r.updatedAt=new Date().toISOString(),t.manifest.updatedAt=r.updatedAt,await Fi(e,r);};t.manifestSaveChain=t.manifestSaveChain.then(n,n),await t.manifestSaveChain;}function rt(e){if(!e)return null;let t=e.trim();return t||null}function Ui(e,t){let n=new Set,r=new Set([...Object.keys(e),...Object.keys(t)]);for(let s of r){let i=e[s],a=t[s];!i||!a||i===a||n.add(s);}return n}async function Hr(e,t,n,r){let s=[...new Set(n.map(c=>c.trim()).filter(Boolean))],i=String(e.wpsuite?.runtimeToken||"").trim();if(s.length===0||!i)return new Map;let a=new URL("/wp-json/smartcloud-static-publisher/v1/change-tokens",e.sourceOrigin).toString();try{let c=await t.post(a,{timeout:3e4,failOnStatusCode:!1,headers:{"content-type":"application/json","x-static-publisher-token":i},data:JSON.stringify({urls:s})});if(!c.ok())return r.warn(`Change token lookup failed with HTTP ${c.status()}`,{endpoint:a,status:c.status()}),new Map;let o=await c.json().catch(()=>null);if(!o||!Array.isArray(o.items))return new Map;let u=new Map;for(let d of o.items)!d||typeof d.url!="string"||u.set(d.url,d);return u}catch(c){return r.warn("Change token lookup failed; falling back to sitemap metadata",{endpoint:a,error:c instanceof Error?`${c.name}: ${c.message.split(/\r?\n/u,1)[0]}`:"Unknown change-token request failure"}),new Map}}async function It(e,t,n,r,s){if(!n.enabled&&!n.trackManifest)return null;if(n.changeTokenCache.has(r))return n.changeTokenCache.get(r)??null;let a=(await Hr(e,t,[r],s)).get(r)??null;return yn(e,r)&&(!a||a.supported!==true||!a.token)&&(a=await qi(e,t,r,s)),a=zr(e,a),n.changeTokenCache.set(r,a),a}async function Li(e,t,n,r,s){if(!n.trackManifest)return;let i=[...new Set(r.map(a=>a.trim()).filter(Boolean))].filter(a=>!n.changeTokenCache.has(a));for(let a=0;a<i.length;a+=250){let c=i.slice(a,a+250),o=await Hr(e,t,c,s);for(let u of c)n.changeTokenCache.set(u,zr(e,o.get(u)??null));}}function ji(e){return `subscriber=${e.wpsuite?.siteSettings?.subscriber===true?"1":"0"}`}function zr(e,t){if(!t?.supported||!t.token||t.tokenSource==="wp-media-file")return t;let n=ji(e);return {...t,token:Ye(`${t.token}
11
- ${n}`,40),tokenSource:t.tokenSource?`${t.tokenSource}+site-subscriber`:"site-subscriber"}}async function qi(e,t,n,r){try{let s=await t.get(n,{timeout:Math.min(e.navigationTimeoutMs,15e3),failOnStatusCode:!1});if(s.status()!==404)return r.warn(`Generated 404 fallback token skipped for ${n}`,{url:n,status:s.status(),requestPath:e.generated404RequestPath,source:"generated-404-change-token-fallback"}),null;let i=await s.text().catch(()=>""),a=Ye(`${n}
9
+ ${e.path}`}function Dr(e,t){let n;try{n=new URL(t);}catch{return null}let r=e.split(";"),s=r.shift()?.trim()??"",i=s.indexOf("=");if(i<=0)return null;let a=s.slice(0,i).trim();if(!a||[...a].some(y=>y<=" "||"=;,".includes(y)))return null;let c=n.hostname.toLowerCase(),o=Ai(n),l=null,d=true,u=false,g=false,f="Unspecified",p=false;for(let y of r){let m=y.indexOf("="),h=(m<0?y:y.slice(0,m)).trim().toLowerCase(),w=m<0?"":y.slice(m+1).trim();if(h==="domain"&&w)c=w.toLowerCase();else if(h==="path"&&w.startsWith("/"))o=w;else if(h==="expires"){let P=Date.parse(w);Number.isFinite(P)&&(l=Math.floor(P/1e3),d=false);}else if(h==="max-age"&&/^-?\d+$/u.test(w)){let P=Number.parseInt(w,10);l=Math.floor(Date.now()/1e3)+P,d=false;}else h==="secure"?u=true:h==="httponly"?g=true:h==="samesite"?f=Fr(w):h==="partitioned"&&(p=true);}return {name:a,domain:c,path:o,expires:l,session:d,secure:u,httpOnly:g,sameSite:f,partitioned:p}}function Ti(e){let t=!Number.isFinite(e.expires)||e.expires<=0;return {name:e.name,domain:e.domain.toLowerCase(),path:e.path||"/",expires:t?null:e.expires,session:t,secure:e.secure,httpOnly:e.httpOnly,sameSite:Fr(e.sameSite),partitioned:e.partitioned===true}}function Or(e,t){let n=new Map(e.map(r=>[nt(r),r.value]));return t.filter(r=>n.get(nt(r))!==r.value).map(Ti)}function ki(e,t){let n=e.session||t.session;return {...e,expires:n?null:Math.max(e.expires??0,t.expires??0)||null,session:n,secure:e.secure&&t.secure,httpOnly:e.httpOnly&&t.httpOnly,sameSite:e.sameSite===t.sameSite?e.sameSite:"Unspecified",partitioned:e.partitioned&&t.partitioned}}var It=class{#e=new Map;add(t){if(!t)return;let n={...t,domain:t.domain.toLowerCase(),path:t.path||"/"},r=nt(n),s=this.#e.get(r);this.#e.set(r,s?ki(s,n):n);}addAll(t){for(let n of t)this.add(n);}artifact(){return {schemaVersion:1,observations:[...this.#e.values()].sort((t,n)=>nt(t).localeCompare(nt(n)))}}};async function At(e,t){await A.mkdir(b.dirname(e),{recursive:true});let n=`${e}.${process.pid}.tmp`;await A.writeFile(n,`${JSON.stringify(t,null,2)}
10
+ `,{encoding:"utf8",mode:384}),await A.rename(n,e);}function rt(e){let t=e;for(let r=0;r<3;r+=1){if(/%(?:2f|5c)/i.test(t))return null;try{let s=decodeURIComponent(t);if(s===t)break;t=s;}catch{return null}}return t.includes("\0")||t.includes("\\")||t.split("/").some(r=>r==="."||r==="..")?null:t}function st(e,t){let n=rt(t);if(n===null)throw new Error(`Unsafe output URL pathname: ${t}`);let r=b.resolve(e),s=b.resolve(r,n.replace(/^\/+/,""));if(s!==r&&!s.startsWith(`${r}${b.sep}`))throw new Error(`Output path escapes configured root: ${t}`);return s}var Fi=["text/css","javascript","image/","font/","application/font","application/json","application/xml","text/xml","application/rss+xml","application/atom+xml","application/xslt+xml","text/xsl","image/svg+xml"],Vr=new Set([".css",".js",".mjs",".json",".map",".xml",".xsl",".rss",".atom",".txt",".enc",".jws",".png",".jpg",".jpeg",".gif",".webp",".avif",".svg",".ico",".woff",".woff2",".ttf",".otf",".eot",".pdf",".mp4",".webm"]),Kr=new Set(["",".html",".htm"]),Di=new Set([".css",".xml",".xsl",".svg",".json",".html",".htm",".txt",".enc",".jws"]),Oi=["/wp-content/","/wp-includes/","/wp-admin/","/static/","/assets/","/build/","/_next/"];function Ui(e){let t=[],n="full",r="full",s=false,i=false,a="";for(let c=0;c<e.length;c++){let o=e[c];if(o==="--retry-timeouts")n="retry-timeouts",s=true;else if(o==="--resume")s=true;else if(o==="--resume-rewrite")s=true,i=true;else if(o==="--crawl-mode"){let l=e[++c];if(!l)throw new Error("--crawl-mode requires a value");r=l==="incremental"?"incremental":"full",s||=r==="incremental";}else if(o.startsWith("--crawl-mode="))r=o.slice(13)==="incremental"?"incremental":"full",s||=r==="incremental";else if(o==="--incremental")r="incremental",s=true;else if(o==="--url"){let l=e[++c];if(!l)throw new Error("--url requires a value");n="single-url",s=true,t.push(l);}else if(o.startsWith("--url="))n="single-url",s=true,t.push(o.slice(6));else if(o==="--urls"){let l=e[++c];if(!l)throw new Error("--urls requires a file path");n="single-url",s=true,t.push(`@${l}`);}else if(o==="--content-sync-plan"){let l=e[++c];if(!l)throw new Error("--content-sync-plan requires a file path");n="content-sync",r="incremental",s=true,a=b.resolve(l);}else o.startsWith("--content-sync-plan=")&&(n="content-sync",r="incremental",s=true,a=b.resolve(o.slice(20)));}return {mode:n,crawlMode:r,urls:t,preserveOutput:s,resumeRewrite:i,contentSyncPlanPath:a}}function Dt(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?b.resolve(e):""}function Li(){let e=Dt();return e?b.join(e,"privacy","cookie-observations.json"):null}function ji(){let e=Dt();return e?b.resolve(e,".."):""}function qi(){let e=process.env.STATIC_PUBLISHER_WP_ROOT||process.env.WPSUITE_STATIC_PUBLISHER_WP_ROOT||"";return e.trim()?b.resolve(e):""}function _i(e){let t=e.trim(),n=[{alias:"@storage-root",root:ji(),requiredEnv:"STATIC_PUBLISHER_RUNTIME_DIR or WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR"},{alias:"@runtime",root:Dt(),requiredEnv:"STATIC_PUBLISHER_RUNTIME_DIR or WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR"},{alias:"@wp-root",root:qi(),requiredEnv:"STATIC_PUBLISHER_WP_ROOT or WPSUITE_STATIC_PUBLISHER_WP_ROOT"}];for(let r of n){if(t!==r.alias&&!t.startsWith(`${r.alias}/`))continue;if(!r.root)return {resolvedPath:null,alias:r.alias,requiredEnv:r.requiredEnv};let s=t.slice(r.alias.length).replace(/^\/+/,"");return {resolvedPath:s?b.resolve(r.root,s):r.root,alias:r.alias}}return {resolvedPath:b.resolve(t)}}function Gr(e){let t=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(t)return b.resolve(t);let n=Dt();return n?b.join(n,"crawl-manifest.json"):b.join(e.outputDir,".crawl-manifest.json")}function Zr(){return {schemaVersion:6,updatedAt:"",pages:{},assets:{}}}function Lr(e){return {url:String(e.url||""),outputPath:String(e.outputPath||""),contentType:typeof e.contentType=="string"&&e.contentType?e.contentType:null,changeToken:typeof e.changeToken=="string"?e.changeToken:null,tokenSource:typeof e.tokenSource=="string"?e.tokenSource:null,sitemapLastmod:typeof e.sitemapLastmod=="string"?e.sitemapLastmod:null,discoveredPages:Array.isArray(e.discoveredPages)?e.discoveredPages.map(t=>String(t)):[],discoveredAssets:Array.isArray(e.discoveredAssets)?e.discoveredAssets.map(t=>String(t)):[],lastCrawledAt:String(e.lastCrawledAt||""),lastSeenRunId:String(e.lastSeenRunId||""),rewriteComplete:e.rewriteComplete===true}}function Ni(e){let t=e.isText===true;return {url:String(e.url||""),outputPath:String(e.outputPath||""),isText:t,contentType:typeof e.contentType=="string"&&e.contentType?e.contentType:null,cacheControl:typeof e.cacheControl=="string"&&e.cacheControl?e.cacheControl:null,changeToken:typeof e.changeToken=="string"?e.changeToken:null,tokenSource:typeof e.tokenSource=="string"?e.tokenSource:null,discoveredAssets:Array.isArray(e.discoveredAssets)?e.discoveredAssets.map(n=>String(n)):[],lastCrawledAt:String(e.lastCrawledAt||""),lastSeenRunId:String(e.lastSeenRunId||""),unreferencedRuns:Math.max(0,Number(e.unreferencedRuns)||0),rewriteComplete:e.rewriteComplete===true||!t}}function hn(e){let t=e?.["content-type"]?.trim();return t||null}function wn(e){let t=e?.["cache-control"]?.trim();return t||null}function Bi(e){return e?e.split(",").map(t=>t.trim().toLowerCase()).some(t=>t==="no-cache"||t==="no-store"||/^max-age\s*=\s*0$/.test(t)):false}function Wi(){return {rewriteTargets:new Set,changedFiles:new Set,deletedFiles:new Set,revalidateFiles:new Set}}function Tt(e,t,n,r){let s=ce(t.outputDir,n);s&&(Bi(r)?e.revalidateFiles.add(s):e.revalidateFiles.delete(s));}function te(e,t,n){let r=ce(t.outputDir,n);r&&(e.changedFiles.add(r),e.deletedFiles.delete(r));}function yn(e,t,n){let r=ce(t.outputDir,n);r&&(e.deletedFiles.add(r),e.changedFiles.delete(r),e.revalidateFiles.delete(r));}function Te(e,t){e.rewriteTargets.add(b.resolve(t));}async function Hi(e){try{let t=await A.readFile(Gr(e),"utf8"),n=JSON.parse(t);if(n&&typeof n=="object"&&n.pages&&typeof n.pages=="object"){if((n.schemaVersion===2||n.schemaVersion===3||n.schemaVersion===4||n.schemaVersion===5||n.schemaVersion===6)&&n.assets&&typeof n.assets=="object")return {schemaVersion:6,updatedAt:String(n.updatedAt||""),pages:Object.fromEntries(Object.entries(n.pages).map(([r,s])=>[r,Lr(s)])),assets:Object.fromEntries(Object.entries(n.assets).map(([r,s])=>[r,Ni(s)]))};if(n.schemaVersion===1)return {schemaVersion:6,updatedAt:String(n.updatedAt||""),pages:Object.fromEntries(Object.entries(n.pages).map(([r,s])=>[r,Lr(s)])),assets:{}}}}catch{}return Zr()}function Sn(e){return JSON.parse(JSON.stringify(e))}async function zi(e,t){let n=Gr(e);await A.mkdir(b.dirname(n),{recursive:true}),await A.writeFile(n,JSON.stringify(t,null,2),"utf8");}async function ve(e,t){if(!t.trackManifest)return;let n=async()=>{let r=Sn(t.manifest);r.updatedAt=new Date().toISOString(),t.manifest.updatedAt=r.updatedAt,await zi(e,r);};t.manifestSaveChain=t.manifestSaveChain.then(n,n),await t.manifestSaveChain;}function at(e){if(!e)return null;let t=e.trim();return t||null}function Qi(e,t){let n=new Set,r=new Set([...Object.keys(e),...Object.keys(t)]);for(let s of r){let i=e[s],a=t[s];!i||!a||i===a||n.add(s);}return n}async function Yr(e,t,n,r){let s=[...new Set(n.map(c=>c.trim()).filter(Boolean))],i=String(e.wpsuite?.runtimeToken||"").trim();if(s.length===0||!i)return new Map;let a=new URL("/wp-json/smartcloud-static-publisher/v1/change-tokens",e.sourceOrigin).toString();try{let c=await t.post(a,{timeout:3e4,failOnStatusCode:!1,headers:{"content-type":"application/json","x-static-publisher-token":i},data:JSON.stringify({urls:s})});if(!c.ok())return r.warn(`Change token lookup failed with HTTP ${c.status()}`,{endpoint:a,status:c.status()}),new Map;let o=await c.json().catch(()=>null);if(!o||!Array.isArray(o.items))return new Map;let l=new Map;for(let d of o.items)!d||typeof d.url!="string"||l.set(d.url,d);return l}catch(c){return r.warn("Change token lookup failed; falling back to sitemap metadata",{endpoint:a,error:c instanceof Error?`${c.name}: ${c.message.split(/\r?\n/u,1)[0]}`:"Unknown change-token request failure"}),new Map}}async function $t(e,t,n,r,s){if(!n.enabled&&!n.trackManifest)return null;if(n.changeTokenCache.has(r))return n.changeTokenCache.get(r)??null;let a=(await Yr(e,t,[r],s)).get(r)??null;return xn(e,r)&&(!a||a.supported!==true||!a.token)&&(a=await Ki(e,t,r,s)),a=Xr(e,a),n.changeTokenCache.set(r,a),a}async function Ji(e,t,n,r,s){if(!n.trackManifest)return;let i=[...new Set(r.map(a=>a.trim()).filter(Boolean))].filter(a=>!n.changeTokenCache.has(a));for(let a=0;a<i.length;a+=250){let c=i.slice(a,a+250),o=await Yr(e,t,c,s);for(let l of c)n.changeTokenCache.set(l,Xr(e,o.get(l)??null));}}function Vi(e){return `subscriber=${e.wpsuite?.siteSettings?.subscriber===true?"1":"0"}`}function Xr(e,t){if(!t?.supported||!t.token||t.tokenSource==="wp-media-file")return t;let n=Vi(e);return {...t,token:et(`${t.token}
11
+ ${n}`,40),tokenSource:t.tokenSource?`${t.tokenSource}+site-subscriber`:"site-subscriber"}}async function Ki(e,t,n,r){try{let s=await t.get(n,{timeout:Math.min(e.navigationTimeoutMs,15e3),failOnStatusCode:!1});if(s.status()!==404)return r.warn(`Generated 404 fallback token skipped for ${n}`,{url:n,status:s.status(),requestPath:e.generated404RequestPath,source:"generated-404-change-token-fallback"}),null;let i=await s.text().catch(()=>""),a=et(`${n}
12
12
  404
13
- ${i}`,40);return r.info(`Using generated 404 fallback change token for ${n}`,{url:n,requestPath:e.generated404RequestPath,source:"generated-404-change-token-fallback"}),{url:n,supported:!0,token:a,tokenSource:"generated-404-response-hash",postId:null,dependencyPostIds:[],reason:null}}catch(s){return r.warn(`Generated 404 fallback token lookup failed for ${n}`,{url:n,error:String(s),requestPath:e.generated404RequestPath,source:"generated-404-change-token-fallback"}),null}}async function _i(e,t,n,r,s,i){let a=yn(e,s);if(!r.enabled)return {action:"render",changeToken:r.trackManifest?await It(e,t,r,s,i):null};let c=r.manifest.pages[s];if(!c)return {action:"render",changeToken:await It(e,t,r,s,i)};let o=await It(e,t,r,s,i);if(o?.supported&&o.token)return c.changeToken===o.token?a&&!await Ni(e,t,s,i)?{action:"render",changeToken:o}:{action:"reuse",changeToken:o}:{action:"render",changeToken:o};let u=rt(n.sitemapLastmodByPage[s]);return u&&c.sitemapLastmod&&c.sitemapLastmod===u?{action:"reuse",changeToken:o}:{action:"render",changeToken:o}}async function Ni(e,t,n,r){try{let i=(await t.get(n,{timeout:Math.min(e.navigationTimeoutMs,15e3),failOnStatusCode:!1})).status();return i===404?!0:(r.warn(`Generated 404 request path no longer returns 404 for ${n}`,{url:n,status:i,requestPath:e.generated404RequestPath,source:"generated-404-status-probe"}),!1)}catch(s){return r.warn(`Generated 404 status probe failed for ${n}`,{url:n,error:String(s),requestPath:e.generated404RequestPath,source:"generated-404-status-probe"}),false}}async function Bi(e,t,n,r,s){let i=$r(n.previousManifest,void 0,r,"previous-manifest"),a=0;for(let[g,f]of Object.entries(n.manifest.pages))if(!n.seenPages.has(g)){ln(s,e,f.outputPath);try{await A.unlink(f.outputPath);}catch(p){(p.code||"")!=="ENOENT"&&r.warn(`Failed to remove stale incremental page output for ${g}`,{url:g,outputPath:f.outputPath,error:String(p)});}delete n.manifest.pages[g],a++;}let c=$r(n.manifest,n.runId,r,"active-manifest"),o=new Set,u=new Set;for(let g of c){for(let y of st(e,g))o.add(y);let f=n.manifest.assets[g]?.outputPath;f&&u.add(f);let p=qe(e,t.assetMap,g);p&&u.add(p);}for(let g of Object.keys(n.manifest.assets)){if(c.has(g)){n.manifest.assets[g].unreferencedRuns=0;continue}let f=n.manifest.assets[g];if(Rr(f.unreferencedRuns)){f.unreferencedRuns+=1,r.warn(`Deferred deletion of newly unreferenced incremental asset ${g}.`,{url:g,unreferencedRuns:f.unreferencedRuns,phase:"incremental-cleanup",source:"asset-deletion-grace"});continue}delete n.manifest.assets[g];}let d=0,l=0;for(let g of i){if(n.manifest.assets[g])continue;let f=new Set,p=n.previousManifest.assets[g]?.outputPath;p&&f.add(p);let y=qe(e,t.assetMap,g);if(!c.has(g)&&y)f.add(y);else if(!c.has(g)){let m=oo(e,g,r,"incremental-cleanup");m&&(f.add(m.originalFilePath),m.hashedFilePath&&f.add(m.hashedFilePath));}for(let m of f)if(!u.has(m)){ln(s,e,m);try{await A.unlink(m),d++;}catch(h){(h.code||"")!=="ENOENT"&&r.warn(`Failed to remove stale incremental asset output for ${g}`,{url:g,outputPath:m,error:String(h)});}}for(let m of st(e,g))o.has(m)||Object.prototype.hasOwnProperty.call(t.assetMap,m)&&(delete t.assetMap[m],l++);}(a>0||d>0||l>0)&&r.info(`Incremental cleanup removed ${a} stale pages, ${d} stale assets, ${l} stale asset mappings`,{removedPages:a,removedAssets:d,removedAssetMappings:l,phase:"incremental-cleanup"});}async function Wi(e,t,n,r){let s=new Set([...Object.values(t.manifest.pages),...Object.values(t.manifest.assets)].map(o=>String(o.outputPath||"").trim()).filter(Boolean).map(o=>S.resolve(o))),i=Object.values(e.postCrawlCopyMap||{}).map(o=>At(String(o||""))).filter(o=>!!o).map(o=>S.resolve(e.outputDir,o)),c=(await Vs(["**/*.html","**/*.htm"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(o=>Pr(S.join(e.outputDir,o),e.outputDir,s,i));for(let o of c){let u=S.join(e.outputDir,o);ln(r,e,u),await A.unlink(u);}c.length>0&&n.info(`Removed ${c.length} orphaned HTML output file(s) that are absent from the current crawl manifest.`,{phase:"incremental-cleanup",source:"manifest-output-reconciliation",removedPages:c.length,samples:c.slice(0,20)});}function $r(e,t,n,r){let s=new Set,i=a=>{let c=a.trim();if(c){if(n){let o=Ce(c,n,"asset",r??"manifest");if(!o)return;s.add(o.toString());return}s.add(c);}};for(let a of Object.values(e.pages))for(let c of a.discoveredAssets||[])i(c);for(let[a,c]of Object.entries(e.assets||{}))t&&c.lastSeenRunId!==t||i(a);return s}function Er(e,t,n){for(let r of t)for(let s of st(e,r))if(n.has(s))return true;return false}function Hi(e,t,n,r,s){let i=new Set(n.rewriteTargets);for(let c of n.changedFiles)i.add(S.resolve(e.outputDir,c));for(let c of Object.values(t.manifest.pages))c.rewriteComplete||i.add(S.resolve(c.outputPath));for(let c of Object.values(t.manifest.assets))c.isText&&!c.rewriteComplete&&i.add(S.resolve(c.outputPath));let a=Ui(r,s);if(a.size===0)return [...i];for(let c of Object.values(t.manifest.pages))Er(e,c.discoveredAssets,a)&&i.add(S.resolve(c.outputPath));for(let[c,o]of Object.entries(t.manifest.assets)){o.isText&&Er(e,o.discoveredAssets,a)&&i.add(S.resolve(o.outputPath));for(let u of st(e,c))if(a.has(u)){te(n,e,o.outputPath),o.isText&&i.add(S.resolve(o.outputPath));break}}return [...i]}function zi(e,t){let n=t?new Set(Array.from(t,s=>S.resolve(s))):null,r=s=>n===null||n.has(S.resolve(s));for(let s of Object.values(e.manifest.pages))r(s.outputPath)&&(s.rewriteComplete=true);for(let s of Object.values(e.manifest.assets)){if(!s.isText){s.rewriteComplete=true;continue}r(s.outputPath)&&(s.rewriteComplete=true);}}function Qi(e,t,n){let r=new Set,s=i=>{if(!i)return;let a=pe(e.outputDir,i);a&&r.add(a);};for(let i of t.donePages)s(he(e,i));for(let i of Object.values(n.manifest.pages))i.lastSeenRunId===n.runId&&s(i.outputPath);for(let i of t.doneAssets)s(qe(e,t.assetMap,i));for(let i of Object.values(n.manifest.assets))i.lastSeenRunId===n.runId&&s(i.outputPath);return r}async function Ji(e,t,n,r,s){let i=[...r.deletedFiles].sort(),a=Qi(e,t,n),c=0;for(let d of [...a].sort()){try{if((await A.stat(S.join(e.outputDir,d))).isFile())continue}catch{}c++,s.warn(`Active output is missing after crawl: ${d}`,{phase:"deploy-plan",file:d,outputPath:S.join(e.outputDir,d)});}if(c>0&&s.warn(`Detected ${c} active output file(s) missing before deploy plan save.`,{phase:"deploy-plan",missingActiveOutputs:c}),i.length===0)return [];let o=[],u=0;for(let d of i){if(!a.has(d)){o.push(d);continue}u++;let l=S.join(e.outputDir,d),g;try{g=(await A.stat(l)).isFile();}catch{g=false;}s.warn(`Deploy plan protected active output from deletion: ${d}`,{phase:"deploy-plan",file:d,outputPath:l,exists:g});}return u>0&&s.info(`Deploy plan skipped ${u} active output deletion(s).`,{phase:"deploy-plan",protectedDeletes:u,deletedFiles:o.length}),o}function st(e,t){let n=new Set([t]);try{let r=new URL(t),i=[new URL(e.sourceOrigin).origin,...Object.keys(e.extraReplacements||{})];if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{i.push(new URL(e.targetOrigin).origin);}catch{}for(let a of i)try{n.add(new URL(a).origin+r.pathname+r.search);}catch{}n.add(r.pathname+r.search),n.add(r.pathname);}catch{}return [...n]}function Vi(e,t){let n=new Set([t]);try{let r=new URL(t),i=[new URL(e.sourceOrigin).origin,...Object.keys(e.extraReplacements||{})];if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{i.push(new URL(e.targetOrigin).origin);}catch{}for(let a of i)try{n.add(new URL(a).origin+r.pathname+r.search);}catch{}n.add(r.pathname+r.search);}catch{}return [...n]}function Qr(e,t){let n=e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/"?e.targetOrigin:"https://relative.invalid/";try{let r=new URL(t,n);return S.join(e.outputDir,wn(r.pathname))}catch{return null}}function Ki(e,t,n){for(let r of Vi(e,n)){let s=t[r];if(!s)continue;let i=Qr(e,s);if(i)return {outputPath:i,publicUrl:s}}return null}function Gi(e,t,n){for(let r of st(e,n)){let s=t[r];if(!s)continue;let i=Qr(e,s);if(i)return i}return null}function qe(e,t,n){let r=Gi(e,t,n);if(r)return r;try{return Et(e,n).originalFilePath}catch{return null}}function Zi(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?S.join(S.resolve(e),"queue-runner-heartbeat.json"):""}async function Mr(e){let t=Zi();if(t)try{let n=await A.readFile(t,"utf8"),r=JSON.parse(n);if(!r||typeof r!="object")return;await A.writeFile(t,JSON.stringify({...r,checkedAt:new Date().toISOString(),status:"running",currentStep:"rewrite",message:e},null,2),"utf8");}catch{}}function Yi(e,t){let n=new Set;for(let r of t.donePages)n.add(S.resolve(he(e,r)));for(let r of t.doneAssets){let s=qe(e,t.assetMap,r);!s||!Pe(s)||n.add(S.resolve(s));}for(let r of t.doneSitemaps){let s=qe(e,t.assetMap,r);!s||!Pe(s)||n.add(S.resolve(s));}return [...n]}var Tt="re:",xt=new Map;function Xi(e){if(!e.startsWith(Tt))return null;let t=xt.get(e);if(t!==void 0)return t||null;let n=e.slice(Tt.length).trim();if(!n)return xt.set(e,false),null;try{let r=new RegExp(n);return xt.set(e,r),r}catch{return console.warn(`[crawl] Ignoring invalid path matcher regex: ${e}`),xt.set(e,false),null}}function hn(e,t){let n=Xi(t);return n?n.test(e):t.startsWith(Tt)?false:e.startsWith(t)}function eo(e,t){return t.find(n=>hn(e,n))}function Jr(e,t){let n=eo(t.pathname,e.blockedPathPrefixes);if(n)return n.startsWith(Tt)?`blocked path regex: ${n}`:`blocked path prefix: ${n}`;let r=e.blockedSearchFragments.find(s=>t.search.includes(s));return r?`blocked search fragment: ${r}`:null}function to(e){let t=new URL(e.sourceOrigin),n=new Set([t.hostname,...e.allowedAssetHosts]);if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{n.add(new URL(e.targetOrigin).hostname);}catch{}for(let r of Object.keys(e.extraReplacements||{}))try{n.add(new URL(r).hostname);}catch{}return n}function no(e,t){return to(e).has(t.hostname)}function ro(e){let t=fe(e.trim()).replace(/\\\//g,"/").replace(/^['"]|['"]$/g,"").trim();return t=t.replace(/[)]+$/g,"").trim(),t=t.replace(/;.*$/g,"").trim(),t}function pn(e){let t=fe(e).replace(/\\\//g,"/");return /[{}]|\bwindow\.|\blocation\.|\bincludes\(|\?\?null|\+|%7B|%7D|%22|<|>|\s/.test(t)||/[()[\]'"]/.test(t)}function so(e){let t=fe(e).replace(/\\\//g,"/").trim(),n=S.extname(t).toLowerCase();return _r.has(n)||Nr.has(n)?false:/^(?:this|[A-Za-z_$][\w$]*)\.[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/.test(t)}function _e(e){return Ii.some(t=>e.pathname.startsWith(t))}function J(e,t,n=e.sourceOrigin,r,s="url",i=false){let a=ro(t);if(!a||a.startsWith("data:")||a.startsWith("blob:")||a.startsWith("mailto:")||a.startsWith("tel:")||a.startsWith("#"))return r?.ignore(s,t,"empty or unsupported scheme",n),null;if(pn(a))return r?.ignore(s,t,"looks like JavaScript/code fragment",n),null;if(so(a))return r?.ignore(s,t,"looks like JavaScript property access",n),null;try{let c=new URL(a,n);if(Vr(c))return r?.reject(s,t,"malformed URL encoding",n),null;if(!["http:","https:"].includes(c.protocol))return r?.ignore(s,t,"unsupported protocol: ",n),null;if(!no(e,c))return r?.ignore(s,c.toString(),"host not allowed",n),null;let o=Jr(e,c);if(o&&!(i&&Vt(c)))return r?.reject(s,c.toString(),o,n),null;let u=new URL(e.sourceOrigin);return c.protocol=u.protocol,c.host=u.host,c.hash="",c}catch{return r?.reject(s,t,"invalid URL",n),null}}function Vr(e){try{return decodeURIComponent(e.pathname),decodeURIComponent(e.search),decodeURIComponent(e.hash),!1}catch{return true}}function Ce(e,t,n,r){try{let s=new URL(e);return Vr(s)?(t.reject(n,e,"malformed URL encoding",r),null):s.protocol!=="http:"&&s.protocol!=="https:"?(t.reject(n,e,`unsupported protocol: ${s.protocol}`,r),null):s}catch{return t.reject(n,e,"invalid URL",r),null}}function Kr(e){return _r.has(S.extname(e.pathname).toLowerCase())}function Re(e){return _e(e)?false:Nr.has(S.extname(e.pathname).toLowerCase())}function Ne(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function W(e,t){return !!(Ne(t)||t.pathname==="/robots.txt"||t.pathname==="/llms.txt"||t.pathname.toLowerCase().endsWith(".xsl")||e.assetPathPrefixes.length>0&&e.assetPathPrefixes.some(n=>hn(t.pathname,n))||Kr(t)||!Re(t)&&!_e(t))}function he(e,t){let n=new URL(t),r=wn(n.pathname);return r.endsWith("/")?r+="index.html":S.extname(r)||(r+="/index.html"),S.join(e.outputDir,r)}function wn(e){try{return decodeURIComponent(e)}catch{return e}}function io(e){if(!e.generated404RequestPath)return null;try{let t=new URL(e.generated404RequestPath,e.sourceOrigin);return !t.pathname.endsWith("/")&&S.extname(t.pathname)===""&&(t.pathname+="/"),t.hash="",t.toString()}catch{return null}}function yn(e,t){let n=io(e);if(!n)return false;try{return new URL(t,e.sourceOrigin).toString()===n}catch{return false}}function Et(e,t){let n=new URL(t),r=wn(n.pathname);r.endsWith("/")&&(r+="index.html");let s=!!n.search,i=r,a=S.join(e.outputDir,i),c=nn(e.targetOrigin,i),o=`${c}${n.search}`;if(!s)return {originalPathname:i,originalFilePath:a,originalPublicUrl:c,originalPublicUrlWithSearch:o,preferQueryHashed:s};let u=S.extname(r),l=`${u?r.slice(0,-u.length):r}.${Ye(n.search)}${u||".bin"}`;return {originalPathname:i,originalFilePath:a,originalPublicUrl:c,originalPublicUrlWithSearch:o,preferQueryHashed:s,hashedPathname:l,hashedFilePath:S.join(e.outputDir,l),hashedPublicUrl:nn(e.targetOrigin,l)}}function oo(e,t,n,r){let s=Ce(t,n,"asset",r);return s?Et(e,s.toString()):null}function ao(e,t,n,r,s){t[n]=r;try{let i=new URL(n),c=[new URL(e.sourceOrigin).origin,...Object.keys(e.extraReplacements||{})];if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{c.push(new URL(e.targetOrigin).origin);}catch{}for(let o of c)try{let u=new URL(o);t[u.origin+i.pathname+i.search]=r;}catch{}try{let o=(()=>{let u=new URL(r,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin);return `${u.pathname}${u.search}`})();t[i.pathname+i.search]=o;}catch{}try{let o=new URL(r,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin).pathname;t[i.pathname]=o;}catch{}try{let o=new URL(s,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin).pathname;t[i.pathname]=o;}catch{}}catch{}}async function Gr(e){try{return await A.readFile(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}async function je(e){try{return await A.stat(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}function co(e){let t=String(e["content-length"]||"").trim();if(!t)return null;let n=Number.parseInt(t,10);return Number.isFinite(n)&&n>=0?n:null}function Zr(e){let t=String(e["last-modified"]||"").trim();if(!t)return null;let n=Date.parse(t);return Number.isFinite(n)?n:null}function uo(e,t){let n=Zr(t),r=co(t),s=e?.mtime.getTime();return n!==null&&Math.abs((s??0)-n)>=1e3||r!==null&&e?.size!==r?false:n!==null||r!==null}async function lo(e,t){if(!t)return;let n=Zr(t);if(n===null)return;let r=await je(e);if(!r)return;let s=new Date(n);await A.utimes(e,r.atime,s).catch(()=>{});}function Dr(e,t){let n=S.extname(e.pathname).toLowerCase(),r=t["content-type"]||"";return xi.has(n)||Ri.some(s=>r.includes(s))&&!r.startsWith("image/")&&!r.includes("font")}function po(e){try{let t=S.extname(new URL(e).pathname).toLowerCase();return t===".js"||t===".mjs"}catch{return false}}function Le(e,t){(e.doneAssets.size%25===0||e.doneAssets.size===1||e.doneAssets.size===e.stats.assetsQueued)&&t.progress(`Asset processing progress: processed ${e.doneAssets.size}, discovered ${e.stats.assetsQueued}.`,{doneAssets:e.doneAssets.size,assetsQueued:e.stats.assetsQueued,phase:"download-assets"});}function go(e){return Ye(e.toString("base64"),40)}function fo(e,t,n){let r=e.assetVariantDigestsByOriginalPath.get(t);if(!r||r.size===0)return false;for(let s of r)if(s!==n)return true;return false}function mo(e,t,n){let r=e.assetVariantDigestsByOriginalPath.get(t);r||(r=new Set,e.assetVariantDigestsByOriginalPath.set(t,r)),r.add(n);}async function gn(e,t,n,r,s,i,a){let c=Et(e,n),o=go(r),u=c.originalFilePath,d=c.originalPublicUrlWithSearch;if(c.preferQueryHashed&&c.hashedFilePath&&c.hashedPublicUrl&&fo(s,c.originalFilePath,o)){let p=Ki(e,t,n);p?(u=p.outputPath,d=p.publicUrl):(u=c.hashedFilePath,d=c.hashedPublicUrl);}ao(e,t,n,d,c.originalPublicUrl);let l=Pe(u)?Buffer.from(on(r.toString("utf8"),e,t,u),"utf8"):r,g=await Gr(u),f=false;return (g===null||!g.equals(l))&&(await Xe(u,l),f=true),i?.debug(`${f?"Saved":"Skipped unchanged"} asset output ${n}`,{phase:"download-assets",url:n,outputPath:u,bytes:l.length,wroteFile:f}),mo(s,c.originalFilePath,o),await lo(u,a),s.stats.assetsSaved++,i&&(s.stats.assetsSaved%50===0||s.stats.assetsSaved===1)&&i.progress(`Asset progress: saved ${s.stats.assetsSaved}, discovered ${s.stats.assetsQueued}, pending ${s.assetQueue.length}.`,{assetsSaved:s.stats.assetsSaved,assetsQueued:s.stats.assetsQueued,assetQueue:s.assetQueue.length,phase:"download-assets"}),{outputPath:u,publicUrl:d,wroteFile:f}}function it(e,t,n,r,s){let i=()=>{let l=String(s??"").trim();if(!l)return {discoveredFrom:"",discoveredFromType:"unknown"};if(l==="seed-path"||l==="cli"||l==="generated-404-request-path")return {discoveredFrom:l,discoveredFromType:l};if(/^https?:\/\//i.test(l))try{let g=new URL(l);return Ne(g)?{discoveredFrom:l,discoveredFromType:"sitemap"}:W(e,g)?{discoveredFrom:l,discoveredFromType:"asset"}:Re(g)&&!_e(g)?{discoveredFrom:l,discoveredFromType:"page"}:{discoveredFrom:l,discoveredFromType:"url"}}catch{return {discoveredFrom:l,discoveredFromType:"url"}}return {discoveredFrom:l,discoveredFromType:"url"}},a=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,c=J(e,n,a,r,"page");if(!c)return;if(!Re(c)||Kr(c)){if(W(e,c)){r.info(`Seeded/non-page URL queued as asset: ${c.toString()}`,{url:c.toString(),source:s}),Q(e,t,c.toString(),r,s);return}r.reject("page",c.toString(),_e(c)?"asset/internal path cannot be page":"not page-like",s);return}let o=c.toString(),{discoveredFrom:u,discoveredFromType:d}=i();u&&!t.pageDiscoverySources.has(o)&&t.pageDiscoverySources.set(o,u),!t.donePages.has(o)&&!t.queuedPages.has(o)&&(t.queuedPages.add(o),t.pageQueue.push(o),t.stats.pagesQueued++,r.debug(`Queued discovered page ${o}`,{phase:"discovery",url:o,discoveredFrom:u,discoveredFromType:d,outputPath:he(e,o)}),(t.stats.pagesQueued%25===0||t.stats.pagesQueued===1)&&r.progress(`Discovery progress: pages ${t.stats.pagesQueued}, assets ${t.stats.assetsQueued}, sitemaps ${t.stats.sitemapsQueued}.`,{pagesQueued:t.stats.pagesQueued,assetsQueued:t.stats.assetsQueued,sitemapsQueued:t.stats.sitemapsQueued,pageQueue:t.pageQueue.length,phase:"discovery"}));}function Q(e,t,n,r,s){let i=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,a=J(e,n,i,r,"asset");if(!a)return;if(!W(e,a)){r.reject("asset",a.toString(),"not a safe asset path/prefix",s);return}let c=a.toString();!t.doneAssets.has(c)&&!t.queuedAssets.has(c)&&(t.queuedAssets.add(c),t.assetQueue.push(c),t.stats.assetsQueued++,(t.stats.assetsQueued%100===0||t.stats.assetsQueued===1)&&r.progress(`Discovery progress: pages ${t.stats.pagesQueued}, assets ${t.stats.assetsQueued}, sitemaps ${t.stats.sitemapsQueued}.`,{pagesQueued:t.stats.pagesQueued,assetsQueued:t.stats.assetsQueued,sitemapsQueued:t.stats.sitemapsQueued,assetQueue:t.assetQueue.length,phase:"discovery"}));}function kt(e,t,n,r,s){let i=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,a=J(e,n,i,r,"sitemap",Jn(s));if(!a)return;if(!Ne(a)){r.reject("sitemap",a.toString(),"not sitemap-like",s);return}let c=a.toString();!t.doneSitemaps.has(c)&&!t.queuedSitemaps.has(c)&&(t.queuedSitemaps.add(c),t.sitemapQueue.push(c),t.stats.sitemapsQueued++,(t.stats.sitemapsQueued%10===0||t.stats.sitemapsQueued===1)&&r.progress(`Discovery progress: pages ${t.stats.pagesQueued}, assets ${t.stats.assetsQueued}, sitemaps ${t.stats.sitemapsQueued}.`,{pagesQueued:t.stats.pagesQueued,assetsQueued:t.stats.assetsQueued,sitemapsQueued:t.stats.sitemapsQueued,sitemapQueue:t.sitemapQueue.length,phase:"discovery"}));}function ho(e){let t=fe(e),n=[...t.matchAll(/<url>\s*([\s\S]*?)\s*<\/url>/gi)].map(r=>r[1]);return n.length>0?n.map(r=>{let s=r.match(/<loc>\s*([^<]+?)\s*<\/loc>/i);if(!s?.[1])return null;let i=r.match(/<lastmod>\s*([^<]+?)\s*<\/lastmod>/i);return {loc:s[1].trim(),lastmod:rt(i?.[1]??null)}}).filter(r=>!!r):[...t.matchAll(/<loc>\s*([^<]+?)\s*<\/loc>/gi)].map(r=>({loc:r[1].trim(),lastmod:null})).filter(r=>r.loc)}function wo(e){return e.split(",").map(t=>t.trim().split(/\s+/)[0]).filter(Boolean)}function yo(e){let t=[],n=/url\(\s*(?:"([^"]+)"|'([^']+)'|([^)]*?))\s*\)/gi;for(let r of e.matchAll(n)){let s=e.slice(Math.max(0,r.index-8),r.index);if(/\bnew\s+$/i.test(s))continue;let i=(r[1]||r[2]||r[3]||"").trim();i&&t.push(i);}return t}function So(e){return e.replace(/<script\b[^>]*>[\s\S]*?<\/script\s*>/gi,"")}function bo(e,t){if(t==="dom-style")return true;let n=(()=>{try{return S.extname(new URL(e).pathname).toLowerCase()}catch{return ""}})();return !(n===".js"||n===".mjs"||n===".map"||t.includes("javascript"))}function At(e){return e.trim().replace(/\\/g,"/").replace(/^https?:\/\/[^/]+/i,"").replace(/^\/+/,"")}async function Yr(e,t,n,r){let s=[];await A.mkdir(t,{recursive:true});let i=await A.readdir(e,{withFileTypes:true});for(let a of i){let c=S.join(e,a.name),o=S.join(t,a.name);if(a.isDirectory()){s.push(...await Yr(c,o,n,r));continue}await Xr(c,o,n,r)&&s.push(o);}return s}async function Po(e,t){try{let[n,r]=await Promise.all([A.stat(e),A.stat(t)]);if(n.size!==r.size)return !1;let[s,i]=await Promise.all([A.readFile(e),A.readFile(t)]);return s.equals(i)}catch{return false}}async function Xr(e,t,n,r){let s=S.resolve(t);return n.has(s)?(r.debug("Skipped post-crawl copy over crawled page output",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):await Po(e,t)?(r.debug("Skipped unchanged post-crawl copy target",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):(await A.mkdir(S.dirname(t),{recursive:true}),await A.copyFile(e,t),true)}async function vo(e,t,n){let r=Object.entries(e.postCrawlCopyMap||{}).map(([c,o])=>({sourcePath:c.trim(),prefix:String(o||"").trim()}));if(r.length===0)return [];let s=0,i=[],a=new Set([...t.donePages].map(c=>S.resolve(he(e,c))));n.mark("copy-extra-paths");for(let c of r){let o=c.sourcePath,u=c.prefix;if(!o||!u){n.warn("Skipped post-crawl copy mapping with empty key/value",{sourcePath:o,prefixPath:u});continue}let d=Ei(o);if(!d.resolvedPath){n.warn("Skipped post-crawl copy source because alias root is not configured",{sourcePath:o,alias:d.alias||"",requiredEnv:d.requiredEnv||"",prefixPath:u});continue}let l=d.resolvedPath,g=At(u);if(!g){n.warn("Skipped post-crawl copy mapping with invalid export prefix",{sourcePath:o,prefixPath:u});continue}let f;try{f=await A.stat(l);}catch{n.warn("Skipped post-crawl copy source because it does not exist",{sourcePath:o,sourceAbs:l,prefixPath:u});continue}let p=S.resolve(e.outputDir,g);if(f.isDirectory()){let w=await Yr(l,p,a,n);i.push(...w),s+=w.length,w.length>0&&n.progress(`Copied static directory to export: ${l} -> /${g}`,{phase:"copy-extra-paths",sourcePath:l,targetPath:`/${g}`,copiedItems:s,changedFiles:w.length});continue}let y=S.basename(l),m=u.endsWith("/")?S.join(p,y):p;await Xr(l,m,a,n)&&(i.push(m),s++,n.progress(`Copied static file to export: ${l} -> /${At(u.endsWith("/")?`${u}${y}`:u)}`,{phase:"copy-extra-paths",sourcePath:l,targetPath:`/${At(u.endsWith("/")?`${u}${y}`:u)}`,copiedItems:s}));}return n.endMark("copy-extra-paths",{mappedSources:r.length,copiedItems:s}),i}function ot(e,t,n,r,s){let i=new Set,a=fe(n).replace(/\\\//g,"/"),c=s==="page-html"?So(a):a,o=bo(t,s);if(o){for(let u of yo(c)){let d=J(e,u,t,r,`${s}:css-url`);d&&W(e,d)&&i.add(d.toString());}for(let u of c.matchAll(/@import\s+(?:url\()?\s*['"]?([^'"\s;]+)['"]?\s*\)?/gi)){let d=J(e,u[1],t,r,`${s}:css-import`);d&&W(e,d)&&i.add(d.toString());}}if(o||s==="page-html")for(let u of c.matchAll(/<\?xml-stylesheet[^>]+href=["']([^"']+)["'][^>]*\?>/gi)){let d=J(e,u[1],t,r,`${s}:xml-stylesheet`);d&&W(e,d)&&i.add(d.toString());}if(s==="page-html")for(let u of yr(a)){let d=u.startsWith("//")?`${new URL(e.sourceOrigin).protocol}${u}`:u;if(pn(d)){r.ignore(`${s}:serialized-url`,d,"looks like JavaScript/code fragment",t);continue}let l=J(e,d,t,r,`${s}:serialized-url`);l&&W(e,l)?i.add(l.toString()):l&&r.ignore("asset",l.toString(),"serialized URL did not pass safe asset path filter",t);}if(s!=="page-html"&&po(t))for(let u of Sr(a)){if(pn(u)||!/\.(?:js|mjs)(?:[?#]|$)/i.test(u))continue;let d=J(e,u,t,r,`${s}:relative-asset`);d&&W(e,d)&&i.add(d.toString());}return [...i]}function Co(e,t,n,r){let s=new Set,i=fe(n);for(let a of i.matchAll(/href=["']([^"'#\s][^"']*?)["']/gi)){let c=a[1],o=J(e,c,t,r,"page-link");o&&(Jr(e,o)||Re(o)&&!_e(o)&&!W(e,o)&&s.add(o.toString()));}return [...s]}async function Or(e,t,n,r,s,i,a=false,c=[]){let o=new Set(c),u=0,d=new Map,l=(g,f)=>{let p=[f],y=new Set;for(;p.length>0;){let m=p.pop();if(m===g)return true;y.has(m)||(y.add(m),p.push(...d.get(m)??[]));}return false};for(;n.sitemapQueue.length>0;){let g=n.sitemapQueue.shift();n.queuedSitemaps.delete(g);let f=Ce(g,r,"sitemap","sitemap-queue");if(!f)continue;let p=f.toString();if(n.doneSitemaps.has(p))continue;n.doneSitemaps.add(p),r.sitemap(`Fetching sitemap ${p}`,{url:p});let y=true;try{let m=await t.get(p,{timeout:6e4});if(y=!1,!m.ok()){if(r.skip("sitemap",p,m.status()),a&&!o.has(p))throw new Error(`Required child content-sync sitemap returned HTTP ${m.status()}: ${p}`);continue}let h=await m.body(),w=m.url()||p,P=Ce(w,r,"sitemap-response",p);if(!P)continue;let x=P.toString();n.successfulSitemaps.add(p),n.successfulSitemaps.add(x),o.has(p)&&(n.resolvedSitemapRoots.add(x),u+=1);let j=await gn(e,n.assetMap,x,h,n,r);te(s,e,j.outputPath),Ie(s,j.outputPath);let M=h.toString("utf8");/<sitemapindex(?:\s|>)/i.test(M)?n.sitemapIndexUrls.add(x):n.sitemapLeafUrls.add(x);let _=ot(e,x,M,r,"sitemap");for(let $ of _)Q(e,n,$,r,x);let C=[];for(let $ of ho(M)){let R=J(e,$.loc,x,r,"sitemap-loc",!0);if(!R){if(a)throw new Error(`Content-sync sitemap contains an inadmissible location: ${$.loc}`);continue}if(Ne(R)){if(l(x,R.toString()))throw new Error(`Sitemap index cycle detected: ${x} -> ${R.toString()}`);if(d.has(x)||d.set(x,new Set),d.get(x).add(R.toString()),C.push(R.toString()),a&&n.doneSitemaps.has(R.toString())&&!n.successfulSitemaps.has(R.toString()))throw new Error(`Required child content-sync sitemap was not resolved: ${R.toString()}`);kt(e,n,R.toString(),r,x);}else if(a){if(Re(R)&&!W(e,R)){let U=i.requiredUrls.has(R.toString()),b=i.manifest.pages[R.toString()],T=!!b&&!!await je(b.outputPath);if(!U&&!T)throw new Error(`Content-sync sitemap coverage is incomplete for ${R.toString()}.`)}r.ignore("sitemap-loc",R.toString(),"content-sync sitemap-only traversal does not enqueue urlset locations",x);}else W(e,R)?Q(e,n,R.toString(),r,x):Re(R)?($.lastmod&&(n.sitemapLastmodByPage[R.toString()]=$.lastmod),it(e,n,R.toString(),r,x)):r.reject("sitemap-loc",R.toString(),"not page/sitemap/asset-like",x);}i.trackManifest&&(i.manifest.assets[x]={url:x,outputPath:j.outputPath,isText:!0,contentType:m.headers()["content-type"]??"application/xml",changeToken:null,tokenSource:null,discoveredAssets:[...new Set([..._,...C])],lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,unreferencedRuns:0,rewriteComplete:!1},await ve(e,i));}catch(m){r.error(`Failed sitemap ${p}`,{url:p,error:String(m)});let h=a&&o.has(p)&&y;if(a&&!h)throw m}}if(a&&o.size>0&&u===0)throw new Error(`None of the configured content-sync sitemap roots could be resolved: ${[...o].join(", ")}`)}async function Ro(e,t,n,r,s,i,a){let c=J(e,r,e.sourceOrigin,s,"asset-fetch");if(!c||!W(e,c))return;let o=c.toString();if(n.doneAssets.has(o))return;let u=i.enabled||i.strictTargeted?i.previousManifest.assets[o]:void 0,d=i.trackManifest?await It(e,t,i,o,s):null,l=d?.token??null,g=vr(u?.changeToken,d),f=i.strictTargeted&&(!d?.supported||!d.token)&&Cr(e.sourceOrigin,o),p=i.strictTargeted?i.manifest.assets[o]:void 0;if(p?.lastSeenRunId===i.runId&&await je(p.outputPath)){n.doneAssets.add(o);for(let m of p.discoveredAssets||[])Q(e,n,m,s,o);te(a,e,p.outputPath),p.isText&&Ie(a,p.outputPath),s.info(`Resumed completed content-sync asset ${o}`,{url:o,source:"content-sync-checkpoint"}),Le(n,s);return}if(u&&(g||f)&&await je(u.outputPath)){i.manifest.assets[o]={...u,changeToken:l,tokenSource:d?.tokenSource??u.tokenSource,discoveredAssets:[...u.discoveredAssets||[]],lastSeenRunId:i.runId,unreferencedRuns:0},delete i.manifest.pages[o],n.doneAssets.add(o);for(let m of u.discoveredAssets||[])Q(e,n,m,s,o);s.info(`Reused unchanged trusted asset ${o}.`,{url:o,tokenSource:d?.tokenSource??"unknown",phase:"download-assets",source:g?"asset-change-token-match":"content-sync-release-fingerprint"}),Le(n,s);return}let y=(m,h={})=>{if(!u||i.strictTargeted)return false;i.manifest.assets[o]={...u,discoveredAssets:[...u.discoveredAssets||[]],lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,unreferencedRuns:0},delete i.manifest.pages[o],n.doneAssets.add(o);for(let w of u.discoveredAssets||[])Q(e,n,w,s,o);return s.warn(`Keeping previous incremental asset ${o} after fetch issue.`,{url:o,reason:m,preservedFromPreviousManifest:true,...h,phase:"download-assets"}),Le(n,s),true};s.asset(`Fetching asset ${o}`,{url:o});try{let m=qe(e,n.assetMap,o),h=m?await je(m):null;if(m&&h)try{let U=await H(`fetch asset headers ${o}`,6e4,()=>t.head(o,{timeout:6e4})),b=U.headers();if(U.ok()&&uo(h,b)){let T=await Gr(m);if(T!==null){let O=U.url()||o,q=Ce(O,s,"asset-response",o);if(!q)return;let v=q.toString(),E=await gn(e,n.assetMap,v,T,n,s,b);E.wroteFile&&(te(a,e,E.outputPath),Pe(E.outputPath)&&Ie(a,E.outputPath)),n.doneAssets.add(o),s.info(`Reused existing asset ${o} based on response headers.`,{url:o,filePath:m,lastModified:b["last-modified"]||"",contentLength:b["content-length"]||"",phase:"download-assets"}),Le(n,s);let F=Dr(c,b),z=u?.discoveredAssets?[...u.discoveredAssets]:[];if(F||u?.isText){let ee=T.toString("utf8"),Te=ot(e,v,ee,s,"asset:cached");(Te.length>0||!u)&&(z=Te);for(let D of z)Q(e,n,D,s,v);}if(i.trackManifest){let ee=F||!!u?.isText;i.manifest.assets[o]={url:o,outputPath:E.outputPath,isText:ee,contentType:un(b)||u?.contentType||null,changeToken:d?.supported?d.token:null,tokenSource:d?.supported?d.tokenSource??null:null,discoveredAssets:z,lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,unreferencedRuns:0,rewriteComplete:!ee},delete i.manifest.pages[o],(i.enabled||i.strictTargeted)&&ee&&await ve(e,i);}return}}}catch{}let w=await H(`fetch asset ${o}`,6e4,()=>t.get(o,{timeout:6e4}));if(!w.ok()){if(y(`http-${w.status()}`,{status:w.status()}))return;if(i.strictTargeted)throw new Error(`Required content-sync asset returned HTTP ${w.status()}: ${o}`);s.skip("asset",o,w.status());return}let P=await H(`read asset response ${o}`,6e4,()=>w.body()),x=w.url()||o,j=Ce(x,s,"asset-response",o);if(!j)return;let M=j.toString(),_=w.headers(),C=await gn(e,n.assetMap,M,P,n,s,_);C.wroteFile&&(te(a,e,C.outputPath),Pe(C.outputPath)&&Ie(a,C.outputPath)),n.doneAssets.add(o),Le(n,s);let $=Dr(c,_),R=[];if($){let U=P.toString("utf8");R=ot(e,M,U,s,`asset:${S.extname(c.pathname).toLowerCase()||_["content-type"]||"unknown"}`);for(let b of R)Q(e,n,b,s,M);}i.trackManifest&&(i.manifest.assets[o]={url:o,outputPath:C.outputPath,isText:$,contentType:un(_),changeToken:d?.supported?d.token:null,tokenSource:d?.supported?d.tokenSource??null:null,discoveredAssets:R,lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,unreferencedRuns:0,rewriteComplete:!$},delete i.manifest.pages[o],(i.enabled||i.strictTargeted)&&$&&await ve(e,i));}catch(m){if(y("fetch-error",{error:String(m)}))return;if(s.error(`Failed asset ${o}`,{url:o,error:String(m)}),i.strictTargeted)throw m}finally{n.doneAssets.has(o)||(n.doneAssets.add(o),Le(n,s));}}async function xo(e,t,n,r,s,i){let a=Math.max(1,Number(e.assetDownloadConcurrency||e.concurrency||1)),c=Array.from({length:a},async()=>{for(;n.assetQueue.length>0;){let o=n.assetQueue.shift();o&&(n.queuedAssets.delete(o),await Ro(e,t,n,o,r,s,i));}});await Promise.all(c);}function Io(e){let t=Math.ceil(e/1e3);return `${t} second${t===1?"":"s"}`}function me(e){return Math.max(6e4,e.navigationTimeoutMs*2,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+15e3)}function nt(e){return Math.max(15e3,Math.min(6e4,me(e)))}function Ao(e){return Math.max(15e3,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+1e4)}function Sn(e){return e.stats.pagesCompleted}function fn(e){let n=e.options?.browserProcess;return {pid:typeof n?.process?.pid=="number"?n.process.pid:null,kill:typeof n?.kill=="function"?n.kill.bind(n):null}}async function cn(e,t,n){let r=fn(e);try{if(r.kill){await r.kill(),t.warn(`Force-killed crawl worker browser after ${n}`,{browserPid:r.pid,reason:n});return}if(r.pid!==null){process.kill(r.pid,"SIGKILL"),t.warn(`Sent SIGKILL to crawl worker browser after ${n}`,{browserPid:r.pid,reason:n});return}t.warn(`Could not resolve crawl worker browser PID after ${n}`,{reason:n});}catch(s){t.error(`Failed to force-kill crawl worker browser after ${n}`,{browserPid:r.pid,reason:n,error:String(s)});}}async function H(e,t,n){let r;try{return await Promise.race([n(),new Promise((s,i)=>{r=globalThis.setTimeout(()=>{i(new at(e,t));},t);})])}finally{r!==void 0&&globalThis.clearTimeout(r);}}var at=class extends Error{constructor(n,r){super(`${n} timed out after ${Io(r)}.`);this.operation=n;this.timeoutMs=r;this.name="OperationTimeoutError";}operation;timeoutMs};async function To(e,t){let n=Ao(e);await t.evaluate(async r=>{await new Promise(s=>{let i=0,a=700,c=Date.now(),o=()=>{window.clearInterval(u),window.scrollTo(0,0),s();},u=window.setInterval(()=>{if(Date.now()-c>=r.maxDurationMs){o();return}window.scrollBy(0,a),i+=a;let d=document.body?.scrollHeight||0,l=document.documentElement?.scrollHeight||0,g=Math.max(d,l);i>=g+window.innerHeight&&o();},120);});},{maxDurationMs:n});}async function ko(e,t){await t.waitForLoadState("domcontentloaded",{timeout:e.readiness.timeoutMs}).catch(()=>{}),e.readiness.waitForSelector&&await t.waitForSelector(e.readiness.waitForSelector,{timeout:e.readiness.timeoutMs}).catch(()=>{}),e.readiness.waitForFunction&&await t.waitForFunction(e.readiness.waitForFunction,void 0,{timeout:e.readiness.timeoutMs}).catch(()=>{}),await To(e,t),await t.waitForTimeout(e.readiness.fallbackWaitMs);}async function $o(e,t){await t.waitForLoadState("domcontentloaded",{timeout:e.readiness.timeoutMs}).catch(()=>{}),await t.waitForLoadState("load",{timeout:e.readiness.timeoutMs}).catch(()=>{}),e.readiness.waitForSelector&&await t.waitForSelector(e.readiness.waitForSelector,{timeout:e.readiness.timeoutMs}).catch(()=>{}),await t.waitForTimeout(e.readiness.fallbackWaitMs);}function Eo(e,t){return e.noJavaScriptRenderPathPrefixes.some(n=>hn(t,n))}async function Mo(e,t,n,r,s=true){let i=new Set;if(s){let c=await t.evaluate(()=>{let o=new Set,u=["href","src","poster","data-src","data-lazy-src","data-original","data-bg","data-background","data-href"],d=new Set,l=["srcset","data-srcset","data-lazy-srcset"],g=f=>{f.querySelectorAll("*").forEach(p=>{for(let m of u){let h=p.getAttribute(m);h&&o.add(h);}for(let m of l){let h=p.getAttribute(m);h&&h.split(",").forEach(w=>{let P=w.trim().split(/\s+/)[0];P&&o.add(P);});}let y=p.getAttribute("style");y&&d.add(y),p.shadowRoot&&g(p.shadowRoot);});};return g(document),{attrs:[...o],styles:[...d]}});for(let o of c.attrs)for(let u of wo(o)){let d=J(e,u,n,r,"dom-attr");d&&W(e,d)&&i.add(d.toString());}for(let o of c.styles)for(let u of ot(e,n,o,r,"dom-style"))i.add(u);}let a=await t.content();for(let c of ot(e,n,a,r,"page-html"))i.add(c);return [...i]}async function Fr(e,t,n,r,s){let i=he(e,t);await Xe(i,n,"utf8");let a=await A.stat(i);s?.debug(`Saved page output ${t}`,{phase:"save-pages",url:t,outputPath:i,bytes:a.size}),r.stats.pagesSaved++;let c=Math.min(r.stats.pagesQueued,Sn(r)+1);s&&(r.stats.pagesSaved%10===0||r.stats.pagesSaved===1)&&s.progress(`Page progress: processed ${c}, rendered ${r.stats.pagesRendered}, saved ${r.stats.pagesSaved}, discovered ${r.stats.pagesQueued}.`,{donePages:c,claimedPages:r.donePages.size,pagesRendered:r.stats.pagesRendered,pagesSaved:r.stats.pagesSaved,pagesQueued:r.stats.pagesQueued,pageQueue:r.pageQueue.length,phase:"save-pages"});}function Do(e){let t=Sn(e);return t%5===0||t===1||t===e.stats.pagesQueued}function Oo(e,t){let n=Sn(e);t.progress(`Render progress: processed ${n}, rendered ${e.stats.pagesRendered}, saved ${e.stats.pagesSaved}, discovered ${e.stats.pagesQueued}.`,{donePages:n,claimedPages:e.donePages.size,pagesRendered:e.stats.pagesRendered,pagesSaved:e.stats.pagesSaved,pagesQueued:e.stats.pagesQueued,phase:"render-pages"});}async function Fo(e){await e.addInitScript(()=>{Object.defineProperty(window,"__WPSUITE_STATIC_EXPORT__",{value:true,writable:false,configurable:true});let t=n=>{if(n==null||String(n)==="")return true;try{let r=new URL(String(n),window.location.href),s=new URL(window.location.href);return r.hash="",s.hash="",r.href===s.href}catch{return false}};try{let n=window.location.assign.bind(window.location),r=window.location.replace.bind(window.location),s=window.location.reload.bind(window.location);Object.defineProperty(window.location,"assign",{configurable:!0,value:i=>{if(t(i)){console.warn("[smartcloud-static-publisher] blocked same-page location.assign",i??"");return}return n(i)}}),Object.defineProperty(window.location,"replace",{configurable:!0,value:i=>{if(t(i)){console.warn("[smartcloud-static-publisher] blocked same-page location.replace",i??"");return}return r(i)}}),Object.defineProperty(window.location,"reload",{configurable:!0,value:()=>{console.warn("[smartcloud-static-publisher] blocked location.reload");}});}catch{}});}async function Uo(e,t,n){let r=await e.newContext({viewport:t.viewport,userAgent:"WPSuiteStaticPublisher/0.8 Playwright SitemapOnly",ignoreHTTPSErrors:t.ignoreHttpsErrors,...n?{javaScriptEnabled:false}:{}});return n||await Fo(r),r}async function Lo(e,t,n,r,s,i,a,c){let o=await chromium.launch({headless:true}),u=fn(o);u.pid!==null&&n.debug(`Launched crawl worker browser PID ${u.pid}`,{browserPid:u.pid});try{for(;t.pageQueue.length>0;){o||(o=await chromium.launch({headless:!0}),u=fn(o),u.pid!==null&&n.debug(`Relaunched crawl worker browser PID ${u.pid}`,{browserPid:u.pid}));let d=o;if(e.maxPages>0&&t.donePages.size>=e.maxPages)break;let l=t.pageQueue.shift();if(!l)break;t.queuedPages.delete(l);let g=null,f=null,p=!1,y=[],m=new Set;try{let h=Ce(l,n,"page","page-queue");if(!h)continue;let w=h.toString();if(l=w,t.queuedPages.delete(l),t.donePages.has(l))continue;t.donePages.add(l),i.enabled&&i.seenPages.add(l);let P=yn(e,l),x=Eo(e,h.pathname);if(!Re(h)||W(e,h)||_e(h)){if(W(e,h)){n.info(`Worker redirected non-page URL to asset queue: ${l}`,{url:l,source:"worker-guard"}),Q(e,t,l,n,"worker-guard");continue}n.reject("page",l,"guard rejected non-page URL before rendering","worker");continue}let j=i.strictTargeted?i.manifest.pages[l]:void 0;if(j?.lastSeenRunId===i.runId&&await je(j.outputPath)){i.seenPages.add(l);for(let v of j.discoveredAssets||[])Q(e,t,v,n,l);te(a,e,j.outputPath),Ie(a,j.outputPath),n.info(`Resumed completed content-sync page ${l}`,{url:l,source:"content-sync-checkpoint"});continue}let M=await _i(e,s,t,i,l,n);if(i.enabled&&r!=="single-url"&&r!=="content-sync"){let v=i.manifest.pages[l];if(M.action==="reuse"&&v){for(let E of v.discoveredAssets)Q(e,t,E,n,l);for(let E of v.discoveredPages)it(e,t,E,n,l);v.lastSeenRunId=i.runId,v.sitemapLastmod=rt(t.sitemapLastmodByPage[l])??v.sitemapLastmod,M.changeToken?.supported&&(v.changeToken=M.changeToken.token,v.tokenSource=M.changeToken.tokenSource??v.tokenSource),n.info(`Incremental reuse skipped unchanged page ${l}`,{url:l,mode:"incremental",reason:M.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}g=await H(`create page context ${l}`,me(e),()=>Uo(d,e,x)),f=await H(`create page ${l}`,me(e),()=>g.newPage()),y=await g.cookies();let _=!1,C=new Set;await f.route("**/*",async v=>{let E=v.request();try{let F=J(e,E.url(),e.sourceOrigin,n,"network-request");F&&W(e,F)&&(C.add(F.toString()),Q(e,t,F.toString(),n,l));}catch{}if(E.isNavigationRequest()&&E.frame()===f.mainFrame())try{let F=new URL(E.url()),z=new URL(w);if(F.hash="",z.hash="",F.href===z.href){if(_){n.warn(`Blocked same-page navigation/reload for ${l}`,{url:l,requestUrl:E.url()});try{await v.abort("aborted");}catch(ee){n.debug(`Ignored canceled route while blocking reload for ${l}`,{url:l,error:String(ee)});}return}_=!0;}}catch{}try{await v.continue();}catch(F){n.debug(`Ignored canceled network route for ${l}`,{url:l,requestUrl:E.url(),error:String(F)});}}),f.on("response",v=>{try{let F=J(e,v.url(),e.sourceOrigin,n,"network-response");F&&W(e,F)&&(C.add(F.toString()),Q(e,t,F.toString(),n,l));}catch{}let E=v.headerValues("set-cookie").then(F=>{for(let z of F)c.add(Ir(z,v.url()));}).catch(()=>{}).then(()=>{});m.add(E),E.finally(()=>m.delete(E));}),n.page(`Rendering ${l}`,{url:l});let $=null,R=null;try{if($=await f.goto(l,{waitUntil:"domcontentloaded",timeout:e.navigationTimeoutMs}),$&&$.ok())try{R=await H(`read response body ${l}`,me(e),()=>$.text());}catch{R=null;}}catch(v){n.warn(`Navigation issue for ${l}; saving current DOM if available`,{url:l,error:String(v)});}if(r==="content-sync"&&$){let v=Ce($.url(),n,"content-sync-final-response",l)?.toString();if(v&&v!==w){if(i.deleteOnMissingUrls.has(l)){await mn(e,i,a,l,i.deleteOnMissingUrls),n.info(`Removed stale content-sync archive route redirected to ${v}`,{url:l,finalResponseUrl:v,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${l} -> ${v}`)}}if(P){let v=$?.status()??null;if(v!==404){n.error(`Generated 404 request path returned unexpected status for ${l}`,{url:l,expectedStatus:404,status:v,requestPath:e.generated404RequestPath});continue}n.info(`Capturing rendered 404 page for ${l}`,{url:l,status:v,outputPath:he(e,l)});}else if($&&!$.ok()){if(r==="content-sync"&&($.status()===404||$.status()===410)&&i.deleteOnMissingUrls.has(l)){await mn(e,i,a,l,i.deleteOnMissingUrls),n.info(`Removed stale content-sync archive page ${l}`,{url:l,status:$.status(),source:"content-sync-tombstone"}),await f.close();continue}if(r==="content-sync")throw new Error(`Required content-sync route returned HTTP ${$.status()}: ${l}`);n.skip("page",l,$.status(),t.pageDiscoverySources.get(l)),await f.close();continue}x?(n.info(`Rendering without JS execution for ${l}`,{url:l,mode:"no-js"}),await H(`wait for rendered page ${l}`,me(e),()=>$o(e,f))):await H(`wait for rendered page ${l}`,me(e),()=>ko(e,f)),t.stats.pagesRendered++;let U=await H(`extract rendered assets ${l}`,me(e),()=>Mo(e,f,w,n,!x));for(let v of U)Q(e,t,v,n,l);let b=[...new Set([...U,...C])],T=R??await H(`capture rendered html ${l}`,me(e),()=>f.content()),O=r!=="single-url"&&r!=="content-sync"&&!P?Co(e,l,T,n):[];if(r!=="single-url"&&r!=="content-sync"&&!P)for(let v of O)it(e,t,v,n,l);await Fr(e,l,T,t,n);let q=he(e,l);if(te(a,e,q),Ie(a,q),i.trackManifest){let v=$?.headers();i.manifest.pages[l]={url:l,outputPath:q,contentType:un(v),changeToken:M.changeToken?.supported?M.changeToken.token:null,tokenSource:M.changeToken?.supported?M.changeToken.tokenSource??null:null,sitemapLastmod:rt(t.sitemapLastmodByPage[l])??null,discoveredPages:O,discoveredAssets:b,lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,rewriteComplete:!1},(i.enabled||i.strictTargeted)&&await ve(e,i);}}catch(h){if(p=h instanceof at,i.strictTargeted)throw n.error(`Failed required content-sync page ${l}`,{url:l,error:String(h)}),h;if(p)n.warn(`Skipped partial DOM for timed-out page ${l}`,{url:l,error:String(h),recovery:"recycle-worker-browser"});else try{let w=f?await H(`capture partial html ${l}`,me(e),()=>f.content()):"";if(w&&w.trim()){await Fr(e,l,w,t,n);let P=he(e,l);if(te(a,e,P),Ie(a,P),i.trackManifest){let x=i.manifest.pages[l]??i.previousManifest.pages[l];i.manifest.pages[l]={url:l,outputPath:P,contentType:x?.contentType??null,changeToken:null,tokenSource:null,sitemapLastmod:rt(t.sitemapLastmodByPage[l])??x?.sitemapLastmod??null,discoveredPages:x?.discoveredPages?[...x.discoveredPages]:[],discoveredAssets:x?.discoveredAssets?[...x.discoveredAssets]:[],lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,rewriteComplete:!1},(i.enabled||i.strictTargeted)&&await ve(e,i);}n.warn(`Saved partial DOM for ${l}`,{url:l,error:String(h)});}}catch(w){w instanceof at&&(p=!0),n.error(`Could not save partial DOM for ${l}`,{url:l,error:String(w)});}n.error(`Failed page ${l}`,{url:l,error:String(h)});}finally{if(!p&&f&&await H(`drain network routes ${l}`,nt(e),()=>f.unrouteAll({behavior:"wait"})).catch(h=>{p=!0,n.warn(`Could not drain network routes for ${l}`,{url:l,browserPid:u.pid,error:String(h),recovery:"recycle-worker-browser"});}),p&&(o=null,await cn(d,n,`page operation timeout for ${l}`),await d.close().catch(()=>{})),m.size>0&&await Promise.allSettled([...m]),!p&&g)try{let h=await g.cookies();c.addAll(Ar(y,h));}catch{}t.stats.pagesCompleted++,Do(t)&&Oo(t,n),!p&&f&&await H(`close page ${l}`,nt(e),()=>f.close()).catch(h=>{p=!0,n.warn(`Timed out while closing page ${l}`,{url:l,browserPid:u.pid,error:String(h),recovery:"recycle-worker-browser"});}),!p&&g&&await H(`close page context ${l}`,nt(e),()=>g.close()).catch(h=>{p=!0,n.warn(`Timed out while closing page context for ${l}`,{url:l,browserPid:u.pid,error:String(h),recovery:"recycle-worker-browser"});}),p&&o===d&&(o=null,await cn(d,n,`page cleanup timeout for ${l}`),await d.close().catch(()=>{}));}}}finally{if(o){let d=o;await H("close worker browser",nt(e),()=>d.close()).catch(l=>(n.warn("Timed out while closing crawl worker browser",{browserPid:u.pid,error:String(l)}),cn(d,n,"browser.close timeout")));}}}async function jo(e){try{let t=await A.readFile(e),r=(e.endsWith(".gz")?gunzipSync(t):t).toString("utf8").trim();if(!r)return [];try{let s=JSON.parse(r);if(Array.isArray(s))return s;if(s&&typeof s=="object")return [s]}catch{return r.split(/\r?\n/).map(s=>s.trim()).filter(Boolean).map(s=>{try{let i=JSON.parse(s);return i&&typeof i=="object"?i:null}catch{return null}}).filter(s=>s!==null)}return []}catch{return []}}function Ur(e){let t=String(e||"").toLowerCase();return t.includes("timeout")||t.includes("timed out")||t.includes("navigation timeout")}function qo(e){let t=String(e?.job?.command||"").trim(),n=String(e?.job?.crawlMode||"full").trim();return t!=="publish"&&t!=="crawl"?false:n!=="incremental"}function _o(e){let t=[e?.archivedAt,e?.job?.endedAt,e?.job?.startedAt];for(let n of t){let r=Date.parse(String(n||""));if(Number.isFinite(r))return r}return 0}async function es(e){try{let t=await A.readFile(e,"utf8"),n=JSON.parse(t);return n&&typeof n=="object"&&!Array.isArray(n)?n:null}catch{return null}}function Lr(e){let t=String(e.originalFileName||"").toLowerCase();return t.endsWith("errors.jsonl")?0:t.endsWith("errors.json")?1:2}function No(e,t){let n=Array.isArray(t?.artifacts)?t.artifacts.filter(r=>String(r?.role||"").trim()==="errors"&&String(r?.storedFileName||"").trim()!=="").sort((r,s)=>Lr(r)-Lr(s)).map(r=>S.join(e,String(r.storedFileName))):[];return [...new Set([...n,S.join(e,"errors.jsonl"),S.join(e,"errors.json"),S.join(e,"errors.jsonl.gz"),S.join(e,"errors.json.gz")])]}async function jr(e){let t=await es(S.join(e,"job.json"));for(let n of No(e,t)){let r=await jo(n);if(r.length>0)return r}return []}async function Bo(e){let t=S.join(e.logDir,"archive"),n;try{n=await A.readdir(t);}catch{return ""}let r="",s=0;for(let i of n){let a=S.join(t,i);if(!(await A.stat(a).catch(()=>null))?.isDirectory())continue;let o=await es(S.join(a,"job.json"));if(!qo(o))continue;let u=_o(o);u>=s&&(s=u,r=a);}return r}async function Wo(e){let t=await Bo(e),n=t!==""?await jr(t):await jr(e.logDir),r=new Set;for(let s of n)if(Ur(s.error)||Ur(s.message)){let i=s.url;typeof i=="string"&&i&&r.add(i);}return [...r]}async function Ho(e){let t=[];for(let n of e)if(n.startsWith("@")){let r=n.slice(1),s=await A.readFile(r,"utf8");t.push(...s.split(/\r?\n/).map(i=>i.trim()).filter(i=>i&&!i.startsWith("#")));}else t.push(n);return t}async function zo(e){if(!e)throw new Error("Content-sync crawl requires an immutable impact plan.");let t=JSON.parse(await A.readFile(e,"utf8"));return Ve(t),t}function Qo(e,t){let n;try{n=new URL(e);}catch{return false}return t.archiveFamilies.some(r=>{let s=new URL(r.url);if(n.origin!==s.origin)return false;if(n.toString()===s.toString())return true;if(n.searchParams.has("paged")&&n.pathname===s.pathname)return /^\d+$/.test(n.searchParams.get("paged")||"");let i=s.pathname.replace(/\/+$/,""),a=n.pathname.slice(i.length).replace(/^\/+/,""),c=String(r.paginationBase||"page").replace(/^\/+|\/+$/g,"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return n.pathname.startsWith(`${i}/`)&&new RegExp(`^${c}/\\d+/?$`).test(a)})}function tt(e,t,n){let r=n==="page"?he(e,t):Et(e,t).originalFilePath;return pe(e.outputDir,r)}async function mn(e,t,n,r,s){let i=t.manifest.pages[r];if(!i)return;let a=pe(e.outputDir,i.outputPath);if(!a)throw new Error(`Manifest-owned page path escaped output root: ${r}`);let c=S.resolve(i.outputPath);Object.entries(t.manifest.pages).some(([u,d])=>u!==r&&!s.has(u)&&S.resolve(d.outputPath)===c)||(await A.rm(i.outputPath,{force:true}),n.deletedFiles.add(a)),delete t.manifest.pages[r];}async function Jo(e,t,n,r,s){let i=t.manifest.assets[r];if(!i)return;let a=pe(e.outputDir,i.outputPath);if(!a)throw new Error(`Manifest-owned resource path escaped output root: ${r}`);let c=S.resolve(i.outputPath);Object.entries(t.manifest.assets).some(([u,d])=>u!==r&&!s.has(u)&&S.resolve(d.outputPath)===c)||(await A.rm(i.outputPath,{force:true}),n.deletedFiles.add(a)),delete t.manifest.assets[r];}function Vo(e,t){let n=new Set,r=[...t];for(;r.length>0;){let s=r.shift();if(n.has(s))continue;n.add(s);let i=e.assets[s];for(let a of i?.discoveredAssets??[])try{Ne(new URL(a))&&r.push(a);}catch{}}return n}async function Ko(e,t,n,r,s){let i=Vo(n.previousManifest,s);for(let a of i){if(t.successfulSitemaps.has(a))continue;let c=n.previousManifest.assets[a];if(!c)continue;let o=pe(e.outputDir,c.outputPath);if(!o)throw new Error(`Manifest-owned sitemap path escaped output root: ${a}`);await A.rm(c.outputPath,{force:true}),r.deletedFiles.add(o),delete n.manifest.assets[a];}await ve(e,n);}async function Go(e){try{let t=await A.readFile(S.join(e.outputDir,"asset-map.json"),"utf8");return JSON.parse(t)}catch{return {}}}async function Zo(e){let t=["crawl.log.jsonl","current-crawl-event.json","rejected.jsonl","ignored.jsonl","skipped-http.jsonl","errors.jsonl","timings.jsonl","rejected.json","ignored.json","skipped-http.json","errors.json","timings.json"];await A.mkdir(e,{recursive:true}),await Promise.all(t.map(async n=>{try{await A.unlink(S.join(e,n));}catch(r){if(r.code!=="ENOENT")throw r}}));}function qr(e,t,n,r,s){let i=J(e,n,e.sourceOrigin,r,"manual-url");i&&(Ne(i)?kt(e,t,i.toString(),r,s):W(e,i)?Q(e,t,i.toString(),r,s):Re(i)?it(e,t,i.toString(),r,s):r.reject("manual-url",i.toString(),"not page/sitemap/asset-like",s));}async function ts(e=process.argv.slice(2)){let t=[],n=await oe(void 0,{onRemotePublisherConfigDiagnostic:b=>{t.push(b);}}),r=Ge(n.sitemapPaths),s=t.at(-1)??null,i=Ai(e),a=i.mode==="content-sync"?await zo(i.contentSyncPlanPath):null;if(i.resumeRewrite&&i.mode!=="full")throw new Error("--resume-rewrite is only supported for full crawl/publish jobs.");let c=[];i.mode==="retry-timeouts"&&(c=await Wo(n)),i.mode==="single-url"&&(c=await Ho(i.urls)),a&&(c=[...a.directRenderUrls,...a.archiveFamilyUrls,...a.resourceRenderUrls]);let o=i.crawlMode==="incremental"&&i.mode==="full",u=o&&typeof n.subscriptionType=="string"&&n.subscriptionType.trim()!=="";if(a&&n.subscriptionType!=="PROFESSIONAL"&&n.subscriptionType!=="AGENCY")throw new Error("Content-sync requires an active Professional or Agency subscription.");let d=(i.mode==="full"||i.mode==="single-url"||i.mode==="content-sync")&&!i.resumeRewrite,l=i.mode!=="full"||i.resumeRewrite||u,g=l?await Go(n):{},f=u||i.mode==="single-url"||i.mode==="content-sync"?await Oi(n):Wr(),p=new Set(c),y=new Set;if(a){for(let b of a.archiveFamilies)if(b.foundPosts===0)for(let T of b.pageUrls)y.add(T);for(let b of Object.keys(f.pages))Qo(b,a)&&!p.has(b)&&(c.push(b),y.add(b));}let m={enabled:u,trackManifest:d,manifest:dn(f),previousManifest:dn(f),runId:a?.jobId??`${Date.now()}`,seenPages:new Set,changeTokenCache:new Map,manifestSaveChain:Promise.resolve(),strictTargeted:!!a,requiredUrls:p,deleteOnMissingUrls:y};await Zo(n.logDir);let h=new Pt(n.logDir,n.logLevel);if(s){let b={status:s.status,virtualAssetBaseUrl:s.virtualAssetBaseUrl,subscriptionType:s.subscriptionType??null,error:s.error,requests:s.requests,source:"remote-publisher-config"};s.status==="loaded"&&s.subscriptionType?h.info("Loaded remote WP Suite publisher configuration.",b):h.warn("Remote WP Suite publisher configuration is unavailable or has no active subscription type.",b);}if(o&&!u){let b="Incremental crawl requires an active remote publisher subscription; refusing to fall back to a destructive full crawl.";throw h.error(b,{requestedMode:i.crawlMode,subscriptionType:n.subscriptionType??null,source:"incremental-subscription-guard"}),await h.flush(),new Error(b)}let w={pageQueue:[],queuedPages:new Set,donePages:new Set,pageDiscoverySources:new Map,assetQueue:[],queuedAssets:new Set,doneAssets:new Set,sitemapQueue:[],queuedSitemaps:new Set,doneSitemaps:new Set,successfulSitemaps:new Set,resolvedSitemapRoots:new Set,sitemapIndexUrls:new Set,sitemapLeafUrls:new Set,sitemapLastmodByPage:{},assetMap:{...g},assetVariantDigestsByOriginalPath:new Map,stats:{pagesQueued:0,pagesRendered:0,pagesCompleted:0,assetsQueued:0,sitemapsQueued:0,assetsSaved:0,pagesSaved:0}},P=Di(),x=new Ct,j=Ti();if(j&&!i.resumeRewrite&&await Rt(j,x.artifact()),await De(n),await nr(n,{startedAt:new Date().toISOString(),outputDir:S.resolve(n.outputDir)}),l||await A.rm(n.outputDir,{recursive:true,force:true}),await A.mkdir(n.outputDir,{recursive:true}),a){let b=new Set([...a.deleteUrls,...m.deleteOnMissingUrls]);for(let O of a.deleteUrls)await mn(n,m,P,O,b);let T=new Set(a.resourceDeleteUrls);for(let O of a.resourceDeleteUrls)await Jo(n,m,P,O,T);}if(i.resumeRewrite)h.info("Resuming final rewrite from existing output.",{phase:"rewrite-text",mode:i.mode,crawlMode:i.crawlMode});else {let b=await request.newContext({ignoreHTTPSErrors:n.ignoreHttpsErrors});try{if(h.mark("discovery"),i.mode==="full"?(r.forEach(O=>kt(n,w,O,h)),await Or(n,b,w,h,P,m),n.seedPaths.forEach(O=>qr(n,w,O,h,"seed-path")),n.generated404RequestPath&&it(n,w,n.generated404RequestPath,h,"generated-404-request-path")):(c.forEach(O=>qr(n,w,O,h,"cli")),a?.sitemapUrls.forEach(O=>kt(n,w,O,h,"content-sync-impact")),await Or(n,b,w,h,P,m,i.mode==="content-sync",a?.sitemapUrls??[]),a&&await Ko(n,w,m,P,a.sitemapUrls)),h.endMark("discovery",{pages:w.pageQueue.length,assets:w.assetQueue.length,sitemaps:w.sitemapQueue.length}),h.summary(`Queued ${w.pageQueue.length} pages, ${w.assetQueue.length} assets, ${w.sitemapQueue.length} sitemaps.`,{mode:i.mode,crawlMode:i.crawlMode,queuedPages:w.pageQueue.length,queuedAssets:w.assetQueue.length,queuedSitemaps:w.sitemapQueue.length}),i.mode==="retry-timeouts"&&w.pageQueue.length===0&&w.assetQueue.length===0&&w.sitemapQueue.length===0){h.summary("No timed-out URLs were queued. Skipping retry crawl.",{mode:i.mode,crawlMode:i.crawlMode,queuedPages:0,queuedAssets:0,queuedSitemaps:0}),await h.flush(),j&&await Rt(j,x.artifact());return}h.mark("render-pages");let T=Array.from({length:Math.max(1,n.concurrency)},()=>Lo(n,w,h,i.mode,b,m,P,x));if(await Promise.all(T),j&&await Rt(j,x.artifact()),h.endMark("render-pages",{pages:w.stats.pagesCompleted}),h.mark("download-assets"),h.progress("Asset download phase started.",{phase:"download-assets",doneAssets:w.doneAssets.size,assetsQueued:w.stats.assetsQueued,assetQueue:w.assetQueue.length}),await Li(n,b,m,[...w.assetQueue],h),await xo(n,b,w,h,m,P),h.endMark("download-assets",{assets:w.doneAssets.size}),m.enabled&&n.maxPages===0&&(await Bi(n,w,m,h,P),i.mode==="full"&&await Wi(n,m,h,P)),i.mode==="full"){let O=await vo(n,w,h);for(let q of O)te(P,n,q);}}finally{await H("dispose crawl request context",nt(n),()=>b.dispose()).catch(T=>{h.warn("Timed out while disposing crawl request context",{error:String(T)});});}}m.trackManifest&&await ve(n,m),await A.writeFile(S.join(n.outputDir,"asset-map.json"),JSON.stringify(w.assetMap,null,2),"utf8"),JSON.stringify(g,null,2)!==JSON.stringify(w.assetMap,null,2)&&te(P,n,S.join(n.outputDir,"asset-map.json"));let M=i.mode==="single-url"||i.mode==="content-sync"?Yi(n,w):m.enabled&&!i.resumeRewrite?Hi(n,m,P,g,w.assetMap):void 0;h.mark("rewrite-text");let _=i.mode==="single-url"||i.mode==="content-sync"?`Rewriting text files touched by URL crawl: 0/${M?.length??0}`:"Rewriting text files...";h.progress(_,{phase:"rewrite-text",index:0,totalFiles:M?.length,changedTextFiles:0}),await Mr(_);let C=Date.now(),$=await yt(n,w.assetMap,{files:M,previousAssetMap:g,onStart:({totalFiles:b,workerCount:T})=>{h.info(`Rewrite phase scanning ${b} text files.`,{phase:"rewrite-text",totalFiles:b,workerCount:T});},onProgress:async({index:b,totalFiles:T,changedTextFiles:O,file:q,changed:v})=>{v&&te(P,n,S.join(n.outputDir,q)),h.checkpoint(`Rewriting text file ${b}/${T}`,{phase:"rewrite-text",index:b,totalFiles:T,changedTextFiles:O,file:q}),h.debug(`${v?"Rewrote":"Checked unchanged"} text file ${b}/${T}: ${q}`,{phase:"rewrite-text",index:b,totalFiles:T,changedTextFiles:O,file:q,changed:v});let E=Date.now();if(!(b===1||b===T||b%50===0||E-C>=5e3))return;C=E;let z=`Rewriting text files: ${b}/${T}`;h.progress(z,{phase:"rewrite-text",index:b,totalFiles:T,changedTextFiles:O,file:q}),await Mr(z);}});m.trackManifest&&(zi(m,M),await ve(n,m)),h.endMark("rewrite-text",{changedTextFiles:$});let R=a?[...P.deletedFiles].sort():m.enabled&&!i.resumeRewrite?await Ji(n,w,m,P,h):[],U=[...new Set((M||[]).map(b=>pe(n.outputDir,S.isAbsolute(b)?b:S.join(n.outputDir,b))).filter(b=>b!==null))].sort();if(a){let b=[...P.changedFiles].sort(),T=new Set(b),O=new Set(a.directRenderUrls.map(D=>tt(n,D,"page")).filter(D=>!!D&&T.has(D))),q=new Set(a.archiveFamilyUrls.map(D=>tt(n,D,"page")).filter(D=>!!D&&T.has(D))),v=new Set(a.resourceRenderUrls.map(D=>tt(n,D,"asset")).filter(D=>!!D&&T.has(D))),E=new Set([...w.sitemapLeafUrls].map(D=>tt(n,D,"asset")).filter(D=>!!D&&T.has(D))),F=new Set([...w.sitemapIndexUrls].map(D=>tt(n,D,"asset")).filter(D=>!!D&&T.has(D))),z=new Set([...O,...q,...v,...E,...F]),ee=b.filter(D=>!z.has(D)),Te=[...new Set([...a.directRenderUrls,...a.archiveFamilyUrls,...w.resolvedSitemapRoots,...w.sitemapLeafUrls,...w.sitemapIndexUrls,...a.deleteUrls,...a.resourceRenderUrls,...a.resourceDeleteUrls].map(D=>{let ut=new URL(D);return `${ut.pathname}${ut.search}`}))].sort();await tn(n,{schemaVersion:2,generatedAt:new Date().toISOString(),outputDir:S.resolve(n.outputDir),runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,changedFiles:b,deletedFiles:R,rewriteTargets:U,consumerId:a.consumerId,fromSequence:a.fromSequence,toSequence:a.toSequence,impactHash:a.impactHash,resolvedSitemapUrls:[...w.resolvedSitemapRoots],uploadGroups:{assets:ee,content:[...O].sort(),listings:[...q].sort(),resources:[...v].sort(),sitemapLeaves:[...E].sort(),sitemapIndexes:[...F].reverse()},invalidationPaths:Te});}else await tn(n,{schemaVersion:1,generatedAt:new Date().toISOString(),outputDir:S.resolve(n.outputDir),runMode:i.mode,crawlMode:i.crawlMode,fullSyncRequired:!(m.enabled&&!i.resumeRewrite),changedFiles:m.enabled&&!i.resumeRewrite?[...P.changedFiles].sort():[],deletedFiles:m.enabled&&!i.resumeRewrite?R:[],rewriteTargets:m.enabled&&!i.resumeRewrite?U:[]});await sr(n),h.summary(i.resumeRewrite?`Done. Resumed final rewrite over existing output and changed ${$} text files.`:`Done. Rendered ${w.stats.pagesRendered} pages, processed ${w.doneSitemaps.size} sitemaps and ${w.doneAssets.size} assets.`,{mode:i.mode,crawlMode:i.crawlMode,resumeRewrite:i.resumeRewrite,...w.stats,pagesRendered:w.stats.pagesRendered,donePages:w.stats.pagesCompleted,claimedPages:w.donePages.size,doneSitemaps:w.doneSitemaps.size,doneAssets:w.doneAssets.size,changedTextFiles:$}),await h.flush();}function Yo(e){let t=e/6e4;if(Number.isInteger(t)&&t>=1)return `${t} minute${t===1?"":"s"}`;let n=Math.ceil(e/1e3);return `${n} second${n===1?"":"s"}`}async function ne(e,t,n){let r=new AbortController,s=globalThis.setTimeout(()=>{r.abort(new Error(`${e} timed out after ${Yo(t)}.`));},t);try{return await n({abortController:r,httpOptions:{abortSignal:r.signal,requestTimeout:t}})}finally{globalThis.clearTimeout(s);}}function Xo(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Mt(e){let t="";for(let r=0;r<e.length;r++){let s=e[r];if(s==="--profile"){let i=(e[r+1]||"").trim();if(!i)throw new Error("Missing value for --profile.");t=i,r+=1;}else if(s.startsWith("--profile=")){let i=s.slice(10).trim();if(!i)throw new Error("Missing value for --profile.");t=i;}}let n=String(process.env.PUBLISHER_DEPLOY_PROFILE||process.env.PUBLISHER_DEPLOYMENT_PROFILE||"").trim();return t||n||null}function ea(e,t){return {...e,...t??{}}}function ta(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function Be(e,t){let n=String(t??e.deploymentTargetOverride??"").trim();if(!Xo(e)){if(n)throw new Error(`Deployment target "${n}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:e}}let r=String(n||e.defaultDeploymentProfile||"").trim();if(!r)return {name:null,profile:null,config:e};let s=e.deploymentProfiles?.[r];if(!s)throw new Error(`Unknown deployment profile "${r}". Check the linked WPSuite publisher configuration.`);return {name:r,profile:s,config:{...e,targetOrigin:s.targetOrigin??e.targetOrigin,s3:ea(e.s3,s.s3),cloudFront:ta(e.cloudFront,s.cloudFront)}}}var is="wpsuite-sha256",os="wpsuite-normalized-sha256",la=/(?:^|[-_:.])(id|uid|nonce|token|hash|instance)(?:$|[-_:.])/i,xn=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,da=["SIGINT","SIGTERM"],Ft=new Set,Dt=new Map,ns=null;function pa(e){Ft.add(e);let t=false;return ()=>{t||(t=true,Ft.delete(e));}}async function ga(){let e=[...Ft];e.length!==0&&await Promise.allSettled(e.map(async t=>{Ft.delete(t),await A.rm(t,{recursive:true,force:true});}));}function fa(){if(!(Dt.size>0))for(let e of da){let t=()=>{ns||(ns=ga().finally(()=>{for(let[n,r]of Dt)process.off(n,r);Dt.clear(),process.exit(1);}));};Dt.set(e,t),process.on(e,t);}}fa();function ma(e,t){let n=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(n)return S.resolve(n);let r=t.trim();return r?S.join(S.resolve(r),"crawl-manifest.json"):S.join(S.resolve(e),".crawl-manifest.json")}function as(e){return S.normalize(e).split(S.sep).join("/")}function rs(e,t){let n=S.relative(S.resolve(e),S.resolve(t));return !n||n.startsWith("..")||S.isAbsolute(n)?null:as(n)}async function ha(e,t,n){let r=ma(e.outputDir,t),s;try{s=await A.readFile(r,"utf8");}catch(d){return d.code==="ENOENT"?(n.warn("Deploy rewrite guard disabled because the crawl manifest is missing.",{manifestPath:r,source:"deploy-guard"}),null):(n.warn("Deploy rewrite guard disabled because the crawl manifest could not be read.",{manifestPath:r,error:d instanceof Error?d.message:String(d),source:"deploy-guard"}),null)}let i;try{i=JSON.parse(s);}catch(d){return n.warn("Deploy rewrite guard disabled because the crawl manifest is invalid JSON.",{manifestPath:r,error:d instanceof Error?d.message:String(d),source:"deploy-guard"}),null}let a=new Map,c=new Map,o=0,u=0;for(let[d,l]of Object.entries(i.pages||{})){let g=String(l?.outputPath||"").trim(),f=g?rs(e.outputDir,g):null;if(f){let p=typeof l?.contentType=="string"&&l.contentType?l.contentType:null;p&&c.set(f,p);}!g||l?.rewriteComplete===true||f&&(o++,a.has(f)||a.set(f,{kind:"page",sourceUrl:d,outputPath:S.resolve(g),relativePath:f}));}for(let[d,l]of Object.entries(i.assets||{})){let g=String(l?.outputPath||"").trim(),f=l?.isText===true,p=g?rs(e.outputDir,g):null;if(p){let y=typeof l?.contentType=="string"&&l.contentType?l.contentType:null;y&&c.set(p,y);}!g||!f||l?.rewriteComplete===true||p&&(u++,a.has(p)||a.set(p,{kind:"asset",sourceUrl:d,outputPath:S.resolve(g),relativePath:p}));}return {manifestPath:r,byRelativePath:a,contentTypeByRelativePath:c,pendingPages:o,pendingTextAssets:u}}function wa(e){return e&&(xn.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 Pn(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,n)=>n%2===1?t:wa(t)).join("")}function ya(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,n)=>n%2===1?t:xn.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 Ut(e,t){return Array.isArray(e)?e.map(n=>Ut(n,t)):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([n,r])=>[n,Ut(r,n)])):typeof e!="string"?e:t&&la.test(t)?ya(e):xn.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)||/^[0-9]{8,}$/.test(e)?Pn(e):e}function Sa(e){return e.replace(/((?:https?:\/\/[^"'\s>]+|\/)(?:wp-content|wp-includes)\/[^"'\s>?]+)\?ver=[A-Za-z0-9._-]+(?=$|[#"'])/gi,"$1")}function ba(e,t){let n=t.trim();if((n.startsWith("{")||n.startsWith("[")||n.startsWith('"'))&&e.startsWith("data-"))try{let s=JSON.parse(n);return JSON.stringify(Ut(s))}catch{}if(e.startsWith("data-")&&/^[A-Za-z0-9+/=]+$/.test(n))try{let s=Buffer.from(n,"base64").toString("utf8"),i=JSON.parse(s),a=JSON.stringify(Ut(i));return Buffer.from(a,"utf8").toString("base64")}catch{}return t}function Pa(e,t){let n=e.toLowerCase();return n==="class"?t.split(/(\s+)/).map((r,s)=>s%2===1?r:Pn(r)).join(""):n==="id"||n==="for"||n==="aria-controls"||n==="aria-labelledby"||n==="aria-describedby"||n==="aria-owns"||n==="aria-activedescendant"||n==="name"?Pn(t):n==="href"||n==="src"?Sa(t):ba(n,t)}function va(e){return e.replace(/<([a-z][^\s/>]*)([^<>]*?)>/gi,(t,n,r)=>{if(n.startsWith("/"))return t;let s=r.replace(/\s([:@a-zA-Z_][-:.a-zA-Z0-9_]*?)=(['"])([\s\S]*?)\2/g,(i,a,c,o)=>{let u=Pa(a,o);return ` ${a}=${c}${u}${c}`});return `<${n}${s}>`})}function bn(e){let t=S.extname(e).toLowerCase();return [".html",".htm",".xml",".xsl",".txt"].includes(t)||["robots.txt","sitemap.xml"].includes(S.basename(e))}function Ot(e,t){return [e.replace(/^\/+|\/+$/g,""),t.replace(/^\/+/,"")].filter(Boolean).join("/")}async function Ca(e,t,n,r){let s=new Map,i,a=0;do{a++;let c=await ne(`S3 list objects page ${a} for s3://${t}/${n||""}`,3e5,({httpOptions:o})=>e.send(new ListObjectsV2Command({Bucket:t,Prefix:n||void 0,ContinuationToken:i}),o));for(let o of c.Contents||[]){if(!o.Key)continue;let u=(o.ETag||"").replace(/^"|"$/g,"").toLowerCase(),d=Number(o.Size??0);s.set(o.Key,{etag:u,size:d});}i=c.NextContinuationToken,r.debug(`Listed S3 keys page ${a}: ${s.size} keys so far`);}while(i);return s}async function Ra(e,t,n,r){if(r.has(n))return r.get(n)??null;try{let s=await ne(`S3 head object ${n}`,3e5,({httpOptions:a})=>e.send(new HeadObjectCommand({Bucket:t,Key:n}),a)),i={etag:(s.ETag||"").replace(/^"|"$/g,"").toLowerCase(),size:Number(s.ContentLength??0)};return r.set(n,i),i}catch{return r.set(n,null),null}}async function xa(e,t,n,r){let s=[...new Set(Array.from(n).filter(Boolean))];if(s.length===0)return r.info("No explicitly tombstoned S3 objects to delete."),[];r.info(`Deleting ${s.length} explicitly tombstoned S3 object(s)\u2026`);for(let i=0;i<s.length;i+=1e3){let a=s.slice(i,i+1e3);if(a.length===0)continue;let c=await ne(`S3 delete objects batch ${Math.floor(i/1e3)+1}`,3e5,({httpOptions:o})=>e.send(new DeleteObjectsCommand({Bucket:t,Delete:{Objects:a.map(u=>({Key:u}))}}),o));if(c.Errors&&c.Errors.length>0){let o=c.Errors.map(u=>`${u.Key||"(unknown)"}: ${u.Code||"DeleteFailed"}`);throw new Error(`S3 reported ${o.length} object deletion error(s): ${o.join(", ")}`)}r.info(`Deleted batch of ${a.length} object(s).`);for(let o of a)r.debug(` deleted: ${o}`);}return s}function Ia(e){return createHash("md5").update(e).digest("hex")}function ss(e){return createHash("sha256").update(e).digest("hex")}function vn(e){return createHash("sha256").update(va(e.toString("utf8"))).digest("hex")}async function Aa(e,t,n,r){if(r.has(n))return r.get(n)??null;try{let a=((await ne(`S3 head object metadata ${n}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:n}),c))).Metadata?.[is]||"").trim().toLowerCase()||null;return r.set(n,a),a}catch{return r.set(n,null),null}}async function Ta(e,t,n,r,s){if(r.has(n))return r.get(n)??null;try{let a=((await ne(`S3 head object normalized metadata ${n}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:n}),c))).Metadata?.[os]||"").trim().toLowerCase();if(a)return r.set(n,a),a}catch{return r.set(n,null),null}if(s.has(n)){let i=s.get(n)??null;return r.set(n,i),i}try{let a=await(await ne(`S3 get object ${n}`,3e5,({httpOptions:o})=>e.send(new GetObjectCommand({Bucket:t,Key:n}),o))).Body?.transformToByteArray(),c=a?vn(Buffer.from(a)):null;return s.set(n,c),r.set(n,c),c}catch{return s.set(n,null),r.set(n,null),null}}function ka(e){return e.replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"")||"profile"}function $a(e,t){let n=e,r=Object.entries(t).sort((s,i)=>i[0].length-s[0].length);for(let[s,i]of r)s&&(n=n.split(s).join(i));return n}async function Ea(e,t,n,r){let s=S.join(e,"asset-map.json"),i;try{i=await A.readFile(s,"utf8");}catch(o){if(o.code==="ENOENT")return;throw o}let a=JSON.parse(i);if(!a||typeof a!="object")return;let c=Object.fromEntries(Object.entries(a).map(([o,u])=>{let d=String(u??"");return t.urlRewriteMode==="absolute"&&t.targetOrigin&&n.targetOrigin&&t.targetOrigin!==n.targetOrigin&&(d=d.split(t.targetOrigin).join(n.targetOrigin)),d=$a(d,r),[o,d]}));await A.writeFile(s,JSON.stringify(c,null,2),"utf8");}async function Ma(e,t,n,r,s,i){let a=e.targetOrigin!==t.targetOrigin,c=Object.keys(r).length>0;if(!n||!a&&!c)return {outputDir:e.outputDir,cleanup:async()=>{}};if(a&&e.urlRewriteMode!=="absolute")throw new Error(`Deployment profile "${n}" changes targetOrigin, but the base crawl output must use urlRewriteMode "absolute" for multi-target redeploys.`);let o=S.join(S.dirname(e.outputDir),`.deploy-profile-${ka(n)}-${Date.now()}`),u=pa(o);s.info(`Preparing deployment profile "${n}" from existing crawl output to ${o}...`);try{if(await A.rm(o,{recursive:!0,force:!0}),i){await A.mkdir(o,{recursive:!0});for(let f of i){let p=S.join(e.outputDir,f),y=S.join(o,f);await A.mkdir(S.dirname(y),{recursive:!0}),await A.copyFile(p,y);}}else await A.cp(e.outputDir,o,{recursive:!0});let d={...e,sourceOrigin:e.targetOrigin,targetOrigin:t.targetOrigin,extraReplacements:r,outputDir:o},l=1;s.info(`Rewriting text files for deployment profile "${n}"...`);let g=await yt(d,{},{files:i,onStart:({totalFiles:f,workerCount:p})=>{l=p,s.info(`Rewriting ${f} text file(s) for deployment profile "${n}" with ${p} worker(s).`);},onProgress:({index:f,totalFiles:p,changedTextFiles:y,file:m,changed:h})=>{let P=`Rewriting [${f}/${p}] ${m} (${h?"rewrote":"checked"}; ${y} changed so far)`;p<=200||f<=5||f%10===0||f===p?(s.progress(P,{file:m,index:f,totalFiles:p,changed:h,changedTextFiles:y,workerCount:l}),s.info(P)):s.debug(P);}});return await Ea(o,e,t,r),s.info(`Prepared deployment profile "${n}" in ${o} (${g} rewritten text file(s)).`),{outputDir:o,cleanup:async()=>{u(),await A.rm(o,{recursive:!0,force:!0});}}}catch(d){throw s.error(`Failed to prepare deployment profile "${n}" temporary output.`,{tempDir:o,outputDir:e.outputDir,error:d instanceof Error?d.message:String(d),...d instanceof Error&&d.stack?{stack:d.stack}:{}}),await A.rm(o,{recursive:true,force:true}).catch(()=>{}),u(),await s.flush().catch(()=>{}),d}}async function cs(e=process.argv.slice(2)){let t=await oe(),n=Mt(e),r=Be(t,n),s=S.resolve(r.config.logDir||"logs"),i=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",a=i?S.join(S.resolve(i),"deploy-diff.json"):"",c=new Fe(s,"deploy.log.jsonl",r.config.logLevel??"info"),o=r.profile?.extraReplacements??{},u=async()=>{};try{let d=await Me(t);if(e.includes("--content-sync")&&d?.schemaVersion!==2)throw new Error("Content-sync deployment requires a valid schema-2 targeted deploy plan.");let g=await rr(t);if(g)throw new Error(`Deployment blocked: the crawl started at ${g.startedAt} did not complete its final rewrite. Run a successful crawl or resume the rewrite before deploying.`);let f=await Ma(t,r.config,r.name,o,c,d?.schemaVersion===2?d.changedFiles:void 0);u=f.cleanup;let p={...r.config,outputDir:f.outputDir},y=p.s3SyncMode??"sdk-upload-delete";if(d?.schemaVersion===2&&d.deletedFiles.length>0&&y!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let m=d?.schemaVersion!==2&&f.outputDir!==t.outputDir&&(t.targetOrigin!==r.config.targetOrigin||Object.keys(o).length>0),h=!!d&&!d.fullSyncRequired&&!m;if(d?.schemaVersion===2&&!h)throw new Error("Content-sync deployment cannot widen into a full-output deployment.");r.name&&c.info(`Using deployment profile: ${r.name}`),c.info(`Starting deploy \u2014 s3://${p.s3.bucket}/${p.s3.prefix||""} (region: ${p.s3.region})`),c.info(`S3 sync mode: ${y}`),d&&d.fullSyncRequired?c.info("Deploy plan requests a full sync; scanning the whole output tree."):d&&m?c.info("Ignoring incremental deploy plan because deployment-profile rewrites require a full sync."):h&&c.info(`Using incremental deploy plan: ${d.changedFiles.length} changed file(s), ${d.deletedFiles.length} deleted file(s).`),c.mark("deploy");let w=new S3Client({region:p.s3.region}),P=f.outputDir===t.outputDir?await ha(t,i,c):null,x=d?.schemaVersion===2?[...d.uploadGroups.assets,...d.uploadGroups.content,...d.uploadGroups.listings,...d.uploadGroups.resources,...d.uploadGroups.sitemapLeaves,...d.uploadGroups.sitemapIndexes]:d?.changedFiles??[];if(d?.schemaVersion===2){let k=[...new Set(x)],K=[...new Set(d.changedFiles)];if(k.length!==K.length||K.some(I=>!k.includes(I)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let j=h?(await Promise.all(x.map(async k=>{try{return (await A.stat(S.join(p.outputDir,k))).isFile()?k:null}catch{if(d?.schemaVersion===2)throw new Error(`Content-sync planned upload is missing: ${k}`);return null}}))).filter(k=>k!==null):await Vs(["**/*"],{cwd:p.outputDir,onlyFiles:!0,dot:!0}),M=[],_=[],C=j.filter(k=>{let K=P?.byRelativePath.get(as(k));if(!K)return !0;let I=Ot(p.s3.prefix,k);return M.push(k),_.push(I),c.reject(K.kind,K.sourceUrl||k,"rewrite not completed; deploy upload blocked","deploy-guard",{file:k,key:I,outputPath:K.outputPath,manifestPath:P?.manifestPath}),!1}),$=new Set((h?C:j).map(k=>Ot(p.s3.prefix,k)));P&&(P.pendingPages>0||P.pendingTextAssets>0)&&c.info(`Deploy rewrite guard loaded ${P.pendingPages} pending page(s) and ${P.pendingTextAssets} pending text asset(s) from crawl manifest.`,{manifestPath:P.manifestPath,pendingPages:P.pendingPages,pendingTextAssets:P.pendingTextAssets,source:"deploy-guard"}),M.length>0&&c.warn(`Deploy rewrite guard blocked ${M.length} file(s) from upload because final crawl rewrite is incomplete.`,{rejected:M.length,manifestPath:P?.manifestPath,source:"deploy-guard"}),h?c.info(`Using ${C.length} eligible planned changed file(s) from deploy plan for upload${M.length>0?` (${M.length} blocked by deploy rewrite guard)`:""}.`):c.info(`Found ${j.length} file(s) in output directory "${p.outputDir}"${M.length>0?`; ${C.length} remain eligible for upload after deploy rewrite guard filtering`:""}.`),c.progress("Deploy preflight: comparing local output with remote S3 state.",{phase:"deploy",mode:y,totalFiles:C.length,scannedFiles:j.length,rejected:M.length,deploymentProfile:r.name||"",s3Bucket:p.s3.bucket,s3Prefix:p.s3.prefix||"",targetedPlan:h,deployGuardEnabled:f.outputDir===t.outputDir});let R=0,U=0,b=0,T=[],O=[],q=[],v=[],E=[],F=new Map,z=new Map,ee=new Map,Te=new Map,D=new Map;h||(c.info("Listing existing S3 objects to detect unchanged files..."),F=await Ca(w,p.s3.bucket,p.s3.prefix,c),c.info(`Found ${F.size} existing key(s) in S3.`));let ut=async k=>F.has(k)?F.get(k)??null:h?Ra(w,p.s3.bucket,k,z):null;for(let k of C){let K=S.join(p.outputDir,k),I=Ot(p.s3.prefix,k),re=P?.contentTypeByRelativePath.get(k),He=re&&re!=="application/octet-stream"?re:ua.lookup(k)||re||"application/octet-stream",hs=bn(k)?p.s3.htmlCacheControl:p.s3.assetCacheControl,B=R+U+b+1;C.length<=200||B<=5||B%10===0||B===C.length?(c.progress(`Uploading [${B}/${C.length}] ${I} (${He})`,{file:k,key:I,index:B,totalFiles:C.length,contentType:He,mode:y}),c.info(`Uploading [${B}/${C.length}] ${I} (${He})`)):c.debug(`Uploading [${B}/${C.length}] ${I} (${He})`);try{let se=await A.readFile(K),ze=await ut(I),Qe=null,ke=null;if(ze){let qt=se.byteLength;if(ze.size===qt){let lt=ze.etag.includes("-");if(ze.etag!==""&&!lt){let ws=Ia(se);if(ze.etag===ws){U++,C.length<=200||U<=5||U%25===0?(c.progress(`Skipped unchanged [${B}/${C.length}] ${I}`,{file:k,key:I,index:B,totalFiles:C.length,uploaded:R,skipped:U,failed:b,mode:y,reason:"etag-md5-match"}),c.info(`Skipped unchanged [${B}/${C.length}] ${I}`)):c.debug(`Skipped unchanged [${B}/${C.length}] ${I}`),O.push(I);continue}}Qe=ss(se);let En=await Aa(w,p.s3.bucket,I,ee);if(En&&En===Qe){U++,C.length<=200||U<=5||U%25===0?(c.progress(`Skipped unchanged [${B}/${C.length}] ${I}`,{file:k,key:I,index:B,totalFiles:C.length,uploaded:R,skipped:U,failed:b,mode:y,reason:"metadata-sha256-match"}),c.info(`Skipped unchanged [${B}/${C.length}] ${I}`)):c.debug(`Skipped unchanged [${B}/${C.length}] ${I}`),O.push(I);continue}}if(bn(k)){ke=vn(se);let lt=await Ta(w,p.s3.bucket,I,Te,D);if(lt&&lt===ke){U++,C.length<=200||U<=5||U%25===0?(c.progress(`Skipped semantically unchanged [${B}/${C.length}] ${I}`,{file:k,key:I,index:B,totalFiles:C.length,uploaded:R,skipped:U,failed:b,mode:y,reason:"normalized-html-match"}),c.info(`Skipped semantically unchanged [${B}/${C.length}] ${I}`)):c.debug(`Skipped semantically unchanged [${B}/${C.length}] ${I}`),O.push(I);continue}}}Qe=Qe??ss(se),ke=ke??(bn(k)?vn(se):null),await ne(`S3 upload ${I}`,9e5,({abortController:qt})=>new Upload({client:w,abortController:qt,params:{Bucket:p.s3.bucket,Key:I,Body:se,ContentType:He,CacheControl:hs,Metadata:{[is]:Qe,...ke?{[os]:ke}:{}}}}).done()),R++,T.push(I),(R%25===0||R===C.length)&&(c.progress(`Uploaded ${R}/${C.length} file(s)\u2026`,{uploaded:R,skipped:U,failed:b,totalFiles:C.length,mode:y}),c.info(`Uploaded ${R}/${C.length} file(s)\u2026`));}catch(se){b++,q.push(I),c.error(`Failed to upload ${I}: ${se.message}`,{key:I,error:String(se)});}}if(c.info(`Upload complete: ${R} uploaded, ${U} skipped unchanged, ${_.length} rejected by guard, ${b} failed.`),q.length>0||_.length>0)throw new Error(`Deployment stopped before deletion because ${q.length} upload(s) failed and ${_.length} upload(s) were rejected.`);if(y==="sdk-upload-delete"){let k=new Set((d?.deletedFiles??[]).map(I=>Ot(p.s3.prefix,I))),K=[...k].filter(I=>!$.has(I));if(!h&&(E=[...F.keys()].filter(re=>!$.has(re)).filter(re=>!k.has(re)),E.length>0)){c.warn(`Protected ${E.length} remote-only S3 object(s) from deletion because the crawl did not provide explicit tombstones.`,{protected:E.length,source:"deploy-delete-guard"});for(let re of E)c.debug(` protected remote-only object: ${re}`);}v=await xa(w,p.s3.bucket,h?K:K.filter(I=>F.has(I)),c);}else c.info("Sync mode is sdk-upload-only \u2014 skipping stale object deletion.");a&&(await A.mkdir(S.dirname(a),{recursive:!0}),await A.writeFile(a,JSON.stringify({generatedAt:new Date().toISOString(),mode:y,summary:{totalFiles:j.length,eligibleFiles:C.length,uploaded:T.length,skipped:O.length,rejected:_.length,failed:q.length,deleted:v.length,protected:E.length,...r.name?{profile:r.name}:{}},uploadedKeys:T,skippedKeys:O,rejectedKeys:_,failedKeys:q,deletedKeys:v,protectedRemoteOnlyKeys:E},null,2),"utf8")),d?.schemaVersion!==2&&await De(t),c.endMark("deploy"),await c.flush();}finally{await u().catch(()=>{});}}function Oa(e){let t=e;return t.endsWith("/")?t+="index.html":S.posix.basename(t).includes(".")||(t+="/index.html"),t}function Fa(e,t){let n=t.trim().replace(/^\/+|\/+$/g,"");if(!n)return null;let r=`/${n}`;return e===r||e.startsWith(`${r}/`)?e:`${r}${e.startsWith("/")?e:`/${e}`}`}function us(e,t){let n=new Set;for(let r of e){let s=new URL(r,"https://content-sync.invalid");if(s.origin!=="https://content-sync.invalid")continue;let i=`${s.pathname}${s.search}`,a=Oa(s.pathname);n.add(i),n.add(`${a}${s.search}`);let c=Fa(a,t);c&&n.add(`${c}${s.search}`);}return [...n].sort()}function qa(){let e=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();return e?S.join(S.resolve(e),"content-sync-invalidation.json"):""}async function In(e,t){if(!e)return;await A.mkdir(S.dirname(e),{recursive:true});let n=`${e}.${process.pid}.${Date.now()}.tmp`;await A.writeFile(n,JSON.stringify({...t,updatedAt:new Date().toISOString()},null,2),"utf8"),await A.rename(n,e);}async function _a(e,t,n,r){if(!e)return null;try{let s=JSON.parse(await A.readFile(e,"utf8"));if(s.schemaVersion!==1||s.distributionId!==t||s.impactHash!==n||!Array.isArray(s.batches))throw new Error("Content-sync invalidation state does not match the deploy plan.");let i=s.batches.flatMap(a=>a.paths);return i.length!==r.length||i.some((a,c)=>a!==r[c])?null:s}catch(s){if(s.code==="ENOENT")return null;throw s}}function Na(e,t=3e3){let n=[];for(let r=0;r<e.length;r+=t)n.push(e.slice(r,r+t));return n}async function ds(e=process.argv.slice(2)){let t=await oe(),n=Mt(e),r=Be(t,n),s=r.config,i=new Fe(s.logDir||"logs","invalidate.log.jsonl",s.logLevel??"info"),a=e.includes("--content-sync"),c=await Me(t);if(a&&c?.schemaVersion!==2)throw new Error("Content-sync invalidation requires a valid schema-2 targeted deploy plan.");if(r.name&&i.info(`Using deployment profile: ${r.name}`),!s.cloudFront.distributionId){i.info("No CloudFront distributionId configured; skipping invalidation."),await i.flush();return}let o=c?.schemaVersion===2?c.invalidationPaths:s.cloudFront.invalidationPaths.length>0?s.cloudFront.invalidationPaths:["/*"],u=c?.schemaVersion===2?us(o,s.s3.prefix||""):o;if(c?.schemaVersion===2&&u.some(p=>p==="/*"))throw new Error("Content-sync cannot use a whole-distribution invalidation.");if(u.length===0){i.info("The targeted deploy plan has no CloudFront paths to invalidate."),await i.flush();return}let d=s.cloudFront.distributionId,l=new CloudFrontClient({region:"us-east-1",maxAttempts:5});if(i.mark("invalidate"),c?.schemaVersion!==2){let p=`smartcloud-static-publisher-${Date.now()}`;await ne(`CloudFront invalidation for distribution ${d}`,3e5,({httpOptions:y})=>l.send(new CreateInvalidationCommand({DistributionId:d,InvalidationBatch:{CallerReference:p,Paths:{Quantity:u.length,Items:u}}}),y)),i.endMark("invalidate"),await i.flush();return}let g=qa(),f=await _a(g,d,c.impactHash,u);if(!f){let p=createHash("sha256").update(JSON.stringify(u)).digest("hex");f={schemaVersion:1,distributionId:d,impactHash:c.impactHash,updatedAt:new Date().toISOString(),batches:Na(u).map((y,m)=>({index:m,callerReference:`wpsuite-content-sync-${c.impactHash.slice(0,24)}-${p.slice(0,24)}-${m}`,paths:y,invalidationId:null,status:"pending"}))},await In(g,f);}for(let p of f.batches){if(!p.invalidationId){let y=await ne(`CloudFront targeted invalidation batch ${p.index+1}`,3e5,({httpOptions:h})=>l.send(new CreateInvalidationCommand({DistributionId:d,InvalidationBatch:{CallerReference:p.callerReference,Paths:{Quantity:p.paths.length,Items:p.paths}}}),h)),m=String(y.Invalidation?.Id||"").trim();if(!m)throw new Error("CloudFront did not return an invalidation ID.");p.invalidationId=m,p.status="created",await In(g,f);}await waitUntilInvalidationCompleted({client:l,maxWaitTime:900,minDelay:2,maxDelay:15},{DistributionId:d,Id:p.invalidationId}),p.status="completed",await In(g,f),i.info(`CloudFront invalidation batch ${p.index+1}/${f.batches.length} completed.`,{invalidationId:p.invalidationId,pathCount:p.paths.length});}i.endMark("invalidate"),await i.flush();}function jt(e){let t=new URL(e);return t.hash="",t.href}function ps(e,t,n){let r=new Set([...t].map(o=>jt(o))),s=new Map,i=new Map,a=o=>{s.set(jt(o),o);},c=o=>{if(!o)return;let u=jt(o);s.has(u)||i.set(u,o);};for(let o of e.deleteUrls)a(o);for(let o of e.resourceDeleteUrls)a(o);for(let o of e.archiveFamilies)if(o.foundPosts===0)for(let u of o.pageUrls)r.has(jt(u))||a(u);c(e.directRenderUrls[0]),c(e.resourceRenderUrls[0]);for(let o of e.archiveFamilies)c(o.pageUrls[0]),c(o.pageUrls.at(-1));for(let o of n)c(o);return {expectedAvailable:[...i.values()],expectedMissing:[...s.values()]}}var fs=["claimed","impact-planned","rendered","deployed","invalidated","verified","acknowledged"];function kn(){let e=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();if(!e)throw new Error("Content-sync requires a runtime directory.");return S.resolve(e)}function We(e,t){return fs.indexOf(e)>=fs.indexOf(t)}async function ye(e,t,n,r,s={}){let i={timestamp:new Date().toISOString(),eventType:t,status:t.endsWith("failed")?"failed":"success",actorSource:"content-sync",jobId:n.id||"",command:"content-sync",message:r,details:s};await A.mkdir(e,{recursive:true}),await A.appendFile(S.join(e,"audit-events.jsonl"),`${JSON.stringify(i)}
14
- `,"utf8");}function Wa(e){return Je({postTypes:e.postTypes??[],listingPaths:e.listingPaths??[],includeSubsites:e.includeSubsites,includePostTypeArchives:e.includePostTypeArchives,includeTaxonomyArchives:e.includeTaxonomyArchives,includeAuthorArchives:e.includeAuthorArchives,includeDateArchives:e.includeDateArchives,includePostsPage:e.includePostsPage,includeSitemapChain:e.includeSitemapChain})}function Ha(e,t){let n=(e.scheduler?.rules??[]).find(r=>r.command==="content-sync"&&r.id===t);if(!n)throw new Error(`Content-sync scheduler rule not found: ${t}`);if(n.enabled===false)throw new Error(`Content-sync scheduler rule is disabled: ${t}`);return n}async function $n(e,t){if(e.subscriptionType!=="PROFESSIONAL"&&e.subscriptionType!=="AGENCY")throw new Error("Content-sync requires an active Professional or Agency subscription.");let n=Wa(t),r=Be(e,t.deploymentProfile),s=r.name||"",i=On(e.sourceOrigin,String(t.id||""),n),a=Fn({sourceOrigin:e.sourceOrigin,targetOrigin:r.config.targetOrigin,deploymentProfile:s,s3Bucket:r.config.s3.bucket,s3Prefix:r.config.s3.prefix,cloudFrontDistributionId:r.config.cloudFront.distributionId,urlRewriteMode:r.config.urlRewriteMode,extraReplacements:r.profile?.extraReplacements??{}}),c=Un({siteId:e.wpsuite?.siteSettings?.siteId,accountId:e.wpsuite?.siteSettings?.accountId,sourceOrigin:e.sourceOrigin,ruleId:String(t.id||""),targetFingerprint:a}),o=`content-sync:${String(t.id||"")}:${le({scopeFingerprint:i,targetFingerprint:a}).slice(0,32)}`,u=new pt({sourceOrigin:e.sourceOrigin,runtimeToken:String(e.wpsuite?.runtimeToken||""),ignoreHttpsErrors:e.ignoreHttpsErrors}),d=await u.releaseFingerprint(n.includeSubsites),l=Dn({wordpressFingerprint:d,targetFingerprint:a,sourceOrigin:e.sourceOrigin,sitemapPaths:Ge(e.sitemapPaths),blockedPathPrefixes:e.blockedPathPrefixes,assetPathPrefixes:e.assetPathPrefixes,noJavaScriptRenderPathPrefixes:e.noJavaScriptRenderPathPrefixes,urlRewriteMode:e.urlRewriteMode});return {rule:t,scope:n,deployment:r,deploymentProfile:s,scopeFingerprint:i,targetFingerprint:a,consumerId:c,coalesceKey:o,client:u,releaseFingerprint:l}}async function za(e){let t=kn(),n=gt(t),r=await oe(),s=(r.scheduler?.rules??[]).filter(u=>u.command==="content-sync"&&u.enabled!==false);if(s.length===0)return 0;let i=await Ht(n),a=await ft(n),c=await Wt(n),o=0;for(let u of s){let d=await $n(r,u),l=c.entries[d.coalesceKey];if(!l||l.releaseJobId!==e||l.scopeFingerprint!==d.scopeFingerprint||l.targetFingerprint!==d.targetFingerprint||l.releaseFingerprint!==d.releaseFingerprint)throw new Error(`The normal release has no matching pre-crawl content-sync cutoff for rule ${String(u.id||"")}.`);let g=le({releaseJobId:e,releaseFingerprint:d.releaseFingerprint,scopeFingerprint:d.scopeFingerprint,targetFingerprint:d.targetFingerprint,sequence:l.sequence}),f=await d.client.establishBaseline({consumerId:d.consumerId,scopeFingerprint:d.scopeFingerprint,baselineId:g,postTypes:d.scope.postTypes,includeSubsites:d.scope.includeSubsites,sequence:l.sequence}),p={schemaVersion:1,ruleId:String(u.id||""),consumerId:d.consumerId,coalesceKey:d.coalesceKey,scopeFingerprint:d.scopeFingerprint,targetFingerprint:d.targetFingerprint,releaseFingerprint:d.releaseFingerprint,baselineId:g,deploymentProfile:d.deploymentProfile,committedSequence:f.committedSequence,releaseJobId:e,verifiedAt:new Date().toISOString()};i.entries[d.coalesceKey]=p;let y=a.rules[d.coalesceKey];a.rules[d.coalesceKey]={ruleId:String(u.id||""),consumerId:d.consumerId,coalesceKey:d.coalesceKey,committedSequence:f.committedSequence,observedHeadSequence:f.headSequence,lag:Math.max(0,f.headSequence-f.committedSequence),lastSuccessfulJobId:y?.lastSuccessfulJobId||e,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:f.headSequence>f.committedSequence,retryAttempt:0,nextRetryAt:null,lastError:null,baselineStatus:"ready",baselineReason:null};let m=await Qn(n,d.coalesceKey);await ye(t,"content-sync-baseline-established",{id:e},"A normal release established a verified content-sync baseline.",{ruleId:u.id,consumerId:d.consumerId,committedSequence:f.committedSequence,supersededCurrent:m}),o++;}return await Nn(n,i),await mt(n,a),await A.rm(n.releaseCutoff,{force:true}),o}async function Qa(e){let t=kn(),n=gt(t),r=await oe(),s=(r.scheduler?.rules??[]).filter(c=>c.command==="content-sync"&&c.enabled!==false);if(s.length===0)return 0;let i=await Wt(n),a=0;for(let c of s){let o=await $n(r,c),u=Object.values(i.entries).find(g=>g.releaseJobId===e&&g.ruleId===String(c.id||""));if(u){if(u.coalesceKey===o.coalesceKey&&u.scopeFingerprint===o.scopeFingerprint&&u.targetFingerprint===o.targetFingerprint&&u.releaseFingerprint===o.releaseFingerprint){a++;continue}throw new Error(`The normal release scope changed after its journal cutoff was captured for rule ${String(c.id||"")}. Start a new normal publish instead of resuming this release.`)}let d=await o.client.unboundHead(o.scope.postTypes,o.scope.includeSubsites),l={schemaVersion:1,releaseJobId:e,ruleId:String(c.id||""),coalesceKey:o.coalesceKey,scopeFingerprint:o.scopeFingerprint,targetFingerprint:o.targetFingerprint,releaseFingerprint:o.releaseFingerprint,sequence:d.headSequence,capturedAt:new Date().toISOString()};i.entries[o.coalesceKey]=l,await ye(t,"content-sync-release-cutoff-captured",{id:e},"A normal release captured its pre-crawl content-sync journal cutoff.",{ruleId:c.id,consumerId:o.consumerId,sequence:d.headSequence}),a++;}return await _n(n,i),a}async function Ja(e,t){let n=JSON.parse(await A.readFile(S.join(e,"current-run.json"),"utf8"));if(n.id!==t||n.command!=="content-sync"||!String(n.ruleId||"").trim())throw new Error("The active queue job is not the requested content-sync job.");return n}async function Tn(e,t,n=0){let r=e.protocol==="https:"?qn:Rs,s=e.protocol==="https:"?new qn.Agent({rejectUnauthorized:!t}):void 0;return new Promise((i,a)=>{let c=r.get(e,{agent:s,timeout:3e4,headers:{"user-agent":"WP-Suite-Content-Sync/1"}},o=>{let u=o.statusCode??500,d=o.headers.location;if(u>=300&&u<400&&d&&n<5){o.resume(),Tn(new URL(d,e),t,n+1).then(i,a);return}let l=[],g=0;o.on("data",f=>{g+=f.byteLength,g<=1048576&&l.push(f);}),o.on("error",a),o.on("end",()=>i({status:u,body:Buffer.concat(l).toString("utf8")}));});c.on("timeout",()=>c.destroy(new Error(`Verification timed out: ${e}`))),c.on("error",a);})}async function Va(e,t,n,r){let s=t.deployment.config.targetOrigin;if(!/^https?:\/\//i.test(s))throw new Error("Content-sync verification requires an absolute HTTP(S) target origin.");let i=JSON.parse(await A.readFile(r,"utf8")),a=new Set;for(let[u,d]of Object.entries(i.pages??{}))a.add(u),typeof d.url=="string"&&a.add(d.url);let c=await Me(e);if(c?.schemaVersion!==2||c.impactHash!==n.impactHash)throw new Error("Content-sync verification requires the matching targeted deploy plan.");let o=ps(n,a,c.resolvedSitemapUrls);for(let u of o.expectedAvailable){let d=new URL(u),l=new URL(`${d.pathname}${d.search}`,s),g=await Tn(l,e.ignoreHttpsErrors);if(g.status<200||g.status>=400)throw new Error(`Content-sync verification failed (${g.status}): ${l}`)}for(let u of o.expectedMissing.slice(0,10)){let d=new URL(u),l=new URL(`${d.pathname}${d.search}`,s),g=await Tn(l,e.ignoreHttpsErrors);if(g.status>=200&&g.status<300)throw new Error(`Deleted content is still publicly available: ${l}`)}}async function Ka(e){let t=await A.readFile(e.candidateManifest,"utf8");JSON.parse(t);let n=`${e.trustedManifest}.${process.pid}.tmp`;await A.writeFile(n,t,"utf8"),await A.rename(n,e.trustedManifest);}async function Ga(e,t){try{let n=JSON.parse(await A.readFile(e.candidateManifest,"utf8"));if([...Object.values(n.pages??{}),...Object.values(n.assets??{})].some(s=>s?.lastSeenRunId===t))return "resumed"}catch(n){if(n.code!=="ENOENT")throw new Error(`The content-sync candidate manifest is invalid: ${n instanceof Error?n.message:String(n)}`,{cause:n})}return await A.copyFile(e.trustedManifest,e.candidateManifest).catch(n=>{throw new Error(`A trusted crawl manifest is required before content-sync: ${n instanceof Error?n.message:String(n)}`,{cause:n})}),"initialized"}async function Ae(e,t,n,r,s={},i=0,a=async()=>{}){t.phase=n,await Bn(e,t),await zn(e,{schemaVersion:1,jobId:t.jobId,phase:n,completedItems:r,totalItems:r,attempt:i,updatedAt:new Date().toISOString(),details:s}),await ye(S.dirname(e.state),"content-sync-checkpoint",{id:t.jobId},`Content-sync checkpoint completed: ${n}.`,{phase:n,completedItems:r,totalItems:r,attempt:i,...s}),await a(n,t);}async function Za(e,t={}){let n={loadConfig:oe,resolveRuleContext:$n,runCrawl:ts,runDeploy:cs,runInvalidate:ds,verifyDeployment:Va,promoteCandidateManifest:Ka,clearDeployPlan:De,afterCheckpoint:async()=>{},...t},r=kn(),s=gt(r),i=await Ja(r,e),a=await n.loadConfig(),c=Ha(a,String(i.ruleId)),o=await n.resolveRuleContext(a,c);if(i.coalesceKey&&i.coalesceKey!==o.coalesceKey)throw new Error("Queued content-sync demand no longer matches the active rule scope.");let d=(await Ht(s)).entries[o.coalesceKey];if(!d||d.scopeFingerprint!==o.scopeFingerprint||d.targetFingerprint!==o.targetFingerprint||d.releaseFingerprint!==o.releaseFingerprint)throw await ye(r,"content-sync-baseline-rejected",i,"Content-sync requires a new verified full or incremental release baseline.",{ruleId:c.id,reason:d?"mismatch":"missing"}),new Error("Content-sync baseline is missing or stale. Run a successful normal publish first.");let l=await zt(s),g=await o.client.head({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:d.baselineId,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites});if(l&&l.jobId!==i.id)throw new Error("Another immutable content-sync range is still active.");if(l||(l={schemaVersion:1,jobId:i.id,ruleId:String(c.id||""),consumerId:o.consumerId,coalesceKey:o.coalesceKey,scopeFingerprint:o.scopeFingerprint,targetFingerprint:o.targetFingerprint,baselineId:d.baselineId,fromSequence:g.committedSequence,toSequence:g.headSequence,phase:"claimed",impactHash:null,startedAt:new Date().toISOString(),updatedAt:new Date().toISOString()},await Ae(s,l,"claimed",0,{fromSequence:l.fromSequence,toSequence:l.toSequence},0,n.afterCheckpoint),await ye(r,"content-sync-range-claimed",i,"Content-sync claimed a closed journal range.",{fromSequence:l.fromSequence,toSequence:l.toSequence})),l.fromSequence===l.toSequence){let h=await ft(s);h.rules[o.coalesceKey]={ruleId:String(c.id||""),consumerId:o.consumerId,coalesceKey:o.coalesceKey,committedSequence:l.toSequence,observedHeadSequence:g.headSequence,lag:Math.max(0,g.headSequence-l.toSequence),lastSuccessfulJobId:i.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:false,retryAttempt:0,nextRetryAt:null,lastError:null},await mt(s,h),await Qt(s);return}let f=await Wn(s);if(!We(l.phase,"impact-planned")){let h=await o.client.events({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:d.baselineId,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites,afterSequence:l.fromSequence,throughSequence:l.toSequence}),w=Bt({events:h,scope:o.scope,sourceOrigin:a.sourceOrigin}),P=await o.client.resolveImpactFamilies(w,o.scope.includeSubsites);f=jn({jobId:i.id,ruleId:String(c.id||""),consumerId:o.consumerId,coalesceKey:o.coalesceKey,scopeFingerprint:o.scopeFingerprint,targetFingerprint:o.targetFingerprint,baselineId:d.baselineId,sourceOrigin:a.sourceOrigin,scope:o.scope,fromSequence:l.fromSequence,toSequence:l.toSequence,events:h,resolvedArchiveFamilies:P,sitemapUrls:Ge(a.sitemapPaths)}),await Hn(s,f),l.impactHash=f.impactHash,await Ae(s,l,"impact-planned",h.length,{eventCount:h.length,foldedCount:f.foldedCount,impactHash:f.impactHash},0,n.afterCheckpoint),await ye(r,"content-sync-impact-planned",i,"Content-sync persisted an immutable impact plan.",{impactHash:f.impactHash,eventCount:h.length});}if(!f||f.jobId!==i.id||l.impactHash!==f.impactHash)throw new Error("The immutable content-sync impact plan is missing or mismatched.");if(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH=s.candidateManifest,!We(l.phase,"rendered")){let h=await Ga(s,i.id);await ye(r,h==="resumed"?"content-sync-render-resumed":"content-sync-render-initialized",i,h==="resumed"?"Content-sync resumed its candidate crawl manifest.":"Content-sync initialized a candidate crawl manifest.",{impactHash:f.impactHash}),await n.runCrawl(["--content-sync-plan",s.impact]),await Ae(s,l,"rendered",f.directRenderUrls.length+f.archiveFamilyUrls.length+f.resourceRenderUrls.length,{impactHash:f.impactHash},i.attempt??0,n.afterCheckpoint);}let p=o.deploymentProfile?["--profile",o.deploymentProfile]:[];if(We(l.phase,"deployed")||(await n.runDeploy([...p,"--content-sync"]),await Ae(s,l,"deployed",1,{impactHash:f.impactHash},0,n.afterCheckpoint),await ye(r,"content-sync-deployed",i,"Content-sync targeted deployment completed.",{impactHash:f.impactHash})),We(l.phase,"invalidated")?We(l.phase,"verified")||await n.runInvalidate([...p,"--content-sync"]):(await n.runInvalidate([...p,"--content-sync"]),await Ae(s,l,"invalidated",1,{impactHash:f.impactHash},0,n.afterCheckpoint)),We(l.phase,"verified")||(await n.verifyDeployment(a,o,f,s.candidateManifest),await n.promoteCandidateManifest(s),await Ae(s,l,"verified",1,{impactHash:f.impactHash},0,n.afterCheckpoint)),g=await o.client.head({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:d.baselineId,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites}),g.committedSequence===l.fromSequence)await o.client.acknowledge({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:d.baselineId,expectedSequence:l.fromSequence,sequence:l.toSequence,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites});else if(g.committedSequence!==l.toSequence)throw new Error("Content-sync acknowledgement cursor is in an unexpected state.");await Ae(s,l,"acknowledged",1,{sequence:l.toSequence},0,n.afterCheckpoint),await ye(r,"content-sync-cursor-acknowledged",i,"Content-sync committed its journal cursor after verification.",{sequence:l.toSequence});let y=await o.client.head({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:d.baselineId,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites}),m=await ft(s);m.rules[o.coalesceKey]={ruleId:String(c.id||""),consumerId:o.consumerId,coalesceKey:o.coalesceKey,committedSequence:l.toSequence,observedHeadSequence:y.headSequence,lag:Math.max(0,y.headSequence-l.toSequence),lastSuccessfulJobId:i.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:y.headSequence>l.toSequence,retryAttempt:0,nextRetryAt:null,lastError:null},await mt(s,m),await n.clearDeployPlan(a),await A.rm(S.join(r,"content-sync-invalidation.json"),{force:true}),await A.rm(s.candidateManifest,{force:true}),await Qt(s);}async function ms(e=process.argv.slice(2)){let n=e.indexOf("--capture-baseline-cutoffs");if(n>=0){let a=String(e[n+1]||"").trim();if(!a)throw new Error("--capture-baseline-cutoffs requires a release job ID.");await Qa(a);return}let r=e.indexOf("--establish-baselines");if(r>=0){let a=String(e[r+1]||"").trim();if(!a)throw new Error("--establish-baselines requires a release job ID.");await za(a);return}let s=e.indexOf("--job-id"),i=s>=0?String(e[s+1]||"").trim():"";if(!i)throw new Error("Content-sync requires --job-id.");await Za(i);}ms().catch(e=>{console.error(e instanceof Error?e.message:String(e)),process.exit(1);});
13
+ ${i}`,40);return r.info(`Using generated 404 fallback change token for ${n}`,{url:n,requestPath:e.generated404RequestPath,source:"generated-404-change-token-fallback"}),{url:n,supported:!0,token:a,tokenSource:"generated-404-response-hash",postId:null,dependencyPostIds:[],reason:null}}catch(s){return r.warn(`Generated 404 fallback token lookup failed for ${n}`,{url:n,error:String(s),requestPath:e.generated404RequestPath,source:"generated-404-change-token-fallback"}),null}}async function Gi(e,t,n,r,s,i){let a=xn(e,s);if(!r.enabled)return {action:"render",changeToken:r.trackManifest?await $t(e,t,r,s,i):null};let c=r.manifest.pages[s];if(!c)return {action:"render",changeToken:await $t(e,t,r,s,i)};let o=await $t(e,t,r,s,i);if(o?.supported&&o.token)return c.changeToken===o.token?a&&!await Zi(e,t,s,i)?{action:"render",changeToken:o}:{action:"reuse",changeToken:o}:{action:"render",changeToken:o};let l=at(n.sitemapLastmodByPage[s]);return l&&c.sitemapLastmod&&c.sitemapLastmod===l?{action:"reuse",changeToken:o}:{action:"render",changeToken:o}}async function Zi(e,t,n,r){try{let i=(await t.get(n,{timeout:Math.min(e.navigationTimeoutMs,15e3),failOnStatusCode:!1})).status();return i===404?!0:(r.warn(`Generated 404 request path no longer returns 404 for ${n}`,{url:n,status:i,requestPath:e.generated404RequestPath,source:"generated-404-status-probe"}),!1)}catch(s){return r.warn(`Generated 404 status probe failed for ${n}`,{url:n,error:String(s),requestPath:e.generated404RequestPath,source:"generated-404-status-probe"}),false}}async function Yi(e,t,n,r,s){let i=jr(n.previousManifest,void 0,r,"previous-manifest"),a=0;for(let[g,f]of Object.entries(n.manifest.pages))if(!n.seenPages.has(g)){yn(s,e,f.outputPath);try{await A.unlink(f.outputPath);}catch(p){(p.code||"")!=="ENOENT"&&r.warn(`Failed to remove stale incremental page output for ${g}`,{url:g,outputPath:f.outputPath,error:String(p)});}delete n.manifest.pages[g],a++;}let c=jr(n.manifest,n.runId,r,"active-manifest"),o=new Set,l=new Set;for(let g of c){for(let y of ct(e,g))o.add(y);let f=n.manifest.assets[g]?.outputPath;f&&l.add(f);let p=Ne(e,t.assetMap,g);p&&l.add(p);}for(let g of Object.keys(n.manifest.assets)){if(c.has(g)){n.manifest.assets[g].unreferencedRuns=0;continue}let f=n.manifest.assets[g];if(Mr(f.unreferencedRuns)){f.unreferencedRuns+=1,r.warn(`Deferred deletion of newly unreferenced incremental asset ${g}.`,{url:g,unreferencedRuns:f.unreferencedRuns,phase:"incremental-cleanup",source:"asset-deletion-grace"});continue}delete n.manifest.assets[g];}let d=0,u=0;for(let g of i){if(n.manifest.assets[g])continue;let f=new Set,p=n.previousManifest.assets[g]?.outputPath;p&&f.add(p);let y=Ne(e,t.assetMap,g);if(!c.has(g)&&y)f.add(y);else if(!c.has(g)){let m=wo(e,g,r,"incremental-cleanup");m&&(f.add(m.originalFilePath),m.hashedFilePath&&f.add(m.hashedFilePath));}for(let m of f)if(!l.has(m)){yn(s,e,m);try{await A.unlink(m),d++;}catch(h){(h.code||"")!=="ENOENT"&&r.warn(`Failed to remove stale incremental asset output for ${g}`,{url:g,outputPath:m,error:String(h)});}}for(let m of ct(e,g))o.has(m)||Object.prototype.hasOwnProperty.call(t.assetMap,m)&&(delete t.assetMap[m],u++);}(a>0||d>0||u>0)&&r.info(`Incremental cleanup removed ${a} stale pages, ${d} stale assets, ${u} stale asset mappings`,{removedPages:a,removedAssets:d,removedAssetMappings:u,phase:"incremental-cleanup"});}async function Xi(e,t,n,r){let s=new Set([...Object.values(t.manifest.pages),...Object.values(t.manifest.assets)].map(o=>String(o.outputPath||"").trim()).filter(Boolean).map(o=>b.resolve(o))),i=Object.values(e.postCrawlCopyMap||{}).map(o=>Et(String(o||""))).filter(o=>!!o).map(o=>b.resolve(e.outputDir,o)),c=(await ri(["**/*.html","**/*.htm"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(o=>kr(b.join(e.outputDir,o),e.outputDir,s,i));for(let o of c){let l=b.join(e.outputDir,o);yn(r,e,l),await A.unlink(l);}c.length>0&&n.info(`Removed ${c.length} orphaned HTML output file(s) that are absent from the current crawl manifest.`,{phase:"incremental-cleanup",source:"manifest-output-reconciliation",removedPages:c.length,samples:c.slice(0,20)});}function jr(e,t,n,r){let s=new Set,i=a=>{let c=a.trim();if(c){if(n){let o=Ce(c,n,"asset",r??"manifest");if(!o)return;s.add(o.toString());return}s.add(c);}};for(let a of Object.values(e.pages))for(let c of a.discoveredAssets||[])i(c);for(let[a,c]of Object.entries(e.assets||{}))t&&c.lastSeenRunId!==t||i(a);return s}function qr(e,t,n){for(let r of t)for(let s of ct(e,r))if(n.has(s))return true;return false}function eo(e,t,n,r,s){let i=new Set(n.rewriteTargets);for(let c of n.changedFiles)i.add(b.resolve(e.outputDir,c));for(let c of Object.values(t.manifest.pages))c.rewriteComplete||i.add(b.resolve(c.outputPath));for(let c of Object.values(t.manifest.assets))c.isText&&!c.rewriteComplete&&i.add(b.resolve(c.outputPath));let a=Qi(r,s);if(a.size===0)return [...i];for(let c of Object.values(t.manifest.pages))qr(e,c.discoveredAssets,a)&&i.add(b.resolve(c.outputPath));for(let[c,o]of Object.entries(t.manifest.assets)){o.isText&&qr(e,o.discoveredAssets,a)&&i.add(b.resolve(o.outputPath));for(let l of ct(e,c))if(a.has(l)){te(n,e,o.outputPath),o.isText&&i.add(b.resolve(o.outputPath));break}}return [...i]}function to(e,t){let n=t?new Set(Array.from(t,s=>b.resolve(s))):null,r=s=>n===null||n.has(b.resolve(s));for(let s of Object.values(e.manifest.pages))r(s.outputPath)&&(s.rewriteComplete=true);for(let s of Object.values(e.manifest.assets)){if(!s.isText){s.rewriteComplete=true;continue}r(s.outputPath)&&(s.rewriteComplete=true);}}function no(e,t,n){let r=new Set,s=i=>{if(!i)return;let a=ce(e.outputDir,i);a&&r.add(a);};for(let i of t.donePages)s(he(e,i));for(let i of Object.values(n.manifest.pages))i.lastSeenRunId===n.runId&&s(i.outputPath);for(let i of t.doneAssets)s(Ne(e,t.assetMap,i));for(let i of Object.values(n.manifest.assets))i.lastSeenRunId===n.runId&&s(i.outputPath);return r}async function ro(e,t,n,r,s){let i=[...r.deletedFiles].sort(),a=no(e,t,n),c=0;for(let d of [...a].sort()){try{if((await A.stat(b.join(e.outputDir,d))).isFile())continue}catch{}c++,s.warn(`Active output is missing after crawl: ${d}`,{phase:"deploy-plan",file:d,outputPath:b.join(e.outputDir,d)});}if(c>0&&s.warn(`Detected ${c} active output file(s) missing before deploy plan save.`,{phase:"deploy-plan",missingActiveOutputs:c}),i.length===0)return [];let o=[],l=0;for(let d of i){if(!a.has(d)){o.push(d);continue}l++;let u=b.join(e.outputDir,d),g;try{g=(await A.stat(u)).isFile();}catch{g=false;}s.warn(`Deploy plan protected active output from deletion: ${d}`,{phase:"deploy-plan",file:d,outputPath:u,exists:g});}return l>0&&s.info(`Deploy plan skipped ${l} active output deletion(s).`,{phase:"deploy-plan",protectedDeletes:l,deletedFiles:o.length}),o}function ct(e,t){let n=new Set([t]);try{let r=new URL(t),i=[new URL(e.sourceOrigin).origin,...Object.keys(e.extraReplacements||{})];if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{i.push(new URL(e.targetOrigin).origin);}catch{}for(let a of i)try{n.add(new URL(a).origin+r.pathname+r.search);}catch{}n.add(r.pathname+r.search),n.add(r.pathname);}catch{}return [...n]}function so(e,t){let n=new Set([t]);try{let r=new URL(t),i=[new URL(e.sourceOrigin).origin,...Object.keys(e.extraReplacements||{})];if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{i.push(new URL(e.targetOrigin).origin);}catch{}for(let a of i)try{n.add(new URL(a).origin+r.pathname+r.search);}catch{}n.add(r.pathname+r.search);}catch{}return [...n]}function es(e,t){let n=e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/"?e.targetOrigin:"https://relative.invalid/";try{let r=new URL(t,n);return st(e.outputDir,r.pathname)}catch{return null}}function io(e,t,n){for(let r of so(e,n)){let s=t[r];if(!s)continue;let i=es(e,s);if(i)return {outputPath:i,publicUrl:s}}return null}function oo(e,t,n){for(let r of ct(e,n)){let s=t[r];if(!s)continue;let i=es(e,s);if(i)return i}return null}function Ne(e,t,n){let r=oo(e,t,n);if(r)return r;try{return Ot(e,n).originalFilePath}catch{return null}}function ao(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?b.join(b.resolve(e),"queue-runner-heartbeat.json"):""}async function _r(e){let t=ao();if(t)try{let n=await A.readFile(t,"utf8"),r=JSON.parse(n);if(!r||typeof r!="object")return;await A.writeFile(t,JSON.stringify({...r,checkedAt:new Date().toISOString(),status:"running",currentStep:"rewrite",message:e},null,2),"utf8");}catch{}}function co(e,t){let n=new Set;for(let r of t.donePages)n.add(b.resolve(he(e,r)));for(let r of t.doneAssets){let s=Ne(e,t.assetMap,r);!s||!Pe(s)||n.add(b.resolve(s));}for(let r of t.doneSitemaps){let s=Ne(e,t.assetMap,r);!s||!Pe(s)||n.add(b.resolve(s));}return [...n]}var Mt="re:",kt=new Map;function uo(e){if(!e.startsWith(Mt))return null;let t=kt.get(e);if(t!==void 0)return t||null;let n=e.slice(Mt.length).trim();if(!n)return kt.set(e,false),null;try{let r=new RegExp(n);return kt.set(e,r),r}catch{return console.warn(`[crawl] Ignoring invalid path matcher regex: ${e}`),kt.set(e,false),null}}function Rn(e,t){let n=uo(t);return n?n.test(e):t.startsWith(Mt)?false:e.startsWith(t)}function lo(e,t){return t.find(n=>Rn(e,n))}function ts(e,t){let n=lo(t.pathname,e.blockedPathPrefixes);if(n)return n.startsWith(Mt)?`blocked path regex: ${n}`:`blocked path prefix: ${n}`;let r=e.blockedSearchFragments.find(s=>t.search.includes(s));return r?`blocked search fragment: ${r}`:null}function po(e){let t=new URL(e.sourceOrigin),n=new Set([t.hostname,...e.allowedAssetHosts]);if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{n.add(new URL(e.targetOrigin).hostname);}catch{}for(let r of Object.keys(e.extraReplacements||{}))try{n.add(new URL(r).hostname);}catch{}return n}function go(e,t){return po(e).has(t.hostname)}function fo(e){let t=fe(e.trim()).replace(/\\\//g,"/").replace(/^['"]|['"]$/g,"").trim();return t=t.replace(/[)]+$/g,"").trim(),t=t.replace(/;.*$/g,"").trim(),t}function bn(e){let t=fe(e).replace(/\\\//g,"/");return /[{}]|\bwindow\.|\blocation\.|\bincludes\(|\?\?null|\+|%7B|%7D|%22|<|>|\s/.test(t)||/[()[\]'"]/.test(t)}function mo(e){let t=fe(e).replace(/\\\//g,"/").trim(),n=b.extname(t).toLowerCase();return Vr.has(n)||Kr.has(n)?false:/^(?:this|[A-Za-z_$][\w$]*)\.[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/.test(t)}function Be(e){return Oi.some(t=>e.pathname.startsWith(t))}function J(e,t,n=e.sourceOrigin,r,s="url",i=false){let a=fo(t);if(!a||a.startsWith("data:")||a.startsWith("blob:")||a.startsWith("mailto:")||a.startsWith("tel:")||a.startsWith("#"))return r?.ignore(s,t,"empty or unsupported scheme",n),null;if(bn(a))return r?.ignore(s,t,"looks like JavaScript/code fragment",n),null;if(mo(a))return r?.ignore(s,t,"looks like JavaScript property access",n),null;try{let c=new URL(a,n);if(ns(c))return r?.reject(s,t,"malformed URL encoding",n),null;if(!["http:","https:"].includes(c.protocol))return r?.ignore(s,t,"unsupported protocol: ",n),null;if(!go(e,c))return r?.ignore(s,c.toString(),"host not allowed",n),null;let o=ts(e,c);if(o&&!(i&&en(c)))return r?.reject(s,c.toString(),o,n),null;let l=new URL(e.sourceOrigin);return c.protocol=l.protocol,c.host=l.host,c.hash="",c}catch{return r?.reject(s,t,"invalid URL",n),null}}function ns(e){try{return decodeURIComponent(e.pathname),decodeURIComponent(e.search),decodeURIComponent(e.hash),!1}catch{return true}}function Ce(e,t,n,r){try{let s=new URL(e);return ns(s)?(t.reject(n,e,"malformed URL encoding",r),null):rt(s.pathname)===null?(t.reject(n,e,"unsafe URL pathname",r),null):s.protocol!=="http:"&&s.protocol!=="https:"?(t.reject(n,e,`unsupported protocol: ${s.protocol}`,r),null):s}catch{return t.reject(n,e,"invalid URL",r),null}}function rs(e){return Vr.has(b.extname(e.pathname).toLowerCase())}function Re(e){return Be(e)?false:Kr.has(b.extname(e.pathname).toLowerCase())}function We(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function H(e,t){return !!(We(t)||t.pathname==="/robots.txt"||t.pathname==="/llms.txt"||t.pathname.toLowerCase().endsWith(".xsl")||e.assetPathPrefixes.length>0&&e.assetPathPrefixes.some(n=>Rn(t.pathname,n))||rs(t)||!Re(t)&&!Be(t))}function he(e,t){let n=new URL(t),r=rt(n.pathname);if(r===null)throw new Error(`Unsafe page URL pathname: ${n.pathname}`);return r.endsWith("/")?r+="index.html":b.extname(r)||(r+="/index.html"),st(e.outputDir,r)}function ho(e){if(!e.generated404RequestPath)return null;try{let t=new URL(e.generated404RequestPath,e.sourceOrigin);return !t.pathname.endsWith("/")&&b.extname(t.pathname)===""&&(t.pathname+="/"),t.hash="",t.toString()}catch{return null}}function xn(e,t){let n=ho(e);if(!n)return false;try{return new URL(t,e.sourceOrigin).toString()===n}catch{return false}}function Ot(e,t){let n=new URL(t),r=rt(n.pathname);if(r===null)throw new Error(`Unsafe asset URL pathname: ${n.pathname}`);r.endsWith("/")&&(r+="index.html");let s=!!n.search,i=r,a=st(e.outputDir,i),c=un(e.targetOrigin,i),o=`${c}${n.search}`;if(!s)return {originalPathname:i,originalFilePath:a,originalPublicUrl:c,originalPublicUrlWithSearch:o,preferQueryHashed:s};let l=b.extname(r),u=`${l?r.slice(0,-l.length):r}.${et(n.search)}${l||".bin"}`;return {originalPathname:i,originalFilePath:a,originalPublicUrl:c,originalPublicUrlWithSearch:o,preferQueryHashed:s,hashedPathname:u,hashedFilePath:st(e.outputDir,u),hashedPublicUrl:un(e.targetOrigin,u)}}function wo(e,t,n,r){let s=Ce(t,n,"asset",r);return s?Ot(e,s.toString()):null}function yo(e,t,n,r,s){t[n]=r;try{let i=new URL(n),c=[new URL(e.sourceOrigin).origin,...Object.keys(e.extraReplacements||{})];if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{c.push(new URL(e.targetOrigin).origin);}catch{}for(let o of c)try{let l=new URL(o);t[l.origin+i.pathname+i.search]=r;}catch{}try{let o=(()=>{let l=new URL(r,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin);return `${l.pathname}${l.search}`})();t[i.pathname+i.search]=o;}catch{}try{let o=new URL(r,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin).pathname;t[i.pathname]=o;}catch{}try{let o=new URL(s,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin).pathname;t[i.pathname]=o;}catch{}}catch{}}async function ss(e){try{return await A.readFile(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}async function _e(e){try{return await A.stat(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}function So(e){let t=String(e["content-length"]||"").trim();if(!t)return null;let n=Number.parseInt(t,10);return Number.isFinite(n)&&n>=0?n:null}function is(e){let t=String(e["last-modified"]||"").trim();if(!t)return null;let n=Date.parse(t);return Number.isFinite(n)?n:null}function bo(e,t){let n=is(t),r=So(t),s=e?.mtime.getTime();return n!==null&&Math.abs((s??0)-n)>=1e3||r!==null&&e?.size!==r?false:n!==null||r!==null}async function Po(e,t){if(!t)return;let n=is(t);if(n===null)return;let r=await _e(e);if(!r)return;let s=new Date(n);await A.utimes(e,r.atime,s).catch(()=>{});}function Nr(e,t){let n=b.extname(e.pathname).toLowerCase(),r=t["content-type"]||"";return Di.has(n)||Fi.some(s=>r.includes(s))&&!r.startsWith("image/")&&!r.includes("font")}function vo(e){try{let t=b.extname(new URL(e).pathname).toLowerCase();return t===".js"||t===".mjs"}catch{return false}}function qe(e,t){(e.doneAssets.size%25===0||e.doneAssets.size===1||e.doneAssets.size===e.stats.assetsQueued)&&t.progress(`Asset processing progress: processed ${e.doneAssets.size}, discovered ${e.stats.assetsQueued}.`,{doneAssets:e.doneAssets.size,assetsQueued:e.stats.assetsQueued,phase:"download-assets"});}function Co(e){return et(e.toString("base64"),40)}function Ro(e,t,n){let r=e.assetVariantDigestsByOriginalPath.get(t);if(!r||r.size===0)return false;for(let s of r)if(s!==n)return true;return false}function xo(e,t,n){let r=e.assetVariantDigestsByOriginalPath.get(t);r||(r=new Set,e.assetVariantDigestsByOriginalPath.set(t,r)),r.add(n);}async function Pn(e,t,n,r,s,i,a){let c=Ot(e,n),o=Co(r),l=c.originalFilePath,d=c.originalPublicUrlWithSearch;if(c.preferQueryHashed&&c.hashedFilePath&&c.hashedPublicUrl&&Ro(s,c.originalFilePath,o)){let p=io(e,t,n);p?(l=p.outputPath,d=p.publicUrl):(l=c.hashedFilePath,d=c.hashedPublicUrl);}yo(e,t,n,d,c.originalPublicUrl);let u=Pe(l)?Buffer.from(pn(r.toString("utf8"),e,t,l),"utf8"):r,g=await ss(l),f=false;return (g===null||!g.equals(u))&&(await tt(l,u),f=true),i?.debug(`${f?"Saved":"Skipped unchanged"} asset output ${n}`,{phase:"download-assets",url:n,outputPath:l,bytes:u.length,wroteFile:f}),xo(s,c.originalFilePath,o),await Po(l,a),s.stats.assetsSaved++,i&&(s.stats.assetsSaved%50===0||s.stats.assetsSaved===1)&&i.progress(`Asset progress: saved ${s.stats.assetsSaved}, discovered ${s.stats.assetsQueued}, pending ${s.assetQueue.length}.`,{assetsSaved:s.stats.assetsSaved,assetsQueued:s.stats.assetsQueued,assetQueue:s.assetQueue.length,phase:"download-assets"}),{outputPath:l,publicUrl:d,wroteFile:f}}function ut(e,t,n,r,s){let i=()=>{let u=String(s??"").trim();if(!u)return {discoveredFrom:"",discoveredFromType:"unknown"};if(u==="seed-path"||u==="cli"||u==="generated-404-request-path")return {discoveredFrom:u,discoveredFromType:u};if(/^https?:\/\//i.test(u))try{let g=new URL(u);return We(g)?{discoveredFrom:u,discoveredFromType:"sitemap"}:H(e,g)?{discoveredFrom:u,discoveredFromType:"asset"}:Re(g)&&!Be(g)?{discoveredFrom:u,discoveredFromType:"page"}:{discoveredFrom:u,discoveredFromType:"url"}}catch{return {discoveredFrom:u,discoveredFromType:"url"}}return {discoveredFrom:u,discoveredFromType:"url"}},a=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,c=J(e,n,a,r,"page");if(!c)return;if(!Re(c)||rs(c)){if(H(e,c)){r.info(`Seeded/non-page URL queued as asset: ${c.toString()}`,{url:c.toString(),source:s}),Q(e,t,c.toString(),r,s);return}r.reject("page",c.toString(),Be(c)?"asset/internal path cannot be page":"not page-like",s);return}let o=c.toString(),{discoveredFrom:l,discoveredFromType:d}=i();l&&!t.pageDiscoverySources.has(o)&&t.pageDiscoverySources.set(o,l),!t.donePages.has(o)&&!t.queuedPages.has(o)&&(t.queuedPages.add(o),t.pageQueue.push(o),t.stats.pagesQueued++,r.debug(`Queued discovered page ${o}`,{phase:"discovery",url:o,discoveredFrom:l,discoveredFromType:d,outputPath:he(e,o)}),(t.stats.pagesQueued%25===0||t.stats.pagesQueued===1)&&r.progress(`Discovery progress: pages ${t.stats.pagesQueued}, assets ${t.stats.assetsQueued}, sitemaps ${t.stats.sitemapsQueued}.`,{pagesQueued:t.stats.pagesQueued,assetsQueued:t.stats.assetsQueued,sitemapsQueued:t.stats.sitemapsQueued,pageQueue:t.pageQueue.length,phase:"discovery"}));}function Q(e,t,n,r,s){let i=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,a=J(e,n,i,r,"asset");if(!a)return;if(!H(e,a)){r.reject("asset",a.toString(),"not a safe asset path/prefix",s);return}let c=a.toString();!t.doneAssets.has(c)&&!t.queuedAssets.has(c)&&(t.queuedAssets.add(c),t.assetQueue.push(c),t.stats.assetsQueued++,(t.stats.assetsQueued%100===0||t.stats.assetsQueued===1)&&r.progress(`Discovery progress: pages ${t.stats.pagesQueued}, assets ${t.stats.assetsQueued}, sitemaps ${t.stats.sitemapsQueued}.`,{pagesQueued:t.stats.pagesQueued,assetsQueued:t.stats.assetsQueued,sitemapsQueued:t.stats.sitemapsQueued,assetQueue:t.assetQueue.length,phase:"discovery"}));}function Ft(e,t,n,r,s){let i=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,a=J(e,n,i,r,"sitemap",tr(s));if(!a)return;if(!We(a)){r.reject("sitemap",a.toString(),"not sitemap-like",s);return}let c=a.toString();!t.doneSitemaps.has(c)&&!t.queuedSitemaps.has(c)&&(t.queuedSitemaps.add(c),t.sitemapQueue.push(c),t.stats.sitemapsQueued++,(t.stats.sitemapsQueued%10===0||t.stats.sitemapsQueued===1)&&r.progress(`Discovery progress: pages ${t.stats.pagesQueued}, assets ${t.stats.assetsQueued}, sitemaps ${t.stats.sitemapsQueued}.`,{pagesQueued:t.stats.pagesQueued,assetsQueued:t.stats.assetsQueued,sitemapsQueued:t.stats.sitemapsQueued,sitemapQueue:t.sitemapQueue.length,phase:"discovery"}));}function Io(e){let t=fe(e),n=[...t.matchAll(/<url>\s*([\s\S]*?)\s*<\/url>/gi)].map(r=>r[1]);return n.length>0?n.map(r=>{let s=r.match(/<loc>\s*([^<]+?)\s*<\/loc>/i);if(!s?.[1])return null;let i=r.match(/<lastmod>\s*([^<]+?)\s*<\/lastmod>/i);return {loc:s[1].trim(),lastmod:at(i?.[1]??null)}}).filter(r=>!!r):[...t.matchAll(/<loc>\s*([^<]+?)\s*<\/loc>/gi)].map(r=>({loc:r[1].trim(),lastmod:null})).filter(r=>r.loc)}function Ao(e){return e.split(",").map(t=>t.trim().split(/\s+/)[0]).filter(Boolean)}function To(e){let t=[],n=/url\(\s*(?:"([^"]+)"|'([^']+)'|([^)]*?))\s*\)/gi;for(let r of e.matchAll(n)){let s=e.slice(Math.max(0,r.index-8),r.index);if(/\bnew\s+$/i.test(s))continue;let i=(r[1]||r[2]||r[3]||"").trim();i&&t.push(i);}return t}function ko(e){return e.replace(/<script\b[^>]*>[\s\S]*?<\/script\s*>/gi,"")}function $o(e,t){if(t==="dom-style")return true;let n=(()=>{try{return b.extname(new URL(e).pathname).toLowerCase()}catch{return ""}})();return !(n===".js"||n===".mjs"||n===".map"||t.includes("javascript"))}function Et(e){return e.trim().replace(/\\/g,"/").replace(/^https?:\/\/[^/]+/i,"").replace(/^\/+/,"")}async function os(e,t,n,r){let s=[];await A.mkdir(t,{recursive:true});let i=await A.readdir(e,{withFileTypes:true});for(let a of i){let c=b.join(e,a.name),o=b.join(t,a.name);if(a.isDirectory()){s.push(...await os(c,o,n,r));continue}await as(c,o,n,r)&&s.push(o);}return s}async function Eo(e,t){try{let[n,r]=await Promise.all([A.stat(e),A.stat(t)]);if(n.size!==r.size)return !1;let[s,i]=await Promise.all([A.readFile(e),A.readFile(t)]);return s.equals(i)}catch{return false}}async function as(e,t,n,r){let s=b.resolve(t);return n.has(s)?(r.debug("Skipped post-crawl copy over crawled page output",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):await Eo(e,t)?(r.debug("Skipped unchanged post-crawl copy target",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):(await A.mkdir(b.dirname(t),{recursive:true}),await A.copyFile(e,t),true)}async function Mo(e,t,n){let r=Object.entries(e.postCrawlCopyMap||{}).map(([c,o])=>({sourcePath:c.trim(),prefix:String(o||"").trim()}));if(r.length===0)return [];let s=0,i=[],a=new Set([...t.donePages].map(c=>b.resolve(he(e,c))));n.mark("copy-extra-paths");for(let c of r){let o=c.sourcePath,l=c.prefix;if(!o||!l){n.warn("Skipped post-crawl copy mapping with empty key/value",{sourcePath:o,prefixPath:l});continue}let d=_i(o);if(!d.resolvedPath){n.warn("Skipped post-crawl copy source because alias root is not configured",{sourcePath:o,alias:d.alias||"",requiredEnv:d.requiredEnv||"",prefixPath:l});continue}let u=d.resolvedPath,g=Et(l);if(!g){n.warn("Skipped post-crawl copy mapping with invalid export prefix",{sourcePath:o,prefixPath:l});continue}let f;try{f=await A.stat(u);}catch{n.warn("Skipped post-crawl copy source because it does not exist",{sourcePath:o,sourceAbs:u,prefixPath:l});continue}let p=b.resolve(e.outputDir,g);if(f.isDirectory()){let w=await os(u,p,a,n);i.push(...w),s+=w.length,w.length>0&&n.progress(`Copied static directory to export: ${u} -> /${g}`,{phase:"copy-extra-paths",sourcePath:u,targetPath:`/${g}`,copiedItems:s,changedFiles:w.length});continue}let y=b.basename(u),m=l.endsWith("/")?b.join(p,y):p;await as(u,m,a,n)&&(i.push(m),s++,n.progress(`Copied static file to export: ${u} -> /${Et(l.endsWith("/")?`${l}${y}`:l)}`,{phase:"copy-extra-paths",sourcePath:u,targetPath:`/${Et(l.endsWith("/")?`${l}${y}`:l)}`,copiedItems:s}));}return n.endMark("copy-extra-paths",{mappedSources:r.length,copiedItems:s}),i}function lt(e,t,n,r,s){let i=new Set,a=fe(n).replace(/\\\//g,"/"),c=s==="page-html"?ko(a):a,o=$o(t,s);if(o){for(let l of To(c)){let d=J(e,l,t,r,`${s}:css-url`);d&&H(e,d)&&i.add(d.toString());}for(let l of c.matchAll(/@import\s+(?:url\()?\s*['"]?([^'"\s;]+)['"]?\s*\)?/gi)){let d=J(e,l[1],t,r,`${s}:css-import`);d&&H(e,d)&&i.add(d.toString());}}if(o||s==="page-html")for(let l of c.matchAll(/<\?xml-stylesheet[^>]+href=["']([^"']+)["'][^>]*\?>/gi)){let d=J(e,l[1],t,r,`${s}:xml-stylesheet`);d&&H(e,d)&&i.add(d.toString());}if(s==="page-html")for(let l of Ir(a)){let d=l.startsWith("//")?`${new URL(e.sourceOrigin).protocol}${l}`:l;if(bn(d)){r.ignore(`${s}:serialized-url`,d,"looks like JavaScript/code fragment",t);continue}let u=J(e,d,t,r,`${s}:serialized-url`);u&&H(e,u)?i.add(u.toString()):u&&r.ignore("asset",u.toString(),"serialized URL did not pass safe asset path filter",t);}if(s!=="page-html"&&vo(t))for(let l of Ar(a)){if(bn(l)||!/\.(?:js|mjs)(?:[?#]|$)/i.test(l))continue;let d=J(e,l,t,r,`${s}:relative-asset`);d&&H(e,d)&&i.add(d.toString());}return [...i]}function Fo(e,t,n,r){let s=new Set,i=fe(n);for(let a of i.matchAll(/href=["']([^"'#\s][^"']*?)["']/gi)){let c=a[1],o=J(e,c,t,r,"page-link");o&&(ts(e,o)||Re(o)&&!Be(o)&&!H(e,o)&&s.add(o.toString()));}return [...s]}async function Br(e,t,n,r,s,i,a=false,c=[]){let o=new Set(c),l=0,d=new Map,u=(g,f)=>{let p=[f],y=new Set;for(;p.length>0;){let m=p.pop();if(m===g)return true;y.has(m)||(y.add(m),p.push(...d.get(m)??[]));}return false};for(;n.sitemapQueue.length>0;){let g=n.sitemapQueue.shift();n.queuedSitemaps.delete(g);let f=Ce(g,r,"sitemap","sitemap-queue");if(!f)continue;let p=f.toString();if(n.doneSitemaps.has(p))continue;n.doneSitemaps.add(p),r.sitemap(`Fetching sitemap ${p}`,{url:p});let y=true;try{let m=await t.get(p,{timeout:6e4});if(y=!1,!m.ok()){if(r.skip("sitemap",p,m.status()),a&&!o.has(p))throw new Error(`Required child content-sync sitemap returned HTTP ${m.status()}: ${p}`);continue}let h=await m.body(),w=m.url()||p,P=Ce(w,r,"sitemap-response",p);if(!P)continue;let x=P.toString();n.successfulSitemaps.add(p),n.successfulSitemaps.add(x),o.has(p)&&(n.resolvedSitemapRoots.add(x),l+=1);let L=await Pn(e,n.assetMap,x,h,n,r);te(s,e,L.outputPath),Te(s,L.outputPath);let M=h.toString("utf8");/<sitemapindex(?:\s|>)/i.test(M)?n.sitemapIndexUrls.add(x):n.sitemapLeafUrls.add(x);let _=lt(e,x,M,r,"sitemap");for(let F of _)Q(e,n,F,r,x);let C=[];for(let F of Io(M)){let T=J(e,F.loc,x,r,"sitemap-loc",!0);if(!T){if(a)throw new Error(`Content-sync sitemap contains an inadmissible location: ${F.loc}`);continue}if(We(T)){if(u(x,T.toString()))throw new Error(`Sitemap index cycle detected: ${x} -> ${T.toString()}`);if(d.has(x)||d.set(x,new Set),d.get(x).add(T.toString()),C.push(T.toString()),a&&n.doneSitemaps.has(T.toString())&&!n.successfulSitemaps.has(T.toString()))throw new Error(`Required child content-sync sitemap was not resolved: ${T.toString()}`);Ft(e,n,T.toString(),r,x);}else if(a){if(Re(T)&&!H(e,T)){let j=i.requiredUrls.has(T.toString()),S=i.manifest.pages[T.toString()],R=!!S&&!!await _e(S.outputPath);if(!j&&!R)throw new Error(`Content-sync sitemap coverage is incomplete for ${T.toString()}.`)}r.ignore("sitemap-loc",T.toString(),"content-sync sitemap-only traversal does not enqueue urlset locations",x);}else H(e,T)?Q(e,n,T.toString(),r,x):Re(T)?(F.lastmod&&(n.sitemapLastmodByPage[T.toString()]=F.lastmod),ut(e,n,T.toString(),r,x)):r.reject("sitemap-loc",T.toString(),"not page/sitemap/asset-like",x);}i.trackManifest&&(i.manifest.assets[x]={url:x,outputPath:L.outputPath,isText:!0,contentType:m.headers()["content-type"]??"application/xml",cacheControl:wn(m.headers()),changeToken:null,tokenSource:null,discoveredAssets:[...new Set([..._,...C])],lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,unreferencedRuns:0,rewriteComplete:!1},await ve(e,i));}catch(m){r.error(`Failed sitemap ${p}`,{url:p,error:String(m)});let h=a&&o.has(p)&&y;if(a&&!h)throw m}}if(a&&o.size>0&&l===0)throw new Error(`None of the configured content-sync sitemap roots could be resolved: ${[...o].join(", ")}`)}async function Do(e,t,n,r,s,i,a){let c=J(e,r,e.sourceOrigin,s,"asset-fetch");if(!c||!H(e,c))return;let o=c.toString();if(n.doneAssets.has(o))return;let l=i.enabled||i.strictTargeted?i.previousManifest.assets[o]:void 0,d=i.trackManifest?await $t(e,t,i,o,s):null,u=d?.token??null,g=$r(l?.changeToken,d),f=i.strictTargeted&&(!d?.supported||!d.token)&&Er(e.sourceOrigin,o),p=i.strictTargeted?i.manifest.assets[o]:void 0;if(p?.lastSeenRunId===i.runId&&await _e(p.outputPath)){n.doneAssets.add(o);for(let m of p.discoveredAssets||[])Q(e,n,m,s,o);te(a,e,p.outputPath),Tt(a,e,p.outputPath,p.cacheControl),p.isText&&Te(a,p.outputPath),s.info(`Resumed completed content-sync asset ${o}`,{url:o,source:"content-sync-checkpoint"}),qe(n,s);return}if(l&&(g||f)&&await _e(l.outputPath)){i.manifest.assets[o]={...l,changeToken:u,tokenSource:d?.tokenSource??l.tokenSource,discoveredAssets:[...l.discoveredAssets||[]],lastSeenRunId:i.runId,unreferencedRuns:0},delete i.manifest.pages[o],n.doneAssets.add(o);for(let m of l.discoveredAssets||[])Q(e,n,m,s,o);Tt(a,e,l.outputPath,l.cacheControl),s.info(`Reused unchanged trusted asset ${o}.`,{url:o,tokenSource:d?.tokenSource??"unknown",phase:"download-assets",source:g?"asset-change-token-match":"content-sync-release-fingerprint"}),qe(n,s);return}let y=(m,h={})=>{if(!l||i.strictTargeted)return false;i.manifest.assets[o]={...l,discoveredAssets:[...l.discoveredAssets||[]],lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,unreferencedRuns:0},delete i.manifest.pages[o],n.doneAssets.add(o);for(let w of l.discoveredAssets||[])Q(e,n,w,s,o);return s.warn(`Keeping previous incremental asset ${o} after fetch issue.`,{url:o,reason:m,preservedFromPreviousManifest:true,...h,phase:"download-assets"}),qe(n,s),true};s.asset(`Fetching asset ${o}`,{url:o});try{let m=Ne(e,n.assetMap,o),h=m?await _e(m):null;if(m&&h&&!i.strictTargeted)try{let S=await z(`fetch asset headers ${o}`,6e4,()=>t.head(o,{timeout:6e4})),R=S.headers();if(S.ok()&&bo(h,R)){let D=await ss(m);if(D!==null){let q=S.url()||o,v=Ce(q,s,"asset-response",o);if(!v)return;let O=v.toString(),E=await Pn(e,n.assetMap,O,D,n,s,R);E.wroteFile&&(te(a,e,E.outputPath),Pe(E.outputPath)&&Te(a,E.outputPath));let B=wn(R)||l?.cacheControl||null;Tt(a,e,E.outputPath,B),n.doneAssets.add(o),s.info(`Reused existing asset ${o} based on response headers.`,{url:o,filePath:m,lastModified:R["last-modified"]||"",contentLength:R["content-length"]||"",phase:"download-assets"}),qe(n,s);let ye=Nr(c,R),xe=l?.discoveredAssets?[...l.discoveredAssets]:[];if(ye||l?.isText){let k=D.toString("utf8"),Ie=lt(e,O,k,s,"asset:cached");(Ie.length>0||!l)&&(xe=Ie);for(let zt of xe)Q(e,n,zt,s,O);}if(i.trackManifest){let k=ye||!!l?.isText;i.manifest.assets[o]={url:o,outputPath:E.outputPath,isText:k,contentType:hn(R)||l?.contentType||null,cacheControl:B,changeToken:d?.supported?d.token:null,tokenSource:d?.supported?d.tokenSource??null:null,discoveredAssets:xe,lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,unreferencedRuns:0,rewriteComplete:!k},delete i.manifest.pages[o],(i.enabled||i.strictTargeted)&&k&&await ve(e,i);}return}}}catch{}let w=await z(`fetch asset ${o}`,6e4,()=>t.get(o,{timeout:6e4}));if(!w.ok()){if(y(`http-${w.status()}`,{status:w.status()}))return;if(i.strictTargeted)throw new Error(`Required content-sync asset returned HTTP ${w.status()}: ${o}`);s.skip("asset",o,w.status());return}let P=await z(`read asset response ${o}`,6e4,()=>w.body()),x=w.url()||o,L=Ce(x,s,"asset-response",o);if(!L)return;let M=L.toString(),_=w.headers(),C=await Pn(e,n.assetMap,M,P,n,s,_);C.wroteFile&&(te(a,e,C.outputPath),Pe(C.outputPath)&&Te(a,C.outputPath));let F=wn(_);Tt(a,e,C.outputPath,F),n.doneAssets.add(o),qe(n,s);let T=Nr(c,_),j=[];if(T){let S=P.toString("utf8");j=lt(e,M,S,s,`asset:${b.extname(c.pathname).toLowerCase()||_["content-type"]||"unknown"}`);for(let R of j)Q(e,n,R,s,M);}i.trackManifest&&(i.manifest.assets[o]={url:o,outputPath:C.outputPath,isText:T,contentType:hn(_),cacheControl:F,changeToken:d?.supported?d.token:null,tokenSource:d?.supported?d.tokenSource??null:null,discoveredAssets:j,lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,unreferencedRuns:0,rewriteComplete:!T},delete i.manifest.pages[o],(i.enabled||i.strictTargeted)&&T&&await ve(e,i));}catch(m){if(y("fetch-error",{error:String(m)}))return;if(s.error(`Failed asset ${o}`,{url:o,error:String(m)}),i.strictTargeted)throw m}finally{n.doneAssets.has(o)||(n.doneAssets.add(o),qe(n,s));}}async function Oo(e,t,n,r,s,i){let a=Math.max(1,Number(e.assetDownloadConcurrency||e.concurrency||1)),c=Array.from({length:a},async()=>{for(;n.assetQueue.length>0;){let o=n.assetQueue.shift();o&&(n.queuedAssets.delete(o),await Do(e,t,n,o,r,s,i));}});await Promise.all(c);}function Uo(e){let t=Math.ceil(e/1e3);return `${t} second${t===1?"":"s"}`}function me(e){return Math.max(6e4,e.navigationTimeoutMs*2,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+15e3)}function ot(e){return Math.max(15e3,Math.min(6e4,me(e)))}function Lo(e){return Math.max(15e3,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+1e4)}function In(e){return e.stats.pagesCompleted}function vn(e){let n=e.options?.browserProcess;return {pid:typeof n?.process?.pid=="number"?n.process.pid:null,kill:typeof n?.kill=="function"?n.kill.bind(n):null}}async function mn(e,t,n){let r=vn(e);try{if(r.kill){await r.kill(),t.warn(`Force-killed crawl worker browser after ${n}`,{browserPid:r.pid,reason:n});return}if(r.pid!==null){process.kill(r.pid,"SIGKILL"),t.warn(`Sent SIGKILL to crawl worker browser after ${n}`,{browserPid:r.pid,reason:n});return}t.warn(`Could not resolve crawl worker browser PID after ${n}`,{reason:n});}catch(s){t.error(`Failed to force-kill crawl worker browser after ${n}`,{browserPid:r.pid,reason:n,error:String(s)});}}async function z(e,t,n){let r;try{return await Promise.race([n(),new Promise((s,i)=>{r=globalThis.setTimeout(()=>{i(new dt(e,t));},t);})])}finally{r!==void 0&&globalThis.clearTimeout(r);}}var dt=class extends Error{constructor(n,r){super(`${n} timed out after ${Uo(r)}.`);this.operation=n;this.timeoutMs=r;this.name="OperationTimeoutError";}operation;timeoutMs};async function jo(e,t){let n=Lo(e);await t.evaluate(async r=>{await new Promise(s=>{let i=0,a=700,c=Date.now(),o=()=>{window.clearInterval(l),window.scrollTo(0,0),s();},l=window.setInterval(()=>{if(Date.now()-c>=r.maxDurationMs){o();return}window.scrollBy(0,a),i+=a;let d=document.body?.scrollHeight||0,u=document.documentElement?.scrollHeight||0,g=Math.max(d,u);i>=g+window.innerHeight&&o();},120);});},{maxDurationMs:n});}async function qo(e,t){await t.waitForLoadState("domcontentloaded",{timeout:e.readiness.timeoutMs}).catch(()=>{}),e.readiness.waitForSelector&&await t.waitForSelector(e.readiness.waitForSelector,{timeout:e.readiness.timeoutMs}).catch(()=>{}),e.readiness.waitForFunction&&await t.waitForFunction(e.readiness.waitForFunction,void 0,{timeout:e.readiness.timeoutMs}).catch(()=>{}),await jo(e,t),await t.waitForTimeout(e.readiness.fallbackWaitMs);}async function _o(e,t){await t.waitForLoadState("domcontentloaded",{timeout:e.readiness.timeoutMs}).catch(()=>{}),await t.waitForLoadState("load",{timeout:e.readiness.timeoutMs}).catch(()=>{}),e.readiness.waitForSelector&&await t.waitForSelector(e.readiness.waitForSelector,{timeout:e.readiness.timeoutMs}).catch(()=>{}),await t.waitForTimeout(e.readiness.fallbackWaitMs);}function No(e,t){return e.noJavaScriptRenderPathPrefixes.some(n=>Rn(t,n))}async function Bo(e,t,n,r,s=true){let i=new Set;if(s){let c=await t.evaluate(()=>{let o=new Set,l=["href","src","poster","data-src","data-lazy-src","data-original","data-bg","data-background","data-href","data-resource-url"],d=new Set,u=["srcset","data-srcset","data-lazy-srcset"],g=f=>{f.querySelectorAll("*").forEach(p=>{for(let m of l){let h=p.getAttribute(m);h&&o.add(h);}for(let m of u){let h=p.getAttribute(m);h&&h.split(",").forEach(w=>{let P=w.trim().split(/\s+/)[0];P&&o.add(P);});}let y=p.getAttribute("style");y&&d.add(y),p.shadowRoot&&g(p.shadowRoot);});};return g(document),{attrs:[...o],styles:[...d]}});for(let o of c.attrs)for(let l of Ao(o)){let d=J(e,l,n,r,"dom-attr");d&&H(e,d)&&i.add(d.toString());}for(let o of c.styles)for(let l of lt(e,n,o,r,"dom-style"))i.add(l);}let a=await t.content();for(let c of lt(e,n,a,r,"page-html"))i.add(c);return [...i]}async function Wr(e,t,n,r,s){let i=he(e,t);await tt(i,n,"utf8");let a=await A.stat(i);s?.debug(`Saved page output ${t}`,{phase:"save-pages",url:t,outputPath:i,bytes:a.size}),r.stats.pagesSaved++;let c=Math.min(r.stats.pagesQueued,In(r)+1);s&&(r.stats.pagesSaved%10===0||r.stats.pagesSaved===1)&&s.progress(`Page progress: processed ${c}, rendered ${r.stats.pagesRendered}, saved ${r.stats.pagesSaved}, discovered ${r.stats.pagesQueued}.`,{donePages:c,claimedPages:r.donePages.size,pagesRendered:r.stats.pagesRendered,pagesSaved:r.stats.pagesSaved,pagesQueued:r.stats.pagesQueued,pageQueue:r.pageQueue.length,phase:"save-pages"});}function Wo(e){let t=In(e);return t%5===0||t===1||t===e.stats.pagesQueued}function Ho(e,t){let n=In(e);t.progress(`Render progress: processed ${n}, rendered ${e.stats.pagesRendered}, saved ${e.stats.pagesSaved}, discovered ${e.stats.pagesQueued}.`,{donePages:n,claimedPages:e.donePages.size,pagesRendered:e.stats.pagesRendered,pagesSaved:e.stats.pagesSaved,pagesQueued:e.stats.pagesQueued,phase:"render-pages"});}async function zo(e){await e.addInitScript(()=>{Object.defineProperty(window,"__WPSUITE_STATIC_EXPORT__",{value:true,writable:false,configurable:true});let t=n=>{if(n==null||String(n)==="")return true;try{let r=new URL(String(n),window.location.href),s=new URL(window.location.href);return r.hash="",s.hash="",r.href===s.href}catch{return false}};try{let n=window.location.assign.bind(window.location),r=window.location.replace.bind(window.location),s=window.location.reload.bind(window.location);Object.defineProperty(window.location,"assign",{configurable:!0,value:i=>{if(t(i)){console.warn("[smartcloud-static-publisher] blocked same-page location.assign",i??"");return}return n(i)}}),Object.defineProperty(window.location,"replace",{configurable:!0,value:i=>{if(t(i)){console.warn("[smartcloud-static-publisher] blocked same-page location.replace",i??"");return}return r(i)}}),Object.defineProperty(window.location,"reload",{configurable:!0,value:()=>{console.warn("[smartcloud-static-publisher] blocked location.reload");}});}catch{}});}async function Qo(e,t,n){let r=await e.newContext({viewport:t.viewport,userAgent:"WPSuiteStaticPublisher/0.8 Playwright SitemapOnly",ignoreHTTPSErrors:t.ignoreHttpsErrors,...n?{javaScriptEnabled:false}:{}});return n||await zo(r),r}async function Jo(e,t,n,r,s,i,a,c){let o=await chromium.launch({headless:true}),l=vn(o);l.pid!==null&&n.debug(`Launched crawl worker browser PID ${l.pid}`,{browserPid:l.pid});try{for(;t.pageQueue.length>0;){o||(o=await chromium.launch({headless:!0}),l=vn(o),l.pid!==null&&n.debug(`Relaunched crawl worker browser PID ${l.pid}`,{browserPid:l.pid}));let d=o;if(e.maxPages>0&&t.donePages.size>=e.maxPages)break;let u=t.pageQueue.shift();if(!u)break;t.queuedPages.delete(u);let g=null,f=null,p=!1,y=[],m=new Set;try{let h=Ce(u,n,"page","page-queue");if(!h)continue;let w=h.toString();if(u=w,t.queuedPages.delete(u),t.donePages.has(u))continue;t.donePages.add(u),i.enabled&&i.seenPages.add(u);let P=xn(e,u),x=No(e,h.pathname);if(!Re(h)||H(e,h)||Be(h)){if(H(e,h)){n.info(`Worker redirected non-page URL to asset queue: ${u}`,{url:u,source:"worker-guard"}),Q(e,t,u,n,"worker-guard");continue}n.reject("page",u,"guard rejected non-page URL before rendering","worker");continue}let L=i.strictTargeted?i.manifest.pages[u]:void 0;if(L?.lastSeenRunId===i.runId&&await _e(L.outputPath)){i.seenPages.add(u);for(let v of L.discoveredAssets||[])Q(e,t,v,n,u);te(a,e,L.outputPath),Te(a,L.outputPath),n.info(`Resumed completed content-sync page ${u}`,{url:u,source:"content-sync-checkpoint"});continue}let M=await Gi(e,s,t,i,u,n);if(i.enabled&&r!=="single-url"&&r!=="content-sync"){let v=i.manifest.pages[u];if(M.action==="reuse"&&v){for(let O of v.discoveredAssets)Q(e,t,O,n,u);for(let O of v.discoveredPages)ut(e,t,O,n,u);v.lastSeenRunId=i.runId,v.sitemapLastmod=at(t.sitemapLastmodByPage[u])??v.sitemapLastmod,M.changeToken?.supported&&(v.changeToken=M.changeToken.token,v.tokenSource=M.changeToken.tokenSource??v.tokenSource),n.info(`Incremental reuse skipped unchanged page ${u}`,{url:u,mode:"incremental",reason:M.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}g=await z(`create page context ${u}`,me(e),()=>Qo(d,e,x)),f=await z(`create page ${u}`,me(e),()=>g.newPage()),y=await g.cookies();let _=!1,C=new Set;await f.route("**/*",async v=>{let O=v.request();try{let E=J(e,O.url(),e.sourceOrigin,n,"network-request");E&&H(e,E)&&(C.add(E.toString()),Q(e,t,E.toString(),n,u));}catch{}if(O.isNavigationRequest()&&O.frame()===f.mainFrame())try{let E=new URL(O.url()),B=new URL(w);if(E.hash="",B.hash="",E.href===B.href){if(_){n.warn(`Blocked same-page navigation/reload for ${u}`,{url:u,requestUrl:O.url()});try{await v.abort("aborted");}catch(ye){n.debug(`Ignored canceled route while blocking reload for ${u}`,{url:u,error:String(ye)});}return}_=!0;}}catch{}try{await v.continue();}catch(E){n.debug(`Ignored canceled network route for ${u}`,{url:u,requestUrl:O.url(),error:String(E)});}}),f.on("response",v=>{try{let E=J(e,v.url(),e.sourceOrigin,n,"network-response");E&&H(e,E)&&(C.add(E.toString()),Q(e,t,E.toString(),n,u));}catch{}let O=v.headerValues("set-cookie").then(E=>{for(let B of E)c.add(Dr(B,v.url()));}).catch(()=>{}).then(()=>{});m.add(O),O.finally(()=>m.delete(O));}),n.page(`Rendering ${u}`,{url:u});let F=null,T=null;try{if(F=await f.goto(u,{waitUntil:"domcontentloaded",timeout:e.navigationTimeoutMs}),F&&F.ok())try{T=await z(`read response body ${u}`,me(e),()=>F.text());}catch{T=null;}}catch(v){n.warn(`Navigation issue for ${u}; saving current DOM if available`,{url:u,error:String(v)});}if(r==="content-sync"&&F){let v=Ce(F.url(),n,"content-sync-final-response",u)?.toString();if(v&&v!==w){if(i.deleteOnMissingUrls.has(u)){await Cn(e,i,a,u,i.deleteOnMissingUrls),n.info(`Removed stale content-sync archive route redirected to ${v}`,{url:u,finalResponseUrl:v,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${u} -> ${v}`)}}if(P){let v=F?.status()??null;if(v!==404){n.error(`Generated 404 request path returned unexpected status for ${u}`,{url:u,expectedStatus:404,status:v,requestPath:e.generated404RequestPath});continue}n.info(`Capturing rendered 404 page for ${u}`,{url:u,status:v,outputPath:he(e,u)});}else if(F&&!F.ok()){if(r==="content-sync"&&(F.status()===404||F.status()===410)&&i.deleteOnMissingUrls.has(u)){await Cn(e,i,a,u,i.deleteOnMissingUrls),n.info(`Removed stale content-sync archive page ${u}`,{url:u,status:F.status(),source:"content-sync-tombstone"}),await f.close();continue}if(r==="content-sync")throw new Error(`Required content-sync route returned HTTP ${F.status()}: ${u}`);n.skip("page",u,F.status(),t.pageDiscoverySources.get(u)),await f.close();continue}x?(n.info(`Rendering without JS execution for ${u}`,{url:u,mode:"no-js"}),await z(`wait for rendered page ${u}`,me(e),()=>_o(e,f))):await z(`wait for rendered page ${u}`,me(e),()=>qo(e,f)),t.stats.pagesRendered++;let j=await z(`extract rendered assets ${u}`,me(e),()=>Bo(e,f,w,n,!x));for(let v of j)Q(e,t,v,n,u);let S=[...new Set([...j,...C])],R=T??await z(`capture rendered html ${u}`,me(e),()=>f.content()),D=r!=="single-url"&&r!=="content-sync"&&!P?Fo(e,u,R,n):[];if(r!=="single-url"&&r!=="content-sync"&&!P)for(let v of D)ut(e,t,v,n,u);await Wr(e,u,R,t,n);let q=he(e,u);if(te(a,e,q),Te(a,q),i.trackManifest){let v=F?.headers();i.manifest.pages[u]={url:u,outputPath:q,contentType:hn(v),changeToken:M.changeToken?.supported?M.changeToken.token:null,tokenSource:M.changeToken?.supported?M.changeToken.tokenSource??null:null,sitemapLastmod:at(t.sitemapLastmodByPage[u])??null,discoveredPages:D,discoveredAssets:S,lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,rewriteComplete:!1},(i.enabled||i.strictTargeted)&&await ve(e,i);}}catch(h){if(p=h instanceof dt,i.strictTargeted)throw n.error(`Failed required content-sync page ${u}`,{url:u,error:String(h)}),h;if(p)n.warn(`Skipped partial DOM for timed-out page ${u}`,{url:u,error:String(h),recovery:"recycle-worker-browser"});else try{let w=f?await z(`capture partial html ${u}`,me(e),()=>f.content()):"";if(w&&w.trim()){await Wr(e,u,w,t,n);let P=he(e,u);if(te(a,e,P),Te(a,P),i.trackManifest){let x=i.manifest.pages[u]??i.previousManifest.pages[u];i.manifest.pages[u]={url:u,outputPath:P,contentType:x?.contentType??null,changeToken:null,tokenSource:null,sitemapLastmod:at(t.sitemapLastmodByPage[u])??x?.sitemapLastmod??null,discoveredPages:x?.discoveredPages?[...x.discoveredPages]:[],discoveredAssets:x?.discoveredAssets?[...x.discoveredAssets]:[],lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,rewriteComplete:!1},(i.enabled||i.strictTargeted)&&await ve(e,i);}n.warn(`Saved partial DOM for ${u}`,{url:u,error:String(h)});}}catch(w){w instanceof dt&&(p=!0),n.error(`Could not save partial DOM for ${u}`,{url:u,error:String(w)});}n.error(`Failed page ${u}`,{url:u,error:String(h)});}finally{if(!p&&f&&await z(`drain network routes ${u}`,ot(e),()=>f.unrouteAll({behavior:"wait"})).catch(h=>{p=!0,n.warn(`Could not drain network routes for ${u}`,{url:u,browserPid:l.pid,error:String(h),recovery:"recycle-worker-browser"});}),p&&(o=null,await mn(d,n,`page operation timeout for ${u}`),await d.close().catch(()=>{})),m.size>0&&await Promise.allSettled([...m]),!p&&g)try{let h=await g.cookies();c.addAll(Or(y,h));}catch{}t.stats.pagesCompleted++,Wo(t)&&Ho(t,n),!p&&f&&await z(`close page ${u}`,ot(e),()=>f.close()).catch(h=>{p=!0,n.warn(`Timed out while closing page ${u}`,{url:u,browserPid:l.pid,error:String(h),recovery:"recycle-worker-browser"});}),!p&&g&&await z(`close page context ${u}`,ot(e),()=>g.close()).catch(h=>{p=!0,n.warn(`Timed out while closing page context for ${u}`,{url:u,browserPid:l.pid,error:String(h),recovery:"recycle-worker-browser"});}),p&&o===d&&(o=null,await mn(d,n,`page cleanup timeout for ${u}`),await d.close().catch(()=>{}));}}}finally{if(o){let d=o;await z("close worker browser",ot(e),()=>d.close()).catch(u=>(n.warn("Timed out while closing crawl worker browser",{browserPid:l.pid,error:String(u)}),mn(d,n,"browser.close timeout")));}}}async function Vo(e){try{let t=await A.readFile(e),r=(e.endsWith(".gz")?gunzipSync(t):t).toString("utf8").trim();if(!r)return [];try{let s=JSON.parse(r);if(Array.isArray(s))return s;if(s&&typeof s=="object")return [s]}catch{return r.split(/\r?\n/).map(s=>s.trim()).filter(Boolean).map(s=>{try{let i=JSON.parse(s);return i&&typeof i=="object"?i:null}catch{return null}}).filter(s=>s!==null)}return []}catch{return []}}function Hr(e){let t=String(e||"").toLowerCase();return t.includes("timeout")||t.includes("timed out")||t.includes("navigation timeout")}function Ko(e){let t=String(e?.job?.command||"").trim(),n=String(e?.job?.crawlMode||"full").trim();return t!=="publish"&&t!=="crawl"?false:n!=="incremental"}function Go(e){let t=[e?.archivedAt,e?.job?.endedAt,e?.job?.startedAt];for(let n of t){let r=Date.parse(String(n||""));if(Number.isFinite(r))return r}return 0}async function cs(e){try{let t=await A.readFile(e,"utf8"),n=JSON.parse(t);return n&&typeof n=="object"&&!Array.isArray(n)?n:null}catch{return null}}function zr(e){let t=String(e.originalFileName||"").toLowerCase();return t.endsWith("errors.jsonl")?0:t.endsWith("errors.json")?1:2}function Zo(e,t){let n=Array.isArray(t?.artifacts)?t.artifacts.filter(r=>String(r?.role||"").trim()==="errors"&&String(r?.storedFileName||"").trim()!=="").sort((r,s)=>zr(r)-zr(s)).map(r=>b.join(e,String(r.storedFileName))):[];return [...new Set([...n,b.join(e,"errors.jsonl"),b.join(e,"errors.json"),b.join(e,"errors.jsonl.gz"),b.join(e,"errors.json.gz")])]}async function Qr(e){let t=await cs(b.join(e,"job.json"));for(let n of Zo(e,t)){let r=await Vo(n);if(r.length>0)return r}return []}async function Yo(e){let t=b.join(e.logDir,"archive"),n;try{n=await A.readdir(t);}catch{return ""}let r="",s=0;for(let i of n){let a=b.join(t,i);if(!(await A.stat(a).catch(()=>null))?.isDirectory())continue;let o=await cs(b.join(a,"job.json"));if(!Ko(o))continue;let l=Go(o);l>=s&&(s=l,r=a);}return r}async function Xo(e){let t=await Yo(e),n=t!==""?await Qr(t):await Qr(e.logDir),r=new Set;for(let s of n)if(Hr(s.error)||Hr(s.message)){let i=s.url;typeof i=="string"&&i&&r.add(i);}return [...r]}async function ea(e){let t=[];for(let n of e)if(n.startsWith("@")){let r=n.slice(1),s=await A.readFile(r,"utf8");t.push(...s.split(/\r?\n/).map(i=>i.trim()).filter(i=>i&&!i.startsWith("#")));}else t.push(n);return t}async function ta(e){if(!e)throw new Error("Content-sync crawl requires an immutable impact plan.");let t=JSON.parse(await A.readFile(e,"utf8"));return Ge(t),t}function na(e,t){let n;try{n=new URL(e);}catch{return false}return t.archiveFamilies.some(r=>{let s=new URL(r.url);if(n.origin!==s.origin)return false;if(n.toString()===s.toString())return true;if(n.searchParams.has("paged")&&n.pathname===s.pathname)return /^\d+$/.test(n.searchParams.get("paged")||"");let i=s.pathname.replace(/\/+$/,""),a=n.pathname.slice(i.length).replace(/^\/+/,""),c=String(r.paginationBase||"page").replace(/^\/+|\/+$/g,"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return n.pathname.startsWith(`${i}/`)&&new RegExp(`^${c}/\\d+/?$`).test(a)})}function it(e,t,n){let r=n==="page"?he(e,t):Ot(e,t).originalFilePath;return ce(e.outputDir,r)}async function Cn(e,t,n,r,s){let i=t.manifest.pages[r];if(!i)return;let a=ce(e.outputDir,i.outputPath);if(!a)throw new Error(`Manifest-owned page path escaped output root: ${r}`);let c=b.resolve(i.outputPath);Object.entries(t.manifest.pages).some(([l,d])=>l!==r&&!s.has(l)&&b.resolve(d.outputPath)===c)||(await A.rm(i.outputPath,{force:true}),n.deletedFiles.add(a),n.revalidateFiles.delete(a)),delete t.manifest.pages[r];}async function ra(e,t,n,r,s){let i=t.manifest.assets[r];if(!i)return;let a=ce(e.outputDir,i.outputPath);if(!a)throw new Error(`Manifest-owned resource path escaped output root: ${r}`);let c=b.resolve(i.outputPath);Object.entries(t.manifest.assets).some(([l,d])=>l!==r&&!s.has(l)&&b.resolve(d.outputPath)===c)||(await A.rm(i.outputPath,{force:true}),n.deletedFiles.add(a)),delete t.manifest.assets[r];}function sa(e,t){let n=new Set,r=[...t];for(;r.length>0;){let s=r.shift();if(n.has(s))continue;n.add(s);let i=e.assets[s];for(let a of i?.discoveredAssets??[])try{We(new URL(a))&&r.push(a);}catch{}}return n}async function ia(e,t,n,r,s){let i=sa(n.previousManifest,s);for(let a of i){if(t.successfulSitemaps.has(a))continue;let c=n.previousManifest.assets[a];if(!c)continue;let o=ce(e.outputDir,c.outputPath);if(!o)throw new Error(`Manifest-owned sitemap path escaped output root: ${a}`);await A.rm(c.outputPath,{force:true}),r.deletedFiles.add(o),delete n.manifest.assets[a];}await ve(e,n);}async function oa(e){try{let t=await A.readFile(b.join(e.outputDir,"asset-map.json"),"utf8");return JSON.parse(t)}catch{return {}}}async function aa(e){let t=["crawl.log.jsonl","current-crawl-event.json","rejected.jsonl","ignored.jsonl","skipped-http.jsonl","errors.jsonl","timings.jsonl","rejected.json","ignored.json","skipped-http.json","errors.json","timings.json"];await A.mkdir(e,{recursive:true}),await Promise.all(t.map(async n=>{try{await A.unlink(b.join(e,n));}catch(r){if(r.code!=="ENOENT")throw r}}));}function Jr(e,t,n,r,s){let i=J(e,n,e.sourceOrigin,r,"manual-url");i&&(We(i)?Ft(e,t,i.toString(),r,s):H(e,i)?Q(e,t,i.toString(),r,s):Re(i)?ut(e,t,i.toString(),r,s):r.reject("manual-url",i.toString(),"not page/sitemap/asset-like",s));}async function us(e=process.argv.slice(2)){let t=[],n=await ie(void 0,{onRemotePublisherConfigDiagnostic:S=>{t.push(S);}}),r=Ye(n.sitemapPaths),s=t.at(-1)??null,i=Ui(e),a=i.mode==="content-sync"?await ta(i.contentSyncPlanPath):null;if(i.resumeRewrite&&i.mode!=="full")throw new Error("--resume-rewrite is only supported for full crawl/publish jobs.");let c=[];i.mode==="retry-timeouts"&&(c=await Xo(n)),i.mode==="single-url"&&(c=await ea(i.urls)),a&&(c=[...a.directRenderUrls,...a.archiveFamilyUrls,...a.resourceRenderUrls]);let o=i.crawlMode==="incremental"&&i.mode==="full",l=o&&typeof n.subscriptionType=="string"&&n.subscriptionType.trim()!=="";if(a&&n.subscriptionType!=="PROFESSIONAL"&&n.subscriptionType!=="AGENCY")throw new Error("Content-sync requires an active Professional or Agency subscription.");let d=(i.mode==="full"||i.mode==="single-url"||i.mode==="content-sync")&&!i.resumeRewrite,u=i.mode!=="full"||i.resumeRewrite||l,g=u?await oa(n):{},f=l||i.mode==="single-url"||i.mode==="content-sync"?await Hi(n):Zr(),p=new Set(c),y=new Set;if(a){for(let S of a.archiveFamilies)if(S.foundPosts===0)for(let R of S.pageUrls)y.add(R);for(let S of Object.keys(f.pages))na(S,a)&&!p.has(S)&&(c.push(S),y.add(S));}let m={enabled:l,trackManifest:d,manifest:Sn(f),previousManifest:Sn(f),runId:a?.jobId??`${Date.now()}`,seenPages:new Set,changeTokenCache:new Map,manifestSaveChain:Promise.resolve(),strictTargeted:!!a,requiredUrls:p,deleteOnMissingUrls:y};await aa(n.logDir);let h=new Rt(n.logDir,n.logLevel);if(s){let S={status:s.status,virtualAssetBaseUrl:s.virtualAssetBaseUrl,subscriptionType:s.subscriptionType??null,error:s.error,requests:s.requests,source:"remote-publisher-config"};s.status==="loaded"&&s.subscriptionType?h.info("Loaded remote WP Suite publisher configuration.",S):h.warn("Remote WP Suite publisher configuration is unavailable or has no active subscription type.",S);}if(o&&!l){let S="Incremental crawl requires an active remote publisher subscription; refusing to fall back to a destructive full crawl.";throw h.error(S,{requestedMode:i.crawlMode,subscriptionType:n.subscriptionType??null,source:"incremental-subscription-guard"}),await h.flush(),new Error(S)}let w={pageQueue:[],queuedPages:new Set,donePages:new Set,pageDiscoverySources:new Map,assetQueue:[],queuedAssets:new Set,doneAssets:new Set,sitemapQueue:[],queuedSitemaps:new Set,doneSitemaps:new Set,successfulSitemaps:new Set,resolvedSitemapRoots:new Set,sitemapIndexUrls:new Set,sitemapLeafUrls:new Set,sitemapLastmodByPage:{},assetMap:{...g},assetVariantDigestsByOriginalPath:new Map,stats:{pagesQueued:0,pagesRendered:0,pagesCompleted:0,assetsQueued:0,sitemapsQueued:0,assetsSaved:0,pagesSaved:0}},P=Wi(),x=new It,L=Li();if(L&&!i.resumeRewrite&&await At(L,x.artifact()),await Oe(n),await lr(n,{startedAt:new Date().toISOString(),outputDir:b.resolve(n.outputDir)}),u||await A.rm(n.outputDir,{recursive:true,force:true}),await A.mkdir(n.outputDir,{recursive:true}),a){let S=new Set([...a.deleteUrls,...m.deleteOnMissingUrls]);for(let D of a.deleteUrls)await Cn(n,m,P,D,S);let R=new Set(a.resourceDeleteUrls);for(let D of a.resourceDeleteUrls)await ra(n,m,P,D,R);}if(i.resumeRewrite)h.info("Resuming final rewrite from existing output.",{phase:"rewrite-text",mode:i.mode,crawlMode:i.crawlMode});else {let S=await request.newContext({ignoreHTTPSErrors:n.ignoreHttpsErrors});try{if(h.mark("discovery"),i.mode==="full"?(r.forEach(D=>Ft(n,w,D,h)),await Br(n,S,w,h,P,m),n.seedPaths.forEach(D=>Jr(n,w,D,h,"seed-path")),n.generated404RequestPath&&ut(n,w,n.generated404RequestPath,h,"generated-404-request-path")):(c.forEach(D=>Jr(n,w,D,h,"cli")),a?.sitemapUrls.forEach(D=>Ft(n,w,D,h,"content-sync-impact")),await Br(n,S,w,h,P,m,i.mode==="content-sync",a?.sitemapUrls??[]),a&&await ia(n,w,m,P,a.sitemapUrls)),h.endMark("discovery",{pages:w.pageQueue.length,assets:w.assetQueue.length,sitemaps:w.sitemapQueue.length}),h.summary(`Queued ${w.pageQueue.length} pages, ${w.assetQueue.length} assets, ${w.sitemapQueue.length} sitemaps.`,{mode:i.mode,crawlMode:i.crawlMode,queuedPages:w.pageQueue.length,queuedAssets:w.assetQueue.length,queuedSitemaps:w.sitemapQueue.length}),i.mode==="retry-timeouts"&&w.pageQueue.length===0&&w.assetQueue.length===0&&w.sitemapQueue.length===0){h.summary("No timed-out URLs were queued. Skipping retry crawl.",{mode:i.mode,crawlMode:i.crawlMode,queuedPages:0,queuedAssets:0,queuedSitemaps:0}),await h.flush(),L&&await At(L,x.artifact());return}h.mark("render-pages");let R=Array.from({length:Math.max(1,n.concurrency)},()=>Jo(n,w,h,i.mode,S,m,P,x));if(await Promise.all(R),L&&await At(L,x.artifact()),h.endMark("render-pages",{pages:w.stats.pagesCompleted}),h.mark("download-assets"),h.progress("Asset download phase started.",{phase:"download-assets",doneAssets:w.doneAssets.size,assetsQueued:w.stats.assetsQueued,assetQueue:w.assetQueue.length}),await Ji(n,S,m,[...w.assetQueue],h),await Oo(n,S,w,h,m,P),h.endMark("download-assets",{assets:w.doneAssets.size}),m.enabled&&n.maxPages===0&&(await Yi(n,w,m,h,P),i.mode==="full"&&await Xi(n,m,h,P)),i.mode==="full"){let D=await Mo(n,w,h);for(let q of D)te(P,n,q);}}finally{await z("dispose crawl request context",ot(n),()=>S.dispose()).catch(R=>{h.warn("Timed out while disposing crawl request context",{error:String(R)});});}}m.trackManifest&&await ve(n,m),await A.writeFile(b.join(n.outputDir,"asset-map.json"),JSON.stringify(w.assetMap,null,2),"utf8"),JSON.stringify(g,null,2)!==JSON.stringify(w.assetMap,null,2)&&te(P,n,b.join(n.outputDir,"asset-map.json"));let M=i.mode==="single-url"||i.mode==="content-sync"?co(n,w):m.enabled&&!i.resumeRewrite?eo(n,m,P,g,w.assetMap):void 0;h.mark("rewrite-text");let _=i.mode==="single-url"||i.mode==="content-sync"?`Rewriting text files touched by URL crawl: 0/${M?.length??0}`:"Rewriting text files...";h.progress(_,{phase:"rewrite-text",index:0,totalFiles:M?.length,changedTextFiles:0}),await _r(_);let C=Date.now(),F=await Pt(n,w.assetMap,{files:M,previousAssetMap:g,onStart:({totalFiles:S,workerCount:R})=>{h.info(`Rewrite phase scanning ${S} text files.`,{phase:"rewrite-text",totalFiles:S,workerCount:R});},onProgress:async({index:S,totalFiles:R,changedTextFiles:D,file:q,changed:v})=>{v&&te(P,n,b.join(n.outputDir,q)),h.checkpoint(`Rewriting text file ${S}/${R}`,{phase:"rewrite-text",index:S,totalFiles:R,changedTextFiles:D,file:q}),h.debug(`${v?"Rewrote":"Checked unchanged"} text file ${S}/${R}: ${q}`,{phase:"rewrite-text",index:S,totalFiles:R,changedTextFiles:D,file:q,changed:v});let O=Date.now();if(!(S===1||S===R||S%50===0||O-C>=5e3))return;C=O;let B=`Rewriting text files: ${S}/${R}`;h.progress(B,{phase:"rewrite-text",index:S,totalFiles:R,changedTextFiles:D,file:q}),await _r(B);}});m.trackManifest&&(to(m,M),await ve(n,m)),h.endMark("rewrite-text",{changedTextFiles:F});let T=a?[...P.deletedFiles].sort():m.enabled&&!i.resumeRewrite?await ro(n,w,m,P,h):[],j=[...new Set((M||[]).map(S=>ce(n.outputDir,b.isAbsolute(S)?S:b.join(n.outputDir,S))).filter(S=>S!==null))].sort();if(a){let S=[...P.changedFiles].sort(),R=new Set(S),D=new Set(a.directRenderUrls.map(k=>it(n,k,"page")).filter(k=>!!k&&R.has(k))),q=new Set(a.archiveFamilyUrls.map(k=>it(n,k,"page")).filter(k=>!!k&&R.has(k))),v=new Set(a.resourceRenderUrls.map(k=>it(n,k,"asset")).filter(k=>!!k&&R.has(k))),O=new Set([...w.sitemapLeafUrls].map(k=>it(n,k,"asset")).filter(k=>!!k&&R.has(k))),E=new Set([...w.sitemapIndexUrls].map(k=>it(n,k,"asset")).filter(k=>!!k&&R.has(k))),B=new Set([...D,...q,...v,...O,...E]),ye=S.filter(k=>!B.has(k)),xe=[...new Set([...a.directRenderUrls,...a.archiveFamilyUrls,...w.resolvedSitemapRoots,...w.sitemapLeafUrls,...w.sitemapIndexUrls,...a.deleteUrls,...a.resourceRenderUrls,...a.resourceDeleteUrls].map(k=>{let Ie=new URL(k);return `${Ie.pathname}${Ie.search}`}))].sort();await cn(n,{schemaVersion:2,generatedAt:new Date().toISOString(),outputDir:b.resolve(n.outputDir),runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,changedFiles:S,deletedFiles:T,rewriteTargets:j,revalidateFiles:[...P.revalidateFiles].sort(),consumerId:a.consumerId,fromSequence:a.fromSequence,toSequence:a.toSequence,impactHash:a.impactHash,resolvedSitemapUrls:[...w.resolvedSitemapRoots],uploadGroups:{assets:ye,content:[...D].sort(),listings:[...q].sort(),resources:[...v].sort(),sitemapLeaves:[...O].sort(),sitemapIndexes:[...E].reverse()},invalidationPaths:xe});}else await cn(n,{schemaVersion:1,generatedAt:new Date().toISOString(),outputDir:b.resolve(n.outputDir),runMode:i.mode,crawlMode:i.crawlMode,fullSyncRequired:!(m.enabled&&!i.resumeRewrite),changedFiles:m.enabled&&!i.resumeRewrite?[...P.changedFiles].sort():[],deletedFiles:m.enabled&&!i.resumeRewrite?T:[],rewriteTargets:m.enabled&&!i.resumeRewrite?j:[],revalidateFiles:[...P.revalidateFiles].sort()});await pr(n),h.summary(i.resumeRewrite?`Done. Resumed final rewrite over existing output and changed ${F} text files.`:`Done. Rendered ${w.stats.pagesRendered} pages, processed ${w.doneSitemaps.size} sitemaps and ${w.doneAssets.size} assets.`,{mode:i.mode,crawlMode:i.crawlMode,resumeRewrite:i.resumeRewrite,...w.stats,pagesRendered:w.stats.pagesRendered,donePages:w.stats.pagesCompleted,claimedPages:w.donePages.size,doneSitemaps:w.doneSitemaps.size,doneAssets:w.doneAssets.size,changedTextFiles:F}),await h.flush();}function ca(e){let t=e/6e4;if(Number.isInteger(t)&&t>=1)return `${t} minute${t===1?"":"s"}`;let n=Math.ceil(e/1e3);return `${n} second${n===1?"":"s"}`}async function X(e,t,n){let r=new AbortController,s=globalThis.setTimeout(()=>{r.abort(new Error(`${e} timed out after ${ca(t)}.`));},t);try{return await n({abortController:r,httpOptions:{abortSignal:r.signal,requestTimeout:t}})}finally{globalThis.clearTimeout(s);}}function ua(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Ut(e){let t="";for(let r=0;r<e.length;r++){let s=e[r];if(s==="--profile"){let i=(e[r+1]||"").trim();if(!i)throw new Error("Missing value for --profile.");t=i,r+=1;}else if(s.startsWith("--profile=")){let i=s.slice(10).trim();if(!i)throw new Error("Missing value for --profile.");t=i;}}let n=String(process.env.PUBLISHER_DEPLOY_PROFILE||process.env.PUBLISHER_DEPLOYMENT_PROFILE||"").trim();return t||n||null}function la(e,t){return {...e,...t??{}}}function da(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function He(e,t){let n=String(t??e.deploymentTargetOverride??"").trim();if(!ua(e)){if(n)throw new Error(`Deployment target "${n}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:e}}let r=String(n||e.defaultDeploymentProfile||"").trim();if(!r)return {name:null,profile:null,config:e};let s=e.deploymentProfiles?.[r];if(!s)throw new Error(`Unknown deployment profile "${r}". Check the linked WPSuite publisher configuration.`);return {name:r,profile:s,config:{...e,targetOrigin:s.targetOrigin??e.targetOrigin,s3:la(e.s3,s.s3),cloudFront:da(e.cloudFront,s.cloudFront)}}}var fs="wpsuite-sha256",ms="wpsuite-normalized-sha256",ba="public, max-age=0, must-revalidate",Pa=/(?:^|[-_:.])(id|uid|nonce|token|hash|instance)(?:$|[-_:.])/i,En=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,va=["SIGINT","SIGTERM"],_t=new Set,Lt=new Map,ls=null;function Ca(e){_t.add(e);let t=false;return ()=>{t||(t=true,_t.delete(e));}}async function Ra(){let e=[..._t];e.length!==0&&await Promise.allSettled(e.map(async t=>{_t.delete(t),await A.rm(t,{recursive:true,force:true});}));}function xa(){if(!(Lt.size>0))for(let e of va){let t=()=>{ls||(ls=Ra().finally(()=>{for(let[n,r]of Lt)process.off(n,r);Lt.clear(),process.exit(1);}));};Lt.set(e,t),process.on(e,t);}}xa();function Ia(e,t){let n=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(n)return b.resolve(n);let r=t.trim();return r?b.join(b.resolve(r),"crawl-manifest.json"):b.join(b.resolve(e),".crawl-manifest.json")}function qt(e){return b.normalize(e).split(b.sep).join("/")}function ds(e,t){let n=b.relative(b.resolve(e),b.resolve(t));return !n||n.startsWith("..")||b.isAbsolute(n)?null:qt(n)}async function Aa(e,t,n){let r=Ia(e.outputDir,t),s;try{s=await A.readFile(r,"utf8");}catch(d){return d.code==="ENOENT"?(n.warn("Deploy rewrite guard disabled because the crawl manifest is missing.",{manifestPath:r,source:"deploy-guard"}),null):(n.warn("Deploy rewrite guard disabled because the crawl manifest could not be read.",{manifestPath:r,error:d instanceof Error?d.message:String(d),source:"deploy-guard"}),null)}let i;try{i=JSON.parse(s);}catch(d){return n.warn("Deploy rewrite guard disabled because the crawl manifest is invalid JSON.",{manifestPath:r,error:d instanceof Error?d.message:String(d),source:"deploy-guard"}),null}let a=new Map,c=new Map,o=0,l=0;for(let[d,u]of Object.entries(i.pages||{})){let g=String(u?.outputPath||"").trim(),f=g?ds(e.outputDir,g):null;if(f){let p=typeof u?.contentType=="string"&&u.contentType?u.contentType:null;p&&c.set(f,p);}!g||u?.rewriteComplete===true||f&&(o++,a.has(f)||a.set(f,{kind:"page",sourceUrl:d,outputPath:b.resolve(g),relativePath:f}));}for(let[d,u]of Object.entries(i.assets||{})){let g=String(u?.outputPath||"").trim(),f=u?.isText===true,p=g?ds(e.outputDir,g):null;if(p){let y=typeof u?.contentType=="string"&&u.contentType?u.contentType:null;y&&c.set(p,y);}!g||!f||u?.rewriteComplete===true||p&&(l++,a.has(p)||a.set(p,{kind:"asset",sourceUrl:d,outputPath:b.resolve(g),relativePath:p}));}return {manifestPath:r,byRelativePath:a,contentTypeByRelativePath:c,pendingPages:o,pendingTextAssets:l}}function Ta(e){return e&&(En.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 Tn(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,n)=>n%2===1?t:Ta(t)).join("")}function ka(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,n)=>n%2===1?t:En.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 Nt(e,t){return Array.isArray(e)?e.map(n=>Nt(n,t)):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([n,r])=>[n,Nt(r,n)])):typeof e!="string"?e:t&&Pa.test(t)?ka(e):En.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)||/^[0-9]{8,}$/.test(e)?Tn(e):e}function $a(e){return e.replace(/((?:https?:\/\/[^"'\s>]+|\/)(?:wp-content|wp-includes)\/[^"'\s>?]+)\?ver=[A-Za-z0-9._-]+(?=$|[#"'])/gi,"$1")}function Ea(e,t){let n=t.trim();if((n.startsWith("{")||n.startsWith("[")||n.startsWith('"'))&&e.startsWith("data-"))try{let s=JSON.parse(n);return JSON.stringify(Nt(s))}catch{}if(e.startsWith("data-")&&/^[A-Za-z0-9+/=]+$/.test(n))try{let s=Buffer.from(n,"base64").toString("utf8"),i=JSON.parse(s),a=JSON.stringify(Nt(i));return Buffer.from(a,"utf8").toString("base64")}catch{}return t}function Ma(e,t){let n=e.toLowerCase();return n==="class"?t.split(/(\s+)/).map((r,s)=>s%2===1?r:Tn(r)).join(""):n==="id"||n==="for"||n==="aria-controls"||n==="aria-labelledby"||n==="aria-describedby"||n==="aria-owns"||n==="aria-activedescendant"||n==="name"?Tn(t):n==="href"||n==="src"?$a(t):Ea(n,t)}function Fa(e){return e.replace(/<([a-z][^\s/>]*)([^<>]*?)>/gi,(t,n,r)=>{if(n.startsWith("/"))return t;let s=r.replace(/\s([:@a-zA-Z_][-:.a-zA-Z0-9_]*?)=(['"])([\s\S]*?)\2/g,(i,a,c,o)=>{let l=Ma(a,o);return ` ${a}=${c}${l}${c}`});return `<${n}${s}>`})}function An(e){let t=b.extname(e).toLowerCase();return [".html",".htm",".xml",".xsl",".txt"].includes(t)||["robots.txt","sitemap.xml"].includes(b.basename(e))}function jt(e,t){return [e.replace(/^\/+|\/+$/g,""),t.replace(/^\/+/,"")].filter(Boolean).join("/")}async function Da(e,t,n,r){let s=new Map,i,a=0;do{a++;let c=await X(`S3 list objects page ${a} for s3://${t}/${n||""}`,3e5,({httpOptions:o})=>e.send(new ListObjectsV2Command({Bucket:t,Prefix:n||void 0,ContinuationToken:i}),o));for(let o of c.Contents||[]){if(!o.Key)continue;let l=(o.ETag||"").replace(/^"|"$/g,"").toLowerCase(),d=Number(o.Size??0);s.set(o.Key,{etag:l,size:d,cacheControl:void 0});}i=c.NextContinuationToken,r.debug(`Listed S3 keys page ${a}: ${s.size} keys so far`);}while(i);return s}async function Oa(e,t,n,r){if(r.has(n))return r.get(n)??null;try{let s=await X(`S3 head object ${n}`,3e5,({httpOptions:a})=>e.send(new HeadObjectCommand({Bucket:t,Key:n}),a)),i={etag:(s.ETag||"").replace(/^"|"$/g,"").toLowerCase(),size:Number(s.ContentLength??0),cacheControl:s.CacheControl?.trim()||null};return r.set(n,i),i}catch{return r.set(n,null),null}}async function Ua(e,t,n,r){if(r.cacheControl!==void 0)return r.cacheControl;try{let s=await X(`S3 head cache metadata ${n}`,3e5,({httpOptions:i})=>e.send(new HeadObjectCommand({Bucket:t,Key:n}),i));r.cacheControl=s.CacheControl?.trim()||null;}catch{r.cacheControl=null;}return r.cacheControl}function ps(e){return String(e||"").split(",").map(t=>t.trim().toLowerCase()).filter(Boolean).sort().join(",")}async function La(e,t,n,r){let s=[...new Set(Array.from(n).filter(Boolean))];if(s.length===0)return r.info("No explicitly tombstoned S3 objects to delete."),[];r.info(`Deleting ${s.length} explicitly tombstoned S3 object(s)\u2026`);for(let i=0;i<s.length;i+=1e3){let a=s.slice(i,i+1e3);if(a.length===0)continue;let c=await X(`S3 delete objects batch ${Math.floor(i/1e3)+1}`,3e5,({httpOptions:o})=>e.send(new DeleteObjectsCommand({Bucket:t,Delete:{Objects:a.map(l=>({Key:l}))}}),o));if(c.Errors&&c.Errors.length>0){let o=c.Errors.map(l=>`${l.Key||"(unknown)"}: ${l.Code||"DeleteFailed"}`);throw new Error(`S3 reported ${o.length} object deletion error(s): ${o.join(", ")}`)}r.info(`Deleted batch of ${a.length} object(s).`);for(let o of a)r.debug(` deleted: ${o}`);}return s}function ja(e){return createHash("md5").update(e).digest("hex")}function gs(e){return createHash("sha256").update(e).digest("hex")}function kn(e){return createHash("sha256").update(Fa(e.toString("utf8"))).digest("hex")}async function qa(e,t,n,r){if(r.has(n))return r.get(n)??null;try{let a=((await X(`S3 head object metadata ${n}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:n}),c))).Metadata?.[fs]||"").trim().toLowerCase()||null;return r.set(n,a),a}catch{return r.set(n,null),null}}async function _a(e,t,n,r,s){if(r.has(n))return r.get(n)??null;try{let a=((await X(`S3 head object normalized metadata ${n}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:n}),c))).Metadata?.[ms]||"").trim().toLowerCase();if(a)return r.set(n,a),a}catch{return r.set(n,null),null}if(s.has(n)){let i=s.get(n)??null;return r.set(n,i),i}try{let a=await(await X(`S3 get object ${n}`,3e5,({httpOptions:o})=>e.send(new GetObjectCommand({Bucket:t,Key:n}),o))).Body?.transformToByteArray(),c=a?kn(Buffer.from(a)):null;return s.set(n,c),r.set(n,c),c}catch{return s.set(n,null),r.set(n,null),null}}function Na(e){return e.replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"")||"profile"}function Ba(e,t){let n=e,r=Object.entries(t).sort((s,i)=>i[0].length-s[0].length);for(let[s,i]of r)s&&(n=n.split(s).join(i));return n}async function Wa(e,t,n,r){let s=b.join(e,"asset-map.json"),i;try{i=await A.readFile(s,"utf8");}catch(o){if(o.code==="ENOENT")return;throw o}let a=JSON.parse(i);if(!a||typeof a!="object")return;let c=Object.fromEntries(Object.entries(a).map(([o,l])=>{let d=String(l??"");return t.urlRewriteMode==="absolute"&&t.targetOrigin&&n.targetOrigin&&t.targetOrigin!==n.targetOrigin&&(d=d.split(t.targetOrigin).join(n.targetOrigin)),d=Ba(d,r),[o,d]}));await A.writeFile(s,JSON.stringify(c,null,2),"utf8");}async function Ha(e,t,n,r,s,i){let a=e.targetOrigin!==t.targetOrigin,c=Object.keys(r).length>0;if(!n||!a&&!c)return {outputDir:e.outputDir,cleanup:async()=>{}};if(a&&e.urlRewriteMode!=="absolute")throw new Error(`Deployment profile "${n}" changes targetOrigin, but the base crawl output must use urlRewriteMode "absolute" for multi-target redeploys.`);let o=b.join(b.dirname(e.outputDir),`.deploy-profile-${Na(n)}-${Date.now()}`),l=Ca(o);s.info(`Preparing deployment profile "${n}" from existing crawl output to ${o}...`);try{if(await A.rm(o,{recursive:!0,force:!0}),i){await A.mkdir(o,{recursive:!0});for(let f of i){let p=b.join(e.outputDir,f),y=b.join(o,f);await A.mkdir(b.dirname(y),{recursive:!0}),await A.copyFile(p,y);}}else await A.cp(e.outputDir,o,{recursive:!0});let d={...e,sourceOrigin:e.targetOrigin,targetOrigin:t.targetOrigin,extraReplacements:r,outputDir:o},u=1;s.info(`Rewriting text files for deployment profile "${n}"...`);let g=await Pt(d,{},{files:i,onStart:({totalFiles:f,workerCount:p})=>{u=p,s.info(`Rewriting ${f} text file(s) for deployment profile "${n}" with ${p} worker(s).`);},onProgress:({index:f,totalFiles:p,changedTextFiles:y,file:m,changed:h})=>{let P=`Rewriting [${f}/${p}] ${m} (${h?"rewrote":"checked"}; ${y} changed so far)`;p<=200||f<=5||f%10===0||f===p?(s.progress(P,{file:m,index:f,totalFiles:p,changed:h,changedTextFiles:y,workerCount:u}),s.info(P)):s.debug(P);}});return await Wa(o,e,t,r),s.info(`Prepared deployment profile "${n}" in ${o} (${g} rewritten text file(s)).`),{outputDir:o,cleanup:async()=>{l(),await A.rm(o,{recursive:!0,force:!0});}}}catch(d){throw s.error(`Failed to prepare deployment profile "${n}" temporary output.`,{tempDir:o,outputDir:e.outputDir,error:d instanceof Error?d.message:String(d),...d instanceof Error&&d.stack?{stack:d.stack}:{}}),await A.rm(o,{recursive:true,force:true}).catch(()=>{}),l(),await s.flush().catch(()=>{}),d}}async function hs(e=process.argv.slice(2)){let t=await ie(),n=Ut(e),r=He(t,n),s=b.resolve(r.config.logDir||"logs"),i=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",a=i?b.join(b.resolve(i),"deploy-diff.json"):"",c=new Le(s,"deploy.log.jsonl",r.config.logLevel??"info"),o=r.profile?.extraReplacements??{},l=async()=>{};try{let d=await De(t);if(e.includes("--content-sync")&&d?.schemaVersion!==2)throw new Error("Content-sync deployment requires a valid schema-2 targeted deploy plan.");let g=await dr(t);if(g)throw new Error(`Deployment blocked: the crawl started at ${g.startedAt} did not complete its final rewrite. Run a successful crawl or resume the rewrite before deploying.`);let f=await Ha(t,r.config,r.name,o,c,d?.schemaVersion===2?d.changedFiles:void 0);l=f.cleanup;let p={...r.config,outputDir:f.outputDir},y=p.s3SyncMode??"sdk-upload-delete";if(d?.schemaVersion===2&&d.deletedFiles.length>0&&y!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let m=d?.schemaVersion!==2&&f.outputDir!==t.outputDir&&(t.targetOrigin!==r.config.targetOrigin||Object.keys(o).length>0),h=!!d&&!d.fullSyncRequired&&!m;if(d?.schemaVersion===2&&!h)throw new Error("Content-sync deployment cannot widen into a full-output deployment.");r.name&&c.info(`Using deployment profile: ${r.name}`),c.info(`Starting deploy \u2014 s3://${p.s3.bucket}/${p.s3.prefix||""} (region: ${p.s3.region})`),c.info(`S3 sync mode: ${y}`),d&&d.fullSyncRequired?c.info("Deploy plan requests a full sync; scanning the whole output tree."):d&&m?c.info("Ignoring incremental deploy plan because deployment-profile rewrites require a full sync."):h&&c.info(`Using incremental deploy plan: ${d.changedFiles.length} changed file(s), ${d.deletedFiles.length} deleted file(s).`),c.mark("deploy");let w=new S3Client({region:p.s3.region}),P=f.outputDir===t.outputDir?await Aa(t,i,c):null,x=d?.schemaVersion===2?[...d.uploadGroups.assets,...d.uploadGroups.content,...d.uploadGroups.listings,...d.uploadGroups.resources,...d.uploadGroups.sitemapLeaves,...d.uploadGroups.sitemapIndexes]:d?.changedFiles??[];if(d?.schemaVersion===2){let $=[...new Set(x)],K=[...new Set(d.changedFiles)];if($.length!==K.length||K.some(I=>!$.includes(I)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let L=h?(await Promise.all(x.map(async $=>{try{return (await A.stat(b.join(p.outputDir,$))).isFile()?$:null}catch{if(d?.schemaVersion===2)throw new Error(`Content-sync planned upload is missing: ${$}`);return null}}))).filter($=>$!==null):await ri(["**/*"],{cwd:p.outputDir,onlyFiles:!0,dot:!0}),M=[],_=[],C=L.filter($=>{let K=P?.byRelativePath.get(qt($));if(!K)return !0;let I=jt(p.s3.prefix,$);return M.push($),_.push(I),c.reject(K.kind,K.sourceUrl||$,"rewrite not completed; deploy upload blocked","deploy-guard",{file:$,key:I,outputPath:K.outputPath,manifestPath:P?.manifestPath}),!1}),F=new Set((d?.revalidateFiles||[]).map(qt)),T=new Set((h?C:L).map($=>jt(p.s3.prefix,$)));P&&(P.pendingPages>0||P.pendingTextAssets>0)&&c.info(`Deploy rewrite guard loaded ${P.pendingPages} pending page(s) and ${P.pendingTextAssets} pending text asset(s) from crawl manifest.`,{manifestPath:P.manifestPath,pendingPages:P.pendingPages,pendingTextAssets:P.pendingTextAssets,source:"deploy-guard"}),M.length>0&&c.warn(`Deploy rewrite guard blocked ${M.length} file(s) from upload because final crawl rewrite is incomplete.`,{rejected:M.length,manifestPath:P?.manifestPath,source:"deploy-guard"}),h?c.info(`Using ${C.length} eligible planned changed file(s) from deploy plan for upload${M.length>0?` (${M.length} blocked by deploy rewrite guard)`:""}.`):c.info(`Found ${L.length} file(s) in output directory "${p.outputDir}"${M.length>0?`; ${C.length} remain eligible for upload after deploy rewrite guard filtering`:""}.`),c.progress("Deploy preflight: comparing local output with remote S3 state.",{phase:"deploy",mode:y,totalFiles:C.length,scannedFiles:L.length,rejected:M.length,deploymentProfile:r.name||"",s3Bucket:p.s3.bucket,s3Prefix:p.s3.prefix||"",targetedPlan:h,deployGuardEnabled:f.outputDir===t.outputDir});let j=0,S=0,R=0,D=[],q=[],v=[],O=[],E=[],B=new Map,ye=new Map,xe=new Map,k=new Map,Ie=new Map;h||(c.info("Listing existing S3 objects to detect unchanged files..."),B=await Da(w,p.s3.bucket,p.s3.prefix,c),c.info(`Found ${B.size} existing key(s) in S3.`));let zt=async $=>B.has($)?B.get($)??null:h?Oa(w,p.s3.bucket,$,ye):null;for(let $ of C){let K=b.join(p.outputDir,$),I=jt(p.s3.prefix,$),ne=P?.contentTypeByRelativePath.get($),Qe=ne&&ne!=="application/octet-stream"?ne:Sa.lookup($)||ne||"application/octet-stream",Ln=F.has(qt($)),jn=Ln?ba:An($)?p.s3.htmlCacheControl:p.s3.assetCacheControl,W=j+S+R+1;C.length<=200||W<=5||W%10===0||W===C.length?(c.progress(`Uploading [${W}/${C.length}] ${I} (${Qe})`,{file:$,key:I,index:W,totalFiles:C.length,contentType:Qe,mode:y}),c.info(`Uploading [${W}/${C.length}] ${I} (${Qe})`)):c.debug(`Uploading [${W}/${C.length}] ${I} (${Qe})`);try{let re=await A.readFile(K),$e=await zt(I),Je=null,Ee=null;if($e){let Ve=!Ln||ps(await Ua(w,p.s3.bucket,I,$e))===ps(jn),Rs=re.byteLength;if($e.size===Rs){let gt=$e.etag.includes("-");if($e.etag!==""&&!gt){let xs=ja(re);if($e.etag===xs&&Ve){S++,C.length<=200||S<=5||S%25===0?(c.progress(`Skipped unchanged [${W}/${C.length}] ${I}`,{file:$,key:I,index:W,totalFiles:C.length,uploaded:j,skipped:S,failed:R,mode:y,reason:"etag-md5-match"}),c.info(`Skipped unchanged [${W}/${C.length}] ${I}`)):c.debug(`Skipped unchanged [${W}/${C.length}] ${I}`),q.push(I);continue}}Je=gs(re);let qn=await qa(w,p.s3.bucket,I,xe);if(qn&&qn===Je&&Ve){S++,C.length<=200||S<=5||S%25===0?(c.progress(`Skipped unchanged [${W}/${C.length}] ${I}`,{file:$,key:I,index:W,totalFiles:C.length,uploaded:j,skipped:S,failed:R,mode:y,reason:"metadata-sha256-match"}),c.info(`Skipped unchanged [${W}/${C.length}] ${I}`)):c.debug(`Skipped unchanged [${W}/${C.length}] ${I}`),q.push(I);continue}}if(An($)){Ee=kn(re);let gt=await _a(w,p.s3.bucket,I,k,Ie);if(gt&&gt===Ee&&Ve){S++,C.length<=200||S<=5||S%25===0?(c.progress(`Skipped semantically unchanged [${W}/${C.length}] ${I}`,{file:$,key:I,index:W,totalFiles:C.length,uploaded:j,skipped:S,failed:R,mode:y,reason:"normalized-html-match"}),c.info(`Skipped semantically unchanged [${W}/${C.length}] ${I}`)):c.debug(`Skipped semantically unchanged [${W}/${C.length}] ${I}`),q.push(I);continue}}}Je=Je??gs(re),Ee=Ee??(An($)?kn(re):null),await X(`S3 upload ${I}`,9e5,({abortController:Ve})=>new Upload({client:w,abortController:Ve,params:{Bucket:p.s3.bucket,Key:I,Body:re,ContentType:Qe,CacheControl:jn,Metadata:{[fs]:Je,...Ee?{[ms]:Ee}:{}}}}).done()),j++,D.push(I),(j%25===0||j===C.length)&&(c.progress(`Uploaded ${j}/${C.length} file(s)\u2026`,{uploaded:j,skipped:S,failed:R,totalFiles:C.length,mode:y}),c.info(`Uploaded ${j}/${C.length} file(s)\u2026`));}catch(re){R++,v.push(I),c.error(`Failed to upload ${I}: ${re.message}`,{key:I,error:String(re)});}}if(c.info(`Upload complete: ${j} uploaded, ${S} skipped unchanged, ${_.length} rejected by guard, ${R} failed.`),v.length>0||_.length>0)throw new Error(`Deployment stopped before deletion because ${v.length} upload(s) failed and ${_.length} upload(s) were rejected.`);if(y==="sdk-upload-delete"){let $=new Set((d?.deletedFiles??[]).map(I=>jt(p.s3.prefix,I))),K=[...$].filter(I=>!T.has(I));if(!h&&(E=[...B.keys()].filter(ne=>!T.has(ne)).filter(ne=>!$.has(ne)),E.length>0)){c.warn(`Protected ${E.length} remote-only S3 object(s) from deletion because the crawl did not provide explicit tombstones.`,{protected:E.length,source:"deploy-delete-guard"});for(let ne of E)c.debug(` protected remote-only object: ${ne}`);}O=await La(w,p.s3.bucket,h?K:K.filter(I=>B.has(I)),c);}else c.info("Sync mode is sdk-upload-only \u2014 skipping stale object deletion.");a&&(await A.mkdir(b.dirname(a),{recursive:!0}),await A.writeFile(a,JSON.stringify({generatedAt:new Date().toISOString(),mode:y,summary:{totalFiles:L.length,eligibleFiles:C.length,uploaded:D.length,skipped:q.length,rejected:_.length,failed:v.length,deleted:O.length,protected:E.length,...r.name?{profile:r.name}:{}},uploadedKeys:D,skippedKeys:q,rejectedKeys:_,failedKeys:v,deletedKeys:O,protectedRemoteOnlyKeys:E},null,2),"utf8")),d?.schemaVersion!==2&&await Oe(t),c.endMark("deploy"),await c.flush();}finally{await l().catch(()=>{});}}function Qa(e){let t=e;return t.endsWith("/")?t+="index.html":b.posix.basename(t).includes(".")||(t+="/index.html"),t}function Ja(e,t){let n=t.trim().replace(/^\/+|\/+$/g,"");if(!n)return null;let r=`/${n}`;return e===r||e.startsWith(`${r}/`)?e:`${r}${e.startsWith("/")?e:`/${e}`}`}function ws(e,t){let n=new Set;for(let r of e){let s=new URL(r,"https://content-sync.invalid");if(s.origin!=="https://content-sync.invalid")continue;let i=`${s.pathname}${s.search}`,a=Qa(s.pathname);n.add(i),n.add(`${a}${s.search}`);let c=Ja(a,t);c&&n.add(`${c}${s.search}`);}return [...n].sort()}function Za(){let e=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();return e?b.join(b.resolve(e),"content-sync-invalidation.json"):""}async function Mn(e,t){if(!e)return;await A.mkdir(b.dirname(e),{recursive:true});let n=`${e}.${process.pid}.${Date.now()}.tmp`;await A.writeFile(n,JSON.stringify({...t,updatedAt:new Date().toISOString()},null,2),"utf8"),await A.rename(n,e);}async function Ya(e,t,n,r){if(!e)return null;try{let s=JSON.parse(await A.readFile(e,"utf8"));if(s.schemaVersion!==1||s.distributionId!==t||s.impactHash!==n||!Array.isArray(s.batches))throw new Error("Content-sync invalidation state does not match the deploy plan.");let i=s.batches.flatMap(a=>a.paths);return i.length!==r.length||i.some((a,c)=>a!==r[c])?null:s}catch(s){if(s.code==="ENOENT")return null;throw s}}function Xa(e,t=3e3){let n=[];for(let r=0;r<e.length;r+=t)n.push(e.slice(r,r+t));return n}async function Ss(e=process.argv.slice(2)){let t=await ie(),n=Ut(e),r=He(t,n),s=r.config,i=new Le(s.logDir||"logs","invalidate.log.jsonl",s.logLevel??"info"),a=e.includes("--content-sync"),c=await De(t);if(a&&c?.schemaVersion!==2)throw new Error("Content-sync invalidation requires a valid schema-2 targeted deploy plan.");if(r.name&&i.info(`Using deployment profile: ${r.name}`),!s.cloudFront.distributionId){i.info("No CloudFront distributionId configured; skipping invalidation."),await i.flush();return}let o=c?.schemaVersion===2?c.invalidationPaths:s.cloudFront.invalidationPaths.length>0?s.cloudFront.invalidationPaths:["/*"],l=c?.schemaVersion===2?ws(o,s.s3.prefix||""):o;if(c?.schemaVersion===2&&l.some(p=>p==="/*"))throw new Error("Content-sync cannot use a whole-distribution invalidation.");if(l.length===0){i.info("The targeted deploy plan has no CloudFront paths to invalidate."),await i.flush();return}let d=s.cloudFront.distributionId,u=new CloudFrontClient({region:"us-east-1",maxAttempts:5});if(i.mark("invalidate"),c?.schemaVersion!==2){let p=`smartcloud-static-publisher-${Date.now()}`;await X(`CloudFront invalidation for distribution ${d}`,3e5,({httpOptions:y})=>u.send(new CreateInvalidationCommand({DistributionId:d,InvalidationBatch:{CallerReference:p,Paths:{Quantity:l.length,Items:l}}}),y)),i.endMark("invalidate"),await i.flush();return}let g=Za(),f=await Ya(g,d,c.impactHash,l);if(!f){let p=createHash("sha256").update(JSON.stringify(l)).digest("hex");f={schemaVersion:1,distributionId:d,impactHash:c.impactHash,updatedAt:new Date().toISOString(),batches:Xa(l).map((y,m)=>({index:m,callerReference:`wpsuite-content-sync-${c.impactHash.slice(0,24)}-${p.slice(0,24)}-${m}`,paths:y,invalidationId:null,status:"pending"}))},await Mn(g,f);}for(let p of f.batches){if(!p.invalidationId){let y=await X(`CloudFront targeted invalidation batch ${p.index+1}`,3e5,({httpOptions:h})=>u.send(new CreateInvalidationCommand({DistributionId:d,InvalidationBatch:{CallerReference:p.callerReference,Paths:{Quantity:p.paths.length,Items:p.paths}}}),h)),m=String(y.Invalidation?.Id||"").trim();if(!m)throw new Error("CloudFront did not return an invalidation ID.");p.invalidationId=m,p.status="created",await Mn(g,f);}await waitUntilInvalidationCompleted({client:u,maxWaitTime:900,minDelay:2,maxDelay:15},{DistributionId:d,Id:p.invalidationId}),p.status="completed",await Mn(g,f),i.info(`CloudFront invalidation batch ${p.index+1}/${f.batches.length} completed.`,{invalidationId:p.invalidationId,pathCount:p.paths.length});}i.endMark("invalidate"),await i.flush();}function Ht(e){let t=new URL(e);return t.hash="",t.href}function bs(e,t,n){let r=new Set([...t].map(o=>Ht(o))),s=new Map,i=new Map,a=o=>{s.set(Ht(o),o);},c=o=>{if(!o)return;let l=Ht(o);s.has(l)||i.set(l,o);};for(let o of e.deleteUrls)a(o);for(let o of e.resourceDeleteUrls)a(o);for(let o of e.archiveFamilies)if(o.foundPosts===0)for(let l of o.pageUrls)r.has(Ht(l))||a(l);for(let o of e.directRenderUrls)c(o);for(let o of e.resourceRenderUrls)c(o);for(let o of e.archiveFamilies)c(o.pageUrls[0]),c(o.pageUrls.at(-1));for(let o of n)c(o);return {expectedAvailable:[...i.values()],expectedMissing:[...s.values()]}}var vs=["claimed","impact-planned","rendered","deployed","invalidated","verified","acknowledged"];function On(){let e=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();if(!e)throw new Error("Content-sync requires a runtime directory.");return b.resolve(e)}function ze(e,t){return vs.indexOf(e)>=vs.indexOf(t)}async function we(e,t,n,r,s={}){let i={timestamp:new Date().toISOString(),eventType:t,status:t.endsWith("failed")?"failed":"success",actorSource:"content-sync",jobId:n.id||"",command:"content-sync",message:r,details:s};await A.mkdir(e,{recursive:true}),await A.appendFile(b.join(e,"audit-events.jsonl"),`${JSON.stringify(i)}
14
+ `,"utf8");}function tc(e){return Ke({postTypes:e.postTypes??[],listingPaths:e.listingPaths??[],includeSubsites:e.includeSubsites,includePostTypeArchives:e.includePostTypeArchives,includeTaxonomyArchives:e.includeTaxonomyArchives,includeAuthorArchives:e.includeAuthorArchives,includeDateArchives:e.includeDateArchives,includePostsPage:e.includePostsPage,includeSitemapChain:e.includeSitemapChain})}function nc(e,t){let n=(e.scheduler?.rules??[]).find(r=>r.command==="content-sync"&&r.id===t);if(!n)throw new Error(`Content-sync scheduler rule not found: ${t}`);if(n.enabled===false)throw new Error(`Content-sync scheduler rule is disabled: ${t}`);return n}async function Un(e,t){if(e.subscriptionType!=="PROFESSIONAL"&&e.subscriptionType!=="AGENCY")throw new Error("Content-sync requires an active Professional or Agency subscription.");let n=tc(t),r=He(e,t.deploymentProfile),s=r.name||"",i=Bn(e.sourceOrigin,String(t.id||""),n),a=Wn({sourceOrigin:e.sourceOrigin,targetOrigin:r.config.targetOrigin,deploymentProfile:s,s3Bucket:r.config.s3.bucket,s3Prefix:r.config.s3.prefix,cloudFrontDistributionId:r.config.cloudFront.distributionId,urlRewriteMode:r.config.urlRewriteMode,extraReplacements:r.profile?.extraReplacements??{}}),c=Hn({siteId:e.wpsuite?.siteSettings?.siteId,accountId:e.wpsuite?.siteSettings?.accountId,sourceOrigin:e.sourceOrigin,ruleId:String(t.id||""),targetFingerprint:a}),o=`content-sync:${String(t.id||"")}:${pe({scopeFingerprint:i,targetFingerprint:a}).slice(0,32)}`,l=new mt({sourceOrigin:e.sourceOrigin,runtimeToken:String(e.wpsuite?.runtimeToken||""),ignoreHttpsErrors:e.ignoreHttpsErrors}),d=await l.releaseFingerprint(n.includeSubsites),u=Nn({wordpressFingerprint:d,targetFingerprint:a,sourceOrigin:e.sourceOrigin,sitemapPaths:Ye(e.sitemapPaths),blockedPathPrefixes:e.blockedPathPrefixes,assetPathPrefixes:e.assetPathPrefixes,noJavaScriptRenderPathPrefixes:e.noJavaScriptRenderPathPrefixes,urlRewriteMode:e.urlRewriteMode});return {rule:t,scope:n,deployment:r,deploymentProfile:s,scopeFingerprint:i,targetFingerprint:a,consumerId:c,coalesceKey:o,client:l,releaseFingerprint:u}}async function rc(e){let t=On(),n=ht(t),r=await ie(),s=(r.scheduler?.rules??[]).filter(l=>l.command==="content-sync"&&l.enabled!==false);if(s.length===0)return 0;let i=await Gt(n),a=await wt(n),c=await Kt(n),o=0;for(let l of s){let d=await Un(r,l),u=c.entries[d.coalesceKey];if(!u||u.releaseJobId!==e||u.scopeFingerprint!==d.scopeFingerprint||u.targetFingerprint!==d.targetFingerprint||u.releaseFingerprint!==d.releaseFingerprint)throw new Error(`The normal release has no matching pre-crawl content-sync cutoff for rule ${String(l.id||"")}.`);let g=pe({releaseJobId:e,releaseFingerprint:d.releaseFingerprint,scopeFingerprint:d.scopeFingerprint,targetFingerprint:d.targetFingerprint,sequence:u.sequence}),f=await d.client.establishBaseline({consumerId:d.consumerId,scopeFingerprint:d.scopeFingerprint,baselineId:g,postTypes:d.scope.postTypes,includeSubsites:d.scope.includeSubsites,sequence:u.sequence}),p={schemaVersion:1,ruleId:String(l.id||""),consumerId:d.consumerId,coalesceKey:d.coalesceKey,scopeFingerprint:d.scopeFingerprint,targetFingerprint:d.targetFingerprint,releaseFingerprint:d.releaseFingerprint,baselineId:g,deploymentProfile:d.deploymentProfile,committedSequence:f.committedSequence,releaseJobId:e,verifiedAt:new Date().toISOString()};i.entries[d.coalesceKey]=p;let y=a.rules[d.coalesceKey];a.rules[d.coalesceKey]={ruleId:String(l.id||""),consumerId:d.consumerId,coalesceKey:d.coalesceKey,committedSequence:f.committedSequence,observedHeadSequence:f.headSequence,lag:Math.max(0,f.headSequence-f.committedSequence),lastSuccessfulJobId:y?.lastSuccessfulJobId||e,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:f.headSequence>f.committedSequence,retryAttempt:0,nextRetryAt:null,lastError:null,baselineStatus:"ready",baselineReason:null};let m=await er(n,d.coalesceKey);await we(t,"content-sync-baseline-established",{id:e},"A normal release established a verified content-sync baseline.",{ruleId:l.id,consumerId:d.consumerId,committedSequence:f.committedSequence,supersededCurrent:m}),o++;}return await Kn(n,i),await yt(n,a),await A.rm(n.releaseCutoff,{force:true}),o}async function sc(e){let t=On(),n=ht(t),r=await ie(),s=(r.scheduler?.rules??[]).filter(c=>c.command==="content-sync"&&c.enabled!==false);if(s.length===0)return 0;let i=await Kt(n),a=0;for(let c of s){let o=await Un(r,c),l=Object.values(i.entries).find(g=>g.releaseJobId===e&&g.ruleId===String(c.id||""));if(l){if(l.coalesceKey===o.coalesceKey&&l.scopeFingerprint===o.scopeFingerprint&&l.targetFingerprint===o.targetFingerprint&&l.releaseFingerprint===o.releaseFingerprint){a++;continue}throw new Error(`The normal release scope changed after its journal cutoff was captured for rule ${String(c.id||"")}. Start a new normal publish instead of resuming this release.`)}let d=await o.client.unboundHead(o.scope.postTypes,o.scope.includeSubsites),u={schemaVersion:1,releaseJobId:e,ruleId:String(c.id||""),coalesceKey:o.coalesceKey,scopeFingerprint:o.scopeFingerprint,targetFingerprint:o.targetFingerprint,releaseFingerprint:o.releaseFingerprint,sequence:d.headSequence,capturedAt:new Date().toISOString()};i.entries[o.coalesceKey]=u,await we(t,"content-sync-release-cutoff-captured",{id:e},"A normal release captured its pre-crawl content-sync journal cutoff.",{ruleId:c.id,consumerId:o.consumerId,sequence:d.headSequence}),a++;}return await Vn(n,i),a}async function ic(e,t){let n=JSON.parse(await A.readFile(b.join(e,"current-run.json"),"utf8"));if(n.id!==t||n.command!=="content-sync"||!String(n.ruleId||"").trim())throw new Error("The active queue job is not the requested content-sync job.");return n}async function Dn(e,t,n=0){let r=e.protocol==="https:"?Jn:Fs,s=e.protocol==="https:"?new Jn.Agent({rejectUnauthorized:!t}):void 0;return new Promise((i,a)=>{let c=r.get(e,{agent:s,timeout:3e4,headers:{"user-agent":"WP-Suite-Content-Sync/1"}},o=>{let l=o.statusCode??500,d=o.headers.location;if(l>=300&&l<400&&d&&n<5){o.resume(),Dn(new URL(d,e),t,n+1).then(i,a);return}let u=[],g=0;o.on("data",f=>{g+=f.byteLength,g<=1048576&&u.push(f);}),o.on("error",a),o.on("end",()=>i({status:l,body:Buffer.concat(u).toString("utf8"),contentType:String(o.headers["content-type"]||"")}));});c.on("timeout",()=>c.destroy(new Error(`Verification timed out: ${e}`))),c.on("error",a);})}async function oc(e,t,n,r){let s=t.deployment.config.targetOrigin;if(!/^https?:\/\//i.test(s))throw new Error("Content-sync verification requires an absolute HTTP(S) target origin.");let i=JSON.parse(await A.readFile(r,"utf8")),a=new Set;for(let[d,u]of Object.entries(i.pages??{}))a.add(d),typeof u.url=="string"&&a.add(u.url);let c=await De(e);if(c?.schemaVersion!==2||c.impactHash!==n.impactHash)throw new Error("Content-sync verification requires the matching targeted deploy plan.");let o=bs(n,a,c.resolvedSitemapUrls),l=new Set(n.resourceRenderUrls.map(d=>{let u=new URL(d);return u.hash="",u.href}));for(let d of o.expectedAvailable){let u=new URL(d),g=new URL(`${u.pathname}${u.search}`,s),f=await Dn(g,e.ignoreHttpsErrors);if(f.status<200||f.status>=400)throw new Error(`Content-sync verification failed (${f.status}): ${g}`);let p=new URL(d);if(p.hash="",l.has(p.href)){if(!f.contentType.toLowerCase().startsWith("application/json"))throw new Error(`Resource verification expected application/json: ${g}`);try{JSON.parse(f.body);}catch{throw new Error(`Resource verification received invalid JSON: ${g}`)}}}for(let d of o.expectedMissing){let u=new URL(d),g=new URL(`${u.pathname}${u.search}`,s),f=await Dn(g,e.ignoreHttpsErrors);if(f.status>=200&&f.status<300)throw new Error(`Deleted content is still publicly available: ${g}`)}}async function ac(e){let t=await A.readFile(e.candidateManifest,"utf8");JSON.parse(t);let n=`${e.trustedManifest}.${process.pid}.tmp`;await A.writeFile(n,t,"utf8"),await A.rename(n,e.trustedManifest);}async function cc(e,t){try{let n=JSON.parse(await A.readFile(e.candidateManifest,"utf8"));if([...Object.values(n.pages??{}),...Object.values(n.assets??{})].some(s=>s?.lastSeenRunId===t))return "resumed"}catch(n){if(n.code!=="ENOENT")throw new Error(`The content-sync candidate manifest is invalid: ${n instanceof Error?n.message:String(n)}`,{cause:n})}return await A.copyFile(e.trustedManifest,e.candidateManifest).catch(n=>{throw new Error(`A trusted crawl manifest is required before content-sync: ${n instanceof Error?n.message:String(n)}`,{cause:n})}),"initialized"}async function ke(e,t,n,r,s={},i=0,a=async()=>{}){t.phase=n,await Gn(e,t),await Xn(e,{schemaVersion:1,jobId:t.jobId,phase:n,completedItems:r,totalItems:r,attempt:i,updatedAt:new Date().toISOString(),details:s}),await we(b.dirname(e.state),"content-sync-checkpoint",{id:t.jobId},`Content-sync checkpoint completed: ${n}.`,{phase:n,completedItems:r,totalItems:r,attempt:i,...s}),await a(n,t);}async function uc(e,t={}){let n={loadConfig:ie,resolveRuleContext:Un,runCrawl:us,runDeploy:hs,runInvalidate:Ss,verifyDeployment:oc,promoteCandidateManifest:ac,clearDeployPlan:Oe,afterCheckpoint:async()=>{},...t},r=On(),s=ht(r),i=await ic(r,e),a=await n.loadConfig(),c=nc(a,String(i.ruleId)),o=await n.resolveRuleContext(a,c);if(i.coalesceKey&&i.coalesceKey!==o.coalesceKey)throw new Error("Queued content-sync demand no longer matches the active rule scope.");let d=(await Gt(s)).entries[o.coalesceKey];if(!d||d.scopeFingerprint!==o.scopeFingerprint||d.targetFingerprint!==o.targetFingerprint||d.releaseFingerprint!==o.releaseFingerprint)throw await we(r,"content-sync-baseline-rejected",i,"Content-sync requires a new verified full or incremental release baseline.",{ruleId:c.id,reason:d?"mismatch":"missing"}),new Error("Content-sync baseline is missing or stale. Run a successful normal publish first.");let u=await Zt(s),g=await o.client.head({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:d.baselineId,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites});if(u&&u.jobId!==i.id)throw new Error("Another immutable content-sync range is still active.");if(u||(u={schemaVersion:1,jobId:i.id,ruleId:String(c.id||""),consumerId:o.consumerId,coalesceKey:o.coalesceKey,scopeFingerprint:o.scopeFingerprint,targetFingerprint:o.targetFingerprint,baselineId:d.baselineId,fromSequence:g.committedSequence,toSequence:g.headSequence,phase:"claimed",impactHash:null,startedAt:new Date().toISOString(),updatedAt:new Date().toISOString()},await ke(s,u,"claimed",0,{fromSequence:u.fromSequence,toSequence:u.toSequence},0,n.afterCheckpoint),await we(r,"content-sync-range-claimed",i,"Content-sync claimed a closed journal range.",{fromSequence:u.fromSequence,toSequence:u.toSequence})),u.fromSequence===u.toSequence){let h=await wt(s);h.rules[o.coalesceKey]={ruleId:String(c.id||""),consumerId:o.consumerId,coalesceKey:o.coalesceKey,committedSequence:u.toSequence,observedHeadSequence:g.headSequence,lag:Math.max(0,g.headSequence-u.toSequence),lastSuccessfulJobId:i.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:false,retryAttempt:0,nextRetryAt:null,lastError:null},await yt(s,h),await Yt(s);return}let f=await Zn(s);if(!ze(u.phase,"impact-planned")){let h=await o.client.events({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:d.baselineId,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites,afterSequence:u.fromSequence,throughSequence:u.toSequence}),w=Vt({events:h,scope:o.scope,sourceOrigin:a.sourceOrigin}),P=await o.client.resolveImpactFamilies(w,o.scope.includeSubsites);f=Qn({jobId:i.id,ruleId:String(c.id||""),consumerId:o.consumerId,coalesceKey:o.coalesceKey,scopeFingerprint:o.scopeFingerprint,targetFingerprint:o.targetFingerprint,baselineId:d.baselineId,sourceOrigin:a.sourceOrigin,scope:o.scope,fromSequence:u.fromSequence,toSequence:u.toSequence,events:h,resolvedArchiveFamilies:P,sitemapUrls:Ye(a.sitemapPaths)}),await Yn(s,f),u.impactHash=f.impactHash,await ke(s,u,"impact-planned",h.length,{eventCount:h.length,foldedCount:f.foldedCount,impactHash:f.impactHash},0,n.afterCheckpoint),await we(r,"content-sync-impact-planned",i,"Content-sync persisted an immutable impact plan.",{impactHash:f.impactHash,eventCount:h.length});}if(!f||f.jobId!==i.id||u.impactHash!==f.impactHash)throw new Error("The immutable content-sync impact plan is missing or mismatched.");if(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH=s.candidateManifest,!ze(u.phase,"rendered")){let h=await cc(s,i.id);await we(r,h==="resumed"?"content-sync-render-resumed":"content-sync-render-initialized",i,h==="resumed"?"Content-sync resumed its candidate crawl manifest.":"Content-sync initialized a candidate crawl manifest.",{impactHash:f.impactHash}),await n.runCrawl(["--content-sync-plan",s.impact]),await ke(s,u,"rendered",f.directRenderUrls.length+f.archiveFamilyUrls.length+f.resourceRenderUrls.length,{impactHash:f.impactHash},i.attempt??0,n.afterCheckpoint);}let p=o.deploymentProfile?["--profile",o.deploymentProfile]:[];if(ze(u.phase,"deployed")||(await n.runDeploy([...p,"--content-sync"]),await ke(s,u,"deployed",1,{impactHash:f.impactHash},0,n.afterCheckpoint),await we(r,"content-sync-deployed",i,"Content-sync targeted deployment completed.",{impactHash:f.impactHash})),ze(u.phase,"invalidated")?ze(u.phase,"verified")||await n.runInvalidate([...p,"--content-sync"]):(await n.runInvalidate([...p,"--content-sync"]),await ke(s,u,"invalidated",1,{impactHash:f.impactHash},0,n.afterCheckpoint)),ze(u.phase,"verified")||(await n.verifyDeployment(a,o,f,s.candidateManifest),await n.promoteCandidateManifest(s),await ke(s,u,"verified",1,{impactHash:f.impactHash},0,n.afterCheckpoint)),g=await o.client.head({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:d.baselineId,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites}),g.committedSequence===u.fromSequence)await o.client.acknowledge({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:d.baselineId,expectedSequence:u.fromSequence,sequence:u.toSequence,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites});else if(g.committedSequence!==u.toSequence)throw new Error("Content-sync acknowledgement cursor is in an unexpected state.");await ke(s,u,"acknowledged",1,{sequence:u.toSequence},0,n.afterCheckpoint),await we(r,"content-sync-cursor-acknowledged",i,"Content-sync committed its journal cursor after verification.",{sequence:u.toSequence});let y=await o.client.head({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:d.baselineId,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites}),m=await wt(s);m.rules[o.coalesceKey]={ruleId:String(c.id||""),consumerId:o.consumerId,coalesceKey:o.coalesceKey,committedSequence:u.toSequence,observedHeadSequence:y.headSequence,lag:Math.max(0,y.headSequence-u.toSequence),lastSuccessfulJobId:i.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:y.headSequence>u.toSequence,retryAttempt:0,nextRetryAt:null,lastError:null},await yt(s,m),await n.clearDeployPlan(a),await A.rm(b.join(r,"content-sync-invalidation.json"),{force:true}),await A.rm(s.candidateManifest,{force:true}),await Yt(s);}async function Cs(e=process.argv.slice(2)){let n=e.indexOf("--capture-baseline-cutoffs");if(n>=0){let a=String(e[n+1]||"").trim();if(!a)throw new Error("--capture-baseline-cutoffs requires a release job ID.");await sc(a);return}let r=e.indexOf("--establish-baselines");if(r>=0){let a=String(e[r+1]||"").trim();if(!a)throw new Error("--establish-baselines requires a release job ID.");await rc(a);return}let s=e.indexOf("--job-id"),i=s>=0?String(e[s+1]||"").trim():"";if(!i)throw new Error("Content-sync requires --job-id.");await uc(i);}Cs().catch(e=>{console.error(e instanceof Error?e.message:String(e)),process.exit(1);});