Skip to content

Commit be736a9

Browse files
committed
release v1.1.0 / runtime version 4 - adds support for async handlers
1 parent 01ddb90 commit be736a9

File tree

5 files changed

+54
-26
lines changed

5 files changed

+54
-26
lines changed

Cakefile

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,25 @@ task 'test', 'runs test',
9292
console.log 'packaging test... ok'
9393

9494
console.log 'running test...'
95-
{ stdout, stderr } = await exec "
96-
docker run
97-
--rm
98-
-v $(PWD)/test/lambda:/var/task
99-
-v $(PWD)/test/layer:/opt
100-
lambci/lambda:provided
101-
index.handler
102-
"
10395

104-
[ result ] = stdout.split(/\n/)
96+
handlers = [ 'handlerCallback', 'handlerAsync' ]
97+
98+
results = await Promise.all handlers.map (name) ->
99+
exec "
100+
docker run
101+
--rm
102+
-v $(PWD)/test/lambda:/var/task
103+
-v $(PWD)/test/layer:/opt
104+
lambci/lambda:provided
105+
index.#{name}
106+
"
107+
105108
expected = '{"statusCode":200,"body":"{\\"message\\":\\"CoffeeScript Serverless v1.0! Your function executed successfully!\\",\\"input\\":{}}"}'
109+
outputs = (result.stdout.split(/\n/)[0] is expected for result in results)
106110

107-
passed = result is expected
108-
console.log "running test... #{if passed then 'PASS' else 'FAIL'}"
111+
console.log "running test (#{handlers[index]}) ... #{if output then 'PASS' else 'FAIL'}" for output, index in outputs
109112

110-
if not passed
113+
if not outputs.every (output) -> output
111114
console.log stderr
112115
process.exit 1
113116

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@
33
A custom runtime for AWS Lambda to execute functions in CoffeeScript.
44

55
> **Note**: This repository is essentially a CoffeeScript port and fork of
6-
[Node-Custom-Lambda](https://github.com/lambci/node-custom-lambda). The only
7-
real difference is where `Node-Custom-Lambda` expects functions to return a
8-
promise, `coffeescript-lambda-runtime` opts to stay with the traditional
9-
callback method.
6+
[Node-Custom-Lambda](https://github.com/lambci/node-custom-lambda).
107

118
> **New to CoffeeScript?**
129
> I recommend starting at https://coffeescript.org/
1310
1411
## How does it work?
15-
`CoffeeScript-lambda-runtime` works by taking care of the compiling and execution of CoffeeScript source code at time of request. This means end-users of the runtime are no longer required to compile their CoffeeScript code to javascript before uploading their functions to AWS Lambda.
12+
`CoffeeScript-lambda-runtime` works by taking care of the compiling and execution of CoffeeScript source code at time of request. This means end-users of the runtime are not required to compile their CoffeeScript code to javascript before uploading their functions to AWS Lambda.
1613

17-
Simply write your functions as you would for NodeJs and it should just work.
14+
Simply write your functions as you would for Node.js and it should just work.
15+
```coffeescript
16+
# feel alive again!
17+
exports.handler = (event, context) ->
18+
statusCode: 200,
19+
body:
20+
JSON.stringify
21+
message: 'CoffeeScript Serverless v1.0! Your function executed successfully!',
22+
input: event,
23+
```
24+
25+
alternatively, if you prefer the callback method:
1826
```coffeescript
1927
# feel alive again!
2028
exports.handler = (event, context, callback) ->
@@ -32,7 +40,7 @@ exports.handler = (event, context, callback) ->
3240

3341
Project|CoffeeScript|NodeJS|ARN|
3442
|-|-|-|-|
35-
|v1.0.0|v2.3.2|v8.10.0|arn:aws:lambda:eu-west-2:321742921541:layer:coffeescript:3|
43+
|v1.1.0|v2.3.2|v8.10.0|arn:aws:lambda:eu-west-2:321742921541:layer:coffeescript:4|
3644

3745
## Building the runtime layer
3846

bootstrap.coffee

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
http = require 'http'
2+
util = require 'util'
23

34
RUNTIME_PATH = '/2018-06-01/runtime'
45

@@ -30,16 +31,20 @@ start = () ->
3031
return process.exit 1
3132

3233
processEvents = (handler) ->
34+
isAsync = isAsyncHandler handler
3335
while true
3436
{ event, context } = await nextInvocation()
3537

3638
try
37-
result = await new Promise (resolve) ->
38-
handler event, context, (err, response) ->
39-
throw new Error err if err?
40-
resolve response
39+
if not isAsync
40+
result = await new Promise (resolve) ->
41+
handler event, context, (err, response) ->
42+
throw new Error err if err?
43+
resolve response
44+
return
4145
return
42-
return
46+
else
47+
result = await handler event, context
4348
catch e
4449
await invokeError e, context
4550
continue
@@ -164,4 +169,9 @@ toLambdaErr = ({ name, message, stack }) ->
164169
stackTrace: if stack then stack.split('\n').slice 1 else '',
165170
}
166171

172+
isAsyncHandler = (handler) ->
173+
if util.types isnt undefined then util.types.isAsyncFunction handler
174+
else if handler.constructor.name is 'AsyncFunction' then true
175+
else handler.length < 3
176+
167177
start()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "coffeescript-lambda-runtime",
33
"private": true,
4-
"version": "1.0.0",
4+
"version": "1.1.0",
55
"description": "A CoffeeScript custom runtime for aws lambda",
66
"main": "index.js",
77
"engines": {

test/lambda/index.coffee

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Test that global requires work
22
import aws4 from 'aws4'
33

4-
exports.handler = (event, context, callback) ->
4+
exports.handlerCallback = (event, context, callback) ->
55
response =
66
statusCode: 200,
77
body:
@@ -10,3 +10,10 @@ exports.handler = (event, context, callback) ->
1010
input: event,
1111

1212
callback null, response
13+
14+
exports.handlerAsync = (event, context) ->
15+
statusCode: 200,
16+
body:
17+
JSON.stringify
18+
message: 'CoffeeScript Serverless v1.0! Your function executed successfully!',
19+
input: event,

0 commit comments

Comments
 (0)