node.js 如何实现可写流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21491567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:47:27  来源:igfitidea点击:

How to implement a writable stream

node.jsnode.js-stream

提问by MonkeyBonkey

I want to pipe data from an amazon kinesis stream to a an s3 log or a bunyan log.

我想将数据从 amazon kinesis 流传输到 s3 日志或 bunyan 日志。

The sample works with a file write stream or stdout. How would I implmeny my own writable stream?

该示例适用于文件写入流或标准输出。我将如何实现我自己的可写流?

//this works
var file = fs.createWriteStream('my.log')
kinesisSource.pipe(file)

this doesn't work saying it has no method 'on'

这行不通,说它没有方法“打开”

var stream = {}; //process.stdout works however
stream.writable = true;
stream.write =function(data){
    console.log(data);
};
kinesisSource.pipe(stream);

what methods do I have to implement for my own custom writable stream, the docs seem to indicate I need to implement 'write' and not 'on'

我必须为自己的自定义可写流实现哪些方法,文档似乎表明我需要实现“写入”而不是“开启”

回答by Paul Mougel

To create your own writable stream, you have three possibilities.

要创建自己的可写流,您有三种可能性。

Create your own class

创建自己的班级

For this you'll need 1) to extend the Writable class 2) to call the Writable constructor in your own constructor 3) define a _write()method in the prototype of your stream object.

为此,您需要 1) 扩展 Writable 类 2) 在您自己的构造函数中调用 Writable 构造函数 3)_write()在流对象的原型中定义一个方法。

Here's an example :

这是一个例子:

var stream = require('stream');
var util = require('util');

function EchoStream () { // step 2
  stream.Writable.call(this);
};
util.inherits(EchoStream, stream.Writable); // step 1
EchoStream.prototype._write = function (chunk, encoding, done) { // step 3
  console.log(chunk.toString());
  done();
}

var myStream = new EchoStream(); // instanciate your brand new stream
process.stdin.pipe(myStream);

Extend an empty Writable object

扩展一个空的 Writable 对象

Instead of defining a new object type, you can instanciate an empty Writableobject and implement the _write()method:

您可以实例化一个空Writable对象并实现该_write()方法,而不是定义新的对象类型:

var stream = require('stream');
var echoStream = new stream.Writable();
echoStream._write = function (chunk, encoding, done) {
  console.log(chunk.toString());
  done();
};

process.stdin.pipe(echoStream);

Use the Simplified Constructor API

使用简化的构造函数 API

If you're using io.js, you can use the simplified constructor API:

如果您使用的是 io.js,则可以使用简化的构造函数 API

var writable = new stream.Writable({
  write: function(chunk, encoding, next) {
    console.log(chunk.toString());
    next();
  }
});

Use an ES6 class in Node 4+

在 Node 4+ 中使用 ES6 类

class EchoStream extends stream.Writable {
  _write(chunk, enc, next) {
    console.log(chunk.toString());
    next();
  }
}

回答by TonyAdo

Actually to create a writeable stream is quite simple. Here's is the example:

实际上,创建可写流非常简单。这是示例:

var fs = require('fs');
var Stream = require('stream');

var ws = new Stream;
ws.writable = true;
ws.bytes = 0;

ws.write = function(buf) {
   ws.bytes += buf.length;
}

ws.end = function(buf) {
   if(arguments.length) ws.write(buf);
   ws.writable = false;

   console.log('bytes length: ' + ws.bytes);
}

fs.createReadStream('file path').pipe(ws);

Also if you want to create your own class, @Paul give a good answer.

另外,如果您想创建自己的课程,@Paul 会给出一个很好的答案。