@smart-cloud/publisher-exporter 1.1.41 → 1.1.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/content-sync.js +8 -8
- package/dist/crawl.js +3 -3
- package/dist/queue-runner.js +3 -2
- package/package.json +1 -1
package/dist/queue-runner.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import {pathToFileURL}from'url';import w from'fs/promises';import {existsSync,createReadStream,createWriteStream}from'fs';import g from'path';import {spawn}from'child_process';import {randomUUID,createHash}from'crypto';import De from'http';import se from'https';import {pipeline}from'stream/promises';import {createGzip}from'zlib';function re(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function U(e){return Array.isArray(e)?e.map(U):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>[t,U(n)])):e}function qe(e){return JSON.stringify(U(e))}function F(e){return createHash("sha256").update(qe(e)).digest("hex")}function xe(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 J(e){let t=re((e.postTypes??[]).map(i=>String(i).toLowerCase().replace(/[^a-z0-9_-]/g,"")));if(t.length===0)throw new Error("Content-sync requires at least one public post type.");let n=re((e.listingPaths??[]).map(i=>{let r=String(i).trim();if(!r)return "";let u=new URL(r,"https://content-sync.invalid/");if(u.origin!=="https://content-sync.invalid"&&/^https?:\/\//i.test(r))throw new Error("Content-sync listing routes must be same-site paths.");let o=`/${u.pathname.replace(/^\/+|\/+$/g,"")}`;return o==="/"?"/":`${o}/`}));return {postTypes:t,listingPaths:n,includePostTypeArchives:e.includePostTypeArchives!==false,includeTaxonomyArchives:e.includeTaxonomyArchives!==false,includeAuthorArchives:e.includeAuthorArchives===true,includeDateArchives:e.includeDateArchives===true,includePostsPage:e.includePostsPage!==false,includeSitemapChain:e.includeSitemapChain!==false}}function ie(e,t,n){return F({schemaVersion:1,sourceOrigin:xe(e),ruleId:t.trim(),scope:J(n)})}function oe(e){return F({schemaVersion:1,...e})}var Te="wp-json/smartcloud-static-publisher/v1/content-sync/",ke=25*1024*1024,D=class extends Error{status;code;constructor(t,n,i){super(i),this.name="ContentSyncApiError",this.status=t,this.code=n;}},O=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(`${Te}${t}`,n)}async post(t,n){let i=this.endpoint(t),r=Buffer.from(JSON.stringify(n),"utf8"),u=i.protocol==="https:"?se:De,o=i.protocol==="https:"?new se.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((s,a)=>{let c=u.request(i,{method:"POST",agent:o,headers:{"content-type":"application/json","content-length":String(r.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},l=>{let d=[],p=0;l.on("data",h=>{if(p+=h.byteLength,p>ke){l.destroy(new Error("Content-sync API response exceeded the size limit."));return}d.push(h);}),l.on("error",a),l.on("end",()=>{let h=Buffer.concat(d).toString("utf8"),m;try{m=h?JSON.parse(h):{};}catch{a(new D(l.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let b=l.statusCode??500;if(b<200||b>=300){a(new D(b,String(m.code??"request-failed"),String(m.message??`Content-sync API request failed (${b}).`)));return}s(m);});});c.on("timeout",()=>c.destroy(new Error(`Content-sync API request timed out: ${t}`))),c.on("error",a),c.end(r);})}async head(t){return this.post("head",t)}async unboundHead(t){return this.post("head",{postTypes:t})}async events(t){let n=[],i=t.afterSequence,r=new Set(t.postTypes);for(;i<t.throughSequence;){let u=await this.post("events",{...t,afterSequence:i,limit:250});if(u.fromSequence!==i||u.throughSequence!==t.throughSequence||u.nextSequence<i||u.nextSequence>t.throughSequence)throw new Error("Content-sync API returned an inconsistent event page.");if(!Array.isArray(u.items))throw new Error("Content-sync API returned a malformed event page.");let o=i;for(let s of u.items){if(!Number.isSafeInteger(s.sequence)||s.sequence<=o||s.sequence>t.throughSequence||!Number.isSafeInteger(s.postId)||s.postId<=0||!r.has(s.postType)||typeof s.operation!="string"||!s.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");o=s.sequence;}if(u.items.length>0&&u.nextSequence!==o)throw new Error("Content-sync API event cursor does not match the final returned event.");if(n.push(...u.items),!u.hasMore)break;if(u.nextSequence===i)throw new Error("Content-sync API event pagination did not advance.");i=u.nextSequence;}if(t.throughSequence>t.afterSequence&&n.at(-1)?.sequence!==t.throughSequence)throw new Error("Content-sync API omitted the claimed journal high-water event.");return n}async resolveImpactFamilies(t){let n=await this.post("impact",{families:t});return Array.isArray(n.families)?n.families:[]}async releaseFingerprint(){let t=await this.post("fingerprint",{}),n=String(t.fingerprint||"").trim();if(!/^[a-f0-9]{64}$/.test(n))throw new Error("WordPress returned an invalid release fingerprint.");return n}async establishBaseline(t){return this.post("baseline",t)}async acknowledge(t){return this.post("ack",t)}};function Ee(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Fe(e,t){return {...e,...t??{}}}function Je(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function ue(e,t){let n=String(t??e.deploymentTargetOverride??"").trim();if(!Ee(e)){if(n)throw new Error(`Deployment target "${n}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:e}}let i=String(n||e.defaultDeploymentProfile||"").trim();if(!i)return {name:null,profile:null,config:e};let r=e.deploymentProfiles?.[i];if(!r)throw new Error(`Unknown deployment profile "${i}". Check the linked WPSuite publisher configuration.`);return {name:i,profile:r,config:{...e,targetOrigin:r.targetOrigin??e.targetOrigin,s3:Fe(e.s3,r.s3),cloudFront:Je(e.cloudFront,r.cloudFront)}}}function ce(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function Me(e){let t=String(e??"").trim();if(!t)return "";let n=t;if(/^https?:\/\//i.test(n))try{n=new URL(n).pathname;}catch{return ""}if(n=n.split(/[?#]/,1)[0]?.replace(/\\/g,"/").trim()??"",!n)return "";let i=n.endsWith("/"),r=n.replace(/^\/+/,"").split("/").map(o=>o.replace(/[^A-Za-z0-9._-]/g,"")).filter(o=>o.length>0&&o!=="."&&o!=="..");if(r.length===0)return "";let u=`/${r.join("/")}`;return i&&g.extname(u)===""&&(u+="/"),u==="/"?"":u}function L(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t.trim(),String(n??"")]).filter(([t])=>t.length>0))}function Ue(e){if(!e||typeof e!="object")return {};let t={};for(let[n,i]of Object.entries(e)){let r=n.trim();if(!r||!i||typeof i!="object")continue;let u=i,o={};if(typeof u.targetOrigin=="string"){let a=ce(u.targetOrigin);a&&(o.targetOrigin=a);}let s=L(u.extraReplacements);if(Object.keys(s).length>0&&(o.extraReplacements=s),u.s3&&typeof u.s3=="object"){let a={},c=u.s3;typeof c.bucket=="string"&&(a.bucket=c.bucket.trim()),typeof c.prefix=="string"&&(a.prefix=c.prefix.trim()),typeof c.region=="string"&&(a.region=c.region.trim()),typeof c.htmlCacheControl=="string"&&(a.htmlCacheControl=c.htmlCacheControl.trim()),typeof c.assetCacheControl=="string"&&(a.assetCacheControl=c.assetCacheControl.trim()),Object.keys(a).length>0&&(o.s3=a);}if(u.cloudFront&&typeof u.cloudFront=="object"){let a={},c=u.cloudFront;typeof c.distributionId=="string"&&(a.distributionId=c.distributionId.trim()),Array.isArray(c.invalidationPaths)&&(a.invalidationPaths=c.invalidationPaths.map(l=>String(l??"").trim()).filter(l=>l.length>0)),Object.keys(a).length>0&&(o.cloudFront=a);}t[r]=o;}return t}function Q(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function Le(e,t){if(!e||typeof e!="object")return null;let n=e,i=n.command;if(i!=="publish"&&i!=="crawl"&&i!=="deploy"&&i!=="invalidate"&&i!=="retry-timeouts"&&i!=="url"&&i!=="content-sync")return null;let r=Number.parseInt(String(n.intervalMinutes??"0"),10);if(!Number.isFinite(r)||r<1)return null;let u=String(n.id??`${i}-${t+1}`).trim();if(!u)return null;let o=String(n.deploymentProfile??"").trim(),s=String(n.url??"").trim(),a=Array.isArray(n.postTypes)?[...new Set(n.postTypes.map(l=>String(l??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],c=Array.isArray(n.listingPaths)?[...new Set(n.listingPaths.map(l=>String(l??"").trim()).filter(Boolean))]:[];return i==="content-sync"&&a.length===0?null:{id:u,enabled:n.enabled!==false,command:i,intervalMinutes:r,...i==="publish"||i==="crawl"?{crawlMode:n.crawlMode==="incremental"?"incremental":"full"}:{},...(i==="publish"||i==="deploy"||i==="invalidate"||i==="content-sync")&&o?{deploymentProfile:o}:{},...s?{url:s}:{},...a.length>0?{postTypes:a}:{},...c.length>0?{listingPaths:c}:{},...typeof n.includePostTypeArchives=="boolean"?{includePostTypeArchives:n.includePostTypeArchives}:{},...typeof n.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:n.includeTaxonomyArchives}:{},...typeof n.includeAuthorArchives=="boolean"?{includeAuthorArchives:n.includeAuthorArchives}:{},...typeof n.includeDateArchives=="boolean"?{includeDateArchives:n.includeDateArchives}:{},...typeof n.includePostsPage=="boolean"?{includePostsPage:n.includePostsPage}:{},...typeof n.includeSitemapChain=="boolean"?{includeSitemapChain:n.includeSitemapChain}:{}}}function le(e){let t=e&&typeof e=="object"?e:{},n=Array.isArray(t.rules)?t.rules.map((i,r)=>Le(i,r)).filter(i=>!!i):[];return {enabled:t.enabled===true,timezone:typeof t.timezone=="string"&&t.timezone.trim()!==""?t.timezone.trim():"UTC",rules:n}}function We(e){if(!e||typeof e!="object")return;let t=e,n={};for(let r of ["accountId","siteId"]){let u=t[r];typeof u=="string"&&u.trim()!==""&&(n[r]=u.trim());}let i=Number(t.lastUpdate??0);return Number.isFinite(i)&&i>0&&(n.lastUpdate=Math.floor(i)),t.subscriber===true&&(n.subscriber=true),Object.keys(n).length>0?n:void 0}function $(e){let t=e&&typeof e=="object"?e:{},n=String(t.accountId??"").trim(),i=String(t.siteId??"").trim(),r=t.subscriber===true,u=We(t.siteSettings),o={...u??{},...n?{accountId:u?.accountId??n}:{},...i?{siteId:u?.siteId??i}:{},...u?.subscriber===true||r?{subscriber:true}:{}},s={},a=String(t.apiBase??"").trim();a&&(s.apiBase=a);let c=String(t.runtimeToken??t.nonce??"").trim();c&&(s.runtimeToken=c);let l=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();l&&(s.virtualAssetBaseUrl=l),Object.keys(o).length>0&&(s.siteSettings=o);let d=Q(t.subscriptionType);return d&&(s.subscriptionType=d),s}function Qe(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function de(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function $e(e,t){let n=new Headers(e instanceof Request?e.headers:void 0);if(t?.headers)for(let[i,r]of new Headers(t.headers).entries())n.set(i,r);return n}async function me(e,t,n=5){let i=new URL(de(e)),r=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),u=i.protocol==="http:"?De:se,o=$e(e,t);return await new Promise((s,a)=>{let c=u.request(i,{method:r,headers:Object.fromEntries(o.entries()),...i.protocol==="https:"?{rejectUnauthorized:false}:{}},l=>{let d=[];l.on("data",p=>{d.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),l.on("error",a),l.on("end",()=>{let p=l.statusCode??0,h=l.headers.location;if(n>0&&h&&[301,302,303,307,308].includes(p)){s(me(new URL(h,i),{method:p===303?"GET":r},n-1));return}let m=new Headers;for(let[b,y]of Object.entries(l.headers))Array.isArray(y)?m.set(b,y.join(", ")):typeof y=="string"&&m.set(b,y);s(new Response(Buffer.concat(d),{status:p,statusText:l.statusMessage??"",headers:m}));});});c.on("error",a),c.end();})}function Ne(e){let t=$(e.wpsuite),n={...t.siteSettings??{},...t.siteSettings?.subscriber===true||t.subscriptionType==="PROFESSIONAL"||t.subscriptionType==="AGENCY"?{subscriber:true}:{}},i=String(n.accountId??"").trim(),r=String(n.siteId??"").trim(),u=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();if(e.wpsuite={...t.apiBase?{apiBase:t.apiBase}:{},...t.runtimeToken?{runtimeToken:t.runtimeToken}:{},...u?{virtualAssetBaseUrl:u}:{},...Object.keys(n).length>0?{siteSettings:n}:{},...t.subscriptionType?{subscriptionType:t.subscriptionType}:{}},!i||!r||!u)return false;Qe(e.sourceOrigin);let o=globalThis,a={...o.WpSuite??{},siteSettings:n,uploadUrl:u};return t.apiBase&&(a.apiBase=t.apiBase),o.WpSuite=a,true}async function ze(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!Ne(e)){let r=e.wpsuite?.siteSettings,u=[String(r?.accountId??"").trim()?"":"accountId",String(r?.siteId??"").trim()?"":"siteId",t?"":"virtualAssetBaseUrl"].filter(Boolean);return {remote:null,diagnostic:{status:"invalid-runtime",virtualAssetBaseUrl:t,subscriptionType:null,error:`Missing WP Suite runtime field(s): ${u.join(", ")}.`,requests:[]}}}let n=globalThis.fetch,i=[];try{typeof n=="function"&&/^https?:\/\//i.test(t)&&(globalThis.fetch=(async(a,c)=>{let l=de(a);if(l.startsWith(t)){let d=(()=>{try{return new URL(l).pathname}catch{return l}})();try{let p=e.ignoreHttpsErrors&&l.startsWith("https://")?await me(a,c):await n(a,c);return i.push({path:d,status:p.status,ok:p.ok,error:null}),p}catch(p){throw i.push({path:d,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return n(a,c)}));let{getConfig:r}=await import('@smart-cloud/wpsuite-core'),u=await r("publisher"),o=u&&typeof u=="object"?u:null,s=Q(o?.subscriptionType);return {remote:o,diagnostic:{status:o?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:s??null,error:o?null:"WP Suite publisher config loader returned no configuration.",requests:i}}}catch(r){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:t,subscriptionType:null,error:r instanceof Error?r.message:String(r),requests:i}}}finally{typeof n=="function"&&(globalThis.fetch=n);}}function Ke(e,t){let n=Ue(t?.deploymentProfiles),i=String(t?.defaultDeploymentProfile??"").trim(),r=n[i]?i:"",u=Q(t?.subscriptionType),o=$(e.wpsuite);return {...e,scheduler:le(t?.scheduler),deploymentProfiles:n,defaultDeploymentProfile:r,...u?{subscriptionType:u}:{},wpsuite:{...o,...o.siteSettings||u?{siteSettings:{...o.siteSettings??{},...o.siteSettings?.subscriber===true||u==="PROFESSIONAL"||u==="AGENCY"?{subscriber:true}:{}}}:{},...u?{subscriptionType:u}:{}}}}function W(e,t){let i=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!i)return t;let r=i.split("/").map(u=>u.trim()).filter(u=>u.length>0&&u!=="."&&u!=="..");return r.length>0?r.join("/"):t}function _e(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?g.resolve(e):""}function ae(e,t,n){let i=String(t||"").trim();return i&&g.isAbsolute(i)?g.resolve(i):g.resolve(e,W(i,n))}async function N(e,t={}){let n=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",i=await w.readFile(n,"utf8"),r=JSON.parse(i);r.sourceOrigin=r.sourceOrigin.replace(/\/$/,""),r.targetOrigin=ce(r.targetOrigin),r.ignoreHttpsErrors??=false,r.outputDir=String(r.outputDir||"export").trim()||"export",r.urlRewriteMode||=r.targetOrigin==="."?"relative":"absolute",r.noJavaScriptRenderPathPrefixes||=[],r.seedPaths||=[],r.generated404RequestPath=Me(r.generated404RequestPath),r.sitemapPaths||=["/sitemap_index.xml","/sitemap.xml"],r.allowedAssetHosts||=[],r.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],r.blockedPathPrefixes||=[],r.blockedSearchFragments||=[],r.concurrency||=1,r.maxPages||=0,r.extraReplacements=L(r.extraReplacements),r.postCrawlCopyMap=L(r.postCrawlCopyMap),r.logDir=String(r.logDir||"logs").trim()||"logs",r.verbose??=false,r.logLevel||=r.verbose?"debug":"info",r.s3SyncMode||="sdk-upload-delete",r.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},r.readiness.timeoutMs??=1500,r.readiness.fallbackWaitMs??=1500,r.viewport||={width:1440,height:1200},r.navigationTimeoutMs||=3e4,r.assetDownloadConcurrency=Number(r.assetDownloadConcurrency)>0?Number(r.assetDownloadConcurrency):r.concurrency,r.rewriteConcurrency=Number(r.rewriteConcurrency)>0?Number(r.rewriteConcurrency):r.assetDownloadConcurrency,r.wpsuite=$(r.wpsuite),r.scheduler=le(void 0),r.deploymentProfiles={},r.defaultDeploymentProfile="",r.deploymentTargetOverride=String(r.deploymentTargetOverride??"").trim();let u=await ze(r);t.onRemotePublisherConfigDiagnostic?.(u.diagnostic);let o=Ke(r,u.remote),s=_e(),a=s?g.resolve(s,".."):"";return a?(o.outputDir=ae(a,o.outputDir,"export"),o.logDir=ae(a,o.logDir,"logs")):(g.isAbsolute(o.outputDir)||(o.outputDir=W(o.outputDir,"export")),g.isAbsolute(o.logDir)||(o.logDir=W(o.logDir,"logs"))),o}function ye(e){let t=String(e.command??"").trim();return (t==="publish"||t==="crawl")&&e.crawlMode==="incremental"||t==="content-sync"}var z=new Map;async function pe(e){let t=g.resolve(e),n=z.get(t);if(n!==void 0)return n;try{let i=JSON.parse(await w.readFile(t,"utf8")),r=String(i.version??"").trim();return z.set(t,r),r}catch{return z.set(t,""),""}}var E=class extends Error{request;step;constructor(t,n){super(`Stop requested during ${n}.`),this.name="StopRequestedError",this.request=t,this.step=n;}},Xe="queue-mutation.lock",et=5e3,tt=3e4;function nt(e){let t=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",n=process.env.STATIC_PUBLISHER_EXPORTER_DIR||process.cwd(),i=process.env.PUBLISHER_CONFIG||"",r=1;for(let s=0;s<e.length;s++){let a=e[s];a==="--runtime-dir"?t=e[++s]||"":a.startsWith("--runtime-dir=")?t=a.slice(14):a==="--exporter-dir"?n=e[++s]||n:a.startsWith("--exporter-dir=")?n=a.slice(15):a==="--config"?i=e[++s]||"":a.startsWith("--config=")?i=a.slice(9):a==="--max-jobs"?r=Number.parseInt(e[++s]||"1",10):a.startsWith("--max-jobs=")&&(r=Number.parseInt(a.slice(11),10));}if(!t)throw new Error("Missing runtime dir. Use --runtime-dir or STATIC_PUBLISHER_RUNTIME_DIR.");let u=g.resolve(t),o=i?g.resolve(i):g.join(u,"config.json");return {runtimeDir:u,exporterDir:g.resolve(n),configPath:o,maxJobs:Number.isFinite(r)&&r>0?r:1}}async function S(e,t){try{let n=await w.readFile(e,"utf8");return JSON.parse(n)}catch(n){if((n&&typeof n=="object"&&"code"in n?String(n.code||""):"")!=="ENOENT"){let r=n instanceof Error?n.message:String(n);console.warn(`[queue-runner] failed to read JSON ${e}: ${r}`);}return t}}async function P(e,t){await w.mkdir(g.dirname(e),{recursive:true}),await w.writeFile(e,JSON.stringify(t,null,2),"utf8");}function be(e){return g.join(e,Xe)}async function rt(e){await new Promise(t=>setTimeout(t,e));}async function it(e){let t=be(e),n=Date.now()+et;for(;;)try{await w.mkdir(g.dirname(t),{recursive:!0}),await w.writeFile(t,JSON.stringify({pid:process.pid,createdAt:new Date().toISOString()},null,2),{encoding:"utf8",flag:"wx"});return}catch(i){if((i&&typeof i=="object"&&"code"in i?String(i.code||""):"")!=="EEXIST")throw i;let u=await w.stat(t).catch(()=>null);if(u&&Date.now()-u.mtimeMs>tt){await w.unlink(t).catch(()=>{});continue}if(Date.now()>=n)throw new Error("Timed out acquiring queue mutation lock.",{cause:i});await rt(50);}}async function ot(e){await w.unlink(be(e)).catch(()=>{});}async function T(e,t){await it(e);try{return await t()}finally{await ot(e);}}function Se(e){return g.join(e,"queue-runner-heartbeat.json")}function X(e){return g.join(e,"current-progress.json")}function K(e){return g.join(e,"scheduler-state.json")}function st(e,t){let i=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!i)return t;let r=i.split("/").map(u=>u.trim()).filter(u=>u.length>0&&u!=="."&&u!=="..");return r.length>0?r.join("/"):t}async function we(e){let t=await S(e.configPath||"",null),n=g.resolve(e.runtimeDir,".."),r=(typeof t?.logDir=="string"?t.logDir:"").trim();return r&&g.isAbsolute(r)?g.resolve(r):g.resolve(n,st(r,"logs"))}function _(e,t){return String(e||"").trim().replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/^-+|-+$/g,"")||t}function ut(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 at(e){return [ut(e.endedAt||e.startedAt),_(e.command||"job","job"),_(e.id||"job","job"),_(e.status||"finished","finished")].join("-")}function ct(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 lt(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 dt(e){return e.endsWith(".gz")?"application/gzip":e.endsWith(".jsonl")?"application/x-ndjson":e.endsWith(".json")?"application/json":"text/plain"}async function H(e,t,n){let i=`${n}.gz`,r=g.join(t,i);await pipeline(createReadStream(e),createGzip({level:9}),createWriteStream(r));let[u,o]=await Promise.all([w.stat(e),w.stat(r)]);return {role:lt(n),originalFileName:n,storedFileName:i,compressed:true,compression:"gzip",contentType:dt(i),originalSize:u.size,storedSize:o.size}}async function G(e,t){let n=await we(t),i=new Date().toISOString(),r=at(e),u=g.join(n,"archive",r),o=[],s=[];await w.mkdir(u,{recursive:true});try{let l=await w.readdir(n,{withFileTypes:!0});for(let d of l){if(!d.isFile()||!ct(d.name))continue;let p=await H(g.join(n,d.name),u,d.name);s.push(p),o.push(p.storedFileName);}}catch(l){if((l&&typeof l=="object"&&"code"in l?String(l.code||""):"")!=="ENOENT")throw l}let a=X(t.runtimeDir);if(existsSync(a)){let l=await H(a,u,"current-progress.json");s.push(l),o.push(l.storedFileName);}if(e.command==="content-sync")for(let l 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 d=g.join(t.runtimeDir,l);if(!existsSync(d))continue;let p=await H(d,u,l);s.push(p),o.push(p.storedFileName);}let c=[...o,"job.json"];return await P(g.join(u,"job.json"),{manifestVersion:1,archivedAt:i,archiveKey:r,archiveDir:u,logDir:n,runtimeDir:t.runtimeDir,exporterDir:t.exporterDir,configPath:t.configPath||"",archivedFiles:c,artifacts:s,job:e}),{archiveKey:r,archiveDir:u,archiveCreatedAt:i,archivedFiles:c,artifacts:s}}function mt(e){return g.join(e,"audit-events.jsonl")}function Y(e){return g.join(e,"stop-request.json")}async function Pe(e){try{let t=await w.readFile(Y(e),"utf8"),n=JSON.parse(t);return n&&typeof n=="object"?n:null}catch{return null}}async function ge(e,t){let n=await Pe(e);if(!n)return null;let i=String(n.targetJobId||"").trim();return i&&i!==t?null:n}async function j(e,t){if(!t){await w.unlink(Y(e)).catch(()=>{});return}let n=await Pe(e);if(!n)return;let i=String(n.targetJobId||"").trim();(!i||i===t)&&await w.unlink(Y(e)).catch(()=>{});}async function A(e,t){try{let n={occurredAt:t.occurredAt||new Date().toISOString(),status:t.status||"info",actorSource:t.actorSource||"queue-runner",...t};await w.mkdir(e,{recursive:!0}),await w.appendFile(mt(e),`${JSON.stringify(n)}
|
|
2
|
-
`,"utf8");}catch{}}async function v(e,t,n){let[i,r]=await Promise.all([pe(g.join(e.exporterDir,"package.json")),pe(g.join(e.exporterDir,"node_modules","@smart-cloud","wpsuite-core","package.json"))]),u={checkedAt:new Date().toISOString(),status:t,pid:process.pid,nodePath:process.execPath,nodeVersion:process.version,exporterVersion:i,wpsuiteCoreVersion:r,runtimeDir:e.runtimeDir,exporterDir:e.exporterDir,...n??{}};try{await P(Se(e.runtimeDir),u);}catch{}}async function k(e){try{await w.unlink(X(e));}catch{}}function pt(e,t){let n=e.command;if(n!=="publish"&&n!=="crawl"&&n!=="deploy"&&n!=="invalidate"&&n!=="retry-timeouts"&&n!=="url"&&n!=="content-sync")return null;let i=Number.parseInt(String(e.intervalMinutes??"0"),10);if(!Number.isFinite(i)||i<1)return null;let r=(e.id||`${n}-${t+1}`).trim();if(!r)return null;let u=e.enabled!==false,o=typeof e.url=="string"?e.url.trim():"",s=typeof e.deploymentProfile=="string"?e.deploymentProfile.trim():"",a=Array.isArray(e.postTypes)?[...new Set(e.postTypes.map(d=>String(d).trim()).filter(Boolean))]:[],c=Array.isArray(e.listingPaths)?[...new Set(e.listingPaths.map(d=>String(d).trim()).filter(Boolean))]:[],l=(n==="publish"||n==="crawl")&&e.crawlMode==="incremental"?"incremental":"full";return n==="url"&&!o||n==="content-sync"&&a.length===0?null:{id:r,enabled:u,command:n,intervalMinutes:i,...n==="publish"||n==="crawl"?{crawlMode:l}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&s?{deploymentProfile:s}:{},...o?{url:o}:{},...a.length>0?{postTypes:a}:{},...c.length>0?{listingPaths:c}:{},...typeof e.includePostTypeArchives=="boolean"?{includePostTypeArchives:e.includePostTypeArchives}:{},...typeof e.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:e.includeTaxonomyArchives}:{},...typeof e.includeAuthorArchives=="boolean"?{includeAuthorArchives:e.includeAuthorArchives}:{},...typeof e.includeDateArchives=="boolean"?{includeDateArchives:e.includeDateArchives}:{},...typeof e.includePostsPage=="boolean"?{includePostsPage:e.includePostsPage}:{},...typeof e.includeSitemapChain=="boolean"?{includeSitemapChain:e.includeSitemapChain}:{}}}async function gt(e){let t=await N(e.configPath).catch(()=>null),n=t?.scheduler,i=Array.isArray(n?.rules)?n.rules.map((r,u)=>pt(r,u)).filter(r=>!!r):[];return {enabled:!!n?.enabled,timezone:typeof n?.timezone=="string"&&n.timezone.trim()?n.timezone.trim():"UTC",rules:i,wpsuite:t?.wpsuite,publisherConfig:t}}function he(e,t){if(!t)throw new Error("Content-sync scheduler requires a valid publisher config.");let n=J({postTypes:e.postTypes??[],listingPaths:e.listingPaths??[],includePostTypeArchives:e.includePostTypeArchives,includeTaxonomyArchives:e.includeTaxonomyArchives,includeAuthorArchives:e.includeAuthorArchives,includeDateArchives:e.includeDateArchives,includePostsPage:e.includePostsPage,includeSitemapChain:e.includeSitemapChain}),i=ie(t.sourceOrigin,e.id,n),r=ue(t,e.deploymentProfile),u=oe({sourceOrigin:t.sourceOrigin,targetOrigin:r.config.targetOrigin,deploymentProfile:r.name||"",s3Bucket:r.config.s3.bucket,s3Prefix:r.config.s3.prefix,cloudFrontDistributionId:r.config.cloudFront.distributionId,urlRewriteMode:r.config.urlRewriteMode,extraReplacements:r.profile?.extraReplacements??{}});return `content-sync:${e.id}:${F({scopeFingerprint:i,targetFingerprint:u}).slice(0,32)}`}async function ht(e,t,n,i,r){let o=(await S(g.join(e.runtimeDir,"content-sync-baseline.json"),{})).entries?.[i];if(!o?.consumerId||!o.scopeFingerprint||!o.baselineId)return {status:"baseline-required",reason:"No verified normal-release baseline exists for this rule scope."};try{let s=t.publisherConfig;if(!s)throw new Error("Publisher configuration is unavailable.");let a=J({postTypes:n.postTypes??[],listingPaths:n.listingPaths??[],includePostTypeArchives:n.includePostTypeArchives,includeTaxonomyArchives:n.includeTaxonomyArchives,includeAuthorArchives:n.includeAuthorArchives,includeDateArchives:n.includeDateArchives,includePostsPage:n.includePostsPage,includeSitemapChain:n.includeSitemapChain}),l=await new O({sourceOrigin:s.sourceOrigin,runtimeToken:String(s.wpsuite?.runtimeToken||""),ignoreHttpsErrors:s.ignoreHttpsErrors}).head({consumerId:o.consumerId,scopeFingerprint:o.scopeFingerprint,baselineId:o.baselineId,postTypes:a.postTypes}),d=l.committedSequence;if(r?.command==="content-sync"&&r.coalesceKey===i){let p=await S(g.join(e.runtimeDir,"content-sync-current.json"),null);p?.coalesceKey===i&&Number.isSafeInteger(p.toSequence)&&(d=Math.max(d,Number(p.toSequence)));}return {status:l.headSequence>d?"pending":"current",headSequence:l.headSequence,committedSequence:l.committedSequence}}catch(s){return s instanceof D&&s.code==="baseline-required"?{status:"baseline-required",reason:s.message}:{status:"unavailable",reason:s instanceof Error?s.message:String(s)}}}function Z(e,t){let n=(t.url||"").trim(),i=t.crawlMode||"full",r=(t.deploymentProfile||"").trim(),u=(t.coalesceKey||"").trim();return e.some(o=>o.command===t.command&&(t.command!=="content-sync"||(o.coalesceKey||"").trim()===u)&&(o.url||"").trim()===n&&(o.crawlMode||"full")===i&&(o.deploymentProfile||"").trim()===r&&(o.status===void 0||o.status==="queued"||o.status==="retry-wait"||o.status==="running"))}async function ft(e,t){if(!t.allowSchedulerAutoEnqueue)return 0;let n=await gt(e);if(!n.enabled||n.rules.length===0)return 0;if(t.assertActiveSubscriptionForIdentity)try{await t.assertActiveSubscriptionForIdentity(n.wpsuite??null);}catch(m){let b=m instanceof Error?m.message:String(m);return console.warn(`[queue-runner] scheduler auto-enqueue skipped: ${b}`),0}let i=g.join(e.runtimeDir,"queue.json"),r=g.join(e.runtimeDir,"current-run.json"),u=Date.now(),o=0,s=[],a=[],c=[],l=new Map,[d,p,h]=await Promise.all([S(i,[]),S(r,null),S(K(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}})]);for(let m of n.rules){if(!m.enabled||m.command!=="content-sync")continue;let b=Math.floor(u/(m.intervalMinutes*60*1e3)),y=h.lastEvaluatedBucketByRuleId?.[m.id]??h.lastEnqueuedBucketByRuleId?.[m.id]??-1;if(b<=y)continue;let q=he(m,n.publisherConfig);Z(d,{command:m.command,deploymentProfile:m.deploymentProfile,coalesceKey:q})||l.set(m.id,await ht(e,n,m,q,p));}await T(e.runtimeDir,async()=>{let m=await S(i,[]),b=await S(r,null),y=await S(K(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}});y.lastEnqueuedBucketByRuleId??={},y.lastEvaluatedBucketByRuleId??={...y.lastEnqueuedBucketByRuleId},y.lastCreatedBucketByRuleId??={...y.lastEnqueuedBucketByRuleId},y.coalescedCountByRuleId??={};let q=[...m];b&&b.status==="running"&&q.unshift(b);for(let f of n.rules){if(!f.enabled)continue;let C=f.intervalMinutes*60*1e3,R=Math.floor(u/C),Ae=y.lastEvaluatedBucketByRuleId[f.id]??y.lastEnqueuedBucketByRuleId[f.id]??-1;if(R<=Ae)continue;let te=f.command==="content-sync"?he(f,n.publisherConfig):"";y.lastEvaluatedBucketByRuleId[f.id]=R;let Ie=f.command==="content-sync"?m:q;if(Z(Ie,{command:f.command,url:f.url,crawlMode:f.crawlMode,deploymentProfile:f.deploymentProfile,coalesceKey:te})){y.coalescedCountByRuleId[f.id]=(y.coalescedCountByRuleId[f.id]??0)+1,a.push(f);continue}if(f.command==="content-sync"){let M=l.get(f.id);if(!M||M.status!=="pending"){let ne=M??{status:"unavailable",reason:"Content-sync demand was not checked."};ne.status!=="current"&&c.push({rule:f,check:ne});continue}}let B={id:randomUUID(),command:f.command,...f.command==="content-sync"?{ruleId:f.id,coalesceKey:te,attempt:0}:{},...(f.command==="publish"||f.command==="crawl")&&f.crawlMode?{crawlMode:f.crawlMode}:{},...(f.command==="publish"||f.command==="deploy"||f.command==="invalidate"||f.command==="content-sync")&&f.deploymentProfile?{deploymentProfile:f.deploymentProfile}:{},...f.url?{url:f.url}:{},enqueueSource:"scheduler",...n.wpsuite?{wpsuite:n.wpsuite}:{},status:"queued",createdAt:new Date().toISOString(),createdBy:0};m.push(B),q.push(B),s.push({job:B,rule:f}),y.lastEnqueuedBucketByRuleId[f.id]=R,y.lastCreatedBucketByRuleId[f.id]=R,o+=1;}o>0&&await P(i,m),await P(K(e.runtimeDir),y);});for(let m of s){if(m.rule.command==="content-sync"){let b=l.get(m.rule.id);await A(e.runtimeDir,{eventType:"content-sync-demand-detected",status:"queued",actorSource:"queue-runner-scheduler",jobId:m.job.id,command:m.job.command,message:"Pending journal changes require a content-sync job.",details:{ruleId:m.rule.id,headSequence:b?.headSequence??0,committedSequence:b?.committedSequence??0}});}await A(e.runtimeDir,{eventType:"job-created",status:"queued",actorSource:"queue-runner-scheduler",jobId:m.job.id,command:m.job.command,message:"Scheduler auto-enqueued a job.",details:{ruleId:m.rule.id,intervalMinutes:m.rule.intervalMinutes,timezone:n.timezone,deploymentProfile:m.rule.deploymentProfile||"",url:m.rule.url||""}});}for(let m of a)await A(e.runtimeDir,{eventType:"content-sync-coalesced",status:"queued",actorSource:"queue-runner-scheduler",command:m.command,message:"Scheduler demand was coalesced into an existing content-sync job.",details:{ruleId:m.id}});for(let m of c)await A(e.runtimeDir,{eventType:m.check.status==="baseline-required"?"content-sync-baseline-required":"content-sync-demand-check-failed",status:m.check.status==="unavailable"?"failed":"info",actorSource:"queue-runner-scheduler",command:"content-sync",message:m.check.reason||"The content-sync consumer is already at the current journal head.",details:{ruleId:m.rule.id,headSequence:m.check.headSequence??0,committedSequence:m.check.committedSequence??0}});return o}async function yt(e){let t={pid:process.pid,startedAt:new Date().toISOString()};await w.writeFile(e,JSON.stringify(t,null,2),{encoding:"utf8",flag:"wx"});}async function bt(e){try{await w.unlink(e);}catch{}}function St(e,t){let n=[g.join(e,"dist",`${t}.js`),g.join(e,`${t}.js`)];for(let i of n)if(existsSync(i))return i;throw new Error(`Cannot find ${t}.js in ${g.join(e,"dist")} or ${e}`)}async function I(e,t,n,i,r,u,o){if(o){let c=await ge(t,o);if(c)throw new E(c,n)}let s=St(e,n),a={...process.env,...u??{}};return a.STATIC_PUBLISHER_RUNTIME_DIR=t,r&&(a.PUBLISHER_CONFIG=r),await new Promise((c,l)=>{let d=null,p=null,h=null,m=null,b=false,y=spawn(process.execPath,[s,...i],{cwd:e,env:a,stdio:"inherit"}),q=()=>{p&&clearInterval(p),h&&clearInterval(h),m&&clearTimeout(m);},f=async()=>{if(!(!o||d||b)){b=true;try{let C=await ge(t,o);if(!C)return;d=C,y.kill("SIGTERM"),m=setTimeout(()=>{y.kill("SIGKILL");},1e4);}finally{b=false;}}};o&&(p=setInterval(()=>{f();},1e3),h=setInterval(()=>{(async()=>{let C=Se(t),R=await S(C,{});await P(C,{...R,checkedAt:new Date().toISOString(),status:"running",currentJobId:o||R.currentJobId||"",currentStep:n});})().catch(()=>{});},15e3)),y.on("error",C=>{q(),l(C);}),y.on("close",(C,R)=>{if(q(),d){l(new E(d,n));return}C===0?c():l(new Error(`${n} exited with code ${C??-1}${R?` (signal ${R})`:""}`));});}),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 Pt(e,t){let n=o=>{if(!o||typeof o!="object")return null;let s=o,a={...s.siteSettings??{},...String(s.accountId??"").trim()&&!s.siteSettings?.accountId?{accountId:String(s.accountId).trim()}:{},...String(s.siteId??"").trim()&&!s.siteSettings?.siteId?{siteId:String(s.siteId).trim()}:{},...s.siteSettings?.subscriber===true||s.subscriber===true?{subscriber:true}:{}},c={...String(s.apiBase??"").trim()?{apiBase:String(s.apiBase).trim()}:{},...String(s.runtimeToken??s.nonce??"").trim()?{runtimeToken:String(s.runtimeToken??s.nonce).trim()}:{},...String(s.virtualAssetBaseUrl??s.uploadUrl??"").trim()?{virtualAssetBaseUrl:String(s.virtualAssetBaseUrl??s.uploadUrl).trim()}:{},...Object.keys(a).length>0?{siteSettings:a}:{},...s.subscriptionType?{subscriptionType:s.subscriptionType}:{}};return Object.keys(c).length>0?c:null},i=await N(t.configPath).catch(()=>null),r=n(i?.wpsuite);if(r)return r;let u=n(e.wpsuite);return u||null}async function vt(e,t){return await Pt(e,t)}function ee(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 Ct(e,t){let n=await S(X(e.runtimeDir),null),i=n&&n.details&&typeof n.details.phase=="string"?n.details.phase.trim():"";if(i)return i;let r=n&&typeof n.currentStep=="string"?n.currentStep.trim():"";if(r)return r;let u=await we(e).catch(()=>"");if(u){let o=await S(g.join(u,"current-crawl-event.json"),null),s=o&&typeof o.currentStep=="string"?o.currentStep.trim():"";if(s)return s}return t}function At(e,t){if((e.command==="publish"||e.command==="crawl")&&t==="rewrite-text")return "rewrite-text"}async function It(e){let t=g.join(e.runtimeDir,"queue.json"),n=g.join(e.runtimeDir,"current-run.json"),i=await T(e.runtimeDir,async()=>{let r=await S(n,null);if(!r||r.status!=="running"&&r.status!=="queued")return null;let u=await S(t,[]),o=Array.isArray(u)?u.filter(a=>a?.id!==r.id):[],s=ee(r);return await P(t,[s,...o]),await P(n,null),s});i&&(await j(e.runtimeDir,i.id),await A(e.runtimeDir,{eventType:"job-recovered",status:"queued",actorSource:"queue-runner",jobId:i.id,command:i.command,message:"Recovered stale current-run entry back into queue."}));}async function Rt(e,t,n){try{if((n.shouldEnforceSubscriptionOnJobExecution?.(e)??n.enforceSubscriptionOnJobExecution)&&n.assertActiveSubscriptionForIdentity){let s=await vt(e,t);await n.assertActiveSubscriptionForIdentity(s);}let r=wt(e),u=e.resumeFromStep==="rewrite-text"?["--resume-rewrite"]:e.crawlMode==="incremental"?["--crawl-mode","incremental"]:[],o=e.deploymentProfile?["--profile",e.deploymentProfile]:[];if(e.command==="publish")return await v(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-release-cutoff"}),await I(t.exporterDir,t.runtimeDir,"content-sync",["--capture-baseline-cutoffs",e.id],t.configPath,r,e.id),await v(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:e.resumeFromStep==="rewrite-text"?"rewrite-text":"crawl"}),await I(t.exporterDir,t.runtimeDir,"crawl",u,t.configPath,r,e.id),await v(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await I(t.exporterDir,t.runtimeDir,"deploy",o,t.configPath,r,e.id),await v(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await I(t.exporterDir,t.runtimeDir,"invalidate",o,t.configPath,r,e.id),await v(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-baseline"}),await I(t.exporterDir,t.runtimeDir,"content-sync",["--establish-baselines",e.id],t.configPath,r,e.id),{exitCode:0};if(e.command==="crawl")return await v(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:e.resumeFromStep==="rewrite-text"?"rewrite-text":"crawl"}),await I(t.exporterDir,t.runtimeDir,"crawl",u,t.configPath,r,e.id),{exitCode:0};if(e.command==="deploy")return await v(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await I(t.exporterDir,t.runtimeDir,"deploy",o,t.configPath,r,e.id),{exitCode:0};if(e.command==="invalidate")return await v(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await I(t.exporterDir,t.runtimeDir,"invalidate",o,t.configPath,r,e.id),{exitCode:0};if(e.command==="retry-timeouts")return await v(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"retry-timeouts"}),await I(t.exporterDir,t.runtimeDir,"crawl",["--retry-timeouts"],t.configPath,r,e.id),{exitCode:0};if(e.command==="url"){let s=(e.url||"").trim();return s?(await v(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"url"}),await I(t.exporterDir,t.runtimeDir,"crawl",["--url",s],t.configPath,r,e.id),{exitCode:0}):{exitCode:2,error:"Missing url for command 'url'"}}return e.command==="content-sync"?!e.ruleId||!e.coalesceKey?{exitCode:2,error:"Content-sync queue jobs require ruleId and coalesceKey."}:(await v(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync"}),await I(t.exporterDir,t.runtimeDir,"content-sync",["--job-id",e.id],t.configPath,r,e.id),{exitCode:0}):{exitCode:2,error:`Unsupported command: ${e.command}`}}catch(i){return i instanceof E?{exitCode:130,error:"Job stop requested.",stopped:true,stopRequest:i.request,stoppedStep:i.step}:{exitCode:1,error:i instanceof Error?i.message:String(i)}}}function ve(e){return async function(n=process.argv.slice(2)){let i=nt(n),r=g.join(i.runtimeDir,"export.lock"),u=g.join(i.runtimeDir,"last-run.json");await k(i.runtimeDir),await v(i,"starting",{message:"queue-runner starting"});try{await yt(r);}catch(o){if((o&&typeof o=="object"&&"code"in o?String(o.code||""):"")!=="EEXIST")throw o;console.log("[queue-runner] lock active, skipping"),await v(i,"lock-active",{message:"lock active, skipped this cron tick"});return}try{await It(i);let o=await ft(i,e),s=0;for(let a=0;a<i.maxJobs;a++){let c=await Tt(i,e);if(c==="none"||(s+=1,c==="stopped"))break}if(s===0)await k(i.runtimeDir),await v(i,"idle",{processedJobs:s,schedulerEnqueued:o,message:"no queued jobs"});else {await k(i.runtimeDir);let a=await S(u,null),c=String(a?.status||"").trim();await v(i,c==="success"?"job-success":c==="stopped"?"job-stopped":"job-failed",{processedJobs:s,schedulerEnqueued:o,lastJobId:a?.id,lastJobCommand:a?.command,lastJobStatus:a?.status,lastJobExitCode:a?.exitCode,lastJobError:a?.error,...c==="stopped"?{currentStep:a?.stoppedStep,stopRequestedAt:a?.stopRequestedAt,stopRequestedByLogin:a?.stopRequestedByLogin,stopRequestedMode:a?.stopMode,lastStoppedStep:a?.stoppedStep,message:a?.stopMode==="requeue"?`Job stopped during ${a?.stoppedStep||a?.command||"active step"} and requeued.`:`Job stopped during ${a?.stoppedStep||a?.command||"active step"} and left out of queue.`}:{}});}}catch(o){let s=o instanceof Error?o.message:String(o);throw await v(i,"error",{message:s}),await A(i.runtimeDir,{eventType:"queue-runner-error",status:"failed",actorSource:"queue-runner",message:"Unhandled queue-runner error.",details:{error:s}}),o}finally{await bt(r);}}}async function qt(e,t,n){let i=g.join(e.runtimeDir,"queue.json"),r=g.join(e.runtimeDir,"current-run.json");await T(e.runtimeDir,async()=>{let u=await S(i,[]),o=Array.isArray(u)?u.filter(c=>c?.id!==t.id):[],s=ee(t),a=At(t,n);a?s.resumeFromStep=a:delete s.resumeFromStep,await P(i,[s,...o]),await P(r,null);});}async function xt(e,t,n){let i=Math.max(0,t.attempt??0)+1,r=Math.min(3600*1e3,6e4*2**(i-1)),u=Math.floor(r*.2*Math.random()),o=new Date(Date.now()+r+u).toISOString(),s={...ee(t),status:"retry-wait",attempt:i,nextAttemptAt:o,error:n};if(await T(e.runtimeDir,async()=>{let a=g.join(e.runtimeDir,"queue.json"),c=g.join(e.runtimeDir,"current-run.json"),d=(await S(a,[])).filter(p=>p.id!==t.id);await P(a,[s,...d]),await P(c,null);}),t.coalesceKey){let a=g.join(e.runtimeDir,"content-sync-state.json"),l=(await S(a,{})).rules??{};l[t.coalesceKey]={...l[t.coalesceKey]??{},ruleId:t.ruleId||"",coalesceKey:t.coalesceKey,retryAttempt:i,nextRetryAt:o,lastError:n,trailingWorkDetected:true},await P(a,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:l});}return s}async function Dt(e,t){if(!t.coalesceKey||!t.ruleId||!(await S(g.join(e.runtimeDir,"content-sync-state.json"),null))?.rules?.[t.coalesceKey]?.trailingWorkDetected)return null;let i=null;await T(e.runtimeDir,async()=>{let u=g.join(e.runtimeDir,"queue.json"),o=await S(u,[]);Z(o,{command:"content-sync",coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile})||(i={id:randomUUID(),command:"content-sync",ruleId:t.ruleId,coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile,enqueueSource:"scheduler",wpsuite:t.wpsuite,status:"queued",attempt:0,createdAt:new Date().toISOString(),createdBy:0},await P(u,[...o,i]));});let r=i;return r&&await A(e.runtimeDir,{eventType:"content-sync-trailing-job-created",status:"queued",actorSource:"queue-runner",jobId:r.id,command:r.command,message:"Content changes after the stable cutoff created one trailing job.",details:{predecessorJobId:t.id,ruleId:t.ruleId,coalesceKey:t.coalesceKey}}),r}async function Tt(e,t){let n=g.join(e.runtimeDir,"queue.json"),i=g.join(e.runtimeDir,"current-run.json"),r=g.join(e.runtimeDir,"last-run.json"),u=new Date().toISOString(),o=await T(e.runtimeDir,async()=>{let d=await S(n,[]);if(!Array.isArray(d)||d.length===0)return null;let p=Date.parse(String(d[0]?.nextAttemptAt||""));if(Number.isFinite(p)&&p>Date.now())return null;let h={...d[0],status:"running",startedAt:u};return delete h.nextAttemptAt,await P(n,d.slice(1)),await P(i,h),h});if(!o)return "none";await k(e.runtimeDir),await A(e.runtimeDir,{eventType:"job-run-started",status:"running",actorSource:"queue-runner",jobId:o.id,command:o.command,message:"Job execution started.",details:{createdAt:o.createdAt||"",startedAt:u,createdBy:o.createdBy??null,deploymentProfile:o.deploymentProfile||"",queuedWithTempAwsCreds:!!o.awsTempCreds}}),await v(e,"running",{currentJobId:o.id,currentJobCommand:o.command,currentStep:o.command});let s=await Rt(o,e,t),a=new Date().toISOString();if(s.stopped){let d=await Ct(e,s.stoppedStep||o.command),p=s.stopRequest?.mode==="requeue"?"requeue":"stop",h={...o,status:"stopped",endedAt:a,exitCode:s.exitCode,...s.error?{error:s.error}:{},stopRequestedAt:s.stopRequest?.requestedAt||"",stopRequestedByUserId:typeof s.stopRequest?.requestedByUserId=="number"?s.stopRequest.requestedByUserId:null,stopRequestedByLogin:s.stopRequest?.requestedByLogin||"",stopMode:p,stoppedStep:d};p==="requeue"?await qt(e,o,d):await P(i,null);try{let m=await G(h,e);h.logArchiveDir=m.archiveDir,h.logArchiveCreatedAt=m.archiveCreatedAt,h.logArchiveFileCount=m.archivedFiles.length;}catch(m){h.logArchiveError=m instanceof Error?m.message:String(m);}return await P(r,h),await j(e.runtimeDir,o.id),await k(e.runtimeDir),await A(e.runtimeDir,{eventType:"job-run-stopped",status:"stopped",actorSource:"queue-runner",jobId:o.id,command:o.command,message:p==="requeue"?"Job stop requested and requeued.":"Job stop requested and removed from active execution without requeue.",details:{startedAt:o.startedAt||"",endedAt:a,deploymentProfile:o.deploymentProfile||"",stopMode:p,stopRequestedAt:s.stopRequest?.requestedAt||"",stopRequestedByLogin:s.stopRequest?.requestedByLogin||"",stopRequestedByUserId:typeof s.stopRequest?.requestedByUserId=="number"?s.stopRequest.requestedByUserId:null,stoppedStep:d,logArchiveKey:h.logArchiveError||!h.logArchiveDir?"":g.basename(h.logArchiveDir),logArchiveDir:h.logArchiveDir||"",logArchiveCreatedAt:h.logArchiveCreatedAt||"",logArchiveFileCount:h.logArchiveFileCount??0,logArchiveError:h.logArchiveError||""}}),"stopped"}if(o.command==="content-sync"&&s.exitCode!==0){await A(e.runtimeDir,{eventType:"content-sync-failed",status:"failed",actorSource:"queue-runner",jobId:o.id,command:o.command,message:"Content-sync execution failed before cursor acknowledgement.",details:{attempt:o.attempt??0,error:s.error||"Content-sync execution failed."}});let d=await xt(e,o,s.error||"Content-sync execution failed."),p={...o,status:"retry-wait",endedAt:a,exitCode:s.exitCode,error:d.error,attempt:d.attempt,nextAttemptAt:d.nextAttemptAt};try{let h=await G(p,e);p.logArchiveDir=h.archiveDir,p.logArchiveCreatedAt=h.archiveCreatedAt,p.logArchiveFileCount=h.archivedFiles.length;}catch(h){p.logArchiveError=h instanceof Error?h.message:String(h);}return await P(r,p),await j(e.runtimeDir,o.id),await A(e.runtimeDir,{eventType:"content-sync-retry-scheduled",status:"retry-wait",actorSource:"queue-runner",jobId:o.id,command:o.command,message:"Content-sync retry was scheduled with bounded backoff.",details:{attempt:d.attempt??0,nextAttemptAt:d.nextAttemptAt||"",error:d.error||""}}),"processed"}let c={...o,status:s.exitCode===0?"success":"failed",endedAt:a,exitCode:s.exitCode,...s.error?{error:s.error}:{}};await P(i,null),c.status==="success"&&c.command==="content-sync"&&await Dt(e,c),await j(e.runtimeDir,o.id);try{let d=await G(c,e);c.logArchiveDir=d.archiveDir,c.logArchiveCreatedAt=d.archiveCreatedAt,c.logArchiveFileCount=d.archivedFiles.length;}catch(d){c.logArchiveError=d instanceof Error?d.message:String(d);}await P(r,c);let l=o.startedAt&&c.endedAt?Math.max(0,Math.round((new Date(c.endedAt).getTime()-new Date(o.startedAt).getTime())/1e3)):void 0;return await A(e.runtimeDir,{eventType:"job-run-finished",status:c.status==="success"?"success":"failed",actorSource:"queue-runner",jobId:c.id,command:c.command,message:c.status==="success"?"Job execution finished successfully.":"Job execution finished with failure.",details:{startedAt:o.startedAt||"",endedAt:c.endedAt||"",durationSec:l,deploymentProfile:c.deploymentProfile||"",exitCode:c.exitCode??null,error:c.error||"",logArchiveKey:c.logArchiveError||!c.logArchiveDir?"":g.basename(c.logArchiveDir),logArchiveDir:c.logArchiveDir||"",logArchiveCreatedAt:c.logArchiveCreatedAt||"",logArchiveFileCount:c.logArchiveFileCount??0,logArchiveError:c.logArchiveError||""}}),"processed"}async function Et(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 Ft={allowSchedulerAutoEnqueue:true,enforceSubscriptionOnJobExecution:false,shouldEnforceSubscriptionOnJobExecution:e=>ye(e)||e.enqueueSource==="scheduler"||e.createdBy===0,assertActiveSubscriptionForIdentity:Et},Ce=ve(Ft),Jt=Ce,un=Ce;process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href&&Jt().catch(e=>{console.error(e),process.exit(1);});export{un as default,Jt as main,Ce as runQueueRunner};
|
|
1
|
+
import {pathToFileURL}from'url';import v from'fs/promises';import {existsSync,createReadStream,createWriteStream}from'fs';import m from'path';import {spawn}from'child_process';import {randomUUID,createHash}from'crypto';import je from'http';import pe from'https';import {pipeline}from'stream/promises';import {createGzip}from'zlib';function ce(e){return [...new Set(e.map(t=>t.trim()).filter(Boolean))].sort()}function W(e){return Array.isArray(e)?e.map(W):e&&typeof e=="object"?Object.fromEntries(Object.entries(e).sort(([t],[n])=>t.localeCompare(n)).map(([t,n])=>[t,W(n)])):e}function ke(e){return JSON.stringify(W(e))}function F(e){return createHash("sha256").update(ke(e)).digest("hex")}function le(e){return F({schemaVersion:1,wordpressFingerprint:e.wordpressFingerprint,targetFingerprint:e.targetFingerprint,sourceOrigin:e.sourceOrigin,sitemapPaths:[...e.sitemapPaths].sort(),blockedPathPrefixes:[...e.blockedPathPrefixes].sort(),assetPathPrefixes:[...e.assetPathPrefixes].sort(),noJavaScriptRenderPathPrefixes:[...e.noJavaScriptRenderPathPrefixes].sort(),urlRewriteMode:e.urlRewriteMode})}function Ee(e){let t=new URL(e);if(t.protocol!=="http:"&&t.protocol!=="https:")throw new Error(`Unsupported source origin protocol: ${t.protocol}`);return t.hash="",t.search="",t.pathname=t.pathname.replace(/\/+$/,""),t.toString().replace(/\/$/,"")}function B(e){let t=ce((e.postTypes??[]).map(i=>String(i).toLowerCase().replace(/[^a-z0-9_-]/g,"")));if(t.length===0)throw new Error("Content-sync requires at least one public post type.");let n=ce((e.listingPaths??[]).map(i=>{let r=String(i).trim();if(!r)return "";let a=new URL(r,"https://content-sync.invalid/");if(a.origin!=="https://content-sync.invalid"&&/^https?:\/\//i.test(r))throw new Error("Content-sync listing routes must be same-site paths.");let s=`/${a.pathname.replace(/^\/+|\/+$/g,"")}`;return s==="/"?"/":`${s}/`}));return {postTypes:t,listingPaths:n,includePostTypeArchives:e.includePostTypeArchives!==false,includeTaxonomyArchives:e.includeTaxonomyArchives!==false,includeAuthorArchives:e.includeAuthorArchives===true,includeDateArchives:e.includeDateArchives===true,includePostsPage:e.includePostsPage!==false,includeSitemapChain:e.includeSitemapChain!==false}}function N(e,t,n){return F({schemaVersion:1,sourceOrigin:Ee(e),ruleId:t.trim(),scope:B(n)})}function K(e){return F({schemaVersion:1,...e})}function ge(e){return e.replace(/((?:x-static-publisher-token|authorization|proxy-authorization|x-api-key|aws-access-key-id|aws-secret-access-key|aws-session-token)\s*:\s*)[^\r\n]+/gi,"$1[REDACTED]").replace(/\b(Basic|Bearer)\s+[^\s]+/gi,"$1 [REDACTED]").replace(/([?&](?:token|access_token|refresh_token|api_key|key|nonce)=)[^&\s]+/gi,"$1[REDACTED]").replace(/((?:"|')?(?:runtimeToken|siteKey|nonce|accessToken|refreshToken|accessKeyId|secretAccessKey|sessionToken)(?:"|')?\s*[:=]\s*(?:"|'))[^"']+/gi,"$1[REDACTED]")}var de=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";de?m.join(de,"current-progress.json"):"";Promise.resolve();var Je="wp-json/smartcloud-static-publisher/v1/content-sync/",Oe=25*1024*1024,k=class extends Error{status;code;constructor(t,n,i){super(i),this.name="ContentSyncApiError",this.status=t,this.code=n;}},M=class{options;constructor(t){if(!t.runtimeToken.trim())throw new Error("Content-sync requires a WordPress runtime token.");this.options={...t,timeoutMs:t.timeoutMs??6e4};}endpoint(t){let n=this.options.sourceOrigin.replace(/\/+$/,"")+"/";return new URL(`${Je}${t}`,n)}async post(t,n){let i=this.endpoint(t),r=Buffer.from(JSON.stringify(n),"utf8"),a=i.protocol==="https:"?pe:je,s=i.protocol==="https:"?new pe.Agent({rejectUnauthorized:!this.options.ignoreHttpsErrors}):void 0;return new Promise((o,u)=>{let c=a.request(i,{method:"POST",agent:s,headers:{"content-type":"application/json","content-length":String(r.byteLength),"x-static-publisher-token":this.options.runtimeToken},timeout:this.options.timeoutMs},d=>{let g=[],p=0;d.on("data",h=>{if(p+=h.byteLength,p>Oe){d.destroy(new Error("Content-sync API response exceeded the size limit."));return}g.push(h);}),d.on("error",u),d.on("end",()=>{let h=Buffer.concat(g).toString("utf8"),l;try{l=h?JSON.parse(h):{};}catch{u(new k(d.statusCode??500,"invalid-json","Content-sync API returned invalid JSON."));return}let y=d.statusCode??500;if(y<200||y>=300){u(new k(y,String(l.code??"request-failed"),String(l.message??`Content-sync API request failed (${y}).`)));return}o(l);});});c.on("timeout",()=>c.destroy(new Error(`Content-sync API request timed out: ${t}`))),c.on("error",u),c.end(r);})}async head(t){return this.post("head",t)}async unboundHead(t){return this.post("head",{postTypes:t})}async events(t){let n=[],i=t.afterSequence,r=new Set(t.postTypes);for(;i<t.throughSequence;){let a=await this.post("events",{...t,afterSequence:i,limit:250});if(a.fromSequence!==i||a.throughSequence!==t.throughSequence||a.nextSequence<i||a.nextSequence>t.throughSequence)throw new Error("Content-sync API returned an inconsistent event page.");if(!Array.isArray(a.items))throw new Error("Content-sync API returned a malformed event page.");let s=i;for(let o of a.items){if(!Number.isSafeInteger(o.sequence)||o.sequence<=s||o.sequence>t.throughSequence||!Number.isSafeInteger(o.postId)||o.postId<=0||!r.has(o.postType)||typeof o.operation!="string"||!o.operation)throw new Error("Content-sync API returned an invalid or out-of-scope journal event.");s=o.sequence;}if(a.items.length>0&&a.nextSequence!==s)throw new Error("Content-sync API event cursor does not match the final returned event.");if(n.push(...a.items),!a.hasMore)break;if(a.nextSequence===i)throw new Error("Content-sync API event pagination did not advance.");i=a.nextSequence;}if(t.throughSequence>t.afterSequence&&n.at(-1)?.sequence!==t.throughSequence)throw new Error("Content-sync API omitted the claimed journal high-water event.");return n}async resolveImpactFamilies(t){let n=await this.post("impact",{families:t});return Array.isArray(n.families)?n.families:[]}async releaseFingerprint(){let t=await this.post("fingerprint",{}),n=String(t.fingerprint||"").trim();if(!/^[a-f0-9]{64}$/.test(n))throw new Error("WordPress returned an invalid release fingerprint.");return n}async establishBaseline(t){return this.post("baseline",t)}async acknowledge(t){return this.post("ack",t)}};function Le(e){return typeof e.subscriptionType=="string"&&e.subscriptionType.trim()!==""}function Be(e,t){return {...e,...t??{}}}function Me(e,t){return {...e,...t??{},invalidationPaths:[...(t?.invalidationPaths??e.invalidationPaths)||[]]}}function _(e,t){let n=String(t??e.deploymentTargetOverride??"").trim();if(!Le(e)){if(n)throw new Error(`Deployment target "${n}" requires an active WPSuite subscription and remote publisher config.`);return {name:null,profile:null,config:e}}let i=String(n||e.defaultDeploymentProfile||"").trim();if(!i)return {name:null,profile:null,config:e};let r=e.deploymentProfiles?.[i];if(!r)throw new Error(`Unknown deployment profile "${i}". Check the linked WPSuite publisher configuration.`);return {name:i,profile:r,config:{...e,targetOrigin:r.targetOrigin??e.targetOrigin,s3:Be(e.s3,r.s3),cloudFront:Me(e.cloudFront,r.cloudFront)}}}function he(e){let t=e.trim();return t==="."?".":t.replace(/\/$/,"")}function We(e){let t=String(e??"").trim();if(!t)return "";let n=t;if(/^https?:\/\//i.test(n))try{n=new URL(n).pathname;}catch{return ""}if(n=n.split(/[?#]/,1)[0]?.replace(/\\/g,"/").trim()??"",!n)return "";let i=n.endsWith("/"),r=n.replace(/^\/+/,"").split("/").map(s=>s.replace(/[^A-Za-z0-9._-]/g,"")).filter(s=>s.length>0&&s!=="."&&s!=="..");if(r.length===0)return "";let a=`/${r.join("/")}`;return i&&m.extname(a)===""&&(a+="/"),a==="/"?"":a}function z(e){return !e||typeof e!="object"?{}:Object.fromEntries(Object.entries(e).map(([t,n])=>[t.trim(),String(n??"")]).filter(([t])=>t.length>0))}function Ne(e){if(!e||typeof e!="object")return {};let t={};for(let[n,i]of Object.entries(e)){let r=n.trim();if(!r||!i||typeof i!="object")continue;let a=i,s={};if(typeof a.targetOrigin=="string"){let u=he(a.targetOrigin);u&&(s.targetOrigin=u);}let o=z(a.extraReplacements);if(Object.keys(o).length>0&&(s.extraReplacements=o),a.s3&&typeof a.s3=="object"){let u={},c=a.s3;typeof c.bucket=="string"&&(u.bucket=c.bucket.trim()),typeof c.prefix=="string"&&(u.prefix=c.prefix.trim()),typeof c.region=="string"&&(u.region=c.region.trim()),typeof c.htmlCacheControl=="string"&&(u.htmlCacheControl=c.htmlCacheControl.trim()),typeof c.assetCacheControl=="string"&&(u.assetCacheControl=c.assetCacheControl.trim()),Object.keys(u).length>0&&(s.s3=u);}if(a.cloudFront&&typeof a.cloudFront=="object"){let u={},c=a.cloudFront;typeof c.distributionId=="string"&&(u.distributionId=c.distributionId.trim()),Array.isArray(c.invalidationPaths)&&(u.invalidationPaths=c.invalidationPaths.map(d=>String(d??"").trim()).filter(d=>d.length>0)),Object.keys(u).length>0&&(s.cloudFront=u);}t[r]=s;}return t}function G(e){return e==="PROFESSIONAL"||e==="AGENCY"?e:void 0}function Ke(e,t){if(!e||typeof e!="object")return null;let n=e,i=n.command;if(i!=="publish"&&i!=="crawl"&&i!=="deploy"&&i!=="invalidate"&&i!=="retry-timeouts"&&i!=="url"&&i!=="content-sync")return null;let r=Number.parseInt(String(n.intervalMinutes??"0"),10);if(!Number.isFinite(r)||r<1)return null;let a=String(n.id??`${i}-${t+1}`).trim();if(!a)return null;let s=String(n.deploymentProfile??"").trim(),o=String(n.url??"").trim(),u=Array.isArray(n.postTypes)?[...new Set(n.postTypes.map(d=>String(d??"").trim().toLowerCase().replace(/[^a-z0-9_-]/g,"")).filter(Boolean))]:[],c=Array.isArray(n.listingPaths)?[...new Set(n.listingPaths.map(d=>String(d??"").trim()).filter(Boolean))]:[];return i==="content-sync"&&u.length===0?null:{id:a,enabled:n.enabled!==false,command:i,intervalMinutes:r,...i==="publish"||i==="crawl"?{crawlMode:n.crawlMode==="incremental"?"incremental":"full"}:{},...(i==="publish"||i==="deploy"||i==="invalidate"||i==="content-sync")&&s?{deploymentProfile:s}:{},...o?{url:o}:{},...u.length>0?{postTypes:u}:{},...c.length>0?{listingPaths:c}:{},...typeof n.includePostTypeArchives=="boolean"?{includePostTypeArchives:n.includePostTypeArchives}:{},...typeof n.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:n.includeTaxonomyArchives}:{},...typeof n.includeAuthorArchives=="boolean"?{includeAuthorArchives:n.includeAuthorArchives}:{},...typeof n.includeDateArchives=="boolean"?{includeDateArchives:n.includeDateArchives}:{},...typeof n.includePostsPage=="boolean"?{includePostsPage:n.includePostsPage}:{},...typeof n.includeSitemapChain=="boolean"?{includeSitemapChain:n.includeSitemapChain}:{}}}function fe(e){let t=e&&typeof e=="object"?e:{},n=Array.isArray(t.rules)?t.rules.map((i,r)=>Ke(i,r)).filter(i=>!!i):[];return {enabled:t.enabled===true,timezone:typeof t.timezone=="string"&&t.timezone.trim()!==""?t.timezone.trim():"UTC",rules:n}}function _e(e){if(!e||typeof e!="object")return;let t=e,n={};for(let r of ["accountId","siteId"]){let a=t[r];typeof a=="string"&&a.trim()!==""&&(n[r]=a.trim());}let i=Number(t.lastUpdate??0);return Number.isFinite(i)&&i>0&&(n.lastUpdate=Math.floor(i)),t.subscriber===true&&(n.subscriber=true),Object.keys(n).length>0?n:void 0}function V(e){let t=e&&typeof e=="object"?e:{},n=String(t.accountId??"").trim(),i=String(t.siteId??"").trim(),r=t.subscriber===true,a=_e(t.siteSettings),s={...a??{},...n?{accountId:a?.accountId??n}:{},...i?{siteId:a?.siteId??i}:{},...a?.subscriber===true||r?{subscriber:true}:{}},o={},u=String(t.apiBase??"").trim();u&&(o.apiBase=u);let c=String(t.runtimeToken??t.nonce??"").trim();c&&(o.runtimeToken=c);let d=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();d&&(o.virtualAssetBaseUrl=d),Object.keys(s).length>0&&(o.siteSettings=s);let g=G(t.subscriptionType);return g&&(o.subscriptionType=g),o}function ze(e){try{let t=new URL(e);globalThis.location=t;}catch{}}function ye(e){return typeof e=="string"?e:e instanceof URL?e.toString():e.url}function He(e,t){let n=new Headers(e instanceof Request?e.headers:void 0);if(t?.headers)for(let[i,r]of new Headers(t.headers).entries())n.set(i,r);return n}async function be(e,t,n=5){let i=new URL(ye(e)),r=String(t?.method??(e instanceof Request?e.method:"GET")).toUpperCase(),a=i.protocol==="http:"?je:pe,s=He(e,t);return await new Promise((o,u)=>{let c=a.request(i,{method:r,headers:Object.fromEntries(s.entries()),...i.protocol==="https:"?{rejectUnauthorized:false}:{}},d=>{let g=[];d.on("data",p=>{g.push(Buffer.isBuffer(p)?p:Buffer.from(p));}),d.on("error",u),d.on("end",()=>{let p=d.statusCode??0,h=d.headers.location;if(n>0&&h&&[301,302,303,307,308].includes(p)){o(be(new URL(h,i),{method:p===303?"GET":r},n-1));return}let l=new Headers;for(let[y,b]of Object.entries(d.headers))Array.isArray(b)?l.set(y,b.join(", ")):typeof b=="string"&&l.set(y,b);o(new Response(Buffer.concat(g),{status:p,statusText:d.statusMessage??"",headers:l}));});});c.on("error",u),c.end();})}function Ge(e){let t=V(e.wpsuite),n={...t.siteSettings??{},...t.siteSettings?.subscriber===true||t.subscriptionType==="PROFESSIONAL"||t.subscriptionType==="AGENCY"?{subscriber:true}:{}},i=String(n.accountId??"").trim(),r=String(n.siteId??"").trim(),a=String(t.virtualAssetBaseUrl??t.uploadUrl??"").trim();if(e.wpsuite={...t.apiBase?{apiBase:t.apiBase}:{},...t.runtimeToken?{runtimeToken:t.runtimeToken}:{},...a?{virtualAssetBaseUrl:a}:{},...Object.keys(n).length>0?{siteSettings:n}:{},...t.subscriptionType?{subscriptionType:t.subscriptionType}:{}},!i||!r||!a)return false;ze(e.sourceOrigin);let s=globalThis,u={...s.WpSuite??{},siteSettings:n,uploadUrl:a};return t.apiBase&&(u.apiBase=t.apiBase),s.WpSuite=u,true}async function Ve(e){let t=String(e.wpsuite?.virtualAssetBaseUrl??e.wpsuite?.uploadUrl??"").trim();if(!Ge(e)){let r=e.wpsuite?.siteSettings,a=[String(r?.accountId??"").trim()?"":"accountId",String(r?.siteId??"").trim()?"":"siteId",t?"":"virtualAssetBaseUrl"].filter(Boolean);return {remote:null,diagnostic:{status:"invalid-runtime",virtualAssetBaseUrl:t,subscriptionType:null,error:`Missing WP Suite runtime field(s): ${a.join(", ")}.`,requests:[]}}}let n=globalThis.fetch,i=[];try{typeof n=="function"&&/^https?:\/\//i.test(t)&&(globalThis.fetch=(async(u,c)=>{let d=ye(u);if(d.startsWith(t)){let g=(()=>{try{return new URL(d).pathname}catch{return d}})();try{let p=e.ignoreHttpsErrors&&d.startsWith("https://")?await be(u,c):await n(u,c);return i.push({path:g,status:p.status,ok:p.ok,error:null}),p}catch(p){throw i.push({path:g,status:null,ok:!1,error:p instanceof Error?p.message:String(p)}),p}}return n(u,c)}));let{getConfig:r}=await import('@smart-cloud/wpsuite-core'),a=await r("publisher"),s=a&&typeof a=="object"?a:null,o=G(s?.subscriptionType);return {remote:s,diagnostic:{status:s?"loaded":"unavailable",virtualAssetBaseUrl:t,subscriptionType:o??null,error:s?null:"WP Suite publisher config loader returned no configuration.",requests:i}}}catch(r){return {remote:null,diagnostic:{status:"unavailable",virtualAssetBaseUrl:t,subscriptionType:null,error:r instanceof Error?r.message:String(r),requests:i}}}finally{typeof n=="function"&&(globalThis.fetch=n);}}function Ye(e,t){let n=Ne(t?.deploymentProfiles),i=String(t?.defaultDeploymentProfile??"").trim(),r=n[i]?i:"",a=G(t?.subscriptionType),s=V(e.wpsuite);return {...e,scheduler:fe(t?.scheduler),deploymentProfiles:n,defaultDeploymentProfile:r,...a?{subscriptionType:a}:{},wpsuite:{...s,...s.siteSettings||a?{siteSettings:{...s.siteSettings??{},...s.siteSettings?.subscriber===true||a==="PROFESSIONAL"||a==="AGENCY"?{subscriber:true}:{}}}:{},...a?{subscriptionType:a}:{}}}}function H(e,t){let i=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!i)return t;let r=i.split("/").map(a=>a.trim()).filter(a=>a.length>0&&a!=="."&&a!=="..");return r.length>0?r.join("/"):t}function Ze(){let e=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"";return e.trim()?m.resolve(e):""}function me(e,t,n){let i=String(t||"").trim();return i&&m.isAbsolute(i)?m.resolve(i):m.resolve(e,H(i,n))}async function Y(e,t={}){let n=String(e??"").trim()||process.env.PUBLISHER_CONFIG||"publisher.config.json",i=await v.readFile(n,"utf8"),r=JSON.parse(i);r.sourceOrigin=r.sourceOrigin.replace(/\/$/,""),r.targetOrigin=he(r.targetOrigin),r.ignoreHttpsErrors??=false,r.outputDir=String(r.outputDir||"export").trim()||"export",r.urlRewriteMode||=r.targetOrigin==="."?"relative":"absolute",r.noJavaScriptRenderPathPrefixes||=[],r.seedPaths||=[],r.generated404RequestPath=We(r.generated404RequestPath),r.sitemapPaths||=["/sitemap_index.xml","/sitemap.xml"],r.allowedAssetHosts||=[],r.assetPathPrefixes||=["/wp-content/","/wp-includes/","/static/","/assets/","/build/","/_next/","/docs/","/sitemap","/robots.txt","/llms.txt"],r.blockedPathPrefixes||=[],r.blockedSearchFragments||=[],r.concurrency||=1,r.maxPages||=0,r.extraReplacements=z(r.extraReplacements),r.postCrawlCopyMap=z(r.postCrawlCopyMap),r.logDir=String(r.logDir||"logs").trim()||"logs",r.verbose??=false,r.logLevel||=r.verbose?"debug":"info",r.s3SyncMode||="sdk-upload-delete",r.readiness||={waitForSelector:null,waitForFunction:null,timeoutMs:1500,fallbackWaitMs:1500},r.readiness.timeoutMs??=1500,r.readiness.fallbackWaitMs??=1500,r.viewport||={width:1440,height:1200},r.navigationTimeoutMs||=3e4,r.assetDownloadConcurrency=Number(r.assetDownloadConcurrency)>0?Number(r.assetDownloadConcurrency):r.concurrency,r.rewriteConcurrency=Number(r.rewriteConcurrency)>0?Number(r.rewriteConcurrency):r.assetDownloadConcurrency,r.wpsuite=V(r.wpsuite),r.scheduler=fe(void 0),r.deploymentProfiles={},r.defaultDeploymentProfile="",r.deploymentTargetOverride=String(r.deploymentTargetOverride??"").trim();let a=await Ve(r);t.onRemotePublisherConfigDiagnostic?.(a.diagnostic);let s=Ye(r,a.remote),o=Ze(),u=o?m.resolve(o,".."):"";return u?(s.outputDir=me(u,s.outputDir,"export"),s.logDir=me(u,s.logDir,"logs")):(m.isAbsolute(s.outputDir)||(s.outputDir=H(s.outputDir,"export")),m.isAbsolute(s.logDir)||(s.logDir=H(s.logDir,"logs"))),s}function Pe(e){let t=String(e.command??"").trim();return (t==="publish"||t==="crawl")&&e.crawlMode==="incremental"||t==="content-sync"}var Z=new Map;async function Se(e){let t=m.resolve(e),n=Z.get(t);if(n!==void 0)return n;try{let i=JSON.parse(await v.readFile(t,"utf8")),r=String(i.version??"").trim();return Z.set(t,r),r}catch{return Z.set(t,""),""}}var O=class extends Error{request;step;constructor(t,n){super(`Stop requested during ${n}.`),this.name="StopRequestedError",this.request=t,this.step=n;}},it="queue-mutation.lock",st=5e3,ot=3e4;function at(e){let t=process.env.STATIC_PUBLISHER_RUNTIME_DIR||process.env.WPSUITE_STATIC_PUBLISHER_RUNTIME_DIR||"",n=process.env.STATIC_PUBLISHER_EXPORTER_DIR||process.cwd(),i=process.env.PUBLISHER_CONFIG||"",r=1;for(let o=0;o<e.length;o++){let u=e[o];u==="--runtime-dir"?t=e[++o]||"":u.startsWith("--runtime-dir=")?t=u.slice(14):u==="--exporter-dir"?n=e[++o]||n:u.startsWith("--exporter-dir=")?n=u.slice(15):u==="--config"?i=e[++o]||"":u.startsWith("--config=")?i=u.slice(9):u==="--max-jobs"?r=Number.parseInt(e[++o]||"1",10):u.startsWith("--max-jobs=")&&(r=Number.parseInt(u.slice(11),10));}if(!t)throw new Error("Missing runtime dir. Use --runtime-dir or STATIC_PUBLISHER_RUNTIME_DIR.");let a=m.resolve(t),s=i?m.resolve(i):m.join(a,"config.json");return {runtimeDir:a,exporterDir:m.resolve(n),configPath:s,maxJobs:Number.isFinite(r)&&r>0?r:1}}async function w(e,t){try{let n=await v.readFile(e,"utf8");return JSON.parse(n)}catch(n){if((n&&typeof n=="object"&&"code"in n?String(n.code||""):"")!=="ENOENT"){let r=n instanceof Error?n.message:String(n);console.warn(`[queue-runner] failed to read JSON ${e}: ${r}`);}return t}}async function S(e,t){await v.mkdir(m.dirname(e),{recursive:true}),await v.writeFile(e,JSON.stringify(t,null,2),"utf8");}function Ce(e){return m.join(e,it)}async function ut(e){await new Promise(t=>setTimeout(t,e));}async function ct(e){let t=Ce(e),n=Date.now()+st;for(;;)try{await v.mkdir(m.dirname(t),{recursive:!0}),await v.writeFile(t,JSON.stringify({pid:process.pid,createdAt:new Date().toISOString()},null,2),{encoding:"utf8",flag:"wx"});return}catch(i){if((i&&typeof i=="object"&&"code"in i?String(i.code||""):"")!=="EEXIST")throw i;let a=await v.stat(t).catch(()=>null);if(a&&Date.now()-a.mtimeMs>ot){await v.unlink(t).catch(()=>{});continue}if(Date.now()>=n)throw new Error("Timed out acquiring queue mutation lock.",{cause:i});await ut(50);}}async function lt(e){await v.unlink(Ce(e)).catch(()=>{});}async function E(e,t){await ct(e);try{return await t()}finally{await lt(e);}}function Ae(e){return m.join(e,"queue-runner-heartbeat.json")}function oe(e){return m.join(e,"current-progress.json")}function X(e){return m.join(e,"scheduler-state.json")}function dt(e,t){let i=String(e||"").replace(/\\/g,"/").trim().replace(/^\/+|\/+$/g,"");if(!i)return t;let r=i.split("/").map(a=>a.trim()).filter(a=>a.length>0&&a!=="."&&a!=="..");return r.length>0?r.join("/"):t}async function Ie(e){let t=await w(e.configPath||"",null),n=m.resolve(e.runtimeDir,".."),r=(typeof t?.logDir=="string"?t.logDir:"").trim();return r&&m.isAbsolute(r)?m.resolve(r):m.resolve(n,dt(r,"logs"))}function ee(e,t){return String(e||"").trim().replace(/[^a-zA-Z0-9._-]+/g,"-").replace(/^-+|-+$/g,"")||t}function gt(e){let t=e?new Date(e):new Date;return (Number.isNaN(t.getTime())?new Date:t).toISOString().replace(/[-:]/g,"").replace(/\.\d{3}Z$/,"Z")}function pt(e){return [gt(e.endedAt||e.startedAt),ee(e.command||"job","job"),ee(e.id||"job","job"),ee(e.status||"finished","finished")].join("-")}function mt(e){return e.endsWith(".log.jsonl")||e.endsWith(".errors.jsonl")||e.endsWith(".rejected.jsonl")?true:["current-crawl-event.json","rejected.jsonl","ignored.jsonl","skipped-http.jsonl","errors.jsonl","timings.jsonl","rejected.json","ignored.json","skipped-http.json","errors.json","timings.json"].includes(e)}function ht(e){return e==="errors.json"||e==="errors.jsonl"||e.endsWith(".errors.jsonl")?"errors":e==="ignored.json"||e==="ignored.jsonl"?"ignored":e==="rejected.json"||e==="rejected.jsonl"||e.endsWith(".rejected.jsonl")?"rejected":e==="skipped-http.json"||e==="skipped-http.jsonl"?"skipped-http":e==="timings.json"||e==="timings.jsonl"?"timings":e==="current-progress.json"?"current-progress":e==="current-crawl-event.json"?"current-crawl-event":e.endsWith(".log.jsonl")?e.replace(/\.log\.jsonl$/i,"-log"):e.replace(/\.[^.]+$/u,"")||"artifact"}function ft(e){return e.endsWith(".gz")?"application/gzip":e.endsWith(".jsonl")?"application/x-ndjson":e.endsWith(".json")?"application/json":"text/plain"}async function te(e,t,n){let i=`${n}.gz`,r=m.join(t,i);await pipeline(createReadStream(e),createGzip({level:9}),createWriteStream(r));let[a,s]=await Promise.all([v.stat(e),v.stat(r)]);return {role:ht(n),originalFileName:n,storedFileName:i,compressed:true,compression:"gzip",contentType:ft(i),originalSize:a.size,storedSize:s.size}}async function U(e,t){let n=await Ie(t),i=new Date().toISOString(),r=pt(e),a=m.join(n,"archive",r),s=[],o=[];await v.mkdir(a,{recursive:true});try{let d=await v.readdir(n,{withFileTypes:!0});for(let g of d){if(!g.isFile()||!mt(g.name))continue;let p=await te(m.join(n,g.name),a,g.name);o.push(p),s.push(p.storedFileName);}}catch(d){if((d&&typeof d=="object"&&"code"in d?String(d.code||""):"")!=="ENOENT")throw d}let u=oe(t.runtimeDir);if(existsSync(u)){let d=await te(u,a,"current-progress.json");o.push(d),s.push(d.storedFileName);}if(e.command==="content-sync")for(let d of ["content-sync-current.json","content-sync-impact-plan.json","content-sync-checkpoint.json","content-sync-state.json","content-sync-invalidation.json","deploy-plan.json"]){let g=m.join(t.runtimeDir,d);if(!existsSync(g))continue;let p=await te(g,a,d);o.push(p),s.push(p.storedFileName);}let c=[...s,"job.json"];return await S(m.join(a,"job.json"),{manifestVersion:1,archivedAt:i,archiveKey:r,archiveDir:a,logDir:n,runtimeDir:t.runtimeDir,exporterDir:t.exporterDir,configPath:t.configPath||"",archivedFiles:c,artifacts:o,job:e}),{archiveKey:r,archiveDir:a,archiveCreatedAt:i,archivedFiles:c,artifacts:o}}function yt(e){return m.join(e,"audit-events.jsonl")}function ie(e){return m.join(e,"stop-request.json")}async function Re(e){try{let t=await v.readFile(ie(e),"utf8"),n=JSON.parse(t);return n&&typeof n=="object"?n:null}catch{return null}}async function we(e,t){let n=await Re(e);if(!n)return null;let i=String(n.targetJobId||"").trim();return i&&i!==t?null:n}async function j(e,t){if(!t){await v.unlink(ie(e)).catch(()=>{});return}let n=await Re(e);if(!n)return;let i=String(n.targetJobId||"").trim();(!i||i===t)&&await v.unlink(ie(e)).catch(()=>{});}async function I(e,t){try{let n={occurredAt:t.occurredAt||new Date().toISOString(),status:t.status||"info",actorSource:t.actorSource||"queue-runner",...t};await v.mkdir(e,{recursive:!0}),await v.appendFile(yt(e),`${JSON.stringify(n)}
|
|
2
|
+
`,"utf8");}catch{}}async function C(e,t,n){let[i,r]=await Promise.all([Se(m.join(e.exporterDir,"package.json")),Se(m.join(e.exporterDir,"node_modules","@smart-cloud","wpsuite-core","package.json"))]),a={checkedAt:new Date().toISOString(),status:t,pid:process.pid,nodePath:process.execPath,nodeVersion:process.version,exporterVersion:i,wpsuiteCoreVersion:r,runtimeDir:e.runtimeDir,exporterDir:e.exporterDir,...n??{}};try{await S(Ae(e.runtimeDir),a);}catch{}}async function J(e){try{await v.unlink(oe(e));}catch{}}function bt(e,t){let n=e.command;if(n!=="publish"&&n!=="crawl"&&n!=="deploy"&&n!=="invalidate"&&n!=="retry-timeouts"&&n!=="url"&&n!=="content-sync")return null;let i=Number.parseInt(String(e.intervalMinutes??"0"),10);if(!Number.isFinite(i)||i<1)return null;let r=(e.id||`${n}-${t+1}`).trim();if(!r)return null;let a=e.enabled!==false,s=typeof e.url=="string"?e.url.trim():"",o=typeof e.deploymentProfile=="string"?e.deploymentProfile.trim():"",u=Array.isArray(e.postTypes)?[...new Set(e.postTypes.map(g=>String(g).trim()).filter(Boolean))]:[],c=Array.isArray(e.listingPaths)?[...new Set(e.listingPaths.map(g=>String(g).trim()).filter(Boolean))]:[],d=(n==="publish"||n==="crawl")&&e.crawlMode==="incremental"?"incremental":"full";return n==="url"&&!s||n==="content-sync"&&u.length===0?null:{id:r,enabled:a,command:n,intervalMinutes:i,...n==="publish"||n==="crawl"?{crawlMode:d}:{},...(n==="publish"||n==="deploy"||n==="invalidate"||n==="content-sync")&&o?{deploymentProfile:o}:{},...s?{url:s}:{},...u.length>0?{postTypes:u}:{},...c.length>0?{listingPaths:c}:{},...typeof e.includePostTypeArchives=="boolean"?{includePostTypeArchives:e.includePostTypeArchives}:{},...typeof e.includeTaxonomyArchives=="boolean"?{includeTaxonomyArchives:e.includeTaxonomyArchives}:{},...typeof e.includeAuthorArchives=="boolean"?{includeAuthorArchives:e.includeAuthorArchives}:{},...typeof e.includeDateArchives=="boolean"?{includeDateArchives:e.includeDateArchives}:{},...typeof e.includePostsPage=="boolean"?{includePostsPage:e.includePostsPage}:{},...typeof e.includeSitemapChain=="boolean"?{includeSitemapChain:e.includeSitemapChain}:{}}}async function St(e){let t=await Y(e.configPath).catch(()=>null),n=t?.scheduler,i=Array.isArray(n?.rules)?n.rules.map((r,a)=>bt(r,a)).filter(r=>!!r):[];return {enabled:!!n?.enabled,timezone:typeof n?.timezone=="string"&&n.timezone.trim()?n.timezone.trim():"UTC",rules:i,wpsuite:t?.wpsuite,publisherConfig:t}}function ne(e,t){if(!t)throw new Error("Content-sync scheduler requires a valid publisher config.");let n=B({postTypes:e.postTypes??[],listingPaths:e.listingPaths??[],includePostTypeArchives:e.includePostTypeArchives,includeTaxonomyArchives:e.includeTaxonomyArchives,includeAuthorArchives:e.includeAuthorArchives,includeDateArchives:e.includeDateArchives,includePostsPage:e.includePostsPage,includeSitemapChain:e.includeSitemapChain}),i=N(t.sourceOrigin,e.id,n),r=_(t,e.deploymentProfile),a=K({sourceOrigin:t.sourceOrigin,targetOrigin:r.config.targetOrigin,deploymentProfile:r.name||"",s3Bucket:r.config.s3.bucket,s3Prefix:r.config.s3.prefix,cloudFrontDistributionId:r.config.cloudFront.distributionId,urlRewriteMode:r.config.urlRewriteMode,extraReplacements:r.profile?.extraReplacements??{}});return `content-sync:${e.id}:${F({scopeFingerprint:i,targetFingerprint:a}).slice(0,32)}`}async function wt(e,t,n,i,r){let s=(await w(m.join(e.runtimeDir,"content-sync-baseline.json"),{})).entries?.[i];if(!s?.consumerId||!s.scopeFingerprint||!s.baselineId)return {status:"baseline-required",reason:"No verified normal-release baseline exists for this rule scope."};try{let o=t.publisherConfig;if(!o)throw new Error("Publisher configuration is unavailable.");let u=B({postTypes:n.postTypes??[],listingPaths:n.listingPaths??[],includePostTypeArchives:n.includePostTypeArchives,includeTaxonomyArchives:n.includeTaxonomyArchives,includeAuthorArchives:n.includeAuthorArchives,includeDateArchives:n.includeDateArchives,includePostsPage:n.includePostsPage,includeSitemapChain:n.includeSitemapChain}),c=N(o.sourceOrigin,n.id,u),d=_(o,n.deploymentProfile),g=K({sourceOrigin:o.sourceOrigin,targetOrigin:d.config.targetOrigin,deploymentProfile:d.name||"",s3Bucket:d.config.s3.bucket,s3Prefix:d.config.s3.prefix,cloudFrontDistributionId:d.config.cloudFront.distributionId,urlRewriteMode:d.config.urlRewriteMode,extraReplacements:d.profile?.extraReplacements??{}});if(s.scopeFingerprint!==c||s.targetFingerprint!==g||!s.releaseFingerprint)return {status:"baseline-required",reason:"The verified content-sync baseline does not match the active scope or deployment target. Run a successful full or incremental publish to establish a new baseline."};let p=new M({sourceOrigin:o.sourceOrigin,runtimeToken:String(o.wpsuite?.runtimeToken||""),ignoreHttpsErrors:o.ignoreHttpsErrors}),h=await p.releaseFingerprint(),l=le({wordpressFingerprint:h,targetFingerprint:g,sourceOrigin:o.sourceOrigin,sitemapPaths:o.sitemapPaths,blockedPathPrefixes:o.blockedPathPrefixes,assetPathPrefixes:o.assetPathPrefixes,noJavaScriptRenderPathPrefixes:o.noJavaScriptRenderPathPrefixes,urlRewriteMode:o.urlRewriteMode});if(s.releaseFingerprint!==l)return {status:"baseline-required",reason:"The installed WordPress release changed after the last verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline."};let y=await p.head({consumerId:s.consumerId,scopeFingerprint:s.scopeFingerprint,baselineId:s.baselineId,postTypes:u.postTypes}),b=y.committedSequence;if(r?.command==="content-sync"&&r.coalesceKey===i){let A=await w(m.join(e.runtimeDir,"content-sync-current.json"),null);A?.coalesceKey===i&&Number.isSafeInteger(A.toSequence)&&(b=Math.max(b,Number(A.toSequence)));}return {status:y.headSequence>b?"pending":"current",headSequence:y.headSequence,committedSequence:y.committedSequence}}catch(o){return o instanceof k&&o.code==="baseline-required"?{status:"baseline-required",reason:o.message}:{status:"unavailable",reason:o instanceof Error?o.message:String(o)}}}async function qe(e,t,n,i,r,a){let s=m.join(e.runtimeDir,"content-sync-state.json"),u=(await w(s,{})).rules??{},c=u[n]??{};u[n]={...c,ruleId:t,coalesceKey:n,...Number.isSafeInteger(r)?{observedHeadSequence:r}:{},...Number.isSafeInteger(a)?{committedSequence:a}:{},retryAttempt:0,nextRetryAt:null,lastError:i,baselineStatus:"required",baselineReason:i,trailingWorkDetected:Number.isSafeInteger(r)&&Number.isSafeInteger(a)?Number(r)>Number(a):!!c.trailingWorkDetected},await S(s,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:u});}function se(e,t){let n=(t.url||"").trim(),i=t.crawlMode||"full",r=(t.deploymentProfile||"").trim(),a=(t.coalesceKey||"").trim();return e.some(s=>s.command===t.command&&(t.command!=="content-sync"||(s.coalesceKey||"").trim()===a)&&(s.url||"").trim()===n&&(s.crawlMode||"full")===i&&(s.deploymentProfile||"").trim()===r&&(s.status===void 0||s.status==="queued"||s.status==="retry-wait"||s.status==="running"))}async function vt(e,t){if(!t.allowSchedulerAutoEnqueue)return 0;let n=await St(e);if(!n.enabled||n.rules.length===0)return 0;if(t.assertActiveSubscriptionForIdentity)try{await t.assertActiveSubscriptionForIdentity(n.wpsuite??null);}catch(l){let y=l instanceof Error?l.message:String(l);return console.warn(`[queue-runner] scheduler auto-enqueue skipped: ${y}`),0}let i=m.join(e.runtimeDir,"queue.json"),r=m.join(e.runtimeDir,"current-run.json"),a=Date.now(),s=0,o=[],u=[],c=[],d=new Map,[g,p,h]=await Promise.all([w(i,[]),w(r,null),w(X(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}})]);for(let l of n.rules){if(!l.enabled||l.command!=="content-sync")continue;let y=Math.floor(a/(l.intervalMinutes*60*1e3)),b=h.lastEvaluatedBucketByRuleId?.[l.id]??h.lastEnqueuedBucketByRuleId?.[l.id]??-1;if(y<=b)continue;let A=ne(l,n.publisherConfig);se(g,{command:l.command,deploymentProfile:l.deploymentProfile,coalesceKey:A})||d.set(l.id,await wt(e,n,l,A,p));}await E(e.runtimeDir,async()=>{let l=await w(i,[]),y=await w(r,null),b=await w(X(e.runtimeDir),{lastEnqueuedBucketByRuleId:{},lastEvaluatedBucketByRuleId:{},lastCreatedBucketByRuleId:{},coalescedCountByRuleId:{}});b.lastEnqueuedBucketByRuleId??={},b.lastEvaluatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.lastCreatedBucketByRuleId??={...b.lastEnqueuedBucketByRuleId},b.coalescedCountByRuleId??={};let A=[...l];y&&y.status==="running"&&A.unshift(y);for(let f of n.rules){if(!f.enabled)continue;let $=f.intervalMinutes*60*1e3,P=Math.floor(a/$),x=b.lastEvaluatedBucketByRuleId[f.id]??b.lastEnqueuedBucketByRuleId[f.id]??-1;if(P<=x)continue;let T=f.command==="content-sync"?ne(f,n.publisherConfig):"";b.lastEvaluatedBucketByRuleId[f.id]=P;let L=f.command==="content-sync"?l:A;if(se(L,{command:f.command,url:f.url,crawlMode:f.crawlMode,deploymentProfile:f.deploymentProfile,coalesceKey:T})){b.coalescedCountByRuleId[f.id]=(b.coalescedCountByRuleId[f.id]??0)+1,u.push(f);continue}if(f.command==="content-sync"){let Q=d.get(f.id);if(!Q||Q.status!=="pending"){let ue=Q??{status:"unavailable",reason:"Content-sync demand was not checked."};ue.status!=="current"&&c.push({rule:f,check:ue});continue}}let q={id:randomUUID(),command:f.command,...f.command==="content-sync"?{ruleId:f.id,coalesceKey:T,attempt:0}:{},...(f.command==="publish"||f.command==="crawl")&&f.crawlMode?{crawlMode:f.crawlMode}:{},...(f.command==="publish"||f.command==="deploy"||f.command==="invalidate"||f.command==="content-sync")&&f.deploymentProfile?{deploymentProfile:f.deploymentProfile}:{},...f.url?{url:f.url}:{},enqueueSource:"scheduler",...n.wpsuite?{wpsuite:n.wpsuite}:{},status:"queued",createdAt:new Date().toISOString(),createdBy:0};l.push(q),A.push(q),o.push({job:q,rule:f}),b.lastEnqueuedBucketByRuleId[f.id]=P,b.lastCreatedBucketByRuleId[f.id]=P,s+=1;}s>0&&await S(i,l),await S(X(e.runtimeDir),b);});for(let l of o){if(l.rule.command==="content-sync"){let y=d.get(l.rule.id);await I(e.runtimeDir,{eventType:"content-sync-demand-detected",status:"queued",actorSource:"queue-runner-scheduler",jobId:l.job.id,command:l.job.command,message:"Pending journal changes require a content-sync job.",details:{ruleId:l.rule.id,headSequence:y?.headSequence??0,committedSequence:y?.committedSequence??0}});}await I(e.runtimeDir,{eventType:"job-created",status:"queued",actorSource:"queue-runner-scheduler",jobId:l.job.id,command:l.job.command,message:"Scheduler auto-enqueued a job.",details:{ruleId:l.rule.id,intervalMinutes:l.rule.intervalMinutes,timezone:n.timezone,deploymentProfile:l.rule.deploymentProfile||"",url:l.rule.url||""}});}for(let l of u)await I(e.runtimeDir,{eventType:"content-sync-coalesced",status:"queued",actorSource:"queue-runner-scheduler",command:l.command,message:"Scheduler demand was coalesced into an existing content-sync job.",details:{ruleId:l.id}});for(let l of c){if(l.check.status==="baseline-required"){let y=ne(l.rule,n.publisherConfig);await qe(e,l.rule.id,y,l.check.reason||"A successful full or incremental publish must establish a new content-sync baseline.",l.check.headSequence,l.check.committedSequence);}await I(e.runtimeDir,{eventType:l.check.status==="baseline-required"?"content-sync-baseline-required":"content-sync-demand-check-failed",status:l.check.status==="unavailable"?"failed":"info",actorSource:"queue-runner-scheduler",command:"content-sync",message:l.check.reason||"The content-sync consumer is already at the current journal head.",details:{ruleId:l.rule.id,headSequence:l.check.headSequence??0,committedSequence:l.check.committedSequence??0}});}return s}async function Pt(e){let t={pid:process.pid,startedAt:new Date().toISOString()};await v.writeFile(e,JSON.stringify(t,null,2),{encoding:"utf8",flag:"wx"});}async function Ct(e){try{await v.unlink(e);}catch{}}function At(e,t){let n=[m.join(e,"dist",`${t}.js`),m.join(e,`${t}.js`)];for(let i of n)if(existsSync(i))return i;throw new Error(`Cannot find ${t}.js in ${m.join(e,"dist")} or ${e}`)}async function R(e,t,n,i,r,a,s){if(s){let c=await we(t,s);if(c)throw new O(c,n)}let o=At(e,n),u={...process.env,...a??{}};return u.STATIC_PUBLISHER_RUNTIME_DIR=t,r&&(u.PUBLISHER_CONFIG=r),await new Promise((c,d)=>{let g=null,p=null,h=null,l=null,y=false,b="",A=spawn(process.execPath,[o,...i],{cwd:e,env:u,stdio:["inherit","inherit","pipe"]});A.stderr?.on("data",P=>{b+=P.toString("utf8");});let f=()=>{p&&clearInterval(p),h&&clearInterval(h),l&&clearTimeout(l);},$=async()=>{if(!(!s||g||y)){y=true;try{let P=await we(t,s);if(!P)return;g=P,A.kill("SIGTERM"),l=setTimeout(()=>{A.kill("SIGKILL");},1e4);}finally{y=false;}}};s&&(p=setInterval(()=>{$();},1e3),h=setInterval(()=>{(async()=>{let P=Ae(t),x=await w(P,{});await S(P,{...x,checkedAt:new Date().toISOString(),status:"running",currentJobId:s||x.currentJobId||"",currentStep:n});})().catch(()=>{});},15e3)),A.on("error",P=>{f(),d(P);}),A.on("close",(P,x)=>{f();let T=ge(b).trim();if(T&&process.stderr.write(`${T}
|
|
3
|
+
`),g){d(new O(g,n));return}if(P===0)c();else {let L=T.split(/\r?\n/).map(q=>q.trim()).find(q=>q&&!q.startsWith("at "));d(new Error(`${n} exited with code ${P??-1}${x?` (signal ${x})`:""}${L?`: ${L}`:""}`));}});}),0}function It(e){let t=e.awsTempCreds;if(!t)return {};let n={};return typeof t.accessKeyId=="string"&&t.accessKeyId.trim()!==""&&(n.AWS_ACCESS_KEY_ID=t.accessKeyId.trim()),typeof t.secretAccessKey=="string"&&t.secretAccessKey.trim()!==""&&(n.AWS_SECRET_ACCESS_KEY=t.secretAccessKey.trim()),typeof t.sessionToken=="string"&&t.sessionToken.trim()!==""&&(n.AWS_SESSION_TOKEN=t.sessionToken.trim()),n}async function Rt(e,t){let n=s=>{if(!s||typeof s!="object")return null;let o=s,u={...o.siteSettings??{},...String(o.accountId??"").trim()&&!o.siteSettings?.accountId?{accountId:String(o.accountId).trim()}:{},...String(o.siteId??"").trim()&&!o.siteSettings?.siteId?{siteId:String(o.siteId).trim()}:{},...o.siteSettings?.subscriber===true||o.subscriber===true?{subscriber:true}:{}},c={...String(o.apiBase??"").trim()?{apiBase:String(o.apiBase).trim()}:{},...String(o.runtimeToken??o.nonce??"").trim()?{runtimeToken:String(o.runtimeToken??o.nonce).trim()}:{},...String(o.virtualAssetBaseUrl??o.uploadUrl??"").trim()?{virtualAssetBaseUrl:String(o.virtualAssetBaseUrl??o.uploadUrl).trim()}:{},...Object.keys(u).length>0?{siteSettings:u}:{},...o.subscriptionType?{subscriptionType:o.subscriptionType}:{}};return Object.keys(c).length>0?c:null},i=await Y(t.configPath).catch(()=>null),r=n(i?.wpsuite);if(r)return r;let a=n(e.wpsuite);return a||null}async function qt(e,t){return await Rt(e,t)}function ae(e){let t={...e,status:"queued"};return delete t.startedAt,delete t.endedAt,delete t.exitCode,delete t.error,delete t.stopRequestedAt,delete t.stopRequestedByUserId,delete t.stopRequestedByLogin,delete t.stopMode,delete t.stoppedStep,t}async function Dt(e,t){let n=await w(oe(e.runtimeDir),null),i=n&&n.details&&typeof n.details.phase=="string"?n.details.phase.trim():"";if(i)return i;let r=n&&typeof n.currentStep=="string"?n.currentStep.trim():"";if(r)return r;let a=await Ie(e).catch(()=>"");if(a){let s=await w(m.join(a,"current-crawl-event.json"),null),o=s&&typeof s.currentStep=="string"?s.currentStep.trim():"";if(o)return o}return t}function xt(e,t){if((e.command==="publish"||e.command==="crawl")&&t==="rewrite-text")return "rewrite-text"}async function Tt(e){let t=m.join(e.runtimeDir,"queue.json"),n=m.join(e.runtimeDir,"current-run.json"),i=await E(e.runtimeDir,async()=>{let r=await w(n,null);if(!r||r.status!=="running"&&r.status!=="queued")return null;let a=await w(t,[]),s=Array.isArray(a)?a.filter(u=>u?.id!==r.id):[],o=ae(r);return await S(t,[o,...s]),await S(n,null),o});i&&(await j(e.runtimeDir,i.id),await I(e.runtimeDir,{eventType:"job-recovered",status:"queued",actorSource:"queue-runner",jobId:i.id,command:i.command,message:"Recovered stale current-run entry back into queue."}));}async function kt(e,t,n){try{if((n.shouldEnforceSubscriptionOnJobExecution?.(e)??n.enforceSubscriptionOnJobExecution)&&n.assertActiveSubscriptionForIdentity){let o=await qt(e,t);await n.assertActiveSubscriptionForIdentity(o);}let r=It(e),a=e.resumeFromStep==="rewrite-text"?["--resume-rewrite"]:e.crawlMode==="incremental"?["--crawl-mode","incremental"]:[],s=e.deploymentProfile?["--profile",e.deploymentProfile]:[];if(e.command==="publish")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-release-cutoff"}),await R(t.exporterDir,t.runtimeDir,"content-sync",["--capture-baseline-cutoffs",e.id],t.configPath,r,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:e.resumeFromStep==="rewrite-text"?"rewrite-text":"crawl"}),await R(t.exporterDir,t.runtimeDir,"crawl",a,t.configPath,r,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await R(t.exporterDir,t.runtimeDir,"deploy",s,t.configPath,r,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await R(t.exporterDir,t.runtimeDir,"invalidate",s,t.configPath,r,e.id),await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync-baseline"}),await R(t.exporterDir,t.runtimeDir,"content-sync",["--establish-baselines",e.id],t.configPath,r,e.id),{exitCode:0};if(e.command==="crawl")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:e.resumeFromStep==="rewrite-text"?"rewrite-text":"crawl"}),await R(t.exporterDir,t.runtimeDir,"crawl",a,t.configPath,r,e.id),{exitCode:0};if(e.command==="deploy")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"deploy"}),await R(t.exporterDir,t.runtimeDir,"deploy",s,t.configPath,r,e.id),{exitCode:0};if(e.command==="invalidate")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"invalidate"}),await R(t.exporterDir,t.runtimeDir,"invalidate",s,t.configPath,r,e.id),{exitCode:0};if(e.command==="retry-timeouts")return await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"retry-timeouts"}),await R(t.exporterDir,t.runtimeDir,"crawl",["--retry-timeouts"],t.configPath,r,e.id),{exitCode:0};if(e.command==="url"){let o=(e.url||"").trim();return o?(await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"url"}),await R(t.exporterDir,t.runtimeDir,"crawl",["--url",o],t.configPath,r,e.id),{exitCode:0}):{exitCode:2,error:"Missing url for command 'url'"}}return e.command==="content-sync"?!e.ruleId||!e.coalesceKey?{exitCode:2,error:"Content-sync queue jobs require ruleId and coalesceKey."}:(await C(t,"running",{currentJobId:e.id,currentJobCommand:e.command,currentStep:"content-sync"}),await R(t.exporterDir,t.runtimeDir,"content-sync",["--job-id",e.id],t.configPath,r,e.id),{exitCode:0}):{exitCode:2,error:`Unsupported command: ${e.command}`}}catch(i){return i instanceof O?{exitCode:130,error:"Job stop requested.",stopped:true,stopRequest:i.request,stoppedStep:i.step}:{exitCode:1,error:i instanceof Error?i.message:String(i)}}}function De(e){return async function(n=process.argv.slice(2)){let i=at(n),r=m.join(i.runtimeDir,"export.lock"),a=m.join(i.runtimeDir,"last-run.json");await J(i.runtimeDir),await C(i,"starting",{message:"queue-runner starting"});try{await Pt(r);}catch(s){if((s&&typeof s=="object"&&"code"in s?String(s.code||""):"")!=="EEXIST")throw s;console.log("[queue-runner] lock active, skipping"),await C(i,"lock-active",{message:"lock active, skipped this cron tick"});return}try{await Tt(i);let s=await vt(i,e),o=0;for(let u=0;u<i.maxJobs;u++){let c=await Jt(i,e);if(c==="none"||(o+=1,c==="stopped"))break}if(o===0)await J(i.runtimeDir),await C(i,"idle",{processedJobs:o,schedulerEnqueued:s,message:"no queued jobs"});else {await J(i.runtimeDir);let u=await w(a,null),c=String(u?.status||"").trim();await C(i,c==="success"?"job-success":c==="stopped"?"job-stopped":"job-failed",{processedJobs:o,schedulerEnqueued:s,lastJobId:u?.id,lastJobCommand:u?.command,lastJobStatus:u?.status,lastJobExitCode:u?.exitCode,lastJobError:u?.error,...c==="stopped"?{currentStep:u?.stoppedStep,stopRequestedAt:u?.stopRequestedAt,stopRequestedByLogin:u?.stopRequestedByLogin,stopRequestedMode:u?.stopMode,lastStoppedStep:u?.stoppedStep,message:u?.stopMode==="requeue"?`Job stopped during ${u?.stoppedStep||u?.command||"active step"} and requeued.`:`Job stopped during ${u?.stoppedStep||u?.command||"active step"} and left out of queue.`}:{}});}}catch(s){let o=s instanceof Error?s.message:String(s);throw await C(i,"error",{message:o}),await I(i.runtimeDir,{eventType:"queue-runner-error",status:"failed",actorSource:"queue-runner",message:"Unhandled queue-runner error.",details:{error:o}}),s}finally{await Ct(r);}}}async function Et(e,t,n){let i=m.join(e.runtimeDir,"queue.json"),r=m.join(e.runtimeDir,"current-run.json");await E(e.runtimeDir,async()=>{let a=await w(i,[]),s=Array.isArray(a)?a.filter(c=>c?.id!==t.id):[],o=ae(t),u=xt(t,n);u?o.resumeFromStep=u:delete o.resumeFromStep,await S(i,[o,...s]),await S(r,null);});}async function Ft(e,t,n){let i=Math.max(0,t.attempt??0)+1,r=Math.min(3600*1e3,6e4*2**(i-1)),a=Math.floor(r*.2*Math.random()),s=new Date(Date.now()+r+a).toISOString(),o={...ae(t),status:"retry-wait",attempt:i,nextAttemptAt:s,error:n};if(await E(e.runtimeDir,async()=>{let u=m.join(e.runtimeDir,"queue.json"),c=m.join(e.runtimeDir,"current-run.json"),g=(await w(u,[])).filter(p=>p.id!==t.id);await S(u,[o,...g]),await S(c,null);}),t.coalesceKey){let u=m.join(e.runtimeDir,"content-sync-state.json"),d=(await w(u,{})).rules??{};d[t.coalesceKey]={...d[t.coalesceKey]??{},ruleId:t.ruleId||"",coalesceKey:t.coalesceKey,retryAttempt:i,nextRetryAt:s,lastError:n,trailingWorkDetected:true},await S(u,{schemaVersion:1,updatedAt:new Date().toISOString(),rules:d});}return o}async function jt(e,t){if(!t.coalesceKey||!t.ruleId||!(await w(m.join(e.runtimeDir,"content-sync-state.json"),null))?.rules?.[t.coalesceKey]?.trailingWorkDetected)return null;let i=null;await E(e.runtimeDir,async()=>{let a=m.join(e.runtimeDir,"queue.json"),s=await w(a,[]);se(s,{command:"content-sync",coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile})||(i={id:randomUUID(),command:"content-sync",ruleId:t.ruleId,coalesceKey:t.coalesceKey,deploymentProfile:t.deploymentProfile,enqueueSource:"scheduler",wpsuite:t.wpsuite,status:"queued",attempt:0,createdAt:new Date().toISOString(),createdBy:0},await S(a,[...s,i]));});let r=i;return r&&await I(e.runtimeDir,{eventType:"content-sync-trailing-job-created",status:"queued",actorSource:"queue-runner",jobId:r.id,command:r.command,message:"Content changes after the stable cutoff created one trailing job.",details:{predecessorJobId:t.id,ruleId:t.ruleId,coalesceKey:t.coalesceKey}}),r}async function Jt(e,t){let n=m.join(e.runtimeDir,"queue.json"),i=m.join(e.runtimeDir,"current-run.json"),r=m.join(e.runtimeDir,"last-run.json"),a=new Date().toISOString(),s=await E(e.runtimeDir,async()=>{let g=await w(n,[]);if(!Array.isArray(g)||g.length===0)return null;let p=Date.parse(String(g[0]?.nextAttemptAt||""));if(Number.isFinite(p)&&p>Date.now())return null;let h={...g[0],status:"running",startedAt:a};return delete h.nextAttemptAt,await S(n,g.slice(1)),await S(i,h),h});if(!s)return "none";await J(e.runtimeDir),await I(e.runtimeDir,{eventType:"job-run-started",status:"running",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Job execution started.",details:{createdAt:s.createdAt||"",startedAt:a,createdBy:s.createdBy??null,deploymentProfile:s.deploymentProfile||"",queuedWithTempAwsCreds:!!s.awsTempCreds}}),await C(e,"running",{currentJobId:s.id,currentJobCommand:s.command,currentStep:s.command});let o=await kt(s,e,t),u=new Date().toISOString();if(o.stopped){let g=await Dt(e,o.stoppedStep||s.command),p=o.stopRequest?.mode==="requeue"?"requeue":"stop",h={...s,status:"stopped",endedAt:u,exitCode:o.exitCode,...o.error?{error:o.error}:{},stopRequestedAt:o.stopRequest?.requestedAt||"",stopRequestedByUserId:typeof o.stopRequest?.requestedByUserId=="number"?o.stopRequest.requestedByUserId:null,stopRequestedByLogin:o.stopRequest?.requestedByLogin||"",stopMode:p,stoppedStep:g};p==="requeue"?await Et(e,s,g):await S(i,null);try{let l=await U(h,e);h.logArchiveDir=l.archiveDir,h.logArchiveCreatedAt=l.archiveCreatedAt,h.logArchiveFileCount=l.archivedFiles.length;}catch(l){h.logArchiveError=l instanceof Error?l.message:String(l);}return await S(r,h),await j(e.runtimeDir,s.id),await J(e.runtimeDir),await I(e.runtimeDir,{eventType:"job-run-stopped",status:"stopped",actorSource:"queue-runner",jobId:s.id,command:s.command,message:p==="requeue"?"Job stop requested and requeued.":"Job stop requested and removed from active execution without requeue.",details:{startedAt:s.startedAt||"",endedAt:u,deploymentProfile:s.deploymentProfile||"",stopMode:p,stopRequestedAt:o.stopRequest?.requestedAt||"",stopRequestedByLogin:o.stopRequest?.requestedByLogin||"",stopRequestedByUserId:typeof o.stopRequest?.requestedByUserId=="number"?o.stopRequest.requestedByUserId:null,stoppedStep:g,logArchiveKey:h.logArchiveError||!h.logArchiveDir?"":m.basename(h.logArchiveDir),logArchiveDir:h.logArchiveDir||"",logArchiveCreatedAt:h.logArchiveCreatedAt||"",logArchiveFileCount:h.logArchiveFileCount??0,logArchiveError:h.logArchiveError||""}}),"stopped"}if(s.command==="content-sync"&&o.exitCode!==0){if(/baseline (?:is missing or stale|required)|verified content-sync baseline|new baseline/i.test(o.error||"")){let l="The installed release no longer matches the verified content-sync baseline. Run a successful full or incremental publish to establish a new baseline.";s.ruleId&&s.coalesceKey&&await qe(e,s.ruleId,s.coalesceKey,l);let y={...s,status:"failed",endedAt:u,exitCode:o.exitCode,error:l};try{let b=await U(y,e);y.logArchiveDir=b.archiveDir,y.logArchiveCreatedAt=b.archiveCreatedAt,y.logArchiveFileCount=b.archivedFiles.length;}catch(b){y.logArchiveError=b instanceof Error?b.message:String(b);}return await S(r,y),await j(e.runtimeDir,s.id),await I(e.runtimeDir,{eventType:"content-sync-baseline-required",status:"failed",actorSource:"queue-runner",jobId:s.id,command:s.command,message:l,details:{ruleId:s.ruleId||"",retryScheduled:false}}),"processed"}await I(e.runtimeDir,{eventType:"content-sync-failed",status:"failed",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Content-sync execution failed before cursor acknowledgement.",details:{attempt:s.attempt??0,error:o.error||"Content-sync execution failed."}});let p=await Ft(e,s,o.error||"Content-sync execution failed."),h={...s,status:"retry-wait",endedAt:u,exitCode:o.exitCode,error:p.error,attempt:p.attempt,nextAttemptAt:p.nextAttemptAt};try{let l=await U(h,e);h.logArchiveDir=l.archiveDir,h.logArchiveCreatedAt=l.archiveCreatedAt,h.logArchiveFileCount=l.archivedFiles.length;}catch(l){h.logArchiveError=l instanceof Error?l.message:String(l);}return await S(r,h),await j(e.runtimeDir,s.id),await I(e.runtimeDir,{eventType:"content-sync-retry-scheduled",status:"retry-wait",actorSource:"queue-runner",jobId:s.id,command:s.command,message:"Content-sync retry was scheduled with bounded backoff.",details:{attempt:p.attempt??0,nextAttemptAt:p.nextAttemptAt||"",error:p.error||""}}),"processed"}let c={...s,status:o.exitCode===0?"success":"failed",endedAt:u,exitCode:o.exitCode,...o.error?{error:o.error}:{}};await S(i,null),c.status==="success"&&c.command==="content-sync"&&await jt(e,c),await j(e.runtimeDir,s.id);try{let g=await U(c,e);c.logArchiveDir=g.archiveDir,c.logArchiveCreatedAt=g.archiveCreatedAt,c.logArchiveFileCount=g.archivedFiles.length;}catch(g){c.logArchiveError=g instanceof Error?g.message:String(g);}await S(r,c);let d=s.startedAt&&c.endedAt?Math.max(0,Math.round((new Date(c.endedAt).getTime()-new Date(s.startedAt).getTime())/1e3)):void 0;return await I(e.runtimeDir,{eventType:"job-run-finished",status:c.status==="success"?"success":"failed",actorSource:"queue-runner",jobId:c.id,command:c.command,message:c.status==="success"?"Job execution finished successfully.":"Job execution finished with failure.",details:{startedAt:s.startedAt||"",endedAt:c.endedAt||"",durationSec:d,deploymentProfile:c.deploymentProfile||"",exitCode:c.exitCode??null,error:c.error||"",logArchiveKey:c.logArchiveError||!c.logArchiveDir?"":m.basename(c.logArchiveDir),logArchiveDir:c.logArchiveDir||"",logArchiveCreatedAt:c.logArchiveCreatedAt||"",logArchiveFileCount:c.logArchiveFileCount??0,logArchiveError:c.logArchiveError||""}}),"processed"}async function Lt(e){if(!e)throw new Error("Missing remote WPSuite publisher state. Refresh the runtime config before running subscription-gated jobs.");if(e.subscriptionType!=="PROFESSIONAL"&&e.subscriptionType!=="AGENCY")throw new Error("This job requires an active WPSuite publisher subscription in the remote site configuration.")}var Bt={allowSchedulerAutoEnqueue:true,enforceSubscriptionOnJobExecution:false,shouldEnforceSubscriptionOnJobExecution:e=>Pe(e)||e.enqueueSource==="scheduler"||e.createdBy===0,assertActiveSubscriptionForIdentity:Lt},xe=De(Bt),Mt=xe,Sn=xe;process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href&&Mt().catch(e=>{console.error(e),process.exit(1);});export{Sn as default,Mt as main,xe as runQueueRunner};
|
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.43",
|
|
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.",
|