@smart-cloud/publisher-exporter 1.1.76 → 1.1.78

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,19 +1,19 @@
1
- import D,{readFile}from'fs/promises';import Xo from'http';import Wn from'https';import C from'path';import cs,{createHash}from'crypto';import {request,chromium}from'playwright';import {gunzipSync}from'zlib';import {setTimeout as setTimeout$1}from'timers/promises';import Ci from'fast-glob';import Os from'mime-types';import {availableParallelism}from'os';import {Worker}from'worker_threads';import {LambdaClient,InvokeCommand}from'@aws-sdk/client-lambda';import {DynamoDBClient,GetItemCommand}from'@aws-sdk/client-dynamodb';import {S3Client,GetObjectCommand,PutObjectCommand,CopyObjectCommand,ListObjectsV2Command,HeadObjectCommand,DeleteObjectsCommand}from'@aws-sdk/client-s3';import {fromTemporaryCredentials}from'@aws-sdk/credential-providers';import {Upload}from'@aws-sdk/lib-storage';import {CloudFrontClient,waitUntilInvalidationCompleted,CreateInvalidationCommand}from'@aws-sdk/client-cloudfront';var zo=new Set(["post-type","taxonomy","author","date","posts-page","listing"]);function ze(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function Jo(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))]}function Qo(e){return new URL(e).pathname.split("/").filter(Boolean).some(r=>{try{return decodeURIComponent(r).endsWith("__trashed")}catch{return r.endsWith("__trashed")}})}function Er(e){return Array.isArray(e)?e.map(Er):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[r])=>t.localeCompare(r)).map(([t,r])=>[t,Er(r)])):e}function Go(e){return JSON.stringify(Er(e))}function Le(e){return createHash("sha256").update(Go(e)).digest("hex")}function jn(e){return Le({schemaVersion:1,wordpressFingerprint:e.wordpressFingerprint,targetFingerprint:e.targetFingerprint,sourceOrigin:e.sourceOrigin,sitemapPaths:[...e.sitemapPaths].sort(),blockedPathPrefixes:[...e.blockedPathPrefixes].sort(),assetPathPrefixes:[...e.assetPathPrefixes].sort(),noJavaScriptRenderPathPrefixes:[...e.noJavaScriptRenderPathPrefixes].sort(),urlRewriteMode:e.urlRewriteMode})}function $r(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 xe(e,t){Zo(t);let r=new URL($r(e)),n=new URL(t,`${r.toString().replace(/\/$/,"")}/`);if(n.origin!==r.origin)throw new Error(`Content-sync URL is outside the source origin: ${t}`);let s=r.pathname.replace(/\/+$/,"");if(s&&n.pathname!==s&&!n.pathname.startsWith(`${s}/`))throw new Error(`Content-sync URL is outside the source site path: ${t}`);return n.hash="",n.toString()}function Zo(e){let r=String(e).split(/[?#]/,1)[0];for(let s=0;s<3;s+=1)try{let o=decodeURIComponent(r);if(o===r)break;r=o;}catch{throw new Error(`Content-sync URL has malformed encoding: ${e}`)}if(r.includes("\0")||r.includes("\\"))throw new Error(`Content-sync URL has an unsafe path: ${e}`);if(r.replace(/^[a-z][a-z0-9+.-]*:\/\/[^/]*/i,"").split("/").some(s=>s==="."||s===".."))throw new Error(`Content-sync URL has an unsafe path: ${e}`)}function Rt(e){let t=ze((e.postTypes??[]).map(n=>String(n).toLowerCase().replace(/[^a-z0-9_-]/g,"")));if(t.length===0)throw new Error("Content-sync requires at least one public post type.");let r=ze((e.listingPaths??[]).map(n=>{let s=String(n).trim();if(!s)return "";let o=new URL(s,"https://content-sync.invalid/");if(o.origin!=="https://content-sync.invalid"&&/^https?:\/\//i.test(s))throw new Error("Content-sync listing routes must be same-site paths.");let a=`/${o.pathname.replace(/^\/+|\/+$/g,"")}`;return a==="/"?"/":`${a}/`}));return {postTypes:t,listingPaths:r,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 Dn(e,t,r){return Le({schemaVersion:1,sourceOrigin:$r(e),ruleId:t.trim(),scope:Rt(r)})}function Fn(e){return Le({schemaVersion:1,...e})}function Un(e){let t=String(e.siteId??"").trim()||String(e.accountId??"").trim()||$r(e.sourceOrigin);return `content-sync:${Le({siteIdentity:t,ruleId:e.ruleId.trim(),targetFingerprint:e.targetFingerprint}).slice(0,48)}`}function Ln(e){let t=[...e].sort((s,o)=>s.sequence-o.sequence),r=new Map,n=-1;for(let s of t){if(!Number.isSafeInteger(s.sequence)||s.sequence<=n)throw new Error("Content-sync journal events are not strictly ordered.");if(n=s.sequence,!Number.isSafeInteger(s.blogId)||s.blogId<1)throw new Error("Content-sync journal event has an invalid multisite identity.");let o=`${s.blogId}:${s.postType}:${s.postId}`,a=r.get(o);if(!a){r.set(o,{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 [...r.values()].sort((s,o)=>s.fromSequence-o.fromSequence)}function Mn(e){return e?.status==="publish"&&!!e.url}function Yo(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 Or(e){let t=Rt(e.scope),r=new Map;Ln(e.events);for(let n of e.events)for(let s of [n.before,n.after])for(let o of s?.archiveFamilies??[]){if(!zo.has(o.kind)||!Yo(o.kind,t))continue;let a={...o,url:xe(e.sourceOrigin,o.url)};r.set(`${a.kind}|${a.url}`,a);}for(let n of t.listingPaths){let s=xe(e.sourceOrigin,n);r.set(`listing|${s}`,{kind:"listing",url:s});}return [...r.values()].sort((n,s)=>n.url.localeCompare(s.url))}function qn(e){let t=Rt(e.scope),r=Ln(e.events),n=new Set,s=new Set,o=new Map,a=new Map(Or({events:e.events,scope:t,sourceOrigin:e.sourceOrigin}).map(f=>[`${f.kind}|${f.url}`,f]));for(let f of r){let w=f.before,m=f.after,h=Mn(w),y=Mn(m);y&&m.url&&n.add(xe(e.sourceOrigin,m.url)),h&&w.url&&(!y||w.url!==m.url)&&s.add(xe(e.sourceOrigin,w.url));}for(let f of [...e.events].sort((w,m)=>w.sequence-m.sequence)){for(let w of [f.before,f.after])for(let m of w?.resourceDeleteUrls??[])o.set(xe(e.sourceOrigin,m),"delete");for(let w of f.after?.resourceRenderUrls??[])o.set(xe(e.sourceOrigin,w),"render");}let c=[...o].filter(([,f])=>f==="render").map(([f])=>f),i=[...o].filter(([,f])=>f==="delete").map(([f])=>f),d=new Map(e.resolvedArchiveFamilies.map(f=>[`${f.kind}|${xe(e.sourceOrigin,f.url)}`,{...f,url:xe(e.sourceOrigin,f.url),pageUrls:ze(f.pageUrls.map(w=>xe(e.sourceOrigin,w)))}])),u=[...a.entries()].map(([f,w])=>{let m=d.get(f);if(!m)throw new Error(`WordPress did not resolve archive family ${f}.`);return {...w,...m}}).sort((f,w)=>f.url.localeCompare(w.url)),l=ze(u.flatMap(f=>f.pageUrls)),g=t.includeSitemapChain?Jo(e.sitemapUrls.map(f=>xe(e.sourceOrigin,f))):[],p={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:r.length,directRenderUrls:ze([...n]),archiveFamilyUrls:l,sitemapUrls:g,deleteUrls:ze([...s]),resourceRenderUrls:ze(c),resourceDeleteUrls:ze(i),archiveFamilies:u};return {...p,impactHash:Le(p)}}function vt(e){let{impactHash:t,...r}=e;if(e.schemaVersion!==1||!t)throw new Error("Invalid content-sync impact plan schema.");if(!Array.isArray(e.resourceRenderUrls)||!Array.isArray(e.resourceDeleteUrls))throw new Error("Invalid content-sync resource impact contract.");if(Le(r)!==t)throw new Error("Content-sync impact plan hash mismatch.");for(let n of [...e.directRenderUrls,...e.deleteUrls,...e.resourceRenderUrls,...e.resourceDeleteUrls])if(Qo(n))throw new Error(`Content-sync impact contains a WordPress trash alias instead of the last public URL: ${n}`)}var ei="wp-json/smartcloud-static-publisher/v1/content-sync/",ti=25*1024*1024,qt=class extends Error{status;code;constructor(t,r,n){super(n),this.name="ContentSyncApiError",this.status=t,this.code=r;}},Wt=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,r="content-sync"){let n=this.options.sourceOrigin.replace(/\/+$/,"")+"/",s=r==="content-sync"?ei:`wp-json/smartcloud-static-publisher/v1/${r}/`;return new URL(`${s}${t}`,n)}async post(t,r,n="content-sync"){let s=this.endpoint(t,n),o=Buffer.from(JSON.stringify(r),"utf8"),a=s.protocol==="https:"?Wn:Xo,c=s.protocol==="https:"?new Wn.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((i,d)=>{let u=a.request(s,{method:"POST",agent:c,headers:{"content-type":"application/json","content-length":String(o.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},l=>{let g=[],p=0;l.on("data",f=>{if(p+=f.byteLength,p>ti){l.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(f);}),l.on("error",d),l.on("end",()=>{let f=Buffer.concat(g).toString("utf8"),w;try{w=f?JSON.parse(f):{};}catch{d(new qt(l.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let m=l.statusCode??500;if(m<200||m>=300){d(new qt(m,String(w.code??"request-failed"),String(w.message??`Content-sync API request failed (${m}).`)));return}i(w);});});u.on("timeout",()=>u.destroy(new Error(`Content-sync API request timed out: ${t}`))),u.on("error",d),u.end(o);})}async head(t){return this.post("head",t)}async unboundHead(t,r){return this.post("head",{postTypes:t,includeSubsites:r})}async events(t){let r=[],n=t.afterSequence,s=new Set(t.postTypes);for(;n<t.throughSequence;){let o=await this.post("events",{...t,afterSequence:n,limit:250});if(o.fromSequence!==n||o.throughSequence!==t.throughSequence||o.nextSequence<n||o.nextSequence>t.throughSequence)throw new Error("Content-sync API returned an inconsistent event page.");if(!Array.isArray(o.items))throw new Error("Content-sync API returned a malformed event page.");let a=n;for(let c of o.items){if(!Number.isSafeInteger(c.sequence)||c.sequence<=a||c.sequence>t.throughSequence||!Number.isSafeInteger(c.postId)||c.postId<=0||!Number.isSafeInteger(c.blogId)||c.blogId<=0||!s.has(c.postType)||typeof c.operation!="string"||!c.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");a=c.sequence;}if(o.items.length>0&&o.nextSequence!==a)throw new Error("Content-sync API event cursor does not match the final returned event.");if(r.push(...o.items),!o.hasMore)break;if(o.nextSequence===n)throw new Error("Content-sync API event pagination did not advance.");n=o.nextSequence;}if(t.throughSequence>t.afterSequence&&r.at(-1)?.sequence!==t.throughSequence)throw new Error("Content-sync API omitted the claimed journal high-water event.");return r}async resolveImpactFamilies(t,r){let n=await this.post("impact",{families:t,includeSubsites:r});return Array.isArray(n.families)?n.families:[]}async releaseFingerprint(t=false){let r=await this.post("fingerprint",{includeSubsites:t}),n=String(r.fingerprint||"").trim();if(!/^[a-f0-9]{64}$/.test(n))throw new Error("WordPress returned an invalid release fingerprint.");return n}async establishBaseline(t){return this.post("baseline",t)}async acknowledge(t){return this.post("ack",t)}async importCookieObservations(t){return this.post("cookie-observations",t,"privacy")}};function _t(e){let t=C.resolve(e);return {state:C.join(t,"content-sync-state.json"),current:C.join(t,"content-sync-current.json"),impact:C.join(t,"content-sync-impact-plan.json"),checkpoint:C.join(t,"content-sync-checkpoint.json"),baseline:C.join(t,"content-sync-baseline.json"),activeRules:C.join(t,"content-sync-active-rules.json"),releaseCutoff:C.join(t,"content-sync-release-cutoff.json"),candidateManifest:C.join(t,"content-sync-candidate-manifest.json"),trustedManifest:C.join(t,"crawl-manifest.json"),invalidation:C.join(t,"content-sync-invalidation.json"),deployPlan:C.join(t,"deploy-plan.json")}}async function Mr(e){let t=await Ct(e.releaseCutoff);if(t===null)return {schemaVersion:1,updatedAt:"",entries:{}};let r=t;if(r.schemaVersion!==1||typeof r.updatedAt!="string"||!r.entries||typeof r.entries!="object"||Array.isArray(r.entries))throw new Error("Invalid content-sync release-cutoff store.");return r}async function _n(e,t){await ct(e.releaseCutoff,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function ct(e,t){await D.mkdir(C.dirname(e),{recursive:true});let r=`${e}.${process.pid}.${Date.now()}.tmp`;await D.writeFile(r,JSON.stringify(t,null,2),"utf8"),await D.rename(r,e);}async function Ct(e){try{return JSON.parse(await D.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 jr(e){let t=await Ct(e.baseline);if(t===null)return {schemaVersion:1,updatedAt:"",entries:{}};let r=t;if(r.schemaVersion!==1||typeof r.updatedAt!="string"||!r.entries||typeof r.entries!="object"||Array.isArray(r.entries))throw new Error("Invalid content-sync baseline store.");return r}async function Nn(e,t){await ct(e.baseline,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Dr(e){let t=await Ct(e.current);if(t===null)return null;let r=t;if(r.schemaVersion!==1||typeof r.jobId!="string"||typeof r.phase!="string"||!Number.isSafeInteger(r.fromSequence)||!Number.isSafeInteger(r.toSequence))throw new Error("Invalid content-sync current-range state.");return r}async function Bn(e,t){await ct(e.current,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Hn(e){let t=await Ct(e.impact);if(t===null)return null;let r=t;return vt(r),r}async function Vn(e,t){vt(t),await ct(e.impact,t);}async function Kn(e,t){await ct(e.checkpoint,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Nt(e){let t=await Ct(e.state);if(t===null)return {schemaVersion:1,updatedAt:"",rules:{}};let r=t;if(r.schemaVersion!==1||typeof r.updatedAt!="string"||!r.rules||typeof r.rules!="object"||Array.isArray(r.rules))throw new Error("Invalid content-sync state store.");return r}async function Bt(e,t){await ct(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Fr(e){await Promise.all([e.current,e.checkpoint].map(async t=>{try{await D.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}));}async function zn(e,t){let r=await Dr(e);return !r||r.coalesceKey!==t?false:(await Promise.all([e.current,e.checkpoint,e.impact,e.candidateManifest,e.invalidation,e.deployPlan].map(async n=>{try{await D.unlink(n);}catch(s){if(s.code!=="ENOENT")throw s}})),true)}var Ur=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function kt(e){let t=new Set;for(let r of [...e,...Ur]){let n=String(r||"").trim();n&&t.add(n);}return [...t]}function Lr(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function Jn(e){if(!e||!/^https?:\/\//i.test(e))return false;try{return Lr(new URL(e))}catch{return false}}function Gn(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function oi(e){let t=String(e??"").trim();if(!t)return "";let r=t;if(/^https?:\/\//i.test(r))try{r=new URL(r).pathname;}catch{return ""}if(r=r.split(/[?#]/,1)[0]?.replace(/\\/g,"/").trim()??"",!r)return "";let n=r.endsWith("/"),s=r.replace(/^\/+/,"").split("/").map(a=>a.replace(/[^A-Za-z0-9._-]/g,"")).filter(a=>a.length>0&&a!=="."&&a!=="..");if(s.length===0)return "";let o=`/${s.join("/")}`;return n&&C.extname(o)===""&&(o+="/"),o==="/"?"":o}function qr(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,r])=>[t.trim(),String(r??"")]).filter(([t])=>t.length>0))}function ii(e){if(!e||typeof e!="object")return {};let t={};for(let[r,n]of Object.entries(e)){let s=r.trim();if(!s||!n||typeof n!="object")continue;let o=n,a={};if(typeof o.targetOrigin=="string"){let i=Gn(o.targetOrigin);i&&(a.targetOrigin=i);}let c=qr(o.extraReplacements);if(Object.keys(c).length>0&&(a.extraReplacements=c),o.s3&&typeof o.s3=="object"){let i={},d=o.s3;typeof d.bucket=="string"&&(i.bucket=d.bucket.trim()),typeof d.prefix=="string"&&(i.prefix=d.prefix.trim()),typeof d.region=="string"&&(i.region=d.region.trim()),typeof d.htmlCacheControl=="string"&&(i.htmlCacheControl=d.htmlCacheControl.trim()),typeof d.assetCacheControl=="string"&&(i.assetCacheControl=d.assetCacheControl.trim()),Object.keys(i).length>0&&(a.s3=i);}if(o.cloudFront&&typeof o.cloudFront=="object"){let i={},d=o.cloudFront;typeof d.distributionId=="string"&&(i.distributionId=d.distributionId.trim()),Array.isArray(d.invalidationPaths)&&(i.invalidationPaths=d.invalidationPaths.map(u=>String(u??"").trim()).filter(u=>u.length>0)),Object.keys(i).length>0&&(a.cloudFront=i);}t[s]=a;}return t}function _r(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function ai(e,t){if(!e||typeof e!="object")return null;let r=e,n=r.command;if(n!=="publish"&&n!=="crawl"&&n!=="deploy"&&n!=="invalidate"&&n!=="retry-timeouts"&&n!=="url"&&n!=="content-sync")return null;let s=Number.parseInt(String(r.intervalMinutes??"0"),10);if(!Number.isFinite(s)||s<1)return null;let o=String(r.id??`${n}-${t+1}`).trim();if(!o)return null;let a=String(r.deploymentProfile??"").trim(),c=String(r.url??"").trim(),i=Array.isArray(r.postTypes)?[...new Set(r.postTypes.map(u=>String(u??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],d=Array.isArray(r.listingPaths)?[...new Set(r.listingPaths.map(u=>String(u??"").trim()).filter(Boolean))]:[];return n==="content-sync"&&i.length===0?null:{id:o,enabled:r.enabled!==false,command:n,intervalMinutes:s,...n==="publish"||n==="crawl"?{crawlMode:r.crawlMode==="incremental"?"incremental":"full"}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&a?{deploymentProfile:a}:{},...c?{url:c}:{},...i.length>0?{postTypes:i}:{},...d.length>0?{listingPaths:d}:{},...typeof r.includeSubsites=="boolean"?{includeSubsites:r.includeSubsites}:{},...typeof r.includePostTypeArchives=="boolean"?{includePostTypeArchives:r.includePostTypeArchives}:{},...typeof r.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:r.includeTaxonomyArchives}:{},...typeof r.includeAuthorArchives=="boolean"?{includeAuthorArchives:r.includeAuthorArchives}:{},...typeof r.includeDateArchives=="boolean"?{includeDateArchives:r.includeDateArchives}:{},...typeof r.includePostsPage=="boolean"?{includePostsPage:r.includePostsPage}:{},...typeof r.includeSitemapChain=="boolean"?{includeSitemapChain:r.includeSitemapChain}:{}}}function Zn(e){let t=e&&typeof e=="object"?e:{},r=Array.isArray(t.rules)?t.rules.map((n,s)=>ai(n,s)).filter(n=>!!n):[];return {enabled:t.enabled===true,timezone:typeof t.timezone=="string"&&t.timezone.trim()!==""?t.timezone.trim():"UTC",rules:r}}function ci(e){let t=e&&typeof e=="object"?e:{},r=t.render&&typeof t.render=="object"?t.render:{},n=Number(r.concurrency??0),s=Number(r.batchSize??0),o=Number(r.maxAttempts??0),a=t.rewrite&&typeof t.rewrite=="object"?t.rewrite:{},c=t.deploy&&typeof t.deploy=="object"?t.deploy:{},i=(d,u)=>({enabled:typeof d.enabled=="boolean"?d.enabled:r.enabled===true,concurrency:Math.min(100,Math.max(1,Number(d.concurrency||u.concurrency))),batchSize:Math.min(500,Math.max(1,Number(d.batchSize||u.batchSize))),maxAttempts:Math.min(5,Math.max(1,Number(d.maxAttempts||2)))});return {render:{enabled:r.enabled===true,...Number.isInteger(n)&&n>0?{concurrency:n}:{},batchSize:Number.isInteger(s)&&s>0?Math.min(20,s):5,...Number.isInteger(o)&&o>0?{maxAttempts:Math.min(5,o)}:{}},rewrite:i(a,{concurrency:8,batchSize:25}),deploy:i(c,{concurrency:8,batchSize:50})}}function li(e){if(!e||typeof e!="object")return;let t=e,r={};for(let s of ["accountId","siteId"]){let o=t[s];typeof o=="string"&&o.trim()!==""&&(r[s]=o.trim());}let n=Number(t.lastUpdate??0);return Number.isFinite(n)&&n>0&&(r.lastUpdate=Math.floor(n)),t.subscriber===true&&(r.subscriber=true),Object.keys(r).length>0?r:void 0}function Nr(e){let t=e&&typeof e=="object"?e:{},r=String(t.accountId??"").trim(),n=String(t.siteId??"").trim(),s=t.subscriber===true,o=li(t.siteSettings),a={...o??{},...r?{accountId:o?.accountId??r}:{},...n?{siteId:o?.siteId??n}:{},...o?.subscriber===true||s?{subscriber:true}:{}},c={},i=String(t.apiBase??"").trim();i&&(c.apiBase=i);let d=String(t.runtimeToken??t.nonce??"").trim();d&&(c.runtimeToken=d);let u=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();u&&(c.virtualAssetBaseUrl=u),Object.keys(a).length>0&&(c.siteSettings=a);let l=_r(t.subscriptionType);return l&&(c.subscriptionType=l),c}function ui(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function Yn(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function di(e,t){let r=new Headers(e instanceof Request?e.headers:void 0);if(t?.headers)for(let[n,s]of new Headers(t.headers).entries())r.set(n,s);return r}async function Xn(e,t,r=5){let n=new URL(Yn(e)),s=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),o=n.protocol==="http:"?Xo:Wn,a=di(e,t);return await new Promise((c,i)=>{let d=o.request(n,{method:s,headers:Object.fromEntries(a.entries()),...n.protocol==="https:"?{rejectUnauthorized:false}:{}},u=>{let l=[];u.on("data",g=>{l.push(Buffer.isBuffer(g)?g:Buffer.from(g));}),u.on("error",i),u.on("end",()=>{let g=u.statusCode??0,p=u.headers.location;if(r>0&&p&&[301,302,303,307,308].includes(g)){c(Xn(new URL(p,n),{method:g===303?"GET":s},r-1));return}let f=new Headers;for(let[w,m]of Object.entries(u.headers))Array.isArray(m)?f.set(w,m.join(", ")):typeof m=="string"&&f.set(w,m);c(new Response(Buffer.concat(l),{status:g,statusText:u.statusMessage??"",headers:f}));});});d.on("error",i),d.end();})}function pi(e){let t=Nr(e.wpsuite),r={...t.siteSettings??{},...t.siteSettings?.subscriber===true||t.subscriptionType==="PROFESSIONAL"||t.subscriptionType==="AGENCY"?{subscriber:true}:{}},n=String(r.accountId??"").trim(),s=String(r.siteId??"").trim(),o=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();if(e.wpsuite={...t.apiBase?{apiBase:t.apiBase}:{},...t.runtimeToken?{runtimeToken:t.runtimeToken}:{},...o?{virtualAssetBaseUrl:o}:{},...Object.keys(r).length>0?{siteSettings:r}:{},...t.subscriptionType?{subscriptionType:t.subscriptionType}:{}},!n||!s||!o)return false;ui(e.sourceOrigin);let a=globalThis,i={...a.WpSuite??{},siteSettings:r,uploadUrl:o};return t.apiBase&&(i.apiBase=t.apiBase),a.WpSuite=i,true}async function mi(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!pi(e)){let s=e.wpsuite?.siteSettings,o=[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): ${o.join(", ")}.`,requests:[]}}}let r=globalThis.fetch,n=[];try{typeof r=="function"&&/^https?:\/\//i.test(t)&&(globalThis.fetch=(async(i,d)=>{let u=Yn(i);if(u.startsWith(t)){let l=(()=>{try{return new URL(u).pathname}catch{return u}})();try{let g=e.ignoreHttpsErrors&&u.startsWith("https://")?await Xn(i,d):await r(i,d);return n.push({path:l,status:g.status,ok:g.ok,error:null}),g}catch(g){throw n.push({path:l,status:null,ok:!1,error:g instanceof Error?g.message:String(g)}),g}}return r(i,d)}));let{getConfig:s}=await import('@smart-cloud/wpsuite-core'),o=await s("publisher"),a=o&&typeof o=="object"?o:null,c=_r(a?.subscriptionType);return {remote:a,diagnostic:{status:a?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:c??null,error:a?null:"WP Suite publisher config loader returned no configuration.",requests:n}}}catch(s){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:t,subscriptionType:null,error:s instanceof Error?s.message:String(s),requests:n}}}finally{typeof r=="function"&&(globalThis.fetch=r);}}function gi(e,t){let r=ii(t?.deploymentProfiles),n=String(t?.defaultDeploymentProfile??"").trim(),s=r[n]?n:"",o=_r(t?.subscriptionType),a=Nr(e.wpsuite);return {...e,scheduler:Zn(t?.scheduler),deploymentProfiles:r,defaultDeploymentProfile:s,...o?{subscriptionType:o}:{},wpsuite:{...a,...a.siteSettings||o?{siteSettings:{...a.siteSettings??{},...a.siteSettings?.subscriber===true||o==="PROFESSIONAL"||o==="AGENCY"?{subscriber:true}:{}}}:{},...o?{subscriptionType:o}:{}}}}function Wr(e,t){let n=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!n)return t;let s=n.split("/").map(o=>o.trim()).filter(o=>o.length>0&&o!=="."&&o!=="..");return s.length>0?s.join("/"):t}function fi(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?C.resolve(e):""}function Qn(e,t,r){let n=String(t||"").trim();return n&&C.isAbsolute(n)?C.resolve(n):C.resolve(e,Wr(n,r))}async function Ie(e,t={}){let r=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",n=await D.readFile(r,"utf8"),s=JSON.parse(n);s.sourceOrigin=s.sourceOrigin.replace(/\/$/,""),s.targetOrigin=Gn(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=oi(s.generated404RequestPath),s.sitemapPaths||=[...Ur],s.allowedAssetHosts||=[],s.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],s.blockedPathPrefixes||=[],s.blockedSearchFragments||=[],s.pageCache={purgeBeforeCrawl:s.pageCache?.purgeBeforeCrawl===true};let o=Number(s.processingConcurrency)>0?Math.min(100,Math.max(1,Number(s.processingConcurrency))):null;s.concurrency=o??s.concurrency??1,s.maxPages||=0,s.extraReplacements=qr(s.extraReplacements),s.postCrawlCopyMap=qr(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=o??(Number(s.assetDownloadConcurrency)>0?Number(s.assetDownloadConcurrency):s.concurrency),s.rewriteConcurrency=o??(Number(s.rewriteConcurrency)>0?Number(s.rewriteConcurrency):s.assetDownloadConcurrency),s.wpsuite=Nr(s.wpsuite),s.scheduler=Zn(void 0),s.remoteWorkers=ci(s.remoteWorkers),typeof s.lambdaDelegationEnabled=="boolean"&&(s.remoteWorkers.render??={},s.remoteWorkers.rewrite??={},s.remoteWorkers.deploy??={},s.remoteWorkers.render.enabled=s.lambdaDelegationEnabled,s.remoteWorkers.rewrite.enabled=s.lambdaDelegationEnabled,s.remoteWorkers.deploy.enabled=s.lambdaDelegationEnabled),o!==null&&(s.processingConcurrency=o,s.remoteWorkers.render??={},s.remoteWorkers.rewrite??={},s.remoteWorkers.deploy??={},s.remoteWorkers.render.concurrency=o,s.remoteWorkers.rewrite.concurrency=o,s.remoteWorkers.deploy.concurrency=o),s.deploymentProfiles={},s.defaultDeploymentProfile="",s.deploymentTargetOverride=String(s.deploymentTargetOverride??"").trim();let a=await mi(s);t.onRemotePublisherConfigDiagnostic?.(a.diagnostic);let c=gi(s,a.remote),i=fi(),d=i?C.resolve(i,".."):"";return d?(c.outputDir=Qn(d,c.outputDir,"export"),c.logDir=Qn(d,c.logDir,"logs")):(C.isAbsolute(c.outputDir)||(c.outputDir=Wr(c.outputDir,"export")),C.isAbsolute(c.logDir)||(c.logDir=Wr(c.logDir,"logs"))),c}async function es(e){await e.addInitScript(()=>{let t=window,r=[],n=new WeakSet,s=new WeakSet;Object.defineProperty(t,"__WPSUITE_STATIC_SERIALIZATION_STATE__",{value:{snapshots:r,runtimeCreatedElements:s},configurable:true});let o=Document.prototype.createElement;Document.prototype.createElement=function(g,p){let f=o.call(this,g,p);return s.add(f),f};let a=l=>{!(l instanceof Element)||n.has(l)||(n.add(l),r.push({host:l,pristine:l.cloneNode(true),sourceOwned:!s.has(l)}));},c=Element.prototype.attachShadow;Element.prototype.attachShadow=function(g){return a(this),c.call(this,g)};let i=new WeakSet,d=l=>{if(typeof l!="object"&&typeof l!="function"||l===null||i.has(l))return l;let g=l;if(typeof g.createRoot!="function")return l;let p=g.createRoot,f=function(w,...m){return a(w),Reflect.apply(p,this,[w,...m])};try{Object.defineProperty(g,"createRoot",{...Object.getOwnPropertyDescriptor(g,"createRoot"),value:f}),i.add(l);}catch{}return l},u=l=>{let g=d(t[l]);try{Object.defineProperty(t,l,{configurable:!0,enumerable:!0,get:()=>g,set:p=>{g=d(p);}});}catch{d(t[l]);}};u("ReactDOM"),u("ReactDOMClient");});}async function Br(e){return e.evaluate(()=>{let t=window.__WPSUITE_STATIC_SERIALIZATION_STATE__;document.querySelectorAll("script, style, link, iframe").forEach(n=>{t?.runtimeCreatedElements.has(n)&&n.remove();});for(let n of t?.snapshots??[])n.host.isConnected&&(n.sourceOwned?n.host.replaceWith(n.pristine.cloneNode(true)):n.host.remove());return document.querySelectorAll("#wpsuite-consent-root, .grecaptcha-badge, iframe[src*='/recaptcha/']").forEach(n=>n.remove()),document.documentElement.classList.remove("wpsuite-consent-scroll-locked"),document.body.classList.remove("wpsuite-consent-scroll-locked"),`${document.doctype?new XMLSerializer().serializeToString(document.doctype):""}${document.documentElement.outerHTML}`})}var hi=".deploy-plan.json",wi="deploy-plan.json",yi=".crawl-incomplete.json",bi="crawl-incomplete.json";function ts(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?C.resolve(e):""}function rs(e){let t=String(e||"").replace(/\\/g,"/").replace(/^\/+/,"").trim(),r=C.posix.normalize(t);return !t||!r||r==="."||/^[a-zA-Z]:/.test(r)||r.startsWith("../")||r===".."?null:r}function Si(e){let t=String(e||"").trim();if(!t.startsWith("/")||t.startsWith("//")||t.includes("*")||t.includes("#"))return null;try{let r=new URL(t,"https://content-sync.invalid");return r.origin!=="https://content-sync.invalid"?null:`${C.posix.normalize(r.pathname)}${r.search}`}catch{return null}}function Te(e){return [...new Set(e.map(rs).filter(t=>t!==null))]}function ns(e,t){let r={generatedAt:String(t.generatedAt||new Date().toISOString()),outputDir:C.resolve(e.outputDir),runMode:t.runMode,crawlMode:t.crawlMode,fullSyncRequired:!!t.fullSyncRequired,changedFiles:Te(t.changedFiles||[]),deletedFiles:Te(t.deletedFiles||[]),rewriteTargets:Te(t.rewriteTargets||[]),revalidateFiles:Te(t.revalidateFiles||[])};return t.schemaVersion===2?{...r,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(n=>String(n||"").trim()).filter(n=>{try{let s=new URL(n);return s.protocol==="http:"||s.protocol==="https:"}catch{return false}}))],uploadGroups:{assets:Te(t.uploadGroups?.assets||[]),content:Te(t.uploadGroups?.content||[]),listings:Te(t.uploadGroups?.listings||[]),resources:Te(t.uploadGroups?.resources||[]),sitemapLeaves:Te(t.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:Te(t.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((t.invalidationPaths||[]).map(Si).filter(n=>n!==null))].sort()}:{schemaVersion:1,...r,runMode:t.runMode}}function Pi(e){if(!e||typeof e!="object")return false;let t=e;if(t.schemaVersion===2){let r=t;return r.runMode==="content-sync"&&r.crawlMode==="incremental"&&r.fullSyncRequired===false&&typeof r.generatedAt=="string"&&typeof r.outputDir=="string"&&typeof r.consumerId=="string"&&r.consumerId.trim()!==""&&Number.isSafeInteger(r.fromSequence)&&Number.isSafeInteger(r.toSequence)&&Number(r.toSequence)>=Number(r.fromSequence)&&typeof r.impactHash=="string"&&/^[a-f0-9]{64}$/.test(r.impactHash)&&Array.isArray(r.resolvedSitemapUrls)&&Array.isArray(r.changedFiles)&&Array.isArray(r.deletedFiles)&&Array.isArray(r.rewriteTargets)&&(r.revalidateFiles===void 0||Array.isArray(r.revalidateFiles))&&!!r.uploadGroups&&Array.isArray(r.uploadGroups.assets)&&Array.isArray(r.uploadGroups.content)&&Array.isArray(r.uploadGroups.listings)&&Array.isArray(r.uploadGroups.resources)&&Array.isArray(r.uploadGroups.sitemapLeaves)&&Array.isArray(r.uploadGroups.sitemapIndexes)&&Array.isArray(r.invalidationPaths)}return t.schemaVersion===1&&typeof t.generatedAt=="string"&&typeof t.outputDir=="string"&&(t.runMode==="full"||t.runMode==="retry-timeouts"||t.runMode==="single-url")&&(t.crawlMode==="full"||t.crawlMode==="incremental")&&typeof t.fullSyncRequired=="boolean"&&Array.isArray(t.changedFiles)&&Array.isArray(t.deletedFiles)&&Array.isArray(t.rewriteTargets)&&(t.revalidateFiles===void 0||Array.isArray(t.revalidateFiles))}function Hr(e){let t=C.join(C.resolve(e.outputDir),hi),r=ts();return r?[C.join(r,wi),t]:[t]}function Vr(e){let t=C.join(C.resolve(e.outputDir),yi),r=ts();return r?[C.join(r,bi),t]:[t]}function me(e,t){let r=C.resolve(e),n=C.resolve(t),s=C.relative(r,n).replace(/\\/g,"/");return rs(s)}async function lt(e){let t=C.resolve(e.outputDir);for(let r of Hr(e))try{let n=await D.readFile(r,"utf8"),s=JSON.parse(n);if(!Pi(s)||C.resolve(s.outputDir)!==t)continue;return ns(e,s)}catch{}return null}async function Kr(e,t){let r=ns(e,t);for(let n of Hr(e)){await D.mkdir(C.dirname(n),{recursive:true});let s=`${n}.${process.pid}.${Date.now()}.tmp`;await D.writeFile(s,JSON.stringify(r,null,2),"utf8"),await D.rename(s,n);}}async function Qe(e){for(let t of Hr(e))try{await D.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}async function zr(e,t){let r={schemaVersion:1,startedAt:String(t.startedAt||new Date().toISOString()),outputDir:C.resolve(e.outputDir)};for(let n of Vr(e)){await D.mkdir(C.dirname(n),{recursive:true});let s=`${n}.${process.pid}.tmp`;await D.writeFile(s,JSON.stringify(r,null,2),"utf8"),await D.rename(s,n);}}async function ss(e){let t=C.resolve(e.outputDir);for(let r of Vr(e)){let n;try{n=await D.readFile(r,"utf8");}catch(o){if(o.code==="ENOENT")continue;throw o}let s;try{s=JSON.parse(n);}catch{throw new Error(`Invalid incomplete crawl marker: ${r}`)}if(s.schemaVersion!==1||typeof s.startedAt!="string"||typeof s.outputDir!="string"||C.resolve(s.outputDir)!==t)throw new Error(`Invalid incomplete crawl marker: ${r}`);return {schemaVersion:1,startedAt:s.startedAt,outputDir:t}}return null}async function os(e){for(let t of Vr(e))try{await D.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}function Ee(e,t=10){return cs.createHash("sha1").update(e).digest("hex").slice(0,t)}async function Ri(e){await D.mkdir(C.dirname(e),{recursive:true});}async function We(e,t,r){await Ri(e);let n=C.join(C.dirname(e),`.${C.basename(e)}.${process.pid}.${Date.now()}.${cs.randomBytes(6).toString("hex")}.tmp`);try{typeof t=="string"?await D.writeFile(n,t,r??"utf8"):await D.writeFile(n,t),await D.rename(n,e);}catch(s){throw await D.unlink(n).catch(()=>{}),s}}function $e(e){return [".html",".htm",".css",".js",".mjs",".json",".xml",".xsl",".txt",".svg",".map",".enc",".jws"].includes(C.extname(e).toLowerCase())}function _e(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 ut(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function Ht(e){return e.replace(/\//g,"\\/")}function is(e){return e.replace(/\//g,"\\\\/")}function vi(e){try{let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")return null;let r=t.pathname==="/"&&!t.search&&!t.hash?"":`${t.pathname}${t.search}${t.hash}`;return `//${t.host}${r}`}catch{return null}}function as(e,t,r){if(!t)return;e[t]=r;let n=Ht(t),s=Ht(r);e[n]=s;let o=is(t),a=is(r);e[o]=a;let c=ut(t),i=ut(r);e[c]=i;let d=ut(n),u=ut(s);e[d]=u;let l=ut(o),g=ut(a);e[l]=g;}function Vt(e,t,r){as(e,t,r);let n=vi(t);n&&n!==t&&as(e,n,r);}function Jr(e,t){let r=t.startsWith("/")?t:`/${t}`,n=(e||"").replace(/\/$/,"");return !n||n==="."||n==="/"?r:`${n}${r}`}function ls(e,t){try{return new URL(e,t==="."?"https://relative.invalid":t).pathname}catch{return e.startsWith("/")?e:`/${e.replace(/^\.\//,"")}`}}function us(e,t,r){if(!t)return r;let n=C.dirname(C.resolve(t)),s=C.resolve(e,r.replace(/^\/+/,"")),o=C.relative(n,s).replace(/\\/g,"/");return o?(o.startsWith(".")||(o=`./${o}`),o):"."}async function Zr(e,t){return (t?[...new Set(t)].map(n=>C.resolve(n)).filter(n=>n.startsWith(C.resolve(e.outputDir))).map(n=>C.relative(e.outputDir,n).replace(/\\/g,"/")).filter(n=>n.length>0):await Ci(["**/*"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(n=>n!=="asset-map.json"&&$e(n))}var Ti=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],Ai=new Set([".html",".htm"]),ds="WPSuite.io Static Publisher",Ei=ds.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function $i(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function Oi(e){return e.startsWith("//")||e.startsWith("\\/\\/")||e.startsWith("\\\\/\\\\/")}function Mi(e,t,r){return Oi(t)?e.replace(new RegExp(`(?<!:)${$i(t)}`,"g"),r):e.split(t).join(r)}function ji(e,t,r){if(e.wpsuite?.siteSettings?.subscriber===true||e.wpsuite?.subscriptionType==="PROFESSIONAL"||e.wpsuite?.subscriptionType==="AGENCY"||!r)return false;let s=C.extname(r).toLowerCase();return Ai.has(s)?/<head\b|<html\b|<!doctype html/i.test(t):false}function Di(e){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${Ei}\\2)[^>]*\\/?>`,"i").test(e))return e;let t=`<meta name="generator" content="${ds}" />`;if(e.match(/(\r?\n)([ \t]*)<\/head>/i))return e.replace(/(\r?\n)([ \t]*)<\/head>/i,`$1$2${t}$1$2</head>`);let n=e.includes(`\r
1
+ import F,{readFile}from'fs/promises';import Xo from'http';import Nn from'https';import k from'path';import us,{createHash}from'crypto';import {request,chromium}from'playwright';import {gunzipSync}from'zlib';import {setTimeout as setTimeout$1}from'timers/promises';import Ci from'fast-glob';import js from'mime-types';import {availableParallelism}from'os';import {Worker}from'worker_threads';import {LambdaClient,InvokeCommand}from'@aws-sdk/client-lambda';import {DynamoDBClient,GetItemCommand}from'@aws-sdk/client-dynamodb';import {S3Client,GetObjectCommand,PutObjectCommand,CopyObjectCommand,ListObjectsV2Command,HeadObjectCommand,DeleteObjectsCommand}from'@aws-sdk/client-s3';import {fromTemporaryCredentials}from'@aws-sdk/credential-providers';import {Upload}from'@aws-sdk/lib-storage';import {CloudFrontClient,waitUntilInvalidationCompleted,CreateInvalidationCommand}from'@aws-sdk/client-cloudfront';var zo=new Set(["post-type","taxonomy","author","date","posts-page","listing"]);function Je(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function Jo(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))]}function Qo(e){return new URL(e).pathname.split("/").filter(Boolean).some(r=>{try{return decodeURIComponent(r).endsWith("__trashed")}catch{return r.endsWith("__trashed")}})}function Mr(e){return Array.isArray(e)?e.map(Mr):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[r])=>t.localeCompare(r)).map(([t,r])=>[t,Mr(r)])):e}function Go(e){return JSON.stringify(Mr(e))}function We(e){return createHash("sha256").update(Go(e)).digest("hex")}function Dn(e){return We({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 jr(e){let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")throw new Error(`Unsupported source origin protocol: ${t.protocol}`);return t.hash="",t.search="",t.pathname=t.pathname.replace(/\/+$/,""),t.toString().replace(/\/$/,"")}function Ie(e,t){Zo(t);let r=new URL(jr(e)),n=new URL(t,`${r.toString().replace(/\/$/,"")}/`);if(n.origin!==r.origin)throw new Error(`Content-sync URL is outside the source origin: ${t}`);let s=r.pathname.replace(/\/+$/,"");if(s&&n.pathname!==s&&!n.pathname.startsWith(`${s}/`))throw new Error(`Content-sync URL is outside the source site path: ${t}`);return n.hash="",n.toString()}function Zo(e){let r=String(e).split(/[?#]/,1)[0];for(let s=0;s<3;s+=1)try{let o=decodeURIComponent(r);if(o===r)break;r=o;}catch{throw new Error(`Content-sync URL has malformed encoding: ${e}`)}if(r.includes("\0")||r.includes("\\"))throw new Error(`Content-sync URL has an unsafe path: ${e}`);if(r.replace(/^[a-z][a-z0-9+.-]*:\/\/[^/]*/i,"").split("/").some(s=>s==="."||s===".."))throw new Error(`Content-sync URL has an unsafe path: ${e}`)}function Rt(e){let t=Je((e.postTypes??[]).map(n=>String(n).toLowerCase().replace(/[^a-z0-9_-]/g,"")));if(t.length===0)throw new Error("Content-sync requires at least one public post type.");let r=Je((e.listingPaths??[]).map(n=>{let s=String(n).trim();if(!s)return "";let o=new URL(s,"https://content-sync.invalid/");if(o.origin!=="https://content-sync.invalid"&&/^https?:\/\//i.test(s))throw new Error("Content-sync listing routes must be same-site paths.");let a=`/${o.pathname.replace(/^\/+|\/+$/g,"")}`;return a==="/"?"/":`${a}/`}));return {postTypes:t,listingPaths:r,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 Un(e,t,r){return We({schemaVersion:1,sourceOrigin:jr(e),ruleId:t.trim(),scope:Rt(r)})}function Ln(e){return We({schemaVersion:1,...e})}function Wn(e){let t=String(e.siteId??"").trim()||String(e.accountId??"").trim()||jr(e.sourceOrigin);return `content-sync:${We({siteIdentity:t,ruleId:e.ruleId.trim(),targetFingerprint:e.targetFingerprint}).slice(0,48)}`}function qn(e){let t=[...e].sort((s,o)=>s.sequence-o.sequence),r=new Map,n=-1;for(let s of t){if(!Number.isSafeInteger(s.sequence)||s.sequence<=n)throw new Error("Content-sync journal events are not strictly ordered.");if(n=s.sequence,!Number.isSafeInteger(s.blogId)||s.blogId<1)throw new Error("Content-sync journal event has an invalid multisite identity.");let o=`${s.blogId}:${s.postType}:${s.postId}`,a=r.get(o);if(!a){r.set(o,{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 [...r.values()].sort((s,o)=>s.fromSequence-o.fromSequence)}function Fn(e){return e?.status==="publish"&&!!e.url}function Yo(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 Fr(e){let t=Rt(e.scope),r=new Map;qn(e.events);for(let n of e.events)for(let s of [n.before,n.after])for(let o of s?.archiveFamilies??[]){if(!zo.has(o.kind)||!Yo(o.kind,t))continue;let a={...o,url:Ie(e.sourceOrigin,o.url)};r.set(`${a.kind}|${a.url}`,a);}for(let n of t.listingPaths){let s=Ie(e.sourceOrigin,n);r.set(`listing|${s}`,{kind:"listing",url:s});}return [...r.values()].sort((n,s)=>n.url.localeCompare(s.url))}function _n(e){let t=Rt(e.scope),r=qn(e.events),n=new Set,s=new Set,o=new Map,a=new Map(Fr({events:e.events,scope:t,sourceOrigin:e.sourceOrigin}).map(f=>[`${f.kind}|${f.url}`,f]));for(let f of r){let w=f.before,m=f.after,h=Fn(w),y=Fn(m);y&&m.url&&n.add(Ie(e.sourceOrigin,m.url)),h&&w.url&&(!y||w.url!==m.url)&&s.add(Ie(e.sourceOrigin,w.url));}for(let f of [...e.events].sort((w,m)=>w.sequence-m.sequence)){for(let w of [f.before,f.after])for(let m of w?.resourceDeleteUrls??[])o.set(Ie(e.sourceOrigin,m),"delete");for(let w of f.after?.resourceRenderUrls??[])o.set(Ie(e.sourceOrigin,w),"render");}let c=[...o].filter(([,f])=>f==="render").map(([f])=>f),i=[...o].filter(([,f])=>f==="delete").map(([f])=>f),d=new Map(e.resolvedArchiveFamilies.map(f=>[`${f.kind}|${Ie(e.sourceOrigin,f.url)}`,{...f,url:Ie(e.sourceOrigin,f.url),pageUrls:Je(f.pageUrls.map(w=>Ie(e.sourceOrigin,w)))}])),u=[...a.entries()].map(([f,w])=>{let m=d.get(f);if(!m)throw new Error(`WordPress did not resolve archive family ${f}.`);return {...w,...m}}).sort((f,w)=>f.url.localeCompare(w.url)),l=Je(u.flatMap(f=>f.pageUrls)),g=t.includeSitemapChain?Jo(e.sitemapUrls.map(f=>Ie(e.sourceOrigin,f))):[],p={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:r.length,directRenderUrls:Je([...n]),archiveFamilyUrls:l,sitemapUrls:g,deleteUrls:Je([...s]),resourceRenderUrls:Je(c),resourceDeleteUrls:Je(i),archiveFamilies:u};return {...p,impactHash:We(p)}}function vt(e){let{impactHash:t,...r}=e;if(e.schemaVersion!==1||!t)throw new Error("Invalid content-sync impact plan schema.");if(!Array.isArray(e.resourceRenderUrls)||!Array.isArray(e.resourceDeleteUrls))throw new Error("Invalid content-sync resource impact contract.");if(We(r)!==t)throw new Error("Content-sync impact plan hash mismatch.");for(let n of [...e.directRenderUrls,...e.deleteUrls,...e.resourceRenderUrls,...e.resourceDeleteUrls])if(Qo(n))throw new Error(`Content-sync impact contains a WordPress trash alias instead of the last public URL: ${n}`)}var ei="wp-json/smartcloud-static-publisher/v1/content-sync/",ti=25*1024*1024,_t=class extends Error{status;code;constructor(t,r,n){super(n),this.name="ContentSyncApiError",this.status=t,this.code=r;}},Nt=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,r="content-sync"){let n=this.options.sourceOrigin.replace(/\/+$/,"")+"/",s=r==="content-sync"?ei:`wp-json/smartcloud-static-publisher/v1/${r}/`;return new URL(`${s}${t}`,n)}async post(t,r,n="content-sync"){let s=this.endpoint(t,n),o=Buffer.from(JSON.stringify(r),"utf8"),a=s.protocol==="https:"?Nn:Xo,c=s.protocol==="https:"?new Nn.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((i,d)=>{let u=a.request(s,{method:"POST",agent:c,headers:{"content-type":"application/json","content-length":String(o.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},l=>{let g=[],p=0;l.on("data",f=>{if(p+=f.byteLength,p>ti){l.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(f);}),l.on("error",d),l.on("end",()=>{let f=Buffer.concat(g).toString("utf8"),w;try{w=f?JSON.parse(f):{};}catch{d(new _t(l.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let m=l.statusCode??500;if(m<200||m>=300){d(new _t(m,String(w.code??"request-failed"),String(w.message??`Content-sync API request failed (${m}).`)));return}i(w);});});u.on("timeout",()=>u.destroy(new Error(`Content-sync API request timed out: ${t}`))),u.on("error",d),u.end(o);})}async head(t){return this.post("head",t)}async unboundHead(t,r){return this.post("head",{postTypes:t,includeSubsites:r})}async events(t){let r=[],n=t.afterSequence,s=new Set(t.postTypes);for(;n<t.throughSequence;){let o=await this.post("events",{...t,afterSequence:n,limit:250});if(o.fromSequence!==n||o.throughSequence!==t.throughSequence||o.nextSequence<n||o.nextSequence>t.throughSequence)throw new Error("Content-sync API returned an inconsistent event page.");if(!Array.isArray(o.items))throw new Error("Content-sync API returned a malformed event page.");let a=n;for(let c of o.items){if(!Number.isSafeInteger(c.sequence)||c.sequence<=a||c.sequence>t.throughSequence||!Number.isSafeInteger(c.postId)||c.postId<=0||!Number.isSafeInteger(c.blogId)||c.blogId<=0||!s.has(c.postType)||typeof c.operation!="string"||!c.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");a=c.sequence;}if(o.items.length>0&&o.nextSequence!==a)throw new Error("Content-sync API event cursor does not match the final returned event.");if(r.push(...o.items),!o.hasMore)break;if(o.nextSequence===n)throw new Error("Content-sync API event pagination did not advance.");n=o.nextSequence;}if(t.throughSequence>t.afterSequence&&r.at(-1)?.sequence!==t.throughSequence)throw new Error("Content-sync API omitted the claimed journal high-water event.");return r}async resolveImpactFamilies(t,r){let n=await this.post("impact",{families:t,includeSubsites:r});return Array.isArray(n.families)?n.families:[]}async releaseFingerprint(t=false){let r=await this.post("fingerprint",{includeSubsites:t}),n=String(r.fingerprint||"").trim();if(!/^[a-f0-9]{64}$/.test(n))throw new Error("WordPress returned an invalid release fingerprint.");return n}async establishBaseline(t){return this.post("baseline",t)}async acknowledge(t){return this.post("ack",t)}async importCookieObservations(t){return this.post("cookie-observations",t,"privacy")}};function Bt(e){let t=k.resolve(e);return {state:k.join(t,"content-sync-state.json"),current:k.join(t,"content-sync-current.json"),impact:k.join(t,"content-sync-impact-plan.json"),checkpoint:k.join(t,"content-sync-checkpoint.json"),baseline:k.join(t,"content-sync-baseline.json"),activeRules:k.join(t,"content-sync-active-rules.json"),releaseCutoff:k.join(t,"content-sync-release-cutoff.json"),candidateManifest:k.join(t,"content-sync-candidate-manifest.json"),trustedManifest:k.join(t,"crawl-manifest.json"),invalidation:k.join(t,"content-sync-invalidation.json"),deployPlan:k.join(t,"deploy-plan.json")}}async function Dr(e){let t=await Ct(e.releaseCutoff);if(t===null)return {schemaVersion:1,updatedAt:"",entries:{}};let r=t;if(r.schemaVersion!==1||typeof r.updatedAt!="string"||!r.entries||typeof r.entries!="object"||Array.isArray(r.entries))throw new Error("Invalid content-sync release-cutoff store.");return r}async function Bn(e,t){await ct(e.releaseCutoff,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function ct(e,t){await F.mkdir(k.dirname(e),{recursive:true});let r=`${e}.${process.pid}.${Date.now()}.tmp`;await F.writeFile(r,JSON.stringify(t,null,2),"utf8"),await F.rename(r,e);}async function Ct(e){try{return JSON.parse(await F.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 Ur(e){let t=await Ct(e.baseline);if(t===null)return {schemaVersion:1,updatedAt:"",entries:{}};let r=t;if(r.schemaVersion!==1||typeof r.updatedAt!="string"||!r.entries||typeof r.entries!="object"||Array.isArray(r.entries))throw new Error("Invalid content-sync baseline store.");return r}async function Hn(e,t){await ct(e.baseline,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Lr(e){let t=await Ct(e.current);if(t===null)return null;let r=t;if(r.schemaVersion!==1||typeof r.jobId!="string"||typeof r.phase!="string"||!Number.isSafeInteger(r.fromSequence)||!Number.isSafeInteger(r.toSequence))throw new Error("Invalid content-sync current-range state.");return r}async function Vn(e,t){await ct(e.current,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Kn(e){let t=await Ct(e.impact);if(t===null)return null;let r=t;return vt(r),r}async function zn(e,t){vt(t),await ct(e.impact,t);}async function Jn(e,t){await ct(e.checkpoint,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Ht(e){let t=await Ct(e.state);if(t===null)return {schemaVersion:1,updatedAt:"",rules:{}};let r=t;if(r.schemaVersion!==1||typeof r.updatedAt!="string"||!r.rules||typeof r.rules!="object"||Array.isArray(r.rules))throw new Error("Invalid content-sync state store.");return r}async function Vt(e,t){await ct(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Wr(e){await Promise.all([e.current,e.checkpoint].map(async t=>{try{await F.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}));}async function Qn(e,t){let r=await Lr(e);return !r||r.coalesceKey!==t?false:(await Promise.all([e.current,e.checkpoint,e.impact,e.candidateManifest,e.invalidation,e.deployPlan].map(async n=>{try{await F.unlink(n);}catch(s){if(s.code!=="ENOENT")throw s}})),true)}var qr=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function kt(e){let t=new Set;for(let r of [...e,...qr]){let n=String(r||"").trim();n&&t.add(n);}return [...t]}function _r(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function Gn(e){if(!e||!/^https?:\/\//i.test(e))return false;try{return _r(new URL(e))}catch{return false}}function Yn(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function oi(e){let t=String(e??"").trim();if(!t)return "";let r=t;if(/^https?:\/\//i.test(r))try{r=new URL(r).pathname;}catch{return ""}if(r=r.split(/[?#]/,1)[0]?.replace(/\\/g,"/").trim()??"",!r)return "";let n=r.endsWith("/"),s=r.replace(/^\/+/,"").split("/").map(a=>a.replace(/[^A-Za-z0-9._-]/g,"")).filter(a=>a.length>0&&a!=="."&&a!=="..");if(s.length===0)return "";let o=`/${s.join("/")}`;return n&&k.extname(o)===""&&(o+="/"),o==="/"?"":o}function Nr(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,r])=>[t.trim(),String(r??"")]).filter(([t])=>t.length>0))}function ii(e){if(!e||typeof e!="object")return {};let t={};for(let[r,n]of Object.entries(e)){let s=r.trim();if(!s||!n||typeof n!="object")continue;let o=n,a={};if(typeof o.targetOrigin=="string"){let i=Yn(o.targetOrigin);i&&(a.targetOrigin=i);}let c=Nr(o.extraReplacements);if(Object.keys(c).length>0&&(a.extraReplacements=c),o.s3&&typeof o.s3=="object"){let i={},d=o.s3;typeof d.bucket=="string"&&(i.bucket=d.bucket.trim()),typeof d.prefix=="string"&&(i.prefix=d.prefix.trim()),typeof d.region=="string"&&(i.region=d.region.trim()),typeof d.htmlCacheControl=="string"&&(i.htmlCacheControl=d.htmlCacheControl.trim()),typeof d.assetCacheControl=="string"&&(i.assetCacheControl=d.assetCacheControl.trim()),Object.keys(i).length>0&&(a.s3=i);}if(o.cloudFront&&typeof o.cloudFront=="object"){let i={},d=o.cloudFront;typeof d.distributionId=="string"&&(i.distributionId=d.distributionId.trim()),Array.isArray(d.invalidationPaths)&&(i.invalidationPaths=d.invalidationPaths.map(u=>String(u??"").trim()).filter(u=>u.length>0)),Object.keys(i).length>0&&(a.cloudFront=i);}t[s]=a;}return t}function Hr(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function ai(e,t){if(!e||typeof e!="object")return null;let r=e,n=r.command;if(n!=="publish"&&n!=="crawl"&&n!=="deploy"&&n!=="invalidate"&&n!=="retry-timeouts"&&n!=="url"&&n!=="content-sync")return null;let s=Number.parseInt(String(r.intervalMinutes??"0"),10);if(!Number.isFinite(s)||s<1)return null;let o=String(r.id??`${n}-${t+1}`).trim();if(!o)return null;let a=String(r.deploymentProfile??"").trim(),c=String(r.url??"").trim(),i=Array.isArray(r.postTypes)?[...new Set(r.postTypes.map(u=>String(u??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],d=Array.isArray(r.listingPaths)?[...new Set(r.listingPaths.map(u=>String(u??"").trim()).filter(Boolean))]:[];return n==="content-sync"&&i.length===0?null:{id:o,enabled:r.enabled!==false,command:n,intervalMinutes:s,...n==="publish"||n==="crawl"?{crawlMode:r.crawlMode==="incremental"?"incremental":"full"}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&a?{deploymentProfile:a}:{},...c?{url:c}:{},...i.length>0?{postTypes:i}:{},...d.length>0?{listingPaths:d}:{},...typeof r.includeSubsites=="boolean"?{includeSubsites:r.includeSubsites}:{},...typeof r.includePostTypeArchives=="boolean"?{includePostTypeArchives:r.includePostTypeArchives}:{},...typeof r.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:r.includeTaxonomyArchives}:{},...typeof r.includeAuthorArchives=="boolean"?{includeAuthorArchives:r.includeAuthorArchives}:{},...typeof r.includeDateArchives=="boolean"?{includeDateArchives:r.includeDateArchives}:{},...typeof r.includePostsPage=="boolean"?{includePostsPage:r.includePostsPage}:{},...typeof r.includeSitemapChain=="boolean"?{includeSitemapChain:r.includeSitemapChain}:{}}}function Xn(e){let t=e&&typeof e=="object"?e:{},r=Array.isArray(t.rules)?t.rules.map((n,s)=>ai(n,s)).filter(n=>!!n):[];return {enabled:t.enabled===true,timezone:typeof t.timezone=="string"&&t.timezone.trim()!==""?t.timezone.trim():"UTC",rules:r}}function ci(e){let t=e&&typeof e=="object"?e:{},r=t.render&&typeof t.render=="object"?t.render:{},n=Number(r.concurrency??0),s=Number(r.batchSize??0),o=Number(r.maxAttempts??0),a=t.rewrite&&typeof t.rewrite=="object"?t.rewrite:{},c=t.deploy&&typeof t.deploy=="object"?t.deploy:{},i=(d,u)=>({enabled:typeof d.enabled=="boolean"?d.enabled:r.enabled===true,concurrency:Math.min(100,Math.max(1,Number(d.concurrency||u.concurrency))),batchSize:Math.min(500,Math.max(1,Number(d.batchSize||u.batchSize))),maxAttempts:Math.min(5,Math.max(1,Number(d.maxAttempts||2)))});return {render:{enabled:r.enabled===true,...Number.isInteger(n)&&n>0?{concurrency:n}:{},batchSize:Number.isInteger(s)&&s>0?Math.min(20,s):5,...Number.isInteger(o)&&o>0?{maxAttempts:Math.min(5,o)}:{}},rewrite:i(a,{concurrency:8,batchSize:25}),deploy:i(c,{concurrency:8,batchSize:50})}}function li(e){if(!e||typeof e!="object")return;let t=e,r={};for(let s of ["accountId","siteId"]){let o=t[s];typeof o=="string"&&o.trim()!==""&&(r[s]=o.trim());}let n=Number(t.lastUpdate??0);return Number.isFinite(n)&&n>0&&(r.lastUpdate=Math.floor(n)),t.subscriber===true&&(r.subscriber=true),Object.keys(r).length>0?r:void 0}function Vr(e){let t=e&&typeof e=="object"?e:{},r=String(t.accountId??"").trim(),n=String(t.siteId??"").trim(),s=t.subscriber===true,o=li(t.siteSettings),a={...o??{},...r?{accountId:o?.accountId??r}:{},...n?{siteId:o?.siteId??n}:{},...o?.subscriber===true||s?{subscriber:true}:{}},c={},i=String(t.apiBase??"").trim();i&&(c.apiBase=i);let d=String(t.runtimeToken??t.nonce??"").trim();d&&(c.runtimeToken=d);let u=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();u&&(c.virtualAssetBaseUrl=u),Object.keys(a).length>0&&(c.siteSettings=a);let l=Hr(t.subscriptionType);return l&&(c.subscriptionType=l),c}function ui(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function es(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function di(e,t){let r=new Headers(e instanceof Request?e.headers:void 0);if(t?.headers)for(let[n,s]of new Headers(t.headers).entries())r.set(n,s);return r}async function ts(e,t,r=5){let n=new URL(es(e)),s=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),o=n.protocol==="http:"?Xo:Nn,a=di(e,t);return await new Promise((c,i)=>{let d=o.request(n,{method:s,headers:Object.fromEntries(a.entries()),...n.protocol==="https:"?{rejectUnauthorized:false}:{}},u=>{let l=[];u.on("data",g=>{l.push(Buffer.isBuffer(g)?g:Buffer.from(g));}),u.on("error",i),u.on("end",()=>{let g=u.statusCode??0,p=u.headers.location;if(r>0&&p&&[301,302,303,307,308].includes(g)){c(ts(new URL(p,n),{method:g===303?"GET":s},r-1));return}let f=new Headers;for(let[w,m]of Object.entries(u.headers))Array.isArray(m)?f.set(w,m.join(", ")):typeof m=="string"&&f.set(w,m);c(new Response(Buffer.concat(l),{status:g,statusText:u.statusMessage??"",headers:f}));});});d.on("error",i),d.end();})}function pi(e){let t=Vr(e.wpsuite),r={...t.siteSettings??{},...t.siteSettings?.subscriber===true||t.subscriptionType==="PROFESSIONAL"||t.subscriptionType==="AGENCY"?{subscriber:true}:{}},n=String(r.accountId??"").trim(),s=String(r.siteId??"").trim(),o=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();if(e.wpsuite={...t.apiBase?{apiBase:t.apiBase}:{},...t.runtimeToken?{runtimeToken:t.runtimeToken}:{},...o?{virtualAssetBaseUrl:o}:{},...Object.keys(r).length>0?{siteSettings:r}:{},...t.subscriptionType?{subscriptionType:t.subscriptionType}:{}},!n||!s||!o)return false;ui(e.sourceOrigin);let a=globalThis,i={...a.WpSuite??{},siteSettings:r,uploadUrl:o};return t.apiBase&&(i.apiBase=t.apiBase),a.WpSuite=i,true}async function mi(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!pi(e)){let s=e.wpsuite?.siteSettings,o=[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): ${o.join(", ")}.`,requests:[]}}}let r=globalThis.fetch,n=[];try{typeof r=="function"&&/^https?:\/\//i.test(t)&&(globalThis.fetch=(async(i,d)=>{let u=es(i);if(u.startsWith(t)){let l=(()=>{try{return new URL(u).pathname}catch{return u}})();try{let g=e.ignoreHttpsErrors&&u.startsWith("https://")?await ts(i,d):await r(i,d);return n.push({path:l,status:g.status,ok:g.ok,error:null}),g}catch(g){throw n.push({path:l,status:null,ok:!1,error:g instanceof Error?g.message:String(g)}),g}}return r(i,d)}));let{getConfig:s}=await import('@smart-cloud/wpsuite-core'),o=await s("publisher"),a=o&&typeof o=="object"?o:null,c=Hr(a?.subscriptionType);return {remote:a,diagnostic:{status:a?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:c??null,error:a?null:"WP Suite publisher config loader returned no configuration.",requests:n}}}catch(s){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:t,subscriptionType:null,error:s instanceof Error?s.message:String(s),requests:n}}}finally{typeof r=="function"&&(globalThis.fetch=r);}}function gi(e,t){let r=ii(t?.deploymentProfiles),n=String(t?.defaultDeploymentProfile??"").trim(),s=r[n]?n:"",o=Hr(t?.subscriptionType),a=Vr(e.wpsuite);return {...e,scheduler:Xn(t?.scheduler),deploymentProfiles:r,defaultDeploymentProfile:s,...o?{subscriptionType:o}:{},wpsuite:{...a,...a.siteSettings||o?{siteSettings:{...a.siteSettings??{},...a.siteSettings?.subscriber===true||o==="PROFESSIONAL"||o==="AGENCY"?{subscriber:true}:{}}}:{},...o?{subscriptionType:o}:{}}}}function Br(e,t){let n=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!n)return t;let s=n.split("/").map(o=>o.trim()).filter(o=>o.length>0&&o!=="."&&o!=="..");return s.length>0?s.join("/"):t}function fi(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?k.resolve(e):""}function Zn(e,t,r){let n=String(t||"").trim();return n&&k.isAbsolute(n)?k.resolve(n):k.resolve(e,Br(n,r))}async function Te(e,t={}){let r=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",n=await F.readFile(r,"utf8"),s=JSON.parse(n);s.sourceOrigin=s.sourceOrigin.replace(/\/$/,""),s.targetOrigin=Yn(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=oi(s.generated404RequestPath),s.sitemapPaths||=[...qr],s.allowedAssetHosts||=[],s.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],s.blockedPathPrefixes||=[],s.blockedSearchFragments||=[],s.pageCache={purgeBeforeCrawl:s.pageCache?.purgeBeforeCrawl===true};let o=Number(s.processingConcurrency)>0?Math.min(100,Math.max(1,Number(s.processingConcurrency))):null;s.concurrency=o??s.concurrency??1,s.maxPages||=0,s.extraReplacements=Nr(s.extraReplacements),s.postCrawlCopyMap=Nr(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=o??(Number(s.assetDownloadConcurrency)>0?Number(s.assetDownloadConcurrency):s.concurrency),s.rewriteConcurrency=o??(Number(s.rewriteConcurrency)>0?Number(s.rewriteConcurrency):s.assetDownloadConcurrency),s.wpsuite=Vr(s.wpsuite),s.scheduler=Xn(void 0),s.remoteWorkers=ci(s.remoteWorkers),typeof s.lambdaDelegationEnabled=="boolean"&&(s.remoteWorkers.render??={},s.remoteWorkers.rewrite??={},s.remoteWorkers.deploy??={},s.remoteWorkers.render.enabled=s.lambdaDelegationEnabled,s.remoteWorkers.rewrite.enabled=s.lambdaDelegationEnabled,s.remoteWorkers.deploy.enabled=s.lambdaDelegationEnabled),o!==null&&(s.processingConcurrency=o,s.remoteWorkers.render??={},s.remoteWorkers.rewrite??={},s.remoteWorkers.deploy??={},s.remoteWorkers.render.concurrency=o,s.remoteWorkers.rewrite.concurrency=o,s.remoteWorkers.deploy.concurrency=o),s.deploymentProfiles={},s.defaultDeploymentProfile="",s.deploymentTargetOverride=String(s.deploymentTargetOverride??"").trim();let a=await mi(s);t.onRemotePublisherConfigDiagnostic?.(a.diagnostic);let c=gi(s,a.remote),i=fi(),d=i?k.resolve(i,".."):"";return d?(c.outputDir=Zn(d,c.outputDir,"export"),c.logDir=Zn(d,c.logDir,"logs")):(k.isAbsolute(c.outputDir)||(c.outputDir=Br(c.outputDir,"export")),k.isAbsolute(c.logDir)||(c.logDir=Br(c.logDir,"logs"))),c}async function rs(e){await e.addInitScript(()=>{let t=window,r=[],n=new WeakSet,s=new WeakSet;Object.defineProperty(t,"__WPSUITE_STATIC_SERIALIZATION_STATE__",{value:{snapshots:r,runtimeCreatedElements:s},configurable:true});let o=Document.prototype.createElement;Document.prototype.createElement=function(g,p){let f=o.call(this,g,p);return s.add(f),f};let a=l=>{!(l instanceof Element)||n.has(l)||(n.add(l),r.push({host:l,pristine:l.cloneNode(true),sourceOwned:!s.has(l)}));},c=Element.prototype.attachShadow;Element.prototype.attachShadow=function(g){return a(this),c.call(this,g)};let i=new WeakSet,d=l=>{if(typeof l!="object"&&typeof l!="function"||l===null||i.has(l))return l;let g=l;if(typeof g.createRoot!="function")return l;let p=g.createRoot,f=function(w,...m){return a(w),Reflect.apply(p,this,[w,...m])};try{Object.defineProperty(g,"createRoot",{...Object.getOwnPropertyDescriptor(g,"createRoot"),value:f}),i.add(l);}catch{}return l},u=l=>{let g=d(t[l]);try{Object.defineProperty(t,l,{configurable:!0,enumerable:!0,get:()=>g,set:p=>{g=d(p);}});}catch{d(t[l]);}};u("ReactDOM"),u("ReactDOMClient");});}async function Kr(e){return e.evaluate(()=>{let t=window.__WPSUITE_STATIC_SERIALIZATION_STATE__,r=/^data-(?:[a-z0-9]+-)*(?:bound|initialized|mounted|hydrated)$/i;document.querySelectorAll("*").forEach(s=>{for(let o of [...s.attributes])r.test(o.name)&&s.removeAttribute(o.name);}),document.querySelectorAll("script, style, link, iframe").forEach(s=>{t?.runtimeCreatedElements.has(s)&&s.remove();});for(let s of t?.snapshots??[])s.host.isConnected&&(s.sourceOwned?s.host.replaceWith(s.pristine.cloneNode(true)):s.host.remove());return document.querySelectorAll("#wpsuite-consent-root, .grecaptcha-badge, iframe[src*='/recaptcha/']").forEach(s=>s.remove()),document.documentElement.classList.remove("wpsuite-consent-scroll-locked"),document.body.classList.remove("wpsuite-consent-scroll-locked"),`${document.doctype?new XMLSerializer().serializeToString(document.doctype):""}${document.documentElement.outerHTML}`})}var hi=".deploy-plan.json",wi="deploy-plan.json",yi=".crawl-incomplete.json",bi="crawl-incomplete.json";function ns(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?k.resolve(e):""}function ss(e){let t=String(e||"").replace(/\\/g,"/").replace(/^\/+/,"").trim(),r=k.posix.normalize(t);return !t||!r||r==="."||/^[a-zA-Z]:/.test(r)||r.startsWith("../")||r===".."?null:r}function Si(e){let t=String(e||"").trim();if(!t.startsWith("/")||t.startsWith("//")||t.includes("*")||t.includes("#"))return null;try{let r=new URL(t,"https://content-sync.invalid");return r.origin!=="https://content-sync.invalid"?null:`${k.posix.normalize(r.pathname)}${r.search}`}catch{return null}}function Ae(e){return [...new Set(e.map(ss).filter(t=>t!==null))]}function os(e,t){let r={generatedAt:String(t.generatedAt||new Date().toISOString()),outputDir:k.resolve(e.outputDir),runMode:t.runMode,crawlMode:t.crawlMode,fullSyncRequired:!!t.fullSyncRequired,changedFiles:Ae(t.changedFiles||[]),deletedFiles:Ae(t.deletedFiles||[]),rewriteTargets:Ae(t.rewriteTargets||[]),revalidateFiles:Ae(t.revalidateFiles||[])};return t.schemaVersion===2?{...r,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(n=>String(n||"").trim()).filter(n=>{try{let s=new URL(n);return s.protocol==="http:"||s.protocol==="https:"}catch{return false}}))],uploadGroups:{assets:Ae(t.uploadGroups?.assets||[]),content:Ae(t.uploadGroups?.content||[]),listings:Ae(t.uploadGroups?.listings||[]),resources:Ae(t.uploadGroups?.resources||[]),sitemapLeaves:Ae(t.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:Ae(t.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((t.invalidationPaths||[]).map(Si).filter(n=>n!==null))].sort()}:{schemaVersion:1,...r,runMode:t.runMode}}function Pi(e){if(!e||typeof e!="object")return false;let t=e;if(t.schemaVersion===2){let r=t;return r.runMode==="content-sync"&&r.crawlMode==="incremental"&&r.fullSyncRequired===false&&typeof r.generatedAt=="string"&&typeof r.outputDir=="string"&&typeof r.consumerId=="string"&&r.consumerId.trim()!==""&&Number.isSafeInteger(r.fromSequence)&&Number.isSafeInteger(r.toSequence)&&Number(r.toSequence)>=Number(r.fromSequence)&&typeof r.impactHash=="string"&&/^[a-f0-9]{64}$/.test(r.impactHash)&&Array.isArray(r.resolvedSitemapUrls)&&Array.isArray(r.changedFiles)&&Array.isArray(r.deletedFiles)&&Array.isArray(r.rewriteTargets)&&(r.revalidateFiles===void 0||Array.isArray(r.revalidateFiles))&&!!r.uploadGroups&&Array.isArray(r.uploadGroups.assets)&&Array.isArray(r.uploadGroups.content)&&Array.isArray(r.uploadGroups.listings)&&Array.isArray(r.uploadGroups.resources)&&Array.isArray(r.uploadGroups.sitemapLeaves)&&Array.isArray(r.uploadGroups.sitemapIndexes)&&Array.isArray(r.invalidationPaths)}return t.schemaVersion===1&&typeof t.generatedAt=="string"&&typeof t.outputDir=="string"&&(t.runMode==="full"||t.runMode==="retry-timeouts"||t.runMode==="single-url")&&(t.crawlMode==="full"||t.crawlMode==="incremental")&&typeof t.fullSyncRequired=="boolean"&&Array.isArray(t.changedFiles)&&Array.isArray(t.deletedFiles)&&Array.isArray(t.rewriteTargets)&&(t.revalidateFiles===void 0||Array.isArray(t.revalidateFiles))}function zr(e){let t=k.join(k.resolve(e.outputDir),hi),r=ns();return r?[k.join(r,wi),t]:[t]}function Jr(e){let t=k.join(k.resolve(e.outputDir),yi),r=ns();return r?[k.join(r,bi),t]:[t]}function ge(e,t){let r=k.resolve(e),n=k.resolve(t),s=k.relative(r,n).replace(/\\/g,"/");return ss(s)}async function lt(e){let t=k.resolve(e.outputDir);for(let r of zr(e))try{let n=await F.readFile(r,"utf8"),s=JSON.parse(n);if(!Pi(s)||k.resolve(s.outputDir)!==t)continue;return os(e,s)}catch{}return null}async function Qr(e,t){let r=os(e,t);for(let n of zr(e)){await F.mkdir(k.dirname(n),{recursive:true});let s=`${n}.${process.pid}.${Date.now()}.tmp`;await F.writeFile(s,JSON.stringify(r,null,2),"utf8"),await F.rename(s,n);}}async function Ge(e){for(let t of zr(e))try{await F.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}async function Gr(e,t){let r={schemaVersion:1,startedAt:String(t.startedAt||new Date().toISOString()),outputDir:k.resolve(e.outputDir)};for(let n of Jr(e)){await F.mkdir(k.dirname(n),{recursive:true});let s=`${n}.${process.pid}.tmp`;await F.writeFile(s,JSON.stringify(r,null,2),"utf8"),await F.rename(s,n);}}async function is(e){let t=k.resolve(e.outputDir);for(let r of Jr(e)){let n;try{n=await F.readFile(r,"utf8");}catch(o){if(o.code==="ENOENT")continue;throw o}let s;try{s=JSON.parse(n);}catch{throw new Error(`Invalid incomplete crawl marker: ${r}`)}if(s.schemaVersion!==1||typeof s.startedAt!="string"||typeof s.outputDir!="string"||k.resolve(s.outputDir)!==t)throw new Error(`Invalid incomplete crawl marker: ${r}`);return {schemaVersion:1,startedAt:s.startedAt,outputDir:t}}return null}async function as(e){for(let t of Jr(e))try{await F.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}function $e(e,t=10){return us.createHash("sha1").update(e).digest("hex").slice(0,t)}async function Ri(e){await F.mkdir(k.dirname(e),{recursive:true});}async function _e(e,t,r){await Ri(e);let n=k.join(k.dirname(e),`.${k.basename(e)}.${process.pid}.${Date.now()}.${us.randomBytes(6).toString("hex")}.tmp`);try{typeof t=="string"?await F.writeFile(n,t,r??"utf8"):await F.writeFile(n,t),await F.rename(n,e);}catch(s){throw await F.unlink(n).catch(()=>{}),s}}function Oe(e){return [".html",".htm",".css",".js",".mjs",".json",".xml",".xsl",".txt",".svg",".map",".enc",".jws"].includes(k.extname(e).toLowerCase())}function Ne(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 ut(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function Kt(e){return e.replace(/\//g,"\\/")}function cs(e){return e.replace(/\//g,"\\\\/")}function vi(e){try{let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")return null;let r=t.pathname==="/"&&!t.search&&!t.hash?"":`${t.pathname}${t.search}${t.hash}`;return `//${t.host}${r}`}catch{return null}}function ls(e,t,r){if(!t)return;e[t]=r;let n=Kt(t),s=Kt(r);e[n]=s;let o=cs(t),a=cs(r);e[o]=a;let c=ut(t),i=ut(r);e[c]=i;let d=ut(n),u=ut(s);e[d]=u;let l=ut(o),g=ut(a);e[l]=g;}function zt(e,t,r){ls(e,t,r);let n=vi(t);n&&n!==t&&ls(e,n,r);}function Zr(e,t){let r=t.startsWith("/")?t:`/${t}`,n=(e||"").replace(/\/$/,"");return !n||n==="."||n==="/"?r:`${n}${r}`}function ds(e,t){try{return new URL(e,t==="."?"https://relative.invalid":t).pathname}catch{return e.startsWith("/")?e:`/${e.replace(/^\.\//,"")}`}}function ps(e,t,r){if(!t)return r;let n=k.dirname(k.resolve(t)),s=k.resolve(e,r.replace(/^\/+/,"")),o=k.relative(n,s).replace(/\\/g,"/");return o?(o.startsWith(".")||(o=`./${o}`),o):"."}async function en(e,t){return (t?[...new Set(t)].map(n=>k.resolve(n)).filter(n=>n.startsWith(k.resolve(e.outputDir))).map(n=>k.relative(e.outputDir,n).replace(/\\/g,"/")).filter(n=>n.length>0):await Ci(["**/*"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(n=>n!=="asset-map.json"&&Oe(n))}var Ti=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],Ai=new Set([".html",".htm"]),ms="WPSuite.io Static Publisher",Ei=ms.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function $i(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function Oi(e){return e.startsWith("//")||e.startsWith("\\/\\/")||e.startsWith("\\\\/\\\\/")}function Mi(e,t,r){return Oi(t)?e.replace(new RegExp(`(?<!:)${$i(t)}`,"g"),r):e.split(t).join(r)}function ji(e,t,r){if(e.wpsuite?.siteSettings?.subscriber===true||e.wpsuite?.subscriptionType==="PROFESSIONAL"||e.wpsuite?.subscriptionType==="AGENCY"||!r)return false;let s=k.extname(r).toLowerCase();return Ai.has(s)?/<head\b|<html\b|<!doctype html/i.test(t):false}function Fi(e){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${Ei}\\2)[^>]*\\/?>`,"i").test(e))return e;let t=`<meta name="generator" content="${ms}" />`;if(e.match(/(\r?\n)([ \t]*)<\/head>/i))return e.replace(/(\r?\n)([ \t]*)<\/head>/i,`$1$2${t}$1$2</head>`);let n=e.includes(`\r
2
2
  `)?`\r
3
3
  `:`
4
- `;return e.replace(/<head\b[^>]*>/i,s=>`${s}${n} ${t}`)}function Fi(e){let t=_e(e).trim();return t.startsWith("{")||t.startsWith("[")||t.includes("\\/")||/"@context"|"@type"/.test(t)}function Ui(e,t){let r=[...new Set(t)].filter(o=>o.includes("/")&&!/\\+\//.test(o)).map(o=>[o,Ht(o)]).filter(([o,a])=>o!==a).sort((o,a)=>a[0].length-o[0].length);if(!r.length)return e;let n=o=>{let a=o;for(let[c,i]of r)a=a.split(c).join(i);return a},s=e.replace(/(<script\b[^>]*\btype=["']application\/(?:ld\+)?json["'][^>]*>)([\s\S]*?)(<\/script>)/gi,(o,a,c,i)=>`${a}${n(c)}${i}`);return s=s.replace(/(<meta\b[^>]*\bcontent=(['"]))([\s\S]*?)(\2[^>]*>)/gi,(o,a,c,i,d)=>Fi(i)?`${a}${n(i)}${d}`:o),s}function Li(e,t){if(!t)return null;let r=C.dirname(C.resolve(t)),n=C.resolve(e.outputDir),s=C.relative(r,n).replace(/\\/g,"/");return s?(s.startsWith(".")||(s=`./${s}`),s.endsWith("/")?s:`${s}/`):"./"}function qi(e,t,r){let n=Li(t,r);if(!n)return e;let s=e;for(let o of Ti){let a=o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),c=new RegExp(`(?:\\.{1,}/)+${a}`,"g"),i=new RegExp(`(?<!\\.)/${a}`,"g");s=s.replace(c,`${n}${o}`).replace(i,`${n}${o}`);let d=o.replace(/\//g,"\\/"),u=n.replace(/\//g,"\\/"),l=d.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),g=new RegExp(`(?:\\.{1,}\\\\/)+${l}`,"g"),p=new RegExp(`(?:\\.{1,}(?:\\\\/|\\\\))+${l}`,"g"),f=new RegExp(`(?<![\\.\\\\])\\/${l}`,"g");s=s.replace(g,`${u}${d}`).replace(p,`${u}${d}`).replace(f,`${u}${d}`);}return s}function Gr(e,t,r){if(e.urlRewriteMode==="absolute")return t;let n=(()=>{try{return new URL(t,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin)}catch{return null}})(),s=n?n.pathname:ls(t,e.targetOrigin),o=n?`${n.search}${n.hash}`:"";return e.urlRewriteMode==="root-relative"?`${s}${o}`:`${us(e.outputDir,r,s)}${o}`}function Qr(e,t,r,n,s){Vt(e,r,Gr(t,n,s));}function Wi(e,t,r,n,s){if(!r||r===n)return;Vt(e,r,n);let o=Gr(t,r,s),a=Gr(t,n,s);Vt(e,o,a);}function Yr(e,t,r,n,s={}){let o=e,a={};Qr(a,t,t.sourceOrigin,t.targetOrigin,n);for(let[i,d]of Object.entries(t.extraReplacements))Qr(a,t,i,d,n);for(let[i,d]of Object.entries(r))Qr(a,t,i,d,n);for(let[i,d]of Object.entries(s)){let u=r[i];!u||u===d||Wi(a,t,d,u,n);}let c=Object.entries(a).sort((i,d)=>d[0].length-i[0].length);for(let[i,d]of c)o=Mi(o,i,d);return t.urlRewriteMode!=="absolute"&&(o=qi(o,t,n)),o=Ui(o,c.map(([,i])=>i)),ji(t,o,n)&&(o=Di(o)),o}async function _i(e,t,r,n){let s=C.join(e.outputDir,n),o=C.extname(s).toLowerCase(),a;try{a=await D.readFile(s,"utf8");}catch(d){if(d.code!=="ENOENT")throw d;return {changed:false}}let i=Yr(a,e,t,o===".js"||o===".mjs"?void 0:s,r);return i!==a?(await We(s,i,"utf8"),{changed:true}):{changed:false}}async function Ni(e,t){return await new Promise((r,n)=>{let s=i=>{if(c(),i?.error){n(new Error(i.error));return}r({changed:!!i?.changed});},o=i=>{c(),n(i);},a=i=>{c(),i!==0&&n(new Error(`Rewrite worker exited with code ${i}.`));},c=()=>{e.off("message",s),e.off("error",o),e.off("exit",a);};e.on("message",s),e.on("error",o),e.on("exit",a),e.postMessage({file:t});})}async function Kt(e,t,r={}){let n=await Zr(e,r.files),s=0;if(n.length===0)return 0;let o=Math.max(1,Number(e.rewriteConcurrency||e.assetDownloadConcurrency||1)),a=Math.max(1,Math.min(o,n.length,availableParallelism()));await r.onStart?.({totalFiles:n.length,workerCount:a});let c=0,i=0,d=null;if(a===1){for(let l of n){let g=await _i(e,t,r.previousAssetMap??{},l);g.changed&&(s+=1),i+=1,await r.onProgress?.({index:i,totalFiles:n.length,changedTextFiles:s,file:l,changed:g.changed});}return s}let u=Array.from({length:a},async()=>{let l=new Worker(new URL("./rewrite-worker.js",import.meta.url),{workerData:{config:e,assetMap:t,previousAssetMap:r.previousAssetMap??{}}});try{for(;!d;){let g=c;if(g>=n.length)return;c+=1;let p=n[g],f=await Ni(l,p);f.changed&&(s+=1),i+=1,await r.onProgress?.({index:i,totalFiles:n.length,changedTextFiles:s,file:p,changed:f.changed});}}catch(g){throw d=g instanceof Error?g:new Error(String(g)),d}finally{await l.terminate().catch(()=>{});}});return await Promise.all(u),s}var Me={error:0,warn:1,info:2,debug:3},Bi=/^(?:authorization|proxyAuthorization|runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken|xStaticPublisherToken|xApiKey)$/i;function tt(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 Jt(e){return typeof e=="string"?tt(e):Array.isArray(e)?e.map(t=>Jt(t)):e instanceof Error?{name:e.name,message:tt(e.message),...e.stack?{stack:tt(e.stack)}:{}}:e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([t,r])=>[t,Bi.test(t)?"[REDACTED]":Jt(r)])):e}function Gt(e){return e?Jt(e):void 0}var ps=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",zt=ps?C.join(ps,"current-progress.json"):"",ms=Promise.resolve();function gs(e){return e.includes("crawl")?"crawl":e.includes("deploy")?"deploy":e.includes("invalidate")?"invalidate":e}function Hi(e,t){let r={...e},n=typeof e.phase=="string"?e.phase.trim():"",s=typeof t.phase=="string"?t.phase.trim():"";if(s!==""&&(s==="deploy"||s.startsWith("deploy-"))&&n!==s)for(let c of ["index","totalFiles","staged","uploaded","skipped","failed","deleted","workerCount","completedBatches","totalBatches"])delete r[c];let a=new Set(["pagesQueued","pagesRendered","assetsQueued","sitemapsQueued","pagesDiscovered","assetsDiscovered","sitemapsDiscovered","pagesSaved","assetsSaved","donePages","doneAssets","doneSitemaps","changedTextFiles","uploaded","failed","index","totalFiles","completedBatches","totalBatches","pageQueue","assetQueue","sitemapQueue"]);for(let[c,i]of Object.entries(t)){let d=r[c];if(typeof d=="number"&&typeof i=="number"&&a.has(c)){r[c]=Math.max(d,i);continue}r[c]=i;}return r}function fs(e,t,r){zt&&(ms=ms.then(async()=>{let n=new Date,s=n.toISOString();await D.mkdir(C.dirname(zt),{recursive:true});let o=await D.readFile(zt,"utf8").then(h=>JSON.parse(h)).catch(()=>null),a=o&&typeof o.details=="object"&&o.details?o.details:{},c=gs(e),i=o?.currentStep||gs(o?.source||""),d=o?.startedAt||s,u={...o?.stepDurationsSec??{}},l=o?.stepStartedAt||s;if(i&&i!==c&&o?.stepStartedAt){let h=Math.max(0,Math.round((n.getTime()-new Date(o.stepStartedAt).getTime())/1e3));u[i]=(u[i]??0)+h,l=s;}let g=Math.max(0,Math.round((n.getTime()-new Date(l).getTime())/1e3)),p=Math.max(0,Math.round((n.getTime()-new Date(d).getTime())/1e3)),f=tt(t),w=Gt(r)??{},m={checkedAt:s,source:e,message:f,details:Hi(a,w),startedAt:d,currentStep:c,stepStartedAt:l,stepElapsedSec:g,totalElapsedSec:p,stepDurationsSec:u};await D.writeFile(zt,JSON.stringify(m,null,2),"utf8");}).catch(()=>{}));}async function hs(e,t){await D.mkdir(C.dirname(e),{recursive:true}),await D.appendFile(e,`${JSON.stringify(Jt(t))}
5
- `,"utf8");}function Vi(e,t){let r=typeof t?.phase=="string"?t.phase.trim():"";return r||(e==="page"?"render-pages":e==="asset"?"download-assets":e==="sitemap"?"discovery":e==="timing"&&typeof t?.name=="string"&&t.name.trim()||"crawl")}function Ki(e,t,r){let n=Gt(r);return {checkedAt:new Date().toISOString(),currentStep:Vi(e,n),level:e,message:tt(t),details:n??{}}}var dt=class{constructor(t,r,n="info"){this.logDir=t;this.logFile=r;this.level=n;this.ensureLogFileReady();}logDir;logFile;level;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;get logPath(){return C.join(this.logDir,this.logFile)}artifactPath(t){return this.logFile.endsWith(".log.jsonl")?C.join(this.logDir,this.logFile.replace(/\.log\.jsonl$/u,`${t}.jsonl`)):C.join(this.logDir,`${this.logFile}${t}.jsonl`)}get errorsPath(){return this.artifactPath(".errors")}get rejectedPath(){return this.artifactPath(".rejected")}ensureLogFileReady(){return this.initPromise||(this.initPromise=(async()=>{await D.mkdir(this.logDir,{recursive:true}),await D.writeFile(this.logPath,"","utf8");})()),this.initPromise}enqueueTask(t){this.writeQueue=this.writeQueue.then(t).catch(r=>{this.writeError=r instanceof Error?r:new Error(String(r));});}enqueueLine(t){this.enqueueTask(async()=>{await this.ensureLogFileReady(),await D.appendFile(this.logPath,`${t}
6
- `,"utf8");});}enqueueJsonLine(t,r){this.enqueueTask(()=>hs(t,r));}accepts(t){return Me[t]<=Me[this.level]}push(t,r,n){if(!this.accepts(t))return;let s=tt(r),o=Gt(n),a=JSON.stringify({time:new Date().toISOString(),level:t,message:s,...o||{}});this.enqueueLine(a),t==="error"?console.error(`[ERROR] ${s}`,o?JSON.stringify(o):""):t==="warn"?console.warn(`[WARN] ${s}`,o?JSON.stringify(o):""):console.log(`[${t.toUpperCase().padEnd(5)}] ${s}`);}info(t,r){this.push("info",t,r);}progress(t,r){fs(this.logFile,t,r);}debug(t,r){this.push("debug",t,r);}warn(t,r){this.push("warn",t,r);}reject(t,r,n,s,o){let a={kind:t,url:r,reason:n,...s?{source:s}:{},...o||{}};this.enqueueJsonLine(this.rejectedPath,a),this.accepts("debug")&&this.push("debug",`Rejected ${t} ${r}: ${n}`,a);}error(t,r){this.enqueueJsonLine(this.errorsPath,{message:t,...r||{}}),this.push("error",t,r);}mark(t){this.marks.set(t,Date.now());}endMark(t,r){let n=this.marks.get(t);if(!n)return;let s=Date.now()-n,o=Number((s/1e3).toFixed(2));this.push("info",`Timing ${t}: ${o}s`,{name:t,ms:s,seconds:o,...r||{}});}async flush(){let t=Date.now()-this.startedAt,r=Number((t/1e3).toFixed(2));if(this.push("info",`Total time: ${r}s`,{name:"total",ms:t,seconds:r}),await this.writeQueue,this.writeError)throw this.writeError}},Qt=class{constructor(t,r="info"){this.logDir=t;this.level=typeof r=="boolean"?r?"debug":"info":r,this.ensureLogFileReady();}logDir;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;level;get logPath(){return C.join(this.logDir,"crawl.log.jsonl")}get currentEventPath(){return C.join(this.logDir,"current-crawl-event.json")}get rejectedPath(){return C.join(this.logDir,"rejected.jsonl")}get ignoredPath(){return C.join(this.logDir,"ignored.jsonl")}get skippedPath(){return C.join(this.logDir,"skipped-http.jsonl")}get errorsPath(){return C.join(this.logDir,"errors.jsonl")}get timingsPath(){return C.join(this.logDir,"timings.jsonl")}ensureLogFileReady(){return this.initPromise||(this.initPromise=(async()=>{await D.mkdir(this.logDir,{recursive:true}),await D.writeFile(this.logPath,"","utf8");})()),this.initPromise}enqueueTask(t){this.writeQueue=this.writeQueue.then(t).catch(r=>{this.writeError=r instanceof Error?r:new Error(String(r));});}enqueueLine(t){this.enqueueTask(async()=>{await this.ensureLogFileReady(),await D.appendFile(this.logPath,`${t}
7
- `,"utf8");});}enqueueJsonLine(t,r){this.enqueueTask(()=>hs(t,r));}updateCurrentEvent(t,r,n){let s=Ki(t,r,n);this.enqueueTask(async()=>{await D.mkdir(this.logDir,{recursive:true}),await D.writeFile(this.currentEventPath,JSON.stringify(s,null,2),"utf8");});}push(t,r,n){let s=tt(r),o=Gt(n),a=JSON.stringify({time:new Date().toISOString(),level:t,message:s,...o||{}});this.enqueueLine(a),this.updateCurrentEvent(t,s,o),t==="error"?console.error(s,o||""):t==="warn"?console.warn(s,o||""):(Me[this.level]>=Me.debug||["summary","page","sitemap","asset","timing"].includes(t))&&console.log(s);}info(t,r){this.push("info",t,r);}debug(t,r){Me[this.level]<Me.debug||this.push("debug",t,r);}progress(t,r){fs("crawl.log.jsonl",t,r),this.updateCurrentEvent("progress",t,r);}checkpoint(t,r){this.updateCurrentEvent("checkpoint",t,r);}page(t,r){this.push("page",t,r);}sitemap(t,r){this.push("sitemap",t,r);}asset(t,r){this.push("asset",t,r);}warn(t,r){this.push("warn",t,r);}error(t,r){this.enqueueJsonLine(this.errorsPath,{message:t,...r||{}}),this.push("error",t,r);}summary(t,r){this.push("summary",t,r);}mark(t){this.marks.set(t,Date.now());}endMark(t,r){let n=this.marks.get(t);if(!n)return;let s=Date.now()-n,o={name:t,ms:s,seconds:Number((s/1e3).toFixed(2)),...r||{}};this.enqueueJsonLine(this.timingsPath,o),this.push("timing",`Timing ${t}: ${o.seconds}s`,o);}reject(t,r,n,s){let o={kind:t,url:r,reason:n,source:s};this.enqueueJsonLine(this.rejectedPath,o),Me[this.level]>=Me.debug&&this.push("reject",`Rejected ${t} ${r}: ${n}`,o);}ignore(t,r,n,s){let o={kind:t,url:r,reason:n,source:s};this.enqueueJsonLine(this.ignoredPath,o),Me[this.level]>=Me.debug&&this.push("ignore",`Ignored ${t} ${r}: ${n}`,o);}skip(t,r,n,s){let o={kind:t,url:r,status:n,source:s};this.enqueueJsonLine(this.skippedPath,o),this.push("warn",`Skipped ${t} ${r}: HTTP ${n}`,o);}async flush(){let t=Date.now()-this.startedAt,r={name:"total",ms:t,seconds:Number((t/1e3).toFixed(2))};if(this.enqueueJsonLine(this.timingsPath,r),await this.writeQueue,this.writeError)throw this.writeError}};var ws="(?:css|js|mjs|json|map|xml|xsl|rss|atom|txt|enc|jws|png|jpe?g|gif|webp|avif|svg|ico|woff2?|ttf|otf|eot|pdf|mp4|webm)";function bs(e){let t=new RegExp(`(?:https?:)?//[^\\s'"<>\\);]+\\.${ws}(?:[?#][^\\s'"<>\\);]*)?(?=[\\s'"<>\\);]|$)|(?<![A-Za-z0-9._-])/[^\\s'"<>\\);]+\\.${ws}(?:[?#][^\\s'"<>\\);]*)?(?=[\\s'"<>\\);]|$)`,"gi");return [...e.matchAll(t)].map(r=>r[0])}function ys(e){return !!e&&/^(?:\.{1,2}\/)?[A-Za-z0-9@_][A-Za-z0-9@._/-]*\.(?:js|mjs)(?:[?#][^\s<>\\]*)?$/i.test(e)&&!e.startsWith("/")&&!e.startsWith("//")&&!/^[a-z][a-z0-9+.-]*:/i.test(e)&&!/(?:^|\/)node_modules(?:\/|$)|(?:^|\/)\.pnpm(?:\/|$)/i.test(e)}function Ss(e){let t=new Set,r=[],n=/\bimport\s*\(\s*["'`]([^"'`\s<>\\]+)["'`]\s*\)/g,s=/\b(?:[A-Za-z_$][\w$]*\.)?u\s*=\s*(?:\([^)]*\)|[A-Za-z_$][\w$]*)\s*=>\s*["'`]([^"'`\s<>\\]+)["'`]/g;for(let o of e.matchAll(n))o[1]&&ys(o[1])&&r.push({index:o.index??0,value:o[1]});for(let o of e.matchAll(s))o[1]&&ys(o[1])&&r.push({index:o.index??0,value:o[1]});for(let o of r.sort((a,c)=>a.index-c.index))t.add(o.value);return [...t]}function Ps(e,t){let r=C.relative(t,e);return r===""||!r.startsWith("..")&&!C.isAbsolute(r)}function Rs(e,t,r,n){let s=C.resolve(e),o=C.resolve(t),a=C.extname(s).toLowerCase();return !Ps(s,o)||a!==".html"&&a!==".htm"||r.has(s)?false:!n.some(c=>Ps(s,C.resolve(c)))}function Xr(e,t){return typeof e=="string"&&e.length>0&&t?.supported===true&&typeof t.token=="string"&&t.token.length>0&&e===t.token}function en(e,t){try{let r=new URL(e),n=new URL(t,r);if(n.origin!==r.origin)return !1;let s=r.pathname.replace(/\/+$/,"");if(s&&n.pathname!==s&&!n.pathname.startsWith(`${s}/`))return !1;let o=s?n.pathname.slice(s.length)||"/":n.pathname;return ["/wp-includes/","/wp-content/plugins/","/wp-content/themes/","/wp-content/mu-plugins/"].some(a=>o.startsWith(a))}catch{return false}}function vs(e){return e<1}function Cs(e){let t=String(e??"").toLowerCase();return t==="strict"?"Strict":t==="lax"?"Lax":t==="none"?"None":"Unspecified"}function Ji(e){let t=e.pathname||"/";if(!t.startsWith("/")||t==="/")return "/";let r=t.lastIndexOf("/");return r<=0?"/":t.slice(0,r)}function It(e){return `${e.name.toLowerCase()}
4
+ `;return e.replace(/<head\b[^>]*>/i,s=>`${s}${n} ${t}`)}function Di(e){let t=Ne(e).trim();return t.startsWith("{")||t.startsWith("[")||t.includes("\\/")||/"@context"|"@type"/.test(t)}function Ui(e,t){let r=[...new Set(t)].filter(o=>o.includes("/")&&!/\\+\//.test(o)).map(o=>[o,Kt(o)]).filter(([o,a])=>o!==a).sort((o,a)=>a[0].length-o[0].length);if(!r.length)return e;let n=o=>{let a=o;for(let[c,i]of r)a=a.split(c).join(i);return a},s=e.replace(/(<script\b[^>]*\btype=["']application\/(?:ld\+)?json["'][^>]*>)([\s\S]*?)(<\/script>)/gi,(o,a,c,i)=>`${a}${n(c)}${i}`);return s=s.replace(/(<meta\b[^>]*\bcontent=(['"]))([\s\S]*?)(\2[^>]*>)/gi,(o,a,c,i,d)=>Di(i)?`${a}${n(i)}${d}`:o),s}function Li(e,t){if(!t)return null;let r=k.dirname(k.resolve(t)),n=k.resolve(e.outputDir),s=k.relative(r,n).replace(/\\/g,"/");return s?(s.startsWith(".")||(s=`./${s}`),s.endsWith("/")?s:`${s}/`):"./"}function Wi(e,t,r){let n=Li(t,r);if(!n)return e;let s=e;for(let o of Ti){let a=o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),c=new RegExp(`(?:\\.{1,}/)+${a}`,"g"),i=new RegExp(`(?<!\\.)/${a}`,"g");s=s.replace(c,`${n}${o}`).replace(i,`${n}${o}`);let d=o.replace(/\//g,"\\/"),u=n.replace(/\//g,"\\/"),l=d.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),g=new RegExp(`(?:\\.{1,}\\\\/)+${l}`,"g"),p=new RegExp(`(?:\\.{1,}(?:\\\\/|\\\\))+${l}`,"g"),f=new RegExp(`(?<![\\.\\\\])\\/${l}`,"g");s=s.replace(g,`${u}${d}`).replace(p,`${u}${d}`).replace(f,`${u}${d}`);}return s}function Xr(e,t,r){if(e.urlRewriteMode==="absolute")return t;let n=(()=>{try{return new URL(t,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin)}catch{return null}})(),s=n?n.pathname:ds(t,e.targetOrigin),o=n?`${n.search}${n.hash}`:"";return e.urlRewriteMode==="root-relative"?`${s}${o}`:`${ps(e.outputDir,r,s)}${o}`}function Yr(e,t,r,n,s){zt(e,r,Xr(t,n,s));}function qi(e,t,r,n,s){if(!r||r===n)return;zt(e,r,n);let o=Xr(t,r,s),a=Xr(t,n,s);zt(e,o,a);}function tn(e,t,r,n,s={}){let o=e,a={};Yr(a,t,t.sourceOrigin,t.targetOrigin,n);for(let[i,d]of Object.entries(t.extraReplacements))Yr(a,t,i,d,n);for(let[i,d]of Object.entries(r))Yr(a,t,i,d,n);for(let[i,d]of Object.entries(s)){let u=r[i];!u||u===d||qi(a,t,d,u,n);}let c=Object.entries(a).sort((i,d)=>d[0].length-i[0].length);for(let[i,d]of c)o=Mi(o,i,d);return t.urlRewriteMode!=="absolute"&&(o=Wi(o,t,n)),o=Ui(o,c.map(([,i])=>i)),ji(t,o,n)&&(o=Fi(o)),o}async function _i(e,t,r,n){let s=k.join(e.outputDir,n),o=k.extname(s).toLowerCase(),a;try{a=await F.readFile(s,"utf8");}catch(d){if(d.code!=="ENOENT")throw d;return {changed:false}}let i=tn(a,e,t,o===".js"||o===".mjs"?void 0:s,r);return i!==a?(await _e(s,i,"utf8"),{changed:true}):{changed:false}}async function Ni(e,t){return await new Promise((r,n)=>{let s=i=>{if(c(),i?.error){n(new Error(i.error));return}r({changed:!!i?.changed});},o=i=>{c(),n(i);},a=i=>{c(),i!==0&&n(new Error(`Rewrite worker exited with code ${i}.`));},c=()=>{e.off("message",s),e.off("error",o),e.off("exit",a);};e.on("message",s),e.on("error",o),e.on("exit",a),e.postMessage({file:t});})}async function Jt(e,t,r={}){let n=await en(e,r.files),s=0;if(n.length===0)return 0;let o=Math.max(1,Number(e.rewriteConcurrency||e.assetDownloadConcurrency||1)),a=Math.max(1,Math.min(o,n.length,availableParallelism()));await r.onStart?.({totalFiles:n.length,workerCount:a});let c=0,i=0,d=null;if(a===1){for(let l of n){let g=await _i(e,t,r.previousAssetMap??{},l);g.changed&&(s+=1),i+=1,await r.onProgress?.({index:i,totalFiles:n.length,changedTextFiles:s,file:l,changed:g.changed});}return s}let u=Array.from({length:a},async()=>{let l=new Worker(new URL("./rewrite-worker.js",import.meta.url),{workerData:{config:e,assetMap:t,previousAssetMap:r.previousAssetMap??{}}});try{for(;!d;){let g=c;if(g>=n.length)return;c+=1;let p=n[g],f=await Ni(l,p);f.changed&&(s+=1),i+=1,await r.onProgress?.({index:i,totalFiles:n.length,changedTextFiles:s,file:p,changed:f.changed});}}catch(g){throw d=g instanceof Error?g:new Error(String(g)),d}finally{await l.terminate().catch(()=>{});}});return await Promise.all(u),s}var je={error:0,warn:1,info:2,debug:3},Bi=/^(?:authorization|proxyAuthorization|runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken|xStaticPublisherToken|xApiKey)$/i;function rt(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 Gt(e){return typeof e=="string"?rt(e):Array.isArray(e)?e.map(t=>Gt(t)):e instanceof Error?{name:e.name,message:rt(e.message),...e.stack?{stack:rt(e.stack)}:{}}:e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([t,r])=>[t,Bi.test(t)?"[REDACTED]":Gt(r)])):e}function Yt(e){return e?Gt(e):void 0}var gs=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",Qt=gs?k.join(gs,"current-progress.json"):"",fs=Promise.resolve();function hs(e){return e.includes("crawl")?"crawl":e.includes("deploy")?"deploy":e.includes("invalidate")?"invalidate":e}function Hi(e,t){let r={...e},n=typeof e.phase=="string"?e.phase.trim():"",s=typeof t.phase=="string"?t.phase.trim():"";if(s!==""&&(s==="deploy"||s.startsWith("deploy-"))&&n!==s)for(let c of ["index","totalFiles","staged","uploaded","skipped","failed","deleted","workerCount","completedBatches","totalBatches"])delete r[c];let a=new Set(["pagesQueued","pagesRendered","assetsQueued","sitemapsQueued","pagesDiscovered","assetsDiscovered","sitemapsDiscovered","pagesSaved","assetsSaved","donePages","doneAssets","doneSitemaps","changedTextFiles","uploaded","failed","index","totalFiles","completedBatches","totalBatches","pageQueue","assetQueue","sitemapQueue"]);for(let[c,i]of Object.entries(t)){let d=r[c];if(typeof d=="number"&&typeof i=="number"&&a.has(c)){r[c]=Math.max(d,i);continue}r[c]=i;}return r}function ws(e,t,r){Qt&&(fs=fs.then(async()=>{let n=new Date,s=n.toISOString();await F.mkdir(k.dirname(Qt),{recursive:true});let o=await F.readFile(Qt,"utf8").then(h=>JSON.parse(h)).catch(()=>null),a=o&&typeof o.details=="object"&&o.details?o.details:{},c=hs(e),i=o?.currentStep||hs(o?.source||""),d=o?.startedAt||s,u={...o?.stepDurationsSec??{}},l=o?.stepStartedAt||s;if(i&&i!==c&&o?.stepStartedAt){let h=Math.max(0,Math.round((n.getTime()-new Date(o.stepStartedAt).getTime())/1e3));u[i]=(u[i]??0)+h,l=s;}let g=Math.max(0,Math.round((n.getTime()-new Date(l).getTime())/1e3)),p=Math.max(0,Math.round((n.getTime()-new Date(d).getTime())/1e3)),f=rt(t),w=Yt(r)??{},m={checkedAt:s,source:e,message:f,details:Hi(a,w),startedAt:d,currentStep:c,stepStartedAt:l,stepElapsedSec:g,totalElapsedSec:p,stepDurationsSec:u};await F.writeFile(Qt,JSON.stringify(m,null,2),"utf8");}).catch(()=>{}));}async function ys(e,t){await F.mkdir(k.dirname(e),{recursive:true}),await F.appendFile(e,`${JSON.stringify(Gt(t))}
5
+ `,"utf8");}function Vi(e,t){let r=typeof t?.phase=="string"?t.phase.trim():"";return r||(e==="page"?"render-pages":e==="asset"?"download-assets":e==="sitemap"?"discovery":e==="timing"&&typeof t?.name=="string"&&t.name.trim()||"crawl")}function Ki(e,t,r){let n=Yt(r);return {checkedAt:new Date().toISOString(),currentStep:Vi(e,n),level:e,message:rt(t),details:n??{}}}var dt=class{constructor(t,r,n="info"){this.logDir=t;this.logFile=r;this.level=n;this.ensureLogFileReady();}logDir;logFile;level;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;get logPath(){return k.join(this.logDir,this.logFile)}artifactPath(t){return this.logFile.endsWith(".log.jsonl")?k.join(this.logDir,this.logFile.replace(/\.log\.jsonl$/u,`${t}.jsonl`)):k.join(this.logDir,`${this.logFile}${t}.jsonl`)}get errorsPath(){return this.artifactPath(".errors")}get rejectedPath(){return this.artifactPath(".rejected")}ensureLogFileReady(){return this.initPromise||(this.initPromise=(async()=>{await F.mkdir(this.logDir,{recursive:true}),await F.writeFile(this.logPath,"","utf8");})()),this.initPromise}enqueueTask(t){this.writeQueue=this.writeQueue.then(t).catch(r=>{this.writeError=r instanceof Error?r:new Error(String(r));});}enqueueLine(t){this.enqueueTask(async()=>{await this.ensureLogFileReady(),await F.appendFile(this.logPath,`${t}
6
+ `,"utf8");});}enqueueJsonLine(t,r){this.enqueueTask(()=>ys(t,r));}accepts(t){return je[t]<=je[this.level]}push(t,r,n){if(!this.accepts(t))return;let s=rt(r),o=Yt(n),a=JSON.stringify({time:new Date().toISOString(),level:t,message:s,...o||{}});this.enqueueLine(a),t==="error"?console.error(`[ERROR] ${s}`,o?JSON.stringify(o):""):t==="warn"?console.warn(`[WARN] ${s}`,o?JSON.stringify(o):""):console.log(`[${t.toUpperCase().padEnd(5)}] ${s}`);}info(t,r){this.push("info",t,r);}progress(t,r){ws(this.logFile,t,r);}debug(t,r){this.push("debug",t,r);}warn(t,r){this.push("warn",t,r);}reject(t,r,n,s,o){let a={kind:t,url:r,reason:n,...s?{source:s}:{},...o||{}};this.enqueueJsonLine(this.rejectedPath,a),this.accepts("debug")&&this.push("debug",`Rejected ${t} ${r}: ${n}`,a);}error(t,r){this.enqueueJsonLine(this.errorsPath,{message:t,...r||{}}),this.push("error",t,r);}mark(t){this.marks.set(t,Date.now());}endMark(t,r){let n=this.marks.get(t);if(!n)return;let s=Date.now()-n,o=Number((s/1e3).toFixed(2));this.push("info",`Timing ${t}: ${o}s`,{name:t,ms:s,seconds:o,...r||{}});}async flush(){let t=Date.now()-this.startedAt,r=Number((t/1e3).toFixed(2));if(this.push("info",`Total time: ${r}s`,{name:"total",ms:t,seconds:r}),await this.writeQueue,this.writeError)throw this.writeError}},Zt=class{constructor(t,r="info"){this.logDir=t;this.level=typeof r=="boolean"?r?"debug":"info":r,this.ensureLogFileReady();}logDir;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;level;get logPath(){return k.join(this.logDir,"crawl.log.jsonl")}get currentEventPath(){return k.join(this.logDir,"current-crawl-event.json")}get rejectedPath(){return k.join(this.logDir,"rejected.jsonl")}get ignoredPath(){return k.join(this.logDir,"ignored.jsonl")}get skippedPath(){return k.join(this.logDir,"skipped-http.jsonl")}get errorsPath(){return k.join(this.logDir,"errors.jsonl")}get timingsPath(){return k.join(this.logDir,"timings.jsonl")}ensureLogFileReady(){return this.initPromise||(this.initPromise=(async()=>{await F.mkdir(this.logDir,{recursive:true}),await F.writeFile(this.logPath,"","utf8");})()),this.initPromise}enqueueTask(t){this.writeQueue=this.writeQueue.then(t).catch(r=>{this.writeError=r instanceof Error?r:new Error(String(r));});}enqueueLine(t){this.enqueueTask(async()=>{await this.ensureLogFileReady(),await F.appendFile(this.logPath,`${t}
7
+ `,"utf8");});}enqueueJsonLine(t,r){this.enqueueTask(()=>ys(t,r));}updateCurrentEvent(t,r,n){let s=Ki(t,r,n);this.enqueueTask(async()=>{await F.mkdir(this.logDir,{recursive:true}),await F.writeFile(this.currentEventPath,JSON.stringify(s,null,2),"utf8");});}push(t,r,n){let s=rt(r),o=Yt(n),a=JSON.stringify({time:new Date().toISOString(),level:t,message:s,...o||{}});this.enqueueLine(a),this.updateCurrentEvent(t,s,o),t==="error"?console.error(s,o||""):t==="warn"?console.warn(s,o||""):(je[this.level]>=je.debug||["summary","page","sitemap","asset","timing"].includes(t))&&console.log(s);}info(t,r){this.push("info",t,r);}debug(t,r){je[this.level]<je.debug||this.push("debug",t,r);}progress(t,r){ws("crawl.log.jsonl",t,r),this.updateCurrentEvent("progress",t,r);}checkpoint(t,r){this.updateCurrentEvent("checkpoint",t,r);}page(t,r){this.push("page",t,r);}sitemap(t,r){this.push("sitemap",t,r);}asset(t,r){this.push("asset",t,r);}warn(t,r){this.push("warn",t,r);}error(t,r){this.enqueueJsonLine(this.errorsPath,{message:t,...r||{}}),this.push("error",t,r);}summary(t,r){this.push("summary",t,r);}mark(t){this.marks.set(t,Date.now());}endMark(t,r){let n=this.marks.get(t);if(!n)return;let s=Date.now()-n,o={name:t,ms:s,seconds:Number((s/1e3).toFixed(2)),...r||{}};this.enqueueJsonLine(this.timingsPath,o),this.push("timing",`Timing ${t}: ${o.seconds}s`,o);}reject(t,r,n,s){let o={kind:t,url:r,reason:n,source:s};this.enqueueJsonLine(this.rejectedPath,o),je[this.level]>=je.debug&&this.push("reject",`Rejected ${t} ${r}: ${n}`,o);}ignore(t,r,n,s){let o={kind:t,url:r,reason:n,source:s};this.enqueueJsonLine(this.ignoredPath,o),je[this.level]>=je.debug&&this.push("ignore",`Ignored ${t} ${r}: ${n}`,o);}skip(t,r,n,s){let o={kind:t,url:r,status:n,source:s};this.enqueueJsonLine(this.skippedPath,o),this.push("warn",`Skipped ${t} ${r}: HTTP ${n}`,o);}async flush(){let t=Date.now()-this.startedAt,r={name:"total",ms:t,seconds:Number((t/1e3).toFixed(2))};if(this.enqueueJsonLine(this.timingsPath,r),await this.writeQueue,this.writeError)throw this.writeError}};var bs="(?:css|js|mjs|json|map|xml|xsl|rss|atom|txt|enc|jws|png|jpe?g|gif|webp|avif|svg|ico|woff2?|ttf|otf|eot|pdf|mp4|webm)";function Ps(e){let t=new RegExp(`(?:https?:)?//[^\\s'"<>\\);]+\\.${bs}(?:[?#][^\\s'"<>\\);]*)?(?=[\\s'"<>\\);]|$)|(?<![A-Za-z0-9._-])/[^\\s'"<>\\);]+\\.${bs}(?:[?#][^\\s'"<>\\);]*)?(?=[\\s'"<>\\);]|$)`,"gi");return [...e.matchAll(t)].map(r=>r[0])}function Ss(e){return !!e&&/^(?:\.{1,2}\/)?[A-Za-z0-9@_][A-Za-z0-9@._/-]*\.(?:js|mjs)(?:[?#][^\s<>\\]*)?$/i.test(e)&&!e.startsWith("/")&&!e.startsWith("//")&&!/^[a-z][a-z0-9+.-]*:/i.test(e)&&!/(?:^|\/)node_modules(?:\/|$)|(?:^|\/)\.pnpm(?:\/|$)/i.test(e)}function Rs(e){let t=new Set,r=[],n=/\bimport\s*\(\s*["'`]([^"'`\s<>\\]+)["'`]\s*\)/g,s=/\b(?:[A-Za-z_$][\w$]*\.)?u\s*=\s*(?:\([^)]*\)|[A-Za-z_$][\w$]*)\s*=>\s*["'`]([^"'`\s<>\\]+)["'`]/g;for(let o of e.matchAll(n))o[1]&&Ss(o[1])&&r.push({index:o.index??0,value:o[1]});for(let o of e.matchAll(s))o[1]&&Ss(o[1])&&r.push({index:o.index??0,value:o[1]});for(let o of r.sort((a,c)=>a.index-c.index))t.add(o.value);return [...t]}function vs(e,t){let r=k.relative(t,e);return r===""||!r.startsWith("..")&&!k.isAbsolute(r)}function Cs(e,t,r,n){let s=k.resolve(e),o=k.resolve(t),a=k.extname(s).toLowerCase();return !vs(s,o)||a!==".html"&&a!==".htm"||r.has(s)?false:!n.some(c=>vs(s,k.resolve(c)))}function rn(e,t){return typeof e=="string"&&e.length>0&&t?.supported===true&&typeof t.token=="string"&&t.token.length>0&&e===t.token}function nn(e,t){try{let r=new URL(e),n=new URL(t,r);if(n.origin!==r.origin)return !1;let s=r.pathname.replace(/\/+$/,"");if(s&&n.pathname!==s&&!n.pathname.startsWith(`${s}/`))return !1;let o=s?n.pathname.slice(s.length)||"/":n.pathname;return ["/wp-includes/","/wp-content/plugins/","/wp-content/themes/","/wp-content/mu-plugins/"].some(a=>o.startsWith(a))}catch{return false}}function ks(e){return e<1}function xs(e){let t=String(e??"").toLowerCase();return t==="strict"?"Strict":t==="lax"?"Lax":t==="none"?"None":"Unspecified"}function Ji(e){let t=e.pathname||"/";if(!t.startsWith("/")||t==="/")return "/";let r=t.lastIndexOf("/");return r<=0?"/":t.slice(0,r)}function It(e){return `${e.name.toLowerCase()}
8
8
  ${e.domain.toLowerCase()}
9
- ${e.path}`}function ks(e,t){let r;try{r=new URL(t);}catch{return null}let n=e.split(";"),s=n.shift()?.trim()??"",o=s.indexOf("=");if(o<=0)return null;let a=s.slice(0,o).trim();if(!a||[...a].some(w=>w<=" "||"=;,".includes(w)))return null;let c=r.hostname.toLowerCase(),i=Ji(r),d=null,u=true,l=false,g=false,p="Unspecified",f=false;for(let w of n){let m=w.indexOf("="),h=(m<0?w:w.slice(0,m)).trim().toLowerCase(),y=m<0?"":w.slice(m+1).trim();if(h==="domain"&&y)c=y.toLowerCase();else if(h==="path"&&y.startsWith("/"))i=y;else if(h==="expires"){let b=Date.parse(y);Number.isFinite(b)&&(d=Math.floor(b/1e3),u=false);}else if(h==="max-age"&&/^-?\d+$/u.test(y)){let b=Number.parseInt(y,10);d=Math.floor(Date.now()/1e3)+b,u=false;}else h==="secure"?l=true:h==="httponly"?g=true:h==="samesite"?p=Cs(y):h==="partitioned"&&(f=true);}return {name:a,domain:c,path:i,expires:d,session:u,secure:l,httpOnly:g,sameSite:p,partitioned:f}}function Qi(e){let t=!Number.isFinite(e.expires)||e.expires<=0;return {name:e.name,domain:e.domain.toLowerCase(),path:e.path||"/",expires:t?null:e.expires,session:t,secure:e.secure,httpOnly:e.httpOnly,sameSite:Cs(e.sameSite),partitioned:e.partitioned===true}}function xs(e,t){let r=new Map(e.map(n=>[It(n),n.value]));return t.filter(n=>r.get(It(n))!==n.value).map(Qi)}function Gi(e,t){let r=e.session||t.session;return {...e,expires:r?null:Math.max(e.expires??0,t.expires??0)||null,session:r,secure:e.secure&&t.secure,httpOnly:e.httpOnly&&t.httpOnly,sameSite:e.sameSite===t.sameSite?e.sameSite:"Unspecified",partitioned:e.partitioned&&t.partitioned}}var Zt=class{#e=new Map;add(t){if(!t)return;let r={...t,domain:t.domain.toLowerCase(),path:t.path||"/"},n=It(r),s=this.#e.get(n);this.#e.set(n,s?Gi(s,r):r);}addAll(t){for(let r of t)this.add(r);}artifact(){return {schemaVersion:1,observations:[...this.#e.values()].sort((t,r)=>It(t).localeCompare(It(r)))}}};async function Yt(e,t){await D.mkdir(C.dirname(e),{recursive:true});let r=`${e}.${process.pid}.tmp`;await D.writeFile(r,`${JSON.stringify(t,null,2)}
10
- `,{encoding:"utf8",mode:384}),await D.rename(r,e);}function Tt(e){let t=e;for(let n=0;n<3;n+=1){if(/%(?:2f|5c)/i.test(t))return null;try{let s=decodeURIComponent(t);if(s===t)break;t=s;}catch{return null}}return t.includes("\0")||t.includes("\\")||t.split("/").some(n=>n==="."||n==="..")?null:t}function At(e,t){let r=Tt(t);if(r===null)throw new Error(`Unsafe output URL pathname: ${t}`);let n=C.resolve(e),s=C.resolve(n,r.replace(/^\/+/,""));if(s!==n&&!s.startsWith(`${n}${C.sep}`))throw new Error(`Output path escapes configured root: ${t}`);return s}var ee=1,Is=/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;function te(e,t){let r=String(e??"").trim();if(!Is.test(r))throw new Error(`${t} must match ${Is.source} and be at most 128 characters.`);return r}function Ts(e){let t=String(e??"").trim().replace(/^\/+|\/+$/g,"");if(!t)return "";let r=t.split("/").filter(Boolean);if(r.some(n=>n==="."||n===".."))throw new Error("S3 prefix contains an unsafe dot path segment.");return `${r.join("/")}/`}function Ge(e,...t){let r=Ts(e),n=t.map((s,o)=>{let a=String(s??"").replace(/^\/+|\/+$/g,"");if(!a||a.split("/").some(c=>c==="."||c===".."))throw new Error(`Unsafe S3 key segment at index ${o}.`);return a});return `${r}${n.join("/")}`}function ve(e){return createHash("sha256").update(e).digest("hex")}function je(e,t){let r=String(e??"").replace(/^\/+/,""),n=Ts(t);if(!r||r.includes("\0"))throw new Error("S3 object key is empty or invalid.");if(n&&!r.startsWith(n))throw new Error(`S3 object key is outside the configured workspace prefix: ${r}`);if(r.split("/").some(s=>s===".."))throw new Error("S3 object key contains an unsafe parent segment.");return r}function ae(e,t,{optional:r=false}={}){if(!(r&&(e==null||e===""))){if(typeof e!="string"||!e.trim())throw new Error(`${t} must be a non-empty string.`);return e.trim()}}function aa(e){if(!e||typeof e!="object")throw new Error("Remote worker config must be a JSON object.");let t=e;if(t.schemaVersion!==1)throw new Error(`Unsupported remote worker config version: ${String(t.schemaVersion)}.`);if(t.protocolVersion!==ee)throw new Error(`Remote worker protocol mismatch: expected ${ee}, received ${String(t.protocolVersion)}.`);let r=t.workspace,n=t.functions;if(!r||!n)throw new Error("Remote worker config requires workspace and functions objects.");return {schemaVersion:1,region:ae(t.region,"region"),...ae(t.roleArn,"roleArn",{optional:true})?{roleArn:ae(t.roleArn,"roleArn")}:{},...ae(t.roleSessionName,"roleSessionName",{optional:true})?{roleSessionName:ae(t.roleSessionName,"roleSessionName")}:{},workspace:{bucket:ae(r.bucket,"workspace.bucket"),prefix:String(r.prefix??"")},...t.status&&typeof t.status=="object"?{status:{tableName:ae(t.status.tableName,"status.tableName")}}:{},functions:{render:ae(n.render,"functions.render"),...ae(n.asset,"functions.asset",{optional:true})?{asset:ae(n.asset,"functions.asset")}:{},rewrite:ae(n.rewrite,"functions.rewrite"),"deploy-copy":ae(n["deploy-copy"],"functions.deploy-copy")},targets:Array.isArray(t.targets)?t.targets.map((s,o)=>{if(!s||typeof s!="object")throw new Error(`targets[${o}] must be an object.`);let a=s;return {id:ae(a.id,`targets[${o}].id`),bucketName:ae(a.bucketName,`targets[${o}].bucketName`),prefix:String(a.prefix??""),region:ae(a.region,`targets[${o}].region`)}}):[],protocolVersion:ee}}async function Xt(e){let t=C.resolve(e);try{return aa(JSON.parse(await readFile(t,"utf8")))}catch(r){throw new Error(`Unable to load remote worker config ${t}: ${r instanceof Error?r.message:String(r)}`,{cause:r})}}var mt=class{config;lambda;s3;dynamodb;#e;constructor(t,r={},n={}){this.config=t;let s=t.roleArn?fromTemporaryCredentials({params:{RoleArn:t.roleArn,RoleSessionName:t.roleSessionName??"publisher-exporter-remote-worker"},clientConfig:{region:t.region}}):void 0;this.lambda=new LambdaClient({region:t.region,...s?{credentials:s}:{},...r}),this.s3=new S3Client({region:t.region,...s?{credentials:s}:{}}),this.dynamodb=t.status?new DynamoDBClient({region:t.region,...s?{credentials:s}:{}}):null,this.#e=n;}async#n(t){if(!this.dynamodb||!this.config.status)return null;let n=(await this.dynamodb.send(new GetItemCommand({TableName:this.config.status.tableName,ConsistentRead:true,Key:{jobId:{S:t.jobId},taskId:{S:t.taskId}}}))).Item;if(!n)return null;let s=n.operation?.S,o=n.status?.S;return !s||!["render","asset","rewrite","deploy-copy"].includes(s)||!o||!["running","succeeded","partial","failed"].includes(o)?null:{jobId:n.jobId?.S??t.jobId,taskId:n.taskId?.S??t.taskId,operation:s,status:o,completedItems:Number(n.completedItems?.N??0),failedItems:Number(n.failedItems?.N??0),totalItems:Number(n.totalItems?.N??0),sequence:Number(n.sequence?.N??0),updatedAt:n.updatedAt?.S??new Date(0).toISOString(),requestId:n.requestId?.S??null}}async#t(t,r){let n=this.lambda.send(new InvokeCommand({FunctionName:r,InvocationType:"RequestResponse",Payload:Buffer.from(JSON.stringify(t),"utf8")}));if(t.operation==="health"||!this.dynamodb||!this.#e.onProgress)return await n;let s=false,o=n.finally(()=>{s=true;}),a=Math.max(500,Math.min(3e4,this.#e.progressPollIntervalMs??2e3)),c=-1;for(;!s;){await Promise.race([o.then(()=>{},()=>{}),new Promise(i=>setTimeout(i,a))]);try{let i=await this.#n(t);i&&i.sequence>c&&(c=i.sequence,await this.#e.onProgress(i));}catch(i){await this.#e.onProgressError?.(i instanceof Error?i:new Error(String(i)));}}return await o}async invoke(t){let r=t.operation==="health"?this.config.functions.render:this.config.functions[t.operation];if(!r)throw new Error(`Remote worker configuration does not provide a ${t.operation} function. Redeploy the Lambda workers and reinstall remote-workers.json.`);let n=await this.#t(t,r),s=n.Payload?JSON.parse(new TextDecoder().decode(n.Payload)):null;if(n.FunctionError)throw new Error(`Remote ${t.operation} worker failed (${n.FunctionError}): ${JSON.stringify(s)}`);if(!s||typeof s!="object")throw new Error(`Remote ${t.operation} worker returned no JSON response.`);let o=s;if(o.schemaVersion!==ee||o.operation!==t.operation||o.jobId!==t.jobId||o.taskId!==t.taskId)throw new Error(`Remote ${t.operation} worker returned a mismatched response.`);return o}async readResult(t){if(!t.resultKey)throw new Error(`Remote ${t.operation} response does not contain a resultKey.`);let r=je(t.resultKey,this.config.workspace.prefix),n=await this.s3.send(new GetObjectCommand({Bucket:this.config.workspace.bucket,Key:r}));if(!n.Body)throw new Error(`Remote worker result has no body: ${r}`);return JSON.parse(await n.Body.transformToString("utf8"))}async readObject(t){if(t.bucket!==this.config.workspace.bucket)throw new Error(`Remote worker object bucket mismatch: ${t.bucket}.`);let r=je(t.key,this.config.workspace.prefix),n=await this.s3.send(new GetObjectCommand({Bucket:this.config.workspace.bucket,Key:r,...t.versionId?{VersionId:t.versionId}:{}}));if(!n.Body)throw new Error(`Remote worker object has no body: ${r}`);let s=await n.Body.transformToByteArray();if(s.byteLength!==t.bytes)throw new Error(`Remote worker object length mismatch for ${r}: expected ${t.bytes}, received ${s.byteLength}.`);if(ve(s)!==t.sha256)throw new Error(`Remote worker object checksum mismatch for ${r}.`);return s}async readObjectText(t){return new TextDecoder("utf8",{fatal:true}).decode(await this.readObject(t))}async health(t){let r=this.config.functions[t];if(!r)throw new Error(`Remote worker configuration does not provide a ${t} function.`);let n=`${t}-${Date.now()}`,s=await this.lambda.send(new InvokeCommand({FunctionName:r,InvocationType:"RequestResponse",Payload:Buffer.from(JSON.stringify({schemaVersion:ee,operation:"health",jobId:"health",taskId:n}),"utf8")})),o=s.Payload?JSON.parse(new TextDecoder().decode(s.Payload)):null;if(s.FunctionError||!o||typeof o!="object")throw new Error(`Remote ${t} health check failed: ${JSON.stringify(o)}`);return o}async writeWorkspaceObject(t){let r=je(t.key,this.config.workspace.prefix),n=typeof t.body=="string"?Buffer.from(t.body,"utf8"):t.body,s=ve(n),o=await this.s3.send(new PutObjectCommand({Bucket:this.config.workspace.bucket,Key:r,Body:n,ContentType:t.contentType,...t.cacheControl?{CacheControl:t.cacheControl}:{},Metadata:{sha256:s,...t.metadata??{}}}));return {bucket:this.config.workspace.bucket,key:r,sha256:s,bytes:n.byteLength,contentType:t.contentType,...o.ETag?{etag:o.ETag}:{},...o.VersionId?{versionId:o.VersionId}:{}}}async copyWorkspaceObject(t,r){if(t.bucket!==this.config.workspace.bucket)throw new Error(`Remote worker object bucket mismatch: ${t.bucket}.`);let n=je(t.key,this.config.workspace.prefix),s=je(r,this.config.workspace.prefix),o=`${t.bucket}/${n.split("/").map(c=>encodeURIComponent(c)).join("/")}`,a=await this.s3.send(new CopyObjectCommand({Bucket:t.bucket,Key:s,CopySource:o,MetadataDirective:"COPY"}));return {...t,key:s,...a.CopyObjectResult?.ETag?{etag:a.CopyObjectResult.ETag}:{},...a.VersionId?{versionId:a.VersionId}:{}}}};function er(e){return Number.isInteger(e)&&e>=500&&e<=599}var nn=3;function tr(e){return Math.min(3e4,5e3*2**Math.max(0,e-1))}var we=class extends Error{constructor(r,n,s,o){let a=`Page failed after ${s} attempt(s), last server response HTTP ${n}: ${r}`;super(a+(o&&o!==a?`; ${o}`:""));this.url=r;this.status=n;this.attempts=s;this.name="PageHttpError";}url;status;attempts};function $s(e){if(e.size===0)return;let t=[...e.values()].slice(0,5).map(r=>`HTTP ${r.status}: ${r.url}`).join("; ");throw new Error(`Crawl incomplete: ${e.size} page(s) failed after HTTP 5xx retries. Deployment is blocked; no complete deploy plan was produced. ${t}`)}function la(e,t,r){if(!e||typeof e!="object")throw new Error("Remote render result must be a JSON object.");let n=e;if(n.schemaVersion!==ee||!n.response||n.response.operation!=="render"||n.response.jobId!==t.jobId||n.response.taskId!==t.taskId||!n.detail||!Array.isArray(n.detail.pages)||n.detail.pages.length!==r.length)throw new Error("Remote render result has an invalid envelope.");for(let[s,o]of r.entries())if(n.detail.pages[s]?.requestedUrl!==o)throw new Error("Remote render result does not match the requested URLs.");return n}function sn(e){return e instanceof Error?e.message:String(e)}var rr=class{#e;#n;#t;#s;#c;#l;#u;#r=[];#d=0;#o=0;#i=false;constructor(t){this.#e=t.client,this.#n=te(t.jobId,"jobId"),this.#t=Math.max(1,Math.min(5,t.maxAttempts??2)),this.#s=t.onRetry,this.#u=t.sleep??setTimeout$1,this.#c=Math.max(1,Math.min(20,t.batchSize??1)),this.#l=Math.max(1,Math.min(100,t.maxConcurrentBatches??1));}async renderBatch(t){let r=[...new Set(t.urls)];if(r.length<1||r.length>20)throw new Error("Remote render batches require between 1 and 20 unique URLs.");let n=new Map,s=new Map,o=new Map,a=new Map,c=r;for(let i=1;i<=this.#t&&c.length>0;i+=1){this.#d+=1;let d=te(`render-${this.#d}-${ve(c.join(`
11
- `)).slice(0,24)}-a${i}`,"taskId"),u=[];try{let l=await this.#e.invoke({schemaVersion:ee,operation:"render",jobId:this.#n,taskId:d,sourceOrigin:new URL(t.sourceOrigin).origin,urls:c,options:t.options}),g=la(await this.#e.readResult(l),l,c);for(let p of g.detail.pages){if(o.delete(p.requestedUrl),er(p.httpStatus)){let f=p.httpStatus;o.set(p.requestedUrl,f),a.set(p.requestedUrl,f),s.set(p.requestedUrl,new we(p.requestedUrl,f,i).message),u.push(p.requestedUrl);continue}if(p.httpStatus!==null&&p.httpStatus!==void 0)a.delete(p.requestedUrl);else if(a.has(p.requestedUrl)){s.set(p.requestedUrl,p.error||"Remote render returned no HTTP status after a server error."),u.push(p.requestedUrl);continue}if(p.outcome==="failed"||p.outcome==="deferred"||!p.html){let f=p.error||`Remote render ended with ${p.outcome}.`;s.set(p.requestedUrl,f),u.push(p.requestedUrl);continue}try{n.set(p.requestedUrl,{page:p,html:t.downloadHtml===!1?null:await this.#e.readObjectText(p.html),taskId:d,attempts:i}),s.delete(p.requestedUrl);}catch(f){s.set(p.requestedUrl,sn(f)),u.push(p.requestedUrl);}}}catch(l){let g=sn(l);for(let p of c)o.delete(p),s.set(p,g),u.push(p);}if(i<this.#t){let l=u.some(g=>o.has(g))?tr(i):0;for(let g of u){let p=o.get(g);this.#s?.({url:g,attempt:i,maxAttempts:this.#t,reason:s.get(g)??"Remote render failed.",...p===void 0?{}:{status:p,delayMs:l}});}l>0&&await this.#u(l);}c=u;}return {pages:r.flatMap(i=>{let d=n.get(i);return d?[d]:[]}),failures:c.map(i=>({url:i,attempts:this.#t,error:s.get(i)??"Remote render failed.",...a.has(i)?{httpStatus:a.get(i)}:{}}))}}async render(t){let r=JSON.stringify({sourceOrigin:new URL(t.sourceOrigin).origin,options:t.options,downloadHtml:t.downloadHtml!==false});return await new Promise((n,s)=>{this.#r.push({input:t,key:r,resolve:n,reject:s}),this.#a();})}#a(){this.#i||this.#r.length===0||(this.#i=true,setTimeout(()=>{this.#i=false,this.#p();},0));}#p(){for(;this.#o<this.#l&&this.#r.length>0;){let t=this.#r.shift();if(!t)break;let r=[t];for(let n=0;n<this.#r.length&&r.length<this.#c;){let s=this.#r[n];s?.key===t.key?(r.push(s),this.#r.splice(n,1)):n+=1;}this.#o+=1,this.renderBatch({sourceOrigin:t.input.sourceOrigin,urls:r.map(n=>n.input.url),options:t.input.options,...t.input.downloadHtml===void 0?{}:{downloadHtml:t.input.downloadHtml}}).then(n=>{let s=new Map(n.pages.map(a=>[a.page.requestedUrl,a])),o=new Map(n.failures.map(a=>[a.url,a]));for(let a of r){let c=s.get(a.input.url);if(c){a.resolve(c);continue}let i=o.get(a.input.url);a.reject(i?.httpStatus===void 0?new Error(`Remote render failed after ${i?.attempts??this.#t} attempt(s) for ${a.input.url}: ${i?.error??"Unknown remote render failure."}`):new we(a.input.url,i.httpStatus,i.attempts,i.error));}}).catch(n=>{let s=sn(n);for(let o of r)o.reject(new Error(s,{cause:n}));}).finally(()=>{this.#o-=1,this.#a();});}this.#r.length>0&&this.#a();}};function ua(e,t,r){if(!e||typeof e!="object")throw new Error("Remote asset result must be a JSON object.");let n=e;if(n.schemaVersion!==ee||n.response?.operation!=="asset"||n.response.jobId!==t.jobId||n.response.taskId!==t.taskId||!n.detail||!Array.isArray(n.detail.assets)||n.detail.assets.length!==r.length)throw new Error("Remote asset result has an invalid envelope.");for(let s=0;s<r.length;s+=1)if(n.detail.assets[s]?.requestedUrl!==r[s])throw new Error("Remote asset result does not match the requested URLs.");return n}var nr=class{#e;#n;#t;#s=0;constructor(t){this.#e=t.client,this.#n=te(t.jobId,"jobId"),this.#t=Math.max(1,Math.min(5,t.maxAttempts??2));}async fetch(t){let r=[...t.urls];if(r.length<1||r.length>20)throw new Error("Remote asset batches require between 1 and 20 URLs.");let n=null;for(let s=1;s<=this.#t;s+=1){this.#s+=1;let o=te(`asset-${this.#s}-${ve(r.join(`
12
- `)).slice(0,24)}-a${s}`,"taskId");try{let a=await this.#e.invoke({schemaVersion:ee,operation:"asset",jobId:this.#n,taskId:o,urls:r,options:{ignoreHttpsErrors:t.ignoreHttpsErrors,timeoutMs:Math.max(1e3,Math.min(12e4,t.timeoutMs??6e4))}});return {assets:ua(await this.#e.readResult(a),a,r).detail.assets,taskId:o,attempts:s}}catch(a){n=a;}}throw new Error(`Remote asset batch failed after ${this.#t} attempt(s): ${n instanceof Error?n.message:String(n)}`,{cause:n})}};function Ms(e,t){let r=[];for(let n=0;n<e.length;n+=t)r.push(e.slice(n,n+t));return r}function js(e){return {sourceOrigin:e.sourceOrigin,targetOrigin:e.targetOrigin,urlRewriteMode:e.urlRewriteMode,outputDir:"/workspace",extraReplacements:e.extraReplacements,wpsuite:{...e.wpsuite?.subscriptionType?{subscriptionType:e.wpsuite.subscriptionType}:{},...e.wpsuite?.siteSettings?{siteSettings:{subscriber:e.wpsuite.siteSettings.subscriber===true}}:{}}}}function Ds(e,t){if(!e||typeof e!="object")throw new Error("Remote rewrite result must be a JSON object.");let r=e;if(r.schemaVersion!==ee||r.response?.operation!=="rewrite"||r.response.jobId!==t.jobId||r.response.taskId!==t.taskId||!r.detail||!Array.isArray(r.detail.objects))throw new Error("Remote rewrite result has an invalid envelope.");return r}async function sr(e,t,r){let n=r.options??{},s=await Zr(e,n.files);if(s.length===0)return 0;let o=te(r.jobId,"jobId"),a=Ge(r.client.config.workspace.prefix,"jobs",o,"rewrite-inputs"),c=`${a}/config.json`.replace(/\/+/g,"/"),i=`${a}/asset-map.json`.replace(/\/+/g,"/"),d=`${a}/previous-asset-map.json`.replace(/\/+/g,"/");await Promise.all([r.client.writeWorkspaceObject({key:c,body:JSON.stringify(js(e)),contentType:"application/json; charset=utf-8"}),r.client.writeWorkspaceObject({key:i,body:JSON.stringify(t),contentType:"application/json; charset=utf-8"}),r.client.writeWorkspaceObject({key:d,body:JSON.stringify(n.previousAssetMap??{}),contentType:"application/json; charset=utf-8"})]);let u=Math.max(1,Math.min(500,r.batchSize)),l=Math.ceil(s.length/u),g=Math.max(1,Math.min(Math.max(1,r.concurrency),l));await n.onStart?.({totalFiles:s.length,workerCount:g});let p=[],f=0,w=0;await Promise.all(Array.from({length:Math.min(16,s.length)},async()=>{for(;;){let O=f;if(O>=s.length)return;f+=1;let x=s[O],k=C.join(e.outputDir,x),R=Os.lookup(x),A=R?Os.contentType(R)||R:"text/plain; charset=utf-8",I=`${a}/files/${ve(x)}.txt`.replace(/\/+/g,"/");await r.client.writeWorkspaceObject({key:I,body:await D.readFile(k),contentType:A}),p[O]={relativePath:x,key:I,fullPath:k,contentType:A},w+=1,await r.onStageProgress?.({index:w,totalFiles:s.length,file:x});}}));let m=Ms(p,u),h=0,y=0,b=0,P=Array.from({length:g},async()=>{for(;;){let O=h;if(O>=m.length)return;h+=1;let x=m[O],k=null,R=null;for(let A=1;A<=Math.max(1,Math.min(5,r.maxAttempts));A+=1){let I=te(`rewrite-${O+1}-a${A}-${ve(x.map(F=>F.relativePath).join(`
13
- `)).slice(0,16)}`,"taskId"),K={schemaVersion:ee,operation:"rewrite",jobId:o,taskId:I,configKey:c,assetMapKey:i,previousAssetMapKey:d,items:x.map(F=>({inputKey:F.key,outputKey:F.key,relativePath:F.relativePath,contentType:F.contentType}))};try{let F=await r.client.invoke(K);R=Ds(await r.client.readResult(F),F);break}catch(F){k=F;}}if(!R)throw new Error(`Remote rewrite batch ${O+1}/${m.length} failed: ${k instanceof Error?k.message:String(k)}`,{cause:k});for(let A of x){let I=R.detail.objects.find(K=>K.outputKey===A.key);if(!I)throw new Error(`Remote rewrite result omitted ${A.relativePath}.`);await We(A.fullPath,Buffer.from(await r.client.readObject(I.output))),I.changed&&(b+=1),y+=1,await n.onProgress?.({index:y,totalFiles:s.length,changedTextFiles:b,file:A.relativePath,changed:I.changed});}}});return await Promise.all(P),b}async function or(e,t,r,n){let s=n.relativePaths?new Set([...n.relativePaths].map(m=>m.replace(/\\/g,"/"))):null,o=Object.values(t.objects).filter(m=>m.isText&&(!s||s.has(m.relativePath))).sort((m,h)=>m.relativePath.localeCompare(h.relativePath));if(o.length===0)return 0;let a=te(n.jobId,"jobId"),c=Ge(n.client.config.workspace.prefix,"jobs",a,"rewrite-config"),i=`${c}/config.json`.replace(/\/+/g,"/"),d=`${c}/asset-map.json`.replace(/\/+/g,"/");await Promise.all([n.client.writeWorkspaceObject({key:i,body:JSON.stringify(js(e)),contentType:"application/json; charset=utf-8"}),n.client.writeWorkspaceObject({key:d,body:JSON.stringify(r),contentType:"application/json; charset=utf-8"})]);let u=Math.max(1,Math.min(500,n.batchSize)),l=Ms(o,u),g=Math.max(1,Math.min(Math.max(1,n.concurrency),l.length)),p=0,f=0,w=0;return await Promise.all(Array.from({length:g},async()=>{for(;;){let m=p;if(m>=l.length)return;p+=1;let h=l[m],y=null,b=null;for(let P=1;P<=Math.max(1,Math.min(5,n.maxAttempts));P+=1){let O=te(`rewrite-object-${m+1}-a${P}-${ve(h.map(x=>x.relativePath).join(`
14
- `)).slice(0,16)}`,"taskId");try{let x=await n.client.invoke({schemaVersion:ee,operation:"rewrite",jobId:a,taskId:O,configKey:i,assetMapKey:d,items:h.map(k=>({inputKey:k.object.key,outputKey:k.object.key,relativePath:k.relativePath,contentType:k.contentType||k.object.contentType}))});y=Ds(await n.client.readResult(x),x);break}catch(x){b=x;}}if(!y)throw new Error(`Remote object rewrite batch ${m+1}/${l.length} failed: ${b instanceof Error?b.message:String(b)}`,{cause:b});for(let P of h){let O=y.detail.objects.find(x=>x.outputKey===P.object.key);if(!O)throw new Error(`Remote rewrite result omitted ${P.relativePath}.`);P.object=O.output,O.changed&&(w+=1),f+=1,await n.onProgress?.({index:f,totalFiles:o.length,changedTextFiles:w,file:P.relativePath,changed:O.changed});}}})),t.assetMap={...r},t.generatedAt=new Date().toISOString(),w}var ma="remote-workers.json";function ir(e={}){let t=String(e.explicitPath??"").trim();if(t)return C.resolve(t);let r=String(e.runtimeDir??process.env.STATIC_PUBLISHER_RUNTIME_DIR??process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR??"").trim();return r?C.join(C.resolve(r),ma):""}function Fs(e,t,r,n=250){let s=Math.max(1,Math.floor(n)),o=[],a=new Set;for(let c of [e,...t]){let i=c.trim();if(!(!i||a.has(i)||r(i))&&(a.add(i),o.push(i),o.length>=s))break}return o}async function Us(e,t){let r=String(e.wpsuite?.runtimeToken||"").trim();if(!r)throw new Error("Page-cache purge is enabled, but the WordPress runtime token is missing.");let n=new URL("/wp-json/smartcloud-static-publisher/v1/page-cache/purge",e.sourceOrigin).toString(),s=await t.post(n,{timeout:3e4,failOnStatusCode:false,maxRedirects:0,headers:{"content-type":"application/json","x-static-publisher-token":r},data:JSON.stringify({scope:"all"})});try{let o=await s.text();if(o.length>65536)throw new Error("WordPress page-cache purge response was too large.");let a=null;try{a=JSON.parse(o);}catch{throw new Error(`WordPress page-cache purge returned invalid JSON (HTTP ${s.status()}).`)}if(!s.ok()){let c=a&&typeof a=="object"&&"message"in a?String(a.message||""):"";throw new Error(`WordPress page-cache purge failed with HTTP ${s.status()}${c?`: ${c}`:""}`)}if(!a||typeof a!="object"||a.purged!==!0)throw new Error("WordPress page-cache provider did not confirm a complete purge.");return {purged:!0,provider:String(a.provider||"custom"),scope:"all",...typeof a.purgedAt=="string"?{purgedAt:a.purgedAt}:{}}}finally{await s.dispose().catch(()=>{});}}function ga(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?C.resolve(e):""}function ar(e){let t=String(process.env.STATIC_PUBLISHER_REMOTE_EXPORT_MANIFEST_PATH||"").trim();if(t)return C.resolve(t);let r=ga();if(r)return C.join(r,"remote-export-manifest.json");let n=C.resolve(e.outputDir);return C.join(C.dirname(n),`.${C.basename(n)}-remote-export-manifest.json`)}function cr(e){let t=C.posix.normalize(String(e||"").replace(/\\/g,"/").replace(/^\/+/,""));if(!t||t==="."||t===".."||t.startsWith("../")||/^[A-Za-z]:/.test(t))throw new Error(`Invalid remote export relative path: ${e}`);return t}function qs(e,t,r){let n=r?.workspace.bucket===t.workspace.bucket&&r.workspace.prefix===t.workspace.prefix;return {schemaVersion:1,generatedAt:new Date().toISOString(),jobId:e,workspace:{...t.workspace},objects:n?{...r.objects}:{},assetMap:n?{...r.assetMap}:{}}}function Ws(e,t){let r=e.objects[cr(t)];if(!r)return true;let n=`${Ge(e.workspace.prefix,"jobs",te(e.jobId,"jobId"))}/`;return !je(r.object.key,e.workspace.prefix).startsWith(n)}function gt(e,t){let r=cr(t.relativePath);if(t.object.bucket!==e.workspace.bucket)throw new Error(`Remote export object bucket mismatch for ${r}.`);je(t.object.key,e.workspace.prefix),e.objects[r]={...t,relativePath:r},e.generatedAt=new Date().toISOString();}function _s(e,t){delete e.objects[cr(t)],e.generatedAt=new Date().toISOString();}async function Ns(e,t){t.generatedAt=new Date().toISOString();let r=ar(e);await D.mkdir(C.dirname(r),{recursive:true}),await We(r,`${JSON.stringify(t,null,2)}
15
- `,"utf8");}async function lr(e){try{let t=JSON.parse(await D.readFile(ar(e),"utf8"));if(t.schemaVersion!==1||typeof t.jobId!="string"||!t.workspace||typeof t.workspace.bucket!="string"||typeof t.workspace.prefix!="string"||!t.objects||typeof t.objects!="object"||!t.assetMap||typeof t.assetMap!="object")return null;let r=t;for(let[n,s]of Object.entries(r.objects)){if(cr(n),!s||s.relativePath!==n||s.object?.bucket!==r.workspace.bucket)return null;je(s.object.key,r.workspace.prefix);}return r}catch{return null}}var ya=["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"],to=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"]),ro=new Set(["",".html",".htm"]),ba=new Set([".css",".xml",".xsl",".svg",".json",".html",".htm",".txt",".enc",".jws"]),Sa=["/wp-content/","/wp-includes/","/wp-admin/","/static/","/assets/","/build/","/_next/"];function Pa(e){let t=[],r="full",n="full",s=false,o=false,a="",c=null,i="";for(let d=0;d<e.length;d++){let u=e[d];if(u==="--retry-timeouts")r="retry-timeouts",s=true;else if(u==="--resume")s=true;else if(u==="--resume-rewrite")s=true,o=true;else if(u==="--crawl-mode"){let l=e[++d];if(!l)throw new Error("--crawl-mode requires a value");n=l==="incremental"?"incremental":"full",s||=n==="incremental";}else if(u.startsWith("--crawl-mode="))n=u.slice(13)==="incremental"?"incremental":"full",s||=n==="incremental";else if(u==="--incremental")n="incremental",s=true;else if(u==="--url"){let l=e[++d];if(!l)throw new Error("--url requires a value");r="single-url",s=true,t.push(l);}else if(u.startsWith("--url="))r="single-url",s=true,t.push(u.slice(6));else if(u==="--urls"){let l=e[++d];if(!l)throw new Error("--urls requires a file path");r="single-url",s=true,t.push(`@${l}`);}else if(u==="--content-sync-plan"){let l=e[++d];if(!l)throw new Error("--content-sync-plan requires a file path");r="content-sync",n="incremental",s=true,a=C.resolve(l);}else if(u.startsWith("--content-sync-plan="))r="content-sync",n="incremental",s=true,a=C.resolve(u.slice(20));else if(u==="--remote-render")c=true;else if(u==="--local-render")c=false;else if(u==="--remote-worker-config"){let l=e[++d];if(!l)throw new Error("--remote-worker-config requires a file path");i=C.resolve(l);}else u.startsWith("--remote-worker-config=")&&(i=C.resolve(u.slice(23)));}return {mode:r,crawlMode:n,urls:t,preserveOutput:s,resumeRewrite:o,contentSyncPlanPath:a,remoteRender:c,remoteWorkerConfigPath:i}}function Dt(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?C.resolve(e):""}function Ra(){let e=Dt();return e?C.join(e,"privacy","cookie-observations.json"):null}function va(){let e=Dt();return e?C.resolve(e,".."):""}function Ca(){let e=process.env.STATIC_PUBLISHER_WP_ROOT||process.env.WPSUITE_STATIC_PUBLISHER_WP_ROOT||"";return e.trim()?C.resolve(e):""}function ka(e){let t=e.trim(),r=[{alias:"@storage-root",root:va(),requiredEnv:"STATIC_PUBLISHER_RUNTIME_DIR or WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR"},{alias:"@runtime",root:Dt(),requiredEnv:"STATIC_PUBLISHER_RUNTIME_DIR or WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR"},{alias:"@wp-root",root:Ca(),requiredEnv:"STATIC_PUBLISHER_WP_ROOT or WPSUITE_STATIC_PUBLISHER_WP_ROOT"}];for(let n of r){if(t!==n.alias&&!t.startsWith(`${n.alias}/`))continue;if(!n.root)return {resolvedPath:null,alias:n.alias,requiredEnv:n.requiredEnv};let s=t.slice(n.alias.length).replace(/^\/+/,"");return {resolvedPath:s?C.resolve(n.root,s):n.root,alias:n.alias}}return {resolvedPath:C.resolve(t)}}function no(e){let t=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(t)return C.resolve(t);let r=Dt();return r?C.join(r,"crawl-manifest.json"):C.join(e.outputDir,".crawl-manifest.json")}function so(){return {schemaVersion:6,updatedAt:"",pages:{},assets:{}}}function Vs(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 xa(e){let t=e.isText===true;return {url:String(e.url||""),outputPath:String(e.outputPath||""),isText:t,contentType:typeof e.contentType=="string"&&e.contentType?e.contentType:null,cacheControl:typeof e.cacheControl=="string"&&e.cacheControl?e.cacheControl:null,changeToken:typeof e.changeToken=="string"?e.changeToken:null,tokenSource:typeof e.tokenSource=="string"?e.tokenSource:null,discoveredAssets:Array.isArray(e.discoveredAssets)?e.discoveredAssets.map(r=>String(r)):[],lastCrawledAt:String(e.lastCrawledAt||""),lastSeenRunId:String(e.lastSeenRunId||""),unreferencedRuns:Math.max(0,Number(e.unreferencedRuns)||0),rewriteComplete:e.rewriteComplete===true||!t}}function ln(e){let t=e?.["content-type"]?.trim();return t||null}function un(e){let t=e?.["cache-control"]?.trim();return t||null}function Ia(e){return e?e.split(",").map(t=>t.trim().toLowerCase()).some(t=>t==="no-cache"||t==="no-store"||/^max-age\s*=\s*0$/.test(t)):false}function Ta(){return {rewriteTargets:new Set,changedFiles:new Set,deletedFiles:new Set,revalidateFiles:new Set}}function ft(e,t,r,n){let s=me(t.outputDir,r);s&&(Ia(n)?e.revalidateFiles.add(s):e.revalidateFiles.delete(s));}function le(e,t,r){let n=me(t.outputDir,r);n&&(e.changedFiles.add(n),e.deletedFiles.delete(n));}function dn(e,t,r){let n=me(t.outputDir,r);n&&(e.deletedFiles.add(n),e.changedFiles.delete(n),e.revalidateFiles.delete(n));}function Ce(e,t){e.rewriteTargets.add(C.resolve(t));}async function Aa(e){try{let t=await D.readFile(no(e),"utf8"),r=JSON.parse(t);if(r&&typeof r=="object"&&r.pages&&typeof r.pages=="object"){if((r.schemaVersion===2||r.schemaVersion===3||r.schemaVersion===4||r.schemaVersion===5||r.schemaVersion===6)&&r.assets&&typeof r.assets=="object")return {schemaVersion:6,updatedAt:String(r.updatedAt||""),pages:Object.fromEntries(Object.entries(r.pages).map(([n,s])=>[n,Vs(s)])),assets:Object.fromEntries(Object.entries(r.assets).map(([n,s])=>[n,xa(s)]))};if(r.schemaVersion===1)return {schemaVersion:6,updatedAt:String(r.updatedAt||""),pages:Object.fromEntries(Object.entries(r.pages).map(([n,s])=>[n,Vs(s)])),assets:{}}}}catch{}return so()}function pn(e){return JSON.parse(JSON.stringify(e))}async function Ea(e,t){let r=no(e);await D.mkdir(C.dirname(r),{recursive:true}),await D.writeFile(r,JSON.stringify(t,null,2),"utf8");}async function He(e,t,r=false){if(!t.trackManifest)return;t.manifestPendingChanges+=1;let n=Date.now();if(!r&&t.manifestPendingChanges<25&&n-t.lastManifestSavedAt<5e3)return;t.manifestPendingChanges=0,t.lastManifestSavedAt=n;let s=async()=>{let o=pn(t.manifest);o.updatedAt=new Date().toISOString(),t.manifest.updatedAt=o.updatedAt,await Ea(e,o);};t.manifestSaveChain=t.manifestSaveChain.then(s,s),await t.manifestSaveChain;}function rt(e){if(!e)return null;let t=e.trim();return t||null}function $a(e,t){let r=new Set,n=new Set([...Object.keys(e),...Object.keys(t)]);for(let s of n){let o=e[s],a=t[s];!o||!a||o===a||r.add(s);}return r}async function oo(e,t,r,n){let s=[...new Set(r.map(c=>c.trim()).filter(Boolean))],o=String(e.wpsuite?.runtimeToken||"").trim();if(s.length===0||!o)return new Map;let a=new URL("/wp-json/smartcloud-static-publisher/v1/change-tokens",e.sourceOrigin).toString();try{let c=await t.post(a,{timeout:3e4,failOnStatusCode:!1,headers:{"content-type":"application/json","x-static-publisher-token":o},data:JSON.stringify({urls:s})});if(!c.ok())return n.warn(`Change token lookup failed with HTTP ${c.status()}`,{endpoint:a,status:c.status()}),new Map;let i=await c.json().catch(()=>null);if(!i||!Array.isArray(i.items))return new Map;let d=new Map;for(let u of i.items)!u||typeof u.url!="string"||d.set(u.url,u);return d}catch(c){return n.warn("Change token lookup failed; falling back to sitemap metadata",{endpoint:a,error:c instanceof Error?`${c.name}: ${c.message.split(/\r?\n/u,1)[0]}`:"Unknown change-token request failure"}),new Map}}async function dr(e,t,r,n,s){if(!r.enabled&&!r.trackManifest)return null;if(r.changeTokenCache.has(n))return r.changeTokenCache.get(n)??null;let a=(await oo(e,t,[n],s)).get(n)??null;return yr(e,n)&&(!a||a.supported!==true||!a.token)&&(a=await ja(e,t,n,s)),a=io(e,a),r.changeTokenCache.set(n,a),a}async function bn(e,t,r,n,s){if(!r.trackManifest)return;let o=[...new Set(n.map(a=>a.trim()).filter(Boolean))].filter(a=>!r.changeTokenCache.has(a));for(let a=0;a<o.length;a+=250){let c=o.slice(a,a+250),i=await oo(e,t,c,s);for(let d of c)r.changeTokenCache.set(d,io(e,i.get(d)??null));}}async function Oa(e,t,r,n,s,o){if(!(!n.trackManifest||n.changeTokenCache.has(s)))for(;!n.changeTokenCache.has(s);){if(!n.pageChangeTokenPrimePromise){let a=Fs(s,r.pageQueue,i=>n.changeTokenCache.has(i));if(a.length===0)return;o.info(`Loading ${a.length} page change tokens in bulk.`,{phase:"prime-page-change-tokens",batchSize:a.length,remainingPages:r.pageQueue.length});let c=bn(e,t,n,a,o).finally(()=>{n.pageChangeTokenPrimePromise===c&&(n.pageChangeTokenPrimePromise=null);});n.pageChangeTokenPrimePromise=c;}await n.pageChangeTokenPrimePromise;}}function Ma(e){return `subscriber=${e.wpsuite?.siteSettings?.subscriber===true?"1":"0"}`}function io(e,t){if(!t?.supported||!t.token||t.tokenSource==="wp-media-file")return t;let r=Ma(e);return {...t,token:Ee(`${t.token}
16
- ${r}`,40),tokenSource:t.tokenSource?`${t.tokenSource}+site-subscriber`:"site-subscriber"}}async function ja(e,t,r,n){try{let s=await t.get(r,{timeout:Math.min(e.navigationTimeoutMs,15e3),failOnStatusCode:!1});if(s.status()!==404)return n.warn(`Generated 404 fallback token skipped for ${r}`,{url:r,status:s.status(),requestPath:e.generated404RequestPath,source:"generated-404-change-token-fallback"}),null;let o=await s.text().catch(()=>""),a=Ee(`${r}
9
+ ${e.path}`}function Is(e,t){let r;try{r=new URL(t);}catch{return null}let n=e.split(";"),s=n.shift()?.trim()??"",o=s.indexOf("=");if(o<=0)return null;let a=s.slice(0,o).trim();if(!a||[...a].some(w=>w<=" "||"=;,".includes(w)))return null;let c=r.hostname.toLowerCase(),i=Ji(r),d=null,u=true,l=false,g=false,p="Unspecified",f=false;for(let w of n){let m=w.indexOf("="),h=(m<0?w:w.slice(0,m)).trim().toLowerCase(),y=m<0?"":w.slice(m+1).trim();if(h==="domain"&&y)c=y.toLowerCase();else if(h==="path"&&y.startsWith("/"))i=y;else if(h==="expires"){let S=Date.parse(y);Number.isFinite(S)&&(d=Math.floor(S/1e3),u=false);}else if(h==="max-age"&&/^-?\d+$/u.test(y)){let S=Number.parseInt(y,10);d=Math.floor(Date.now()/1e3)+S,u=false;}else h==="secure"?l=true:h==="httponly"?g=true:h==="samesite"?p=xs(y):h==="partitioned"&&(f=true);}return {name:a,domain:c,path:i,expires:d,session:u,secure:l,httpOnly:g,sameSite:p,partitioned:f}}function Qi(e){let t=!Number.isFinite(e.expires)||e.expires<=0;return {name:e.name,domain:e.domain.toLowerCase(),path:e.path||"/",expires:t?null:e.expires,session:t,secure:e.secure,httpOnly:e.httpOnly,sameSite:xs(e.sameSite),partitioned:e.partitioned===true}}function Ts(e,t){let r=new Map(e.map(n=>[It(n),n.value]));return t.filter(n=>r.get(It(n))!==n.value).map(Qi)}function Gi(e,t){let r=e.session||t.session;return {...e,expires:r?null:Math.max(e.expires??0,t.expires??0)||null,session:r,secure:e.secure&&t.secure,httpOnly:e.httpOnly&&t.httpOnly,sameSite:e.sameSite===t.sameSite?e.sameSite:"Unspecified",partitioned:e.partitioned&&t.partitioned}}var Xt=class{#e=new Map;add(t){if(!t)return;let r={...t,domain:t.domain.toLowerCase(),path:t.path||"/"},n=It(r),s=this.#e.get(n);this.#e.set(n,s?Gi(s,r):r);}addAll(t){for(let r of t)this.add(r);}artifact(){return {schemaVersion:1,observations:[...this.#e.values()].sort((t,r)=>It(t).localeCompare(It(r)))}}};async function er(e,t){await F.mkdir(k.dirname(e),{recursive:true});let r=`${e}.${process.pid}.tmp`;await F.writeFile(r,`${JSON.stringify(t,null,2)}
10
+ `,{encoding:"utf8",mode:384}),await F.rename(r,e);}function Tt(e){let t=e;for(let n=0;n<3;n+=1){if(/%(?:2f|5c)/i.test(t))return null;try{let s=decodeURIComponent(t);if(s===t)break;t=s;}catch{return null}}return t.includes("\0")||t.includes("\\")||t.split("/").some(n=>n==="."||n==="..")?null:t}function At(e,t){let r=Tt(t);if(r===null)throw new Error(`Unsafe output URL pathname: ${t}`);let n=k.resolve(e),s=k.resolve(n,r.replace(/^\/+/,""));if(s!==n&&!s.startsWith(`${n}${k.sep}`))throw new Error(`Output path escapes configured root: ${t}`);return s}var re=1,As=/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;function ne(e,t){let r=String(e??"").trim();if(!As.test(r))throw new Error(`${t} must match ${As.source} and be at most 128 characters.`);return r}function Es(e){let t=String(e??"").trim().replace(/^\/+|\/+$/g,"");if(!t)return "";let r=t.split("/").filter(Boolean);if(r.some(n=>n==="."||n===".."))throw new Error("S3 prefix contains an unsafe dot path segment.");return `${r.join("/")}/`}function Ze(e,...t){let r=Es(e),n=t.map((s,o)=>{let a=String(s??"").replace(/^\/+|\/+$/g,"");if(!a||a.split("/").some(c=>c==="."||c===".."))throw new Error(`Unsafe S3 key segment at index ${o}.`);return a});return `${r}${n.join("/")}`}function Ce(e){return createHash("sha256").update(e).digest("hex")}function Fe(e,t){let r=String(e??"").replace(/^\/+/,""),n=Es(t);if(!r||r.includes("\0"))throw new Error("S3 object key is empty or invalid.");if(n&&!r.startsWith(n))throw new Error(`S3 object key is outside the configured workspace prefix: ${r}`);if(r.split("/").some(s=>s===".."))throw new Error("S3 object key contains an unsafe parent segment.");return r}function ce(e,t,{optional:r=false}={}){if(!(r&&(e==null||e===""))){if(typeof e!="string"||!e.trim())throw new Error(`${t} must be a non-empty string.`);return e.trim()}}function aa(e){if(!e||typeof e!="object")throw new Error("Remote worker config must be a JSON object.");let t=e;if(t.schemaVersion!==1)throw new Error(`Unsupported remote worker config version: ${String(t.schemaVersion)}.`);if(t.protocolVersion!==re)throw new Error(`Remote worker protocol mismatch: expected ${re}, received ${String(t.protocolVersion)}.`);let r=t.workspace,n=t.functions;if(!r||!n)throw new Error("Remote worker config requires workspace and functions objects.");return {schemaVersion:1,region:ce(t.region,"region"),...ce(t.roleArn,"roleArn",{optional:true})?{roleArn:ce(t.roleArn,"roleArn")}:{},...ce(t.roleSessionName,"roleSessionName",{optional:true})?{roleSessionName:ce(t.roleSessionName,"roleSessionName")}:{},workspace:{bucket:ce(r.bucket,"workspace.bucket"),prefix:String(r.prefix??"")},...t.status&&typeof t.status=="object"?{status:{tableName:ce(t.status.tableName,"status.tableName")}}:{},functions:{render:ce(n.render,"functions.render"),...ce(n.asset,"functions.asset",{optional:true})?{asset:ce(n.asset,"functions.asset")}:{},rewrite:ce(n.rewrite,"functions.rewrite"),"deploy-copy":ce(n["deploy-copy"],"functions.deploy-copy")},targets:Array.isArray(t.targets)?t.targets.map((s,o)=>{if(!s||typeof s!="object")throw new Error(`targets[${o}] must be an object.`);let a=s;return {id:ce(a.id,`targets[${o}].id`),bucketName:ce(a.bucketName,`targets[${o}].bucketName`),prefix:String(a.prefix??""),region:ce(a.region,`targets[${o}].region`)}}):[],protocolVersion:re}}async function tr(e){let t=k.resolve(e);try{return aa(JSON.parse(await readFile(t,"utf8")))}catch(r){throw new Error(`Unable to load remote worker config ${t}: ${r instanceof Error?r.message:String(r)}`,{cause:r})}}var mt=class{config;lambda;s3;dynamodb;#e;constructor(t,r={},n={}){this.config=t;let s=t.roleArn?fromTemporaryCredentials({params:{RoleArn:t.roleArn,RoleSessionName:t.roleSessionName??"publisher-exporter-remote-worker"},clientConfig:{region:t.region}}):void 0;this.lambda=new LambdaClient({region:t.region,...s?{credentials:s}:{},...r}),this.s3=new S3Client({region:t.region,...s?{credentials:s}:{}}),this.dynamodb=t.status?new DynamoDBClient({region:t.region,...s?{credentials:s}:{}}):null,this.#e=n;}async#n(t){if(!this.dynamodb||!this.config.status)return null;let n=(await this.dynamodb.send(new GetItemCommand({TableName:this.config.status.tableName,ConsistentRead:true,Key:{jobId:{S:t.jobId},taskId:{S:t.taskId}}}))).Item;if(!n)return null;let s=n.operation?.S,o=n.status?.S;return !s||!["render","asset","rewrite","deploy-copy"].includes(s)||!o||!["running","succeeded","partial","failed"].includes(o)?null:{jobId:n.jobId?.S??t.jobId,taskId:n.taskId?.S??t.taskId,operation:s,status:o,completedItems:Number(n.completedItems?.N??0),failedItems:Number(n.failedItems?.N??0),totalItems:Number(n.totalItems?.N??0),sequence:Number(n.sequence?.N??0),updatedAt:n.updatedAt?.S??new Date(0).toISOString(),requestId:n.requestId?.S??null}}async#t(t,r){let n=this.lambda.send(new InvokeCommand({FunctionName:r,InvocationType:"RequestResponse",Payload:Buffer.from(JSON.stringify(t),"utf8")}));if(t.operation==="health"||!this.dynamodb||!this.#e.onProgress)return await n;let s=false,o=n.finally(()=>{s=true;}),a=Math.max(500,Math.min(3e4,this.#e.progressPollIntervalMs??2e3)),c=-1;for(;!s;){await Promise.race([o.then(()=>{},()=>{}),new Promise(i=>setTimeout(i,a))]);try{let i=await this.#n(t);i&&i.sequence>c&&(c=i.sequence,await this.#e.onProgress(i));}catch(i){await this.#e.onProgressError?.(i instanceof Error?i:new Error(String(i)));}}return await o}async invoke(t){let r=t.operation==="health"?this.config.functions.render:this.config.functions[t.operation];if(!r)throw new Error(`Remote worker configuration does not provide a ${t.operation} function. Redeploy the Lambda workers and reinstall remote-workers.json.`);let n=await this.#t(t,r),s=n.Payload?JSON.parse(new TextDecoder().decode(n.Payload)):null;if(n.FunctionError)throw new Error(`Remote ${t.operation} worker failed (${n.FunctionError}): ${JSON.stringify(s)}`);if(!s||typeof s!="object")throw new Error(`Remote ${t.operation} worker returned no JSON response.`);let o=s;if(o.schemaVersion!==re||o.operation!==t.operation||o.jobId!==t.jobId||o.taskId!==t.taskId)throw new Error(`Remote ${t.operation} worker returned a mismatched response.`);return o}async readResult(t){if(!t.resultKey)throw new Error(`Remote ${t.operation} response does not contain a resultKey.`);let r=Fe(t.resultKey,this.config.workspace.prefix),n=await this.s3.send(new GetObjectCommand({Bucket:this.config.workspace.bucket,Key:r}));if(!n.Body)throw new Error(`Remote worker result has no body: ${r}`);return JSON.parse(await n.Body.transformToString("utf8"))}async readObject(t){if(t.bucket!==this.config.workspace.bucket)throw new Error(`Remote worker object bucket mismatch: ${t.bucket}.`);let r=Fe(t.key,this.config.workspace.prefix),n=await this.s3.send(new GetObjectCommand({Bucket:this.config.workspace.bucket,Key:r,...t.versionId?{VersionId:t.versionId}:{}}));if(!n.Body)throw new Error(`Remote worker object has no body: ${r}`);let s=await n.Body.transformToByteArray();if(s.byteLength!==t.bytes)throw new Error(`Remote worker object length mismatch for ${r}: expected ${t.bytes}, received ${s.byteLength}.`);if(Ce(s)!==t.sha256)throw new Error(`Remote worker object checksum mismatch for ${r}.`);return s}async readObjectText(t){return new TextDecoder("utf8",{fatal:true}).decode(await this.readObject(t))}async health(t){let r=this.config.functions[t];if(!r)throw new Error(`Remote worker configuration does not provide a ${t} function.`);let n=`${t}-${Date.now()}`,s=await this.lambda.send(new InvokeCommand({FunctionName:r,InvocationType:"RequestResponse",Payload:Buffer.from(JSON.stringify({schemaVersion:re,operation:"health",jobId:"health",taskId:n}),"utf8")})),o=s.Payload?JSON.parse(new TextDecoder().decode(s.Payload)):null;if(s.FunctionError||!o||typeof o!="object")throw new Error(`Remote ${t} health check failed: ${JSON.stringify(o)}`);return o}async writeWorkspaceObject(t){let r=Fe(t.key,this.config.workspace.prefix),n=typeof t.body=="string"?Buffer.from(t.body,"utf8"):t.body,s=Ce(n),o=await this.s3.send(new PutObjectCommand({Bucket:this.config.workspace.bucket,Key:r,Body:n,ContentType:t.contentType,...t.cacheControl?{CacheControl:t.cacheControl}:{},Metadata:{sha256:s,...t.metadata??{}}}));return {bucket:this.config.workspace.bucket,key:r,sha256:s,bytes:n.byteLength,contentType:t.contentType,...o.ETag?{etag:o.ETag}:{},...o.VersionId?{versionId:o.VersionId}:{}}}async copyWorkspaceObject(t,r){if(t.bucket!==this.config.workspace.bucket)throw new Error(`Remote worker object bucket mismatch: ${t.bucket}.`);let n=Fe(t.key,this.config.workspace.prefix),s=Fe(r,this.config.workspace.prefix),o=`${t.bucket}/${n.split("/").map(c=>encodeURIComponent(c)).join("/")}`,a=await this.s3.send(new CopyObjectCommand({Bucket:t.bucket,Key:s,CopySource:o,MetadataDirective:"COPY"}));return {...t,key:s,...a.CopyObjectResult?.ETag?{etag:a.CopyObjectResult.ETag}:{},...a.VersionId?{versionId:a.VersionId}:{}}}};function rr(e){return Number.isInteger(e)&&e>=500&&e<=599}var an=3;function nr(e){return Math.min(3e4,5e3*2**Math.max(0,e-1))}var ye=class extends Error{constructor(r,n,s,o){let a=`Page failed after ${s} attempt(s), last server response HTTP ${n}: ${r}`;super(a+(o&&o!==a?`; ${o}`:""));this.url=r;this.status=n;this.attempts=s;this.name="PageHttpError";}url;status;attempts};function Ms(e){if(e.size===0)return;let t=[...e.values()].slice(0,5).map(r=>`HTTP ${r.status}: ${r.url}`).join("; ");throw new Error(`Crawl incomplete: ${e.size} page(s) failed after HTTP 5xx retries. Deployment is blocked; no complete deploy plan was produced. ${t}`)}function la(e,t,r){if(!e||typeof e!="object")throw new Error("Remote render result must be a JSON object.");let n=e;if(n.schemaVersion!==re||!n.response||n.response.operation!=="render"||n.response.jobId!==t.jobId||n.response.taskId!==t.taskId||!n.detail||!Array.isArray(n.detail.pages)||n.detail.pages.length!==r.length)throw new Error("Remote render result has an invalid envelope.");for(let[s,o]of r.entries())if(n.detail.pages[s]?.requestedUrl!==o)throw new Error("Remote render result does not match the requested URLs.");return n}function cn(e){return e instanceof Error?e.message:String(e)}var sr=class{#e;#n;#t;#s;#c;#l;#u;#r=[];#d=0;#o=0;#i=false;constructor(t){this.#e=t.client,this.#n=ne(t.jobId,"jobId"),this.#t=Math.max(1,Math.min(5,t.maxAttempts??2)),this.#s=t.onRetry,this.#u=t.sleep??setTimeout$1,this.#c=Math.max(1,Math.min(20,t.batchSize??1)),this.#l=Math.max(1,Math.min(100,t.maxConcurrentBatches??1));}async renderBatch(t){let r=[...new Set(t.urls)];if(r.length<1||r.length>20)throw new Error("Remote render batches require between 1 and 20 unique URLs.");let n=new Map,s=new Map,o=new Map,a=new Map,c=r;for(let i=1;i<=this.#t&&c.length>0;i+=1){this.#d+=1;let d=ne(`render-${this.#d}-${Ce(c.join(`
11
+ `)).slice(0,24)}-a${i}`,"taskId"),u=[];try{let l=await this.#e.invoke({schemaVersion:re,operation:"render",jobId:this.#n,taskId:d,sourceOrigin:new URL(t.sourceOrigin).origin,urls:c,options:t.options}),g=la(await this.#e.readResult(l),l,c);for(let p of g.detail.pages){if(o.delete(p.requestedUrl),rr(p.httpStatus)){let f=p.httpStatus;o.set(p.requestedUrl,f),a.set(p.requestedUrl,f),s.set(p.requestedUrl,new ye(p.requestedUrl,f,i).message),u.push(p.requestedUrl);continue}if(p.httpStatus!==null&&p.httpStatus!==void 0)a.delete(p.requestedUrl);else if(a.has(p.requestedUrl)){s.set(p.requestedUrl,p.error||"Remote render returned no HTTP status after a server error."),u.push(p.requestedUrl);continue}if(p.outcome==="failed"||p.outcome==="deferred"||!p.html){let f=p.error||`Remote render ended with ${p.outcome}.`;s.set(p.requestedUrl,f),u.push(p.requestedUrl);continue}try{n.set(p.requestedUrl,{page:p,html:t.downloadHtml===!1?null:await this.#e.readObjectText(p.html),taskId:d,attempts:i}),s.delete(p.requestedUrl);}catch(f){s.set(p.requestedUrl,cn(f)),u.push(p.requestedUrl);}}}catch(l){let g=cn(l);for(let p of c)o.delete(p),s.set(p,g),u.push(p);}if(i<this.#t){let l=u.some(g=>o.has(g))?nr(i):0;for(let g of u){let p=o.get(g);this.#s?.({url:g,attempt:i,maxAttempts:this.#t,reason:s.get(g)??"Remote render failed.",...p===void 0?{}:{status:p,delayMs:l}});}l>0&&await this.#u(l);}c=u;}return {pages:r.flatMap(i=>{let d=n.get(i);return d?[d]:[]}),failures:c.map(i=>({url:i,attempts:this.#t,error:s.get(i)??"Remote render failed.",...a.has(i)?{httpStatus:a.get(i)}:{}}))}}async render(t){let r=JSON.stringify({sourceOrigin:new URL(t.sourceOrigin).origin,options:t.options,downloadHtml:t.downloadHtml!==false});return await new Promise((n,s)=>{this.#r.push({input:t,key:r,resolve:n,reject:s}),this.#a();})}#a(){this.#i||this.#r.length===0||(this.#i=true,setTimeout(()=>{this.#i=false,this.#p();},0));}#p(){for(;this.#o<this.#l&&this.#r.length>0;){let t=this.#r.shift();if(!t)break;let r=[t];for(let n=0;n<this.#r.length&&r.length<this.#c;){let s=this.#r[n];s?.key===t.key?(r.push(s),this.#r.splice(n,1)):n+=1;}this.#o+=1,this.renderBatch({sourceOrigin:t.input.sourceOrigin,urls:r.map(n=>n.input.url),options:t.input.options,...t.input.downloadHtml===void 0?{}:{downloadHtml:t.input.downloadHtml}}).then(n=>{let s=new Map(n.pages.map(a=>[a.page.requestedUrl,a])),o=new Map(n.failures.map(a=>[a.url,a]));for(let a of r){let c=s.get(a.input.url);if(c){a.resolve(c);continue}let i=o.get(a.input.url);a.reject(i?.httpStatus===void 0?new Error(`Remote render failed after ${i?.attempts??this.#t} attempt(s) for ${a.input.url}: ${i?.error??"Unknown remote render failure."}`):new ye(a.input.url,i.httpStatus,i.attempts,i.error));}}).catch(n=>{let s=cn(n);for(let o of r)o.reject(new Error(s,{cause:n}));}).finally(()=>{this.#o-=1,this.#a();});}this.#r.length>0&&this.#a();}};function ua(e,t,r){if(!e||typeof e!="object")throw new Error("Remote asset result must be a JSON object.");let n=e;if(n.schemaVersion!==re||n.response?.operation!=="asset"||n.response.jobId!==t.jobId||n.response.taskId!==t.taskId||!n.detail||!Array.isArray(n.detail.assets)||n.detail.assets.length!==r.length)throw new Error("Remote asset result has an invalid envelope.");for(let s=0;s<r.length;s+=1)if(n.detail.assets[s]?.requestedUrl!==r[s])throw new Error("Remote asset result does not match the requested URLs.");return n}var or=class{#e;#n;#t;#s=0;constructor(t){this.#e=t.client,this.#n=ne(t.jobId,"jobId"),this.#t=Math.max(1,Math.min(5,t.maxAttempts??2));}async fetch(t){let r=[...t.urls];if(r.length<1||r.length>20)throw new Error("Remote asset batches require between 1 and 20 URLs.");let n=null;for(let s=1;s<=this.#t;s+=1){this.#s+=1;let o=ne(`asset-${this.#s}-${Ce(r.join(`
12
+ `)).slice(0,24)}-a${s}`,"taskId");try{let a=await this.#e.invoke({schemaVersion:re,operation:"asset",jobId:this.#n,taskId:o,urls:r,options:{ignoreHttpsErrors:t.ignoreHttpsErrors,timeoutMs:Math.max(1e3,Math.min(12e4,t.timeoutMs??6e4))}});return {assets:ua(await this.#e.readResult(a),a,r).detail.assets,taskId:o,attempts:s}}catch(a){n=a;}}throw new Error(`Remote asset batch failed after ${this.#t} attempt(s): ${n instanceof Error?n.message:String(n)}`,{cause:n})}};function Fs(e,t){let r=[];for(let n=0;n<e.length;n+=t)r.push(e.slice(n,n+t));return r}function Ds(e){return {sourceOrigin:e.sourceOrigin,targetOrigin:e.targetOrigin,urlRewriteMode:e.urlRewriteMode,outputDir:"/workspace",extraReplacements:e.extraReplacements,wpsuite:{...e.wpsuite?.subscriptionType?{subscriptionType:e.wpsuite.subscriptionType}:{},...e.wpsuite?.siteSettings?{siteSettings:{subscriber:e.wpsuite.siteSettings.subscriber===true}}:{}}}}function Us(e,t){if(!e||typeof e!="object")throw new Error("Remote rewrite result must be a JSON object.");let r=e;if(r.schemaVersion!==re||r.response?.operation!=="rewrite"||r.response.jobId!==t.jobId||r.response.taskId!==t.taskId||!r.detail||!Array.isArray(r.detail.objects))throw new Error("Remote rewrite result has an invalid envelope.");return r}async function ir(e,t,r){let n=r.options??{},s=await en(e,n.files);if(s.length===0)return 0;let o=ne(r.jobId,"jobId"),a=Ze(r.client.config.workspace.prefix,"jobs",o,"rewrite-inputs"),c=`${a}/config.json`.replace(/\/+/g,"/"),i=`${a}/asset-map.json`.replace(/\/+/g,"/"),d=`${a}/previous-asset-map.json`.replace(/\/+/g,"/");await Promise.all([r.client.writeWorkspaceObject({key:c,body:JSON.stringify(Ds(e)),contentType:"application/json; charset=utf-8"}),r.client.writeWorkspaceObject({key:i,body:JSON.stringify(t),contentType:"application/json; charset=utf-8"}),r.client.writeWorkspaceObject({key:d,body:JSON.stringify(n.previousAssetMap??{}),contentType:"application/json; charset=utf-8"})]);let u=Math.max(1,Math.min(500,r.batchSize)),l=Math.ceil(s.length/u),g=Math.max(1,Math.min(Math.max(1,r.concurrency),l));await n.onStart?.({totalFiles:s.length,workerCount:g});let p=[],f=0,w=0;await Promise.all(Array.from({length:Math.min(16,s.length)},async()=>{for(;;){let A=f;if(A>=s.length)return;f+=1;let C=s[A],R=k.join(e.outputDir,C),$=js.lookup(C),T=$?js.contentType($)||$:"text/plain; charset=utf-8",N=`${a}/files/${Ce(C)}.txt`.replace(/\/+/g,"/");await r.client.writeWorkspaceObject({key:N,body:await F.readFile(R),contentType:T}),p[A]={relativePath:C,key:N,fullPath:R,contentType:T},w+=1,await r.onStageProgress?.({index:w,totalFiles:s.length,file:C});}}));let m=Fs(p,u),h=0,y=0,S=0,P=0,M=Array.from({length:g},async()=>{for(;;){let A=h;if(A>=m.length)return;h+=1;let C=m[A],R=null,$=null;for(let T=1;T<=Math.max(1,Math.min(5,r.maxAttempts));T+=1){let N=ne(`rewrite-${A+1}-a${T}-${Ce(C.map(b=>b.relativePath).join(`
13
+ `)).slice(0,16)}`,"taskId"),H={schemaVersion:re,operation:"rewrite",jobId:o,taskId:N,configKey:c,assetMapKey:i,previousAssetMapKey:d,items:C.map(b=>({inputKey:b.key,outputKey:b.key,relativePath:b.relativePath,contentType:b.contentType}))};try{let b=await r.client.invoke(H);$=Us(await r.client.readResult(b),b);break}catch(b){R=b;}}if(!$)throw new Error(`Remote rewrite batch ${A+1}/${m.length} failed: ${R instanceof Error?R.message:String(R)}`,{cause:R});for(let T of C){let N=$.detail.objects.find(H=>H.outputKey===T.key);if(!N)throw new Error(`Remote rewrite result omitted ${T.relativePath}.`);await _e(T.fullPath,Buffer.from(await r.client.readObject(N.output))),N.changed&&(P+=1),y+=1,await n.onProgress?.({index:y,totalFiles:s.length,changedTextFiles:P,file:T.relativePath,changed:N.changed});}S+=1,await r.onBatchProgress?.({completedItems:y,totalItems:s.length,completedBatches:S,totalBatches:m.length,changedTextFiles:P});}});return await Promise.all(M),P}async function ar(e,t,r,n){let s=n.relativePaths?new Set([...n.relativePaths].map(h=>h.replace(/\\/g,"/"))):null,o=Object.values(t.objects).filter(h=>h.isText&&(!s||s.has(h.relativePath))).sort((h,y)=>h.relativePath.localeCompare(y.relativePath));if(o.length===0)return 0;let a=ne(n.jobId,"jobId"),c=Ze(n.client.config.workspace.prefix,"jobs",a,"rewrite-config"),i=`${c}/config.json`.replace(/\/+/g,"/"),d=`${c}/asset-map.json`.replace(/\/+/g,"/");await Promise.all([n.client.writeWorkspaceObject({key:i,body:JSON.stringify(Ds(e)),contentType:"application/json; charset=utf-8"}),n.client.writeWorkspaceObject({key:d,body:JSON.stringify(r),contentType:"application/json; charset=utf-8"})]);let u=Math.max(1,Math.min(500,n.batchSize)),l=Fs(o,u),g=Math.max(1,Math.min(Math.max(1,n.concurrency),l.length)),p=0,f=0,w=0,m=0;return await Promise.all(Array.from({length:g},async()=>{for(;;){let h=p;if(h>=l.length)return;p+=1;let y=l[h],S=null,P=null;for(let M=1;M<=Math.max(1,Math.min(5,n.maxAttempts));M+=1){let A=ne(`rewrite-object-${h+1}-a${M}-${Ce(y.map(C=>C.relativePath).join(`
14
+ `)).slice(0,16)}`,"taskId");try{let C=await n.client.invoke({schemaVersion:re,operation:"rewrite",jobId:a,taskId:A,configKey:i,assetMapKey:d,items:y.map(R=>({inputKey:R.object.key,outputKey:R.object.key,relativePath:R.relativePath,contentType:R.contentType||R.object.contentType}))});S=Us(await n.client.readResult(C),C);break}catch(C){P=C;}}if(!S)throw new Error(`Remote object rewrite batch ${h+1}/${l.length} failed: ${P instanceof Error?P.message:String(P)}`,{cause:P});for(let M of y){let A=S.detail.objects.find(C=>C.outputKey===M.object.key);if(!A)throw new Error(`Remote rewrite result omitted ${M.relativePath}.`);M.object=A.output,A.changed&&(m+=1),f+=1,await n.onProgress?.({index:f,totalFiles:o.length,changedTextFiles:m,file:M.relativePath,changed:A.changed});}w+=1,await n.onBatchProgress?.({completedItems:f,totalItems:o.length,completedBatches:w,totalBatches:l.length,changedTextFiles:m});}})),t.assetMap={...r},t.generatedAt=new Date().toISOString(),m}var ma="remote-workers.json";function cr(e={}){let t=String(e.explicitPath??"").trim();if(t)return k.resolve(t);let r=String(e.runtimeDir??process.env.STATIC_PUBLISHER_RUNTIME_DIR??process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR??"").trim();return r?k.join(k.resolve(r),ma):""}function Ls(e,t,r,n=250){let s=Math.max(1,Math.floor(n)),o=[],a=new Set;for(let c of [e,...t]){let i=c.trim();if(!(!i||a.has(i)||r(i))&&(a.add(i),o.push(i),o.length>=s))break}return o}async function Ws(e,t){let r=String(e.wpsuite?.runtimeToken||"").trim();if(!r)throw new Error("Page-cache purge is enabled, but the WordPress runtime token is missing.");let n=new URL("/wp-json/smartcloud-static-publisher/v1/page-cache/purge",e.sourceOrigin).toString(),s=await t.post(n,{timeout:3e4,failOnStatusCode:false,maxRedirects:0,headers:{"content-type":"application/json","x-static-publisher-token":r},data:JSON.stringify({scope:"all"})});try{let o=await s.text();if(o.length>65536)throw new Error("WordPress page-cache purge response was too large.");let a=null;try{a=JSON.parse(o);}catch{throw new Error(`WordPress page-cache purge returned invalid JSON (HTTP ${s.status()}).`)}if(!s.ok()){let c=a&&typeof a=="object"&&"message"in a?String(a.message||""):"";throw new Error(`WordPress page-cache purge failed with HTTP ${s.status()}${c?`: ${c}`:""}`)}if(!a||typeof a!="object"||a.purged!==!0)throw new Error("WordPress page-cache provider did not confirm a complete purge.");return {purged:!0,provider:String(a.provider||"custom"),scope:"all",...typeof a.purgedAt=="string"?{purgedAt:a.purgedAt}:{}}}finally{await s.dispose().catch(()=>{});}}function ga(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?k.resolve(e):""}function lr(e){let t=String(process.env.STATIC_PUBLISHER_REMOTE_EXPORT_MANIFEST_PATH||"").trim();if(t)return k.resolve(t);let r=ga();if(r)return k.join(r,"remote-export-manifest.json");let n=k.resolve(e.outputDir);return k.join(k.dirname(n),`.${k.basename(n)}-remote-export-manifest.json`)}function ur(e){let t=k.posix.normalize(String(e||"").replace(/\\/g,"/").replace(/^\/+/,""));if(!t||t==="."||t===".."||t.startsWith("../")||/^[A-Za-z]:/.test(t))throw new Error(`Invalid remote export relative path: ${e}`);return t}function _s(e,t,r){let n=r?.workspace.bucket===t.workspace.bucket&&r.workspace.prefix===t.workspace.prefix;return {schemaVersion:1,generatedAt:new Date().toISOString(),jobId:e,workspace:{...t.workspace},objects:n?{...r.objects}:{},assetMap:n?{...r.assetMap}:{}}}function Ns(e,t){let r=e.objects[ur(t)];if(!r)return true;let n=`${Ze(e.workspace.prefix,"jobs",ne(e.jobId,"jobId"))}/`;return !Fe(r.object.key,e.workspace.prefix).startsWith(n)}function gt(e,t){let r=ur(t.relativePath);if(t.object.bucket!==e.workspace.bucket)throw new Error(`Remote export object bucket mismatch for ${r}.`);Fe(t.object.key,e.workspace.prefix),e.objects[r]={...t,relativePath:r},e.generatedAt=new Date().toISOString();}function Bs(e,t){delete e.objects[ur(t)],e.generatedAt=new Date().toISOString();}async function Hs(e,t){t.generatedAt=new Date().toISOString();let r=lr(e);await F.mkdir(k.dirname(r),{recursive:true}),await _e(r,`${JSON.stringify(t,null,2)}
15
+ `,"utf8");}async function dr(e){try{let t=JSON.parse(await F.readFile(lr(e),"utf8"));if(t.schemaVersion!==1||typeof t.jobId!="string"||!t.workspace||typeof t.workspace.bucket!="string"||typeof t.workspace.prefix!="string"||!t.objects||typeof t.objects!="object"||!t.assetMap||typeof t.assetMap!="object")return null;let r=t;for(let[n,s]of Object.entries(r.objects)){if(ur(n),!s||s.relativePath!==n||s.object?.bucket!==r.workspace.bucket)return null;Fe(s.object.key,r.workspace.prefix);}return r}catch{return null}}var ya=["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"],no=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"]),so=new Set(["",".html",".htm"]),ba=new Set([".css",".xml",".xsl",".svg",".json",".html",".htm",".txt",".enc",".jws"]),Sa=["/wp-content/","/wp-includes/","/wp-admin/","/static/","/assets/","/build/","/_next/"];function Pa(e){let t=[],r="full",n="full",s=false,o=false,a="",c=null,i="";for(let d=0;d<e.length;d++){let u=e[d];if(u==="--retry-timeouts")r="retry-timeouts",s=true;else if(u==="--resume")s=true;else if(u==="--resume-rewrite")s=true,o=true;else if(u==="--crawl-mode"){let l=e[++d];if(!l)throw new Error("--crawl-mode requires a value");n=l==="incremental"?"incremental":"full",s||=n==="incremental";}else if(u.startsWith("--crawl-mode="))n=u.slice(13)==="incremental"?"incremental":"full",s||=n==="incremental";else if(u==="--incremental")n="incremental",s=true;else if(u==="--url"){let l=e[++d];if(!l)throw new Error("--url requires a value");r="single-url",s=true,t.push(l);}else if(u.startsWith("--url="))r="single-url",s=true,t.push(u.slice(6));else if(u==="--urls"){let l=e[++d];if(!l)throw new Error("--urls requires a file path");r="single-url",s=true,t.push(`@${l}`);}else if(u==="--content-sync-plan"){let l=e[++d];if(!l)throw new Error("--content-sync-plan requires a file path");r="content-sync",n="incremental",s=true,a=k.resolve(l);}else if(u.startsWith("--content-sync-plan="))r="content-sync",n="incremental",s=true,a=k.resolve(u.slice(20));else if(u==="--remote-render")c=true;else if(u==="--local-render")c=false;else if(u==="--remote-worker-config"){let l=e[++d];if(!l)throw new Error("--remote-worker-config requires a file path");i=k.resolve(l);}else u.startsWith("--remote-worker-config=")&&(i=k.resolve(u.slice(23)));}return {mode:r,crawlMode:n,urls:t,preserveOutput:s,resumeRewrite:o,contentSyncPlanPath:a,remoteRender:c,remoteWorkerConfigPath:i}}function Dt(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?k.resolve(e):""}function Ra(){let e=Dt();return e?k.join(e,"privacy","cookie-observations.json"):null}function va(){let e=Dt();return e?k.resolve(e,".."):""}function Ca(){let e=process.env.STATIC_PUBLISHER_WP_ROOT||process.env.WPSUITE_STATIC_PUBLISHER_WP_ROOT||"";return e.trim()?k.resolve(e):""}function ka(e){let t=e.trim(),r=[{alias:"@storage-root",root:va(),requiredEnv:"STATIC_PUBLISHER_RUNTIME_DIR or WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR"},{alias:"@runtime",root:Dt(),requiredEnv:"STATIC_PUBLISHER_RUNTIME_DIR or WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR"},{alias:"@wp-root",root:Ca(),requiredEnv:"STATIC_PUBLISHER_WP_ROOT or WPSUITE_STATIC_PUBLISHER_WP_ROOT"}];for(let n of r){if(t!==n.alias&&!t.startsWith(`${n.alias}/`))continue;if(!n.root)return {resolvedPath:null,alias:n.alias,requiredEnv:n.requiredEnv};let s=t.slice(n.alias.length).replace(/^\/+/,"");return {resolvedPath:s?k.resolve(n.root,s):n.root,alias:n.alias}}return {resolvedPath:k.resolve(t)}}function oo(e){let t=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(t)return k.resolve(t);let r=Dt();return r?k.join(r,"crawl-manifest.json"):k.join(e.outputDir,".crawl-manifest.json")}function io(){return {schemaVersion:6,updatedAt:"",pages:{},assets:{}}}function zs(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 xa(e){let t=e.isText===true;return {url:String(e.url||""),outputPath:String(e.outputPath||""),isText:t,contentType:typeof e.contentType=="string"&&e.contentType?e.contentType:null,cacheControl:typeof e.cacheControl=="string"&&e.cacheControl?e.cacheControl:null,changeToken:typeof e.changeToken=="string"?e.changeToken:null,tokenSource:typeof e.tokenSource=="string"?e.tokenSource:null,discoveredAssets:Array.isArray(e.discoveredAssets)?e.discoveredAssets.map(r=>String(r)):[],lastCrawledAt:String(e.lastCrawledAt||""),lastSeenRunId:String(e.lastSeenRunId||""),unreferencedRuns:Math.max(0,Number(e.unreferencedRuns)||0),rewriteComplete:e.rewriteComplete===true||!t}}function dn(e){let t=e?.["content-type"]?.trim();return t||null}function pn(e){let t=e?.["cache-control"]?.trim();return t||null}function Ia(e){return e?e.split(",").map(t=>t.trim().toLowerCase()).some(t=>t==="no-cache"||t==="no-store"||/^max-age\s*=\s*0$/.test(t)):false}function Ta(){return {rewriteTargets:new Set,changedFiles:new Set,deletedFiles:new Set,revalidateFiles:new Set}}function ft(e,t,r,n){let s=ge(t.outputDir,r);s&&(Ia(n)?e.revalidateFiles.add(s):e.revalidateFiles.delete(s));}function ue(e,t,r){let n=ge(t.outputDir,r);n&&(e.changedFiles.add(n),e.deletedFiles.delete(n));}function mn(e,t,r){let n=ge(t.outputDir,r);n&&(e.deletedFiles.add(n),e.changedFiles.delete(n),e.revalidateFiles.delete(n));}function ke(e,t){e.rewriteTargets.add(k.resolve(t));}async function Aa(e){try{let t=await F.readFile(oo(e),"utf8"),r=JSON.parse(t);if(r&&typeof r=="object"&&r.pages&&typeof r.pages=="object"){if((r.schemaVersion===2||r.schemaVersion===3||r.schemaVersion===4||r.schemaVersion===5||r.schemaVersion===6)&&r.assets&&typeof r.assets=="object")return {schemaVersion:6,updatedAt:String(r.updatedAt||""),pages:Object.fromEntries(Object.entries(r.pages).map(([n,s])=>[n,zs(s)])),assets:Object.fromEntries(Object.entries(r.assets).map(([n,s])=>[n,xa(s)]))};if(r.schemaVersion===1)return {schemaVersion:6,updatedAt:String(r.updatedAt||""),pages:Object.fromEntries(Object.entries(r.pages).map(([n,s])=>[n,zs(s)])),assets:{}}}}catch{}return io()}function gn(e){return JSON.parse(JSON.stringify(e))}async function Ea(e,t){let r=oo(e);await F.mkdir(k.dirname(r),{recursive:true}),await F.writeFile(r,JSON.stringify(t,null,2),"utf8");}async function Ve(e,t,r=false){if(!t.trackManifest)return;t.manifestPendingChanges+=1;let n=Date.now();if(!r&&t.manifestPendingChanges<25&&n-t.lastManifestSavedAt<5e3)return;t.manifestPendingChanges=0,t.lastManifestSavedAt=n;let s=async()=>{let o=gn(t.manifest);o.updatedAt=new Date().toISOString(),t.manifest.updatedAt=o.updatedAt,await Ea(e,o);};t.manifestSaveChain=t.manifestSaveChain.then(s,s),await t.manifestSaveChain;}function nt(e){if(!e)return null;let t=e.trim();return t||null}function $a(e,t){let r=new Set,n=new Set([...Object.keys(e),...Object.keys(t)]);for(let s of n){let o=e[s],a=t[s];!o||!a||o===a||r.add(s);}return r}async function ao(e,t,r,n){let s=[...new Set(r.map(c=>c.trim()).filter(Boolean))],o=String(e.wpsuite?.runtimeToken||"").trim();if(s.length===0||!o)return new Map;let a=new URL("/wp-json/smartcloud-static-publisher/v1/change-tokens",e.sourceOrigin).toString();try{let c=await t.post(a,{timeout:3e4,failOnStatusCode:!1,headers:{"content-type":"application/json","x-static-publisher-token":o},data:JSON.stringify({urls:s})});if(!c.ok())return n.warn(`Change token lookup failed with HTTP ${c.status()}`,{endpoint:a,status:c.status()}),new Map;let i=await c.json().catch(()=>null);if(!i||!Array.isArray(i.items))return new Map;let d=new Map;for(let u of i.items)!u||typeof u.url!="string"||d.set(u.url,u);return d}catch(c){return n.warn("Change token lookup failed; falling back to sitemap metadata",{endpoint:a,error:c instanceof Error?`${c.name}: ${c.message.split(/\r?\n/u,1)[0]}`:"Unknown change-token request failure"}),new Map}}async function mr(e,t,r,n,s){if(!r.enabled&&!r.trackManifest)return null;if(r.changeTokenCache.has(n))return r.changeTokenCache.get(n)??null;let a=(await ao(e,t,[n],s)).get(n)??null;return Sr(e,n)&&(!a||a.supported!==true||!a.token)&&(a=await ja(e,t,n,s)),a=co(e,a),r.changeTokenCache.set(n,a),a}async function Pn(e,t,r,n,s){if(!r.trackManifest)return;let o=[...new Set(n.map(a=>a.trim()).filter(Boolean))].filter(a=>!r.changeTokenCache.has(a));for(let a=0;a<o.length;a+=250){let c=o.slice(a,a+250),i=await ao(e,t,c,s);for(let d of c)r.changeTokenCache.set(d,co(e,i.get(d)??null));}}async function Oa(e,t,r,n,s,o){if(!(!n.trackManifest||n.changeTokenCache.has(s)))for(;!n.changeTokenCache.has(s);){if(!n.pageChangeTokenPrimePromise){let a=Ls(s,r.pageQueue,i=>n.changeTokenCache.has(i));if(a.length===0)return;o.info(`Loading ${a.length} page change tokens in bulk.`,{phase:"prime-page-change-tokens",batchSize:a.length,remainingPages:r.pageQueue.length});let c=Pn(e,t,n,a,o).finally(()=>{n.pageChangeTokenPrimePromise===c&&(n.pageChangeTokenPrimePromise=null);});n.pageChangeTokenPrimePromise=c;}await n.pageChangeTokenPrimePromise;}}function Ma(e){return `subscriber=${e.wpsuite?.siteSettings?.subscriber===true?"1":"0"}`}function co(e,t){if(!t?.supported||!t.token||t.tokenSource==="wp-media-file")return t;let r=Ma(e);return {...t,token:$e(`${t.token}
16
+ ${r}`,40),tokenSource:t.tokenSource?`${t.tokenSource}+site-subscriber`:"site-subscriber"}}async function ja(e,t,r,n){try{let s=await t.get(r,{timeout:Math.min(e.navigationTimeoutMs,15e3),failOnStatusCode:!1});if(s.status()!==404)return n.warn(`Generated 404 fallback token skipped for ${r}`,{url:r,status:s.status(),requestPath:e.generated404RequestPath,source:"generated-404-change-token-fallback"}),null;let o=await s.text().catch(()=>""),a=$e(`${r}
17
17
  404
18
- ${o}`,40);return n.info(`Using generated 404 fallback change token for ${r}`,{url:r,requestPath:e.generated404RequestPath,source:"generated-404-change-token-fallback"}),{url:r,supported:!0,token:a,tokenSource:"generated-404-response-hash",postId:null,dependencyPostIds:[],reason:null}}catch(s){return n.warn(`Generated 404 fallback token lookup failed for ${r}`,{url:r,error:String(s),requestPath:e.generated404RequestPath,source:"generated-404-change-token-fallback"}),null}}async function ao(e,t,r,n,s,o){let a=yr(e,s);if(await Oa(e,t,r,n,s,o),!n.enabled)return {action:"render",changeToken:n.trackManifest?await dr(e,t,n,s,o):null};let c=n.manifest.pages[s];if(!c)return {action:"render",changeToken:await dr(e,t,n,s,o)};let i=await dr(e,t,n,s,o);if(i?.supported&&i.token)return c.changeToken===i.token?a&&!await Da(e,t,s,o)?{action:"render",changeToken:i}:{action:"reuse",changeToken:i}:{action:"render",changeToken:i};let d=rt(r.sitemapLastmodByPage[s]);return d&&c.sitemapLastmod&&c.sitemapLastmod===d?{action:"reuse",changeToken:i}:{action:"render",changeToken:i}}async function Da(e,t,r,n){try{let o=(await t.get(r,{timeout:Math.min(e.navigationTimeoutMs,15e3),failOnStatusCode:!1})).status();return o===404?!0:(n.warn(`Generated 404 request path no longer returns 404 for ${r}`,{url:r,status:o,requestPath:e.generated404RequestPath,source:"generated-404-status-probe"}),!1)}catch(s){return n.warn(`Generated 404 status probe failed for ${r}`,{url:r,error:String(s),requestPath:e.generated404RequestPath,source:"generated-404-status-probe"}),false}}async function Fa(e,t,r,n,s){let o=Ks(r.previousManifest,void 0,n,"previous-manifest"),a=0;for(let[g,p]of Object.entries(r.manifest.pages))if(!r.seenPages.has(g)){dn(s,e,p.outputPath);try{await D.unlink(p.outputPath);}catch(f){(f.code||"")!=="ENOENT"&&n.warn(`Failed to remove stale incremental page output for ${g}`,{url:g,outputPath:p.outputPath,error:String(f)});}delete r.manifest.pages[g],a++;}let c=Ks(r.manifest,r.runId,n,"active-manifest"),i=new Set,d=new Set;for(let g of c){for(let w of Ot(e,g))i.add(w);let p=r.manifest.assets[g]?.outputPath;p&&d.add(p);let f=ht(e,t.assetMap,g);f&&d.add(f);}for(let g of Object.keys(r.manifest.assets)){if(c.has(g)){r.manifest.assets[g].unreferencedRuns=0;continue}let p=r.manifest.assets[g];if(vs(p.unreferencedRuns)){p.unreferencedRuns+=1,n.warn(`Deferred deletion of newly unreferenced incremental asset ${g}.`,{url:g,unreferencedRuns:p.unreferencedRuns,phase:"incremental-cleanup",source:"asset-deletion-grace"});continue}delete r.manifest.assets[g];}let u=0,l=0;for(let g of o){if(r.manifest.assets[g])continue;let p=new Set,f=r.previousManifest.assets[g]?.outputPath;f&&p.add(f);let w=ht(e,t.assetMap,g);if(!c.has(g)&&w)p.add(w);else if(!c.has(g)){let m=Xa(e,g,n,"incremental-cleanup");m&&(p.add(m.originalFilePath),m.hashedFilePath&&p.add(m.hashedFilePath));}for(let m of p)if(!d.has(m)){dn(s,e,m);try{await D.unlink(m),u++;}catch(h){(h.code||"")!=="ENOENT"&&n.warn(`Failed to remove stale incremental asset output for ${g}`,{url:g,outputPath:m,error:String(h)});}}for(let m of Ot(e,g))i.has(m)||Object.prototype.hasOwnProperty.call(t.assetMap,m)&&(delete t.assetMap[m],l++);}(a>0||u>0||l>0)&&n.info(`Incremental cleanup removed ${a} stale pages, ${u} stale assets, ${l} stale asset mappings`,{removedPages:a,removedAssets:u,removedAssetMappings:l,phase:"incremental-cleanup"});}async function Ua(e,t,r,n){let s=new Set([...Object.values(t.manifest.pages),...Object.values(t.manifest.assets)].map(i=>String(i.outputPath||"").trim()).filter(Boolean).map(i=>C.resolve(i))),o=Object.values(e.postCrawlCopyMap||{}).map(i=>pr(String(i||""))).filter(i=>!!i).map(i=>C.resolve(e.outputDir,i)),c=(await Ci(["**/*.html","**/*.htm"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(i=>Rs(C.join(e.outputDir,i),e.outputDir,s,o));for(let i of c){let d=C.join(e.outputDir,i);dn(n,e,d),await D.unlink(d);}c.length>0&&r.info(`Removed ${c.length} orphaned HTML output file(s) that are absent from the current crawl manifest.`,{phase:"incremental-cleanup",source:"manifest-output-reconciliation",removedPages:c.length,samples:c.slice(0,20)});}function Ks(e,t,r,n){let s=new Set,o=a=>{let c=a.trim();if(c){if(r){let i=ke(c,r,"asset",n??"manifest");if(!i)return;s.add(i.toString());return}s.add(c);}};for(let a of Object.values(e.pages))for(let c of a.discoveredAssets||[])o(c);for(let[a,c]of Object.entries(e.assets||{}))t&&c.lastSeenRunId!==t||o(a);return s}function zs(e,t,r){for(let n of t)for(let s of Ot(e,n))if(r.has(s))return true;return false}function La(e,t,r,n,s){let o=new Set(r.rewriteTargets);for(let c of r.changedFiles)o.add(C.resolve(e.outputDir,c));for(let c of Object.values(t.manifest.pages))c.rewriteComplete||o.add(C.resolve(c.outputPath));for(let c of Object.values(t.manifest.assets))c.isText&&!c.rewriteComplete&&o.add(C.resolve(c.outputPath));let a=$a(n,s);if(a.size===0)return [...o];for(let c of Object.values(t.manifest.pages))zs(e,c.discoveredAssets,a)&&o.add(C.resolve(c.outputPath));for(let[c,i]of Object.entries(t.manifest.assets)){i.isText&&zs(e,i.discoveredAssets,a)&&o.add(C.resolve(i.outputPath));for(let d of Ot(e,c))if(a.has(d)){le(r,e,i.outputPath),i.isText&&o.add(C.resolve(i.outputPath));break}}return [...o]}function qa(e,t){let r=t?new Set(Array.from(t,s=>C.resolve(s))):null,n=s=>r===null||r.has(C.resolve(s));for(let s of Object.values(e.manifest.pages))n(s.outputPath)&&(s.rewriteComplete=true);for(let s of Object.values(e.manifest.assets)){if(!s.isText){s.rewriteComplete=true;continue}n(s.outputPath)&&(s.rewriteComplete=true);}}function Wa(e,t,r){let n=new Set,s=o=>{if(!o)return;let a=me(e.outputDir,o);a&&n.add(a);};for(let o of t.donePages)s(De(e,o));for(let o of Object.values(r.manifest.pages))o.lastSeenRunId===r.runId&&s(o.outputPath);for(let o of t.doneAssets)s(ht(e,t.assetMap,o));for(let o of Object.values(r.manifest.assets))o.lastSeenRunId===r.runId&&s(o.outputPath);return n}async function _a(e,t,r,n,s,o){let a=[...n.deletedFiles].sort(),c=Wa(e,t,r),i=0;for(let l of [...c].sort())if(!o?.objects[l]){try{if((await D.stat(C.join(e.outputDir,l))).isFile())continue}catch{}i++,s.warn(`Active output is missing after crawl: ${l}`,{phase:"deploy-plan",file:l,outputPath:C.join(e.outputDir,l)});}if(i>0&&s.warn(`Detected ${i} active output file(s) missing before deploy plan save.`,{phase:"deploy-plan",missingActiveOutputs:i}),a.length===0)return [];let d=[],u=0;for(let l of a){if(!c.has(l)){d.push(l);continue}u++;let g=C.join(e.outputDir,l),p;try{p=(await D.stat(g)).isFile();}catch{p=false;}s.warn(`Deploy plan protected active output from deletion: ${l}`,{phase:"deploy-plan",file:l,outputPath:g,exists:p});}return u>0&&s.info(`Deploy plan skipped ${u} active output deletion(s).`,{phase:"deploy-plan",protectedDeletes:u,deletedFiles:d.length}),d}function Ot(e,t){let r=new Set([t]);try{let n=new URL(t),o=[new URL(e.sourceOrigin).origin,...Object.keys(e.extraReplacements||{})];if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{o.push(new URL(e.targetOrigin).origin);}catch{}for(let a of o)try{r.add(new URL(a).origin+n.pathname+n.search);}catch{}r.add(n.pathname+n.search),r.add(n.pathname);}catch{}return [...r]}function Na(e,t){let r=new Set([t]);try{let n=new URL(t),o=[new URL(e.sourceOrigin).origin,...Object.keys(e.extraReplacements||{})];if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{o.push(new URL(e.targetOrigin).origin);}catch{}for(let a of o)try{r.add(new URL(a).origin+n.pathname+n.search);}catch{}r.add(n.pathname+n.search);}catch{}return [...r]}function co(e,t){let r=e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/"?e.targetOrigin:"https://relative.invalid/";try{let n=new URL(t,r);return At(e.outputDir,n.pathname)}catch{return null}}function lo(e,t,r){for(let n of Na(e,r)){let s=t[n];if(!s)continue;let o=co(e,s);if(o)return {outputPath:o,publicUrl:s}}return null}function Ba(e,t,r){for(let n of Ot(e,r)){let s=t[n];if(!s)continue;let o=co(e,s);if(o)return o}return null}function ht(e,t,r){let n=Ba(e,t,r);if(n)return n;try{return Ft(e,r).originalFilePath}catch{return null}}function Ha(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?C.join(C.resolve(e),"queue-runner-heartbeat.json"):""}async function an(e){let t=Ha();if(t)try{let r=await D.readFile(t,"utf8"),n=JSON.parse(r);if(!n||typeof n!="object")return;await D.writeFile(t,JSON.stringify({...n,checkedAt:new Date().toISOString(),status:"running",currentStep:"rewrite",message:e},null,2),"utf8");}catch{}}function Va(e,t){let r=new Set;for(let n of t.donePages)r.add(C.resolve(De(e,n)));for(let n of t.doneAssets){let s=ht(e,t.assetMap,n);!s||!$e(s)||r.add(C.resolve(s));}for(let n of t.doneSitemaps){let s=ht(e,t.assetMap,n);!s||!$e(s)||r.add(C.resolve(s));}return [...r]}var mr="re:",ur=new Map;function Ka(e){if(!e.startsWith(mr))return null;let t=ur.get(e);if(t!==void 0)return t||null;let r=e.slice(mr.length).trim();if(!r)return ur.set(e,false),null;try{let n=new RegExp(r);return ur.set(e,n),n}catch{return console.warn(`[crawl] Ignoring invalid path matcher regex: ${e}`),ur.set(e,false),null}}function Sn(e,t){let r=Ka(t);return r?r.test(e):t.startsWith(mr)?false:e.startsWith(t)}function za(e,t){return t.find(r=>Sn(e,r))}function uo(e,t){let r=za(t.pathname,e.blockedPathPrefixes);if(r)return r.startsWith(mr)?`blocked path regex: ${r}`:`blocked path prefix: ${r}`;let n=e.blockedSearchFragments.find(s=>t.search.includes(s));return n?`blocked search fragment: ${n}`:null}function Ja(e){let t=new URL(e.sourceOrigin),r=new Set([t.hostname,...e.allowedAssetHosts]);if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{r.add(new URL(e.targetOrigin).hostname);}catch{}for(let n of Object.keys(e.extraReplacements||{}))try{r.add(new URL(n).hostname);}catch{}return r}function Qa(e,t){return Ja(e).has(t.hostname)}function Ga(e){let t=_e(e.trim()).replace(/\\\//g,"/").replace(/^['"]|['"]$/g,"").trim();return t=t.replace(/[)]+$/g,"").trim(),t=t.replace(/;.*$/g,"").trim(),t}function mn(e){let t=_e(e).replace(/\\\//g,"/");return /[{}]|\bwindow\.|\blocation\.|\bincludes\(|\?\?null|\+|%7B|%7D|%22|<|>|\s/.test(t)||/[()[\]'"]/.test(t)}function Za(e){let t=_e(e).replace(/\\\//g,"/").trim(),r=C.extname(t).toLowerCase();return to.has(r)||ro.has(r)?false:/^(?:this|[A-Za-z_$][\w$]*)\.[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/.test(t)}function nt(e){return Sa.some(t=>e.pathname.startsWith(t))}function oe(e,t,r=e.sourceOrigin,n,s="url",o=false){let a=Ga(t);if(!a||a.startsWith("data:")||a.startsWith("blob:")||a.startsWith("mailto:")||a.startsWith("tel:")||a.startsWith("#"))return n?.ignore(s,t,"empty or unsupported scheme",r),null;if(mn(a))return n?.ignore(s,t,"looks like JavaScript/code fragment",r),null;if(Za(a))return n?.ignore(s,t,"looks like JavaScript property access",r),null;try{let c=new URL(a,r);if(po(c))return n?.reject(s,t,"malformed URL encoding",r),null;if(!["http:","https:"].includes(c.protocol))return n?.ignore(s,t,"unsupported protocol: ",r),null;if(!Qa(e,c))return n?.ignore(s,c.toString(),"host not allowed",r),null;let i=uo(e,c);if(i&&!(o&&Lr(c)))return n?.reject(s,c.toString(),i,r),null;let d=new URL(e.sourceOrigin);return c.protocol=d.protocol,c.host=d.host,c.hash="",c}catch{return n?.reject(s,t,"invalid URL",r),null}}function po(e){try{return decodeURIComponent(e.pathname),decodeURIComponent(e.search),decodeURIComponent(e.hash),!1}catch{return true}}function ke(e,t,r,n){try{let s=new URL(e);return po(s)?(t.reject(r,e,"malformed URL encoding",n),null):Tt(s.pathname)===null?(t.reject(r,e,"unsafe URL pathname",n),null):s.protocol!=="http:"&&s.protocol!=="https:"?(t.reject(r,e,`unsupported protocol: ${s.protocol}`,n),null):s}catch{return t.reject(r,e,"invalid URL",n),null}}function mo(e){return to.has(C.extname(e.pathname).toLowerCase())}function Ve(e){return nt(e)?false:ro.has(C.extname(e.pathname).toLowerCase())}function yt(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function G(e,t){return !!(yt(t)||t.pathname==="/robots.txt"||t.pathname==="/llms.txt"||t.pathname.toLowerCase().endsWith(".xsl")||e.assetPathPrefixes.length>0&&e.assetPathPrefixes.some(r=>Sn(t.pathname,r))||mo(t)||!Ve(t)&&!nt(t))}function De(e,t){let r=new URL(t),n=Tt(r.pathname);if(n===null)throw new Error(`Unsafe page URL pathname: ${r.pathname}`);return n.endsWith("/")?n+="index.html":C.extname(n)||(n+="/index.html"),At(e.outputDir,n)}function Ya(e){if(!e.generated404RequestPath)return null;try{let t=new URL(e.generated404RequestPath,e.sourceOrigin);return !t.pathname.endsWith("/")&&C.extname(t.pathname)===""&&(t.pathname+="/"),t.hash="",t.toString()}catch{return null}}function yr(e,t){let r=Ya(e);if(!r)return false;try{return new URL(t,e.sourceOrigin).toString()===r}catch{return false}}function Ft(e,t){let r=new URL(t),n=Tt(r.pathname);if(n===null)throw new Error(`Unsafe asset URL pathname: ${r.pathname}`);n.endsWith("/")&&(n+="index.html");let s=!!r.search,o=n,a=At(e.outputDir,o),c=Jr(e.targetOrigin,o),i=`${c}${r.search}`;if(!s)return {originalPathname:o,originalFilePath:a,originalPublicUrl:c,originalPublicUrlWithSearch:i,preferQueryHashed:s};let d=C.extname(n),l=`${d?n.slice(0,-d.length):n}.${Ee(r.search)}${d||".bin"}`;return {originalPathname:o,originalFilePath:a,originalPublicUrl:c,originalPublicUrlWithSearch:i,preferQueryHashed:s,hashedPathname:l,hashedFilePath:At(e.outputDir,l),hashedPublicUrl:Jr(e.targetOrigin,l)}}function Xa(e,t,r,n){let s=ke(t,r,"asset",n);return s?Ft(e,s.toString()):null}function gn(e,t,r,n,s){t[r]=n;try{let o=new URL(r),c=[new URL(e.sourceOrigin).origin,...Object.keys(e.extraReplacements||{})];if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{c.push(new URL(e.targetOrigin).origin);}catch{}for(let i of c)try{let d=new URL(i);t[d.origin+o.pathname+o.search]=n;}catch{}try{let i=(()=>{let d=new URL(n,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin);return `${d.pathname}${d.search}`})();t[o.pathname+o.search]=i;}catch{}try{let i=new URL(n,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin).pathname;t[o.pathname]=i;}catch{}try{let i=new URL(s,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin).pathname;t[o.pathname]=i;}catch{}}catch{}}async function go(e){try{return await D.readFile(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}async function Ye(e){try{return await D.stat(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}function ec(e){let t=String(e["content-length"]||"").trim();if(!t)return null;let r=Number.parseInt(t,10);return Number.isFinite(r)&&r>=0?r:null}function fo(e){let t=String(e["last-modified"]||"").trim();if(!t)return null;let r=Date.parse(t);return Number.isFinite(r)?r:null}function tc(e,t){let r=fo(t),n=ec(t),s=e?.mtime.getTime();return r!==null&&Math.abs((s??0)-r)>=1e3||n!==null&&e?.size!==n?false:r!==null||n!==null}async function rc(e,t){if(!t)return;let r=fo(t);if(r===null)return;let n=await Ye(e);if(!n)return;let s=new Date(r);await D.utimes(e,n.atime,s).catch(()=>{});}function fn(e,t){let r=C.extname(e.pathname).toLowerCase(),n=t["content-type"]||"";return ba.has(r)||ya.some(s=>n.includes(s))&&!n.startsWith("image/")&&!n.includes("font")}function nc(e){try{let t=C.extname(new URL(e).pathname).toLowerCase();return t===".js"||t===".mjs"}catch{return false}}function Ze(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 sc(e){return Ee(e.toString("base64"),40)}function ho(e,t,r){let n=e.assetVariantDigestsByOriginalPath.get(t);if(!n||n.size===0)return false;for(let s of n)if(s!==r)return true;return false}function wo(e,t,r){let n=e.assetVariantDigestsByOriginalPath.get(t);n||(n=new Set,e.assetVariantDigestsByOriginalPath.set(t,n)),n.add(r);}async function hn(e,t,r,n,s,o,a){let c=Ft(e,r),i=sc(n),d=c.originalFilePath,u=c.originalPublicUrlWithSearch;if(c.preferQueryHashed&&c.hashedFilePath&&c.hashedPublicUrl&&ho(s,c.originalFilePath,i)){let f=lo(e,t,r);f?(d=f.outputPath,u=f.publicUrl):(d=c.hashedFilePath,u=c.hashedPublicUrl);}gn(e,t,r,u,c.originalPublicUrl);let l=$e(d)?Buffer.from(Yr(n.toString("utf8"),e,t,d),"utf8"):n,g=await go(d),p=false;return (g===null||!g.equals(l))&&(await We(d,l),p=true),o?.debug(`${p?"Saved":"Skipped unchanged"} asset output ${r}`,{phase:"download-assets",url:r,outputPath:d,bytes:l.length,wroteFile:p}),wo(s,c.originalFilePath,i),await rc(d,a),s.stats.assetsSaved++,o&&(s.stats.assetsSaved%50===0||s.stats.assetsSaved===1)&&o.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:d,publicUrl:u,wroteFile:p}}function st(e,t,r,n,s){let o=()=>{let l=String(s??"").trim();if(!l)return {discoveredFrom:"",discoveredFromType:"unknown"};if(l==="seed-path"||l==="cli"||l==="generated-404-request-path")return {discoveredFrom:l,discoveredFromType:l};if(/^https?:\/\//i.test(l))try{let g=new URL(l);return yt(g)?{discoveredFrom:l,discoveredFromType:"sitemap"}:G(e,g)?{discoveredFrom:l,discoveredFromType:"asset"}:Ve(g)&&!nt(g)?{discoveredFrom:l,discoveredFromType:"page"}:{discoveredFrom:l,discoveredFromType:"url"}}catch{return {discoveredFrom:l,discoveredFromType:"url"}}return {discoveredFrom:l,discoveredFromType:"url"}},a=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,c=oe(e,r,a,n,"page");if(!c)return;if(!Ve(c)||mo(c)){if(G(e,c)){n.info(`Seeded/non-page URL queued as asset: ${c.toString()}`,{url:c.toString(),source:s}),Q(e,t,c.toString(),n,s);return}n.reject("page",c.toString(),nt(c)?"asset/internal path cannot be page":"not page-like",s);return}let i=c.toString(),{discoveredFrom:d,discoveredFromType:u}=o();d&&!t.pageDiscoverySources.has(i)&&t.pageDiscoverySources.set(i,d),!t.donePages.has(i)&&!t.queuedPages.has(i)&&(t.queuedPages.add(i),t.pageQueue.push(i),t.stats.pagesQueued++,n.debug(`Queued discovered page ${i}`,{phase:"discovery",url:i,discoveredFrom:d,discoveredFromType:u,outputPath:De(e,i)}),(t.stats.pagesQueued%25===0||t.stats.pagesQueued===1)&&n.progress(`Discovery progress: pages ${t.stats.pagesQueued}, assets ${t.stats.assetsQueued}, sitemaps ${t.stats.sitemapsQueued}.`,{pagesQueued:t.stats.pagesQueued,assetsQueued:t.stats.assetsQueued,sitemapsQueued:t.stats.sitemapsQueued,pageQueue:t.pageQueue.length,phase:"discovery"}));}function Q(e,t,r,n,s){let o=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,a=oe(e,r,o,n,"asset");if(!a)return;if(!G(e,a)){n.reject("asset",a.toString(),"not a safe asset path/prefix",s);return}let c=a.toString();!t.doneAssets.has(c)&&!t.queuedAssets.has(c)&&(t.queuedAssets.add(c),t.assetQueue.push(c),t.stats.assetsQueued++,(t.stats.assetsQueued%100===0||t.stats.assetsQueued===1)&&n.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 gr(e,t,r,n,s){let o=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,a=oe(e,r,o,n,"sitemap",Jn(s));if(!a)return;if(!yt(a)){n.reject("sitemap",a.toString(),"not sitemap-like",s);return}let c=a.toString();!t.doneSitemaps.has(c)&&!t.queuedSitemaps.has(c)&&(t.queuedSitemaps.add(c),t.sitemapQueue.push(c),t.stats.sitemapsQueued++,(t.stats.sitemapsQueued%10===0||t.stats.sitemapsQueued===1)&&n.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 oc(e){let t=_e(e),r=[...t.matchAll(/<url>\s*([\s\S]*?)\s*<\/url>/gi)].map(n=>n[1]);return r.length>0?r.map(n=>{let s=n.match(/<loc>\s*([^<]+?)\s*<\/loc>/i);if(!s?.[1])return null;let o=n.match(/<lastmod>\s*([^<]+?)\s*<\/lastmod>/i);return {loc:s[1].trim(),lastmod:rt(o?.[1]??null)}}).filter(n=>!!n):[...t.matchAll(/<loc>\s*([^<]+?)\s*<\/loc>/gi)].map(n=>({loc:n[1].trim(),lastmod:null})).filter(n=>n.loc)}function ic(e){return e.split(",").map(t=>t.trim().split(/\s+/)[0]).filter(Boolean)}function ac(e){let t=[],r=/url\(\s*(?:"([^"]+)"|'([^']+)'|([^)]*?))\s*\)/gi;for(let n of e.matchAll(r)){let s=e.slice(Math.max(0,n.index-8),n.index);if(/\bnew\s+$/i.test(s))continue;let o=(n[1]||n[2]||n[3]||"").trim();o&&t.push(o);}return t}function cc(e){return e.replace(/<script\b[^>]*>[\s\S]*?<\/script\s*>/gi,"")}function lc(e,t){if(t==="dom-style")return true;let r=(()=>{try{return C.extname(new URL(e).pathname).toLowerCase()}catch{return ""}})();return !(r===".js"||r===".mjs"||r===".map"||t.includes("javascript"))}function pr(e){return e.trim().replace(/\\/g,"/").replace(/^https?:\/\/[^/]+/i,"").replace(/^\/+/,"")}async function yo(e,t,r,n){let s=[];await D.mkdir(t,{recursive:true});let o=await D.readdir(e,{withFileTypes:true});for(let a of o){let c=C.join(e,a.name),i=C.join(t,a.name);if(a.isDirectory()){s.push(...await yo(c,i,r,n));continue}await bo(c,i,r,n)&&s.push(i);}return s}async function uc(e,t){try{let[r,n]=await Promise.all([D.stat(e),D.stat(t)]);if(r.size!==n.size)return !1;let[s,o]=await Promise.all([D.readFile(e),D.readFile(t)]);return s.equals(o)}catch{return false}}async function bo(e,t,r,n){let s=C.resolve(t);return r.has(s)?(n.debug("Skipped post-crawl copy over crawled page output",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):await uc(e,t)?(n.debug("Skipped unchanged post-crawl copy target",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):(await D.mkdir(C.dirname(t),{recursive:true}),await D.copyFile(e,t),true)}async function dc(e,t,r){let n=Object.entries(e.postCrawlCopyMap||{}).map(([c,i])=>({sourcePath:c.trim(),prefix:String(i||"").trim()}));if(n.length===0)return [];let s=0,o=[],a=new Set([...t.donePages].map(c=>C.resolve(De(e,c))));r.mark("copy-extra-paths");for(let c of n){let i=c.sourcePath,d=c.prefix;if(!i||!d){r.warn("Skipped post-crawl copy mapping with empty key/value",{sourcePath:i,prefixPath:d});continue}let u=ka(i);if(!u.resolvedPath){r.warn("Skipped post-crawl copy source because alias root is not configured",{sourcePath:i,alias:u.alias||"",requiredEnv:u.requiredEnv||"",prefixPath:d});continue}let l=u.resolvedPath,g=pr(d);if(!g){r.warn("Skipped post-crawl copy mapping with invalid export prefix",{sourcePath:i,prefixPath:d});continue}let p;try{p=await D.stat(l);}catch{r.warn("Skipped post-crawl copy source because it does not exist",{sourcePath:i,sourceAbs:l,prefixPath:d});continue}let f=C.resolve(e.outputDir,g);if(p.isDirectory()){let y=await yo(l,f,a,r);o.push(...y),s+=y.length,y.length>0&&r.progress(`Copied static directory to export: ${l} -> /${g}`,{phase:"copy-extra-paths",sourcePath:l,targetPath:`/${g}`,copiedItems:s,changedFiles:y.length});continue}let w=C.basename(l),m=d.endsWith("/")?C.join(f,w):f;await bo(l,m,a,r)&&(o.push(m),s++,r.progress(`Copied static file to export: ${l} -> /${pr(d.endsWith("/")?`${d}${w}`:d)}`,{phase:"copy-extra-paths",sourcePath:l,targetPath:`/${pr(d.endsWith("/")?`${d}${w}`:d)}`,copiedItems:s}));}return r.endMark("copy-extra-paths",{mappedSources:n.length,copiedItems:s}),o}function wt(e,t,r,n,s){let o=new Set,a=_e(r).replace(/\\\//g,"/"),c=s==="page-html"?cc(a):a,i=lc(t,s);if(i){for(let d of ac(c)){let u=oe(e,d,t,n,`${s}:css-url`);u&&G(e,u)&&o.add(u.toString());}for(let d of c.matchAll(/@import\s+(?:url\()?\s*['"]?([^'"\s;]+)['"]?\s*\)?/gi)){let u=oe(e,d[1],t,n,`${s}:css-import`);u&&G(e,u)&&o.add(u.toString());}}if(i||s==="page-html")for(let d of c.matchAll(/<\?xml-stylesheet[^>]+href=["']([^"']+)["'][^>]*\?>/gi)){let u=oe(e,d[1],t,n,`${s}:xml-stylesheet`);u&&G(e,u)&&o.add(u.toString());}if(s==="page-html")for(let d of bs(a)){let u=d.startsWith("//")?`${new URL(e.sourceOrigin).protocol}${d}`:d;if(mn(u)){n.ignore(`${s}:serialized-url`,u,"looks like JavaScript/code fragment",t);continue}let l=oe(e,u,t,n,`${s}:serialized-url`);l&&G(e,l)?o.add(l.toString()):l&&n.ignore("asset",l.toString(),"serialized URL did not pass safe asset path filter",t);}if(s!=="page-html"&&nc(t))for(let d of Ss(a)){if(mn(d)||!/\.(?:js|mjs)(?:[?#]|$)/i.test(d))continue;let u=oe(e,d,t,n,`${s}:relative-asset`);u&&G(e,u)&&o.add(u.toString());}return [...o]}function So(e,t,r,n){let s=new Set,o=_e(r);for(let a of o.matchAll(/href=["']([^"'#\s][^"']*?)["']/gi)){let c=a[1],i=oe(e,c,t,n,"page-link");i&&(uo(e,i)||Ve(i)&&!nt(i)&&!G(e,i)&&s.add(i.toString()));}return [...s]}async function Js(e,t,r,n,s,o,a=false,c=[]){let i=new Set(c),d=0,u=new Map,l=(g,p)=>{let f=[p],w=new Set;for(;f.length>0;){let m=f.pop();if(m===g)return true;w.has(m)||(w.add(m),f.push(...u.get(m)??[]));}return false};for(;r.sitemapQueue.length>0;){let g=r.sitemapQueue.shift();r.queuedSitemaps.delete(g);let p=ke(g,n,"sitemap","sitemap-queue");if(!p)continue;let f=p.toString();if(r.doneSitemaps.has(f))continue;r.doneSitemaps.add(f),n.sitemap(`Fetching sitemap ${f}`,{url:f});let w=true;try{let m=await t.get(f,{timeout:6e4});if(w=!1,!m.ok()){if(n.skip("sitemap",f,m.status()),a&&!i.has(f))throw new Error(`Required child content-sync sitemap returned HTTP ${m.status()}: ${f}`);continue}let h=await m.body(),y=m.url()||f,b=ke(y,n,"sitemap-response",f);if(!b)continue;let P=b.toString();r.successfulSitemaps.add(f),r.successfulSitemaps.add(P),i.has(f)&&(r.resolvedSitemapRoots.add(P),d+=1);let O=await hn(e,r.assetMap,P,h,r,n);le(s,e,O.outputPath),Ce(s,O.outputPath);let x=h.toString("utf8");/<sitemapindex(?:\s|>)/i.test(x)?r.sitemapIndexUrls.add(P):r.sitemapLeafUrls.add(P);let k=wt(e,P,x,n,"sitemap");for(let A of k)Q(e,r,A,n,P);let R=[];for(let A of oc(x)){let I=oe(e,A.loc,P,n,"sitemap-loc",!0);if(!I){if(a)throw new Error(`Content-sync sitemap contains an inadmissible location: ${A.loc}`);continue}if(yt(I)){if(l(P,I.toString()))throw new Error(`Sitemap index cycle detected: ${P} -> ${I.toString()}`);if(u.has(P)||u.set(P,new Set),u.get(P).add(I.toString()),R.push(I.toString()),a&&r.doneSitemaps.has(I.toString())&&!r.successfulSitemaps.has(I.toString()))throw new Error(`Required child content-sync sitemap was not resolved: ${I.toString()}`);gr(e,r,I.toString(),n,P);}else if(a){if(Ve(I)&&!G(e,I)){let K=o.requiredUrls.has(I.toString()),F=o.manifest.pages[I.toString()],S=!!F&&!!await Ye(F.outputPath);if(!K&&!S)throw new Error(`Content-sync sitemap coverage is incomplete for ${I.toString()}.`)}n.ignore("sitemap-loc",I.toString(),"content-sync sitemap-only traversal does not enqueue urlset locations",P);}else G(e,I)?Q(e,r,I.toString(),n,P):Ve(I)?(A.lastmod&&(r.sitemapLastmodByPage[I.toString()]=A.lastmod),st(e,r,I.toString(),n,P)):n.reject("sitemap-loc",I.toString(),"not page/sitemap/asset-like",P);}o.trackManifest&&(o.manifest.assets[P]={url:P,outputPath:O.outputPath,isText:!0,contentType:m.headers()["content-type"]??"application/xml",cacheControl:un(m.headers()),changeToken:null,tokenSource:null,discoveredAssets:[...new Set([...k,...R])],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!1},await He(e,o));}catch(m){n.error(`Failed sitemap ${f}`,{url:f,error:String(m)});let h=a&&i.has(f)&&w;if(a&&!h)throw m}}if(a&&i.size>0&&d===0)throw new Error(`None of the configured content-sync sitemap roots could be resolved: ${[...i].join(", ")}`)}async function pc(e,t,r,n,s,o,a){let c=oe(e,n,e.sourceOrigin,s,"asset-fetch");if(!c||!G(e,c))return;let i=c.toString();if(r.doneAssets.has(i))return;let d=o.enabled||o.strictTargeted?o.previousManifest.assets[i]:void 0,u=o.trackManifest?await dr(e,t,o,i,s):null,l=u?.token??null,g=Xr(d?.changeToken,u),p=o.strictTargeted&&(!u?.supported||!u.token)&&en(e.sourceOrigin,i),f=o.strictTargeted?o.manifest.assets[i]:void 0;if(f?.lastSeenRunId===o.runId&&await Ye(f.outputPath)){r.doneAssets.add(i);for(let m of f.discoveredAssets||[])Q(e,r,m,s,i);le(a,e,f.outputPath),ft(a,e,f.outputPath,f.cacheControl),f.isText&&Ce(a,f.outputPath),s.info(`Resumed completed content-sync asset ${i}`,{url:i,source:"content-sync-checkpoint"}),Ze(r,s);return}if(d&&(g||p)&&await Ye(d.outputPath)){o.manifest.assets[i]={...d,changeToken:l,tokenSource:u?.tokenSource??d.tokenSource,discoveredAssets:[...d.discoveredAssets||[]],lastSeenRunId:o.runId,unreferencedRuns:0},delete o.manifest.pages[i],r.doneAssets.add(i);for(let m of d.discoveredAssets||[])Q(e,r,m,s,i);ft(a,e,d.outputPath,d.cacheControl),s.info(`Reused unchanged trusted asset ${i}.`,{url:i,tokenSource:u?.tokenSource??"unknown",phase:"download-assets",source:g?"asset-change-token-match":"content-sync-release-fingerprint"}),Ze(r,s);return}let w=(m,h={})=>{if(!d||o.strictTargeted)return false;o.manifest.assets[i]={...d,discoveredAssets:[...d.discoveredAssets||[]],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0},delete o.manifest.pages[i],r.doneAssets.add(i);for(let y of d.discoveredAssets||[])Q(e,r,y,s,i);return s.warn(`Keeping previous incremental asset ${i} after fetch issue.`,{url:i,reason:m,preservedFromPreviousManifest:true,...h,phase:"download-assets"}),Ze(r,s),true};s.asset(`Fetching asset ${i}`,{url:i});try{let m=ht(e,r.assetMap,i),h=m?await Ye(m):null;if(m&&h&&!o.strictTargeted)try{let F=await se(`fetch asset headers ${i}`,6e4,()=>t.head(i,{timeout:6e4})),S=F.headers();if(F.ok()&&tc(h,S)){let q=await go(m);if(q!==null){let ue=F.url()||i,H=ke(ue,s,"asset-response",i);if(!H)return;let B=H.toString(),T=await hn(e,r.assetMap,B,q,r,s,S);T.wroteFile&&(le(a,e,T.outputPath),$e(T.outputPath)&&Ce(a,T.outputPath));let W=un(S)||d?.cacheControl||null;ft(a,e,T.outputPath,W),r.doneAssets.add(i),s.info(`Reused existing asset ${i} based on response headers.`,{url:i,filePath:m,lastModified:S["last-modified"]||"",contentLength:S["content-length"]||"",phase:"download-assets"}),Ze(r,s);let U=fn(c,S),z=d?.discoveredAssets?[...d.discoveredAssets]:[];if(U||d?.isText){let J=q.toString("utf8"),de=wt(e,B,J,s,"asset:cached");(de.length>0||!d)&&(z=de);for(let Se of z)Q(e,r,Se,s,B);}if(o.trackManifest){let J=U||!!d?.isText;o.manifest.assets[i]={url:i,outputPath:T.outputPath,isText:J,contentType:ln(S)||d?.contentType||null,cacheControl:W,changeToken:u?.supported?u.token:null,tokenSource:u?.supported?u.tokenSource??null:null,discoveredAssets:z,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!J},delete o.manifest.pages[i],(o.enabled||o.strictTargeted)&&J&&await He(e,o);}return}}}catch{}let y=await se(`fetch asset ${i}`,6e4,()=>t.get(i,{timeout:6e4}));if(!y.ok()){if(w(`http-${y.status()}`,{status:y.status()}))return;if(o.strictTargeted)throw new Error(`Required content-sync asset returned HTTP ${y.status()}: ${i}`);s.skip("asset",i,y.status());return}let b=await se(`read asset response ${i}`,6e4,()=>y.body()),P=y.url()||i,O=ke(P,s,"asset-response",i);if(!O)return;let x=O.toString(),k=y.headers(),R=await hn(e,r.assetMap,x,b,r,s,k);R.wroteFile&&(le(a,e,R.outputPath),$e(R.outputPath)&&Ce(a,R.outputPath));let A=un(k);ft(a,e,R.outputPath,A),r.doneAssets.add(i),Ze(r,s);let I=fn(c,k),K=[];if(I){let F=b.toString("utf8");K=wt(e,x,F,s,`asset:${C.extname(c.pathname).toLowerCase()||k["content-type"]||"unknown"}`);for(let S of K)Q(e,r,S,s,x);}o.trackManifest&&(o.manifest.assets[i]={url:i,outputPath:R.outputPath,isText:I,contentType:ln(k),cacheControl:A,changeToken:u?.supported?u.token:null,tokenSource:u?.supported?u.tokenSource??null:null,discoveredAssets:K,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!I},delete o.manifest.pages[i],(o.enabled||o.strictTargeted)&&I&&await He(e,o));}catch(m){if(w("fetch-error",{error:String(m)}))return;if(s.error(`Failed asset ${i}`,{url:i,error:String(m)}),o.strictTargeted)throw m}finally{r.doneAssets.has(i)||(r.doneAssets.add(i),Ze(r,s));}}async function mc(e,t,r,n,s,o){let a=Math.max(1,Number(e.assetDownloadConcurrency||e.concurrency||1)),c=Array.from({length:a},async()=>{for(;r.assetQueue.length>0;){let i=r.assetQueue.shift();i&&(r.queuedAssets.delete(i),await pc(e,t,r,i,n,s,o));}});await Promise.all(c);}function fr(e,t){let r=me(e.outputDir,t);if(!r)throw new Error(`Remote export output is outside outputDir: ${t}`);return r}function gc(e,t){return fn(e,{"content-type":t})}function Qs(e){return /(?:^|\/)index\.html?$|\.html?$/i.test(e)}async function fc(e,t,r,n,s,o,a,c,i){let d=Math.max(1,Math.min(100,i)),u=20;for(;r.assetQueue.length>0;){let l=r.assetQueue.splice(0,d*u);for(let w of l)r.queuedAssets.delete(w);await bn(e,t,s,l,n);let g=[];for(let w of l){let m=s.manifest.assets[w],h=m?me(e.outputDir,m.outputPath):null,y=h?a.objects[h]:void 0,b=s.changeTokenCache.get(w)??null;if(!(s.enabled&&m&&y?.kind==="asset"&&y.sourceUrl===w&&(Xr(m.changeToken,b)||(!b?.supported||!b.token)&&en(e.sourceOrigin,w)))||!m||!h||!y){g.push(w);continue}s.manifest.assets[w]={...m,changeToken:b?.token??null,tokenSource:b?.tokenSource??m.tokenSource,lastSeenRunId:s.runId,unreferencedRuns:0};for(let O of m.discoveredAssets||[])Q(e,r,O,n,w);r.doneAssets.add(w),ft(o,e,m.outputPath,m.cacheControl),n.info(`Reused unchanged S3-native asset ${w}.`,{url:w,relativePath:h,tokenSource:b?.tokenSource??"release-fingerprint",phase:"download-assets",execution:"lambda"}),Ze(r,n);}if(g.length===0)continue;let p=[];for(let w=0;w<g.length;w+=u)p.push(g.slice(w,w+u));let f=await Promise.all(p.map(w=>c.fetch({urls:w,ignoreHttpsErrors:e.ignoreHttpsErrors,timeoutMs:6e4})));for(let w of f)for(let m of w.assets){let h=m.requestedUrl;try{if(m.outcome!=="fetched"||!m.object||!m.finalUrl){let A=m.error||`Remote asset fetch ended with ${m.outcome}.`;if(n.error(`Failed remote asset ${h}`,{url:h,status:m.httpStatus,outcome:m.outcome,error:A,execution:"lambda"}),s.strictTargeted)throw new Error(A);continue}let y=ke(m.finalUrl,n,"remote-asset-response",h);if(!y)continue;let b=Ft(e,y.toString()),P=b.originalFilePath,O=b.originalPublicUrlWithSearch;if(b.preferQueryHashed&&b.hashedFilePath&&b.hashedPublicUrl&&ho(r,b.originalFilePath,m.object.sha256)){let A=lo(e,r.assetMap,y.toString());P=A?.outputPath??b.hashedFilePath,O=A?.publicUrl??b.hashedPublicUrl;}gn(e,r.assetMap,y.toString(),O,b.originalPublicUrl),y.toString()!==h&&gn(e,r.assetMap,h,O,b.originalPublicUrl),wo(r,b.originalFilePath,m.object.sha256);let x=fr(e,P),k=m.contentType?.trim()||"application/octet-stream",R=gc(y,k);gt(a,{relativePath:x,sourceUrl:h,kind:"asset",contentType:k,cacheControl:m.cacheControl,isText:R,object:m.object}),le(o,e,P),R&&Ce(o,P),ft(o,e,P,m.cacheControl);for(let A of m.discoveredAssets)Q(e,r,A,n,y.toString());r.stats.assetsSaved++,s.trackManifest&&(s.manifest.assets[h]={url:h,outputPath:P,isText:R,contentType:k,cacheControl:m.cacheControl,changeToken:null,tokenSource:null,discoveredAssets:[...m.discoveredAssets],lastCrawledAt:new Date().toISOString(),lastSeenRunId:s.runId,unreferencedRuns:0,rewriteComplete:!R},delete s.manifest.pages[h]),n.debug(`Remote asset fetch completed ${h}`,{url:h,finalUrl:y.toString(),relativePath:x,bytes:m.object.bytes,taskId:w.taskId,attempts:w.attempts,execution:"lambda"});}finally{r.doneAssets.add(h),Ze(r,n);}}}}async function hc(e,t,r,n){for(let s of [...t.changedFiles].sort()){if(!Ws(r,s))continue;let o=C.join(e.outputDir,s),a=await D.readFile(o).catch(u=>{throw new Error(`Coordinator-owned output is missing before S3 staging: ${s}`,{cause:u})}),c=$e(o),i=String(Os.lookup(s)||"")||(c?"text/plain; charset=utf-8":"application/octet-stream"),d=await n.writeWorkspaceObject({key:`${r.workspace.prefix}jobs/${r.jobId}/coordinator/${Ee(s,40)}-${C.basename(s)}`,body:a,contentType:i,cacheControl:Qs(s)?e.s3.htmlCacheControl:e.s3.assetCacheControl});gt(r,{relativePath:s,sourceUrl:null,kind:"generated",contentType:i,cacheControl:Qs(s)?e.s3.htmlCacheControl:e.s3.assetCacheControl,isText:c,object:d});}}function wc(e){let t=Math.ceil(e/1e3);return `${t} second${t===1?"":"s"}`}function Be(e){return Math.max(6e4,e.navigationTimeoutMs*2,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+15e3)}function $t(e){return Math.max(15e3,Math.min(6e4,Be(e)))}function Po(e){return Math.max(15e3,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+1e4)}function Pn(e){return e.stats.pagesCompleted}function wn(e){let r=e.options?.browserProcess;return {pid:typeof r?.process?.pid=="number"?r.process.pid:null,kill:typeof r?.kill=="function"?r.kill.bind(r):null}}async function cn(e,t,r){let n=wn(e);try{if(n.kill){await n.kill(),t.warn(`Force-killed crawl worker browser after ${r}`,{browserPid:n.pid,reason:r});return}if(n.pid!==null){process.kill(n.pid,"SIGKILL"),t.warn(`Sent SIGKILL to crawl worker browser after ${r}`,{browserPid:n.pid,reason:r});return}t.warn(`Could not resolve crawl worker browser PID after ${r}`,{reason:r});}catch(s){t.error(`Failed to force-kill crawl worker browser after ${r}`,{browserPid:n.pid,reason:r,error:String(s)});}}async function se(e,t,r){let n;try{return await Promise.race([r(),new Promise((s,o)=>{n=globalThis.setTimeout(()=>{o(new Mt(e,t));},t);})])}finally{n!==void 0&&globalThis.clearTimeout(n);}}var Mt=class extends Error{constructor(r,n){super(`${r} timed out after ${wc(n)}.`);this.operation=r;this.timeoutMs=n;this.name="OperationTimeoutError";}operation;timeoutMs};async function yc(e,t){let r=Po(e);await t.evaluate(async n=>{await new Promise(s=>{let o=0,a=700,c=Date.now(),i=()=>{window.clearInterval(d),window.scrollTo(0,0),s();},d=window.setInterval(()=>{if(Date.now()-c>=n.maxDurationMs){i();return}window.scrollBy(0,a),o+=a;let u=document.body?.scrollHeight||0,l=document.documentElement?.scrollHeight||0,g=Math.max(u,l);o>=g+window.innerHeight&&i();},120);});},{maxDurationMs:r});}async function bc(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 yc(e,t),await t.waitForTimeout(e.readiness.fallbackWaitMs);}async function Sc(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 Ro(e,t){return e.noJavaScriptRenderPathPrefixes.some(r=>Sn(t,r))}async function Pc(e,t,r,n,s=true){let o=new Set;if(s){let c=await t.evaluate(()=>{let i=new Set,d=["href","src","poster","data-src","data-lazy-src","data-original","data-bg","data-background","data-href","data-resource-url"],u=new Set,l=["srcset","data-srcset","data-lazy-srcset"],g=p=>{p.querySelectorAll("*").forEach(f=>{for(let m of d){let h=f.getAttribute(m);h&&i.add(h);}for(let m of l){let h=f.getAttribute(m);h&&h.split(",").forEach(y=>{let b=y.trim().split(/\s+/)[0];b&&i.add(b);});}let w=f.getAttribute("style");w&&u.add(w),f.shadowRoot&&g(f.shadowRoot);});};return g(document),{attrs:[...i],styles:[...u]}});for(let i of c.attrs)for(let d of ic(i)){let u=oe(e,d,r,n,"dom-attr");u&&G(e,u)&&o.add(u.toString());}for(let i of c.styles)for(let d of wt(e,r,i,n,"dom-style"))o.add(d);}let a=await t.content();for(let c of wt(e,r,a,n,"page-html"))o.add(c);return [...o]}async function yn(e,t,r,n,s){let o=De(e,t);await We(o,r,"utf8");let a=await D.stat(o);s?.debug(`Saved page output ${t}`,{phase:"save-pages",url:t,outputPath:o,bytes:a.size});let c=hr(n,o),i=Math.min(n.stats.pagesQueued,Pn(n)+1);s&&c&&(n.stats.pagesSaved%10===0||n.stats.pagesSaved===1)&&s.progress(`Page progress: processed ${i}, rendered ${n.stats.pagesRendered}, saved ${n.stats.pagesSaved}, discovered ${n.stats.pagesQueued}.`,{donePages:i,claimedPages:n.donePages.size,pagesRendered:n.stats.pagesRendered,pagesSaved:n.stats.pagesSaved,pagesQueued:n.stats.pagesQueued,pageQueue:n.pageQueue.length,phase:"save-pages"});}function hr(e,t){let r=C.resolve(t);return e.savedPageOutputs.has(r)?false:(e.savedPageOutputs.add(r),e.stats.pagesSaved=e.savedPageOutputs.size,true)}async function wr(e,t,r,n,s){let o=me(e.outputDir,n);if(!o)return;if(s){let c=s.objects[o];c?.kind==="page"&&c.sourceUrl===r&&c.object?.bucket===s.workspace.bucket&&typeof c.object.key=="string"&&c.object.key.startsWith(s.workspace.prefix)&&hr(t,n);return}(await Ye(n).catch(()=>null))?.isFile()&&hr(t,n);}function vo(e){let t=Pn(e);return t%5===0||t===1||t===e.stats.pagesQueued}function Co(e,t){let r=Pn(e);t.progress(`Render progress: processed ${r}, rendered ${e.stats.pagesRendered}, saved ${e.stats.pagesSaved}, discovered ${e.stats.pagesQueued}.`,{donePages:r,claimedPages:e.donePages.size,pagesRendered:e.stats.pagesRendered,pagesSaved:e.stats.pagesSaved,pagesQueued:e.stats.pagesQueued,phase:"render-pages"});}async function Rc(e){await es(e),await e.addInitScript(()=>{Object.defineProperty(window,"__WPSUITE_STATIC_EXPORT__",{value:true,writable:false,configurable:true});let t=r=>{if(r==null||String(r)==="")return true;try{let n=new URL(String(r),window.location.href),s=new URL(window.location.href);return n.hash="",s.hash="",n.href===s.href}catch{return false}};try{let r=window.location.assign.bind(window.location),n=window.location.replace.bind(window.location),s=window.location.reload.bind(window.location);Object.defineProperty(window.location,"assign",{configurable:!0,value:o=>{if(t(o)){console.warn("[smartcloud-static-publisher] blocked same-page location.assign",o??"");return}return r(o)}}),Object.defineProperty(window.location,"replace",{configurable:!0,value:o=>{if(t(o)){console.warn("[smartcloud-static-publisher] blocked same-page location.replace",o??"");return}return n(o)}}),Object.defineProperty(window.location,"reload",{configurable:!0,value:()=>{console.warn("[smartcloud-static-publisher] blocked location.reload");}});}catch{}});}async function vc(e,t,r){let n=await e.newContext({viewport:t.viewport,userAgent:"WPSuiteStaticPublisher/0.8 Playwright SitemapOnly",ignoreHTTPSErrors:t.ignoreHttpsErrors,...r?{javaScriptEnabled:false}:{}});return r||await Rc(n),n}async function Cc(e,t,r,n,s,o,a,c,i,d){for(;t.pageQueue.length>0&&!(e.maxPages>0&&t.donePages.size>=e.maxPages);){let u=t.pageQueue.shift();if(!u)break;t.queuedPages.delete(u);try{let l=ke(u,r,"page","page-queue");if(!l)continue;let g=l.toString();if(u=g,t.queuedPages.delete(u),t.donePages.has(u))continue;t.donePages.add(u),o.enabled&&o.seenPages.add(u);let p=yr(e,u),f=Ro(e,l.pathname);if(!Ve(l)||G(e,l)||nt(l)){if(G(e,l)){r.info(`Remote worker redirected non-page URL to asset queue: ${u}`,{url:u,source:"remote-worker-guard"}),Q(e,t,u,r,"remote-worker-guard");continue}r.reject("page",u,"guard rejected non-page URL before remote rendering","remote-worker");continue}let w=o.strictTargeted?o.manifest.pages[u]:void 0;if(w?.lastSeenRunId===o.runId&&await Ye(w.outputPath)){o.seenPages.add(u);for(let R of w.discoveredAssets||[])Q(e,t,R,r,u);le(a,e,w.outputPath),Ce(a,w.outputPath),await wr(e,t,u,w.outputPath,d),r.info(`Resumed completed content-sync page ${u}`,{url:u,source:"content-sync-checkpoint"});continue}let m=await ao(e,s,t,o,u,r);if(o.enabled&&n!=="single-url"&&n!=="content-sync"){let R=o.manifest.pages[u];if(m.action==="reuse"&&R){for(let A of R.discoveredAssets)Q(e,t,A,r,u);for(let A of R.discoveredPages)st(e,t,A,r,u);R.lastSeenRunId=o.runId,R.sitemapLastmod=rt(t.sitemapLastmodByPage[u])??R.sitemapLastmod,m.changeToken?.supported&&(R.changeToken=m.changeToken.token,R.tokenSource=m.changeToken.tokenSource??R.tokenSource),await wr(e,t,u,R.outputPath,d),r.info(`Incremental reuse skipped unchanged page ${u}`,{url:u,mode:"incremental",reason:m.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}r.page(`Rendering remotely ${u}`,{url:u,execution:"lambda"});let h=await i.render({sourceOrigin:e.sourceOrigin,url:u,options:{ignoreHttpsErrors:e.ignoreHttpsErrors,javaScriptEnabled:!f,viewport:e.viewport,navigationTimeoutMs:e.navigationTimeoutMs,autoScrollTimeoutMs:Po(e),readiness:e.readiness},downloadHtml:!d}),y=h.page,b=y.finalUrl?ke(y.finalUrl,r,"remote-final-response",u)?.toString():void 0;if(n==="content-sync"&&b&&b!==g){if(o.deleteOnMissingUrls.has(u)){await jt(e,o,a,u,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive route redirected to ${b}`,{url:u,finalResponseUrl:b,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${u} -> ${b}`)}let P=y.httpStatus;if(p){if(P!==404){r.error(`Generated 404 request path returned unexpected status for ${u}`,{url:u,expectedStatus:404,status:P,requestPath:e.generated404RequestPath,execution:"lambda"});continue}}else if(P!==null&&P>=400){if(n==="content-sync"&&(P===404||P===410)&&o.deleteOnMissingUrls.has(u)){await jt(e,o,a,u,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive page ${u}`,{url:u,status:P,source:"content-sync-tombstone"});continue}if(n==="content-sync")throw new Error(`Required content-sync route returned HTTP ${P}: ${u}`);r.skip("page",u,P,t.pageDiscoverySources.get(u));continue}t.stats.pagesRendered++;let O=new Set;if(!d){if(h.html===null)throw new Error("Remote render did not return HTML for local output mode.");for(let R of wt(e,g,h.html,r,"remote-page-html"))O.add(R);}for(let R of [...y.discoveredAssets,...y.networkUrls]){let A=oe(e,R,g,r,"remote-render");A&&G(e,A)&&O.add(A.toString());}for(let R of O)Q(e,t,R,r,u);let x=n!=="single-url"&&n!=="content-sync"&&!p?[...new Set([...!d&&h.html!==null?So(e,u,h.html,r):[],...y.discoveredPages])]:[];for(let R of x)st(e,t,R,r,u);for(let R of y.cookies){let A=R.sameSite==="Strict"||R.sameSite==="Lax"||R.sameSite==="None"?R.sameSite:"Unspecified",I=!Number.isFinite(R.expires)||R.expires<=0;c.add({name:R.name,domain:R.domain,path:R.path||"/",expires:I?null:R.expires,session:I,secure:R.secure,httpOnly:R.httpOnly,sameSite:A,partitioned:R.partitioned===!0});}let k=De(e,u);if(d){if(!y.html)throw new Error("Remote render completed without a workspace HTML object.");gt(d,{relativePath:fr(e,k),sourceUrl:u,kind:"page",contentType:y.contentType?.trim()||"text/html; charset=utf-8",cacheControl:e.s3.htmlCacheControl,isText:!0,object:y.html}),hr(t,k);}else {if(h.html===null)throw new Error("Remote render did not return HTML for local output mode.");await yn(e,u,h.html,t,r);}le(a,e,k),Ce(a,k),o.trackManifest&&(o.manifest.pages[u]={url:u,outputPath:k,contentType:y.contentType?.trim()||null,changeToken:m.changeToken?.supported?m.changeToken.token:null,tokenSource:m.changeToken?.supported?m.changeToken.tokenSource??null:null,sitemapLastmod:rt(t.sitemapLastmodByPage[u])??null,discoveredPages:x,discoveredAssets:[...O],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await He(e,o)),r.debug(`Remote render completed ${u}`,{url:u,taskId:h.taskId,attempts:h.attempts,execution:"lambda"});}catch(l){if(r.error(`Failed remote page ${u}`,{url:u,error:String(l),execution:"lambda",...l instanceof we?{status:l.status,attempts:l.attempts}:{}}),l instanceof we){t.pageHttpFailures.set(u,l);continue}if(o.strictTargeted)throw l}finally{t.stats.pagesCompleted++,vo(t)&&Co(t,r);}}}async function kc(e,t,r,n,s,o,a,c){let i=await chromium.launch({headless:true}),d=wn(i);d.pid!==null&&r.debug(`Launched crawl worker browser PID ${d.pid}`,{browserPid:d.pid});try{for(;t.pageQueue.length>0;){i||(i=await chromium.launch({headless:!0}),d=wn(i),d.pid!==null&&r.debug(`Relaunched crawl worker browser PID ${d.pid}`,{browserPid:d.pid}));let u=i;if(e.maxPages>0&&t.donePages.size>=e.maxPages)break;let l=t.pageQueue.shift();if(!l)break;t.queuedPages.delete(l);let g=null,p=null,f=!1,w=null,m=[],h=new Set;try{let y=ke(l,r,"page","page-queue");if(!y)continue;let b=y.toString();if(l=b,t.queuedPages.delete(l),t.donePages.has(l))continue;t.donePages.add(l),o.enabled&&o.seenPages.add(l);let P=yr(e,l),O=Ro(e,y.pathname);if(!Ve(y)||G(e,y)||nt(y)){if(G(e,y)){r.info(`Worker redirected non-page URL to asset queue: ${l}`,{url:l,source:"worker-guard"}),Q(e,t,l,r,"worker-guard");continue}r.reject("page",l,"guard rejected non-page URL before rendering","worker");continue}let x=o.strictTargeted?o.manifest.pages[l]:void 0;if(x?.lastSeenRunId===o.runId&&await Ye(x.outputPath)){o.seenPages.add(l);for(let T of x.discoveredAssets||[])Q(e,t,T,r,l);le(a,e,x.outputPath),Ce(a,x.outputPath),await wr(e,t,l,x.outputPath),r.info(`Resumed completed content-sync page ${l}`,{url:l,source:"content-sync-checkpoint"});continue}let k=await ao(e,s,t,o,l,r);if(o.enabled&&n!=="single-url"&&n!=="content-sync"){let T=o.manifest.pages[l];if(k.action==="reuse"&&T){for(let W of T.discoveredAssets)Q(e,t,W,r,l);for(let W of T.discoveredPages)st(e,t,W,r,l);T.lastSeenRunId=o.runId,T.sitemapLastmod=rt(t.sitemapLastmodByPage[l])??T.sitemapLastmod,k.changeToken?.supported&&(T.changeToken=k.changeToken.token,T.tokenSource=k.changeToken.tokenSource??T.tokenSource),await wr(e,t,l,T.outputPath),r.info(`Incremental reuse skipped unchanged page ${l}`,{url:l,mode:"incremental",reason:k.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}g=await se(`create page context ${l}`,Be(e),()=>vc(u,e,O)),p=await se(`create page ${l}`,Be(e),()=>g.newPage()),m=await g.cookies();let R=!1,A=new Set;await p.route("**/*",async T=>{let W=T.request();try{let U=oe(e,W.url(),e.sourceOrigin,r,"network-request");U&&G(e,U)&&(A.add(U.toString()),Q(e,t,U.toString(),r,l));}catch{}if(W.isNavigationRequest()&&W.frame()===p.mainFrame())try{let U=new URL(W.url()),z=new URL(b);if(U.hash="",z.hash="",U.href===z.href){if(R){r.warn(`Blocked same-page navigation/reload for ${l}`,{url:l,requestUrl:W.url()});try{await T.abort("aborted");}catch(J){r.debug(`Ignored canceled route while blocking reload for ${l}`,{url:l,error:String(J)});}return}R=!0;}}catch{}try{await T.continue();}catch(U){r.debug(`Ignored canceled network route for ${l}`,{url:l,requestUrl:W.url(),error:String(U)});}}),p.on("response",T=>{try{let U=oe(e,T.url(),e.sourceOrigin,r,"network-response");U&&G(e,U)&&(A.add(U.toString()),Q(e,t,U.toString(),r,l));}catch{}let W=T.headerValues("set-cookie").then(U=>{for(let z of U)c.add(ks(z,T.url()));}).catch(()=>{}).then(()=>{});h.add(W),W.finally(()=>h.delete(W));}),r.page(`Rendering ${l}`,{url:l});let I=null,K=null;try{if(I=await p.goto(l,{waitUntil:"domcontentloaded",timeout:e.navigationTimeoutMs}),I&&I.ok())try{K=await se(`read response body ${l}`,Be(e),()=>I.text());}catch{K=null;}}catch(T){let W=t.pageHttpFailures.get(l);if(W){let U=(t.pageHttpAttempts.get(l)??0)+1;throw t.pageHttpAttempts.set(l,U),new we(l,W.status,U,`Retry navigation failed: ${String(T)}`)}r.warn(`Navigation issue for ${l}; saving current DOM if available`,{url:l,error:String(T)});}let F=I?.status();if(er(F)){let T=(t.pageHttpAttempts.get(l)??0)+1;throw t.pageHttpAttempts.set(l,T),new we(l,F,T)}if(I)t.pageHttpFailures.delete(l);else if(t.pageHttpFailures.has(l)){let T=t.pageHttpFailures.get(l),W=(t.pageHttpAttempts.get(l)??0)+1;throw t.pageHttpAttempts.set(l,W),new we(l,T.status,W,"Retry navigation returned no HTTP response.")}if(n==="content-sync"&&I){let T=ke(I.url(),r,"content-sync-final-response",l)?.toString();if(T&&T!==b){if(o.deleteOnMissingUrls.has(l)){await jt(e,o,a,l,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive route redirected to ${T}`,{url:l,finalResponseUrl:T,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${l} -> ${T}`)}}if(P){let T=I?.status()??null;if(T!==404){r.error(`Generated 404 request path returned unexpected status for ${l}`,{url:l,expectedStatus:404,status:T,requestPath:e.generated404RequestPath});continue}r.info(`Capturing rendered 404 page for ${l}`,{url:l,status:T,outputPath:De(e,l)});}else if(I&&!I.ok()){if(n==="content-sync"&&(I.status()===404||I.status()===410)&&o.deleteOnMissingUrls.has(l)){await jt(e,o,a,l,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive page ${l}`,{url:l,status:I.status(),source:"content-sync-tombstone"}),await p.close();continue}if(n==="content-sync")throw new Error(`Required content-sync route returned HTTP ${I.status()}: ${l}`);r.skip("page",l,I.status(),t.pageDiscoverySources.get(l)),await p.close();continue}O?(r.info(`Rendering without JS execution for ${l}`,{url:l,mode:"no-js"}),await se(`wait for rendered page ${l}`,Be(e),()=>Sc(e,p))):await se(`wait for rendered page ${l}`,Be(e),()=>bc(e,p)),t.stats.pagesRendered++;let S=await se(`extract rendered assets ${l}`,Be(e),()=>Pc(e,p,b,r,!O));for(let T of S)Q(e,t,T,r,l);let q=[...new Set([...S,...A])],ue=K??await se(`capture rendered html ${l}`,Be(e),()=>Br(p)),H=n!=="single-url"&&n!=="content-sync"&&!P?So(e,l,ue,r):[];if(n!=="single-url"&&n!=="content-sync"&&!P)for(let T of H)st(e,t,T,r,l);await yn(e,l,ue,t,r);let B=De(e,l);if(le(a,e,B),Ce(a,B),o.trackManifest){let T=I?.headers();o.manifest.pages[l]={url:l,outputPath:B,contentType:ln(T),changeToken:k.changeToken?.supported?k.changeToken.token:null,tokenSource:k.changeToken?.supported?k.changeToken.tokenSource??null:null,sitemapLastmod:rt(t.sitemapLastmodByPage[l])??null,discoveredPages:H,discoveredAssets:q,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await He(e,o);}}catch(y){if(y instanceof we){t.pageHttpFailures.set(l,y),y.attempts<nn?(w=tr(y.attempts),r.warn(`Retrying page after HTTP ${y.status}: ${l}`,{url:l,status:y.status,attempt:y.attempts,maxAttempts:nn,delayMs:w,reason:y.message,execution:"local"})):(t.pageHttpFailures.set(l,y),r.error(`Failed page after HTTP retries ${l}`,{url:l,status:y.status,attempts:y.attempts,error:String(y),execution:"local"}));continue}if(f=y instanceof Mt,o.strictTargeted)throw r.error(`Failed required content-sync page ${l}`,{url:l,error:String(y)}),y;if(f)r.warn(`Skipped partial DOM for timed-out page ${l}`,{url:l,error:String(y),recovery:"recycle-worker-browser"});else try{let b=p?await se(`capture partial html ${l}`,Be(e),()=>Br(p)):"";if(b&&b.trim()){await yn(e,l,b,t,r);let P=De(e,l);if(le(a,e,P),Ce(a,P),o.trackManifest){let O=o.manifest.pages[l]??o.previousManifest.pages[l];o.manifest.pages[l]={url:l,outputPath:P,contentType:O?.contentType??null,changeToken:null,tokenSource:null,sitemapLastmod:rt(t.sitemapLastmodByPage[l])??O?.sitemapLastmod??null,discoveredPages:O?.discoveredPages?[...O.discoveredPages]:[],discoveredAssets:O?.discoveredAssets?[...O.discoveredAssets]:[],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await He(e,o);}r.warn(`Saved partial DOM for ${l}`,{url:l,error:String(y)});}}catch(b){b instanceof Mt&&(f=!0),r.error(`Could not save partial DOM for ${l}`,{url:l,error:String(b)});}r.error(`Failed page ${l}`,{url:l,error:String(y)});}finally{if(!f&&p&&await se(`drain network routes ${l}`,$t(e),()=>p.unrouteAll({behavior:"wait"})).catch(y=>{f=!0,r.warn(`Could not drain network routes for ${l}`,{url:l,browserPid:d.pid,error:String(y),recovery:"recycle-worker-browser"});}),f&&(i=null,await cn(u,r,`page operation timeout for ${l}`),await u.close().catch(()=>{})),h.size>0&&await Promise.allSettled([...h]),!f&&g)try{let y=await g.cookies();c.addAll(xs(m,y));}catch{}w===null&&(t.stats.pagesCompleted++,vo(t)&&Co(t,r)),!f&&p&&await se(`close page ${l}`,$t(e),()=>p.close()).catch(y=>{f=!0,r.warn(`Timed out while closing page ${l}`,{url:l,browserPid:d.pid,error:String(y),recovery:"recycle-worker-browser"});}),!f&&g&&await se(`close page context ${l}`,$t(e),()=>g.close()).catch(y=>{f=!0,r.warn(`Timed out while closing page context for ${l}`,{url:l,browserPid:d.pid,error:String(y),recovery:"recycle-worker-browser"});}),f&&i===u&&(i=null,await cn(u,r,`page cleanup timeout for ${l}`),await u.close().catch(()=>{})),w!==null&&(await setTimeout$1(w),t.donePages.delete(l),t.queuedPages.add(l),t.pageQueue.push(l));}}}finally{if(i){let u=i;await se("close worker browser",$t(e),()=>u.close()).catch(l=>(r.warn("Timed out while closing crawl worker browser",{browserPid:d.pid,error:String(l)}),cn(u,r,"browser.close timeout")));}}}async function xc(e){try{let t=await D.readFile(e),n=(e.endsWith(".gz")?gunzipSync(t):t).toString("utf8").trim();if(!n)return [];try{let s=JSON.parse(n);if(Array.isArray(s))return s;if(s&&typeof s=="object")return [s]}catch{return n.split(/\r?\n/).map(s=>s.trim()).filter(Boolean).map(s=>{try{let o=JSON.parse(s);return o&&typeof o=="object"?o:null}catch{return null}}).filter(s=>s!==null)}return []}catch{return []}}function Gs(e){let t=String(e||"").toLowerCase();return t.includes("timeout")||t.includes("timed out")||t.includes("navigation timeout")}function Ic(e){let t=String(e?.job?.command||"").trim(),r=String(e?.job?.crawlMode||"full").trim();return t!=="publish"&&t!=="crawl"?false:r!=="incremental"}function Tc(e){let t=[e?.archivedAt,e?.job?.endedAt,e?.job?.startedAt];for(let r of t){let n=Date.parse(String(r||""));if(Number.isFinite(n))return n}return 0}async function ko(e){try{let t=await D.readFile(e,"utf8"),r=JSON.parse(t);return r&&typeof r=="object"&&!Array.isArray(r)?r:null}catch{return null}}function Zs(e){let t=String(e.originalFileName||"").toLowerCase();return t.endsWith("errors.jsonl")?0:t.endsWith("errors.json")?1:2}function Ac(e,t){let r=Array.isArray(t?.artifacts)?t.artifacts.filter(n=>String(n?.role||"").trim()==="errors"&&String(n?.storedFileName||"").trim()!=="").sort((n,s)=>Zs(n)-Zs(s)).map(n=>C.join(e,String(n.storedFileName))):[];return [...new Set([...r,C.join(e,"errors.jsonl"),C.join(e,"errors.json"),C.join(e,"errors.jsonl.gz"),C.join(e,"errors.json.gz")])]}async function Ys(e){let t=await ko(C.join(e,"job.json"));for(let r of Ac(e,t)){let n=await xc(r);if(n.length>0)return n}return []}async function Ec(e){let t=C.join(e.logDir,"archive"),r;try{r=await D.readdir(t);}catch{return ""}let n="",s=0;for(let o of r){let a=C.join(t,o);if(!(await D.stat(a).catch(()=>null))?.isDirectory())continue;let i=await ko(C.join(a,"job.json"));if(!Ic(i))continue;let d=Tc(i);d>=s&&(s=d,n=a);}return n}async function $c(e){let t=await Ec(e),r=t!==""?await Ys(t):await Ys(e.logDir),n=new Set;for(let s of r)if(Gs(s.error)||Gs(s.message)){let o=s.url;typeof o=="string"&&o&&n.add(o);}return [...n]}async function Oc(e){let t=[];for(let r of e)if(r.startsWith("@")){let n=r.slice(1),s=await D.readFile(n,"utf8");t.push(...s.split(/\r?\n/).map(o=>o.trim()).filter(o=>o&&!o.startsWith("#")));}else t.push(r);return t}async function Mc(e){if(!e)throw new Error("Content-sync crawl requires an immutable impact plan.");let t=JSON.parse(await D.readFile(e,"utf8"));return vt(t),t}function jc(e,t){let r;try{r=new URL(e);}catch{return false}return t.archiveFamilies.some(n=>{let s=new URL(n.url);if(r.origin!==s.origin)return false;if(r.toString()===s.toString())return true;if(r.searchParams.has("paged")&&r.pathname===s.pathname)return /^\d+$/.test(r.searchParams.get("paged")||"");let o=s.pathname.replace(/\/+$/,""),a=r.pathname.slice(o.length).replace(/^\/+/,""),c=String(n.paginationBase||"page").replace(/^\/+|\/+$/g,"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return r.pathname.startsWith(`${o}/`)&&new RegExp(`^${c}/\\d+/?$`).test(a)})}function Et(e,t,r){let n=r==="page"?De(e,t):Ft(e,t).originalFilePath;return me(e.outputDir,n)}async function jt(e,t,r,n,s){let o=t.manifest.pages[n];if(!o)return;let a=me(e.outputDir,o.outputPath);if(!a)throw new Error(`Manifest-owned page path escaped output root: ${n}`);let c=C.resolve(o.outputPath);Object.entries(t.manifest.pages).some(([d,u])=>d!==n&&!s.has(d)&&C.resolve(u.outputPath)===c)||(await D.rm(o.outputPath,{force:true}),r.deletedFiles.add(a),r.revalidateFiles.delete(a)),delete t.manifest.pages[n];}async function Dc(e,t,r,n,s){let o=t.manifest.assets[n];if(!o)return;let a=me(e.outputDir,o.outputPath);if(!a)throw new Error(`Manifest-owned resource path escaped output root: ${n}`);let c=C.resolve(o.outputPath);Object.entries(t.manifest.assets).some(([d,u])=>d!==n&&!s.has(d)&&C.resolve(u.outputPath)===c)||(await D.rm(o.outputPath,{force:true}),r.deletedFiles.add(a)),delete t.manifest.assets[n];}function Fc(e,t){let r=new Set,n=[...t];for(;n.length>0;){let s=n.shift();if(r.has(s))continue;r.add(s);let o=e.assets[s];for(let a of o?.discoveredAssets??[])try{yt(new URL(a))&&n.push(a);}catch{}}return r}async function Uc(e,t,r,n,s){let o=Fc(r.previousManifest,s);for(let a of o){if(t.successfulSitemaps.has(a))continue;let c=r.previousManifest.assets[a];if(!c)continue;let i=me(e.outputDir,c.outputPath);if(!i)throw new Error(`Manifest-owned sitemap path escaped output root: ${a}`);await D.rm(c.outputPath,{force:true}),n.deletedFiles.add(i),delete r.manifest.assets[a];}await He(e,r);}async function Lc(e){try{let t=await D.readFile(C.join(e.outputDir,"asset-map.json"),"utf8");return JSON.parse(t)}catch{return {}}}async function qc(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 D.mkdir(e,{recursive:true}),await Promise.all(t.map(async r=>{try{await D.unlink(C.join(e,r));}catch(n){if(n.code!=="ENOENT")throw n}}));}function Xs(e,t,r,n,s){let o=oe(e,r,e.sourceOrigin,n,"manual-url");o&&(yt(o)?gr(e,t,o.toString(),n,s):G(e,o)?Q(e,t,o.toString(),n,s):Ve(o)?st(e,t,o.toString(),n,s):n.reject("manual-url",o.toString(),"not page/sitemap/asset-like",s));}async function xo(e=process.argv.slice(2)){let t=[],r=await Ie(void 0,{onRemotePublisherConfigDiagnostic:v=>{t.push(v);}}),n=kt(r.sitemapPaths),s=t.at(-1)??null,o=Pa(e),a=o.mode==="content-sync"?await Mc(o.contentSyncPlanPath):null;if(o.resumeRewrite&&o.mode!=="full")throw new Error("--resume-rewrite is only supported for full crawl/publish jobs.");let c=[];o.mode==="retry-timeouts"&&(c=await $c(r)),o.mode==="single-url"&&(c=await Oc(o.urls)),a&&(c=[...a.directRenderUrls,...a.archiveFamilyUrls,...a.resourceRenderUrls]);let i=o.crawlMode==="incremental"&&o.mode==="full",d=i&&typeof r.subscriptionType=="string"&&r.subscriptionType.trim()!=="";if(a&&r.subscriptionType!=="PROFESSIONAL"&&r.subscriptionType!=="AGENCY")throw new Error("Content-sync requires an active Professional or Agency subscription.");let u=(o.mode==="full"||o.mode==="single-url"||o.mode==="content-sync")&&!o.resumeRewrite,l=o.mode!=="full"||o.resumeRewrite||d,g=l?await Lc(r):{},p=d||o.mode==="single-url"||o.mode==="content-sync"?await Aa(r):so(),f=new Set(c),w=new Set;if(a){for(let v of a.archiveFamilies)if(v.foundPosts===0)for(let j of v.pageUrls)w.add(j);for(let v of Object.keys(p.pages))jc(v,a)&&!f.has(v)&&(c.push(v),w.add(v));}let m={enabled:d,trackManifest:u,manifest:pn(p),previousManifest:pn(p),runId:a?.jobId??`${Date.now()}`,seenPages:new Set,changeTokenCache:new Map,pageChangeTokenPrimePromise:null,manifestSaveChain:Promise.resolve(),manifestPendingChanges:0,lastManifestSavedAt:Date.now(),strictTargeted:!!a,requiredUrls:f,deleteOnMissingUrls:w};await qc(r.logDir);let h=new Qt(r.logDir,r.logLevel),y=o.remoteRender??r.remoteWorkers?.render?.enabled===true,b=ir({explicitPath:o.remoteWorkerConfigPath,runtimeDir:Dt()}),P=null,O=null,x=null,k=null,R=Math.max(1,r.concurrency),A=1,I=r.remoteWorkers?.rewrite?.enabled===true,K=r.remoteWorkers?.deploy?.enabled===true,F=y&&I&&K&&!o.resumeRewrite;if(!F&&o.mode==="full"&&!o.resumeRewrite&&await D.rm(ar(r),{force:true}),y&&!o.resumeRewrite||I){if(!b)throw new Error("Remote Lambda workers are enabled, but the runtime directory is unknown. Run through queue-runner with --runtime-dir, set STATIC_PUBLISHER_RUNTIME_DIR, or use --remote-worker-config for a diagnostic run.");let v=await Xt(b),j=false;if(x=new mt(v,{},{onProgress:M=>{h.progress(`Lambda ${M.operation} batch: ${M.completedItems+M.failedItems}/${M.totalItems}`,{remoteWorkerOperation:M.operation,remoteWorkerTaskId:M.taskId,remoteWorkerStatus:M.status,remoteWorkerCompletedItems:M.completedItems,remoteWorkerFailedItems:M.failedItems,remoteWorkerTotalItems:M.totalItems,remoteWorkerUpdatedAt:M.updatedAt});},onProgressError:M=>{j||(j=true,h.warn("Lambda progress status could not be read; task execution continues.",{error:M.message}));}}),F){if(!v.functions.asset)throw new Error("Full Lambda delegation requires an asset worker. Redeploy the Lambda worker stack and reinstall remote-workers.json.");let M=l?await lr(r):null;k=qs(`crawl-${Ee(m.runId,40)}`,v,M),O=new nr({client:x,jobId:k.jobId,maxAttempts:2}),h.info("S3-native Lambda data plane is enabled.",{execution:"lambda",assetWorker:v.functions.asset,workspaceBucket:v.workspace.bucket,workspacePrefix:v.workspace.prefix});}}if(y&&!o.resumeRewrite){if(!x)throw new Error("Remote render worker client was not initialized.");R=Math.max(1,Math.min(100,F?r.concurrency:r.remoteWorkers?.render?.concurrency??r.concurrency)),A=Math.max(1,Math.min(20,r.remoteWorkers?.render?.batchSize??5)),P=new rr({client:x,jobId:`crawl-${Ee(m.runId,40)}`,maxAttempts:r.remoteWorkers?.render?.maxAttempts??2,batchSize:A,maxConcurrentBatches:R,onRetry:v=>{h.warn(`Retrying remote render for ${v.url}`,{url:v.url,attempt:v.attempt,maxAttempts:v.maxAttempts,reason:v.reason,status:v.status,delayMs:v.delayMs,execution:"lambda"});}}),h.info("Lambda page rendering is enabled.",{remoteWorkerConfigPath:C.resolve(b),concurrency:R,maxAttempts:r.remoteWorkers?.render?.maxAttempts??2,batchSize:A,execution:"lambda"});}if(s){let v={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.",v):h.warn("Remote WP Suite publisher configuration is unavailable or has no active subscription type.",v);}if(i&&!d){let v="Incremental crawl requires an active remote publisher subscription; refusing to fall back to a destructive full crawl.";throw h.error(v,{requestedMode:o.crawlMode,subscriptionType:r.subscriptionType??null,source:"incremental-subscription-guard"}),await h.flush(),new Error(v)}let S={pageQueue:[],queuedPages:new Set,donePages:new Set,savedPageOutputs:new Set,pageHttpAttempts:new Map,pageHttpFailures:new Map,pageDiscoverySources:new Map,assetQueue:[],queuedAssets:new Set,doneAssets:new Set,sitemapQueue:[],queuedSitemaps:new Set,doneSitemaps:new Set,successfulSitemaps:new Set,resolvedSitemapRoots:new Set,sitemapIndexUrls:new Set,sitemapLeafUrls:new Set,sitemapLastmodByPage:{},assetMap:{...g},assetVariantDigestsByOriginalPath:new Map,stats:{pagesQueued:0,pagesRendered:0,pagesCompleted:0,assetsQueued:0,sitemapsQueued:0,assetsSaved:0,pagesSaved:0}},q=Ta(),ue=new Zt,H=Ra();if(H&&!o.resumeRewrite&&await Yt(H,ue.artifact()),await Qe(r),await zr(r,{startedAt:new Date().toISOString(),outputDir:C.resolve(r.outputDir)}),!o.resumeRewrite&&r.pageCache?.purgeBeforeCrawl===true){h.mark("purge-page-cache");let v=await request.newContext({ignoreHTTPSErrors:r.ignoreHttpsErrors});try{let j=await Us(r,v);h.info("WordPress page cache was purged before crawl discovery.",{provider:j.provider,scope:j.scope,purgedAt:j.purgedAt??null}),h.endMark("purge-page-cache",{provider:j.provider,scope:j.scope});}catch(j){throw h.error("WordPress page-cache purge failed; crawl stopped before changing export output.",{error:j instanceof Error?j.message:String(j)}),await h.flush(),j}finally{await v.dispose();}}if(l||await D.rm(r.outputDir,{recursive:true,force:true}),await D.mkdir(r.outputDir,{recursive:true}),l||await zr(r,{startedAt:new Date().toISOString(),outputDir:C.resolve(r.outputDir)}),a){let v=new Set([...a.deleteUrls,...m.deleteOnMissingUrls]);for(let M of a.deleteUrls)await jt(r,m,q,M,v);let j=new Set(a.resourceDeleteUrls);for(let M of a.resourceDeleteUrls)await Dc(r,m,q,M,j);}if(o.resumeRewrite)h.info("Resuming final rewrite from existing output.",{phase:"rewrite-text",mode:o.mode,crawlMode:o.crawlMode});else {let v=await request.newContext({ignoreHTTPSErrors:r.ignoreHttpsErrors});try{if(h.mark("discovery"),o.mode==="full"?(n.forEach(M=>gr(r,S,M,h)),await Js(r,v,S,h,q,m),r.seedPaths.forEach(M=>Xs(r,S,M,h,"seed-path")),r.generated404RequestPath&&st(r,S,r.generated404RequestPath,h,"generated-404-request-path")):(c.forEach(M=>Xs(r,S,M,h,"cli")),a?.sitemapUrls.forEach(M=>gr(r,S,M,h,"content-sync-impact")),await Js(r,v,S,h,q,m,o.mode==="content-sync",a?.sitemapUrls??[]),a&&await Uc(r,S,m,q,a.sitemapUrls)),h.endMark("discovery",{pages:S.pageQueue.length,assets:S.assetQueue.length,sitemaps:S.sitemapQueue.length}),h.summary(`Queued ${S.pageQueue.length} pages, ${S.assetQueue.length} assets, ${S.sitemapQueue.length} sitemaps.`,{mode:o.mode,crawlMode:o.crawlMode,queuedPages:S.pageQueue.length,queuedAssets:S.assetQueue.length,queuedSitemaps:S.sitemapQueue.length}),o.mode==="retry-timeouts"&&S.pageQueue.length===0&&S.assetQueue.length===0&&S.sitemapQueue.length===0){h.summary("No timed-out URLs were queued. Skipping retry crawl.",{mode:o.mode,crawlMode:o.crawlMode,queuedPages:0,queuedAssets:0,queuedSitemaps:0}),await h.flush(),H&&await Yt(H,ue.artifact());return}h.mark("render-pages");let j=Array.from({length:P?R*A:R},()=>P?Cc(r,S,h,o.mode,v,m,q,ue,P,k??void 0):kc(r,S,h,o.mode,v,m,q,ue));if(await Promise.all(j),S.pageHttpFailures.size>0&&await h.flush(),$s(S.pageHttpFailures),H&&await Yt(H,ue.artifact()),h.endMark("render-pages",{pages:S.stats.pagesCompleted}),h.mark("download-assets"),h.progress("Asset download phase started.",{phase:"download-assets",doneAssets:S.doneAssets.size,assetsQueued:S.stats.assetsQueued,assetQueue:S.assetQueue.length}),k&&O?await fc(r,v,S,h,m,q,k,O,r.concurrency):(await bn(r,v,m,[...S.assetQueue],h),await mc(r,v,S,h,m,q)),h.endMark("download-assets",{assets:S.doneAssets.size}),m.enabled&&r.maxPages===0&&(await Fa(r,S,m,h,q),o.mode==="full"&&await Ua(r,m,h,q)),o.mode==="full"){let M=await dc(r,S,h);for(let X of M)if(le(q,r,X),k&&x){let re=fr(r,X),$=await D.readFile(X),N=$e(X),E=String(Os.lookup(re)||"")||(N?"text/plain; charset=utf-8":"application/octet-stream"),ne=await x.writeWorkspaceObject({key:`${k.workspace.prefix}jobs/${k.jobId}/copied/${Ee(re,40)}-${C.basename(re)}`,body:$,contentType:E});gt(k,{relativePath:re,sourceUrl:null,kind:"generated",contentType:ne.contentType,cacheControl:null,isText:N,object:ne}),N&&Ce(q,X);}}}finally{await se("dispose crawl request context",$t(r),()=>v.dispose()).catch(j=>{h.warn("Timed out while disposing crawl request context",{error:String(j)});});}}if(m.trackManifest&&await He(r,m,true),k)for(let v of q.deletedFiles)_s(k,v);let B=JSON.stringify(S.assetMap,null,2);if(await D.writeFile(C.join(r.outputDir,"asset-map.json"),B,"utf8"),k&&x){let v=await x.writeWorkspaceObject({key:`${k.workspace.prefix}jobs/${k.jobId}/generated/asset-map.json`,body:B,contentType:"application/json; charset=utf-8"});gt(k,{relativePath:"asset-map.json",sourceUrl:null,kind:"generated",contentType:"application/json; charset=utf-8",cacheControl:r.s3.assetCacheControl,isText:true,object:v});}JSON.stringify(g,null,2)!==JSON.stringify(S.assetMap,null,2)&&le(q,r,C.join(r.outputDir,"asset-map.json")),k&&x&&await hc(r,q,k,x);let T=o.mode==="single-url"||o.mode==="content-sync"?Va(r,S):m.enabled&&!o.resumeRewrite?La(r,m,q,g,S.assetMap):void 0;h.mark("rewrite-text");let W=o.mode==="single-url"||o.mode==="content-sync"?`Rewriting text files touched by URL crawl: 0/${T?.length??0}`:"Rewriting text files...";h.progress(W,{phase:"rewrite-text",index:0,totalFiles:T?.length,changedTextFiles:0}),await an(W);let U=Date.now(),z={files:T,previousAssetMap:g,onStart:({totalFiles:v,workerCount:j})=>{h.info(`Rewrite phase scanning ${v} text files.`,{phase:"rewrite-text",totalFiles:v,workerCount:j});},onProgress:async({index:v,totalFiles:j,changedTextFiles:M,file:X,changed:re})=>{re&&le(q,r,C.join(r.outputDir,X)),h.checkpoint(`Rewriting text file ${v}/${j}`,{phase:"rewrite-text",index:v,totalFiles:j,changedTextFiles:M,file:X}),h.debug(`${re?"Rewrote":"Checked unchanged"} text file ${v}/${j}: ${X}`,{phase:"rewrite-text",index:v,totalFiles:j,changedTextFiles:M,file:X,changed:re});let $=Date.now();if(!(v===1||v===j||v%50===0||$-U>=5e3))return;U=$;let E=`Rewriting text files: ${v}/${j}`;h.progress(E,{phase:"rewrite-text",index:v,totalFiles:j,changedTextFiles:M,file:X}),await an(E);}},J;if(k&&x){let v=T?.map(M=>fr(r,C.isAbsolute(M)?M:C.join(r.outputDir,M))),j=Object.values(k.objects).filter(M=>M.isText&&(!v||v.includes(M.relativePath))).length;await z.onStart?.({totalFiles:j,workerCount:Math.min(r.concurrency,j)}),J=await or(r,k,S.assetMap,{client:x,jobId:k.jobId,concurrency:r.concurrency,batchSize:25,maxAttempts:2,...v?{relativePaths:v}:{},onProgress:z.onProgress}),await Ns(r,k);}else I&&x?J=await sr(r,S.assetMap,{client:x,jobId:`crawl-${Ee(m.runId,40)}`,concurrency:r.remoteWorkers?.rewrite?.concurrency??r.rewriteConcurrency,batchSize:r.remoteWorkers?.rewrite?.batchSize??25,maxAttempts:r.remoteWorkers?.rewrite?.maxAttempts??2,onStageProgress:async({index:v,totalFiles:j,file:M})=>{let X=Date.now();if(v!==1&&v!==j&&v%25!==0&&X-U<5e3)return;U=X;let re=`Staging text files for Lambda rewrite: ${v}/${j}`;h.progress(re,{phase:"rewrite-stage",index:v,totalFiles:j,file:M,execution:"lambda"}),await an(re);},options:z}):J=await Kt(r,S.assetMap,z);m.trackManifest&&(qa(m,T),await He(r,m,true)),h.endMark("rewrite-text",{changedTextFiles:J});let de=a?[...q.deletedFiles].sort():m.enabled&&!o.resumeRewrite?await _a(r,S,m,q,h,k):[],Se=[...new Set((T||[]).map(v=>me(r.outputDir,C.isAbsolute(v)?v:C.join(r.outputDir,v))).filter(v=>v!==null))].sort();if(a){let v=[...q.changedFiles].sort(),j=new Set(v),M=new Set(a.directRenderUrls.map(L=>Et(r,L,"page")).filter(L=>!!L&&j.has(L))),X=new Set(a.archiveFamilyUrls.map(L=>Et(r,L,"page")).filter(L=>!!L&&j.has(L))),re=new Set(a.resourceRenderUrls.map(L=>Et(r,L,"asset")).filter(L=>!!L&&j.has(L))),$=new Set([...S.sitemapLeafUrls].map(L=>Et(r,L,"asset")).filter(L=>!!L&&j.has(L))),N=new Set([...S.sitemapIndexUrls].map(L=>Et(r,L,"asset")).filter(L=>!!L&&j.has(L))),E=new Set([...M,...X,...re,...$,...N]),ne=v.filter(L=>!E.has(L)),he=[...new Set([...a.directRenderUrls,...a.archiveFamilyUrls,...S.resolvedSitemapRoots,...S.sitemapLeafUrls,...S.sitemapIndexUrls,...a.deleteUrls,...a.resourceRenderUrls,...a.resourceDeleteUrls].map(L=>{let Pe=new URL(L);return `${Pe.pathname}${Pe.search}`}))].sort();await Kr(r,{schemaVersion:2,generatedAt:new Date().toISOString(),outputDir:C.resolve(r.outputDir),runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,changedFiles:v,deletedFiles:de,rewriteTargets:Se,revalidateFiles:[...q.revalidateFiles].sort(),consumerId:a.consumerId,fromSequence:a.fromSequence,toSequence:a.toSequence,impactHash:a.impactHash,resolvedSitemapUrls:[...S.resolvedSitemapRoots],uploadGroups:{assets:ne,content:[...M].sort(),listings:[...X].sort(),resources:[...re].sort(),sitemapLeaves:[...$].sort(),sitemapIndexes:[...N].reverse()},invalidationPaths:he});}else await Kr(r,{schemaVersion:1,generatedAt:new Date().toISOString(),outputDir:C.resolve(r.outputDir),runMode:o.mode,crawlMode:o.crawlMode,fullSyncRequired:!(m.enabled&&!o.resumeRewrite),changedFiles:m.enabled&&!o.resumeRewrite?[...q.changedFiles].sort():[],deletedFiles:m.enabled&&!o.resumeRewrite?de:[],rewriteTargets:m.enabled&&!o.resumeRewrite?Se:[],revalidateFiles:[...q.revalidateFiles].sort()});await os(r),h.summary(o.resumeRewrite?`Done. Resumed final rewrite over existing output and changed ${J} text files.`:`Done. Rendered ${S.stats.pagesRendered} pages, processed ${S.doneSitemaps.size} sitemaps and ${S.doneAssets.size} assets.`,{mode:o.mode,crawlMode:o.crawlMode,resumeRewrite:o.resumeRewrite,...S.stats,pagesRendered:S.stats.pagesRendered,donePages:S.stats.pagesCompleted,claimedPages:S.donePages.size,doneSitemaps:S.doneSitemaps.size,doneAssets:S.doneAssets.size,changedTextFiles:J}),await h.flush();}function Wc(e){let t=e/6e4;if(Number.isInteger(t)&&t>=1)return `${t} minute${t===1?"":"s"}`;let r=Math.ceil(e/1e3);return `${r} second${r===1?"":"s"}`}async function ye(e,t,r){let n=new AbortController,s=globalThis.setTimeout(()=>{n.abort(new Error(`${e} timed out after ${Wc(t)}.`));},t);try{return await r({abortController:n,httpOptions:{abortSignal:n.signal,requestTimeout:t}})}finally{globalThis.clearTimeout(s);}}function _c(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function br(e){let t="";for(let n=0;n<e.length;n++){let s=e[n];if(s==="--profile"){let o=(e[n+1]||"").trim();if(!o)throw new Error("Missing value for --profile.");t=o,n+=1;}else if(s.startsWith("--profile=")){let o=s.slice(10).trim();if(!o)throw new Error("Missing value for --profile.");t=o;}}let r=String(process.env.PUBLISHER_DEPLOY_PROFILE||process.env.PUBLISHER_DEPLOYMENT_PROFILE||"").trim();return t||r||null}function Nc(e,t){return {...e,...t??{}}}function Bc(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function bt(e,t){let r=String(t??e.deploymentTargetOverride??"").trim();if(!_c(e)){if(r)throw new Error(`Deployment target "${r}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:e}}let n=String(r||e.defaultDeploymentProfile||"").trim();if(!n)return {name:null,profile:null,config:e};let s=e.deploymentProfiles?.[n];if(!s)throw new Error(`Unknown deployment profile "${n}". Check the linked WPSuite publisher configuration.`);return {name:n,profile:s,config:{...e,targetOrigin:s.targetOrigin??e.targetOrigin,s3:Nc(e.s3,s.s3),cloudFront:Bc(e.cloudFront,s.cloudFront)}}}function Rn(e){return String(e??"").split(";").map(t=>t.trim().toLowerCase()).filter(Boolean).join(";")}var $o="wpsuite-sha256",Oo="wpsuite-normalized-sha256",kn="public, max-age=0, must-revalidate",Zc=/(?:^|[-_:.])(id|uid|nonce|token|hash|instance)(?:$|[-_:.])/i,xn=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,Yc=["SIGINT","SIGTERM"],Rr=new Set,Sr=new Map,Io=null;function Xc(e){Rr.add(e);let t=false;return ()=>{t||(t=true,Rr.delete(e));}}async function el(){let e=[...Rr];e.length!==0&&await Promise.allSettled(e.map(async t=>{Rr.delete(t),await D.rm(t,{recursive:true,force:true});}));}function tl(){if(!(Sr.size>0))for(let e of Yc){let t=()=>{Io||(Io=el().finally(()=>{for(let[r,n]of Sr)process.off(r,n);Sr.clear(),process.exit(1);}));};Sr.set(e,t),process.on(e,t);}}tl();function rl(e,t){let r=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(r)return C.resolve(r);let n=t.trim();return n?C.join(C.resolve(n),"crawl-manifest.json"):C.join(C.resolve(e),".crawl-manifest.json")}function Xe(e){return C.normalize(e).split(C.sep).join("/")}function To(e,t){let r=C.relative(C.resolve(e),C.resolve(t));return !r||r.startsWith("..")||C.isAbsolute(r)?null:Xe(r)}async function nl(e,t,r){let n=rl(e.outputDir,t),s;try{s=await D.readFile(n,"utf8");}catch(u){return u.code==="ENOENT"?(r.warn("Deploy rewrite guard disabled because the crawl manifest is missing.",{manifestPath:n,source:"deploy-guard"}),null):(r.warn("Deploy rewrite guard disabled because the crawl manifest could not be read.",{manifestPath:n,error:u instanceof Error?u.message:String(u),source:"deploy-guard"}),null)}let o;try{o=JSON.parse(s);}catch(u){return r.warn("Deploy rewrite guard disabled because the crawl manifest is invalid JSON.",{manifestPath:n,error:u instanceof Error?u.message:String(u),source:"deploy-guard"}),null}let a=new Map,c=new Map,i=0,d=0;for(let[u,l]of Object.entries(o.pages||{})){let g=String(l?.outputPath||"").trim(),p=g?To(e.outputDir,g):null;if(p){let f=typeof l?.contentType=="string"&&l.contentType?l.contentType:null;f&&c.set(p,f);}!g||l?.rewriteComplete===true||p&&(i++,a.has(p)||a.set(p,{kind:"page",sourceUrl:u,outputPath:C.resolve(g),relativePath:p}));}for(let[u,l]of Object.entries(o.assets||{})){let g=String(l?.outputPath||"").trim(),p=l?.isText===true,f=g?To(e.outputDir,g):null;if(f){let w=typeof l?.contentType=="string"&&l.contentType?l.contentType:null;w&&c.set(f,w);}!g||!p||l?.rewriteComplete===true||f&&(d++,a.has(f)||a.set(f,{kind:"asset",sourceUrl:u,outputPath:C.resolve(g),relativePath:f}));}return {manifestPath:n,byRelativePath:a,contentTypeByRelativePath:c,pendingPages:i,pendingTextAssets:d}}function sl(e){return e&&(xn.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)?"__hex__":/^[0-9]{8,}$/.test(e)?"__num__":/^(?=.*[a-z])(?=.*\d)[a-z0-9]{8,}$/i.test(e)?"__alnum__":e)}function vn(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,r)=>r%2===1?t:sl(t)).join("")}function ol(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,r)=>r%2===1?t:xn.test(t)?"__uuid__":/^[0-9a-f]{6,}$/i.test(t)?"__hex__":/^[0-9]{6,}$/.test(t)?"__num__":/^[a-z0-9_-]{6,}$/i.test(t)?"__id__":t).join("")}function vr(e,t){return Array.isArray(e)?e.map(r=>vr(r,t)):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([r,n])=>[r,vr(n,r)])):typeof e!="string"?e:t&&Zc.test(t)?ol(e):xn.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)||/^[0-9]{8,}$/.test(e)?vn(e):e}function il(e){return e.replace(/((?:https?:\/\/[^"'\s>]+|\/)(?:wp-content|wp-includes)\/[^"'\s>?]+)\?ver=[A-Za-z0-9._-]+(?=$|[#"'])/gi,"$1")}function al(e,t){let r=t.trim();if((r.startsWith("{")||r.startsWith("[")||r.startsWith('"'))&&e.startsWith("data-"))try{let s=JSON.parse(r);return JSON.stringify(vr(s))}catch{}if(e.startsWith("data-")&&/^[A-Za-z0-9+/=]+$/.test(r))try{let s=Buffer.from(r,"base64").toString("utf8"),o=JSON.parse(s),a=JSON.stringify(vr(o));return Buffer.from(a,"utf8").toString("base64")}catch{}return t}function cl(e,t){let r=e.toLowerCase();return r==="class"?t.split(/(\s+)/).map((n,s)=>s%2===1?n:vn(n)).join(""):r==="id"||r==="for"||r==="aria-controls"||r==="aria-labelledby"||r==="aria-describedby"||r==="aria-owns"||r==="aria-activedescendant"||r==="name"?vn(t):r==="href"||r==="src"?il(t):al(r,t)}function ll(e){return e.replace(/<([a-z][^\s/>]*)([^<>]*?)>/gi,(t,r,n)=>{if(r.startsWith("/"))return t;let s=n.replace(/\s([:@a-zA-Z_][-:.a-zA-Z0-9_]*?)=(['"])([\s\S]*?)\2/g,(o,a,c,i)=>{let d=cl(a,i);return ` ${a}=${c}${d}${c}`});return `<${r}${s}>`})}function St(e){let t=C.extname(e).toLowerCase();return [".html",".htm",".xml",".xsl",".txt"].includes(t)||["robots.txt","sitemap.xml"].includes(C.basename(e))}function Pr(e,t){return [e.replace(/^\/+|\/+$/g,""),t.replace(/^\/+/,"")].filter(Boolean).join("/")}async function ul(e,t,r,n){let s=new Map,o,a=0;do{a++;let c=await ye(`S3 list objects page ${a} for s3://${t}/${r||""}`,3e5,({httpOptions:i})=>e.send(new ListObjectsV2Command({Bucket:t,Prefix:r||void 0,ContinuationToken:o}),i));for(let i of c.Contents||[]){if(!i.Key)continue;let d=(i.ETag||"").replace(/^"|"$/g,"").toLowerCase(),u=Number(i.Size??0);s.set(i.Key,{etag:d,size:u,cacheControl:void 0,contentType:void 0});}o=c.NextContinuationToken,n.debug(`Listed S3 keys page ${a}: ${s.size} keys so far`);}while(o);return s}async function dl(e,t,r,n){if(n.has(r))return n.get(r)??null;try{let s=await ye(`S3 head object ${r}`,3e5,({httpOptions:a})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),a)),o={etag:(s.ETag||"").replace(/^"|"$/g,"").toLowerCase(),size:Number(s.ContentLength??0),cacheControl:s.CacheControl?.trim()||null,contentType:s.ContentType?.trim()||null};return n.set(r,o),o}catch{return n.set(r,null),null}}async function pl(e,t,r,n){if(n.cacheControl!==void 0&&n.contentType!==void 0)return {cacheControl:n.cacheControl,contentType:n.contentType};try{let s=await ye(`S3 head cache metadata ${r}`,3e5,({httpOptions:o})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),o));n.cacheControl=s.CacheControl?.trim()||null,n.contentType=s.ContentType?.trim()||null;}catch{n.cacheControl=null,n.contentType=null;}return {cacheControl:n.cacheControl,contentType:n.contentType}}function Ao(e){return String(e||"").split(",").map(t=>t.trim().toLowerCase()).filter(Boolean).sort().join(",")}async function ml(e,t,r,n){let s=[...new Set(Array.from(r).filter(Boolean))];if(s.length===0)return n.info("No explicitly tombstoned S3 objects to delete."),[];n.info(`Deleting ${s.length} explicitly tombstoned S3 object(s)\u2026`);for(let o=0;o<s.length;o+=1e3){let a=s.slice(o,o+1e3);if(a.length===0)continue;let c=await ye(`S3 delete objects batch ${Math.floor(o/1e3)+1}`,3e5,({httpOptions:i})=>e.send(new DeleteObjectsCommand({Bucket:t,Delete:{Objects:a.map(d=>({Key:d}))}}),i));if(c.Errors&&c.Errors.length>0){let i=c.Errors.map(d=>`${d.Key||"(unknown)"}: ${d.Code||"DeleteFailed"}`);throw new Error(`S3 reported ${i.length} object deletion error(s): ${i.join(", ")}`)}n.info(`Deleted batch of ${a.length} object(s).`);for(let i of a)n.debug(` deleted: ${i}`);}return s}function gl(e){return createHash("md5").update(e).digest("hex")}function et(e){return createHash("sha256").update(e).digest("hex")}function Cr(e){return createHash("sha256").update(ll(e.toString("utf8"))).digest("hex")}async function fl(e,t,r,n){if(n.has(r))return n.get(r)??null;try{let a=((await ye(`S3 head object metadata ${r}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),c))).Metadata?.[$o]||"").trim().toLowerCase()||null;return n.set(r,a),a}catch{return n.set(r,null),null}}async function hl(e,t,r,n,s){if(n.has(r))return n.get(r)??null;try{let a=((await ye(`S3 head object normalized metadata ${r}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),c))).Metadata?.[Oo]||"").trim().toLowerCase();if(a)return n.set(r,a),a}catch{return n.set(r,null),null}if(s.has(r)){let o=s.get(r)??null;return n.set(r,o),o}try{let a=await(await ye(`S3 get object ${r}`,3e5,({httpOptions:i})=>e.send(new GetObjectCommand({Bucket:t,Key:r}),i))).Body?.transformToByteArray(),c=a?Cr(Buffer.from(a)):null;return s.set(r,c),n.set(r,c),c}catch{return s.set(r,null),n.set(r,null),null}}function wl(e){return e.replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"")||"profile"}function yl(e,t){let r=e,n=Object.entries(t).sort((s,o)=>o[0].length-s[0].length);for(let[s,o]of n)s&&(r=r.split(s).join(o));return r}async function bl(e,t,r,n){let s=C.join(e,"asset-map.json"),o;try{o=await D.readFile(s,"utf8");}catch(i){if(i.code==="ENOENT")return;throw i}let a=JSON.parse(o);if(!a||typeof a!="object")return;let c=Object.fromEntries(Object.entries(a).map(([i,d])=>{let u=String(d??"");return t.urlRewriteMode==="absolute"&&t.targetOrigin&&r.targetOrigin&&t.targetOrigin!==r.targetOrigin&&(u=u.split(t.targetOrigin).join(r.targetOrigin)),u=yl(u,n),[i,u]}));await D.writeFile(s,JSON.stringify(c,null,2),"utf8");}async function Sl(e,t,r,n,s,o,a){let c=e.targetOrigin!==t.targetOrigin,i=Object.keys(n).length>0;if(!r||!c&&!i)return {outputDir:e.outputDir,cleanup:async()=>{}};if(c&&e.urlRewriteMode!=="absolute")throw new Error(`Deployment profile "${r}" changes targetOrigin, but the base crawl output must use urlRewriteMode "absolute" for multi-target redeploys.`);let d=C.join(C.dirname(e.outputDir),`.deploy-profile-${wl(r)}-${Date.now()}`),u=Xc(d);s.info(`Preparing deployment profile "${r}" from existing crawl output to ${d}...`);try{if(await D.rm(d,{recursive:!0,force:!0}),o){await D.mkdir(d,{recursive:!0});for(let w of o){let m=C.join(e.outputDir,w),h=C.join(d,w);await D.mkdir(C.dirname(h),{recursive:!0}),await D.copyFile(m,h);}}else await D.cp(e.outputDir,d,{recursive:!0});let l={...e,sourceOrigin:e.targetOrigin,targetOrigin:t.targetOrigin,extraReplacements:n,outputDir:d},g=1;s.info(`Rewriting text files for deployment profile "${r}"...`);let p={files:o,onStart:({totalFiles:w,workerCount:m})=>{g=m,s.info(`Rewriting ${w} text file(s) for deployment profile "${r}" with ${m} worker(s).`);},onProgress:({index:w,totalFiles:m,changedTextFiles:h,file:y,changed:b})=>{let O=`Rewriting [${w}/${m}] ${y} (${b?"rewrote":"checked"}; ${h} changed so far)`;m<=200||w<=5||w%10===0||w===m?(s.progress(O,{file:y,index:w,totalFiles:m,changed:b,changedTextFiles:h,workerCount:g}),s.info(O)):s.debug(O);}},f=a?await sr(l,{},{...a,options:p}):await Kt(l,{},p);return await bl(d,e,t,n),s.info(`Prepared deployment profile "${r}" in ${d} (${f} rewritten text file(s)).`),{outputDir:d,cleanup:async()=>{u(),await D.rm(d,{recursive:!0,force:!0});}}}catch(l){throw s.error(`Failed to prepare deployment profile "${r}" temporary output.`,{tempDir:d,outputDir:e.outputDir,error:l instanceof Error?l.message:String(l),...l instanceof Error&&l.stack?{stack:l.stack}:{}}),await D.rm(d,{recursive:true,force:true}).catch(()=>{}),u(),await s.flush().catch(()=>{}),l}}function Mo(e,t){let r=s=>s.replace(/^\/+|\/+$/g,""),n=e.targets.find(s=>s.bucketName===t.s3.bucket&&r(s.prefix)===r(t.s3.prefix)&&s.region===t.s3.region);if(!n)throw new Error(`No CDK Lambda deployment target matches s3://${t.s3.bucket}/${t.s3.prefix||""} in ${t.s3.region}. Redeploy the CDK with this target and reinstall remote-workers.json.`);return n}function Pl(e,t){if(!e||typeof e!="object")throw new Error("Remote deploy result must be a JSON object.");let r=e;if(r.schemaVersion!==ee||r.response?.operation!=="deploy-copy"||r.response.jobId!==t.jobId||r.response.taskId!==t.taskId||!r.detail||!Array.isArray(r.detail.objects)||!Array.isArray(r.detail.deletedKeys)||!Array.isArray(r.detail.deleteErrors))throw new Error("Remote deploy result has an invalid envelope.");return r}async function kr(e){let t=null;for(let r=1;r<=Math.max(1,Math.min(5,e.maxAttempts));r+=1){let n=te(`deploy-${e.batchIndex}-a${r}-${Date.now()}`,"taskId");try{let s=await e.client.invoke({schemaVersion:ee,operation:"deploy-copy",jobId:e.jobId,taskId:n,targetId:e.targetId,items:e.items,...e.deleteRelativeKeys?{deleteRelativeKeys:e.deleteRelativeKeys}:{}}),o=Pl(await e.client.readResult(s),s);if(o.response.status==="partial")throw new Error(`Worker reported ${o.response.failedItems} failed item(s).`);return o}catch(s){t=s;}}throw new Error(`Remote deploy task failed after ${e.maxAttempts} attempt(s): ${t instanceof Error?t.message:String(t)}`,{cause:t})}async function Rl(e){let t=Mo(e.remoteConfig,e.config),r=te(`deploy-${Date.now()}-${et(Buffer.from(`${t.id}:${e.config.outputDir}`)).slice(0,12)}`,"jobId"),n=Ge(e.remoteConfig.workspace.prefix,"jobs",r,"deploy-inputs");e.log.progress(`Staging export for Lambda deploy: 0/${e.files.length}`,{phase:"deploy-stage",index:0,totalFiles:e.files.length,uploaded:0,skipped:0,failed:0,mode:"lambda-s3-copy"});let s=0,o=[],a=Math.max(1,Math.min(16,e.concurrency*2)),c=0;await Promise.all(Array.from({length:Math.min(a,e.files.length)},async()=>{for(;;){let y=c;if(y>=e.files.length)return;c+=1;let b=e.files[y],P=await D.readFile(C.join(e.config.outputDir,b)),O=e.contentTypeByRelativePath?.get(b)||Os.lookup(b)||"application/octet-stream",x=e.revalidateFiles.has(Xe(b))?kn:St(b)?e.config.s3.htmlCacheControl:e.config.s3.assetCacheControl,k=et(P),R=St(b)?Cr(P):void 0,A=`${n}/files/${et(Buffer.from(b))}-${C.basename(b)}`.replace(/\/+/g,"/");await e.client.writeWorkspaceObject({key:A,body:P,contentType:String(O),cacheControl:x,metadata:{"wpsuite-sha256":k,...R?{"wpsuite-normalized-sha256":R}:{}}}),o[y]={sourceKey:A,targetRelativeKey:b,sha256:k,...R?{normalizedSha256:R}:{},bytes:P.byteLength,contentType:String(O),cacheControl:x},s+=1,(s===e.files.length||s<=5||s%25===0)&&e.log.progress(`Staging export for Lambda deploy: ${s}/${e.files.length}`,{phase:"deploy-stage",index:s,totalFiles:e.files.length,staged:s,mode:"lambda-s3-copy"});}}));let i=[],d=Math.max(1,Math.min(500,e.batchSize));for(let y=0;y<o.length;y+=d)i.push(o.slice(y,y+d));let u=[],l=[],g=[],p=0,f=0,w=0,m=Math.max(1,Math.min(Math.max(1,e.concurrency),i.length));if(e.log.progress(`Lambda deploy: 0/${e.files.length}`,{phase:"deploy-copy",index:0,totalFiles:e.files.length,uploaded:0,skipped:0,failed:0,mode:"lambda-s3-copy",workerCount:m,completedBatches:0,totalBatches:i.length}),await Promise.all(Array.from({length:m},async()=>{for(;;){let y=w;if(y>=i.length)return;w+=1;let b=Date.now(),P=await kr({client:e.client,jobId:r,targetId:t.id,batchIndex:y+1,items:i[y],maxAttempts:e.maxAttempts}),O=0,x=0,k=0;for(let A of P.detail.objects)p+=1,A.status==="copied"?(u.push(A.targetKey),O+=1):A.status==="skipped"?(l.push(A.targetKey),x+=1):(g.push(A.targetKey),k+=1),e.log.debug(`Lambda deploy ${A.status}: ${A.targetKey}`,{phase:"deploy-copy",batch:y+1,sourceKey:A.sourceKey,targetKey:A.targetKey,status:A.status,...A.error?{error:A.error}:{}});f+=1;let R=Date.now()-b;e.log.info(`Lambda deploy batch ${y+1}/${i.length} completed: ${O} copied, ${x} skipped unchanged, ${k} failed.`,{phase:"deploy-copy",batch:y+1,totalBatches:i.length,items:P.detail.objects.length,copied:O,skipped:x,failed:k,elapsedMs:R,elapsedSeconds:Math.round(R/10)/100,workerExporterVersion:P.response.exporterVersion,...P.requestId?{lambdaRequestId:P.requestId}:{},...P.completedAt?{workerCompletedAt:P.completedAt}:{}}),e.log.progress(`Lambda deploy: ${p}/${e.files.length}`,{phase:"deploy-copy",index:p,totalFiles:e.files.length,uploaded:u.length,skipped:l.length,failed:g.length,mode:"lambda-s3-copy",workerCount:m,completedBatches:f,totalBatches:i.length});}})),g.length>0)throw new Error(`Remote deployment failed for ${g.length} object(s).`);let h=[];for(let y=0;y<e.deletedFiles.length;y+=1e3){let b=e.deletedFiles.slice(y,y+1e3);if(b.length===0)continue;let P=await kr({client:e.client,jobId:r,targetId:t.id,batchIndex:i.length+Math.floor(y/1e3)+1,items:[],deleteRelativeKeys:b,maxAttempts:e.maxAttempts});h.push(...P.detail.deletedKeys),e.log.progress(`Lambda deploy deletions: ${h.length}/${e.deletedFiles.length}`,{phase:"deploy-delete",index:h.length,totalFiles:e.deletedFiles.length,deleted:h.length,mode:"lambda-s3-copy"});}return {uploadedKeys:u,skippedKeys:l,failedKeys:g,deletedKeys:h}}async function vl(e){let t=Mo(e.remoteConfig,e.config),r=te(`deploy-manifest-${Date.now()}-${et(Buffer.from(t.id)).slice(0,12)}`,"jobId"),n=e.objects.map(p=>({sourceKey:p.object.key,targetRelativeKey:p.relativePath,sha256:p.object.sha256,bytes:p.object.bytes,contentType:p.contentType||"application/octet-stream",cacheControl:e.revalidateFiles.has(Xe(p.relativePath))?kn:St(p.relativePath)?e.config.s3.htmlCacheControl:e.config.s3.assetCacheControl})),s=[],o=Math.max(1,Math.min(500,e.batchSize));for(let p=0;p<n.length;p+=o)s.push(n.slice(p,p+o));let a=[],c=[],i=[],d=0,u=0,l=Math.max(1,Math.min(Math.max(1,e.concurrency),Math.max(1,s.length)));if(e.log.progress(`Lambda deploy: 0/${n.length}`,{phase:"deploy-copy",index:0,totalFiles:n.length,mode:"lambda-s3-native",workerCount:l}),await Promise.all(Array.from({length:s.length===0?0:l},async()=>{for(;;){let p=d;if(p>=s.length)return;d+=1;let f=await kr({client:e.client,jobId:r,targetId:t.id,batchIndex:p+1,items:s[p],maxAttempts:e.maxAttempts});for(let w of f.detail.objects)u+=1,w.status==="copied"?a.push(w.targetKey):w.status==="skipped"?c.push(w.targetKey):i.push(w.targetKey);e.log.progress(`Lambda deploy: ${u}/${n.length}`,{phase:"deploy-copy",index:u,totalFiles:n.length,uploaded:a.length,skipped:c.length,failed:i.length,mode:"lambda-s3-native",workerCount:l});}})),i.length>0)throw new Error(`Remote deployment failed for ${i.length} object(s).`);let g=[];for(let p=0;p<e.deletedFiles.length;p+=1e3){let f=e.deletedFiles.slice(p,p+1e3);if(f.length===0)continue;let w=await kr({client:e.client,jobId:r,targetId:t.id,batchIndex:s.length+Math.floor(p/1e3)+1,items:[],deleteRelativeKeys:f,maxAttempts:e.maxAttempts});g.push(...w.detail.deletedKeys);}return {uploadedKeys:a,skippedKeys:c,failedKeys:i,deletedKeys:g}}async function Cl(e,t,r,n,s){let o={...t,jobId:te(`profile-${et(Buffer.from(n)).slice(0,16)}-${Date.now()}`,"jobId"),objects:{},assetMap:{...t.assetMap},generatedAt:new Date().toISOString()},a=0,c=Math.max(1,Math.min(Math.max(1,s),Math.max(1,r.length)));return await Promise.all(Array.from({length:r.length===0?0:c},async()=>{for(;;){let i=a;if(i>=r.length)return;a+=1;let d=r[i],u=Ge(e.config.workspace.prefix,"jobs",o.jobId,"profile",et(Buffer.from(d.relativePath)),C.basename(d.relativePath));o.objects[d.relativePath]={...d,object:await e.copyWorkspaceObject(d.object,u)};}})),o}async function jo(e=process.argv.slice(2)){let t=await Ie(),r=br(e),n=bt(t,r),s=C.resolve(n.config.logDir||"logs"),o=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",a=o?C.join(C.resolve(o),"deploy-diff.json"):"",c=new dt(s,"deploy.log.jsonl",n.config.logLevel??"info"),i=n.profile?.extraReplacements??{},d=async()=>{};try{let u=await lt(t);if(e.includes("--content-sync")&&u?.schemaVersion!==2)throw new Error("Content-sync deployment requires a valid schema-2 targeted deploy plan.");let g=await ss(t);if(g)throw new Error(`Deployment blocked: the crawl started at ${g.startedAt} did not complete its final rewrite. Run a successful crawl or resume the rewrite before deploying.`);let p=t.remoteWorkers?.rewrite?.enabled===!0,f=t.remoteWorkers?.deploy?.enabled===!0,w=null,m=null;if(p||f){let $=ir({runtimeDir:o});if(!$)throw new Error("Remote Lambda workers are enabled, but the runtime directory is unknown.");w=await Xt($);let N=!1;m=new mt(w,{},{onProgress:E=>{c.progress(`Lambda ${E.operation} batch: ${E.completedItems+E.failedItems}/${E.totalItems}`,{remoteWorkerOperation:E.operation,remoteWorkerTaskId:E.taskId,remoteWorkerStatus:E.status,remoteWorkerCompletedItems:E.completedItems,remoteWorkerFailedItems:E.failedItems,remoteWorkerTotalItems:E.totalItems,remoteWorkerUpdatedAt:E.updatedAt});},onProgressError:E=>{N||(N=!0,c.warn("Lambda progress status could not be read; task execution continues.",{error:E.message}));}});}let h=f&&p&&t.remoteWorkers?.render?.enabled===!0&&m&&w?.functions.asset?await lr(t):null;if(h&&m&&w){if(h.workspace.bucket!==w.workspace.bucket||h.workspace.prefix!==w.workspace.prefix)throw new Error("Remote export manifest workspace does not match remote-workers.json.");let $=n.config,N=$.s3SyncMode??"sdk-upload-delete";if(u?.schemaVersion===2&&u.deletedFiles.length>0&&N!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let E=u?.schemaVersion===2?[...u.uploadGroups.assets,...u.uploadGroups.content,...u.uploadGroups.listings,...u.uploadGroups.resources,...u.uploadGroups.sitemapLeaves,...u.uploadGroups.sitemapIndexes]:u?.changedFiles??[];if(u?.schemaVersion===2){let ce=[...new Set(E)],Ue=[...new Set(u.changedFiles)];if(ce.length!==Ue.length||Ue.some(it=>!ce.includes(it)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let he=!!u&&!u.fullSyncRequired?[...new Set(E.map(Xe))]:Object.keys(h.objects).sort(),L=he.filter(ce=>!h.objects[ce]);if(L.length>0)throw new Error(`Remote export manifest is missing ${L.length} planned object(s): ${L.slice(0,10).join(", ")}`);let Pe=h,V=he.map(ce=>h.objects[ce]);n.name&&(t.targetOrigin!==$.targetOrigin||Object.keys(i).length>0)&&(Pe=await Cl(m,h,V,n.name||"default",t.concurrency),await or($,Pe,Pe.assetMap,{client:m,jobId:Pe.jobId,concurrency:t.concurrency,batchSize:25,maxAttempts:2}),V=he.map(ce=>Pe.objects[ce])),n.name&&c.info(`Using deployment profile: ${n.name}`),c.info(`Starting S3-native Lambda deploy - s3://${$.s3.bucket}/${$.s3.prefix||""} (${$.s3.region}).`),c.mark("deploy");let ie=await vl({client:m,remoteConfig:w,config:$,objects:V,deletedFiles:N==="sdk-upload-delete"?u?.deletedFiles??[]:[],revalidateFiles:new Set((u?.revalidateFiles??[]).map(Xe)),concurrency:t.concurrency,batchSize:50,maxAttempts:2,log:c});a&&(await D.mkdir(C.dirname(a),{recursive:!0}),await D.writeFile(a,JSON.stringify({generatedAt:new Date().toISOString(),mode:"lambda-s3-native",summary:{totalFiles:V.length,eligibleFiles:V.length,uploaded:ie.uploadedKeys.length,skipped:ie.skippedKeys.length,rejected:0,failed:ie.failedKeys.length,deleted:ie.deletedKeys.length,protected:0,...n.name?{profile:n.name}:{}},uploadedKeys:ie.uploadedKeys,skippedKeys:ie.skippedKeys,rejectedKeys:[],failedKeys:ie.failedKeys,deletedKeys:ie.deletedKeys,protectedRemoteOnlyKeys:[]},null,2),"utf8")),u?.schemaVersion!==2&&await Qe(t),c.endMark("deploy"),await c.flush();return}let y=await Sl(t,n.config,n.name,i,c,u?.schemaVersion===2?u.changedFiles:void 0,p&&m?{client:m,jobId:`profile-${Date.now()}`,concurrency:t.remoteWorkers?.rewrite?.concurrency??t.rewriteConcurrency,batchSize:t.remoteWorkers?.rewrite?.batchSize??25,maxAttempts:t.remoteWorkers?.rewrite?.maxAttempts??2,onStageProgress:({index:$,totalFiles:N,file:E})=>{($===1||$===N||$%25===0)&&c.progress(`Staging deployment-profile rewrite: ${$}/${N}`,{phase:"rewrite-stage",index:$,totalFiles:N,file:E,execution:"lambda"});}}:void 0);d=y.cleanup;let b={...n.config,outputDir:y.outputDir},P=b.s3SyncMode??"sdk-upload-delete";if(u?.schemaVersion===2&&u.deletedFiles.length>0&&P!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let O=u?.schemaVersion!==2&&y.outputDir!==t.outputDir&&(t.targetOrigin!==n.config.targetOrigin||Object.keys(i).length>0),x=!!u&&!u.fullSyncRequired&&!O;if(u?.schemaVersion===2&&!x)throw new Error("Content-sync deployment cannot widen into a full-output deployment.");n.name&&c.info(`Using deployment profile: ${n.name}`),c.info(`Starting deploy \u2014 s3://${b.s3.bucket}/${b.s3.prefix||""} (region: ${b.s3.region})`),c.info(`S3 sync mode: ${P}`),u&&u.fullSyncRequired?c.info("Deploy plan requests a full sync; scanning the whole output tree."):u&&O?c.info("Ignoring incremental deploy plan because deployment-profile rewrites require a full sync."):x&&c.info(`Using incremental deploy plan: ${u.changedFiles.length} changed file(s), ${u.deletedFiles.length} deleted file(s).`),c.mark("deploy");let k=new S3Client({region:b.s3.region}),R=y.outputDir===t.outputDir?await nl(t,o,c):null,A=u?.schemaVersion===2?[...u.uploadGroups.assets,...u.uploadGroups.content,...u.uploadGroups.listings,...u.uploadGroups.resources,...u.uploadGroups.sitemapLeaves,...u.uploadGroups.sitemapIndexes]:u?.changedFiles??[];if(u?.schemaVersion===2){let $=[...new Set(A)],N=[...new Set(u.changedFiles)];if($.length!==N.length||N.some(E=>!$.includes(E)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let I=x?(await Promise.all(A.map(async $=>{try{return (await D.stat(C.join(b.outputDir,$))).isFile()?$:null}catch{if(u?.schemaVersion===2)throw new Error(`Content-sync planned upload is missing: ${$}`);return null}}))).filter($=>$!==null):await Ci(["**/*"],{cwd:b.outputDir,onlyFiles:!0,dot:!0}),K=[],F=[],S=I.filter($=>{let N=R?.byRelativePath.get(Xe($));if(!N)return !0;let E=Pr(b.s3.prefix,$);return K.push($),F.push(E),c.reject(N.kind,N.sourceUrl||$,"rewrite not completed; deploy upload blocked","deploy-guard",{file:$,key:E,outputPath:N.outputPath,manifestPath:R?.manifestPath}),!1}),q=new Set((u?.revalidateFiles||[]).map(Xe)),ue=new Set((x?S:I).map($=>Pr(b.s3.prefix,$)));R&&(R.pendingPages>0||R.pendingTextAssets>0)&&c.info(`Deploy rewrite guard loaded ${R.pendingPages} pending page(s) and ${R.pendingTextAssets} pending text asset(s) from crawl manifest.`,{manifestPath:R.manifestPath,pendingPages:R.pendingPages,pendingTextAssets:R.pendingTextAssets,source:"deploy-guard"}),K.length>0&&c.warn(`Deploy rewrite guard blocked ${K.length} file(s) from upload because final crawl rewrite is incomplete.`,{rejected:K.length,manifestPath:R?.manifestPath,source:"deploy-guard"}),x?c.info(`Using ${S.length} eligible planned changed file(s) from deploy plan for upload${K.length>0?` (${K.length} blocked by deploy rewrite guard)`:""}.`):c.info(`Found ${I.length} file(s) in output directory "${b.outputDir}"${K.length>0?`; ${S.length} remain eligible for upload after deploy rewrite guard filtering`:""}.`),c.progress("Deploy preflight: comparing local output with remote S3 state.",{phase:"deploy",mode:P,totalFiles:S.length,scannedFiles:I.length,rejected:K.length,deploymentProfile:n.name||"",s3Bucket:b.s3.bucket,s3Prefix:b.s3.prefix||"",targetedPlan:x,deployGuardEnabled:y.outputDir===t.outputDir});let H=0,B=0,T=0,W=[],U=[],z=[],J=[],de=[],Se=new Map,v=new Map,j=new Map,M=new Map,X=new Map;if(f){if(!m||!w)throw new Error("Remote deploy worker client was not initialized.");if(P!=="sdk-upload-delete"&&P!=="sdk-upload-only")throw new Error(`Unsupported remote deploy mode: ${P}`);let $=await Rl({client:m,remoteConfig:w,config:b,files:S,deletedFiles:P==="sdk-upload-delete"?u?.deletedFiles??[]:[],revalidateFiles:q,contentTypeByRelativePath:R?.contentTypeByRelativePath,concurrency:b.remoteWorkers?.deploy?.concurrency??b.assetDownloadConcurrency,batchSize:b.remoteWorkers?.deploy?.batchSize??50,maxAttempts:b.remoteWorkers?.deploy?.maxAttempts??2,log:c});W.push(...$.uploadedKeys),U.push(...$.skippedKeys),z.push(...$.failedKeys),J=$.deletedKeys,H=W.length,B=U.length,T=z.length,c.info(`Lambda deploy complete: ${H} copied, ${B} skipped unchanged, ${J.length} deleted, ${T} failed.`),a&&(await D.mkdir(C.dirname(a),{recursive:!0}),await D.writeFile(a,JSON.stringify({generatedAt:new Date().toISOString(),mode:"lambda-s3-copy",summary:{totalFiles:I.length,eligibleFiles:S.length,uploaded:H,skipped:B,rejected:F.length,failed:T,deleted:J.length,protected:0,...n.name?{profile:n.name}:{}},uploadedKeys:W,skippedKeys:U,rejectedKeys:F,failedKeys:z,deletedKeys:J,protectedRemoteOnlyKeys:[]},null,2),"utf8")),u?.schemaVersion!==2&&await Qe(t),c.endMark("deploy"),await c.flush();return}x||(c.info("Listing existing S3 objects to detect unchanged files..."),Se=await ul(k,b.s3.bucket,b.s3.prefix,c),c.info(`Found ${Se.size} existing key(s) in S3.`));let re=async $=>Se.has($)?Se.get($)??null:x?dl(k,b.s3.bucket,$,v):null;for(let $ of S){let N=C.join(b.outputDir,$),E=Pr(b.s3.prefix,$),ne=R?.contentTypeByRelativePath.get($),he=ne&&ne!=="application/octet-stream"?ne:Os.lookup($)||ne||"application/octet-stream",L=q.has(Xe($)),Pe=L?kn:St($)?b.s3.htmlCacheControl:b.s3.assetCacheControl,V=H+B+T+1;S.length<=200||V<=5||V%10===0||V===S.length?(c.progress(`Uploading [${V}/${S.length}] ${E} (${he})`,{file:$,key:E,index:V,totalFiles:S.length,contentType:he,mode:P}),c.info(`Uploading [${V}/${S.length}] ${E} (${he})`)):c.debug(`Uploading [${V}/${S.length}] ${E} (${he})`);try{let Re=await D.readFile(N),ie=await re(E),ce=null,Ue=null;if(ie){let it=await pl(k,b.s3.bucket,E,ie),No=!L||Ao(it.cacheControl)===Ao(Pe),Bo=Rn(it.contentType)===Rn(String(he)),Ar=No&&Bo,Ho=Re.byteLength;if(ie.size===Ho){let Lt=ie.etag.includes("-");if(ie.etag!==""&&!Lt){let Vo=gl(Re);if(ie.etag===Vo&&Ar){B++,S.length<=200||B<=5||B%25===0?(c.progress(`Skipped unchanged [${V}/${S.length}] ${E}`,{file:$,key:E,index:V,totalFiles:S.length,uploaded:H,skipped:B,failed:T,mode:P,reason:"etag-md5-match"}),c.info(`Skipped unchanged [${V}/${S.length}] ${E}`)):c.debug(`Skipped unchanged [${V}/${S.length}] ${E}`),U.push(E);continue}}ce=et(Re);let On=await fl(k,b.s3.bucket,E,j);if(On&&On===ce&&Ar){B++,S.length<=200||B<=5||B%25===0?(c.progress(`Skipped unchanged [${V}/${S.length}] ${E}`,{file:$,key:E,index:V,totalFiles:S.length,uploaded:H,skipped:B,failed:T,mode:P,reason:"metadata-sha256-match"}),c.info(`Skipped unchanged [${V}/${S.length}] ${E}`)):c.debug(`Skipped unchanged [${V}/${S.length}] ${E}`),U.push(E);continue}}if(St($)){Ue=Cr(Re);let Lt=await hl(k,b.s3.bucket,E,M,X);if(Lt&&Lt===Ue&&Ar){B++,S.length<=200||B<=5||B%25===0?(c.progress(`Skipped semantically unchanged [${V}/${S.length}] ${E}`,{file:$,key:E,index:V,totalFiles:S.length,uploaded:H,skipped:B,failed:T,mode:P,reason:"normalized-html-match"}),c.info(`Skipped semantically unchanged [${V}/${S.length}] ${E}`)):c.debug(`Skipped semantically unchanged [${V}/${S.length}] ${E}`),U.push(E);continue}}}ce=ce??et(Re),Ue=Ue??(St($)?Cr(Re):null),await ye(`S3 upload ${E}`,9e5,({abortController:it})=>new Upload({client:k,abortController:it,params:{Bucket:b.s3.bucket,Key:E,Body:Re,ContentType:he,CacheControl:Pe,Metadata:{[$o]:ce,...Ue?{[Oo]:Ue}:{}}}}).done()),H++,W.push(E),(H%25===0||H===S.length)&&(c.progress(`Uploaded ${H}/${S.length} file(s)\u2026`,{uploaded:H,skipped:B,failed:T,totalFiles:S.length,mode:P}),c.info(`Uploaded ${H}/${S.length} file(s)\u2026`));}catch(Re){T++,z.push(E),c.error(`Failed to upload ${E}: ${Re.message}`,{key:E,error:String(Re)});}}if(c.info(`Upload complete: ${H} uploaded, ${B} skipped unchanged, ${F.length} rejected by guard, ${T} failed.`),z.length>0||F.length>0)throw new Error(`Deployment stopped before deletion because ${z.length} upload(s) failed and ${F.length} upload(s) were rejected.`);if(P==="sdk-upload-delete"){let $=new Set((u?.deletedFiles??[]).map(E=>Pr(b.s3.prefix,E))),N=[...$].filter(E=>!ue.has(E));if(!x&&(de=[...Se.keys()].filter(ne=>!ue.has(ne)).filter(ne=>!$.has(ne)),de.length>0)){c.warn(`Protected ${de.length} remote-only S3 object(s) from deletion because the crawl did not provide explicit tombstones.`,{protected:de.length,source:"deploy-delete-guard"});for(let ne of de)c.debug(` protected remote-only object: ${ne}`);}J=await ml(k,b.s3.bucket,x?N:N.filter(E=>Se.has(E)),c);}else c.info("Sync mode is sdk-upload-only \u2014 skipping stale object deletion.");a&&(await D.mkdir(C.dirname(a),{recursive:!0}),await D.writeFile(a,JSON.stringify({generatedAt:new Date().toISOString(),mode:P,summary:{totalFiles:I.length,eligibleFiles:S.length,uploaded:W.length,skipped:U.length,rejected:F.length,failed:z.length,deleted:J.length,protected:de.length,...n.name?{profile:n.name}:{}},uploadedKeys:W,skippedKeys:U,rejectedKeys:F,failedKeys:z,deletedKeys:J,protectedRemoteOnlyKeys:de},null,2),"utf8")),u?.schemaVersion!==2&&await Qe(t),c.endMark("deploy"),await c.flush();}finally{await d().catch(()=>{});}}function xl(e){let t=e;return t.endsWith("/")?t+="index.html":C.posix.basename(t).includes(".")||(t+="/index.html"),t}function Il(e,t){let r=t.trim().replace(/^\/+|\/+$/g,"");if(!r)return null;let n=`/${r}`;return e===n||e.startsWith(`${n}/`)?e:`${n}${e.startsWith("/")?e:`/${e}`}`}function Do(e,t){let r=new Set;for(let n of e){let s=new URL(n,"https://content-sync.invalid");if(s.origin!=="https://content-sync.invalid")continue;let o=`${s.pathname}${s.search}`,a=xl(s.pathname);r.add(o),r.add(`${a}${s.search}`);let c=Il(a,t);c&&r.add(`${c}${s.search}`);}return [...r].sort()}function $l(){let e=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();return e?C.join(C.resolve(e),"content-sync-invalidation.json"):""}async function In(e,t){if(!e)return;await D.mkdir(C.dirname(e),{recursive:true});let r=`${e}.${process.pid}.${Date.now()}.tmp`;await D.writeFile(r,JSON.stringify({...t,updatedAt:new Date().toISOString()},null,2),"utf8"),await D.rename(r,e);}async function Ol(e,t,r,n){if(!e)return null;try{let s=JSON.parse(await D.readFile(e,"utf8"));if(s.schemaVersion!==1||s.distributionId!==t||s.impactHash!==r||!Array.isArray(s.batches))throw new Error("Content-sync invalidation state does not match the deploy plan.");let o=s.batches.flatMap(a=>a.paths);return o.length!==n.length||o.some((a,c)=>a!==n[c])?null:s}catch(s){if(s.code==="ENOENT")return null;throw s}}function Ml(e,t=3e3){let r=[];for(let n=0;n<e.length;n+=t)r.push(e.slice(n,n+t));return r}async function Uo(e=process.argv.slice(2)){let t=await Ie(),r=br(e),n=bt(t,r),s=n.config,o=new dt(s.logDir||"logs","invalidate.log.jsonl",s.logLevel??"info"),a=e.includes("--content-sync"),c=await lt(t);if(a&&c?.schemaVersion!==2)throw new Error("Content-sync invalidation requires a valid schema-2 targeted deploy plan.");if(n.name&&o.info(`Using deployment profile: ${n.name}`),!s.cloudFront.distributionId){o.info("No CloudFront distributionId configured; skipping invalidation."),await o.flush();return}let i=c?.schemaVersion===2?c.invalidationPaths:s.cloudFront.invalidationPaths.length>0?s.cloudFront.invalidationPaths:["/*"],d=c?.schemaVersion===2?Do(i,s.s3.prefix||""):i;if(c?.schemaVersion===2&&d.some(f=>f==="/*"))throw new Error("Content-sync cannot use a whole-distribution invalidation.");if(d.length===0){o.info("The targeted deploy plan has no CloudFront paths to invalidate."),await o.flush();return}let u=s.cloudFront.distributionId,l=new CloudFrontClient({region:"us-east-1",maxAttempts:5});if(o.mark("invalidate"),c?.schemaVersion!==2){let f=`smartcloud-static-publisher-${Date.now()}`;await ye(`CloudFront invalidation for distribution ${u}`,3e5,({httpOptions:w})=>l.send(new CreateInvalidationCommand({DistributionId:u,InvalidationBatch:{CallerReference:f,Paths:{Quantity:d.length,Items:d}}}),w)),o.endMark("invalidate"),await o.flush();return}let g=$l(),p=await Ol(g,u,c.impactHash,d);if(!p){let f=createHash("sha256").update(JSON.stringify(d)).digest("hex");p={schemaVersion:1,distributionId:u,impactHash:c.impactHash,updatedAt:new Date().toISOString(),batches:Ml(d).map((w,m)=>({index:m,callerReference:`wpsuite-content-sync-${c.impactHash.slice(0,24)}-${f.slice(0,24)}-${m}`,paths:w,invalidationId:null,status:"pending"}))},await In(g,p);}for(let f of p.batches){if(!f.invalidationId){let w=await ye(`CloudFront targeted invalidation batch ${f.index+1}`,3e5,({httpOptions:h})=>l.send(new CreateInvalidationCommand({DistributionId:u,InvalidationBatch:{CallerReference:f.callerReference,Paths:{Quantity:f.paths.length,Items:f.paths}}}),h)),m=String(w.Invalidation?.Id||"").trim();if(!m)throw new Error("CloudFront did not return an invalidation ID.");f.invalidationId=m,f.status="created",await In(g,p);}await waitUntilInvalidationCompleted({client:l,maxWaitTime:900,minDelay:2,maxDelay:15},{DistributionId:u,Id:f.invalidationId}),f.status="completed",await In(g,p),o.info(`CloudFront invalidation batch ${f.index+1}/${p.batches.length} completed.`,{invalidationId:f.invalidationId,pathCount:f.paths.length});}o.endMark("invalidate"),await o.flush();}function Tr(e){let t=new URL(e);return t.hash="",t.href}function Lo(e,t,r){let n=new Set([...t].map(i=>Tr(i))),s=new Map,o=new Map,a=i=>{s.set(Tr(i),i);},c=i=>{if(!i)return;let d=Tr(i);s.has(d)||o.set(d,i);};for(let i of e.deleteUrls)a(i);for(let i of e.resourceDeleteUrls)a(i);for(let i of e.archiveFamilies)if(i.foundPosts===0)for(let d of i.pageUrls)n.has(Tr(d))||a(d);for(let i of e.directRenderUrls)c(i);for(let i of e.resourceRenderUrls)c(i);for(let i of e.archiveFamilies)c(i.pageUrls[0]),c(i.pageUrls.at(-1));for(let i of r)c(i);return {expectedAvailable:[...o.values()],expectedMissing:[...s.values()]}}var Wo=["claimed","impact-planned","rendered","deployed","invalidated","verified","acknowledged"];function En(){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 C.resolve(e)}function Pt(e,t){return Wo.indexOf(e)>=Wo.indexOf(t)}async function Ke(e,t,r,n,s={}){let o={timestamp:new Date().toISOString(),eventType:t,status:t.endsWith("failed")?"failed":"success",actorSource:"content-sync",jobId:r.id||"",command:"content-sync",message:n,details:s};await D.mkdir(e,{recursive:true}),await D.appendFile(C.join(e,"audit-events.jsonl"),`${JSON.stringify(o)}
19
- `,"utf8");}function Dl(e){return Rt({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 Fl(e,t){let r=(e.scheduler?.rules??[]).filter(s=>s.command==="content-sync"&&s.id===t);if(r.length===0)throw new Error(`Content-sync scheduler rule not found: ${t}`);if(r.length>1)throw new Error(`Content-sync scheduler rule ID is not unique: ${t}`);let n=r[0];if(n.enabled===false)throw new Error(`Content-sync scheduler rule is disabled: ${t}`);return n}async function $n(e,t){if(e.subscriptionType!=="PROFESSIONAL"&&e.subscriptionType!=="AGENCY")throw new Error("Content-sync requires an active Professional or Agency subscription.");let r=Dl(t),n=bt(e,t.deploymentProfile),s=n.name||"",o=Dn(e.sourceOrigin,String(t.id||""),r),a=Fn({sourceOrigin:e.sourceOrigin,targetOrigin:n.config.targetOrigin,deploymentProfile:s,s3Bucket:n.config.s3.bucket,s3Prefix:n.config.s3.prefix,cloudFrontDistributionId:n.config.cloudFront.distributionId,urlRewriteMode:n.config.urlRewriteMode,extraReplacements:n.profile?.extraReplacements??{}}),c=Un({siteId:e.wpsuite?.siteSettings?.siteId,accountId:e.wpsuite?.siteSettings?.accountId,sourceOrigin:e.sourceOrigin,ruleId:String(t.id||""),targetFingerprint:a}),i=`content-sync:${String(t.id||"")}:${Le({scopeFingerprint:o,targetFingerprint:a}).slice(0,32)}`,d=new Wt({sourceOrigin:e.sourceOrigin,runtimeToken:String(e.wpsuite?.runtimeToken||""),ignoreHttpsErrors:e.ignoreHttpsErrors}),u=await d.releaseFingerprint(r.includeSubsites),l=jn({wordpressFingerprint:u,targetFingerprint:a,sourceOrigin:e.sourceOrigin,sitemapPaths:kt(e.sitemapPaths),blockedPathPrefixes:e.blockedPathPrefixes,assetPathPrefixes:e.assetPathPrefixes,noJavaScriptRenderPathPrefixes:e.noJavaScriptRenderPathPrefixes,urlRewriteMode:e.urlRewriteMode});return {rule:t,scope:r,deployment:n,deploymentProfile:s,scopeFingerprint:o,targetFingerprint:a,consumerId:c,coalesceKey:i,client:d,releaseFingerprint:l}}async function Ul(e){let t=En(),r=_t(t),n=await Ie(),s=(n.scheduler?.rules??[]).filter(d=>d.command==="content-sync"&&d.enabled!==false);if(s.length===0)return 0;let o=await jr(r),a=await Nt(r),c=await Mr(r),i=0;for(let d of s){let u=await $n(n,d),l=c.entries[u.coalesceKey];if(!l||l.releaseJobId!==e||l.scopeFingerprint!==u.scopeFingerprint||l.targetFingerprint!==u.targetFingerprint||l.releaseFingerprint!==u.releaseFingerprint)throw new Error(`The normal release has no matching pre-crawl content-sync cutoff for rule ${String(d.id||"")}.`);let g=Le({releaseJobId:e,releaseFingerprint:u.releaseFingerprint,scopeFingerprint:u.scopeFingerprint,targetFingerprint:u.targetFingerprint,sequence:l.sequence}),p=await u.client.establishBaseline({consumerId:u.consumerId,scopeFingerprint:u.scopeFingerprint,baselineId:g,postTypes:u.scope.postTypes,includeSubsites:u.scope.includeSubsites,sequence:l.sequence}),f={schemaVersion:1,ruleId:String(d.id||""),consumerId:u.consumerId,coalesceKey:u.coalesceKey,scopeFingerprint:u.scopeFingerprint,targetFingerprint:u.targetFingerprint,releaseFingerprint:u.releaseFingerprint,baselineId:g,deploymentProfile:u.deploymentProfile,committedSequence:p.committedSequence,releaseJobId:e,verifiedAt:new Date().toISOString()};o.entries[u.coalesceKey]=f;let w=a.rules[u.coalesceKey];a.rules[u.coalesceKey]={ruleId:String(d.id||""),consumerId:u.consumerId,coalesceKey:u.coalesceKey,committedSequence:p.committedSequence,observedHeadSequence:p.headSequence,lag:Math.max(0,p.headSequence-p.committedSequence),lastSuccessfulJobId:w?.lastSuccessfulJobId||e,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:p.headSequence>p.committedSequence,retryAttempt:0,nextRetryAt:null,lastError:null,baselineStatus:"ready",baselineReason:null};let m=await zn(r,u.coalesceKey);await Ke(t,"content-sync-baseline-established",{id:e},"A normal release established a verified content-sync baseline.",{ruleId:d.id,consumerId:u.consumerId,committedSequence:p.committedSequence,supersededCurrent:m}),i++;}return await Nn(r,o),await Bt(r,a),await D.rm(r.releaseCutoff,{force:true}),i}async function Ll(e){let t=En(),r=_t(t),n=await Ie(),s=(n.scheduler?.rules??[]).filter(c=>c.command==="content-sync"&&c.enabled!==false);if(s.length===0)return 0;let o=await Mr(r),a=0;for(let c of s){let i=await $n(n,c),d=Object.values(o.entries).find(g=>g.releaseJobId===e&&g.ruleId===String(c.id||""));if(d){if(d.coalesceKey===i.coalesceKey&&d.scopeFingerprint===i.scopeFingerprint&&d.targetFingerprint===i.targetFingerprint&&d.releaseFingerprint===i.releaseFingerprint){a++;continue}throw new Error(`The normal release scope changed after its journal cutoff was captured for rule ${String(c.id||"")}. Start a new normal publish instead of resuming this release.`)}let u=await i.client.unboundHead(i.scope.postTypes,i.scope.includeSubsites),l={schemaVersion:1,releaseJobId:e,ruleId:String(c.id||""),coalesceKey:i.coalesceKey,scopeFingerprint:i.scopeFingerprint,targetFingerprint:i.targetFingerprint,releaseFingerprint:i.releaseFingerprint,sequence:u.headSequence,capturedAt:new Date().toISOString()};o.entries[i.coalesceKey]=l,await Ke(t,"content-sync-release-cutoff-captured",{id:e},"A normal release captured its pre-crawl content-sync journal cutoff.",{ruleId:c.id,consumerId:i.consumerId,sequence:u.headSequence}),a++;}return await _n(r,o),a}async function ql(e,t){let r=JSON.parse(await D.readFile(C.join(e,"current-run.json"),"utf8"));if(r.id!==t||r.command!=="content-sync"||!String(r.ruleId||"").trim())throw new Error("The active queue job is not the requested content-sync job.");return r}async function An(e,t,r=0){let n=e.protocol==="https:"?Wn:Xo,s=e.protocol==="https:"?new Wn.Agent({rejectUnauthorized:!t}):void 0;return new Promise((o,a)=>{let c=n.get(e,{agent:s,timeout:3e4,headers:{"user-agent":"WP-Suite-Content-Sync/1"}},i=>{let d=i.statusCode??500,u=i.headers.location;if(d>=300&&d<400&&u&&r<5){i.resume(),An(new URL(u,e),t,r+1).then(o,a);return}let l=[],g=0;i.on("data",p=>{g+=p.byteLength,g<=1048576&&l.push(p);}),i.on("error",a),i.on("end",()=>o({status:d,body:Buffer.concat(l).toString("utf8"),contentType:String(i.headers["content-type"]||"")}));});c.on("timeout",()=>c.destroy(new Error(`Verification timed out: ${e}`))),c.on("error",a);})}async function Wl(e,t,r,n){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 o=JSON.parse(await D.readFile(n,"utf8")),a=new Set;for(let[u,l]of Object.entries(o.pages??{}))a.add(u),typeof l.url=="string"&&a.add(l.url);let c=await lt(e);if(c?.schemaVersion!==2||c.impactHash!==r.impactHash)throw new Error("Content-sync verification requires the matching targeted deploy plan.");let i=Lo(r,a,c.resolvedSitemapUrls),d=new Set(r.resourceRenderUrls.map(u=>{let l=new URL(u);return l.hash="",l.href}));for(let u of i.expectedAvailable){let l=new URL(u),g=new URL(`${l.pathname}${l.search}`,s),p=await An(g,e.ignoreHttpsErrors);if(p.status<200||p.status>=400)throw new Error(`Content-sync verification failed (${p.status}): ${g}`);let f=new URL(u);if(f.hash="",d.has(f.href)){if(!p.contentType.toLowerCase().startsWith("application/json"))throw new Error(`Resource verification expected application/json: ${g}`);try{JSON.parse(p.body);}catch{throw new Error(`Resource verification received invalid JSON: ${g}`)}}}for(let u of i.expectedMissing){let l=new URL(u),g=new URL(`${l.pathname}${l.search}`,s),p=await An(g,e.ignoreHttpsErrors);if(p.status>=200&&p.status<300)throw new Error(`Deleted content is still publicly available: ${g}`)}}async function _l(e){let t=await D.readFile(e.candidateManifest,"utf8");JSON.parse(t);let r=`${e.trustedManifest}.${process.pid}.tmp`;await D.writeFile(r,t,"utf8"),await D.rename(r,e.trustedManifest);}async function Nl(e,t){try{let r=JSON.parse(await D.readFile(e.candidateManifest,"utf8"));if([...Object.values(r.pages??{}),...Object.values(r.assets??{})].some(s=>s?.lastSeenRunId===t))return "resumed"}catch(r){if(r.code!=="ENOENT")throw new Error(`The content-sync candidate manifest is invalid: ${r instanceof Error?r.message:String(r)}`,{cause:r})}return await D.copyFile(e.trustedManifest,e.candidateManifest).catch(r=>{throw new Error(`A trusted crawl manifest is required before content-sync: ${r instanceof Error?r.message:String(r)}`,{cause:r})}),"initialized"}async function ot(e,t,r,n,s={},o=0,a=async()=>{}){t.phase=r,await Bn(e,t),await Kn(e,{schemaVersion:1,jobId:t.jobId,phase:r,completedItems:n,totalItems:n,attempt:o,updatedAt:new Date().toISOString(),details:s}),await Ke(C.dirname(e.state),"content-sync-checkpoint",{id:t.jobId},`Content-sync checkpoint completed: ${r}.`,{phase:r,completedItems:n,totalItems:n,attempt:o,...s}),await a(r,t);}async function Bl(e,t={}){let r={loadConfig:Ie,resolveRuleContext:$n,runCrawl:xo,runDeploy:jo,runInvalidate:Uo,verifyDeployment:Wl,promoteCandidateManifest:_l,clearDeployPlan:Qe,afterCheckpoint:async()=>{},...t},n=En(),s=_t(n),o=await ql(n,e),a=await r.loadConfig(),c=Fl(a,String(o.ruleId)),i=await r.resolveRuleContext(a,c);if(o.coalesceKey&&o.coalesceKey!==i.coalesceKey)throw new Error("Queued content-sync demand no longer matches the active rule scope.");let u=(await jr(s)).entries[i.coalesceKey];if(!u||u.scopeFingerprint!==i.scopeFingerprint||u.targetFingerprint!==i.targetFingerprint||u.releaseFingerprint!==i.releaseFingerprint)throw await Ke(n,"content-sync-baseline-rejected",o,"Content-sync requires a new verified full or incremental release baseline.",{ruleId:c.id,reason:u?"mismatch":"missing"}),new Error("Content-sync baseline is missing or stale. Run a successful normal publish first.");let l=await Dr(s),g=await i.client.head({consumerId:i.consumerId,scopeFingerprint:i.scopeFingerprint,baselineId:u.baselineId,postTypes:i.scope.postTypes,includeSubsites:i.scope.includeSubsites});if(l&&l.jobId!==o.id)throw new Error("Another immutable content-sync range is still active.");if(l||(l={schemaVersion:1,jobId:o.id,ruleId:String(c.id||""),consumerId:i.consumerId,coalesceKey:i.coalesceKey,scopeFingerprint:i.scopeFingerprint,targetFingerprint:i.targetFingerprint,baselineId:u.baselineId,fromSequence:g.committedSequence,toSequence:g.headSequence,phase:"claimed",impactHash:null,startedAt:new Date().toISOString(),updatedAt:new Date().toISOString()},await ot(s,l,"claimed",0,{fromSequence:l.fromSequence,toSequence:l.toSequence},0,r.afterCheckpoint),await Ke(n,"content-sync-range-claimed",o,"Content-sync claimed a closed journal range.",{fromSequence:l.fromSequence,toSequence:l.toSequence})),l.fromSequence===l.toSequence){let h=await Nt(s);h.rules[i.coalesceKey]={ruleId:String(c.id||""),consumerId:i.consumerId,coalesceKey:i.coalesceKey,committedSequence:l.toSequence,observedHeadSequence:g.headSequence,lag:Math.max(0,g.headSequence-l.toSequence),lastSuccessfulJobId:o.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:false,retryAttempt:0,nextRetryAt:null,lastError:null},await Bt(s,h),await Fr(s);return}let p=await Hn(s);if(!Pt(l.phase,"impact-planned")){let h=await i.client.events({consumerId:i.consumerId,scopeFingerprint:i.scopeFingerprint,baselineId:u.baselineId,postTypes:i.scope.postTypes,includeSubsites:i.scope.includeSubsites,afterSequence:l.fromSequence,throughSequence:l.toSequence}),y=Or({events:h,scope:i.scope,sourceOrigin:a.sourceOrigin}),b=await i.client.resolveImpactFamilies(y,i.scope.includeSubsites);p=qn({jobId:o.id,ruleId:String(c.id||""),consumerId:i.consumerId,coalesceKey:i.coalesceKey,scopeFingerprint:i.scopeFingerprint,targetFingerprint:i.targetFingerprint,baselineId:u.baselineId,sourceOrigin:a.sourceOrigin,scope:i.scope,fromSequence:l.fromSequence,toSequence:l.toSequence,events:h,resolvedArchiveFamilies:b,sitemapUrls:kt(a.sitemapPaths)}),await Vn(s,p),l.impactHash=p.impactHash,await ot(s,l,"impact-planned",h.length,{eventCount:h.length,foldedCount:p.foldedCount,impactHash:p.impactHash},0,r.afterCheckpoint),await Ke(n,"content-sync-impact-planned",o,"Content-sync persisted an immutable impact plan.",{impactHash:p.impactHash,eventCount:h.length});}if(!p||p.jobId!==o.id||l.impactHash!==p.impactHash)throw new Error("The immutable content-sync impact plan is missing or mismatched.");if(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH=s.candidateManifest,!Pt(l.phase,"rendered")){let h=await Nl(s,o.id);await Ke(n,h==="resumed"?"content-sync-render-resumed":"content-sync-render-initialized",o,h==="resumed"?"Content-sync resumed its candidate crawl manifest.":"Content-sync initialized a candidate crawl manifest.",{impactHash:p.impactHash}),await r.runCrawl(["--content-sync-plan",s.impact]),await ot(s,l,"rendered",p.directRenderUrls.length+p.archiveFamilyUrls.length+p.resourceRenderUrls.length,{impactHash:p.impactHash},o.attempt??0,r.afterCheckpoint);}let f=i.deploymentProfile?["--profile",i.deploymentProfile]:[];if(Pt(l.phase,"deployed")||(await r.runDeploy([...f,"--content-sync"]),await ot(s,l,"deployed",1,{impactHash:p.impactHash},0,r.afterCheckpoint),await Ke(n,"content-sync-deployed",o,"Content-sync targeted deployment completed.",{impactHash:p.impactHash})),Pt(l.phase,"invalidated")?Pt(l.phase,"verified")||await r.runInvalidate([...f,"--content-sync"]):(await r.runInvalidate([...f,"--content-sync"]),await ot(s,l,"invalidated",1,{impactHash:p.impactHash},0,r.afterCheckpoint)),Pt(l.phase,"verified")||(await r.verifyDeployment(a,i,p,s.candidateManifest),await r.promoteCandidateManifest(s),await ot(s,l,"verified",1,{impactHash:p.impactHash},0,r.afterCheckpoint)),g=await i.client.head({consumerId:i.consumerId,scopeFingerprint:i.scopeFingerprint,baselineId:u.baselineId,postTypes:i.scope.postTypes,includeSubsites:i.scope.includeSubsites}),g.committedSequence===l.fromSequence)await i.client.acknowledge({consumerId:i.consumerId,scopeFingerprint:i.scopeFingerprint,baselineId:u.baselineId,expectedSequence:l.fromSequence,sequence:l.toSequence,postTypes:i.scope.postTypes,includeSubsites:i.scope.includeSubsites});else if(g.committedSequence!==l.toSequence)throw new Error("Content-sync acknowledgement cursor is in an unexpected state.");await ot(s,l,"acknowledged",1,{sequence:l.toSequence},0,r.afterCheckpoint),await Ke(n,"content-sync-cursor-acknowledged",o,"Content-sync committed its journal cursor after verification.",{sequence:l.toSequence});let w=await i.client.head({consumerId:i.consumerId,scopeFingerprint:i.scopeFingerprint,baselineId:u.baselineId,postTypes:i.scope.postTypes,includeSubsites:i.scope.includeSubsites}),m=await Nt(s);m.rules[i.coalesceKey]={ruleId:String(c.id||""),consumerId:i.consumerId,coalesceKey:i.coalesceKey,committedSequence:l.toSequence,observedHeadSequence:w.headSequence,lag:Math.max(0,w.headSequence-l.toSequence),lastSuccessfulJobId:o.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:w.headSequence>l.toSequence,retryAttempt:0,nextRetryAt:null,lastError:null},await Bt(s,m),await r.clearDeployPlan(a),await D.rm(C.join(n,"content-sync-invalidation.json"),{force:true}),await D.rm(s.candidateManifest,{force:true}),await Fr(s);}async function _o(e=process.argv.slice(2)){let r=e.indexOf("--capture-baseline-cutoffs");if(r>=0){let a=String(e[r+1]||"").trim();if(!a)throw new Error("--capture-baseline-cutoffs requires a release job ID.");await Ll(a);return}let n=e.indexOf("--establish-baselines");if(n>=0){let a=String(e[n+1]||"").trim();if(!a)throw new Error("--establish-baselines requires a release job ID.");await Ul(a);return}let s=e.indexOf("--job-id"),o=s>=0?String(e[s+1]||"").trim():"";if(!o)throw new Error("Content-sync requires --job-id.");await Bl(o);}_o().catch(e=>{console.error(e instanceof Error?e.message:String(e)),process.exit(1);});
18
+ ${o}`,40);return n.info(`Using generated 404 fallback change token for ${r}`,{url:r,requestPath:e.generated404RequestPath,source:"generated-404-change-token-fallback"}),{url:r,supported:!0,token:a,tokenSource:"generated-404-response-hash",postId:null,dependencyPostIds:[],reason:null}}catch(s){return n.warn(`Generated 404 fallback token lookup failed for ${r}`,{url:r,error:String(s),requestPath:e.generated404RequestPath,source:"generated-404-change-token-fallback"}),null}}async function lo(e,t,r,n,s,o){let a=Sr(e,s);if(await Oa(e,t,r,n,s,o),!n.enabled)return {action:"render",changeToken:n.trackManifest?await mr(e,t,n,s,o):null};let c=n.manifest.pages[s];if(!c)return {action:"render",changeToken:await mr(e,t,n,s,o)};let i=await mr(e,t,n,s,o);if(i?.supported&&i.token)return c.changeToken===i.token?a&&!await Fa(e,t,s,o)?{action:"render",changeToken:i}:{action:"reuse",changeToken:i}:{action:"render",changeToken:i};let d=nt(r.sitemapLastmodByPage[s]);return d&&c.sitemapLastmod&&c.sitemapLastmod===d?{action:"reuse",changeToken:i}:{action:"render",changeToken:i}}async function Fa(e,t,r,n){try{let o=(await t.get(r,{timeout:Math.min(e.navigationTimeoutMs,15e3),failOnStatusCode:!1})).status();return o===404?!0:(n.warn(`Generated 404 request path no longer returns 404 for ${r}`,{url:r,status:o,requestPath:e.generated404RequestPath,source:"generated-404-status-probe"}),!1)}catch(s){return n.warn(`Generated 404 status probe failed for ${r}`,{url:r,error:String(s),requestPath:e.generated404RequestPath,source:"generated-404-status-probe"}),false}}async function Da(e,t,r,n,s){let o=Js(r.previousManifest,void 0,n,"previous-manifest"),a=0;for(let[g,p]of Object.entries(r.manifest.pages))if(!r.seenPages.has(g)){mn(s,e,p.outputPath);try{await F.unlink(p.outputPath);}catch(f){(f.code||"")!=="ENOENT"&&n.warn(`Failed to remove stale incremental page output for ${g}`,{url:g,outputPath:p.outputPath,error:String(f)});}delete r.manifest.pages[g],a++;}let c=Js(r.manifest,r.runId,n,"active-manifest"),i=new Set,d=new Set;for(let g of c){for(let w of Mt(e,g))i.add(w);let p=r.manifest.assets[g]?.outputPath;p&&d.add(p);let f=ht(e,t.assetMap,g);f&&d.add(f);}for(let g of Object.keys(r.manifest.assets)){if(c.has(g)){r.manifest.assets[g].unreferencedRuns=0;continue}let p=r.manifest.assets[g];if(ks(p.unreferencedRuns)){p.unreferencedRuns+=1,n.warn(`Deferred deletion of newly unreferenced incremental asset ${g}.`,{url:g,unreferencedRuns:p.unreferencedRuns,phase:"incremental-cleanup",source:"asset-deletion-grace"});continue}delete r.manifest.assets[g];}let u=0,l=0;for(let g of o){if(r.manifest.assets[g])continue;let p=new Set,f=r.previousManifest.assets[g]?.outputPath;f&&p.add(f);let w=ht(e,t.assetMap,g);if(!c.has(g)&&w)p.add(w);else if(!c.has(g)){let m=Xa(e,g,n,"incremental-cleanup");m&&(p.add(m.originalFilePath),m.hashedFilePath&&p.add(m.hashedFilePath));}for(let m of p)if(!d.has(m)){mn(s,e,m);try{await F.unlink(m),u++;}catch(h){(h.code||"")!=="ENOENT"&&n.warn(`Failed to remove stale incremental asset output for ${g}`,{url:g,outputPath:m,error:String(h)});}}for(let m of Mt(e,g))i.has(m)||Object.prototype.hasOwnProperty.call(t.assetMap,m)&&(delete t.assetMap[m],l++);}(a>0||u>0||l>0)&&n.info(`Incremental cleanup removed ${a} stale pages, ${u} stale assets, ${l} stale asset mappings`,{removedPages:a,removedAssets:u,removedAssetMappings:l,phase:"incremental-cleanup"});}async function Ua(e,t,r,n){let s=new Set([...Object.values(t.manifest.pages),...Object.values(t.manifest.assets)].map(i=>String(i.outputPath||"").trim()).filter(Boolean).map(i=>k.resolve(i))),o=Object.values(e.postCrawlCopyMap||{}).map(i=>gr(String(i||""))).filter(i=>!!i).map(i=>k.resolve(e.outputDir,i)),c=(await Ci(["**/*.html","**/*.htm"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(i=>Cs(k.join(e.outputDir,i),e.outputDir,s,o));for(let i of c){let d=k.join(e.outputDir,i);mn(n,e,d),await F.unlink(d);}c.length>0&&r.info(`Removed ${c.length} orphaned HTML output file(s) that are absent from the current crawl manifest.`,{phase:"incremental-cleanup",source:"manifest-output-reconciliation",removedPages:c.length,samples:c.slice(0,20)});}function Js(e,t,r,n){let s=new Set,o=a=>{let c=a.trim();if(c){if(r){let i=xe(c,r,"asset",n??"manifest");if(!i)return;s.add(i.toString());return}s.add(c);}};for(let a of Object.values(e.pages))for(let c of a.discoveredAssets||[])o(c);for(let[a,c]of Object.entries(e.assets||{}))t&&c.lastSeenRunId!==t||o(a);return s}function Qs(e,t,r){for(let n of t)for(let s of Mt(e,n))if(r.has(s))return true;return false}function La(e,t,r,n,s){let o=new Set(r.rewriteTargets);for(let c of r.changedFiles)o.add(k.resolve(e.outputDir,c));for(let c of Object.values(t.manifest.pages))c.rewriteComplete||o.add(k.resolve(c.outputPath));for(let c of Object.values(t.manifest.assets))c.isText&&!c.rewriteComplete&&o.add(k.resolve(c.outputPath));let a=$a(n,s);if(a.size===0)return [...o];for(let c of Object.values(t.manifest.pages))Qs(e,c.discoveredAssets,a)&&o.add(k.resolve(c.outputPath));for(let[c,i]of Object.entries(t.manifest.assets)){i.isText&&Qs(e,i.discoveredAssets,a)&&o.add(k.resolve(i.outputPath));for(let d of Mt(e,c))if(a.has(d)){ue(r,e,i.outputPath),i.isText&&o.add(k.resolve(i.outputPath));break}}return [...o]}function Wa(e,t){let r=t?new Set(Array.from(t,s=>k.resolve(s))):null,n=s=>r===null||r.has(k.resolve(s));for(let s of Object.values(e.manifest.pages))n(s.outputPath)&&(s.rewriteComplete=true);for(let s of Object.values(e.manifest.assets)){if(!s.isText){s.rewriteComplete=true;continue}n(s.outputPath)&&(s.rewriteComplete=true);}}function qa(e,t,r){let n=new Set,s=o=>{if(!o)return;let a=ge(e.outputDir,o);a&&n.add(a);};for(let o of t.donePages)s(De(e,o));for(let o of Object.values(r.manifest.pages))o.lastSeenRunId===r.runId&&s(o.outputPath);for(let o of t.doneAssets)s(ht(e,t.assetMap,o));for(let o of Object.values(r.manifest.assets))o.lastSeenRunId===r.runId&&s(o.outputPath);return n}async function _a(e,t,r,n,s,o){let a=[...n.deletedFiles].sort(),c=qa(e,t,r),i=0;for(let l of [...c].sort())if(!o?.objects[l]){try{if((await F.stat(k.join(e.outputDir,l))).isFile())continue}catch{}i++,s.warn(`Active output is missing after crawl: ${l}`,{phase:"deploy-plan",file:l,outputPath:k.join(e.outputDir,l)});}if(i>0&&s.warn(`Detected ${i} active output file(s) missing before deploy plan save.`,{phase:"deploy-plan",missingActiveOutputs:i}),a.length===0)return [];let d=[],u=0;for(let l of a){if(!c.has(l)){d.push(l);continue}u++;let g=k.join(e.outputDir,l),p;try{p=(await F.stat(g)).isFile();}catch{p=false;}s.warn(`Deploy plan protected active output from deletion: ${l}`,{phase:"deploy-plan",file:l,outputPath:g,exists:p});}return u>0&&s.info(`Deploy plan skipped ${u} active output deletion(s).`,{phase:"deploy-plan",protectedDeletes:u,deletedFiles:d.length}),d}function Mt(e,t){let r=new Set([t]);try{let n=new URL(t),o=[new URL(e.sourceOrigin).origin,...Object.keys(e.extraReplacements||{})];if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{o.push(new URL(e.targetOrigin).origin);}catch{}for(let a of o)try{r.add(new URL(a).origin+n.pathname+n.search);}catch{}r.add(n.pathname+n.search),r.add(n.pathname);}catch{}return [...r]}function Na(e,t){let r=new Set([t]);try{let n=new URL(t),o=[new URL(e.sourceOrigin).origin,...Object.keys(e.extraReplacements||{})];if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{o.push(new URL(e.targetOrigin).origin);}catch{}for(let a of o)try{r.add(new URL(a).origin+n.pathname+n.search);}catch{}r.add(n.pathname+n.search);}catch{}return [...r]}function uo(e,t){let r=e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/"?e.targetOrigin:"https://relative.invalid/";try{let n=new URL(t,r);return At(e.outputDir,n.pathname)}catch{return null}}function po(e,t,r){for(let n of Na(e,r)){let s=t[n];if(!s)continue;let o=uo(e,s);if(o)return {outputPath:o,publicUrl:s}}return null}function Ba(e,t,r){for(let n of Mt(e,r)){let s=t[n];if(!s)continue;let o=uo(e,s);if(o)return o}return null}function ht(e,t,r){let n=Ba(e,t,r);if(n)return n;try{return Ut(e,r).originalFilePath}catch{return null}}function Ha(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?k.join(k.resolve(e),"queue-runner-heartbeat.json"):""}async function Et(e){let t=Ha();if(t)try{let r=await F.readFile(t,"utf8"),n=JSON.parse(r);if(!n||typeof n!="object")return;await F.writeFile(t,JSON.stringify({...n,checkedAt:new Date().toISOString(),status:"running",currentStep:"rewrite",message:e},null,2),"utf8");}catch{}}function Va(e,t){let r=new Set;for(let n of t.donePages)r.add(k.resolve(De(e,n)));for(let n of t.doneAssets){let s=ht(e,t.assetMap,n);!s||!Oe(s)||r.add(k.resolve(s));}for(let n of t.doneSitemaps){let s=ht(e,t.assetMap,n);!s||!Oe(s)||r.add(k.resolve(s));}return [...r]}var fr="re:",pr=new Map;function Ka(e){if(!e.startsWith(fr))return null;let t=pr.get(e);if(t!==void 0)return t||null;let r=e.slice(fr.length).trim();if(!r)return pr.set(e,false),null;try{let n=new RegExp(r);return pr.set(e,n),n}catch{return console.warn(`[crawl] Ignoring invalid path matcher regex: ${e}`),pr.set(e,false),null}}function Rn(e,t){let r=Ka(t);return r?r.test(e):t.startsWith(fr)?false:e.startsWith(t)}function za(e,t){return t.find(r=>Rn(e,r))}function mo(e,t){let r=za(t.pathname,e.blockedPathPrefixes);if(r)return r.startsWith(fr)?`blocked path regex: ${r}`:`blocked path prefix: ${r}`;let n=e.blockedSearchFragments.find(s=>t.search.includes(s));return n?`blocked search fragment: ${n}`:null}function Ja(e){let t=new URL(e.sourceOrigin),r=new Set([t.hostname,...e.allowedAssetHosts]);if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{r.add(new URL(e.targetOrigin).hostname);}catch{}for(let n of Object.keys(e.extraReplacements||{}))try{r.add(new URL(n).hostname);}catch{}return r}function Qa(e,t){return Ja(e).has(t.hostname)}function Ga(e){let t=Ne(e.trim()).replace(/\\\//g,"/").replace(/^['"]|['"]$/g,"").trim();return t=t.replace(/[)]+$/g,"").trim(),t=t.replace(/;.*$/g,"").trim(),t}function fn(e){let t=Ne(e).replace(/\\\//g,"/");return /[{}]|\bwindow\.|\blocation\.|\bincludes\(|\?\?null|\+|%7B|%7D|%22|<|>|\s/.test(t)||/[()[\]'"]/.test(t)}function Za(e){let t=Ne(e).replace(/\\\//g,"/").trim(),r=k.extname(t).toLowerCase();return no.has(r)||so.has(r)?false:/^(?:this|[A-Za-z_$][\w$]*)\.[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/.test(t)}function st(e){return Sa.some(t=>e.pathname.startsWith(t))}function ie(e,t,r=e.sourceOrigin,n,s="url",o=false){let a=Ga(t);if(!a||a.startsWith("data:")||a.startsWith("blob:")||a.startsWith("mailto:")||a.startsWith("tel:")||a.startsWith("#"))return n?.ignore(s,t,"empty or unsupported scheme",r),null;if(fn(a))return n?.ignore(s,t,"looks like JavaScript/code fragment",r),null;if(Za(a))return n?.ignore(s,t,"looks like JavaScript property access",r),null;try{let c=new URL(a,r);if(go(c))return n?.reject(s,t,"malformed URL encoding",r),null;if(!["http:","https:"].includes(c.protocol))return n?.ignore(s,t,"unsupported protocol: ",r),null;if(!Qa(e,c))return n?.ignore(s,c.toString(),"host not allowed",r),null;let i=mo(e,c);if(i&&!(o&&_r(c)))return n?.reject(s,c.toString(),i,r),null;let d=new URL(e.sourceOrigin);return c.protocol=d.protocol,c.host=d.host,c.hash="",c}catch{return n?.reject(s,t,"invalid URL",r),null}}function go(e){try{return decodeURIComponent(e.pathname),decodeURIComponent(e.search),decodeURIComponent(e.hash),!1}catch{return true}}function xe(e,t,r,n){try{let s=new URL(e);return go(s)?(t.reject(r,e,"malformed URL encoding",n),null):Tt(s.pathname)===null?(t.reject(r,e,"unsafe URL pathname",n),null):s.protocol!=="http:"&&s.protocol!=="https:"?(t.reject(r,e,`unsupported protocol: ${s.protocol}`,n),null):s}catch{return t.reject(r,e,"invalid URL",n),null}}function fo(e){return no.has(k.extname(e.pathname).toLowerCase())}function Ke(e){return st(e)?false:so.has(k.extname(e.pathname).toLowerCase())}function yt(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function Y(e,t){return !!(yt(t)||t.pathname==="/robots.txt"||t.pathname==="/llms.txt"||t.pathname.toLowerCase().endsWith(".xsl")||e.assetPathPrefixes.length>0&&e.assetPathPrefixes.some(r=>Rn(t.pathname,r))||fo(t)||!Ke(t)&&!st(t))}function De(e,t){let r=new URL(t),n=Tt(r.pathname);if(n===null)throw new Error(`Unsafe page URL pathname: ${r.pathname}`);return n.endsWith("/")?n+="index.html":k.extname(n)||(n+="/index.html"),At(e.outputDir,n)}function Ya(e){if(!e.generated404RequestPath)return null;try{let t=new URL(e.generated404RequestPath,e.sourceOrigin);return !t.pathname.endsWith("/")&&k.extname(t.pathname)===""&&(t.pathname+="/"),t.hash="",t.toString()}catch{return null}}function Sr(e,t){let r=Ya(e);if(!r)return false;try{return new URL(t,e.sourceOrigin).toString()===r}catch{return false}}function Ut(e,t){let r=new URL(t),n=Tt(r.pathname);if(n===null)throw new Error(`Unsafe asset URL pathname: ${r.pathname}`);n.endsWith("/")&&(n+="index.html");let s=!!r.search,o=n,a=At(e.outputDir,o),c=Zr(e.targetOrigin,o),i=`${c}${r.search}`;if(!s)return {originalPathname:o,originalFilePath:a,originalPublicUrl:c,originalPublicUrlWithSearch:i,preferQueryHashed:s};let d=k.extname(n),l=`${d?n.slice(0,-d.length):n}.${$e(r.search)}${d||".bin"}`;return {originalPathname:o,originalFilePath:a,originalPublicUrl:c,originalPublicUrlWithSearch:i,preferQueryHashed:s,hashedPathname:l,hashedFilePath:At(e.outputDir,l),hashedPublicUrl:Zr(e.targetOrigin,l)}}function Xa(e,t,r,n){let s=xe(t,r,"asset",n);return s?Ut(e,s.toString()):null}function hn(e,t,r,n,s){t[r]=n;try{let o=new URL(r),c=[new URL(e.sourceOrigin).origin,...Object.keys(e.extraReplacements||{})];if(e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/")try{c.push(new URL(e.targetOrigin).origin);}catch{}for(let i of c)try{let d=new URL(i);t[d.origin+o.pathname+o.search]=n;}catch{}try{let i=(()=>{let d=new URL(n,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin);return `${d.pathname}${d.search}`})();t[o.pathname+o.search]=i;}catch{}try{let i=new URL(n,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin).pathname;t[o.pathname]=i;}catch{}try{let i=new URL(s,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin).pathname;t[o.pathname]=i;}catch{}}catch{}}async function ho(e){try{return await F.readFile(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}async function Xe(e){try{return await F.stat(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}function ec(e){let t=String(e["content-length"]||"").trim();if(!t)return null;let r=Number.parseInt(t,10);return Number.isFinite(r)&&r>=0?r:null}function wo(e){let t=String(e["last-modified"]||"").trim();if(!t)return null;let r=Date.parse(t);return Number.isFinite(r)?r:null}function tc(e,t){let r=wo(t),n=ec(t),s=e?.mtime.getTime();return r!==null&&Math.abs((s??0)-r)>=1e3||n!==null&&e?.size!==n?false:r!==null||n!==null}async function rc(e,t){if(!t)return;let r=wo(t);if(r===null)return;let n=await Xe(e);if(!n)return;let s=new Date(r);await F.utimes(e,n.atime,s).catch(()=>{});}function wn(e,t){let r=k.extname(e.pathname).toLowerCase(),n=t["content-type"]||"";return ba.has(r)||ya.some(s=>n.includes(s))&&!n.startsWith("image/")&&!n.includes("font")}function nc(e){try{let t=k.extname(new URL(e).pathname).toLowerCase();return t===".js"||t===".mjs"}catch{return false}}function Ye(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 sc(e){return $e(e.toString("base64"),40)}function yo(e,t,r){let n=e.assetVariantDigestsByOriginalPath.get(t);if(!n||n.size===0)return false;for(let s of n)if(s!==r)return true;return false}function bo(e,t,r){let n=e.assetVariantDigestsByOriginalPath.get(t);n||(n=new Set,e.assetVariantDigestsByOriginalPath.set(t,n)),n.add(r);}async function yn(e,t,r,n,s,o,a){let c=Ut(e,r),i=sc(n),d=c.originalFilePath,u=c.originalPublicUrlWithSearch;if(c.preferQueryHashed&&c.hashedFilePath&&c.hashedPublicUrl&&yo(s,c.originalFilePath,i)){let f=po(e,t,r);f?(d=f.outputPath,u=f.publicUrl):(d=c.hashedFilePath,u=c.hashedPublicUrl);}hn(e,t,r,u,c.originalPublicUrl);let l=Oe(d)?Buffer.from(tn(n.toString("utf8"),e,t,d),"utf8"):n,g=await ho(d),p=false;return (g===null||!g.equals(l))&&(await _e(d,l),p=true),o?.debug(`${p?"Saved":"Skipped unchanged"} asset output ${r}`,{phase:"download-assets",url:r,outputPath:d,bytes:l.length,wroteFile:p}),bo(s,c.originalFilePath,i),await rc(d,a),s.stats.assetsSaved++,o&&(s.stats.assetsSaved%50===0||s.stats.assetsSaved===1)&&o.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:d,publicUrl:u,wroteFile:p}}function ot(e,t,r,n,s){let o=()=>{let l=String(s??"").trim();if(!l)return {discoveredFrom:"",discoveredFromType:"unknown"};if(l==="seed-path"||l==="cli"||l==="generated-404-request-path")return {discoveredFrom:l,discoveredFromType:l};if(/^https?:\/\//i.test(l))try{let g=new URL(l);return yt(g)?{discoveredFrom:l,discoveredFromType:"sitemap"}:Y(e,g)?{discoveredFrom:l,discoveredFromType:"asset"}:Ke(g)&&!st(g)?{discoveredFrom:l,discoveredFromType:"page"}:{discoveredFrom:l,discoveredFromType:"url"}}catch{return {discoveredFrom:l,discoveredFromType:"url"}}return {discoveredFrom:l,discoveredFromType:"url"}},a=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,c=ie(e,r,a,n,"page");if(!c)return;if(!Ke(c)||fo(c)){if(Y(e,c)){n.info(`Seeded/non-page URL queued as asset: ${c.toString()}`,{url:c.toString(),source:s}),Z(e,t,c.toString(),n,s);return}n.reject("page",c.toString(),st(c)?"asset/internal path cannot be page":"not page-like",s);return}let i=c.toString(),{discoveredFrom:d,discoveredFromType:u}=o();d&&!t.pageDiscoverySources.has(i)&&t.pageDiscoverySources.set(i,d),!t.donePages.has(i)&&!t.queuedPages.has(i)&&(t.queuedPages.add(i),t.pageQueue.push(i),t.stats.pagesQueued++,n.debug(`Queued discovered page ${i}`,{phase:"discovery",url:i,discoveredFrom:d,discoveredFromType:u,outputPath:De(e,i)}),(t.stats.pagesQueued%25===0||t.stats.pagesQueued===1)&&n.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 Z(e,t,r,n,s){let o=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,a=ie(e,r,o,n,"asset");if(!a)return;if(!Y(e,a)){n.reject("asset",a.toString(),"not a safe asset path/prefix",s);return}let c=a.toString();!t.doneAssets.has(c)&&!t.queuedAssets.has(c)&&(t.queuedAssets.add(c),t.assetQueue.push(c),t.stats.assetsQueued++,(t.stats.assetsQueued%100===0||t.stats.assetsQueued===1)&&n.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 hr(e,t,r,n,s){let o=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,a=ie(e,r,o,n,"sitemap",Gn(s));if(!a)return;if(!yt(a)){n.reject("sitemap",a.toString(),"not sitemap-like",s);return}let c=a.toString();!t.doneSitemaps.has(c)&&!t.queuedSitemaps.has(c)&&(t.queuedSitemaps.add(c),t.sitemapQueue.push(c),t.stats.sitemapsQueued++,(t.stats.sitemapsQueued%10===0||t.stats.sitemapsQueued===1)&&n.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 oc(e){let t=Ne(e),r=[...t.matchAll(/<url>\s*([\s\S]*?)\s*<\/url>/gi)].map(n=>n[1]);return r.length>0?r.map(n=>{let s=n.match(/<loc>\s*([^<]+?)\s*<\/loc>/i);if(!s?.[1])return null;let o=n.match(/<lastmod>\s*([^<]+?)\s*<\/lastmod>/i);return {loc:s[1].trim(),lastmod:nt(o?.[1]??null)}}).filter(n=>!!n):[...t.matchAll(/<loc>\s*([^<]+?)\s*<\/loc>/gi)].map(n=>({loc:n[1].trim(),lastmod:null})).filter(n=>n.loc)}function ic(e){return e.split(",").map(t=>t.trim().split(/\s+/)[0]).filter(Boolean)}function ac(e){let t=[],r=/url\(\s*(?:"([^"]+)"|'([^']+)'|([^)]*?))\s*\)/gi;for(let n of e.matchAll(r)){let s=e.slice(Math.max(0,n.index-8),n.index);if(/\bnew\s+$/i.test(s))continue;let o=(n[1]||n[2]||n[3]||"").trim();o&&t.push(o);}return t}function cc(e){return e.replace(/<script\b[^>]*>[\s\S]*?<\/script\s*>/gi,"")}function lc(e,t){if(t==="dom-style")return true;let r=(()=>{try{return k.extname(new URL(e).pathname).toLowerCase()}catch{return ""}})();return !(r===".js"||r===".mjs"||r===".map"||t.includes("javascript"))}function gr(e){return e.trim().replace(/\\/g,"/").replace(/^https?:\/\/[^/]+/i,"").replace(/^\/+/,"")}async function So(e,t,r,n){let s=[];await F.mkdir(t,{recursive:true});let o=await F.readdir(e,{withFileTypes:true});for(let a of o){let c=k.join(e,a.name),i=k.join(t,a.name);if(a.isDirectory()){s.push(...await So(c,i,r,n));continue}await Po(c,i,r,n)&&s.push(i);}return s}async function uc(e,t){try{let[r,n]=await Promise.all([F.stat(e),F.stat(t)]);if(r.size!==n.size)return !1;let[s,o]=await Promise.all([F.readFile(e),F.readFile(t)]);return s.equals(o)}catch{return false}}async function Po(e,t,r,n){let s=k.resolve(t);return r.has(s)?(n.debug("Skipped post-crawl copy over crawled page output",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):await uc(e,t)?(n.debug("Skipped unchanged post-crawl copy target",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):(await F.mkdir(k.dirname(t),{recursive:true}),await F.copyFile(e,t),true)}async function dc(e,t,r){let n=Object.entries(e.postCrawlCopyMap||{}).map(([c,i])=>({sourcePath:c.trim(),prefix:String(i||"").trim()}));if(n.length===0)return [];let s=0,o=[],a=new Set([...t.donePages].map(c=>k.resolve(De(e,c))));r.mark("copy-extra-paths");for(let c of n){let i=c.sourcePath,d=c.prefix;if(!i||!d){r.warn("Skipped post-crawl copy mapping with empty key/value",{sourcePath:i,prefixPath:d});continue}let u=ka(i);if(!u.resolvedPath){r.warn("Skipped post-crawl copy source because alias root is not configured",{sourcePath:i,alias:u.alias||"",requiredEnv:u.requiredEnv||"",prefixPath:d});continue}let l=u.resolvedPath,g=gr(d);if(!g){r.warn("Skipped post-crawl copy mapping with invalid export prefix",{sourcePath:i,prefixPath:d});continue}let p;try{p=await F.stat(l);}catch{r.warn("Skipped post-crawl copy source because it does not exist",{sourcePath:i,sourceAbs:l,prefixPath:d});continue}let f=k.resolve(e.outputDir,g);if(p.isDirectory()){let y=await So(l,f,a,r);o.push(...y),s+=y.length,y.length>0&&r.progress(`Copied static directory to export: ${l} -> /${g}`,{phase:"copy-extra-paths",sourcePath:l,targetPath:`/${g}`,copiedItems:s,changedFiles:y.length});continue}let w=k.basename(l),m=d.endsWith("/")?k.join(f,w):f;await Po(l,m,a,r)&&(o.push(m),s++,r.progress(`Copied static file to export: ${l} -> /${gr(d.endsWith("/")?`${d}${w}`:d)}`,{phase:"copy-extra-paths",sourcePath:l,targetPath:`/${gr(d.endsWith("/")?`${d}${w}`:d)}`,copiedItems:s}));}return r.endMark("copy-extra-paths",{mappedSources:n.length,copiedItems:s}),o}function wt(e,t,r,n,s){let o=new Set,a=Ne(r).replace(/\\\//g,"/"),c=s==="page-html"?cc(a):a,i=lc(t,s);if(i){for(let d of ac(c)){let u=ie(e,d,t,n,`${s}:css-url`);u&&Y(e,u)&&o.add(u.toString());}for(let d of c.matchAll(/@import\s+(?:url\()?\s*['"]?([^'"\s;]+)['"]?\s*\)?/gi)){let u=ie(e,d[1],t,n,`${s}:css-import`);u&&Y(e,u)&&o.add(u.toString());}}if(i||s==="page-html")for(let d of c.matchAll(/<\?xml-stylesheet[^>]+href=["']([^"']+)["'][^>]*\?>/gi)){let u=ie(e,d[1],t,n,`${s}:xml-stylesheet`);u&&Y(e,u)&&o.add(u.toString());}if(s==="page-html")for(let d of Ps(a)){let u=d.startsWith("//")?`${new URL(e.sourceOrigin).protocol}${d}`:d;if(fn(u)){n.ignore(`${s}:serialized-url`,u,"looks like JavaScript/code fragment",t);continue}let l=ie(e,u,t,n,`${s}:serialized-url`);l&&Y(e,l)?o.add(l.toString()):l&&n.ignore("asset",l.toString(),"serialized URL did not pass safe asset path filter",t);}if(s!=="page-html"&&nc(t))for(let d of Rs(a)){if(fn(d)||!/\.(?:js|mjs)(?:[?#]|$)/i.test(d))continue;let u=ie(e,d,t,n,`${s}:relative-asset`);u&&Y(e,u)&&o.add(u.toString());}return [...o]}function Ro(e,t,r,n){let s=new Set,o=Ne(r);for(let a of o.matchAll(/href=["']([^"'#\s][^"']*?)["']/gi)){let c=a[1],i=ie(e,c,t,n,"page-link");i&&(mo(e,i)||Ke(i)&&!st(i)&&!Y(e,i)&&s.add(i.toString()));}return [...s]}async function Gs(e,t,r,n,s,o,a=false,c=[]){let i=new Set(c),d=0,u=new Map,l=(g,p)=>{let f=[p],w=new Set;for(;f.length>0;){let m=f.pop();if(m===g)return true;w.has(m)||(w.add(m),f.push(...u.get(m)??[]));}return false};for(;r.sitemapQueue.length>0;){let g=r.sitemapQueue.shift();r.queuedSitemaps.delete(g);let p=xe(g,n,"sitemap","sitemap-queue");if(!p)continue;let f=p.toString();if(r.doneSitemaps.has(f))continue;r.doneSitemaps.add(f),n.sitemap(`Fetching sitemap ${f}`,{url:f});let w=true;try{let m=await t.get(f,{timeout:6e4});if(w=!1,!m.ok()){if(n.skip("sitemap",f,m.status()),a&&!i.has(f))throw new Error(`Required child content-sync sitemap returned HTTP ${m.status()}: ${f}`);continue}let h=await m.body(),y=m.url()||f,S=xe(y,n,"sitemap-response",f);if(!S)continue;let P=S.toString();r.successfulSitemaps.add(f),r.successfulSitemaps.add(P),i.has(f)&&(r.resolvedSitemapRoots.add(P),d+=1);let M=await yn(e,r.assetMap,P,h,r,n);ue(s,e,M.outputPath),ke(s,M.outputPath);let A=h.toString("utf8");/<sitemapindex(?:\s|>)/i.test(A)?r.sitemapIndexUrls.add(P):r.sitemapLeafUrls.add(P);let C=wt(e,P,A,n,"sitemap");for(let $ of C)Z(e,r,$,n,P);let R=[];for(let $ of oc(A)){let T=ie(e,$.loc,P,n,"sitemap-loc",!0);if(!T){if(a)throw new Error(`Content-sync sitemap contains an inadmissible location: ${$.loc}`);continue}if(yt(T)){if(l(P,T.toString()))throw new Error(`Sitemap index cycle detected: ${P} -> ${T.toString()}`);if(u.has(P)||u.set(P,new Set),u.get(P).add(T.toString()),R.push(T.toString()),a&&r.doneSitemaps.has(T.toString())&&!r.successfulSitemaps.has(T.toString()))throw new Error(`Required child content-sync sitemap was not resolved: ${T.toString()}`);hr(e,r,T.toString(),n,P);}else if(a){if(Ke(T)&&!Y(e,T)){let N=o.requiredUrls.has(T.toString()),H=o.manifest.pages[T.toString()],b=!!H&&!!await Xe(H.outputPath);if(!N&&!b)throw new Error(`Content-sync sitemap coverage is incomplete for ${T.toString()}.`)}n.ignore("sitemap-loc",T.toString(),"content-sync sitemap-only traversal does not enqueue urlset locations",P);}else Y(e,T)?Z(e,r,T.toString(),n,P):Ke(T)?($.lastmod&&(r.sitemapLastmodByPage[T.toString()]=$.lastmod),ot(e,r,T.toString(),n,P)):n.reject("sitemap-loc",T.toString(),"not page/sitemap/asset-like",P);}o.trackManifest&&(o.manifest.assets[P]={url:P,outputPath:M.outputPath,isText:!0,contentType:m.headers()["content-type"]??"application/xml",cacheControl:pn(m.headers()),changeToken:null,tokenSource:null,discoveredAssets:[...new Set([...C,...R])],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!1},await Ve(e,o));}catch(m){n.error(`Failed sitemap ${f}`,{url:f,error:String(m)});let h=a&&i.has(f)&&w;if(a&&!h)throw m}}if(a&&i.size>0&&d===0)throw new Error(`None of the configured content-sync sitemap roots could be resolved: ${[...i].join(", ")}`)}async function pc(e,t,r,n,s,o,a){let c=ie(e,n,e.sourceOrigin,s,"asset-fetch");if(!c||!Y(e,c))return;let i=c.toString();if(r.doneAssets.has(i))return;let d=o.enabled||o.strictTargeted?o.previousManifest.assets[i]:void 0,u=o.trackManifest?await mr(e,t,o,i,s):null,l=u?.token??null,g=rn(d?.changeToken,u),p=o.strictTargeted&&(!u?.supported||!u.token)&&nn(e.sourceOrigin,i),f=o.strictTargeted?o.manifest.assets[i]:void 0;if(f?.lastSeenRunId===o.runId&&await Xe(f.outputPath)){r.doneAssets.add(i);for(let m of f.discoveredAssets||[])Z(e,r,m,s,i);ue(a,e,f.outputPath),ft(a,e,f.outputPath,f.cacheControl),f.isText&&ke(a,f.outputPath),s.info(`Resumed completed content-sync asset ${i}`,{url:i,source:"content-sync-checkpoint"}),Ye(r,s);return}if(d&&(g||p)&&await Xe(d.outputPath)){o.manifest.assets[i]={...d,changeToken:l,tokenSource:u?.tokenSource??d.tokenSource,discoveredAssets:[...d.discoveredAssets||[]],lastSeenRunId:o.runId,unreferencedRuns:0},delete o.manifest.pages[i],r.doneAssets.add(i);for(let m of d.discoveredAssets||[])Z(e,r,m,s,i);ft(a,e,d.outputPath,d.cacheControl),s.info(`Reused unchanged trusted asset ${i}.`,{url:i,tokenSource:u?.tokenSource??"unknown",phase:"download-assets",source:g?"asset-change-token-match":"content-sync-release-fingerprint"}),Ye(r,s);return}let w=(m,h={})=>{if(!d||o.strictTargeted)return false;o.manifest.assets[i]={...d,discoveredAssets:[...d.discoveredAssets||[]],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0},delete o.manifest.pages[i],r.doneAssets.add(i);for(let y of d.discoveredAssets||[])Z(e,r,y,s,i);return s.warn(`Keeping previous incremental asset ${i} after fetch issue.`,{url:i,reason:m,preservedFromPreviousManifest:true,...h,phase:"download-assets"}),Ye(r,s),true};s.asset(`Fetching asset ${i}`,{url:i});try{let m=ht(e,r.assetMap,i),h=m?await Xe(m):null;if(m&&h&&!o.strictTargeted)try{let H=await oe(`fetch asset headers ${i}`,6e4,()=>t.head(i,{timeout:6e4})),b=H.headers();if(H.ok()&&tc(h,b)){let W=await ho(m);if(W!==null){let de=H.url()||i,V=xe(de,s,"asset-response",i);if(!V)return;let B=V.toString(),E=await yn(e,r.assetMap,B,W,r,s,b);E.wroteFile&&(ue(a,e,E.outputPath),Oe(E.outputPath)&&ke(a,E.outputPath));let q=pn(b)||d?.cacheControl||null;ft(a,e,E.outputPath,q),r.doneAssets.add(i),s.info(`Reused existing asset ${i} based on response headers.`,{url:i,filePath:m,lastModified:b["last-modified"]||"",contentLength:b["content-length"]||"",phase:"download-assets"}),Ye(r,s);let D=wn(c,b),J=d?.discoveredAssets?[...d.discoveredAssets]:[];if(D||d?.isText){let Q=W.toString("utf8"),pe=wt(e,B,Q,s,"asset:cached");(pe.length>0||!d)&&(J=pe);for(let Pe of J)Z(e,r,Pe,s,B);}if(o.trackManifest){let Q=D||!!d?.isText;o.manifest.assets[i]={url:i,outputPath:E.outputPath,isText:Q,contentType:dn(b)||d?.contentType||null,cacheControl:q,changeToken:u?.supported?u.token:null,tokenSource:u?.supported?u.tokenSource??null:null,discoveredAssets:J,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!Q},delete o.manifest.pages[i],(o.enabled||o.strictTargeted)&&Q&&await Ve(e,o);}return}}}catch{}let y=await oe(`fetch asset ${i}`,6e4,()=>t.get(i,{timeout:6e4}));if(!y.ok()){if(w(`http-${y.status()}`,{status:y.status()}))return;if(o.strictTargeted)throw new Error(`Required content-sync asset returned HTTP ${y.status()}: ${i}`);s.skip("asset",i,y.status());return}let S=await oe(`read asset response ${i}`,6e4,()=>y.body()),P=y.url()||i,M=xe(P,s,"asset-response",i);if(!M)return;let A=M.toString(),C=y.headers(),R=await yn(e,r.assetMap,A,S,r,s,C);R.wroteFile&&(ue(a,e,R.outputPath),Oe(R.outputPath)&&ke(a,R.outputPath));let $=pn(C);ft(a,e,R.outputPath,$),r.doneAssets.add(i),Ye(r,s);let T=wn(c,C),N=[];if(T){let H=S.toString("utf8");N=wt(e,A,H,s,`asset:${k.extname(c.pathname).toLowerCase()||C["content-type"]||"unknown"}`);for(let b of N)Z(e,r,b,s,A);}o.trackManifest&&(o.manifest.assets[i]={url:i,outputPath:R.outputPath,isText:T,contentType:dn(C),cacheControl:$,changeToken:u?.supported?u.token:null,tokenSource:u?.supported?u.tokenSource??null:null,discoveredAssets:N,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!T},delete o.manifest.pages[i],(o.enabled||o.strictTargeted)&&T&&await Ve(e,o));}catch(m){if(w("fetch-error",{error:String(m)}))return;if(s.error(`Failed asset ${i}`,{url:i,error:String(m)}),o.strictTargeted)throw m}finally{r.doneAssets.has(i)||(r.doneAssets.add(i),Ye(r,s));}}async function mc(e,t,r,n,s,o){let a=Math.max(1,Number(e.assetDownloadConcurrency||e.concurrency||1)),c=Array.from({length:a},async()=>{for(;r.assetQueue.length>0;){let i=r.assetQueue.shift();i&&(r.queuedAssets.delete(i),await pc(e,t,r,i,n,s,o));}});await Promise.all(c);}function wr(e,t){let r=ge(e.outputDir,t);if(!r)throw new Error(`Remote export output is outside outputDir: ${t}`);return r}function gc(e,t){return wn(e,{"content-type":t})}function Zs(e){return /(?:^|\/)index\.html?$|\.html?$/i.test(e)}async function fc(e,t,r,n,s,o,a,c,i){let d=Math.max(1,Math.min(100,i)),u=20;for(;r.assetQueue.length>0;){let l=r.assetQueue.splice(0,d*u);for(let w of l)r.queuedAssets.delete(w);await Pn(e,t,s,l,n);let g=[];for(let w of l){let m=s.manifest.assets[w],h=m?ge(e.outputDir,m.outputPath):null,y=h?a.objects[h]:void 0,S=s.changeTokenCache.get(w)??null;if(!(s.enabled&&m&&y?.kind==="asset"&&y.sourceUrl===w&&(rn(m.changeToken,S)||(!S?.supported||!S.token)&&nn(e.sourceOrigin,w)))||!m||!h||!y){g.push(w);continue}s.manifest.assets[w]={...m,changeToken:S?.token??null,tokenSource:S?.tokenSource??m.tokenSource,lastSeenRunId:s.runId,unreferencedRuns:0};for(let M of m.discoveredAssets||[])Z(e,r,M,n,w);r.doneAssets.add(w),ft(o,e,m.outputPath,m.cacheControl),n.info(`Reused unchanged S3-native asset ${w}.`,{url:w,relativePath:h,tokenSource:S?.tokenSource??"release-fingerprint",phase:"download-assets",execution:"lambda"}),Ye(r,n);}if(g.length===0)continue;let p=[];for(let w=0;w<g.length;w+=u)p.push(g.slice(w,w+u));let f=await Promise.all(p.map(w=>c.fetch({urls:w,ignoreHttpsErrors:e.ignoreHttpsErrors,timeoutMs:6e4})));for(let w of f)for(let m of w.assets){let h=m.requestedUrl;try{if(m.outcome!=="fetched"||!m.object||!m.finalUrl){let $=m.error||`Remote asset fetch ended with ${m.outcome}.`;if(n.error(`Failed remote asset ${h}`,{url:h,status:m.httpStatus,outcome:m.outcome,error:$,execution:"lambda"}),s.strictTargeted)throw new Error($);continue}let y=xe(m.finalUrl,n,"remote-asset-response",h);if(!y)continue;let S=Ut(e,y.toString()),P=S.originalFilePath,M=S.originalPublicUrlWithSearch;if(S.preferQueryHashed&&S.hashedFilePath&&S.hashedPublicUrl&&yo(r,S.originalFilePath,m.object.sha256)){let $=po(e,r.assetMap,y.toString());P=$?.outputPath??S.hashedFilePath,M=$?.publicUrl??S.hashedPublicUrl;}hn(e,r.assetMap,y.toString(),M,S.originalPublicUrl),y.toString()!==h&&hn(e,r.assetMap,h,M,S.originalPublicUrl),bo(r,S.originalFilePath,m.object.sha256);let A=wr(e,P),C=m.contentType?.trim()||"application/octet-stream",R=gc(y,C);gt(a,{relativePath:A,sourceUrl:h,kind:"asset",contentType:C,cacheControl:m.cacheControl,isText:R,object:m.object}),ue(o,e,P),R&&ke(o,P),ft(o,e,P,m.cacheControl);for(let $ of m.discoveredAssets)Z(e,r,$,n,y.toString());r.stats.assetsSaved++,s.trackManifest&&(s.manifest.assets[h]={url:h,outputPath:P,isText:R,contentType:C,cacheControl:m.cacheControl,changeToken:null,tokenSource:null,discoveredAssets:[...m.discoveredAssets],lastCrawledAt:new Date().toISOString(),lastSeenRunId:s.runId,unreferencedRuns:0,rewriteComplete:!R},delete s.manifest.pages[h]),n.debug(`Remote asset fetch completed ${h}`,{url:h,finalUrl:y.toString(),relativePath:A,bytes:m.object.bytes,taskId:w.taskId,attempts:w.attempts,execution:"lambda"});}finally{r.doneAssets.add(h),Ye(r,n);}}}}async function hc(e,t,r,n){for(let s of [...t.changedFiles].sort()){if(!Ns(r,s))continue;let o=k.join(e.outputDir,s),a=await F.readFile(o).catch(u=>{throw new Error(`Coordinator-owned output is missing before S3 staging: ${s}`,{cause:u})}),c=Oe(o),i=String(js.lookup(s)||"")||(c?"text/plain; charset=utf-8":"application/octet-stream"),d=await n.writeWorkspaceObject({key:`${r.workspace.prefix}jobs/${r.jobId}/coordinator/${$e(s,40)}-${k.basename(s)}`,body:a,contentType:i,cacheControl:Zs(s)?e.s3.htmlCacheControl:e.s3.assetCacheControl});gt(r,{relativePath:s,sourceUrl:null,kind:"generated",contentType:i,cacheControl:Zs(s)?e.s3.htmlCacheControl:e.s3.assetCacheControl,isText:c,object:d});}}function wc(e){let t=Math.ceil(e/1e3);return `${t} second${t===1?"":"s"}`}function He(e){return Math.max(6e4,e.navigationTimeoutMs*2,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+15e3)}function Ot(e){return Math.max(15e3,Math.min(6e4,He(e)))}function vo(e){return Math.max(15e3,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+1e4)}function vn(e){return e.stats.pagesCompleted}function bn(e){let r=e.options?.browserProcess;return {pid:typeof r?.process?.pid=="number"?r.process.pid:null,kill:typeof r?.kill=="function"?r.kill.bind(r):null}}async function un(e,t,r){let n=bn(e);try{if(n.kill){await n.kill(),t.warn(`Force-killed crawl worker browser after ${r}`,{browserPid:n.pid,reason:r});return}if(n.pid!==null){process.kill(n.pid,"SIGKILL"),t.warn(`Sent SIGKILL to crawl worker browser after ${r}`,{browserPid:n.pid,reason:r});return}t.warn(`Could not resolve crawl worker browser PID after ${r}`,{reason:r});}catch(s){t.error(`Failed to force-kill crawl worker browser after ${r}`,{browserPid:n.pid,reason:r,error:String(s)});}}async function oe(e,t,r){let n;try{return await Promise.race([r(),new Promise((s,o)=>{n=globalThis.setTimeout(()=>{o(new jt(e,t));},t);})])}finally{n!==void 0&&globalThis.clearTimeout(n);}}var jt=class extends Error{constructor(r,n){super(`${r} timed out after ${wc(n)}.`);this.operation=r;this.timeoutMs=n;this.name="OperationTimeoutError";}operation;timeoutMs};async function yc(e,t){let r=vo(e);await t.evaluate(async n=>{await new Promise(s=>{let o=0,a=700,c=Date.now(),i=()=>{window.clearInterval(d),window.scrollTo(0,0),s();},d=window.setInterval(()=>{if(Date.now()-c>=n.maxDurationMs){i();return}window.scrollBy(0,a),o+=a;let u=document.body?.scrollHeight||0,l=document.documentElement?.scrollHeight||0,g=Math.max(u,l);o>=g+window.innerHeight&&i();},120);});},{maxDurationMs:r});}async function bc(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 yc(e,t),await t.waitForTimeout(e.readiness.fallbackWaitMs);}async function Sc(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 Co(e,t){return e.noJavaScriptRenderPathPrefixes.some(r=>Rn(t,r))}async function Pc(e,t,r,n,s=true){let o=new Set;if(s){let c=await t.evaluate(()=>{let i=new Set,d=["href","src","poster","data-src","data-lazy-src","data-original","data-bg","data-background","data-href","data-resource-url"],u=new Set,l=["srcset","data-srcset","data-lazy-srcset"],g=p=>{p.querySelectorAll("*").forEach(f=>{for(let m of d){let h=f.getAttribute(m);h&&i.add(h);}for(let m of l){let h=f.getAttribute(m);h&&h.split(",").forEach(y=>{let S=y.trim().split(/\s+/)[0];S&&i.add(S);});}let w=f.getAttribute("style");w&&u.add(w),f.shadowRoot&&g(f.shadowRoot);});};return g(document),{attrs:[...i],styles:[...u]}});for(let i of c.attrs)for(let d of ic(i)){let u=ie(e,d,r,n,"dom-attr");u&&Y(e,u)&&o.add(u.toString());}for(let i of c.styles)for(let d of wt(e,r,i,n,"dom-style"))o.add(d);}let a=await t.content();for(let c of wt(e,r,a,n,"page-html"))o.add(c);return [...o]}async function Sn(e,t,r,n,s){let o=De(e,t);await _e(o,r,"utf8");let a=await F.stat(o);s?.debug(`Saved page output ${t}`,{phase:"save-pages",url:t,outputPath:o,bytes:a.size});let c=yr(n,o),i=Math.min(n.stats.pagesQueued,vn(n)+1);s&&c&&(n.stats.pagesSaved%10===0||n.stats.pagesSaved===1)&&s.progress(`Page progress: processed ${i}, rendered ${n.stats.pagesRendered}, saved ${n.stats.pagesSaved}, discovered ${n.stats.pagesQueued}.`,{donePages:i,claimedPages:n.donePages.size,pagesRendered:n.stats.pagesRendered,pagesSaved:n.stats.pagesSaved,pagesQueued:n.stats.pagesQueued,pageQueue:n.pageQueue.length,phase:"save-pages"});}function yr(e,t){let r=k.resolve(t);return e.savedPageOutputs.has(r)?false:(e.savedPageOutputs.add(r),e.stats.pagesSaved=e.savedPageOutputs.size,true)}async function br(e,t,r,n,s){let o=ge(e.outputDir,n);if(!o)return;if(s){let c=s.objects[o];c?.kind==="page"&&c.sourceUrl===r&&c.object?.bucket===s.workspace.bucket&&typeof c.object.key=="string"&&c.object.key.startsWith(s.workspace.prefix)&&yr(t,n);return}(await Xe(n).catch(()=>null))?.isFile()&&yr(t,n);}function ko(e){let t=vn(e);return t%5===0||t===1||t===e.stats.pagesQueued}function xo(e,t){let r=vn(e);t.progress(`Render progress: processed ${r}, rendered ${e.stats.pagesRendered}, saved ${e.stats.pagesSaved}, discovered ${e.stats.pagesQueued}.`,{donePages:r,claimedPages:e.donePages.size,pagesRendered:e.stats.pagesRendered,pagesSaved:e.stats.pagesSaved,pagesQueued:e.stats.pagesQueued,phase:"render-pages"});}async function Rc(e){await rs(e),await e.addInitScript(()=>{Object.defineProperty(window,"__WPSUITE_STATIC_EXPORT__",{value:true,writable:false,configurable:true});let t=r=>{if(r==null||String(r)==="")return true;try{let n=new URL(String(r),window.location.href),s=new URL(window.location.href);return n.hash="",s.hash="",n.href===s.href}catch{return false}};try{let r=window.location.assign.bind(window.location),n=window.location.replace.bind(window.location),s=window.location.reload.bind(window.location);Object.defineProperty(window.location,"assign",{configurable:!0,value:o=>{if(t(o)){console.warn("[smartcloud-static-publisher] blocked same-page location.assign",o??"");return}return r(o)}}),Object.defineProperty(window.location,"replace",{configurable:!0,value:o=>{if(t(o)){console.warn("[smartcloud-static-publisher] blocked same-page location.replace",o??"");return}return n(o)}}),Object.defineProperty(window.location,"reload",{configurable:!0,value:()=>{console.warn("[smartcloud-static-publisher] blocked location.reload");}});}catch{}});}async function vc(e,t,r){let n=await e.newContext({viewport:t.viewport,userAgent:"WPSuiteStaticPublisher/0.8 Playwright SitemapOnly",ignoreHTTPSErrors:t.ignoreHttpsErrors,...r?{javaScriptEnabled:false}:{}});return r||await Rc(n),n}async function Cc(e,t,r,n,s,o,a,c,i,d){for(;t.pageQueue.length>0&&!(e.maxPages>0&&t.donePages.size>=e.maxPages);){let u=t.pageQueue.shift();if(!u)break;t.queuedPages.delete(u);try{let l=xe(u,r,"page","page-queue");if(!l)continue;let g=l.toString();if(u=g,t.queuedPages.delete(u),t.donePages.has(u))continue;t.donePages.add(u),o.enabled&&o.seenPages.add(u);let p=Sr(e,u),f=Co(e,l.pathname);if(!Ke(l)||Y(e,l)||st(l)){if(Y(e,l)){r.info(`Remote worker redirected non-page URL to asset queue: ${u}`,{url:u,source:"remote-worker-guard"}),Z(e,t,u,r,"remote-worker-guard");continue}r.reject("page",u,"guard rejected non-page URL before remote rendering","remote-worker");continue}let w=o.strictTargeted?o.manifest.pages[u]:void 0;if(w?.lastSeenRunId===o.runId&&await Xe(w.outputPath)){o.seenPages.add(u);for(let R of w.discoveredAssets||[])Z(e,t,R,r,u);ue(a,e,w.outputPath),ke(a,w.outputPath),await br(e,t,u,w.outputPath,d),r.info(`Resumed completed content-sync page ${u}`,{url:u,source:"content-sync-checkpoint"});continue}let m=await lo(e,s,t,o,u,r);if(o.enabled&&n!=="single-url"&&n!=="content-sync"){let R=o.manifest.pages[u];if(m.action==="reuse"&&R){for(let $ of R.discoveredAssets)Z(e,t,$,r,u);for(let $ of R.discoveredPages)ot(e,t,$,r,u);R.lastSeenRunId=o.runId,R.sitemapLastmod=nt(t.sitemapLastmodByPage[u])??R.sitemapLastmod,m.changeToken?.supported&&(R.changeToken=m.changeToken.token,R.tokenSource=m.changeToken.tokenSource??R.tokenSource),await br(e,t,u,R.outputPath,d),r.info(`Incremental reuse skipped unchanged page ${u}`,{url:u,mode:"incremental",reason:m.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}r.page(`Rendering remotely ${u}`,{url:u,execution:"lambda"});let h=await i.render({sourceOrigin:e.sourceOrigin,url:u,options:{ignoreHttpsErrors:e.ignoreHttpsErrors,javaScriptEnabled:!f,viewport:e.viewport,navigationTimeoutMs:e.navigationTimeoutMs,autoScrollTimeoutMs:vo(e),readiness:e.readiness},downloadHtml:!d}),y=h.page,S=y.finalUrl?xe(y.finalUrl,r,"remote-final-response",u)?.toString():void 0;if(n==="content-sync"&&S&&S!==g){if(o.deleteOnMissingUrls.has(u)){await Ft(e,o,a,u,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive route redirected to ${S}`,{url:u,finalResponseUrl:S,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${u} -> ${S}`)}let P=y.httpStatus;if(p){if(P!==404){r.error(`Generated 404 request path returned unexpected status for ${u}`,{url:u,expectedStatus:404,status:P,requestPath:e.generated404RequestPath,execution:"lambda"});continue}}else if(P!==null&&P>=400){if(n==="content-sync"&&(P===404||P===410)&&o.deleteOnMissingUrls.has(u)){await Ft(e,o,a,u,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive page ${u}`,{url:u,status:P,source:"content-sync-tombstone"});continue}if(n==="content-sync")throw new Error(`Required content-sync route returned HTTP ${P}: ${u}`);r.skip("page",u,P,t.pageDiscoverySources.get(u));continue}t.stats.pagesRendered++;let M=new Set;if(!d){if(h.html===null)throw new Error("Remote render did not return HTML for local output mode.");for(let R of wt(e,g,h.html,r,"remote-page-html"))M.add(R);}for(let R of [...y.discoveredAssets,...y.networkUrls]){let $=ie(e,R,g,r,"remote-render");$&&Y(e,$)&&M.add($.toString());}for(let R of M)Z(e,t,R,r,u);let A=n!=="single-url"&&n!=="content-sync"&&!p?[...new Set([...!d&&h.html!==null?Ro(e,u,h.html,r):[],...y.discoveredPages])]:[];for(let R of A)ot(e,t,R,r,u);for(let R of y.cookies){let $=R.sameSite==="Strict"||R.sameSite==="Lax"||R.sameSite==="None"?R.sameSite:"Unspecified",T=!Number.isFinite(R.expires)||R.expires<=0;c.add({name:R.name,domain:R.domain,path:R.path||"/",expires:T?null:R.expires,session:T,secure:R.secure,httpOnly:R.httpOnly,sameSite:$,partitioned:R.partitioned===!0});}let C=De(e,u);if(d){if(!y.html)throw new Error("Remote render completed without a workspace HTML object.");gt(d,{relativePath:wr(e,C),sourceUrl:u,kind:"page",contentType:y.contentType?.trim()||"text/html; charset=utf-8",cacheControl:e.s3.htmlCacheControl,isText:!0,object:y.html}),yr(t,C);}else {if(h.html===null)throw new Error("Remote render did not return HTML for local output mode.");await Sn(e,u,h.html,t,r);}ue(a,e,C),ke(a,C),o.trackManifest&&(o.manifest.pages[u]={url:u,outputPath:C,contentType:y.contentType?.trim()||null,changeToken:m.changeToken?.supported?m.changeToken.token:null,tokenSource:m.changeToken?.supported?m.changeToken.tokenSource??null:null,sitemapLastmod:nt(t.sitemapLastmodByPage[u])??null,discoveredPages:A,discoveredAssets:[...M],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Ve(e,o)),r.debug(`Remote render completed ${u}`,{url:u,taskId:h.taskId,attempts:h.attempts,execution:"lambda"});}catch(l){if(r.error(`Failed remote page ${u}`,{url:u,error:String(l),execution:"lambda",...l instanceof ye?{status:l.status,attempts:l.attempts}:{}}),l instanceof ye){t.pageHttpFailures.set(u,l);continue}if(o.strictTargeted)throw l}finally{t.stats.pagesCompleted++,ko(t)&&xo(t,r);}}}async function kc(e,t,r,n,s,o,a,c){let i=await chromium.launch({headless:true}),d=bn(i);d.pid!==null&&r.debug(`Launched crawl worker browser PID ${d.pid}`,{browserPid:d.pid});try{for(;t.pageQueue.length>0;){i||(i=await chromium.launch({headless:!0}),d=bn(i),d.pid!==null&&r.debug(`Relaunched crawl worker browser PID ${d.pid}`,{browserPid:d.pid}));let u=i;if(e.maxPages>0&&t.donePages.size>=e.maxPages)break;let l=t.pageQueue.shift();if(!l)break;t.queuedPages.delete(l);let g=null,p=null,f=!1,w=null,m=[],h=new Set;try{let y=xe(l,r,"page","page-queue");if(!y)continue;let S=y.toString();if(l=S,t.queuedPages.delete(l),t.donePages.has(l))continue;t.donePages.add(l),o.enabled&&o.seenPages.add(l);let P=Sr(e,l),M=Co(e,y.pathname);if(!Ke(y)||Y(e,y)||st(y)){if(Y(e,y)){r.info(`Worker redirected non-page URL to asset queue: ${l}`,{url:l,source:"worker-guard"}),Z(e,t,l,r,"worker-guard");continue}r.reject("page",l,"guard rejected non-page URL before rendering","worker");continue}let A=o.strictTargeted?o.manifest.pages[l]:void 0;if(A?.lastSeenRunId===o.runId&&await Xe(A.outputPath)){o.seenPages.add(l);for(let E of A.discoveredAssets||[])Z(e,t,E,r,l);ue(a,e,A.outputPath),ke(a,A.outputPath),await br(e,t,l,A.outputPath),r.info(`Resumed completed content-sync page ${l}`,{url:l,source:"content-sync-checkpoint"});continue}let C=await lo(e,s,t,o,l,r);if(o.enabled&&n!=="single-url"&&n!=="content-sync"){let E=o.manifest.pages[l];if(C.action==="reuse"&&E){for(let q of E.discoveredAssets)Z(e,t,q,r,l);for(let q of E.discoveredPages)ot(e,t,q,r,l);E.lastSeenRunId=o.runId,E.sitemapLastmod=nt(t.sitemapLastmodByPage[l])??E.sitemapLastmod,C.changeToken?.supported&&(E.changeToken=C.changeToken.token,E.tokenSource=C.changeToken.tokenSource??E.tokenSource),await br(e,t,l,E.outputPath),r.info(`Incremental reuse skipped unchanged page ${l}`,{url:l,mode:"incremental",reason:C.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}g=await oe(`create page context ${l}`,He(e),()=>vc(u,e,M)),p=await oe(`create page ${l}`,He(e),()=>g.newPage()),m=await g.cookies();let R=!1,$=new Set;await p.route("**/*",async E=>{let q=E.request();try{let D=ie(e,q.url(),e.sourceOrigin,r,"network-request");D&&Y(e,D)&&($.add(D.toString()),Z(e,t,D.toString(),r,l));}catch{}if(q.isNavigationRequest()&&q.frame()===p.mainFrame())try{let D=new URL(q.url()),J=new URL(S);if(D.hash="",J.hash="",D.href===J.href){if(R){r.warn(`Blocked same-page navigation/reload for ${l}`,{url:l,requestUrl:q.url()});try{await E.abort("aborted");}catch(Q){r.debug(`Ignored canceled route while blocking reload for ${l}`,{url:l,error:String(Q)});}return}R=!0;}}catch{}try{await E.continue();}catch(D){r.debug(`Ignored canceled network route for ${l}`,{url:l,requestUrl:q.url(),error:String(D)});}}),p.on("response",E=>{try{let D=ie(e,E.url(),e.sourceOrigin,r,"network-response");D&&Y(e,D)&&($.add(D.toString()),Z(e,t,D.toString(),r,l));}catch{}let q=E.headerValues("set-cookie").then(D=>{for(let J of D)c.add(Is(J,E.url()));}).catch(()=>{}).then(()=>{});h.add(q),q.finally(()=>h.delete(q));}),r.page(`Rendering ${l}`,{url:l});let T=null,N=null;try{if(T=await p.goto(l,{waitUntil:"domcontentloaded",timeout:e.navigationTimeoutMs}),T&&T.ok())try{N=await oe(`read response body ${l}`,He(e),()=>T.text());}catch{N=null;}}catch(E){let q=t.pageHttpFailures.get(l);if(q){let D=(t.pageHttpAttempts.get(l)??0)+1;throw t.pageHttpAttempts.set(l,D),new ye(l,q.status,D,`Retry navigation failed: ${String(E)}`)}r.warn(`Navigation issue for ${l}; saving current DOM if available`,{url:l,error:String(E)});}let H=T?.status();if(rr(H)){let E=(t.pageHttpAttempts.get(l)??0)+1;throw t.pageHttpAttempts.set(l,E),new ye(l,H,E)}if(T)t.pageHttpFailures.delete(l);else if(t.pageHttpFailures.has(l)){let E=t.pageHttpFailures.get(l),q=(t.pageHttpAttempts.get(l)??0)+1;throw t.pageHttpAttempts.set(l,q),new ye(l,E.status,q,"Retry navigation returned no HTTP response.")}if(n==="content-sync"&&T){let E=xe(T.url(),r,"content-sync-final-response",l)?.toString();if(E&&E!==S){if(o.deleteOnMissingUrls.has(l)){await Ft(e,o,a,l,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive route redirected to ${E}`,{url:l,finalResponseUrl:E,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${l} -> ${E}`)}}if(P){let E=T?.status()??null;if(E!==404){r.error(`Generated 404 request path returned unexpected status for ${l}`,{url:l,expectedStatus:404,status:E,requestPath:e.generated404RequestPath});continue}r.info(`Capturing rendered 404 page for ${l}`,{url:l,status:E,outputPath:De(e,l)});}else if(T&&!T.ok()){if(n==="content-sync"&&(T.status()===404||T.status()===410)&&o.deleteOnMissingUrls.has(l)){await Ft(e,o,a,l,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive page ${l}`,{url:l,status:T.status(),source:"content-sync-tombstone"}),await p.close();continue}if(n==="content-sync")throw new Error(`Required content-sync route returned HTTP ${T.status()}: ${l}`);r.skip("page",l,T.status(),t.pageDiscoverySources.get(l)),await p.close();continue}M?(r.info(`Rendering without JS execution for ${l}`,{url:l,mode:"no-js"}),await oe(`wait for rendered page ${l}`,He(e),()=>Sc(e,p))):await oe(`wait for rendered page ${l}`,He(e),()=>bc(e,p)),t.stats.pagesRendered++;let b=await oe(`extract rendered assets ${l}`,He(e),()=>Pc(e,p,S,r,!M));for(let E of b)Z(e,t,E,r,l);let W=[...new Set([...b,...$])],de=N??await oe(`capture rendered html ${l}`,He(e),()=>Kr(p)),V=n!=="single-url"&&n!=="content-sync"&&!P?Ro(e,l,de,r):[];if(n!=="single-url"&&n!=="content-sync"&&!P)for(let E of V)ot(e,t,E,r,l);await Sn(e,l,de,t,r);let B=De(e,l);if(ue(a,e,B),ke(a,B),o.trackManifest){let E=T?.headers();o.manifest.pages[l]={url:l,outputPath:B,contentType:dn(E),changeToken:C.changeToken?.supported?C.changeToken.token:null,tokenSource:C.changeToken?.supported?C.changeToken.tokenSource??null:null,sitemapLastmod:nt(t.sitemapLastmodByPage[l])??null,discoveredPages:V,discoveredAssets:W,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Ve(e,o);}}catch(y){if(y instanceof ye){t.pageHttpFailures.set(l,y),y.attempts<an?(w=nr(y.attempts),r.warn(`Retrying page after HTTP ${y.status}: ${l}`,{url:l,status:y.status,attempt:y.attempts,maxAttempts:an,delayMs:w,reason:y.message,execution:"local"})):(t.pageHttpFailures.set(l,y),r.error(`Failed page after HTTP retries ${l}`,{url:l,status:y.status,attempts:y.attempts,error:String(y),execution:"local"}));continue}if(f=y instanceof jt,o.strictTargeted)throw r.error(`Failed required content-sync page ${l}`,{url:l,error:String(y)}),y;if(f)r.warn(`Skipped partial DOM for timed-out page ${l}`,{url:l,error:String(y),recovery:"recycle-worker-browser"});else try{let S=p?await oe(`capture partial html ${l}`,He(e),()=>Kr(p)):"";if(S&&S.trim()){await Sn(e,l,S,t,r);let P=De(e,l);if(ue(a,e,P),ke(a,P),o.trackManifest){let M=o.manifest.pages[l]??o.previousManifest.pages[l];o.manifest.pages[l]={url:l,outputPath:P,contentType:M?.contentType??null,changeToken:null,tokenSource:null,sitemapLastmod:nt(t.sitemapLastmodByPage[l])??M?.sitemapLastmod??null,discoveredPages:M?.discoveredPages?[...M.discoveredPages]:[],discoveredAssets:M?.discoveredAssets?[...M.discoveredAssets]:[],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Ve(e,o);}r.warn(`Saved partial DOM for ${l}`,{url:l,error:String(y)});}}catch(S){S instanceof jt&&(f=!0),r.error(`Could not save partial DOM for ${l}`,{url:l,error:String(S)});}r.error(`Failed page ${l}`,{url:l,error:String(y)});}finally{if(!f&&p&&await oe(`drain network routes ${l}`,Ot(e),()=>p.unrouteAll({behavior:"wait"})).catch(y=>{f=!0,r.warn(`Could not drain network routes for ${l}`,{url:l,browserPid:d.pid,error:String(y),recovery:"recycle-worker-browser"});}),f&&(i=null,await un(u,r,`page operation timeout for ${l}`),await u.close().catch(()=>{})),h.size>0&&await Promise.allSettled([...h]),!f&&g)try{let y=await g.cookies();c.addAll(Ts(m,y));}catch{}w===null&&(t.stats.pagesCompleted++,ko(t)&&xo(t,r)),!f&&p&&await oe(`close page ${l}`,Ot(e),()=>p.close()).catch(y=>{f=!0,r.warn(`Timed out while closing page ${l}`,{url:l,browserPid:d.pid,error:String(y),recovery:"recycle-worker-browser"});}),!f&&g&&await oe(`close page context ${l}`,Ot(e),()=>g.close()).catch(y=>{f=!0,r.warn(`Timed out while closing page context for ${l}`,{url:l,browserPid:d.pid,error:String(y),recovery:"recycle-worker-browser"});}),f&&i===u&&(i=null,await un(u,r,`page cleanup timeout for ${l}`),await u.close().catch(()=>{})),w!==null&&(await setTimeout$1(w),t.donePages.delete(l),t.queuedPages.add(l),t.pageQueue.push(l));}}}finally{if(i){let u=i;await oe("close worker browser",Ot(e),()=>u.close()).catch(l=>(r.warn("Timed out while closing crawl worker browser",{browserPid:d.pid,error:String(l)}),un(u,r,"browser.close timeout")));}}}async function xc(e){try{let t=await F.readFile(e),n=(e.endsWith(".gz")?gunzipSync(t):t).toString("utf8").trim();if(!n)return [];try{let s=JSON.parse(n);if(Array.isArray(s))return s;if(s&&typeof s=="object")return [s]}catch{return n.split(/\r?\n/).map(s=>s.trim()).filter(Boolean).map(s=>{try{let o=JSON.parse(s);return o&&typeof o=="object"?o:null}catch{return null}}).filter(s=>s!==null)}return []}catch{return []}}function Ys(e){let t=String(e||"").toLowerCase();return t.includes("timeout")||t.includes("timed out")||t.includes("navigation timeout")}function Ic(e){let t=String(e?.job?.command||"").trim(),r=String(e?.job?.crawlMode||"full").trim();return t!=="publish"&&t!=="crawl"?false:r!=="incremental"}function Tc(e){let t=[e?.archivedAt,e?.job?.endedAt,e?.job?.startedAt];for(let r of t){let n=Date.parse(String(r||""));if(Number.isFinite(n))return n}return 0}async function Io(e){try{let t=await F.readFile(e,"utf8"),r=JSON.parse(t);return r&&typeof r=="object"&&!Array.isArray(r)?r:null}catch{return null}}function Xs(e){let t=String(e.originalFileName||"").toLowerCase();return t.endsWith("errors.jsonl")?0:t.endsWith("errors.json")?1:2}function Ac(e,t){let r=Array.isArray(t?.artifacts)?t.artifacts.filter(n=>String(n?.role||"").trim()==="errors"&&String(n?.storedFileName||"").trim()!=="").sort((n,s)=>Xs(n)-Xs(s)).map(n=>k.join(e,String(n.storedFileName))):[];return [...new Set([...r,k.join(e,"errors.jsonl"),k.join(e,"errors.json"),k.join(e,"errors.jsonl.gz"),k.join(e,"errors.json.gz")])]}async function eo(e){let t=await Io(k.join(e,"job.json"));for(let r of Ac(e,t)){let n=await xc(r);if(n.length>0)return n}return []}async function Ec(e){let t=k.join(e.logDir,"archive"),r;try{r=await F.readdir(t);}catch{return ""}let n="",s=0;for(let o of r){let a=k.join(t,o);if(!(await F.stat(a).catch(()=>null))?.isDirectory())continue;let i=await Io(k.join(a,"job.json"));if(!Ic(i))continue;let d=Tc(i);d>=s&&(s=d,n=a);}return n}async function $c(e){let t=await Ec(e),r=t!==""?await eo(t):await eo(e.logDir),n=new Set;for(let s of r)if(Ys(s.error)||Ys(s.message)){let o=s.url;typeof o=="string"&&o&&n.add(o);}return [...n]}async function Oc(e){let t=[];for(let r of e)if(r.startsWith("@")){let n=r.slice(1),s=await F.readFile(n,"utf8");t.push(...s.split(/\r?\n/).map(o=>o.trim()).filter(o=>o&&!o.startsWith("#")));}else t.push(r);return t}async function Mc(e){if(!e)throw new Error("Content-sync crawl requires an immutable impact plan.");let t=JSON.parse(await F.readFile(e,"utf8"));return vt(t),t}function jc(e,t){let r;try{r=new URL(e);}catch{return false}return t.archiveFamilies.some(n=>{let s=new URL(n.url);if(r.origin!==s.origin)return false;if(r.toString()===s.toString())return true;if(r.searchParams.has("paged")&&r.pathname===s.pathname)return /^\d+$/.test(r.searchParams.get("paged")||"");let o=s.pathname.replace(/\/+$/,""),a=r.pathname.slice(o.length).replace(/^\/+/,""),c=String(n.paginationBase||"page").replace(/^\/+|\/+$/g,"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return r.pathname.startsWith(`${o}/`)&&new RegExp(`^${c}/\\d+/?$`).test(a)})}function $t(e,t,r){let n=r==="page"?De(e,t):Ut(e,t).originalFilePath;return ge(e.outputDir,n)}async function Ft(e,t,r,n,s){let o=t.manifest.pages[n];if(!o)return;let a=ge(e.outputDir,o.outputPath);if(!a)throw new Error(`Manifest-owned page path escaped output root: ${n}`);let c=k.resolve(o.outputPath);Object.entries(t.manifest.pages).some(([d,u])=>d!==n&&!s.has(d)&&k.resolve(u.outputPath)===c)||(await F.rm(o.outputPath,{force:true}),r.deletedFiles.add(a),r.revalidateFiles.delete(a)),delete t.manifest.pages[n];}async function Fc(e,t,r,n,s){let o=t.manifest.assets[n];if(!o)return;let a=ge(e.outputDir,o.outputPath);if(!a)throw new Error(`Manifest-owned resource path escaped output root: ${n}`);let c=k.resolve(o.outputPath);Object.entries(t.manifest.assets).some(([d,u])=>d!==n&&!s.has(d)&&k.resolve(u.outputPath)===c)||(await F.rm(o.outputPath,{force:true}),r.deletedFiles.add(a)),delete t.manifest.assets[n];}function Dc(e,t){let r=new Set,n=[...t];for(;n.length>0;){let s=n.shift();if(r.has(s))continue;r.add(s);let o=e.assets[s];for(let a of o?.discoveredAssets??[])try{yt(new URL(a))&&n.push(a);}catch{}}return r}async function Uc(e,t,r,n,s){let o=Dc(r.previousManifest,s);for(let a of o){if(t.successfulSitemaps.has(a))continue;let c=r.previousManifest.assets[a];if(!c)continue;let i=ge(e.outputDir,c.outputPath);if(!i)throw new Error(`Manifest-owned sitemap path escaped output root: ${a}`);await F.rm(c.outputPath,{force:true}),n.deletedFiles.add(i),delete r.manifest.assets[a];}await Ve(e,r);}async function Lc(e){try{let t=await F.readFile(k.join(e.outputDir,"asset-map.json"),"utf8");return JSON.parse(t)}catch{return {}}}async function Wc(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 F.mkdir(e,{recursive:true}),await Promise.all(t.map(async r=>{try{await F.unlink(k.join(e,r));}catch(n){if(n.code!=="ENOENT")throw n}}));}function to(e,t,r,n,s){let o=ie(e,r,e.sourceOrigin,n,"manual-url");o&&(yt(o)?hr(e,t,o.toString(),n,s):Y(e,o)?Z(e,t,o.toString(),n,s):Ke(o)?ot(e,t,o.toString(),n,s):n.reject("manual-url",o.toString(),"not page/sitemap/asset-like",s));}async function To(e=process.argv.slice(2)){let t=[],r=await Te(void 0,{onRemotePublisherConfigDiagnostic:v=>{t.push(v);}}),n=kt(r.sitemapPaths),s=t.at(-1)??null,o=Pa(e),a=o.mode==="content-sync"?await Mc(o.contentSyncPlanPath):null;if(o.resumeRewrite&&o.mode!=="full")throw new Error("--resume-rewrite is only supported for full crawl/publish jobs.");let c=[];o.mode==="retry-timeouts"&&(c=await $c(r)),o.mode==="single-url"&&(c=await Oc(o.urls)),a&&(c=[...a.directRenderUrls,...a.archiveFamilyUrls,...a.resourceRenderUrls]);let i=o.crawlMode==="incremental"&&o.mode==="full",d=i&&typeof r.subscriptionType=="string"&&r.subscriptionType.trim()!=="";if(a&&r.subscriptionType!=="PROFESSIONAL"&&r.subscriptionType!=="AGENCY")throw new Error("Content-sync requires an active Professional or Agency subscription.");let u=(o.mode==="full"||o.mode==="single-url"||o.mode==="content-sync")&&!o.resumeRewrite,l=o.mode!=="full"||o.resumeRewrite||d,g=l?await Lc(r):{},p=d||o.mode==="single-url"||o.mode==="content-sync"?await Aa(r):io(),f=new Set(c),w=new Set;if(a){for(let v of a.archiveFamilies)if(v.foundPosts===0)for(let j of v.pageUrls)w.add(j);for(let v of Object.keys(p.pages))jc(v,a)&&!f.has(v)&&(c.push(v),w.add(v));}let m={enabled:d,trackManifest:u,manifest:gn(p),previousManifest:gn(p),runId:a?.jobId??`${Date.now()}`,seenPages:new Set,changeTokenCache:new Map,pageChangeTokenPrimePromise:null,manifestSaveChain:Promise.resolve(),manifestPendingChanges:0,lastManifestSavedAt:Date.now(),strictTargeted:!!a,requiredUrls:f,deleteOnMissingUrls:w};await Wc(r.logDir);let h=new Zt(r.logDir,r.logLevel),y=o.remoteRender??r.remoteWorkers?.render?.enabled===true,S=cr({explicitPath:o.remoteWorkerConfigPath,runtimeDir:Dt()}),P=null,M=null,A=null,C=null,R=Math.max(1,r.concurrency),$=1,T=r.remoteWorkers?.rewrite?.enabled===true,N=r.remoteWorkers?.deploy?.enabled===true,H=y&&T&&N&&!o.resumeRewrite;if(!H&&o.mode==="full"&&!o.resumeRewrite&&await F.rm(lr(r),{force:true}),y&&!o.resumeRewrite||T){if(!S)throw new Error("Remote Lambda workers are enabled, but the runtime directory is unknown. Run through queue-runner with --runtime-dir, set STATIC_PUBLISHER_RUNTIME_DIR, or use --remote-worker-config for a diagnostic run.");let v=await tr(S),j=false;if(A=new mt(v,{},{onProgress:O=>{if(O.operation==="rewrite"){h.debug("Lambda rewrite task progress.",{remoteWorkerOperation:O.operation,remoteWorkerTaskId:O.taskId,remoteWorkerStatus:O.status,remoteWorkerCompletedItems:O.completedItems,remoteWorkerFailedItems:O.failedItems,remoteWorkerTotalItems:O.totalItems,remoteWorkerUpdatedAt:O.updatedAt});return}h.progress(`Lambda ${O.operation} batch: ${O.completedItems+O.failedItems}/${O.totalItems}`,{remoteWorkerOperation:O.operation,remoteWorkerTaskId:O.taskId,remoteWorkerStatus:O.status,remoteWorkerCompletedItems:O.completedItems,remoteWorkerFailedItems:O.failedItems,remoteWorkerTotalItems:O.totalItems,remoteWorkerUpdatedAt:O.updatedAt});},onProgressError:O=>{j||(j=true,h.warn("Lambda progress status could not be read; task execution continues.",{error:O.message}));}}),H){if(!v.functions.asset)throw new Error("Full Lambda delegation requires an asset worker. Redeploy the Lambda worker stack and reinstall remote-workers.json.");let O=l?await dr(r):null;C=_s(`crawl-${$e(m.runId,40)}`,v,O),M=new or({client:A,jobId:C.jobId,maxAttempts:2}),h.info("S3-native Lambda data plane is enabled.",{execution:"lambda",assetWorker:v.functions.asset,workspaceBucket:v.workspace.bucket,workspacePrefix:v.workspace.prefix});}}if(y&&!o.resumeRewrite){if(!A)throw new Error("Remote render worker client was not initialized.");R=Math.max(1,Math.min(100,H?r.concurrency:r.remoteWorkers?.render?.concurrency??r.concurrency)),$=Math.max(1,Math.min(20,r.remoteWorkers?.render?.batchSize??5)),P=new sr({client:A,jobId:`crawl-${$e(m.runId,40)}`,maxAttempts:r.remoteWorkers?.render?.maxAttempts??2,batchSize:$,maxConcurrentBatches:R,onRetry:v=>{h.warn(`Retrying remote render for ${v.url}`,{url:v.url,attempt:v.attempt,maxAttempts:v.maxAttempts,reason:v.reason,status:v.status,delayMs:v.delayMs,execution:"lambda"});}}),h.info("Lambda page rendering is enabled.",{remoteWorkerConfigPath:k.resolve(S),concurrency:R,maxAttempts:r.remoteWorkers?.render?.maxAttempts??2,batchSize:$,execution:"lambda"});}if(s){let v={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.",v):h.warn("Remote WP Suite publisher configuration is unavailable or has no active subscription type.",v);}if(i&&!d){let v="Incremental crawl requires an active remote publisher subscription; refusing to fall back to a destructive full crawl.";throw h.error(v,{requestedMode:o.crawlMode,subscriptionType:r.subscriptionType??null,source:"incremental-subscription-guard"}),await h.flush(),new Error(v)}let b={pageQueue:[],queuedPages:new Set,donePages:new Set,savedPageOutputs:new Set,pageHttpAttempts:new Map,pageHttpFailures:new Map,pageDiscoverySources:new Map,assetQueue:[],queuedAssets:new Set,doneAssets:new Set,sitemapQueue:[],queuedSitemaps:new Set,doneSitemaps:new Set,successfulSitemaps:new Set,resolvedSitemapRoots:new Set,sitemapIndexUrls:new Set,sitemapLeafUrls:new Set,sitemapLastmodByPage:{},assetMap:{...g},assetVariantDigestsByOriginalPath:new Map,stats:{pagesQueued:0,pagesRendered:0,pagesCompleted:0,assetsQueued:0,sitemapsQueued:0,assetsSaved:0,pagesSaved:0}},W=Ta(),de=new Xt,V=Ra();if(V&&!o.resumeRewrite&&await er(V,de.artifact()),await Ge(r),await Gr(r,{startedAt:new Date().toISOString(),outputDir:k.resolve(r.outputDir)}),!o.resumeRewrite&&r.pageCache?.purgeBeforeCrawl===true){h.mark("purge-page-cache");let v=await request.newContext({ignoreHTTPSErrors:r.ignoreHttpsErrors});try{let j=await Ws(r,v);h.info("WordPress page cache was purged before crawl discovery.",{provider:j.provider,scope:j.scope,purgedAt:j.purgedAt??null}),h.endMark("purge-page-cache",{provider:j.provider,scope:j.scope});}catch(j){throw h.error("WordPress page-cache purge failed; crawl stopped before changing export output.",{error:j instanceof Error?j.message:String(j)}),await h.flush(),j}finally{await v.dispose();}}if(l||await F.rm(r.outputDir,{recursive:true,force:true}),await F.mkdir(r.outputDir,{recursive:true}),l||await Gr(r,{startedAt:new Date().toISOString(),outputDir:k.resolve(r.outputDir)}),a){let v=new Set([...a.deleteUrls,...m.deleteOnMissingUrls]);for(let O of a.deleteUrls)await Ft(r,m,W,O,v);let j=new Set(a.resourceDeleteUrls);for(let O of a.resourceDeleteUrls)await Fc(r,m,W,O,j);}if(o.resumeRewrite)h.info("Resuming final rewrite from existing output.",{phase:"rewrite-text",mode:o.mode,crawlMode:o.crawlMode});else {let v=await request.newContext({ignoreHTTPSErrors:r.ignoreHttpsErrors});try{if(h.mark("discovery"),o.mode==="full"?(n.forEach(O=>hr(r,b,O,h)),await Gs(r,v,b,h,W,m),r.seedPaths.forEach(O=>to(r,b,O,h,"seed-path")),r.generated404RequestPath&&ot(r,b,r.generated404RequestPath,h,"generated-404-request-path")):(c.forEach(O=>to(r,b,O,h,"cli")),a?.sitemapUrls.forEach(O=>hr(r,b,O,h,"content-sync-impact")),await Gs(r,v,b,h,W,m,o.mode==="content-sync",a?.sitemapUrls??[]),a&&await Uc(r,b,m,W,a.sitemapUrls)),h.endMark("discovery",{pages:b.pageQueue.length,assets:b.assetQueue.length,sitemaps:b.sitemapQueue.length}),h.summary(`Queued ${b.pageQueue.length} pages, ${b.assetQueue.length} assets, ${b.sitemapQueue.length} sitemaps.`,{mode:o.mode,crawlMode:o.crawlMode,queuedPages:b.pageQueue.length,queuedAssets:b.assetQueue.length,queuedSitemaps:b.sitemapQueue.length}),o.mode==="retry-timeouts"&&b.pageQueue.length===0&&b.assetQueue.length===0&&b.sitemapQueue.length===0){h.summary("No timed-out URLs were queued. Skipping retry crawl.",{mode:o.mode,crawlMode:o.crawlMode,queuedPages:0,queuedAssets:0,queuedSitemaps:0}),await h.flush(),V&&await er(V,de.artifact());return}h.mark("render-pages");let j=Array.from({length:P?R*$:R},()=>P?Cc(r,b,h,o.mode,v,m,W,de,P,C??void 0):kc(r,b,h,o.mode,v,m,W,de));if(await Promise.all(j),b.pageHttpFailures.size>0&&await h.flush(),Ms(b.pageHttpFailures),V&&await er(V,de.artifact()),h.endMark("render-pages",{pages:b.stats.pagesCompleted}),h.mark("download-assets"),h.progress("Asset download phase started.",{phase:"download-assets",doneAssets:b.doneAssets.size,assetsQueued:b.stats.assetsQueued,assetQueue:b.assetQueue.length}),C&&M?await fc(r,v,b,h,m,W,C,M,r.concurrency):(await Pn(r,v,m,[...b.assetQueue],h),await mc(r,v,b,h,m,W)),h.endMark("download-assets",{assets:b.doneAssets.size}),m.enabled&&r.maxPages===0&&(await Da(r,b,m,h,W),o.mode==="full"&&await Ua(r,m,h,W)),o.mode==="full"){let O=await dc(r,b,h);for(let K of O)if(ue(W,r,K),C&&A){let G=wr(r,K),I=await F.readFile(K),U=Oe(K),x=String(js.lookup(G)||"")||(U?"text/plain; charset=utf-8":"application/octet-stream"),te=await A.writeWorkspaceObject({key:`${C.workspace.prefix}jobs/${C.jobId}/copied/${$e(G,40)}-${k.basename(G)}`,body:I,contentType:x});gt(C,{relativePath:G,sourceUrl:null,kind:"generated",contentType:te.contentType,cacheControl:null,isText:U,object:te}),U&&ke(W,K);}}}finally{await oe("dispose crawl request context",Ot(r),()=>v.dispose()).catch(j=>{h.warn("Timed out while disposing crawl request context",{error:String(j)});});}}if(m.trackManifest&&await Ve(r,m,true),C)for(let v of W.deletedFiles)Bs(C,v);let B=JSON.stringify(b.assetMap,null,2);if(await F.writeFile(k.join(r.outputDir,"asset-map.json"),B,"utf8"),C&&A){let v=await A.writeWorkspaceObject({key:`${C.workspace.prefix}jobs/${C.jobId}/generated/asset-map.json`,body:B,contentType:"application/json; charset=utf-8"});gt(C,{relativePath:"asset-map.json",sourceUrl:null,kind:"generated",contentType:"application/json; charset=utf-8",cacheControl:r.s3.assetCacheControl,isText:true,object:v});}JSON.stringify(g,null,2)!==JSON.stringify(b.assetMap,null,2)&&ue(W,r,k.join(r.outputDir,"asset-map.json")),C&&A&&await hc(r,W,C,A);let E=o.mode==="single-url"||o.mode==="content-sync"?Va(r,b):m.enabled&&!o.resumeRewrite?La(r,m,W,g,b.assetMap):void 0;h.mark("rewrite-text");let q=o.mode==="single-url"||o.mode==="content-sync"?`Rewriting text files touched by URL crawl: 0/${E?.length??0}`:"Rewriting text files...";h.progress(q,{phase:"rewrite-text",index:0,totalFiles:E?.length,changedTextFiles:0}),await Et(q);let D=Date.now(),J={files:E,previousAssetMap:g,onStart:({totalFiles:v,workerCount:j})=>{h.info(`Rewrite phase scanning ${v} text files.`,{phase:"rewrite-text",totalFiles:v,workerCount:j});},onProgress:async({index:v,totalFiles:j,changedTextFiles:O,file:K,changed:G})=>{G&&ue(W,r,k.join(r.outputDir,K)),h.checkpoint(`Rewriting text file ${v}/${j}`,{phase:"rewrite-text",index:v,totalFiles:j,changedTextFiles:O,file:K}),h.debug(`${G?"Rewrote":"Checked unchanged"} text file ${v}/${j}: ${K}`,{phase:"rewrite-text",index:v,totalFiles:j,changedTextFiles:O,file:K,changed:G});let I=Date.now();if(!(v===1||v===j||v%50===0||I-D>=5e3))return;D=I;let x=`Rewriting text files: ${v}/${j}`;h.progress(x,{phase:"rewrite-text",index:v,totalFiles:j,changedTextFiles:O,file:K}),await Et(x);}},Q;if(C&&A){let v=E?.map(O=>wr(r,k.isAbsolute(O)?O:k.join(r.outputDir,O))),j=Object.values(C.objects).filter(O=>O.isText&&(!v||v.includes(O.relativePath))).length;await J.onStart?.({totalFiles:j,workerCount:Math.min(r.concurrency,j)}),Q=await ar(r,C,b.assetMap,{client:A,jobId:C.jobId,concurrency:r.concurrency,batchSize:25,maxAttempts:2,...v?{relativePaths:v}:{},onProgress:J.onProgress,onBatchProgress:async({completedItems:O,totalItems:K,completedBatches:G,totalBatches:I,changedTextFiles:U})=>{let x=`Lambda rewrite: ${O}/${K} files; ${G}/${I} batches completed`;h.progress(x,{phase:"rewrite-text",execution:"lambda",index:O,totalFiles:K,completedBatches:G,totalBatches:I,changedTextFiles:U}),await Et(x);}}),await Hs(r,C);}else T&&A?Q=await ir(r,b.assetMap,{client:A,jobId:`crawl-${$e(m.runId,40)}`,concurrency:r.remoteWorkers?.rewrite?.concurrency??r.rewriteConcurrency,batchSize:r.remoteWorkers?.rewrite?.batchSize??25,maxAttempts:r.remoteWorkers?.rewrite?.maxAttempts??2,onStageProgress:async({index:v,totalFiles:j,file:O})=>{let K=Date.now();if(v!==1&&v!==j&&v%25!==0&&K-D<5e3)return;D=K;let G=`Staging text files for Lambda rewrite: ${v}/${j}`;h.progress(G,{phase:"rewrite-stage",index:v,totalFiles:j,file:O,execution:"lambda"}),await Et(G);},onBatchProgress:async({completedItems:v,totalItems:j,completedBatches:O,totalBatches:K,changedTextFiles:G})=>{let I=`Lambda rewrite: ${v}/${j} files; ${O}/${K} batches completed`;h.progress(I,{phase:"rewrite-text",execution:"lambda",index:v,totalFiles:j,completedBatches:O,totalBatches:K,changedTextFiles:G}),await Et(I);},options:J}):Q=await Jt(r,b.assetMap,J);m.trackManifest&&(Wa(m,E),await Ve(r,m,true)),h.endMark("rewrite-text",{changedTextFiles:Q});let pe=a?[...W.deletedFiles].sort():m.enabled&&!o.resumeRewrite?await _a(r,b,m,W,h,C):[],Pe=[...new Set((E||[]).map(v=>ge(r.outputDir,k.isAbsolute(v)?v:k.join(r.outputDir,v))).filter(v=>v!==null))].sort();if(a){let v=[...W.changedFiles].sort(),j=new Set(v),O=new Set(a.directRenderUrls.map(L=>$t(r,L,"page")).filter(L=>!!L&&j.has(L))),K=new Set(a.archiveFamilyUrls.map(L=>$t(r,L,"page")).filter(L=>!!L&&j.has(L))),G=new Set(a.resourceRenderUrls.map(L=>$t(r,L,"asset")).filter(L=>!!L&&j.has(L))),I=new Set([...b.sitemapLeafUrls].map(L=>$t(r,L,"asset")).filter(L=>!!L&&j.has(L))),U=new Set([...b.sitemapIndexUrls].map(L=>$t(r,L,"asset")).filter(L=>!!L&&j.has(L))),x=new Set([...O,...K,...G,...I,...U]),te=v.filter(L=>!x.has(L)),le=[...new Set([...a.directRenderUrls,...a.archiveFamilyUrls,...b.resolvedSitemapRoots,...b.sitemapLeafUrls,...b.sitemapIndexUrls,...a.deleteUrls,...a.resourceRenderUrls,...a.resourceDeleteUrls].map(L=>{let Re=new URL(L);return `${Re.pathname}${Re.search}`}))].sort();await Qr(r,{schemaVersion:2,generatedAt:new Date().toISOString(),outputDir:k.resolve(r.outputDir),runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,changedFiles:v,deletedFiles:pe,rewriteTargets:Pe,revalidateFiles:[...W.revalidateFiles].sort(),consumerId:a.consumerId,fromSequence:a.fromSequence,toSequence:a.toSequence,impactHash:a.impactHash,resolvedSitemapUrls:[...b.resolvedSitemapRoots],uploadGroups:{assets:te,content:[...O].sort(),listings:[...K].sort(),resources:[...G].sort(),sitemapLeaves:[...I].sort(),sitemapIndexes:[...U].reverse()},invalidationPaths:le});}else await Qr(r,{schemaVersion:1,generatedAt:new Date().toISOString(),outputDir:k.resolve(r.outputDir),runMode:o.mode,crawlMode:o.crawlMode,fullSyncRequired:!(m.enabled&&!o.resumeRewrite),changedFiles:m.enabled&&!o.resumeRewrite?[...W.changedFiles].sort():[],deletedFiles:m.enabled&&!o.resumeRewrite?pe:[],rewriteTargets:m.enabled&&!o.resumeRewrite?Pe:[],revalidateFiles:[...W.revalidateFiles].sort()});await as(r),h.summary(o.resumeRewrite?`Done. Resumed final rewrite over existing output and changed ${Q} text files.`:`Done. Rendered ${b.stats.pagesRendered} pages, processed ${b.doneSitemaps.size} sitemaps and ${b.doneAssets.size} assets.`,{mode:o.mode,crawlMode:o.crawlMode,resumeRewrite:o.resumeRewrite,...b.stats,pagesRendered:b.stats.pagesRendered,donePages:b.stats.pagesCompleted,claimedPages:b.donePages.size,doneSitemaps:b.doneSitemaps.size,doneAssets:b.doneAssets.size,changedTextFiles:Q}),await h.flush();}function qc(e){let t=e/6e4;if(Number.isInteger(t)&&t>=1)return `${t} minute${t===1?"":"s"}`;let r=Math.ceil(e/1e3);return `${r} second${r===1?"":"s"}`}async function be(e,t,r){let n=new AbortController,s=globalThis.setTimeout(()=>{n.abort(new Error(`${e} timed out after ${qc(t)}.`));},t);try{return await r({abortController:n,httpOptions:{abortSignal:n.signal,requestTimeout:t}})}finally{globalThis.clearTimeout(s);}}function _c(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Pr(e){let t="";for(let n=0;n<e.length;n++){let s=e[n];if(s==="--profile"){let o=(e[n+1]||"").trim();if(!o)throw new Error("Missing value for --profile.");t=o,n+=1;}else if(s.startsWith("--profile=")){let o=s.slice(10).trim();if(!o)throw new Error("Missing value for --profile.");t=o;}}let r=String(process.env.PUBLISHER_DEPLOY_PROFILE||process.env.PUBLISHER_DEPLOYMENT_PROFILE||"").trim();return t||r||null}function Nc(e,t){return {...e,...t??{}}}function Bc(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function bt(e,t){let r=String(t??e.deploymentTargetOverride??"").trim();if(!_c(e)){if(r)throw new Error(`Deployment target "${r}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:e}}let n=String(r||e.defaultDeploymentProfile||"").trim();if(!n)return {name:null,profile:null,config:e};let s=e.deploymentProfiles?.[n];if(!s)throw new Error(`Unknown deployment profile "${n}". Check the linked WPSuite publisher configuration.`);return {name:n,profile:s,config:{...e,targetOrigin:s.targetOrigin??e.targetOrigin,s3:Nc(e.s3,s.s3),cloudFront:Bc(e.cloudFront,s.cloudFront)}}}function Cn(e){return String(e??"").split(";").map(t=>t.trim().toLowerCase()).filter(Boolean).join(";")}var Mo="wpsuite-sha256",jo="wpsuite-normalized-sha256",In="public, max-age=0, must-revalidate",Zc=/(?:^|[-_:.])(id|uid|nonce|token|hash|instance)(?:$|[-_:.])/i,Tn=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,Yc=["SIGINT","SIGTERM"],Cr=new Set,Rr=new Map,Ao=null;function Xc(e){Cr.add(e);let t=false;return ()=>{t||(t=true,Cr.delete(e));}}async function el(){let e=[...Cr];e.length!==0&&await Promise.allSettled(e.map(async t=>{Cr.delete(t),await F.rm(t,{recursive:true,force:true});}));}function tl(){if(!(Rr.size>0))for(let e of Yc){let t=()=>{Ao||(Ao=el().finally(()=>{for(let[r,n]of Rr)process.off(r,n);Rr.clear(),process.exit(1);}));};Rr.set(e,t),process.on(e,t);}}tl();function rl(e,t){let r=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(r)return k.resolve(r);let n=t.trim();return n?k.join(k.resolve(n),"crawl-manifest.json"):k.join(k.resolve(e),".crawl-manifest.json")}function et(e){return k.normalize(e).split(k.sep).join("/")}function Eo(e,t){let r=k.relative(k.resolve(e),k.resolve(t));return !r||r.startsWith("..")||k.isAbsolute(r)?null:et(r)}async function nl(e,t,r){let n=rl(e.outputDir,t),s;try{s=await F.readFile(n,"utf8");}catch(u){return u.code==="ENOENT"?(r.warn("Deploy rewrite guard disabled because the crawl manifest is missing.",{manifestPath:n,source:"deploy-guard"}),null):(r.warn("Deploy rewrite guard disabled because the crawl manifest could not be read.",{manifestPath:n,error:u instanceof Error?u.message:String(u),source:"deploy-guard"}),null)}let o;try{o=JSON.parse(s);}catch(u){return r.warn("Deploy rewrite guard disabled because the crawl manifest is invalid JSON.",{manifestPath:n,error:u instanceof Error?u.message:String(u),source:"deploy-guard"}),null}let a=new Map,c=new Map,i=0,d=0;for(let[u,l]of Object.entries(o.pages||{})){let g=String(l?.outputPath||"").trim(),p=g?Eo(e.outputDir,g):null;if(p){let f=typeof l?.contentType=="string"&&l.contentType?l.contentType:null;f&&c.set(p,f);}!g||l?.rewriteComplete===true||p&&(i++,a.has(p)||a.set(p,{kind:"page",sourceUrl:u,outputPath:k.resolve(g),relativePath:p}));}for(let[u,l]of Object.entries(o.assets||{})){let g=String(l?.outputPath||"").trim(),p=l?.isText===true,f=g?Eo(e.outputDir,g):null;if(f){let w=typeof l?.contentType=="string"&&l.contentType?l.contentType:null;w&&c.set(f,w);}!g||!p||l?.rewriteComplete===true||f&&(d++,a.has(f)||a.set(f,{kind:"asset",sourceUrl:u,outputPath:k.resolve(g),relativePath:f}));}return {manifestPath:n,byRelativePath:a,contentTypeByRelativePath:c,pendingPages:i,pendingTextAssets:d}}function sl(e){return e&&(Tn.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 kn(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,r)=>r%2===1?t:sl(t)).join("")}function ol(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,r)=>r%2===1?t:Tn.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 kr(e,t){return Array.isArray(e)?e.map(r=>kr(r,t)):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([r,n])=>[r,kr(n,r)])):typeof e!="string"?e:t&&Zc.test(t)?ol(e):Tn.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)||/^[0-9]{8,}$/.test(e)?kn(e):e}function il(e){return e.replace(/((?:https?:\/\/[^"'\s>]+|\/)(?:wp-content|wp-includes)\/[^"'\s>?]+)\?ver=[A-Za-z0-9._-]+(?=$|[#"'])/gi,"$1")}function al(e,t){let r=t.trim();if((r.startsWith("{")||r.startsWith("[")||r.startsWith('"'))&&e.startsWith("data-"))try{let s=JSON.parse(r);return JSON.stringify(kr(s))}catch{}if(e.startsWith("data-")&&/^[A-Za-z0-9+/=]+$/.test(r))try{let s=Buffer.from(r,"base64").toString("utf8"),o=JSON.parse(s),a=JSON.stringify(kr(o));return Buffer.from(a,"utf8").toString("base64")}catch{}return t}function cl(e,t){let r=e.toLowerCase();return r==="class"?t.split(/(\s+)/).map((n,s)=>s%2===1?n:kn(n)).join(""):r==="id"||r==="for"||r==="aria-controls"||r==="aria-labelledby"||r==="aria-describedby"||r==="aria-owns"||r==="aria-activedescendant"||r==="name"?kn(t):r==="href"||r==="src"?il(t):al(r,t)}function ll(e){return e.replace(/<([a-z][^\s/>]*)([^<>]*?)>/gi,(t,r,n)=>{if(r.startsWith("/"))return t;let s=n.replace(/\s([:@a-zA-Z_][-:.a-zA-Z0-9_]*?)=(['"])([\s\S]*?)\2/g,(o,a,c,i)=>{let d=cl(a,i);return ` ${a}=${c}${d}${c}`});return `<${r}${s}>`})}function St(e){let t=k.extname(e).toLowerCase();return [".html",".htm",".xml",".xsl",".txt"].includes(t)||["robots.txt","sitemap.xml"].includes(k.basename(e))}function vr(e,t){return [e.replace(/^\/+|\/+$/g,""),t.replace(/^\/+/,"")].filter(Boolean).join("/")}async function ul(e,t,r,n){let s=new Map,o,a=0;do{a++;let c=await be(`S3 list objects page ${a} for s3://${t}/${r||""}`,3e5,({httpOptions:i})=>e.send(new ListObjectsV2Command({Bucket:t,Prefix:r||void 0,ContinuationToken:o}),i));for(let i of c.Contents||[]){if(!i.Key)continue;let d=(i.ETag||"").replace(/^"|"$/g,"").toLowerCase(),u=Number(i.Size??0);s.set(i.Key,{etag:d,size:u,cacheControl:void 0,contentType:void 0});}o=c.NextContinuationToken,n.debug(`Listed S3 keys page ${a}: ${s.size} keys so far`);}while(o);return s}async function dl(e,t,r,n){if(n.has(r))return n.get(r)??null;try{let s=await be(`S3 head object ${r}`,3e5,({httpOptions:a})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),a)),o={etag:(s.ETag||"").replace(/^"|"$/g,"").toLowerCase(),size:Number(s.ContentLength??0),cacheControl:s.CacheControl?.trim()||null,contentType:s.ContentType?.trim()||null};return n.set(r,o),o}catch{return n.set(r,null),null}}async function pl(e,t,r,n){if(n.cacheControl!==void 0&&n.contentType!==void 0)return {cacheControl:n.cacheControl,contentType:n.contentType};try{let s=await be(`S3 head cache metadata ${r}`,3e5,({httpOptions:o})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),o));n.cacheControl=s.CacheControl?.trim()||null,n.contentType=s.ContentType?.trim()||null;}catch{n.cacheControl=null,n.contentType=null;}return {cacheControl:n.cacheControl,contentType:n.contentType}}function $o(e){return String(e||"").split(",").map(t=>t.trim().toLowerCase()).filter(Boolean).sort().join(",")}async function ml(e,t,r,n){let s=[...new Set(Array.from(r).filter(Boolean))];if(s.length===0)return n.info("No explicitly tombstoned S3 objects to delete."),[];n.info(`Deleting ${s.length} explicitly tombstoned S3 object(s)\u2026`);for(let o=0;o<s.length;o+=1e3){let a=s.slice(o,o+1e3);if(a.length===0)continue;let c=await be(`S3 delete objects batch ${Math.floor(o/1e3)+1}`,3e5,({httpOptions:i})=>e.send(new DeleteObjectsCommand({Bucket:t,Delete:{Objects:a.map(d=>({Key:d}))}}),i));if(c.Errors&&c.Errors.length>0){let i=c.Errors.map(d=>`${d.Key||"(unknown)"}: ${d.Code||"DeleteFailed"}`);throw new Error(`S3 reported ${i.length} object deletion error(s): ${i.join(", ")}`)}n.info(`Deleted batch of ${a.length} object(s).`);for(let i of a)n.debug(` deleted: ${i}`);}return s}function gl(e){return createHash("md5").update(e).digest("hex")}function tt(e){return createHash("sha256").update(e).digest("hex")}function xr(e){return createHash("sha256").update(ll(e.toString("utf8"))).digest("hex")}async function fl(e,t,r,n){if(n.has(r))return n.get(r)??null;try{let a=((await be(`S3 head object metadata ${r}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),c))).Metadata?.[Mo]||"").trim().toLowerCase()||null;return n.set(r,a),a}catch{return n.set(r,null),null}}async function hl(e,t,r,n,s){if(n.has(r))return n.get(r)??null;try{let a=((await be(`S3 head object normalized metadata ${r}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),c))).Metadata?.[jo]||"").trim().toLowerCase();if(a)return n.set(r,a),a}catch{return n.set(r,null),null}if(s.has(r)){let o=s.get(r)??null;return n.set(r,o),o}try{let a=await(await be(`S3 get object ${r}`,3e5,({httpOptions:i})=>e.send(new GetObjectCommand({Bucket:t,Key:r}),i))).Body?.transformToByteArray(),c=a?xr(Buffer.from(a)):null;return s.set(r,c),n.set(r,c),c}catch{return s.set(r,null),n.set(r,null),null}}function wl(e){return e.replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"")||"profile"}function yl(e,t){let r=e,n=Object.entries(t).sort((s,o)=>o[0].length-s[0].length);for(let[s,o]of n)s&&(r=r.split(s).join(o));return r}async function bl(e,t,r,n){let s=k.join(e,"asset-map.json"),o;try{o=await F.readFile(s,"utf8");}catch(i){if(i.code==="ENOENT")return;throw i}let a=JSON.parse(o);if(!a||typeof a!="object")return;let c=Object.fromEntries(Object.entries(a).map(([i,d])=>{let u=String(d??"");return t.urlRewriteMode==="absolute"&&t.targetOrigin&&r.targetOrigin&&t.targetOrigin!==r.targetOrigin&&(u=u.split(t.targetOrigin).join(r.targetOrigin)),u=yl(u,n),[i,u]}));await F.writeFile(s,JSON.stringify(c,null,2),"utf8");}async function Sl(e,t,r,n,s,o,a){let c=e.targetOrigin!==t.targetOrigin,i=Object.keys(n).length>0;if(!r||!c&&!i)return {outputDir:e.outputDir,cleanup:async()=>{}};if(c&&e.urlRewriteMode!=="absolute")throw new Error(`Deployment profile "${r}" changes targetOrigin, but the base crawl output must use urlRewriteMode "absolute" for multi-target redeploys.`);let d=k.join(k.dirname(e.outputDir),`.deploy-profile-${wl(r)}-${Date.now()}`),u=Xc(d);s.info(`Preparing deployment profile "${r}" from existing crawl output to ${d}...`);try{if(await F.rm(d,{recursive:!0,force:!0}),o){await F.mkdir(d,{recursive:!0});for(let w of o){let m=k.join(e.outputDir,w),h=k.join(d,w);await F.mkdir(k.dirname(h),{recursive:!0}),await F.copyFile(m,h);}}else await F.cp(e.outputDir,d,{recursive:!0});let l={...e,sourceOrigin:e.targetOrigin,targetOrigin:t.targetOrigin,extraReplacements:n,outputDir:d},g=1;s.info(`Rewriting text files for deployment profile "${r}"...`);let p={files:o,onStart:({totalFiles:w,workerCount:m})=>{g=m,s.info(`Rewriting ${w} text file(s) for deployment profile "${r}" with ${m} worker(s).`);},onProgress:({index:w,totalFiles:m,changedTextFiles:h,file:y,changed:S})=>{let M=`Rewriting [${w}/${m}] ${y} (${S?"rewrote":"checked"}; ${h} changed so far)`;m<=200||w<=5||w%10===0||w===m?(s.progress(M,{file:y,index:w,totalFiles:m,changed:S,changedTextFiles:h,workerCount:g}),s.info(M)):s.debug(M);}},f=a?await ir(l,{},{...a,options:p}):await Jt(l,{},p);return await bl(d,e,t,n),s.info(`Prepared deployment profile "${r}" in ${d} (${f} rewritten text file(s)).`),{outputDir:d,cleanup:async()=>{u(),await F.rm(d,{recursive:!0,force:!0});}}}catch(l){throw s.error(`Failed to prepare deployment profile "${r}" temporary output.`,{tempDir:d,outputDir:e.outputDir,error:l instanceof Error?l.message:String(l),...l instanceof Error&&l.stack?{stack:l.stack}:{}}),await F.rm(d,{recursive:true,force:true}).catch(()=>{}),u(),await s.flush().catch(()=>{}),l}}function Fo(e,t){let r=s=>s.replace(/^\/+|\/+$/g,""),n=e.targets.find(s=>s.bucketName===t.s3.bucket&&r(s.prefix)===r(t.s3.prefix)&&s.region===t.s3.region);if(!n)throw new Error(`No CDK Lambda deployment target matches s3://${t.s3.bucket}/${t.s3.prefix||""} in ${t.s3.region}. Redeploy the CDK with this target and reinstall remote-workers.json.`);return n}function Pl(e,t){if(!e||typeof e!="object")throw new Error("Remote deploy result must be a JSON object.");let r=e;if(r.schemaVersion!==re||r.response?.operation!=="deploy-copy"||r.response.jobId!==t.jobId||r.response.taskId!==t.taskId||!r.detail||!Array.isArray(r.detail.objects)||!Array.isArray(r.detail.deletedKeys)||!Array.isArray(r.detail.deleteErrors))throw new Error("Remote deploy result has an invalid envelope.");return r}async function Ir(e){let t=null;for(let r=1;r<=Math.max(1,Math.min(5,e.maxAttempts));r+=1){let n=ne(`deploy-${e.batchIndex}-a${r}-${Date.now()}`,"taskId");try{let s=await e.client.invoke({schemaVersion:re,operation:"deploy-copy",jobId:e.jobId,taskId:n,targetId:e.targetId,items:e.items,...e.deleteRelativeKeys?{deleteRelativeKeys:e.deleteRelativeKeys}:{}}),o=Pl(await e.client.readResult(s),s);if(o.response.status==="partial")throw new Error(`Worker reported ${o.response.failedItems} failed item(s).`);return o}catch(s){t=s;}}throw new Error(`Remote deploy task failed after ${e.maxAttempts} attempt(s): ${t instanceof Error?t.message:String(t)}`,{cause:t})}async function Rl(e){let t=Fo(e.remoteConfig,e.config),r=ne(`deploy-${Date.now()}-${tt(Buffer.from(`${t.id}:${e.config.outputDir}`)).slice(0,12)}`,"jobId"),n=Ze(e.remoteConfig.workspace.prefix,"jobs",r,"deploy-inputs");e.log.progress(`Staging export for Lambda deploy: 0/${e.files.length}`,{phase:"deploy-stage",index:0,totalFiles:e.files.length,uploaded:0,skipped:0,failed:0,mode:"lambda-s3-copy"});let s=0,o=[],a=Math.max(1,Math.min(16,e.concurrency*2)),c=0;await Promise.all(Array.from({length:Math.min(a,e.files.length)},async()=>{for(;;){let y=c;if(y>=e.files.length)return;c+=1;let S=e.files[y],P=await F.readFile(k.join(e.config.outputDir,S)),M=e.contentTypeByRelativePath?.get(S)||js.lookup(S)||"application/octet-stream",A=e.revalidateFiles.has(et(S))?In:St(S)?e.config.s3.htmlCacheControl:e.config.s3.assetCacheControl,C=tt(P),R=St(S)?xr(P):void 0,$=`${n}/files/${tt(Buffer.from(S))}-${k.basename(S)}`.replace(/\/+/g,"/");await e.client.writeWorkspaceObject({key:$,body:P,contentType:String(M),cacheControl:A,metadata:{"wpsuite-sha256":C,...R?{"wpsuite-normalized-sha256":R}:{}}}),o[y]={sourceKey:$,targetRelativeKey:S,sha256:C,...R?{normalizedSha256:R}:{},bytes:P.byteLength,contentType:String(M),cacheControl:A},s+=1,(s===e.files.length||s<=5||s%25===0)&&e.log.progress(`Staging export for Lambda deploy: ${s}/${e.files.length}`,{phase:"deploy-stage",index:s,totalFiles:e.files.length,staged:s,mode:"lambda-s3-copy"});}}));let i=[],d=Math.max(1,Math.min(500,e.batchSize));for(let y=0;y<o.length;y+=d)i.push(o.slice(y,y+d));let u=[],l=[],g=[],p=0,f=0,w=0,m=Math.max(1,Math.min(Math.max(1,e.concurrency),i.length));if(e.log.progress(`Lambda deploy: 0/${e.files.length}`,{phase:"deploy-copy",index:0,totalFiles:e.files.length,uploaded:0,skipped:0,failed:0,mode:"lambda-s3-copy",workerCount:m,completedBatches:0,totalBatches:i.length}),await Promise.all(Array.from({length:m},async()=>{for(;;){let y=w;if(y>=i.length)return;w+=1;let S=Date.now(),P=await Ir({client:e.client,jobId:r,targetId:t.id,batchIndex:y+1,items:i[y],maxAttempts:e.maxAttempts}),M=0,A=0,C=0;for(let $ of P.detail.objects)p+=1,$.status==="copied"?(u.push($.targetKey),M+=1):$.status==="skipped"?(l.push($.targetKey),A+=1):(g.push($.targetKey),C+=1),e.log.debug(`Lambda deploy ${$.status}: ${$.targetKey}`,{phase:"deploy-copy",batch:y+1,sourceKey:$.sourceKey,targetKey:$.targetKey,status:$.status,...$.error?{error:$.error}:{}});f+=1;let R=Date.now()-S;e.log.info(`Lambda deploy batch ${y+1}/${i.length} completed: ${M} copied, ${A} skipped unchanged, ${C} failed.`,{phase:"deploy-copy",batch:y+1,totalBatches:i.length,items:P.detail.objects.length,copied:M,skipped:A,failed:C,elapsedMs:R,elapsedSeconds:Math.round(R/10)/100,workerExporterVersion:P.response.exporterVersion,...P.requestId?{lambdaRequestId:P.requestId}:{},...P.completedAt?{workerCompletedAt:P.completedAt}:{}}),e.log.progress(`Lambda deploy: ${p}/${e.files.length}`,{phase:"deploy-copy",index:p,totalFiles:e.files.length,uploaded:u.length,skipped:l.length,failed:g.length,mode:"lambda-s3-copy",workerCount:m,completedBatches:f,totalBatches:i.length});}})),g.length>0)throw new Error(`Remote deployment failed for ${g.length} object(s).`);let h=[];for(let y=0;y<e.deletedFiles.length;y+=1e3){let S=e.deletedFiles.slice(y,y+1e3);if(S.length===0)continue;let P=await Ir({client:e.client,jobId:r,targetId:t.id,batchIndex:i.length+Math.floor(y/1e3)+1,items:[],deleteRelativeKeys:S,maxAttempts:e.maxAttempts});h.push(...P.detail.deletedKeys),e.log.progress(`Lambda deploy deletions: ${h.length}/${e.deletedFiles.length}`,{phase:"deploy-delete",index:h.length,totalFiles:e.deletedFiles.length,deleted:h.length,mode:"lambda-s3-copy"});}return {uploadedKeys:u,skippedKeys:l,failedKeys:g,deletedKeys:h}}async function vl(e){let t=Fo(e.remoteConfig,e.config),r=ne(`deploy-manifest-${Date.now()}-${tt(Buffer.from(t.id)).slice(0,12)}`,"jobId"),n=e.objects.map(p=>({sourceKey:p.object.key,targetRelativeKey:p.relativePath,sha256:p.object.sha256,bytes:p.object.bytes,contentType:p.contentType||"application/octet-stream",cacheControl:e.revalidateFiles.has(et(p.relativePath))?In:St(p.relativePath)?e.config.s3.htmlCacheControl:e.config.s3.assetCacheControl})),s=[],o=Math.max(1,Math.min(500,e.batchSize));for(let p=0;p<n.length;p+=o)s.push(n.slice(p,p+o));let a=[],c=[],i=[],d=0,u=0,l=Math.max(1,Math.min(Math.max(1,e.concurrency),Math.max(1,s.length)));if(e.log.progress(`Lambda deploy: 0/${n.length}`,{phase:"deploy-copy",index:0,totalFiles:n.length,mode:"lambda-s3-native",workerCount:l}),await Promise.all(Array.from({length:s.length===0?0:l},async()=>{for(;;){let p=d;if(p>=s.length)return;d+=1;let f=await Ir({client:e.client,jobId:r,targetId:t.id,batchIndex:p+1,items:s[p],maxAttempts:e.maxAttempts});for(let w of f.detail.objects)u+=1,w.status==="copied"?a.push(w.targetKey):w.status==="skipped"?c.push(w.targetKey):i.push(w.targetKey);e.log.progress(`Lambda deploy: ${u}/${n.length}`,{phase:"deploy-copy",index:u,totalFiles:n.length,uploaded:a.length,skipped:c.length,failed:i.length,mode:"lambda-s3-native",workerCount:l});}})),i.length>0)throw new Error(`Remote deployment failed for ${i.length} object(s).`);let g=[];for(let p=0;p<e.deletedFiles.length;p+=1e3){let f=e.deletedFiles.slice(p,p+1e3);if(f.length===0)continue;let w=await Ir({client:e.client,jobId:r,targetId:t.id,batchIndex:s.length+Math.floor(p/1e3)+1,items:[],deleteRelativeKeys:f,maxAttempts:e.maxAttempts});g.push(...w.detail.deletedKeys);}return {uploadedKeys:a,skippedKeys:c,failedKeys:i,deletedKeys:g}}async function Cl(e,t,r,n,s){let o={...t,jobId:ne(`profile-${tt(Buffer.from(n)).slice(0,16)}-${Date.now()}`,"jobId"),objects:{},assetMap:{...t.assetMap},generatedAt:new Date().toISOString()},a=0,c=Math.max(1,Math.min(Math.max(1,s),Math.max(1,r.length)));return await Promise.all(Array.from({length:r.length===0?0:c},async()=>{for(;;){let i=a;if(i>=r.length)return;a+=1;let d=r[i],u=Ze(e.config.workspace.prefix,"jobs",o.jobId,"profile",tt(Buffer.from(d.relativePath)),k.basename(d.relativePath));o.objects[d.relativePath]={...d,object:await e.copyWorkspaceObject(d.object,u)};}})),o}async function Do(e=process.argv.slice(2)){let t=await Te(),r=Pr(e),n=bt(t,r),s=k.resolve(n.config.logDir||"logs"),o=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",a=o?k.join(k.resolve(o),"deploy-diff.json"):"",c=new dt(s,"deploy.log.jsonl",n.config.logLevel??"info"),i=n.profile?.extraReplacements??{},d=async()=>{};try{let u=await lt(t);if(e.includes("--content-sync")&&u?.schemaVersion!==2)throw new Error("Content-sync deployment requires a valid schema-2 targeted deploy plan.");let g=await is(t);if(g)throw new Error(`Deployment blocked: the crawl started at ${g.startedAt} did not complete its final rewrite. Run a successful crawl or resume the rewrite before deploying.`);let p=t.remoteWorkers?.rewrite?.enabled===!0,f=t.remoteWorkers?.deploy?.enabled===!0,w=null,m=null;if(p||f){let I=cr({runtimeDir:o});if(!I)throw new Error("Remote Lambda workers are enabled, but the runtime directory is unknown.");w=await tr(I);let U=!1;m=new mt(w,{},{onProgress:x=>{if(x.operation==="rewrite"){c.debug("Lambda rewrite task progress.",{remoteWorkerOperation:x.operation,remoteWorkerTaskId:x.taskId,remoteWorkerStatus:x.status,remoteWorkerCompletedItems:x.completedItems,remoteWorkerFailedItems:x.failedItems,remoteWorkerTotalItems:x.totalItems,remoteWorkerUpdatedAt:x.updatedAt});return}c.progress(`Lambda ${x.operation} batch: ${x.completedItems+x.failedItems}/${x.totalItems}`,{remoteWorkerOperation:x.operation,remoteWorkerTaskId:x.taskId,remoteWorkerStatus:x.status,remoteWorkerCompletedItems:x.completedItems,remoteWorkerFailedItems:x.failedItems,remoteWorkerTotalItems:x.totalItems,remoteWorkerUpdatedAt:x.updatedAt});},onProgressError:x=>{U||(U=!0,c.warn("Lambda progress status could not be read; task execution continues.",{error:x.message}));}});}let h=f&&p&&t.remoteWorkers?.render?.enabled===!0&&m&&w?.functions.asset?await dr(t):null;if(h&&m&&w){if(h.workspace.bucket!==w.workspace.bucket||h.workspace.prefix!==w.workspace.prefix)throw new Error("Remote export manifest workspace does not match remote-workers.json.");let I=n.config,U=I.s3SyncMode??"sdk-upload-delete";if(u?.schemaVersion===2&&u.deletedFiles.length>0&&U!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let x=u?.schemaVersion===2?[...u.uploadGroups.assets,...u.uploadGroups.content,...u.uploadGroups.listings,...u.uploadGroups.resources,...u.uploadGroups.sitemapLeaves,...u.uploadGroups.sitemapIndexes]:u?.changedFiles??[];if(u?.schemaVersion===2){let se=[...new Set(x)],we=[...new Set(u.changedFiles)];if(se.length!==we.length||we.some(Le=>!se.includes(Le)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let le=!!u&&!u.fullSyncRequired?[...new Set(x.map(et))]:Object.keys(h.objects).sort(),L=le.filter(se=>!h.objects[se]);if(L.length>0)throw new Error(`Remote export manifest is missing ${L.length} planned object(s): ${L.slice(0,10).join(", ")}`);let Re=h,z=le.map(se=>h.objects[se]);n.name&&(t.targetOrigin!==I.targetOrigin||Object.keys(i).length>0)&&(Re=await Cl(m,h,z,n.name||"default",t.concurrency),await ar(I,Re,Re.assetMap,{client:m,jobId:Re.jobId,concurrency:t.concurrency,batchSize:25,maxAttempts:2,onBatchProgress:({completedItems:se,totalItems:we,completedBatches:Le,totalBatches:Wt,changedTextFiles:$r})=>{c.progress(`Lambda deployment-profile rewrite: ${se}/${we} files; ${Le}/${Wt} batches completed`,{phase:"rewrite-text",execution:"lambda",index:se,totalFiles:we,completedBatches:Le,totalBatches:Wt,changedTextFiles:$r});}}),z=le.map(se=>Re.objects[se])),n.name&&c.info(`Using deployment profile: ${n.name}`),c.info(`Starting S3-native Lambda deploy - s3://${I.s3.bucket}/${I.s3.prefix||""} (${I.s3.region}).`),c.mark("deploy");let ae=await vl({client:m,remoteConfig:w,config:I,objects:z,deletedFiles:U==="sdk-upload-delete"?u?.deletedFiles??[]:[],revalidateFiles:new Set((u?.revalidateFiles??[]).map(et)),concurrency:t.concurrency,batchSize:50,maxAttempts:2,log:c});a&&(await F.mkdir(k.dirname(a),{recursive:!0}),await F.writeFile(a,JSON.stringify({generatedAt:new Date().toISOString(),mode:"lambda-s3-native",summary:{totalFiles:z.length,eligibleFiles:z.length,uploaded:ae.uploadedKeys.length,skipped:ae.skippedKeys.length,rejected:0,failed:ae.failedKeys.length,deleted:ae.deletedKeys.length,protected:0,...n.name?{profile:n.name}:{}},uploadedKeys:ae.uploadedKeys,skippedKeys:ae.skippedKeys,rejectedKeys:[],failedKeys:ae.failedKeys,deletedKeys:ae.deletedKeys,protectedRemoteOnlyKeys:[]},null,2),"utf8")),u?.schemaVersion!==2&&await Ge(t),c.endMark("deploy"),await c.flush();return}let y=await Sl(t,n.config,n.name,i,c,u?.schemaVersion===2?u.changedFiles:void 0,p&&m?{client:m,jobId:`profile-${Date.now()}`,concurrency:t.remoteWorkers?.rewrite?.concurrency??t.rewriteConcurrency,batchSize:t.remoteWorkers?.rewrite?.batchSize??25,maxAttempts:t.remoteWorkers?.rewrite?.maxAttempts??2,onStageProgress:({index:I,totalFiles:U,file:x})=>{(I===1||I===U||I%25===0)&&c.progress(`Staging deployment-profile rewrite: ${I}/${U}`,{phase:"rewrite-stage",index:I,totalFiles:U,file:x,execution:"lambda"});},onBatchProgress:({completedItems:I,totalItems:U,completedBatches:x,totalBatches:te,changedTextFiles:le})=>{c.progress(`Lambda deployment-profile rewrite: ${I}/${U} files; ${x}/${te} batches completed`,{phase:"rewrite-text",execution:"lambda",index:I,totalFiles:U,completedBatches:x,totalBatches:te,changedTextFiles:le});}}:void 0);d=y.cleanup;let S={...n.config,outputDir:y.outputDir},P=S.s3SyncMode??"sdk-upload-delete";if(u?.schemaVersion===2&&u.deletedFiles.length>0&&P!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let M=u?.schemaVersion!==2&&y.outputDir!==t.outputDir&&(t.targetOrigin!==n.config.targetOrigin||Object.keys(i).length>0),A=!!u&&!u.fullSyncRequired&&!M;if(u?.schemaVersion===2&&!A)throw new Error("Content-sync deployment cannot widen into a full-output deployment.");n.name&&c.info(`Using deployment profile: ${n.name}`),c.info(`Starting deploy \u2014 s3://${S.s3.bucket}/${S.s3.prefix||""} (region: ${S.s3.region})`),c.info(`S3 sync mode: ${P}`),u&&u.fullSyncRequired?c.info("Deploy plan requests a full sync; scanning the whole output tree."):u&&M?c.info("Ignoring incremental deploy plan because deployment-profile rewrites require a full sync."):A&&c.info(`Using incremental deploy plan: ${u.changedFiles.length} changed file(s), ${u.deletedFiles.length} deleted file(s).`),c.mark("deploy");let C=new S3Client({region:S.s3.region}),R=y.outputDir===t.outputDir?await nl(t,o,c):null,$=u?.schemaVersion===2?[...u.uploadGroups.assets,...u.uploadGroups.content,...u.uploadGroups.listings,...u.uploadGroups.resources,...u.uploadGroups.sitemapLeaves,...u.uploadGroups.sitemapIndexes]:u?.changedFiles??[];if(u?.schemaVersion===2){let I=[...new Set($)],U=[...new Set(u.changedFiles)];if(I.length!==U.length||U.some(x=>!I.includes(x)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let T=A?(await Promise.all($.map(async I=>{try{return (await F.stat(k.join(S.outputDir,I))).isFile()?I:null}catch{if(u?.schemaVersion===2)throw new Error(`Content-sync planned upload is missing: ${I}`);return null}}))).filter(I=>I!==null):await Ci(["**/*"],{cwd:S.outputDir,onlyFiles:!0,dot:!0}),N=[],H=[],b=T.filter(I=>{let U=R?.byRelativePath.get(et(I));if(!U)return !0;let x=vr(S.s3.prefix,I);return N.push(I),H.push(x),c.reject(U.kind,U.sourceUrl||I,"rewrite not completed; deploy upload blocked","deploy-guard",{file:I,key:x,outputPath:U.outputPath,manifestPath:R?.manifestPath}),!1}),W=new Set((u?.revalidateFiles||[]).map(et)),de=new Set((A?b:T).map(I=>vr(S.s3.prefix,I)));R&&(R.pendingPages>0||R.pendingTextAssets>0)&&c.info(`Deploy rewrite guard loaded ${R.pendingPages} pending page(s) and ${R.pendingTextAssets} pending text asset(s) from crawl manifest.`,{manifestPath:R.manifestPath,pendingPages:R.pendingPages,pendingTextAssets:R.pendingTextAssets,source:"deploy-guard"}),N.length>0&&c.warn(`Deploy rewrite guard blocked ${N.length} file(s) from upload because final crawl rewrite is incomplete.`,{rejected:N.length,manifestPath:R?.manifestPath,source:"deploy-guard"}),A?c.info(`Using ${b.length} eligible planned changed file(s) from deploy plan for upload${N.length>0?` (${N.length} blocked by deploy rewrite guard)`:""}.`):c.info(`Found ${T.length} file(s) in output directory "${S.outputDir}"${N.length>0?`; ${b.length} remain eligible for upload after deploy rewrite guard filtering`:""}.`),c.progress("Deploy preflight: comparing local output with remote S3 state.",{phase:"deploy",mode:P,totalFiles:b.length,scannedFiles:T.length,rejected:N.length,deploymentProfile:n.name||"",s3Bucket:S.s3.bucket,s3Prefix:S.s3.prefix||"",targetedPlan:A,deployGuardEnabled:y.outputDir===t.outputDir});let V=0,B=0,E=0,q=[],D=[],J=[],Q=[],pe=[],Pe=new Map,v=new Map,j=new Map,O=new Map,K=new Map;if(f){if(!m||!w)throw new Error("Remote deploy worker client was not initialized.");if(P!=="sdk-upload-delete"&&P!=="sdk-upload-only")throw new Error(`Unsupported remote deploy mode: ${P}`);let I=await Rl({client:m,remoteConfig:w,config:S,files:b,deletedFiles:P==="sdk-upload-delete"?u?.deletedFiles??[]:[],revalidateFiles:W,contentTypeByRelativePath:R?.contentTypeByRelativePath,concurrency:S.remoteWorkers?.deploy?.concurrency??S.assetDownloadConcurrency,batchSize:S.remoteWorkers?.deploy?.batchSize??50,maxAttempts:S.remoteWorkers?.deploy?.maxAttempts??2,log:c});q.push(...I.uploadedKeys),D.push(...I.skippedKeys),J.push(...I.failedKeys),Q=I.deletedKeys,V=q.length,B=D.length,E=J.length,c.info(`Lambda deploy complete: ${V} copied, ${B} skipped unchanged, ${Q.length} deleted, ${E} failed.`),a&&(await F.mkdir(k.dirname(a),{recursive:!0}),await F.writeFile(a,JSON.stringify({generatedAt:new Date().toISOString(),mode:"lambda-s3-copy",summary:{totalFiles:T.length,eligibleFiles:b.length,uploaded:V,skipped:B,rejected:H.length,failed:E,deleted:Q.length,protected:0,...n.name?{profile:n.name}:{}},uploadedKeys:q,skippedKeys:D,rejectedKeys:H,failedKeys:J,deletedKeys:Q,protectedRemoteOnlyKeys:[]},null,2),"utf8")),u?.schemaVersion!==2&&await Ge(t),c.endMark("deploy"),await c.flush();return}A||(c.info("Listing existing S3 objects to detect unchanged files..."),Pe=await ul(C,S.s3.bucket,S.s3.prefix,c),c.info(`Found ${Pe.size} existing key(s) in S3.`));let G=async I=>Pe.has(I)?Pe.get(I)??null:A?dl(C,S.s3.bucket,I,v):null;for(let I of b){let U=k.join(S.outputDir,I),x=vr(S.s3.prefix,I),te=R?.contentTypeByRelativePath.get(I),le=te&&te!=="application/octet-stream"?te:js.lookup(I)||te||"application/octet-stream",L=W.has(et(I)),Re=L?In:St(I)?S.s3.htmlCacheControl:S.s3.assetCacheControl,z=V+B+E+1;b.length<=200||z<=5||z%10===0||z===b.length?(c.progress(`Uploading [${z}/${b.length}] ${x} (${le})`,{file:I,key:x,index:z,totalFiles:b.length,contentType:le,mode:P}),c.info(`Uploading [${z}/${b.length}] ${x} (${le})`)):c.debug(`Uploading [${z}/${b.length}] ${x} (${le})`);try{let ve=await F.readFile(U),ae=await G(x),se=null,we=null;if(ae){let Le=await pl(C,S.s3.bucket,x,ae),Wt=!L||$o(Le.cacheControl)===$o(Re),$r=Cn(Le.contentType)===Cn(String(le)),Or=Wt&&$r,Ho=ve.byteLength;if(ae.size===Ho){let qt=ae.etag.includes("-");if(ae.etag!==""&&!qt){let Vo=gl(ve);if(ae.etag===Vo&&Or){B++,b.length<=200||B<=5||B%25===0?(c.progress(`Skipped unchanged [${z}/${b.length}] ${x}`,{file:I,key:x,index:z,totalFiles:b.length,uploaded:V,skipped:B,failed:E,mode:P,reason:"etag-md5-match"}),c.info(`Skipped unchanged [${z}/${b.length}] ${x}`)):c.debug(`Skipped unchanged [${z}/${b.length}] ${x}`),D.push(x);continue}}se=tt(ve);let jn=await fl(C,S.s3.bucket,x,j);if(jn&&jn===se&&Or){B++,b.length<=200||B<=5||B%25===0?(c.progress(`Skipped unchanged [${z}/${b.length}] ${x}`,{file:I,key:x,index:z,totalFiles:b.length,uploaded:V,skipped:B,failed:E,mode:P,reason:"metadata-sha256-match"}),c.info(`Skipped unchanged [${z}/${b.length}] ${x}`)):c.debug(`Skipped unchanged [${z}/${b.length}] ${x}`),D.push(x);continue}}if(St(I)){we=xr(ve);let qt=await hl(C,S.s3.bucket,x,O,K);if(qt&&qt===we&&Or){B++,b.length<=200||B<=5||B%25===0?(c.progress(`Skipped semantically unchanged [${z}/${b.length}] ${x}`,{file:I,key:x,index:z,totalFiles:b.length,uploaded:V,skipped:B,failed:E,mode:P,reason:"normalized-html-match"}),c.info(`Skipped semantically unchanged [${z}/${b.length}] ${x}`)):c.debug(`Skipped semantically unchanged [${z}/${b.length}] ${x}`),D.push(x);continue}}}se=se??tt(ve),we=we??(St(I)?xr(ve):null),await be(`S3 upload ${x}`,9e5,({abortController:Le})=>new Upload({client:C,abortController:Le,params:{Bucket:S.s3.bucket,Key:x,Body:ve,ContentType:le,CacheControl:Re,Metadata:{[Mo]:se,...we?{[jo]:we}:{}}}}).done()),V++,q.push(x),(V%25===0||V===b.length)&&(c.progress(`Uploaded ${V}/${b.length} file(s)\u2026`,{uploaded:V,skipped:B,failed:E,totalFiles:b.length,mode:P}),c.info(`Uploaded ${V}/${b.length} file(s)\u2026`));}catch(ve){E++,J.push(x),c.error(`Failed to upload ${x}: ${ve.message}`,{key:x,error:String(ve)});}}if(c.info(`Upload complete: ${V} uploaded, ${B} skipped unchanged, ${H.length} rejected by guard, ${E} failed.`),J.length>0||H.length>0)throw new Error(`Deployment stopped before deletion because ${J.length} upload(s) failed and ${H.length} upload(s) were rejected.`);if(P==="sdk-upload-delete"){let I=new Set((u?.deletedFiles??[]).map(x=>vr(S.s3.prefix,x))),U=[...I].filter(x=>!de.has(x));if(!A&&(pe=[...Pe.keys()].filter(te=>!de.has(te)).filter(te=>!I.has(te)),pe.length>0)){c.warn(`Protected ${pe.length} remote-only S3 object(s) from deletion because the crawl did not provide explicit tombstones.`,{protected:pe.length,source:"deploy-delete-guard"});for(let te of pe)c.debug(` protected remote-only object: ${te}`);}Q=await ml(C,S.s3.bucket,A?U:U.filter(x=>Pe.has(x)),c);}else c.info("Sync mode is sdk-upload-only \u2014 skipping stale object deletion.");a&&(await F.mkdir(k.dirname(a),{recursive:!0}),await F.writeFile(a,JSON.stringify({generatedAt:new Date().toISOString(),mode:P,summary:{totalFiles:T.length,eligibleFiles:b.length,uploaded:q.length,skipped:D.length,rejected:H.length,failed:J.length,deleted:Q.length,protected:pe.length,...n.name?{profile:n.name}:{}},uploadedKeys:q,skippedKeys:D,rejectedKeys:H,failedKeys:J,deletedKeys:Q,protectedRemoteOnlyKeys:pe},null,2),"utf8")),u?.schemaVersion!==2&&await Ge(t),c.endMark("deploy"),await c.flush();}finally{await d().catch(()=>{});}}function xl(e){let t=e;return t.endsWith("/")?t+="index.html":k.posix.basename(t).includes(".")||(t+="/index.html"),t}function Il(e,t){let r=t.trim().replace(/^\/+|\/+$/g,"");if(!r)return null;let n=`/${r}`;return e===n||e.startsWith(`${n}/`)?e:`${n}${e.startsWith("/")?e:`/${e}`}`}function Uo(e,t){let r=new Set;for(let n of e){let s=new URL(n,"https://content-sync.invalid");if(s.origin!=="https://content-sync.invalid")continue;let o=`${s.pathname}${s.search}`,a=xl(s.pathname);r.add(o),r.add(`${a}${s.search}`);let c=Il(a,t);c&&r.add(`${c}${s.search}`);}return [...r].sort()}function $l(){let e=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();return e?k.join(k.resolve(e),"content-sync-invalidation.json"):""}async function An(e,t){if(!e)return;await F.mkdir(k.dirname(e),{recursive:true});let r=`${e}.${process.pid}.${Date.now()}.tmp`;await F.writeFile(r,JSON.stringify({...t,updatedAt:new Date().toISOString()},null,2),"utf8"),await F.rename(r,e);}async function Ol(e,t,r,n){if(!e)return null;try{let s=JSON.parse(await F.readFile(e,"utf8"));if(s.schemaVersion!==1||s.distributionId!==t||s.impactHash!==r||!Array.isArray(s.batches))throw new Error("Content-sync invalidation state does not match the deploy plan.");let o=s.batches.flatMap(a=>a.paths);return o.length!==n.length||o.some((a,c)=>a!==n[c])?null:s}catch(s){if(s.code==="ENOENT")return null;throw s}}function Ml(e,t=3e3){let r=[];for(let n=0;n<e.length;n+=t)r.push(e.slice(n,n+t));return r}async function Wo(e=process.argv.slice(2)){let t=await Te(),r=Pr(e),n=bt(t,r),s=n.config,o=new dt(s.logDir||"logs","invalidate.log.jsonl",s.logLevel??"info"),a=e.includes("--content-sync"),c=await lt(t);if(a&&c?.schemaVersion!==2)throw new Error("Content-sync invalidation requires a valid schema-2 targeted deploy plan.");if(n.name&&o.info(`Using deployment profile: ${n.name}`),!s.cloudFront.distributionId){o.info("No CloudFront distributionId configured; skipping invalidation."),await o.flush();return}let i=c?.schemaVersion===2?c.invalidationPaths:s.cloudFront.invalidationPaths.length>0?s.cloudFront.invalidationPaths:["/*"],d=c?.schemaVersion===2?Uo(i,s.s3.prefix||""):i;if(c?.schemaVersion===2&&d.some(f=>f==="/*"))throw new Error("Content-sync cannot use a whole-distribution invalidation.");if(d.length===0){o.info("The targeted deploy plan has no CloudFront paths to invalidate."),await o.flush();return}let u=s.cloudFront.distributionId,l=new CloudFrontClient({region:"us-east-1",maxAttempts:5});if(o.mark("invalidate"),c?.schemaVersion!==2){let f=`smartcloud-static-publisher-${Date.now()}`;await be(`CloudFront invalidation for distribution ${u}`,3e5,({httpOptions:w})=>l.send(new CreateInvalidationCommand({DistributionId:u,InvalidationBatch:{CallerReference:f,Paths:{Quantity:d.length,Items:d}}}),w)),o.endMark("invalidate"),await o.flush();return}let g=$l(),p=await Ol(g,u,c.impactHash,d);if(!p){let f=createHash("sha256").update(JSON.stringify(d)).digest("hex");p={schemaVersion:1,distributionId:u,impactHash:c.impactHash,updatedAt:new Date().toISOString(),batches:Ml(d).map((w,m)=>({index:m,callerReference:`wpsuite-content-sync-${c.impactHash.slice(0,24)}-${f.slice(0,24)}-${m}`,paths:w,invalidationId:null,status:"pending"}))},await An(g,p);}for(let f of p.batches){if(!f.invalidationId){let w=await be(`CloudFront targeted invalidation batch ${f.index+1}`,3e5,({httpOptions:h})=>l.send(new CreateInvalidationCommand({DistributionId:u,InvalidationBatch:{CallerReference:f.callerReference,Paths:{Quantity:f.paths.length,Items:f.paths}}}),h)),m=String(w.Invalidation?.Id||"").trim();if(!m)throw new Error("CloudFront did not return an invalidation ID.");f.invalidationId=m,f.status="created",await An(g,p);}await waitUntilInvalidationCompleted({client:l,maxWaitTime:900,minDelay:2,maxDelay:15},{DistributionId:u,Id:f.invalidationId}),f.status="completed",await An(g,p),o.info(`CloudFront invalidation batch ${f.index+1}/${p.batches.length} completed.`,{invalidationId:f.invalidationId,pathCount:f.paths.length});}o.endMark("invalidate"),await o.flush();}function Er(e){let t=new URL(e);return t.hash="",t.href}function qo(e,t,r){let n=new Set([...t].map(i=>Er(i))),s=new Map,o=new Map,a=i=>{s.set(Er(i),i);},c=i=>{if(!i)return;let d=Er(i);s.has(d)||o.set(d,i);};for(let i of e.deleteUrls)a(i);for(let i of e.resourceDeleteUrls)a(i);for(let i of e.archiveFamilies)if(i.foundPosts===0)for(let d of i.pageUrls)n.has(Er(d))||a(d);for(let i of e.directRenderUrls)c(i);for(let i of e.resourceRenderUrls)c(i);for(let i of e.archiveFamilies)c(i.pageUrls[0]),c(i.pageUrls.at(-1));for(let i of r)c(i);return {expectedAvailable:[...o.values()],expectedMissing:[...s.values()]}}var No=["claimed","impact-planned","rendered","deployed","invalidated","verified","acknowledged"];function On(){let e=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();if(!e)throw new Error("Content-sync requires a runtime directory.");return k.resolve(e)}function Pt(e,t){return No.indexOf(e)>=No.indexOf(t)}async function ze(e,t,r,n,s={}){let o={timestamp:new Date().toISOString(),eventType:t,status:t.endsWith("failed")?"failed":"success",actorSource:"content-sync",jobId:r.id||"",command:"content-sync",message:n,details:s};await F.mkdir(e,{recursive:true}),await F.appendFile(k.join(e,"audit-events.jsonl"),`${JSON.stringify(o)}
19
+ `,"utf8");}function Fl(e){return Rt({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 Dl(e,t){let r=(e.scheduler?.rules??[]).filter(s=>s.command==="content-sync"&&s.id===t);if(r.length===0)throw new Error(`Content-sync scheduler rule not found: ${t}`);if(r.length>1)throw new Error(`Content-sync scheduler rule ID is not unique: ${t}`);let n=r[0];if(n.enabled===false)throw new Error(`Content-sync scheduler rule is disabled: ${t}`);return n}async function Mn(e,t){if(e.subscriptionType!=="PROFESSIONAL"&&e.subscriptionType!=="AGENCY")throw new Error("Content-sync requires an active Professional or Agency subscription.");let r=Fl(t),n=bt(e,t.deploymentProfile),s=n.name||"",o=Un(e.sourceOrigin,String(t.id||""),r),a=Ln({sourceOrigin:e.sourceOrigin,targetOrigin:n.config.targetOrigin,deploymentProfile:s,s3Bucket:n.config.s3.bucket,s3Prefix:n.config.s3.prefix,cloudFrontDistributionId:n.config.cloudFront.distributionId,urlRewriteMode:n.config.urlRewriteMode,extraReplacements:n.profile?.extraReplacements??{}}),c=Wn({siteId:e.wpsuite?.siteSettings?.siteId,accountId:e.wpsuite?.siteSettings?.accountId,sourceOrigin:e.sourceOrigin,ruleId:String(t.id||""),targetFingerprint:a}),i=`content-sync:${String(t.id||"")}:${We({scopeFingerprint:o,targetFingerprint:a}).slice(0,32)}`,d=new Nt({sourceOrigin:e.sourceOrigin,runtimeToken:String(e.wpsuite?.runtimeToken||""),ignoreHttpsErrors:e.ignoreHttpsErrors}),u=await d.releaseFingerprint(r.includeSubsites),l=Dn({wordpressFingerprint:u,targetFingerprint:a,sourceOrigin:e.sourceOrigin,sitemapPaths:kt(e.sitemapPaths),blockedPathPrefixes:e.blockedPathPrefixes,assetPathPrefixes:e.assetPathPrefixes,noJavaScriptRenderPathPrefixes:e.noJavaScriptRenderPathPrefixes,urlRewriteMode:e.urlRewriteMode});return {rule:t,scope:r,deployment:n,deploymentProfile:s,scopeFingerprint:o,targetFingerprint:a,consumerId:c,coalesceKey:i,client:d,releaseFingerprint:l}}async function Ul(e){let t=On(),r=Bt(t),n=await Te(),s=(n.scheduler?.rules??[]).filter(d=>d.command==="content-sync"&&d.enabled!==false);if(s.length===0)return 0;let o=await Ur(r),a=await Ht(r),c=await Dr(r),i=0;for(let d of s){let u=await Mn(n,d),l=c.entries[u.coalesceKey];if(!l||l.releaseJobId!==e||l.scopeFingerprint!==u.scopeFingerprint||l.targetFingerprint!==u.targetFingerprint||l.releaseFingerprint!==u.releaseFingerprint)throw new Error(`The normal release has no matching pre-crawl content-sync cutoff for rule ${String(d.id||"")}.`);let g=We({releaseJobId:e,releaseFingerprint:u.releaseFingerprint,scopeFingerprint:u.scopeFingerprint,targetFingerprint:u.targetFingerprint,sequence:l.sequence}),p=await u.client.establishBaseline({consumerId:u.consumerId,scopeFingerprint:u.scopeFingerprint,baselineId:g,postTypes:u.scope.postTypes,includeSubsites:u.scope.includeSubsites,sequence:l.sequence}),f={schemaVersion:1,ruleId:String(d.id||""),consumerId:u.consumerId,coalesceKey:u.coalesceKey,scopeFingerprint:u.scopeFingerprint,targetFingerprint:u.targetFingerprint,releaseFingerprint:u.releaseFingerprint,baselineId:g,deploymentProfile:u.deploymentProfile,committedSequence:p.committedSequence,releaseJobId:e,verifiedAt:new Date().toISOString()};o.entries[u.coalesceKey]=f;let w=a.rules[u.coalesceKey];a.rules[u.coalesceKey]={ruleId:String(d.id||""),consumerId:u.consumerId,coalesceKey:u.coalesceKey,committedSequence:p.committedSequence,observedHeadSequence:p.headSequence,lag:Math.max(0,p.headSequence-p.committedSequence),lastSuccessfulJobId:w?.lastSuccessfulJobId||e,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:p.headSequence>p.committedSequence,retryAttempt:0,nextRetryAt:null,lastError:null,baselineStatus:"ready",baselineReason:null};let m=await Qn(r,u.coalesceKey);await ze(t,"content-sync-baseline-established",{id:e},"A normal release established a verified content-sync baseline.",{ruleId:d.id,consumerId:u.consumerId,committedSequence:p.committedSequence,supersededCurrent:m}),i++;}return await Hn(r,o),await Vt(r,a),await F.rm(r.releaseCutoff,{force:true}),i}async function Ll(e){let t=On(),r=Bt(t),n=await Te(),s=(n.scheduler?.rules??[]).filter(c=>c.command==="content-sync"&&c.enabled!==false);if(s.length===0)return 0;let o=await Dr(r),a=0;for(let c of s){let i=await Mn(n,c),d=Object.values(o.entries).find(g=>g.releaseJobId===e&&g.ruleId===String(c.id||""));if(d){if(d.coalesceKey===i.coalesceKey&&d.scopeFingerprint===i.scopeFingerprint&&d.targetFingerprint===i.targetFingerprint&&d.releaseFingerprint===i.releaseFingerprint){a++;continue}throw new Error(`The normal release scope changed after its journal cutoff was captured for rule ${String(c.id||"")}. Start a new normal publish instead of resuming this release.`)}let u=await i.client.unboundHead(i.scope.postTypes,i.scope.includeSubsites),l={schemaVersion:1,releaseJobId:e,ruleId:String(c.id||""),coalesceKey:i.coalesceKey,scopeFingerprint:i.scopeFingerprint,targetFingerprint:i.targetFingerprint,releaseFingerprint:i.releaseFingerprint,sequence:u.headSequence,capturedAt:new Date().toISOString()};o.entries[i.coalesceKey]=l,await ze(t,"content-sync-release-cutoff-captured",{id:e},"A normal release captured its pre-crawl content-sync journal cutoff.",{ruleId:c.id,consumerId:i.consumerId,sequence:u.headSequence}),a++;}return await Bn(r,o),a}async function Wl(e,t){let r=JSON.parse(await F.readFile(k.join(e,"current-run.json"),"utf8"));if(r.id!==t||r.command!=="content-sync"||!String(r.ruleId||"").trim())throw new Error("The active queue job is not the requested content-sync job.");return r}async function $n(e,t,r=0){let n=e.protocol==="https:"?Nn:Xo,s=e.protocol==="https:"?new Nn.Agent({rejectUnauthorized:!t}):void 0;return new Promise((o,a)=>{let c=n.get(e,{agent:s,timeout:3e4,headers:{"user-agent":"WP-Suite-Content-Sync/1"}},i=>{let d=i.statusCode??500,u=i.headers.location;if(d>=300&&d<400&&u&&r<5){i.resume(),$n(new URL(u,e),t,r+1).then(o,a);return}let l=[],g=0;i.on("data",p=>{g+=p.byteLength,g<=1048576&&l.push(p);}),i.on("error",a),i.on("end",()=>o({status:d,body:Buffer.concat(l).toString("utf8"),contentType:String(i.headers["content-type"]||"")}));});c.on("timeout",()=>c.destroy(new Error(`Verification timed out: ${e}`))),c.on("error",a);})}async function ql(e,t,r,n){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 o=JSON.parse(await F.readFile(n,"utf8")),a=new Set;for(let[u,l]of Object.entries(o.pages??{}))a.add(u),typeof l.url=="string"&&a.add(l.url);let c=await lt(e);if(c?.schemaVersion!==2||c.impactHash!==r.impactHash)throw new Error("Content-sync verification requires the matching targeted deploy plan.");let i=qo(r,a,c.resolvedSitemapUrls),d=new Set(r.resourceRenderUrls.map(u=>{let l=new URL(u);return l.hash="",l.href}));for(let u of i.expectedAvailable){let l=new URL(u),g=new URL(`${l.pathname}${l.search}`,s),p=await $n(g,e.ignoreHttpsErrors);if(p.status<200||p.status>=400)throw new Error(`Content-sync verification failed (${p.status}): ${g}`);let f=new URL(u);if(f.hash="",d.has(f.href)){if(!p.contentType.toLowerCase().startsWith("application/json"))throw new Error(`Resource verification expected application/json: ${g}`);try{JSON.parse(p.body);}catch{throw new Error(`Resource verification received invalid JSON: ${g}`)}}}for(let u of i.expectedMissing){let l=new URL(u),g=new URL(`${l.pathname}${l.search}`,s),p=await $n(g,e.ignoreHttpsErrors);if(p.status>=200&&p.status<300)throw new Error(`Deleted content is still publicly available: ${g}`)}}async function _l(e){let t=await F.readFile(e.candidateManifest,"utf8");JSON.parse(t);let r=`${e.trustedManifest}.${process.pid}.tmp`;await F.writeFile(r,t,"utf8"),await F.rename(r,e.trustedManifest);}async function Nl(e,t){try{let r=JSON.parse(await F.readFile(e.candidateManifest,"utf8"));if([...Object.values(r.pages??{}),...Object.values(r.assets??{})].some(s=>s?.lastSeenRunId===t))return "resumed"}catch(r){if(r.code!=="ENOENT")throw new Error(`The content-sync candidate manifest is invalid: ${r instanceof Error?r.message:String(r)}`,{cause:r})}return await F.copyFile(e.trustedManifest,e.candidateManifest).catch(r=>{throw new Error(`A trusted crawl manifest is required before content-sync: ${r instanceof Error?r.message:String(r)}`,{cause:r})}),"initialized"}async function it(e,t,r,n,s={},o=0,a=async()=>{}){t.phase=r,await Vn(e,t),await Jn(e,{schemaVersion:1,jobId:t.jobId,phase:r,completedItems:n,totalItems:n,attempt:o,updatedAt:new Date().toISOString(),details:s}),await ze(k.dirname(e.state),"content-sync-checkpoint",{id:t.jobId},`Content-sync checkpoint completed: ${r}.`,{phase:r,completedItems:n,totalItems:n,attempt:o,...s}),await a(r,t);}async function Bl(e,t={}){let r={loadConfig:Te,resolveRuleContext:Mn,runCrawl:To,runDeploy:Do,runInvalidate:Wo,verifyDeployment:ql,promoteCandidateManifest:_l,clearDeployPlan:Ge,afterCheckpoint:async()=>{},...t},n=On(),s=Bt(n),o=await Wl(n,e),a=await r.loadConfig(),c=Dl(a,String(o.ruleId)),i=await r.resolveRuleContext(a,c);if(o.coalesceKey&&o.coalesceKey!==i.coalesceKey)throw new Error("Queued content-sync demand no longer matches the active rule scope.");let u=(await Ur(s)).entries[i.coalesceKey];if(!u||u.scopeFingerprint!==i.scopeFingerprint||u.targetFingerprint!==i.targetFingerprint||u.releaseFingerprint!==i.releaseFingerprint)throw await ze(n,"content-sync-baseline-rejected",o,"Content-sync requires a new verified full or incremental release baseline.",{ruleId:c.id,reason:u?"mismatch":"missing"}),new Error("Content-sync baseline is missing or stale. Run a successful normal publish first.");let l=await Lr(s),g=await i.client.head({consumerId:i.consumerId,scopeFingerprint:i.scopeFingerprint,baselineId:u.baselineId,postTypes:i.scope.postTypes,includeSubsites:i.scope.includeSubsites});if(l&&l.jobId!==o.id)throw new Error("Another immutable content-sync range is still active.");if(l||(l={schemaVersion:1,jobId:o.id,ruleId:String(c.id||""),consumerId:i.consumerId,coalesceKey:i.coalesceKey,scopeFingerprint:i.scopeFingerprint,targetFingerprint:i.targetFingerprint,baselineId:u.baselineId,fromSequence:g.committedSequence,toSequence:g.headSequence,phase:"claimed",impactHash:null,startedAt:new Date().toISOString(),updatedAt:new Date().toISOString()},await it(s,l,"claimed",0,{fromSequence:l.fromSequence,toSequence:l.toSequence},0,r.afterCheckpoint),await ze(n,"content-sync-range-claimed",o,"Content-sync claimed a closed journal range.",{fromSequence:l.fromSequence,toSequence:l.toSequence})),l.fromSequence===l.toSequence){let h=await Ht(s);h.rules[i.coalesceKey]={ruleId:String(c.id||""),consumerId:i.consumerId,coalesceKey:i.coalesceKey,committedSequence:l.toSequence,observedHeadSequence:g.headSequence,lag:Math.max(0,g.headSequence-l.toSequence),lastSuccessfulJobId:o.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:false,retryAttempt:0,nextRetryAt:null,lastError:null},await Vt(s,h),await Wr(s);return}let p=await Kn(s);if(!Pt(l.phase,"impact-planned")){let h=await i.client.events({consumerId:i.consumerId,scopeFingerprint:i.scopeFingerprint,baselineId:u.baselineId,postTypes:i.scope.postTypes,includeSubsites:i.scope.includeSubsites,afterSequence:l.fromSequence,throughSequence:l.toSequence}),y=Fr({events:h,scope:i.scope,sourceOrigin:a.sourceOrigin}),S=await i.client.resolveImpactFamilies(y,i.scope.includeSubsites);p=_n({jobId:o.id,ruleId:String(c.id||""),consumerId:i.consumerId,coalesceKey:i.coalesceKey,scopeFingerprint:i.scopeFingerprint,targetFingerprint:i.targetFingerprint,baselineId:u.baselineId,sourceOrigin:a.sourceOrigin,scope:i.scope,fromSequence:l.fromSequence,toSequence:l.toSequence,events:h,resolvedArchiveFamilies:S,sitemapUrls:kt(a.sitemapPaths)}),await zn(s,p),l.impactHash=p.impactHash,await it(s,l,"impact-planned",h.length,{eventCount:h.length,foldedCount:p.foldedCount,impactHash:p.impactHash},0,r.afterCheckpoint),await ze(n,"content-sync-impact-planned",o,"Content-sync persisted an immutable impact plan.",{impactHash:p.impactHash,eventCount:h.length});}if(!p||p.jobId!==o.id||l.impactHash!==p.impactHash)throw new Error("The immutable content-sync impact plan is missing or mismatched.");if(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH=s.candidateManifest,!Pt(l.phase,"rendered")){let h=await Nl(s,o.id);await ze(n,h==="resumed"?"content-sync-render-resumed":"content-sync-render-initialized",o,h==="resumed"?"Content-sync resumed its candidate crawl manifest.":"Content-sync initialized a candidate crawl manifest.",{impactHash:p.impactHash}),await r.runCrawl(["--content-sync-plan",s.impact]),await it(s,l,"rendered",p.directRenderUrls.length+p.archiveFamilyUrls.length+p.resourceRenderUrls.length,{impactHash:p.impactHash},o.attempt??0,r.afterCheckpoint);}let f=i.deploymentProfile?["--profile",i.deploymentProfile]:[];if(Pt(l.phase,"deployed")||(await r.runDeploy([...f,"--content-sync"]),await it(s,l,"deployed",1,{impactHash:p.impactHash},0,r.afterCheckpoint),await ze(n,"content-sync-deployed",o,"Content-sync targeted deployment completed.",{impactHash:p.impactHash})),Pt(l.phase,"invalidated")?Pt(l.phase,"verified")||await r.runInvalidate([...f,"--content-sync"]):(await r.runInvalidate([...f,"--content-sync"]),await it(s,l,"invalidated",1,{impactHash:p.impactHash},0,r.afterCheckpoint)),Pt(l.phase,"verified")||(await r.verifyDeployment(a,i,p,s.candidateManifest),await r.promoteCandidateManifest(s),await it(s,l,"verified",1,{impactHash:p.impactHash},0,r.afterCheckpoint)),g=await i.client.head({consumerId:i.consumerId,scopeFingerprint:i.scopeFingerprint,baselineId:u.baselineId,postTypes:i.scope.postTypes,includeSubsites:i.scope.includeSubsites}),g.committedSequence===l.fromSequence)await i.client.acknowledge({consumerId:i.consumerId,scopeFingerprint:i.scopeFingerprint,baselineId:u.baselineId,expectedSequence:l.fromSequence,sequence:l.toSequence,postTypes:i.scope.postTypes,includeSubsites:i.scope.includeSubsites});else if(g.committedSequence!==l.toSequence)throw new Error("Content-sync acknowledgement cursor is in an unexpected state.");await it(s,l,"acknowledged",1,{sequence:l.toSequence},0,r.afterCheckpoint),await ze(n,"content-sync-cursor-acknowledged",o,"Content-sync committed its journal cursor after verification.",{sequence:l.toSequence});let w=await i.client.head({consumerId:i.consumerId,scopeFingerprint:i.scopeFingerprint,baselineId:u.baselineId,postTypes:i.scope.postTypes,includeSubsites:i.scope.includeSubsites}),m=await Ht(s);m.rules[i.coalesceKey]={ruleId:String(c.id||""),consumerId:i.consumerId,coalesceKey:i.coalesceKey,committedSequence:l.toSequence,observedHeadSequence:w.headSequence,lag:Math.max(0,w.headSequence-l.toSequence),lastSuccessfulJobId:o.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:w.headSequence>l.toSequence,retryAttempt:0,nextRetryAt:null,lastError:null},await Vt(s,m),await r.clearDeployPlan(a),await F.rm(k.join(n,"content-sync-invalidation.json"),{force:true}),await F.rm(s.candidateManifest,{force:true}),await Wr(s);}async function Bo(e=process.argv.slice(2)){let r=e.indexOf("--capture-baseline-cutoffs");if(r>=0){let a=String(e[r+1]||"").trim();if(!a)throw new Error("--capture-baseline-cutoffs requires a release job ID.");await Ll(a);return}let n=e.indexOf("--establish-baselines");if(n>=0){let a=String(e[n+1]||"").trim();if(!a)throw new Error("--establish-baselines requires a release job ID.");await Ul(a);return}let s=e.indexOf("--job-id"),o=s>=0?String(e[s+1]||"").trim():"";if(!o)throw new Error("Content-sync requires --job-id.");await Bl(o);}Bo().catch(e=>{console.error(e instanceof Error?e.message:String(e)),process.exit(1);});