elad / node-imagemagick-native

ImageMagick's Magick++ bindings for NodeJS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

stream delay

lagden opened this issue · comments

Node v6.9.1
ImageMagick 6.9.6-0 Q16 x86_64 2016-10-07
imagemagick-native v1.9.3


Hi!
I am having problems with the stream...

When the pipe ends, I check the file size and it is 0

Take a look on the script below:

'use strict';

const fs = require('fs')
const im = require('imagemagick-native')

const inFile = fs.createReadStream('original.jpg')
const outFile = fs.createWriteStream('result.jpg')

const imageStream = im.streams.convert({
	width: 300,
	height: 300,
	resizeStyle: 'aspectfill',
	gravity: 'Center'
})

inFile
	.on('end', () => {
		const stat = fs.statSync('result.jpg')
		// expected the file size here
		console.log('on pipe end', stat.size) // on pipe end 0
		
		// Workaround
		setTimeout(() => {
			const stat = fs.statSync('result.jpg')
			// after 1 second is OK!!
			console.log('on pipe end + 1 sec', stat.size) // on pipe end + 1 sec 135905
		}, 1000)
	})
	.pipe(imageStream)
	.pipe(outFile)

Sorry!!! I did a mistake!! :finnadie:

outFile.on('close', () => {
	const stat = fs.statSync('result.jpg')
	console.log('on pipe end', stat.size)
})