It's bit strange to me the way stream is behaving sometime. Simple task at work is to pull a image from S3 and transform it using sharp library; finally upload resized image back to S3. I can see resized image in S3 but code fails with error.
Here is a code snippet doing all the work -
// Read from S3
const readStream = s3Handler.readStream(params)
// resize Stream
let resizeStream = sharp()
resizeStream
.metadata()
.then((metadata) => {
// Resize the image to a width specified by the `percent` value and output as PNG
sharpOptions = { width: width*(metadata.percent), height: height*(metadata.percent), fit: 'contain' }
resizeStream
.resize(sharpOptions)
.toFormat('png')
.png({ quality: 100, compressionLevel: 5 })
.toBuffer()
//return streamResize.resize(sharpOptions).png().toBuffer()
})
// push to s3
const { writeStream, uploaded } = s3Handler.writeStream({
Bucket: bucket,
Key: key,
Ext: ext,
})
readStream.pipe(resizeStream).pipe(writeStream)
await uploaded
Here is the error I get from above code -
{
"errorType": "Error",
"errorMessage": "stream.push() after EOF",
"code": "ERR_STREAM_PUSH_AFTER_EOF",
"stack": [
"Error [ERR_STREAM_PUSH_AFTER_EOF]: stream.push() after EOF",
" at readableAddChunk (_stream_readable.js:260:30)",
" at Sharp.Readable.push (_stream_readable.js:213:10)",
" at Object.<anonymous> (/var/task/node_modules/sharp/lib/output.js:863:18)"
]
}
Any suggestions much appreciated.