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

Commit 3ca2c26

Browse files
author
Martin Cizek
committed
Support for arrays in url parameter
1 parent 7d52f86 commit 3ca2c26

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

docs/client.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The code to create a new client looks like:
1414

1515
var ldap = require('ldapjs');
1616
var client = ldap.createClient({
17-
url: 'ldap://127.0.0.1:1389,127.0.0.2:1389'
17+
url: ['ldap://127.0.0.1:1389', 'ldap://127.0.0.2:1389']
1818
});
1919

2020
You can use `ldap://` or `ldaps://`; the latter would connect over SSL (note

lib/client/client.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,18 @@ function Client (options) {
112112
var self = this
113113
this.servers = []
114114
if (options.url) {
115-
var [, protocol, hosts, path] = options.url.match(/^([a-zA-Z]+:\/\/)([^/]*)(.*)/)
115+
var urls = options.url
116116

117-
hosts.split(',').forEach((host) => {
118-
this.servers.push(url.parse(`${protocol}${host}${path}`))
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((host) => {
126+
return url.parse(host)
119127
})
120128
}
121129

lib/client/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77
Client: Client,
88
createClient: function createClient (options) {
99
if (isObject(options) === false) throw TypeError('options (object) required')
10-
if (options.url && typeof options.url !== 'string') throw TypeError('options.url (string) required')
10+
if (options.url && typeof options.url !== 'string' && !Array.isArray(options.url)) throw TypeError('options.url (string|array) required')
1111
if (options.socketPath && typeof options.socketPath !== 'string') throw TypeError('options.socketPath must be a string')
1212
if ((options.url && options.socketPath) || !(options.url || options.socketPath)) throw TypeError('options.url ^ options.socketPath (String) required')
1313
if (!options.log) options.log = logger

test/client.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,8 @@ tap.test('createClient', t => {
341341
})
342342

343343
t.test('url must be a string', async t => {
344-
const match = /options\.url \(string\) required/
344+
const match = /options\.url \(string\|array\) required/
345345
t.throws(() => ldap.createClient({ url: {} }), match)
346-
t.throws(() => ldap.createClient({ url: [] }), match)
347346
t.throws(() => ldap.createClient({ url: 42 }), match)
348347
})
349348

0 commit comments

Comments
 (0)