@splitsoftware/splitio
Version:
64 lines (63 loc) • 2.96 kB
JavaScript
import { LOCALHOST_MODE, STORAGE_MEMORY, STORAGE_REDIS, CONSUMER_MODE, STANDALONE_MODE } from '@splitsoftware/splitio-commons/esm/utils/constants';
export function validateStorage(settings) {
var log = settings.log, mode = settings.mode, _a = settings.storage, _b = _a === void 0 ? { type: 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 STORAGE_REDIS: {
// If passing REDIS storage in localhost or standalone mode, we log an error and fallback to MEMORY storage
if (mode === STANDALONE_MODE || mode === LOCALHOST_MODE) {
log.error('The provided REDIS storage is invalid for this mode. It requires consumer mode. Fallback into default MEMORY storage.');
return {
type: 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 STORAGE_MEMORY:
default: {
// If passing MEMORY storage in consumer mode, throw an error (no way to fallback to REDIS storage)
if (mode === CONSUMER_MODE)
throw new Error('A REDIS storage is required on consumer mode');
// If passing an invalid storage type, log an error
if (type !== STORAGE_MEMORY)
log.error("The provided '" + type + "' storage type is invalid. Fallback into default MEMORY storage.");
return {
type: STORAGE_MEMORY,
prefix: prefix
};
}
}
}