( buffer: Buffer, originalUrl?: string, contentType?: string, )
| 184 | |
| 185 | // Process OG image with Sharp (resize to 300px width) |
| 186 | async function processOgImage( |
| 187 | buffer: Buffer, |
| 188 | originalUrl?: string, |
| 189 | contentType?: string, |
| 190 | ): Promise<Buffer> { |
| 191 | // If buffer is small enough, return it as-is |
| 192 | if (buffer.length < 10000) { |
| 193 | logger.debug( |
| 194 | { originalUrl, bufferSize: buffer.length }, |
| 195 | 'Serving OG image directly without processing', |
| 196 | ); |
| 197 | return buffer; |
| 198 | } |
| 199 | |
| 200 | try { |
| 201 | // For OG images, process with Sharp to 300px width, maintaining aspect ratio |
| 202 | return await sharp(buffer) |
| 203 | .resize(300, null, { |
| 204 | fit: 'inside', |
| 205 | withoutEnlargement: true, |
| 206 | }) |
| 207 | .png() |
| 208 | .toBuffer(); |
| 209 | } catch (error) { |
| 210 | logger.warn( |
| 211 | { |
| 212 | err: error, |
| 213 | originalUrl, |
| 214 | bufferSize: buffer.length, |
| 215 | }, |
| 216 | 'Sharp failed to process OG image, trying fallback', |
| 217 | ); |
| 218 | |
| 219 | throw error; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // Check if URL is a direct image |
| 224 | function isDirectImage(url: URL): boolean { |
no test coverage detected