Skip to content

Commit

Permalink
refactor: rewrite interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Mar 3, 2024
1 parent 5588a1d commit 04c8acb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 46 deletions.
16 changes: 7 additions & 9 deletions lib/dispatcher/dispatcher.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
'use strict'
const EventEmitter = require('node:events')

const kDispatcherVersion = Symbol.for('undici.dispatcher.version')

class Dispatcher extends EventEmitter {
[kDispatcherVersion] = 1

dispatch () {
throw new Error('not implemented')
}
Expand All @@ -21,7 +17,6 @@ class Dispatcher extends EventEmitter {
compose (...args) {
// So we handle [interceptor1, interceptor2] or interceptor1, interceptor2, ...
const interceptors = Array.isArray(args[0]) ? args[0] : args
let dispatcher = this
for (const interceptor of interceptors) {
if (interceptor == null) {
continue
Expand All @@ -31,13 +26,16 @@ class Dispatcher extends EventEmitter {
throw new Error('invalid interceptor')
}

dispatcher = interceptor(dispatcher) ?? dispatcher
const newDispatch = interceptor(this)

if (dispatcher[kDispatcherVersion] !== 1) {
throw new Error('invalid dispatcher')
if (newDispatch == null || typeof newDispatch !== 'function' || newDispatch.length !== 2) {
throw new Error('invalid interceptor')
}

this.dispatch = newDispatch
}
return dispatcher

return this
}
}

Expand Down
50 changes: 15 additions & 35 deletions lib/interceptor/retry.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,20 @@
'use strict'

const Dispatcher = require('../dispatcher/dispatcher')
const RetryHandler = require('../handler/retry-handler')

class RetryDispatcher extends Dispatcher {
#dispatcher
#opts

constructor (dispatcher, opts) {
super()

this.#dispatcher = dispatcher
this.#opts = opts
}

dispatch (opts, handler) {
opts.retryOptions = { ...this.#opts, ...opts.retryOptions }

return this.#dispatcher.dispatch(
opts,
new RetryHandler(opts, {
handler,
dispatch: this.#dispatcher.dispatch.bind(this.#dispatcher)
})
)
module.exports = globalOpts => {
return dispatcher => {
const bindedDispatch = dispatcher.dispatch.bind(dispatcher)

return function retryInterceptor (opts, handler) {
opts.retryOptions = { ...globalOpts, ...opts.retryOptions }

return bindedDispatch(
opts,
new RetryHandler(opts, {
handler,
dispatch: bindedDispatch
})
)
}
}

close (...args) {
return this.#dispatcher.close(...args)
}

destroy (...args) {
return this.#dispatcher.destroy(...args)
}
}

module.exports = opts => {
return dispatcher => new RetryDispatcher(dispatcher, opts)
}
3 changes: 1 addition & 2 deletions test/interceptors/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const { test, after } = require('node:test')
const { createServer } = require('node:http')
const { once } = require('node:events')

const { RetryHandler, Client, interceptors } = require('../..')
const { RequestHandler } = require('../../lib/api/api-request')
const { Client, interceptors } = require('../..')
const { retry } = interceptors

test('Should retry status code', async t => {
Expand Down

0 comments on commit 04c8acb

Please sign in to comment.