Skip to content

Commit

Permalink
feat: normalize origin for BalancedPool addUpstream/removeUpstream (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mnutt authored and metcoder95 committed Mar 1, 2022
1 parent 0b653eb commit 8acb03f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/balanced-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
} = require('./pool-base')
const Pool = require('./pool')
const { kUrl } = require('./core/symbols')
const { parseOrigin } = require('./core/util')
const kFactory = Symbol('factory')

const kOptions = Symbol('options')
Expand Down Expand Up @@ -44,22 +45,26 @@ class BalancedPool extends PoolBase {
}

addUpstream (upstream) {
const upstreamOrigin = parseOrigin(upstream).origin

if (this[kClients].find((pool) => (
pool[kUrl].origin === upstream &&
pool[kUrl].origin === upstreamOrigin &&
pool.closed !== true &&
pool.destroyed !== true
))) {
return this
}

this[kAddClient](this[kFactory](upstream, Object.assign({}, this[kOptions])))
this[kAddClient](this[kFactory](upstreamOrigin, Object.assign({}, this[kOptions])))

return this
}

removeUpstream (upstream) {
const upstreamOrigin = parseOrigin(upstream).origin

const pool = this[kClients].find((pool) => (
pool[kUrl].origin === upstream &&
pool[kUrl].origin === upstreamOrigin &&
pool.closed !== true &&
pool.destroyed !== true
))
Expand Down
7 changes: 7 additions & 0 deletions test/balanced-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ test('upstream add/remove/get', async (t) => {
client.removeUpstream('http://localhost:2424')
t.same(client.upstreams, [])

client.addUpstream('http://localhost:80')
client.addUpstream('http://localhost:80')
client.addUpstream(new URL('http://localhost:80'))
t.same(client.upstreams, ['http://localhost'])
client.removeUpstream('http://localhost:80')
t.same(client.upstreams, [])

t.throws(() => client.dispatch())

const p = client.close()
Expand Down

0 comments on commit 8acb03f

Please sign in to comment.