Skip to content
This repository was archived by the owner on Nov 19, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ An optional function that is called when the call to the Redis client errors. It
##### `timer`
An optional function that will be called upon every rate limit request. The argument will be the time in milliseconds to perform the rate limit process.

##### `noHeaders`
An optional boolean to no set rate limit headers on the response.

## Managing Routes
Settings for individual routes can be set while registering a route.

Expand Down Expand Up @@ -135,3 +138,5 @@ Rate-limiting information for each request is attached to the response header wi
`x-rate-limit-remaining:` remaining number of requests allows within current window

`x-rate-limit-reset:` time when rate-limiter will reset (UTC seconds-since-epoch)

You can disable those by settings `noHeaders: true` on the plugin options or on per-route basis.
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ exports.register = (server, options) => {
});

server.ext('onPreResponse', async (request, h) => {
const routeSettings = request.route.settings.plugins.rateLimit;
const rate = request.plugins['hapi-rate-limiter'] ? request.plugins['hapi-rate-limiter'].rate : null;

if (rate) {
if (rate && !(routeSettings.noHeaders || options.noHeaders)) {
// If an error was thrown, set headers on the error-response.
const headers = request.response.output ? request.response.output.headers : request.response.headers;

Expand Down
29 changes: 29 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ describe('plugin', async () => {
}
}
},
{
method: 'POST',
path: '/default_no_headers',
config: {
plugins: {
rateLimit: {
enabled: true,
noHeaders: true
}
},
handler: (request) => {
return { rate: request.plugins['hapi-rate-limiter'].rate };
}
}
},
{
method: 'POST',
path: '/short_limit_test',
Expand Down Expand Up @@ -433,6 +448,20 @@ describe('plugin', async () => {
});
});

it('allows to not returns the headers', () => {
return server.inject({
method: 'POST',
url: '/default_no_headers',
auth: {
credentials: { api_key: '123' },
strategy: 'basic'
}
})
.then((response) => {
expect(response.headers).to.not.contain.all.keys(['x-rate-limit-reset', 'x-rate-limit-limit', 'x-rate-limit-remaining']);
});
});

it('ignores requests with invalid auth credentials', () => {
return server.inject({
method: 'POST',
Expand Down