2016-08-13 82 views
0

我正在创建一个可读流(来自包含JSON文档的文件)的数组,并且试图将它们传递给另一个流。如何在读取回调处理程序中添加附加数据和从节点流读取的数据?

在文件中的数据通过...但我收到的管道输送到流中的每个对象来了,我想知道从哪个文件中这一数据来源于:

var fs = require('fs'); 
var path = require('path'); 
var JSONStream = require('JSONStream'); 

var tmp1 = path.join(__dirname, 'data', 'tmp1.json'); 
var tmp2 = path.join(__dirname, 'data', 'tmp2.json'); 

var jsonStream = JSONStream.parse(); 
jsonStream.on('data', function (data) { 
    console.log('---\nFrom which file does this data come from?'); 
    console.log(data); 
}); 

[tmp1, tmp2].map(p => { 
    return fs.createReadStream(p); 
}).forEach(stream => stream.pipe(jsonStream)); 

输出:

--- 
From which file does this data come from? 
{ a: 1, b: 2 } 
--- 
From which file does this data come from? 
{ a: 3, b: 4 } 
--- 
From which file does this data come from? 
{ a: 5, b: 6 } 
--- 
From which file does this data come from? 
{ a: 100, b: 200 } 
--- 
From which file does this data come from? 
{ a: 300, b: 400 } 
--- 
From which file does this data come from? 
{ a: 500, b: 600 } 

文件路径需要进一步处理读取的对象(在jsonStream.on('data') callback),但我不知道如何去传递这些额外的数据。

回答

0

一个可能的解决方案如下(我这标志着作为答案,除非我得到一个更好的答案):

var fs = require('fs'); 
var path = require('path'); 
var JSONStream = require('JSONStream'); 
var through = require('through2'); 

var tmp1 = path.join(__dirname, 'data', 'tmp1.json'); 
var tmp2 = path.join(__dirname, 'data', 'tmp2.json'); 

[tmp1, tmp2] 
    .map(p => fs.createReadStream(p)) 
    .map(stream => [path.parse(stream.path), stream.pipe(JSONStream.parse())]) 
    .map(([parsedPath, jsonStream]) => { 
    return jsonStream.pipe(through.obj(function (obj, _, cb) { 
     this.push({ 
     fileName: parsedPath.name, 
     data: obj 
     }); 
     cb(); 
    })); 
    }) 
    .map(stream => { 
    stream.on('data', function (data) { 
     console.log(JSON.stringify(data, null, 2)); 
    }); 
    }) 
    ; 

输出:

{ 
    "fileName": "tmp1", 
    "data": { 
    "a": 1, 
    "b": 2 
    } 
} 
{ 
    "fileName": "tmp1", 
    "data": { 
    "a": 3, 
    "b": 4 
    } 
} 
{ 
    "fileName": "tmp1", 
    "data": { 
    "a": 5, 
    "b": 6 
    } 
} 
{ 
    "fileName": "tmp2", 
    "data": { 
    "a": 100, 
    "b": 200 
    } 
} 
{ 
    "fileName": "tmp2", 
    "data": { 
    "a": 300, 
    "b": 400 
    } 
} 
{ 
    "fileName": "tmp2", 
    "data": { 
    "a": 500, 
    "b": 600 
    } 
}