Skip to content

Commit 8a940ec

Browse files
committed
Revert "Dispatch compose (#2795)"
This reverts commit 649185a.
1 parent 649185a commit 8a940ec

18 files changed

+504
-374
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,3 @@ fuzz-results-*.json
7979
# Bundle output
8080
undici-fetch.js
8181
/test/imports/undici-import.js
82-
83-
.tap

index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ const MockClient = require('./lib/mock/mock-client')
1414
const MockAgent = require('./lib/mock/mock-agent')
1515
const MockPool = require('./lib/mock/mock-pool')
1616
const mockErrors = require('./lib/mock/mock-errors')
17+
const ProxyAgent = require('./lib/proxy-agent')
18+
const RetryAgent = require('./lib/retry-agent')
19+
const RetryHandler = require('./lib/handler/RetryHandler')
1720
const { getGlobalDispatcher, setGlobalDispatcher } = require('./lib/global')
21+
const DecoratorHandler = require('./lib/handler/DecoratorHandler')
22+
const RedirectHandler = require('./lib/handler/RedirectHandler')
1823

1924
Object.assign(Dispatcher.prototype, api)
2025

@@ -23,12 +28,12 @@ module.exports.Client = Client
2328
module.exports.Pool = Pool
2429
module.exports.BalancedPool = BalancedPool
2530
module.exports.Agent = Agent
31+
module.exports.ProxyAgent = ProxyAgent
32+
module.exports.RetryAgent = RetryAgent
33+
module.exports.RetryHandler = RetryHandler
2634

27-
module.exports.interceptor = {
28-
redirect: require('./lib/interceptor/redirect'),
29-
retry: require('./lib/interceptor/retry'),
30-
proxy: require('./lib/interceptor/proxy')
31-
}
35+
module.exports.DecoratorHandler = DecoratorHandler
36+
module.exports.RedirectHandler = RedirectHandler
3237

3338
module.exports.buildConnector = buildConnector
3439
module.exports.errors = errors

lib/api/api-connect.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { AsyncResource } = require('node:async_hooks')
44
const { InvalidArgumentError, RequestAbortedError, SocketError } = require('../core/errors')
55
const util = require('../core/util')
6-
const redirect = require('../interceptor/redirect')
6+
const RedirectHandler = require('../handler/RedirectHandler')
77
const { addSignal, removeSignal } = require('./abort-signal')
88

99
class ConnectHandler extends AsyncResource {
@@ -91,9 +91,19 @@ function connect (opts, callback) {
9191
}
9292

9393
try {
94-
this
95-
.compose(redirect(opts))
96-
.dispatch({ ...opts, method: opts?.method || 'CONNECT' }, new ConnectHandler(opts, callback))
94+
const connectHandler = new ConnectHandler(opts, callback)
95+
const connectOptions = { ...opts, method: 'CONNECT' }
96+
97+
if (opts?.maxRedirections != null && (!Number.isInteger(opts?.maxRedirections) || opts?.maxRedirections < 0)) {
98+
throw new InvalidArgumentError('maxRedirections must be a positive number')
99+
}
100+
101+
if (opts?.maxRedirections > 0) {
102+
RedirectHandler.buildDispatch(this, opts.maxRedirections)(connectOptions, connectHandler)
103+
return
104+
}
105+
106+
this.dispatch(connectOptions, connectHandler)
97107
} catch (err) {
98108
if (typeof callback !== 'function') {
99109
throw err

lib/api/api-pipeline.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const {
1313
RequestAbortedError
1414
} = require('../core/errors')
1515
const util = require('../core/util')
16-
const redirect = require('../interceptor/redirect')
16+
const RedirectHandler = require('../handler/RedirectHandler')
1717
const { addSignal, removeSignal } = require('./abort-signal')
1818

1919
const kResume = Symbol('resume')
@@ -241,9 +241,15 @@ function pipeline (opts, handler) {
241241
try {
242242
const pipelineHandler = new PipelineHandler(opts, handler)
243243

244-
this
245-
.compose(redirect(opts))
246-
.dispatch({ ...opts, body: pipelineHandler.req }, pipelineHandler)
244+
if (opts?.maxRedirections != null && (!Number.isInteger(opts?.maxRedirections) || opts?.maxRedirections < 0)) {
245+
throw new InvalidArgumentError('maxRedirections must be a positive number')
246+
}
247+
248+
if (opts?.maxRedirections > 0) {
249+
RedirectHandler.buildDispatch(this, opts.maxRedirections)({ ...opts, body: pipelineHandler.req }, pipelineHandler)
250+
} else {
251+
this.dispatch({ ...opts, body: pipelineHandler.req }, pipelineHandler)
252+
}
247253

248254
return pipelineHandler.ret
249255
} catch (err) {

lib/api/api-request.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77
RequestAbortedError
88
} = require('../core/errors')
99
const util = require('../core/util')
10-
const redirect = require('../interceptor/redirect')
10+
const RedirectHandler = require('../handler/RedirectHandler')
1111
const { getResolveErrorBodyCallback } = require('./util')
1212
const { addSignal, removeSignal } = require('./abort-signal')
1313

@@ -167,9 +167,18 @@ function request (opts, callback) {
167167
}
168168

169169
try {
170-
this
171-
.compose(redirect(opts))
172-
.dispatch(opts, new RequestHandler(opts, callback))
170+
const handler = new RequestHandler(opts, callback)
171+
172+
if (opts?.maxRedirections != null && (!Number.isInteger(opts?.maxRedirections) || opts?.maxRedirections < 0)) {
173+
throw new InvalidArgumentError('maxRedirections must be a positive number')
174+
}
175+
176+
if (opts?.maxRedirections > 0) {
177+
RedirectHandler.buildDispatch(this, opts.maxRedirections)(opts, handler)
178+
return
179+
}
180+
181+
this.dispatch(opts, handler)
173182
} catch (err) {
174183
if (typeof callback !== 'function') {
175184
throw err

lib/api/api-stream.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77
RequestAbortedError
88
} = require('../core/errors')
99
const util = require('../core/util')
10-
const redirect = require('../interceptor/redirect')
10+
const RedirectHandler = require('../handler/RedirectHandler')
1111
const { getResolveErrorBodyCallback } = require('./util')
1212
const { AsyncResource } = require('node:async_hooks')
1313
const { addSignal, removeSignal } = require('./abort-signal')
@@ -208,9 +208,18 @@ function stream (opts, factory, callback) {
208208
}
209209

210210
try {
211-
this
212-
.compose(redirect(opts))
213-
.dispatch(opts, new StreamHandler(opts, factory, callback))
211+
const handler = new StreamHandler(opts, factory, callback)
212+
213+
if (opts?.maxRedirections != null && (!Number.isInteger(opts?.maxRedirections) || opts?.maxRedirections < 0)) {
214+
throw new InvalidArgumentError('maxRedirections must be a positive number')
215+
}
216+
217+
if (opts?.maxRedirections > 0) {
218+
RedirectHandler.buildDispatch(this, opts.maxRedirections)(opts, handler)
219+
return
220+
}
221+
222+
this.dispatch(opts, handler)
214223
} catch (err) {
215224
if (typeof callback !== 'function') {
216225
throw err

lib/api/api-upgrade.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const assert = require('node:assert')
44
const { AsyncResource } = require('node:async_hooks')
55
const { InvalidArgumentError, RequestAbortedError, SocketError } = require('../core/errors')
66
const util = require('../core/util')
7-
const redirect = require('../interceptor/redirect')
7+
const RedirectHandler = require('../handler/RedirectHandler')
88
const { addSignal, removeSignal } = require('./abort-signal')
99

1010
class UpgradeHandler extends AsyncResource {
@@ -88,9 +88,23 @@ function upgrade (opts, callback) {
8888
}
8989

9090
try {
91-
this
92-
.compose(redirect(opts))
93-
.dispatch({ ...opts, method: opts?.method || 'GET', upgrade: opts?.protocol || 'Websocket' }, new UpgradeHandler(opts, callback))
91+
const upgradeHandler = new UpgradeHandler(opts, callback)
92+
const upgradeOpts = {
93+
...opts,
94+
method: opts.method || 'GET',
95+
upgrade: opts.protocol || 'Websocket'
96+
}
97+
98+
if (opts?.maxRedirections != null && (!Number.isInteger(opts?.maxRedirections) || opts?.maxRedirections < 0)) {
99+
throw new InvalidArgumentError('maxRedirections must be a positive number')
100+
}
101+
102+
if (opts?.maxRedirections > 0) {
103+
RedirectHandler.buildDispatch(this, opts.maxRedirections)(upgradeOpts, upgradeHandler)
104+
return
105+
}
106+
107+
this.dispatch(upgradeOpts, upgradeHandler)
94108
} catch (err) {
95109
if (typeof callback !== 'function') {
96110
throw err

lib/dispatcher.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
const EventEmitter = require('node:events')
44

5-
const kDispatcherVersion = Symbol.for('undici.dispatcher.version')
6-
75
class Dispatcher extends EventEmitter {
8-
[kDispatcherVersion] = 1
9-
106
dispatch () {
117
throw new Error('not implemented')
128
}
@@ -18,26 +14,6 @@ class Dispatcher extends EventEmitter {
1814
destroy () {
1915
throw new Error('not implemented')
2016
}
21-
22-
compose (...interceptors) {
23-
let dispatcher = this
24-
for (const interceptor of interceptors) {
25-
if (interceptor == null) {
26-
continue
27-
}
28-
29-
if (typeof interceptor !== 'function') {
30-
throw new Error('invalid interceptor')
31-
}
32-
33-
dispatcher = interceptor(dispatcher) ?? dispatcher
34-
35-
if (dispatcher[kDispatcherVersion] !== 1) {
36-
throw new Error('invalid dispatcher')
37-
}
38-
}
39-
return dispatcher
40-
}
4117
}
4218

4319
module.exports = Dispatcher

lib/handler/DecoratorHandler.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
module.exports = class DecoratorHandler {
4+
constructor (handler) {
5+
this.handler = handler
6+
}
7+
8+
onConnect (...args) {
9+
return this.handler.onConnect(...args)
10+
}
11+
12+
onError (...args) {
13+
return this.handler.onError(...args)
14+
}
15+
16+
onUpgrade (...args) {
17+
return this.handler.onUpgrade(...args)
18+
}
19+
20+
onHeaders (...args) {
21+
return this.handler.onHeaders(...args)
22+
}
23+
24+
onData (...args) {
25+
return this.handler.onData(...args)
26+
}
27+
28+
onComplete (...args) {
29+
return this.handler.onComplete(...args)
30+
}
31+
}

lib/interceptor/redirect.js renamed to lib/handler/RedirectHandler.js

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const { kBodyUsed } = require('../core/symbols')
55
const assert = require('node:assert')
66
const { InvalidArgumentError } = require('../core/errors')
77
const EE = require('node:events')
8-
const Dispatcher = require('../dispatcher')
98

109
const redirectableStatusCodes = [300, 301, 302, 303, 307, 308]
1110

@@ -25,14 +24,27 @@ class BodyAsyncIterable {
2524
}
2625

2726
class RedirectHandler {
28-
constructor (dispatcher, dispatcherOpts, redirectOpts, handler) {
29-
const { maxRedirections } = redirectOpts ?? {}
27+
static buildDispatch (dispatcher, maxRedirections) {
28+
if (maxRedirections != null && (!Number.isInteger(maxRedirections) || maxRedirections < 0)) {
29+
throw new InvalidArgumentError('maxRedirections must be a positive number')
30+
}
31+
32+
const dispatch = dispatcher.dispatch.bind(dispatcher)
33+
return (opts, originalHandler) => dispatch(opts, new RedirectHandler(dispatch, maxRedirections, opts, originalHandler))
34+
}
35+
36+
constructor (dispatch, maxRedirections, opts, handler) {
37+
if (maxRedirections != null && (!Number.isInteger(maxRedirections) || maxRedirections < 0)) {
38+
throw new InvalidArgumentError('maxRedirections must be a positive number')
39+
}
40+
41+
util.validateHandler(handler, opts.method, opts.upgrade)
3042

31-
this.dispatcher = dispatcher
43+
this.dispatch = dispatch
3244
this.location = null
3345
this.abort = null
34-
this.opts = { ...dispatcherOpts } // opts must be a copy
35-
this.maxRedirections = maxRedirections ?? 0
46+
this.opts = { ...opts, maxRedirections: 0 } // opts must be a copy
47+
this.maxRedirections = maxRedirections
3648
this.handler = handler
3749
this.history = []
3850
this.redirectionLimitReached = false
@@ -165,7 +177,7 @@ class RedirectHandler {
165177
this.location = null
166178
this.abort = null
167179

168-
this.dispatcher.dispatch(this.opts, this)
180+
this.dispatch(this.opts, this)
169181
} else {
170182
this.handler.onComplete(trailers)
171183
}
@@ -220,38 +232,4 @@ function cleanRequestHeaders (headers, removeContent, unknownOrigin) {
220232
return ret
221233
}
222234

223-
class RedirectDispatcher extends Dispatcher {
224-
#opts
225-
#dispatcher
226-
227-
constructor (dispatcher, opts) {
228-
super()
229-
230-
this.#dispatcher = dispatcher
231-
this.#opts = opts
232-
}
233-
234-
dispatch (opts, handler) {
235-
return this.#dispatcher.dispatch(opts, new RedirectHandler(this.#dispatcher, opts, this.#opts, handler))
236-
}
237-
238-
close (...args) {
239-
return this.#dispatcher.close(...args)
240-
}
241-
242-
destroy (...args) {
243-
return this.#dispatcher.destroy(...args)
244-
}
245-
}
246-
247-
module.exports = (opts) => {
248-
if (opts?.maxRedirections == null || opts?.maxRedirections === 0) {
249-
return null
250-
}
251-
252-
if (!Number.isInteger(opts.maxRedirections) || opts.maxRedirections < 0) {
253-
throw new InvalidArgumentError('maxRedirections must be a positive number')
254-
}
255-
256-
return (dispatcher) => new RedirectDispatcher(dispatcher, opts)
257-
}
235+
module.exports = RedirectHandler

0 commit comments

Comments
 (0)