bun-plugin-stx 0.2.271 → 0.2.273
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/serve.js +42 -4
- package/package.json +1 -1
package/dist/serve.js
CHANGED
|
@@ -9114,6 +9114,8 @@ async function serve(options) {
|
|
|
9114
9114
|
markPlaceholdersReady = resolve13;
|
|
9115
9115
|
});
|
|
9116
9116
|
const stxModule = options.stxModule ? Promise.resolve(options.stxModule) : defaultStxModule;
|
|
9117
|
+
const imageDeliveryOutputDir = stateDir(process19.cwd(), "image-delivery");
|
|
9118
|
+
const imageDeliveryRoot = nodePath.join(imageDeliveryOutputDir, "_stx", "images");
|
|
9117
9119
|
let resolvedStxModule = null;
|
|
9118
9120
|
Promise.resolve(stxModule).then((m) => {
|
|
9119
9121
|
resolvedStxModule = m;
|
|
@@ -10308,6 +10310,35 @@ ${block}`;
|
|
|
10308
10310
|
return new Response(null, { headers });
|
|
10309
10311
|
return new Response(content2, { headers });
|
|
10310
10312
|
}
|
|
10313
|
+
if ((req.method === "GET" || req.method === "HEAD") && path.startsWith("/_stx/images/")) {
|
|
10314
|
+
let decodedPath;
|
|
10315
|
+
try {
|
|
10316
|
+
decodedPath = decodeURIComponent(path.slice("/_stx/images/".length));
|
|
10317
|
+
} catch {
|
|
10318
|
+
return new Response("Invalid image path", { status: 400 });
|
|
10319
|
+
}
|
|
10320
|
+
if (!decodedPath || decodedPath.includes("\x00"))
|
|
10321
|
+
return new Response("Invalid image path", { status: 400 });
|
|
10322
|
+
const resolvedPath = nodePath.resolve(imageDeliveryRoot, decodedPath);
|
|
10323
|
+
if (!resolvedPath.startsWith(`${imageDeliveryRoot}${nodePath.sep}`))
|
|
10324
|
+
return new Response("Invalid image path", { status: 400 });
|
|
10325
|
+
try {
|
|
10326
|
+
const file = Bun.file(resolvedPath);
|
|
10327
|
+
if (await file.exists()) {
|
|
10328
|
+
const stat4 = await file.stat().catch(() => null);
|
|
10329
|
+
if (stat4 && !stat4.isDirectory()) {
|
|
10330
|
+
const ext = resolvedPath.split(".").pop()?.toLowerCase();
|
|
10331
|
+
const headers = {
|
|
10332
|
+
"Content-Type": staticContentTypes[ext || ""] || "application/octet-stream",
|
|
10333
|
+
"Cache-Control": "public, max-age=31536000, immutable",
|
|
10334
|
+
...corsHeaders
|
|
10335
|
+
};
|
|
10336
|
+
return req.method === "HEAD" ? new Response(null, { headers }) : new Response(file, { headers });
|
|
10337
|
+
}
|
|
10338
|
+
}
|
|
10339
|
+
} catch {}
|
|
10340
|
+
return new Response("Image not found", { status: 404 });
|
|
10341
|
+
}
|
|
10311
10342
|
if (path === "/_stx/stores.js") {
|
|
10312
10343
|
const stx = await stxModule;
|
|
10313
10344
|
const code = typeof stx.getStoreScript === "function" ? await stx.getStoreScript() : null;
|
|
@@ -10854,12 +10885,19 @@ ${trailingPageScripts.join(`
|
|
|
10854
10885
|
}
|
|
10855
10886
|
(async () => {
|
|
10856
10887
|
try {
|
|
10857
|
-
const
|
|
10858
|
-
const
|
|
10859
|
-
|
|
10860
|
-
|
|
10888
|
+
const stx = await stxModule;
|
|
10889
|
+
const publicRoot = nodePath.resolve(process19.cwd(), publicDir);
|
|
10890
|
+
const [placeholderResult, deliveryResult] = await Promise.allSettled([
|
|
10891
|
+
stx.warmImagePlaceholders(publicRoot, {
|
|
10892
|
+
cachePath: stateDir(process19.cwd(), "image-placeholders.json")
|
|
10893
|
+
}),
|
|
10894
|
+
stx.prepareImageDelivery(publicRoot, imageDeliveryOutputDir)
|
|
10895
|
+
]);
|
|
10896
|
+
const derived = placeholderResult.status === "fulfilled" ? placeholderResult.value : 0;
|
|
10861
10897
|
if (derived > 0 && !production)
|
|
10862
10898
|
console.log(`[stx] derived ${derived} image placeholder(s)`);
|
|
10899
|
+
if (deliveryResult.status === "fulfilled" && deliveryResult.value.count > 0 && !production)
|
|
10900
|
+
console.log(`[stx] optimized ${deliveryResult.value.count} image(s) for responsive delivery`);
|
|
10863
10901
|
} catch {} finally {
|
|
10864
10902
|
markPlaceholdersReady();
|
|
10865
10903
|
}
|
package/package.json
CHANGED