Skip to content

Commit

Permalink
refactor: Client.clients & Agent is a Client.
Browse files Browse the repository at this point in the history
Refs: #616
  • Loading branch information
ronag committed Mar 25, 2021
1 parent b3dd69f commit c9192de
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
25 changes: 10 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
const Client = require('./lib/core/client')
const errors = require('./lib/core/errors')
const Pool = require('./lib/client-pool')
const { Agent, getGlobalAgent, setGlobalAgent } = require('./lib/agent')
const { Agent, getGlobalAgent, setGlobalAgent } = require('./lib/client-agent')
const util = require('./lib/core/util')
const { InvalidArgumentError, InvalidReturnValueError } = require('./lib/core/errors')
const { InvalidArgumentError } = require('./lib/core/errors')
const api = require('./lib/api')

Object.assign(Client.prototype, api)
Object.assign(Pool.prototype, api)
Object.assign(Agent.prototype, api)

function undici (url, opts) {
return new Pool(url, opts)
Expand All @@ -25,7 +26,7 @@ module.exports.Agent = Agent
module.exports.setGlobalAgent = setGlobalAgent
module.exports.getGlobalAgent = getGlobalAgent

function dispatchFromAgent (requestType) {
function dispatchFromAgent (fn) {
return (url, { agent = getGlobalAgent(), method = 'GET', ...opts } = {}, ...additionalArgs) => {
if (opts.path != null) {
throw new InvalidArgumentError('unsupported opts.path')
Expand All @@ -34,18 +35,12 @@ function dispatchFromAgent (requestType) {
const { origin, pathname, search } = util.parseURL(url)
const path = `${pathname || '/'}${search || ''}`

const client = agent.get(origin)

if (client && typeof client[requestType] !== 'function') {
throw new InvalidReturnValueError(`Client returned from Agent.get() does not implement method ${requestType}`)
}

return client[requestType]({ ...opts, method, path }, ...additionalArgs)
return fn.call(agent, { ...opts, origin, method, path }, ...additionalArgs)
}
}

module.exports.request = dispatchFromAgent('request')
module.exports.stream = dispatchFromAgent('stream')
module.exports.pipeline = dispatchFromAgent('pipeline')
module.exports.connect = dispatchFromAgent('connect')
module.exports.upgrade = dispatchFromAgent('upgrade')
module.exports.request = dispatchFromAgent(api.request)
module.exports.stream = dispatchFromAgent(api.stream)
module.exports.pipeline = dispatchFromAgent(api.pipeline)
module.exports.connect = dispatchFromAgent(api.connect)
module.exports.upgrade = dispatchFromAgent(api.upgrade)
31 changes: 25 additions & 6 deletions lib/agent.js → lib/client-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,41 @@ class Agent extends EventEmitter {
}
}

get (origin) {
if (typeof origin !== 'string' || origin === '') {
// TODO
// url // This doesn't make sense for agent... return null?
// connected
// busy
// pending
// running
// size
// destroyed
// closed
// 'drain'

get clients () {
return [...this[kCache].values()]
}

dispatch (opts, handler) {
if (!opts || typeof opts !== 'object') {
throw new InvalidArgumentError('opts must be an object.')
}

if (typeof opts.origin !== 'string' || opts.origin === '') {
throw new InvalidArgumentError('Origin must be a non-empty string.')
}

let pool = this[kCache].get(origin)
let pool = this[kCache].get(opts.origin)

if (!pool) {
pool = this[kFactory](origin)
pool = this[kFactory](opts.origin)
.on('connect', this[kOnConnect])
.on('disconnect', this[kOnDisconnect])

this[kCache].set(origin, pool)
this[kCache].set(opts.origin, pool)
}

return pool
return pool.dispatch(opts, handler)
}

close () {
Expand Down
8 changes: 8 additions & 0 deletions lib/client-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,16 @@ class Pool extends EventEmitter {
return this[kClosedPromise] != null
}

get clients () {
return [...this[kClients]]
}

dispatch (opts, handler) {
try {
if (opts.origin && opts.origin !== this.url.origin) {
throw new InvalidArgumentError('origin')
}

if (this[kDestroyed]) {
throw new ClientDestroyedError()
}
Expand Down
6 changes: 6 additions & 0 deletions lib/core/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ class Client extends EventEmitter {
resume(this, true)
}

// TODO (fix): clients?

get connected () {
return (
this[kSocket] &&
Expand Down Expand Up @@ -242,6 +244,10 @@ class Client extends EventEmitter {
}

dispatch (opts, handler) {
if (opts.origin && opts.origin !== this.url.origin) {
throw new InvalidArgumentError('origin')
}

try {
const request = new Request(opts, handler)
if (this[kDestroyed]) {
Expand Down

0 comments on commit c9192de

Please sign in to comment.