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: pass down context in onConnect #3858

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
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
Loading