@smart-cloud/publisher-exporter 1.1.56 → 1.1.58
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/content-sync.js +10 -10
- package/dist/crawl.js +8 -8
- package/dist/queue-runner.js +3 -3
- package/package.json +1 -1
package/dist/queue-runner.js
CHANGED
|
@@ -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 He 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 z(e){return Array.isArray(e)?e.map(z):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>[t,z(n)])):e}function Ke(e){return JSON.stringify(z(e))}function L(e){return createHash("sha256").update(Ke(e)).digest("hex")}function fe(e){return L({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 _e(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 $(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 H(e,t,n){return L({schemaVersion:1,sourceOrigin:_e(e),ruleId:t.trim(),scope:$(n)})}function V(e){return L({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 Ve="wp-json/smartcloud-static-publisher/v1/content-sync/",Ge=25*1024*1024,E=class extends Error{status;code;constructor(t,n,r){super(r),this.name="ContentSyncApiError",this.status=t,this.code=n;}},W=class{options;constructor(t){if(!t.runtimeToken.trim())throw new Error("Content-sync requires a WordPress runtime token.");this.options={...t,timeoutMs:t.timeoutMs??6e4};}endpoint(t){let n=this.options.sourceOrigin.replace(/\/+$/,"")+"/";return new URL(`${Ve}${t}`,n)}async post(t,n){let r=this.endpoint(t),i=Buffer.from(JSON.stringify(n),"utf8"),a=r.protocol==="https:"?be:He,s=r.protocol==="https:"?new be.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((o,c)=>{let u=a.request(r,{method:"POST",agent:s,headers:{"content-type":"application/json","content-length":String(i.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},d=>{let g=[],p=0;d.on("data",h=>{if(p+=h.byteLength,p>Ge){d.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(h);}),d.on("error",c),d.on("end",()=>{let h=Buffer.concat(g).toString("utf8"),y;try{y=h?JSON.parse(h):{};}catch{c(new E(d.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let l=d.statusCode??500;if(l<200||l>=300){c(new E(l,String(y.code??"request-failed"),String(y.message??`Content-sync API request failed (${l}).`)));return}o(y);});});u.on("timeout",()=>u.destroy(new Error(`Content-sync API request timed out: ${t}`))),u.on("error",c),u.end(i);})}async head(t){return this.post("head",t)}async unboundHead(t,n){return this.post("head",{postTypes:t,includeSubsites:n})}async events(t){let n=[],r=t.afterSequence,i=new Set(t.postTypes);for(;r<t.throughSequence;){let a=await this.post("events",{...t,afterSequence:r,limit:250});if(a.fromSequence!==r||a.throughSequence!==t.throughSequence||a.nextSequence<r||a.nextSequence>t.throughSequence)throw new Error("Content-sync API returned an inconsistent event page.");if(!Array.isArray(a.items))throw new Error("Content-sync API returned a malformed event page.");let s=r;for(let o of a.items){if(!Number.isSafeInteger(o.sequence)||o.sequence<=s||o.sequence>t.throughSequence||!Number.isSafeInteger(o.postId)||o.postId<=0||!Number.isSafeInteger(o.blogId)||o.blogId<=0||!i.has(o.postType)||typeof o.operation!="string"||!o.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");s=o.sequence;}if(a.items.length>0&&a.nextSequence!==s)throw new Error("Content-sync API event cursor does not match the final returned event.");if(n.push(...a.items),!a.hasMore)break;if(a.nextSequence===r)throw new Error("Content-sync API event pagination did not advance.");r=a.nextSequence;}if(t.throughSequence>t.afterSequence&&n.at(-1)?.sequence!==t.throughSequence)throw new Error("Content-sync API omitted the claimed journal high-water event.");return n}async resolveImpactFamilies(t,n){let r=await this.post("impact",{families:t,includeSubsites:n});return Array.isArray(r.families)?r.families:[]}async releaseFingerprint(t=false){let n=await this.post("fingerprint",{includeSubsites:t}),r=String(n.fingerprint||"").trim();if(!/^[a-f0-9]{64}$/.test(r))throw new Error("WordPress returned an invalid release fingerprint.");return r}async establishBaseline(t){return this.post("baseline",t)}async acknowledge(t){return this.post("ack",t)}};function Ye(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Ze(e,t){return {...e,...t??{}}}function Xe(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function G(e,t){let n=String(t??e.deploymentTargetOverride??"").trim();if(!Ye(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:Ze(e.s3,i.s3),cloudFront:Xe(e.cloudFront,i.cloudFront)}}}var Y=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function we(e){let t=new Set;for(let n of [...e,...Y]){let r=String(n||"").trim();r&&t.add(r);}return [...t]}function Pe(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function rt(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 Z(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t.trim(),String(n??"")]).filter(([t])=>t.length>0))}function it(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=Z(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(d=>String(d??"").trim()).filter(d=>d.length>0)),Object.keys(c).length>0&&(s.cloudFront=c);}t[i]=s;}return t}function ee(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function st(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(d=>String(d??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],u=Array.isArray(n.listingPaths)?[...new Set(n.listingPaths.map(d=>String(d??"").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)=>st(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 ot(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 te(e){let t=e&&typeof e=="object"?e:{},n=String(t.accountId??"").trim(),r=String(t.siteId??"").trim(),i=t.subscriber===true,a=ot(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 d=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();d&&(o.virtualAssetBaseUrl=d),Object.keys(s).length>0&&(o.siteSettings=s);let g=ee(t.subscriptionType);return g&&(o.subscriptionType=g),o}function at(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 ct(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:"?He:be,s=ct(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}:{}},d=>{let g=[];d.on("data",p=>{g.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),d.on("error",c),d.on("end",()=>{let p=d.statusCode??0,h=d.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 y=new Headers;for(let[l,S]of Object.entries(d.headers))Array.isArray(S)?y.set(l,S.join(", ")):typeof S=="string"&&y.set(l,S);o(new Response(Buffer.concat(g),{status:p,statusText:d.statusMessage??"",headers:y}));});});u.on("error",c),u.end();})}function ut(e){let t=te(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;at(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 lt(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!ut(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 d=Ae(c);if(d.startsWith(t)){let g=(()=>{try{return new URL(d).pathname}catch{return d}})();try{let p=e.ignoreHttpsErrors&&d.startsWith("https://")?await Ie(c,u):await n(c,u);return r.push({path:g,status:p.status,ok:p.ok,error:null}),p}catch(p){throw r.push({path:g,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return n(c,u)}));let{getConfig:i}=await import('@smart-cloud/wpsuite-core'),a=await i("publisher"),s=a&&typeof a=="object"?a:null,o=ee(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 dt(e,t){let n=it(t?.deploymentProfiles),r=String(t?.defaultDeploymentProfile??"").trim(),i=n[r]?r:"",a=ee(t?.subscriptionType),s=te(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 X(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 gt(){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,X(r,n))}async function ne(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=rt(i.generated404RequestPath),i.sitemapPaths||=[...Y],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=Z(i.extraReplacements),i.postCrawlCopyMap=Z(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=te(i.wpsuite),i.scheduler=Ce(void 0),i.deploymentProfiles={},i.defaultDeploymentProfile="",i.deploymentTargetOverride=String(i.deploymentTargetOverride??"").trim();let a=await lt(i);t.onRemotePublisherConfigDiagnostic?.(a.diagnostic);let s=dt(i,a.remote),o=gt(),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=X(s.outputDir,"export")),m.isAbsolute(s.logDir)||(s.logDir=X(s.logDir,"logs"))),s}function re(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 ie(e,t){await Re(e.activeRules,{contractVersion:1,evaluatedAt:new Date().toISOString(),entries:t});}async function Re(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 pt(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 xe(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 Re(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function ke(e,t){let n=await pt(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 Oe(e){let t=String(e.command??"").trim();return (t==="publish"||t==="crawl")&&e.crawlMode==="incremental"||t==="content-sync"}var se=new Map;async function Te(e){let t=m.resolve(e),n=se.get(t);if(n!==void 0)return n;try{let r=JSON.parse(await P.readFile(t,"utf8")),i=String(r.version??"").trim();return se.set(t,i),i}catch{return se.set(t,""),""}}var U=class extends Error{request;step;constructor(t,n){super(`Stop requested during ${n}.`),this.name="StopRequestedError",this.request=t,this.step=n;}},bt="queue-mutation.lock",wt=5e3,vt=3e4;function Pt(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 Le(e){return m.join(e,bt)}async function Ct(e){await new Promise(t=>setTimeout(t,e));}async function At(e){let t=Le(e),n=Date.now()+wt;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>vt){await P.unlink(t).catch(()=>{});continue}if(Date.now()>=n)throw new Error("Timed out acquiring queue mutation lock.",{cause:r});await Ct(50);}}async function It(e){await P.unlink(Le(e)).catch(()=>{});}async function j(e,t){await At(e);try{return await t()}finally{await It(e);}}function Be(e){return m.join(e,"queue-runner-heartbeat.json")}function ge(e){return m.join(e,"current-progress.json")}function oe(e){return m.join(e,"scheduler-state.json")}function Rt(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 Me(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,Rt(i,"logs"))}function ae(e,t){return String(e||"").trim().replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/^-+|-+$/g,"")||t}function qt(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 xt(e){return [qt(e.endedAt||e.startedAt),ae(e.command||"job","job"),ae(e.id||"job","job"),ae(e.status||"finished","finished")].join("-")}function Dt(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 kt(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 Tt(e){return e.endsWith(".gz")?"application/gzip":e.endsWith(".jsonl")?"application/x-ndjson":e.endsWith(".json")?"application/json":"text/plain"}async function ce(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:kt(n),originalFileName:n,storedFileName:r,compressed:true,compression:"gzip",contentType:Tt(r),originalSize:a.size,storedSize:s.size}}async function N(e,t){let n=await Me(t),r=new Date().toISOString(),i=xt(e),a=m.join(n,"archive",i),s=[],o=[];await P.mkdir(a,{recursive:true});try{let d=await P.readdir(n,{withFileTypes:!0});for(let g of d){if(!g.isFile()||!Dt(g.name))continue;let p=await ce(m.join(n,g.name),a,g.name);o.push(p),s.push(p.storedFileName);}}catch(d){if((d&&typeof d=="object"&&"code"in d?String(d.code||""):"")!=="ENOENT")throw d}let c=ge(t.runtimeDir);if(existsSync(c)){let d=await ce(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 g=m.join(t.runtimeDir,d);if(!existsSync(g))continue;let p=await ce(g,a,d);o.push(p),s.push(p.storedFileName);}let u=[...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:u,artifacts:o,job:e}),{archiveKey:i,archiveDir:a,archiveCreatedAt:r,archivedFiles:u,artifacts:o}}function Et(e){return m.join(e,"audit-events.jsonl")}function le(e){return m.join(e,"stop-request.json")}async function Ue(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 Ee(e,t){let n=await Ue(e);if(!n)return null;let r=String(n.targetJobId||"").trim();return r&&r!==t?null:n}async function M(e,t){if(!t){await P.unlink(le(e)).catch(()=>{});return}let n=await Ue(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(Et(e),`${JSON.stringify(n)}
|
|
2
|
-
`,"utf8");}catch{}}async function C(e,t,n){let[r,i]=await Promise.all([Te(m.join(e.exporterDir,"package.json")),Te(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(Be(e.runtimeDir),a);}catch{}}async function F(e){try{await P.unlink(ge(e));}catch{}}function Ft(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(g=>String(g).trim()).filter(Boolean))]:[],u=Array.isArray(e.listingPaths)?[...new Set(e.listingPaths.map(g=>String(g).trim()).filter(Boolean))]:[],d=(n==="publish"||n==="crawl")&&e.crawlMode==="incremental"?"incremental":"full";return n==="url"&&!s||n==="content-sync"&&c.length===0?null:{id:i,enabled:a,command:n,intervalMinutes:r,...n==="publish"||n==="crawl"?{crawlMode:d}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&o?{deploymentProfile:o}:{},...s?{url:s}:{},...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 ne(e.configPath).catch(()=>null),n=t?.scheduler,r=Array.isArray(n?.rules)?n.rules.map((i,a)=>Ft(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 Q(e,t){if(!t)throw new Error("Content-sync scheduler requires a valid publisher config.");let 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=H(t.sourceOrigin,e.id,n),i=G(t,e.deploymentProfile),a=V({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}:${L({scopeFingerprint:r,targetFingerprint:a}).slice(0,32)}`}function Jt(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 Fe(e,t){let n=re(e.runtimeDir);if(await ie(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 d=Q(u,t.publisherConfig),g=Jt(u,d,s,o);g&&c.push({ruleId:u.id,coalesceKey:d,consumerId:g});}await ie(n,c);}async function Ot(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=$({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=H(o.sourceOrigin,n.id,c),d=G(o,n.deploymentProfile),g=V({sourceOrigin:o.sourceOrigin,targetOrigin:d.config.targetOrigin,deploymentProfile:d.name||"",s3Bucket:d.config.s3.bucket,s3Prefix:d.config.s3.prefix,cloudFrontDistributionId:d.config.cloudFront.distributionId,urlRewriteMode:d.config.urlRewriteMode,extraReplacements:d.profile?.extraReplacements??{}});if(s.scopeFingerprint!==u||s.targetFingerprint!==g||!s.releaseFingerprint)return {status:"baseline-required",reason:"The verified content-sync baseline does not match the active scope or deployment target. Run a successful full or incremental publish to establish a new baseline."};let p=new W({sourceOrigin:o.sourceOrigin,runtimeToken:String(o.wpsuite?.runtimeToken||""),ignoreHttpsErrors:o.ignoreHttpsErrors}),h=await p.releaseFingerprint(c.includeSubsites),y=fe({wordpressFingerprint:h,targetFingerprint:g,sourceOrigin:o.sourceOrigin,sitemapPaths:we(o.sitemapPaths),blockedPathPrefixes:o.blockedPathPrefixes,assetPathPrefixes:o.assetPathPrefixes,noJavaScriptRenderPathPrefixes:o.noJavaScriptRenderPathPrefixes,urlRewriteMode:o.urlRewriteMode});if(s.releaseFingerprint!==y)return {status:"baseline-required",reason:"The installed WordPress release changed after the last verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline."};let l=await p.head({consumerId:s.consumerId,scopeFingerprint:s.scopeFingerprint,baselineId:s.baselineId,postTypes:c.postTypes,includeSubsites:c.includeSubsites}),S=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)&&(S=Math.max(S,Number(b.toSequence)));}return {status:l.headSequence>S?"pending":"current",headSequence:l.headSequence,committedSequence:l.committedSequence}}catch(o){return o instanceof E&&o.code==="baseline-required"?{status:"baseline-required",reason:o.message}:{status:"unavailable",reason:o instanceof Error?o.message:String(o)}}}async function $e(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 Lt(e,t,n){if(!t.allowSchedulerAutoEnqueue)return await Fe(e,null),0;let r=await jt(e);if(await Fe(e,r),!r.enabled||r.rules.length===0)return 0;if(t.assertActiveSubscriptionForIdentity)try{await t.assertActiveSubscriptionForIdentity(r.wpsuite??null);}catch(l){let S=l instanceof Error?l.message:String(l);return console.warn(`[queue-runner] scheduler auto-enqueue skipped: ${S}`),0}let i=m.join(e.runtimeDir,"queue.json"),a=m.join(e.runtimeDir,"current-run.json"),s=Date.now(),o=0,c=[],u=[],d=[],g=new Map,[p,h,y]=await Promise.all([w(i,[]),w(a,null),w(oe(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}})]);for(let l of r.rules){if(!l.enabled||l.command!=="content-sync")continue;let S=Math.floor(s/(l.intervalMinutes*60*1e3)),b=y.lastEvaluatedBucketByRuleId?.[l.id]??y.lastEnqueuedBucketByRuleId?.[l.id]??-1;if(S<=b)continue;let D=Q(l,r.publisherConfig);de(p,{command:l.command,deploymentProfile:l.deploymentProfile,coalesceKey:D})||g.set(l.id,await Ot(e,r,l,D,h));}await j(e.runtimeDir,async()=>{let l=await w(i,[]),S=await w(a,null),b=await w(oe(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}});b.lastEnqueuedBucketByRuleId??={},b.lastEvaluatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.lastCreatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.coalescedCountByRuleId??={};let D=[...l];S&&S.status==="running"&&D.unshift(S);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"?Q(f,r.publisherConfig):"";b.lastEvaluatedBucketByRuleId[f.id]=x;let T=f.command==="content-sync"?l:D;if(de(T,{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 _=g.get(f.id);if(!_||_.status!=="pending"){let me=_??{status:"unavailable",reason:"Content-sync demand was not checked."};me.status!=="current"&&d.push({rule:f,check:me});continue}}let K={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(K),D.push(K),c.push({job:K,rule:f}),b.lastEnqueuedBucketByRuleId[f.id]=x,b.lastCreatedBucketByRuleId[f.id]=x,o+=1;}o>0&&await v(i,l),await v(oe(e.runtimeDir),b);});for(let l of c){if(l.rule.command==="content-sync"){let S=g.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:S?.headSequence??0,committedSequence:S?.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 d){if(l.check.status==="baseline-required"){let S=Q(l.rule,r.publisherConfig);await $e(e,l.rule.id,S,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 Bt(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 Mt(e){try{await P.unlink(e);}catch{}}function Ut(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 Ee(t,s);if(u)throw new U(u,n)}let o=Ut(e,n),c={...process.env,...a??{}};return c.STATIC_PUBLISHER_RUNTIME_DIR=t,i&&(c.PUBLISHER_CONFIG=i),await new Promise((u,d)=>{let g=null,p=null,h=null,y=null,l=false,S="",b=spawn(process.execPath,[o,...r],{cwd:e,env:c,stdio:["inherit","inherit","pipe"]});b.stderr?.on("data",A=>{S+=A.toString("utf8");});let D=()=>{p&&clearInterval(p),h&&clearInterval(h),y&&clearTimeout(y);},f=async()=>{if(!(!s||g||l)){l=true;try{let A=await Ee(t,s);if(!A)return;g=A,b.kill("SIGTERM"),y=setTimeout(()=>{b.kill("SIGKILL");},1e4);}finally{l=false;}}};s&&(p=setInterval(()=>{f();},1e3),h=setInterval(()=>{(async()=>{let A=Be(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(),d(A);}),b.on("close",(A,x)=>{D();let J=Se(S).trim();if(J&&process.stderr.write(`${J}
|
|
3
|
-
`),g){d(new U(g,n));return}if(A===0)u();else {let O=J.split(/\r?\n/).map(T=>T.trim()).find(T=>T&&!T.startsWith("at "));d(new Error(`${n} exited with code ${A??-1}${x?` (signal ${x})`:""}${O?`: ${O}`:""}`));}});}),0}function $t(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 ne(t.configPath).catch(()=>null),i=n(r?.wpsuite);if(i)return i;let a=n(e.wpsuite);return a||null}async function Nt(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 Qt(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 Me(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 Kt(e,t){if((e.command==="publish"||e.command==="crawl")&&t==="rewrite-text")return "rewrite-text"}async function _t(e){let t=m.join(e.runtimeDir,"queue.json"),n=m.join(e.runtimeDir,"current-run.json"),r=await j(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 M(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 zt(e,t,n){try{if((n.shouldEnforceSubscriptionOnJobExecution?.(e)??n.enforceSubscriptionOnJobExecution)&&n.assertActiveSubscriptionForIdentity){let o=await Nt(e,t);await n.assertActiveSubscriptionForIdentity(o);}let i=$t(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 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),{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),{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),{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),{exitCode:0}):{exitCode:2,error:`Unsupported command: ${e.command}`}}catch(r){return r instanceof U?{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=Pt(n),i=m.join(r.runtimeDir,"export.lock"),a=m.join(r.runtimeDir,"last-run.json");await F(r.runtimeDir),await C(r,"starting",{message:"queue-runner starting"});try{await Bt(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 _t(r);let s=await Lt(r,e),o=0;for(let c=0;c<r.maxJobs;c++){let u=await Yt(r,e);if(u==="none"||(o+=1,u==="stopped"))break}if(o===0)await F(r.runtimeDir),await C(r,"idle",{processedJobs:o,schedulerEnqueued:s,message:"no queued jobs"});else {await F(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 Mt(i);}}}async function je(e,t){if(t.command!=="content-sync"||!t.coalesceKey)return false;let n=re(e.runtimeDir),r=await ke(n,t.coalesceKey),i=await xe(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 Ht(e,t,n){let r=m.join(e.runtimeDir,"queue.json"),i=m.join(e.runtimeDir,"current-run.json");await j(e.runtimeDir,async()=>{let a=await w(r,[]),s=Array.isArray(a)?a.filter(u=>u?.id!==t.id):[],o=pe(t),c=Kt(t,n);c?o.resumeFromStep=c:delete o.resumeFromStep,await v(r,[o,...s]),await v(i,null);});}async function Vt(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 j(e.runtimeDir,async()=>{let c=m.join(e.runtimeDir,"queue.json"),u=m.join(e.runtimeDir,"current-run.json"),g=(await w(c,[])).filter(p=>p.id!==t.id);await v(c,[o,...g]),await v(u,null);}),t.coalesceKey){let c=m.join(e.runtimeDir,"content-sync-state.json"),d=(await w(c,{})).rules??{};d[t.coalesceKey]={...d[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:d});}return o}async function Gt(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 j(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 Yt(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 j(e.runtimeDir,async()=>{let g=await w(n,[]);if(!Array.isArray(g)||g.length===0)return null;let p=Date.parse(String(g[0]?.nextAttemptAt||""));if(Number.isFinite(p)&&p>Date.now())return null;let h={...g[0],status:"running",startedAt:a};return delete h.nextAttemptAt,await v(n,g.slice(1)),await v(r,h),h});if(!s)return "none";await F(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 zt(s,e,t),c=new Date().toISOString();if(o.stopped){let g=await Qt(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:g},y=false;p==="requeue"?await Ht(e,s,g):(await v(r,null),y=await je(e,s));try{let l=await N(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 M(e.runtimeDir,s.id),await F(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:g,logArchiveKey:h.logArchiveError||!h.logArchiveDir?"":m.basename(h.logArchiveDir),logArchiveDir:h.logArchiveDir||"",logArchiveCreatedAt:h.logArchiveCreatedAt||"",logArchiveFileCount:h.logArchiveFileCount??0,logArchiveError:h.logArchiveError||"",contentSyncAbandoned:y,journalCursorPreserved:y}}),"stopped"}if(s.command==="content-sync"&&o.exitCode!==0){if(/baseline (?:is missing or stale|required)|verified content-sync baseline|new baseline/i.test(o.error||"")){let y="The installed release no longer matches the verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline.";s.ruleId&&s.coalesceKey&&await $e(e,s.ruleId,s.coalesceKey,y);let l={...s,status:"failed",endedAt:c,exitCode:o.exitCode,error:y};await v(r,null),await F(e.runtimeDir),await je(e,s);try{let S=await N(l,e);l.logArchiveDir=S.archiveDir,l.logArchiveCreatedAt=S.archiveCreatedAt,l.logArchiveFileCount=S.archivedFiles.length;}catch(S){l.logArchiveError=S instanceof Error?S.message:String(S);}return await v(i,l),await M(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:y,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 Vt(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 y=await N(h,e);h.logArchiveDir=y.archiveDir,h.logArchiveCreatedAt=y.archiveCreatedAt,h.logArchiveFileCount=y.archivedFiles.length;}catch(y){h.logArchiveError=y instanceof Error?y.message:String(y);}return await v(i,h),await M(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 Gt(e,u),await M(e.runtimeDir,s.id);try{let g=await N(u,e);u.logArchiveDir=g.archiveDir,u.logArchiveCreatedAt=g.archiveCreatedAt,u.logArchiveFileCount=g.archivedFiles.length;}catch(g){u.logArchiveError=g instanceof Error?g.message:String(g);}await v(i,u);let d=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:d,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 Xt(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 en={allowSchedulerAutoEnqueue:true,enforceSubscriptionOnJobExecution:false,shouldEnforceSubscriptionOnJobExecution:e=>Oe(e)||e.enqueueSource==="scheduler"||e.createdBy===0,assertActiveSubscriptionForIdentity:Xt},Ne=We(en),tn=Ne,Qn=Ne;process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href&&tn().catch(e=>{console.error(e),process.exit(1);});export{Qn as default,tn as main,Ne 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 Ve from'http';import Se from'https';import {pipeline}from'stream/promises';import {createGzip}from'zlib';function me(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function _(e){return Array.isArray(e)?e.map(_):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>[t,_(n)])):e}function _e(e){return JSON.stringify(_(e))}function O(e){return createHash("sha256").update(_e(e)).digest("hex")}function he(e){return O({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 ze(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 U(e){let t=me((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=me((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 z(e,t,n){return O({schemaVersion:1,sourceOrigin:ze(e),ruleId:t.trim(),scope:U(n)})}function H(e){return O({schemaVersion:1,...e})}function ye(e){return e.replace(/((?:x-static-publisher-token|authorization|proxy-authorization|x-api-key|aws-access-key-id|aws-secret-access-key|aws-session-token)\s*:\s*)[^\r\n]+/gi,"$1[REDACTED]").replace(/\b(Basic|Bearer)\s+[^\s]+/gi,"$1 [REDACTED]").replace(/([?&](?:token|access_token|refresh_token|api_key|key|nonce)=)[^&\s]+/gi,"$1[REDACTED]").replace(/((?:"|')?(?:runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken)(?:"|')?\s*[:=]\s*(?:"|'))[^"']+/gi,"$1[REDACTED]")}var fe=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";fe?m.join(fe,"current-progress.json"):"";Promise.resolve();var Ge="wp-json/smartcloud-static-publisher/v1/content-sync/",Ye=25*1024*1024,T=class extends Error{status;code;constructor(t,n,r){super(r),this.name="ContentSyncApiError",this.status=t,this.code=n;}},$=class{options;constructor(t){if(!t.runtimeToken.trim())throw new Error("Content-sync requires a WordPress runtime token.");this.options={...t,timeoutMs:t.timeoutMs??6e4};}endpoint(t){let n=this.options.sourceOrigin.replace(/\/+$/,"")+"/";return new URL(`${Ge}${t}`,n)}async post(t,n){let r=this.endpoint(t),i=Buffer.from(JSON.stringify(n),"utf8"),a=r.protocol==="https:"?Se:Ve,s=r.protocol==="https:"?new Se.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((o,c)=>{let u=a.request(r,{method:"POST",agent:s,headers:{"content-type":"application/json","content-length":String(i.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},d=>{let g=[],p=0;d.on("data",h=>{if(p+=h.byteLength,p>Ye){d.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(h);}),d.on("error",c),d.on("end",()=>{let h=Buffer.concat(g).toString("utf8"),y;try{y=h?JSON.parse(h):{};}catch{c(new T(d.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let l=d.statusCode??500;if(l<200||l>=300){c(new T(l,String(y.code??"request-failed"),String(y.message??`Content-sync API request failed (${l}).`)));return}o(y);});});u.on("timeout",()=>u.destroy(new Error(`Content-sync API request timed out: ${t}`))),u.on("error",c),u.end(i);})}async head(t){return this.post("head",t)}async unboundHead(t,n){return this.post("head",{postTypes:t,includeSubsites:n})}async events(t){let n=[],r=t.afterSequence,i=new Set(t.postTypes);for(;r<t.throughSequence;){let a=await this.post("events",{...t,afterSequence:r,limit:250});if(a.fromSequence!==r||a.throughSequence!==t.throughSequence||a.nextSequence<r||a.nextSequence>t.throughSequence)throw new Error("Content-sync API returned an inconsistent event page.");if(!Array.isArray(a.items))throw new Error("Content-sync API returned a malformed event page.");let s=r;for(let o of a.items){if(!Number.isSafeInteger(o.sequence)||o.sequence<=s||o.sequence>t.throughSequence||!Number.isSafeInteger(o.postId)||o.postId<=0||!Number.isSafeInteger(o.blogId)||o.blogId<=0||!i.has(o.postType)||typeof o.operation!="string"||!o.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");s=o.sequence;}if(a.items.length>0&&a.nextSequence!==s)throw new Error("Content-sync API event cursor does not match the final returned event.");if(n.push(...a.items),!a.hasMore)break;if(a.nextSequence===r)throw new Error("Content-sync API event pagination did not advance.");r=a.nextSequence;}if(t.throughSequence>t.afterSequence&&n.at(-1)?.sequence!==t.throughSequence)throw new Error("Content-sync API omitted the claimed journal high-water event.");return n}async resolveImpactFamilies(t,n){let r=await this.post("impact",{families:t,includeSubsites:n});return Array.isArray(r.families)?r.families:[]}async releaseFingerprint(t=false){let n=await this.post("fingerprint",{includeSubsites:t}),r=String(n.fingerprint||"").trim();if(!/^[a-f0-9]{64}$/.test(r))throw new Error("WordPress returned an invalid release fingerprint.");return r}async establishBaseline(t){return this.post("baseline",t)}async acknowledge(t){return this.post("ack",t)}};function Ze(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Xe(e,t){return {...e,...t??{}}}function et(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function V(e,t){let n=String(t??e.deploymentTargetOverride??"").trim();if(!Ze(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:Xe(e.s3,i.s3),cloudFront:et(e.cloudFront,i.cloudFront)}}}var G=["/sitemap_index.xml","/wp-sitemap.xml","/sitemap.xml"];function be(e){let t=new Set;for(let n of [...e,...G]){let r=String(n||"").trim();r&&t.add(r);}return [...t]}function ve(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function it(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 Y(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,s={};if(typeof a.targetOrigin=="string"){let c=ve(a.targetOrigin);c&&(s.targetOrigin=c);}let o=Y(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(d=>String(d??"").trim()).filter(d=>d.length>0)),Object.keys(c).length>0&&(s.cloudFront=c);}t[i]=s;}return t}function X(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function ot(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(d=>String(d??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],u=Array.isArray(n.listingPaths)?[...new Set(n.listingPaths.map(d=>String(d??"").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 Pe(e){let t=e&&typeof e=="object"?e:{},n=Array.isArray(t.rules)?t.rules.map((r,i)=>ot(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 at(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 ee(e){let t=e&&typeof e=="object"?e:{},n=String(t.accountId??"").trim(),r=String(t.siteId??"").trim(),i=t.subscriber===true,a=at(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 d=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();d&&(o.virtualAssetBaseUrl=d),Object.keys(s).length>0&&(o.siteSettings=s);let g=X(t.subscriptionType);return g&&(o.subscriptionType=g),o}function ct(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function Ce(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function ut(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 Ae(e,t,n=5){let r=new URL(Ce(e)),i=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),a=r.protocol==="http:"?Ve:Se,s=ut(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}:{}},d=>{let g=[];d.on("data",p=>{g.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),d.on("error",c),d.on("end",()=>{let p=d.statusCode??0,h=d.headers.location;if(n>0&&h&&[301,302,303,307,308].includes(p)){o(Ae(new URL(h,r),{method:p===303?"GET":i},n-1));return}let y=new Headers;for(let[l,S]of Object.entries(d.headers))Array.isArray(S)?y.set(l,S.join(", ")):typeof S=="string"&&y.set(l,S);o(new Response(Buffer.concat(g),{status:p,statusText:d.statusMessage??"",headers:y}));});});u.on("error",c),u.end();})}function lt(e){let t=ee(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;ct(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 dt(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!lt(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 d=Ce(c);if(d.startsWith(t)){let g=(()=>{try{return new URL(d).pathname}catch{return d}})();try{let p=e.ignoreHttpsErrors&&d.startsWith("https://")?await Ae(c,u):await n(c,u);return r.push({path:g,status:p.status,ok:p.ok,error:null}),p}catch(p){throw r.push({path:g,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return n(c,u)}));let{getConfig:i}=await import('@smart-cloud/wpsuite-core'),a=await i("publisher"),s=a&&typeof a=="object"?a:null,o=X(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 gt(e,t){let n=st(t?.deploymentProfiles),r=String(t?.defaultDeploymentProfile??"").trim(),i=n[r]?r:"",a=X(t?.subscriptionType),s=ee(e.wpsuite);return {...e,scheduler:Pe(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 Z(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 pt(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?m.resolve(e):""}function we(e,t,n){let r=String(t||"").trim();return r&&m.isAbsolute(r)?m.resolve(r):m.resolve(e,Z(r,n))}async function te(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=ve(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=it(i.generated404RequestPath),i.sitemapPaths||=[...G],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=Y(i.extraReplacements),i.postCrawlCopyMap=Y(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=ee(i.wpsuite),i.scheduler=Pe(void 0),i.deploymentProfiles={},i.defaultDeploymentProfile="",i.deploymentTargetOverride=String(i.deploymentTargetOverride??"").trim();let a=await dt(i);t.onRemotePublisherConfigDiagnostic?.(a.diagnostic);let s=gt(i,a.remote),o=pt(),c=o?m.resolve(o,".."):"";return c?(s.outputDir=we(c,s.outputDir,"export"),s.logDir=we(c,s.logDir,"logs")):(m.isAbsolute(s.outputDir)||(s.outputDir=Z(s.outputDir,"export")),m.isAbsolute(s.logDir)||(s.logDir=Z(s.logDir,"logs"))),s}function Ie(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 ne(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 re(e,t){await Re(e.activeRules,{contractVersion:1,evaluatedAt:new Date().toISOString(),entries:t});}async function Re(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 mt(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 xe(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 Re(e.state,{...t,schemaVersion:1,updatedAt:new Date().toISOString()});}async function ke(e,t){let n=await mt(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 Oe(e){let t=String(e.command??"").trim();return (t==="publish"||t==="crawl")&&e.crawlMode==="incremental"||t==="content-sync"}var ie=new Map;async function Te(e){let t=m.resolve(e),n=ie.get(t);if(n!==void 0)return n;try{let r=JSON.parse(await P.readFile(t,"utf8")),i=String(r.version??"").trim();return ie.set(t,i),i}catch{return ie.set(t,""),""}}var M=class extends Error{request;step;constructor(t,n){super(`Stop requested during ${n}.`),this.name="StopRequestedError",this.request=t,this.step=n;}},wt="queue-mutation.lock",vt=5e3,Pt=3e4;function Ct(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 Le(e){return m.join(e,wt)}async function At(e){await new Promise(t=>setTimeout(t,e));}async function It(e){let t=Le(e),n=Date.now()+vt;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>Pt){await P.unlink(t).catch(()=>{});continue}if(Date.now()>=n)throw new Error("Timed out acquiring queue mutation lock.",{cause:r});await At(50);}}async function Rt(e){await P.unlink(Le(e)).catch(()=>{});}async function F(e,t){await It(e);try{return await t()}finally{await Rt(e);}}function Be(e){return m.join(e,"queue-runner-heartbeat.json")}function de(e){return m.join(e,"current-progress.json")}function se(e){return m.join(e,"scheduler-state.json")}function qt(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 Me(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,qt(i,"logs"))}function oe(e,t){return String(e||"").trim().replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/^-+|-+$/g,"")||t}function xt(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 Dt(e){return [xt(e.endedAt||e.startedAt),oe(e.command||"job","job"),oe(e.id||"job","job"),oe(e.status||"finished","finished")].join("-")}function kt(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 Tt(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 Et(e){return e.endsWith(".gz")?"application/gzip":e.endsWith(".jsonl")?"application/x-ndjson":e.endsWith(".json")?"application/json":"text/plain"}async function ae(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:Tt(n),originalFileName:n,storedFileName:r,compressed:true,compression:"gzip",contentType:Et(r),originalSize:a.size,storedSize:s.size}}async function W(e,t){let n=await Me(t),r=new Date().toISOString(),i=Dt(e),a=m.join(n,"archive",i),s=[],o=[];await P.mkdir(a,{recursive:true});try{let d=await P.readdir(n,{withFileTypes:!0});for(let g of d){if(!g.isFile()||!kt(g.name))continue;let p=await ae(m.join(n,g.name),a,g.name);o.push(p),s.push(p.storedFileName);}}catch(d){if((d&&typeof d=="object"&&"code"in d?String(d.code||""):"")!=="ENOENT")throw d}let c=de(t.runtimeDir);if(existsSync(c)){let d=await ae(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 g=m.join(t.runtimeDir,d);if(!existsSync(g))continue;let p=await ae(g,a,d);o.push(p),s.push(p.storedFileName);}let u=[...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:u,artifacts:o,job:e}),{archiveKey:i,archiveDir:a,archiveCreatedAt:r,archivedFiles:u,artifacts:o}}function Ft(e){return m.join(e,"audit-events.jsonl")}function ue(e){return m.join(e,"stop-request.json")}async function Ue(e){try{let t=await P.readFile(ue(e),"utf8"),n=JSON.parse(t);return n&&typeof n=="object"?n:null}catch{return null}}async function Ee(e,t){let n=await Ue(e);if(!n)return null;let r=String(n.targetJobId||"").trim();return r&&r!==t?null:n}async function B(e,t){if(!t){await P.unlink(ue(e)).catch(()=>{});return}let n=await Ue(e);if(!n)return;let r=String(n.targetJobId||"").trim();(!r||r===t)&&await P.unlink(ue(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(Ft(e),`${JSON.stringify(n)}
|
|
2
|
+
`,"utf8");}catch{}}async function C(e,t,n){let[r,i]=await Promise.all([Te(m.join(e.exporterDir,"package.json")),Te(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(Be(e.runtimeDir),a);}catch{}}async function E(e){try{await P.unlink(de(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,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(g=>String(g).trim()).filter(Boolean))]:[],u=Array.isArray(e.listingPaths)?[...new Set(e.listingPaths.map(g=>String(g).trim()).filter(Boolean))]:[],d=(n==="publish"||n==="crawl")&&e.crawlMode==="incremental"?"incremental":"full";return n==="url"&&!s||n==="content-sync"&&c.length===0?null:{id:i,enabled:a,command:n,intervalMinutes:r,...n==="publish"||n==="crawl"?{crawlMode:d}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&o?{deploymentProfile:o}:{},...s?{url:s}:{},...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 te(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 N(e,t){if(!t)throw new Error("Content-sync scheduler requires a valid publisher config.");let n=U({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=z(t.sourceOrigin,e.id,n),i=V(t,e.deploymentProfile),a=H({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}:${O({scopeFingerprint:r,targetFingerprint:a}).slice(0,32)}`}function Ot(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 Fe(e,t){let n=ne(e.runtimeDir);if(await re(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 d=N(u,t.publisherConfig),g=Ot(u,d,s,o);g&&c.push({ruleId:u.id,coalesceKey:d,consumerId:g});}await re(n,c);}async function Lt(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=U({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=z(o.sourceOrigin,n.id,c),d=V(o,n.deploymentProfile),g=H({sourceOrigin:o.sourceOrigin,targetOrigin:d.config.targetOrigin,deploymentProfile:d.name||"",s3Bucket:d.config.s3.bucket,s3Prefix:d.config.s3.prefix,cloudFrontDistributionId:d.config.cloudFront.distributionId,urlRewriteMode:d.config.urlRewriteMode,extraReplacements:d.profile?.extraReplacements??{}});if(s.scopeFingerprint!==u||s.targetFingerprint!==g||!s.releaseFingerprint)return {status:"baseline-required",reason:"The verified content-sync baseline does not match the active scope or deployment target. Run a successful full or incremental publish to establish a new baseline."};let p=new $({sourceOrigin:o.sourceOrigin,runtimeToken:String(o.wpsuite?.runtimeToken||""),ignoreHttpsErrors:o.ignoreHttpsErrors}),h=await p.releaseFingerprint(c.includeSubsites),y=he({wordpressFingerprint:h,targetFingerprint:g,sourceOrigin:o.sourceOrigin,sitemapPaths:be(o.sitemapPaths),blockedPathPrefixes:o.blockedPathPrefixes,assetPathPrefixes:o.assetPathPrefixes,noJavaScriptRenderPathPrefixes:o.noJavaScriptRenderPathPrefixes,urlRewriteMode:o.urlRewriteMode});if(s.releaseFingerprint!==y)return {status:"baseline-required",reason:"The installed WordPress release changed after the last verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline."};let l=await p.head({consumerId:s.consumerId,scopeFingerprint:s.scopeFingerprint,baselineId:s.baselineId,postTypes:c.postTypes,includeSubsites:c.includeSubsites}),S=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)&&(S=Math.max(S,Number(b.toSequence)));}return {status:l.headSequence>S?"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 $e(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 le(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 Bt(e,t,n){if(!t.allowSchedulerAutoEnqueue)return await Fe(e,null),0;let r=await Jt(e);if(await Fe(e,r),!r.enabled||r.rules.length===0)return 0;if(t.assertActiveSubscriptionForIdentity)try{await t.assertActiveSubscriptionForIdentity(r.wpsuite??null);}catch(l){let S=l instanceof Error?l.message:String(l);return console.warn(`[queue-runner] scheduler auto-enqueue skipped: ${S}`),0}let i=m.join(e.runtimeDir,"queue.json"),a=m.join(e.runtimeDir,"current-run.json"),s=Date.now(),o=0,c=[],u=[],d=[],g=new Map,[p,h,y]=await Promise.all([w(i,[]),w(a,null),w(se(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}})]);for(let l of r.rules){if(!l.enabled||l.command!=="content-sync")continue;let S=Math.floor(s/(l.intervalMinutes*60*1e3)),b=y.lastEvaluatedBucketByRuleId?.[l.id]??y.lastEnqueuedBucketByRuleId?.[l.id]??-1;if(S<=b)continue;let D=N(l,r.publisherConfig);le(p,{command:l.command,deploymentProfile:l.deploymentProfile,coalesceKey:D})||g.set(l.id,await Lt(e,r,l,D,h));}await F(e.runtimeDir,async()=>{let l=await w(i,[]),S=await w(a,null),b=await w(se(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}});b.lastEnqueuedBucketByRuleId??={},b.lastEvaluatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.lastCreatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.coalescedCountByRuleId??={};let D=[...l];S&&S.status==="running"&&D.unshift(S);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 J=f.command==="content-sync"?N(f,r.publisherConfig):"";b.lastEvaluatedBucketByRuleId[f.id]=x;let Qe=f.command==="content-sync"?l:D;if(le(Qe,{command:f.command,url:f.url,crawlMode:f.crawlMode,deploymentProfile:f.deploymentProfile,coalesceKey:J})){b.coalescedCountByRuleId[f.id]=(b.coalescedCountByRuleId[f.id]??0)+1,u.push(f);continue}if(f.command==="content-sync"){let K=g.get(f.id);if(!K||K.status!=="pending"){let pe=K??{status:"unavailable",reason:"Content-sync demand was not checked."};pe.status!=="current"&&d.push({rule:f,check:pe});continue}}let Q={id:randomUUID(),command:f.command,...f.command==="content-sync"?{ruleId:f.id,coalesceKey:J,attempt:0}:{},...(f.command==="publish"||f.command==="crawl")&&f.crawlMode?{crawlMode:f.crawlMode}:{},...(f.command==="publish"||f.command==="deploy"||f.command==="invalidate"||f.command==="content-sync")&&f.deploymentProfile?{deploymentProfile:f.deploymentProfile}:{},...f.url?{url:f.url}:{},enqueueSource:"scheduler",...r.wpsuite?{wpsuite:r.wpsuite}:{},status:"queued",createdAt:new Date().toISOString(),createdBy:0};l.push(Q),D.push(Q),c.push({job:Q,rule:f}),b.lastEnqueuedBucketByRuleId[f.id]=x,b.lastCreatedBucketByRuleId[f.id]=x,o+=1;}o>0&&await v(i,l),await v(se(e.runtimeDir),b);});for(let l of c){if(l.rule.command==="content-sync"){let S=g.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:S?.headSequence??0,committedSequence:S?.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 d){if(l.check.status==="baseline-required"){let S=N(l.rule,r.publisherConfig);await $e(e,l.rule.id,S,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 Mt(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 Ut(e){try{await P.unlink(e);}catch{}}function $t(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 Ee(t,s);if(u)throw new M(u,n)}let o=$t(e,n),c={...process.env,...a??{}};return c.STATIC_PUBLISHER_RUNTIME_DIR=t,i&&(c.PUBLISHER_CONFIG=i),await new Promise((u,d)=>{let g=null,p=null,h=null,y=null,l=false,S="",b=spawn(process.execPath,[o,...r],{cwd:e,env:c,stdio:["inherit","inherit","pipe"]});b.stderr?.on("data",A=>{S+=A.toString("utf8");});let D=()=>{p&&clearInterval(p),h&&clearInterval(h),y&&clearTimeout(y);},f=async()=>{if(!(!s||g||l)){l=true;try{let A=await Ee(t,s);if(!A)return;g=A,b.kill("SIGTERM"),y=setTimeout(()=>{b.kill("SIGKILL");},1e4);}finally{l=false;}}};s&&(p=setInterval(()=>{f();},1e3),h=setInterval(()=>{(async()=>{let A=Be(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(),d(A);}),b.on("close",(A,x)=>{D();let j=ye(S).trim();if(j&&process.stderr.write(`${j}
|
|
3
|
+
`),g){d(new M(g,n));return}if(A===0)u();else {let J=Ie(j);d(new Error(`${n} exited with code ${A??-1}${x?` (signal ${x})`:""}${J?`: ${J}`:""}`));}});}),0}function Wt(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 Nt(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 te(t.configPath).catch(()=>null),i=n(r?.wpsuite);if(i)return i;let a=n(e.wpsuite);return a||null}async function Qt(e,t){return await Nt(e,t)}function ge(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 Kt(e,t){let n=await w(de(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 Me(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 _t(e,t){if((e.command==="publish"||e.command==="crawl")&&t==="rewrite-text")return "rewrite-text"}async function zt(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=ge(i);return await v(t,[o,...s]),await v(n,null),o});r&&(await B(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 Ht(e,t,n){try{if((n.shouldEnforceSubscriptionOnJobExecution?.(e)??n.enforceSubscriptionOnJobExecution)&&n.assertActiveSubscriptionForIdentity){let o=await Qt(e,t);await n.assertActiveSubscriptionForIdentity(o);}let i=Wt(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 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),{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),{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),{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),{exitCode:0}):{exitCode:2,error:`Unsupported command: ${e.command}`}}catch(r){return r instanceof M?{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=Ct(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 Mt(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 zt(r);let s=await Bt(r,e),o=0;for(let c=0;c<r.maxJobs;c++){let u=await Zt(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 Ut(i);}}}async function je(e,t){if(t.command!=="content-sync"||!t.coalesceKey)return false;let n=ne(e.runtimeDir),r=await ke(n,t.coalesceKey),i=await xe(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 Vt(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=ge(t),c=_t(t,n);c?o.resumeFromStep=c:delete o.resumeFromStep,await v(r,[o,...s]),await v(i,null);});}async function Gt(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={...ge(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"),g=(await w(c,[])).filter(p=>p.id!==t.id);await v(c,[o,...g]),await v(u,null);}),t.coalesceKey){let c=m.join(e.runtimeDir,"content-sync-state.json"),d=(await w(c,{})).rules??{};d[t.coalesceKey]={...d[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:d});}return o}async function Yt(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,[]);le(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 Zt(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 g=await w(n,[]);if(!Array.isArray(g)||g.length===0)return null;let p=Date.parse(String(g[0]?.nextAttemptAt||""));if(Number.isFinite(p)&&p>Date.now())return null;let h={...g[0],status:"running",startedAt:a};return delete h.nextAttemptAt,await v(n,g.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 Ht(s,e,t),c=new Date().toISOString();if(o.stopped){let g=await Kt(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:g},y=false;p==="requeue"?await Vt(e,s,g):(await v(r,null),y=await je(e,s));try{let l=await W(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 B(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:g,logArchiveKey:h.logArchiveError||!h.logArchiveDir?"":m.basename(h.logArchiveDir),logArchiveDir:h.logArchiveDir||"",logArchiveCreatedAt:h.logArchiveCreatedAt||"",logArchiveFileCount:h.logArchiveFileCount??0,logArchiveError:h.logArchiveError||"",contentSyncAbandoned:y,journalCursorPreserved:y}}),"stopped"}if(s.command==="content-sync"&&o.exitCode!==0){if(/baseline (?:is missing or stale|required)|verified content-sync baseline|new baseline/i.test(o.error||"")){let y="The installed release no longer matches the verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline.";s.ruleId&&s.coalesceKey&&await $e(e,s.ruleId,s.coalesceKey,y);let l={...s,status:"failed",endedAt:c,exitCode:o.exitCode,error:y};await v(r,null),await E(e.runtimeDir),await je(e,s);try{let S=await W(l,e);l.logArchiveDir=S.archiveDir,l.logArchiveCreatedAt=S.archiveCreatedAt,l.logArchiveFileCount=S.archivedFiles.length;}catch(S){l.logArchiveError=S instanceof Error?S.message:String(S);}return await v(i,l),await B(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:y,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 Gt(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 y=await W(h,e);h.logArchiveDir=y.archiveDir,h.logArchiveCreatedAt=y.archiveCreatedAt,h.logArchiveFileCount=y.archivedFiles.length;}catch(y){h.logArchiveError=y instanceof Error?y.message:String(y);}return await v(i,h),await B(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 Yt(e,u),await B(e.runtimeDir,s.id);try{let g=await W(u,e);u.logArchiveDir=g.archiveDir,u.logArchiveCreatedAt=g.archiveCreatedAt,u.logArchiveFileCount=g.archivedFiles.length;}catch(g){u.logArchiveError=g instanceof Error?g.message:String(g);}await v(i,u);let d=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:d,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 en(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 tn={allowSchedulerAutoEnqueue:true,enforceSubscriptionOnJobExecution:false,shouldEnforceSubscriptionOnJobExecution:e=>Oe(e)||e.enqueueSource==="scheduler"||e.createdBy===0,assertActiveSubscriptionForIdentity:en},Ne=We(tn),nn=Ne,zn=Ne;process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href&&nn().catch(e=>{console.error(e),process.exit(1);});export{zn as default,nn as main,Ne as runQueueRunner};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smart-cloud/publisher-exporter",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.58",
|
|
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.",
|