实现转换流


Transform 流是 Duplex 流,其中输出以某种方式从输入计算。 示例包括压缩、加密、或解密数据的压缩流或加密流。

不要求输出与输入大小相同、块数相同或同时到达。 例如,Hash 流只会有一个单一的输出块,它在输入结束时提供。 zlib 流将产生比其输入小得多或大得多的输出。

stream.Transform 类被扩展以实现 Transform 流。

stream.Transform 类原型上继承自 stream.Duplex 并实现其自己版本的 writable._write()readable._read() 方法。 自定义的 Transform 实现必须实现 transform._transform() 方法,也可以实现 transform._flush() 方法。

使用 Transform 流时必须小心,因为如果不消耗 Readable 端的输出,写入流的数据可能导致流的 Writable 端暂停。

A Transform stream is a Duplex stream where the output is computed in some way from the input. Examples include zlib streams or crypto streams that compress, encrypt, or decrypt data.

There is no requirement that the output be the same size as the input, the same number of chunks, or arrive at the same time. For example, a Hash stream will only ever have a single chunk of output which is provided when the input is ended. A zlib stream will produce output that is either much smaller or much larger than its input.

The stream.Transform class is extended to implement a Transform stream.

The stream.Transform class prototypically inherits from stream.Duplex and implements its own versions of the writable._write() and readable._read() methods. Custom Transform implementations must implement the transform._transform() method and may also implement the transform._flush() method.

Care must be taken when using Transform streams in that data written to the stream can cause the Writable side of the stream to become paused if the output on the Readable side is not consumed.