@smart-cloud/publisher-exporter 1.1.44 → 1.1.45

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/dist/deploy.js CHANGED
@@ -1,4 +1,4 @@
1
- import D from'fs/promises';import wt from'http';import bt from'https';import b from'path';import {S3Client,HeadObjectCommand,GetObjectCommand,DeleteObjectsCommand,ListObjectsV2Command}from'@aws-sdk/client-s3';import {Upload}from'@aws-sdk/lib-storage';import Kt,{createHash}from'crypto';import Qt from'fast-glob';import Rr from'mime-types';import {availableParallelism}from'os';import {Worker}from'worker_threads';function Me(t){let e=t.trim();return e==="."?".":e.replace(/\/$/,"")}function Pt(t){let e=String(t??"").trim();if(!e)return "";let r=e;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 i=r.endsWith("/"),n=r.replace(/^\/+/,"").split("/").map(o=>o.replace(/[^A-Za-z0-9._-]/g,"")).filter(o=>o.length>0&&o!=="."&&o!=="..");if(n.length===0)return "";let s=`/${n.join("/")}`;return i&&b.extname(s)===""&&(s+="/"),s==="/"?"":s}function me(t){return !t||typeof t!="object"?{}:Object.fromEntries(Object.entries(t).map(([e,r])=>[e.trim(),String(r??"")]).filter(([e])=>e.length>0))}function St(t){if(!t||typeof t!="object")return {};let e={};for(let[r,i]of Object.entries(t)){let n=r.trim();if(!n||!i||typeof i!="object")continue;let s=i,o={};if(typeof s.targetOrigin=="string"){let a=Me(s.targetOrigin);a&&(o.targetOrigin=a);}let l=me(s.extraReplacements);if(Object.keys(l).length>0&&(o.extraReplacements=l),s.s3&&typeof s.s3=="object"){let a={},c=s.s3;typeof c.bucket=="string"&&(a.bucket=c.bucket.trim()),typeof c.prefix=="string"&&(a.prefix=c.prefix.trim()),typeof c.region=="string"&&(a.region=c.region.trim()),typeof c.htmlCacheControl=="string"&&(a.htmlCacheControl=c.htmlCacheControl.trim()),typeof c.assetCacheControl=="string"&&(a.assetCacheControl=c.assetCacheControl.trim()),Object.keys(a).length>0&&(o.s3=a);}if(s.cloudFront&&typeof s.cloudFront=="object"){let a={},c=s.cloudFront;typeof c.distributionId=="string"&&(a.distributionId=c.distributionId.trim()),Array.isArray(c.invalidationPaths)&&(a.invalidationPaths=c.invalidationPaths.map(u=>String(u??"").trim()).filter(u=>u.length>0)),Object.keys(a).length>0&&(o.cloudFront=a);}e[n]=o;}return e}function ye(t){return t==="PROFESSIONAL"||t==="AGENCY"?t:void 0}function vt(t,e){if(!t||typeof t!="object")return null;let r=t,i=r.command;if(i!=="publish"&&i!=="crawl"&&i!=="deploy"&&i!=="invalidate"&&i!=="retry-timeouts"&&i!=="url"&&i!=="content-sync")return null;let n=Number.parseInt(String(r.intervalMinutes??"0"),10);if(!Number.isFinite(n)||n<1)return null;let s=String(r.id??`${i}-${e+1}`).trim();if(!s)return null;let o=String(r.deploymentProfile??"").trim(),l=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))]:[],c=Array.isArray(r.listingPaths)?[...new Set(r.listingPaths.map(u=>String(u??"").trim()).filter(Boolean))]:[];return i==="content-sync"&&a.length===0?null:{id:s,enabled:r.enabled!==false,command:i,intervalMinutes:n,...i==="publish"||i==="crawl"?{crawlMode:r.crawlMode==="incremental"?"incremental":"full"}:{},...(i==="publish"||i==="deploy"||i==="invalidate"||i==="content-sync")&&o?{deploymentProfile:o}:{},...l?{url:l}:{},...a.length>0?{postTypes:a}:{},...c.length>0?{listingPaths:c}:{},...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 Ie(t){let e=t&&typeof t=="object"?t:{},r=Array.isArray(e.rules)?e.rules.map((i,n)=>vt(i,n)).filter(i=>!!i):[];return {enabled:e.enabled===true,timezone:typeof e.timezone=="string"&&e.timezone.trim()!==""?e.timezone.trim():"UTC",rules:r}}function Rt(t){if(!t||typeof t!="object")return;let e=t,r={};for(let n of ["accountId","siteId"]){let s=e[n];typeof s=="string"&&s.trim()!==""&&(r[n]=s.trim());}let i=Number(e.lastUpdate??0);return Number.isFinite(i)&&i>0&&(r.lastUpdate=Math.floor(i)),e.subscriber===true&&(r.subscriber=true),Object.keys(r).length>0?r:void 0}function we(t){let e=t&&typeof t=="object"?t:{},r=String(e.accountId??"").trim(),i=String(e.siteId??"").trim(),n=e.subscriber===true,s=Rt(e.siteSettings),o={...s??{},...r?{accountId:s?.accountId??r}:{},...i?{siteId:s?.siteId??i}:{},...s?.subscriber===true||n?{subscriber:true}:{}},l={},a=String(e.apiBase??"").trim();a&&(l.apiBase=a);let c=String(e.runtimeToken??e.nonce??"").trim();c&&(l.runtimeToken=c);let u=String(e.virtualAssetBaseUrl??e.uploadUrl??"").trim();u&&(l.virtualAssetBaseUrl=u),Object.keys(o).length>0&&(l.siteSettings=o);let m=ye(e.subscriptionType);return m&&(l.subscriptionType=m),l}function Dt(t){try{let e=new URL(t);globalThis.location=e;}catch{}}function Le(t){return typeof t=="string"?t:t instanceof URL?t.toString():t.url}function xt(t,e){let r=new Headers(t instanceof Request?t.headers:void 0);if(e?.headers)for(let[i,n]of new Headers(e.headers).entries())r.set(i,n);return r}async function Oe(t,e,r=5){let i=new URL(Le(t)),n=String(e?.method??(t instanceof Request?t.method:"GET")).toUpperCase(),s=i.protocol==="http:"?wt:bt,o=xt(t,e);return await new Promise((l,a)=>{let c=s.request(i,{method:n,headers:Object.fromEntries(o.entries()),...i.protocol==="https:"?{rejectUnauthorized:false}:{}},u=>{let m=[];u.on("data",p=>{m.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),u.on("error",a),u.on("end",()=>{let p=u.statusCode??0,d=u.headers.location;if(r>0&&d&&[301,302,303,307,308].includes(p)){l(Oe(new URL(d,i),{method:p===303?"GET":n},r-1));return}let g=new Headers;for(let[w,T]of Object.entries(u.headers))Array.isArray(T)?g.set(w,T.join(", ")):typeof T=="string"&&g.set(w,T);l(new Response(Buffer.concat(m),{status:p,statusText:u.statusMessage??"",headers:g}));});});c.on("error",a),c.end();})}function $t(t){let e=we(t.wpsuite),r={...e.siteSettings??{},...e.siteSettings?.subscriber===true||e.subscriptionType==="PROFESSIONAL"||e.subscriptionType==="AGENCY"?{subscriber:true}:{}},i=String(r.accountId??"").trim(),n=String(r.siteId??"").trim(),s=String(e.virtualAssetBaseUrl??e.uploadUrl??"").trim();if(t.wpsuite={...e.apiBase?{apiBase:e.apiBase}:{},...e.runtimeToken?{runtimeToken:e.runtimeToken}:{},...s?{virtualAssetBaseUrl:s}:{},...Object.keys(r).length>0?{siteSettings:r}:{},...e.subscriptionType?{subscriptionType:e.subscriptionType}:{}},!i||!n||!s)return false;Dt(t.sourceOrigin);let o=globalThis,a={...o.WpSuite??{},siteSettings:r,uploadUrl:s};return e.apiBase&&(a.apiBase=e.apiBase),o.WpSuite=a,true}async function Tt(t){let e=String(t.wpsuite?.virtualAssetBaseUrl??t.wpsuite?.uploadUrl??"").trim();if(!$t(t)){let n=t.wpsuite?.siteSettings,s=[String(n?.accountId??"").trim()?"":"accountId",String(n?.siteId??"").trim()?"":"siteId",e?"":"virtualAssetBaseUrl"].filter(Boolean);return {remote:null,diagnostic:{status:"invalid-runtime",virtualAssetBaseUrl:e,subscriptionType:null,error:`Missing WP Suite runtime field(s): ${s.join(", ")}.`,requests:[]}}}let r=globalThis.fetch,i=[];try{typeof r=="function"&&/^https?:\/\//i.test(e)&&(globalThis.fetch=(async(a,c)=>{let u=Le(a);if(u.startsWith(e)){let m=(()=>{try{return new URL(u).pathname}catch{return u}})();try{let p=t.ignoreHttpsErrors&&u.startsWith("https://")?await Oe(a,c):await r(a,c);return i.push({path:m,status:p.status,ok:p.ok,error:null}),p}catch(p){throw i.push({path:m,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return r(a,c)}));let{getConfig:n}=await import('@smart-cloud/wpsuite-core'),s=await n("publisher"),o=s&&typeof s=="object"?s:null,l=ye(o?.subscriptionType);return {remote:o,diagnostic:{status:o?"loaded":"unavailable",virtualAssetBaseUrl:e,subscriptionType:l??null,error:o?null:"WP Suite publisher config loader returned no configuration.",requests:i}}}catch(n){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:e,subscriptionType:null,error:n instanceof Error?n.message:String(n),requests:i}}}finally{typeof r=="function"&&(globalThis.fetch=r);}}function Et(t,e){let r=St(e?.deploymentProfiles),i=String(e?.defaultDeploymentProfile??"").trim(),n=r[i]?i:"",s=ye(e?.subscriptionType),o=we(t.wpsuite);return {...t,scheduler:Ie(e?.scheduler),deploymentProfiles:r,defaultDeploymentProfile:n,...s?{subscriptionType:s}:{},wpsuite:{...o,...o.siteSettings||s?{siteSettings:{...o.siteSettings??{},...o.siteSettings?.subscriber===true||s==="PROFESSIONAL"||s==="AGENCY"?{subscriber:true}:{}}}:{},...s?{subscriptionType:s}:{}}}}function he(t,e){let i=String(t||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!i)return e;let n=i.split("/").map(s=>s.trim()).filter(s=>s.length>0&&s!=="."&&s!=="..");return n.length>0?n.join("/"):e}function Ct(){let t=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return t.trim()?b.resolve(t):""}function Ae(t,e,r){let i=String(e||"").trim();return i&&b.isAbsolute(i)?b.resolve(i):b.resolve(t,he(i,r))}async function ne(t,e={}){let r=String("").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",i=await D.readFile(r,"utf8"),n=JSON.parse(i);n.sourceOrigin=n.sourceOrigin.replace(/\/$/,""),n.targetOrigin=Me(n.targetOrigin),n.ignoreHttpsErrors??=false,n.outputDir=String(n.outputDir||"export").trim()||"export",n.urlRewriteMode||=n.targetOrigin==="."?"relative":"absolute",n.noJavaScriptRenderPathPrefixes||=[],n.seedPaths||=[],n.generated404RequestPath=Pt(n.generated404RequestPath),n.sitemapPaths||=["/sitemap_index.xml","/sitemap.xml"],n.allowedAssetHosts||=[],n.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],n.blockedPathPrefixes||=[],n.blockedSearchFragments||=[],n.concurrency||=1,n.maxPages||=0,n.extraReplacements=me(n.extraReplacements),n.postCrawlCopyMap=me(n.postCrawlCopyMap),n.logDir=String(n.logDir||"logs").trim()||"logs",n.verbose??=false,n.logLevel||=n.verbose?"debug":"info",n.s3SyncMode||="sdk-upload-delete",n.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},n.readiness.timeoutMs??=1500,n.readiness.fallbackWaitMs??=1500,n.viewport||={width:1440,height:1200},n.navigationTimeoutMs||=3e4,n.assetDownloadConcurrency=Number(n.assetDownloadConcurrency)>0?Number(n.assetDownloadConcurrency):n.concurrency,n.rewriteConcurrency=Number(n.rewriteConcurrency)>0?Number(n.rewriteConcurrency):n.assetDownloadConcurrency,n.wpsuite=we(n.wpsuite),n.scheduler=Ie(void 0),n.deploymentProfiles={},n.defaultDeploymentProfile="",n.deploymentTargetOverride=String(n.deploymentTargetOverride??"").trim();let s=await Tt(n);e.onRemotePublisherConfigDiagnostic?.(s.diagnostic);let o=Et(n,s.remote),l=Ct(),a=l?b.resolve(l,".."):"";return a?(o.outputDir=Ae(a,o.outputDir,"export"),o.logDir=Ae(a,o.logDir,"logs")):(b.isAbsolute(o.outputDir)||(o.outputDir=he(o.outputDir,"export")),b.isAbsolute(o.logDir)||(o.logDir=he(o.logDir,"logs"))),o}function kt(t){let e=t/6e4;if(Number.isInteger(e)&&e>=1)return `${e} minute${e===1?"":"s"}`;let r=Math.ceil(t/1e3);return `${r} second${r===1?"":"s"}`}async function O(t,e,r){let i=new AbortController,n=globalThis.setTimeout(()=>{i.abort(new Error(`${t} timed out after ${kt(e)}.`));},e);try{return await r({abortController:i,httpOptions:{abortSignal:i.signal,requestTimeout:e}})}finally{globalThis.clearTimeout(n);}}var At=".deploy-plan.json",Mt="deploy-plan.json",It=".crawl-incomplete.json",Lt="crawl-incomplete.json";function je(){let t=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return t.trim()?b.resolve(t):""}function Ot(t){let e=String(t||"").replace(/\\/g,"/").replace(/^\/+/,"").trim(),r=b.posix.normalize(e);return !e||!r||r==="."||/^[a-zA-Z]:/.test(r)||r.startsWith("../")||r===".."?null:r}function jt(t){let e=String(t||"").trim();if(!e.startsWith("/")||e.startsWith("//")||e.includes("*")||e.includes("#"))return null;try{let r=new URL(e,"https://content-sync.invalid");return r.origin!=="https://content-sync.invalid"?null:`${b.posix.normalize(r.pathname)}${r.search}`}catch{return null}}function j(t){return [...new Set(t.map(Ot).filter(e=>e!==null))]}function Ft(t,e){let r={generatedAt:String(e.generatedAt||new Date().toISOString()),outputDir:b.resolve(t.outputDir),runMode:e.runMode,crawlMode:e.crawlMode,fullSyncRequired:!!e.fullSyncRequired,changedFiles:j(e.changedFiles||[]),deletedFiles:j(e.deletedFiles||[]),rewriteTargets:j(e.rewriteTargets||[])};return e.schemaVersion===2?{...r,schemaVersion:2,runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,consumerId:String(e.consumerId||"").trim(),fromSequence:Math.max(0,Number(e.fromSequence)||0),toSequence:Math.max(0,Number(e.toSequence)||0),impactHash:String(e.impactHash||"").trim(),uploadGroups:{assets:j(e.uploadGroups?.assets||[]),content:j(e.uploadGroups?.content||[]),listings:j(e.uploadGroups?.listings||[]),sitemapLeaves:j(e.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:j(e.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((e.invalidationPaths||[]).map(jt).filter(i=>i!==null))].sort()}:{schemaVersion:1,...r,runMode:e.runMode}}function _t(t){if(!t||typeof t!="object")return false;let e=t;if(e.schemaVersion===2){let r=e;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.changedFiles)&&Array.isArray(r.deletedFiles)&&Array.isArray(r.rewriteTargets)&&!!r.uploadGroups&&Array.isArray(r.uploadGroups.assets)&&Array.isArray(r.uploadGroups.content)&&Array.isArray(r.uploadGroups.listings)&&Array.isArray(r.uploadGroups.sitemapLeaves)&&Array.isArray(r.uploadGroups.sitemapIndexes)&&Array.isArray(r.invalidationPaths)}return e.schemaVersion===1&&typeof e.generatedAt=="string"&&typeof e.outputDir=="string"&&(e.runMode==="full"||e.runMode==="retry-timeouts"||e.runMode==="single-url")&&(e.crawlMode==="full"||e.crawlMode==="incremental")&&typeof e.fullSyncRequired=="boolean"&&Array.isArray(e.changedFiles)&&Array.isArray(e.deletedFiles)&&Array.isArray(e.rewriteTargets)}function Fe(t){let e=b.join(b.resolve(t.outputDir),At),r=je();return r?[b.join(r,Mt),e]:[e]}function Ut(t){let e=b.join(b.resolve(t.outputDir),It),r=je();return r?[b.join(r,Lt),e]:[e]}async function _e(t){let e=b.resolve(t.outputDir);for(let r of Fe(t))try{let i=await D.readFile(r,"utf8"),n=JSON.parse(i);if(!_t(n)||b.resolve(n.outputDir)!==e)continue;return Ft(t,n)}catch{}return null}async function Ue(t){for(let e of Fe(t))try{await D.unlink(e);}catch(r){if(r.code!=="ENOENT")throw r}}async function Ne(t){let e=b.resolve(t.outputDir);for(let r of Ut(t)){let i;try{i=await D.readFile(r,"utf8");}catch(s){if(s.code==="ENOENT")continue;throw s}let n;try{n=JSON.parse(i);}catch{throw new Error(`Invalid incomplete crawl marker: ${r}`)}if(n.schemaVersion!==1||typeof n.startedAt!="string"||typeof n.outputDir!="string"||b.resolve(n.outputDir)!==e)throw new Error(`Invalid incomplete crawl marker: ${r}`);return {schemaVersion:1,startedAt:n.startedAt,outputDir:e}}return null}function Nt(t){return typeof t.subscriptionType=="string"&&t.subscriptionType.trim()!==""}function ze(t){let e="";for(let i=0;i<t.length;i++){let n=t[i];if(n==="--profile"){let s=(t[i+1]||"").trim();if(!s)throw new Error("Missing value for --profile.");e=s,i+=1;}else if(n.startsWith("--profile=")){let s=n.slice(10).trim();if(!s)throw new Error("Missing value for --profile.");e=s;}}let r=String(process.env.PUBLISHER_DEPLOY_PROFILE||process.env.PUBLISHER_DEPLOYMENT_PROFILE||"").trim();return e||r||null}function zt(t,e){return {...t,...e??{}}}function Wt(t,e){return {...t,...e??{},invalidationPaths:[...(e?.invalidationPaths??t.invalidationPaths)||[]]}}function We(t,e){let r=String(e??t.deploymentTargetOverride??"").trim();if(!Nt(t)){if(r)throw new Error(`Deployment target "${r}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:t}}let i=String(r||t.defaultDeploymentProfile||"").trim();if(!i)return {name:null,profile:null,config:t};let n=t.deploymentProfiles?.[i];if(!n)throw new Error(`Unknown deployment profile "${i}". Check the linked WPSuite publisher configuration.`);return {name:i,profile:n,config:{...t,targetOrigin:n.targetOrigin??t.targetOrigin,s3:zt(t.s3,n.s3),cloudFront:Wt(t.cloudFront,n.cloudFront)}}}var qe={error:0,warn:1,info:2,debug:3},qt=/^(?:authorization|proxyAuthorization|runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken|xStaticPublisherToken|xApiKey)$/i;function ee(t){return t.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 se(t){return typeof t=="string"?ee(t):Array.isArray(t)?t.map(e=>se(e)):t instanceof Error?{name:t.name,message:ee(t.message),...t.stack?{stack:ee(t.stack)}:{}}:t&&typeof t=="object"?Object.fromEntries(Object.entries(t).map(([e,r])=>[e,qt.test(e)?"[REDACTED]":se(r)])):t}function Ke(t){return t?se(t):void 0}var Be=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",ie=Be?b.join(Be,"current-progress.json"):"",Ge=Promise.resolve();function He(t){return t.includes("crawl")?"crawl":t.includes("deploy")?"deploy":t.includes("invalidate")?"invalidate":t}function Bt(t,e){let r={...t},i=new Set(["pagesQueued","pagesRendered","assetsQueued","sitemapsQueued","pagesDiscovered","assetsDiscovered","sitemapsDiscovered","pagesSaved","assetsSaved","donePages","doneAssets","doneSitemaps","changedTextFiles","uploaded","failed","index","totalFiles","pageQueue","assetQueue","sitemapQueue"]);for(let[n,s]of Object.entries(e)){let o=r[n];if(typeof o=="number"&&typeof s=="number"&&i.has(n)){r[n]=Math.max(o,s);continue}r[n]=s;}return r}function Gt(t,e,r){ie&&(Ge=Ge.then(async()=>{let i=new Date,n=i.toISOString();await D.mkdir(b.dirname(ie),{recursive:true});let s=await D.readFile(ie,"utf8").then(S=>JSON.parse(S)).catch(()=>null),o=s&&typeof s.details=="object"&&s.details?s.details:{},l=He(t),a=s?.currentStep||He(s?.source||""),c=s?.startedAt||n,u={...s?.stepDurationsSec??{}},m=s?.stepStartedAt||n;if(a&&a!==l&&s?.stepStartedAt){let S=Math.max(0,Math.round((i.getTime()-new Date(s.stepStartedAt).getTime())/1e3));u[a]=(u[a]??0)+S,m=n;}let p=Math.max(0,Math.round((i.getTime()-new Date(m).getTime())/1e3)),d=Math.max(0,Math.round((i.getTime()-new Date(c).getTime())/1e3)),g=ee(e),w=Ke(r)??{},T={checkedAt:n,source:t,message:g,details:Bt(o,w),startedAt:c,currentStep:l,stepStartedAt:m,stepElapsedSec:p,totalElapsedSec:d,stepDurationsSec:u};await D.writeFile(ie,JSON.stringify(T,null,2),"utf8");}).catch(()=>{}));}async function Ht(t,e){await D.mkdir(b.dirname(t),{recursive:true}),await D.appendFile(t,`${JSON.stringify(se(e))}
1
+ import D from'fs/promises';import wt from'http';import bt from'https';import b from'path';import {S3Client,HeadObjectCommand,GetObjectCommand,DeleteObjectsCommand,ListObjectsV2Command}from'@aws-sdk/client-s3';import {Upload}from'@aws-sdk/lib-storage';import Kt,{createHash}from'crypto';import Qt from'fast-glob';import Rr from'mime-types';import {availableParallelism}from'os';import {Worker}from'worker_threads';function Me(t){let e=t.trim();return e==="."?".":e.replace(/\/$/,"")}function Pt(t){let e=String(t??"").trim();if(!e)return "";let r=e;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 i=r.endsWith("/"),n=r.replace(/^\/+/,"").split("/").map(o=>o.replace(/[^A-Za-z0-9._-]/g,"")).filter(o=>o.length>0&&o!=="."&&o!=="..");if(n.length===0)return "";let s=`/${n.join("/")}`;return i&&b.extname(s)===""&&(s+="/"),s==="/"?"":s}function me(t){return !t||typeof t!="object"?{}:Object.fromEntries(Object.entries(t).map(([e,r])=>[e.trim(),String(r??"")]).filter(([e])=>e.length>0))}function St(t){if(!t||typeof t!="object")return {};let e={};for(let[r,i]of Object.entries(t)){let n=r.trim();if(!n||!i||typeof i!="object")continue;let s=i,o={};if(typeof s.targetOrigin=="string"){let a=Me(s.targetOrigin);a&&(o.targetOrigin=a);}let l=me(s.extraReplacements);if(Object.keys(l).length>0&&(o.extraReplacements=l),s.s3&&typeof s.s3=="object"){let a={},c=s.s3;typeof c.bucket=="string"&&(a.bucket=c.bucket.trim()),typeof c.prefix=="string"&&(a.prefix=c.prefix.trim()),typeof c.region=="string"&&(a.region=c.region.trim()),typeof c.htmlCacheControl=="string"&&(a.htmlCacheControl=c.htmlCacheControl.trim()),typeof c.assetCacheControl=="string"&&(a.assetCacheControl=c.assetCacheControl.trim()),Object.keys(a).length>0&&(o.s3=a);}if(s.cloudFront&&typeof s.cloudFront=="object"){let a={},c=s.cloudFront;typeof c.distributionId=="string"&&(a.distributionId=c.distributionId.trim()),Array.isArray(c.invalidationPaths)&&(a.invalidationPaths=c.invalidationPaths.map(u=>String(u??"").trim()).filter(u=>u.length>0)),Object.keys(a).length>0&&(o.cloudFront=a);}e[n]=o;}return e}function ye(t){return t==="PROFESSIONAL"||t==="AGENCY"?t:void 0}function vt(t,e){if(!t||typeof t!="object")return null;let r=t,i=r.command;if(i!=="publish"&&i!=="crawl"&&i!=="deploy"&&i!=="invalidate"&&i!=="retry-timeouts"&&i!=="url"&&i!=="content-sync")return null;let n=Number.parseInt(String(r.intervalMinutes??"0"),10);if(!Number.isFinite(n)||n<1)return null;let s=String(r.id??`${i}-${e+1}`).trim();if(!s)return null;let o=String(r.deploymentProfile??"").trim(),l=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))]:[],c=Array.isArray(r.listingPaths)?[...new Set(r.listingPaths.map(u=>String(u??"").trim()).filter(Boolean))]:[];return i==="content-sync"&&a.length===0?null:{id:s,enabled:r.enabled!==false,command:i,intervalMinutes:n,...i==="publish"||i==="crawl"?{crawlMode:r.crawlMode==="incremental"?"incremental":"full"}:{},...(i==="publish"||i==="deploy"||i==="invalidate"||i==="content-sync")&&o?{deploymentProfile:o}:{},...l?{url:l}:{},...a.length>0?{postTypes:a}:{},...c.length>0?{listingPaths:c}:{},...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 Ie(t){let e=t&&typeof t=="object"?t:{},r=Array.isArray(e.rules)?e.rules.map((i,n)=>vt(i,n)).filter(i=>!!i):[];return {enabled:e.enabled===true,timezone:typeof e.timezone=="string"&&e.timezone.trim()!==""?e.timezone.trim():"UTC",rules:r}}function Rt(t){if(!t||typeof t!="object")return;let e=t,r={};for(let n of ["accountId","siteId"]){let s=e[n];typeof s=="string"&&s.trim()!==""&&(r[n]=s.trim());}let i=Number(e.lastUpdate??0);return Number.isFinite(i)&&i>0&&(r.lastUpdate=Math.floor(i)),e.subscriber===true&&(r.subscriber=true),Object.keys(r).length>0?r:void 0}function we(t){let e=t&&typeof t=="object"?t:{},r=String(e.accountId??"").trim(),i=String(e.siteId??"").trim(),n=e.subscriber===true,s=Rt(e.siteSettings),o={...s??{},...r?{accountId:s?.accountId??r}:{},...i?{siteId:s?.siteId??i}:{},...s?.subscriber===true||n?{subscriber:true}:{}},l={},a=String(e.apiBase??"").trim();a&&(l.apiBase=a);let c=String(e.runtimeToken??e.nonce??"").trim();c&&(l.runtimeToken=c);let u=String(e.virtualAssetBaseUrl??e.uploadUrl??"").trim();u&&(l.virtualAssetBaseUrl=u),Object.keys(o).length>0&&(l.siteSettings=o);let m=ye(e.subscriptionType);return m&&(l.subscriptionType=m),l}function Dt(t){try{let e=new URL(t);globalThis.location=e;}catch{}}function Le(t){return typeof t=="string"?t:t instanceof URL?t.toString():t.url}function xt(t,e){let r=new Headers(t instanceof Request?t.headers:void 0);if(e?.headers)for(let[i,n]of new Headers(e.headers).entries())r.set(i,n);return r}async function Oe(t,e,r=5){let i=new URL(Le(t)),n=String(e?.method??(t instanceof Request?t.method:"GET")).toUpperCase(),s=i.protocol==="http:"?wt:bt,o=xt(t,e);return await new Promise((l,a)=>{let c=s.request(i,{method:n,headers:Object.fromEntries(o.entries()),...i.protocol==="https:"?{rejectUnauthorized:false}:{}},u=>{let m=[];u.on("data",p=>{m.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),u.on("error",a),u.on("end",()=>{let p=u.statusCode??0,d=u.headers.location;if(r>0&&d&&[301,302,303,307,308].includes(p)){l(Oe(new URL(d,i),{method:p===303?"GET":n},r-1));return}let g=new Headers;for(let[w,T]of Object.entries(u.headers))Array.isArray(T)?g.set(w,T.join(", ")):typeof T=="string"&&g.set(w,T);l(new Response(Buffer.concat(m),{status:p,statusText:u.statusMessage??"",headers:g}));});});c.on("error",a),c.end();})}function $t(t){let e=we(t.wpsuite),r={...e.siteSettings??{},...e.siteSettings?.subscriber===true||e.subscriptionType==="PROFESSIONAL"||e.subscriptionType==="AGENCY"?{subscriber:true}:{}},i=String(r.accountId??"").trim(),n=String(r.siteId??"").trim(),s=String(e.virtualAssetBaseUrl??e.uploadUrl??"").trim();if(t.wpsuite={...e.apiBase?{apiBase:e.apiBase}:{},...e.runtimeToken?{runtimeToken:e.runtimeToken}:{},...s?{virtualAssetBaseUrl:s}:{},...Object.keys(r).length>0?{siteSettings:r}:{},...e.subscriptionType?{subscriptionType:e.subscriptionType}:{}},!i||!n||!s)return false;Dt(t.sourceOrigin);let o=globalThis,a={...o.WpSuite??{},siteSettings:r,uploadUrl:s};return e.apiBase&&(a.apiBase=e.apiBase),o.WpSuite=a,true}async function Tt(t){let e=String(t.wpsuite?.virtualAssetBaseUrl??t.wpsuite?.uploadUrl??"").trim();if(!$t(t)){let n=t.wpsuite?.siteSettings,s=[String(n?.accountId??"").trim()?"":"accountId",String(n?.siteId??"").trim()?"":"siteId",e?"":"virtualAssetBaseUrl"].filter(Boolean);return {remote:null,diagnostic:{status:"invalid-runtime",virtualAssetBaseUrl:e,subscriptionType:null,error:`Missing WP Suite runtime field(s): ${s.join(", ")}.`,requests:[]}}}let r=globalThis.fetch,i=[];try{typeof r=="function"&&/^https?:\/\//i.test(e)&&(globalThis.fetch=(async(a,c)=>{let u=Le(a);if(u.startsWith(e)){let m=(()=>{try{return new URL(u).pathname}catch{return u}})();try{let p=t.ignoreHttpsErrors&&u.startsWith("https://")?await Oe(a,c):await r(a,c);return i.push({path:m,status:p.status,ok:p.ok,error:null}),p}catch(p){throw i.push({path:m,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return r(a,c)}));let{getConfig:n}=await import('@smart-cloud/wpsuite-core'),s=await n("publisher"),o=s&&typeof s=="object"?s:null,l=ye(o?.subscriptionType);return {remote:o,diagnostic:{status:o?"loaded":"unavailable",virtualAssetBaseUrl:e,subscriptionType:l??null,error:o?null:"WP Suite publisher config loader returned no configuration.",requests:i}}}catch(n){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:e,subscriptionType:null,error:n instanceof Error?n.message:String(n),requests:i}}}finally{typeof r=="function"&&(globalThis.fetch=r);}}function Et(t,e){let r=St(e?.deploymentProfiles),i=String(e?.defaultDeploymentProfile??"").trim(),n=r[i]?i:"",s=ye(e?.subscriptionType),o=we(t.wpsuite);return {...t,scheduler:Ie(e?.scheduler),deploymentProfiles:r,defaultDeploymentProfile:n,...s?{subscriptionType:s}:{},wpsuite:{...o,...o.siteSettings||s?{siteSettings:{...o.siteSettings??{},...o.siteSettings?.subscriber===true||s==="PROFESSIONAL"||s==="AGENCY"?{subscriber:true}:{}}}:{},...s?{subscriptionType:s}:{}}}}function he(t,e){let i=String(t||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!i)return e;let n=i.split("/").map(s=>s.trim()).filter(s=>s.length>0&&s!=="."&&s!=="..");return n.length>0?n.join("/"):e}function Ct(){let t=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return t.trim()?b.resolve(t):""}function Ae(t,e,r){let i=String(e||"").trim();return i&&b.isAbsolute(i)?b.resolve(i):b.resolve(t,he(i,r))}async function ne(t,e={}){let r=String("").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",i=await D.readFile(r,"utf8"),n=JSON.parse(i);n.sourceOrigin=n.sourceOrigin.replace(/\/$/,""),n.targetOrigin=Me(n.targetOrigin),n.ignoreHttpsErrors??=false,n.outputDir=String(n.outputDir||"export").trim()||"export",n.urlRewriteMode||=n.targetOrigin==="."?"relative":"absolute",n.noJavaScriptRenderPathPrefixes||=[],n.seedPaths||=[],n.generated404RequestPath=Pt(n.generated404RequestPath),n.sitemapPaths||=["/sitemap_index.xml","/sitemap.xml"],n.allowedAssetHosts||=[],n.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],n.blockedPathPrefixes||=[],n.blockedSearchFragments||=[],n.concurrency||=1,n.maxPages||=0,n.extraReplacements=me(n.extraReplacements),n.postCrawlCopyMap=me(n.postCrawlCopyMap),n.logDir=String(n.logDir||"logs").trim()||"logs",n.verbose??=false,n.logLevel||=n.verbose?"debug":"info",n.s3SyncMode||="sdk-upload-delete",n.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},n.readiness.timeoutMs??=1500,n.readiness.fallbackWaitMs??=1500,n.viewport||={width:1440,height:1200},n.navigationTimeoutMs||=3e4,n.assetDownloadConcurrency=Number(n.assetDownloadConcurrency)>0?Number(n.assetDownloadConcurrency):n.concurrency,n.rewriteConcurrency=Number(n.rewriteConcurrency)>0?Number(n.rewriteConcurrency):n.assetDownloadConcurrency,n.wpsuite=we(n.wpsuite),n.scheduler=Ie(void 0),n.deploymentProfiles={},n.defaultDeploymentProfile="",n.deploymentTargetOverride=String(n.deploymentTargetOverride??"").trim();let s=await Tt(n);e.onRemotePublisherConfigDiagnostic?.(s.diagnostic);let o=Et(n,s.remote),l=Ct(),a=l?b.resolve(l,".."):"";return a?(o.outputDir=Ae(a,o.outputDir,"export"),o.logDir=Ae(a,o.logDir,"logs")):(b.isAbsolute(o.outputDir)||(o.outputDir=he(o.outputDir,"export")),b.isAbsolute(o.logDir)||(o.logDir=he(o.logDir,"logs"))),o}function kt(t){let e=t/6e4;if(Number.isInteger(e)&&e>=1)return `${e} minute${e===1?"":"s"}`;let r=Math.ceil(t/1e3);return `${r} second${r===1?"":"s"}`}async function O(t,e,r){let i=new AbortController,n=globalThis.setTimeout(()=>{i.abort(new Error(`${t} timed out after ${kt(e)}.`));},e);try{return await r({abortController:i,httpOptions:{abortSignal:i.signal,requestTimeout:e}})}finally{globalThis.clearTimeout(n);}}var At=".deploy-plan.json",Mt="deploy-plan.json",It=".crawl-incomplete.json",Lt="crawl-incomplete.json";function je(){let t=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return t.trim()?b.resolve(t):""}function Ot(t){let e=String(t||"").replace(/\\/g,"/").replace(/^\/+/,"").trim(),r=b.posix.normalize(e);return !e||!r||r==="."||/^[a-zA-Z]:/.test(r)||r.startsWith("../")||r===".."?null:r}function jt(t){let e=String(t||"").trim();if(!e.startsWith("/")||e.startsWith("//")||e.includes("*")||e.includes("#"))return null;try{let r=new URL(e,"https://content-sync.invalid");return r.origin!=="https://content-sync.invalid"?null:`${b.posix.normalize(r.pathname)}${r.search}`}catch{return null}}function j(t){return [...new Set(t.map(Ot).filter(e=>e!==null))]}function Ft(t,e){let r={generatedAt:String(e.generatedAt||new Date().toISOString()),outputDir:b.resolve(t.outputDir),runMode:e.runMode,crawlMode:e.crawlMode,fullSyncRequired:!!e.fullSyncRequired,changedFiles:j(e.changedFiles||[]),deletedFiles:j(e.deletedFiles||[]),rewriteTargets:j(e.rewriteTargets||[])};return e.schemaVersion===2?{...r,schemaVersion:2,runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,consumerId:String(e.consumerId||"").trim(),fromSequence:Math.max(0,Number(e.fromSequence)||0),toSequence:Math.max(0,Number(e.toSequence)||0),impactHash:String(e.impactHash||"").trim(),uploadGroups:{assets:j(e.uploadGroups?.assets||[]),content:j(e.uploadGroups?.content||[]),listings:j(e.uploadGroups?.listings||[]),sitemapLeaves:j(e.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:j(e.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((e.invalidationPaths||[]).map(jt).filter(i=>i!==null))].sort()}:{schemaVersion:1,...r,runMode:e.runMode}}function _t(t){if(!t||typeof t!="object")return false;let e=t;if(e.schemaVersion===2){let r=e;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.changedFiles)&&Array.isArray(r.deletedFiles)&&Array.isArray(r.rewriteTargets)&&!!r.uploadGroups&&Array.isArray(r.uploadGroups.assets)&&Array.isArray(r.uploadGroups.content)&&Array.isArray(r.uploadGroups.listings)&&Array.isArray(r.uploadGroups.sitemapLeaves)&&Array.isArray(r.uploadGroups.sitemapIndexes)&&Array.isArray(r.invalidationPaths)}return e.schemaVersion===1&&typeof e.generatedAt=="string"&&typeof e.outputDir=="string"&&(e.runMode==="full"||e.runMode==="retry-timeouts"||e.runMode==="single-url")&&(e.crawlMode==="full"||e.crawlMode==="incremental")&&typeof e.fullSyncRequired=="boolean"&&Array.isArray(e.changedFiles)&&Array.isArray(e.deletedFiles)&&Array.isArray(e.rewriteTargets)}function Fe(t){let e=b.join(b.resolve(t.outputDir),At),r=je();return r?[b.join(r,Mt),e]:[e]}function Ut(t){let e=b.join(b.resolve(t.outputDir),It),r=je();return r?[b.join(r,Lt),e]:[e]}async function _e(t){let e=b.resolve(t.outputDir);for(let r of Fe(t))try{let i=await D.readFile(r,"utf8"),n=JSON.parse(i);if(!_t(n)||b.resolve(n.outputDir)!==e)continue;return Ft(t,n)}catch{}return null}async function Ue(t){for(let e of Fe(t))try{await D.unlink(e);}catch(r){if(r.code!=="ENOENT")throw r}}async function Ne(t){let e=b.resolve(t.outputDir);for(let r of Ut(t)){let i;try{i=await D.readFile(r,"utf8");}catch(s){if(s.code==="ENOENT")continue;throw s}let n;try{n=JSON.parse(i);}catch{throw new Error(`Invalid incomplete crawl marker: ${r}`)}if(n.schemaVersion!==1||typeof n.startedAt!="string"||typeof n.outputDir!="string"||b.resolve(n.outputDir)!==e)throw new Error(`Invalid incomplete crawl marker: ${r}`);return {schemaVersion:1,startedAt:n.startedAt,outputDir:e}}return null}function Nt(t){return typeof t.subscriptionType=="string"&&t.subscriptionType.trim()!==""}function ze(t){let e="";for(let i=0;i<t.length;i++){let n=t[i];if(n==="--profile"){let s=(t[i+1]||"").trim();if(!s)throw new Error("Missing value for --profile.");e=s,i+=1;}else if(n.startsWith("--profile=")){let s=n.slice(10).trim();if(!s)throw new Error("Missing value for --profile.");e=s;}}let r=String(process.env.PUBLISHER_DEPLOY_PROFILE||process.env.PUBLISHER_DEPLOYMENT_PROFILE||"").trim();return e||r||null}function zt(t,e){return {...t,...e??{}}}function Wt(t,e){return {...t,...e??{},invalidationPaths:[...(e?.invalidationPaths??t.invalidationPaths)||[]]}}function We(t,e){let r=String(e??t.deploymentTargetOverride??"").trim();if(!Nt(t)){if(r)throw new Error(`Deployment target "${r}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:t}}let i=String(r||t.defaultDeploymentProfile||"").trim();if(!i)return {name:null,profile:null,config:t};let n=t.deploymentProfiles?.[i];if(!n)throw new Error(`Unknown deployment profile "${i}". Check the linked WPSuite publisher configuration.`);return {name:i,profile:n,config:{...t,targetOrigin:n.targetOrigin??t.targetOrigin,s3:zt(t.s3,n.s3),cloudFront:Wt(t.cloudFront,n.cloudFront)}}}var qe={error:0,warn:1,info:2,debug:3},qt=/^(?:authorization|proxyAuthorization|runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken|xStaticPublisherToken|xApiKey)$/i;function ee(t){return t.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 se(t){return typeof t=="string"?ee(t):Array.isArray(t)?t.map(e=>se(e)):t instanceof Error?{name:t.name,message:ee(t.message),...t.stack?{stack:ee(t.stack)}:{}}:t&&typeof t=="object"?Object.fromEntries(Object.entries(t).map(([e,r])=>[e,qt.test(e)?"[REDACTED]":se(r)])):t}function Ke(t){return t?se(t):void 0}var Be=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",ie=Be?b.join(Be,"current-progress.json"):"",Ge=Promise.resolve();function He(t){return t.includes("crawl")?"crawl":t.includes("deploy")?"deploy":t.includes("invalidate")?"invalidate":t}function Bt(t,e){let r={...t},i=new Set(["pagesQueued","pagesRendered","assetsQueued","sitemapsQueued","pagesDiscovered","assetsDiscovered","sitemapsDiscovered","pagesSaved","assetsSaved","donePages","doneAssets","doneSitemaps","changedTextFiles","uploaded","failed","index","totalFiles","pageQueue","assetQueue","sitemapQueue"]);for(let[n,s]of Object.entries(e)){let o=r[n];if(typeof o=="number"&&typeof s=="number"&&i.has(n)){r[n]=Math.max(o,s);continue}r[n]=s;}return r}function Gt(t,e,r){ie&&(Ge=Ge.then(async()=>{let i=new Date,n=i.toISOString();await D.mkdir(b.dirname(ie),{recursive:true});let s=await D.readFile(ie,"utf8").then(S=>JSON.parse(S)).catch(()=>null),o=s&&typeof s.details=="object"&&s.details?s.details:{},l=He(t),a=s?.currentStep||He(s?.source||""),c=s?.startedAt||n,u={...s?.stepDurationsSec??{}},m=s?.stepStartedAt||n;if(a&&a!==l&&s?.stepStartedAt){let S=Math.max(0,Math.round((i.getTime()-new Date(s.stepStartedAt).getTime())/1e3));u[a]=(u[a]??0)+S,m=n;}let p=Math.max(0,Math.round((i.getTime()-new Date(m).getTime())/1e3)),d=Math.max(0,Math.round((i.getTime()-new Date(c).getTime())/1e3)),g=ee(e),w=Ke(r)??{},T={checkedAt:n,source:t,message:g,details:Bt(o,w),startedAt:c,currentStep:l,stepStartedAt:m,stepElapsedSec:p,totalElapsedSec:d,stepDurationsSec:u};await D.writeFile(ie,JSON.stringify(T,null,2),"utf8");}).catch(()=>{}));}async function Ht(t,e){await D.mkdir(b.dirname(t),{recursive:true}),await D.appendFile(t,`${JSON.stringify(se(e))}
2
2
  `,"utf8");}var G=class{constructor(e,r,i="info"){this.logDir=e;this.logFile=r;this.level=i;this.ensureLogFileReady();}logDir;logFile;level;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;get logPath(){return b.join(this.logDir,this.logFile)}artifactPath(e){return this.logFile.endsWith(".log.jsonl")?b.join(this.logDir,this.logFile.replace(/\.log\.jsonl$/u,`${e}.jsonl`)):b.join(this.logDir,`${this.logFile}${e}.jsonl`)}get errorsPath(){return this.artifactPath(".errors")}get rejectedPath(){return this.artifactPath(".rejected")}ensureLogFileReady(){return this.initPromise||(this.initPromise=(async()=>{await D.mkdir(this.logDir,{recursive:true}),await D.writeFile(this.logPath,"","utf8");})()),this.initPromise}enqueueTask(e){this.writeQueue=this.writeQueue.then(e).catch(r=>{this.writeError=r instanceof Error?r:new Error(String(r));});}enqueueLine(e){this.enqueueTask(async()=>{await this.ensureLogFileReady(),await D.appendFile(this.logPath,`${e}
3
3
  `,"utf8");});}enqueueJsonLine(e,r){this.enqueueTask(()=>Ht(e,r));}accepts(e){return qe[e]<=qe[this.level]}push(e,r,i){if(!this.accepts(e))return;let n=ee(r),s=Ke(i),o=JSON.stringify({time:new Date().toISOString(),level:e,message:n,...s||{}});this.enqueueLine(o),e==="error"?console.error(`[ERROR] ${n}`,s?JSON.stringify(s):""):e==="warn"?console.warn(`[WARN] ${n}`,s?JSON.stringify(s):""):console.log(`[${e.toUpperCase().padEnd(5)}] ${n}`);}info(e,r){this.push("info",e,r);}progress(e,r){Gt(this.logFile,e,r);}debug(e,r){this.push("debug",e,r);}warn(e,r){this.push("warn",e,r);}reject(e,r,i,n,s){let o={kind:e,url:r,reason:i,...n?{source:n}:{},...s||{}};this.enqueueJsonLine(this.rejectedPath,o),this.accepts("debug")&&this.push("debug",`Rejected ${e} ${r}: ${i}`,o);}error(e,r){this.enqueueJsonLine(this.errorsPath,{message:e,...r||{}}),this.push("error",e,r);}mark(e){this.marks.set(e,Date.now());}endMark(e,r){let i=this.marks.get(e);if(!i)return;let n=Date.now()-i,s=Number((n/1e3).toFixed(2));this.push("info",`Timing ${e}: ${s}s`,{name:e,ms:n,seconds:s,...r||{}});}async flush(){let e=Date.now()-this.startedAt,r=Number((e/1e3).toFixed(2));if(this.push("info",`Total time: ${r}s`,{name:"total",ms:e,seconds:r}),await this.writeQueue,this.writeError)throw this.writeError}};async function Jt(t){await D.mkdir(b.dirname(t),{recursive:true});}async function Qe(t,e,r){await Jt(t);let i=b.join(b.dirname(t),`.${b.basename(t)}.${process.pid}.${Date.now()}.${Kt.randomBytes(6).toString("hex")}.tmp`);try{typeof e=="string"?await D.writeFile(i,e,r??"utf8"):await D.writeFile(i,e),await D.rename(i,t);}catch(n){throw await D.unlink(i).catch(()=>{}),n}}function Ye(t){return [".html",".htm",".css",".js",".mjs",".json",".xml",".xsl",".txt",".svg",".map",".enc",".jws"].includes(b.extname(t).toLowerCase())}function Ze(t){return t.replace(/&quot;/g,'"').replace(/&#34;/g,'"').replace(/&apos;/g,"'").replace(/&#39;/g,"'").replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">")}function H(t){return t.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function oe(t){return t.replace(/\//g,"\\/")}function Je(t){return t.replace(/\//g,"\\\\/")}function Vt(t){try{let e=new URL(t);if(e.protocol!=="http:"&&e.protocol!=="https:")return null;let r=e.pathname==="/"&&!e.search&&!e.hash?"":`${e.pathname}${e.search}${e.hash}`;return `//${e.host}${r}`}catch{return null}}function Ve(t,e,r){if(!e)return;t[e]=r;let i=oe(e),n=oe(r);t[i]=n;let s=Je(e),o=Je(r);t[s]=o;let l=H(e),a=H(r);t[l]=a;let c=H(i),u=H(n);t[c]=u;let m=H(s),p=H(o);t[m]=p;}function ae(t,e,r){Ve(t,e,r);let i=Vt(e);i&&i!==e&&Ve(t,i,r);}function Xe(t,e){try{return new URL(t,e==="."?"https://relative.invalid":e).pathname}catch{return t.startsWith("/")?t:`/${t.replace(/^\.\//,"")}`}}function et(t,e,r){if(!e)return r;let i=b.dirname(b.resolve(e)),n=b.resolve(t,r.replace(/^\/+/,"")),s=b.relative(i,n).replace(/\\/g,"/");return s?(s.startsWith(".")||(s=`./${s}`),s):"."}var er=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],tr=new Set([".html",".htm"]),tt="WPSuite.io Static Publisher",rr=tt.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function nr(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function ir(t){return t.startsWith("//")||t.startsWith("\\/\\/")||t.startsWith("\\\\/\\\\/")}function sr(t,e,r){return ir(e)?t.replace(new RegExp(`(?<!:)${nr(e)}`,"g"),r):t.split(e).join(r)}function or(t,e,r){if(t.wpsuite?.siteSettings?.subscriber===true||t.wpsuite?.subscriptionType==="PROFESSIONAL"||t.wpsuite?.subscriptionType==="AGENCY"||!r)return false;let n=b.extname(r).toLowerCase();return tr.has(n)?/<head\b|<html\b|<!doctype html/i.test(e):false}function ar(t){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${rr}\\2)[^>]*\\/?>`,"i").test(t))return t;let e=`<meta name="generator" content="${tt}" />`;if(t.match(/(\r?\n)([ \t]*)<\/head>/i))return t.replace(/(\r?\n)([ \t]*)<\/head>/i,`$1$2${e}$1$2</head>`);let i=t.includes(`\r
4
4
  `)?`\r
@@ -1,3 +1,3 @@
1
- import w from'fs/promises';import ee from'http';import te from'https';import y from'path';import {CloudFrontClient,waitUntilInvalidationCompleted,CreateInvalidationCommand}from'@aws-sdk/client-cloudfront';import {createHash}from'crypto';function j(r){let e=r.trim();return e==="."?".":e.replace(/\/$/,"")}function re(r){let e=String(r??"").trim();if(!e)return "";let t=e;if(/^https?:\/\//i.test(t))try{t=new URL(t).pathname;}catch{return ""}if(t=t.split(/[?#]/,1)[0]?.replace(/\\/g,"/").trim()??"",!t)return "";let n=t.endsWith("/"),i=t.replace(/^\/+/,"").split("/").map(o=>o.replace(/[^A-Za-z0-9._-]/g,"")).filter(o=>o.length>0&&o!=="."&&o!=="..");if(i.length===0)return "";let s=`/${i.join("/")}`;return n&&y.extname(s)===""&&(s+="/"),s==="/"?"":s}function k(r){return !r||typeof r!="object"?{}:Object.fromEntries(Object.entries(r).map(([e,t])=>[e.trim(),String(t??"")]).filter(([e])=>e.length>0))}function ie(r){if(!r||typeof r!="object")return {};let e={};for(let[t,n]of Object.entries(r)){let i=t.trim();if(!i||!n||typeof n!="object")continue;let s=n,o={};if(typeof s.targetOrigin=="string"){let a=j(s.targetOrigin);a&&(o.targetOrigin=a);}let u=k(s.extraReplacements);if(Object.keys(u).length>0&&(o.extraReplacements=u),s.s3&&typeof s.s3=="object"){let a={},l=s.s3;typeof l.bucket=="string"&&(a.bucket=l.bucket.trim()),typeof l.prefix=="string"&&(a.prefix=l.prefix.trim()),typeof l.region=="string"&&(a.region=l.region.trim()),typeof l.htmlCacheControl=="string"&&(a.htmlCacheControl=l.htmlCacheControl.trim()),typeof l.assetCacheControl=="string"&&(a.assetCacheControl=l.assetCacheControl.trim()),Object.keys(a).length>0&&(o.s3=a);}if(s.cloudFront&&typeof s.cloudFront=="object"){let a={},l=s.cloudFront;typeof l.distributionId=="string"&&(a.distributionId=l.distributionId.trim()),Array.isArray(l.invalidationPaths)&&(a.invalidationPaths=l.invalidationPaths.map(c=>String(c??"").trim()).filter(c=>c.length>0)),Object.keys(a).length>0&&(o.cloudFront=a);}e[i]=o;}return e}function L(r){return r==="PROFESSIONAL"||r==="AGENCY"?r:void 0}function ne(r,e){if(!r||typeof r!="object")return null;let t=r,n=t.command;if(n!=="publish"&&n!=="crawl"&&n!=="deploy"&&n!=="invalidate"&&n!=="retry-timeouts"&&n!=="url"&&n!=="content-sync")return null;let i=Number.parseInt(String(t.intervalMinutes??"0"),10);if(!Number.isFinite(i)||i<1)return null;let s=String(t.id??`${n}-${e+1}`).trim();if(!s)return null;let o=String(t.deploymentProfile??"").trim(),u=String(t.url??"").trim(),a=Array.isArray(t.postTypes)?[...new Set(t.postTypes.map(c=>String(c??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],l=Array.isArray(t.listingPaths)?[...new Set(t.listingPaths.map(c=>String(c??"").trim()).filter(Boolean))]:[];return n==="content-sync"&&a.length===0?null:{id:s,enabled:t.enabled!==false,command:n,intervalMinutes:i,...n==="publish"||n==="crawl"?{crawlMode:t.crawlMode==="incremental"?"incremental":"full"}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&o?{deploymentProfile:o}:{},...u?{url:u}:{},...a.length>0?{postTypes:a}:{},...l.length>0?{listingPaths:l}:{},...typeof t.includePostTypeArchives=="boolean"?{includePostTypeArchives:t.includePostTypeArchives}:{},...typeof t.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:t.includeTaxonomyArchives}:{},...typeof t.includeAuthorArchives=="boolean"?{includeAuthorArchives:t.includeAuthorArchives}:{},...typeof t.includeDateArchives=="boolean"?{includeDateArchives:t.includeDateArchives}:{},...typeof t.includePostsPage=="boolean"?{includePostsPage:t.includePostsPage}:{},...typeof t.includeSitemapChain=="boolean"?{includeSitemapChain:t.includeSitemapChain}:{}}}function U(r){let e=r&&typeof r=="object"?r:{},t=Array.isArray(e.rules)?e.rules.map((n,i)=>ne(n,i)).filter(n=>!!n):[];return {enabled:e.enabled===true,timezone:typeof e.timezone=="string"&&e.timezone.trim()!==""?e.timezone.trim():"UTC",rules:t}}function se(r){if(!r||typeof r!="object")return;let e=r,t={};for(let i of ["accountId","siteId"]){let s=e[i];typeof s=="string"&&s.trim()!==""&&(t[i]=s.trim());}let n=Number(e.lastUpdate??0);return Number.isFinite(n)&&n>0&&(t.lastUpdate=Math.floor(n)),e.subscriber===true&&(t.subscriber=true),Object.keys(t).length>0?t:void 0}function x(r){let e=r&&typeof r=="object"?r:{},t=String(e.accountId??"").trim(),n=String(e.siteId??"").trim(),i=e.subscriber===true,s=se(e.siteSettings),o={...s??{},...t?{accountId:s?.accountId??t}:{},...n?{siteId:s?.siteId??n}:{},...s?.subscriber===true||i?{subscriber:true}:{}},u={},a=String(e.apiBase??"").trim();a&&(u.apiBase=a);let l=String(e.runtimeToken??e.nonce??"").trim();l&&(u.runtimeToken=l);let c=String(e.virtualAssetBaseUrl??e.uploadUrl??"").trim();c&&(u.virtualAssetBaseUrl=c),Object.keys(o).length>0&&(u.siteSettings=o);let d=L(e.subscriptionType);return d&&(u.subscriptionType=d),u}function oe(r){try{let e=new URL(r);globalThis.location=e;}catch{}}function $(r){return typeof r=="string"?r:r instanceof URL?r.toString():r.url}function ae(r,e){let t=new Headers(r instanceof Request?r.headers:void 0);if(e?.headers)for(let[n,i]of new Headers(e.headers).entries())t.set(n,i);return t}async function _(r,e,t=5){let n=new URL($(r)),i=String(e?.method??(r instanceof Request?r.method:"GET")).toUpperCase(),s=n.protocol==="http:"?ee:te,o=ae(r,e);return await new Promise((u,a)=>{let l=s.request(n,{method:i,headers:Object.fromEntries(o.entries()),...n.protocol==="https:"?{rejectUnauthorized:false}:{}},c=>{let d=[];c.on("data",p=>{d.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),c.on("error",a),c.on("end",()=>{let p=c.statusCode??0,m=c.headers.location;if(t>0&&m&&[301,302,303,307,308].includes(p)){u(_(new URL(m,n),{method:p===303?"GET":i},t-1));return}let g=new Headers;for(let[f,h]of Object.entries(c.headers))Array.isArray(h)?g.set(f,h.join(", ")):typeof h=="string"&&g.set(f,h);u(new Response(Buffer.concat(d),{status:p,statusText:c.statusMessage??"",headers:g}));});});l.on("error",a),l.end();})}function le(r){let e=x(r.wpsuite),t={...e.siteSettings??{},...e.siteSettings?.subscriber===true||e.subscriptionType==="PROFESSIONAL"||e.subscriptionType==="AGENCY"?{subscriber:true}:{}},n=String(t.accountId??"").trim(),i=String(t.siteId??"").trim(),s=String(e.virtualAssetBaseUrl??e.uploadUrl??"").trim();if(r.wpsuite={...e.apiBase?{apiBase:e.apiBase}:{},...e.runtimeToken?{runtimeToken:e.runtimeToken}:{},...s?{virtualAssetBaseUrl:s}:{},...Object.keys(t).length>0?{siteSettings:t}:{},...e.subscriptionType?{subscriptionType:e.subscriptionType}:{}},!n||!i||!s)return false;oe(r.sourceOrigin);let o=globalThis,a={...o.WpSuite??{},siteSettings:t,uploadUrl:s};return e.apiBase&&(a.apiBase=e.apiBase),o.WpSuite=a,true}async function ue(r){let e=String(r.wpsuite?.virtualAssetBaseUrl??r.wpsuite?.uploadUrl??"").trim();if(!le(r)){let i=r.wpsuite?.siteSettings,s=[String(i?.accountId??"").trim()?"":"accountId",String(i?.siteId??"").trim()?"":"siteId",e?"":"virtualAssetBaseUrl"].filter(Boolean);return {remote:null,diagnostic:{status:"invalid-runtime",virtualAssetBaseUrl:e,subscriptionType:null,error:`Missing WP Suite runtime field(s): ${s.join(", ")}.`,requests:[]}}}let t=globalThis.fetch,n=[];try{typeof t=="function"&&/^https?:\/\//i.test(e)&&(globalThis.fetch=(async(a,l)=>{let c=$(a);if(c.startsWith(e)){let d=(()=>{try{return new URL(c).pathname}catch{return c}})();try{let p=r.ignoreHttpsErrors&&c.startsWith("https://")?await _(a,l):await t(a,l);return n.push({path:d,status:p.status,ok:p.ok,error:null}),p}catch(p){throw n.push({path:d,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return t(a,l)}));let{getConfig:i}=await import('@smart-cloud/wpsuite-core'),s=await i("publisher"),o=s&&typeof s=="object"?s:null,u=L(o?.subscriptionType);return {remote:o,diagnostic:{status:o?"loaded":"unavailable",virtualAssetBaseUrl:e,subscriptionType:u??null,error:o?null:"WP Suite publisher config loader returned no configuration.",requests:n}}}catch(i){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:e,subscriptionType:null,error:i instanceof Error?i.message:String(i),requests:n}}}finally{typeof t=="function"&&(globalThis.fetch=t);}}function ce(r,e){let t=ie(e?.deploymentProfiles),n=String(e?.defaultDeploymentProfile??"").trim(),i=t[n]?n:"",s=L(e?.subscriptionType),o=x(r.wpsuite);return {...r,scheduler:U(e?.scheduler),deploymentProfiles:t,defaultDeploymentProfile:i,...s?{subscriptionType:s}:{},wpsuite:{...o,...o.siteSettings||s?{siteSettings:{...o.siteSettings??{},...o.siteSettings?.subscriber===true||s==="PROFESSIONAL"||s==="AGENCY"?{subscriber:true}:{}}}:{},...s?{subscriptionType:s}:{}}}}function E(r,e){let n=String(r||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!n)return e;let i=n.split("/").map(s=>s.trim()).filter(s=>s.length>0&&s!=="."&&s!=="..");return i.length>0?i.join("/"):e}function ge(){let r=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return r.trim()?y.resolve(r):""}function N(r,e,t){let n=String(e||"").trim();return n&&y.isAbsolute(n)?y.resolve(n):y.resolve(r,E(n,t))}async function D(r,e={}){let t=String("").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",n=await w.readFile(t,"utf8"),i=JSON.parse(n);i.sourceOrigin=i.sourceOrigin.replace(/\/$/,""),i.targetOrigin=j(i.targetOrigin),i.ignoreHttpsErrors??=false,i.outputDir=String(i.outputDir||"export").trim()||"export",i.urlRewriteMode||=i.targetOrigin==="."?"relative":"absolute",i.noJavaScriptRenderPathPrefixes||=[],i.seedPaths||=[],i.generated404RequestPath=re(i.generated404RequestPath),i.sitemapPaths||=["/sitemap_index.xml","/sitemap.xml"],i.allowedAssetHosts||=[],i.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],i.blockedPathPrefixes||=[],i.blockedSearchFragments||=[],i.concurrency||=1,i.maxPages||=0,i.extraReplacements=k(i.extraReplacements),i.postCrawlCopyMap=k(i.postCrawlCopyMap),i.logDir=String(i.logDir||"logs").trim()||"logs",i.verbose??=false,i.logLevel||=i.verbose?"debug":"info",i.s3SyncMode||="sdk-upload-delete",i.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},i.readiness.timeoutMs??=1500,i.readiness.fallbackWaitMs??=1500,i.viewport||={width:1440,height:1200},i.navigationTimeoutMs||=3e4,i.assetDownloadConcurrency=Number(i.assetDownloadConcurrency)>0?Number(i.assetDownloadConcurrency):i.concurrency,i.rewriteConcurrency=Number(i.rewriteConcurrency)>0?Number(i.rewriteConcurrency):i.assetDownloadConcurrency,i.wpsuite=x(i.wpsuite),i.scheduler=U(void 0),i.deploymentProfiles={},i.defaultDeploymentProfile="",i.deploymentTargetOverride=String(i.deploymentTargetOverride??"").trim();let s=await ue(i);e.onRemotePublisherConfigDiagnostic?.(s.diagnostic);let o=ce(i,s.remote),u=ge(),a=u?y.resolve(u,".."):"";return a?(o.outputDir=N(a,o.outputDir,"export"),o.logDir=N(a,o.logDir,"logs")):(y.isAbsolute(o.outputDir)||(o.outputDir=E(o.outputDir,"export")),y.isAbsolute(o.logDir)||(o.logDir=E(o.logDir,"logs"))),o}function pe(r){let e=r/6e4;if(Number.isInteger(e)&&e>=1)return `${e} minute${"s"}`;let t=Math.ceil(r/1e3);return `${t} second${t===1?"":"s"}`}async function M(r,e,t){let n=new AbortController,i=globalThis.setTimeout(()=>{n.abort(new Error(`${r} timed out after ${pe(e)}.`));},e);try{return await t({abortController:n,httpOptions:{abortSignal:n.signal,requestTimeout:e}})}finally{globalThis.clearTimeout(i);}}function me(r){return String(r||"").replace(/\\/g,"/").replace(/^\/+|\/+$/g,"")}function he(r,e){let t=r;t.endsWith("/")?t+="index.html":y.posix.basename(t).includes(".")||(t+="/index.html");let n=me(e);return n?`/${n}${t}`.replace(/\/{2,}/g,"/"):t}function q(r,e){let t=new Set;for(let n of r){let i=new URL(n,"https://content-sync.invalid");if(i.origin!=="https://content-sync.invalid")continue;let s=`${i.pathname}${i.search}`;t.add(s),t.add(`${he(i.pathname,e)}${i.search}`);}return [...t].sort()}var ye=".deploy-plan.json",Pe="deploy-plan.json";function be(){let r=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return r.trim()?y.resolve(r):""}function we(r){let e=String(r||"").replace(/\\/g,"/").replace(/^\/+/,"").trim(),t=y.posix.normalize(e);return !e||!t||t==="."||/^[a-zA-Z]:/.test(t)||t.startsWith("../")||t===".."?null:t}function Se(r){let e=String(r||"").trim();if(!e.startsWith("/")||e.startsWith("//")||e.includes("*")||e.includes("#"))return null;try{let t=new URL(e,"https://content-sync.invalid");return t.origin!=="https://content-sync.invalid"?null:`${y.posix.normalize(t.pathname)}${t.search}`}catch{return null}}function b(r){return [...new Set(r.map(we).filter(e=>e!==null))]}function ve(r,e){let t={generatedAt:String(e.generatedAt||new Date().toISOString()),outputDir:y.resolve(r.outputDir),runMode:e.runMode,crawlMode:e.crawlMode,fullSyncRequired:!!e.fullSyncRequired,changedFiles:b(e.changedFiles||[]),deletedFiles:b(e.deletedFiles||[]),rewriteTargets:b(e.rewriteTargets||[])};return e.schemaVersion===2?{...t,schemaVersion:2,runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,consumerId:String(e.consumerId||"").trim(),fromSequence:Math.max(0,Number(e.fromSequence)||0),toSequence:Math.max(0,Number(e.toSequence)||0),impactHash:String(e.impactHash||"").trim(),uploadGroups:{assets:b(e.uploadGroups?.assets||[]),content:b(e.uploadGroups?.content||[]),listings:b(e.uploadGroups?.listings||[]),sitemapLeaves:b(e.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:b(e.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((e.invalidationPaths||[]).map(Se).filter(n=>n!==null))].sort()}:{schemaVersion:1,...t,runMode:e.runMode}}function Ie(r){if(!r||typeof r!="object")return false;let e=r;if(e.schemaVersion===2){let t=e;return t.runMode==="content-sync"&&t.crawlMode==="incremental"&&t.fullSyncRequired===false&&typeof t.generatedAt=="string"&&typeof t.outputDir=="string"&&typeof t.consumerId=="string"&&t.consumerId.trim()!==""&&Number.isSafeInteger(t.fromSequence)&&Number.isSafeInteger(t.toSequence)&&Number(t.toSequence)>=Number(t.fromSequence)&&typeof t.impactHash=="string"&&/^[a-f0-9]{64}$/.test(t.impactHash)&&Array.isArray(t.changedFiles)&&Array.isArray(t.deletedFiles)&&Array.isArray(t.rewriteTargets)&&!!t.uploadGroups&&Array.isArray(t.uploadGroups.assets)&&Array.isArray(t.uploadGroups.content)&&Array.isArray(t.uploadGroups.listings)&&Array.isArray(t.uploadGroups.sitemapLeaves)&&Array.isArray(t.uploadGroups.sitemapIndexes)&&Array.isArray(t.invalidationPaths)}return e.schemaVersion===1&&typeof e.generatedAt=="string"&&typeof e.outputDir=="string"&&(e.runMode==="full"||e.runMode==="retry-timeouts"||e.runMode==="single-url")&&(e.crawlMode==="full"||e.crawlMode==="incremental")&&typeof e.fullSyncRequired=="boolean"&&Array.isArray(e.changedFiles)&&Array.isArray(e.deletedFiles)&&Array.isArray(e.rewriteTargets)}function Ce(r){let e=y.join(y.resolve(r.outputDir),ye),t=be();return t?[y.join(t,Pe),e]:[e]}async function W(r){let e=y.resolve(r.outputDir);for(let t of Ce(r))try{let n=await w.readFile(t,"utf8"),i=JSON.parse(n);if(!Ie(i)||y.resolve(i.outputDir)!==e)continue;return ve(r,i)}catch{}return null}function De(r){return typeof r.subscriptionType=="string"&&r.subscriptionType.trim()!==""}function B(r){let e="";for(let n=0;n<r.length;n++){let i=r[n];if(i==="--profile"){let s=(r[n+1]||"").trim();if(!s)throw new Error("Missing value for --profile.");e=s,n+=1;}else if(i.startsWith("--profile=")){let s=i.slice(10).trim();if(!s)throw new Error("Missing value for --profile.");e=s;}}let t=String(process.env.PUBLISHER_DEPLOY_PROFILE||process.env.PUBLISHER_DEPLOYMENT_PROFILE||"").trim();return e||t||null}function Te(r,e){return {...r,...e??{}}}function Re(r,e){return {...r,...e??{},invalidationPaths:[...(e?.invalidationPaths??r.invalidationPaths)||[]]}}function H(r,e){let t=String(e??r.deploymentTargetOverride??"").trim();if(!De(r)){if(t)throw new Error(`Deployment target "${t}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:r}}let n=String(t||r.defaultDeploymentProfile||"").trim();if(!n)return {name:null,profile:null,config:r};let i=r.deploymentProfiles?.[n];if(!i)throw new Error(`Unknown deployment profile "${n}". Check the linked WPSuite publisher configuration.`);return {name:n,profile:i,config:{...r,targetOrigin:i.targetOrigin??r.targetOrigin,s3:Te(r.s3,i.s3),cloudFront:Re(r.cloudFront,i.cloudFront)}}}var z={error:0,warn:1,info:2,debug:3},Ae=/^(?:authorization|proxyAuthorization|runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken|xStaticPublisherToken|xApiKey)$/i;function C(r){return r.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 R(r){return typeof r=="string"?C(r):Array.isArray(r)?r.map(e=>R(e)):r instanceof Error?{name:r.name,message:C(r.message),...r.stack?{stack:C(r.stack)}:{}}:r&&typeof r=="object"?Object.fromEntries(Object.entries(r).map(([e,t])=>[e,Ae.test(e)?"[REDACTED]":R(t)])):r}function Q(r){return r?R(r):void 0}var J=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",T=J?y.join(J,"current-progress.json"):"",G=Promise.resolve();function V(r){return r.includes("crawl")?"crawl":r.includes("deploy")?"deploy":r.includes("invalidate")?"invalidate":r}function ke(r,e){let t={...r},n=new Set(["pagesQueued","pagesRendered","assetsQueued","sitemapsQueued","pagesDiscovered","assetsDiscovered","sitemapsDiscovered","pagesSaved","assetsSaved","donePages","doneAssets","doneSitemaps","changedTextFiles","uploaded","failed","index","totalFiles","pageQueue","assetQueue","sitemapQueue"]);for(let[i,s]of Object.entries(e)){let o=t[i];if(typeof o=="number"&&typeof s=="number"&&n.has(i)){t[i]=Math.max(o,s);continue}t[i]=s;}return t}function Ee(r,e,t){T&&(G=G.then(async()=>{let n=new Date,i=n.toISOString();await w.mkdir(y.dirname(T),{recursive:true});let s=await w.readFile(T,"utf8").then(I=>JSON.parse(I)).catch(()=>null),o=s&&typeof s.details=="object"&&s.details?s.details:{},u=V(r),a=s?.currentStep||V(s?.source||""),l=s?.startedAt||i,c={...s?.stepDurationsSec??{}},d=s?.stepStartedAt||i;if(a&&a!==u&&s?.stepStartedAt){let I=Math.max(0,Math.round((n.getTime()-new Date(s.stepStartedAt).getTime())/1e3));c[a]=(c[a]??0)+I,d=i;}let p=Math.max(0,Math.round((n.getTime()-new Date(d).getTime())/1e3)),m=Math.max(0,Math.round((n.getTime()-new Date(l).getTime())/1e3)),g=C(e),f=Q(t)??{},h={checkedAt:i,source:r,message:g,details:ke(o,f),startedAt:l,currentStep:u,stepStartedAt:d,stepElapsedSec:p,totalElapsedSec:m,stepDurationsSec:c};await w.writeFile(T,JSON.stringify(h,null,2),"utf8");}).catch(()=>{}));}async function Le(r,e){await w.mkdir(y.dirname(r),{recursive:true}),await w.appendFile(r,`${JSON.stringify(R(e))}
1
+ import w from'fs/promises';import ee from'http';import te from'https';import y from'path';import {CloudFrontClient,waitUntilInvalidationCompleted,CreateInvalidationCommand}from'@aws-sdk/client-cloudfront';import {createHash}from'crypto';function j(r){let e=r.trim();return e==="."?".":e.replace(/\/$/,"")}function re(r){let e=String(r??"").trim();if(!e)return "";let t=e;if(/^https?:\/\//i.test(t))try{t=new URL(t).pathname;}catch{return ""}if(t=t.split(/[?#]/,1)[0]?.replace(/\\/g,"/").trim()??"",!t)return "";let n=t.endsWith("/"),i=t.replace(/^\/+/,"").split("/").map(o=>o.replace(/[^A-Za-z0-9._-]/g,"")).filter(o=>o.length>0&&o!=="."&&o!=="..");if(i.length===0)return "";let s=`/${i.join("/")}`;return n&&y.extname(s)===""&&(s+="/"),s==="/"?"":s}function k(r){return !r||typeof r!="object"?{}:Object.fromEntries(Object.entries(r).map(([e,t])=>[e.trim(),String(t??"")]).filter(([e])=>e.length>0))}function ie(r){if(!r||typeof r!="object")return {};let e={};for(let[t,n]of Object.entries(r)){let i=t.trim();if(!i||!n||typeof n!="object")continue;let s=n,o={};if(typeof s.targetOrigin=="string"){let a=j(s.targetOrigin);a&&(o.targetOrigin=a);}let u=k(s.extraReplacements);if(Object.keys(u).length>0&&(o.extraReplacements=u),s.s3&&typeof s.s3=="object"){let a={},l=s.s3;typeof l.bucket=="string"&&(a.bucket=l.bucket.trim()),typeof l.prefix=="string"&&(a.prefix=l.prefix.trim()),typeof l.region=="string"&&(a.region=l.region.trim()),typeof l.htmlCacheControl=="string"&&(a.htmlCacheControl=l.htmlCacheControl.trim()),typeof l.assetCacheControl=="string"&&(a.assetCacheControl=l.assetCacheControl.trim()),Object.keys(a).length>0&&(o.s3=a);}if(s.cloudFront&&typeof s.cloudFront=="object"){let a={},l=s.cloudFront;typeof l.distributionId=="string"&&(a.distributionId=l.distributionId.trim()),Array.isArray(l.invalidationPaths)&&(a.invalidationPaths=l.invalidationPaths.map(c=>String(c??"").trim()).filter(c=>c.length>0)),Object.keys(a).length>0&&(o.cloudFront=a);}e[i]=o;}return e}function L(r){return r==="PROFESSIONAL"||r==="AGENCY"?r:void 0}function ne(r,e){if(!r||typeof r!="object")return null;let t=r,n=t.command;if(n!=="publish"&&n!=="crawl"&&n!=="deploy"&&n!=="invalidate"&&n!=="retry-timeouts"&&n!=="url"&&n!=="content-sync")return null;let i=Number.parseInt(String(t.intervalMinutes??"0"),10);if(!Number.isFinite(i)||i<1)return null;let s=String(t.id??`${n}-${e+1}`).trim();if(!s)return null;let o=String(t.deploymentProfile??"").trim(),u=String(t.url??"").trim(),a=Array.isArray(t.postTypes)?[...new Set(t.postTypes.map(c=>String(c??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],l=Array.isArray(t.listingPaths)?[...new Set(t.listingPaths.map(c=>String(c??"").trim()).filter(Boolean))]:[];return n==="content-sync"&&a.length===0?null:{id:s,enabled:t.enabled!==false,command:n,intervalMinutes:i,...n==="publish"||n==="crawl"?{crawlMode:t.crawlMode==="incremental"?"incremental":"full"}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&o?{deploymentProfile:o}:{},...u?{url:u}:{},...a.length>0?{postTypes:a}:{},...l.length>0?{listingPaths:l}:{},...typeof t.includeSubsites=="boolean"?{includeSubsites:t.includeSubsites}:{},...typeof t.includePostTypeArchives=="boolean"?{includePostTypeArchives:t.includePostTypeArchives}:{},...typeof t.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:t.includeTaxonomyArchives}:{},...typeof t.includeAuthorArchives=="boolean"?{includeAuthorArchives:t.includeAuthorArchives}:{},...typeof t.includeDateArchives=="boolean"?{includeDateArchives:t.includeDateArchives}:{},...typeof t.includePostsPage=="boolean"?{includePostsPage:t.includePostsPage}:{},...typeof t.includeSitemapChain=="boolean"?{includeSitemapChain:t.includeSitemapChain}:{}}}function U(r){let e=r&&typeof r=="object"?r:{},t=Array.isArray(e.rules)?e.rules.map((n,i)=>ne(n,i)).filter(n=>!!n):[];return {enabled:e.enabled===true,timezone:typeof e.timezone=="string"&&e.timezone.trim()!==""?e.timezone.trim():"UTC",rules:t}}function se(r){if(!r||typeof r!="object")return;let e=r,t={};for(let i of ["accountId","siteId"]){let s=e[i];typeof s=="string"&&s.trim()!==""&&(t[i]=s.trim());}let n=Number(e.lastUpdate??0);return Number.isFinite(n)&&n>0&&(t.lastUpdate=Math.floor(n)),e.subscriber===true&&(t.subscriber=true),Object.keys(t).length>0?t:void 0}function x(r){let e=r&&typeof r=="object"?r:{},t=String(e.accountId??"").trim(),n=String(e.siteId??"").trim(),i=e.subscriber===true,s=se(e.siteSettings),o={...s??{},...t?{accountId:s?.accountId??t}:{},...n?{siteId:s?.siteId??n}:{},...s?.subscriber===true||i?{subscriber:true}:{}},u={},a=String(e.apiBase??"").trim();a&&(u.apiBase=a);let l=String(e.runtimeToken??e.nonce??"").trim();l&&(u.runtimeToken=l);let c=String(e.virtualAssetBaseUrl??e.uploadUrl??"").trim();c&&(u.virtualAssetBaseUrl=c),Object.keys(o).length>0&&(u.siteSettings=o);let d=L(e.subscriptionType);return d&&(u.subscriptionType=d),u}function oe(r){try{let e=new URL(r);globalThis.location=e;}catch{}}function $(r){return typeof r=="string"?r:r instanceof URL?r.toString():r.url}function ae(r,e){let t=new Headers(r instanceof Request?r.headers:void 0);if(e?.headers)for(let[n,i]of new Headers(e.headers).entries())t.set(n,i);return t}async function _(r,e,t=5){let n=new URL($(r)),i=String(e?.method??(r instanceof Request?r.method:"GET")).toUpperCase(),s=n.protocol==="http:"?ee:te,o=ae(r,e);return await new Promise((u,a)=>{let l=s.request(n,{method:i,headers:Object.fromEntries(o.entries()),...n.protocol==="https:"?{rejectUnauthorized:false}:{}},c=>{let d=[];c.on("data",g=>{d.push(Buffer.isBuffer(g)?g:Buffer.from(g));}),c.on("error",a),c.on("end",()=>{let g=c.statusCode??0,m=c.headers.location;if(t>0&&m&&[301,302,303,307,308].includes(g)){u(_(new URL(m,n),{method:g===303?"GET":i},t-1));return}let p=new Headers;for(let[f,h]of Object.entries(c.headers))Array.isArray(h)?p.set(f,h.join(", ")):typeof h=="string"&&p.set(f,h);u(new Response(Buffer.concat(d),{status:g,statusText:c.statusMessage??"",headers:p}));});});l.on("error",a),l.end();})}function le(r){let e=x(r.wpsuite),t={...e.siteSettings??{},...e.siteSettings?.subscriber===true||e.subscriptionType==="PROFESSIONAL"||e.subscriptionType==="AGENCY"?{subscriber:true}:{}},n=String(t.accountId??"").trim(),i=String(t.siteId??"").trim(),s=String(e.virtualAssetBaseUrl??e.uploadUrl??"").trim();if(r.wpsuite={...e.apiBase?{apiBase:e.apiBase}:{},...e.runtimeToken?{runtimeToken:e.runtimeToken}:{},...s?{virtualAssetBaseUrl:s}:{},...Object.keys(t).length>0?{siteSettings:t}:{},...e.subscriptionType?{subscriptionType:e.subscriptionType}:{}},!n||!i||!s)return false;oe(r.sourceOrigin);let o=globalThis,a={...o.WpSuite??{},siteSettings:t,uploadUrl:s};return e.apiBase&&(a.apiBase=e.apiBase),o.WpSuite=a,true}async function ue(r){let e=String(r.wpsuite?.virtualAssetBaseUrl??r.wpsuite?.uploadUrl??"").trim();if(!le(r)){let i=r.wpsuite?.siteSettings,s=[String(i?.accountId??"").trim()?"":"accountId",String(i?.siteId??"").trim()?"":"siteId",e?"":"virtualAssetBaseUrl"].filter(Boolean);return {remote:null,diagnostic:{status:"invalid-runtime",virtualAssetBaseUrl:e,subscriptionType:null,error:`Missing WP Suite runtime field(s): ${s.join(", ")}.`,requests:[]}}}let t=globalThis.fetch,n=[];try{typeof t=="function"&&/^https?:\/\//i.test(e)&&(globalThis.fetch=(async(a,l)=>{let c=$(a);if(c.startsWith(e)){let d=(()=>{try{return new URL(c).pathname}catch{return c}})();try{let g=r.ignoreHttpsErrors&&c.startsWith("https://")?await _(a,l):await t(a,l);return n.push({path:d,status:g.status,ok:g.ok,error:null}),g}catch(g){throw n.push({path:d,status:null,ok:!1,error:g instanceof Error?g.message:String(g)}),g}}return t(a,l)}));let{getConfig:i}=await import('@smart-cloud/wpsuite-core'),s=await i("publisher"),o=s&&typeof s=="object"?s:null,u=L(o?.subscriptionType);return {remote:o,diagnostic:{status:o?"loaded":"unavailable",virtualAssetBaseUrl:e,subscriptionType:u??null,error:o?null:"WP Suite publisher config loader returned no configuration.",requests:n}}}catch(i){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:e,subscriptionType:null,error:i instanceof Error?i.message:String(i),requests:n}}}finally{typeof t=="function"&&(globalThis.fetch=t);}}function ce(r,e){let t=ie(e?.deploymentProfiles),n=String(e?.defaultDeploymentProfile??"").trim(),i=t[n]?n:"",s=L(e?.subscriptionType),o=x(r.wpsuite);return {...r,scheduler:U(e?.scheduler),deploymentProfiles:t,defaultDeploymentProfile:i,...s?{subscriptionType:s}:{},wpsuite:{...o,...o.siteSettings||s?{siteSettings:{...o.siteSettings??{},...o.siteSettings?.subscriber===true||s==="PROFESSIONAL"||s==="AGENCY"?{subscriber:true}:{}}}:{},...s?{subscriptionType:s}:{}}}}function E(r,e){let n=String(r||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!n)return e;let i=n.split("/").map(s=>s.trim()).filter(s=>s.length>0&&s!=="."&&s!=="..");return i.length>0?i.join("/"):e}function pe(){let r=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return r.trim()?y.resolve(r):""}function N(r,e,t){let n=String(e||"").trim();return n&&y.isAbsolute(n)?y.resolve(n):y.resolve(r,E(n,t))}async function D(r,e={}){let t=String("").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",n=await w.readFile(t,"utf8"),i=JSON.parse(n);i.sourceOrigin=i.sourceOrigin.replace(/\/$/,""),i.targetOrigin=j(i.targetOrigin),i.ignoreHttpsErrors??=false,i.outputDir=String(i.outputDir||"export").trim()||"export",i.urlRewriteMode||=i.targetOrigin==="."?"relative":"absolute",i.noJavaScriptRenderPathPrefixes||=[],i.seedPaths||=[],i.generated404RequestPath=re(i.generated404RequestPath),i.sitemapPaths||=["/sitemap_index.xml","/sitemap.xml"],i.allowedAssetHosts||=[],i.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],i.blockedPathPrefixes||=[],i.blockedSearchFragments||=[],i.concurrency||=1,i.maxPages||=0,i.extraReplacements=k(i.extraReplacements),i.postCrawlCopyMap=k(i.postCrawlCopyMap),i.logDir=String(i.logDir||"logs").trim()||"logs",i.verbose??=false,i.logLevel||=i.verbose?"debug":"info",i.s3SyncMode||="sdk-upload-delete",i.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},i.readiness.timeoutMs??=1500,i.readiness.fallbackWaitMs??=1500,i.viewport||={width:1440,height:1200},i.navigationTimeoutMs||=3e4,i.assetDownloadConcurrency=Number(i.assetDownloadConcurrency)>0?Number(i.assetDownloadConcurrency):i.concurrency,i.rewriteConcurrency=Number(i.rewriteConcurrency)>0?Number(i.rewriteConcurrency):i.assetDownloadConcurrency,i.wpsuite=x(i.wpsuite),i.scheduler=U(void 0),i.deploymentProfiles={},i.defaultDeploymentProfile="",i.deploymentTargetOverride=String(i.deploymentTargetOverride??"").trim();let s=await ue(i);e.onRemotePublisherConfigDiagnostic?.(s.diagnostic);let o=ce(i,s.remote),u=pe(),a=u?y.resolve(u,".."):"";return a?(o.outputDir=N(a,o.outputDir,"export"),o.logDir=N(a,o.logDir,"logs")):(y.isAbsolute(o.outputDir)||(o.outputDir=E(o.outputDir,"export")),y.isAbsolute(o.logDir)||(o.logDir=E(o.logDir,"logs"))),o}function ge(r){let e=r/6e4;if(Number.isInteger(e)&&e>=1)return `${e} minute${"s"}`;let t=Math.ceil(r/1e3);return `${t} second${t===1?"":"s"}`}async function M(r,e,t){let n=new AbortController,i=globalThis.setTimeout(()=>{n.abort(new Error(`${r} timed out after ${ge(e)}.`));},e);try{return await t({abortController:n,httpOptions:{abortSignal:n.signal,requestTimeout:e}})}finally{globalThis.clearTimeout(i);}}function me(r){let e=r;return e.endsWith("/")?e+="index.html":y.posix.basename(e).includes(".")||(e+="/index.html"),e}function q(r,e){let t=new Set;for(let n of r){let i=new URL(n,"https://content-sync.invalid");if(i.origin!=="https://content-sync.invalid")continue;let s=`${i.pathname}${i.search}`;t.add(s),t.add(`${me(i.pathname)}${i.search}`);}return [...t].sort()}var fe=".deploy-plan.json",ye="deploy-plan.json";function Pe(){let r=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return r.trim()?y.resolve(r):""}function be(r){let e=String(r||"").replace(/\\/g,"/").replace(/^\/+/,"").trim(),t=y.posix.normalize(e);return !e||!t||t==="."||/^[a-zA-Z]:/.test(t)||t.startsWith("../")||t===".."?null:t}function we(r){let e=String(r||"").trim();if(!e.startsWith("/")||e.startsWith("//")||e.includes("*")||e.includes("#"))return null;try{let t=new URL(e,"https://content-sync.invalid");return t.origin!=="https://content-sync.invalid"?null:`${y.posix.normalize(t.pathname)}${t.search}`}catch{return null}}function b(r){return [...new Set(r.map(be).filter(e=>e!==null))]}function Se(r,e){let t={generatedAt:String(e.generatedAt||new Date().toISOString()),outputDir:y.resolve(r.outputDir),runMode:e.runMode,crawlMode:e.crawlMode,fullSyncRequired:!!e.fullSyncRequired,changedFiles:b(e.changedFiles||[]),deletedFiles:b(e.deletedFiles||[]),rewriteTargets:b(e.rewriteTargets||[])};return e.schemaVersion===2?{...t,schemaVersion:2,runMode:"content-sync",crawlMode:"incremental",fullSyncRequired:false,consumerId:String(e.consumerId||"").trim(),fromSequence:Math.max(0,Number(e.fromSequence)||0),toSequence:Math.max(0,Number(e.toSequence)||0),impactHash:String(e.impactHash||"").trim(),uploadGroups:{assets:b(e.uploadGroups?.assets||[]),content:b(e.uploadGroups?.content||[]),listings:b(e.uploadGroups?.listings||[]),sitemapLeaves:b(e.uploadGroups?.sitemapLeaves||[]),sitemapIndexes:b(e.uploadGroups?.sitemapIndexes||[])},invalidationPaths:[...new Set((e.invalidationPaths||[]).map(we).filter(n=>n!==null))].sort()}:{schemaVersion:1,...t,runMode:e.runMode}}function ve(r){if(!r||typeof r!="object")return false;let e=r;if(e.schemaVersion===2){let t=e;return t.runMode==="content-sync"&&t.crawlMode==="incremental"&&t.fullSyncRequired===false&&typeof t.generatedAt=="string"&&typeof t.outputDir=="string"&&typeof t.consumerId=="string"&&t.consumerId.trim()!==""&&Number.isSafeInteger(t.fromSequence)&&Number.isSafeInteger(t.toSequence)&&Number(t.toSequence)>=Number(t.fromSequence)&&typeof t.impactHash=="string"&&/^[a-f0-9]{64}$/.test(t.impactHash)&&Array.isArray(t.changedFiles)&&Array.isArray(t.deletedFiles)&&Array.isArray(t.rewriteTargets)&&!!t.uploadGroups&&Array.isArray(t.uploadGroups.assets)&&Array.isArray(t.uploadGroups.content)&&Array.isArray(t.uploadGroups.listings)&&Array.isArray(t.uploadGroups.sitemapLeaves)&&Array.isArray(t.uploadGroups.sitemapIndexes)&&Array.isArray(t.invalidationPaths)}return e.schemaVersion===1&&typeof e.generatedAt=="string"&&typeof e.outputDir=="string"&&(e.runMode==="full"||e.runMode==="retry-timeouts"||e.runMode==="single-url")&&(e.crawlMode==="full"||e.crawlMode==="incremental")&&typeof e.fullSyncRequired=="boolean"&&Array.isArray(e.changedFiles)&&Array.isArray(e.deletedFiles)&&Array.isArray(e.rewriteTargets)}function Ie(r){let e=y.join(y.resolve(r.outputDir),fe),t=Pe();return t?[y.join(t,ye),e]:[e]}async function W(r){let e=y.resolve(r.outputDir);for(let t of Ie(r))try{let n=await w.readFile(t,"utf8"),i=JSON.parse(n);if(!ve(i)||y.resolve(i.outputDir)!==e)continue;return Se(r,i)}catch{}return null}function Ce(r){return typeof r.subscriptionType=="string"&&r.subscriptionType.trim()!==""}function B(r){let e="";for(let n=0;n<r.length;n++){let i=r[n];if(i==="--profile"){let s=(r[n+1]||"").trim();if(!s)throw new Error("Missing value for --profile.");e=s,n+=1;}else if(i.startsWith("--profile=")){let s=i.slice(10).trim();if(!s)throw new Error("Missing value for --profile.");e=s;}}let t=String(process.env.PUBLISHER_DEPLOY_PROFILE||process.env.PUBLISHER_DEPLOYMENT_PROFILE||"").trim();return e||t||null}function De(r,e){return {...r,...e??{}}}function Te(r,e){return {...r,...e??{},invalidationPaths:[...(e?.invalidationPaths??r.invalidationPaths)||[]]}}function H(r,e){let t=String(e??r.deploymentTargetOverride??"").trim();if(!Ce(r)){if(t)throw new Error(`Deployment target "${t}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:r}}let n=String(t||r.defaultDeploymentProfile||"").trim();if(!n)return {name:null,profile:null,config:r};let i=r.deploymentProfiles?.[n];if(!i)throw new Error(`Unknown deployment profile "${n}". Check the linked WPSuite publisher configuration.`);return {name:n,profile:i,config:{...r,targetOrigin:i.targetOrigin??r.targetOrigin,s3:De(r.s3,i.s3),cloudFront:Te(r.cloudFront,i.cloudFront)}}}var z={error:0,warn:1,info:2,debug:3},Re=/^(?:authorization|proxyAuthorization|runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken|xStaticPublisherToken|xApiKey)$/i;function C(r){return r.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 R(r){return typeof r=="string"?C(r):Array.isArray(r)?r.map(e=>R(e)):r instanceof Error?{name:r.name,message:C(r.message),...r.stack?{stack:C(r.stack)}:{}}:r&&typeof r=="object"?Object.fromEntries(Object.entries(r).map(([e,t])=>[e,Re.test(e)?"[REDACTED]":R(t)])):r}function Q(r){return r?R(r):void 0}var J=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",T=J?y.join(J,"current-progress.json"):"",G=Promise.resolve();function V(r){return r.includes("crawl")?"crawl":r.includes("deploy")?"deploy":r.includes("invalidate")?"invalidate":r}function Ae(r,e){let t={...r},n=new Set(["pagesQueued","pagesRendered","assetsQueued","sitemapsQueued","pagesDiscovered","assetsDiscovered","sitemapsDiscovered","pagesSaved","assetsSaved","donePages","doneAssets","doneSitemaps","changedTextFiles","uploaded","failed","index","totalFiles","pageQueue","assetQueue","sitemapQueue"]);for(let[i,s]of Object.entries(e)){let o=t[i];if(typeof o=="number"&&typeof s=="number"&&n.has(i)){t[i]=Math.max(o,s);continue}t[i]=s;}return t}function ke(r,e,t){T&&(G=G.then(async()=>{let n=new Date,i=n.toISOString();await w.mkdir(y.dirname(T),{recursive:true});let s=await w.readFile(T,"utf8").then(I=>JSON.parse(I)).catch(()=>null),o=s&&typeof s.details=="object"&&s.details?s.details:{},u=V(r),a=s?.currentStep||V(s?.source||""),l=s?.startedAt||i,c={...s?.stepDurationsSec??{}},d=s?.stepStartedAt||i;if(a&&a!==u&&s?.stepStartedAt){let I=Math.max(0,Math.round((n.getTime()-new Date(s.stepStartedAt).getTime())/1e3));c[a]=(c[a]??0)+I,d=i;}let g=Math.max(0,Math.round((n.getTime()-new Date(d).getTime())/1e3)),m=Math.max(0,Math.round((n.getTime()-new Date(l).getTime())/1e3)),p=C(e),f=Q(t)??{},h={checkedAt:i,source:r,message:p,details:Ae(o,f),startedAt:l,currentStep:u,stepStartedAt:d,stepElapsedSec:g,totalElapsedSec:m,stepDurationsSec:c};await w.writeFile(T,JSON.stringify(h,null,2),"utf8");}).catch(()=>{}));}async function Ee(r,e){await w.mkdir(y.dirname(r),{recursive:true}),await w.appendFile(r,`${JSON.stringify(R(e))}
2
2
  `,"utf8");}var v=class{constructor(e,t,n="info"){this.logDir=e;this.logFile=t;this.level=n;this.ensureLogFileReady();}logDir;logFile;level;startedAt=Date.now();marks=new Map;initPromise=null;writeQueue=Promise.resolve();writeError=null;get logPath(){return y.join(this.logDir,this.logFile)}artifactPath(e){return this.logFile.endsWith(".log.jsonl")?y.join(this.logDir,this.logFile.replace(/\.log\.jsonl$/u,`${e}.jsonl`)):y.join(this.logDir,`${this.logFile}${e}.jsonl`)}get errorsPath(){return this.artifactPath(".errors")}get rejectedPath(){return this.artifactPath(".rejected")}ensureLogFileReady(){return this.initPromise||(this.initPromise=(async()=>{await w.mkdir(this.logDir,{recursive:true}),await w.writeFile(this.logPath,"","utf8");})()),this.initPromise}enqueueTask(e){this.writeQueue=this.writeQueue.then(e).catch(t=>{this.writeError=t instanceof Error?t:new Error(String(t));});}enqueueLine(e){this.enqueueTask(async()=>{await this.ensureLogFileReady(),await w.appendFile(this.logPath,`${e}
3
- `,"utf8");});}enqueueJsonLine(e,t){this.enqueueTask(()=>Le(e,t));}accepts(e){return z[e]<=z[this.level]}push(e,t,n){if(!this.accepts(e))return;let i=C(t),s=Q(n),o=JSON.stringify({time:new Date().toISOString(),level:e,message:i,...s||{}});this.enqueueLine(o),e==="error"?console.error(`[ERROR] ${i}`,s?JSON.stringify(s):""):e==="warn"?console.warn(`[WARN] ${i}`,s?JSON.stringify(s):""):console.log(`[${e.toUpperCase().padEnd(5)}] ${i}`);}info(e,t){this.push("info",e,t);}progress(e,t){Ee(this.logFile,e,t);}debug(e,t){this.push("debug",e,t);}warn(e,t){this.push("warn",e,t);}reject(e,t,n,i,s){let o={kind:e,url:t,reason:n,...i?{source:i}:{},...s||{}};this.enqueueJsonLine(this.rejectedPath,o),this.accepts("debug")&&this.push("debug",`Rejected ${e} ${t}: ${n}`,o);}error(e,t){this.enqueueJsonLine(this.errorsPath,{message:e,...t||{}}),this.push("error",e,t);}mark(e){this.marks.set(e,Date.now());}endMark(e,t){let n=this.marks.get(e);if(!n)return;let i=Date.now()-n,s=Number((i/1e3).toFixed(2));this.push("info",`Timing ${e}: ${s}s`,{name:e,ms:i,seconds:s,...t||{}});}async flush(){let e=Date.now()-this.startedAt,t=Number((e/1e3).toFixed(2));if(this.push("info",`Total time: ${t}s`,{name:"total",ms:e,seconds:t}),await this.writeQueue,this.writeError)throw this.writeError}};function Oe(){let r=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();return r?y.join(y.resolve(r),"content-sync-invalidation.json"):""}async function F(r,e){if(!r)return;await w.mkdir(y.dirname(r),{recursive:true});let t=`${r}.${process.pid}.${Date.now()}.tmp`;await w.writeFile(t,JSON.stringify({...e,updatedAt:new Date().toISOString()},null,2),"utf8"),await w.rename(t,r);}async function Ne(r,e,t,n){if(!r)return null;try{let i=JSON.parse(await w.readFile(r,"utf8"));if(i.schemaVersion!==1||i.distributionId!==e||i.impactHash!==t||!Array.isArray(i.batches))throw new Error("Content-sync invalidation state does not match the deploy plan.");let s=i.batches.flatMap(o=>o.paths);return s.length!==n.length||s.some((o,u)=>o!==n[u])?null:i}catch(i){if(i.code==="ENOENT")return null;throw i}}function je(r,e=3e3){let t=[];for(let n=0;n<r.length;n+=e)t.push(r.slice(n,n+e));return t}async function Z(r=process.argv.slice(2)){let e=await D(),t=B(r),n=H(e,t),i=n.config,s=new v(i.logDir||"logs","invalidate.log.jsonl",i.logLevel??"info"),o=r.includes("--content-sync"),u=await W(e);if(o&&u?.schemaVersion!==2)throw new Error("Content-sync invalidation requires a valid schema-2 targeted deploy plan.");if(n.name&&s.info(`Using deployment profile: ${n.name}`),!i.cloudFront.distributionId){s.info("No CloudFront distributionId configured; skipping invalidation."),await s.flush();return}let a=u?.schemaVersion===2?u.invalidationPaths:i.cloudFront.invalidationPaths.length>0?i.cloudFront.invalidationPaths:["/*"],l=u?.schemaVersion===2?q(a,i.s3.prefix||""):a;if(u?.schemaVersion===2&&l.some(g=>g==="/*"))throw new Error("Content-sync cannot use a whole-distribution invalidation.");if(l.length===0){s.info("The targeted deploy plan has no CloudFront paths to invalidate."),await s.flush();return}let c=i.cloudFront.distributionId,d=new CloudFrontClient({region:"us-east-1",maxAttempts:5});if(s.mark("invalidate"),u?.schemaVersion!==2){let g=`smartcloud-static-publisher-${Date.now()}`;await M(`CloudFront invalidation for distribution ${c}`,3e5,({httpOptions:f})=>d.send(new CreateInvalidationCommand({DistributionId:c,InvalidationBatch:{CallerReference:g,Paths:{Quantity:l.length,Items:l}}}),f)),s.endMark("invalidate"),await s.flush();return}let p=Oe(),m=await Ne(p,c,u.impactHash,l);if(!m){let g=createHash("sha256").update(JSON.stringify(l)).digest("hex");m={schemaVersion:1,distributionId:c,impactHash:u.impactHash,updatedAt:new Date().toISOString(),batches:je(l).map((f,h)=>({index:h,callerReference:`wpsuite-content-sync-${u.impactHash.slice(0,24)}-${g.slice(0,24)}-${h}`,paths:f,invalidationId:null,status:"pending"}))},await F(p,m);}for(let g of m.batches){if(!g.invalidationId){let f=await M(`CloudFront targeted invalidation batch ${g.index+1}`,3e5,({httpOptions:I})=>d.send(new CreateInvalidationCommand({DistributionId:c,InvalidationBatch:{CallerReference:g.callerReference,Paths:{Quantity:g.paths.length,Items:g.paths}}}),I)),h=String(f.Invalidation?.Id||"").trim();if(!h)throw new Error("CloudFront did not return an invalidation ID.");g.invalidationId=h,g.status="created",await F(p,m);}await waitUntilInvalidationCompleted({client:d,maxWaitTime:900,minDelay:2,maxDelay:15},{DistributionId:c,Id:g.invalidationId}),g.status="completed",await F(p,m),s.info(`CloudFront invalidation batch ${g.index+1}/${m.batches.length} completed.`,{invalidationId:g.invalidationId,pathCount:g.paths.length});}s.endMark("invalidate"),await s.flush();}Z().catch(async r=>{console.error(r);try{let e=await D().catch(()=>null),t=new v(e?.logDir??"logs","invalidate.log.jsonl","debug");t.error(`Unhandled error: ${r instanceof Error?r.message:String(r)}`,r instanceof Error?{stack:r.stack}:void 0),await t.flush();}catch{}process.exit(1);});
3
+ `,"utf8");});}enqueueJsonLine(e,t){this.enqueueTask(()=>Ee(e,t));}accepts(e){return z[e]<=z[this.level]}push(e,t,n){if(!this.accepts(e))return;let i=C(t),s=Q(n),o=JSON.stringify({time:new Date().toISOString(),level:e,message:i,...s||{}});this.enqueueLine(o),e==="error"?console.error(`[ERROR] ${i}`,s?JSON.stringify(s):""):e==="warn"?console.warn(`[WARN] ${i}`,s?JSON.stringify(s):""):console.log(`[${e.toUpperCase().padEnd(5)}] ${i}`);}info(e,t){this.push("info",e,t);}progress(e,t){ke(this.logFile,e,t);}debug(e,t){this.push("debug",e,t);}warn(e,t){this.push("warn",e,t);}reject(e,t,n,i,s){let o={kind:e,url:t,reason:n,...i?{source:i}:{},...s||{}};this.enqueueJsonLine(this.rejectedPath,o),this.accepts("debug")&&this.push("debug",`Rejected ${e} ${t}: ${n}`,o);}error(e,t){this.enqueueJsonLine(this.errorsPath,{message:e,...t||{}}),this.push("error",e,t);}mark(e){this.marks.set(e,Date.now());}endMark(e,t){let n=this.marks.get(e);if(!n)return;let i=Date.now()-n,s=Number((i/1e3).toFixed(2));this.push("info",`Timing ${e}: ${s}s`,{name:e,ms:i,seconds:s,...t||{}});}async flush(){let e=Date.now()-this.startedAt,t=Number((e/1e3).toFixed(2));if(this.push("info",`Total time: ${t}s`,{name:"total",ms:e,seconds:t}),await this.writeQueue,this.writeError)throw this.writeError}};function Fe(){let r=String(process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"").trim();return r?y.join(y.resolve(r),"content-sync-invalidation.json"):""}async function F(r,e){if(!r)return;await w.mkdir(y.dirname(r),{recursive:true});let t=`${r}.${process.pid}.${Date.now()}.tmp`;await w.writeFile(t,JSON.stringify({...e,updatedAt:new Date().toISOString()},null,2),"utf8"),await w.rename(t,r);}async function Oe(r,e,t,n){if(!r)return null;try{let i=JSON.parse(await w.readFile(r,"utf8"));if(i.schemaVersion!==1||i.distributionId!==e||i.impactHash!==t||!Array.isArray(i.batches))throw new Error("Content-sync invalidation state does not match the deploy plan.");let s=i.batches.flatMap(o=>o.paths);return s.length!==n.length||s.some((o,u)=>o!==n[u])?null:i}catch(i){if(i.code==="ENOENT")return null;throw i}}function Ne(r,e=3e3){let t=[];for(let n=0;n<r.length;n+=e)t.push(r.slice(n,n+e));return t}async function Z(r=process.argv.slice(2)){let e=await D(),t=B(r),n=H(e,t),i=n.config,s=new v(i.logDir||"logs","invalidate.log.jsonl",i.logLevel??"info"),o=r.includes("--content-sync"),u=await W(e);if(o&&u?.schemaVersion!==2)throw new Error("Content-sync invalidation requires a valid schema-2 targeted deploy plan.");if(n.name&&s.info(`Using deployment profile: ${n.name}`),!i.cloudFront.distributionId){s.info("No CloudFront distributionId configured; skipping invalidation."),await s.flush();return}let a=u?.schemaVersion===2?u.invalidationPaths:i.cloudFront.invalidationPaths.length>0?i.cloudFront.invalidationPaths:["/*"],l=u?.schemaVersion===2?q(a,i.s3.prefix||""):a;if(u?.schemaVersion===2&&l.some(p=>p==="/*"))throw new Error("Content-sync cannot use a whole-distribution invalidation.");if(l.length===0){s.info("The targeted deploy plan has no CloudFront paths to invalidate."),await s.flush();return}let c=i.cloudFront.distributionId,d=new CloudFrontClient({region:"us-east-1",maxAttempts:5});if(s.mark("invalidate"),u?.schemaVersion!==2){let p=`smartcloud-static-publisher-${Date.now()}`;await M(`CloudFront invalidation for distribution ${c}`,3e5,({httpOptions:f})=>d.send(new CreateInvalidationCommand({DistributionId:c,InvalidationBatch:{CallerReference:p,Paths:{Quantity:l.length,Items:l}}}),f)),s.endMark("invalidate"),await s.flush();return}let g=Fe(),m=await Oe(g,c,u.impactHash,l);if(!m){let p=createHash("sha256").update(JSON.stringify(l)).digest("hex");m={schemaVersion:1,distributionId:c,impactHash:u.impactHash,updatedAt:new Date().toISOString(),batches:Ne(l).map((f,h)=>({index:h,callerReference:`wpsuite-content-sync-${u.impactHash.slice(0,24)}-${p.slice(0,24)}-${h}`,paths:f,invalidationId:null,status:"pending"}))},await F(g,m);}for(let p of m.batches){if(!p.invalidationId){let f=await M(`CloudFront targeted invalidation batch ${p.index+1}`,3e5,({httpOptions:I})=>d.send(new CreateInvalidationCommand({DistributionId:c,InvalidationBatch:{CallerReference:p.callerReference,Paths:{Quantity:p.paths.length,Items:p.paths}}}),I)),h=String(f.Invalidation?.Id||"").trim();if(!h)throw new Error("CloudFront did not return an invalidation ID.");p.invalidationId=h,p.status="created",await F(g,m);}await waitUntilInvalidationCompleted({client:d,maxWaitTime:900,minDelay:2,maxDelay:15},{DistributionId:c,Id:p.invalidationId}),p.status="completed",await F(g,m),s.info(`CloudFront invalidation batch ${p.index+1}/${m.batches.length} completed.`,{invalidationId:p.invalidationId,pathCount:p.paths.length});}s.endMark("invalidate"),await s.flush();}Z().catch(async r=>{console.error(r);try{let e=await D().catch(()=>null),t=new v(e?.logDir??"logs","invalidate.log.jsonl","debug");t.error(`Unhandled error: ${r instanceof Error?r.message:String(r)}`,r instanceof Error?{stack:r.stack}:void 0),await t.flush();}catch{}process.exit(1);});
@@ -1,3 +1,3 @@
1
- import {pathToFileURL}from'url';import v from'fs/promises';import {existsSync,createReadStream,createWriteStream}from'fs';import m from'path';import {spawn}from'child_process';import {randomUUID,createHash}from'crypto';import je from'http';import pe from'https';import {pipeline}from'stream/promises';import {createGzip}from'zlib';function ce(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function W(e){return Array.isArray(e)?e.map(W):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>[t,W(n)])):e}function ke(e){return JSON.stringify(W(e))}function F(e){return createHash("sha256").update(ke(e)).digest("hex")}function le(e){return F({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 Ee(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 B(e){let t=ce((e.postTypes??[]).map(i=>String(i).toLowerCase().replace(/[^a-z0-9_-]/g,"")));if(t.length===0)throw new Error("Content-sync requires at least one public post type.");let n=ce((e.listingPaths??[]).map(i=>{let r=String(i).trim();if(!r)return "";let a=new URL(r,"https://content-sync.invalid/");if(a.origin!=="https://content-sync.invalid"&&/^https?:\/\//i.test(r))throw new Error("Content-sync listing routes must be same-site paths.");let s=`/${a.pathname.replace(/^\/+|\/+$/g,"")}`;return s==="/"?"/":`${s}/`}));return {postTypes:t,listingPaths:n,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 N(e,t,n){return F({schemaVersion:1,sourceOrigin:Ee(e),ruleId:t.trim(),scope:B(n)})}function K(e){return F({schemaVersion:1,...e})}function ge(e){return e.replace(/((?:x-static-publisher-token|authorization|proxy-authorization|x-api-key|aws-access-key-id|aws-secret-access-key|aws-session-token)\s*:\s*)[^\r\n]+/gi,"$1[REDACTED]").replace(/\b(Basic|Bearer)\s+[^\s]+/gi,"$1 [REDACTED]").replace(/([?&](?:token|access_token|refresh_token|api_key|key|nonce)=)[^&\s]+/gi,"$1[REDACTED]").replace(/((?:"|')?(?:runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken)(?:"|')?\s*[:=]\s*(?:"|'))[^"']+/gi,"$1[REDACTED]")}var de=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";de?m.join(de,"current-progress.json"):"";Promise.resolve();var Je="wp-json/smartcloud-static-publisher/v1/content-sync/",Oe=25*1024*1024,k=class extends Error{status;code;constructor(t,n,i){super(i),this.name="ContentSyncApiError",this.status=t,this.code=n;}},M=class{options;constructor(t){if(!t.runtimeToken.trim())throw new Error("Content-sync requires a WordPress runtime token.");this.options={...t,timeoutMs:t.timeoutMs??6e4};}endpoint(t){let n=this.options.sourceOrigin.replace(/\/+$/,"")+"/";return new URL(`${Je}${t}`,n)}async post(t,n){let i=this.endpoint(t),r=Buffer.from(JSON.stringify(n),"utf8"),a=i.protocol==="https:"?pe:je,s=i.protocol==="https:"?new pe.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((o,u)=>{let c=a.request(i,{method:"POST",agent:s,headers:{"content-type":"application/json","content-length":String(r.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},d=>{let g=[],p=0;d.on("data",h=>{if(p+=h.byteLength,p>Oe){d.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(h);}),d.on("error",u),d.on("end",()=>{let h=Buffer.concat(g).toString("utf8"),l;try{l=h?JSON.parse(h):{};}catch{u(new k(d.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let y=d.statusCode??500;if(y<200||y>=300){u(new k(y,String(l.code??"request-failed"),String(l.message??`Content-sync API request failed (${y}).`)));return}o(l);});});c.on("timeout",()=>c.destroy(new Error(`Content-sync API request timed out: ${t}`))),c.on("error",u),c.end(r);})}async head(t){return this.post("head",t)}async unboundHead(t){return this.post("head",{postTypes:t})}async events(t){let n=[],i=t.afterSequence,r=new Set(t.postTypes);for(;i<t.throughSequence;){let a=await this.post("events",{...t,afterSequence:i,limit:250});if(a.fromSequence!==i||a.throughSequence!==t.throughSequence||a.nextSequence<i||a.nextSequence>t.throughSequence)throw new Error("Content-sync API returned an inconsistent event page.");if(!Array.isArray(a.items))throw new Error("Content-sync API returned a malformed event page.");let s=i;for(let o of a.items){if(!Number.isSafeInteger(o.sequence)||o.sequence<=s||o.sequence>t.throughSequence||!Number.isSafeInteger(o.postId)||o.postId<=0||!r.has(o.postType)||typeof o.operation!="string"||!o.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");s=o.sequence;}if(a.items.length>0&&a.nextSequence!==s)throw new Error("Content-sync API event cursor does not match the final returned event.");if(n.push(...a.items),!a.hasMore)break;if(a.nextSequence===i)throw new Error("Content-sync API event pagination did not advance.");i=a.nextSequence;}if(t.throughSequence>t.afterSequence&&n.at(-1)?.sequence!==t.throughSequence)throw new Error("Content-sync API omitted the claimed journal high-water event.");return n}async resolveImpactFamilies(t){let n=await this.post("impact",{families:t});return Array.isArray(n.families)?n.families:[]}async releaseFingerprint(){let t=await this.post("fingerprint",{}),n=String(t.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)}};function Le(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Be(e,t){return {...e,...t??{}}}function Me(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function _(e,t){let n=String(t??e.deploymentTargetOverride??"").trim();if(!Le(e)){if(n)throw new Error(`Deployment target "${n}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:e}}let i=String(n||e.defaultDeploymentProfile||"").trim();if(!i)return {name:null,profile:null,config:e};let r=e.deploymentProfiles?.[i];if(!r)throw new Error(`Unknown deployment profile "${i}". Check the linked WPSuite publisher configuration.`);return {name:i,profile:r,config:{...e,targetOrigin:r.targetOrigin??e.targetOrigin,s3:Be(e.s3,r.s3),cloudFront:Me(e.cloudFront,r.cloudFront)}}}function he(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function We(e){let t=String(e??"").trim();if(!t)return "";let n=t;if(/^https?:\/\//i.test(n))try{n=new URL(n).pathname;}catch{return ""}if(n=n.split(/[?#]/,1)[0]?.replace(/\\/g,"/").trim()??"",!n)return "";let i=n.endsWith("/"),r=n.replace(/^\/+/,"").split("/").map(s=>s.replace(/[^A-Za-z0-9._-]/g,"")).filter(s=>s.length>0&&s!=="."&&s!=="..");if(r.length===0)return "";let a=`/${r.join("/")}`;return i&&m.extname(a)===""&&(a+="/"),a==="/"?"":a}function z(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t.trim(),String(n??"")]).filter(([t])=>t.length>0))}function Ne(e){if(!e||typeof e!="object")return {};let t={};for(let[n,i]of Object.entries(e)){let r=n.trim();if(!r||!i||typeof i!="object")continue;let a=i,s={};if(typeof a.targetOrigin=="string"){let u=he(a.targetOrigin);u&&(s.targetOrigin=u);}let o=z(a.extraReplacements);if(Object.keys(o).length>0&&(s.extraReplacements=o),a.s3&&typeof a.s3=="object"){let u={},c=a.s3;typeof c.bucket=="string"&&(u.bucket=c.bucket.trim()),typeof c.prefix=="string"&&(u.prefix=c.prefix.trim()),typeof c.region=="string"&&(u.region=c.region.trim()),typeof c.htmlCacheControl=="string"&&(u.htmlCacheControl=c.htmlCacheControl.trim()),typeof c.assetCacheControl=="string"&&(u.assetCacheControl=c.assetCacheControl.trim()),Object.keys(u).length>0&&(s.s3=u);}if(a.cloudFront&&typeof a.cloudFront=="object"){let u={},c=a.cloudFront;typeof c.distributionId=="string"&&(u.distributionId=c.distributionId.trim()),Array.isArray(c.invalidationPaths)&&(u.invalidationPaths=c.invalidationPaths.map(d=>String(d??"").trim()).filter(d=>d.length>0)),Object.keys(u).length>0&&(s.cloudFront=u);}t[r]=s;}return t}function G(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function Ke(e,t){if(!e||typeof e!="object")return null;let n=e,i=n.command;if(i!=="publish"&&i!=="crawl"&&i!=="deploy"&&i!=="invalidate"&&i!=="retry-timeouts"&&i!=="url"&&i!=="content-sync")return null;let r=Number.parseInt(String(n.intervalMinutes??"0"),10);if(!Number.isFinite(r)||r<1)return null;let a=String(n.id??`${i}-${t+1}`).trim();if(!a)return null;let s=String(n.deploymentProfile??"").trim(),o=String(n.url??"").trim(),u=Array.isArray(n.postTypes)?[...new Set(n.postTypes.map(d=>String(d??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],c=Array.isArray(n.listingPaths)?[...new Set(n.listingPaths.map(d=>String(d??"").trim()).filter(Boolean))]:[];return i==="content-sync"&&u.length===0?null:{id:a,enabled:n.enabled!==false,command:i,intervalMinutes:r,...i==="publish"||i==="crawl"?{crawlMode:n.crawlMode==="incremental"?"incremental":"full"}:{},...(i==="publish"||i==="deploy"||i==="invalidate"||i==="content-sync")&&s?{deploymentProfile:s}:{},...o?{url:o}:{},...u.length>0?{postTypes:u}:{},...c.length>0?{listingPaths:c}:{},...typeof n.includePostTypeArchives=="boolean"?{includePostTypeArchives:n.includePostTypeArchives}:{},...typeof n.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:n.includeTaxonomyArchives}:{},...typeof n.includeAuthorArchives=="boolean"?{includeAuthorArchives:n.includeAuthorArchives}:{},...typeof n.includeDateArchives=="boolean"?{includeDateArchives:n.includeDateArchives}:{},...typeof n.includePostsPage=="boolean"?{includePostsPage:n.includePostsPage}:{},...typeof n.includeSitemapChain=="boolean"?{includeSitemapChain:n.includeSitemapChain}:{}}}function fe(e){let t=e&&typeof e=="object"?e:{},n=Array.isArray(t.rules)?t.rules.map((i,r)=>Ke(i,r)).filter(i=>!!i):[];return {enabled:t.enabled===true,timezone:typeof t.timezone=="string"&&t.timezone.trim()!==""?t.timezone.trim():"UTC",rules:n}}function _e(e){if(!e||typeof e!="object")return;let t=e,n={};for(let r of ["accountId","siteId"]){let a=t[r];typeof a=="string"&&a.trim()!==""&&(n[r]=a.trim());}let i=Number(t.lastUpdate??0);return Number.isFinite(i)&&i>0&&(n.lastUpdate=Math.floor(i)),t.subscriber===true&&(n.subscriber=true),Object.keys(n).length>0?n:void 0}function V(e){let t=e&&typeof e=="object"?e:{},n=String(t.accountId??"").trim(),i=String(t.siteId??"").trim(),r=t.subscriber===true,a=_e(t.siteSettings),s={...a??{},...n?{accountId:a?.accountId??n}:{},...i?{siteId:a?.siteId??i}:{},...a?.subscriber===true||r?{subscriber:true}:{}},o={},u=String(t.apiBase??"").trim();u&&(o.apiBase=u);let c=String(t.runtimeToken??t.nonce??"").trim();c&&(o.runtimeToken=c);let d=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();d&&(o.virtualAssetBaseUrl=d),Object.keys(s).length>0&&(o.siteSettings=s);let g=G(t.subscriptionType);return g&&(o.subscriptionType=g),o}function ze(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function ye(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function He(e,t){let n=new Headers(e instanceof Request?e.headers:void 0);if(t?.headers)for(let[i,r]of new Headers(t.headers).entries())n.set(i,r);return n}async function be(e,t,n=5){let i=new URL(ye(e)),r=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),a=i.protocol==="http:"?je:pe,s=He(e,t);return await new Promise((o,u)=>{let c=a.request(i,{method:r,headers:Object.fromEntries(s.entries()),...i.protocol==="https:"?{rejectUnauthorized:false}:{}},d=>{let g=[];d.on("data",p=>{g.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),d.on("error",u),d.on("end",()=>{let p=d.statusCode??0,h=d.headers.location;if(n>0&&h&&[301,302,303,307,308].includes(p)){o(be(new URL(h,i),{method:p===303?"GET":r},n-1));return}let l=new Headers;for(let[y,b]of Object.entries(d.headers))Array.isArray(b)?l.set(y,b.join(", ")):typeof b=="string"&&l.set(y,b);o(new Response(Buffer.concat(g),{status:p,statusText:d.statusMessage??"",headers:l}));});});c.on("error",u),c.end();})}function Ge(e){let t=V(e.wpsuite),n={...t.siteSettings??{},...t.siteSettings?.subscriber===true||t.subscriptionType==="PROFESSIONAL"||t.subscriptionType==="AGENCY"?{subscriber:true}:{}},i=String(n.accountId??"").trim(),r=String(n.siteId??"").trim(),a=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();if(e.wpsuite={...t.apiBase?{apiBase:t.apiBase}:{},...t.runtimeToken?{runtimeToken:t.runtimeToken}:{},...a?{virtualAssetBaseUrl:a}:{},...Object.keys(n).length>0?{siteSettings:n}:{},...t.subscriptionType?{subscriptionType:t.subscriptionType}:{}},!i||!r||!a)return false;ze(e.sourceOrigin);let s=globalThis,u={...s.WpSuite??{},siteSettings:n,uploadUrl:a};return t.apiBase&&(u.apiBase=t.apiBase),s.WpSuite=u,true}async function Ve(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!Ge(e)){let r=e.wpsuite?.siteSettings,a=[String(r?.accountId??"").trim()?"":"accountId",String(r?.siteId??"").trim()?"":"siteId",t?"":"virtualAssetBaseUrl"].filter(Boolean);return {remote:null,diagnostic:{status:"invalid-runtime",virtualAssetBaseUrl:t,subscriptionType:null,error:`Missing WP Suite runtime field(s): ${a.join(", ")}.`,requests:[]}}}let n=globalThis.fetch,i=[];try{typeof n=="function"&&/^https?:\/\//i.test(t)&&(globalThis.fetch=(async(u,c)=>{let d=ye(u);if(d.startsWith(t)){let g=(()=>{try{return new URL(d).pathname}catch{return d}})();try{let p=e.ignoreHttpsErrors&&d.startsWith("https://")?await be(u,c):await n(u,c);return i.push({path:g,status:p.status,ok:p.ok,error:null}),p}catch(p){throw i.push({path:g,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return n(u,c)}));let{getConfig:r}=await import('@smart-cloud/wpsuite-core'),a=await r("publisher"),s=a&&typeof a=="object"?a:null,o=G(s?.subscriptionType);return {remote:s,diagnostic:{status:s?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:o??null,error:s?null:"WP Suite publisher config loader returned no configuration.",requests:i}}}catch(r){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:t,subscriptionType:null,error:r instanceof Error?r.message:String(r),requests:i}}}finally{typeof n=="function"&&(globalThis.fetch=n);}}function Ye(e,t){let n=Ne(t?.deploymentProfiles),i=String(t?.defaultDeploymentProfile??"").trim(),r=n[i]?i:"",a=G(t?.subscriptionType),s=V(e.wpsuite);return {...e,scheduler:fe(t?.scheduler),deploymentProfiles:n,defaultDeploymentProfile:r,...a?{subscriptionType:a}:{},wpsuite:{...s,...s.siteSettings||a?{siteSettings:{...s.siteSettings??{},...s.siteSettings?.subscriber===true||a==="PROFESSIONAL"||a==="AGENCY"?{subscriber:true}:{}}}:{},...a?{subscriptionType:a}:{}}}}function H(e,t){let i=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!i)return t;let r=i.split("/").map(a=>a.trim()).filter(a=>a.length>0&&a!=="."&&a!=="..");return r.length>0?r.join("/"):t}function Ze(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?m.resolve(e):""}function me(e,t,n){let i=String(t||"").trim();return i&&m.isAbsolute(i)?m.resolve(i):m.resolve(e,H(i,n))}async function Y(e,t={}){let n=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",i=await v.readFile(n,"utf8"),r=JSON.parse(i);r.sourceOrigin=r.sourceOrigin.replace(/\/$/,""),r.targetOrigin=he(r.targetOrigin),r.ignoreHttpsErrors??=false,r.outputDir=String(r.outputDir||"export").trim()||"export",r.urlRewriteMode||=r.targetOrigin==="."?"relative":"absolute",r.noJavaScriptRenderPathPrefixes||=[],r.seedPaths||=[],r.generated404RequestPath=We(r.generated404RequestPath),r.sitemapPaths||=["/sitemap_index.xml","/sitemap.xml"],r.allowedAssetHosts||=[],r.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],r.blockedPathPrefixes||=[],r.blockedSearchFragments||=[],r.concurrency||=1,r.maxPages||=0,r.extraReplacements=z(r.extraReplacements),r.postCrawlCopyMap=z(r.postCrawlCopyMap),r.logDir=String(r.logDir||"logs").trim()||"logs",r.verbose??=false,r.logLevel||=r.verbose?"debug":"info",r.s3SyncMode||="sdk-upload-delete",r.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},r.readiness.timeoutMs??=1500,r.readiness.fallbackWaitMs??=1500,r.viewport||={width:1440,height:1200},r.navigationTimeoutMs||=3e4,r.assetDownloadConcurrency=Number(r.assetDownloadConcurrency)>0?Number(r.assetDownloadConcurrency):r.concurrency,r.rewriteConcurrency=Number(r.rewriteConcurrency)>0?Number(r.rewriteConcurrency):r.assetDownloadConcurrency,r.wpsuite=V(r.wpsuite),r.scheduler=fe(void 0),r.deploymentProfiles={},r.defaultDeploymentProfile="",r.deploymentTargetOverride=String(r.deploymentTargetOverride??"").trim();let a=await Ve(r);t.onRemotePublisherConfigDiagnostic?.(a.diagnostic);let s=Ye(r,a.remote),o=Ze(),u=o?m.resolve(o,".."):"";return u?(s.outputDir=me(u,s.outputDir,"export"),s.logDir=me(u,s.logDir,"logs")):(m.isAbsolute(s.outputDir)||(s.outputDir=H(s.outputDir,"export")),m.isAbsolute(s.logDir)||(s.logDir=H(s.logDir,"logs"))),s}function Pe(e){let t=String(e.command??"").trim();return (t==="publish"||t==="crawl")&&e.crawlMode==="incremental"||t==="content-sync"}var Z=new Map;async function Se(e){let t=m.resolve(e),n=Z.get(t);if(n!==void 0)return n;try{let i=JSON.parse(await v.readFile(t,"utf8")),r=String(i.version??"").trim();return Z.set(t,r),r}catch{return Z.set(t,""),""}}var O=class extends Error{request;step;constructor(t,n){super(`Stop requested during ${n}.`),this.name="StopRequestedError",this.request=t,this.step=n;}},it="queue-mutation.lock",st=5e3,ot=3e4;function at(e){let t=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",n=process.env.STATIC_PUBLISHER_EXPORTER_DIR||process.cwd(),i=process.env.PUBLISHER_CONFIG||"",r=1;for(let o=0;o<e.length;o++){let u=e[o];u==="--runtime-dir"?t=e[++o]||"":u.startsWith("--runtime-dir=")?t=u.slice(14):u==="--exporter-dir"?n=e[++o]||n:u.startsWith("--exporter-dir=")?n=u.slice(15):u==="--config"?i=e[++o]||"":u.startsWith("--config=")?i=u.slice(9):u==="--max-jobs"?r=Number.parseInt(e[++o]||"1",10):u.startsWith("--max-jobs=")&&(r=Number.parseInt(u.slice(11),10));}if(!t)throw new Error("Missing runtime dir. Use --runtime-dir or STATIC_PUBLISHER_RUNTIME_DIR.");let a=m.resolve(t),s=i?m.resolve(i):m.join(a,"config.json");return {runtimeDir:a,exporterDir:m.resolve(n),configPath:s,maxJobs:Number.isFinite(r)&&r>0?r:1}}async function w(e,t){try{let n=await v.readFile(e,"utf8");return JSON.parse(n)}catch(n){if((n&&typeof n=="object"&&"code"in n?String(n.code||""):"")!=="ENOENT"){let r=n instanceof Error?n.message:String(n);console.warn(`[queue-runner] failed to read JSON ${e}: ${r}`);}return t}}async function S(e,t){await v.mkdir(m.dirname(e),{recursive:true}),await v.writeFile(e,JSON.stringify(t,null,2),"utf8");}function Ce(e){return m.join(e,it)}async function ut(e){await new Promise(t=>setTimeout(t,e));}async function ct(e){let t=Ce(e),n=Date.now()+st;for(;;)try{await v.mkdir(m.dirname(t),{recursive:!0}),await v.writeFile(t,JSON.stringify({pid:process.pid,createdAt:new Date().toISOString()},null,2),{encoding:"utf8",flag:"wx"});return}catch(i){if((i&&typeof i=="object"&&"code"in i?String(i.code||""):"")!=="EEXIST")throw i;let a=await v.stat(t).catch(()=>null);if(a&&Date.now()-a.mtimeMs>ot){await v.unlink(t).catch(()=>{});continue}if(Date.now()>=n)throw new Error("Timed out acquiring queue mutation lock.",{cause:i});await ut(50);}}async function lt(e){await v.unlink(Ce(e)).catch(()=>{});}async function E(e,t){await ct(e);try{return await t()}finally{await lt(e);}}function Ae(e){return m.join(e,"queue-runner-heartbeat.json")}function oe(e){return m.join(e,"current-progress.json")}function X(e){return m.join(e,"scheduler-state.json")}function dt(e,t){let i=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!i)return t;let r=i.split("/").map(a=>a.trim()).filter(a=>a.length>0&&a!=="."&&a!=="..");return r.length>0?r.join("/"):t}async function Ie(e){let t=await w(e.configPath||"",null),n=m.resolve(e.runtimeDir,".."),r=(typeof t?.logDir=="string"?t.logDir:"").trim();return r&&m.isAbsolute(r)?m.resolve(r):m.resolve(n,dt(r,"logs"))}function ee(e,t){return String(e||"").trim().replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/^-+|-+$/g,"")||t}function gt(e){let t=e?new Date(e):new Date;return (Number.isNaN(t.getTime())?new Date:t).toISOString().replace(/[-:]/g,"").replace(/\.\d{3}Z$/,"Z")}function pt(e){return [gt(e.endedAt||e.startedAt),ee(e.command||"job","job"),ee(e.id||"job","job"),ee(e.status||"finished","finished")].join("-")}function mt(e){return e.endsWith(".log.jsonl")||e.endsWith(".errors.jsonl")||e.endsWith(".rejected.jsonl")?true:["current-crawl-event.json","rejected.jsonl","ignored.jsonl","skipped-http.jsonl","errors.jsonl","timings.jsonl","rejected.json","ignored.json","skipped-http.json","errors.json","timings.json"].includes(e)}function ht(e){return e==="errors.json"||e==="errors.jsonl"||e.endsWith(".errors.jsonl")?"errors":e==="ignored.json"||e==="ignored.jsonl"?"ignored":e==="rejected.json"||e==="rejected.jsonl"||e.endsWith(".rejected.jsonl")?"rejected":e==="skipped-http.json"||e==="skipped-http.jsonl"?"skipped-http":e==="timings.json"||e==="timings.jsonl"?"timings":e==="current-progress.json"?"current-progress":e==="current-crawl-event.json"?"current-crawl-event":e.endsWith(".log.jsonl")?e.replace(/\.log\.jsonl$/i,"-log"):e.replace(/\.[^.]+$/u,"")||"artifact"}function ft(e){return e.endsWith(".gz")?"application/gzip":e.endsWith(".jsonl")?"application/x-ndjson":e.endsWith(".json")?"application/json":"text/plain"}async function te(e,t,n){let i=`${n}.gz`,r=m.join(t,i);await pipeline(createReadStream(e),createGzip({level:9}),createWriteStream(r));let[a,s]=await Promise.all([v.stat(e),v.stat(r)]);return {role:ht(n),originalFileName:n,storedFileName:i,compressed:true,compression:"gzip",contentType:ft(i),originalSize:a.size,storedSize:s.size}}async function U(e,t){let n=await Ie(t),i=new Date().toISOString(),r=pt(e),a=m.join(n,"archive",r),s=[],o=[];await v.mkdir(a,{recursive:true});try{let d=await v.readdir(n,{withFileTypes:!0});for(let g of d){if(!g.isFile()||!mt(g.name))continue;let p=await te(m.join(n,g.name),a,g.name);o.push(p),s.push(p.storedFileName);}}catch(d){if((d&&typeof d=="object"&&"code"in d?String(d.code||""):"")!=="ENOENT")throw d}let u=oe(t.runtimeDir);if(existsSync(u)){let d=await te(u,a,"current-progress.json");o.push(d),s.push(d.storedFileName);}if(e.command==="content-sync")for(let d of ["content-sync-current.json","content-sync-impact-plan.json","content-sync-checkpoint.json","content-sync-state.json","content-sync-invalidation.json","deploy-plan.json"]){let g=m.join(t.runtimeDir,d);if(!existsSync(g))continue;let p=await te(g,a,d);o.push(p),s.push(p.storedFileName);}let c=[...s,"job.json"];return await S(m.join(a,"job.json"),{manifestVersion:1,archivedAt:i,archiveKey:r,archiveDir:a,logDir:n,runtimeDir:t.runtimeDir,exporterDir:t.exporterDir,configPath:t.configPath||"",archivedFiles:c,artifacts:o,job:e}),{archiveKey:r,archiveDir:a,archiveCreatedAt:i,archivedFiles:c,artifacts:o}}function yt(e){return m.join(e,"audit-events.jsonl")}function ie(e){return m.join(e,"stop-request.json")}async function Re(e){try{let t=await v.readFile(ie(e),"utf8"),n=JSON.parse(t);return n&&typeof n=="object"?n:null}catch{return null}}async function we(e,t){let n=await Re(e);if(!n)return null;let i=String(n.targetJobId||"").trim();return i&&i!==t?null:n}async function j(e,t){if(!t){await v.unlink(ie(e)).catch(()=>{});return}let n=await Re(e);if(!n)return;let i=String(n.targetJobId||"").trim();(!i||i===t)&&await v.unlink(ie(e)).catch(()=>{});}async function I(e,t){try{let n={occurredAt:t.occurredAt||new Date().toISOString(),status:t.status||"info",actorSource:t.actorSource||"queue-runner",...t};await v.mkdir(e,{recursive:!0}),await v.appendFile(yt(e),`${JSON.stringify(n)}
2
- `,"utf8");}catch{}}async function C(e,t,n){let[i,r]=await Promise.all([Se(m.join(e.exporterDir,"package.json")),Se(m.join(e.exporterDir,"node_modules","@smart-cloud","wpsuite-core","package.json"))]),a={checkedAt:new Date().toISOString(),status:t,pid:process.pid,nodePath:process.execPath,nodeVersion:process.version,exporterVersion:i,wpsuiteCoreVersion:r,runtimeDir:e.runtimeDir,exporterDir:e.exporterDir,...n??{}};try{await S(Ae(e.runtimeDir),a);}catch{}}async function J(e){try{await v.unlink(oe(e));}catch{}}function bt(e,t){let n=e.command;if(n!=="publish"&&n!=="crawl"&&n!=="deploy"&&n!=="invalidate"&&n!=="retry-timeouts"&&n!=="url"&&n!=="content-sync")return null;let i=Number.parseInt(String(e.intervalMinutes??"0"),10);if(!Number.isFinite(i)||i<1)return null;let r=(e.id||`${n}-${t+1}`).trim();if(!r)return null;let a=e.enabled!==false,s=typeof e.url=="string"?e.url.trim():"",o=typeof e.deploymentProfile=="string"?e.deploymentProfile.trim():"",u=Array.isArray(e.postTypes)?[...new Set(e.postTypes.map(g=>String(g).trim()).filter(Boolean))]:[],c=Array.isArray(e.listingPaths)?[...new Set(e.listingPaths.map(g=>String(g).trim()).filter(Boolean))]:[],d=(n==="publish"||n==="crawl")&&e.crawlMode==="incremental"?"incremental":"full";return n==="url"&&!s||n==="content-sync"&&u.length===0?null:{id:r,enabled:a,command:n,intervalMinutes:i,...n==="publish"||n==="crawl"?{crawlMode:d}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&o?{deploymentProfile:o}:{},...s?{url:s}:{},...u.length>0?{postTypes:u}:{},...c.length>0?{listingPaths:c}:{},...typeof e.includePostTypeArchives=="boolean"?{includePostTypeArchives:e.includePostTypeArchives}:{},...typeof e.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:e.includeTaxonomyArchives}:{},...typeof e.includeAuthorArchives=="boolean"?{includeAuthorArchives:e.includeAuthorArchives}:{},...typeof e.includeDateArchives=="boolean"?{includeDateArchives:e.includeDateArchives}:{},...typeof e.includePostsPage=="boolean"?{includePostsPage:e.includePostsPage}:{},...typeof e.includeSitemapChain=="boolean"?{includeSitemapChain:e.includeSitemapChain}:{}}}async function St(e){let t=await Y(e.configPath).catch(()=>null),n=t?.scheduler,i=Array.isArray(n?.rules)?n.rules.map((r,a)=>bt(r,a)).filter(r=>!!r):[];return {enabled:!!n?.enabled,timezone:typeof n?.timezone=="string"&&n.timezone.trim()?n.timezone.trim():"UTC",rules:i,wpsuite:t?.wpsuite,publisherConfig:t}}function ne(e,t){if(!t)throw new Error("Content-sync scheduler requires a valid publisher config.");let n=B({postTypes:e.postTypes??[],listingPaths:e.listingPaths??[],includePostTypeArchives:e.includePostTypeArchives,includeTaxonomyArchives:e.includeTaxonomyArchives,includeAuthorArchives:e.includeAuthorArchives,includeDateArchives:e.includeDateArchives,includePostsPage:e.includePostsPage,includeSitemapChain:e.includeSitemapChain}),i=N(t.sourceOrigin,e.id,n),r=_(t,e.deploymentProfile),a=K({sourceOrigin:t.sourceOrigin,targetOrigin:r.config.targetOrigin,deploymentProfile:r.name||"",s3Bucket:r.config.s3.bucket,s3Prefix:r.config.s3.prefix,cloudFrontDistributionId:r.config.cloudFront.distributionId,urlRewriteMode:r.config.urlRewriteMode,extraReplacements:r.profile?.extraReplacements??{}});return `content-sync:${e.id}:${F({scopeFingerprint:i,targetFingerprint:a}).slice(0,32)}`}async function wt(e,t,n,i,r){let s=(await w(m.join(e.runtimeDir,"content-sync-baseline.json"),{})).entries?.[i];if(!s?.consumerId||!s.scopeFingerprint||!s.baselineId)return {status:"baseline-required",reason:"No verified normal-release baseline exists for this rule scope."};try{let o=t.publisherConfig;if(!o)throw new Error("Publisher configuration is unavailable.");let u=B({postTypes:n.postTypes??[],listingPaths:n.listingPaths??[],includePostTypeArchives:n.includePostTypeArchives,includeTaxonomyArchives:n.includeTaxonomyArchives,includeAuthorArchives:n.includeAuthorArchives,includeDateArchives:n.includeDateArchives,includePostsPage:n.includePostsPage,includeSitemapChain:n.includeSitemapChain}),c=N(o.sourceOrigin,n.id,u),d=_(o,n.deploymentProfile),g=K({sourceOrigin:o.sourceOrigin,targetOrigin:d.config.targetOrigin,deploymentProfile:d.name||"",s3Bucket:d.config.s3.bucket,s3Prefix:d.config.s3.prefix,cloudFrontDistributionId:d.config.cloudFront.distributionId,urlRewriteMode:d.config.urlRewriteMode,extraReplacements:d.profile?.extraReplacements??{}});if(s.scopeFingerprint!==c||s.targetFingerprint!==g||!s.releaseFingerprint)return {status:"baseline-required",reason:"The verified content-sync baseline does not match the active scope or deployment target. Run a successful full or incremental publish to establish a new baseline."};let p=new M({sourceOrigin:o.sourceOrigin,runtimeToken:String(o.wpsuite?.runtimeToken||""),ignoreHttpsErrors:o.ignoreHttpsErrors}),h=await p.releaseFingerprint(),l=le({wordpressFingerprint:h,targetFingerprint:g,sourceOrigin:o.sourceOrigin,sitemapPaths:o.sitemapPaths,blockedPathPrefixes:o.blockedPathPrefixes,assetPathPrefixes:o.assetPathPrefixes,noJavaScriptRenderPathPrefixes:o.noJavaScriptRenderPathPrefixes,urlRewriteMode:o.urlRewriteMode});if(s.releaseFingerprint!==l)return {status:"baseline-required",reason:"The installed WordPress release changed after the last verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline."};let y=await p.head({consumerId:s.consumerId,scopeFingerprint:s.scopeFingerprint,baselineId:s.baselineId,postTypes:u.postTypes}),b=y.committedSequence;if(r?.command==="content-sync"&&r.coalesceKey===i){let A=await w(m.join(e.runtimeDir,"content-sync-current.json"),null);A?.coalesceKey===i&&Number.isSafeInteger(A.toSequence)&&(b=Math.max(b,Number(A.toSequence)));}return {status:y.headSequence>b?"pending":"current",headSequence:y.headSequence,committedSequence:y.committedSequence}}catch(o){return o instanceof k&&o.code==="baseline-required"?{status:"baseline-required",reason:o.message}:{status:"unavailable",reason:o instanceof Error?o.message:String(o)}}}async function qe(e,t,n,i,r,a){let s=m.join(e.runtimeDir,"content-sync-state.json"),u=(await w(s,{})).rules??{},c=u[n]??{};u[n]={...c,ruleId:t,coalesceKey:n,...Number.isSafeInteger(r)?{observedHeadSequence:r}:{},...Number.isSafeInteger(a)?{committedSequence:a}:{},retryAttempt:0,nextRetryAt:null,lastError:i,baselineStatus:"required",baselineReason:i,trailingWorkDetected:Number.isSafeInteger(r)&&Number.isSafeInteger(a)?Number(r)>Number(a):!!c.trailingWorkDetected},await S(s,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:u});}function se(e,t){let n=(t.url||"").trim(),i=t.crawlMode||"full",r=(t.deploymentProfile||"").trim(),a=(t.coalesceKey||"").trim();return e.some(s=>s.command===t.command&&(t.command!=="content-sync"||(s.coalesceKey||"").trim()===a)&&(s.url||"").trim()===n&&(s.crawlMode||"full")===i&&(s.deploymentProfile||"").trim()===r&&(s.status===void 0||s.status==="queued"||s.status==="retry-wait"||s.status==="running"))}async function vt(e,t){if(!t.allowSchedulerAutoEnqueue)return 0;let n=await St(e);if(!n.enabled||n.rules.length===0)return 0;if(t.assertActiveSubscriptionForIdentity)try{await t.assertActiveSubscriptionForIdentity(n.wpsuite??null);}catch(l){let y=l instanceof Error?l.message:String(l);return console.warn(`[queue-runner] scheduler auto-enqueue skipped: ${y}`),0}let i=m.join(e.runtimeDir,"queue.json"),r=m.join(e.runtimeDir,"current-run.json"),a=Date.now(),s=0,o=[],u=[],c=[],d=new Map,[g,p,h]=await Promise.all([w(i,[]),w(r,null),w(X(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}})]);for(let l of n.rules){if(!l.enabled||l.command!=="content-sync")continue;let y=Math.floor(a/(l.intervalMinutes*60*1e3)),b=h.lastEvaluatedBucketByRuleId?.[l.id]??h.lastEnqueuedBucketByRuleId?.[l.id]??-1;if(y<=b)continue;let A=ne(l,n.publisherConfig);se(g,{command:l.command,deploymentProfile:l.deploymentProfile,coalesceKey:A})||d.set(l.id,await wt(e,n,l,A,p));}await E(e.runtimeDir,async()=>{let l=await w(i,[]),y=await w(r,null),b=await w(X(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}});b.lastEnqueuedBucketByRuleId??={},b.lastEvaluatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.lastCreatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.coalescedCountByRuleId??={};let A=[...l];y&&y.status==="running"&&A.unshift(y);for(let f of n.rules){if(!f.enabled)continue;let $=f.intervalMinutes*60*1e3,P=Math.floor(a/$),x=b.lastEvaluatedBucketByRuleId[f.id]??b.lastEnqueuedBucketByRuleId[f.id]??-1;if(P<=x)continue;let T=f.command==="content-sync"?ne(f,n.publisherConfig):"";b.lastEvaluatedBucketByRuleId[f.id]=P;let L=f.command==="content-sync"?l:A;if(se(L,{command:f.command,url:f.url,crawlMode:f.crawlMode,deploymentProfile:f.deploymentProfile,coalesceKey:T})){b.coalescedCountByRuleId[f.id]=(b.coalescedCountByRuleId[f.id]??0)+1,u.push(f);continue}if(f.command==="content-sync"){let Q=d.get(f.id);if(!Q||Q.status!=="pending"){let ue=Q??{status:"unavailable",reason:"Content-sync demand was not checked."};ue.status!=="current"&&c.push({rule:f,check:ue});continue}}let q={id:randomUUID(),command:f.command,...f.command==="content-sync"?{ruleId:f.id,coalesceKey:T,attempt:0}:{},...(f.command==="publish"||f.command==="crawl")&&f.crawlMode?{crawlMode:f.crawlMode}:{},...(f.command==="publish"||f.command==="deploy"||f.command==="invalidate"||f.command==="content-sync")&&f.deploymentProfile?{deploymentProfile:f.deploymentProfile}:{},...f.url?{url:f.url}:{},enqueueSource:"scheduler",...n.wpsuite?{wpsuite:n.wpsuite}:{},status:"queued",createdAt:new Date().toISOString(),createdBy:0};l.push(q),A.push(q),o.push({job:q,rule:f}),b.lastEnqueuedBucketByRuleId[f.id]=P,b.lastCreatedBucketByRuleId[f.id]=P,s+=1;}s>0&&await S(i,l),await S(X(e.runtimeDir),b);});for(let l of o){if(l.rule.command==="content-sync"){let y=d.get(l.rule.id);await I(e.runtimeDir,{eventType:"content-sync-demand-detected",status:"queued",actorSource:"queue-runner-scheduler",jobId:l.job.id,command:l.job.command,message:"Pending journal changes require a content-sync job.",details:{ruleId:l.rule.id,headSequence:y?.headSequence??0,committedSequence:y?.committedSequence??0}});}await I(e.runtimeDir,{eventType:"job-created",status:"queued",actorSource:"queue-runner-scheduler",jobId:l.job.id,command:l.job.command,message:"Scheduler auto-enqueued a job.",details:{ruleId:l.rule.id,intervalMinutes:l.rule.intervalMinutes,timezone:n.timezone,deploymentProfile:l.rule.deploymentProfile||"",url:l.rule.url||""}});}for(let l of u)await I(e.runtimeDir,{eventType:"content-sync-coalesced",status:"queued",actorSource:"queue-runner-scheduler",command:l.command,message:"Scheduler demand was coalesced into an existing content-sync job.",details:{ruleId:l.id}});for(let l of c){if(l.check.status==="baseline-required"){let y=ne(l.rule,n.publisherConfig);await qe(e,l.rule.id,y,l.check.reason||"A successful full or incremental publish must establish a new content-sync baseline.",l.check.headSequence,l.check.committedSequence);}await I(e.runtimeDir,{eventType:l.check.status==="baseline-required"?"content-sync-baseline-required":"content-sync-demand-check-failed",status:l.check.status==="unavailable"?"failed":"info",actorSource:"queue-runner-scheduler",command:"content-sync",message:l.check.reason||"The content-sync consumer is already at the current journal head.",details:{ruleId:l.rule.id,headSequence:l.check.headSequence??0,committedSequence:l.check.committedSequence??0}});}return s}async function Pt(e){let t={pid:process.pid,startedAt:new Date().toISOString()};await v.writeFile(e,JSON.stringify(t,null,2),{encoding:"utf8",flag:"wx"});}async function Ct(e){try{await v.unlink(e);}catch{}}function At(e,t){let n=[m.join(e,"dist",`${t}.js`),m.join(e,`${t}.js`)];for(let i of n)if(existsSync(i))return i;throw new Error(`Cannot find ${t}.js in ${m.join(e,"dist")} or ${e}`)}async function R(e,t,n,i,r,a,s){if(s){let c=await we(t,s);if(c)throw new O(c,n)}let o=At(e,n),u={...process.env,...a??{}};return u.STATIC_PUBLISHER_RUNTIME_DIR=t,r&&(u.PUBLISHER_CONFIG=r),await new Promise((c,d)=>{let g=null,p=null,h=null,l=null,y=false,b="",A=spawn(process.execPath,[o,...i],{cwd:e,env:u,stdio:["inherit","inherit","pipe"]});A.stderr?.on("data",P=>{b+=P.toString("utf8");});let f=()=>{p&&clearInterval(p),h&&clearInterval(h),l&&clearTimeout(l);},$=async()=>{if(!(!s||g||y)){y=true;try{let P=await we(t,s);if(!P)return;g=P,A.kill("SIGTERM"),l=setTimeout(()=>{A.kill("SIGKILL");},1e4);}finally{y=false;}}};s&&(p=setInterval(()=>{$();},1e3),h=setInterval(()=>{(async()=>{let P=Ae(t),x=await w(P,{});await S(P,{...x,checkedAt:new Date().toISOString(),status:"running",currentJobId:s||x.currentJobId||"",currentStep:n});})().catch(()=>{});},15e3)),A.on("error",P=>{f(),d(P);}),A.on("close",(P,x)=>{f();let T=ge(b).trim();if(T&&process.stderr.write(`${T}
3
- `),g){d(new O(g,n));return}if(P===0)c();else {let L=T.split(/\r?\n/).map(q=>q.trim()).find(q=>q&&!q.startsWith("at "));d(new Error(`${n} exited with code ${P??-1}${x?` (signal ${x})`:""}${L?`: ${L}`:""}`));}});}),0}function It(e){let t=e.awsTempCreds;if(!t)return {};let n={};return typeof t.accessKeyId=="string"&&t.accessKeyId.trim()!==""&&(n.AWS_ACCESS_KEY_ID=t.accessKeyId.trim()),typeof t.secretAccessKey=="string"&&t.secretAccessKey.trim()!==""&&(n.AWS_SECRET_ACCESS_KEY=t.secretAccessKey.trim()),typeof t.sessionToken=="string"&&t.sessionToken.trim()!==""&&(n.AWS_SESSION_TOKEN=t.sessionToken.trim()),n}async function Rt(e,t){let n=s=>{if(!s||typeof s!="object")return null;let o=s,u={...o.siteSettings??{},...String(o.accountId??"").trim()&&!o.siteSettings?.accountId?{accountId:String(o.accountId).trim()}:{},...String(o.siteId??"").trim()&&!o.siteSettings?.siteId?{siteId:String(o.siteId).trim()}:{},...o.siteSettings?.subscriber===true||o.subscriber===true?{subscriber:true}:{}},c={...String(o.apiBase??"").trim()?{apiBase:String(o.apiBase).trim()}:{},...String(o.runtimeToken??o.nonce??"").trim()?{runtimeToken:String(o.runtimeToken??o.nonce).trim()}:{},...String(o.virtualAssetBaseUrl??o.uploadUrl??"").trim()?{virtualAssetBaseUrl:String(o.virtualAssetBaseUrl??o.uploadUrl).trim()}:{},...Object.keys(u).length>0?{siteSettings:u}:{},...o.subscriptionType?{subscriptionType:o.subscriptionType}:{}};return Object.keys(c).length>0?c:null},i=await Y(t.configPath).catch(()=>null),r=n(i?.wpsuite);if(r)return r;let a=n(e.wpsuite);return a||null}async function qt(e,t){return await Rt(e,t)}function ae(e){let t={...e,status:"queued"};return delete t.startedAt,delete t.endedAt,delete t.exitCode,delete t.error,delete t.stopRequestedAt,delete t.stopRequestedByUserId,delete t.stopRequestedByLogin,delete t.stopMode,delete t.stoppedStep,t}async function Dt(e,t){let n=await w(oe(e.runtimeDir),null),i=n&&n.details&&typeof n.details.phase=="string"?n.details.phase.trim():"";if(i)return i;let r=n&&typeof n.currentStep=="string"?n.currentStep.trim():"";if(r)return r;let a=await Ie(e).catch(()=>"");if(a){let s=await w(m.join(a,"current-crawl-event.json"),null),o=s&&typeof s.currentStep=="string"?s.currentStep.trim():"";if(o)return o}return t}function xt(e,t){if((e.command==="publish"||e.command==="crawl")&&t==="rewrite-text")return "rewrite-text"}async function Tt(e){let t=m.join(e.runtimeDir,"queue.json"),n=m.join(e.runtimeDir,"current-run.json"),i=await E(e.runtimeDir,async()=>{let r=await w(n,null);if(!r||r.status!=="running"&&r.status!=="queued")return null;let a=await w(t,[]),s=Array.isArray(a)?a.filter(u=>u?.id!==r.id):[],o=ae(r);return await S(t,[o,...s]),await S(n,null),o});i&&(await j(e.runtimeDir,i.id),await I(e.runtimeDir,{eventType:"job-recovered",status:"queued",actorSource:"queue-runner",jobId:i.id,command:i.command,message:"Recovered stale current-run entry back into queue."}));}async function kt(e,t,n){try{if((n.shouldEnforceSubscriptionOnJobExecution?.(e)??n.enforceSubscriptionOnJobExecution)&&n.assertActiveSubscriptionForIdentity){let o=await qt(e,t);await n.assertActiveSubscriptionForIdentity(o);}let r=It(e),a=e.resumeFromStep==="rewrite-text"?["--resume-rewrite"]:e.crawlMode==="incremental"?["--crawl-mode","incremental"]:[],s=e.deploymentProfile?["--profile",e.deploymentProfile]:[];if(e.command==="publish")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-release-cutoff"}),await R(t.exporterDir,t.runtimeDir,"content-sync",["--capture-baseline-cutoffs",e.id],t.configPath,r,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:e.resumeFromStep==="rewrite-text"?"rewrite-text":"crawl"}),await R(t.exporterDir,t.runtimeDir,"crawl",a,t.configPath,r,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await R(t.exporterDir,t.runtimeDir,"deploy",s,t.configPath,r,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await R(t.exporterDir,t.runtimeDir,"invalidate",s,t.configPath,r,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-baseline"}),await R(t.exporterDir,t.runtimeDir,"content-sync",["--establish-baselines",e.id],t.configPath,r,e.id),{exitCode:0};if(e.command==="crawl")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:e.resumeFromStep==="rewrite-text"?"rewrite-text":"crawl"}),await R(t.exporterDir,t.runtimeDir,"crawl",a,t.configPath,r,e.id),{exitCode:0};if(e.command==="deploy")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await R(t.exporterDir,t.runtimeDir,"deploy",s,t.configPath,r,e.id),{exitCode:0};if(e.command==="invalidate")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await R(t.exporterDir,t.runtimeDir,"invalidate",s,t.configPath,r,e.id),{exitCode:0};if(e.command==="retry-timeouts")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"retry-timeouts"}),await R(t.exporterDir,t.runtimeDir,"crawl",["--retry-timeouts"],t.configPath,r,e.id),{exitCode:0};if(e.command==="url"){let o=(e.url||"").trim();return o?(await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"url"}),await R(t.exporterDir,t.runtimeDir,"crawl",["--url",o],t.configPath,r,e.id),{exitCode:0}):{exitCode:2,error:"Missing url for command 'url'"}}return e.command==="content-sync"?!e.ruleId||!e.coalesceKey?{exitCode:2,error:"Content-sync queue jobs require ruleId and coalesceKey."}:(await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync"}),await R(t.exporterDir,t.runtimeDir,"content-sync",["--job-id",e.id],t.configPath,r,e.id),{exitCode:0}):{exitCode:2,error:`Unsupported command: ${e.command}`}}catch(i){return i instanceof O?{exitCode:130,error:"Job stop requested.",stopped:true,stopRequest:i.request,stoppedStep:i.step}:{exitCode:1,error:i instanceof Error?i.message:String(i)}}}function De(e){return async function(n=process.argv.slice(2)){let i=at(n),r=m.join(i.runtimeDir,"export.lock"),a=m.join(i.runtimeDir,"last-run.json");await J(i.runtimeDir),await C(i,"starting",{message:"queue-runner starting"});try{await Pt(r);}catch(s){if((s&&typeof s=="object"&&"code"in s?String(s.code||""):"")!=="EEXIST")throw s;console.log("[queue-runner] lock active, skipping"),await C(i,"lock-active",{message:"lock active, skipped this cron tick"});return}try{await Tt(i);let s=await vt(i,e),o=0;for(let u=0;u<i.maxJobs;u++){let c=await Jt(i,e);if(c==="none"||(o+=1,c==="stopped"))break}if(o===0)await J(i.runtimeDir),await C(i,"idle",{processedJobs:o,schedulerEnqueued:s,message:"no queued jobs"});else {await J(i.runtimeDir);let u=await w(a,null),c=String(u?.status||"").trim();await C(i,c==="success"?"job-success":c==="stopped"?"job-stopped":"job-failed",{processedJobs:o,schedulerEnqueued:s,lastJobId:u?.id,lastJobCommand:u?.command,lastJobStatus:u?.status,lastJobExitCode:u?.exitCode,lastJobError:u?.error,...c==="stopped"?{currentStep:u?.stoppedStep,stopRequestedAt:u?.stopRequestedAt,stopRequestedByLogin:u?.stopRequestedByLogin,stopRequestedMode:u?.stopMode,lastStoppedStep:u?.stoppedStep,message:u?.stopMode==="requeue"?`Job stopped during ${u?.stoppedStep||u?.command||"active step"} and requeued.`:`Job stopped during ${u?.stoppedStep||u?.command||"active step"} and left out of queue.`}:{}});}}catch(s){let o=s instanceof Error?s.message:String(s);throw await C(i,"error",{message:o}),await I(i.runtimeDir,{eventType:"queue-runner-error",status:"failed",actorSource:"queue-runner",message:"Unhandled queue-runner error.",details:{error:o}}),s}finally{await Ct(r);}}}async function Et(e,t,n){let i=m.join(e.runtimeDir,"queue.json"),r=m.join(e.runtimeDir,"current-run.json");await E(e.runtimeDir,async()=>{let a=await w(i,[]),s=Array.isArray(a)?a.filter(c=>c?.id!==t.id):[],o=ae(t),u=xt(t,n);u?o.resumeFromStep=u:delete o.resumeFromStep,await S(i,[o,...s]),await S(r,null);});}async function Ft(e,t,n){let i=Math.max(0,t.attempt??0)+1,r=Math.min(3600*1e3,6e4*2**(i-1)),a=Math.floor(r*.2*Math.random()),s=new Date(Date.now()+r+a).toISOString(),o={...ae(t),status:"retry-wait",attempt:i,nextAttemptAt:s,error:n};if(await E(e.runtimeDir,async()=>{let u=m.join(e.runtimeDir,"queue.json"),c=m.join(e.runtimeDir,"current-run.json"),g=(await w(u,[])).filter(p=>p.id!==t.id);await S(u,[o,...g]),await S(c,null);}),t.coalesceKey){let u=m.join(e.runtimeDir,"content-sync-state.json"),d=(await w(u,{})).rules??{};d[t.coalesceKey]={...d[t.coalesceKey]??{},ruleId:t.ruleId||"",coalesceKey:t.coalesceKey,retryAttempt:i,nextRetryAt:s,lastError:n,trailingWorkDetected:true},await S(u,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:d});}return o}async function jt(e,t){if(!t.coalesceKey||!t.ruleId||!(await w(m.join(e.runtimeDir,"content-sync-state.json"),null))?.rules?.[t.coalesceKey]?.trailingWorkDetected)return null;let i=null;await E(e.runtimeDir,async()=>{let a=m.join(e.runtimeDir,"queue.json"),s=await w(a,[]);se(s,{command:"content-sync",coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile})||(i={id:randomUUID(),command:"content-sync",ruleId:t.ruleId,coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile,enqueueSource:"scheduler",wpsuite:t.wpsuite,status:"queued",attempt:0,createdAt:new Date().toISOString(),createdBy:0},await S(a,[...s,i]));});let r=i;return r&&await I(e.runtimeDir,{eventType:"content-sync-trailing-job-created",status:"queued",actorSource:"queue-runner",jobId:r.id,command:r.command,message:"Content changes after the stable cutoff created one trailing job.",details:{predecessorJobId:t.id,ruleId:t.ruleId,coalesceKey:t.coalesceKey}}),r}async function Jt(e,t){let n=m.join(e.runtimeDir,"queue.json"),i=m.join(e.runtimeDir,"current-run.json"),r=m.join(e.runtimeDir,"last-run.json"),a=new Date().toISOString(),s=await E(e.runtimeDir,async()=>{let g=await w(n,[]);if(!Array.isArray(g)||g.length===0)return null;let p=Date.parse(String(g[0]?.nextAttemptAt||""));if(Number.isFinite(p)&&p>Date.now())return null;let h={...g[0],status:"running",startedAt:a};return delete h.nextAttemptAt,await S(n,g.slice(1)),await S(i,h),h});if(!s)return "none";await J(e.runtimeDir),await I(e.runtimeDir,{eventType:"job-run-started",status:"running",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Job execution started.",details:{createdAt:s.createdAt||"",startedAt:a,createdBy:s.createdBy??null,deploymentProfile:s.deploymentProfile||"",queuedWithTempAwsCreds:!!s.awsTempCreds}}),await C(e,"running",{currentJobId:s.id,currentJobCommand:s.command,currentStep:s.command});let o=await kt(s,e,t),u=new Date().toISOString();if(o.stopped){let g=await Dt(e,o.stoppedStep||s.command),p=o.stopRequest?.mode==="requeue"?"requeue":"stop",h={...s,status:"stopped",endedAt:u,exitCode:o.exitCode,...o.error?{error:o.error}:{},stopRequestedAt:o.stopRequest?.requestedAt||"",stopRequestedByUserId:typeof o.stopRequest?.requestedByUserId=="number"?o.stopRequest.requestedByUserId:null,stopRequestedByLogin:o.stopRequest?.requestedByLogin||"",stopMode:p,stoppedStep:g};p==="requeue"?await Et(e,s,g):await S(i,null);try{let l=await U(h,e);h.logArchiveDir=l.archiveDir,h.logArchiveCreatedAt=l.archiveCreatedAt,h.logArchiveFileCount=l.archivedFiles.length;}catch(l){h.logArchiveError=l instanceof Error?l.message:String(l);}return await S(r,h),await j(e.runtimeDir,s.id),await J(e.runtimeDir),await I(e.runtimeDir,{eventType:"job-run-stopped",status:"stopped",actorSource:"queue-runner",jobId:s.id,command:s.command,message:p==="requeue"?"Job stop requested and requeued.":"Job stop requested and removed from active execution without requeue.",details:{startedAt:s.startedAt||"",endedAt:u,deploymentProfile:s.deploymentProfile||"",stopMode:p,stopRequestedAt:o.stopRequest?.requestedAt||"",stopRequestedByLogin:o.stopRequest?.requestedByLogin||"",stopRequestedByUserId:typeof o.stopRequest?.requestedByUserId=="number"?o.stopRequest.requestedByUserId:null,stoppedStep:g,logArchiveKey:h.logArchiveError||!h.logArchiveDir?"":m.basename(h.logArchiveDir),logArchiveDir:h.logArchiveDir||"",logArchiveCreatedAt:h.logArchiveCreatedAt||"",logArchiveFileCount:h.logArchiveFileCount??0,logArchiveError:h.logArchiveError||""}}),"stopped"}if(s.command==="content-sync"&&o.exitCode!==0){if(/baseline (?:is missing or stale|required)|verified content-sync baseline|new baseline/i.test(o.error||"")){let l="The installed release no longer matches the verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline.";s.ruleId&&s.coalesceKey&&await qe(e,s.ruleId,s.coalesceKey,l);let y={...s,status:"failed",endedAt:u,exitCode:o.exitCode,error:l};try{let b=await U(y,e);y.logArchiveDir=b.archiveDir,y.logArchiveCreatedAt=b.archiveCreatedAt,y.logArchiveFileCount=b.archivedFiles.length;}catch(b){y.logArchiveError=b instanceof Error?b.message:String(b);}return await S(r,y),await j(e.runtimeDir,s.id),await I(e.runtimeDir,{eventType:"content-sync-baseline-required",status:"failed",actorSource:"queue-runner",jobId:s.id,command:s.command,message:l,details:{ruleId:s.ruleId||"",retryScheduled:false}}),"processed"}await I(e.runtimeDir,{eventType:"content-sync-failed",status:"failed",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Content-sync execution failed before cursor acknowledgement.",details:{attempt:s.attempt??0,error:o.error||"Content-sync execution failed."}});let p=await Ft(e,s,o.error||"Content-sync execution failed."),h={...s,status:"retry-wait",endedAt:u,exitCode:o.exitCode,error:p.error,attempt:p.attempt,nextAttemptAt:p.nextAttemptAt};try{let l=await U(h,e);h.logArchiveDir=l.archiveDir,h.logArchiveCreatedAt=l.archiveCreatedAt,h.logArchiveFileCount=l.archivedFiles.length;}catch(l){h.logArchiveError=l instanceof Error?l.message:String(l);}return await S(r,h),await j(e.runtimeDir,s.id),await I(e.runtimeDir,{eventType:"content-sync-retry-scheduled",status:"retry-wait",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Content-sync retry was scheduled with bounded backoff.",details:{attempt:p.attempt??0,nextAttemptAt:p.nextAttemptAt||"",error:p.error||""}}),"processed"}let c={...s,status:o.exitCode===0?"success":"failed",endedAt:u,exitCode:o.exitCode,...o.error?{error:o.error}:{}};await S(i,null),c.status==="success"&&c.command==="content-sync"&&await jt(e,c),await j(e.runtimeDir,s.id);try{let g=await U(c,e);c.logArchiveDir=g.archiveDir,c.logArchiveCreatedAt=g.archiveCreatedAt,c.logArchiveFileCount=g.archivedFiles.length;}catch(g){c.logArchiveError=g instanceof Error?g.message:String(g);}await S(r,c);let d=s.startedAt&&c.endedAt?Math.max(0,Math.round((new Date(c.endedAt).getTime()-new Date(s.startedAt).getTime())/1e3)):void 0;return await I(e.runtimeDir,{eventType:"job-run-finished",status:c.status==="success"?"success":"failed",actorSource:"queue-runner",jobId:c.id,command:c.command,message:c.status==="success"?"Job execution finished successfully.":"Job execution finished with failure.",details:{startedAt:s.startedAt||"",endedAt:c.endedAt||"",durationSec:d,deploymentProfile:c.deploymentProfile||"",exitCode:c.exitCode??null,error:c.error||"",logArchiveKey:c.logArchiveError||!c.logArchiveDir?"":m.basename(c.logArchiveDir),logArchiveDir:c.logArchiveDir||"",logArchiveCreatedAt:c.logArchiveCreatedAt||"",logArchiveFileCount:c.logArchiveFileCount??0,logArchiveError:c.logArchiveError||""}}),"processed"}async function Lt(e){if(!e)throw new Error("Missing remote WPSuite publisher state. Refresh the runtime config before running subscription-gated jobs.");if(e.subscriptionType!=="PROFESSIONAL"&&e.subscriptionType!=="AGENCY")throw new Error("This job requires an active WPSuite publisher subscription in the remote site configuration.")}var Bt={allowSchedulerAutoEnqueue:true,enforceSubscriptionOnJobExecution:false,shouldEnforceSubscriptionOnJobExecution:e=>Pe(e)||e.enqueueSource==="scheduler"||e.createdBy===0,assertActiveSubscriptionForIdentity:Lt},xe=De(Bt),Mt=xe,Sn=xe;process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href&&Mt().catch(e=>{console.error(e),process.exit(1);});export{Sn as default,Mt as main,xe as runQueueRunner};
1
+ import {pathToFileURL}from'url';import P from'fs/promises';import {existsSync,createReadStream,createWriteStream}from'fs';import m from'path';import {spawn}from'child_process';import {randomUUID,createHash}from'crypto';import Je from'http';import me from'https';import {pipeline}from'stream/promises';import {createGzip}from'zlib';function le(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function N(e){return Array.isArray(e)?e.map(N):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>[t,N(n)])):e}function Ee(e){return JSON.stringify(N(e))}function J(e){return createHash("sha256").update(Ee(e)).digest("hex")}function de(e){return J({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 Fe(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 M(e){let t=le((e.postTypes??[]).map(r=>String(r).toLowerCase().replace(/[^a-z0-9_-]/g,"")));if(t.length===0)throw new Error("Content-sync requires at least one public post type.");let n=le((e.listingPaths??[]).map(r=>{let i=String(r).trim();if(!i)return "";let a=new URL(i,"https://content-sync.invalid/");if(a.origin!=="https://content-sync.invalid"&&/^https?:\/\//i.test(i))throw new Error("Content-sync listing routes must be same-site paths.");let s=`/${a.pathname.replace(/^\/+|\/+$/g,"")}`;return s==="/"?"/":`${s}/`}));return {postTypes:t,listingPaths:n,includeSubsites:e.includeSubsites===true,includePostTypeArchives:e.includePostTypeArchives!==false,includeTaxonomyArchives:e.includeTaxonomyArchives!==false,includeAuthorArchives:e.includeAuthorArchives===true,includeDateArchives:e.includeDateArchives===true,includePostsPage:e.includePostsPage!==false,includeSitemapChain:e.includeSitemapChain!==false}}function _(e,t,n){return J({schemaVersion:1,sourceOrigin:Fe(e),ruleId:t.trim(),scope:M(n)})}function K(e){return J({schemaVersion:1,...e})}function pe(e){return e.replace(/((?:x-static-publisher-token|authorization|proxy-authorization|x-api-key|aws-access-key-id|aws-secret-access-key|aws-session-token)\s*:\s*)[^\r\n]+/gi,"$1[REDACTED]").replace(/\b(Basic|Bearer)\s+[^\s]+/gi,"$1 [REDACTED]").replace(/([?&](?:token|access_token|refresh_token|api_key|key|nonce)=)[^&\s]+/gi,"$1[REDACTED]").replace(/((?:"|')?(?:runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken)(?:"|')?\s*[:=]\s*(?:"|'))[^"']+/gi,"$1[REDACTED]")}var ge=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";ge?m.join(ge,"current-progress.json"):"";Promise.resolve();var Oe="wp-json/smartcloud-static-publisher/v1/content-sync/",Le=25*1024*1024,k=class extends Error{status;code;constructor(t,n,r){super(r),this.name="ContentSyncApiError",this.status=t,this.code=n;}},U=class{options;constructor(t){if(!t.runtimeToken.trim())throw new Error("Content-sync requires a WordPress runtime token.");this.options={...t,timeoutMs:t.timeoutMs??6e4};}endpoint(t){let n=this.options.sourceOrigin.replace(/\/+$/,"")+"/";return new URL(`${Oe}${t}`,n)}async post(t,n){let r=this.endpoint(t),i=Buffer.from(JSON.stringify(n),"utf8"),a=r.protocol==="https:"?me:Je,s=r.protocol==="https:"?new me.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((o,u)=>{let c=a.request(r,{method:"POST",agent:s,headers:{"content-type":"application/json","content-length":String(i.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},d=>{let g=[],p=0;d.on("data",h=>{if(p+=h.byteLength,p>Le){d.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(h);}),d.on("error",u),d.on("end",()=>{let h=Buffer.concat(g).toString("utf8"),y;try{y=h?JSON.parse(h):{};}catch{u(new k(d.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let l=d.statusCode??500;if(l<200||l>=300){u(new k(l,String(y.code??"request-failed"),String(y.message??`Content-sync API request failed (${l}).`)));return}o(y);});});c.on("timeout",()=>c.destroy(new Error(`Content-sync API request timed out: ${t}`))),c.on("error",u),c.end(i);})}async head(t){return this.post("head",t)}async unboundHead(t,n){return this.post("head",{postTypes:t,includeSubsites:n})}async events(t){let n=[],r=t.afterSequence,i=new Set(t.postTypes);for(;r<t.throughSequence;){let a=await this.post("events",{...t,afterSequence:r,limit:250});if(a.fromSequence!==r||a.throughSequence!==t.throughSequence||a.nextSequence<r||a.nextSequence>t.throughSequence)throw new Error("Content-sync API returned an inconsistent event page.");if(!Array.isArray(a.items))throw new Error("Content-sync API returned a malformed event page.");let s=r;for(let o of a.items){if(!Number.isSafeInteger(o.sequence)||o.sequence<=s||o.sequence>t.throughSequence||!Number.isSafeInteger(o.postId)||o.postId<=0||!Number.isSafeInteger(o.blogId)||o.blogId<=0||!i.has(o.postType)||typeof o.operation!="string"||!o.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");s=o.sequence;}if(a.items.length>0&&a.nextSequence!==s)throw new Error("Content-sync API event cursor does not match the final returned event.");if(n.push(...a.items),!a.hasMore)break;if(a.nextSequence===r)throw new Error("Content-sync API event pagination did not advance.");r=a.nextSequence;}if(t.throughSequence>t.afterSequence&&n.at(-1)?.sequence!==t.throughSequence)throw new Error("Content-sync API omitted the claimed journal high-water event.");return n}async resolveImpactFamilies(t,n){let r=await this.post("impact",{families:t,includeSubsites:n});return Array.isArray(r.families)?r.families:[]}async releaseFingerprint(t=false){let n=await this.post("fingerprint",{includeSubsites:t}),r=String(n.fingerprint||"").trim();if(!/^[a-f0-9]{64}$/.test(r))throw new Error("WordPress returned an invalid release fingerprint.");return r}async establishBaseline(t){return this.post("baseline",t)}async acknowledge(t){return this.post("ack",t)}};function Be(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Me(e,t){return {...e,...t??{}}}function Ue(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function z(e,t){let n=String(t??e.deploymentTargetOverride??"").trim();if(!Be(e)){if(n)throw new Error(`Deployment target "${n}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:e}}let r=String(n||e.defaultDeploymentProfile||"").trim();if(!r)return {name:null,profile:null,config:e};let i=e.deploymentProfiles?.[r];if(!i)throw new Error(`Unknown deployment profile "${r}". Check the linked WPSuite publisher configuration.`);return {name:r,profile:i,config:{...e,targetOrigin:i.targetOrigin??e.targetOrigin,s3:Me(e.s3,i.s3),cloudFront:Ue(e.cloudFront,i.cloudFront)}}}function fe(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function Ne(e){let t=String(e??"").trim();if(!t)return "";let n=t;if(/^https?:\/\//i.test(n))try{n=new URL(n).pathname;}catch{return ""}if(n=n.split(/[?#]/,1)[0]?.replace(/\\/g,"/").trim()??"",!n)return "";let r=n.endsWith("/"),i=n.replace(/^\/+/,"").split("/").map(s=>s.replace(/[^A-Za-z0-9._-]/g,"")).filter(s=>s.length>0&&s!=="."&&s!=="..");if(i.length===0)return "";let a=`/${i.join("/")}`;return r&&m.extname(a)===""&&(a+="/"),a==="/"?"":a}function H(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t.trim(),String(n??"")]).filter(([t])=>t.length>0))}function _e(e){if(!e||typeof e!="object")return {};let t={};for(let[n,r]of Object.entries(e)){let i=n.trim();if(!i||!r||typeof r!="object")continue;let a=r,s={};if(typeof a.targetOrigin=="string"){let u=fe(a.targetOrigin);u&&(s.targetOrigin=u);}let o=H(a.extraReplacements);if(Object.keys(o).length>0&&(s.extraReplacements=o),a.s3&&typeof a.s3=="object"){let u={},c=a.s3;typeof c.bucket=="string"&&(u.bucket=c.bucket.trim()),typeof c.prefix=="string"&&(u.prefix=c.prefix.trim()),typeof c.region=="string"&&(u.region=c.region.trim()),typeof c.htmlCacheControl=="string"&&(u.htmlCacheControl=c.htmlCacheControl.trim()),typeof c.assetCacheControl=="string"&&(u.assetCacheControl=c.assetCacheControl.trim()),Object.keys(u).length>0&&(s.s3=u);}if(a.cloudFront&&typeof a.cloudFront=="object"){let u={},c=a.cloudFront;typeof c.distributionId=="string"&&(u.distributionId=c.distributionId.trim()),Array.isArray(c.invalidationPaths)&&(u.invalidationPaths=c.invalidationPaths.map(d=>String(d??"").trim()).filter(d=>d.length>0)),Object.keys(u).length>0&&(s.cloudFront=u);}t[i]=s;}return t}function V(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function Ke(e,t){if(!e||typeof e!="object")return null;let n=e,r=n.command;if(r!=="publish"&&r!=="crawl"&&r!=="deploy"&&r!=="invalidate"&&r!=="retry-timeouts"&&r!=="url"&&r!=="content-sync")return null;let i=Number.parseInt(String(n.intervalMinutes??"0"),10);if(!Number.isFinite(i)||i<1)return null;let a=String(n.id??`${r}-${t+1}`).trim();if(!a)return null;let s=String(n.deploymentProfile??"").trim(),o=String(n.url??"").trim(),u=Array.isArray(n.postTypes)?[...new Set(n.postTypes.map(d=>String(d??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],c=Array.isArray(n.listingPaths)?[...new Set(n.listingPaths.map(d=>String(d??"").trim()).filter(Boolean))]:[];return r==="content-sync"&&u.length===0?null:{id:a,enabled:n.enabled!==false,command:r,intervalMinutes:i,...r==="publish"||r==="crawl"?{crawlMode:n.crawlMode==="incremental"?"incremental":"full"}:{},...(r==="publish"||r==="deploy"||r==="invalidate"||r==="content-sync")&&s?{deploymentProfile:s}:{},...o?{url:o}:{},...u.length>0?{postTypes:u}:{},...c.length>0?{listingPaths:c}:{},...typeof n.includeSubsites=="boolean"?{includeSubsites:n.includeSubsites}:{},...typeof n.includePostTypeArchives=="boolean"?{includePostTypeArchives:n.includePostTypeArchives}:{},...typeof n.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:n.includeTaxonomyArchives}:{},...typeof n.includeAuthorArchives=="boolean"?{includeAuthorArchives:n.includeAuthorArchives}:{},...typeof n.includeDateArchives=="boolean"?{includeDateArchives:n.includeDateArchives}:{},...typeof n.includePostsPage=="boolean"?{includePostsPage:n.includePostsPage}:{},...typeof n.includeSitemapChain=="boolean"?{includeSitemapChain:n.includeSitemapChain}:{}}}function ye(e){let t=e&&typeof e=="object"?e:{},n=Array.isArray(t.rules)?t.rules.map((r,i)=>Ke(r,i)).filter(r=>!!r):[];return {enabled:t.enabled===true,timezone:typeof t.timezone=="string"&&t.timezone.trim()!==""?t.timezone.trim():"UTC",rules:n}}function ze(e){if(!e||typeof e!="object")return;let t=e,n={};for(let i of ["accountId","siteId"]){let a=t[i];typeof a=="string"&&a.trim()!==""&&(n[i]=a.trim());}let r=Number(t.lastUpdate??0);return Number.isFinite(r)&&r>0&&(n.lastUpdate=Math.floor(r)),t.subscriber===true&&(n.subscriber=true),Object.keys(n).length>0?n:void 0}function Y(e){let t=e&&typeof e=="object"?e:{},n=String(t.accountId??"").trim(),r=String(t.siteId??"").trim(),i=t.subscriber===true,a=ze(t.siteSettings),s={...a??{},...n?{accountId:a?.accountId??n}:{},...r?{siteId:a?.siteId??r}:{},...a?.subscriber===true||i?{subscriber:true}:{}},o={},u=String(t.apiBase??"").trim();u&&(o.apiBase=u);let c=String(t.runtimeToken??t.nonce??"").trim();c&&(o.runtimeToken=c);let d=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();d&&(o.virtualAssetBaseUrl=d),Object.keys(s).length>0&&(o.siteSettings=s);let g=V(t.subscriptionType);return g&&(o.subscriptionType=g),o}function He(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function be(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function Ge(e,t){let n=new Headers(e instanceof Request?e.headers:void 0);if(t?.headers)for(let[r,i]of new Headers(t.headers).entries())n.set(r,i);return n}async function Se(e,t,n=5){let r=new URL(be(e)),i=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),a=r.protocol==="http:"?Je:me,s=Ge(e,t);return await new Promise((o,u)=>{let c=a.request(r,{method:i,headers:Object.fromEntries(s.entries()),...r.protocol==="https:"?{rejectUnauthorized:false}:{}},d=>{let g=[];d.on("data",p=>{g.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),d.on("error",u),d.on("end",()=>{let p=d.statusCode??0,h=d.headers.location;if(n>0&&h&&[301,302,303,307,308].includes(p)){o(Se(new URL(h,r),{method:p===303?"GET":i},n-1));return}let y=new Headers;for(let[l,b]of Object.entries(d.headers))Array.isArray(b)?y.set(l,b.join(", ")):typeof b=="string"&&y.set(l,b);o(new Response(Buffer.concat(g),{status:p,statusText:d.statusMessage??"",headers:y}));});});c.on("error",u),c.end();})}function Ve(e){let t=Y(e.wpsuite),n={...t.siteSettings??{},...t.siteSettings?.subscriber===true||t.subscriptionType==="PROFESSIONAL"||t.subscriptionType==="AGENCY"?{subscriber:true}:{}},r=String(n.accountId??"").trim(),i=String(n.siteId??"").trim(),a=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();if(e.wpsuite={...t.apiBase?{apiBase:t.apiBase}:{},...t.runtimeToken?{runtimeToken:t.runtimeToken}:{},...a?{virtualAssetBaseUrl:a}:{},...Object.keys(n).length>0?{siteSettings:n}:{},...t.subscriptionType?{subscriptionType:t.subscriptionType}:{}},!r||!i||!a)return false;He(e.sourceOrigin);let s=globalThis,u={...s.WpSuite??{},siteSettings:n,uploadUrl:a};return t.apiBase&&(u.apiBase=t.apiBase),s.WpSuite=u,true}async function Ye(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!Ve(e)){let i=e.wpsuite?.siteSettings,a=[String(i?.accountId??"").trim()?"":"accountId",String(i?.siteId??"").trim()?"":"siteId",t?"":"virtualAssetBaseUrl"].filter(Boolean);return {remote:null,diagnostic:{status:"invalid-runtime",virtualAssetBaseUrl:t,subscriptionType:null,error:`Missing WP Suite runtime field(s): ${a.join(", ")}.`,requests:[]}}}let n=globalThis.fetch,r=[];try{typeof n=="function"&&/^https?:\/\//i.test(t)&&(globalThis.fetch=(async(u,c)=>{let d=be(u);if(d.startsWith(t)){let g=(()=>{try{return new URL(d).pathname}catch{return d}})();try{let p=e.ignoreHttpsErrors&&d.startsWith("https://")?await Se(u,c):await n(u,c);return r.push({path:g,status:p.status,ok:p.ok,error:null}),p}catch(p){throw r.push({path:g,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return n(u,c)}));let{getConfig:i}=await import('@smart-cloud/wpsuite-core'),a=await i("publisher"),s=a&&typeof a=="object"?a:null,o=V(s?.subscriptionType);return {remote:s,diagnostic:{status:s?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:o??null,error:s?null:"WP Suite publisher config loader returned no configuration.",requests:r}}}catch(i){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:t,subscriptionType:null,error:i instanceof Error?i.message:String(i),requests:r}}}finally{typeof n=="function"&&(globalThis.fetch=n);}}function Ze(e,t){let n=_e(t?.deploymentProfiles),r=String(t?.defaultDeploymentProfile??"").trim(),i=n[r]?r:"",a=V(t?.subscriptionType),s=Y(e.wpsuite);return {...e,scheduler:ye(t?.scheduler),deploymentProfiles:n,defaultDeploymentProfile:i,...a?{subscriptionType:a}:{},wpsuite:{...s,...s.siteSettings||a?{siteSettings:{...s.siteSettings??{},...s.siteSettings?.subscriber===true||a==="PROFESSIONAL"||a==="AGENCY"?{subscriber:true}:{}}}:{},...a?{subscriptionType:a}:{}}}}function G(e,t){let r=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!r)return t;let i=r.split("/").map(a=>a.trim()).filter(a=>a.length>0&&a!=="."&&a!=="..");return i.length>0?i.join("/"):t}function Xe(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?m.resolve(e):""}function he(e,t,n){let r=String(t||"").trim();return r&&m.isAbsolute(r)?m.resolve(r):m.resolve(e,G(r,n))}async function Z(e,t={}){let n=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",r=await P.readFile(n,"utf8"),i=JSON.parse(r);i.sourceOrigin=i.sourceOrigin.replace(/\/$/,""),i.targetOrigin=fe(i.targetOrigin),i.ignoreHttpsErrors??=false,i.outputDir=String(i.outputDir||"export").trim()||"export",i.urlRewriteMode||=i.targetOrigin==="."?"relative":"absolute",i.noJavaScriptRenderPathPrefixes||=[],i.seedPaths||=[],i.generated404RequestPath=Ne(i.generated404RequestPath),i.sitemapPaths||=["/sitemap_index.xml","/sitemap.xml"],i.allowedAssetHosts||=[],i.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],i.blockedPathPrefixes||=[],i.blockedSearchFragments||=[],i.concurrency||=1,i.maxPages||=0,i.extraReplacements=H(i.extraReplacements),i.postCrawlCopyMap=H(i.postCrawlCopyMap),i.logDir=String(i.logDir||"logs").trim()||"logs",i.verbose??=false,i.logLevel||=i.verbose?"debug":"info",i.s3SyncMode||="sdk-upload-delete",i.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},i.readiness.timeoutMs??=1500,i.readiness.fallbackWaitMs??=1500,i.viewport||={width:1440,height:1200},i.navigationTimeoutMs||=3e4,i.assetDownloadConcurrency=Number(i.assetDownloadConcurrency)>0?Number(i.assetDownloadConcurrency):i.concurrency,i.rewriteConcurrency=Number(i.rewriteConcurrency)>0?Number(i.rewriteConcurrency):i.assetDownloadConcurrency,i.wpsuite=Y(i.wpsuite),i.scheduler=ye(void 0),i.deploymentProfiles={},i.defaultDeploymentProfile="",i.deploymentTargetOverride=String(i.deploymentTargetOverride??"").trim();let a=await Ye(i);t.onRemotePublisherConfigDiagnostic?.(a.diagnostic);let s=Ze(i,a.remote),o=Xe(),u=o?m.resolve(o,".."):"";return u?(s.outputDir=he(u,s.outputDir,"export"),s.logDir=he(u,s.logDir,"logs")):(m.isAbsolute(s.outputDir)||(s.outputDir=G(s.outputDir,"export")),m.isAbsolute(s.logDir)||(s.logDir=G(s.logDir,"logs"))),s}function Ce(e){let t=String(e.command??"").trim();return (t==="publish"||t==="crawl")&&e.crawlMode==="incremental"||t==="content-sync"}var X=new Map;async function we(e){let t=m.resolve(e),n=X.get(t);if(n!==void 0)return n;try{let r=JSON.parse(await P.readFile(t,"utf8")),i=String(r.version??"").trim();return X.set(t,i),i}catch{return X.set(t,""),""}}var B=class extends Error{request;step;constructor(t,n){super(`Stop requested during ${n}.`),this.name="StopRequestedError",this.request=t,this.step=n;}},st="queue-mutation.lock",ot=5e3,at=3e4;function ut(e){let t=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",n=process.env.STATIC_PUBLISHER_EXPORTER_DIR||process.cwd(),r=process.env.PUBLISHER_CONFIG||"",i=1;for(let o=0;o<e.length;o++){let u=e[o];u==="--runtime-dir"?t=e[++o]||"":u.startsWith("--runtime-dir=")?t=u.slice(14):u==="--exporter-dir"?n=e[++o]||n:u.startsWith("--exporter-dir=")?n=u.slice(15):u==="--config"?r=e[++o]||"":u.startsWith("--config=")?r=u.slice(9):u==="--max-jobs"?i=Number.parseInt(e[++o]||"1",10):u.startsWith("--max-jobs=")&&(i=Number.parseInt(u.slice(11),10));}if(!t)throw new Error("Missing runtime dir. Use --runtime-dir or STATIC_PUBLISHER_RUNTIME_DIR.");let a=m.resolve(t),s=r?m.resolve(r):m.join(a,"config.json");return {runtimeDir:a,exporterDir:m.resolve(n),configPath:s,maxJobs:Number.isFinite(i)&&i>0?i:1}}async function v(e,t){try{let n=await P.readFile(e,"utf8");return JSON.parse(n)}catch(n){if((n&&typeof n=="object"&&"code"in n?String(n.code||""):"")!=="ENOENT"){let i=n instanceof Error?n.message:String(n);console.warn(`[queue-runner] failed to read JSON ${e}: ${i}`);}return t}}async function w(e,t){await P.mkdir(m.dirname(e),{recursive:true}),await P.writeFile(e,JSON.stringify(t,null,2),"utf8");}function Ae(e){return m.join(e,st)}async function ct(e){await new Promise(t=>setTimeout(t,e));}async function lt(e){let t=Ae(e),n=Date.now()+ot;for(;;)try{await P.mkdir(m.dirname(t),{recursive:!0}),await P.writeFile(t,JSON.stringify({pid:process.pid,createdAt:new Date().toISOString()},null,2),{encoding:"utf8",flag:"wx"});return}catch(r){if((r&&typeof r=="object"&&"code"in r?String(r.code||""):"")!=="EEXIST")throw r;let a=await P.stat(t).catch(()=>null);if(a&&Date.now()-a.mtimeMs>at){await P.unlink(t).catch(()=>{});continue}if(Date.now()>=n)throw new Error("Timed out acquiring queue mutation lock.",{cause:r});await ct(50);}}async function dt(e){await P.unlink(Ae(e)).catch(()=>{});}async function E(e,t){await lt(e);try{return await t()}finally{await dt(e);}}function Ie(e){return m.join(e,"queue-runner-heartbeat.json")}function ae(e){return m.join(e,"current-progress.json")}function ee(e){return m.join(e,"scheduler-state.json")}function gt(e,t){let r=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!r)return t;let i=r.split("/").map(a=>a.trim()).filter(a=>a.length>0&&a!=="."&&a!=="..");return i.length>0?i.join("/"):t}async function Re(e){let t=await v(e.configPath||"",null),n=m.resolve(e.runtimeDir,".."),i=(typeof t?.logDir=="string"?t.logDir:"").trim();return i&&m.isAbsolute(i)?m.resolve(i):m.resolve(n,gt(i,"logs"))}function te(e,t){return String(e||"").trim().replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/^-+|-+$/g,"")||t}function pt(e){let t=e?new Date(e):new Date;return (Number.isNaN(t.getTime())?new Date:t).toISOString().replace(/[-:]/g,"").replace(/\.\d{3}Z$/,"Z")}function mt(e){return [pt(e.endedAt||e.startedAt),te(e.command||"job","job"),te(e.id||"job","job"),te(e.status||"finished","finished")].join("-")}function ht(e){return e.endsWith(".log.jsonl")||e.endsWith(".errors.jsonl")||e.endsWith(".rejected.jsonl")?true:["current-crawl-event.json","rejected.jsonl","ignored.jsonl","skipped-http.jsonl","errors.jsonl","timings.jsonl","rejected.json","ignored.json","skipped-http.json","errors.json","timings.json"].includes(e)}function ft(e){return e==="errors.json"||e==="errors.jsonl"||e.endsWith(".errors.jsonl")?"errors":e==="ignored.json"||e==="ignored.jsonl"?"ignored":e==="rejected.json"||e==="rejected.jsonl"||e.endsWith(".rejected.jsonl")?"rejected":e==="skipped-http.json"||e==="skipped-http.jsonl"?"skipped-http":e==="timings.json"||e==="timings.jsonl"?"timings":e==="current-progress.json"?"current-progress":e==="current-crawl-event.json"?"current-crawl-event":e.endsWith(".log.jsonl")?e.replace(/\.log\.jsonl$/i,"-log"):e.replace(/\.[^.]+$/u,"")||"artifact"}function yt(e){return e.endsWith(".gz")?"application/gzip":e.endsWith(".jsonl")?"application/x-ndjson":e.endsWith(".json")?"application/json":"text/plain"}async function ne(e,t,n){let r=`${n}.gz`,i=m.join(t,r);await pipeline(createReadStream(e),createGzip({level:9}),createWriteStream(i));let[a,s]=await Promise.all([P.stat(e),P.stat(i)]);return {role:ft(n),originalFileName:n,storedFileName:r,compressed:true,compression:"gzip",contentType:yt(r),originalSize:a.size,storedSize:s.size}}async function $(e,t){let n=await Re(t),r=new Date().toISOString(),i=mt(e),a=m.join(n,"archive",i),s=[],o=[];await P.mkdir(a,{recursive:true});try{let d=await P.readdir(n,{withFileTypes:!0});for(let g of d){if(!g.isFile()||!ht(g.name))continue;let p=await ne(m.join(n,g.name),a,g.name);o.push(p),s.push(p.storedFileName);}}catch(d){if((d&&typeof d=="object"&&"code"in d?String(d.code||""):"")!=="ENOENT")throw d}let u=ae(t.runtimeDir);if(existsSync(u)){let d=await ne(u,a,"current-progress.json");o.push(d),s.push(d.storedFileName);}if(e.command==="content-sync")for(let d of ["content-sync-current.json","content-sync-impact-plan.json","content-sync-checkpoint.json","content-sync-state.json","content-sync-invalidation.json","deploy-plan.json"]){let g=m.join(t.runtimeDir,d);if(!existsSync(g))continue;let p=await ne(g,a,d);o.push(p),s.push(p.storedFileName);}let c=[...s,"job.json"];return await w(m.join(a,"job.json"),{manifestVersion:1,archivedAt:r,archiveKey:i,archiveDir:a,logDir:n,runtimeDir:t.runtimeDir,exporterDir:t.exporterDir,configPath:t.configPath||"",archivedFiles:c,artifacts:o,job:e}),{archiveKey:i,archiveDir:a,archiveCreatedAt:r,archivedFiles:c,artifacts:o}}function bt(e){return m.join(e,"audit-events.jsonl")}function se(e){return m.join(e,"stop-request.json")}async function qe(e){try{let t=await P.readFile(se(e),"utf8"),n=JSON.parse(t);return n&&typeof n=="object"?n:null}catch{return null}}async function ve(e,t){let n=await qe(e);if(!n)return null;let r=String(n.targetJobId||"").trim();return r&&r!==t?null:n}async function O(e,t){if(!t){await P.unlink(se(e)).catch(()=>{});return}let n=await qe(e);if(!n)return;let r=String(n.targetJobId||"").trim();(!r||r===t)&&await P.unlink(se(e)).catch(()=>{});}async function I(e,t){try{let n={occurredAt:t.occurredAt||new Date().toISOString(),status:t.status||"info",actorSource:t.actorSource||"queue-runner",...t};await P.mkdir(e,{recursive:!0}),await P.appendFile(bt(e),`${JSON.stringify(n)}
2
+ `,"utf8");}catch{}}async function C(e,t,n){let[r,i]=await Promise.all([we(m.join(e.exporterDir,"package.json")),we(m.join(e.exporterDir,"node_modules","@smart-cloud","wpsuite-core","package.json"))]),a={checkedAt:new Date().toISOString(),status:t,pid:process.pid,nodePath:process.execPath,nodeVersion:process.version,exporterVersion:r,wpsuiteCoreVersion:i,runtimeDir:e.runtimeDir,exporterDir:e.exporterDir,...n??{}};try{await w(Ie(e.runtimeDir),a);}catch{}}async function L(e){try{await P.unlink(ae(e));}catch{}}function St(e,t){let n=e.command;if(n!=="publish"&&n!=="crawl"&&n!=="deploy"&&n!=="invalidate"&&n!=="retry-timeouts"&&n!=="url"&&n!=="content-sync")return null;let r=Number.parseInt(String(e.intervalMinutes??"0"),10);if(!Number.isFinite(r)||r<1)return null;let i=(e.id||`${n}-${t+1}`).trim();if(!i)return null;let a=e.enabled!==false,s=typeof e.url=="string"?e.url.trim():"",o=typeof e.deploymentProfile=="string"?e.deploymentProfile.trim():"",u=Array.isArray(e.postTypes)?[...new Set(e.postTypes.map(g=>String(g).trim()).filter(Boolean))]:[],c=Array.isArray(e.listingPaths)?[...new Set(e.listingPaths.map(g=>String(g).trim()).filter(Boolean))]:[],d=(n==="publish"||n==="crawl")&&e.crawlMode==="incremental"?"incremental":"full";return n==="url"&&!s||n==="content-sync"&&u.length===0?null:{id:i,enabled:a,command:n,intervalMinutes:r,...n==="publish"||n==="crawl"?{crawlMode:d}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&o?{deploymentProfile:o}:{},...s?{url:s}:{},...u.length>0?{postTypes:u}:{},...c.length>0?{listingPaths:c}:{},...typeof e.includeSubsites=="boolean"?{includeSubsites:e.includeSubsites}:{},...typeof e.includePostTypeArchives=="boolean"?{includePostTypeArchives:e.includePostTypeArchives}:{},...typeof e.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:e.includeTaxonomyArchives}:{},...typeof e.includeAuthorArchives=="boolean"?{includeAuthorArchives:e.includeAuthorArchives}:{},...typeof e.includeDateArchives=="boolean"?{includeDateArchives:e.includeDateArchives}:{},...typeof e.includePostsPage=="boolean"?{includePostsPage:e.includePostsPage}:{},...typeof e.includeSitemapChain=="boolean"?{includeSitemapChain:e.includeSitemapChain}:{}}}async function wt(e){let t=await Z(e.configPath).catch(()=>null),n=t?.scheduler,r=Array.isArray(n?.rules)?n.rules.map((i,a)=>St(i,a)).filter(i=>!!i):[];return {enabled:!!n?.enabled,timezone:typeof n?.timezone=="string"&&n.timezone.trim()?n.timezone.trim():"UTC",rules:r,wpsuite:t?.wpsuite,publisherConfig:t}}function re(e,t){if(!t)throw new Error("Content-sync scheduler requires a valid publisher config.");let n=M({postTypes:e.postTypes??[],listingPaths:e.listingPaths??[],includeSubsites:e.includeSubsites,includePostTypeArchives:e.includePostTypeArchives,includeTaxonomyArchives:e.includeTaxonomyArchives,includeAuthorArchives:e.includeAuthorArchives,includeDateArchives:e.includeDateArchives,includePostsPage:e.includePostsPage,includeSitemapChain:e.includeSitemapChain}),r=_(t.sourceOrigin,e.id,n),i=z(t,e.deploymentProfile),a=K({sourceOrigin:t.sourceOrigin,targetOrigin:i.config.targetOrigin,deploymentProfile:i.name||"",s3Bucket:i.config.s3.bucket,s3Prefix:i.config.s3.prefix,cloudFrontDistributionId:i.config.cloudFront.distributionId,urlRewriteMode:i.config.urlRewriteMode,extraReplacements:i.profile?.extraReplacements??{}});return `content-sync:${e.id}:${J({scopeFingerprint:r,targetFingerprint:a}).slice(0,32)}`}async function vt(e,t,n,r,i){let s=(await v(m.join(e.runtimeDir,"content-sync-baseline.json"),{})).entries?.[r];if(!s?.consumerId||!s.scopeFingerprint||!s.baselineId)return {status:"baseline-required",reason:"No verified normal-release baseline exists for this rule scope."};try{let o=t.publisherConfig;if(!o)throw new Error("Publisher configuration is unavailable.");let u=M({postTypes:n.postTypes??[],listingPaths:n.listingPaths??[],includeSubsites:n.includeSubsites,includePostTypeArchives:n.includePostTypeArchives,includeTaxonomyArchives:n.includeTaxonomyArchives,includeAuthorArchives:n.includeAuthorArchives,includeDateArchives:n.includeDateArchives,includePostsPage:n.includePostsPage,includeSitemapChain:n.includeSitemapChain}),c=_(o.sourceOrigin,n.id,u),d=z(o,n.deploymentProfile),g=K({sourceOrigin:o.sourceOrigin,targetOrigin:d.config.targetOrigin,deploymentProfile:d.name||"",s3Bucket:d.config.s3.bucket,s3Prefix:d.config.s3.prefix,cloudFrontDistributionId:d.config.cloudFront.distributionId,urlRewriteMode:d.config.urlRewriteMode,extraReplacements:d.profile?.extraReplacements??{}});if(s.scopeFingerprint!==c||s.targetFingerprint!==g||!s.releaseFingerprint)return {status:"baseline-required",reason:"The verified content-sync baseline does not match the active scope or deployment target. Run a successful full or incremental publish to establish a new baseline."};let p=new U({sourceOrigin:o.sourceOrigin,runtimeToken:String(o.wpsuite?.runtimeToken||""),ignoreHttpsErrors:o.ignoreHttpsErrors}),h=await p.releaseFingerprint(u.includeSubsites),y=de({wordpressFingerprint:h,targetFingerprint:g,sourceOrigin:o.sourceOrigin,sitemapPaths:o.sitemapPaths,blockedPathPrefixes:o.blockedPathPrefixes,assetPathPrefixes:o.assetPathPrefixes,noJavaScriptRenderPathPrefixes:o.noJavaScriptRenderPathPrefixes,urlRewriteMode:o.urlRewriteMode});if(s.releaseFingerprint!==y)return {status:"baseline-required",reason:"The installed WordPress release changed after the last verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline."};let l=await p.head({consumerId:s.consumerId,scopeFingerprint:s.scopeFingerprint,baselineId:s.baselineId,postTypes:u.postTypes,includeSubsites:u.includeSubsites}),b=l.committedSequence;if(i?.command==="content-sync"&&i.coalesceKey===r){let S=await v(m.join(e.runtimeDir,"content-sync-current.json"),null);S?.coalesceKey===r&&Number.isSafeInteger(S.toSequence)&&(b=Math.max(b,Number(S.toSequence)));}return {status:l.headSequence>b?"pending":"current",headSequence:l.headSequence,committedSequence:l.committedSequence}}catch(o){return o instanceof k&&o.code==="baseline-required"?{status:"baseline-required",reason:o.message}:{status:"unavailable",reason:o instanceof Error?o.message:String(o)}}}async function De(e,t,n,r,i,a){let s=m.join(e.runtimeDir,"content-sync-state.json"),u=(await v(s,{})).rules??{},c=u[n]??{};u[n]={...c,ruleId:t,coalesceKey:n,...Number.isSafeInteger(i)?{observedHeadSequence:i}:{},...Number.isSafeInteger(a)?{committedSequence:a}:{},retryAttempt:0,nextRetryAt:null,lastError:r,baselineStatus:"required",baselineReason:r,trailingWorkDetected:Number.isSafeInteger(i)&&Number.isSafeInteger(a)?Number(i)>Number(a):!!c.trailingWorkDetected},await w(s,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:u});}function oe(e,t){let n=(t.url||"").trim(),r=t.crawlMode||"full",i=(t.deploymentProfile||"").trim(),a=(t.coalesceKey||"").trim();return e.some(s=>s.command===t.command&&(t.command!=="content-sync"||(s.coalesceKey||"").trim()===a)&&(s.url||"").trim()===n&&(s.crawlMode||"full")===r&&(s.deploymentProfile||"").trim()===i&&(s.status===void 0||s.status==="queued"||s.status==="retry-wait"||s.status==="running"))}async function Pt(e,t,n){if(!t.allowSchedulerAutoEnqueue)return 0;let r=await wt(e);if(!r.enabled||r.rules.length===0)return 0;if(t.assertActiveSubscriptionForIdentity)try{await t.assertActiveSubscriptionForIdentity(r.wpsuite??null);}catch(l){let b=l instanceof Error?l.message:String(l);return console.warn(`[queue-runner] scheduler auto-enqueue skipped: ${b}`),0}let i=m.join(e.runtimeDir,"queue.json"),a=m.join(e.runtimeDir,"current-run.json"),s=Date.now(),o=0,u=[],c=[],d=[],g=new Map,[p,h,y]=await Promise.all([v(i,[]),v(a,null),v(ee(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}})]);for(let l of r.rules){if(!l.enabled||l.command!=="content-sync")continue;let b=Math.floor(s/(l.intervalMinutes*60*1e3)),S=y.lastEvaluatedBucketByRuleId?.[l.id]??y.lastEnqueuedBucketByRuleId?.[l.id]??-1;if(b<=S)continue;let D=re(l,r.publisherConfig);oe(p,{command:l.command,deploymentProfile:l.deploymentProfile,coalesceKey:D})||g.set(l.id,await vt(e,r,l,D,h));}await E(e.runtimeDir,async()=>{let l=await v(i,[]),b=await v(a,null),S=await v(ee(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}});S.lastEnqueuedBucketByRuleId??={},S.lastEvaluatedBucketByRuleId??={...S.lastEnqueuedBucketByRuleId},S.lastCreatedBucketByRuleId??={...S.lastEnqueuedBucketByRuleId},S.coalescedCountByRuleId??={};let D=[...l];b&&b.status==="running"&&D.unshift(b);for(let f of r.rules){if(!f.enabled)continue;let A=f.intervalMinutes*60*1e3,q=Math.floor(s/A),F=S.lastEvaluatedBucketByRuleId[f.id]??S.lastEnqueuedBucketByRuleId[f.id]??-1;if(q<=F)continue;let j=f.command==="content-sync"?re(f,r.publisherConfig):"";S.lastEvaluatedBucketByRuleId[f.id]=q;let T=f.command==="content-sync"?l:D;if(oe(T,{command:f.command,url:f.url,crawlMode:f.crawlMode,deploymentProfile:f.deploymentProfile,coalesceKey:j})){S.coalescedCountByRuleId[f.id]=(S.coalescedCountByRuleId[f.id]??0)+1,c.push(f);continue}if(f.command==="content-sync"){let W=g.get(f.id);if(!W||W.status!=="pending"){let ce=W??{status:"unavailable",reason:"Content-sync demand was not checked."};ce.status!=="current"&&d.push({rule:f,check:ce});continue}}let Q={id:randomUUID(),command:f.command,...f.command==="content-sync"?{ruleId:f.id,coalesceKey:j,attempt:0}:{},...(f.command==="publish"||f.command==="crawl")&&f.crawlMode?{crawlMode:f.crawlMode}:{},...(f.command==="publish"||f.command==="deploy"||f.command==="invalidate"||f.command==="content-sync")&&f.deploymentProfile?{deploymentProfile:f.deploymentProfile}:{},...f.url?{url:f.url}:{},enqueueSource:"scheduler",...r.wpsuite?{wpsuite:r.wpsuite}:{},status:"queued",createdAt:new Date().toISOString(),createdBy:0};l.push(Q),D.push(Q),u.push({job:Q,rule:f}),S.lastEnqueuedBucketByRuleId[f.id]=q,S.lastCreatedBucketByRuleId[f.id]=q,o+=1;}o>0&&await w(i,l),await w(ee(e.runtimeDir),S);});for(let l of u){if(l.rule.command==="content-sync"){let b=g.get(l.rule.id);await I(e.runtimeDir,{eventType:"content-sync-demand-detected",status:"queued",actorSource:"queue-runner-scheduler",jobId:l.job.id,command:l.job.command,message:"Pending journal changes require a content-sync job.",details:{ruleId:l.rule.id,headSequence:b?.headSequence??0,committedSequence:b?.committedSequence??0}});}await I(e.runtimeDir,{eventType:"job-created",status:"queued",actorSource:"queue-runner-scheduler",jobId:l.job.id,command:l.job.command,message:"Scheduler auto-enqueued a job.",details:{ruleId:l.rule.id,intervalMinutes:l.rule.intervalMinutes,timezone:r.timezone,deploymentProfile:l.rule.deploymentProfile||"",url:l.rule.url||""}});}for(let l of c)await I(e.runtimeDir,{eventType:"content-sync-coalesced",status:"queued",actorSource:"queue-runner-scheduler",command:l.command,message:"Scheduler demand was coalesced into an existing content-sync job.",details:{ruleId:l.id}});for(let l of d){if(l.check.status==="baseline-required"){let b=re(l.rule,r.publisherConfig);await De(e,l.rule.id,b,l.check.reason||"A successful full or incremental publish must establish a new content-sync baseline.",l.check.headSequence,l.check.committedSequence);}await I(e.runtimeDir,{eventType:l.check.status==="baseline-required"?"content-sync-baseline-required":"content-sync-demand-check-failed",status:l.check.status==="unavailable"?"failed":"info",actorSource:"queue-runner-scheduler",command:"content-sync",message:l.check.reason||"The content-sync consumer is already at the current journal head.",details:{ruleId:l.rule.id,headSequence:l.check.headSequence??0,committedSequence:l.check.committedSequence??0}});}return o}async function Ct(e){let t={pid:process.pid,startedAt:new Date().toISOString()};await P.writeFile(e,JSON.stringify(t,null,2),{encoding:"utf8",flag:"wx"});}async function At(e){try{await P.unlink(e);}catch{}}function It(e,t){let n=[m.join(e,"dist",`${t}.js`),m.join(e,`${t}.js`)];for(let r of n)if(existsSync(r))return r;throw new Error(`Cannot find ${t}.js in ${m.join(e,"dist")} or ${e}`)}async function R(e,t,n,r,i,a,s){if(s){let c=await ve(t,s);if(c)throw new B(c,n)}let o=It(e,n),u={...process.env,...a??{}};return u.STATIC_PUBLISHER_RUNTIME_DIR=t,i&&(u.PUBLISHER_CONFIG=i),await new Promise((c,d)=>{let g=null,p=null,h=null,y=null,l=false,b="",S=spawn(process.execPath,[o,...r],{cwd:e,env:u,stdio:["inherit","inherit","pipe"]});S.stderr?.on("data",A=>{b+=A.toString("utf8");});let D=()=>{p&&clearInterval(p),h&&clearInterval(h),y&&clearTimeout(y);},f=async()=>{if(!(!s||g||l)){l=true;try{let A=await ve(t,s);if(!A)return;g=A,S.kill("SIGTERM"),y=setTimeout(()=>{S.kill("SIGKILL");},1e4);}finally{l=false;}}};s&&(p=setInterval(()=>{f();},1e3),h=setInterval(()=>{(async()=>{let A=Ie(t),q=await v(A,{});await w(A,{...q,checkedAt:new Date().toISOString(),status:"running",currentJobId:s||q.currentJobId||"",currentStep:n});})().catch(()=>{});},15e3)),S.on("error",A=>{D(),d(A);}),S.on("close",(A,q)=>{D();let F=pe(b).trim();if(F&&process.stderr.write(`${F}
3
+ `),g){d(new B(g,n));return}if(A===0)c();else {let j=F.split(/\r?\n/).map(T=>T.trim()).find(T=>T&&!T.startsWith("at "));d(new Error(`${n} exited with code ${A??-1}${q?` (signal ${q})`:""}${j?`: ${j}`:""}`));}});}),0}function Rt(e){let t=e.awsTempCreds;if(!t)return {};let n={};return typeof t.accessKeyId=="string"&&t.accessKeyId.trim()!==""&&(n.AWS_ACCESS_KEY_ID=t.accessKeyId.trim()),typeof t.secretAccessKey=="string"&&t.secretAccessKey.trim()!==""&&(n.AWS_SECRET_ACCESS_KEY=t.secretAccessKey.trim()),typeof t.sessionToken=="string"&&t.sessionToken.trim()!==""&&(n.AWS_SESSION_TOKEN=t.sessionToken.trim()),n}async function qt(e,t){let n=s=>{if(!s||typeof s!="object")return null;let o=s,u={...o.siteSettings??{},...String(o.accountId??"").trim()&&!o.siteSettings?.accountId?{accountId:String(o.accountId).trim()}:{},...String(o.siteId??"").trim()&&!o.siteSettings?.siteId?{siteId:String(o.siteId).trim()}:{},...o.siteSettings?.subscriber===true||o.subscriber===true?{subscriber:true}:{}},c={...String(o.apiBase??"").trim()?{apiBase:String(o.apiBase).trim()}:{},...String(o.runtimeToken??o.nonce??"").trim()?{runtimeToken:String(o.runtimeToken??o.nonce).trim()}:{},...String(o.virtualAssetBaseUrl??o.uploadUrl??"").trim()?{virtualAssetBaseUrl:String(o.virtualAssetBaseUrl??o.uploadUrl).trim()}:{},...Object.keys(u).length>0?{siteSettings:u}:{},...o.subscriptionType?{subscriptionType:o.subscriptionType}:{}};return Object.keys(c).length>0?c:null},r=await Z(t.configPath).catch(()=>null),i=n(r?.wpsuite);if(i)return i;let a=n(e.wpsuite);return a||null}async function Dt(e,t){return await qt(e,t)}function ue(e){let t={...e,status:"queued"};return delete t.startedAt,delete t.endedAt,delete t.exitCode,delete t.error,delete t.stopRequestedAt,delete t.stopRequestedByUserId,delete t.stopRequestedByLogin,delete t.stopMode,delete t.stoppedStep,t}async function xt(e,t){let n=await v(ae(e.runtimeDir),null),r=n&&n.details&&typeof n.details.phase=="string"?n.details.phase.trim():"";if(r)return r;let i=n&&typeof n.currentStep=="string"?n.currentStep.trim():"";if(i)return i;let a=await Re(e).catch(()=>"");if(a){let s=await v(m.join(a,"current-crawl-event.json"),null),o=s&&typeof s.currentStep=="string"?s.currentStep.trim():"";if(o)return o}return t}function Tt(e,t){if((e.command==="publish"||e.command==="crawl")&&t==="rewrite-text")return "rewrite-text"}async function kt(e){let t=m.join(e.runtimeDir,"queue.json"),n=m.join(e.runtimeDir,"current-run.json"),r=await E(e.runtimeDir,async()=>{let i=await v(n,null);if(!i||i.status!=="running"&&i.status!=="queued")return null;let a=await v(t,[]),s=Array.isArray(a)?a.filter(u=>u?.id!==i.id):[],o=ue(i);return await w(t,[o,...s]),await w(n,null),o});r&&(await O(e.runtimeDir,r.id),await I(e.runtimeDir,{eventType:"job-recovered",status:"queued",actorSource:"queue-runner",jobId:r.id,command:r.command,message:"Recovered stale current-run entry back into queue."}));}async function Et(e,t,n){try{if((n.shouldEnforceSubscriptionOnJobExecution?.(e)??n.enforceSubscriptionOnJobExecution)&&n.assertActiveSubscriptionForIdentity){let o=await Dt(e,t);await n.assertActiveSubscriptionForIdentity(o);}let i=Rt(e),a=e.resumeFromStep==="rewrite-text"?["--resume-rewrite"]:e.crawlMode==="incremental"?["--crawl-mode","incremental"]:[],s=e.deploymentProfile?["--profile",e.deploymentProfile]:[];if(e.command==="publish")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-release-cutoff"}),await R(t.exporterDir,t.runtimeDir,"content-sync",["--capture-baseline-cutoffs",e.id],t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:e.resumeFromStep==="rewrite-text"?"rewrite-text":"crawl"}),await R(t.exporterDir,t.runtimeDir,"crawl",a,t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await R(t.exporterDir,t.runtimeDir,"deploy",s,t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await R(t.exporterDir,t.runtimeDir,"invalidate",s,t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-baseline"}),await R(t.exporterDir,t.runtimeDir,"content-sync",["--establish-baselines",e.id],t.configPath,i,e.id),{exitCode:0};if(e.command==="crawl")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:e.resumeFromStep==="rewrite-text"?"rewrite-text":"crawl"}),await R(t.exporterDir,t.runtimeDir,"crawl",a,t.configPath,i,e.id),{exitCode:0};if(e.command==="deploy")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await R(t.exporterDir,t.runtimeDir,"deploy",s,t.configPath,i,e.id),{exitCode:0};if(e.command==="invalidate")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await R(t.exporterDir,t.runtimeDir,"invalidate",s,t.configPath,i,e.id),{exitCode:0};if(e.command==="retry-timeouts")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"retry-timeouts"}),await R(t.exporterDir,t.runtimeDir,"crawl",["--retry-timeouts"],t.configPath,i,e.id),{exitCode:0};if(e.command==="url"){let o=(e.url||"").trim();return o?(await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"url"}),await R(t.exporterDir,t.runtimeDir,"crawl",["--url",o],t.configPath,i,e.id),{exitCode:0}):{exitCode:2,error:"Missing url for command 'url'"}}return e.command==="content-sync"?!e.ruleId||!e.coalesceKey?{exitCode:2,error:"Content-sync queue jobs require ruleId and coalesceKey."}:(await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync"}),await R(t.exporterDir,t.runtimeDir,"content-sync",["--job-id",e.id],t.configPath,i,e.id),{exitCode:0}):{exitCode:2,error:`Unsupported command: ${e.command}`}}catch(r){return r instanceof B?{exitCode:130,error:"Job stop requested.",stopped:true,stopRequest:r.request,stoppedStep:r.step}:{exitCode:1,error:r instanceof Error?r.message:String(r)}}}function xe(e){return async function(n=process.argv.slice(2)){let r=ut(n),i=m.join(r.runtimeDir,"export.lock"),a=m.join(r.runtimeDir,"last-run.json");await L(r.runtimeDir),await C(r,"starting",{message:"queue-runner starting"});try{await Ct(i);}catch(s){if((s&&typeof s=="object"&&"code"in s?String(s.code||""):"")!=="EEXIST")throw s;console.log("[queue-runner] lock active, skipping"),await C(r,"lock-active",{message:"lock active, skipped this cron tick"});return}try{await kt(r);let s=await Pt(r,e),o=0;for(let u=0;u<r.maxJobs;u++){let c=await Ot(r,e);if(c==="none"||(o+=1,c==="stopped"))break}if(o===0)await L(r.runtimeDir),await C(r,"idle",{processedJobs:o,schedulerEnqueued:s,message:"no queued jobs"});else {await L(r.runtimeDir);let u=await v(a,null),c=String(u?.status||"").trim();await C(r,c==="success"?"job-success":c==="stopped"?"job-stopped":"job-failed",{processedJobs:o,schedulerEnqueued:s,lastJobId:u?.id,lastJobCommand:u?.command,lastJobStatus:u?.status,lastJobExitCode:u?.exitCode,lastJobError:u?.error,...c==="stopped"?{currentStep:u?.stoppedStep,stopRequestedAt:u?.stopRequestedAt,stopRequestedByLogin:u?.stopRequestedByLogin,stopRequestedMode:u?.stopMode,lastStoppedStep:u?.stoppedStep,message:u?.stopMode==="requeue"?`Job stopped during ${u?.stoppedStep||u?.command||"active step"} and requeued.`:`Job stopped during ${u?.stoppedStep||u?.command||"active step"} and left out of queue.`}:{}});}}catch(s){let o=s instanceof Error?s.message:String(s);throw await C(r,"error",{message:o}),await I(r.runtimeDir,{eventType:"queue-runner-error",status:"failed",actorSource:"queue-runner",message:"Unhandled queue-runner error.",details:{error:o}}),s}finally{await At(i);}}}async function Ft(e,t,n){let r=m.join(e.runtimeDir,"queue.json"),i=m.join(e.runtimeDir,"current-run.json");await E(e.runtimeDir,async()=>{let a=await v(r,[]),s=Array.isArray(a)?a.filter(c=>c?.id!==t.id):[],o=ue(t),u=Tt(t,n);u?o.resumeFromStep=u:delete o.resumeFromStep,await w(r,[o,...s]),await w(i,null);});}async function jt(e,t,n){let r=Math.max(0,t.attempt??0)+1,i=Math.min(3600*1e3,6e4*2**(r-1)),a=Math.floor(i*.2*Math.random()),s=new Date(Date.now()+i+a).toISOString(),o={...ue(t),status:"retry-wait",attempt:r,nextAttemptAt:s,error:n};if(await E(e.runtimeDir,async()=>{let u=m.join(e.runtimeDir,"queue.json"),c=m.join(e.runtimeDir,"current-run.json"),g=(await v(u,[])).filter(p=>p.id!==t.id);await w(u,[o,...g]),await w(c,null);}),t.coalesceKey){let u=m.join(e.runtimeDir,"content-sync-state.json"),d=(await v(u,{})).rules??{};d[t.coalesceKey]={...d[t.coalesceKey]??{},ruleId:t.ruleId||"",coalesceKey:t.coalesceKey,retryAttempt:r,nextRetryAt:s,lastError:n,trailingWorkDetected:true},await w(u,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:d});}return o}async function Jt(e,t){if(!t.coalesceKey||!t.ruleId||!(await v(m.join(e.runtimeDir,"content-sync-state.json"),null))?.rules?.[t.coalesceKey]?.trailingWorkDetected)return null;let r=null;await E(e.runtimeDir,async()=>{let a=m.join(e.runtimeDir,"queue.json"),s=await v(a,[]);oe(s,{command:"content-sync",coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile})||(r={id:randomUUID(),command:"content-sync",ruleId:t.ruleId,coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile,enqueueSource:"scheduler",wpsuite:t.wpsuite,status:"queued",attempt:0,createdAt:new Date().toISOString(),createdBy:0},await w(a,[...s,r]));});let i=r;return i&&await I(e.runtimeDir,{eventType:"content-sync-trailing-job-created",status:"queued",actorSource:"queue-runner",jobId:i.id,command:i.command,message:"Content changes after the stable cutoff created one trailing job.",details:{predecessorJobId:t.id,ruleId:t.ruleId,coalesceKey:t.coalesceKey}}),i}async function Ot(e,t){let n=m.join(e.runtimeDir,"queue.json"),r=m.join(e.runtimeDir,"current-run.json"),i=m.join(e.runtimeDir,"last-run.json"),a=new Date().toISOString(),s=await E(e.runtimeDir,async()=>{let g=await v(n,[]);if(!Array.isArray(g)||g.length===0)return null;let p=Date.parse(String(g[0]?.nextAttemptAt||""));if(Number.isFinite(p)&&p>Date.now())return null;let h={...g[0],status:"running",startedAt:a};return delete h.nextAttemptAt,await w(n,g.slice(1)),await w(r,h),h});if(!s)return "none";await L(e.runtimeDir),await I(e.runtimeDir,{eventType:"job-run-started",status:"running",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Job execution started.",details:{createdAt:s.createdAt||"",startedAt:a,createdBy:s.createdBy??null,deploymentProfile:s.deploymentProfile||"",queuedWithTempAwsCreds:!!s.awsTempCreds}}),await C(e,"running",{currentJobId:s.id,currentJobCommand:s.command,currentStep:s.command});let o=await Et(s,e,t),u=new Date().toISOString();if(o.stopped){let g=await xt(e,o.stoppedStep||s.command),p=o.stopRequest?.mode==="requeue"?"requeue":"stop",h={...s,status:"stopped",endedAt:u,exitCode:o.exitCode,...o.error?{error:o.error}:{},stopRequestedAt:o.stopRequest?.requestedAt||"",stopRequestedByUserId:typeof o.stopRequest?.requestedByUserId=="number"?o.stopRequest.requestedByUserId:null,stopRequestedByLogin:o.stopRequest?.requestedByLogin||"",stopMode:p,stoppedStep:g};p==="requeue"?await Ft(e,s,g):await w(r,null);try{let y=await $(h,e);h.logArchiveDir=y.archiveDir,h.logArchiveCreatedAt=y.archiveCreatedAt,h.logArchiveFileCount=y.archivedFiles.length;}catch(y){h.logArchiveError=y instanceof Error?y.message:String(y);}return await w(i,h),await O(e.runtimeDir,s.id),await L(e.runtimeDir),await I(e.runtimeDir,{eventType:"job-run-stopped",status:"stopped",actorSource:"queue-runner",jobId:s.id,command:s.command,message:p==="requeue"?"Job stop requested and requeued.":"Job stop requested and removed from active execution without requeue.",details:{startedAt:s.startedAt||"",endedAt:u,deploymentProfile:s.deploymentProfile||"",stopMode:p,stopRequestedAt:o.stopRequest?.requestedAt||"",stopRequestedByLogin:o.stopRequest?.requestedByLogin||"",stopRequestedByUserId:typeof o.stopRequest?.requestedByUserId=="number"?o.stopRequest.requestedByUserId:null,stoppedStep:g,logArchiveKey:h.logArchiveError||!h.logArchiveDir?"":m.basename(h.logArchiveDir),logArchiveDir:h.logArchiveDir||"",logArchiveCreatedAt:h.logArchiveCreatedAt||"",logArchiveFileCount:h.logArchiveFileCount??0,logArchiveError:h.logArchiveError||""}}),"stopped"}if(s.command==="content-sync"&&o.exitCode!==0){if(/baseline (?:is missing or stale|required)|verified content-sync baseline|new baseline/i.test(o.error||"")){let y="The installed release no longer matches the verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline.";s.ruleId&&s.coalesceKey&&await De(e,s.ruleId,s.coalesceKey,y);let l={...s,status:"failed",endedAt:u,exitCode:o.exitCode,error:y};try{let b=await $(l,e);l.logArchiveDir=b.archiveDir,l.logArchiveCreatedAt=b.archiveCreatedAt,l.logArchiveFileCount=b.archivedFiles.length;}catch(b){l.logArchiveError=b instanceof Error?b.message:String(b);}return await w(i,l),await O(e.runtimeDir,s.id),await I(e.runtimeDir,{eventType:"content-sync-baseline-required",status:"failed",actorSource:"queue-runner",jobId:s.id,command:s.command,message:y,details:{ruleId:s.ruleId||"",retryScheduled:false}}),"processed"}await I(e.runtimeDir,{eventType:"content-sync-failed",status:"failed",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Content-sync execution failed before cursor acknowledgement.",details:{attempt:s.attempt??0,error:o.error||"Content-sync execution failed."}});let p=await jt(e,s,o.error||"Content-sync execution failed."),h={...s,status:"retry-wait",endedAt:u,exitCode:o.exitCode,error:p.error,attempt:p.attempt,nextAttemptAt:p.nextAttemptAt};try{let y=await $(h,e);h.logArchiveDir=y.archiveDir,h.logArchiveCreatedAt=y.archiveCreatedAt,h.logArchiveFileCount=y.archivedFiles.length;}catch(y){h.logArchiveError=y instanceof Error?y.message:String(y);}return await w(i,h),await O(e.runtimeDir,s.id),await I(e.runtimeDir,{eventType:"content-sync-retry-scheduled",status:"retry-wait",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Content-sync retry was scheduled with bounded backoff.",details:{attempt:p.attempt??0,nextAttemptAt:p.nextAttemptAt||"",error:p.error||""}}),"processed"}let c={...s,status:o.exitCode===0?"success":"failed",endedAt:u,exitCode:o.exitCode,...o.error?{error:o.error}:{}};await w(r,null),c.status==="success"&&c.command==="content-sync"&&await Jt(e,c),await O(e.runtimeDir,s.id);try{let g=await $(c,e);c.logArchiveDir=g.archiveDir,c.logArchiveCreatedAt=g.archiveCreatedAt,c.logArchiveFileCount=g.archivedFiles.length;}catch(g){c.logArchiveError=g instanceof Error?g.message:String(g);}await w(i,c);let d=s.startedAt&&c.endedAt?Math.max(0,Math.round((new Date(c.endedAt).getTime()-new Date(s.startedAt).getTime())/1e3)):void 0;return await I(e.runtimeDir,{eventType:"job-run-finished",status:c.status==="success"?"success":"failed",actorSource:"queue-runner",jobId:c.id,command:c.command,message:c.status==="success"?"Job execution finished successfully.":"Job execution finished with failure.",details:{startedAt:s.startedAt||"",endedAt:c.endedAt||"",durationSec:d,deploymentProfile:c.deploymentProfile||"",exitCode:c.exitCode??null,error:c.error||"",logArchiveKey:c.logArchiveError||!c.logArchiveDir?"":m.basename(c.logArchiveDir),logArchiveDir:c.logArchiveDir||"",logArchiveCreatedAt:c.logArchiveCreatedAt||"",logArchiveFileCount:c.logArchiveFileCount??0,logArchiveError:c.logArchiveError||""}}),"processed"}async function Bt(e){if(!e)throw new Error("Missing remote WPSuite publisher state. Refresh the runtime config before running subscription-gated jobs.");if(e.subscriptionType!=="PROFESSIONAL"&&e.subscriptionType!=="AGENCY")throw new Error("This job requires an active WPSuite publisher subscription in the remote site configuration.")}var Mt={allowSchedulerAutoEnqueue:true,enforceSubscriptionOnJobExecution:false,shouldEnforceSubscriptionOnJobExecution:e=>Ce(e)||e.enqueueSource==="scheduler"||e.createdBy===0,assertActiveSubscriptionForIdentity:Bt},Te=xe(Mt),Ut=Te,wn=Te;process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href&&Ut().catch(e=>{console.error(e),process.exit(1);});export{wn as default,Ut as main,Te as runQueueRunner};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smart-cloud/publisher-exporter",
3
- "version": "1.1.44",
3
+ "version": "1.1.45",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "Headless Playwright static publisher for WordPress/Elementor sites with sitemap-only page discovery, strict asset capture, escaped URL rewrite, structured logs, and targeted retry modes.",
@@ -43,7 +43,7 @@
43
43
  "@aws-sdk/client-cloudfront": "^3.1100.0",
44
44
  "@aws-sdk/client-s3": "^3.1100.0",
45
45
  "@aws-sdk/lib-storage": "^3.1100.0",
46
- "@smart-cloud/publisher-core": "^1.1.7",
46
+ "@smart-cloud/publisher-core": "^1.1.8",
47
47
  "@smart-cloud/wpsuite-core": "^2.3.2",
48
48
  "baseline-browser-mapping": "^2.11.12",
49
49
  "caniuse-lite": "^1.0.30001809",