@coorpacademy/squirrel
Version:
Local mirror mecanism for ETCD
158 lines (127 loc) • 5.31 kB
JavaScript
;
exports.__esModule = true;
exports.createSquirrel = exports.fallbackPath = undefined;
exports.BrandNotFound = BrandNotFound;
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fp = require('lodash/fp');
var _ = _interopRequireWildcard(_fp);
var _debug = require('debug');
var _debug2 = _interopRequireDefault(_debug);
var _2 = require('..');
var _3 = _interopRequireDefault(_2);
var _cluster = require('./cluster');
var cluster = _interopRequireWildcard(_cluster);
var _local = require('./local');
var local = _interopRequireWildcard(_local);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const debug = (0, _debug2.default)('core:squirrel');
function BrandNotFound(message, brand) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
this.brand = brand;
this.status = 404;
}
function squirrelFactory(options = {}) {
const hosts = _.get('etcd.hosts', options);
if (hosts) {
// allow clustered workers to have their own temp file
return _3.default;
}
return local.createSquirrel;
}
function squirrelResolver(_options = {}, squirrel) {
function matchHost(host) {
debug('find by host', host);
return squirrel.getBy('host', host).then(function (node) {
debug('result by host', host, JSON.stringify(node, null, 4));
if (!node) return Promise.reject(new BrandNotFound(`Not found : host => ${host}`, host));
return node;
});
}
function matchWS(ws) {
debug('find by ws', ws);
return squirrel.getBy('ws', ws).then(function (node) {
debug('result by ws', ws, JSON.stringify(node, null, 4));
if (!node) return Promise.reject(new BrandNotFound(`Not found : ws => ${ws}`, ws));
return node;
});
}
function matchAlias(alias) {
debug('find by alias', alias);
return squirrel.getBy('alias', alias).then(function (node) {
debug('result by alias', alias, JSON.stringify(node, null, 4));
if (!node) return Promise.reject(new BrandNotFound(`Not found : alias => ${alias}`, alias));
return node;
});
}
function matchName(name) {
debug('find by name', name);
return squirrel.getBy('payload.name', name).then(function (node) {
debug('result by name', name, JSON.stringify(node, null, 4));
if (!node) return Promise.reject(new BrandNotFound(`Not found : payload.name => ${name}`, name));
return node;
});
}
function matchDbName(dbName) {
debug('find by dbname', dbName);
return squirrel.getBy('payload.mongodb.dbName', dbName).then(function (node) {
debug('result by dbName', dbName, JSON.stringify(node, null, 4));
if (!node) return Promise.reject(new BrandNotFound(`Not found : payload.mongodb.dbName => ${dbName}`, dbName));
return node;
});
}
const ifBrandNotFound = fun => err => {
if (err instanceof BrandNotFound) {
return fun();
}
throw err;
};
const resolver = function (hostname = '') {
return Promise.resolve(matchHost(hostname)).catch(ifBrandNotFound(() => matchWS(hostname))).catch(ifBrandNotFound(() => matchAlias(hostname.split(':').shift().split('.').shift()))).catch(ifBrandNotFound(() => matchName(hostname))).catch(ifBrandNotFound(() => matchDbName(hostname))).catch(ifBrandNotFound(function () {
throw new BrandNotFound('Brand not defined', hostname);
})).then(_.identity);
};
resolver.getBrands = function () {
return Promise.resolve(squirrel.getAll('payload.name'));
};
resolver.setBrand = function (hostname, value) {
return resolver(hostname).catch(ifBrandNotFound(_.constant(null))).then(node => {
const brand = _.get('payload.brand', node) || hostname;
return squirrel.set(brand, value);
});
};
resolver.patchBrand = function (hostname, patch) {
return resolver(hostname).then(node => {
const mergedValue = _.mergeAllWith((patchValue, nodeValue, key) => {
if (_.isArray(patchValue) || key === 'dashboardSections') return patchValue;
}, [node, patch]);
return resolver.setBrand(hostname, mergedValue);
});
};
resolver.delBrand = function (hostname) {
return resolver(hostname).catch(ifBrandNotFound(_.constant(null))).then(node => {
const brand = _.get('id', node) || hostname;
return squirrel.del(brand);
});
};
return resolver;
}
function createResolver(_options = {}) {
const options = _.defaultsDeep({
etcd: {
indexes: ['host', 'ws', 'alias', 'payload.name', 'payload.mongodb.dbName']
}
}, _options);
const squirrel = squirrelFactory(options)(options.etcd);
return squirrelResolver(options, squirrel);
}
function getFallbackPath() {
return _path2.default.join(`tmp/squirrel-${cluster.workerId()}.json`);
}
exports.default = createResolver;
const fallbackPath = exports.fallbackPath = getFallbackPath;
const createSquirrel = exports.createSquirrel = squirrelFactory;
//# sourceMappingURL=index.js.map