Node.js流'end'事件未触发

时间:2016-03-04 20:49:30

标签: javascript node.js stream

以下数据流不会触发'end'事件。触发'data'事件,我可以看到每个数据行都记录到控制台。

var AWS = require('aws-sdk');
var ogr2ogr = require('ogr2ogr');
var JSONStream = require('JSONStream');

var S3 = new AWS.S3();
var source = S3.getObject({bucket: ..., key: ...}).createReadStream();

var stream = ogr2ogr(source).format("GeoJSON").stream()
  .pipe(JSONStream.parse('features.*'));

stream.on('data', function(data){
  console.log(data);                // Correctly outputs 70 rows of data.
})

stream.on('end', function(){
     console.log('end');            // This code is never executed.
})    

stream.on('error', function(err){
     console.log(err);              // No errors...
})

如果我创建了一个写入,那么这个过程是有效的 - >在ogr2ogr转换后读取流。

1 个答案:

答案 0 :(得分:1)

查看文档:{​​{3}}

Note that the 'end' event will not fire unless the data is completely consumed. This can be done by switching into flowing mode, or by calling stream.read() repeatedly until you get to the end