flamesshield-sdk
Version:
Flames Shield Smart rate limiting for Firebase Cloud Functions to protect your services from excessive requests
59 lines (58 loc) • 2.54 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigurationRetriever = void 0;
const remote_config_1 = require("firebase-admin/remote-config");
const CACHE_DURATION_MS = 1 * 60 * 1000;
const getConfigurationServerRc = (app) => __awaiter(void 0, void 0, void 0, function* () {
const rc = (0, remote_config_1.getRemoteConfig)(app);
return yield rc.getServerTemplate();
});
const getConfigurationRc = (app) => __awaiter(void 0, void 0, void 0, function* () {
const rc = (0, remote_config_1.getRemoteConfig)(app);
return yield rc.getTemplate();
});
class ConfigurationRetriever {
constructor(app, loader = getConfigurationRc) {
this.app = app;
this.loader = loader;
}
load() {
return __awaiter(this, void 0, void 0, function* () {
const now = Date.now();
if (!this.lastFetchTime || now - this.lastFetchTime > CACHE_DURATION_MS) {
this.configuration = yield this.loader(this.app);
this.lastFetchTime = now;
}
});
}
throwIfNotLoaded() {
if (!this.configuration) {
throw new Error("Configuration not loaded - call load() first");
}
}
getBoolean(key) {
var _a, _b;
this.throwIfNotLoaded();
if (!((_a = this.configuration) === null || _a === void 0 ? void 0 : _a.parameters[key])) {
return false;
}
const defaultValue = (_b = this.configuration.parameters[key]) === null || _b === void 0 ? void 0 : _b.defaultValue;
if (defaultValue === null || defaultValue === undefined) {
return false;
}
if (!('value' in defaultValue)) {
return false;
}
return defaultValue.value === "true";
}
}
exports.ConfigurationRetriever = ConfigurationRetriever;