CodeGenieApp / CodeGenieApp/serverless-express

Content-Length header is a number instead of a string which causes issues downstream

Open
#685 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
5.3k
Forks
676
PR merge metrics
No merged PRs in 30d

Description

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used [patch-package](https://github.com/ds300/patch-package) to patch `@vendia/serverless-express@4.12.6` for the project I'm working on.

My issue is with apollo-server as described in more detail [here](https://github.com/apollographql/apollo-server/issues/7289). However, other people experience a similar issue with ALB such as the case described [here](https://github.com/CodeGenieApp/serverless-express/issues/563). These issues have existed for a long time so I figured it is about time this gets addressed. People have been forced to switch to [H4ad/serverless-adapter](https://github.com/H4ad/serverless-adapter) instead in the meantime.

I will be putting up a PR for this issue shortly and will link to this issue. Until then, here is the diff that solved my problem:

```diff
diff --git a/node_modules/@vendia/serverless-express/src/event-sources/aws/api-gateway-v2.js b/node_modules/@vendia/serverless-express/src/event-sources/aws/api-gateway-v2.js
index 56896c1..c2136cf 100644
--- a/node_modules/@vendia/serverless-express/src/event-sources/aws/api-gateway-v2.js
+++ b/node_modules/@vendia/serverless-express/src/event-sources/aws/api-gateway-v2.js
@@ -33,7 +33,7 @@ function getRequestValuesFromApiGatewayEvent ({ event }) {
if (event.body) {
body = getEventBody({ event })
const isBase64Encoded = event.isBase64Encoded
- headers['content-length'] = Buffer.byteLength(body, isBase64Encoded ? 'base64' : 'utf8')
+ headers['content-length'] = Buffer.byteLength(body, isBase64Encoded ? 'base64' : 'utf8').toString()
}

return {
diff --git a/node_modules/@vendia/serverless-express/src/event-sources/aws/lambda-edge.js b/node_modules/@vendia/serverless-express/src/event-sources/aws/lambda-edge.js
index 217fbe9..ebc135e 100644
--- a/node_modules/@vendia/serverless-express/src/event-sources/aws/lambda-edge.js
+++ b/node_modules/@vendia/serverless-express/src/event-sources/aws/lambda-edge.js
@@ -28,7 +28,7 @@ function getRequestValuesFromLambdaEdgeEvent ({ event }) {
body: requestBodyObject.data,
isBase64Encoded
})
- headers['content-length'] = Buffer.byteLength(body, isBase64Encoded ? 'base64' : 'utf8')
+ headers['content-length'] = Buffer.byteLength(body, isBase64Encoded ? 'base64' : 'utf8').toString()
}

const path = url.format({
diff --git a/node_modules/@vendia/serverless-express/src/event-sources/azure/http-function-runtime-v3.js b/node_modules/@vendia/serverless-express/src/event-sources/azure/http-function-runtime-v3.js
index c5c887e..57c464a 100644
--- a/node_modules/@vendia/serverless-express/src/event-sources/azure/http-function-runtime-v3.js
+++ b/node_modules/@vendia/serverless-express/src/event-sources/azure/http-function-runtime-v3.js
@@ -17,7 +17,7 @@ function getRequestValuesFromHttpFunctionEvent ({ event }) {

const body = context.rawBody
if (body) {
- headers['content-length'] = Buffer.byteLength(body, 'utf8')
+ headers['content-length'] = Buffer.byteLength(body, 'utf8').toString()
}

return {
diff --git a/node_modules/@vendia/serverless-express/src/event-sources/utils.js b/node_modules/@vendia/serverless-express/src/event-sources/utils.js
index c5537e1..accd919 100644
--- a/node_modules/@vendia/serverless-express/src/event-sources/utils.js
+++ b/node_modules/@vendia/serverless-express/src/event-sources/utils.js
@@ -41,7 +41,7 @@ function getRequestValuesFromEvent ({
if (event.body) {
body = getEventBody({ event })
const { isBase64Encoded } = event
- headers['content-length'] = Buffer.byteLength(body, isBase64Encoded ? 'base64' : 'utf8')
+ headers['content-length'] = Buffer.byteLength(body, isBase64Encoded ? 'base64' : 'utf8').toString()
}

const remoteAddress = (event && event.requestContext && event.requestContext.identity && event.requestContext.identity.sourceIp) || ''
```

This issue body was [partially generated by patch-package](https://github.com/ds300/patch-package/issues/296).

Contributor guide

Open the contributing guide

Research direction

Start by reading the four mentioned event-source files: api-gateway-v2.js, lambda-edge.js, http-function-runtime-v3.js, and utils.js. Compare their Content-Length handling with the supplied diff and verify that each affected header has the expected string type; the issue provides no test file to run.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, azure, javascript, node.js
Domain
api, backend, cloud
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.