ReviewOS

also looking at this

stacks/rpx

feat: imgx integration — on-the-fly image transforms from query params

#2264
Merged glennmichael123 wants to merge feat/imgx-integration into main
15 files +1118 -16
packages/rpx/src/proxy-handler.tsmodified+12-3
Changes to packages/rpx/src/proxy-handler.ts
@@ -15,11 +15,13 @@
1515 */
1616import type { ServerWebSocket } from 'bun'
1717import type { ResolvedAuth } from './auth'
18import type { ResolvedImgxOptions } from './imgx'
1819import type { UpstreamPool, UpstreamState } from './load-balancer'
1920import type { ResolvedRedirect } from './redirect'
2021import type { ResolvedStaticRoute } from './static-files'
2122import type { PathRewrite } from './types'
2223import { enforceBasicAuth } from './auth'
24import { applyImgxTransform } from './imgx'
2325import { markFailure, markSuccess, selectUpstream } from './load-balancer'
2426import { FALLBACK, POOL_BUSY, proxyViaPool, TIMEOUT } from './proxy-pool'
2527import { buildRedirectLocation } from './redirect'
@@ -47,6 +49,12 @@ export interface ProxyRoute {
4749 changeOrigin?: boolean
4850 /** Per-route path rewrites (vite/nginx-style prefix routing). */
4951 pathRewrites?: PathRewrite[]
52 /**
53 * When set, transform image responses on the fly from imgix/meema-style
54 * query params (`?w=200&h=1200&q=80`) via the imgx (ts-images) pipeline.
55 * Applies to proxied and static responses alike.
56 */
57 imgx?: ResolvedImgxOptions
5058 /** When set, serve files from a local directory instead of proxying. */
5159 static?: ResolvedStaticRoute
5260 /** When set, answer with an HTTP redirect (Location) instead of proxying/serving. */
@@ -446,7 +454,8 @@ export function createProxyFetchHandler(getRoute: GetRoute, verbose?: boolean, o
446454 if (route.static) {
447455 const strip = route.stripBasePathPrefix ?? true
448456 const staticPath = strip ? stripBasePath(pathname, route.basePath) : pathname
449 return serveStaticFile(staticPath, route.static)
457 const staticRes = await serveStaticFile(staticPath, route.static)
458 return applyImgxTransform(req, pathname, search, route.imgx, staticRes, verbose)
450459 }
451460
452461 // WebSocket upgrade: hand the socket to Bun and dial the upstream on open.
@@ -524,7 +533,7 @@ export function createProxyFetchHandler(getRoute: GetRoute, verbose?: boolean, o
524533 })
525534 if (pool)
526535 markSuccess(pool, upstream)
527 return res
536 return await applyImgxTransform(req, pathname, search, route.imgx, res, verbose)
528537 }
529538 catch (err) {
530539 // Upstream stalled past the configured timeout → 504 (no fetch retry — it
@@ -562,7 +571,7 @@ export function createProxyFetchHandler(getRoute: GetRoute, verbose?: boolean, o
562571 })
563572 if (pool)
564573 markSuccess(pool, upstream)
565 return res
574 return await applyImgxTransform(req, pathname, search, route.imgx, res, verbose)
566575 }
567576 catch (fetchErr) {
568577 debugLog('request', `Proxy error for ${hostname}: ${fetchErr}`, verbose)