UNPKG

nestjs-cluster-throttle

Version:

Enterprise-grade rate limiting module for NestJS with Redis support, multiple strategies, and cluster mode

97 lines 3.67 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InternalGeoProvider = void 0; const common_1 = require("@nestjs/common"); let InternalGeoProvider = class InternalGeoProvider { constructor() { this.ipRanges = new Map(); this.initializeBasicRanges(); } async lookup(ip) { try { const ipNum = this.ipToNumber(ip); for (const [countryCode, ranges] of this.ipRanges.entries()) { for (const range of ranges) { if (ipNum >= range.start && ipNum <= range.end) { return { countryCode, country: range.country, }; } } } return null; } catch (error) { console.error('Error looking up IP:', error); return null; } } ipToNumber(ip) { const parts = ip.split('.'); if (parts.length !== 4) { throw new Error('Invalid IP address'); } return parts.reduce((acc, part, index) => { return acc + (parseInt(part, 10) << (8 * (3 - index))); }, 0); } initializeBasicRanges() { this.ipRanges.set('US', [ { start: this.ipToNumber('3.0.0.0'), end: this.ipToNumber('3.255.255.255'), country: 'United States', }, { start: this.ipToNumber('4.0.0.0'), end: this.ipToNumber('4.255.255.255'), country: 'United States', }, ]); this.ipRanges.set('GB', [ { start: this.ipToNumber('2.16.0.0'), end: this.ipToNumber('2.16.255.255'), country: 'United Kingdom', }, ]); this.ipRanges.set('XX', [ { start: this.ipToNumber('127.0.0.0'), end: this.ipToNumber('127.255.255.255'), country: 'Localhost', }, { start: this.ipToNumber('10.0.0.0'), end: this.ipToNumber('10.255.255.255'), country: 'Private', }, { start: this.ipToNumber('172.16.0.0'), end: this.ipToNumber('172.31.255.255'), country: 'Private', }, { start: this.ipToNumber('192.168.0.0'), end: this.ipToNumber('192.168.255.255'), country: 'Private', }, ]); } }; exports.InternalGeoProvider = InternalGeoProvider; exports.InternalGeoProvider = InternalGeoProvider = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", []) ], InternalGeoProvider); //# sourceMappingURL=internal-geo.provider.js.map