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

后端 未结 4 1660
忘掉有多难
忘掉有多难 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: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.

提交回复
热议问题