[minor] Dont allow `.set()` calls to change values in readOnly stores: argv, env, and literal

master
indexzero 2011-11-20 20:00:04 -05:00
parent 1aa2f1f315
commit 78ce55602f
4 changed files with 27 additions and 9 deletions

View File

@ -62,7 +62,7 @@ var Provider = exports.Provider = function (options) {
['defaults', 'overrides'].forEach(function (type) {
Provider.prototype[type] = function (options) {
return this.add('literal', options);
}
};
});
//
@ -399,19 +399,24 @@ Provider.prototype.save = function (value, callback) {
Provider.prototype._execute = function (action, syncLength /* [arguments] */) {
var args = Array.prototype.slice.call(arguments, 2),
callback = typeof args[args.length - 1] === 'function' && args.pop(),
destructive = ['set', 'clear', 'merge'].indexOf(action) !== -1,
self = this,
response;
function runAction (name, next) {
var store = self.sources[name];
if (destructive && store.readOnly) {
return next();
}
return store[action].length > syncLength
? store[action].apply(store, args.concat(next))
: next(null, store[action].apply(store, args));
}
if (callback) {
return async.forEach(Object.keys(self.sources), runAction, function (err) {
return async.forEach(Object.keys(this.sources), runAction, function (err) {
return err ? callback(err) : callback();
});
}
@ -420,6 +425,11 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) {
Object.keys(this.sources).forEach(function (name) {
if (typeof response === 'undefined') {
var store = self.sources[name];
if (destructive && store.readOnly) {
return;
}
response = store[action].apply(store, args);
}
});

View File

@ -18,8 +18,9 @@ var util = require('util'),
var Argv = exports.Argv = function (options) {
Memory.call(this, options);
this.type = 'argv';
this.options = options || false;
this.type = 'argv';
this.readOnly = true;
this.options = options || false;
};
// Inherit from the Memory store
@ -51,9 +52,11 @@ Argv.prototype.loadArgv = function () {
return;
}
this.readOnly = false;
Object.keys(argv).forEach(function (key) {
self.set(key, argv[key]);
});
this.readOnly = true;
return this.store;
};

View File

@ -17,8 +17,9 @@ var util = require('util'),
var Env = exports.Env = function (options) {
Memory.call(this, options);
this.type = 'env';
this.options = options || [];
this.type = 'env';
this.readOnly = true;
this.options = options || [];
};
// Inherit from the Memory store
@ -40,12 +41,14 @@ Env.prototype.loadSync = function () {
Env.prototype.loadEnv = function () {
var self = this;
this.readOnly = false;
Object.keys(process.env).filter(function (key) {
return !self.options.length || self.options.indexOf(key) !== -1;
}).forEach(function (key) {
self.set(key, process.env[key]);
});
this.readOnly = true;
return this.store;
};

View File

@ -10,8 +10,10 @@ var util = require('util'),
var Literal = exports.Literal = function Literal (store) {
Memory.call(this);
this.type = 'literal'
this.store = store || {};
this.type = 'literal';
this.readOnly = true;
this.store = store || {};
};
// Inherit from Memory store.