dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.72k stars 165 forks source link

Do not raise exception. Fixes #142 #184

Closed davidkarlsen closed 3 years ago

davidkarlsen commented 3 years ago

See https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html for documentation on the header. I just tried the patched library, and I get the response in return just fine.


This change is Reviewable

davidkarlsen commented 3 years ago

Hmm, I see offsets interleaved with the data:

48<--
Error 404: java.io.FileNotFoundException: SRVE0190E: File not found: /x

0<--
dougmoscrop commented 3 years ago

Try setting transfer-encoding: identity on the response headers before handing it off to the application framework

davidkarlsen commented 3 years ago

hmm, where do you mean to do this? the flow is:

api-gw -> lambda (which uses this lib via https://github.com/http-party/node-http-proxy) -> backend (which sets transfer-encoding chunked)

you mean I can set transfer-encoding: identity instead of chunked when the response passes through the lambda?

dougmoscrop commented 3 years ago

Just as an experiment, try here: https://github.com/dougmoscrop/serverless-http/blob/master/lib/framework/get-framework.js#L8

response.setHeader('...')

davidkarlsen commented 3 years ago

Didn't seem to have any impact.

dougmoscrop commented 3 years ago

Yeah; I imagine the right thing to do here, unfortunately, is to read+understand the chunked encoding and strip out the chunks before handing it back from Lambda to API Gateway. If I'm not mistaken, that's how the HTTP spec actually expects intermediary nodes to work although I haven't exhaustively looked in to this. I'm happy to try to help here, I just have, literally, a dozen competing priorities right now, so if you do have cycles please keep it up and I will merge anything that improves the situation, but I'm not convinced removing the error is going to make peoples lives easier (maybe a link to this PR/issue in the error message, would be helpful)

davidkarlsen commented 3 years ago

Yes. Agree - I was a bit quick to conclude it worked. A bit surprised AWS api gw is like it is.

dougmoscrop commented 3 years ago

So to be fair, the blame falls solely on this library. If you were just writing a "plain Lambda", you'd be returning:

{ "statusCode": 200, "headers": { }, "body": "hello" }

aws-serverless-express actually runs a node server inside your lambda, that it talks to on localhost, and so it deserializes/reserializes multiple times to send the request and get the response before handing it back which gives it a bit of an edge that way. this library is creating http.IncomingMessage/ServerResponse objects, and it's the frameworks themselves sending back chunked responses, that normally the http client would strip out.

We can solve this, I'm sure