Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion apps/server/src/httpResponseErrorGuard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// @effect-diagnostics nodeBuiltinImport:off
import * as NodeHttp from "node:http";
import * as NodeNet from "node:net";
import { afterEach, describe, expect, it } from "vite-plus/test";
import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
import * as Effect from "effect/Effect";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import { afterEach, describe, expect, it } from "@effect/vitest";

import * as HttpResponseErrorGuard from "./httpResponseErrorGuard.ts";

Expand Down Expand Up @@ -239,4 +242,30 @@ describe("rejectRequestsUntilServing", () => {
expect(servingUpgrade.headers["retry-after"]).toBeUndefined();
expect(writeErrors).toEqual([]);
});

it.effect(
"rejects early requests on NodeHttpServer, which adds its own listener before listening",
() =>
Effect.scoped(
Effect.gen(function* () {
const server = yield* NodeHttpServer.make(
() =>
HttpResponseErrorGuard.rejectRequestsUntilServing(
HttpResponseErrorGuard.keepIdleConnections(
guardHttpResponseWriteErrors(NodeHttp.createServer()),
),
),
{ host: "127.0.0.1", port: 0 },
);
const port = (server.address as { readonly port: number }).port;

const early = yield* Effect.promise(() => fetchResponse(port, "/"));
expect(early.statusCode).toBe(503);
expect(early.headers["retry-after"]).toBe("1");

yield* server.serve(Effect.succeed(HttpServerResponse.empty({ status: 204 })));
expect(yield* Effect.promise(() => fetchStatus(port, "/"))).toBe(204);
}),
),
);
});
6 changes: 3 additions & 3 deletions apps/server/src/httpResponseErrorGuard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export function guardHttpResponseWriteErrors<T extends NodeHttp.Server>(
// into a closed connection, and a slow client reconnects in the middle of a connect.
const HTTP_KEEP_ALIVE_TIMEOUT_MS = 95_000;

/** Install before `rejectRequestsUntilServing`, whose handoff starts at the next request listener. */
export function keepIdleConnections<T extends NodeHttp.Server>(server: T): T {
server.keepAliveTimeout = HTTP_KEEP_ALIVE_TIMEOUT_MS;
// `close` drops only the connections idle at that moment. One that finishes
Expand All @@ -48,8 +47,8 @@ export function keepIdleConnections<T extends NodeHttp.Server>(server: T): T {

/**
* NodeHttpServer listens before serve() attaches its handlers, so requests in
* between would hang. Install this after the write-error guards so only the
* serving handlers trigger the handoff.
* between would hang. Its own pre-listen fallback stays silent while any other
* request listener exists, so only a listener added after listening takes over.
*/
export function rejectRequestsUntilServing<T extends NodeHttp.Server>(server: T): T {
const rejectRequest = (_request: NodeHttp.IncomingMessage, response: NodeHttp.ServerResponse) => {
Expand All @@ -64,6 +63,7 @@ export function rejectRequestsUntilServing<T extends NodeHttp.Server>(server: T)
server.on("upgrade", rejectUpgrade);
// Node emits newListener before adding the handler, making the handoff synchronous.
server.on("newListener", function onNewListener(event) {
if (!server.listening) return;
if (event === "request") server.off("request", rejectRequest);
if (event === "upgrade") server.off("upgrade", rejectUpgrade);
if (
Expand Down
Loading