81 lines
1.8 KiB
JavaScript
81 lines
1.8 KiB
JavaScript
// @ts-check
|
|
import crypto from "node:crypto";
|
|
import objectHash from "object-hash";
|
|
import getImageSources from "./getImageSources.js";
|
|
import getProcessedImage from "./getProcessedImage.js";
|
|
import getArtDirectedImages from "./getArtDirectedImages.js";
|
|
|
|
const imagesData = new Map();
|
|
|
|
export default async function ({
|
|
src,
|
|
type,
|
|
sizes: imagesizes,
|
|
format,
|
|
breakpoints,
|
|
placeholder,
|
|
fallbackFormat,
|
|
includeSourceFormat,
|
|
formatOptions,
|
|
artDirectives,
|
|
transformConfigs,
|
|
}) {
|
|
const args = Array.from(arguments);
|
|
const hash = objectHash(args);
|
|
if (imagesData.has(hash)) {
|
|
return imagesData.get(hash);
|
|
}
|
|
const { path, base, rest, image, imageWidth, imageHeight, imageFormat } =
|
|
await getProcessedImage(src, transformConfigs);
|
|
|
|
src = path;
|
|
|
|
rest.aspect = `${imageWidth / imageHeight}`;
|
|
|
|
if (!fallbackFormat) {
|
|
fallbackFormat = imageFormat;
|
|
}
|
|
try {
|
|
const [mainImage, artDirectedImages] = await Promise.all([
|
|
getImageSources(
|
|
src,
|
|
base,
|
|
image,
|
|
format,
|
|
imageWidth,
|
|
imagesizes,
|
|
breakpoints,
|
|
placeholder,
|
|
imageFormat,
|
|
formatOptions,
|
|
fallbackFormat,
|
|
includeSourceFormat,
|
|
rest
|
|
),
|
|
getArtDirectedImages(
|
|
artDirectives,
|
|
placeholder,
|
|
format,
|
|
imagesizes,
|
|
breakpoints,
|
|
fallbackFormat,
|
|
includeSourceFormat,
|
|
formatOptions,
|
|
rest
|
|
),
|
|
]);
|
|
|
|
const images = [...artDirectedImages, mainImage]
|
|
//const uuid = crypto.createHash('md5').update(src).digest('hex').substring(0, 5)
|
|
const uuid = crypto.randomBytes(4).toString("hex").toUpperCase()
|
|
const returnObject = {
|
|
uuid,
|
|
images,
|
|
}
|
|
imagesData.set(hash, returnObject)
|
|
return returnObject;
|
|
} catch (error) {
|
|
console.error(`Error getImage :${src}`, error)
|
|
}
|
|
}
|