From b75793c69855e338cd611b8f596f0de185a82950 Mon Sep 17 00:00:00 2001 From: Do Minh Hai <1484298+dominhhai@users.noreply.github.com> Date: Thu, 21 Dec 2023 11:50:13 +0700 Subject: [PATCH 1/5] support step-functions --- src/configure.d.ts | 2 +- src/configure.js | 4 ++-- src/event-sources/aws/dynamodb.js | 4 ++-- src/event-sources/aws/eventbridge.js | 4 ++-- src/event-sources/aws/kinesis.js | 4 ++-- src/event-sources/aws/sns.js | 4 ++-- src/event-sources/aws/sqs.js | 24 ++++++++++++------- src/event-sources/aws/step-functions.js | 32 +++++++++++++++++++++++++ src/event-sources/index.js | 3 +++ src/event-sources/utils.js | 16 +++++++++++++ 10 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 src/event-sources/aws/step-functions.js diff --git a/src/configure.d.ts b/src/configure.d.ts index acfb24c4..05891a80 100644 --- a/src/configure.d.ts +++ b/src/configure.d.ts @@ -3,7 +3,7 @@ import { Handler } from 'aws-lambda'; import { Logger } from './logger'; import Framework from './frameworks'; -type EventSources = 'AWS_SNS' | 'AWS_DYNAMODB' | 'AWS_EVENTBRIDGE' | 'AWS_SQS' | 'AWS_KINESIS_DATA_STREAM' | 'AWS_S3'; +type EventSources = 'AWS_SNS' | 'AWS_DYNAMODB' | 'AWS_EVENTBRIDGE' | 'AWS_SQS' | 'AWS_KINESIS_DATA_STREAM' | 'AWS_S3' | 'AWS_STEP_FUNCTIONS'; interface EventSource { getRequest?: any; // TODO: diff --git a/src/configure.js b/src/configure.js index 02dbaa3a..51ed8e11 100644 --- a/src/configure.js +++ b/src/configure.js @@ -57,7 +57,7 @@ function configure ({ } setCurrentInvoke({ event, context }) - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { const promise = { resolve, reject @@ -70,7 +70,7 @@ function configure ({ }) try { - forwardRequestToNodeServer({ + await forwardRequestToNodeServer({ app, framework, event, diff --git a/src/event-sources/aws/dynamodb.js b/src/event-sources/aws/dynamodb.js index 014f44a1..15ee5188 100644 --- a/src/event-sources/aws/dynamodb.js +++ b/src/event-sources/aws/dynamodb.js @@ -1,4 +1,4 @@ -const { emptyResponseMapper } = require('../utils') +const { emptyResponseMapperToEventSource } = require('../utils') const getRequestValuesFromDynamoDB = ({ event }) => { const method = 'POST' @@ -14,5 +14,5 @@ const getRequestValuesFromDynamoDB = ({ event }) => { module.exports = { getRequest: getRequestValuesFromDynamoDB, - getResponse: emptyResponseMapper + getResponse: emptyResponseMapperToEventSource } diff --git a/src/event-sources/aws/eventbridge.js b/src/event-sources/aws/eventbridge.js index 1b719fd0..3d1e12cd 100644 --- a/src/event-sources/aws/eventbridge.js +++ b/src/event-sources/aws/eventbridge.js @@ -1,4 +1,4 @@ -const { emptyResponseMapper } = require('../utils') +const { emptyResponseMapperToEventSource } = require('../utils') const getRequestValuesFromEventBridge = ({ event }) => { const method = 'POST' @@ -14,5 +14,5 @@ const getRequestValuesFromEventBridge = ({ event }) => { module.exports = { getRequest: getRequestValuesFromEventBridge, - getResponse: emptyResponseMapper + getResponse: emptyResponseMapperToEventSource } diff --git a/src/event-sources/aws/kinesis.js b/src/event-sources/aws/kinesis.js index ecbdc0e0..88b19304 100644 --- a/src/event-sources/aws/kinesis.js +++ b/src/event-sources/aws/kinesis.js @@ -1,4 +1,4 @@ -const { emptyResponseMapper } = require('../utils') +const { emptyResponseMapperToEventSource } = require('../utils') const getRequestValuesFromKinesis = ({ event }) => { const method = 'POST' @@ -14,5 +14,5 @@ const getRequestValuesFromKinesis = ({ event }) => { module.exports = { getRequest: getRequestValuesFromKinesis, - getResponse: emptyResponseMapper + getResponse: emptyResponseMapperToEventSource } diff --git a/src/event-sources/aws/sns.js b/src/event-sources/aws/sns.js index 8d8de2d0..0eac8392 100644 --- a/src/event-sources/aws/sns.js +++ b/src/event-sources/aws/sns.js @@ -1,4 +1,4 @@ -const { emptyResponseMapper } = require('../utils') +const { emptyResponseMapperToEventSource } = require('../utils') const getRequestValuesFromSns = ({ event }) => { const method = 'POST' @@ -14,5 +14,5 @@ const getRequestValuesFromSns = ({ event }) => { module.exports = { getRequest: getRequestValuesFromSns, - getResponse: emptyResponseMapper + getResponse: emptyResponseMapperToEventSource } diff --git a/src/event-sources/aws/sqs.js b/src/event-sources/aws/sqs.js index 457285c4..c479d55a 100644 --- a/src/event-sources/aws/sqs.js +++ b/src/event-sources/aws/sqs.js @@ -1,18 +1,24 @@ const getRequestValuesFromSqs = ({ event }) => { - const method = 'POST' - const headers = { host: 'sqs.amazonaws.com' } - const body = event + const method = "POST"; + const headers = { host: "sqs.amazonaws.com" }; + const body = event; return { method, headers, - body - } -} + body, + }; +}; -const getResponseToSqs = ({ body }) => JSON.parse(body) +const getResponseToSqs = ({ statusCode, body }) => { + if (statusCode >= 400) { + throw new Error(body); + } + + return JSON.parse(body); +}; module.exports = { getRequest: getRequestValuesFromSqs, - getResponse: getResponseToSqs -} + getResponse: getResponseToSqs, +}; diff --git a/src/event-sources/aws/step-functions.js b/src/event-sources/aws/step-functions.js new file mode 100644 index 00000000..6415e1fd --- /dev/null +++ b/src/event-sources/aws/step-functions.js @@ -0,0 +1,32 @@ +const getRequestValuesFromStepFunctions = ({ event }) => { + const method = "POST"; + const headers = { host: "stepfunctions.amazonaws.com" }; + const body = event; + + return { + method, + headers, + body, + }; +}; + +function getResponseToStepFunctions({ + statusCode, + body, + isBase64Encoded = false, +}) { + if (statusCode >= 400) { + throw new Error(body); + } + + if (isBase64Encoded) { + throw new Error("base64 encoding is not supported"); + } + + return JSON.parse(body); +} + +module.exports = { + getRequest: getRequestValuesFromStepFunctions, + getResponse: getResponseToStepFunctions, +}; diff --git a/src/event-sources/index.js b/src/event-sources/index.js index cff31555..ef7e6749 100644 --- a/src/event-sources/index.js +++ b/src/event-sources/index.js @@ -10,6 +10,7 @@ const azureHttpFunctionV4EventSource = require('./azure/http-function-runtime-v4 const awsEventBridgeEventSource = require('./aws/eventbridge') const awsKinesisEventSource = require('./aws/kinesis') const awsS3 = require('./aws/s3') +const awsStepFunctionsEventSource = require('./aws/step-functions') function getEventSource ({ eventSourceName }) { switch (eventSourceName) { @@ -37,6 +38,8 @@ function getEventSource ({ eventSourceName }) { return awsKinesisEventSource case 'AWS_S3': return awsS3 + case 'AWS_STEP_FUNCTIONS': + return awsStepFunctionsEventSource default: throw new Error('Couldn\'t detect valid event source.') } diff --git a/src/event-sources/utils.js b/src/event-sources/utils.js index 85669116..2152c3b5 100644 --- a/src/event-sources/utils.js +++ b/src/event-sources/utils.js @@ -127,6 +127,10 @@ function getEventSourceNameBasedOnEvent ({ return 'AWS_EVENTBRIDGE' } + if (event.context && event.context.Execution && event.context.State && event.context.StateMachine) { + return 'AWS_STEP_FUNCTIONS' + } + throw new Error('Unable to determine event source based on event.') } @@ -148,6 +152,17 @@ function getCommaDelimitedHeaders ({ headersMap, separator = ',', lowerCaseKey = const emptyResponseMapper = () => {} +function emptyResponseMapperToEventSource({ + statusCode, + body, +}) { + if (statusCode >= 400) { + throw new Error(body) + } + + return emptyResponseMapper() +} + const parseCookie = (str) => str.split(';') .map((v) => v.split('=')) @@ -167,5 +182,6 @@ module.exports = { getEventBody, getCommaDelimitedHeaders, emptyResponseMapper, + emptyResponseMapperToEventSource, parseCookie } From 29e91c08ee004f31eb4d4e4a85b03188b2a1d608 Mon Sep 17 00:00:00 2001 From: Do Minh Hai <1484298+dominhhai@users.noreply.github.com> Date: Thu, 21 Dec 2023 11:54:44 +0700 Subject: [PATCH 2/5] add docs for sfn --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ffce6c34..ca63951c 100644 --- a/README.md +++ b/README.md @@ -281,6 +281,8 @@ serverlessExpress({ 'AWS_SQS': '/sqs' 'AWS_EVENTBRIDGE': '/eventbridge', 'AWS_KINESIS_DATA_STREAM': '/kinesis', + 'AWS_S3': '/s3', + 'AWS_STEP_FUNCTIONS': '/step-functions', } }) ``` From 1d94a8fef89d710a279e25fb3e67a95d03dd3415 Mon Sep 17 00:00:00 2001 From: Do Minh Hai <1484298+dominhhai@users.noreply.github.com> Date: Thu, 21 Dec 2023 13:22:28 +0700 Subject: [PATCH 3/5] fix coding convention style --- src/event-sources/aws/sqs.js | 20 ++++++++++---------- src/event-sources/aws/step-functions.js | 18 +++++++++--------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/event-sources/aws/sqs.js b/src/event-sources/aws/sqs.js index c479d55a..8b8e8a46 100644 --- a/src/event-sources/aws/sqs.js +++ b/src/event-sources/aws/sqs.js @@ -1,24 +1,24 @@ const getRequestValuesFromSqs = ({ event }) => { - const method = "POST"; - const headers = { host: "sqs.amazonaws.com" }; - const body = event; + const method = 'POST' + const headers = { host: 'sqs.amazonaws.com' } + const body = event return { method, headers, body, - }; -}; + } +} const getResponseToSqs = ({ statusCode, body }) => { if (statusCode >= 400) { - throw new Error(body); + throw new Error(body) } - - return JSON.parse(body); -}; + + return JSON.parse(body) +} module.exports = { getRequest: getRequestValuesFromSqs, getResponse: getResponseToSqs, -}; +} diff --git a/src/event-sources/aws/step-functions.js b/src/event-sources/aws/step-functions.js index 6415e1fd..e4913624 100644 --- a/src/event-sources/aws/step-functions.js +++ b/src/event-sources/aws/step-functions.js @@ -1,14 +1,14 @@ const getRequestValuesFromStepFunctions = ({ event }) => { - const method = "POST"; - const headers = { host: "stepfunctions.amazonaws.com" }; - const body = event; + const method = 'POST' + const headers = { host: 'stepfunctions.amazonaws.com' } + const body = event return { method, headers, body, - }; -}; + } +} function getResponseToStepFunctions({ statusCode, @@ -16,17 +16,17 @@ function getResponseToStepFunctions({ isBase64Encoded = false, }) { if (statusCode >= 400) { - throw new Error(body); + throw new Error(body) } if (isBase64Encoded) { - throw new Error("base64 encoding is not supported"); + throw new Error('base64 encoding is not supported') } - return JSON.parse(body); + return JSON.parse(body) } module.exports = { getRequest: getRequestValuesFromStepFunctions, getResponse: getResponseToStepFunctions, -}; +} From 93332b072cb32c1fff8d3350051f911d92c4d28a Mon Sep 17 00:00:00 2001 From: Do Minh Hai <1484298+dominhhai@users.noreply.github.com> Date: Thu, 21 Dec 2023 15:18:27 +0700 Subject: [PATCH 4/5] fix lint & revert throw base on http-code --- src/configure.js | 4 ++-- src/event-sources/aws/dynamodb.js | 4 ++-- src/event-sources/aws/eventbridge.js | 4 ++-- src/event-sources/aws/kinesis.js | 4 ++-- src/event-sources/aws/sns.js | 4 ++-- src/event-sources/aws/sqs.js | 4 ++-- src/event-sources/aws/step-functions.js | 14 +++++--------- .../azure/http-function-runtime-v4.js | 4 ++-- src/event-sources/utils.js | 12 ------------ 9 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/configure.js b/src/configure.js index 51ed8e11..02dbaa3a 100644 --- a/src/configure.js +++ b/src/configure.js @@ -57,7 +57,7 @@ function configure ({ } setCurrentInvoke({ event, context }) - return new Promise(async (resolve, reject) => { + return new Promise((resolve, reject) => { const promise = { resolve, reject @@ -70,7 +70,7 @@ function configure ({ }) try { - await forwardRequestToNodeServer({ + forwardRequestToNodeServer({ app, framework, event, diff --git a/src/event-sources/aws/dynamodb.js b/src/event-sources/aws/dynamodb.js index 15ee5188..014f44a1 100644 --- a/src/event-sources/aws/dynamodb.js +++ b/src/event-sources/aws/dynamodb.js @@ -1,4 +1,4 @@ -const { emptyResponseMapperToEventSource } = require('../utils') +const { emptyResponseMapper } = require('../utils') const getRequestValuesFromDynamoDB = ({ event }) => { const method = 'POST' @@ -14,5 +14,5 @@ const getRequestValuesFromDynamoDB = ({ event }) => { module.exports = { getRequest: getRequestValuesFromDynamoDB, - getResponse: emptyResponseMapperToEventSource + getResponse: emptyResponseMapper } diff --git a/src/event-sources/aws/eventbridge.js b/src/event-sources/aws/eventbridge.js index 3d1e12cd..1b719fd0 100644 --- a/src/event-sources/aws/eventbridge.js +++ b/src/event-sources/aws/eventbridge.js @@ -1,4 +1,4 @@ -const { emptyResponseMapperToEventSource } = require('../utils') +const { emptyResponseMapper } = require('../utils') const getRequestValuesFromEventBridge = ({ event }) => { const method = 'POST' @@ -14,5 +14,5 @@ const getRequestValuesFromEventBridge = ({ event }) => { module.exports = { getRequest: getRequestValuesFromEventBridge, - getResponse: emptyResponseMapperToEventSource + getResponse: emptyResponseMapper } diff --git a/src/event-sources/aws/kinesis.js b/src/event-sources/aws/kinesis.js index 88b19304..ecbdc0e0 100644 --- a/src/event-sources/aws/kinesis.js +++ b/src/event-sources/aws/kinesis.js @@ -1,4 +1,4 @@ -const { emptyResponseMapperToEventSource } = require('../utils') +const { emptyResponseMapper } = require('../utils') const getRequestValuesFromKinesis = ({ event }) => { const method = 'POST' @@ -14,5 +14,5 @@ const getRequestValuesFromKinesis = ({ event }) => { module.exports = { getRequest: getRequestValuesFromKinesis, - getResponse: emptyResponseMapperToEventSource + getResponse: emptyResponseMapper } diff --git a/src/event-sources/aws/sns.js b/src/event-sources/aws/sns.js index 0eac8392..8d8de2d0 100644 --- a/src/event-sources/aws/sns.js +++ b/src/event-sources/aws/sns.js @@ -1,4 +1,4 @@ -const { emptyResponseMapperToEventSource } = require('../utils') +const { emptyResponseMapper } = require('../utils') const getRequestValuesFromSns = ({ event }) => { const method = 'POST' @@ -14,5 +14,5 @@ const getRequestValuesFromSns = ({ event }) => { module.exports = { getRequest: getRequestValuesFromSns, - getResponse: emptyResponseMapperToEventSource + getResponse: emptyResponseMapper } diff --git a/src/event-sources/aws/sqs.js b/src/event-sources/aws/sqs.js index 8b8e8a46..4e0a0bb2 100644 --- a/src/event-sources/aws/sqs.js +++ b/src/event-sources/aws/sqs.js @@ -6,7 +6,7 @@ const getRequestValuesFromSqs = ({ event }) => { return { method, headers, - body, + body } } @@ -20,5 +20,5 @@ const getResponseToSqs = ({ statusCode, body }) => { module.exports = { getRequest: getRequestValuesFromSqs, - getResponse: getResponseToSqs, + getResponse: getResponseToSqs } diff --git a/src/event-sources/aws/step-functions.js b/src/event-sources/aws/step-functions.js index e4913624..c7c3a67b 100644 --- a/src/event-sources/aws/step-functions.js +++ b/src/event-sources/aws/step-functions.js @@ -6,19 +6,15 @@ const getRequestValuesFromStepFunctions = ({ event }) => { return { method, headers, - body, + body } } -function getResponseToStepFunctions({ +const getResponseToStepFunctions = ({ statusCode, body, - isBase64Encoded = false, -}) { - if (statusCode >= 400) { - throw new Error(body) - } - + isBase64Encoded = false +}) => { if (isBase64Encoded) { throw new Error('base64 encoding is not supported') } @@ -28,5 +24,5 @@ function getResponseToStepFunctions({ module.exports = { getRequest: getRequestValuesFromStepFunctions, - getResponse: getResponseToStepFunctions, + getResponse: getResponseToStepFunctions } diff --git a/src/event-sources/azure/http-function-runtime-v4.js b/src/event-sources/azure/http-function-runtime-v4.js index 3aa86408..d3abc66a 100644 --- a/src/event-sources/azure/http-function-runtime-v4.js +++ b/src/event-sources/azure/http-function-runtime-v4.js @@ -1,6 +1,6 @@ const { getRequest, getResponse } = require('./http-function-runtime-v3') module.exports = { - getRequest: getRequest, - getResponse: getResponse + getRequest, + getResponse } diff --git a/src/event-sources/utils.js b/src/event-sources/utils.js index 2152c3b5..cc03f1b1 100644 --- a/src/event-sources/utils.js +++ b/src/event-sources/utils.js @@ -152,17 +152,6 @@ function getCommaDelimitedHeaders ({ headersMap, separator = ',', lowerCaseKey = const emptyResponseMapper = () => {} -function emptyResponseMapperToEventSource({ - statusCode, - body, -}) { - if (statusCode >= 400) { - throw new Error(body) - } - - return emptyResponseMapper() -} - const parseCookie = (str) => str.split(';') .map((v) => v.split('=')) @@ -182,6 +171,5 @@ module.exports = { getEventBody, getCommaDelimitedHeaders, emptyResponseMapper, - emptyResponseMapperToEventSource, parseCookie } From 4a09648e3c9544b287cb53462595513190cf557f Mon Sep 17 00:00:00 2001 From: Do Minh Hai <1484298+dominhhai@users.noreply.github.com> Date: Thu, 21 Dec 2023 15:19:42 +0700 Subject: [PATCH 5/5] revert sqs handler --- src/event-sources/aws/sqs.js | 8 +------- src/event-sources/aws/step-functions.js | 1 - 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/event-sources/aws/sqs.js b/src/event-sources/aws/sqs.js index 4e0a0bb2..457285c4 100644 --- a/src/event-sources/aws/sqs.js +++ b/src/event-sources/aws/sqs.js @@ -10,13 +10,7 @@ const getRequestValuesFromSqs = ({ event }) => { } } -const getResponseToSqs = ({ statusCode, body }) => { - if (statusCode >= 400) { - throw new Error(body) - } - - return JSON.parse(body) -} +const getResponseToSqs = ({ body }) => JSON.parse(body) module.exports = { getRequest: getRequestValuesFromSqs, diff --git a/src/event-sources/aws/step-functions.js b/src/event-sources/aws/step-functions.js index c7c3a67b..b785aaef 100644 --- a/src/event-sources/aws/step-functions.js +++ b/src/event-sources/aws/step-functions.js @@ -11,7 +11,6 @@ const getRequestValuesFromStepFunctions = ({ event }) => { } const getResponseToStepFunctions = ({ - statusCode, body, isBase64Encoded = false }) => {