Skip to content

Commit

Permalink
Merge pull request #116 from mmalecki/fix-special-properties
Browse files Browse the repository at this point in the history
Prefix keys with a `'+'` when using object as a hash
  • Loading branch information
Eran Hammer committed Jan 22, 2015
2 parents 9078ebc + 6253ced commit 51ab78a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ internals.Policy.prototype.get = function (key, callback) { // key: string o
// Check if request is already pending

var id = (key && typeof key === 'object') ? key.id : key;
if (this._pendings[id]) {
this._pendings[id].push(callback);
var pendingsId = '+' + id;
if (this._pendings[pendingsId]) {
this._pendings[pendingsId].push(callback);
return;
}

this._pendings[id] = [callback];
this._pendings[pendingsId] = [callback];

// Lookup in cache

Expand Down Expand Up @@ -169,6 +170,7 @@ internals.Policy.prototype._generate = function (id, key, cached, report, callba

internals.Policy.prototype._finalize = function (id, err, value, cached, report) {

id = '+' + id;
var pendings = this._pendings[id];
delete this._pendings[id];

Expand Down
23 changes: 23 additions & 0 deletions test/policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ describe('Policy', function () {
});
});

it('works with special property names', function (done) {

var client = new Catbox.Client(Import);
var policy = new Catbox.Policy({ expiresIn: 1000 }, client, 'test');

client.start(function (err) {

expect(err).to.not.exist();

policy.set('__proto__', '123', null, function (err) {

expect(err).to.not.exist();

policy.get('__proto__', function (err, value, cached, report) {

expect(err).to.not.exist();
expect(value).to.equal('123');
done();
});
});
});
});

it('finds nothing when using empty policy rules', function (done) {

var client = new Catbox.Client(Import);
Expand Down

0 comments on commit 51ab78a

Please sign in to comment.