Skip to content

Commit 116247d

Browse files
authored
Merge pull request #29 from dial-once/bugfix/fix-setting-undefined
Fix setting undefined so it returns an error or allow if allowed by isCacheableValue and add tests.
2 parents e958989 + ce0b29b commit 116247d

File tree

3 files changed

+147
-84
lines changed

3 files changed

+147
-84
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
coverage
33
docs
4+
.idea/

index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function redisStore(args) {
5757
* Helper to handle callback and release the connection
5858
* @private
5959
* @param {Object} conn - The Redis connection
60-
* @param {Function} [cb] - A callback that returns a potential error and the resoibse
60+
* @param {Function} [cb] - A callback that returns a potential error and the result
6161
* @param {Object} [opts] - The options (optional)
6262
*/
6363
function handleResponse(conn, cb, opts) {
@@ -71,7 +71,15 @@ function redisStore(args) {
7171
}
7272

7373
if (opts.parse) {
74-
result = JSON.parse(result);
74+
75+
try {
76+
// allow undefined only if allowed by isCacheableValue
77+
if(! ( (result === undefined || result === "undefined") && typeof args.isCacheableValue === 'function' && args.isCacheableValue(result))) {
78+
result = JSON.parse(result);
79+
}
80+
} catch (e) {
81+
return cb && cb(e);
82+
}
7583
}
7684

7785
if (cb) {
@@ -113,10 +121,12 @@ function redisStore(args) {
113121
* @param {Function} [cb] - A callback that returns a potential error, otherwise null
114122
*/
115123
self.set = function(key, value, options, cb) {
124+
116125
if (typeof options === 'function') {
117126
cb = options;
118127
options = {};
119128
}
129+
120130
options = options || {};
121131

122132
var ttl = (options.ttl || options.ttl === 0) ? options.ttl : redisOptions.ttl;
@@ -126,7 +136,6 @@ function redisStore(args) {
126136
return cb && cb(err);
127137
}
128138
var val = JSON.stringify(value);
129-
130139
if (ttl) {
131140
conn.setex(key, ttl, val, handleResponse(conn, cb));
132141
} else {

0 commit comments

Comments
 (0)