Last active
August 2, 2018 01:41
-
-
Save bryanhelmig/2c0ab8aa035145b78cee 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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