Skip to content

Commit

Permalink
Merge pull request express-rate-limit#8 from Mindtraveller/master
Browse files Browse the repository at this point in the history
Added support for decrement method (see https://github.com/nfriedly/e…
  • Loading branch information
wyattjoh authored Jan 13, 2018
2 parents 4f5779b + 8ce18b1 commit cf156be
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea
55 changes: 39 additions & 16 deletions lib/redis-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ var RedisStore = function(options) {
// create the client if one isn't provided
options.client = options.client || redis.createClient();

var setExpire = function(replies, rdskey) {
// if this is new or has no expiry
if (replies[0] === 1 || replies[1] === -1) {
// then expire it after the timeout
options.client.pexpire(rdskey, expiryMs);
}
};

var processReplies = function(replies) {
// in ioredis, every reply consists of an array [err, value].
// We don't need the error here, and if we aren't dealing with an array,
// nothing is changed.
return replies.map(function(val) {
if (Array.isArray(val) && val.length >= 2) {
return val[1];
}

return val;
});
};

this.incr = function(key, cb) {
var rdskey = options.prefix + key;

Expand All @@ -24,27 +45,29 @@ var RedisStore = function(options) {
return cb(err);
}

// in ioredis, every reply consists of an array [err, value].
// We don't need the error here, and if we aren't dealing with an array,
// nothing is changed.
replies = replies.map(function(val) {
if (Array.isArray(val) && val.length >= 2) {
return val[1];
}

return val;
});

// if this is new or has no expiry
if (replies[0] === 1 || replies[1] === -1) {
// then expire it after the timeout
options.client.pexpire(rdskey, expiryMs);
}
replies = processReplies(replies);
setExpire(replies, rdskey);

cb(null, replies[0]);
});
};

this.decrement = function(key) {
var rdskey = options.prefix + key;

options.client.multi()
.decr(rdskey)
.pttl(rdskey)
.exec(function(err, replies) {
if (err) {
return;
}

replies = processReplies(replies);
setExpire(replies, rdskey);
});
};

this.resetKey = function(key) {
var rdskey = options.prefix + key;

Expand Down
36 changes: 36 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ describe("rate-limit-redis node module", function() {
return this;
};

this.decr = function(key) {
opts.push(function() {
if (keys[key]) {
keys[key].value--;
} else {
keys[key] = { value: 0 };
}

return keys[key].value;
});

return this;
};

this.pttl = function(key) {
opts.push(function() {
if (keys[key] && keys[key].ttl) {
Expand Down Expand Up @@ -115,6 +129,28 @@ describe("rate-limit-redis node module", function() {
});
});

it("decrements the key for the store each decrement", function(done) {
var store = new RedisStore({
client: new MockRedisClient()
});
var key = "test-store-decrement";

store.incr(key, function() {
store.decrement(key);
store.incr(key, function(err, value) {
if (err) {
done(err);
} else {
if (value === 1) {
done();
} else {
done(new Error("decrement did not decrement the store"));
}
}
});
});
});

it("resets the key for the store when used with resetKey", function(done) {
var store = new RedisStore({
client: new MockRedisClient()
Expand Down

0 comments on commit cf156be

Please sign in to comment.