Skip to content
Merged
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
132 changes: 80 additions & 52 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
"@express-rate-limit/tsconfig": "1.0.2",
"@jest/globals": "29.7.0",
"@types/express": "4.17.18",
"@types/jest": "29.5.5",
"@types/supertest": "2.0.12",
"body-parser": "1.20.6",
"del-cli": "5.1.0",
Expand All @@ -99,7 +98,7 @@
"npm-run-all": "4.1.5",
"prettier": "3.0.3",
"supertest": "6.3.3",
"ts-jest": "29.1.1",
"ts-jest": "29.4.12",
"typescript": "5.2.2",
"xo": "0.56.0"
},
Expand Down
6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ request timeout. Can be the number itself (in milliseconds) or a (sync/async)
function that accepts the Express `req` and `res` objects and then returns a
number.

Defaults to `Infinity`.
Defaults to `2147483647` (2^31 - 1 ms, which is ~24 days).

**Note**: Providing a value higher than `2,147,483,647` will throw an error due
to
[Node.js `setTimeout` limits](https://nodejs.org/api/timers.html#settimeoutcallback-delay-args:~:text=specified%2E-,When%20delay%20is%20larger%20than%202147483647%20or%20less%20than%201%20or%20NaN%2C%20the%20delay%20will%20be%20set%20to%201%2E%20Non%2Dinteger%20delays%20are%20truncated%20to%20an%20integer).

For example, for the following configuration:

Expand Down
17 changes: 16 additions & 1 deletion source/slow-down.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ const filterUndefinedOptions = (
return filteredOptions
}

/**
* The maximum value for a 32-bit signed integer, which is the maximum
* delay allowed by Node.js setTimeout.
*/
const max32BitSignedInt = 2 ** 31 - 1

// Consider exporting then extending express-rate-limit's ValidationError
class ExpressSlowDownWarning extends Error {
name: string
Expand Down Expand Up @@ -78,6 +84,15 @@ export const slowDown = (
'The limit/max option is not supported by express-slow-down, please use delayAfter instead.',
)

if (
typeof notUndefinedOptions.maxDelayMs === 'number' &&
notUndefinedOptions.maxDelayMs > max32BitSignedInt
) {
throw new Error(
`The 'maxDelayMs' option cannot be greater than ${max32BitSignedInt} due to Node.js setTimeout limits.`,
)
}

// Consolidate the validation options that have been passed by the user, and
// apply them later, along with `limit: false`.
const validate =
Expand Down Expand Up @@ -128,7 +143,7 @@ export const slowDown = (
const delayAfter = request[options.requestPropertyName!].limit
return (used - delayAfter) * 1000
},
maxDelayMs: Number.POSITIVE_INFINITY,
maxDelayMs: max32BitSignedInt,
requestPropertyName: 'slowDown',
// Disable the headers by default, but allow users to override
legacyHeaders: false,
Expand Down
Loading
Loading