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: Avoid error if connection is aborted before internal request object is created #221

Merged
merged 2 commits into from
Jan 31, 2025
Merged
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
11 changes: 8 additions & 3 deletions src/listener.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IncomingMessage, ServerResponse, OutgoingHttpHeaders } from 'node:http'
import type { Http2ServerRequest, Http2ServerResponse } from 'node:http2'
import {
getAbortController,
abortControllerKey,
newRequest,
Request as LightweightRequest,
toRequestError,
Expand Down Expand Up @@ -187,10 +187,15 @@ export const getRequestListener = (

// Detect if request was aborted.
outgoing.on('close', () => {
const abortController = req[abortControllerKey] as AbortController | undefined
if (!abortController) {
return
}

if (incoming.errored) {
req[getAbortController]().abort(incoming.errored.toString())
req[abortControllerKey].abort(incoming.errored.toString())
} else if (!outgoing.writableFinished) {
req[getAbortController]().abort('Client connection prematurely closed.')
req[abortControllerKey].abort('Client connection prematurely closed.')
}
})

Expand Down
2 changes: 1 addition & 1 deletion src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const getRequestCache = Symbol('getRequestCache')
const requestCache = Symbol('requestCache')
const incomingKey = Symbol('incomingKey')
const urlKey = Symbol('urlKey')
const abortControllerKey = Symbol('abortControllerKey')
export const abortControllerKey = Symbol('abortControllerKey')
export const getAbortController = Symbol('getAbortController')

const requestPrototype: Record<string | symbol, any> = {
Expand Down
11 changes: 11 additions & 0 deletions test/listener.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ describe('Abort request', () => {
}
}
)

it('should handle request abort without requestCache', async () => {
const fetchCallback = async () => {
// NOTE: we don't req.signal
await new Promise(() => {}) // never resolve
}
const requestListener = getRequestListener(fetchCallback)
const server = createServer(requestListener)
const req = request(server).post('/abort').timeout({ deadline: 1 })
await expect(req).rejects.toHaveProperty('timeout')
})
})

describe('overrideGlobalObjects', () => {
Expand Down
16 changes: 16 additions & 0 deletions test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Request as LightweightRequest,
GlobalRequest,
getAbortController,
abortControllerKey,
RequestError,
} from '../src/request'

Expand Down Expand Up @@ -80,6 +81,21 @@ describe('Request', () => {
expect(z).not.toBe(y)
})

it('should be able to safely check if an AbortController has been initialized by referencing the abortControllerKey', async () => {
const req = newRequest({
headers: {
host: 'localhost',
},
rawHeaders: ['host', 'localhost'],
url: '/foo.txt',
} as IncomingMessage)

expect(req[abortControllerKey]).toBeUndefined() // not initialized, do not initialize internal request object automatically

expect(req[getAbortController]()).toBeDefined()
expect(req[abortControllerKey]).toBeDefined() // initialized
})

it('Should throw error if host header contains path', async () => {
expect(() => {
newRequest({
Expand Down
Loading