Skip to content

Commit 081810c

Browse files
authored
refactor(gateway): flatten HTTP request stages (#127264)
1 parent 6448550 commit 081810c

1 file changed

Lines changed: 55 additions & 65 deletions

File tree

‎src/gateway/server-http.ts‎

Lines changed: 55 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,13 @@ function isWebSocketUpgradeRequest(req: IncomingMessage): boolean {
137137
);
138138
}
139139

140-
type GatewayHttpRequestStage = {
141-
run: () => Promise<boolean> | boolean;
142-
};
140+
type GatewayHttpRequestStage = () => Promise<boolean> | boolean;
143141

144142
async function runGatewayHttpRequestStages(
145143
stages: readonly GatewayHttpRequestStage[],
146144
): Promise<boolean> {
147145
for (const stage of stages) {
148-
if (await stage.run()) {
146+
if (await stage()) {
149147
return true;
150148
}
151149
}
@@ -335,34 +333,30 @@ export function createGatewayHttpServer(opts: {
335333
return true;
336334
};
337335
const requestStages: GatewayHttpRequestStage[] = [
338-
{
339-
run: () =>
340-
handleGatewayProbeRequest(
341-
req,
342-
res,
343-
scopedRequestPath,
344-
resolvedAuthValue,
345-
trustedProxies,
346-
allowRealIpFallback,
347-
rateLimiter,
348-
getReadiness,
349-
getStartup,
350-
),
351-
},
336+
() =>
337+
handleGatewayProbeRequest(
338+
req,
339+
res,
340+
scopedRequestPath,
341+
resolvedAuthValue,
342+
trustedProxies,
343+
allowRealIpFallback,
344+
rateLimiter,
345+
getReadiness,
346+
getStartup,
347+
),
352348
];
353349
const addRequestStage = (
354350
enabled: boolean,
355-
run: GatewayHttpRequestStage["run"],
351+
stage: GatewayHttpRequestStage,
356352
admitted = false,
357353
) => {
358354
if (enabled) {
359-
requestStages.push({
360-
run: admitted ? () => runWithGatewayHttpWorkAdmission(res, run) : run,
361-
});
355+
requestStages.push(admitted ? () => runWithGatewayHttpWorkAdmission(res, stage) : stage);
362356
}
363357
};
364-
const addAdmittedStage = (enabled: boolean, run: GatewayHttpRequestStage["run"]) =>
365-
addRequestStage(enabled, run, true);
358+
const addAdmittedStage = (enabled: boolean, stage: GatewayHttpRequestStage) =>
359+
addRequestStage(enabled, stage, true);
366360

367361
const workerGatewayRoute = classifyWorkerGatewayPath(scopedRequestPath);
368362
addRequestStage(workerGatewayRoute !== "outside", () => {
@@ -519,16 +513,16 @@ export function createGatewayHttpServer(opts: {
519513
configSnapshot.mcp?.apps?.enabled === true &&
520514
(mcpAppRoute === "shell" || mcpAppRoute === "view")
521515
) {
522-
requestStages.push({
523-
run: async () =>
516+
requestStages.push(
517+
async () =>
524518
await runWithGatewayHttpWorkAdmission(res, async () => {
525519
const standalone = await getMcpAppStandaloneModule();
526520
return await standalone.handleMcpAppStandaloneHttpRequest(req, res, {
527521
sandboxPort: configSnapshot.mcp?.apps?.sandboxPort,
528522
sandboxOrigin: configSnapshot.mcp?.apps?.sandboxOrigin,
529523
});
530524
}),
531-
});
525+
);
532526
}
533527
// Core and recovery routes run first, then plugin routes, then read-only Control UI
534528
// surfaces. Non-GET requests the SPA does not claim reach the startup 503 before final 404.
@@ -538,46 +532,42 @@ export function createGatewayHttpServer(opts: {
538532
let pluginRequestOperatorScopes: string[] | undefined;
539533
// Auth and dispatch stay separate so authorized context reaches the handler.
540534
requestStages.push(
541-
{
542-
run: async () => {
543-
if (
544-
!(shouldEnforcePluginGatewayAuth ?? shouldEnforceDefaultPluginGatewayAuth)(
545-
pluginPathContext,
546-
) ||
547-
(await getCachedPluginGatewayAuthBypassPaths(configSnapshot)).has(scopedRequestPath)
548-
) {
549-
return false;
550-
}
551-
// Bypass paths come only from activated channel plugins; every other protected
552-
// route must authorize before runtime scopes are derived.
553-
const { authorizePluginGatewayHttpRequestOrReply } = await getHttpAuthUtilsModule();
554-
const { resolvePluginRouteRuntimeOperatorScopes } =
555-
await getPluginRouteRuntimeScopesModule();
556-
const authResult = await authorizePluginGatewayHttpRequestOrReply({
557-
req,
558-
res,
559-
...routeAuth,
560-
requestPath: scopedRequestPath,
561-
resolveOperatorScopes: resolvePluginRouteRuntimeOperatorScopes,
562-
});
563-
if (!authResult) {
564-
return true;
565-
}
566-
pluginGatewayAuthSatisfied = true;
567-
pluginGatewayRequestAuth = authResult.requestAuth;
568-
pluginRequestOperatorScopes = authResult.operatorScopes;
535+
async () => {
536+
if (
537+
!(shouldEnforcePluginGatewayAuth ?? shouldEnforceDefaultPluginGatewayAuth)(
538+
pluginPathContext,
539+
) ||
540+
(await getCachedPluginGatewayAuthBypassPaths(configSnapshot)).has(scopedRequestPath)
541+
) {
569542
return false;
570-
},
571-
},
572-
{
573-
run: () =>
574-
handlePluginRequest(req, res, pluginPathContext, {
575-
gatewayAuthSatisfied: pluginGatewayAuthSatisfied,
576-
gatewayRequestAuth: pluginGatewayRequestAuth,
577-
gatewayRequestOperatorScopes: pluginRequestOperatorScopes,
578-
gatewayRequestClientIp: requestClientIp,
579-
}),
543+
}
544+
// Bypass paths come only from activated channel plugins; every other protected
545+
// route must authorize before runtime scopes are derived.
546+
const { authorizePluginGatewayHttpRequestOrReply } = await getHttpAuthUtilsModule();
547+
const { resolvePluginRouteRuntimeOperatorScopes } =
548+
await getPluginRouteRuntimeScopesModule();
549+
const authResult = await authorizePluginGatewayHttpRequestOrReply({
550+
req,
551+
res,
552+
...routeAuth,
553+
requestPath: scopedRequestPath,
554+
resolveOperatorScopes: resolvePluginRouteRuntimeOperatorScopes,
555+
});
556+
if (!authResult) {
557+
return true;
558+
}
559+
pluginGatewayAuthSatisfied = true;
560+
pluginGatewayRequestAuth = authResult.requestAuth;
561+
pluginRequestOperatorScopes = authResult.operatorScopes;
562+
return false;
580563
},
564+
() =>
565+
handlePluginRequest(req, res, pluginPathContext, {
566+
gatewayAuthSatisfied: pluginGatewayAuthSatisfied,
567+
gatewayRequestAuth: pluginGatewayRequestAuth,
568+
gatewayRequestOperatorScopes: pluginRequestOperatorScopes,
569+
gatewayRequestClientIp: requestClientIp,
570+
}),
581571
);
582572
}
583573

0 commit comments

Comments
 (0)