fix(dev): share auth across dev frontend proxy

This commit is contained in:
Codex
2026-05-19 04:41:28 +00:00
parent dfb344ef0c
commit 0c833a7be2
9 changed files with 184 additions and 22 deletions
@@ -95,12 +95,29 @@ const nativeServiceFailures = new Map<string, number>();
const nativeServiceEndpointCache = new Map<string, { endpoint: NativeServiceEndpoint; expiresAt: number }>();
const nativeServiceTunnels = new Map<string, NativeServiceTunnel>();
logWriter?.prune();
const forwardedResponseHeaderNames = [
"cache-control",
"content-disposition",
"etag",
"expires",
"last-modified",
"location",
"set-cookie",
"www-authenticate",
] as const;
function envString(name: string, fallback: string): string {
const value = process.env[name];
return value === undefined || value.length === 0 ? fallback : value;
}
function copyForwardedResponseHeaders(from: Headers, to: Headers): void {
for (const name of forwardedResponseHeaderNames) {
const value = from.get(name);
if (value !== null && value.length > 0) to.set(name, value);
}
}
function envNumber(name: string, fallback: number): number {
const raw = process.env[name];
if (raw === undefined || raw.trim().length === 0) return fallback;
@@ -636,17 +653,19 @@ async function fetchNativeServiceUrl(
signal: controller.signal,
});
const body = await boundedText(upstream, 8 * 1024 * 1024);
const responseHeaders = new Headers({
"content-type": upstream.headers.get("content-type") ?? "application/octet-stream",
"x-unidesk-proxy-mode": "kubernetes-native-service",
"x-unidesk-k3s-service": service.id,
"x-unidesk-k3s-service-url": upstreamUrl.origin,
"x-unidesk-upstream-duration-ms": String(Date.now() - startedAt),
"x-unidesk-response-truncated": body.truncated ? "true" : "false",
...extraHeaders,
});
copyForwardedResponseHeaders(upstream.headers, responseHeaders);
return new Response(body.text, {
status: upstream.status,
headers: {
"content-type": upstream.headers.get("content-type") ?? "application/octet-stream",
"x-unidesk-proxy-mode": "kubernetes-native-service",
"x-unidesk-k3s-service": service.id,
"x-unidesk-k3s-service-url": upstreamUrl.origin,
"x-unidesk-upstream-duration-ms": String(Date.now() - startedAt),
"x-unidesk-response-truncated": body.truncated ? "true" : "false",
...extraHeaders,
},
headers: responseHeaders,
});
} finally {
clearTimeout(timer);