From 5e96e515b261234993e67cb1610e6b4779c81beb Mon Sep 17 00:00:00 2001 From: Romain Lalaut Date: Fri, 26 May 2017 14:48:39 +0200 Subject: [PATCH] Allow to connect to an endpoint needing auth --- README.md | 10 ++++++++++ index.js | 18 ++++++++++-------- package.json | 1 + 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3e44bed..ccd2dc9 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,16 @@ const no4 = new arangochair('http://127.0.0.1:8529/'); // ArangoDB node to monit const no4 = new arangochair('http://127.0.0.1:8529/myDb'); // ArangoDB node to monitor, with database name +const no4 = new arangochair('http://user:password@127.0.0.1:8529/myDb'); // ArangoDB node to monitor, with database name, with auth + +const no4 = new arangochair({ + protocol: 'http', + auth: 'user:password', + hostname: 'localhost', + port: 8529, + path: '/myDb' +}); // ArangoDB node to monitor, with database name, with auth (`url.parse` and `url.format` properties) + no4.subscribe({collection:'users'}); no4.start(); no4.on('users', (doc, type) => { diff --git a/index.js b/index.js index 5067049..542c418 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ const EventEmitter = require('events'); const https = require('request-easy').https; const http = require('request-easy').http; const url = require('url'); - +const isString = require('lodash.isstring'); const mapTextToType = { 'insert/update':'2300', @@ -16,16 +16,18 @@ const mapTypeToText = { '2302': 'delete' }; - - class ArangoChair extends EventEmitter { - constructor(adbUrl) { + constructor(opts) { super(); + let adbUrl + if(isString(opts)){ + adbUrl = opts; + } else { + adbUrl = url.format(opts); + } + adbUrl = url.parse(adbUrl); - this.req = new (adbUrl.protocol === 'https:'? https : http)({ - hostname:adbUrl.hostname, - port:adbUrl.port - }); + this.req = new (adbUrl.protocol === 'https:'? https : http)(adbUrl); const db = '/' === adbUrl.pathname ? '/_system' : adbUrl.pathname; diff --git a/package.json b/package.json index c293f85..a90efc8 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "dependencies": { + "lodash.isstring": "^4.0.1", "request-easy": "^2.0.1" }, "name": "arangochair",