MCPcopy
hub / github.com/axios/axios / estimateDataURLDecodedBytes

Function estimateDataURLDecodedBytes

lib/helpers/estimateDataURLDecodedBytes.js:18–105  ·  view source on GitHub ↗
(url)

Source from the content-addressed store, hash-verified

16 i + 2 < len && isHexDigit(str.charCodeAt(i + 1)) && isHexDigit(str.charCodeAt(i + 2));
17
18export default function estimateDataURLDecodedBytes(url) {
19 if (!url || typeof url !== 'string') return 0;
20 if (!url.startsWith('data:')) return 0;
21
22 const comma = url.indexOf(',');
23 if (comma < 0) return 0;
24
25 const meta = url.slice(5, comma);
26 const body = url.slice(comma + 1);
27 const isBase64 = /;base64/i.test(meta);
28
29 if (isBase64) {
30 let effectiveLen = body.length;
31 const len = body.length; // cache length
32
33 for (let i = 0; i < len; i++) {
34 if (body.charCodeAt(i) === 37 /* '%' */ && i + 2 < len) {
35 const a = body.charCodeAt(i + 1);
36 const b = body.charCodeAt(i + 2);
37 const isHex = isHexDigit(a) && isHexDigit(b);
38
39 if (isHex) {
40 effectiveLen -= 2;
41 i += 2;
42 }
43 }
44 }
45
46 let pad = 0;
47 let idx = len - 1;
48
49 const tailIsPct3D = (j) =>
50 j >= 2 &&
51 body.charCodeAt(j - 2) === 37 && // '%'
52 body.charCodeAt(j - 1) === 51 && // '3'
53 (body.charCodeAt(j) === 68 || body.charCodeAt(j) === 100); // 'D' or 'd'
54
55 if (idx >= 0) {
56 if (body.charCodeAt(idx) === 61 /* '=' */) {
57 pad++;
58 idx--;
59 } else if (tailIsPct3D(idx)) {
60 pad++;
61 idx -= 3;
62 }
63 }
64
65 if (pad === 1 && idx >= 0) {
66 if (body.charCodeAt(idx) === 61 /* '=' */) {
67 pad++;
68 } else if (tailIsPct3D(idx)) {
69 pad++;
70 }
71 }
72
73 const groups = Math.floor(effectiveLen / 4);
74 const bytes = groups * 3 - (pad || 0);
75 return bytes > 0 ? bytes : 0;

Callers 3

factoryFunction · 0.85
http.jsFile · 0.85

Calls 3

isHexDigitFunction · 0.85
tailIsPct3DFunction · 0.85
isPercentEncodedByteFunction · 0.85

Tested by

no test coverage detected