43 likes
·
14.2K reads
12 comments
Thanks, This is really helpful
Thanks for the great guide, and I was confused with the Writeable demo: Why the "wait drain" was logged after only one HELLO is logged to stdout, I assume the buffer was empty after the first HELLO logged, the 2nd HELLO should also be logged before the first "wait drain", not sure where did i misunderstood, is this because the stdout.write is async, after calling the "cb", the "hello" in the buffer will be removed?
Good spot! The buffer is flushed on first write. Because the cb
will be called after an event loop turn (it's async!), we need to wait for drain
. To counteract this effect there are cork() and uncork(). They are used to send out the http headers for example.
Well written article. How did you design the diagram?
We have an amazing designer at platformatic :).
thank you mate informative
Thank you for a great streams overview!
How would you write out the correct version of HTTP stream example with stream/promises pipeline, pass res as last parameter? And for fastify, reply.send?
use @fastify/static
:). Don't stream files directly because there are infinite edge cases that will inevitably result in a security issues in your server.
Matteo Collina Thanks for the reply! Yes, understood for static files which can be directly served, but I was thinking more of the case when there is a pipeline - reading file then transforming and finally sending as a response...
Stasa Stanisic you can just call reply.send(stream)
or return stream
in your Fastify route. Using barebone HTTP, you should do pipeline(a, b, c, res, (err) => { ... })
.
Really great article. Would love for an in depth article on async generators. Saw a library recently using them for streams & it seemed very concise. Would be interested to leer more.