- Notifications
You must be signed in to change notification settings - Fork611
can we not read body after using await?#1178
-
I tried doing some checks based on headers, which needed a DB call with await. After that I was not able to read body, and it got stuck. Is that expected behaviour? |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 1 comment 5 replies
-
The issue you're describing with uWebSockets.js, based on the discussion context from the GitHub issue#1178, stems from the way uWebSockets.js handles HTTP request bodies in combination with asynchronous operations like Problem ExplanationIn uWebSockets.js, the HTTP request body is streamed and must be read synchronously within the request handler. When you use Expected BehaviorYes, this is expected behavior in uWebSockets.js. The library’s documentation (and community discussions) indicates that the request body should be read immediately in the handler, typically using the Would Callbacks Work Instead?Using callbacks instead of For example, a correct approach might look like this: app.post('/endpoint',(res,req)=>{letbody='';// Read the body synchronously using onDatares.onData((chunk,isLast)=>{body+=chunk.toString();if(isLast){// Now perform async operations, e.g., DB callsomeDbCallWithCallback((dbResult)=>{// Process headers or DB resultres.write(Buffer.from('Response'));});}});}); Here, the body is fully read before any asynchronous operations are performed, avoiding the issue you encountered. Why Async/Await Causes ProblemsWhen you use Solutions
Additional Notes
RecommendationTo confirm the exact behavior or potential workarounds, you could:
If you need me to search for updates on the GitHub issue or provide a more tailored code example, let me know! |
BetaWas this translation helpful?Give feedback.
All reactions
🚀 1👀 1
-
The only reason I was trying to use Thanks a lot for the detailed explanation@uNetworkingAB. I now understand why it behaves like this. |
BetaWas this translation helpful?Give feedback.
All reactions
-
If you absolutely want to reject body data until auth:ed, you can pause the socket. But this still requires handling spurious data so your code still will look the same. I have forgotten how to pause but you can find it if you ask AI or deepwiki or something like that |
BetaWas this translation helpful?Give feedback.
All reactions
-
Ah, yes you call res.pause() and res.resume() when you have authenticated |
BetaWas this translation helpful?Give feedback.
All reactions
-
Thanks for the info, I will try using this. |
BetaWas this translation helpful?Give feedback.
All reactions
-
@uNetworkingAB Is there a limit on the size of the buffer that will be used during a read pause, is this limit at the OS level? |
BetaWas this translation helpful?Give feedback.