Files

44 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2017-01-05 15:50:05 +01:00
'use strict';
const debugLog = require('./debugLog');
module.exports = {
2016-08-07 20:28:08 -07:00
getFunctionOptions(fun, funName, servicePath) {
2016-05-23 13:43:08 +02:00
// Split handler into method name and path i.e. handler.run
2016-08-06 20:41:03 -07:00
const handlerPath = fun.handler.split('.')[0];
const handlerName = fun.handler.split('/').pop().split('.')[1];
2017-01-05 09:57:57 +01:00
return {
2016-10-15 17:48:15 +02:00
funName,
handlerName, // i.e. run
2016-08-07 20:28:08 -07:00
handlerPath: `${servicePath}/${handlerPath}`,
2017-03-19 12:38:21 +01:00
funTimeout: (fun.timeout || 30) * 1000,
2016-08-01 01:32:22 -07:00
babelOptions: ((fun.custom || {}).runtime || {}).babel,
};
},
// Create a function handler
2016-05-23 13:43:08 +02:00
// The function handler is used to simulate Lambda functions
createHandler(funOptions, options) {
if (!options.skipCacheInvalidation) {
debugLog('Invalidating cache...');
2016-05-23 13:43:08 +02:00
for (const key in require.cache) {
// Require cache invalidation, brutal and fragile.
// Might cause errors, if so please submit an issue.
if (!key.match('node_modules')) delete require.cache[key];
}
}
debugLog(`Loading handler... (${funOptions.handlerPath})`);
const handler = require(funOptions.handlerPath)[funOptions.handlerName];
if (typeof handler !== 'function') {
2016-05-23 13:43:08 +02:00
throw new Error(`Serverless-offline: handler for '${funOptions.funName}' is not a function`);
}
return handler;
2016-05-23 13:43:08 +02:00
},
};