Skip to content

Instantly share code, notes, and snippets.

@vfulco
Forked from bryanhelmig/default-lambda-eval.js
Created August 2, 2018 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfulco/9c76faf4730d3808fe47ae5e4b01f809 to your computer and use it in GitHub Desktop.
Save vfulco/9c76faf4730d3808fe47ae5e4b01f809 to your computer and use it in GitHub Desktop.
The default function template for AWS Lambda in Zapier. Just create a Lambda function with this code named "zapier_eval" and we'll take care of the rest! This code is released to the public domain - feel free to customize, fork or repackage this.
'use strict';
var domain = require('domain');
var userMethod = 'handler';
var userCodeWrapper = function(userCode) {
return '\
"use strict";\n\
' + userCode + '\n\
try {\n\
if (' + userMethod + '.length > 1) {\n\
' + userMethod + '(userInput, callback);\n\
} else {\n\
callback(null, ' + userMethod + '(userInput));\n\
}\n\
} catch(err) {\n\
callback(err, null);\n\
}';
};
module.exports.handler = function(event, context) {
var callback = function(err, val) {
context.done(err, val);
};
var userCode = userCodeWrapper(event.userCode);
var d = domain.create();
d.on('error', function(err) {
context.done(err, null);
});
d.run(function(){
var wrapperFunc = new Function('require', 'userInput', 'callback', userCode);
wrapperFunc(require, event.userInput, callback);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment