also looking at this
feat: imgx integration — on-the-fly image transforms from query params
#2264
15 files
+1118
-16
| @@ -15,11 +15,13 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | import type { ServerWebSocket } from 'bun' |
| 17 | 17 | import type { ResolvedAuth } from './auth' |
| 18 | import type { ResolvedImgxOptions } from './imgx' | |
| 18 | 19 | import type { UpstreamPool, UpstreamState } from './load-balancer' |
| 19 | 20 | import type { ResolvedRedirect } from './redirect' |
| 20 | 21 | import type { ResolvedStaticRoute } from './static-files' |
| 21 | 22 | import type { PathRewrite } from './types' |
| 22 | 23 | import { enforceBasicAuth } from './auth' |
| 24 | import { applyImgxTransform } from './imgx' | |
| 23 | 25 | import { markFailure, markSuccess, selectUpstream } from './load-balancer' |
| 24 | 26 | import { FALLBACK, POOL_BUSY, proxyViaPool, TIMEOUT } from './proxy-pool' |
| 25 | 27 | import { buildRedirectLocation } from './redirect' |
| @@ -47,6 +49,12 @@ export interface ProxyRoute { | ||
| 47 | 49 | changeOrigin?: boolean |
| 48 | 50 | /** Per-route path rewrites (vite/nginx-style prefix routing). */ |
| 49 | 51 | 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 | |
| 50 | 58 | /** When set, serve files from a local directory instead of proxying. */ |
| 51 | 59 | static?: ResolvedStaticRoute |
| 52 | 60 | /** When set, answer with an HTTP redirect (Location) instead of proxying/serving. */ |
| @@ -446,7 +454,8 @@ export function createProxyFetchHandler(getRoute: GetRoute, verbose?: boolean, o | ||
| 446 | 454 | if (route.static) { |
| 447 | 455 | const strip = route.stripBasePathPrefix ?? true |
| 448 | 456 | 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) | |
| 450 | 459 | } |
| 451 | 460 | |
| 452 | 461 | // 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 | ||
| 524 | 533 | }) |
| 525 | 534 | if (pool) |
| 526 | 535 | markSuccess(pool, upstream) |
| 527 | return res | |
| 536 | return await applyImgxTransform(req, pathname, search, route.imgx, res, verbose) | |
| 528 | 537 | } |
| 529 | 538 | catch (err) { |
| 530 | 539 | // Upstream stalled past the configured timeout → 504 (no fetch retry — it |
| @@ -562,7 +571,7 @@ export function createProxyFetchHandler(getRoute: GetRoute, verbose?: boolean, o | ||
| 562 | 571 | }) |
| 563 | 572 | if (pool) |
| 564 | 573 | markSuccess(pool, upstream) |
| 565 | return res | |
| 574 | return await applyImgxTransform(req, pathname, search, route.imgx, res, verbose) | |
| 566 | 575 | } |
| 567 | 576 | catch (fetchErr) { |
| 568 | 577 | debugLog('request', `Proxy error for ${hostname}: ${fetchErr}`, verbose) |