datocms-plugin-asset-optimization 0.7.7 → 0.7.8

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.
@@ -0,0 +1 @@
1
+ import{b as y,A as g}from"./index-CV8dvyAg.js";const w=t=>new Promise(e=>setTimeout(e,t)),R=(t,e,s,r)=>{const c=e*r**t;return Math.min(c,s)},C=(t,e)=>{const s=t?Number.parseInt(t,10):0;return s>0?s*1e3:R(e.retryCount,e.initialRetryDelay,e.maxRetryDelay,e.retryBackoffFactor)},q=(t,e,s)=>y({apiToken:t,environment:e,autoRetry:!1,...s?{baseUrl:s}:{}}),v=t=>t instanceof g&&t.response.status===429,b=t=>t.response.headers["retry-after"]??t.response.headers["x-ratelimit-reset"],$=t=>{const e={};for(const[s,r]of Object.entries(t))typeof r=="string"?e[s]=r:(typeof r=="number"||typeof r=="boolean")&&(e[s]=String(r));return e},A=async(t,e,s)=>{const r=await fetch(t);if(!r.ok)throw new Error(`Failed to fetch image from URL: ${r.status} ${r.statusText}`);const l=await(await r.blob()).arrayBuffer(),i=new Uint8Array(l),a=await fetch(e,{method:"PUT",headers:{...s,"Content-Length":i.length.toString()},body:i});if(!a.ok)throw new Error(`Failed to upload file to S3: ${a.status} ${a.statusText}`)};async function p(t,e,s,r,c,l,i=0,a=1e3,u=6e4,h=2){if(console.log(`Replacing DatoCMS asset ID ${t} with image from URL: ${e}${i>0?` (retry ${i})`:""}`),!t||!s)throw new Error("Missing required parameters: assetId and apiToken are required");const d={retryCount:i,initialRetryDelay:a,maxRetryDelay:u,retryBackoffFactor:h},m=async(o=1)=>p(t,e,s,r,c,l,i+o,a,u,h);try{const o=q(s,r,c),n=await o.uploadRequest.create({filename:l||"optimized-image.jpg"});await A(e,n.url,$(n.request_headers));const f=await o.uploads.rawUpdate(t,{data:{id:t,type:"upload",attributes:{path:n.id}}});return console.log("Asset replaced successfully:",f),f}catch(o){if(v(o)&&i<5){const n=C(b(o),d);return console.error(`Rate limit exceeded while replacing asset. Retrying in ${n}ms...`),await w(n),m()}throw console.error("Error replacing asset:",o),o}}class D{queue=[];activeCount=0;config;processing=!1;completedCount=0;failedCount=0;resolvePromise=null;constructor(e){this.config={...e,concurrency:e.concurrency||3,initialRetryDelay:e.initialRetryDelay||1e3,maxRetryDelay:e.maxRetryDelay||6e4,retryBackoffFactor:e.retryBackoffFactor||2}}addTask(e,s,r){this.queue.push({assetId:e,newImageUrl:s,filename:r,retryCount:0})}async processTask(e){try{await p(e.assetId,e.newImageUrl,this.config.apiToken,this.config.environment,this.config.baseUrl,e.filename,e.retryCount,this.config.initialRetryDelay,this.config.maxRetryDelay,this.config.retryBackoffFactor),this.completedCount++}catch(s){console.error(`Failed to replace asset ${e.assetId} after multiple retries:`,s),this.failedCount++}finally{this.activeCount--,this.processQueue()}}processQueue(){if(this.queue.length===0&&this.activeCount===0&&this.resolvePromise){this.resolvePromise({succeeded:this.completedCount,failed:this.failedCount}),this.resolvePromise=null,this.processing=!1;return}for(;this.queue.length>0&&this.activeCount<(this.config.concurrency||3);){const e=this.queue.shift();e&&(this.activeCount++,this.processTask(e))}}start(){return this.processing?Promise.reject(new Error("Asset replacer is already processing")):(this.processing=!0,this.completedCount=0,this.failedCount=0,new Promise(e=>{this.resolvePromise=e,this.processQueue()}))}getStatus(){return{queued:this.queue.length,active:this.activeCount,completed:this.completedCount,failed:this.failedCount}}}export{D as AssetReplacer,p as default};