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 <noreply@anthropic.com>
This commit is contained in:
@@ -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);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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');
|
||||||
|
});
|
||||||
|
});
|
||||||
Vendored
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 68 B After Width: | Height: | Size: 95 B |
Reference in New Issue
Block a user