transform._transform(chunk, encoding, callback)


  • chunk <Buffer> | <string> | <any> 要转换的 Buffer,从 string 转换为 stream.write()。 如果流的 decodeStrings 选项是 false 或者流在对象模式下运行,则块将不会被转换,而是传给 stream.write() 的任何内容。
  • encoding <string> 如果块是字符串,则这是编码类型。 如果块是缓冲区,则这是特殊值 'buffer'。 在这种情况下忽略它。
  • callback <Function> 在处理提供的 chunk 后调用的回调函数(可选地带有错误参数和数据)。

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

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

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

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

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

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

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

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

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

  • 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> 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> A callback function (optionally with an error argument and data) to be called after the supplied chunk has been processed.

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.

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.

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.

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. 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);
};

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() is never called in parallel; streams implement a queue mechanism, and to receive the next chunk, callback must be called, either synchronously or asynchronously.