@@ -5,17 +5,41 @@ import { getColor, getPastel } from '@/lib/colors';
55
66const lib = lorelei ;
77
8+ // ✅ Modern UTF-8 safe base64 encoder (no deprecated APIs)
9+ function toBase64 ( str : string ) : string {
10+ if ( typeof window === 'undefined' ) {
11+ // Server (Node.js)
12+ return Buffer . from ( str , 'utf-8' ) . toString ( 'base64' ) ;
13+ } else {
14+ // Browser (UTF-8 safe)
15+ const encoder = new TextEncoder ( ) ;
16+ const bytes = encoder . encode ( str ) ;
17+ let binary = '' ;
18+ const chunkSize = 0x8000 ;
19+
20+ for ( let i = 0 ; i < bytes . length ; i += chunkSize ) {
21+ const chunk = bytes . subarray ( i , i + chunkSize ) ;
22+ binary += String . fromCharCode ( ...chunk ) ;
23+ }
24+
25+ return btoa ( binary ) ;
26+ }
27+ }
28+
829export function Avatar ( { seed, size = 128 , ...props } : { seed : string ; size ?: number } ) {
930 const backgroundColor = getPastel ( getColor ( seed ) , 4 ) ;
1031
1132 const avatar = useMemo ( ( ) => {
12- return createAvatar ( lib , {
33+ const svg = createAvatar ( lib , {
1334 ...props ,
1435 seed,
1536 size,
1637 backgroundColor : [ backgroundColor ] ,
17- } ) . toDataUri ( ) ;
18- } , [ ] ) ;
38+ } ) . toString ( ) ;
39+
40+ const base64 = toBase64 ( svg ) ;
41+ return `data:image/svg+xml;base64,${ base64 } ` ;
42+ } , [ seed , size , backgroundColor , props ] ) ;
1943
2044 return < img src = { avatar } alt = "Avatar" style = { { borderRadius : '100%' , width : size } } /> ;
2145}
0 commit comments