@openscript/unplugin-favicons
Version:
Generate favicons for your project with caching for blazing fast rebuilds.
50 lines • 2.17 kB
JavaScript
import { colorize } from "consola/utils";
import { put as cachePut } from "../utils/cache.js";
import consola from "../utils/consola.js";
/**
* @private
*
* Caches the provided `FaviconResponse` using the provided cache key.
*
* Note: Because `cacache` only allows for the serialization of strings and
* Buffers, we have to manually separate binary data from the rest of the
* response.
*/
const putCacheResponse = async (cacheKey, response, label) => {
const serializableResponse = {
files: [],
html: [],
images: [],
};
// Write images to cache as individual entities and add all other metadata to
// the new response.
// eslint-disable-next-line compat/compat
const imagesPromise = Promise.all(response.images.map(async (image) => {
const { contents, ...meta } = image;
const key = `${cacheKey}/images/${meta.name}`;
serializableResponse.images.push(meta);
consola.verbose(`${label ?? ""} Caching ${colorize("green", meta.name)}.`);
await cachePut(key, contents, { logLabel: meta.name });
}));
// Write files to cache as individual entities and add all other metadata to
// the new response.
// eslint-disable-next-line compat/compat
const filesPromise = Promise.all(response.files.map(async (file) => {
const { contents, ...meta } = file;
const key = `${cacheKey}/files/${meta.name}`;
serializableResponse.files.push(meta);
consola.verbose(`${label ?? ""} Caching ${colorize("green", meta.name)}.`);
await cachePut(key, contents, { logLabel: meta.name });
}));
// Add all HTML to the new response.
response.html.forEach((html) => {
serializableResponse.html.push(html);
});
// Serialize response and write it to cache.
const serializedResponse = JSON.stringify(serializableResponse);
const responsePromise = cachePut(cacheKey, serializedResponse, { logLabel: label ?? "" });
// eslint-disable-next-line compat/compat
await Promise.all([filesPromise, imagesPromise, responsePromise]);
};
export default putCacheResponse;
//# sourceMappingURL=put-cache-response.js.map