2016-08-13 42 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 
    } 
}