Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Nov 11, 2017. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path_lambda.js
More file actions
Latest commit
75 lines (66 loc) · 2.14 KB
/
Copy path_lambda.js
File metadata and controls
75 lines (66 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
varwaterfall=require('run-waterfall')
varisArray=Array.isArray
varisFunction=require('lodash.isfunction')
varreject=require('lodash.reject')
varerrback=require('serialize-error')
module.exports=functionlambda(){
varfirstRun=true// important to keep this here in this closure
varargs=[].slice.call(arguments,0)// grab the args
// fail loudly for programmer not passing anything
if(args.length===0){
throwError('lambda requires at least one callback function')
}
// check for lambda([], (err, result)=>) sig
varcustomFormatter=isArray(args[0])&&isFunction(args[1])
varfmt=customFormatter? args[1] : false
varfns=fmt? args[0] : args
// we only deal in function values around here
varnotOnlyFns=reject(fns,isFunction).length>0
if(notOnlyFns){
throwError('bad argument found: lambda(...fns) or lambda([...],(err, result)=>)')
}
// returns a lambda sig
returnfunction(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)
})
}
// asummptions:
// - err should be an array of Errors
// - because lambda deals in json we need to serialize them
functionformatter(err,result){
if(fmt){
fmt(err,result,context)
}
else{
if(err){
result={
ok: false,
errors: (isArray(err)? err : [err]).map(errback)
}
}
else{
if(typeofresult==='undefined')result={}
result.ok=true
}
// 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(result)
}
}
// the real worker here
waterfall(fns,formatter)
}
}