@splitsoftware/splitio
Version:
68 lines (67 loc) • 3.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateStorage = void 0;
var constants_1 = require("@splitsoftware/splitio-commons/cjs/utils/constants");
function validateStorage(settings) {
var log = settings.log, mode = settings.mode, _a = settings.storage, _b = _a === void 0 ? { type: constants_1.STORAGE_MEMORY } : _a, type = _b.type, _c = _b.options, options = _c === void 0 ? {} : _c, prefix = _b.prefix;
// We can have MEMORY, REDIS or an invalid storage type
switch (type) {
case constants_1.STORAGE_REDIS: {
// If passing REDIS storage in localhost or standalone mode, we log an error and fallback to MEMORY storage
if (mode === constants_1.STANDALONE_MODE || mode === constants_1.LOCALHOST_MODE) {
log.error('The provided REDIS storage is invalid for this mode. It requires consumer mode. Fallback into default MEMORY storage.');
return {
type: constants_1.STORAGE_MEMORY,
prefix: prefix
};
}
var host = options.host, port = options.port, db = options.db, pass = options.pass, url = options.url, tls = options.tls, connectionTimeout = options.connectionTimeout, operationTimeout = options.operationTimeout;
if (process.env.REDIS_HOST)
host = process.env.REDIS_HOST;
if (process.env.REDIS_PORT)
port = process.env.REDIS_PORT;
if (process.env.REDIS_DB)
db = process.env.REDIS_DB;
if (process.env.REDIS_PASS)
pass = process.env.REDIS_PASS;
if (process.env.REDIS_URL)
url = process.env.REDIS_URL;
var newOpts = {
connectionTimeout: connectionTimeout,
operationTimeout: operationTimeout
};
if (url) {
newOpts.url = url;
}
else {
newOpts.host = host;
newOpts.port = port;
newOpts.db = db;
newOpts.pass = pass;
}
if (tls) {
newOpts.tls = tls;
}
return {
type: type,
prefix: prefix,
options: newOpts
};
}
// For now, we don't have modifiers or settings for MEMORY in Node.js
case constants_1.STORAGE_MEMORY:
default: {
// If passing MEMORY storage in consumer mode, throw an error (no way to fallback to REDIS storage)
if (mode === constants_1.CONSUMER_MODE)
throw new Error('A REDIS storage is required on consumer mode');
// If passing an invalid storage type, log an error
if (type !== constants_1.STORAGE_MEMORY)
log.error("The provided '" + type + "' storage type is invalid. Fallback into default MEMORY storage.");
return {
type: constants_1.STORAGE_MEMORY,
prefix: prefix
};
}
}
}
exports.validateStorage = validateStorage;