Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Added support for AWS API Gateway v2 payloads #2191

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
673 changes: 664 additions & 9 deletions THIRD_PARTY_NOTICES.md

Large diffs are not rendered by default.

49 changes: 46 additions & 3 deletions lib/serverless/api-gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ class LambdaProxyWebRequest {

this.headers = normalizeHeaders(event)
this.url = {
path: event.path,
path: '',
port: lowerCaseHeaders['x-forwarded-port'],
requestParameters: normalizeQueryStringParameters(event)
}
this.method = event.httpMethod
this.method = ''

if (isGatewayV1Event(event) === true) {
this.url.path = event.path
this.method = event.httpMethod
} else if (isGatewayV2Event(event) === true) {
this.url.path = event.requestContext.http.path
this.method = event.requestContext.http.method
}

this.transportType = lowerCaseHeaders['x-forwarded-proto']
}
}
Expand Down Expand Up @@ -94,7 +103,41 @@ function normalizeHeaders(event, lowerCaseKey = false) {
* to create a web transaction.
*/
function isLambdaProxyEvent(event) {
return !!(event.path && (event.headers ?? event.multiValueHeaders) && event.httpMethod)
return isGatewayV1Event(event) || isGatewayV2Event(event)
}

function isGatewayV1Event(event) {
let result = false

if (event?.version === '1.0') {
result = true
} else if (
typeof event?.path === 'string' &&
(event.headers ?? event.multiValueHeaders) &&
typeof event?.httpMethod === 'string'
// eslint-disable-next-line sonarjs/no-duplicated-branches
) {
result = true
}

return result
}

function isGatewayV2Event(event) {
let result = false

if (event?.version === '2.0') {
result = true
} else if (
typeof event?.requestContext?.http?.path === 'string' &&
Object.prototype.toString.call(event.headers) === '[object Object]' &&
typeof event?.requestContext?.http?.method === 'string'
// eslint-disable-next-line sonarjs/no-duplicated-branches
) {
result = true
}

return result
}

/**
Expand Down
29 changes: 28 additions & 1 deletion lib/serverless/event-sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"requestContext.elb"
]
},

"apiGateway": {
"attributes": {
"aws.lambda.eventSource.accountId": "requestContext.accountId",
Expand All @@ -24,13 +25,32 @@
"requestContext.stage"
]
},

"apiGatewayV2": {
"attributes": {
"aws.lambda.eventSource.accountId": "requestContext.accountId",
"aws.lambda.eventSource.apiId": "requestContext.apiId",
"aws.lambda.eventSource.stage": "requestContext.stage"
},
Comment on lines +30 to +34
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"resourceId" and "resourcePath", found in the v1 payload, are not present in v2 payloads.

"name": "apiGatewayV2",
"required_keys": [
"version",
"headers",
"requestContext.http",
"requestContext.http.path",
"requestContext.http.method",
"requestContext.stage"
]
},

"cloudFront": {
"attributes": {},
"name": "cloudFront",
"required_keys": [
"Records[0].cf"
]
},

"cloudWatchScheduled": {
"attributes": {
"aws.lambda.eventSource.account": "account",
Expand All @@ -45,6 +65,7 @@
"source"
]
},

"dynamoStreams": {
"attributes": {
"aws.lambda.eventSource.length": "Records.length"
Expand All @@ -54,6 +75,7 @@
"Records[0].dynamodb"
]
},

"firehose": {
"attributes": {
"aws.lambda.eventSource.length": "records.length",
Expand All @@ -65,6 +87,7 @@
"records[0].kinesisRecordMetadata"
]
},

"kinesis": {
"attributes": {
"aws.lambda.eventSource.length": "Records.length",
Expand All @@ -75,6 +98,7 @@
"Records[0].kinesis"
]
},

"s3": {
"attributes": {
"aws.lambda.eventSource.bucketName": "Records[0].s3.bucket.name",
Expand All @@ -91,6 +115,7 @@
"Records[0].s3"
]
},

"ses": {
"attributes": {
"aws.lambda.eventSource.date": "Records[0].ses.mail.commonHeaders.date",
Expand All @@ -103,6 +128,7 @@
"Records[0].ses"
]
},

"sns": {
"attributes": {
"aws.lambda.eventSource.length": "Records.length",
Expand All @@ -116,6 +142,7 @@
"Records[0].Sns"
]
},

"sqs": {
"attributes": {
"aws.lambda.eventSource.length": "Records.length"
Expand All @@ -125,4 +152,4 @@
"Records[0].receiptHandle"
]
}
}
}
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
"proxy": "^2.1.1",
"proxyquire": "^1.8.0",
"q": "*",
"rfdc": "^1.3.1",
"rimraf": "^2.6.3",
"should": "*",
"sinon": "^4.5.0",
Expand Down
Loading
Loading