Problem Description
The avatar image fetching in src/services/github.service.js has no safeguards against large images or slow responses, leading to potential performance degradation and massive SVG file sizes:
- No timeout - Avatar fetch can hang indefinitely
- No size limit - Can fetch arbitrarily large images
- No image optimization - Converts entire image to base64 without compression
- No dimension validation - No check for image resolution
- Cached with profile - Large images stored in cache, consuming memory
Root Cause
The fetchAvatarDataUri function (lines 112-136) lacks:
- Request timeout mechanism
- Maximum file size validation
- Image dimension limits
- Image compression/optimization
- Fallback to default avatar on failure
Impact
Performance Degradation:
- Large avatar images (several MB) converted to base64
- SVG files can become 5-10MB instead of ~50KB
- Slow SVG rendering in browsers
- Increased bandwidth usage
- Poor user experience on slow connections
Cache Bloat:
- Large avatar data URIs stored in cache
- Consumes significant memory per cached entry
- Reduces effective cache capacity
- With 1000 cache entries and 5MB avatars = 5GB memory usage
Request Hanging:
- No timeout on avatar fetch
- If GitHub CDN is slow, request hangs
- Blocks entire profile generation
- Cascading performance issues
Current Code Evidence:
// src/services/github.service.js (lines 112-136)
async function fetchAvatarDataUri(avatarUrl) {
if (!avatarUrl) {
return null;
}
try {
const response = await fetch(avatarUrl, { // NO TIMEOUT
headers: {
'User-Agent': 'samdev-pulse',
'Accept': 'image/*',
},
});
if (!response.ok) {
throw new Error(`Avatar fetch error: ${response.status}`);
}
const contentType = response.headers.get('content-type') || 'image/png';
const buffer = Buffer.from(await response.arrayBuffer()); // NO SIZE LIMIT
const base64 = buffer.toString('base64'); // NO COMPRESSION
return `data:${contentType};base64,${base64}`;
} catch {
return null;
}
}
Proposed Solution
- Add timeout to avatar fetch (5 seconds)
- Add size limit (max 500KB)
- Add dimension validation (max 512x512px)
- Add image compression using sharp library
- Add fallback to default avatar on oversized images
- Add cache key separation for avatar data
Files to Modify
- src/services/github.service.js - Add timeout, size limit, compression
- package.json - Add sharp dependency for image processing
- .env.example - Add AVATAR_MAX_SIZE configuration
Acceptance Criteria
✅ Avatar fetch has 5-second timeout
✅ Avatar images limited to 500KB max
✅ Avatar images resized to max 512x512px
✅ Images compressed before base64 conversion
✅ Fallback to default avatar on oversized images
✅ SVG file sizes reduced by 90%+ for large avatars
✅ Cache memory usage reduced significantly
✅ Unit tests for size/dimension validation
Problem Description
The avatar image fetching in src/services/github.service.js has no safeguards against large images or slow responses, leading to potential performance degradation and massive SVG file sizes:
Root Cause
The fetchAvatarDataUri function (lines 112-136) lacks:
Impact
Performance Degradation:
Cache Bloat:
Request Hanging:
Current Code Evidence:
Proposed Solution
Files to Modify
Acceptance Criteria
✅ Avatar fetch has 5-second timeout
✅ Avatar images limited to 500KB max
✅ Avatar images resized to max 512x512px
✅ Images compressed before base64 conversion
✅ Fallback to default avatar on oversized images
✅ SVG file sizes reduced by 90%+ for large avatars
✅ Cache memory usage reduced significantly
✅ Unit tests for size/dimension validation