All files / Nodejs/lib configuration.js

84.12% Statements 53/63
80.51% Branches 62/77
71.42% Functions 5/7
85.48% Lines 53/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  1x 1x 1x 28x 28x 2x 2x   28x 28x   84x       28x 28x 26x                 28x     28x     28x     28x     28x 28x 28x 28x     36x 36x     36x     36x     36x     36x               28x     28x 26x     28x         28x 28x 28x 28x 28x 28x   28x 26x     28x 26x     28x 26x     28x 2x     28x 28x     28x       28x 28x 28x 28x   28x    
"use strict";
let extend = require("extend");
let file = require("./leo-sdk-config.js");
module.exports = function(data) {
	let doValidation = true;
	if (typeof data === "boolean") {
		doValidation = data;
		data = undefined;
	}
	let updateListeners = [];
	let configuration = {
		onUpdate: (callback) => {
			updateListeners.push(callback);
		},
		update: function(newConfig) {
			let config;
			newConfig = newConfig || global.leosdk;
			if (newConfig != null && (newConfig.LeoKinesisStream || newConfig.LeoS3 || newConfig.LeoFirehoseStream)) {
				newConfig = {
					region: newConfig.Region,
					resources: newConfig,
					firehose: newConfig.LeoFirehoseStream,
					kinesis: newConfig.LeoKinesisStream,
					s3: newConfig.LeoS3
				};
			}
 
			var resources = {
				resources: process.env.Resources && JSON.parse(process.env.Resources) || {}
			};
			Iif ("leosdk" in process.env) {
				resources = JSON.parse(process.env["leosdk"]);
			}
			let profile = (typeof newConfig === "string" ? newConfig : null) || process.env.LEO_DEFAULT_PROFILE || "default";
 
 
			Iif (!file[profile] && profile != "default" && doValidation) {
				throw new Error(`Profile "${profile}" does not exist!`);
			}
			config = extend(true, {}, file[profile] || {}, resources, typeof newConfig === "object" ? newConfig : {});
			update(this, config);
			updateListeners.map(hook => hook(this));
			return this;
		},
		validate: function() {
			let errors = [];
			Iif (!this.aws.region) {
				errors.push("region");
			}
			Iif (!this.stream) {
				errors.push("kinesis");
			}
			Iif (!this.bus.s3) {
				errors.push("s3");
			}
			Iif (!this.bus.firehose) {
				errors.push("firehose");
			}
			Iif (errors.length > 0) {
				throw new Error("Invalid Settings: Missing " + errors.join(", "));
			}
		},
		setProfile: function(profile) {
			return this.update(profile);
		}
	};
	configuration.update(data);
 
 
	if (doValidation) {
		configuration.validate();
	}
 
	return configuration;
};
 
 
function update(config, newConfig) {
	newConfig = extend(true, {}, newConfig);
	config.bus = config.bus || {};
	config.aws = config.aws || {};
	config._meta = config._meta || {};
	var bus = newConfig.bus = newConfig.bus || {};
	var aws = newConfig.aws = newConfig.aws || {};
 
	if (newConfig.kinesis && !newConfig.stream) {
		newConfig.stream = newConfig.kinesis;
	}
 
	if (newConfig.s3 && !bus.s3) {
		bus.s3 = newConfig.s3;
	}
 
	if (newConfig.firehose && !bus.firehose) {
		bus.firehose = newConfig.firehose;
	}
 
	if (!newConfig.region) {
		newConfig.region = aws.region || (newConfig.resources && newConfig.resources.Region) || 'us-west-2';
	}
 
	Eif (newConfig.region && !aws.region) {
		aws.region = newConfig.region;
	}
 
	Iif (newConfig.profile && !aws.profile) {
		aws.profile = newConfig.profile;
	}
 
	let u = newConfig.update;
	delete newConfig.update;
	extend(true, config, newConfig);
	config._meta.region = config.aws.region;
 
	newConfig.update = u;
}