@vendia/serverless-express
Advanced tools
Comparing version 4.3.12 to 4.4.0
{ | ||
"name": "@vendia/serverless-express", | ||
"version": "4.3.12", | ||
"version": "4.4.0", | ||
"description": "This library enables you to utilize AWS Lambda and Amazon API Gateway to respond to web and API requests using your existing Node.js application framework.", | ||
@@ -65,3 +65,4 @@ "keywords": [ | ||
"testPathIgnorePatterns": [ | ||
"examples/nestjs" | ||
"examples/nestjs", | ||
"examples/basic-starter-api-gateway-v2-typescript" | ||
] | ||
@@ -68,0 +69,0 @@ }, |
@@ -173,2 +173,46 @@ # Serverless Express by [Vendia](https://vendia.net/) | ||
#### eventSourceRoutes | ||
Introduced in `@vendia/serverless-express@4.4.0` native support for `aws:sns` and `aws:dynamodb` events were introduced. | ||
A single function can be configured to handle events from SNS and DynamoDB, as well as the previously supported events. | ||
Assuming the following function configuration in `serverless.yml`: | ||
```yaml | ||
functions: | ||
lambda-handler: | ||
handler: src/lambda.handler | ||
events: | ||
- http: | ||
path: / | ||
method: get | ||
- sns: | ||
topicName: my-topic | ||
- stream: | ||
type: dynamodb | ||
arn: arn:aws:dynamodb:us-east-1:012345678990:table/my-table/stream/2021-07-15T15:05:51.683 | ||
``` | ||
And the following configuration: | ||
```js | ||
serverlessExpress({ | ||
app, | ||
eventSourceRoutes: { | ||
'AWS_SNS': '/sns', | ||
'AWS_DYNAMODB': '/dynamodb' | ||
} | ||
}) | ||
``` | ||
Events from SNS and DynamoDB will `POST` to the routes configured in Express to handle `/sns` and `/dynamodb`, | ||
respectively. | ||
Also, to ensure the events propagated from an internal event and not externally, it is **highly recommended** to | ||
ensure the `Host` header matches: | ||
- SNS: `sns.amazonaws.com` | ||
- DynamoDB: `dynamodb.amazonaws.com` | ||
### logSettings | ||
@@ -175,0 +219,0 @@ |
@@ -5,2 +5,4 @@ import { RequestListener } from "http"; | ||
type EventSources = "AWS_SNS" | "AWS_DYNAMODB"; | ||
interface EventSource { | ||
@@ -25,2 +27,3 @@ getRequest?: any; // TODO: | ||
eventSource?: EventSource; // TODO: | ||
eventSourceRoutes?: { [key in EventSources]?: string }; | ||
} | ||
@@ -27,0 +30,0 @@ |
@@ -28,2 +28,3 @@ const util = require('util') | ||
eventSource: configureEventFns, | ||
eventSourceRoutes: configureEventSourceRoutes, | ||
respondWithErrors: configureRespondWithErrors = process.env.NODE_ENV === 'development' | ||
@@ -42,2 +43,3 @@ } = {}) { | ||
eventSource = configureEventFns || getEventSource({ eventSourceName }), | ||
eventSourceRoutes = configureEventSourceRoutes || {}, | ||
log = configureLog, | ||
@@ -82,2 +84,3 @@ respondWithErrors = configureRespondWithErrors | ||
eventSource, | ||
eventSourceRoutes, | ||
log | ||
@@ -91,2 +94,3 @@ }) | ||
respondWithErrors, | ||
eventSourceName, | ||
eventSource | ||
@@ -93,0 +97,0 @@ }) |
@@ -5,2 +5,4 @@ const awsApiGatewayV1EventSource = require('./aws/api-gateway-v1') | ||
const awsLambdaEdgeEventSource = require('./aws/lambda-edge') | ||
const awsSnsEventSource = require('./aws/sns') | ||
const awsDynamoDbEventSource = require('./aws/dynamodb') | ||
@@ -17,2 +19,6 @@ function getEventSource ({ eventSourceName }) { | ||
return awsLambdaEdgeEventSource | ||
case 'AWS_DYNAMODB': | ||
return awsDynamoDbEventSource | ||
case 'AWS_SNS': | ||
return awsSnsEventSource | ||
default: | ||
@@ -19,0 +25,0 @@ throw new Error('Couldn\'t detect valid event source.') |
@@ -74,3 +74,12 @@ const url = require('url') | ||
if (event.requestContext && event.requestContext.elb) return 'AWS_ALB' | ||
if (event.Records) return 'AWS_LAMBDA_EDGE' | ||
if (event.Records) { | ||
const eventSource = event.Records[0] ? event.Records[0].EventSource || event.Records[0].eventSource : undefined | ||
if (eventSource === 'aws:sns') { | ||
return 'AWS_SNS' | ||
} | ||
if (eventSource === 'aws:dynamodb') { | ||
return 'AWS_DYNAMODB' | ||
} | ||
return 'AWS_LAMBDA_EDGE' | ||
} | ||
if (event.requestContext) { | ||
@@ -99,2 +108,4 @@ return event.version === '2.0' ? 'AWS_API_GATEWAY_V2' : 'AWS_API_GATEWAY_V1' | ||
const emptyResponseMapper = () => {} | ||
module.exports = { | ||
@@ -106,3 +117,4 @@ getPathWithQueryStringParams, | ||
getEventBody, | ||
getCommaDelimitedHeaders | ||
getCommaDelimitedHeaders, | ||
emptyResponseMapper | ||
} |
@@ -12,2 +12,7 @@ function makeResolver ({ | ||
if (resolutionMode === 'PROMISE') return promise.resolve(response) | ||
}, | ||
fail: ({ error }) => { | ||
if (resolutionMode === 'CONTEXT') return context.fail(error) | ||
if (resolutionMode === 'CALLBACK') return callback(error, null) | ||
if (resolutionMode === 'PROMISE') return promise.reject(error) | ||
} | ||
@@ -14,0 +19,0 @@ } |
@@ -55,2 +55,3 @@ const util = require('util') | ||
respondWithErrors, | ||
eventSourceName, | ||
eventSource | ||
@@ -60,2 +61,12 @@ }) { | ||
if ( | ||
eventSourceName !== 'AWS_ALB' && | ||
eventSourceName !== 'AWS_LAMBDA_EDGE' && | ||
eventSourceName !== 'AWS_API_GATEWAY_V1' && | ||
eventSourceName !== 'AWS_API_GATEWAY_V2' | ||
) { | ||
resolver.fail({ error }) | ||
return | ||
} | ||
const body = respondWithErrors ? error.stack : '' | ||
@@ -133,5 +144,11 @@ const errorResponse = eventSource.getResponse({ | ||
eventSource = getEventSource({ eventSourceName }), | ||
eventSourceRoutes, | ||
log | ||
}) { | ||
const requestValues = eventSource.getRequest({ event, context, log }) | ||
if (!requestValues.path && eventSourceRoutes[eventSourceName]) { | ||
requestValues.path = eventSourceRoutes[eventSourceName] | ||
} | ||
log.debug('SERVERLESS_EXPRESS:FORWARD_REQUEST_TO_NODE_SERVER:REQUEST_VALUES', { requestValues }) | ||
@@ -138,0 +155,0 @@ const { request, response } = await getRequestResponse(requestValues) |
58775
34
1060
290