diff --git a/lib/dispatcher/dispatcher.js b/lib/dispatcher/dispatcher.js index a5a2e8edd0f..854fe0d8526 100644 --- a/lib/dispatcher/dispatcher.js +++ b/lib/dispatcher/dispatcher.js @@ -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') } @@ -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 @@ -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 } } diff --git a/lib/interceptor/retry.js b/lib/interceptor/retry.js index c19f357e5e6..251bd8a3e25 100644 --- a/lib/interceptor/retry.js +++ b/lib/interceptor/retry.js @@ -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) } diff --git a/test/interceptors/retry.js b/test/interceptors/retry.js index 1cf37398f19..6e6f997cafa 100644 --- a/test/interceptors/retry.js +++ b/test/interceptors/retry.js @@ -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 => {