将可写入的stream扩展成变换stream?

我想围绕你怎么做,因为你不能只从inheritanceinheritance, 似乎并不像你可以从这个特定的writeable streaminheritance。

理想情况下,它会是这样的:

 const Writeable = require('Writeable'); class Transform extends Writeable { constructor() { super(); } _transform(chunk, encoding) { } } 

但我可以说,这是行不通的。 我试图inheritance的特定可写stream将是tapjs / tap-parser 。 我理想的情况是能够利用它内部的事件监听器来parsingTAP输出。

我不确定es6语法,但是这里是一个传统的Transformstream,它接收TAP输出并输出描述各种parsing部分的对象:

 const Transform = require('stream').Transform; const inherits = require('util').inherits; const Parser = require('tap-parser'); function MyTransform() { const self = this; this._parser = new Parser(); Transform.call(this, { readableObjectMode: true }); this._parser.on('complete', function(results) { self.push({ type: 'complete', results }); }).on('assert', function(assert) { self.push({ type: 'assert', assert }); }).on('comment', function(comment) { self.push({ type: 'comment', comment }); }).on('plan', function(plan) { self.push({ type: 'plan', plan }); }).on('version', function(version) { self.push({ type: 'version', version }); }).on('bailout', function(reason) { self.push({ type: 'bailout', reason }); }).on('extra', function(extra) { self.push({ type: 'extra', extra }); }); } inherits(MyTransform, Transform); MyTransform.prototype._write = function(chunk, encoding, cb) { this._parser.write(chunk, cb); }; MyTransform.prototype._flush = function(cb) { const self = this; this._parser.end(function() { self.push(null); cb(); }); }; 

嗯,你写了关于Writable的,但是粘贴了Transform的代码,不知道你是否犯了一个错误,但是如果你这样做是有目的的,这里就是隔夜变换的一个例子。

数据line在哪里,可以在传递数据之前进行处理。

 let Transform = require("stream").Transform; class awesome_class_name extends Transform { constructor() { super() } _transform (line, encoding, processed) { // // Add the data that came in, to the output stream // this.push(line); // // We let system know that we finished processing the data. // processed(); } }