transform._transform(chunk, encoding, callback)


  • chunk <Buffer> | <string> | <any> 要转换的数据块。 chunk 总会是一个 buffer,除非 decodeStrings 选项设为 false 或者流处在对象模式。
  • encoding <string> 如果 chunk 是字符串,则 encoding 是字符串的字符编码。 如果 chunk 是 buffer,则 encoding 的值为 'buffer'。
  • callback <Function>chunk 处理完成时的回调函数。

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

所有转换流的实现都必须提供 _transform() 方法来接收输入并生产输出。 transform._transform() 的实现会处理写入的字节,进行一些计算操作,然后使用 readable.push() 输出到可读流。

transform.push() 可能会被调用零次或多次用来从每次输入的数据块产生输出,调用的次数取决需要多少数据来产生输出的结果。

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

当前数据被完全消费之后,必须调用 callback 函数。 当处理输入的过程中发生出错时,callback 的第一个参数传入 Error 对象,否则传入 null。 如果 callback 传入了第二个参数,则它会被转发到 readable.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 chunk to be transformed. Will always be a buffer unless the decodeStrings option was set to false or the stream is operating in object mode.
  • 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 this 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 readable.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 readable.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.