UNPKG

@aikidosec/firewall

Version:

Zen by Aikido is an embedded Web Application Firewall that autonomously protects Node.js apps against common and critical attacks

37 lines (36 loc) 1.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.pollForChanges = pollForChanges; const getConfig_1 = require("./getConfig"); const getConfigLastUpdatedAt_1 = require("./getConfigLastUpdatedAt"); let interval = null; let currentLastUpdatedAt = null; function pollForChanges({ onConfigUpdate, serverless, token, logger, lastUpdatedAt, }) { if (!token) { logger.log("No token provided, not polling for config updates"); return; } if (serverless) { logger.log("Running in serverless environment, not polling for config updates"); return; } currentLastUpdatedAt = lastUpdatedAt; if (interval) { clearInterval(interval); } interval = setInterval(() => { check(token, onConfigUpdate).catch((error) => { logger.log(`Failed to check for config updates: ${error.message}`); }); }, 60 * 1000); interval.unref(); } async function check(token, onConfigUpdate) { const configLastUpdatedAt = await (0, getConfigLastUpdatedAt_1.getConfigLastUpdatedAt)(token); if (typeof currentLastUpdatedAt === "number" && configLastUpdatedAt > currentLastUpdatedAt) { const config = await (0, getConfig_1.getConfig)(token); currentLastUpdatedAt = config.configUpdatedAt; onConfigUpdate(config); } }