@kth/cortina-block
Version:
Node.js module for fetching Cortina blocks and optionally cache using Redis.
128 lines (127 loc) • 5.66 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cortinaMiddleware = cortinaMiddleware;
const log_1 = __importDefault(require("@kth/log"));
const kth_node_redis_1 = require("kth-node-redis");
const redis_utils_1 = require("./redis-utils");
const fetch_blocks_1 = require("./fetch-blocks");
const config_1 = require("./config");
__exportStar(require("./types"), exports);
// Gets HTML blocks from Cortina or Redis.
function cortina(options) {
const { blockApiUrl, language, shouldSkipCookieScripts, blocksConfig, redisConfig, redisKey } = options;
const { memoryCache } = options;
const fullBlocksConfig = { ...config_1.defaultBlocksConfig, ...blocksConfig };
if (shouldSkipCookieScripts) {
fullBlocksConfig.klaroConfig = config_1.devBlocks.klaroConfig;
fullBlocksConfig.matomoAnalytics = config_1.devBlocks.matomoAnalytics;
}
if (!blockApiUrl) {
throw new Error('Block api url must be specified.');
}
if (redisConfig) {
return fetchWithRedis(redisConfig, blockApiUrl, language, fullBlocksConfig, redisKey);
}
return (0, fetch_blocks_1.fetchAllBlocks)(fullBlocksConfig, blockApiUrl, language, memoryCache);
}
const fetchWithRedis = async (redisConfig, blockApiUrl, language, fullBlocksConfig, redisKey) => {
const { defaultKey, redisExpire } = config_1.redisItemSettings;
const finalRedisKey = redisKey || defaultKey;
const redisClient = await (0, kth_node_redis_1.getClient)('cortina', redisConfig);
// Try to get from Redis otherwise get from web service then cache result
// in Redis using redisKey. If Redis connection fails, call API
// directly and don't cache results.
return (0, redis_utils_1.getRedisItem)(redisClient, finalRedisKey, language)
.then(storedBlocks => {
if (storedBlocks) {
return storedBlocks;
}
return (0, fetch_blocks_1.fetchAllBlocks)(fullBlocksConfig, blockApiUrl, language, false).then(cortinaBlocks => (0, redis_utils_1.setRedisItem)(redisClient, finalRedisKey, redisExpire, language, cortinaBlocks));
})
.catch(err => {
log_1.default.error('Redis failed:', err.message, err.code);
return (0, fetch_blocks_1.fetchAllBlocks)(fullBlocksConfig, blockApiUrl, language, false);
});
};
const getLanguage = (res, supportedLanguages) => {
let detectedLanguage = res.locals?.locale?.language ?? 'sv';
const finalSupportedLanguages = supportedLanguages || config_1.defaultSupportedLanguages;
if (!finalSupportedLanguages.includes(detectedLanguage)) {
return finalSupportedLanguages[0];
}
return detectedLanguage;
};
const validateConfig = (config) => {
if (config.memoryCache && config.redisConfig) {
const errorMessage = '@kth/cortina-block config "memoryCache" and "redisConfig" are not valid at the same time';
log_1.default.error(errorMessage);
throw new Error(errorMessage);
}
if (config.redisKey && !config.redisConfig) {
const errorMessage = '@kth/cortina-block config "redisKey" is not allowed without "redisConfig"';
log_1.default.error(errorMessage);
throw new Error(errorMessage);
}
};
const shouldUseMemoryCache = (config) => {
const { memoryCache: memoryCacheConfig = true, redisConfig } = config;
if (!redisConfig)
return !!memoryCacheConfig;
return false;
};
function cortinaMiddleware(config) {
validateConfig(config);
return async (req, res, next) => {
// don't load cortina blocks for static content, or if query parameter 'nocortinablocks' is present
if (/^\/static\/.*/.test(req.url) || req.query.nocortinablocks !== undefined) {
next();
return;
}
const { redisConfig, redisKey, skipCookieScriptsInDev = true, supportedLanguages } = config;
const memoryCache = shouldUseMemoryCache(config);
const language = getLanguage(res, supportedLanguages);
let shouldSkipCookieScripts = false;
if (req.hostname?.includes('localhost') && skipCookieScriptsInDev) {
shouldSkipCookieScripts = true;
}
const { blockApiUrl, blocksConfig } = config;
return cortina({
blockApiUrl,
language,
shouldSkipCookieScripts,
blocksConfig,
redisConfig,
redisKey,
memoryCache,
})
.then(blocks => {
// @ts-ignore
res.locals.blocks = blocks;
log_1.default.debug('Cortina blocks loaded.');
next();
})
.catch(err => {
log_1.default.error('Cortina failed to load blocks: ' + err.message);
// @ts-ignore
res.locals.blocks = {};
next();
});
};
}