This repository was archived by the owner on Sep 25, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +66
-2
lines changed
Expand file tree Collapse file tree 5 files changed +66
-2
lines changed Original file line number Diff line number Diff line change @@ -65,6 +65,7 @@ zero-config := (dirname: String, opts?: {
6565 get: (keypath?: Keypath) => Any,
6666 set: ((keypath: Keypath, value: Any) => void) &
6767 (value: Any) => void,
68+ freeze: () => void,
6869 getRemote: (keypath?: Keypath) => Any,
6970 setRemote: ((keypath: Keypath, value: Any) => void) &
7071 (value: Any) => void
@@ -224,6 +225,19 @@ Note you can also call `config.set(entireObject)` to merge an
224225 deep extend to set all the key / value pairs in ` entireObject `
225226 onto the config instance.
226227
228+ #### ` config.freeze() `
229+
230+ Since the ` config ` object is supposed to represent a set of
231+ static, immutable configuration that's loaded at process
232+ startup time it would be useful to enforce this.
233+
234+ Once you are ready to stop mutating ` config ` you can call
235+ ` .freeze() ` . Any future calls to ` .set() ` will throw a
236+ config frozen exception.
237+
238+ Note that you can always call ` config.setRemote() ` as that is
239+ not effected by ` .freeze() `
240+
227241#### ` var value = config.getRemote(keypath) `
228242
229243The same as ` config.get() ` but gets from a different in memory
Original file line number Diff line number Diff line change @@ -7,9 +7,12 @@ var errors = require('./errors.js');
77module . exports = ConfigWrapper ;
88
99function ConfigWrapper ( configObject ) {
10+ var frozen = false ;
11+
1012 return {
1113 get : getKey ,
12- set : setKey
14+ set : setKey ,
15+ freeze : freeze
1316 } ;
1417
1518 function getKey ( keyPath ) {
@@ -21,6 +24,14 @@ function ConfigWrapper(configObject) {
2124 }
2225
2326 function setKey ( keyPath , value ) {
27+ if ( frozen ) {
28+ throw errors . SetFrozenObject ( {
29+ keyPath : keyPath ,
30+ valueStr : JSON . stringify ( value ) ,
31+ value : value
32+ } ) ;
33+ }
34+
2435 if ( arguments . length === 1 ) {
2536 return multiSet ( keyPath ) ;
2637 }
@@ -42,6 +53,10 @@ function ConfigWrapper(configObject) {
4253 return putPath ( configObject , keyPath , v ) ;
4354 }
4455
56+ function freeze ( ) {
57+ frozen = true ;
58+ }
59+
4560 function multiSet ( obj ) {
4661 if ( obj === null || typeof obj !== 'object' ) {
4762 throw errors . InvalidMultiSetArgument ( {
Original file line number Diff line number Diff line change @@ -50,11 +50,25 @@ var InvalidMultiSetArgument = TypedError({
5050 obj : null
5151} ) ;
5252
53+ var SetFrozenObject = TypedError ( {
54+ type : 'set.frozen.object' ,
55+ message : 'Cannot `config.set(key, value)`. Config is ' +
56+ 'frozen.\n' +
57+ 'expected `config.set()` not to be called. Instead ' +
58+ 'it was called with {keyPath} and {valueStr}.\n' +
59+ 'SUGGESTED FIX: Do not call `config.set()` it was ' +
60+ 'frozen by someone else.\n' ,
61+ keyPath : null ,
62+ valueStr : null ,
63+ value : null
64+ } ) ;
65+
5366module . exports = {
5467 InvalidDirname : InvalidDirname ,
5568 MissingDatacenter : MissingDatacenter ,
5669 DatacenterRequired : DatacenterRequired ,
5770 DatacenterFileRequired : DatacenterFileRequired ,
5871 InvalidKeyPath : InvalidKeyPath ,
59- InvalidMultiSetArgument : InvalidMultiSetArgument
72+ InvalidMultiSetArgument : InvalidMultiSetArgument ,
73+ SetFrozenObject : SetFrozenObject
6074} ;
Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ function fetchConfigSync(dirname, opts) {
3939
4040 config . get = localConfigWrapper . get ;
4141 config . set = localConfigWrapper . set ;
42+ config . freeze = localConfigWrapper . freeze ;
4243 config . getRemote = remoteConfigWrapper . get ;
4344 config . setRemote = remoteConfigWrapper . set ;
4445
Original file line number Diff line number Diff line change @@ -370,3 +370,23 @@ test('config.setRemote()', function t(assert) {
370370
371371 assert . end ( ) ;
372372} ) ;
373+
374+ test ( 'config.freeze()' , function t ( assert ) {
375+ var config = fetchConfig ( __dirname ) ;
376+
377+ config . set ( 'foo' , 'bar' ) ;
378+
379+ assert . equal ( config . get ( 'foo' ) , 'bar' ) ;
380+
381+ config . freeze ( ) ;
382+
383+ assert . throws ( function ( ) {
384+ config . set ( 'foo' , 'baz' ) ;
385+ } , / C o n f i g i s f r o z e n / ) ;
386+
387+ assert . throws ( function ( ) {
388+ config . set ( 'bar' , 'baz' ) ;
389+ } , / C o n f i g i s f r o z e n / ) ;
390+
391+ assert . end ( ) ;
392+ } ) ;
You can’t perform that action at this time.
0 commit comments