@cortec/polka
Advanced tools
Comparing version 1.3.2 to 1.4.0
@@ -10,2 +10,3 @@ "use strict"; | ||
const polka_1 = __importDefault(require("polka")); | ||
const rate_limiter_flexible_1 = require("rate-limiter-flexible"); | ||
const zod_1 = require("zod"); | ||
@@ -38,2 +39,3 @@ const zod_validation_error_1 = require("zod-validation-error"); | ||
const logger = ctx.provide('logger'); | ||
const redis = ctx.provide('redis'); | ||
const polkaConfig = config?.get(this.name); | ||
@@ -76,5 +78,21 @@ const app = (0, polka_1.default)({ | ||
const missing = controller.modules?.filter((module) => ctx.has(module)); | ||
let rateLimit = null; | ||
if (missing?.length) { | ||
throw new Error(`The following modules are missing: ${missing.join(', ')}`); | ||
} | ||
// Rate limiting | ||
if (controller.rateLimit) { | ||
const client = redis?.cache(controller.rateLimit.cache); | ||
if (!client) { | ||
throw new Error(`Cache '${controller.rateLimit.cache}' is not configured for rate limiting`); | ||
} | ||
rateLimit = new rate_limiter_flexible_1.RateLimiterRedis({ | ||
storeClient: client, | ||
duration: controller.rateLimit.duration, | ||
points: controller.rateLimit.limit, | ||
execEvenly: false, | ||
blockDuration: 0, | ||
keyPrefix: controller.rateLimit.keyPrefix ?? `rlflx:${path}:${method}`, | ||
}); | ||
} | ||
const METHOD = method.toUpperCase(); | ||
@@ -116,3 +134,13 @@ /** | ||
const handler = async (req, res, next) => { | ||
const reqCtx = { | ||
session: req.session, | ||
...controller.ctx?.call(ctx, req), | ||
}; | ||
try { | ||
if (rateLimit && controller.rateLimit) { | ||
const rateLimitRes = await rateLimit.consume(controller.rateLimit.count.call(ctx, req, reqCtx)); | ||
if (!rateLimitRes) { | ||
throw new ResponseError_1.default(HttpStatusCodes_1.default.TOO_MANY_REQUESTS, 'Too many requests', {}); | ||
} | ||
} | ||
if (controller.schema) { | ||
@@ -134,6 +162,2 @@ try { | ||
} | ||
const reqCtx = { | ||
session: req.session, | ||
...controller.ctx?.call(ctx, req), | ||
}; | ||
const response = await (nr | ||
@@ -140,0 +164,0 @@ ? nr.api.startSegment('controller', true, () => controller.onRequest.call(ctx, req, reqCtx)) |
{ | ||
"name": "@cortec/polka", | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"description": "<description>", | ||
@@ -27,3 +27,2 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@cortec/types": "^1.6.0", | ||
"@total-typescript/ts-reset": "^0.4.2", | ||
@@ -33,2 +32,3 @@ "body-parser": "^1.20.2", | ||
"polka": "^0.5.2", | ||
"rate-limiter-flexible": "^2.4.1", | ||
"zod": "^3.21.4", | ||
@@ -38,6 +38,8 @@ "zod-validation-error": "^1.1.0" | ||
"devDependencies": { | ||
"@cortec/config": "^1.4.2", | ||
"@cortec/logger": "^1.1.1", | ||
"@cortec/newrelic": "^1.4.2", | ||
"@cortec/sentry": "^1.4.2", | ||
"@cortec/config": "^1.4.3", | ||
"@cortec/logger": "^1.1.2", | ||
"@cortec/newrelic": "^1.4.3", | ||
"@cortec/redis": "^1.5.0", | ||
"@cortec/sentry": "^1.4.3", | ||
"@cortec/types": "^1.7.0", | ||
"@types/polka": "^0.5.4" | ||
@@ -49,3 +51,3 @@ }, | ||
}, | ||
"gitHead": "5611228c3ea48c1bcf27ee8c64423c85cdf50768" | ||
"gitHead": "608a6febeea6c1a7cf4e9984debb51a05832a407" | ||
} |
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
49478
954
0
7
+ Addedrate-limiter-flexible@^2.4.1
+ Addedrate-limiter-flexible@2.4.2(transitive)
- Removed@cortec/types@^1.6.0
- Removed@cortec/types@1.15.0(transitive)
- Removed@types/node@22.13.9(transitive)
- Removed@types/signale@1.4.7(transitive)
- Removedundici-types@6.20.0(transitive)