All files / Nodejs/wrappers test.js

0% Statements 0/63
0% Branches 0/31
0% Functions 0/8
0% Lines 0/63

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                                                                                                                                                                                                           
"use strict";
 
 
let cachedHandler;
module.exports = function(configOverride, botHandler) {
	process.resources = process.env.Resources && JSON.parse(process.env.Resources) || {};
 
	let config = require("../leoConfigure.js");
	const logger = require('leo-logger')('test.wrapper');
	const leosdk = require("../index.js");
	const refUtil = require("../lib/reference.js");
	const assert = require("../lib/assert.js");
 
	process.__config = config;
	const fill = require("../lib/build-config").fillWithTableReferences;
	process.env.TZ = config.timezone;
 
	const botId = config.name;
	const settings = config.cron && config.cron.settings || {};
 
	const moment = require("moment");
	let decrypted = false;
	const cron = leosdk.bot;
	const dynamodb = leosdk.aws.dynamodb;
 
	for (let x of process.listeners('uncaughtException')) { //remove lambdas default listener
		process.removeListener('uncaughtException', x);
	}
	let theCallback;
	let theContext;
	let __theEvent;
	process.on('uncaughtException', function(err) {
		logger.error((new Date).toUTCString() + ' uncaughtException:', err.message);
		logger.error(err.stack);
		theCallback(null, "Application Error");
	});
 
	function empty(obj) {
		for (let k in obj) {
			delete obj[k];
		}
	}
 
	if (!botHandler) {
		botHandler = configOverride;
		configOverride = {};
	}
 
	Object.assign(config, configOverride);
	return function(event, context, callback) {
		let cb = callback;
		callback = (err, data) => {
			assert.print();
			cb(err, data);
		}
		context.assert = assert.clear();
		context.callbackWaitsForEmptyEventLoop = false;
		context.resources = process.resources;
		context.botId = event.botId || botId;
 
		context.settings = settings;
 
		theCallback = callback;
		//clear out the registry
		empty(config.registry);
		leosdk.configuration.registry = config.registry;
		config.registry.context = context;
		config.registry.__cron = event.__cron;
		global.cron_run_again = false;
		if (event.__cron && event.__cron.id) { //If it is in cron, use that regardless
			config.registry.id = event.__cron.id;
		} else if (!config.registry.id) { //If they didn't specify it in their config, then let's get it from the function name
			config.registry.id = process.env.AWS_LAMBDA_FUNCTION_NAME;
		}
		logger.log("Registry", JSON.stringify(config.registry, null, 2));
		theContext = context;
		__theEvent = event;
		let startTime = moment.now();
 
		try {
			let promise = botHandler(event || {}, context, function(err, data) {
				callback(null, err || data);
			});
			if (promise && typeof promise.then == "function" && botHandler.length < 3) {
				promise.then(data => {
					callback(null, err || data);
				});
			}
			if (promise && typeof promise.catch == "function") {
				promise.catch(err => {
					callback(null, err);
				});
			}
		} catch (e) {
			logger.log("error", e);
			callback(null, e);
		}
 
 
	}
}