4cko
/usr/local/rvm/gems/ruby-3.4.7/bin:/usr/local/rvm/gems/ruby-3.4.7@global/bin:/usr/local/rvm/rubies/ruby-3.4.7/bin:/home/codespace/.vscode-remote/data/User/globalStorage/github.copilot-chat/debugCommand:/home/codespace/.vscode-remote/data/User/globalStorage/github.copilot-chat/copilotCli:/vscode/bin/linux-x64/7e7950df89d055b5a378379db9ee14290772148a/bin/remote-cli:/home/codespace/.local/bin:/home/codespace/.dotnet:/home/codespace/nvm/current/bin:/home/codespace/.php/current/bin:/home/codespace/.python/current/bin:/home/codespace/java/current/bin:/home/codespace/.ruby/current/bin:/home/codespace/.local/bin:/usr/local/python/current/bin:/usr/local/py-utils/bin:/usr/local/jupyter:/usr/local/oryx:/usr/local/go/bin:/go/bin:/usr/local/sdkman/bin:/usr/local/sdkman/candidates/java/current/bin:/usr/local/sdkman/candidates/gradle/current/bin:/usr/local/sdkman/candidates/maven/current/bin:/usr/local/sdkman/candidates/ant/current/bin:/usr/local/rvm/gems/default/bin:/usr/local/rvm/gems/default@global/bin:/usr/local/rvm/rubies/default/bin:/usr/local/share/rbenv/bin:/usr/local/php/current/bin:/opt/conda/bin:/usr/local/nvs:/usr/local/share/nvm/versions/node/v24.14.0/bin:/usr/local/hugo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/share/dotnet:/home/codespace/.dotnet/tools:/usr/local/rvm/bin
a2c64e7 | /** | |
| * Estimate decoded byte length of a data:// URL *without* allocating large buffers. | |
| * - For base64: compute exact decoded size using length and padding; | |
| * handle %XX at the character-count level (no string allocation). | |
| * - For non-base64: compute the exact percent-decoded UTF-8 byte length. | |
| * | |
| * @param {string} url | |
| * @returns {number} | |
| */ | |
| const isHexDigit = (charCode) => | |
| (charCode >= 48 && charCode <= 57) || | |
| (charCode >= 65 && charCode <= 70) || | |
| (charCode >= 97 && charCode <= 102); | |
| const isPercentEncodedByte = (str, i, len) => | |
| i + 2 < len && isHexDigit(str.charCodeAt(i + 1)) && isHexDigit(str.charCodeAt(i + 2)); | |
| export default function estimateDataURLDecodedBytes(url) { | |
| if (!url || typeof url !== 'string') return 0; | |
| if (!url.startsWith('data:')) return 0; | |
| const comma = url.indexOf(','); | |
| if (comma < 0) return 0; | |
| const meta = url.slice(5, comma); | |
| const body = url.slice(comma + 1); | |
| const isBase64 = /;base64/i.test(meta); | |
| if (isBase64) { | |
| let effectiveLen = body.length; | |
| const len = body.length; // cache length | |
| for (let i = 0; i < len; i++) { | |
| if (body.charCodeAt(i) === 37 /* '%' */ && i + 2 < len) { | |
| const a = body.charCodeAt(i + 1); | |
| const b = body.charCodeAt(i + 2); | |
| const isHex = isHexDigit(a) && isHexDigit(b); | |
| if (isHex) { | |
| effectiveLen -= 2; | |
| i += 2; | |
| } | |
| } | |
| } | |
| let pad = 0; | |
| let idx = len - 1; | |
| const tailIsPct3D = (j) => | |
| j >= 2 && | |
| body.charCodeAt(j - 2) === 37 && // '%' | |
| body.charCodeAt(j - 1) === 51 && // '3' | |
| (body.charCodeAt(j) === 68 || body.charCodeAt(j) === 100); // 'D' or 'd' | |
| if (idx >= 0) { | |
| if (body.charCodeAt(idx) === 61 /* '=' */) { | |
| pad++; | |
| idx--; | |
| } else if (tailIsPct3D(idx)) { | |
| pad++; | |
| idx -= 3; | |
| } | |
| } | |
| if (pad === 1 && idx >= 0) { | |
| if (body.charCodeAt(idx) === 61 /* '=' */) { | |
| pad++; | |
| } else if (tailIsPct3D(idx)) { | |
| pad++; | |
| } | |
| } | |
| const groups = Math.floor(effectiveLen / 4); | |
| const bytes = groups * 3 - (pad || 0); | |
| return bytes > 0 ? bytes : 0; | |
| } | |
| // Compute UTF-8 byte length directly from UTF-16 code units without allocating | |
| // a byte buffer (TextEncoder.encode would defeat the DoS guard on large bodies). | |
| // Valid %XX triplets count as one decoded byte; this matches the bytes that | |
| // decodeURIComponent(body) would produce before Buffer re-encodes the string. | |
| let bytes = 0; | |
| for (let i = 0, len = body.length; i < len; i++) { | |
| const c = body.charCodeAt(i); | |
| if (c === 37 /* '%' */ && isPercentEncodedByte(body, i, len)) { | |
| bytes += 1; | |
| i += 2; | |
| } else if (c < 0x80) { | |
| bytes += 1; | |
| } else if (c < 0x800) { | |
| bytes += 2; | |
| } else if (c >= 0xd800 && c <= 0xdbff && i + 1 < len) { | |
| const next = body.charCodeAt(i + 1); | |
| if (next >= 0xdc00 && next <= 0xdfff) { | |
| bytes += 4; | |
| i++; | |
| } else { | |
| bytes += 3; | |
| } | |
| } else { | |
| bytes += 3; | |
| } | |
| } | |
| return bytes; | |
| } | |