Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Unexpected data on Writable Stream #1576

Closed
EFXPROMedia opened this issue Feb 12, 2019 · 5 comments
Closed

Error: Unexpected data on Writable Stream #1576

EFXPROMedia opened this issue Feb 12, 2019 · 5 comments
Labels

Comments

@EFXPROMedia
Copy link

EFXPROMedia commented Feb 12, 2019

I keep getting the error Errors:

Unexpected data on Writable Stream

and

**(sharp:37544): GLib-CRITICAL : 16:05:21.651: g_hash_table_lookup: assertion 'hash_table != NULL' failed

My code is:

if (req.query.format != null) {
	const imagePath = "./" + req._parsedOriginalUrl.pathname;
	const read = fs.createReadStream(path.resolve(__dirname, imagePath)).setEncoding('base64');
	const transform = Sharp(path.resolve(__dirname, imagePath)).resize(200, 200, {
		fit: "cover"
	}).jpeg({
		quality: 20
	}).toBuffer((err, data, info) => {
		console.log("FILE INFO", info)
	});
	const write = fs.createWriteStream("./"+ req.params.imageName);
	read.pipe(transform).pipe(write);
}

Is there anything wrong with my code or is this is an error with the package.

@lovell
Copy link
Owner

lovell commented Feb 13, 2019

Hello, you're providing a filesystem path to the sharp constructor as the input and then also attempting to pipe data into it; choose one.

(The g_hash_table_lookup warning is because the Node process exited without waiting for in-flight async tasks to complete.)

@EFXPROMedia
Copy link
Author

I am not sure if I understood, the documentation explains to get a readable stream of the file then use sharp with the readable stream as input then pipe it to the response! but it doesnt matter what I do I get an error. Should I be using Sync methods for this operation?

I am basically trying to read an image from the GET response and transform.resize() > pipe it to the response using sharp!

@lovell
Copy link
Owner

lovell commented Feb 13, 2019

A sharp instance can be treated as a Writable stream (for input), a Readable stream (for output), a Transform stream (for both input and output) or not a stream at all if you provide filesystem paths or a Buffer.

Here's you example re-worked to use filesystem paths:

	const imagePath = "./" + req._parsedOriginalUrl.pathname;
	const transform = Sharp(path.resolve(__dirname, imagePath)).resize(200, 200, {
		fit: "cover"
	}).jpeg({
		quality: 20
	}).toFile("./"+ req.params.imageName, (err, info) => {
		console.log("FILE INFO", info)
	});

...and here's your example re-worked to use a Transform stream:

	const imagePath = "./" + req._parsedOriginalUrl.pathname;
	const read = fs.createReadStream(path.resolve(__dirname, imagePath));
	const transform = Sharp().resize(200, 200, {
		fit: "cover"
	}).jpeg({
		quality: 20
	});
	const write = fs.createWriteStream("./"+ req.params.imageName);
	read.pipe(transform).pipe(write);

@EFXPROMedia
Copy link
Author

EFXPROMedia commented Feb 13, 2019

This helped me to figure out thanks... in my case I rendered the image on the response so I piped it to the response instead of the write and it works!

read.pipe(transform).pipe(res);

@lovell
Copy link
Owner

lovell commented Feb 15, 2019

I've just spotted that this was a cross-post of a question originally asked at https://stackoverflow.com/questions/54635393/render-opmised-images-with-node-js-and-express-with-sharp-or-canvas

Please (1) mention existing StackOverflow questions when opening new issues on GitHub and (2) now is a good time for you to update your original StackOverflow question to include the answer to help others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants