Skip to content

Commit

Permalink
fix: suppress max zero warning when custom validation object is passe…
Browse files Browse the repository at this point in the history
…d in

Fixes #46
  • Loading branch information
nfriedly committed Nov 16, 2023
1 parent eb8734c commit 8b74e31
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# express-slow-down changelog

## v2.0.1

### Fixed

- express-rate-limit's `WRN_ERL_MAX_ZERO` warning is not correctly suppressed
even when supplying a custom validations object in the config.

## v2.0.0

express-slow-down v2 is built on top of express-rate-limit v7.
Expand Down
8 changes: 5 additions & 3 deletions source/slow-down.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,16 @@ export const slowDown = (
},
maxDelayMs: Number.POSITIVE_INFINITY,
requestPropertyName: 'slowDown',

// Next the user's options are pulled in, overriding defaults from above
...notUndefinedOptions,

// This is a combination of the user's validate settings and our own overrides
validate: {
...validate,
limit: false, // We know the behavor of limit=0 changed - we depend on the new behavior!
},

// The following options are passed directly to `express-rate-limit`.
...notUndefinedOptions,

// These settings cannot be overriden.
limit: 0, // We want the handler to run on every request.
// Disable the headers, we don't want to send them.
Expand Down
13 changes: 13 additions & 0 deletions test/library/options-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Tests the parsing/handling of options passed in by the user

import slowDown from '../../source/index.js'
import { expectNoDelay } from '../helpers/requests.js'

describe('options', () => {
beforeEach(() => {
Expand Down Expand Up @@ -51,4 +52,16 @@ describe('options', () => {
expect(console.warn).not.toBeCalled()
expect(console.error).not.toBeCalled()
})

it('should not warn about max being zero when validate.delayMs is false', async () => {
jest.spyOn(global, 'setTimeout')
const instance = slowDown({
delayAfter: 1,
delayMs: 100,
validate: { delayMs: false },
})
await expectNoDelay(instance)
expect(console.warn).not.toBeCalled()
expect(console.error).not.toBeCalled()
})
})

0 comments on commit 8b74e31

Please sign in to comment.