lambda-wrapper
Advanced tools
Comparing version 0.1.1 to 0.1.2
150
index.js
@@ -0,89 +1,82 @@ | ||
'use strict'; | ||
const AWS = require('aws-sdk'); | ||
// Wrapper class for AWS Lambda | ||
function Wrapped(mod, options) { | ||
if (options == null) { | ||
options = {}; | ||
} | ||
class Wrapped { | ||
constructor(mod, opts) { | ||
const options = opts || {}; | ||
this.lambdaModule = mod; | ||
var handler = options.handler || 'handler'; | ||
const handler = options.handler || 'handler'; | ||
if (mod[handler]) { | ||
this.handler =mod[handler]; | ||
this.handler = mod[handler]; | ||
} | ||
} | ||
} | ||
Wrapped.prototype.runHandler = function(event, customContext, callback) { | ||
var callback; | ||
runHandler(event, customContext, cb) { | ||
return new Promise((resolve, reject) => { | ||
var defaultContext = { | ||
succeed: function(success) { | ||
return callback(null, success); | ||
}, | ||
fail: function(error) { | ||
return callback(error, null); | ||
}, | ||
done: function(error, success) { | ||
return callback(error, success); | ||
const promiseCallback = (error, response) => { | ||
if(error) { | ||
return reject(error); | ||
} | ||
}; | ||
return resolve(response); | ||
}; | ||
var lambdaContext = customContext; | ||
const callback = cb || promiseCallback; | ||
if (!lambdaContext.succeed) { | ||
lambdaContext.succeed = defaultContext.succeed; | ||
} | ||
if (!lambdaContext.fail) { | ||
lambdaContext.fail = defaultContext.fail; | ||
} | ||
if (!lambdaContext.done) { | ||
lambdaContext.done = defaultContext.done; | ||
} | ||
const defaultContext = { | ||
succeed: success => callback(null, success), | ||
fail: error => callback(error, null), | ||
done: (error, success) => callback(error, success) | ||
}; | ||
try { | ||
const lambdaContext = Object.assign({}, defaultContext, customContext); | ||
try { | ||
if (this.handler) { | ||
this.handler(event, lambdaContext, callback); | ||
this.handler(event, lambdaContext, callback); | ||
} else { | ||
var AWS = require('aws-sdk'); | ||
if (this.lambdaModule.region) { | ||
AWS.config.update({ | ||
region: this.lambdaModule.region | ||
}); | ||
if (this.lambdaModule.region) { | ||
AWS.config.update({ | ||
region: this.lambdaModule.region | ||
}); | ||
} | ||
const lambda = new AWS.Lambda(); | ||
const params = { | ||
FunctionName: this.lambdaModule.lambdaFunction, | ||
InvocationType: 'RequestResponse', | ||
LogType: 'None', | ||
Payload: JSON.stringify(event) | ||
}; | ||
lambda.invoke(params, (err, data) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
var lambda = new AWS.Lambda(); | ||
var params = { | ||
FunctionName: this.lambdaModule.lambdaFunction, | ||
InvocationType: 'RequestResponse', | ||
LogType: 'None', | ||
Payload: JSON.stringify(event), | ||
}; | ||
lambda.invoke(params, function(err, data) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
callback(null, JSON.parse(data.Payload)); | ||
}); | ||
return callback(null, JSON.parse(data.Payload)); | ||
}); | ||
} | ||
} catch (ex) { | ||
throw(ex); | ||
} | ||
} catch (ex) { | ||
throw (ex); | ||
} | ||
}); | ||
} | ||
run(event, callback) { | ||
return this.runHandler(event, {}, callback); | ||
} | ||
} | ||
Wrapped.prototype.run = function(event, callback) { | ||
return this.runHandler(event,{},callback); | ||
}; | ||
// Wrapper factory | ||
function wrap(mod, options) { | ||
var wrapped = new Wrapped(mod, options); | ||
const wrap = (mod, options) => new Wrapped(mod, options); | ||
return wrapped; | ||
} | ||
// Static variables (for backwards compatibility) | ||
var latest; | ||
let latest; | ||
@@ -94,16 +87,25 @@ // Public interface for the module | ||
// reusable wrap method | ||
wrap: wrap, | ||
// reusable wrap method | ||
wrap, | ||
// static init/run interface for backwards compatibility | ||
init: function(mod, options) { | ||
latest = wrap(mod, options); | ||
}, | ||
run: function(event, callback) { | ||
if (typeof latest === typeof undefined) { | ||
return callback('Module not initialized', null); | ||
} else { | ||
latest.run(event, callback); | ||
} | ||
// static init/run interface for backwards compatibility | ||
init: (mod, options) => { | ||
latest = wrap(mod, options); | ||
}, | ||
run: (event, callback) => new Promise((resolve, reject) => { | ||
if (typeof latest === typeof undefined) { | ||
const error = 'Module not initialized'; | ||
reject(error); | ||
return callback(error, null); | ||
} | ||
return latest.run(event, (err, data) => { | ||
if (callback) { | ||
return callback(err, data); | ||
} | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(data); | ||
}); | ||
}) | ||
}; |
@@ -6,7 +6,6 @@ // Test function. Save as lambdaWrapper-test to your AWS env | ||
exports.handler = (event, context, callback) => { | ||
callback(null, { | ||
src: 'lambda', | ||
event: event | ||
}); | ||
callback(null, { | ||
src: 'lambda', | ||
event | ||
}); | ||
}; |
{ | ||
"name": "lambda-wrapper", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Wrapper for running Amazon Lambda modules locally", | ||
@@ -20,3 +20,4 @@ "main": "index.js", | ||
"contributors": [ | ||
"Toni Ruottu (https://sc5.io)" | ||
"Toni Ruottu (https://sc5.io)", | ||
"Eetu Tuomala (https://sc5.io)" | ||
], | ||
@@ -32,3 +33,5 @@ "license": "MIT", | ||
"mocha": "2.4.5" | ||
}, | ||
"dependencies": { | ||
} | ||
} |
@@ -46,2 +46,3 @@ # lambda-wrapper | ||
* 2016/10/21 - v0.1.2 - Support for using promises | ||
* 2016/07/26 - v0.1.1 - Support for alternative handler. runHandler method for passing custom context. | ||
@@ -48,0 +49,0 @@ * 2016/04/26 - v0.1.0 - Support for running lambda functions also from AWS |
@@ -1,6 +0,11 @@ | ||
var testMod1 = { | ||
handler: function(event, context) { | ||
'use strict'; | ||
const wrapper = require('../index.js'); | ||
const expect = require('chai').expect; | ||
const testMod1 = { | ||
handler: (event, context) => { | ||
if (event.test === 'success') { | ||
context.succeed('Success'); | ||
} | ||
} | ||
if (event.test === 'fail') { | ||
@@ -12,22 +17,22 @@ context.fail('Fail'); | ||
var testMod2 = { | ||
handler: function(event, context) { | ||
context.succeed(event); | ||
const testMod2 = { | ||
handler: (event, context) => { | ||
context.succeed(event); | ||
} | ||
}; | ||
var testMod3 = { | ||
handler: function(event, context, callback) { | ||
const testMod3 = { | ||
handler: (event, context, callback) => { | ||
callback(null, event); | ||
} | ||
} | ||
}; | ||
var testMod4 = { | ||
myHandler: function(event,context,callback) { | ||
const testMod4 = { | ||
myHandler: (event, context, callback) => { | ||
callback(null, event); | ||
} | ||
} | ||
}; | ||
var testMod5 = { | ||
handler: function(event,context,callback) { | ||
const testMod5 = { | ||
handler: (event, context, callback) => { | ||
callback(null, { | ||
@@ -37,11 +42,9 @@ test: context.functionName | ||
} | ||
} | ||
}; | ||
var wrapper = require('../index.js'); | ||
var expect = require('chai').expect; | ||
describe('lambda-wrapper local', () => { | ||
it('init + run with success - callback', (done) => { | ||
wrapper.init(testMod1); | ||
describe('lambda-wrapper', function() { | ||
it('init + run with success', function(done) { | ||
wrapper.init(testMod1); | ||
wrapper.run({test: 'success'}, function(err, response) { | ||
wrapper.run({test: 'success'}, (err, response) => { | ||
expect(response).to.be.equal('Success'); | ||
@@ -51,5 +54,18 @@ done(); | ||
}); | ||
it('init + run with failure', function(done) { | ||
it('init + run with success - promise', (done) => { | ||
wrapper.init(testMod1); | ||
wrapper.run({test: 'fail'}, function(err, response) { | ||
wrapper.run({test: 'success'}) | ||
.then((response) => { | ||
expect(response).to.be.equal('Success'); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
it('init + run with failure - callback', (done) => { | ||
wrapper.init(testMod1); | ||
wrapper.run({test: 'fail'}, (err, response) => { | ||
expect(err).to.be.equal('Fail'); | ||
@@ -59,60 +75,148 @@ done(); | ||
}); | ||
it('wrap + run module 2', function(done) { | ||
var w2 = wrapper.wrap(testMod2); | ||
w2.run({foo: 'bar'}, function(err, response) { | ||
expect(response.foo).to.be.equal('bar'); | ||
done(); | ||
}); | ||
it('init + run with failure - promise', (done) => { | ||
wrapper.init(testMod1); | ||
wrapper.run({test: 'fail'}).catch((err) => { | ||
expect(err).to.be.equal('Fail'); | ||
done(); | ||
}); | ||
}); | ||
it('wrap + run module 1', function(done) { | ||
var w1 = wrapper.wrap(testMod1); | ||
w1.run({test: 'success'}, function(err, response) { | ||
expect(response).to.be.equal('Success'); | ||
done(); | ||
}); | ||
it('wrap + run module 2 - callback', (done) => { | ||
const w = wrapper.wrap(testMod2); | ||
w.run({foo: 'bar'}, (err, response) => { | ||
expect(response.foo).to.be.equal('bar'); | ||
done(); | ||
}); | ||
}); | ||
it('wrap + run module 3 (callback notation)', function(done) { | ||
var w1 = wrapper.wrap(testMod3); | ||
w1.run({test: 'cbsuccess'}, function(err, response) { | ||
expect(response.test).to.be.equal('cbsuccess'); | ||
done(); | ||
}); | ||
it('wrap + run module 2 - promise', (done) => { | ||
const w = wrapper.wrap(testMod2); | ||
w.run({foo: 'bar'}) | ||
.then((response) => { | ||
expect(response.foo).to.be.equal('bar'); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
it('wrap + run module 4 (alternate handler)', function(done) { | ||
var w4 = wrapper.wrap(testMod4, { | ||
it('wrap + run module 1 - callback', (done) => { | ||
const w = wrapper.wrap(testMod1); | ||
w.run({test: 'success'}, (err, response) => { | ||
expect(response).to.be.equal('Success'); | ||
done(); | ||
}); | ||
}); | ||
it('wrap + run module 1 - promise', (done) => { | ||
const w = wrapper.wrap(testMod1); | ||
w.run({test: 'success'}) | ||
.then((response) => { | ||
expect(response).to.be.equal('Success'); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
it('wrap + run module 3 (callback notation) - callback', (done) => { | ||
const w = wrapper.wrap(testMod3); | ||
w.run({test: 'cbsuccess'}, (err, response) => { | ||
expect(response.test).to.be.equal('cbsuccess'); | ||
done(); | ||
}); | ||
}); | ||
it('wrap + run module 3 (callback notation) - promise', (done) => { | ||
const w = wrapper.wrap(testMod3); | ||
w.run({test: 'cbsuccess'}) | ||
.then((response) => { | ||
expect(response.test).to.be.equal('cbsuccess'); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
it('wrap + run module 4 (alternate handler) - callback', (done) => { | ||
const w = wrapper.wrap(testMod4, { | ||
handler: 'myHandler' | ||
}); | ||
w4.run({test: 'cbsuccess'}, function(err, response) { | ||
expect(response.test).to.be.equal('cbsuccess'); | ||
done(); | ||
}); | ||
w.run({test: 'cbsuccess'}, (err, response) => { | ||
expect(response.test).to.be.equal('cbsuccess'); | ||
done(); | ||
}); | ||
}); | ||
it('wrap + runHandler module 5 (custom context)', function(done) { | ||
var w5 = wrapper.wrap(testMod5); | ||
w5.runHandler({test: 'cbsuccess'}, {functionName: 'testing'}, function(err, response) { | ||
expect(response.test).to.be.equal('testing'); | ||
done(); | ||
}); | ||
it('wrap + run module 4 (alternate handler) - promise', (done) => { | ||
const w = wrapper.wrap(testMod4, { | ||
handler: 'myHandler' | ||
}); | ||
w.run({test: 'cbsuccess'}) | ||
.then((response) => { | ||
expect(response.test).to.be.equal('cbsuccess'); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
it('can call lambda functions deployed in AWS', function(done) { | ||
var wLive = wrapper.wrap({ | ||
it('wrap + runHandler module 5 (custom context) - callback', (done) => { | ||
const w = wrapper.wrap(testMod5); | ||
w.runHandler({ test: 'cbsuccess' }, { functionName: 'testing' }, (err, response) => { | ||
expect(response.test).to.be.equal('testing'); | ||
done(); | ||
}); | ||
}); | ||
it('wrap + runHandler module 5 (custom context) - promise', (done) => { | ||
const w = wrapper.wrap(testMod5); | ||
w.runHandler({ test: 'cbsuccess' }, { functionName: 'testing' }) | ||
.then((response) => { | ||
expect(response.test).to.be.equal('testing'); | ||
done(); | ||
}) | ||
.catch(done); | ||
}); | ||
}); | ||
describe('lambda-wrapper live', () => { | ||
it('can call lambda functions deployed in AWS - callback', (done) => { | ||
const w = wrapper.wrap({ | ||
lambdaFunction: 'lambdaWrapper-test', | ||
region: process.env.AWS_DEFAULT_REGION || 'eu-central-1' | ||
}); | ||
wLive.run({test: 'livesuccess'}, function(err, response) { | ||
if (err) { | ||
return done(err); | ||
} | ||
expect(response.src).to.be.equal('lambda'); | ||
expect(response.event.test).to.be.equal('livesuccess'); | ||
done(); | ||
}); | ||
w.run({ test: 'livesuccess' }, (err, response) => { | ||
if (err) { | ||
return done(err); | ||
} | ||
expect(response.src).to.be.equal('lambda'); | ||
expect(response.event.test).to.be.equal('livesuccess'); | ||
done(); | ||
}); | ||
}); | ||
it('can call lambda functions deployed in AWS - promise', (done) => { | ||
const w = wrapper.wrap({ | ||
lambdaFunction: 'lambdaWrapper-test', | ||
region: process.env.AWS_DEFAULT_REGION || 'eu-central-1' | ||
}); | ||
w.run({ test: 'livesuccess' }) | ||
.then((response) => { | ||
expect(response.src).to.be.equal('lambda'); | ||
expect(response.event.test).to.be.equal('livesuccess'); | ||
done(); | ||
}).catch(done); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13378
275
61
2
1