2011-03-31 06:32:47 +00:00
|
|
|
/*
|
|
|
|
* memory.js: Simple memory storage engine for nconf configuration(s)
|
|
|
|
*
|
2011-11-24 05:33:08 +00:00
|
|
|
* (C) 2011, Nodejitsu Inc.
|
2011-03-31 06:32:47 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
var common = require('../common');
|
2011-03-31 06:32:47 +00:00
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// ### function Memory (options)
|
|
|
|
// #### @options {Object} Options for this instance
|
|
|
|
// Constructor function for the Memory nconf store which maintains
|
|
|
|
// a nested json structure based on key delimiters `:`.
|
|
|
|
//
|
2012-04-14 19:28:55 +00:00
|
|
|
// e.g. `my:nested:key` ==> `{ my: { nested: { key: } } }`
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
var Memory = exports.Memory = function (options) {
|
2011-09-16 10:49:47 +00:00
|
|
|
options = options || {};
|
|
|
|
this.type = 'memory';
|
|
|
|
this.store = {};
|
|
|
|
this.mtimes = {};
|
|
|
|
this.readOnly = false;
|
|
|
|
this.loadFrom = options.loadFrom || null;
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
if (this.loadFrom) {
|
|
|
|
this.store = common.loadFilesSync(this.loadFrom);
|
|
|
|
}
|
2011-03-31 06:32:47 +00:00
|
|
|
};
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// ### function get (key)
|
|
|
|
// #### @key {string} Key to retrieve for this instance.
|
|
|
|
// Retrieves the value for the specified key (if any).
|
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
Memory.prototype.get = function (key) {
|
2012-04-14 19:28:55 +00:00
|
|
|
var target = this.store,
|
2011-09-16 10:49:47 +00:00
|
|
|
path = common.path(key);
|
2011-03-31 06:32:47 +00:00
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// Scope into the object to get the appropriate nested context
|
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
while (path.length > 0) {
|
|
|
|
key = path.shift();
|
2012-12-20 23:03:37 +00:00
|
|
|
if (target && typeof target !== 'string' && key in target) {
|
2012-09-07 14:29:30 +00:00
|
|
|
target = target[key];
|
|
|
|
continue;
|
2011-03-31 06:32:47 +00:00
|
|
|
}
|
2012-09-07 14:29:30 +00:00
|
|
|
return undefined;
|
2011-03-31 06:32:47 +00:00
|
|
|
}
|
2012-09-07 14:29:30 +00:00
|
|
|
|
|
|
|
return target;
|
2011-03-31 06:32:47 +00:00
|
|
|
};
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// ### function set (key, value)
|
|
|
|
// #### @key {string} Key to set in this instance
|
|
|
|
// #### @value {literal|Object} Value for the specified key
|
|
|
|
// Sets the `value` for the specified `key` in this instance.
|
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
Memory.prototype.set = function (key, value) {
|
2011-09-16 10:49:47 +00:00
|
|
|
if (this.readOnly) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
|
|
|
var target = this.store,
|
2011-09-16 10:49:47 +00:00
|
|
|
path = common.path(key);
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2012-09-07 14:29:30 +00:00
|
|
|
if (path.length === 0) {
|
|
|
|
//
|
|
|
|
// Root must be an object
|
|
|
|
//
|
|
|
|
if (!value || typeof value !== 'object') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.reset();
|
|
|
|
this.store = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// Update the `mtime` (modified time) of the key
|
|
|
|
//
|
|
|
|
this.mtimes[key] = Date.now();
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// Scope into the object to get the appropriate nested context
|
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
while (path.length > 1) {
|
|
|
|
key = path.shift();
|
2011-06-05 05:29:14 +00:00
|
|
|
if (!target[key] || typeof target[key] !== 'object') {
|
2011-03-31 06:32:47 +00:00
|
|
|
target[key] = {};
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-03-31 06:32:47 +00:00
|
|
|
target = target[key];
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
// Set the specified value in the nested JSON structure
|
2011-03-31 06:32:47 +00:00
|
|
|
key = path.shift();
|
|
|
|
target[key] = value;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// ### function clear (key)
|
|
|
|
// #### @key {string} Key to remove from this instance
|
|
|
|
// Removes the value for the specified `key` from this instance.
|
|
|
|
//
|
2011-03-31 06:32:47 +00:00
|
|
|
Memory.prototype.clear = function (key) {
|
2011-09-16 10:49:47 +00:00
|
|
|
if (this.readOnly) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
|
|
|
var target = this.store,
|
2012-09-07 14:29:30 +00:00
|
|
|
value = target,
|
2011-09-16 10:49:47 +00:00
|
|
|
path = common.path(key);
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// Remove the key from the set of `mtimes` (modified times)
|
|
|
|
//
|
|
|
|
delete this.mtimes[key];
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// Scope into the object to get the appropriate nested context
|
|
|
|
//
|
2012-09-07 14:29:30 +00:00
|
|
|
for (var i = 0; i < path.length - 1; i++) {
|
|
|
|
key = path[i];
|
|
|
|
value = target[key];
|
|
|
|
if (typeof value !== 'function' && typeof value !== 'object') {
|
|
|
|
return false;
|
2011-03-31 06:32:47 +00:00
|
|
|
}
|
2012-09-07 14:29:30 +00:00
|
|
|
target = value;
|
2011-03-31 06:32:47 +00:00
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
// Delete the key from the nested JSON structure
|
2012-09-07 14:29:30 +00:00
|
|
|
key = path[i];
|
2011-03-31 06:32:47 +00:00
|
|
|
delete target[key];
|
|
|
|
return true;
|
2011-04-02 07:03:16 +00:00
|
|
|
};
|
|
|
|
|
2011-06-05 05:35:54 +00:00
|
|
|
//
|
|
|
|
// ### function merge (key, value)
|
|
|
|
// #### @key {string} Key to merge the value into
|
|
|
|
// #### @value {literal|Object} Value to merge into the key
|
|
|
|
// Merges the properties in `value` into the existing object value
|
|
|
|
// at `key`. If the existing value `key` is not an Object, it will be
|
|
|
|
// completely overwritten.
|
|
|
|
//
|
2011-06-05 05:29:14 +00:00
|
|
|
Memory.prototype.merge = function (key, value) {
|
2011-09-16 10:49:47 +00:00
|
|
|
if (this.readOnly) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-06-05 05:29:14 +00:00
|
|
|
//
|
|
|
|
// If the key is not an `Object` or is an `Array`,
|
|
|
|
// then simply set it. Merging is for Objects.
|
|
|
|
//
|
|
|
|
if (typeof value !== 'object' || Array.isArray(value)) {
|
|
|
|
return this.set(key, value);
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-06-05 05:29:14 +00:00
|
|
|
var self = this,
|
2012-04-14 19:28:55 +00:00
|
|
|
target = this.store,
|
2011-09-16 10:49:47 +00:00
|
|
|
path = common.path(key),
|
2011-06-05 05:29:14 +00:00
|
|
|
fullKey = key;
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-06-05 05:29:14 +00:00
|
|
|
//
|
|
|
|
// Update the `mtime` (modified time) of the key
|
|
|
|
//
|
|
|
|
this.mtimes[key] = Date.now();
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-06-05 05:29:14 +00:00
|
|
|
//
|
|
|
|
// Scope into the object to get the appropriate nested context
|
|
|
|
//
|
|
|
|
while (path.length > 1) {
|
|
|
|
key = path.shift();
|
|
|
|
if (!target[key]) {
|
|
|
|
target[key] = {};
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-06-05 05:29:14 +00:00
|
|
|
target = target[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the specified value in the nested JSON structure
|
|
|
|
key = path.shift();
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-06-05 05:29:14 +00:00
|
|
|
//
|
|
|
|
// If the current value at the key target is not an `Object`,
|
2011-08-28 12:50:26 +00:00
|
|
|
// or is an `Array` then simply override it because the new value
|
2011-06-05 05:29:14 +00:00
|
|
|
// is an Object.
|
|
|
|
//
|
|
|
|
if (typeof target[key] !== 'object' || Array.isArray(target[key])) {
|
|
|
|
target[key] = value;
|
|
|
|
return true;
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-06-05 05:29:14 +00:00
|
|
|
return Object.keys(value).every(function (nested) {
|
2012-06-21 06:46:10 +00:00
|
|
|
return self.merge(common.key(fullKey, nested), value[nested]);
|
2011-06-05 05:29:14 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
|
|
|
// ### function reset (callback)
|
|
|
|
// Clears all keys associated with this instance.
|
|
|
|
//
|
|
|
|
Memory.prototype.reset = function () {
|
2011-09-16 10:49:47 +00:00
|
|
|
if (this.readOnly) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-14 19:28:55 +00:00
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
this.mtimes = {};
|
|
|
|
this.store = {};
|
|
|
|
return true;
|
2012-01-02 22:20:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### function loadSync
|
|
|
|
// Returns the store managed by this instance
|
|
|
|
//
|
|
|
|
Memory.prototype.loadSync = function () {
|
|
|
|
return this.store || {};
|
2012-06-21 06:46:10 +00:00
|
|
|
};
|