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

Issue Detecting PNG #442

Closed
patrickheeney opened this issue Mar 11, 2021 · 5 comments
Closed

Issue Detecting PNG #442

patrickheeney opened this issue Mar 11, 2021 · 5 comments
Labels
cannot reproduce The issue described cannot be reproduced

Comments

@patrickheeney
Copy link

patrickheeney commented Mar 11, 2021

I have a third party vendor that is providing images that I am attempting to detect what format. The PNG opens fine on a mac, however I get an error when using the library:

EndOfStreamError: End-Of-Stream
          at BufferTokenizer.peekToken (/node_modules/strtok3/lib/BufferTokenizer.js:92:19)
          at BufferTokenizer.readToken (/node_modules/strtok3/lib/BufferTokenizer.js:81:29)
          at readChunkHeader (/file-type/core.js:964:27)
          at _fromTokenizer (/file-type/core.js:970:18)

When I open the PNG and re-save, it appears to work correctly. I am unaware of how the original images are being generated in this way but they appear to be valid even before re-saving.

wget -O image https://images2.imgbox.com/98/88/UbYCwMuO_o.png?download=true
const FileType = require('file-type');
const readChunk = require('read-chunk');

(async () => {
	const buffer = readChunk.sync('image', 0, 4100);
	console.log(await FileType.fromBuffer(buffer));
	//=> {ext: 'png', mime: 'image/png'}
})();

It seems that the tokenizer.position is 2-4 bytes from the length on the last iteration of while (tokenizer.position < tokenizer.fileInfo.size) which causes it to attempt to read past the length. I can't quite figure out where it is ending up 2-4 bytes short so the final iteration of the do while loop doesn't happen.

EDIT. Interestingly when I change the chunk length to const buffer = readChunk.sync('./image', 0, 4097); it works, anything above that it fails. I got that line from the README.

@Borewit Borewit added the cannot reproduce The issue described cannot be reproduced label May 4, 2021
@Borewit
Copy link
Collaborator

Borewit commented May 4, 2021

I am sorry, you need to describe a way to reproduce the issue. Please double check you are using streams correctly, you can only read from a stream once.

@Borewit Borewit closed this as completed May 4, 2021
@patrickheeney
Copy link
Author

@Borewit Did you see the steps I listed in the original issue for reproducing the issue? You run the wget command and the script I posted. Do you not see the error? What output do you get?

@patrickheeney
Copy link
Author

@Borewit Here is the full step by step reproduction:

mkdir ~/Dev/test
cd ~/Dev/test
npm install file-type@15.0.0 read-chunk@3.2.0
wget -O image https://images2.imgbox.com/98/88/UbYCwMuO_o.png?download=true
echo "const FileType = require('file-type');
const readChunk = require('read-chunk');

(async () => {
	const buffer = readChunk.sync('image', 0, 4100);
	console.log(await FileType.fromBuffer(buffer));
	//=> {ext: 'png', mime: 'image/png'}
})();" >> test.js
node test.js

EDIT. When I try the latest version it might be OK. Let me know if you see the problem on 15.0.0?

@patrickheeney
Copy link
Author

It was fixed in ec88386 . I had some cached decencies that were using version 15.0.0. Sorry about that and thanks for the fix last year.

@Borewit
Copy link
Collaborator

Borewit commented May 4, 2021

Glad that everything is working.

Duplicate of issue #391.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cannot reproduce The issue described cannot be reproduced
Projects
None yet
Development

No branches or pull requests

2 participants