also looking at this
feat: imgx integration — on-the-fly image transforms from query params
#2264
15 files
+1118
-16
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | /* eslint-disable no-console */ |
| 2 | import type { BaseProxyConfig, CleanupOptions, LoadBalancerConfig, ProxyConfig, ProxyFrom, ProxyOption, ProxyOptions, ProxySetupOptions, ResolvedProxyOptions, SingleProxyConfig, SSLConfig, StartOptions } from './types' | |
| 2 | import type { BaseProxyConfig, CleanupOptions, ImgxOptions, LoadBalancerConfig, ProxyConfig, ProxyFrom, ProxyOption, ProxyOptions, ProxySetupOptions, ResolvedProxyOptions, SingleProxyConfig, SSLConfig, StartOptions } from './types' | |
| 3 | 3 | import { exec, execSync } from 'node:child_process' |
| 4 | 4 | import * as fs from 'node:fs' |
| 5 | 5 | import * as http from 'node:http' |
| @@ -15,6 +15,7 @@ import { config } from './config' | ||
| 15 | 15 | import { runViaDaemon } from './daemon-runner' |
| 16 | 16 | import { addHosts, checkHosts, removeHosts } from './hosts' |
| 17 | 17 | import { checkExistingCertificates, cleanupCertificates, generateCertificate, httpsConfig, loadSSLConfig } from './https' |
| 18 | import { resolveImgx } from './imgx' | |
| 18 | 19 | import { createUpstreamPool, primaryUpstreamUrl, startHealthChecks, stopHealthChecks } from './load-balancer' |
| 19 | 20 | import type { UpstreamPool } from './load-balancer' |
| 20 | 21 | import { DefaultPortManager, findAvailablePort, isPortInUse } from './port-manager' |
| @@ -486,6 +487,7 @@ async function createProxyServer( | ||
| 486 | 487 | auth?: ResolvedAuth, |
| 487 | 488 | originalFrom?: ProxyFrom, |
| 488 | 489 | loadBalancer?: LoadBalancerConfig, |
| 490 | imgx?: boolean | ImgxOptions, | |
| 489 | 491 | ): Promise<void> { |
| 490 | 492 | debugLog('proxy', `Creating proxy server ${from} -> ${to} with cleanUrls: ${cleanUrls}`, verbose) |
| 491 | 493 | |
| @@ -511,6 +513,7 @@ async function createProxyServer( | ||
| 511 | 513 | upstreamPool: pool, |
| 512 | 514 | cleanUrls: cleanUrls || false, |
| 513 | 515 | changeOrigin: changeOrigin || false, |
| 516 | imgx: resolveImgx(imgx), | |
| 514 | 517 | basePath: '/', |
| 515 | 518 | auth, |
| 516 | 519 | } as ProxyRoute, |
| @@ -630,7 +633,7 @@ export async function setupProxy(options: ProxySetupOptions): Promise<void> { | ||
| 630 | 633 | debugLog('setup', `Using standard ${targetPort === 443 ? 'HTTPS' : 'HTTP'} port ${targetPort} for ${to}`, verbose) |
| 631 | 634 | } |
| 632 | 635 | |
| 633 | await createProxyServer(from, to, finalPort, sourceUrl, ssl, vitePluginUsage, verbose, cleanUrls, changeOrigin, resolveAuth((options as { auth?: import('./types').BasicAuthConfig }).auth), originalFrom, options.loadBalancer) | |
| 636 | await createProxyServer(from, to, finalPort, sourceUrl, ssl, vitePluginUsage, verbose, cleanUrls, changeOrigin, resolveAuth((options as { auth?: import('./types').BasicAuthConfig }).auth), originalFrom, options.loadBalancer, options.imgx) | |
| 634 | 637 | } |
| 635 | 638 | catch (err) { |
| 636 | 639 | debugLog('setup', `Setup failed: ${err}`, verbose) |
| @@ -735,6 +738,7 @@ export function startProxy(options: ProxyOption): void { | ||
| 735 | 738 | path: mergedOptions.path, |
| 736 | 739 | cleanUrls: mergedOptions.cleanUrls, |
| 737 | 740 | changeOrigin: mergedOptions.changeOrigin, |
| 741 | imgx: mergedOptions.imgx, | |
| 738 | 742 | pathRewrites: mergedOptions.pathRewrites, |
| 739 | 743 | }], |
| 740 | 744 | verbose: mergedOptions.verbose, |
| @@ -796,6 +800,7 @@ export function startProxy(options: ProxyOption): void { | ||
| 796 | 800 | cleanup: mergedOptions.cleanup, |
| 797 | 801 | vitePluginUsage: mergedOptions.vitePluginUsage, |
| 798 | 802 | changeOrigin: mergedOptions.changeOrigin, |
| 803 | imgx: mergedOptions.imgx, | |
| 799 | 804 | verbose: mergedOptions.verbose, |
| 800 | 805 | regenerateUntrustedCerts: mergedOptions.regenerateUntrustedCerts, |
| 801 | 806 | } |
| @@ -855,6 +860,7 @@ export async function startProxies(options?: ProxyOptions): Promise<void> { | ||
| 855 | 860 | verbose: false, |
| 856 | 861 | cleanUrls: false, |
| 857 | 862 | changeOrigin: false, |
| 863 | imgx: false, | |
| 858 | 864 | regenerateUntrustedCerts: true, |
| 859 | 865 | } as ResolvedProxyOptions |
| 860 | 866 | |
| @@ -889,7 +895,7 @@ export async function startProxies(options?: ProxyOptions): Promise<void> { | ||
| 889 | 895 | if (mergedOptions.viaDaemon) { |
| 890 | 896 | const isMulti = 'proxies' in mergedOptions && Array.isArray(mergedOptions.proxies) |
| 891 | 897 | const proxies = isMulti |
| 892 | ? (mergedOptions.proxies as Array<BaseProxyConfig & { cleanUrls?: boolean, changeOrigin?: boolean }>) | |
| 898 | ? (mergedOptions.proxies as Array<BaseProxyConfig & { cleanUrls?: boolean, changeOrigin?: boolean, imgx?: boolean | ImgxOptions }>) | |
| 893 | 899 | .map(p => ({ |
| 894 | 900 | id: p.id, |
| 895 | 901 | from: p.from, |
| @@ -897,6 +903,7 @@ export async function startProxies(options?: ProxyOptions): Promise<void> { | ||
| 897 | 903 | path: p.path, |
| 898 | 904 | cleanUrls: p.cleanUrls ?? mergedOptions.cleanUrls, |
| 899 | 905 | changeOrigin: p.changeOrigin ?? mergedOptions.changeOrigin, |
| 906 | imgx: p.imgx ?? mergedOptions.imgx, | |
| 900 | 907 | pathRewrites: p.pathRewrites, |
| 901 | 908 | })) |
| 902 | 909 | : [{ |
| @@ -906,6 +913,7 @@ export async function startProxies(options?: ProxyOptions): Promise<void> { | ||
| 906 | 913 | path: mergedOptions.path, |
| 907 | 914 | cleanUrls: mergedOptions.cleanUrls, |
| 908 | 915 | changeOrigin: mergedOptions.changeOrigin, |
| 916 | imgx: mergedOptions.imgx, | |
| 909 | 917 | pathRewrites: mergedOptions.pathRewrites, |
| 910 | 918 | }] |
| 911 | 919 | await runViaDaemon({ proxies, verbose }) |
| @@ -1080,6 +1088,7 @@ export async function startProxies(options?: ProxyOptions): Promise<void> { | ||
| 1080 | 1088 | cleanUrls: proxy.cleanUrls ?? ('cleanUrls' in mergedOptions ? mergedOptions.cleanUrls : false), |
| 1081 | 1089 | vitePluginUsage: mergedOptions.vitePluginUsage, |
| 1082 | 1090 | changeOrigin: proxy.changeOrigin ?? mergedOptions.changeOrigin, |
| 1091 | imgx: proxy.imgx ?? mergedOptions.imgx, | |
| 1083 | 1092 | verbose, |
| 1084 | 1093 | _cachedSSLConfig: mergedOptions._cachedSSLConfig, |
| 1085 | 1094 | } as ProxyOption)) |
| @@ -1092,6 +1101,7 @@ export async function startProxies(options?: ProxyOptions): Promise<void> { | ||
| 1092 | 1101 | vitePluginUsage: mergedOptions.vitePluginUsage, |
| 1093 | 1102 | start: ('start' in mergedOptions) ? mergedOptions.start : undefined, |
| 1094 | 1103 | changeOrigin: mergedOptions.changeOrigin, |
| 1104 | imgx: mergedOptions.imgx, | |
| 1095 | 1105 | auth: 'auth' in mergedOptions ? mergedOptions.auth : undefined, |
| 1096 | 1106 | verbose, |
| 1097 | 1107 | _cachedSSLConfig: mergedOptions._cachedSSLConfig, |
| @@ -1355,6 +1365,7 @@ export async function startProxies(options?: ProxyOptions): Promise<void> { | ||
| 1355 | 1365 | verbose: option.verbose || false, |
| 1356 | 1366 | _cachedSSLConfig: mergedOptions._cachedSSLConfig, |
| 1357 | 1367 | changeOrigin: option.changeOrigin || false, |
| 1368 | imgx: option.imgx, | |
| 1358 | 1369 | loadBalancer: option.loadBalancer, |
| 1359 | 1370 | auth: option.auth, |
| 1360 | 1371 | path: option.path, |
| @@ -1397,6 +1408,7 @@ export async function collectRouteEntries( | ||
| 1397 | 1408 | const basePath = normalizePathPrefix(routePath) |
| 1398 | 1409 | |
| 1399 | 1410 | const auth = resolveAuth(option.auth) |
| 1411 | const imgx = resolveImgx(option.imgx) | |
| 1400 | 1412 | |
| 1401 | 1413 | // Redirect route: answer with a Location (e.g. an alternate domain → its |
| 1402 | 1414 | // canonical host). Takes precedence over proxy/static — no upstream needed. |
| @@ -1414,7 +1426,7 @@ export async function collectRouteEntries( | ||
| 1414 | 1426 | routeEntries.push({ |
| 1415 | 1427 | host: domain, |
| 1416 | 1428 | path: routePath, |
| 1417 | route: { static: resolveStaticRoute(option.static, cleanUrls), cleanUrls, basePath, auth }, | |
| 1429 | route: { static: resolveStaticRoute(option.static, cleanUrls), cleanUrls, imgx, basePath, auth }, | |
| 1418 | 1430 | }) |
| 1419 | 1431 | debugLog('proxies', `Route: ${domain}${routePath ?? ''} → static ${typeof option.static === 'string' ? option.static : option.static.dir}${auth ? ' (auth)' : ''}`, verbose) |
| 1420 | 1432 | } |
| @@ -1435,6 +1447,7 @@ export async function collectRouteEntries( | ||
| 1435 | 1447 | upstreamPool: pool, |
| 1436 | 1448 | cleanUrls, |
| 1437 | 1449 | changeOrigin: option.changeOrigin || false, |
| 1450 | imgx, | |
| 1438 | 1451 | pathRewrites: option.pathRewrites, |
| 1439 | 1452 | basePath, |
| 1440 | 1453 | auth, |