transform._transform(chunk, encoding, callback)
chunk<Buffer> | <string> | <any> 要转换的Buffer,由传递给stream.write()的string转换而来。如果流的decodeStrings选项为false或流在对象模式下运行,则该 chunk 不会被转换,并且将保持为传递给stream.write()的内容。encoding<string> 如果块是一个字符串,那么这是编码类型。如果块是一个缓冲区,那么这是特殊值'buffer'。在这种情况下忽略它。callback<Function> 一个回调函数(可选带有错误参数和数据),在提供的chunk处理完成后被调用。
此函数绝对不能被应用代码直接调用。它应由子类实现,并且仅由内部的 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() 从不并行调用;流实现了一个队列机制,要接收下一个数据块,必须调用 callback,可以是同步调用也可以是异步调用。