@skillsmith/core 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.tsbuildinfo +1 -1
- package/dist/src/analysis/CodebaseAnalyzer.d.ts +4 -134
- package/dist/src/analysis/CodebaseAnalyzer.d.ts.map +1 -1
- package/dist/src/analysis/CodebaseAnalyzer.js +7 -315
- package/dist/src/analysis/CodebaseAnalyzer.js.map +1 -1
- package/dist/src/analysis/framework-detector.d.ts +47 -0
- package/dist/src/analysis/framework-detector.d.ts.map +1 -0
- package/dist/src/analysis/framework-detector.js +151 -0
- package/dist/src/analysis/framework-detector.js.map +1 -0
- package/dist/src/analysis/index.d.ts +6 -1
- package/dist/src/analysis/index.d.ts.map +1 -1
- package/dist/src/analysis/index.js +10 -1
- package/dist/src/analysis/index.js.map +1 -1
- package/dist/src/analysis/parsers.d.ts +43 -0
- package/dist/src/analysis/parsers.d.ts.map +1 -0
- package/dist/src/analysis/parsers.js +219 -0
- package/dist/src/analysis/parsers.js.map +1 -0
- package/dist/src/analysis/types.d.ts +138 -0
- package/dist/src/analysis/types.d.ts.map +1 -0
- package/dist/src/analysis/types.js +25 -0
- package/dist/src/analysis/types.js.map +1 -0
- package/dist/src/analytics/AnalyticsRepository.d.ts.map +1 -1
- package/dist/src/analytics/AnalyticsRepository.js +4 -4
- package/dist/src/analytics/AnalyticsRepository.js.map +1 -1
- package/dist/src/api/cache.d.ts +108 -0
- package/dist/src/api/cache.d.ts.map +1 -0
- package/dist/src/api/cache.js +216 -0
- package/dist/src/api/cache.js.map +1 -0
- package/dist/src/api/client.d.ts +152 -0
- package/dist/src/api/client.d.ts.map +1 -0
- package/dist/src/api/client.js +221 -0
- package/dist/src/api/client.js.map +1 -0
- package/dist/src/api/index.d.ts +10 -0
- package/dist/src/api/index.d.ts.map +1 -0
- package/dist/src/api/index.js +10 -0
- package/dist/src/api/index.js.map +1 -0
- package/dist/src/benchmarks/BenchmarkRunner.d.ts +6 -175
- package/dist/src/benchmarks/BenchmarkRunner.d.ts.map +1 -1
- package/dist/src/benchmarks/BenchmarkRunner.js +6 -123
- package/dist/src/benchmarks/BenchmarkRunner.js.map +1 -1
- package/dist/src/benchmarks/MemoryProfiler.d.ts +4 -262
- package/dist/src/benchmarks/MemoryProfiler.d.ts.map +1 -1
- package/dist/src/benchmarks/MemoryProfiler.js +4 -400
- package/dist/src/benchmarks/MemoryProfiler.js.map +1 -1
- package/dist/src/benchmarks/comparator.d.ts +38 -0
- package/dist/src/benchmarks/comparator.d.ts.map +1 -0
- package/dist/src/benchmarks/comparator.js +81 -0
- package/dist/src/benchmarks/comparator.js.map +1 -0
- package/dist/src/benchmarks/formatters.d.ts +30 -0
- package/dist/src/benchmarks/formatters.d.ts.map +1 -0
- package/dist/src/benchmarks/formatters.js +88 -0
- package/dist/src/benchmarks/formatters.js.map +1 -0
- package/dist/src/benchmarks/index.d.ts +5 -1
- package/dist/src/benchmarks/index.d.ts.map +1 -1
- package/dist/src/benchmarks/index.js +9 -2
- package/dist/src/benchmarks/index.js.map +1 -1
- package/dist/src/benchmarks/memory/MemoryProfiler.d.ts +165 -0
- package/dist/src/benchmarks/memory/MemoryProfiler.d.ts.map +1 -0
- package/dist/src/benchmarks/memory/MemoryProfiler.js +312 -0
- package/dist/src/benchmarks/memory/MemoryProfiler.js.map +1 -0
- package/dist/src/benchmarks/memory/baseline-manager.d.ts +64 -0
- package/dist/src/benchmarks/memory/baseline-manager.d.ts.map +1 -0
- package/dist/src/benchmarks/memory/baseline-manager.js +95 -0
- package/dist/src/benchmarks/memory/baseline-manager.js.map +1 -0
- package/dist/src/benchmarks/memory/index.d.ts +12 -0
- package/dist/src/benchmarks/memory/index.d.ts.map +1 -0
- package/dist/src/benchmarks/memory/index.js +12 -0
- package/dist/src/benchmarks/memory/index.js.map +1 -0
- package/dist/src/benchmarks/memory/leak-detector.d.ts +23 -0
- package/dist/src/benchmarks/memory/leak-detector.d.ts.map +1 -0
- package/dist/src/benchmarks/memory/leak-detector.js +46 -0
- package/dist/src/benchmarks/memory/leak-detector.js.map +1 -0
- package/dist/src/benchmarks/memory/regression-detector.d.ts +17 -0
- package/dist/src/benchmarks/memory/regression-detector.d.ts.map +1 -0
- package/dist/src/benchmarks/memory/regression-detector.js +56 -0
- package/dist/src/benchmarks/memory/regression-detector.js.map +1 -0
- package/dist/src/benchmarks/memory/types.d.ts +111 -0
- package/dist/src/benchmarks/memory/types.d.ts.map +1 -0
- package/dist/src/benchmarks/memory/types.js +7 -0
- package/dist/src/benchmarks/memory/types.js.map +1 -0
- package/dist/src/benchmarks/memory/utils.d.ts +17 -0
- package/dist/src/benchmarks/memory/utils.d.ts.map +1 -0
- package/dist/src/benchmarks/memory/utils.js +29 -0
- package/dist/src/benchmarks/memory/utils.js.map +1 -0
- package/dist/src/benchmarks/types.d.ts +175 -0
- package/dist/src/benchmarks/types.d.ts.map +1 -0
- package/dist/src/benchmarks/types.js +20 -0
- package/dist/src/benchmarks/types.js.map +1 -0
- package/dist/src/index.d.ts +4 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +7 -3
- package/dist/src/index.js.map +1 -1
- package/dist/src/repositories/QuarantineRepository.d.ts +4 -251
- package/dist/src/repositories/QuarantineRepository.d.ts.map +1 -1
- package/dist/src/repositories/QuarantineRepository.js +4 -441
- package/dist/src/repositories/QuarantineRepository.js.map +1 -1
- package/dist/src/repositories/quarantine/QuarantineRepository.d.ts +168 -0
- package/dist/src/repositories/quarantine/QuarantineRepository.d.ts.map +1 -0
- package/dist/src/repositories/quarantine/QuarantineRepository.js +341 -0
- package/dist/src/repositories/quarantine/QuarantineRepository.js.map +1 -0
- package/dist/src/repositories/quarantine/index.d.ts +10 -0
- package/dist/src/repositories/quarantine/index.d.ts.map +1 -0
- package/dist/src/repositories/quarantine/index.js +10 -0
- package/dist/src/repositories/quarantine/index.js.map +1 -0
- package/dist/src/repositories/quarantine/queries.d.ts +58 -0
- package/dist/src/repositories/quarantine/queries.d.ts.map +1 -0
- package/dist/src/repositories/quarantine/queries.js +88 -0
- package/dist/src/repositories/quarantine/queries.js.map +1 -0
- package/dist/src/repositories/quarantine/query-builder.d.ts +44 -0
- package/dist/src/repositories/quarantine/query-builder.d.ts.map +1 -0
- package/dist/src/repositories/quarantine/query-builder.js +87 -0
- package/dist/src/repositories/quarantine/query-builder.js.map +1 -0
- package/dist/src/repositories/quarantine/types.d.ts +155 -0
- package/dist/src/repositories/quarantine/types.d.ts.map +1 -0
- package/dist/src/repositories/quarantine/types.js +13 -0
- package/dist/src/repositories/quarantine/types.js.map +1 -0
- package/dist/src/scripts/__tests__/scan-imported-skills.test.d.ts +1 -0
- package/dist/src/scripts/__tests__/scan-imported-skills.test.d.ts.map +1 -1
- package/dist/src/scripts/__tests__/scan-imported-skills.test.js +7 -43
- package/dist/src/scripts/__tests__/scan-imported-skills.test.js.map +1 -1
- package/dist/src/scripts/github-import/checkpoint.d.ts +21 -0
- package/dist/src/scripts/github-import/checkpoint.d.ts.map +1 -0
- package/dist/src/scripts/github-import/checkpoint.js +52 -0
- package/dist/src/scripts/github-import/checkpoint.js.map +1 -0
- package/dist/src/scripts/github-import/deduplication.d.ts +15 -0
- package/dist/src/scripts/github-import/deduplication.d.ts.map +1 -0
- package/dist/src/scripts/github-import/deduplication.js +33 -0
- package/dist/src/scripts/github-import/deduplication.js.map +1 -0
- package/dist/src/scripts/github-import/github-client.d.ts +29 -0
- package/dist/src/scripts/github-import/github-client.d.ts.map +1 -0
- package/dist/src/scripts/github-import/github-client.js +184 -0
- package/dist/src/scripts/github-import/github-client.js.map +1 -0
- package/dist/src/scripts/github-import/index.d.ts +29 -0
- package/dist/src/scripts/github-import/index.d.ts.map +1 -0
- package/dist/src/scripts/github-import/index.js +198 -0
- package/dist/src/scripts/github-import/index.js.map +1 -0
- package/dist/src/scripts/github-import/output.d.ts +12 -0
- package/dist/src/scripts/github-import/output.d.ts.map +1 -0
- package/dist/src/scripts/github-import/output.js +34 -0
- package/dist/src/scripts/github-import/output.js.map +1 -0
- package/dist/src/scripts/github-import/types.d.ts +108 -0
- package/dist/src/scripts/github-import/types.d.ts.map +1 -0
- package/dist/src/scripts/github-import/types.js +44 -0
- package/dist/src/scripts/github-import/types.js.map +1 -0
- package/dist/src/scripts/github-import/utils.d.ts +13 -0
- package/dist/src/scripts/github-import/utils.d.ts.map +1 -0
- package/dist/src/scripts/github-import/utils.js +28 -0
- package/dist/src/scripts/github-import/utils.js.map +1 -0
- package/dist/src/scripts/import-github-skills.d.ts +3 -12
- package/dist/src/scripts/import-github-skills.d.ts.map +1 -1
- package/dist/src/scripts/import-github-skills.js +18 -398
- package/dist/src/scripts/import-github-skills.js.map +1 -1
- package/dist/src/scripts/scan-imported-skills.d.ts +7 -7
- package/dist/src/scripts/scan-imported-skills.d.ts.map +1 -1
- package/dist/src/scripts/scan-imported-skills.js +8 -397
- package/dist/src/scripts/scan-imported-skills.js.map +1 -1
- package/dist/src/scripts/skill-scanner/categorizer.d.ts +43 -0
- package/dist/src/scripts/skill-scanner/categorizer.d.ts.map +1 -0
- package/dist/src/scripts/skill-scanner/categorizer.js +61 -0
- package/dist/src/scripts/skill-scanner/categorizer.js.map +1 -0
- package/dist/src/scripts/skill-scanner/file-scanner.d.ts +43 -0
- package/dist/src/scripts/skill-scanner/file-scanner.d.ts.map +1 -0
- package/dist/src/scripts/skill-scanner/file-scanner.js +90 -0
- package/dist/src/scripts/skill-scanner/file-scanner.js.map +1 -0
- package/dist/src/scripts/skill-scanner/index.d.ts +22 -0
- package/dist/src/scripts/skill-scanner/index.d.ts.map +1 -0
- package/dist/src/scripts/skill-scanner/index.js +43 -0
- package/dist/src/scripts/skill-scanner/index.js.map +1 -0
- package/dist/src/scripts/skill-scanner/logger.d.ts +59 -0
- package/dist/src/scripts/skill-scanner/logger.d.ts.map +1 -0
- package/dist/src/scripts/skill-scanner/logger.js +104 -0
- package/dist/src/scripts/skill-scanner/logger.js.map +1 -0
- package/dist/src/scripts/skill-scanner/reporter.d.ts +56 -0
- package/dist/src/scripts/skill-scanner/reporter.d.ts.map +1 -0
- package/dist/src/scripts/skill-scanner/reporter.js +166 -0
- package/dist/src/scripts/skill-scanner/reporter.js.map +1 -0
- package/dist/src/scripts/skill-scanner/scanner.d.ts +44 -0
- package/dist/src/scripts/skill-scanner/scanner.d.ts.map +1 -0
- package/dist/src/scripts/skill-scanner/scanner.js +140 -0
- package/dist/src/scripts/skill-scanner/scanner.js.map +1 -0
- package/dist/src/scripts/skill-scanner/trust-scorer.d.ts +59 -0
- package/dist/src/scripts/skill-scanner/trust-scorer.d.ts.map +1 -0
- package/dist/src/scripts/skill-scanner/trust-scorer.js +62 -0
- package/dist/src/scripts/skill-scanner/trust-scorer.js.map +1 -0
- package/dist/src/scripts/skill-scanner/types.d.ts +98 -0
- package/dist/src/scripts/skill-scanner/types.d.ts.map +1 -0
- package/dist/src/scripts/skill-scanner/types.js +7 -0
- package/dist/src/scripts/skill-scanner/types.js.map +1 -0
- package/dist/src/scripts/validate-skills.d.ts +3 -172
- package/dist/src/scripts/validate-skills.d.ts.map +1 -1
- package/dist/src/scripts/validate-skills.js +6 -519
- package/dist/src/scripts/validate-skills.js.map +1 -1
- package/dist/src/scripts/validation/deduplication.d.ts +23 -0
- package/dist/src/scripts/validation/deduplication.d.ts.map +1 -0
- package/dist/src/scripts/validation/deduplication.js +127 -0
- package/dist/src/scripts/validation/deduplication.js.map +1 -0
- package/dist/src/scripts/validation/field-validators.d.ts +9 -0
- package/dist/src/scripts/validation/field-validators.d.ts.map +1 -0
- package/dist/src/scripts/validation/field-validators.js +135 -0
- package/dist/src/scripts/validation/field-validators.js.map +1 -0
- package/dist/src/scripts/validation/index.d.ts +29 -0
- package/dist/src/scripts/validation/index.d.ts.map +1 -0
- package/dist/src/scripts/validation/index.js +84 -0
- package/dist/src/scripts/validation/index.js.map +1 -0
- package/dist/src/scripts/validation/normalizers.d.ts +29 -0
- package/dist/src/scripts/validation/normalizers.d.ts.map +1 -0
- package/dist/src/scripts/validation/normalizers.js +79 -0
- package/dist/src/scripts/validation/normalizers.js.map +1 -0
- package/dist/src/scripts/validation/pipeline.d.ts +13 -0
- package/dist/src/scripts/validation/pipeline.d.ts.map +1 -0
- package/dist/src/scripts/validation/pipeline.js +126 -0
- package/dist/src/scripts/validation/pipeline.js.map +1 -0
- package/dist/src/scripts/validation/types.d.ts +128 -0
- package/dist/src/scripts/validation/types.d.ts.map +1 -0
- package/dist/src/scripts/validation/types.js +43 -0
- package/dist/src/scripts/validation/types.js.map +1 -0
- package/dist/src/security/index.d.ts +5 -4
- package/dist/src/security/index.d.ts.map +1 -1
- package/dist/src/security/index.js +8 -2
- package/dist/src/security/index.js.map +1 -1
- package/dist/src/security/rate-limiter/RateLimiter.d.ts +86 -0
- package/dist/src/security/rate-limiter/RateLimiter.d.ts.map +1 -0
- package/dist/src/security/rate-limiter/RateLimiter.js +260 -0
- package/dist/src/security/rate-limiter/RateLimiter.js.map +1 -0
- package/dist/src/security/rate-limiter/constants.d.ts +15 -0
- package/dist/src/security/rate-limiter/constants.d.ts.map +1 -0
- package/dist/src/security/rate-limiter/constants.js +15 -0
- package/dist/src/security/rate-limiter/constants.js.map +1 -0
- package/dist/src/security/rate-limiter/errors.d.ts +22 -0
- package/dist/src/security/rate-limiter/errors.d.ts.map +1 -0
- package/dist/src/security/rate-limiter/errors.js +32 -0
- package/dist/src/security/rate-limiter/errors.js.map +1 -0
- package/dist/src/security/rate-limiter/index.d.ts +12 -0
- package/dist/src/security/rate-limiter/index.d.ts.map +1 -0
- package/dist/src/security/rate-limiter/index.js +16 -0
- package/dist/src/security/rate-limiter/index.js.map +1 -0
- package/dist/src/security/rate-limiter/metrics-manager.d.ts +55 -0
- package/dist/src/security/rate-limiter/metrics-manager.d.ts.map +1 -0
- package/dist/src/security/rate-limiter/metrics-manager.js +144 -0
- package/dist/src/security/rate-limiter/metrics-manager.js.map +1 -0
- package/dist/src/security/rate-limiter/presets.d.ts +52 -0
- package/dist/src/security/rate-limiter/presets.d.ts.map +1 -0
- package/dist/src/security/rate-limiter/presets.js +53 -0
- package/dist/src/security/rate-limiter/presets.js.map +1 -0
- package/dist/src/security/rate-limiter/queue-manager.d.ts +59 -0
- package/dist/src/security/rate-limiter/queue-manager.d.ts.map +1 -0
- package/dist/src/security/rate-limiter/queue-manager.js +189 -0
- package/dist/src/security/rate-limiter/queue-manager.js.map +1 -0
- package/dist/src/security/rate-limiter/storage.d.ts +34 -0
- package/dist/src/security/rate-limiter/storage.d.ts.map +1 -0
- package/dist/src/security/rate-limiter/storage.js +80 -0
- package/dist/src/security/rate-limiter/storage.js.map +1 -0
- package/dist/src/security/rate-limiter/token-bucket.d.ts +44 -0
- package/dist/src/security/rate-limiter/token-bucket.d.ts.map +1 -0
- package/dist/src/security/rate-limiter/token-bucket.js +99 -0
- package/dist/src/security/rate-limiter/token-bucket.js.map +1 -0
- package/dist/src/security/rate-limiter/types.d.ts +104 -0
- package/dist/src/security/rate-limiter/types.d.ts.map +1 -0
- package/dist/src/security/rate-limiter/types.js +7 -0
- package/dist/src/security/rate-limiter/types.js.map +1 -0
- package/dist/src/security/scanner/SecurityScanner.d.ts +94 -0
- package/dist/src/security/scanner/SecurityScanner.d.ts.map +1 -0
- package/dist/src/security/scanner/SecurityScanner.js +403 -0
- package/dist/src/security/scanner/SecurityScanner.js.map +1 -0
- package/dist/src/security/scanner/index.d.ts +11 -0
- package/dist/src/security/scanner/index.d.ts.map +1 -0
- package/dist/src/security/scanner/index.js +14 -0
- package/dist/src/security/scanner/index.js.map +1 -0
- package/dist/src/security/scanner/patterns.d.ts +14 -0
- package/dist/src/security/scanner/patterns.d.ts.map +1 -0
- package/dist/src/security/scanner/patterns.js +147 -0
- package/dist/src/security/scanner/patterns.js.map +1 -0
- package/dist/src/security/scanner/regex-utils.d.ts +33 -0
- package/dist/src/security/scanner/regex-utils.d.ts.map +1 -0
- package/dist/src/security/scanner/regex-utils.js +41 -0
- package/dist/src/security/scanner/regex-utils.js.map +1 -0
- package/dist/src/security/scanner/types.d.ts +63 -0
- package/dist/src/security/scanner/types.d.ts.map +1 -0
- package/dist/src/security/scanner/types.js +7 -0
- package/dist/src/security/scanner/types.js.map +1 -0
- package/dist/src/security/scanner/weights.d.ts +15 -0
- package/dist/src/security/scanner/weights.d.ts.map +1 -0
- package/dist/src/security/scanner/weights.js +28 -0
- package/dist/src/security/scanner/weights.js.map +1 -0
- package/dist/src/session/SessionHealthMonitor.d.ts +5 -168
- package/dist/src/session/SessionHealthMonitor.d.ts.map +1 -1
- package/dist/src/session/SessionHealthMonitor.js +22 -91
- package/dist/src/session/SessionHealthMonitor.js.map +1 -1
- package/dist/src/session/health-checks.d.ts +37 -0
- package/dist/src/session/health-checks.d.ts.map +1 -0
- package/dist/src/session/health-checks.js +64 -0
- package/dist/src/session/health-checks.js.map +1 -0
- package/dist/src/session/health-types.d.ts +78 -0
- package/dist/src/session/health-types.d.ts.map +1 -0
- package/dist/src/session/health-types.js +16 -0
- package/dist/src/session/health-types.js.map +1 -0
- package/dist/src/session/index.d.ts +5 -1
- package/dist/src/session/index.d.ts.map +1 -1
- package/dist/src/session/index.js +9 -0
- package/dist/src/session/index.js.map +1 -1
- package/dist/src/session/metrics-collector.d.ts +23 -0
- package/dist/src/session/metrics-collector.d.ts.map +1 -0
- package/dist/src/session/metrics-collector.js +44 -0
- package/dist/src/session/metrics-collector.js.map +1 -0
- package/dist/src/session/typed-event-emitter.d.ts +70 -0
- package/dist/src/session/typed-event-emitter.d.ts.map +1 -0
- package/dist/src/session/typed-event-emitter.js +79 -0
- package/dist/src/session/typed-event-emitter.js.map +1 -0
- package/dist/src/sources/BaseSourceAdapter.d.ts +1 -1
- package/dist/src/sources/BaseSourceAdapter.d.ts.map +1 -1
- package/dist/src/sources/BaseSourceAdapter.js +2 -2
- package/dist/src/sources/BaseSourceAdapter.js.map +1 -1
- package/dist/src/telemetry/index.d.ts +1 -0
- package/dist/src/telemetry/index.d.ts.map +1 -1
- package/dist/src/telemetry/index.js +2 -0
- package/dist/src/telemetry/index.js.map +1 -1
- package/dist/src/telemetry/posthog.d.ts +125 -0
- package/dist/src/telemetry/posthog.d.ts.map +1 -0
- package/dist/src/telemetry/posthog.js +198 -0
- package/dist/src/telemetry/posthog.js.map +1 -0
- package/dist/tests/RateLimiter.test.js +4 -4
- package/dist/tests/RateLimiter.test.js.map +1 -1
- package/dist/tests/e2e/security/security.e2e.test.js +3 -3
- package/dist/tests/e2e/security/security.e2e.test.js.map +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limiter - SMI-730, SMI-1013, SMI-1189
|
|
3
|
+
*
|
|
4
|
+
* Token bucket algorithm for rate limiting API endpoints and adapters.
|
|
5
|
+
* Prevents abuse and DoS attacks with configurable limits and windows.
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Token bucket algorithm for smooth rate limiting
|
|
9
|
+
* - Per-IP and per-user limits
|
|
10
|
+
* - Configurable limits and windows
|
|
11
|
+
* - In-memory storage (Redis-compatible interface)
|
|
12
|
+
* - Graceful degradation on errors
|
|
13
|
+
* - Request queue for waiting when rate limited (SMI-1013)
|
|
14
|
+
* - Configurable timeout for queued requests (SMI-1013)
|
|
15
|
+
*/
|
|
16
|
+
import { createLogger } from '../../utils/logger.js';
|
|
17
|
+
import { InMemoryRateLimitStorage } from './storage.js';
|
|
18
|
+
import { tryConsumeToken as tryConsumeTokenCore, getTokenBucketState, resetTokenBucket, } from './token-bucket.js';
|
|
19
|
+
import { MetricsManager } from './metrics-manager.js';
|
|
20
|
+
import { QueueManager } from './queue-manager.js';
|
|
21
|
+
const log = createLogger('RateLimiter');
|
|
22
|
+
/**
|
|
23
|
+
* Rate Limiter using Token Bucket Algorithm
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const limiter = new RateLimiter({
|
|
28
|
+
* maxTokens: 100,
|
|
29
|
+
* refillRate: 100 / 60,
|
|
30
|
+
* windowMs: 60000,
|
|
31
|
+
* })
|
|
32
|
+
*
|
|
33
|
+
* const result = await limiter.checkLimit('user:123')
|
|
34
|
+
* if (result.allowed) {
|
|
35
|
+
* // Process request
|
|
36
|
+
* } else {
|
|
37
|
+
* // Return 429 Too Many Requests
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export class RateLimiter {
|
|
42
|
+
config;
|
|
43
|
+
storage;
|
|
44
|
+
metricsManager;
|
|
45
|
+
queueManager;
|
|
46
|
+
constructor(config, storage = new InMemoryRateLimitStorage()) {
|
|
47
|
+
this.config = {
|
|
48
|
+
keyPrefix: 'ratelimit',
|
|
49
|
+
debug: false,
|
|
50
|
+
failMode: 'open',
|
|
51
|
+
enableQueue: false,
|
|
52
|
+
queueTimeoutMs: 30000,
|
|
53
|
+
maxQueueSize: 100,
|
|
54
|
+
...config,
|
|
55
|
+
};
|
|
56
|
+
this.storage = storage;
|
|
57
|
+
this.metricsManager = new MetricsManager(this.config.debug, log);
|
|
58
|
+
this.queueManager = new QueueManager({
|
|
59
|
+
maxQueueSize: this.config.maxQueueSize,
|
|
60
|
+
queueTimeoutMs: this.config.queueTimeoutMs,
|
|
61
|
+
debug: this.config.debug,
|
|
62
|
+
}, log);
|
|
63
|
+
// Start queue processor if queuing is enabled (SMI-1013)
|
|
64
|
+
if (this.config.enableQueue) {
|
|
65
|
+
this.queueManager.startProcessor((key, cost) => this.tryConsumeToken(key, cost), (key, allowed) => this.metricsManager.update(key, allowed));
|
|
66
|
+
}
|
|
67
|
+
// Start metrics cleanup interval
|
|
68
|
+
this.metricsManager.startCleanup();
|
|
69
|
+
if (this.config.debug) {
|
|
70
|
+
log.info('Rate limiter initialized', {
|
|
71
|
+
maxTokens: this.config.maxTokens,
|
|
72
|
+
refillRate: this.config.refillRate,
|
|
73
|
+
windowMs: this.config.windowMs,
|
|
74
|
+
failMode: this.config.failMode,
|
|
75
|
+
enableQueue: this.config.enableQueue,
|
|
76
|
+
queueTimeoutMs: this.config.queueTimeoutMs,
|
|
77
|
+
maxQueueSize: this.config.maxQueueSize,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Try to consume a token without queuing (internal method)
|
|
83
|
+
*/
|
|
84
|
+
async tryConsumeToken(key, cost) {
|
|
85
|
+
return tryConsumeTokenCore(this.storage, this.config, key, cost);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Check if a request is allowed under rate limit
|
|
89
|
+
*/
|
|
90
|
+
async checkLimit(key, cost = 1) {
|
|
91
|
+
const storageKey = `${this.config.keyPrefix}:${key}`;
|
|
92
|
+
const now = Date.now();
|
|
93
|
+
try {
|
|
94
|
+
let bucket = await this.storage.get(storageKey);
|
|
95
|
+
if (!bucket) {
|
|
96
|
+
bucket = {
|
|
97
|
+
tokens: this.config.maxTokens,
|
|
98
|
+
lastRefill: now,
|
|
99
|
+
firstRequest: now,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
// Refill tokens
|
|
103
|
+
const elapsedMs = now - bucket.lastRefill;
|
|
104
|
+
const elapsedSeconds = elapsedMs / 1000;
|
|
105
|
+
const tokensToAdd = elapsedSeconds * this.config.refillRate;
|
|
106
|
+
if (tokensToAdd > 0) {
|
|
107
|
+
bucket.tokens = Math.min(this.config.maxTokens, bucket.tokens + tokensToAdd);
|
|
108
|
+
bucket.lastRefill = now;
|
|
109
|
+
}
|
|
110
|
+
const allowed = bucket.tokens >= cost;
|
|
111
|
+
if (allowed) {
|
|
112
|
+
bucket.tokens -= cost;
|
|
113
|
+
await this.storage.set(storageKey, bucket, this.config.windowMs);
|
|
114
|
+
if (this.config.debug) {
|
|
115
|
+
log.debug(`Rate limit check: ${key}`, { allowed: true, remaining: bucket.tokens, cost });
|
|
116
|
+
}
|
|
117
|
+
this.metricsManager.update(key, true);
|
|
118
|
+
return {
|
|
119
|
+
allowed: true,
|
|
120
|
+
remaining: Math.floor(bucket.tokens),
|
|
121
|
+
limit: this.config.maxTokens,
|
|
122
|
+
metrics: this.metricsManager.get(key),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
const tokensNeeded = cost - bucket.tokens;
|
|
127
|
+
const retryAfterMs = Math.ceil((tokensNeeded / this.config.refillRate) * 1000);
|
|
128
|
+
const resetAt = new Date(now + retryAfterMs).toISOString();
|
|
129
|
+
if (this.config.debug) {
|
|
130
|
+
log.debug(`Rate limit exceeded: ${key}`, {
|
|
131
|
+
allowed: false,
|
|
132
|
+
remaining: bucket.tokens,
|
|
133
|
+
cost,
|
|
134
|
+
retryAfterMs,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
this.metricsManager.update(key, false);
|
|
138
|
+
const currentMetrics = this.metricsManager.get(key);
|
|
139
|
+
if (this.config.onLimitExceeded && currentMetrics) {
|
|
140
|
+
this.config.onLimitExceeded(key, currentMetrics);
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
allowed: false,
|
|
144
|
+
remaining: Math.floor(bucket.tokens),
|
|
145
|
+
limit: this.config.maxTokens,
|
|
146
|
+
retryAfterMs,
|
|
147
|
+
resetAt,
|
|
148
|
+
metrics: currentMetrics,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
this.metricsManager.update(key, this.config.failMode === 'open', true);
|
|
154
|
+
if (this.config.failMode === 'closed') {
|
|
155
|
+
log.error(`Rate limiter error (fail-closed) for ${key}: ${error instanceof Error ? error.message : String(error)}`);
|
|
156
|
+
return {
|
|
157
|
+
allowed: false,
|
|
158
|
+
remaining: 0,
|
|
159
|
+
limit: this.config.maxTokens,
|
|
160
|
+
retryAfterMs: this.config.windowMs,
|
|
161
|
+
resetAt: new Date(Date.now() + this.config.windowMs).toISOString(),
|
|
162
|
+
metrics: this.metricsManager.get(key),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
log.error(`Rate limiter error (fail-open) for ${key}: ${error instanceof Error ? error.message : String(error)}`);
|
|
166
|
+
return {
|
|
167
|
+
allowed: true,
|
|
168
|
+
remaining: this.config.maxTokens,
|
|
169
|
+
limit: this.config.maxTokens,
|
|
170
|
+
metrics: this.metricsManager.get(key),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Wait for a token to become available (SMI-1013)
|
|
176
|
+
*/
|
|
177
|
+
async waitForToken(key, cost = 1) {
|
|
178
|
+
if (!this.config.enableQueue) {
|
|
179
|
+
return this.checkLimit(key, cost);
|
|
180
|
+
}
|
|
181
|
+
const immediateResult = await this.tryConsumeToken(key, cost);
|
|
182
|
+
if (immediateResult.allowed) {
|
|
183
|
+
this.metricsManager.update(key, true);
|
|
184
|
+
return {
|
|
185
|
+
...immediateResult,
|
|
186
|
+
queued: false,
|
|
187
|
+
metrics: this.metricsManager.get(key),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
const result = await this.queueManager.queueRequest(key, cost, (allowed) => this.metricsManager.update(key, allowed));
|
|
191
|
+
return {
|
|
192
|
+
...result,
|
|
193
|
+
metrics: this.metricsManager.get(key),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Get queue status for a key (SMI-1013)
|
|
198
|
+
*/
|
|
199
|
+
getQueueStatus(key) {
|
|
200
|
+
return this.queueManager.getStatus(key);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Clear queue for a key (SMI-1013)
|
|
204
|
+
*/
|
|
205
|
+
clearQueue(key) {
|
|
206
|
+
this.queueManager.clear(key);
|
|
207
|
+
if (this.config.debug) {
|
|
208
|
+
log.debug(`Queue cleared${key ? ` for key: ${key}` : ' (all)'}`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Reset rate limit for a key
|
|
213
|
+
*/
|
|
214
|
+
async reset(key) {
|
|
215
|
+
await resetTokenBucket(this.storage, this.config.keyPrefix, key);
|
|
216
|
+
if (this.config.debug) {
|
|
217
|
+
log.debug(`Rate limit reset: ${key}`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Get current state for a key
|
|
222
|
+
*/
|
|
223
|
+
async getState(key) {
|
|
224
|
+
return getTokenBucketState(this.storage, this.config.keyPrefix, key);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Get metrics for a specific key or all keys
|
|
228
|
+
*/
|
|
229
|
+
getMetrics(key) {
|
|
230
|
+
if (key) {
|
|
231
|
+
return this.metricsManager.get(key);
|
|
232
|
+
}
|
|
233
|
+
return this.metricsManager.getAll();
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Reset metrics for a specific key or all keys
|
|
237
|
+
*/
|
|
238
|
+
resetMetrics(key) {
|
|
239
|
+
if (key) {
|
|
240
|
+
this.metricsManager.delete(key);
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
this.metricsManager.clear();
|
|
244
|
+
}
|
|
245
|
+
if (this.config.debug) {
|
|
246
|
+
log.debug(`Metrics reset${key ? ` for key: ${key}` : ' (all)'}`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Dispose of resources
|
|
251
|
+
*/
|
|
252
|
+
dispose() {
|
|
253
|
+
this.queueManager.dispose();
|
|
254
|
+
this.metricsManager.dispose();
|
|
255
|
+
if (this.storage instanceof InMemoryRateLimitStorage) {
|
|
256
|
+
this.storage.dispose();
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
//# sourceMappingURL=RateLimiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RateLimiter.js","sourceRoot":"","sources":["../../../../src/security/rate-limiter/RateLimiter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,uB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O,EAAE,CAAA;QACxB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limiter Constants - SMI-730, SMI-1189
|
|
3
|
+
*
|
|
4
|
+
* Constants for rate limiting functionality.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Maximum number of unique keys to track in queues and metrics
|
|
8
|
+
* Prevents unbounded memory growth from malicious or misconfigured clients
|
|
9
|
+
*/
|
|
10
|
+
export declare const MAX_UNIQUE_KEYS = 10000;
|
|
11
|
+
/**
|
|
12
|
+
* Metrics TTL in milliseconds (1 hour) - metrics older than this are cleaned up
|
|
13
|
+
*/
|
|
14
|
+
export declare const METRICS_TTL_MS: number;
|
|
15
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../../src/security/rate-limiter/constants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;GAGG;AACH,eAAO,MAAM,eAAe,QAAQ,CAAA;AAEpC;;GAEG;AACH,eAAO,MAAM,cAAc,QAAiB,CAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limiter Constants - SMI-730, SMI-1189
|
|
3
|
+
*
|
|
4
|
+
* Constants for rate limiting functionality.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Maximum number of unique keys to track in queues and metrics
|
|
8
|
+
* Prevents unbounded memory growth from malicious or misconfigured clients
|
|
9
|
+
*/
|
|
10
|
+
export const MAX_UNIQUE_KEYS = 10000;
|
|
11
|
+
/**
|
|
12
|
+
* Metrics TTL in milliseconds (1 hour) - metrics older than this are cleaned up
|
|
13
|
+
*/
|
|
14
|
+
export const METRICS_TTL_MS = 60 * 60 * 1000;
|
|
15
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../../src/security/rate-limiter/constants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;GAGG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,CAAA;AAEpC;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAA"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limiter Errors - SMI-1013, SMI-1189
|
|
3
|
+
*
|
|
4
|
+
* Error classes for rate limiting queue functionality.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Error thrown when queue timeout is exceeded (SMI-1013)
|
|
8
|
+
*/
|
|
9
|
+
export declare class RateLimitQueueTimeoutError extends Error {
|
|
10
|
+
readonly key: string;
|
|
11
|
+
readonly timeoutMs: number;
|
|
12
|
+
constructor(key: string, timeoutMs: number);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Error thrown when queue is full (SMI-1013)
|
|
16
|
+
*/
|
|
17
|
+
export declare class RateLimitQueueFullError extends Error {
|
|
18
|
+
readonly key: string;
|
|
19
|
+
readonly maxQueueSize: number;
|
|
20
|
+
constructor(key: string, maxQueueSize: number);
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../../src/security/rate-limiter/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,qBAAa,0BAA2B,SAAQ,KAAK;aAEjC,GAAG,EAAE,MAAM;aACX,SAAS,EAAE,MAAM;gBADjB,GAAG,EAAE,MAAM,EACX,SAAS,EAAE,MAAM;CAKpC;AAED;;GAEG;AACH,qBAAa,uBAAwB,SAAQ,KAAK;aAE9B,GAAG,EAAE,MAAM;aACX,YAAY,EAAE,MAAM;gBADpB,GAAG,EAAE,MAAM,EACX,YAAY,EAAE,MAAM;CAKvC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limiter Errors - SMI-1013, SMI-1189
|
|
3
|
+
*
|
|
4
|
+
* Error classes for rate limiting queue functionality.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Error thrown when queue timeout is exceeded (SMI-1013)
|
|
8
|
+
*/
|
|
9
|
+
export class RateLimitQueueTimeoutError extends Error {
|
|
10
|
+
key;
|
|
11
|
+
timeoutMs;
|
|
12
|
+
constructor(key, timeoutMs) {
|
|
13
|
+
super(`Rate limit queue timeout exceeded for key '${key}' after ${timeoutMs}ms`);
|
|
14
|
+
this.key = key;
|
|
15
|
+
this.timeoutMs = timeoutMs;
|
|
16
|
+
this.name = 'RateLimitQueueTimeoutError';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Error thrown when queue is full (SMI-1013)
|
|
21
|
+
*/
|
|
22
|
+
export class RateLimitQueueFullError extends Error {
|
|
23
|
+
key;
|
|
24
|
+
maxQueueSize;
|
|
25
|
+
constructor(key, maxQueueSize) {
|
|
26
|
+
super(`Rate limit queue full for key '${key}' (max: ${maxQueueSize})`);
|
|
27
|
+
this.key = key;
|
|
28
|
+
this.maxQueueSize = maxQueueSize;
|
|
29
|
+
this.name = 'RateLimitQueueFullError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../../src/security/rate-limiter/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;GAEG;AACH,MAAM,OAAO,0BAA2B,SAAQ,KAAK;IAEjC;IACA;IAFlB,YACkB,GAAW,EACX,SAAiB;QAEjC,KAAK,CAAC,8CAA8C,GAAG,WAAW,SAAS,IAAI,CAAC,CAAA;QAHhE,QAAG,GAAH,GAAG,CAAQ;QACX,cAAS,GAAT,SAAS,CAAQ;QAGjC,IAAI,CAAC,IAAI,GAAG,4BAA4B,CAAA;IAC1C,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAE9B;IACA;IAFlB,YACkB,GAAW,EACX,YAAoB;QAEpC,KAAK,CAAC,kCAAkC,GAAG,WAAW,YAAY,GAAG,CAAC,CAAA;QAHtD,QAAG,GAAH,GAAG,CAAQ;QACX,iBAAY,GAAZ,YAAY,CAAQ;QAGpC,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAA;IACvC,CAAC;CACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limiter Module - SMI-730, SMI-1013, SMI-1189
|
|
3
|
+
*
|
|
4
|
+
* Re-exports for rate limiting functionality.
|
|
5
|
+
*/
|
|
6
|
+
export type { RateLimitConfig, RateLimitMetrics, RateLimitResult, RateLimitStorage, TokenBucket, QueuedRequest, } from './types.js';
|
|
7
|
+
export { MAX_UNIQUE_KEYS, METRICS_TTL_MS } from './constants.js';
|
|
8
|
+
export { RateLimitQueueTimeoutError, RateLimitQueueFullError } from './errors.js';
|
|
9
|
+
export { InMemoryRateLimitStorage } from './storage.js';
|
|
10
|
+
export { RateLimiter } from './RateLimiter.js';
|
|
11
|
+
export { RATE_LIMIT_PRESETS, createRateLimiterFromPreset } from './presets.js';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/security/rate-limiter/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,YAAY,EACV,eAAe,EACf,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,WAAW,EACX,aAAa,GACd,MAAM,YAAY,CAAA;AAGnB,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAGhE,OAAO,EAAE,0BAA0B,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAGjF,OAAO,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAA;AAGvD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAG9C,OAAO,EAAE,kBAAkB,EAAE,2BAA2B,EAAE,MAAM,cAAc,CAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limiter Module - SMI-730, SMI-1013, SMI-1189
|
|
3
|
+
*
|
|
4
|
+
* Re-exports for rate limiting functionality.
|
|
5
|
+
*/
|
|
6
|
+
// Constants
|
|
7
|
+
export { MAX_UNIQUE_KEYS, METRICS_TTL_MS } from './constants.js';
|
|
8
|
+
// Errors
|
|
9
|
+
export { RateLimitQueueTimeoutError, RateLimitQueueFullError } from './errors.js';
|
|
10
|
+
// Storage
|
|
11
|
+
export { InMemoryRateLimitStorage } from './storage.js';
|
|
12
|
+
// Main class
|
|
13
|
+
export { RateLimiter } from './RateLimiter.js';
|
|
14
|
+
// Presets
|
|
15
|
+
export { RATE_LIMIT_PRESETS, createRateLimiterFromPreset } from './presets.js';
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/security/rate-limiter/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAYH,YAAY;AACZ,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEhE,SAAS;AACT,OAAO,EAAE,0BAA0B,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAEjF,UAAU;AACV,OAAO,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAA;AAEvD,aAAa;AACb,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAE9C,UAAU;AACV,OAAO,EAAE,kBAAkB,EAAE,2BAA2B,EAAE,MAAM,cAAc,CAAA"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limit Metrics Manager - SMI-730, SMI-1189
|
|
3
|
+
*
|
|
4
|
+
* Metrics tracking and management for rate limiting.
|
|
5
|
+
*/
|
|
6
|
+
import type { RateLimitMetrics } from './types.js';
|
|
7
|
+
/**
|
|
8
|
+
* Manages rate limit metrics with bounds checking and cleanup
|
|
9
|
+
*/
|
|
10
|
+
export declare class MetricsManager {
|
|
11
|
+
private readonly metrics;
|
|
12
|
+
private cleanupInterval;
|
|
13
|
+
private readonly debug;
|
|
14
|
+
private readonly log;
|
|
15
|
+
constructor(debug?: boolean, log?: {
|
|
16
|
+
debug: (msg: string) => void;
|
|
17
|
+
});
|
|
18
|
+
/**
|
|
19
|
+
* Start periodic cleanup of stale metrics
|
|
20
|
+
*/
|
|
21
|
+
startCleanup(): void;
|
|
22
|
+
/**
|
|
23
|
+
* Stop metrics cleanup
|
|
24
|
+
*/
|
|
25
|
+
stopCleanup(): void;
|
|
26
|
+
/**
|
|
27
|
+
* Clean up metrics older than METRICS_TTL_MS
|
|
28
|
+
*/
|
|
29
|
+
private cleanupStaleMetrics;
|
|
30
|
+
/**
|
|
31
|
+
* Update metrics for a key with bounds checking
|
|
32
|
+
*/
|
|
33
|
+
update(key: string, allowed: boolean, error?: boolean): void;
|
|
34
|
+
/**
|
|
35
|
+
* Get metrics for a specific key
|
|
36
|
+
*/
|
|
37
|
+
get(key: string): RateLimitMetrics | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Get all metrics
|
|
40
|
+
*/
|
|
41
|
+
getAll(): Map<string, RateLimitMetrics>;
|
|
42
|
+
/**
|
|
43
|
+
* Delete metrics for a key
|
|
44
|
+
*/
|
|
45
|
+
delete(key: string): void;
|
|
46
|
+
/**
|
|
47
|
+
* Clear all metrics
|
|
48
|
+
*/
|
|
49
|
+
clear(): void;
|
|
50
|
+
/**
|
|
51
|
+
* Dispose of resources
|
|
52
|
+
*/
|
|
53
|
+
dispose(): void;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=metrics-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metrics-manager.d.ts","sourceRoot":"","sources":["../../../../src/security/rate-limiter/metrics-manager.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAGlD;;GAEG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA2C;IACnE,OAAO,CAAC,eAAe,CAA8B;IACrD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAkC;gBAE1C,KAAK,UAAQ,EAAE,GAAG,CAAC,EAAE;QAAE,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAA;KAAE;IAKjE;;OAEG;IACH,YAAY,IAAI,IAAI;IAUpB;;OAEG;IACH,WAAW,IAAI,IAAI;IAOnB;;OAEG;IACH,OAAO,CAAC,mBAAmB;IA6B3B;;OAEG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,UAAQ,GAAG,IAAI;IAiD1D;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS;IAI9C;;OAEG;IACH,MAAM,IAAI,GAAG,CAAC,MAAM,EAAE,gBAAgB,CAAC;IAIvC;;OAEG;IACH,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAIzB;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACH,OAAO,IAAI,IAAI;CAIhB"}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limit Metrics Manager - SMI-730, SMI-1189
|
|
3
|
+
*
|
|
4
|
+
* Metrics tracking and management for rate limiting.
|
|
5
|
+
*/
|
|
6
|
+
import { MAX_UNIQUE_KEYS, METRICS_TTL_MS } from './constants.js';
|
|
7
|
+
/**
|
|
8
|
+
* Manages rate limit metrics with bounds checking and cleanup
|
|
9
|
+
*/
|
|
10
|
+
export class MetricsManager {
|
|
11
|
+
metrics = new Map();
|
|
12
|
+
cleanupInterval = null;
|
|
13
|
+
debug;
|
|
14
|
+
log;
|
|
15
|
+
constructor(debug = false, log) {
|
|
16
|
+
this.debug = debug;
|
|
17
|
+
this.log = log || { debug: () => { } };
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Start periodic cleanup of stale metrics
|
|
21
|
+
*/
|
|
22
|
+
startCleanup() {
|
|
23
|
+
// Clean up stale metrics every 5 minutes
|
|
24
|
+
this.cleanupInterval = setInterval(() => {
|
|
25
|
+
this.cleanupStaleMetrics();
|
|
26
|
+
}, 5 * 60 * 1000);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Stop metrics cleanup
|
|
30
|
+
*/
|
|
31
|
+
stopCleanup() {
|
|
32
|
+
if (this.cleanupInterval) {
|
|
33
|
+
clearInterval(this.cleanupInterval);
|
|
34
|
+
this.cleanupInterval = null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Clean up metrics older than METRICS_TTL_MS
|
|
39
|
+
*/
|
|
40
|
+
cleanupStaleMetrics() {
|
|
41
|
+
const now = new Date();
|
|
42
|
+
let cleaned = 0;
|
|
43
|
+
for (const [key, metrics] of this.metrics.entries()) {
|
|
44
|
+
if (now.getTime() - metrics.lastUpdated.getTime() > METRICS_TTL_MS) {
|
|
45
|
+
this.metrics.delete(key);
|
|
46
|
+
cleaned++;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// Also enforce MAX_UNIQUE_KEYS limit if somehow exceeded
|
|
50
|
+
if (this.metrics.size > MAX_UNIQUE_KEYS) {
|
|
51
|
+
// Sort by lastUpdated and remove oldest entries
|
|
52
|
+
const entries = Array.from(this.metrics.entries()).sort((a, b) => a[1].lastUpdated.getTime() - b[1].lastUpdated.getTime());
|
|
53
|
+
const toRemove = entries.slice(0, this.metrics.size - MAX_UNIQUE_KEYS);
|
|
54
|
+
for (const [key] of toRemove) {
|
|
55
|
+
this.metrics.delete(key);
|
|
56
|
+
cleaned++;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (cleaned > 0 && this.debug) {
|
|
60
|
+
this.log.debug(`Cleaned up ${cleaned} stale metric entries`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Update metrics for a key with bounds checking
|
|
65
|
+
*/
|
|
66
|
+
update(key, allowed, error = false) {
|
|
67
|
+
// Check if we've hit the max unique keys limit
|
|
68
|
+
if (!this.metrics.has(key) && this.metrics.size >= MAX_UNIQUE_KEYS) {
|
|
69
|
+
// Evict oldest entry before adding new one
|
|
70
|
+
let oldestKey = null;
|
|
71
|
+
let oldestTime = Infinity;
|
|
72
|
+
for (const [k, m] of this.metrics.entries()) {
|
|
73
|
+
if (m.lastUpdated.getTime() < oldestTime) {
|
|
74
|
+
oldestTime = m.lastUpdated.getTime();
|
|
75
|
+
oldestKey = k;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (oldestKey) {
|
|
79
|
+
this.metrics.delete(oldestKey);
|
|
80
|
+
if (this.debug) {
|
|
81
|
+
this.log.debug(`Evicted oldest metrics entry: ${oldestKey}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const now = new Date();
|
|
86
|
+
const existing = this.metrics.get(key) || {
|
|
87
|
+
allowed: 0,
|
|
88
|
+
blocked: 0,
|
|
89
|
+
errors: 0,
|
|
90
|
+
lastReset: now,
|
|
91
|
+
lastUpdated: now,
|
|
92
|
+
};
|
|
93
|
+
if (error) {
|
|
94
|
+
existing.errors++;
|
|
95
|
+
// Also track allowed/blocked for error cases (fail-open vs fail-closed)
|
|
96
|
+
if (allowed) {
|
|
97
|
+
existing.allowed++;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
existing.blocked++;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else if (allowed) {
|
|
104
|
+
existing.allowed++;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
existing.blocked++;
|
|
108
|
+
}
|
|
109
|
+
existing.lastUpdated = now;
|
|
110
|
+
this.metrics.set(key, existing);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Get metrics for a specific key
|
|
114
|
+
*/
|
|
115
|
+
get(key) {
|
|
116
|
+
return this.metrics.get(key);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Get all metrics
|
|
120
|
+
*/
|
|
121
|
+
getAll() {
|
|
122
|
+
return new Map(this.metrics);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Delete metrics for a key
|
|
126
|
+
*/
|
|
127
|
+
delete(key) {
|
|
128
|
+
this.metrics.delete(key);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Clear all metrics
|
|
132
|
+
*/
|
|
133
|
+
clear() {
|
|
134
|
+
this.metrics.clear();
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Dispose of resources
|
|
138
|
+
*/
|
|
139
|
+
dispose() {
|
|
140
|
+
this.stopCleanup();
|
|
141
|
+
this.clear();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
//# sourceMappingURL=metrics-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metrics-manager.js","sourceRoot":"","sources":["../../../../src/security/rate-limiter/metrics-manager.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEhE;;GAEG;AACH,MAAM,OAAO,cAAc;IACR,OAAO,GAAkC,IAAI,GAAG,EAAE,CAAA;IAC3D,eAAe,GAA0B,IAAI,CAAA;IACp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