Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix keys with a '+' when using object as a hash #116

Merged
merged 1 commit into from Jan 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/policy.js
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
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