@blamejs/core 0.7.18 → 0.7.20
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/CHANGELOG.md +427 -423
- package/README.md +150 -150
- package/bin/blamejs.js +0 -0
- package/index.js +310 -308
- package/lib/api-key.js +660 -660
- package/lib/api-snapshot.js +338 -338
- package/lib/app-shutdown.js +385 -385
- package/lib/app.js +365 -365
- package/lib/archive.js +250 -250
- package/lib/atomic-file.js +544 -544
- package/lib/audit-chain.js +177 -177
- package/lib/audit-sign.js +344 -344
- package/lib/audit-tools.js +677 -677
- package/lib/audit.js +766 -766
- package/lib/auth/jwt-external.js +365 -0
- package/lib/auth/jwt.js +337 -311
- package/lib/auth/lockout.js +436 -436
- package/lib/auth/oauth.js +721 -721
- package/lib/auth/passkey.js +181 -181
- package/lib/auth/password.js +628 -594
- package/lib/backup/bundle.js +217 -217
- package/lib/backup/crypto.js +176 -176
- package/lib/backup/index.js +515 -515
- package/lib/backup/manifest.js +282 -282
- package/lib/break-glass.js +1338 -1338
- package/lib/bundler.js +441 -441
- package/lib/cache-redis.js +256 -256
- package/lib/cache.js +1206 -1206
- package/lib/canonical-json.js +115 -115
- package/lib/chain-writer.js +234 -234
- package/lib/cli-helpers.js +206 -206
- package/lib/cli.js +2334 -2334
- package/lib/cluster-provider-db.js +317 -317
- package/lib/cluster-storage.js +226 -226
- package/lib/cluster.js +703 -703
- package/lib/config-drift.js +301 -301
- package/lib/consent.js +222 -222
- package/lib/constants.js +191 -191
- package/lib/cookies.js +350 -315
- package/lib/credential-hash.js +322 -322
- package/lib/crypto.js +266 -266
- package/lib/csv.js +275 -275
- package/lib/db-declare-row-policy.js +267 -267
- package/lib/db-declare-view.js +420 -420
- package/lib/db-query.js +406 -406
- package/lib/db-schema.js +319 -319
- package/lib/db.js +1288 -1288
- package/lib/deprecate.js +222 -222
- package/lib/dev.js +335 -335
- package/lib/dual-control.js +473 -473
- package/lib/error-page.js +420 -420
- package/lib/external-db-migrate.js +441 -441
- package/lib/external-db.js +1061 -1061
- package/lib/file-type.js +273 -273
- package/lib/forms.js +422 -422
- package/lib/framework-error.js +293 -293
- package/lib/framework-schema.js +717 -717
- package/lib/handlers.js +350 -350
- package/lib/http-client-cookie-jar.js +508 -508
- package/lib/http-client.js +1195 -1195
- package/lib/i18n.js +878 -878
- package/lib/jobs.js +185 -185
- package/lib/log-stream-cloudwatch.js +369 -369
- package/lib/log-stream-local.js +146 -146
- package/lib/log-stream-otlp-grpc.js +410 -410
- package/lib/log-stream-otlp.js +286 -286
- package/lib/log-stream-syslog.js +302 -302
- package/lib/log-stream-webhook.js +199 -199
- package/lib/log-stream.js +330 -330
- package/lib/log.js +500 -500
- package/lib/mail-bounce.js +528 -528
- package/lib/mail-dkim.js +369 -369
- package/lib/mail.js +981 -981
- package/lib/metrics.js +683 -683
- package/lib/middleware/api-encrypt.js +936 -936
- package/lib/middleware/attach-user.js +157 -157
- package/lib/middleware/bearer-auth.js +152 -0
- package/lib/middleware/body-parser.js +1170 -1170
- package/lib/middleware/bot-guard.js +178 -178
- package/lib/middleware/compression.js +452 -452
- package/lib/middleware/cors.js +314 -314
- package/lib/middleware/csp-nonce.js +348 -348
- package/lib/middleware/csrf-protect.js +399 -316
- package/lib/middleware/db-role-for.js +264 -264
- package/lib/middleware/fetch-metadata.js +129 -0
- package/lib/middleware/health.js +392 -392
- package/lib/middleware/index.js +85 -79
- package/lib/middleware/rate-limit.js +358 -358
- package/lib/middleware/request-id.js +61 -61
- package/lib/middleware/request-log.js +168 -168
- package/lib/middleware/require-auth.js +104 -104
- package/lib/middleware/security-headers.js +121 -116
- package/lib/middleware/sse.js +166 -166
- package/lib/migrations.js +383 -383
- package/lib/mtls-ca.js +518 -518
- package/lib/mtls-engine-default.js +481 -481
- package/lib/network-dns.js +632 -632
- package/lib/network-heartbeat.js +290 -290
- package/lib/network-nts.js +574 -574
- package/lib/network-proxy.js +265 -265
- package/lib/network-tls.js +328 -328
- package/lib/network.js +233 -233
- package/lib/notify.js +612 -612
- package/lib/ntp-check.js +229 -229
- package/lib/numeric-bounds.js +111 -111
- package/lib/object-store/azure-blob-bucket-ops.js +349 -349
- package/lib/object-store/azure-blob.js +488 -488
- package/lib/object-store/gcs-bucket-ops.js +351 -351
- package/lib/object-store/gcs.js +519 -519
- package/lib/object-store/http-put.js +153 -153
- package/lib/object-store/index.js +197 -197
- package/lib/object-store/sigv4-bucket-ops.js +1092 -1092
- package/lib/object-store/sigv4.js +903 -903
- package/lib/observability.js +151 -151
- package/lib/otel-export.js +269 -269
- package/lib/pagination.js +464 -464
- package/lib/parsers/index.js +80 -80
- package/lib/parsers/safe-env.js +642 -642
- package/lib/parsers/safe-ini.js +292 -292
- package/lib/parsers/safe-toml.js +784 -784
- package/lib/parsers/safe-xml.js +390 -390
- package/lib/parsers/safe-yaml.js +1015 -1015
- package/lib/permissions.js +708 -708
- package/lib/pqc-agent.js +87 -87
- package/lib/pqc-gate.js +279 -279
- package/lib/protobuf-encoder.js +190 -190
- package/lib/protocol-dispatcher.js +161 -161
- package/lib/pubsub-redis.js +167 -167
- package/lib/pubsub.js +429 -429
- package/lib/queue-local.js +476 -476
- package/lib/queue-redis.js +745 -745
- package/lib/queue-sqs.js +319 -319
- package/lib/queue.js +695 -695
- package/lib/redis-client.js +519 -519
- package/lib/request-helpers.js +340 -340
- package/lib/restore-bundle.js +237 -237
- package/lib/restore-rollback.js +259 -259
- package/lib/restore.js +409 -409
- package/lib/retry.js +376 -376
- package/lib/router.js +748 -748
- package/lib/safe-async.js +735 -735
- package/lib/safe-buffer.js +237 -237
- package/lib/safe-json.js +541 -541
- package/lib/safe-schema.js +1266 -1266
- package/lib/safe-url.js +159 -159
- package/lib/scheduler.js +706 -706
- package/lib/security-assert.js +373 -373
- package/lib/seeders.js +618 -618
- package/lib/session.js +535 -478
- package/lib/slug.js +269 -269
- package/lib/ssrf-guard.js +401 -401
- package/lib/static.js +7 -5
- package/lib/storage.js +471 -471
- package/lib/subject.js +281 -281
- package/lib/template.js +791 -791
- package/lib/testing.js +798 -798
- package/lib/time.js +310 -310
- package/lib/totp.js +302 -302
- package/lib/tracing.js +494 -494
- package/lib/uuid.js +132 -132
- package/lib/validate-opts.js +340 -340
- package/lib/vault/index.js +308 -308
- package/lib/vault/rotate.js +784 -784
- package/lib/vault/wrap.js +296 -296
- package/lib/vendor/noble-ciphers.cjs +9 -9
- package/lib/webhook.js +595 -595
- package/lib/websocket.js +1048 -1048
- package/package.json +77 -77
- package/sbom.cyclonedx.json +7 -7
|
@@ -1,358 +1,358 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Rate-limit middleware — pluggable backend, default in-memory.
|
|
4
|
-
*
|
|
5
|
-
* Per-IP by default; key extractor is configurable (per-user, per-API-key,
|
|
6
|
-
* per-route). Two built-in backends:
|
|
7
|
-
*
|
|
8
|
-
* - 'memory' (default) — token-bucket, in-process. Each key gets
|
|
9
|
-
* `burst` tokens up front; tokens refill at `refillPerSecond`;
|
|
10
|
-
* each request costs 1 token. Single-process accuracy only.
|
|
11
|
-
*
|
|
12
|
-
* - 'cluster' — fixed-window counter shared across the cluster
|
|
13
|
-
* via `_blamejs_rate_limit_counters`. Atomic INSERT...ON CONFLICT
|
|
14
|
-
* increments per key within a window and rolls over when the
|
|
15
|
-
* window advances. Multi-process / multi-node accurate.
|
|
16
|
-
*
|
|
17
|
-
* Cluster opt-in switches the algorithm shape from token-bucket
|
|
18
|
-
* to fixed-window because that's what models cleanly in SQL —
|
|
19
|
-
* the operator-facing config keys change accordingly.
|
|
20
|
-
*
|
|
21
|
-
* Operators can also pass a custom `{ take, reset }` object as the
|
|
22
|
-
* backend for Redis / Memcached / etc.
|
|
23
|
-
*
|
|
24
|
-
* Options:
|
|
25
|
-
* {
|
|
26
|
-
* keyFn: (req) → 'rate-key' (default: client IP)
|
|
27
|
-
* statusOnLimit: 429
|
|
28
|
-
* bodyOnLimit: 'Too Many Requests'
|
|
29
|
-
* header: true // set X-RateLimit-* response headers
|
|
30
|
-
* skipPaths: [] // string-prefix or regex matchers
|
|
31
|
-
* scope: 'global' | 'per-route' (default 'global')
|
|
32
|
-
* backend: 'memory' (default) | 'cluster' | { take, reset, gc? }
|
|
33
|
-
*
|
|
34
|
-
* // Memory-backend tuning (token bucket):
|
|
35
|
-
* burst: 60 // initial token bucket size
|
|
36
|
-
* refillPerSecond: 10 // sustained throughput
|
|
37
|
-
*
|
|
38
|
-
* // Cluster-backend tuning (fixed window):
|
|
39
|
-
* limit: 60 // max requests per window
|
|
40
|
-
* windowMs: C.TIME.minutes(1) // window duration
|
|
41
|
-
* pruneIntervalMs: C.TIME.minutes(5) // how often the leader prunes expired rows
|
|
42
|
-
* }
|
|
43
|
-
*
|
|
44
|
-
* Audit: every limit hit emits system.ratelimit.block with the key + path.
|
|
45
|
-
*/
|
|
46
|
-
var C = require("../constants");
|
|
47
|
-
var lazyRequire = require("../lazy-require");
|
|
48
|
-
var requestHelpers = require("../request-helpers");
|
|
49
|
-
var safeAsync = require("../safe-async");
|
|
50
|
-
var validateOpts = require("../validate-opts");
|
|
51
|
-
var clusterStorage = require("../cluster-storage");
|
|
52
|
-
|
|
53
|
-
var audit = lazyRequire(function () { return require("../audit"); });
|
|
54
|
-
var logger = lazyRequire(function () { return require("../log").boot("rate-limit"); });
|
|
55
|
-
|
|
56
|
-
// `_clientIp` defers to `requestHelpers.clientIp`, threading the
|
|
57
|
-
// per-middleware `trustProxy` opt. Default refuses forwarded headers
|
|
58
|
-
// (returning the socket address only) — operators behind a sanitizing
|
|
59
|
-
// reverse proxy opt in via `trustProxy: true` (or a hop count).
|
|
60
|
-
function _clientIpFor(trustProxy) {
|
|
61
|
-
return function (req) {
|
|
62
|
-
var ip = requestHelpers.clientIp(req, { trustProxy: trustProxy });
|
|
63
|
-
return ip || "unknown";
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Reject NaN / Infinity / negative / non-positive / non-number at create
|
|
68
|
-
// time so a misconfigured rate-limit can't silently degrade to "no
|
|
69
|
-
// limit" or produce divide-by-zero verdicts at request time.
|
|
70
|
-
function _requirePositiveNumber(name, value) {
|
|
71
|
-
if (typeof value !== "number" || !isFinite(value) || value <= 0) {
|
|
72
|
-
throw new Error("middleware.rateLimit: " + name + " must be a positive finite number, got " +
|
|
73
|
-
JSON.stringify(value));
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// ---- Memory backend (token bucket) ----
|
|
78
|
-
|
|
79
|
-
function _memoryBackend(opts) {
|
|
80
|
-
// Default 1-per-second tokens fully refilled in 1 minute = 60 tokens.
|
|
81
|
-
var burst = opts.burst != null ? opts.burst : C.TIME.minutes(1) / C.TIME.seconds(1);
|
|
82
|
-
var refillPerSecond = opts.refillPerSecond != null ? opts.refillPerSecond : 10;
|
|
83
|
-
_requirePositiveNumber("burst", burst);
|
|
84
|
-
_requirePositiveNumber("refillPerSecond", refillPerSecond);
|
|
85
|
-
var buckets = new Map();
|
|
86
|
-
|
|
87
|
-
// Periodic GC of stale buckets so the map doesn't grow unbounded.
|
|
88
|
-
var gcInterval = safeAsync.repeating(function () {
|
|
89
|
-
var cutoff = Date.now() - C.TIME.hours(1);
|
|
90
|
-
for (var k of buckets.keys()) {
|
|
91
|
-
if (buckets.get(k).lastRefillAt < cutoff) buckets.delete(k);
|
|
92
|
-
}
|
|
93
|
-
}, C.TIME.minutes(5), { name: "rate-limit-gc" });
|
|
94
|
-
|
|
95
|
-
// Memory backend returns verdicts SYNCHRONOUSLY — no awaitable.
|
|
96
|
-
// The middleware checks `.then` to decide whether to chain. Keeping
|
|
97
|
-
// the hot path synchronous means single-process apps pay no
|
|
98
|
-
// microtask cost per request.
|
|
99
|
-
function take(key, _cost) {
|
|
100
|
-
var now = Date.now();
|
|
101
|
-
var b = buckets.get(key);
|
|
102
|
-
if (!b) {
|
|
103
|
-
b = { tokens: burst, lastRefillAt: now };
|
|
104
|
-
buckets.set(key, b);
|
|
105
|
-
} else {
|
|
106
|
-
var elapsed = (now - b.lastRefillAt) / C.TIME.seconds(1);
|
|
107
|
-
b.tokens = Math.min(burst, b.tokens + elapsed * refillPerSecond);
|
|
108
|
-
b.lastRefillAt = now;
|
|
109
|
-
}
|
|
110
|
-
if (b.tokens >= 1) {
|
|
111
|
-
b.tokens -= 1;
|
|
112
|
-
return {
|
|
113
|
-
allowed: true,
|
|
114
|
-
limit: burst,
|
|
115
|
-
remaining: Math.floor(b.tokens),
|
|
116
|
-
retryAfter: 0,
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
var deficit = 1 - b.tokens;
|
|
120
|
-
var waitMs = Math.ceil(C.TIME.seconds(deficit / refillPerSecond));
|
|
121
|
-
return {
|
|
122
|
-
allowed: false,
|
|
123
|
-
limit: burst,
|
|
124
|
-
remaining: 0,
|
|
125
|
-
retryAfter: Math.ceil(waitMs / C.TIME.seconds(1)),
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function reset(key) {
|
|
130
|
-
buckets.delete(key);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function close() {
|
|
134
|
-
try { gcInterval.stop(); } catch (_e) { /* timer already stopped */ }
|
|
135
|
-
buckets.clear();
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return { take: take, reset: reset, close: close };
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// ---- Cluster backend (fixed-window counter, SQL-backed) ----
|
|
142
|
-
|
|
143
|
-
function _clusterBackend(opts) {
|
|
144
|
-
// Default 60 requests per 1-minute window — derive the count from
|
|
145
|
-
// the same C.TIME source so the cadence stays in one place.
|
|
146
|
-
var limit = opts.limit != null ? opts.limit : C.TIME.minutes(1) / C.TIME.seconds(1);
|
|
147
|
-
var windowMs = opts.windowMs != null ? opts.windowMs : C.TIME.minutes(1);
|
|
148
|
-
var pruneIntervalMs = opts.pruneIntervalMs != null
|
|
149
|
-
? opts.pruneIntervalMs : C.TIME.minutes(5);
|
|
150
|
-
_requirePositiveNumber("limit", limit);
|
|
151
|
-
_requirePositiveNumber("windowMs", windowMs);
|
|
152
|
-
_requirePositiveNumber("pruneIntervalMs", pruneIntervalMs);
|
|
153
|
-
var lastPruneAt = 0;
|
|
154
|
-
|
|
155
|
-
// Best-effort prune of expired window rows. Rate-limited at the
|
|
156
|
-
// call site so we don't hammer the DB on every request.
|
|
157
|
-
function _maybePrune() {
|
|
158
|
-
var now = Date.now();
|
|
159
|
-
if (now - lastPruneAt < pruneIntervalMs) return;
|
|
160
|
-
lastPruneAt = now;
|
|
161
|
-
var cutoff = now - windowMs;
|
|
162
|
-
clusterStorage.execute(
|
|
163
|
-
"DELETE FROM _blamejs_rate_limit_counters WHERE windowStart < ?",
|
|
164
|
-
[cutoff]
|
|
165
|
-
).catch(function (e) {
|
|
166
|
-
try {
|
|
167
|
-
logger().warn("rate-limit prune failed: " + ((e && e.message) || String(e)));
|
|
168
|
-
} catch (_e) { /* logger best-effort */ }
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
async function take(key, _cost) {
|
|
173
|
-
var now = Date.now();
|
|
174
|
-
var windowStart = Math.floor(now / windowMs) * windowMs;
|
|
175
|
-
|
|
176
|
-
// Atomic increment: a fresh window resets count to 1; an existing
|
|
177
|
-
// row in the same window gets count + 1. Postgres + SQLite both
|
|
178
|
-
// support ON CONFLICT...DO UPDATE...RETURNING.
|
|
179
|
-
var result = await clusterStorage.execute(
|
|
180
|
-
"INSERT INTO _blamejs_rate_limit_counters (key, windowStart, count) " +
|
|
181
|
-
"VALUES (?, ?, 1) " +
|
|
182
|
-
"ON CONFLICT (key) DO UPDATE SET " +
|
|
183
|
-
" count = CASE " +
|
|
184
|
-
" WHEN excluded.windowStart > _blamejs_rate_limit_counters.windowStart " +
|
|
185
|
-
" THEN 1 " +
|
|
186
|
-
" ELSE _blamejs_rate_limit_counters.count + 1 " +
|
|
187
|
-
" END, " +
|
|
188
|
-
" windowStart = CASE " +
|
|
189
|
-
" WHEN excluded.windowStart > _blamejs_rate_limit_counters.windowStart " +
|
|
190
|
-
" THEN excluded.windowStart " +
|
|
191
|
-
" ELSE _blamejs_rate_limit_counters.windowStart " +
|
|
192
|
-
" END " +
|
|
193
|
-
"RETURNING count, windowStart",
|
|
194
|
-
[key, windowStart]
|
|
195
|
-
);
|
|
196
|
-
var row = result.rows && result.rows[0];
|
|
197
|
-
var count = row ? row.count : 1;
|
|
198
|
-
var rowWindow = row ? row.windowStart : windowStart;
|
|
199
|
-
|
|
200
|
-
_maybePrune();
|
|
201
|
-
|
|
202
|
-
if (count <= limit) {
|
|
203
|
-
return {
|
|
204
|
-
allowed: true,
|
|
205
|
-
limit: limit,
|
|
206
|
-
remaining: Math.max(0, limit - count),
|
|
207
|
-
retryAfter: 0,
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
var retryMs = (rowWindow + windowMs) - now;
|
|
211
|
-
return {
|
|
212
|
-
allowed: false,
|
|
213
|
-
limit: limit,
|
|
214
|
-
remaining: 0,
|
|
215
|
-
retryAfter: Math.max(1, Math.ceil(retryMs / C.TIME.seconds(1))),
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
async function reset(key) {
|
|
220
|
-
await clusterStorage.execute(
|
|
221
|
-
"DELETE FROM _blamejs_rate_limit_counters WHERE key = ?",
|
|
222
|
-
[key]
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function close() { /* no resources to release */ }
|
|
227
|
-
|
|
228
|
-
return { take: take, reset: reset, close: close };
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// ---- Backend resolution ----
|
|
232
|
-
|
|
233
|
-
function _resolveBackend(opts) {
|
|
234
|
-
var requested = opts.backend;
|
|
235
|
-
if (requested && typeof requested === "object" && typeof requested.take === "function") {
|
|
236
|
-
return requested; // operator-supplied custom backend
|
|
237
|
-
}
|
|
238
|
-
if (requested === "cluster") return _clusterBackend(opts);
|
|
239
|
-
if (!requested || requested === "memory") return _memoryBackend(opts);
|
|
240
|
-
throw new Error("rate-limit: unknown backend '" + requested +
|
|
241
|
-
"' (must be 'memory', 'cluster', or { take, reset })");
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
function create(opts) {
|
|
245
|
-
opts = opts || {};
|
|
246
|
-
validateOpts(opts, [
|
|
247
|
-
"keyFn", "statusOnLimit", "bodyOnLimit", "header", "skipPaths", "scope",
|
|
248
|
-
"backend", "trustProxy",
|
|
249
|
-
// memory backend
|
|
250
|
-
"burst", "refillPerSecond",
|
|
251
|
-
// cluster backend
|
|
252
|
-
"limit", "windowMs", "pruneIntervalMs",
|
|
253
|
-
], "middleware.rateLimit");
|
|
254
|
-
var trustProxy = opts.trustProxy === true || typeof opts.trustProxy === "number"
|
|
255
|
-
? opts.trustProxy : false;
|
|
256
|
-
var _clientIp = _clientIpFor(trustProxy);
|
|
257
|
-
var keyFn = opts.keyFn || _clientIp;
|
|
258
|
-
var statusOnLimit = opts.statusOnLimit || 429;
|
|
259
|
-
var bodyOnLimit = opts.bodyOnLimit !== undefined ? opts.bodyOnLimit : "Too Many Requests";
|
|
260
|
-
var emitHeaders = opts.header !== false;
|
|
261
|
-
var skipPaths = opts.skipPaths || [];
|
|
262
|
-
// Throw at create(): each entry must be a string prefix or a RegExp.
|
|
263
|
-
// Anything else would crash _shouldSkip with TypeError on the first request.
|
|
264
|
-
for (var sp = 0; sp < skipPaths.length; sp++) {
|
|
265
|
-
if (typeof skipPaths[sp] !== "string" && !(skipPaths[sp] instanceof RegExp)) {
|
|
266
|
-
throw new Error("middleware.rateLimit: skipPaths[" + sp +
|
|
267
|
-
"] must be a string prefix or RegExp, got " + typeof skipPaths[sp]);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
var scope = opts.scope || "global";
|
|
271
|
-
|
|
272
|
-
var backend = _resolveBackend(opts);
|
|
273
|
-
|
|
274
|
-
function _shouldSkip(req) {
|
|
275
|
-
var path = req.pathname || req.url || "/";
|
|
276
|
-
for (var i = 0; i < skipPaths.length; i++) {
|
|
277
|
-
if (typeof skipPaths[i] === "string" ? path.indexOf(skipPaths[i]) === 0 : skipPaths[i].test(path)) {
|
|
278
|
-
return true;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
return false;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
function _writeBlocked(req, res, k, verdict) {
|
|
285
|
-
if (emitHeaders && typeof res.setHeader === "function") {
|
|
286
|
-
res.setHeader("X-RateLimit-Limit", String(verdict.limit));
|
|
287
|
-
res.setHeader("X-RateLimit-Remaining", String(verdict.remaining));
|
|
288
|
-
if (verdict.retryAfter > 0) res.setHeader("Retry-After", String(verdict.retryAfter));
|
|
289
|
-
}
|
|
290
|
-
try {
|
|
291
|
-
// Override `ip` with the x-forwarded-for-aware client IP so the
|
|
292
|
-
// audit event carries the proxied origin even when extractActorContext
|
|
293
|
-
// would have read the socket address.
|
|
294
|
-
audit().emit({
|
|
295
|
-
actor: requestHelpers.extractActorContext(req, { ip: _clientIp(req) }),
|
|
296
|
-
action: "system.ratelimit.block",
|
|
297
|
-
outcome: "denied",
|
|
298
|
-
reason: "rate limit exceeded",
|
|
299
|
-
metadata: { key: k, method: req.method, path: req.pathname || req.url, retryAfter: verdict.retryAfter },
|
|
300
|
-
requestId: req.requestId,
|
|
301
|
-
});
|
|
302
|
-
} catch (_e) { /* audit best-effort */ }
|
|
303
|
-
if (typeof res.writeHead === "function") {
|
|
304
|
-
res.writeHead(statusOnLimit, { "Content-Type": "text/plain" });
|
|
305
|
-
res.end(bodyOnLimit);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
var middleware = function rateLimit(req, res, next) {
|
|
310
|
-
if (_shouldSkip(req)) return next();
|
|
311
|
-
var k = keyFn(req);
|
|
312
|
-
if (scope === "per-route") k = (req.method || "GET") + ":" + (req.pathname || req.url || "/") + "|" + k;
|
|
313
|
-
|
|
314
|
-
function _handle(verdict) {
|
|
315
|
-
if (emitHeaders && typeof res.setHeader === "function") {
|
|
316
|
-
res.setHeader("X-RateLimit-Limit", String(verdict.limit));
|
|
317
|
-
res.setHeader("X-RateLimit-Remaining", String(verdict.remaining));
|
|
318
|
-
}
|
|
319
|
-
if (!verdict.allowed) return _writeBlocked(req, res, k, verdict);
|
|
320
|
-
next();
|
|
321
|
-
}
|
|
322
|
-
function _onErr(e) {
|
|
323
|
-
// Fail-open on backend errors. The framework's job is to throttle
|
|
324
|
-
// attackers, not to crash the request path. Surface the error to
|
|
325
|
-
// ops via boot logger; the next request retries.
|
|
326
|
-
try {
|
|
327
|
-
logger().error("rate-limit backend take() failed: " + ((e && e.message) || String(e)));
|
|
328
|
-
} catch (_e) { /* best-effort */ }
|
|
329
|
-
next();
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
// Memory backend returns sync; cluster / custom backends return a
|
|
333
|
-
// Promise. Detect via `.then` so the hot path stays synchronous.
|
|
334
|
-
var verdictOrPromise;
|
|
335
|
-
try {
|
|
336
|
-
verdictOrPromise = backend.take(k, 1);
|
|
337
|
-
} catch (e) { return _onErr(e); }
|
|
338
|
-
|
|
339
|
-
if (verdictOrPromise && typeof verdictOrPromise.then === "function") {
|
|
340
|
-
verdictOrPromise.then(_handle, _onErr);
|
|
341
|
-
} else {
|
|
342
|
-
_handle(verdictOrPromise);
|
|
343
|
-
}
|
|
344
|
-
};
|
|
345
|
-
|
|
346
|
-
// Expose a couple of operator hooks on the middleware function.
|
|
347
|
-
middleware.reset = function (key) { return backend.reset(key); };
|
|
348
|
-
middleware.close = function () { return backend.close && backend.close(); };
|
|
349
|
-
|
|
350
|
-
return middleware;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
module.exports = {
|
|
354
|
-
create: create,
|
|
355
|
-
// Backends exported for tests + advanced operator wiring.
|
|
356
|
-
_memoryBackend: _memoryBackend,
|
|
357
|
-
_clusterBackend: _clusterBackend,
|
|
358
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Rate-limit middleware — pluggable backend, default in-memory.
|
|
4
|
+
*
|
|
5
|
+
* Per-IP by default; key extractor is configurable (per-user, per-API-key,
|
|
6
|
+
* per-route). Two built-in backends:
|
|
7
|
+
*
|
|
8
|
+
* - 'memory' (default) — token-bucket, in-process. Each key gets
|
|
9
|
+
* `burst` tokens up front; tokens refill at `refillPerSecond`;
|
|
10
|
+
* each request costs 1 token. Single-process accuracy only.
|
|
11
|
+
*
|
|
12
|
+
* - 'cluster' — fixed-window counter shared across the cluster
|
|
13
|
+
* via `_blamejs_rate_limit_counters`. Atomic INSERT...ON CONFLICT
|
|
14
|
+
* increments per key within a window and rolls over when the
|
|
15
|
+
* window advances. Multi-process / multi-node accurate.
|
|
16
|
+
*
|
|
17
|
+
* Cluster opt-in switches the algorithm shape from token-bucket
|
|
18
|
+
* to fixed-window because that's what models cleanly in SQL —
|
|
19
|
+
* the operator-facing config keys change accordingly.
|
|
20
|
+
*
|
|
21
|
+
* Operators can also pass a custom `{ take, reset }` object as the
|
|
22
|
+
* backend for Redis / Memcached / etc.
|
|
23
|
+
*
|
|
24
|
+
* Options:
|
|
25
|
+
* {
|
|
26
|
+
* keyFn: (req) → 'rate-key' (default: client IP)
|
|
27
|
+
* statusOnLimit: 429
|
|
28
|
+
* bodyOnLimit: 'Too Many Requests'
|
|
29
|
+
* header: true // set X-RateLimit-* response headers
|
|
30
|
+
* skipPaths: [] // string-prefix or regex matchers
|
|
31
|
+
* scope: 'global' | 'per-route' (default 'global')
|
|
32
|
+
* backend: 'memory' (default) | 'cluster' | { take, reset, gc? }
|
|
33
|
+
*
|
|
34
|
+
* // Memory-backend tuning (token bucket):
|
|
35
|
+
* burst: 60 // initial token bucket size
|
|
36
|
+
* refillPerSecond: 10 // sustained throughput
|
|
37
|
+
*
|
|
38
|
+
* // Cluster-backend tuning (fixed window):
|
|
39
|
+
* limit: 60 // max requests per window
|
|
40
|
+
* windowMs: C.TIME.minutes(1) // window duration
|
|
41
|
+
* pruneIntervalMs: C.TIME.minutes(5) // how often the leader prunes expired rows
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* Audit: every limit hit emits system.ratelimit.block with the key + path.
|
|
45
|
+
*/
|
|
46
|
+
var C = require("../constants");
|
|
47
|
+
var lazyRequire = require("../lazy-require");
|
|
48
|
+
var requestHelpers = require("../request-helpers");
|
|
49
|
+
var safeAsync = require("../safe-async");
|
|
50
|
+
var validateOpts = require("../validate-opts");
|
|
51
|
+
var clusterStorage = require("../cluster-storage");
|
|
52
|
+
|
|
53
|
+
var audit = lazyRequire(function () { return require("../audit"); });
|
|
54
|
+
var logger = lazyRequire(function () { return require("../log").boot("rate-limit"); });
|
|
55
|
+
|
|
56
|
+
// `_clientIp` defers to `requestHelpers.clientIp`, threading the
|
|
57
|
+
// per-middleware `trustProxy` opt. Default refuses forwarded headers
|
|
58
|
+
// (returning the socket address only) — operators behind a sanitizing
|
|
59
|
+
// reverse proxy opt in via `trustProxy: true` (or a hop count).
|
|
60
|
+
function _clientIpFor(trustProxy) {
|
|
61
|
+
return function (req) {
|
|
62
|
+
var ip = requestHelpers.clientIp(req, { trustProxy: trustProxy });
|
|
63
|
+
return ip || "unknown";
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Reject NaN / Infinity / negative / non-positive / non-number at create
|
|
68
|
+
// time so a misconfigured rate-limit can't silently degrade to "no
|
|
69
|
+
// limit" or produce divide-by-zero verdicts at request time.
|
|
70
|
+
function _requirePositiveNumber(name, value) {
|
|
71
|
+
if (typeof value !== "number" || !isFinite(value) || value <= 0) {
|
|
72
|
+
throw new Error("middleware.rateLimit: " + name + " must be a positive finite number, got " +
|
|
73
|
+
JSON.stringify(value));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ---- Memory backend (token bucket) ----
|
|
78
|
+
|
|
79
|
+
function _memoryBackend(opts) {
|
|
80
|
+
// Default 1-per-second tokens fully refilled in 1 minute = 60 tokens.
|
|
81
|
+
var burst = opts.burst != null ? opts.burst : C.TIME.minutes(1) / C.TIME.seconds(1);
|
|
82
|
+
var refillPerSecond = opts.refillPerSecond != null ? opts.refillPerSecond : 10;
|
|
83
|
+
_requirePositiveNumber("burst", burst);
|
|
84
|
+
_requirePositiveNumber("refillPerSecond", refillPerSecond);
|
|
85
|
+
var buckets = new Map();
|
|
86
|
+
|
|
87
|
+
// Periodic GC of stale buckets so the map doesn't grow unbounded.
|
|
88
|
+
var gcInterval = safeAsync.repeating(function () {
|
|
89
|
+
var cutoff = Date.now() - C.TIME.hours(1);
|
|
90
|
+
for (var k of buckets.keys()) {
|
|
91
|
+
if (buckets.get(k).lastRefillAt < cutoff) buckets.delete(k);
|
|
92
|
+
}
|
|
93
|
+
}, C.TIME.minutes(5), { name: "rate-limit-gc" });
|
|
94
|
+
|
|
95
|
+
// Memory backend returns verdicts SYNCHRONOUSLY — no awaitable.
|
|
96
|
+
// The middleware checks `.then` to decide whether to chain. Keeping
|
|
97
|
+
// the hot path synchronous means single-process apps pay no
|
|
98
|
+
// microtask cost per request.
|
|
99
|
+
function take(key, _cost) {
|
|
100
|
+
var now = Date.now();
|
|
101
|
+
var b = buckets.get(key);
|
|
102
|
+
if (!b) {
|
|
103
|
+
b = { tokens: burst, lastRefillAt: now };
|
|
104
|
+
buckets.set(key, b);
|
|
105
|
+
} else {
|
|
106
|
+
var elapsed = (now - b.lastRefillAt) / C.TIME.seconds(1);
|
|
107
|
+
b.tokens = Math.min(burst, b.tokens + elapsed * refillPerSecond);
|
|
108
|
+
b.lastRefillAt = now;
|
|
109
|
+
}
|
|
110
|
+
if (b.tokens >= 1) {
|
|
111
|
+
b.tokens -= 1;
|
|
112
|
+
return {
|
|
113
|
+
allowed: true,
|
|
114
|
+
limit: burst,
|
|
115
|
+
remaining: Math.floor(b.tokens),
|
|
116
|
+
retryAfter: 0,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
var deficit = 1 - b.tokens;
|
|
120
|
+
var waitMs = Math.ceil(C.TIME.seconds(deficit / refillPerSecond));
|
|
121
|
+
return {
|
|
122
|
+
allowed: false,
|
|
123
|
+
limit: burst,
|
|
124
|
+
remaining: 0,
|
|
125
|
+
retryAfter: Math.ceil(waitMs / C.TIME.seconds(1)),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function reset(key) {
|
|
130
|
+
buckets.delete(key);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function close() {
|
|
134
|
+
try { gcInterval.stop(); } catch (_e) { /* timer already stopped */ }
|
|
135
|
+
buckets.clear();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return { take: take, reset: reset, close: close };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ---- Cluster backend (fixed-window counter, SQL-backed) ----
|
|
142
|
+
|
|
143
|
+
function _clusterBackend(opts) {
|
|
144
|
+
// Default 60 requests per 1-minute window — derive the count from
|
|
145
|
+
// the same C.TIME source so the cadence stays in one place.
|
|
146
|
+
var limit = opts.limit != null ? opts.limit : C.TIME.minutes(1) / C.TIME.seconds(1);
|
|
147
|
+
var windowMs = opts.windowMs != null ? opts.windowMs : C.TIME.minutes(1);
|
|
148
|
+
var pruneIntervalMs = opts.pruneIntervalMs != null
|
|
149
|
+
? opts.pruneIntervalMs : C.TIME.minutes(5);
|
|
150
|
+
_requirePositiveNumber("limit", limit);
|
|
151
|
+
_requirePositiveNumber("windowMs", windowMs);
|
|
152
|
+
_requirePositiveNumber("pruneIntervalMs", pruneIntervalMs);
|
|
153
|
+
var lastPruneAt = 0;
|
|
154
|
+
|
|
155
|
+
// Best-effort prune of expired window rows. Rate-limited at the
|
|
156
|
+
// call site so we don't hammer the DB on every request.
|
|
157
|
+
function _maybePrune() {
|
|
158
|
+
var now = Date.now();
|
|
159
|
+
if (now - lastPruneAt < pruneIntervalMs) return;
|
|
160
|
+
lastPruneAt = now;
|
|
161
|
+
var cutoff = now - windowMs;
|
|
162
|
+
clusterStorage.execute(
|
|
163
|
+
"DELETE FROM _blamejs_rate_limit_counters WHERE windowStart < ?",
|
|
164
|
+
[cutoff]
|
|
165
|
+
).catch(function (e) {
|
|
166
|
+
try {
|
|
167
|
+
logger().warn("rate-limit prune failed: " + ((e && e.message) || String(e)));
|
|
168
|
+
} catch (_e) { /* logger best-effort */ }
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function take(key, _cost) {
|
|
173
|
+
var now = Date.now();
|
|
174
|
+
var windowStart = Math.floor(now / windowMs) * windowMs;
|
|
175
|
+
|
|
176
|
+
// Atomic increment: a fresh window resets count to 1; an existing
|
|
177
|
+
// row in the same window gets count + 1. Postgres + SQLite both
|
|
178
|
+
// support ON CONFLICT...DO UPDATE...RETURNING.
|
|
179
|
+
var result = await clusterStorage.execute(
|
|
180
|
+
"INSERT INTO _blamejs_rate_limit_counters (key, windowStart, count) " +
|
|
181
|
+
"VALUES (?, ?, 1) " +
|
|
182
|
+
"ON CONFLICT (key) DO UPDATE SET " +
|
|
183
|
+
" count = CASE " +
|
|
184
|
+
" WHEN excluded.windowStart > _blamejs_rate_limit_counters.windowStart " +
|
|
185
|
+
" THEN 1 " +
|
|
186
|
+
" ELSE _blamejs_rate_limit_counters.count + 1 " +
|
|
187
|
+
" END, " +
|
|
188
|
+
" windowStart = CASE " +
|
|
189
|
+
" WHEN excluded.windowStart > _blamejs_rate_limit_counters.windowStart " +
|
|
190
|
+
" THEN excluded.windowStart " +
|
|
191
|
+
" ELSE _blamejs_rate_limit_counters.windowStart " +
|
|
192
|
+
" END " +
|
|
193
|
+
"RETURNING count, windowStart",
|
|
194
|
+
[key, windowStart]
|
|
195
|
+
);
|
|
196
|
+
var row = result.rows && result.rows[0];
|
|
197
|
+
var count = row ? row.count : 1;
|
|
198
|
+
var rowWindow = row ? row.windowStart : windowStart;
|
|
199
|
+
|
|
200
|
+
_maybePrune();
|
|
201
|
+
|
|
202
|
+
if (count <= limit) {
|
|
203
|
+
return {
|
|
204
|
+
allowed: true,
|
|
205
|
+
limit: limit,
|
|
206
|
+
remaining: Math.max(0, limit - count),
|
|
207
|
+
retryAfter: 0,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
var retryMs = (rowWindow + windowMs) - now;
|
|
211
|
+
return {
|
|
212
|
+
allowed: false,
|
|
213
|
+
limit: limit,
|
|
214
|
+
remaining: 0,
|
|
215
|
+
retryAfter: Math.max(1, Math.ceil(retryMs / C.TIME.seconds(1))),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async function reset(key) {
|
|
220
|
+
await clusterStorage.execute(
|
|
221
|
+
"DELETE FROM _blamejs_rate_limit_counters WHERE key = ?",
|
|
222
|
+
[key]
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function close() { /* no resources to release */ }
|
|
227
|
+
|
|
228
|
+
return { take: take, reset: reset, close: close };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// ---- Backend resolution ----
|
|
232
|
+
|
|
233
|
+
function _resolveBackend(opts) {
|
|
234
|
+
var requested = opts.backend;
|
|
235
|
+
if (requested && typeof requested === "object" && typeof requested.take === "function") {
|
|
236
|
+
return requested; // operator-supplied custom backend
|
|
237
|
+
}
|
|
238
|
+
if (requested === "cluster") return _clusterBackend(opts);
|
|
239
|
+
if (!requested || requested === "memory") return _memoryBackend(opts);
|
|
240
|
+
throw new Error("rate-limit: unknown backend '" + requested +
|
|
241
|
+
"' (must be 'memory', 'cluster', or { take, reset })");
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function create(opts) {
|
|
245
|
+
opts = opts || {};
|
|
246
|
+
validateOpts(opts, [
|
|
247
|
+
"keyFn", "statusOnLimit", "bodyOnLimit", "header", "skipPaths", "scope",
|
|
248
|
+
"backend", "trustProxy",
|
|
249
|
+
// memory backend
|
|
250
|
+
"burst", "refillPerSecond",
|
|
251
|
+
// cluster backend
|
|
252
|
+
"limit", "windowMs", "pruneIntervalMs",
|
|
253
|
+
], "middleware.rateLimit");
|
|
254
|
+
var trustProxy = opts.trustProxy === true || typeof opts.trustProxy === "number"
|
|
255
|
+
? opts.trustProxy : false;
|
|
256
|
+
var _clientIp = _clientIpFor(trustProxy);
|
|
257
|
+
var keyFn = opts.keyFn || _clientIp;
|
|
258
|
+
var statusOnLimit = opts.statusOnLimit || 429;
|
|
259
|
+
var bodyOnLimit = opts.bodyOnLimit !== undefined ? opts.bodyOnLimit : "Too Many Requests";
|
|
260
|
+
var emitHeaders = opts.header !== false;
|
|
261
|
+
var skipPaths = opts.skipPaths || [];
|
|
262
|
+
// Throw at create(): each entry must be a string prefix or a RegExp.
|
|
263
|
+
// Anything else would crash _shouldSkip with TypeError on the first request.
|
|
264
|
+
for (var sp = 0; sp < skipPaths.length; sp++) {
|
|
265
|
+
if (typeof skipPaths[sp] !== "string" && !(skipPaths[sp] instanceof RegExp)) {
|
|
266
|
+
throw new Error("middleware.rateLimit: skipPaths[" + sp +
|
|
267
|
+
"] must be a string prefix or RegExp, got " + typeof skipPaths[sp]);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
var scope = opts.scope || "global";
|
|
271
|
+
|
|
272
|
+
var backend = _resolveBackend(opts);
|
|
273
|
+
|
|
274
|
+
function _shouldSkip(req) {
|
|
275
|
+
var path = req.pathname || req.url || "/";
|
|
276
|
+
for (var i = 0; i < skipPaths.length; i++) {
|
|
277
|
+
if (typeof skipPaths[i] === "string" ? path.indexOf(skipPaths[i]) === 0 : skipPaths[i].test(path)) {
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function _writeBlocked(req, res, k, verdict) {
|
|
285
|
+
if (emitHeaders && typeof res.setHeader === "function") {
|
|
286
|
+
res.setHeader("X-RateLimit-Limit", String(verdict.limit));
|
|
287
|
+
res.setHeader("X-RateLimit-Remaining", String(verdict.remaining));
|
|
288
|
+
if (verdict.retryAfter > 0) res.setHeader("Retry-After", String(verdict.retryAfter));
|
|
289
|
+
}
|
|
290
|
+
try {
|
|
291
|
+
// Override `ip` with the x-forwarded-for-aware client IP so the
|
|
292
|
+
// audit event carries the proxied origin even when extractActorContext
|
|
293
|
+
// would have read the socket address.
|
|
294
|
+
audit().emit({
|
|
295
|
+
actor: requestHelpers.extractActorContext(req, { ip: _clientIp(req) }),
|
|
296
|
+
action: "system.ratelimit.block",
|
|
297
|
+
outcome: "denied",
|
|
298
|
+
reason: "rate limit exceeded",
|
|
299
|
+
metadata: { key: k, method: req.method, path: req.pathname || req.url, retryAfter: verdict.retryAfter },
|
|
300
|
+
requestId: req.requestId,
|
|
301
|
+
});
|
|
302
|
+
} catch (_e) { /* audit best-effort */ }
|
|
303
|
+
if (typeof res.writeHead === "function") {
|
|
304
|
+
res.writeHead(statusOnLimit, { "Content-Type": "text/plain" });
|
|
305
|
+
res.end(bodyOnLimit);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
var middleware = function rateLimit(req, res, next) {
|
|
310
|
+
if (_shouldSkip(req)) return next();
|
|
311
|
+
var k = keyFn(req);
|
|
312
|
+
if (scope === "per-route") k = (req.method || "GET") + ":" + (req.pathname || req.url || "/") + "|" + k;
|
|
313
|
+
|
|
314
|
+
function _handle(verdict) {
|
|
315
|
+
if (emitHeaders && typeof res.setHeader === "function") {
|
|
316
|
+
res.setHeader("X-RateLimit-Limit", String(verdict.limit));
|
|
317
|
+
res.setHeader("X-RateLimit-Remaining", String(verdict.remaining));
|
|
318
|
+
}
|
|
319
|
+
if (!verdict.allowed) return _writeBlocked(req, res, k, verdict);
|
|
320
|
+
next();
|
|
321
|
+
}
|
|
322
|
+
function _onErr(e) {
|
|
323
|
+
// Fail-open on backend errors. The framework's job is to throttle
|
|
324
|
+
// attackers, not to crash the request path. Surface the error to
|
|
325
|
+
// ops via boot logger; the next request retries.
|
|
326
|
+
try {
|
|
327
|
+
logger().error("rate-limit backend take() failed: " + ((e && e.message) || String(e)));
|
|
328
|
+
} catch (_e) { /* best-effort */ }
|
|
329
|
+
next();
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Memory backend returns sync; cluster / custom backends return a
|
|
333
|
+
// Promise. Detect via `.then` so the hot path stays synchronous.
|
|
334
|
+
var verdictOrPromise;
|
|
335
|
+
try {
|
|
336
|
+
verdictOrPromise = backend.take(k, 1);
|
|
337
|
+
} catch (e) { return _onErr(e); }
|
|
338
|
+
|
|
339
|
+
if (verdictOrPromise && typeof verdictOrPromise.then === "function") {
|
|
340
|
+
verdictOrPromise.then(_handle, _onErr);
|
|
341
|
+
} else {
|
|
342
|
+
_handle(verdictOrPromise);
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
// Expose a couple of operator hooks on the middleware function.
|
|
347
|
+
middleware.reset = function (key) { return backend.reset(key); };
|
|
348
|
+
middleware.close = function () { return backend.close && backend.close(); };
|
|
349
|
+
|
|
350
|
+
return middleware;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
module.exports = {
|
|
354
|
+
create: create,
|
|
355
|
+
// Backends exported for tests + advanced operator wiring.
|
|
356
|
+
_memoryBackend: _memoryBackend,
|
|
357
|
+
_clusterBackend: _clusterBackend,
|
|
358
|
+
};
|