我想通过在函数中解析一个warc文件来将JSON数据分配给一个变量。该变量在函数外部不可访问,并在控制台上返回一个空数组。
var metadataObj = {
metadata: []
};
fs
.createReadStream('mywarc-file.warc')
.pipe(new WARCStreamTransform())
.on('data', record => {
if (targetURL === record.warcHeader['WARC-Target-URI']){
if(record.warcHeader['WARC-Type'] === 'response'){
metadataObj.metadata.push({
Url: record.warcHeader['WARC-Target-URI'],
WarcID:record.warcHeader['WARC-Warcinfo-ID'],
Timestamp:record.warcHeader['WARC-Date'],
ContentType:record.warcHeader['Content-Type']
})
}else{
metadataObj.metadata.push({
Host: record.httpInfo.headers['Host'],
userAgent: record.httpInfo.headers['User-Agent']
})
}
}
})
console.log(metadataObj.metadata)