[关闭]
@bornkiller 2014-12-26T07:30:38.000000Z 字数 2354 阅读 2206

详解Node Stream

nodejs


前言

之前基于流处理构建工具gulp介绍,现在对stream模块进行简要分析,以便更好的使用构建工具,以及进行插件开发。

Stream

Stream模块有四个类,Readable, Writable, Duplex, TransformTransform可以看做自成体系的子类。从使用角度来说,模块定义的类都为基类,是不具备直接使用条件的,需要程序实现相关接口方可使用。

Stream.Readable

此类需要实现_read接口,用通俗的话来讲,可读流相当于发货仓库,仓库中的货物储备交由_read处理,具体发货模块内部自行处理。可读流对象有flowing modenon-flowing mode两种模式,前者自动处理发货,后者需要手动控制发货。

  1. // inherit stream.Readable
  2. function Love() {
  3. stream.Readable.call(this);
  4. this._max = 5;
  5. this._index = 0;
  6. }
  7. util.inherits(Love, stream.Readable);
  8. Love.prototype._read = function() {
  9. var i = this._index++;
  10. if (i > this._max) {
  11. this.push('beautiful');
  12. this.push(null);
  13. }
  14. else {
  15. var str = '' + i;
  16. var buf = new Buffer(str, 'utf8');
  17. this.push(buf);
  18. }
  19. };

在初始化时,会自动调用_read方法,利用ctx.push方法写入内容到内部存储buffer(进货)。代码很简单,传输的内容为0-5,以及单词beautiful。现在仓库中已经有货物,然后处理发货流程。

flowing mode下,监听data事件即可,non-flowing mode下,使用readable.read方法获取内容,两种方式实际效果等同。此处readable事件触发比较不解,暂时无法深入。

  1. // flowing mode
  2. title.on('data', function(data) {
  3. writer.write(data);
  4. });
  5. // non-flowing mode
  6. title.on('readable', function() {
  7. var chunk;
  8. while (null !== (chunk = title.read())) {
  9. writer.write(chunk);
  10. }
  11. });

至此,可以简单理解可读流就是进货出货的方式,定义接口实现进货,数据读取实现出货。

stream.Writable

此类需要实现_write接口,用通俗的话来讲,可写流就是快递签收的过程。卖家不断发货,买家不断收货,签收的流程就是由_write接口定义。

  1. // inherit stream.Writable
  2. function Story() {
  3. stream.Writable.call(this);
  4. this._storage = new Buffer('');
  5. }
  6. util.inherits(Story, stream.Writable);
  7. Story.prototype._write = function(chunk, encoding, callback) {
  8. this._storage = Buffer.concat([this._storage, chunk]);
  9. callback();
  10. };

此处定义方式很简单,收到数据后,将数据保存在this._storage私有变量中,这样就定义好可写流。下面来看如何综合使用两个类。

  1. var reader = new Love();
  2. var writer = new Story();
  3. reader.on('readable', function() {
  4. var chunk;
  5. while (null !== (chunk = title.read())) {
  6. writer.write(chunk);
  7. }
  8. });
  9. reader.on('end', function() {
  10. writer.end();
  11. });
  12. writer.on('finish', function() {
  13. fs.writeFileSync('./output.txt', this._storage);
  14. });

此处使用,将可读流传下来的数据全部写入output.txt文件之中,非常简单的示例。

stream.Duplex

可读可写流,兼而有之两者特性,不清楚是否可以同时兼任两者,本人暂时未找到处理方案。

stream.Transform

能够同时兼任可读流与可写流,gulp插件总结来说,就是自定义的stream.Transform流。需要实现接口_transform_flush,两者都可以_read的特点,可以向后传递数据。

  1. function Knight() {
  2. stream.Transform.call(this);
  3. }
  4. util.inherits(Knight, stream.Transform);
  5. Knight.prototype._transform = function(chunk, encoding, callback) {
  6. this.push(chunk);
  7. callback();
  8. };
  9. Knight.prototype._flush = function(callback) {
  10. this.push('dark knight');
  11. callback();
  12. };

示例非常简单,在内容后面再加上dark knight字符串,不再赘述。

总结

对所有流来说,通常使用pipe方法更为简便直接,所以应避免使用其他方式。完整代码地址:http://snowykiss.qiniudn.com/stream.js

联系方式

QQ: 491229492
https://github.com/bornkiller

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注