diff --git a/docs/client.md b/docs/client.md index f953e59..c7e2508 100644 --- a/docs/client.md +++ b/docs/client.md @@ -14,7 +14,7 @@ The code to create a new client looks like: var ldap = require('ldapjs'); var client = ldap.createClient({ - url: 'ldap://127.0.0.1:1389,127.0.0.2:1389' + url: ['ldap://127.0.0.1:1389', 'ldap://127.0.0.2:1389'] }); You can use `ldap://` or `ldaps://`; the latter would connect over SSL (note diff --git a/lib/client/client.js b/lib/client/client.js index 45a9bee..addc89a 100644 --- a/lib/client/client.js +++ b/lib/client/client.js @@ -112,10 +112,18 @@ function Client (options) { var self = this this.servers = [] if (options.url) { - var [, protocol, hosts, path] = options.url.match(/^([a-zA-Z]+:\/\/)([^/]*)(.*)/) + var urls = options.url - hosts.split(',').forEach((host) => { - this.servers.push(url.parse(`${protocol}${host}${path}`)) + if (typeof options.url === 'string') { + var [, protocol, hosts, path] = options.url.match(/^([a-zA-Z]+:\/\/)([^/]*)(.*)/) + + urls = hosts.split(',').map((host) => { + return `${protocol}${host}${path}` + }) + } + + this.servers = urls.map((host) => { + return url.parse(host) }) } diff --git a/lib/client/index.js b/lib/client/index.js index 0c23052..fb9db3a 100644 --- a/lib/client/index.js +++ b/lib/client/index.js @@ -7,7 +7,7 @@ module.exports = { Client: Client, createClient: function createClient (options) { if (isObject(options) === false) throw TypeError('options (object) required') - if (options.url && typeof options.url !== 'string') throw TypeError('options.url (string) required') + if (options.url && typeof options.url !== 'string' && !Array.isArray(options.url)) throw TypeError('options.url (string|array) required') if (options.socketPath && typeof options.socketPath !== 'string') throw TypeError('options.socketPath must be a string') if ((options.url && options.socketPath) || !(options.url || options.socketPath)) throw TypeError('options.url ^ options.socketPath (String) required') if (!options.log) options.log = logger diff --git a/test/client.test.js b/test/client.test.js index bd9b9a2..51cf6af 100644 --- a/test/client.test.js +++ b/test/client.test.js @@ -341,9 +341,8 @@ tap.test('createClient', t => { }) t.test('url must be a string', async t => { - const match = /options\.url \(string\) required/ + const match = /options\.url \(string\|array\) required/ t.throws(() => ldap.createClient({ url: {} }), match) - t.throws(() => ldap.createClient({ url: [] }), match) t.throws(() => ldap.createClient({ url: 42 }), match) })