Skip to content

Conversation

tangye1234
Copy link
Contributor

Using a ReadableStream instead of buffer.concat provides an improvement in several ways:

  1. According to web standards, the request.body should either be null or an instanceof ReadableStream, not Buffer (Uint8Array).
  2. In a Hono node server, it's important not to consume the incoming message body until it's actually used.
  3. Using a ReadableStream is especially beneficial when clients post large bodies, as it allows the Hono app to be built quickly by the node server.

@tangye1234
Copy link
Contributor Author

tangye1234 commented Mar 7, 2023

init['body'] = incoming as any

Using this alternative approach might suffice as well. I discovered that @remix-run/web-fetch's Request object can convert a Node.js stream into a ReadableStream through the Stream.on('data', ...) method, which is intended to cater to older versions of Node.js.

However, in terms of the underlying mechanics, it would be more advantageous to use a ReadableStream, given that our Node server is an adapter based on Node 16. Employing async iterators appears acceptable and comes across as more lucid to developers.

@yusukebe
Copy link
Member

yusukebe commented Mar 8, 2023

Hi @tangye1234 !

Great work! I also think it's better to use ReadableStream. Let's go with it!

@yusukebe yusukebe merged commit 31336ad into honojs:main Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants