From 8cf5a7240aaebf063858fcf175800b785073b6ad Mon Sep 17 00:00:00 2001 From: Burak Keskin Date: Mon, 31 Aug 2026 14:13:46 +0300 Subject: [PATCH] Honor a numeric timeout of 0 instead of using the 5000ms default. time || 5000 is falsy for zero, so timeout(0) waited five seconds. timeout("0ms") already meant zero milliseconds. --- HISTORY.md | 6 ++++++ index.js | 2 +- test/test.js | 7 +++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 65dfa63..7510f2e 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,9 @@ +unreleased +========== + + * Fix: a numeric timeout of `0` was treated as the 5000ms default + because `time || 5000` is falsy for zero + 1.9.1 / 2025-07-17 ========== diff --git a/index.js b/index.js index 339cec4..d3dc9ab 100644 --- a/index.js +++ b/index.js @@ -39,7 +39,7 @@ function timeout (time, options) { var delay = typeof time === 'string' ? ms(time) - : Number(time || 5000) + : Number(time == null ? 5000 : time) var respond = opts.respond === undefined || opts.respond === true diff --git a/test/test.js b/test/test.js index aefc6a9..36830ac 100644 --- a/test/test.js +++ b/test/test.js @@ -21,6 +21,13 @@ describe('timeout()', function () { .expect(503, /123ms/, done) }) + it('should accept a zero millisecond timeout', function (done) { + var server = createServer(0) + request(server) + .get('/') + .expect(503, /after 0ms/, done) + }) + it('should accept string timeout', function (done) { var server = createServer('45ms') request(server)