Count read and written bytes in a Duplex stream in Node.JS

  Kiến thức lập trình

I have a Duplex stream object coming from a library that I’m piping into HTTP requests so the request and the response goes through it.


Node.js versions and provides multiple methods of consuming stream
data. In general, developers should choose one of the methods of
consuming data and should never use multiple methods to consume data
from a single stream. Specifically, using a combination of on(‘data’),
on(‘readable’), pipe(), or async iterators could lead to unintuitive

I would like to know the total amount of bytes read and written.

const req = http.request({
    createConnection: () => {
        const duplexStream = getDuplexStreamFromLibrary();
        // TODO: Count read and written bytes in `duplexStream`
        return duplexStream;

req.on("response", (res) => {

How can I do it?