feat: add image derivative orchestration
Define thumbnail, medium, and large WebP derivative variants behind an injected renderer and write them through ImageStorage with deterministic keys. Add focused tests and document the remaining worker-backed renderer. Verification: npm test passed 70 tests and integration passed 14 tests. No build script is configured.
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
const variants = Object.freeze([
|
||||
{ name: 'thumbnail', maxWidth: 320 },
|
||||
{ name: 'medium', maxWidth: 1280 },
|
||||
{ name: 'large', maxWidth: 2400 },
|
||||
]);
|
||||
|
||||
function derivativeKey(sourceKey, variantName) {
|
||||
const extensionIndex = sourceKey.lastIndexOf('.');
|
||||
const base = extensionIndex === -1 ? sourceKey : sourceKey.slice(0, extensionIndex);
|
||||
return `${base}-${variantName}.webp`;
|
||||
}
|
||||
|
||||
export function createImageDerivativeProcessor({ imageStorage, renderer }) {
|
||||
if (typeof renderer !== 'function') {
|
||||
throw new TypeError('Image derivative renderer is required.');
|
||||
}
|
||||
|
||||
return {
|
||||
async process({ sourceKey, data }) {
|
||||
const results = [];
|
||||
|
||||
for (const variant of variants) {
|
||||
const derivativeData = await renderer({ data, maxWidth: variant.maxWidth });
|
||||
const key = derivativeKey(sourceKey, variant.name);
|
||||
await imageStorage.put({ key, data: derivativeData });
|
||||
results.push({ name: variant.name, key, maxWidth: variant.maxWidth });
|
||||
}
|
||||
|
||||
return results;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user