@smart-cloud/publisher-exporter 1.1.66 → 1.1.67
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/dist/content-sync.js +15 -15
- package/dist/deploy.js +6 -6
- package/dist/remote-worker-health.js +1 -1
- package/dist/remote-worker.js +3 -3
- package/package.json +1 -1
package/dist/content-sync.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import F,{readFile}from'fs/promises';import Lo from'http';import En from'https';import v from'path';import Yn,{createHash}from'crypto';import {request,chromium}from'playwright';import {gunzipSync}from'zlib';import li from'fast-glob';import Ps 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 Oo=new Set(["post-type","taxonomy","author","date","posts-page","listing"]);function He(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function Mo(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))]}function jo(e){return new URL(e).pathname.split("/").filter(Boolean).some(r=>{try{return decodeURIComponent(r).endsWith("__trashed")}catch{return r.endsWith("__trashed")}})}function vr(e){return Array.isArray(e)?e.map(vr):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[r])=>t.localeCompare(r)).map(([t,r])=>[t,vr(r)])):e}function Fo(e){return JSON.stringify(vr(e))}function De(e){return createHash("sha256").update(Fo(e)).digest("hex")}function Cn(e){return De({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 Cr(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 ke(e,t){Do(t);let r=new URL(Cr(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 Do(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 Pt(e){let t=He((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=He((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 i=`/${o.pathname.replace(/^\/+|\/+$/g,"")}`;return i==="/"?"/":`${i}/`}));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 kn(e,t,r){return De({schemaVersion:1,sourceOrigin:Cr(e),ruleId:t.trim(),scope:Pt(r)})}function xn(e){return De({schemaVersion:1,...e})}function In(e){let t=String(e.siteId??"").trim()||String(e.accountId??"").trim()||Cr(e.sourceOrigin);return `content-sync:${De({siteIdentity:t,ruleId:e.ruleId.trim(),targetFingerprint:e.targetFingerprint}).slice(0,48)}`}function An(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}`,i=r.get(o);if(!i){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}i.toSequence=s.sequence,i.after=s.after,i.operations.push(s.operation),s.correlationId&&!i.correlationIds.includes(s.correlationId)&&i.correlationIds.push(s.correlationId);}return [...r.values()].sort((s,o)=>s.fromSequence-o.fromSequence)}function vn(e){return e?.status==="publish"&&!!e.url}function Uo(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 kr(e){let t=Pt(e.scope),r=new Map;An(e.events);for(let n of e.events)for(let s of [n.before,n.after])for(let o of s?.archiveFamilies??[]){if(!Oo.has(o.kind)||!Uo(o.kind,t))continue;let i={...o,url:ke(e.sourceOrigin,o.url)};r.set(`${i.kind}|${i.url}`,i);}for(let n of t.listingPaths){let s=ke(e.sourceOrigin,n);r.set(`listing|${s}`,{kind:"listing",url:s});}return [...r.values()].sort((n,s)=>n.url.localeCompare(s.url))}function Tn(e){let t=Pt(e.scope),r=An(e.events),n=new Set,s=new Set,o=new Map,i=new Map(kr({events:e.events,scope:t,sourceOrigin:e.sourceOrigin}).map(m=>[`${m.kind}|${m.url}`,m]));for(let m of r){let w=m.before,p=m.after,h=vn(w),y=vn(p);y&&p.url&&n.add(ke(e.sourceOrigin,p.url)),h&&w.url&&(!y||w.url!==p.url)&&s.add(ke(e.sourceOrigin,w.url));}for(let m of [...e.events].sort((w,p)=>w.sequence-p.sequence)){for(let w of [m.before,m.after])for(let p of w?.resourceDeleteUrls??[])o.set(ke(e.sourceOrigin,p),"delete");for(let w of m.after?.resourceRenderUrls??[])o.set(ke(e.sourceOrigin,w),"render");}let c=[...o].filter(([,m])=>m==="render").map(([m])=>m),a=[...o].filter(([,m])=>m==="delete").map(([m])=>m),d=new Map(e.resolvedArchiveFamilies.map(m=>[`${m.kind}|${ke(e.sourceOrigin,m.url)}`,{...m,url:ke(e.sourceOrigin,m.url),pageUrls:He(m.pageUrls.map(w=>ke(e.sourceOrigin,w)))}])),l=[...i.entries()].map(([m,w])=>{let p=d.get(m);if(!p)throw new Error(`WordPress did not resolve archive family ${m}.`);return {...w,...p}}).sort((m,w)=>m.url.localeCompare(w.url)),u=He(l.flatMap(m=>m.pageUrls)),g=t.includeSitemapChain?Mo(e.sitemapUrls.map(m=>ke(e.sourceOrigin,m))):[],f={schemaVersion:1,generatedAt:new Date().toISOString(),jobId:e.jobId,ruleId:e.ruleId,consumerId:e.consumerId,coalesceKey:e.coalesceKey,scopeFingerprint:e.scopeFingerprint,targetFingerprint:e.targetFingerprint,baselineId:e.baselineId,fromSequence:e.fromSequence,toSequence:e.toSequence,eventCount:e.events.length,foldedCount:r.length,directRenderUrls:He([...n]),archiveFamilyUrls:u,sitemapUrls:g,deleteUrls:He([...s]),resourceRenderUrls:He(c),resourceDeleteUrls:He(a),archiveFamilies:l};return {...f,impactHash:De(f)}}function Rt(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(De(r)!==t)throw new Error("Content-sync impact plan hash mismatch.");for(let n of [...e.directRenderUrls,...e.deleteUrls,...e.resourceRenderUrls,...e.resourceDeleteUrls])if(jo(n))throw new Error(`Content-sync impact contains a WordPress trash alias instead of the last public URL: ${n}`)}var qo="wp-json/smartcloud-static-publisher/v1/content-sync/",Wo=25*1024*1024,Lt=class extends Error{status;code;constructor(t,r,n){super(n),this.name="ContentSyncApiError",this.status=t,this.code=r;}},qt=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"?qo:`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"),i=s.protocol==="https:"?En:Lo,c=s.protocol==="https:"?new En.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((a,d)=>{let l=i.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},u=>{let g=[],f=0;u.on("data",m=>{if(f+=m.byteLength,f>Wo){u.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(m);}),u.on("error",d),u.on("end",()=>{let m=Buffer.concat(g).toString("utf8"),w;try{w=m?JSON.parse(m):{};}catch{d(new Lt(u.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let p=u.statusCode??500;if(p<200||p>=300){d(new Lt(p,String(w.code??"request-failed"),String(w.message??`Content-sync API request failed (${p}).`)));return}a(w);});});l.on("timeout",()=>l.destroy(new Error(`Content-sync API request timed out: ${t}`))),l.on("error",d),l.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 i=n;for(let c of o.items){if(!Number.isSafeInteger(c.sequence)||c.sequence<=i||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.");i=c.sequence;}if(o.items.length>0&&o.nextSequence!==i)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 Wt(e){let t=v.resolve(e);return {state:v.join(t,"content-sync-state.json"),current:v.join(t,"content-sync-current.json"),impact:v.join(t,"content-sync-impact-plan.json"),checkpoint:v.join(t,"content-sync-checkpoint.json"),baseline:v.join(t,"content-sync-baseline.json"),activeRules:v.join(t,"content-sync-active-rules.json"),releaseCutoff:v.join(t,"content-sync-release-cutoff.json"),candidateManifest:v.join(t,"content-sync-candidate-manifest.json"),trustedManifest:v.join(t,"crawl-manifest.json"),invalidation:v.join(t,"content-sync-invalidation.json"),deployPlan:v.join(t,"deploy-plan.json")}}async function xr(e){let t=await vt(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 it(e.releaseCutoff,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function it(e,t){await F.mkdir(v.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 vt(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 Ir(e){let t=await vt(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 On(e,t){await it(e.baseline,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Ar(e){let t=await vt(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 Mn(e,t){await it(e.current,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function jn(e){let t=await vt(e.impact);if(t===null)return null;let r=t;return Rt(r),r}async function Fn(e,t){Rt(t),await it(e.impact,t);}async function Dn(e,t){await it(e.checkpoint,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Nt(e){let t=await vt(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 _t(e,t){await it(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Tr(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 Un(e,t){let r=await Ar(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 Er=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function Ct(e){let t=new Set;for(let r of [...e,...Er]){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 Ln(e){if(!e||!/^https?:\/\//i.test(e))return false;try{return $r(new URL(e))}catch{return false}}function Wn(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function Vo(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(i=>i.replace(/[^A-Za-z0-9._-]/g,"")).filter(i=>i.length>0&&i!=="."&&i!=="..");if(s.length===0)return "";let o=`/${s.join("/")}`;return n&&v.extname(o)===""&&(o+="/"),o==="/"?"":o}function Or(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,r])=>[t.trim(),String(r??"")]).filter(([t])=>t.length>0))}function Ko(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,i={};if(typeof o.targetOrigin=="string"){let a=Wn(o.targetOrigin);a&&(i.targetOrigin=a);}let c=Or(o.extraReplacements);if(Object.keys(c).length>0&&(i.extraReplacements=c),o.s3&&typeof o.s3=="object"){let a={},d=o.s3;typeof d.bucket=="string"&&(a.bucket=d.bucket.trim()),typeof d.prefix=="string"&&(a.prefix=d.prefix.trim()),typeof d.region=="string"&&(a.region=d.region.trim()),typeof d.htmlCacheControl=="string"&&(a.htmlCacheControl=d.htmlCacheControl.trim()),typeof d.assetCacheControl=="string"&&(a.assetCacheControl=d.assetCacheControl.trim()),Object.keys(a).length>0&&(i.s3=a);}if(o.cloudFront&&typeof o.cloudFront=="object"){let a={},d=o.cloudFront;typeof d.distributionId=="string"&&(a.distributionId=d.distributionId.trim()),Array.isArray(d.invalidationPaths)&&(a.invalidationPaths=d.invalidationPaths.map(l=>String(l??"").trim()).filter(l=>l.length>0)),Object.keys(a).length>0&&(i.cloudFront=a);}t[s]=i;}return t}function jr(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function Ho(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 i=String(r.deploymentProfile??"").trim(),c=String(r.url??"").trim(),a=Array.isArray(r.postTypes)?[...new Set(r.postTypes.map(l=>String(l??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],d=Array.isArray(r.listingPaths)?[...new Set(r.listingPaths.map(l=>String(l??"").trim()).filter(Boolean))]:[];return n==="content-sync"&&a.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")&&i?{deploymentProfile:i}:{},...c?{url:c}:{},...a.length>0?{postTypes:a}:{},...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 Nn(e){let t=e&&typeof e=="object"?e:{},r=Array.isArray(t.rules)?t.rules.map((n,s)=>Ho(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 zo(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),i=t.rewrite&&typeof t.rewrite=="object"?t.rewrite:{},c=t.deploy&&typeof t.deploy=="object"?t.deploy:{},a=(d,l)=>({enabled:typeof d.enabled=="boolean"?d.enabled:r.enabled===true,concurrency:Math.min(100,Math.max(1,Number(d.concurrency||l.concurrency))),batchSize:Math.min(500,Math.max(1,Number(d.batchSize||l.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:a(i,{concurrency:8,batchSize:25}),deploy:a(c,{concurrency:8,batchSize:50})}}function Jo(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 Fr(e){let t=e&&typeof e=="object"?e:{},r=String(t.accountId??"").trim(),n=String(t.siteId??"").trim(),s=t.subscriber===true,o=Jo(t.siteSettings),i={...o??{},...r?{accountId:o?.accountId??r}:{},...n?{siteId:o?.siteId??n}:{},...o?.subscriber===true||s?{subscriber:true}:{}},c={},a=String(t.apiBase??"").trim();a&&(c.apiBase=a);let d=String(t.runtimeToken??t.nonce??"").trim();d&&(c.runtimeToken=d);let l=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();l&&(c.virtualAssetBaseUrl=l),Object.keys(i).length>0&&(c.siteSettings=i);let u=jr(t.subscriptionType);return u&&(c.subscriptionType=u),c}function Qo(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function _n(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function Go(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 Bn(e,t,r=5){let n=new URL(_n(e)),s=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),o=n.protocol==="http:"?Lo:En,i=Go(e,t);return await new Promise((c,a)=>{let d=o.request(n,{method:s,headers:Object.fromEntries(i.entries()),...n.protocol==="https:"?{rejectUnauthorized:false}:{}},l=>{let u=[];l.on("data",g=>{u.push(Buffer.isBuffer(g)?g:Buffer.from(g));}),l.on("error",a),l.on("end",()=>{let g=l.statusCode??0,f=l.headers.location;if(r>0&&f&&[301,302,303,307,308].includes(g)){c(Bn(new URL(f,n),{method:g===303?"GET":s},r-1));return}let m=new Headers;for(let[w,p]of Object.entries(l.headers))Array.isArray(p)?m.set(w,p.join(", ")):typeof p=="string"&&m.set(w,p);c(new Response(Buffer.concat(u),{status:g,statusText:l.statusMessage??"",headers:m}));});});d.on("error",a),d.end();})}function Zo(e){let t=Fr(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;Qo(e.sourceOrigin);let i=globalThis,a={...i.WpSuite??{},siteSettings:r,uploadUrl:o};return t.apiBase&&(a.apiBase=t.apiBase),i.WpSuite=a,true}async function Yo(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!Zo(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(a,d)=>{let l=_n(a);if(l.startsWith(t)){let u=(()=>{try{return new URL(l).pathname}catch{return l}})();try{let g=e.ignoreHttpsErrors&&l.startsWith("https://")?await Bn(a,d):await r(a,d);return n.push({path:u,status:g.status,ok:g.ok,error:null}),g}catch(g){throw n.push({path:u,status:null,ok:!1,error:g instanceof Error?g.message:String(g)}),g}}return r(a,d)}));let{getConfig:s}=await import('@smart-cloud/wpsuite-core'),o=await s("publisher"),i=o&&typeof o=="object"?o:null,c=jr(i?.subscriptionType);return {remote:i,diagnostic:{status:i?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:c??null,error:i?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 Xo(e,t){let r=Ko(t?.deploymentProfiles),n=String(t?.defaultDeploymentProfile??"").trim(),s=r[n]?n:"",o=jr(t?.subscriptionType),i=Fr(e.wpsuite);return {...e,scheduler:Nn(t?.scheduler),deploymentProfiles:r,defaultDeploymentProfile:s,...o?{subscriptionType:o}:{},wpsuite:{...i,...i.siteSettings||o?{siteSettings:{...i.siteSettings??{},...i.siteSettings?.subscriber===true||o==="PROFESSIONAL"||o==="AGENCY"?{subscriber:true}:{}}}:{},...o?{subscriptionType:o}:{}}}}function Mr(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 ei(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?v.resolve(e):""}function qn(e,t,r){let n=String(t||"").trim();return n&&v.isAbsolute(n)?v.resolve(n):v.resolve(e,Mr(n,r))}async function xe(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=Wn(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=Vo(s.generated404RequestPath),s.sitemapPaths||=[...Er],s.allowedAssetHosts||=[],s.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],s.blockedPathPrefixes||=[],s.blockedSearchFragments||=[];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=Or(s.extraReplacements),s.postCrawlCopyMap=Or(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=Fr(s.wpsuite),s.scheduler=Nn(void 0),s.remoteWorkers=zo(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 i=await Yo(s);t.onRemotePublisherConfigDiagnostic?.(i.diagnostic);let c=Xo(s,i.remote),a=ei(),d=a?v.resolve(a,".."):"";return d?(c.outputDir=qn(d,c.outputDir,"export"),c.logDir=qn(d,c.logDir,"logs")):(v.isAbsolute(c.outputDir)||(c.outputDir=Mr(c.outputDir,"export")),v.isAbsolute(c.logDir)||(c.logDir=Mr(c.logDir,"logs"))),c}var ti=".deploy-plan.json",ri="deploy-plan.json",ni=".crawl-incomplete.json",si="crawl-incomplete.json";function Vn(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?v.resolve(e):""}function Kn(e){let t=String(e||"").replace(/\\/g,"/").replace(/^\/+/,"").trim(),r=v.posix.normalize(t);return !t||!r||r==="."||/^[a-zA-Z]:/.test(r)||r.startsWith("../")||r===".."?null:r}function oi(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:`${v.posix.normalize(r.pathname)}${r.search}`}catch{return null}}function Ie(e){return [...new Set(e.map(Kn).filter(t=>t!==null))]}function Hn(e,t){let r={generatedAt:String(t.generatedAt||new Date().toISOString()),outputDir:v.resolve(e.outputDir),runMode:t.runMode,crawlMode:t.crawlMode,fullSyncRequired:!!t.fullSyncRequired,changedFiles:Ie(t.changedFiles||[]),deletedFiles:Ie(t.deletedFiles||[]),rewriteTargets:Ie(t.rewriteTargets||[]),revalidateFiles:Ie(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:Ie(t.uploadGroups?.assets||[]),content:Ie(t.uploadGroups?.content||[]),listings:Ie(t.uploadGroups?.listings||[]),resources:Ie(t.uploadGroups?.resources||[]),sitemapLeaves:Ie(t.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:Ie(t.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((t.invalidationPaths||[]).map(oi).filter(n=>n!==null))].sort()}:{schemaVersion:1,...r,runMode:t.runMode}}function ii(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 Dr(e){let t=v.join(v.resolve(e.outputDir),ti),r=Vn();return r?[v.join(r,ri),t]:[t]}function Ur(e){let t=v.join(v.resolve(e.outputDir),ni),r=Vn();return r?[v.join(r,si),t]:[t]}function fe(e,t){let r=v.resolve(e),n=v.resolve(t),s=v.relative(r,n).replace(/\\/g,"/");return Kn(s)}async function at(e){let t=v.resolve(e.outputDir);for(let r of Dr(e))try{let n=await F.readFile(r,"utf8"),s=JSON.parse(n);if(!ii(s)||v.resolve(s.outputDir)!==t)continue;return Hn(e,s)}catch{}return null}async function Lr(e,t){let r=Hn(e,t);for(let n of Dr(e)){await F.mkdir(v.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 Je(e){for(let t of Dr(e))try{await F.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}async function zn(e,t){let r={schemaVersion:1,startedAt:String(t.startedAt||new Date().toISOString()),outputDir:v.resolve(e.outputDir)};for(let n of Ur(e)){await F.mkdir(v.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 Jn(e){let t=v.resolve(e.outputDir);for(let r of Ur(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"||v.resolve(s.outputDir)!==t)throw new Error(`Invalid incomplete crawl marker: ${r}`);return {schemaVersion:1,startedAt:s.startedAt,outputDir:t}}return null}async function Qn(e){for(let t of Ur(e))try{await F.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}function Te(e,t=10){return Yn.createHash("sha1").update(e).digest("hex").slice(0,t)}async function ai(e){await F.mkdir(v.dirname(e),{recursive:true});}async function Le(e,t,r){await ai(e);let n=v.join(v.dirname(e),`.${v.basename(e)}.${process.pid}.${Date.now()}.${Yn.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 Ee(e){return [".html",".htm",".css",".js",".mjs",".json",".xml",".xsl",".txt",".svg",".map",".enc",".jws"].includes(v.extname(e).toLowerCase())}function qe(e){return e.replace(/"/g,'"').replace(/"/g,'"').replace(/'/g,"'").replace(/'/g,"'").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function ct(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">")}function Bt(e){return e.replace(/\//g,"\\/")}function Gn(e){return e.replace(/\//g,"\\\\/")}function ci(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 Zn(e,t,r){if(!t)return;e[t]=r;let n=Bt(t),s=Bt(r);e[n]=s;let o=Gn(t),i=Gn(r);e[o]=i;let c=ct(t),a=ct(r);e[c]=a;let d=ct(n),l=ct(s);e[d]=l;let u=ct(o),g=ct(i);e[u]=g;}function Vt(e,t,r){Zn(e,t,r);let n=ci(t);n&&n!==t&&Zn(e,n,r);}function qr(e,t){let r=t.startsWith("/")?t:`/${t}`,n=(e||"").replace(/\/$/,"");return !n||n==="."||n==="/"?r:`${n}${r}`}function Xn(e,t){try{return new URL(e,t==="."?"https://relative.invalid":t).pathname}catch{return e.startsWith("/")?e:`/${e.replace(/^\.\//,"")}`}}function es(e,t,r){if(!t)return r;let n=v.dirname(v.resolve(t)),s=v.resolve(e,r.replace(/^\/+/,"")),o=v.relative(n,s).replace(/\\/g,"/");return o?(o.startsWith(".")||(o=`./${o}`),o):"."}async function _r(e,t){return (t?[...new Set(t)].map(n=>v.resolve(n)).filter(n=>n.startsWith(v.resolve(e.outputDir))).map(n=>v.relative(e.outputDir,n).replace(/\\/g,"/")).filter(n=>n.length>0):await li(["**/*"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(n=>n!=="asset-map.json"&&Ee(n))}var mi=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],gi=new Set([".html",".htm"]),ts="WPSuite.io Static Publisher",fi=ts.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function hi(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function wi(e){return e.startsWith("//")||e.startsWith("\\/\\/")||e.startsWith("\\\\/\\\\/")}function yi(e,t,r){return wi(t)?e.replace(new RegExp(`(?<!:)${hi(t)}`,"g"),r):e.split(t).join(r)}function bi(e,t,r){if(e.wpsuite?.siteSettings?.subscriber===true||e.wpsuite?.subscriptionType==="PROFESSIONAL"||e.wpsuite?.subscriptionType==="AGENCY"||!r)return false;let s=v.extname(r).toLowerCase();return gi.has(s)?/<head\b|<html\b|<!doctype html/i.test(t):false}function Si(e){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${fi}\\2)[^>]*\\/?>`,"i").test(e))return e;let t=`<meta name="generator" content="${ts}" />`;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 _o from'http';import On from'https';import v from'path';import es,{createHash}from'crypto';import {request,chromium}from'playwright';import {gunzipSync}from'zlib';import mi from'fast-glob';import vs 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 Do=new Set(["post-type","taxonomy","author","date","posts-page","listing"]);function He(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function Uo(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))]}function Lo(e){return new URL(e).pathname.split("/").filter(Boolean).some(r=>{try{return decodeURIComponent(r).endsWith("__trashed")}catch{return r.endsWith("__trashed")}})}function Cr(e){return Array.isArray(e)?e.map(Cr):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[r])=>t.localeCompare(r)).map(([t,r])=>[t,Cr(r)])):e}function qo(e){return JSON.stringify(Cr(e))}function De(e){return createHash("sha256").update(qo(e)).digest("hex")}function xn(e){return De({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 kr(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 ke(e,t){Wo(t);let r=new URL(kr(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 Wo(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 Pt(e){let t=He((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=He((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 i=`/${o.pathname.replace(/^\/+|\/+$/g,"")}`;return i==="/"?"/":`${i}/`}));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 In(e,t,r){return De({schemaVersion:1,sourceOrigin:kr(e),ruleId:t.trim(),scope:Pt(r)})}function Tn(e){return De({schemaVersion:1,...e})}function An(e){let t=String(e.siteId??"").trim()||String(e.accountId??"").trim()||kr(e.sourceOrigin);return `content-sync:${De({siteIdentity:t,ruleId:e.ruleId.trim(),targetFingerprint:e.targetFingerprint}).slice(0,48)}`}function En(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}`,i=r.get(o);if(!i){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}i.toSequence=s.sequence,i.after=s.after,i.operations.push(s.operation),s.correlationId&&!i.correlationIds.includes(s.correlationId)&&i.correlationIds.push(s.correlationId);}return [...r.values()].sort((s,o)=>s.fromSequence-o.fromSequence)}function kn(e){return e?.status==="publish"&&!!e.url}function No(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 xr(e){let t=Pt(e.scope),r=new Map;En(e.events);for(let n of e.events)for(let s of [n.before,n.after])for(let o of s?.archiveFamilies??[]){if(!Do.has(o.kind)||!No(o.kind,t))continue;let i={...o,url:ke(e.sourceOrigin,o.url)};r.set(`${i.kind}|${i.url}`,i);}for(let n of t.listingPaths){let s=ke(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=Pt(e.scope),r=En(e.events),n=new Set,s=new Set,o=new Map,i=new Map(xr({events:e.events,scope:t,sourceOrigin:e.sourceOrigin}).map(m=>[`${m.kind}|${m.url}`,m]));for(let m of r){let w=m.before,p=m.after,h=kn(w),y=kn(p);y&&p.url&&n.add(ke(e.sourceOrigin,p.url)),h&&w.url&&(!y||w.url!==p.url)&&s.add(ke(e.sourceOrigin,w.url));}for(let m of [...e.events].sort((w,p)=>w.sequence-p.sequence)){for(let w of [m.before,m.after])for(let p of w?.resourceDeleteUrls??[])o.set(ke(e.sourceOrigin,p),"delete");for(let w of m.after?.resourceRenderUrls??[])o.set(ke(e.sourceOrigin,w),"render");}let c=[...o].filter(([,m])=>m==="render").map(([m])=>m),a=[...o].filter(([,m])=>m==="delete").map(([m])=>m),d=new Map(e.resolvedArchiveFamilies.map(m=>[`${m.kind}|${ke(e.sourceOrigin,m.url)}`,{...m,url:ke(e.sourceOrigin,m.url),pageUrls:He(m.pageUrls.map(w=>ke(e.sourceOrigin,w)))}])),l=[...i.entries()].map(([m,w])=>{let p=d.get(m);if(!p)throw new Error(`WordPress did not resolve archive family ${m}.`);return {...w,...p}}).sort((m,w)=>m.url.localeCompare(w.url)),u=He(l.flatMap(m=>m.pageUrls)),g=t.includeSitemapChain?Uo(e.sitemapUrls.map(m=>ke(e.sourceOrigin,m))):[],f={schemaVersion:1,generatedAt:new Date().toISOString(),jobId:e.jobId,ruleId:e.ruleId,consumerId:e.consumerId,coalesceKey:e.coalesceKey,scopeFingerprint:e.scopeFingerprint,targetFingerprint:e.targetFingerprint,baselineId:e.baselineId,fromSequence:e.fromSequence,toSequence:e.toSequence,eventCount:e.events.length,foldedCount:r.length,directRenderUrls:He([...n]),archiveFamilyUrls:u,sitemapUrls:g,deleteUrls:He([...s]),resourceRenderUrls:He(c),resourceDeleteUrls:He(a),archiveFamilies:l};return {...f,impactHash:De(f)}}function Rt(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(De(r)!==t)throw new Error("Content-sync impact plan hash mismatch.");for(let n of [...e.directRenderUrls,...e.deleteUrls,...e.resourceRenderUrls,...e.resourceDeleteUrls])if(Lo(n))throw new Error(`Content-sync impact contains a WordPress trash alias instead of the last public URL: ${n}`)}var Bo="wp-json/smartcloud-static-publisher/v1/content-sync/",Vo=25*1024*1024,Lt=class extends Error{status;code;constructor(t,r,n){super(n),this.name="ContentSyncApiError",this.status=t,this.code=r;}},qt=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"?Bo:`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"),i=s.protocol==="https:"?On:_o,c=s.protocol==="https:"?new On.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((a,d)=>{let l=i.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},u=>{let g=[],f=0;u.on("data",m=>{if(f+=m.byteLength,f>Vo){u.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(m);}),u.on("error",d),u.on("end",()=>{let m=Buffer.concat(g).toString("utf8"),w;try{w=m?JSON.parse(m):{};}catch{d(new Lt(u.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let p=u.statusCode??500;if(p<200||p>=300){d(new Lt(p,String(w.code??"request-failed"),String(w.message??`Content-sync API request failed (${p}).`)));return}a(w);});});l.on("timeout",()=>l.destroy(new Error(`Content-sync API request timed out: ${t}`))),l.on("error",d),l.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 i=n;for(let c of o.items){if(!Number.isSafeInteger(c.sequence)||c.sequence<=i||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.");i=c.sequence;}if(o.items.length>0&&o.nextSequence!==i)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 Wt(e){let t=v.resolve(e);return {state:v.join(t,"content-sync-state.json"),current:v.join(t,"content-sync-current.json"),impact:v.join(t,"content-sync-impact-plan.json"),checkpoint:v.join(t,"content-sync-checkpoint.json"),baseline:v.join(t,"content-sync-baseline.json"),activeRules:v.join(t,"content-sync-active-rules.json"),releaseCutoff:v.join(t,"content-sync-release-cutoff.json"),candidateManifest:v.join(t,"content-sync-candidate-manifest.json"),trustedManifest:v.join(t,"crawl-manifest.json"),invalidation:v.join(t,"content-sync-invalidation.json"),deployPlan:v.join(t,"deploy-plan.json")}}async function Ir(e){let t=await vt(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 Mn(e,t){await it(e.releaseCutoff,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function it(e,t){await F.mkdir(v.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 vt(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 Tr(e){let t=await vt(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 jn(e,t){await it(e.baseline,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Ar(e){let t=await vt(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 Fn(e,t){await it(e.current,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Dn(e){let t=await vt(e.impact);if(t===null)return null;let r=t;return Rt(r),r}async function Un(e,t){Rt(t),await it(e.impact,t);}async function Ln(e,t){await it(e.checkpoint,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Nt(e){let t=await vt(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 _t(e,t){await it(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Er(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 Ar(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 $r=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function Ct(e){let t=new Set;for(let r of [...e,...$r]){let n=String(r||"").trim();n&&t.add(n);}return [...t]}function Or(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function Wn(e){if(!e||!/^https?:\/\//i.test(e))return false;try{return Or(new URL(e))}catch{return false}}function _n(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function Jo(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(i=>i.replace(/[^A-Za-z0-9._-]/g,"")).filter(i=>i.length>0&&i!=="."&&i!=="..");if(s.length===0)return "";let o=`/${s.join("/")}`;return n&&v.extname(o)===""&&(o+="/"),o==="/"?"":o}function Mr(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,r])=>[t.trim(),String(r??"")]).filter(([t])=>t.length>0))}function Qo(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,i={};if(typeof o.targetOrigin=="string"){let a=_n(o.targetOrigin);a&&(i.targetOrigin=a);}let c=Mr(o.extraReplacements);if(Object.keys(c).length>0&&(i.extraReplacements=c),o.s3&&typeof o.s3=="object"){let a={},d=o.s3;typeof d.bucket=="string"&&(a.bucket=d.bucket.trim()),typeof d.prefix=="string"&&(a.prefix=d.prefix.trim()),typeof d.region=="string"&&(a.region=d.region.trim()),typeof d.htmlCacheControl=="string"&&(a.htmlCacheControl=d.htmlCacheControl.trim()),typeof d.assetCacheControl=="string"&&(a.assetCacheControl=d.assetCacheControl.trim()),Object.keys(a).length>0&&(i.s3=a);}if(o.cloudFront&&typeof o.cloudFront=="object"){let a={},d=o.cloudFront;typeof d.distributionId=="string"&&(a.distributionId=d.distributionId.trim()),Array.isArray(d.invalidationPaths)&&(a.invalidationPaths=d.invalidationPaths.map(l=>String(l??"").trim()).filter(l=>l.length>0)),Object.keys(a).length>0&&(i.cloudFront=a);}t[s]=i;}return t}function Fr(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function Go(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 i=String(r.deploymentProfile??"").trim(),c=String(r.url??"").trim(),a=Array.isArray(r.postTypes)?[...new Set(r.postTypes.map(l=>String(l??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],d=Array.isArray(r.listingPaths)?[...new Set(r.listingPaths.map(l=>String(l??"").trim()).filter(Boolean))]:[];return n==="content-sync"&&a.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")&&i?{deploymentProfile:i}:{},...c?{url:c}:{},...a.length>0?{postTypes:a}:{},...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 Bn(e){let t=e&&typeof e=="object"?e:{},r=Array.isArray(t.rules)?t.rules.map((n,s)=>Go(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 Zo(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),i=t.rewrite&&typeof t.rewrite=="object"?t.rewrite:{},c=t.deploy&&typeof t.deploy=="object"?t.deploy:{},a=(d,l)=>({enabled:typeof d.enabled=="boolean"?d.enabled:r.enabled===true,concurrency:Math.min(100,Math.max(1,Number(d.concurrency||l.concurrency))),batchSize:Math.min(500,Math.max(1,Number(d.batchSize||l.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:a(i,{concurrency:8,batchSize:25}),deploy:a(c,{concurrency:8,batchSize:50})}}function Yo(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 Dr(e){let t=e&&typeof e=="object"?e:{},r=String(t.accountId??"").trim(),n=String(t.siteId??"").trim(),s=t.subscriber===true,o=Yo(t.siteSettings),i={...o??{},...r?{accountId:o?.accountId??r}:{},...n?{siteId:o?.siteId??n}:{},...o?.subscriber===true||s?{subscriber:true}:{}},c={},a=String(t.apiBase??"").trim();a&&(c.apiBase=a);let d=String(t.runtimeToken??t.nonce??"").trim();d&&(c.runtimeToken=d);let l=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();l&&(c.virtualAssetBaseUrl=l),Object.keys(i).length>0&&(c.siteSettings=i);let u=Fr(t.subscriptionType);return u&&(c.subscriptionType=u),c}function Xo(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function Vn(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function ei(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 Kn(e,t,r=5){let n=new URL(Vn(e)),s=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),o=n.protocol==="http:"?_o:On,i=ei(e,t);return await new Promise((c,a)=>{let d=o.request(n,{method:s,headers:Object.fromEntries(i.entries()),...n.protocol==="https:"?{rejectUnauthorized:false}:{}},l=>{let u=[];l.on("data",g=>{u.push(Buffer.isBuffer(g)?g:Buffer.from(g));}),l.on("error",a),l.on("end",()=>{let g=l.statusCode??0,f=l.headers.location;if(r>0&&f&&[301,302,303,307,308].includes(g)){c(Kn(new URL(f,n),{method:g===303?"GET":s},r-1));return}let m=new Headers;for(let[w,p]of Object.entries(l.headers))Array.isArray(p)?m.set(w,p.join(", ")):typeof p=="string"&&m.set(w,p);c(new Response(Buffer.concat(u),{status:g,statusText:l.statusMessage??"",headers:m}));});});d.on("error",a),d.end();})}function ti(e){let t=Dr(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;Xo(e.sourceOrigin);let i=globalThis,a={...i.WpSuite??{},siteSettings:r,uploadUrl:o};return t.apiBase&&(a.apiBase=t.apiBase),i.WpSuite=a,true}async function ri(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!ti(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(a,d)=>{let l=Vn(a);if(l.startsWith(t)){let u=(()=>{try{return new URL(l).pathname}catch{return l}})();try{let g=e.ignoreHttpsErrors&&l.startsWith("https://")?await Kn(a,d):await r(a,d);return n.push({path:u,status:g.status,ok:g.ok,error:null}),g}catch(g){throw n.push({path:u,status:null,ok:!1,error:g instanceof Error?g.message:String(g)}),g}}return r(a,d)}));let{getConfig:s}=await import('@smart-cloud/wpsuite-core'),o=await s("publisher"),i=o&&typeof o=="object"?o:null,c=Fr(i?.subscriptionType);return {remote:i,diagnostic:{status:i?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:c??null,error:i?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 ni(e,t){let r=Qo(t?.deploymentProfiles),n=String(t?.defaultDeploymentProfile??"").trim(),s=r[n]?n:"",o=Fr(t?.subscriptionType),i=Dr(e.wpsuite);return {...e,scheduler:Bn(t?.scheduler),deploymentProfiles:r,defaultDeploymentProfile:s,...o?{subscriptionType:o}:{},wpsuite:{...i,...i.siteSettings||o?{siteSettings:{...i.siteSettings??{},...i.siteSettings?.subscriber===true||o==="PROFESSIONAL"||o==="AGENCY"?{subscriber:true}:{}}}:{},...o?{subscriptionType:o}:{}}}}function jr(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 si(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?v.resolve(e):""}function Nn(e,t,r){let n=String(t||"").trim();return n&&v.isAbsolute(n)?v.resolve(n):v.resolve(e,jr(n,r))}async function xe(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=_n(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=Jo(s.generated404RequestPath),s.sitemapPaths||=[...$r],s.allowedAssetHosts||=[],s.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],s.blockedPathPrefixes||=[],s.blockedSearchFragments||=[];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=Mr(s.extraReplacements),s.postCrawlCopyMap=Mr(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=Dr(s.wpsuite),s.scheduler=Bn(void 0),s.remoteWorkers=Zo(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 i=await ri(s);t.onRemotePublisherConfigDiagnostic?.(i.diagnostic);let c=ni(s,i.remote),a=si(),d=a?v.resolve(a,".."):"";return d?(c.outputDir=Nn(d,c.outputDir,"export"),c.logDir=Nn(d,c.logDir,"logs")):(v.isAbsolute(c.outputDir)||(c.outputDir=jr(c.outputDir,"export")),v.isAbsolute(c.logDir)||(c.logDir=jr(c.logDir,"logs"))),c}var oi=".deploy-plan.json",ii="deploy-plan.json",ai=".crawl-incomplete.json",ci="crawl-incomplete.json";function Hn(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?v.resolve(e):""}function zn(e){let t=String(e||"").replace(/\\/g,"/").replace(/^\/+/,"").trim(),r=v.posix.normalize(t);return !t||!r||r==="."||/^[a-zA-Z]:/.test(r)||r.startsWith("../")||r===".."?null:r}function li(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:`${v.posix.normalize(r.pathname)}${r.search}`}catch{return null}}function Ie(e){return [...new Set(e.map(zn).filter(t=>t!==null))]}function Jn(e,t){let r={generatedAt:String(t.generatedAt||new Date().toISOString()),outputDir:v.resolve(e.outputDir),runMode:t.runMode,crawlMode:t.crawlMode,fullSyncRequired:!!t.fullSyncRequired,changedFiles:Ie(t.changedFiles||[]),deletedFiles:Ie(t.deletedFiles||[]),rewriteTargets:Ie(t.rewriteTargets||[]),revalidateFiles:Ie(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:Ie(t.uploadGroups?.assets||[]),content:Ie(t.uploadGroups?.content||[]),listings:Ie(t.uploadGroups?.listings||[]),resources:Ie(t.uploadGroups?.resources||[]),sitemapLeaves:Ie(t.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:Ie(t.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((t.invalidationPaths||[]).map(li).filter(n=>n!==null))].sort()}:{schemaVersion:1,...r,runMode:t.runMode}}function ui(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 Ur(e){let t=v.join(v.resolve(e.outputDir),oi),r=Hn();return r?[v.join(r,ii),t]:[t]}function Lr(e){let t=v.join(v.resolve(e.outputDir),ai),r=Hn();return r?[v.join(r,ci),t]:[t]}function he(e,t){let r=v.resolve(e),n=v.resolve(t),s=v.relative(r,n).replace(/\\/g,"/");return zn(s)}async function at(e){let t=v.resolve(e.outputDir);for(let r of Ur(e))try{let n=await F.readFile(r,"utf8"),s=JSON.parse(n);if(!ui(s)||v.resolve(s.outputDir)!==t)continue;return Jn(e,s)}catch{}return null}async function qr(e,t){let r=Jn(e,t);for(let n of Ur(e)){await F.mkdir(v.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 Je(e){for(let t of Ur(e))try{await F.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}async function Qn(e,t){let r={schemaVersion:1,startedAt:String(t.startedAt||new Date().toISOString()),outputDir:v.resolve(e.outputDir)};for(let n of Lr(e)){await F.mkdir(v.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 Gn(e){let t=v.resolve(e.outputDir);for(let r of Lr(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"||v.resolve(s.outputDir)!==t)throw new Error(`Invalid incomplete crawl marker: ${r}`);return {schemaVersion:1,startedAt:s.startedAt,outputDir:t}}return null}async function Zn(e){for(let t of Lr(e))try{await F.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}function Ae(e,t=10){return es.createHash("sha1").update(e).digest("hex").slice(0,t)}async function di(e){await F.mkdir(v.dirname(e),{recursive:true});}async function Le(e,t,r){await di(e);let n=v.join(v.dirname(e),`.${v.basename(e)}.${process.pid}.${Date.now()}.${es.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 Ee(e){return [".html",".htm",".css",".js",".mjs",".json",".xml",".xsl",".txt",".svg",".map",".enc",".jws"].includes(v.extname(e).toLowerCase())}function qe(e){return e.replace(/"/g,'"').replace(/"/g,'"').replace(/'/g,"'").replace(/'/g,"'").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function ct(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">")}function Bt(e){return e.replace(/\//g,"\\/")}function Yn(e){return e.replace(/\//g,"\\\\/")}function pi(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 Xn(e,t,r){if(!t)return;e[t]=r;let n=Bt(t),s=Bt(r);e[n]=s;let o=Yn(t),i=Yn(r);e[o]=i;let c=ct(t),a=ct(r);e[c]=a;let d=ct(n),l=ct(s);e[d]=l;let u=ct(o),g=ct(i);e[u]=g;}function Vt(e,t,r){Xn(e,t,r);let n=pi(t);n&&n!==t&&Xn(e,n,r);}function Wr(e,t){let r=t.startsWith("/")?t:`/${t}`,n=(e||"").replace(/\/$/,"");return !n||n==="."||n==="/"?r:`${n}${r}`}function ts(e,t){try{return new URL(e,t==="."?"https://relative.invalid":t).pathname}catch{return e.startsWith("/")?e:`/${e.replace(/^\.\//,"")}`}}function rs(e,t,r){if(!t)return r;let n=v.dirname(v.resolve(t)),s=v.resolve(e,r.replace(/^\/+/,"")),o=v.relative(n,s).replace(/\\/g,"/");return o?(o.startsWith(".")||(o=`./${o}`),o):"."}async function Br(e,t){return (t?[...new Set(t)].map(n=>v.resolve(n)).filter(n=>n.startsWith(v.resolve(e.outputDir))).map(n=>v.relative(e.outputDir,n).replace(/\\/g,"/")).filter(n=>n.length>0):await mi(["**/*"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(n=>n!=="asset-map.json"&&Ee(n))}var wi=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],yi=new Set([".html",".htm"]),ns="WPSuite.io Static Publisher",bi=ns.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function Si(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function Pi(e){return e.startsWith("//")||e.startsWith("\\/\\/")||e.startsWith("\\\\/\\\\/")}function Ri(e,t,r){return Pi(t)?e.replace(new RegExp(`(?<!:)${Si(t)}`,"g"),r):e.split(t).join(r)}function vi(e,t,r){if(e.wpsuite?.siteSettings?.subscriber===true||e.wpsuite?.subscriptionType==="PROFESSIONAL"||e.wpsuite?.subscriptionType==="AGENCY"||!r)return false;let s=v.extname(r).toLowerCase();return yi.has(s)?/<head\b|<html\b|<!doctype html/i.test(t):false}function Ci(e){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${bi}\\2)[^>]*\\/?>`,"i").test(e))return e;let t=`<meta name="generator" content="${ns}" />`;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
|
|
5
|
-
`,"utf8");}function
|
|
6
|
-
`,"utf8");});}enqueueJsonLine(t,r){this.enqueueTask(()=>
|
|
7
|
-
`,"utf8");});}enqueueJsonLine(t,r){this.enqueueTask(()=>
|
|
4
|
+
`;return e.replace(/<head\b[^>]*>/i,s=>`${s}${n} ${t}`)}function ki(e){let t=qe(e).trim();return t.startsWith("{")||t.startsWith("[")||t.includes("\\/")||/"@context"|"@type"/.test(t)}function xi(e,t){let r=[...new Set(t)].filter(o=>o.includes("/")&&!/\\+\//.test(o)).map(o=>[o,Bt(o)]).filter(([o,i])=>o!==i).sort((o,i)=>i[0].length-o[0].length);if(!r.length)return e;let n=o=>{let i=o;for(let[c,a]of r)i=i.split(c).join(a);return i},s=e.replace(/(<script\b[^>]*\btype=["']application\/(?:ld\+)?json["'][^>]*>)([\s\S]*?)(<\/script>)/gi,(o,i,c,a)=>`${i}${n(c)}${a}`);return s=s.replace(/(<meta\b[^>]*\bcontent=(['"]))([\s\S]*?)(\2[^>]*>)/gi,(o,i,c,a,d)=>ki(a)?`${i}${n(a)}${d}`:o),s}function Ii(e,t){if(!t)return null;let r=v.dirname(v.resolve(t)),n=v.resolve(e.outputDir),s=v.relative(r,n).replace(/\\/g,"/");return s?(s.startsWith(".")||(s=`./${s}`),s.endsWith("/")?s:`${s}/`):"./"}function Ti(e,t,r){let n=Ii(t,r);if(!n)return e;let s=e;for(let o of wi){let i=o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),c=new RegExp(`(?:\\.{1,}/)+${i}`,"g"),a=new RegExp(`(?<!\\.)/${i}`,"g");s=s.replace(c,`${n}${o}`).replace(a,`${n}${o}`);let d=o.replace(/\//g,"\\/"),l=n.replace(/\//g,"\\/"),u=d.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),g=new RegExp(`(?:\\.{1,}\\\\/)+${u}`,"g"),f=new RegExp(`(?:\\.{1,}(?:\\\\/|\\\\))+${u}`,"g"),m=new RegExp(`(?<![\\.\\\\])\\/${u}`,"g");s=s.replace(g,`${l}${d}`).replace(f,`${l}${d}`).replace(m,`${l}${d}`);}return s}function _r(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:ts(t,e.targetOrigin),o=n?`${n.search}${n.hash}`:"";return e.urlRewriteMode==="root-relative"?`${s}${o}`:`${rs(e.outputDir,r,s)}${o}`}function Nr(e,t,r,n,s){Vt(e,r,_r(t,n,s));}function Ai(e,t,r,n,s){if(!r||r===n)return;Vt(e,r,n);let o=_r(t,r,s),i=_r(t,n,s);Vt(e,o,i);}function Vr(e,t,r,n,s={}){let o=e,i={};Nr(i,t,t.sourceOrigin,t.targetOrigin,n);for(let[a,d]of Object.entries(t.extraReplacements))Nr(i,t,a,d,n);for(let[a,d]of Object.entries(r))Nr(i,t,a,d,n);for(let[a,d]of Object.entries(s)){let l=r[a];!l||l===d||Ai(i,t,d,l,n);}let c=Object.entries(i).sort((a,d)=>d[0].length-a[0].length);for(let[a,d]of c)o=Ri(o,a,d);return t.urlRewriteMode!=="absolute"&&(o=Ti(o,t,n)),o=xi(o,c.map(([,a])=>a)),vi(t,o,n)&&(o=Ci(o)),o}async function Ei(e,t,r,n){let s=v.join(e.outputDir,n),o=v.extname(s).toLowerCase(),i;try{i=await F.readFile(s,"utf8");}catch(d){if(d.code!=="ENOENT")throw d;return {changed:false}}let a=Vr(i,e,t,o===".js"||o===".mjs"?void 0:s,r);return a!==i?(await Le(s,a,"utf8"),{changed:true}):{changed:false}}async function $i(e,t){return await new Promise((r,n)=>{let s=a=>{if(c(),a?.error){n(new Error(a.error));return}r({changed:!!a?.changed});},o=a=>{c(),n(a);},i=a=>{c(),a!==0&&n(new Error(`Rewrite worker exited with code ${a}.`));},c=()=>{e.off("message",s),e.off("error",o),e.off("exit",i);};e.on("message",s),e.on("error",o),e.on("exit",i),e.postMessage({file:t});})}async function Kt(e,t,r={}){let n=await Br(e,r.files),s=0;if(n.length===0)return 0;let o=Math.max(1,Number(e.rewriteConcurrency||e.assetDownloadConcurrency||1)),i=Math.max(1,Math.min(o,n.length,availableParallelism()));await r.onStart?.({totalFiles:n.length,workerCount:i});let c=0,a=0,d=null;if(i===1){for(let u of n){let g=await Ei(e,t,r.previousAssetMap??{},u);g.changed&&(s+=1),a+=1,await r.onProgress?.({index:a,totalFiles:n.length,changedTextFiles:s,file:u,changed:g.changed});}return s}let l=Array.from({length:i},async()=>{let u=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 f=n[g],m=await $i(u,f);m.changed&&(s+=1),a+=1,await r.onProgress?.({index:a,totalFiles:n.length,changedTextFiles:s,file:f,changed:m.changed});}}catch(g){throw d=g instanceof Error?g:new Error(String(g)),d}finally{await u.terminate().catch(()=>{});}});return await Promise.all(l),s}var Oe={error:0,warn:1,info:2,debug:3},Oi=/^(?:authorization|proxyAuthorization|runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken|xStaticPublisherToken|xApiKey)$/i;function Ye(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 zt(e){return typeof e=="string"?Ye(e):Array.isArray(e)?e.map(t=>zt(t)):e instanceof Error?{name:e.name,message:Ye(e.message),...e.stack?{stack:Ye(e.stack)}:{}}:e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([t,r])=>[t,Oi.test(t)?"[REDACTED]":zt(r)])):e}function Qt(e){return e?zt(e):void 0}var ss=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",Ht=ss?v.join(ss,"current-progress.json"):"",os=Promise.resolve();function is(e){return e.includes("crawl")?"crawl":e.includes("deploy")?"deploy":e.includes("invalidate")?"invalidate":e}function Mi(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 i=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,a]of Object.entries(t)){let d=r[c];if(typeof d=="number"&&typeof a=="number"&&i.has(c)){r[c]=Math.max(d,a);continue}r[c]=a;}return r}function as(e,t,r){Ht&&(os=os.then(async()=>{let n=new Date,s=n.toISOString();await F.mkdir(v.dirname(Ht),{recursive:true});let o=await F.readFile(Ht,"utf8").then(h=>JSON.parse(h)).catch(()=>null),i=o&&typeof o.details=="object"&&o.details?o.details:{},c=is(e),a=o?.currentStep||is(o?.source||""),d=o?.startedAt||s,l={...o?.stepDurationsSec??{}},u=o?.stepStartedAt||s;if(a&&a!==c&&o?.stepStartedAt){let h=Math.max(0,Math.round((n.getTime()-new Date(o.stepStartedAt).getTime())/1e3));l[a]=(l[a]??0)+h,u=s;}let g=Math.max(0,Math.round((n.getTime()-new Date(u).getTime())/1e3)),f=Math.max(0,Math.round((n.getTime()-new Date(d).getTime())/1e3)),m=Ye(t),w=Qt(r)??{},p={checkedAt:s,source:e,message:m,details:Mi(i,w),startedAt:d,currentStep:c,stepStartedAt:u,stepElapsedSec:g,totalElapsedSec:f,stepDurationsSec:l};await F.writeFile(Ht,JSON.stringify(p,null,2),"utf8");}).catch(()=>{}));}async function cs(e,t){await F.mkdir(v.dirname(e),{recursive:true}),await F.appendFile(e,`${JSON.stringify(zt(t))}
|
|
5
|
+
`,"utf8");}function ji(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 Fi(e,t,r){let n=Qt(r);return {checkedAt:new Date().toISOString(),currentStep:ji(e,n),level:e,message:Ye(t),details:n??{}}}var lt=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 v.join(this.logDir,this.logFile)}artifactPath(t){return this.logFile.endsWith(".log.jsonl")?v.join(this.logDir,this.logFile.replace(/\.log\.jsonl$/u,`${t}.jsonl`)):v.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(()=>cs(t,r));}accepts(t){return Oe[t]<=Oe[this.level]}push(t,r,n){if(!this.accepts(t))return;let s=Ye(r),o=Qt(n),i=JSON.stringify({time:new Date().toISOString(),level:t,message:s,...o||{}});this.enqueueLine(i),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){as(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 i={kind:t,url:r,reason:n,...s?{source:s}:{},...o||{}};this.enqueueJsonLine(this.rejectedPath,i),this.accepts("debug")&&this.push("debug",`Rejected ${t} ${r}: ${n}`,i);}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}},Jt=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 v.join(this.logDir,"crawl.log.jsonl")}get currentEventPath(){return v.join(this.logDir,"current-crawl-event.json")}get rejectedPath(){return v.join(this.logDir,"rejected.jsonl")}get ignoredPath(){return v.join(this.logDir,"ignored.jsonl")}get skippedPath(){return v.join(this.logDir,"skipped-http.jsonl")}get errorsPath(){return v.join(this.logDir,"errors.jsonl")}get timingsPath(){return v.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(()=>cs(t,r));}updateCurrentEvent(t,r,n){let s=Fi(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=Ye(r),o=Qt(n),i=JSON.stringify({time:new Date().toISOString(),level:t,message:s,...o||{}});this.enqueueLine(i),this.updateCurrentEvent(t,s,o),t==="error"?console.error(s,o||""):t==="warn"?console.warn(s,o||""):(Oe[this.level]>=Oe.debug||["summary","page","sitemap","asset","timing"].includes(t))&&console.log(s);}info(t,r){this.push("info",t,r);}debug(t,r){Oe[this.level]<Oe.debug||this.push("debug",t,r);}progress(t,r){as("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),Oe[this.level]>=Oe.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),Oe[this.level]>=Oe.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 ls="(?: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 ds(e){let t=new RegExp(`(?:https?:)?//[^\\s'"<>\\);]+\\.${ls}(?:[?#][^\\s'"<>\\);]*)?(?=[\\s'"<>\\);]|$)|(?<![A-Za-z0-9._-])/[^\\s'"<>\\);]+\\.${ls}(?:[?#][^\\s'"<>\\);]*)?(?=[\\s'"<>\\);]|$)`,"gi");return [...e.matchAll(t)].map(r=>r[0])}function us(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 ps(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]&&us(o[1])&&r.push({index:o.index??0,value:o[1]});for(let o of e.matchAll(s))o[1]&&us(o[1])&&r.push({index:o.index??0,value:o[1]});for(let o of r.sort((i,c)=>i.index-c.index))t.add(o.value);return [...t]}function ms(e,t){let r=v.relative(t,e);return r===""||!r.startsWith("..")&&!v.isAbsolute(r)}function gs(e,t,r,n){let s=v.resolve(e),o=v.resolve(t),i=v.extname(s).toLowerCase();return !ms(s,o)||i!==".html"&&i!==".htm"||r.has(s)?false:!n.some(c=>ms(s,v.resolve(c)))}function Kr(e,t){return typeof e=="string"&&e.length>0&&t?.supported===true&&typeof t.token=="string"&&t.token.length>0&&e===t.token}function Hr(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(i=>o.startsWith(i))}catch{return false}}function fs(e){return e<1}function hs(e){let t=String(e??"").toLowerCase();return t==="strict"?"Strict":t==="lax"?"Lax":t==="none"?"None":"Unspecified"}function Ui(e){let t=e.pathname||"/";if(!t.startsWith("/")||t==="/")return "/";let r=t.lastIndexOf("/");return r<=0?"/":t.slice(0,r)}function xt(e){return `${e.name.toLowerCase()}
|
|
8
8
|
${e.domain.toLowerCase()}
|
|
9
|
-
${e.path}`}function
|
|
10
|
-
`,{encoding:"utf8",mode:384}),await F.rename(r,e);}function It(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=It(t);if(r===null)throw new Error(`Unsafe output URL pathname: ${t}`);let n=v.resolve(e),s=v.resolve(n,r.replace(/^\/+/,""));if(s!==n&&!s.startsWith(`${n}${v.sep}`))throw new Error(`Output path escapes configured root: ${t}`);return s}var Z=1,ws=/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;function ne(e,t){let r=String(e??"").trim();if(!ws.test(r))throw new Error(`${t} must match ${ws.source} and be at most 128 characters.`);return r}function ys(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 dt(e,...t){let r=ys(e),n=t.map((s,o)=>{let i=String(s??"").replace(/^\/+|\/+$/g,"");if(!i||i.split("/").some(c=>c==="."||c===".."))throw new Error(`Unsafe S3 key segment at index ${o}.`);return i});return `${r}${n.join("/")}`}function Re(e){return createHash("sha256").update(e).digest("hex")}function We(e,t){let r=String(e??"").replace(/^\/+/,""),n=ys(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 Hi(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!==Z)throw new Error(`Remote worker protocol mismatch: expected ${Z}, 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 i=s;return {id:ae(i.id,`targets[${o}].id`),bucketName:ae(i.bucketName,`targets[${o}].bucketName`),prefix:String(i.prefix??""),region:ae(i.region,`targets[${o}].region`)}}):[],protocolVersion:Z}}async function Yt(e){let t=v.resolve(e);try{return Hi(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 pt=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;}),i=Math.max(500,Math.min(3e4,this.#e.progressPollIntervalMs??2e3)),c=-1;for(;!s;){await Promise.race([o.then(()=>{},()=>{}),new Promise(a=>setTimeout(a,i))]);try{let a=await this.#n(t);a&&a.sequence>c&&(c=a.sequence,await this.#e.onProgress(a));}catch(a){await this.#e.onProgressError?.(a instanceof Error?a:new Error(String(a)));}}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!==Z||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=We(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=We(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(Re(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:Z,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=We(t.key,this.config.workspace.prefix),n=typeof t.body=="string"?Buffer.from(t.body,"utf8"):t.body,s=Re(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=We(t.key,this.config.workspace.prefix),s=We(r,this.config.workspace.prefix),o=`${t.bucket}/${n.split("/").map(c=>encodeURIComponent(c)).join("/")}`,i=await this.s3.send(new CopyObjectCommand({Bucket:t.bucket,Key:s,CopySource:o,MetadataDirective:"COPY"}));return {...t,key:s,...i.CopyObjectResult?.ETag?{etag:i.CopyObjectResult.ETag}:{},...i.VersionId?{versionId:i.VersionId}:{}}}};function zi(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!==Z||!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 Jr(e){return e instanceof Error?e.message:String(e)}var Xt=class{#e;#n;#t;#s;#c;#l;#r=[];#u=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.#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=r;for(let i=1;i<=this.#t&&o.length>0;i+=1){this.#u+=1;let c=ne(`render-${this.#u}-${Re(o.join(`
|
|
11
|
-
`)).slice(0,24)}-a${i}`,"taskId"),a=[];try{let d=await this.#e.invoke({schemaVersion:Z,operation:"render",jobId:this.#n,taskId:c,sourceOrigin:new URL(t.sourceOrigin).origin,urls:o,options:t.options}),l=
|
|
12
|
-
`)).slice(0,24)}-a${s}`,"taskId");try{let i=await this.#e.invoke({schemaVersion:Z,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:
|
|
13
|
-
`)).slice(0,16)}`,"taskId"),V={schemaVersion:Z,operation:"rewrite",jobId:o,taskId:O,configKey:c,assetMapKey:a,previousAssetMapKey:d,items:x.map(U=>({inputKey:U.key,outputKey:U.key,relativePath:U.relativePath,contentType:U.contentType}))};try{let U=await r.client.invoke(V);R=
|
|
14
|
-
`)).slice(0,16)}`,"taskId");try{let x=await n.client.invoke({schemaVersion:Z,operation:"rewrite",jobId:i,taskId:$,configKey:a,assetMapKey:d,items:h.map(I=>({inputKey:I.object.key,outputKey:I.object.key,relativePath:I.relativePath,contentType:I.contentType||I.object.contentType}))});y=
|
|
15
|
-
`,"utf8");}async function or(e){try{let t=JSON.parse(await F.readFile(sr(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(
|
|
16
|
-
${r}`,40),tokenSource:t.tokenSource?`${t.tokenSource}+site-subscriber`:"site-subscriber"}}async function
|
|
9
|
+
${e.path}`}function ws(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 i=s.slice(0,o).trim();if(!i||[...i].some(w=>w<=" "||"=;,".includes(w)))return null;let c=r.hostname.toLowerCase(),a=Ui(r),d=null,l=true,u=false,g=false,f="Unspecified",m=false;for(let w of n){let p=w.indexOf("="),h=(p<0?w:w.slice(0,p)).trim().toLowerCase(),y=p<0?"":w.slice(p+1).trim();if(h==="domain"&&y)c=y.toLowerCase();else if(h==="path"&&y.startsWith("/"))a=y;else if(h==="expires"){let b=Date.parse(y);Number.isFinite(b)&&(d=Math.floor(b/1e3),l=false);}else if(h==="max-age"&&/^-?\d+$/u.test(y)){let b=Number.parseInt(y,10);d=Math.floor(Date.now()/1e3)+b,l=false;}else h==="secure"?u=true:h==="httponly"?g=true:h==="samesite"?f=hs(y):h==="partitioned"&&(m=true);}return {name:i,domain:c,path:a,expires:d,session:l,secure:u,httpOnly:g,sameSite:f,partitioned:m}}function Li(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:hs(e.sameSite),partitioned:e.partitioned===true}}function ys(e,t){let r=new Map(e.map(n=>[xt(n),n.value]));return t.filter(n=>r.get(xt(n))!==n.value).map(Li)}function qi(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 Gt=class{#e=new Map;add(t){if(!t)return;let r={...t,domain:t.domain.toLowerCase(),path:t.path||"/"},n=xt(r),s=this.#e.get(n);this.#e.set(n,s?qi(s,r):r);}addAll(t){for(let r of t)this.add(r);}artifact(){return {schemaVersion:1,observations:[...this.#e.values()].sort((t,r)=>xt(t).localeCompare(xt(r)))}}};async function Zt(e,t){await F.mkdir(v.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 It(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 Tt(e,t){let r=It(t);if(r===null)throw new Error(`Unsafe output URL pathname: ${t}`);let n=v.resolve(e),s=v.resolve(n,r.replace(/^\/+/,""));if(s!==n&&!s.startsWith(`${n}${v.sep}`))throw new Error(`Output path escapes configured root: ${t}`);return s}var Z=1,bs=/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;function ne(e,t){let r=String(e??"").trim();if(!bs.test(r))throw new Error(`${t} must match ${bs.source} and be at most 128 characters.`);return r}function Ss(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 dt(e,...t){let r=Ss(e),n=t.map((s,o)=>{let i=String(s??"").replace(/^\/+|\/+$/g,"");if(!i||i.split("/").some(c=>c==="."||c===".."))throw new Error(`Unsafe S3 key segment at index ${o}.`);return i});return `${r}${n.join("/")}`}function Re(e){return createHash("sha256").update(e).digest("hex")}function We(e,t){let r=String(e??"").replace(/^\/+/,""),n=Ss(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 Gi(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!==Z)throw new Error(`Remote worker protocol mismatch: expected ${Z}, 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 i=s;return {id:ae(i.id,`targets[${o}].id`),bucketName:ae(i.bucketName,`targets[${o}].bucketName`),prefix:String(i.prefix??""),region:ae(i.region,`targets[${o}].region`)}}):[],protocolVersion:Z}}async function Yt(e){let t=v.resolve(e);try{return Gi(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 pt=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;}),i=Math.max(500,Math.min(3e4,this.#e.progressPollIntervalMs??2e3)),c=-1;for(;!s;){await Promise.race([o.then(()=>{},()=>{}),new Promise(a=>setTimeout(a,i))]);try{let a=await this.#n(t);a&&a.sequence>c&&(c=a.sequence,await this.#e.onProgress(a));}catch(a){await this.#e.onProgressError?.(a instanceof Error?a:new Error(String(a)));}}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!==Z||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=We(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=We(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(Re(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:Z,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=We(t.key,this.config.workspace.prefix),n=typeof t.body=="string"?Buffer.from(t.body,"utf8"):t.body,s=Re(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=We(t.key,this.config.workspace.prefix),s=We(r,this.config.workspace.prefix),o=`${t.bucket}/${n.split("/").map(c=>encodeURIComponent(c)).join("/")}`,i=await this.s3.send(new CopyObjectCommand({Bucket:t.bucket,Key:s,CopySource:o,MetadataDirective:"COPY"}));return {...t,key:s,...i.CopyObjectResult?.ETag?{etag:i.CopyObjectResult.ETag}:{},...i.VersionId?{versionId:i.VersionId}:{}}}};function Zi(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!==Z||!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 Qr(e){return e instanceof Error?e.message:String(e)}var Xt=class{#e;#n;#t;#s;#c;#l;#r=[];#u=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.#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=r;for(let i=1;i<=this.#t&&o.length>0;i+=1){this.#u+=1;let c=ne(`render-${this.#u}-${Re(o.join(`
|
|
11
|
+
`)).slice(0,24)}-a${i}`,"taskId"),a=[];try{let d=await this.#e.invoke({schemaVersion:Z,operation:"render",jobId:this.#n,taskId:c,sourceOrigin:new URL(t.sourceOrigin).origin,urls:o,options:t.options}),l=Zi(await this.#e.readResult(d),d,o);for(let u of l.detail.pages){if(u.outcome==="failed"||u.outcome==="deferred"||!u.html){let g=u.error||`Remote render ended with ${u.outcome}.`;s.set(u.requestedUrl,g),a.push(u.requestedUrl);continue}try{n.set(u.requestedUrl,{page:u,html:t.downloadHtml===!1?null:await this.#e.readObjectText(u.html),taskId:c,attempts:i}),s.delete(u.requestedUrl);}catch(g){s.set(u.requestedUrl,Qr(g)),a.push(u.requestedUrl);}}}catch(d){let l=Qr(d);for(let u of o)s.set(u,l),a.push(u);}if(i<this.#t)for(let d of a)this.#s?.({url:d,attempt:i,maxAttempts:this.#t,reason:s.get(d)??"Remote render failed."});o=a;}return {pages:r.flatMap(i=>{let c=n.get(i);return c?[c]:[]}),failures:o.map(i=>({url:i,attempts:this.#t,error:s.get(i)??"Remote render failed."}))}}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.#d();},0));}#d(){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(i=>[i.page.requestedUrl,i])),o=new Map(n.failures.map(i=>[i.url,i]));for(let i of r){let c=s.get(i.input.url);if(c){i.resolve(c);continue}let a=o.get(i.input.url);i.reject(new Error(`Remote render failed after ${a?.attempts??this.#t} attempt(s) for ${i.input.url}: ${a?.error??"Unknown remote render failure."}`));}}).catch(n=>{let s=Qr(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 Yi(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!==Z||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 er=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}-${Re(r.join(`
|
|
12
|
+
`)).slice(0,24)}-a${s}`,"taskId");try{let i=await this.#e.invoke({schemaVersion:Z,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:Yi(await this.#e.readResult(i),i,r).detail.assets,taskId:o,attempts:s}}catch(i){n=i;}}throw new Error(`Remote asset batch failed after ${this.#t} attempt(s): ${n instanceof Error?n.message:String(n)}`,{cause:n})}};function Cs(e,t){let r=[];for(let n=0;n<e.length;n+=t)r.push(e.slice(n,n+t));return r}function ks(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 xs(e,t){if(!e||typeof e!="object")throw new Error("Remote rewrite result must be a JSON object.");let r=e;if(r.schemaVersion!==Z||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 tr(e,t,r){let n=r.options??{},s=await Br(e,n.files);if(s.length===0)return 0;let o=ne(r.jobId,"jobId"),i=dt(r.client.config.workspace.prefix,"jobs",o,"rewrite-inputs"),c=`${i}/config.json`.replace(/\/+/g,"/"),a=`${i}/asset-map.json`.replace(/\/+/g,"/"),d=`${i}/previous-asset-map.json`.replace(/\/+/g,"/");await Promise.all([r.client.writeWorkspaceObject({key:c,body:JSON.stringify(ks(e)),contentType:"application/json; charset=utf-8"}),r.client.writeWorkspaceObject({key:a,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 l=Math.max(1,Math.min(500,r.batchSize)),u=Math.ceil(s.length/l),g=Math.max(1,Math.min(Math.max(1,r.concurrency),u));await n.onStart?.({totalFiles:s.length,workerCount:g});let f=[],m=0,w=0;await Promise.all(Array.from({length:Math.min(16,s.length)},async()=>{for(;;){let $=m;if($>=s.length)return;m+=1;let x=s[$],I=v.join(e.outputDir,x),R=vs.lookup(x),C=R?vs.contentType(R)||R:"text/plain; charset=utf-8",O=`${i}/files/${Re(x)}.txt`.replace(/\/+/g,"/");await r.client.writeWorkspaceObject({key:O,body:await F.readFile(I),contentType:C}),f[$]={relativePath:x,key:O,fullPath:I,contentType:C},w+=1,await r.onStageProgress?.({index:w,totalFiles:s.length,file:x});}}));let p=Cs(f,l),h=0,y=0,b=0,P=Array.from({length:g},async()=>{for(;;){let $=h;if($>=p.length)return;h+=1;let x=p[$],I=null,R=null;for(let C=1;C<=Math.max(1,Math.min(5,r.maxAttempts));C+=1){let O=ne(`rewrite-${$+1}-a${C}-${Re(x.map(U=>U.relativePath).join(`
|
|
13
|
+
`)).slice(0,16)}`,"taskId"),V={schemaVersion:Z,operation:"rewrite",jobId:o,taskId:O,configKey:c,assetMapKey:a,previousAssetMapKey:d,items:x.map(U=>({inputKey:U.key,outputKey:U.key,relativePath:U.relativePath,contentType:U.contentType}))};try{let U=await r.client.invoke(V);R=xs(await r.client.readResult(U),U);break}catch(U){I=U;}}if(!R)throw new Error(`Remote rewrite batch ${$+1}/${p.length} failed: ${I instanceof Error?I.message:String(I)}`,{cause:I});for(let C of x){let O=R.detail.objects.find(V=>V.outputKey===C.key);if(!O)throw new Error(`Remote rewrite result omitted ${C.relativePath}.`);await Le(C.fullPath,Buffer.from(await r.client.readObject(O.output))),O.changed&&(b+=1),y+=1,await n.onProgress?.({index:y,totalFiles:s.length,changedTextFiles:b,file:C.relativePath,changed:O.changed});}}});return await Promise.all(P),b}async function rr(e,t,r,n){let s=n.relativePaths?new Set([...n.relativePaths].map(p=>p.replace(/\\/g,"/"))):null,o=Object.values(t.objects).filter(p=>p.isText&&(!s||s.has(p.relativePath))).sort((p,h)=>p.relativePath.localeCompare(h.relativePath));if(o.length===0)return 0;let i=ne(n.jobId,"jobId"),c=dt(n.client.config.workspace.prefix,"jobs",i,"rewrite-config"),a=`${c}/config.json`.replace(/\/+/g,"/"),d=`${c}/asset-map.json`.replace(/\/+/g,"/");await Promise.all([n.client.writeWorkspaceObject({key:a,body:JSON.stringify(ks(e)),contentType:"application/json; charset=utf-8"}),n.client.writeWorkspaceObject({key:d,body:JSON.stringify(r),contentType:"application/json; charset=utf-8"})]);let l=Math.max(1,Math.min(500,n.batchSize)),u=Cs(o,l),g=Math.max(1,Math.min(Math.max(1,n.concurrency),u.length)),f=0,m=0,w=0;return await Promise.all(Array.from({length:g},async()=>{for(;;){let p=f;if(p>=u.length)return;f+=1;let h=u[p],y=null,b=null;for(let P=1;P<=Math.max(1,Math.min(5,n.maxAttempts));P+=1){let $=ne(`rewrite-object-${p+1}-a${P}-${Re(h.map(x=>x.relativePath).join(`
|
|
14
|
+
`)).slice(0,16)}`,"taskId");try{let x=await n.client.invoke({schemaVersion:Z,operation:"rewrite",jobId:i,taskId:$,configKey:a,assetMapKey:d,items:h.map(I=>({inputKey:I.object.key,outputKey:I.object.key,relativePath:I.relativePath,contentType:I.contentType||I.object.contentType}))});y=xs(await n.client.readResult(x),x);break}catch(x){b=x;}}if(!y)throw new Error(`Remote object rewrite batch ${p+1}/${u.length} failed: ${b instanceof Error?b.message:String(b)}`,{cause:b});for(let P of h){let $=y.detail.objects.find(x=>x.outputKey===P.object.key);if(!$)throw new Error(`Remote rewrite result omitted ${P.relativePath}.`);P.object=$.output,$.changed&&(w+=1),m+=1,await n.onProgress?.({index:m,totalFiles:o.length,changedTextFiles:w,file:P.relativePath,changed:$.changed});}}})),t.assetMap={...r},t.generatedAt=new Date().toISOString(),w}var ta="remote-workers.json";function nr(e={}){let t=String(e.explicitPath??"").trim();if(t)return v.resolve(t);let r=String(e.runtimeDir??process.env.STATIC_PUBLISHER_RUNTIME_DIR??process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR??"").trim();return r?v.join(v.resolve(r),ta):""}function Is(e,t,r,n=250){let s=Math.max(1,Math.floor(n)),o=[],i=new Set;for(let c of [e,...t]){let a=c.trim();if(!(!a||i.has(a)||r(a))&&(i.add(a),o.push(a),o.length>=s))break}return o}function ra(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?v.resolve(e):""}function sr(e){let t=String(process.env.STATIC_PUBLISHER_REMOTE_EXPORT_MANIFEST_PATH||"").trim();if(t)return v.resolve(t);let r=ra();if(r)return v.join(r,"remote-export-manifest.json");let n=v.resolve(e.outputDir);return v.join(v.dirname(n),`.${v.basename(n)}-remote-export-manifest.json`)}function Zr(e){let t=v.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 As(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 mt(e,t){let r=Zr(t.relativePath);if(t.object.bucket!==e.workspace.bucket)throw new Error(`Remote export object bucket mismatch for ${r}.`);We(t.object.key,e.workspace.prefix),e.objects[r]={...t,relativePath:r},e.generatedAt=new Date().toISOString();}function Es(e,t){delete e.objects[Zr(t)],e.generatedAt=new Date().toISOString();}async function $s(e,t){t.generatedAt=new Date().toISOString();let r=sr(e);await F.mkdir(v.dirname(r),{recursive:true}),await Le(r,`${JSON.stringify(t,null,2)}
|
|
15
|
+
`,"utf8");}async function or(e){try{let t=JSON.parse(await F.readFile(sr(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(Zr(n),!s||s.relativePath!==n||s.object?.bucket!==r.workspace.bucket)return null;We(s.object.key,r.workspace.prefix);}return r}catch{return null}}var ia=["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"],Vs=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"]),Ks=new Set(["",".html",".htm"]),aa=new Set([".css",".xml",".xsl",".svg",".json",".html",".htm",".txt",".enc",".jws"]),ca=["/wp-content/","/wp-includes/","/wp-admin/","/static/","/assets/","/build/","/_next/"];function la(e){let t=[],r="full",n="full",s=false,o=false,i="",c=null,a="";for(let d=0;d<e.length;d++){let l=e[d];if(l==="--retry-timeouts")r="retry-timeouts",s=true;else if(l==="--resume")s=true;else if(l==="--resume-rewrite")s=true,o=true;else if(l==="--crawl-mode"){let u=e[++d];if(!u)throw new Error("--crawl-mode requires a value");n=u==="incremental"?"incremental":"full",s||=n==="incremental";}else if(l.startsWith("--crawl-mode="))n=l.slice(13)==="incremental"?"incremental":"full",s||=n==="incremental";else if(l==="--incremental")n="incremental",s=true;else if(l==="--url"){let u=e[++d];if(!u)throw new Error("--url requires a value");r="single-url",s=true,t.push(u);}else if(l.startsWith("--url="))r="single-url",s=true,t.push(l.slice(6));else if(l==="--urls"){let u=e[++d];if(!u)throw new Error("--urls requires a file path");r="single-url",s=true,t.push(`@${u}`);}else if(l==="--content-sync-plan"){let u=e[++d];if(!u)throw new Error("--content-sync-plan requires a file path");r="content-sync",n="incremental",s=true,i=v.resolve(u);}else if(l.startsWith("--content-sync-plan="))r="content-sync",n="incremental",s=true,i=v.resolve(l.slice(20));else if(l==="--remote-render")c=true;else if(l==="--local-render")c=false;else if(l==="--remote-worker-config"){let u=e[++d];if(!u)throw new Error("--remote-worker-config requires a file path");a=v.resolve(u);}else l.startsWith("--remote-worker-config=")&&(a=v.resolve(l.slice(23)));}return {mode:r,crawlMode:n,urls:t,preserveOutput:s,resumeRewrite:o,contentSyncPlanPath:i,remoteRender:c,remoteWorkerConfigPath:a}}function jt(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?v.resolve(e):""}function ua(){let e=jt();return e?v.join(e,"privacy","cookie-observations.json"):null}function da(){let e=jt();return e?v.resolve(e,".."):""}function pa(){let e=process.env.STATIC_PUBLISHER_WP_ROOT||process.env.WPSUITE_STATIC_PUBLISHER_WP_ROOT||"";return e.trim()?v.resolve(e):""}function ma(e){let t=e.trim(),r=[{alias:"@storage-root",root:da(),requiredEnv:"STATIC_PUBLISHER_RUNTIME_DIR or WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR"},{alias:"@runtime",root:jt(),requiredEnv:"STATIC_PUBLISHER_RUNTIME_DIR or WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR"},{alias:"@wp-root",root:pa(),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?v.resolve(n.root,s):n.root,alias:n.alias}}return {resolvedPath:v.resolve(t)}}function Hs(e){let t=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(t)return v.resolve(t);let r=jt();return r?v.join(r,"crawl-manifest.json"):v.join(e.outputDir,".crawl-manifest.json")}function zs(){return {schemaVersion:6,updatedAt:"",pages:{},assets:{}}}function Ms(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 ga(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 en(e){let t=e?.["content-type"]?.trim();return t||null}function tn(e){let t=e?.["cache-control"]?.trim();return t||null}function fa(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 ha(){return {rewriteTargets:new Set,changedFiles:new Set,deletedFiles:new Set,revalidateFiles:new Set}}function gt(e,t,r,n){let s=he(t.outputDir,r);s&&(fa(n)?e.revalidateFiles.add(s):e.revalidateFiles.delete(s));}function ue(e,t,r){let n=he(t.outputDir,r);n&&(e.changedFiles.add(n),e.deletedFiles.delete(n));}function rn(e,t,r){let n=he(t.outputDir,r);n&&(e.deletedFiles.add(n),e.changedFiles.delete(n),e.revalidateFiles.delete(n));}function ve(e,t){e.rewriteTargets.add(v.resolve(t));}async function wa(e){try{let t=await F.readFile(Hs(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,Ms(s)])),assets:Object.fromEntries(Object.entries(r.assets).map(([n,s])=>[n,ga(s)]))};if(r.schemaVersion===1)return {schemaVersion:6,updatedAt:String(r.updatedAt||""),pages:Object.fromEntries(Object.entries(r.pages).map(([n,s])=>[n,Ms(s)])),assets:{}}}}catch{}return zs()}function nn(e){return JSON.parse(JSON.stringify(e))}async function ya(e,t){let r=Hs(e);await F.mkdir(v.dirname(r),{recursive:true}),await F.writeFile(r,JSON.stringify(t,null,2),"utf8");}async function Be(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=nn(t.manifest);o.updatedAt=new Date().toISOString(),t.manifest.updatedAt=o.updatedAt,await ya(e,o);};t.manifestSaveChain=t.manifestSaveChain.then(s,s),await t.manifestSaveChain;}function Xe(e){if(!e)return null;let t=e.trim();return t||null}function ba(e,t){let r=new Set,n=new Set([...Object.keys(e),...Object.keys(t)]);for(let s of n){let o=e[s],i=t[s];!o||!i||o===i||r.add(s);}return r}async function Js(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 i=new URL("/wp-json/smartcloud-static-publisher/v1/change-tokens",e.sourceOrigin).toString();try{let c=await t.post(i,{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:i,status:c.status()}),new Map;let a=await c.json().catch(()=>null);if(!a||!Array.isArray(a.items))return new Map;let d=new Map;for(let l of a.items)!l||typeof l.url!="string"||d.set(l.url,l);return d}catch(c){return n.warn("Change token lookup failed; falling back to sitemap metadata",{endpoint:i,error:c instanceof Error?`${c.name}: ${c.message.split(/\r?\n/u,1)[0]}`:"Unknown change-token request failure"}),new Map}}async function ar(e,t,r,n,s){if(!r.enabled&&!r.trackManifest)return null;if(r.changeTokenCache.has(n))return r.changeTokenCache.get(n)??null;let i=(await Js(e,t,[n],s)).get(n)??null;return pr(e,n)&&(!i||i.supported!==true||!i.token)&&(i=await Ra(e,t,n,s)),i=Qs(e,i),r.changeTokenCache.set(n,i),i}async function dn(e,t,r,n,s){if(!r.trackManifest)return;let o=[...new Set(n.map(i=>i.trim()).filter(Boolean))].filter(i=>!r.changeTokenCache.has(i));for(let i=0;i<o.length;i+=250){let c=o.slice(i,i+250),a=await Js(e,t,c,s);for(let d of c)r.changeTokenCache.set(d,Qs(e,a.get(d)??null));}}async function Sa(e,t,r,n,s,o){if(!(!n.trackManifest||n.changeTokenCache.has(s)))for(;!n.changeTokenCache.has(s);){if(!n.pageChangeTokenPrimePromise){let i=Is(s,r.pageQueue,a=>n.changeTokenCache.has(a));if(i.length===0)return;o.info(`Loading ${i.length} page change tokens in bulk.`,{phase:"prime-page-change-tokens",batchSize:i.length,remainingPages:r.pageQueue.length});let c=dn(e,t,n,i,o).finally(()=>{n.pageChangeTokenPrimePromise===c&&(n.pageChangeTokenPrimePromise=null);});n.pageChangeTokenPrimePromise=c;}await n.pageChangeTokenPrimePromise;}}function Pa(e){return `subscriber=${e.wpsuite?.siteSettings?.subscriber===true?"1":"0"}`}function Qs(e,t){if(!t?.supported||!t.token||t.tokenSource==="wp-media-file")return t;let r=Pa(e);return {...t,token:Ae(`${t.token}
|
|
16
|
+
${r}`,40),tokenSource:t.tokenSource?`${t.tokenSource}+site-subscriber`:"site-subscriber"}}async function Ra(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(()=>""),i=Ae(`${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:i,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 Js(e,t,r,n,s,o){let i=pr(e,s);if(await ha(e,t,r,n,s,o),!n.enabled)return {action:"render",changeToken:n.trackManifest?await ar(e,t,n,s,o):null};let c=n.manifest.pages[s];if(!c)return {action:"render",changeToken:await ar(e,t,n,s,o)};let a=await ar(e,t,n,s,o);if(a?.supported&&a.token)return c.changeToken===a.token?i&&!await ba(e,t,s,o)?{action:"render",changeToken:a}:{action:"reuse",changeToken:a}:{action:"render",changeToken:a};let d=et(r.sitemapLastmodByPage[s]);return d&&c.sitemapLastmod&&c.sitemapLastmod===d?{action:"reuse",changeToken:a}:{action:"render",changeToken:a}}async function ba(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 Sa(e,t,r,n,s){let o=Os(r.previousManifest,void 0,n,"previous-manifest"),i=0;for(let[g,f]of Object.entries(r.manifest.pages))if(!r.seenPages.has(g)){tn(s,e,f.outputPath);try{await F.unlink(f.outputPath);}catch(m){(m.code||"")!=="ENOENT"&&n.warn(`Failed to remove stale incremental page output for ${g}`,{url:g,outputPath:f.outputPath,error:String(m)});}delete r.manifest.pages[g],i++;}let c=Os(r.manifest,r.runId,n,"active-manifest"),a=new Set,d=new Set;for(let g of c){for(let w of $t(e,g))a.add(w);let f=r.manifest.assets[g]?.outputPath;f&&d.add(f);let m=ft(e,t.assetMap,g);m&&d.add(m);}for(let g of Object.keys(r.manifest.assets)){if(c.has(g)){r.manifest.assets[g].unreferencedRuns=0;continue}let f=r.manifest.assets[g];if(ms(f.unreferencedRuns)){f.unreferencedRuns+=1,n.warn(`Deferred deletion of newly unreferenced incremental asset ${g}.`,{url:g,unreferencedRuns:f.unreferencedRuns,phase:"incremental-cleanup",source:"asset-deletion-grace"});continue}delete r.manifest.assets[g];}let l=0,u=0;for(let g of o){if(r.manifest.assets[g])continue;let f=new Set,m=r.previousManifest.assets[g]?.outputPath;m&&f.add(m);let w=ft(e,t.assetMap,g);if(!c.has(g)&&w)f.add(w);else if(!c.has(g)){let p=Ua(e,g,n,"incremental-cleanup");p&&(f.add(p.originalFilePath),p.hashedFilePath&&f.add(p.hashedFilePath));}for(let p of f)if(!d.has(p)){tn(s,e,p);try{await F.unlink(p),l++;}catch(h){(h.code||"")!=="ENOENT"&&n.warn(`Failed to remove stale incremental asset output for ${g}`,{url:g,outputPath:p,error:String(h)});}}for(let p of $t(e,g))a.has(p)||Object.prototype.hasOwnProperty.call(t.assetMap,p)&&(delete t.assetMap[p],u++);}(i>0||l>0||u>0)&&n.info(`Incremental cleanup removed ${i} stale pages, ${l} stale assets, ${u} stale asset mappings`,{removedPages:i,removedAssets:l,removedAssetMappings:u,phase:"incremental-cleanup"});}async function Pa(e,t,r,n){let s=new Set([...Object.values(t.manifest.pages),...Object.values(t.manifest.assets)].map(a=>String(a.outputPath||"").trim()).filter(Boolean).map(a=>v.resolve(a))),o=Object.values(e.postCrawlCopyMap||{}).map(a=>cr(String(a||""))).filter(a=>!!a).map(a=>v.resolve(e.outputDir,a)),c=(await li(["**/*.html","**/*.htm"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(a=>ps(v.join(e.outputDir,a),e.outputDir,s,o));for(let a of c){let d=v.join(e.outputDir,a);tn(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 Os(e,t,r,n){let s=new Set,o=i=>{let c=i.trim();if(c){if(r){let a=Ce(c,r,"asset",n??"manifest");if(!a)return;s.add(a.toString());return}s.add(c);}};for(let i of Object.values(e.pages))for(let c of i.discoveredAssets||[])o(c);for(let[i,c]of Object.entries(e.assets||{}))t&&c.lastSeenRunId!==t||o(i);return s}function Ms(e,t,r){for(let n of t)for(let s of $t(e,n))if(r.has(s))return true;return false}function Ra(e,t,r,n,s){let o=new Set(r.rewriteTargets);for(let c of r.changedFiles)o.add(v.resolve(e.outputDir,c));for(let c of Object.values(t.manifest.pages))c.rewriteComplete||o.add(v.resolve(c.outputPath));for(let c of Object.values(t.manifest.assets))c.isText&&!c.rewriteComplete&&o.add(v.resolve(c.outputPath));let i=fa(n,s);if(i.size===0)return [...o];for(let c of Object.values(t.manifest.pages))Ms(e,c.discoveredAssets,i)&&o.add(v.resolve(c.outputPath));for(let[c,a]of Object.entries(t.manifest.assets)){a.isText&&Ms(e,a.discoveredAssets,i)&&o.add(v.resolve(a.outputPath));for(let d of $t(e,c))if(i.has(d)){ue(r,e,a.outputPath),a.isText&&o.add(v.resolve(a.outputPath));break}}return [...o]}function va(e,t){let r=t?new Set(Array.from(t,s=>v.resolve(s))):null,n=s=>r===null||r.has(v.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 Ca(e,t,r){let n=new Set,s=o=>{if(!o)return;let i=fe(e.outputDir,o);i&&n.add(i);};for(let o of t.donePages)s(Me(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(ft(e,t.assetMap,o));for(let o of Object.values(r.manifest.assets))o.lastSeenRunId===r.runId&&s(o.outputPath);return n}async function ka(e,t,r,n,s,o){let i=[...n.deletedFiles].sort(),c=Ca(e,t,r),a=0;for(let u of [...c].sort())if(!o?.objects[u]){try{if((await F.stat(v.join(e.outputDir,u))).isFile())continue}catch{}a++,s.warn(`Active output is missing after crawl: ${u}`,{phase:"deploy-plan",file:u,outputPath:v.join(e.outputDir,u)});}if(a>0&&s.warn(`Detected ${a} active output file(s) missing before deploy plan save.`,{phase:"deploy-plan",missingActiveOutputs:a}),i.length===0)return [];let d=[],l=0;for(let u of i){if(!c.has(u)){d.push(u);continue}l++;let g=v.join(e.outputDir,u),f;try{f=(await F.stat(g)).isFile();}catch{f=false;}s.warn(`Deploy plan protected active output from deletion: ${u}`,{phase:"deploy-plan",file:u,outputPath:g,exists:f});}return l>0&&s.info(`Deploy plan skipped ${l} active output deletion(s).`,{phase:"deploy-plan",protectedDeletes:l,deletedFiles:d.length}),d}function $t(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 i of o)try{r.add(new URL(i).origin+n.pathname+n.search);}catch{}r.add(n.pathname+n.search),r.add(n.pathname);}catch{}return [...r]}function xa(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 i of o)try{r.add(new URL(i).origin+n.pathname+n.search);}catch{}r.add(n.pathname+n.search);}catch{}return [...r]}function Qs(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 Gs(e,t,r){for(let n of xa(e,r)){let s=t[n];if(!s)continue;let o=Qs(e,s);if(o)return {outputPath:o,publicUrl:s}}return null}function Ia(e,t,r){for(let n of $t(e,r)){let s=t[n];if(!s)continue;let o=Qs(e,s);if(o)return o}return null}function ft(e,t,r){let n=Ia(e,t,r);if(n)return n;try{return Ft(e,r).originalFilePath}catch{return null}}function Aa(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?v.join(v.resolve(e),"queue-runner-heartbeat.json"):""}async function Zr(e){let t=Aa();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 Ta(e,t){let r=new Set;for(let n of t.donePages)r.add(v.resolve(Me(e,n)));for(let n of t.doneAssets){let s=ft(e,t.assetMap,n);!s||!Ee(s)||r.add(v.resolve(s));}for(let n of t.doneSitemaps){let s=ft(e,t.assetMap,n);!s||!Ee(s)||r.add(v.resolve(s));}return [...r]}var lr="re:",ir=new Map;function Ea(e){if(!e.startsWith(lr))return null;let t=ir.get(e);if(t!==void 0)return t||null;let r=e.slice(lr.length).trim();if(!r)return ir.set(e,false),null;try{let n=new RegExp(r);return ir.set(e,n),n}catch{return console.warn(`[crawl] Ignoring invalid path matcher regex: ${e}`),ir.set(e,false),null}}function dn(e,t){let r=Ea(t);return r?r.test(e):t.startsWith(lr)?false:e.startsWith(t)}function $a(e,t){return t.find(r=>dn(e,r))}function Zs(e,t){let r=$a(t.pathname,e.blockedPathPrefixes);if(r)return r.startsWith(lr)?`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 Oa(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 Ma(e,t){return Oa(e).has(t.hostname)}function ja(e){let t=qe(e.trim()).replace(/\\\//g,"/").replace(/^['"]|['"]$/g,"").trim();return t=t.replace(/[)]+$/g,"").trim(),t=t.replace(/;.*$/g,"").trim(),t}function nn(e){let t=qe(e).replace(/\\\//g,"/");return /[{}]|\bwindow\.|\blocation\.|\bincludes\(|\?\?null|\+|%7B|%7D|%22|<|>|\s/.test(t)||/[()[\]'"]/.test(t)}function Fa(e){let t=qe(e).replace(/\\\//g,"/").trim(),r=v.extname(t).toLowerCase();return _s.has(r)||Bs.has(r)?false:/^(?:this|[A-Za-z_$][\w$]*)\.[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/.test(t)}function rt(e){return sa.some(t=>e.pathname.startsWith(t))}function oe(e,t,r=e.sourceOrigin,n,s="url",o=false){let i=ja(t);if(!i||i.startsWith("data:")||i.startsWith("blob:")||i.startsWith("mailto:")||i.startsWith("tel:")||i.startsWith("#"))return n?.ignore(s,t,"empty or unsupported scheme",r),null;if(nn(i))return n?.ignore(s,t,"looks like JavaScript/code fragment",r),null;if(Fa(i))return n?.ignore(s,t,"looks like JavaScript property access",r),null;try{let c=new URL(i,r);if(Ys(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(!Ma(e,c))return n?.ignore(s,c.toString(),"host not allowed",r),null;let a=Zs(e,c);if(a&&!(o&&$r(c)))return n?.reject(s,c.toString(),a,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 Ys(e){try{return decodeURIComponent(e.pathname),decodeURIComponent(e.search),decodeURIComponent(e.hash),!1}catch{return true}}function Ce(e,t,r,n){try{let s=new URL(e);return Ys(s)?(t.reject(r,e,"malformed URL encoding",n),null):It(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 Xs(e){return _s.has(v.extname(e.pathname).toLowerCase())}function Ve(e){return rt(e)?false:Bs.has(v.extname(e.pathname).toLowerCase())}function wt(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function H(e,t){return !!(wt(t)||t.pathname==="/robots.txt"||t.pathname==="/llms.txt"||t.pathname.toLowerCase().endsWith(".xsl")||e.assetPathPrefixes.length>0&&e.assetPathPrefixes.some(r=>dn(t.pathname,r))||Xs(t)||!Ve(t)&&!rt(t))}function Me(e,t){let r=new URL(t),n=It(r.pathname);if(n===null)throw new Error(`Unsafe page URL pathname: ${r.pathname}`);return n.endsWith("/")?n+="index.html":v.extname(n)||(n+="/index.html"),At(e.outputDir,n)}function Da(e){if(!e.generated404RequestPath)return null;try{let t=new URL(e.generated404RequestPath,e.sourceOrigin);return !t.pathname.endsWith("/")&&v.extname(t.pathname)===""&&(t.pathname+="/"),t.hash="",t.toString()}catch{return null}}function pr(e,t){let r=Da(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=It(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,i=At(e.outputDir,o),c=qr(e.targetOrigin,o),a=`${c}${r.search}`;if(!s)return {originalPathname:o,originalFilePath:i,originalPublicUrl:c,originalPublicUrlWithSearch:a,preferQueryHashed:s};let d=v.extname(n),u=`${d?n.slice(0,-d.length):n}.${Te(r.search)}${d||".bin"}`;return {originalPathname:o,originalFilePath:i,originalPublicUrl:c,originalPublicUrlWithSearch:a,preferQueryHashed:s,hashedPathname:u,hashedFilePath:At(e.outputDir,u),hashedPublicUrl:qr(e.targetOrigin,u)}}function Ua(e,t,r,n){let s=Ce(t,r,"asset",n);return s?Ft(e,s.toString()):null}function sn(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 a of c)try{let d=new URL(a);t[d.origin+o.pathname+o.search]=n;}catch{}try{let a=(()=>{let d=new URL(n,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin);return `${d.pathname}${d.search}`})();t[o.pathname+o.search]=a;}catch{}try{let a=new URL(n,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin).pathname;t[o.pathname]=a;}catch{}try{let a=new URL(s,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin).pathname;t[o.pathname]=a;}catch{}}catch{}}async function eo(e){try{return await F.readFile(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}async function tt(e){try{return await F.stat(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}function La(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 to(e){let t=String(e["last-modified"]||"").trim();if(!t)return null;let r=Date.parse(t);return Number.isFinite(r)?r:null}function qa(e,t){let r=to(t),n=La(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 Wa(e,t){if(!t)return;let r=to(t);if(r===null)return;let n=await tt(e);if(!n)return;let s=new Date(r);await F.utimes(e,n.atime,s).catch(()=>{});}function on(e,t){let r=v.extname(e.pathname).toLowerCase(),n=t["content-type"]||"";return na.has(r)||ra.some(s=>n.includes(s))&&!n.startsWith("image/")&&!n.includes("font")}function Na(e){try{let t=v.extname(new URL(e).pathname).toLowerCase();return t===".js"||t===".mjs"}catch{return false}}function Qe(e,t){(e.doneAssets.size%25===0||e.doneAssets.size===1||e.doneAssets.size===e.stats.assetsQueued)&&t.progress(`Asset processing progress: processed ${e.doneAssets.size}, discovered ${e.stats.assetsQueued}.`,{doneAssets:e.doneAssets.size,assetsQueued:e.stats.assetsQueued,phase:"download-assets"});}function _a(e){return Te(e.toString("base64"),40)}function ro(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 no(e,t,r){let n=e.assetVariantDigestsByOriginalPath.get(t);n||(n=new Set,e.assetVariantDigestsByOriginalPath.set(t,n)),n.add(r);}async function an(e,t,r,n,s,o,i){let c=Ft(e,r),a=_a(n),d=c.originalFilePath,l=c.originalPublicUrlWithSearch;if(c.preferQueryHashed&&c.hashedFilePath&&c.hashedPublicUrl&&ro(s,c.originalFilePath,a)){let m=Gs(e,t,r);m?(d=m.outputPath,l=m.publicUrl):(d=c.hashedFilePath,l=c.hashedPublicUrl);}sn(e,t,r,l,c.originalPublicUrl);let u=Ee(d)?Buffer.from(Br(n.toString("utf8"),e,t,d),"utf8"):n,g=await eo(d),f=false;return (g===null||!g.equals(u))&&(await Le(d,u),f=true),o?.debug(`${f?"Saved":"Skipped unchanged"} asset output ${r}`,{phase:"download-assets",url:r,outputPath:d,bytes:u.length,wroteFile:f}),no(s,c.originalFilePath,a),await Wa(d,i),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:l,wroteFile:f}}function nt(e,t,r,n,s){let o=()=>{let u=String(s??"").trim();if(!u)return {discoveredFrom:"",discoveredFromType:"unknown"};if(u==="seed-path"||u==="cli"||u==="generated-404-request-path")return {discoveredFrom:u,discoveredFromType:u};if(/^https?:\/\//i.test(u))try{let g=new URL(u);return wt(g)?{discoveredFrom:u,discoveredFromType:"sitemap"}:H(e,g)?{discoveredFrom:u,discoveredFromType:"asset"}:Ve(g)&&!rt(g)?{discoveredFrom:u,discoveredFromType:"page"}:{discoveredFrom:u,discoveredFromType:"url"}}catch{return {discoveredFrom:u,discoveredFromType:"url"}}return {discoveredFrom:u,discoveredFromType:"url"}},i=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,c=oe(e,r,i,n,"page");if(!c)return;if(!Ve(c)||Xs(c)){if(H(e,c)){n.info(`Seeded/non-page URL queued as asset: ${c.toString()}`,{url:c.toString(),source:s}),K(e,t,c.toString(),n,s);return}n.reject("page",c.toString(),rt(c)?"asset/internal path cannot be page":"not page-like",s);return}let a=c.toString(),{discoveredFrom:d,discoveredFromType:l}=o();d&&!t.pageDiscoverySources.has(a)&&t.pageDiscoverySources.set(a,d),!t.donePages.has(a)&&!t.queuedPages.has(a)&&(t.queuedPages.add(a),t.pageQueue.push(a),t.stats.pagesQueued++,n.debug(`Queued discovered page ${a}`,{phase:"discovery",url:a,discoveredFrom:d,discoveredFromType:l,outputPath:Me(e,a)}),(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 K(e,t,r,n,s){let o=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,i=oe(e,r,o,n,"asset");if(!i)return;if(!H(e,i)){n.reject("asset",i.toString(),"not a safe asset path/prefix",s);return}let c=i.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 ur(e,t,r,n,s){let o=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,i=oe(e,r,o,n,"sitemap",Ln(s));if(!i)return;if(!wt(i)){n.reject("sitemap",i.toString(),"not sitemap-like",s);return}let c=i.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 Ba(e){let t=qe(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:et(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 Va(e){return e.split(",").map(t=>t.trim().split(/\s+/)[0]).filter(Boolean)}function Ka(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 Ha(e){return e.replace(/<script\b[^>]*>[\s\S]*?<\/script\s*>/gi,"")}function za(e,t){if(t==="dom-style")return true;let r=(()=>{try{return v.extname(new URL(e).pathname).toLowerCase()}catch{return ""}})();return !(r===".js"||r===".mjs"||r===".map"||t.includes("javascript"))}function cr(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 i of o){let c=v.join(e,i.name),a=v.join(t,i.name);if(i.isDirectory()){s.push(...await so(c,a,r,n));continue}await oo(c,a,r,n)&&s.push(a);}return s}async function Ja(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 oo(e,t,r,n){let s=v.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 Ja(e,t)?(n.debug("Skipped unchanged post-crawl copy target",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):(await F.mkdir(v.dirname(t),{recursive:true}),await F.copyFile(e,t),true)}async function Qa(e,t,r){let n=Object.entries(e.postCrawlCopyMap||{}).map(([c,a])=>({sourcePath:c.trim(),prefix:String(a||"").trim()}));if(n.length===0)return [];let s=0,o=[],i=new Set([...t.donePages].map(c=>v.resolve(Me(e,c))));r.mark("copy-extra-paths");for(let c of n){let a=c.sourcePath,d=c.prefix;if(!a||!d){r.warn("Skipped post-crawl copy mapping with empty key/value",{sourcePath:a,prefixPath:d});continue}let l=la(a);if(!l.resolvedPath){r.warn("Skipped post-crawl copy source because alias root is not configured",{sourcePath:a,alias:l.alias||"",requiredEnv:l.requiredEnv||"",prefixPath:d});continue}let u=l.resolvedPath,g=cr(d);if(!g){r.warn("Skipped post-crawl copy mapping with invalid export prefix",{sourcePath:a,prefixPath:d});continue}let f;try{f=await F.stat(u);}catch{r.warn("Skipped post-crawl copy source because it does not exist",{sourcePath:a,sourceAbs:u,prefixPath:d});continue}let m=v.resolve(e.outputDir,g);if(f.isDirectory()){let y=await so(u,m,i,r);o.push(...y),s+=y.length,y.length>0&&r.progress(`Copied static directory to export: ${u} -> /${g}`,{phase:"copy-extra-paths",sourcePath:u,targetPath:`/${g}`,copiedItems:s,changedFiles:y.length});continue}let w=v.basename(u),p=d.endsWith("/")?v.join(m,w):m;await oo(u,p,i,r)&&(o.push(p),s++,r.progress(`Copied static file to export: ${u} -> /${cr(d.endsWith("/")?`${d}${w}`:d)}`,{phase:"copy-extra-paths",sourcePath:u,targetPath:`/${cr(d.endsWith("/")?`${d}${w}`:d)}`,copiedItems:s}));}return r.endMark("copy-extra-paths",{mappedSources:n.length,copiedItems:s}),o}function ht(e,t,r,n,s){let o=new Set,i=qe(r).replace(/\\\//g,"/"),c=s==="page-html"?Ha(i):i,a=za(t,s);if(a){for(let d of Ka(c)){let l=oe(e,d,t,n,`${s}:css-url`);l&&H(e,l)&&o.add(l.toString());}for(let d of c.matchAll(/@import\s+(?:url\()?\s*['"]?([^'"\s;]+)['"]?\s*\)?/gi)){let l=oe(e,d[1],t,n,`${s}:css-import`);l&&H(e,l)&&o.add(l.toString());}}if(a||s==="page-html")for(let d of c.matchAll(/<\?xml-stylesheet[^>]+href=["']([^"']+)["'][^>]*\?>/gi)){let l=oe(e,d[1],t,n,`${s}:xml-stylesheet`);l&&H(e,l)&&o.add(l.toString());}if(s==="page-html")for(let d of ls(i)){let l=d.startsWith("//")?`${new URL(e.sourceOrigin).protocol}${d}`:d;if(nn(l)){n.ignore(`${s}:serialized-url`,l,"looks like JavaScript/code fragment",t);continue}let u=oe(e,l,t,n,`${s}:serialized-url`);u&&H(e,u)?o.add(u.toString()):u&&n.ignore("asset",u.toString(),"serialized URL did not pass safe asset path filter",t);}if(s!=="page-html"&&Na(t))for(let d of us(i)){if(nn(d)||!/\.(?:js|mjs)(?:[?#]|$)/i.test(d))continue;let l=oe(e,d,t,n,`${s}:relative-asset`);l&&H(e,l)&&o.add(l.toString());}return [...o]}function io(e,t,r,n){let s=new Set,o=qe(r);for(let i of o.matchAll(/href=["']([^"'#\s][^"']*?)["']/gi)){let c=i[1],a=oe(e,c,t,n,"page-link");a&&(Zs(e,a)||Ve(a)&&!rt(a)&&!H(e,a)&&s.add(a.toString()));}return [...s]}async function js(e,t,r,n,s,o,i=false,c=[]){let a=new Set(c),d=0,l=new Map,u=(g,f)=>{let m=[f],w=new Set;for(;m.length>0;){let p=m.pop();if(p===g)return true;w.has(p)||(w.add(p),m.push(...l.get(p)??[]));}return false};for(;r.sitemapQueue.length>0;){let g=r.sitemapQueue.shift();r.queuedSitemaps.delete(g);let f=Ce(g,n,"sitemap","sitemap-queue");if(!f)continue;let m=f.toString();if(r.doneSitemaps.has(m))continue;r.doneSitemaps.add(m),n.sitemap(`Fetching sitemap ${m}`,{url:m});let w=true;try{let p=await t.get(m,{timeout:6e4});if(w=!1,!p.ok()){if(n.skip("sitemap",m,p.status()),i&&!a.has(m))throw new Error(`Required child content-sync sitemap returned HTTP ${p.status()}: ${m}`);continue}let h=await p.body(),y=p.url()||m,b=Ce(y,n,"sitemap-response",m);if(!b)continue;let P=b.toString();r.successfulSitemaps.add(m),r.successfulSitemaps.add(P),a.has(m)&&(r.resolvedSitemapRoots.add(P),d+=1);let $=await an(e,r.assetMap,P,h,r,n);ue(s,e,$.outputPath),ve(s,$.outputPath);let x=h.toString("utf8");/<sitemapindex(?:\s|>)/i.test(x)?r.sitemapIndexUrls.add(P):r.sitemapLeafUrls.add(P);let I=ht(e,P,x,n,"sitemap");for(let C of I)K(e,r,C,n,P);let R=[];for(let C of Ba(x)){let O=oe(e,C.loc,P,n,"sitemap-loc",!0);if(!O){if(i)throw new Error(`Content-sync sitemap contains an inadmissible location: ${C.loc}`);continue}if(wt(O)){if(u(P,O.toString()))throw new Error(`Sitemap index cycle detected: ${P} -> ${O.toString()}`);if(l.has(P)||l.set(P,new Set),l.get(P).add(O.toString()),R.push(O.toString()),i&&r.doneSitemaps.has(O.toString())&&!r.successfulSitemaps.has(O.toString()))throw new Error(`Required child content-sync sitemap was not resolved: ${O.toString()}`);ur(e,r,O.toString(),n,P);}else if(i){if(Ve(O)&&!H(e,O)){let V=o.requiredUrls.has(O.toString()),U=o.manifest.pages[O.toString()],S=!!U&&!!await tt(U.outputPath);if(!V&&!S)throw new Error(`Content-sync sitemap coverage is incomplete for ${O.toString()}.`)}n.ignore("sitemap-loc",O.toString(),"content-sync sitemap-only traversal does not enqueue urlset locations",P);}else H(e,O)?K(e,r,O.toString(),n,P):Ve(O)?(C.lastmod&&(r.sitemapLastmodByPage[O.toString()]=C.lastmod),nt(e,r,O.toString(),n,P)):n.reject("sitemap-loc",O.toString(),"not page/sitemap/asset-like",P);}o.trackManifest&&(o.manifest.assets[P]={url:P,outputPath:$.outputPath,isText:!0,contentType:p.headers()["content-type"]??"application/xml",cacheControl:en(p.headers()),changeToken:null,tokenSource:null,discoveredAssets:[...new Set([...I,...R])],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!1},await Be(e,o));}catch(p){n.error(`Failed sitemap ${m}`,{url:m,error:String(p)});let h=i&&a.has(m)&&w;if(i&&!h)throw p}}if(i&&a.size>0&&d===0)throw new Error(`None of the configured content-sync sitemap roots could be resolved: ${[...a].join(", ")}`)}async function Ga(e,t,r,n,s,o,i){let c=oe(e,n,e.sourceOrigin,s,"asset-fetch");if(!c||!H(e,c))return;let a=c.toString();if(r.doneAssets.has(a))return;let d=o.enabled||o.strictTargeted?o.previousManifest.assets[a]:void 0,l=o.trackManifest?await ar(e,t,o,a,s):null,u=l?.token??null,g=Vr(d?.changeToken,l),f=o.strictTargeted&&(!l?.supported||!l.token)&&Kr(e.sourceOrigin,a),m=o.strictTargeted?o.manifest.assets[a]:void 0;if(m?.lastSeenRunId===o.runId&&await tt(m.outputPath)){r.doneAssets.add(a);for(let p of m.discoveredAssets||[])K(e,r,p,s,a);ue(i,e,m.outputPath),gt(i,e,m.outputPath,m.cacheControl),m.isText&&ve(i,m.outputPath),s.info(`Resumed completed content-sync asset ${a}`,{url:a,source:"content-sync-checkpoint"}),Qe(r,s);return}if(d&&(g||f)&&await tt(d.outputPath)){o.manifest.assets[a]={...d,changeToken:u,tokenSource:l?.tokenSource??d.tokenSource,discoveredAssets:[...d.discoveredAssets||[]],lastSeenRunId:o.runId,unreferencedRuns:0},delete o.manifest.pages[a],r.doneAssets.add(a);for(let p of d.discoveredAssets||[])K(e,r,p,s,a);gt(i,e,d.outputPath,d.cacheControl),s.info(`Reused unchanged trusted asset ${a}.`,{url:a,tokenSource:l?.tokenSource??"unknown",phase:"download-assets",source:g?"asset-change-token-match":"content-sync-release-fingerprint"}),Qe(r,s);return}let w=(p,h={})=>{if(!d||o.strictTargeted)return false;o.manifest.assets[a]={...d,discoveredAssets:[...d.discoveredAssets||[]],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0},delete o.manifest.pages[a],r.doneAssets.add(a);for(let y of d.discoveredAssets||[])K(e,r,y,s,a);return s.warn(`Keeping previous incremental asset ${a} after fetch issue.`,{url:a,reason:p,preservedFromPreviousManifest:true,...h,phase:"download-assets"}),Qe(r,s),true};s.asset(`Fetching asset ${a}`,{url:a});try{let p=ft(e,r.assetMap,a),h=p?await tt(p):null;if(p&&h&&!o.strictTargeted)try{let U=await se(`fetch asset headers ${a}`,6e4,()=>t.head(a,{timeout:6e4})),S=U.headers();if(U.ok()&&qa(h,S)){let W=await eo(p);if(W!==null){let ce=U.url()||a,E=Ce(ce,s,"asset-response",a);if(!E)return;let D=E.toString(),j=await an(e,r.assetMap,D,W,r,s,S);j.wroteFile&&(ue(i,e,j.outputPath),Ee(j.outputPath)&&ve(i,j.outputPath));let X=en(S)||d?.cacheControl||null;gt(i,e,j.outputPath,X),r.doneAssets.add(a),s.info(`Reused existing asset ${a} based on response headers.`,{url:a,filePath:p,lastModified:S["last-modified"]||"",contentLength:S["content-length"]||"",phase:"download-assets"}),Qe(r,s);let Y=on(c,S),ee=d?.discoveredAssets?[...d.discoveredAssets]:[];if(Y||d?.isText){let z=W.toString("utf8"),de=ht(e,D,z,s,"asset:cached");(de.length>0||!d)&&(ee=de);for(let ye of ee)K(e,r,ye,s,D);}if(o.trackManifest){let z=Y||!!d?.isText;o.manifest.assets[a]={url:a,outputPath:j.outputPath,isText:z,contentType:Xr(S)||d?.contentType||null,cacheControl:X,changeToken:l?.supported?l.token:null,tokenSource:l?.supported?l.tokenSource??null:null,discoveredAssets:ee,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!z},delete o.manifest.pages[a],(o.enabled||o.strictTargeted)&&z&&await Be(e,o);}return}}}catch{}let y=await se(`fetch asset ${a}`,6e4,()=>t.get(a,{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()}: ${a}`);s.skip("asset",a,y.status());return}let b=await se(`read asset response ${a}`,6e4,()=>y.body()),P=y.url()||a,$=Ce(P,s,"asset-response",a);if(!$)return;let x=$.toString(),I=y.headers(),R=await an(e,r.assetMap,x,b,r,s,I);R.wroteFile&&(ue(i,e,R.outputPath),Ee(R.outputPath)&&ve(i,R.outputPath));let C=en(I);gt(i,e,R.outputPath,C),r.doneAssets.add(a),Qe(r,s);let O=on(c,I),V=[];if(O){let U=b.toString("utf8");V=ht(e,x,U,s,`asset:${v.extname(c.pathname).toLowerCase()||I["content-type"]||"unknown"}`);for(let S of V)K(e,r,S,s,x);}o.trackManifest&&(o.manifest.assets[a]={url:a,outputPath:R.outputPath,isText:O,contentType:Xr(I),cacheControl:C,changeToken:l?.supported?l.token:null,tokenSource:l?.supported?l.tokenSource??null:null,discoveredAssets:V,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!O},delete o.manifest.pages[a],(o.enabled||o.strictTargeted)&&O&&await Be(e,o));}catch(p){if(w("fetch-error",{error:String(p)}))return;if(s.error(`Failed asset ${a}`,{url:a,error:String(p)}),o.strictTargeted)throw p}finally{r.doneAssets.has(a)||(r.doneAssets.add(a),Qe(r,s));}}async function Za(e,t,r,n,s,o){let i=Math.max(1,Number(e.assetDownloadConcurrency||e.concurrency||1)),c=Array.from({length:i},async()=>{for(;r.assetQueue.length>0;){let a=r.assetQueue.shift();a&&(r.queuedAssets.delete(a),await Ga(e,t,r,a,n,s,o));}});await Promise.all(c);}function dr(e,t){let r=fe(e.outputDir,t);if(!r)throw new Error(`Remote export output is outside outputDir: ${t}`);return r}function Ya(e,t){return on(e,{"content-type":t})}function Fs(e){return /(?:^|\/)index\.html?$|\.html?$/i.test(e)}async function Xa(e,t,r,n,s,o,i,c,a){let d=Math.max(1,Math.min(100,a)),l=20;for(;r.assetQueue.length>0;){let u=r.assetQueue.splice(0,d*l);for(let w of u)r.queuedAssets.delete(w);await un(e,t,s,u,n);let g=[];for(let w of u){let p=s.manifest.assets[w],h=p?fe(e.outputDir,p.outputPath):null,y=h?i.objects[h]:void 0,b=s.changeTokenCache.get(w)??null;if(!(s.enabled&&p&&y?.kind==="asset"&&y.sourceUrl===w&&(Vr(p.changeToken,b)||(!b?.supported||!b.token)&&Kr(e.sourceOrigin,w)))||!p||!h||!y){g.push(w);continue}s.manifest.assets[w]={...p,changeToken:b?.token??null,tokenSource:b?.tokenSource??p.tokenSource,lastSeenRunId:s.runId,unreferencedRuns:0};for(let $ of p.discoveredAssets||[])K(e,r,$,n,w);r.doneAssets.add(w),gt(o,e,p.outputPath,p.cacheControl),n.info(`Reused unchanged S3-native asset ${w}.`,{url:w,relativePath:h,tokenSource:b?.tokenSource??"release-fingerprint",phase:"download-assets",execution:"lambda"}),Qe(r,n);}if(g.length===0)continue;let f=[];for(let w=0;w<g.length;w+=l)f.push(g.slice(w,w+l));let m=await Promise.all(f.map(w=>c.fetch({urls:w,ignoreHttpsErrors:e.ignoreHttpsErrors,timeoutMs:6e4})));for(let w of m)for(let p of w.assets){let h=p.requestedUrl;try{if(p.outcome!=="fetched"||!p.object||!p.finalUrl){let C=p.error||`Remote asset fetch ended with ${p.outcome}.`;if(n.error(`Failed remote asset ${h}`,{url:h,status:p.httpStatus,outcome:p.outcome,error:C,execution:"lambda"}),s.strictTargeted)throw new Error(C);continue}let y=Ce(p.finalUrl,n,"remote-asset-response",h);if(!y)continue;let b=Ft(e,y.toString()),P=b.originalFilePath,$=b.originalPublicUrlWithSearch;if(b.preferQueryHashed&&b.hashedFilePath&&b.hashedPublicUrl&&ro(r,b.originalFilePath,p.object.sha256)){let C=Gs(e,r.assetMap,y.toString());P=C?.outputPath??b.hashedFilePath,$=C?.publicUrl??b.hashedPublicUrl;}sn(e,r.assetMap,y.toString(),$,b.originalPublicUrl),y.toString()!==h&&sn(e,r.assetMap,h,$,b.originalPublicUrl),no(r,b.originalFilePath,p.object.sha256);let x=dr(e,P),I=p.contentType?.trim()||"application/octet-stream",R=Ya(y,I);mt(i,{relativePath:x,sourceUrl:h,kind:"asset",contentType:I,cacheControl:p.cacheControl,isText:R,object:p.object}),ue(o,e,P),R&&ve(o,P),gt(o,e,P,p.cacheControl);for(let C of p.discoveredAssets)K(e,r,C,n,y.toString());r.stats.assetsSaved++,s.trackManifest&&(s.manifest.assets[h]={url:h,outputPath:P,isText:R,contentType:I,cacheControl:p.cacheControl,changeToken:null,tokenSource:null,discoveredAssets:[...p.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:p.object.bytes,taskId:w.taskId,attempts:w.attempts,execution:"lambda"});}finally{r.doneAssets.add(h),Qe(r,n);}}}}async function ec(e,t,r,n){for(let s of [...t.changedFiles].sort()){if(r.objects[s])continue;let o=v.join(e.outputDir,s),i=await F.readFile(o).catch(l=>{throw new Error(`Coordinator-owned output is missing before S3 staging: ${s}`,{cause:l})}),c=Ee(o),a=String(Ps.lookup(s)||"")||(c?"text/plain; charset=utf-8":"application/octet-stream"),d=await n.writeWorkspaceObject({key:`${r.workspace.prefix}jobs/${r.jobId}/coordinator/${Te(s,40)}-${v.basename(s)}`,body:i,contentType:a,cacheControl:Fs(s)?e.s3.htmlCacheControl:e.s3.assetCacheControl});mt(r,{relativePath:s,sourceUrl:null,kind:"generated",contentType:a,cacheControl:Fs(s)?e.s3.htmlCacheControl:e.s3.assetCacheControl,isText:c,object:d});}}function tc(e){let t=Math.ceil(e/1e3);return `${t} second${t===1?"":"s"}`}function _e(e){return Math.max(6e4,e.navigationTimeoutMs*2,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+15e3)}function Et(e){return Math.max(15e3,Math.min(6e4,_e(e)))}function ao(e){return Math.max(15e3,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+1e4)}function pn(e){return e.stats.pagesCompleted}function cn(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 Yr(e,t,r){let n=cn(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 Ot(e,t));},t);})])}finally{n!==void 0&&globalThis.clearTimeout(n);}}var Ot=class extends Error{constructor(r,n){super(`${r} timed out after ${tc(n)}.`);this.operation=r;this.timeoutMs=n;this.name="OperationTimeoutError";}operation;timeoutMs};async function rc(e,t){let r=ao(e);await t.evaluate(async n=>{await new Promise(s=>{let o=0,i=700,c=Date.now(),a=()=>{window.clearInterval(d),window.scrollTo(0,0),s();},d=window.setInterval(()=>{if(Date.now()-c>=n.maxDurationMs){a();return}window.scrollBy(0,i),o+=i;let l=document.body?.scrollHeight||0,u=document.documentElement?.scrollHeight||0,g=Math.max(l,u);o>=g+window.innerHeight&&a();},120);});},{maxDurationMs:r});}async function nc(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 rc(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=>dn(t,r))}async function oc(e,t,r,n,s=true){let o=new Set;if(s){let c=await t.evaluate(()=>{let a=new Set,d=["href","src","poster","data-src","data-lazy-src","data-original","data-bg","data-background","data-href","data-resource-url"],l=new Set,u=["srcset","data-srcset","data-lazy-srcset"],g=f=>{f.querySelectorAll("*").forEach(m=>{for(let p of d){let h=m.getAttribute(p);h&&a.add(h);}for(let p of u){let h=m.getAttribute(p);h&&h.split(",").forEach(y=>{let b=y.trim().split(/\s+/)[0];b&&a.add(b);});}let w=m.getAttribute("style");w&&l.add(w),m.shadowRoot&&g(m.shadowRoot);});};return g(document),{attrs:[...a],styles:[...l]}});for(let a of c.attrs)for(let d of Va(a)){let l=oe(e,d,r,n,"dom-attr");l&&H(e,l)&&o.add(l.toString());}for(let a of c.styles)for(let d of ht(e,r,a,n,"dom-style"))o.add(d);}let i=await t.content();for(let c of ht(e,r,i,n,"page-html"))o.add(c);return [...o]}async function Ds(e){return await e.evaluate(()=>{document.querySelectorAll("#wpsuite-consent-root").forEach(t=>t.remove()),document.documentElement.classList.remove("wpsuite-consent-scroll-locked"),document.body.classList.remove("wpsuite-consent-scroll-locked");}),e.content()}async function ln(e,t,r,n,s){let o=Me(e,t);await Le(o,r,"utf8");let i=await F.stat(o);s?.debug(`Saved page output ${t}`,{phase:"save-pages",url:t,outputPath:o,bytes:i.size}),n.stats.pagesSaved++;let c=Math.min(n.stats.pagesQueued,pn(n)+1);s&&(n.stats.pagesSaved%10===0||n.stats.pagesSaved===1)&&s.progress(`Page progress: processed ${c}, rendered ${n.stats.pagesRendered}, saved ${n.stats.pagesSaved}, discovered ${n.stats.pagesQueued}.`,{donePages:c,claimedPages:n.donePages.size,pagesRendered:n.stats.pagesRendered,pagesSaved:n.stats.pagesSaved,pagesQueued:n.stats.pagesQueued,pageQueue:n.pageQueue.length,phase:"save-pages"});}function lo(e){let t=pn(e);return t%5===0||t===1||t===e.stats.pagesQueued}function uo(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 ic(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 ac(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 ic(n),n}async function cc(e,t,r,n,s,o,i,c,a,d){for(;t.pageQueue.length>0&&!(e.maxPages>0&&t.donePages.size>=e.maxPages);){let l=t.pageQueue.shift();if(!l)break;t.queuedPages.delete(l);try{let u=Ce(l,r,"page","page-queue");if(!u)continue;let g=u.toString();if(l=g,t.queuedPages.delete(l),t.donePages.has(l))continue;t.donePages.add(l),o.enabled&&o.seenPages.add(l);let f=pr(e,l),m=co(e,u.pathname);if(!Ve(u)||H(e,u)||rt(u)){if(H(e,u)){r.info(`Remote worker redirected non-page URL to asset queue: ${l}`,{url:l,source:"remote-worker-guard"}),K(e,t,l,r,"remote-worker-guard");continue}r.reject("page",l,"guard rejected non-page URL before remote rendering","remote-worker");continue}let w=o.strictTargeted?o.manifest.pages[l]:void 0;if(w?.lastSeenRunId===o.runId&&await tt(w.outputPath)){o.seenPages.add(l);for(let R of w.discoveredAssets||[])K(e,t,R,r,l);ue(i,e,w.outputPath),ve(i,w.outputPath),r.info(`Resumed completed content-sync page ${l}`,{url:l,source:"content-sync-checkpoint"});continue}let p=await Js(e,s,t,o,l,r);if(o.enabled&&n!=="single-url"&&n!=="content-sync"){let R=o.manifest.pages[l];if(p.action==="reuse"&&R){for(let C of R.discoveredAssets)K(e,t,C,r,l);for(let C of R.discoveredPages)nt(e,t,C,r,l);R.lastSeenRunId=o.runId,R.sitemapLastmod=et(t.sitemapLastmodByPage[l])??R.sitemapLastmod,p.changeToken?.supported&&(R.changeToken=p.changeToken.token,R.tokenSource=p.changeToken.tokenSource??R.tokenSource),r.info(`Incremental reuse skipped unchanged page ${l}`,{url:l,mode:"incremental",reason:p.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}r.page(`Rendering remotely ${l}`,{url:l,execution:"lambda"});let h=await a.render({sourceOrigin:e.sourceOrigin,url:l,options:{ignoreHttpsErrors:e.ignoreHttpsErrors,javaScriptEnabled:!m,viewport:e.viewport,navigationTimeoutMs:e.navigationTimeoutMs,autoScrollTimeoutMs:ao(e),readiness:e.readiness},downloadHtml:!d}),y=h.page,b=y.finalUrl?Ce(y.finalUrl,r,"remote-final-response",l)?.toString():void 0;if(n==="content-sync"&&b&&b!==g){if(o.deleteOnMissingUrls.has(l)){await Mt(e,o,i,l,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive route redirected to ${b}`,{url:l,finalResponseUrl:b,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${l} -> ${b}`)}let P=y.httpStatus;if(f){if(P!==404){r.error(`Generated 404 request path returned unexpected status for ${l}`,{url:l,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(l)){await Mt(e,o,i,l,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive page ${l}`,{url:l,status:P,source:"content-sync-tombstone"});continue}if(n==="content-sync")throw new Error(`Required content-sync route returned HTTP ${P}: ${l}`);r.skip("page",l,P,t.pageDiscoverySources.get(l));continue}t.stats.pagesRendered++;let $=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 ht(e,g,h.html,r,"remote-page-html"))$.add(R);}for(let R of [...y.discoveredAssets,...y.networkUrls]){let C=oe(e,R,g,r,"remote-render");C&&H(e,C)&&$.add(C.toString());}for(let R of $)K(e,t,R,r,l);let x=n!=="single-url"&&n!=="content-sync"&&!f?[...new Set([...!d&&h.html!==null?io(e,l,h.html,r):[],...y.discoveredPages])]:[];for(let R of x)nt(e,t,R,r,l);for(let R of y.cookies){let C=R.sameSite==="Strict"||R.sameSite==="Lax"||R.sameSite==="None"?R.sameSite:"Unspecified",O=!Number.isFinite(R.expires)||R.expires<=0;c.add({name:R.name,domain:R.domain,path:R.path||"/",expires:O?null:R.expires,session:O,secure:R.secure,httpOnly:R.httpOnly,sameSite:C,partitioned:R.partitioned===!0});}let I=Me(e,l);if(d){if(!y.html)throw new Error("Remote render completed without a workspace HTML object.");mt(d,{relativePath:dr(e,I),sourceUrl:l,kind:"page",contentType:y.contentType?.trim()||"text/html; charset=utf-8",cacheControl:e.s3.htmlCacheControl,isText:!0,object:y.html});}else {if(h.html===null)throw new Error("Remote render did not return HTML for local output mode.");await ln(e,l,h.html,t,r);}ue(i,e,I),ve(i,I),o.trackManifest&&(o.manifest.pages[l]={url:l,outputPath:I,contentType:y.contentType?.trim()||null,changeToken:p.changeToken?.supported?p.changeToken.token:null,tokenSource:p.changeToken?.supported?p.changeToken.tokenSource??null:null,sitemapLastmod:et(t.sitemapLastmodByPage[l])??null,discoveredPages:x,discoveredAssets:[...$],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Be(e,o)),r.debug(`Remote render completed ${l}`,{url:l,taskId:h.taskId,attempts:h.attempts,execution:"lambda"});}catch(u){if(r.error(`Failed remote page ${l}`,{url:l,error:String(u),execution:"lambda"}),o.strictTargeted)throw u}finally{t.stats.pagesCompleted++,lo(t)&&uo(t,r);}}}async function lc(e,t,r,n,s,o,i,c){let a=await chromium.launch({headless:true}),d=cn(a);d.pid!==null&&r.debug(`Launched crawl worker browser PID ${d.pid}`,{browserPid:d.pid});try{for(;t.pageQueue.length>0;){a||(a=await chromium.launch({headless:!0}),d=cn(a),d.pid!==null&&r.debug(`Relaunched crawl worker browser PID ${d.pid}`,{browserPid:d.pid}));let l=a;if(e.maxPages>0&&t.donePages.size>=e.maxPages)break;let u=t.pageQueue.shift();if(!u)break;t.queuedPages.delete(u);let g=null,f=null,m=!1,w=[],p=new Set;try{let h=Ce(u,r,"page","page-queue");if(!h)continue;let y=h.toString();if(u=y,t.queuedPages.delete(u),t.donePages.has(u))continue;t.donePages.add(u),o.enabled&&o.seenPages.add(u);let b=pr(e,u),P=co(e,h.pathname);if(!Ve(h)||H(e,h)||rt(h)){if(H(e,h)){r.info(`Worker redirected non-page URL to asset queue: ${u}`,{url:u,source:"worker-guard"}),K(e,t,u,r,"worker-guard");continue}r.reject("page",u,"guard rejected non-page URL before rendering","worker");continue}let $=o.strictTargeted?o.manifest.pages[u]:void 0;if($?.lastSeenRunId===o.runId&&await tt($.outputPath)){o.seenPages.add(u);for(let E of $.discoveredAssets||[])K(e,t,E,r,u);ue(i,e,$.outputPath),ve(i,$.outputPath),r.info(`Resumed completed content-sync page ${u}`,{url:u,source:"content-sync-checkpoint"});continue}let x=await Js(e,s,t,o,u,r);if(o.enabled&&n!=="single-url"&&n!=="content-sync"){let E=o.manifest.pages[u];if(x.action==="reuse"&&E){for(let D of E.discoveredAssets)K(e,t,D,r,u);for(let D of E.discoveredPages)nt(e,t,D,r,u);E.lastSeenRunId=o.runId,E.sitemapLastmod=et(t.sitemapLastmodByPage[u])??E.sitemapLastmod,x.changeToken?.supported&&(E.changeToken=x.changeToken.token,E.tokenSource=x.changeToken.tokenSource??E.tokenSource),r.info(`Incremental reuse skipped unchanged page ${u}`,{url:u,mode:"incremental",reason:x.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}g=await se(`create page context ${u}`,_e(e),()=>ac(l,e,P)),f=await se(`create page ${u}`,_e(e),()=>g.newPage()),w=await g.cookies();let I=!1,R=new Set;await f.route("**/*",async E=>{let D=E.request();try{let j=oe(e,D.url(),e.sourceOrigin,r,"network-request");j&&H(e,j)&&(R.add(j.toString()),K(e,t,j.toString(),r,u));}catch{}if(D.isNavigationRequest()&&D.frame()===f.mainFrame())try{let j=new URL(D.url()),X=new URL(y);if(j.hash="",X.hash="",j.href===X.href){if(I){r.warn(`Blocked same-page navigation/reload for ${u}`,{url:u,requestUrl:D.url()});try{await E.abort("aborted");}catch(Y){r.debug(`Ignored canceled route while blocking reload for ${u}`,{url:u,error:String(Y)});}return}I=!0;}}catch{}try{await E.continue();}catch(j){r.debug(`Ignored canceled network route for ${u}`,{url:u,requestUrl:D.url(),error:String(j)});}}),f.on("response",E=>{try{let j=oe(e,E.url(),e.sourceOrigin,r,"network-response");j&&H(e,j)&&(R.add(j.toString()),K(e,t,j.toString(),r,u));}catch{}let D=E.headerValues("set-cookie").then(j=>{for(let X of j)c.add(fs(X,E.url()));}).catch(()=>{}).then(()=>{});p.add(D),D.finally(()=>p.delete(D));}),r.page(`Rendering ${u}`,{url:u});let C=null,O=null;try{if(C=await f.goto(u,{waitUntil:"domcontentloaded",timeout:e.navigationTimeoutMs}),C&&C.ok())try{O=await se(`read response body ${u}`,_e(e),()=>C.text());}catch{O=null;}}catch(E){r.warn(`Navigation issue for ${u}; saving current DOM if available`,{url:u,error:String(E)});}if(n==="content-sync"&&C){let E=Ce(C.url(),r,"content-sync-final-response",u)?.toString();if(E&&E!==y){if(o.deleteOnMissingUrls.has(u)){await Mt(e,o,i,u,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive route redirected to ${E}`,{url:u,finalResponseUrl:E,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${u} -> ${E}`)}}if(b){let E=C?.status()??null;if(E!==404){r.error(`Generated 404 request path returned unexpected status for ${u}`,{url:u,expectedStatus:404,status:E,requestPath:e.generated404RequestPath});continue}r.info(`Capturing rendered 404 page for ${u}`,{url:u,status:E,outputPath:Me(e,u)});}else if(C&&!C.ok()){if(n==="content-sync"&&(C.status()===404||C.status()===410)&&o.deleteOnMissingUrls.has(u)){await Mt(e,o,i,u,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive page ${u}`,{url:u,status:C.status(),source:"content-sync-tombstone"}),await f.close();continue}if(n==="content-sync")throw new Error(`Required content-sync route returned HTTP ${C.status()}: ${u}`);r.skip("page",u,C.status(),t.pageDiscoverySources.get(u)),await f.close();continue}P?(r.info(`Rendering without JS execution for ${u}`,{url:u,mode:"no-js"}),await se(`wait for rendered page ${u}`,_e(e),()=>sc(e,f))):await se(`wait for rendered page ${u}`,_e(e),()=>nc(e,f)),t.stats.pagesRendered++;let V=await se(`extract rendered assets ${u}`,_e(e),()=>oc(e,f,y,r,!P));for(let E of V)K(e,t,E,r,u);let U=[...new Set([...V,...R])],S=O??await se(`capture rendered html ${u}`,_e(e),()=>Ds(f)),W=n!=="single-url"&&n!=="content-sync"&&!b?io(e,u,S,r):[];if(n!=="single-url"&&n!=="content-sync"&&!b)for(let E of W)nt(e,t,E,r,u);await ln(e,u,S,t,r);let ce=Me(e,u);if(ue(i,e,ce),ve(i,ce),o.trackManifest){let E=C?.headers();o.manifest.pages[u]={url:u,outputPath:ce,contentType:Xr(E),changeToken:x.changeToken?.supported?x.changeToken.token:null,tokenSource:x.changeToken?.supported?x.changeToken.tokenSource??null:null,sitemapLastmod:et(t.sitemapLastmodByPage[u])??null,discoveredPages:W,discoveredAssets:U,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Be(e,o);}}catch(h){if(m=h instanceof Ot,o.strictTargeted)throw r.error(`Failed required content-sync page ${u}`,{url:u,error:String(h)}),h;if(m)r.warn(`Skipped partial DOM for timed-out page ${u}`,{url:u,error:String(h),recovery:"recycle-worker-browser"});else try{let y=f?await se(`capture partial html ${u}`,_e(e),()=>Ds(f)):"";if(y&&y.trim()){await ln(e,u,y,t,r);let b=Me(e,u);if(ue(i,e,b),ve(i,b),o.trackManifest){let P=o.manifest.pages[u]??o.previousManifest.pages[u];o.manifest.pages[u]={url:u,outputPath:b,contentType:P?.contentType??null,changeToken:null,tokenSource:null,sitemapLastmod:et(t.sitemapLastmodByPage[u])??P?.sitemapLastmod??null,discoveredPages:P?.discoveredPages?[...P.discoveredPages]:[],discoveredAssets:P?.discoveredAssets?[...P.discoveredAssets]:[],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Be(e,o);}r.warn(`Saved partial DOM for ${u}`,{url:u,error:String(h)});}}catch(y){y instanceof Ot&&(m=!0),r.error(`Could not save partial DOM for ${u}`,{url:u,error:String(y)});}r.error(`Failed page ${u}`,{url:u,error:String(h)});}finally{if(!m&&f&&await se(`drain network routes ${u}`,Et(e),()=>f.unrouteAll({behavior:"wait"})).catch(h=>{m=!0,r.warn(`Could not drain network routes for ${u}`,{url:u,browserPid:d.pid,error:String(h),recovery:"recycle-worker-browser"});}),m&&(a=null,await Yr(l,r,`page operation timeout for ${u}`),await l.close().catch(()=>{})),p.size>0&&await Promise.allSettled([...p]),!m&&g)try{let h=await g.cookies();c.addAll(hs(w,h));}catch{}t.stats.pagesCompleted++,lo(t)&&uo(t,r),!m&&f&&await se(`close page ${u}`,Et(e),()=>f.close()).catch(h=>{m=!0,r.warn(`Timed out while closing page ${u}`,{url:u,browserPid:d.pid,error:String(h),recovery:"recycle-worker-browser"});}),!m&&g&&await se(`close page context ${u}`,Et(e),()=>g.close()).catch(h=>{m=!0,r.warn(`Timed out while closing page context for ${u}`,{url:u,browserPid:d.pid,error:String(h),recovery:"recycle-worker-browser"});}),m&&a===l&&(a=null,await Yr(l,r,`page cleanup timeout for ${u}`),await l.close().catch(()=>{}));}}}finally{if(a){let l=a;await se("close worker browser",Et(e),()=>l.close()).catch(u=>(r.warn("Timed out while closing crawl worker browser",{browserPid:d.pid,error:String(u)}),Yr(l,r,"browser.close timeout")));}}}async function uc(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 Us(e){let t=String(e||"").toLowerCase();return t.includes("timeout")||t.includes("timed out")||t.includes("navigation timeout")}function dc(e){let t=String(e?.job?.command||"").trim(),r=String(e?.job?.crawlMode||"full").trim();return t!=="publish"&&t!=="crawl"?false:r!=="incremental"}function pc(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 po(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 Ls(e){let t=String(e.originalFileName||"").toLowerCase();return t.endsWith("errors.jsonl")?0:t.endsWith("errors.json")?1:2}function mc(e,t){let r=Array.isArray(t?.artifacts)?t.artifacts.filter(n=>String(n?.role||"").trim()==="errors"&&String(n?.storedFileName||"").trim()!=="").sort((n,s)=>Ls(n)-Ls(s)).map(n=>v.join(e,String(n.storedFileName))):[];return [...new Set([...r,v.join(e,"errors.jsonl"),v.join(e,"errors.json"),v.join(e,"errors.jsonl.gz"),v.join(e,"errors.json.gz")])]}async function qs(e){let t=await po(v.join(e,"job.json"));for(let r of mc(e,t)){let n=await uc(r);if(n.length>0)return n}return []}async function gc(e){let t=v.join(e.logDir,"archive"),r;try{r=await F.readdir(t);}catch{return ""}let n="",s=0;for(let o of r){let i=v.join(t,o);if(!(await F.stat(i).catch(()=>null))?.isDirectory())continue;let a=await po(v.join(i,"job.json"));if(!dc(a))continue;let d=pc(a);d>=s&&(s=d,n=i);}return n}async function fc(e){let t=await gc(e),r=t!==""?await qs(t):await qs(e.logDir),n=new Set;for(let s of r)if(Us(s.error)||Us(s.message)){let o=s.url;typeof o=="string"&&o&&n.add(o);}return [...n]}async function hc(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 wc(e){if(!e)throw new Error("Content-sync crawl requires an immutable impact plan.");let t=JSON.parse(await F.readFile(e,"utf8"));return Rt(t),t}function yc(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(/\/+$/,""),i=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(i)})}function Tt(e,t,r){let n=r==="page"?Me(e,t):Ft(e,t).originalFilePath;return fe(e.outputDir,n)}async function Mt(e,t,r,n,s){let o=t.manifest.pages[n];if(!o)return;let i=fe(e.outputDir,o.outputPath);if(!i)throw new Error(`Manifest-owned page path escaped output root: ${n}`);let c=v.resolve(o.outputPath);Object.entries(t.manifest.pages).some(([d,l])=>d!==n&&!s.has(d)&&v.resolve(l.outputPath)===c)||(await F.rm(o.outputPath,{force:true}),r.deletedFiles.add(i),r.revalidateFiles.delete(i)),delete t.manifest.pages[n];}async function bc(e,t,r,n,s){let o=t.manifest.assets[n];if(!o)return;let i=fe(e.outputDir,o.outputPath);if(!i)throw new Error(`Manifest-owned resource path escaped output root: ${n}`);let c=v.resolve(o.outputPath);Object.entries(t.manifest.assets).some(([d,l])=>d!==n&&!s.has(d)&&v.resolve(l.outputPath)===c)||(await F.rm(o.outputPath,{force:true}),r.deletedFiles.add(i)),delete t.manifest.assets[n];}function Sc(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 i of o?.discoveredAssets??[])try{wt(new URL(i))&&n.push(i);}catch{}}return r}async function Pc(e,t,r,n,s){let o=Sc(r.previousManifest,s);for(let i of o){if(t.successfulSitemaps.has(i))continue;let c=r.previousManifest.assets[i];if(!c)continue;let a=fe(e.outputDir,c.outputPath);if(!a)throw new Error(`Manifest-owned sitemap path escaped output root: ${i}`);await F.rm(c.outputPath,{force:true}),n.deletedFiles.add(a),delete r.manifest.assets[i];}await Be(e,r);}async function Rc(e){try{let t=await F.readFile(v.join(e.outputDir,"asset-map.json"),"utf8");return JSON.parse(t)}catch{return {}}}async function vc(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(v.join(e,r));}catch(n){if(n.code!=="ENOENT")throw n}}));}function Ws(e,t,r,n,s){let o=oe(e,r,e.sourceOrigin,n,"manual-url");o&&(wt(o)?ur(e,t,o.toString(),n,s):H(e,o)?K(e,t,o.toString(),n,s):Ve(o)?nt(e,t,o.toString(),n,s):n.reject("manual-url",o.toString(),"not page/sitemap/asset-like",s));}async function mo(e=process.argv.slice(2)){let t=[],r=await xe(void 0,{onRemotePublisherConfigDiagnostic:k=>{t.push(k);}}),n=Ct(r.sitemapPaths),s=t.at(-1)??null,o=oa(e),i=o.mode==="content-sync"?await wc(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 fc(r)),o.mode==="single-url"&&(c=await hc(o.urls)),i&&(c=[...i.directRenderUrls,...i.archiveFamilyUrls,...i.resourceRenderUrls]);let a=o.crawlMode==="incremental"&&o.mode==="full",d=a&&typeof r.subscriptionType=="string"&&r.subscriptionType.trim()!=="";if(i&&r.subscriptionType!=="PROFESSIONAL"&&r.subscriptionType!=="AGENCY")throw new Error("Content-sync requires an active Professional or Agency subscription.");let l=(o.mode==="full"||o.mode==="single-url"||o.mode==="content-sync")&&!o.resumeRewrite,u=o.mode!=="full"||o.resumeRewrite||d,g=u?await Rc(r):{},f=d||o.mode==="single-url"||o.mode==="content-sync"?await ma(r):Ks(),m=new Set(c),w=new Set;if(i){for(let k of i.archiveFamilies)if(k.foundPosts===0)for(let L of k.pageUrls)w.add(L);for(let k of Object.keys(f.pages))yc(k,i)&&!m.has(k)&&(c.push(k),w.add(k));}let p={enabled:d,trackManifest:l,manifest:rn(f),previousManifest:rn(f),runId:i?.jobId??`${Date.now()}`,seenPages:new Set,changeTokenCache:new Map,pageChangeTokenPrimePromise:null,manifestSaveChain:Promise.resolve(),manifestPendingChanges:0,lastManifestSavedAt:Date.now(),strictTargeted:!!i,requiredUrls:m,deleteOnMissingUrls:w};await vc(r.logDir);let h=new Jt(r.logDir,r.logLevel),y=o.remoteRender??r.remoteWorkers?.render?.enabled===true,b=nr({explicitPath:o.remoteWorkerConfigPath,runtimeDir:jt()}),P=null,$=null,x=null,I=null,R=Math.max(1,r.concurrency),C=1,O=r.remoteWorkers?.rewrite?.enabled===true,V=r.remoteWorkers?.deploy?.enabled===true,U=y&&O&&V&&!o.resumeRewrite;if(!U&&o.mode==="full"&&!o.resumeRewrite&&await F.rm(sr(r),{force:true}),y&&!o.resumeRewrite||O){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 k=await Yt(b),L=false;if(x=new pt(k,{},{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=>{L||(L=true,h.warn("Lambda progress status could not be read; task execution continues.",{error:M.message}));}}),U){if(!k.functions.asset)throw new Error("Full Lambda delegation requires an asset worker. Redeploy the Lambda worker stack and reinstall remote-workers.json.");let M=u?await or(r):null;I=Is(`crawl-${Te(p.runId,40)}`,k,M),$=new er({client:x,jobId:I.jobId,maxAttempts:2}),h.info("S3-native Lambda data plane is enabled.",{execution:"lambda",assetWorker:k.functions.asset,workspaceBucket:k.workspace.bucket,workspacePrefix:k.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,U?r.concurrency:r.remoteWorkers?.render?.concurrency??r.concurrency)),C=Math.max(1,Math.min(20,r.remoteWorkers?.render?.batchSize??5)),P=new Xt({client:x,jobId:`crawl-${Te(p.runId,40)}`,maxAttempts:r.remoteWorkers?.render?.maxAttempts??2,batchSize:C,maxConcurrentBatches:R,onRetry:k=>{h.warn(`Retrying remote render for ${k.url}`,{url:k.url,attempt:k.attempt,maxAttempts:k.maxAttempts,reason:k.reason,execution:"lambda"});}}),h.info("Lambda page rendering is enabled.",{remoteWorkerConfigPath:v.resolve(b),concurrency:R,maxAttempts:r.remoteWorkers?.render?.maxAttempts??2,batchSize:C,execution:"lambda"});}if(s){let k={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.",k):h.warn("Remote WP Suite publisher configuration is unavailable or has no active subscription type.",k);}if(a&&!d){let k="Incremental crawl requires an active remote publisher subscription; refusing to fall back to a destructive full crawl.";throw h.error(k,{requestedMode:o.crawlMode,subscriptionType:r.subscriptionType??null,source:"incremental-subscription-guard"}),await h.flush(),new Error(k)}let S={pageQueue:[],queuedPages:new Set,donePages:new Set,pageDiscoverySources:new Map,assetQueue:[],queuedAssets:new Set,doneAssets:new Set,sitemapQueue:[],queuedSitemaps:new Set,doneSitemaps:new Set,successfulSitemaps:new Set,resolvedSitemapRoots:new Set,sitemapIndexUrls:new Set,sitemapLeafUrls:new Set,sitemapLastmodByPage:{},assetMap:{...g},assetVariantDigestsByOriginalPath:new Map,stats:{pagesQueued:0,pagesRendered:0,pagesCompleted:0,assetsQueued:0,sitemapsQueued:0,assetsSaved:0,pagesSaved:0}},W=pa(),ce=new Gt,E=ia();if(E&&!o.resumeRewrite&&await Zt(E,ce.artifact()),await Je(r),await zn(r,{startedAt:new Date().toISOString(),outputDir:v.resolve(r.outputDir)}),u||await F.rm(r.outputDir,{recursive:true,force:true}),await F.mkdir(r.outputDir,{recursive:true}),i){let k=new Set([...i.deleteUrls,...p.deleteOnMissingUrls]);for(let M of i.deleteUrls)await Mt(r,p,W,M,k);let L=new Set(i.resourceDeleteUrls);for(let M of i.resourceDeleteUrls)await bc(r,p,W,M,L);}if(o.resumeRewrite)h.info("Resuming final rewrite from existing output.",{phase:"rewrite-text",mode:o.mode,crawlMode:o.crawlMode});else {let k=await request.newContext({ignoreHTTPSErrors:r.ignoreHttpsErrors});try{if(h.mark("discovery"),o.mode==="full"?(n.forEach(M=>ur(r,S,M,h)),await js(r,k,S,h,W,p),r.seedPaths.forEach(M=>Ws(r,S,M,h,"seed-path")),r.generated404RequestPath&&nt(r,S,r.generated404RequestPath,h,"generated-404-request-path")):(c.forEach(M=>Ws(r,S,M,h,"cli")),i?.sitemapUrls.forEach(M=>ur(r,S,M,h,"content-sync-impact")),await js(r,k,S,h,W,p,o.mode==="content-sync",i?.sitemapUrls??[]),i&&await Pc(r,S,p,W,i.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(),E&&await Zt(E,ce.artifact());return}h.mark("render-pages");let L=Array.from({length:P?R*C:R},()=>P?cc(r,S,h,o.mode,k,p,W,ce,P,I??void 0):lc(r,S,h,o.mode,k,p,W,ce));if(await Promise.all(L),E&&await Zt(E,ce.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}),I&&$?await Xa(r,k,S,h,p,W,I,$,r.concurrency):(await un(r,k,p,[...S.assetQueue],h),await Za(r,k,S,h,p,W)),h.endMark("download-assets",{assets:S.doneAssets.size}),p.enabled&&r.maxPages===0&&(await Sa(r,S,p,h,W),o.mode==="full"&&await Pa(r,p,h,W)),o.mode==="full"){let M=await Qa(r,S,h);for(let G of M)if(ue(W,r,G),I&&x){let te=dr(r,G),T=await F.readFile(G),_=Ee(G),A=String(Ps.lookup(te)||"")||(_?"text/plain; charset=utf-8":"application/octet-stream"),re=await x.writeWorkspaceObject({key:`${I.workspace.prefix}jobs/${I.jobId}/copied/${Te(te,40)}-${v.basename(te)}`,body:T,contentType:A});mt(I,{relativePath:te,sourceUrl:null,kind:"generated",contentType:re.contentType,cacheControl:null,isText:_,object:re}),_&&ve(W,G);}}}finally{await se("dispose crawl request context",Et(r),()=>k.dispose()).catch(L=>{h.warn("Timed out while disposing crawl request context",{error:String(L)});});}}if(p.trackManifest&&await Be(r,p,true),I)for(let k of W.deletedFiles)As(I,k);let D=JSON.stringify(S.assetMap,null,2);if(await F.writeFile(v.join(r.outputDir,"asset-map.json"),D,"utf8"),I&&x){let k=await x.writeWorkspaceObject({key:`${I.workspace.prefix}jobs/${I.jobId}/generated/asset-map.json`,body:D,contentType:"application/json; charset=utf-8"});mt(I,{relativePath:"asset-map.json",sourceUrl:null,kind:"generated",contentType:"application/json; charset=utf-8",cacheControl:r.s3.assetCacheControl,isText:true,object:k});}JSON.stringify(g,null,2)!==JSON.stringify(S.assetMap,null,2)&&ue(W,r,v.join(r.outputDir,"asset-map.json")),I&&x&&await ec(r,W,I,x);let j=o.mode==="single-url"||o.mode==="content-sync"?Ta(r,S):p.enabled&&!o.resumeRewrite?Ra(r,p,W,g,S.assetMap):void 0;h.mark("rewrite-text");let X=o.mode==="single-url"||o.mode==="content-sync"?`Rewriting text files touched by URL crawl: 0/${j?.length??0}`:"Rewriting text files...";h.progress(X,{phase:"rewrite-text",index:0,totalFiles:j?.length,changedTextFiles:0}),await Zr(X);let Y=Date.now(),ee={files:j,previousAssetMap:g,onStart:({totalFiles:k,workerCount:L})=>{h.info(`Rewrite phase scanning ${k} text files.`,{phase:"rewrite-text",totalFiles:k,workerCount:L});},onProgress:async({index:k,totalFiles:L,changedTextFiles:M,file:G,changed:te})=>{te&&ue(W,r,v.join(r.outputDir,G)),h.checkpoint(`Rewriting text file ${k}/${L}`,{phase:"rewrite-text",index:k,totalFiles:L,changedTextFiles:M,file:G}),h.debug(`${te?"Rewrote":"Checked unchanged"} text file ${k}/${L}: ${G}`,{phase:"rewrite-text",index:k,totalFiles:L,changedTextFiles:M,file:G,changed:te});let T=Date.now();if(!(k===1||k===L||k%50===0||T-Y>=5e3))return;Y=T;let A=`Rewriting text files: ${k}/${L}`;h.progress(A,{phase:"rewrite-text",index:k,totalFiles:L,changedTextFiles:M,file:G}),await Zr(A);}},z;if(I&&x){let k=j?.map(M=>dr(r,v.isAbsolute(M)?M:v.join(r.outputDir,M))),L=Object.values(I.objects).filter(M=>M.isText&&(!k||k.includes(M.relativePath))).length;await ee.onStart?.({totalFiles:L,workerCount:Math.min(r.concurrency,L)}),z=await rr(r,I,S.assetMap,{client:x,jobId:I.jobId,concurrency:r.concurrency,batchSize:25,maxAttempts:2,...k?{relativePaths:k}:{},onProgress:ee.onProgress}),await Ts(r,I);}else O&&x?z=await tr(r,S.assetMap,{client:x,jobId:`crawl-${Te(p.runId,40)}`,concurrency:r.remoteWorkers?.rewrite?.concurrency??r.rewriteConcurrency,batchSize:r.remoteWorkers?.rewrite?.batchSize??25,maxAttempts:r.remoteWorkers?.rewrite?.maxAttempts??2,onStageProgress:async({index:k,totalFiles:L,file:M})=>{let G=Date.now();if(k!==1&&k!==L&&k%25!==0&&G-Y<5e3)return;Y=G;let te=`Staging text files for Lambda rewrite: ${k}/${L}`;h.progress(te,{phase:"rewrite-stage",index:k,totalFiles:L,file:M,execution:"lambda"}),await Zr(te);},options:ee}):z=await Kt(r,S.assetMap,ee);p.trackManifest&&(va(p,j),await Be(r,p,true)),h.endMark("rewrite-text",{changedTextFiles:z});let de=i?[...W.deletedFiles].sort():p.enabled&&!o.resumeRewrite?await ka(r,S,p,W,h,I):[],ye=[...new Set((j||[]).map(k=>fe(r.outputDir,v.isAbsolute(k)?k:v.join(r.outputDir,k))).filter(k=>k!==null))].sort();if(i){let k=[...W.changedFiles].sort(),L=new Set(k),M=new Set(i.directRenderUrls.map(q=>Tt(r,q,"page")).filter(q=>!!q&&L.has(q))),G=new Set(i.archiveFamilyUrls.map(q=>Tt(r,q,"page")).filter(q=>!!q&&L.has(q))),te=new Set(i.resourceRenderUrls.map(q=>Tt(r,q,"asset")).filter(q=>!!q&&L.has(q))),T=new Set([...S.sitemapLeafUrls].map(q=>Tt(r,q,"asset")).filter(q=>!!q&&L.has(q))),_=new Set([...S.sitemapIndexUrls].map(q=>Tt(r,q,"asset")).filter(q=>!!q&&L.has(q))),A=new Set([...M,...G,...te,...T,..._]),re=k.filter(q=>!A.has(q)),be=[...new Set([...i.directRenderUrls,...i.archiveFamilyUrls,...S.resolvedSitemapRoots,...S.sitemapLeafUrls,...S.sitemapIndexUrls,...i.deleteUrls,...i.resourceRenderUrls,...i.resourceDeleteUrls].map(q=>{let Se=new URL(q);return `${Se.pathname}${Se.search}`}))].sort();await Lr(r,{schemaVersion:2,generatedAt:new Date().toISOString(),outputDir:v.resolve(r.outputDir),runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,changedFiles:k,deletedFiles:de,rewriteTargets:ye,revalidateFiles:[...W.revalidateFiles].sort(),consumerId:i.consumerId,fromSequence:i.fromSequence,toSequence:i.toSequence,impactHash:i.impactHash,resolvedSitemapUrls:[...S.resolvedSitemapRoots],uploadGroups:{assets:re,content:[...M].sort(),listings:[...G].sort(),resources:[...te].sort(),sitemapLeaves:[...T].sort(),sitemapIndexes:[..._].reverse()},invalidationPaths:be});}else await Lr(r,{schemaVersion:1,generatedAt:new Date().toISOString(),outputDir:v.resolve(r.outputDir),runMode:o.mode,crawlMode:o.crawlMode,fullSyncRequired:!(p.enabled&&!o.resumeRewrite),changedFiles:p.enabled&&!o.resumeRewrite?[...W.changedFiles].sort():[],deletedFiles:p.enabled&&!o.resumeRewrite?de:[],rewriteTargets:p.enabled&&!o.resumeRewrite?ye:[],revalidateFiles:[...W.revalidateFiles].sort()});await Qn(r),h.summary(o.resumeRewrite?`Done. Resumed final rewrite over existing output and changed ${z} 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:z}),await h.flush();}function Cc(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 he(e,t,r){let n=new AbortController,s=globalThis.setTimeout(()=>{n.abort(new Error(`${e} timed out after ${Cc(t)}.`));},t);try{return await r({abortController:n,httpOptions:{abortSignal:n.signal,requestTimeout:t}})}finally{globalThis.clearTimeout(s);}}function kc(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function mr(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 xc(e,t){return {...e,...t??{}}}function Ic(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function yt(e,t){let r=String(t??e.deploymentTargetOverride??"").trim();if(!kc(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:xc(e.s3,s.s3),cloudFront:Ic(e.cloudFront,s.cloudFront)}}}var yo="wpsuite-sha256",bo="wpsuite-normalized-sha256",fn="public, max-age=0, must-revalidate",Fc=/(?:^|[-_:.])(id|uid|nonce|token|hash|instance)(?:$|[-_:.])/i,hn=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,Dc=["SIGINT","SIGTERM"],hr=new Set,gr=new Map,go=null;function Uc(e){hr.add(e);let t=false;return ()=>{t||(t=true,hr.delete(e));}}async function Lc(){let e=[...hr];e.length!==0&&await Promise.allSettled(e.map(async t=>{hr.delete(t),await F.rm(t,{recursive:true,force:true});}));}function qc(){if(!(gr.size>0))for(let e of Dc){let t=()=>{go||(go=Lc().finally(()=>{for(let[r,n]of gr)process.off(r,n);gr.clear(),process.exit(1);}));};gr.set(e,t),process.on(e,t);}}qc();function Wc(e,t){let r=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(r)return v.resolve(r);let n=t.trim();return n?v.join(v.resolve(n),"crawl-manifest.json"):v.join(v.resolve(e),".crawl-manifest.json")}function Ge(e){return v.normalize(e).split(v.sep).join("/")}function fo(e,t){let r=v.relative(v.resolve(e),v.resolve(t));return !r||r.startsWith("..")||v.isAbsolute(r)?null:Ge(r)}async function Nc(e,t,r){let n=Wc(e.outputDir,t),s;try{s=await F.readFile(n,"utf8");}catch(l){return l.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:l instanceof Error?l.message:String(l),source:"deploy-guard"}),null)}let o;try{o=JSON.parse(s);}catch(l){return r.warn("Deploy rewrite guard disabled because the crawl manifest is invalid JSON.",{manifestPath:n,error:l instanceof Error?l.message:String(l),source:"deploy-guard"}),null}let i=new Map,c=new Map,a=0,d=0;for(let[l,u]of Object.entries(o.pages||{})){let g=String(u?.outputPath||"").trim(),f=g?fo(e.outputDir,g):null;if(f){let m=typeof u?.contentType=="string"&&u.contentType?u.contentType:null;m&&c.set(f,m);}!g||u?.rewriteComplete===true||f&&(a++,i.has(f)||i.set(f,{kind:"page",sourceUrl:l,outputPath:v.resolve(g),relativePath:f}));}for(let[l,u]of Object.entries(o.assets||{})){let g=String(u?.outputPath||"").trim(),f=u?.isText===true,m=g?fo(e.outputDir,g):null;if(m){let w=typeof u?.contentType=="string"&&u.contentType?u.contentType:null;w&&c.set(m,w);}!g||!f||u?.rewriteComplete===true||m&&(d++,i.has(m)||i.set(m,{kind:"asset",sourceUrl:l,outputPath:v.resolve(g),relativePath:m}));}return {manifestPath:n,byRelativePath:i,contentTypeByRelativePath:c,pendingPages:a,pendingTextAssets:d}}function _c(e){return e&&(hn.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)?"__hex__":/^[0-9]{8,}$/.test(e)?"__num__":/^(?=.*[a-z])(?=.*\d)[a-z0-9]{8,}$/i.test(e)?"__alnum__":e)}function mn(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,r)=>r%2===1?t:_c(t)).join("")}function Bc(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,r)=>r%2===1?t:hn.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 wr(e,t){return Array.isArray(e)?e.map(r=>wr(r,t)):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([r,n])=>[r,wr(n,r)])):typeof e!="string"?e:t&&Fc.test(t)?Bc(e):hn.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)||/^[0-9]{8,}$/.test(e)?mn(e):e}function Vc(e){return e.replace(/((?:https?:\/\/[^"'\s>]+|\/)(?:wp-content|wp-includes)\/[^"'\s>?]+)\?ver=[A-Za-z0-9._-]+(?=$|[#"'])/gi,"$1")}function Kc(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(wr(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),i=JSON.stringify(wr(o));return Buffer.from(i,"utf8").toString("base64")}catch{}return t}function Hc(e,t){let r=e.toLowerCase();return r==="class"?t.split(/(\s+)/).map((n,s)=>s%2===1?n:mn(n)).join(""):r==="id"||r==="for"||r==="aria-controls"||r==="aria-labelledby"||r==="aria-describedby"||r==="aria-owns"||r==="aria-activedescendant"||r==="name"?mn(t):r==="href"||r==="src"?Vc(t):Kc(r,t)}function zc(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,i,c,a)=>{let d=Hc(i,a);return ` ${i}=${c}${d}${c}`});return `<${r}${s}>`})}function bt(e){let t=v.extname(e).toLowerCase();return [".html",".htm",".xml",".xsl",".txt"].includes(t)||["robots.txt","sitemap.xml"].includes(v.basename(e))}function fr(e,t){return [e.replace(/^\/+|\/+$/g,""),t.replace(/^\/+/,"")].filter(Boolean).join("/")}async function Jc(e,t,r,n){let s=new Map,o,i=0;do{i++;let c=await he(`S3 list objects page ${i} for s3://${t}/${r||""}`,3e5,({httpOptions:a})=>e.send(new ListObjectsV2Command({Bucket:t,Prefix:r||void 0,ContinuationToken:o}),a));for(let a of c.Contents||[]){if(!a.Key)continue;let d=(a.ETag||"").replace(/^"|"$/g,"").toLowerCase(),l=Number(a.Size??0);s.set(a.Key,{etag:d,size:l,cacheControl:void 0});}o=c.NextContinuationToken,n.debug(`Listed S3 keys page ${i}: ${s.size} keys so far`);}while(o);return s}async function Qc(e,t,r,n){if(n.has(r))return n.get(r)??null;try{let s=await he(`S3 head object ${r}`,3e5,({httpOptions:i})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),i)),o={etag:(s.ETag||"").replace(/^"|"$/g,"").toLowerCase(),size:Number(s.ContentLength??0),cacheControl:s.CacheControl?.trim()||null};return n.set(r,o),o}catch{return n.set(r,null),null}}async function Gc(e,t,r,n){if(n.cacheControl!==void 0)return n.cacheControl;try{let s=await he(`S3 head cache metadata ${r}`,3e5,({httpOptions:o})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),o));n.cacheControl=s.CacheControl?.trim()||null;}catch{n.cacheControl=null;}return n.cacheControl}function ho(e){return String(e||"").split(",").map(t=>t.trim().toLowerCase()).filter(Boolean).sort().join(",")}async function Zc(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 i=s.slice(o,o+1e3);if(i.length===0)continue;let c=await he(`S3 delete objects batch ${Math.floor(o/1e3)+1}`,3e5,({httpOptions:a})=>e.send(new DeleteObjectsCommand({Bucket:t,Delete:{Objects:i.map(d=>({Key:d}))}}),a));if(c.Errors&&c.Errors.length>0){let a=c.Errors.map(d=>`${d.Key||"(unknown)"}: ${d.Code||"DeleteFailed"}`);throw new Error(`S3 reported ${a.length} object deletion error(s): ${a.join(", ")}`)}n.info(`Deleted batch of ${i.length} object(s).`);for(let a of i)n.debug(` deleted: ${a}`);}return s}function Yc(e){return createHash("md5").update(e).digest("hex")}function Ze(e){return createHash("sha256").update(e).digest("hex")}function yr(e){return createHash("sha256").update(zc(e.toString("utf8"))).digest("hex")}async function Xc(e,t,r,n){if(n.has(r))return n.get(r)??null;try{let i=((await he(`S3 head object metadata ${r}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),c))).Metadata?.[yo]||"").trim().toLowerCase()||null;return n.set(r,i),i}catch{return n.set(r,null),null}}async function el(e,t,r,n,s){if(n.has(r))return n.get(r)??null;try{let i=((await he(`S3 head object normalized metadata ${r}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),c))).Metadata?.[bo]||"").trim().toLowerCase();if(i)return n.set(r,i),i}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 i=await(await he(`S3 get object ${r}`,3e5,({httpOptions:a})=>e.send(new GetObjectCommand({Bucket:t,Key:r}),a))).Body?.transformToByteArray(),c=i?yr(Buffer.from(i)):null;return s.set(r,c),n.set(r,c),c}catch{return s.set(r,null),n.set(r,null),null}}function tl(e){return e.replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"")||"profile"}function rl(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 nl(e,t,r,n){let s=v.join(e,"asset-map.json"),o;try{o=await F.readFile(s,"utf8");}catch(a){if(a.code==="ENOENT")return;throw a}let i=JSON.parse(o);if(!i||typeof i!="object")return;let c=Object.fromEntries(Object.entries(i).map(([a,d])=>{let l=String(d??"");return t.urlRewriteMode==="absolute"&&t.targetOrigin&&r.targetOrigin&&t.targetOrigin!==r.targetOrigin&&(l=l.split(t.targetOrigin).join(r.targetOrigin)),l=rl(l,n),[a,l]}));await F.writeFile(s,JSON.stringify(c,null,2),"utf8");}async function sl(e,t,r,n,s,o,i){let c=e.targetOrigin!==t.targetOrigin,a=Object.keys(n).length>0;if(!r||!c&&!a)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=v.join(v.dirname(e.outputDir),`.deploy-profile-${tl(r)}-${Date.now()}`),l=Uc(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 p=v.join(e.outputDir,w),h=v.join(d,w);await F.mkdir(v.dirname(h),{recursive:!0}),await F.copyFile(p,h);}}else await F.cp(e.outputDir,d,{recursive:!0});let u={...e,sourceOrigin:e.targetOrigin,targetOrigin:t.targetOrigin,extraReplacements:n,outputDir:d},g=1;s.info(`Rewriting text files for deployment profile "${r}"...`);let f={files:o,onStart:({totalFiles:w,workerCount:p})=>{g=p,s.info(`Rewriting ${w} text file(s) for deployment profile "${r}" with ${p} worker(s).`);},onProgress:({index:w,totalFiles:p,changedTextFiles:h,file:y,changed:b})=>{let $=`Rewriting [${w}/${p}] ${y} (${b?"rewrote":"checked"}; ${h} changed so far)`;p<=200||w<=5||w%10===0||w===p?(s.progress($,{file:y,index:w,totalFiles:p,changed:b,changedTextFiles:h,workerCount:g}),s.info($)):s.debug($);}},m=i?await tr(u,{},{...i,options:f}):await Kt(u,{},f);return await nl(d,e,t,n),s.info(`Prepared deployment profile "${r}" in ${d} (${m} rewritten text file(s)).`),{outputDir:d,cleanup:async()=>{l(),await F.rm(d,{recursive:!0,force:!0});}}}catch(u){throw s.error(`Failed to prepare deployment profile "${r}" temporary output.`,{tempDir:d,outputDir:e.outputDir,error:u instanceof Error?u.message:String(u),...u instanceof Error&&u.stack?{stack:u.stack}:{}}),await F.rm(d,{recursive:true,force:true}).catch(()=>{}),l(),await s.flush().catch(()=>{}),u}}function So(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 ol(e,t){if(!e||typeof e!="object")throw new Error("Remote deploy result must be a JSON object.");let r=e;if(r.schemaVersion!==Z||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 br(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:Z,operation:"deploy-copy",jobId:e.jobId,taskId:n,targetId:e.targetId,items:e.items,...e.deleteRelativeKeys?{deleteRelativeKeys:e.deleteRelativeKeys}:{}}),o=ol(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 il(e){let t=So(e.remoteConfig,e.config),r=ne(`deploy-${Date.now()}-${Ze(Buffer.from(`${t.id}:${e.config.outputDir}`)).slice(0,12)}`,"jobId"),n=dt(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=[],i=Math.max(1,Math.min(16,e.concurrency*2)),c=0;await Promise.all(Array.from({length:Math.min(i,e.files.length)},async()=>{for(;;){let y=c;if(y>=e.files.length)return;c+=1;let b=e.files[y],P=await F.readFile(v.join(e.config.outputDir,b)),$=e.contentTypeByRelativePath?.get(b)||Ps.lookup(b)||"application/octet-stream",x=e.revalidateFiles.has(Ge(b))?fn:bt(b)?e.config.s3.htmlCacheControl:e.config.s3.assetCacheControl,I=Ze(P),R=bt(b)?yr(P):void 0,C=`${n}/files/${Ze(Buffer.from(b))}-${v.basename(b)}`.replace(/\/+/g,"/");await e.client.writeWorkspaceObject({key:C,body:P,contentType:String($),cacheControl:x,metadata:{"wpsuite-sha256":I,...R?{"wpsuite-normalized-sha256":R}:{}}}),o[y]={sourceKey:C,targetRelativeKey:b,sha256:I,...R?{normalizedSha256:R}:{},bytes:P.byteLength,contentType:String($),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 a=[],d=Math.max(1,Math.min(500,e.batchSize));for(let y=0;y<o.length;y+=d)a.push(o.slice(y,y+d));let l=[],u=[],g=[],f=0,m=0,w=0,p=Math.max(1,Math.min(Math.max(1,e.concurrency),a.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:p,completedBatches:0,totalBatches:a.length}),await Promise.all(Array.from({length:p},async()=>{for(;;){let y=w;if(y>=a.length)return;w+=1;let b=Date.now(),P=await br({client:e.client,jobId:r,targetId:t.id,batchIndex:y+1,items:a[y],maxAttempts:e.maxAttempts}),$=0,x=0,I=0;for(let C of P.detail.objects)f+=1,C.status==="copied"?(l.push(C.targetKey),$+=1):C.status==="skipped"?(u.push(C.targetKey),x+=1):(g.push(C.targetKey),I+=1),e.log.debug(`Lambda deploy ${C.status}: ${C.targetKey}`,{phase:"deploy-copy",batch:y+1,sourceKey:C.sourceKey,targetKey:C.targetKey,status:C.status,...C.error?{error:C.error}:{}});m+=1;let R=Date.now()-b;e.log.info(`Lambda deploy batch ${y+1}/${a.length} completed: ${$} copied, ${x} skipped unchanged, ${I} failed.`,{phase:"deploy-copy",batch:y+1,totalBatches:a.length,items:P.detail.objects.length,copied:$,skipped:x,failed:I,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: ${f}/${e.files.length}`,{phase:"deploy-copy",index:f,totalFiles:e.files.length,uploaded:l.length,skipped:u.length,failed:g.length,mode:"lambda-s3-copy",workerCount:p,completedBatches:m,totalBatches:a.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 br({client:e.client,jobId:r,targetId:t.id,batchIndex:a.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:l,skippedKeys:u,failedKeys:g,deletedKeys:h}}async function al(e){let t=So(e.remoteConfig,e.config),r=ne(`deploy-manifest-${Date.now()}-${Ze(Buffer.from(t.id)).slice(0,12)}`,"jobId"),n=e.objects.map(f=>({sourceKey:f.object.key,targetRelativeKey:f.relativePath,sha256:f.object.sha256,bytes:f.object.bytes,contentType:f.contentType||"application/octet-stream",cacheControl:e.revalidateFiles.has(Ge(f.relativePath))?fn:bt(f.relativePath)?e.config.s3.htmlCacheControl:e.config.s3.assetCacheControl})),s=[],o=Math.max(1,Math.min(500,e.batchSize));for(let f=0;f<n.length;f+=o)s.push(n.slice(f,f+o));let i=[],c=[],a=[],d=0,l=0,u=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:u}),await Promise.all(Array.from({length:s.length===0?0:u},async()=>{for(;;){let f=d;if(f>=s.length)return;d+=1;let m=await br({client:e.client,jobId:r,targetId:t.id,batchIndex:f+1,items:s[f],maxAttempts:e.maxAttempts});for(let w of m.detail.objects)l+=1,w.status==="copied"?i.push(w.targetKey):w.status==="skipped"?c.push(w.targetKey):a.push(w.targetKey);e.log.progress(`Lambda deploy: ${l}/${n.length}`,{phase:"deploy-copy",index:l,totalFiles:n.length,uploaded:i.length,skipped:c.length,failed:a.length,mode:"lambda-s3-native",workerCount:u});}})),a.length>0)throw new Error(`Remote deployment failed for ${a.length} object(s).`);let g=[];for(let f=0;f<e.deletedFiles.length;f+=1e3){let m=e.deletedFiles.slice(f,f+1e3);if(m.length===0)continue;let w=await br({client:e.client,jobId:r,targetId:t.id,batchIndex:s.length+Math.floor(f/1e3)+1,items:[],deleteRelativeKeys:m,maxAttempts:e.maxAttempts});g.push(...w.detail.deletedKeys);}return {uploadedKeys:i,skippedKeys:c,failedKeys:a,deletedKeys:g}}async function cl(e,t,r,n,s){let o={...t,jobId:ne(`profile-${Ze(Buffer.from(n)).slice(0,16)}-${Date.now()}`,"jobId"),objects:{},assetMap:{...t.assetMap},generatedAt:new Date().toISOString()},i=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 a=i;if(a>=r.length)return;i+=1;let d=r[a],l=dt(e.config.workspace.prefix,"jobs",o.jobId,"profile",Ze(Buffer.from(d.relativePath)),v.basename(d.relativePath));o.objects[d.relativePath]={...d,object:await e.copyWorkspaceObject(d.object,l)};}})),o}async function Po(e=process.argv.slice(2)){let t=await xe(),r=mr(e),n=yt(t,r),s=v.resolve(n.config.logDir||"logs"),o=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",i=o?v.join(v.resolve(o),"deploy-diff.json"):"",c=new lt(s,"deploy.log.jsonl",n.config.logLevel??"info"),a=n.profile?.extraReplacements??{},d=async()=>{};try{let l=await at(t);if(e.includes("--content-sync")&&l?.schemaVersion!==2)throw new Error("Content-sync deployment requires a valid schema-2 targeted deploy plan.");let g=await Jn(t);if(g)throw new Error(`Deployment blocked: the crawl started at ${g.startedAt} did not complete its final rewrite. Run a successful crawl or resume the rewrite before deploying.`);let f=t.remoteWorkers?.rewrite?.enabled===!0,m=t.remoteWorkers?.deploy?.enabled===!0,w=null,p=null;if(f||m){let T=nr({runtimeDir:o});if(!T)throw new Error("Remote Lambda workers are enabled, but the runtime directory is unknown.");w=await Yt(T);let _=!1;p=new pt(w,{},{onProgress:A=>{c.progress(`Lambda ${A.operation} batch: ${A.completedItems+A.failedItems}/${A.totalItems}`,{remoteWorkerOperation:A.operation,remoteWorkerTaskId:A.taskId,remoteWorkerStatus:A.status,remoteWorkerCompletedItems:A.completedItems,remoteWorkerFailedItems:A.failedItems,remoteWorkerTotalItems:A.totalItems,remoteWorkerUpdatedAt:A.updatedAt});},onProgressError:A=>{_||(_=!0,c.warn("Lambda progress status could not be read; task execution continues.",{error:A.message}));}});}let h=m&&f&&t.remoteWorkers?.render?.enabled===!0&&p&&w?.functions.asset?await or(t):null;if(h&&p&&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 T=n.config,_=T.s3SyncMode??"sdk-upload-delete";if(l?.schemaVersion===2&&l.deletedFiles.length>0&&_!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let A=l?.schemaVersion===2?[...l.uploadGroups.assets,...l.uploadGroups.content,...l.uploadGroups.listings,...l.uploadGroups.resources,...l.uploadGroups.sitemapLeaves,...l.uploadGroups.sitemapIndexes]:l?.changedFiles??[];if(l?.schemaVersion===2){let le=[...new Set(A)],Fe=[...new Set(l.changedFiles)];if(le.length!==Fe.length||Fe.some(Ye=>!le.includes(Ye)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let be=!!l&&!l.fullSyncRequired?[...new Set(A.map(Ge))]:Object.keys(h.objects).sort(),q=be.filter(le=>!h.objects[le]);if(q.length>0)throw new Error(`Remote export manifest is missing ${q.length} planned object(s): ${q.slice(0,10).join(", ")}`);let Se=h,B=be.map(le=>h.objects[le]);n.name&&(t.targetOrigin!==T.targetOrigin||Object.keys(a).length>0)&&(Se=await cl(p,h,B,n.name||"default",t.concurrency),await rr(T,Se,Se.assetMap,{client:p,jobId:Se.jobId,concurrency:t.concurrency,batchSize:25,maxAttempts:2}),B=be.map(le=>Se.objects[le])),n.name&&c.info(`Using deployment profile: ${n.name}`),c.info(`Starting S3-native Lambda deploy - s3://${T.s3.bucket}/${T.s3.prefix||""} (${T.s3.region}).`),c.mark("deploy");let ie=await al({client:p,remoteConfig:w,config:T,objects:B,deletedFiles:_==="sdk-upload-delete"?l?.deletedFiles??[]:[],revalidateFiles:new Set((l?.revalidateFiles??[]).map(Ge)),concurrency:t.concurrency,batchSize:50,maxAttempts:2,log:c});i&&(await F.mkdir(v.dirname(i),{recursive:!0}),await F.writeFile(i,JSON.stringify({generatedAt:new Date().toISOString(),mode:"lambda-s3-native",summary:{totalFiles:B.length,eligibleFiles:B.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")),l?.schemaVersion!==2&&await Je(t),c.endMark("deploy"),await c.flush();return}let y=await sl(t,n.config,n.name,a,c,l?.schemaVersion===2?l.changedFiles:void 0,f&&p?{client:p,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:T,totalFiles:_,file:A})=>{(T===1||T===_||T%25===0)&&c.progress(`Staging deployment-profile rewrite: ${T}/${_}`,{phase:"rewrite-stage",index:T,totalFiles:_,file:A,execution:"lambda"});}}:void 0);d=y.cleanup;let b={...n.config,outputDir:y.outputDir},P=b.s3SyncMode??"sdk-upload-delete";if(l?.schemaVersion===2&&l.deletedFiles.length>0&&P!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let $=l?.schemaVersion!==2&&y.outputDir!==t.outputDir&&(t.targetOrigin!==n.config.targetOrigin||Object.keys(a).length>0),x=!!l&&!l.fullSyncRequired&&!$;if(l?.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}`),l&&l.fullSyncRequired?c.info("Deploy plan requests a full sync; scanning the whole output tree."):l&&$?c.info("Ignoring incremental deploy plan because deployment-profile rewrites require a full sync."):x&&c.info(`Using incremental deploy plan: ${l.changedFiles.length} changed file(s), ${l.deletedFiles.length} deleted file(s).`),c.mark("deploy");let I=new S3Client({region:b.s3.region}),R=y.outputDir===t.outputDir?await Nc(t,o,c):null,C=l?.schemaVersion===2?[...l.uploadGroups.assets,...l.uploadGroups.content,...l.uploadGroups.listings,...l.uploadGroups.resources,...l.uploadGroups.sitemapLeaves,...l.uploadGroups.sitemapIndexes]:l?.changedFiles??[];if(l?.schemaVersion===2){let T=[...new Set(C)],_=[...new Set(l.changedFiles)];if(T.length!==_.length||_.some(A=>!T.includes(A)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let O=x?(await Promise.all(C.map(async T=>{try{return (await F.stat(v.join(b.outputDir,T))).isFile()?T:null}catch{if(l?.schemaVersion===2)throw new Error(`Content-sync planned upload is missing: ${T}`);return null}}))).filter(T=>T!==null):await li(["**/*"],{cwd:b.outputDir,onlyFiles:!0,dot:!0}),V=[],U=[],S=O.filter(T=>{let _=R?.byRelativePath.get(Ge(T));if(!_)return !0;let A=fr(b.s3.prefix,T);return V.push(T),U.push(A),c.reject(_.kind,_.sourceUrl||T,"rewrite not completed; deploy upload blocked","deploy-guard",{file:T,key:A,outputPath:_.outputPath,manifestPath:R?.manifestPath}),!1}),W=new Set((l?.revalidateFiles||[]).map(Ge)),ce=new Set((x?S:O).map(T=>fr(b.s3.prefix,T)));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"}),V.length>0&&c.warn(`Deploy rewrite guard blocked ${V.length} file(s) from upload because final crawl rewrite is incomplete.`,{rejected:V.length,manifestPath:R?.manifestPath,source:"deploy-guard"}),x?c.info(`Using ${S.length} eligible planned changed file(s) from deploy plan for upload${V.length>0?` (${V.length} blocked by deploy rewrite guard)`:""}.`):c.info(`Found ${O.length} file(s) in output directory "${b.outputDir}"${V.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:O.length,rejected:V.length,deploymentProfile:n.name||"",s3Bucket:b.s3.bucket,s3Prefix:b.s3.prefix||"",targetedPlan:x,deployGuardEnabled:y.outputDir===t.outputDir});let E=0,D=0,j=0,X=[],Y=[],ee=[],z=[],de=[],ye=new Map,k=new Map,L=new Map,M=new Map,G=new Map;if(m){if(!p||!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 T=await il({client:p,remoteConfig:w,config:b,files:S,deletedFiles:P==="sdk-upload-delete"?l?.deletedFiles??[]:[],revalidateFiles:W,contentTypeByRelativePath:R?.contentTypeByRelativePath,concurrency:b.remoteWorkers?.deploy?.concurrency??b.assetDownloadConcurrency,batchSize:b.remoteWorkers?.deploy?.batchSize??50,maxAttempts:b.remoteWorkers?.deploy?.maxAttempts??2,log:c});X.push(...T.uploadedKeys),Y.push(...T.skippedKeys),ee.push(...T.failedKeys),z=T.deletedKeys,E=X.length,D=Y.length,j=ee.length,c.info(`Lambda deploy complete: ${E} copied, ${D} skipped unchanged, ${z.length} deleted, ${j} failed.`),i&&(await F.mkdir(v.dirname(i),{recursive:!0}),await F.writeFile(i,JSON.stringify({generatedAt:new Date().toISOString(),mode:"lambda-s3-copy",summary:{totalFiles:O.length,eligibleFiles:S.length,uploaded:E,skipped:D,rejected:U.length,failed:j,deleted:z.length,protected:0,...n.name?{profile:n.name}:{}},uploadedKeys:X,skippedKeys:Y,rejectedKeys:U,failedKeys:ee,deletedKeys:z,protectedRemoteOnlyKeys:[]},null,2),"utf8")),l?.schemaVersion!==2&&await Je(t),c.endMark("deploy"),await c.flush();return}x||(c.info("Listing existing S3 objects to detect unchanged files..."),ye=await Jc(I,b.s3.bucket,b.s3.prefix,c),c.info(`Found ${ye.size} existing key(s) in S3.`));let te=async T=>ye.has(T)?ye.get(T)??null:x?Qc(I,b.s3.bucket,T,k):null;for(let T of S){let _=v.join(b.outputDir,T),A=fr(b.s3.prefix,T),re=R?.contentTypeByRelativePath.get(T),be=re&&re!=="application/octet-stream"?re:Ps.lookup(T)||re||"application/octet-stream",q=W.has(Ge(T)),Se=q?fn:bt(T)?b.s3.htmlCacheControl:b.s3.assetCacheControl,B=E+D+j+1;S.length<=200||B<=5||B%10===0||B===S.length?(c.progress(`Uploading [${B}/${S.length}] ${A} (${be})`,{file:T,key:A,index:B,totalFiles:S.length,contentType:be,mode:P}),c.info(`Uploading [${B}/${S.length}] ${A} (${be})`)):c.debug(`Uploading [${B}/${S.length}] ${A} (${be})`);try{let Pe=await F.readFile(_),ie=await te(A),le=null,Fe=null;if(ie){let Ye=!q||ho(await Gc(I,b.s3.bucket,A,ie))===ho(Se),To=Pe.byteLength;if(ie.size===To){let Ut=ie.etag.includes("-");if(ie.etag!==""&&!Ut){let Eo=Yc(Pe);if(ie.etag===Eo&&Ye){D++,S.length<=200||D<=5||D%25===0?(c.progress(`Skipped unchanged [${B}/${S.length}] ${A}`,{file:T,key:A,index:B,totalFiles:S.length,uploaded:E,skipped:D,failed:j,mode:P,reason:"etag-md5-match"}),c.info(`Skipped unchanged [${B}/${S.length}] ${A}`)):c.debug(`Skipped unchanged [${B}/${S.length}] ${A}`),Y.push(A);continue}}le=Ze(Pe);let Rn=await Xc(I,b.s3.bucket,A,L);if(Rn&&Rn===le&&Ye){D++,S.length<=200||D<=5||D%25===0?(c.progress(`Skipped unchanged [${B}/${S.length}] ${A}`,{file:T,key:A,index:B,totalFiles:S.length,uploaded:E,skipped:D,failed:j,mode:P,reason:"metadata-sha256-match"}),c.info(`Skipped unchanged [${B}/${S.length}] ${A}`)):c.debug(`Skipped unchanged [${B}/${S.length}] ${A}`),Y.push(A);continue}}if(bt(T)){Fe=yr(Pe);let Ut=await el(I,b.s3.bucket,A,M,G);if(Ut&&Ut===Fe&&Ye){D++,S.length<=200||D<=5||D%25===0?(c.progress(`Skipped semantically unchanged [${B}/${S.length}] ${A}`,{file:T,key:A,index:B,totalFiles:S.length,uploaded:E,skipped:D,failed:j,mode:P,reason:"normalized-html-match"}),c.info(`Skipped semantically unchanged [${B}/${S.length}] ${A}`)):c.debug(`Skipped semantically unchanged [${B}/${S.length}] ${A}`),Y.push(A);continue}}}le=le??Ze(Pe),Fe=Fe??(bt(T)?yr(Pe):null),await he(`S3 upload ${A}`,9e5,({abortController:Ye})=>new Upload({client:I,abortController:Ye,params:{Bucket:b.s3.bucket,Key:A,Body:Pe,ContentType:be,CacheControl:Se,Metadata:{[yo]:le,...Fe?{[bo]:Fe}:{}}}}).done()),E++,X.push(A),(E%25===0||E===S.length)&&(c.progress(`Uploaded ${E}/${S.length} file(s)\u2026`,{uploaded:E,skipped:D,failed:j,totalFiles:S.length,mode:P}),c.info(`Uploaded ${E}/${S.length} file(s)\u2026`));}catch(Pe){j++,ee.push(A),c.error(`Failed to upload ${A}: ${Pe.message}`,{key:A,error:String(Pe)});}}if(c.info(`Upload complete: ${E} uploaded, ${D} skipped unchanged, ${U.length} rejected by guard, ${j} failed.`),ee.length>0||U.length>0)throw new Error(`Deployment stopped before deletion because ${ee.length} upload(s) failed and ${U.length} upload(s) were rejected.`);if(P==="sdk-upload-delete"){let T=new Set((l?.deletedFiles??[]).map(A=>fr(b.s3.prefix,A))),_=[...T].filter(A=>!ce.has(A));if(!x&&(de=[...ye.keys()].filter(re=>!ce.has(re)).filter(re=>!T.has(re)),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 re of de)c.debug(` protected remote-only object: ${re}`);}z=await Zc(I,b.s3.bucket,x?_:_.filter(A=>ye.has(A)),c);}else c.info("Sync mode is sdk-upload-only \u2014 skipping stale object deletion.");i&&(await F.mkdir(v.dirname(i),{recursive:!0}),await F.writeFile(i,JSON.stringify({generatedAt:new Date().toISOString(),mode:P,summary:{totalFiles:O.length,eligibleFiles:S.length,uploaded:X.length,skipped:Y.length,rejected:U.length,failed:ee.length,deleted:z.length,protected:de.length,...n.name?{profile:n.name}:{}},uploadedKeys:X,skippedKeys:Y,rejectedKeys:U,failedKeys:ee,deletedKeys:z,protectedRemoteOnlyKeys:de},null,2),"utf8")),l?.schemaVersion!==2&&await Je(t),c.endMark("deploy"),await c.flush();}finally{await d().catch(()=>{});}}function ul(e){let t=e;return t.endsWith("/")?t+="index.html":v.posix.basename(t).includes(".")||(t+="/index.html"),t}function dl(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 Ro(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}`,i=ul(s.pathname);r.add(o),r.add(`${i}${s.search}`);let c=dl(i,t);c&&r.add(`${c}${s.search}`);}return [...r].sort()}function fl(){let e=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();return e?v.join(v.resolve(e),"content-sync-invalidation.json"):""}async function wn(e,t){if(!e)return;await F.mkdir(v.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 hl(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(i=>i.paths);return o.length!==n.length||o.some((i,c)=>i!==n[c])?null:s}catch(s){if(s.code==="ENOENT")return null;throw s}}function wl(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 Co(e=process.argv.slice(2)){let t=await xe(),r=mr(e),n=yt(t,r),s=n.config,o=new lt(s.logDir||"logs","invalidate.log.jsonl",s.logLevel??"info"),i=e.includes("--content-sync"),c=await at(t);if(i&&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 a=c?.schemaVersion===2?c.invalidationPaths:s.cloudFront.invalidationPaths.length>0?s.cloudFront.invalidationPaths:["/*"],d=c?.schemaVersion===2?Ro(a,s.s3.prefix||""):a;if(c?.schemaVersion===2&&d.some(m=>m==="/*"))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 l=s.cloudFront.distributionId,u=new CloudFrontClient({region:"us-east-1",maxAttempts:5});if(o.mark("invalidate"),c?.schemaVersion!==2){let m=`smartcloud-static-publisher-${Date.now()}`;await he(`CloudFront invalidation for distribution ${l}`,3e5,({httpOptions:w})=>u.send(new CreateInvalidationCommand({DistributionId:l,InvalidationBatch:{CallerReference:m,Paths:{Quantity:d.length,Items:d}}}),w)),o.endMark("invalidate"),await o.flush();return}let g=fl(),f=await hl(g,l,c.impactHash,d);if(!f){let m=createHash("sha256").update(JSON.stringify(d)).digest("hex");f={schemaVersion:1,distributionId:l,impactHash:c.impactHash,updatedAt:new Date().toISOString(),batches:wl(d).map((w,p)=>({index:p,callerReference:`wpsuite-content-sync-${c.impactHash.slice(0,24)}-${m.slice(0,24)}-${p}`,paths:w,invalidationId:null,status:"pending"}))},await wn(g,f);}for(let m of f.batches){if(!m.invalidationId){let w=await he(`CloudFront targeted invalidation batch ${m.index+1}`,3e5,({httpOptions:h})=>u.send(new CreateInvalidationCommand({DistributionId:l,InvalidationBatch:{CallerReference:m.callerReference,Paths:{Quantity:m.paths.length,Items:m.paths}}}),h)),p=String(w.Invalidation?.Id||"").trim();if(!p)throw new Error("CloudFront did not return an invalidation ID.");m.invalidationId=p,m.status="created",await wn(g,f);}await waitUntilInvalidationCompleted({client:u,maxWaitTime:900,minDelay:2,maxDelay:15},{DistributionId:l,Id:m.invalidationId}),m.status="completed",await wn(g,f),o.info(`CloudFront invalidation batch ${m.index+1}/${f.batches.length} completed.`,{invalidationId:m.invalidationId,pathCount:m.paths.length});}o.endMark("invalidate"),await o.flush();}function Rr(e){let t=new URL(e);return t.hash="",t.href}function ko(e,t,r){let n=new Set([...t].map(a=>Rr(a))),s=new Map,o=new Map,i=a=>{s.set(Rr(a),a);},c=a=>{if(!a)return;let d=Rr(a);s.has(d)||o.set(d,a);};for(let a of e.deleteUrls)i(a);for(let a of e.resourceDeleteUrls)i(a);for(let a of e.archiveFamilies)if(a.foundPosts===0)for(let d of a.pageUrls)n.has(Rr(d))||i(d);for(let a of e.directRenderUrls)c(a);for(let a of e.resourceRenderUrls)c(a);for(let a of e.archiveFamilies)c(a.pageUrls[0]),c(a.pageUrls.at(-1));for(let a of r)c(a);return {expectedAvailable:[...o.values()],expectedMissing:[...s.values()]}}var Io=["claimed","impact-planned","rendered","deployed","invalidated","verified","acknowledged"];function Sn(){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 v.resolve(e)}function St(e,t){return Io.indexOf(e)>=Io.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 F.mkdir(e,{recursive:true}),await F.appendFile(v.join(e,"audit-events.jsonl"),`${JSON.stringify(o)}
|
|
19
|
-
`,"utf8");}function bl(e){return Pt({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 Sl(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 Pn(e,t){if(e.subscriptionType!=="PROFESSIONAL"&&e.subscriptionType!=="AGENCY")throw new Error("Content-sync requires an active Professional or Agency subscription.");let r=bl(t),n=yt(e,t.deploymentProfile),s=n.name||"",o=kn(e.sourceOrigin,String(t.id||""),r),i=xn({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=In({siteId:e.wpsuite?.siteSettings?.siteId,accountId:e.wpsuite?.siteSettings?.accountId,sourceOrigin:e.sourceOrigin,ruleId:String(t.id||""),targetFingerprint:i}),a=`content-sync:${String(t.id||"")}:${De({scopeFingerprint:o,targetFingerprint:i}).slice(0,32)}`,d=new qt({sourceOrigin:e.sourceOrigin,runtimeToken:String(e.wpsuite?.runtimeToken||""),ignoreHttpsErrors:e.ignoreHttpsErrors}),l=await d.releaseFingerprint(r.includeSubsites),u=Cn({wordpressFingerprint:l,targetFingerprint:i,sourceOrigin:e.sourceOrigin,sitemapPaths:Ct(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:i,consumerId:c,coalesceKey:a,client:d,releaseFingerprint:u}}async function Pl(e){let t=Sn(),r=Wt(t),n=await xe(),s=(n.scheduler?.rules??[]).filter(d=>d.command==="content-sync"&&d.enabled!==false);if(s.length===0)return 0;let o=await Ir(r),i=await Nt(r),c=await xr(r),a=0;for(let d of s){let l=await Pn(n,d),u=c.entries[l.coalesceKey];if(!u||u.releaseJobId!==e||u.scopeFingerprint!==l.scopeFingerprint||u.targetFingerprint!==l.targetFingerprint||u.releaseFingerprint!==l.releaseFingerprint)throw new Error(`The normal release has no matching pre-crawl content-sync cutoff for rule ${String(d.id||"")}.`);let g=De({releaseJobId:e,releaseFingerprint:l.releaseFingerprint,scopeFingerprint:l.scopeFingerprint,targetFingerprint:l.targetFingerprint,sequence:u.sequence}),f=await l.client.establishBaseline({consumerId:l.consumerId,scopeFingerprint:l.scopeFingerprint,baselineId:g,postTypes:l.scope.postTypes,includeSubsites:l.scope.includeSubsites,sequence:u.sequence}),m={schemaVersion:1,ruleId:String(d.id||""),consumerId:l.consumerId,coalesceKey:l.coalesceKey,scopeFingerprint:l.scopeFingerprint,targetFingerprint:l.targetFingerprint,releaseFingerprint:l.releaseFingerprint,baselineId:g,deploymentProfile:l.deploymentProfile,committedSequence:f.committedSequence,releaseJobId:e,verifiedAt:new Date().toISOString()};o.entries[l.coalesceKey]=m;let w=i.rules[l.coalesceKey];i.rules[l.coalesceKey]={ruleId:String(d.id||""),consumerId:l.consumerId,coalesceKey:l.coalesceKey,committedSequence:f.committedSequence,observedHeadSequence:f.headSequence,lag:Math.max(0,f.headSequence-f.committedSequence),lastSuccessfulJobId:w?.lastSuccessfulJobId||e,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:f.headSequence>f.committedSequence,retryAttempt:0,nextRetryAt:null,lastError:null,baselineStatus:"ready",baselineReason:null};let p=await Un(r,l.coalesceKey);await Ke(t,"content-sync-baseline-established",{id:e},"A normal release established a verified content-sync baseline.",{ruleId:d.id,consumerId:l.consumerId,committedSequence:f.committedSequence,supersededCurrent:p}),a++;}return await On(r,o),await _t(r,i),await F.rm(r.releaseCutoff,{force:true}),a}async function Rl(e){let t=Sn(),r=Wt(t),n=await xe(),s=(n.scheduler?.rules??[]).filter(c=>c.command==="content-sync"&&c.enabled!==false);if(s.length===0)return 0;let o=await xr(r),i=0;for(let c of s){let a=await Pn(n,c),d=Object.values(o.entries).find(g=>g.releaseJobId===e&&g.ruleId===String(c.id||""));if(d){if(d.coalesceKey===a.coalesceKey&&d.scopeFingerprint===a.scopeFingerprint&&d.targetFingerprint===a.targetFingerprint&&d.releaseFingerprint===a.releaseFingerprint){i++;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 l=await a.client.unboundHead(a.scope.postTypes,a.scope.includeSubsites),u={schemaVersion:1,releaseJobId:e,ruleId:String(c.id||""),coalesceKey:a.coalesceKey,scopeFingerprint:a.scopeFingerprint,targetFingerprint:a.targetFingerprint,releaseFingerprint:a.releaseFingerprint,sequence:l.headSequence,capturedAt:new Date().toISOString()};o.entries[a.coalesceKey]=u,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:a.consumerId,sequence:l.headSequence}),i++;}return await $n(r,o),i}async function vl(e,t){let r=JSON.parse(await F.readFile(v.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 bn(e,t,r=0){let n=e.protocol==="https:"?En:Lo,s=e.protocol==="https:"?new En.Agent({rejectUnauthorized:!t}):void 0;return new Promise((o,i)=>{let c=n.get(e,{agent:s,timeout:3e4,headers:{"user-agent":"WP-Suite-Content-Sync/1"}},a=>{let d=a.statusCode??500,l=a.headers.location;if(d>=300&&d<400&&l&&r<5){a.resume(),bn(new URL(l,e),t,r+1).then(o,i);return}let u=[],g=0;a.on("data",f=>{g+=f.byteLength,g<=1048576&&u.push(f);}),a.on("error",i),a.on("end",()=>o({status:d,body:Buffer.concat(u).toString("utf8"),contentType:String(a.headers["content-type"]||"")}));});c.on("timeout",()=>c.destroy(new Error(`Verification timed out: ${e}`))),c.on("error",i);})}async function Cl(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")),i=new Set;for(let[l,u]of Object.entries(o.pages??{}))i.add(l),typeof u.url=="string"&&i.add(u.url);let c=await at(e);if(c?.schemaVersion!==2||c.impactHash!==r.impactHash)throw new Error("Content-sync verification requires the matching targeted deploy plan.");let a=ko(r,i,c.resolvedSitemapUrls),d=new Set(r.resourceRenderUrls.map(l=>{let u=new URL(l);return u.hash="",u.href}));for(let l of a.expectedAvailable){let u=new URL(l),g=new URL(`${u.pathname}${u.search}`,s),f=await bn(g,e.ignoreHttpsErrors);if(f.status<200||f.status>=400)throw new Error(`Content-sync verification failed (${f.status}): ${g}`);let m=new URL(l);if(m.hash="",d.has(m.href)){if(!f.contentType.toLowerCase().startsWith("application/json"))throw new Error(`Resource verification expected application/json: ${g}`);try{JSON.parse(f.body);}catch{throw new Error(`Resource verification received invalid JSON: ${g}`)}}}for(let l of a.expectedMissing){let u=new URL(l),g=new URL(`${u.pathname}${u.search}`,s),f=await bn(g,e.ignoreHttpsErrors);if(f.status>=200&&f.status<300)throw new Error(`Deleted content is still publicly available: ${g}`)}}async function kl(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 xl(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 st(e,t,r,n,s={},o=0,i=async()=>{}){t.phase=r,await Mn(e,t),await Dn(e,{schemaVersion:1,jobId:t.jobId,phase:r,completedItems:n,totalItems:n,attempt:o,updatedAt:new Date().toISOString(),details:s}),await Ke(v.dirname(e.state),"content-sync-checkpoint",{id:t.jobId},`Content-sync checkpoint completed: ${r}.`,{phase:r,completedItems:n,totalItems:n,attempt:o,...s}),await i(r,t);}async function Il(e,t={}){let r={loadConfig:xe,resolveRuleContext:Pn,runCrawl:mo,runDeploy:Po,runInvalidate:Co,verifyDeployment:Cl,promoteCandidateManifest:kl,clearDeployPlan:Je,afterCheckpoint:async()=>{},...t},n=Sn(),s=Wt(n),o=await vl(n,e),i=await r.loadConfig(),c=Sl(i,String(o.ruleId)),a=await r.resolveRuleContext(i,c);if(o.coalesceKey&&o.coalesceKey!==a.coalesceKey)throw new Error("Queued content-sync demand no longer matches the active rule scope.");let l=(await Ir(s)).entries[a.coalesceKey];if(!l||l.scopeFingerprint!==a.scopeFingerprint||l.targetFingerprint!==a.targetFingerprint||l.releaseFingerprint!==a.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:l?"mismatch":"missing"}),new Error("Content-sync baseline is missing or stale. Run a successful normal publish first.");let u=await Ar(s),g=await a.client.head({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:l.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites});if(u&&u.jobId!==o.id)throw new Error("Another immutable content-sync range is still active.");if(u||(u={schemaVersion:1,jobId:o.id,ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,scopeFingerprint:a.scopeFingerprint,targetFingerprint:a.targetFingerprint,baselineId:l.baselineId,fromSequence:g.committedSequence,toSequence:g.headSequence,phase:"claimed",impactHash:null,startedAt:new Date().toISOString(),updatedAt:new Date().toISOString()},await st(s,u,"claimed",0,{fromSequence:u.fromSequence,toSequence:u.toSequence},0,r.afterCheckpoint),await Ke(n,"content-sync-range-claimed",o,"Content-sync claimed a closed journal range.",{fromSequence:u.fromSequence,toSequence:u.toSequence})),u.fromSequence===u.toSequence){let h=await Nt(s);h.rules[a.coalesceKey]={ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,committedSequence:u.toSequence,observedHeadSequence:g.headSequence,lag:Math.max(0,g.headSequence-u.toSequence),lastSuccessfulJobId:o.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:false,retryAttempt:0,nextRetryAt:null,lastError:null},await _t(s,h),await Tr(s);return}let f=await jn(s);if(!St(u.phase,"impact-planned")){let h=await a.client.events({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:l.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites,afterSequence:u.fromSequence,throughSequence:u.toSequence}),y=kr({events:h,scope:a.scope,sourceOrigin:i.sourceOrigin}),b=await a.client.resolveImpactFamilies(y,a.scope.includeSubsites);f=Tn({jobId:o.id,ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,scopeFingerprint:a.scopeFingerprint,targetFingerprint:a.targetFingerprint,baselineId:l.baselineId,sourceOrigin:i.sourceOrigin,scope:a.scope,fromSequence:u.fromSequence,toSequence:u.toSequence,events:h,resolvedArchiveFamilies:b,sitemapUrls:Ct(i.sitemapPaths)}),await Fn(s,f),u.impactHash=f.impactHash,await st(s,u,"impact-planned",h.length,{eventCount:h.length,foldedCount:f.foldedCount,impactHash:f.impactHash},0,r.afterCheckpoint),await Ke(n,"content-sync-impact-planned",o,"Content-sync persisted an immutable impact plan.",{impactHash:f.impactHash,eventCount:h.length});}if(!f||f.jobId!==o.id||u.impactHash!==f.impactHash)throw new Error("The immutable content-sync impact plan is missing or mismatched.");if(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH=s.candidateManifest,!St(u.phase,"rendered")){let h=await xl(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:f.impactHash}),await r.runCrawl(["--content-sync-plan",s.impact]),await st(s,u,"rendered",f.directRenderUrls.length+f.archiveFamilyUrls.length+f.resourceRenderUrls.length,{impactHash:f.impactHash},o.attempt??0,r.afterCheckpoint);}let m=a.deploymentProfile?["--profile",a.deploymentProfile]:[];if(St(u.phase,"deployed")||(await r.runDeploy([...m,"--content-sync"]),await st(s,u,"deployed",1,{impactHash:f.impactHash},0,r.afterCheckpoint),await Ke(n,"content-sync-deployed",o,"Content-sync targeted deployment completed.",{impactHash:f.impactHash})),St(u.phase,"invalidated")?St(u.phase,"verified")||await r.runInvalidate([...m,"--content-sync"]):(await r.runInvalidate([...m,"--content-sync"]),await st(s,u,"invalidated",1,{impactHash:f.impactHash},0,r.afterCheckpoint)),St(u.phase,"verified")||(await r.verifyDeployment(i,a,f,s.candidateManifest),await r.promoteCandidateManifest(s),await st(s,u,"verified",1,{impactHash:f.impactHash},0,r.afterCheckpoint)),g=await a.client.head({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:l.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites}),g.committedSequence===u.fromSequence)await a.client.acknowledge({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:l.baselineId,expectedSequence:u.fromSequence,sequence:u.toSequence,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites});else if(g.committedSequence!==u.toSequence)throw new Error("Content-sync acknowledgement cursor is in an unexpected state.");await st(s,u,"acknowledged",1,{sequence:u.toSequence},0,r.afterCheckpoint),await Ke(n,"content-sync-cursor-acknowledged",o,"Content-sync committed its journal cursor after verification.",{sequence:u.toSequence});let w=await a.client.head({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:l.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites}),p=await Nt(s);p.rules[a.coalesceKey]={ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,committedSequence:u.toSequence,observedHeadSequence:w.headSequence,lag:Math.max(0,w.headSequence-u.toSequence),lastSuccessfulJobId:o.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:w.headSequence>u.toSequence,retryAttempt:0,nextRetryAt:null,lastError:null},await _t(s,p),await r.clearDeployPlan(i),await F.rm(v.join(n,"content-sync-invalidation.json"),{force:true}),await F.rm(s.candidateManifest,{force:true}),await Tr(s);}async function Ao(e=process.argv.slice(2)){let r=e.indexOf("--capture-baseline-cutoffs");if(r>=0){let i=String(e[r+1]||"").trim();if(!i)throw new Error("--capture-baseline-cutoffs requires a release job ID.");await Rl(i);return}let n=e.indexOf("--establish-baselines");if(n>=0){let i=String(e[n+1]||"").trim();if(!i)throw new Error("--establish-baselines requires a release job ID.");await Pl(i);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 Il(o);}Ao().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:i,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 Gs(e,t,r,n,s,o){let i=pr(e,s);if(await Sa(e,t,r,n,s,o),!n.enabled)return {action:"render",changeToken:n.trackManifest?await ar(e,t,n,s,o):null};let c=n.manifest.pages[s];if(!c)return {action:"render",changeToken:await ar(e,t,n,s,o)};let a=await ar(e,t,n,s,o);if(a?.supported&&a.token)return c.changeToken===a.token?i&&!await va(e,t,s,o)?{action:"render",changeToken:a}:{action:"reuse",changeToken:a}:{action:"render",changeToken:a};let d=Xe(r.sitemapLastmodByPage[s]);return d&&c.sitemapLastmod&&c.sitemapLastmod===d?{action:"reuse",changeToken:a}:{action:"render",changeToken:a}}async function va(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 Ca(e,t,r,n,s){let o=js(r.previousManifest,void 0,n,"previous-manifest"),i=0;for(let[g,f]of Object.entries(r.manifest.pages))if(!r.seenPages.has(g)){rn(s,e,f.outputPath);try{await F.unlink(f.outputPath);}catch(m){(m.code||"")!=="ENOENT"&&n.warn(`Failed to remove stale incremental page output for ${g}`,{url:g,outputPath:f.outputPath,error:String(m)});}delete r.manifest.pages[g],i++;}let c=js(r.manifest,r.runId,n,"active-manifest"),a=new Set,d=new Set;for(let g of c){for(let w of $t(e,g))a.add(w);let f=r.manifest.assets[g]?.outputPath;f&&d.add(f);let m=ft(e,t.assetMap,g);m&&d.add(m);}for(let g of Object.keys(r.manifest.assets)){if(c.has(g)){r.manifest.assets[g].unreferencedRuns=0;continue}let f=r.manifest.assets[g];if(fs(f.unreferencedRuns)){f.unreferencedRuns+=1,n.warn(`Deferred deletion of newly unreferenced incremental asset ${g}.`,{url:g,unreferencedRuns:f.unreferencedRuns,phase:"incremental-cleanup",source:"asset-deletion-grace"});continue}delete r.manifest.assets[g];}let l=0,u=0;for(let g of o){if(r.manifest.assets[g])continue;let f=new Set,m=r.previousManifest.assets[g]?.outputPath;m&&f.add(m);let w=ft(e,t.assetMap,g);if(!c.has(g)&&w)f.add(w);else if(!c.has(g)){let p=Na(e,g,n,"incremental-cleanup");p&&(f.add(p.originalFilePath),p.hashedFilePath&&f.add(p.hashedFilePath));}for(let p of f)if(!d.has(p)){rn(s,e,p);try{await F.unlink(p),l++;}catch(h){(h.code||"")!=="ENOENT"&&n.warn(`Failed to remove stale incremental asset output for ${g}`,{url:g,outputPath:p,error:String(h)});}}for(let p of $t(e,g))a.has(p)||Object.prototype.hasOwnProperty.call(t.assetMap,p)&&(delete t.assetMap[p],u++);}(i>0||l>0||u>0)&&n.info(`Incremental cleanup removed ${i} stale pages, ${l} stale assets, ${u} stale asset mappings`,{removedPages:i,removedAssets:l,removedAssetMappings:u,phase:"incremental-cleanup"});}async function ka(e,t,r,n){let s=new Set([...Object.values(t.manifest.pages),...Object.values(t.manifest.assets)].map(a=>String(a.outputPath||"").trim()).filter(Boolean).map(a=>v.resolve(a))),o=Object.values(e.postCrawlCopyMap||{}).map(a=>cr(String(a||""))).filter(a=>!!a).map(a=>v.resolve(e.outputDir,a)),c=(await mi(["**/*.html","**/*.htm"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(a=>gs(v.join(e.outputDir,a),e.outputDir,s,o));for(let a of c){let d=v.join(e.outputDir,a);rn(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=i=>{let c=i.trim();if(c){if(r){let a=Ce(c,r,"asset",n??"manifest");if(!a)return;s.add(a.toString());return}s.add(c);}};for(let i of Object.values(e.pages))for(let c of i.discoveredAssets||[])o(c);for(let[i,c]of Object.entries(e.assets||{}))t&&c.lastSeenRunId!==t||o(i);return s}function Fs(e,t,r){for(let n of t)for(let s of $t(e,n))if(r.has(s))return true;return false}function xa(e,t,r,n,s){let o=new Set(r.rewriteTargets);for(let c of r.changedFiles)o.add(v.resolve(e.outputDir,c));for(let c of Object.values(t.manifest.pages))c.rewriteComplete||o.add(v.resolve(c.outputPath));for(let c of Object.values(t.manifest.assets))c.isText&&!c.rewriteComplete&&o.add(v.resolve(c.outputPath));let i=ba(n,s);if(i.size===0)return [...o];for(let c of Object.values(t.manifest.pages))Fs(e,c.discoveredAssets,i)&&o.add(v.resolve(c.outputPath));for(let[c,a]of Object.entries(t.manifest.assets)){a.isText&&Fs(e,a.discoveredAssets,i)&&o.add(v.resolve(a.outputPath));for(let d of $t(e,c))if(i.has(d)){ue(r,e,a.outputPath),a.isText&&o.add(v.resolve(a.outputPath));break}}return [...o]}function Ia(e,t){let r=t?new Set(Array.from(t,s=>v.resolve(s))):null,n=s=>r===null||r.has(v.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 Ta(e,t,r){let n=new Set,s=o=>{if(!o)return;let i=he(e.outputDir,o);i&&n.add(i);};for(let o of t.donePages)s(Me(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(ft(e,t.assetMap,o));for(let o of Object.values(r.manifest.assets))o.lastSeenRunId===r.runId&&s(o.outputPath);return n}async function Aa(e,t,r,n,s,o){let i=[...n.deletedFiles].sort(),c=Ta(e,t,r),a=0;for(let u of [...c].sort())if(!o?.objects[u]){try{if((await F.stat(v.join(e.outputDir,u))).isFile())continue}catch{}a++,s.warn(`Active output is missing after crawl: ${u}`,{phase:"deploy-plan",file:u,outputPath:v.join(e.outputDir,u)});}if(a>0&&s.warn(`Detected ${a} active output file(s) missing before deploy plan save.`,{phase:"deploy-plan",missingActiveOutputs:a}),i.length===0)return [];let d=[],l=0;for(let u of i){if(!c.has(u)){d.push(u);continue}l++;let g=v.join(e.outputDir,u),f;try{f=(await F.stat(g)).isFile();}catch{f=false;}s.warn(`Deploy plan protected active output from deletion: ${u}`,{phase:"deploy-plan",file:u,outputPath:g,exists:f});}return l>0&&s.info(`Deploy plan skipped ${l} active output deletion(s).`,{phase:"deploy-plan",protectedDeletes:l,deletedFiles:d.length}),d}function $t(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 i of o)try{r.add(new URL(i).origin+n.pathname+n.search);}catch{}r.add(n.pathname+n.search),r.add(n.pathname);}catch{}return [...r]}function Ea(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 i of o)try{r.add(new URL(i).origin+n.pathname+n.search);}catch{}r.add(n.pathname+n.search);}catch{}return [...r]}function Zs(e,t){let r=e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/"?e.targetOrigin:"https://relative.invalid/";try{let n=new URL(t,r);return Tt(e.outputDir,n.pathname)}catch{return null}}function Ys(e,t,r){for(let n of Ea(e,r)){let s=t[n];if(!s)continue;let o=Zs(e,s);if(o)return {outputPath:o,publicUrl:s}}return null}function $a(e,t,r){for(let n of $t(e,r)){let s=t[n];if(!s)continue;let o=Zs(e,s);if(o)return o}return null}function ft(e,t,r){let n=$a(e,t,r);if(n)return n;try{return Ft(e,r).originalFilePath}catch{return null}}function Oa(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?v.join(v.resolve(e),"queue-runner-heartbeat.json"):""}async function Yr(e){let t=Oa();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 Ma(e,t){let r=new Set;for(let n of t.donePages)r.add(v.resolve(Me(e,n)));for(let n of t.doneAssets){let s=ft(e,t.assetMap,n);!s||!Ee(s)||r.add(v.resolve(s));}for(let n of t.doneSitemaps){let s=ft(e,t.assetMap,n);!s||!Ee(s)||r.add(v.resolve(s));}return [...r]}var lr="re:",ir=new Map;function ja(e){if(!e.startsWith(lr))return null;let t=ir.get(e);if(t!==void 0)return t||null;let r=e.slice(lr.length).trim();if(!r)return ir.set(e,false),null;try{let n=new RegExp(r);return ir.set(e,n),n}catch{return console.warn(`[crawl] Ignoring invalid path matcher regex: ${e}`),ir.set(e,false),null}}function pn(e,t){let r=ja(t);return r?r.test(e):t.startsWith(lr)?false:e.startsWith(t)}function Fa(e,t){return t.find(r=>pn(e,r))}function Xs(e,t){let r=Fa(t.pathname,e.blockedPathPrefixes);if(r)return r.startsWith(lr)?`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 Da(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 Ua(e,t){return Da(e).has(t.hostname)}function La(e){let t=qe(e.trim()).replace(/\\\//g,"/").replace(/^['"]|['"]$/g,"").trim();return t=t.replace(/[)]+$/g,"").trim(),t=t.replace(/;.*$/g,"").trim(),t}function sn(e){let t=qe(e).replace(/\\\//g,"/");return /[{}]|\bwindow\.|\blocation\.|\bincludes\(|\?\?null|\+|%7B|%7D|%22|<|>|\s/.test(t)||/[()[\]'"]/.test(t)}function qa(e){let t=qe(e).replace(/\\\//g,"/").trim(),r=v.extname(t).toLowerCase();return Vs.has(r)||Ks.has(r)?false:/^(?:this|[A-Za-z_$][\w$]*)\.[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/.test(t)}function tt(e){return ca.some(t=>e.pathname.startsWith(t))}function oe(e,t,r=e.sourceOrigin,n,s="url",o=false){let i=La(t);if(!i||i.startsWith("data:")||i.startsWith("blob:")||i.startsWith("mailto:")||i.startsWith("tel:")||i.startsWith("#"))return n?.ignore(s,t,"empty or unsupported scheme",r),null;if(sn(i))return n?.ignore(s,t,"looks like JavaScript/code fragment",r),null;if(qa(i))return n?.ignore(s,t,"looks like JavaScript property access",r),null;try{let c=new URL(i,r);if(eo(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(!Ua(e,c))return n?.ignore(s,c.toString(),"host not allowed",r),null;let a=Xs(e,c);if(a&&!(o&&Or(c)))return n?.reject(s,c.toString(),a,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 eo(e){try{return decodeURIComponent(e.pathname),decodeURIComponent(e.search),decodeURIComponent(e.hash),!1}catch{return true}}function Ce(e,t,r,n){try{let s=new URL(e);return eo(s)?(t.reject(r,e,"malformed URL encoding",n),null):It(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 to(e){return Vs.has(v.extname(e.pathname).toLowerCase())}function Ve(e){return tt(e)?false:Ks.has(v.extname(e.pathname).toLowerCase())}function wt(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function H(e,t){return !!(wt(t)||t.pathname==="/robots.txt"||t.pathname==="/llms.txt"||t.pathname.toLowerCase().endsWith(".xsl")||e.assetPathPrefixes.length>0&&e.assetPathPrefixes.some(r=>pn(t.pathname,r))||to(t)||!Ve(t)&&!tt(t))}function Me(e,t){let r=new URL(t),n=It(r.pathname);if(n===null)throw new Error(`Unsafe page URL pathname: ${r.pathname}`);return n.endsWith("/")?n+="index.html":v.extname(n)||(n+="/index.html"),Tt(e.outputDir,n)}function Wa(e){if(!e.generated404RequestPath)return null;try{let t=new URL(e.generated404RequestPath,e.sourceOrigin);return !t.pathname.endsWith("/")&&v.extname(t.pathname)===""&&(t.pathname+="/"),t.hash="",t.toString()}catch{return null}}function pr(e,t){let r=Wa(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=It(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,i=Tt(e.outputDir,o),c=Wr(e.targetOrigin,o),a=`${c}${r.search}`;if(!s)return {originalPathname:o,originalFilePath:i,originalPublicUrl:c,originalPublicUrlWithSearch:a,preferQueryHashed:s};let d=v.extname(n),u=`${d?n.slice(0,-d.length):n}.${Ae(r.search)}${d||".bin"}`;return {originalPathname:o,originalFilePath:i,originalPublicUrl:c,originalPublicUrlWithSearch:a,preferQueryHashed:s,hashedPathname:u,hashedFilePath:Tt(e.outputDir,u),hashedPublicUrl:Wr(e.targetOrigin,u)}}function Na(e,t,r,n){let s=Ce(t,r,"asset",n);return s?Ft(e,s.toString()):null}function on(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 a of c)try{let d=new URL(a);t[d.origin+o.pathname+o.search]=n;}catch{}try{let a=(()=>{let d=new URL(n,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin);return `${d.pathname}${d.search}`})();t[o.pathname+o.search]=a;}catch{}try{let a=new URL(n,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin).pathname;t[o.pathname]=a;}catch{}try{let a=new URL(s,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin).pathname;t[o.pathname]=a;}catch{}}catch{}}async function ro(e){try{return await F.readFile(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}async function et(e){try{return await F.stat(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}function _a(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 no(e){let t=String(e["last-modified"]||"").trim();if(!t)return null;let r=Date.parse(t);return Number.isFinite(r)?r:null}function Ba(e,t){let r=no(t),n=_a(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 Va(e,t){if(!t)return;let r=no(t);if(r===null)return;let n=await et(e);if(!n)return;let s=new Date(r);await F.utimes(e,n.atime,s).catch(()=>{});}function an(e,t){let r=v.extname(e.pathname).toLowerCase(),n=t["content-type"]||"";return aa.has(r)||ia.some(s=>n.includes(s))&&!n.startsWith("image/")&&!n.includes("font")}function Ka(e){try{let t=v.extname(new URL(e).pathname).toLowerCase();return t===".js"||t===".mjs"}catch{return false}}function Qe(e,t){(e.doneAssets.size%25===0||e.doneAssets.size===1||e.doneAssets.size===e.stats.assetsQueued)&&t.progress(`Asset processing progress: processed ${e.doneAssets.size}, discovered ${e.stats.assetsQueued}.`,{doneAssets:e.doneAssets.size,assetsQueued:e.stats.assetsQueued,phase:"download-assets"});}function Ha(e){return Ae(e.toString("base64"),40)}function so(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 oo(e,t,r){let n=e.assetVariantDigestsByOriginalPath.get(t);n||(n=new Set,e.assetVariantDigestsByOriginalPath.set(t,n)),n.add(r);}async function cn(e,t,r,n,s,o,i){let c=Ft(e,r),a=Ha(n),d=c.originalFilePath,l=c.originalPublicUrlWithSearch;if(c.preferQueryHashed&&c.hashedFilePath&&c.hashedPublicUrl&&so(s,c.originalFilePath,a)){let m=Ys(e,t,r);m?(d=m.outputPath,l=m.publicUrl):(d=c.hashedFilePath,l=c.hashedPublicUrl);}on(e,t,r,l,c.originalPublicUrl);let u=Ee(d)?Buffer.from(Vr(n.toString("utf8"),e,t,d),"utf8"):n,g=await ro(d),f=false;return (g===null||!g.equals(u))&&(await Le(d,u),f=true),o?.debug(`${f?"Saved":"Skipped unchanged"} asset output ${r}`,{phase:"download-assets",url:r,outputPath:d,bytes:u.length,wroteFile:f}),oo(s,c.originalFilePath,a),await Va(d,i),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:l,wroteFile:f}}function rt(e,t,r,n,s){let o=()=>{let u=String(s??"").trim();if(!u)return {discoveredFrom:"",discoveredFromType:"unknown"};if(u==="seed-path"||u==="cli"||u==="generated-404-request-path")return {discoveredFrom:u,discoveredFromType:u};if(/^https?:\/\//i.test(u))try{let g=new URL(u);return wt(g)?{discoveredFrom:u,discoveredFromType:"sitemap"}:H(e,g)?{discoveredFrom:u,discoveredFromType:"asset"}:Ve(g)&&!tt(g)?{discoveredFrom:u,discoveredFromType:"page"}:{discoveredFrom:u,discoveredFromType:"url"}}catch{return {discoveredFrom:u,discoveredFromType:"url"}}return {discoveredFrom:u,discoveredFromType:"url"}},i=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,c=oe(e,r,i,n,"page");if(!c)return;if(!Ve(c)||to(c)){if(H(e,c)){n.info(`Seeded/non-page URL queued as asset: ${c.toString()}`,{url:c.toString(),source:s}),K(e,t,c.toString(),n,s);return}n.reject("page",c.toString(),tt(c)?"asset/internal path cannot be page":"not page-like",s);return}let a=c.toString(),{discoveredFrom:d,discoveredFromType:l}=o();d&&!t.pageDiscoverySources.has(a)&&t.pageDiscoverySources.set(a,d),!t.donePages.has(a)&&!t.queuedPages.has(a)&&(t.queuedPages.add(a),t.pageQueue.push(a),t.stats.pagesQueued++,n.debug(`Queued discovered page ${a}`,{phase:"discovery",url:a,discoveredFrom:d,discoveredFromType:l,outputPath:Me(e,a)}),(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 K(e,t,r,n,s){let o=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,i=oe(e,r,o,n,"asset");if(!i)return;if(!H(e,i)){n.reject("asset",i.toString(),"not a safe asset path/prefix",s);return}let c=i.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 ur(e,t,r,n,s){let o=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,i=oe(e,r,o,n,"sitemap",Wn(s));if(!i)return;if(!wt(i)){n.reject("sitemap",i.toString(),"not sitemap-like",s);return}let c=i.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 za(e){let t=qe(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:Xe(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 Ja(e){return e.split(",").map(t=>t.trim().split(/\s+/)[0]).filter(Boolean)}function Qa(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 Ga(e){return e.replace(/<script\b[^>]*>[\s\S]*?<\/script\s*>/gi,"")}function Za(e,t){if(t==="dom-style")return true;let r=(()=>{try{return v.extname(new URL(e).pathname).toLowerCase()}catch{return ""}})();return !(r===".js"||r===".mjs"||r===".map"||t.includes("javascript"))}function cr(e){return e.trim().replace(/\\/g,"/").replace(/^https?:\/\/[^/]+/i,"").replace(/^\/+/,"")}async function io(e,t,r,n){let s=[];await F.mkdir(t,{recursive:true});let o=await F.readdir(e,{withFileTypes:true});for(let i of o){let c=v.join(e,i.name),a=v.join(t,i.name);if(i.isDirectory()){s.push(...await io(c,a,r,n));continue}await ao(c,a,r,n)&&s.push(a);}return s}async function Ya(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 ao(e,t,r,n){let s=v.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 Ya(e,t)?(n.debug("Skipped unchanged post-crawl copy target",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):(await F.mkdir(v.dirname(t),{recursive:true}),await F.copyFile(e,t),true)}async function Xa(e,t,r){let n=Object.entries(e.postCrawlCopyMap||{}).map(([c,a])=>({sourcePath:c.trim(),prefix:String(a||"").trim()}));if(n.length===0)return [];let s=0,o=[],i=new Set([...t.donePages].map(c=>v.resolve(Me(e,c))));r.mark("copy-extra-paths");for(let c of n){let a=c.sourcePath,d=c.prefix;if(!a||!d){r.warn("Skipped post-crawl copy mapping with empty key/value",{sourcePath:a,prefixPath:d});continue}let l=ma(a);if(!l.resolvedPath){r.warn("Skipped post-crawl copy source because alias root is not configured",{sourcePath:a,alias:l.alias||"",requiredEnv:l.requiredEnv||"",prefixPath:d});continue}let u=l.resolvedPath,g=cr(d);if(!g){r.warn("Skipped post-crawl copy mapping with invalid export prefix",{sourcePath:a,prefixPath:d});continue}let f;try{f=await F.stat(u);}catch{r.warn("Skipped post-crawl copy source because it does not exist",{sourcePath:a,sourceAbs:u,prefixPath:d});continue}let m=v.resolve(e.outputDir,g);if(f.isDirectory()){let y=await io(u,m,i,r);o.push(...y),s+=y.length,y.length>0&&r.progress(`Copied static directory to export: ${u} -> /${g}`,{phase:"copy-extra-paths",sourcePath:u,targetPath:`/${g}`,copiedItems:s,changedFiles:y.length});continue}let w=v.basename(u),p=d.endsWith("/")?v.join(m,w):m;await ao(u,p,i,r)&&(o.push(p),s++,r.progress(`Copied static file to export: ${u} -> /${cr(d.endsWith("/")?`${d}${w}`:d)}`,{phase:"copy-extra-paths",sourcePath:u,targetPath:`/${cr(d.endsWith("/")?`${d}${w}`:d)}`,copiedItems:s}));}return r.endMark("copy-extra-paths",{mappedSources:n.length,copiedItems:s}),o}function ht(e,t,r,n,s){let o=new Set,i=qe(r).replace(/\\\//g,"/"),c=s==="page-html"?Ga(i):i,a=Za(t,s);if(a){for(let d of Qa(c)){let l=oe(e,d,t,n,`${s}:css-url`);l&&H(e,l)&&o.add(l.toString());}for(let d of c.matchAll(/@import\s+(?:url\()?\s*['"]?([^'"\s;]+)['"]?\s*\)?/gi)){let l=oe(e,d[1],t,n,`${s}:css-import`);l&&H(e,l)&&o.add(l.toString());}}if(a||s==="page-html")for(let d of c.matchAll(/<\?xml-stylesheet[^>]+href=["']([^"']+)["'][^>]*\?>/gi)){let l=oe(e,d[1],t,n,`${s}:xml-stylesheet`);l&&H(e,l)&&o.add(l.toString());}if(s==="page-html")for(let d of ds(i)){let l=d.startsWith("//")?`${new URL(e.sourceOrigin).protocol}${d}`:d;if(sn(l)){n.ignore(`${s}:serialized-url`,l,"looks like JavaScript/code fragment",t);continue}let u=oe(e,l,t,n,`${s}:serialized-url`);u&&H(e,u)?o.add(u.toString()):u&&n.ignore("asset",u.toString(),"serialized URL did not pass safe asset path filter",t);}if(s!=="page-html"&&Ka(t))for(let d of ps(i)){if(sn(d)||!/\.(?:js|mjs)(?:[?#]|$)/i.test(d))continue;let l=oe(e,d,t,n,`${s}:relative-asset`);l&&H(e,l)&&o.add(l.toString());}return [...o]}function co(e,t,r,n){let s=new Set,o=qe(r);for(let i of o.matchAll(/href=["']([^"'#\s][^"']*?)["']/gi)){let c=i[1],a=oe(e,c,t,n,"page-link");a&&(Xs(e,a)||Ve(a)&&!tt(a)&&!H(e,a)&&s.add(a.toString()));}return [...s]}async function Ds(e,t,r,n,s,o,i=false,c=[]){let a=new Set(c),d=0,l=new Map,u=(g,f)=>{let m=[f],w=new Set;for(;m.length>0;){let p=m.pop();if(p===g)return true;w.has(p)||(w.add(p),m.push(...l.get(p)??[]));}return false};for(;r.sitemapQueue.length>0;){let g=r.sitemapQueue.shift();r.queuedSitemaps.delete(g);let f=Ce(g,n,"sitemap","sitemap-queue");if(!f)continue;let m=f.toString();if(r.doneSitemaps.has(m))continue;r.doneSitemaps.add(m),n.sitemap(`Fetching sitemap ${m}`,{url:m});let w=true;try{let p=await t.get(m,{timeout:6e4});if(w=!1,!p.ok()){if(n.skip("sitemap",m,p.status()),i&&!a.has(m))throw new Error(`Required child content-sync sitemap returned HTTP ${p.status()}: ${m}`);continue}let h=await p.body(),y=p.url()||m,b=Ce(y,n,"sitemap-response",m);if(!b)continue;let P=b.toString();r.successfulSitemaps.add(m),r.successfulSitemaps.add(P),a.has(m)&&(r.resolvedSitemapRoots.add(P),d+=1);let $=await cn(e,r.assetMap,P,h,r,n);ue(s,e,$.outputPath),ve(s,$.outputPath);let x=h.toString("utf8");/<sitemapindex(?:\s|>)/i.test(x)?r.sitemapIndexUrls.add(P):r.sitemapLeafUrls.add(P);let I=ht(e,P,x,n,"sitemap");for(let C of I)K(e,r,C,n,P);let R=[];for(let C of za(x)){let O=oe(e,C.loc,P,n,"sitemap-loc",!0);if(!O){if(i)throw new Error(`Content-sync sitemap contains an inadmissible location: ${C.loc}`);continue}if(wt(O)){if(u(P,O.toString()))throw new Error(`Sitemap index cycle detected: ${P} -> ${O.toString()}`);if(l.has(P)||l.set(P,new Set),l.get(P).add(O.toString()),R.push(O.toString()),i&&r.doneSitemaps.has(O.toString())&&!r.successfulSitemaps.has(O.toString()))throw new Error(`Required child content-sync sitemap was not resolved: ${O.toString()}`);ur(e,r,O.toString(),n,P);}else if(i){if(Ve(O)&&!H(e,O)){let V=o.requiredUrls.has(O.toString()),U=o.manifest.pages[O.toString()],S=!!U&&!!await et(U.outputPath);if(!V&&!S)throw new Error(`Content-sync sitemap coverage is incomplete for ${O.toString()}.`)}n.ignore("sitemap-loc",O.toString(),"content-sync sitemap-only traversal does not enqueue urlset locations",P);}else H(e,O)?K(e,r,O.toString(),n,P):Ve(O)?(C.lastmod&&(r.sitemapLastmodByPage[O.toString()]=C.lastmod),rt(e,r,O.toString(),n,P)):n.reject("sitemap-loc",O.toString(),"not page/sitemap/asset-like",P);}o.trackManifest&&(o.manifest.assets[P]={url:P,outputPath:$.outputPath,isText:!0,contentType:p.headers()["content-type"]??"application/xml",cacheControl:tn(p.headers()),changeToken:null,tokenSource:null,discoveredAssets:[...new Set([...I,...R])],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!1},await Be(e,o));}catch(p){n.error(`Failed sitemap ${m}`,{url:m,error:String(p)});let h=i&&a.has(m)&&w;if(i&&!h)throw p}}if(i&&a.size>0&&d===0)throw new Error(`None of the configured content-sync sitemap roots could be resolved: ${[...a].join(", ")}`)}async function ec(e,t,r,n,s,o,i){let c=oe(e,n,e.sourceOrigin,s,"asset-fetch");if(!c||!H(e,c))return;let a=c.toString();if(r.doneAssets.has(a))return;let d=o.enabled||o.strictTargeted?o.previousManifest.assets[a]:void 0,l=o.trackManifest?await ar(e,t,o,a,s):null,u=l?.token??null,g=Kr(d?.changeToken,l),f=o.strictTargeted&&(!l?.supported||!l.token)&&Hr(e.sourceOrigin,a),m=o.strictTargeted?o.manifest.assets[a]:void 0;if(m?.lastSeenRunId===o.runId&&await et(m.outputPath)){r.doneAssets.add(a);for(let p of m.discoveredAssets||[])K(e,r,p,s,a);ue(i,e,m.outputPath),gt(i,e,m.outputPath,m.cacheControl),m.isText&&ve(i,m.outputPath),s.info(`Resumed completed content-sync asset ${a}`,{url:a,source:"content-sync-checkpoint"}),Qe(r,s);return}if(d&&(g||f)&&await et(d.outputPath)){o.manifest.assets[a]={...d,changeToken:u,tokenSource:l?.tokenSource??d.tokenSource,discoveredAssets:[...d.discoveredAssets||[]],lastSeenRunId:o.runId,unreferencedRuns:0},delete o.manifest.pages[a],r.doneAssets.add(a);for(let p of d.discoveredAssets||[])K(e,r,p,s,a);gt(i,e,d.outputPath,d.cacheControl),s.info(`Reused unchanged trusted asset ${a}.`,{url:a,tokenSource:l?.tokenSource??"unknown",phase:"download-assets",source:g?"asset-change-token-match":"content-sync-release-fingerprint"}),Qe(r,s);return}let w=(p,h={})=>{if(!d||o.strictTargeted)return false;o.manifest.assets[a]={...d,discoveredAssets:[...d.discoveredAssets||[]],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0},delete o.manifest.pages[a],r.doneAssets.add(a);for(let y of d.discoveredAssets||[])K(e,r,y,s,a);return s.warn(`Keeping previous incremental asset ${a} after fetch issue.`,{url:a,reason:p,preservedFromPreviousManifest:true,...h,phase:"download-assets"}),Qe(r,s),true};s.asset(`Fetching asset ${a}`,{url:a});try{let p=ft(e,r.assetMap,a),h=p?await et(p):null;if(p&&h&&!o.strictTargeted)try{let U=await se(`fetch asset headers ${a}`,6e4,()=>t.head(a,{timeout:6e4})),S=U.headers();if(U.ok()&&Ba(h,S)){let W=await ro(p);if(W!==null){let ce=U.url()||a,E=Ce(ce,s,"asset-response",a);if(!E)return;let D=E.toString(),j=await cn(e,r.assetMap,D,W,r,s,S);j.wroteFile&&(ue(i,e,j.outputPath),Ee(j.outputPath)&&ve(i,j.outputPath));let X=tn(S)||d?.cacheControl||null;gt(i,e,j.outputPath,X),r.doneAssets.add(a),s.info(`Reused existing asset ${a} based on response headers.`,{url:a,filePath:p,lastModified:S["last-modified"]||"",contentLength:S["content-length"]||"",phase:"download-assets"}),Qe(r,s);let Y=an(c,S),ee=d?.discoveredAssets?[...d.discoveredAssets]:[];if(Y||d?.isText){let z=W.toString("utf8"),de=ht(e,D,z,s,"asset:cached");(de.length>0||!d)&&(ee=de);for(let be of ee)K(e,r,be,s,D);}if(o.trackManifest){let z=Y||!!d?.isText;o.manifest.assets[a]={url:a,outputPath:j.outputPath,isText:z,contentType:en(S)||d?.contentType||null,cacheControl:X,changeToken:l?.supported?l.token:null,tokenSource:l?.supported?l.tokenSource??null:null,discoveredAssets:ee,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!z},delete o.manifest.pages[a],(o.enabled||o.strictTargeted)&&z&&await Be(e,o);}return}}}catch{}let y=await se(`fetch asset ${a}`,6e4,()=>t.get(a,{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()}: ${a}`);s.skip("asset",a,y.status());return}let b=await se(`read asset response ${a}`,6e4,()=>y.body()),P=y.url()||a,$=Ce(P,s,"asset-response",a);if(!$)return;let x=$.toString(),I=y.headers(),R=await cn(e,r.assetMap,x,b,r,s,I);R.wroteFile&&(ue(i,e,R.outputPath),Ee(R.outputPath)&&ve(i,R.outputPath));let C=tn(I);gt(i,e,R.outputPath,C),r.doneAssets.add(a),Qe(r,s);let O=an(c,I),V=[];if(O){let U=b.toString("utf8");V=ht(e,x,U,s,`asset:${v.extname(c.pathname).toLowerCase()||I["content-type"]||"unknown"}`);for(let S of V)K(e,r,S,s,x);}o.trackManifest&&(o.manifest.assets[a]={url:a,outputPath:R.outputPath,isText:O,contentType:en(I),cacheControl:C,changeToken:l?.supported?l.token:null,tokenSource:l?.supported?l.tokenSource??null:null,discoveredAssets:V,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!O},delete o.manifest.pages[a],(o.enabled||o.strictTargeted)&&O&&await Be(e,o));}catch(p){if(w("fetch-error",{error:String(p)}))return;if(s.error(`Failed asset ${a}`,{url:a,error:String(p)}),o.strictTargeted)throw p}finally{r.doneAssets.has(a)||(r.doneAssets.add(a),Qe(r,s));}}async function tc(e,t,r,n,s,o){let i=Math.max(1,Number(e.assetDownloadConcurrency||e.concurrency||1)),c=Array.from({length:i},async()=>{for(;r.assetQueue.length>0;){let a=r.assetQueue.shift();a&&(r.queuedAssets.delete(a),await ec(e,t,r,a,n,s,o));}});await Promise.all(c);}function dr(e,t){let r=he(e.outputDir,t);if(!r)throw new Error(`Remote export output is outside outputDir: ${t}`);return r}function rc(e,t){return an(e,{"content-type":t})}function Us(e){return /(?:^|\/)index\.html?$|\.html?$/i.test(e)}async function nc(e,t,r,n,s,o,i,c,a){let d=Math.max(1,Math.min(100,a)),l=20;for(;r.assetQueue.length>0;){let u=r.assetQueue.splice(0,d*l);for(let w of u)r.queuedAssets.delete(w);await dn(e,t,s,u,n);let g=[];for(let w of u){let p=s.manifest.assets[w],h=p?he(e.outputDir,p.outputPath):null,y=h?i.objects[h]:void 0,b=s.changeTokenCache.get(w)??null;if(!(s.enabled&&p&&y?.kind==="asset"&&y.sourceUrl===w&&(Kr(p.changeToken,b)||(!b?.supported||!b.token)&&Hr(e.sourceOrigin,w)))||!p||!h||!y){g.push(w);continue}s.manifest.assets[w]={...p,changeToken:b?.token??null,tokenSource:b?.tokenSource??p.tokenSource,lastSeenRunId:s.runId,unreferencedRuns:0};for(let $ of p.discoveredAssets||[])K(e,r,$,n,w);r.doneAssets.add(w),gt(o,e,p.outputPath,p.cacheControl),n.info(`Reused unchanged S3-native asset ${w}.`,{url:w,relativePath:h,tokenSource:b?.tokenSource??"release-fingerprint",phase:"download-assets",execution:"lambda"}),Qe(r,n);}if(g.length===0)continue;let f=[];for(let w=0;w<g.length;w+=l)f.push(g.slice(w,w+l));let m=await Promise.all(f.map(w=>c.fetch({urls:w,ignoreHttpsErrors:e.ignoreHttpsErrors,timeoutMs:6e4})));for(let w of m)for(let p of w.assets){let h=p.requestedUrl;try{if(p.outcome!=="fetched"||!p.object||!p.finalUrl){let C=p.error||`Remote asset fetch ended with ${p.outcome}.`;if(n.error(`Failed remote asset ${h}`,{url:h,status:p.httpStatus,outcome:p.outcome,error:C,execution:"lambda"}),s.strictTargeted)throw new Error(C);continue}let y=Ce(p.finalUrl,n,"remote-asset-response",h);if(!y)continue;let b=Ft(e,y.toString()),P=b.originalFilePath,$=b.originalPublicUrlWithSearch;if(b.preferQueryHashed&&b.hashedFilePath&&b.hashedPublicUrl&&so(r,b.originalFilePath,p.object.sha256)){let C=Ys(e,r.assetMap,y.toString());P=C?.outputPath??b.hashedFilePath,$=C?.publicUrl??b.hashedPublicUrl;}on(e,r.assetMap,y.toString(),$,b.originalPublicUrl),y.toString()!==h&&on(e,r.assetMap,h,$,b.originalPublicUrl),oo(r,b.originalFilePath,p.object.sha256);let x=dr(e,P),I=p.contentType?.trim()||"application/octet-stream",R=rc(y,I);mt(i,{relativePath:x,sourceUrl:h,kind:"asset",contentType:I,cacheControl:p.cacheControl,isText:R,object:p.object}),ue(o,e,P),R&&ve(o,P),gt(o,e,P,p.cacheControl);for(let C of p.discoveredAssets)K(e,r,C,n,y.toString());r.stats.assetsSaved++,s.trackManifest&&(s.manifest.assets[h]={url:h,outputPath:P,isText:R,contentType:I,cacheControl:p.cacheControl,changeToken:null,tokenSource:null,discoveredAssets:[...p.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:p.object.bytes,taskId:w.taskId,attempts:w.attempts,execution:"lambda"});}finally{r.doneAssets.add(h),Qe(r,n);}}}}async function sc(e,t,r,n){for(let s of [...t.changedFiles].sort()){if(r.objects[s])continue;let o=v.join(e.outputDir,s),i=await F.readFile(o).catch(l=>{throw new Error(`Coordinator-owned output is missing before S3 staging: ${s}`,{cause:l})}),c=Ee(o),a=String(vs.lookup(s)||"")||(c?"text/plain; charset=utf-8":"application/octet-stream"),d=await n.writeWorkspaceObject({key:`${r.workspace.prefix}jobs/${r.jobId}/coordinator/${Ae(s,40)}-${v.basename(s)}`,body:i,contentType:a,cacheControl:Us(s)?e.s3.htmlCacheControl:e.s3.assetCacheControl});mt(r,{relativePath:s,sourceUrl:null,kind:"generated",contentType:a,cacheControl:Us(s)?e.s3.htmlCacheControl:e.s3.assetCacheControl,isText:c,object:d});}}function oc(e){let t=Math.ceil(e/1e3);return `${t} second${t===1?"":"s"}`}function _e(e){return Math.max(6e4,e.navigationTimeoutMs*2,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+15e3)}function Et(e){return Math.max(15e3,Math.min(6e4,_e(e)))}function lo(e){return Math.max(15e3,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+1e4)}function mn(e){return e.stats.pagesCompleted}function ln(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 Xr(e,t,r){let n=ln(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 Ot(e,t));},t);})])}finally{n!==void 0&&globalThis.clearTimeout(n);}}var Ot=class extends Error{constructor(r,n){super(`${r} timed out after ${oc(n)}.`);this.operation=r;this.timeoutMs=n;this.name="OperationTimeoutError";}operation;timeoutMs};async function ic(e,t){let r=lo(e);await t.evaluate(async n=>{await new Promise(s=>{let o=0,i=700,c=Date.now(),a=()=>{window.clearInterval(d),window.scrollTo(0,0),s();},d=window.setInterval(()=>{if(Date.now()-c>=n.maxDurationMs){a();return}window.scrollBy(0,i),o+=i;let l=document.body?.scrollHeight||0,u=document.documentElement?.scrollHeight||0,g=Math.max(l,u);o>=g+window.innerHeight&&a();},120);});},{maxDurationMs:r});}async function ac(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 ic(e,t),await t.waitForTimeout(e.readiness.fallbackWaitMs);}async function cc(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 uo(e,t){return e.noJavaScriptRenderPathPrefixes.some(r=>pn(t,r))}async function lc(e,t,r,n,s=true){let o=new Set;if(s){let c=await t.evaluate(()=>{let a=new Set,d=["href","src","poster","data-src","data-lazy-src","data-original","data-bg","data-background","data-href","data-resource-url"],l=new Set,u=["srcset","data-srcset","data-lazy-srcset"],g=f=>{f.querySelectorAll("*").forEach(m=>{for(let p of d){let h=m.getAttribute(p);h&&a.add(h);}for(let p of u){let h=m.getAttribute(p);h&&h.split(",").forEach(y=>{let b=y.trim().split(/\s+/)[0];b&&a.add(b);});}let w=m.getAttribute("style");w&&l.add(w),m.shadowRoot&&g(m.shadowRoot);});};return g(document),{attrs:[...a],styles:[...l]}});for(let a of c.attrs)for(let d of Ja(a)){let l=oe(e,d,r,n,"dom-attr");l&&H(e,l)&&o.add(l.toString());}for(let a of c.styles)for(let d of ht(e,r,a,n,"dom-style"))o.add(d);}let i=await t.content();for(let c of ht(e,r,i,n,"page-html"))o.add(c);return [...o]}async function Ls(e){return await e.evaluate(()=>{document.querySelectorAll("#wpsuite-consent-root").forEach(t=>t.remove()),document.documentElement.classList.remove("wpsuite-consent-scroll-locked"),document.body.classList.remove("wpsuite-consent-scroll-locked");}),e.content()}async function un(e,t,r,n,s){let o=Me(e,t);await Le(o,r,"utf8");let i=await F.stat(o);s?.debug(`Saved page output ${t}`,{phase:"save-pages",url:t,outputPath:o,bytes:i.size}),n.stats.pagesSaved++;let c=Math.min(n.stats.pagesQueued,mn(n)+1);s&&(n.stats.pagesSaved%10===0||n.stats.pagesSaved===1)&&s.progress(`Page progress: processed ${c}, rendered ${n.stats.pagesRendered}, saved ${n.stats.pagesSaved}, discovered ${n.stats.pagesQueued}.`,{donePages:c,claimedPages:n.donePages.size,pagesRendered:n.stats.pagesRendered,pagesSaved:n.stats.pagesSaved,pagesQueued:n.stats.pagesQueued,pageQueue:n.pageQueue.length,phase:"save-pages"});}function po(e){let t=mn(e);return t%5===0||t===1||t===e.stats.pagesQueued}function mo(e,t){let r=mn(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 uc(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 dc(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 uc(n),n}async function pc(e,t,r,n,s,o,i,c,a,d){for(;t.pageQueue.length>0&&!(e.maxPages>0&&t.donePages.size>=e.maxPages);){let l=t.pageQueue.shift();if(!l)break;t.queuedPages.delete(l);try{let u=Ce(l,r,"page","page-queue");if(!u)continue;let g=u.toString();if(l=g,t.queuedPages.delete(l),t.donePages.has(l))continue;t.donePages.add(l),o.enabled&&o.seenPages.add(l);let f=pr(e,l),m=uo(e,u.pathname);if(!Ve(u)||H(e,u)||tt(u)){if(H(e,u)){r.info(`Remote worker redirected non-page URL to asset queue: ${l}`,{url:l,source:"remote-worker-guard"}),K(e,t,l,r,"remote-worker-guard");continue}r.reject("page",l,"guard rejected non-page URL before remote rendering","remote-worker");continue}let w=o.strictTargeted?o.manifest.pages[l]:void 0;if(w?.lastSeenRunId===o.runId&&await et(w.outputPath)){o.seenPages.add(l);for(let R of w.discoveredAssets||[])K(e,t,R,r,l);ue(i,e,w.outputPath),ve(i,w.outputPath),r.info(`Resumed completed content-sync page ${l}`,{url:l,source:"content-sync-checkpoint"});continue}let p=await Gs(e,s,t,o,l,r);if(o.enabled&&n!=="single-url"&&n!=="content-sync"){let R=o.manifest.pages[l];if(p.action==="reuse"&&R){for(let C of R.discoveredAssets)K(e,t,C,r,l);for(let C of R.discoveredPages)rt(e,t,C,r,l);R.lastSeenRunId=o.runId,R.sitemapLastmod=Xe(t.sitemapLastmodByPage[l])??R.sitemapLastmod,p.changeToken?.supported&&(R.changeToken=p.changeToken.token,R.tokenSource=p.changeToken.tokenSource??R.tokenSource),r.info(`Incremental reuse skipped unchanged page ${l}`,{url:l,mode:"incremental",reason:p.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}r.page(`Rendering remotely ${l}`,{url:l,execution:"lambda"});let h=await a.render({sourceOrigin:e.sourceOrigin,url:l,options:{ignoreHttpsErrors:e.ignoreHttpsErrors,javaScriptEnabled:!m,viewport:e.viewport,navigationTimeoutMs:e.navigationTimeoutMs,autoScrollTimeoutMs:lo(e),readiness:e.readiness},downloadHtml:!d}),y=h.page,b=y.finalUrl?Ce(y.finalUrl,r,"remote-final-response",l)?.toString():void 0;if(n==="content-sync"&&b&&b!==g){if(o.deleteOnMissingUrls.has(l)){await Mt(e,o,i,l,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive route redirected to ${b}`,{url:l,finalResponseUrl:b,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${l} -> ${b}`)}let P=y.httpStatus;if(f){if(P!==404){r.error(`Generated 404 request path returned unexpected status for ${l}`,{url:l,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(l)){await Mt(e,o,i,l,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive page ${l}`,{url:l,status:P,source:"content-sync-tombstone"});continue}if(n==="content-sync")throw new Error(`Required content-sync route returned HTTP ${P}: ${l}`);r.skip("page",l,P,t.pageDiscoverySources.get(l));continue}t.stats.pagesRendered++;let $=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 ht(e,g,h.html,r,"remote-page-html"))$.add(R);}for(let R of [...y.discoveredAssets,...y.networkUrls]){let C=oe(e,R,g,r,"remote-render");C&&H(e,C)&&$.add(C.toString());}for(let R of $)K(e,t,R,r,l);let x=n!=="single-url"&&n!=="content-sync"&&!f?[...new Set([...!d&&h.html!==null?co(e,l,h.html,r):[],...y.discoveredPages])]:[];for(let R of x)rt(e,t,R,r,l);for(let R of y.cookies){let C=R.sameSite==="Strict"||R.sameSite==="Lax"||R.sameSite==="None"?R.sameSite:"Unspecified",O=!Number.isFinite(R.expires)||R.expires<=0;c.add({name:R.name,domain:R.domain,path:R.path||"/",expires:O?null:R.expires,session:O,secure:R.secure,httpOnly:R.httpOnly,sameSite:C,partitioned:R.partitioned===!0});}let I=Me(e,l);if(d){if(!y.html)throw new Error("Remote render completed without a workspace HTML object.");mt(d,{relativePath:dr(e,I),sourceUrl:l,kind:"page",contentType:y.contentType?.trim()||"text/html; charset=utf-8",cacheControl:e.s3.htmlCacheControl,isText:!0,object:y.html});}else {if(h.html===null)throw new Error("Remote render did not return HTML for local output mode.");await un(e,l,h.html,t,r);}ue(i,e,I),ve(i,I),o.trackManifest&&(o.manifest.pages[l]={url:l,outputPath:I,contentType:y.contentType?.trim()||null,changeToken:p.changeToken?.supported?p.changeToken.token:null,tokenSource:p.changeToken?.supported?p.changeToken.tokenSource??null:null,sitemapLastmod:Xe(t.sitemapLastmodByPage[l])??null,discoveredPages:x,discoveredAssets:[...$],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Be(e,o)),r.debug(`Remote render completed ${l}`,{url:l,taskId:h.taskId,attempts:h.attempts,execution:"lambda"});}catch(u){if(r.error(`Failed remote page ${l}`,{url:l,error:String(u),execution:"lambda"}),o.strictTargeted)throw u}finally{t.stats.pagesCompleted++,po(t)&&mo(t,r);}}}async function mc(e,t,r,n,s,o,i,c){let a=await chromium.launch({headless:true}),d=ln(a);d.pid!==null&&r.debug(`Launched crawl worker browser PID ${d.pid}`,{browserPid:d.pid});try{for(;t.pageQueue.length>0;){a||(a=await chromium.launch({headless:!0}),d=ln(a),d.pid!==null&&r.debug(`Relaunched crawl worker browser PID ${d.pid}`,{browserPid:d.pid}));let l=a;if(e.maxPages>0&&t.donePages.size>=e.maxPages)break;let u=t.pageQueue.shift();if(!u)break;t.queuedPages.delete(u);let g=null,f=null,m=!1,w=[],p=new Set;try{let h=Ce(u,r,"page","page-queue");if(!h)continue;let y=h.toString();if(u=y,t.queuedPages.delete(u),t.donePages.has(u))continue;t.donePages.add(u),o.enabled&&o.seenPages.add(u);let b=pr(e,u),P=uo(e,h.pathname);if(!Ve(h)||H(e,h)||tt(h)){if(H(e,h)){r.info(`Worker redirected non-page URL to asset queue: ${u}`,{url:u,source:"worker-guard"}),K(e,t,u,r,"worker-guard");continue}r.reject("page",u,"guard rejected non-page URL before rendering","worker");continue}let $=o.strictTargeted?o.manifest.pages[u]:void 0;if($?.lastSeenRunId===o.runId&&await et($.outputPath)){o.seenPages.add(u);for(let E of $.discoveredAssets||[])K(e,t,E,r,u);ue(i,e,$.outputPath),ve(i,$.outputPath),r.info(`Resumed completed content-sync page ${u}`,{url:u,source:"content-sync-checkpoint"});continue}let x=await Gs(e,s,t,o,u,r);if(o.enabled&&n!=="single-url"&&n!=="content-sync"){let E=o.manifest.pages[u];if(x.action==="reuse"&&E){for(let D of E.discoveredAssets)K(e,t,D,r,u);for(let D of E.discoveredPages)rt(e,t,D,r,u);E.lastSeenRunId=o.runId,E.sitemapLastmod=Xe(t.sitemapLastmodByPage[u])??E.sitemapLastmod,x.changeToken?.supported&&(E.changeToken=x.changeToken.token,E.tokenSource=x.changeToken.tokenSource??E.tokenSource),r.info(`Incremental reuse skipped unchanged page ${u}`,{url:u,mode:"incremental",reason:x.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}g=await se(`create page context ${u}`,_e(e),()=>dc(l,e,P)),f=await se(`create page ${u}`,_e(e),()=>g.newPage()),w=await g.cookies();let I=!1,R=new Set;await f.route("**/*",async E=>{let D=E.request();try{let j=oe(e,D.url(),e.sourceOrigin,r,"network-request");j&&H(e,j)&&(R.add(j.toString()),K(e,t,j.toString(),r,u));}catch{}if(D.isNavigationRequest()&&D.frame()===f.mainFrame())try{let j=new URL(D.url()),X=new URL(y);if(j.hash="",X.hash="",j.href===X.href){if(I){r.warn(`Blocked same-page navigation/reload for ${u}`,{url:u,requestUrl:D.url()});try{await E.abort("aborted");}catch(Y){r.debug(`Ignored canceled route while blocking reload for ${u}`,{url:u,error:String(Y)});}return}I=!0;}}catch{}try{await E.continue();}catch(j){r.debug(`Ignored canceled network route for ${u}`,{url:u,requestUrl:D.url(),error:String(j)});}}),f.on("response",E=>{try{let j=oe(e,E.url(),e.sourceOrigin,r,"network-response");j&&H(e,j)&&(R.add(j.toString()),K(e,t,j.toString(),r,u));}catch{}let D=E.headerValues("set-cookie").then(j=>{for(let X of j)c.add(ws(X,E.url()));}).catch(()=>{}).then(()=>{});p.add(D),D.finally(()=>p.delete(D));}),r.page(`Rendering ${u}`,{url:u});let C=null,O=null;try{if(C=await f.goto(u,{waitUntil:"domcontentloaded",timeout:e.navigationTimeoutMs}),C&&C.ok())try{O=await se(`read response body ${u}`,_e(e),()=>C.text());}catch{O=null;}}catch(E){r.warn(`Navigation issue for ${u}; saving current DOM if available`,{url:u,error:String(E)});}if(n==="content-sync"&&C){let E=Ce(C.url(),r,"content-sync-final-response",u)?.toString();if(E&&E!==y){if(o.deleteOnMissingUrls.has(u)){await Mt(e,o,i,u,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive route redirected to ${E}`,{url:u,finalResponseUrl:E,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${u} -> ${E}`)}}if(b){let E=C?.status()??null;if(E!==404){r.error(`Generated 404 request path returned unexpected status for ${u}`,{url:u,expectedStatus:404,status:E,requestPath:e.generated404RequestPath});continue}r.info(`Capturing rendered 404 page for ${u}`,{url:u,status:E,outputPath:Me(e,u)});}else if(C&&!C.ok()){if(n==="content-sync"&&(C.status()===404||C.status()===410)&&o.deleteOnMissingUrls.has(u)){await Mt(e,o,i,u,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive page ${u}`,{url:u,status:C.status(),source:"content-sync-tombstone"}),await f.close();continue}if(n==="content-sync")throw new Error(`Required content-sync route returned HTTP ${C.status()}: ${u}`);r.skip("page",u,C.status(),t.pageDiscoverySources.get(u)),await f.close();continue}P?(r.info(`Rendering without JS execution for ${u}`,{url:u,mode:"no-js"}),await se(`wait for rendered page ${u}`,_e(e),()=>cc(e,f))):await se(`wait for rendered page ${u}`,_e(e),()=>ac(e,f)),t.stats.pagesRendered++;let V=await se(`extract rendered assets ${u}`,_e(e),()=>lc(e,f,y,r,!P));for(let E of V)K(e,t,E,r,u);let U=[...new Set([...V,...R])],S=O??await se(`capture rendered html ${u}`,_e(e),()=>Ls(f)),W=n!=="single-url"&&n!=="content-sync"&&!b?co(e,u,S,r):[];if(n!=="single-url"&&n!=="content-sync"&&!b)for(let E of W)rt(e,t,E,r,u);await un(e,u,S,t,r);let ce=Me(e,u);if(ue(i,e,ce),ve(i,ce),o.trackManifest){let E=C?.headers();o.manifest.pages[u]={url:u,outputPath:ce,contentType:en(E),changeToken:x.changeToken?.supported?x.changeToken.token:null,tokenSource:x.changeToken?.supported?x.changeToken.tokenSource??null:null,sitemapLastmod:Xe(t.sitemapLastmodByPage[u])??null,discoveredPages:W,discoveredAssets:U,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Be(e,o);}}catch(h){if(m=h instanceof Ot,o.strictTargeted)throw r.error(`Failed required content-sync page ${u}`,{url:u,error:String(h)}),h;if(m)r.warn(`Skipped partial DOM for timed-out page ${u}`,{url:u,error:String(h),recovery:"recycle-worker-browser"});else try{let y=f?await se(`capture partial html ${u}`,_e(e),()=>Ls(f)):"";if(y&&y.trim()){await un(e,u,y,t,r);let b=Me(e,u);if(ue(i,e,b),ve(i,b),o.trackManifest){let P=o.manifest.pages[u]??o.previousManifest.pages[u];o.manifest.pages[u]={url:u,outputPath:b,contentType:P?.contentType??null,changeToken:null,tokenSource:null,sitemapLastmod:Xe(t.sitemapLastmodByPage[u])??P?.sitemapLastmod??null,discoveredPages:P?.discoveredPages?[...P.discoveredPages]:[],discoveredAssets:P?.discoveredAssets?[...P.discoveredAssets]:[],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Be(e,o);}r.warn(`Saved partial DOM for ${u}`,{url:u,error:String(h)});}}catch(y){y instanceof Ot&&(m=!0),r.error(`Could not save partial DOM for ${u}`,{url:u,error:String(y)});}r.error(`Failed page ${u}`,{url:u,error:String(h)});}finally{if(!m&&f&&await se(`drain network routes ${u}`,Et(e),()=>f.unrouteAll({behavior:"wait"})).catch(h=>{m=!0,r.warn(`Could not drain network routes for ${u}`,{url:u,browserPid:d.pid,error:String(h),recovery:"recycle-worker-browser"});}),m&&(a=null,await Xr(l,r,`page operation timeout for ${u}`),await l.close().catch(()=>{})),p.size>0&&await Promise.allSettled([...p]),!m&&g)try{let h=await g.cookies();c.addAll(ys(w,h));}catch{}t.stats.pagesCompleted++,po(t)&&mo(t,r),!m&&f&&await se(`close page ${u}`,Et(e),()=>f.close()).catch(h=>{m=!0,r.warn(`Timed out while closing page ${u}`,{url:u,browserPid:d.pid,error:String(h),recovery:"recycle-worker-browser"});}),!m&&g&&await se(`close page context ${u}`,Et(e),()=>g.close()).catch(h=>{m=!0,r.warn(`Timed out while closing page context for ${u}`,{url:u,browserPid:d.pid,error:String(h),recovery:"recycle-worker-browser"});}),m&&a===l&&(a=null,await Xr(l,r,`page cleanup timeout for ${u}`),await l.close().catch(()=>{}));}}}finally{if(a){let l=a;await se("close worker browser",Et(e),()=>l.close()).catch(u=>(r.warn("Timed out while closing crawl worker browser",{browserPid:d.pid,error:String(u)}),Xr(l,r,"browser.close timeout")));}}}async function gc(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 qs(e){let t=String(e||"").toLowerCase();return t.includes("timeout")||t.includes("timed out")||t.includes("navigation timeout")}function fc(e){let t=String(e?.job?.command||"").trim(),r=String(e?.job?.crawlMode||"full").trim();return t!=="publish"&&t!=="crawl"?false:r!=="incremental"}function hc(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 go(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 Ws(e){let t=String(e.originalFileName||"").toLowerCase();return t.endsWith("errors.jsonl")?0:t.endsWith("errors.json")?1:2}function wc(e,t){let r=Array.isArray(t?.artifacts)?t.artifacts.filter(n=>String(n?.role||"").trim()==="errors"&&String(n?.storedFileName||"").trim()!=="").sort((n,s)=>Ws(n)-Ws(s)).map(n=>v.join(e,String(n.storedFileName))):[];return [...new Set([...r,v.join(e,"errors.jsonl"),v.join(e,"errors.json"),v.join(e,"errors.jsonl.gz"),v.join(e,"errors.json.gz")])]}async function Ns(e){let t=await go(v.join(e,"job.json"));for(let r of wc(e,t)){let n=await gc(r);if(n.length>0)return n}return []}async function yc(e){let t=v.join(e.logDir,"archive"),r;try{r=await F.readdir(t);}catch{return ""}let n="",s=0;for(let o of r){let i=v.join(t,o);if(!(await F.stat(i).catch(()=>null))?.isDirectory())continue;let a=await go(v.join(i,"job.json"));if(!fc(a))continue;let d=hc(a);d>=s&&(s=d,n=i);}return n}async function bc(e){let t=await yc(e),r=t!==""?await Ns(t):await Ns(e.logDir),n=new Set;for(let s of r)if(qs(s.error)||qs(s.message)){let o=s.url;typeof o=="string"&&o&&n.add(o);}return [...n]}async function Sc(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 Pc(e){if(!e)throw new Error("Content-sync crawl requires an immutable impact plan.");let t=JSON.parse(await F.readFile(e,"utf8"));return Rt(t),t}function Rc(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(/\/+$/,""),i=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(i)})}function At(e,t,r){let n=r==="page"?Me(e,t):Ft(e,t).originalFilePath;return he(e.outputDir,n)}async function Mt(e,t,r,n,s){let o=t.manifest.pages[n];if(!o)return;let i=he(e.outputDir,o.outputPath);if(!i)throw new Error(`Manifest-owned page path escaped output root: ${n}`);let c=v.resolve(o.outputPath);Object.entries(t.manifest.pages).some(([d,l])=>d!==n&&!s.has(d)&&v.resolve(l.outputPath)===c)||(await F.rm(o.outputPath,{force:true}),r.deletedFiles.add(i),r.revalidateFiles.delete(i)),delete t.manifest.pages[n];}async function vc(e,t,r,n,s){let o=t.manifest.assets[n];if(!o)return;let i=he(e.outputDir,o.outputPath);if(!i)throw new Error(`Manifest-owned resource path escaped output root: ${n}`);let c=v.resolve(o.outputPath);Object.entries(t.manifest.assets).some(([d,l])=>d!==n&&!s.has(d)&&v.resolve(l.outputPath)===c)||(await F.rm(o.outputPath,{force:true}),r.deletedFiles.add(i)),delete t.manifest.assets[n];}function Cc(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 i of o?.discoveredAssets??[])try{wt(new URL(i))&&n.push(i);}catch{}}return r}async function kc(e,t,r,n,s){let o=Cc(r.previousManifest,s);for(let i of o){if(t.successfulSitemaps.has(i))continue;let c=r.previousManifest.assets[i];if(!c)continue;let a=he(e.outputDir,c.outputPath);if(!a)throw new Error(`Manifest-owned sitemap path escaped output root: ${i}`);await F.rm(c.outputPath,{force:true}),n.deletedFiles.add(a),delete r.manifest.assets[i];}await Be(e,r);}async function xc(e){try{let t=await F.readFile(v.join(e.outputDir,"asset-map.json"),"utf8");return JSON.parse(t)}catch{return {}}}async function Ic(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(v.join(e,r));}catch(n){if(n.code!=="ENOENT")throw n}}));}function _s(e,t,r,n,s){let o=oe(e,r,e.sourceOrigin,n,"manual-url");o&&(wt(o)?ur(e,t,o.toString(),n,s):H(e,o)?K(e,t,o.toString(),n,s):Ve(o)?rt(e,t,o.toString(),n,s):n.reject("manual-url",o.toString(),"not page/sitemap/asset-like",s));}async function fo(e=process.argv.slice(2)){let t=[],r=await xe(void 0,{onRemotePublisherConfigDiagnostic:k=>{t.push(k);}}),n=Ct(r.sitemapPaths),s=t.at(-1)??null,o=la(e),i=o.mode==="content-sync"?await Pc(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 bc(r)),o.mode==="single-url"&&(c=await Sc(o.urls)),i&&(c=[...i.directRenderUrls,...i.archiveFamilyUrls,...i.resourceRenderUrls]);let a=o.crawlMode==="incremental"&&o.mode==="full",d=a&&typeof r.subscriptionType=="string"&&r.subscriptionType.trim()!=="";if(i&&r.subscriptionType!=="PROFESSIONAL"&&r.subscriptionType!=="AGENCY")throw new Error("Content-sync requires an active Professional or Agency subscription.");let l=(o.mode==="full"||o.mode==="single-url"||o.mode==="content-sync")&&!o.resumeRewrite,u=o.mode!=="full"||o.resumeRewrite||d,g=u?await xc(r):{},f=d||o.mode==="single-url"||o.mode==="content-sync"?await wa(r):zs(),m=new Set(c),w=new Set;if(i){for(let k of i.archiveFamilies)if(k.foundPosts===0)for(let L of k.pageUrls)w.add(L);for(let k of Object.keys(f.pages))Rc(k,i)&&!m.has(k)&&(c.push(k),w.add(k));}let p={enabled:d,trackManifest:l,manifest:nn(f),previousManifest:nn(f),runId:i?.jobId??`${Date.now()}`,seenPages:new Set,changeTokenCache:new Map,pageChangeTokenPrimePromise:null,manifestSaveChain:Promise.resolve(),manifestPendingChanges:0,lastManifestSavedAt:Date.now(),strictTargeted:!!i,requiredUrls:m,deleteOnMissingUrls:w};await Ic(r.logDir);let h=new Jt(r.logDir,r.logLevel),y=o.remoteRender??r.remoteWorkers?.render?.enabled===true,b=nr({explicitPath:o.remoteWorkerConfigPath,runtimeDir:jt()}),P=null,$=null,x=null,I=null,R=Math.max(1,r.concurrency),C=1,O=r.remoteWorkers?.rewrite?.enabled===true,V=r.remoteWorkers?.deploy?.enabled===true,U=y&&O&&V&&!o.resumeRewrite;if(!U&&o.mode==="full"&&!o.resumeRewrite&&await F.rm(sr(r),{force:true}),y&&!o.resumeRewrite||O){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 k=await Yt(b),L=false;if(x=new pt(k,{},{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=>{L||(L=true,h.warn("Lambda progress status could not be read; task execution continues.",{error:M.message}));}}),U){if(!k.functions.asset)throw new Error("Full Lambda delegation requires an asset worker. Redeploy the Lambda worker stack and reinstall remote-workers.json.");let M=u?await or(r):null;I=As(`crawl-${Ae(p.runId,40)}`,k,M),$=new er({client:x,jobId:I.jobId,maxAttempts:2}),h.info("S3-native Lambda data plane is enabled.",{execution:"lambda",assetWorker:k.functions.asset,workspaceBucket:k.workspace.bucket,workspacePrefix:k.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,U?r.concurrency:r.remoteWorkers?.render?.concurrency??r.concurrency)),C=Math.max(1,Math.min(20,r.remoteWorkers?.render?.batchSize??5)),P=new Xt({client:x,jobId:`crawl-${Ae(p.runId,40)}`,maxAttempts:r.remoteWorkers?.render?.maxAttempts??2,batchSize:C,maxConcurrentBatches:R,onRetry:k=>{h.warn(`Retrying remote render for ${k.url}`,{url:k.url,attempt:k.attempt,maxAttempts:k.maxAttempts,reason:k.reason,execution:"lambda"});}}),h.info("Lambda page rendering is enabled.",{remoteWorkerConfigPath:v.resolve(b),concurrency:R,maxAttempts:r.remoteWorkers?.render?.maxAttempts??2,batchSize:C,execution:"lambda"});}if(s){let k={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.",k):h.warn("Remote WP Suite publisher configuration is unavailable or has no active subscription type.",k);}if(a&&!d){let k="Incremental crawl requires an active remote publisher subscription; refusing to fall back to a destructive full crawl.";throw h.error(k,{requestedMode:o.crawlMode,subscriptionType:r.subscriptionType??null,source:"incremental-subscription-guard"}),await h.flush(),new Error(k)}let S={pageQueue:[],queuedPages:new Set,donePages:new Set,pageDiscoverySources:new Map,assetQueue:[],queuedAssets:new Set,doneAssets:new Set,sitemapQueue:[],queuedSitemaps:new Set,doneSitemaps:new Set,successfulSitemaps:new Set,resolvedSitemapRoots:new Set,sitemapIndexUrls:new Set,sitemapLeafUrls:new Set,sitemapLastmodByPage:{},assetMap:{...g},assetVariantDigestsByOriginalPath:new Map,stats:{pagesQueued:0,pagesRendered:0,pagesCompleted:0,assetsQueued:0,sitemapsQueued:0,assetsSaved:0,pagesSaved:0}},W=ha(),ce=new Gt,E=ua();if(E&&!o.resumeRewrite&&await Zt(E,ce.artifact()),await Je(r),await Qn(r,{startedAt:new Date().toISOString(),outputDir:v.resolve(r.outputDir)}),u||await F.rm(r.outputDir,{recursive:true,force:true}),await F.mkdir(r.outputDir,{recursive:true}),i){let k=new Set([...i.deleteUrls,...p.deleteOnMissingUrls]);for(let M of i.deleteUrls)await Mt(r,p,W,M,k);let L=new Set(i.resourceDeleteUrls);for(let M of i.resourceDeleteUrls)await vc(r,p,W,M,L);}if(o.resumeRewrite)h.info("Resuming final rewrite from existing output.",{phase:"rewrite-text",mode:o.mode,crawlMode:o.crawlMode});else {let k=await request.newContext({ignoreHTTPSErrors:r.ignoreHttpsErrors});try{if(h.mark("discovery"),o.mode==="full"?(n.forEach(M=>ur(r,S,M,h)),await Ds(r,k,S,h,W,p),r.seedPaths.forEach(M=>_s(r,S,M,h,"seed-path")),r.generated404RequestPath&&rt(r,S,r.generated404RequestPath,h,"generated-404-request-path")):(c.forEach(M=>_s(r,S,M,h,"cli")),i?.sitemapUrls.forEach(M=>ur(r,S,M,h,"content-sync-impact")),await Ds(r,k,S,h,W,p,o.mode==="content-sync",i?.sitemapUrls??[]),i&&await kc(r,S,p,W,i.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(),E&&await Zt(E,ce.artifact());return}h.mark("render-pages");let L=Array.from({length:P?R*C:R},()=>P?pc(r,S,h,o.mode,k,p,W,ce,P,I??void 0):mc(r,S,h,o.mode,k,p,W,ce));if(await Promise.all(L),E&&await Zt(E,ce.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}),I&&$?await nc(r,k,S,h,p,W,I,$,r.concurrency):(await dn(r,k,p,[...S.assetQueue],h),await tc(r,k,S,h,p,W)),h.endMark("download-assets",{assets:S.doneAssets.size}),p.enabled&&r.maxPages===0&&(await Ca(r,S,p,h,W),o.mode==="full"&&await ka(r,p,h,W)),o.mode==="full"){let M=await Xa(r,S,h);for(let G of M)if(ue(W,r,G),I&&x){let te=dr(r,G),A=await F.readFile(G),_=Ee(G),T=String(vs.lookup(te)||"")||(_?"text/plain; charset=utf-8":"application/octet-stream"),re=await x.writeWorkspaceObject({key:`${I.workspace.prefix}jobs/${I.jobId}/copied/${Ae(te,40)}-${v.basename(te)}`,body:A,contentType:T});mt(I,{relativePath:te,sourceUrl:null,kind:"generated",contentType:re.contentType,cacheControl:null,isText:_,object:re}),_&&ve(W,G);}}}finally{await se("dispose crawl request context",Et(r),()=>k.dispose()).catch(L=>{h.warn("Timed out while disposing crawl request context",{error:String(L)});});}}if(p.trackManifest&&await Be(r,p,true),I)for(let k of W.deletedFiles)Es(I,k);let D=JSON.stringify(S.assetMap,null,2);if(await F.writeFile(v.join(r.outputDir,"asset-map.json"),D,"utf8"),I&&x){let k=await x.writeWorkspaceObject({key:`${I.workspace.prefix}jobs/${I.jobId}/generated/asset-map.json`,body:D,contentType:"application/json; charset=utf-8"});mt(I,{relativePath:"asset-map.json",sourceUrl:null,kind:"generated",contentType:"application/json; charset=utf-8",cacheControl:r.s3.assetCacheControl,isText:true,object:k});}JSON.stringify(g,null,2)!==JSON.stringify(S.assetMap,null,2)&&ue(W,r,v.join(r.outputDir,"asset-map.json")),I&&x&&await sc(r,W,I,x);let j=o.mode==="single-url"||o.mode==="content-sync"?Ma(r,S):p.enabled&&!o.resumeRewrite?xa(r,p,W,g,S.assetMap):void 0;h.mark("rewrite-text");let X=o.mode==="single-url"||o.mode==="content-sync"?`Rewriting text files touched by URL crawl: 0/${j?.length??0}`:"Rewriting text files...";h.progress(X,{phase:"rewrite-text",index:0,totalFiles:j?.length,changedTextFiles:0}),await Yr(X);let Y=Date.now(),ee={files:j,previousAssetMap:g,onStart:({totalFiles:k,workerCount:L})=>{h.info(`Rewrite phase scanning ${k} text files.`,{phase:"rewrite-text",totalFiles:k,workerCount:L});},onProgress:async({index:k,totalFiles:L,changedTextFiles:M,file:G,changed:te})=>{te&&ue(W,r,v.join(r.outputDir,G)),h.checkpoint(`Rewriting text file ${k}/${L}`,{phase:"rewrite-text",index:k,totalFiles:L,changedTextFiles:M,file:G}),h.debug(`${te?"Rewrote":"Checked unchanged"} text file ${k}/${L}: ${G}`,{phase:"rewrite-text",index:k,totalFiles:L,changedTextFiles:M,file:G,changed:te});let A=Date.now();if(!(k===1||k===L||k%50===0||A-Y>=5e3))return;Y=A;let T=`Rewriting text files: ${k}/${L}`;h.progress(T,{phase:"rewrite-text",index:k,totalFiles:L,changedTextFiles:M,file:G}),await Yr(T);}},z;if(I&&x){let k=j?.map(M=>dr(r,v.isAbsolute(M)?M:v.join(r.outputDir,M))),L=Object.values(I.objects).filter(M=>M.isText&&(!k||k.includes(M.relativePath))).length;await ee.onStart?.({totalFiles:L,workerCount:Math.min(r.concurrency,L)}),z=await rr(r,I,S.assetMap,{client:x,jobId:I.jobId,concurrency:r.concurrency,batchSize:25,maxAttempts:2,...k?{relativePaths:k}:{},onProgress:ee.onProgress}),await $s(r,I);}else O&&x?z=await tr(r,S.assetMap,{client:x,jobId:`crawl-${Ae(p.runId,40)}`,concurrency:r.remoteWorkers?.rewrite?.concurrency??r.rewriteConcurrency,batchSize:r.remoteWorkers?.rewrite?.batchSize??25,maxAttempts:r.remoteWorkers?.rewrite?.maxAttempts??2,onStageProgress:async({index:k,totalFiles:L,file:M})=>{let G=Date.now();if(k!==1&&k!==L&&k%25!==0&&G-Y<5e3)return;Y=G;let te=`Staging text files for Lambda rewrite: ${k}/${L}`;h.progress(te,{phase:"rewrite-stage",index:k,totalFiles:L,file:M,execution:"lambda"}),await Yr(te);},options:ee}):z=await Kt(r,S.assetMap,ee);p.trackManifest&&(Ia(p,j),await Be(r,p,true)),h.endMark("rewrite-text",{changedTextFiles:z});let de=i?[...W.deletedFiles].sort():p.enabled&&!o.resumeRewrite?await Aa(r,S,p,W,h,I):[],be=[...new Set((j||[]).map(k=>he(r.outputDir,v.isAbsolute(k)?k:v.join(r.outputDir,k))).filter(k=>k!==null))].sort();if(i){let k=[...W.changedFiles].sort(),L=new Set(k),M=new Set(i.directRenderUrls.map(q=>At(r,q,"page")).filter(q=>!!q&&L.has(q))),G=new Set(i.archiveFamilyUrls.map(q=>At(r,q,"page")).filter(q=>!!q&&L.has(q))),te=new Set(i.resourceRenderUrls.map(q=>At(r,q,"asset")).filter(q=>!!q&&L.has(q))),A=new Set([...S.sitemapLeafUrls].map(q=>At(r,q,"asset")).filter(q=>!!q&&L.has(q))),_=new Set([...S.sitemapIndexUrls].map(q=>At(r,q,"asset")).filter(q=>!!q&&L.has(q))),T=new Set([...M,...G,...te,...A,..._]),re=k.filter(q=>!T.has(q)),fe=[...new Set([...i.directRenderUrls,...i.archiveFamilyUrls,...S.resolvedSitemapRoots,...S.sitemapLeafUrls,...S.sitemapIndexUrls,...i.deleteUrls,...i.resourceRenderUrls,...i.resourceDeleteUrls].map(q=>{let Se=new URL(q);return `${Se.pathname}${Se.search}`}))].sort();await qr(r,{schemaVersion:2,generatedAt:new Date().toISOString(),outputDir:v.resolve(r.outputDir),runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,changedFiles:k,deletedFiles:de,rewriteTargets:be,revalidateFiles:[...W.revalidateFiles].sort(),consumerId:i.consumerId,fromSequence:i.fromSequence,toSequence:i.toSequence,impactHash:i.impactHash,resolvedSitemapUrls:[...S.resolvedSitemapRoots],uploadGroups:{assets:re,content:[...M].sort(),listings:[...G].sort(),resources:[...te].sort(),sitemapLeaves:[...A].sort(),sitemapIndexes:[..._].reverse()},invalidationPaths:fe});}else await qr(r,{schemaVersion:1,generatedAt:new Date().toISOString(),outputDir:v.resolve(r.outputDir),runMode:o.mode,crawlMode:o.crawlMode,fullSyncRequired:!(p.enabled&&!o.resumeRewrite),changedFiles:p.enabled&&!o.resumeRewrite?[...W.changedFiles].sort():[],deletedFiles:p.enabled&&!o.resumeRewrite?de:[],rewriteTargets:p.enabled&&!o.resumeRewrite?be:[],revalidateFiles:[...W.revalidateFiles].sort()});await Zn(r),h.summary(o.resumeRewrite?`Done. Resumed final rewrite over existing output and changed ${z} 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:z}),await h.flush();}function Tc(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 we(e,t,r){let n=new AbortController,s=globalThis.setTimeout(()=>{n.abort(new Error(`${e} timed out after ${Tc(t)}.`));},t);try{return await r({abortController:n,httpOptions:{abortSignal:n.signal,requestTimeout:t}})}finally{globalThis.clearTimeout(s);}}function Ac(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function mr(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 Ec(e,t){return {...e,...t??{}}}function $c(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function yt(e,t){let r=String(t??e.deploymentTargetOverride??"").trim();if(!Ac(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:Ec(e.s3,s.s3),cloudFront:$c(e.cloudFront,s.cloudFront)}}}function gn(e){return String(e??"").split(";").map(t=>t.trim().toLowerCase()).filter(Boolean).join(";")}var So="wpsuite-sha256",Po="wpsuite-normalized-sha256",wn="public, max-age=0, must-revalidate",qc=/(?:^|[-_:.])(id|uid|nonce|token|hash|instance)(?:$|[-_:.])/i,yn=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,Wc=["SIGINT","SIGTERM"],hr=new Set,gr=new Map,ho=null;function Nc(e){hr.add(e);let t=false;return ()=>{t||(t=true,hr.delete(e));}}async function _c(){let e=[...hr];e.length!==0&&await Promise.allSettled(e.map(async t=>{hr.delete(t),await F.rm(t,{recursive:true,force:true});}));}function Bc(){if(!(gr.size>0))for(let e of Wc){let t=()=>{ho||(ho=_c().finally(()=>{for(let[r,n]of gr)process.off(r,n);gr.clear(),process.exit(1);}));};gr.set(e,t),process.on(e,t);}}Bc();function Vc(e,t){let r=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(r)return v.resolve(r);let n=t.trim();return n?v.join(v.resolve(n),"crawl-manifest.json"):v.join(v.resolve(e),".crawl-manifest.json")}function Ge(e){return v.normalize(e).split(v.sep).join("/")}function wo(e,t){let r=v.relative(v.resolve(e),v.resolve(t));return !r||r.startsWith("..")||v.isAbsolute(r)?null:Ge(r)}async function Kc(e,t,r){let n=Vc(e.outputDir,t),s;try{s=await F.readFile(n,"utf8");}catch(l){return l.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:l instanceof Error?l.message:String(l),source:"deploy-guard"}),null)}let o;try{o=JSON.parse(s);}catch(l){return r.warn("Deploy rewrite guard disabled because the crawl manifest is invalid JSON.",{manifestPath:n,error:l instanceof Error?l.message:String(l),source:"deploy-guard"}),null}let i=new Map,c=new Map,a=0,d=0;for(let[l,u]of Object.entries(o.pages||{})){let g=String(u?.outputPath||"").trim(),f=g?wo(e.outputDir,g):null;if(f){let m=typeof u?.contentType=="string"&&u.contentType?u.contentType:null;m&&c.set(f,m);}!g||u?.rewriteComplete===true||f&&(a++,i.has(f)||i.set(f,{kind:"page",sourceUrl:l,outputPath:v.resolve(g),relativePath:f}));}for(let[l,u]of Object.entries(o.assets||{})){let g=String(u?.outputPath||"").trim(),f=u?.isText===true,m=g?wo(e.outputDir,g):null;if(m){let w=typeof u?.contentType=="string"&&u.contentType?u.contentType:null;w&&c.set(m,w);}!g||!f||u?.rewriteComplete===true||m&&(d++,i.has(m)||i.set(m,{kind:"asset",sourceUrl:l,outputPath:v.resolve(g),relativePath:m}));}return {manifestPath:n,byRelativePath:i,contentTypeByRelativePath:c,pendingPages:a,pendingTextAssets:d}}function Hc(e){return e&&(yn.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 fn(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,r)=>r%2===1?t:Hc(t)).join("")}function zc(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,r)=>r%2===1?t:yn.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 wr(e,t){return Array.isArray(e)?e.map(r=>wr(r,t)):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([r,n])=>[r,wr(n,r)])):typeof e!="string"?e:t&&qc.test(t)?zc(e):yn.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)||/^[0-9]{8,}$/.test(e)?fn(e):e}function Jc(e){return e.replace(/((?:https?:\/\/[^"'\s>]+|\/)(?:wp-content|wp-includes)\/[^"'\s>?]+)\?ver=[A-Za-z0-9._-]+(?=$|[#"'])/gi,"$1")}function Qc(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(wr(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),i=JSON.stringify(wr(o));return Buffer.from(i,"utf8").toString("base64")}catch{}return t}function Gc(e,t){let r=e.toLowerCase();return r==="class"?t.split(/(\s+)/).map((n,s)=>s%2===1?n:fn(n)).join(""):r==="id"||r==="for"||r==="aria-controls"||r==="aria-labelledby"||r==="aria-describedby"||r==="aria-owns"||r==="aria-activedescendant"||r==="name"?fn(t):r==="href"||r==="src"?Jc(t):Qc(r,t)}function Zc(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,i,c,a)=>{let d=Gc(i,a);return ` ${i}=${c}${d}${c}`});return `<${r}${s}>`})}function bt(e){let t=v.extname(e).toLowerCase();return [".html",".htm",".xml",".xsl",".txt"].includes(t)||["robots.txt","sitemap.xml"].includes(v.basename(e))}function fr(e,t){return [e.replace(/^\/+|\/+$/g,""),t.replace(/^\/+/,"")].filter(Boolean).join("/")}async function Yc(e,t,r,n){let s=new Map,o,i=0;do{i++;let c=await we(`S3 list objects page ${i} for s3://${t}/${r||""}`,3e5,({httpOptions:a})=>e.send(new ListObjectsV2Command({Bucket:t,Prefix:r||void 0,ContinuationToken:o}),a));for(let a of c.Contents||[]){if(!a.Key)continue;let d=(a.ETag||"").replace(/^"|"$/g,"").toLowerCase(),l=Number(a.Size??0);s.set(a.Key,{etag:d,size:l,cacheControl:void 0,contentType:void 0});}o=c.NextContinuationToken,n.debug(`Listed S3 keys page ${i}: ${s.size} keys so far`);}while(o);return s}async function Xc(e,t,r,n){if(n.has(r))return n.get(r)??null;try{let s=await we(`S3 head object ${r}`,3e5,({httpOptions:i})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),i)),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 el(e,t,r,n){if(n.cacheControl!==void 0&&n.contentType!==void 0)return {cacheControl:n.cacheControl,contentType:n.contentType};try{let s=await we(`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 yo(e){return String(e||"").split(",").map(t=>t.trim().toLowerCase()).filter(Boolean).sort().join(",")}async function tl(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 i=s.slice(o,o+1e3);if(i.length===0)continue;let c=await we(`S3 delete objects batch ${Math.floor(o/1e3)+1}`,3e5,({httpOptions:a})=>e.send(new DeleteObjectsCommand({Bucket:t,Delete:{Objects:i.map(d=>({Key:d}))}}),a));if(c.Errors&&c.Errors.length>0){let a=c.Errors.map(d=>`${d.Key||"(unknown)"}: ${d.Code||"DeleteFailed"}`);throw new Error(`S3 reported ${a.length} object deletion error(s): ${a.join(", ")}`)}n.info(`Deleted batch of ${i.length} object(s).`);for(let a of i)n.debug(` deleted: ${a}`);}return s}function rl(e){return createHash("md5").update(e).digest("hex")}function Ze(e){return createHash("sha256").update(e).digest("hex")}function yr(e){return createHash("sha256").update(Zc(e.toString("utf8"))).digest("hex")}async function nl(e,t,r,n){if(n.has(r))return n.get(r)??null;try{let i=((await we(`S3 head object metadata ${r}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),c))).Metadata?.[So]||"").trim().toLowerCase()||null;return n.set(r,i),i}catch{return n.set(r,null),null}}async function sl(e,t,r,n,s){if(n.has(r))return n.get(r)??null;try{let i=((await we(`S3 head object normalized metadata ${r}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),c))).Metadata?.[Po]||"").trim().toLowerCase();if(i)return n.set(r,i),i}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 i=await(await we(`S3 get object ${r}`,3e5,({httpOptions:a})=>e.send(new GetObjectCommand({Bucket:t,Key:r}),a))).Body?.transformToByteArray(),c=i?yr(Buffer.from(i)):null;return s.set(r,c),n.set(r,c),c}catch{return s.set(r,null),n.set(r,null),null}}function ol(e){return e.replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"")||"profile"}function il(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 al(e,t,r,n){let s=v.join(e,"asset-map.json"),o;try{o=await F.readFile(s,"utf8");}catch(a){if(a.code==="ENOENT")return;throw a}let i=JSON.parse(o);if(!i||typeof i!="object")return;let c=Object.fromEntries(Object.entries(i).map(([a,d])=>{let l=String(d??"");return t.urlRewriteMode==="absolute"&&t.targetOrigin&&r.targetOrigin&&t.targetOrigin!==r.targetOrigin&&(l=l.split(t.targetOrigin).join(r.targetOrigin)),l=il(l,n),[a,l]}));await F.writeFile(s,JSON.stringify(c,null,2),"utf8");}async function cl(e,t,r,n,s,o,i){let c=e.targetOrigin!==t.targetOrigin,a=Object.keys(n).length>0;if(!r||!c&&!a)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=v.join(v.dirname(e.outputDir),`.deploy-profile-${ol(r)}-${Date.now()}`),l=Nc(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 p=v.join(e.outputDir,w),h=v.join(d,w);await F.mkdir(v.dirname(h),{recursive:!0}),await F.copyFile(p,h);}}else await F.cp(e.outputDir,d,{recursive:!0});let u={...e,sourceOrigin:e.targetOrigin,targetOrigin:t.targetOrigin,extraReplacements:n,outputDir:d},g=1;s.info(`Rewriting text files for deployment profile "${r}"...`);let f={files:o,onStart:({totalFiles:w,workerCount:p})=>{g=p,s.info(`Rewriting ${w} text file(s) for deployment profile "${r}" with ${p} worker(s).`);},onProgress:({index:w,totalFiles:p,changedTextFiles:h,file:y,changed:b})=>{let $=`Rewriting [${w}/${p}] ${y} (${b?"rewrote":"checked"}; ${h} changed so far)`;p<=200||w<=5||w%10===0||w===p?(s.progress($,{file:y,index:w,totalFiles:p,changed:b,changedTextFiles:h,workerCount:g}),s.info($)):s.debug($);}},m=i?await tr(u,{},{...i,options:f}):await Kt(u,{},f);return await al(d,e,t,n),s.info(`Prepared deployment profile "${r}" in ${d} (${m} rewritten text file(s)).`),{outputDir:d,cleanup:async()=>{l(),await F.rm(d,{recursive:!0,force:!0});}}}catch(u){throw s.error(`Failed to prepare deployment profile "${r}" temporary output.`,{tempDir:d,outputDir:e.outputDir,error:u instanceof Error?u.message:String(u),...u instanceof Error&&u.stack?{stack:u.stack}:{}}),await F.rm(d,{recursive:true,force:true}).catch(()=>{}),l(),await s.flush().catch(()=>{}),u}}function Ro(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 ll(e,t){if(!e||typeof e!="object")throw new Error("Remote deploy result must be a JSON object.");let r=e;if(r.schemaVersion!==Z||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 br(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:Z,operation:"deploy-copy",jobId:e.jobId,taskId:n,targetId:e.targetId,items:e.items,...e.deleteRelativeKeys?{deleteRelativeKeys:e.deleteRelativeKeys}:{}}),o=ll(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 ul(e){let t=Ro(e.remoteConfig,e.config),r=ne(`deploy-${Date.now()}-${Ze(Buffer.from(`${t.id}:${e.config.outputDir}`)).slice(0,12)}`,"jobId"),n=dt(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=[],i=Math.max(1,Math.min(16,e.concurrency*2)),c=0;await Promise.all(Array.from({length:Math.min(i,e.files.length)},async()=>{for(;;){let y=c;if(y>=e.files.length)return;c+=1;let b=e.files[y],P=await F.readFile(v.join(e.config.outputDir,b)),$=e.contentTypeByRelativePath?.get(b)||vs.lookup(b)||"application/octet-stream",x=e.revalidateFiles.has(Ge(b))?wn:bt(b)?e.config.s3.htmlCacheControl:e.config.s3.assetCacheControl,I=Ze(P),R=bt(b)?yr(P):void 0,C=`${n}/files/${Ze(Buffer.from(b))}-${v.basename(b)}`.replace(/\/+/g,"/");await e.client.writeWorkspaceObject({key:C,body:P,contentType:String($),cacheControl:x,metadata:{"wpsuite-sha256":I,...R?{"wpsuite-normalized-sha256":R}:{}}}),o[y]={sourceKey:C,targetRelativeKey:b,sha256:I,...R?{normalizedSha256:R}:{},bytes:P.byteLength,contentType:String($),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 a=[],d=Math.max(1,Math.min(500,e.batchSize));for(let y=0;y<o.length;y+=d)a.push(o.slice(y,y+d));let l=[],u=[],g=[],f=0,m=0,w=0,p=Math.max(1,Math.min(Math.max(1,e.concurrency),a.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:p,completedBatches:0,totalBatches:a.length}),await Promise.all(Array.from({length:p},async()=>{for(;;){let y=w;if(y>=a.length)return;w+=1;let b=Date.now(),P=await br({client:e.client,jobId:r,targetId:t.id,batchIndex:y+1,items:a[y],maxAttempts:e.maxAttempts}),$=0,x=0,I=0;for(let C of P.detail.objects)f+=1,C.status==="copied"?(l.push(C.targetKey),$+=1):C.status==="skipped"?(u.push(C.targetKey),x+=1):(g.push(C.targetKey),I+=1),e.log.debug(`Lambda deploy ${C.status}: ${C.targetKey}`,{phase:"deploy-copy",batch:y+1,sourceKey:C.sourceKey,targetKey:C.targetKey,status:C.status,...C.error?{error:C.error}:{}});m+=1;let R=Date.now()-b;e.log.info(`Lambda deploy batch ${y+1}/${a.length} completed: ${$} copied, ${x} skipped unchanged, ${I} failed.`,{phase:"deploy-copy",batch:y+1,totalBatches:a.length,items:P.detail.objects.length,copied:$,skipped:x,failed:I,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: ${f}/${e.files.length}`,{phase:"deploy-copy",index:f,totalFiles:e.files.length,uploaded:l.length,skipped:u.length,failed:g.length,mode:"lambda-s3-copy",workerCount:p,completedBatches:m,totalBatches:a.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 br({client:e.client,jobId:r,targetId:t.id,batchIndex:a.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:l,skippedKeys:u,failedKeys:g,deletedKeys:h}}async function dl(e){let t=Ro(e.remoteConfig,e.config),r=ne(`deploy-manifest-${Date.now()}-${Ze(Buffer.from(t.id)).slice(0,12)}`,"jobId"),n=e.objects.map(f=>({sourceKey:f.object.key,targetRelativeKey:f.relativePath,sha256:f.object.sha256,bytes:f.object.bytes,contentType:f.contentType||"application/octet-stream",cacheControl:e.revalidateFiles.has(Ge(f.relativePath))?wn:bt(f.relativePath)?e.config.s3.htmlCacheControl:e.config.s3.assetCacheControl})),s=[],o=Math.max(1,Math.min(500,e.batchSize));for(let f=0;f<n.length;f+=o)s.push(n.slice(f,f+o));let i=[],c=[],a=[],d=0,l=0,u=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:u}),await Promise.all(Array.from({length:s.length===0?0:u},async()=>{for(;;){let f=d;if(f>=s.length)return;d+=1;let m=await br({client:e.client,jobId:r,targetId:t.id,batchIndex:f+1,items:s[f],maxAttempts:e.maxAttempts});for(let w of m.detail.objects)l+=1,w.status==="copied"?i.push(w.targetKey):w.status==="skipped"?c.push(w.targetKey):a.push(w.targetKey);e.log.progress(`Lambda deploy: ${l}/${n.length}`,{phase:"deploy-copy",index:l,totalFiles:n.length,uploaded:i.length,skipped:c.length,failed:a.length,mode:"lambda-s3-native",workerCount:u});}})),a.length>0)throw new Error(`Remote deployment failed for ${a.length} object(s).`);let g=[];for(let f=0;f<e.deletedFiles.length;f+=1e3){let m=e.deletedFiles.slice(f,f+1e3);if(m.length===0)continue;let w=await br({client:e.client,jobId:r,targetId:t.id,batchIndex:s.length+Math.floor(f/1e3)+1,items:[],deleteRelativeKeys:m,maxAttempts:e.maxAttempts});g.push(...w.detail.deletedKeys);}return {uploadedKeys:i,skippedKeys:c,failedKeys:a,deletedKeys:g}}async function pl(e,t,r,n,s){let o={...t,jobId:ne(`profile-${Ze(Buffer.from(n)).slice(0,16)}-${Date.now()}`,"jobId"),objects:{},assetMap:{...t.assetMap},generatedAt:new Date().toISOString()},i=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 a=i;if(a>=r.length)return;i+=1;let d=r[a],l=dt(e.config.workspace.prefix,"jobs",o.jobId,"profile",Ze(Buffer.from(d.relativePath)),v.basename(d.relativePath));o.objects[d.relativePath]={...d,object:await e.copyWorkspaceObject(d.object,l)};}})),o}async function vo(e=process.argv.slice(2)){let t=await xe(),r=mr(e),n=yt(t,r),s=v.resolve(n.config.logDir||"logs"),o=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",i=o?v.join(v.resolve(o),"deploy-diff.json"):"",c=new lt(s,"deploy.log.jsonl",n.config.logLevel??"info"),a=n.profile?.extraReplacements??{},d=async()=>{};try{let l=await at(t);if(e.includes("--content-sync")&&l?.schemaVersion!==2)throw new Error("Content-sync deployment requires a valid schema-2 targeted deploy plan.");let g=await Gn(t);if(g)throw new Error(`Deployment blocked: the crawl started at ${g.startedAt} did not complete its final rewrite. Run a successful crawl or resume the rewrite before deploying.`);let f=t.remoteWorkers?.rewrite?.enabled===!0,m=t.remoteWorkers?.deploy?.enabled===!0,w=null,p=null;if(f||m){let A=nr({runtimeDir:o});if(!A)throw new Error("Remote Lambda workers are enabled, but the runtime directory is unknown.");w=await Yt(A);let _=!1;p=new pt(w,{},{onProgress:T=>{c.progress(`Lambda ${T.operation} batch: ${T.completedItems+T.failedItems}/${T.totalItems}`,{remoteWorkerOperation:T.operation,remoteWorkerTaskId:T.taskId,remoteWorkerStatus:T.status,remoteWorkerCompletedItems:T.completedItems,remoteWorkerFailedItems:T.failedItems,remoteWorkerTotalItems:T.totalItems,remoteWorkerUpdatedAt:T.updatedAt});},onProgressError:T=>{_||(_=!0,c.warn("Lambda progress status could not be read; task execution continues.",{error:T.message}));}});}let h=m&&f&&t.remoteWorkers?.render?.enabled===!0&&p&&w?.functions.asset?await or(t):null;if(h&&p&&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 A=n.config,_=A.s3SyncMode??"sdk-upload-delete";if(l?.schemaVersion===2&&l.deletedFiles.length>0&&_!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let T=l?.schemaVersion===2?[...l.uploadGroups.assets,...l.uploadGroups.content,...l.uploadGroups.listings,...l.uploadGroups.resources,...l.uploadGroups.sitemapLeaves,...l.uploadGroups.sitemapIndexes]:l?.changedFiles??[];if(l?.schemaVersion===2){let le=[...new Set(T)],Fe=[...new Set(l.changedFiles)];if(le.length!==Fe.length||Fe.some(st=>!le.includes(st)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let fe=!!l&&!l.fullSyncRequired?[...new Set(T.map(Ge))]:Object.keys(h.objects).sort(),q=fe.filter(le=>!h.objects[le]);if(q.length>0)throw new Error(`Remote export manifest is missing ${q.length} planned object(s): ${q.slice(0,10).join(", ")}`);let Se=h,B=fe.map(le=>h.objects[le]);n.name&&(t.targetOrigin!==A.targetOrigin||Object.keys(a).length>0)&&(Se=await pl(p,h,B,n.name||"default",t.concurrency),await rr(A,Se,Se.assetMap,{client:p,jobId:Se.jobId,concurrency:t.concurrency,batchSize:25,maxAttempts:2}),B=fe.map(le=>Se.objects[le])),n.name&&c.info(`Using deployment profile: ${n.name}`),c.info(`Starting S3-native Lambda deploy - s3://${A.s3.bucket}/${A.s3.prefix||""} (${A.s3.region}).`),c.mark("deploy");let ie=await dl({client:p,remoteConfig:w,config:A,objects:B,deletedFiles:_==="sdk-upload-delete"?l?.deletedFiles??[]:[],revalidateFiles:new Set((l?.revalidateFiles??[]).map(Ge)),concurrency:t.concurrency,batchSize:50,maxAttempts:2,log:c});i&&(await F.mkdir(v.dirname(i),{recursive:!0}),await F.writeFile(i,JSON.stringify({generatedAt:new Date().toISOString(),mode:"lambda-s3-native",summary:{totalFiles:B.length,eligibleFiles:B.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")),l?.schemaVersion!==2&&await Je(t),c.endMark("deploy"),await c.flush();return}let y=await cl(t,n.config,n.name,a,c,l?.schemaVersion===2?l.changedFiles:void 0,f&&p?{client:p,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:A,totalFiles:_,file:T})=>{(A===1||A===_||A%25===0)&&c.progress(`Staging deployment-profile rewrite: ${A}/${_}`,{phase:"rewrite-stage",index:A,totalFiles:_,file:T,execution:"lambda"});}}:void 0);d=y.cleanup;let b={...n.config,outputDir:y.outputDir},P=b.s3SyncMode??"sdk-upload-delete";if(l?.schemaVersion===2&&l.deletedFiles.length>0&&P!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let $=l?.schemaVersion!==2&&y.outputDir!==t.outputDir&&(t.targetOrigin!==n.config.targetOrigin||Object.keys(a).length>0),x=!!l&&!l.fullSyncRequired&&!$;if(l?.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}`),l&&l.fullSyncRequired?c.info("Deploy plan requests a full sync; scanning the whole output tree."):l&&$?c.info("Ignoring incremental deploy plan because deployment-profile rewrites require a full sync."):x&&c.info(`Using incremental deploy plan: ${l.changedFiles.length} changed file(s), ${l.deletedFiles.length} deleted file(s).`),c.mark("deploy");let I=new S3Client({region:b.s3.region}),R=y.outputDir===t.outputDir?await Kc(t,o,c):null,C=l?.schemaVersion===2?[...l.uploadGroups.assets,...l.uploadGroups.content,...l.uploadGroups.listings,...l.uploadGroups.resources,...l.uploadGroups.sitemapLeaves,...l.uploadGroups.sitemapIndexes]:l?.changedFiles??[];if(l?.schemaVersion===2){let A=[...new Set(C)],_=[...new Set(l.changedFiles)];if(A.length!==_.length||_.some(T=>!A.includes(T)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let O=x?(await Promise.all(C.map(async A=>{try{return (await F.stat(v.join(b.outputDir,A))).isFile()?A:null}catch{if(l?.schemaVersion===2)throw new Error(`Content-sync planned upload is missing: ${A}`);return null}}))).filter(A=>A!==null):await mi(["**/*"],{cwd:b.outputDir,onlyFiles:!0,dot:!0}),V=[],U=[],S=O.filter(A=>{let _=R?.byRelativePath.get(Ge(A));if(!_)return !0;let T=fr(b.s3.prefix,A);return V.push(A),U.push(T),c.reject(_.kind,_.sourceUrl||A,"rewrite not completed; deploy upload blocked","deploy-guard",{file:A,key:T,outputPath:_.outputPath,manifestPath:R?.manifestPath}),!1}),W=new Set((l?.revalidateFiles||[]).map(Ge)),ce=new Set((x?S:O).map(A=>fr(b.s3.prefix,A)));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"}),V.length>0&&c.warn(`Deploy rewrite guard blocked ${V.length} file(s) from upload because final crawl rewrite is incomplete.`,{rejected:V.length,manifestPath:R?.manifestPath,source:"deploy-guard"}),x?c.info(`Using ${S.length} eligible planned changed file(s) from deploy plan for upload${V.length>0?` (${V.length} blocked by deploy rewrite guard)`:""}.`):c.info(`Found ${O.length} file(s) in output directory "${b.outputDir}"${V.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:O.length,rejected:V.length,deploymentProfile:n.name||"",s3Bucket:b.s3.bucket,s3Prefix:b.s3.prefix||"",targetedPlan:x,deployGuardEnabled:y.outputDir===t.outputDir});let E=0,D=0,j=0,X=[],Y=[],ee=[],z=[],de=[],be=new Map,k=new Map,L=new Map,M=new Map,G=new Map;if(m){if(!p||!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 A=await ul({client:p,remoteConfig:w,config:b,files:S,deletedFiles:P==="sdk-upload-delete"?l?.deletedFiles??[]:[],revalidateFiles:W,contentTypeByRelativePath:R?.contentTypeByRelativePath,concurrency:b.remoteWorkers?.deploy?.concurrency??b.assetDownloadConcurrency,batchSize:b.remoteWorkers?.deploy?.batchSize??50,maxAttempts:b.remoteWorkers?.deploy?.maxAttempts??2,log:c});X.push(...A.uploadedKeys),Y.push(...A.skippedKeys),ee.push(...A.failedKeys),z=A.deletedKeys,E=X.length,D=Y.length,j=ee.length,c.info(`Lambda deploy complete: ${E} copied, ${D} skipped unchanged, ${z.length} deleted, ${j} failed.`),i&&(await F.mkdir(v.dirname(i),{recursive:!0}),await F.writeFile(i,JSON.stringify({generatedAt:new Date().toISOString(),mode:"lambda-s3-copy",summary:{totalFiles:O.length,eligibleFiles:S.length,uploaded:E,skipped:D,rejected:U.length,failed:j,deleted:z.length,protected:0,...n.name?{profile:n.name}:{}},uploadedKeys:X,skippedKeys:Y,rejectedKeys:U,failedKeys:ee,deletedKeys:z,protectedRemoteOnlyKeys:[]},null,2),"utf8")),l?.schemaVersion!==2&&await Je(t),c.endMark("deploy"),await c.flush();return}x||(c.info("Listing existing S3 objects to detect unchanged files..."),be=await Yc(I,b.s3.bucket,b.s3.prefix,c),c.info(`Found ${be.size} existing key(s) in S3.`));let te=async A=>be.has(A)?be.get(A)??null:x?Xc(I,b.s3.bucket,A,k):null;for(let A of S){let _=v.join(b.outputDir,A),T=fr(b.s3.prefix,A),re=R?.contentTypeByRelativePath.get(A),fe=re&&re!=="application/octet-stream"?re:vs.lookup(A)||re||"application/octet-stream",q=W.has(Ge(A)),Se=q?wn:bt(A)?b.s3.htmlCacheControl:b.s3.assetCacheControl,B=E+D+j+1;S.length<=200||B<=5||B%10===0||B===S.length?(c.progress(`Uploading [${B}/${S.length}] ${T} (${fe})`,{file:A,key:T,index:B,totalFiles:S.length,contentType:fe,mode:P}),c.info(`Uploading [${B}/${S.length}] ${T} (${fe})`)):c.debug(`Uploading [${B}/${S.length}] ${T} (${fe})`);try{let Pe=await F.readFile(_),ie=await te(T),le=null,Fe=null;if(ie){let st=await el(I,b.s3.bucket,T,ie),$o=!q||yo(st.cacheControl)===yo(Se),Oo=gn(st.contentType)===gn(String(fe)),vr=$o&&Oo,Mo=Pe.byteLength;if(ie.size===Mo){let Ut=ie.etag.includes("-");if(ie.etag!==""&&!Ut){let jo=rl(Pe);if(ie.etag===jo&&vr){D++,S.length<=200||D<=5||D%25===0?(c.progress(`Skipped unchanged [${B}/${S.length}] ${T}`,{file:A,key:T,index:B,totalFiles:S.length,uploaded:E,skipped:D,failed:j,mode:P,reason:"etag-md5-match"}),c.info(`Skipped unchanged [${B}/${S.length}] ${T}`)):c.debug(`Skipped unchanged [${B}/${S.length}] ${T}`),Y.push(T);continue}}le=Ze(Pe);let Cn=await nl(I,b.s3.bucket,T,L);if(Cn&&Cn===le&&vr){D++,S.length<=200||D<=5||D%25===0?(c.progress(`Skipped unchanged [${B}/${S.length}] ${T}`,{file:A,key:T,index:B,totalFiles:S.length,uploaded:E,skipped:D,failed:j,mode:P,reason:"metadata-sha256-match"}),c.info(`Skipped unchanged [${B}/${S.length}] ${T}`)):c.debug(`Skipped unchanged [${B}/${S.length}] ${T}`),Y.push(T);continue}}if(bt(A)){Fe=yr(Pe);let Ut=await sl(I,b.s3.bucket,T,M,G);if(Ut&&Ut===Fe&&vr){D++,S.length<=200||D<=5||D%25===0?(c.progress(`Skipped semantically unchanged [${B}/${S.length}] ${T}`,{file:A,key:T,index:B,totalFiles:S.length,uploaded:E,skipped:D,failed:j,mode:P,reason:"normalized-html-match"}),c.info(`Skipped semantically unchanged [${B}/${S.length}] ${T}`)):c.debug(`Skipped semantically unchanged [${B}/${S.length}] ${T}`),Y.push(T);continue}}}le=le??Ze(Pe),Fe=Fe??(bt(A)?yr(Pe):null),await we(`S3 upload ${T}`,9e5,({abortController:st})=>new Upload({client:I,abortController:st,params:{Bucket:b.s3.bucket,Key:T,Body:Pe,ContentType:fe,CacheControl:Se,Metadata:{[So]:le,...Fe?{[Po]:Fe}:{}}}}).done()),E++,X.push(T),(E%25===0||E===S.length)&&(c.progress(`Uploaded ${E}/${S.length} file(s)\u2026`,{uploaded:E,skipped:D,failed:j,totalFiles:S.length,mode:P}),c.info(`Uploaded ${E}/${S.length} file(s)\u2026`));}catch(Pe){j++,ee.push(T),c.error(`Failed to upload ${T}: ${Pe.message}`,{key:T,error:String(Pe)});}}if(c.info(`Upload complete: ${E} uploaded, ${D} skipped unchanged, ${U.length} rejected by guard, ${j} failed.`),ee.length>0||U.length>0)throw new Error(`Deployment stopped before deletion because ${ee.length} upload(s) failed and ${U.length} upload(s) were rejected.`);if(P==="sdk-upload-delete"){let A=new Set((l?.deletedFiles??[]).map(T=>fr(b.s3.prefix,T))),_=[...A].filter(T=>!ce.has(T));if(!x&&(de=[...be.keys()].filter(re=>!ce.has(re)).filter(re=>!A.has(re)),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 re of de)c.debug(` protected remote-only object: ${re}`);}z=await tl(I,b.s3.bucket,x?_:_.filter(T=>be.has(T)),c);}else c.info("Sync mode is sdk-upload-only \u2014 skipping stale object deletion.");i&&(await F.mkdir(v.dirname(i),{recursive:!0}),await F.writeFile(i,JSON.stringify({generatedAt:new Date().toISOString(),mode:P,summary:{totalFiles:O.length,eligibleFiles:S.length,uploaded:X.length,skipped:Y.length,rejected:U.length,failed:ee.length,deleted:z.length,protected:de.length,...n.name?{profile:n.name}:{}},uploadedKeys:X,skippedKeys:Y,rejectedKeys:U,failedKeys:ee,deletedKeys:z,protectedRemoteOnlyKeys:de},null,2),"utf8")),l?.schemaVersion!==2&&await Je(t),c.endMark("deploy"),await c.flush();}finally{await d().catch(()=>{});}}function gl(e){let t=e;return t.endsWith("/")?t+="index.html":v.posix.basename(t).includes(".")||(t+="/index.html"),t}function fl(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 Co(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}`,i=gl(s.pathname);r.add(o),r.add(`${i}${s.search}`);let c=fl(i,t);c&&r.add(`${c}${s.search}`);}return [...r].sort()}function bl(){let e=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();return e?v.join(v.resolve(e),"content-sync-invalidation.json"):""}async function bn(e,t){if(!e)return;await F.mkdir(v.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 Sl(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(i=>i.paths);return o.length!==n.length||o.some((i,c)=>i!==n[c])?null:s}catch(s){if(s.code==="ENOENT")return null;throw s}}function Pl(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 xo(e=process.argv.slice(2)){let t=await xe(),r=mr(e),n=yt(t,r),s=n.config,o=new lt(s.logDir||"logs","invalidate.log.jsonl",s.logLevel??"info"),i=e.includes("--content-sync"),c=await at(t);if(i&&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 a=c?.schemaVersion===2?c.invalidationPaths:s.cloudFront.invalidationPaths.length>0?s.cloudFront.invalidationPaths:["/*"],d=c?.schemaVersion===2?Co(a,s.s3.prefix||""):a;if(c?.schemaVersion===2&&d.some(m=>m==="/*"))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 l=s.cloudFront.distributionId,u=new CloudFrontClient({region:"us-east-1",maxAttempts:5});if(o.mark("invalidate"),c?.schemaVersion!==2){let m=`smartcloud-static-publisher-${Date.now()}`;await we(`CloudFront invalidation for distribution ${l}`,3e5,({httpOptions:w})=>u.send(new CreateInvalidationCommand({DistributionId:l,InvalidationBatch:{CallerReference:m,Paths:{Quantity:d.length,Items:d}}}),w)),o.endMark("invalidate"),await o.flush();return}let g=bl(),f=await Sl(g,l,c.impactHash,d);if(!f){let m=createHash("sha256").update(JSON.stringify(d)).digest("hex");f={schemaVersion:1,distributionId:l,impactHash:c.impactHash,updatedAt:new Date().toISOString(),batches:Pl(d).map((w,p)=>({index:p,callerReference:`wpsuite-content-sync-${c.impactHash.slice(0,24)}-${m.slice(0,24)}-${p}`,paths:w,invalidationId:null,status:"pending"}))},await bn(g,f);}for(let m of f.batches){if(!m.invalidationId){let w=await we(`CloudFront targeted invalidation batch ${m.index+1}`,3e5,({httpOptions:h})=>u.send(new CreateInvalidationCommand({DistributionId:l,InvalidationBatch:{CallerReference:m.callerReference,Paths:{Quantity:m.paths.length,Items:m.paths}}}),h)),p=String(w.Invalidation?.Id||"").trim();if(!p)throw new Error("CloudFront did not return an invalidation ID.");m.invalidationId=p,m.status="created",await bn(g,f);}await waitUntilInvalidationCompleted({client:u,maxWaitTime:900,minDelay:2,maxDelay:15},{DistributionId:l,Id:m.invalidationId}),m.status="completed",await bn(g,f),o.info(`CloudFront invalidation batch ${m.index+1}/${f.batches.length} completed.`,{invalidationId:m.invalidationId,pathCount:m.paths.length});}o.endMark("invalidate"),await o.flush();}function Rr(e){let t=new URL(e);return t.hash="",t.href}function Io(e,t,r){let n=new Set([...t].map(a=>Rr(a))),s=new Map,o=new Map,i=a=>{s.set(Rr(a),a);},c=a=>{if(!a)return;let d=Rr(a);s.has(d)||o.set(d,a);};for(let a of e.deleteUrls)i(a);for(let a of e.resourceDeleteUrls)i(a);for(let a of e.archiveFamilies)if(a.foundPosts===0)for(let d of a.pageUrls)n.has(Rr(d))||i(d);for(let a of e.directRenderUrls)c(a);for(let a of e.resourceRenderUrls)c(a);for(let a of e.archiveFamilies)c(a.pageUrls[0]),c(a.pageUrls.at(-1));for(let a of r)c(a);return {expectedAvailable:[...o.values()],expectedMissing:[...s.values()]}}var Ao=["claimed","impact-planned","rendered","deployed","invalidated","verified","acknowledged"];function Rn(){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 v.resolve(e)}function St(e,t){return Ao.indexOf(e)>=Ao.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 F.mkdir(e,{recursive:true}),await F.appendFile(v.join(e,"audit-events.jsonl"),`${JSON.stringify(o)}
|
|
19
|
+
`,"utf8");}function vl(e){return Pt({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 Cl(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 vn(e,t){if(e.subscriptionType!=="PROFESSIONAL"&&e.subscriptionType!=="AGENCY")throw new Error("Content-sync requires an active Professional or Agency subscription.");let r=vl(t),n=yt(e,t.deploymentProfile),s=n.name||"",o=In(e.sourceOrigin,String(t.id||""),r),i=Tn({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=An({siteId:e.wpsuite?.siteSettings?.siteId,accountId:e.wpsuite?.siteSettings?.accountId,sourceOrigin:e.sourceOrigin,ruleId:String(t.id||""),targetFingerprint:i}),a=`content-sync:${String(t.id||"")}:${De({scopeFingerprint:o,targetFingerprint:i}).slice(0,32)}`,d=new qt({sourceOrigin:e.sourceOrigin,runtimeToken:String(e.wpsuite?.runtimeToken||""),ignoreHttpsErrors:e.ignoreHttpsErrors}),l=await d.releaseFingerprint(r.includeSubsites),u=xn({wordpressFingerprint:l,targetFingerprint:i,sourceOrigin:e.sourceOrigin,sitemapPaths:Ct(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:i,consumerId:c,coalesceKey:a,client:d,releaseFingerprint:u}}async function kl(e){let t=Rn(),r=Wt(t),n=await xe(),s=(n.scheduler?.rules??[]).filter(d=>d.command==="content-sync"&&d.enabled!==false);if(s.length===0)return 0;let o=await Tr(r),i=await Nt(r),c=await Ir(r),a=0;for(let d of s){let l=await vn(n,d),u=c.entries[l.coalesceKey];if(!u||u.releaseJobId!==e||u.scopeFingerprint!==l.scopeFingerprint||u.targetFingerprint!==l.targetFingerprint||u.releaseFingerprint!==l.releaseFingerprint)throw new Error(`The normal release has no matching pre-crawl content-sync cutoff for rule ${String(d.id||"")}.`);let g=De({releaseJobId:e,releaseFingerprint:l.releaseFingerprint,scopeFingerprint:l.scopeFingerprint,targetFingerprint:l.targetFingerprint,sequence:u.sequence}),f=await l.client.establishBaseline({consumerId:l.consumerId,scopeFingerprint:l.scopeFingerprint,baselineId:g,postTypes:l.scope.postTypes,includeSubsites:l.scope.includeSubsites,sequence:u.sequence}),m={schemaVersion:1,ruleId:String(d.id||""),consumerId:l.consumerId,coalesceKey:l.coalesceKey,scopeFingerprint:l.scopeFingerprint,targetFingerprint:l.targetFingerprint,releaseFingerprint:l.releaseFingerprint,baselineId:g,deploymentProfile:l.deploymentProfile,committedSequence:f.committedSequence,releaseJobId:e,verifiedAt:new Date().toISOString()};o.entries[l.coalesceKey]=m;let w=i.rules[l.coalesceKey];i.rules[l.coalesceKey]={ruleId:String(d.id||""),consumerId:l.consumerId,coalesceKey:l.coalesceKey,committedSequence:f.committedSequence,observedHeadSequence:f.headSequence,lag:Math.max(0,f.headSequence-f.committedSequence),lastSuccessfulJobId:w?.lastSuccessfulJobId||e,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:f.headSequence>f.committedSequence,retryAttempt:0,nextRetryAt:null,lastError:null,baselineStatus:"ready",baselineReason:null};let p=await qn(r,l.coalesceKey);await Ke(t,"content-sync-baseline-established",{id:e},"A normal release established a verified content-sync baseline.",{ruleId:d.id,consumerId:l.consumerId,committedSequence:f.committedSequence,supersededCurrent:p}),a++;}return await jn(r,o),await _t(r,i),await F.rm(r.releaseCutoff,{force:true}),a}async function xl(e){let t=Rn(),r=Wt(t),n=await xe(),s=(n.scheduler?.rules??[]).filter(c=>c.command==="content-sync"&&c.enabled!==false);if(s.length===0)return 0;let o=await Ir(r),i=0;for(let c of s){let a=await vn(n,c),d=Object.values(o.entries).find(g=>g.releaseJobId===e&&g.ruleId===String(c.id||""));if(d){if(d.coalesceKey===a.coalesceKey&&d.scopeFingerprint===a.scopeFingerprint&&d.targetFingerprint===a.targetFingerprint&&d.releaseFingerprint===a.releaseFingerprint){i++;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 l=await a.client.unboundHead(a.scope.postTypes,a.scope.includeSubsites),u={schemaVersion:1,releaseJobId:e,ruleId:String(c.id||""),coalesceKey:a.coalesceKey,scopeFingerprint:a.scopeFingerprint,targetFingerprint:a.targetFingerprint,releaseFingerprint:a.releaseFingerprint,sequence:l.headSequence,capturedAt:new Date().toISOString()};o.entries[a.coalesceKey]=u,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:a.consumerId,sequence:l.headSequence}),i++;}return await Mn(r,o),i}async function Il(e,t){let r=JSON.parse(await F.readFile(v.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 Pn(e,t,r=0){let n=e.protocol==="https:"?On:_o,s=e.protocol==="https:"?new On.Agent({rejectUnauthorized:!t}):void 0;return new Promise((o,i)=>{let c=n.get(e,{agent:s,timeout:3e4,headers:{"user-agent":"WP-Suite-Content-Sync/1"}},a=>{let d=a.statusCode??500,l=a.headers.location;if(d>=300&&d<400&&l&&r<5){a.resume(),Pn(new URL(l,e),t,r+1).then(o,i);return}let u=[],g=0;a.on("data",f=>{g+=f.byteLength,g<=1048576&&u.push(f);}),a.on("error",i),a.on("end",()=>o({status:d,body:Buffer.concat(u).toString("utf8"),contentType:String(a.headers["content-type"]||"")}));});c.on("timeout",()=>c.destroy(new Error(`Verification timed out: ${e}`))),c.on("error",i);})}async function Tl(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")),i=new Set;for(let[l,u]of Object.entries(o.pages??{}))i.add(l),typeof u.url=="string"&&i.add(u.url);let c=await at(e);if(c?.schemaVersion!==2||c.impactHash!==r.impactHash)throw new Error("Content-sync verification requires the matching targeted deploy plan.");let a=Io(r,i,c.resolvedSitemapUrls),d=new Set(r.resourceRenderUrls.map(l=>{let u=new URL(l);return u.hash="",u.href}));for(let l of a.expectedAvailable){let u=new URL(l),g=new URL(`${u.pathname}${u.search}`,s),f=await Pn(g,e.ignoreHttpsErrors);if(f.status<200||f.status>=400)throw new Error(`Content-sync verification failed (${f.status}): ${g}`);let m=new URL(l);if(m.hash="",d.has(m.href)){if(!f.contentType.toLowerCase().startsWith("application/json"))throw new Error(`Resource verification expected application/json: ${g}`);try{JSON.parse(f.body);}catch{throw new Error(`Resource verification received invalid JSON: ${g}`)}}}for(let l of a.expectedMissing){let u=new URL(l),g=new URL(`${u.pathname}${u.search}`,s),f=await Pn(g,e.ignoreHttpsErrors);if(f.status>=200&&f.status<300)throw new Error(`Deleted content is still publicly available: ${g}`)}}async function Al(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 El(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 nt(e,t,r,n,s={},o=0,i=async()=>{}){t.phase=r,await Fn(e,t),await Ln(e,{schemaVersion:1,jobId:t.jobId,phase:r,completedItems:n,totalItems:n,attempt:o,updatedAt:new Date().toISOString(),details:s}),await Ke(v.dirname(e.state),"content-sync-checkpoint",{id:t.jobId},`Content-sync checkpoint completed: ${r}.`,{phase:r,completedItems:n,totalItems:n,attempt:o,...s}),await i(r,t);}async function $l(e,t={}){let r={loadConfig:xe,resolveRuleContext:vn,runCrawl:fo,runDeploy:vo,runInvalidate:xo,verifyDeployment:Tl,promoteCandidateManifest:Al,clearDeployPlan:Je,afterCheckpoint:async()=>{},...t},n=Rn(),s=Wt(n),o=await Il(n,e),i=await r.loadConfig(),c=Cl(i,String(o.ruleId)),a=await r.resolveRuleContext(i,c);if(o.coalesceKey&&o.coalesceKey!==a.coalesceKey)throw new Error("Queued content-sync demand no longer matches the active rule scope.");let l=(await Tr(s)).entries[a.coalesceKey];if(!l||l.scopeFingerprint!==a.scopeFingerprint||l.targetFingerprint!==a.targetFingerprint||l.releaseFingerprint!==a.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:l?"mismatch":"missing"}),new Error("Content-sync baseline is missing or stale. Run a successful normal publish first.");let u=await Ar(s),g=await a.client.head({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:l.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites});if(u&&u.jobId!==o.id)throw new Error("Another immutable content-sync range is still active.");if(u||(u={schemaVersion:1,jobId:o.id,ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,scopeFingerprint:a.scopeFingerprint,targetFingerprint:a.targetFingerprint,baselineId:l.baselineId,fromSequence:g.committedSequence,toSequence:g.headSequence,phase:"claimed",impactHash:null,startedAt:new Date().toISOString(),updatedAt:new Date().toISOString()},await nt(s,u,"claimed",0,{fromSequence:u.fromSequence,toSequence:u.toSequence},0,r.afterCheckpoint),await Ke(n,"content-sync-range-claimed",o,"Content-sync claimed a closed journal range.",{fromSequence:u.fromSequence,toSequence:u.toSequence})),u.fromSequence===u.toSequence){let h=await Nt(s);h.rules[a.coalesceKey]={ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,committedSequence:u.toSequence,observedHeadSequence:g.headSequence,lag:Math.max(0,g.headSequence-u.toSequence),lastSuccessfulJobId:o.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:false,retryAttempt:0,nextRetryAt:null,lastError:null},await _t(s,h),await Er(s);return}let f=await Dn(s);if(!St(u.phase,"impact-planned")){let h=await a.client.events({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:l.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites,afterSequence:u.fromSequence,throughSequence:u.toSequence}),y=xr({events:h,scope:a.scope,sourceOrigin:i.sourceOrigin}),b=await a.client.resolveImpactFamilies(y,a.scope.includeSubsites);f=$n({jobId:o.id,ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,scopeFingerprint:a.scopeFingerprint,targetFingerprint:a.targetFingerprint,baselineId:l.baselineId,sourceOrigin:i.sourceOrigin,scope:a.scope,fromSequence:u.fromSequence,toSequence:u.toSequence,events:h,resolvedArchiveFamilies:b,sitemapUrls:Ct(i.sitemapPaths)}),await Un(s,f),u.impactHash=f.impactHash,await nt(s,u,"impact-planned",h.length,{eventCount:h.length,foldedCount:f.foldedCount,impactHash:f.impactHash},0,r.afterCheckpoint),await Ke(n,"content-sync-impact-planned",o,"Content-sync persisted an immutable impact plan.",{impactHash:f.impactHash,eventCount:h.length});}if(!f||f.jobId!==o.id||u.impactHash!==f.impactHash)throw new Error("The immutable content-sync impact plan is missing or mismatched.");if(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH=s.candidateManifest,!St(u.phase,"rendered")){let h=await El(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:f.impactHash}),await r.runCrawl(["--content-sync-plan",s.impact]),await nt(s,u,"rendered",f.directRenderUrls.length+f.archiveFamilyUrls.length+f.resourceRenderUrls.length,{impactHash:f.impactHash},o.attempt??0,r.afterCheckpoint);}let m=a.deploymentProfile?["--profile",a.deploymentProfile]:[];if(St(u.phase,"deployed")||(await r.runDeploy([...m,"--content-sync"]),await nt(s,u,"deployed",1,{impactHash:f.impactHash},0,r.afterCheckpoint),await Ke(n,"content-sync-deployed",o,"Content-sync targeted deployment completed.",{impactHash:f.impactHash})),St(u.phase,"invalidated")?St(u.phase,"verified")||await r.runInvalidate([...m,"--content-sync"]):(await r.runInvalidate([...m,"--content-sync"]),await nt(s,u,"invalidated",1,{impactHash:f.impactHash},0,r.afterCheckpoint)),St(u.phase,"verified")||(await r.verifyDeployment(i,a,f,s.candidateManifest),await r.promoteCandidateManifest(s),await nt(s,u,"verified",1,{impactHash:f.impactHash},0,r.afterCheckpoint)),g=await a.client.head({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:l.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites}),g.committedSequence===u.fromSequence)await a.client.acknowledge({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:l.baselineId,expectedSequence:u.fromSequence,sequence:u.toSequence,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites});else if(g.committedSequence!==u.toSequence)throw new Error("Content-sync acknowledgement cursor is in an unexpected state.");await nt(s,u,"acknowledged",1,{sequence:u.toSequence},0,r.afterCheckpoint),await Ke(n,"content-sync-cursor-acknowledged",o,"Content-sync committed its journal cursor after verification.",{sequence:u.toSequence});let w=await a.client.head({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:l.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites}),p=await Nt(s);p.rules[a.coalesceKey]={ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,committedSequence:u.toSequence,observedHeadSequence:w.headSequence,lag:Math.max(0,w.headSequence-u.toSequence),lastSuccessfulJobId:o.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:w.headSequence>u.toSequence,retryAttempt:0,nextRetryAt:null,lastError:null},await _t(s,p),await r.clearDeployPlan(i),await F.rm(v.join(n,"content-sync-invalidation.json"),{force:true}),await F.rm(s.candidateManifest,{force:true}),await Er(s);}async function Eo(e=process.argv.slice(2)){let r=e.indexOf("--capture-baseline-cutoffs");if(r>=0){let i=String(e[r+1]||"").trim();if(!i)throw new Error("--capture-baseline-cutoffs requires a release job ID.");await xl(i);return}let n=e.indexOf("--establish-baselines");if(n>=0){let i=String(e[n+1]||"").trim();if(!i)throw new Error("--establish-baselines requires a release job ID.");await kl(i);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 $l(o);}Eo().catch(e=>{console.error(e instanceof Error?e.message:String(e)),process.exit(1);});
|