NodeJS: What's the difference between a Duplex stream and a Transform stream?

后端 未结 4 1661
忘掉有多难
忘掉有多难 2020-12-23 11:53

The Stream docs state that Duplex Streams \"are streams that implement both the Readable and Writable interfaces\" and Transform Streams \"are Duplex streams where the outpu

相关标签:
4条回答
  • 2020-12-23 12:12

    A Duplex stream can be thought of a readable stream with a writable stream. Both are independent and each have separate internal buffer. The reads and writes events happen independently.

                                 Duplex Stream
                              ------------------|
                        Read  <-----               External Source
                You           ------------------|   
                        Write ----->               External Sink
                              ------------------|
                You don't get what you write. It is sent to another source.
    

    A Transform stream is a duplex where the read and write takes place in a causal way. The end points of the duplex stream are linked via some transform. Read requires write to have occurred.

                                     Transform Stream
                               --------------|--------------
                You     Write  ---->                   ---->  Read  You
                               --------------|--------------
                You write something, it is transformed, then you read something.
    
    0 讨论(0)
  • 2020-12-23 12:24

    If you read the API for stream implementors section of the docs, they state that some possible use cases for Duplex and Transform streams are "Reading and writing" and "Operate on written data, then read the result" respectively.

    Simply put the Transform stream lets you implement the _transform method that takes some input and returns the output after some operation has been done on the data, and can be used for things like compressing or hashing, whereas the Duplex stream can be used for things like a TCP socket connection where you just send and receive data.

    0 讨论(0)
  • 2020-12-23 12:34

    The difference is just syntactic sugar. Transform streams are full duplex streams but rather than implementing _write and _read methods, you are asked to implement just the _transform method. You can read more about streams on the excellent substack's streams guide or from Isaacs's readable-stream repo.

    0 讨论(0)
  • 2020-12-23 12:34

    According to the docs:

    Duplex - streams that are both Readable and Writable (for example, net.Socket).

    Transform - Duplex streams that can modify or transform the data as it is written and read (for example, zlib.createDeflate()).

    So to put it simply:

    • A duplex stream can be read from and written to, but there's not necessarily any connection between the input and output.
    • A transform stream can also be read from and written to, but the output will always be the result of a transformation on its input. Transform streams are Duplex streams where the output is in some way related to the input.
    0 讨论(0)
提交回复
热议问题