Apply the auto-fixable lint rules and the formatter, and drop the Vite build artifacts.
This commit is contained in:
@@ -43,8 +43,7 @@ class LRUCache<K, V> {
|
||||
set(key: K, value: V): void {
|
||||
if (this.cache.has(key)) {
|
||||
this.cache.delete(key);
|
||||
}
|
||||
else if (this.cache.size >= this.maxSize) {
|
||||
} else if (this.cache.size >= this.maxSize) {
|
||||
const firstKey = this.cache.keys().next().value;
|
||||
if (firstKey !== undefined) {
|
||||
this.cache.delete(firstKey);
|
||||
@@ -90,7 +89,7 @@ const RIFF_SIGNATURE = [0x52, 0x49, 0x46, 0x46]; // "RIFF"
|
||||
const WEBP_OFFSET_SIGNATURE = [0x57, 0x45, 0x42, 0x50]; // "WEBP" at offset 8
|
||||
|
||||
/** JPEG signature */
|
||||
const JPEG_SIGNATURE = [0xFF, 0xD8];
|
||||
const JPEG_SIGNATURE = [0xff, 0xd8];
|
||||
|
||||
/** GIF signatures */
|
||||
const GIF87A_SIGNATURE = [0x47, 0x49, 0x46, 0x38, 0x37, 0x61]; // "GIF87a"
|
||||
@@ -103,9 +102,9 @@ export type ImageFormat = 'png' | 'webp' | 'jpeg' | 'gif' | 'unknown';
|
||||
*/
|
||||
export function detectImageFormat(data: ArrayBuffer | Uint8Array): ImageFormat {
|
||||
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
|
||||
|
||||
|
||||
if (bytes.length < 12) return 'unknown';
|
||||
|
||||
|
||||
// Check PNG signature
|
||||
let isPNG = true;
|
||||
for (let i = 0; i < 8; i++) {
|
||||
@@ -115,7 +114,7 @@ export function detectImageFormat(data: ArrayBuffer | Uint8Array): ImageFormat {
|
||||
}
|
||||
}
|
||||
if (isPNG) return 'png';
|
||||
|
||||
|
||||
// Check WebP signature (RIFF + WEBP)
|
||||
let isRIFF = true;
|
||||
for (let i = 0; i < 4; i++) {
|
||||
@@ -134,12 +133,12 @@ export function detectImageFormat(data: ArrayBuffer | Uint8Array): ImageFormat {
|
||||
}
|
||||
if (isWebP) return 'webp';
|
||||
}
|
||||
|
||||
|
||||
// Check JPEG signature (FFD8)
|
||||
if (bytes[0] === JPEG_SIGNATURE[0] && bytes[1] === JPEG_SIGNATURE[1]) {
|
||||
return 'jpeg';
|
||||
}
|
||||
|
||||
|
||||
// Check GIF signatures
|
||||
let isGIF87a = true;
|
||||
for (let i = 0; i < 6; i++) {
|
||||
@@ -149,7 +148,7 @@ export function detectImageFormat(data: ArrayBuffer | Uint8Array): ImageFormat {
|
||||
}
|
||||
}
|
||||
if (isGIF87a) return 'gif';
|
||||
|
||||
|
||||
let isGIF89a = true;
|
||||
for (let i = 0; i < 6; i++) {
|
||||
if (bytes[i] !== GIF89A_SIGNATURE[i]) {
|
||||
@@ -158,7 +157,7 @@ export function detectImageFormat(data: ArrayBuffer | Uint8Array): ImageFormat {
|
||||
}
|
||||
}
|
||||
if (isGIF89a) return 'gif';
|
||||
|
||||
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
@@ -169,9 +168,9 @@ export function detectImageFormat(data: ArrayBuffer | Uint8Array): ImageFormat {
|
||||
*/
|
||||
export function extractColorFromImage(imageData: ArrayBuffer | Uint8Array): string | undefined {
|
||||
const format = detectImageFormat(imageData);
|
||||
|
||||
|
||||
console.log('[extractColorFromImage] Detected format:', format);
|
||||
|
||||
|
||||
switch (format) {
|
||||
case 'png':
|
||||
return extractColorFromPNG(imageData);
|
||||
@@ -194,7 +193,7 @@ export function extractColorFromImage(imageData: ArrayBuffer | Uint8Array): stri
|
||||
*/
|
||||
export function extractBannerFromImage(imageData: ArrayBuffer | Uint8Array): string | undefined {
|
||||
const format = detectImageFormat(imageData);
|
||||
|
||||
|
||||
switch (format) {
|
||||
case 'png':
|
||||
return extractBannerFromPNG(imageData);
|
||||
@@ -212,9 +211,9 @@ export function extractBannerFromImage(imageData: ArrayBuffer | Uint8Array): str
|
||||
*/
|
||||
export function extractMetadataFromImage(imageData: ArrayBuffer | Uint8Array): ImageMetadata {
|
||||
const format = detectImageFormat(imageData);
|
||||
|
||||
|
||||
console.log('[extractMetadataFromImage] Detected format:', format);
|
||||
|
||||
|
||||
switch (format) {
|
||||
case 'png':
|
||||
const pngMetadata = extractMetadataFromPNG(imageData);
|
||||
@@ -240,9 +239,12 @@ export function extractMetadataFromImage(imageData: ArrayBuffer | Uint8Array): I
|
||||
* @param color - The color string to embed (e.g., '#ff5500')
|
||||
* @returns New image data with embedded color, or null if not a supported format
|
||||
*/
|
||||
export function embedColorInImage(imageData: ArrayBuffer | Uint8Array, color: string): Uint8Array | null {
|
||||
export function embedColorInImage(
|
||||
imageData: ArrayBuffer | Uint8Array,
|
||||
color: string,
|
||||
): Uint8Array | null {
|
||||
const format = detectImageFormat(imageData);
|
||||
|
||||
|
||||
switch (format) {
|
||||
case 'png':
|
||||
return embedColorInPNG(imageData, color);
|
||||
@@ -269,7 +271,7 @@ export function needsPngForMetadata(metadata: ImageMetadata): boolean {
|
||||
* Convert image data to PNG via canvas (needed for banner/border/gradient metadata).
|
||||
*/
|
||||
export async function convertImageDataToPng(
|
||||
imageData: ArrayBuffer | Uint8Array
|
||||
imageData: ArrayBuffer | Uint8Array,
|
||||
): Promise<Uint8Array | null> {
|
||||
const bytes = imageData instanceof Uint8Array ? imageData : new Uint8Array(imageData);
|
||||
const format = detectImageFormat(bytes);
|
||||
@@ -320,7 +322,7 @@ export function uint8ArrayToBlob(data: Uint8Array, type: string): Blob {
|
||||
*/
|
||||
export function embedMetadataInImage(
|
||||
imageData: ArrayBuffer | Uint8Array,
|
||||
metadata: ImageMetadata
|
||||
metadata: ImageMetadata,
|
||||
): Uint8Array | null {
|
||||
const format = detectImageFormat(imageData);
|
||||
const bytes = imageData instanceof Uint8Array ? imageData : new Uint8Array(imageData);
|
||||
@@ -335,7 +337,7 @@ export function embedMetadataInImage(
|
||||
if (needsPngForMetadata(metadata)) {
|
||||
console.warn(
|
||||
'[embedMetadataInImage] Banner/border/gradient require PNG; convert with convertImageDataToPng first. Got:',
|
||||
format
|
||||
format,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
@@ -357,7 +359,7 @@ export function embedMetadataInImage(
|
||||
*/
|
||||
export function removeColorFromImage(imageData: ArrayBuffer | Uint8Array): Uint8Array | null {
|
||||
const format = detectImageFormat(imageData);
|
||||
|
||||
|
||||
switch (format) {
|
||||
case 'png':
|
||||
return removeColorFromPNG(imageData);
|
||||
@@ -410,7 +412,9 @@ export function getExtension(format: ImageFormat): string {
|
||||
}
|
||||
|
||||
// LRU cache for avatar metadata to avoid refetching
|
||||
const avatarMetadataCache = new LRUCache<string, { metadata: ImageMetadata; timestamp: number }>(100);
|
||||
const avatarMetadataCache = new LRUCache<string, { metadata: ImageMetadata; timestamp: number }>(
|
||||
100,
|
||||
);
|
||||
const METADATA_CACHE_TTL_MS = 30 * 60 * 1000; // 30 minutes
|
||||
|
||||
// In-flight fetch tracking to prevent duplicate parallel requests
|
||||
@@ -423,7 +427,10 @@ const inflightMetadataFetches = new Map<string, Promise<ImageMetadata>>();
|
||||
* @param accessToken - Optional access token for authenticated media
|
||||
* @returns The color string if found, or undefined
|
||||
*/
|
||||
export async function fetchAndExtractColor(url: string, accessToken?: string | null): Promise<string | undefined> {
|
||||
export async function fetchAndExtractColor(
|
||||
url: string,
|
||||
accessToken?: string | null,
|
||||
): Promise<string | undefined> {
|
||||
// Check if full metadata is cached
|
||||
const cached = avatarMetadataCache.get(url);
|
||||
if (cached && Date.now() - cached.timestamp < METADATA_CACHE_TTL_MS) {
|
||||
@@ -440,27 +447,27 @@ export async function fetchAndExtractColor(url: string, accessToken?: string | n
|
||||
}
|
||||
|
||||
console.log('[fetchAndExtractColor] Fetching and extracting color from', url);
|
||||
|
||||
|
||||
const fetchPromise = (async () => {
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: accessToken ? { Authorization: `Bearer ${accessToken}` } : undefined,
|
||||
});
|
||||
|
||||
|
||||
console.log('[fetchAndExtractColor] Fetch response status:', response.ok, response.status);
|
||||
|
||||
|
||||
if (!response.ok) return undefined;
|
||||
|
||||
|
||||
const data = await response.arrayBuffer();
|
||||
console.log('[fetchAndExtractColor] Downloaded', data.byteLength, 'bytes');
|
||||
|
||||
|
||||
const color = extractColorFromImage(data);
|
||||
console.log('[fetchAndExtractColor] Extracted color:', color);
|
||||
|
||||
|
||||
// Don't cache here - fetchAndExtractMetadata will cache full metadata
|
||||
// This avoids cache collision where color-only fetch overwrites full metadata
|
||||
|
||||
|
||||
return color;
|
||||
} catch (error) {
|
||||
console.error('[fetchAndExtractColor] Error fetching/extracting color:', error);
|
||||
@@ -480,7 +487,10 @@ export async function fetchAndExtractColor(url: string, accessToken?: string | n
|
||||
* @param accessToken - Optional access token for authenticated media
|
||||
* @returns Object with color and banner if found
|
||||
*/
|
||||
export async function fetchAndExtractMetadata(url: string, accessToken?: string | null): Promise<ImageMetadata> {
|
||||
export async function fetchAndExtractMetadata(
|
||||
url: string,
|
||||
accessToken?: string | null,
|
||||
): Promise<ImageMetadata> {
|
||||
// Check cache first
|
||||
const cached = avatarMetadataCache.get(url);
|
||||
if (cached && Date.now() - cached.timestamp < METADATA_CACHE_TTL_MS) {
|
||||
@@ -504,23 +514,23 @@ export async function fetchAndExtractMetadata(url: string, accessToken?: string
|
||||
method: 'GET',
|
||||
headers: accessToken ? { Authorization: `Bearer ${accessToken}` } : undefined,
|
||||
});
|
||||
|
||||
|
||||
console.log('[fetchAndExtractMetadata] Fetch response status:', response.ok, response.status);
|
||||
|
||||
|
||||
if (!response.ok) return {};
|
||||
|
||||
|
||||
const data = await response.arrayBuffer();
|
||||
console.log('[fetchAndExtractMetadata] Downloaded', data.byteLength, 'bytes');
|
||||
|
||||
|
||||
const metadata = extractMetadataFromImage(data);
|
||||
console.log('[fetchAndExtractMetadata] Extracted metadata:', metadata);
|
||||
|
||||
|
||||
// Cache the result
|
||||
avatarMetadataCache.set(url, {
|
||||
metadata,
|
||||
timestamp: Date.now()
|
||||
avatarMetadataCache.set(url, {
|
||||
metadata,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
|
||||
|
||||
return metadata;
|
||||
} catch {
|
||||
return {};
|
||||
|
||||
Reference in New Issue
Block a user