| 
									
										
										
										
											2017-04-26 20:16:47 -06:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-09 12:40:39 -06:00
										 |  |  | var config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Everything that uses the config should be reading it when relevant rather than
 | 
					
						
							|  |  |  | // just at the beginning, so we keep the reference for the main object and just
 | 
					
						
							|  |  |  | // change all of its properties to match the new config.
 | 
					
						
							|  |  |  | function update(conf) { | 
					
						
							|  |  |  |   var newKeys = Object.keys(conf); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Object.keys(config).forEach(function (key) { | 
					
						
							|  |  |  |     if (newKeys.indexOf(key) < 0) { | 
					
						
							|  |  |  |       delete config[key]; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       config[key] = conf[key]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   console.log('config', JSON.stringify(config)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function create(conf) { | 
					
						
							|  |  |  |   config = conf; | 
					
						
							| 
									
										
										
										
											2017-04-26 20:16:47 -06:00
										 |  |  |   var deps = { | 
					
						
							|  |  |  |     messenger: process | 
					
						
							| 
									
										
										
										
											2017-05-10 16:05:54 -06:00
										 |  |  |     // Note that if a custom createConnections is used it will be called with different
 | 
					
						
							|  |  |  |     // sets of custom options based on what is actually being proxied. Most notably the
 | 
					
						
							|  |  |  |     // HTTP proxying connection creation is not something we currently control.
 | 
					
						
							| 
									
										
										
										
											2017-04-27 16:05:34 -06:00
										 |  |  |   , net: require('net') | 
					
						
							| 
									
										
										
										
											2017-04-26 20:16:47 -06:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2017-05-17 14:06:24 -06:00
										 |  |  |   deps.proxy = require('./proxy-conn').create(deps, conf); | 
					
						
							| 
									
										
										
										
											2017-06-09 11:18:05 -06:00
										 |  |  |   deps.storage = { | 
					
						
							|  |  |  |     config: { | 
					
						
							|  |  |  |       save: function (changes) { | 
					
						
							|  |  |  |         process.send({ | 
					
						
							| 
									
										
										
										
											2017-06-09 16:03:12 -06:00
										 |  |  |           type: 'com.daplie.goldilocks.config-change' | 
					
						
							|  |  |  |         , changes: changes | 
					
						
							| 
									
										
										
										
											2017-06-09 11:18:05 -06:00
										 |  |  |         }); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2017-05-17 14:06:24 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-26 20:16:47 -06:00
										 |  |  |   require('./goldilocks.js').create(deps, conf); | 
					
						
							| 
									
										
										
										
											2017-06-09 12:40:39 -06:00
										 |  |  |   process.removeListener('message', create); | 
					
						
							|  |  |  |   process.on('message', update); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | process.on('message', create); |