diff --git a/lib/nconf/provider.js b/lib/nconf/provider.js index 425bc04..a67509f 100644 --- a/lib/nconf/provider.js +++ b/lib/nconf/provider.js @@ -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); } }); diff --git a/lib/nconf/stores/argv.js b/lib/nconf/stores/argv.js index 62c8381..1e2d4a0 100644 --- a/lib/nconf/stores/argv.js +++ b/lib/nconf/stores/argv.js @@ -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; }; \ No newline at end of file diff --git a/lib/nconf/stores/env.js b/lib/nconf/stores/env.js index fa3ce42..89bffd9 100644 --- a/lib/nconf/stores/env.js +++ b/lib/nconf/stores/env.js @@ -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; }; diff --git a/lib/nconf/stores/literal.js b/lib/nconf/stores/literal.js index ff69f63..37a723c 100644 --- a/lib/nconf/stores/literal.js +++ b/lib/nconf/stores/literal.js @@ -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.