transform._transform(chunk, encoding, callback)


  • chunk <Buffer> | <string> | <any> 要转换的 Buffer,从 string 转换为 stream.write()。如果流的 decodeStrings 选项是 false 或者流在对象模式下运行,则块将不会被转换并且将是传递给 stream.write() 的任何内容。

    ¥chunk <Buffer> | <string> | <any> The Buffer to be transformed, converted from the string passed to stream.write(). If the stream's decodeStrings option is false or the stream is operating in object mode, the chunk will not be converted & will be whatever was passed to stream.write().

  • encoding <string> 如果块是字符串,则这是编码类型。如果 chunk 是缓冲区,那么这是特殊值 'buffer'。在这种情况下忽略它。

    ¥encoding <string> If the chunk is a string, then this is the encoding type. If chunk is a buffer, then this is the special value 'buffer'. Ignore it in that case.

  • callback <Function> 在处理提供的 chunk 后调用的回调函数(可选地带有错误参数和数据)。

    ¥callback <Function> A callback function (optionally with an error argument and data) to be called after the supplied chunk has been processed.

此函数不得由应用代码直接调用。它应该由子类实现,并且只能由内部 Readable 类方法调用。

¥This function MUST NOT be called by application code directly. It should be implemented by child classes, and called by the internal Readable class methods only.

所有 Transform 流实现都必须提供 _transform() 方法来接受输入和产生输出。transform._transform() 实现处理正在写入的字节,计算输出,然后使用 transform.push() 方法将该输出传递给可读部分。

¥All Transform stream implementations must provide a _transform() method to accept input and produce output. The transform._transform() implementation handles the bytes being written, computes an output, then passes that output off to the readable portion using the transform.push() method.

transform.push() 方法可能被调用零次或多次以从单个输入块生成输出,具体取决于作为块的结果要输出多少。

¥The transform.push() method may be called zero or more times to generate output from a single input chunk, depending on how much is to be output as a result of the chunk.

任何给定的输入数据块都可能不会生成任何输出。

¥It is possible that no output is generated from any given chunk of input data.

callback 函数只有在当前块被完全消耗时才必须被调用。如果在处理输入时发生错误,则传递给 callback 的第一个参数必须是 Error 对象,否则为 null。如果第二个参数传递给 callback,它将被转发到 transform.push() 方法,但前提是第一个参数为假。换句话说,以下是等价的:

¥The callback function must be called only when the current chunk is completely consumed. The first argument passed to the callback must be an Error object if an error occurred while processing the input or null otherwise. If a second argument is passed to the callback, it will be forwarded on to the transform.push() method, but only if the first argument is falsy. In other words, the following are equivalent:

transform.prototype._transform = function(data, encoding, callback) {
  this.push(data);
  callback();
};

transform.prototype._transform = function(data, encoding, callback) {
  callback(null, data);
}; 

transform._transform() 方法带有下划线前缀,因为它是定义它的类的内部方法,不应由用户程序直接调用。

¥The transform._transform() method is prefixed with an underscore because it is internal to the class that defines it, and should never be called directly by user programs.

transform._transform() 永远不会被并行调用;streams 实现了队列机制,要接收下一个块,必须同步或异步调用 callback

¥transform._transform() is never called in parallel; streams implement a queue mechanism, and to receive the next chunk, callback must be called, either synchronously or asynchronously.