source: frontend/node_modules/axios/lib/helpers/estimateDataURLDecodedBytes.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.8 KB
Line 
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} url
8 * @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 length
24
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 if (typeof Buffer !== 'undefined' && typeof Buffer.byteLength === 'function') {
73 return Buffer.byteLength(body, 'utf8');
74 }
75
76 // Compute UTF-8 byte length directly from UTF-16 code units without allocating
77 // a byte buffer (TextEncoder.encode would defeat the DoS guard on large bodies).
78 // Using body.length here would undercount non-ASCII (e.g. '€' is 1 code unit
79 // but 3 UTF-8 bytes).
80 let bytes = 0;
81 for (let i = 0, len = body.length; i < len; i++) {
82 const c = body.charCodeAt(i);
83 if (c < 0x80) {
84 bytes += 1;
85 } else if (c < 0x800) {
86 bytes += 2;
87 } else if (c >= 0xd800 && c <= 0xdbff && i + 1 < len) {
88 const next = body.charCodeAt(i + 1);
89 if (next >= 0xdc00 && next <= 0xdfff) {
90 bytes += 4;
91 i++;
92 } else {
93 bytes += 3;
94 }
95 } else {
96 bytes += 3;
97 }
98 }
99 return bytes;
100}
Note: See TracBrowser for help on using the repository browser.