2017-01-05 15:50:05 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-05-18 23:15:49 +10:00
|
|
|
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
|
|
|
|
2016-05-23 20:38:39 +10: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
|
|
|
|
2016-05-18 23:15:49 +10: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,
|
2016-05-18 23:15:49 +10:00
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
2016-05-23 20:38:39 +10:00
|
|
|
// Create a function handler
|
2016-05-23 13:43:08 +02:00
|
|
|
// The function handler is used to simulate Lambda functions
|
|
|
|
|
createHandler(funOptions, options) {
|
2016-05-18 23:15:49 +10:00
|
|
|
if (!options.skipCacheInvalidation) {
|
|
|
|
|
debugLog('Invalidating cache...');
|
|
|
|
|
|
2016-05-23 13:43:08 +02:00
|
|
|
for (const key in require.cache) {
|
2016-05-18 23:15:49 +10:00
|
|
|
// 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`);
|
2016-05-18 23:15:49 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handler;
|
2016-05-23 13:43:08 +02:00
|
|
|
},
|
|
|
|
|
};
|