@kiln-js/image 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +10 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -174,6 +174,7 @@ function inspectImageOptimization(request, config = {}) {
|
|
|
174
174
|
};
|
|
175
175
|
}
|
|
176
176
|
function createImageHandler(config = {}) {
|
|
177
|
+
const svgCache = /* @__PURE__ */ new Map();
|
|
177
178
|
return async (request) => {
|
|
178
179
|
const check = inspectImageOptimization(request, config);
|
|
179
180
|
if (!check.ok) {
|
|
@@ -186,13 +187,17 @@ function createImageHandler(config = {}) {
|
|
|
186
187
|
status: 200,
|
|
187
188
|
headers: {
|
|
188
189
|
"cache-control": meta.cacheControl,
|
|
190
|
+
vary: "Accept",
|
|
189
191
|
"x-kiln-image-format": meta.negotiatedFormat,
|
|
190
192
|
"x-kiln-image-savings": `${meta.estimatedSavingsPct}%`
|
|
191
193
|
}
|
|
192
194
|
});
|
|
193
195
|
}
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
+
const cacheKey = `${meta.width}|${meta.quality}|${meta.negotiatedFormat}|${meta.estimatedSavingsPct}`;
|
|
197
|
+
let svg = svgCache.get(cacheKey);
|
|
198
|
+
if (!svg) {
|
|
199
|
+
const height = Math.round(meta.width * 0.625);
|
|
200
|
+
svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${meta.width} ${height}" width="${meta.width}" height="${height}">
|
|
196
201
|
<defs>
|
|
197
202
|
<linearGradient id="bg" x1="0%" y1="0%" x2="100%" y2="100%">
|
|
198
203
|
<stop offset="0%" stop-color="#141413" />
|
|
@@ -205,6 +210,9 @@ function createImageHandler(config = {}) {
|
|
|
205
210
|
<text x="32" y="${Math.round(height - 56)}" fill="#f5f5f4" font-family="monospace" font-size="16" font-weight="bold">@kiln-js/image [${meta.negotiatedFormat.toUpperCase()}]</text>
|
|
206
211
|
<text x="32" y="${Math.round(height - 30)}" fill="#a8a29e" font-family="monospace" font-size="13">${meta.width}w \xB7 q=${meta.quality} \xB7 -${meta.estimatedSavingsPct}% bytes</text>
|
|
207
212
|
</svg>`;
|
|
213
|
+
if (svgCache.size > 64) svgCache.delete(svgCache.keys().next().value);
|
|
214
|
+
svgCache.set(cacheKey, svg);
|
|
215
|
+
}
|
|
208
216
|
return new Response(svg, {
|
|
209
217
|
status: 200,
|
|
210
218
|
headers: {
|