Skip to content

Commit

Permalink
Guards around setting max/maxAge to non-numbers
Browse files Browse the repository at this point in the history
Close #136
  • Loading branch information
isaacs committed Nov 21, 2018
1 parent 989d730 commit 1a4be6b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var LRU = require("lru-cache")
, length: function (n, key) { return n * 2 + key.length }
, dispose: function (key, n) { n.close() }
, maxAge: 1000 * 60 * 60 }
, cache = LRU(options)
, otherCache = LRU(50) // sets just the max size
, cache = new LRU(options)
, otherCache = new LRU(50) // sets just the max size

cache.set("key", "value")
cache.get("key") // "value"
Expand Down Expand Up @@ -49,10 +49,13 @@ away.
* `max` The maximum size of the cache, checked by applying the length
function to all values in the cache. Not setting this is kind of
silly, since that's the whole purpose of this lib, but it defaults
to `Infinity`.
to `Infinity`. Setting it to a non-number or negative number will
throw a `TypeError`. Setting it to 0 makes it be `Infinity`.
* `maxAge` Maximum age in ms. Items are not pro-actively pruned out
as they age, but if you try to get an item that is too old, it'll
drop it and return undefined instead of giving it to you.
Setting this to a negative value will make everything seem old!
Setting it to a non-number will throw a `TypeError`.
* `length` Function that is used to calculate the length of stored
items. If you're storing strings or buffers, then you probably want
to do something like `function(n, key){return n.length}`. The default is
Expand Down
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ class LRUCache {
if (!options)
options = {}

const max = this[MAX] = options.max
if (options.max && (typeof options.max !== 'number' || options.max < 0))
throw new TypeError('max must be a non-negative number')
// Kind of weird to have a default max of Infinity, but oh well.
if (!max ||
!(typeof max === 'number') ||
max <= 0)
this[MAX] = Infinity
const max = this[MAX] = options.max || Infinity

const lc = options.length || naiveLength
this[LENGTH_CALCULATOR] = (typeof lc !== 'function') ? naiveLength : lc
this[ALLOW_STALE] = options.stale || false
if (options.maxAge && typeof options.maxAge !== 'number')
throw new TypeError('maxAge must be a number')
this[MAX_AGE] = options.maxAge || 0
this[DISPOSE] = options.dispose
this[NO_DISPOSE_ON_SET] = options.noDisposeOnSet || false
Expand All @@ -49,10 +49,10 @@ class LRUCache {

// resize the cache when the max changes.
set max (mL) {
if (!mL || !(typeof mL === 'number') || mL <= 0)
mL = Infinity
if (typeof mL !== 'number' || mL < 0)
throw new TypeError('max must be a non-negative number')

this[MAX] = mL
this[MAX] = mL || Infinity
trim(this)
}
get max () {
Expand All @@ -67,8 +67,8 @@ class LRUCache {
}

set maxAge (mA) {
if (!mA || !(typeof mA === 'number') || mA < 0)
mA = 0
if (typeof mA !== 'number')
throw new TypeError('maxAge must be a non-negative number')

this[MAX_AGE] = mA
trim(this)
Expand Down
15 changes: 9 additions & 6 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test('max', function (t) {
}

// now remove the max restriction, and try again.
cache.max = 'hello'
cache.max = 0
for (i = 0; i < 100; i++) cache.set(i, i)
t.equal(cache.length, 100)
for (i = 0; i < 100; i++) {
Expand Down Expand Up @@ -520,11 +520,6 @@ test('maxAge on list, cleared in forEach', function (t) {
// hacky. make it seem older.
l.dumpLru().head.value.now = Date.now() - 100000

// setting maxAge to invalid values does nothing.
t.equal(l.maxAge, 0)
l.maxAge = -100
t.equal(l.maxAge, 0)
l.maxAge = {}
t.equal(l.maxAge, 0)

l.maxAge = 1
Expand All @@ -539,3 +534,11 @@ test('maxAge on list, cleared in forEach', function (t) {

t.end()
})

test('bad max/maxAge options', t => {
t.throws(() => new LRU({ maxAge: true }), 'maxAge must be a number')
t.throws(() => { new LRU().maxAge = 'foo' }, 'maxAge must be a number')
t.throws(() => new LRU({ max: true }), 'max must be a non-negative number')
t.throws(() => { new LRU().max = 'foo' }, 'max must be a non-negative number')
t.end()
})

0 comments on commit 1a4be6b

Please sign in to comment.