Skip to content

toNodeHandler seems to be stuck when running in Lambda #22

@paul-mais

Description

@paul-mais

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

toNodeHandler in the Lambda environment gets stuck on res.once("drain", next);. It works fine locally, so I'm not sure if this is a safe fix — but it works for me for now.

Here is the diff that solved my problem:

diff --git a/node_modules/better-call/dist/node.cjs b/node_modules/better-call/dist/node.cjs
index 803708e..d6be0a4 100644
--- a/node_modules/better-call/dist/node.cjs
+++ b/node_modules/better-call/dist/node.cjs
@@ -156,9 +156,19 @@ async function setResponse(res, response) {
       for (; ; ) {
         const { done, value } = await reader.read();
         if (done) break;
-        if (!res.write(value)) {
-          res.once("drain", next);
-          return;
+        const writeResult = res.write(value);
+        if (!writeResult) {
+          // In AWS Lambda/serverless environments, drain events may not work properly
+          // Check if we're in a Lambda-like environment and handle differently
+          if (process.env.AWS_LAMBDA_FUNCTION_NAME || process.env.LAMBDA_TASK_ROOT) {
+            console.log('setResponse AWS Lambda detected, continuing without drain');
+            // In Lambda, continue without waiting for drain
+            continue;
+          } else {
+            // Standard Node.js behavior
+            res.once("drain", next);
+            return;
+          }
         }
       }
       res.end();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions