You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My current workaround is to detect if it's data: url, download it to the assets folder and rewrite the url of the request as it's trying local file and deleting that file after that.
import{createApp,toWebHandler}from"h3";import{createIPX,createIPXH3App,createIPXH3Handler,ipxFSStorage,ipxHttpStorage,}from"ipx";import{rmasremove}from"node:fs/promises";constipx=createIPX({storage: ipxFSStorage({dir: "./assets"}),httpStorage: ipxHttpStorage({allowAllDomains: true,maxAge: 60*60*24}),});exportconstapp=createApp().use("/optimize",createIPXH3Handler(ipx));consthandler=toWebHandler(app);Bun.serve({port: 3000,asyncfetch(req){leturl=newURL(req.url);if(url.pathname.startsWith("/optimize")){const[_,seg]=url.pathname.split("/optimize/");constindex=seg.indexOf("data");constfpath=index===-1 ? seg : seg.slice(index);letdigest;// download it locally to the assets folder, then delete// at least until https://github.com/unjs/ipx/issues/244if(fpath.startsWith("data:")){constbuf=awaitfetch(fpath).then((res)=>res.arrayBuffer());digest=awaitcreateDigest(newUint8Array(buf));awaitBun.write(`./assets/${digest}`,buf);constresp=awaithandler(newRequest(`${url.origin}/optimize/${seg.slice(0,index-1)}/${digest}`,req,),);remove(`./assets/${digest}`);returnresp;}returnhandler(req);}returnnewResponse("Not found",{status: 404});},});asyncfunctioncreateDigest(msg: string|Uint8Array,algo: "SHA-1"|"SHA-256"|"SHA-512"="SHA-256",){constdata=typeofmsg==="string" ? newTextEncoder().encode(msg) : msg;consthashBuffer=awaitcrypto.subtle.digest(algo,data);consthashArray=Array.from(newUint8Array(hashBuffer));consthashHex=hashArray.map((b)=>b.toString(16).padStart(2,"0")).join("");returnhashHex;}
Describe the feature
It would be pretty basic option, basically skipping the
url.hostname
check invalidateId
or the validation altogther https://github.com/unjs/ipx/blob/main/src/storage/http.ts#99Everything else can stay the same and actually work, because Fetch can fetch data URLs.
Additional information
example URI
And of course, there could be URL limit hit this way, but for small images it's okay (as above)
Other way would be to expose a similar thing programmatically accepting buffers.
The text was updated successfully, but these errors were encountered: