Skip to content

Commit

Permalink
feat: undici.compose
Browse files Browse the repository at this point in the history
Refs: #3370
Refs: #3368
  • Loading branch information
ronag committed Jun 25, 2024
1 parent dd98299 commit 93e323c
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 7 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ const { getGlobalDispatcher, setGlobalDispatcher } = require('./lib/global')
const DecoratorHandler = require('./lib/handler/decorator-handler')
const RedirectHandler = require('./lib/handler/redirect-handler')
const createRedirectInterceptor = require('./lib/interceptor/redirect-interceptor')
const compose = require('./lib/util/compose')

Object.assign(Dispatcher.prototype, api)

module.exports.compose = compose
module.exports.Dispatcher = Dispatcher
module.exports.Client = Client
module.exports.Pool = Pool
Expand Down
10 changes: 8 additions & 2 deletions lib/api/api-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,14 @@ function connect (opts, callback) {
}

try {
const connectHandler = new ConnectHandler(opts, callback)
this.dispatch({ ...opts, method: 'CONNECT' }, connectHandler)
const handler = new ConnectHandler(opts, callback)
opts = { ...opts, method: 'CONNECT' }

if (typeof this === 'function') {
this(opts, handler)
} else {
this.dispatch(opts, handler)
}
} catch (err) {
if (typeof callback !== 'function') {
throw err
Expand Down
9 changes: 8 additions & 1 deletion lib/api/api-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,14 @@ class PipelineHandler extends AsyncResource {
function pipeline (opts, handler) {
try {
const pipelineHandler = new PipelineHandler(opts, handler)
this.dispatch({ ...opts, body: pipelineHandler.req }, pipelineHandler)
opts = { ...opts, body: pipelineHandler.req }

if (typeof this === 'function') {
this(opts, handler)
} else {
this.dispatch(opts, handler)
}

return pipelineHandler.ret
} catch (err) {
return new PassThrough().destroy(err)
Expand Down
8 changes: 7 additions & 1 deletion lib/api/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,13 @@ function request (opts, callback) {
}

try {
this.dispatch(opts, new RequestHandler(opts, callback))
const handler = new RequestHandler(opts, callback)

if (typeof this === 'function') {
this(opts, handler)
} else {
this.dispatch(opts, handler)
}
} catch (err) {
if (typeof callback !== 'function') {
throw err
Expand Down
8 changes: 7 additions & 1 deletion lib/api/api-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ function stream (opts, factory, callback) {
}

try {
this.dispatch(opts, new StreamHandler(opts, factory, callback))
const handler = new StreamHandler(opts, factory, callback)

if (typeof this === 'function') {
this(opts, handler)
} else {
this.dispatch(opts, handler)
}
} catch (err) {
if (typeof callback !== 'function') {
throw err
Expand Down
11 changes: 9 additions & 2 deletions lib/api/api-upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,18 @@ function upgrade (opts, callback) {

try {
const upgradeHandler = new UpgradeHandler(opts, callback)
this.dispatch({

opts = {
...opts,
method: opts.method || 'GET',
upgrade: opts.protocol || 'Websocket'
}, upgradeHandler)
}

if (typeof this === 'function') {
this(opts, upgradeHandler)
} else {
this.dispatch(opts, upgradeHandler)
}
} catch (err) {
if (typeof callback !== 'function') {
throw err
Expand Down
1 change: 1 addition & 0 deletions lib/dispatcher/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Dispatcher extends EventEmitter {
throw new Error('not implemented')
}

/** @deprecated in favor of undici.compose */
compose (...args) {
// So we handle [interceptor1, interceptor2] or interceptor1, interceptor2, ...
const interceptors = Array.isArray(args[0]) ? args[0] : args
Expand Down
30 changes: 30 additions & 0 deletions lib/util/compose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

module.exports = function compose (...args) {
// So we handle [interceptor1, interceptor2] or interceptor1, interceptor2, ...
const interceptors = Array.isArray(args[0]) ? args[0] : args

let dispatch

for (let interceptor of interceptors) {
if (interceptor == null) {
continue
}

if (typeof interceptor !== 'function') {
if (interceptor?.dispatch === 'function') {
interceptor = interceptor.dispatch.bind(interceptor)
} else {
throw new TypeError(`invalid interceptor, expected function received ${typeof interceptor}`)
}
}

dispatch = interceptor(dispatch)

if (dispatch == null || typeof dispatch !== 'function' || dispatch.length !== 2) {
throw new TypeError('invalid interceptor')
}
}

return dispatch
}

0 comments on commit 93e323c

Please sign in to comment.