@seckav/security-sdk
Version:
SecKav Security SDK - Enterprise-grade security platform with AI-powered threat detection, LLM-powered misconfiguration scanning (Gemini/GPT-4/Claude), end-to-end encryption, behavioral analysis, enhanced file scanning, adaptive rate limiting, GDPR/DPDP/C
86 lines • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RateLimitModule = void 0;
const rateLimitMiddleware_1 = require("../rateLimitMiddleware");
/**
* Rate Limiting Module for SecKav SDK
* Wraps the existing rate limiting functionality in the new modular architecture
*/
class RateLimitModule {
constructor(config) {
this.config = config;
// Initialize the existing rate limit SDK
this.rateLimitSDK = new rateLimitMiddleware_1.RateLimitSDK({
apiUrl: config.apiUrl,
organizationId: config.organizationId,
apiKey: config.apiKey,
timeout: config.timeout,
onError: config.onError,
});
}
/**
* Check rate limit for Express.js request
*/
async checkRequest(req) {
const endpoint = req.path || req.url;
const method = req.method;
const clientId = req.headers['x-client-id'] || req.user?.id;
const userAgent = req.headers['user-agent'];
const ipAddress = req.ip || req.connection.remoteAddress;
return await this.rateLimitSDK.checkRateLimit(endpoint, method, clientId, userAgent, ipAddress);
}
/**
* Check rate limit for Next.js request
*/
async checkNextRequest(req) {
const endpoint = req.nextUrl?.pathname || req.url;
const method = req.method;
const clientId = req.headers.get('x-client-id');
const userAgent = req.headers.get('user-agent') || undefined;
const ipAddress = req.ip || req.headers.get('x-forwarded-for') || req.headers.get('x-real-ip');
return await this.rateLimitSDK.checkRateLimit(endpoint, method, clientId, userAgent, ipAddress);
}
/**
* Handle rate limit response for Express.js
*/
handleRateLimitResponse(res, result) {
// Set headers
Object.entries(result.headers || {}).forEach(([key, value]) => {
res.setHeader(key, value);
});
// Handle different error types
if (result.error === 'ENDPOINT_NOT_REGISTERED') {
return res.status(403).json({
error: 'Endpoint not registered',
message: result.message,
action: result.action,
dashboardUrl: result.action?.dashboardUrl
});
}
// Handle rate limiting
return res.status(429).json({
error: 'Too many requests',
retryAfter: result.retryAfter,
policy: result.policy,
});
}
/**
* Get the underlying rate limit SDK for advanced usage
*/
getSDK() {
return this.rateLimitSDK;
}
/**
* Get module information
*/
getInfo() {
return {
name: 'RateLimit',
version: '1.0.0',
organization: this.config.organizationId,
apiUrl: this.config.apiUrl,
};
}
}
exports.RateLimitModule = RateLimitModule;
//# sourceMappingURL=RateLimit.js.map