/
memory.js
260 lines (224 loc) · 6.47 KB
/
memory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* memory.js: Simple memory storage engine for nconf configuration(s)
*
* (C) 2011, Charlie Robbins and the Contributors.
*
*/
const common = require('../common');
const DEFAULT_ACCESS_SEPARATOR = ':';
const DEFAULT_INPUT_SEPARATOR = '__';
// Helper function for preparing a string for regex matching
function escapeRegExp(string) {
return typeof string === 'string' && string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
//
// ### 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 `:`.
//
// e.g. `my:nested:key` ==> `{ my: { nested: { key: } } }`
//
var Memory = exports.Memory = function (options) {
options = options || {};
this.type = 'memory';
this.store = {};
this.mtimes = {};
this.readOnly = false;
this.loadFrom = options.loadFrom || null;
this.accessSeparator = options.accessSeparator || DEFAULT_ACCESS_SEPARATOR;
this.inputSeparator = options.inputSeparator || DEFAULT_INPUT_SEPARATOR;
this.parseValues = options.parseValues || false;
this.disableDefaultAccessSeparator = options.disableDefaultAccessSeparator || false;
if (typeof(options) === 'string' || options instanceof RegExp) {
this.inputSeparator = options;
}
if (this.loadFrom) {
this.store = common.loadFilesSync(this.loadFrom);
}
};
Memory.prototype._normalizeKey = function (key) {
let inputSeparator = this.inputSeparator;
if (inputSeparator instanceof RegExp) {
inputSeparator = inputSeparator.source;
} else {
inputSeparator = escapeRegExp(inputSeparator);
}
let separatorRegexStr = `${escapeRegExp(this.accessSeparator)}|${inputSeparator}`;
if (!this.disableDefaultAccessSeparator) {
separatorRegexStr += `|${DEFAULT_ACCESS_SEPARATOR}`;
}
const separatorRegEx = new RegExp(separatorRegexStr, 'g');
return key && key.replace(separatorRegEx, this.accessSeparator);
}
//
// ### function get (key)
// #### @key {string} Key to retrieve for this instance.
// Retrieves the value for the specified key (if any).
//
Memory.prototype.get = function (key) {
var target = this.store,
path = common.path(this._normalizeKey(key), this.accessSeparator);
//
// Scope into the object to get the appropriate nested context
//
while (path.length > 0) {
key = path.shift();
if (target && typeof target !== 'string' && Object.hasOwnProperty.call(target, key)) {
target = target[key];
continue;
}
return undefined;
}
return target;
};
//
// ### 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.
//
Memory.prototype.set = function (key, value) {
if (this.readOnly) {
return false;
}
var target = this.store,
path = common.path(this._normalizeKey(key), this.accessSeparator);
if (path.length === 0) {
//
// Root must be an object
//
if (!value || typeof value !== 'object') {
return false;
}
else {
this.reset();
this.store = value;
return true;
}
}
//
// Update the `mtime` (modified time) of the key
//
this.mtimes[key] = Date.now();
//
// Scope into the object to get the appropriate nested context
//
while (path.length > 1) {
key = path.shift();
if (!target[key] || typeof target[key] !== 'object') {
target[key] = {};
}
target = target[key];
}
// Set the specified value in the nested JSON structure
key = path.shift();
if (this.parseValues) {
value = common.parseValues.call(common, value);
}
target[key] = value;
return true;
};
//
// ### function clear (key)
// #### @key {string} Key to remove from this instance
// Removes the value for the specified `key` from this instance.
//
Memory.prototype.clear = function (key) {
if (this.readOnly) {
return false;
}
var target = this.store,
value = target,
path = common.path(key, this.accessSeparator);
//
// Remove the key from the set of `mtimes` (modified times)
//
delete this.mtimes[key];
//
// Scope into the object to get the appropriate nested context
//
for (var i = 0; i < path.length - 1; i++) {
key = path[i];
value = target[key];
if (typeof value !== 'function' && typeof value !== 'object') {
return false;
}
target = value;
}
// Delete the key from the nested JSON structure
key = path[i];
delete target[key];
return true;
};
//
// ### 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.
//
Memory.prototype.merge = function (key, value) {
if (this.readOnly) {
return false;
}
//
// 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) || value === null) {
return this.set(key, value);
}
var self = this,
target = this.store,
path = common.path(key, this.accessSeparator),
fullKey = key;
//
// Update the `mtime` (modified time) of the key
//
this.mtimes[key] = Date.now();
//
// Scope into the object to get the appropriate nested context
//
while (path.length > 1) {
key = path.shift();
if (!target[key]) {
target[key] = {};
}
target = target[key];
}
// Set the specified value in the nested JSON structure
key = path.shift();
//
// If the current value at the key target is not an `Object`,
// or is an `Array` then simply override it because the new value
// is an Object.
//
if (typeof target[key] !== 'object' || Array.isArray(target[key])) {
target[key] = value;
return true;
}
return Object.keys(value).every(function (nested) {
return self.merge(common.keyed(self.accessSeparator, fullKey, nested), value[nested]);
});
};
//
// ### function reset (callback)
// Clears all keys associated with this instance.
//
Memory.prototype.reset = function () {
if (this.readOnly) {
return false;
}
this.mtimes = {};
this.store = {};
return true;
};
//
// ### function loadSync
// Returns the store managed by this instance
//
Memory.prototype.loadSync = function () {
return this.store || {};
};