如何在HummusJS中将流从请求修改为新的响应流 [英] How to modify a stream from request to a new response stream in HummusJS
问题描述
我需要使用HummusJS将png注入到PDF中.
I need to inject a png into a PDF using HummusJS.
在我的API的生产版本中,我将收到一个包含base64的发布请求,该请求将转换为二进制阅读器流. (在下面的示例中,我正在使用从本地文件读取的测试.)
In the production version of my API, I'll be receiving a post request containing a base64 that I'll convert to binary reader stream. (In the example below, I'm using a test read from a local file.)
这是我的测试输入流(在创建"对象工厂中定义):
Here's my test input stream (which is defined in a "create" object factory):
hummusReadStreamObject (filePath) {
let fileData = fs.readFileSync(filePath).toString('hex');
let result = []
for (var i = 0; i < fileData.length; i+=2) {
result.push('0x'+fileData[i]+''+fileData[i+1])
}
return new hummus.PDFRStreamForBuffer(result)
},
我生成一个PNG(我已经确认,PNG已写入且有效),然后在流的最终"事件期间执行修改
I generate a PNG (I have confirmed, the PNG is written and valid) and then perform modification during the stream's "finally" event
png.on('finish', function () {
create.modifiedPDFResponseStream({
pngFileName,
readStream: create.hummusReadStreamObject('/foo/file/path'),
res
})
})
modifiedPDFResponseStream
现在可以拉入png并应该将其附加到文件中:
the modifiedPDFResponseStream
now pulls in the png and is supposed to append it to the file:
modifiedPDFResponseStream ({ pngFileName, readStream, res }) {
const writeStream = new hummus.PDFStreamForResponse(res)
const pdfWriter = hummus.createWriterToModify(
readStream,
writeStream,
{ log: `path/to/error.txt` })
debugger
const pageModifier = new hummus.PDFPageModifier(pdfWriter,0);
if (pngFileName) {
const ctx = pageModifier.startContext().getContext()
ctx.drawImage(2, 2, `./path/to/${pngFileName}`)
pageModifier.endContext().writePage()
pdfWriter.end()
}
}
我认为我已经接近解决方案,错误日志不会报告任何问题,但是在通过Chrome进行调试时,我收到以下异常:
I sense that I'm close to a solution, the error logs do not report any problems, but i receive the following exception when debugging via Chrome:
events.js:183 Uncaught Error: write after end
at write_ (_http_outgoing.js:622:15)
at ServerResponse.write (_http_outgoing.js:617:10)
at PDFStreamForResponse.write
问题与在.png的.on('finish', ...)
事件期间填充流有关吗?如果是这样,我是否可以采用一种同步方法来缓解此问题?
is the issue something to do with the fact that the stream is being populated during the .png's .on('finish', ...)
event? If so, is there a synchronous approach I could take that would mitigate this problem?
推荐答案
受@JAM在"等待所有流完成-流式传输文件目录"
我解决了这个问题,方法是让.pngWriteStream
方法返回一个promise,并让writestream在完成时解析:
I solved this issue by having the .pngWriteStream
method return a promise and having the writestream resolve on finish:
return new Promise((resolve, reject) => {
try {
writeStream.on('finish', resolve)
} catch (e) {
$.logger.error(e.message)
reject()
}
因此代替:
const png = create.pngWriteStream({ ... })
png.on('finish', function () {
create.modifiedPDFResponseStream({
pngFileName,
readStream: create.hummusReadStreamObject('/foo/file/path'),
res
})
})
我在等待诺言的解决
const png = await create.pngWriteStream({
fileName: pngFileName,
value: '123456789' })
并且img文件可用于hummus.js写入pdf!
and the img file is available for hummus.js to write to the pdf!
create.modifiedPDFResponseStream({
pngFileName,
readStream: create.hummusReadStreamObject('/foo/file/path'),
writeStream: new hummus.PDFStreamForResponse(res)
})
这篇关于如何在HummusJS中将流从请求修改为新的响应流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!