Support for arrays in url parameter

This commit is contained in:
Martin Cizek 2020-09-17 06:28:03 +02:00
parent 7d52f867a0
commit 3ca2c265da
4 changed files with 14 additions and 7 deletions

View File

@ -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

View File

@ -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)
})
}

View File

@ -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

View File

@ -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)
})