Skip to content

Commit

Permalink
fix(server): do not listen the connection for abort but the request i…
Browse files Browse the repository at this point in the history
…tself
  • Loading branch information
ardatan committed Dec 13, 2023
1 parent c320861 commit 340c719
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-cherries-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@whatwg-node/server": patch
---

Handle aborted requests correctly
19 changes: 12 additions & 7 deletions packages/server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export interface NodeRequest {
headers?: any;
req?: IncomingMessage | Http2ServerRequest;
raw?: IncomingMessage | Http2ServerRequest;
connection?: Socket;
socket?: Socket;
query?: any;
once?(event: string, listener: (...args: any[]) => void): void;
aborted?: boolean;
}

export type NodeResponse = ServerResponse | Http2ServerResponse;
Expand Down Expand Up @@ -134,18 +134,23 @@ export function normalizeNodeRequest(
if (RequestCtor !== globalThis.Request) {
signal = new ServerAdapterRequestAbortSignal();

if (rawRequest.connection?.once) {
rawRequest.connection?.once('close', () =>
(signal as ServerAdapterRequestAbortSignal).sendAbort(),
);
if (rawRequest?.once) {
rawRequest.once('close', () => {
if (rawRequest.aborted) {
(signal as ServerAdapterRequestAbortSignal).sendAbort();
}
});
}
} else {
const controller = new AbortController();
signal = controller.signal;

if (rawRequest.once) {
rawRequest.once('end', () => controller.abort());
rawRequest.once('close', () => controller.abort());
rawRequest.once('close', () => {
if (rawRequest.aborted) {
controller.abort();
}
});
}
}

Expand Down

0 comments on commit 340c719

Please sign in to comment.