@smart-cloud/publisher-exporter 1.1.83 → 1.1.84
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 +58 -0
- package/dist/content-sync.js +15 -15
- package/dist/crawl.js +13 -13
- package/dist/deploy.js +6 -6
- package/dist/remote-worker.js +5 -5
- package/dist/rewrite-worker.js +2 -2
- package/package.json +3 -4
package/dist/content-sync.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import U,{readFile}from'fs/promises';import si from'http';import Vn from'https';import C from'path';import ms,{createHash}from'crypto';import {fromIni,fromTemporaryCredentials}from'@aws-sdk/credential-providers';import {request,chromium}from'playwright';import {gunzipSync}from'zlib';import {setTimeout as setTimeout$1}from'timers/promises';import Oi from'fast-glob';import Ws 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 {Upload}from'@aws-sdk/lib-storage';import {CloudFrontClient,waitUntilInvalidationCompleted,CreateInvalidationCommand}from'@aws-sdk/client-cloudfront';var Yo=new Set(["post-type","taxonomy","author","date","posts-page","listing"]);function Qe(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function Xo(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))]}function ei(e){return new URL(e).pathname.split("/").filter(Boolean).some(r=>{try{return decodeURIComponent(r).endsWith("__trashed")}catch{return r.endsWith("__trashed")}})}function Dr(e){return Array.isArray(e)?e.map(Dr):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[r])=>t.localeCompare(r)).map(([t,r])=>[t,Dr(r)])):e}function ti(e){return JSON.stringify(Dr(e))}function We(e){return createHash("sha256").update(ti(e)).digest("hex")}function Wn(e){return We({schemaVersion:1,wordpressFingerprint:e.wordpressFingerprint,targetFingerprint:e.targetFingerprint,sourceOrigin:e.sourceOrigin,sitemapPaths:[...e.sitemapPaths].sort(),blockedPathPrefixes:[...e.blockedPathPrefixes].sort(),assetPathPrefixes:[...e.assetPathPrefixes].sort(),noJavaScriptRenderPathPrefixes:[...e.noJavaScriptRenderPathPrefixes].sort(),urlRewriteMode:e.urlRewriteMode})}function Ur(e){let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")throw new Error(`Unsupported source origin protocol: ${t.protocol}`);return t.hash="",t.search="",t.pathname=t.pathname.replace(/\/+$/,""),t.toString().replace(/\/$/,"")}function xe(e,t){ri(t);let r=new URL(Ur(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 ri(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 kt(e){let t=Qe((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=Qe((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 qn(e,t,r){return We({schemaVersion:1,sourceOrigin:Ur(e),ruleId:t.trim(),scope:kt(r)})}function _n(e){return We({schemaVersion:1,...e})}function Nn(e){let t=String(e.siteId??"").trim()||String(e.accountId??"").trim()||Ur(e.sourceOrigin);return `content-sync:${We({siteIdentity:t,ruleId:e.ruleId.trim(),targetFingerprint:e.targetFingerprint}).slice(0,48)}`}function Bn(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 Ln(e){return e?.status==="publish"&&!!e.url}function ni(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 Lr(e){let t=kt(e.scope),r=new Map;Bn(e.events);for(let n of e.events)for(let s of [n.before,n.after])for(let o of s?.archiveFamilies??[]){if(!Yo.has(o.kind)||!ni(o.kind,t))continue;let i={...o,url:xe(e.sourceOrigin,o.url)};r.set(`${i.kind}|${i.url}`,i);}for(let n of t.listingPaths){let s=xe(e.sourceOrigin,n);r.set(`listing|${s}`,{kind:"listing",url:s});}return [...r.values()].sort((n,s)=>n.url.localeCompare(s.url))}function Hn(e){let t=kt(e.scope),r=Bn(e.events),n=new Set,s=new Set,o=new Map,i=new Map(Lr({events:e.events,scope:t,sourceOrigin:e.sourceOrigin}).map(f=>[`${f.kind}|${f.url}`,f]));for(let f of r){let w=f.before,m=f.after,h=Ln(w),y=Ln(m);y&&m.url&&n.add(xe(e.sourceOrigin,m.url)),h&&w.url&&(!y||w.url!==m.url)&&s.add(xe(e.sourceOrigin,w.url));}for(let f of [...e.events].sort((w,m)=>w.sequence-m.sequence)){for(let w of [f.before,f.after])for(let m of w?.resourceDeleteUrls??[])o.set(xe(e.sourceOrigin,m),"delete");for(let w of f.after?.resourceRenderUrls??[])o.set(xe(e.sourceOrigin,w),"render");}let c=[...o].filter(([,f])=>f==="render").map(([f])=>f),a=[...o].filter(([,f])=>f==="delete").map(([f])=>f),d=new Map(e.resolvedArchiveFamilies.map(f=>[`${f.kind}|${xe(e.sourceOrigin,f.url)}`,{...f,url:xe(e.sourceOrigin,f.url),pageUrls:Qe(f.pageUrls.map(w=>xe(e.sourceOrigin,w)))}])),u=[...i.entries()].map(([f,w])=>{let m=d.get(f);if(!m)throw new Error(`WordPress did not resolve archive family ${f}.`);return {...w,...m}}).sort((f,w)=>f.url.localeCompare(w.url)),l=Qe(u.flatMap(f=>f.pageUrls)),g=t.includeSitemapChain?Xo(e.sitemapUrls.map(f=>xe(e.sourceOrigin,f))):[],p={schemaVersion:1,generatedAt:new Date().toISOString(),jobId:e.jobId,ruleId:e.ruleId,consumerId:e.consumerId,coalesceKey:e.coalesceKey,scopeFingerprint:e.scopeFingerprint,targetFingerprint:e.targetFingerprint,baselineId:e.baselineId,fromSequence:e.fromSequence,toSequence:e.toSequence,eventCount:e.events.length,foldedCount:r.length,directRenderUrls:Qe([...n]),archiveFamilyUrls:l,sitemapUrls:g,deleteUrls:Qe([...s]),resourceRenderUrls:Qe(c),resourceDeleteUrls:Qe(a),archiveFamilies:u};return {...p,impactHash:We(p)}}function xt(e){let{impactHash:t,...r}=e;if(e.schemaVersion!==1||!t)throw new Error("Invalid content-sync impact plan schema.");if(!Array.isArray(e.resourceRenderUrls)||!Array.isArray(e.resourceDeleteUrls))throw new Error("Invalid content-sync resource impact contract.");if(We(r)!==t)throw new Error("Content-sync impact plan hash mismatch.");for(let n of [...e.directRenderUrls,...e.deleteUrls,...e.resourceRenderUrls,...e.resourceDeleteUrls])if(ei(n))throw new Error(`Content-sync impact contains a WordPress trash alias instead of the last public URL: ${n}`)}var oi="wp-json/smartcloud-static-publisher/v1/content-sync/",ii=25*1024*1024,Vt=class extends Error{status;code;constructor(t,r,n){super(n),this.name="ContentSyncApiError",this.status=t,this.code=r;}},Kt=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"?oi:`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:"?Vn:si,c=s.protocol==="https:"?new Vn.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((a,d)=>{let u=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},l=>{let g=[],p=0;l.on("data",f=>{if(p+=f.byteLength,p>ii){l.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(f);}),l.on("error",d),l.on("end",()=>{let f=Buffer.concat(g).toString("utf8"),w;try{w=f?JSON.parse(f):{};}catch{d(new Vt(l.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let m=l.statusCode??500;if(m<200||m>=300){d(new Vt(m,String(w.code??"request-failed"),String(w.message??`Content-sync API request failed (${m}).`)));return}a(w);});});u.on("timeout",()=>u.destroy(new Error(`Content-sync API request timed out: ${t}`))),u.on("error",d),u.end(o);})}async head(t){return this.post("head",t)}async unboundHead(t,r){return this.post("head",{postTypes:t,includeSubsites:r})}async events(t){let r=[],n=t.afterSequence,s=new Set(t.postTypes);for(;n<t.throughSequence;){let o=await this.post("events",{...t,afterSequence:n,limit:250});if(o.fromSequence!==n||o.throughSequence!==t.throughSequence||o.nextSequence<n||o.nextSequence>t.throughSequence)throw new Error("Content-sync API returned an inconsistent event page.");if(!Array.isArray(o.items))throw new Error("Content-sync API returned a malformed event page.");let 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 zt(e){let t=C.resolve(e);return {state:C.join(t,"content-sync-state.json"),current:C.join(t,"content-sync-current.json"),impact:C.join(t,"content-sync-impact-plan.json"),checkpoint:C.join(t,"content-sync-checkpoint.json"),baseline:C.join(t,"content-sync-baseline.json"),activeRules:C.join(t,"content-sync-active-rules.json"),releaseCutoff:C.join(t,"content-sync-release-cutoff.json"),candidateManifest:C.join(t,"content-sync-candidate-manifest.json"),trustedManifest:C.join(t,"crawl-manifest.json"),invalidation:C.join(t,"content-sync-invalidation.json"),deployPlan:C.join(t,"deploy-plan.json")}}async function Wr(e){let t=await It(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 Kn(e,t){await lt(e.releaseCutoff,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function lt(e,t){await U.mkdir(C.dirname(e),{recursive:true});let r=`${e}.${process.pid}.${Date.now()}.tmp`;await U.writeFile(r,JSON.stringify(t,null,2),"utf8"),await U.rename(r,e);}async function It(e){try{return JSON.parse(await U.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 qr(e){let t=await It(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 zn(e,t){await lt(e.baseline,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function _r(e){let t=await It(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 Jn(e,t){await lt(e.current,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Qn(e){let t=await It(e.impact);if(t===null)return null;let r=t;return xt(r),r}async function Gn(e,t){xt(t),await lt(e.impact,t);}async function Zn(e,t){await lt(e.checkpoint,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Jt(e){let t=await It(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 Qt(e,t){await lt(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Nr(e){await Promise.all([e.current,e.checkpoint].map(async t=>{try{await U.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}));}async function Yn(e,t){let r=await _r(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 U.unlink(n);}catch(s){if(s.code!=="ENOENT")throw s}})),true)}var ci=/^[A-Za-z0-9][A-Za-z0-9_.@+=,-]{0,127}$/;function ut(e,t="awsProfile"){let r=String(e??"").trim();if(!r)return "";if(!ci.test(r))throw new Error(`${t} must start with a letter or number and contain only letters, numbers, dot, dash, underscore, comma, plus, equals, or at-sign characters.`);return r}function li(e){return !!(String(e.AWS_ACCESS_KEY_ID??"").trim()&&String(e.AWS_SECRET_ACCESS_KEY??"").trim())}function dt(e,t=process.env){let r=ut(e);if(!(!r||li(t)))return fromIni({profile:r})}var Br=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function Tt(e){let t=new Set;for(let r of [...e,...Br]){let n=String(r||"").trim();n&&t.add(n);}return [...t]}function Hr(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function Xn(e){if(!e||!/^https?:\/\//i.test(e))return false;try{return Hr(new URL(e))}catch{return false}}function ts(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function mi(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&&C.extname(o)===""&&(o+="/"),o==="/"?"":o}function Vr(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,r])=>[t.trim(),String(r??"")]).filter(([t])=>t.length>0))}function gi(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.awsProfile=="string"){let a=ut(o.awsProfile,`deploymentProfiles.${s}.awsProfile`);a&&(i.awsProfile=a);}if(typeof o.targetOrigin=="string"){let a=ts(o.targetOrigin);a&&(i.targetOrigin=a);}let c=Vr(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(u=>String(u??"").trim()).filter(u=>u.length>0)),Object.keys(a).length>0&&(i.cloudFront=a);}t[s]=i;}return t}function zr(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function fi(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(u=>String(u??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],d=Array.isArray(r.listingPaths)?[...new Set(r.listingPaths.map(u=>String(u??"").trim()).filter(Boolean))]:[];return n==="content-sync"&&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 rs(e){let t=e&&typeof e=="object"?e:{},r=Array.isArray(t.rules)?t.rules.map((n,s)=>fi(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 hi(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,u)=>({enabled:typeof d.enabled=="boolean"?d.enabled:r.enabled===true,concurrency:Math.min(100,Math.max(1,Number(d.concurrency||u.concurrency))),batchSize:Math.min(500,Math.max(1,Number(d.batchSize||u.batchSize))),maxAttempts:Math.min(5,Math.max(1,Number(d.maxAttempts||2)))});return {render:{enabled:r.enabled===true,...Number.isInteger(n)&&n>0?{concurrency:n}:{},batchSize:Number.isInteger(s)&&s>0?Math.min(20,s):5,...Number.isInteger(o)&&o>0?{maxAttempts:Math.min(5,o)}:{}},rewrite:a(i,{concurrency:8,batchSize:25}),deploy:a(c,{concurrency:8,batchSize:50})}}function wi(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 Jr(e){let t=e&&typeof e=="object"?e:{},r=String(t.accountId??"").trim(),n=String(t.siteId??"").trim(),s=t.subscriber===true,o=wi(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 u=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();u&&(c.virtualAssetBaseUrl=u),Object.keys(i).length>0&&(c.siteSettings=i);let l=zr(t.subscriptionType);return l&&(c.subscriptionType=l),c}function yi(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function ns(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function bi(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 ss(e,t,r=5){let n=new URL(ns(e)),s=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),o=n.protocol==="http:"?si:Vn,i=bi(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}:{}},u=>{let l=[];u.on("data",g=>{l.push(Buffer.isBuffer(g)?g:Buffer.from(g));}),u.on("error",a),u.on("end",()=>{let g=u.statusCode??0,p=u.headers.location;if(r>0&&p&&[301,302,303,307,308].includes(g)){c(ss(new URL(p,n),{method:g===303?"GET":s},r-1));return}let f=new Headers;for(let[w,m]of Object.entries(u.headers))Array.isArray(m)?f.set(w,m.join(", ")):typeof m=="string"&&f.set(w,m);c(new Response(Buffer.concat(l),{status:g,statusText:u.statusMessage??"",headers:f}));});});d.on("error",a),d.end();})}function Si(e){let t=Jr(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;yi(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 Pi(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!Si(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 u=ns(a);if(u.startsWith(t)){let l=(()=>{try{return new URL(u).pathname}catch{return u}})();try{let g=e.ignoreHttpsErrors&&u.startsWith("https://")?await ss(a,d):await r(a,d);return n.push({path:l,status:g.status,ok:g.ok,error:null}),g}catch(g){throw n.push({path:l,status:null,ok:!1,error:g instanceof Error?g.message:String(g)}),g}}return r(a,d)}));let{getConfig:s}=await import('@smart-cloud/wpsuite-core'),o=await s("publisher"),i=o&&typeof o=="object"?o:null,c=zr(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 Ri(e,t){let r=gi(t?.deploymentProfiles),n=String(t?.defaultDeploymentProfile??"").trim(),s=r[n]?n:"",o=zr(t?.subscriptionType),i=Jr(e.wpsuite);return {...e,scheduler:rs(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 Kr(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 vi(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?C.resolve(e):""}function es(e,t,r){let n=String(t||"").trim();return n&&C.isAbsolute(n)?C.resolve(n):C.resolve(e,Kr(n,r))}async function Ie(e,t={}){let r=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",n=await U.readFile(r,"utf8"),s=JSON.parse(n);s.sourceOrigin=s.sourceOrigin.replace(/\/$/,""),s.targetOrigin=ts(s.targetOrigin),s.awsProfile=ut(s.awsProfile),s.ignoreHttpsErrors??=false,s.outputDir=String(s.outputDir||"export").trim()||"export",s.urlRewriteMode||=s.targetOrigin==="."?"relative":"absolute",s.noJavaScriptRenderPathPrefixes||=[],s.seedPaths||=[],s.generated404RequestPath=mi(s.generated404RequestPath),s.sitemapPaths||=[...Br],s.allowedAssetHosts||=[],s.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],s.blockedPathPrefixes||=[],s.blockedSearchFragments||=[],s.pageCache={purgeBeforeCrawl:s.pageCache?.purgeBeforeCrawl===true};let o=Number(s.processingConcurrency)>0?Math.min(100,Math.max(1,Number(s.processingConcurrency))):null;s.concurrency=o??s.concurrency??1,s.maxPages||=0,s.extraReplacements=Vr(s.extraReplacements),s.postCrawlCopyMap=Vr(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=Jr(s.wpsuite),s.scheduler=rs(void 0),s.remoteWorkers=hi(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 Pi(s);t.onRemotePublisherConfigDiagnostic?.(i.diagnostic);let c=Ri(s,i.remote),a=vi(),d=a?C.resolve(a,".."):"";return d?(c.outputDir=es(d,c.outputDir,"export"),c.logDir=es(d,c.logDir,"logs")):(C.isAbsolute(c.outputDir)||(c.outputDir=Kr(c.outputDir,"export")),C.isAbsolute(c.logDir)||(c.logDir=Kr(c.logDir,"logs"))),c}async function os(e){await e.addInitScript(()=>{let t=window,r=[],n=new WeakSet,s=new WeakSet;Object.defineProperty(t,"__WPSUITE_STATIC_SERIALIZATION_STATE__",{value:{snapshots:r,runtimeCreatedElements:s},configurable:true});let o=Document.prototype.createElement;Document.prototype.createElement=function(g,p){let f=o.call(this,g,p);return s.add(f),f};let i=l=>{!(l instanceof Element)||n.has(l)||(n.add(l),r.push({host:l,pristine:l.cloneNode(true),sourceOwned:!s.has(l)}));},c=Element.prototype.attachShadow;Element.prototype.attachShadow=function(g){return i(this),c.call(this,g)};let a=new WeakSet,d=l=>{if(typeof l!="object"&&typeof l!="function"||l===null||a.has(l))return l;let g=l;if(typeof g.createRoot!="function")return l;let p=g.createRoot,f=function(w,...m){return i(w),Reflect.apply(p,this,[w,...m])};try{Object.defineProperty(g,"createRoot",{...Object.getOwnPropertyDescriptor(g,"createRoot"),value:f}),a.add(l);}catch{}return l},u=l=>{let g=d(t[l]);try{Object.defineProperty(t,l,{configurable:!0,enumerable:!0,get:()=>g,set:p=>{g=d(p);}});}catch{d(t[l]);}};u("ReactDOM"),u("ReactDOMClient");});}async function Qr(e){return e.evaluate(()=>{let t=window.__WPSUITE_STATIC_SERIALIZATION_STATE__,r=/^data-(?:[a-z0-9]+-)*(?:bound|initialized|mounted|hydrated)$/i;document.querySelectorAll("*").forEach(s=>{for(let o of [...s.attributes])r.test(o.name)&&s.removeAttribute(o.name);});for(let s of t?.snapshots??[])s.host.isConnected&&(s.sourceOwned?s.host.replaceWith(s.pristine.cloneNode(true)):s.host.remove());return document.querySelectorAll("*").forEach(s=>{t?.runtimeCreatedElements.has(s)&&s.remove();}),document.querySelectorAll("#wpsuite-consent-root, .grecaptcha-badge, iframe[src*='/recaptcha/']").forEach(s=>s.remove()),document.documentElement.classList.remove("wpsuite-consent-scroll-locked"),document.body.classList.remove("wpsuite-consent-scroll-locked"),`${document.doctype?new XMLSerializer().serializeToString(document.doctype):""}${document.documentElement.outerHTML}`})}var Ci=".deploy-plan.json",ki="deploy-plan.json",xi=".crawl-incomplete.json",Ii="crawl-incomplete.json";function is(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?C.resolve(e):""}function as(e){let t=String(e||"").replace(/\\/g,"/").replace(/^\/+/,"").trim(),r=C.posix.normalize(t);return !t||!r||r==="."||/^[a-zA-Z]:/.test(r)||r.startsWith("../")||r===".."?null:r}function Ti(e){let t=String(e||"").trim();if(!t.startsWith("/")||t.startsWith("//")||t.includes("*")||t.includes("#"))return null;try{let r=new URL(t,"https://content-sync.invalid");return r.origin!=="https://content-sync.invalid"?null:`${C.posix.normalize(r.pathname)}${r.search}`}catch{return null}}function Te(e){return [...new Set(e.map(as).filter(t=>t!==null))]}function cs(e,t){let r={generatedAt:String(t.generatedAt||new Date().toISOString()),outputDir:C.resolve(e.outputDir),runMode:t.runMode,crawlMode:t.crawlMode,fullSyncRequired:!!t.fullSyncRequired,changedFiles:Te(t.changedFiles||[]),deletedFiles:Te(t.deletedFiles||[]),rewriteTargets:Te(t.rewriteTargets||[]),revalidateFiles:Te(t.revalidateFiles||[])};return t.schemaVersion===2?{...r,schemaVersion:2,runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,consumerId:String(t.consumerId||"").trim(),fromSequence:Math.max(0,Number(t.fromSequence)||0),toSequence:Math.max(0,Number(t.toSequence)||0),impactHash:String(t.impactHash||"").trim(),resolvedSitemapUrls:[...new Set((t.resolvedSitemapUrls||[]).map(n=>String(n||"").trim()).filter(n=>{try{let s=new URL(n);return s.protocol==="http:"||s.protocol==="https:"}catch{return false}}))],uploadGroups:{assets:Te(t.uploadGroups?.assets||[]),content:Te(t.uploadGroups?.content||[]),listings:Te(t.uploadGroups?.listings||[]),resources:Te(t.uploadGroups?.resources||[]),sitemapLeaves:Te(t.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:Te(t.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((t.invalidationPaths||[]).map(Ti).filter(n=>n!==null))].sort()}:{schemaVersion:1,...r,runMode:t.runMode}}function Ai(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 Gr(e){let t=C.join(C.resolve(e.outputDir),Ci),r=is();return r?[C.join(r,ki),t]:[t]}function Zr(e){let t=C.join(C.resolve(e.outputDir),xi),r=is();return r?[C.join(r,Ii),t]:[t]}function ge(e,t){let r=C.resolve(e),n=C.resolve(t),s=C.relative(r,n).replace(/\\/g,"/");return as(s)}async function pt(e){let t=C.resolve(e.outputDir);for(let r of Gr(e))try{let n=await U.readFile(r,"utf8"),s=JSON.parse(n);if(!Ai(s)||C.resolve(s.outputDir)!==t)continue;return cs(e,s)}catch{}return null}async function Yr(e,t){let r=cs(e,t);for(let n of Gr(e)){await U.mkdir(C.dirname(n),{recursive:true});let s=`${n}.${process.pid}.${Date.now()}.tmp`;await U.writeFile(s,JSON.stringify(r,null,2),"utf8"),await U.rename(s,n);}}async function Ze(e){for(let t of Gr(e))try{await U.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}async function Xr(e,t){let r={schemaVersion:1,startedAt:String(t.startedAt||new Date().toISOString()),outputDir:C.resolve(e.outputDir)};for(let n of Zr(e)){await U.mkdir(C.dirname(n),{recursive:true});let s=`${n}.${process.pid}.tmp`;await U.writeFile(s,JSON.stringify(r,null,2),"utf8"),await U.rename(s,n);}}async function ls(e){let t=C.resolve(e.outputDir);for(let r of Zr(e)){let n;try{n=await U.readFile(r,"utf8");}catch(o){if(o.code==="ENOENT")continue;throw o}let s;try{s=JSON.parse(n);}catch{throw new Error(`Invalid incomplete crawl marker: ${r}`)}if(s.schemaVersion!==1||typeof s.startedAt!="string"||typeof s.outputDir!="string"||C.resolve(s.outputDir)!==t)throw new Error(`Invalid incomplete crawl marker: ${r}`);return {schemaVersion:1,startedAt:s.startedAt,outputDir:t}}return null}async function us(e){for(let t of Zr(e))try{await U.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}function Ee(e,t=10){return ms.createHash("sha1").update(e).digest("hex").slice(0,t)}async function Ei(e){await U.mkdir(C.dirname(e),{recursive:true});}async function _e(e,t,r){await Ei(e);let n=C.join(C.dirname(e),`.${C.basename(e)}.${process.pid}.${Date.now()}.${ms.randomBytes(6).toString("hex")}.tmp`);try{typeof t=="string"?await U.writeFile(n,t,r??"utf8"):await U.writeFile(n,t),await U.rename(n,e);}catch(s){throw await U.unlink(n).catch(()=>{}),s}}function $e(e){return [".html",".htm",".css",".js",".mjs",".json",".xml",".xsl",".txt",".svg",".map",".enc",".jws"].includes(C.extname(e).toLowerCase())}function Ne(e){return e.replace(/"/g,'"').replace(/"/g,'"').replace(/'/g,"'").replace(/'/g,"'").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function mt(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">")}function Gt(e){return e.replace(/\//g,"\\/")}function ds(e){return e.replace(/\//g,"\\\\/")}function $i(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 ps(e,t,r){if(!t)return;e[t]=r;let n=Gt(t),s=Gt(r);e[n]=s;let o=ds(t),i=ds(r);e[o]=i;let c=mt(t),a=mt(r);e[c]=a;let d=mt(n),u=mt(s);e[d]=u;let l=mt(o),g=mt(i);e[l]=g;}function Et(e,t,r){ps(e,t,r);let n=$i(t);n&&n!==t&&ps(e,n,r);}function en(e,t){let r=t.startsWith("/")?t:`/${t}`,n=(e||"").replace(/\/$/,"");return !n||n==="."||n==="/"?r:`${n}${r}`}function gs(e,t){try{return new URL(e,t==="."?"https://relative.invalid":t).pathname}catch{return e.startsWith("/")?e:`/${e.replace(/^\.\//,"")}`}}function fs(e,t,r){if(!t)return r;let n=C.dirname(C.resolve(t)),s=C.resolve(e,r.replace(/^\/+/,"")),o=C.relative(n,s).replace(/\\/g,"/");return o?(o.startsWith(".")||(o=`./${o}`),o):"."}async function nn(e,t){return (t?[...new Set(t)].map(n=>C.resolve(n)).filter(n=>n.startsWith(C.resolve(e.outputDir))).map(n=>C.relative(e.outputDir,n).replace(/\\/g,"/")).filter(n=>n.length>0):await Oi(["**/*"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(n=>n!=="asset-map.json"&&$e(n))}var Di=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],Ui=new Set([".html",".htm"]),ws="WPSuite.io Static Publisher",Li=ws.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function hs(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function Wi(e){return e.startsWith("//")||e.startsWith("\\/\\/")||e.startsWith("\\\\/\\\\/")}function qi(e){let t=e.replace(/\\+\//g,"/");return t.startsWith("/")&&!t.startsWith("//")}function _i(e,t,r){return qi(t)?e.replace(new RegExp(`(?<![A-Za-z0-9._~%\\\\-])${hs(t)}`,"g"),r):Wi(t)?e.replace(new RegExp(`(?<!:)${hs(t)}`,"g"),r):e.split(t).join(r)}function Ni(e,t){let r="\uE000WPSUITE_STATIC_REWRITE_";for(;e.includes(r)||t.some(([o,i])=>o.includes(r)||i.includes(r));)r+="_";let n=e,s=[];for(let[o,[i,c]]of t.entries()){let a=`${r}${o.toString(36)}\uE001`,d=_i(n,i,a);d!==n&&(s.push([a,c]),n=d);}for(let[o,i]of s)n=n.split(o).join(i);return n}function Bi(e,t,r){if(e.wpsuite?.siteSettings?.subscriber===true||e.wpsuite?.subscriptionType==="PROFESSIONAL"||e.wpsuite?.subscriptionType==="AGENCY"||!r)return false;let s=C.extname(r).toLowerCase();return Ui.has(s)?/<head\b|<html\b|<!doctype html/i.test(t):false}function Hi(e){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${Li}\\2)[^>]*\\/?>`,"i").test(e))return e;let t=`<meta name="generator" content="${ws}" />`;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 U,{readFile}from'fs/promises';import li from'http';import Qn from'https';import C from'path';import ws,{createHash}from'crypto';import {fromIni,fromTemporaryCredentials}from'@aws-sdk/credential-providers';import {request,chromium}from'playwright';import {gunzipSync}from'zlib';import {setTimeout as setTimeout$1}from'timers/promises';import Hs 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 {Upload}from'@aws-sdk/lib-storage';import {CloudFrontClient,waitUntilInvalidationCompleted,CreateInvalidationCommand}from'@aws-sdk/client-cloudfront';var ni=new Set(["post-type","taxonomy","author","date","posts-page","listing"]);function Qe(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function si(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))]}function oi(e){return new URL(e).pathname.split("/").filter(Boolean).some(r=>{try{return decodeURIComponent(r).endsWith("__trashed")}catch{return r.endsWith("__trashed")}})}function Wr(e){return Array.isArray(e)?e.map(Wr):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[r])=>t.localeCompare(r)).map(([t,r])=>[t,Wr(r)])):e}function ii(e){return JSON.stringify(Wr(e))}function We(e){return createHash("sha256").update(ii(e)).digest("hex")}function Bn(e){return We({schemaVersion:1,wordpressFingerprint:e.wordpressFingerprint,targetFingerprint:e.targetFingerprint,sourceOrigin:e.sourceOrigin,sitemapPaths:[...e.sitemapPaths].sort(),blockedPathPrefixes:[...e.blockedPathPrefixes].sort(),assetPathPrefixes:[...e.assetPathPrefixes].sort(),noJavaScriptRenderPathPrefixes:[...e.noJavaScriptRenderPathPrefixes].sort(),urlRewriteMode:e.urlRewriteMode})}function qr(e){let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")throw new Error(`Unsupported source origin protocol: ${t.protocol}`);return t.hash="",t.search="",t.pathname=t.pathname.replace(/\/+$/,""),t.toString().replace(/\/$/,"")}function xe(e,t){ai(t);let r=new URL(qr(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 ai(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 xt(e){let t=Qe((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=Qe((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 Hn(e,t,r){return We({schemaVersion:1,sourceOrigin:qr(e),ruleId:t.trim(),scope:xt(r)})}function Vn(e){return We({schemaVersion:1,...e})}function Kn(e){let t=String(e.siteId??"").trim()||String(e.accountId??"").trim()||qr(e.sourceOrigin);return `content-sync:${We({siteIdentity:t,ruleId:e.ruleId.trim(),targetFingerprint:e.targetFingerprint}).slice(0,48)}`}function zn(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 Nn(e){return e?.status==="publish"&&!!e.url}function ci(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 _r(e){let t=xt(e.scope),r=new Map;zn(e.events);for(let n of e.events)for(let s of [n.before,n.after])for(let o of s?.archiveFamilies??[]){if(!ni.has(o.kind)||!ci(o.kind,t))continue;let i={...o,url:xe(e.sourceOrigin,o.url)};r.set(`${i.kind}|${i.url}`,i);}for(let n of t.listingPaths){let s=xe(e.sourceOrigin,n);r.set(`listing|${s}`,{kind:"listing",url:s});}return [...r.values()].sort((n,s)=>n.url.localeCompare(s.url))}function Jn(e){let t=xt(e.scope),r=zn(e.events),n=new Set,s=new Set,o=new Map,i=new Map(_r({events:e.events,scope:t,sourceOrigin:e.sourceOrigin}).map(f=>[`${f.kind}|${f.url}`,f]));for(let f of r){let w=f.before,m=f.after,h=Nn(w),y=Nn(m);y&&m.url&&n.add(xe(e.sourceOrigin,m.url)),h&&w.url&&(!y||w.url!==m.url)&&s.add(xe(e.sourceOrigin,w.url));}for(let f of [...e.events].sort((w,m)=>w.sequence-m.sequence)){for(let w of [f.before,f.after])for(let m of w?.resourceDeleteUrls??[])o.set(xe(e.sourceOrigin,m),"delete");for(let w of f.after?.resourceRenderUrls??[])o.set(xe(e.sourceOrigin,w),"render");}let c=[...o].filter(([,f])=>f==="render").map(([f])=>f),a=[...o].filter(([,f])=>f==="delete").map(([f])=>f),d=new Map(e.resolvedArchiveFamilies.map(f=>[`${f.kind}|${xe(e.sourceOrigin,f.url)}`,{...f,url:xe(e.sourceOrigin,f.url),pageUrls:Qe(f.pageUrls.map(w=>xe(e.sourceOrigin,w)))}])),u=[...i.entries()].map(([f,w])=>{let m=d.get(f);if(!m)throw new Error(`WordPress did not resolve archive family ${f}.`);return {...w,...m}}).sort((f,w)=>f.url.localeCompare(w.url)),l=Qe(u.flatMap(f=>f.pageUrls)),g=t.includeSitemapChain?si(e.sitemapUrls.map(f=>xe(e.sourceOrigin,f))):[],p={schemaVersion:1,generatedAt:new Date().toISOString(),jobId:e.jobId,ruleId:e.ruleId,consumerId:e.consumerId,coalesceKey:e.coalesceKey,scopeFingerprint:e.scopeFingerprint,targetFingerprint:e.targetFingerprint,baselineId:e.baselineId,fromSequence:e.fromSequence,toSequence:e.toSequence,eventCount:e.events.length,foldedCount:r.length,directRenderUrls:Qe([...n]),archiveFamilyUrls:l,sitemapUrls:g,deleteUrls:Qe([...s]),resourceRenderUrls:Qe(c),resourceDeleteUrls:Qe(a),archiveFamilies:u};return {...p,impactHash:We(p)}}function It(e){let{impactHash:t,...r}=e;if(e.schemaVersion!==1||!t)throw new Error("Invalid content-sync impact plan schema.");if(!Array.isArray(e.resourceRenderUrls)||!Array.isArray(e.resourceDeleteUrls))throw new Error("Invalid content-sync resource impact contract.");if(We(r)!==t)throw new Error("Content-sync impact plan hash mismatch.");for(let n of [...e.directRenderUrls,...e.deleteUrls,...e.resourceRenderUrls,...e.resourceDeleteUrls])if(oi(n))throw new Error(`Content-sync impact contains a WordPress trash alias instead of the last public URL: ${n}`)}var ui="wp-json/smartcloud-static-publisher/v1/content-sync/",di=25*1024*1024,Kt=class extends Error{status;code;constructor(t,r,n){super(n),this.name="ContentSyncApiError",this.status=t,this.code=r;}},zt=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"?ui:`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:"?Qn:li,c=s.protocol==="https:"?new Qn.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((a,d)=>{let u=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},l=>{let g=[],p=0;l.on("data",f=>{if(p+=f.byteLength,p>di){l.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(f);}),l.on("error",d),l.on("end",()=>{let f=Buffer.concat(g).toString("utf8"),w;try{w=f?JSON.parse(f):{};}catch{d(new Kt(l.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let m=l.statusCode??500;if(m<200||m>=300){d(new Kt(m,String(w.code??"request-failed"),String(w.message??`Content-sync API request failed (${m}).`)));return}a(w);});});u.on("timeout",()=>u.destroy(new Error(`Content-sync API request timed out: ${t}`))),u.on("error",d),u.end(o);})}async head(t){return this.post("head",t)}async unboundHead(t,r){return this.post("head",{postTypes:t,includeSubsites:r})}async events(t){let r=[],n=t.afterSequence,s=new Set(t.postTypes);for(;n<t.throughSequence;){let o=await this.post("events",{...t,afterSequence:n,limit:250});if(o.fromSequence!==n||o.throughSequence!==t.throughSequence||o.nextSequence<n||o.nextSequence>t.throughSequence)throw new Error("Content-sync API returned an inconsistent event page.");if(!Array.isArray(o.items))throw new Error("Content-sync API returned a malformed event page.");let 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 Jt(e){let t=C.resolve(e);return {state:C.join(t,"content-sync-state.json"),current:C.join(t,"content-sync-current.json"),impact:C.join(t,"content-sync-impact-plan.json"),checkpoint:C.join(t,"content-sync-checkpoint.json"),baseline:C.join(t,"content-sync-baseline.json"),activeRules:C.join(t,"content-sync-active-rules.json"),releaseCutoff:C.join(t,"content-sync-release-cutoff.json"),candidateManifest:C.join(t,"content-sync-candidate-manifest.json"),trustedManifest:C.join(t,"crawl-manifest.json"),invalidation:C.join(t,"content-sync-invalidation.json"),deployPlan:C.join(t,"deploy-plan.json")}}async function Nr(e){let t=await Tt(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 Gn(e,t){await lt(e.releaseCutoff,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function lt(e,t){await U.mkdir(C.dirname(e),{recursive:true});let r=`${e}.${process.pid}.${Date.now()}.tmp`;await U.writeFile(r,JSON.stringify(t,null,2),"utf8"),await U.rename(r,e);}async function Tt(e){try{return JSON.parse(await U.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 Br(e){let t=await Tt(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 Zn(e,t){await lt(e.baseline,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Hr(e){let t=await Tt(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 Yn(e,t){await lt(e.current,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Xn(e){let t=await Tt(e.impact);if(t===null)return null;let r=t;return It(r),r}async function es(e,t){It(t),await lt(e.impact,t);}async function ts(e,t){await lt(e.checkpoint,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Qt(e){let t=await Tt(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 Gt(e,t){await lt(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Vr(e){await Promise.all([e.current,e.checkpoint].map(async t=>{try{await U.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}));}async function rs(e,t){let r=await Hr(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 U.unlink(n);}catch(s){if(s.code!=="ENOENT")throw s}})),true)}var mi=/^[A-Za-z0-9][A-Za-z0-9_.@+=,-]{0,127}$/;function ut(e,t="awsProfile"){let r=String(e??"").trim();if(!r)return "";if(!mi.test(r))throw new Error(`${t} must start with a letter or number and contain only letters, numbers, dot, dash, underscore, comma, plus, equals, or at-sign characters.`);return r}function gi(e){return !!(String(e.AWS_ACCESS_KEY_ID??"").trim()&&String(e.AWS_SECRET_ACCESS_KEY??"").trim())}function dt(e,t=process.env){let r=ut(e);if(!(!r||gi(t)))return fromIni({profile:r})}var Kr=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function At(e){let t=new Set;for(let r of [...e,...Kr]){let n=String(r||"").trim();n&&t.add(n);}return [...t]}function zr(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function ns(e){if(!e||!/^https?:\/\//i.test(e))return false;try{return zr(new URL(e))}catch{return false}}function os(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function yi(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&&C.extname(o)===""&&(o+="/"),o==="/"?"":o}function Jr(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,r])=>[t.trim(),String(r??"")]).filter(([t])=>t.length>0))}function bi(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.awsProfile=="string"){let a=ut(o.awsProfile,`deploymentProfiles.${s}.awsProfile`);a&&(i.awsProfile=a);}if(typeof o.targetOrigin=="string"){let a=os(o.targetOrigin);a&&(i.targetOrigin=a);}let c=Jr(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(u=>String(u??"").trim()).filter(u=>u.length>0)),Object.keys(a).length>0&&(i.cloudFront=a);}t[s]=i;}return t}function Gr(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function Si(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(u=>String(u??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],d=Array.isArray(r.listingPaths)?[...new Set(r.listingPaths.map(u=>String(u??"").trim()).filter(Boolean))]:[];return n==="content-sync"&&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 is(e){let t=e&&typeof e=="object"?e:{},r=Array.isArray(t.rules)?t.rules.map((n,s)=>Si(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 Pi(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,u)=>({enabled:typeof d.enabled=="boolean"?d.enabled:r.enabled===true,concurrency:Math.min(100,Math.max(1,Number(d.concurrency||u.concurrency))),batchSize:Math.min(500,Math.max(1,Number(d.batchSize||u.batchSize))),maxAttempts:Math.min(5,Math.max(1,Number(d.maxAttempts||2)))});return {render:{enabled:r.enabled===true,...Number.isInteger(n)&&n>0?{concurrency:n}:{},batchSize:Number.isInteger(s)&&s>0?Math.min(20,s):5,...Number.isInteger(o)&&o>0?{maxAttempts:Math.min(5,o)}:{}},rewrite:a(i,{concurrency:8,batchSize:25}),deploy:a(c,{concurrency:8,batchSize:50})}}function Ri(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 Zr(e){let t=e&&typeof e=="object"?e:{},r=String(t.accountId??"").trim(),n=String(t.siteId??"").trim(),s=t.subscriber===true,o=Ri(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 u=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();u&&(c.virtualAssetBaseUrl=u),Object.keys(i).length>0&&(c.siteSettings=i);let l=Gr(t.subscriptionType);return l&&(c.subscriptionType=l),c}function vi(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function as(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function Ci(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 cs(e,t,r=5){let n=new URL(as(e)),s=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),o=n.protocol==="http:"?li:Qn,i=Ci(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}:{}},u=>{let l=[];u.on("data",g=>{l.push(Buffer.isBuffer(g)?g:Buffer.from(g));}),u.on("error",a),u.on("end",()=>{let g=u.statusCode??0,p=u.headers.location;if(r>0&&p&&[301,302,303,307,308].includes(g)){c(cs(new URL(p,n),{method:g===303?"GET":s},r-1));return}let f=new Headers;for(let[w,m]of Object.entries(u.headers))Array.isArray(m)?f.set(w,m.join(", ")):typeof m=="string"&&f.set(w,m);c(new Response(Buffer.concat(l),{status:g,statusText:u.statusMessage??"",headers:f}));});});d.on("error",a),d.end();})}function ki(e){let t=Zr(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;vi(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 xi(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!ki(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 u=as(a);if(u.startsWith(t)){let l=(()=>{try{return new URL(u).pathname}catch{return u}})();try{let g=e.ignoreHttpsErrors&&u.startsWith("https://")?await cs(a,d):await r(a,d);return n.push({path:l,status:g.status,ok:g.ok,error:null}),g}catch(g){throw n.push({path:l,status:null,ok:!1,error:g instanceof Error?g.message:String(g)}),g}}return r(a,d)}));let{getConfig:s}=await import('@smart-cloud/wpsuite-core'),o=await s("publisher"),i=o&&typeof o=="object"?o:null,c=Gr(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 Ii(e,t){let r=bi(t?.deploymentProfiles),n=String(t?.defaultDeploymentProfile??"").trim(),s=r[n]?n:"",o=Gr(t?.subscriptionType),i=Zr(e.wpsuite);return {...e,scheduler:is(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 Qr(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 Ti(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?C.resolve(e):""}function ss(e,t,r){let n=String(t||"").trim();return n&&C.isAbsolute(n)?C.resolve(n):C.resolve(e,Qr(n,r))}async function Ie(e,t={}){let r=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",n=await U.readFile(r,"utf8"),s=JSON.parse(n);s.sourceOrigin=s.sourceOrigin.replace(/\/$/,""),s.targetOrigin=os(s.targetOrigin),s.awsProfile=ut(s.awsProfile),s.ignoreHttpsErrors??=false,s.outputDir=String(s.outputDir||"export").trim()||"export",s.urlRewriteMode||=s.targetOrigin==="."?"relative":"absolute",s.noJavaScriptRenderPathPrefixes||=[],s.seedPaths||=[],s.generated404RequestPath=yi(s.generated404RequestPath),s.sitemapPaths||=[...Kr],s.allowedAssetHosts||=[],s.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],s.blockedPathPrefixes||=[],s.blockedSearchFragments||=[],s.pageCache={purgeBeforeCrawl:s.pageCache?.purgeBeforeCrawl===true};let o=Number(s.processingConcurrency)>0?Math.min(100,Math.max(1,Number(s.processingConcurrency))):null;s.concurrency=o??s.concurrency??1,s.maxPages||=0,s.extraReplacements=Jr(s.extraReplacements),s.postCrawlCopyMap=Jr(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=Zr(s.wpsuite),s.scheduler=is(void 0),s.remoteWorkers=Pi(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 xi(s);t.onRemotePublisherConfigDiagnostic?.(i.diagnostic);let c=Ii(s,i.remote),a=Ti(),d=a?C.resolve(a,".."):"";return d?(c.outputDir=ss(d,c.outputDir,"export"),c.logDir=ss(d,c.logDir,"logs")):(C.isAbsolute(c.outputDir)||(c.outputDir=Qr(c.outputDir,"export")),C.isAbsolute(c.logDir)||(c.logDir=Qr(c.logDir,"logs"))),c}async function ls(e){await e.addInitScript(()=>{let t=window,r=[],n=new WeakSet,s=new WeakSet;Object.defineProperty(t,"__WPSUITE_STATIC_SERIALIZATION_STATE__",{value:{snapshots:r,runtimeCreatedElements:s},configurable:true});let o=Document.prototype.createElement;Document.prototype.createElement=function(g,p){let f=o.call(this,g,p);return s.add(f),f};let i=l=>{!(l instanceof Element)||n.has(l)||(n.add(l),r.push({host:l,pristine:l.cloneNode(true),sourceOwned:!s.has(l)}));},c=Element.prototype.attachShadow;Element.prototype.attachShadow=function(g){return i(this),c.call(this,g)};let a=new WeakSet,d=l=>{if(typeof l!="object"&&typeof l!="function"||l===null||a.has(l))return l;let g=l;if(typeof g.createRoot!="function")return l;let p=g.createRoot,f=function(w,...m){return i(w),Reflect.apply(p,this,[w,...m])};try{Object.defineProperty(g,"createRoot",{...Object.getOwnPropertyDescriptor(g,"createRoot"),value:f}),a.add(l);}catch{}return l},u=l=>{let g=d(t[l]);try{Object.defineProperty(t,l,{configurable:!0,enumerable:!0,get:()=>g,set:p=>{g=d(p);}});}catch{d(t[l]);}};u("ReactDOM"),u("ReactDOMClient");});}async function Yr(e){return e.evaluate(()=>{let t=window.__WPSUITE_STATIC_SERIALIZATION_STATE__,r=/^data-(?:[a-z0-9]+-)*(?:bound|initialized|mounted|hydrated)$/i;document.querySelectorAll("*").forEach(s=>{for(let o of [...s.attributes])r.test(o.name)&&s.removeAttribute(o.name);});for(let s of t?.snapshots??[])s.host.isConnected&&(s.sourceOwned?s.host.replaceWith(s.pristine.cloneNode(true)):s.host.remove());return document.querySelectorAll("*").forEach(s=>{t?.runtimeCreatedElements.has(s)&&s.remove();}),document.querySelectorAll("#wpsuite-consent-root, .grecaptcha-badge, iframe[src*='/recaptcha/']").forEach(s=>s.remove()),document.documentElement.classList.remove("wpsuite-consent-scroll-locked"),document.body.classList.remove("wpsuite-consent-scroll-locked"),`${document.doctype?new XMLSerializer().serializeToString(document.doctype):""}${document.documentElement.outerHTML}`})}function Xr(e){let t=e.code;return t==="ENOENT"||t==="ENOTDIR"||t==="ELOOP"}async function pt(e){let t=C.resolve(e),r;try{r=await U.realpath(t);}catch(o){if(Xr(o))return [];throw o}let n=[{directory:t,ancestors:new Set([r])}],s=[];for(;n.length;){let{directory:o,ancestors:i}=n.pop(),c;try{c=await U.readdir(o,{withFileTypes:!0});}catch(a){if(Xr(a))continue;throw a}for(let a of c){let d=C.join(o,a.name);try{let u=a.isSymbolicLink()?await U.stat(d):a;if(u.isFile())s.push(C.relative(t,d).split(C.sep).join("/"));else if(u.isDirectory()){let l=await U.realpath(d);if(i.has(l))continue;n.push({directory:d,ancestors:new Set([...i,l])});}}catch(u){if(!Xr(u))throw u}}}return s.sort()}var Ai=".deploy-plan.json",Ei="deploy-plan.json",$i=".crawl-incomplete.json",Oi="crawl-incomplete.json";function us(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?C.resolve(e):""}function ds(e){let t=String(e||"").replace(/\\/g,"/").replace(/^\/+/,"").trim(),r=C.posix.normalize(t);return !t||!r||r==="."||/^[a-zA-Z]:/.test(r)||r.startsWith("../")||r===".."?null:r}function Mi(e){let t=String(e||"").trim();if(!t.startsWith("/")||t.startsWith("//")||t.includes("*")||t.includes("#"))return null;try{let r=new URL(t,"https://content-sync.invalid");return r.origin!=="https://content-sync.invalid"?null:`${C.posix.normalize(r.pathname)}${r.search}`}catch{return null}}function Te(e){return [...new Set(e.map(ds).filter(t=>t!==null))]}function ps(e,t){let r={generatedAt:String(t.generatedAt||new Date().toISOString()),outputDir:C.resolve(e.outputDir),runMode:t.runMode,crawlMode:t.crawlMode,fullSyncRequired:!!t.fullSyncRequired,changedFiles:Te(t.changedFiles||[]),deletedFiles:Te(t.deletedFiles||[]),rewriteTargets:Te(t.rewriteTargets||[]),revalidateFiles:Te(t.revalidateFiles||[])};return t.schemaVersion===2?{...r,schemaVersion:2,runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,consumerId:String(t.consumerId||"").trim(),fromSequence:Math.max(0,Number(t.fromSequence)||0),toSequence:Math.max(0,Number(t.toSequence)||0),impactHash:String(t.impactHash||"").trim(),resolvedSitemapUrls:[...new Set((t.resolvedSitemapUrls||[]).map(n=>String(n||"").trim()).filter(n=>{try{let s=new URL(n);return s.protocol==="http:"||s.protocol==="https:"}catch{return false}}))],uploadGroups:{assets:Te(t.uploadGroups?.assets||[]),content:Te(t.uploadGroups?.content||[]),listings:Te(t.uploadGroups?.listings||[]),resources:Te(t.uploadGroups?.resources||[]),sitemapLeaves:Te(t.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:Te(t.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((t.invalidationPaths||[]).map(Mi).filter(n=>n!==null))].sort()}:{schemaVersion:1,...r,runMode:t.runMode}}function ji(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 en(e){let t=C.join(C.resolve(e.outputDir),Ai),r=us();return r?[C.join(r,Ei),t]:[t]}function tn(e){let t=C.join(C.resolve(e.outputDir),$i),r=us();return r?[C.join(r,Oi),t]:[t]}function ge(e,t){let r=C.resolve(e),n=C.resolve(t),s=C.relative(r,n).replace(/\\/g,"/");return ds(s)}async function mt(e){let t=C.resolve(e.outputDir);for(let r of en(e))try{let n=await U.readFile(r,"utf8"),s=JSON.parse(n);if(!ji(s)||C.resolve(s.outputDir)!==t)continue;return ps(e,s)}catch{}return null}async function rn(e,t){let r=ps(e,t);for(let n of en(e)){await U.mkdir(C.dirname(n),{recursive:true});let s=`${n}.${process.pid}.${Date.now()}.tmp`;await U.writeFile(s,JSON.stringify(r,null,2),"utf8"),await U.rename(s,n);}}async function Ze(e){for(let t of en(e))try{await U.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}async function nn(e,t){let r={schemaVersion:1,startedAt:String(t.startedAt||new Date().toISOString()),outputDir:C.resolve(e.outputDir)};for(let n of tn(e)){await U.mkdir(C.dirname(n),{recursive:true});let s=`${n}.${process.pid}.tmp`;await U.writeFile(s,JSON.stringify(r,null,2),"utf8"),await U.rename(s,n);}}async function ms(e){let t=C.resolve(e.outputDir);for(let r of tn(e)){let n;try{n=await U.readFile(r,"utf8");}catch(o){if(o.code==="ENOENT")continue;throw o}let s;try{s=JSON.parse(n);}catch{throw new Error(`Invalid incomplete crawl marker: ${r}`)}if(s.schemaVersion!==1||typeof s.startedAt!="string"||typeof s.outputDir!="string"||C.resolve(s.outputDir)!==t)throw new Error(`Invalid incomplete crawl marker: ${r}`);return {schemaVersion:1,startedAt:s.startedAt,outputDir:t}}return null}async function gs(e){for(let t of tn(e))try{await U.unlink(t);}catch(r){if(r.code!=="ENOENT")throw r}}function Ee(e,t=10){return ws.createHash("sha1").update(e).digest("hex").slice(0,t)}async function Fi(e){await U.mkdir(C.dirname(e),{recursive:true});}async function _e(e,t,r){await Fi(e);let n=C.join(C.dirname(e),`.${C.basename(e)}.${process.pid}.${Date.now()}.${ws.randomBytes(6).toString("hex")}.tmp`);try{typeof t=="string"?await U.writeFile(n,t,r??"utf8"):await U.writeFile(n,t),await U.rename(n,e);}catch(s){throw await U.unlink(n).catch(()=>{}),s}}function $e(e){return [".html",".htm",".css",".js",".mjs",".json",".xml",".xsl",".txt",".svg",".map",".enc",".jws"].includes(C.extname(e).toLowerCase())}function Ne(e){return e.replace(/"/g,'"').replace(/"/g,'"').replace(/'/g,"'").replace(/'/g,"'").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}function gt(e){return e.replace(/&/g,"&").replace(/"/g,""").replace(/'/g,"'").replace(/</g,"<").replace(/>/g,">")}function Xt(e){return e.replace(/\//g,"\\/")}function fs(e){return e.replace(/\//g,"\\\\/")}function Di(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 hs(e,t,r){if(!t)return;e[t]=r;let n=Xt(t),s=Xt(r);e[n]=s;let o=fs(t),i=fs(r);e[o]=i;let c=gt(t),a=gt(r);e[c]=a;let d=gt(n),u=gt(s);e[d]=u;let l=gt(o),g=gt(i);e[l]=g;}function $t(e,t,r){hs(e,t,r);let n=Di(t);n&&n!==t&&hs(e,n,r);}function sn(e,t){let r=t.startsWith("/")?t:`/${t}`,n=(e||"").replace(/\/$/,"");return !n||n==="."||n==="/"?r:`${n}${r}`}function ys(e,t){try{return new URL(e,t==="."?"https://relative.invalid":t).pathname}catch{return e.startsWith("/")?e:`/${e.replace(/^\.\//,"")}`}}function bs(e,t,r){if(!t)return r;let n=C.dirname(C.resolve(t)),s=C.resolve(e,r.replace(/^\/+/,"")),o=C.relative(n,s).replace(/\\/g,"/");return o?(o.startsWith(".")||(o=`./${o}`),o):"."}async function cn(e,t){return (t?[...new Set(t)].map(n=>C.resolve(n)).filter(n=>n.startsWith(C.resolve(e.outputDir))).map(n=>C.relative(e.outputDir,n).replace(/\\/g,"/")).filter(n=>n.length>0):await pt(e.outputDir)).filter(n=>n!=="asset-map.json"&&$e(n))}var qi=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],_i=new Set([".html",".htm"]),Ps="WPSuite.io Static Publisher",Ni=Ps.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function Ss(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function Bi(e){return e.startsWith("//")||e.startsWith("\\/\\/")||e.startsWith("\\\\/\\\\/")}function Hi(e){let t=e.replace(/\\+\//g,"/");return t.startsWith("/")&&!t.startsWith("//")}function Vi(e,t,r){return Hi(t)?e.replace(new RegExp(`(?<![A-Za-z0-9._~%\\\\-])${Ss(t)}`,"g"),r):Bi(t)?e.replace(new RegExp(`(?<!:)${Ss(t)}`,"g"),r):e.split(t).join(r)}function Ki(e,t){let r="\uE000WPSUITE_STATIC_REWRITE_";for(;e.includes(r)||t.some(([o,i])=>o.includes(r)||i.includes(r));)r+="_";let n=e,s=[];for(let[o,[i,c]]of t.entries()){let a=`${r}${o.toString(36)}\uE001`,d=Vi(n,i,a);d!==n&&(s.push([a,c]),n=d);}for(let[o,i]of s)n=n.split(o).join(i);return n}function zi(e,t,r){if(e.wpsuite?.siteSettings?.subscriber===true||e.wpsuite?.subscriptionType==="PROFESSIONAL"||e.wpsuite?.subscriptionType==="AGENCY"||!r)return false;let s=C.extname(r).toLowerCase();return _i.has(s)?/<head\b|<html\b|<!doctype html/i.test(t):false}function Ji(e){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${Ni}\\2)[^>]*\\/?>`,"i").test(e))return e;let t=`<meta name="generator" content="${Ps}" />`;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 Qi(e){let t=Ne(e).trim();return t.startsWith("{")||t.startsWith("[")||t.includes("\\/")||/"@context"|"@type"/.test(t)}function Gi(e,t){let r=[...new Set(t)].filter(o=>o.includes("/")&&!/\\+\//.test(o)).map(o=>[o,Xt(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)=>Qi(a)?`${i}${n(a)}${d}`:o),s}function Rs(e,t){if(!t)return null;let r=C.dirname(C.resolve(t)),n=C.resolve(e.outputDir),s=C.relative(r,n).replace(/\\/g,"/");return s?(s.startsWith(".")||(s=`./${s}`),s.endsWith("/")?s:`${s}/`):"./"}function Zi(e,t,r){let n=Rs(t,r);if(!n)return e;let s=e;for(let o of qi){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,"\\/"),u=n.replace(/\//g,"\\/"),l=d.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),g=new RegExp(`(?:\\.{1,}\\\\/)+${l}`,"g"),p=new RegExp(`(?:\\.{1,}(?:\\\\/|\\\\))+${l}`,"g"),f=new RegExp(`(?<![\\.\\\\])\\/${l}`,"g");s=s.replace(g,`${u}${d}`).replace(p,`${u}${d}`).replace(f,`${u}${d}`);}return s}function an(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:ys(t,e.targetOrigin),o=n?`${n.search}${n.hash}`:"";return e.urlRewriteMode==="root-relative"?`${s}${o}`:`${bs(e.outputDir,r,s)}${o}`}function on(e,t,r,n,s){$t(e,r,an(t,n,s));}function Yi(e,t,r,n,s){if(!r||r===n)return;$t(e,r,n);let o=an(t,r,s),i=an(t,n,s);$t(e,o,i);}function ln(e,t,r,n,s={}){let o=e,i={},c=t.sourceOrigin.replace(/\/+$/,"");if(t.urlRewriteMode!=="absolute"){let d=t.urlRewriteMode==="root-relative"?"/":Rs(t,n)||"/";$t(i,`${c}/`,d);}on(i,t,c,t.targetOrigin,n);for(let[d,u]of Object.entries(t.extraReplacements))on(i,t,d,u,n);for(let[d,u]of Object.entries(r))on(i,t,d,u,n);for(let[d,u]of Object.entries(s)){let l=r[d];!l||l===u||Yi(i,t,u,l,n);}let a=Object.entries(i).sort((d,u)=>u[0].length-d[0].length);return o=Ki(o,a),t.urlRewriteMode!=="absolute"&&(o=Zi(o,t,n)),o=Gi(o,a.map(([,d])=>d)),zi(t,o,n)&&(o=Ji(o)),o}async function Xi(e,t,r,n){let s=C.join(e.outputDir,n),o=C.extname(s).toLowerCase(),i;try{i=await U.readFile(s,"utf8");}catch(d){if(d.code!=="ENOENT")throw d;return {changed:false}}let a=ln(i,e,t,o===".js"||o===".mjs"?void 0:s,r);return a!==i?(await _e(s,a,"utf8"),{changed:true}):{changed:false}}async function ea(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 er(e,t,r={}){let n=await cn(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 l of n){let g=await Xi(e,t,r.previousAssetMap??{},l);g.changed&&(s+=1),a+=1,await r.onProgress?.({index:a,totalFiles:n.length,changedTextFiles:s,file:l,changed:g.changed});}return s}let u=Array.from({length:i},async()=>{let l=new Worker(new URL("./rewrite-worker.js",import.meta.url),{workerData:{config:e,assetMap:t,previousAssetMap:r.previousAssetMap??{}}});try{for(;!d;){let g=c;if(g>=n.length)return;c+=1;let p=n[g],f=await ea(l,p);f.changed&&(s+=1),a+=1,await r.onProgress?.({index:a,totalFiles:n.length,changedTextFiles:s,file:p,changed:f.changed});}}catch(g){throw d=g instanceof Error?g:new Error(String(g)),d}finally{await l.terminate().catch(()=>{});}});return await Promise.all(u),s}var Me={error:0,warn:1,info:2,debug:3},ta=/^(?:authorization|proxyAuthorization|runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken|xStaticPublisherToken|xApiKey)$/i;function rt(e){return e.replace(/((?:x-static-publisher-token|authorization|proxy-authorization|x-api-key|aws-access-key-id|aws-secret-access-key|aws-session-token)\s*:\s*)[^\r\n]+/gi,"$1[REDACTED]").replace(/\b(Basic|Bearer)\s+[^\s]+/gi,"$1 [REDACTED]").replace(/([?&](?:token|access_token|refresh_token|api_key|key|nonce)=)[^&\s]+/gi,"$1[REDACTED]").replace(/((?:"|')?(?:runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken)(?:"|')?\s*[:=]\s*(?:"|'))[^"']+/gi,"$1[REDACTED]")}function rr(e){return typeof e=="string"?rt(e):Array.isArray(e)?e.map(t=>rr(t)):e instanceof Error?{name:e.name,message:rt(e.message),...e.stack?{stack:rt(e.stack)}:{}}:e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([t,r])=>[t,ta.test(t)?"[REDACTED]":rr(r)])):e}function sr(e){return e?rr(e):void 0}var vs=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",tr=vs?C.join(vs,"current-progress.json"):"",Cs=Promise.resolve();function ks(e){return e.includes("crawl")?"crawl":e.includes("deploy")?"deploy":e.includes("invalidate")?"invalidate":e}function ra(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 xs(e,t,r){tr&&(Cs=Cs.then(async()=>{let n=new Date,s=n.toISOString();await U.mkdir(C.dirname(tr),{recursive:true});let o=await U.readFile(tr,"utf8").then(h=>JSON.parse(h)).catch(()=>null),i=o&&typeof o.details=="object"&&o.details?o.details:{},c=ks(e),a=o?.currentStep||ks(o?.source||""),d=o?.startedAt||s,u={...o?.stepDurationsSec??{}},l=o?.stepStartedAt||s;if(a&&a!==c&&o?.stepStartedAt){let h=Math.max(0,Math.round((n.getTime()-new Date(o.stepStartedAt).getTime())/1e3));u[a]=(u[a]??0)+h,l=s;}let g=Math.max(0,Math.round((n.getTime()-new Date(l).getTime())/1e3)),p=Math.max(0,Math.round((n.getTime()-new Date(d).getTime())/1e3)),f=rt(t),w=sr(r)??{},m={checkedAt:s,source:e,message:f,details:ra(i,w),startedAt:d,currentStep:c,stepStartedAt:l,stepElapsedSec:g,totalElapsedSec:p,stepDurationsSec:u};await U.writeFile(tr,JSON.stringify(m,null,2),"utf8");}).catch(()=>{}));}async function Is(e,t){await U.mkdir(C.dirname(e),{recursive:true}),await U.appendFile(e,`${JSON.stringify(rr(t))}
|
|
5
|
+
`,"utf8");}function na(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 sa(e,t,r){let n=sr(r);return {checkedAt:new Date().toISOString(),currentStep:na(e,n),level:e,message:rt(t),details:n??{}}}var ft=class{constructor(t,r,n="info"){this.logDir=t;this.logFile=r;this.level=n;this.ensureLogFileReady();}logDir;logFile;level;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;get logPath(){return C.join(this.logDir,this.logFile)}artifactPath(t){return this.logFile.endsWith(".log.jsonl")?C.join(this.logDir,this.logFile.replace(/\.log\.jsonl$/u,`${t}.jsonl`)):C.join(this.logDir,`${this.logFile}${t}.jsonl`)}get errorsPath(){return this.artifactPath(".errors")}get rejectedPath(){return this.artifactPath(".rejected")}ensureLogFileReady(){return this.initPromise||(this.initPromise=(async()=>{await U.mkdir(this.logDir,{recursive:true}),await U.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 U.appendFile(this.logPath,`${t}
|
|
6
|
+
`,"utf8");});}enqueueJsonLine(t,r){this.enqueueTask(()=>Is(t,r));}accepts(t){return Me[t]<=Me[this.level]}push(t,r,n){if(!this.accepts(t))return;let s=rt(r),o=sr(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){xs(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}},nr=class{constructor(t,r="info"){this.logDir=t;this.level=typeof r=="boolean"?r?"debug":"info":r,this.ensureLogFileReady();}logDir;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;level;get logPath(){return C.join(this.logDir,"crawl.log.jsonl")}get currentEventPath(){return C.join(this.logDir,"current-crawl-event.json")}get rejectedPath(){return C.join(this.logDir,"rejected.jsonl")}get ignoredPath(){return C.join(this.logDir,"ignored.jsonl")}get skippedPath(){return C.join(this.logDir,"skipped-http.jsonl")}get errorsPath(){return C.join(this.logDir,"errors.jsonl")}get timingsPath(){return C.join(this.logDir,"timings.jsonl")}ensureLogFileReady(){return this.initPromise||(this.initPromise=(async()=>{await U.mkdir(this.logDir,{recursive:true}),await U.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 U.appendFile(this.logPath,`${t}
|
|
7
|
+
`,"utf8");});}enqueueJsonLine(t,r){this.enqueueTask(()=>Is(t,r));}updateCurrentEvent(t,r,n){let s=sa(t,r,n);this.enqueueTask(async()=>{await U.mkdir(this.logDir,{recursive:true}),await U.writeFile(this.currentEventPath,JSON.stringify(s,null,2),"utf8");});}push(t,r,n){let s=rt(r),o=sr(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||""):(Me[this.level]>=Me.debug||["summary","page","sitemap","asset","timing"].includes(t))&&console.log(s);}info(t,r){this.push("info",t,r);}debug(t,r){Me[this.level]<Me.debug||this.push("debug",t,r);}progress(t,r){xs("crawl.log.jsonl",t,r),this.updateCurrentEvent("progress",t,r);}checkpoint(t,r){this.updateCurrentEvent("checkpoint",t,r);}page(t,r){this.push("page",t,r);}sitemap(t,r){this.push("sitemap",t,r);}asset(t,r){this.push("asset",t,r);}warn(t,r){this.push("warn",t,r);}error(t,r){this.enqueueJsonLine(this.errorsPath,{message:t,...r||{}}),this.push("error",t,r);}summary(t,r){this.push("summary",t,r);}mark(t){this.marks.set(t,Date.now());}endMark(t,r){let n=this.marks.get(t);if(!n)return;let s=Date.now()-n,o={name:t,ms:s,seconds:Number((s/1e3).toFixed(2)),...r||{}};this.enqueueJsonLine(this.timingsPath,o),this.push("timing",`Timing ${t}: ${o.seconds}s`,o);}reject(t,r,n,s){let o={kind:t,url:r,reason:n,source:s};this.enqueueJsonLine(this.rejectedPath,o),Me[this.level]>=Me.debug&&this.push("reject",`Rejected ${t} ${r}: ${n}`,o);}ignore(t,r,n,s){let o={kind:t,url:r,reason:n,source:s};this.enqueueJsonLine(this.ignoredPath,o),Me[this.level]>=Me.debug&&this.push("ignore",`Ignored ${t} ${r}: ${n}`,o);}skip(t,r,n,s){let o={kind:t,url:r,status:n,source:s};this.enqueueJsonLine(this.skippedPath,o),this.push("warn",`Skipped ${t} ${r}: HTTP ${n}`,o);}async flush(){let t=Date.now()-this.startedAt,r={name:"total",ms:t,seconds:Number((t/1e3).toFixed(2))};if(this.enqueueJsonLine(this.timingsPath,r),await this.writeQueue,this.writeError)throw this.writeError}};var Ts="(?: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 Es(e){let t=new RegExp(`(?:https?:)?//[^\\s'"<>\\);]+\\.${Ts}(?:[?#][^\\s'"<>\\);]*)?(?=[\\s'"<>\\);]|$)|(?<![A-Za-z0-9._-])/[^\\s'"<>\\);]+\\.${Ts}(?:[?#][^\\s'"<>\\);]*)?(?=[\\s'"<>\\);]|$)`,"gi");return [...e.matchAll(t)].map(r=>r[0])}function As(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 $s(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]&&As(o[1])&&r.push({index:o.index??0,value:o[1]});for(let o of e.matchAll(s))o[1]&&As(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 Os(e){let t=new Set;for(let r of e.matchAll(/<script\b([^>]*?)>([\s\S]*?)<\/script\s*>/gi)){let n=new Map;for(let i of (r[1]??"").matchAll(/([^\s=/>]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+)))?/g))n.set(i[1].toLowerCase(),i[2]??i[3]??i[4]??"");if(!n.has("data-static-export-assets"))continue;if(n.get("type")?.trim().toLowerCase()!=="application/json")throw new Error("data-static-export-assets requires application/json.");let s;try{s=JSON.parse(r[2]??"");}catch{throw new Error("Invalid JSON in data-static-export-assets.")}let o=s;if(!o||typeof o!="object"||Array.isArray(o)||o.schemaVersion!==void 0&&o.schemaVersion!==1||!Array.isArray(o.assets)||o.assets.length>2048||o.assets.some(i=>typeof i!="string"||!i.trim()))throw new Error("Invalid data-static-export-assets schema (expected version 1 and URL strings).");for(let i of o.assets)t.add(i.trim());}return [...t]}function Ms(e,t){let r=C.relative(t,e);return r===""||!r.startsWith("..")&&!C.isAbsolute(r)}function js(e,t,r,n){let s=C.resolve(e),o=C.resolve(t),i=C.extname(s).toLowerCase();return !Ms(s,o)||i!==".html"&&i!==".htm"||r.has(s)?false:!n.some(c=>Ms(s,C.resolve(c)))}function un(e,t){return typeof e=="string"&&e.length>0&&t?.supported===true&&typeof t.token=="string"&&t.token.length>0&&e===t.token}function dn(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 Ds(e){let t=String(e??"").toLowerCase();return t==="strict"?"Strict":t==="lax"?"Lax":t==="none"?"None":"Unspecified"}function ia(e){let t=e.pathname||"/";if(!t.startsWith("/")||t==="/")return "/";let r=t.lastIndexOf("/");return r<=0?"/":t.slice(0,r)}function Ot(e){return `${e.name.toLowerCase()}
|
|
8
8
|
${e.domain.toLowerCase()}
|
|
9
|
-
${e.path}`}function
|
|
10
|
-
`,{encoding:"utf8",mode:384}),await U.rename(r,e);}function Ot(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 Mt(e,t){let r=Ot(t);if(r===null)throw new Error(`Unsafe output URL pathname: ${t}`);let n=C.resolve(e),s=C.resolve(n,r.replace(/^\/+/,""));if(s!==n&&!s.startsWith(`${n}${C.sep}`))throw new Error(`Output path escapes configured root: ${t}`);return s}var ee=1,js=/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;function te(e,t){let r=String(e??"").trim();if(!js.test(r))throw new Error(`${t} must match ${js.source} and be at most 128 characters.`);return r}function Fs(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 Ye(e,...t){let r=Fs(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 ve(e){return createHash("sha256").update(e).digest("hex")}function je(e,t){let r=String(e??"").replace(/^\/+/,""),n=Fs(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 le(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 fa(e){if(!e||typeof e!="object")throw new Error("Remote worker config must be a JSON object.");let t=e;if(t.schemaVersion!==1)throw new Error(`Unsupported remote worker config version: ${String(t.schemaVersion)}.`);if(t.protocolVersion!==ee)throw new Error(`Remote worker protocol mismatch: expected ${ee}, received ${String(t.protocolVersion)}.`);let r=t.workspace,n=t.functions;if(!r||!n)throw new Error("Remote worker config requires workspace and functions objects.");let s=ut(t.awsProfile,"awsProfile");return {schemaVersion:1,region:le(t.region,"region"),...s?{awsProfile:s}:{},...le(t.roleArn,"roleArn",{optional:true})?{roleArn:le(t.roleArn,"roleArn")}:{},...le(t.roleSessionName,"roleSessionName",{optional:true})?{roleSessionName:le(t.roleSessionName,"roleSessionName")}:{},workspace:{bucket:le(r.bucket,"workspace.bucket"),prefix:String(r.prefix??"")},...t.status&&typeof t.status=="object"?{status:{tableName:le(t.status.tableName,"status.tableName")}}:{},functions:{render:le(n.render,"functions.render"),...le(n.asset,"functions.asset",{optional:true})?{asset:le(n.asset,"functions.asset")}:{},rewrite:le(n.rewrite,"functions.rewrite"),"deploy-copy":le(n["deploy-copy"],"functions.deploy-copy")},targets:Array.isArray(t.targets)?t.targets.map((o,i)=>{if(!o||typeof o!="object")throw new Error(`targets[${i}] must be an object.`);let c=o;return {id:le(c.id,`targets[${i}].id`),bucketName:le(c.bucketName,`targets[${i}].bucketName`),prefix:String(c.prefix??""),region:le(c.region,`targets[${i}].region`)}}):[],protocolVersion:ee}}async function sr(e){let t=C.resolve(e);try{return fa(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 ht=class{config;lambda;s3;dynamodb;#e;constructor(t,r={},n={}){this.config=t;let o=dt(t.awsProfile)??(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,...o?{credentials:o}:{},...r}),this.s3=new S3Client({region:t.region,...o?{credentials:o}:{}}),this.dynamodb=t.status?new DynamoDBClient({region:t.region,...o?{credentials:o}:{}}):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!==ee||o.operation!==t.operation||o.jobId!==t.jobId||o.taskId!==t.taskId)throw new Error(`Remote ${t.operation} worker returned a mismatched response.`);return o}async readResult(t){if(!t.resultKey)throw new Error(`Remote ${t.operation} response does not contain a resultKey.`);let r=je(t.resultKey,this.config.workspace.prefix),n=await this.s3.send(new GetObjectCommand({Bucket:this.config.workspace.bucket,Key:r}));if(!n.Body)throw new Error(`Remote worker result has no body: ${r}`);return JSON.parse(await n.Body.transformToString("utf8"))}async readObject(t){if(t.bucket!==this.config.workspace.bucket)throw new Error(`Remote worker object bucket mismatch: ${t.bucket}.`);let r=je(t.key,this.config.workspace.prefix),n=await this.s3.send(new GetObjectCommand({Bucket:this.config.workspace.bucket,Key:r,...t.versionId?{VersionId:t.versionId}:{}}));if(!n.Body)throw new Error(`Remote worker object has no body: ${r}`);let s=await n.Body.transformToByteArray();if(s.byteLength!==t.bytes)throw new Error(`Remote worker object length mismatch for ${r}: expected ${t.bytes}, received ${s.byteLength}.`);if(ve(s)!==t.sha256)throw new Error(`Remote worker object checksum mismatch for ${r}.`);return s}async readObjectText(t){return new TextDecoder("utf8",{fatal:true}).decode(await this.readObject(t))}async health(t){let r=this.config.functions[t];if(!r)throw new Error(`Remote worker configuration does not provide a ${t} function.`);let n=`${t}-${Date.now()}`,s=await this.lambda.send(new InvokeCommand({FunctionName:r,InvocationType:"RequestResponse",Payload:Buffer.from(JSON.stringify({schemaVersion:ee,operation:"health",jobId:"health",taskId:n}),"utf8")})),o=s.Payload?JSON.parse(new TextDecoder().decode(s.Payload)):null;if(s.FunctionError||!o||typeof o!="object")throw new Error(`Remote ${t} health check failed: ${JSON.stringify(o)}`);return o}async writeWorkspaceObject(t){let r=je(t.key,this.config.workspace.prefix),n=typeof t.body=="string"?Buffer.from(t.body,"utf8"):t.body,s=ve(n),o=await this.s3.send(new PutObjectCommand({Bucket:this.config.workspace.bucket,Key:r,Body:n,ContentType:t.contentType,...t.cacheControl?{CacheControl:t.cacheControl}:{},Metadata:{sha256:s,...t.metadata??{}}}));return {bucket:this.config.workspace.bucket,key:r,sha256:s,bytes:n.byteLength,contentType:t.contentType,...o.ETag?{etag:o.ETag}:{},...o.VersionId?{versionId:o.VersionId}:{}}}async copyWorkspaceObject(t,r){if(t.bucket!==this.config.workspace.bucket)throw new Error(`Remote worker object bucket mismatch: ${t.bucket}.`);let n=je(t.key,this.config.workspace.prefix),s=je(r,this.config.workspace.prefix),o=`${t.bucket}/${n.split("/").map(c=>encodeURIComponent(c)).join("/")}`,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 or(e){return Number.isInteger(e)&&e>=500&&e<=599}var un=3;function ir(e){return Math.min(3e4,5e3*2**Math.max(0,e-1))}var be=class extends Error{constructor(r,n,s,o){let i=`Page failed after ${s} attempt(s), last server response HTTP ${n}: ${r}`;super(i+(o&&o!==i?`; ${o}`:""));this.url=r;this.status=n;this.attempts=s;this.name="PageHttpError";}url;status;attempts};function Ls(e){if(e.size===0)return;let t=[...e.values()].slice(0,5).map(r=>`HTTP ${r.status}: ${r.url}`).join("; ");throw new Error(`Crawl incomplete: ${e.size} page(s) failed after HTTP 5xx retries. Deployment is blocked; no complete deploy plan was produced. ${t}`)}function wa(e,t,r){if(!e||typeof e!="object")throw new Error("Remote render result must be a JSON object.");let n=e;if(n.schemaVersion!==ee||!n.response||n.response.operation!=="render"||n.response.jobId!==t.jobId||n.response.taskId!==t.taskId||!n.detail||!Array.isArray(n.detail.pages)||n.detail.pages.length!==r.length)throw new Error("Remote render result has an invalid envelope.");for(let[s,o]of r.entries())if(n.detail.pages[s]?.requestedUrl!==o)throw new Error("Remote render result does not match the requested URLs.");return n}function dn(e){return e instanceof Error?e.message:String(e)}var ar=class{#e;#n;#t;#s;#c;#l;#u;#r=[];#d=0;#o=0;#i=false;constructor(t){this.#e=t.client,this.#n=te(t.jobId,"jobId"),this.#t=Math.max(1,Math.min(5,t.maxAttempts??2)),this.#s=t.onRetry,this.#u=t.sleep??setTimeout$1,this.#c=Math.max(1,Math.min(20,t.batchSize??1)),this.#l=Math.max(1,Math.min(100,t.maxConcurrentBatches??1));}async renderBatch(t){let r=[...new Set(t.urls)];if(r.length<1||r.length>20)throw new Error("Remote render batches require between 1 and 20 unique URLs.");let n=new Map,s=new Map,o=new Map,i=new Map,c=r;for(let a=1;a<=this.#t&&c.length>0;a+=1){this.#d+=1;let d=te(`render-${this.#d}-${ve(c.join(`
|
|
11
|
-
`)).slice(0,24)}-a${a}`,"taskId"),u=[];try{let l=await this.#e.invoke({schemaVersion:ee,operation:"render",jobId:this.#n,taskId:d,sourceOrigin:new URL(t.sourceOrigin).origin,urls:c,options:t.options}),g=
|
|
12
|
-
`)).slice(0,24)}-a${s}`,"taskId");try{let i=await this.#e.invoke({schemaVersion:ee,operation:"asset",jobId:this.#n,taskId:o,urls:r,options:{ignoreHttpsErrors:t.ignoreHttpsErrors,timeoutMs:Math.max(1e3,Math.min(12e4,t.timeoutMs??6e4))}});return {assets:
|
|
13
|
-
`)).slice(0,16)}`,"taskId"),B={schemaVersion:ee,operation:"rewrite",jobId:o,taskId:N,configKey:c,assetMapKey:a,previousAssetMapKey:d,items:k.map(R=>({inputKey:R.key,outputKey:R.key,relativePath:R.relativePath,contentType:R.contentType}))};try{let R=await r.client.invoke(B);T=
|
|
14
|
-
`)).slice(0,16)}`,"taskId");try{let k=await n.client.invoke({schemaVersion:ee,operation:"rewrite",jobId:i,taskId:E,configKey:a,assetMapKey:d,items:y.map(v=>({inputKey:v.object.key,outputKey:v.object.key,relativePath:v.relativePath,contentType:v.contentType||v.object.contentType}))});b=
|
|
15
|
-
`,"utf8");}async function
|
|
16
|
-
${r}`,40),tokenSource:t.tokenSource?`${t.tokenSource}+site-subscriber`:"site-subscriber"}}async function
|
|
9
|
+
${e.path}`}function Us(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=ia(r),d=null,u=true,l=false,g=false,p="Unspecified",f=false;for(let w of n){let m=w.indexOf("="),h=(m<0?w:w.slice(0,m)).trim().toLowerCase(),y=m<0?"":w.slice(m+1).trim();if(h==="domain"&&y)c=y.toLowerCase();else if(h==="path"&&y.startsWith("/"))a=y;else if(h==="expires"){let b=Date.parse(y);Number.isFinite(b)&&(d=Math.floor(b/1e3),u=false);}else if(h==="max-age"&&/^-?\d+$/u.test(y)){let b=Number.parseInt(y,10);d=Math.floor(Date.now()/1e3)+b,u=false;}else h==="secure"?l=true:h==="httponly"?g=true:h==="samesite"?p=Ds(y):h==="partitioned"&&(f=true);}return {name:i,domain:c,path:a,expires:d,session:u,secure:l,httpOnly:g,sameSite:p,partitioned:f}}function aa(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:Ds(e.sameSite),partitioned:e.partitioned===true}}function Ls(e,t){let r=new Map(e.map(n=>[Ot(n),n.value]));return t.filter(n=>r.get(Ot(n))!==n.value).map(aa)}function ca(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 or=class{#e=new Map;add(t){if(!t)return;let r={...t,domain:t.domain.toLowerCase(),path:t.path||"/"},n=Ot(r),s=this.#e.get(n);this.#e.set(n,s?ca(s,r):r);}addAll(t){for(let r of t)this.add(r);}artifact(){return {schemaVersion:1,observations:[...this.#e.values()].sort((t,r)=>Ot(t).localeCompare(Ot(r)))}}};async function ir(e,t){await U.mkdir(C.dirname(e),{recursive:true});let r=`${e}.${process.pid}.tmp`;await U.writeFile(r,`${JSON.stringify(t,null,2)}
|
|
10
|
+
`,{encoding:"utf8",mode:384}),await U.rename(r,e);}function Mt(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 jt(e,t){let r=Mt(t);if(r===null)throw new Error(`Unsafe output URL pathname: ${t}`);let n=C.resolve(e),s=C.resolve(n,r.replace(/^\/+/,""));if(s!==n&&!s.startsWith(`${n}${C.sep}`))throw new Error(`Output path escapes configured root: ${t}`);return s}var ee=1,Ws=/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;function te(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 qs(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 Ye(e,...t){let r=qs(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 ve(e){return createHash("sha256").update(e).digest("hex")}function je(e,t){let r=String(e??"").replace(/^\/+/,""),n=qs(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 le(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 ba(e){if(!e||typeof e!="object")throw new Error("Remote worker config must be a JSON object.");let t=e;if(t.schemaVersion!==1)throw new Error(`Unsupported remote worker config version: ${String(t.schemaVersion)}.`);if(t.protocolVersion!==ee)throw new Error(`Remote worker protocol mismatch: expected ${ee}, received ${String(t.protocolVersion)}.`);let r=t.workspace,n=t.functions;if(!r||!n)throw new Error("Remote worker config requires workspace and functions objects.");let s=ut(t.awsProfile,"awsProfile");return {schemaVersion:1,region:le(t.region,"region"),...s?{awsProfile:s}:{},...le(t.roleArn,"roleArn",{optional:true})?{roleArn:le(t.roleArn,"roleArn")}:{},...le(t.roleSessionName,"roleSessionName",{optional:true})?{roleSessionName:le(t.roleSessionName,"roleSessionName")}:{},workspace:{bucket:le(r.bucket,"workspace.bucket"),prefix:String(r.prefix??"")},...t.status&&typeof t.status=="object"?{status:{tableName:le(t.status.tableName,"status.tableName")}}:{},functions:{render:le(n.render,"functions.render"),...le(n.asset,"functions.asset",{optional:true})?{asset:le(n.asset,"functions.asset")}:{},rewrite:le(n.rewrite,"functions.rewrite"),"deploy-copy":le(n["deploy-copy"],"functions.deploy-copy")},targets:Array.isArray(t.targets)?t.targets.map((o,i)=>{if(!o||typeof o!="object")throw new Error(`targets[${i}] must be an object.`);let c=o;return {id:le(c.id,`targets[${i}].id`),bucketName:le(c.bucketName,`targets[${i}].bucketName`),prefix:String(c.prefix??""),region:le(c.region,`targets[${i}].region`)}}):[],protocolVersion:ee}}async function ar(e){let t=C.resolve(e);try{return ba(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 wt=class{config;lambda;s3;dynamodb;#e;constructor(t,r={},n={}){this.config=t;let o=dt(t.awsProfile)??(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,...o?{credentials:o}:{},...r}),this.s3=new S3Client({region:t.region,...o?{credentials:o}:{}}),this.dynamodb=t.status?new DynamoDBClient({region:t.region,...o?{credentials:o}:{}}):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!==ee||o.operation!==t.operation||o.jobId!==t.jobId||o.taskId!==t.taskId)throw new Error(`Remote ${t.operation} worker returned a mismatched response.`);return o}async readResult(t){if(!t.resultKey)throw new Error(`Remote ${t.operation} response does not contain a resultKey.`);let r=je(t.resultKey,this.config.workspace.prefix),n=await this.s3.send(new GetObjectCommand({Bucket:this.config.workspace.bucket,Key:r}));if(!n.Body)throw new Error(`Remote worker result has no body: ${r}`);return JSON.parse(await n.Body.transformToString("utf8"))}async readObject(t){if(t.bucket!==this.config.workspace.bucket)throw new Error(`Remote worker object bucket mismatch: ${t.bucket}.`);let r=je(t.key,this.config.workspace.prefix),n=await this.s3.send(new GetObjectCommand({Bucket:this.config.workspace.bucket,Key:r,...t.versionId?{VersionId:t.versionId}:{}}));if(!n.Body)throw new Error(`Remote worker object has no body: ${r}`);let s=await n.Body.transformToByteArray();if(s.byteLength!==t.bytes)throw new Error(`Remote worker object length mismatch for ${r}: expected ${t.bytes}, received ${s.byteLength}.`);if(ve(s)!==t.sha256)throw new Error(`Remote worker object checksum mismatch for ${r}.`);return s}async readObjectText(t){return new TextDecoder("utf8",{fatal:true}).decode(await this.readObject(t))}async health(t){let r=this.config.functions[t];if(!r)throw new Error(`Remote worker configuration does not provide a ${t} function.`);let n=`${t}-${Date.now()}`,s=await this.lambda.send(new InvokeCommand({FunctionName:r,InvocationType:"RequestResponse",Payload:Buffer.from(JSON.stringify({schemaVersion:ee,operation:"health",jobId:"health",taskId:n}),"utf8")})),o=s.Payload?JSON.parse(new TextDecoder().decode(s.Payload)):null;if(s.FunctionError||!o||typeof o!="object")throw new Error(`Remote ${t} health check failed: ${JSON.stringify(o)}`);return o}async writeWorkspaceObject(t){let r=je(t.key,this.config.workspace.prefix),n=typeof t.body=="string"?Buffer.from(t.body,"utf8"):t.body,s=ve(n),o=await this.s3.send(new PutObjectCommand({Bucket:this.config.workspace.bucket,Key:r,Body:n,ContentType:t.contentType,...t.cacheControl?{CacheControl:t.cacheControl}:{},Metadata:{sha256:s,...t.metadata??{}}}));return {bucket:this.config.workspace.bucket,key:r,sha256:s,bytes:n.byteLength,contentType:t.contentType,...o.ETag?{etag:o.ETag}:{},...o.VersionId?{versionId:o.VersionId}:{}}}async copyWorkspaceObject(t,r){if(t.bucket!==this.config.workspace.bucket)throw new Error(`Remote worker object bucket mismatch: ${t.bucket}.`);let n=je(t.key,this.config.workspace.prefix),s=je(r,this.config.workspace.prefix),o=`${t.bucket}/${n.split("/").map(c=>encodeURIComponent(c)).join("/")}`,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 cr(e){return Number.isInteger(e)&&e>=500&&e<=599}var gn=3;function lr(e){return Math.min(3e4,5e3*2**Math.max(0,e-1))}var be=class extends Error{constructor(r,n,s,o){let i=`Page failed after ${s} attempt(s), last server response HTTP ${n}: ${r}`;super(i+(o&&o!==i?`; ${o}`:""));this.url=r;this.status=n;this.attempts=s;this.name="PageHttpError";}url;status;attempts};function Bs(e){if(e.size===0)return;let t=[...e.values()].slice(0,5).map(r=>`HTTP ${r.status}: ${r.url}`).join("; ");throw new Error(`Crawl incomplete: ${e.size} page(s) failed after HTTP 5xx retries. Deployment is blocked; no complete deploy plan was produced. ${t}`)}function Pa(e,t,r){if(!e||typeof e!="object")throw new Error("Remote render result must be a JSON object.");let n=e;if(n.schemaVersion!==ee||!n.response||n.response.operation!=="render"||n.response.jobId!==t.jobId||n.response.taskId!==t.taskId||!n.detail||!Array.isArray(n.detail.pages)||n.detail.pages.length!==r.length)throw new Error("Remote render result has an invalid envelope.");for(let[s,o]of r.entries())if(n.detail.pages[s]?.requestedUrl!==o)throw new Error("Remote render result does not match the requested URLs.");return n}function fn(e){return e instanceof Error?e.message:String(e)}var ur=class{#e;#n;#t;#s;#c;#l;#u;#r=[];#d=0;#o=0;#i=false;constructor(t){this.#e=t.client,this.#n=te(t.jobId,"jobId"),this.#t=Math.max(1,Math.min(5,t.maxAttempts??2)),this.#s=t.onRetry,this.#u=t.sleep??setTimeout$1,this.#c=Math.max(1,Math.min(20,t.batchSize??1)),this.#l=Math.max(1,Math.min(100,t.maxConcurrentBatches??1));}async renderBatch(t){let r=[...new Set(t.urls)];if(r.length<1||r.length>20)throw new Error("Remote render batches require between 1 and 20 unique URLs.");let n=new Map,s=new Map,o=new Map,i=new Map,c=r;for(let a=1;a<=this.#t&&c.length>0;a+=1){this.#d+=1;let d=te(`render-${this.#d}-${ve(c.join(`
|
|
11
|
+
`)).slice(0,24)}-a${a}`,"taskId"),u=[];try{let l=await this.#e.invoke({schemaVersion:ee,operation:"render",jobId:this.#n,taskId:d,sourceOrigin:new URL(t.sourceOrigin).origin,urls:c,options:t.options}),g=Pa(await this.#e.readResult(l),l,c);for(let p of g.detail.pages){if(o.delete(p.requestedUrl),cr(p.httpStatus)){let f=p.httpStatus;o.set(p.requestedUrl,f),i.set(p.requestedUrl,f),s.set(p.requestedUrl,new be(p.requestedUrl,f,a).message),u.push(p.requestedUrl);continue}if(p.httpStatus!==null&&p.httpStatus!==void 0)i.delete(p.requestedUrl);else if(i.has(p.requestedUrl)){s.set(p.requestedUrl,p.error||"Remote render returned no HTTP status after a server error."),u.push(p.requestedUrl);continue}if(p.outcome==="failed"||p.outcome==="deferred"||!p.html){let f=p.error||`Remote render ended with ${p.outcome}.`;s.set(p.requestedUrl,f),u.push(p.requestedUrl);continue}try{n.set(p.requestedUrl,{page:p,html:t.downloadHtml===!1?null:await this.#e.readObjectText(p.html),taskId:d,attempts:a}),s.delete(p.requestedUrl);}catch(f){s.set(p.requestedUrl,fn(f)),u.push(p.requestedUrl);}}}catch(l){let g=fn(l);for(let p of c)o.delete(p),s.set(p,g),u.push(p);}if(a<this.#t){let l=u.some(g=>o.has(g))?lr(a):0;for(let g of u){let p=o.get(g);this.#s?.({url:g,attempt:a,maxAttempts:this.#t,reason:s.get(g)??"Remote render failed.",...p===void 0?{}:{status:p,delayMs:l}});}l>0&&await this.#u(l);}c=u;}return {pages:r.flatMap(a=>{let d=n.get(a);return d?[d]:[]}),failures:c.map(a=>({url:a,attempts:this.#t,error:s.get(a)??"Remote render failed.",...i.has(a)?{httpStatus:i.get(a)}:{}}))}}async render(t){let r=JSON.stringify({sourceOrigin:new URL(t.sourceOrigin).origin,options:t.options,downloadHtml:t.downloadHtml!==false});return await new Promise((n,s)=>{this.#r.push({input:t,key:r,resolve:n,reject:s}),this.#a();})}#a(){this.#i||this.#r.length===0||(this.#i=true,setTimeout(()=>{this.#i=false,this.#p();},0));}#p(){for(;this.#o<this.#l&&this.#r.length>0;){let t=this.#r.shift();if(!t)break;let r=[t];for(let n=0;n<this.#r.length&&r.length<this.#c;){let s=this.#r[n];s?.key===t.key?(r.push(s),this.#r.splice(n,1)):n+=1;}this.#o+=1,this.renderBatch({sourceOrigin:t.input.sourceOrigin,urls:r.map(n=>n.input.url),options:t.input.options,...t.input.downloadHtml===void 0?{}:{downloadHtml:t.input.downloadHtml}}).then(n=>{let s=new Map(n.pages.map(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(a?.httpStatus===void 0?new Error(`Remote render failed after ${a?.attempts??this.#t} attempt(s) for ${i.input.url}: ${a?.error??"Unknown remote render failure."}`):new be(i.input.url,a.httpStatus,a.attempts,a.error));}}).catch(n=>{let s=fn(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 Ra(e,t,r){if(!e||typeof e!="object")throw new Error("Remote asset result must be a JSON object.");let n=e;if(n.schemaVersion!==ee||n.response?.operation!=="asset"||n.response.jobId!==t.jobId||n.response.taskId!==t.taskId||!n.detail||!Array.isArray(n.detail.assets)||n.detail.assets.length!==r.length)throw new Error("Remote asset result has an invalid envelope.");for(let s=0;s<r.length;s+=1)if(n.detail.assets[s]?.requestedUrl!==r[s])throw new Error("Remote asset result does not match the requested URLs.");return n}var dr=class{#e;#n;#t;#s=0;constructor(t){this.#e=t.client,this.#n=te(t.jobId,"jobId"),this.#t=Math.max(1,Math.min(5,t.maxAttempts??2));}async fetch(t){let r=[...t.urls];if(r.length<1||r.length>20)throw new Error("Remote asset batches require between 1 and 20 URLs.");let n=null;for(let s=1;s<=this.#t;s+=1){this.#s+=1;let o=te(`asset-${this.#s}-${ve(r.join(`
|
|
12
|
+
`)).slice(0,24)}-a${s}`,"taskId");try{let i=await this.#e.invoke({schemaVersion:ee,operation:"asset",jobId:this.#n,taskId:o,urls:r,options:{ignoreHttpsErrors:t.ignoreHttpsErrors,timeoutMs:Math.max(1e3,Math.min(12e4,t.timeoutMs??6e4))}});return {assets:Ra(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 Vs(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 zs(e,t){if(!e||typeof e!="object")throw new Error("Remote rewrite result must be a JSON object.");let r=e;if(r.schemaVersion!==ee||r.response?.operation!=="rewrite"||r.response.jobId!==t.jobId||r.response.taskId!==t.taskId||!r.detail||!Array.isArray(r.detail.objects))throw new Error("Remote rewrite result has an invalid envelope.");return r}async function pr(e,t,r){let n=r.options??{},s=await cn(e,n.files);if(s.length===0)return 0;let o=te(r.jobId,"jobId"),i=Ye(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 u=Math.max(1,Math.min(500,r.batchSize)),l=Math.ceil(s.length/u),g=Math.max(1,Math.min(Math.max(1,r.concurrency),l));await n.onStart?.({totalFiles:s.length,workerCount:g});let p=[],f=0,w=0;await Promise.all(Array.from({length:Math.min(16,s.length)},async()=>{for(;;){let E=f;if(E>=s.length)return;f+=1;let k=s[E],v=C.join(e.outputDir,k),T=Hs.lookup(k),$=T?Hs.contentType(T)||T:"text/plain; charset=utf-8",N=`${i}/files/${ve(k)}.txt`.replace(/\/+/g,"/");await r.client.writeWorkspaceObject({key:N,body:await U.readFile(v),contentType:$}),p[E]={relativePath:k,key:N,fullPath:v,contentType:$},w+=1,await r.onStageProgress?.({index:w,totalFiles:s.length,file:k});}}));let m=Vs(p,u),h=0,y=0,b=0,S=0,j=Array.from({length:g},async()=>{for(;;){let E=h;if(E>=m.length)return;h+=1;let k=m[E],v=null,T=null;for(let $=1;$<=Math.max(1,Math.min(5,r.maxAttempts));$+=1){let N=te(`rewrite-${E+1}-a${$}-${ve(k.map(R=>R.relativePath).join(`
|
|
13
|
+
`)).slice(0,16)}`,"taskId"),B={schemaVersion:ee,operation:"rewrite",jobId:o,taskId:N,configKey:c,assetMapKey:a,previousAssetMapKey:d,items:k.map(R=>({inputKey:R.key,outputKey:R.key,relativePath:R.relativePath,contentType:R.contentType}))};try{let R=await r.client.invoke(B);T=zs(await r.client.readResult(R),R);break}catch(R){v=R;}}if(!T)throw new Error(`Remote rewrite batch ${E+1}/${m.length} failed: ${v instanceof Error?v.message:String(v)}`,{cause:v});for(let $ of k){let N=T.detail.objects.find(B=>B.outputKey===$.key);if(!N)throw new Error(`Remote rewrite result omitted ${$.relativePath}.`);await _e($.fullPath,Buffer.from(await r.client.readObject(N.output))),N.changed&&(S+=1),y+=1,await n.onProgress?.({index:y,totalFiles:s.length,changedTextFiles:S,file:$.relativePath,changed:N.changed});}b+=1,await r.onBatchProgress?.({completedItems:y,totalItems:s.length,completedBatches:b,totalBatches:m.length,changedTextFiles:S});}});return await Promise.all(j),S}async function mr(e,t,r,n){let s=n.relativePaths?new Set([...n.relativePaths].map(h=>h.replace(/\\/g,"/"))):null,o=Object.values(t.objects).filter(h=>h.isText&&(!s||s.has(h.relativePath))).sort((h,y)=>h.relativePath.localeCompare(y.relativePath));if(o.length===0)return 0;let i=te(n.jobId,"jobId"),c=Ye(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 u=Math.max(1,Math.min(500,n.batchSize)),l=Vs(o,u),g=Math.max(1,Math.min(Math.max(1,n.concurrency),l.length)),p=0,f=0,w=0,m=0;return await Promise.all(Array.from({length:g},async()=>{for(;;){let h=p;if(h>=l.length)return;p+=1;let y=l[h],b=null,S=null;for(let j=1;j<=Math.max(1,Math.min(5,n.maxAttempts));j+=1){let E=te(`rewrite-object-${h+1}-a${j}-${ve(y.map(k=>k.relativePath).join(`
|
|
14
|
+
`)).slice(0,16)}`,"taskId");try{let k=await n.client.invoke({schemaVersion:ee,operation:"rewrite",jobId:i,taskId:E,configKey:a,assetMapKey:d,items:y.map(v=>({inputKey:v.object.key,outputKey:v.object.key,relativePath:v.relativePath,contentType:v.contentType||v.object.contentType}))});b=zs(await n.client.readResult(k),k);break}catch(k){S=k;}}if(!b)throw new Error(`Remote object rewrite batch ${h+1}/${l.length} failed: ${S instanceof Error?S.message:String(S)}`,{cause:S});for(let j of y){let E=b.detail.objects.find(k=>k.outputKey===j.object.key);if(!E)throw new Error(`Remote rewrite result omitted ${j.relativePath}.`);j.object=E.output,E.changed&&(m+=1),f+=1,await n.onProgress?.({index:f,totalFiles:o.length,changedTextFiles:m,file:j.relativePath,changed:E.changed});}w+=1,await n.onBatchProgress?.({completedItems:f,totalItems:o.length,completedBatches:w,totalBatches:l.length,changedTextFiles:m});}})),t.assetMap={...r},t.generatedAt=new Date().toISOString(),m}var ka="remote-workers.json";function gr(e={}){let t=String(e.explicitPath??"").trim();if(t)return C.resolve(t);let r=String(e.runtimeDir??process.env.STATIC_PUBLISHER_RUNTIME_DIR??process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR??"").trim();return r?C.join(C.resolve(r),ka):""}function Js(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}async function Qs(e,t){let r=String(e.wpsuite?.runtimeToken||"").trim();if(!r)throw new Error("Page-cache purge is enabled, but the WordPress runtime token is missing.");let n=new URL("/wp-json/smartcloud-static-publisher/v1/page-cache/purge",e.sourceOrigin).toString(),s=await t.post(n,{timeout:3e4,failOnStatusCode:false,maxRedirects:0,headers:{"content-type":"application/json","x-static-publisher-token":r},data:JSON.stringify({scope:"all"})});try{let o=await s.text();if(o.length>65536)throw new Error("WordPress page-cache purge response was too large.");let i=null;try{i=JSON.parse(o);}catch{throw new Error(`WordPress page-cache purge returned invalid JSON (HTTP ${s.status()}).`)}if(!s.ok()){let c=i&&typeof i=="object"&&"message"in i?String(i.message||""):"";throw new Error(`WordPress page-cache purge failed with HTTP ${s.status()}${c?`: ${c}`:""}`)}if(!i||typeof i!="object"||i.purged!==!0)throw new Error("WordPress page-cache provider did not confirm a complete purge.");return {purged:!0,provider:String(i.provider||"custom"),scope:"all",...typeof i.purgedAt=="string"?{purgedAt:i.purgedAt}:{}}}finally{await s.dispose().catch(()=>{});}}function xa(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?C.resolve(e):""}function fr(e){let t=String(process.env.STATIC_PUBLISHER_REMOTE_EXPORT_MANIFEST_PATH||"").trim();if(t)return C.resolve(t);let r=xa();if(r)return C.join(r,"remote-export-manifest.json");let n=C.resolve(e.outputDir);return C.join(C.dirname(n),`.${C.basename(n)}-remote-export-manifest.json`)}function hr(e){let t=C.posix.normalize(String(e||"").replace(/\\/g,"/").replace(/^\/+/,""));if(!t||t==="."||t===".."||t.startsWith("../")||/^[A-Za-z]:/.test(t))throw new Error(`Invalid remote export relative path: ${e}`);return t}function Zs(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 Ys(e,t){let r=e.objects[hr(t)];if(!r)return true;let n=`${Ye(e.workspace.prefix,"jobs",te(e.jobId,"jobId"))}/`;return !je(r.object.key,e.workspace.prefix).startsWith(n)}function yt(e,t){let r=hr(t.relativePath);if(t.object.bucket!==e.workspace.bucket)throw new Error(`Remote export object bucket mismatch for ${r}.`);je(t.object.key,e.workspace.prefix),e.objects[r]={...t,relativePath:r},e.generatedAt=new Date().toISOString();}function Xs(e,t){delete e.objects[hr(t)],e.generatedAt=new Date().toISOString();}async function eo(e,t){t.generatedAt=new Date().toISOString();let r=fr(e);await U.mkdir(C.dirname(r),{recursive:true}),await _e(r,`${JSON.stringify(t,null,2)}
|
|
15
|
+
`,"utf8");}async function wr(e){try{let t=JSON.parse(await U.readFile(fr(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(hr(n),!s||s.relativePath!==n||s.object?.bucket!==r.workspace.bucket)return null;je(s.object.key,r.workspace.prefix);}return r}catch{return null}}var Aa=["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"],go=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"]),fo=new Set(["",".html",".htm"]),Ea=new Set([".css",".xml",".xsl",".svg",".json",".html",".htm",".txt",".enc",".jws"]),$a=["/wp-content/","/wp-includes/","/wp-admin/","/static/","/assets/","/build/","/_next/"];function Oa(e){let t=[],r="full",n="full",s=false,o=false,i="",c=null,a="";for(let d=0;d<e.length;d++){let u=e[d];if(u==="--retry-timeouts")r="retry-timeouts",s=true;else if(u==="--resume")s=true;else if(u==="--resume-rewrite")s=true,o=true;else if(u==="--crawl-mode"){let l=e[++d];if(!l)throw new Error("--crawl-mode requires a value");n=l==="incremental"?"incremental":"full",s||=n==="incremental";}else if(u.startsWith("--crawl-mode="))n=u.slice(13)==="incremental"?"incremental":"full",s||=n==="incremental";else if(u==="--incremental")n="incremental",s=true;else if(u==="--url"){let l=e[++d];if(!l)throw new Error("--url requires a value");r="single-url",s=true,t.push(l);}else if(u.startsWith("--url="))r="single-url",s=true,t.push(u.slice(6));else if(u==="--urls"){let l=e[++d];if(!l)throw new Error("--urls requires a file path");r="single-url",s=true,t.push(`@${l}`);}else if(u==="--content-sync-plan"){let l=e[++d];if(!l)throw new Error("--content-sync-plan requires a file path");r="content-sync",n="incremental",s=true,i=C.resolve(l);}else if(u.startsWith("--content-sync-plan="))r="content-sync",n="incremental",s=true,i=C.resolve(u.slice(20));else if(u==="--remote-render")c=true;else if(u==="--local-render")c=false;else if(u==="--remote-worker-config"){let l=e[++d];if(!l)throw new Error("--remote-worker-config requires a file path");a=C.resolve(l);}else u.startsWith("--remote-worker-config=")&&(a=C.resolve(u.slice(23)));}return {mode:r,crawlMode:n,urls:t,preserveOutput:s,resumeRewrite:o,contentSyncPlanPath:i,remoteRender:c,remoteWorkerConfigPath:a}}function _t(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?C.resolve(e):""}function Ma(){let e=_t();return e?C.join(e,"privacy","cookie-observations.json"):null}function ja(){let e=_t();return e?C.resolve(e,".."):""}function Fa(){let e=process.env.STATIC_PUBLISHER_WP_ROOT||process.env.WPSUITE_STATIC_PUBLISHER_WP_ROOT||"";return e.trim()?C.resolve(e):""}function Da(e){let t=e.trim(),r=[{alias:"@storage-root",root:ja(),requiredEnv:"STATIC_PUBLISHER_RUNTIME_DIR or WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR"},{alias:"@runtime",root:_t(),requiredEnv:"STATIC_PUBLISHER_RUNTIME_DIR or WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR"},{alias:"@wp-root",root:Fa(),requiredEnv:"STATIC_PUBLISHER_WP_ROOT or WPSUITE_STATIC_PUBLISHER_WP_ROOT"}];for(let n of r){if(t!==n.alias&&!t.startsWith(`${n.alias}/`))continue;if(!n.root)return {resolvedPath:null,alias:n.alias,requiredEnv:n.requiredEnv};let s=t.slice(n.alias.length).replace(/^\/+/,"");return {resolvedPath:s?C.resolve(n.root,s):n.root,alias:n.alias}}return {resolvedPath:C.resolve(t)}}function ho(e){let t=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(t)return C.resolve(t);let r=_t();return r?C.join(r,"crawl-manifest.json"):C.join(e.outputDir,".crawl-manifest.json")}function wo(){return {schemaVersion:6,updatedAt:"",pages:{},assets:{}}}function no(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 Ua(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 yn(e){let t=e?.["content-type"]?.trim();return t||null}function bn(e){let t=e?.["cache-control"]?.trim();return t||null}function La(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 Wa(){return {rewriteTargets:new Set,changedFiles:new Set,deletedFiles:new Set,revalidateFiles:new Set}}function bt(e,t,r,n){let s=ge(t.outputDir,r);s&&(La(n)?e.revalidateFiles.add(s):e.revalidateFiles.delete(s));}function ue(e,t,r){let n=ge(t.outputDir,r);n&&(e.changedFiles.add(n),e.deletedFiles.delete(n));}function Sn(e,t,r){let n=ge(t.outputDir,r);n&&(e.deletedFiles.add(n),e.changedFiles.delete(n),e.revalidateFiles.delete(n));}function Ce(e,t){e.rewriteTargets.add(C.resolve(t));}async function qa(e){try{let t=await U.readFile(ho(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,no(s)])),assets:Object.fromEntries(Object.entries(r.assets).map(([n,s])=>[n,Ua(s)]))};if(r.schemaVersion===1)return {schemaVersion:6,updatedAt:String(r.updatedAt||""),pages:Object.fromEntries(Object.entries(r.pages).map(([n,s])=>[n,no(s)])),assets:{}}}}catch{}return wo()}function Pn(e){return JSON.parse(JSON.stringify(e))}async function _a(e,t){let r=ho(e);await U.mkdir(C.dirname(r),{recursive:true}),await U.writeFile(r,JSON.stringify(t,null,2),"utf8");}async function Ve(e,t,r=false){if(!t.trackManifest)return;t.manifestPendingChanges+=1;let n=Date.now();if(!r&&t.manifestPendingChanges<25&&n-t.lastManifestSavedAt<5e3)return;t.manifestPendingChanges=0,t.lastManifestSavedAt=n;let s=async()=>{let o=Pn(t.manifest);o.updatedAt=new Date().toISOString(),t.manifest.updatedAt=o.updatedAt,await _a(e,o);};t.manifestSaveChain=t.manifestSaveChain.then(s,s),await t.manifestSaveChain;}function nt(e){if(!e)return null;let t=e.trim();return t||null}function Na(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 yo(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 u of a.items)!u||typeof u.url!="string"||d.set(u.url,u);return d}catch(c){return n.warn("Change token lookup failed; falling back to sitemap metadata",{endpoint:i,error:c instanceof Error?`${c.name}: ${c.message.split(/\r?\n/u,1)[0]}`:"Unknown change-token request failure"}),new Map}}async function br(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 yo(e,t,[n],s)).get(n)??null;return xr(e,n)&&(!i||i.supported!==true||!i.token)&&(i=await Va(e,t,n,s)),i=bo(e,i),r.changeTokenCache.set(n,i),i}async function Tn(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 yo(e,t,c,s);for(let d of c)r.changeTokenCache.set(d,bo(e,a.get(d)??null));}}async function Ba(e,t,r,n,s,o){if(!(!n.trackManifest||n.changeTokenCache.has(s)))for(;!n.changeTokenCache.has(s);){if(!n.pageChangeTokenPrimePromise){let i=Js(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=Tn(e,t,n,i,o).finally(()=>{n.pageChangeTokenPrimePromise===c&&(n.pageChangeTokenPrimePromise=null);});n.pageChangeTokenPrimePromise=c;}await n.pageChangeTokenPrimePromise;}}function Ha(e){return `subscriber=${e.wpsuite?.siteSettings?.subscriber===true?"1":"0"}`}function bo(e,t){if(!t?.supported||!t.token||t.tokenSource==="wp-media-file")return t;let r=Ha(e);return {...t,token:Ee(`${t.token}
|
|
16
|
+
${r}`,40),tokenSource:t.tokenSource?`${t.tokenSource}+site-subscriber`:"site-subscriber"}}async function Va(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=Ee(`${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 fo(e,t,r,n,s,o){let i=vr(e,s);if(await qa(e,t,r,n,s,o),!n.enabled)return {action:"render",changeToken:n.trackManifest?await hr(e,t,n,s,o):null};let c=n.manifest.pages[s];if(!c)return {action:"render",changeToken:await hr(e,t,n,s,o)};let a=await hr(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=nt(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 Ha(e,t,r,n,s){let o=Xs(r.previousManifest,void 0,n,"previous-manifest"),i=0;for(let[g,p]of Object.entries(r.manifest.pages))if(!r.seenPages.has(g)){hn(s,e,p.outputPath);try{await U.unlink(p.outputPath);}catch(f){(f.code||"")!=="ENOENT"&&n.warn(`Failed to remove stale incremental page output for ${g}`,{url:g,outputPath:p.outputPath,error:String(f)});}delete r.manifest.pages[g],i++;}let c=Xs(r.manifest,r.runId,n,"active-manifest"),a=new Set,d=new Set;for(let g of c){for(let w of Ut(e,g))a.add(w);let p=r.manifest.assets[g]?.outputPath;p&&d.add(p);let f=bt(e,t.assetMap,g);f&&d.add(f);}for(let g of Object.keys(r.manifest.assets)){if(c.has(g)){r.manifest.assets[g].unreferencedRuns=0;continue}let p=r.manifest.assets[g];if(Es(p.unreferencedRuns)){p.unreferencedRuns+=1,n.warn(`Deferred deletion of newly unreferenced incremental asset ${g}.`,{url:g,unreferencedRuns:p.unreferencedRuns,phase:"incremental-cleanup",source:"asset-deletion-grace"});continue}delete r.manifest.assets[g];}let u=0,l=0;for(let g of o){if(r.manifest.assets[g])continue;let p=new Set,f=r.previousManifest.assets[g]?.outputPath;f&&p.add(f);let w=bt(e,t.assetMap,g);if(!c.has(g)&&w)p.add(w);else if(!c.has(g)){let m=ac(e,g,n,"incremental-cleanup");m&&(p.add(m.originalFilePath),m.hashedFilePath&&p.add(m.hashedFilePath));}for(let m of p)if(!d.has(m)){hn(s,e,m);try{await U.unlink(m),u++;}catch(h){(h.code||"")!=="ENOENT"&&n.warn(`Failed to remove stale incremental asset output for ${g}`,{url:g,outputPath:m,error:String(h)});}}for(let m of Ut(e,g))a.has(m)||Object.prototype.hasOwnProperty.call(t.assetMap,m)&&(delete t.assetMap[m],l++);}(i>0||u>0||l>0)&&n.info(`Incremental cleanup removed ${i} stale pages, ${u} stale assets, ${l} stale asset mappings`,{removedPages:i,removedAssets:u,removedAssetMappings:l,phase:"incremental-cleanup"});}async function Va(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=>C.resolve(a))),o=Object.values(e.postCrawlCopyMap||{}).map(a=>wr(String(a||""))).filter(a=>!!a).map(a=>C.resolve(e.outputDir,a)),c=(await Oi(["**/*.html","**/*.htm"],{cwd:e.outputDir,onlyFiles:true,dot:true})).filter(a=>As(C.join(e.outputDir,a),e.outputDir,s,o));for(let a of c){let d=C.join(e.outputDir,a);hn(n,e,d),await U.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 Xs(e,t,r,n){let s=new Set,o=i=>{let c=i.trim();if(c){if(r){let a=ke(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 eo(e,t,r){for(let n of t)for(let s of Ut(e,n))if(r.has(s))return true;return false}function Ka(e,t,r,n,s){let o=new Set(r.rewriteTargets);for(let c of r.changedFiles)o.add(C.resolve(e.outputDir,c));for(let c of Object.values(t.manifest.pages))c.rewriteComplete||o.add(C.resolve(c.outputPath));for(let c of Object.values(t.manifest.assets))c.isText&&!c.rewriteComplete&&o.add(C.resolve(c.outputPath));let i=Wa(n,s);if(i.size===0)return [...o];for(let c of Object.values(t.manifest.pages))eo(e,c.discoveredAssets,i)&&o.add(C.resolve(c.outputPath));for(let[c,a]of Object.entries(t.manifest.assets)){a.isText&&eo(e,a.discoveredAssets,i)&&o.add(C.resolve(a.outputPath));for(let d of Ut(e,c))if(i.has(d)){ue(r,e,a.outputPath),a.isText&&o.add(C.resolve(a.outputPath));break}}return [...o]}function za(e,t){let r=t?new Set(Array.from(t,s=>C.resolve(s))):null,n=s=>r===null||r.has(C.resolve(s));for(let s of Object.values(e.manifest.pages))n(s.outputPath)&&(s.rewriteComplete=true);for(let s of Object.values(e.manifest.assets)){if(!s.isText){s.rewriteComplete=true;continue}n(s.outputPath)&&(s.rewriteComplete=true);}}function Ja(e,t,r){let n=new Set,s=o=>{if(!o)return;let i=ge(e.outputDir,o);i&&n.add(i);};for(let o of t.donePages)s(Fe(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(bt(e,t.assetMap,o));for(let o of Object.values(r.manifest.assets))o.lastSeenRunId===r.runId&&s(o.outputPath);return n}async function Qa(e,t,r,n,s,o){let i=[...n.deletedFiles].sort(),c=Ja(e,t,r),a=0;for(let l of [...c].sort())if(!o?.objects[l]){try{if((await U.stat(C.join(e.outputDir,l))).isFile())continue}catch{}a++,s.warn(`Active output is missing after crawl: ${l}`,{phase:"deploy-plan",file:l,outputPath:C.join(e.outputDir,l)});}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=[],u=0;for(let l of i){if(!c.has(l)){d.push(l);continue}u++;let g=C.join(e.outputDir,l),p;try{p=(await U.stat(g)).isFile();}catch{p=false;}s.warn(`Deploy plan protected active output from deletion: ${l}`,{phase:"deploy-plan",file:l,outputPath:g,exists:p});}return u>0&&s.info(`Deploy plan skipped ${u} active output deletion(s).`,{phase:"deploy-plan",protectedDeletes:u,deletedFiles:d.length}),d}function Ut(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 Ga(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 ho(e,t){let r=e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/"?e.targetOrigin:"https://relative.invalid/";try{let n=new URL(t,r);return Mt(e.outputDir,n.pathname)}catch{return null}}function wo(e,t,r){for(let n of Ga(e,r)){let s=t[n];if(!s)continue;let o=ho(e,s);if(o)return {outputPath:o,publicUrl:s}}return null}function Za(e,t,r){for(let n of Ut(e,r)){let s=t[n];if(!s)continue;let o=ho(e,s);if(o)return o}return null}function bt(e,t,r){let n=Za(e,t,r);if(n)return n;try{return _t(e,r).originalFilePath}catch{return null}}function Ya(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?C.join(C.resolve(e),"queue-runner-heartbeat.json"):""}async function jt(e){let t=Ya();if(t)try{let r=await U.readFile(t,"utf8"),n=JSON.parse(r);if(!n||typeof n!="object")return;await U.writeFile(t,JSON.stringify({...n,checkedAt:new Date().toISOString(),status:"running",currentStep:"rewrite",message:e},null,2),"utf8");}catch{}}function Xa(e,t){let r=new Set;for(let n of t.donePages)r.add(C.resolve(Fe(e,n)));for(let n of t.doneAssets){let s=bt(e,t.assetMap,n);!s||!$e(s)||r.add(C.resolve(s));}for(let n of t.doneSitemaps){let s=bt(e,t.assetMap,n);!s||!$e(s)||r.add(C.resolve(s));}return [...r]}var yr="re:",fr=new Map;function ec(e){if(!e.startsWith(yr))return null;let t=fr.get(e);if(t!==void 0)return t||null;let r=e.slice(yr.length).trim();if(!r)return fr.set(e,false),null;try{let n=new RegExp(r);return fr.set(e,n),n}catch{return console.warn(`[crawl] Ignoring invalid path matcher regex: ${e}`),fr.set(e,false),null}}function kn(e,t){let r=ec(t);return r?r.test(e):t.startsWith(yr)?false:e.startsWith(t)}function tc(e,t){return t.find(r=>kn(e,r))}function yo(e,t){let r=tc(t.pathname,e.blockedPathPrefixes);if(r)return r.startsWith(yr)?`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 rc(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 nc(e,t){return rc(e).has(t.hostname)}function sc(e){let t=Ne(e.trim()).replace(/\\\//g,"/").replace(/^['"]|['"]$/g,"").trim();return t=t.replace(/[)]+$/g,"").trim(),t=t.replace(/;.*$/g,"").trim(),t}function yn(e){let t=Ne(e).replace(/\\\//g,"/");return /[{}]|\bwindow\.|\blocation\.|\bincludes\(|\?\?null|\+|%7B|%7D|%22|<|>|\s/.test(t)||/[()[\]'"]/.test(t)}function oc(e){let t=Ne(e).replace(/\\\//g,"/").trim(),r=C.extname(t).toLowerCase();return co.has(r)||lo.has(r)?false:/^(?:this|[A-Za-z_$][\w$]*)\.[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/.test(t)}function ot(e){return Ta.some(t=>e.pathname.startsWith(t))}function oe(e,t,r=e.sourceOrigin,n,s="url",o=false){let i=sc(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(yn(i))return n?.ignore(s,t,"looks like JavaScript/code fragment",r),null;if(oc(i))return n?.ignore(s,t,"looks like JavaScript property access",r),null;try{let c=new URL(i,r);if(bo(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(!nc(e,c))return n?.ignore(s,c.toString(),"host not allowed",r),null;let a=yo(e,c);if(a&&!(o&&Hr(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 bo(e){try{return decodeURIComponent(e.pathname),decodeURIComponent(e.search),decodeURIComponent(e.hash),!1}catch{return true}}function ke(e,t,r,n){try{let s=new URL(e);return bo(s)?(t.reject(r,e,"malformed URL encoding",n),null):Ot(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 So(e){return co.has(C.extname(e.pathname).toLowerCase())}function Ke(e){return ot(e)?false:lo.has(C.extname(e.pathname).toLowerCase())}function Pt(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function Z(e,t){return !!(Pt(t)||t.pathname==="/robots.txt"||t.pathname==="/llms.txt"||t.pathname.toLowerCase().endsWith(".xsl")||e.assetPathPrefixes.length>0&&e.assetPathPrefixes.some(r=>kn(t.pathname,r))||So(t)||!Ke(t)&&!ot(t))}function Fe(e,t){let r=new URL(t),n=Ot(r.pathname);if(n===null)throw new Error(`Unsafe page URL pathname: ${r.pathname}`);return n.endsWith("/")?n+="index.html":C.extname(n)||(n+="/index.html"),Mt(e.outputDir,n)}function ic(e){if(!e.generated404RequestPath)return null;try{let t=new URL(e.generated404RequestPath,e.sourceOrigin);return !t.pathname.endsWith("/")&&C.extname(t.pathname)===""&&(t.pathname+="/"),t.hash="",t.toString()}catch{return null}}function vr(e,t){let r=ic(e);if(!r)return false;try{return new URL(t,e.sourceOrigin).toString()===r}catch{return false}}function _t(e,t){let r=new URL(t),n=Ot(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=Mt(e.outputDir,o),c=en(e.targetOrigin,o),a=`${c}${r.search}`;if(!s)return {originalPathname:o,originalFilePath:i,originalPublicUrl:c,originalPublicUrlWithSearch:a,preferQueryHashed:s};let d=C.extname(n),l=`${d?n.slice(0,-d.length):n}.${Ee(r.search)}${d||".bin"}`;return {originalPathname:o,originalFilePath:i,originalPublicUrl:c,originalPublicUrlWithSearch:a,preferQueryHashed:s,hashedPathname:l,hashedFilePath:Mt(e.outputDir,l),hashedPublicUrl:en(e.targetOrigin,l)}}function ac(e,t,r,n){let s=ke(t,r,"asset",n);return s?_t(e,s.toString()):null}function bn(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 Po(e){try{return await U.readFile(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}async function st(e){try{return await U.stat(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}function cc(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 Ro(e){let t=String(e["last-modified"]||"").trim();if(!t)return null;let r=Date.parse(t);return Number.isFinite(r)?r:null}function lc(e,t){let r=Ro(t),n=cc(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 uc(e,t){if(!t)return;let r=Ro(t);if(r===null)return;let n=await st(e);if(!n)return;let s=new Date(r);await U.utimes(e,n.atime,s).catch(()=>{});}function Sn(e,t){let r=C.extname(e.pathname).toLowerCase(),n=t["content-type"]||"";return Ia.has(r)||xa.some(s=>n.includes(s))&&!n.startsWith("image/")&&!n.includes("font")}function dc(e){try{let t=C.extname(new URL(e).pathname).toLowerCase();return t===".js"||t===".mjs"}catch{return false}}function Xe(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 pc(e){return Ee(e.toString("base64"),40)}function vo(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 Co(e,t,r){let n=e.assetVariantDigestsByOriginalPath.get(t);n||(n=new Set,e.assetVariantDigestsByOriginalPath.set(t,n)),n.add(r);}async function Pn(e,t,r,n,s,o,i){let c=_t(e,r),a=pc(n),d=c.originalFilePath,u=c.originalPublicUrlWithSearch;if(c.preferQueryHashed&&c.hashedFilePath&&c.hashedPublicUrl&&vo(s,c.originalFilePath,a)){let f=wo(e,t,r);f?(d=f.outputPath,u=f.publicUrl):(d=c.hashedFilePath,u=c.hashedPublicUrl);}bn(e,t,r,u,c.originalPublicUrl);let l=$e(d)?Buffer.from(sn(n.toString("utf8"),e,t,d),"utf8"):n,g=await Po(d),p=false;return (g===null||!g.equals(l))&&(await _e(d,l),p=true),o?.debug(`${p?"Saved":"Skipped unchanged"} asset output ${r}`,{phase:"download-assets",url:r,outputPath:d,bytes:l.length,wroteFile:p}),Co(s,c.originalFilePath,a),await uc(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:u,wroteFile:p}}function it(e,t,r,n,s){let o=()=>{let l=String(s??"").trim();if(!l)return {discoveredFrom:"",discoveredFromType:"unknown"};if(l==="seed-path"||l==="cli"||l==="generated-404-request-path")return {discoveredFrom:l,discoveredFromType:l};if(/^https?:\/\//i.test(l))try{let g=new URL(l);return Pt(g)?{discoveredFrom:l,discoveredFromType:"sitemap"}:Z(e,g)?{discoveredFrom:l,discoveredFromType:"asset"}:Ke(g)&&!ot(g)?{discoveredFrom:l,discoveredFromType:"page"}:{discoveredFrom:l,discoveredFromType:"url"}}catch{return {discoveredFrom:l,discoveredFromType:"url"}}return {discoveredFrom:l,discoveredFromType:"url"}},i=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,c=oe(e,r,i,n,"page");if(!c)return;if(!Ke(c)||So(c)){if(Z(e,c)){n.info(`Seeded/non-page URL queued as asset: ${c.toString()}`,{url:c.toString(),source:s}),G(e,t,c.toString(),n,s);return}n.reject("page",c.toString(),ot(c)?"asset/internal path cannot be page":"not page-like",s);return}let a=c.toString(),{discoveredFrom:d,discoveredFromType:u}=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:u,outputPath:Fe(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 G(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(!Z(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 br(e,t,r,n,s){let o=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,i=oe(e,r,o,n,"sitemap",Xn(s));if(!i)return;if(!Pt(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 mc(e){let t=Ne(e),r=[...t.matchAll(/<url>\s*([\s\S]*?)\s*<\/url>/gi)].map(n=>n[1]);return r.length>0?r.map(n=>{let s=n.match(/<loc>\s*([^<]+?)\s*<\/loc>/i);if(!s?.[1])return null;let o=n.match(/<lastmod>\s*([^<]+?)\s*<\/lastmod>/i);return {loc:s[1].trim(),lastmod:nt(o?.[1]??null)}}).filter(n=>!!n):[...t.matchAll(/<loc>\s*([^<]+?)\s*<\/loc>/gi)].map(n=>({loc:n[1].trim(),lastmod:null})).filter(n=>n.loc)}function gc(e){return e.split(",").map(t=>t.trim().split(/\s+/)[0]).filter(Boolean)}function fc(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 hc(e){return e.replace(/<script\b[^>]*>[\s\S]*?<\/script\s*>/gi,"")}function wc(e,t){if(t==="dom-style")return true;let r=(()=>{try{return C.extname(new URL(e).pathname).toLowerCase()}catch{return ""}})();return !(r===".js"||r===".mjs"||r===".map"||t.includes("javascript"))}function wr(e){return e.trim().replace(/\\/g,"/").replace(/^https?:\/\/[^/]+/i,"").replace(/^\/+/,"")}async function ko(e,t,r,n){let s=[];await U.mkdir(t,{recursive:true});let o=await U.readdir(e,{withFileTypes:true});for(let i of o){let c=C.join(e,i.name),a=C.join(t,i.name);if(i.isDirectory()){s.push(...await ko(c,a,r,n));continue}await xo(c,a,r,n)&&s.push(a);}return s}async function yc(e,t){try{let[r,n]=await Promise.all([U.stat(e),U.stat(t)]);if(r.size!==n.size)return !1;let[s,o]=await Promise.all([U.readFile(e),U.readFile(t)]);return s.equals(o)}catch{return false}}async function xo(e,t,r,n){let s=C.resolve(t);return r.has(s)?(n.debug("Skipped post-crawl copy over crawled page output",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):await yc(e,t)?(n.debug("Skipped unchanged post-crawl copy target",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):(await U.mkdir(C.dirname(t),{recursive:true}),await U.copyFile(e,t),true)}async function bc(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=>C.resolve(Fe(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 u=Ma(a);if(!u.resolvedPath){r.warn("Skipped post-crawl copy source because alias root is not configured",{sourcePath:a,alias:u.alias||"",requiredEnv:u.requiredEnv||"",prefixPath:d});continue}let l=u.resolvedPath,g=wr(d);if(!g){r.warn("Skipped post-crawl copy mapping with invalid export prefix",{sourcePath:a,prefixPath:d});continue}let p;try{p=await U.stat(l);}catch{r.warn("Skipped post-crawl copy source because it does not exist",{sourcePath:a,sourceAbs:l,prefixPath:d});continue}let f=C.resolve(e.outputDir,g);if(p.isDirectory()){let y=await ko(l,f,i,r);o.push(...y),s+=y.length,y.length>0&&r.progress(`Copied static directory to export: ${l} -> /${g}`,{phase:"copy-extra-paths",sourcePath:l,targetPath:`/${g}`,copiedItems:s,changedFiles:y.length});continue}let w=C.basename(l),m=d.endsWith("/")?C.join(f,w):f;await xo(l,m,i,r)&&(o.push(m),s++,r.progress(`Copied static file to export: ${l} -> /${wr(d.endsWith("/")?`${d}${w}`:d)}`,{phase:"copy-extra-paths",sourcePath:l,targetPath:`/${wr(d.endsWith("/")?`${d}${w}`:d)}`,copiedItems:s}));}return r.endMark("copy-extra-paths",{mappedSources:n.length,copiedItems:s}),o}function St(e,t,r,n,s){let o=new Set,i=Ne(r).replace(/\\\//g,"/"),c=s==="page-html"?hc(i):i,a=wc(t,s);if(a){for(let d of fc(c)){let u=oe(e,d,t,n,`${s}:css-url`);u&&Z(e,u)&&o.add(u.toString());}for(let d of c.matchAll(/@import\s+(?:url\()?\s*['"]?([^'"\s;]+)['"]?\s*\)?/gi)){let u=oe(e,d[1],t,n,`${s}:css-import`);u&&Z(e,u)&&o.add(u.toString());}}if(a||s==="page-html")for(let d of c.matchAll(/<\?xml-stylesheet[^>]+href=["']([^"']+)["'][^>]*\?>/gi)){let u=oe(e,d[1],t,n,`${s}:xml-stylesheet`);u&&Z(e,u)&&o.add(u.toString());}if(s==="page-html")for(let d of xs(i)){let u=d.startsWith("//")?`${new URL(e.sourceOrigin).protocol}${d}`:d;if(yn(u)){n.ignore(`${s}:serialized-url`,u,"looks like JavaScript/code fragment",t);continue}let l=oe(e,u,t,n,`${s}:serialized-url`);l&&Z(e,l)?o.add(l.toString()):l&&n.ignore("asset",l.toString(),"serialized URL did not pass safe asset path filter",t);}if(s!=="page-html"&&dc(t))for(let d of Is(i)){if(yn(d)||!/\.(?:js|mjs)(?:[?#]|$)/i.test(d))continue;let u=oe(e,d,t,n,`${s}:relative-asset`);u&&Z(e,u)&&o.add(u.toString());}return [...o]}function Io(e,t,r,n){let s=new Set,o=Ne(r);for(let i of o.matchAll(/href=["']([^"'#\s][^"']*?)["']/gi)){let c=i[1],a=oe(e,c,t,n,"page-link");a&&(yo(e,a)||Ke(a)&&!ot(a)&&!Z(e,a)&&s.add(a.toString()));}return [...s]}async function to(e,t,r,n,s,o,i=false,c=[]){let a=new Set(c),d=0,u=new Map,l=(g,p)=>{let f=[p],w=new Set;for(;f.length>0;){let m=f.pop();if(m===g)return true;w.has(m)||(w.add(m),f.push(...u.get(m)??[]));}return false};for(;r.sitemapQueue.length>0;){let g=r.sitemapQueue.shift();r.queuedSitemaps.delete(g);let p=ke(g,n,"sitemap","sitemap-queue");if(!p)continue;let f=p.toString();if(r.doneSitemaps.has(f))continue;r.doneSitemaps.add(f),n.sitemap(`Fetching sitemap ${f}`,{url:f});let w=true;try{let m=await t.get(f,{timeout:6e4});if(w=!1,!m.ok()){if(n.skip("sitemap",f,m.status()),i&&!a.has(f))throw new Error(`Required child content-sync sitemap returned HTTP ${m.status()}: ${f}`);continue}let h=await m.body(),y=m.url()||f,b=ke(y,n,"sitemap-response",f);if(!b)continue;let S=b.toString();r.successfulSitemaps.add(f),r.successfulSitemaps.add(S),a.has(f)&&(r.resolvedSitemapRoots.add(S),d+=1);let j=await Pn(e,r.assetMap,S,h,r,n);ue(s,e,j.outputPath),Ce(s,j.outputPath);let E=h.toString("utf8");/<sitemapindex(?:\s|>)/i.test(E)?r.sitemapIndexUrls.add(S):r.sitemapLeafUrls.add(S);let k=St(e,S,E,n,"sitemap");for(let T of k)G(e,r,T,n,S);let v=[];for(let T of mc(E)){let $=oe(e,T.loc,S,n,"sitemap-loc",!0);if(!$){if(i)throw new Error(`Content-sync sitemap contains an inadmissible location: ${T.loc}`);continue}if(Pt($)){if(l(S,$.toString()))throw new Error(`Sitemap index cycle detected: ${S} -> ${$.toString()}`);if(u.has(S)||u.set(S,new Set),u.get(S).add($.toString()),v.push($.toString()),i&&r.doneSitemaps.has($.toString())&&!r.successfulSitemaps.has($.toString()))throw new Error(`Required child content-sync sitemap was not resolved: ${$.toString()}`);br(e,r,$.toString(),n,S);}else if(i){if(Ke($)&&!Z(e,$)){let N=o.requiredUrls.has($.toString()),B=!!o.previousManifest.pages[$.toString()];if(!N&&!B)throw new Error(`Content-sync sitemap coverage is incomplete for ${$.toString()}.`)}n.ignore("sitemap-loc",$.toString(),"content-sync sitemap-only traversal does not enqueue urlset locations",S);}else Z(e,$)?G(e,r,$.toString(),n,S):Ke($)?(T.lastmod&&(r.sitemapLastmodByPage[$.toString()]=T.lastmod),it(e,r,$.toString(),n,S)):n.reject("sitemap-loc",$.toString(),"not page/sitemap/asset-like",S);}o.trackManifest&&(o.manifest.assets[S]={url:S,outputPath:j.outputPath,isText:!0,contentType:m.headers()["content-type"]??"application/xml",cacheControl:fn(m.headers()),changeToken:null,tokenSource:null,discoveredAssets:[...new Set([...k,...v])],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!1},await Ve(e,o));}catch(m){n.error(`Failed sitemap ${f}`,{url:f,error:String(m)});let h=i&&a.has(f)&&w;if(i&&!h)throw m}}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 Sc(e,t,r,n,s,o,i){let c=oe(e,n,e.sourceOrigin,s,"asset-fetch");if(!c||!Z(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,u=o.trackManifest?await hr(e,t,o,a,s):null,l=u?.token??null,g=on(d?.changeToken,u),p=o.strictTargeted&&(!u?.supported||!u.token)&&an(e.sourceOrigin,a),f=o.strictTargeted?o.manifest.assets[a]:void 0;if(f?.lastSeenRunId===o.runId&&await st(f.outputPath)){r.doneAssets.add(a);for(let m of f.discoveredAssets||[])G(e,r,m,s,a);ue(i,e,f.outputPath),yt(i,e,f.outputPath,f.cacheControl),f.isText&&Ce(i,f.outputPath),s.info(`Resumed completed content-sync asset ${a}`,{url:a,source:"content-sync-checkpoint"}),Xe(r,s);return}if(d&&(g||p)&&await st(d.outputPath)){o.manifest.assets[a]={...d,changeToken:l,tokenSource:u?.tokenSource??d.tokenSource,discoveredAssets:[...d.discoveredAssets||[]],lastSeenRunId:o.runId,unreferencedRuns:0},delete o.manifest.pages[a],r.doneAssets.add(a);for(let m of d.discoveredAssets||[])G(e,r,m,s,a);yt(i,e,d.outputPath,d.cacheControl),s.info(`Reused unchanged trusted asset ${a}.`,{url:a,tokenSource:u?.tokenSource??"unknown",phase:"download-assets",source:g?"asset-change-token-match":"content-sync-release-fingerprint"}),Xe(r,s);return}let w=(m,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||[])G(e,r,y,s,a);return s.warn(`Keeping previous incremental asset ${a} after fetch issue.`,{url:a,reason:m,preservedFromPreviousManifest:true,...h,phase:"download-assets"}),Xe(r,s),true};s.asset(`Fetching asset ${a}`,{url:a});try{let m=bt(e,r.assetMap,a),h=m?await st(m):null;if(m&&h&&!o.strictTargeted)try{let B=await se(`fetch asset headers ${a}`,6e4,()=>t.head(a,{timeout:6e4})),R=B.headers();if(B.ok()&&lc(h,R)){let O=await Po(m);if(O!==null){let de=B.url()||a,ie=ke(de,s,"asset-response",a);if(!ie)return;let H=ie.toString(),x=await Pn(e,r.assetMap,H,O,r,s,R);x.wroteFile&&(ue(i,e,x.outputPath),$e(x.outputPath)&&Ce(i,x.outputPath));let L=fn(R)||d?.cacheControl||null;yt(i,e,x.outputPath,L),r.doneAssets.add(a),s.info(`Reused existing asset ${a} based on response headers.`,{url:a,filePath:m,lastModified:R["last-modified"]||"",contentLength:R["content-length"]||"",phase:"download-assets"}),Xe(r,s);let q=Sn(c,R),J=d?.discoveredAssets?[...d.discoveredAssets]:[];if(q||d?.isText){let z=O.toString("utf8"),pe=St(e,H,z,s,"asset:cached");(pe.length>0||!d)&&(J=pe);for(let we of J)G(e,r,we,s,H);}if(o.trackManifest){let z=q||!!d?.isText;o.manifest.assets[a]={url:a,outputPath:x.outputPath,isText:z,contentType:gn(R)||d?.contentType||null,cacheControl:L,changeToken:u?.supported?u.token:null,tokenSource:u?.supported?u.tokenSource??null:null,discoveredAssets:J,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!z},delete o.manifest.pages[a],(o.enabled||o.strictTargeted)&&z&&await Ve(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()),S=y.url()||a,j=ke(S,s,"asset-response",a);if(!j)return;let E=j.toString(),k=y.headers(),v=await Pn(e,r.assetMap,E,b,r,s,k);v.wroteFile&&(ue(i,e,v.outputPath),$e(v.outputPath)&&Ce(i,v.outputPath));let T=fn(k);yt(i,e,v.outputPath,T),r.doneAssets.add(a),Xe(r,s);let $=Sn(c,k),N=[];if($){let B=b.toString("utf8");N=St(e,E,B,s,`asset:${C.extname(c.pathname).toLowerCase()||k["content-type"]||"unknown"}`);for(let R of N)G(e,r,R,s,E);}o.trackManifest&&(o.manifest.assets[a]={url:a,outputPath:v.outputPath,isText:$,contentType:gn(k),cacheControl:T,changeToken:u?.supported?u.token:null,tokenSource:u?.supported?u.tokenSource??null:null,discoveredAssets:N,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!$},delete o.manifest.pages[a],(o.enabled||o.strictTargeted)&&$&&await Ve(e,o));}catch(m){if(w("fetch-error",{error:String(m)}))return;if(s.error(`Failed asset ${a}`,{url:a,error:String(m)}),o.strictTargeted)throw m}finally{r.doneAssets.has(a)||(r.doneAssets.add(a),Xe(r,s));}}async function Pc(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 Sc(e,t,r,a,n,s,o));}});await Promise.all(c);}function Sr(e,t){let r=ge(e.outputDir,t);if(!r)throw new Error(`Remote export output is outside outputDir: ${t}`);return r}function Rc(e,t){return Sn(e,{"content-type":t})}function ro(e){return /(?:^|\/)index\.html?$|\.html?$/i.test(e)}async function vc(e,t,r,n,s,o,i,c,a){let d=Math.max(1,Math.min(100,a)),u=20;for(;r.assetQueue.length>0;){let l=r.assetQueue.splice(0,d*u);for(let w of l)r.queuedAssets.delete(w);await Cn(e,t,s,l,n);let g=[];for(let w of l){let m=s.manifest.assets[w],h=m?ge(e.outputDir,m.outputPath):null,y=h?i.objects[h]:void 0,b=s.changeTokenCache.get(w)??null;if(!(s.enabled&&m&&y?.kind==="asset"&&y.sourceUrl===w&&(on(m.changeToken,b)||(!b?.supported||!b.token)&&an(e.sourceOrigin,w)))||!m||!h||!y){g.push(w);continue}s.manifest.assets[w]={...m,changeToken:b?.token??null,tokenSource:b?.tokenSource??m.tokenSource,lastSeenRunId:s.runId,unreferencedRuns:0};for(let j of m.discoveredAssets||[])G(e,r,j,n,w);r.doneAssets.add(w),yt(o,e,m.outputPath,m.cacheControl),n.info(`Reused unchanged S3-native asset ${w}.`,{url:w,relativePath:h,tokenSource:b?.tokenSource??"release-fingerprint",phase:"download-assets",execution:"lambda"}),Xe(r,n);}if(g.length===0)continue;let p=[];for(let w=0;w<g.length;w+=u)p.push(g.slice(w,w+u));let f=await Promise.all(p.map(w=>c.fetch({urls:w,ignoreHttpsErrors:e.ignoreHttpsErrors,timeoutMs:6e4})));for(let w of f)for(let m of w.assets){let h=m.requestedUrl;try{if(m.outcome!=="fetched"||!m.object||!m.finalUrl){let T=m.error||`Remote asset fetch ended with ${m.outcome}.`;if(n.error(`Failed remote asset ${h}`,{url:h,status:m.httpStatus,outcome:m.outcome,error:T,execution:"lambda"}),s.strictTargeted)throw new Error(T);continue}let y=ke(m.finalUrl,n,"remote-asset-response",h);if(!y)continue;let b=_t(e,y.toString()),S=b.originalFilePath,j=b.originalPublicUrlWithSearch;if(b.preferQueryHashed&&b.hashedFilePath&&b.hashedPublicUrl&&vo(r,b.originalFilePath,m.object.sha256)){let T=wo(e,r.assetMap,y.toString());S=T?.outputPath??b.hashedFilePath,j=T?.publicUrl??b.hashedPublicUrl;}bn(e,r.assetMap,y.toString(),j,b.originalPublicUrl),y.toString()!==h&&bn(e,r.assetMap,h,j,b.originalPublicUrl),Co(r,b.originalFilePath,m.object.sha256);let E=Sr(e,S),k=m.contentType?.trim()||"application/octet-stream",v=Rc(y,k);wt(i,{relativePath:E,sourceUrl:h,kind:"asset",contentType:k,cacheControl:m.cacheControl,isText:v,object:m.object}),ue(o,e,S),v&&Ce(o,S),yt(o,e,S,m.cacheControl);for(let T of m.discoveredAssets)G(e,r,T,n,y.toString());r.stats.assetsSaved++,s.trackManifest&&(s.manifest.assets[h]={url:h,outputPath:S,isText:v,contentType:k,cacheControl:m.cacheControl,changeToken:null,tokenSource:null,discoveredAssets:[...m.discoveredAssets],lastCrawledAt:new Date().toISOString(),lastSeenRunId:s.runId,unreferencedRuns:0,rewriteComplete:!v},delete s.manifest.pages[h]),n.debug(`Remote asset fetch completed ${h}`,{url:h,finalUrl:y.toString(),relativePath:E,bytes:m.object.bytes,taskId:w.taskId,attempts:w.attempts,execution:"lambda"});}finally{r.doneAssets.add(h),Xe(r,n);}}}}async function Cc(e,t,r,n){for(let s of [...t.changedFiles].sort()){if(!zs(r,s))continue;let o=C.join(e.outputDir,s),i=await U.readFile(o).catch(u=>{throw new Error(`Coordinator-owned output is missing before S3 staging: ${s}`,{cause:u})}),c=$e(o),a=String(Ws.lookup(s)||"")||(c?"text/plain; charset=utf-8":"application/octet-stream"),d=await n.writeWorkspaceObject({key:`${r.workspace.prefix}jobs/${r.jobId}/coordinator/${Ee(s,40)}-${C.basename(s)}`,body:i,contentType:a,cacheControl:ro(s)?e.s3.htmlCacheControl:e.s3.assetCacheControl});wt(r,{relativePath:s,sourceUrl:null,kind:"generated",contentType:a,cacheControl:ro(s)?e.s3.htmlCacheControl:e.s3.assetCacheControl,isText:c,object:d});}}function kc(e){let t=Math.ceil(e/1e3);return `${t} second${t===1?"":"s"}`}function He(e){return Math.max(6e4,e.navigationTimeoutMs*2,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+15e3)}function Dt(e){return Math.max(15e3,Math.min(6e4,He(e)))}function To(e){return Math.max(15e3,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+1e4)}function xn(e){return e.stats.pagesCompleted}function Rn(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 mn(e,t,r){let n=Rn(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 Lt(e,t));},t);})])}finally{n!==void 0&&globalThis.clearTimeout(n);}}var Lt=class extends Error{constructor(r,n){super(`${r} timed out after ${kc(n)}.`);this.operation=r;this.timeoutMs=n;this.name="OperationTimeoutError";}operation;timeoutMs};async function xc(e,t){let r=To(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 u=document.body?.scrollHeight||0,l=document.documentElement?.scrollHeight||0,g=Math.max(u,l);o>=g+window.innerHeight&&a();},120);});},{maxDurationMs:r});}async function Ic(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 xc(e,t),await t.waitForTimeout(e.readiness.fallbackWaitMs);}async function Tc(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 Ao(e,t){return e.noJavaScriptRenderPathPrefixes.some(r=>kn(t,r))}async function Ac(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"],u=new Set,l=["srcset","data-srcset","data-lazy-srcset"],g=p=>{p.querySelectorAll("*").forEach(f=>{for(let m of d){let h=f.getAttribute(m);h&&a.add(h);}for(let m of l){let h=f.getAttribute(m);h&&h.split(",").forEach(y=>{let b=y.trim().split(/\s+/)[0];b&&a.add(b);});}let w=f.getAttribute("style");w&&u.add(w),f.shadowRoot&&g(f.shadowRoot);});};return g(document),{attrs:[...a],styles:[...u]}});for(let a of c.attrs)for(let d of gc(a)){let u=oe(e,d,r,n,"dom-attr");u&&Z(e,u)&&o.add(u.toString());}for(let a of c.styles)for(let d of St(e,r,a,n,"dom-style"))o.add(d);}let i=await t.content();for(let c of St(e,r,i,n,"page-html"))o.add(c);return [...o]}async function vn(e,t,r,n,s){let o=Fe(e,t);await _e(o,r,"utf8");let i=await U.stat(o);s?.debug(`Saved page output ${t}`,{phase:"save-pages",url:t,outputPath:o,bytes:i.size});let c=Pr(n,o),a=Math.min(n.stats.pagesQueued,xn(n)+1);s&&c&&(n.stats.pagesSaved%10===0||n.stats.pagesSaved===1)&&s.progress(`Page progress: processed ${a}, rendered ${n.stats.pagesRendered}, saved ${n.stats.pagesSaved}, discovered ${n.stats.pagesQueued}.`,{donePages:a,claimedPages:n.donePages.size,pagesRendered:n.stats.pagesRendered,pagesSaved:n.stats.pagesSaved,pagesQueued:n.stats.pagesQueued,pageQueue:n.pageQueue.length,phase:"save-pages"});}function Pr(e,t){let r=C.resolve(t);return e.savedPageOutputs.has(r)?false:(e.savedPageOutputs.add(r),e.stats.pagesSaved=e.savedPageOutputs.size,true)}async function Rr(e,t,r,n,s){let o=ge(e.outputDir,n);if(!o)return;if(s){let c=s.objects[o];c?.kind==="page"&&c.sourceUrl===r&&c.object?.bucket===s.workspace.bucket&&typeof c.object.key=="string"&&c.object.key.startsWith(s.workspace.prefix)&&Pr(t,n);return}(await st(n).catch(()=>null))?.isFile()&&Pr(t,n);}function Eo(e){let t=xn(e);return t%5===0||t===1||t===e.stats.pagesQueued}function $o(e,t){let r=xn(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 Ec(e){await os(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 $c(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 Ec(n),n}async function Oc(e,t,r,n,s,o,i,c,a,d){for(;t.pageQueue.length>0&&!(e.maxPages>0&&t.donePages.size>=e.maxPages);){let u=t.pageQueue.shift();if(!u)break;t.queuedPages.delete(u);try{let l=ke(u,r,"page","page-queue");if(!l)continue;let g=l.toString();if(u=g,t.queuedPages.delete(u),t.donePages.has(u))continue;t.donePages.add(u),o.enabled&&o.seenPages.add(u);let p=vr(e,u),f=Ao(e,l.pathname);if(!Ke(l)||Z(e,l)||ot(l)){if(Z(e,l)){r.info(`Remote worker redirected non-page URL to asset queue: ${u}`,{url:u,source:"remote-worker-guard"}),G(e,t,u,r,"remote-worker-guard");continue}r.reject("page",u,"guard rejected non-page URL before remote rendering","remote-worker");continue}let w=o.strictTargeted?o.manifest.pages[u]:void 0;if(w?.lastSeenRunId===o.runId&&await st(w.outputPath)){o.seenPages.add(u);for(let v of w.discoveredAssets||[])G(e,t,v,r,u);ue(i,e,w.outputPath),Ce(i,w.outputPath),await Rr(e,t,u,w.outputPath,d),r.info(`Resumed completed content-sync page ${u}`,{url:u,source:"content-sync-checkpoint"});continue}let m=await fo(e,s,t,o,u,r);if(o.enabled&&n!=="single-url"&&n!=="content-sync"){let v=o.manifest.pages[u];if(m.action==="reuse"&&v){for(let T of v.discoveredAssets)G(e,t,T,r,u);for(let T of v.discoveredPages)it(e,t,T,r,u);v.lastSeenRunId=o.runId,v.sitemapLastmod=nt(t.sitemapLastmodByPage[u])??v.sitemapLastmod,m.changeToken?.supported&&(v.changeToken=m.changeToken.token,v.tokenSource=m.changeToken.tokenSource??v.tokenSource),await Rr(e,t,u,v.outputPath,d),r.info(`Incremental reuse skipped unchanged page ${u}`,{url:u,mode:"incremental",reason:m.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}r.page(`Rendering remotely ${u}`,{url:u,execution:"lambda"});let h=await a.render({sourceOrigin:e.sourceOrigin,url:u,options:{ignoreHttpsErrors:e.ignoreHttpsErrors,javaScriptEnabled:!f,viewport:e.viewport,navigationTimeoutMs:e.navigationTimeoutMs,autoScrollTimeoutMs:To(e),readiness:e.readiness},downloadHtml:!d}),y=h.page,b=y.finalUrl?ke(y.finalUrl,r,"remote-final-response",u)?.toString():void 0;if(n==="content-sync"&&b&&b!==g){if(o.deleteOnMissingUrls.has(u)){await Wt(e,o,i,u,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive route redirected to ${b}`,{url:u,finalResponseUrl:b,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${u} -> ${b}`)}let S=y.httpStatus;if(p){if(S!==404){r.error(`Generated 404 request path returned unexpected status for ${u}`,{url:u,expectedStatus:404,status:S,requestPath:e.generated404RequestPath,execution:"lambda"});continue}}else if(S!==null&&S>=400){if(n==="content-sync"&&(S===404||S===410)&&o.deleteOnMissingUrls.has(u)){await Wt(e,o,i,u,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive page ${u}`,{url:u,status:S,source:"content-sync-tombstone"});continue}if(n==="content-sync")throw new Error(`Required content-sync route returned HTTP ${S}: ${u}`);r.skip("page",u,S,t.pageDiscoverySources.get(u));continue}t.stats.pagesRendered++;let j=new Set;if(!d){if(h.html===null)throw new Error("Remote render did not return HTML for local output mode.");for(let v of St(e,g,h.html,r,"remote-page-html"))j.add(v);}for(let v of [...y.discoveredAssets,...y.networkUrls]){let T=oe(e,v,g,r,"remote-render");T&&Z(e,T)&&j.add(T.toString());}for(let v of j)G(e,t,v,r,u);let E=n!=="single-url"&&n!=="content-sync"&&!p?[...new Set([...!d&&h.html!==null?Io(e,u,h.html,r):[],...y.discoveredPages])]:[];for(let v of E)it(e,t,v,r,u);for(let v of y.cookies){let T=v.sameSite==="Strict"||v.sameSite==="Lax"||v.sameSite==="None"?v.sameSite:"Unspecified",$=!Number.isFinite(v.expires)||v.expires<=0;c.add({name:v.name,domain:v.domain,path:v.path||"/",expires:$?null:v.expires,session:$,secure:v.secure,httpOnly:v.httpOnly,sameSite:T,partitioned:v.partitioned===!0});}let k=Fe(e,u);if(d){if(!y.html)throw new Error("Remote render completed without a workspace HTML object.");wt(d,{relativePath:Sr(e,k),sourceUrl:u,kind:"page",contentType:y.contentType?.trim()||"text/html; charset=utf-8",cacheControl:e.s3.htmlCacheControl,isText:!0,object:y.html}),Pr(t,k);}else {if(h.html===null)throw new Error("Remote render did not return HTML for local output mode.");await vn(e,u,h.html,t,r);}ue(i,e,k),Ce(i,k),o.trackManifest&&(o.manifest.pages[u]={url:u,outputPath:k,contentType:y.contentType?.trim()||null,changeToken:m.changeToken?.supported?m.changeToken.token:null,tokenSource:m.changeToken?.supported?m.changeToken.tokenSource??null:null,sitemapLastmod:nt(t.sitemapLastmodByPage[u])??null,discoveredPages:E,discoveredAssets:[...j],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Ve(e,o)),r.debug(`Remote render completed ${u}`,{url:u,taskId:h.taskId,attempts:h.attempts,execution:"lambda"});}catch(l){if(r.error(`Failed remote page ${u}`,{url:u,error:String(l),execution:"lambda",...l instanceof be?{status:l.status,attempts:l.attempts}:{}}),l instanceof be){t.pageHttpFailures.set(u,l);continue}if(o.strictTargeted)throw l}finally{t.stats.pagesCompleted++,Eo(t)&&$o(t,r);}}}async function Mc(e,t,r,n,s,o,i,c){let a=await chromium.launch({headless:true}),d=Rn(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=Rn(a),d.pid!==null&&r.debug(`Relaunched crawl worker browser PID ${d.pid}`,{browserPid:d.pid}));let u=a;if(e.maxPages>0&&t.donePages.size>=e.maxPages)break;let l=t.pageQueue.shift();if(!l)break;t.queuedPages.delete(l);let g=null,p=null,f=!1,w=null,m=[],h=new Set;try{let y=ke(l,r,"page","page-queue");if(!y)continue;let b=y.toString();if(l=b,t.queuedPages.delete(l),t.donePages.has(l))continue;t.donePages.add(l),o.enabled&&o.seenPages.add(l);let S=vr(e,l),j=Ao(e,y.pathname);if(!Ke(y)||Z(e,y)||ot(y)){if(Z(e,y)){r.info(`Worker redirected non-page URL to asset queue: ${l}`,{url:l,source:"worker-guard"}),G(e,t,l,r,"worker-guard");continue}r.reject("page",l,"guard rejected non-page URL before rendering","worker");continue}let E=o.strictTargeted?o.manifest.pages[l]:void 0;if(E?.lastSeenRunId===o.runId&&await st(E.outputPath)){o.seenPages.add(l);for(let x of E.discoveredAssets||[])G(e,t,x,r,l);ue(i,e,E.outputPath),Ce(i,E.outputPath),await Rr(e,t,l,E.outputPath),r.info(`Resumed completed content-sync page ${l}`,{url:l,source:"content-sync-checkpoint"});continue}let k=await fo(e,s,t,o,l,r);if(o.enabled&&n!=="single-url"&&n!=="content-sync"){let x=o.manifest.pages[l];if(k.action==="reuse"&&x){for(let L of x.discoveredAssets)G(e,t,L,r,l);for(let L of x.discoveredPages)it(e,t,L,r,l);x.lastSeenRunId=o.runId,x.sitemapLastmod=nt(t.sitemapLastmodByPage[l])??x.sitemapLastmod,k.changeToken?.supported&&(x.changeToken=k.changeToken.token,x.tokenSource=k.changeToken.tokenSource??x.tokenSource),await Rr(e,t,l,x.outputPath),r.info(`Incremental reuse skipped unchanged page ${l}`,{url:l,mode:"incremental",reason:k.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}g=await se(`create page context ${l}`,He(e),()=>$c(u,e,j)),p=await se(`create page ${l}`,He(e),()=>g.newPage()),m=await g.cookies();let v=!1,T=new Set;await p.route("**/*",async x=>{let L=x.request();try{let q=oe(e,L.url(),e.sourceOrigin,r,"network-request");q&&Z(e,q)&&(T.add(q.toString()),G(e,t,q.toString(),r,l));}catch{}if(L.isNavigationRequest()&&L.frame()===p.mainFrame())try{let q=new URL(L.url()),J=new URL(b);if(q.hash="",J.hash="",q.href===J.href){if(v){r.warn(`Blocked same-page navigation/reload for ${l}`,{url:l,requestUrl:L.url()});try{await x.abort("aborted");}catch(z){r.debug(`Ignored canceled route while blocking reload for ${l}`,{url:l,error:String(z)});}return}v=!0;}}catch{}try{await x.continue();}catch(q){r.debug(`Ignored canceled network route for ${l}`,{url:l,requestUrl:L.url(),error:String(q)});}}),p.on("response",x=>{try{let q=oe(e,x.url(),e.sourceOrigin,r,"network-response");q&&Z(e,q)&&(T.add(q.toString()),G(e,t,q.toString(),r,l));}catch{}let L=x.headerValues("set-cookie").then(q=>{for(let J of q)c.add(Os(J,x.url()));}).catch(()=>{}).then(()=>{});h.add(L),L.finally(()=>h.delete(L));}),r.page(`Rendering ${l}`,{url:l});let $=null,N=null;try{if($=await p.goto(l,{waitUntil:"domcontentloaded",timeout:e.navigationTimeoutMs}),$&&$.ok())try{N=await se(`read response body ${l}`,He(e),()=>$.text());}catch{N=null;}}catch(x){let L=t.pageHttpFailures.get(l);if(L){let q=(t.pageHttpAttempts.get(l)??0)+1;throw t.pageHttpAttempts.set(l,q),new be(l,L.status,q,`Retry navigation failed: ${String(x)}`)}r.warn(`Navigation issue for ${l}; saving current DOM if available`,{url:l,error:String(x)});}let B=$?.status();if(or(B)){let x=(t.pageHttpAttempts.get(l)??0)+1;throw t.pageHttpAttempts.set(l,x),new be(l,B,x)}if($)t.pageHttpFailures.delete(l);else if(t.pageHttpFailures.has(l)){let x=t.pageHttpFailures.get(l),L=(t.pageHttpAttempts.get(l)??0)+1;throw t.pageHttpAttempts.set(l,L),new be(l,x.status,L,"Retry navigation returned no HTTP response.")}if(n==="content-sync"&&$){let x=ke($.url(),r,"content-sync-final-response",l)?.toString();if(x&&x!==b){if(o.deleteOnMissingUrls.has(l)){await Wt(e,o,i,l,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive route redirected to ${x}`,{url:l,finalResponseUrl:x,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${l} -> ${x}`)}}if(S){let x=$?.status()??null;if(x!==404){r.error(`Generated 404 request path returned unexpected status for ${l}`,{url:l,expectedStatus:404,status:x,requestPath:e.generated404RequestPath});continue}r.info(`Capturing rendered 404 page for ${l}`,{url:l,status:x,outputPath:Fe(e,l)});}else if($&&!$.ok()){if(n==="content-sync"&&($.status()===404||$.status()===410)&&o.deleteOnMissingUrls.has(l)){await Wt(e,o,i,l,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive page ${l}`,{url:l,status:$.status(),source:"content-sync-tombstone"}),await p.close();continue}if(n==="content-sync")throw new Error(`Required content-sync route returned HTTP ${$.status()}: ${l}`);r.skip("page",l,$.status(),t.pageDiscoverySources.get(l)),await p.close();continue}j?(r.info(`Rendering without JS execution for ${l}`,{url:l,mode:"no-js"}),await se(`wait for rendered page ${l}`,He(e),()=>Tc(e,p))):await se(`wait for rendered page ${l}`,He(e),()=>Ic(e,p)),t.stats.pagesRendered++;let R=await se(`extract rendered assets ${l}`,He(e),()=>Ac(e,p,b,r,!j));for(let x of R)G(e,t,x,r,l);let O=[...new Set([...R,...T])],de=N??await se(`capture rendered html ${l}`,He(e),()=>Qr(p)),ie=n!=="single-url"&&n!=="content-sync"&&!S?Io(e,l,de,r):[];if(n!=="single-url"&&n!=="content-sync"&&!S)for(let x of ie)it(e,t,x,r,l);await vn(e,l,de,t,r);let H=Fe(e,l);if(ue(i,e,H),Ce(i,H),o.trackManifest){let x=$?.headers();o.manifest.pages[l]={url:l,outputPath:H,contentType:gn(x),changeToken:k.changeToken?.supported?k.changeToken.token:null,tokenSource:k.changeToken?.supported?k.changeToken.tokenSource??null:null,sitemapLastmod:nt(t.sitemapLastmodByPage[l])??null,discoveredPages:ie,discoveredAssets:O,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Ve(e,o);}}catch(y){if(y instanceof be){t.pageHttpFailures.set(l,y),y.attempts<un?(w=ir(y.attempts),r.warn(`Retrying page after HTTP ${y.status}: ${l}`,{url:l,status:y.status,attempt:y.attempts,maxAttempts:un,delayMs:w,reason:y.message,execution:"local"})):(t.pageHttpFailures.set(l,y),r.error(`Failed page after HTTP retries ${l}`,{url:l,status:y.status,attempts:y.attempts,error:String(y),execution:"local"}));continue}if(f=y instanceof Lt,o.strictTargeted)throw r.error(`Failed required content-sync page ${l}`,{url:l,error:String(y)}),y;if(f)r.warn(`Skipped partial DOM for timed-out page ${l}`,{url:l,error:String(y),recovery:"recycle-worker-browser"});else try{let b=p?await se(`capture partial html ${l}`,He(e),()=>Qr(p)):"";if(b&&b.trim()){await vn(e,l,b,t,r);let S=Fe(e,l);if(ue(i,e,S),Ce(i,S),o.trackManifest){let j=o.manifest.pages[l]??o.previousManifest.pages[l];o.manifest.pages[l]={url:l,outputPath:S,contentType:j?.contentType??null,changeToken:null,tokenSource:null,sitemapLastmod:nt(t.sitemapLastmodByPage[l])??j?.sitemapLastmod??null,discoveredPages:j?.discoveredPages?[...j.discoveredPages]:[],discoveredAssets:j?.discoveredAssets?[...j.discoveredAssets]:[],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Ve(e,o);}r.warn(`Saved partial DOM for ${l}`,{url:l,error:String(y)});}}catch(b){b instanceof Lt&&(f=!0),r.error(`Could not save partial DOM for ${l}`,{url:l,error:String(b)});}r.error(`Failed page ${l}`,{url:l,error:String(y)});}finally{if(!f&&p&&await se(`drain network routes ${l}`,Dt(e),()=>p.unrouteAll({behavior:"wait"})).catch(y=>{f=!0,r.warn(`Could not drain network routes for ${l}`,{url:l,browserPid:d.pid,error:String(y),recovery:"recycle-worker-browser"});}),f&&(a=null,await mn(u,r,`page operation timeout for ${l}`),await u.close().catch(()=>{})),h.size>0&&await Promise.allSettled([...h]),!f&&g)try{let y=await g.cookies();c.addAll(Ms(m,y));}catch{}w===null&&(t.stats.pagesCompleted++,Eo(t)&&$o(t,r)),!f&&p&&await se(`close page ${l}`,Dt(e),()=>p.close()).catch(y=>{f=!0,r.warn(`Timed out while closing page ${l}`,{url:l,browserPid:d.pid,error:String(y),recovery:"recycle-worker-browser"});}),!f&&g&&await se(`close page context ${l}`,Dt(e),()=>g.close()).catch(y=>{f=!0,r.warn(`Timed out while closing page context for ${l}`,{url:l,browserPid:d.pid,error:String(y),recovery:"recycle-worker-browser"});}),f&&a===u&&(a=null,await mn(u,r,`page cleanup timeout for ${l}`),await u.close().catch(()=>{})),w!==null&&(await setTimeout$1(w),t.donePages.delete(l),t.queuedPages.add(l),t.pageQueue.push(l));}}}finally{if(a){let u=a;await se("close worker browser",Dt(e),()=>u.close()).catch(l=>(r.warn("Timed out while closing crawl worker browser",{browserPid:d.pid,error:String(l)}),mn(u,r,"browser.close timeout")));}}}async function jc(e){try{let t=await U.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 no(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 Dc(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 Oo(e){try{let t=await U.readFile(e,"utf8"),r=JSON.parse(t);return r&&typeof r=="object"&&!Array.isArray(r)?r:null}catch{return null}}function so(e){let t=String(e.originalFileName||"").toLowerCase();return t.endsWith("errors.jsonl")?0:t.endsWith("errors.json")?1:2}function Uc(e,t){let r=Array.isArray(t?.artifacts)?t.artifacts.filter(n=>String(n?.role||"").trim()==="errors"&&String(n?.storedFileName||"").trim()!=="").sort((n,s)=>so(n)-so(s)).map(n=>C.join(e,String(n.storedFileName))):[];return [...new Set([...r,C.join(e,"errors.jsonl"),C.join(e,"errors.json"),C.join(e,"errors.jsonl.gz"),C.join(e,"errors.json.gz")])]}async function oo(e){let t=await Oo(C.join(e,"job.json"));for(let r of Uc(e,t)){let n=await jc(r);if(n.length>0)return n}return []}async function Lc(e){let t=C.join(e.logDir,"archive"),r;try{r=await U.readdir(t);}catch{return ""}let n="",s=0;for(let o of r){let i=C.join(t,o);if(!(await U.stat(i).catch(()=>null))?.isDirectory())continue;let a=await Oo(C.join(i,"job.json"));if(!Fc(a))continue;let d=Dc(a);d>=s&&(s=d,n=i);}return n}async function Wc(e){let t=await Lc(e),r=t!==""?await oo(t):await oo(e.logDir),n=new Set;for(let s of r)if(no(s.error)||no(s.message)){let o=s.url;typeof o=="string"&&o&&n.add(o);}return [...n]}async function qc(e){let t=[];for(let r of e)if(r.startsWith("@")){let n=r.slice(1),s=await U.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 _c(e){if(!e)throw new Error("Content-sync crawl requires an immutable impact plan.");let t=JSON.parse(await U.readFile(e,"utf8"));return xt(t),t}function Nc(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 Ft(e,t,r){let n=r==="page"?Fe(e,t):_t(e,t).originalFilePath;return ge(e.outputDir,n)}async function Wt(e,t,r,n,s){let o=t.manifest.pages[n];if(!o)return;let i=ge(e.outputDir,o.outputPath);if(!i)throw new Error(`Manifest-owned page path escaped output root: ${n}`);let c=C.resolve(o.outputPath);Object.entries(t.manifest.pages).some(([d,u])=>d!==n&&!s.has(d)&&C.resolve(u.outputPath)===c)||(await U.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=ge(e.outputDir,o.outputPath);if(!i)throw new Error(`Manifest-owned resource path escaped output root: ${n}`);let c=C.resolve(o.outputPath);Object.entries(t.manifest.assets).some(([d,u])=>d!==n&&!s.has(d)&&C.resolve(u.outputPath)===c)||(await U.rm(o.outputPath,{force:true}),r.deletedFiles.add(i)),delete t.manifest.assets[n];}function Hc(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{Pt(new URL(i))&&n.push(i);}catch{}}return r}async function Vc(e,t,r,n,s){let o=Hc(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=ge(e.outputDir,c.outputPath);if(!a)throw new Error(`Manifest-owned sitemap path escaped output root: ${i}`);await U.rm(c.outputPath,{force:true}),n.deletedFiles.add(a),delete r.manifest.assets[i];}await Ve(e,r);}async function Kc(e){try{let t=await U.readFile(C.join(e.outputDir,"asset-map.json"),"utf8");return JSON.parse(t)}catch{return {}}}async function zc(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 U.mkdir(e,{recursive:true}),await Promise.all(t.map(async r=>{try{await U.unlink(C.join(e,r));}catch(n){if(n.code!=="ENOENT")throw n}}));}function io(e,t,r,n,s){let o=oe(e,r,e.sourceOrigin,n,"manual-url");o&&(Pt(o)?br(e,t,o.toString(),n,s):Z(e,o)?G(e,t,o.toString(),n,s):Ke(o)?it(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 Ie(void 0,{onRemotePublisherConfigDiagnostic:P=>{t.push(P);}}),n=Tt(r.sitemapPaths),s=t.at(-1)??null,o=Aa(e),i=o.mode==="content-sync"?await _c(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 Wc(r)),o.mode==="single-url"&&(c=await qc(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 u=(o.mode==="full"||o.mode==="single-url"||o.mode==="content-sync")&&!o.resumeRewrite,l=o.mode!=="full"||o.resumeRewrite||d,g=l?await Kc(r):{},p=d||o.mode==="single-url"||o.mode==="content-sync"?await Ua(r):po(),f=new Set(c),w=new Set;if(i){for(let P of i.archiveFamilies)if(P.foundPosts===0)for(let F of P.pageUrls)w.add(F);for(let P of Object.keys(p.pages))Nc(P,i)&&!f.has(P)&&(c.push(P),w.add(P));}let m={enabled:d,trackManifest:u,manifest:wn(p),previousManifest:wn(p),runId:i?.jobId??`${Date.now()}`,seenPages:new Set,changeTokenCache:new Map,pageChangeTokenPrimePromise:null,manifestSaveChain:Promise.resolve(),manifestPendingChanges:0,lastManifestSavedAt:Date.now(),strictTargeted:!!i,requiredUrls:f,deleteOnMissingUrls:w};await zc(r.logDir);let h=new er(r.logDir,r.logLevel),y=o.remoteRender??r.remoteWorkers?.render?.enabled===true,b=dr({explicitPath:o.remoteWorkerConfigPath,runtimeDir:qt()}),S=null,j=null,E=null,k=null,v=Math.max(1,r.concurrency),T=1,$=r.remoteWorkers?.rewrite?.enabled===true,N=r.remoteWorkers?.deploy?.enabled===true,B=y&&$&&N&&!o.resumeRewrite;if(!B&&o.mode==="full"&&!o.resumeRewrite&&await U.rm(pr(r),{force:true}),y&&!o.resumeRewrite||$){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 P=await sr(b),F=false;if(E=new ht(P,{},{onProgress:M=>{if(M.operation==="rewrite"){h.debug("Lambda rewrite task progress.",{remoteWorkerOperation:M.operation,remoteWorkerTaskId:M.taskId,remoteWorkerStatus:M.status,remoteWorkerCompletedItems:M.completedItems,remoteWorkerFailedItems:M.failedItems,remoteWorkerTotalItems:M.totalItems,remoteWorkerUpdatedAt:M.updatedAt});return}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=>{F||(F=true,h.warn("Lambda progress status could not be read; task execution continues.",{error:M.message}));}}),B){if(!P.functions.asset)throw new Error("Full Lambda delegation requires an asset worker. Redeploy the Lambda worker stack and reinstall remote-workers.json.");let M=l?await gr(r):null;k=Ks(`crawl-${Ee(m.runId,40)}`,P,M),j=new cr({client:E,jobId:k.jobId,maxAttempts:2}),h.info("S3-native Lambda data plane is enabled.",{execution:"lambda",assetWorker:P.functions.asset,workspaceBucket:P.workspace.bucket,workspacePrefix:P.workspace.prefix});}}if(y&&!o.resumeRewrite){if(!E)throw new Error("Remote render worker client was not initialized.");v=Math.max(1,Math.min(100,B?r.concurrency:r.remoteWorkers?.render?.concurrency??r.concurrency)),T=Math.max(1,Math.min(20,r.remoteWorkers?.render?.batchSize??5)),S=new ar({client:E,jobId:`crawl-${Ee(m.runId,40)}`,maxAttempts:r.remoteWorkers?.render?.maxAttempts??2,batchSize:T,maxConcurrentBatches:v,onRetry:P=>{h.warn(`Retrying remote render for ${P.url}`,{url:P.url,attempt:P.attempt,maxAttempts:P.maxAttempts,reason:P.reason,status:P.status,delayMs:P.delayMs,execution:"lambda"});}}),h.info("Lambda page rendering is enabled.",{remoteWorkerConfigPath:C.resolve(b),concurrency:v,maxAttempts:r.remoteWorkers?.render?.maxAttempts??2,batchSize:T,execution:"lambda"});}if(s){let P={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.",P):h.warn("Remote WP Suite publisher configuration is unavailable or has no active subscription type.",P);}if(a&&!d){let P="Incremental crawl requires an active remote publisher subscription; refusing to fall back to a destructive full crawl.";throw h.error(P,{requestedMode:o.crawlMode,subscriptionType:r.subscriptionType??null,source:"incremental-subscription-guard"}),await h.flush(),new Error(P)}let R={pageQueue:[],queuedPages:new Set,donePages:new Set,savedPageOutputs:new Set,pageHttpAttempts:new Map,pageHttpFailures:new Map,pageDiscoverySources:new Map,assetQueue:[],queuedAssets:new Set,doneAssets:new Set,sitemapQueue:[],queuedSitemaps:new Set,doneSitemaps:new Set,successfulSitemaps:new Set,resolvedSitemapRoots:new Set,sitemapIndexUrls:new Set,sitemapLeafUrls:new Set,sitemapLastmodByPage:{},assetMap:{...g},assetVariantDigestsByOriginalPath:new Map,stats:{pagesQueued:0,pagesRendered:0,pagesCompleted:0,assetsQueued:0,sitemapsQueued:0,assetsSaved:0,pagesSaved:0}},O=Da(),de=new rr,ie=Ea();if(ie&&!o.resumeRewrite&&await nr(ie,de.artifact()),await Ze(r),await Xr(r,{startedAt:new Date().toISOString(),outputDir:C.resolve(r.outputDir)}),!o.resumeRewrite&&r.pageCache?.purgeBeforeCrawl===true){h.mark("purge-page-cache");let P=await request.newContext({ignoreHTTPSErrors:r.ignoreHttpsErrors});try{let F=await Hs(r,P);h.info("WordPress page cache was purged before crawl discovery.",{provider:F.provider,scope:F.scope,purgedAt:F.purgedAt??null}),h.endMark("purge-page-cache",{provider:F.provider,scope:F.scope});}catch(F){throw h.error("WordPress page-cache purge failed; crawl stopped before changing export output.",{error:F instanceof Error?F.message:String(F)}),await h.flush(),F}finally{await P.dispose();}}if(l||await U.rm(r.outputDir,{recursive:true,force:true}),await U.mkdir(r.outputDir,{recursive:true}),l||await Xr(r,{startedAt:new Date().toISOString(),outputDir:C.resolve(r.outputDir)}),i){let P=new Set([...i.deleteUrls,...m.deleteOnMissingUrls]);for(let M of i.deleteUrls)await Wt(r,m,O,M,P);let F=new Set(i.resourceDeleteUrls);for(let M of i.resourceDeleteUrls)await Bc(r,m,O,M,F);}if(o.resumeRewrite)h.info("Resuming final rewrite from existing output.",{phase:"rewrite-text",mode:o.mode,crawlMode:o.crawlMode});else {let P=await request.newContext({ignoreHTTPSErrors:r.ignoreHttpsErrors});try{if(h.mark("discovery"),o.mode==="full"?(n.forEach(M=>br(r,R,M,h)),await to(r,P,R,h,O,m),r.seedPaths.forEach(M=>io(r,R,M,h,"seed-path")),r.generated404RequestPath&&it(r,R,r.generated404RequestPath,h,"generated-404-request-path")):(c.forEach(M=>io(r,R,M,h,"cli")),i?.sitemapUrls.forEach(M=>br(r,R,M,h,"content-sync-impact")),await to(r,P,R,h,O,m,o.mode==="content-sync",i?.sitemapUrls??[]),i&&await Vc(r,R,m,O,i.sitemapUrls)),h.endMark("discovery",{pages:R.pageQueue.length,assets:R.assetQueue.length,sitemaps:R.sitemapQueue.length}),h.summary(`Queued ${R.pageQueue.length} pages, ${R.assetQueue.length} assets, ${R.sitemapQueue.length} sitemaps.`,{mode:o.mode,crawlMode:o.crawlMode,queuedPages:R.pageQueue.length,queuedAssets:R.assetQueue.length,queuedSitemaps:R.sitemapQueue.length}),o.mode==="retry-timeouts"&&R.pageQueue.length===0&&R.assetQueue.length===0&&R.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(),ie&&await nr(ie,de.artifact());return}h.mark("render-pages");let F=Array.from({length:S?v*T:v},()=>S?Oc(r,R,h,o.mode,P,m,O,de,S,k??void 0):Mc(r,R,h,o.mode,P,m,O,de));if(await Promise.all(F),R.pageHttpFailures.size>0&&await h.flush(),Ls(R.pageHttpFailures),ie&&await nr(ie,de.artifact()),h.endMark("render-pages",{pages:R.stats.pagesCompleted}),h.mark("download-assets"),h.progress("Asset download phase started.",{phase:"download-assets",doneAssets:R.doneAssets.size,assetsQueued:R.stats.assetsQueued,assetQueue:R.assetQueue.length}),k&&j?await vc(r,P,R,h,m,O,k,j,r.concurrency):(await Cn(r,P,m,[...R.assetQueue],h),await Pc(r,P,R,h,m,O)),h.endMark("download-assets",{assets:R.doneAssets.size}),m.enabled&&r.maxPages===0&&(await Ha(r,R,m,h,O),o.mode==="full"&&await Va(r,m,h,O)),o.mode==="full"){let M=await bc(r,R,h);for(let V of M)if(ue(O,r,V),k&&E){let Q=Sr(r,V),ae=await U.readFile(V),A=$e(V),W=String(Ws.lookup(Q)||"")||(A?"text/plain; charset=utf-8":"application/octet-stream"),I=await E.writeWorkspaceObject({key:`${k.workspace.prefix}jobs/${k.jobId}/copied/${Ee(Q,40)}-${C.basename(Q)}`,body:ae,contentType:W});wt(k,{relativePath:Q,sourceUrl:null,kind:"generated",contentType:I.contentType,cacheControl:null,isText:A,object:I}),A&&Ce(O,V);}}}finally{await se("dispose crawl request context",Dt(r),()=>P.dispose()).catch(F=>{h.warn("Timed out while disposing crawl request context",{error:String(F)});});}}if(m.trackManifest&&await Ve(r,m,true),k)for(let P of O.deletedFiles)Js(k,P);let H=JSON.stringify(R.assetMap,null,2);if(await U.writeFile(C.join(r.outputDir,"asset-map.json"),H,"utf8"),k&&E){let P=await E.writeWorkspaceObject({key:`${k.workspace.prefix}jobs/${k.jobId}/generated/asset-map.json`,body:H,contentType:"application/json; charset=utf-8"});wt(k,{relativePath:"asset-map.json",sourceUrl:null,kind:"generated",contentType:"application/json; charset=utf-8",cacheControl:r.s3.assetCacheControl,isText:true,object:P});}JSON.stringify(g,null,2)!==JSON.stringify(R.assetMap,null,2)&&ue(O,r,C.join(r.outputDir,"asset-map.json")),k&&E&&await Cc(r,O,k,E);let x=o.mode==="single-url"||o.mode==="content-sync"?Xa(r,R):m.enabled&&!o.resumeRewrite?Ka(r,m,O,g,R.assetMap):void 0;h.mark("rewrite-text");let L=o.mode==="single-url"||o.mode==="content-sync"?`Rewriting text files touched by URL crawl: 0/${x?.length??0}`:"Rewriting text files...";h.progress(L,{phase:"rewrite-text",index:0,totalFiles:x?.length,changedTextFiles:0}),await jt(L);let q=Date.now(),J={files:x,previousAssetMap:g,onStart:({totalFiles:P,workerCount:F})=>{h.info(`Rewrite phase scanning ${P} text files.`,{phase:"rewrite-text",totalFiles:P,workerCount:F});},onProgress:async({index:P,totalFiles:F,changedTextFiles:M,file:V,changed:Q})=>{Q&&ue(O,r,C.join(r.outputDir,V)),h.checkpoint(`Rewriting text file ${P}/${F}`,{phase:"rewrite-text",index:P,totalFiles:F,changedTextFiles:M,file:V}),h.debug(`${Q?"Rewrote":"Checked unchanged"} text file ${P}/${F}: ${V}`,{phase:"rewrite-text",index:P,totalFiles:F,changedTextFiles:M,file:V,changed:Q});let ae=Date.now();if(!(P===1||P===F||P%50===0||ae-q>=5e3))return;q=ae;let W=`Rewriting text files: ${P}/${F}`;h.progress(W,{phase:"rewrite-text",index:P,totalFiles:F,changedTextFiles:M,file:V}),await jt(W);}},z;if(k&&E){let P=x?.map(M=>Sr(r,C.isAbsolute(M)?M:C.join(r.outputDir,M))),F=Object.values(k.objects).filter(M=>M.isText&&(!P||P.includes(M.relativePath))).length;await J.onStart?.({totalFiles:F,workerCount:Math.min(r.concurrency,F)}),z=await ur(r,k,R.assetMap,{client:E,jobId:k.jobId,concurrency:r.concurrency,batchSize:25,maxAttempts:2,...P?{relativePaths:P}:{},onProgress:J.onProgress,onBatchProgress:async({completedItems:M,totalItems:V,completedBatches:Q,totalBatches:ae,changedTextFiles:A})=>{let W=`Lambda rewrite: ${M}/${V} files; ${Q}/${ae} batches completed`;h.progress(W,{phase:"rewrite-text",execution:"lambda",index:M,totalFiles:V,completedBatches:Q,totalBatches:ae,changedTextFiles:A}),await jt(W);}}),await Qs(r,k);}else $&&E?z=await lr(r,R.assetMap,{client:E,jobId:`crawl-${Ee(m.runId,40)}`,concurrency:r.remoteWorkers?.rewrite?.concurrency??r.rewriteConcurrency,batchSize:r.remoteWorkers?.rewrite?.batchSize??25,maxAttempts:r.remoteWorkers?.rewrite?.maxAttempts??2,onStageProgress:async({index:P,totalFiles:F,file:M})=>{let V=Date.now();if(P!==1&&P!==F&&P%25!==0&&V-q<5e3)return;q=V;let Q=`Staging text files for Lambda rewrite: ${P}/${F}`;h.progress(Q,{phase:"rewrite-stage",index:P,totalFiles:F,file:M,execution:"lambda"}),await jt(Q);},onBatchProgress:async({completedItems:P,totalItems:F,completedBatches:M,totalBatches:V,changedTextFiles:Q})=>{let ae=`Lambda rewrite: ${P}/${F} files; ${M}/${V} batches completed`;h.progress(ae,{phase:"rewrite-text",execution:"lambda",index:P,totalFiles:F,completedBatches:M,totalBatches:V,changedTextFiles:Q}),await jt(ae);},options:J}):z=await Zt(r,R.assetMap,J);m.trackManifest&&(za(m,x),await Ve(r,m,true)),h.endMark("rewrite-text",{changedTextFiles:z});let pe=i?[...O.deletedFiles].sort():m.enabled&&!o.resumeRewrite?await Qa(r,R,m,O,h,k):[],we=[...new Set((x||[]).map(P=>ge(r.outputDir,C.isAbsolute(P)?P:C.join(r.outputDir,P))).filter(P=>P!==null))].sort();if(i){let P=[...O.changedFiles].sort(),F=new Set(P),M=new Set(i.directRenderUrls.map(D=>Ft(r,D,"page")).filter(D=>!!D&&F.has(D))),V=new Set(i.archiveFamilyUrls.map(D=>Ft(r,D,"page")).filter(D=>!!D&&F.has(D))),Q=new Set(i.resourceRenderUrls.map(D=>Ft(r,D,"asset")).filter(D=>!!D&&F.has(D))),ae=new Set([...R.sitemapLeafUrls].map(D=>Ft(r,D,"asset")).filter(D=>!!D&&F.has(D))),A=new Set([...R.sitemapIndexUrls].map(D=>Ft(r,D,"asset")).filter(D=>!!D&&F.has(D))),W=new Set([...M,...V,...Q,...ae,...A]),I=P.filter(D=>!W.has(D)),ne=[...new Set([...i.directRenderUrls,...i.archiveFamilyUrls,...R.resolvedSitemapRoots,...R.sitemapLeafUrls,...R.sitemapIndexUrls,...i.deleteUrls,...i.resourceRenderUrls,...i.resourceDeleteUrls].map(D=>{let Ue=new URL(D);return `${Ue.pathname}${Ue.search}`}))].sort();await Yr(r,{schemaVersion:2,generatedAt:new Date().toISOString(),outputDir:C.resolve(r.outputDir),runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,changedFiles:P,deletedFiles:pe,rewriteTargets:we,revalidateFiles:[...O.revalidateFiles].sort(),consumerId:i.consumerId,fromSequence:i.fromSequence,toSequence:i.toSequence,impactHash:i.impactHash,resolvedSitemapUrls:[...R.resolvedSitemapRoots],uploadGroups:{assets:I,content:[...M].sort(),listings:[...V].sort(),resources:[...Q].sort(),sitemapLeaves:[...ae].sort(),sitemapIndexes:[...A].reverse()},invalidationPaths:ne});}else await Yr(r,{schemaVersion:1,generatedAt:new Date().toISOString(),outputDir:C.resolve(r.outputDir),runMode:o.mode,crawlMode:o.crawlMode,fullSyncRequired:!(m.enabled&&!o.resumeRewrite),changedFiles:m.enabled&&!o.resumeRewrite?[...O.changedFiles].sort():[],deletedFiles:m.enabled&&!o.resumeRewrite?pe:[],rewriteTargets:m.enabled&&!o.resumeRewrite?we:[],revalidateFiles:[...O.revalidateFiles].sort()});await us(r),h.summary(o.resumeRewrite?`Done. Resumed final rewrite over existing output and changed ${z} text files.`:`Done. Rendered ${R.stats.pagesRendered} pages, processed ${R.doneSitemaps.size} sitemaps and ${R.doneAssets.size} assets.`,{mode:o.mode,crawlMode:o.crawlMode,resumeRewrite:o.resumeRewrite,...R.stats,pagesRendered:R.stats.pagesRendered,donePages:R.stats.pagesCompleted,claimedPages:R.donePages.size,doneSitemaps:R.doneSitemaps.size,doneAssets:R.doneAssets.size,changedTextFiles:z}),await h.flush();}function Jc(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 Se(e,t,r){let n=new AbortController,s=globalThis.setTimeout(()=>{n.abort(new Error(`${e} timed out after ${Jc(t)}.`));},t);try{return await r({abortController:n,httpOptions:{abortSignal:n.signal,requestTimeout:t}})}finally{globalThis.clearTimeout(s);}}function Qc(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Cr(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 Gc(e,t){return {...e,...t??{}}}function Zc(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function Rt(e,t){let r=String(t??e.deploymentTargetOverride??"").trim();if(!Qc(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,awsProfile:s.awsProfile??e.awsProfile,targetOrigin:s.targetOrigin??e.targetOrigin,s3:Gc(e.s3,s.s3),cloudFront:Zc(e.cloudFront,s.cloudFront)}}}function In(e){return String(e??"").split(";").map(t=>t.trim().toLowerCase()).filter(Boolean).join(";")}var Lo="wpsuite-sha256",Wo="wpsuite-normalized-sha256",En="public, max-age=0, must-revalidate",ol=/(?:^|[-_:.])(id|uid|nonce|token|hash|instance)(?:$|[-_:.])/i,$n=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,il=["SIGINT","SIGTERM"],Ir=new Set,kr=new Map,jo=null;function al(e){Ir.add(e);let t=false;return ()=>{t||(t=true,Ir.delete(e));}}async function cl(){let e=[...Ir];e.length!==0&&await Promise.allSettled(e.map(async t=>{Ir.delete(t),await U.rm(t,{recursive:true,force:true});}));}function ll(){if(!(kr.size>0))for(let e of il){let t=()=>{jo||(jo=cl().finally(()=>{for(let[r,n]of kr)process.off(r,n);kr.clear(),process.exit(1);}));};kr.set(e,t),process.on(e,t);}}ll();function ul(e,t){let r=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(r)return C.resolve(r);let n=t.trim();return n?C.join(C.resolve(n),"crawl-manifest.json"):C.join(C.resolve(e),".crawl-manifest.json")}function et(e){return C.normalize(e).split(C.sep).join("/")}function Fo(e,t){let r=C.relative(C.resolve(e),C.resolve(t));return !r||r.startsWith("..")||C.isAbsolute(r)?null:et(r)}async function dl(e,t,r){let n=ul(e.outputDir,t),s;try{s=await U.readFile(n,"utf8");}catch(u){return u.code==="ENOENT"?(r.warn("Deploy rewrite guard disabled because the crawl manifest is missing.",{manifestPath:n,source:"deploy-guard"}),null):(r.warn("Deploy rewrite guard disabled because the crawl manifest could not be read.",{manifestPath:n,error:u instanceof Error?u.message:String(u),source:"deploy-guard"}),null)}let o;try{o=JSON.parse(s);}catch(u){return r.warn("Deploy rewrite guard disabled because the crawl manifest is invalid JSON.",{manifestPath:n,error:u instanceof Error?u.message:String(u),source:"deploy-guard"}),null}let i=new Map,c=new Map,a=0,d=0;for(let[u,l]of Object.entries(o.pages||{})){let g=String(l?.outputPath||"").trim(),p=g?Fo(e.outputDir,g):null;if(p){let f=typeof l?.contentType=="string"&&l.contentType?l.contentType:null;f&&c.set(p,f);}!g||l?.rewriteComplete===true||p&&(a++,i.has(p)||i.set(p,{kind:"page",sourceUrl:u,outputPath:C.resolve(g),relativePath:p}));}for(let[u,l]of Object.entries(o.assets||{})){let g=String(l?.outputPath||"").trim(),p=l?.isText===true,f=g?Fo(e.outputDir,g):null;if(f){let w=typeof l?.contentType=="string"&&l.contentType?l.contentType:null;w&&c.set(f,w);}!g||!p||l?.rewriteComplete===true||f&&(d++,i.has(f)||i.set(f,{kind:"asset",sourceUrl:u,outputPath:C.resolve(g),relativePath:f}));}return {manifestPath:n,byRelativePath:i,contentTypeByRelativePath:c,pendingPages:a,pendingTextAssets:d}}function pl(e){return e&&($n.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)?"__hex__":/^[0-9]{8,}$/.test(e)?"__num__":/^(?=.*[a-z])(?=.*\d)[a-z0-9]{8,}$/i.test(e)?"__alnum__":e)}function Tn(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,r)=>r%2===1?t:pl(t)).join("")}function ml(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,r)=>r%2===1?t:$n.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 Tr(e,t){return Array.isArray(e)?e.map(r=>Tr(r,t)):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([r,n])=>[r,Tr(n,r)])):typeof e!="string"?e:t&&ol.test(t)?ml(e):$n.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)||/^[0-9]{8,}$/.test(e)?Tn(e):e}function gl(e){return e.replace(/((?:https?:\/\/[^"'\s>]+|\/)(?:wp-content|wp-includes)\/[^"'\s>?]+)\?ver=[A-Za-z0-9._-]+(?=$|[#"'])/gi,"$1")}function fl(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(Tr(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(Tr(o));return Buffer.from(i,"utf8").toString("base64")}catch{}return t}function hl(e,t){let r=e.toLowerCase();return r==="class"?t.split(/(\s+)/).map((n,s)=>s%2===1?n:Tn(n)).join(""):r==="id"||r==="for"||r==="aria-controls"||r==="aria-labelledby"||r==="aria-describedby"||r==="aria-owns"||r==="aria-activedescendant"||r==="name"?Tn(t):r==="href"||r==="src"?gl(t):fl(r,t)}function wl(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=hl(i,a);return ` ${i}=${c}${d}${c}`});return `<${r}${s}>`})}function vt(e){let t=C.extname(e).toLowerCase();return [".html",".htm",".xml",".xsl",".txt"].includes(t)||["robots.txt","sitemap.xml"].includes(C.basename(e))}function xr(e,t){return [e.replace(/^\/+|\/+$/g,""),t.replace(/^\/+/,"")].filter(Boolean).join("/")}async function yl(e,t,r,n){let s=new Map,o,i=0;do{i++;let c=await Se(`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(),u=Number(a.Size??0);s.set(a.Key,{etag:d,size:u,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 bl(e,t,r,n){if(n.has(r))return n.get(r)??null;try{let s=await Se(`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 Sl(e,t,r,n){if(n.cacheControl!==void 0&&n.contentType!==void 0)return {cacheControl:n.cacheControl,contentType:n.contentType};try{let s=await Se(`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 Do(e){return String(e||"").split(",").map(t=>t.trim().toLowerCase()).filter(Boolean).sort().join(",")}async function Pl(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 Se(`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 tt(e){return createHash("sha256").update(e).digest("hex")}function Ar(e){return createHash("sha256").update(wl(e.toString("utf8"))).digest("hex")}async function vl(e,t,r,n){if(n.has(r))return n.get(r)??null;try{let i=((await Se(`S3 head object metadata ${r}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),c))).Metadata?.[Lo]||"").trim().toLowerCase()||null;return n.set(r,i),i}catch{return n.set(r,null),null}}async function Cl(e,t,r,n,s){if(n.has(r))return n.get(r)??null;try{let i=((await Se(`S3 head object normalized metadata ${r}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),c))).Metadata?.[Wo]||"").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 Se(`S3 get object ${r}`,3e5,({httpOptions:a})=>e.send(new GetObjectCommand({Bucket:t,Key:r}),a))).Body?.transformToByteArray(),c=i?Ar(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 kl(e){return e.replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"")||"profile"}function xl(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 Il(e,t,r,n){let s=C.join(e,"asset-map.json"),o;try{o=await U.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 u=String(d??"");return t.urlRewriteMode==="absolute"&&t.targetOrigin&&r.targetOrigin&&t.targetOrigin!==r.targetOrigin&&(u=u.split(t.targetOrigin).join(r.targetOrigin)),u=xl(u,n),[a,u]}));await U.writeFile(s,JSON.stringify(c,null,2),"utf8");}async function Tl(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=C.join(C.dirname(e.outputDir),`.deploy-profile-${kl(r)}-${Date.now()}`),u=al(d);s.info(`Preparing deployment profile "${r}" from existing crawl output to ${d}...`);try{if(await U.rm(d,{recursive:!0,force:!0}),o){await U.mkdir(d,{recursive:!0});for(let w of o){let m=C.join(e.outputDir,w),h=C.join(d,w);await U.mkdir(C.dirname(h),{recursive:!0}),await U.copyFile(m,h);}}else await U.cp(e.outputDir,d,{recursive:!0});let l={...e,sourceOrigin:e.targetOrigin,targetOrigin:t.targetOrigin,extraReplacements:n,outputDir:d},g=1;s.info(`Rewriting text files for deployment profile "${r}"...`);let p={files:o,onStart:({totalFiles:w,workerCount:m})=>{g=m,s.info(`Rewriting ${w} text file(s) for deployment profile "${r}" with ${m} worker(s).`);},onProgress:({index:w,totalFiles:m,changedTextFiles:h,file:y,changed:b})=>{let j=`Rewriting [${w}/${m}] ${y} (${b?"rewrote":"checked"}; ${h} changed so far)`;m<=200||w<=5||w%10===0||w===m?(s.progress(j,{file:y,index:w,totalFiles:m,changed:b,changedTextFiles:h,workerCount:g}),s.info(j)):s.debug(j);}},f=i?await lr(l,{},{...i,options:p}):await Zt(l,{},p);return await Il(d,e,t,n),s.info(`Prepared deployment profile "${r}" in ${d} (${f} rewritten text file(s)).`),{outputDir:d,cleanup:async()=>{u(),await U.rm(d,{recursive:!0,force:!0});}}}catch(l){throw s.error(`Failed to prepare deployment profile "${r}" temporary output.`,{tempDir:d,outputDir:e.outputDir,error:l instanceof Error?l.message:String(l),...l instanceof Error&&l.stack?{stack:l.stack}:{}}),await U.rm(d,{recursive:true,force:true}).catch(()=>{}),u(),await s.flush().catch(()=>{}),l}}function qo(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 Al(e,t){if(!e||typeof e!="object")throw new Error("Remote deploy result must be a JSON object.");let r=e;if(r.schemaVersion!==ee||r.response?.operation!=="deploy-copy"||r.response.jobId!==t.jobId||r.response.taskId!==t.taskId||!r.detail||!Array.isArray(r.detail.objects)||!Array.isArray(r.detail.deletedKeys)||!Array.isArray(r.detail.deleteErrors))throw new Error("Remote deploy result has an invalid envelope.");return r}async function Er(e){let t=null;for(let r=1;r<=Math.max(1,Math.min(5,e.maxAttempts));r+=1){let n=te(`deploy-${e.batchIndex}-a${r}-${Date.now()}`,"taskId");try{let s=await e.client.invoke({schemaVersion:ee,operation:"deploy-copy",jobId:e.jobId,taskId:n,targetId:e.targetId,items:e.items,...e.deleteRelativeKeys?{deleteRelativeKeys:e.deleteRelativeKeys}:{}}),o=Al(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 El(e){let t=qo(e.remoteConfig,e.config),r=te(`deploy-${Date.now()}-${tt(Buffer.from(`${t.id}:${e.config.outputDir}`)).slice(0,12)}`,"jobId"),n=Ye(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],S=await U.readFile(C.join(e.config.outputDir,b)),j=e.contentTypeByRelativePath?.get(b)||Ws.lookup(b)||"application/octet-stream",E=e.revalidateFiles.has(et(b))?En:vt(b)?e.config.s3.htmlCacheControl:e.config.s3.assetCacheControl,k=tt(S),v=vt(b)?Ar(S):void 0,T=`${n}/files/${tt(Buffer.from(b))}-${C.basename(b)}`.replace(/\/+/g,"/");await e.client.writeWorkspaceObject({key:T,body:S,contentType:String(j),cacheControl:E,metadata:{"wpsuite-sha256":k,...v?{"wpsuite-normalized-sha256":v}:{}}}),o[y]={sourceKey:T,targetRelativeKey:b,sha256:k,...v?{normalizedSha256:v}:{},bytes:S.byteLength,contentType:String(j),cacheControl:E},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 u=[],l=[],g=[],p=0,f=0,w=0,m=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:m,completedBatches:0,totalBatches:a.length}),await Promise.all(Array.from({length:m},async()=>{for(;;){let y=w;if(y>=a.length)return;w+=1;let b=Date.now(),S=await Er({client:e.client,jobId:r,targetId:t.id,batchIndex:y+1,items:a[y],maxAttempts:e.maxAttempts}),j=0,E=0,k=0;for(let T of S.detail.objects)p+=1,T.status==="copied"?(u.push(T.targetKey),j+=1):T.status==="skipped"?(l.push(T.targetKey),E+=1):(g.push(T.targetKey),k+=1),e.log.debug(`Lambda deploy ${T.status}: ${T.targetKey}`,{phase:"deploy-copy",batch:y+1,sourceKey:T.sourceKey,targetKey:T.targetKey,status:T.status,...T.error?{error:T.error}:{}});f+=1;let v=Date.now()-b;e.log.info(`Lambda deploy batch ${y+1}/${a.length} completed: ${j} copied, ${E} skipped unchanged, ${k} failed.`,{phase:"deploy-copy",batch:y+1,totalBatches:a.length,items:S.detail.objects.length,copied:j,skipped:E,failed:k,elapsedMs:v,elapsedSeconds:Math.round(v/10)/100,workerExporterVersion:S.response.exporterVersion,...S.requestId?{lambdaRequestId:S.requestId}:{},...S.completedAt?{workerCompletedAt:S.completedAt}:{}}),e.log.progress(`Lambda deploy: ${p}/${e.files.length}`,{phase:"deploy-copy",index:p,totalFiles:e.files.length,uploaded:u.length,skipped:l.length,failed:g.length,mode:"lambda-s3-copy",workerCount:m,completedBatches:f,totalBatches: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 S=await Er({client:e.client,jobId:r,targetId:t.id,batchIndex:a.length+Math.floor(y/1e3)+1,items:[],deleteRelativeKeys:b,maxAttempts:e.maxAttempts});h.push(...S.detail.deletedKeys),e.log.progress(`Lambda deploy deletions: ${h.length}/${e.deletedFiles.length}`,{phase:"deploy-delete",index:h.length,totalFiles:e.deletedFiles.length,deleted:h.length,mode:"lambda-s3-copy"});}return {uploadedKeys:u,skippedKeys:l,failedKeys:g,deletedKeys:h}}async function $l(e){let t=qo(e.remoteConfig,e.config),r=te(`deploy-manifest-${Date.now()}-${tt(Buffer.from(t.id)).slice(0,12)}`,"jobId"),n=e.objects.map(p=>({sourceKey:p.object.key,targetRelativeKey:p.relativePath,sha256:p.object.sha256,bytes:p.object.bytes,contentType:p.contentType||"application/octet-stream",cacheControl:e.revalidateFiles.has(et(p.relativePath))?En:vt(p.relativePath)?e.config.s3.htmlCacheControl:e.config.s3.assetCacheControl})),s=[],o=Math.max(1,Math.min(500,e.batchSize));for(let p=0;p<n.length;p+=o)s.push(n.slice(p,p+o));let i=[],c=[],a=[],d=0,u=0,l=Math.max(1,Math.min(Math.max(1,e.concurrency),Math.max(1,s.length)));if(e.log.progress(`Lambda deploy: 0/${n.length}`,{phase:"deploy-copy",index:0,totalFiles:n.length,mode:"lambda-s3-native",workerCount:l}),await Promise.all(Array.from({length:s.length===0?0:l},async()=>{for(;;){let p=d;if(p>=s.length)return;d+=1;let f=await Er({client:e.client,jobId:r,targetId:t.id,batchIndex:p+1,items:s[p],maxAttempts:e.maxAttempts});for(let w of f.detail.objects)u+=1,w.status==="copied"?i.push(w.targetKey):w.status==="skipped"?c.push(w.targetKey):a.push(w.targetKey);e.log.progress(`Lambda deploy: ${u}/${n.length}`,{phase:"deploy-copy",index:u,totalFiles:n.length,uploaded:i.length,skipped:c.length,failed:a.length,mode:"lambda-s3-native",workerCount:l});}})),a.length>0)throw new Error(`Remote deployment failed for ${a.length} object(s).`);let g=[];for(let p=0;p<e.deletedFiles.length;p+=1e3){let f=e.deletedFiles.slice(p,p+1e3);if(f.length===0)continue;let w=await Er({client:e.client,jobId:r,targetId:t.id,batchIndex:s.length+Math.floor(p/1e3)+1,items:[],deleteRelativeKeys:f,maxAttempts:e.maxAttempts});g.push(...w.detail.deletedKeys);}return {uploadedKeys:i,skippedKeys:c,failedKeys:a,deletedKeys:g}}async function Ol(e,t,r,n,s){let o={...t,jobId:te(`profile-${tt(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],u=Ye(e.config.workspace.prefix,"jobs",o.jobId,"profile",tt(Buffer.from(d.relativePath)),C.basename(d.relativePath));o.objects[d.relativePath]={...d,object:await e.copyWorkspaceObject(d.object,u)};}})),o}async function _o(e=process.argv.slice(2)){let t=await Ie(),r=Cr(e),n=Rt(t,r),s=C.resolve(n.config.logDir||"logs"),o=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",i=o?C.join(C.resolve(o),"deploy-diff.json"):"",c=new gt(s,"deploy.log.jsonl",n.config.logLevel??"info"),a=n.profile?.extraReplacements??{},d=async()=>{};try{let u=await pt(t);if(e.includes("--content-sync")&&u?.schemaVersion!==2)throw new Error("Content-sync deployment requires a valid schema-2 targeted deploy plan.");let g=await ls(t);if(g)throw new Error(`Deployment blocked: the crawl started at ${g.startedAt} did not complete its final rewrite. Run a successful crawl or resume the rewrite before deploying.`);let p=t.remoteWorkers?.rewrite?.enabled===!0,f=t.remoteWorkers?.deploy?.enabled===!0,w=null,m=null;if(p||f){let A=dr({runtimeDir:o});if(!A)throw new Error("Remote Lambda workers are enabled, but the runtime directory is unknown.");w=await sr(A);let W=!1;m=new ht(w,{},{onProgress:I=>{if(I.operation==="rewrite"){c.debug("Lambda rewrite task progress.",{remoteWorkerOperation:I.operation,remoteWorkerTaskId:I.taskId,remoteWorkerStatus:I.status,remoteWorkerCompletedItems:I.completedItems,remoteWorkerFailedItems:I.failedItems,remoteWorkerTotalItems:I.totalItems,remoteWorkerUpdatedAt:I.updatedAt});return}c.progress(`Lambda ${I.operation} batch: ${I.completedItems+I.failedItems}/${I.totalItems}`,{remoteWorkerOperation:I.operation,remoteWorkerTaskId:I.taskId,remoteWorkerStatus:I.status,remoteWorkerCompletedItems:I.completedItems,remoteWorkerFailedItems:I.failedItems,remoteWorkerTotalItems:I.totalItems,remoteWorkerUpdatedAt:I.updatedAt});},onProgressError:I=>{W||(W=!0,c.warn("Lambda progress status could not be read; task execution continues.",{error:I.message}));}});}let h=f&&p&&t.remoteWorkers?.render?.enabled===!0&&m&&w?.functions.asset?await gr(t):null;if(h&&m&&w){if(h.workspace.bucket!==w.workspace.bucket||h.workspace.prefix!==w.workspace.prefix)throw new Error("Remote export manifest workspace does not match remote-workers.json.");let A=n.config,W=A.s3SyncMode??"sdk-upload-delete";if(u?.schemaVersion===2&&u.deletedFiles.length>0&&W!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let I=u?.schemaVersion===2?[...u.uploadGroups.assets,...u.uploadGroups.content,...u.uploadGroups.listings,...u.uploadGroups.resources,...u.uploadGroups.sitemapLeaves,...u.uploadGroups.sitemapIndexes]:u?.changedFiles??[];if(u?.schemaVersion===2){let re=[...new Set(I)],ye=[...new Set(u.changedFiles)];if(re.length!==ye.length||ye.some(Le=>!re.includes(Le)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let D=!!u&&!u.fullSyncRequired?[...new Set(I.map(et))]:Object.keys(h.objects).sort(),Ue=D.filter(re=>!h.objects[re]);if(Ue.length>0)throw new Error(`Remote export manifest is missing ${Ue.length} planned object(s): ${Ue.slice(0,10).join(", ")}`);let Je=h,K=D.map(re=>h.objects[re]);n.name&&(t.targetOrigin!==A.targetOrigin||Object.keys(a).length>0)&&(Je=await Ol(m,h,K,n.name||"default",t.concurrency),await ur(A,Je,Je.assetMap,{client:m,jobId:Je.jobId,concurrency:t.concurrency,batchSize:25,maxAttempts:2,onBatchProgress:({completedItems:re,totalItems:ye,completedBatches:Le,totalBatches:Bt,changedTextFiles:jr})=>{c.progress(`Lambda deployment-profile rewrite: ${re}/${ye} files; ${Le}/${Bt} batches completed`,{phase:"rewrite-text",execution:"lambda",index:re,totalFiles:ye,completedBatches:Le,totalBatches:Bt,changedTextFiles:jr});}}),K=D.map(re=>Je.objects[re])),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 ce=await $l({client:m,remoteConfig:w,config:A,objects:K,deletedFiles:W==="sdk-upload-delete"?u?.deletedFiles??[]:[],revalidateFiles:new Set((u?.revalidateFiles??[]).map(et)),concurrency:t.concurrency,batchSize:50,maxAttempts:2,log:c});i&&(await U.mkdir(C.dirname(i),{recursive:!0}),await U.writeFile(i,JSON.stringify({generatedAt:new Date().toISOString(),mode:"lambda-s3-native",summary:{totalFiles:K.length,eligibleFiles:K.length,uploaded:ce.uploadedKeys.length,skipped:ce.skippedKeys.length,rejected:0,failed:ce.failedKeys.length,deleted:ce.deletedKeys.length,protected:0,...n.name?{profile:n.name}:{}},uploadedKeys:ce.uploadedKeys,skippedKeys:ce.skippedKeys,rejectedKeys:[],failedKeys:ce.failedKeys,deletedKeys:ce.deletedKeys,protectedRemoteOnlyKeys:[]},null,2),"utf8")),u?.schemaVersion!==2&&await Ze(t),c.endMark("deploy"),await c.flush();return}let y=await Tl(t,n.config,n.name,a,c,u?.schemaVersion===2?u.changedFiles:void 0,p&&m?{client:m,jobId:`profile-${Date.now()}`,concurrency:t.remoteWorkers?.rewrite?.concurrency??t.rewriteConcurrency,batchSize:t.remoteWorkers?.rewrite?.batchSize??25,maxAttempts:t.remoteWorkers?.rewrite?.maxAttempts??2,onStageProgress:({index:A,totalFiles:W,file:I})=>{(A===1||A===W||A%25===0)&&c.progress(`Staging deployment-profile rewrite: ${A}/${W}`,{phase:"rewrite-stage",index:A,totalFiles:W,file:I,execution:"lambda"});},onBatchProgress:({completedItems:A,totalItems:W,completedBatches:I,totalBatches:ne,changedTextFiles:D})=>{c.progress(`Lambda deployment-profile rewrite: ${A}/${W} files; ${I}/${ne} batches completed`,{phase:"rewrite-text",execution:"lambda",index:A,totalFiles:W,completedBatches:I,totalBatches:ne,changedTextFiles:D});}}:void 0);d=y.cleanup;let b={...n.config,outputDir:y.outputDir},S=b.s3SyncMode??"sdk-upload-delete";if(u?.schemaVersion===2&&u.deletedFiles.length>0&&S!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let j=u?.schemaVersion!==2&&y.outputDir!==t.outputDir&&(t.targetOrigin!==n.config.targetOrigin||Object.keys(a).length>0),E=!!u&&!u.fullSyncRequired&&!j;if(u?.schemaVersion===2&&!E)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: ${S}`),u&&u.fullSyncRequired?c.info("Deploy plan requests a full sync; scanning the whole output tree."):u&&j?c.info("Ignoring incremental deploy plan because deployment-profile rewrites require a full sync."):E&&c.info(`Using incremental deploy plan: ${u.changedFiles.length} changed file(s), ${u.deletedFiles.length} deleted file(s).`),c.mark("deploy");let k=dt(b.awsProfile),v=new S3Client({region:b.s3.region,...k?{credentials:k}:{}}),T=y.outputDir===t.outputDir?await dl(t,o,c):null,$=u?.schemaVersion===2?[...u.uploadGroups.assets,...u.uploadGroups.content,...u.uploadGroups.listings,...u.uploadGroups.resources,...u.uploadGroups.sitemapLeaves,...u.uploadGroups.sitemapIndexes]:u?.changedFiles??[];if(u?.schemaVersion===2){let A=[...new Set($)],W=[...new Set(u.changedFiles)];if(A.length!==W.length||W.some(I=>!A.includes(I)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let N=E?(await Promise.all($.map(async A=>{try{return (await U.stat(C.join(b.outputDir,A))).isFile()?A:null}catch{if(u?.schemaVersion===2)throw new Error(`Content-sync planned upload is missing: ${A}`);return null}}))).filter(A=>A!==null):await Oi(["**/*"],{cwd:b.outputDir,onlyFiles:!0,dot:!0}),B=[],R=[],O=N.filter(A=>{let W=T?.byRelativePath.get(et(A));if(!W)return !0;let I=xr(b.s3.prefix,A);return B.push(A),R.push(I),c.reject(W.kind,W.sourceUrl||A,"rewrite not completed; deploy upload blocked","deploy-guard",{file:A,key:I,outputPath:W.outputPath,manifestPath:T?.manifestPath}),!1}),de=new Set((u?.revalidateFiles||[]).map(et)),ie=new Set((E?O:N).map(A=>xr(b.s3.prefix,A)));T&&(T.pendingPages>0||T.pendingTextAssets>0)&&c.info(`Deploy rewrite guard loaded ${T.pendingPages} pending page(s) and ${T.pendingTextAssets} pending text asset(s) from crawl manifest.`,{manifestPath:T.manifestPath,pendingPages:T.pendingPages,pendingTextAssets:T.pendingTextAssets,source:"deploy-guard"}),B.length>0&&c.warn(`Deploy rewrite guard blocked ${B.length} file(s) from upload because final crawl rewrite is incomplete.`,{rejected:B.length,manifestPath:T?.manifestPath,source:"deploy-guard"}),E?c.info(`Using ${O.length} eligible planned changed file(s) from deploy plan for upload${B.length>0?` (${B.length} blocked by deploy rewrite guard)`:""}.`):c.info(`Found ${N.length} file(s) in output directory "${b.outputDir}"${B.length>0?`; ${O.length} remain eligible for upload after deploy rewrite guard filtering`:""}.`),c.progress("Deploy preflight: comparing local output with remote S3 state.",{phase:"deploy",mode:S,totalFiles:O.length,scannedFiles:N.length,rejected:B.length,deploymentProfile:n.name||"",s3Bucket:b.s3.bucket,s3Prefix:b.s3.prefix||"",targetedPlan:E,deployGuardEnabled:y.outputDir===t.outputDir});let H=0,x=0,L=0,q=[],J=[],z=[],pe=[],we=[],P=new Map,F=new Map,M=new Map,V=new Map,Q=new Map;if(f){if(!m||!w)throw new Error("Remote deploy worker client was not initialized.");if(S!=="sdk-upload-delete"&&S!=="sdk-upload-only")throw new Error(`Unsupported remote deploy mode: ${S}`);let A=await El({client:m,remoteConfig:w,config:b,files:O,deletedFiles:S==="sdk-upload-delete"?u?.deletedFiles??[]:[],revalidateFiles:de,contentTypeByRelativePath:T?.contentTypeByRelativePath,concurrency:b.remoteWorkers?.deploy?.concurrency??b.assetDownloadConcurrency,batchSize:b.remoteWorkers?.deploy?.batchSize??50,maxAttempts:b.remoteWorkers?.deploy?.maxAttempts??2,log:c});q.push(...A.uploadedKeys),J.push(...A.skippedKeys),z.push(...A.failedKeys),pe=A.deletedKeys,H=q.length,x=J.length,L=z.length,c.info(`Lambda deploy complete: ${H} copied, ${x} skipped unchanged, ${pe.length} deleted, ${L} failed.`),i&&(await U.mkdir(C.dirname(i),{recursive:!0}),await U.writeFile(i,JSON.stringify({generatedAt:new Date().toISOString(),mode:"lambda-s3-copy",summary:{totalFiles:N.length,eligibleFiles:O.length,uploaded:H,skipped:x,rejected:R.length,failed:L,deleted:pe.length,protected:0,...n.name?{profile:n.name}:{}},uploadedKeys:q,skippedKeys:J,rejectedKeys:R,failedKeys:z,deletedKeys:pe,protectedRemoteOnlyKeys:[]},null,2),"utf8")),u?.schemaVersion!==2&&await Ze(t),c.endMark("deploy"),await c.flush();return}E||(c.info("Listing existing S3 objects to detect unchanged files..."),P=await yl(v,b.s3.bucket,b.s3.prefix,c),c.info(`Found ${P.size} existing key(s) in S3.`));let ae=async A=>P.has(A)?P.get(A)??null:E?bl(v,b.s3.bucket,A,F):null;for(let A of O){let W=C.join(b.outputDir,A),I=xr(b.s3.prefix,A),ne=T?.contentTypeByRelativePath.get(A),D=ne&&ne!=="application/octet-stream"?ne:Ws.lookup(A)||ne||"application/octet-stream",Ue=de.has(et(A)),Je=Ue?En:vt(A)?b.s3.htmlCacheControl:b.s3.assetCacheControl,K=H+x+L+1;O.length<=200||K<=5||K%10===0||K===O.length?(c.progress(`Uploading [${K}/${O.length}] ${I} (${D})`,{file:A,key:I,index:K,totalFiles:O.length,contentType:D,mode:S}),c.info(`Uploading [${K}/${O.length}] ${I} (${D})`)):c.debug(`Uploading [${K}/${O.length}] ${I} (${D})`);try{let Re=await U.readFile(W),ce=await ae(I),re=null,ye=null;if(ce){let Le=await Sl(v,b.s3.bucket,I,ce),Bt=!Ue||Do(Le.cacheControl)===Do(Je),jr=In(Le.contentType)===In(String(D)),Fr=Bt&&jr,Qo=Re.byteLength;if(ce.size===Qo){let Ht=ce.etag.includes("-");if(ce.etag!==""&&!Ht){let Go=Rl(Re);if(ce.etag===Go&&Fr){x++,O.length<=200||x<=5||x%25===0?(c.progress(`Skipped unchanged [${K}/${O.length}] ${I}`,{file:A,key:I,index:K,totalFiles:O.length,uploaded:H,skipped:x,failed:L,mode:S,reason:"etag-md5-match"}),c.info(`Skipped unchanged [${K}/${O.length}] ${I}`)):c.debug(`Skipped unchanged [${K}/${O.length}] ${I}`),J.push(I);continue}}re=tt(Re);let Un=await vl(v,b.s3.bucket,I,M);if(Un&&Un===re&&Fr){x++,O.length<=200||x<=5||x%25===0?(c.progress(`Skipped unchanged [${K}/${O.length}] ${I}`,{file:A,key:I,index:K,totalFiles:O.length,uploaded:H,skipped:x,failed:L,mode:S,reason:"metadata-sha256-match"}),c.info(`Skipped unchanged [${K}/${O.length}] ${I}`)):c.debug(`Skipped unchanged [${K}/${O.length}] ${I}`),J.push(I);continue}}if(vt(A)){ye=Ar(Re);let Ht=await Cl(v,b.s3.bucket,I,V,Q);if(Ht&&Ht===ye&&Fr){x++,O.length<=200||x<=5||x%25===0?(c.progress(`Skipped semantically unchanged [${K}/${O.length}] ${I}`,{file:A,key:I,index:K,totalFiles:O.length,uploaded:H,skipped:x,failed:L,mode:S,reason:"normalized-html-match"}),c.info(`Skipped semantically unchanged [${K}/${O.length}] ${I}`)):c.debug(`Skipped semantically unchanged [${K}/${O.length}] ${I}`),J.push(I);continue}}}re=re??tt(Re),ye=ye??(vt(A)?Ar(Re):null),await Se(`S3 upload ${I}`,9e5,({abortController:Le})=>new Upload({client:v,abortController:Le,params:{Bucket:b.s3.bucket,Key:I,Body:Re,ContentType:D,CacheControl:Je,Metadata:{[Lo]:re,...ye?{[Wo]:ye}:{}}}}).done()),H++,q.push(I),(H%25===0||H===O.length)&&(c.progress(`Uploaded ${H}/${O.length} file(s)\u2026`,{uploaded:H,skipped:x,failed:L,totalFiles:O.length,mode:S}),c.info(`Uploaded ${H}/${O.length} file(s)\u2026`));}catch(Re){L++,z.push(I),c.error(`Failed to upload ${I}: ${Re.message}`,{key:I,error:String(Re)});}}if(c.info(`Upload complete: ${H} uploaded, ${x} skipped unchanged, ${R.length} rejected by guard, ${L} failed.`),z.length>0||R.length>0)throw new Error(`Deployment stopped before deletion because ${z.length} upload(s) failed and ${R.length} upload(s) were rejected.`);if(S==="sdk-upload-delete"){let A=new Set((u?.deletedFiles??[]).map(I=>xr(b.s3.prefix,I))),W=[...A].filter(I=>!ie.has(I));if(!E&&(we=[...P.keys()].filter(ne=>!ie.has(ne)).filter(ne=>!A.has(ne)),we.length>0)){c.warn(`Protected ${we.length} remote-only S3 object(s) from deletion because the crawl did not provide explicit tombstones.`,{protected:we.length,source:"deploy-delete-guard"});for(let ne of we)c.debug(` protected remote-only object: ${ne}`);}pe=await Pl(v,b.s3.bucket,E?W:W.filter(I=>P.has(I)),c);}else c.info("Sync mode is sdk-upload-only \u2014 skipping stale object deletion.");i&&(await U.mkdir(C.dirname(i),{recursive:!0}),await U.writeFile(i,JSON.stringify({generatedAt:new Date().toISOString(),mode:S,summary:{totalFiles:N.length,eligibleFiles:O.length,uploaded:q.length,skipped:J.length,rejected:R.length,failed:z.length,deleted:pe.length,protected:we.length,...n.name?{profile:n.name}:{}},uploadedKeys:q,skippedKeys:J,rejectedKeys:R,failedKeys:z,deletedKeys:pe,protectedRemoteOnlyKeys:we},null,2),"utf8")),u?.schemaVersion!==2&&await Ze(t),c.endMark("deploy"),await c.flush();}finally{await d().catch(()=>{});}}function jl(e){let t=e;return t.endsWith("/")?t+="index.html":C.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 No(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=jl(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 Wl(){let e=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();return e?C.join(C.resolve(e),"content-sync-invalidation.json"):""}async function On(e,t){if(!e)return;await U.mkdir(C.dirname(e),{recursive:true});let r=`${e}.${process.pid}.${Date.now()}.tmp`;await U.writeFile(r,JSON.stringify({...t,updatedAt:new Date().toISOString()},null,2),"utf8"),await U.rename(r,e);}async function ql(e,t,r,n){if(!e)return null;try{let s=JSON.parse(await U.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 _l(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 Ho(e=process.argv.slice(2)){let t=await Ie(),r=Cr(e),n=Rt(t,r),s=n.config,o=new gt(s.logDir||"logs","invalidate.log.jsonl",s.logLevel??"info"),i=e.includes("--content-sync"),c=await pt(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?No(a,s.s3.prefix||""):a;if(c?.schemaVersion===2&&d.some(w=>w==="/*"))throw new Error("Content-sync cannot use a whole-distribution invalidation.");if(d.length===0){o.info("The targeted deploy plan has no CloudFront paths to invalidate."),await o.flush();return}let u=s.cloudFront.distributionId,l=dt(s.awsProfile),g=new CloudFrontClient({region:"us-east-1",maxAttempts:5,...l?{credentials:l}:{}});if(o.mark("invalidate"),c?.schemaVersion!==2){let w=`smartcloud-static-publisher-${Date.now()}`;await Se(`CloudFront invalidation for distribution ${u}`,3e5,({httpOptions:m})=>g.send(new CreateInvalidationCommand({DistributionId:u,InvalidationBatch:{CallerReference:w,Paths:{Quantity:d.length,Items:d}}}),m)),o.endMark("invalidate"),await o.flush();return}let p=Wl(),f=await ql(p,u,c.impactHash,d);if(!f){let w=createHash("sha256").update(JSON.stringify(d)).digest("hex");f={schemaVersion:1,distributionId:u,impactHash:c.impactHash,updatedAt:new Date().toISOString(),batches:_l(d).map((m,h)=>({index:h,callerReference:`wpsuite-content-sync-${c.impactHash.slice(0,24)}-${w.slice(0,24)}-${h}`,paths:m,invalidationId:null,status:"pending"}))},await On(p,f);}for(let w of f.batches){if(!w.invalidationId){let m=await Se(`CloudFront targeted invalidation batch ${w.index+1}`,3e5,({httpOptions:y})=>g.send(new CreateInvalidationCommand({DistributionId:u,InvalidationBatch:{CallerReference:w.callerReference,Paths:{Quantity:w.paths.length,Items:w.paths}}}),y)),h=String(m.Invalidation?.Id||"").trim();if(!h)throw new Error("CloudFront did not return an invalidation ID.");w.invalidationId=h,w.status="created",await On(p,f);}await waitUntilInvalidationCompleted({client:g,maxWaitTime:900,minDelay:2,maxDelay:15},{DistributionId:u,Id:w.invalidationId}),w.status="completed",await On(p,f),o.info(`CloudFront invalidation batch ${w.index+1}/${f.batches.length} completed.`,{invalidationId:w.invalidationId,pathCount:w.paths.length});}o.endMark("invalidate"),await o.flush();}function Mr(e){let t=new URL(e);return t.hash="",t.href}function Vo(e,t,r){let n=new Set([...t].map(a=>Mr(a))),s=new Map,o=new Map,i=a=>{s.set(Mr(a),a);},c=a=>{if(!a)return;let d=Mr(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(Mr(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 zo=["claimed","impact-planned","rendered","deployed","invalidated","verified","acknowledged"];function Fn(){let e=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();if(!e)throw new Error("Content-sync requires a runtime directory.");return C.resolve(e)}function Ct(e,t){return zo.indexOf(e)>=zo.indexOf(t)}async function ze(e,t,r,n,s={}){let o={timestamp:new Date().toISOString(),eventType:t,status:t.endsWith("failed")?"failed":"success",actorSource:"content-sync",jobId:r.id||"",command:"content-sync",message:n,details:s};await U.mkdir(e,{recursive:true}),await U.appendFile(C.join(e,"audit-events.jsonl"),`${JSON.stringify(o)}
|
|
19
|
-
`,"utf8");}function Bl(e){return kt({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 Hl(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 Dn(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=Rt(e,t.deploymentProfile),s=n.name||"",o=qn(e.sourceOrigin,String(t.id||""),r),i=_n({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=Nn({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||"")}:${We({scopeFingerprint:o,targetFingerprint:i}).slice(0,32)}`,d=new Kt({sourceOrigin:e.sourceOrigin,runtimeToken:String(e.wpsuite?.runtimeToken||""),ignoreHttpsErrors:e.ignoreHttpsErrors}),u=await d.releaseFingerprint(r.includeSubsites),l=Wn({wordpressFingerprint:u,targetFingerprint:i,sourceOrigin:e.sourceOrigin,sitemapPaths:Tt(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:l}}async function Vl(e){let t=Fn(),r=zt(t),n=await Ie(),s=(n.scheduler?.rules??[]).filter(d=>d.command==="content-sync"&&d.enabled!==false);if(s.length===0)return 0;let o=await qr(r),i=await Jt(r),c=await Wr(r),a=0;for(let d of s){let u=await Dn(n,d),l=c.entries[u.coalesceKey];if(!l||l.releaseJobId!==e||l.scopeFingerprint!==u.scopeFingerprint||l.targetFingerprint!==u.targetFingerprint||l.releaseFingerprint!==u.releaseFingerprint)throw new Error(`The normal release has no matching pre-crawl content-sync cutoff for rule ${String(d.id||"")}.`);let g=We({releaseJobId:e,releaseFingerprint:u.releaseFingerprint,scopeFingerprint:u.scopeFingerprint,targetFingerprint:u.targetFingerprint,sequence:l.sequence}),p=await u.client.establishBaseline({consumerId:u.consumerId,scopeFingerprint:u.scopeFingerprint,baselineId:g,postTypes:u.scope.postTypes,includeSubsites:u.scope.includeSubsites,sequence:l.sequence}),f={schemaVersion:1,ruleId:String(d.id||""),consumerId:u.consumerId,coalesceKey:u.coalesceKey,scopeFingerprint:u.scopeFingerprint,targetFingerprint:u.targetFingerprint,releaseFingerprint:u.releaseFingerprint,baselineId:g,deploymentProfile:u.deploymentProfile,committedSequence:p.committedSequence,releaseJobId:e,verifiedAt:new Date().toISOString()};o.entries[u.coalesceKey]=f;let w=i.rules[u.coalesceKey];i.rules[u.coalesceKey]={ruleId:String(d.id||""),consumerId:u.consumerId,coalesceKey:u.coalesceKey,committedSequence:p.committedSequence,observedHeadSequence:p.headSequence,lag:Math.max(0,p.headSequence-p.committedSequence),lastSuccessfulJobId:w?.lastSuccessfulJobId||e,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:p.headSequence>p.committedSequence,retryAttempt:0,nextRetryAt:null,lastError:null,baselineStatus:"ready",baselineReason:null};let m=await Yn(r,u.coalesceKey);await ze(t,"content-sync-baseline-established",{id:e},"A normal release established a verified content-sync baseline.",{ruleId:d.id,consumerId:u.consumerId,committedSequence:p.committedSequence,supersededCurrent:m}),a++;}return await zn(r,o),await Qt(r,i),await U.rm(r.releaseCutoff,{force:true}),a}async function Kl(e){let t=Fn(),r=zt(t),n=await Ie(),s=(n.scheduler?.rules??[]).filter(c=>c.command==="content-sync"&&c.enabled!==false);if(s.length===0)return 0;let o=await Wr(r),i=0;for(let c of s){let a=await Dn(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 u=await a.client.unboundHead(a.scope.postTypes,a.scope.includeSubsites),l={schemaVersion:1,releaseJobId:e,ruleId:String(c.id||""),coalesceKey:a.coalesceKey,scopeFingerprint:a.scopeFingerprint,targetFingerprint:a.targetFingerprint,releaseFingerprint:a.releaseFingerprint,sequence:u.headSequence,capturedAt:new Date().toISOString()};o.entries[a.coalesceKey]=l,await ze(t,"content-sync-release-cutoff-captured",{id:e},"A normal release captured its pre-crawl content-sync journal cutoff.",{ruleId:c.id,consumerId:a.consumerId,sequence:u.headSequence}),i++;}return await Kn(r,o),i}async function zl(e,t){let r=JSON.parse(await U.readFile(C.join(e,"current-run.json"),"utf8"));if(r.id!==t||r.command!=="content-sync"||!String(r.ruleId||"").trim())throw new Error("The active queue job is not the requested content-sync job.");return r}async function jn(e,t,r=0){let n=e.protocol==="https:"?Vn:si,s=e.protocol==="https:"?new Vn.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,u=a.headers.location;if(d>=300&&d<400&&u&&r<5){a.resume(),jn(new URL(u,e),t,r+1).then(o,i);return}let l=[],g=0;a.on("data",p=>{g+=p.byteLength,g<=1048576&&l.push(p);}),a.on("error",i),a.on("end",()=>o({status:d,body:Buffer.concat(l).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 Jl(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 U.readFile(n,"utf8")),i=new Set;for(let[u,l]of Object.entries(o.pages??{}))i.add(u),typeof l.url=="string"&&i.add(l.url);let c=await pt(e);if(c?.schemaVersion!==2||c.impactHash!==r.impactHash)throw new Error("Content-sync verification requires the matching targeted deploy plan.");let a=Vo(r,i,c.resolvedSitemapUrls),d=new Set(r.resourceRenderUrls.map(u=>{let l=new URL(u);return l.hash="",l.href}));for(let u of a.expectedAvailable){let l=new URL(u),g=new URL(`${l.pathname}${l.search}`,s),p=await jn(g,e.ignoreHttpsErrors);if(p.status<200||p.status>=400)throw new Error(`Content-sync verification failed (${p.status}): ${g}`);let f=new URL(u);if(f.hash="",d.has(f.href)){if(!p.contentType.toLowerCase().startsWith("application/json"))throw new Error(`Resource verification expected application/json: ${g}`);try{JSON.parse(p.body);}catch{throw new Error(`Resource verification received invalid JSON: ${g}`)}}}for(let u of a.expectedMissing){let l=new URL(u),g=new URL(`${l.pathname}${l.search}`,s),p=await jn(g,e.ignoreHttpsErrors);if(p.status>=200&&p.status<300)throw new Error(`Deleted content is still publicly available: ${g}`)}}async function Ql(e){let t=await U.readFile(e.candidateManifest,"utf8");JSON.parse(t);let r=`${e.trustedManifest}.${process.pid}.tmp`;await U.writeFile(r,t,"utf8"),await U.rename(r,e.trustedManifest);}async function Gl(e,t){try{let r=JSON.parse(await U.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 U.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 at(e,t,r,n,s={},o=0,i=async()=>{}){t.phase=r,await Jn(e,t),await Zn(e,{schemaVersion:1,jobId:t.jobId,phase:r,completedItems:n,totalItems:n,attempt:o,updatedAt:new Date().toISOString(),details:s}),await ze(C.dirname(e.state),"content-sync-checkpoint",{id:t.jobId},`Content-sync checkpoint completed: ${r}.`,{phase:r,completedItems:n,totalItems:n,attempt:o,...s}),await i(r,t);}async function Zl(e,t={}){let r={loadConfig:Ie,resolveRuleContext:Dn,runCrawl:Mo,runDeploy:_o,runInvalidate:Ho,verifyDeployment:Jl,promoteCandidateManifest:Ql,clearDeployPlan:Ze,afterCheckpoint:async()=>{},...t},n=Fn(),s=zt(n),o=await zl(n,e),i=await r.loadConfig(),c=Hl(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 u=(await qr(s)).entries[a.coalesceKey];if(!u||u.scopeFingerprint!==a.scopeFingerprint||u.targetFingerprint!==a.targetFingerprint||u.releaseFingerprint!==a.releaseFingerprint)throw await ze(n,"content-sync-baseline-rejected",o,"Content-sync requires a new verified full or incremental release baseline.",{ruleId:c.id,reason:u?"mismatch":"missing"}),new Error("Content-sync baseline is missing or stale. Run a successful normal publish first.");let l=await _r(s),g=await a.client.head({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:u.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites});if(l&&l.jobId!==o.id)throw new Error("Another immutable content-sync range is still active.");if(l||(l={schemaVersion:1,jobId:o.id,ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,scopeFingerprint:a.scopeFingerprint,targetFingerprint:a.targetFingerprint,baselineId:u.baselineId,fromSequence:g.committedSequence,toSequence:g.headSequence,phase:"claimed",impactHash:null,startedAt:new Date().toISOString(),updatedAt:new Date().toISOString()},await at(s,l,"claimed",0,{fromSequence:l.fromSequence,toSequence:l.toSequence},0,r.afterCheckpoint),await ze(n,"content-sync-range-claimed",o,"Content-sync claimed a closed journal range.",{fromSequence:l.fromSequence,toSequence:l.toSequence})),l.fromSequence===l.toSequence){let h=await Jt(s);h.rules[a.coalesceKey]={ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,committedSequence:l.toSequence,observedHeadSequence:g.headSequence,lag:Math.max(0,g.headSequence-l.toSequence),lastSuccessfulJobId:o.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:false,retryAttempt:0,nextRetryAt:null,lastError:null},await Qt(s,h),await Nr(s);return}let p=await Qn(s);if(!Ct(l.phase,"impact-planned")){let h=await a.client.events({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:u.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites,afterSequence:l.fromSequence,throughSequence:l.toSequence}),y=Lr({events:h,scope:a.scope,sourceOrigin:i.sourceOrigin}),b=await a.client.resolveImpactFamilies(y,a.scope.includeSubsites);p=Hn({jobId:o.id,ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,scopeFingerprint:a.scopeFingerprint,targetFingerprint:a.targetFingerprint,baselineId:u.baselineId,sourceOrigin:i.sourceOrigin,scope:a.scope,fromSequence:l.fromSequence,toSequence:l.toSequence,events:h,resolvedArchiveFamilies:b,sitemapUrls:Tt(i.sitemapPaths)}),await Gn(s,p),l.impactHash=p.impactHash,await at(s,l,"impact-planned",h.length,{eventCount:h.length,foldedCount:p.foldedCount,impactHash:p.impactHash},0,r.afterCheckpoint),await ze(n,"content-sync-impact-planned",o,"Content-sync persisted an immutable impact plan.",{impactHash:p.impactHash,eventCount:h.length});}if(!p||p.jobId!==o.id||l.impactHash!==p.impactHash)throw new Error("The immutable content-sync impact plan is missing or mismatched.");if(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH=s.candidateManifest,!Ct(l.phase,"rendered")){let h=await Gl(s,o.id);await ze(n,h==="resumed"?"content-sync-render-resumed":"content-sync-render-initialized",o,h==="resumed"?"Content-sync resumed its candidate crawl manifest.":"Content-sync initialized a candidate crawl manifest.",{impactHash:p.impactHash}),await r.runCrawl(["--content-sync-plan",s.impact]),await at(s,l,"rendered",p.directRenderUrls.length+p.archiveFamilyUrls.length+p.resourceRenderUrls.length,{impactHash:p.impactHash},o.attempt??0,r.afterCheckpoint);}let f=a.deploymentProfile?["--profile",a.deploymentProfile]:[];if(Ct(l.phase,"deployed")||(await r.runDeploy([...f,"--content-sync"]),await at(s,l,"deployed",1,{impactHash:p.impactHash},0,r.afterCheckpoint),await ze(n,"content-sync-deployed",o,"Content-sync targeted deployment completed.",{impactHash:p.impactHash})),Ct(l.phase,"invalidated")?Ct(l.phase,"verified")||await r.runInvalidate([...f,"--content-sync"]):(await r.runInvalidate([...f,"--content-sync"]),await at(s,l,"invalidated",1,{impactHash:p.impactHash},0,r.afterCheckpoint)),Ct(l.phase,"verified")||(await r.verifyDeployment(i,a,p,s.candidateManifest),await r.promoteCandidateManifest(s),await at(s,l,"verified",1,{impactHash:p.impactHash},0,r.afterCheckpoint)),g=await a.client.head({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:u.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites}),g.committedSequence===l.fromSequence)await a.client.acknowledge({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:u.baselineId,expectedSequence:l.fromSequence,sequence:l.toSequence,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites});else if(g.committedSequence!==l.toSequence)throw new Error("Content-sync acknowledgement cursor is in an unexpected state.");await at(s,l,"acknowledged",1,{sequence:l.toSequence},0,r.afterCheckpoint),await ze(n,"content-sync-cursor-acknowledged",o,"Content-sync committed its journal cursor after verification.",{sequence:l.toSequence});let w=await a.client.head({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:u.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites}),m=await Jt(s);m.rules[a.coalesceKey]={ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,committedSequence:l.toSequence,observedHeadSequence:w.headSequence,lag:Math.max(0,w.headSequence-l.toSequence),lastSuccessfulJobId:o.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:w.headSequence>l.toSequence,retryAttempt:0,nextRetryAt:null,lastError:null},await Qt(s,m),await r.clearDeployPlan(i),await U.rm(C.join(n,"content-sync-invalidation.json"),{force:true}),await U.rm(s.candidateManifest,{force:true}),await Nr(s);}async function Jo(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 Kl(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 Vl(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 Zl(o);}Jo().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 So(e,t,r,n,s,o){let i=xr(e,s);if(await Ba(e,t,r,n,s,o),!n.enabled)return {action:"render",changeToken:n.trackManifest?await br(e,t,n,s,o):null};let c=n.manifest.pages[s];if(!c)return {action:"render",changeToken:await br(e,t,n,s,o)};let a=await br(e,t,n,s,o);if(a?.supported&&a.token)return c.changeToken===a.token?i&&!await Ka(e,t,s,o)?{action:"render",changeToken:a}:{action:"reuse",changeToken:a}:{action:"render",changeToken:a};let d=nt(r.sitemapLastmodByPage[s]);return d&&c.sitemapLastmod&&c.sitemapLastmod===d?{action:"reuse",changeToken:a}:{action:"render",changeToken:a}}async function Ka(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 za(e,t,r,n,s){let o=so(r.previousManifest,void 0,n,"previous-manifest"),i=0;for(let[g,p]of Object.entries(r.manifest.pages))if(!r.seenPages.has(g)){Sn(s,e,p.outputPath);try{await U.unlink(p.outputPath);}catch(f){(f.code||"")!=="ENOENT"&&n.warn(`Failed to remove stale incremental page output for ${g}`,{url:g,outputPath:p.outputPath,error:String(f)});}delete r.manifest.pages[g],i++;}let c=so(r.manifest,r.runId,n,"active-manifest"),a=new Set,d=new Set;for(let g of c){for(let w of Lt(e,g))a.add(w);let p=r.manifest.assets[g]?.outputPath;p&&d.add(p);let f=St(e,t.assetMap,g);f&&d.add(f);}for(let g of Object.keys(r.manifest.assets)){if(c.has(g)){r.manifest.assets[g].unreferencedRuns=0;continue}let p=r.manifest.assets[g];if(Fs(p.unreferencedRuns)){p.unreferencedRuns+=1,n.warn(`Deferred deletion of newly unreferenced incremental asset ${g}.`,{url:g,unreferencedRuns:p.unreferencedRuns,phase:"incremental-cleanup",source:"asset-deletion-grace"});continue}delete r.manifest.assets[g];}let u=0,l=0;for(let g of o){if(r.manifest.assets[g])continue;let p=new Set,f=r.previousManifest.assets[g]?.outputPath;f&&p.add(f);let w=St(e,t.assetMap,g);if(!c.has(g)&&w)p.add(w);else if(!c.has(g)){let m=uc(e,g,n,"incremental-cleanup");m&&(p.add(m.originalFilePath),m.hashedFilePath&&p.add(m.hashedFilePath));}for(let m of p)if(!d.has(m)){Sn(s,e,m);try{await U.unlink(m),u++;}catch(h){(h.code||"")!=="ENOENT"&&n.warn(`Failed to remove stale incremental asset output for ${g}`,{url:g,outputPath:m,error:String(h)});}}for(let m of Lt(e,g))a.has(m)||Object.prototype.hasOwnProperty.call(t.assetMap,m)&&(delete t.assetMap[m],l++);}(i>0||u>0||l>0)&&n.info(`Incremental cleanup removed ${i} stale pages, ${u} stale assets, ${l} stale asset mappings`,{removedPages:i,removedAssets:u,removedAssetMappings:l,phase:"incremental-cleanup"});}async function Ja(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=>C.resolve(a))),o=Object.values(e.postCrawlCopyMap||{}).map(a=>Sr(String(a||""))).filter(a=>!!a).map(a=>C.resolve(e.outputDir,a)),c=(await pt(e.outputDir)).filter(a=>a.endsWith(".html")||a.endsWith(".htm")).filter(a=>js(C.join(e.outputDir,a),e.outputDir,s,o));for(let a of c){let d=C.join(e.outputDir,a);Sn(n,e,d),await U.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 so(e,t,r,n){let s=new Set,o=i=>{let c=i.trim();if(c){if(r){let a=ke(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 oo(e,t,r){for(let n of t)for(let s of Lt(e,n))if(r.has(s))return true;return false}function Qa(e,t,r,n,s){let o=new Set(r.rewriteTargets);for(let c of r.changedFiles)o.add(C.resolve(e.outputDir,c));for(let c of Object.values(t.manifest.pages))c.rewriteComplete||o.add(C.resolve(c.outputPath));for(let c of Object.values(t.manifest.assets))c.isText&&!c.rewriteComplete&&o.add(C.resolve(c.outputPath));let i=Na(n,s);if(i.size===0)return [...o];for(let c of Object.values(t.manifest.pages))oo(e,c.discoveredAssets,i)&&o.add(C.resolve(c.outputPath));for(let[c,a]of Object.entries(t.manifest.assets)){a.isText&&oo(e,a.discoveredAssets,i)&&o.add(C.resolve(a.outputPath));for(let d of Lt(e,c))if(i.has(d)){ue(r,e,a.outputPath),a.isText&&o.add(C.resolve(a.outputPath));break}}return [...o]}function Ga(e,t){let r=t?new Set(Array.from(t,s=>C.resolve(s))):null,n=s=>r===null||r.has(C.resolve(s));for(let s of Object.values(e.manifest.pages))n(s.outputPath)&&(s.rewriteComplete=true);for(let s of Object.values(e.manifest.assets)){if(!s.isText){s.rewriteComplete=true;continue}n(s.outputPath)&&(s.rewriteComplete=true);}}function Za(e,t,r){let n=new Set,s=o=>{if(!o)return;let i=ge(e.outputDir,o);i&&n.add(i);};for(let o of t.donePages)s(Fe(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(St(e,t.assetMap,o));for(let o of Object.values(r.manifest.assets))o.lastSeenRunId===r.runId&&s(o.outputPath);return n}async function Ya(e,t,r,n,s,o){let i=[...n.deletedFiles].sort(),c=Za(e,t,r),a=0;for(let l of [...c].sort())if(!o?.objects[l]){try{if((await U.stat(C.join(e.outputDir,l))).isFile())continue}catch{}a++,s.warn(`Active output is missing after crawl: ${l}`,{phase:"deploy-plan",file:l,outputPath:C.join(e.outputDir,l)});}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=[],u=0;for(let l of i){if(!c.has(l)){d.push(l);continue}u++;let g=C.join(e.outputDir,l),p;try{p=(await U.stat(g)).isFile();}catch{p=false;}s.warn(`Deploy plan protected active output from deletion: ${l}`,{phase:"deploy-plan",file:l,outputPath:g,exists:p});}return u>0&&s.info(`Deploy plan skipped ${u} active output deletion(s).`,{phase:"deploy-plan",protectedDeletes:u,deletedFiles:d.length}),d}function Lt(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 Po(e,t){let r=e.targetOrigin&&e.targetOrigin!=="."&&e.targetOrigin!=="/"?e.targetOrigin:"https://relative.invalid/";try{let n=new URL(t,r);return jt(e.outputDir,n.pathname)}catch{return null}}function Ro(e,t,r){for(let n of Xa(e,r)){let s=t[n];if(!s)continue;let o=Po(e,s);if(o)return {outputPath:o,publicUrl:s}}return null}function ec(e,t,r){for(let n of Lt(e,r)){let s=t[n];if(!s)continue;let o=Po(e,s);if(o)return o}return null}function St(e,t,r){let n=ec(e,t,r);if(n)return n;try{return Nt(e,r).originalFilePath}catch{return null}}function tc(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?C.join(C.resolve(e),"queue-runner-heartbeat.json"):""}async function Ft(e){let t=tc();if(t)try{let r=await U.readFile(t,"utf8"),n=JSON.parse(r);if(!n||typeof n!="object")return;await U.writeFile(t,JSON.stringify({...n,checkedAt:new Date().toISOString(),status:"running",currentStep:"rewrite",message:e},null,2),"utf8");}catch{}}function rc(e,t){let r=new Set;for(let n of t.donePages)r.add(C.resolve(Fe(e,n)));for(let n of t.doneAssets){let s=St(e,t.assetMap,n);!s||!$e(s)||r.add(C.resolve(s));}for(let n of t.doneSitemaps){let s=St(e,t.assetMap,n);!s||!$e(s)||r.add(C.resolve(s));}return [...r]}var Pr="re:",yr=new Map;function nc(e){if(!e.startsWith(Pr))return null;let t=yr.get(e);if(t!==void 0)return t||null;let r=e.slice(Pr.length).trim();if(!r)return yr.set(e,false),null;try{let n=new RegExp(r);return yr.set(e,n),n}catch{return console.warn(`[crawl] Ignoring invalid path matcher regex: ${e}`),yr.set(e,false),null}}function An(e,t){let r=nc(t);return r?r.test(e):t.startsWith(Pr)?false:e.startsWith(t)}function sc(e,t){return t.find(r=>An(e,r))}function vo(e,t){let r=sc(t.pathname,e.blockedPathPrefixes);if(r)return r.startsWith(Pr)?`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 oc(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 ic(e,t){return oc(e).has(t.hostname)}function ac(e){let t=Ne(e.trim()).replace(/\\\//g,"/").replace(/^['"]|['"]$/g,"").trim();return t=t.replace(/[)]+$/g,"").trim(),t=t.replace(/;.*$/g,"").trim(),t}function Rn(e){let t=Ne(e).replace(/\\\//g,"/");return /[{}]|\bwindow\.|\blocation\.|\bincludes\(|\?\?null|\+|%7B|%7D|%22|<|>|\s/.test(t)||/[()[\]'"]/.test(t)}function cc(e){let t=Ne(e).replace(/\\\//g,"/").trim(),r=C.extname(t).toLowerCase();return go.has(r)||fo.has(r)?false:/^(?:this|[A-Za-z_$][\w$]*)\.[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*$/.test(t)}function ot(e){return $a.some(t=>e.pathname.startsWith(t))}function ne(e,t,r=e.sourceOrigin,n,s="url",o=false){let i=ac(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(Rn(i))return n?.ignore(s,t,"looks like JavaScript/code fragment",r),null;if(cc(i))return n?.ignore(s,t,"looks like JavaScript property access",r),null;try{let c=new URL(i,r);if(Co(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(!ic(e,c))return n?.ignore(s,c.toString(),"host not allowed",r),null;let a=vo(e,c);if(a&&!(o&&zr(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 Co(e){try{return decodeURIComponent(e.pathname),decodeURIComponent(e.search),decodeURIComponent(e.hash),!1}catch{return true}}function ke(e,t,r,n){try{let s=new URL(e);return Co(s)?(t.reject(r,e,"malformed URL encoding",n),null):Mt(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 ko(e){return go.has(C.extname(e.pathname).toLowerCase())}function Ke(e){return ot(e)?false:fo.has(C.extname(e.pathname).toLowerCase())}function Rt(e){let t=e.pathname.toLowerCase();return t.endsWith(".xml")&&(t.includes("sitemap")||t.endsWith("/sitemap.xml"))}function Q(e,t){return !!(Rt(t)||t.pathname==="/robots.txt"||t.pathname==="/llms.txt"||t.pathname.toLowerCase().endsWith(".xsl")||e.assetPathPrefixes.length>0&&e.assetPathPrefixes.some(r=>An(t.pathname,r))||ko(t)||!Ke(t)&&!ot(t))}function Fe(e,t){let r=new URL(t),n=Mt(r.pathname);if(n===null)throw new Error(`Unsafe page URL pathname: ${r.pathname}`);return n.endsWith("/")?n+="index.html":C.extname(n)||(n+="/index.html"),jt(e.outputDir,n)}function lc(e){if(!e.generated404RequestPath)return null;try{let t=new URL(e.generated404RequestPath,e.sourceOrigin);return !t.pathname.endsWith("/")&&C.extname(t.pathname)===""&&(t.pathname+="/"),t.hash="",t.toString()}catch{return null}}function xr(e,t){let r=lc(e);if(!r)return false;try{return new URL(t,e.sourceOrigin).toString()===r}catch{return false}}function Nt(e,t){let r=new URL(t),n=Mt(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=jt(e.outputDir,o),c=sn(e.targetOrigin,o),a=`${c}${r.search}`;if(!s)return {originalPathname:o,originalFilePath:i,originalPublicUrl:c,originalPublicUrlWithSearch:a,preferQueryHashed:s};let d=C.extname(n),l=`${d?n.slice(0,-d.length):n}.${Ee(r.search)}${d||".bin"}`;return {originalPathname:o,originalFilePath:i,originalPublicUrl:c,originalPublicUrlWithSearch:a,preferQueryHashed:s,hashedPathname:l,hashedFilePath:jt(e.outputDir,l),hashedPublicUrl:sn(e.targetOrigin,l)}}function uc(e,t,r,n){let s=ke(t,r,"asset",n);return s?Nt(e,s.toString()):null}function vn(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 xo(e){try{return await U.readFile(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}async function st(e){try{return await U.stat(e)}catch(t){if(t.code==="ENOENT")return null;throw t}}function dc(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 Io(e){let t=String(e["last-modified"]||"").trim();if(!t)return null;let r=Date.parse(t);return Number.isFinite(r)?r:null}function pc(e,t){let r=Io(t),n=dc(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 mc(e,t){if(!t)return;let r=Io(t);if(r===null)return;let n=await st(e);if(!n)return;let s=new Date(r);await U.utimes(e,n.atime,s).catch(()=>{});}function Cn(e,t){let r=C.extname(e.pathname).toLowerCase(),n=t["content-type"]||"";return Ea.has(r)||Aa.some(s=>n.includes(s))&&!n.startsWith("image/")&&!n.includes("font")}function gc(e){try{let t=C.extname(new URL(e).pathname).toLowerCase();return t===".js"||t===".mjs"}catch{return false}}function Xe(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 fc(e){return Ee(e.toString("base64"),40)}function To(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 Ao(e,t,r){let n=e.assetVariantDigestsByOriginalPath.get(t);n||(n=new Set,e.assetVariantDigestsByOriginalPath.set(t,n)),n.add(r);}async function kn(e,t,r,n,s,o,i){let c=Nt(e,r),a=fc(n),d=c.originalFilePath,u=c.originalPublicUrlWithSearch;if(c.preferQueryHashed&&c.hashedFilePath&&c.hashedPublicUrl&&To(s,c.originalFilePath,a)){let f=Ro(e,t,r);f?(d=f.outputPath,u=f.publicUrl):(d=c.hashedFilePath,u=c.hashedPublicUrl);}vn(e,t,r,u,c.originalPublicUrl);let l=$e(d)?Buffer.from(ln(n.toString("utf8"),e,t,d),"utf8"):n,g=await xo(d),p=false;return (g===null||!g.equals(l))&&(await _e(d,l),p=true),o?.debug(`${p?"Saved":"Skipped unchanged"} asset output ${r}`,{phase:"download-assets",url:r,outputPath:d,bytes:l.length,wroteFile:p}),Ao(s,c.originalFilePath,a),await mc(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:u,wroteFile:p}}function it(e,t,r,n,s){let o=()=>{let l=String(s??"").trim();if(!l)return {discoveredFrom:"",discoveredFromType:"unknown"};if(l==="seed-path"||l==="cli"||l==="generated-404-request-path")return {discoveredFrom:l,discoveredFromType:l};if(/^https?:\/\//i.test(l))try{let g=new URL(l);return Rt(g)?{discoveredFrom:l,discoveredFromType:"sitemap"}:Q(e,g)?{discoveredFrom:l,discoveredFromType:"asset"}:Ke(g)&&!ot(g)?{discoveredFrom:l,discoveredFromType:"page"}:{discoveredFrom:l,discoveredFromType:"url"}}catch{return {discoveredFrom:l,discoveredFromType:"url"}}return {discoveredFrom:l,discoveredFromType:"url"}},i=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,c=ne(e,r,i,n,"page");if(!c)return;if(!Ke(c)||ko(c)){if(Q(e,c)){n.info(`Seeded/non-page URL queued as asset: ${c.toString()}`,{url:c.toString(),source:s}),Z(e,t,c.toString(),n,s);return}n.reject("page",c.toString(),ot(c)?"asset/internal path cannot be page":"not page-like",s);return}let a=c.toString(),{discoveredFrom:d,discoveredFromType:u}=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:u,outputPath:Fe(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 Z(e,t,r,n,s){let o=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,i=ne(e,r,o,n,"asset");if(!i)return;if(!Q(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 Rr(e,t,r,n,s){let o=s&&/^https?:\/\//i.test(s)?s:e.sourceOrigin,i=ne(e,r,o,n,"sitemap",ns(s));if(!i)return;if(!Rt(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 hc(e){let t=Ne(e),r=[...t.matchAll(/<url>\s*([\s\S]*?)\s*<\/url>/gi)].map(n=>n[1]);return r.length>0?r.map(n=>{let s=n.match(/<loc>\s*([^<]+?)\s*<\/loc>/i);if(!s?.[1])return null;let o=n.match(/<lastmod>\s*([^<]+?)\s*<\/lastmod>/i);return {loc:s[1].trim(),lastmod:nt(o?.[1]??null)}}).filter(n=>!!n):[...t.matchAll(/<loc>\s*([^<]+?)\s*<\/loc>/gi)].map(n=>({loc:n[1].trim(),lastmod:null})).filter(n=>n.loc)}function wc(e){return e.split(",").map(t=>t.trim().split(/\s+/)[0]).filter(Boolean)}function yc(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 bc(e){return e.replace(/<script\b[^>]*>[\s\S]*?<\/script\s*>/gi,"")}function Sc(e,t){if(t==="dom-style")return true;let r=(()=>{try{return C.extname(new URL(e).pathname).toLowerCase()}catch{return ""}})();return !(r===".js"||r===".mjs"||r===".map"||t.includes("javascript"))}function Sr(e){return e.trim().replace(/\\/g,"/").replace(/^https?:\/\/[^/]+/i,"").replace(/^\/+/,"")}async function Eo(e,t,r,n){let s=[];await U.mkdir(t,{recursive:true});let o=await U.readdir(e,{withFileTypes:true});for(let i of o){let c=C.join(e,i.name),a=C.join(t,i.name);if(i.isDirectory()){s.push(...await Eo(c,a,r,n));continue}await $o(c,a,r,n)&&s.push(a);}return s}async function Pc(e,t){try{let[r,n]=await Promise.all([U.stat(e),U.stat(t)]);if(r.size!==n.size)return !1;let[s,o]=await Promise.all([U.readFile(e),U.readFile(t)]);return s.equals(o)}catch{return false}}async function $o(e,t,r,n){let s=C.resolve(t);return r.has(s)?(n.debug("Skipped post-crawl copy over crawled page output",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):await Pc(e,t)?(n.debug("Skipped unchanged post-crawl copy target",{phase:"copy-extra-paths",sourcePath:e,targetPath:t}),false):(await U.mkdir(C.dirname(t),{recursive:true}),await U.copyFile(e,t),true)}async function Rc(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=>C.resolve(Fe(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 u=Da(a);if(!u.resolvedPath){r.warn("Skipped post-crawl copy source because alias root is not configured",{sourcePath:a,alias:u.alias||"",requiredEnv:u.requiredEnv||"",prefixPath:d});continue}let l=u.resolvedPath,g=Sr(d);if(!g){r.warn("Skipped post-crawl copy mapping with invalid export prefix",{sourcePath:a,prefixPath:d});continue}let p;try{p=await U.stat(l);}catch{r.warn("Skipped post-crawl copy source because it does not exist",{sourcePath:a,sourceAbs:l,prefixPath:d});continue}let f=C.resolve(e.outputDir,g);if(p.isDirectory()){let y=await Eo(l,f,i,r);o.push(...y),s+=y.length,y.length>0&&r.progress(`Copied static directory to export: ${l} -> /${g}`,{phase:"copy-extra-paths",sourcePath:l,targetPath:`/${g}`,copiedItems:s,changedFiles:y.length});continue}let w=C.basename(l),m=d.endsWith("/")?C.join(f,w):f;await $o(l,m,i,r)&&(o.push(m),s++,r.progress(`Copied static file to export: ${l} -> /${Sr(d.endsWith("/")?`${d}${w}`:d)}`,{phase:"copy-extra-paths",sourcePath:l,targetPath:`/${Sr(d.endsWith("/")?`${d}${w}`:d)}`,copiedItems:s}));}return r.endMark("copy-extra-paths",{mappedSources:n.length,copiedItems:s}),o}function Pt(e,t,r,n,s){let o=new Set;if(s.endsWith("page-html"))for(let d of Os(r)){let u=ne(e,d,t,n,`${s}:declared-asset`);u&&Q(e,u)&&o.add(u.toString());}let i=Ne(r).replace(/\\\//g,"/"),c=s==="page-html"?bc(i):i,a=Sc(t,s);if(a){for(let d of yc(c)){let u=ne(e,d,t,n,`${s}:css-url`);u&&Q(e,u)&&o.add(u.toString());}for(let d of c.matchAll(/@import\s+(?:url\()?\s*['"]?([^'"\s;]+)['"]?\s*\)?/gi)){let u=ne(e,d[1],t,n,`${s}:css-import`);u&&Q(e,u)&&o.add(u.toString());}}if(a||s==="page-html")for(let d of c.matchAll(/<\?xml-stylesheet[^>]+href=["']([^"']+)["'][^>]*\?>/gi)){let u=ne(e,d[1],t,n,`${s}:xml-stylesheet`);u&&Q(e,u)&&o.add(u.toString());}if(s==="page-html")for(let d of Es(i)){let u=d.startsWith("//")?`${new URL(e.sourceOrigin).protocol}${d}`:d;if(Rn(u)){n.ignore(`${s}:serialized-url`,u,"looks like JavaScript/code fragment",t);continue}let l=ne(e,u,t,n,`${s}:serialized-url`);l&&Q(e,l)?o.add(l.toString()):l&&n.ignore("asset",l.toString(),"serialized URL did not pass safe asset path filter",t);}if(s!=="page-html"&&gc(t))for(let d of $s(i)){if(Rn(d)||!/\.(?:js|mjs)(?:[?#]|$)/i.test(d))continue;let u=ne(e,d,t,n,`${s}:relative-asset`);u&&Q(e,u)&&o.add(u.toString());}return [...o]}function Oo(e,t,r,n){let s=new Set,o=Ne(r);for(let i of o.matchAll(/href=["']([^"'#\s][^"']*?)["']/gi)){let c=i[1],a=ne(e,c,t,n,"page-link");a&&(vo(e,a)||Ke(a)&&!ot(a)&&!Q(e,a)&&s.add(a.toString()));}return [...s]}async function io(e,t,r,n,s,o,i=false,c=[]){let a=new Set(c),d=0,u=new Map,l=(g,p)=>{let f=[p],w=new Set;for(;f.length>0;){let m=f.pop();if(m===g)return true;w.has(m)||(w.add(m),f.push(...u.get(m)??[]));}return false};for(;r.sitemapQueue.length>0;){let g=r.sitemapQueue.shift();r.queuedSitemaps.delete(g);let p=ke(g,n,"sitemap","sitemap-queue");if(!p)continue;let f=p.toString();if(r.doneSitemaps.has(f))continue;r.doneSitemaps.add(f),n.sitemap(`Fetching sitemap ${f}`,{url:f});let w=true;try{let m=await t.get(f,{timeout:6e4});if(w=!1,!m.ok()){if(n.skip("sitemap",f,m.status()),i&&!a.has(f))throw new Error(`Required child content-sync sitemap returned HTTP ${m.status()}: ${f}`);continue}let h=await m.body(),y=m.url()||f,b=ke(y,n,"sitemap-response",f);if(!b)continue;let S=b.toString();r.successfulSitemaps.add(f),r.successfulSitemaps.add(S),a.has(f)&&(r.resolvedSitemapRoots.add(S),d+=1);let j=await kn(e,r.assetMap,S,h,r,n);ue(s,e,j.outputPath),Ce(s,j.outputPath);let E=h.toString("utf8");/<sitemapindex(?:\s|>)/i.test(E)?r.sitemapIndexUrls.add(S):r.sitemapLeafUrls.add(S);let k=Pt(e,S,E,n,"sitemap");for(let T of k)Z(e,r,T,n,S);let v=[];for(let T of hc(E)){let $=ne(e,T.loc,S,n,"sitemap-loc",!0);if(!$){if(i)throw new Error(`Content-sync sitemap contains an inadmissible location: ${T.loc}`);continue}if(Rt($)){if(l(S,$.toString()))throw new Error(`Sitemap index cycle detected: ${S} -> ${$.toString()}`);if(u.has(S)||u.set(S,new Set),u.get(S).add($.toString()),v.push($.toString()),i&&r.doneSitemaps.has($.toString())&&!r.successfulSitemaps.has($.toString()))throw new Error(`Required child content-sync sitemap was not resolved: ${$.toString()}`);Rr(e,r,$.toString(),n,S);}else if(i){if(Ke($)&&!Q(e,$)){let N=o.requiredUrls.has($.toString()),B=!!o.previousManifest.pages[$.toString()];if(!N&&!B)throw new Error(`Content-sync sitemap coverage is incomplete for ${$.toString()}.`)}n.ignore("sitemap-loc",$.toString(),"content-sync sitemap-only traversal does not enqueue urlset locations",S);}else Q(e,$)?Z(e,r,$.toString(),n,S):Ke($)?(T.lastmod&&(r.sitemapLastmodByPage[$.toString()]=T.lastmod),it(e,r,$.toString(),n,S)):n.reject("sitemap-loc",$.toString(),"not page/sitemap/asset-like",S);}o.trackManifest&&(o.manifest.assets[S]={url:S,outputPath:j.outputPath,isText:!0,contentType:m.headers()["content-type"]??"application/xml",cacheControl:bn(m.headers()),changeToken:null,tokenSource:null,discoveredAssets:[...new Set([...k,...v])],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!1},await Ve(e,o));}catch(m){n.error(`Failed sitemap ${f}`,{url:f,error:String(m)});let h=i&&a.has(f)&&w;if(i&&!h)throw m}}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 vc(e,t,r,n,s,o,i){let c=ne(e,n,e.sourceOrigin,s,"asset-fetch");if(!c||!Q(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,u=o.trackManifest?await br(e,t,o,a,s):null,l=u?.token??null,g=un(d?.changeToken,u),p=o.strictTargeted&&(!u?.supported||!u.token)&&dn(e.sourceOrigin,a),f=o.strictTargeted?o.manifest.assets[a]:void 0;if(f?.lastSeenRunId===o.runId&&await st(f.outputPath)){r.doneAssets.add(a);for(let m of f.discoveredAssets||[])Z(e,r,m,s,a);ue(i,e,f.outputPath),bt(i,e,f.outputPath,f.cacheControl),f.isText&&Ce(i,f.outputPath),s.info(`Resumed completed content-sync asset ${a}`,{url:a,source:"content-sync-checkpoint"}),Xe(r,s);return}if(d&&(g||p)&&await st(d.outputPath)){o.manifest.assets[a]={...d,changeToken:l,tokenSource:u?.tokenSource??d.tokenSource,discoveredAssets:[...d.discoveredAssets||[]],lastSeenRunId:o.runId,unreferencedRuns:0},delete o.manifest.pages[a],r.doneAssets.add(a);for(let m of d.discoveredAssets||[])Z(e,r,m,s,a);bt(i,e,d.outputPath,d.cacheControl),s.info(`Reused unchanged trusted asset ${a}.`,{url:a,tokenSource:u?.tokenSource??"unknown",phase:"download-assets",source:g?"asset-change-token-match":"content-sync-release-fingerprint"}),Xe(r,s);return}let w=(m,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||[])Z(e,r,y,s,a);return s.warn(`Keeping previous incremental asset ${a} after fetch issue.`,{url:a,reason:m,preservedFromPreviousManifest:true,...h,phase:"download-assets"}),Xe(r,s),true};s.asset(`Fetching asset ${a}`,{url:a});try{let m=St(e,r.assetMap,a),h=m?await st(m):null;if(m&&h&&!o.strictTargeted)try{let B=await oe(`fetch asset headers ${a}`,6e4,()=>t.head(a,{timeout:6e4})),R=B.headers();if(B.ok()&&pc(h,R)){let O=await xo(m);if(O!==null){let de=B.url()||a,ie=ke(de,s,"asset-response",a);if(!ie)return;let H=ie.toString(),x=await kn(e,r.assetMap,H,O,r,s,R);x.wroteFile&&(ue(i,e,x.outputPath),$e(x.outputPath)&&Ce(i,x.outputPath));let L=bn(R)||d?.cacheControl||null;bt(i,e,x.outputPath,L),r.doneAssets.add(a),s.info(`Reused existing asset ${a} based on response headers.`,{url:a,filePath:m,lastModified:R["last-modified"]||"",contentLength:R["content-length"]||"",phase:"download-assets"}),Xe(r,s);let q=Cn(c,R),J=d?.discoveredAssets?[...d.discoveredAssets]:[];if(q||d?.isText){let z=O.toString("utf8"),pe=Pt(e,H,z,s,"asset:cached");(pe.length>0||!d)&&(J=pe);for(let we of J)Z(e,r,we,s,H);}if(o.trackManifest){let z=q||!!d?.isText;o.manifest.assets[a]={url:a,outputPath:x.outputPath,isText:z,contentType:yn(R)||d?.contentType||null,cacheControl:L,changeToken:u?.supported?u.token:null,tokenSource:u?.supported?u.tokenSource??null:null,discoveredAssets:J,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!z},delete o.manifest.pages[a],(o.enabled||o.strictTargeted)&&z&&await Ve(e,o);}return}}}catch{}let y=await oe(`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 oe(`read asset response ${a}`,6e4,()=>y.body()),S=y.url()||a,j=ke(S,s,"asset-response",a);if(!j)return;let E=j.toString(),k=y.headers(),v=await kn(e,r.assetMap,E,b,r,s,k);v.wroteFile&&(ue(i,e,v.outputPath),$e(v.outputPath)&&Ce(i,v.outputPath));let T=bn(k);bt(i,e,v.outputPath,T),r.doneAssets.add(a),Xe(r,s);let $=Cn(c,k),N=[];if($){let B=b.toString("utf8");N=Pt(e,E,B,s,`asset:${C.extname(c.pathname).toLowerCase()||k["content-type"]||"unknown"}`);for(let R of N)Z(e,r,R,s,E);}o.trackManifest&&(o.manifest.assets[a]={url:a,outputPath:v.outputPath,isText:$,contentType:yn(k),cacheControl:T,changeToken:u?.supported?u.token:null,tokenSource:u?.supported?u.tokenSource??null:null,discoveredAssets:N,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,unreferencedRuns:0,rewriteComplete:!$},delete o.manifest.pages[a],(o.enabled||o.strictTargeted)&&$&&await Ve(e,o));}catch(m){if(w("fetch-error",{error:String(m)}))return;if(s.error(`Failed asset ${a}`,{url:a,error:String(m)}),o.strictTargeted)throw m}finally{r.doneAssets.has(a)||(r.doneAssets.add(a),Xe(r,s));}}async function Cc(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 vc(e,t,r,a,n,s,o));}});await Promise.all(c);}function vr(e,t){let r=ge(e.outputDir,t);if(!r)throw new Error(`Remote export output is outside outputDir: ${t}`);return r}function kc(e,t){return Cn(e,{"content-type":t})}function ao(e){return /(?:^|\/)index\.html?$|\.html?$/i.test(e)}async function xc(e,t,r,n,s,o,i,c,a){let d=Math.max(1,Math.min(100,a)),u=20;for(;r.assetQueue.length>0;){let l=r.assetQueue.splice(0,d*u);for(let w of l)r.queuedAssets.delete(w);await Tn(e,t,s,l,n);let g=[];for(let w of l){let m=s.manifest.assets[w],h=m?ge(e.outputDir,m.outputPath):null,y=h?i.objects[h]:void 0,b=s.changeTokenCache.get(w)??null;if(!(s.enabled&&m&&y?.kind==="asset"&&y.sourceUrl===w&&(un(m.changeToken,b)||(!b?.supported||!b.token)&&dn(e.sourceOrigin,w)))||!m||!h||!y){g.push(w);continue}s.manifest.assets[w]={...m,changeToken:b?.token??null,tokenSource:b?.tokenSource??m.tokenSource,lastSeenRunId:s.runId,unreferencedRuns:0};for(let j of m.discoveredAssets||[])Z(e,r,j,n,w);r.doneAssets.add(w),bt(o,e,m.outputPath,m.cacheControl),n.info(`Reused unchanged S3-native asset ${w}.`,{url:w,relativePath:h,tokenSource:b?.tokenSource??"release-fingerprint",phase:"download-assets",execution:"lambda"}),Xe(r,n);}if(g.length===0)continue;let p=[];for(let w=0;w<g.length;w+=u)p.push(g.slice(w,w+u));let f=await Promise.all(p.map(w=>c.fetch({urls:w,ignoreHttpsErrors:e.ignoreHttpsErrors,timeoutMs:6e4})));for(let w of f)for(let m of w.assets){let h=m.requestedUrl;try{if(m.outcome!=="fetched"||!m.object||!m.finalUrl){let T=m.error||`Remote asset fetch ended with ${m.outcome}.`;if(n.error(`Failed remote asset ${h}`,{url:h,status:m.httpStatus,outcome:m.outcome,error:T,execution:"lambda"}),s.strictTargeted)throw new Error(T);continue}let y=ke(m.finalUrl,n,"remote-asset-response",h);if(!y)continue;let b=Nt(e,y.toString()),S=b.originalFilePath,j=b.originalPublicUrlWithSearch;if(b.preferQueryHashed&&b.hashedFilePath&&b.hashedPublicUrl&&To(r,b.originalFilePath,m.object.sha256)){let T=Ro(e,r.assetMap,y.toString());S=T?.outputPath??b.hashedFilePath,j=T?.publicUrl??b.hashedPublicUrl;}vn(e,r.assetMap,y.toString(),j,b.originalPublicUrl),y.toString()!==h&&vn(e,r.assetMap,h,j,b.originalPublicUrl),Ao(r,b.originalFilePath,m.object.sha256);let E=vr(e,S),k=m.contentType?.trim()||"application/octet-stream",v=kc(y,k);yt(i,{relativePath:E,sourceUrl:h,kind:"asset",contentType:k,cacheControl:m.cacheControl,isText:v,object:m.object}),ue(o,e,S),v&&Ce(o,S),bt(o,e,S,m.cacheControl);for(let T of m.discoveredAssets)Z(e,r,T,n,y.toString());r.stats.assetsSaved++,s.trackManifest&&(s.manifest.assets[h]={url:h,outputPath:S,isText:v,contentType:k,cacheControl:m.cacheControl,changeToken:null,tokenSource:null,discoveredAssets:[...m.discoveredAssets],lastCrawledAt:new Date().toISOString(),lastSeenRunId:s.runId,unreferencedRuns:0,rewriteComplete:!v},delete s.manifest.pages[h]),n.debug(`Remote asset fetch completed ${h}`,{url:h,finalUrl:y.toString(),relativePath:E,bytes:m.object.bytes,taskId:w.taskId,attempts:w.attempts,execution:"lambda"});}finally{r.doneAssets.add(h),Xe(r,n);}}}}async function Ic(e,t,r,n){for(let s of [...t.changedFiles].sort()){if(!Ys(r,s))continue;let o=C.join(e.outputDir,s),i=await U.readFile(o).catch(u=>{throw new Error(`Coordinator-owned output is missing before S3 staging: ${s}`,{cause:u})}),c=$e(o),a=String(Hs.lookup(s)||"")||(c?"text/plain; charset=utf-8":"application/octet-stream"),d=await n.writeWorkspaceObject({key:`${r.workspace.prefix}jobs/${r.jobId}/coordinator/${Ee(s,40)}-${C.basename(s)}`,body:i,contentType:a,cacheControl:ao(s)?e.s3.htmlCacheControl:e.s3.assetCacheControl});yt(r,{relativePath:s,sourceUrl:null,kind:"generated",contentType:a,cacheControl:ao(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 He(e){return Math.max(6e4,e.navigationTimeoutMs*2,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+15e3)}function Ut(e){return Math.max(15e3,Math.min(6e4,He(e)))}function Mo(e){return Math.max(15e3,e.readiness.timeoutMs+e.readiness.fallbackWaitMs+1e4)}function En(e){return e.stats.pagesCompleted}function xn(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 wn(e,t,r){let n=xn(e);try{if(n.kill){await n.kill(),t.warn(`Force-killed crawl worker browser after ${r}`,{browserPid:n.pid,reason:r});return}if(n.pid!==null){process.kill(n.pid,"SIGKILL"),t.warn(`Sent SIGKILL to crawl worker browser after ${r}`,{browserPid:n.pid,reason:r});return}t.warn(`Could not resolve crawl worker browser PID after ${r}`,{reason:r});}catch(s){t.error(`Failed to force-kill crawl worker browser after ${r}`,{browserPid:n.pid,reason:r,error:String(s)});}}async function oe(e,t,r){let n;try{return await Promise.race([r(),new Promise((s,o)=>{n=globalThis.setTimeout(()=>{o(new Wt(e,t));},t);})])}finally{n!==void 0&&globalThis.clearTimeout(n);}}var Wt=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 Ac(e,t){let r=Mo(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 u=document.body?.scrollHeight||0,l=document.documentElement?.scrollHeight||0,g=Math.max(u,l);o>=g+window.innerHeight&&a();},120);});},{maxDurationMs:r});}async function Ec(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 Ac(e,t),await t.waitForTimeout(e.readiness.fallbackWaitMs);}async function $c(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 jo(e,t){return e.noJavaScriptRenderPathPrefixes.some(r=>An(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"],u=new Set,l=["srcset","data-srcset","data-lazy-srcset"],g=p=>{p.querySelectorAll("*").forEach(f=>{for(let m of d){let h=f.getAttribute(m);h&&a.add(h);}for(let m of l){let h=f.getAttribute(m);h&&h.split(",").forEach(y=>{let b=y.trim().split(/\s+/)[0];b&&a.add(b);});}let w=f.getAttribute("style");w&&u.add(w),f.shadowRoot&&g(f.shadowRoot);});};return g(document),{attrs:[...a],styles:[...u]}});for(let a of c.attrs)for(let d of wc(a)){let u=ne(e,d,r,n,"dom-attr");u&&Q(e,u)&&o.add(u.toString());}for(let a of c.styles)for(let d of Pt(e,r,a,n,"dom-style"))o.add(d);}let i=await t.content();for(let c of Pt(e,r,i,n,"page-html"))o.add(c);return [...o]}async function In(e,t,r,n,s){let o=Fe(e,t);await _e(o,r,"utf8");let i=await U.stat(o);s?.debug(`Saved page output ${t}`,{phase:"save-pages",url:t,outputPath:o,bytes:i.size});let c=Cr(n,o),a=Math.min(n.stats.pagesQueued,En(n)+1);s&&c&&(n.stats.pagesSaved%10===0||n.stats.pagesSaved===1)&&s.progress(`Page progress: processed ${a}, rendered ${n.stats.pagesRendered}, saved ${n.stats.pagesSaved}, discovered ${n.stats.pagesQueued}.`,{donePages:a,claimedPages:n.donePages.size,pagesRendered:n.stats.pagesRendered,pagesSaved:n.stats.pagesSaved,pagesQueued:n.stats.pagesQueued,pageQueue:n.pageQueue.length,phase:"save-pages"});}function Cr(e,t){let r=C.resolve(t);return e.savedPageOutputs.has(r)?false:(e.savedPageOutputs.add(r),e.stats.pagesSaved=e.savedPageOutputs.size,true)}async function kr(e,t,r,n,s){let o=ge(e.outputDir,n);if(!o)return;if(s){let c=s.objects[o];c?.kind==="page"&&c.sourceUrl===r&&c.object?.bucket===s.workspace.bucket&&typeof c.object.key=="string"&&c.object.key.startsWith(s.workspace.prefix)&&Cr(t,n);return}(await st(n).catch(()=>null))?.isFile()&&Cr(t,n);}function Fo(e){let t=En(e);return t%5===0||t===1||t===e.stats.pagesQueued}function Do(e,t){let r=En(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 Mc(e){await ls(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 jc(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 Mc(n),n}async function Fc(e,t,r,n,s,o,i,c,a,d){for(;t.pageQueue.length>0&&!(e.maxPages>0&&t.donePages.size>=e.maxPages);){let u=t.pageQueue.shift();if(!u)break;t.queuedPages.delete(u);try{let l=ke(u,r,"page","page-queue");if(!l)continue;let g=l.toString();if(u=g,t.queuedPages.delete(u),t.donePages.has(u))continue;t.donePages.add(u),o.enabled&&o.seenPages.add(u);let p=xr(e,u),f=jo(e,l.pathname);if(!Ke(l)||Q(e,l)||ot(l)){if(Q(e,l)){r.info(`Remote worker redirected non-page URL to asset queue: ${u}`,{url:u,source:"remote-worker-guard"}),Z(e,t,u,r,"remote-worker-guard");continue}r.reject("page",u,"guard rejected non-page URL before remote rendering","remote-worker");continue}let w=o.strictTargeted?o.manifest.pages[u]:void 0;if(w?.lastSeenRunId===o.runId&&await st(w.outputPath)){o.seenPages.add(u);for(let v of w.discoveredAssets||[])Z(e,t,v,r,u);ue(i,e,w.outputPath),Ce(i,w.outputPath),await kr(e,t,u,w.outputPath,d),r.info(`Resumed completed content-sync page ${u}`,{url:u,source:"content-sync-checkpoint"});continue}let m=await So(e,s,t,o,u,r);if(o.enabled&&n!=="single-url"&&n!=="content-sync"){let v=o.manifest.pages[u];if(m.action==="reuse"&&v){for(let T of v.discoveredAssets)Z(e,t,T,r,u);for(let T of v.discoveredPages)it(e,t,T,r,u);v.lastSeenRunId=o.runId,v.sitemapLastmod=nt(t.sitemapLastmodByPage[u])??v.sitemapLastmod,m.changeToken?.supported&&(v.changeToken=m.changeToken.token,v.tokenSource=m.changeToken.tokenSource??v.tokenSource),await kr(e,t,u,v.outputPath,d),r.info(`Incremental reuse skipped unchanged page ${u}`,{url:u,mode:"incremental",reason:m.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}r.page(`Rendering remotely ${u}`,{url:u,execution:"lambda"});let h=await a.render({sourceOrigin:e.sourceOrigin,url:u,options:{ignoreHttpsErrors:e.ignoreHttpsErrors,javaScriptEnabled:!f,viewport:e.viewport,navigationTimeoutMs:e.navigationTimeoutMs,autoScrollTimeoutMs:Mo(e),readiness:e.readiness},downloadHtml:!d}),y=h.page,b=y.finalUrl?ke(y.finalUrl,r,"remote-final-response",u)?.toString():void 0;if(n==="content-sync"&&b&&b!==g){if(o.deleteOnMissingUrls.has(u)){await qt(e,o,i,u,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive route redirected to ${b}`,{url:u,finalResponseUrl:b,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${u} -> ${b}`)}let S=y.httpStatus;if(p){if(S!==404){r.error(`Generated 404 request path returned unexpected status for ${u}`,{url:u,expectedStatus:404,status:S,requestPath:e.generated404RequestPath,execution:"lambda"});continue}}else if(S!==null&&S>=400){if(n==="content-sync"&&(S===404||S===410)&&o.deleteOnMissingUrls.has(u)){await qt(e,o,i,u,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive page ${u}`,{url:u,status:S,source:"content-sync-tombstone"});continue}if(n==="content-sync")throw new Error(`Required content-sync route returned HTTP ${S}: ${u}`);r.skip("page",u,S,t.pageDiscoverySources.get(u));continue}t.stats.pagesRendered++;let j=new Set;if(!d){if(h.html===null)throw new Error("Remote render did not return HTML for local output mode.");for(let v of Pt(e,g,h.html,r,"remote-page-html"))j.add(v);}for(let v of [...y.discoveredAssets,...y.networkUrls]){let T=ne(e,v,g,r,"remote-render");T&&Q(e,T)&&j.add(T.toString());}for(let v of j)Z(e,t,v,r,u);let E=n!=="single-url"&&n!=="content-sync"&&!p?[...new Set([...!d&&h.html!==null?Oo(e,u,h.html,r):[],...y.discoveredPages])]:[];for(let v of E)it(e,t,v,r,u);for(let v of y.cookies){let T=v.sameSite==="Strict"||v.sameSite==="Lax"||v.sameSite==="None"?v.sameSite:"Unspecified",$=!Number.isFinite(v.expires)||v.expires<=0;c.add({name:v.name,domain:v.domain,path:v.path||"/",expires:$?null:v.expires,session:$,secure:v.secure,httpOnly:v.httpOnly,sameSite:T,partitioned:v.partitioned===!0});}let k=Fe(e,u);if(d){if(!y.html)throw new Error("Remote render completed without a workspace HTML object.");yt(d,{relativePath:vr(e,k),sourceUrl:u,kind:"page",contentType:y.contentType?.trim()||"text/html; charset=utf-8",cacheControl:e.s3.htmlCacheControl,isText:!0,object:y.html}),Cr(t,k);}else {if(h.html===null)throw new Error("Remote render did not return HTML for local output mode.");await In(e,u,h.html,t,r);}ue(i,e,k),Ce(i,k),o.trackManifest&&(o.manifest.pages[u]={url:u,outputPath:k,contentType:y.contentType?.trim()||null,changeToken:m.changeToken?.supported?m.changeToken.token:null,tokenSource:m.changeToken?.supported?m.changeToken.tokenSource??null:null,sitemapLastmod:nt(t.sitemapLastmodByPage[u])??null,discoveredPages:E,discoveredAssets:[...j],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Ve(e,o)),r.debug(`Remote render completed ${u}`,{url:u,taskId:h.taskId,attempts:h.attempts,execution:"lambda"});}catch(l){if(r.error(`Failed remote page ${u}`,{url:u,error:String(l),execution:"lambda",...l instanceof be?{status:l.status,attempts:l.attempts}:{}}),l instanceof be){t.pageHttpFailures.set(u,l);continue}if(o.strictTargeted)throw l}finally{t.stats.pagesCompleted++,Fo(t)&&Do(t,r);}}}async function Dc(e,t,r,n,s,o,i,c){let a=await chromium.launch({headless:true}),d=xn(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=xn(a),d.pid!==null&&r.debug(`Relaunched crawl worker browser PID ${d.pid}`,{browserPid:d.pid}));let u=a;if(e.maxPages>0&&t.donePages.size>=e.maxPages)break;let l=t.pageQueue.shift();if(!l)break;t.queuedPages.delete(l);let g=null,p=null,f=!1,w=null,m=[],h=new Set;try{let y=ke(l,r,"page","page-queue");if(!y)continue;let b=y.toString();if(l=b,t.queuedPages.delete(l),t.donePages.has(l))continue;t.donePages.add(l),o.enabled&&o.seenPages.add(l);let S=xr(e,l),j=jo(e,y.pathname);if(!Ke(y)||Q(e,y)||ot(y)){if(Q(e,y)){r.info(`Worker redirected non-page URL to asset queue: ${l}`,{url:l,source:"worker-guard"}),Z(e,t,l,r,"worker-guard");continue}r.reject("page",l,"guard rejected non-page URL before rendering","worker");continue}let E=o.strictTargeted?o.manifest.pages[l]:void 0;if(E?.lastSeenRunId===o.runId&&await st(E.outputPath)){o.seenPages.add(l);for(let x of E.discoveredAssets||[])Z(e,t,x,r,l);ue(i,e,E.outputPath),Ce(i,E.outputPath),await kr(e,t,l,E.outputPath),r.info(`Resumed completed content-sync page ${l}`,{url:l,source:"content-sync-checkpoint"});continue}let k=await So(e,s,t,o,l,r);if(o.enabled&&n!=="single-url"&&n!=="content-sync"){let x=o.manifest.pages[l];if(k.action==="reuse"&&x){for(let L of x.discoveredAssets)Z(e,t,L,r,l);for(let L of x.discoveredPages)it(e,t,L,r,l);x.lastSeenRunId=o.runId,x.sitemapLastmod=nt(t.sitemapLastmodByPage[l])??x.sitemapLastmod,k.changeToken?.supported&&(x.changeToken=k.changeToken.token,x.tokenSource=k.changeToken.tokenSource??x.tokenSource),await kr(e,t,l,x.outputPath),r.info(`Incremental reuse skipped unchanged page ${l}`,{url:l,mode:"incremental",reason:k.changeToken?.supported===!0?"change-token-match":"sitemap-lastmod-match"});continue}}g=await oe(`create page context ${l}`,He(e),()=>jc(u,e,j)),p=await oe(`create page ${l}`,He(e),()=>g.newPage()),m=await g.cookies();let v=!1,T=new Set;await p.route("**/*",async x=>{let L=x.request();try{let q=ne(e,L.url(),e.sourceOrigin,r,"network-request");q&&Q(e,q)&&(T.add(q.toString()),Z(e,t,q.toString(),r,l));}catch{}if(L.isNavigationRequest()&&L.frame()===p.mainFrame())try{let q=new URL(L.url()),J=new URL(b);if(q.hash="",J.hash="",q.href===J.href){if(v){r.warn(`Blocked same-page navigation/reload for ${l}`,{url:l,requestUrl:L.url()});try{await x.abort("aborted");}catch(z){r.debug(`Ignored canceled route while blocking reload for ${l}`,{url:l,error:String(z)});}return}v=!0;}}catch{}try{await x.continue();}catch(q){r.debug(`Ignored canceled network route for ${l}`,{url:l,requestUrl:L.url(),error:String(q)});}}),p.on("response",x=>{try{let q=ne(e,x.url(),e.sourceOrigin,r,"network-response");q&&Q(e,q)&&(T.add(q.toString()),Z(e,t,q.toString(),r,l));}catch{}let L=x.headerValues("set-cookie").then(q=>{for(let J of q)c.add(Us(J,x.url()));}).catch(()=>{}).then(()=>{});h.add(L),L.finally(()=>h.delete(L));}),r.page(`Rendering ${l}`,{url:l});let $=null,N=null;try{if($=await p.goto(l,{waitUntil:"domcontentloaded",timeout:e.navigationTimeoutMs}),$&&$.ok())try{N=await oe(`read response body ${l}`,He(e),()=>$.text());}catch{N=null;}}catch(x){let L=t.pageHttpFailures.get(l);if(L){let q=(t.pageHttpAttempts.get(l)??0)+1;throw t.pageHttpAttempts.set(l,q),new be(l,L.status,q,`Retry navigation failed: ${String(x)}`)}r.warn(`Navigation issue for ${l}; saving current DOM if available`,{url:l,error:String(x)});}let B=$?.status();if(cr(B)){let x=(t.pageHttpAttempts.get(l)??0)+1;throw t.pageHttpAttempts.set(l,x),new be(l,B,x)}if($)t.pageHttpFailures.delete(l);else if(t.pageHttpFailures.has(l)){let x=t.pageHttpFailures.get(l),L=(t.pageHttpAttempts.get(l)??0)+1;throw t.pageHttpAttempts.set(l,L),new be(l,x.status,L,"Retry navigation returned no HTTP response.")}if(n==="content-sync"&&$){let x=ke($.url(),r,"content-sync-final-response",l)?.toString();if(x&&x!==b){if(o.deleteOnMissingUrls.has(l)){await qt(e,o,i,l,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive route redirected to ${x}`,{url:l,finalResponseUrl:x,source:"content-sync-tombstone"});continue}throw new Error(`Required content-sync route redirected outside its planned URL: ${l} -> ${x}`)}}if(S){let x=$?.status()??null;if(x!==404){r.error(`Generated 404 request path returned unexpected status for ${l}`,{url:l,expectedStatus:404,status:x,requestPath:e.generated404RequestPath});continue}r.info(`Capturing rendered 404 page for ${l}`,{url:l,status:x,outputPath:Fe(e,l)});}else if($&&!$.ok()){if(n==="content-sync"&&($.status()===404||$.status()===410)&&o.deleteOnMissingUrls.has(l)){await qt(e,o,i,l,o.deleteOnMissingUrls),r.info(`Removed stale content-sync archive page ${l}`,{url:l,status:$.status(),source:"content-sync-tombstone"}),await p.close();continue}if(n==="content-sync")throw new Error(`Required content-sync route returned HTTP ${$.status()}: ${l}`);r.skip("page",l,$.status(),t.pageDiscoverySources.get(l)),await p.close();continue}j?(r.info(`Rendering without JS execution for ${l}`,{url:l,mode:"no-js"}),await oe(`wait for rendered page ${l}`,He(e),()=>$c(e,p))):await oe(`wait for rendered page ${l}`,He(e),()=>Ec(e,p)),t.stats.pagesRendered++;let R=await oe(`extract rendered assets ${l}`,He(e),()=>Oc(e,p,b,r,!j));for(let x of R)Z(e,t,x,r,l);let O=[...new Set([...R,...T])],de=N??await oe(`capture rendered html ${l}`,He(e),()=>Yr(p)),ie=n!=="single-url"&&n!=="content-sync"&&!S?Oo(e,l,de,r):[];if(n!=="single-url"&&n!=="content-sync"&&!S)for(let x of ie)it(e,t,x,r,l);await In(e,l,de,t,r);let H=Fe(e,l);if(ue(i,e,H),Ce(i,H),o.trackManifest){let x=$?.headers();o.manifest.pages[l]={url:l,outputPath:H,contentType:yn(x),changeToken:k.changeToken?.supported?k.changeToken.token:null,tokenSource:k.changeToken?.supported?k.changeToken.tokenSource??null:null,sitemapLastmod:nt(t.sitemapLastmodByPage[l])??null,discoveredPages:ie,discoveredAssets:O,lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Ve(e,o);}}catch(y){if(y instanceof be){t.pageHttpFailures.set(l,y),y.attempts<gn?(w=lr(y.attempts),r.warn(`Retrying page after HTTP ${y.status}: ${l}`,{url:l,status:y.status,attempt:y.attempts,maxAttempts:gn,delayMs:w,reason:y.message,execution:"local"})):(t.pageHttpFailures.set(l,y),r.error(`Failed page after HTTP retries ${l}`,{url:l,status:y.status,attempts:y.attempts,error:String(y),execution:"local"}));continue}if(f=y instanceof Wt,o.strictTargeted)throw r.error(`Failed required content-sync page ${l}`,{url:l,error:String(y)}),y;if(f)r.warn(`Skipped partial DOM for timed-out page ${l}`,{url:l,error:String(y),recovery:"recycle-worker-browser"});else try{let b=p?await oe(`capture partial html ${l}`,He(e),()=>Yr(p)):"";if(b&&b.trim()){await In(e,l,b,t,r);let S=Fe(e,l);if(ue(i,e,S),Ce(i,S),o.trackManifest){let j=o.manifest.pages[l]??o.previousManifest.pages[l];o.manifest.pages[l]={url:l,outputPath:S,contentType:j?.contentType??null,changeToken:null,tokenSource:null,sitemapLastmod:nt(t.sitemapLastmodByPage[l])??j?.sitemapLastmod??null,discoveredPages:j?.discoveredPages?[...j.discoveredPages]:[],discoveredAssets:j?.discoveredAssets?[...j.discoveredAssets]:[],lastCrawledAt:new Date().toISOString(),lastSeenRunId:o.runId,rewriteComplete:!1},(o.enabled||o.strictTargeted)&&await Ve(e,o);}r.warn(`Saved partial DOM for ${l}`,{url:l,error:String(y)});}}catch(b){b instanceof Wt&&(f=!0),r.error(`Could not save partial DOM for ${l}`,{url:l,error:String(b)});}r.error(`Failed page ${l}`,{url:l,error:String(y)});}finally{if(!f&&p&&await oe(`drain network routes ${l}`,Ut(e),()=>p.unrouteAll({behavior:"wait"})).catch(y=>{f=!0,r.warn(`Could not drain network routes for ${l}`,{url:l,browserPid:d.pid,error:String(y),recovery:"recycle-worker-browser"});}),f&&(a=null,await wn(u,r,`page operation timeout for ${l}`),await u.close().catch(()=>{})),h.size>0&&await Promise.allSettled([...h]),!f&&g)try{let y=await g.cookies();c.addAll(Ls(m,y));}catch{}w===null&&(t.stats.pagesCompleted++,Fo(t)&&Do(t,r)),!f&&p&&await oe(`close page ${l}`,Ut(e),()=>p.close()).catch(y=>{f=!0,r.warn(`Timed out while closing page ${l}`,{url:l,browserPid:d.pid,error:String(y),recovery:"recycle-worker-browser"});}),!f&&g&&await oe(`close page context ${l}`,Ut(e),()=>g.close()).catch(y=>{f=!0,r.warn(`Timed out while closing page context for ${l}`,{url:l,browserPid:d.pid,error:String(y),recovery:"recycle-worker-browser"});}),f&&a===u&&(a=null,await wn(u,r,`page cleanup timeout for ${l}`),await u.close().catch(()=>{})),w!==null&&(await setTimeout$1(w),t.donePages.delete(l),t.queuedPages.add(l),t.pageQueue.push(l));}}}finally{if(a){let u=a;await oe("close worker browser",Ut(e),()=>u.close()).catch(l=>(r.warn("Timed out while closing crawl worker browser",{browserPid:d.pid,error:String(l)}),wn(u,r,"browser.close timeout")));}}}async function Uc(e){try{let t=await U.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 co(e){let t=String(e||"").toLowerCase();return t.includes("timeout")||t.includes("timed out")||t.includes("navigation timeout")}function Lc(e){let t=String(e?.job?.command||"").trim(),r=String(e?.job?.crawlMode||"full").trim();return t!=="publish"&&t!=="crawl"?false:r!=="incremental"}function Wc(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 Uo(e){try{let t=await U.readFile(e,"utf8"),r=JSON.parse(t);return r&&typeof r=="object"&&!Array.isArray(r)?r:null}catch{return null}}function lo(e){let t=String(e.originalFileName||"").toLowerCase();return t.endsWith("errors.jsonl")?0:t.endsWith("errors.json")?1:2}function qc(e,t){let r=Array.isArray(t?.artifacts)?t.artifacts.filter(n=>String(n?.role||"").trim()==="errors"&&String(n?.storedFileName||"").trim()!=="").sort((n,s)=>lo(n)-lo(s)).map(n=>C.join(e,String(n.storedFileName))):[];return [...new Set([...r,C.join(e,"errors.jsonl"),C.join(e,"errors.json"),C.join(e,"errors.jsonl.gz"),C.join(e,"errors.json.gz")])]}async function uo(e){let t=await Uo(C.join(e,"job.json"));for(let r of qc(e,t)){let n=await Uc(r);if(n.length>0)return n}return []}async function _c(e){let t=C.join(e.logDir,"archive"),r;try{r=await U.readdir(t);}catch{return ""}let n="",s=0;for(let o of r){let i=C.join(t,o);if(!(await U.stat(i).catch(()=>null))?.isDirectory())continue;let a=await Uo(C.join(i,"job.json"));if(!Lc(a))continue;let d=Wc(a);d>=s&&(s=d,n=i);}return n}async function Nc(e){let t=await _c(e),r=t!==""?await uo(t):await uo(e.logDir),n=new Set;for(let s of r)if(co(s.error)||co(s.message)){let o=s.url;typeof o=="string"&&o&&n.add(o);}return [...n]}async function Bc(e){let t=[];for(let r of e)if(r.startsWith("@")){let n=r.slice(1),s=await U.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 Hc(e){if(!e)throw new Error("Content-sync crawl requires an immutable impact plan.");let t=JSON.parse(await U.readFile(e,"utf8"));return It(t),t}function Vc(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 Dt(e,t,r){let n=r==="page"?Fe(e,t):Nt(e,t).originalFilePath;return ge(e.outputDir,n)}async function qt(e,t,r,n,s){let o=t.manifest.pages[n];if(!o)return;let i=ge(e.outputDir,o.outputPath);if(!i)throw new Error(`Manifest-owned page path escaped output root: ${n}`);let c=C.resolve(o.outputPath);Object.entries(t.manifest.pages).some(([d,u])=>d!==n&&!s.has(d)&&C.resolve(u.outputPath)===c)||(await U.rm(o.outputPath,{force:true}),r.deletedFiles.add(i),r.revalidateFiles.delete(i)),delete t.manifest.pages[n];}async function Kc(e,t,r,n,s){let o=t.manifest.assets[n];if(!o)return;let i=ge(e.outputDir,o.outputPath);if(!i)throw new Error(`Manifest-owned resource path escaped output root: ${n}`);let c=C.resolve(o.outputPath);Object.entries(t.manifest.assets).some(([d,u])=>d!==n&&!s.has(d)&&C.resolve(u.outputPath)===c)||(await U.rm(o.outputPath,{force:true}),r.deletedFiles.add(i)),delete t.manifest.assets[n];}function zc(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{Rt(new URL(i))&&n.push(i);}catch{}}return r}async function Jc(e,t,r,n,s){let o=zc(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=ge(e.outputDir,c.outputPath);if(!a)throw new Error(`Manifest-owned sitemap path escaped output root: ${i}`);await U.rm(c.outputPath,{force:true}),n.deletedFiles.add(a),delete r.manifest.assets[i];}await Ve(e,r);}async function Qc(e){try{let t=await U.readFile(C.join(e.outputDir,"asset-map.json"),"utf8");return JSON.parse(t)}catch{return {}}}async function Gc(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 U.mkdir(e,{recursive:true}),await Promise.all(t.map(async r=>{try{await U.unlink(C.join(e,r));}catch(n){if(n.code!=="ENOENT")throw n}}));}function po(e,t,r,n,s){let o=ne(e,r,e.sourceOrigin,n,"manual-url");o&&(Rt(o)?Rr(e,t,o.toString(),n,s):Q(e,o)?Z(e,t,o.toString(),n,s):Ke(o)?it(e,t,o.toString(),n,s):n.reject("manual-url",o.toString(),"not page/sitemap/asset-like",s));}async function Lo(e=process.argv.slice(2)){let t=[],r=await Ie(void 0,{onRemotePublisherConfigDiagnostic:P=>{t.push(P);}}),n=At(r.sitemapPaths),s=t.at(-1)??null,o=Oa(e),i=o.mode==="content-sync"?await Hc(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 Nc(r)),o.mode==="single-url"&&(c=await Bc(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 u=(o.mode==="full"||o.mode==="single-url"||o.mode==="content-sync")&&!o.resumeRewrite,l=o.mode!=="full"||o.resumeRewrite||d,g=l?await Qc(r):{},p=d||o.mode==="single-url"||o.mode==="content-sync"?await qa(r):wo(),f=new Set(c),w=new Set;if(i){for(let P of i.archiveFamilies)if(P.foundPosts===0)for(let F of P.pageUrls)w.add(F);for(let P of Object.keys(p.pages))Vc(P,i)&&!f.has(P)&&(c.push(P),w.add(P));}let m={enabled:d,trackManifest:u,manifest:Pn(p),previousManifest:Pn(p),runId:i?.jobId??`${Date.now()}`,seenPages:new Set,changeTokenCache:new Map,pageChangeTokenPrimePromise:null,manifestSaveChain:Promise.resolve(),manifestPendingChanges:0,lastManifestSavedAt:Date.now(),strictTargeted:!!i,requiredUrls:f,deleteOnMissingUrls:w};await Gc(r.logDir);let h=new nr(r.logDir,r.logLevel),y=o.remoteRender??r.remoteWorkers?.render?.enabled===true,b=gr({explicitPath:o.remoteWorkerConfigPath,runtimeDir:_t()}),S=null,j=null,E=null,k=null,v=Math.max(1,r.concurrency),T=1,$=r.remoteWorkers?.rewrite?.enabled===true,N=r.remoteWorkers?.deploy?.enabled===true,B=y&&$&&N&&!o.resumeRewrite;if(!B&&o.mode==="full"&&!o.resumeRewrite&&await U.rm(fr(r),{force:true}),y&&!o.resumeRewrite||$){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 P=await ar(b),F=false;if(E=new wt(P,{},{onProgress:M=>{if(M.operation==="rewrite"){h.debug("Lambda rewrite task progress.",{remoteWorkerOperation:M.operation,remoteWorkerTaskId:M.taskId,remoteWorkerStatus:M.status,remoteWorkerCompletedItems:M.completedItems,remoteWorkerFailedItems:M.failedItems,remoteWorkerTotalItems:M.totalItems,remoteWorkerUpdatedAt:M.updatedAt});return}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=>{F||(F=true,h.warn("Lambda progress status could not be read; task execution continues.",{error:M.message}));}}),B){if(!P.functions.asset)throw new Error("Full Lambda delegation requires an asset worker. Redeploy the Lambda worker stack and reinstall remote-workers.json.");let M=l?await wr(r):null;k=Zs(`crawl-${Ee(m.runId,40)}`,P,M),j=new dr({client:E,jobId:k.jobId,maxAttempts:2}),h.info("S3-native Lambda data plane is enabled.",{execution:"lambda",assetWorker:P.functions.asset,workspaceBucket:P.workspace.bucket,workspacePrefix:P.workspace.prefix});}}if(y&&!o.resumeRewrite){if(!E)throw new Error("Remote render worker client was not initialized.");v=Math.max(1,Math.min(100,B?r.concurrency:r.remoteWorkers?.render?.concurrency??r.concurrency)),T=Math.max(1,Math.min(20,r.remoteWorkers?.render?.batchSize??5)),S=new ur({client:E,jobId:`crawl-${Ee(m.runId,40)}`,maxAttempts:r.remoteWorkers?.render?.maxAttempts??2,batchSize:T,maxConcurrentBatches:v,onRetry:P=>{h.warn(`Retrying remote render for ${P.url}`,{url:P.url,attempt:P.attempt,maxAttempts:P.maxAttempts,reason:P.reason,status:P.status,delayMs:P.delayMs,execution:"lambda"});}}),h.info("Lambda page rendering is enabled.",{remoteWorkerConfigPath:C.resolve(b),concurrency:v,maxAttempts:r.remoteWorkers?.render?.maxAttempts??2,batchSize:T,execution:"lambda"});}if(s){let P={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.",P):h.warn("Remote WP Suite publisher configuration is unavailable or has no active subscription type.",P);}if(a&&!d){let P="Incremental crawl requires an active remote publisher subscription; refusing to fall back to a destructive full crawl.";throw h.error(P,{requestedMode:o.crawlMode,subscriptionType:r.subscriptionType??null,source:"incremental-subscription-guard"}),await h.flush(),new Error(P)}let R={pageQueue:[],queuedPages:new Set,donePages:new Set,savedPageOutputs:new Set,pageHttpAttempts:new Map,pageHttpFailures:new Map,pageDiscoverySources:new Map,assetQueue:[],queuedAssets:new Set,doneAssets:new Set,sitemapQueue:[],queuedSitemaps:new Set,doneSitemaps:new Set,successfulSitemaps:new Set,resolvedSitemapRoots:new Set,sitemapIndexUrls:new Set,sitemapLeafUrls:new Set,sitemapLastmodByPage:{},assetMap:{...g},assetVariantDigestsByOriginalPath:new Map,stats:{pagesQueued:0,pagesRendered:0,pagesCompleted:0,assetsQueued:0,sitemapsQueued:0,assetsSaved:0,pagesSaved:0}},O=Wa(),de=new or,ie=Ma();if(ie&&!o.resumeRewrite&&await ir(ie,de.artifact()),await Ze(r),await nn(r,{startedAt:new Date().toISOString(),outputDir:C.resolve(r.outputDir)}),!o.resumeRewrite&&r.pageCache?.purgeBeforeCrawl===true){h.mark("purge-page-cache");let P=await request.newContext({ignoreHTTPSErrors:r.ignoreHttpsErrors});try{let F=await Qs(r,P);h.info("WordPress page cache was purged before crawl discovery.",{provider:F.provider,scope:F.scope,purgedAt:F.purgedAt??null}),h.endMark("purge-page-cache",{provider:F.provider,scope:F.scope});}catch(F){throw h.error("WordPress page-cache purge failed; crawl stopped before changing export output.",{error:F instanceof Error?F.message:String(F)}),await h.flush(),F}finally{await P.dispose();}}if(l||await U.rm(r.outputDir,{recursive:true,force:true}),await U.mkdir(r.outputDir,{recursive:true}),l||await nn(r,{startedAt:new Date().toISOString(),outputDir:C.resolve(r.outputDir)}),i){let P=new Set([...i.deleteUrls,...m.deleteOnMissingUrls]);for(let M of i.deleteUrls)await qt(r,m,O,M,P);let F=new Set(i.resourceDeleteUrls);for(let M of i.resourceDeleteUrls)await Kc(r,m,O,M,F);}if(o.resumeRewrite)h.info("Resuming final rewrite from existing output.",{phase:"rewrite-text",mode:o.mode,crawlMode:o.crawlMode});else {let P=await request.newContext({ignoreHTTPSErrors:r.ignoreHttpsErrors});try{if(h.mark("discovery"),o.mode==="full"?(n.forEach(M=>Rr(r,R,M,h)),await io(r,P,R,h,O,m),r.seedPaths.forEach(M=>po(r,R,M,h,"seed-path")),r.generated404RequestPath&&it(r,R,r.generated404RequestPath,h,"generated-404-request-path")):(c.forEach(M=>po(r,R,M,h,"cli")),i?.sitemapUrls.forEach(M=>Rr(r,R,M,h,"content-sync-impact")),await io(r,P,R,h,O,m,o.mode==="content-sync",i?.sitemapUrls??[]),i&&await Jc(r,R,m,O,i.sitemapUrls)),h.endMark("discovery",{pages:R.pageQueue.length,assets:R.assetQueue.length,sitemaps:R.sitemapQueue.length}),h.summary(`Queued ${R.pageQueue.length} pages, ${R.assetQueue.length} assets, ${R.sitemapQueue.length} sitemaps.`,{mode:o.mode,crawlMode:o.crawlMode,queuedPages:R.pageQueue.length,queuedAssets:R.assetQueue.length,queuedSitemaps:R.sitemapQueue.length}),o.mode==="retry-timeouts"&&R.pageQueue.length===0&&R.assetQueue.length===0&&R.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(),ie&&await ir(ie,de.artifact());return}h.mark("render-pages");let F=Array.from({length:S?v*T:v},()=>S?Fc(r,R,h,o.mode,P,m,O,de,S,k??void 0):Dc(r,R,h,o.mode,P,m,O,de));if(await Promise.all(F),R.pageHttpFailures.size>0&&await h.flush(),Bs(R.pageHttpFailures),ie&&await ir(ie,de.artifact()),h.endMark("render-pages",{pages:R.stats.pagesCompleted}),h.mark("download-assets"),h.progress("Asset download phase started.",{phase:"download-assets",doneAssets:R.doneAssets.size,assetsQueued:R.stats.assetsQueued,assetQueue:R.assetQueue.length}),k&&j?await xc(r,P,R,h,m,O,k,j,r.concurrency):(await Tn(r,P,m,[...R.assetQueue],h),await Cc(r,P,R,h,m,O)),h.endMark("download-assets",{assets:R.doneAssets.size}),m.enabled&&r.maxPages===0&&(await za(r,R,m,h,O),o.mode==="full"&&await Ja(r,m,h,O)),o.mode==="full"){let M=await Rc(r,R,h);for(let V of M)if(ue(O,r,V),k&&E){let G=vr(r,V),ae=await U.readFile(V),A=$e(V),W=String(Hs.lookup(G)||"")||(A?"text/plain; charset=utf-8":"application/octet-stream"),I=await E.writeWorkspaceObject({key:`${k.workspace.prefix}jobs/${k.jobId}/copied/${Ee(G,40)}-${C.basename(G)}`,body:ae,contentType:W});yt(k,{relativePath:G,sourceUrl:null,kind:"generated",contentType:I.contentType,cacheControl:null,isText:A,object:I}),A&&Ce(O,V);}}}finally{await oe("dispose crawl request context",Ut(r),()=>P.dispose()).catch(F=>{h.warn("Timed out while disposing crawl request context",{error:String(F)});});}}if(m.trackManifest&&await Ve(r,m,true),k)for(let P of O.deletedFiles)Xs(k,P);let H=JSON.stringify(R.assetMap,null,2);if(await U.writeFile(C.join(r.outputDir,"asset-map.json"),H,"utf8"),k&&E){let P=await E.writeWorkspaceObject({key:`${k.workspace.prefix}jobs/${k.jobId}/generated/asset-map.json`,body:H,contentType:"application/json; charset=utf-8"});yt(k,{relativePath:"asset-map.json",sourceUrl:null,kind:"generated",contentType:"application/json; charset=utf-8",cacheControl:r.s3.assetCacheControl,isText:true,object:P});}JSON.stringify(g,null,2)!==JSON.stringify(R.assetMap,null,2)&&ue(O,r,C.join(r.outputDir,"asset-map.json")),k&&E&&await Ic(r,O,k,E);let x=o.mode==="single-url"||o.mode==="content-sync"?rc(r,R):m.enabled&&!o.resumeRewrite?Qa(r,m,O,g,R.assetMap):void 0;h.mark("rewrite-text");let L=o.mode==="single-url"||o.mode==="content-sync"?`Rewriting text files touched by URL crawl: 0/${x?.length??0}`:"Rewriting text files...";h.progress(L,{phase:"rewrite-text",index:0,totalFiles:x?.length,changedTextFiles:0}),await Ft(L);let q=Date.now(),J={files:x,previousAssetMap:g,onStart:({totalFiles:P,workerCount:F})=>{h.info(`Rewrite phase scanning ${P} text files.`,{phase:"rewrite-text",totalFiles:P,workerCount:F});},onProgress:async({index:P,totalFiles:F,changedTextFiles:M,file:V,changed:G})=>{G&&ue(O,r,C.join(r.outputDir,V)),h.checkpoint(`Rewriting text file ${P}/${F}`,{phase:"rewrite-text",index:P,totalFiles:F,changedTextFiles:M,file:V}),h.debug(`${G?"Rewrote":"Checked unchanged"} text file ${P}/${F}: ${V}`,{phase:"rewrite-text",index:P,totalFiles:F,changedTextFiles:M,file:V,changed:G});let ae=Date.now();if(!(P===1||P===F||P%50===0||ae-q>=5e3))return;q=ae;let W=`Rewriting text files: ${P}/${F}`;h.progress(W,{phase:"rewrite-text",index:P,totalFiles:F,changedTextFiles:M,file:V}),await Ft(W);}},z;if(k&&E){let P=x?.map(M=>vr(r,C.isAbsolute(M)?M:C.join(r.outputDir,M))),F=Object.values(k.objects).filter(M=>M.isText&&(!P||P.includes(M.relativePath))).length;await J.onStart?.({totalFiles:F,workerCount:Math.min(r.concurrency,F)}),z=await mr(r,k,R.assetMap,{client:E,jobId:k.jobId,concurrency:r.concurrency,batchSize:25,maxAttempts:2,...P?{relativePaths:P}:{},onProgress:J.onProgress,onBatchProgress:async({completedItems:M,totalItems:V,completedBatches:G,totalBatches:ae,changedTextFiles:A})=>{let W=`Lambda rewrite: ${M}/${V} files; ${G}/${ae} batches completed`;h.progress(W,{phase:"rewrite-text",execution:"lambda",index:M,totalFiles:V,completedBatches:G,totalBatches:ae,changedTextFiles:A}),await Ft(W);}}),await eo(r,k);}else $&&E?z=await pr(r,R.assetMap,{client:E,jobId:`crawl-${Ee(m.runId,40)}`,concurrency:r.remoteWorkers?.rewrite?.concurrency??r.rewriteConcurrency,batchSize:r.remoteWorkers?.rewrite?.batchSize??25,maxAttempts:r.remoteWorkers?.rewrite?.maxAttempts??2,onStageProgress:async({index:P,totalFiles:F,file:M})=>{let V=Date.now();if(P!==1&&P!==F&&P%25!==0&&V-q<5e3)return;q=V;let G=`Staging text files for Lambda rewrite: ${P}/${F}`;h.progress(G,{phase:"rewrite-stage",index:P,totalFiles:F,file:M,execution:"lambda"}),await Ft(G);},onBatchProgress:async({completedItems:P,totalItems:F,completedBatches:M,totalBatches:V,changedTextFiles:G})=>{let ae=`Lambda rewrite: ${P}/${F} files; ${M}/${V} batches completed`;h.progress(ae,{phase:"rewrite-text",execution:"lambda",index:P,totalFiles:F,completedBatches:M,totalBatches:V,changedTextFiles:G}),await Ft(ae);},options:J}):z=await er(r,R.assetMap,J);m.trackManifest&&(Ga(m,x),await Ve(r,m,true)),h.endMark("rewrite-text",{changedTextFiles:z});let pe=i?[...O.deletedFiles].sort():m.enabled&&!o.resumeRewrite?await Ya(r,R,m,O,h,k):[],we=[...new Set((x||[]).map(P=>ge(r.outputDir,C.isAbsolute(P)?P:C.join(r.outputDir,P))).filter(P=>P!==null))].sort();if(i){let P=[...O.changedFiles].sort(),F=new Set(P),M=new Set(i.directRenderUrls.map(D=>Dt(r,D,"page")).filter(D=>!!D&&F.has(D))),V=new Set(i.archiveFamilyUrls.map(D=>Dt(r,D,"page")).filter(D=>!!D&&F.has(D))),G=new Set(i.resourceRenderUrls.map(D=>Dt(r,D,"asset")).filter(D=>!!D&&F.has(D))),ae=new Set([...R.sitemapLeafUrls].map(D=>Dt(r,D,"asset")).filter(D=>!!D&&F.has(D))),A=new Set([...R.sitemapIndexUrls].map(D=>Dt(r,D,"asset")).filter(D=>!!D&&F.has(D))),W=new Set([...M,...V,...G,...ae,...A]),I=P.filter(D=>!W.has(D)),se=[...new Set([...i.directRenderUrls,...i.archiveFamilyUrls,...R.resolvedSitemapRoots,...R.sitemapLeafUrls,...R.sitemapIndexUrls,...i.deleteUrls,...i.resourceRenderUrls,...i.resourceDeleteUrls].map(D=>{let Ue=new URL(D);return `${Ue.pathname}${Ue.search}`}))].sort();await rn(r,{schemaVersion:2,generatedAt:new Date().toISOString(),outputDir:C.resolve(r.outputDir),runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,changedFiles:P,deletedFiles:pe,rewriteTargets:we,revalidateFiles:[...O.revalidateFiles].sort(),consumerId:i.consumerId,fromSequence:i.fromSequence,toSequence:i.toSequence,impactHash:i.impactHash,resolvedSitemapUrls:[...R.resolvedSitemapRoots],uploadGroups:{assets:I,content:[...M].sort(),listings:[...V].sort(),resources:[...G].sort(),sitemapLeaves:[...ae].sort(),sitemapIndexes:[...A].reverse()},invalidationPaths:se});}else await rn(r,{schemaVersion:1,generatedAt:new Date().toISOString(),outputDir:C.resolve(r.outputDir),runMode:o.mode,crawlMode:o.crawlMode,fullSyncRequired:!(m.enabled&&!o.resumeRewrite),changedFiles:m.enabled&&!o.resumeRewrite?[...O.changedFiles].sort():[],deletedFiles:m.enabled&&!o.resumeRewrite?pe:[],rewriteTargets:m.enabled&&!o.resumeRewrite?we:[],revalidateFiles:[...O.revalidateFiles].sort()});await gs(r),h.summary(o.resumeRewrite?`Done. Resumed final rewrite over existing output and changed ${z} text files.`:`Done. Rendered ${R.stats.pagesRendered} pages, processed ${R.doneSitemaps.size} sitemaps and ${R.doneAssets.size} assets.`,{mode:o.mode,crawlMode:o.crawlMode,resumeRewrite:o.resumeRewrite,...R.stats,pagesRendered:R.stats.pagesRendered,donePages:R.stats.pagesCompleted,claimedPages:R.donePages.size,doneSitemaps:R.doneSitemaps.size,doneAssets:R.doneAssets.size,changedTextFiles:z}),await h.flush();}function Zc(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 Se(e,t,r){let n=new AbortController,s=globalThis.setTimeout(()=>{n.abort(new Error(`${e} timed out after ${Zc(t)}.`));},t);try{return await r({abortController:n,httpOptions:{abortSignal:n.signal,requestTimeout:t}})}finally{globalThis.clearTimeout(s);}}function Yc(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Ir(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 el(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function vt(e,t){let r=String(t??e.deploymentTargetOverride??"").trim();if(!Yc(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,awsProfile:s.awsProfile??e.awsProfile,targetOrigin:s.targetOrigin??e.targetOrigin,s3:Xc(e.s3,s.s3),cloudFront:el(e.cloudFront,s.cloudFront)}}}function $n(e){return String(e??"").split(";").map(t=>t.trim().toLowerCase()).filter(Boolean).join(";")}var Bo="wpsuite-sha256",Ho="wpsuite-normalized-sha256",jn="public, max-age=0, must-revalidate",al=/(?:^|[-_:.])(id|uid|nonce|token|hash|instance)(?:$|[-_:.])/i,Fn=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,cl=["SIGINT","SIGTERM"],Er=new Set,Tr=new Map,Wo=null;function ll(e){Er.add(e);let t=false;return ()=>{t||(t=true,Er.delete(e));}}async function ul(){let e=[...Er];e.length!==0&&await Promise.allSettled(e.map(async t=>{Er.delete(t),await U.rm(t,{recursive:true,force:true});}));}function dl(){if(!(Tr.size>0))for(let e of cl){let t=()=>{Wo||(Wo=ul().finally(()=>{for(let[r,n]of Tr)process.off(r,n);Tr.clear(),process.exit(1);}));};Tr.set(e,t),process.on(e,t);}}dl();function pl(e,t){let r=String(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH||"").trim();if(r)return C.resolve(r);let n=t.trim();return n?C.join(C.resolve(n),"crawl-manifest.json"):C.join(C.resolve(e),".crawl-manifest.json")}function et(e){return C.normalize(e).split(C.sep).join("/")}function qo(e,t){let r=C.relative(C.resolve(e),C.resolve(t));return !r||r.startsWith("..")||C.isAbsolute(r)?null:et(r)}async function ml(e,t,r){let n=pl(e.outputDir,t),s;try{s=await U.readFile(n,"utf8");}catch(u){return u.code==="ENOENT"?(r.warn("Deploy rewrite guard disabled because the crawl manifest is missing.",{manifestPath:n,source:"deploy-guard"}),null):(r.warn("Deploy rewrite guard disabled because the crawl manifest could not be read.",{manifestPath:n,error:u instanceof Error?u.message:String(u),source:"deploy-guard"}),null)}let o;try{o=JSON.parse(s);}catch(u){return r.warn("Deploy rewrite guard disabled because the crawl manifest is invalid JSON.",{manifestPath:n,error:u instanceof Error?u.message:String(u),source:"deploy-guard"}),null}let i=new Map,c=new Map,a=0,d=0;for(let[u,l]of Object.entries(o.pages||{})){let g=String(l?.outputPath||"").trim(),p=g?qo(e.outputDir,g):null;if(p){let f=typeof l?.contentType=="string"&&l.contentType?l.contentType:null;f&&c.set(p,f);}!g||l?.rewriteComplete===true||p&&(a++,i.has(p)||i.set(p,{kind:"page",sourceUrl:u,outputPath:C.resolve(g),relativePath:p}));}for(let[u,l]of Object.entries(o.assets||{})){let g=String(l?.outputPath||"").trim(),p=l?.isText===true,f=g?qo(e.outputDir,g):null;if(f){let w=typeof l?.contentType=="string"&&l.contentType?l.contentType:null;w&&c.set(f,w);}!g||!p||l?.rewriteComplete===true||f&&(d++,i.has(f)||i.set(f,{kind:"asset",sourceUrl:u,outputPath:C.resolve(g),relativePath:f}));}return {manifestPath:n,byRelativePath:i,contentTypeByRelativePath:c,pendingPages:a,pendingTextAssets:d}}function gl(e){return e&&(Fn.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 On(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,r)=>r%2===1?t:gl(t)).join("")}function fl(e){return e.split(/([_:.\-/=?&#]+)/g).map((t,r)=>r%2===1?t:Fn.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 $r(e,t){return Array.isArray(e)?e.map(r=>$r(r,t)):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).map(([r,n])=>[r,$r(n,r)])):typeof e!="string"?e:t&&al.test(t)?fl(e):Fn.test(e)?"__uuid__":/^[0-9a-f]{6,}$/i.test(e)||/^[0-9]{8,}$/.test(e)?On(e):e}function hl(e){return e.replace(/((?:https?:\/\/[^"'\s>]+|\/)(?:wp-content|wp-includes)\/[^"'\s>?]+)\?ver=[A-Za-z0-9._-]+(?=$|[#"'])/gi,"$1")}function wl(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($r(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($r(o));return Buffer.from(i,"utf8").toString("base64")}catch{}return t}function yl(e,t){let r=e.toLowerCase();return r==="class"?t.split(/(\s+)/).map((n,s)=>s%2===1?n:On(n)).join(""):r==="id"||r==="for"||r==="aria-controls"||r==="aria-labelledby"||r==="aria-describedby"||r==="aria-owns"||r==="aria-activedescendant"||r==="name"?On(t):r==="href"||r==="src"?hl(t):wl(r,t)}function bl(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=yl(i,a);return ` ${i}=${c}${d}${c}`});return `<${r}${s}>`})}function Ct(e){let t=C.extname(e).toLowerCase();return [".html",".htm",".xml",".xsl",".txt"].includes(t)||["robots.txt","sitemap.xml"].includes(C.basename(e))}function Ar(e,t){return [e.replace(/^\/+|\/+$/g,""),t.replace(/^\/+/,"")].filter(Boolean).join("/")}async function Sl(e,t,r,n){let s=new Map,o,i=0;do{i++;let c=await Se(`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(),u=Number(a.Size??0);s.set(a.Key,{etag:d,size:u,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 Pl(e,t,r,n){if(n.has(r))return n.get(r)??null;try{let s=await Se(`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 Rl(e,t,r,n){if(n.cacheControl!==void 0&&n.contentType!==void 0)return {cacheControl:n.cacheControl,contentType:n.contentType};try{let s=await Se(`S3 head cache metadata ${r}`,3e5,({httpOptions:o})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),o));n.cacheControl=s.CacheControl?.trim()||null,n.contentType=s.ContentType?.trim()||null;}catch{n.cacheControl=null,n.contentType=null;}return {cacheControl:n.cacheControl,contentType:n.contentType}}function _o(e){return String(e||"").split(",").map(t=>t.trim().toLowerCase()).filter(Boolean).sort().join(",")}async function vl(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 Se(`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 Cl(e){return createHash("md5").update(e).digest("hex")}function tt(e){return createHash("sha256").update(e).digest("hex")}function Or(e){return createHash("sha256").update(bl(e.toString("utf8"))).digest("hex")}async function kl(e,t,r,n){if(n.has(r))return n.get(r)??null;try{let i=((await Se(`S3 head object metadata ${r}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),c))).Metadata?.[Bo]||"").trim().toLowerCase()||null;return n.set(r,i),i}catch{return n.set(r,null),null}}async function xl(e,t,r,n,s){if(n.has(r))return n.get(r)??null;try{let i=((await Se(`S3 head object normalized metadata ${r}`,3e5,({httpOptions:c})=>e.send(new HeadObjectCommand({Bucket:t,Key:r}),c))).Metadata?.[Ho]||"").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 Se(`S3 get object ${r}`,3e5,({httpOptions:a})=>e.send(new GetObjectCommand({Bucket:t,Key:r}),a))).Body?.transformToByteArray(),c=i?Or(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 Il(e){return e.replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"")||"profile"}function Tl(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=C.join(e,"asset-map.json"),o;try{o=await U.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 u=String(d??"");return t.urlRewriteMode==="absolute"&&t.targetOrigin&&r.targetOrigin&&t.targetOrigin!==r.targetOrigin&&(u=u.split(t.targetOrigin).join(r.targetOrigin)),u=Tl(u,n),[a,u]}));await U.writeFile(s,JSON.stringify(c,null,2),"utf8");}async function El(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=C.join(C.dirname(e.outputDir),`.deploy-profile-${Il(r)}-${Date.now()}`),u=ll(d);s.info(`Preparing deployment profile "${r}" from existing crawl output to ${d}...`);try{if(await U.rm(d,{recursive:!0,force:!0}),o){await U.mkdir(d,{recursive:!0});for(let w of o){let m=C.join(e.outputDir,w),h=C.join(d,w);await U.mkdir(C.dirname(h),{recursive:!0}),await U.copyFile(m,h);}}else await U.cp(e.outputDir,d,{recursive:!0});let l={...e,sourceOrigin:e.targetOrigin,targetOrigin:t.targetOrigin,extraReplacements:n,outputDir:d},g=1;s.info(`Rewriting text files for deployment profile "${r}"...`);let p={files:o,onStart:({totalFiles:w,workerCount:m})=>{g=m,s.info(`Rewriting ${w} text file(s) for deployment profile "${r}" with ${m} worker(s).`);},onProgress:({index:w,totalFiles:m,changedTextFiles:h,file:y,changed:b})=>{let j=`Rewriting [${w}/${m}] ${y} (${b?"rewrote":"checked"}; ${h} changed so far)`;m<=200||w<=5||w%10===0||w===m?(s.progress(j,{file:y,index:w,totalFiles:m,changed:b,changedTextFiles:h,workerCount:g}),s.info(j)):s.debug(j);}},f=i?await pr(l,{},{...i,options:p}):await er(l,{},p);return await Al(d,e,t,n),s.info(`Prepared deployment profile "${r}" in ${d} (${f} rewritten text file(s)).`),{outputDir:d,cleanup:async()=>{u(),await U.rm(d,{recursive:!0,force:!0});}}}catch(l){throw s.error(`Failed to prepare deployment profile "${r}" temporary output.`,{tempDir:d,outputDir:e.outputDir,error:l instanceof Error?l.message:String(l),...l instanceof Error&&l.stack?{stack:l.stack}:{}}),await U.rm(d,{recursive:true,force:true}).catch(()=>{}),u(),await s.flush().catch(()=>{}),l}}function Vo(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 $l(e,t){if(!e||typeof e!="object")throw new Error("Remote deploy result must be a JSON object.");let r=e;if(r.schemaVersion!==ee||r.response?.operation!=="deploy-copy"||r.response.jobId!==t.jobId||r.response.taskId!==t.taskId||!r.detail||!Array.isArray(r.detail.objects)||!Array.isArray(r.detail.deletedKeys)||!Array.isArray(r.detail.deleteErrors))throw new Error("Remote deploy result has an invalid envelope.");return r}async function Mr(e){let t=null;for(let r=1;r<=Math.max(1,Math.min(5,e.maxAttempts));r+=1){let n=te(`deploy-${e.batchIndex}-a${r}-${Date.now()}`,"taskId");try{let s=await e.client.invoke({schemaVersion:ee,operation:"deploy-copy",jobId:e.jobId,taskId:n,targetId:e.targetId,items:e.items,...e.deleteRelativeKeys?{deleteRelativeKeys:e.deleteRelativeKeys}:{}}),o=$l(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 Ol(e){let t=Vo(e.remoteConfig,e.config),r=te(`deploy-${Date.now()}-${tt(Buffer.from(`${t.id}:${e.config.outputDir}`)).slice(0,12)}`,"jobId"),n=Ye(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],S=await U.readFile(C.join(e.config.outputDir,b)),j=e.contentTypeByRelativePath?.get(b)||Hs.lookup(b)||"application/octet-stream",E=e.revalidateFiles.has(et(b))?jn:Ct(b)?e.config.s3.htmlCacheControl:e.config.s3.assetCacheControl,k=tt(S),v=Ct(b)?Or(S):void 0,T=`${n}/files/${tt(Buffer.from(b))}-${C.basename(b)}`.replace(/\/+/g,"/");await e.client.writeWorkspaceObject({key:T,body:S,contentType:String(j),cacheControl:E,metadata:{"wpsuite-sha256":k,...v?{"wpsuite-normalized-sha256":v}:{}}}),o[y]={sourceKey:T,targetRelativeKey:b,sha256:k,...v?{normalizedSha256:v}:{},bytes:S.byteLength,contentType:String(j),cacheControl:E},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 u=[],l=[],g=[],p=0,f=0,w=0,m=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:m,completedBatches:0,totalBatches:a.length}),await Promise.all(Array.from({length:m},async()=>{for(;;){let y=w;if(y>=a.length)return;w+=1;let b=Date.now(),S=await Mr({client:e.client,jobId:r,targetId:t.id,batchIndex:y+1,items:a[y],maxAttempts:e.maxAttempts}),j=0,E=0,k=0;for(let T of S.detail.objects)p+=1,T.status==="copied"?(u.push(T.targetKey),j+=1):T.status==="skipped"?(l.push(T.targetKey),E+=1):(g.push(T.targetKey),k+=1),e.log.debug(`Lambda deploy ${T.status}: ${T.targetKey}`,{phase:"deploy-copy",batch:y+1,sourceKey:T.sourceKey,targetKey:T.targetKey,status:T.status,...T.error?{error:T.error}:{}});f+=1;let v=Date.now()-b;e.log.info(`Lambda deploy batch ${y+1}/${a.length} completed: ${j} copied, ${E} skipped unchanged, ${k} failed.`,{phase:"deploy-copy",batch:y+1,totalBatches:a.length,items:S.detail.objects.length,copied:j,skipped:E,failed:k,elapsedMs:v,elapsedSeconds:Math.round(v/10)/100,workerExporterVersion:S.response.exporterVersion,...S.requestId?{lambdaRequestId:S.requestId}:{},...S.completedAt?{workerCompletedAt:S.completedAt}:{}}),e.log.progress(`Lambda deploy: ${p}/${e.files.length}`,{phase:"deploy-copy",index:p,totalFiles:e.files.length,uploaded:u.length,skipped:l.length,failed:g.length,mode:"lambda-s3-copy",workerCount:m,completedBatches:f,totalBatches: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 S=await Mr({client:e.client,jobId:r,targetId:t.id,batchIndex:a.length+Math.floor(y/1e3)+1,items:[],deleteRelativeKeys:b,maxAttempts:e.maxAttempts});h.push(...S.detail.deletedKeys),e.log.progress(`Lambda deploy deletions: ${h.length}/${e.deletedFiles.length}`,{phase:"deploy-delete",index:h.length,totalFiles:e.deletedFiles.length,deleted:h.length,mode:"lambda-s3-copy"});}return {uploadedKeys:u,skippedKeys:l,failedKeys:g,deletedKeys:h}}async function Ml(e){let t=Vo(e.remoteConfig,e.config),r=te(`deploy-manifest-${Date.now()}-${tt(Buffer.from(t.id)).slice(0,12)}`,"jobId"),n=e.objects.map(p=>({sourceKey:p.object.key,targetRelativeKey:p.relativePath,sha256:p.object.sha256,bytes:p.object.bytes,contentType:p.contentType||"application/octet-stream",cacheControl:e.revalidateFiles.has(et(p.relativePath))?jn:Ct(p.relativePath)?e.config.s3.htmlCacheControl:e.config.s3.assetCacheControl})),s=[],o=Math.max(1,Math.min(500,e.batchSize));for(let p=0;p<n.length;p+=o)s.push(n.slice(p,p+o));let i=[],c=[],a=[],d=0,u=0,l=Math.max(1,Math.min(Math.max(1,e.concurrency),Math.max(1,s.length)));if(e.log.progress(`Lambda deploy: 0/${n.length}`,{phase:"deploy-copy",index:0,totalFiles:n.length,mode:"lambda-s3-native",workerCount:l}),await Promise.all(Array.from({length:s.length===0?0:l},async()=>{for(;;){let p=d;if(p>=s.length)return;d+=1;let f=await Mr({client:e.client,jobId:r,targetId:t.id,batchIndex:p+1,items:s[p],maxAttempts:e.maxAttempts});for(let w of f.detail.objects)u+=1,w.status==="copied"?i.push(w.targetKey):w.status==="skipped"?c.push(w.targetKey):a.push(w.targetKey);e.log.progress(`Lambda deploy: ${u}/${n.length}`,{phase:"deploy-copy",index:u,totalFiles:n.length,uploaded:i.length,skipped:c.length,failed:a.length,mode:"lambda-s3-native",workerCount:l});}})),a.length>0)throw new Error(`Remote deployment failed for ${a.length} object(s).`);let g=[];for(let p=0;p<e.deletedFiles.length;p+=1e3){let f=e.deletedFiles.slice(p,p+1e3);if(f.length===0)continue;let w=await Mr({client:e.client,jobId:r,targetId:t.id,batchIndex:s.length+Math.floor(p/1e3)+1,items:[],deleteRelativeKeys:f,maxAttempts:e.maxAttempts});g.push(...w.detail.deletedKeys);}return {uploadedKeys:i,skippedKeys:c,failedKeys:a,deletedKeys:g}}async function jl(e,t,r,n,s){let o={...t,jobId:te(`profile-${tt(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],u=Ye(e.config.workspace.prefix,"jobs",o.jobId,"profile",tt(Buffer.from(d.relativePath)),C.basename(d.relativePath));o.objects[d.relativePath]={...d,object:await e.copyWorkspaceObject(d.object,u)};}})),o}async function Ko(e=process.argv.slice(2)){let t=await Ie(),r=Ir(e),n=vt(t,r),s=C.resolve(n.config.logDir||"logs"),o=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",i=o?C.join(C.resolve(o),"deploy-diff.json"):"",c=new ft(s,"deploy.log.jsonl",n.config.logLevel??"info"),a=n.profile?.extraReplacements??{},d=async()=>{};try{let u=await mt(t);if(e.includes("--content-sync")&&u?.schemaVersion!==2)throw new Error("Content-sync deployment requires a valid schema-2 targeted deploy plan.");let g=await ms(t);if(g)throw new Error(`Deployment blocked: the crawl started at ${g.startedAt} did not complete its final rewrite. Run a successful crawl or resume the rewrite before deploying.`);let p=t.remoteWorkers?.rewrite?.enabled===!0,f=t.remoteWorkers?.deploy?.enabled===!0,w=null,m=null;if(p||f){let A=gr({runtimeDir:o});if(!A)throw new Error("Remote Lambda workers are enabled, but the runtime directory is unknown.");w=await ar(A);let W=!1;m=new wt(w,{},{onProgress:I=>{if(I.operation==="rewrite"){c.debug("Lambda rewrite task progress.",{remoteWorkerOperation:I.operation,remoteWorkerTaskId:I.taskId,remoteWorkerStatus:I.status,remoteWorkerCompletedItems:I.completedItems,remoteWorkerFailedItems:I.failedItems,remoteWorkerTotalItems:I.totalItems,remoteWorkerUpdatedAt:I.updatedAt});return}c.progress(`Lambda ${I.operation} batch: ${I.completedItems+I.failedItems}/${I.totalItems}`,{remoteWorkerOperation:I.operation,remoteWorkerTaskId:I.taskId,remoteWorkerStatus:I.status,remoteWorkerCompletedItems:I.completedItems,remoteWorkerFailedItems:I.failedItems,remoteWorkerTotalItems:I.totalItems,remoteWorkerUpdatedAt:I.updatedAt});},onProgressError:I=>{W||(W=!0,c.warn("Lambda progress status could not be read; task execution continues.",{error:I.message}));}});}let h=f&&p&&t.remoteWorkers?.render?.enabled===!0&&m&&w?.functions.asset?await wr(t):null;if(h&&m&&w){if(h.workspace.bucket!==w.workspace.bucket||h.workspace.prefix!==w.workspace.prefix)throw new Error("Remote export manifest workspace does not match remote-workers.json.");let A=n.config,W=A.s3SyncMode??"sdk-upload-delete";if(u?.schemaVersion===2&&u.deletedFiles.length>0&&W!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let I=u?.schemaVersion===2?[...u.uploadGroups.assets,...u.uploadGroups.content,...u.uploadGroups.listings,...u.uploadGroups.resources,...u.uploadGroups.sitemapLeaves,...u.uploadGroups.sitemapIndexes]:u?.changedFiles??[];if(u?.schemaVersion===2){let re=[...new Set(I)],ye=[...new Set(u.changedFiles)];if(re.length!==ye.length||ye.some(Le=>!re.includes(Le)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let D=!!u&&!u.fullSyncRequired?[...new Set(I.map(et))]:Object.keys(h.objects).sort(),Ue=D.filter(re=>!h.objects[re]);if(Ue.length>0)throw new Error(`Remote export manifest is missing ${Ue.length} planned object(s): ${Ue.slice(0,10).join(", ")}`);let Je=h,K=D.map(re=>h.objects[re]);n.name&&(t.targetOrigin!==A.targetOrigin||Object.keys(a).length>0)&&(Je=await jl(m,h,K,n.name||"default",t.concurrency),await mr(A,Je,Je.assetMap,{client:m,jobId:Je.jobId,concurrency:t.concurrency,batchSize:25,maxAttempts:2,onBatchProgress:({completedItems:re,totalItems:ye,completedBatches:Le,totalBatches:Ht,changedTextFiles:Ur})=>{c.progress(`Lambda deployment-profile rewrite: ${re}/${ye} files; ${Le}/${Ht} batches completed`,{phase:"rewrite-text",execution:"lambda",index:re,totalFiles:ye,completedBatches:Le,totalBatches:Ht,changedTextFiles:Ur});}}),K=D.map(re=>Je.objects[re])),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 ce=await Ml({client:m,remoteConfig:w,config:A,objects:K,deletedFiles:W==="sdk-upload-delete"?u?.deletedFiles??[]:[],revalidateFiles:new Set((u?.revalidateFiles??[]).map(et)),concurrency:t.concurrency,batchSize:50,maxAttempts:2,log:c});i&&(await U.mkdir(C.dirname(i),{recursive:!0}),await U.writeFile(i,JSON.stringify({generatedAt:new Date().toISOString(),mode:"lambda-s3-native",summary:{totalFiles:K.length,eligibleFiles:K.length,uploaded:ce.uploadedKeys.length,skipped:ce.skippedKeys.length,rejected:0,failed:ce.failedKeys.length,deleted:ce.deletedKeys.length,protected:0,...n.name?{profile:n.name}:{}},uploadedKeys:ce.uploadedKeys,skippedKeys:ce.skippedKeys,rejectedKeys:[],failedKeys:ce.failedKeys,deletedKeys:ce.deletedKeys,protectedRemoteOnlyKeys:[]},null,2),"utf8")),u?.schemaVersion!==2&&await Ze(t),c.endMark("deploy"),await c.flush();return}let y=await El(t,n.config,n.name,a,c,u?.schemaVersion===2?u.changedFiles:void 0,p&&m?{client:m,jobId:`profile-${Date.now()}`,concurrency:t.remoteWorkers?.rewrite?.concurrency??t.rewriteConcurrency,batchSize:t.remoteWorkers?.rewrite?.batchSize??25,maxAttempts:t.remoteWorkers?.rewrite?.maxAttempts??2,onStageProgress:({index:A,totalFiles:W,file:I})=>{(A===1||A===W||A%25===0)&&c.progress(`Staging deployment-profile rewrite: ${A}/${W}`,{phase:"rewrite-stage",index:A,totalFiles:W,file:I,execution:"lambda"});},onBatchProgress:({completedItems:A,totalItems:W,completedBatches:I,totalBatches:se,changedTextFiles:D})=>{c.progress(`Lambda deployment-profile rewrite: ${A}/${W} files; ${I}/${se} batches completed`,{phase:"rewrite-text",execution:"lambda",index:A,totalFiles:W,completedBatches:I,totalBatches:se,changedTextFiles:D});}}:void 0);d=y.cleanup;let b={...n.config,outputDir:y.outputDir},S=b.s3SyncMode??"sdk-upload-delete";if(u?.schemaVersion===2&&u.deletedFiles.length>0&&S!=="sdk-upload-delete")throw new Error("Content-sync tombstones require the sdk-upload-delete S3 sync mode.");let j=u?.schemaVersion!==2&&y.outputDir!==t.outputDir&&(t.targetOrigin!==n.config.targetOrigin||Object.keys(a).length>0),E=!!u&&!u.fullSyncRequired&&!j;if(u?.schemaVersion===2&&!E)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: ${S}`),u&&u.fullSyncRequired?c.info("Deploy plan requests a full sync; scanning the whole output tree."):u&&j?c.info("Ignoring incremental deploy plan because deployment-profile rewrites require a full sync."):E&&c.info(`Using incremental deploy plan: ${u.changedFiles.length} changed file(s), ${u.deletedFiles.length} deleted file(s).`),c.mark("deploy");let k=dt(b.awsProfile),v=new S3Client({region:b.s3.region,...k?{credentials:k}:{}}),T=y.outputDir===t.outputDir?await ml(t,o,c):null,$=u?.schemaVersion===2?[...u.uploadGroups.assets,...u.uploadGroups.content,...u.uploadGroups.listings,...u.uploadGroups.resources,...u.uploadGroups.sitemapLeaves,...u.uploadGroups.sitemapIndexes]:u?.changedFiles??[];if(u?.schemaVersion===2){let A=[...new Set($)],W=[...new Set(u.changedFiles)];if(A.length!==W.length||W.some(I=>!A.includes(I)))throw new Error("Content-sync deploy-plan upload groups do not cover the exact changed-file set.")}let N=E?(await Promise.all($.map(async A=>{try{return (await U.stat(C.join(b.outputDir,A))).isFile()?A:null}catch{if(u?.schemaVersion===2)throw new Error(`Content-sync planned upload is missing: ${A}`);return null}}))).filter(A=>A!==null):await pt(b.outputDir),B=[],R=[],O=N.filter(A=>{let W=T?.byRelativePath.get(et(A));if(!W)return !0;let I=Ar(b.s3.prefix,A);return B.push(A),R.push(I),c.reject(W.kind,W.sourceUrl||A,"rewrite not completed; deploy upload blocked","deploy-guard",{file:A,key:I,outputPath:W.outputPath,manifestPath:T?.manifestPath}),!1}),de=new Set((u?.revalidateFiles||[]).map(et)),ie=new Set((E?O:N).map(A=>Ar(b.s3.prefix,A)));T&&(T.pendingPages>0||T.pendingTextAssets>0)&&c.info(`Deploy rewrite guard loaded ${T.pendingPages} pending page(s) and ${T.pendingTextAssets} pending text asset(s) from crawl manifest.`,{manifestPath:T.manifestPath,pendingPages:T.pendingPages,pendingTextAssets:T.pendingTextAssets,source:"deploy-guard"}),B.length>0&&c.warn(`Deploy rewrite guard blocked ${B.length} file(s) from upload because final crawl rewrite is incomplete.`,{rejected:B.length,manifestPath:T?.manifestPath,source:"deploy-guard"}),E?c.info(`Using ${O.length} eligible planned changed file(s) from deploy plan for upload${B.length>0?` (${B.length} blocked by deploy rewrite guard)`:""}.`):c.info(`Found ${N.length} file(s) in output directory "${b.outputDir}"${B.length>0?`; ${O.length} remain eligible for upload after deploy rewrite guard filtering`:""}.`),c.progress("Deploy preflight: comparing local output with remote S3 state.",{phase:"deploy",mode:S,totalFiles:O.length,scannedFiles:N.length,rejected:B.length,deploymentProfile:n.name||"",s3Bucket:b.s3.bucket,s3Prefix:b.s3.prefix||"",targetedPlan:E,deployGuardEnabled:y.outputDir===t.outputDir});let H=0,x=0,L=0,q=[],J=[],z=[],pe=[],we=[],P=new Map,F=new Map,M=new Map,V=new Map,G=new Map;if(f){if(!m||!w)throw new Error("Remote deploy worker client was not initialized.");if(S!=="sdk-upload-delete"&&S!=="sdk-upload-only")throw new Error(`Unsupported remote deploy mode: ${S}`);let A=await Ol({client:m,remoteConfig:w,config:b,files:O,deletedFiles:S==="sdk-upload-delete"?u?.deletedFiles??[]:[],revalidateFiles:de,contentTypeByRelativePath:T?.contentTypeByRelativePath,concurrency:b.remoteWorkers?.deploy?.concurrency??b.assetDownloadConcurrency,batchSize:b.remoteWorkers?.deploy?.batchSize??50,maxAttempts:b.remoteWorkers?.deploy?.maxAttempts??2,log:c});q.push(...A.uploadedKeys),J.push(...A.skippedKeys),z.push(...A.failedKeys),pe=A.deletedKeys,H=q.length,x=J.length,L=z.length,c.info(`Lambda deploy complete: ${H} copied, ${x} skipped unchanged, ${pe.length} deleted, ${L} failed.`),i&&(await U.mkdir(C.dirname(i),{recursive:!0}),await U.writeFile(i,JSON.stringify({generatedAt:new Date().toISOString(),mode:"lambda-s3-copy",summary:{totalFiles:N.length,eligibleFiles:O.length,uploaded:H,skipped:x,rejected:R.length,failed:L,deleted:pe.length,protected:0,...n.name?{profile:n.name}:{}},uploadedKeys:q,skippedKeys:J,rejectedKeys:R,failedKeys:z,deletedKeys:pe,protectedRemoteOnlyKeys:[]},null,2),"utf8")),u?.schemaVersion!==2&&await Ze(t),c.endMark("deploy"),await c.flush();return}E||(c.info("Listing existing S3 objects to detect unchanged files..."),P=await Sl(v,b.s3.bucket,b.s3.prefix,c),c.info(`Found ${P.size} existing key(s) in S3.`));let ae=async A=>P.has(A)?P.get(A)??null:E?Pl(v,b.s3.bucket,A,F):null;for(let A of O){let W=C.join(b.outputDir,A),I=Ar(b.s3.prefix,A),se=T?.contentTypeByRelativePath.get(A),D=se&&se!=="application/octet-stream"?se:Hs.lookup(A)||se||"application/octet-stream",Ue=de.has(et(A)),Je=Ue?jn:Ct(A)?b.s3.htmlCacheControl:b.s3.assetCacheControl,K=H+x+L+1;O.length<=200||K<=5||K%10===0||K===O.length?(c.progress(`Uploading [${K}/${O.length}] ${I} (${D})`,{file:A,key:I,index:K,totalFiles:O.length,contentType:D,mode:S}),c.info(`Uploading [${K}/${O.length}] ${I} (${D})`)):c.debug(`Uploading [${K}/${O.length}] ${I} (${D})`);try{let Re=await U.readFile(W),ce=await ae(I),re=null,ye=null;if(ce){let Le=await Rl(v,b.s3.bucket,I,ce),Ht=!Ue||_o(Le.cacheControl)===_o(Je),Ur=$n(Le.contentType)===$n(String(D)),Lr=Ht&&Ur,ei=Re.byteLength;if(ce.size===ei){let Vt=ce.etag.includes("-");if(ce.etag!==""&&!Vt){let ti=Cl(Re);if(ce.etag===ti&&Lr){x++,O.length<=200||x<=5||x%25===0?(c.progress(`Skipped unchanged [${K}/${O.length}] ${I}`,{file:A,key:I,index:K,totalFiles:O.length,uploaded:H,skipped:x,failed:L,mode:S,reason:"etag-md5-match"}),c.info(`Skipped unchanged [${K}/${O.length}] ${I}`)):c.debug(`Skipped unchanged [${K}/${O.length}] ${I}`),J.push(I);continue}}re=tt(Re);let _n=await kl(v,b.s3.bucket,I,M);if(_n&&_n===re&&Lr){x++,O.length<=200||x<=5||x%25===0?(c.progress(`Skipped unchanged [${K}/${O.length}] ${I}`,{file:A,key:I,index:K,totalFiles:O.length,uploaded:H,skipped:x,failed:L,mode:S,reason:"metadata-sha256-match"}),c.info(`Skipped unchanged [${K}/${O.length}] ${I}`)):c.debug(`Skipped unchanged [${K}/${O.length}] ${I}`),J.push(I);continue}}if(Ct(A)){ye=Or(Re);let Vt=await xl(v,b.s3.bucket,I,V,G);if(Vt&&Vt===ye&&Lr){x++,O.length<=200||x<=5||x%25===0?(c.progress(`Skipped semantically unchanged [${K}/${O.length}] ${I}`,{file:A,key:I,index:K,totalFiles:O.length,uploaded:H,skipped:x,failed:L,mode:S,reason:"normalized-html-match"}),c.info(`Skipped semantically unchanged [${K}/${O.length}] ${I}`)):c.debug(`Skipped semantically unchanged [${K}/${O.length}] ${I}`),J.push(I);continue}}}re=re??tt(Re),ye=ye??(Ct(A)?Or(Re):null),await Se(`S3 upload ${I}`,9e5,({abortController:Le})=>new Upload({client:v,abortController:Le,params:{Bucket:b.s3.bucket,Key:I,Body:Re,ContentType:D,CacheControl:Je,Metadata:{[Bo]:re,...ye?{[Ho]:ye}:{}}}}).done()),H++,q.push(I),(H%25===0||H===O.length)&&(c.progress(`Uploaded ${H}/${O.length} file(s)\u2026`,{uploaded:H,skipped:x,failed:L,totalFiles:O.length,mode:S}),c.info(`Uploaded ${H}/${O.length} file(s)\u2026`));}catch(Re){L++,z.push(I),c.error(`Failed to upload ${I}: ${Re.message}`,{key:I,error:String(Re)});}}if(c.info(`Upload complete: ${H} uploaded, ${x} skipped unchanged, ${R.length} rejected by guard, ${L} failed.`),z.length>0||R.length>0)throw new Error(`Deployment stopped before deletion because ${z.length} upload(s) failed and ${R.length} upload(s) were rejected.`);if(S==="sdk-upload-delete"){let A=new Set((u?.deletedFiles??[]).map(I=>Ar(b.s3.prefix,I))),W=[...A].filter(I=>!ie.has(I));if(!E&&(we=[...P.keys()].filter(se=>!ie.has(se)).filter(se=>!A.has(se)),we.length>0)){c.warn(`Protected ${we.length} remote-only S3 object(s) from deletion because the crawl did not provide explicit tombstones.`,{protected:we.length,source:"deploy-delete-guard"});for(let se of we)c.debug(` protected remote-only object: ${se}`);}pe=await vl(v,b.s3.bucket,E?W:W.filter(I=>P.has(I)),c);}else c.info("Sync mode is sdk-upload-only \u2014 skipping stale object deletion.");i&&(await U.mkdir(C.dirname(i),{recursive:!0}),await U.writeFile(i,JSON.stringify({generatedAt:new Date().toISOString(),mode:S,summary:{totalFiles:N.length,eligibleFiles:O.length,uploaded:q.length,skipped:J.length,rejected:R.length,failed:z.length,deleted:pe.length,protected:we.length,...n.name?{profile:n.name}:{}},uploadedKeys:q,skippedKeys:J,rejectedKeys:R,failedKeys:z,deletedKeys:pe,protectedRemoteOnlyKeys:we},null,2),"utf8")),u?.schemaVersion!==2&&await Ze(t),c.endMark("deploy"),await c.flush();}finally{await d().catch(()=>{});}}function Dl(e){let t=e;return t.endsWith("/")?t+="index.html":C.posix.basename(t).includes(".")||(t+="/index.html"),t}function Ul(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 zo(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=Dl(s.pathname);r.add(o),r.add(`${i}${s.search}`);let c=Ul(i,t);c&&r.add(`${c}${s.search}`);}return [...r].sort()}function _l(){let e=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();return e?C.join(C.resolve(e),"content-sync-invalidation.json"):""}async function Dn(e,t){if(!e)return;await U.mkdir(C.dirname(e),{recursive:true});let r=`${e}.${process.pid}.${Date.now()}.tmp`;await U.writeFile(r,JSON.stringify({...t,updatedAt:new Date().toISOString()},null,2),"utf8"),await U.rename(r,e);}async function Nl(e,t,r,n){if(!e)return null;try{let s=JSON.parse(await U.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 Bl(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 Qo(e=process.argv.slice(2)){let t=await Ie(),r=Ir(e),n=vt(t,r),s=n.config,o=new ft(s.logDir||"logs","invalidate.log.jsonl",s.logLevel??"info"),i=e.includes("--content-sync"),c=await mt(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?zo(a,s.s3.prefix||""):a;if(c?.schemaVersion===2&&d.some(w=>w==="/*"))throw new Error("Content-sync cannot use a whole-distribution invalidation.");if(d.length===0){o.info("The targeted deploy plan has no CloudFront paths to invalidate."),await o.flush();return}let u=s.cloudFront.distributionId,l=dt(s.awsProfile),g=new CloudFrontClient({region:"us-east-1",maxAttempts:5,...l?{credentials:l}:{}});if(o.mark("invalidate"),c?.schemaVersion!==2){let w=`smartcloud-static-publisher-${Date.now()}`;await Se(`CloudFront invalidation for distribution ${u}`,3e5,({httpOptions:m})=>g.send(new CreateInvalidationCommand({DistributionId:u,InvalidationBatch:{CallerReference:w,Paths:{Quantity:d.length,Items:d}}}),m)),o.endMark("invalidate"),await o.flush();return}let p=_l(),f=await Nl(p,u,c.impactHash,d);if(!f){let w=createHash("sha256").update(JSON.stringify(d)).digest("hex");f={schemaVersion:1,distributionId:u,impactHash:c.impactHash,updatedAt:new Date().toISOString(),batches:Bl(d).map((m,h)=>({index:h,callerReference:`wpsuite-content-sync-${c.impactHash.slice(0,24)}-${w.slice(0,24)}-${h}`,paths:m,invalidationId:null,status:"pending"}))},await Dn(p,f);}for(let w of f.batches){if(!w.invalidationId){let m=await Se(`CloudFront targeted invalidation batch ${w.index+1}`,3e5,({httpOptions:y})=>g.send(new CreateInvalidationCommand({DistributionId:u,InvalidationBatch:{CallerReference:w.callerReference,Paths:{Quantity:w.paths.length,Items:w.paths}}}),y)),h=String(m.Invalidation?.Id||"").trim();if(!h)throw new Error("CloudFront did not return an invalidation ID.");w.invalidationId=h,w.status="created",await Dn(p,f);}await waitUntilInvalidationCompleted({client:g,maxWaitTime:900,minDelay:2,maxDelay:15},{DistributionId:u,Id:w.invalidationId}),w.status="completed",await Dn(p,f),o.info(`CloudFront invalidation batch ${w.index+1}/${f.batches.length} completed.`,{invalidationId:w.invalidationId,pathCount:w.paths.length});}o.endMark("invalidate"),await o.flush();}function Dr(e){let t=new URL(e);return t.hash="",t.href}function Go(e,t,r){let n=new Set([...t].map(a=>Dr(a))),s=new Map,o=new Map,i=a=>{s.set(Dr(a),a);},c=a=>{if(!a)return;let d=Dr(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(Dr(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 Yo=["claimed","impact-planned","rendered","deployed","invalidated","verified","acknowledged"];function Wn(){let e=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();if(!e)throw new Error("Content-sync requires a runtime directory.");return C.resolve(e)}function kt(e,t){return Yo.indexOf(e)>=Yo.indexOf(t)}async function ze(e,t,r,n,s={}){let o={timestamp:new Date().toISOString(),eventType:t,status:t.endsWith("failed")?"failed":"success",actorSource:"content-sync",jobId:r.id||"",command:"content-sync",message:n,details:s};await U.mkdir(e,{recursive:true}),await U.appendFile(C.join(e,"audit-events.jsonl"),`${JSON.stringify(o)}
|
|
19
|
+
`,"utf8");}function Vl(e){return xt({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 Kl(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 qn(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=vt(e,t.deploymentProfile),s=n.name||"",o=Hn(e.sourceOrigin,String(t.id||""),r),i=Vn({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=Kn({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||"")}:${We({scopeFingerprint:o,targetFingerprint:i}).slice(0,32)}`,d=new zt({sourceOrigin:e.sourceOrigin,runtimeToken:String(e.wpsuite?.runtimeToken||""),ignoreHttpsErrors:e.ignoreHttpsErrors}),u=await d.releaseFingerprint(r.includeSubsites),l=Bn({wordpressFingerprint:u,targetFingerprint:i,sourceOrigin:e.sourceOrigin,sitemapPaths:At(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:l}}async function zl(e){let t=Wn(),r=Jt(t),n=await Ie(),s=(n.scheduler?.rules??[]).filter(d=>d.command==="content-sync"&&d.enabled!==false);if(s.length===0)return 0;let o=await Br(r),i=await Qt(r),c=await Nr(r),a=0;for(let d of s){let u=await qn(n,d),l=c.entries[u.coalesceKey];if(!l||l.releaseJobId!==e||l.scopeFingerprint!==u.scopeFingerprint||l.targetFingerprint!==u.targetFingerprint||l.releaseFingerprint!==u.releaseFingerprint)throw new Error(`The normal release has no matching pre-crawl content-sync cutoff for rule ${String(d.id||"")}.`);let g=We({releaseJobId:e,releaseFingerprint:u.releaseFingerprint,scopeFingerprint:u.scopeFingerprint,targetFingerprint:u.targetFingerprint,sequence:l.sequence}),p=await u.client.establishBaseline({consumerId:u.consumerId,scopeFingerprint:u.scopeFingerprint,baselineId:g,postTypes:u.scope.postTypes,includeSubsites:u.scope.includeSubsites,sequence:l.sequence}),f={schemaVersion:1,ruleId:String(d.id||""),consumerId:u.consumerId,coalesceKey:u.coalesceKey,scopeFingerprint:u.scopeFingerprint,targetFingerprint:u.targetFingerprint,releaseFingerprint:u.releaseFingerprint,baselineId:g,deploymentProfile:u.deploymentProfile,committedSequence:p.committedSequence,releaseJobId:e,verifiedAt:new Date().toISOString()};o.entries[u.coalesceKey]=f;let w=i.rules[u.coalesceKey];i.rules[u.coalesceKey]={ruleId:String(d.id||""),consumerId:u.consumerId,coalesceKey:u.coalesceKey,committedSequence:p.committedSequence,observedHeadSequence:p.headSequence,lag:Math.max(0,p.headSequence-p.committedSequence),lastSuccessfulJobId:w?.lastSuccessfulJobId||e,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:p.headSequence>p.committedSequence,retryAttempt:0,nextRetryAt:null,lastError:null,baselineStatus:"ready",baselineReason:null};let m=await rs(r,u.coalesceKey);await ze(t,"content-sync-baseline-established",{id:e},"A normal release established a verified content-sync baseline.",{ruleId:d.id,consumerId:u.consumerId,committedSequence:p.committedSequence,supersededCurrent:m}),a++;}return await Zn(r,o),await Gt(r,i),await U.rm(r.releaseCutoff,{force:true}),a}async function Jl(e){let t=Wn(),r=Jt(t),n=await Ie(),s=(n.scheduler?.rules??[]).filter(c=>c.command==="content-sync"&&c.enabled!==false);if(s.length===0)return 0;let o=await Nr(r),i=0;for(let c of s){let a=await qn(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 u=await a.client.unboundHead(a.scope.postTypes,a.scope.includeSubsites),l={schemaVersion:1,releaseJobId:e,ruleId:String(c.id||""),coalesceKey:a.coalesceKey,scopeFingerprint:a.scopeFingerprint,targetFingerprint:a.targetFingerprint,releaseFingerprint:a.releaseFingerprint,sequence:u.headSequence,capturedAt:new Date().toISOString()};o.entries[a.coalesceKey]=l,await ze(t,"content-sync-release-cutoff-captured",{id:e},"A normal release captured its pre-crawl content-sync journal cutoff.",{ruleId:c.id,consumerId:a.consumerId,sequence:u.headSequence}),i++;}return await Gn(r,o),i}async function Ql(e,t){let r=JSON.parse(await U.readFile(C.join(e,"current-run.json"),"utf8"));if(r.id!==t||r.command!=="content-sync"||!String(r.ruleId||"").trim())throw new Error("The active queue job is not the requested content-sync job.");return r}async function Ln(e,t,r=0){let n=e.protocol==="https:"?Qn:li,s=e.protocol==="https:"?new Qn.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,u=a.headers.location;if(d>=300&&d<400&&u&&r<5){a.resume(),Ln(new URL(u,e),t,r+1).then(o,i);return}let l=[],g=0;a.on("data",p=>{g+=p.byteLength,g<=1048576&&l.push(p);}),a.on("error",i),a.on("end",()=>o({status:d,body:Buffer.concat(l).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 Gl(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 U.readFile(n,"utf8")),i=new Set;for(let[u,l]of Object.entries(o.pages??{}))i.add(u),typeof l.url=="string"&&i.add(l.url);let c=await mt(e);if(c?.schemaVersion!==2||c.impactHash!==r.impactHash)throw new Error("Content-sync verification requires the matching targeted deploy plan.");let a=Go(r,i,c.resolvedSitemapUrls),d=new Set(r.resourceRenderUrls.map(u=>{let l=new URL(u);return l.hash="",l.href}));for(let u of a.expectedAvailable){let l=new URL(u),g=new URL(`${l.pathname}${l.search}`,s),p=await Ln(g,e.ignoreHttpsErrors);if(p.status<200||p.status>=400)throw new Error(`Content-sync verification failed (${p.status}): ${g}`);let f=new URL(u);if(f.hash="",d.has(f.href)){if(!p.contentType.toLowerCase().startsWith("application/json"))throw new Error(`Resource verification expected application/json: ${g}`);try{JSON.parse(p.body);}catch{throw new Error(`Resource verification received invalid JSON: ${g}`)}}}for(let u of a.expectedMissing){let l=new URL(u),g=new URL(`${l.pathname}${l.search}`,s),p=await Ln(g,e.ignoreHttpsErrors);if(p.status>=200&&p.status<300)throw new Error(`Deleted content is still publicly available: ${g}`)}}async function Zl(e){let t=await U.readFile(e.candidateManifest,"utf8");JSON.parse(t);let r=`${e.trustedManifest}.${process.pid}.tmp`;await U.writeFile(r,t,"utf8"),await U.rename(r,e.trustedManifest);}async function Yl(e,t){try{let r=JSON.parse(await U.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 U.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 at(e,t,r,n,s={},o=0,i=async()=>{}){t.phase=r,await Yn(e,t),await ts(e,{schemaVersion:1,jobId:t.jobId,phase:r,completedItems:n,totalItems:n,attempt:o,updatedAt:new Date().toISOString(),details:s}),await ze(C.dirname(e.state),"content-sync-checkpoint",{id:t.jobId},`Content-sync checkpoint completed: ${r}.`,{phase:r,completedItems:n,totalItems:n,attempt:o,...s}),await i(r,t);}async function Xl(e,t={}){let r={loadConfig:Ie,resolveRuleContext:qn,runCrawl:Lo,runDeploy:Ko,runInvalidate:Qo,verifyDeployment:Gl,promoteCandidateManifest:Zl,clearDeployPlan:Ze,afterCheckpoint:async()=>{},...t},n=Wn(),s=Jt(n),o=await Ql(n,e),i=await r.loadConfig(),c=Kl(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 u=(await Br(s)).entries[a.coalesceKey];if(!u||u.scopeFingerprint!==a.scopeFingerprint||u.targetFingerprint!==a.targetFingerprint||u.releaseFingerprint!==a.releaseFingerprint)throw await ze(n,"content-sync-baseline-rejected",o,"Content-sync requires a new verified full or incremental release baseline.",{ruleId:c.id,reason:u?"mismatch":"missing"}),new Error("Content-sync baseline is missing or stale. Run a successful normal publish first.");let l=await Hr(s),g=await a.client.head({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:u.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites});if(l&&l.jobId!==o.id)throw new Error("Another immutable content-sync range is still active.");if(l||(l={schemaVersion:1,jobId:o.id,ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,scopeFingerprint:a.scopeFingerprint,targetFingerprint:a.targetFingerprint,baselineId:u.baselineId,fromSequence:g.committedSequence,toSequence:g.headSequence,phase:"claimed",impactHash:null,startedAt:new Date().toISOString(),updatedAt:new Date().toISOString()},await at(s,l,"claimed",0,{fromSequence:l.fromSequence,toSequence:l.toSequence},0,r.afterCheckpoint),await ze(n,"content-sync-range-claimed",o,"Content-sync claimed a closed journal range.",{fromSequence:l.fromSequence,toSequence:l.toSequence})),l.fromSequence===l.toSequence){let h=await Qt(s);h.rules[a.coalesceKey]={ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,committedSequence:l.toSequence,observedHeadSequence:g.headSequence,lag:Math.max(0,g.headSequence-l.toSequence),lastSuccessfulJobId:o.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:false,retryAttempt:0,nextRetryAt:null,lastError:null},await Gt(s,h),await Vr(s);return}let p=await Xn(s);if(!kt(l.phase,"impact-planned")){let h=await a.client.events({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:u.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites,afterSequence:l.fromSequence,throughSequence:l.toSequence}),y=_r({events:h,scope:a.scope,sourceOrigin:i.sourceOrigin}),b=await a.client.resolveImpactFamilies(y,a.scope.includeSubsites);p=Jn({jobId:o.id,ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,scopeFingerprint:a.scopeFingerprint,targetFingerprint:a.targetFingerprint,baselineId:u.baselineId,sourceOrigin:i.sourceOrigin,scope:a.scope,fromSequence:l.fromSequence,toSequence:l.toSequence,events:h,resolvedArchiveFamilies:b,sitemapUrls:At(i.sitemapPaths)}),await es(s,p),l.impactHash=p.impactHash,await at(s,l,"impact-planned",h.length,{eventCount:h.length,foldedCount:p.foldedCount,impactHash:p.impactHash},0,r.afterCheckpoint),await ze(n,"content-sync-impact-planned",o,"Content-sync persisted an immutable impact plan.",{impactHash:p.impactHash,eventCount:h.length});}if(!p||p.jobId!==o.id||l.impactHash!==p.impactHash)throw new Error("The immutable content-sync impact plan is missing or mismatched.");if(process.env.STATIC_PUBLISHER_CRAWL_MANIFEST_PATH=s.candidateManifest,!kt(l.phase,"rendered")){let h=await Yl(s,o.id);await ze(n,h==="resumed"?"content-sync-render-resumed":"content-sync-render-initialized",o,h==="resumed"?"Content-sync resumed its candidate crawl manifest.":"Content-sync initialized a candidate crawl manifest.",{impactHash:p.impactHash}),await r.runCrawl(["--content-sync-plan",s.impact]),await at(s,l,"rendered",p.directRenderUrls.length+p.archiveFamilyUrls.length+p.resourceRenderUrls.length,{impactHash:p.impactHash},o.attempt??0,r.afterCheckpoint);}let f=a.deploymentProfile?["--profile",a.deploymentProfile]:[];if(kt(l.phase,"deployed")||(await r.runDeploy([...f,"--content-sync"]),await at(s,l,"deployed",1,{impactHash:p.impactHash},0,r.afterCheckpoint),await ze(n,"content-sync-deployed",o,"Content-sync targeted deployment completed.",{impactHash:p.impactHash})),kt(l.phase,"invalidated")?kt(l.phase,"verified")||await r.runInvalidate([...f,"--content-sync"]):(await r.runInvalidate([...f,"--content-sync"]),await at(s,l,"invalidated",1,{impactHash:p.impactHash},0,r.afterCheckpoint)),kt(l.phase,"verified")||(await r.verifyDeployment(i,a,p,s.candidateManifest),await r.promoteCandidateManifest(s),await at(s,l,"verified",1,{impactHash:p.impactHash},0,r.afterCheckpoint)),g=await a.client.head({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:u.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites}),g.committedSequence===l.fromSequence)await a.client.acknowledge({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:u.baselineId,expectedSequence:l.fromSequence,sequence:l.toSequence,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites});else if(g.committedSequence!==l.toSequence)throw new Error("Content-sync acknowledgement cursor is in an unexpected state.");await at(s,l,"acknowledged",1,{sequence:l.toSequence},0,r.afterCheckpoint),await ze(n,"content-sync-cursor-acknowledged",o,"Content-sync committed its journal cursor after verification.",{sequence:l.toSequence});let w=await a.client.head({consumerId:a.consumerId,scopeFingerprint:a.scopeFingerprint,baselineId:u.baselineId,postTypes:a.scope.postTypes,includeSubsites:a.scope.includeSubsites}),m=await Qt(s);m.rules[a.coalesceKey]={ruleId:String(c.id||""),consumerId:a.consumerId,coalesceKey:a.coalesceKey,committedSequence:l.toSequence,observedHeadSequence:w.headSequence,lag:Math.max(0,w.headSequence-l.toSequence),lastSuccessfulJobId:o.id,lastSuccessfulAt:new Date().toISOString(),trailingWorkDetected:w.headSequence>l.toSequence,retryAttempt:0,nextRetryAt:null,lastError:null},await Gt(s,m),await r.clearDeployPlan(i),await U.rm(C.join(n,"content-sync-invalidation.json"),{force:true}),await U.rm(s.candidateManifest,{force:true}),await Vr(s);}async function Xo(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 Jl(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 zl(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 Xl(o);}Xo().catch(e=>{console.error(e instanceof Error?e.message:String(e)),process.exit(1);});
|