opusdev/vector-similarity-api
1
1/**2 * Estimate decoded byte length of a data:// URL *without* allocating large buffers.3 * - For base64: compute exact decoded size using length and padding;4 * handle %XX at the character-count level (no string allocation).5 * - For non-base64: use UTF-8 byteLength of the encoded body as a safe upper bound.6 *7 * @param {string} url8 * @returns {number}9 */10export default function estimateDataURLDecodedBytes(url) {11 if (!url || typeof url !== 'string') return 0;12 if (!url.startsWith('data:')) return 0;13 14 const comma = url.indexOf(',');15 if (comma < 0) return 0;16 17 const meta = url.slice(5, comma);18 const body = url.slice(comma + 1);19 const isBase64 = /;base64/i.test(meta);20 21 if (isBase64) {22 let effectiveLen = body.length;23 const len = body.length; // cache length24 25 for (let i = 0; i < len; i++) {26 if (body.charCodeAt(i) === 37 /* '%' */ && i + 2 < len) {27 const a = body.charCodeAt(i + 1);28 const b = body.charCodeAt(i + 2);29 const isHex =30 ((a >= 48 && a <= 57) || (a >= 65 && a <= 70) || (a >= 97 && a <= 102)) &&31 ((b >= 48 && b <= 57) || (b >= 65 && b <= 70) || (b >= 97 && b <= 102));32 33 if (isHex) {34 effectiveLen -= 2;35 i += 2;36 }37 }38 }39 40 let pad = 0;41 let idx = len - 1;42 43 const tailIsPct3D = (j) =>44 j >= 2 &&45 body.charCodeAt(j - 2) === 37 && // '%'46 body.charCodeAt(j - 1) === 51 && // '3'47 (body.charCodeAt(j) === 68 || body.charCodeAt(j) === 100); // 'D' or 'd'48 49 if (idx >= 0) {50 if (body.charCodeAt(idx) === 61 /* '=' */) {51 pad++;52 idx--;53 } else if (tailIsPct3D(idx)) {54 pad++;55 idx -= 3;56 }57 }58 59 if (pad === 1 && idx >= 0) {60 if (body.charCodeAt(idx) === 61 /* '=' */) {61 pad++;62 } else if (tailIsPct3D(idx)) {63 pad++;64 }65 }66 67 const groups = Math.floor(effectiveLen / 4);68 const bytes = groups * 3 - (pad || 0);69 return bytes > 0 ? bytes : 0;70 }71 72 return Buffer.byteLength(body, 'utf8');73}74 