Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: detection of client premature close #218

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ export const getRequestListener = (
outgoing.on('close', () => {
if (incoming.errored) {
req[getAbortController]().abort(incoming.errored.toString())
} else if (!outgoing.writableFinished) {
req[getAbortController]().abort('Client connection prematurely closed.')
}
})

Expand Down
13 changes: 5 additions & 8 deletions test/listener.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('Abort request', () => {
server.close()
})

it('should emit an abort event when the nodejs request is aborted', async () => {
it.each(['get', 'put', 'patch', 'delete'] as const)('should emit an abort event when the nodejs %s request is aborted', async (method) => {
const requests: Request[] = []
const abortedPromise = new Promise<void>((resolve) => {
onAbort = (req) => {
Expand All @@ -189,8 +189,7 @@ describe('Abort request', () => {
}
})

const req = request(server)
.get('/abort')
const req = request(server)[method]('/abort')
.end(() => {})

await reqReadyPromise
Expand All @@ -205,7 +204,7 @@ describe('Abort request', () => {
expect(abortedReq.signal.aborted).toBe(true)
})

it('should emit an abort event when the nodejs request is aborted on multiple requests', async () => {
it.each(['get', 'post', 'head', 'patch', 'delete', 'put'] as const)('should emit an abort event when the nodejs request is aborted on multiple %s requests', async (method) => {
const requests: Request[] = []

{
Expand All @@ -220,8 +219,7 @@ describe('Abort request', () => {
reqReadyResolve = r
})

const req = request(server)
.get('/abort')
const req = request(server)[method]('/abort')
.end(() => {})

await reqReadyPromise
Expand Down Expand Up @@ -250,8 +248,7 @@ describe('Abort request', () => {
reqReadyResolve = r
})

const req = request(server)
.get('/abort')
const req = request(server)[method]('/abort')
.end(() => {})

await reqReadyPromise
Expand Down
Loading