forked from coolaj86/goldilocks.js
		
	
		
			
				
	
	
		
			48 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			48 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 'use strict';
 | |
| 
 | |
| 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;
 | |
|   var deps = {
 | |
|     messenger: process
 | |
|     // 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.
 | |
|   , net: require('net')
 | |
|   };
 | |
|   deps.proxy = require('./proxy-conn').create(deps, conf);
 | |
|   deps.storage = {
 | |
|     config: {
 | |
|       save: function (changes) {
 | |
|         process.send({
 | |
|           type: 'com.daplie.goldilocks.config-change'
 | |
|         , changes: changes
 | |
|         });
 | |
|       }
 | |
|     }
 | |
|   };
 | |
| 
 | |
|   require('./goldilocks.js').create(deps, conf);
 | |
|   process.removeListener('message', create);
 | |
|   process.on('message', update);
 | |
| }
 | |
| 
 | |
| process.on('message', create);
 |