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: detect requests without body correctly #5290

Merged
merged 5 commits into from
Dec 6, 2022
Merged

fix: detect requests without body correctly #5290

merged 5 commits into from
Dec 6, 2022

Conversation

danez
Copy link
Contributor

@danez danez commented Dec 3, 2022

if a request goes through an edge function the content-length header will be removed and instead the transfer-enconding is set to chunked. Our detection in the functions (non-edge) server was detecting these passthrough requests as requests without body, because of the missing content-length. content-length should be ignored anyways when transfer-encoding is used according to the HTTP/1.1 spec.

Fixes #5030

This also changes request.get(...) and request.headers[...] to request.header(...) for readability.

red-fox-in-winter-forest-royalty-free-image-1610492147

@danez danez self-assigned this Dec 3, 2022
@danez danez requested a review from a team December 3, 2022 12:12
@github-actions
Copy link

github-actions bot commented Dec 3, 2022

📊 Benchmark results

Comparing with fd7db45

Package size: 247 MB

(no change)

^  247 MB  247 MB  247 MB  247 MB  247 MB  247 MB 
│   ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐    ┌──┐  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-5     T-4     T-3     T-2     T-1      T    
Legend

if a request goes through an edge function the content-length header
will not be set and instead the transfer-enconding is set to chunked.
@@ -41,6 +41,13 @@ const buildClientContext = function (headers) {
}
}

const hasBody = (req) =>
// copied from is-type package
(req.header('transfer-encoding') !== undefined || !Number.isNaN(req.header('content-length'))) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Why're we specifically checking for the NaN value here?

In the type-is package the global isNaN function is used which behaves differently from the Number.isNaN function since the global isNaN is true for any non-numeric value (strings, undefined, null etc.) while the Number.isNaN is only true for the NaN value & nothing else.

Ref: https://github.com/jshttp/type-is/blob/7d19b7aab1ad671f59ba157ae0640cd4b1302ca5/index.js#L92-L95

Copy link
Contributor Author

@danez danez Dec 3, 2022

Choose a reason for hiding this comment

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

Interesting, I wasn't aware of that and I just changed it because some eslint rule recommended it.

I changed it to the global version

@danez danez added the automerge Add to Kodiak auto merge queue label Dec 6, 2022
@kodiakhq kodiakhq bot merged commit 57ee933 into main Dec 6, 2022
@kodiakhq kodiakhq bot deleted the fix-body branch December 6, 2022 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Add to Kodiak auto merge queue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Netlify Functions not receiving a body in local dev but works once deployed to Netlify
3 participants