Search code examples
node.jsbinaryascii

Check if a file is binary or ASCII with Node.js?


I'm wondering what would be the best way to check if a file is binary or ASCII with Node.js?

There appears to be two ways not specific to node.js:

  1. Checking the MIME type: How to Check if File is ASCII or Binary in PHP - however this has it's problems, as for instance pre-precessors often don't have a recognised mime type and revert to application/octet-stream when checking them using mime

  2. Via checking the byte size using a stream buffer with How to identify the file content as ASCII or binary - which seems quite intensive, and does yet provide a node.js example.

So is there another way already? Perhaps a secret node.js call or module that I don't know about? Or if I have to do this myself, what way would be suggested?

Thanks


Solution

  • Thanks to the comments on this question by David Schwartz, I created istextorbinary to solve this problem.