curveball / core

The Curveball framework is a TypeScript framework for node.js with support for modern HTTP features.
https://curveballjs.org/
MIT License
526 stars 7 forks source link

Streams support #165

Closed imdmitrykravchenko closed 3 years ago

imdmitrykravchenko commented 3 years ago

Hello! Thanks for framework, it works just great.

I'm wondering if it has any approach for working with streams?

I'd like to render React on the server side using renderToNodeStream method, but cannot find such methods like pipe in Response class.

Thanks in advance!

evert commented 3 years ago

I've held off on this until somebody asked for it.

I have an idea on how to handle this elegantly, avoiding some of the pitfalls koa has. The basic idea is when setting the body, you can instead provide a callback.

Example:

ctx.response.body = (stream: Writable) => {
  ReactDOMServer.renderToNodeStream(element).pipe(stream);
}

Will this work for you?

P.S.: I'm very interested in having first-class React support via a middleware. Ideally I just want you to be able to call:

ctx.response.body = <div>hello world</div>;

This is something I'll tackle soon, but if it sounds interesting to contribute to a curveball/react package, let me know!

evert commented 3 years ago

I tried to implement a react middleware. It works! And didn't need these changes.

With this middleware, you can simply do:

ctx.response.body = <html><div>hi</div</html>;

and it will just work.