Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.

Commit 23e44b2

Browse files
author
Martin Cizek
committed
cr
1 parent 543cb97 commit 23e44b2

File tree

3 files changed

+25
-38
lines changed

3 files changed

+25
-38
lines changed

docs/client.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ client is:
2424

2525
|Attribute |Description |
2626
|---------------|-----------------------------------------------------------|
27-
|url |A valid LDAP URL(s) (proto/host(s)/port(s) |
27+
|url |A string or array of valid LDAP URL(s) (proto/host/port) |
2828
|socketPath |Socket path if using AF\_UNIX sockets |
2929
|log |A compatible logger instance (Default: no-op logger) |
3030
|timeout |Milliseconds client should let operations live for before timing out (Default: Infinity)|

lib/client/client.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,14 @@ function Client (options) {
110110
EventEmitter.call(this, options)
111111

112112
var self = this
113-
this.servers = []
114-
if (options.url) {
115-
var urls = options.url
116-
117-
if (typeof options.url === 'string') {
118-
var [, protocol, hosts, path] = options.url.match(/^([a-zA-Z]+:\/\/)([^/]*)(.*)/)
119-
120-
urls = hosts.split(',').map((host) => {
121-
return `${protocol}${host}${path}`
122-
})
123-
}
124-
125-
this.servers = urls.map(url.parse)
126-
}
127-
113+
this.urls = options.url ? [].concat(options.url).map(url.parse) : []
128114
// select random server at the start
129-
this.nextServer = Math.floor(Math.random() * this.servers.length)
115+
this._nextServer = Math.floor(Math.random() * this.urls.length)
116+
// updated in connectSocket() after each connect
117+
this.host = undefined
118+
this.port = false
119+
this.secure = false
120+
this.url = undefined
130121
this.tlsOptions = options.tlsOptions
131122
this.socketPath = options.socketPath || false
132123

@@ -803,8 +794,15 @@ Client.prototype.connect = function connect () {
803794

804795
// Establish basic socket connection
805796
function connectSocket (cb) {
806-
self.nextServer = (self.nextServer + 1) % self.servers.length
807-
self.url = self.servers[self.nextServer]
797+
self._nextServer = (self._nextServer + 1) % self.urls.length
798+
self.url = self.urls[self._nextServer]
799+
800+
if (self.url) {
801+
self.host = url.hostname
802+
self.port = url.port
803+
self.secure = url.secure
804+
}
805+
808806
cb = once(cb)
809807

810808
function onResult (err, res) {
@@ -833,8 +831,8 @@ Client.prototype.connect = function connect () {
833831
setupClient(cb)
834832
}
835833

836-
var port = (self.servers.length && self.servers[self.nextServer].port) || self.socketPath
837-
var hostname = (self.servers.length && self.servers[self.nextServer].hostname) || undefined
834+
var port = (self.urls.length && self.urls[self._nextServer].port) || self.socketPath
835+
var hostname = (self.urls.length && self.urls[self._nextServer].hostname) || undefined
838836
if (self.secure) {
839837
socket = tls.connect(port, hostname, self.tlsOptions)
840838
socket.once('secureConnect', onConnect)
@@ -843,7 +841,7 @@ Client.prototype.connect = function connect () {
843841
socket.once('connect', onConnect)
844842
}
845843
socket.once('error', onResult)
846-
initSocket()
844+
initSocket(self.url)
847845

848846
// Setup connection timeout handling, if desired
849847
if (self.connectTimeout) {
@@ -858,9 +856,9 @@ Client.prototype.connect = function connect () {
858856
}
859857

860858
// Initialize socket events and LDAP parser.
861-
function initSocket () {
859+
function initSocket (url) {
862860
tracker = messageTrackerFactory({
863-
id: self.url ? self.url.href : self.socketPath,
861+
id: url ? url.href : self.socketPath,
864862
parser: new Parser({ log: log })
865863
})
866864

@@ -989,8 +987,8 @@ Client.prototype.connect = function connect () {
989987
maxDelay: this.reconnect.maxDelay
990988
})
991989
failAfter = this.reconnect.failAfter
992-
if (this.servers.length > 1 && failAfter) {
993-
failAfter *= this.servers.length
990+
if (this.urls.length > 1 && failAfter) {
991+
failAfter *= this.urls.length
994992
}
995993
} else {
996994
retry = backoff.exponential({

test/client.test.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -388,18 +388,7 @@ tap.test('createClient', t => {
388388
connectTimeout: 1
389389
})
390390

391-
t.equal(client.servers.length, 2)
392-
})
393-
})
394-
395-
t.test('url string is correctly parsed and assigned', async t => {
396-
getPort().then(function (unusedPortNumber) {
397-
const client = ldap.createClient({
398-
url: `ldap://0.0.0.0:${unusedPortNumber},0.0.0.1:${unusedPortNumber}`,
399-
connectTimeout: 1
400-
})
401-
402-
t.equal(client.servers.length, 2)
391+
t.equal(client.urls.length, 2)
403392
})
404393
})
405394

0 commit comments

Comments
 (0)