flamesshield-sdk
Version:
Flames Shield Smart rate limiting for Firebase Cloud Functions to protect your services from excessive requests
76 lines (75 loc) • 3.38 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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RateLimitingEnabled = void 0;
const crypto = __importStar(require("crypto"));
/**
* Hashes a function name to make it compatible with Firebase Remote Config parameter keys
* by creating a SHA-256 hash and returning a shortened hex representation
* @param functionName The function name to hash
* @returns Hashed function name safe for Remote Config parameter keys
*/
function sanitizeFunctionNameForRemoteConfig(functionName) {
// Create a SHA-256 hash of the function name
const hash = crypto.createHash('sha256').update(functionName).digest('hex');
// Return first 12 characters of the hash to keep it reasonably sized
return hash.substring(0, 12);
}
class RateLimitingEnabled {
constructor(functionName, configurationApi, options) {
this.verbose = (options === null || options === void 0 ? void 0 : options.verbose) || false;
this.functionName = functionName;
if (this.verbose) {
console.log(`[FlameShield] Checking rate limiting configuration for function: ${functionName}`);
}
const config = _a.ConfigurationForApi(configurationApi, functionName);
this.enabled = config.enabled || false;
if (this.verbose) {
console.log(`[FlameShield] Rate limiting for function ${functionName} is ${this.enabled ? 'enabled' : 'disabled'}`);
console.log("[FlameShield] Function name sanitized for Remote Config:", sanitizeFunctionNameForRemoteConfig(functionName));
}
}
static generateEnvName(functionName) {
return `FLAMESHIELD_ENABLED_${sanitizeFunctionNameForRemoteConfig(functionName).toUpperCase()}`;
}
}
exports.RateLimitingEnabled = RateLimitingEnabled;
_a = RateLimitingEnabled;
RateLimitingEnabled.ConfigurationForApi = (api, functionName) => {
const envName = _a.generateEnvName(functionName);
const enabled = api.getBoolean(`${envName}_ENABLED`);
return { enabled };
};