mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-13 23:11:40 +00:00
address greptile review feedback (greploop iteration 4)
- utf8ToBase64: use chunked String.fromCharCode to avoid O(n²) string concatenation for large payloads - sanitizeImageSrc: split data URI pathname on both ';' and ',' to correctly extract MIME type from non-base64 inline data URIs Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
75ed872643
commit
8180f0dfc1
2 changed files with 10 additions and 6 deletions
|
|
@ -21,9 +21,11 @@ export const sanitizeImageSrc = (url: string | undefined): string => {
|
|||
) {
|
||||
return parsed.href;
|
||||
}
|
||||
// Restrict data: URIs to image and PDF MIME types only
|
||||
// Restrict data: URIs to image and PDF MIME types only.
|
||||
// Split on both ';' and ',' to handle both `data:type;base64,...`
|
||||
// and `data:type,...` (non-base64 inline) formats.
|
||||
if (proto === "data:") {
|
||||
const mime = parsed.pathname.split(";")[0].toLowerCase();
|
||||
const mime = parsed.pathname.split(/[;,]/)[0].toLowerCase();
|
||||
if (mime.startsWith("image/") || mime === "application/pdf") {
|
||||
return parsed.href;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,11 +14,13 @@
|
|||
*/
|
||||
function utf8ToBase64(str: string): string {
|
||||
const bytes = new TextEncoder().encode(str);
|
||||
let binary = "";
|
||||
for (const b of bytes) {
|
||||
binary += String.fromCharCode(b);
|
||||
// Use chunked String.fromCharCode to avoid O(n²) string concatenation
|
||||
const chunks: string[] = [];
|
||||
const chunkSize = 8192;
|
||||
for (let i = 0; i < bytes.length; i += chunkSize) {
|
||||
chunks.push(String.fromCharCode(...bytes.subarray(i, i + chunkSize)));
|
||||
}
|
||||
return btoa(binary);
|
||||
return btoa(chunks.join(""));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue