Apply the auto-fixable lint rules and the formatter, and drop the Vite build artifacts.
Checks / checks (push) Successful in 2m11s
Trigger cinny-mobile / dispatch (push) Successful in 1s

This commit is contained in:
2026-11-01 04:23:53 +11:00
parent 3cd159a63e
commit a6ad8651b3
605 changed files with 11480 additions and 9125 deletions
+46 -39
View File
@@ -1,7 +1,7 @@
/**
* Utilities for reading and writing WebP metadata (custom XMP chunks)
* Used to embed user color in avatar images
*
*
* WebP uses RIFF container format:
* - File starts with "RIFF" (4 bytes) + file size (4 bytes LE) + "WEBP" (4 bytes)
* - Chunks: chunk ID (4 bytes) + chunk size (4 bytes LE) + chunk data (+ padding byte if odd size)
@@ -9,7 +9,7 @@
/* eslint-disable no-bitwise */
/* eslint-disable no-plusplus */
/* eslint-disable no-console */
/* eslint-disable no-restricted-syntax */
/** RIFF signature bytes */
@@ -20,13 +20,15 @@ const WEBP_SIGNATURE = new Uint8Array([0x57, 0x45, 0x42, 0x50]); // "WEBP"
export const PAARROT_COLOR_KEY = 'paarrot:color';
/** Custom chunk ID for paarrot color - using XMP_ as it's a standard metadata chunk */
const XMP_CHUNK_ID = new Uint8Array([0x58, 0x4D, 0x50, 0x20]); // "XMP "
const XMP_CHUNK_ID = new Uint8Array([0x58, 0x4d, 0x50, 0x20]); // "XMP "
/**
* Read a 32-bit little-endian integer from a buffer
*/
function readUint32LE(data: Uint8Array, offset: number): number {
return data[offset] | (data[offset + 1] << 8) | (data[offset + 2] << 16) | (data[offset + 3] << 24);
return (
data[offset] | (data[offset + 1] << 8) | (data[offset + 2] << 16) | (data[offset + 3] << 24)
);
}
/**
@@ -46,17 +48,17 @@ function writeUint32LE(value: number): Uint8Array {
*/
function isWebP(data: Uint8Array): boolean {
if (data.length < 12) return false;
// Check RIFF signature
for (let i = 0; i < 4; i++) {
if (data[i] !== RIFF_SIGNATURE[i]) return false;
}
// Check WEBP signature at offset 8
for (let i = 0; i < 4; i++) {
if (data[8 + i] !== WEBP_SIGNATURE[i]) return false;
}
return true;
}
@@ -90,28 +92,30 @@ function extractColorFromXMP(xmpData: string): string | undefined {
/**
* Parse WebP chunks from image data
*/
function parseChunks(data: Uint8Array): Array<{ id: string; data: Uint8Array; offset: number; length: number }> {
function parseChunks(
data: Uint8Array,
): Array<{ id: string; data: Uint8Array; offset: number; length: number }> {
const chunks: Array<{ id: string; data: Uint8Array; offset: number; length: number }> = [];
let offset = 12; // Skip RIFF header (4) + size (4) + WEBP (4)
while (offset + 8 <= data.length) {
const id = new TextDecoder().decode(data.slice(offset, offset + 4));
const size = readUint32LE(data, offset + 4);
const chunkData = data.slice(offset + 8, offset + 8 + size);
// Chunk length includes padding byte if size is odd
const paddedSize = size + (size % 2);
chunks.push({
id,
data: chunkData,
offset,
length: 8 + paddedSize, // header (8) + data + optional padding
});
offset += 8 + paddedSize;
}
return chunks;
}
@@ -122,13 +126,13 @@ function parseChunks(data: Uint8Array): Array<{ id: string; data: Uint8Array; of
*/
export function extractColorFromWebP(imageData: ArrayBuffer | Uint8Array): string | undefined {
const data = imageData instanceof Uint8Array ? imageData : new Uint8Array(imageData);
if (!isWebP(data)) {
return undefined;
}
const chunks = parseChunks(data);
for (const chunk of chunks) {
if (chunk.id === 'XMP ') {
const xmpString = new TextDecoder().decode(chunk.data);
@@ -138,7 +142,7 @@ export function extractColorFromWebP(imageData: ArrayBuffer | Uint8Array): strin
}
}
}
return undefined;
}
@@ -148,18 +152,21 @@ export function extractColorFromWebP(imageData: ArrayBuffer | Uint8Array): strin
* @param color - The color string to embed (e.g., '#ff5500')
* @returns New WebP data with embedded color, or null if not a valid WebP
*/
export function embedColorInWebP(imageData: ArrayBuffer | Uint8Array, color: string): Uint8Array | null {
export function embedColorInWebP(
imageData: ArrayBuffer | Uint8Array,
color: string,
): Uint8Array | null {
const data = imageData instanceof Uint8Array ? imageData : new Uint8Array(imageData);
if (!isWebP(data)) {
return null;
}
const chunks = parseChunks(data);
// Find existing XMP chunk with our color
let existingXMPChunk: { offset: number; length: number } | null = null;
for (const chunk of chunks) {
if (chunk.id === 'XMP ') {
const xmpString = new TextDecoder().decode(chunk.data);
@@ -169,21 +176,21 @@ export function embedColorInWebP(imageData: ArrayBuffer | Uint8Array, color: str
}
}
}
// Create XMP chunk with color
const xmpData = createXMPData(color);
const xmpBytes = new TextEncoder().encode(xmpData);
const xmpPaddedSize = xmpBytes.length + (xmpBytes.length % 2); // Add padding byte if odd
// XMP chunk: ID (4) + size (4) + data + optional padding
const xmpChunk = new Uint8Array(8 + xmpPaddedSize);
xmpChunk.set(XMP_CHUNK_ID, 0);
xmpChunk.set(writeUint32LE(xmpBytes.length), 4);
xmpChunk.set(xmpBytes, 8);
// Padding byte is already 0 from Uint8Array initialization
let newData: Uint8Array;
if (existingXMPChunk) {
// Replace existing chunk
newData = new Uint8Array(data.length - existingXMPChunk.length + xmpChunk.length);
@@ -191,7 +198,7 @@ export function embedColorInWebP(imageData: ArrayBuffer | Uint8Array, color: str
newData.set(xmpChunk, existingXMPChunk.offset);
newData.set(
data.slice(existingXMPChunk.offset + existingXMPChunk.length),
existingXMPChunk.offset + xmpChunk.length
existingXMPChunk.offset + xmpChunk.length,
);
} else {
// Insert new chunk at end (before any trailing data)
@@ -199,11 +206,11 @@ export function embedColorInWebP(imageData: ArrayBuffer | Uint8Array, color: str
newData.set(data, 0);
newData.set(xmpChunk, data.length);
}
// Update RIFF file size (at offset 4)
const newFileSize = newData.length - 8; // RIFF size excludes first 8 bytes
newData.set(writeUint32LE(newFileSize), 4);
return newData;
}
@@ -214,16 +221,16 @@ export function embedColorInWebP(imageData: ArrayBuffer | Uint8Array, color: str
*/
export function removeColorFromWebP(imageData: ArrayBuffer | Uint8Array): Uint8Array | null {
const data = imageData instanceof Uint8Array ? imageData : new Uint8Array(imageData);
if (!isWebP(data)) {
return null;
}
const chunks = parseChunks(data);
// Find existing XMP chunk with our color
let existingXMPChunk: { offset: number; length: number } | null = null;
for (const chunk of chunks) {
if (chunk.id === 'XMP ') {
const xmpString = new TextDecoder().decode(chunk.data);
@@ -233,24 +240,24 @@ export function removeColorFromWebP(imageData: ArrayBuffer | Uint8Array): Uint8A
}
}
}
if (!existingXMPChunk) {
// No color to remove
return data;
}
// Build new WebP data without the XMP chunk
const newData = new Uint8Array(data.length - existingXMPChunk.length);
newData.set(data.slice(0, existingXMPChunk.offset), 0);
newData.set(
data.slice(existingXMPChunk.offset + existingXMPChunk.length),
existingXMPChunk.offset
existingXMPChunk.offset,
);
// Update RIFF file size
const newFileSize = newData.length - 8;
newData.set(writeUint32LE(newFileSize), 4);
return newData;
}
@@ -263,7 +270,7 @@ export async function fetchAndExtractColorFromWebP(url: string): Promise<string
try {
const response = await fetch(url);
if (!response.ok) return undefined;
const data = await response.arrayBuffer();
return extractColorFromWebP(data);
} catch {