new stream.Transform([options])
options<Object> 同时传递给Writable和Readable构造函数。还具有以下字段:transform<Function> 方法在stream._transform()方法中的实现。flush<Function> 方法的stream._flush()实现。
const { Transform } = require('node:stream');
class MyTransform extends Transform {
constructor(options) {
super(options);
// ...
}
} 或者,当使用 ES6 之前的样式构造函数时:
【Or, when using pre-ES6 style constructors:】
const { Transform } = require('node:stream');
const util = require('node:util');
function MyTransform(options) {
if (!(this instanceof MyTransform))
return new MyTransform(options);
Transform.call(this, options);
}
util.inherits(MyTransform, Transform); 或者,使用简化的构造函数方法:
【Or, using the simplified constructor approach:】
const { Transform } = require('node:stream');
const myTransform = new Transform({
transform(chunk, encoding, callback) {
// ...
},
});