@smart-cloud/publisher-exporter 1.1.53 → 1.1.54

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,4 +1,4 @@
1
- import T from'fs/promises';import ms from'http';import Dn from'https';import S from'path';import er,{createHash}from'crypto';import {request,chromium}from'playwright';import {gunzipSync}from'zlib';import qs 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 Xo from'mime-types';import {CloudFrontClient,waitUntilInvalidationCompleted,CreateInvalidationCommand}from'@aws-sdk/client-cloudfront';var ls=new Set(["post-type","taxonomy","author","date","posts-page","listing"]);function ke(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function ds(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 Ut(e){return Array.isArray(e)?e.map(Ut):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>[t,Ut(n)])):e}function gs(e){return JSON.stringify(Ut(e))}function ue(e){return createHash("sha256").update(gs(e)).digest("hex")}function An(e){return ue({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 Lt(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 he(e,t){let n=new URL(Lt(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 Qe(e){let t=ke((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=ke((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 Tn(e,t,n){return ue({schemaVersion:1,sourceOrigin:Lt(e),ruleId:t.trim(),scope:Qe(n)})}function kn(e){return ue({schemaVersion:1,...e})}function $n(e){let t=String(e.siteId??"").trim()||String(e.accountId??"").trim()||Lt(e.sourceOrigin);return `content-sync:${ue({siteIdentity:t,ruleId:e.ruleId.trim(),targetFingerprint:e.targetFingerprint}).slice(0,48)}`}function En(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 xn(e){return e?.status==="publish"&&!!e.url}function fs(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 jt(e){let t=Qe(e.scope),n=new Map;En(e.events);for(let r of e.events)for(let s of [r.before,r.after])for(let i of s?.archiveFamilies??[]){if(!ls.has(i.kind)||!fs(i.kind,t))continue;let a={...i,url:he(e.sourceOrigin,i.url)};n.set(`${a.kind}|${a.url}`,a);}for(let r of t.listingPaths){let s=he(e.sourceOrigin,r);n.set(`listing|${s}`,{kind:"listing",url:s});}return [...n.values()].sort((r,s)=>r.url.localeCompare(s.url))}function Mn(e){let t=Qe(e.scope),n=En(e.events),r=new Set,s=new Set,i=new Map(jt({events:e.events,scope:t,sourceOrigin:e.sourceOrigin}).map(d=>[`${d.kind}|${d.url}`,d]));for(let d of n){let p=d.before,f=d.after,g=xn(p),y=xn(f);y&&f.url&&r.add(he(e.sourceOrigin,f.url)),g&&p.url&&(!y||p.url!==f.url)&&s.add(he(e.sourceOrigin,p.url));}let a=new Map(e.resolvedArchiveFamilies.map(d=>[`${d.kind}|${he(e.sourceOrigin,d.url)}`,{...d,url:he(e.sourceOrigin,d.url),pageUrls:ke(d.pageUrls.map(p=>he(e.sourceOrigin,p)))}])),u=[...i.entries()].map(([d,p])=>{let f=a.get(d);if(!f)throw new Error(`WordPress did not resolve archive family ${d}.`);return {...p,...f}}).sort((d,p)=>d.url.localeCompare(p.url)),o=ke(u.flatMap(d=>d.pageUrls)),l=t.includeSitemapChain?ds(e.sitemapUrls.map(d=>he(e.sourceOrigin,d))):[],c={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:ke([...r]),archiveFamilyUrls:o,sitemapUrls:l,deleteUrls:ke([...s]),archiveFamilies:u};return {...c,impactHash:ue(c)}}function Je(e){let{impactHash:t,...n}=e;if(e.schemaVersion!==1||!t)throw new Error("Invalid content-sync impact plan schema.");if(ue(n)!==t)throw new Error("Content-sync impact plan hash mismatch.");for(let r of [...e.directRenderUrls,...e.deleteUrls])if(ps(r))throw new Error(`Content-sync impact contains a WordPress trash alias instead of the last public URL: ${r}`)}var hs="wp-json/smartcloud-static-publisher/v1/content-sync/",ws=25*1024*1024,ot=class extends Error{status;code;constructor(t,n,r){super(r),this.name="ContentSyncApiError",this.status=t,this.code=n;}},at=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){let n=this.options.sourceOrigin.replace(/\/+$/,"")+"/";return new URL(`${hs}${t}`,n)}async post(t,n){let r=this.endpoint(t),s=Buffer.from(JSON.stringify(n),"utf8"),i=r.protocol==="https:"?Dn:ms,a=r.protocol==="https:"?new Dn.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((u,o)=>{let l=i.request(r,{method:"POST",agent:a,headers:{"content-type":"application/json","content-length":String(s.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},c=>{let d=[],p=0;c.on("data",f=>{if(p+=f.byteLength,p>ws){c.destroy(new Error("Content-sync API response exceeded the size limit."));return}d.push(f);}),c.on("error",o),c.on("end",()=>{let f=Buffer.concat(d).toString("utf8"),g;try{g=f?JSON.parse(f):{};}catch{o(new ot(c.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let y=c.statusCode??500;if(y<200||y>=300){o(new ot(y,String(g.code??"request-failed"),String(g.message??`Content-sync API request failed (${y}).`)));return}u(g);});});l.on("timeout",()=>l.destroy(new Error(`Content-sync API request timed out: ${t}`))),l.on("error",o),l.end(s);})}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 u of i.items){if(!Number.isSafeInteger(u.sequence)||u.sequence<=a||u.sequence>t.throughSequence||!Number.isSafeInteger(u.postId)||u.postId<=0||!Number.isSafeInteger(u.blogId)||u.blogId<=0||!s.has(u.postType)||typeof u.operation!="string"||!u.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");a=u.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)}};function ct(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"),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 qt(e){let t=await Ve(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 Fn(e,t){await Ee(e.releaseCutoff,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Ee(e,t){await T.mkdir(S.dirname(e),{recursive:true});let n=`${e}.${process.pid}.${Date.now()}.tmp`;await T.writeFile(n,JSON.stringify(t,null,2),"utf8"),await T.rename(n,e);}async function Ve(e){try{return JSON.parse(await T.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 _t(e){let t=await Ve(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 On(e,t){await Ee(e.baseline,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Nt(e){let t=await Ve(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 Un(e,t){await Ee(e.current,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Ln(e){let t=await Ve(e.impact);if(t===null)return null;let n=t;return Je(n),n}async function jn(e,t){Je(t),await Ee(e.impact,t);}async function qn(e,t){await Ee(e.checkpoint,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function ut(e){let t=await Ve(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 lt(e,t){await Ee(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Bt(e){await Promise.all([e.current,e.checkpoint].map(async t=>{try{await T.unlink(t);}catch(n){if(n.code!=="ENOENT")throw n}}));}async function _n(e,t){let n=await Nt(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 T.unlink(r);}catch(s){if(s.code!=="ENOENT")throw s}})),true)}var Ht=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function Ke(e){let t=new Set;for(let n of [...e,...Ht]){let r=String(n||"").trim();r&&t.add(r);}return [...t]}function Bn(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function Ps(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 Wt(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t.trim(),String(n??"")]).filter(([t])=>t.length>0))}function vs(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=Bn(i.targetOrigin);o&&(a.targetOrigin=o);}let u=Wt(i.extraReplacements);if(Object.keys(u).length>0&&(a.extraReplacements=u),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(c=>String(c??"").trim()).filter(c=>c.length>0)),Object.keys(o).length>0&&(a.cloudFront=o);}t[s]=a;}return t}function Qt(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function Cs(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(),u=String(n.url??"").trim(),o=Array.isArray(n.postTypes)?[...new Set(n.postTypes.map(c=>String(c??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],l=Array.isArray(n.listingPaths)?[...new Set(n.listingPaths.map(c=>String(c??"").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}:{},...u?{url:u}:{},...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 Hn(e){let t=e&&typeof e=="object"?e:{},n=Array.isArray(t.rules)?t.rules.map((r,s)=>Cs(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 Rs(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 Jt(e){let t=e&&typeof e=="object"?e:{},n=String(t.accountId??"").trim(),r=String(t.siteId??"").trim(),s=t.subscriber===true,i=Rs(t.siteSettings),a={...i??{},...n?{accountId:i?.accountId??n}:{},...r?{siteId:i?.siteId??r}:{},...i?.subscriber===true||s?{subscriber:true}:{}},u={},o=String(t.apiBase??"").trim();o&&(u.apiBase=o);let l=String(t.runtimeToken??t.nonce??"").trim();l&&(u.runtimeToken=l);let c=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();c&&(u.virtualAssetBaseUrl=c),Object.keys(a).length>0&&(u.siteSettings=a);let d=Qt(t.subscriptionType);return d&&(u.subscriptionType=d),u}function Is(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function Wn(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function xs(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 zn(e,t,n=5){let r=new URL(Wn(e)),s=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),i=r.protocol==="http:"?ms:Dn,a=xs(e,t);return await new Promise((u,o)=>{let l=i.request(r,{method:s,headers:Object.fromEntries(a.entries()),...r.protocol==="https:"?{rejectUnauthorized:false}:{}},c=>{let d=[];c.on("data",p=>{d.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),c.on("error",o),c.on("end",()=>{let p=c.statusCode??0,f=c.headers.location;if(n>0&&f&&[301,302,303,307,308].includes(p)){u(zn(new URL(f,r),{method:p===303?"GET":s},n-1));return}let g=new Headers;for(let[y,m]of Object.entries(c.headers))Array.isArray(m)?g.set(y,m.join(", ")):typeof m=="string"&&g.set(y,m);u(new Response(Buffer.concat(d),{status:p,statusText:c.statusMessage??"",headers:g}));});});l.on("error",o),l.end();})}function As(e){let t=Jt(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;Is(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 Ts(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!As(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 c=Wn(o);if(c.startsWith(t)){let d=(()=>{try{return new URL(c).pathname}catch{return c}})();try{let p=e.ignoreHttpsErrors&&c.startsWith("https://")?await zn(o,l):await n(o,l);return r.push({path:d,status:p.status,ok:p.ok,error:null}),p}catch(p){throw r.push({path:d,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return n(o,l)}));let{getConfig:s}=await import('@smart-cloud/wpsuite-core'),i=await s("publisher"),a=i&&typeof i=="object"?i:null,u=Qt(a?.subscriptionType);return {remote:a,diagnostic:{status:a?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:u??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 ks(e,t){let n=vs(t?.deploymentProfiles),r=String(t?.defaultDeploymentProfile??"").trim(),s=n[r]?r:"",i=Qt(t?.subscriptionType),a=Jt(e.wpsuite);return {...e,scheduler:Hn(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 zt(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 $s(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?S.resolve(e):""}function Nn(e,t,n){let r=String(t||"").trim();return r&&S.isAbsolute(r)?S.resolve(r):S.resolve(e,zt(r,n))}async function se(e,t={}){let n=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",r=await T.readFile(n,"utf8"),s=JSON.parse(r);s.sourceOrigin=s.sourceOrigin.replace(/\/$/,""),s.targetOrigin=Bn(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=Ps(s.generated404RequestPath),s.sitemapPaths||=[...Ht],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=Wt(s.extraReplacements),s.postCrawlCopyMap=Wt(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=Jt(s.wpsuite),s.scheduler=Hn(void 0),s.deploymentProfiles={},s.defaultDeploymentProfile="",s.deploymentTargetOverride=String(s.deploymentTargetOverride??"").trim();let i=await Ts(s);t.onRemotePublisherConfigDiagnostic?.(i.diagnostic);let a=ks(s,i.remote),u=$s(),o=u?S.resolve(u,".."):"";return o?(a.outputDir=Nn(o,a.outputDir,"export"),a.logDir=Nn(o,a.logDir,"logs")):(S.isAbsolute(a.outputDir)||(a.outputDir=zt(a.outputDir,"export")),S.isAbsolute(a.logDir)||(a.logDir=zt(a.logDir,"logs"))),a}var Es=".deploy-plan.json",Ms="deploy-plan.json",Ds=".crawl-incomplete.json",Fs="crawl-incomplete.json";function Qn(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?S.resolve(e):""}function Jn(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 Os(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 ye(e){return [...new Set(e.map(Jn).filter(t=>t!==null))]}function Vn(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:ye(t.changedFiles||[]),deletedFiles:ye(t.deletedFiles||[]),rewriteTargets:ye(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:ye(t.uploadGroups?.assets||[]),content:ye(t.uploadGroups?.content||[]),listings:ye(t.uploadGroups?.listings||[]),sitemapLeaves:ye(t.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:ye(t.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((t.invalidationPaths||[]).map(Os).filter(r=>r!==null))].sort()}:{schemaVersion:1,...n,runMode:t.runMode}}function Us(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.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 Vt(e){let t=S.join(S.resolve(e.outputDir),Es),n=Qn();return n?[S.join(n,Ms),t]:[t]}function Kt(e){let t=S.join(S.resolve(e.outputDir),Ds),n=Qn();return n?[S.join(n,Fs),t]:[t]}function Se(e,t){let n=S.resolve(e),r=S.resolve(t),s=S.relative(n,r).replace(/\\/g,"/");return Jn(s)}async function Me(e){let t=S.resolve(e.outputDir);for(let n of Vt(e))try{let r=await T.readFile(n,"utf8"),s=JSON.parse(r);if(!Us(s)||S.resolve(s.outputDir)!==t)continue;return Vn(e,s)}catch{}return null}async function Gt(e,t){let n=Vn(e,t);for(let r of Vt(e)){await T.mkdir(S.dirname(r),{recursive:true});let s=`${r}.${process.pid}.${Date.now()}.tmp`;await T.writeFile(s,JSON.stringify(n,null,2),"utf8"),await T.rename(s,r);}}async function De(e){for(let t of Vt(e))try{await T.unlink(t);}catch(n){if(n.code!=="ENOENT")throw n}}async function Kn(e,t){let n={schemaVersion:1,startedAt:String(t.startedAt||new Date().toISOString()),outputDir:S.resolve(e.outputDir)};for(let r of Kt(e)){await T.mkdir(S.dirname(r),{recursive:true});let s=`${r}.${process.pid}.tmp`;await T.writeFile(s,JSON.stringify(n,null,2),"utf8"),await T.rename(s,r);}}async function Gn(e){let t=S.resolve(e.outputDir);for(let n of Kt(e)){let r;try{r=await T.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 Zn(e){for(let t of Kt(e))try{await T.unlink(t);}catch(n){if(n.code!=="ENOENT")throw n}}function Ze(e,t=10){return er.createHash("sha1").update(e).digest("hex").slice(0,t)}async function Ls(e){await T.mkdir(S.dirname(e),{recursive:true});}async function Ye(e,t,n){await Ls(e);let r=S.join(S.dirname(e),`.${S.basename(e)}.${process.pid}.${Date.now()}.${er.randomBytes(6).toString("hex")}.tmp`);try{typeof t=="string"?await T.writeFile(r,t,n??"utf8"):await T.writeFile(r,t),await T.rename(r,e);}catch(s){throw await T.unlink(r).catch(()=>{}),s}}function be(e){return [".html",".htm",".css",".js",".mjs",".json",".xml",".xsl",".txt",".svg",".map",".enc",".jws"].includes(S.extname(e).toLowerCase())}function de(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 Fe(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function dt(e){return e.replace(/\//g,"\\/")}function Yn(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 Xn(e,t,n){if(!t)return;e[t]=n;let r=dt(t),s=dt(n);e[r]=s;let i=Yn(t),a=Yn(n);e[i]=a;let u=Fe(t),o=Fe(n);e[u]=o;let l=Fe(r),c=Fe(s);e[l]=c;let d=Fe(i),p=Fe(a);e[d]=p;}function pt(e,t,n){Xn(e,t,n);let r=js(t);r&&r!==t&&Xn(e,r,n);}function Zt(e,t){let n=t.startsWith("/")?t:`/${t}`,r=(e||"").replace(/\/$/,"");return !r||r==="."||r==="/"?n:`${r}${n}`}function tr(e,t){try{return new URL(e,t==="."?"https://relative.invalid":t).pathname}catch{return e.startsWith("/")?e:`/${e.replace(/^\.\//,"")}`}}function nr(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 Hs=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],Ws=new Set([".html",".htm"]),rr="WPSuite.io Static Publisher",zs=rr.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function Qs(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function Js(e){return e.startsWith("//")||e.startsWith("\\/\\/")||e.startsWith("\\\\/\\\\/")}function Vs(e,t,n){return Js(t)?e.replace(new RegExp(`(?<!:)${Qs(t)}`,"g"),n):e.split(t).join(n)}function Ks(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 Ws.has(s)?/<head\b|<html\b|<!doctype html/i.test(t):false}function Gs(e){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${zs}\\2)[^>]*\\/?>`,"i").test(e))return e;let t=`<meta name="generator" content="${rr}" />`;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 T from'fs/promises';import ms from'http';import Dn from'https';import S from'path';import er,{createHash}from'crypto';import {request,chromium}from'playwright';import {gunzipSync}from'zlib';import qs 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 Xo from'mime-types';import {CloudFrontClient,waitUntilInvalidationCompleted,CreateInvalidationCommand}from'@aws-sdk/client-cloudfront';var ls=new Set(["post-type","taxonomy","author","date","posts-page","listing"]);function ke(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function ds(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 Ut(e){return Array.isArray(e)?e.map(Ut):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>[t,Ut(n)])):e}function gs(e){return JSON.stringify(Ut(e))}function ue(e){return createHash("sha256").update(gs(e)).digest("hex")}function An(e){return ue({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 Lt(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 he(e,t){let n=new URL(Lt(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 Qe(e){let t=ke((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=ke((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 Tn(e,t,n){return ue({schemaVersion:1,sourceOrigin:Lt(e),ruleId:t.trim(),scope:Qe(n)})}function kn(e){return ue({schemaVersion:1,...e})}function $n(e){let t=String(e.siteId??"").trim()||String(e.accountId??"").trim()||Lt(e.sourceOrigin);return `content-sync:${ue({siteIdentity:t,ruleId:e.ruleId.trim(),targetFingerprint:e.targetFingerprint}).slice(0,48)}`}function En(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 xn(e){return e?.status==="publish"&&!!e.url}function fs(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 jt(e){let t=Qe(e.scope),n=new Map;En(e.events);for(let r of e.events)for(let s of [r.before,r.after])for(let i of s?.archiveFamilies??[]){if(!ls.has(i.kind)||!fs(i.kind,t))continue;let a={...i,url:he(e.sourceOrigin,i.url)};n.set(`${a.kind}|${a.url}`,a);}for(let r of t.listingPaths){let s=he(e.sourceOrigin,r);n.set(`listing|${s}`,{kind:"listing",url:s});}return [...n.values()].sort((r,s)=>r.url.localeCompare(s.url))}function Mn(e){let t=Qe(e.scope),n=En(e.events),r=new Set,s=new Set,i=new Map(jt({events:e.events,scope:t,sourceOrigin:e.sourceOrigin}).map(d=>[`${d.kind}|${d.url}`,d]));for(let d of n){let p=d.before,f=d.after,g=xn(p),y=xn(f);y&&f.url&&r.add(he(e.sourceOrigin,f.url)),g&&p.url&&(!y||p.url!==f.url)&&s.add(he(e.sourceOrigin,p.url));}let a=new Map(e.resolvedArchiveFamilies.map(d=>[`${d.kind}|${he(e.sourceOrigin,d.url)}`,{...d,url:he(e.sourceOrigin,d.url),pageUrls:ke(d.pageUrls.map(p=>he(e.sourceOrigin,p)))}])),u=[...i.entries()].map(([d,p])=>{let f=a.get(d);if(!f)throw new Error(`WordPress did not resolve archive family ${d}.`);return {...p,...f}}).sort((d,p)=>d.url.localeCompare(p.url)),o=ke(u.flatMap(d=>d.pageUrls)),l=t.includeSitemapChain?ds(e.sitemapUrls.map(d=>he(e.sourceOrigin,d))):[],c={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:ke([...r]),archiveFamilyUrls:o,sitemapUrls:l,deleteUrls:ke([...s]),archiveFamilies:u};return {...c,impactHash:ue(c)}}function Je(e){let{impactHash:t,...n}=e;if(e.schemaVersion!==1||!t)throw new Error("Invalid content-sync impact plan schema.");if(ue(n)!==t)throw new Error("Content-sync impact plan hash mismatch.");for(let r of [...e.directRenderUrls,...e.deleteUrls])if(ps(r))throw new Error(`Content-sync impact contains a WordPress trash alias instead of the last public URL: ${r}`)}var hs="wp-json/smartcloud-static-publisher/v1/content-sync/",ws=25*1024*1024,ot=class extends Error{status;code;constructor(t,n,r){super(r),this.name="ContentSyncApiError",this.status=t,this.code=n;}},at=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){let n=this.options.sourceOrigin.replace(/\/+$/,"")+"/";return new URL(`${hs}${t}`,n)}async post(t,n){let r=this.endpoint(t),s=Buffer.from(JSON.stringify(n),"utf8"),i=r.protocol==="https:"?Dn:ms,a=r.protocol==="https:"?new Dn.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((u,o)=>{let l=i.request(r,{method:"POST",agent:a,headers:{"content-type":"application/json","content-length":String(s.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},c=>{let d=[],p=0;c.on("data",f=>{if(p+=f.byteLength,p>ws){c.destroy(new Error("Content-sync API response exceeded the size limit."));return}d.push(f);}),c.on("error",o),c.on("end",()=>{let f=Buffer.concat(d).toString("utf8"),g;try{g=f?JSON.parse(f):{};}catch{o(new ot(c.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let y=c.statusCode??500;if(y<200||y>=300){o(new ot(y,String(g.code??"request-failed"),String(g.message??`Content-sync API request failed (${y}).`)));return}u(g);});});l.on("timeout",()=>l.destroy(new Error(`Content-sync API request timed out: ${t}`))),l.on("error",o),l.end(s);})}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 u of i.items){if(!Number.isSafeInteger(u.sequence)||u.sequence<=a||u.sequence>t.throughSequence||!Number.isSafeInteger(u.postId)||u.postId<=0||!Number.isSafeInteger(u.blogId)||u.blogId<=0||!s.has(u.postType)||typeof u.operation!="string"||!u.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");a=u.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)}};function ct(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 qt(e){let t=await Ve(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 Fn(e,t){await Ee(e.releaseCutoff,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Ee(e,t){await T.mkdir(S.dirname(e),{recursive:true});let n=`${e}.${process.pid}.${Date.now()}.tmp`;await T.writeFile(n,JSON.stringify(t,null,2),"utf8"),await T.rename(n,e);}async function Ve(e){try{return JSON.parse(await T.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 _t(e){let t=await Ve(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 On(e,t){await Ee(e.baseline,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Nt(e){let t=await Ve(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 Un(e,t){await Ee(e.current,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Ln(e){let t=await Ve(e.impact);if(t===null)return null;let n=t;return Je(n),n}async function jn(e,t){Je(t),await Ee(e.impact,t);}async function qn(e,t){await Ee(e.checkpoint,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function ut(e){let t=await Ve(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 lt(e,t){await Ee(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Bt(e){await Promise.all([e.current,e.checkpoint].map(async t=>{try{await T.unlink(t);}catch(n){if(n.code!=="ENOENT")throw n}}));}async function _n(e,t){let n=await Nt(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 T.unlink(r);}catch(s){if(s.code!=="ENOENT")throw s}})),true)}var Ht=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function Ke(e){let t=new Set;for(let n of [...e,...Ht]){let r=String(n||"").trim();r&&t.add(r);}return [...t]}function Bn(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function Ps(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 Wt(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t.trim(),String(n??"")]).filter(([t])=>t.length>0))}function vs(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=Bn(i.targetOrigin);o&&(a.targetOrigin=o);}let u=Wt(i.extraReplacements);if(Object.keys(u).length>0&&(a.extraReplacements=u),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(c=>String(c??"").trim()).filter(c=>c.length>0)),Object.keys(o).length>0&&(a.cloudFront=o);}t[s]=a;}return t}function Qt(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function Cs(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(),u=String(n.url??"").trim(),o=Array.isArray(n.postTypes)?[...new Set(n.postTypes.map(c=>String(c??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],l=Array.isArray(n.listingPaths)?[...new Set(n.listingPaths.map(c=>String(c??"").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}:{},...u?{url:u}:{},...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 Hn(e){let t=e&&typeof e=="object"?e:{},n=Array.isArray(t.rules)?t.rules.map((r,s)=>Cs(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 Rs(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 Jt(e){let t=e&&typeof e=="object"?e:{},n=String(t.accountId??"").trim(),r=String(t.siteId??"").trim(),s=t.subscriber===true,i=Rs(t.siteSettings),a={...i??{},...n?{accountId:i?.accountId??n}:{},...r?{siteId:i?.siteId??r}:{},...i?.subscriber===true||s?{subscriber:true}:{}},u={},o=String(t.apiBase??"").trim();o&&(u.apiBase=o);let l=String(t.runtimeToken??t.nonce??"").trim();l&&(u.runtimeToken=l);let c=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();c&&(u.virtualAssetBaseUrl=c),Object.keys(a).length>0&&(u.siteSettings=a);let d=Qt(t.subscriptionType);return d&&(u.subscriptionType=d),u}function Is(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function Wn(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function xs(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 zn(e,t,n=5){let r=new URL(Wn(e)),s=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),i=r.protocol==="http:"?ms:Dn,a=xs(e,t);return await new Promise((u,o)=>{let l=i.request(r,{method:s,headers:Object.fromEntries(a.entries()),...r.protocol==="https:"?{rejectUnauthorized:false}:{}},c=>{let d=[];c.on("data",p=>{d.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),c.on("error",o),c.on("end",()=>{let p=c.statusCode??0,f=c.headers.location;if(n>0&&f&&[301,302,303,307,308].includes(p)){u(zn(new URL(f,r),{method:p===303?"GET":s},n-1));return}let g=new Headers;for(let[y,m]of Object.entries(c.headers))Array.isArray(m)?g.set(y,m.join(", ")):typeof m=="string"&&g.set(y,m);u(new Response(Buffer.concat(d),{status:p,statusText:c.statusMessage??"",headers:g}));});});l.on("error",o),l.end();})}function As(e){let t=Jt(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;Is(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 Ts(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!As(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 c=Wn(o);if(c.startsWith(t)){let d=(()=>{try{return new URL(c).pathname}catch{return c}})();try{let p=e.ignoreHttpsErrors&&c.startsWith("https://")?await zn(o,l):await n(o,l);return r.push({path:d,status:p.status,ok:p.ok,error:null}),p}catch(p){throw r.push({path:d,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return n(o,l)}));let{getConfig:s}=await import('@smart-cloud/wpsuite-core'),i=await s("publisher"),a=i&&typeof i=="object"?i:null,u=Qt(a?.subscriptionType);return {remote:a,diagnostic:{status:a?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:u??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 ks(e,t){let n=vs(t?.deploymentProfiles),r=String(t?.defaultDeploymentProfile??"").trim(),s=n[r]?r:"",i=Qt(t?.subscriptionType),a=Jt(e.wpsuite);return {...e,scheduler:Hn(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 zt(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 $s(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?S.resolve(e):""}function Nn(e,t,n){let r=String(t||"").trim();return r&&S.isAbsolute(r)?S.resolve(r):S.resolve(e,zt(r,n))}async function se(e,t={}){let n=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",r=await T.readFile(n,"utf8"),s=JSON.parse(r);s.sourceOrigin=s.sourceOrigin.replace(/\/$/,""),s.targetOrigin=Bn(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=Ps(s.generated404RequestPath),s.sitemapPaths||=[...Ht],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=Wt(s.extraReplacements),s.postCrawlCopyMap=Wt(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=Jt(s.wpsuite),s.scheduler=Hn(void 0),s.deploymentProfiles={},s.defaultDeploymentProfile="",s.deploymentTargetOverride=String(s.deploymentTargetOverride??"").trim();let i=await Ts(s);t.onRemotePublisherConfigDiagnostic?.(i.diagnostic);let a=ks(s,i.remote),u=$s(),o=u?S.resolve(u,".."):"";return o?(a.outputDir=Nn(o,a.outputDir,"export"),a.logDir=Nn(o,a.logDir,"logs")):(S.isAbsolute(a.outputDir)||(a.outputDir=zt(a.outputDir,"export")),S.isAbsolute(a.logDir)||(a.logDir=zt(a.logDir,"logs"))),a}var Es=".deploy-plan.json",Ms="deploy-plan.json",Ds=".crawl-incomplete.json",Fs="crawl-incomplete.json";function Qn(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?S.resolve(e):""}function Jn(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 Os(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 ye(e){return [...new Set(e.map(Jn).filter(t=>t!==null))]}function Vn(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:ye(t.changedFiles||[]),deletedFiles:ye(t.deletedFiles||[]),rewriteTargets:ye(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:ye(t.uploadGroups?.assets||[]),content:ye(t.uploadGroups?.content||[]),listings:ye(t.uploadGroups?.listings||[]),sitemapLeaves:ye(t.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:ye(t.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((t.invalidationPaths||[]).map(Os).filter(r=>r!==null))].sort()}:{schemaVersion:1,...n,runMode:t.runMode}}function Us(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.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 Vt(e){let t=S.join(S.resolve(e.outputDir),Es),n=Qn();return n?[S.join(n,Ms),t]:[t]}function Kt(e){let t=S.join(S.resolve(e.outputDir),Ds),n=Qn();return n?[S.join(n,Fs),t]:[t]}function Se(e,t){let n=S.resolve(e),r=S.resolve(t),s=S.relative(n,r).replace(/\\/g,"/");return Jn(s)}async function Me(e){let t=S.resolve(e.outputDir);for(let n of Vt(e))try{let r=await T.readFile(n,"utf8"),s=JSON.parse(r);if(!Us(s)||S.resolve(s.outputDir)!==t)continue;return Vn(e,s)}catch{}return null}async function Gt(e,t){let n=Vn(e,t);for(let r of Vt(e)){await T.mkdir(S.dirname(r),{recursive:true});let s=`${r}.${process.pid}.${Date.now()}.tmp`;await T.writeFile(s,JSON.stringify(n,null,2),"utf8"),await T.rename(s,r);}}async function De(e){for(let t of Vt(e))try{await T.unlink(t);}catch(n){if(n.code!=="ENOENT")throw n}}async function Kn(e,t){let n={schemaVersion:1,startedAt:String(t.startedAt||new Date().toISOString()),outputDir:S.resolve(e.outputDir)};for(let r of Kt(e)){await T.mkdir(S.dirname(r),{recursive:true});let s=`${r}.${process.pid}.tmp`;await T.writeFile(s,JSON.stringify(n,null,2),"utf8"),await T.rename(s,r);}}async function Gn(e){let t=S.resolve(e.outputDir);for(let n of Kt(e)){let r;try{r=await T.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 Zn(e){for(let t of Kt(e))try{await T.unlink(t);}catch(n){if(n.code!=="ENOENT")throw n}}function Ze(e,t=10){return er.createHash("sha1").update(e).digest("hex").slice(0,t)}async function Ls(e){await T.mkdir(S.dirname(e),{recursive:true});}async function Ye(e,t,n){await Ls(e);let r=S.join(S.dirname(e),`.${S.basename(e)}.${process.pid}.${Date.now()}.${er.randomBytes(6).toString("hex")}.tmp`);try{typeof t=="string"?await T.writeFile(r,t,n??"utf8"):await T.writeFile(r,t),await T.rename(r,e);}catch(s){throw await T.unlink(r).catch(()=>{}),s}}function be(e){return [".html",".htm",".css",".js",".mjs",".json",".xml",".xsl",".txt",".svg",".map",".enc",".jws"].includes(S.extname(e).toLowerCase())}function de(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 Fe(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function dt(e){return e.replace(/\//g,"\\/")}function Yn(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 Xn(e,t,n){if(!t)return;e[t]=n;let r=dt(t),s=dt(n);e[r]=s;let i=Yn(t),a=Yn(n);e[i]=a;let u=Fe(t),o=Fe(n);e[u]=o;let l=Fe(r),c=Fe(s);e[l]=c;let d=Fe(i),p=Fe(a);e[d]=p;}function pt(e,t,n){Xn(e,t,n);let r=js(t);r&&r!==t&&Xn(e,r,n);}function Zt(e,t){let n=t.startsWith("/")?t:`/${t}`,r=(e||"").replace(/\/$/,"");return !r||r==="."||r==="/"?n:`${r}${n}`}function tr(e,t){try{return new URL(e,t==="."?"https://relative.invalid":t).pathname}catch{return e.startsWith("/")?e:`/${e.replace(/^\.\//,"")}`}}function nr(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 Hs=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],Ws=new Set([".html",".htm"]),rr="WPSuite.io Static Publisher",zs=rr.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function Qs(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function Js(e){return e.startsWith("//")||e.startsWith("\\/\\/")||e.startsWith("\\\\/\\\\/")}function Vs(e,t,n){return Js(t)?e.replace(new RegExp(`(?<!:)${Qs(t)}`,"g"),n):e.split(t).join(n)}function Ks(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 Ws.has(s)?/<head\b|<html\b|<!doctype html/i.test(t):false}function Gs(e){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${zs}\\2)[^>]*\\/?>`,"i").test(e))return e;let t=`<meta name="generator" content="${rr}" />`;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
4
  `;return e.replace(/<head\b[^>]*>/i,s=>`${s}${r} ${t}`)}function Zs(e){let t=de(e).trim();return t.startsWith("{")||t.startsWith("[")||t.includes("\\/")||/"@context"|"@type"/.test(t)}function Ys(e,t){let n=[...new Set(t)].filter(i=>i.includes("/")&&!/\\+\//.test(i)).map(i=>[i,dt(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[u,o]of n)a=a.split(u).join(o);return a},s=e.replace(/(<script\b[^>]*\btype=["']application\/(?:ld\+)?json["'][^>]*>)([\s\S]*?)(<\/script>)/gi,(i,a,u,o)=>`${a}${r(u)}${o}`);return s=s.replace(/(<meta\b[^>]*\bcontent=(['"]))([\s\S]*?)(\2[^>]*>)/gi,(i,a,u,o,l)=>Zs(o)?`${a}${r(o)}${l}`:i),s}function Xs(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 ei(e,t,n){let r=Xs(t,n);if(!r)return e;let s=e;for(let i of Hs){let a=i.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),u=new RegExp(`(?:\\.{1,}/)+${a}`,"g"),o=new RegExp(`(?<!\\.)/${a}`,"g");s=s.replace(u,`${r}${i}`).replace(o,`${r}${i}`);let l=i.replace(/\//g,"\\/"),c=r.replace(/\//g,"\\/"),d=l.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),p=new RegExp(`(?:\\.{1,}\\\\/)+${d}`,"g"),f=new RegExp(`(?:\\.{1,}(?:\\\\/|\\\\))+${d}`,"g"),g=new RegExp(`(?<![\\.\\\\])\\/${d}`,"g");s=s.replace(p,`${c}${l}`).replace(f,`${c}${l}`).replace(g,`${c}${l}`);}return s}function Xt(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:tr(t,e.targetOrigin),i=r?`${r.search}${r.hash}`:"";return e.urlRewriteMode==="root-relative"?`${s}${i}`:`${nr(e.outputDir,n,s)}${i}`}function Yt(e,t,n,r,s){pt(e,n,Xt(t,r,s));}function ti(e,t,n,r,s){if(!n||n===r)return;pt(e,n,r);let i=Xt(t,n,s),a=Xt(t,r,s);pt(e,i,a);}function en(e,t,n,r,s={}){let i=e,a={};Yt(a,t,t.sourceOrigin,t.targetOrigin,r);for(let[o,l]of Object.entries(t.extraReplacements))Yt(a,t,o,l,r);for(let[o,l]of Object.entries(n))Yt(a,t,o,l,r);for(let[o,l]of Object.entries(s)){let c=n[o];!c||c===l||ti(a,t,l,c,r);}let u=Object.entries(a).sort((o,l)=>l[0].length-o[0].length);for(let[o,l]of u)i=Vs(i,o,l);return t.urlRewriteMode!=="absolute"&&(i=ei(i,t,r)),i=Ys(i,u.map(([,o])=>o)),Ks(t,i,r)&&(i=Gs(i)),i}async function ni(e,t,n,r){let s=S.join(e.outputDir,r),i=S.extname(s).toLowerCase(),a;try{a=await T.readFile(s,"utf8");}catch(l){if(l.code!=="ENOENT")throw l;return {changed:false}}let o=en(a,e,t,i===".js"||i===".mjs"?void 0:s,n);return o!==a?(await Ye(s,o,"utf8"),{changed:true}):{changed:false}}async function ri(e,t){return await new Promise((n,r)=>{let s=o=>{if(u(),o?.error){r(new Error(o.error));return}n({changed:!!o?.changed});},i=o=>{u(),r(o);},a=o=>{u(),o!==0&&r(new Error(`Rewrite worker exited with code ${o}.`));},u=()=>{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 gt(e,t,n={}){let s=(n.files?[...new Set(n.files)].map(p=>S.resolve(p)).filter(p=>p.startsWith(S.resolve(e.outputDir))).map(p=>S.relative(e.outputDir,p).replace(/\\/g,"/")).filter(p=>p.length>0):await qs(["**/*"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(p=>p!=="asset-map.json"&&be(p)),i=0;if(s.length===0)return 0;let a=Math.max(1,Number(e.rewriteConcurrency||e.assetDownloadConcurrency||1)),u=Math.max(1,Math.min(a,s.length,availableParallelism()));await n.onStart?.({totalFiles:s.length,workerCount:u});let o=0,l=0,c=null;if(u===1){for(let p of s){let f=await ni(e,t,n.previousAssetMap??{},p);f.changed&&(i+=1),l+=1,await n.onProgress?.({index:l,totalFiles:s.length,changedTextFiles:i,file:p,changed:f.changed});}return i}let d=Array.from({length:u},async()=>{let p=new Worker(new URL("./rewrite-worker.js",import.meta.url),{workerData:{config:e,assetMap:t,previousAssetMap:n.previousAssetMap??{}}});try{for(;!c;){let f=o;if(f>=s.length)return;o+=1;let g=s[f],y=await ri(p,g);y.changed&&(i+=1),l+=1,await n.onProgress?.({index:l,totalFiles:s.length,changedTextFiles:i,file:g,changed:y.changed});}}catch(f){throw c=f instanceof Error?f:new Error(String(f)),c}finally{await p.terminate().catch(()=>{});}});return await Promise.all(d),i}var ae={error:0,warn:1,info:2,debug:3},si=/^(?:authorization|proxyAuthorization|runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken|xStaticPublisherToken|xApiKey)$/i;function Re(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 mt(e){return typeof e=="string"?Re(e):Array.isArray(e)?e.map(t=>mt(t)):e instanceof Error?{name:e.name,message:Re(e.message),...e.stack?{stack:Re(e.stack)}:{}}:e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([t,n])=>[t,si.test(t)?"[REDACTED]":mt(n)])):e}function wt(e){return e?mt(e):void 0}var sr=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",ft=sr?S.join(sr,"current-progress.json"):"",ir=Promise.resolve();function or(e){return e.includes("crawl")?"crawl":e.includes("deploy")?"deploy":e.includes("invalidate")?"invalidate":e}function ii(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 ar(e,t,n){ft&&(ir=ir.then(async()=>{let r=new Date,s=r.toISOString();await T.mkdir(S.dirname(ft),{recursive:true});let i=await T.readFile(ft,"utf8").then(h=>JSON.parse(h)).catch(()=>null),a=i&&typeof i.details=="object"&&i.details?i.details:{},u=or(e),o=i?.currentStep||or(i?.source||""),l=i?.startedAt||s,c={...i?.stepDurationsSec??{}},d=i?.stepStartedAt||s;if(o&&o!==u&&i?.stepStartedAt){let h=Math.max(0,Math.round((r.getTime()-new Date(i.stepStartedAt).getTime())/1e3));c[o]=(c[o]??0)+h,d=s;}let p=Math.max(0,Math.round((r.getTime()-new Date(d).getTime())/1e3)),f=Math.max(0,Math.round((r.getTime()-new Date(l).getTime())/1e3)),g=Re(t),y=wt(n)??{},m={checkedAt:s,source:e,message:g,details:ii(a,y),startedAt:l,currentStep:u,stepStartedAt:d,stepElapsedSec:p,totalElapsedSec:f,stepDurationsSec:c};await T.writeFile(ft,JSON.stringify(m,null,2),"utf8");}).catch(()=>{}));}async function cr(e,t){await T.mkdir(S.dirname(e),{recursive:true}),await T.appendFile(e,`${JSON.stringify(mt(t))}
@@ -10,5 +10,5 @@ ${e.path}`}function mr(e,t){let n;try{n=new URL(t);}catch{return null}let r=e.sp
10
10
  `,{encoding:"utf8",mode:384}),await T.rename(n,e);}var mi=["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"],kr=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"]),$r=new Set(["",".html",".htm"]),hi=new Set([".css",".xml",".xsl",".svg",".json",".html",".htm",".txt",".enc",".jws"]),wi=["/wp-content/","/wp-includes/","/wp-admin/","/static/","/assets/","/build/","/_next/"];function yi(e){let t=[],n="full",r="full",s=false,i=false,a="";for(let u=0;u<e.length;u++){let o=e[u];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[++u];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[++u];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[++u];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[++u];if(!l)throw new Error("--content-sync-plan requires a file path");n="content-sync",r="incremental",s=true,a=S.resolve(l);}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 xt(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?S.resolve(e):""}function Si(){let e=xt();return e?S.join(e,"privacy","cookie-observations.json"):null}function bi(){let e=xt();return e?S.resolve(e,".."):""}function Pi(){let e=process.env.STATIC_PUBLISHER_WP_ROOT||process.env.WPSUITE_STATIC_PUBLISHER_WP_ROOT||"";return e.trim()?S.resolve(e):""}function vi(e){let t=e.trim(),n=[{alias:"@storage-root",root:bi(),requiredEnv:"STATIC_PUBLISHER_RUNTIME_DIR or WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR"},{alias:"@runtime",root:xt(),requiredEnv:"STATIC_PUBLISHER_RUNTIME_DIR or WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR"},{alias:"@wp-root",root:Pi(),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 Er(e){let t=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(t)return S.resolve(t);let n=xt();return n?S.join(n,"crawl-manifest.json"):S.join(e.outputDir,".crawl-manifest.json")}function Mr(){return {schemaVersion:5,updatedAt:"",pages:{},assets:{}}}function wr(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 Ci(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 sn(e){let t=e?.["content-type"]?.trim();return t||null}function Ri(){return {rewriteTargets:new Set,changedFiles:new Set,deletedFiles:new Set}}function X(e,t,n){let r=Se(t.outputDir,n);r&&(e.changedFiles.add(r),e.deletedFiles.delete(r));}function yr(e,t,n){let r=Se(t.outputDir,n);r&&(e.deletedFiles.add(r),e.changedFiles.delete(r));}function Ie(e,t){e.rewriteTargets.add(S.resolve(t));}async function Ii(e){try{let t=await T.readFile(Er(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,wr(s)])),assets:Object.fromEntries(Object.entries(n.assets).map(([r,s])=>[r,Ci(s)]))};if(n.schemaVersion===1)return {schemaVersion:5,updatedAt:String(n.updatedAt||""),pages:Object.fromEntries(Object.entries(n.pages).map(([r,s])=>[r,wr(s)])),assets:{}}}}catch{}return Mr()}function on(e){return JSON.parse(JSON.stringify(e))}async function xi(e,t){let n=Er(e);await T.mkdir(S.dirname(n),{recursive:true}),await T.writeFile(n,JSON.stringify(t,null,2),"utf8");}async function Pe(e,t){if(!t.trackManifest)return;let n=async()=>{let r=on(t.manifest);r.updatedAt=new Date().toISOString(),t.manifest.updatedAt=r.updatedAt,await xi(e,r);};t.manifestSaveChain=t.manifestSaveChain.then(n,n),await t.manifestSaveChain;}function et(e){if(!e)return null;let t=e.trim();return t||null}function Ai(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 Dr(e,t,n,r){let s=[...new Set(n.map(u=>u.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 u=await t.post(a,{timeout:3e4,failOnStatusCode:!1,headers:{"content-type":"application/json","x-static-publisher-token":i},data:JSON.stringify({urls:s})});if(!u.ok())return r.warn(`Change token lookup failed with HTTP ${u.status()}`,{endpoint:a,status:u.status()}),new Map;let o=await u.json().catch(()=>null);if(!o||!Array.isArray(o.items))return new Map;let l=new Map;for(let c of o.items)!c||typeof c.url!="string"||l.set(c.url,c);return l}catch(u){return r.warn("Change token lookup failed; falling back to sitemap metadata",{endpoint:a,error:u instanceof Error?`${u.name}: ${u.message.split(/\r?\n/u,1)[0]}`:"Unknown change-token request failure"}),new Map}}async function vt(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 Dr(e,t,[r],s)).get(r)??null;return pn(e,r)&&(!a||a.supported!==true||!a.token)&&(a=await $i(e,t,r,s)),a=Fr(e,a),n.changeTokenCache.set(r,a),a}async function Ti(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 u=i.slice(a,a+250),o=await Dr(e,t,u,s);for(let l of u)n.changeTokenCache.set(l,Fr(e,o.get(l)??null));}}function ki(e){return `subscriber=${e.wpsuite?.siteSettings?.subscriber===true?"1":"0"}`}function Fr(e,t){if(!t?.supported||!t.token||t.tokenSource==="wp-media-file")return t;let n=ki(e);return {...t,token:Ze(`${t.token}
11
11
  ${n}`,40),tokenSource:t.tokenSource?`${t.tokenSource}+site-subscriber`:"site-subscriber"}}async function $i(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=Ze(`${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 Ei(e,t,n,r,s,i){let a=pn(e,s);if(!r.enabled)return {action:"render",changeToken:r.trackManifest?await vt(e,t,r,s,i):null};let u=r.manifest.pages[s];if(!u)return {action:"render",changeToken:await vt(e,t,r,s,i)};let o=await vt(e,t,r,s,i);if(o?.supported&&o.token)return u.changeToken===o.token?a&&!await Mi(e,t,s,i)?{action:"render",changeToken:o}:{action:"reuse",changeToken:o}:{action:"render",changeToken:o};let l=et(n.sitemapLastmodByPage[s]);return l&&u.sitemapLastmod&&u.sitemapLastmod===l?{action:"reuse",changeToken:o}:{action:"render",changeToken:o}}async function Mi(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 Di(e,t,n,r,s){let i=Sr(n.previousManifest,void 0,r,"previous-manifest"),a=0;for(let[p,f]of Object.entries(n.manifest.pages))if(!n.seenPages.has(p)){yr(s,e,f.outputPath);try{await T.unlink(f.outputPath);}catch(g){(g.code||"")!=="ENOENT"&&r.warn(`Failed to remove stale incremental page output for ${p}`,{url:p,outputPath:f.outputPath,error:String(g)});}delete n.manifest.pages[p],a++;}let u=Sr(n.manifest,n.runId,r,"active-manifest"),o=new Set,l=new Set;for(let p of u){for(let y of tt(e,p))o.add(y);let f=n.manifest.assets[p]?.outputPath;f&&l.add(f);let g=je(e,t.assetMap,p);g&&l.add(g);}for(let p of Object.keys(n.manifest.assets)){if(u.has(p)){n.manifest.assets[p].unreferencedRuns=0;continue}let f=n.manifest.assets[p];if(gr(f.unreferencedRuns)){f.unreferencedRuns+=1,r.warn(`Deferred deletion of newly unreferenced incremental asset ${p}.`,{url:p,unreferencedRuns:f.unreferencedRuns,phase:"incremental-cleanup",source:"asset-deletion-grace"});continue}delete n.manifest.assets[p];}let c=0,d=0;for(let p of i){if(n.manifest.assets[p])continue;let f=new Set,g=n.previousManifest.assets[p]?.outputPath;g&&f.add(g);let y=je(e,t.assetMap,p);if(!u.has(p)&&y)f.add(y);else if(!u.has(p)){let m=Gi(e,p,r,"incremental-cleanup");m&&(f.add(m.originalFilePath),m.hashedFilePath&&f.add(m.hashedFilePath));}for(let m of f)if(!l.has(m)){yr(s,e,m);try{await T.unlink(m),c++;}catch(h){(h.code||"")!=="ENOENT"&&r.warn(`Failed to remove stale incremental asset output for ${p}`,{url:p,outputPath:m,error:String(h)});}}for(let m of tt(e,p))o.has(m)||Object.prototype.hasOwnProperty.call(t.assetMap,m)&&(delete t.assetMap[m],d++);}(a>0||c>0||d>0)&&r.info(`Incremental cleanup removed ${a} stale pages, ${c} stale assets, ${d} stale asset mappings`,{removedPages:a,removedAssets:c,removedAssetMappings:d,phase:"incremental-cleanup"});}function Sr(e,t,n,r){let s=new Set,i=a=>{let u=a.trim();if(u){if(n){let o=ve(u,n,"asset",r??"manifest");if(!o)return;s.add(o.toString());return}s.add(u);}};for(let a of Object.values(e.pages))for(let u of a.discoveredAssets||[])i(u);for(let[a,u]of Object.entries(e.assets||{}))t&&u.lastSeenRunId!==t||i(a);return s}function br(e,t,n){for(let r of t)for(let s of tt(e,r))if(n.has(s))return true;return false}function Fi(e,t,n,r,s){let i=new Set(n.rewriteTargets);for(let u of n.changedFiles)i.add(S.resolve(e.outputDir,u));for(let u of Object.values(t.manifest.pages))u.rewriteComplete||i.add(S.resolve(u.outputPath));for(let u of Object.values(t.manifest.assets))u.isText&&!u.rewriteComplete&&i.add(S.resolve(u.outputPath));let a=Ai(r,s);if(a.size===0)return [...i];for(let u of Object.values(t.manifest.pages))br(e,u.discoveredAssets,a)&&i.add(S.resolve(u.outputPath));for(let[u,o]of Object.entries(t.manifest.assets)){o.isText&&br(e,o.discoveredAssets,a)&&i.add(S.resolve(o.outputPath));for(let l of tt(e,u))if(a.has(l)){X(n,e,o.outputPath),o.isText&&i.add(S.resolve(o.outputPath));break}}return [...i]}function Oi(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 Ui(e,t,n){let r=new Set,s=i=>{if(!i)return;let a=Se(e.outputDir,i);a&&r.add(a);};for(let i of t.donePages)s(ge(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(je(e,t.assetMap,i));for(let i of Object.values(n.manifest.assets))i.lastSeenRunId===n.runId&&s(i.outputPath);return r}async function Li(e,t,n,r,s){let i=[...r.deletedFiles].sort(),a=Ui(e,t,n),u=0;for(let c of [...a].sort()){try{if((await T.stat(S.join(e.outputDir,c))).isFile())continue}catch{}u++,s.warn(`Active output is missing after crawl: ${c}`,{phase:"deploy-plan",file:c,outputPath:S.join(e.outputDir,c)});}if(u>0&&s.warn(`Detected ${u} active output file(s) missing before deploy plan save.`,{phase:"deploy-plan",missingActiveOutputs:u}),i.length===0)return [];let o=[],l=0;for(let c of i){if(!a.has(c)){o.push(c);continue}l++;let d=S.join(e.outputDir,c),p;try{p=(await T.stat(d)).isFile();}catch{p=false;}s.warn(`Deploy plan protected active output from deletion: ${c}`,{phase:"deploy-plan",file:c,outputPath:d,exists:p});}return l>0&&s.info(`Deploy plan skipped ${l} active output deletion(s).`,{phase:"deploy-plan",protectedDeletes:l,deletedFiles:o.length}),o}function tt(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 ji(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 Or(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,dn(r.pathname))}catch{return null}}function qi(e,t,n){for(let r of ji(e,n)){let s=t[r];if(!s)continue;let i=Or(e,s);if(i)return {outputPath:i,publicUrl:s}}return null}function _i(e,t,n){for(let r of tt(e,n)){let s=t[r];if(!s)continue;let i=Or(e,s);if(i)return i}return null}function je(e,t,n){let r=_i(e,t,n);if(r)return r;try{return At(e,n).originalFilePath}catch{return null}}function Ni(){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 Pr(e){let t=Ni();if(t)try{let n=await T.readFile(t,"utf8"),r=JSON.parse(n);if(!r||typeof r!="object")return;await T.writeFile(t,JSON.stringify({...r,checkedAt:new Date().toISOString(),status:"running",currentStep:"rewrite",message:e},null,2),"utf8");}catch{}}function Bi(e,t){let n=new Set;for(let r of t.donePages)n.add(S.resolve(ge(e,r)));for(let r of t.doneAssets){let s=je(e,t.assetMap,r);!s||!be(s)||n.add(S.resolve(s));}for(let r of t.doneSitemaps){let s=je(e,t.assetMap,r);!s||!be(s)||n.add(S.resolve(s));}return [...n]}var Rt="re:",bt=new Map;function Hi(e){if(!e.startsWith(Rt))return null;let t=bt.get(e);if(t!==void 0)return t||null;let n=e.slice(Rt.length).trim();if(!n)return bt.set(e,false),null;try{let r=new RegExp(n);return bt.set(e,r),r}catch{return console.warn(`[crawl] Ignoring invalid path matcher regex: ${e}`),bt.set(e,false),null}}function ln(e,t){let n=Hi(t);return n?n.test(e):t.startsWith(Rt)?false:e.startsWith(t)}function Wi(e,t){return t.find(n=>ln(e,n))}function Ur(e,t){let n=Wi(t.pathname,e.blockedPathPrefixes);if(n)return n.startsWith(Rt)?`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 zi(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 Qi(e,t){return zi(e).has(t.hostname)}function Ji(e){let t=de(e.trim()).replace(/\\\//g,"/").replace(/^['"]|['"]$/g,"").trim();return t=t.replace(/[)]+$/g,"").trim(),t=t.replace(/;.*$/g,"").trim(),t}function an(e){let t=de(e).replace(/\\\//g,"/");return /[{}]|\bwindow\.|\blocation\.|\bincludes\(|\?\?null|\+|%7B|%7D|%22|<|>|\s/.test(t)||/[()[\]'"]/.test(t)}function Vi(e){let t=de(e).replace(/\\\//g,"/").trim(),n=S.extname(t).toLowerCase();return kr.has(n)||$r.has(n)?false:/^(?:this|[A-Za-z_$][\w$]*)\.[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/.test(t)}function qe(e){return wi.some(t=>e.pathname.startsWith(t))}function z(e,t,n=e.sourceOrigin,r,s="url"){let i=Ji(t);if(!i||i.startsWith("data:")||i.startsWith("blob:")||i.startsWith("mailto:")||i.startsWith("tel:")||i.startsWith("#"))return r?.ignore(s,t,"empty or unsupported scheme",n),null;if(an(i))return r?.ignore(s,t,"looks like JavaScript/code fragment",n),null;if(Vi(i))return r?.ignore(s,t,"looks like JavaScript property access",n),null;try{let a=new URL(i,n);if(Lr(a))return r?.reject(s,t,"malformed URL encoding",n),null;if(!["http:","https:"].includes(a.protocol))return r?.ignore(s,t,"unsupported protocol: ",n),null;if(!Qi(e,a))return r?.ignore(s,a.toString(),"host not allowed",n),null;let u=Ur(e,a);if(u)return r?.reject(s,a.toString(),u,n),null;let o=new URL(e.sourceOrigin);return a.protocol=o.protocol,a.host=o.host,a.hash="",a}catch{return r?.reject(s,t,"invalid URL",n),null}}function Lr(e){try{return decodeURIComponent(e.pathname),decodeURIComponent(e.search),decodeURIComponent(e.hash),!1}catch{return true}}function ve(e,t,n,r){try{let s=new URL(e);return Lr(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 jr(e){return kr.has(S.extname(e.pathname).toLowerCase())}function Ce(e){return qe(e)?false:$r.has(S.extname(e.pathname).toLowerCase())}function _e(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function B(e,t){return !!(_e(t)||t.pathname==="/robots.txt"||t.pathname==="/llms.txt"||t.pathname.toLowerCase().endsWith(".xsl")||e.assetPathPrefixes.length>0&&e.assetPathPrefixes.some(n=>ln(t.pathname,n))||jr(t)||!Ce(t)&&!qe(t))}function ge(e,t){let n=new URL(t),r=dn(n.pathname);return r.endsWith("/")?r+="index.html":S.extname(r)||(r+="/index.html"),S.join(e.outputDir,r)}function dn(e){try{return decodeURIComponent(e)}catch{return e}}function Ki(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 pn(e,t){let n=Ki(e);if(!n)return false;try{return new URL(t,e.sourceOrigin).toString()===n}catch{return false}}function At(e,t){let n=new URL(t),r=dn(n.pathname);r.endsWith("/")&&(r+="index.html");let s=!!n.search,i=r,a=S.join(e.outputDir,i),u=Zt(e.targetOrigin,i),o=`${u}${n.search}`;if(!s)return {originalPathname:i,originalFilePath:a,originalPublicUrl:u,originalPublicUrlWithSearch:o,preferQueryHashed:s};let l=S.extname(r),d=`${l?r.slice(0,-l.length):r}.${Ze(n.search)}${l||".bin"}`;return {originalPathname:i,originalFilePath:a,originalPublicUrl:u,originalPublicUrlWithSearch:o,preferQueryHashed:s,hashedPathname:d,hashedFilePath:S.join(e.outputDir,d),hashedPublicUrl:Zt(e.targetOrigin,d)}}function Gi(e,t,n,r){let s=ve(t,n,"asset",r);return s?At(e,s.toString()):null}function Zi(e,t,n,r,s){t[n]=r;try{let i=new URL(n),u=[new URL(e.sourceOrigin).origin,...Object.keys(e.extraReplacements||{})];if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{u.push(new URL(e.targetOrigin).origin);}catch{}for(let o of u)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 qr(e){try{return await T.readFile(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}async function Le(e){try{return await T.stat(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}function Yi(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 _r(e){let t=String(e["last-modified"]||"").trim();if(!t)return null;let n=Date.parse(t);return Number.isFinite(n)?n:null}function Xi(e,t){let n=_r(t),r=Yi(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 eo(e,t){if(!t)return;let n=_r(t);if(n===null)return;let r=await Le(e);if(!r)return;let s=new Date(n);await T.utimes(e,r.atime,s).catch(()=>{});}function vr(e,t){let n=S.extname(e.pathname).toLowerCase(),r=t["content-type"]||"";return hi.has(n)||mi.some(s=>r.includes(s))&&!r.startsWith("image/")&&!r.includes("font")}function to(e){try{let t=S.extname(new URL(e).pathname).toLowerCase();return t===".js"||t===".mjs"}catch{return false}}function Ue(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 no(e){return Ze(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 so(e,t,n){let r=e.assetVariantDigestsByOriginalPath.get(t);r||(r=new Set,e.assetVariantDigestsByOriginalPath.set(t,r)),r.add(n);}async function cn(e,t,n,r,s,i,a){let u=At(e,n),o=no(r),l=u.originalFilePath,c=u.originalPublicUrlWithSearch;if(u.preferQueryHashed&&u.hashedFilePath&&u.hashedPublicUrl&&ro(s,u.originalFilePath,o)){let g=qi(e,t,n);g?(l=g.outputPath,c=g.publicUrl):(l=u.hashedFilePath,c=u.hashedPublicUrl);}Zi(e,t,n,c,u.originalPublicUrl);let d=be(l)?Buffer.from(en(r.toString("utf8"),e,t,l),"utf8"):r,p=await qr(l),f=false;return (p===null||!p.equals(d))&&(await Ye(l,d),f=true),i?.debug(`${f?"Saved":"Skipped unchanged"} asset output ${n}`,{phase:"download-assets",url:n,outputPath:l,bytes:d.length,wroteFile:f}),so(s,u.originalFilePath,o),await eo(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:c,wroteFile:f}}function nt(e,t,n,r,s){let i=()=>{let d=String(s??"").trim();if(!d)return {discoveredFrom:"",discoveredFromType:"unknown"};if(d==="seed-path"||d==="cli"||d==="generated-404-request-path")return {discoveredFrom:d,discoveredFromType:d};if(/^https?:\/\//i.test(d))try{let p=new URL(d);return _e(p)?{discoveredFrom:d,discoveredFromType:"sitemap"}:B(e,p)?{discoveredFrom:d,discoveredFromType:"asset"}:Ce(p)&&!qe(p)?{discoveredFrom:d,discoveredFromType:"page"}:{discoveredFrom:d,discoveredFromType:"url"}}catch{return {discoveredFrom:d,discoveredFromType:"url"}}return {discoveredFrom:d,discoveredFromType:"url"}},a=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,u=z(e,n,a,r,"page");if(!u)return;if(!Ce(u)||jr(u)){if(B(e,u)){r.info(`Seeded/non-page URL queued as asset: ${u.toString()}`,{url:u.toString(),source:s}),W(e,t,u.toString(),r,s);return}r.reject("page",u.toString(),qe(u)?"asset/internal path cannot be page":"not page-like",s);return}let o=u.toString(),{discoveredFrom:l,discoveredFromType:c}=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:c,outputPath:ge(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 W(e,t,n,r,s){let i=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,a=z(e,n,i,r,"asset");if(!a)return;if(!B(e,a)){r.reject("asset",a.toString(),"not a safe asset path/prefix",s);return}let u=a.toString();!t.doneAssets.has(u)&&!t.queuedAssets.has(u)&&(t.queuedAssets.add(u),t.assetQueue.push(u),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 It(e,t,n,r,s){let i=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,a=z(e,n,i,r,"sitemap");if(!a)return;if(!_e(a)){r.reject("sitemap",a.toString(),"not sitemap-like",s);return}let u=a.toString();!t.doneSitemaps.has(u)&&!t.queuedSitemaps.has(u)&&(t.queuedSitemaps.add(u),t.sitemapQueue.push(u),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=de(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:et(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 oo(e){return e.split(",").map(t=>t.trim().split(/\s+/)[0]).filter(Boolean)}function ao(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 co(e){return e.replace(/<script\b[^>]*>[\s\S]*?<\/script\s*>/gi,"")}function uo(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 rn(e){return e.trim().replace(/\\/g,"/").replace(/^https?:\/\/[^/]+/i,"").replace(/^\/+/,"")}async function Nr(e,t,n,r){let s=[];await T.mkdir(t,{recursive:true});let i=await T.readdir(e,{withFileTypes:true});for(let a of i){let u=S.join(e,a.name),o=S.join(t,a.name);if(a.isDirectory()){s.push(...await Nr(u,o,n,r));continue}await Br(u,o,n,r)&&s.push(o);}return s}async function lo(e,t){try{let[n,r]=await Promise.all([T.stat(e),T.stat(t)]);if(n.size!==r.size)return !1;let[s,i]=await Promise.all([T.readFile(e),T.readFile(t)]);return s.equals(i)}catch{return false}}async function Br(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 lo(e,t)?(r.debug("Skipped unchanged post-crawl copy target",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):(await T.mkdir(S.dirname(t),{recursive:true}),await T.copyFile(e,t),true)}async function po(e,t,n){let r=Object.entries(e.postCrawlCopyMap||{}).map(([u,o])=>({sourcePath:u.trim(),prefix:String(o||"").trim()}));if(r.length===0)return [];let s=0,i=[],a=new Set([...t.donePages].map(u=>S.resolve(ge(e,u))));n.mark("copy-extra-paths");for(let u of r){let o=u.sourcePath,l=u.prefix;if(!o||!l){n.warn("Skipped post-crawl copy mapping with empty key/value",{sourcePath:o,prefixPath:l});continue}let c=vi(o);if(!c.resolvedPath){n.warn("Skipped post-crawl copy source because alias root is not configured",{sourcePath:o,alias:c.alias||"",requiredEnv:c.requiredEnv||"",prefixPath:l});continue}let d=c.resolvedPath,p=rn(l);if(!p){n.warn("Skipped post-crawl copy mapping with invalid export prefix",{sourcePath:o,prefixPath:l});continue}let f;try{f=await T.stat(d);}catch{n.warn("Skipped post-crawl copy source because it does not exist",{sourcePath:o,sourceAbs:d,prefixPath:l});continue}let g=S.resolve(e.outputDir,p);if(f.isDirectory()){let w=await Nr(d,g,a,n);i.push(...w),s+=w.length,w.length>0&&n.progress(`Copied static directory to export: ${d} -> /${p}`,{phase:"copy-extra-paths",sourcePath:d,targetPath:`/${p}`,copiedItems:s,changedFiles:w.length});continue}let y=S.basename(d),m=l.endsWith("/")?S.join(g,y):g;await Br(d,m,a,n)&&(i.push(m),s++,n.progress(`Copied static file to export: ${d} -> /${rn(l.endsWith("/")?`${l}${y}`:l)}`,{phase:"copy-extra-paths",sourcePath:d,targetPath:`/${rn(l.endsWith("/")?`${l}${y}`:l)}`,copiedItems:s}));}return n.endMark("copy-extra-paths",{mappedSources:r.length,copiedItems:s}),i}function rt(e,t,n,r,s){let i=new Set,a=de(n).replace(/\\\//g,"/"),u=s==="page-html"?co(a):a,o=uo(t,s);if(o){for(let c of ao(u)){let d=z(e,c,t,r,`${s}:css-url`);d&&B(e,d)&&i.add(d.toString());}for(let c of u.matchAll(/@import\s+(?:url\()?\s*['"]?([^'"\s;]+)['"]?\s*\)?/gi)){let d=z(e,c[1],t,r,`${s}:css-import`);d&&B(e,d)&&i.add(d.toString());}}if(o||s==="page-html")for(let c of u.matchAll(/<\?xml-stylesheet[^>]+href=["']([^"']+)["'][^>]*\?>/gi)){let d=z(e,c[1],t,r,`${s}:xml-stylesheet`);d&&B(e,d)&&i.add(d.toString());}let l=new RegExp(`(?:https?:)?//[^\\s'"<>\\);]+\\.${tn}(?:\\?[^\\s'"<>\\);]*)?|(?<!\\.)/[^\\s'"<>\\);]+\\.${tn}(?:\\?[^\\s'"<>\\);]*)?`,"gi");if(s==="page-html")for(let c of a.matchAll(l)){let d=c[0].startsWith("//")?`${new URL(e.sourceOrigin).protocol}${c[0]}`:c[0];if(an(d)){r.ignore(`${s}:serialized-url`,d,"looks like JavaScript/code fragment",t);continue}let p=z(e,d,t,r,`${s}:serialized-url`);p&&B(e,p)?i.add(p.toString()):p&&r.ignore("asset",p.toString(),"serialized URL did not pass safe asset path filter",t);}if(s!=="page-html"&&to(t))for(let c of lr(a)){if(an(c)||!/\.(?:js|mjs)(?:[?#]|$)/i.test(c))continue;let d=z(e,c,t,r,`${s}:relative-asset`);d&&B(e,d)&&i.add(d.toString());}return [...i]}function go(e,t,n,r){let s=new Set,i=de(n);for(let a of i.matchAll(/href=["']([^"'#\s][^"']*?)["']/gi)){let u=a[1],o=z(e,u,t,r,"page-link");o&&(Ur(e,o)||Ce(o)&&!qe(o)&&!B(e,o)&&s.add(o.toString()));}return [...s]}async function Cr(e,t,n,r,s,i,a=false,u=[]){let o=new Set(u),l=0,c=new Map,d=(p,f)=>{let g=[f],y=new Set;for(;g.length>0;){let m=g.pop();if(m===p)return true;y.has(m)||(y.add(m),g.push(...c.get(m)??[]));}return false};for(;n.sitemapQueue.length>0;){let p=n.sitemapQueue.shift();n.queuedSitemaps.delete(p);let f=ve(p,r,"sitemap","sitemap-queue");if(!f)continue;let g=f.toString();if(n.doneSitemaps.has(g))continue;n.doneSitemaps.add(g),r.sitemap(`Fetching sitemap ${g}`,{url:g});let y=true;try{let m=await t.get(g,{timeout:6e4});if(y=!1,!m.ok()){if(r.skip("sitemap",g,m.status()),a&&!o.has(g))throw new Error(`Required child content-sync sitemap returned HTTP ${m.status()}: ${g}`);continue}let h=await m.body(),w=m.url()||g,v=ve(w,r,"sitemap-response",g);if(!v)continue;let I=v.toString();n.successfulSitemaps.add(g),n.successfulSitemaps.add(I),o.has(g)&&(n.resolvedSitemapRoots.add(I),l+=1);let j=await cn(e,n.assetMap,I,h,n,r);X(s,e,j.outputPath),Ie(s,j.outputPath);let U=h.toString("utf8");/<sitemapindex(?:\s|>)/i.test(U)?n.sitemapIndexUrls.add(I):n.sitemapLeafUrls.add(I);let $=rt(e,I,U,r,"sitemap");for(let q of $)W(e,n,q,r,I);let C=[];for(let q of io(U)){let R=z(e,q.loc,I,r,"sitemap-loc");if(!R){if(a)throw new Error(`Content-sync sitemap contains an inadmissible location: ${q.loc}`);continue}if(_e(R)){if(d(I,R.toString()))throw new Error(`Sitemap index cycle detected: ${I} -> ${R.toString()}`);if(c.has(I)||c.set(I,new Set),c.get(I).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()}`);It(e,n,R.toString(),r,I);}else if(a){if(Ce(R)&&!B(e,R)){let M=i.requiredUrls.has(R.toString()),b=i.manifest.pages[R.toString()],A=!!b&&!!await Le(b.outputPath);if(!M&&!A)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",I);}else B(e,R)?W(e,n,R.toString(),r,I):Ce(R)?(q.lastmod&&(n.sitemapLastmodByPage[R.toString()]=q.lastmod),nt(e,n,R.toString(),r,I)):r.reject("sitemap-loc",R.toString(),"not page/sitemap/asset-like",I);}i.trackManifest&&(i.manifest.assets[I]={url:I,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 Pe(e,i));}catch(m){r.error(`Failed sitemap ${g}`,{url:g,error:String(m)});let h=a&&o.has(g)&&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 fo(e,t,n,r,s,i,a){let u=z(e,r,e.sourceOrigin,s,"asset-fetch");if(!u||!B(e,u))return;let o=u.toString();if(n.doneAssets.has(o))return;let l=i.enabled||i.strictTargeted?i.previousManifest.assets[o]:void 0,c=i.trackManifest?await vt(e,t,i,o,s):null,d=c?.token??null,p=dr(l?.changeToken,c),f=i.strictTargeted&&(!c?.supported||!c.token)&&pr(e.sourceOrigin,o),g=i.strictTargeted?i.manifest.assets[o]:void 0;if(g?.lastSeenRunId===i.runId&&await Le(g.outputPath)){n.doneAssets.add(o);for(let m of g.discoveredAssets||[])W(e,n,m,s,o);X(a,e,g.outputPath),g.isText&&Ie(a,g.outputPath),s.info(`Resumed completed content-sync asset ${o}`,{url:o,source:"content-sync-checkpoint"}),Ue(n,s);return}if(l&&(p||f)&&await Le(l.outputPath)){i.manifest.assets[o]={...l,changeToken:d,tokenSource:c?.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||[])W(e,n,m,s,o);s.info(`Reused unchanged trusted asset ${o}.`,{url:o,tokenSource:c?.tokenSource??"unknown",phase:"download-assets",source:p?"asset-change-token-match":"content-sync-release-fingerprint"}),Ue(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||[])W(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"}),Ue(n,s),true};s.asset(`Fetching asset ${o}`,{url:o});try{let m=je(e,n.assetMap,o),h=m?await Le(m):null;if(m&&h)try{let M=await H(`fetch asset headers ${o}`,6e4,()=>t.head(o,{timeout:6e4})),b=M.headers();if(M.ok()&&Xi(h,b)){let A=await qr(m);if(A!==null){let P=M.url()||o,E=ve(P,s,"asset-response",o);if(!E)return;let D=E.toString(),L=await cn(e,n.assetMap,D,A,n,s,b);L.wroteFile&&(X(a,e,L.outputPath),be(L.outputPath)&&Ie(a,L.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"}),Ue(n,s);let G=vr(u,b),te=l?.discoveredAssets?[...l.discoveredAssets]:[];if(G||l?.isText){let ce=A.toString("utf8"),F=rt(e,D,ce,s,"asset:cached");(F.length>0||!l)&&(te=F);for(let Ae of te)W(e,n,Ae,s,D);}if(i.trackManifest){let ce=G||!!l?.isText;i.manifest.assets[o]={url:o,outputPath:L.outputPath,isText:ce,contentType:sn(b)||l?.contentType||null,changeToken:c?.supported?c.token:null,tokenSource:c?.supported?c.tokenSource??null:null,discoveredAssets:te,lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,unreferencedRuns:0,rewriteComplete:!ce},delete i.manifest.pages[o],(i.enabled||i.strictTargeted)&&ce&&await Pe(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 v=await H(`read asset response ${o}`,6e4,()=>w.body()),I=w.url()||o,j=ve(I,s,"asset-response",o);if(!j)return;let U=j.toString(),$=w.headers(),C=await cn(e,n.assetMap,U,v,n,s,$);C.wroteFile&&(X(a,e,C.outputPath),be(C.outputPath)&&Ie(a,C.outputPath)),n.doneAssets.add(o),Ue(n,s);let q=vr(u,$),R=[];if(q){let M=v.toString("utf8");R=rt(e,U,M,s,`asset:${S.extname(u.pathname).toLowerCase()||$["content-type"]||"unknown"}`);for(let b of R)W(e,n,b,s,U);}i.trackManifest&&(i.manifest.assets[o]={url:o,outputPath:C.outputPath,isText:q,contentType:sn($),changeToken:c?.supported?c.token:null,tokenSource:c?.supported?c.tokenSource??null:null,discoveredAssets:R,lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,unreferencedRuns:0,rewriteComplete:!q},delete i.manifest.pages[o],(i.enabled||i.strictTargeted)&&q&&await Pe(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),Ue(n,s));}}async function mo(e,t,n,r,s,i){let a=Math.max(1,Number(e.assetDownloadConcurrency||e.concurrency||1)),u=Array.from({length:a},async()=>{for(;n.assetQueue.length>0;){let o=n.assetQueue.shift();o&&(n.queuedAssets.delete(o),await fo(e,t,n,o,r,s,i));}});await Promise.all(u);}function ho(e){let t=Math.ceil(e/1e3);return `${t} second${t===1?"":"s"}`}function pe(e){return Math.max(6e4,e.navigationTimeoutMs*2,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+15e3)}function Ct(e){return Math.max(15e3,Math.min(6e4,pe(e)))}function wo(e){return Math.max(15e3,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+1e4)}function gn(e){return e.stats.pagesCompleted}function Hr(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 yo(e,t,n){let r=Hr(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 Error(`${e} timed out after ${ho(t)}.`));},t);})])}finally{r!==void 0&&globalThis.clearTimeout(r);}}async function So(e,t){let n=wo(e);await t.evaluate(async r=>{await new Promise(s=>{let i=0,a=700,u=Date.now(),o=()=>{window.clearInterval(l),window.scrollTo(0,0),s();},l=window.setInterval(()=>{if(Date.now()-u>=r.maxDurationMs){o();return}window.scrollBy(0,a),i+=a;let c=document.body?.scrollHeight||0,d=document.documentElement?.scrollHeight||0,p=Math.max(c,d);i>=p+window.innerHeight&&o();},120);});},{maxDurationMs:n});}async function bo(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 So(e,t),await t.waitForTimeout(e.readiness.fallbackWaitMs);}async function Po(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 vo(e,t){return e.noJavaScriptRenderPathPrefixes.some(n=>ln(t,n))}async function Co(e,t,n,r,s=true){let i=new Set;if(s){let u=await t.evaluate(()=>{let o=new Set,l=["href","src","poster","data-src","data-lazy-src","data-original","data-bg","data-background","data-href"],c=new Set,d=["srcset","data-srcset","data-lazy-srcset"],p=f=>{f.querySelectorAll("*").forEach(g=>{for(let m of l){let h=g.getAttribute(m);h&&o.add(h);}for(let m of d){let h=g.getAttribute(m);h&&h.split(",").forEach(w=>{let v=w.trim().split(/\s+/)[0];v&&o.add(v);});}let y=g.getAttribute("style");y&&c.add(y),g.shadowRoot&&p(g.shadowRoot);});};return p(document),{attrs:[...o],styles:[...c]}});for(let o of u.attrs)for(let l of oo(o)){let c=z(e,l,n,r,"dom-attr");c&&B(e,c)&&i.add(c.toString());}for(let o of u.styles)for(let l of rt(e,n,o,r,"dom-style"))i.add(l);}let a=await t.content();for(let u of rt(e,n,a,r,"page-html"))i.add(u);return [...i]}async function Rr(e,t,n,r,s){let i=ge(e,t);await Ye(i,n,"utf8");let a=await T.stat(i);s?.debug(`Saved page output ${t}`,{phase:"save-pages",url:t,outputPath:i,bytes:a.size}),r.stats.pagesSaved++;let u=Math.min(r.stats.pagesQueued,gn(r)+1);s&&(r.stats.pagesSaved%10===0||r.stats.pagesSaved===1)&&s.progress(`Page progress: processed ${u}, rendered ${r.stats.pagesRendered}, saved ${r.stats.pagesSaved}, discovered ${r.stats.pagesQueued}.`,{donePages:u,claimedPages:r.donePages.size,pagesRendered:r.stats.pagesRendered,pagesSaved:r.stats.pagesSaved,pagesQueued:r.stats.pagesQueued,pageQueue:r.pageQueue.length,phase:"save-pages"});}function Ro(e){let t=gn(e);return t%5===0||t===1||t===e.stats.pagesQueued}function Io(e,t){let n=gn(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 xo(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 Ao(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 xo(r),r}async function To(e,t,n,r,s,i,a,u){let o=await chromium.launch({headless:true}),l=Hr(o);l.pid!==null&&n.debug(`Launched crawl worker browser PID ${l.pid}`,{browserPid:l.pid});try{for(;t.pageQueue.length>0&&!(e.maxPages>0&&t.donePages.size>=e.maxPages);){let c=t.pageQueue.shift();if(!c)break;t.queuedPages.delete(c);let d=null,p=null,f=[],g=new Set;try{let y=ve(c,n,"page","page-queue");if(!y)continue;let m=y.toString();if(c=m,t.queuedPages.delete(c),t.donePages.has(c))continue;t.donePages.add(c),i.enabled&&i.seenPages.add(c);let h=pn(e,c),w=vo(e,y.pathname);if(!Ce(y)||B(e,y)||qe(y)){if(B(e,y)){n.info(`Worker redirected non-page URL to asset queue: ${c}`,{url:c,source:"worker-guard"}),W(e,t,c,n,"worker-guard");continue}n.reject("page",c,"guard rejected non-page URL before rendering","worker");continue}let v=i.strictTargeted?i.manifest.pages[c]:void 0;if(v?.lastSeenRunId===i.runId&&await Le(v.outputPath)){i.seenPages.add(c);for(let P of v.discoveredAssets||[])W(e,t,P,n,c);X(a,e,v.outputPath),Ie(a,v.outputPath),n.info(`Resumed completed content-sync page ${c}`,{url:c,source:"content-sync-checkpoint"});continue}let I=await Ei(e,s,t,i,c,n);if(i.enabled&&r!=="single-url"&&r!=="content-sync"){let P=i.manifest.pages[c];if(I.action==="reuse"&&P){for(let E of P.discoveredAssets)W(e,t,E,n,c);for(let E of P.discoveredPages)nt(e,t,E,n,c);P.lastSeenRunId=i.runId,P.sitemapLastmod=et(t.sitemapLastmodByPage[c])??P.sitemapLastmod,I.changeToken?.supported&&(P.changeToken=I.changeToken.token,P.tokenSource=I.changeToken.tokenSource??P.tokenSource),n.info(`Incremental reuse skipped unchanged page ${c}`,{url:c,mode:"incremental",reason:I.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}d=await H(`create page context ${c}`,pe(e),()=>Ao(o,e,w)),p=await H(`create page ${c}`,pe(e),()=>d.newPage()),f=await d.cookies();let j=!1,U=new Set;await p.route("**/*",async P=>{let E=P.request();try{let D=z(e,E.url(),e.sourceOrigin,n,"network-request");D&&B(e,D)&&(U.add(D.toString()),W(e,t,D.toString(),n,c));}catch{}if(E.isNavigationRequest()&&E.frame()===p.mainFrame())try{let D=new URL(E.url()),L=new URL(m);if(D.hash="",L.hash="",D.href===L.href){if(j){n.warn(`Blocked same-page navigation/reload for ${c}`,{url:c,requestUrl:E.url()}),await P.abort("aborted");return}j=!0;}}catch{}await P.continue();}),p.on("response",P=>{try{let D=z(e,P.url(),e.sourceOrigin,n,"network-response");D&&B(e,D)&&(U.add(D.toString()),W(e,t,D.toString(),n,c));}catch{}let E=P.headerValues("set-cookie").then(D=>{for(let L of D)u.add(mr(L,P.url()));}).catch(()=>{}).then(()=>{});g.add(E),E.finally(()=>g.delete(E));}),n.page(`Rendering ${c}`,{url:c});let $=null,C=null;try{if($=await p.goto(c,{waitUntil:"domcontentloaded",timeout:e.navigationTimeoutMs}),$&&$.ok())try{C=await H(`read response body ${c}`,pe(e),()=>$.text());}catch{C=null;}}catch(P){n.warn(`Navigation issue for ${c}; saving current DOM if available`,{url:c,error:String(P)});}if(r==="content-sync"&&$){let P=ve($.url(),n,"content-sync-final-response",c)?.toString();if(P&&P!==m){if(i.deleteOnMissingUrls.has(c)){await un(e,i,a,c,i.deleteOnMissingUrls),n.info(`Removed stale content-sync archive route redirected to ${P}`,{url:c,finalResponseUrl:P,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${c} -> ${P}`)}}if(h){let P=$?.status()??null;if(P!==404){n.error(`Generated 404 request path returned unexpected status for ${c}`,{url:c,expectedStatus:404,status:P,requestPath:e.generated404RequestPath});continue}n.info(`Capturing rendered 404 page for ${c}`,{url:c,status:P,outputPath:ge(e,c)});}else if($&&!$.ok()){if(r==="content-sync"&&($.status()===404||$.status()===410)&&i.deleteOnMissingUrls.has(c)){await un(e,i,a,c,i.deleteOnMissingUrls),n.info(`Removed stale content-sync archive page ${c}`,{url:c,status:$.status(),source:"content-sync-tombstone"}),await p.close();continue}if(r==="content-sync")throw new Error(`Required content-sync route returned HTTP ${$.status()}: ${c}`);n.skip("page",c,$.status(),t.pageDiscoverySources.get(c)),await p.close();continue}w?(n.info(`Rendering without JS execution for ${c}`,{url:c,mode:"no-js"}),await H(`wait for rendered page ${c}`,pe(e),()=>Po(e,p))):await H(`wait for rendered page ${c}`,pe(e),()=>bo(e,p)),t.stats.pagesRendered++;let q=await H(`extract rendered assets ${c}`,pe(e),()=>Co(e,p,m,n,!w));for(let P of q)W(e,t,P,n,c);let R=[...new Set([...q,...U])],M=C??await H(`capture rendered html ${c}`,pe(e),()=>p.content()),b=r!=="single-url"&&r!=="content-sync"&&!h?go(e,c,M,n):[];if(r!=="single-url"&&r!=="content-sync"&&!h)for(let P of b)nt(e,t,P,n,c);await Rr(e,c,M,t,n);let A=ge(e,c);if(X(a,e,A),Ie(a,A),i.trackManifest){let P=$?.headers();i.manifest.pages[c]={url:c,outputPath:A,contentType:sn(P),changeToken:I.changeToken?.supported?I.changeToken.token:null,tokenSource:I.changeToken?.supported?I.changeToken.tokenSource??null:null,sitemapLastmod:et(t.sitemapLastmodByPage[c])??null,discoveredPages:b,discoveredAssets:R,lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,rewriteComplete:!1},(i.enabled||i.strictTargeted)&&await Pe(e,i);}}catch(y){if(i.strictTargeted)throw n.error(`Failed required content-sync page ${c}`,{url:c,error:String(y)}),y;try{let m=p?await H(`capture partial html ${c}`,pe(e),()=>p.content()):"";if(m&&m.trim()){await Rr(e,c,m,t,n);let h=ge(e,c);if(X(a,e,h),Ie(a,h),i.trackManifest){let w=i.manifest.pages[c]??i.previousManifest.pages[c];i.manifest.pages[c]={url:c,outputPath:h,contentType:w?.contentType??null,changeToken:null,tokenSource:null,sitemapLastmod:et(t.sitemapLastmodByPage[c])??w?.sitemapLastmod??null,discoveredPages:w?.discoveredPages?[...w.discoveredPages]:[],discoveredAssets:w?.discoveredAssets?[...w.discoveredAssets]:[],lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,rewriteComplete:!1},(i.enabled||i.strictTargeted)&&await Pe(e,i);}n.warn(`Saved partial DOM for ${c}`,{url:c,error:String(y)});}}catch(m){n.error(`Could not save partial DOM for ${c}`,{url:c,error:String(m)});}n.error(`Failed page ${c}`,{url:c,error:String(y)});}finally{if(g.size>0&&await Promise.allSettled([...g]),d)try{let y=await d.cookies();u.addAll(hr(f,y));}catch{}t.stats.pagesCompleted++,Ro(t)&&Io(t,n),p&&await H(`close page ${c}`,Ct(e),()=>p.close()).catch(y=>{n.warn(`Timed out while closing page ${c}`,{url:c,browserPid:l.pid,error:String(y)});}),d&&await H(`close page context ${c}`,Ct(e),()=>d.close()).catch(y=>{n.warn(`Timed out while closing page context for ${c}`,{url:c,browserPid:l.pid,error:String(y)});});}}}finally{await H("close worker browser",Ct(e),()=>o.close()).catch(c=>(n.warn("Timed out while closing crawl worker browser",{browserPid:l.pid,error:String(c)}),yo(o,n,"browser.close timeout")));}}async function ko(e){try{let t=await T.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 Ir(e){let t=String(e||"").toLowerCase();return t.includes("timeout")||t.includes("timed out")||t.includes("navigation timeout")}function $o(e){let t=String(e?.job?.command||"").trim(),n=String(e?.job?.crawlMode||"full").trim();return t!=="publish"&&t!=="crawl"?false:n!=="incremental"}function Eo(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 Wr(e){try{let t=await T.readFile(e,"utf8"),n=JSON.parse(t);return n&&typeof n=="object"&&!Array.isArray(n)?n:null}catch{return null}}function xr(e){let t=String(e.originalFileName||"").toLowerCase();return t.endsWith("errors.jsonl")?0:t.endsWith("errors.json")?1:2}function Mo(e,t){let n=Array.isArray(t?.artifacts)?t.artifacts.filter(r=>String(r?.role||"").trim()==="errors"&&String(r?.storedFileName||"").trim()!=="").sort((r,s)=>xr(r)-xr(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 Ar(e){let t=await Wr(S.join(e,"job.json"));for(let n of Mo(e,t)){let r=await ko(n);if(r.length>0)return r}return []}async function Do(e){let t=S.join(e.logDir,"archive"),n;try{n=await T.readdir(t);}catch{return ""}let r="",s=0;for(let i of n){let a=S.join(t,i);if(!(await T.stat(a).catch(()=>null))?.isDirectory())continue;let o=await Wr(S.join(a,"job.json"));if(!$o(o))continue;let l=Eo(o);l>=s&&(s=l,r=a);}return r}async function Fo(e){let t=await Do(e),n=t!==""?await Ar(t):await Ar(e.logDir),r=new Set;for(let s of n)if(Ir(s.error)||Ir(s.message)){let i=s.url;typeof i=="string"&&i&&r.add(i);}return [...r]}async function Oo(e){let t=[];for(let n of e)if(n.startsWith("@")){let r=n.slice(1),s=await T.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 Uo(e){if(!e)throw new Error("Content-sync crawl requires an immutable impact plan.");let t=JSON.parse(await T.readFile(e,"utf8"));return Je(t),t}function Lo(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(/^\/+/,""),u=String(r.paginationBase||"page").replace(/^\/+|\/+$/g,"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return n.pathname.startsWith(`${i}/`)&&new RegExp(`^${u}/\\d+/?$`).test(a)})}function Pt(e,t,n){let r=n==="page"?ge(e,t):At(e,t).originalFilePath;return Se(e.outputDir,r)}async function un(e,t,n,r,s){let i=t.manifest.pages[r];if(!i)return;let a=Se(e.outputDir,i.outputPath);if(!a)throw new Error(`Manifest-owned page path escaped output root: ${r}`);let u=S.resolve(i.outputPath);Object.entries(t.manifest.pages).some(([l,c])=>l!==r&&!s.has(l)&&S.resolve(c.outputPath)===u)||(await T.rm(i.outputPath,{force:true}),n.deletedFiles.add(a)),delete t.manifest.pages[r];}function jo(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{_e(new URL(a))&&r.push(a);}catch{}}return n}async function qo(e,t,n,r,s){let i=jo(n.previousManifest,s);for(let a of i){if(t.successfulSitemaps.has(a))continue;let u=n.previousManifest.assets[a];if(!u)continue;let o=Se(e.outputDir,u.outputPath);if(!o)throw new Error(`Manifest-owned sitemap path escaped output root: ${a}`);await T.rm(u.outputPath,{force:true}),r.deletedFiles.add(o),delete n.manifest.assets[a];}await Pe(e,n);}async function _o(e){try{let t=await T.readFile(S.join(e.outputDir,"asset-map.json"),"utf8");return JSON.parse(t)}catch{return {}}}async function No(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 T.mkdir(e,{recursive:true}),await Promise.all(t.map(async n=>{try{await T.unlink(S.join(e,n));}catch(r){if(r.code!=="ENOENT")throw r}}));}function Tr(e,t,n,r,s){let i=z(e,n,e.sourceOrigin,r,"manual-url");i&&(_e(i)?It(e,t,i.toString(),r,s):B(e,i)?W(e,t,i.toString(),r,s):Ce(i)?nt(e,t,i.toString(),r,s):r.reject("manual-url",i.toString(),"not page/sitemap/asset-like",s));}async function zr(e=process.argv.slice(2)){let t=[],n=await se(void 0,{onRemotePublisherConfigDiagnostic:b=>{t.push(b);}}),r=Ke(n.sitemapPaths),s=t.at(-1)??null,i=yi(e),a=i.mode==="content-sync"?await Uo(i.contentSyncPlanPath):null;if(i.resumeRewrite&&i.mode!=="full")throw new Error("--resume-rewrite is only supported for full crawl/publish jobs.");let u=[];i.mode==="retry-timeouts"&&(u=await Fo(n)),i.mode==="single-url"&&(u=await Oo(i.urls)),a&&(u=[...a.directRenderUrls,...a.archiveFamilyUrls]);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 c=(i.mode==="full"||i.mode==="single-url"||i.mode==="content-sync")&&!i.resumeRewrite,d=i.mode!=="full"||i.resumeRewrite||l,p=d?await _o(n):{},f=l||i.mode==="single-url"||i.mode==="content-sync"?await Ii(n):Mr(),g=new Set(u),y=new Set;if(a){for(let b of a.archiveFamilies)if(b.foundPosts===0)for(let A of b.pageUrls)y.add(A);for(let b of Object.keys(f.pages))Lo(b,a)&&!g.has(b)&&(u.push(b),y.add(b));}let m={enabled:l,trackManifest:c,manifest:on(f),previousManifest:on(f),runId:a?.jobId??`${Date.now()}`,seenPages:new Set,changeTokenCache:new Map,manifestSaveChain:Promise.resolve(),strictTargeted:!!a,requiredUrls:g,deleteOnMissingUrls:y};await No(n.logDir);let h=new ht(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&&!l){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:{...p},assetVariantDigestsByOriginalPath:new Map,stats:{pagesQueued:0,pagesRendered:0,pagesCompleted:0,assetsQueued:0,sitemapsQueued:0,assetsSaved:0,pagesSaved:0}},v=Ri(),I=new yt,j=Si();if(j&&!i.resumeRewrite&&await St(j,I.artifact()),await De(n),await Kn(n,{startedAt:new Date().toISOString(),outputDir:S.resolve(n.outputDir)}),d||await T.rm(n.outputDir,{recursive:true,force:true}),await T.mkdir(n.outputDir,{recursive:true}),a){let b=new Set([...a.deleteUrls,...m.deleteOnMissingUrls]);for(let A of a.deleteUrls)await un(n,m,v,A,b);}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(P=>It(n,w,P,h)),await Cr(n,b,w,h,v,m),n.seedPaths.forEach(P=>Tr(n,w,P,h,"seed-path")),n.generated404RequestPath&&nt(n,w,n.generated404RequestPath,h,"generated-404-request-path")):(u.forEach(P=>Tr(n,w,P,h,"cli")),a?.sitemapUrls.forEach(P=>It(n,w,P,h,"content-sync-impact")),await Cr(n,b,w,h,v,m,i.mode==="content-sync",a?.sitemapUrls??[]),a&&await qo(n,w,m,v,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 St(j,I.artifact());return}h.mark("render-pages");let A=Array.from({length:Math.max(1,n.concurrency)},()=>To(n,w,h,i.mode,b,m,v,I));if(await Promise.all(A),j&&await St(j,I.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 Ti(n,b,m,[...w.assetQueue],h),await mo(n,b,w,h,m,v),h.endMark("download-assets",{assets:w.doneAssets.size}),m.enabled&&n.maxPages===0&&await Di(n,w,m,h,v),i.mode==="full"){let P=await po(n,w,h);for(let E of P)X(v,n,E);}}finally{await H("dispose crawl request context",Ct(n),()=>b.dispose()).catch(A=>{h.warn("Timed out while disposing crawl request context",{error:String(A)});});}}m.trackManifest&&await Pe(n,m),await T.writeFile(S.join(n.outputDir,"asset-map.json"),JSON.stringify(w.assetMap,null,2),"utf8"),JSON.stringify(p,null,2)!==JSON.stringify(w.assetMap,null,2)&&X(v,n,S.join(n.outputDir,"asset-map.json"));let U=i.mode==="single-url"||i.mode==="content-sync"?Bi(n,w):m.enabled&&!i.resumeRewrite?Fi(n,m,v,p,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/${U?.length??0}`:"Rewriting text files...";h.progress($,{phase:"rewrite-text",index:0,totalFiles:U?.length,changedTextFiles:0}),await Pr($);let C=Date.now(),q=await gt(n,w.assetMap,{files:U,previousAssetMap:p,onStart:({totalFiles:b,workerCount:A})=>{h.info(`Rewrite phase scanning ${b} text files.`,{phase:"rewrite-text",totalFiles:b,workerCount:A});},onProgress:async({index:b,totalFiles:A,changedTextFiles:P,file:E,changed:D})=>{D&&X(v,n,S.join(n.outputDir,E)),h.checkpoint(`Rewriting text file ${b}/${A}`,{phase:"rewrite-text",index:b,totalFiles:A,changedTextFiles:P,file:E}),h.debug(`${D?"Rewrote":"Checked unchanged"} text file ${b}/${A}: ${E}`,{phase:"rewrite-text",index:b,totalFiles:A,changedTextFiles:P,file:E,changed:D});let L=Date.now();if(!(b===1||b===A||b%50===0||L-C>=5e3))return;C=L;let te=`Rewriting text files: ${b}/${A}`;h.progress(te,{phase:"rewrite-text",index:b,totalFiles:A,changedTextFiles:P,file:E}),await Pr(te);}});m.trackManifest&&(Oi(m,U),await Pe(n,m)),h.endMark("rewrite-text",{changedTextFiles:q});let R=a?[...v.deletedFiles].sort():m.enabled&&!i.resumeRewrite?await Li(n,w,m,v,h):[],M=[...new Set((U||[]).map(b=>Se(n.outputDir,S.isAbsolute(b)?b:S.join(n.outputDir,b))).filter(b=>b!==null))].sort();if(a){let b=[...v.changedFiles].sort(),A=new Set(b),P=new Set(a.directRenderUrls.map(F=>Pt(n,F,"page")).filter(F=>!!F&&A.has(F))),E=new Set(a.archiveFamilyUrls.map(F=>Pt(n,F,"page")).filter(F=>!!F&&A.has(F))),D=new Set([...w.sitemapLeafUrls].map(F=>Pt(n,F,"asset")).filter(F=>!!F&&A.has(F))),L=new Set([...w.sitemapIndexUrls].map(F=>Pt(n,F,"asset")).filter(F=>!!F&&A.has(F))),G=new Set([...P,...E,...D,...L]),te=b.filter(F=>!G.has(F)),ce=[...new Set([...a.directRenderUrls,...a.archiveFamilyUrls,...w.resolvedSitemapRoots,...w.sitemapLeafUrls,...w.sitemapIndexUrls,...a.deleteUrls].map(F=>{let Ae=new URL(F);return `${Ae.pathname}${Ae.search}`}))].sort();await Gt(n,{schemaVersion:2,generatedAt:new Date().toISOString(),outputDir:S.resolve(n.outputDir),runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,changedFiles:b,deletedFiles:R,rewriteTargets:M,consumerId:a.consumerId,fromSequence:a.fromSequence,toSequence:a.toSequence,impactHash:a.impactHash,resolvedSitemapUrls:[...w.resolvedSitemapRoots],uploadGroups:{assets:te,content:[...P].sort(),listings:[...E].sort(),sitemapLeaves:[...D].sort(),sitemapIndexes:[...L].reverse()},invalidationPaths:ce});}else await Gt(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?[...v.changedFiles].sort():[],deletedFiles:m.enabled&&!i.resumeRewrite?R:[],rewriteTargets:m.enabled&&!i.resumeRewrite?M:[]});await Zn(n),h.summary(i.resumeRewrite?`Done. Resumed final rewrite over existing output and changed ${q} 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:q}),await h.flush();}function Bo(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 ee(e,t,n){let r=new AbortController,s=globalThis.setTimeout(()=>{r.abort(new Error(`${e} timed out after ${Bo(t)}.`));},t);try{return await n({abortController:r,httpOptions:{abortSignal:r.signal,requestTimeout:t}})}finally{globalThis.clearTimeout(s);}}function Ho(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Tt(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 Wo(e,t){return {...e,...t??{}}}function zo(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function Ne(e,t){let n=String(t??e.deploymentTargetOverride??"").trim();if(!Ho(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:Wo(e.s3,s.s3),cloudFront:zo(e.cloudFront,s.cloudFront)}}}var Kr="wpsuite-sha256",Gr="wpsuite-normalized-sha256",ea=/(?:^|[-_:.])(id|uid|nonce|token|hash|instance)(?:$|[-_:.])/i,Sn=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,ta=["SIGINT","SIGTERM"],Et=new Set,kt=new Map,Qr=null;function na(e){Et.add(e);let t=false;return ()=>{t||(t=true,Et.delete(e));}}async function ra(){let e=[...Et];e.length!==0&&await Promise.allSettled(e.map(async t=>{Et.delete(t),await T.rm(t,{recursive:true,force:true});}));}function sa(){if(!(kt.size>0))for(let e of ta){let t=()=>{Qr||(Qr=ra().finally(()=>{for(let[n,r]of kt)process.off(n,r);kt.clear(),process.exit(1);}));};kt.set(e,t),process.on(e,t);}}sa();function ia(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 Zr(e){return S.normalize(e).split(S.sep).join("/")}function Jr(e,t){let n=S.relative(S.resolve(e),S.resolve(t));return !n||n.startsWith("..")||S.isAbsolute(n)?null:Zr(n)}async function oa(e,t,n){let r=ia(e.outputDir,t),s;try{s=await T.readFile(r,"utf8");}catch(c){return c.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:c instanceof Error?c.message:String(c),source:"deploy-guard"}),null)}let i;try{i=JSON.parse(s);}catch(c){return n.warn("Deploy rewrite guard disabled because the crawl manifest is invalid JSON.",{manifestPath:r,error:c instanceof Error?c.message:String(c),source:"deploy-guard"}),null}let a=new Map,u=new Map,o=0,l=0;for(let[c,d]of Object.entries(i.pages||{})){let p=String(d?.outputPath||"").trim(),f=p?Jr(e.outputDir,p):null;if(f){let g=typeof d?.contentType=="string"&&d.contentType?d.contentType:null;g&&u.set(f,g);}!p||d?.rewriteComplete===true||f&&(o++,a.has(f)||a.set(f,{kind:"page",sourceUrl:c,outputPath:S.resolve(p),relativePath:f}));}for(let[c,d]of Object.entries(i.assets||{})){let p=String(d?.outputPath||"").trim(),f=d?.isText===true,g=p?Jr(e.outputDir,p):null;if(g){let y=typeof d?.contentType=="string"&&d.contentType?d.contentType:null;y&&u.set(g,y);}!p||!f||d?.rewriteComplete===true||g&&(l++,a.has(g)||a.set(g,{kind:"asset",sourceUrl:c,outputPath:S.resolve(p),relativePath:g}));}return {manifestPath:r,byRelativePath:a,contentTypeByRelativePath:u,pendingPages:o,pendingTextAssets:l}}function aa(e){return e&&(Sn.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 mn(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,n)=>n%2===1?t:aa(t)).join("")}function ca(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,n)=>n%2===1?t:Sn.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 Mt(e,t){return Array.isArray(e)?e.map(n=>Mt(n,t)):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([n,r])=>[n,Mt(r,n)])):typeof e!="string"?e:t&&ea.test(t)?ca(e):Sn.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)||/^[0-9]{8,}$/.test(e)?mn(e):e}function ua(e){return e.replace(/((?:https?:\/\/[^"'\s>]+|\/)(?:wp-content|wp-includes)\/[^"'\s>?]+)\?ver=[A-Za-z0-9._-]+(?=$|[#"'])/gi,"$1")}function la(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(Mt(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(Mt(i));return Buffer.from(a,"utf8").toString("base64")}catch{}return t}function da(e,t){let n=e.toLowerCase();return n==="class"?t.split(/(\s+)/).map((r,s)=>s%2===1?r:mn(r)).join(""):n==="id"||n==="for"||n==="aria-controls"||n==="aria-labelledby"||n==="aria-describedby"||n==="aria-owns"||n==="aria-activedescendant"||n==="name"?mn(t):n==="href"||n==="src"?ua(t):la(n,t)}function pa(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,u,o)=>{let l=da(a,o);return ` ${a}=${u}${l}${u}`});return `<${n}${s}>`})}function fn(e){let t=S.extname(e).toLowerCase();return [".html",".htm",".xml",".xsl",".txt"].includes(t)||["robots.txt","sitemap.xml"].includes(S.basename(e))}function $t(e,t){return [e.replace(/^\/+|\/+$/g,""),t.replace(/^\/+/,"")].filter(Boolean).join("/")}async function ga(e,t,n,r){let s=new Map,i,a=0;do{a++;let u=await ee(`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 u.Contents||[]){if(!o.Key)continue;let l=(o.ETag||"").replace(/^"|"$/g,"").toLowerCase(),c=Number(o.Size??0);s.set(o.Key,{etag:l,size:c});}i=u.NextContinuationToken,r.debug(`Listed S3 keys page ${a}: ${s.size} keys so far`);}while(i);return s}async function fa(e,t,n,r){if(r.has(n))return r.get(n)??null;try{let s=await ee(`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 ma(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 u=await ee(`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(u.Errors&&u.Errors.length>0){let o=u.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 ha(e){return createHash("md5").update(e).digest("hex")}function Vr(e){return createHash("sha256").update(e).digest("hex")}function hn(e){return createHash("sha256").update(pa(e.toString("utf8"))).digest("hex")}async function wa(e,t,n,r){if(r.has(n))return r.get(n)??null;try{let a=((await ee(`S3 head object metadata ${n}`,3e5,({httpOptions:u})=>e.send(new HeadObjectCommand({Bucket:t,Key:n}),u))).Metadata?.[Kr]||"").trim().toLowerCase()||null;return r.set(n,a),a}catch{return r.set(n,null),null}}async function ya(e,t,n,r,s){if(r.has(n))return r.get(n)??null;try{let a=((await ee(`S3 head object normalized metadata ${n}`,3e5,({httpOptions:u})=>e.send(new HeadObjectCommand({Bucket:t,Key:n}),u))).Metadata?.[Gr]||"").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 ee(`S3 get object ${n}`,3e5,({httpOptions:o})=>e.send(new GetObjectCommand({Bucket:t,Key:n}),o))).Body?.transformToByteArray(),u=a?hn(Buffer.from(a)):null;return s.set(n,u),r.set(n,u),u}catch{return s.set(n,null),r.set(n,null),null}}function Sa(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 Pa(e,t,n,r){let s=S.join(e,"asset-map.json"),i;try{i=await T.readFile(s,"utf8");}catch(o){if(o.code==="ENOENT")return;throw o}let a=JSON.parse(i);if(!a||typeof a!="object")return;let u=Object.fromEntries(Object.entries(a).map(([o,l])=>{let c=String(l??"");return t.urlRewriteMode==="absolute"&&t.targetOrigin&&n.targetOrigin&&t.targetOrigin!==n.targetOrigin&&(c=c.split(t.targetOrigin).join(n.targetOrigin)),c=ba(c,r),[o,c]}));await T.writeFile(s,JSON.stringify(u,null,2),"utf8");}async function va(e,t,n,r,s,i){let a=e.targetOrigin!==t.targetOrigin,u=Object.keys(r).length>0;if(!n||!a&&!u)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-${Sa(n)}-${Date.now()}`),l=na(o);s.info(`Preparing deployment profile "${n}" from existing crawl output to ${o}...`);try{if(await T.rm(o,{recursive:!0,force:!0}),i){await T.mkdir(o,{recursive:!0});for(let f of i){let g=S.join(e.outputDir,f),y=S.join(o,f);await T.mkdir(S.dirname(y),{recursive:!0}),await T.copyFile(g,y);}}else await T.cp(e.outputDir,o,{recursive:!0});let c={...e,sourceOrigin:e.targetOrigin,targetOrigin:t.targetOrigin,extraReplacements:r,outputDir:o},d=1;s.info(`Rewriting text files for deployment profile "${n}"...`);let p=await gt(c,{},{files:i,onStart:({totalFiles:f,workerCount:g})=>{d=g,s.info(`Rewriting ${f} text file(s) for deployment profile "${n}" with ${g} worker(s).`);},onProgress:({index:f,totalFiles:g,changedTextFiles:y,file:m,changed:h})=>{let v=`Rewriting [${f}/${g}] ${m} (${h?"rewrote":"checked"}; ${y} changed so far)`;g<=200||f<=5||f%10===0||f===g?(s.progress(v,{file:m,index:f,totalFiles:g,changed:h,changedTextFiles:y,workerCount:d}),s.info(v)):s.debug(v);}});return await Pa(o,e,t,r),s.info(`Prepared deployment profile "${n}" in ${o} (${p} rewritten text file(s)).`),{outputDir:o,cleanup:async()=>{l(),await T.rm(o,{recursive:!0,force:!0});}}}catch(c){throw s.error(`Failed to prepare deployment profile "${n}" temporary output.`,{tempDir:o,outputDir:e.outputDir,error:c instanceof Error?c.message:String(c),...c instanceof Error&&c.stack?{stack:c.stack}:{}}),await T.rm(o,{recursive:true,force:true}).catch(()=>{}),l(),await s.flush().catch(()=>{}),c}}async function Yr(e=process.argv.slice(2)){let t=await se(),n=Tt(e),r=Ne(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"):"",u=new Oe(s,"deploy.log.jsonl",r.config.logLevel??"info"),o=r.profile?.extraReplacements??{},l=async()=>{};try{let c=await Me(t);if(e.includes("--content-sync")&&c?.schemaVersion!==2)throw new Error("Content-sync deployment requires a valid schema-2 targeted deploy plan.");let p=await Gn(t);if(p)throw new Error(`Deployment blocked: the crawl started at ${p.startedAt} did not complete its final rewrite. Run a successful crawl or resume the rewrite before deploying.`);let f=await va(t,r.config,r.name,o,u,c?.schemaVersion===2?c.changedFiles:void 0);l=f.cleanup;let g={...r.config,outputDir:f.outputDir},y=g.s3SyncMode??"sdk-upload-delete";if(c?.schemaVersion===2&&c.deletedFiles.length>0&&y!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let m=c?.schemaVersion!==2&&f.outputDir!==t.outputDir&&(t.targetOrigin!==r.config.targetOrigin||Object.keys(o).length>0),h=!!c&&!c.fullSyncRequired&&!m;if(c?.schemaVersion===2&&!h)throw new Error("Content-sync deployment cannot widen into a full-output deployment.");r.name&&u.info(`Using deployment profile: ${r.name}`),u.info(`Starting deploy \u2014 s3://${g.s3.bucket}/${g.s3.prefix||""} (region: ${g.s3.region})`),u.info(`S3 sync mode: ${y}`),c&&c.fullSyncRequired?u.info("Deploy plan requests a full sync; scanning the whole output tree."):c&&m?u.info("Ignoring incremental deploy plan because deployment-profile rewrites require a full sync."):h&&u.info(`Using incremental deploy plan: ${c.changedFiles.length} changed file(s), ${c.deletedFiles.length} deleted file(s).`),u.mark("deploy");let w=new S3Client({region:g.s3.region}),v=f.outputDir===t.outputDir?await oa(t,i,u):null,I=c?.schemaVersion===2?[...c.uploadGroups.assets,...c.uploadGroups.content,...c.uploadGroups.listings,...c.uploadGroups.sitemapLeaves,...c.uploadGroups.sitemapIndexes]:c?.changedFiles??[];if(c?.schemaVersion===2){let k=[...new Set(I)],J=[...new Set(c.changedFiles)];if(k.length!==J.length||J.some(x=>!k.includes(x)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let j=h?(await Promise.all(I.map(async k=>{try{return (await T.stat(S.join(g.outputDir,k))).isFile()?k:null}catch{if(c?.schemaVersion===2)throw new Error(`Content-sync planned upload is missing: ${k}`);return null}}))).filter(k=>k!==null):await qs(["**/*"],{cwd:g.outputDir,onlyFiles:!0,dot:!0}),U=[],$=[],C=j.filter(k=>{let J=v?.byRelativePath.get(Zr(k));if(!J)return !0;let x=$t(g.s3.prefix,k);return U.push(k),$.push(x),u.reject(J.kind,J.sourceUrl||k,"rewrite not completed; deploy upload blocked","deploy-guard",{file:k,key:x,outputPath:J.outputPath,manifestPath:v?.manifestPath}),!1}),q=new Set((h?C:j).map(k=>$t(g.s3.prefix,k)));v&&(v.pendingPages>0||v.pendingTextAssets>0)&&u.info(`Deploy rewrite guard loaded ${v.pendingPages} pending page(s) and ${v.pendingTextAssets} pending text asset(s) from crawl manifest.`,{manifestPath:v.manifestPath,pendingPages:v.pendingPages,pendingTextAssets:v.pendingTextAssets,source:"deploy-guard"}),U.length>0&&u.warn(`Deploy rewrite guard blocked ${U.length} file(s) from upload because final crawl rewrite is incomplete.`,{rejected:U.length,manifestPath:v?.manifestPath,source:"deploy-guard"}),h?u.info(`Using ${C.length} eligible planned changed file(s) from deploy plan for upload${U.length>0?` (${U.length} blocked by deploy rewrite guard)`:""}.`):u.info(`Found ${j.length} file(s) in output directory "${g.outputDir}"${U.length>0?`; ${C.length} remain eligible for upload after deploy rewrite guard filtering`:""}.`),u.progress("Deploy preflight: comparing local output with remote S3 state.",{phase:"deploy",mode:y,totalFiles:C.length,scannedFiles:j.length,rejected:U.length,deploymentProfile:r.name||"",s3Bucket:g.s3.bucket,s3Prefix:g.s3.prefix||"",targetedPlan:h,deployGuardEnabled:f.outputDir===t.outputDir});let R=0,M=0,b=0,A=[],P=[],E=[],D=[],L=[],G=new Map,te=new Map,ce=new Map,F=new Map,Ae=new Map;h||(u.info("Listing existing S3 objects to detect unchanged files..."),G=await ga(w,g.s3.bucket,g.s3.prefix,u),u.info(`Found ${G.size} existing key(s) in S3.`));let os=async k=>G.has(k)?G.get(k)??null:h?fa(w,g.s3.bucket,k,te):null;for(let k of C){let J=S.join(g.outputDir,k),x=$t(g.s3.prefix,k),ne=v?.contentTypeByRelativePath.get(k),He=ne&&ne!=="application/octet-stream"?ne:Xo.lookup(k)||ne||"application/octet-stream",as=fn(k)?g.s3.htmlCacheControl:g.s3.assetCacheControl,N=R+M+b+1;C.length<=200||N<=5||N%10===0||N===C.length?(u.progress(`Uploading [${N}/${C.length}] ${x} (${He})`,{file:k,key:x,index:N,totalFiles:C.length,contentType:He,mode:y}),u.info(`Uploading [${N}/${C.length}] ${x} (${He})`)):u.debug(`Uploading [${N}/${C.length}] ${x} (${He})`);try{let re=await T.readFile(J),We=await os(x),ze=null,Te=null;if(We){let Ot=re.byteLength;if(We.size===Ot){let it=We.etag.includes("-");if(We.etag!==""&&!it){let cs=ha(re);if(We.etag===cs){M++,C.length<=200||M<=5||M%25===0?(u.progress(`Skipped unchanged [${N}/${C.length}] ${x}`,{file:k,key:x,index:N,totalFiles:C.length,uploaded:R,skipped:M,failed:b,mode:y,reason:"etag-md5-match"}),u.info(`Skipped unchanged [${N}/${C.length}] ${x}`)):u.debug(`Skipped unchanged [${N}/${C.length}] ${x}`),P.push(x);continue}}ze=Vr(re);let In=await wa(w,g.s3.bucket,x,ce);if(In&&In===ze){M++,C.length<=200||M<=5||M%25===0?(u.progress(`Skipped unchanged [${N}/${C.length}] ${x}`,{file:k,key:x,index:N,totalFiles:C.length,uploaded:R,skipped:M,failed:b,mode:y,reason:"metadata-sha256-match"}),u.info(`Skipped unchanged [${N}/${C.length}] ${x}`)):u.debug(`Skipped unchanged [${N}/${C.length}] ${x}`),P.push(x);continue}}if(fn(k)){Te=hn(re);let it=await ya(w,g.s3.bucket,x,F,Ae);if(it&&it===Te){M++,C.length<=200||M<=5||M%25===0?(u.progress(`Skipped semantically unchanged [${N}/${C.length}] ${x}`,{file:k,key:x,index:N,totalFiles:C.length,uploaded:R,skipped:M,failed:b,mode:y,reason:"normalized-html-match"}),u.info(`Skipped semantically unchanged [${N}/${C.length}] ${x}`)):u.debug(`Skipped semantically unchanged [${N}/${C.length}] ${x}`),P.push(x);continue}}}ze=ze??Vr(re),Te=Te??(fn(k)?hn(re):null),await ee(`S3 upload ${x}`,9e5,({abortController:Ot})=>new Upload({client:w,abortController:Ot,params:{Bucket:g.s3.bucket,Key:x,Body:re,ContentType:He,CacheControl:as,Metadata:{[Kr]:ze,...Te?{[Gr]:Te}:{}}}}).done()),R++,A.push(x),(R%25===0||R===C.length)&&(u.progress(`Uploaded ${R}/${C.length} file(s)\u2026`,{uploaded:R,skipped:M,failed:b,totalFiles:C.length,mode:y}),u.info(`Uploaded ${R}/${C.length} file(s)\u2026`));}catch(re){b++,E.push(x),u.error(`Failed to upload ${x}: ${re.message}`,{key:x,error:String(re)});}}if(u.info(`Upload complete: ${R} uploaded, ${M} skipped unchanged, ${$.length} rejected by guard, ${b} failed.`),E.length>0||$.length>0)throw new Error(`Deployment stopped before deletion because ${E.length} upload(s) failed and ${$.length} upload(s) were rejected.`);if(y==="sdk-upload-delete"){let k=new Set((c?.deletedFiles??[]).map(x=>$t(g.s3.prefix,x))),J=[...k].filter(x=>!q.has(x));if(!h&&(L=[...G.keys()].filter(ne=>!q.has(ne)).filter(ne=>!k.has(ne)),L.length>0)){u.warn(`Protected ${L.length} remote-only S3 object(s) from deletion because the crawl did not provide explicit tombstones.`,{protected:L.length,source:"deploy-delete-guard"});for(let ne of L)u.debug(` protected remote-only object: ${ne}`);}D=await ma(w,g.s3.bucket,h?J:J.filter(x=>G.has(x)),u);}else u.info("Sync mode is sdk-upload-only \u2014 skipping stale object deletion.");a&&(await T.mkdir(S.dirname(a),{recursive:!0}),await T.writeFile(a,JSON.stringify({generatedAt:new Date().toISOString(),mode:y,summary:{totalFiles:j.length,eligibleFiles:C.length,uploaded:A.length,skipped:P.length,rejected:$.length,failed:E.length,deleted:D.length,protected:L.length,...r.name?{profile:r.name}:{}},uploadedKeys:A,skippedKeys:P,rejectedKeys:$,failedKeys:E,deletedKeys:D,protectedRemoteOnlyKeys:L},null,2),"utf8")),c?.schemaVersion!==2&&await De(t),u.endMark("deploy"),await u.flush();}finally{await l().catch(()=>{});}}function Ra(e){let t=e;return t.endsWith("/")?t+="index.html":S.posix.basename(t).includes(".")||(t+="/index.html"),t}function Ia(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 Xr(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=Ra(s.pathname);n.add(i),n.add(`${a}${s.search}`);let u=Ia(a,t);u&&n.add(`${u}${s.search}`);}return [...n].sort()}function ka(){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 bn(e,t){if(!e)return;await T.mkdir(S.dirname(e),{recursive:true});let n=`${e}.${process.pid}.${Date.now()}.tmp`;await T.writeFile(n,JSON.stringify({...t,updatedAt:new Date().toISOString()},null,2),"utf8"),await T.rename(n,e);}async function $a(e,t,n,r){if(!e)return null;try{let s=JSON.parse(await T.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,u)=>a!==r[u])?null:s}catch(s){if(s.code==="ENOENT")return null;throw s}}function Ea(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 ts(e=process.argv.slice(2)){let t=await se(),n=Tt(e),r=Ne(t,n),s=r.config,i=new Oe(s.logDir||"logs","invalidate.log.jsonl",s.logLevel??"info"),a=e.includes("--content-sync"),u=await Me(t);if(a&&u?.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=u?.schemaVersion===2?u.invalidationPaths:s.cloudFront.invalidationPaths.length>0?s.cloudFront.invalidationPaths:["/*"],l=u?.schemaVersion===2?Xr(o,s.s3.prefix||""):o;if(u?.schemaVersion===2&&l.some(g=>g==="/*"))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 c=s.cloudFront.distributionId,d=new CloudFrontClient({region:"us-east-1",maxAttempts:5});if(i.mark("invalidate"),u?.schemaVersion!==2){let g=`smartcloud-static-publisher-${Date.now()}`;await ee(`CloudFront invalidation for distribution ${c}`,3e5,({httpOptions:y})=>d.send(new CreateInvalidationCommand({DistributionId:c,InvalidationBatch:{CallerReference:g,Paths:{Quantity:l.length,Items:l}}}),y)),i.endMark("invalidate"),await i.flush();return}let p=ka(),f=await $a(p,c,u.impactHash,l);if(!f){let g=createHash("sha256").update(JSON.stringify(l)).digest("hex");f={schemaVersion:1,distributionId:c,impactHash:u.impactHash,updatedAt:new Date().toISOString(),batches:Ea(l).map((y,m)=>({index:m,callerReference:`wpsuite-content-sync-${u.impactHash.slice(0,24)}-${g.slice(0,24)}-${m}`,paths:y,invalidationId:null,status:"pending"}))},await bn(p,f);}for(let g of f.batches){if(!g.invalidationId){let y=await ee(`CloudFront targeted invalidation batch ${g.index+1}`,3e5,({httpOptions:h})=>d.send(new CreateInvalidationCommand({DistributionId:c,InvalidationBatch:{CallerReference:g.callerReference,Paths:{Quantity:g.paths.length,Items:g.paths}}}),h)),m=String(y.Invalidation?.Id||"").trim();if(!m)throw new Error("CloudFront did not return an invalidation ID.");g.invalidationId=m,g.status="created",await bn(p,f);}await waitUntilInvalidationCompleted({client:d,maxWaitTime:900,minDelay:2,maxDelay:15},{DistributionId:c,Id:g.invalidationId}),g.status="completed",await bn(p,f),i.info(`CloudFront invalidation batch ${g.index+1}/${f.batches.length} completed.`,{invalidationId:g.invalidationId,pathCount:g.paths.length});}i.endMark("invalidate"),await i.flush();}function Ft(e){let t=new URL(e);return t.hash="",t.href}function ns(e,t,n){let r=new Set([...t].map(o=>Ft(o))),s=new Map,i=new Map,a=o=>{s.set(Ft(o),o);},u=o=>{if(!o)return;let l=Ft(o);s.has(l)||i.set(l,o);};for(let o of e.deleteUrls)a(o);for(let o of e.archiveFamilies)if(o.foundPosts===0)for(let l of o.pageUrls)r.has(Ft(l))||a(l);u(e.directRenderUrls[0]);for(let o of e.archiveFamilies)u(o.pageUrls[0]),u(o.pageUrls.at(-1));for(let o of n)u(o);return {expectedAvailable:[...i.values()],expectedMissing:[...s.values()]}}var ss=["claimed","impact-planned","rendered","deployed","invalidated","verified","acknowledged"];function Cn(){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 Be(e,t){return ss.indexOf(e)>=ss.indexOf(t)}async function me(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 T.mkdir(e,{recursive:true}),await T.appendFile(S.join(e,"audit-events.jsonl"),`${JSON.stringify(i)}
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 Ei(e,t,n,r,s,i){let a=pn(e,s);if(!r.enabled)return {action:"render",changeToken:r.trackManifest?await vt(e,t,r,s,i):null};let u=r.manifest.pages[s];if(!u)return {action:"render",changeToken:await vt(e,t,r,s,i)};let o=await vt(e,t,r,s,i);if(o?.supported&&o.token)return u.changeToken===o.token?a&&!await Mi(e,t,s,i)?{action:"render",changeToken:o}:{action:"reuse",changeToken:o}:{action:"render",changeToken:o};let l=et(n.sitemapLastmodByPage[s]);return l&&u.sitemapLastmod&&u.sitemapLastmod===l?{action:"reuse",changeToken:o}:{action:"render",changeToken:o}}async function Mi(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 Di(e,t,n,r,s){let i=Sr(n.previousManifest,void 0,r,"previous-manifest"),a=0;for(let[p,f]of Object.entries(n.manifest.pages))if(!n.seenPages.has(p)){yr(s,e,f.outputPath);try{await T.unlink(f.outputPath);}catch(g){(g.code||"")!=="ENOENT"&&r.warn(`Failed to remove stale incremental page output for ${p}`,{url:p,outputPath:f.outputPath,error:String(g)});}delete n.manifest.pages[p],a++;}let u=Sr(n.manifest,n.runId,r,"active-manifest"),o=new Set,l=new Set;for(let p of u){for(let y of tt(e,p))o.add(y);let f=n.manifest.assets[p]?.outputPath;f&&l.add(f);let g=je(e,t.assetMap,p);g&&l.add(g);}for(let p of Object.keys(n.manifest.assets)){if(u.has(p)){n.manifest.assets[p].unreferencedRuns=0;continue}let f=n.manifest.assets[p];if(gr(f.unreferencedRuns)){f.unreferencedRuns+=1,r.warn(`Deferred deletion of newly unreferenced incremental asset ${p}.`,{url:p,unreferencedRuns:f.unreferencedRuns,phase:"incremental-cleanup",source:"asset-deletion-grace"});continue}delete n.manifest.assets[p];}let c=0,d=0;for(let p of i){if(n.manifest.assets[p])continue;let f=new Set,g=n.previousManifest.assets[p]?.outputPath;g&&f.add(g);let y=je(e,t.assetMap,p);if(!u.has(p)&&y)f.add(y);else if(!u.has(p)){let m=Gi(e,p,r,"incremental-cleanup");m&&(f.add(m.originalFilePath),m.hashedFilePath&&f.add(m.hashedFilePath));}for(let m of f)if(!l.has(m)){yr(s,e,m);try{await T.unlink(m),c++;}catch(h){(h.code||"")!=="ENOENT"&&r.warn(`Failed to remove stale incremental asset output for ${p}`,{url:p,outputPath:m,error:String(h)});}}for(let m of tt(e,p))o.has(m)||Object.prototype.hasOwnProperty.call(t.assetMap,m)&&(delete t.assetMap[m],d++);}(a>0||c>0||d>0)&&r.info(`Incremental cleanup removed ${a} stale pages, ${c} stale assets, ${d} stale asset mappings`,{removedPages:a,removedAssets:c,removedAssetMappings:d,phase:"incremental-cleanup"});}function Sr(e,t,n,r){let s=new Set,i=a=>{let u=a.trim();if(u){if(n){let o=ve(u,n,"asset",r??"manifest");if(!o)return;s.add(o.toString());return}s.add(u);}};for(let a of Object.values(e.pages))for(let u of a.discoveredAssets||[])i(u);for(let[a,u]of Object.entries(e.assets||{}))t&&u.lastSeenRunId!==t||i(a);return s}function br(e,t,n){for(let r of t)for(let s of tt(e,r))if(n.has(s))return true;return false}function Fi(e,t,n,r,s){let i=new Set(n.rewriteTargets);for(let u of n.changedFiles)i.add(S.resolve(e.outputDir,u));for(let u of Object.values(t.manifest.pages))u.rewriteComplete||i.add(S.resolve(u.outputPath));for(let u of Object.values(t.manifest.assets))u.isText&&!u.rewriteComplete&&i.add(S.resolve(u.outputPath));let a=Ai(r,s);if(a.size===0)return [...i];for(let u of Object.values(t.manifest.pages))br(e,u.discoveredAssets,a)&&i.add(S.resolve(u.outputPath));for(let[u,o]of Object.entries(t.manifest.assets)){o.isText&&br(e,o.discoveredAssets,a)&&i.add(S.resolve(o.outputPath));for(let l of tt(e,u))if(a.has(l)){X(n,e,o.outputPath),o.isText&&i.add(S.resolve(o.outputPath));break}}return [...i]}function Oi(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 Ui(e,t,n){let r=new Set,s=i=>{if(!i)return;let a=Se(e.outputDir,i);a&&r.add(a);};for(let i of t.donePages)s(ge(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(je(e,t.assetMap,i));for(let i of Object.values(n.manifest.assets))i.lastSeenRunId===n.runId&&s(i.outputPath);return r}async function Li(e,t,n,r,s){let i=[...r.deletedFiles].sort(),a=Ui(e,t,n),u=0;for(let c of [...a].sort()){try{if((await T.stat(S.join(e.outputDir,c))).isFile())continue}catch{}u++,s.warn(`Active output is missing after crawl: ${c}`,{phase:"deploy-plan",file:c,outputPath:S.join(e.outputDir,c)});}if(u>0&&s.warn(`Detected ${u} active output file(s) missing before deploy plan save.`,{phase:"deploy-plan",missingActiveOutputs:u}),i.length===0)return [];let o=[],l=0;for(let c of i){if(!a.has(c)){o.push(c);continue}l++;let d=S.join(e.outputDir,c),p;try{p=(await T.stat(d)).isFile();}catch{p=false;}s.warn(`Deploy plan protected active output from deletion: ${c}`,{phase:"deploy-plan",file:c,outputPath:d,exists:p});}return l>0&&s.info(`Deploy plan skipped ${l} active output deletion(s).`,{phase:"deploy-plan",protectedDeletes:l,deletedFiles:o.length}),o}function tt(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 ji(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 Or(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,dn(r.pathname))}catch{return null}}function qi(e,t,n){for(let r of ji(e,n)){let s=t[r];if(!s)continue;let i=Or(e,s);if(i)return {outputPath:i,publicUrl:s}}return null}function _i(e,t,n){for(let r of tt(e,n)){let s=t[r];if(!s)continue;let i=Or(e,s);if(i)return i}return null}function je(e,t,n){let r=_i(e,t,n);if(r)return r;try{return At(e,n).originalFilePath}catch{return null}}function Ni(){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 Pr(e){let t=Ni();if(t)try{let n=await T.readFile(t,"utf8"),r=JSON.parse(n);if(!r||typeof r!="object")return;await T.writeFile(t,JSON.stringify({...r,checkedAt:new Date().toISOString(),status:"running",currentStep:"rewrite",message:e},null,2),"utf8");}catch{}}function Bi(e,t){let n=new Set;for(let r of t.donePages)n.add(S.resolve(ge(e,r)));for(let r of t.doneAssets){let s=je(e,t.assetMap,r);!s||!be(s)||n.add(S.resolve(s));}for(let r of t.doneSitemaps){let s=je(e,t.assetMap,r);!s||!be(s)||n.add(S.resolve(s));}return [...n]}var Rt="re:",bt=new Map;function Hi(e){if(!e.startsWith(Rt))return null;let t=bt.get(e);if(t!==void 0)return t||null;let n=e.slice(Rt.length).trim();if(!n)return bt.set(e,false),null;try{let r=new RegExp(n);return bt.set(e,r),r}catch{return console.warn(`[crawl] Ignoring invalid path matcher regex: ${e}`),bt.set(e,false),null}}function ln(e,t){let n=Hi(t);return n?n.test(e):t.startsWith(Rt)?false:e.startsWith(t)}function Wi(e,t){return t.find(n=>ln(e,n))}function Ur(e,t){let n=Wi(t.pathname,e.blockedPathPrefixes);if(n)return n.startsWith(Rt)?`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 zi(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 Qi(e,t){return zi(e).has(t.hostname)}function Ji(e){let t=de(e.trim()).replace(/\\\//g,"/").replace(/^['"]|['"]$/g,"").trim();return t=t.replace(/[)]+$/g,"").trim(),t=t.replace(/;.*$/g,"").trim(),t}function an(e){let t=de(e).replace(/\\\//g,"/");return /[{}]|\bwindow\.|\blocation\.|\bincludes\(|\?\?null|\+|%7B|%7D|%22|<|>|\s/.test(t)||/[()[\]'"]/.test(t)}function Vi(e){let t=de(e).replace(/\\\//g,"/").trim(),n=S.extname(t).toLowerCase();return kr.has(n)||$r.has(n)?false:/^(?:this|[A-Za-z_$][\w$]*)\.[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/.test(t)}function qe(e){return wi.some(t=>e.pathname.startsWith(t))}function z(e,t,n=e.sourceOrigin,r,s="url"){let i=Ji(t);if(!i||i.startsWith("data:")||i.startsWith("blob:")||i.startsWith("mailto:")||i.startsWith("tel:")||i.startsWith("#"))return r?.ignore(s,t,"empty or unsupported scheme",n),null;if(an(i))return r?.ignore(s,t,"looks like JavaScript/code fragment",n),null;if(Vi(i))return r?.ignore(s,t,"looks like JavaScript property access",n),null;try{let a=new URL(i,n);if(Lr(a))return r?.reject(s,t,"malformed URL encoding",n),null;if(!["http:","https:"].includes(a.protocol))return r?.ignore(s,t,"unsupported protocol: ",n),null;if(!Qi(e,a))return r?.ignore(s,a.toString(),"host not allowed",n),null;let u=Ur(e,a);if(u)return r?.reject(s,a.toString(),u,n),null;let o=new URL(e.sourceOrigin);return a.protocol=o.protocol,a.host=o.host,a.hash="",a}catch{return r?.reject(s,t,"invalid URL",n),null}}function Lr(e){try{return decodeURIComponent(e.pathname),decodeURIComponent(e.search),decodeURIComponent(e.hash),!1}catch{return true}}function ve(e,t,n,r){try{let s=new URL(e);return Lr(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 jr(e){return kr.has(S.extname(e.pathname).toLowerCase())}function Ce(e){return qe(e)?false:$r.has(S.extname(e.pathname).toLowerCase())}function _e(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function B(e,t){return !!(_e(t)||t.pathname==="/robots.txt"||t.pathname==="/llms.txt"||t.pathname.toLowerCase().endsWith(".xsl")||e.assetPathPrefixes.length>0&&e.assetPathPrefixes.some(n=>ln(t.pathname,n))||jr(t)||!Ce(t)&&!qe(t))}function ge(e,t){let n=new URL(t),r=dn(n.pathname);return r.endsWith("/")?r+="index.html":S.extname(r)||(r+="/index.html"),S.join(e.outputDir,r)}function dn(e){try{return decodeURIComponent(e)}catch{return e}}function Ki(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 pn(e,t){let n=Ki(e);if(!n)return false;try{return new URL(t,e.sourceOrigin).toString()===n}catch{return false}}function At(e,t){let n=new URL(t),r=dn(n.pathname);r.endsWith("/")&&(r+="index.html");let s=!!n.search,i=r,a=S.join(e.outputDir,i),u=Zt(e.targetOrigin,i),o=`${u}${n.search}`;if(!s)return {originalPathname:i,originalFilePath:a,originalPublicUrl:u,originalPublicUrlWithSearch:o,preferQueryHashed:s};let l=S.extname(r),d=`${l?r.slice(0,-l.length):r}.${Ze(n.search)}${l||".bin"}`;return {originalPathname:i,originalFilePath:a,originalPublicUrl:u,originalPublicUrlWithSearch:o,preferQueryHashed:s,hashedPathname:d,hashedFilePath:S.join(e.outputDir,d),hashedPublicUrl:Zt(e.targetOrigin,d)}}function Gi(e,t,n,r){let s=ve(t,n,"asset",r);return s?At(e,s.toString()):null}function Zi(e,t,n,r,s){t[n]=r;try{let i=new URL(n),u=[new URL(e.sourceOrigin).origin,...Object.keys(e.extraReplacements||{})];if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{u.push(new URL(e.targetOrigin).origin);}catch{}for(let o of u)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 qr(e){try{return await T.readFile(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}async function Le(e){try{return await T.stat(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}function Yi(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 _r(e){let t=String(e["last-modified"]||"").trim();if(!t)return null;let n=Date.parse(t);return Number.isFinite(n)?n:null}function Xi(e,t){let n=_r(t),r=Yi(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 eo(e,t){if(!t)return;let n=_r(t);if(n===null)return;let r=await Le(e);if(!r)return;let s=new Date(n);await T.utimes(e,r.atime,s).catch(()=>{});}function vr(e,t){let n=S.extname(e.pathname).toLowerCase(),r=t["content-type"]||"";return hi.has(n)||mi.some(s=>r.includes(s))&&!r.startsWith("image/")&&!r.includes("font")}function to(e){try{let t=S.extname(new URL(e).pathname).toLowerCase();return t===".js"||t===".mjs"}catch{return false}}function Ue(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 no(e){return Ze(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 so(e,t,n){let r=e.assetVariantDigestsByOriginalPath.get(t);r||(r=new Set,e.assetVariantDigestsByOriginalPath.set(t,r)),r.add(n);}async function cn(e,t,n,r,s,i,a){let u=At(e,n),o=no(r),l=u.originalFilePath,c=u.originalPublicUrlWithSearch;if(u.preferQueryHashed&&u.hashedFilePath&&u.hashedPublicUrl&&ro(s,u.originalFilePath,o)){let g=qi(e,t,n);g?(l=g.outputPath,c=g.publicUrl):(l=u.hashedFilePath,c=u.hashedPublicUrl);}Zi(e,t,n,c,u.originalPublicUrl);let d=be(l)?Buffer.from(en(r.toString("utf8"),e,t,l),"utf8"):r,p=await qr(l),f=false;return (p===null||!p.equals(d))&&(await Ye(l,d),f=true),i?.debug(`${f?"Saved":"Skipped unchanged"} asset output ${n}`,{phase:"download-assets",url:n,outputPath:l,bytes:d.length,wroteFile:f}),so(s,u.originalFilePath,o),await eo(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:c,wroteFile:f}}function nt(e,t,n,r,s){let i=()=>{let d=String(s??"").trim();if(!d)return {discoveredFrom:"",discoveredFromType:"unknown"};if(d==="seed-path"||d==="cli"||d==="generated-404-request-path")return {discoveredFrom:d,discoveredFromType:d};if(/^https?:\/\//i.test(d))try{let p=new URL(d);return _e(p)?{discoveredFrom:d,discoveredFromType:"sitemap"}:B(e,p)?{discoveredFrom:d,discoveredFromType:"asset"}:Ce(p)&&!qe(p)?{discoveredFrom:d,discoveredFromType:"page"}:{discoveredFrom:d,discoveredFromType:"url"}}catch{return {discoveredFrom:d,discoveredFromType:"url"}}return {discoveredFrom:d,discoveredFromType:"url"}},a=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,u=z(e,n,a,r,"page");if(!u)return;if(!Ce(u)||jr(u)){if(B(e,u)){r.info(`Seeded/non-page URL queued as asset: ${u.toString()}`,{url:u.toString(),source:s}),W(e,t,u.toString(),r,s);return}r.reject("page",u.toString(),qe(u)?"asset/internal path cannot be page":"not page-like",s);return}let o=u.toString(),{discoveredFrom:l,discoveredFromType:c}=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:c,outputPath:ge(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 W(e,t,n,r,s){let i=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,a=z(e,n,i,r,"asset");if(!a)return;if(!B(e,a)){r.reject("asset",a.toString(),"not a safe asset path/prefix",s);return}let u=a.toString();!t.doneAssets.has(u)&&!t.queuedAssets.has(u)&&(t.queuedAssets.add(u),t.assetQueue.push(u),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 It(e,t,n,r,s){let i=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,a=z(e,n,i,r,"sitemap");if(!a)return;if(!_e(a)){r.reject("sitemap",a.toString(),"not sitemap-like",s);return}let u=a.toString();!t.doneSitemaps.has(u)&&!t.queuedSitemaps.has(u)&&(t.queuedSitemaps.add(u),t.sitemapQueue.push(u),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=de(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:et(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 oo(e){return e.split(",").map(t=>t.trim().split(/\s+/)[0]).filter(Boolean)}function ao(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 co(e){return e.replace(/<script\b[^>]*>[\s\S]*?<\/script\s*>/gi,"")}function uo(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 rn(e){return e.trim().replace(/\\/g,"/").replace(/^https?:\/\/[^/]+/i,"").replace(/^\/+/,"")}async function Nr(e,t,n,r){let s=[];await T.mkdir(t,{recursive:true});let i=await T.readdir(e,{withFileTypes:true});for(let a of i){let u=S.join(e,a.name),o=S.join(t,a.name);if(a.isDirectory()){s.push(...await Nr(u,o,n,r));continue}await Br(u,o,n,r)&&s.push(o);}return s}async function lo(e,t){try{let[n,r]=await Promise.all([T.stat(e),T.stat(t)]);if(n.size!==r.size)return !1;let[s,i]=await Promise.all([T.readFile(e),T.readFile(t)]);return s.equals(i)}catch{return false}}async function Br(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 lo(e,t)?(r.debug("Skipped unchanged post-crawl copy target",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):(await T.mkdir(S.dirname(t),{recursive:true}),await T.copyFile(e,t),true)}async function po(e,t,n){let r=Object.entries(e.postCrawlCopyMap||{}).map(([u,o])=>({sourcePath:u.trim(),prefix:String(o||"").trim()}));if(r.length===0)return [];let s=0,i=[],a=new Set([...t.donePages].map(u=>S.resolve(ge(e,u))));n.mark("copy-extra-paths");for(let u of r){let o=u.sourcePath,l=u.prefix;if(!o||!l){n.warn("Skipped post-crawl copy mapping with empty key/value",{sourcePath:o,prefixPath:l});continue}let c=vi(o);if(!c.resolvedPath){n.warn("Skipped post-crawl copy source because alias root is not configured",{sourcePath:o,alias:c.alias||"",requiredEnv:c.requiredEnv||"",prefixPath:l});continue}let d=c.resolvedPath,p=rn(l);if(!p){n.warn("Skipped post-crawl copy mapping with invalid export prefix",{sourcePath:o,prefixPath:l});continue}let f;try{f=await T.stat(d);}catch{n.warn("Skipped post-crawl copy source because it does not exist",{sourcePath:o,sourceAbs:d,prefixPath:l});continue}let g=S.resolve(e.outputDir,p);if(f.isDirectory()){let w=await Nr(d,g,a,n);i.push(...w),s+=w.length,w.length>0&&n.progress(`Copied static directory to export: ${d} -> /${p}`,{phase:"copy-extra-paths",sourcePath:d,targetPath:`/${p}`,copiedItems:s,changedFiles:w.length});continue}let y=S.basename(d),m=l.endsWith("/")?S.join(g,y):g;await Br(d,m,a,n)&&(i.push(m),s++,n.progress(`Copied static file to export: ${d} -> /${rn(l.endsWith("/")?`${l}${y}`:l)}`,{phase:"copy-extra-paths",sourcePath:d,targetPath:`/${rn(l.endsWith("/")?`${l}${y}`:l)}`,copiedItems:s}));}return n.endMark("copy-extra-paths",{mappedSources:r.length,copiedItems:s}),i}function rt(e,t,n,r,s){let i=new Set,a=de(n).replace(/\\\//g,"/"),u=s==="page-html"?co(a):a,o=uo(t,s);if(o){for(let c of ao(u)){let d=z(e,c,t,r,`${s}:css-url`);d&&B(e,d)&&i.add(d.toString());}for(let c of u.matchAll(/@import\s+(?:url\()?\s*['"]?([^'"\s;]+)['"]?\s*\)?/gi)){let d=z(e,c[1],t,r,`${s}:css-import`);d&&B(e,d)&&i.add(d.toString());}}if(o||s==="page-html")for(let c of u.matchAll(/<\?xml-stylesheet[^>]+href=["']([^"']+)["'][^>]*\?>/gi)){let d=z(e,c[1],t,r,`${s}:xml-stylesheet`);d&&B(e,d)&&i.add(d.toString());}let l=new RegExp(`(?:https?:)?//[^\\s'"<>\\);]+\\.${tn}(?:\\?[^\\s'"<>\\);]*)?|(?<!\\.)/[^\\s'"<>\\);]+\\.${tn}(?:\\?[^\\s'"<>\\);]*)?`,"gi");if(s==="page-html")for(let c of a.matchAll(l)){let d=c[0].startsWith("//")?`${new URL(e.sourceOrigin).protocol}${c[0]}`:c[0];if(an(d)){r.ignore(`${s}:serialized-url`,d,"looks like JavaScript/code fragment",t);continue}let p=z(e,d,t,r,`${s}:serialized-url`);p&&B(e,p)?i.add(p.toString()):p&&r.ignore("asset",p.toString(),"serialized URL did not pass safe asset path filter",t);}if(s!=="page-html"&&to(t))for(let c of lr(a)){if(an(c)||!/\.(?:js|mjs)(?:[?#]|$)/i.test(c))continue;let d=z(e,c,t,r,`${s}:relative-asset`);d&&B(e,d)&&i.add(d.toString());}return [...i]}function go(e,t,n,r){let s=new Set,i=de(n);for(let a of i.matchAll(/href=["']([^"'#\s][^"']*?)["']/gi)){let u=a[1],o=z(e,u,t,r,"page-link");o&&(Ur(e,o)||Ce(o)&&!qe(o)&&!B(e,o)&&s.add(o.toString()));}return [...s]}async function Cr(e,t,n,r,s,i,a=false,u=[]){let o=new Set(u),l=0,c=new Map,d=(p,f)=>{let g=[f],y=new Set;for(;g.length>0;){let m=g.pop();if(m===p)return true;y.has(m)||(y.add(m),g.push(...c.get(m)??[]));}return false};for(;n.sitemapQueue.length>0;){let p=n.sitemapQueue.shift();n.queuedSitemaps.delete(p);let f=ve(p,r,"sitemap","sitemap-queue");if(!f)continue;let g=f.toString();if(n.doneSitemaps.has(g))continue;n.doneSitemaps.add(g),r.sitemap(`Fetching sitemap ${g}`,{url:g});let y=true;try{let m=await t.get(g,{timeout:6e4});if(y=!1,!m.ok()){if(r.skip("sitemap",g,m.status()),a&&!o.has(g))throw new Error(`Required child content-sync sitemap returned HTTP ${m.status()}: ${g}`);continue}let h=await m.body(),w=m.url()||g,v=ve(w,r,"sitemap-response",g);if(!v)continue;let I=v.toString();n.successfulSitemaps.add(g),n.successfulSitemaps.add(I),o.has(g)&&(n.resolvedSitemapRoots.add(I),l+=1);let j=await cn(e,n.assetMap,I,h,n,r);X(s,e,j.outputPath),Ie(s,j.outputPath);let U=h.toString("utf8");/<sitemapindex(?:\s|>)/i.test(U)?n.sitemapIndexUrls.add(I):n.sitemapLeafUrls.add(I);let $=rt(e,I,U,r,"sitemap");for(let q of $)W(e,n,q,r,I);let C=[];for(let q of io(U)){let R=z(e,q.loc,I,r,"sitemap-loc");if(!R){if(a)throw new Error(`Content-sync sitemap contains an inadmissible location: ${q.loc}`);continue}if(_e(R)){if(d(I,R.toString()))throw new Error(`Sitemap index cycle detected: ${I} -> ${R.toString()}`);if(c.has(I)||c.set(I,new Set),c.get(I).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()}`);It(e,n,R.toString(),r,I);}else if(a){if(Ce(R)&&!B(e,R)){let M=i.requiredUrls.has(R.toString()),b=i.manifest.pages[R.toString()],A=!!b&&!!await Le(b.outputPath);if(!M&&!A)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",I);}else B(e,R)?W(e,n,R.toString(),r,I):Ce(R)?(q.lastmod&&(n.sitemapLastmodByPage[R.toString()]=q.lastmod),nt(e,n,R.toString(),r,I)):r.reject("sitemap-loc",R.toString(),"not page/sitemap/asset-like",I);}i.trackManifest&&(i.manifest.assets[I]={url:I,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 Pe(e,i));}catch(m){r.error(`Failed sitemap ${g}`,{url:g,error:String(m)});let h=a&&o.has(g)&&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 fo(e,t,n,r,s,i,a){let u=z(e,r,e.sourceOrigin,s,"asset-fetch");if(!u||!B(e,u))return;let o=u.toString();if(n.doneAssets.has(o))return;let l=i.enabled||i.strictTargeted?i.previousManifest.assets[o]:void 0,c=i.trackManifest?await vt(e,t,i,o,s):null,d=c?.token??null,p=dr(l?.changeToken,c),f=i.strictTargeted&&(!c?.supported||!c.token)&&pr(e.sourceOrigin,o),g=i.strictTargeted?i.manifest.assets[o]:void 0;if(g?.lastSeenRunId===i.runId&&await Le(g.outputPath)){n.doneAssets.add(o);for(let m of g.discoveredAssets||[])W(e,n,m,s,o);X(a,e,g.outputPath),g.isText&&Ie(a,g.outputPath),s.info(`Resumed completed content-sync asset ${o}`,{url:o,source:"content-sync-checkpoint"}),Ue(n,s);return}if(l&&(p||f)&&await Le(l.outputPath)){i.manifest.assets[o]={...l,changeToken:d,tokenSource:c?.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||[])W(e,n,m,s,o);s.info(`Reused unchanged trusted asset ${o}.`,{url:o,tokenSource:c?.tokenSource??"unknown",phase:"download-assets",source:p?"asset-change-token-match":"content-sync-release-fingerprint"}),Ue(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||[])W(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"}),Ue(n,s),true};s.asset(`Fetching asset ${o}`,{url:o});try{let m=je(e,n.assetMap,o),h=m?await Le(m):null;if(m&&h)try{let M=await H(`fetch asset headers ${o}`,6e4,()=>t.head(o,{timeout:6e4})),b=M.headers();if(M.ok()&&Xi(h,b)){let A=await qr(m);if(A!==null){let P=M.url()||o,E=ve(P,s,"asset-response",o);if(!E)return;let D=E.toString(),L=await cn(e,n.assetMap,D,A,n,s,b);L.wroteFile&&(X(a,e,L.outputPath),be(L.outputPath)&&Ie(a,L.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"}),Ue(n,s);let Z=vr(u,b),te=l?.discoveredAssets?[...l.discoveredAssets]:[];if(Z||l?.isText){let ce=A.toString("utf8"),F=rt(e,D,ce,s,"asset:cached");(F.length>0||!l)&&(te=F);for(let Ae of te)W(e,n,Ae,s,D);}if(i.trackManifest){let ce=Z||!!l?.isText;i.manifest.assets[o]={url:o,outputPath:L.outputPath,isText:ce,contentType:sn(b)||l?.contentType||null,changeToken:c?.supported?c.token:null,tokenSource:c?.supported?c.tokenSource??null:null,discoveredAssets:te,lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,unreferencedRuns:0,rewriteComplete:!ce},delete i.manifest.pages[o],(i.enabled||i.strictTargeted)&&ce&&await Pe(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 v=await H(`read asset response ${o}`,6e4,()=>w.body()),I=w.url()||o,j=ve(I,s,"asset-response",o);if(!j)return;let U=j.toString(),$=w.headers(),C=await cn(e,n.assetMap,U,v,n,s,$);C.wroteFile&&(X(a,e,C.outputPath),be(C.outputPath)&&Ie(a,C.outputPath)),n.doneAssets.add(o),Ue(n,s);let q=vr(u,$),R=[];if(q){let M=v.toString("utf8");R=rt(e,U,M,s,`asset:${S.extname(u.pathname).toLowerCase()||$["content-type"]||"unknown"}`);for(let b of R)W(e,n,b,s,U);}i.trackManifest&&(i.manifest.assets[o]={url:o,outputPath:C.outputPath,isText:q,contentType:sn($),changeToken:c?.supported?c.token:null,tokenSource:c?.supported?c.tokenSource??null:null,discoveredAssets:R,lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,unreferencedRuns:0,rewriteComplete:!q},delete i.manifest.pages[o],(i.enabled||i.strictTargeted)&&q&&await Pe(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),Ue(n,s));}}async function mo(e,t,n,r,s,i){let a=Math.max(1,Number(e.assetDownloadConcurrency||e.concurrency||1)),u=Array.from({length:a},async()=>{for(;n.assetQueue.length>0;){let o=n.assetQueue.shift();o&&(n.queuedAssets.delete(o),await fo(e,t,n,o,r,s,i));}});await Promise.all(u);}function ho(e){let t=Math.ceil(e/1e3);return `${t} second${t===1?"":"s"}`}function pe(e){return Math.max(6e4,e.navigationTimeoutMs*2,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+15e3)}function Ct(e){return Math.max(15e3,Math.min(6e4,pe(e)))}function wo(e){return Math.max(15e3,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+1e4)}function gn(e){return e.stats.pagesCompleted}function Hr(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 yo(e,t,n){let r=Hr(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 Error(`${e} timed out after ${ho(t)}.`));},t);})])}finally{r!==void 0&&globalThis.clearTimeout(r);}}async function So(e,t){let n=wo(e);await t.evaluate(async r=>{await new Promise(s=>{let i=0,a=700,u=Date.now(),o=()=>{window.clearInterval(l),window.scrollTo(0,0),s();},l=window.setInterval(()=>{if(Date.now()-u>=r.maxDurationMs){o();return}window.scrollBy(0,a),i+=a;let c=document.body?.scrollHeight||0,d=document.documentElement?.scrollHeight||0,p=Math.max(c,d);i>=p+window.innerHeight&&o();},120);});},{maxDurationMs:n});}async function bo(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 So(e,t),await t.waitForTimeout(e.readiness.fallbackWaitMs);}async function Po(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 vo(e,t){return e.noJavaScriptRenderPathPrefixes.some(n=>ln(t,n))}async function Co(e,t,n,r,s=true){let i=new Set;if(s){let u=await t.evaluate(()=>{let o=new Set,l=["href","src","poster","data-src","data-lazy-src","data-original","data-bg","data-background","data-href"],c=new Set,d=["srcset","data-srcset","data-lazy-srcset"],p=f=>{f.querySelectorAll("*").forEach(g=>{for(let m of l){let h=g.getAttribute(m);h&&o.add(h);}for(let m of d){let h=g.getAttribute(m);h&&h.split(",").forEach(w=>{let v=w.trim().split(/\s+/)[0];v&&o.add(v);});}let y=g.getAttribute("style");y&&c.add(y),g.shadowRoot&&p(g.shadowRoot);});};return p(document),{attrs:[...o],styles:[...c]}});for(let o of u.attrs)for(let l of oo(o)){let c=z(e,l,n,r,"dom-attr");c&&B(e,c)&&i.add(c.toString());}for(let o of u.styles)for(let l of rt(e,n,o,r,"dom-style"))i.add(l);}let a=await t.content();for(let u of rt(e,n,a,r,"page-html"))i.add(u);return [...i]}async function Rr(e,t,n,r,s){let i=ge(e,t);await Ye(i,n,"utf8");let a=await T.stat(i);s?.debug(`Saved page output ${t}`,{phase:"save-pages",url:t,outputPath:i,bytes:a.size}),r.stats.pagesSaved++;let u=Math.min(r.stats.pagesQueued,gn(r)+1);s&&(r.stats.pagesSaved%10===0||r.stats.pagesSaved===1)&&s.progress(`Page progress: processed ${u}, rendered ${r.stats.pagesRendered}, saved ${r.stats.pagesSaved}, discovered ${r.stats.pagesQueued}.`,{donePages:u,claimedPages:r.donePages.size,pagesRendered:r.stats.pagesRendered,pagesSaved:r.stats.pagesSaved,pagesQueued:r.stats.pagesQueued,pageQueue:r.pageQueue.length,phase:"save-pages"});}function Ro(e){let t=gn(e);return t%5===0||t===1||t===e.stats.pagesQueued}function Io(e,t){let n=gn(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 xo(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 Ao(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 xo(r),r}async function To(e,t,n,r,s,i,a,u){let o=await chromium.launch({headless:true}),l=Hr(o);l.pid!==null&&n.debug(`Launched crawl worker browser PID ${l.pid}`,{browserPid:l.pid});try{for(;t.pageQueue.length>0&&!(e.maxPages>0&&t.donePages.size>=e.maxPages);){let c=t.pageQueue.shift();if(!c)break;t.queuedPages.delete(c);let d=null,p=null,f=[],g=new Set;try{let y=ve(c,n,"page","page-queue");if(!y)continue;let m=y.toString();if(c=m,t.queuedPages.delete(c),t.donePages.has(c))continue;t.donePages.add(c),i.enabled&&i.seenPages.add(c);let h=pn(e,c),w=vo(e,y.pathname);if(!Ce(y)||B(e,y)||qe(y)){if(B(e,y)){n.info(`Worker redirected non-page URL to asset queue: ${c}`,{url:c,source:"worker-guard"}),W(e,t,c,n,"worker-guard");continue}n.reject("page",c,"guard rejected non-page URL before rendering","worker");continue}let v=i.strictTargeted?i.manifest.pages[c]:void 0;if(v?.lastSeenRunId===i.runId&&await Le(v.outputPath)){i.seenPages.add(c);for(let P of v.discoveredAssets||[])W(e,t,P,n,c);X(a,e,v.outputPath),Ie(a,v.outputPath),n.info(`Resumed completed content-sync page ${c}`,{url:c,source:"content-sync-checkpoint"});continue}let I=await Ei(e,s,t,i,c,n);if(i.enabled&&r!=="single-url"&&r!=="content-sync"){let P=i.manifest.pages[c];if(I.action==="reuse"&&P){for(let E of P.discoveredAssets)W(e,t,E,n,c);for(let E of P.discoveredPages)nt(e,t,E,n,c);P.lastSeenRunId=i.runId,P.sitemapLastmod=et(t.sitemapLastmodByPage[c])??P.sitemapLastmod,I.changeToken?.supported&&(P.changeToken=I.changeToken.token,P.tokenSource=I.changeToken.tokenSource??P.tokenSource),n.info(`Incremental reuse skipped unchanged page ${c}`,{url:c,mode:"incremental",reason:I.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}d=await H(`create page context ${c}`,pe(e),()=>Ao(o,e,w)),p=await H(`create page ${c}`,pe(e),()=>d.newPage()),f=await d.cookies();let j=!1,U=new Set;await p.route("**/*",async P=>{let E=P.request();try{let D=z(e,E.url(),e.sourceOrigin,n,"network-request");D&&B(e,D)&&(U.add(D.toString()),W(e,t,D.toString(),n,c));}catch{}if(E.isNavigationRequest()&&E.frame()===p.mainFrame())try{let D=new URL(E.url()),L=new URL(m);if(D.hash="",L.hash="",D.href===L.href){if(j){n.warn(`Blocked same-page navigation/reload for ${c}`,{url:c,requestUrl:E.url()}),await P.abort("aborted");return}j=!0;}}catch{}await P.continue();}),p.on("response",P=>{try{let D=z(e,P.url(),e.sourceOrigin,n,"network-response");D&&B(e,D)&&(U.add(D.toString()),W(e,t,D.toString(),n,c));}catch{}let E=P.headerValues("set-cookie").then(D=>{for(let L of D)u.add(mr(L,P.url()));}).catch(()=>{}).then(()=>{});g.add(E),E.finally(()=>g.delete(E));}),n.page(`Rendering ${c}`,{url:c});let $=null,C=null;try{if($=await p.goto(c,{waitUntil:"domcontentloaded",timeout:e.navigationTimeoutMs}),$&&$.ok())try{C=await H(`read response body ${c}`,pe(e),()=>$.text());}catch{C=null;}}catch(P){n.warn(`Navigation issue for ${c}; saving current DOM if available`,{url:c,error:String(P)});}if(r==="content-sync"&&$){let P=ve($.url(),n,"content-sync-final-response",c)?.toString();if(P&&P!==m){if(i.deleteOnMissingUrls.has(c)){await un(e,i,a,c,i.deleteOnMissingUrls),n.info(`Removed stale content-sync archive route redirected to ${P}`,{url:c,finalResponseUrl:P,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${c} -> ${P}`)}}if(h){let P=$?.status()??null;if(P!==404){n.error(`Generated 404 request path returned unexpected status for ${c}`,{url:c,expectedStatus:404,status:P,requestPath:e.generated404RequestPath});continue}n.info(`Capturing rendered 404 page for ${c}`,{url:c,status:P,outputPath:ge(e,c)});}else if($&&!$.ok()){if(r==="content-sync"&&($.status()===404||$.status()===410)&&i.deleteOnMissingUrls.has(c)){await un(e,i,a,c,i.deleteOnMissingUrls),n.info(`Removed stale content-sync archive page ${c}`,{url:c,status:$.status(),source:"content-sync-tombstone"}),await p.close();continue}if(r==="content-sync")throw new Error(`Required content-sync route returned HTTP ${$.status()}: ${c}`);n.skip("page",c,$.status(),t.pageDiscoverySources.get(c)),await p.close();continue}w?(n.info(`Rendering without JS execution for ${c}`,{url:c,mode:"no-js"}),await H(`wait for rendered page ${c}`,pe(e),()=>Po(e,p))):await H(`wait for rendered page ${c}`,pe(e),()=>bo(e,p)),t.stats.pagesRendered++;let q=await H(`extract rendered assets ${c}`,pe(e),()=>Co(e,p,m,n,!w));for(let P of q)W(e,t,P,n,c);let R=[...new Set([...q,...U])],M=C??await H(`capture rendered html ${c}`,pe(e),()=>p.content()),b=r!=="single-url"&&r!=="content-sync"&&!h?go(e,c,M,n):[];if(r!=="single-url"&&r!=="content-sync"&&!h)for(let P of b)nt(e,t,P,n,c);await Rr(e,c,M,t,n);let A=ge(e,c);if(X(a,e,A),Ie(a,A),i.trackManifest){let P=$?.headers();i.manifest.pages[c]={url:c,outputPath:A,contentType:sn(P),changeToken:I.changeToken?.supported?I.changeToken.token:null,tokenSource:I.changeToken?.supported?I.changeToken.tokenSource??null:null,sitemapLastmod:et(t.sitemapLastmodByPage[c])??null,discoveredPages:b,discoveredAssets:R,lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,rewriteComplete:!1},(i.enabled||i.strictTargeted)&&await Pe(e,i);}}catch(y){if(i.strictTargeted)throw n.error(`Failed required content-sync page ${c}`,{url:c,error:String(y)}),y;try{let m=p?await H(`capture partial html ${c}`,pe(e),()=>p.content()):"";if(m&&m.trim()){await Rr(e,c,m,t,n);let h=ge(e,c);if(X(a,e,h),Ie(a,h),i.trackManifest){let w=i.manifest.pages[c]??i.previousManifest.pages[c];i.manifest.pages[c]={url:c,outputPath:h,contentType:w?.contentType??null,changeToken:null,tokenSource:null,sitemapLastmod:et(t.sitemapLastmodByPage[c])??w?.sitemapLastmod??null,discoveredPages:w?.discoveredPages?[...w.discoveredPages]:[],discoveredAssets:w?.discoveredAssets?[...w.discoveredAssets]:[],lastCrawledAt:new Date().toISOString(),lastSeenRunId:i.runId,rewriteComplete:!1},(i.enabled||i.strictTargeted)&&await Pe(e,i);}n.warn(`Saved partial DOM for ${c}`,{url:c,error:String(y)});}}catch(m){n.error(`Could not save partial DOM for ${c}`,{url:c,error:String(m)});}n.error(`Failed page ${c}`,{url:c,error:String(y)});}finally{if(g.size>0&&await Promise.allSettled([...g]),d)try{let y=await d.cookies();u.addAll(hr(f,y));}catch{}t.stats.pagesCompleted++,Ro(t)&&Io(t,n),p&&await H(`close page ${c}`,Ct(e),()=>p.close()).catch(y=>{n.warn(`Timed out while closing page ${c}`,{url:c,browserPid:l.pid,error:String(y)});}),d&&await H(`close page context ${c}`,Ct(e),()=>d.close()).catch(y=>{n.warn(`Timed out while closing page context for ${c}`,{url:c,browserPid:l.pid,error:String(y)});});}}}finally{await H("close worker browser",Ct(e),()=>o.close()).catch(c=>(n.warn("Timed out while closing crawl worker browser",{browserPid:l.pid,error:String(c)}),yo(o,n,"browser.close timeout")));}}async function ko(e){try{let t=await T.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 Ir(e){let t=String(e||"").toLowerCase();return t.includes("timeout")||t.includes("timed out")||t.includes("navigation timeout")}function $o(e){let t=String(e?.job?.command||"").trim(),n=String(e?.job?.crawlMode||"full").trim();return t!=="publish"&&t!=="crawl"?false:n!=="incremental"}function Eo(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 Wr(e){try{let t=await T.readFile(e,"utf8"),n=JSON.parse(t);return n&&typeof n=="object"&&!Array.isArray(n)?n:null}catch{return null}}function xr(e){let t=String(e.originalFileName||"").toLowerCase();return t.endsWith("errors.jsonl")?0:t.endsWith("errors.json")?1:2}function Mo(e,t){let n=Array.isArray(t?.artifacts)?t.artifacts.filter(r=>String(r?.role||"").trim()==="errors"&&String(r?.storedFileName||"").trim()!=="").sort((r,s)=>xr(r)-xr(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 Ar(e){let t=await Wr(S.join(e,"job.json"));for(let n of Mo(e,t)){let r=await ko(n);if(r.length>0)return r}return []}async function Do(e){let t=S.join(e.logDir,"archive"),n;try{n=await T.readdir(t);}catch{return ""}let r="",s=0;for(let i of n){let a=S.join(t,i);if(!(await T.stat(a).catch(()=>null))?.isDirectory())continue;let o=await Wr(S.join(a,"job.json"));if(!$o(o))continue;let l=Eo(o);l>=s&&(s=l,r=a);}return r}async function Fo(e){let t=await Do(e),n=t!==""?await Ar(t):await Ar(e.logDir),r=new Set;for(let s of n)if(Ir(s.error)||Ir(s.message)){let i=s.url;typeof i=="string"&&i&&r.add(i);}return [...r]}async function Oo(e){let t=[];for(let n of e)if(n.startsWith("@")){let r=n.slice(1),s=await T.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 Uo(e){if(!e)throw new Error("Content-sync crawl requires an immutable impact plan.");let t=JSON.parse(await T.readFile(e,"utf8"));return Je(t),t}function Lo(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(/^\/+/,""),u=String(r.paginationBase||"page").replace(/^\/+|\/+$/g,"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return n.pathname.startsWith(`${i}/`)&&new RegExp(`^${u}/\\d+/?$`).test(a)})}function Pt(e,t,n){let r=n==="page"?ge(e,t):At(e,t).originalFilePath;return Se(e.outputDir,r)}async function un(e,t,n,r,s){let i=t.manifest.pages[r];if(!i)return;let a=Se(e.outputDir,i.outputPath);if(!a)throw new Error(`Manifest-owned page path escaped output root: ${r}`);let u=S.resolve(i.outputPath);Object.entries(t.manifest.pages).some(([l,c])=>l!==r&&!s.has(l)&&S.resolve(c.outputPath)===u)||(await T.rm(i.outputPath,{force:true}),n.deletedFiles.add(a)),delete t.manifest.pages[r];}function jo(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{_e(new URL(a))&&r.push(a);}catch{}}return n}async function qo(e,t,n,r,s){let i=jo(n.previousManifest,s);for(let a of i){if(t.successfulSitemaps.has(a))continue;let u=n.previousManifest.assets[a];if(!u)continue;let o=Se(e.outputDir,u.outputPath);if(!o)throw new Error(`Manifest-owned sitemap path escaped output root: ${a}`);await T.rm(u.outputPath,{force:true}),r.deletedFiles.add(o),delete n.manifest.assets[a];}await Pe(e,n);}async function _o(e){try{let t=await T.readFile(S.join(e.outputDir,"asset-map.json"),"utf8");return JSON.parse(t)}catch{return {}}}async function No(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 T.mkdir(e,{recursive:true}),await Promise.all(t.map(async n=>{try{await T.unlink(S.join(e,n));}catch(r){if(r.code!=="ENOENT")throw r}}));}function Tr(e,t,n,r,s){let i=z(e,n,e.sourceOrigin,r,"manual-url");i&&(_e(i)?It(e,t,i.toString(),r,s):B(e,i)?W(e,t,i.toString(),r,s):Ce(i)?nt(e,t,i.toString(),r,s):r.reject("manual-url",i.toString(),"not page/sitemap/asset-like",s));}async function zr(e=process.argv.slice(2)){let t=[],n=await se(void 0,{onRemotePublisherConfigDiagnostic:b=>{t.push(b);}}),r=Ke(n.sitemapPaths),s=t.at(-1)??null,i=yi(e),a=i.mode==="content-sync"?await Uo(i.contentSyncPlanPath):null;if(i.resumeRewrite&&i.mode!=="full")throw new Error("--resume-rewrite is only supported for full crawl/publish jobs.");let u=[];i.mode==="retry-timeouts"&&(u=await Fo(n)),i.mode==="single-url"&&(u=await Oo(i.urls)),a&&(u=[...a.directRenderUrls,...a.archiveFamilyUrls]);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 c=(i.mode==="full"||i.mode==="single-url"||i.mode==="content-sync")&&!i.resumeRewrite,d=i.mode!=="full"||i.resumeRewrite||l,p=d?await _o(n):{},f=l||i.mode==="single-url"||i.mode==="content-sync"?await Ii(n):Mr(),g=new Set(u),y=new Set;if(a){for(let b of a.archiveFamilies)if(b.foundPosts===0)for(let A of b.pageUrls)y.add(A);for(let b of Object.keys(f.pages))Lo(b,a)&&!g.has(b)&&(u.push(b),y.add(b));}let m={enabled:l,trackManifest:c,manifest:on(f),previousManifest:on(f),runId:a?.jobId??`${Date.now()}`,seenPages:new Set,changeTokenCache:new Map,manifestSaveChain:Promise.resolve(),strictTargeted:!!a,requiredUrls:g,deleteOnMissingUrls:y};await No(n.logDir);let h=new ht(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&&!l){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:{...p},assetVariantDigestsByOriginalPath:new Map,stats:{pagesQueued:0,pagesRendered:0,pagesCompleted:0,assetsQueued:0,sitemapsQueued:0,assetsSaved:0,pagesSaved:0}},v=Ri(),I=new yt,j=Si();if(j&&!i.resumeRewrite&&await St(j,I.artifact()),await De(n),await Kn(n,{startedAt:new Date().toISOString(),outputDir:S.resolve(n.outputDir)}),d||await T.rm(n.outputDir,{recursive:true,force:true}),await T.mkdir(n.outputDir,{recursive:true}),a){let b=new Set([...a.deleteUrls,...m.deleteOnMissingUrls]);for(let A of a.deleteUrls)await un(n,m,v,A,b);}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(P=>It(n,w,P,h)),await Cr(n,b,w,h,v,m),n.seedPaths.forEach(P=>Tr(n,w,P,h,"seed-path")),n.generated404RequestPath&&nt(n,w,n.generated404RequestPath,h,"generated-404-request-path")):(u.forEach(P=>Tr(n,w,P,h,"cli")),a?.sitemapUrls.forEach(P=>It(n,w,P,h,"content-sync-impact")),await Cr(n,b,w,h,v,m,i.mode==="content-sync",a?.sitemapUrls??[]),a&&await qo(n,w,m,v,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 St(j,I.artifact());return}h.mark("render-pages");let A=Array.from({length:Math.max(1,n.concurrency)},()=>To(n,w,h,i.mode,b,m,v,I));if(await Promise.all(A),j&&await St(j,I.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 Ti(n,b,m,[...w.assetQueue],h),await mo(n,b,w,h,m,v),h.endMark("download-assets",{assets:w.doneAssets.size}),m.enabled&&n.maxPages===0&&await Di(n,w,m,h,v),i.mode==="full"){let P=await po(n,w,h);for(let E of P)X(v,n,E);}}finally{await H("dispose crawl request context",Ct(n),()=>b.dispose()).catch(A=>{h.warn("Timed out while disposing crawl request context",{error:String(A)});});}}m.trackManifest&&await Pe(n,m),await T.writeFile(S.join(n.outputDir,"asset-map.json"),JSON.stringify(w.assetMap,null,2),"utf8"),JSON.stringify(p,null,2)!==JSON.stringify(w.assetMap,null,2)&&X(v,n,S.join(n.outputDir,"asset-map.json"));let U=i.mode==="single-url"||i.mode==="content-sync"?Bi(n,w):m.enabled&&!i.resumeRewrite?Fi(n,m,v,p,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/${U?.length??0}`:"Rewriting text files...";h.progress($,{phase:"rewrite-text",index:0,totalFiles:U?.length,changedTextFiles:0}),await Pr($);let C=Date.now(),q=await gt(n,w.assetMap,{files:U,previousAssetMap:p,onStart:({totalFiles:b,workerCount:A})=>{h.info(`Rewrite phase scanning ${b} text files.`,{phase:"rewrite-text",totalFiles:b,workerCount:A});},onProgress:async({index:b,totalFiles:A,changedTextFiles:P,file:E,changed:D})=>{D&&X(v,n,S.join(n.outputDir,E)),h.checkpoint(`Rewriting text file ${b}/${A}`,{phase:"rewrite-text",index:b,totalFiles:A,changedTextFiles:P,file:E}),h.debug(`${D?"Rewrote":"Checked unchanged"} text file ${b}/${A}: ${E}`,{phase:"rewrite-text",index:b,totalFiles:A,changedTextFiles:P,file:E,changed:D});let L=Date.now();if(!(b===1||b===A||b%50===0||L-C>=5e3))return;C=L;let te=`Rewriting text files: ${b}/${A}`;h.progress(te,{phase:"rewrite-text",index:b,totalFiles:A,changedTextFiles:P,file:E}),await Pr(te);}});m.trackManifest&&(Oi(m,U),await Pe(n,m)),h.endMark("rewrite-text",{changedTextFiles:q});let R=a?[...v.deletedFiles].sort():m.enabled&&!i.resumeRewrite?await Li(n,w,m,v,h):[],M=[...new Set((U||[]).map(b=>Se(n.outputDir,S.isAbsolute(b)?b:S.join(n.outputDir,b))).filter(b=>b!==null))].sort();if(a){let b=[...v.changedFiles].sort(),A=new Set(b),P=new Set(a.directRenderUrls.map(F=>Pt(n,F,"page")).filter(F=>!!F&&A.has(F))),E=new Set(a.archiveFamilyUrls.map(F=>Pt(n,F,"page")).filter(F=>!!F&&A.has(F))),D=new Set([...w.sitemapLeafUrls].map(F=>Pt(n,F,"asset")).filter(F=>!!F&&A.has(F))),L=new Set([...w.sitemapIndexUrls].map(F=>Pt(n,F,"asset")).filter(F=>!!F&&A.has(F))),Z=new Set([...P,...E,...D,...L]),te=b.filter(F=>!Z.has(F)),ce=[...new Set([...a.directRenderUrls,...a.archiveFamilyUrls,...w.resolvedSitemapRoots,...w.sitemapLeafUrls,...w.sitemapIndexUrls,...a.deleteUrls].map(F=>{let Ae=new URL(F);return `${Ae.pathname}${Ae.search}`}))].sort();await Gt(n,{schemaVersion:2,generatedAt:new Date().toISOString(),outputDir:S.resolve(n.outputDir),runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,changedFiles:b,deletedFiles:R,rewriteTargets:M,consumerId:a.consumerId,fromSequence:a.fromSequence,toSequence:a.toSequence,impactHash:a.impactHash,resolvedSitemapUrls:[...w.resolvedSitemapRoots],uploadGroups:{assets:te,content:[...P].sort(),listings:[...E].sort(),sitemapLeaves:[...D].sort(),sitemapIndexes:[...L].reverse()},invalidationPaths:ce});}else await Gt(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?[...v.changedFiles].sort():[],deletedFiles:m.enabled&&!i.resumeRewrite?R:[],rewriteTargets:m.enabled&&!i.resumeRewrite?M:[]});await Zn(n),h.summary(i.resumeRewrite?`Done. Resumed final rewrite over existing output and changed ${q} 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:q}),await h.flush();}function Bo(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 ee(e,t,n){let r=new AbortController,s=globalThis.setTimeout(()=>{r.abort(new Error(`${e} timed out after ${Bo(t)}.`));},t);try{return await n({abortController:r,httpOptions:{abortSignal:r.signal,requestTimeout:t}})}finally{globalThis.clearTimeout(s);}}function Ho(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Tt(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 Wo(e,t){return {...e,...t??{}}}function zo(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function Ne(e,t){let n=String(t??e.deploymentTargetOverride??"").trim();if(!Ho(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:Wo(e.s3,s.s3),cloudFront:zo(e.cloudFront,s.cloudFront)}}}var Kr="wpsuite-sha256",Gr="wpsuite-normalized-sha256",ea=/(?:^|[-_:.])(id|uid|nonce|token|hash|instance)(?:$|[-_:.])/i,Sn=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,ta=["SIGINT","SIGTERM"],Et=new Set,kt=new Map,Qr=null;function na(e){Et.add(e);let t=false;return ()=>{t||(t=true,Et.delete(e));}}async function ra(){let e=[...Et];e.length!==0&&await Promise.allSettled(e.map(async t=>{Et.delete(t),await T.rm(t,{recursive:true,force:true});}));}function sa(){if(!(kt.size>0))for(let e of ta){let t=()=>{Qr||(Qr=ra().finally(()=>{for(let[n,r]of kt)process.off(n,r);kt.clear(),process.exit(1);}));};kt.set(e,t),process.on(e,t);}}sa();function ia(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 Zr(e){return S.normalize(e).split(S.sep).join("/")}function Jr(e,t){let n=S.relative(S.resolve(e),S.resolve(t));return !n||n.startsWith("..")||S.isAbsolute(n)?null:Zr(n)}async function oa(e,t,n){let r=ia(e.outputDir,t),s;try{s=await T.readFile(r,"utf8");}catch(c){return c.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:c instanceof Error?c.message:String(c),source:"deploy-guard"}),null)}let i;try{i=JSON.parse(s);}catch(c){return n.warn("Deploy rewrite guard disabled because the crawl manifest is invalid JSON.",{manifestPath:r,error:c instanceof Error?c.message:String(c),source:"deploy-guard"}),null}let a=new Map,u=new Map,o=0,l=0;for(let[c,d]of Object.entries(i.pages||{})){let p=String(d?.outputPath||"").trim(),f=p?Jr(e.outputDir,p):null;if(f){let g=typeof d?.contentType=="string"&&d.contentType?d.contentType:null;g&&u.set(f,g);}!p||d?.rewriteComplete===true||f&&(o++,a.has(f)||a.set(f,{kind:"page",sourceUrl:c,outputPath:S.resolve(p),relativePath:f}));}for(let[c,d]of Object.entries(i.assets||{})){let p=String(d?.outputPath||"").trim(),f=d?.isText===true,g=p?Jr(e.outputDir,p):null;if(g){let y=typeof d?.contentType=="string"&&d.contentType?d.contentType:null;y&&u.set(g,y);}!p||!f||d?.rewriteComplete===true||g&&(l++,a.has(g)||a.set(g,{kind:"asset",sourceUrl:c,outputPath:S.resolve(p),relativePath:g}));}return {manifestPath:r,byRelativePath:a,contentTypeByRelativePath:u,pendingPages:o,pendingTextAssets:l}}function aa(e){return e&&(Sn.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 mn(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,n)=>n%2===1?t:aa(t)).join("")}function ca(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,n)=>n%2===1?t:Sn.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 Mt(e,t){return Array.isArray(e)?e.map(n=>Mt(n,t)):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([n,r])=>[n,Mt(r,n)])):typeof e!="string"?e:t&&ea.test(t)?ca(e):Sn.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)||/^[0-9]{8,}$/.test(e)?mn(e):e}function ua(e){return e.replace(/((?:https?:\/\/[^"'\s>]+|\/)(?:wp-content|wp-includes)\/[^"'\s>?]+)\?ver=[A-Za-z0-9._-]+(?=$|[#"'])/gi,"$1")}function la(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(Mt(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(Mt(i));return Buffer.from(a,"utf8").toString("base64")}catch{}return t}function da(e,t){let n=e.toLowerCase();return n==="class"?t.split(/(\s+)/).map((r,s)=>s%2===1?r:mn(r)).join(""):n==="id"||n==="for"||n==="aria-controls"||n==="aria-labelledby"||n==="aria-describedby"||n==="aria-owns"||n==="aria-activedescendant"||n==="name"?mn(t):n==="href"||n==="src"?ua(t):la(n,t)}function pa(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,u,o)=>{let l=da(a,o);return ` ${a}=${u}${l}${u}`});return `<${n}${s}>`})}function fn(e){let t=S.extname(e).toLowerCase();return [".html",".htm",".xml",".xsl",".txt"].includes(t)||["robots.txt","sitemap.xml"].includes(S.basename(e))}function $t(e,t){return [e.replace(/^\/+|\/+$/g,""),t.replace(/^\/+/,"")].filter(Boolean).join("/")}async function ga(e,t,n,r){let s=new Map,i,a=0;do{a++;let u=await ee(`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 u.Contents||[]){if(!o.Key)continue;let l=(o.ETag||"").replace(/^"|"$/g,"").toLowerCase(),c=Number(o.Size??0);s.set(o.Key,{etag:l,size:c});}i=u.NextContinuationToken,r.debug(`Listed S3 keys page ${a}: ${s.size} keys so far`);}while(i);return s}async function fa(e,t,n,r){if(r.has(n))return r.get(n)??null;try{let s=await ee(`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 ma(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 u=await ee(`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(u.Errors&&u.Errors.length>0){let o=u.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 ha(e){return createHash("md5").update(e).digest("hex")}function Vr(e){return createHash("sha256").update(e).digest("hex")}function hn(e){return createHash("sha256").update(pa(e.toString("utf8"))).digest("hex")}async function wa(e,t,n,r){if(r.has(n))return r.get(n)??null;try{let a=((await ee(`S3 head object metadata ${n}`,3e5,({httpOptions:u})=>e.send(new HeadObjectCommand({Bucket:t,Key:n}),u))).Metadata?.[Kr]||"").trim().toLowerCase()||null;return r.set(n,a),a}catch{return r.set(n,null),null}}async function ya(e,t,n,r,s){if(r.has(n))return r.get(n)??null;try{let a=((await ee(`S3 head object normalized metadata ${n}`,3e5,({httpOptions:u})=>e.send(new HeadObjectCommand({Bucket:t,Key:n}),u))).Metadata?.[Gr]||"").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 ee(`S3 get object ${n}`,3e5,({httpOptions:o})=>e.send(new GetObjectCommand({Bucket:t,Key:n}),o))).Body?.transformToByteArray(),u=a?hn(Buffer.from(a)):null;return s.set(n,u),r.set(n,u),u}catch{return s.set(n,null),r.set(n,null),null}}function Sa(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 Pa(e,t,n,r){let s=S.join(e,"asset-map.json"),i;try{i=await T.readFile(s,"utf8");}catch(o){if(o.code==="ENOENT")return;throw o}let a=JSON.parse(i);if(!a||typeof a!="object")return;let u=Object.fromEntries(Object.entries(a).map(([o,l])=>{let c=String(l??"");return t.urlRewriteMode==="absolute"&&t.targetOrigin&&n.targetOrigin&&t.targetOrigin!==n.targetOrigin&&(c=c.split(t.targetOrigin).join(n.targetOrigin)),c=ba(c,r),[o,c]}));await T.writeFile(s,JSON.stringify(u,null,2),"utf8");}async function va(e,t,n,r,s,i){let a=e.targetOrigin!==t.targetOrigin,u=Object.keys(r).length>0;if(!n||!a&&!u)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-${Sa(n)}-${Date.now()}`),l=na(o);s.info(`Preparing deployment profile "${n}" from existing crawl output to ${o}...`);try{if(await T.rm(o,{recursive:!0,force:!0}),i){await T.mkdir(o,{recursive:!0});for(let f of i){let g=S.join(e.outputDir,f),y=S.join(o,f);await T.mkdir(S.dirname(y),{recursive:!0}),await T.copyFile(g,y);}}else await T.cp(e.outputDir,o,{recursive:!0});let c={...e,sourceOrigin:e.targetOrigin,targetOrigin:t.targetOrigin,extraReplacements:r,outputDir:o},d=1;s.info(`Rewriting text files for deployment profile "${n}"...`);let p=await gt(c,{},{files:i,onStart:({totalFiles:f,workerCount:g})=>{d=g,s.info(`Rewriting ${f} text file(s) for deployment profile "${n}" with ${g} worker(s).`);},onProgress:({index:f,totalFiles:g,changedTextFiles:y,file:m,changed:h})=>{let v=`Rewriting [${f}/${g}] ${m} (${h?"rewrote":"checked"}; ${y} changed so far)`;g<=200||f<=5||f%10===0||f===g?(s.progress(v,{file:m,index:f,totalFiles:g,changed:h,changedTextFiles:y,workerCount:d}),s.info(v)):s.debug(v);}});return await Pa(o,e,t,r),s.info(`Prepared deployment profile "${n}" in ${o} (${p} rewritten text file(s)).`),{outputDir:o,cleanup:async()=>{l(),await T.rm(o,{recursive:!0,force:!0});}}}catch(c){throw s.error(`Failed to prepare deployment profile "${n}" temporary output.`,{tempDir:o,outputDir:e.outputDir,error:c instanceof Error?c.message:String(c),...c instanceof Error&&c.stack?{stack:c.stack}:{}}),await T.rm(o,{recursive:true,force:true}).catch(()=>{}),l(),await s.flush().catch(()=>{}),c}}async function Yr(e=process.argv.slice(2)){let t=await se(),n=Tt(e),r=Ne(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"):"",u=new Oe(s,"deploy.log.jsonl",r.config.logLevel??"info"),o=r.profile?.extraReplacements??{},l=async()=>{};try{let c=await Me(t);if(e.includes("--content-sync")&&c?.schemaVersion!==2)throw new Error("Content-sync deployment requires a valid schema-2 targeted deploy plan.");let p=await Gn(t);if(p)throw new Error(`Deployment blocked: the crawl started at ${p.startedAt} did not complete its final rewrite. Run a successful crawl or resume the rewrite before deploying.`);let f=await va(t,r.config,r.name,o,u,c?.schemaVersion===2?c.changedFiles:void 0);l=f.cleanup;let g={...r.config,outputDir:f.outputDir},y=g.s3SyncMode??"sdk-upload-delete";if(c?.schemaVersion===2&&c.deletedFiles.length>0&&y!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let m=c?.schemaVersion!==2&&f.outputDir!==t.outputDir&&(t.targetOrigin!==r.config.targetOrigin||Object.keys(o).length>0),h=!!c&&!c.fullSyncRequired&&!m;if(c?.schemaVersion===2&&!h)throw new Error("Content-sync deployment cannot widen into a full-output deployment.");r.name&&u.info(`Using deployment profile: ${r.name}`),u.info(`Starting deploy \u2014 s3://${g.s3.bucket}/${g.s3.prefix||""} (region: ${g.s3.region})`),u.info(`S3 sync mode: ${y}`),c&&c.fullSyncRequired?u.info("Deploy plan requests a full sync; scanning the whole output tree."):c&&m?u.info("Ignoring incremental deploy plan because deployment-profile rewrites require a full sync."):h&&u.info(`Using incremental deploy plan: ${c.changedFiles.length} changed file(s), ${c.deletedFiles.length} deleted file(s).`),u.mark("deploy");let w=new S3Client({region:g.s3.region}),v=f.outputDir===t.outputDir?await oa(t,i,u):null,I=c?.schemaVersion===2?[...c.uploadGroups.assets,...c.uploadGroups.content,...c.uploadGroups.listings,...c.uploadGroups.sitemapLeaves,...c.uploadGroups.sitemapIndexes]:c?.changedFiles??[];if(c?.schemaVersion===2){let k=[...new Set(I)],J=[...new Set(c.changedFiles)];if(k.length!==J.length||J.some(x=>!k.includes(x)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let j=h?(await Promise.all(I.map(async k=>{try{return (await T.stat(S.join(g.outputDir,k))).isFile()?k:null}catch{if(c?.schemaVersion===2)throw new Error(`Content-sync planned upload is missing: ${k}`);return null}}))).filter(k=>k!==null):await qs(["**/*"],{cwd:g.outputDir,onlyFiles:!0,dot:!0}),U=[],$=[],C=j.filter(k=>{let J=v?.byRelativePath.get(Zr(k));if(!J)return !0;let x=$t(g.s3.prefix,k);return U.push(k),$.push(x),u.reject(J.kind,J.sourceUrl||k,"rewrite not completed; deploy upload blocked","deploy-guard",{file:k,key:x,outputPath:J.outputPath,manifestPath:v?.manifestPath}),!1}),q=new Set((h?C:j).map(k=>$t(g.s3.prefix,k)));v&&(v.pendingPages>0||v.pendingTextAssets>0)&&u.info(`Deploy rewrite guard loaded ${v.pendingPages} pending page(s) and ${v.pendingTextAssets} pending text asset(s) from crawl manifest.`,{manifestPath:v.manifestPath,pendingPages:v.pendingPages,pendingTextAssets:v.pendingTextAssets,source:"deploy-guard"}),U.length>0&&u.warn(`Deploy rewrite guard blocked ${U.length} file(s) from upload because final crawl rewrite is incomplete.`,{rejected:U.length,manifestPath:v?.manifestPath,source:"deploy-guard"}),h?u.info(`Using ${C.length} eligible planned changed file(s) from deploy plan for upload${U.length>0?` (${U.length} blocked by deploy rewrite guard)`:""}.`):u.info(`Found ${j.length} file(s) in output directory "${g.outputDir}"${U.length>0?`; ${C.length} remain eligible for upload after deploy rewrite guard filtering`:""}.`),u.progress("Deploy preflight: comparing local output with remote S3 state.",{phase:"deploy",mode:y,totalFiles:C.length,scannedFiles:j.length,rejected:U.length,deploymentProfile:r.name||"",s3Bucket:g.s3.bucket,s3Prefix:g.s3.prefix||"",targetedPlan:h,deployGuardEnabled:f.outputDir===t.outputDir});let R=0,M=0,b=0,A=[],P=[],E=[],D=[],L=[],Z=new Map,te=new Map,ce=new Map,F=new Map,Ae=new Map;h||(u.info("Listing existing S3 objects to detect unchanged files..."),Z=await ga(w,g.s3.bucket,g.s3.prefix,u),u.info(`Found ${Z.size} existing key(s) in S3.`));let os=async k=>Z.has(k)?Z.get(k)??null:h?fa(w,g.s3.bucket,k,te):null;for(let k of C){let J=S.join(g.outputDir,k),x=$t(g.s3.prefix,k),ne=v?.contentTypeByRelativePath.get(k),He=ne&&ne!=="application/octet-stream"?ne:Xo.lookup(k)||ne||"application/octet-stream",as=fn(k)?g.s3.htmlCacheControl:g.s3.assetCacheControl,N=R+M+b+1;C.length<=200||N<=5||N%10===0||N===C.length?(u.progress(`Uploading [${N}/${C.length}] ${x} (${He})`,{file:k,key:x,index:N,totalFiles:C.length,contentType:He,mode:y}),u.info(`Uploading [${N}/${C.length}] ${x} (${He})`)):u.debug(`Uploading [${N}/${C.length}] ${x} (${He})`);try{let re=await T.readFile(J),We=await os(x),ze=null,Te=null;if(We){let Ot=re.byteLength;if(We.size===Ot){let it=We.etag.includes("-");if(We.etag!==""&&!it){let cs=ha(re);if(We.etag===cs){M++,C.length<=200||M<=5||M%25===0?(u.progress(`Skipped unchanged [${N}/${C.length}] ${x}`,{file:k,key:x,index:N,totalFiles:C.length,uploaded:R,skipped:M,failed:b,mode:y,reason:"etag-md5-match"}),u.info(`Skipped unchanged [${N}/${C.length}] ${x}`)):u.debug(`Skipped unchanged [${N}/${C.length}] ${x}`),P.push(x);continue}}ze=Vr(re);let In=await wa(w,g.s3.bucket,x,ce);if(In&&In===ze){M++,C.length<=200||M<=5||M%25===0?(u.progress(`Skipped unchanged [${N}/${C.length}] ${x}`,{file:k,key:x,index:N,totalFiles:C.length,uploaded:R,skipped:M,failed:b,mode:y,reason:"metadata-sha256-match"}),u.info(`Skipped unchanged [${N}/${C.length}] ${x}`)):u.debug(`Skipped unchanged [${N}/${C.length}] ${x}`),P.push(x);continue}}if(fn(k)){Te=hn(re);let it=await ya(w,g.s3.bucket,x,F,Ae);if(it&&it===Te){M++,C.length<=200||M<=5||M%25===0?(u.progress(`Skipped semantically unchanged [${N}/${C.length}] ${x}`,{file:k,key:x,index:N,totalFiles:C.length,uploaded:R,skipped:M,failed:b,mode:y,reason:"normalized-html-match"}),u.info(`Skipped semantically unchanged [${N}/${C.length}] ${x}`)):u.debug(`Skipped semantically unchanged [${N}/${C.length}] ${x}`),P.push(x);continue}}}ze=ze??Vr(re),Te=Te??(fn(k)?hn(re):null),await ee(`S3 upload ${x}`,9e5,({abortController:Ot})=>new Upload({client:w,abortController:Ot,params:{Bucket:g.s3.bucket,Key:x,Body:re,ContentType:He,CacheControl:as,Metadata:{[Kr]:ze,...Te?{[Gr]:Te}:{}}}}).done()),R++,A.push(x),(R%25===0||R===C.length)&&(u.progress(`Uploaded ${R}/${C.length} file(s)\u2026`,{uploaded:R,skipped:M,failed:b,totalFiles:C.length,mode:y}),u.info(`Uploaded ${R}/${C.length} file(s)\u2026`));}catch(re){b++,E.push(x),u.error(`Failed to upload ${x}: ${re.message}`,{key:x,error:String(re)});}}if(u.info(`Upload complete: ${R} uploaded, ${M} skipped unchanged, ${$.length} rejected by guard, ${b} failed.`),E.length>0||$.length>0)throw new Error(`Deployment stopped before deletion because ${E.length} upload(s) failed and ${$.length} upload(s) were rejected.`);if(y==="sdk-upload-delete"){let k=new Set((c?.deletedFiles??[]).map(x=>$t(g.s3.prefix,x))),J=[...k].filter(x=>!q.has(x));if(!h&&(L=[...Z.keys()].filter(ne=>!q.has(ne)).filter(ne=>!k.has(ne)),L.length>0)){u.warn(`Protected ${L.length} remote-only S3 object(s) from deletion because the crawl did not provide explicit tombstones.`,{protected:L.length,source:"deploy-delete-guard"});for(let ne of L)u.debug(` protected remote-only object: ${ne}`);}D=await ma(w,g.s3.bucket,h?J:J.filter(x=>Z.has(x)),u);}else u.info("Sync mode is sdk-upload-only \u2014 skipping stale object deletion.");a&&(await T.mkdir(S.dirname(a),{recursive:!0}),await T.writeFile(a,JSON.stringify({generatedAt:new Date().toISOString(),mode:y,summary:{totalFiles:j.length,eligibleFiles:C.length,uploaded:A.length,skipped:P.length,rejected:$.length,failed:E.length,deleted:D.length,protected:L.length,...r.name?{profile:r.name}:{}},uploadedKeys:A,skippedKeys:P,rejectedKeys:$,failedKeys:E,deletedKeys:D,protectedRemoteOnlyKeys:L},null,2),"utf8")),c?.schemaVersion!==2&&await De(t),u.endMark("deploy"),await u.flush();}finally{await l().catch(()=>{});}}function Ra(e){let t=e;return t.endsWith("/")?t+="index.html":S.posix.basename(t).includes(".")||(t+="/index.html"),t}function Ia(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 Xr(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=Ra(s.pathname);n.add(i),n.add(`${a}${s.search}`);let u=Ia(a,t);u&&n.add(`${u}${s.search}`);}return [...n].sort()}function ka(){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 bn(e,t){if(!e)return;await T.mkdir(S.dirname(e),{recursive:true});let n=`${e}.${process.pid}.${Date.now()}.tmp`;await T.writeFile(n,JSON.stringify({...t,updatedAt:new Date().toISOString()},null,2),"utf8"),await T.rename(n,e);}async function $a(e,t,n,r){if(!e)return null;try{let s=JSON.parse(await T.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,u)=>a!==r[u])?null:s}catch(s){if(s.code==="ENOENT")return null;throw s}}function Ea(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 ts(e=process.argv.slice(2)){let t=await se(),n=Tt(e),r=Ne(t,n),s=r.config,i=new Oe(s.logDir||"logs","invalidate.log.jsonl",s.logLevel??"info"),a=e.includes("--content-sync"),u=await Me(t);if(a&&u?.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=u?.schemaVersion===2?u.invalidationPaths:s.cloudFront.invalidationPaths.length>0?s.cloudFront.invalidationPaths:["/*"],l=u?.schemaVersion===2?Xr(o,s.s3.prefix||""):o;if(u?.schemaVersion===2&&l.some(g=>g==="/*"))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 c=s.cloudFront.distributionId,d=new CloudFrontClient({region:"us-east-1",maxAttempts:5});if(i.mark("invalidate"),u?.schemaVersion!==2){let g=`smartcloud-static-publisher-${Date.now()}`;await ee(`CloudFront invalidation for distribution ${c}`,3e5,({httpOptions:y})=>d.send(new CreateInvalidationCommand({DistributionId:c,InvalidationBatch:{CallerReference:g,Paths:{Quantity:l.length,Items:l}}}),y)),i.endMark("invalidate"),await i.flush();return}let p=ka(),f=await $a(p,c,u.impactHash,l);if(!f){let g=createHash("sha256").update(JSON.stringify(l)).digest("hex");f={schemaVersion:1,distributionId:c,impactHash:u.impactHash,updatedAt:new Date().toISOString(),batches:Ea(l).map((y,m)=>({index:m,callerReference:`wpsuite-content-sync-${u.impactHash.slice(0,24)}-${g.slice(0,24)}-${m}`,paths:y,invalidationId:null,status:"pending"}))},await bn(p,f);}for(let g of f.batches){if(!g.invalidationId){let y=await ee(`CloudFront targeted invalidation batch ${g.index+1}`,3e5,({httpOptions:h})=>d.send(new CreateInvalidationCommand({DistributionId:c,InvalidationBatch:{CallerReference:g.callerReference,Paths:{Quantity:g.paths.length,Items:g.paths}}}),h)),m=String(y.Invalidation?.Id||"").trim();if(!m)throw new Error("CloudFront did not return an invalidation ID.");g.invalidationId=m,g.status="created",await bn(p,f);}await waitUntilInvalidationCompleted({client:d,maxWaitTime:900,minDelay:2,maxDelay:15},{DistributionId:c,Id:g.invalidationId}),g.status="completed",await bn(p,f),i.info(`CloudFront invalidation batch ${g.index+1}/${f.batches.length} completed.`,{invalidationId:g.invalidationId,pathCount:g.paths.length});}i.endMark("invalidate"),await i.flush();}function Ft(e){let t=new URL(e);return t.hash="",t.href}function ns(e,t,n){let r=new Set([...t].map(o=>Ft(o))),s=new Map,i=new Map,a=o=>{s.set(Ft(o),o);},u=o=>{if(!o)return;let l=Ft(o);s.has(l)||i.set(l,o);};for(let o of e.deleteUrls)a(o);for(let o of e.archiveFamilies)if(o.foundPosts===0)for(let l of o.pageUrls)r.has(Ft(l))||a(l);u(e.directRenderUrls[0]);for(let o of e.archiveFamilies)u(o.pageUrls[0]),u(o.pageUrls.at(-1));for(let o of n)u(o);return {expectedAvailable:[...i.values()],expectedMissing:[...s.values()]}}var ss=["claimed","impact-planned","rendered","deployed","invalidated","verified","acknowledged"];function Cn(){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 Be(e,t){return ss.indexOf(e)>=ss.indexOf(t)}async function me(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 T.mkdir(e,{recursive:true}),await T.appendFile(S.join(e,"audit-events.jsonl"),`${JSON.stringify(i)}
14
14
  `,"utf8");}function Da(e){return Qe({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 Fa(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 Rn(e,t){if(e.subscriptionType!=="PROFESSIONAL"&&e.subscriptionType!=="AGENCY")throw new Error("Content-sync requires an active Professional or Agency subscription.");let n=Da(t),r=Ne(e,t.deploymentProfile),s=r.name||"",i=Tn(e.sourceOrigin,String(t.id||""),n),a=kn({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??{}}),u=$n({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||"")}:${ue({scopeFingerprint:i,targetFingerprint:a}).slice(0,32)}`,l=new at({sourceOrigin:e.sourceOrigin,runtimeToken:String(e.wpsuite?.runtimeToken||""),ignoreHttpsErrors:e.ignoreHttpsErrors}),c=await l.releaseFingerprint(n.includeSubsites),d=An({wordpressFingerprint:c,targetFingerprint:a,sourceOrigin:e.sourceOrigin,sitemapPaths:Ke(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:u,coalesceKey:o,client:l,releaseFingerprint:d}}async function Oa(e){let t=Cn(),n=ct(t),r=await se(),s=(r.scheduler?.rules??[]).filter(l=>l.command==="content-sync"&&l.enabled!==false);if(s.length===0)return 0;let i=await _t(n),a=await ut(n),u=await qt(n),o=0;for(let l of s){let c=await Rn(r,l),d=u.entries[c.coalesceKey];if(!d||d.releaseJobId!==e||d.scopeFingerprint!==c.scopeFingerprint||d.targetFingerprint!==c.targetFingerprint||d.releaseFingerprint!==c.releaseFingerprint)throw new Error(`The normal release has no matching pre-crawl content-sync cutoff for rule ${String(l.id||"")}.`);let p=ue({releaseJobId:e,releaseFingerprint:c.releaseFingerprint,scopeFingerprint:c.scopeFingerprint,targetFingerprint:c.targetFingerprint,sequence:d.sequence}),f=await c.client.establishBaseline({consumerId:c.consumerId,scopeFingerprint:c.scopeFingerprint,baselineId:p,postTypes:c.scope.postTypes,includeSubsites:c.scope.includeSubsites,sequence:d.sequence}),g={schemaVersion:1,ruleId:String(l.id||""),consumerId:c.consumerId,coalesceKey:c.coalesceKey,scopeFingerprint:c.scopeFingerprint,targetFingerprint:c.targetFingerprint,releaseFingerprint:c.releaseFingerprint,baselineId:p,deploymentProfile:c.deploymentProfile,committedSequence:f.committedSequence,releaseJobId:e,verifiedAt:new Date().toISOString()};i.entries[c.coalesceKey]=g;let y=a.rules[c.coalesceKey];a.rules[c.coalesceKey]={ruleId:String(l.id||""),consumerId:c.consumerId,coalesceKey:c.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 _n(n,c.coalesceKey);await me(t,"content-sync-baseline-established",{id:e},"A normal release established a verified content-sync baseline.",{ruleId:l.id,consumerId:c.consumerId,committedSequence:f.committedSequence,supersededCurrent:m}),o++;}return await On(n,i),await lt(n,a),await T.rm(n.releaseCutoff,{force:true}),o}async function Ua(e){let t=Cn(),n=ct(t),r=await se(),s=(r.scheduler?.rules??[]).filter(u=>u.command==="content-sync"&&u.enabled!==false);if(s.length===0)return 0;let i=await qt(n),a=0;for(let u of s){let o=await Rn(r,u),l=Object.values(i.entries).find(p=>p.releaseJobId===e&&p.ruleId===String(u.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(u.id||"")}. Start a new normal publish instead of resuming this release.`)}let c=await o.client.unboundHead(o.scope.postTypes,o.scope.includeSubsites),d={schemaVersion:1,releaseJobId:e,ruleId:String(u.id||""),coalesceKey:o.coalesceKey,scopeFingerprint:o.scopeFingerprint,targetFingerprint:o.targetFingerprint,releaseFingerprint:o.releaseFingerprint,sequence:c.headSequence,capturedAt:new Date().toISOString()};i.entries[o.coalesceKey]=d,await me(t,"content-sync-release-cutoff-captured",{id:e},"A normal release captured its pre-crawl content-sync journal cutoff.",{ruleId:u.id,consumerId:o.consumerId,sequence:c.headSequence}),a++;}return await Fn(n,i),a}async function La(e,t){let n=JSON.parse(await T.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 vn(e,t,n=0){let r=e.protocol==="https:"?Dn:ms,s=e.protocol==="https:"?new Dn.Agent({rejectUnauthorized:!t}):void 0;return new Promise((i,a)=>{let u=r.get(e,{agent:s,timeout:3e4,headers:{"user-agent":"WP-Suite-Content-Sync/1"}},o=>{let l=o.statusCode??500,c=o.headers.location;if(l>=300&&l<400&&c&&n<5){o.resume(),vn(new URL(c,e),t,n+1).then(i,a);return}let d=[],p=0;o.on("data",f=>{p+=f.byteLength,p<=1048576&&d.push(f);}),o.on("error",a),o.on("end",()=>i({status:l,body:Buffer.concat(d).toString("utf8")}));});u.on("timeout",()=>u.destroy(new Error(`Verification timed out: ${e}`))),u.on("error",a);})}async function ja(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 T.readFile(r,"utf8")),a=new Set;for(let[l,c]of Object.entries(i.pages??{}))a.add(l),typeof c.url=="string"&&a.add(c.url);let u=await Me(e);if(u?.schemaVersion!==2||u.impactHash!==n.impactHash)throw new Error("Content-sync verification requires the matching targeted deploy plan.");let o=ns(n,a,u.resolvedSitemapUrls);for(let l of o.expectedAvailable){let c=new URL(l),d=new URL(`${c.pathname}${c.search}`,s),p=await vn(d,e.ignoreHttpsErrors);if(p.status<200||p.status>=400)throw new Error(`Content-sync verification failed (${p.status}): ${d}`)}for(let l of o.expectedMissing.slice(0,10)){let c=new URL(l),d=new URL(`${c.pathname}${c.search}`,s),p=await vn(d,e.ignoreHttpsErrors);if(p.status>=200&&p.status<300)throw new Error(`Deleted content is still publicly available: ${d}`)}}async function qa(e){let t=await T.readFile(e.candidateManifest,"utf8");JSON.parse(t);let n=`${e.trustedManifest}.${process.pid}.tmp`;await T.writeFile(n,t,"utf8"),await T.rename(n,e.trustedManifest);}async function _a(e,t){try{let n=JSON.parse(await T.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 T.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 xe(e,t,n,r,s={},i=0,a=async()=>{}){t.phase=n,await Un(e,t),await qn(e,{schemaVersion:1,jobId:t.jobId,phase:n,completedItems:r,totalItems:r,attempt:i,updatedAt:new Date().toISOString(),details:s}),await me(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 Na(e,t={}){let n={loadConfig:se,resolveRuleContext:Rn,runCrawl:zr,runDeploy:Yr,runInvalidate:ts,verifyDeployment:ja,promoteCandidateManifest:qa,clearDeployPlan:De,afterCheckpoint:async()=>{},...t},r=Cn(),s=ct(r),i=await La(r,e),a=await n.loadConfig(),u=Fa(a,String(i.ruleId)),o=await n.resolveRuleContext(a,u);if(i.coalesceKey&&i.coalesceKey!==o.coalesceKey)throw new Error("Queued content-sync demand no longer matches the active rule scope.");let c=(await _t(s)).entries[o.coalesceKey];if(!c||c.scopeFingerprint!==o.scopeFingerprint||c.targetFingerprint!==o.targetFingerprint||c.releaseFingerprint!==o.releaseFingerprint)throw await me(r,"content-sync-baseline-rejected",i,"Content-sync requires a new verified full or incremental release baseline.",{ruleId:u.id,reason:c?"mismatch":"missing"}),new Error("Content-sync baseline is missing or stale. Run a successful normal publish first.");let d=await Nt(s),p=await o.client.head({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:c.baselineId,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites});if(d&&d.jobId!==i.id)throw new Error("Another immutable content-sync range is still active.");if(d||(d={schemaVersion:1,jobId:i.id,ruleId:String(u.id||""),consumerId:o.consumerId,coalesceKey:o.coalesceKey,scopeFingerprint:o.scopeFingerprint,targetFingerprint:o.targetFingerprint,baselineId:c.baselineId,fromSequence:p.committedSequence,toSequence:p.headSequence,phase:"claimed",impactHash:null,startedAt:new Date().toISOString(),updatedAt:new Date().toISOString()},await xe(s,d,"claimed",0,{fromSequence:d.fromSequence,toSequence:d.toSequence},0,n.afterCheckpoint),await me(r,"content-sync-range-claimed",i,"Content-sync claimed a closed journal range.",{fromSequence:d.fromSequence,toSequence:d.toSequence})),d.fromSequence===d.toSequence){let h=await ut(s);h.rules[o.coalesceKey]={ruleId:String(u.id||""),consumerId:o.consumerId,coalesceKey:o.coalesceKey,committedSequence:d.toSequence,observedHeadSequence:p.headSequence,lag:Math.max(0,p.headSequence-d.toSequence),lastSuccessfulJobId:i.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:false,retryAttempt:0,nextRetryAt:null,lastError:null},await lt(s,h),await Bt(s);return}let f=await Ln(s);if(!Be(d.phase,"impact-planned")){let h=await o.client.events({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:c.baselineId,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites,afterSequence:d.fromSequence,throughSequence:d.toSequence}),w=jt({events:h,scope:o.scope,sourceOrigin:a.sourceOrigin}),v=await o.client.resolveImpactFamilies(w,o.scope.includeSubsites);f=Mn({jobId:i.id,ruleId:String(u.id||""),consumerId:o.consumerId,coalesceKey:o.coalesceKey,scopeFingerprint:o.scopeFingerprint,targetFingerprint:o.targetFingerprint,baselineId:c.baselineId,sourceOrigin:a.sourceOrigin,scope:o.scope,fromSequence:d.fromSequence,toSequence:d.toSequence,events:h,resolvedArchiveFamilies:v,sitemapUrls:Ke(a.sitemapPaths)}),await jn(s,f),d.impactHash=f.impactHash,await xe(s,d,"impact-planned",h.length,{eventCount:h.length,foldedCount:f.foldedCount,impactHash:f.impactHash},0,n.afterCheckpoint),await me(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||d.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,!Be(d.phase,"rendered")){let h=await _a(s,i.id);await me(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 xe(s,d,"rendered",f.directRenderUrls.length+f.archiveFamilyUrls.length,{impactHash:f.impactHash},i.attempt??0,n.afterCheckpoint);}let g=o.deploymentProfile?["--profile",o.deploymentProfile]:[];if(Be(d.phase,"deployed")||(await n.runDeploy([...g,"--content-sync"]),await xe(s,d,"deployed",1,{impactHash:f.impactHash},0,n.afterCheckpoint),await me(r,"content-sync-deployed",i,"Content-sync targeted deployment completed.",{impactHash:f.impactHash})),Be(d.phase,"invalidated")?Be(d.phase,"verified")||await n.runInvalidate([...g,"--content-sync"]):(await n.runInvalidate([...g,"--content-sync"]),await xe(s,d,"invalidated",1,{impactHash:f.impactHash},0,n.afterCheckpoint)),Be(d.phase,"verified")||(await n.verifyDeployment(a,o,f,s.candidateManifest),await n.promoteCandidateManifest(s),await xe(s,d,"verified",1,{impactHash:f.impactHash},0,n.afterCheckpoint)),p=await o.client.head({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:c.baselineId,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites}),p.committedSequence===d.fromSequence)await o.client.acknowledge({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:c.baselineId,expectedSequence:d.fromSequence,sequence:d.toSequence,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites});else if(p.committedSequence!==d.toSequence)throw new Error("Content-sync acknowledgement cursor is in an unexpected state.");await xe(s,d,"acknowledged",1,{sequence:d.toSequence},0,n.afterCheckpoint),await me(r,"content-sync-cursor-acknowledged",i,"Content-sync committed its journal cursor after verification.",{sequence:d.toSequence});let y=await o.client.head({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:c.baselineId,postTypes:o.scope.postTypes,includeSubsites:o.scope.includeSubsites}),m=await ut(s);m.rules[o.coalesceKey]={ruleId:String(u.id||""),consumerId:o.consumerId,coalesceKey:o.coalesceKey,committedSequence:d.toSequence,observedHeadSequence:y.headSequence,lag:Math.max(0,y.headSequence-d.toSequence),lastSuccessfulJobId:i.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:y.headSequence>d.toSequence,retryAttempt:0,nextRetryAt:null,lastError:null},await lt(s,m),await n.clearDeployPlan(a),await T.rm(S.join(r,"content-sync-invalidation.json"),{force:true}),await T.rm(s.candidateManifest,{force:true}),await Bt(s);}async function is(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 Ua(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 Oa(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 Na(i);}is().catch(e=>{console.error(e instanceof Error?e.message:String(e)),process.exit(1);});
@@ -1,3 +1,3 @@
1
- import {pathToFileURL}from'url';import P from'fs/promises';import {existsSync,createReadStream,createWriteStream}from'fs';import m from'path';import {spawn}from'child_process';import {randomUUID,createHash}from'crypto';import Ke from'http';import ye from'https';import {pipeline}from'stream/promises';import {createGzip}from'zlib';function pe(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function _(e){return Array.isArray(e)?e.map(_):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>[t,_(n)])):e}function Ne(e){return JSON.stringify(_(e))}function L(e){return createHash("sha256").update(Ne(e)).digest("hex")}function me(e){return L({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 Qe(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 $(e){let t=pe((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=pe((e.listingPaths??[]).map(r=>{let i=String(r).trim();if(!i)return "";let a=new URL(i,"https://content-sync.invalid/");if(a.origin!=="https://content-sync.invalid"&&/^https?:\/\//i.test(i))throw new Error("Content-sync listing routes must be same-site paths.");let s=`/${a.pathname.replace(/^\/+|\/+$/g,"")}`;return s==="/"?"/":`${s}/`}));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 z(e,t,n){return L({schemaVersion:1,sourceOrigin:Qe(e),ruleId:t.trim(),scope:$(n)})}function H(e){return L({schemaVersion:1,...e})}function fe(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]")}var he=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";he?m.join(he,"current-progress.json"):"";Promise.resolve();var _e="wp-json/smartcloud-static-publisher/v1/content-sync/",ze=25*1024*1024,E=class extends Error{status;code;constructor(t,n,r){super(r),this.name="ContentSyncApiError",this.status=t,this.code=n;}},N=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){let n=this.options.sourceOrigin.replace(/\/+$/,"")+"/";return new URL(`${_e}${t}`,n)}async post(t,n){let r=this.endpoint(t),i=Buffer.from(JSON.stringify(n),"utf8"),a=r.protocol==="https:"?ye:Ke,s=r.protocol==="https:"?new ye.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((o,c)=>{let u=a.request(r,{method:"POST",agent:s,headers:{"content-type":"application/json","content-length":String(i.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},d=>{let g=[],p=0;d.on("data",h=>{if(p+=h.byteLength,p>ze){d.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(h);}),d.on("error",c),d.on("end",()=>{let h=Buffer.concat(g).toString("utf8"),y;try{y=h?JSON.parse(h):{};}catch{c(new E(d.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let l=d.statusCode??500;if(l<200||l>=300){c(new E(l,String(y.code??"request-failed"),String(y.message??`Content-sync API request failed (${l}).`)));return}o(y);});});u.on("timeout",()=>u.destroy(new Error(`Content-sync API request timed out: ${t}`))),u.on("error",c),u.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,i=new Set(t.postTypes);for(;r<t.throughSequence;){let a=await this.post("events",{...t,afterSequence:r,limit:250});if(a.fromSequence!==r||a.throughSequence!==t.throughSequence||a.nextSequence<r||a.nextSequence>t.throughSequence)throw new Error("Content-sync API returned an inconsistent event page.");if(!Array.isArray(a.items))throw new Error("Content-sync API returned a malformed event page.");let s=r;for(let o of a.items){if(!Number.isSafeInteger(o.sequence)||o.sequence<=s||o.sequence>t.throughSequence||!Number.isSafeInteger(o.postId)||o.postId<=0||!Number.isSafeInteger(o.blogId)||o.blogId<=0||!i.has(o.postType)||typeof o.operation!="string"||!o.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");s=o.sequence;}if(a.items.length>0&&a.nextSequence!==s)throw new Error("Content-sync API event cursor does not match the final returned event.");if(n.push(...a.items),!a.hasMore)break;if(a.nextSequence===r)throw new Error("Content-sync API event pagination did not advance.");r=a.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)}};function He(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Ve(e,t){return {...e,...t??{}}}function Ge(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function V(e,t){let n=String(t??e.deploymentTargetOverride??"").trim();if(!He(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 i=e.deploymentProfiles?.[r];if(!i)throw new Error(`Unknown deployment profile "${r}". Check the linked WPSuite publisher configuration.`);return {name:r,profile:i,config:{...e,targetOrigin:i.targetOrigin??e.targetOrigin,s3:Ve(e.s3,i.s3),cloudFront:Ge(e.cloudFront,i.cloudFront)}}}var G=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function Se(e){let t=new Set;for(let n of [...e,...G]){let r=String(n||"").trim();r&&t.add(r);}return [...t]}function we(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function et(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("/"),i=n.replace(/^\/+/,"").split("/").map(s=>s.replace(/[^A-Za-z0-9._-]/g,"")).filter(s=>s.length>0&&s!=="."&&s!=="..");if(i.length===0)return "";let a=`/${i.join("/")}`;return r&&m.extname(a)===""&&(a+="/"),a==="/"?"":a}function Y(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t.trim(),String(n??"")]).filter(([t])=>t.length>0))}function tt(e){if(!e||typeof e!="object")return {};let t={};for(let[n,r]of Object.entries(e)){let i=n.trim();if(!i||!r||typeof r!="object")continue;let a=r,s={};if(typeof a.targetOrigin=="string"){let c=we(a.targetOrigin);c&&(s.targetOrigin=c);}let o=Y(a.extraReplacements);if(Object.keys(o).length>0&&(s.extraReplacements=o),a.s3&&typeof a.s3=="object"){let c={},u=a.s3;typeof u.bucket=="string"&&(c.bucket=u.bucket.trim()),typeof u.prefix=="string"&&(c.prefix=u.prefix.trim()),typeof u.region=="string"&&(c.region=u.region.trim()),typeof u.htmlCacheControl=="string"&&(c.htmlCacheControl=u.htmlCacheControl.trim()),typeof u.assetCacheControl=="string"&&(c.assetCacheControl=u.assetCacheControl.trim()),Object.keys(c).length>0&&(s.s3=c);}if(a.cloudFront&&typeof a.cloudFront=="object"){let c={},u=a.cloudFront;typeof u.distributionId=="string"&&(c.distributionId=u.distributionId.trim()),Array.isArray(u.invalidationPaths)&&(c.invalidationPaths=u.invalidationPaths.map(d=>String(d??"").trim()).filter(d=>d.length>0)),Object.keys(c).length>0&&(s.cloudFront=c);}t[i]=s;}return t}function X(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function nt(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 i=Number.parseInt(String(n.intervalMinutes??"0"),10);if(!Number.isFinite(i)||i<1)return null;let a=String(n.id??`${r}-${t+1}`).trim();if(!a)return null;let s=String(n.deploymentProfile??"").trim(),o=String(n.url??"").trim(),c=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"&&c.length===0?null:{id:a,enabled:n.enabled!==false,command:r,intervalMinutes:i,...r==="publish"||r==="crawl"?{crawlMode:n.crawlMode==="incremental"?"incremental":"full"}:{},...(r==="publish"||r==="deploy"||r==="invalidate"||r==="content-sync")&&s?{deploymentProfile:s}:{},...o?{url:o}:{},...c.length>0?{postTypes:c}:{},...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 ve(e){let t=e&&typeof e=="object"?e:{},n=Array.isArray(t.rules)?t.rules.map((r,i)=>nt(r,i)).filter(r=>!!r):[];return {enabled:t.enabled===true,timezone:typeof t.timezone=="string"&&t.timezone.trim()!==""?t.timezone.trim():"UTC",rules:n}}function rt(e){if(!e||typeof e!="object")return;let t=e,n={};for(let i of ["accountId","siteId"]){let a=t[i];typeof a=="string"&&a.trim()!==""&&(n[i]=a.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 ee(e){let t=e&&typeof e=="object"?e:{},n=String(t.accountId??"").trim(),r=String(t.siteId??"").trim(),i=t.subscriber===true,a=rt(t.siteSettings),s={...a??{},...n?{accountId:a?.accountId??n}:{},...r?{siteId:a?.siteId??r}:{},...a?.subscriber===true||i?{subscriber:true}:{}},o={},c=String(t.apiBase??"").trim();c&&(o.apiBase=c);let u=String(t.runtimeToken??t.nonce??"").trim();u&&(o.runtimeToken=u);let d=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();d&&(o.virtualAssetBaseUrl=d),Object.keys(s).length>0&&(o.siteSettings=s);let g=X(t.subscriptionType);return g&&(o.subscriptionType=g),o}function it(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function Pe(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function st(e,t){let n=new Headers(e instanceof Request?e.headers:void 0);if(t?.headers)for(let[r,i]of new Headers(t.headers).entries())n.set(r,i);return n}async function Ce(e,t,n=5){let r=new URL(Pe(e)),i=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),a=r.protocol==="http:"?Ke:ye,s=st(e,t);return await new Promise((o,c)=>{let u=a.request(r,{method:i,headers:Object.fromEntries(s.entries()),...r.protocol==="https:"?{rejectUnauthorized:false}:{}},d=>{let g=[];d.on("data",p=>{g.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),d.on("error",c),d.on("end",()=>{let p=d.statusCode??0,h=d.headers.location;if(n>0&&h&&[301,302,303,307,308].includes(p)){o(Ce(new URL(h,r),{method:p===303?"GET":i},n-1));return}let y=new Headers;for(let[l,S]of Object.entries(d.headers))Array.isArray(S)?y.set(l,S.join(", ")):typeof S=="string"&&y.set(l,S);o(new Response(Buffer.concat(g),{status:p,statusText:d.statusMessage??"",headers:y}));});});u.on("error",c),u.end();})}function ot(e){let t=ee(e.wpsuite),n={...t.siteSettings??{},...t.siteSettings?.subscriber===true||t.subscriptionType==="PROFESSIONAL"||t.subscriptionType==="AGENCY"?{subscriber:true}:{}},r=String(n.accountId??"").trim(),i=String(n.siteId??"").trim(),a=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();if(e.wpsuite={...t.apiBase?{apiBase:t.apiBase}:{},...t.runtimeToken?{runtimeToken:t.runtimeToken}:{},...a?{virtualAssetBaseUrl:a}:{},...Object.keys(n).length>0?{siteSettings:n}:{},...t.subscriptionType?{subscriptionType:t.subscriptionType}:{}},!r||!i||!a)return false;it(e.sourceOrigin);let s=globalThis,c={...s.WpSuite??{},siteSettings:n,uploadUrl:a};return t.apiBase&&(c.apiBase=t.apiBase),s.WpSuite=c,true}async function at(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!ot(e)){let i=e.wpsuite?.siteSettings,a=[String(i?.accountId??"").trim()?"":"accountId",String(i?.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): ${a.join(", ")}.`,requests:[]}}}let n=globalThis.fetch,r=[];try{typeof n=="function"&&/^https?:\/\//i.test(t)&&(globalThis.fetch=(async(c,u)=>{let d=Pe(c);if(d.startsWith(t)){let g=(()=>{try{return new URL(d).pathname}catch{return d}})();try{let p=e.ignoreHttpsErrors&&d.startsWith("https://")?await Ce(c,u):await n(c,u);return r.push({path:g,status:p.status,ok:p.ok,error:null}),p}catch(p){throw r.push({path:g,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return n(c,u)}));let{getConfig:i}=await import('@smart-cloud/wpsuite-core'),a=await i("publisher"),s=a&&typeof a=="object"?a:null,o=X(s?.subscriptionType);return {remote:s,diagnostic:{status:s?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:o??null,error:s?null:"WP Suite publisher config loader returned no configuration.",requests:r}}}catch(i){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:t,subscriptionType:null,error:i instanceof Error?i.message:String(i),requests:r}}}finally{typeof n=="function"&&(globalThis.fetch=n);}}function ct(e,t){let n=tt(t?.deploymentProfiles),r=String(t?.defaultDeploymentProfile??"").trim(),i=n[r]?r:"",a=X(t?.subscriptionType),s=ee(e.wpsuite);return {...e,scheduler:ve(t?.scheduler),deploymentProfiles:n,defaultDeploymentProfile:i,...a?{subscriptionType:a}:{},wpsuite:{...s,...s.siteSettings||a?{siteSettings:{...s.siteSettings??{},...s.siteSettings?.subscriber===true||a==="PROFESSIONAL"||a==="AGENCY"?{subscriber:true}:{}}}:{},...a?{subscriptionType:a}:{}}}}function Z(e,t){let r=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!r)return t;let i=r.split("/").map(a=>a.trim()).filter(a=>a.length>0&&a!=="."&&a!=="..");return i.length>0?i.join("/"):t}function ut(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?m.resolve(e):""}function be(e,t,n){let r=String(t||"").trim();return r&&m.isAbsolute(r)?m.resolve(r):m.resolve(e,Z(r,n))}async function te(e,t={}){let n=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",r=await P.readFile(n,"utf8"),i=JSON.parse(r);i.sourceOrigin=i.sourceOrigin.replace(/\/$/,""),i.targetOrigin=we(i.targetOrigin),i.ignoreHttpsErrors??=false,i.outputDir=String(i.outputDir||"export").trim()||"export",i.urlRewriteMode||=i.targetOrigin==="."?"relative":"absolute",i.noJavaScriptRenderPathPrefixes||=[],i.seedPaths||=[],i.generated404RequestPath=et(i.generated404RequestPath),i.sitemapPaths||=[...G],i.allowedAssetHosts||=[],i.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],i.blockedPathPrefixes||=[],i.blockedSearchFragments||=[],i.concurrency||=1,i.maxPages||=0,i.extraReplacements=Y(i.extraReplacements),i.postCrawlCopyMap=Y(i.postCrawlCopyMap),i.logDir=String(i.logDir||"logs").trim()||"logs",i.verbose??=false,i.logLevel||=i.verbose?"debug":"info",i.s3SyncMode||="sdk-upload-delete",i.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},i.readiness.timeoutMs??=1500,i.readiness.fallbackWaitMs??=1500,i.viewport||={width:1440,height:1200},i.navigationTimeoutMs||=3e4,i.assetDownloadConcurrency=Number(i.assetDownloadConcurrency)>0?Number(i.assetDownloadConcurrency):i.concurrency,i.rewriteConcurrency=Number(i.rewriteConcurrency)>0?Number(i.rewriteConcurrency):i.assetDownloadConcurrency,i.wpsuite=ee(i.wpsuite),i.scheduler=ve(void 0),i.deploymentProfiles={},i.defaultDeploymentProfile="",i.deploymentTargetOverride=String(i.deploymentTargetOverride??"").trim();let a=await at(i);t.onRemotePublisherConfigDiagnostic?.(a.diagnostic);let s=ct(i,a.remote),o=ut(),c=o?m.resolve(o,".."):"";return c?(s.outputDir=be(c,s.outputDir,"export"),s.logDir=be(c,s.logDir,"logs")):(m.isAbsolute(s.outputDir)||(s.outputDir=Z(s.outputDir,"export")),m.isAbsolute(s.logDir)||(s.logDir=Z(s.logDir,"logs"))),s}function Ae(e){let t=m.resolve(e);return {state:m.join(t,"content-sync-state.json"),current:m.join(t,"content-sync-current.json"),impact:m.join(t,"content-sync-impact-plan.json"),checkpoint:m.join(t,"content-sync-checkpoint.json"),baseline:m.join(t,"content-sync-baseline.json"),releaseCutoff:m.join(t,"content-sync-release-cutoff.json"),candidateManifest:m.join(t,"content-sync-candidate-manifest.json"),trustedManifest:m.join(t,"crawl-manifest.json"),invalidation:m.join(t,"content-sync-invalidation.json"),deployPlan:m.join(t,"deploy-plan.json")}}async function lt(e,t){await P.mkdir(m.dirname(e),{recursive:true});let n=`${e}.${process.pid}.${Date.now()}.tmp`;await P.writeFile(n,JSON.stringify(t,null,2),"utf8"),await P.rename(n,e);}async function Ie(e){try{return JSON.parse(await P.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 dt(e){let t=await Ie(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 Re(e){let t=await Ie(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 qe(e,t){await lt(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function xe(e,t){let n=await dt(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 P.unlink(r);}catch(i){if(i.code!=="ENOENT")throw i}})),true)}function Fe(e){let t=String(e.command??"").trim();return (t==="publish"||t==="crawl")&&e.crawlMode==="incremental"||t==="content-sync"}var ne=new Map;async function De(e){let t=m.resolve(e),n=ne.get(t);if(n!==void 0)return n;try{let r=JSON.parse(await P.readFile(t,"utf8")),i=String(r.version??"").trim();return ne.set(t,i),i}catch{return ne.set(t,""),""}}var U=class extends Error{request;step;constructor(t,n){super(`Stop requested during ${n}.`),this.name="StopRequestedError",this.request=t,this.step=n;}},yt="queue-mutation.lock",St=5e3,bt=3e4;function wt(e){let t=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",n=process.env.STATIC_PUBLISHER_EXPORTER_DIR||process.cwd(),r=process.env.PUBLISHER_CONFIG||"",i=1;for(let o=0;o<e.length;o++){let c=e[o];c==="--runtime-dir"?t=e[++o]||"":c.startsWith("--runtime-dir=")?t=c.slice(14):c==="--exporter-dir"?n=e[++o]||n:c.startsWith("--exporter-dir=")?n=c.slice(15):c==="--config"?r=e[++o]||"":c.startsWith("--config=")?r=c.slice(9):c==="--max-jobs"?i=Number.parseInt(e[++o]||"1",10):c.startsWith("--max-jobs=")&&(i=Number.parseInt(c.slice(11),10));}if(!t)throw new Error("Missing runtime dir. Use --runtime-dir or STATIC_PUBLISHER_RUNTIME_DIR.");let a=m.resolve(t),s=r?m.resolve(r):m.join(a,"config.json");return {runtimeDir:a,exporterDir:m.resolve(n),configPath:s,maxJobs:Number.isFinite(i)&&i>0?i:1}}async function v(e,t){try{let n=await P.readFile(e,"utf8");return JSON.parse(n)}catch(n){if((n&&typeof n=="object"&&"code"in n?String(n.code||""):"")!=="ENOENT"){let i=n instanceof Error?n.message:String(n);console.warn(`[queue-runner] failed to read JSON ${e}: ${i}`);}return t}}async function w(e,t){await P.mkdir(m.dirname(e),{recursive:true}),await P.writeFile(e,JSON.stringify(t,null,2),"utf8");}function je(e){return m.join(e,yt)}async function vt(e){await new Promise(t=>setTimeout(t,e));}async function Pt(e){let t=je(e),n=Date.now()+St;for(;;)try{await P.mkdir(m.dirname(t),{recursive:!0}),await P.writeFile(t,JSON.stringify({pid:process.pid,createdAt:new Date().toISOString()},null,2),{encoding:"utf8",flag:"wx"});return}catch(r){if((r&&typeof r=="object"&&"code"in r?String(r.code||""):"")!=="EEXIST")throw r;let a=await P.stat(t).catch(()=>null);if(a&&Date.now()-a.mtimeMs>bt){await P.unlink(t).catch(()=>{});continue}if(Date.now()>=n)throw new Error("Timed out acquiring queue mutation lock.",{cause:r});await vt(50);}}async function Ct(e){await P.unlink(je(e)).catch(()=>{});}async function j(e,t){await Pt(e);try{return await t()}finally{await Ct(e);}}function Je(e){return m.join(e,"queue-runner-heartbeat.json")}function le(e){return m.join(e,"current-progress.json")}function re(e){return m.join(e,"scheduler-state.json")}function At(e,t){let r=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!r)return t;let i=r.split("/").map(a=>a.trim()).filter(a=>a.length>0&&a!=="."&&a!=="..");return i.length>0?i.join("/"):t}async function Oe(e){let t=await v(e.configPath||"",null),n=m.resolve(e.runtimeDir,".."),i=(typeof t?.logDir=="string"?t.logDir:"").trim();return i&&m.isAbsolute(i)?m.resolve(i):m.resolve(n,At(i,"logs"))}function ie(e,t){return String(e||"").trim().replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/^-+|-+$/g,"")||t}function It(e){let t=e?new Date(e):new Date;return (Number.isNaN(t.getTime())?new Date:t).toISOString().replace(/[-:]/g,"").replace(/\.\d{3}Z$/,"Z")}function Rt(e){return [It(e.endedAt||e.startedAt),ie(e.command||"job","job"),ie(e.id||"job","job"),ie(e.status||"finished","finished")].join("-")}function qt(e){return e.endsWith(".log.jsonl")||e.endsWith(".errors.jsonl")||e.endsWith(".rejected.jsonl")?true:["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"].includes(e)}function xt(e){return e==="errors.json"||e==="errors.jsonl"||e.endsWith(".errors.jsonl")?"errors":e==="ignored.json"||e==="ignored.jsonl"?"ignored":e==="rejected.json"||e==="rejected.jsonl"||e.endsWith(".rejected.jsonl")?"rejected":e==="skipped-http.json"||e==="skipped-http.jsonl"?"skipped-http":e==="timings.json"||e==="timings.jsonl"?"timings":e==="current-progress.json"?"current-progress":e==="current-crawl-event.json"?"current-crawl-event":e.endsWith(".log.jsonl")?e.replace(/\.log\.jsonl$/i,"-log"):e.replace(/\.[^.]+$/u,"")||"artifact"}function Dt(e){return e.endsWith(".gz")?"application/gzip":e.endsWith(".jsonl")?"application/x-ndjson":e.endsWith(".json")?"application/json":"text/plain"}async function se(e,t,n){let r=`${n}.gz`,i=m.join(t,r);await pipeline(createReadStream(e),createGzip({level:9}),createWriteStream(i));let[a,s]=await Promise.all([P.stat(e),P.stat(i)]);return {role:xt(n),originalFileName:n,storedFileName:r,compressed:true,compression:"gzip",contentType:Dt(r),originalSize:a.size,storedSize:s.size}}async function Q(e,t){let n=await Oe(t),r=new Date().toISOString(),i=Rt(e),a=m.join(n,"archive",i),s=[],o=[];await P.mkdir(a,{recursive:true});try{let d=await P.readdir(n,{withFileTypes:!0});for(let g of d){if(!g.isFile()||!qt(g.name))continue;let p=await se(m.join(n,g.name),a,g.name);o.push(p),s.push(p.storedFileName);}}catch(d){if((d&&typeof d=="object"&&"code"in d?String(d.code||""):"")!=="ENOENT")throw d}let c=le(t.runtimeDir);if(existsSync(c)){let d=await se(c,a,"current-progress.json");o.push(d),s.push(d.storedFileName);}if(e.command==="content-sync")for(let d of ["content-sync-current.json","content-sync-impact-plan.json","content-sync-checkpoint.json","content-sync-state.json","content-sync-invalidation.json","deploy-plan.json"]){let g=m.join(t.runtimeDir,d);if(!existsSync(g))continue;let p=await se(g,a,d);o.push(p),s.push(p.storedFileName);}let u=[...s,"job.json"];return await w(m.join(a,"job.json"),{manifestVersion:1,archivedAt:r,archiveKey:i,archiveDir:a,logDir:n,runtimeDir:t.runtimeDir,exporterDir:t.exporterDir,configPath:t.configPath||"",archivedFiles:u,artifacts:o,job:e}),{archiveKey:i,archiveDir:a,archiveCreatedAt:r,archivedFiles:u,artifacts:o}}function kt(e){return m.join(e,"audit-events.jsonl")}function ce(e){return m.join(e,"stop-request.json")}async function Le(e){try{let t=await P.readFile(ce(e),"utf8"),n=JSON.parse(t);return n&&typeof n=="object"?n:null}catch{return null}}async function ke(e,t){let n=await Le(e);if(!n)return null;let r=String(n.targetJobId||"").trim();return r&&r!==t?null:n}async function M(e,t){if(!t){await P.unlink(ce(e)).catch(()=>{});return}let n=await Le(e);if(!n)return;let r=String(n.targetJobId||"").trim();(!r||r===t)&&await P.unlink(ce(e)).catch(()=>{});}async function I(e,t){try{let n={occurredAt:t.occurredAt||new Date().toISOString(),status:t.status||"info",actorSource:t.actorSource||"queue-runner",...t};await P.mkdir(e,{recursive:!0}),await P.appendFile(kt(e),`${JSON.stringify(n)}
2
- `,"utf8");}catch{}}async function C(e,t,n){let[r,i]=await Promise.all([De(m.join(e.exporterDir,"package.json")),De(m.join(e.exporterDir,"node_modules","@smart-cloud","wpsuite-core","package.json"))]),a={checkedAt:new Date().toISOString(),status:t,pid:process.pid,nodePath:process.execPath,nodeVersion:process.version,exporterVersion:r,wpsuiteCoreVersion:i,runtimeDir:e.runtimeDir,exporterDir:e.exporterDir,...n??{}};try{await w(Je(e.runtimeDir),a);}catch{}}async function F(e){try{await P.unlink(le(e));}catch{}}function Tt(e,t){let n=e.command;if(n!=="publish"&&n!=="crawl"&&n!=="deploy"&&n!=="invalidate"&&n!=="retry-timeouts"&&n!=="url"&&n!=="content-sync")return null;let r=Number.parseInt(String(e.intervalMinutes??"0"),10);if(!Number.isFinite(r)||r<1)return null;let i=(e.id||`${n}-${t+1}`).trim();if(!i)return null;let a=e.enabled!==false,s=typeof e.url=="string"?e.url.trim():"",o=typeof e.deploymentProfile=="string"?e.deploymentProfile.trim():"",c=Array.isArray(e.postTypes)?[...new Set(e.postTypes.map(g=>String(g).trim()).filter(Boolean))]:[],u=Array.isArray(e.listingPaths)?[...new Set(e.listingPaths.map(g=>String(g).trim()).filter(Boolean))]:[],d=(n==="publish"||n==="crawl")&&e.crawlMode==="incremental"?"incremental":"full";return n==="url"&&!s||n==="content-sync"&&c.length===0?null:{id:i,enabled:a,command:n,intervalMinutes:r,...n==="publish"||n==="crawl"?{crawlMode:d}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&o?{deploymentProfile:o}:{},...s?{url:s}:{},...c.length>0?{postTypes:c}:{},...u.length>0?{listingPaths:u}:{},...typeof e.includeSubsites=="boolean"?{includeSubsites:e.includeSubsites}:{},...typeof e.includePostTypeArchives=="boolean"?{includePostTypeArchives:e.includePostTypeArchives}:{},...typeof e.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:e.includeTaxonomyArchives}:{},...typeof e.includeAuthorArchives=="boolean"?{includeAuthorArchives:e.includeAuthorArchives}:{},...typeof e.includeDateArchives=="boolean"?{includeDateArchives:e.includeDateArchives}:{},...typeof e.includePostsPage=="boolean"?{includePostsPage:e.includePostsPage}:{},...typeof e.includeSitemapChain=="boolean"?{includeSitemapChain:e.includeSitemapChain}:{}}}async function Et(e){let t=await te(e.configPath).catch(()=>null),n=t?.scheduler,r=Array.isArray(n?.rules)?n.rules.map((i,a)=>Tt(i,a)).filter(i=>!!i):[];return {enabled:!!n?.enabled,timezone:typeof n?.timezone=="string"&&n.timezone.trim()?n.timezone.trim():"UTC",rules:r,wpsuite:t?.wpsuite,publisherConfig:t}}function oe(e,t){if(!t)throw new Error("Content-sync scheduler requires a valid publisher config.");let n=$({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}),r=z(t.sourceOrigin,e.id,n),i=V(t,e.deploymentProfile),a=H({sourceOrigin:t.sourceOrigin,targetOrigin:i.config.targetOrigin,deploymentProfile:i.name||"",s3Bucket:i.config.s3.bucket,s3Prefix:i.config.s3.prefix,cloudFrontDistributionId:i.config.cloudFront.distributionId,urlRewriteMode:i.config.urlRewriteMode,extraReplacements:i.profile?.extraReplacements??{}});return `content-sync:${e.id}:${L({scopeFingerprint:r,targetFingerprint:a}).slice(0,32)}`}async function Ft(e,t,n,r,i){let s=(await v(m.join(e.runtimeDir,"content-sync-baseline.json"),{})).entries?.[r];if(!s?.consumerId||!s.scopeFingerprint||!s.baselineId)return {status:"baseline-required",reason:"No verified normal-release baseline exists for this rule scope."};try{let o=t.publisherConfig;if(!o)throw new Error("Publisher configuration is unavailable.");let c=$({postTypes:n.postTypes??[],listingPaths:n.listingPaths??[],includeSubsites:n.includeSubsites,includePostTypeArchives:n.includePostTypeArchives,includeTaxonomyArchives:n.includeTaxonomyArchives,includeAuthorArchives:n.includeAuthorArchives,includeDateArchives:n.includeDateArchives,includePostsPage:n.includePostsPage,includeSitemapChain:n.includeSitemapChain}),u=z(o.sourceOrigin,n.id,c),d=V(o,n.deploymentProfile),g=H({sourceOrigin:o.sourceOrigin,targetOrigin:d.config.targetOrigin,deploymentProfile:d.name||"",s3Bucket:d.config.s3.bucket,s3Prefix:d.config.s3.prefix,cloudFrontDistributionId:d.config.cloudFront.distributionId,urlRewriteMode:d.config.urlRewriteMode,extraReplacements:d.profile?.extraReplacements??{}});if(s.scopeFingerprint!==u||s.targetFingerprint!==g||!s.releaseFingerprint)return {status:"baseline-required",reason:"The verified content-sync baseline does not match the active scope or deployment target. Run a successful full or incremental publish to establish a new baseline."};let p=new N({sourceOrigin:o.sourceOrigin,runtimeToken:String(o.wpsuite?.runtimeToken||""),ignoreHttpsErrors:o.ignoreHttpsErrors}),h=await p.releaseFingerprint(c.includeSubsites),y=me({wordpressFingerprint:h,targetFingerprint:g,sourceOrigin:o.sourceOrigin,sitemapPaths:Se(o.sitemapPaths),blockedPathPrefixes:o.blockedPathPrefixes,assetPathPrefixes:o.assetPathPrefixes,noJavaScriptRenderPathPrefixes:o.noJavaScriptRenderPathPrefixes,urlRewriteMode:o.urlRewriteMode});if(s.releaseFingerprint!==y)return {status:"baseline-required",reason:"The installed WordPress release changed after the last verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline."};let l=await p.head({consumerId:s.consumerId,scopeFingerprint:s.scopeFingerprint,baselineId:s.baselineId,postTypes:c.postTypes,includeSubsites:c.includeSubsites}),S=l.committedSequence;if(i?.command==="content-sync"&&i.coalesceKey===r){let b=await v(m.join(e.runtimeDir,"content-sync-current.json"),null);b?.coalesceKey===r&&Number.isSafeInteger(b.toSequence)&&(S=Math.max(S,Number(b.toSequence)));}return {status:l.headSequence>S?"pending":"current",headSequence:l.headSequence,committedSequence:l.committedSequence}}catch(o){return o instanceof E&&o.code==="baseline-required"?{status:"baseline-required",reason:o.message}:{status:"unavailable",reason:o instanceof Error?o.message:String(o)}}}async function Be(e,t,n,r,i,a){let s=m.join(e.runtimeDir,"content-sync-state.json"),c=(await v(s,{})).rules??{},u=c[n]??{};c[n]={...u,ruleId:t,coalesceKey:n,...Number.isSafeInteger(i)?{observedHeadSequence:i}:{},...Number.isSafeInteger(a)?{committedSequence:a}:{},retryAttempt:0,nextRetryAt:null,lastError:r,baselineStatus:"required",baselineReason:r,trailingWorkDetected:Number.isSafeInteger(i)&&Number.isSafeInteger(a)?Number(i)>Number(a):!!u.trailingWorkDetected},await w(s,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:c});}function ue(e,t){let n=(t.url||"").trim(),r=t.crawlMode||"full",i=(t.deploymentProfile||"").trim(),a=(t.coalesceKey||"").trim();return e.some(s=>s.command===t.command&&(t.command!=="content-sync"||(s.coalesceKey||"").trim()===a)&&(s.url||"").trim()===n&&(s.crawlMode||"full")===r&&(s.deploymentProfile||"").trim()===i&&(s.status===void 0||s.status==="queued"||s.status==="retry-wait"||s.status==="running"))}async function jt(e,t,n){if(!t.allowSchedulerAutoEnqueue)return 0;let r=await Et(e);if(!r.enabled||r.rules.length===0)return 0;if(t.assertActiveSubscriptionForIdentity)try{await t.assertActiveSubscriptionForIdentity(r.wpsuite??null);}catch(l){let S=l instanceof Error?l.message:String(l);return console.warn(`[queue-runner] scheduler auto-enqueue skipped: ${S}`),0}let i=m.join(e.runtimeDir,"queue.json"),a=m.join(e.runtimeDir,"current-run.json"),s=Date.now(),o=0,c=[],u=[],d=[],g=new Map,[p,h,y]=await Promise.all([v(i,[]),v(a,null),v(re(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}})]);for(let l of r.rules){if(!l.enabled||l.command!=="content-sync")continue;let S=Math.floor(s/(l.intervalMinutes*60*1e3)),b=y.lastEvaluatedBucketByRuleId?.[l.id]??y.lastEnqueuedBucketByRuleId?.[l.id]??-1;if(S<=b)continue;let D=oe(l,r.publisherConfig);ue(p,{command:l.command,deploymentProfile:l.deploymentProfile,coalesceKey:D})||g.set(l.id,await Ft(e,r,l,D,h));}await j(e.runtimeDir,async()=>{let l=await v(i,[]),S=await v(a,null),b=await v(re(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}});b.lastEnqueuedBucketByRuleId??={},b.lastEvaluatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.lastCreatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.coalescedCountByRuleId??={};let D=[...l];S&&S.status==="running"&&D.unshift(S);for(let f of r.rules){if(!f.enabled)continue;let A=f.intervalMinutes*60*1e3,x=Math.floor(s/A),J=b.lastEvaluatedBucketByRuleId[f.id]??b.lastEnqueuedBucketByRuleId[f.id]??-1;if(x<=J)continue;let O=f.command==="content-sync"?oe(f,r.publisherConfig):"";b.lastEvaluatedBucketByRuleId[f.id]=x;let T=f.command==="content-sync"?l:D;if(ue(T,{command:f.command,url:f.url,crawlMode:f.crawlMode,deploymentProfile:f.deploymentProfile,coalesceKey:O})){b.coalescedCountByRuleId[f.id]=(b.coalescedCountByRuleId[f.id]??0)+1,u.push(f);continue}if(f.command==="content-sync"){let K=g.get(f.id);if(!K||K.status!=="pending"){let ge=K??{status:"unavailable",reason:"Content-sync demand was not checked."};ge.status!=="current"&&d.push({rule:f,check:ge});continue}}let W={id:randomUUID(),command:f.command,...f.command==="content-sync"?{ruleId:f.id,coalesceKey:O,attempt:0}:{},...(f.command==="publish"||f.command==="crawl")&&f.crawlMode?{crawlMode:f.crawlMode}:{},...(f.command==="publish"||f.command==="deploy"||f.command==="invalidate"||f.command==="content-sync")&&f.deploymentProfile?{deploymentProfile:f.deploymentProfile}:{},...f.url?{url:f.url}:{},enqueueSource:"scheduler",...r.wpsuite?{wpsuite:r.wpsuite}:{},status:"queued",createdAt:new Date().toISOString(),createdBy:0};l.push(W),D.push(W),c.push({job:W,rule:f}),b.lastEnqueuedBucketByRuleId[f.id]=x,b.lastCreatedBucketByRuleId[f.id]=x,o+=1;}o>0&&await w(i,l),await w(re(e.runtimeDir),b);});for(let l of c){if(l.rule.command==="content-sync"){let S=g.get(l.rule.id);await I(e.runtimeDir,{eventType:"content-sync-demand-detected",status:"queued",actorSource:"queue-runner-scheduler",jobId:l.job.id,command:l.job.command,message:"Pending journal changes require a content-sync job.",details:{ruleId:l.rule.id,headSequence:S?.headSequence??0,committedSequence:S?.committedSequence??0}});}await I(e.runtimeDir,{eventType:"job-created",status:"queued",actorSource:"queue-runner-scheduler",jobId:l.job.id,command:l.job.command,message:"Scheduler auto-enqueued a job.",details:{ruleId:l.rule.id,intervalMinutes:l.rule.intervalMinutes,timezone:r.timezone,deploymentProfile:l.rule.deploymentProfile||"",url:l.rule.url||""}});}for(let l of u)await I(e.runtimeDir,{eventType:"content-sync-coalesced",status:"queued",actorSource:"queue-runner-scheduler",command:l.command,message:"Scheduler demand was coalesced into an existing content-sync job.",details:{ruleId:l.id}});for(let l of d){if(l.check.status==="baseline-required"){let S=oe(l.rule,r.publisherConfig);await Be(e,l.rule.id,S,l.check.reason||"A successful full or incremental publish must establish a new content-sync baseline.",l.check.headSequence,l.check.committedSequence);}await I(e.runtimeDir,{eventType:l.check.status==="baseline-required"?"content-sync-baseline-required":"content-sync-demand-check-failed",status:l.check.status==="unavailable"?"failed":"info",actorSource:"queue-runner-scheduler",command:"content-sync",message:l.check.reason||"The content-sync consumer is already at the current journal head.",details:{ruleId:l.rule.id,headSequence:l.check.headSequence??0,committedSequence:l.check.committedSequence??0}});}return o}async function Jt(e){let t={pid:process.pid,startedAt:new Date().toISOString()};await P.writeFile(e,JSON.stringify(t,null,2),{encoding:"utf8",flag:"wx"});}async function Ot(e){try{await P.unlink(e);}catch{}}function Lt(e,t){let n=[m.join(e,"dist",`${t}.js`),m.join(e,`${t}.js`)];for(let r of n)if(existsSync(r))return r;throw new Error(`Cannot find ${t}.js in ${m.join(e,"dist")} or ${e}`)}async function q(e,t,n,r,i,a,s){if(s){let u=await ke(t,s);if(u)throw new U(u,n)}let o=Lt(e,n),c={...process.env,...a??{}};return c.STATIC_PUBLISHER_RUNTIME_DIR=t,i&&(c.PUBLISHER_CONFIG=i),await new Promise((u,d)=>{let g=null,p=null,h=null,y=null,l=false,S="",b=spawn(process.execPath,[o,...r],{cwd:e,env:c,stdio:["inherit","inherit","pipe"]});b.stderr?.on("data",A=>{S+=A.toString("utf8");});let D=()=>{p&&clearInterval(p),h&&clearInterval(h),y&&clearTimeout(y);},f=async()=>{if(!(!s||g||l)){l=true;try{let A=await ke(t,s);if(!A)return;g=A,b.kill("SIGTERM"),y=setTimeout(()=>{b.kill("SIGKILL");},1e4);}finally{l=false;}}};s&&(p=setInterval(()=>{f();},1e3),h=setInterval(()=>{(async()=>{let A=Je(t),x=await v(A,{});await w(A,{...x,checkedAt:new Date().toISOString(),status:"running",currentJobId:s||x.currentJobId||"",currentStep:n});})().catch(()=>{});},15e3)),b.on("error",A=>{D(),d(A);}),b.on("close",(A,x)=>{D();let J=fe(S).trim();if(J&&process.stderr.write(`${J}
3
- `),g){d(new U(g,n));return}if(A===0)u();else {let O=J.split(/\r?\n/).map(T=>T.trim()).find(T=>T&&!T.startsWith("at "));d(new Error(`${n} exited with code ${A??-1}${x?` (signal ${x})`:""}${O?`: ${O}`:""}`));}});}),0}function Bt(e){let t=e.awsTempCreds;if(!t)return {};let n={};return typeof t.accessKeyId=="string"&&t.accessKeyId.trim()!==""&&(n.AWS_ACCESS_KEY_ID=t.accessKeyId.trim()),typeof t.secretAccessKey=="string"&&t.secretAccessKey.trim()!==""&&(n.AWS_SECRET_ACCESS_KEY=t.secretAccessKey.trim()),typeof t.sessionToken=="string"&&t.sessionToken.trim()!==""&&(n.AWS_SESSION_TOKEN=t.sessionToken.trim()),n}async function Mt(e,t){let n=s=>{if(!s||typeof s!="object")return null;let o=s,c={...o.siteSettings??{},...String(o.accountId??"").trim()&&!o.siteSettings?.accountId?{accountId:String(o.accountId).trim()}:{},...String(o.siteId??"").trim()&&!o.siteSettings?.siteId?{siteId:String(o.siteId).trim()}:{},...o.siteSettings?.subscriber===true||o.subscriber===true?{subscriber:true}:{}},u={...String(o.apiBase??"").trim()?{apiBase:String(o.apiBase).trim()}:{},...String(o.runtimeToken??o.nonce??"").trim()?{runtimeToken:String(o.runtimeToken??o.nonce).trim()}:{},...String(o.virtualAssetBaseUrl??o.uploadUrl??"").trim()?{virtualAssetBaseUrl:String(o.virtualAssetBaseUrl??o.uploadUrl).trim()}:{},...Object.keys(c).length>0?{siteSettings:c}:{},...o.subscriptionType?{subscriptionType:o.subscriptionType}:{}};return Object.keys(u).length>0?u:null},r=await te(t.configPath).catch(()=>null),i=n(r?.wpsuite);if(i)return i;let a=n(e.wpsuite);return a||null}async function Ut(e,t){return await Mt(e,t)}function de(e){let t={...e,status:"queued"};return delete t.startedAt,delete t.endedAt,delete t.exitCode,delete t.error,delete t.stopRequestedAt,delete t.stopRequestedByUserId,delete t.stopRequestedByLogin,delete t.stopMode,delete t.stoppedStep,t}async function $t(e,t){let n=await v(le(e.runtimeDir),null),r=n&&n.details&&typeof n.details.phase=="string"?n.details.phase.trim():"";if(r)return r;let i=n&&typeof n.currentStep=="string"?n.currentStep.trim():"";if(i)return i;let a=await Oe(e).catch(()=>"");if(a){let s=await v(m.join(a,"current-crawl-event.json"),null),o=s&&typeof s.currentStep=="string"?s.currentStep.trim():"";if(o)return o}return t}function Nt(e,t){if((e.command==="publish"||e.command==="crawl")&&t==="rewrite-text")return "rewrite-text"}async function Qt(e){let t=m.join(e.runtimeDir,"queue.json"),n=m.join(e.runtimeDir,"current-run.json"),r=await j(e.runtimeDir,async()=>{let i=await v(n,null);if(!i||i.status!=="running"&&i.status!=="queued")return null;let a=await v(t,[]),s=Array.isArray(a)?a.filter(c=>c?.id!==i.id):[],o=de(i);return await w(t,[o,...s]),await w(n,null),o});r&&(await M(e.runtimeDir,r.id),await I(e.runtimeDir,{eventType:"job-recovered",status:"queued",actorSource:"queue-runner",jobId:r.id,command:r.command,message:"Recovered stale current-run entry back into queue."}));}async function Wt(e,t,n){try{if((n.shouldEnforceSubscriptionOnJobExecution?.(e)??n.enforceSubscriptionOnJobExecution)&&n.assertActiveSubscriptionForIdentity){let o=await Ut(e,t);await n.assertActiveSubscriptionForIdentity(o);}let i=Bt(e),a=e.resumeFromStep==="rewrite-text"?["--resume-rewrite"]:e.crawlMode==="incremental"?["--crawl-mode","incremental"]:[],s=e.deploymentProfile?["--profile",e.deploymentProfile]:[];if(e.command==="publish")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-release-cutoff"}),await q(t.exporterDir,t.runtimeDir,"content-sync",["--capture-baseline-cutoffs",e.id],t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:e.resumeFromStep==="rewrite-text"?"rewrite-text":"crawl"}),await q(t.exporterDir,t.runtimeDir,"crawl",a,t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await q(t.exporterDir,t.runtimeDir,"deploy",s,t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await q(t.exporterDir,t.runtimeDir,"invalidate",s,t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-baseline"}),await q(t.exporterDir,t.runtimeDir,"content-sync",["--establish-baselines",e.id],t.configPath,i,e.id),{exitCode:0};if(e.command==="crawl")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:e.resumeFromStep==="rewrite-text"?"rewrite-text":"crawl"}),await q(t.exporterDir,t.runtimeDir,"crawl",a,t.configPath,i,e.id),{exitCode:0};if(e.command==="deploy")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await q(t.exporterDir,t.runtimeDir,"deploy",s,t.configPath,i,e.id),{exitCode:0};if(e.command==="invalidate")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await q(t.exporterDir,t.runtimeDir,"invalidate",s,t.configPath,i,e.id),{exitCode:0};if(e.command==="retry-timeouts")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"retry-timeouts"}),await q(t.exporterDir,t.runtimeDir,"crawl",["--retry-timeouts"],t.configPath,i,e.id),{exitCode:0};if(e.command==="url"){let o=(e.url||"").trim();return o?(await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"url"}),await q(t.exporterDir,t.runtimeDir,"crawl",["--url",o],t.configPath,i,e.id),{exitCode:0}):{exitCode:2,error:"Missing url for command 'url'"}}return e.command==="content-sync"?!e.ruleId||!e.coalesceKey?{exitCode:2,error:"Content-sync queue jobs require ruleId and coalesceKey."}:(await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync"}),await q(t.exporterDir,t.runtimeDir,"content-sync",["--job-id",e.id],t.configPath,i,e.id),{exitCode:0}):{exitCode:2,error:`Unsupported command: ${e.command}`}}catch(r){return r instanceof U?{exitCode:130,error:"Job stop requested.",stopped:true,stopRequest:r.request,stoppedStep:r.step}:{exitCode:1,error:r instanceof Error?r.message:String(r)}}}function Me(e){return async function(n=process.argv.slice(2)){let r=wt(n),i=m.join(r.runtimeDir,"export.lock"),a=m.join(r.runtimeDir,"last-run.json");await F(r.runtimeDir),await C(r,"starting",{message:"queue-runner starting"});try{await Jt(i);}catch(s){if((s&&typeof s=="object"&&"code"in s?String(s.code||""):"")!=="EEXIST")throw s;console.log("[queue-runner] lock active, skipping"),await C(r,"lock-active",{message:"lock active, skipped this cron tick"});return}try{await Qt(r);let s=await jt(r,e),o=0;for(let c=0;c<r.maxJobs;c++){let u=await Ht(r,e);if(u==="none"||(o+=1,u==="stopped"))break}if(o===0)await F(r.runtimeDir),await C(r,"idle",{processedJobs:o,schedulerEnqueued:s,message:"no queued jobs"});else {await F(r.runtimeDir);let c=await v(a,null),u=String(c?.status||"").trim();await C(r,u==="success"?"job-success":u==="stopped"?"job-stopped":"job-failed",{processedJobs:o,schedulerEnqueued:s,lastJobId:c?.id,lastJobCommand:c?.command,lastJobStatus:c?.status,lastJobExitCode:c?.exitCode,lastJobError:c?.error,...u==="stopped"?{currentStep:c?.stoppedStep,stopRequestedAt:c?.stopRequestedAt,stopRequestedByLogin:c?.stopRequestedByLogin,stopRequestedMode:c?.stopMode,lastStoppedStep:c?.stoppedStep,message:c?.stopMode==="requeue"?`Job stopped during ${c?.stoppedStep||c?.command||"active step"} and requeued.`:`Job stopped during ${c?.stoppedStep||c?.command||"active step"} and left out of queue.`}:{}});}}catch(s){let o=s instanceof Error?s.message:String(s);throw await C(r,"error",{message:o}),await I(r.runtimeDir,{eventType:"queue-runner-error",status:"failed",actorSource:"queue-runner",message:"Unhandled queue-runner error.",details:{error:o}}),s}finally{await Ot(i);}}}async function Te(e,t){if(t.command!=="content-sync"||!t.coalesceKey)return false;let n=Ae(e.runtimeDir),r=await xe(n,t.coalesceKey),i=await Re(n),a=i.rules[t.coalesceKey];return a&&(i.rules[t.coalesceKey]={...a,trailingWorkDetected:true,retryAttempt:0,nextRetryAt:null,lastError:null},await qe(n,i)),r}async function Kt(e,t,n){let r=m.join(e.runtimeDir,"queue.json"),i=m.join(e.runtimeDir,"current-run.json");await j(e.runtimeDir,async()=>{let a=await v(r,[]),s=Array.isArray(a)?a.filter(u=>u?.id!==t.id):[],o=de(t),c=Nt(t,n);c?o.resumeFromStep=c:delete o.resumeFromStep,await w(r,[o,...s]),await w(i,null);});}async function _t(e,t,n){let r=Math.max(0,t.attempt??0)+1,i=Math.min(3600*1e3,6e4*2**(r-1)),a=Math.floor(i*.2*Math.random()),s=new Date(Date.now()+i+a).toISOString(),o={...de(t),status:"retry-wait",attempt:r,nextAttemptAt:s,error:n};if(await j(e.runtimeDir,async()=>{let c=m.join(e.runtimeDir,"queue.json"),u=m.join(e.runtimeDir,"current-run.json"),g=(await v(c,[])).filter(p=>p.id!==t.id);await w(c,[o,...g]),await w(u,null);}),t.coalesceKey){let c=m.join(e.runtimeDir,"content-sync-state.json"),d=(await v(c,{})).rules??{};d[t.coalesceKey]={...d[t.coalesceKey]??{},ruleId:t.ruleId||"",coalesceKey:t.coalesceKey,retryAttempt:r,nextRetryAt:s,lastError:n,trailingWorkDetected:true},await w(c,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:d});}return o}async function zt(e,t){if(!t.coalesceKey||!t.ruleId||!(await v(m.join(e.runtimeDir,"content-sync-state.json"),null))?.rules?.[t.coalesceKey]?.trailingWorkDetected)return null;let r=null;await j(e.runtimeDir,async()=>{let a=m.join(e.runtimeDir,"queue.json"),s=await v(a,[]);ue(s,{command:"content-sync",coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile})||(r={id:randomUUID(),command:"content-sync",ruleId:t.ruleId,coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile,enqueueSource:"scheduler",wpsuite:t.wpsuite,status:"queued",attempt:0,createdAt:new Date().toISOString(),createdBy:0},await w(a,[...s,r]));});let i=r;return i&&await I(e.runtimeDir,{eventType:"content-sync-trailing-job-created",status:"queued",actorSource:"queue-runner",jobId:i.id,command:i.command,message:"Content changes after the stable cutoff created one trailing job.",details:{predecessorJobId:t.id,ruleId:t.ruleId,coalesceKey:t.coalesceKey}}),i}async function Ht(e,t){let n=m.join(e.runtimeDir,"queue.json"),r=m.join(e.runtimeDir,"current-run.json"),i=m.join(e.runtimeDir,"last-run.json"),a=new Date().toISOString(),s=await j(e.runtimeDir,async()=>{let g=await v(n,[]);if(!Array.isArray(g)||g.length===0)return null;let p=Date.parse(String(g[0]?.nextAttemptAt||""));if(Number.isFinite(p)&&p>Date.now())return null;let h={...g[0],status:"running",startedAt:a};return delete h.nextAttemptAt,await w(n,g.slice(1)),await w(r,h),h});if(!s)return "none";await F(e.runtimeDir),await I(e.runtimeDir,{eventType:"job-run-started",status:"running",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Job execution started.",details:{createdAt:s.createdAt||"",startedAt:a,createdBy:s.createdBy??null,deploymentProfile:s.deploymentProfile||"",queuedWithTempAwsCreds:!!s.awsTempCreds}}),await C(e,"running",{currentJobId:s.id,currentJobCommand:s.command,currentStep:s.command});let o=await Wt(s,e,t),c=new Date().toISOString();if(o.stopped){let g=await $t(e,o.stoppedStep||s.command),p=o.stopRequest?.mode==="requeue"?"requeue":"stop",h={...s,status:"stopped",endedAt:c,exitCode:o.exitCode,...o.error?{error:o.error}:{},stopRequestedAt:o.stopRequest?.requestedAt||"",stopRequestedByUserId:typeof o.stopRequest?.requestedByUserId=="number"?o.stopRequest.requestedByUserId:null,stopRequestedByLogin:o.stopRequest?.requestedByLogin||"",stopMode:p,stoppedStep:g},y=false;p==="requeue"?await Kt(e,s,g):(await w(r,null),y=await Te(e,s));try{let l=await Q(h,e);h.logArchiveDir=l.archiveDir,h.logArchiveCreatedAt=l.archiveCreatedAt,h.logArchiveFileCount=l.archivedFiles.length;}catch(l){h.logArchiveError=l instanceof Error?l.message:String(l);}return await w(i,h),await M(e.runtimeDir,s.id),await F(e.runtimeDir),await I(e.runtimeDir,{eventType:"job-run-stopped",status:"stopped",actorSource:"queue-runner",jobId:s.id,command:s.command,message:p==="requeue"?"Job stop requested and requeued.":"Job stop requested and removed from active execution without requeue.",details:{startedAt:s.startedAt||"",endedAt:c,deploymentProfile:s.deploymentProfile||"",stopMode:p,stopRequestedAt:o.stopRequest?.requestedAt||"",stopRequestedByLogin:o.stopRequest?.requestedByLogin||"",stopRequestedByUserId:typeof o.stopRequest?.requestedByUserId=="number"?o.stopRequest.requestedByUserId:null,stoppedStep:g,logArchiveKey:h.logArchiveError||!h.logArchiveDir?"":m.basename(h.logArchiveDir),logArchiveDir:h.logArchiveDir||"",logArchiveCreatedAt:h.logArchiveCreatedAt||"",logArchiveFileCount:h.logArchiveFileCount??0,logArchiveError:h.logArchiveError||"",contentSyncAbandoned:y,journalCursorPreserved:y}}),"stopped"}if(s.command==="content-sync"&&o.exitCode!==0){if(/baseline (?:is missing or stale|required)|verified content-sync baseline|new baseline/i.test(o.error||"")){let y="The installed release no longer matches the verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline.";s.ruleId&&s.coalesceKey&&await Be(e,s.ruleId,s.coalesceKey,y);let l={...s,status:"failed",endedAt:c,exitCode:o.exitCode,error:y};await w(r,null),await F(e.runtimeDir),await Te(e,s);try{let S=await Q(l,e);l.logArchiveDir=S.archiveDir,l.logArchiveCreatedAt=S.archiveCreatedAt,l.logArchiveFileCount=S.archivedFiles.length;}catch(S){l.logArchiveError=S instanceof Error?S.message:String(S);}return await w(i,l),await M(e.runtimeDir,s.id),await I(e.runtimeDir,{eventType:"content-sync-baseline-required",status:"failed",actorSource:"queue-runner",jobId:s.id,command:s.command,message:y,details:{ruleId:s.ruleId||"",retryScheduled:false}}),"processed"}await I(e.runtimeDir,{eventType:"content-sync-failed",status:"failed",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Content-sync execution failed before cursor acknowledgement.",details:{attempt:s.attempt??0,error:o.error||"Content-sync execution failed."}});let p=await _t(e,s,o.error||"Content-sync execution failed."),h={...s,status:"retry-wait",endedAt:c,exitCode:o.exitCode,error:p.error,attempt:p.attempt,nextAttemptAt:p.nextAttemptAt};try{let y=await Q(h,e);h.logArchiveDir=y.archiveDir,h.logArchiveCreatedAt=y.archiveCreatedAt,h.logArchiveFileCount=y.archivedFiles.length;}catch(y){h.logArchiveError=y instanceof Error?y.message:String(y);}return await w(i,h),await M(e.runtimeDir,s.id),await I(e.runtimeDir,{eventType:"content-sync-retry-scheduled",status:"retry-wait",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Content-sync retry was scheduled with bounded backoff.",details:{attempt:p.attempt??0,nextAttemptAt:p.nextAttemptAt||"",error:p.error||""}}),"processed"}let u={...s,status:o.exitCode===0?"success":"failed",endedAt:c,exitCode:o.exitCode,...o.error?{error:o.error}:{}};await w(r,null),u.status==="success"&&u.command==="content-sync"&&await zt(e,u),await M(e.runtimeDir,s.id);try{let g=await Q(u,e);u.logArchiveDir=g.archiveDir,u.logArchiveCreatedAt=g.archiveCreatedAt,u.logArchiveFileCount=g.archivedFiles.length;}catch(g){u.logArchiveError=g instanceof Error?g.message:String(g);}await w(i,u);let d=s.startedAt&&u.endedAt?Math.max(0,Math.round((new Date(u.endedAt).getTime()-new Date(s.startedAt).getTime())/1e3)):void 0;return await I(e.runtimeDir,{eventType:"job-run-finished",status:u.status==="success"?"success":"failed",actorSource:"queue-runner",jobId:u.id,command:u.command,message:u.status==="success"?"Job execution finished successfully.":"Job execution finished with failure.",details:{startedAt:s.startedAt||"",endedAt:u.endedAt||"",durationSec:d,deploymentProfile:u.deploymentProfile||"",exitCode:u.exitCode??null,error:u.error||"",logArchiveKey:u.logArchiveError||!u.logArchiveDir?"":m.basename(u.logArchiveDir),logArchiveDir:u.logArchiveDir||"",logArchiveCreatedAt:u.logArchiveCreatedAt||"",logArchiveFileCount:u.logArchiveFileCount??0,logArchiveError:u.logArchiveError||""}}),"processed"}async function Gt(e){if(!e)throw new Error("Missing remote WPSuite publisher state. Refresh the runtime config before running subscription-gated jobs.");if(e.subscriptionType!=="PROFESSIONAL"&&e.subscriptionType!=="AGENCY")throw new Error("This job requires an active WPSuite publisher subscription in the remote site configuration.")}var Yt={allowSchedulerAutoEnqueue:true,enforceSubscriptionOnJobExecution:false,shouldEnforceSubscriptionOnJobExecution:e=>Fe(e)||e.enqueueSource==="scheduler"||e.createdBy===0,assertActiveSubscriptionForIdentity:Gt},Ue=Me(Yt),Zt=Ue,$n=Ue;process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href&&Zt().catch(e=>{console.error(e),process.exit(1);});export{$n as default,Zt as main,Ue as runQueueRunner};
1
+ import {pathToFileURL}from'url';import P from'fs/promises';import {existsSync,createReadStream,createWriteStream}from'fs';import m from'path';import {spawn}from'child_process';import {randomUUID,createHash}from'crypto';import He from'http';import be from'https';import {pipeline}from'stream/promises';import {createGzip}from'zlib';function he(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function z(e){return Array.isArray(e)?e.map(z):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>[t,z(n)])):e}function Ke(e){return JSON.stringify(z(e))}function L(e){return createHash("sha256").update(Ke(e)).digest("hex")}function fe(e){return L({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 _e(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 $(e){let t=he((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=he((e.listingPaths??[]).map(r=>{let i=String(r).trim();if(!i)return "";let a=new URL(i,"https://content-sync.invalid/");if(a.origin!=="https://content-sync.invalid"&&/^https?:\/\//i.test(i))throw new Error("Content-sync listing routes must be same-site paths.");let s=`/${a.pathname.replace(/^\/+|\/+$/g,"")}`;return s==="/"?"/":`${s}/`}));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 H(e,t,n){return L({schemaVersion:1,sourceOrigin:_e(e),ruleId:t.trim(),scope:$(n)})}function V(e){return L({schemaVersion:1,...e})}function Se(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]")}var ye=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";ye?m.join(ye,"current-progress.json"):"";Promise.resolve();var Ve="wp-json/smartcloud-static-publisher/v1/content-sync/",Ge=25*1024*1024,E=class extends Error{status;code;constructor(t,n,r){super(r),this.name="ContentSyncApiError",this.status=t,this.code=n;}},N=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){let n=this.options.sourceOrigin.replace(/\/+$/,"")+"/";return new URL(`${Ve}${t}`,n)}async post(t,n){let r=this.endpoint(t),i=Buffer.from(JSON.stringify(n),"utf8"),a=r.protocol==="https:"?be:He,s=r.protocol==="https:"?new be.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((o,c)=>{let u=a.request(r,{method:"POST",agent:s,headers:{"content-type":"application/json","content-length":String(i.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},d=>{let g=[],p=0;d.on("data",h=>{if(p+=h.byteLength,p>Ge){d.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(h);}),d.on("error",c),d.on("end",()=>{let h=Buffer.concat(g).toString("utf8"),y;try{y=h?JSON.parse(h):{};}catch{c(new E(d.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let l=d.statusCode??500;if(l<200||l>=300){c(new E(l,String(y.code??"request-failed"),String(y.message??`Content-sync API request failed (${l}).`)));return}o(y);});});u.on("timeout",()=>u.destroy(new Error(`Content-sync API request timed out: ${t}`))),u.on("error",c),u.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,i=new Set(t.postTypes);for(;r<t.throughSequence;){let a=await this.post("events",{...t,afterSequence:r,limit:250});if(a.fromSequence!==r||a.throughSequence!==t.throughSequence||a.nextSequence<r||a.nextSequence>t.throughSequence)throw new Error("Content-sync API returned an inconsistent event page.");if(!Array.isArray(a.items))throw new Error("Content-sync API returned a malformed event page.");let s=r;for(let o of a.items){if(!Number.isSafeInteger(o.sequence)||o.sequence<=s||o.sequence>t.throughSequence||!Number.isSafeInteger(o.postId)||o.postId<=0||!Number.isSafeInteger(o.blogId)||o.blogId<=0||!i.has(o.postType)||typeof o.operation!="string"||!o.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");s=o.sequence;}if(a.items.length>0&&a.nextSequence!==s)throw new Error("Content-sync API event cursor does not match the final returned event.");if(n.push(...a.items),!a.hasMore)break;if(a.nextSequence===r)throw new Error("Content-sync API event pagination did not advance.");r=a.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)}};function Ye(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Ze(e,t){return {...e,...t??{}}}function Xe(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function G(e,t){let n=String(t??e.deploymentTargetOverride??"").trim();if(!Ye(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 i=e.deploymentProfiles?.[r];if(!i)throw new Error(`Unknown deployment profile "${r}". Check the linked WPSuite publisher configuration.`);return {name:r,profile:i,config:{...e,targetOrigin:i.targetOrigin??e.targetOrigin,s3:Ze(e.s3,i.s3),cloudFront:Xe(e.cloudFront,i.cloudFront)}}}var Y=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function we(e){let t=new Set;for(let n of [...e,...Y]){let r=String(n||"").trim();r&&t.add(r);}return [...t]}function Pe(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function rt(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("/"),i=n.replace(/^\/+/,"").split("/").map(s=>s.replace(/[^A-Za-z0-9._-]/g,"")).filter(s=>s.length>0&&s!=="."&&s!=="..");if(i.length===0)return "";let a=`/${i.join("/")}`;return r&&m.extname(a)===""&&(a+="/"),a==="/"?"":a}function Z(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t.trim(),String(n??"")]).filter(([t])=>t.length>0))}function it(e){if(!e||typeof e!="object")return {};let t={};for(let[n,r]of Object.entries(e)){let i=n.trim();if(!i||!r||typeof r!="object")continue;let a=r,s={};if(typeof a.targetOrigin=="string"){let c=Pe(a.targetOrigin);c&&(s.targetOrigin=c);}let o=Z(a.extraReplacements);if(Object.keys(o).length>0&&(s.extraReplacements=o),a.s3&&typeof a.s3=="object"){let c={},u=a.s3;typeof u.bucket=="string"&&(c.bucket=u.bucket.trim()),typeof u.prefix=="string"&&(c.prefix=u.prefix.trim()),typeof u.region=="string"&&(c.region=u.region.trim()),typeof u.htmlCacheControl=="string"&&(c.htmlCacheControl=u.htmlCacheControl.trim()),typeof u.assetCacheControl=="string"&&(c.assetCacheControl=u.assetCacheControl.trim()),Object.keys(c).length>0&&(s.s3=c);}if(a.cloudFront&&typeof a.cloudFront=="object"){let c={},u=a.cloudFront;typeof u.distributionId=="string"&&(c.distributionId=u.distributionId.trim()),Array.isArray(u.invalidationPaths)&&(c.invalidationPaths=u.invalidationPaths.map(d=>String(d??"").trim()).filter(d=>d.length>0)),Object.keys(c).length>0&&(s.cloudFront=c);}t[i]=s;}return t}function ee(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function st(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 i=Number.parseInt(String(n.intervalMinutes??"0"),10);if(!Number.isFinite(i)||i<1)return null;let a=String(n.id??`${r}-${t+1}`).trim();if(!a)return null;let s=String(n.deploymentProfile??"").trim(),o=String(n.url??"").trim(),c=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"&&c.length===0?null:{id:a,enabled:n.enabled!==false,command:r,intervalMinutes:i,...r==="publish"||r==="crawl"?{crawlMode:n.crawlMode==="incremental"?"incremental":"full"}:{},...(r==="publish"||r==="deploy"||r==="invalidate"||r==="content-sync")&&s?{deploymentProfile:s}:{},...o?{url:o}:{},...c.length>0?{postTypes:c}:{},...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 Ce(e){let t=e&&typeof e=="object"?e:{},n=Array.isArray(t.rules)?t.rules.map((r,i)=>st(r,i)).filter(r=>!!r):[];return {enabled:t.enabled===true,timezone:typeof t.timezone=="string"&&t.timezone.trim()!==""?t.timezone.trim():"UTC",rules:n}}function ot(e){if(!e||typeof e!="object")return;let t=e,n={};for(let i of ["accountId","siteId"]){let a=t[i];typeof a=="string"&&a.trim()!==""&&(n[i]=a.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 te(e){let t=e&&typeof e=="object"?e:{},n=String(t.accountId??"").trim(),r=String(t.siteId??"").trim(),i=t.subscriber===true,a=ot(t.siteSettings),s={...a??{},...n?{accountId:a?.accountId??n}:{},...r?{siteId:a?.siteId??r}:{},...a?.subscriber===true||i?{subscriber:true}:{}},o={},c=String(t.apiBase??"").trim();c&&(o.apiBase=c);let u=String(t.runtimeToken??t.nonce??"").trim();u&&(o.runtimeToken=u);let d=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();d&&(o.virtualAssetBaseUrl=d),Object.keys(s).length>0&&(o.siteSettings=s);let g=ee(t.subscriptionType);return g&&(o.subscriptionType=g),o}function at(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function Ae(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function ct(e,t){let n=new Headers(e instanceof Request?e.headers:void 0);if(t?.headers)for(let[r,i]of new Headers(t.headers).entries())n.set(r,i);return n}async function Ie(e,t,n=5){let r=new URL(Ae(e)),i=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),a=r.protocol==="http:"?He:be,s=ct(e,t);return await new Promise((o,c)=>{let u=a.request(r,{method:i,headers:Object.fromEntries(s.entries()),...r.protocol==="https:"?{rejectUnauthorized:false}:{}},d=>{let g=[];d.on("data",p=>{g.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),d.on("error",c),d.on("end",()=>{let p=d.statusCode??0,h=d.headers.location;if(n>0&&h&&[301,302,303,307,308].includes(p)){o(Ie(new URL(h,r),{method:p===303?"GET":i},n-1));return}let y=new Headers;for(let[l,S]of Object.entries(d.headers))Array.isArray(S)?y.set(l,S.join(", ")):typeof S=="string"&&y.set(l,S);o(new Response(Buffer.concat(g),{status:p,statusText:d.statusMessage??"",headers:y}));});});u.on("error",c),u.end();})}function ut(e){let t=te(e.wpsuite),n={...t.siteSettings??{},...t.siteSettings?.subscriber===true||t.subscriptionType==="PROFESSIONAL"||t.subscriptionType==="AGENCY"?{subscriber:true}:{}},r=String(n.accountId??"").trim(),i=String(n.siteId??"").trim(),a=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();if(e.wpsuite={...t.apiBase?{apiBase:t.apiBase}:{},...t.runtimeToken?{runtimeToken:t.runtimeToken}:{},...a?{virtualAssetBaseUrl:a}:{},...Object.keys(n).length>0?{siteSettings:n}:{},...t.subscriptionType?{subscriptionType:t.subscriptionType}:{}},!r||!i||!a)return false;at(e.sourceOrigin);let s=globalThis,c={...s.WpSuite??{},siteSettings:n,uploadUrl:a};return t.apiBase&&(c.apiBase=t.apiBase),s.WpSuite=c,true}async function lt(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!ut(e)){let i=e.wpsuite?.siteSettings,a=[String(i?.accountId??"").trim()?"":"accountId",String(i?.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): ${a.join(", ")}.`,requests:[]}}}let n=globalThis.fetch,r=[];try{typeof n=="function"&&/^https?:\/\//i.test(t)&&(globalThis.fetch=(async(c,u)=>{let d=Ae(c);if(d.startsWith(t)){let g=(()=>{try{return new URL(d).pathname}catch{return d}})();try{let p=e.ignoreHttpsErrors&&d.startsWith("https://")?await Ie(c,u):await n(c,u);return r.push({path:g,status:p.status,ok:p.ok,error:null}),p}catch(p){throw r.push({path:g,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return n(c,u)}));let{getConfig:i}=await import('@smart-cloud/wpsuite-core'),a=await i("publisher"),s=a&&typeof a=="object"?a:null,o=ee(s?.subscriptionType);return {remote:s,diagnostic:{status:s?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:o??null,error:s?null:"WP Suite publisher config loader returned no configuration.",requests:r}}}catch(i){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:t,subscriptionType:null,error:i instanceof Error?i.message:String(i),requests:r}}}finally{typeof n=="function"&&(globalThis.fetch=n);}}function dt(e,t){let n=it(t?.deploymentProfiles),r=String(t?.defaultDeploymentProfile??"").trim(),i=n[r]?r:"",a=ee(t?.subscriptionType),s=te(e.wpsuite);return {...e,scheduler:Ce(t?.scheduler),deploymentProfiles:n,defaultDeploymentProfile:i,...a?{subscriptionType:a}:{},wpsuite:{...s,...s.siteSettings||a?{siteSettings:{...s.siteSettings??{},...s.siteSettings?.subscriber===true||a==="PROFESSIONAL"||a==="AGENCY"?{subscriber:true}:{}}}:{},...a?{subscriptionType:a}:{}}}}function X(e,t){let r=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!r)return t;let i=r.split("/").map(a=>a.trim()).filter(a=>a.length>0&&a!=="."&&a!=="..");return i.length>0?i.join("/"):t}function gt(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?m.resolve(e):""}function ve(e,t,n){let r=String(t||"").trim();return r&&m.isAbsolute(r)?m.resolve(r):m.resolve(e,X(r,n))}async function ne(e,t={}){let n=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",r=await P.readFile(n,"utf8"),i=JSON.parse(r);i.sourceOrigin=i.sourceOrigin.replace(/\/$/,""),i.targetOrigin=Pe(i.targetOrigin),i.ignoreHttpsErrors??=false,i.outputDir=String(i.outputDir||"export").trim()||"export",i.urlRewriteMode||=i.targetOrigin==="."?"relative":"absolute",i.noJavaScriptRenderPathPrefixes||=[],i.seedPaths||=[],i.generated404RequestPath=rt(i.generated404RequestPath),i.sitemapPaths||=[...Y],i.allowedAssetHosts||=[],i.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],i.blockedPathPrefixes||=[],i.blockedSearchFragments||=[],i.concurrency||=1,i.maxPages||=0,i.extraReplacements=Z(i.extraReplacements),i.postCrawlCopyMap=Z(i.postCrawlCopyMap),i.logDir=String(i.logDir||"logs").trim()||"logs",i.verbose??=false,i.logLevel||=i.verbose?"debug":"info",i.s3SyncMode||="sdk-upload-delete",i.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},i.readiness.timeoutMs??=1500,i.readiness.fallbackWaitMs??=1500,i.viewport||={width:1440,height:1200},i.navigationTimeoutMs||=3e4,i.assetDownloadConcurrency=Number(i.assetDownloadConcurrency)>0?Number(i.assetDownloadConcurrency):i.concurrency,i.rewriteConcurrency=Number(i.rewriteConcurrency)>0?Number(i.rewriteConcurrency):i.assetDownloadConcurrency,i.wpsuite=te(i.wpsuite),i.scheduler=Ce(void 0),i.deploymentProfiles={},i.defaultDeploymentProfile="",i.deploymentTargetOverride=String(i.deploymentTargetOverride??"").trim();let a=await lt(i);t.onRemotePublisherConfigDiagnostic?.(a.diagnostic);let s=dt(i,a.remote),o=gt(),c=o?m.resolve(o,".."):"";return c?(s.outputDir=ve(c,s.outputDir,"export"),s.logDir=ve(c,s.logDir,"logs")):(m.isAbsolute(s.outputDir)||(s.outputDir=X(s.outputDir,"export")),m.isAbsolute(s.logDir)||(s.logDir=X(s.logDir,"logs"))),s}function re(e){let t=m.resolve(e);return {state:m.join(t,"content-sync-state.json"),current:m.join(t,"content-sync-current.json"),impact:m.join(t,"content-sync-impact-plan.json"),checkpoint:m.join(t,"content-sync-checkpoint.json"),baseline:m.join(t,"content-sync-baseline.json"),activeRules:m.join(t,"content-sync-active-rules.json"),releaseCutoff:m.join(t,"content-sync-release-cutoff.json"),candidateManifest:m.join(t,"content-sync-candidate-manifest.json"),trustedManifest:m.join(t,"crawl-manifest.json"),invalidation:m.join(t,"content-sync-invalidation.json"),deployPlan:m.join(t,"deploy-plan.json")}}async function ie(e,t){await Re(e.activeRules,{contractVersion:1,evaluatedAt:new Date().toISOString(),entries:t});}async function Re(e,t){await P.mkdir(m.dirname(e),{recursive:true});let n=`${e}.${process.pid}.${Date.now()}.tmp`;await P.writeFile(n,JSON.stringify(t,null,2),"utf8"),await P.rename(n,e);}async function qe(e){try{return JSON.parse(await P.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 pt(e){let t=await qe(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 xe(e){let t=await qe(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 De(e,t){await Re(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function ke(e,t){let n=await pt(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 P.unlink(r);}catch(i){if(i.code!=="ENOENT")throw i}})),true)}function Oe(e){let t=String(e.command??"").trim();return (t==="publish"||t==="crawl")&&e.crawlMode==="incremental"||t==="content-sync"}var se=new Map;async function Te(e){let t=m.resolve(e),n=se.get(t);if(n!==void 0)return n;try{let r=JSON.parse(await P.readFile(t,"utf8")),i=String(r.version??"").trim();return se.set(t,i),i}catch{return se.set(t,""),""}}var U=class extends Error{request;step;constructor(t,n){super(`Stop requested during ${n}.`),this.name="StopRequestedError",this.request=t,this.step=n;}},bt="queue-mutation.lock",wt=5e3,vt=3e4;function Pt(e){let t=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",n=process.env.STATIC_PUBLISHER_EXPORTER_DIR||process.cwd(),r=process.env.PUBLISHER_CONFIG||"",i=1;for(let o=0;o<e.length;o++){let c=e[o];c==="--runtime-dir"?t=e[++o]||"":c.startsWith("--runtime-dir=")?t=c.slice(14):c==="--exporter-dir"?n=e[++o]||n:c.startsWith("--exporter-dir=")?n=c.slice(15):c==="--config"?r=e[++o]||"":c.startsWith("--config=")?r=c.slice(9):c==="--max-jobs"?i=Number.parseInt(e[++o]||"1",10):c.startsWith("--max-jobs=")&&(i=Number.parseInt(c.slice(11),10));}if(!t)throw new Error("Missing runtime dir. Use --runtime-dir or STATIC_PUBLISHER_RUNTIME_DIR.");let a=m.resolve(t),s=r?m.resolve(r):m.join(a,"config.json");return {runtimeDir:a,exporterDir:m.resolve(n),configPath:s,maxJobs:Number.isFinite(i)&&i>0?i:1}}async function w(e,t){try{let n=await P.readFile(e,"utf8");return JSON.parse(n)}catch(n){if((n&&typeof n=="object"&&"code"in n?String(n.code||""):"")!=="ENOENT"){let i=n instanceof Error?n.message:String(n);console.warn(`[queue-runner] failed to read JSON ${e}: ${i}`);}return t}}async function v(e,t){await P.mkdir(m.dirname(e),{recursive:true}),await P.writeFile(e,JSON.stringify(t,null,2),"utf8");}function Le(e){return m.join(e,bt)}async function Ct(e){await new Promise(t=>setTimeout(t,e));}async function At(e){let t=Le(e),n=Date.now()+wt;for(;;)try{await P.mkdir(m.dirname(t),{recursive:!0}),await P.writeFile(t,JSON.stringify({pid:process.pid,createdAt:new Date().toISOString()},null,2),{encoding:"utf8",flag:"wx"});return}catch(r){if((r&&typeof r=="object"&&"code"in r?String(r.code||""):"")!=="EEXIST")throw r;let a=await P.stat(t).catch(()=>null);if(a&&Date.now()-a.mtimeMs>vt){await P.unlink(t).catch(()=>{});continue}if(Date.now()>=n)throw new Error("Timed out acquiring queue mutation lock.",{cause:r});await Ct(50);}}async function It(e){await P.unlink(Le(e)).catch(()=>{});}async function j(e,t){await At(e);try{return await t()}finally{await It(e);}}function Be(e){return m.join(e,"queue-runner-heartbeat.json")}function ge(e){return m.join(e,"current-progress.json")}function oe(e){return m.join(e,"scheduler-state.json")}function Rt(e,t){let r=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!r)return t;let i=r.split("/").map(a=>a.trim()).filter(a=>a.length>0&&a!=="."&&a!=="..");return i.length>0?i.join("/"):t}async function Me(e){let t=await w(e.configPath||"",null),n=m.resolve(e.runtimeDir,".."),i=(typeof t?.logDir=="string"?t.logDir:"").trim();return i&&m.isAbsolute(i)?m.resolve(i):m.resolve(n,Rt(i,"logs"))}function ae(e,t){return String(e||"").trim().replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/^-+|-+$/g,"")||t}function qt(e){let t=e?new Date(e):new Date;return (Number.isNaN(t.getTime())?new Date:t).toISOString().replace(/[-:]/g,"").replace(/\.\d{3}Z$/,"Z")}function xt(e){return [qt(e.endedAt||e.startedAt),ae(e.command||"job","job"),ae(e.id||"job","job"),ae(e.status||"finished","finished")].join("-")}function Dt(e){return e.endsWith(".log.jsonl")||e.endsWith(".errors.jsonl")||e.endsWith(".rejected.jsonl")?true:["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"].includes(e)}function kt(e){return e==="errors.json"||e==="errors.jsonl"||e.endsWith(".errors.jsonl")?"errors":e==="ignored.json"||e==="ignored.jsonl"?"ignored":e==="rejected.json"||e==="rejected.jsonl"||e.endsWith(".rejected.jsonl")?"rejected":e==="skipped-http.json"||e==="skipped-http.jsonl"?"skipped-http":e==="timings.json"||e==="timings.jsonl"?"timings":e==="current-progress.json"?"current-progress":e==="current-crawl-event.json"?"current-crawl-event":e.endsWith(".log.jsonl")?e.replace(/\.log\.jsonl$/i,"-log"):e.replace(/\.[^.]+$/u,"")||"artifact"}function Tt(e){return e.endsWith(".gz")?"application/gzip":e.endsWith(".jsonl")?"application/x-ndjson":e.endsWith(".json")?"application/json":"text/plain"}async function ce(e,t,n){let r=`${n}.gz`,i=m.join(t,r);await pipeline(createReadStream(e),createGzip({level:9}),createWriteStream(i));let[a,s]=await Promise.all([P.stat(e),P.stat(i)]);return {role:kt(n),originalFileName:n,storedFileName:r,compressed:true,compression:"gzip",contentType:Tt(r),originalSize:a.size,storedSize:s.size}}async function Q(e,t){let n=await Me(t),r=new Date().toISOString(),i=xt(e),a=m.join(n,"archive",i),s=[],o=[];await P.mkdir(a,{recursive:true});try{let d=await P.readdir(n,{withFileTypes:!0});for(let g of d){if(!g.isFile()||!Dt(g.name))continue;let p=await ce(m.join(n,g.name),a,g.name);o.push(p),s.push(p.storedFileName);}}catch(d){if((d&&typeof d=="object"&&"code"in d?String(d.code||""):"")!=="ENOENT")throw d}let c=ge(t.runtimeDir);if(existsSync(c)){let d=await ce(c,a,"current-progress.json");o.push(d),s.push(d.storedFileName);}if(e.command==="content-sync")for(let d of ["content-sync-current.json","content-sync-impact-plan.json","content-sync-checkpoint.json","content-sync-state.json","content-sync-invalidation.json","deploy-plan.json"]){let g=m.join(t.runtimeDir,d);if(!existsSync(g))continue;let p=await ce(g,a,d);o.push(p),s.push(p.storedFileName);}let u=[...s,"job.json"];return await v(m.join(a,"job.json"),{manifestVersion:1,archivedAt:r,archiveKey:i,archiveDir:a,logDir:n,runtimeDir:t.runtimeDir,exporterDir:t.exporterDir,configPath:t.configPath||"",archivedFiles:u,artifacts:o,job:e}),{archiveKey:i,archiveDir:a,archiveCreatedAt:r,archivedFiles:u,artifacts:o}}function Et(e){return m.join(e,"audit-events.jsonl")}function le(e){return m.join(e,"stop-request.json")}async function Ue(e){try{let t=await P.readFile(le(e),"utf8"),n=JSON.parse(t);return n&&typeof n=="object"?n:null}catch{return null}}async function Ee(e,t){let n=await Ue(e);if(!n)return null;let r=String(n.targetJobId||"").trim();return r&&r!==t?null:n}async function M(e,t){if(!t){await P.unlink(le(e)).catch(()=>{});return}let n=await Ue(e);if(!n)return;let r=String(n.targetJobId||"").trim();(!r||r===t)&&await P.unlink(le(e)).catch(()=>{});}async function R(e,t){try{let n={occurredAt:t.occurredAt||new Date().toISOString(),status:t.status||"info",actorSource:t.actorSource||"queue-runner",...t};await P.mkdir(e,{recursive:!0}),await P.appendFile(Et(e),`${JSON.stringify(n)}
2
+ `,"utf8");}catch{}}async function C(e,t,n){let[r,i]=await Promise.all([Te(m.join(e.exporterDir,"package.json")),Te(m.join(e.exporterDir,"node_modules","@smart-cloud","wpsuite-core","package.json"))]),a={checkedAt:new Date().toISOString(),status:t,pid:process.pid,nodePath:process.execPath,nodeVersion:process.version,exporterVersion:r,wpsuiteCoreVersion:i,runtimeDir:e.runtimeDir,exporterDir:e.exporterDir,...n??{}};try{await v(Be(e.runtimeDir),a);}catch{}}async function F(e){try{await P.unlink(ge(e));}catch{}}function Ft(e,t){let n=e.command;if(n!=="publish"&&n!=="crawl"&&n!=="deploy"&&n!=="invalidate"&&n!=="retry-timeouts"&&n!=="url"&&n!=="content-sync")return null;let r=Number.parseInt(String(e.intervalMinutes??"0"),10);if(!Number.isFinite(r)||r<1)return null;let i=(e.id||`${n}-${t+1}`).trim();if(!i)return null;let a=e.enabled!==false,s=typeof e.url=="string"?e.url.trim():"",o=typeof e.deploymentProfile=="string"?e.deploymentProfile.trim():"",c=Array.isArray(e.postTypes)?[...new Set(e.postTypes.map(g=>String(g).trim()).filter(Boolean))]:[],u=Array.isArray(e.listingPaths)?[...new Set(e.listingPaths.map(g=>String(g).trim()).filter(Boolean))]:[],d=(n==="publish"||n==="crawl")&&e.crawlMode==="incremental"?"incremental":"full";return n==="url"&&!s||n==="content-sync"&&c.length===0?null:{id:i,enabled:a,command:n,intervalMinutes:r,...n==="publish"||n==="crawl"?{crawlMode:d}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&o?{deploymentProfile:o}:{},...s?{url:s}:{},...c.length>0?{postTypes:c}:{},...u.length>0?{listingPaths:u}:{},...typeof e.includeSubsites=="boolean"?{includeSubsites:e.includeSubsites}:{},...typeof e.includePostTypeArchives=="boolean"?{includePostTypeArchives:e.includePostTypeArchives}:{},...typeof e.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:e.includeTaxonomyArchives}:{},...typeof e.includeAuthorArchives=="boolean"?{includeAuthorArchives:e.includeAuthorArchives}:{},...typeof e.includeDateArchives=="boolean"?{includeDateArchives:e.includeDateArchives}:{},...typeof e.includePostsPage=="boolean"?{includePostsPage:e.includePostsPage}:{},...typeof e.includeSitemapChain=="boolean"?{includeSitemapChain:e.includeSitemapChain}:{}}}async function jt(e){let t=await ne(e.configPath).catch(()=>null),n=t?.scheduler,r=Array.isArray(n?.rules)?n.rules.map((i,a)=>Ft(i,a)).filter(i=>!!i):[];return {enabled:!!n?.enabled,timezone:typeof n?.timezone=="string"&&n.timezone.trim()?n.timezone.trim():"UTC",rules:r,wpsuite:t?.wpsuite,publisherConfig:t}}function W(e,t){if(!t)throw new Error("Content-sync scheduler requires a valid publisher config.");let n=$({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}),r=H(t.sourceOrigin,e.id,n),i=G(t,e.deploymentProfile),a=V({sourceOrigin:t.sourceOrigin,targetOrigin:i.config.targetOrigin,deploymentProfile:i.name||"",s3Bucket:i.config.s3.bucket,s3Prefix:i.config.s3.prefix,cloudFrontDistributionId:i.config.cloudFront.distributionId,urlRewriteMode:i.config.urlRewriteMode,extraReplacements:i.profile?.extraReplacements??{}});return `content-sync:${e.id}:${L({scopeFingerprint:r,targetFingerprint:a}).slice(0,32)}`}function Jt(e,t,n,r){let i=[n[t],r[t]].filter(s=>!!s).filter(s=>s.coalesceKey===t&&s.ruleId===e.id).map(s=>String(s.consumerId??"").trim()).filter(Boolean),a=[...new Set(i)];return a.length===1?a[0]:""}async function Fe(e,t){let n=re(e.runtimeDir);if(await ie(n,[]),!t?.enabled||!t.publisherConfig)return;let r=t.rules.filter(u=>u.enabled&&u.command==="content-sync");if(r.length===0)return;let[i,a]=await Promise.all([w(n.state,{}),w(n.baseline,{})]),s=i.rules??{},o=a.entries??{},c=[];for(let u of r){let d=W(u,t.publisherConfig),g=Jt(u,d,s,o);g&&c.push({ruleId:u.id,coalesceKey:d,consumerId:g});}await ie(n,c);}async function Ot(e,t,n,r,i){let s=(await w(m.join(e.runtimeDir,"content-sync-baseline.json"),{})).entries?.[r];if(!s?.consumerId||!s.scopeFingerprint||!s.baselineId)return {status:"baseline-required",reason:"No verified normal-release baseline exists for this rule scope."};try{let o=t.publisherConfig;if(!o)throw new Error("Publisher configuration is unavailable.");let c=$({postTypes:n.postTypes??[],listingPaths:n.listingPaths??[],includeSubsites:n.includeSubsites,includePostTypeArchives:n.includePostTypeArchives,includeTaxonomyArchives:n.includeTaxonomyArchives,includeAuthorArchives:n.includeAuthorArchives,includeDateArchives:n.includeDateArchives,includePostsPage:n.includePostsPage,includeSitemapChain:n.includeSitemapChain}),u=H(o.sourceOrigin,n.id,c),d=G(o,n.deploymentProfile),g=V({sourceOrigin:o.sourceOrigin,targetOrigin:d.config.targetOrigin,deploymentProfile:d.name||"",s3Bucket:d.config.s3.bucket,s3Prefix:d.config.s3.prefix,cloudFrontDistributionId:d.config.cloudFront.distributionId,urlRewriteMode:d.config.urlRewriteMode,extraReplacements:d.profile?.extraReplacements??{}});if(s.scopeFingerprint!==u||s.targetFingerprint!==g||!s.releaseFingerprint)return {status:"baseline-required",reason:"The verified content-sync baseline does not match the active scope or deployment target. Run a successful full or incremental publish to establish a new baseline."};let p=new N({sourceOrigin:o.sourceOrigin,runtimeToken:String(o.wpsuite?.runtimeToken||""),ignoreHttpsErrors:o.ignoreHttpsErrors}),h=await p.releaseFingerprint(c.includeSubsites),y=fe({wordpressFingerprint:h,targetFingerprint:g,sourceOrigin:o.sourceOrigin,sitemapPaths:we(o.sitemapPaths),blockedPathPrefixes:o.blockedPathPrefixes,assetPathPrefixes:o.assetPathPrefixes,noJavaScriptRenderPathPrefixes:o.noJavaScriptRenderPathPrefixes,urlRewriteMode:o.urlRewriteMode});if(s.releaseFingerprint!==y)return {status:"baseline-required",reason:"The installed WordPress release changed after the last verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline."};let l=await p.head({consumerId:s.consumerId,scopeFingerprint:s.scopeFingerprint,baselineId:s.baselineId,postTypes:c.postTypes,includeSubsites:c.includeSubsites}),S=l.committedSequence;if(i?.command==="content-sync"&&i.coalesceKey===r){let b=await w(m.join(e.runtimeDir,"content-sync-current.json"),null);b?.coalesceKey===r&&Number.isSafeInteger(b.toSequence)&&(S=Math.max(S,Number(b.toSequence)));}return {status:l.headSequence>S?"pending":"current",headSequence:l.headSequence,committedSequence:l.committedSequence}}catch(o){return o instanceof E&&o.code==="baseline-required"?{status:"baseline-required",reason:o.message}:{status:"unavailable",reason:o instanceof Error?o.message:String(o)}}}async function $e(e,t,n,r,i,a){let s=m.join(e.runtimeDir,"content-sync-state.json"),c=(await w(s,{})).rules??{},u=c[n]??{};c[n]={...u,ruleId:t,coalesceKey:n,...Number.isSafeInteger(i)?{observedHeadSequence:i}:{},...Number.isSafeInteger(a)?{committedSequence:a}:{},retryAttempt:0,nextRetryAt:null,lastError:r,baselineStatus:"required",baselineReason:r,trailingWorkDetected:Number.isSafeInteger(i)&&Number.isSafeInteger(a)?Number(i)>Number(a):!!u.trailingWorkDetected},await v(s,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:c});}function de(e,t){let n=(t.url||"").trim(),r=t.crawlMode||"full",i=(t.deploymentProfile||"").trim(),a=(t.coalesceKey||"").trim();return e.some(s=>s.command===t.command&&(t.command!=="content-sync"||(s.coalesceKey||"").trim()===a)&&(s.url||"").trim()===n&&(s.crawlMode||"full")===r&&(s.deploymentProfile||"").trim()===i&&(s.status===void 0||s.status==="queued"||s.status==="retry-wait"||s.status==="running"))}async function Lt(e,t,n){if(!t.allowSchedulerAutoEnqueue)return await Fe(e,null),0;let r=await jt(e);if(await Fe(e,r),!r.enabled||r.rules.length===0)return 0;if(t.assertActiveSubscriptionForIdentity)try{await t.assertActiveSubscriptionForIdentity(r.wpsuite??null);}catch(l){let S=l instanceof Error?l.message:String(l);return console.warn(`[queue-runner] scheduler auto-enqueue skipped: ${S}`),0}let i=m.join(e.runtimeDir,"queue.json"),a=m.join(e.runtimeDir,"current-run.json"),s=Date.now(),o=0,c=[],u=[],d=[],g=new Map,[p,h,y]=await Promise.all([w(i,[]),w(a,null),w(oe(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}})]);for(let l of r.rules){if(!l.enabled||l.command!=="content-sync")continue;let S=Math.floor(s/(l.intervalMinutes*60*1e3)),b=y.lastEvaluatedBucketByRuleId?.[l.id]??y.lastEnqueuedBucketByRuleId?.[l.id]??-1;if(S<=b)continue;let D=W(l,r.publisherConfig);de(p,{command:l.command,deploymentProfile:l.deploymentProfile,coalesceKey:D})||g.set(l.id,await Ot(e,r,l,D,h));}await j(e.runtimeDir,async()=>{let l=await w(i,[]),S=await w(a,null),b=await w(oe(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}});b.lastEnqueuedBucketByRuleId??={},b.lastEvaluatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.lastCreatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.coalescedCountByRuleId??={};let D=[...l];S&&S.status==="running"&&D.unshift(S);for(let f of r.rules){if(!f.enabled)continue;let A=f.intervalMinutes*60*1e3,x=Math.floor(s/A),J=b.lastEvaluatedBucketByRuleId[f.id]??b.lastEnqueuedBucketByRuleId[f.id]??-1;if(x<=J)continue;let O=f.command==="content-sync"?W(f,r.publisherConfig):"";b.lastEvaluatedBucketByRuleId[f.id]=x;let T=f.command==="content-sync"?l:D;if(de(T,{command:f.command,url:f.url,crawlMode:f.crawlMode,deploymentProfile:f.deploymentProfile,coalesceKey:O})){b.coalescedCountByRuleId[f.id]=(b.coalescedCountByRuleId[f.id]??0)+1,u.push(f);continue}if(f.command==="content-sync"){let _=g.get(f.id);if(!_||_.status!=="pending"){let me=_??{status:"unavailable",reason:"Content-sync demand was not checked."};me.status!=="current"&&d.push({rule:f,check:me});continue}}let K={id:randomUUID(),command:f.command,...f.command==="content-sync"?{ruleId:f.id,coalesceKey:O,attempt:0}:{},...(f.command==="publish"||f.command==="crawl")&&f.crawlMode?{crawlMode:f.crawlMode}:{},...(f.command==="publish"||f.command==="deploy"||f.command==="invalidate"||f.command==="content-sync")&&f.deploymentProfile?{deploymentProfile:f.deploymentProfile}:{},...f.url?{url:f.url}:{},enqueueSource:"scheduler",...r.wpsuite?{wpsuite:r.wpsuite}:{},status:"queued",createdAt:new Date().toISOString(),createdBy:0};l.push(K),D.push(K),c.push({job:K,rule:f}),b.lastEnqueuedBucketByRuleId[f.id]=x,b.lastCreatedBucketByRuleId[f.id]=x,o+=1;}o>0&&await v(i,l),await v(oe(e.runtimeDir),b);});for(let l of c){if(l.rule.command==="content-sync"){let S=g.get(l.rule.id);await R(e.runtimeDir,{eventType:"content-sync-demand-detected",status:"queued",actorSource:"queue-runner-scheduler",jobId:l.job.id,command:l.job.command,message:"Pending journal changes require a content-sync job.",details:{ruleId:l.rule.id,headSequence:S?.headSequence??0,committedSequence:S?.committedSequence??0}});}await R(e.runtimeDir,{eventType:"job-created",status:"queued",actorSource:"queue-runner-scheduler",jobId:l.job.id,command:l.job.command,message:"Scheduler auto-enqueued a job.",details:{ruleId:l.rule.id,intervalMinutes:l.rule.intervalMinutes,timezone:r.timezone,deploymentProfile:l.rule.deploymentProfile||"",url:l.rule.url||""}});}for(let l of u)await R(e.runtimeDir,{eventType:"content-sync-coalesced",status:"queued",actorSource:"queue-runner-scheduler",command:l.command,message:"Scheduler demand was coalesced into an existing content-sync job.",details:{ruleId:l.id}});for(let l of d){if(l.check.status==="baseline-required"){let S=W(l.rule,r.publisherConfig);await $e(e,l.rule.id,S,l.check.reason||"A successful full or incremental publish must establish a new content-sync baseline.",l.check.headSequence,l.check.committedSequence);}await R(e.runtimeDir,{eventType:l.check.status==="baseline-required"?"content-sync-baseline-required":"content-sync-demand-check-failed",status:l.check.status==="unavailable"?"failed":"info",actorSource:"queue-runner-scheduler",command:"content-sync",message:l.check.reason||"The content-sync consumer is already at the current journal head.",details:{ruleId:l.rule.id,headSequence:l.check.headSequence??0,committedSequence:l.check.committedSequence??0}});}return o}async function Bt(e){let t={pid:process.pid,startedAt:new Date().toISOString()};await P.writeFile(e,JSON.stringify(t,null,2),{encoding:"utf8",flag:"wx"});}async function Mt(e){try{await P.unlink(e);}catch{}}function Ut(e,t){let n=[m.join(e,"dist",`${t}.js`),m.join(e,`${t}.js`)];for(let r of n)if(existsSync(r))return r;throw new Error(`Cannot find ${t}.js in ${m.join(e,"dist")} or ${e}`)}async function q(e,t,n,r,i,a,s){if(s){let u=await Ee(t,s);if(u)throw new U(u,n)}let o=Ut(e,n),c={...process.env,...a??{}};return c.STATIC_PUBLISHER_RUNTIME_DIR=t,i&&(c.PUBLISHER_CONFIG=i),await new Promise((u,d)=>{let g=null,p=null,h=null,y=null,l=false,S="",b=spawn(process.execPath,[o,...r],{cwd:e,env:c,stdio:["inherit","inherit","pipe"]});b.stderr?.on("data",A=>{S+=A.toString("utf8");});let D=()=>{p&&clearInterval(p),h&&clearInterval(h),y&&clearTimeout(y);},f=async()=>{if(!(!s||g||l)){l=true;try{let A=await Ee(t,s);if(!A)return;g=A,b.kill("SIGTERM"),y=setTimeout(()=>{b.kill("SIGKILL");},1e4);}finally{l=false;}}};s&&(p=setInterval(()=>{f();},1e3),h=setInterval(()=>{(async()=>{let A=Be(t),x=await w(A,{});await v(A,{...x,checkedAt:new Date().toISOString(),status:"running",currentJobId:s||x.currentJobId||"",currentStep:n});})().catch(()=>{});},15e3)),b.on("error",A=>{D(),d(A);}),b.on("close",(A,x)=>{D();let J=Se(S).trim();if(J&&process.stderr.write(`${J}
3
+ `),g){d(new U(g,n));return}if(A===0)u();else {let O=J.split(/\r?\n/).map(T=>T.trim()).find(T=>T&&!T.startsWith("at "));d(new Error(`${n} exited with code ${A??-1}${x?` (signal ${x})`:""}${O?`: ${O}`:""}`));}});}),0}function $t(e){let t=e.awsTempCreds;if(!t)return {};let n={};return typeof t.accessKeyId=="string"&&t.accessKeyId.trim()!==""&&(n.AWS_ACCESS_KEY_ID=t.accessKeyId.trim()),typeof t.secretAccessKey=="string"&&t.secretAccessKey.trim()!==""&&(n.AWS_SECRET_ACCESS_KEY=t.secretAccessKey.trim()),typeof t.sessionToken=="string"&&t.sessionToken.trim()!==""&&(n.AWS_SESSION_TOKEN=t.sessionToken.trim()),n}async function Nt(e,t){let n=s=>{if(!s||typeof s!="object")return null;let o=s,c={...o.siteSettings??{},...String(o.accountId??"").trim()&&!o.siteSettings?.accountId?{accountId:String(o.accountId).trim()}:{},...String(o.siteId??"").trim()&&!o.siteSettings?.siteId?{siteId:String(o.siteId).trim()}:{},...o.siteSettings?.subscriber===true||o.subscriber===true?{subscriber:true}:{}},u={...String(o.apiBase??"").trim()?{apiBase:String(o.apiBase).trim()}:{},...String(o.runtimeToken??o.nonce??"").trim()?{runtimeToken:String(o.runtimeToken??o.nonce).trim()}:{},...String(o.virtualAssetBaseUrl??o.uploadUrl??"").trim()?{virtualAssetBaseUrl:String(o.virtualAssetBaseUrl??o.uploadUrl).trim()}:{},...Object.keys(c).length>0?{siteSettings:c}:{},...o.subscriptionType?{subscriptionType:o.subscriptionType}:{}};return Object.keys(u).length>0?u:null},r=await ne(t.configPath).catch(()=>null),i=n(r?.wpsuite);if(i)return i;let a=n(e.wpsuite);return a||null}async function Qt(e,t){return await Nt(e,t)}function pe(e){let t={...e,status:"queued"};return delete t.startedAt,delete t.endedAt,delete t.exitCode,delete t.error,delete t.stopRequestedAt,delete t.stopRequestedByUserId,delete t.stopRequestedByLogin,delete t.stopMode,delete t.stoppedStep,t}async function Wt(e,t){let n=await w(ge(e.runtimeDir),null),r=n&&n.details&&typeof n.details.phase=="string"?n.details.phase.trim():"";if(r)return r;let i=n&&typeof n.currentStep=="string"?n.currentStep.trim():"";if(i)return i;let a=await Me(e).catch(()=>"");if(a){let s=await w(m.join(a,"current-crawl-event.json"),null),o=s&&typeof s.currentStep=="string"?s.currentStep.trim():"";if(o)return o}return t}function Kt(e,t){if((e.command==="publish"||e.command==="crawl")&&t==="rewrite-text")return "rewrite-text"}async function _t(e){let t=m.join(e.runtimeDir,"queue.json"),n=m.join(e.runtimeDir,"current-run.json"),r=await j(e.runtimeDir,async()=>{let i=await w(n,null);if(!i||i.status!=="running"&&i.status!=="queued")return null;let a=await w(t,[]),s=Array.isArray(a)?a.filter(c=>c?.id!==i.id):[],o=pe(i);return await v(t,[o,...s]),await v(n,null),o});r&&(await M(e.runtimeDir,r.id),await R(e.runtimeDir,{eventType:"job-recovered",status:"queued",actorSource:"queue-runner",jobId:r.id,command:r.command,message:"Recovered stale current-run entry back into queue."}));}async function zt(e,t,n){try{if((n.shouldEnforceSubscriptionOnJobExecution?.(e)??n.enforceSubscriptionOnJobExecution)&&n.assertActiveSubscriptionForIdentity){let o=await Qt(e,t);await n.assertActiveSubscriptionForIdentity(o);}let i=$t(e),a=e.resumeFromStep==="rewrite-text"?["--resume-rewrite"]:e.crawlMode==="incremental"?["--crawl-mode","incremental"]:[],s=e.deploymentProfile?["--profile",e.deploymentProfile]:[];if(e.command==="publish")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-release-cutoff"}),await q(t.exporterDir,t.runtimeDir,"content-sync",["--capture-baseline-cutoffs",e.id],t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:e.resumeFromStep==="rewrite-text"?"rewrite-text":"crawl"}),await q(t.exporterDir,t.runtimeDir,"crawl",a,t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await q(t.exporterDir,t.runtimeDir,"deploy",s,t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await q(t.exporterDir,t.runtimeDir,"invalidate",s,t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-baseline"}),await q(t.exporterDir,t.runtimeDir,"content-sync",["--establish-baselines",e.id],t.configPath,i,e.id),{exitCode:0};if(e.command==="crawl")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:e.resumeFromStep==="rewrite-text"?"rewrite-text":"crawl"}),await q(t.exporterDir,t.runtimeDir,"crawl",a,t.configPath,i,e.id),{exitCode:0};if(e.command==="deploy")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await q(t.exporterDir,t.runtimeDir,"deploy",s,t.configPath,i,e.id),{exitCode:0};if(e.command==="invalidate")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await q(t.exporterDir,t.runtimeDir,"invalidate",s,t.configPath,i,e.id),{exitCode:0};if(e.command==="retry-timeouts")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"retry-timeouts"}),await q(t.exporterDir,t.runtimeDir,"crawl",["--retry-timeouts"],t.configPath,i,e.id),{exitCode:0};if(e.command==="url"){let o=(e.url||"").trim();return o?(await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"url"}),await q(t.exporterDir,t.runtimeDir,"crawl",["--url",o],t.configPath,i,e.id),{exitCode:0}):{exitCode:2,error:"Missing url for command 'url'"}}return e.command==="content-sync"?!e.ruleId||!e.coalesceKey?{exitCode:2,error:"Content-sync queue jobs require ruleId and coalesceKey."}:(await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync"}),await q(t.exporterDir,t.runtimeDir,"content-sync",["--job-id",e.id],t.configPath,i,e.id),{exitCode:0}):{exitCode:2,error:`Unsupported command: ${e.command}`}}catch(r){return r instanceof U?{exitCode:130,error:"Job stop requested.",stopped:true,stopRequest:r.request,stoppedStep:r.step}:{exitCode:1,error:r instanceof Error?r.message:String(r)}}}function Ne(e){return async function(n=process.argv.slice(2)){let r=Pt(n),i=m.join(r.runtimeDir,"export.lock"),a=m.join(r.runtimeDir,"last-run.json");await F(r.runtimeDir),await C(r,"starting",{message:"queue-runner starting"});try{await Bt(i);}catch(s){if((s&&typeof s=="object"&&"code"in s?String(s.code||""):"")!=="EEXIST")throw s;console.log("[queue-runner] lock active, skipping"),await C(r,"lock-active",{message:"lock active, skipped this cron tick"});return}try{await _t(r);let s=await Lt(r,e),o=0;for(let c=0;c<r.maxJobs;c++){let u=await Yt(r,e);if(u==="none"||(o+=1,u==="stopped"))break}if(o===0)await F(r.runtimeDir),await C(r,"idle",{processedJobs:o,schedulerEnqueued:s,message:"no queued jobs"});else {await F(r.runtimeDir);let c=await w(a,null),u=String(c?.status||"").trim();await C(r,u==="success"?"job-success":u==="stopped"?"job-stopped":"job-failed",{processedJobs:o,schedulerEnqueued:s,lastJobId:c?.id,lastJobCommand:c?.command,lastJobStatus:c?.status,lastJobExitCode:c?.exitCode,lastJobError:c?.error,...u==="stopped"?{currentStep:c?.stoppedStep,stopRequestedAt:c?.stopRequestedAt,stopRequestedByLogin:c?.stopRequestedByLogin,stopRequestedMode:c?.stopMode,lastStoppedStep:c?.stoppedStep,message:c?.stopMode==="requeue"?`Job stopped during ${c?.stoppedStep||c?.command||"active step"} and requeued.`:`Job stopped during ${c?.stoppedStep||c?.command||"active step"} and left out of queue.`}:{}});}}catch(s){let o=s instanceof Error?s.message:String(s);throw await C(r,"error",{message:o}),await R(r.runtimeDir,{eventType:"queue-runner-error",status:"failed",actorSource:"queue-runner",message:"Unhandled queue-runner error.",details:{error:o}}),s}finally{await Mt(i);}}}async function je(e,t){if(t.command!=="content-sync"||!t.coalesceKey)return false;let n=re(e.runtimeDir),r=await ke(n,t.coalesceKey),i=await xe(n),a=i.rules[t.coalesceKey];return a&&(i.rules[t.coalesceKey]={...a,trailingWorkDetected:true,retryAttempt:0,nextRetryAt:null,lastError:null},await De(n,i)),r}async function Ht(e,t,n){let r=m.join(e.runtimeDir,"queue.json"),i=m.join(e.runtimeDir,"current-run.json");await j(e.runtimeDir,async()=>{let a=await w(r,[]),s=Array.isArray(a)?a.filter(u=>u?.id!==t.id):[],o=pe(t),c=Kt(t,n);c?o.resumeFromStep=c:delete o.resumeFromStep,await v(r,[o,...s]),await v(i,null);});}async function Vt(e,t,n){let r=Math.max(0,t.attempt??0)+1,i=Math.min(3600*1e3,6e4*2**(r-1)),a=Math.floor(i*.2*Math.random()),s=new Date(Date.now()+i+a).toISOString(),o={...pe(t),status:"retry-wait",attempt:r,nextAttemptAt:s,error:n};if(await j(e.runtimeDir,async()=>{let c=m.join(e.runtimeDir,"queue.json"),u=m.join(e.runtimeDir,"current-run.json"),g=(await w(c,[])).filter(p=>p.id!==t.id);await v(c,[o,...g]),await v(u,null);}),t.coalesceKey){let c=m.join(e.runtimeDir,"content-sync-state.json"),d=(await w(c,{})).rules??{};d[t.coalesceKey]={...d[t.coalesceKey]??{},ruleId:t.ruleId||"",coalesceKey:t.coalesceKey,retryAttempt:r,nextRetryAt:s,lastError:n,trailingWorkDetected:true},await v(c,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:d});}return o}async function Gt(e,t){if(!t.coalesceKey||!t.ruleId||!(await w(m.join(e.runtimeDir,"content-sync-state.json"),null))?.rules?.[t.coalesceKey]?.trailingWorkDetected)return null;let r=null;await j(e.runtimeDir,async()=>{let a=m.join(e.runtimeDir,"queue.json"),s=await w(a,[]);de(s,{command:"content-sync",coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile})||(r={id:randomUUID(),command:"content-sync",ruleId:t.ruleId,coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile,enqueueSource:"scheduler",wpsuite:t.wpsuite,status:"queued",attempt:0,createdAt:new Date().toISOString(),createdBy:0},await v(a,[...s,r]));});let i=r;return i&&await R(e.runtimeDir,{eventType:"content-sync-trailing-job-created",status:"queued",actorSource:"queue-runner",jobId:i.id,command:i.command,message:"Content changes after the stable cutoff created one trailing job.",details:{predecessorJobId:t.id,ruleId:t.ruleId,coalesceKey:t.coalesceKey}}),i}async function Yt(e,t){let n=m.join(e.runtimeDir,"queue.json"),r=m.join(e.runtimeDir,"current-run.json"),i=m.join(e.runtimeDir,"last-run.json"),a=new Date().toISOString(),s=await j(e.runtimeDir,async()=>{let g=await w(n,[]);if(!Array.isArray(g)||g.length===0)return null;let p=Date.parse(String(g[0]?.nextAttemptAt||""));if(Number.isFinite(p)&&p>Date.now())return null;let h={...g[0],status:"running",startedAt:a};return delete h.nextAttemptAt,await v(n,g.slice(1)),await v(r,h),h});if(!s)return "none";await F(e.runtimeDir),await R(e.runtimeDir,{eventType:"job-run-started",status:"running",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Job execution started.",details:{createdAt:s.createdAt||"",startedAt:a,createdBy:s.createdBy??null,deploymentProfile:s.deploymentProfile||"",queuedWithTempAwsCreds:!!s.awsTempCreds}}),await C(e,"running",{currentJobId:s.id,currentJobCommand:s.command,currentStep:s.command});let o=await zt(s,e,t),c=new Date().toISOString();if(o.stopped){let g=await Wt(e,o.stoppedStep||s.command),p=o.stopRequest?.mode==="requeue"?"requeue":"stop",h={...s,status:"stopped",endedAt:c,exitCode:o.exitCode,...o.error?{error:o.error}:{},stopRequestedAt:o.stopRequest?.requestedAt||"",stopRequestedByUserId:typeof o.stopRequest?.requestedByUserId=="number"?o.stopRequest.requestedByUserId:null,stopRequestedByLogin:o.stopRequest?.requestedByLogin||"",stopMode:p,stoppedStep:g},y=false;p==="requeue"?await Ht(e,s,g):(await v(r,null),y=await je(e,s));try{let l=await Q(h,e);h.logArchiveDir=l.archiveDir,h.logArchiveCreatedAt=l.archiveCreatedAt,h.logArchiveFileCount=l.archivedFiles.length;}catch(l){h.logArchiveError=l instanceof Error?l.message:String(l);}return await v(i,h),await M(e.runtimeDir,s.id),await F(e.runtimeDir),await R(e.runtimeDir,{eventType:"job-run-stopped",status:"stopped",actorSource:"queue-runner",jobId:s.id,command:s.command,message:p==="requeue"?"Job stop requested and requeued.":"Job stop requested and removed from active execution without requeue.",details:{startedAt:s.startedAt||"",endedAt:c,deploymentProfile:s.deploymentProfile||"",stopMode:p,stopRequestedAt:o.stopRequest?.requestedAt||"",stopRequestedByLogin:o.stopRequest?.requestedByLogin||"",stopRequestedByUserId:typeof o.stopRequest?.requestedByUserId=="number"?o.stopRequest.requestedByUserId:null,stoppedStep:g,logArchiveKey:h.logArchiveError||!h.logArchiveDir?"":m.basename(h.logArchiveDir),logArchiveDir:h.logArchiveDir||"",logArchiveCreatedAt:h.logArchiveCreatedAt||"",logArchiveFileCount:h.logArchiveFileCount??0,logArchiveError:h.logArchiveError||"",contentSyncAbandoned:y,journalCursorPreserved:y}}),"stopped"}if(s.command==="content-sync"&&o.exitCode!==0){if(/baseline (?:is missing or stale|required)|verified content-sync baseline|new baseline/i.test(o.error||"")){let y="The installed release no longer matches the verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline.";s.ruleId&&s.coalesceKey&&await $e(e,s.ruleId,s.coalesceKey,y);let l={...s,status:"failed",endedAt:c,exitCode:o.exitCode,error:y};await v(r,null),await F(e.runtimeDir),await je(e,s);try{let S=await Q(l,e);l.logArchiveDir=S.archiveDir,l.logArchiveCreatedAt=S.archiveCreatedAt,l.logArchiveFileCount=S.archivedFiles.length;}catch(S){l.logArchiveError=S instanceof Error?S.message:String(S);}return await v(i,l),await M(e.runtimeDir,s.id),await R(e.runtimeDir,{eventType:"content-sync-baseline-required",status:"failed",actorSource:"queue-runner",jobId:s.id,command:s.command,message:y,details:{ruleId:s.ruleId||"",retryScheduled:false}}),"processed"}await R(e.runtimeDir,{eventType:"content-sync-failed",status:"failed",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Content-sync execution failed before cursor acknowledgement.",details:{attempt:s.attempt??0,error:o.error||"Content-sync execution failed."}});let p=await Vt(e,s,o.error||"Content-sync execution failed."),h={...s,status:"retry-wait",endedAt:c,exitCode:o.exitCode,error:p.error,attempt:p.attempt,nextAttemptAt:p.nextAttemptAt};try{let y=await Q(h,e);h.logArchiveDir=y.archiveDir,h.logArchiveCreatedAt=y.archiveCreatedAt,h.logArchiveFileCount=y.archivedFiles.length;}catch(y){h.logArchiveError=y instanceof Error?y.message:String(y);}return await v(i,h),await M(e.runtimeDir,s.id),await R(e.runtimeDir,{eventType:"content-sync-retry-scheduled",status:"retry-wait",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Content-sync retry was scheduled with bounded backoff.",details:{attempt:p.attempt??0,nextAttemptAt:p.nextAttemptAt||"",error:p.error||""}}),"processed"}let u={...s,status:o.exitCode===0?"success":"failed",endedAt:c,exitCode:o.exitCode,...o.error?{error:o.error}:{}};await v(r,null),u.status==="success"&&u.command==="content-sync"&&await Gt(e,u),await M(e.runtimeDir,s.id);try{let g=await Q(u,e);u.logArchiveDir=g.archiveDir,u.logArchiveCreatedAt=g.archiveCreatedAt,u.logArchiveFileCount=g.archivedFiles.length;}catch(g){u.logArchiveError=g instanceof Error?g.message:String(g);}await v(i,u);let d=s.startedAt&&u.endedAt?Math.max(0,Math.round((new Date(u.endedAt).getTime()-new Date(s.startedAt).getTime())/1e3)):void 0;return await R(e.runtimeDir,{eventType:"job-run-finished",status:u.status==="success"?"success":"failed",actorSource:"queue-runner",jobId:u.id,command:u.command,message:u.status==="success"?"Job execution finished successfully.":"Job execution finished with failure.",details:{startedAt:s.startedAt||"",endedAt:u.endedAt||"",durationSec:d,deploymentProfile:u.deploymentProfile||"",exitCode:u.exitCode??null,error:u.error||"",logArchiveKey:u.logArchiveError||!u.logArchiveDir?"":m.basename(u.logArchiveDir),logArchiveDir:u.logArchiveDir||"",logArchiveCreatedAt:u.logArchiveCreatedAt||"",logArchiveFileCount:u.logArchiveFileCount??0,logArchiveError:u.logArchiveError||""}}),"processed"}async function Xt(e){if(!e)throw new Error("Missing remote WPSuite publisher state. Refresh the runtime config before running subscription-gated jobs.");if(e.subscriptionType!=="PROFESSIONAL"&&e.subscriptionType!=="AGENCY")throw new Error("This job requires an active WPSuite publisher subscription in the remote site configuration.")}var en={allowSchedulerAutoEnqueue:true,enforceSubscriptionOnJobExecution:false,shouldEnforceSubscriptionOnJobExecution:e=>Oe(e)||e.enqueueSource==="scheduler"||e.createdBy===0,assertActiveSubscriptionForIdentity:Xt},Qe=Ne(en),tn=Qe,Wn=Qe;process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href&&tn().catch(e=>{console.error(e),process.exit(1);});export{Wn as default,tn as main,Qe as runQueueRunner};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smart-cloud/publisher-exporter",
3
- "version": "1.1.53",
3
+ "version": "1.1.54",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "Headless Playwright static publisher for WordPress/Elementor sites with sitemap-only page discovery, strict asset capture, escaped URL rewrite, structured logs, and targeted retry modes.",