@smallwins/lambda
Advanced tools
Comparing version 4.1.0 to 4.3.0
65
index.js
@@ -1,64 +0,7 @@ | ||
var async = require('async') | ||
var _ = require('lodash') | ||
var errback = require('serialize-error') | ||
var lambda = require('./src/lambda') | ||
var dynamo = require('./src/dynamo') | ||
var sns = require('./src/sns') | ||
/** | ||
* lambda - accepts node style callbacks and returns an aws lambda function | ||
* | ||
*/ | ||
function lambda() { | ||
lambda.sources = {dynamo:dynamo, sns:sns} | ||
var firstRun = true // important to keep this here in this closure | ||
var fns = [].slice.call(arguments, 0) // grab all the functions | ||
// fail loudly for programmer not passing anything | ||
if (fns.length === 0) { | ||
throw Error('lambda requires at least one callback function') | ||
} | ||
// fail loud if the programmer passes something other than a fn | ||
var notOnlyFns = _.reject(fns, _.isFunction) | ||
if (notOnlyFns.length) { | ||
throw Error('lambda only accepts callback functions as arguments') | ||
} | ||
// returns a lambda sig | ||
return function(event, context) { | ||
// this is to avoid warm start (sometimes lambda containers are cached … yeaaaaah.) | ||
if (firstRun) { | ||
fns.unshift(function(callback) { | ||
callback(null, event) | ||
}) | ||
firstRun = false | ||
} | ||
else { | ||
// mutates! wtf. remove the cached callback | ||
fns.shift() | ||
// add the fresh event | ||
fns.unshift(function(callback) { | ||
callback(null, event) | ||
}) | ||
} | ||
// the real worker here | ||
async.waterfall(fns, function(err, result) { | ||
if (err) { | ||
// asummptions: | ||
// - err should be an array of Errors | ||
// - because lambda deals in json we need to serialize them | ||
var errors = (_.isArray(err)? err : [err]).map(errback) | ||
// deliberate use context.succeed; | ||
// there is no (good) use case for the (current) context.fail behavior (but happy to discuss in an issue)! | ||
context.succeed({ok:false, errors:errors}) | ||
} | ||
else { | ||
context.succeed(result) | ||
} | ||
}) | ||
} | ||
} | ||
lambda.sources = require('./src/sources') | ||
module.exports = lambda |
{ | ||
"name": "@smallwins/lambda", | ||
"version": "4.1.0", | ||
"version": "4.3.0", | ||
"description": "Author your AWS Lambda functions as node style errbacks.", | ||
@@ -10,3 +10,4 @@ "main": "index", | ||
"list": "./scripts/list.js", | ||
"deploy": "./scripts/deploy.js" | ||
"deploy": "./scripts/deploy.js", | ||
"invoke": "./scripts/invoke.js" | ||
}, | ||
@@ -16,3 +17,4 @@ "bin": { | ||
"lambda-list": "./scripts/list.js", | ||
"lambda-deploy": "./scripts/deploy.js" | ||
"lambda-deploy": "./scripts/deploy.js", | ||
"lambda-invoke": "./scripts/invoke.js" | ||
}, | ||
@@ -19,0 +21,0 @@ "keywords": [ |
@@ -124,2 +124,3 @@ [ ![Codeship Status for smallwins/lambda](https://codeship.com/projects/2e4082e0-d808-0133-2035-1eae90b9310e/status?branch=master)](https://codeship.com/projects/143109) | ||
console.log('save a version ', record) | ||
callback(null, record) | ||
} | ||
@@ -130,3 +131,3 @@ | ||
## app api | ||
## api | ||
@@ -137,4 +138,4 @@ - `lambda(...fns)` | ||
- `lambda.sources.dynamo.insert(...fns)` | ||
- `lambda.sources.dynamo.update(...fns)` | ||
- `lambda.sources.dynamo.destroy(...fns)` | ||
- `lambda.sources.dynamo.modify(...fns)` | ||
- `lambda.sources.dynamo.remove(...fns)` | ||
- `lambda.sources.sns(...fns)` | ||
@@ -147,8 +148,40 @@ | ||
// process event, use to pass data | ||
callback(null, event) | ||
// callback(Error('something went wrong') // pass one error | ||
// callback([Error('missing email'), Error('missing password')]) // or array of errors | ||
var result = {ok:true, event:event} | ||
callback(null, result) | ||
} | ||
``` | ||
### errors | ||
Always use `Error` type as the first parameter to callback: | ||
```javascript | ||
function fails(event, callback) { | ||
callback(Error('something went wrong') | ||
} | ||
``` | ||
Or an array of `Error`s: | ||
```javascript | ||
function fails(event, callback) { | ||
callback([ | ||
Error('missing email'), | ||
Error('missing password') | ||
]) | ||
} | ||
``` | ||
`@smallwins/lambda` serializes error into slack-rpc style JSON making them easy to work from API Gateway: | ||
```javascript | ||
{ | ||
ok: false, | ||
errors: [ | ||
{name:'Error', message:'missing email', stack'...'}, | ||
{name:'Error', message:'missing password', stack'...'} | ||
] | ||
} | ||
``` | ||
## scripting api | ||
@@ -182,2 +215,3 @@ | ||
"deploy":"AWS_PROFILE=smallwins lambda-deploy" | ||
"invoke":"AWS_PROFILE=smallwins lambda-invoke" | ||
} | ||
@@ -187,4 +221,15 @@ } | ||
- Create a new lambda `npm run create src/lambdas/forgot` | ||
- List all deployed lambdas `npm run list` | ||
- You can deploy with `npm run deploy src/lambdas/signup brian` (and the lambda will be deployed to with the alias `brian`) | ||
- `npm run create src/lambdas/forgot` creates a new lambda | ||
- `npm run list` lists all deployed lambdas | ||
- `npm run deploy src/lambdas/signup brian` deploys the lambda with the alias `brian` | ||
- `npm run invoke src/lambdas/login brian '{"email":"b@brian.io", "pswd":"..."}'` to invoke a lambda | ||
The `./scripts/invoke.js` is also a module and useful for testing. | ||
``` | ||
var invoke = require('@smallwins/lambda/scripts/invoke') | ||
invoke('path/to/lambda', alias, payload, (err, response)=> { | ||
console.log(err, response) | ||
}) | ||
``` | ||
@@ -30,4 +30,4 @@ #!/usr/bin/env node | ||
"dependencies": { | ||
"@smallwins/lambda": "^2.0.0", | ||
"@smallwins/validate": "^3.0.0" | ||
"@smallwins/lambda": ">4.0.0", | ||
"@smallwins/validate": ">3.0.0" | ||
}, | ||
@@ -62,7 +62,7 @@ "devDependencies": { | ||
var test = require('tape') | ||
var lambda = require('./') | ||
var fn = require('./') | ||
test('sanity', t=> { | ||
t.plan(1) | ||
t.ok(lambda, 'lambda exists') | ||
t.ok(fn, 'lambda function exists') | ||
}) | ||
@@ -69,0 +69,0 @@ ` |
@@ -57,4 +57,6 @@ #!/usr/bin/env node | ||
if (isUndefined(config)) { | ||
console.error('Error: package.json missing lambda') | ||
process.exit(1) | ||
// try the default role (will fail b/c this isn't an arn) | ||
package.json.lambda = { | ||
role: "lambda_basic_execution" | ||
} | ||
} | ||
@@ -61,0 +63,0 @@ |
@@ -12,3 +12,2 @@ #!/usr/bin/env node | ||
}) | ||
console.log('\n') | ||
}) |
@@ -14,3 +14,3 @@ var isArray = require('lodash').isArray | ||
succeed: function fakeSucceed(v) { | ||
t.ok(isArray(v), 'got an Errors array') | ||
t.ok(isArray(v.errors), 'got an Errors array') | ||
console.log('faked fail called with ', v) | ||
@@ -17,0 +17,0 @@ } |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
24336
15
627
230
6