let decipher = crypto.createDecipheriv(type, file.metadata?.key, file.metadata?.iv)
let decompress = new fflate.Decompress()
let decipherStream = fs.createReadStream(location).pipe(decipher)
decipherStream.on('data', (data) => decompress.push(data))
decipherStream.on('finish', () => decompress.push(new Uint8Array(), true))
decompress.ondata = (data: any, final: any) => {
if (!final) res.write(data)
if (final) res.send()
}
res.attachment(file.originalName)
字符串
我有这段工作代码来获取和解压缩一个文件,并将其作为API响应发送,我需要使它与不同的进程一起工作,而不是作为API响应。
这是我的尝试:
let decipher = crypto.createDecipheriv(type, file.metadata?.key, file.metadata?.iv)
let decompress = new fflate.Decompress()
let decipherStream = fs.createReadStream(location).pipe(decipher)
decipherStream.on('data', (data) => decompress.push(data))
decipherStream.on('finish', () => decompress.push(new Uint8Array(), true))
let stream = new Readable()
stream._read = function (){}
decompress.ondata = (data: any, final: any) => {
console.log(data)
if (!final) stream.push(data)
if (final) stream.push(null)
}
let headernames:any = []
let jsonobjs:any = []
let jsonout = ''
await csv().fromStream(stream)
.on('headers', (headers: any) => {
headernames = headers;
console.log("Headers are", headernames);
})
.on('data', (data: any) => {
jsonout = data.toString('utf8');
jsonobjs.push(jsonout);
})
.on('done', () => {
console.log("jsonout:",jsonout);
return jsonobjs;
})
型
这只发送csv中的第一行数据,然后它挂起,任何帮助都将不胜感激。
1条答案
按热度按时间hiz5n14c1#
对于其他有关于fflate库和csvtoJson库的具体问题的人,我找到了解决方案,而不是试图将流放入CSV()。fromStream我使用CSV().fromString,并提供了对fflate的回调。解压缩函数:
字符串