我正在创建一个可读流数组(来自包含 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 }
需要文件路径来进一步处理读取的对象(in jsonStream.on('data') callback
),但我不知道如何传递这些附加数据。