@smart-cloud/publisher-exporter 1.1.60 → 1.1.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
- import {pathToFileURL}from'url';import P from'fs/promises';import {existsSync,createReadStream,createWriteStream}from'fs';import m from'path';import {spawn}from'child_process';import {randomUUID,createHash}from'crypto';import Ge from'http';import be from'https';import {pipeline}from'stream/promises';import {createGzip}from'zlib';function he(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function G(e){return Array.isArray(e)?e.map(G):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>[t,G(n)])):e}function ze(e){return JSON.stringify(G(e))}function J(e){return createHash("sha256").update(ze(e)).digest("hex")}function fe(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 He(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 N(e){let t=he((e.postTypes??[]).map(r=>String(r).toLowerCase().replace(/[^a-z0-9_-]/g,"")));if(t.length===0)throw new Error("Content-sync requires at least one public post type.");let n=he((e.listingPaths??[]).map(r=>{let i=String(r).trim();if(!i)return "";let a=new URL(i,"https://content-sync.invalid/");if(a.origin!=="https://content-sync.invalid"&&/^https?:\/\//i.test(i))throw new Error("Content-sync listing routes must be same-site paths.");let s=`/${a.pathname.replace(/^\/+|\/+$/g,"")}`;return s==="/"?"/":`${s}/`}));return {postTypes:t,listingPaths:n,includeSubsites:e.includeSubsites===true,includePostTypeArchives:e.includePostTypeArchives!==false,includeTaxonomyArchives:e.includeTaxonomyArchives!==false,includeAuthorArchives:e.includeAuthorArchives===true,includeDateArchives:e.includeDateArchives===true,includePostsPage:e.includePostsPage!==false,includeSitemapChain:e.includeSitemapChain!==false}}function Y(e,t,n){return J({schemaVersion:1,sourceOrigin:He(e),ruleId:t.trim(),scope:N(n)})}function Z(e){return J({schemaVersion:1,...e})}function Se(e){return e.replace(/((?:x-static-publisher-token|authorization|proxy-authorization|x-api-key|aws-access-key-id|aws-secret-access-key|aws-session-token)\s*:\s*)[^\r\n]+/gi,"$1[REDACTED]").replace(/\b(Basic|Bearer)\s+[^\s]+/gi,"$1 [REDACTED]").replace(/([?&](?:token|access_token|refresh_token|api_key|key|nonce)=)[^&\s]+/gi,"$1[REDACTED]").replace(/((?:"|')?(?:runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken)(?:"|')?\s*[:=]\s*(?:"|'))[^"']+/gi,"$1[REDACTED]")}var ye=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";ye?m.join(ye,"current-progress.json"):"";Promise.resolve();var Ye="wp-json/smartcloud-static-publisher/v1/content-sync/",Ze=25*1024*1024,T=class extends Error{status;code;constructor(t,n,r){super(r),this.name="ContentSyncApiError",this.status=t,this.code=n;}},L=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,n="content-sync"){let r=this.options.sourceOrigin.replace(/\/+$/,"")+"/",i=n==="content-sync"?Ye:`wp-json/smartcloud-static-publisher/v1/${n}/`;return new URL(`${i}${t}`,r)}async post(t,n,r="content-sync"){let i=this.endpoint(t,r),a=Buffer.from(JSON.stringify(n),"utf8"),s=i.protocol==="https:"?be:Ge,o=i.protocol==="https:"?new be.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((c,u)=>{let g=s.request(i,{method:"POST",agent:o,headers:{"content-type":"application/json","content-length":String(a.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},d=>{let p=[],h=0;d.on("data",S=>{if(h+=S.byteLength,h>Ze){d.destroy(new Error("Content-sync API response exceeded the size limit."));return}p.push(S);}),d.on("error",u),d.on("end",()=>{let S=Buffer.concat(p).toString("utf8"),l;try{l=S?JSON.parse(S):{};}catch{u(new T(d.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let y=d.statusCode??500;if(y<200||y>=300){u(new T(y,String(l.code??"request-failed"),String(l.message??`Content-sync API request failed (${y}).`)));return}c(l);});});g.on("timeout",()=>g.destroy(new Error(`Content-sync API request timed out: ${t}`))),g.on("error",u),g.end(a);})}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)}async importCookieObservations(t){return this.post("cookie-observations",t,"privacy")}};function Xe(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function et(e,t){return {...e,...t??{}}}function tt(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function X(e,t){let n=String(t??e.deploymentTargetOverride??"").trim();if(!Xe(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:et(e.s3,i.s3),cloudFront:tt(e.cloudFront,i.cloudFront)}}}var ee=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function we(e){let t=new Set;for(let n of [...e,...ee]){let r=String(n||"").trim();r&&t.add(r);}return [...t]}function Pe(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function st(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 te(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t.trim(),String(n??"")]).filter(([t])=>t.length>0))}function ot(e){if(!e||typeof e!="object")return {};let t={};for(let[n,r]of Object.entries(e)){let i=n.trim();if(!i||!r||typeof r!="object")continue;let a=r,s={};if(typeof a.targetOrigin=="string"){let c=Pe(a.targetOrigin);c&&(s.targetOrigin=c);}let o=te(a.extraReplacements);if(Object.keys(o).length>0&&(s.extraReplacements=o),a.s3&&typeof a.s3=="object"){let c={},u=a.s3;typeof u.bucket=="string"&&(c.bucket=u.bucket.trim()),typeof u.prefix=="string"&&(c.prefix=u.prefix.trim()),typeof u.region=="string"&&(c.region=u.region.trim()),typeof u.htmlCacheControl=="string"&&(c.htmlCacheControl=u.htmlCacheControl.trim()),typeof u.assetCacheControl=="string"&&(c.assetCacheControl=u.assetCacheControl.trim()),Object.keys(c).length>0&&(s.s3=c);}if(a.cloudFront&&typeof a.cloudFront=="object"){let c={},u=a.cloudFront;typeof u.distributionId=="string"&&(c.distributionId=u.distributionId.trim()),Array.isArray(u.invalidationPaths)&&(c.invalidationPaths=u.invalidationPaths.map(g=>String(g??"").trim()).filter(g=>g.length>0)),Object.keys(c).length>0&&(s.cloudFront=c);}t[i]=s;}return t}function re(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function at(e,t){if(!e||typeof e!="object")return null;let n=e,r=n.command;if(r!=="publish"&&r!=="crawl"&&r!=="deploy"&&r!=="invalidate"&&r!=="retry-timeouts"&&r!=="url"&&r!=="content-sync")return null;let i=Number.parseInt(String(n.intervalMinutes??"0"),10);if(!Number.isFinite(i)||i<1)return null;let a=String(n.id??`${r}-${t+1}`).trim();if(!a)return null;let s=String(n.deploymentProfile??"").trim(),o=String(n.url??"").trim(),c=Array.isArray(n.postTypes)?[...new Set(n.postTypes.map(g=>String(g??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],u=Array.isArray(n.listingPaths)?[...new Set(n.listingPaths.map(g=>String(g??"").trim()).filter(Boolean))]:[];return r==="content-sync"&&c.length===0?null:{id:a,enabled:n.enabled!==false,command:r,intervalMinutes:i,...r==="publish"||r==="crawl"?{crawlMode:n.crawlMode==="incremental"?"incremental":"full"}:{},...(r==="publish"||r==="deploy"||r==="invalidate"||r==="content-sync")&&s?{deploymentProfile:s}:{},...o?{url:o}:{},...c.length>0?{postTypes:c}:{},...u.length>0?{listingPaths:u}:{},...typeof n.includeSubsites=="boolean"?{includeSubsites:n.includeSubsites}:{},...typeof n.includePostTypeArchives=="boolean"?{includePostTypeArchives:n.includePostTypeArchives}:{},...typeof n.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:n.includeTaxonomyArchives}:{},...typeof n.includeAuthorArchives=="boolean"?{includeAuthorArchives:n.includeAuthorArchives}:{},...typeof n.includeDateArchives=="boolean"?{includeDateArchives:n.includeDateArchives}:{},...typeof n.includePostsPage=="boolean"?{includePostsPage:n.includePostsPage}:{},...typeof n.includeSitemapChain=="boolean"?{includeSitemapChain:n.includeSitemapChain}:{}}}function Ce(e){let t=e&&typeof e=="object"?e:{},n=Array.isArray(t.rules)?t.rules.map((r,i)=>at(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 ct(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 ie(e){let t=e&&typeof e=="object"?e:{},n=String(t.accountId??"").trim(),r=String(t.siteId??"").trim(),i=t.subscriber===true,a=ct(t.siteSettings),s={...a??{},...n?{accountId:a?.accountId??n}:{},...r?{siteId:a?.siteId??r}:{},...a?.subscriber===true||i?{subscriber:true}:{}},o={},c=String(t.apiBase??"").trim();c&&(o.apiBase=c);let u=String(t.runtimeToken??t.nonce??"").trim();u&&(o.runtimeToken=u);let g=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();g&&(o.virtualAssetBaseUrl=g),Object.keys(s).length>0&&(o.siteSettings=s);let d=re(t.subscriptionType);return d&&(o.subscriptionType=d),o}function ut(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function Ae(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function lt(e,t){let n=new Headers(e instanceof Request?e.headers:void 0);if(t?.headers)for(let[r,i]of new Headers(t.headers).entries())n.set(r,i);return n}async function Ie(e,t,n=5){let r=new URL(Ae(e)),i=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),a=r.protocol==="http:"?Ge:be,s=lt(e,t);return await new Promise((o,c)=>{let u=a.request(r,{method:i,headers:Object.fromEntries(s.entries()),...r.protocol==="https:"?{rejectUnauthorized:false}:{}},g=>{let d=[];g.on("data",p=>{d.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),g.on("error",c),g.on("end",()=>{let p=g.statusCode??0,h=g.headers.location;if(n>0&&h&&[301,302,303,307,308].includes(p)){o(Ie(new URL(h,r),{method:p===303?"GET":i},n-1));return}let S=new Headers;for(let[l,y]of Object.entries(g.headers))Array.isArray(y)?S.set(l,y.join(", ")):typeof y=="string"&&S.set(l,y);o(new Response(Buffer.concat(d),{status:p,statusText:g.statusMessage??"",headers:S}));});});u.on("error",c),u.end();})}function dt(e){let t=ie(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;ut(e.sourceOrigin);let s=globalThis,c={...s.WpSuite??{},siteSettings:n,uploadUrl:a};return t.apiBase&&(c.apiBase=t.apiBase),s.WpSuite=c,true}async function gt(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!dt(e)){let i=e.wpsuite?.siteSettings,a=[String(i?.accountId??"").trim()?"":"accountId",String(i?.siteId??"").trim()?"":"siteId",t?"":"virtualAssetBaseUrl"].filter(Boolean);return {remote:null,diagnostic:{status:"invalid-runtime",virtualAssetBaseUrl:t,subscriptionType:null,error:`Missing WP Suite runtime field(s): ${a.join(", ")}.`,requests:[]}}}let n=globalThis.fetch,r=[];try{typeof n=="function"&&/^https?:\/\//i.test(t)&&(globalThis.fetch=(async(c,u)=>{let g=Ae(c);if(g.startsWith(t)){let d=(()=>{try{return new URL(g).pathname}catch{return g}})();try{let p=e.ignoreHttpsErrors&&g.startsWith("https://")?await Ie(c,u):await n(c,u);return r.push({path:d,status:p.status,ok:p.ok,error:null}),p}catch(p){throw r.push({path:d,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return n(c,u)}));let{getConfig:i}=await import('@smart-cloud/wpsuite-core'),a=await i("publisher"),s=a&&typeof a=="object"?a:null,o=re(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 pt(e,t){let n=ot(t?.deploymentProfiles),r=String(t?.defaultDeploymentProfile??"").trim(),i=n[r]?r:"",a=re(t?.subscriptionType),s=ie(e.wpsuite);return {...e,scheduler:Ce(t?.scheduler),deploymentProfiles:n,defaultDeploymentProfile:i,...a?{subscriptionType:a}:{},wpsuite:{...s,...s.siteSettings||a?{siteSettings:{...s.siteSettings??{},...s.siteSettings?.subscriber===true||a==="PROFESSIONAL"||a==="AGENCY"?{subscriber:true}:{}}}:{},...a?{subscriptionType:a}:{}}}}function ne(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 mt(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?m.resolve(e):""}function ve(e,t,n){let r=String(t||"").trim();return r&&m.isAbsolute(r)?m.resolve(r):m.resolve(e,ne(r,n))}async function Q(e,t={}){let n=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",r=await P.readFile(n,"utf8"),i=JSON.parse(r);i.sourceOrigin=i.sourceOrigin.replace(/\/$/,""),i.targetOrigin=Pe(i.targetOrigin),i.ignoreHttpsErrors??=false,i.outputDir=String(i.outputDir||"export").trim()||"export",i.urlRewriteMode||=i.targetOrigin==="."?"relative":"absolute",i.noJavaScriptRenderPathPrefixes||=[],i.seedPaths||=[],i.generated404RequestPath=st(i.generated404RequestPath),i.sitemapPaths||=[...ee],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=te(i.extraReplacements),i.postCrawlCopyMap=te(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=ie(i.wpsuite),i.scheduler=Ce(void 0),i.deploymentProfiles={},i.defaultDeploymentProfile="",i.deploymentTargetOverride=String(i.deploymentTargetOverride??"").trim();let a=await gt(i);t.onRemotePublisherConfigDiagnostic?.(a.diagnostic);let s=pt(i,a.remote),o=mt(),c=o?m.resolve(o,".."):"";return c?(s.outputDir=ve(c,s.outputDir,"export"),s.logDir=ve(c,s.logDir,"logs")):(m.isAbsolute(s.outputDir)||(s.outputDir=ne(s.outputDir,"export")),m.isAbsolute(s.logDir)||(s.logDir=ne(s.logDir,"logs"))),s}function Re(e){let t=e.split(/\r?\n/).map(r=>r.trim()).filter(Boolean),n=[...t].reverse().find(r=>/^(?:Error|[A-Za-z][A-Za-z0-9]*Error):\s/.test(r));return n||(t.find(r=>!r.startsWith("at "))??"")}function se(e){let t=m.resolve(e);return {state:m.join(t,"content-sync-state.json"),current:m.join(t,"content-sync-current.json"),impact:m.join(t,"content-sync-impact-plan.json"),checkpoint:m.join(t,"content-sync-checkpoint.json"),baseline:m.join(t,"content-sync-baseline.json"),activeRules:m.join(t,"content-sync-active-rules.json"),releaseCutoff:m.join(t,"content-sync-release-cutoff.json"),candidateManifest:m.join(t,"content-sync-candidate-manifest.json"),trustedManifest:m.join(t,"crawl-manifest.json"),invalidation:m.join(t,"content-sync-invalidation.json"),deployPlan:m.join(t,"deploy-plan.json")}}async function oe(e,t){await qe(e.activeRules,{contractVersion:1,evaluatedAt:new Date().toISOString(),entries:t});}async function qe(e,t){await P.mkdir(m.dirname(e),{recursive:true});let n=`${e}.${process.pid}.${Date.now()}.tmp`;await P.writeFile(n,JSON.stringify(t,null,2),"utf8"),await P.rename(n,e);}async function xe(e){try{return JSON.parse(await P.readFile(e,"utf8"))}catch(t){if(t.code==="ENOENT")return null;throw new Error(`Invalid or unreadable content-sync state file ${e}: ${t instanceof Error?t.message:String(t)}`,{cause:t})}}async function ht(e){let t=await xe(e.current);if(t===null)return null;let n=t;if(n.schemaVersion!==1||typeof n.jobId!="string"||typeof n.phase!="string"||!Number.isSafeInteger(n.fromSequence)||!Number.isSafeInteger(n.toSequence))throw new Error("Invalid content-sync current-range state.");return n}async function De(e){let t=await xe(e.state);if(t===null)return {schemaVersion:1,updatedAt:"",rules:{}};let n=t;if(n.schemaVersion!==1||typeof n.updatedAt!="string"||!n.rules||typeof n.rules!="object"||Array.isArray(n.rules))throw new Error("Invalid content-sync state store.");return n}async function ke(e,t){await qe(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Te(e,t){let n=await ht(e);return !n||n.coalesceKey!==t?false:(await Promise.all([e.current,e.checkpoint,e.impact,e.candidateManifest,e.invalidation,e.deployPlan].map(async r=>{try{await P.unlink(r);}catch(i){if(i.code!=="ENOENT")throw i}})),true)}function Le(e){let t=String(e.command??"").trim();return (t==="publish"||t==="crawl")&&e.crawlMode==="incremental"||t==="content-sync"}var ae=new Map;async function Ee(e){let t=m.resolve(e),n=ae.get(t);if(n!==void 0)return n;try{let r=JSON.parse(await P.readFile(t,"utf8")),i=String(r.version??"").trim();return ae.set(t,i),i}catch{return ae.set(t,""),""}}var $=class extends Error{request;step;constructor(t,n){super(`Stop requested during ${n}.`),this.name="StopRequestedError",this.request=t,this.step=n;}},vt="queue-mutation.lock",Pt=5e3,Ct=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(),r=process.env.PUBLISHER_CONFIG||"",i=1;for(let o=0;o<e.length;o++){let c=e[o];c==="--runtime-dir"?t=e[++o]||"":c.startsWith("--runtime-dir=")?t=c.slice(14):c==="--exporter-dir"?n=e[++o]||n:c.startsWith("--exporter-dir=")?n=c.slice(15):c==="--config"?r=e[++o]||"":c.startsWith("--config=")?r=c.slice(9):c==="--max-jobs"?i=Number.parseInt(e[++o]||"1",10):c.startsWith("--max-jobs=")&&(i=Number.parseInt(c.slice(11),10));}if(!t)throw new Error("Missing runtime dir. Use --runtime-dir or STATIC_PUBLISHER_RUNTIME_DIR.");let a=m.resolve(t),s=r?m.resolve(r):m.join(a,"config.json");return {runtimeDir:a,exporterDir:m.resolve(n),configPath:s,maxJobs:Number.isFinite(i)&&i>0?i:1}}async function w(e,t){try{let n=await P.readFile(e,"utf8");return JSON.parse(n)}catch(n){if((n&&typeof n=="object"&&"code"in n?String(n.code||""):"")!=="ENOENT"){let i=n instanceof Error?n.message:String(n);console.warn(`[queue-runner] failed to read JSON ${e}: ${i}`);}return t}}async function v(e,t){await P.mkdir(m.dirname(e),{recursive:true}),await P.writeFile(e,JSON.stringify(t,null,2),"utf8");}function Be(e){return m.join(e,vt)}async function It(e){await new Promise(t=>setTimeout(t,e));}async function Rt(e){let t=Be(e),n=Date.now()+Pt;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>Ct){await P.unlink(t).catch(()=>{});continue}if(Date.now()>=n)throw new Error("Timed out acquiring queue mutation lock.",{cause:r});await It(50);}}async function qt(e){await P.unlink(Be(e)).catch(()=>{});}async function F(e,t){await Rt(e);try{return await t()}finally{await qt(e);}}function Me(e){return m.join(e,"queue-runner-heartbeat.json")}function ge(e){return m.join(e,"current-progress.json")}function ce(e){return m.join(e,"scheduler-state.json")}function xt(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 Ue(e){let t=await w(e.configPath||"",null),n=m.resolve(e.runtimeDir,".."),i=(typeof t?.logDir=="string"?t.logDir:"").trim();return i&&m.isAbsolute(i)?m.resolve(i):m.resolve(n,xt(i,"logs"))}function ue(e,t){return String(e||"").trim().replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/^-+|-+$/g,"")||t}function Dt(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 kt(e){return [Dt(e.endedAt||e.startedAt),ue(e.command||"job","job"),ue(e.id||"job","job"),ue(e.status||"finished","finished")].join("-")}function Tt(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 Et(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 W(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:Et(n),originalFileName:n,storedFileName:r,compressed:true,compression:"gzip",contentType:Ft(r),originalSize:a.size,storedSize:s.size}}async function K(e,t){let n=await Ue(t),r=new Date().toISOString(),i=kt(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 p of d){if(!p.isFile()||!Tt(p.name))continue;let h=await W(m.join(n,p.name),a,p.name);o.push(h),s.push(h.storedFileName);}}catch(d){if((d&&typeof d=="object"&&"code"in d?String(d.code||""):"")!=="ENOENT")throw d}let c=ge(t.runtimeDir);if(existsSync(c)){let d=await W(c,a,"current-progress.json");o.push(d),s.push(d.storedFileName);}if(e.command==="content-sync")for(let d of ["content-sync-current.json","content-sync-impact-plan.json","content-sync-checkpoint.json","content-sync-state.json","content-sync-invalidation.json","deploy-plan.json"]){let p=m.join(t.runtimeDir,d);if(!existsSync(p))continue;let h=await W(p,a,d);o.push(h),s.push(h.storedFileName);}let u=m.join(t.runtimeDir,"privacy","cookie-observations.json");if(existsSync(u)){let d=await W(u,a,"cookie-observations.json");o.push(d),s.push(d.storedFileName);}let g=[...s,"job.json"];return await v(m.join(a,"job.json"),{manifestVersion:1,archivedAt:r,archiveKey:i,archiveDir:a,logDir:n,runtimeDir:t.runtimeDir,exporterDir:t.exporterDir,configPath:t.configPath||"",archivedFiles:g,artifacts:o,job:e}),{archiveKey:i,archiveDir:a,archiveCreatedAt:r,archivedFiles:g,artifacts:o}}function jt(e){return m.join(e,"audit-events.jsonl")}function le(e){return m.join(e,"stop-request.json")}async function $e(e){try{let t=await P.readFile(le(e),"utf8"),n=JSON.parse(t);return n&&typeof n=="object"?n:null}catch{return null}}async function Fe(e,t){let n=await $e(e);if(!n)return null;let r=String(n.targetJobId||"").trim();return r&&r!==t?null:n}async function U(e,t){if(!t){await P.unlink(le(e)).catch(()=>{});return}let n=await $e(e);if(!n)return;let r=String(n.targetJobId||"").trim();(!r||r===t)&&await P.unlink(le(e)).catch(()=>{});}async function R(e,t){try{let n={occurredAt:t.occurredAt||new Date().toISOString(),status:t.status||"info",actorSource:t.actorSource||"queue-runner",...t};await P.mkdir(e,{recursive:!0}),await P.appendFile(jt(e),`${JSON.stringify(n)}
2
- `,"utf8");}catch{}}async function C(e,t,n){let[r,i]=await Promise.all([Ee(m.join(e.exporterDir,"package.json")),Ee(m.join(e.exporterDir,"node_modules","@smart-cloud","wpsuite-core","package.json"))]),a={checkedAt:new Date().toISOString(),status:t,pid:process.pid,nodePath:process.execPath,nodeVersion:process.version,exporterVersion:r,wpsuiteCoreVersion:i,runtimeDir:e.runtimeDir,exporterDir:e.exporterDir,...n??{}};try{await v(Me(e.runtimeDir),a);}catch{}}async function E(e){try{await P.unlink(ge(e));}catch{}}function Ot(e,t){let n=e.command;if(n!=="publish"&&n!=="crawl"&&n!=="deploy"&&n!=="invalidate"&&n!=="retry-timeouts"&&n!=="url"&&n!=="content-sync")return null;let r=Number.parseInt(String(e.intervalMinutes??"0"),10);if(!Number.isFinite(r)||r<1)return null;let i=(e.id||`${n}-${t+1}`).trim();if(!i)return null;let a=e.enabled!==false,s=typeof e.url=="string"?e.url.trim():"",o=typeof e.deploymentProfile=="string"?e.deploymentProfile.trim():"",c=Array.isArray(e.postTypes)?[...new Set(e.postTypes.map(d=>String(d).trim()).filter(Boolean))]:[],u=Array.isArray(e.listingPaths)?[...new Set(e.listingPaths.map(d=>String(d).trim()).filter(Boolean))]:[],g=(n==="publish"||n==="crawl")&&e.crawlMode==="incremental"?"incremental":"full";return n==="url"&&!s||n==="content-sync"&&c.length===0?null:{id:i,enabled:a,command:n,intervalMinutes:r,...n==="publish"||n==="crawl"?{crawlMode:g}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&o?{deploymentProfile:o}:{},...s?{url:s}:{},...c.length>0?{postTypes:c}:{},...u.length>0?{listingPaths:u}:{},...typeof e.includeSubsites=="boolean"?{includeSubsites:e.includeSubsites}:{},...typeof e.includePostTypeArchives=="boolean"?{includePostTypeArchives:e.includePostTypeArchives}:{},...typeof e.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:e.includeTaxonomyArchives}:{},...typeof e.includeAuthorArchives=="boolean"?{includeAuthorArchives:e.includeAuthorArchives}:{},...typeof e.includeDateArchives=="boolean"?{includeDateArchives:e.includeDateArchives}:{},...typeof e.includePostsPage=="boolean"?{includePostsPage:e.includePostsPage}:{},...typeof e.includeSitemapChain=="boolean"?{includeSitemapChain:e.includeSitemapChain}:{}}}async function Jt(e){let t=await Q(e.configPath).catch(()=>null),n=t?.scheduler,r=Array.isArray(n?.rules)?n.rules.map((i,a)=>Ot(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 z(e,t){if(!t)throw new Error("Content-sync scheduler requires a valid publisher config.");let n=N({postTypes:e.postTypes??[],listingPaths:e.listingPaths??[],includeSubsites:e.includeSubsites,includePostTypeArchives:e.includePostTypeArchives,includeTaxonomyArchives:e.includeTaxonomyArchives,includeAuthorArchives:e.includeAuthorArchives,includeDateArchives:e.includeDateArchives,includePostsPage:e.includePostsPage,includeSitemapChain:e.includeSitemapChain}),r=Y(t.sourceOrigin,e.id,n),i=X(t,e.deploymentProfile),a=Z({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)}`}function Lt(e,t,n,r){let i=[n[t],r[t]].filter(s=>!!s).filter(s=>s.coalesceKey===t&&s.ruleId===e.id).map(s=>String(s.consumerId??"").trim()).filter(Boolean),a=[...new Set(i)];return a.length===1?a[0]:""}async function je(e,t){let n=se(e.runtimeDir);if(await oe(n,[]),!t?.enabled||!t.publisherConfig)return;let r=t.rules.filter(u=>u.enabled&&u.command==="content-sync");if(r.length===0)return;let[i,a]=await Promise.all([w(n.state,{}),w(n.baseline,{})]),s=i.rules??{},o=a.entries??{},c=[];for(let u of r){let g=z(u,t.publisherConfig),d=Lt(u,g,s,o);d&&c.push({ruleId:u.id,coalesceKey:g,consumerId:d});}await oe(n,c);}async function Bt(e,t,n,r,i){let s=(await w(m.join(e.runtimeDir,"content-sync-baseline.json"),{})).entries?.[r];if(!s?.consumerId||!s.scopeFingerprint||!s.baselineId)return {status:"baseline-required",reason:"No verified normal-release baseline exists for this rule scope."};try{let o=t.publisherConfig;if(!o)throw new Error("Publisher configuration is unavailable.");let c=N({postTypes:n.postTypes??[],listingPaths:n.listingPaths??[],includeSubsites:n.includeSubsites,includePostTypeArchives:n.includePostTypeArchives,includeTaxonomyArchives:n.includeTaxonomyArchives,includeAuthorArchives:n.includeAuthorArchives,includeDateArchives:n.includeDateArchives,includePostsPage:n.includePostsPage,includeSitemapChain:n.includeSitemapChain}),u=Y(o.sourceOrigin,n.id,c),g=X(o,n.deploymentProfile),d=Z({sourceOrigin:o.sourceOrigin,targetOrigin:g.config.targetOrigin,deploymentProfile:g.name||"",s3Bucket:g.config.s3.bucket,s3Prefix:g.config.s3.prefix,cloudFrontDistributionId:g.config.cloudFront.distributionId,urlRewriteMode:g.config.urlRewriteMode,extraReplacements:g.profile?.extraReplacements??{}});if(s.scopeFingerprint!==u||s.targetFingerprint!==d||!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 L({sourceOrigin:o.sourceOrigin,runtimeToken:String(o.wpsuite?.runtimeToken||""),ignoreHttpsErrors:o.ignoreHttpsErrors}),h=await p.releaseFingerprint(c.includeSubsites),S=fe({wordpressFingerprint:h,targetFingerprint:d,sourceOrigin:o.sourceOrigin,sitemapPaths:we(o.sitemapPaths),blockedPathPrefixes:o.blockedPathPrefixes,assetPathPrefixes:o.assetPathPrefixes,noJavaScriptRenderPathPrefixes:o.noJavaScriptRenderPathPrefixes,urlRewriteMode:o.urlRewriteMode});if(s.releaseFingerprint!==S)return {status:"baseline-required",reason:"The installed WordPress release changed after the last verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline."};let l=await p.head({consumerId:s.consumerId,scopeFingerprint:s.scopeFingerprint,baselineId:s.baselineId,postTypes:c.postTypes,includeSubsites:c.includeSubsites}),y=l.committedSequence;if(i?.command==="content-sync"&&i.coalesceKey===r){let b=await w(m.join(e.runtimeDir,"content-sync-current.json"),null);b?.coalesceKey===r&&Number.isSafeInteger(b.toSequence)&&(y=Math.max(y,Number(b.toSequence)));}return {status:l.headSequence>y?"pending":"current",headSequence:l.headSequence,committedSequence:l.committedSequence}}catch(o){return o instanceof T&&o.code==="baseline-required"?{status:"baseline-required",reason:o.message}:{status:"unavailable",reason:o instanceof Error?o.message:String(o)}}}async function Ne(e,t,n,r,i,a){let s=m.join(e.runtimeDir,"content-sync-state.json"),c=(await w(s,{})).rules??{},u=c[n]??{};c[n]={...u,ruleId:t,coalesceKey:n,...Number.isSafeInteger(i)?{observedHeadSequence:i}:{},...Number.isSafeInteger(a)?{committedSequence:a}:{},retryAttempt:0,nextRetryAt:null,lastError:r,baselineStatus:"required",baselineReason:r,trailingWorkDetected:Number.isSafeInteger(i)&&Number.isSafeInteger(a)?Number(i)>Number(a):!!u.trailingWorkDetected},await v(s,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:c});}function de(e,t){let n=(t.url||"").trim(),r=t.crawlMode||"full",i=(t.deploymentProfile||"").trim(),a=(t.coalesceKey||"").trim();return e.some(s=>s.command===t.command&&(t.command!=="content-sync"||(s.coalesceKey||"").trim()===a)&&(s.url||"").trim()===n&&(s.crawlMode||"full")===r&&(s.deploymentProfile||"").trim()===i&&(s.status===void 0||s.status==="queued"||s.status==="retry-wait"||s.status==="running"))}async function Mt(e,t,n){if(!t.allowSchedulerAutoEnqueue)return await je(e,null),0;let r=await Jt(e);if(await je(e,r),!r.enabled||r.rules.length===0)return 0;if(t.assertActiveSubscriptionForIdentity)try{await t.assertActiveSubscriptionForIdentity(r.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"),a=m.join(e.runtimeDir,"current-run.json"),s=Date.now(),o=0,c=[],u=[],g=[],d=new Map,[p,h,S]=await Promise.all([w(i,[]),w(a,null),w(ce(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}})]);for(let l of r.rules){if(!l.enabled||l.command!=="content-sync")continue;let y=Math.floor(s/(l.intervalMinutes*60*1e3)),b=S.lastEvaluatedBucketByRuleId?.[l.id]??S.lastEnqueuedBucketByRuleId?.[l.id]??-1;if(y<=b)continue;let D=z(l,r.publisherConfig);de(p,{command:l.command,deploymentProfile:l.deploymentProfile,coalesceKey:D})||d.set(l.id,await Bt(e,r,l,D,h));}await F(e.runtimeDir,async()=>{let l=await w(i,[]),y=await w(a,null),b=await w(ce(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}});b.lastEnqueuedBucketByRuleId??={},b.lastEvaluatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.lastCreatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.coalescedCountByRuleId??={};let D=[...l];y&&y.status==="running"&&D.unshift(y);for(let f of r.rules){if(!f.enabled)continue;let A=f.intervalMinutes*60*1e3,x=Math.floor(s/A),j=b.lastEvaluatedBucketByRuleId[f.id]??b.lastEnqueuedBucketByRuleId[f.id]??-1;if(x<=j)continue;let O=f.command==="content-sync"?z(f,r.publisherConfig):"";b.lastEvaluatedBucketByRuleId[f.id]=x;let Ke=f.command==="content-sync"?l:D;if(de(Ke,{command:f.command,url:f.url,crawlMode:f.crawlMode,deploymentProfile:f.deploymentProfile,coalesceKey:O})){b.coalescedCountByRuleId[f.id]=(b.coalescedCountByRuleId[f.id]??0)+1,u.push(f);continue}if(f.command==="content-sync"){let V=d.get(f.id);if(!V||V.status!=="pending"){let me=V??{status:"unavailable",reason:"Content-sync demand was not checked."};me.status!=="current"&&g.push({rule:f,check:me});continue}}let H={id:randomUUID(),command:f.command,...f.command==="content-sync"?{ruleId:f.id,coalesceKey:O,attempt:0}:{},...(f.command==="publish"||f.command==="crawl")&&f.crawlMode?{crawlMode:f.crawlMode}:{},...(f.command==="publish"||f.command==="deploy"||f.command==="invalidate"||f.command==="content-sync")&&f.deploymentProfile?{deploymentProfile:f.deploymentProfile}:{},...f.url?{url:f.url}:{},enqueueSource:"scheduler",...r.wpsuite?{wpsuite:r.wpsuite}:{},status:"queued",createdAt:new Date().toISOString(),createdBy:0};l.push(H),D.push(H),c.push({job:H,rule:f}),b.lastEnqueuedBucketByRuleId[f.id]=x,b.lastCreatedBucketByRuleId[f.id]=x,o+=1;}o>0&&await v(i,l),await v(ce(e.runtimeDir),b);});for(let l of c){if(l.rule.command==="content-sync"){let y=d.get(l.rule.id);await R(e.runtimeDir,{eventType:"content-sync-demand-detected",status:"queued",actorSource:"queue-runner-scheduler",jobId:l.job.id,command:l.job.command,message:"Pending journal changes require a content-sync job.",details:{ruleId:l.rule.id,headSequence:y?.headSequence??0,committedSequence:y?.committedSequence??0}});}await R(e.runtimeDir,{eventType:"job-created",status:"queued",actorSource:"queue-runner-scheduler",jobId:l.job.id,command:l.job.command,message:"Scheduler auto-enqueued a job.",details:{ruleId:l.rule.id,intervalMinutes:l.rule.intervalMinutes,timezone:r.timezone,deploymentProfile:l.rule.deploymentProfile||"",url:l.rule.url||""}});}for(let l of u)await R(e.runtimeDir,{eventType:"content-sync-coalesced",status:"queued",actorSource:"queue-runner-scheduler",command:l.command,message:"Scheduler demand was coalesced into an existing content-sync job.",details:{ruleId:l.id}});for(let l of g){if(l.check.status==="baseline-required"){let y=z(l.rule,r.publisherConfig);await Ne(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 R(e.runtimeDir,{eventType:l.check.status==="baseline-required"?"content-sync-baseline-required":"content-sync-demand-check-failed",status:l.check.status==="unavailable"?"failed":"info",actorSource:"queue-runner-scheduler",command:"content-sync",message:l.check.reason||"The content-sync consumer is already at the current journal head.",details:{ruleId:l.rule.id,headSequence:l.check.headSequence??0,committedSequence:l.check.committedSequence??0}});}return o}async function Ut(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 $t(e){try{await P.unlink(e);}catch{}}function Nt(e,t){let n=[m.join(e,"dist",`${t}.js`),m.join(e,`${t}.js`)];for(let r of n)if(existsSync(r))return r;throw new Error(`Cannot find ${t}.js in ${m.join(e,"dist")} or ${e}`)}async function q(e,t,n,r,i,a,s){if(s){let u=await Fe(t,s);if(u)throw new $(u,n)}let o=Nt(e,n),c={...process.env,...a??{}};return c.STATIC_PUBLISHER_RUNTIME_DIR=t,i&&(c.PUBLISHER_CONFIG=i),await new Promise((u,g)=>{let d=null,p=null,h=null,S=null,l=false,y="",b=spawn(process.execPath,[o,...r],{cwd:e,env:c,stdio:["inherit","inherit","pipe"]});b.stderr?.on("data",A=>{y+=A.toString("utf8");});let D=()=>{p&&clearInterval(p),h&&clearInterval(h),S&&clearTimeout(S);},f=async()=>{if(!(!s||d||l)){l=true;try{let A=await Fe(t,s);if(!A)return;d=A,b.kill("SIGTERM"),S=setTimeout(()=>{b.kill("SIGKILL");},1e4);}finally{l=false;}}};s&&(p=setInterval(()=>{f();},1e3),h=setInterval(()=>{(async()=>{let A=Me(t),x=await w(A,{});await v(A,{...x,checkedAt:new Date().toISOString(),status:"running",currentJobId:s||x.currentJobId||"",currentStep:n});})().catch(()=>{});},15e3)),b.on("error",A=>{D(),g(A);}),b.on("close",(A,x)=>{D();let j=Se(y).trim();if(j&&process.stderr.write(`${j}
3
- `),d){g(new $(d,n));return}if(A===0)u();else {let O=Re(j);g(new Error(`${n} exited with code ${A??-1}${x?` (signal ${x})`:""}${O?`: ${O}`:""}`));}});}),0}function Qt(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 Wt(e,t){let n=s=>{if(!s||typeof s!="object")return null;let o=s,c={...o.siteSettings??{},...String(o.accountId??"").trim()&&!o.siteSettings?.accountId?{accountId:String(o.accountId).trim()}:{},...String(o.siteId??"").trim()&&!o.siteSettings?.siteId?{siteId:String(o.siteId).trim()}:{},...o.siteSettings?.subscriber===true||o.subscriber===true?{subscriber:true}:{}},u={...String(o.apiBase??"").trim()?{apiBase:String(o.apiBase).trim()}:{},...String(o.runtimeToken??o.nonce??"").trim()?{runtimeToken:String(o.runtimeToken??o.nonce).trim()}:{},...String(o.virtualAssetBaseUrl??o.uploadUrl??"").trim()?{virtualAssetBaseUrl:String(o.virtualAssetBaseUrl??o.uploadUrl).trim()}:{},...Object.keys(c).length>0?{siteSettings:c}:{},...o.subscriptionType?{subscriptionType:o.subscriptionType}:{}};return Object.keys(u).length>0?u:null},r=await Q(t.configPath).catch(()=>null),i=n(r?.wpsuite);if(i)return i;let a=n(e.wpsuite);return a||null}async function Kt(e,t){return await Wt(e,t)}function pe(e){let t={...e,status:"queued"};return delete t.startedAt,delete t.endedAt,delete t.exitCode,delete t.error,delete t.stopRequestedAt,delete t.stopRequestedByUserId,delete t.stopRequestedByLogin,delete t.stopMode,delete t.stoppedStep,t}async function _t(e,t){let n=await w(ge(e.runtimeDir),null),r=n&&n.details&&typeof n.details.phase=="string"?n.details.phase.trim():"";if(r)return r;let i=n&&typeof n.currentStep=="string"?n.currentStep.trim():"";if(i)return i;let a=await Ue(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 zt(e,t){if((e.command==="publish"||e.command==="crawl")&&t==="rewrite-text")return "rewrite-text"}async function Ht(e){let t=m.join(e.runtimeDir,"queue.json"),n=m.join(e.runtimeDir,"current-run.json"),r=await F(e.runtimeDir,async()=>{let i=await w(n,null);if(!i||i.status!=="running"&&i.status!=="queued")return null;let a=await w(t,[]),s=Array.isArray(a)?a.filter(c=>c?.id!==i.id):[],o=pe(i);return await v(t,[o,...s]),await v(n,null),o});r&&(await U(e.runtimeDir,r.id),await R(e.runtimeDir,{eventType:"job-recovered",status:"queued",actorSource:"queue-runner",jobId:r.id,command:r.command,message:"Recovered stale current-run entry back into queue."}));}async function M(e,t){if(e.resumeFromStep==="rewrite-text")return;let n=m.join(t.runtimeDir,"privacy","cookie-observations.json"),r;try{let o=await P.stat(n);if(!o.isFile()||o.size>5*1024*1024)throw new Error("Cookie observation artifact is missing or exceeds 5 MiB.");r=JSON.parse(await P.readFile(n,"utf8"));}catch(o){if((o&&typeof o=="object"&&"code"in o?String(o.code||""):"")==="ENOENT")return;throw o}let i=await Q(t.configPath),a=String(i.wpsuite?.runtimeToken||"").trim();if(!a)return;await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"consent-cookie-observations"}),await new L({sourceOrigin:i.sourceOrigin,runtimeToken:a,ignoreHttpsErrors:!!i.ignoreHttpsErrors}).importCookieObservations(r);}async function Vt(e,t,n){try{if((n.shouldEnforceSubscriptionOnJobExecution?.(e)??n.enforceSubscriptionOnJobExecution)&&n.assertActiveSubscriptionForIdentity){let o=await Kt(e,t);await n.assertActiveSubscriptionForIdentity(o);}let i=Qt(e),a=e.resumeFromStep==="rewrite-text"?["--resume-rewrite"]:e.crawlMode==="incremental"?["--crawl-mode","incremental"]:[],s=e.deploymentProfile?["--profile",e.deploymentProfile]:[];if(e.command==="publish")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-release-cutoff"}),await q(t.exporterDir,t.runtimeDir,"content-sync",["--capture-baseline-cutoffs",e.id],t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:e.resumeFromStep==="rewrite-text"?"rewrite-text":"crawl"}),await q(t.exporterDir,t.runtimeDir,"crawl",a,t.configPath,i,e.id),await M(e,t),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await q(t.exporterDir,t.runtimeDir,"deploy",s,t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await q(t.exporterDir,t.runtimeDir,"invalidate",s,t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-baseline"}),await q(t.exporterDir,t.runtimeDir,"content-sync",["--establish-baselines",e.id],t.configPath,i,e.id),{exitCode:0};if(e.command==="crawl")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:e.resumeFromStep==="rewrite-text"?"rewrite-text":"crawl"}),await q(t.exporterDir,t.runtimeDir,"crawl",a,t.configPath,i,e.id),await M(e,t),{exitCode:0};if(e.command==="deploy")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await q(t.exporterDir,t.runtimeDir,"deploy",s,t.configPath,i,e.id),{exitCode:0};if(e.command==="invalidate")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await q(t.exporterDir,t.runtimeDir,"invalidate",s,t.configPath,i,e.id),{exitCode:0};if(e.command==="retry-timeouts")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"retry-timeouts"}),await q(t.exporterDir,t.runtimeDir,"crawl",["--retry-timeouts"],t.configPath,i,e.id),await M(e,t),{exitCode:0};if(e.command==="url"){let o=(e.url||"").trim();return o?(await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"url"}),await q(t.exporterDir,t.runtimeDir,"crawl",["--url",o],t.configPath,i,e.id),await M(e,t),{exitCode:0}):{exitCode:2,error:"Missing url for command 'url'"}}return e.command==="content-sync"?!e.ruleId||!e.coalesceKey?{exitCode:2,error:"Content-sync queue jobs require ruleId and coalesceKey."}:(await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync"}),await q(t.exporterDir,t.runtimeDir,"content-sync",["--job-id",e.id],t.configPath,i,e.id),await M(e,t),{exitCode:0}):{exitCode:2,error:`Unsupported command: ${e.command}`}}catch(r){return r instanceof $?{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 Qe(e){return async function(n=process.argv.slice(2)){let r=At(n),i=m.join(r.runtimeDir,"export.lock"),a=m.join(r.runtimeDir,"last-run.json");await E(r.runtimeDir),await C(r,"starting",{message:"queue-runner starting"});try{await Ut(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 Ht(r);let s=await Mt(r,e),o=0;for(let c=0;c<r.maxJobs;c++){let u=await Xt(r,e);if(u==="none"||(o+=1,u==="stopped"))break}if(o===0)await E(r.runtimeDir),await C(r,"idle",{processedJobs:o,schedulerEnqueued:s,message:"no queued jobs"});else {await E(r.runtimeDir);let c=await w(a,null),u=String(c?.status||"").trim();await C(r,u==="success"?"job-success":u==="stopped"?"job-stopped":"job-failed",{processedJobs:o,schedulerEnqueued:s,lastJobId:c?.id,lastJobCommand:c?.command,lastJobStatus:c?.status,lastJobExitCode:c?.exitCode,lastJobError:c?.error,...u==="stopped"?{currentStep:c?.stoppedStep,stopRequestedAt:c?.stopRequestedAt,stopRequestedByLogin:c?.stopRequestedByLogin,stopRequestedMode:c?.stopMode,lastStoppedStep:c?.stoppedStep,message:c?.stopMode==="requeue"?`Job stopped during ${c?.stoppedStep||c?.command||"active step"} and requeued.`:`Job stopped during ${c?.stoppedStep||c?.command||"active step"} and left out of queue.`}:{}});}}catch(s){let o=s instanceof Error?s.message:String(s);throw await C(r,"error",{message:o}),await R(r.runtimeDir,{eventType:"queue-runner-error",status:"failed",actorSource:"queue-runner",message:"Unhandled queue-runner error.",details:{error:o}}),s}finally{await $t(i);}}}async function Oe(e,t){if(t.command!=="content-sync"||!t.coalesceKey)return false;let n=se(e.runtimeDir),r=await Te(n,t.coalesceKey),i=await De(n),a=i.rules[t.coalesceKey];return a&&(i.rules[t.coalesceKey]={...a,trailingWorkDetected:true,retryAttempt:0,nextRetryAt:null,lastError:null},await ke(n,i)),r}async function Gt(e,t,n){let r=m.join(e.runtimeDir,"queue.json"),i=m.join(e.runtimeDir,"current-run.json");await F(e.runtimeDir,async()=>{let a=await w(r,[]),s=Array.isArray(a)?a.filter(u=>u?.id!==t.id):[],o=pe(t),c=zt(t,n);c?o.resumeFromStep=c:delete o.resumeFromStep,await v(r,[o,...s]),await v(i,null);});}async function Yt(e,t,n){let r=Math.max(0,t.attempt??0)+1,i=Math.min(3600*1e3,6e4*2**(r-1)),a=Math.floor(i*.2*Math.random()),s=new Date(Date.now()+i+a).toISOString(),o={...pe(t),status:"retry-wait",attempt:r,nextAttemptAt:s,error:n};if(await F(e.runtimeDir,async()=>{let c=m.join(e.runtimeDir,"queue.json"),u=m.join(e.runtimeDir,"current-run.json"),d=(await w(c,[])).filter(p=>p.id!==t.id);await v(c,[o,...d]),await v(u,null);}),t.coalesceKey){let c=m.join(e.runtimeDir,"content-sync-state.json"),g=(await w(c,{})).rules??{};g[t.coalesceKey]={...g[t.coalesceKey]??{},ruleId:t.ruleId||"",coalesceKey:t.coalesceKey,retryAttempt:r,nextRetryAt:s,lastError:n,trailingWorkDetected:true},await v(c,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:g});}return o}async function Zt(e,t){if(!t.coalesceKey||!t.ruleId||!(await w(m.join(e.runtimeDir,"content-sync-state.json"),null))?.rules?.[t.coalesceKey]?.trailingWorkDetected)return null;let r=null;await F(e.runtimeDir,async()=>{let a=m.join(e.runtimeDir,"queue.json"),s=await w(a,[]);de(s,{command:"content-sync",coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile})||(r={id:randomUUID(),command:"content-sync",ruleId:t.ruleId,coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile,enqueueSource:"scheduler",wpsuite:t.wpsuite,status:"queued",attempt:0,createdAt:new Date().toISOString(),createdBy:0},await v(a,[...s,r]));});let i=r;return i&&await R(e.runtimeDir,{eventType:"content-sync-trailing-job-created",status:"queued",actorSource:"queue-runner",jobId:i.id,command:i.command,message:"Content changes after the stable cutoff created one trailing job.",details:{predecessorJobId:t.id,ruleId:t.ruleId,coalesceKey:t.coalesceKey}}),i}async function Xt(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 F(e.runtimeDir,async()=>{let d=await w(n,[]);if(!Array.isArray(d)||d.length===0)return null;let p=Date.parse(String(d[0]?.nextAttemptAt||""));if(Number.isFinite(p)&&p>Date.now())return null;let h={...d[0],status:"running",startedAt:a};return delete h.nextAttemptAt,await v(n,d.slice(1)),await v(r,h),h});if(!s)return "none";await E(e.runtimeDir),await R(e.runtimeDir,{eventType:"job-run-started",status:"running",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Job execution started.",details:{createdAt:s.createdAt||"",startedAt:a,createdBy:s.createdBy??null,deploymentProfile:s.deploymentProfile||"",queuedWithTempAwsCreds:!!s.awsTempCreds}}),await C(e,"running",{currentJobId:s.id,currentJobCommand:s.command,currentStep:s.command});let o=await Vt(s,e,t),c=new Date().toISOString();if(o.stopped){let d=await _t(e,o.stoppedStep||s.command),p=o.stopRequest?.mode==="requeue"?"requeue":"stop",h={...s,status:"stopped",endedAt:c,exitCode:o.exitCode,...o.error?{error:o.error}:{},stopRequestedAt:o.stopRequest?.requestedAt||"",stopRequestedByUserId:typeof o.stopRequest?.requestedByUserId=="number"?o.stopRequest.requestedByUserId:null,stopRequestedByLogin:o.stopRequest?.requestedByLogin||"",stopMode:p,stoppedStep:d},S=false;p==="requeue"?await Gt(e,s,d):(await v(r,null),S=await Oe(e,s));try{let l=await K(h,e);h.logArchiveDir=l.archiveDir,h.logArchiveCreatedAt=l.archiveCreatedAt,h.logArchiveFileCount=l.archivedFiles.length;}catch(l){h.logArchiveError=l instanceof Error?l.message:String(l);}return await v(i,h),await U(e.runtimeDir,s.id),await E(e.runtimeDir),await R(e.runtimeDir,{eventType:"job-run-stopped",status:"stopped",actorSource:"queue-runner",jobId:s.id,command:s.command,message:p==="requeue"?"Job stop requested and requeued.":"Job stop requested and removed from active execution without requeue.",details:{startedAt:s.startedAt||"",endedAt:c,deploymentProfile:s.deploymentProfile||"",stopMode:p,stopRequestedAt:o.stopRequest?.requestedAt||"",stopRequestedByLogin:o.stopRequest?.requestedByLogin||"",stopRequestedByUserId:typeof o.stopRequest?.requestedByUserId=="number"?o.stopRequest.requestedByUserId:null,stoppedStep:d,logArchiveKey:h.logArchiveError||!h.logArchiveDir?"":m.basename(h.logArchiveDir),logArchiveDir:h.logArchiveDir||"",logArchiveCreatedAt:h.logArchiveCreatedAt||"",logArchiveFileCount:h.logArchiveFileCount??0,logArchiveError:h.logArchiveError||"",contentSyncAbandoned:S,journalCursorPreserved:S}}),"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 S="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 Ne(e,s.ruleId,s.coalesceKey,S);let l={...s,status:"failed",endedAt:c,exitCode:o.exitCode,error:S};await v(r,null),await E(e.runtimeDir),await Oe(e,s);try{let y=await K(l,e);l.logArchiveDir=y.archiveDir,l.logArchiveCreatedAt=y.archiveCreatedAt,l.logArchiveFileCount=y.archivedFiles.length;}catch(y){l.logArchiveError=y instanceof Error?y.message:String(y);}return await v(i,l),await U(e.runtimeDir,s.id),await R(e.runtimeDir,{eventType:"content-sync-baseline-required",status:"failed",actorSource:"queue-runner",jobId:s.id,command:s.command,message:S,details:{ruleId:s.ruleId||"",retryScheduled:false}}),"processed"}await R(e.runtimeDir,{eventType:"content-sync-failed",status:"failed",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Content-sync execution failed before cursor acknowledgement.",details:{attempt:s.attempt??0,error:o.error||"Content-sync execution failed."}});let p=await Yt(e,s,o.error||"Content-sync execution failed."),h={...s,status:"retry-wait",endedAt:c,exitCode:o.exitCode,error:p.error,attempt:p.attempt,nextAttemptAt:p.nextAttemptAt};try{let S=await K(h,e);h.logArchiveDir=S.archiveDir,h.logArchiveCreatedAt=S.archiveCreatedAt,h.logArchiveFileCount=S.archivedFiles.length;}catch(S){h.logArchiveError=S instanceof Error?S.message:String(S);}return await v(i,h),await U(e.runtimeDir,s.id),await R(e.runtimeDir,{eventType:"content-sync-retry-scheduled",status:"retry-wait",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Content-sync retry was scheduled with bounded backoff.",details:{attempt:p.attempt??0,nextAttemptAt:p.nextAttemptAt||"",error:p.error||""}}),"processed"}let u={...s,status:o.exitCode===0?"success":"failed",endedAt:c,exitCode:o.exitCode,...o.error?{error:o.error}:{}};await v(r,null),u.status==="success"&&u.command==="content-sync"&&await Zt(e,u),await U(e.runtimeDir,s.id);try{let d=await K(u,e);u.logArchiveDir=d.archiveDir,u.logArchiveCreatedAt=d.archiveCreatedAt,u.logArchiveFileCount=d.archivedFiles.length;}catch(d){u.logArchiveError=d instanceof Error?d.message:String(d);}await v(i,u);let g=s.startedAt&&u.endedAt?Math.max(0,Math.round((new Date(u.endedAt).getTime()-new Date(s.startedAt).getTime())/1e3)):void 0;return await R(e.runtimeDir,{eventType:"job-run-finished",status:u.status==="success"?"success":"failed",actorSource:"queue-runner",jobId:u.id,command:u.command,message:u.status==="success"?"Job execution finished successfully.":"Job execution finished with failure.",details:{startedAt:s.startedAt||"",endedAt:u.endedAt||"",durationSec:g,deploymentProfile:u.deploymentProfile||"",exitCode:u.exitCode??null,error:u.error||"",logArchiveKey:u.logArchiveError||!u.logArchiveDir?"":m.basename(u.logArchiveDir),logArchiveDir:u.logArchiveDir||"",logArchiveCreatedAt:u.logArchiveCreatedAt||"",logArchiveFileCount:u.logArchiveFileCount??0,logArchiveError:u.logArchiveError||""}}),"processed"}async function tn(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 nn={allowSchedulerAutoEnqueue:true,enforceSubscriptionOnJobExecution:false,shouldEnforceSubscriptionOnJobExecution:e=>Le(e)||e.enqueueSource==="scheduler"||e.createdBy===0,assertActiveSubscriptionForIdentity:tn},We=Qe(nn),rn=We,Hn=We;process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href&&rn().catch(e=>{console.error(e),process.exit(1);});export{Hn as default,rn as main,We 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 Ge from'http';import be from'https';import {pipeline}from'stream/promises';import {createGzip}from'zlib';function he(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function G(e){return Array.isArray(e)?e.map(G):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>[t,G(n)])):e}function _e(e){return JSON.stringify(G(e))}function J(e){return createHash("sha256").update(_e(e)).digest("hex")}function fe(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 He(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 N(e){let t=he((e.postTypes??[]).map(r=>String(r).toLowerCase().replace(/[^a-z0-9_-]/g,"")));if(t.length===0)throw new Error("Content-sync requires at least one public post type.");let n=he((e.listingPaths??[]).map(r=>{let i=String(r).trim();if(!i)return "";let a=new URL(i,"https://content-sync.invalid/");if(a.origin!=="https://content-sync.invalid"&&/^https?:\/\//i.test(i))throw new Error("Content-sync listing routes must be same-site paths.");let o=`/${a.pathname.replace(/^\/+|\/+$/g,"")}`;return o==="/"?"/":`${o}/`}));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 Y(e,t,n){return J({schemaVersion:1,sourceOrigin:He(e),ruleId:t.trim(),scope:N(n)})}function Z(e){return J({schemaVersion:1,...e})}function Se(e){return e.replace(/((?:x-static-publisher-token|authorization|proxy-authorization|x-api-key|aws-access-key-id|aws-secret-access-key|aws-session-token)\s*:\s*)[^\r\n]+/gi,"$1[REDACTED]").replace(/\b(Basic|Bearer)\s+[^\s]+/gi,"$1 [REDACTED]").replace(/([?&](?:token|access_token|refresh_token|api_key|key|nonce)=)[^&\s]+/gi,"$1[REDACTED]").replace(/((?:"|')?(?:runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken)(?:"|')?\s*[:=]\s*(?:"|'))[^"']+/gi,"$1[REDACTED]")}var ye=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";ye?m.join(ye,"current-progress.json"):"";Promise.resolve();var Ye="wp-json/smartcloud-static-publisher/v1/content-sync/",Ze=25*1024*1024,T=class extends Error{status;code;constructor(t,n,r){super(r),this.name="ContentSyncApiError",this.status=t,this.code=n;}},L=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,n="content-sync"){let r=this.options.sourceOrigin.replace(/\/+$/,"")+"/",i=n==="content-sync"?Ye:`wp-json/smartcloud-static-publisher/v1/${n}/`;return new URL(`${i}${t}`,r)}async post(t,n,r="content-sync"){let i=this.endpoint(t,r),a=Buffer.from(JSON.stringify(n),"utf8"),o=i.protocol==="https:"?be:Ge,s=i.protocol==="https:"?new be.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((c,u)=>{let g=o.request(i,{method:"POST",agent:s,headers:{"content-type":"application/json","content-length":String(a.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},d=>{let p=[],h=0;d.on("data",S=>{if(h+=S.byteLength,h>Ze){d.destroy(new Error("Content-sync API response exceeded the size limit."));return}p.push(S);}),d.on("error",u),d.on("end",()=>{let S=Buffer.concat(p).toString("utf8"),l;try{l=S?JSON.parse(S):{};}catch{u(new T(d.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let y=d.statusCode??500;if(y<200||y>=300){u(new T(y,String(l.code??"request-failed"),String(l.message??`Content-sync API request failed (${y}).`)));return}c(l);});});g.on("timeout",()=>g.destroy(new Error(`Content-sync API request timed out: ${t}`))),g.on("error",u),g.end(a);})}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 o=r;for(let s of a.items){if(!Number.isSafeInteger(s.sequence)||s.sequence<=o||s.sequence>t.throughSequence||!Number.isSafeInteger(s.postId)||s.postId<=0||!Number.isSafeInteger(s.blogId)||s.blogId<=0||!i.has(s.postType)||typeof s.operation!="string"||!s.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");o=s.sequence;}if(a.items.length>0&&a.nextSequence!==o)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)}async importCookieObservations(t){return this.post("cookie-observations",t,"privacy")}};function Xe(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function et(e,t){return {...e,...t??{}}}function tt(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function X(e,t){let n=String(t??e.deploymentTargetOverride??"").trim();if(!Xe(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:et(e.s3,i.s3),cloudFront:tt(e.cloudFront,i.cloudFront)}}}var ee=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function we(e){let t=new Set;for(let n of [...e,...ee]){let r=String(n||"").trim();r&&t.add(r);}return [...t]}function Pe(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function ot(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(o=>o.replace(/[^A-Za-z0-9._-]/g,"")).filter(o=>o.length>0&&o!=="."&&o!=="..");if(i.length===0)return "";let a=`/${i.join("/")}`;return r&&m.extname(a)===""&&(a+="/"),a==="/"?"":a}function te(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t.trim(),String(n??"")]).filter(([t])=>t.length>0))}function st(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,o={};if(typeof a.targetOrigin=="string"){let c=Pe(a.targetOrigin);c&&(o.targetOrigin=c);}let s=te(a.extraReplacements);if(Object.keys(s).length>0&&(o.extraReplacements=s),a.s3&&typeof a.s3=="object"){let c={},u=a.s3;typeof u.bucket=="string"&&(c.bucket=u.bucket.trim()),typeof u.prefix=="string"&&(c.prefix=u.prefix.trim()),typeof u.region=="string"&&(c.region=u.region.trim()),typeof u.htmlCacheControl=="string"&&(c.htmlCacheControl=u.htmlCacheControl.trim()),typeof u.assetCacheControl=="string"&&(c.assetCacheControl=u.assetCacheControl.trim()),Object.keys(c).length>0&&(o.s3=c);}if(a.cloudFront&&typeof a.cloudFront=="object"){let c={},u=a.cloudFront;typeof u.distributionId=="string"&&(c.distributionId=u.distributionId.trim()),Array.isArray(u.invalidationPaths)&&(c.invalidationPaths=u.invalidationPaths.map(g=>String(g??"").trim()).filter(g=>g.length>0)),Object.keys(c).length>0&&(o.cloudFront=c);}t[i]=o;}return t}function re(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function at(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 o=String(n.deploymentProfile??"").trim(),s=String(n.url??"").trim(),c=Array.isArray(n.postTypes)?[...new Set(n.postTypes.map(g=>String(g??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],u=Array.isArray(n.listingPaths)?[...new Set(n.listingPaths.map(g=>String(g??"").trim()).filter(Boolean))]:[];return r==="content-sync"&&c.length===0?null:{id:a,enabled:n.enabled!==false,command:r,intervalMinutes:i,...r==="publish"||r==="crawl"?{crawlMode:n.crawlMode==="incremental"?"incremental":"full"}:{},...(r==="publish"||r==="deploy"||r==="invalidate"||r==="content-sync")&&o?{deploymentProfile:o}:{},...s?{url:s}:{},...c.length>0?{postTypes:c}:{},...u.length>0?{listingPaths:u}:{},...typeof n.includeSubsites=="boolean"?{includeSubsites:n.includeSubsites}:{},...typeof n.includePostTypeArchives=="boolean"?{includePostTypeArchives:n.includePostTypeArchives}:{},...typeof n.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:n.includeTaxonomyArchives}:{},...typeof n.includeAuthorArchives=="boolean"?{includeAuthorArchives:n.includeAuthorArchives}:{},...typeof n.includeDateArchives=="boolean"?{includeDateArchives:n.includeDateArchives}:{},...typeof n.includePostsPage=="boolean"?{includePostsPage:n.includePostsPage}:{},...typeof n.includeSitemapChain=="boolean"?{includeSitemapChain:n.includeSitemapChain}:{}}}function Ce(e){let t=e&&typeof e=="object"?e:{},n=Array.isArray(t.rules)?t.rules.map((r,i)=>at(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 ct(e){let t=e&&typeof e=="object"?e:{},n=t.render&&typeof t.render=="object"?t.render:{},r=Number(n.concurrency??0),i=Number(n.maxAttempts??0),a=t.rewrite&&typeof t.rewrite=="object"?t.rewrite:{},o=t.deploy&&typeof t.deploy=="object"?t.deploy:{},s=(c,u)=>({enabled:typeof c.enabled=="boolean"?c.enabled:n.enabled===true,concurrency:Math.min(100,Math.max(1,Number(c.concurrency||u.concurrency))),batchSize:Math.min(500,Math.max(1,Number(c.batchSize||u.batchSize))),maxAttempts:Math.min(5,Math.max(1,Number(c.maxAttempts||2)))});return {render:{enabled:n.enabled===true,...Number.isInteger(r)&&r>0?{concurrency:r}:{},...Number.isInteger(i)&&i>0?{maxAttempts:Math.min(5,i)}:{}},rewrite:s(a,{concurrency:8,batchSize:25}),deploy:s(o,{concurrency:8,batchSize:50})}}function ut(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 ie(e){let t=e&&typeof e=="object"?e:{},n=String(t.accountId??"").trim(),r=String(t.siteId??"").trim(),i=t.subscriber===true,a=ut(t.siteSettings),o={...a??{},...n?{accountId:a?.accountId??n}:{},...r?{siteId:a?.siteId??r}:{},...a?.subscriber===true||i?{subscriber:true}:{}},s={},c=String(t.apiBase??"").trim();c&&(s.apiBase=c);let u=String(t.runtimeToken??t.nonce??"").trim();u&&(s.runtimeToken=u);let g=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();g&&(s.virtualAssetBaseUrl=g),Object.keys(o).length>0&&(s.siteSettings=o);let d=re(t.subscriptionType);return d&&(s.subscriptionType=d),s}function lt(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function Ae(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function dt(e,t){let n=new Headers(e instanceof Request?e.headers:void 0);if(t?.headers)for(let[r,i]of new Headers(t.headers).entries())n.set(r,i);return n}async function Ie(e,t,n=5){let r=new URL(Ae(e)),i=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),a=r.protocol==="http:"?Ge:be,o=dt(e,t);return await new Promise((s,c)=>{let u=a.request(r,{method:i,headers:Object.fromEntries(o.entries()),...r.protocol==="https:"?{rejectUnauthorized:false}:{}},g=>{let d=[];g.on("data",p=>{d.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),g.on("error",c),g.on("end",()=>{let p=g.statusCode??0,h=g.headers.location;if(n>0&&h&&[301,302,303,307,308].includes(p)){s(Ie(new URL(h,r),{method:p===303?"GET":i},n-1));return}let S=new Headers;for(let[l,y]of Object.entries(g.headers))Array.isArray(y)?S.set(l,y.join(", ")):typeof y=="string"&&S.set(l,y);s(new Response(Buffer.concat(d),{status:p,statusText:g.statusMessage??"",headers:S}));});});u.on("error",c),u.end();})}function gt(e){let t=ie(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;lt(e.sourceOrigin);let o=globalThis,c={...o.WpSuite??{},siteSettings:n,uploadUrl:a};return t.apiBase&&(c.apiBase=t.apiBase),o.WpSuite=c,true}async function pt(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!gt(e)){let i=e.wpsuite?.siteSettings,a=[String(i?.accountId??"").trim()?"":"accountId",String(i?.siteId??"").trim()?"":"siteId",t?"":"virtualAssetBaseUrl"].filter(Boolean);return {remote:null,diagnostic:{status:"invalid-runtime",virtualAssetBaseUrl:t,subscriptionType:null,error:`Missing WP Suite runtime field(s): ${a.join(", ")}.`,requests:[]}}}let n=globalThis.fetch,r=[];try{typeof n=="function"&&/^https?:\/\//i.test(t)&&(globalThis.fetch=(async(c,u)=>{let g=Ae(c);if(g.startsWith(t)){let d=(()=>{try{return new URL(g).pathname}catch{return g}})();try{let p=e.ignoreHttpsErrors&&g.startsWith("https://")?await Ie(c,u):await n(c,u);return r.push({path:d,status:p.status,ok:p.ok,error:null}),p}catch(p){throw r.push({path:d,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return n(c,u)}));let{getConfig:i}=await import('@smart-cloud/wpsuite-core'),a=await i("publisher"),o=a&&typeof a=="object"?a:null,s=re(o?.subscriptionType);return {remote:o,diagnostic:{status:o?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:s??null,error:o?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 mt(e,t){let n=st(t?.deploymentProfiles),r=String(t?.defaultDeploymentProfile??"").trim(),i=n[r]?r:"",a=re(t?.subscriptionType),o=ie(e.wpsuite);return {...e,scheduler:Ce(t?.scheduler),deploymentProfiles:n,defaultDeploymentProfile:i,...a?{subscriptionType:a}:{},wpsuite:{...o,...o.siteSettings||a?{siteSettings:{...o.siteSettings??{},...o.siteSettings?.subscriber===true||a==="PROFESSIONAL"||a==="AGENCY"?{subscriber:true}:{}}}:{},...a?{subscriptionType:a}:{}}}}function ne(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 ht(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?m.resolve(e):""}function ve(e,t,n){let r=String(t||"").trim();return r&&m.isAbsolute(r)?m.resolve(r):m.resolve(e,ne(r,n))}async function W(e,t={}){let n=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",r=await P.readFile(n,"utf8"),i=JSON.parse(r);i.sourceOrigin=i.sourceOrigin.replace(/\/$/,""),i.targetOrigin=Pe(i.targetOrigin),i.ignoreHttpsErrors??=false,i.outputDir=String(i.outputDir||"export").trim()||"export",i.urlRewriteMode||=i.targetOrigin==="."?"relative":"absolute",i.noJavaScriptRenderPathPrefixes||=[],i.seedPaths||=[],i.generated404RequestPath=ot(i.generated404RequestPath),i.sitemapPaths||=[...ee],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=te(i.extraReplacements),i.postCrawlCopyMap=te(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=ie(i.wpsuite),i.scheduler=Ce(void 0),i.remoteWorkers=ct(i.remoteWorkers),i.deploymentProfiles={},i.defaultDeploymentProfile="",i.deploymentTargetOverride=String(i.deploymentTargetOverride??"").trim();let a=await pt(i);t.onRemotePublisherConfigDiagnostic?.(a.diagnostic);let o=mt(i,a.remote),s=ht(),c=s?m.resolve(s,".."):"";return c?(o.outputDir=ve(c,o.outputDir,"export"),o.logDir=ve(c,o.logDir,"logs")):(m.isAbsolute(o.outputDir)||(o.outputDir=ne(o.outputDir,"export")),m.isAbsolute(o.logDir)||(o.logDir=ne(o.logDir,"logs"))),o}function Re(e){let t=e.split(/\r?\n/).map(r=>r.trim()).filter(Boolean),n=[...t].reverse().find(r=>/^(?:Error|[A-Za-z][A-Za-z0-9]*Error):\s/.test(r));return n||(t.find(r=>!r.startsWith("at "))??"")}function oe(e){let t=m.resolve(e);return {state:m.join(t,"content-sync-state.json"),current:m.join(t,"content-sync-current.json"),impact:m.join(t,"content-sync-impact-plan.json"),checkpoint:m.join(t,"content-sync-checkpoint.json"),baseline:m.join(t,"content-sync-baseline.json"),activeRules:m.join(t,"content-sync-active-rules.json"),releaseCutoff:m.join(t,"content-sync-release-cutoff.json"),candidateManifest:m.join(t,"content-sync-candidate-manifest.json"),trustedManifest:m.join(t,"crawl-manifest.json"),invalidation:m.join(t,"content-sync-invalidation.json"),deployPlan:m.join(t,"deploy-plan.json")}}async function se(e,t){await xe(e.activeRules,{contractVersion:1,evaluatedAt:new Date().toISOString(),entries:t});}async function xe(e,t){await P.mkdir(m.dirname(e),{recursive:true});let n=`${e}.${process.pid}.${Date.now()}.tmp`;await P.writeFile(n,JSON.stringify(t,null,2),"utf8"),await P.rename(n,e);}async function qe(e){try{return JSON.parse(await P.readFile(e,"utf8"))}catch(t){if(t.code==="ENOENT")return null;throw new Error(`Invalid or unreadable content-sync state file ${e}: ${t instanceof Error?t.message:String(t)}`,{cause:t})}}async function ft(e){let t=await qe(e.current);if(t===null)return null;let n=t;if(n.schemaVersion!==1||typeof n.jobId!="string"||typeof n.phase!="string"||!Number.isSafeInteger(n.fromSequence)||!Number.isSafeInteger(n.toSequence))throw new Error("Invalid content-sync current-range state.");return n}async function ke(e){let t=await qe(e.state);if(t===null)return {schemaVersion:1,updatedAt:"",rules:{}};let n=t;if(n.schemaVersion!==1||typeof n.updatedAt!="string"||!n.rules||typeof n.rules!="object"||Array.isArray(n.rules))throw new Error("Invalid content-sync state store.");return n}async function De(e,t){await xe(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function Te(e,t){let n=await ft(e);return !n||n.coalesceKey!==t?false:(await Promise.all([e.current,e.checkpoint,e.impact,e.candidateManifest,e.invalidation,e.deployPlan].map(async r=>{try{await P.unlink(r);}catch(i){if(i.code!=="ENOENT")throw i}})),true)}function Le(e){let t=String(e.command??"").trim();return (t==="publish"||t==="crawl")&&e.crawlMode==="incremental"||t==="content-sync"}var ae=new Map;async function Ee(e){let t=m.resolve(e),n=ae.get(t);if(n!==void 0)return n;try{let r=JSON.parse(await P.readFile(t,"utf8")),i=String(r.version??"").trim();return ae.set(t,i),i}catch{return ae.set(t,""),""}}var $=class extends Error{request;step;constructor(t,n){super(`Stop requested during ${n}.`),this.name="StopRequestedError",this.request=t,this.step=n;}},Pt="queue-mutation.lock",Ct=5e3,At=3e4;function It(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 s=0;s<e.length;s++){let c=e[s];c==="--runtime-dir"?t=e[++s]||"":c.startsWith("--runtime-dir=")?t=c.slice(14):c==="--exporter-dir"?n=e[++s]||n:c.startsWith("--exporter-dir=")?n=c.slice(15):c==="--config"?r=e[++s]||"":c.startsWith("--config=")?r=c.slice(9):c==="--max-jobs"?i=Number.parseInt(e[++s]||"1",10):c.startsWith("--max-jobs=")&&(i=Number.parseInt(c.slice(11),10));}if(!t)throw new Error("Missing runtime dir. Use --runtime-dir or STATIC_PUBLISHER_RUNTIME_DIR.");let a=m.resolve(t),o=r?m.resolve(r):m.join(a,"config.json");return {runtimeDir:a,exporterDir:m.resolve(n),configPath:o,maxJobs:Number.isFinite(i)&&i>0?i:1}}async function w(e,t){try{let n=await P.readFile(e,"utf8");return JSON.parse(n)}catch(n){if((n&&typeof n=="object"&&"code"in n?String(n.code||""):"")!=="ENOENT"){let i=n instanceof Error?n.message:String(n);console.warn(`[queue-runner] failed to read JSON ${e}: ${i}`);}return t}}async function v(e,t){await P.mkdir(m.dirname(e),{recursive:true}),await P.writeFile(e,JSON.stringify(t,null,2),"utf8");}function Be(e){return m.join(e,Pt)}async function Rt(e){await new Promise(t=>setTimeout(t,e));}async function xt(e){let t=Be(e),n=Date.now()+Ct;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 Rt(50);}}async function qt(e){await P.unlink(Be(e)).catch(()=>{});}async function F(e,t){await xt(e);try{return await t()}finally{await qt(e);}}function Me(e){return m.join(e,"queue-runner-heartbeat.json")}function ge(e){return m.join(e,"current-progress.json")}function ce(e){return m.join(e,"scheduler-state.json")}function kt(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 Ue(e){let t=await w(e.configPath||"",null),n=m.resolve(e.runtimeDir,".."),i=(typeof t?.logDir=="string"?t.logDir:"").trim();return i&&m.isAbsolute(i)?m.resolve(i):m.resolve(n,kt(i,"logs"))}function ue(e,t){return String(e||"").trim().replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/^-+|-+$/g,"")||t}function Dt(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 Tt(e){return [Dt(e.endedAt||e.startedAt),ue(e.command||"job","job"),ue(e.id||"job","job"),ue(e.status||"finished","finished")].join("-")}function Et(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 jt(e){return e.endsWith(".gz")?"application/gzip":e.endsWith(".jsonl")?"application/x-ndjson":e.endsWith(".json")?"application/json":"text/plain"}async function Q(e,t,n){let r=`${n}.gz`,i=m.join(t,r);await pipeline(createReadStream(e),createGzip({level:9}),createWriteStream(i));let[a,o]=await Promise.all([P.stat(e),P.stat(i)]);return {role:Ft(n),originalFileName:n,storedFileName:r,compressed:true,compression:"gzip",contentType:jt(r),originalSize:a.size,storedSize:o.size}}async function K(e,t){let n=await Ue(t),r=new Date().toISOString(),i=Tt(e),a=m.join(n,"archive",i),o=[],s=[];await P.mkdir(a,{recursive:true});try{let d=await P.readdir(n,{withFileTypes:!0});for(let p of d){if(!p.isFile()||!Et(p.name))continue;let h=await Q(m.join(n,p.name),a,p.name);s.push(h),o.push(h.storedFileName);}}catch(d){if((d&&typeof d=="object"&&"code"in d?String(d.code||""):"")!=="ENOENT")throw d}let c=ge(t.runtimeDir);if(existsSync(c)){let d=await Q(c,a,"current-progress.json");s.push(d),o.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 p=m.join(t.runtimeDir,d);if(!existsSync(p))continue;let h=await Q(p,a,d);s.push(h),o.push(h.storedFileName);}let u=m.join(t.runtimeDir,"privacy","cookie-observations.json");if(existsSync(u)){let d=await Q(u,a,"cookie-observations.json");s.push(d),o.push(d.storedFileName);}let g=[...o,"job.json"];return await v(m.join(a,"job.json"),{manifestVersion:1,archivedAt:r,archiveKey:i,archiveDir:a,logDir:n,runtimeDir:t.runtimeDir,exporterDir:t.exporterDir,configPath:t.configPath||"",archivedFiles:g,artifacts:s,job:e}),{archiveKey:i,archiveDir:a,archiveCreatedAt:r,archivedFiles:g,artifacts:s}}function Ot(e){return m.join(e,"audit-events.jsonl")}function le(e){return m.join(e,"stop-request.json")}async function $e(e){try{let t=await P.readFile(le(e),"utf8"),n=JSON.parse(t);return n&&typeof n=="object"?n:null}catch{return null}}async function Fe(e,t){let n=await $e(e);if(!n)return null;let r=String(n.targetJobId||"").trim();return r&&r!==t?null:n}async function U(e,t){if(!t){await P.unlink(le(e)).catch(()=>{});return}let n=await $e(e);if(!n)return;let r=String(n.targetJobId||"").trim();(!r||r===t)&&await P.unlink(le(e)).catch(()=>{});}async function R(e,t){try{let n={occurredAt:t.occurredAt||new Date().toISOString(),status:t.status||"info",actorSource:t.actorSource||"queue-runner",...t};await P.mkdir(e,{recursive:!0}),await P.appendFile(Ot(e),`${JSON.stringify(n)}
2
+ `,"utf8");}catch{}}async function C(e,t,n){let[r,i]=await Promise.all([Ee(m.join(e.exporterDir,"package.json")),Ee(m.join(e.exporterDir,"node_modules","@smart-cloud","wpsuite-core","package.json"))]),a={checkedAt:new Date().toISOString(),status:t,pid:process.pid,nodePath:process.execPath,nodeVersion:process.version,exporterVersion:r,wpsuiteCoreVersion:i,runtimeDir:e.runtimeDir,exporterDir:e.exporterDir,...n??{}};try{await v(Me(e.runtimeDir),a);}catch{}}async function E(e){try{await P.unlink(ge(e));}catch{}}function Jt(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,o=typeof e.url=="string"?e.url.trim():"",s=typeof e.deploymentProfile=="string"?e.deploymentProfile.trim():"",c=Array.isArray(e.postTypes)?[...new Set(e.postTypes.map(d=>String(d).trim()).filter(Boolean))]:[],u=Array.isArray(e.listingPaths)?[...new Set(e.listingPaths.map(d=>String(d).trim()).filter(Boolean))]:[],g=(n==="publish"||n==="crawl")&&e.crawlMode==="incremental"?"incremental":"full";return n==="url"&&!o||n==="content-sync"&&c.length===0?null:{id:i,enabled:a,command:n,intervalMinutes:r,...n==="publish"||n==="crawl"?{crawlMode:g}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&s?{deploymentProfile:s}:{},...o?{url:o}:{},...c.length>0?{postTypes:c}:{},...u.length>0?{listingPaths:u}:{},...typeof e.includeSubsites=="boolean"?{includeSubsites:e.includeSubsites}:{},...typeof e.includePostTypeArchives=="boolean"?{includePostTypeArchives:e.includePostTypeArchives}:{},...typeof e.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:e.includeTaxonomyArchives}:{},...typeof e.includeAuthorArchives=="boolean"?{includeAuthorArchives:e.includeAuthorArchives}:{},...typeof e.includeDateArchives=="boolean"?{includeDateArchives:e.includeDateArchives}:{},...typeof e.includePostsPage=="boolean"?{includePostsPage:e.includePostsPage}:{},...typeof e.includeSitemapChain=="boolean"?{includeSitemapChain:e.includeSitemapChain}:{}}}async function Lt(e){let t=await W(e.configPath).catch(()=>null),n=t?.scheduler,r=Array.isArray(n?.rules)?n.rules.map((i,a)=>Jt(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 _(e,t){if(!t)throw new Error("Content-sync scheduler requires a valid publisher config.");let n=N({postTypes:e.postTypes??[],listingPaths:e.listingPaths??[],includeSubsites:e.includeSubsites,includePostTypeArchives:e.includePostTypeArchives,includeTaxonomyArchives:e.includeTaxonomyArchives,includeAuthorArchives:e.includeAuthorArchives,includeDateArchives:e.includeDateArchives,includePostsPage:e.includePostsPage,includeSitemapChain:e.includeSitemapChain}),r=Y(t.sourceOrigin,e.id,n),i=X(t,e.deploymentProfile),a=Z({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)}`}function Bt(e,t,n,r){let i=[n[t],r[t]].filter(o=>!!o).filter(o=>o.coalesceKey===t&&o.ruleId===e.id).map(o=>String(o.consumerId??"").trim()).filter(Boolean),a=[...new Set(i)];return a.length===1?a[0]:""}async function je(e,t){let n=oe(e.runtimeDir);if(await se(n,[]),!t?.enabled||!t.publisherConfig)return;let r=t.rules.filter(u=>u.enabled&&u.command==="content-sync");if(r.length===0)return;let[i,a]=await Promise.all([w(n.state,{}),w(n.baseline,{})]),o=i.rules??{},s=a.entries??{},c=[];for(let u of r){let g=_(u,t.publisherConfig),d=Bt(u,g,o,s);d&&c.push({ruleId:u.id,coalesceKey:g,consumerId:d});}await se(n,c);}async function Mt(e,t,n,r,i){let o=(await w(m.join(e.runtimeDir,"content-sync-baseline.json"),{})).entries?.[r];if(!o?.consumerId||!o.scopeFingerprint||!o.baselineId)return {status:"baseline-required",reason:"No verified normal-release baseline exists for this rule scope."};try{let s=t.publisherConfig;if(!s)throw new Error("Publisher configuration is unavailable.");let c=N({postTypes:n.postTypes??[],listingPaths:n.listingPaths??[],includeSubsites:n.includeSubsites,includePostTypeArchives:n.includePostTypeArchives,includeTaxonomyArchives:n.includeTaxonomyArchives,includeAuthorArchives:n.includeAuthorArchives,includeDateArchives:n.includeDateArchives,includePostsPage:n.includePostsPage,includeSitemapChain:n.includeSitemapChain}),u=Y(s.sourceOrigin,n.id,c),g=X(s,n.deploymentProfile),d=Z({sourceOrigin:s.sourceOrigin,targetOrigin:g.config.targetOrigin,deploymentProfile:g.name||"",s3Bucket:g.config.s3.bucket,s3Prefix:g.config.s3.prefix,cloudFrontDistributionId:g.config.cloudFront.distributionId,urlRewriteMode:g.config.urlRewriteMode,extraReplacements:g.profile?.extraReplacements??{}});if(o.scopeFingerprint!==u||o.targetFingerprint!==d||!o.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 L({sourceOrigin:s.sourceOrigin,runtimeToken:String(s.wpsuite?.runtimeToken||""),ignoreHttpsErrors:s.ignoreHttpsErrors}),h=await p.releaseFingerprint(c.includeSubsites),S=fe({wordpressFingerprint:h,targetFingerprint:d,sourceOrigin:s.sourceOrigin,sitemapPaths:we(s.sitemapPaths),blockedPathPrefixes:s.blockedPathPrefixes,assetPathPrefixes:s.assetPathPrefixes,noJavaScriptRenderPathPrefixes:s.noJavaScriptRenderPathPrefixes,urlRewriteMode:s.urlRewriteMode});if(o.releaseFingerprint!==S)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:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:o.baselineId,postTypes:c.postTypes,includeSubsites:c.includeSubsites}),y=l.committedSequence;if(i?.command==="content-sync"&&i.coalesceKey===r){let b=await w(m.join(e.runtimeDir,"content-sync-current.json"),null);b?.coalesceKey===r&&Number.isSafeInteger(b.toSequence)&&(y=Math.max(y,Number(b.toSequence)));}return {status:l.headSequence>y?"pending":"current",headSequence:l.headSequence,committedSequence:l.committedSequence}}catch(s){return s instanceof T&&s.code==="baseline-required"?{status:"baseline-required",reason:s.message}:{status:"unavailable",reason:s instanceof Error?s.message:String(s)}}}async function Ne(e,t,n,r,i,a){let o=m.join(e.runtimeDir,"content-sync-state.json"),c=(await w(o,{})).rules??{},u=c[n]??{};c[n]={...u,ruleId:t,coalesceKey:n,...Number.isSafeInteger(i)?{observedHeadSequence:i}:{},...Number.isSafeInteger(a)?{committedSequence:a}:{},retryAttempt:0,nextRetryAt:null,lastError:r,baselineStatus:"required",baselineReason:r,trailingWorkDetected:Number.isSafeInteger(i)&&Number.isSafeInteger(a)?Number(i)>Number(a):!!u.trailingWorkDetected},await v(o,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:c});}function de(e,t){let n=(t.url||"").trim(),r=t.crawlMode||"full",i=(t.deploymentProfile||"").trim(),a=(t.coalesceKey||"").trim();return e.some(o=>o.command===t.command&&(t.command!=="content-sync"||(o.coalesceKey||"").trim()===a)&&(o.url||"").trim()===n&&(o.crawlMode||"full")===r&&(o.deploymentProfile||"").trim()===i&&(o.status===void 0||o.status==="queued"||o.status==="retry-wait"||o.status==="running"))}async function Ut(e,t,n){if(!t.allowSchedulerAutoEnqueue)return await je(e,null),0;let r=await Lt(e);if(await je(e,r),!r.enabled||r.rules.length===0)return 0;if(t.assertActiveSubscriptionForIdentity)try{await t.assertActiveSubscriptionForIdentity(r.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"),a=m.join(e.runtimeDir,"current-run.json"),o=Date.now(),s=0,c=[],u=[],g=[],d=new Map,[p,h,S]=await Promise.all([w(i,[]),w(a,null),w(ce(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}})]);for(let l of r.rules){if(!l.enabled||l.command!=="content-sync")continue;let y=Math.floor(o/(l.intervalMinutes*60*1e3)),b=S.lastEvaluatedBucketByRuleId?.[l.id]??S.lastEnqueuedBucketByRuleId?.[l.id]??-1;if(y<=b)continue;let k=_(l,r.publisherConfig);de(p,{command:l.command,deploymentProfile:l.deploymentProfile,coalesceKey:k})||d.set(l.id,await Mt(e,r,l,k,h));}await F(e.runtimeDir,async()=>{let l=await w(i,[]),y=await w(a,null),b=await w(ce(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}});b.lastEnqueuedBucketByRuleId??={},b.lastEvaluatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.lastCreatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.coalescedCountByRuleId??={};let k=[...l];y&&y.status==="running"&&k.unshift(y);for(let f of r.rules){if(!f.enabled)continue;let A=f.intervalMinutes*60*1e3,q=Math.floor(o/A),j=b.lastEvaluatedBucketByRuleId[f.id]??b.lastEnqueuedBucketByRuleId[f.id]??-1;if(q<=j)continue;let O=f.command==="content-sync"?_(f,r.publisherConfig):"";b.lastEvaluatedBucketByRuleId[f.id]=q;let Ke=f.command==="content-sync"?l:k;if(de(Ke,{command:f.command,url:f.url,crawlMode:f.crawlMode,deploymentProfile:f.deploymentProfile,coalesceKey:O})){b.coalescedCountByRuleId[f.id]=(b.coalescedCountByRuleId[f.id]??0)+1,u.push(f);continue}if(f.command==="content-sync"){let V=d.get(f.id);if(!V||V.status!=="pending"){let me=V??{status:"unavailable",reason:"Content-sync demand was not checked."};me.status!=="current"&&g.push({rule:f,check:me});continue}}let H={id:randomUUID(),command:f.command,...f.command==="content-sync"?{ruleId:f.id,coalesceKey:O,attempt:0}:{},...(f.command==="publish"||f.command==="crawl")&&f.crawlMode?{crawlMode:f.crawlMode}:{},...(f.command==="publish"||f.command==="deploy"||f.command==="invalidate"||f.command==="content-sync")&&f.deploymentProfile?{deploymentProfile:f.deploymentProfile}:{},...f.url?{url:f.url}:{},enqueueSource:"scheduler",...r.wpsuite?{wpsuite:r.wpsuite}:{},status:"queued",createdAt:new Date().toISOString(),createdBy:0};l.push(H),k.push(H),c.push({job:H,rule:f}),b.lastEnqueuedBucketByRuleId[f.id]=q,b.lastCreatedBucketByRuleId[f.id]=q,s+=1;}s>0&&await v(i,l),await v(ce(e.runtimeDir),b);});for(let l of c){if(l.rule.command==="content-sync"){let y=d.get(l.rule.id);await R(e.runtimeDir,{eventType:"content-sync-demand-detected",status:"queued",actorSource:"queue-runner-scheduler",jobId:l.job.id,command:l.job.command,message:"Pending journal changes require a content-sync job.",details:{ruleId:l.rule.id,headSequence:y?.headSequence??0,committedSequence:y?.committedSequence??0}});}await R(e.runtimeDir,{eventType:"job-created",status:"queued",actorSource:"queue-runner-scheduler",jobId:l.job.id,command:l.job.command,message:"Scheduler auto-enqueued a job.",details:{ruleId:l.rule.id,intervalMinutes:l.rule.intervalMinutes,timezone:r.timezone,deploymentProfile:l.rule.deploymentProfile||"",url:l.rule.url||""}});}for(let l of u)await R(e.runtimeDir,{eventType:"content-sync-coalesced",status:"queued",actorSource:"queue-runner-scheduler",command:l.command,message:"Scheduler demand was coalesced into an existing content-sync job.",details:{ruleId:l.id}});for(let l of g){if(l.check.status==="baseline-required"){let y=_(l.rule,r.publisherConfig);await Ne(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 R(e.runtimeDir,{eventType:l.check.status==="baseline-required"?"content-sync-baseline-required":"content-sync-demand-check-failed",status:l.check.status==="unavailable"?"failed":"info",actorSource:"queue-runner-scheduler",command:"content-sync",message:l.check.reason||"The content-sync consumer is already at the current journal head.",details:{ruleId:l.rule.id,headSequence:l.check.headSequence??0,committedSequence:l.check.committedSequence??0}});}return s}async function $t(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 Nt(e){try{await P.unlink(e);}catch{}}function Wt(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 x(e,t,n,r,i,a,o){if(o){let u=await Fe(t,o);if(u)throw new $(u,n)}let s=Wt(e,n),c={...process.env,...a??{}};return c.STATIC_PUBLISHER_RUNTIME_DIR=t,i&&(c.PUBLISHER_CONFIG=i),await new Promise((u,g)=>{let d=null,p=null,h=null,S=null,l=false,y="",b=spawn(process.execPath,[s,...r],{cwd:e,env:c,stdio:["inherit","inherit","pipe"]});b.stderr?.on("data",A=>{y+=A.toString("utf8");});let k=()=>{p&&clearInterval(p),h&&clearInterval(h),S&&clearTimeout(S);},f=async()=>{if(!(!o||d||l)){l=true;try{let A=await Fe(t,o);if(!A)return;d=A,b.kill("SIGTERM"),S=setTimeout(()=>{b.kill("SIGKILL");},1e4);}finally{l=false;}}};o&&(p=setInterval(()=>{f();},1e3),h=setInterval(()=>{(async()=>{let A=Me(t),q=await w(A,{});await v(A,{...q,checkedAt:new Date().toISOString(),status:"running",currentJobId:o||q.currentJobId||"",currentStep:n});})().catch(()=>{});},15e3)),b.on("error",A=>{k(),g(A);}),b.on("close",(A,q)=>{k();let j=Se(y).trim();if(j&&process.stderr.write(`${j}
3
+ `),d){g(new $(d,n));return}if(A===0)u();else {let O=Re(j);g(new Error(`${n} exited with code ${A??-1}${q?` (signal ${q})`:""}${O?`: ${O}`:""}`));}});}),0}function Qt(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 Kt(e,t){let n=o=>{if(!o||typeof o!="object")return null;let s=o,c={...s.siteSettings??{},...String(s.accountId??"").trim()&&!s.siteSettings?.accountId?{accountId:String(s.accountId).trim()}:{},...String(s.siteId??"").trim()&&!s.siteSettings?.siteId?{siteId:String(s.siteId).trim()}:{},...s.siteSettings?.subscriber===true||s.subscriber===true?{subscriber:true}:{}},u={...String(s.apiBase??"").trim()?{apiBase:String(s.apiBase).trim()}:{},...String(s.runtimeToken??s.nonce??"").trim()?{runtimeToken:String(s.runtimeToken??s.nonce).trim()}:{},...String(s.virtualAssetBaseUrl??s.uploadUrl??"").trim()?{virtualAssetBaseUrl:String(s.virtualAssetBaseUrl??s.uploadUrl).trim()}:{},...Object.keys(c).length>0?{siteSettings:c}:{},...s.subscriptionType?{subscriptionType:s.subscriptionType}:{}};return Object.keys(u).length>0?u:null},r=await W(t.configPath).catch(()=>null),i=n(r?.wpsuite);if(i)return i;let a=n(e.wpsuite);return a||null}async function zt(e,t){return await Kt(e,t)}function pe(e){let t={...e,status:"queued"};return delete t.startedAt,delete t.endedAt,delete t.exitCode,delete t.error,delete t.stopRequestedAt,delete t.stopRequestedByUserId,delete t.stopRequestedByLogin,delete t.stopMode,delete t.stoppedStep,t}async function _t(e,t){let n=await w(ge(e.runtimeDir),null),r=n&&n.details&&typeof n.details.phase=="string"?n.details.phase.trim():"";if(r)return r;let i=n&&typeof n.currentStep=="string"?n.currentStep.trim():"";if(i)return i;let a=await Ue(e).catch(()=>"");if(a){let o=await w(m.join(a,"current-crawl-event.json"),null),s=o&&typeof o.currentStep=="string"?o.currentStep.trim():"";if(s)return s}return t}function Ht(e,t){if((e.command==="publish"||e.command==="crawl")&&t==="rewrite-text")return "rewrite-text"}async function Vt(e){let t=m.join(e.runtimeDir,"queue.json"),n=m.join(e.runtimeDir,"current-run.json"),r=await F(e.runtimeDir,async()=>{let i=await w(n,null);if(!i||i.status!=="running"&&i.status!=="queued")return null;let a=await w(t,[]),o=Array.isArray(a)?a.filter(c=>c?.id!==i.id):[],s=pe(i);return await v(t,[s,...o]),await v(n,null),s});r&&(await U(e.runtimeDir,r.id),await R(e.runtimeDir,{eventType:"job-recovered",status:"queued",actorSource:"queue-runner",jobId:r.id,command:r.command,message:"Recovered stale current-run entry back into queue."}));}async function M(e,t){if(e.resumeFromStep==="rewrite-text")return;let n=m.join(t.runtimeDir,"privacy","cookie-observations.json"),r;try{let s=await P.stat(n);if(!s.isFile()||s.size>5*1024*1024)throw new Error("Cookie observation artifact is missing or exceeds 5 MiB.");r=JSON.parse(await P.readFile(n,"utf8"));}catch(s){if((s&&typeof s=="object"&&"code"in s?String(s.code||""):"")==="ENOENT")return;throw s}let i=await W(t.configPath),a=String(i.wpsuite?.runtimeToken||"").trim();if(!a)return;await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"consent-cookie-observations"}),await new L({sourceOrigin:i.sourceOrigin,runtimeToken:a,ignoreHttpsErrors:!!i.ignoreHttpsErrors}).importCookieObservations(r);}async function Gt(e,t,n){try{if((n.shouldEnforceSubscriptionOnJobExecution?.(e)??n.enforceSubscriptionOnJobExecution)&&n.assertActiveSubscriptionForIdentity){let s=await zt(e,t);await n.assertActiveSubscriptionForIdentity(s);}let i=Qt(e),a=e.resumeFromStep==="rewrite-text"?["--resume-rewrite"]:e.crawlMode==="incremental"?["--crawl-mode","incremental"]:[],o=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 x(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 x(t.exporterDir,t.runtimeDir,"crawl",a,t.configPath,i,e.id),await M(e,t),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await x(t.exporterDir,t.runtimeDir,"deploy",o,t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await x(t.exporterDir,t.runtimeDir,"invalidate",o,t.configPath,i,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-baseline"}),await x(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 x(t.exporterDir,t.runtimeDir,"crawl",a,t.configPath,i,e.id),await M(e,t),{exitCode:0};if(e.command==="deploy")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await x(t.exporterDir,t.runtimeDir,"deploy",o,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 x(t.exporterDir,t.runtimeDir,"invalidate",o,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 x(t.exporterDir,t.runtimeDir,"crawl",["--retry-timeouts"],t.configPath,i,e.id),await M(e,t),{exitCode:0};if(e.command==="url"){let s=(e.url||"").trim();return s?(await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"url"}),await x(t.exporterDir,t.runtimeDir,"crawl",["--url",s],t.configPath,i,e.id),await M(e,t),{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 x(t.exporterDir,t.runtimeDir,"content-sync",["--job-id",e.id],t.configPath,i,e.id),await M(e,t),{exitCode:0}):{exitCode:2,error:`Unsupported command: ${e.command}`}}catch(r){return r instanceof $?{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 We(e){return async function(n=process.argv.slice(2)){let r=It(n),i=m.join(r.runtimeDir,"export.lock"),a=m.join(r.runtimeDir,"last-run.json");await E(r.runtimeDir),await C(r,"starting",{message:"queue-runner starting"});try{await $t(i);}catch(o){if((o&&typeof o=="object"&&"code"in o?String(o.code||""):"")!=="EEXIST")throw o;console.log("[queue-runner] lock active, skipping"),await C(r,"lock-active",{message:"lock active, skipped this cron tick"});return}try{await Vt(r);let o=await Ut(r,e),s=0;for(let c=0;c<r.maxJobs;c++){let u=await en(r,e);if(u==="none"||(s+=1,u==="stopped"))break}if(s===0)await E(r.runtimeDir),await C(r,"idle",{processedJobs:s,schedulerEnqueued:o,message:"no queued jobs"});else {await E(r.runtimeDir);let c=await w(a,null),u=String(c?.status||"").trim();await C(r,u==="success"?"job-success":u==="stopped"?"job-stopped":"job-failed",{processedJobs:s,schedulerEnqueued:o,lastJobId:c?.id,lastJobCommand:c?.command,lastJobStatus:c?.status,lastJobExitCode:c?.exitCode,lastJobError:c?.error,...u==="stopped"?{currentStep:c?.stoppedStep,stopRequestedAt:c?.stopRequestedAt,stopRequestedByLogin:c?.stopRequestedByLogin,stopRequestedMode:c?.stopMode,lastStoppedStep:c?.stoppedStep,message:c?.stopMode==="requeue"?`Job stopped during ${c?.stoppedStep||c?.command||"active step"} and requeued.`:`Job stopped during ${c?.stoppedStep||c?.command||"active step"} and left out of queue.`}:{}});}}catch(o){let s=o instanceof Error?o.message:String(o);throw await C(r,"error",{message:s}),await R(r.runtimeDir,{eventType:"queue-runner-error",status:"failed",actorSource:"queue-runner",message:"Unhandled queue-runner error.",details:{error:s}}),o}finally{await Nt(i);}}}async function Oe(e,t){if(t.command!=="content-sync"||!t.coalesceKey)return false;let n=oe(e.runtimeDir),r=await Te(n,t.coalesceKey),i=await ke(n),a=i.rules[t.coalesceKey];return a&&(i.rules[t.coalesceKey]={...a,trailingWorkDetected:true,retryAttempt:0,nextRetryAt:null,lastError:null},await De(n,i)),r}async function Yt(e,t,n){let r=m.join(e.runtimeDir,"queue.json"),i=m.join(e.runtimeDir,"current-run.json");await F(e.runtimeDir,async()=>{let a=await w(r,[]),o=Array.isArray(a)?a.filter(u=>u?.id!==t.id):[],s=pe(t),c=Ht(t,n);c?s.resumeFromStep=c:delete s.resumeFromStep,await v(r,[s,...o]),await v(i,null);});}async function Zt(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()),o=new Date(Date.now()+i+a).toISOString(),s={...pe(t),status:"retry-wait",attempt:r,nextAttemptAt:o,error:n};if(await F(e.runtimeDir,async()=>{let c=m.join(e.runtimeDir,"queue.json"),u=m.join(e.runtimeDir,"current-run.json"),d=(await w(c,[])).filter(p=>p.id!==t.id);await v(c,[s,...d]),await v(u,null);}),t.coalesceKey){let c=m.join(e.runtimeDir,"content-sync-state.json"),g=(await w(c,{})).rules??{};g[t.coalesceKey]={...g[t.coalesceKey]??{},ruleId:t.ruleId||"",coalesceKey:t.coalesceKey,retryAttempt:r,nextRetryAt:o,lastError:n,trailingWorkDetected:true},await v(c,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:g});}return s}async function Xt(e,t){if(!t.coalesceKey||!t.ruleId||!(await w(m.join(e.runtimeDir,"content-sync-state.json"),null))?.rules?.[t.coalesceKey]?.trailingWorkDetected)return null;let r=null;await F(e.runtimeDir,async()=>{let a=m.join(e.runtimeDir,"queue.json"),o=await w(a,[]);de(o,{command:"content-sync",coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile})||(r={id:randomUUID(),command:"content-sync",ruleId:t.ruleId,coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile,enqueueSource:"scheduler",wpsuite:t.wpsuite,status:"queued",attempt:0,createdAt:new Date().toISOString(),createdBy:0},await v(a,[...o,r]));});let i=r;return i&&await R(e.runtimeDir,{eventType:"content-sync-trailing-job-created",status:"queued",actorSource:"queue-runner",jobId:i.id,command:i.command,message:"Content changes after the stable cutoff created one trailing job.",details:{predecessorJobId:t.id,ruleId:t.ruleId,coalesceKey:t.coalesceKey}}),i}async function en(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(),o=await F(e.runtimeDir,async()=>{let d=await w(n,[]);if(!Array.isArray(d)||d.length===0)return null;let p=Date.parse(String(d[0]?.nextAttemptAt||""));if(Number.isFinite(p)&&p>Date.now())return null;let h={...d[0],status:"running",startedAt:a};return delete h.nextAttemptAt,await v(n,d.slice(1)),await v(r,h),h});if(!o)return "none";await E(e.runtimeDir),await R(e.runtimeDir,{eventType:"job-run-started",status:"running",actorSource:"queue-runner",jobId:o.id,command:o.command,message:"Job execution started.",details:{createdAt:o.createdAt||"",startedAt:a,createdBy:o.createdBy??null,deploymentProfile:o.deploymentProfile||"",queuedWithTempAwsCreds:!!o.awsTempCreds}}),await C(e,"running",{currentJobId:o.id,currentJobCommand:o.command,currentStep:o.command});let s=await Gt(o,e,t),c=new Date().toISOString();if(s.stopped){let d=await _t(e,s.stoppedStep||o.command),p=s.stopRequest?.mode==="requeue"?"requeue":"stop",h={...o,status:"stopped",endedAt:c,exitCode:s.exitCode,...s.error?{error:s.error}:{},stopRequestedAt:s.stopRequest?.requestedAt||"",stopRequestedByUserId:typeof s.stopRequest?.requestedByUserId=="number"?s.stopRequest.requestedByUserId:null,stopRequestedByLogin:s.stopRequest?.requestedByLogin||"",stopMode:p,stoppedStep:d},S=false;p==="requeue"?await Yt(e,o,d):(await v(r,null),S=await Oe(e,o));try{let l=await K(h,e);h.logArchiveDir=l.archiveDir,h.logArchiveCreatedAt=l.archiveCreatedAt,h.logArchiveFileCount=l.archivedFiles.length;}catch(l){h.logArchiveError=l instanceof Error?l.message:String(l);}return await v(i,h),await U(e.runtimeDir,o.id),await E(e.runtimeDir),await R(e.runtimeDir,{eventType:"job-run-stopped",status:"stopped",actorSource:"queue-runner",jobId:o.id,command:o.command,message:p==="requeue"?"Job stop requested and requeued.":"Job stop requested and removed from active execution without requeue.",details:{startedAt:o.startedAt||"",endedAt:c,deploymentProfile:o.deploymentProfile||"",stopMode:p,stopRequestedAt:s.stopRequest?.requestedAt||"",stopRequestedByLogin:s.stopRequest?.requestedByLogin||"",stopRequestedByUserId:typeof s.stopRequest?.requestedByUserId=="number"?s.stopRequest.requestedByUserId:null,stoppedStep:d,logArchiveKey:h.logArchiveError||!h.logArchiveDir?"":m.basename(h.logArchiveDir),logArchiveDir:h.logArchiveDir||"",logArchiveCreatedAt:h.logArchiveCreatedAt||"",logArchiveFileCount:h.logArchiveFileCount??0,logArchiveError:h.logArchiveError||"",contentSyncAbandoned:S,journalCursorPreserved:S}}),"stopped"}if(o.command==="content-sync"&&s.exitCode!==0){if(/baseline (?:is missing or stale|required)|verified content-sync baseline|new baseline/i.test(s.error||"")){let S="The installed release no longer matches the verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline.";o.ruleId&&o.coalesceKey&&await Ne(e,o.ruleId,o.coalesceKey,S);let l={...o,status:"failed",endedAt:c,exitCode:s.exitCode,error:S};await v(r,null),await E(e.runtimeDir),await Oe(e,o);try{let y=await K(l,e);l.logArchiveDir=y.archiveDir,l.logArchiveCreatedAt=y.archiveCreatedAt,l.logArchiveFileCount=y.archivedFiles.length;}catch(y){l.logArchiveError=y instanceof Error?y.message:String(y);}return await v(i,l),await U(e.runtimeDir,o.id),await R(e.runtimeDir,{eventType:"content-sync-baseline-required",status:"failed",actorSource:"queue-runner",jobId:o.id,command:o.command,message:S,details:{ruleId:o.ruleId||"",retryScheduled:false}}),"processed"}await R(e.runtimeDir,{eventType:"content-sync-failed",status:"failed",actorSource:"queue-runner",jobId:o.id,command:o.command,message:"Content-sync execution failed before cursor acknowledgement.",details:{attempt:o.attempt??0,error:s.error||"Content-sync execution failed."}});let p=await Zt(e,o,s.error||"Content-sync execution failed."),h={...o,status:"retry-wait",endedAt:c,exitCode:s.exitCode,error:p.error,attempt:p.attempt,nextAttemptAt:p.nextAttemptAt};try{let S=await K(h,e);h.logArchiveDir=S.archiveDir,h.logArchiveCreatedAt=S.archiveCreatedAt,h.logArchiveFileCount=S.archivedFiles.length;}catch(S){h.logArchiveError=S instanceof Error?S.message:String(S);}return await v(i,h),await U(e.runtimeDir,o.id),await R(e.runtimeDir,{eventType:"content-sync-retry-scheduled",status:"retry-wait",actorSource:"queue-runner",jobId:o.id,command:o.command,message:"Content-sync retry was scheduled with bounded backoff.",details:{attempt:p.attempt??0,nextAttemptAt:p.nextAttemptAt||"",error:p.error||""}}),"processed"}let u={...o,status:s.exitCode===0?"success":"failed",endedAt:c,exitCode:s.exitCode,...s.error?{error:s.error}:{}};await v(r,null),u.status==="success"&&u.command==="content-sync"&&await Xt(e,u),await U(e.runtimeDir,o.id);try{let d=await K(u,e);u.logArchiveDir=d.archiveDir,u.logArchiveCreatedAt=d.archiveCreatedAt,u.logArchiveFileCount=d.archivedFiles.length;}catch(d){u.logArchiveError=d instanceof Error?d.message:String(d);}await v(i,u);let g=o.startedAt&&u.endedAt?Math.max(0,Math.round((new Date(u.endedAt).getTime()-new Date(o.startedAt).getTime())/1e3)):void 0;return await R(e.runtimeDir,{eventType:"job-run-finished",status:u.status==="success"?"success":"failed",actorSource:"queue-runner",jobId:u.id,command:u.command,message:u.status==="success"?"Job execution finished successfully.":"Job execution finished with failure.",details:{startedAt:o.startedAt||"",endedAt:u.endedAt||"",durationSec:g,deploymentProfile:u.deploymentProfile||"",exitCode:u.exitCode??null,error:u.error||"",logArchiveKey:u.logArchiveError||!u.logArchiveDir?"":m.basename(u.logArchiveDir),logArchiveDir:u.logArchiveDir||"",logArchiveCreatedAt:u.logArchiveCreatedAt||"",logArchiveFileCount:u.logArchiveFileCount??0,logArchiveError:u.logArchiveError||""}}),"processed"}async function nn(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 rn={allowSchedulerAutoEnqueue:true,enforceSubscriptionOnJobExecution:false,shouldEnforceSubscriptionOnJobExecution:e=>Le(e)||e.enqueueSource==="scheduler"||e.createdBy===0,assertActiveSubscriptionForIdentity:nn},Qe=We(rn),on=Qe,Vn=Qe;process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href&&on().catch(e=>{console.error(e),process.exit(1);});export{Vn as default,on as main,Qe as runQueueRunner};
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import m from'path';import {readFile}from'fs/promises';import {LambdaClient,InvokeCommand}from'@aws-sdk/client-lambda';import {S3Client,GetObjectCommand,PutObjectCommand}from'@aws-sdk/client-s3';import {fromTemporaryCredentials}from'@aws-sdk/credential-providers';import {createHash}from'crypto';var c=1;function R(o){let e=String(o??"").trim().replace(/^\/+|\/+$/g,"");if(!e)return "";let r=e.split("/").filter(Boolean);if(r.some(t=>t==="."||t===".."))throw new Error("S3 prefix contains an unsafe dot path segment.");return `${r.join("/")}/`}function p(o){return createHash("sha256").update(o).digest("hex")}function l(o,e){let r=String(o??"").replace(/^\/+/,""),t=R(e);if(!r||r.includes("\0"))throw new Error("S3 object key is empty or invalid.");if(t&&!r.startsWith(t))throw new Error(`S3 object key is outside the configured workspace prefix: ${r}`);if(r.split("/").some(n=>n===".."))throw new Error("S3 object key contains an unsafe parent segment.");return r}function a(o,e,{optional:r=false}={}){if(!(r&&(o==null||o===""))){if(typeof o!="string"||!o.trim())throw new Error(`${e} must be a non-empty string.`);return o.trim()}}function $(o){if(!o||typeof o!="object")throw new Error("Remote worker config must be a JSON object.");let e=o;if(e.schemaVersion!==1)throw new Error(`Unsupported remote worker config version: ${String(e.schemaVersion)}.`);if(e.protocolVersion!==c)throw new Error(`Remote worker protocol mismatch: expected ${c}, received ${String(e.protocolVersion)}.`);let r=e.workspace,t=e.functions;if(!r||!t)throw new Error("Remote worker config requires workspace and functions objects.");return {schemaVersion:1,region:a(e.region,"region"),...a(e.roleArn,"roleArn",{optional:true})?{roleArn:a(e.roleArn,"roleArn")}:{},...a(e.roleSessionName,"roleSessionName",{optional:true})?{roleSessionName:a(e.roleSessionName,"roleSessionName")}:{},workspace:{bucket:a(r.bucket,"workspace.bucket"),prefix:String(r.prefix??"")},functions:{render:a(t.render,"functions.render"),rewrite:a(t.rewrite,"functions.rewrite"),"deploy-copy":a(t["deploy-copy"],"functions.deploy-copy")},targets:Array.isArray(e.targets)?e.targets.map((n,s)=>{if(!n||typeof n!="object")throw new Error(`targets[${s}] must be an object.`);let i=n;return {id:a(i.id,`targets[${s}].id`),bucketName:a(i.bucketName,`targets[${s}].bucketName`),prefix:String(i.prefix??""),region:a(i.region,`targets[${s}].region`)}}):[],protocolVersion:c}}async function h(o){let e=m.resolve(o);try{return $(JSON.parse(await readFile(e,"utf8")))}catch(r){throw new Error(`Unable to load remote worker config ${e}: ${r instanceof Error?r.message:String(r)}`,{cause:r})}}var u=class{config;lambda;s3;constructor(e,r={}){this.config=e;let t=e.roleArn?fromTemporaryCredentials({params:{RoleArn:e.roleArn,RoleSessionName:e.roleSessionName??"publisher-exporter-remote-worker"},clientConfig:{region:e.region}}):void 0;this.lambda=new LambdaClient({region:e.region,...t?{credentials:t}:{},...r}),this.s3=new S3Client({region:e.region,...t?{credentials:t}:{}});}async invoke(e){let r=e.operation==="health"?this.config.functions.render:this.config.functions[e.operation],t=await this.lambda.send(new InvokeCommand({FunctionName:r,InvocationType:"RequestResponse",Payload:Buffer.from(JSON.stringify(e),"utf8")})),n=t.Payload?JSON.parse(new TextDecoder().decode(t.Payload)):null;if(t.FunctionError)throw new Error(`Remote ${e.operation} worker failed (${t.FunctionError}): ${JSON.stringify(n)}`);if(!n||typeof n!="object")throw new Error(`Remote ${e.operation} worker returned no JSON response.`);let s=n;if(s.schemaVersion!==c||s.operation!==e.operation||s.jobId!==e.jobId||s.taskId!==e.taskId)throw new Error(`Remote ${e.operation} worker returned a mismatched response.`);return s}async readResult(e){if(!e.resultKey)throw new Error(`Remote ${e.operation} response does not contain a resultKey.`);let r=l(e.resultKey,this.config.workspace.prefix),t=await this.s3.send(new GetObjectCommand({Bucket:this.config.workspace.bucket,Key:r}));if(!t.Body)throw new Error(`Remote worker result has no body: ${r}`);return JSON.parse(await t.Body.transformToString("utf8"))}async readObject(e){if(e.bucket!==this.config.workspace.bucket)throw new Error(`Remote worker object bucket mismatch: ${e.bucket}.`);let r=l(e.key,this.config.workspace.prefix),t=await this.s3.send(new GetObjectCommand({Bucket:this.config.workspace.bucket,Key:r,...e.versionId?{VersionId:e.versionId}:{}}));if(!t.Body)throw new Error(`Remote worker object has no body: ${r}`);let n=await t.Body.transformToByteArray();if(n.byteLength!==e.bytes)throw new Error(`Remote worker object length mismatch for ${r}: expected ${e.bytes}, received ${n.byteLength}.`);if(p(n)!==e.sha256)throw new Error(`Remote worker object checksum mismatch for ${r}.`);return n}async readObjectText(e){return new TextDecoder("utf8",{fatal:true}).decode(await this.readObject(e))}async health(e){let r=this.config.functions[e],t=`${e}-${Date.now()}`,n=await this.lambda.send(new InvokeCommand({FunctionName:r,InvocationType:"RequestResponse",Payload:Buffer.from(JSON.stringify({schemaVersion:c,operation:"health",jobId:"health",taskId:t}),"utf8")})),s=n.Payload?JSON.parse(new TextDecoder().decode(n.Payload)):null;if(n.FunctionError||!s||typeof s!="object")throw new Error(`Remote ${e} health check failed: ${JSON.stringify(s)}`);return s}async writeWorkspaceObject(e){let r=l(e.key,this.config.workspace.prefix),t=typeof e.body=="string"?Buffer.from(e.body,"utf8"):e.body,n=p(t),s=await this.s3.send(new PutObjectCommand({Bucket:this.config.workspace.bucket,Key:r,Body:t,ContentType:e.contentType,...e.cacheControl?{CacheControl:e.cacheControl}:{},Metadata:{sha256:n,...e.metadata??{}}}));return {bucket:this.config.workspace.bucket,key:r,sha256:n,bytes:t.byteLength,contentType:e.contentType,...s.ETag?{etag:s.ETag}:{},...s.VersionId?{versionId:s.VersionId}:{}}}};var d="remote-workers.json";function w(o={}){let e=String(o.explicitPath??"").trim();if(e)return m.resolve(e);let r=String(o.runtimeDir??process.env.STATIC_PUBLISHER_RUNTIME_DIR??process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR??"").trim();return r?m.join(m.resolve(r),d):""}function v(o){let e=o.findIndex(i=>i==="--config"||i==="-c"),r=o.find(i=>i.startsWith("--config=")),t=o.findIndex(i=>i==="--runtime-dir"),n=o.find(i=>i.startsWith("--runtime-dir=")),s=w({explicitPath:r?.slice(9)??(e>=0?o[e+1]:void 0),runtimeDir:n?.slice(14)??(t>=0?o[t+1]:void 0)});if(!s)throw new Error(`Pass --runtime-dir <path>; ${d} must be installed there. --config remains available for diagnostic overrides.`);return m.resolve(s)}async function P(){let o=await h(v(process.argv.slice(2))),e=new u(o),r=await Promise.all(["render","rewrite","deploy-copy"].map(async t=>({operation:t,response:await e.health(t)})));console.log(JSON.stringify({ok:true,results:r},null,2));}P().catch(o=>{console.error(o instanceof Error?o.message:String(o)),process.exit(1);});
@@ -0,0 +1,5 @@
1
+ import {S3Client,HeadObjectCommand,CopyObjectCommand,DeleteObjectsCommand,PutObjectCommand,GetObjectCommand}from'@aws-sdk/client-s3';import {chromium}from'playwright';import {createHash}from'crypto';import {lookup}from'dns/promises';import {isIP}from'net';import'fast-glob';import'fs/promises';import'os';import T from'path';import'worker_threads';var b=1,ne=/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;function v(e,t){let r=String(e??"").trim();if(!ne.test(r))throw new Error(`${t} must match ${ne.source} and be at most 128 characters.`);return r}function C(e){let t=String(e??"").trim().replace(/^\/+|\/+$/g,"");if(!t)return "";let r=t.split("/").filter(Boolean);if(r.some(n=>n==="."||n===".."))throw new Error("S3 prefix contains an unsafe dot path segment.");return `${r.join("/")}/`}function E(e,...t){let r=C(e),n=t.map((o,s)=>{let i=String(o??"").replace(/^\/+|\/+$/g,"");if(!i||i.split("/").some(c=>c==="."||c===".."))throw new Error(`Unsafe S3 key segment at index ${s}.`);return i});return `${r}${n.join("/")}`}function z(e,t,r){return `${E(e,"jobs",v(t,"jobId"),"tasks",v(r,"taskId"))}/`}function M(e){return createHash("sha256").update(e).digest("hex")}function k(e,t){let r=String(e??"").replace(/^\/+/,""),n=C(t);if(!r||r.includes("\0"))throw new Error("S3 object key is empty or invalid.");if(n&&!r.startsWith(n))throw new Error(`S3 object key is outside the configured workspace prefix: ${r}`);if(r.split("/").some(o=>o===".."))throw new Error("S3 object key contains an unsafe parent segment.");return r}function O(e,t){if(!e||typeof e!="object"||Array.isArray(e))throw new Error(`${t} must be a JSON object.`);return e}function m(e,t){if(typeof e!="string"||!e.trim())throw new Error(`${t} must be a non-empty string.`);return e.trim()}function R(e,t,r,n){if(!Number.isInteger(e)||Number(e)<r||Number(e)>n)throw new Error(`${t} must be an integer between ${r} and ${n}.`);return Number(e)}function xe(e){return e.hostname.replace(/^\[|\]$/g,"").replace(/\.$/,"").toLowerCase()}function A(e){let t=e.split(".");if(t.length!==4)return null;let r=t.map(n=>Number(n));return r.every((n,o)=>Number.isInteger(n)&&n>=0&&n<=255&&String(n)===t[o])?r:null}function J(e){let t=e.toLowerCase();if(t.includes("%")||t.split("::").length>2)return null;let r=t,n=r.lastIndexOf(":");if(r.includes(".")&&n>=0){let l=A(r.slice(n+1));if(!l)return null;r=`${r.slice(0,n)}:${((l[0]??0)*256+(l[1]??0)).toString(16)}:${((l[2]??0)*256+(l[3]??0)).toString(16)}`;}let[o,s]=r.split("::"),i=o?o.split(":"):[],c=s?s.split(":"):[],u=r.includes("::"),a=8-i.length-c.length;return u&&a<1||!u&&a!==0||[...i,...c].some(l=>!/^[0-9a-f]{1,4}$/.test(l))?null:[...i.map(l=>Number.parseInt(l,16)),...Array.from({length:a},()=>0),...c.map(l=>Number.parseInt(l,16))]}function oe(e){let t=A(e);if(!t)return "an invalid IPv4 address";let[r=0,n=0,o=0,s=0]=t;return r===0?"the unspecified/current-network IPv4 range":r===10||r===172&&n>=16&&n<=31||r===192&&n===168?"an RFC1918 private IPv4 range":r===127?"the IPv4 loopback range":r===169&&n===254?"the IPv4 link-local/metadata range":r===100&&n===100&&o===100&&s===200?"a cloud instance metadata endpoint":r===100&&n>=64&&n<=127?"the shared carrier-grade NAT IPv4 range":r===198&&(n===18||n===19)?"the IPv4 benchmarking range":r>=224&&r<=239?"the IPv4 multicast range":r>=240?"the reserved IPv4 range":null}function Se(e){let t=J(e);if(!t||t.length!==8)return "an invalid IPv6 address";if(t.every(o=>o===0))return "the unspecified IPv6 address";if(t.slice(0,7).every(o=>o===0)&&t[7]===1)return "the IPv6 loopback address";let r=t[0]??0;if((r&65024)===64512)return "an IPv6 unique-local range";if((r&65472)===65152)return "the IPv6 link-local range";if((r&65280)===65280)return "the IPv6 multicast range";if(t.slice(0,5).every(o=>o===0)&&t[5]===65535){let o=t[6]??0,s=t[7]??0;return oe(`${o>>8}.${o&255}.${s>>8}.${s&255}`)}return null}function G(e){return A(e)?oe(e):J(e)?Se(e):"an invalid IP address"}function V(e,t){let r=m(e,t),n;try{n=new URL(r);}catch{throw new Error(`${t} must be a valid URL.`)}if(n.protocol!=="http:"&&n.protocol!=="https:")throw new Error(`${t} must use HTTP or HTTPS.`);if(n.username||n.password)throw new Error(`${t} must not contain URL credentials.`);let o=xe(n);if(!o)throw new Error(`${t} must contain a hostname.`);if(o==="localhost"||o.endsWith(".localhost"))throw new Error(`${t} must not target localhost.`);if(o==="metadata.google.internal")throw new Error(`${t} must not target a cloud instance metadata endpoint.`);if(A(o)||J(o)){let s=G(o);if(s)throw new Error(`${t} must not target ${s}.`)}return n}function Pe(e){let t=V(e.sourceOrigin,"sourceOrigin").origin;if(!Array.isArray(e.urls)||e.urls.length<1||e.urls.length>20)throw new Error("Render tasks require between 1 and 20 URLs.");for(let[s,i]of e.urls.entries())if(V(i,`urls[${s}]`).origin!==t)throw new Error(`urls[${s}] is outside sourceOrigin.`);let r=O(e.options,"options");if(typeof r.ignoreHttpsErrors!="boolean")throw new Error("options.ignoreHttpsErrors must be a boolean.");if(typeof r.javaScriptEnabled!="boolean")throw new Error("options.javaScriptEnabled must be a boolean.");let n=O(r.viewport,"options.viewport");R(n.width,"options.viewport.width",320,7680),R(n.height,"options.viewport.height",240,7680),R(r.navigationTimeoutMs,"options.navigationTimeoutMs",1e3,6e5),R(r.autoScrollTimeoutMs,"options.autoScrollTimeoutMs",0,12e4);let o=O(r.readiness,"options.readiness");for(let s of ["waitForSelector","waitForFunction"])if(o[s]!==null&&typeof o[s]!="string")throw new Error(`options.readiness.${s} must be a string or null.`);R(o.timeoutMs,"options.readiness.timeoutMs",0,12e4),R(o.fallbackWaitMs,"options.readiness.fallbackWaitMs",0,12e4);}function $e(e){if(m(e.configKey,"configKey"),m(e.assetMapKey,"assetMapKey"),e.previousAssetMapKey!==void 0&&m(e.previousAssetMapKey,"previousAssetMapKey"),!Array.isArray(e.items)||e.items.length<1||e.items.length>500)throw new Error("Rewrite tasks require between 1 and 500 objects.");for(let[t,r]of e.items.entries()){let n=O(r,`items[${t}]`);m(n.inputKey,`items[${t}].inputKey`),m(n.outputKey,`items[${t}].outputKey`),m(n.relativePath,`items[${t}].relativePath`);}}function Ie(e){if(v(m(e.targetId,"targetId"),"targetId"),!Array.isArray(e.items)||e.items.length>1e3)throw new Error("Deploy copy tasks accept at most 1000 objects.");for(let[r,n]of e.items.entries()){let o=O(n,`items[${r}]`);m(o.sourceKey,`items[${r}].sourceKey`),m(o.targetRelativeKey,`items[${r}].targetRelativeKey`),m(o.sha256,`items[${r}].sha256`),o.normalizedSha256!==void 0&&m(o.normalizedSha256,`items[${r}].normalizedSha256`),R(o.bytes,`items[${r}].bytes`,0,Number.MAX_SAFE_INTEGER),m(o.contentType,`items[${r}].contentType`),m(o.cacheControl,`items[${r}].cacheControl`);}let t=e.deleteRelativeKeys??[];if(!Array.isArray(t)||t.length>1e3)throw new Error("Deploy copy tasks accept at most 1000 deletion keys.");for(let[r,n]of t.entries())m(n,`deleteRelativeKeys[${r}]`);if(e.items.length===0&&t.length===0)throw new Error("Deploy copy tasks require copy or deletion work.")}function se(e){if(!e||typeof e!="object")throw new Error("Worker event must be a JSON object.");let t=e;if(t.schemaVersion!==b)throw new Error(`Unsupported worker protocol version: ${String(t.schemaVersion)}.`);let r=t.operation;if(r!=="render"&&r!=="rewrite"&&r!=="deploy-copy"&&r!=="health")throw new Error(`Unsupported worker operation: ${String(r)}.`);return v(String(t.jobId??""),"jobId"),v(String(t.taskId??""),"taskId"),r==="render"&&Pe(t),r==="rewrite"&&$e(t),r==="deploy-copy"&&Ie(t),e}function Ce(e){return e.hostname.replace(/^\[|\]$/g,"").replace(/\.$/,"").toLowerCase()}async function Te(e){return (await lookup(e,{all:true,verbatim:true})).map(t=>t.address)}async function We(e,t,r=Te){let n=V(e,t),o=Ce(n);if(isIP(o))return n;let s;try{s=await r(o);}catch{throw new Error(`${t} hostname could not be resolved safely.`)}if(s.length===0)throw new Error(`${t} hostname resolved to no addresses.`);for(let i of s){let c=G(i);if(c)throw new Error(`${t} hostname resolves to ${c}.`)}return n}async function ie(e,t=We){let r=null;return await e.route("**/*",async n=>{try{await t(n.request().url(),"render request");}catch(o){r??=o instanceof Error?o:new Error(String(o)),await n.abort("blockedbyclient").catch(()=>{});return}await n.continue();}),await e.routeWebSocket("**/*",async n=>{try{await t(n.url(),"render WebSocket request");}catch(o){r??=o instanceof Error?o:new Error(String(o)),await n.close({code:1008,reason:"Blocked by network policy"});return}await n.close({code:1008,reason:"Only HTTP(S) requests are allowed"}),r??=new Error("render WebSocket request must use HTTP or HTTPS.");}),{getViolation:()=>r}}function le(e){return e.replace(/&quot;/g,'"').replace(/&#34;/g,'"').replace(/&apos;/g,"'").replace(/&#39;/g,"'").replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">")}function x(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function U(e){return e.replace(/\//g,"\\/")}function ae(e){return e.replace(/\//g,"\\\\/")}function je(e){try{let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")return null;let r=t.pathname==="/"&&!t.search&&!t.hash?"":`${t.pathname}${t.search}${t.hash}`;return `//${t.host}${r}`}catch{return null}}function ce(e,t,r){if(!t)return;e[t]=r;let n=U(t),o=U(r);e[n]=o;let s=ae(t),i=ae(r);e[s]=i;let c=x(t),u=x(r);e[c]=u;let a=x(n),l=x(o);e[a]=l;let p=x(s),d=x(i);e[p]=d;}function L(e,t,r){ce(e,t,r);let n=je(t);n&&n!==t&&ce(e,n,r);}function ue(e,t){try{return new URL(e,t==="."?"https://relative.invalid":t).pathname}catch{return e.startsWith("/")?e:`/${e.replace(/^\.\//,"")}`}}function pe(e,t,r){if(!t)return r;let n=T.dirname(T.resolve(t)),o=T.resolve(e,r.replace(/^\/+/,"")),s=T.relative(n,o).replace(/\\/g,"/");return s?(s.startsWith(".")||(s=`./${s}`),s):"."}var Ve=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],Me=new Set([".html",".htm"]),de="WPSuite.io Static Publisher",Ae=de.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function Ke(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function Ue(e){return e.startsWith("//")||e.startsWith("\\/\\/")||e.startsWith("\\\\/\\\\/")}function Le(e,t,r){return Ue(t)?e.replace(new RegExp(`(?<!:)${Ke(t)}`,"g"),r):e.split(t).join(r)}function _e(e,t,r){if(e.wpsuite?.siteSettings?.subscriber===true||e.wpsuite?.subscriptionType==="PROFESSIONAL"||e.wpsuite?.subscriptionType==="AGENCY"||!r)return false;let o=T.extname(r).toLowerCase();return Me.has(o)?/<head\b|<html\b|<!doctype html/i.test(t):false}function Fe(e){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${Ae}\\2)[^>]*\\/?>`,"i").test(e))return e;let t=`<meta name="generator" content="${de}" />`;if(e.match(/(\r?\n)([ \t]*)<\/head>/i))return e.replace(/(\r?\n)([ \t]*)<\/head>/i,`$1$2${t}$1$2</head>`);let n=e.includes(`\r
2
+ `)?`\r
3
+ `:`
4
+ `;return e.replace(/<head\b[^>]*>/i,o=>`${o}${n} ${t}`)}function Ne(e){let t=le(e).trim();return t.startsWith("{")||t.startsWith("[")||t.includes("\\/")||/"@context"|"@type"/.test(t)}function He(e,t){let r=[...new Set(t)].filter(s=>s.includes("/")&&!/\\+\//.test(s)).map(s=>[s,U(s)]).filter(([s,i])=>s!==i).sort((s,i)=>i[0].length-s[0].length);if(!r.length)return e;let n=s=>{let i=s;for(let[c,u]of r)i=i.split(c).join(u);return i},o=e.replace(/(<script\b[^>]*\btype=["']application\/(?:ld\+)?json["'][^>]*>)([\s\S]*?)(<\/script>)/gi,(s,i,c,u)=>`${i}${n(c)}${u}`);return o=o.replace(/(<meta\b[^>]*\bcontent=(['"]))([\s\S]*?)(\2[^>]*>)/gi,(s,i,c,u,a)=>Ne(u)?`${i}${n(u)}${a}`:s),o}function Be(e,t){if(!t)return null;let r=T.dirname(T.resolve(t)),n=T.resolve(e.outputDir),o=T.relative(r,n).replace(/\\/g,"/");return o?(o.startsWith(".")||(o=`./${o}`),o.endsWith("/")?o:`${o}/`):"./"}function De(e,t,r){let n=Be(t,r);if(!n)return e;let o=e;for(let s of Ve){let i=s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),c=new RegExp(`(?:\\.{1,}/)+${i}`,"g"),u=new RegExp(`(?<!\\.)/${i}`,"g");o=o.replace(c,`${n}${s}`).replace(u,`${n}${s}`);let a=s.replace(/\//g,"\\/"),l=n.replace(/\//g,"\\/"),p=a.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),d=new RegExp(`(?:\\.{1,}\\\\/)+${p}`,"g"),P=new RegExp(`(?:\\.{1,}(?:\\\\/|\\\\))+${p}`,"g"),$=new RegExp(`(?<![\\.\\\\])\\/${p}`,"g");o=o.replace(d,`${l}${a}`).replace(P,`${l}${a}`).replace($,`${l}${a}`);}return o}function Y(e,t,r){if(e.urlRewriteMode==="absolute")return t;let n=(()=>{try{return new URL(t,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin)}catch{return null}})(),o=n?n.pathname:ue(t,e.targetOrigin),s=n?`${n.search}${n.hash}`:"";return e.urlRewriteMode==="root-relative"?`${o}${s}`:`${pe(e.outputDir,r,o)}${s}`}function X(e,t,r,n,o){L(e,r,Y(t,n,o));}function qe(e,t,r,n,o){if(!r||r===n)return;L(e,r,n);let s=Y(t,r,o),i=Y(t,n,o);L(e,s,i);}function ge(e,t,r,n,o={}){let s=e,i={};X(i,t,t.sourceOrigin,t.targetOrigin,n);for(let[u,a]of Object.entries(t.extraReplacements))X(i,t,u,a,n);for(let[u,a]of Object.entries(r))X(i,t,u,a,n);for(let[u,a]of Object.entries(o)){let l=r[u];!l||l===a||qe(i,t,a,l,n);}let c=Object.entries(i).sort((u,a)=>a[0].length-u[0].length);for(let[u,a]of c)s=Le(s,u,a);return t.urlRewriteMode!=="absolute"&&(s=De(s,t,n)),s=He(s,c.map(([,u])=>u)),_e(t,s,n)&&(s=Fe(s)),s}var S=String(process.env.PUBLISHER_WORKSPACE_BUCKET??"").trim(),w=C(process.env.PUBLISHER_WORKSPACE_PREFIX??""),_=String(process.env.PUBLISHER_ALLOWED_OPERATION??"").trim(),H=String(process.env.PUBLISHER_EXPORTER_VERSION??"unknown").trim(),Z=String(process.env.PUBLISHER_PROXY_URL??"").trim(),Qe=et(process.env.PUBLISHER_ALLOWED_TARGETS??"[]"),we=new S3Client({}),me=new Map,h=null;function et(e){try{let t=JSON.parse(e);return Array.isArray(t)?t.filter(r=>{if(!r||typeof r!="object")return !1;let n=r;return ["id","bucketName","prefix","region"].every(o=>typeof n[o]=="string")}).map(r=>({...r,prefix:C(r.prefix)})):[]}catch{return []}}function Q(){if(!S)throw new Error("PUBLISHER_WORKSPACE_BUCKET is required.")}async function tt(){if(h){let t=await h.catch(()=>null);if(t?.isConnected())return t}let e=chromium.launch({headless:true,args:["--no-sandbox","--disable-setuid-sandbox","--disable-dev-shm-usage","--no-zygote","--single-process","--disable-gpu"],...Z?{proxy:{server:Z}}:{}});h=e;try{let t=await e;return t.once("disconnected",()=>{h===e&&(h=null);}),t}catch(t){throw h===e&&(h=null),t}}async function rt(e){let t;for(let r=1;r<=2;r+=1){let n=await tt(),o=null;try{o=await n.newContext({viewport:e.options.viewport,ignoreHTTPSErrors:e.options.ignoreHttpsErrors,javaScriptEnabled:e.options.javaScriptEnabled,serviceWorkers:"block",userAgent:"WPSuiteStaticPublisher/remote-worker-v1"}),e.options.javaScriptEnabled&&await nt(o);let s=await ie(o);return {context:o,page:await o.newPage(),networkPolicyViolation:s.getViolation}}catch(s){t=s,await o?.close().catch(()=>{}),h=null,await n.close().catch(()=>{});}}throw t instanceof Error?t:new Error(`Unable to create a Chromium page: ${String(t)}`)}async function nt(e){await e.addInitScript(()=>{Object.defineProperty(window,"__WPSUITE_STATIC_EXPORT__",{value:true,writable:false,configurable:true});let t=r=>{if(r==null||String(r)==="")return true;try{let n=new URL(String(r),window.location.href),o=new URL(window.location.href);return n.hash="",o.hash="",n.href===o.href}catch{return false}};try{let r=window.location.assign.bind(window.location),n=window.location.replace.bind(window.location);Object.defineProperty(window.location,"assign",{configurable:!0,value:o=>{if(!t(o))return r(o)}}),Object.defineProperty(window.location,"replace",{configurable:!0,value:o=>{if(!t(o))return n(o)}}),Object.defineProperty(window.location,"reload",{configurable:!0,value:()=>{}});}catch{}});}async function ot(e,t){await e.evaluate(async r=>{await new Promise(n=>{let o=0,s=700,i=Date.now(),c=()=>{window.clearInterval(u),window.scrollTo(0,0),n();},u=window.setInterval(()=>{if(Date.now()-i>=r){c();return}window.scrollBy(0,s),o+=s;let a=Math.max(document.body?.scrollHeight||0,document.documentElement?.scrollHeight||0);o>=a+window.innerHeight&&c();},120);});},t);}async function ye(e){Q();let t=k(e,w),r=await we.send(new GetObjectCommand({Bucket:S,Key:t}));if(!r.Body)throw new Error(`S3 object has no body: ${t}`);return await r.Body.transformToString("utf8")}async function N(e){return JSON.parse(await ye(e))}async function ee(e,t,r){Q();let n=k(e,w),o=typeof t=="string"?Buffer.from(t,"utf8"):t,s=await we.send(new PutObjectCommand({Bucket:S,Key:n,Body:o,ContentType:r,Metadata:{sha256:M(o)}}));return {bucket:S,key:n,sha256:M(o),bytes:o.byteLength,contentType:r,...s.ETag?{etag:s.ETag}:{},...s.VersionId?{versionId:s.VersionId}:{}}}async function st(e,t){return await ee(e,`${JSON.stringify(t,null,2)}
5
+ `,"application/json; charset=utf-8")}async function it(e){try{let t=await N(e);return t.response??t}catch(t){if(t.name==="NoSuchKey"||t.$metadata?.httpStatusCode===404)return null;throw t}}function F(e){return [...new Set(e)].filter(t=>{try{let r=new URL(t);return (r.protocol==="http:"||r.protocol==="https:")&&!r.username&&!r.password}catch{return false}}).sort()}async function at(e,t,r){let n=null,o=null,s,i=new Set;try{let c=await rt(e);n=c.context,o=c.page,s=c.networkPolicyViolation,o.on("request",g=>i.add(g.url()));let u=await o.goto(t,{waitUntil:"domcontentloaded",timeout:e.options.navigationTimeoutMs});e.options.readiness.waitForSelector&&await o.waitForSelector(e.options.readiness.waitForSelector,{timeout:e.options.readiness.timeoutMs}).catch(()=>{}),e.options.javaScriptEnabled&&e.options.readiness.waitForFunction&&await o.waitForFunction(e.options.readiness.waitForFunction,void 0,{timeout:e.options.readiness.timeoutMs}).catch(()=>{}),e.options.javaScriptEnabled?await ot(o,e.options.autoScrollTimeoutMs):await o.waitForLoadState("load",{timeout:e.options.readiness.timeoutMs}).catch(()=>{}),e.options.readiness.fallbackWaitMs>0&&await o.waitForTimeout(e.options.readiness.fallbackWaitMs);let a=s?.();if(a)throw a;let l=await o.evaluate(()=>{let g=(y,Re)=>{let D=[];for(let be of document.querySelectorAll(y))for(let te of Re){let q=be.getAttribute(te);if(q)if(te.includes("srcset"))for(let Ee of q.split(",")){let re=Ee.trim().split(/\s+/,1)[0];re&&D.push(re);}else D.push(q);}return D},I=y=>{try{return new URL(y,document.baseURI).href}catch{return null}};return {links:g("a[href]",["href"]).map(I).filter(y=>y!==null),assets:g("img,script,link,source,video,audio,iframe,[data-src],[data-href],[data-resource-url]",["src","href","srcset","data-src","data-srcset","data-href","data-resource-url","poster"]).map(I).filter(y=>y!==null)}}),p=await o.content(),d=s();if(d)throw d;let P=new URL(t),$=F(l.links).filter(g=>{let I=new URL(g);return I.hash="",I.origin===P.origin}),B=E(z(w,e.jobId,e.taskId),"pages",`${String(r).padStart(4,"0")}-${M(t).slice(0,24)}.html`),W=await ee(B,p,"text/html; charset=utf-8"),f=(await n.cookies()).map(g=>({name:g.name,domain:g.domain,path:g.path,expires:g.expires,httpOnly:g.httpOnly,secure:g.secure,sameSite:g.sameSite,..."partitionKey"in g?{partitioned:!0}:{}})),j=u?.status()??null;return {requestedUrl:t,finalUrl:o?.url()||null,outcome:j!==null&&j>=400?"http-error":"rendered",httpStatus:j,contentType:u?.headers()["content-type"]??null,html:W,discoveredPages:$,discoveredAssets:F(l.assets),networkUrls:F([...i]),cookies:f}}catch(c){return {requestedUrl:t,finalUrl:o?.url()||null,outcome:"failed",httpStatus:null,contentType:null,html:null,discoveredPages:[],discoveredAssets:[],networkUrls:F([...i]),cookies:[],error:c instanceof Error?c.message:String(c)}}finally{await n?.close().catch(()=>{});}}async function ct(e,t){if(!Array.isArray(e.urls)||e.urls.length<1||e.urls.length>20)throw new Error("Render tasks require between 1 and 20 URLs.");let r=new URL(e.sourceOrigin).origin,n=[];for(let s=0;s<e.urls.length;s+=1){let i=e.urls[s];if(!i)continue;let c=t.getRemainingTimeInMillis?.();if(c!==void 0&&c<6e4){n.push({requestedUrl:i,finalUrl:null,outcome:"deferred",httpStatus:null,contentType:null,html:null,discoveredPages:[],discoveredAssets:[],networkUrls:[],cookies:[],error:"Worker stopped accepting URLs because less than 60 seconds remained."});continue}let u=new URL(i);if(u.origin!==r)throw new Error(`Primary render URL is outside sourceOrigin: ${i}`);n.push(await at(e,u.toString(),s));}let o=n.filter(s=>s.outcome==="failed"||s.outcome==="deferred").length;return {response:{schemaVersion:b,operation:"render",jobId:e.jobId,taskId:e.taskId,status:o>0?"partial":"succeeded",completedItems:n.length-o,failedItems:o,exporterVersion:H},detail:{pages:n}}}async function lt(e){if(!Array.isArray(e.items)||e.items.length<1||e.items.length>500)throw new Error("Rewrite tasks require between 1 and 500 objects.");let r={...await N(e.configKey),outputDir:"/workspace"},n=await N(e.assetMapKey),o=e.previousAssetMapKey?await N(e.previousAssetMapKey):{},s=[];for(let i of e.items){let c=k(i.inputKey,w),u=k(i.outputKey,w),a=await ye(c),l=`/workspace/${i.relativePath.replace(/^\/+/,"")}`,p=ge(a,r,n,l,o),d=await ee(u,p,"text/plain; charset=utf-8");s.push({inputKey:c,outputKey:u,changed:p!==a,output:d});}return {response:{schemaVersion:b,operation:"rewrite",jobId:e.jobId,taskId:e.taskId,status:"succeeded",completedItems:s.length,failedItems:0,exporterVersion:H},detail:{objects:s}}}function ut(e,t){return `${e}/${t.split("/").map(r=>encodeURIComponent(r)).join("/")}`}async function pt(e){if(!Array.isArray(e.items)||e.items.length>1e3)throw new Error("Deploy copy tasks accept at most 1000 objects.");let t=Qe.find(a=>a.id===e.targetId);if(!t)throw new Error(`Unknown deployment target: ${e.targetId}`);let r=me.get(t.region);r||(r=new S3Client({region:t.region}),me.set(t.region,r));let n=[];for(let a of e.items){let l=k(a.sourceKey,w),p=E(t.prefix,a.targetRelativeKey);try{let d=await r.send(new HeadObjectCommand({Bucket:t.bucketName,Key:p})).catch(f=>{if((f&&typeof f=="object"&&"$metadata"in f?Number(f.$metadata?.httpStatusCode):0)===404||f?.name==="NotFound")return null;throw f}),P=String(d?.CacheControl??"").replace(/\s+/g,"")===a.cacheControl.replace(/\s+/g,""),$=d?.Metadata?.["wpsuite-sha256"]===a.sha256,B=!!a.normalizedSha256&&d?.Metadata?.["wpsuite-normalized-sha256"]===a.normalizedSha256;if(P&&($||B)){n.push({sourceKey:l,targetKey:p,status:"skipped"});continue}let W=await r.send(new CopyObjectCommand({Bucket:t.bucketName,Key:p,CopySource:ut(S,l),MetadataDirective:"REPLACE",ContentType:a.contentType,CacheControl:a.cacheControl,Metadata:{"wpsuite-sha256":a.sha256,...a.normalizedSha256?{"wpsuite-normalized-sha256":a.normalizedSha256}:{}}}));n.push({sourceKey:l,targetKey:p,status:"copied",...W.CopyObjectResult?.ETag?{etag:W.CopyObjectResult.ETag}:{}});}catch(d){n.push({sourceKey:l,targetKey:p,status:"failed",error:d instanceof Error?d.message:String(d)});}}let s=(e.deleteRelativeKeys??[]).map(a=>E(t.prefix,a)),i=[],c=[];if(s.length>0){let a=await r.send(new DeleteObjectsCommand({Bucket:t.bucketName,Delete:{Objects:s.map(l=>({Key:l})),Quiet:false}}));i.push(...(a.Deleted??[]).flatMap(l=>l.Key?[l.Key]:[])),c.push(...(a.Errors??[]).map(l=>({key:l.Key??"",error:l.Message??l.Code??"S3 deletion failed"})));}let u=n.filter(a=>a.status==="failed").length+c.length;return {response:{schemaVersion:b,operation:"deploy-copy",jobId:e.jobId,taskId:e.taskId,status:u>0?"partial":"succeeded",completedItems:n.filter(a=>a.status!=="failed").length+i.length,failedItems:u,exporterVersion:H},detail:{objects:n,deletedKeys:i,deleteErrors:c,targetId:t.id}}}async function fe(e,t){if(e.operation==="health")return {response:{schemaVersion:b,operation:"health",jobId:e.jobId,taskId:e.taskId,status:"succeeded",completedItems:1,failedItems:0,exporterVersion:H},detail:{allowedOperation:_,workspaceBucket:S,workspacePrefix:w,proxyConfigured:!!Z}};if(_&&_!==e.operation)throw new Error(`Function is configured for ${_}, not ${e.operation}.`);return e.operation==="render"?await ct(e,t):e.operation==="rewrite"?await lt(e):await pt(e)}async function Nt(e,t={}){let r=se(e);if(r.operation==="health")return (await fe(r,t)).response;Q();let n=E(z(w,r.jobId,r.taskId),"result.json"),o=await it(n);if(o)return o;let s=await fe(r,t),i={...s.response,resultKey:n};return await st(n,{schemaVersion:b,response:i,detail:s.detail,completedAt:new Date().toISOString(),requestId:t.awsRequestId??null}),i}export{Nt as handler};
@@ -1,4 +1,4 @@
1
- import h from'fs/promises';import p from'path';import {workerData,parentPort}from'worker_threads';import'fast-glob';import'os';import A from'crypto';async function k(e){await h.mkdir(p.dirname(e),{recursive:true});}async function R(e,t,s){await k(e);let r=p.join(p.dirname(e),`.${p.basename(e)}.${process.pid}.${Date.now()}.${A.randomBytes(6).toString("hex")}.tmp`);try{typeof t=="string"?await h.writeFile(r,t,s??"utf8"):await h.writeFile(r,t),await h.rename(r,e);}catch(i){throw await h.unlink(r).catch(()=>{}),i}}function S(e){return e.replace(/&quot;/g,'"').replace(/&#34;/g,'"').replace(/&apos;/g,"'").replace(/&#39;/g,"'").replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">")}function g(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function d(e){return e.replace(/\//g,"\\/")}function y(e){return e.replace(/\//g,"\\\\/")}function O(e){try{let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")return null;let s=t.pathname==="/"&&!t.search&&!t.hash?"":`${t.pathname}${t.search}${t.hash}`;return `//${t.host}${s}`}catch{return null}}function E(e,t,s){if(!t)return;e[t]=s;let r=d(t),i=d(s);e[r]=i;let n=y(t),o=y(s);e[n]=o;let c=g(t),a=g(s);e[c]=a;let l=g(r),u=g(i);e[l]=u;let f=g(n),b=g(o);e[f]=b;}function w(e,t,s){E(e,t,s);let r=O(t);r&&r!==t&&E(e,r,s);}function v(e,t){try{return new URL(e,t==="."?"https://relative.invalid":t).pathname}catch{return e.startsWith("/")?e:`/${e.replace(/^\.\//,"")}`}}function F(e,t,s){if(!t)return s;let r=p.dirname(p.resolve(t)),i=p.resolve(e,s.replace(/^\/+/,"")),n=p.relative(r,i).replace(/\\/g,"/");return n?(n.startsWith(".")||(n=`./${n}`),n):"."}var H=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],L=new Set([".html",".htm"]),M="WPSuite.io Static Publisher",U=M.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function N(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function I(e){return e.startsWith("//")||e.startsWith("\\/\\/")||e.startsWith("\\\\/\\\\/")}function q(e,t,s){return I(t)?e.replace(new RegExp(`(?<!:)${N(t)}`,"g"),s):e.split(t).join(s)}function B(e,t,s){if(e.wpsuite?.siteSettings?.subscriber===true||e.wpsuite?.subscriptionType==="PROFESSIONAL"||e.wpsuite?.subscriptionType==="AGENCY"||!s)return false;let i=p.extname(s).toLowerCase();return L.has(i)?/<head\b|<html\b|<!doctype html/i.test(t):false}function V(e){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${U}\\2)[^>]*\\/?>`,"i").test(e))return e;let t=`<meta name="generator" content="${M}" />`;if(e.match(/(\r?\n)([ \t]*)<\/head>/i))return e.replace(/(\r?\n)([ \t]*)<\/head>/i,`$1$2${t}$1$2</head>`);let r=e.includes(`\r
1
+ import d from'fs/promises';import p from'path';import {workerData,parentPort}from'worker_threads';import'fast-glob';import'os';import A from'crypto';async function k(e){await d.mkdir(p.dirname(e),{recursive:true});}async function R(e,t,s){await k(e);let r=p.join(p.dirname(e),`.${p.basename(e)}.${process.pid}.${Date.now()}.${A.randomBytes(6).toString("hex")}.tmp`);try{typeof t=="string"?await d.writeFile(r,t,s??"utf8"):await d.writeFile(r,t),await d.rename(r,e);}catch(i){throw await d.unlink(r).catch(()=>{}),i}}function S(e){return e.replace(/&quot;/g,'"').replace(/&#34;/g,'"').replace(/&apos;/g,"'").replace(/&#39;/g,"'").replace(/&amp;/g,"&").replace(/&lt;/g,"<").replace(/&gt;/g,">")}function g(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/'/g,"&#39;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function m(e){return e.replace(/\//g,"\\/")}function y(e){return e.replace(/\//g,"\\\\/")}function O(e){try{let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")return null;let s=t.pathname==="/"&&!t.search&&!t.hash?"":`${t.pathname}${t.search}${t.hash}`;return `//${t.host}${s}`}catch{return null}}function E(e,t,s){if(!t)return;e[t]=s;let r=m(t),i=m(s);e[r]=i;let n=y(t),o=y(s);e[n]=o;let c=g(t),a=g(s);e[c]=a;let l=g(r),u=g(i);e[l]=u;let h=g(n),b=g(o);e[h]=b;}function w(e,t,s){E(e,t,s);let r=O(t);r&&r!==t&&E(e,r,s);}function v(e,t){try{return new URL(e,t==="."?"https://relative.invalid":t).pathname}catch{return e.startsWith("/")?e:`/${e.replace(/^\.\//,"")}`}}function F(e,t,s){if(!t)return s;let r=p.dirname(p.resolve(t)),i=p.resolve(e,s.replace(/^\/+/,"")),n=p.relative(r,i).replace(/\\/g,"/");return n?(n.startsWith(".")||(n=`./${n}`),n):"."}var H=["wp-content/","wp-includes/","wp-admin/","wp-json/","_next/"],L=new Set([".html",".htm"]),M="WPSuite.io Static Publisher",U=M.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");function N(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function I(e){return e.startsWith("//")||e.startsWith("\\/\\/")||e.startsWith("\\\\/\\\\/")}function q(e,t,s){return I(t)?e.replace(new RegExp(`(?<!:)${N(t)}`,"g"),s):e.split(t).join(s)}function B(e,t,s){if(e.wpsuite?.siteSettings?.subscriber===true||e.wpsuite?.subscriptionType==="PROFESSIONAL"||e.wpsuite?.subscriptionType==="AGENCY"||!s)return false;let i=p.extname(s).toLowerCase();return L.has(i)?/<head\b|<html\b|<!doctype html/i.test(t):false}function V(e){if(new RegExp(`<meta\\b(?=[^>]*\\bname=(["'])generator\\1)(?=[^>]*\\bcontent=(["'])${U}\\2)[^>]*\\/?>`,"i").test(e))return e;let t=`<meta name="generator" content="${M}" />`;if(e.match(/(\r?\n)([ \t]*)<\/head>/i))return e.replace(/(\r?\n)([ \t]*)<\/head>/i,`$1$2${t}$1$2</head>`);let r=e.includes(`\r
2
2
  `)?`\r
3
3
  `:`
4
- `;return e.replace(/<head\b[^>]*>/i,i=>`${i}${r} ${t}`)}function G(e){let t=S(e).trim();return t.startsWith("{")||t.startsWith("[")||t.includes("\\/")||/"@context"|"@type"/.test(t)}function J(e,t){let s=[...new Set(t)].filter(n=>n.includes("/")&&!/\\+\//.test(n)).map(n=>[n,d(n)]).filter(([n,o])=>n!==o).sort((n,o)=>o[0].length-n[0].length);if(!s.length)return e;let r=n=>{let o=n;for(let[c,a]of s)o=o.split(c).join(a);return o},i=e.replace(/(<script\b[^>]*\btype=["']application\/(?:ld\+)?json["'][^>]*>)([\s\S]*?)(<\/script>)/gi,(n,o,c,a)=>`${o}${r(c)}${a}`);return i=i.replace(/(<meta\b[^>]*\bcontent=(['"]))([\s\S]*?)(\2[^>]*>)/gi,(n,o,c,a,l)=>G(a)?`${o}${r(a)}${l}`:n),i}function z(e,t){if(!t)return null;let s=p.dirname(p.resolve(t)),r=p.resolve(e.outputDir),i=p.relative(s,r).replace(/\\/g,"/");return i?(i.startsWith(".")||(i=`./${i}`),i.endsWith("/")?i:`${i}/`):"./"}function _(e,t,s){let r=z(t,s);if(!r)return e;let i=e;for(let n of H){let o=n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),c=new RegExp(`(?:\\.{1,}/)+${o}`,"g"),a=new RegExp(`(?<!\\.)/${o}`,"g");i=i.replace(c,`${r}${n}`).replace(a,`${r}${n}`);let l=n.replace(/\//g,"\\/"),u=r.replace(/\//g,"\\/"),f=l.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),b=new RegExp(`(?:\\.{1,}\\\\/)+${f}`,"g"),j=new RegExp(`(?:\\.{1,}(?:\\\\/|\\\\))+${f}`,"g"),D=new RegExp(`(?<![\\.\\\\])\\/${f}`,"g");i=i.replace(b,`${u}${l}`).replace(j,`${u}${l}`).replace(D,`${u}${l}`);}return i}function P(e,t,s){if(e.urlRewriteMode==="absolute")return t;let r=(()=>{try{return new URL(t,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin)}catch{return null}})(),i=r?r.pathname:v(t,e.targetOrigin),n=r?`${r.search}${r.hash}`:"";return e.urlRewriteMode==="root-relative"?`${i}${n}`:`${F(e.outputDir,s,i)}${n}`}function $(e,t,s,r,i){w(e,s,P(t,r,i));}function K(e,t,s,r,i){if(!s||s===r)return;w(e,s,r);let n=P(t,s,i),o=P(t,r,i);w(e,n,o);}function C(e,t,s,r,i={}){let n=e,o={};$(o,t,t.sourceOrigin,t.targetOrigin,r);for(let[a,l]of Object.entries(t.extraReplacements))$(o,t,a,l,r);for(let[a,l]of Object.entries(s))$(o,t,a,l,r);for(let[a,l]of Object.entries(i)){let u=s[a];!u||u===l||K(o,t,l,u,r);}let c=Object.entries(o).sort((a,l)=>l[0].length-a[0].length);for(let[a,l]of c)n=q(n,a,l);return t.urlRewriteMode!=="absolute"&&(n=_(n,t,r)),n=J(n,c.map(([,a])=>a)),B(t,n,r)&&(n=V(n)),n}var{config:T,assetMap:X,previousAssetMap:Z}=workerData;async function ee(e){let t=p.join(T.outputDir,e),s=p.extname(t).toLowerCase(),r;try{r=await h.readFile(t,"utf8");}catch(o){if(o.code==="ENOENT")return false;throw o}let n=C(r,T,X,s===".js"||s===".mjs"?void 0:t,Z);return n===r?false:(await R(t,n,"utf8"),true)}if(!parentPort)throw new Error("rewrite worker requires a parent port");parentPort.on("message",async e=>{try{let t=await ee(e.file);parentPort?.postMessage({changed:t});}catch(t){parentPort?.postMessage({changed:false,error:t instanceof Error?t.message:String(t)});}});
4
+ `;return e.replace(/<head\b[^>]*>/i,i=>`${i}${r} ${t}`)}function G(e){let t=S(e).trim();return t.startsWith("{")||t.startsWith("[")||t.includes("\\/")||/"@context"|"@type"/.test(t)}function J(e,t){let s=[...new Set(t)].filter(n=>n.includes("/")&&!/\\+\//.test(n)).map(n=>[n,m(n)]).filter(([n,o])=>n!==o).sort((n,o)=>o[0].length-n[0].length);if(!s.length)return e;let r=n=>{let o=n;for(let[c,a]of s)o=o.split(c).join(a);return o},i=e.replace(/(<script\b[^>]*\btype=["']application\/(?:ld\+)?json["'][^>]*>)([\s\S]*?)(<\/script>)/gi,(n,o,c,a)=>`${o}${r(c)}${a}`);return i=i.replace(/(<meta\b[^>]*\bcontent=(['"]))([\s\S]*?)(\2[^>]*>)/gi,(n,o,c,a,l)=>G(a)?`${o}${r(a)}${l}`:n),i}function z(e,t){if(!t)return null;let s=p.dirname(p.resolve(t)),r=p.resolve(e.outputDir),i=p.relative(s,r).replace(/\\/g,"/");return i?(i.startsWith(".")||(i=`./${i}`),i.endsWith("/")?i:`${i}/`):"./"}function _(e,t,s){let r=z(t,s);if(!r)return e;let i=e;for(let n of H){let o=n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),c=new RegExp(`(?:\\.{1,}/)+${o}`,"g"),a=new RegExp(`(?<!\\.)/${o}`,"g");i=i.replace(c,`${r}${n}`).replace(a,`${r}${n}`);let l=n.replace(/\//g,"\\/"),u=r.replace(/\//g,"\\/"),h=l.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),b=new RegExp(`(?:\\.{1,}\\\\/)+${h}`,"g"),j=new RegExp(`(?:\\.{1,}(?:\\\\/|\\\\))+${h}`,"g"),D=new RegExp(`(?<![\\.\\\\])\\/${h}`,"g");i=i.replace(b,`${u}${l}`).replace(j,`${u}${l}`).replace(D,`${u}${l}`);}return i}function P(e,t,s){if(e.urlRewriteMode==="absolute")return t;let r=(()=>{try{return new URL(t,e.targetOrigin==="."?"https://relative.invalid":e.targetOrigin)}catch{return null}})(),i=r?r.pathname:v(t,e.targetOrigin),n=r?`${r.search}${r.hash}`:"";return e.urlRewriteMode==="root-relative"?`${i}${n}`:`${F(e.outputDir,s,i)}${n}`}function $(e,t,s,r,i){w(e,s,P(t,r,i));}function K(e,t,s,r,i){if(!s||s===r)return;w(e,s,r);let n=P(t,s,i),o=P(t,r,i);w(e,n,o);}function C(e,t,s,r,i={}){let n=e,o={};$(o,t,t.sourceOrigin,t.targetOrigin,r);for(let[a,l]of Object.entries(t.extraReplacements))$(o,t,a,l,r);for(let[a,l]of Object.entries(s))$(o,t,a,l,r);for(let[a,l]of Object.entries(i)){let u=s[a];!u||u===l||K(o,t,l,u,r);}let c=Object.entries(o).sort((a,l)=>l[0].length-a[0].length);for(let[a,l]of c)n=q(n,a,l);return t.urlRewriteMode!=="absolute"&&(n=_(n,t,r)),n=J(n,c.map(([,a])=>a)),B(t,n,r)&&(n=V(n)),n}var{config:T,assetMap:X,previousAssetMap:Z}=workerData;async function ee(e){let t=p.join(T.outputDir,e),s=p.extname(t).toLowerCase(),r;try{r=await d.readFile(t,"utf8");}catch(o){if(o.code==="ENOENT")return false;throw o}let n=C(r,T,X,s===".js"||s===".mjs"?void 0:t,Z);return n===r?false:(await R(t,n,"utf8"),true)}if(!parentPort)throw new Error("rewrite worker requires a parent port");parentPort.on("message",async e=>{try{let t=await ee(e.file);parentPort?.postMessage({changed:t});}catch(t){parentPort?.postMessage({changed:false,error:t instanceof Error?t.message:String(t)});}});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smart-cloud/publisher-exporter",
3
- "version": "1.1.60",
3
+ "version": "1.1.62",
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.",
@@ -10,6 +10,7 @@
10
10
  "files": [
11
11
  "dist",
12
12
  "publisher.config.example.json",
13
+ "remote-workers.example.json",
13
14
  "README.md",
14
15
  "package.json"
15
16
  ],
@@ -35,6 +36,7 @@
35
36
  "deploy": "node dist/deploy.js",
36
37
  "invalidate": "node dist/invalidate.js",
37
38
  "logs:prune": "node dist/prune-logs.js",
39
+ "remote-workers:health": "node dist/remote-worker-health.js",
38
40
  "queue:run": "node dist/queue-runner.js",
39
41
  "queue:drain": "node dist/queue-runner.js --max-jobs=100",
40
42
  "publish:site": "npm run build && npm run crawl && npm run deploy && npm run invalidate",
@@ -44,7 +46,9 @@
44
46
  },
45
47
  "dependencies": {
46
48
  "@aws-sdk/client-cloudfront": "^3.1127.0",
49
+ "@aws-sdk/client-lambda": "^3.1127.0",
47
50
  "@aws-sdk/client-s3": "^3.1127.0",
51
+ "@aws-sdk/credential-providers": "^3.1127.0",
48
52
  "@aws-sdk/lib-storage": "^3.1127.0",
49
53
  "@smart-cloud/publisher-core": "^1.1.10",
50
54
  "@smart-cloud/wpsuite-core": "^2.4.1",
@@ -30,6 +30,13 @@
30
30
  "maxPages": 0,
31
31
  "concurrency": 2,
32
32
  "assetDownloadConcurrency": 6,
33
+ "remoteWorkers": {
34
+ "render": {
35
+ "enabled": false,
36
+ "concurrency": 4,
37
+ "maxAttempts": 2
38
+ }
39
+ },
33
40
  "logLevel": "info",
34
41
  "s3SyncMode": "sdk-upload-delete",
35
42
  "s3": {
@@ -0,0 +1,24 @@
1
+ {
2
+ "schemaVersion": 1,
3
+ "region": "eu-central-1",
4
+ "roleArn": "arn:aws:iam::123456789012:role/WpSuiteStaticPublisherWorkers-ExporterAccessRole",
5
+ "roleSessionName": "wpsuite-static-publisher",
6
+ "workspace": {
7
+ "bucket": "existing-workspace-bucket",
8
+ "prefix": "static-publisher/dev/"
9
+ },
10
+ "functions": {
11
+ "render": "arn:aws:lambda:eu-central-1:123456789012:function:render-worker:live",
12
+ "rewrite": "arn:aws:lambda:eu-central-1:123456789012:function:rewrite-worker:live",
13
+ "deploy-copy": "arn:aws:lambda:eu-central-1:123456789012:function:deploy-worker:live"
14
+ },
15
+ "targets": [
16
+ {
17
+ "id": "production",
18
+ "bucketName": "static-site-bucket",
19
+ "prefix": "www/",
20
+ "region": "eu-central-1"
21
+ }
22
+ ],
23
+ "protocolVersion": 1
24
+ }