repomix
Version:
A tool to pack repository contents to single file for AI consumption
65 lines (64 loc) • 2.44 kB
JavaScript
const MIN_BASE64_LENGTH_DATA_URI = 40;
const MIN_BASE64_LENGTH_STANDALONE = 256;
const TRUNCATION_LENGTH = 32;
const MIN_CHAR_DIVERSITY = 10;
const MIN_CHAR_TYPE_COUNT = 3;
const dataUriPattern = new RegExp(`data:([a-zA-Z0-9\\/\\-\\+]+)(;[a-zA-Z0-9\\-=]+)*;base64,([A-Za-z0-9+/=]{${MIN_BASE64_LENGTH_DATA_URI},})`, 'g');
const standaloneBase64Pattern = new RegExp(`([A-Za-z0-9+/]{${MIN_BASE64_LENGTH_STANDALONE},}={0,2})`, 'g');
const hasLongBase64Run = (content) => {
const len = content.length;
if (len < MIN_BASE64_LENGTH_STANDALONE)
return false;
let run = 0;
for (let i = 0; i < len; i++) {
const c = content.charCodeAt(i);
if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (c >= 48 && c <= 57) || c === 43 || c === 47) {
run++;
if (run >= MIN_BASE64_LENGTH_STANDALONE)
return true;
}
else {
run = 0;
}
}
return false;
};
export const truncateBase64Content = (content) => {
let processedContent = content;
if (content.includes(';base64,')) {
dataUriPattern.lastIndex = 0;
processedContent = processedContent.replace(dataUriPattern, (_match, mimeType, params, base64Data) => {
const preview = base64Data.substring(0, TRUNCATION_LENGTH);
return `data:${mimeType}${params || ''};base64,${preview}...`;
});
}
if (hasLongBase64Run(processedContent)) {
standaloneBase64Pattern.lastIndex = 0;
processedContent = processedContent.replace(standaloneBase64Pattern, (match, base64String) => {
if (isLikelyBase64(base64String)) {
const preview = base64String.substring(0, TRUNCATION_LENGTH);
return `${preview}...`;
}
return match;
});
}
return processedContent;
};
function isLikelyBase64(str) {
if (!/^[A-Za-z0-9+/]+=*$/.test(str)) {
return false;
}
const charSet = new Set(str);
if (charSet.size < MIN_CHAR_DIVERSITY) {
return false;
}
const hasNumbers = /[0-9]/.test(str);
const hasUpperCase = /[A-Z]/.test(str);
const hasLowerCase = /[a-z]/.test(str);
const hasSpecialChars = /[+/]/.test(str);
if (!hasNumbers) {
return false;
}
const charTypeCount = [hasNumbers, hasUpperCase, hasLowerCase, hasSpecialChars].filter(Boolean).length;
return charTypeCount >= MIN_CHAR_TYPE_COUNT;
}