UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines 1.55 kB
import{isAttachmentRefUrl,parseAttachmentRef}from"#internal/attachments/refs.js";async function fileDataToBytes(e){return typeof Buffer<`u`&&Buffer.isBuffer(e)?e:e instanceof Uint8Array?Buffer.from(e):e instanceof ArrayBuffer?Buffer.from(new Uint8Array(e)):typeof e==`string`?decodeStringData(e):e instanceof URL&&e.protocol===`data:`?decodeStringData(e.href):null}function getKnownByteLength(n){return typeof Buffer<`u`&&Buffer.isBuffer(n)||n instanceof Uint8Array||n instanceof ArrayBuffer?n.byteLength:typeof n==`string`?computeStringByteLength(n):isAttachmentRefUrl(n)?parseAttachmentRef(n).size??null:n instanceof URL&&n.protocol===`data:`?computeStringByteLength(n.href):null}function decodeStringData(e){if(e.startsWith(`data:`)){let t=e.indexOf(`,`);if(t===-1)return null;let n=e.slice(5,t),r=e.slice(t+1);return n.endsWith(`;base64`)?Buffer.from(r,`base64`):Buffer.from(decodeURIComponent(r),`utf8`)}return/^https?:\/\//.test(e)?null:Buffer.from(e,`base64`)}function computeStringByteLength(e){if(e.startsWith(`data:`)){let t=e.indexOf(`,`);if(t===-1)return null;let n=e.slice(5,t),r=e.slice(t+1);if(n.endsWith(`;base64`))return estimateBase64ByteLength(r);try{return Buffer.byteLength(decodeURIComponent(r),`utf8`)}catch{return Buffer.byteLength(r,`utf8`)}}return/^https?:\/\//.test(e)?null:estimateBase64ByteLength(e)}function estimateBase64ByteLength(e){let t=e.trimEnd();if(t.length===0)return 0;let n=0;return t.endsWith(`==`)?n=2:t.endsWith(`=`)&&(n=1),Math.max(0,Math.floor(t.length*3/4)-n)}export{fileDataToBytes,getKnownByteLength};