slouch/scripts/config.js
Geoff Cox da7ca1123e 100% coverage (#4)
* doc(readme): clean up reasons

* doc(motto)

* test(db-and-doc): more coverage

* test(create-or-update-ignore-conflict)

* test(upsert)

* test(ignore-missing)

* test(post-and-ignore-conflict)

* test(get-merge-put)

* refactor(all): rename post and put

* test(get-merge-create-or-update)

* test(get-merge-update-ignore-conflict)

* test(get-merge-upsert)

* test(get-modify-upsert)

* refactor(doc): redundant code

* test(destroy-ignore-conflict)

* test(get-and-destroy)

* test(mark-as-destroyed)

* test(set-destroyed)

* refactor(attachment)

* test(doc): 100% coverage

* test(attachment): create with base 64

* test(attachment): clean up binary code

* test(attachment): get

* test(attachment): destroy

* test(system): is couchdb 1

* test(system): get

* test(system): reset

* test(updates)

* test(updates)

* test(all): unique DB names

* test(system): reactivate tests

* test(user): add role

* test(user): downsert role

* feat(stream-iterator): indefinite

* test(user): 100% coverage

* test(request-class)

* test(request-class): 100% coverage

* test(config)

* test(config): more coverage

* test(config): more coverage

* test(config): 100% coverage

* test(all): 100% coverage

* refactor(beautify)

* test(coverage): enforce 100%

* test(system): fix race condition

* test(user): shortcut for browser

* test(updates): test continuous stream in phantomjs

* test(updates): test continuous stream in phantomjs

* test(continuous): mock for phantomjs

* test(system): abort iterators

* test(system): fake abort
2017-07-18 07:45:32 -07:00

114 lines
3.3 KiB
JavaScript

'use strict';
var request = require('./request'),
Promise = require('sporks/scripts/promise');
var Config = function (slouch) {
this._slouch = slouch;
this._req = request;
};
Config.prototype._couchDB2Request = function (node, path, opts, parseBody) {
opts.uri = this._slouch._url + '/_node/' + node + '/_config/' + path;
return this._req.request(opts, parseBody);
};
// Warning: as per https://github.com/klaemo/docker-couchdb/issues/42#issuecomment-169610897, this
// isn't really the best approach as a more complete solution would implement some rollback
// mechanism when a node fails after several attempts. (Retries are already attempted by the
// request).
Config.prototype._couchDB2Requests = function (path, opts, parseBody, maxNumNodes) {
var self = this,
promises = [],
i = 0;
return self._slouch.membership.get().then(function (members) {
members.cluster_nodes.forEach(function (node) {
if (typeof maxNumNodes === 'undefined' || i++ < maxNumNodes) {
promises.push(self._couchDB2Request(node, path, opts, parseBody));
}
});
// Only return a single promise when there is a single promise so that the return value
// is consistent for a single node.
return promises.length > 1 ? Promise.all(promises) : promises[0];
});
};
Config.prototype._couchDB1Request = function (path, opts, parseBody) {
opts.uri = this._slouch._url + '/_config/' + path;
return this._req.request(opts, parseBody);
};
Config.prototype._request = function (path, opts, parseBody, maxNumNodes) {
var self = this;
return self._slouch.system.isCouchDB1().then(function (isCouchDB1) {
if (isCouchDB1) {
return self._couchDB1Request(path, opts, parseBody);
} else {
return self._couchDB2Requests(path, opts, parseBody, maxNumNodes);
}
});
};
Config.prototype.get = function (path) {
return this._request(path, {
method: 'GET'
}, true);
};
Config.prototype.set = function (path, value) {
return this._request(path, {
method: 'PUT',
body: JSON.stringify(this._toString(value))
});
};
Config.prototype.unset = function (path) {
return this._request(path, {
method: 'DELETE'
});
};
Config.prototype.unsetIgnoreMissing = function (path) {
var self = this;
return self._slouch.doc.ignoreMissing(function () {
return self.unset(path);
});
};
Config.prototype.setCouchHttpdAuthTimeout = function (timeoutSecs) {
// Convert timeout value to a string
return this.set('couch_httpd_auth/timeout', timeoutSecs + '');
};
Config.prototype._toString = function (value) {
if (typeof value === 'boolean') {
return value ? 'true' : 'false';
} else if (typeof value === 'string') {
return value;
} else {
return value + ''; // convert to string
}
};
Config.prototype.setCouchHttpdAuthAllowPersistentCookies = function (allow) {
return this.set('couch_httpd_auth/allow_persistent_cookies', allow);
};
Config.prototype.setLogLevel = function (level) {
return this.set('log/level', level);
};
Config.prototype.setCompactionRule = function (dbName, rule) {
return this.set('compactions/' + dbName, rule);
};
Config.prototype.setCouchDBMaxDBsOpen = function (maxDBsOpen) {
return this.set('couchdb/max_dbs_open', maxDBsOpen);
};
Config.prototype.setHttpdMaxConnections = function (maxConnections) {
return this.set('httpd/max_connections', maxConnections);
};
module.exports = Config;