dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda πŸŽ‰
Other
1.73k stars 167 forks source link

gRPC connect-es support #278

Open rogersgt opened 1 year ago

rogersgt commented 1 year ago

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch serverless-http@3.2.0 for the project I'm working on.

Issue

I'm using serverless-http with express, and @bufbuild/connect-express to support a gRPC-compatible HTTP api on AWS Lambda and API Gateway. It may not be a super common use case, but we liked the idea of a lightweight backend; and also enjoy the type benefits of leveraging gRPC protocol syntax.

We noticed that copying the same code that runs just fine on a traditional express server with this gRPC adapter library was executing code successfully; however the response was never sent. The write method buf connect uses should actually return a boolean indicating whether the function should wait for a drain event or not, but the custom write method serverless-http defines doesn't return anything; but also doesn't provide a drain event, and so this leads to the hang.

Solution

Simply having the assignSocket method in the response.js file return a boolean value solved this issue for us. If this interferes with any other use case behavior, any assistance in resolving this issue in the preferred way would be very helpful. Thank you in advance!

diff --git a/node_modules/serverless-http/lib/response.js b/node_modules/serverless-http/lib/response.js
index da745c4..f3bf74c 100644
--- a/node_modules/serverless-http/lib/response.js
+++ b/node_modules/serverless-http/lib/response.js
@@ -124,6 +124,7 @@ module.exports = class ServerlessResponse extends http.ServerResponse {
         if (typeof cb === 'function') {
           cb();
         }
+        return true;
       },
     });

This issue body was partially generated by patch-package.