Skip to content

Commit

Permalink
fix: pass down context in onConnect (#3858)
Browse files Browse the repository at this point in the history
* fix: pass down context in onConnect

* tests: check if context is present

---------

Co-authored-by: Carlos Fuentes <[email protected]>
  • Loading branch information
DTrombett and metcoder95 authored Nov 22, 2024
1 parent 0ea7897 commit a781473
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/handler/cache-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ class CacheHandler extends DecoratorHandler {
this.#handler = handler
}

onConnect (abort) {
onConnect (...args) {
if (this.#writeStream) {
this.#writeStream.destroy()
this.#writeStream = undefined
}

if (typeof this.#handler.onConnect === 'function') {
this.#handler.onConnect(abort)
this.#handler.onConnect(...args)
}
}

Expand Down
24 changes: 9 additions & 15 deletions lib/handler/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class RetryHandler {
this.opts = { ...dispatchOpts, body: wrapRequestBody(opts.body) }
this.abort = null
this.aborted = false
this.connectCalled = false
this.retryOpts = {
retry: retryFn ?? RetryHandler[kRetryHandlerDefaultRetry],
retryAfter: retryAfter ?? true,
Expand Down Expand Up @@ -68,16 +69,6 @@ class RetryHandler {
this.end = null
this.etag = null
this.resume = null

// Handle possible onConnect duplication
this.handler.onConnect(reason => {
this.aborted = true
if (this.abort) {
this.abort(reason)
} else {
this.reason = reason
}
})
}

onRequestSent () {
Expand All @@ -92,11 +83,14 @@ class RetryHandler {
}
}

onConnect (abort) {
if (this.aborted) {
abort(this.reason)
} else {
this.abort = abort
onConnect (abort, context) {
this.abort = abort
if (!this.connectCalled) {
this.connectCalled = true
this.handler.onConnect(reason => {
this.aborted = true
this.abort(reason)
}, context)
}
}

Expand Down
37 changes: 36 additions & 1 deletion test/interceptors/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { createServer } = require('node:http')
const { once } = require('node:events')

const { Client, interceptors } = require('../..')
const { retry } = interceptors
const { retry, redirect } = interceptors

test('Should retry status code', async t => {
t = tspl(t, { plan: 4 })
Expand Down Expand Up @@ -243,6 +243,41 @@ test('Should retry with defaults', async t => {
t.equal(await response.body.text(), 'hello world!')
})

test('Should pass context from other interceptors', async t => {
t = tspl(t, { plan: 2 })

const server = createServer()
const requestOptions = {
method: 'GET',
path: '/'
}

server.on('request', (req, res) => {
res.writeHead(200)
res.end('hello world!')
})

server.listen(0)

await once(server, 'listening')

const client = new Client(
`http://localhost:${server.address().port}`
).compose(redirect({ maxRedirections: 1 }), retry())

after(async () => {
await client.close()
server.close()

await once(server, 'close')
})

const response = await client.request(requestOptions)

t.equal(response.statusCode, 200)
t.deepStrictEqual(response.context, { history: [] })
})

test('Should handle 206 partial content', async t => {
t = tspl(t, { plan: 5 })

Expand Down

0 comments on commit a781473

Please sign in to comment.