104 lines
2.6 KiB
JavaScript
104 lines
2.6 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";
|
|
import pMap from "p-map";
|
|
|
|
const imagesData = new Map();
|
|
|
|
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
export default async function ({
|
|
src,
|
|
type,
|
|
sizes: imagesizes,
|
|
format,
|
|
breakpoints,
|
|
placeholder,
|
|
fallbackFormat,
|
|
includeSourceFormat,
|
|
formatOptions,
|
|
artDirectives,
|
|
transformConfigs,
|
|
}) {
|
|
try {
|
|
const args = Array.from(arguments);
|
|
const hash = objectHash(args);
|
|
if (imagesData.has(hash)) {
|
|
return imagesData.get(hash);
|
|
}
|
|
// const start = performance.now();
|
|
const { path, base, rest, image, imageWidth, imageHeight, imageFormat } =
|
|
await getProcessedImage(src, transformConfigs);
|
|
|
|
await delay(100);
|
|
src = path;
|
|
|
|
rest.aspect = `${imageWidth / imageHeight}`;
|
|
if (!fallbackFormat) {
|
|
fallbackFormat = imageFormat;
|
|
}
|
|
|
|
// Fetch both image sources and art-directed images
|
|
const [mainImage, artDirectedImages] = await pMap(
|
|
[
|
|
async () =>
|
|
await getImageSources(
|
|
src,
|
|
base,
|
|
image,
|
|
format,
|
|
imageWidth,
|
|
imagesizes,
|
|
breakpoints,
|
|
placeholder,
|
|
imageFormat,
|
|
formatOptions,
|
|
fallbackFormat,
|
|
includeSourceFormat,
|
|
rest
|
|
),
|
|
async () => {
|
|
await delay(250);
|
|
return await getArtDirectedImages(
|
|
artDirectives,
|
|
placeholder,
|
|
format,
|
|
imagesizes,
|
|
breakpoints,
|
|
fallbackFormat,
|
|
includeSourceFormat,
|
|
formatOptions,
|
|
rest
|
|
);
|
|
},
|
|
],
|
|
async (task) => await task(),
|
|
{ concurrency: 1 }
|
|
);
|
|
|
|
// Ensure artDirectedImages is an array
|
|
const images = Array.isArray(artDirectedImages) ? [...artDirectedImages, mainImage] : [mainImage];
|
|
|
|
const uuid = crypto.randomBytes(4).toString("hex").toUpperCase();
|
|
|
|
const returnObject = {
|
|
uuid,
|
|
images,
|
|
};
|
|
|
|
imagesData.set(hash, returnObject);
|
|
|
|
//const end = performance.now();
|
|
|
|
//console.log( `Responsive Image sets generated for ${type} at ${args[0].src} in ${end - start}ms`);
|
|
|
|
return returnObject;
|
|
} catch (error) {
|
|
console.error("Error processing images:", error);
|
|
throw error;
|
|
}
|
|
}
|