fastify-rate-limit
Advanced tools
Comparing version 4.0.1 to 4.0.2
16
index.js
@@ -77,3 +77,3 @@ 'use strict' | ||
// onRoute add the preHandler rate-limit function if needed | ||
// onRoute add the onRequest rate-limit function if needed | ||
fastify.addHook('onRoute', (routeOptions) => { | ||
@@ -115,12 +115,12 @@ if (routeOptions.config && typeof routeOptions.config.rateLimit !== 'undefined') { | ||
if (Array.isArray(routeOptions.preHandler)) { | ||
routeOptions.preHandler.push(preHandler) | ||
} else if (typeof routeOptions.preHandler === 'function') { | ||
routeOptions.preHandler = [routeOptions.preHandler, preHandler] | ||
if (Array.isArray(routeOptions.onRequest)) { | ||
routeOptions.onRequest.push(onRequest) | ||
} else if (typeof routeOptions.onRequest === 'function') { | ||
routeOptions.onRequest = [routeOptions.onRequest, onRequest] | ||
} else { | ||
routeOptions.preHandler = [preHandler] | ||
routeOptions.onRequest = [onRequest] | ||
} | ||
// PreHandler function that will be use for current endpoint been processed | ||
function preHandler (req, res, next) { | ||
// onRequest function that will be use for current endpoint been processed | ||
function onRequest (req, res, next) { | ||
// We retrieve the key from the generator. (can be the global one, or the one define in the endpoint) | ||
@@ -127,0 +127,0 @@ const key = params.keyGenerator(req) |
{ | ||
"name": "fastify-rate-limit", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"description": "A low overhead rate limiter for your routes", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -423,3 +423,3 @@ 'use strict' | ||
test('does not override the preHandler', t => { | ||
test('does not override the onRequest', t => { | ||
t.plan(5) | ||
@@ -433,4 +433,4 @@ const fastify = Fastify() | ||
fastify.get('/', { | ||
preHandler: function (req, reply, next) { | ||
t.pass('preHandler called') | ||
onRequest: function (req, reply, next) { | ||
t.pass('onRequest called') | ||
next() | ||
@@ -450,3 +450,3 @@ } | ||
test('does not override the preHandler as an array', t => { | ||
test('does not override the onRequest as an array', t => { | ||
t.plan(5) | ||
@@ -460,4 +460,4 @@ const fastify = Fastify() | ||
fastify.get('/', { | ||
preHandler: [function (req, reply, next) { | ||
t.pass('preHandler called') | ||
onRequest: [function (req, reply, next) { | ||
t.pass('onRequest called') | ||
next() | ||
@@ -608,1 +608,31 @@ }] | ||
}) | ||
test('stops fastify lifecycle after onRequest and before preValidation', t => { | ||
t.plan(6) | ||
const fastify = Fastify() | ||
fastify.register(rateLimit, { max: 1, timeWindow: 1000 }) | ||
let preValidationCallCount = 0 | ||
fastify.get('/', { | ||
preValidation: function (req, reply, next) { | ||
t.pass('preValidation called only once') | ||
preValidationCallCount++ | ||
next() | ||
} | ||
}, | ||
(req, reply) => { | ||
reply.send('hello!') | ||
}) | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.strictEqual(res.statusCode, 200) | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.strictEqual(res.statusCode, 429) | ||
t.equal(preValidationCallCount, 1) | ||
}) | ||
}) | ||
}) |
@@ -487,3 +487,3 @@ 'use strict' | ||
test('does not override the preHandler', t => { | ||
test('does not override onRequest', t => { | ||
t.plan(5) | ||
@@ -494,4 +494,4 @@ const fastify = Fastify() | ||
fastify.get('/', { | ||
preHandler: function (req, reply, next) { | ||
t.pass('preHandler called') | ||
onRequest: function (req, reply, next) { | ||
t.pass('onRequest called') | ||
next() | ||
@@ -902,1 +902,37 @@ }, | ||
}) | ||
test('stops fastify lifecycle after onRequest and before preValidation', t => { | ||
t.plan(6) | ||
const fastify = Fastify() | ||
fastify.register(rateLimit, { global: false }) | ||
let preValidationCallCount = 0 | ||
fastify.get('/', { | ||
config: { | ||
rateLimit: { | ||
max: 1, | ||
timeWindow: 1000 | ||
} | ||
}, | ||
preValidation: function (req, reply, next) { | ||
t.pass('preValidation called only once') | ||
preValidationCallCount++ | ||
next() | ||
} | ||
}, | ||
(req, reply) => { | ||
reply.send('hello!') | ||
}) | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.strictEqual(res.statusCode, 200) | ||
fastify.inject('/', (err, res) => { | ||
t.error(err) | ||
t.strictEqual(res.statusCode, 429) | ||
t.equal(preValidationCallCount, 1) | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75250
1920