@@ -24,8 +24,9 @@ var Provider = exports.Provider = function (options) {
24
24
// Setup default options for working with `stores`,
25
25
// `overrides`, `process.env` and `process.argv`.
26
26
//
27
- options = options || { } ;
28
- this . sources = [ ] ;
27
+ options = options || { } ;
28
+ this . _stores = { } ;
29
+ this . _sources = [ ] ;
29
30
30
31
//
31
32
// Add any stores passed in through the options
@@ -43,6 +44,19 @@ var Provider = exports.Provider = function (options) {
43
44
self . add ( store . name || name || store . type , store ) ;
44
45
} ) ;
45
46
}
47
+
48
+ //
49
+ // Add any read-only sources to this instance
50
+ //
51
+ if ( options . source ) {
52
+ this . _sources . push ( this . create ( options . source . type || options . source . name , options . source ) ) ;
53
+ }
54
+ else if ( options . sources ) {
55
+ Object . keys ( options . sources ) . forEach ( function ( name ) {
56
+ var source = options . sources [ name ] ;
57
+ self . _sources . push ( self . create ( source . type || source . name || name , source ) ) ;
58
+ } ) ;
59
+ }
46
60
} ;
47
61
48
62
//
@@ -86,7 +100,7 @@ Provider.prototype.use = function (name, options) {
86
100
} ) ;
87
101
}
88
102
89
- var store = this . sources [ name ] ,
103
+ var store = this . _stores [ name ] ,
90
104
update = store && ! sameOptions ( store ) ;
91
105
92
106
if ( ! store || update ) {
@@ -118,10 +132,10 @@ Provider.prototype.add = function (name, options) {
118
132
throw new Error ( 'Cannot add store with unknown type: ' + type ) ;
119
133
}
120
134
121
- this . sources [ name ] = this . create ( type , options ) ;
135
+ this . _stores [ name ] = this . create ( type , options ) ;
122
136
123
- if ( this . sources [ name ] . loadSync ) {
124
- this . sources [ name ] . loadSync ( ) ;
137
+ if ( this . _stores [ name ] . loadSync ) {
138
+ this . _stores [ name ] . loadSync ( ) ;
125
139
}
126
140
127
141
return this ;
@@ -135,7 +149,7 @@ Provider.prototype.add = function (name, options) {
135
149
// this was used in the call to `.add()`.
136
150
//
137
151
Provider . prototype . remove = function ( name ) {
138
- delete this . sources [ name ] ;
152
+ delete this . _stores [ name ] ;
139
153
return this ;
140
154
} ;
141
155
@@ -171,14 +185,14 @@ Provider.prototype.get = function (key, callback) {
171
185
// the entire set of stores, but up until there is a defined value.
172
186
//
173
187
var current = 0 ,
174
- names = Object . keys ( this . sources ) ,
188
+ names = Object . keys ( this . _stores ) ,
175
189
self = this ,
176
190
response ;
177
191
178
192
async . whilst ( function ( ) {
179
193
return typeof response === 'undefined' && current < names . length ;
180
194
} , function ( next ) {
181
- var store = self . sources [ names [ current ] ] ;
195
+ var store = self . _stores [ names [ current ] ] ;
182
196
current ++ ;
183
197
184
198
if ( store . get . length >= 2 ) {
@@ -268,7 +282,7 @@ Provider.prototype.merge = function () {
268
282
//
269
283
Provider . prototype . load = function ( callback ) {
270
284
var self = this ,
271
- stores = Object . keys ( this . sources ) . map ( function ( name ) { return self . sources [ name ] } ) ;
285
+ stores = Object . keys ( this . _stores ) . map ( function ( name ) { return self . _stores [ name ] } ) ;
272
286
273
287
function loadStoreSync ( store ) {
274
288
if ( ! store . loadSync ) {
@@ -317,11 +331,11 @@ Provider.prototype.load = function (callback) {
317
331
// then do so.
318
332
//
319
333
if ( ! callback ) {
320
- mergeSources ( loadBatch ( self . sources ) ) ;
334
+ mergeSources ( loadBatch ( self . _sources ) ) ;
321
335
return loadBatch ( stores ) ;
322
336
}
323
337
324
- loadBatch ( self . sources , function ( err , data ) {
338
+ loadBatch ( self . _sources , function ( err , data ) {
325
339
if ( err ) {
326
340
return callback ( err ) ;
327
341
}
@@ -331,7 +345,7 @@ Provider.prototype.load = function (callback) {
331
345
} ) ;
332
346
}
333
347
334
- return self . sources . length
348
+ return self . _sources . length
335
349
? loadSources ( )
336
350
: loadBatch ( stores , callback ) ;
337
351
} ;
@@ -350,10 +364,10 @@ Provider.prototype.save = function (value, callback) {
350
364
}
351
365
352
366
var self = this ,
353
- names = Object . keys ( this . sources ) ;
367
+ names = Object . keys ( this . _stores ) ;
354
368
355
369
function saveStoreSync ( name ) {
356
- var store = self . sources [ name ] ;
370
+ var store = self . _stores [ name ] ;
357
371
358
372
if ( ! store . saveSync ) {
359
373
throw new Error ( 'nconf store ' + store . type + ' has no saveSync() method' ) ;
@@ -363,7 +377,7 @@ Provider.prototype.save = function (value, callback) {
363
377
}
364
378
365
379
function saveStore ( name , next ) {
366
- var store = self . sources [ name ] ;
380
+ var store = self . _stores [ name ] ;
367
381
368
382
if ( ! store . save && ! store . saveSync ) {
369
383
return next ( new Error ( 'nconf store ' + store . type + ' has no save() method' ) ) ;
@@ -404,7 +418,7 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) {
404
418
response ;
405
419
406
420
function runAction ( name , next ) {
407
- var store = self . sources [ name ] ;
421
+ var store = self . _stores [ name ] ;
408
422
409
423
if ( destructive && store . readOnly ) {
410
424
return next ( ) ;
@@ -416,15 +430,15 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) {
416
430
}
417
431
418
432
if ( callback ) {
419
- return async . forEach ( Object . keys ( this . sources ) , runAction , function ( err ) {
433
+ return async . forEach ( Object . keys ( this . _stores ) , runAction , function ( err ) {
420
434
return err ? callback ( err ) : callback ( ) ;
421
435
} ) ;
422
436
}
423
437
424
438
425
- Object . keys ( this . sources ) . forEach ( function ( name ) {
439
+ Object . keys ( this . _stores ) . forEach ( function ( name ) {
426
440
if ( typeof response === 'undefined' ) {
427
- var store = self . sources [ name ] ;
441
+ var store = self . _stores [ name ] ;
428
442
429
443
if ( destructive && store . readOnly ) {
430
444
return ;
0 commit comments