feat: Create image pipeline

This commit is contained in:
Wanjohi
2025-06-04 13:50:06 +03:00
parent e67a8d2b32
commit 0124af1b70
9 changed files with 289 additions and 6 deletions

View File

@@ -0,0 +1,50 @@
import { Hono } from "hono";
import { HTTPException } from "hono/http-exception";
import { Resource } from "sst";
export namespace ImageRoute {
export const route = new Hono()
.get(
"/:hashWithExt",
(c) => {
const { hashWithExt } = c.req.param();
// Validate format
// Split hash and extension
const match = hashWithExt.match(/^([a-zA-Z0-9_-]+)\.(avif|webp)$/);
if (!match) {
throw new HTTPException(400, { message: "Invalid image hash or format" });
}
const [, hash, format] = match;
const query = c.req.query();
// Parse dimensions
const width = parseInt(query.w || query.width || "");
const height = parseInt(query.h || query.height || "");
const dpr = parseFloat(query.dpr || "1");
if (isNaN(width) || width <= 0) {
throw new HTTPException(400, { message: "Invalid width" });
}
if (!isNaN(height) && height < 0) {
throw new HTTPException(400, { message: "Invalid height" });
}
if (dpr < 1 || dpr > 4) {
throw new HTTPException(400, { message: "Invalid dpr" });
}
const image = `${Resource.ImageRouter.url}/images/00641801e0f80a82bbbbf9dec5593cd7cbe2a5eec45d36199a5c14ed30d8df66`
// Normalize and build cache key
const cacheKey = `${hash}_${format}_w${width}${height ? `_h${height}` : ""}_dpr${dpr}`;
// Add aggressive caching
c.header("Cache-Control", "public, max-age=315360000, immutable");
// Placeholder image response (to be replaced by real logic)
return c.text(`Would serve image: ${cacheKey}`);
}
)
}

View File

@@ -0,0 +1,18 @@
import { Hono } from "hono";
import { logger } from "hono/logger";
import { ImageRoute } from "./image";
const app = new Hono();
app
.use(logger(), async (c, next) => {
c.header("Cache-Control", "public, max-age=315360000, immutable");
return next();
})
const routes = app
.get("/", (c) => c.text("Hello World 👋🏾"))
.route("/image", ImageRoute.route)
export type Routes = typeof routes;
export default app;