From 50afcf57de100080b0df6c365ce3e89de722663c Mon Sep 17 00:00:00 2001 From: Jeremy Greer Date: Tue, 13 Aug 2019 17:03:13 -0700 Subject: [PATCH] Throw for large timeouts --- index.js | 3 +++ test.js | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/index.js b/index.js index e62782b..f538345 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ function Cache () { var _missCount = 0; var _size = 0; var _debug = false; + var MAX_TIMEOUT = 2147483647; this.put = function(key, value, time, timeoutCallback) { if (_debug) { @@ -16,6 +17,8 @@ function Cache () { throw new Error('Cache timeout must be a positive number'); } else if (typeof timeoutCallback !== 'undefined' && typeof timeoutCallback !== 'function') { throw new Error('Cache timeout callback must be a function'); + } else if (time > MAX_TIMEOUT) { + throw new Error('Cache timeout out of range'); } var oldRecord = _cache[key]; diff --git a/test.js b/test.js index af0ee5a..c5681ce 100644 --- a/test.js +++ b/test.js @@ -70,6 +70,12 @@ describe('node-cache', function() { }).to.throw(); }); + it('should throw an error given a timeout that is too large', function() { + expect(function() { + cache.put('key', 'value', 2147483648); + }).to.throw(); + }); + it('should throw an error given a non-function timeout callback', function() { expect(function() { cache.put('key', 'value', 100, 'foo');