From 5a990106b080e2020d2f9d8059ab2ed83aa1b1d0 Mon Sep 17 00:00:00 2001 From: Anthony GAEREMYNCK <1@anthony.sh> Date: Thu, 30 Jul 2026 09:07:26 +0200 Subject: [PATCH] feat: add image converter family (sharp) Regenerates the sample.png test fixture via sharp itself: the hand-encoded base64 PNG from the plan had valid magic bytes (enough to fool file-type's signature check) but was malformed past the header, which libpng rejected as soon as sharp actually tried to decode it. Co-Authored-By: Claude Sonnet 5 --- src/converters/image.js | 25 ++++++++++++++++++ test/converters/image.test.js | 47 ++++++++++++++++++++++++++++++++++ test/fixtures/sample.png | Bin 68 -> 95 bytes 3 files changed, 72 insertions(+) create mode 100644 src/converters/image.js create mode 100644 test/converters/image.test.js diff --git a/src/converters/image.js b/src/converters/image.js new file mode 100644 index 0000000..6c4086e --- /dev/null +++ b/src/converters/image.js @@ -0,0 +1,25 @@ +import sharp from 'sharp'; +import { register } from './registry.js'; + +const IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'webp', 'gif', 'tiff', 'avif']; + +function sharpFormatName(format) { + return format === 'jpg' ? 'jpeg' : format; +} + +export function registerImageConverters() { + for (const sourceFormat of IMAGE_FORMATS) { + for (const targetFormat of IMAGE_FORMATS) { + if (sourceFormat === targetFormat) continue; + + register({ + family: 'image', + sourceFormat, + targetFormat, + convert: async (inputPath, outputPath) => { + await sharp(inputPath).toFormat(sharpFormatName(targetFormat)).toFile(outputPath); + }, + }); + } + } +} diff --git a/test/converters/image.test.js b/test/converters/image.test.js new file mode 100644 index 0000000..684e311 --- /dev/null +++ b/test/converters/image.test.js @@ -0,0 +1,47 @@ +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; +import fs from 'node:fs/promises'; +import path from 'node:path'; +import os from 'node:os'; +import { registerImageConverters } from '../../src/converters/image.js'; +import { resolve, listTargetFormats } from '../../src/converters/registry.js'; +import { detectInputMime } from '../../src/mime.js'; + +let tmpDir; + +beforeAll(async () => { + registerImageConverters(); + tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'converter-image-')); +}); + +afterAll(async () => { + await fs.rm(tmpDir, { recursive: true, force: true }); +}); + +describe('image converters', () => { + it('registers every pair among the supported formats', () => { + const targets = listTargetFormats('png').sort(); + expect(targets).toEqual(['avif', 'gif', 'jpeg', 'jpg', 'tiff', 'webp']); + }); + + it('converts a PNG fixture to WebP', async () => { + const inputPath = path.join(import.meta.dirname, '..', 'fixtures', 'sample.png'); + const outputPath = path.join(tmpDir, 'output.webp'); + const entry = resolve('png', 'webp'); + + await entry.convert(inputPath, outputPath); + + const detected = await detectInputMime(outputPath); + expect(detected.mime).toBe('image/webp'); + }); + + it('converts a PNG fixture to JPG', async () => { + const inputPath = path.join(import.meta.dirname, '..', 'fixtures', 'sample.png'); + const outputPath = path.join(tmpDir, 'output.jpg'); + const entry = resolve('png', 'jpg'); + + await entry.convert(inputPath, outputPath); + + const detected = await detectInputMime(outputPath); + expect(detected.mime).toBe('image/jpeg'); + }); +}); diff --git a/test/fixtures/sample.png b/test/fixtures/sample.png index 520b38b7bfbb918d01382f7f5abbd6714eda7eac..6e2747d6f3dd4a57f582ea7fdfc008aa4b870b78 100644 GIT binary patch literal 95 zcmeAS@N?(olHy`uVBq!ia0vp^EFjFm1SHiab7}%9&H|6fVg?507a+{IwK^ypC@ARZ o;uyj)Gx^8)0}SpQVsU&7>n8Jm+))|r29#v*boFyt=akR{06fkWzyJUM literal 68 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx1|;Q0k8}blZci7-kcv6U2@FgO46F={FMr*+ Q0TgENboFyt=akR{0DRC61ONa4