@futdevpro/nts-dynamo 1.15.145 → 1.15.151

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.
Files changed (56) hide show
  1. package/.dynamo/logs/cicd-pipeline/output.log +1806 -1957
  2. package/.dynamo/logs/cicd-pipeline/status.json +33 -33
  3. package/README.md +16 -0
  4. package/__documentations/2026-08-21-distributed-rate-limit-bfr-ctm-010.md +59 -0
  5. package/__documentations/2026-08-22-npm-token-state-classifier.md +92 -0
  6. package/build/_collections/npm-token-check.util.d.ts +139 -0
  7. package/build/_collections/npm-token-check.util.d.ts.map +1 -0
  8. package/build/_collections/npm-token-check.util.js +302 -0
  9. package/build/_collections/npm-token-check.util.js.map +1 -0
  10. package/build/_modules/rate-limit/_enums/rate-limit-store-kind.type-enum.d.ts +6 -0
  11. package/build/_modules/rate-limit/_enums/rate-limit-store-kind.type-enum.d.ts.map +1 -0
  12. package/build/_modules/rate-limit/_enums/rate-limit-store-kind.type-enum.js +10 -0
  13. package/build/_modules/rate-limit/_enums/rate-limit-store-kind.type-enum.js.map +1 -0
  14. package/build/_modules/rate-limit/_models/rate-limit-config.interface.d.ts +15 -6
  15. package/build/_modules/rate-limit/_models/rate-limit-config.interface.d.ts.map +1 -1
  16. package/build/_modules/rate-limit/_models/rate-limit-consume-request.interface.d.ts +9 -0
  17. package/build/_modules/rate-limit/_models/rate-limit-consume-request.interface.d.ts.map +1 -0
  18. package/build/_modules/rate-limit/_models/rate-limit-consume-request.interface.js +3 -0
  19. package/build/_modules/rate-limit/_models/rate-limit-consume-request.interface.js.map +1 -0
  20. package/build/_modules/rate-limit/_models/rate-limit-consume-result.interface.d.ts +7 -0
  21. package/build/_modules/rate-limit/_models/rate-limit-consume-result.interface.d.ts.map +1 -0
  22. package/build/_modules/rate-limit/_models/rate-limit-consume-result.interface.js +3 -0
  23. package/build/_modules/rate-limit/_models/rate-limit-consume-result.interface.js.map +1 -0
  24. package/build/_modules/rate-limit/_models/rate-limit-store.interface.d.ts +9 -0
  25. package/build/_modules/rate-limit/_models/rate-limit-store.interface.d.ts.map +1 -0
  26. package/build/_modules/rate-limit/_models/rate-limit-store.interface.js +3 -0
  27. package/build/_modules/rate-limit/_models/rate-limit-store.interface.js.map +1 -0
  28. package/build/_modules/rate-limit/index.d.ts +5 -0
  29. package/build/_modules/rate-limit/index.d.ts.map +1 -1
  30. package/build/_modules/rate-limit/index.js +5 -1
  31. package/build/_modules/rate-limit/index.js.map +1 -1
  32. package/build/_modules/rate-limit/rate-limit-in-memory.store.d.ts +19 -0
  33. package/build/_modules/rate-limit/rate-limit-in-memory.store.d.ts.map +1 -0
  34. package/build/_modules/rate-limit/rate-limit-in-memory.store.js +71 -0
  35. package/build/_modules/rate-limit/rate-limit-in-memory.store.js.map +1 -0
  36. package/build/_modules/rate-limit/rate-limit.middleware.d.ts +39 -103
  37. package/build/_modules/rate-limit/rate-limit.middleware.d.ts.map +1 -1
  38. package/build/_modules/rate-limit/rate-limit.middleware.js +325 -236
  39. package/build/_modules/rate-limit/rate-limit.middleware.js.map +1 -1
  40. package/build/index.d.ts +1 -0
  41. package/build/index.d.ts.map +1 -1
  42. package/build/index.js +1 -0
  43. package/build/index.js.map +1 -1
  44. package/package.json +2 -2
  45. package/src/_collections/npm-token-check.util.spec.ts +218 -0
  46. package/src/_collections/npm-token-check.util.ts +327 -0
  47. package/src/_modules/rate-limit/_enums/rate-limit-store-kind.type-enum.ts +5 -0
  48. package/src/_modules/rate-limit/_models/rate-limit-config.interface.ts +19 -6
  49. package/src/_modules/rate-limit/_models/rate-limit-consume-request.interface.ts +9 -0
  50. package/src/_modules/rate-limit/_models/rate-limit-consume-result.interface.ts +6 -0
  51. package/src/_modules/rate-limit/_models/rate-limit-store.interface.ts +9 -0
  52. package/src/_modules/rate-limit/index.ts +5 -0
  53. package/src/_modules/rate-limit/rate-limit-in-memory.store.ts +91 -0
  54. package/src/_modules/rate-limit/rate-limit.middleware.spec.ts +424 -200
  55. package/src/_modules/rate-limit/rate-limit.middleware.ts +436 -275
  56. package/src/index.ts +1 -0
@@ -2,288 +2,377 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DyNTS_RateLimit_Middleware = void 0;
4
4
  const fsm_dynamo_1 = require("@futdevpro/fsm-dynamo");
5
- const singleton_service_base_1 = require("../../_services/base/singleton.service-base");
6
5
  const global_settings_const_1 = require("../../_collections/global-settings.const");
7
- /** Default request-limit per default-window. */
6
+ const singleton_service_base_1 = require("../../_services/base/singleton.service-base");
7
+ const rate_limit_store_kind_type_enum_1 = require("./_enums/rate-limit-store-kind.type-enum");
8
+ const rate_limit_in_memory_store_1 = require("./rate-limit-in-memory.store");
8
9
  const DEFAULT_LIMIT = 100;
9
- /** Default sliding-window hossza ms-ben (1 perc). */
10
- const DEFAULT_WINDOW_MS = 60000;
11
- /** Default response-header allitas. */
10
+ const DEFAULT_WINDOW_MS = 60_000;
12
11
  const DEFAULT_RESPONSE_HEADERS = true;
13
- /** Periodikus garbage-collection intervallum ms-ben (5 perc).
14
- * A request-log-bol takaritja a regen inaktiv kulcsokat (memory-leak prevention).
15
- */
16
- const GC_INTERVAL_MS = 5 * 60 * 1000;
17
- /** Service-nev az error-okhoz. */
12
+ const MAX_LIMIT = 1_000_000;
13
+ const MAX_WINDOW_MS = 7 * 24 * 60 * 60 * 1_000;
14
+ const MAX_BUCKET_PART_LENGTH = 240;
18
15
  const SERVICE_NAME = 'DyNTS_RateLimit_Middleware';
19
- /** ErrorCode-builder system shortcode + sajat kod. */
20
- const buildErrorCode = (subcode) => {
21
- const sys = global_settings_const_1.DyNTS_global_settings.systemShortCodeName ?? 'DyNTS';
22
- return `${sys}|DyNTS-RL-${subcode}`;
23
- };
24
- /**
25
- * Sliding-window HTTP rate-limit middleware — opt-in service-szel, a meglevo
26
- * `DyNTS_Endpoint_Params.preProcesses` mechanizmus mellol mukodik.
27
- *
28
- * **Hasznalat (host app):**
29
- * ```ts
30
- * const rateLimit = DyNTS_RateLimit_Middleware.getInstance();
31
- * rateLimit.configure({
32
- * defaultLimit: 100, // 100 req/perc default
33
- * defaultWindowMs: 60_000,
34
- * keyExtractor: (req) => req.headers['x-api-key'] as string || req.ip,
35
- * });
36
- *
37
- * new DyNTS_Endpoint_Params({
38
- * ...,
39
- * preProcesses: [rateLimit.check, ...other],
40
- * });
41
- *
42
- * // subscription-tier-up: per-kulcs egyedi limit
43
- * rateLimit.setPolicyForKey('subscriber-tier-key-123', {
44
- * limit: 1000,
45
- * windowMs: 60_000,
46
- * });
47
- * ```
48
- *
49
- * **Viselkedes:**
50
- * - Sliding-window algoritmus: minden request egy timestamp; a window-on
51
- * kivuli timestamp-ek nem szamolnak. Tobb pontos mint a fix-bucket
52
- * (boundary-burst nincs).
53
- * - In-memory storage — single-instance MVP-nek megfelelo. Multi-instance
54
- * prod-hoz Redis-backed extension kell (lasd a kozelebb dokumentumaltot).
55
- * - Limit lepes: 429 DyFM_Error + `X-RateLimit-*` + `Retry-After` header-ek.
56
- *
57
- * **Storage:** `Map<storageKey, timestamps[]>` ahol storageKey = `${subject}|${endpoint}`.
58
- * Periodikus GC takaritja a inaktiv kulcsokat.
59
- *
60
- * **Singleton:** `getInstance()`-szel hivd. A `.check` mezo binding-elve van
61
- * `this`-re, igy direkt atadhato `preProcesses`-be ujracsomagolas nelkul.
62
- */
16
+ /** Atomic HTTP rate-limit boundary with an explicit shared-store production gate. */
63
17
  class DyNTS_RateLimit_Middleware extends singleton_service_base_1.DyNTS_SingletonServiceBase {
64
- static getInstance() {
65
- return DyNTS_RateLimit_Middleware.getSingletonInstance();
66
- }
67
18
  defaultLimit = DEFAULT_LIMIT;
68
19
  defaultWindowMs = DEFAULT_WINDOW_MS;
69
- responseHeaders = DEFAULT_RESPONSE_HEADERS;
70
- keyExtractor = (req) => this.defaultKeyExtractor(req);
20
+ isResponseHeadersEnabled = DEFAULT_RESPONSE_HEADERS;
21
+ isSharedStoreRequired = false;
22
+ isKeyExtractorExplicit = false;
23
+ isEndpointGrouperExplicit = false;
24
+ keyExtractor = (req) => req.ip ?? 'unknown';
71
25
  endpointGrouper = (req) => req.path;
72
- /** request-log: storageKey → timestamp-tomb (Date.now() ms). */
73
- requestLog = new Map();
74
- /** Per-kulcs egyedi policy-k. */
26
+ costExtractor = (req) => {
27
+ void req;
28
+ return 1;
29
+ };
30
+ clock = () => Date.now();
31
+ store = new rate_limit_in_memory_store_1.DyNTS_RateLimit_InMemoryStore();
75
32
  keyPolicies = new Map();
76
- /** Per-endpoint(-csoport) egyedi policy-k (a `endpointGrouper` outputjara kulcsolva). */
77
33
  endpointPolicies = new Map();
78
- /** GC timer handle. */
79
- gcTimer = null;
80
- /**
81
- * Konfig override. Hianyzo mezok a default-okat orzik. Hivhato barmikor —
82
- * a `check()` a friss config-ot olvassa.
83
- */
34
+ /** Returns the process-wide rate-limit middleware. */
35
+ static getInstance() {
36
+ return DyNTS_RateLimit_Middleware.getSingletonInstance();
37
+ }
38
+ /** Applies a validated configuration atomically; invalid settings do not partially mutate the service. */
84
39
  configure(config) {
85
- if (config.defaultLimit !== undefined) {
86
- this.defaultLimit = config.defaultLimit;
87
- }
88
- if (config.defaultWindowMs !== undefined) {
89
- this.defaultWindowMs = config.defaultWindowMs;
90
- }
91
- if (config.keyExtractor !== undefined) {
92
- this.keyExtractor = config.keyExtractor;
93
- }
94
- if (config.endpointGrouper !== undefined) {
95
- this.endpointGrouper = config.endpointGrouper;
96
- }
97
- if (config.responseHeaders !== undefined) {
98
- this.responseHeaders = config.responseHeaders;
99
- }
100
- if (config.initialKeyPolicies !== undefined) {
101
- for (const [key, policy] of Object.entries(config.initialKeyPolicies)) {
102
- this.keyPolicies.set(key, policy);
103
- }
104
- }
105
- if (config.initialEndpointPolicies !== undefined) {
106
- for (const [endpoint, policy] of Object.entries(config.initialEndpointPolicies)) {
107
- this.endpointPolicies.set(endpoint, policy);
108
- }
109
- }
110
- this.startGcTimer();
40
+ const snapshot = this.buildConfigSnapshot(config);
41
+ this.defaultLimit = snapshot.defaultLimit;
42
+ this.defaultWindowMs = snapshot.defaultWindowMs;
43
+ this.isResponseHeadersEnabled = snapshot.isResponseHeadersEnabled;
44
+ this.isSharedStoreRequired = snapshot.isSharedStoreRequired;
45
+ this.keyExtractor = snapshot.keyExtractor;
46
+ this.endpointGrouper = snapshot.endpointGrouper;
47
+ this.costExtractor = snapshot.costExtractor;
48
+ this.clock = snapshot.clock;
49
+ this.store = snapshot.store;
50
+ this.isKeyExtractorExplicit = snapshot.isKeyExtractorExplicit;
51
+ this.isEndpointGrouperExplicit = snapshot.isEndpointGrouperExplicit;
52
+ this.keyPolicies = snapshot.keyPolicies;
53
+ this.endpointPolicies = snapshot.endpointPolicies;
111
54
  }
112
- /**
113
- * Aktualis konfig olvasasa (diagnosztika celokra).
114
- */
55
+ /** Returns content-free configuration diagnostics. */
115
56
  getConfig() {
116
57
  return {
117
58
  defaultLimit: this.defaultLimit,
118
59
  defaultWindowMs: this.defaultWindowMs,
119
- responseHeaders: this.responseHeaders,
60
+ responseHeaders: this.isResponseHeadersEnabled,
120
61
  activeKeyPolicies: this.keyPolicies.size,
121
62
  activeEndpointPolicies: this.endpointPolicies.size,
122
- trackedStorageKeys: this.requestLog.size,
63
+ trackedStorageKeys: this.store instanceof rate_limit_in_memory_store_1.DyNTS_RateLimit_InMemoryStore
64
+ ? this.store.getTrackedBucketCount()
65
+ : 0,
66
+ storeKind: this.store.storeKind,
67
+ requireSharedStore: this.isSharedStoreRequired,
123
68
  };
124
69
  }
125
- /**
126
- * Per-kulcs egyedi policy beallitas (pl. subscription-tier alapjan).
127
- * A `key`-nek pontosan azzal a stringgel kell egyeznie, amit a `keyExtractor`
128
- * visszaad.
129
- */
70
+ /** Sets one validated subject policy. */
130
71
  setPolicyForKey(key, policy) {
131
- this.keyPolicies.set(key, policy);
72
+ this.assertBucketPart(key);
73
+ this.assertPolicy(policy);
74
+ this.keyPolicies.set(key, { ...policy });
132
75
  }
133
- /**
134
- * Per-kulcs policy torlese (visszaall a default-ra).
135
- */
76
+ /** Removes one subject policy. */
136
77
  clearPolicyForKey(key) {
137
78
  this.keyPolicies.delete(key);
138
79
  }
139
- /**
140
- * Per-endpoint(-csoport) egyedi policy beallitas. Az `endpoint`-nek pontosan
141
- * azzal a stringgel kell egyeznie, amit az `endpointGrouper` visszaad (default: `req.path`).
142
- * Akkor hasznos, ha egy endpointnak a globalis default-tol eltero limit kell — pl. egy
143
- * webhook nagy burst-toleranciat igenyel (a legit, distributed forgalom ne bukjon), mig
144
- * egy admin-endpoint szuk limitet. Precedencia: per-kulcs policy > per-endpoint policy > default.
145
- */
80
+ /** Sets one validated endpoint-group policy. */
146
81
  setPolicyForEndpoint(endpoint, policy) {
147
- this.endpointPolicies.set(endpoint, policy);
82
+ this.assertBucketPart(endpoint);
83
+ this.assertPolicy(policy);
84
+ this.endpointPolicies.set(endpoint, { ...policy });
148
85
  }
149
- /**
150
- * Per-endpoint policy torlese (visszaall a default-ra).
151
- */
86
+ /** Removes one endpoint-group policy. */
152
87
  clearPolicyForEndpoint(endpoint) {
153
88
  this.endpointPolicies.delete(endpoint);
154
89
  }
155
- /**
156
- * Pre-process function — atadhato `DyNTS_Endpoint_Params.preProcesses`-be.
157
- *
158
- * Throws:
159
- * - 429 ha az aktualis request meghaladna a limit-et a sliding window-on
160
- *
161
- * Side-effect: ha `responseHeaders === true`, beallitja az `X-RateLimit-Limit`,
162
- * `X-RateLimit-Remaining`, `X-RateLimit-Reset` header-eket; 429 eseten
163
- * a `Retry-After` header-t is.
164
- */
90
+ /** Atomically consumes one request permit or raises a stable content-free error. */
165
91
  check = async (req, res) => {
166
- const subject = this.keyExtractor(req);
167
- const endpoint = this.endpointGrouper(req);
168
- const storageKey = `${subject}|${endpoint}`;
169
- // precedencia: per-kulcs policy (legspecifikusabb, pl. subscriber-tier) > per-endpoint policy > default
170
- const policy = this.keyPolicies.get(subject) ??
171
- this.endpointPolicies.get(endpoint) ??
172
- {
173
- limit: this.defaultLimit,
174
- windowMs: this.defaultWindowMs,
175
- };
176
- const now = Date.now();
177
- const windowStart = now - policy.windowMs;
178
- // sliding-window: tartomanyon kivuli timestamp-eket eldobjuk
179
- const existing = this.requestLog.get(storageKey) ?? [];
180
- const recent = existing.filter((t) => t > windowStart);
181
- if (recent.length >= policy.limit) {
182
- const oldest = recent[0];
183
- const resetAt = oldest + policy.windowMs;
184
- const retryAfterSec = Math.max(1, Math.ceil((resetAt - now) / 1000));
185
- if (this.responseHeaders) {
186
- res.setHeader('X-RateLimit-Limit', policy.limit.toString());
187
- res.setHeader('X-RateLimit-Remaining', '0');
188
- res.setHeader('X-RateLimit-Reset', Math.ceil(resetAt / 1000).toString());
189
- res.setHeader('Retry-After', retryAfterSec.toString());
190
- }
191
- // Frissitjuk a log-ot a kiszurt verzioval (felesleges regi timestamp-eket eldobtuk)
192
- this.requestLog.set(storageKey, recent);
193
- throw new fsm_dynamo_1.DyFM_Error({
194
- status: 429,
195
- errorCode: buildErrorCode('LIMIT'),
196
- addECToUserMsg: true,
197
- message: `Rate limit exceeded: ${policy.limit} req per ${policy.windowMs}ms for ${storageKey}`,
198
- userMessage: `Too many requests, retry after ${retryAfterSec}s`,
199
- issuerService: SERVICE_NAME,
92
+ const subject = this.extractBucketPart(this.keyExtractor, req);
93
+ const endpoint = this.extractBucketPart(this.endpointGrouper, req);
94
+ const cost = this.extractCost(req);
95
+ const policy = this.keyPolicies.get(subject)
96
+ ?? this.endpointPolicies.get(endpoint)
97
+ ?? { limit: this.defaultLimit, windowMs: this.defaultWindowMs };
98
+ this.assertPolicy(policy);
99
+ this.assertCost(cost, policy);
100
+ const now = this.getNow();
101
+ let result;
102
+ try {
103
+ result = await this.store.consume({
104
+ bucketKey: this.buildBucketKey(subject, endpoint),
105
+ policy: policy,
106
+ cost: cost,
107
+ now: now,
200
108
  });
201
109
  }
202
- // alatta vagyunk a limit-nek: append the current request
203
- recent.push(now);
204
- this.requestLog.set(storageKey, recent);
205
- if (this.responseHeaders) {
206
- res.setHeader('X-RateLimit-Limit', policy.limit.toString());
207
- res.setHeader('X-RateLimit-Remaining', Math.max(0, policy.limit - recent.length).toString());
208
- res.setHeader('X-RateLimit-Reset', Math.ceil((now + policy.windowMs) / 1000).toString());
110
+ catch {
111
+ this.throwRateLimitError({
112
+ status: 503,
113
+ subcode: 'STORE_UNAVAILABLE',
114
+ message: 'The configured rate-limit store could not commit a permit decision.',
115
+ userMessage: 'Request protection is temporarily unavailable. Please try again later.',
116
+ });
117
+ }
118
+ this.assertStoreResult(result, policy, now);
119
+ this.setResponseHeaders(res, {
120
+ policy: policy,
121
+ result: result,
122
+ now: now,
123
+ });
124
+ if (!result.allowed) {
125
+ const retryAfterSeconds = this.getRetryAfterSeconds(result.resetAt, now);
126
+ this.throwRateLimitError({
127
+ status: 429,
128
+ subcode: 'LIMIT',
129
+ message: 'The rate-limit policy rejected a request.',
130
+ userMessage: `Too many requests. Retry after ${retryAfterSeconds} seconds.`,
131
+ });
209
132
  }
210
133
  };
211
- /**
212
- * Default key-extractor: x-forwarded-for vagy req.ip vagy 'unknown'.
213
- * Csak akkor hasznalt, ha a host nem allit be sajat extractort a configure-ben.
214
- */
215
- defaultKeyExtractor(req) {
216
- const xff = req.headers['x-forwarded-for'];
217
- if (typeof xff === 'string' && xff.length > 0) {
218
- const first = xff.split(',')[0]?.trim() ?? '';
219
- if (first.length > 0) {
220
- return first;
221
- }
134
+ /** Performs explicit opportunistic cleanup only for the local test/development store. */
135
+ runGc() {
136
+ if (this.store instanceof rate_limit_in_memory_store_1.DyNTS_RateLimit_InMemoryStore) {
137
+ this.store.cleanup(this.clock());
222
138
  }
223
- if (Array.isArray(xff) && xff.length > 0) {
224
- const first = xff[0]?.split(',')[0]?.trim() ?? '';
225
- if (first.length > 0) {
226
- return first;
227
- }
139
+ }
140
+ /** Backward-compatible no-op: this implementation never starts a timer. */
141
+ stopGcTimer() { }
142
+ /** Resets all state to deterministic local defaults for tests. */
143
+ _resetForTesting() {
144
+ this.defaultLimit = DEFAULT_LIMIT;
145
+ this.defaultWindowMs = DEFAULT_WINDOW_MS;
146
+ this.isResponseHeadersEnabled = DEFAULT_RESPONSE_HEADERS;
147
+ this.isSharedStoreRequired = false;
148
+ this.isKeyExtractorExplicit = false;
149
+ this.isEndpointGrouperExplicit = false;
150
+ this.keyExtractor = (req) => req.ip ?? 'unknown';
151
+ this.endpointGrouper = (req) => req.path;
152
+ this.costExtractor = (req) => {
153
+ void req;
154
+ return 1;
155
+ };
156
+ this.clock = () => Date.now();
157
+ this.store = new rate_limit_in_memory_store_1.DyNTS_RateLimit_InMemoryStore();
158
+ this.keyPolicies.clear();
159
+ this.endpointPolicies.clear();
160
+ }
161
+ buildConfigSnapshot(config) {
162
+ const defaultLimit = config.defaultLimit ?? this.defaultLimit;
163
+ const defaultWindowMs = config.defaultWindowMs ?? this.defaultWindowMs;
164
+ const store = config.store ?? this.store;
165
+ const isSharedStoreRequired = config.requireSharedStore ?? this.isSharedStoreRequired;
166
+ const isKeyExtractorExplicit = config.keyExtractor !== undefined || this.isKeyExtractorExplicit;
167
+ const isEndpointGrouperExplicit = config.endpointGrouper !== undefined
168
+ || this.isEndpointGrouperExplicit;
169
+ const keyPolicies = new Map(this.keyPolicies);
170
+ const endpointPolicies = new Map(this.endpointPolicies);
171
+ this.assertPolicy({ limit: defaultLimit, windowMs: defaultWindowMs });
172
+ this.assertConfigurationFunctions(config);
173
+ this.assertStore(store);
174
+ this.addInitialPolicies(keyPolicies, config.initialKeyPolicies);
175
+ this.addInitialPolicies(endpointPolicies, config.initialEndpointPolicies);
176
+ if (isSharedStoreRequired
177
+ && (store.storeKind !== rate_limit_store_kind_type_enum_1.DyNTS_RateLimitStoreKind_Type.shared
178
+ || !isKeyExtractorExplicit
179
+ || !isEndpointGrouperExplicit)) {
180
+ this.throwRateLimitError({
181
+ status: 500,
182
+ subcode: 'SHARED_STORE_REQUIRED',
183
+ message: 'Production rate limiting requires a shared store and explicit opaque bucket extractors.',
184
+ userMessage: 'Request protection is not configured.',
185
+ });
228
186
  }
229
- return req.ip ?? 'unknown';
187
+ return {
188
+ defaultLimit: defaultLimit,
189
+ defaultWindowMs: defaultWindowMs,
190
+ isResponseHeadersEnabled: config.responseHeaders ?? this.isResponseHeadersEnabled,
191
+ isSharedStoreRequired: isSharedStoreRequired,
192
+ keyExtractor: config.keyExtractor ?? this.keyExtractor,
193
+ endpointGrouper: config.endpointGrouper ?? this.endpointGrouper,
194
+ costExtractor: config.costExtractor ?? this.costExtractor,
195
+ clock: config.clock ?? this.clock,
196
+ store: store,
197
+ isKeyExtractorExplicit: isKeyExtractorExplicit,
198
+ isEndpointGrouperExplicit: isEndpointGrouperExplicit,
199
+ keyPolicies: keyPolicies,
200
+ endpointPolicies: endpointPolicies,
201
+ };
230
202
  }
231
- /**
232
- * GC timer inditasa (idempotent). Periodikusan eltavolitja az inaktiv
233
- * storage-key-eket a request-log-bol — memory-leak prevention.
234
- */
235
- startGcTimer() {
236
- if (this.gcTimer !== null) {
203
+ addInitialPolicies(target, policies) {
204
+ if (!policies) {
237
205
  return;
238
206
  }
239
- this.gcTimer = setInterval(() => {
240
- this.runGc();
241
- }, GC_INTERVAL_MS);
242
- // unref hogy a process ne maradjon eletben a timer miatt
243
- if (typeof this.gcTimer.unref === 'function') {
244
- this.gcTimer.unref();
207
+ for (const entry of Object.entries(policies)) {
208
+ const key = entry[0];
209
+ const policy = entry[1];
210
+ this.assertBucketPart(key);
211
+ this.assertPolicy(policy);
212
+ target.set(key, { ...policy });
245
213
  }
246
214
  }
247
- /**
248
- * GC sweep — minden storage-key-rol levagja a regi timestamp-eket, es
249
- * eltavolitja az ureseket. Hivhato kulonosen test-bol.
250
- */
251
- runGc() {
252
- const now = Date.now();
253
- const horizon = now - this.defaultWindowMs;
254
- for (const [key, timestamps] of this.requestLog) {
255
- const recent = timestamps.filter((t) => t > horizon);
256
- if (recent.length === 0) {
257
- this.requestLog.delete(key);
258
- }
259
- else {
260
- this.requestLog.set(key, recent);
261
- }
215
+ assertPolicy(policy) {
216
+ if (!Number.isSafeInteger(policy.limit)
217
+ || policy.limit <= 0
218
+ || policy.limit > MAX_LIMIT
219
+ || !Number.isSafeInteger(policy.windowMs)
220
+ || policy.windowMs <= 0
221
+ || policy.windowMs > MAX_WINDOW_MS) {
222
+ this.throwRateLimitError({
223
+ status: 500,
224
+ subcode: 'POLICY_INVALID',
225
+ message: 'A rate-limit policy is outside the supported safety bounds.',
226
+ userMessage: 'Request protection is not configured.',
227
+ });
228
+ }
229
+ }
230
+ assertCost(cost, policy) {
231
+ if (!Number.isSafeInteger(cost) || cost <= 0 || cost > policy.limit) {
232
+ this.throwRateLimitError({
233
+ status: 500,
234
+ subcode: 'COST_INVALID',
235
+ message: 'A rate-limit request cost is outside the active policy.',
236
+ userMessage: 'Request protection is not configured.',
237
+ });
262
238
  }
263
239
  }
264
- /**
265
- * GC timer leallitasa (graceful shutdown vagy test-cleanup).
266
- */
267
- stopGcTimer() {
268
- if (this.gcTimer !== null) {
269
- clearInterval(this.gcTimer);
270
- this.gcTimer = null;
240
+ assertConfigurationFunctions(config) {
241
+ const hasInvalidFunction = (config.keyExtractor !== undefined && typeof config.keyExtractor !== 'function') || (config.endpointGrouper !== undefined && typeof config.endpointGrouper !== 'function') || (config.costExtractor !== undefined && typeof config.costExtractor !== 'function') || (config.clock !== undefined && typeof config.clock !== 'function');
242
+ if (hasInvalidFunction) {
243
+ this.throwRateLimitError({
244
+ status: 500,
245
+ subcode: 'CONFIG_INVALID',
246
+ message: 'A rate-limit configuration callback is invalid.',
247
+ userMessage: 'Request protection is not configured.',
248
+ });
271
249
  }
272
250
  }
273
- /**
274
- * Test-only: visszaallitja a default config-ot + uriti a state-et, hogy a
275
- * specfajlok ne szivarogjak at egymas state-jet. Production code NE hivja.
276
- */
277
- _resetForTesting() {
278
- this.defaultLimit = DEFAULT_LIMIT;
279
- this.defaultWindowMs = DEFAULT_WINDOW_MS;
280
- this.responseHeaders = DEFAULT_RESPONSE_HEADERS;
281
- this.keyExtractor = (req) => this.defaultKeyExtractor(req);
282
- this.endpointGrouper = (req) => req.path;
283
- this.requestLog.clear();
284
- this.keyPolicies.clear();
285
- this.endpointPolicies.clear();
286
- this.stopGcTimer();
251
+ assertStore(store) {
252
+ const hasKnownKind = store?.storeKind === rate_limit_store_kind_type_enum_1.DyNTS_RateLimitStoreKind_Type.inMemory
253
+ || store?.storeKind === rate_limit_store_kind_type_enum_1.DyNTS_RateLimitStoreKind_Type.shared;
254
+ if (!hasKnownKind || typeof store.consume !== 'function') {
255
+ this.throwRateLimitError({
256
+ status: 500,
257
+ subcode: 'STORE_INVALID',
258
+ message: 'The configured rate-limit store does not satisfy the Bedrock contract.',
259
+ userMessage: 'Request protection is not configured.',
260
+ });
261
+ }
262
+ }
263
+ extractBucketPart(extractor, req) {
264
+ let value;
265
+ try {
266
+ value = extractor(req);
267
+ }
268
+ catch {
269
+ value = undefined;
270
+ }
271
+ if (typeof value !== 'string') {
272
+ this.throwBucketKeyInvalid();
273
+ }
274
+ const normalized = value.trim();
275
+ this.assertBucketPart(normalized);
276
+ return normalized;
277
+ }
278
+ extractCost(req) {
279
+ try {
280
+ return this.costExtractor(req);
281
+ }
282
+ catch {
283
+ this.throwRateLimitError({
284
+ status: 500,
285
+ subcode: 'COST_INVALID',
286
+ message: 'The rate-limit request cost could not be derived.',
287
+ userMessage: 'Request protection is not configured.',
288
+ });
289
+ }
290
+ }
291
+ buildBucketKey(subject, endpoint) {
292
+ return `${subject.length}:${subject}${endpoint.length}:${endpoint}`;
293
+ }
294
+ getNow() {
295
+ let now;
296
+ try {
297
+ now = this.clock();
298
+ }
299
+ catch {
300
+ now = undefined;
301
+ }
302
+ if (!Number.isSafeInteger(now) || typeof now !== 'number' || now < 0) {
303
+ this.throwRateLimitError({
304
+ status: 503,
305
+ subcode: 'CLOCK_INVALID',
306
+ message: 'The rate-limit clock returned an invalid timestamp.',
307
+ userMessage: 'Request protection is temporarily unavailable. Please try again later.',
308
+ });
309
+ }
310
+ return now;
311
+ }
312
+ assertBucketPart(value) {
313
+ if (!value || value.length > MAX_BUCKET_PART_LENGTH) {
314
+ this.throwBucketKeyInvalid();
315
+ }
316
+ }
317
+ throwBucketKeyInvalid() {
318
+ this.throwRateLimitError({
319
+ status: 500,
320
+ subcode: 'BUCKET_KEY_INVALID',
321
+ message: 'A rate-limit bucket component is missing, invalid or too large.',
322
+ userMessage: 'Request protection is not configured.',
323
+ });
324
+ }
325
+ assertStoreResult(result, policy, now) {
326
+ const isValid = typeof result === 'object'
327
+ && result !== null
328
+ && 'allowed' in result
329
+ && 'remaining' in result
330
+ && 'resetAt' in result
331
+ && typeof result.allowed === 'boolean'
332
+ && typeof result.remaining === 'number'
333
+ && Number.isSafeInteger(result.remaining)
334
+ && result.remaining >= 0
335
+ && result.remaining <= policy.limit
336
+ && typeof result.resetAt === 'number'
337
+ && Number.isSafeInteger(result.resetAt)
338
+ && result.resetAt >= now;
339
+ if (!isValid) {
340
+ this.throwRateLimitError({
341
+ status: 503,
342
+ subcode: 'STORE_RESULT_INVALID',
343
+ message: 'The rate-limit store returned an invalid permit decision.',
344
+ userMessage: 'Request protection is temporarily unavailable. Please try again later.',
345
+ });
346
+ }
347
+ }
348
+ setResponseHeaders(res, context) {
349
+ if (!this.isResponseHeadersEnabled) {
350
+ return;
351
+ }
352
+ const resetSeconds = Math.ceil(context.result.resetAt / 1_000).toString();
353
+ res.setHeader('X-RateLimit-Limit', context.policy.limit.toString());
354
+ res.setHeader('X-RateLimit-Remaining', context.result.remaining.toString());
355
+ res.setHeader('X-RateLimit-Reset', resetSeconds);
356
+ res.setHeader('RateLimit-Limit', context.policy.limit.toString());
357
+ res.setHeader('RateLimit-Remaining', context.result.remaining.toString());
358
+ res.setHeader('RateLimit-Reset', Math.max(0, Math.ceil((context.result.resetAt - context.now) / 1_000)).toString());
359
+ if (!context.result.allowed) {
360
+ res.setHeader('Retry-After', this.getRetryAfterSeconds(context.result.resetAt, context.now).toString());
361
+ }
362
+ }
363
+ getRetryAfterSeconds(resetAt, now) {
364
+ return Math.max(1, Math.ceil((resetAt - now) / 1_000));
365
+ }
366
+ throwRateLimitError(context) {
367
+ const systemShortCode = global_settings_const_1.DyNTS_global_settings.systemShortCodeName ?? 'DyNTS';
368
+ throw new fsm_dynamo_1.DyFM_Error({
369
+ status: context.status,
370
+ errorCode: `${systemShortCode}|DyNTS-RL-${context.subcode}`,
371
+ addECToUserMsg: true,
372
+ message: context.message,
373
+ userMessage: context.userMessage,
374
+ issuerService: SERVICE_NAME,
375
+ });
287
376
  }
288
377
  }
289
378
  exports.DyNTS_RateLimit_Middleware = DyNTS_RateLimit_Middleware;