Socket
Socket
Sign inDemoInstall

isbinaryfile

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

isbinaryfile - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

tests/fixtures/01_grep

37

index.js

@@ -6,2 +6,3 @@ var fs = require('fs');

if (size === undefined) {
var file = bytes;
bytes = fs.readFileSync(file);

@@ -14,3 +15,16 @@ size = fs.statSync(file).size;

for (var i = 0; i < total_bytes; i++) {
if (size == 0)
return false;
if (size >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
/* UTF-8 BOM. This isn't binary. */
return 0;
}
for (var i = 0; i < total_bytes; i++) {
// Read at least 32 bytes before making a decision
if (i > 32 && (suspicious_bytes * 100) / total_bytes > 10) {
return true;
}
if (bytes[i] == '0') {

@@ -21,12 +35,21 @@ // NULL char. It's binary

else if ((bytes[i] < 7 || bytes[i] > 14) && (bytes[i] < 32 || bytes[i] > 127)) {
// UTF-8 detection
if (bytes[i] > 191 && bytes[i] < 224 && i + 1 < total_bytes) {
i++;
if (bytes[i] < 192) {
continue;
}
}
else if (bytes[i] > 223 && bytes[i] < 239 && i + 2 < total_bytes) {
i++;
if (bytes[i] < 192 && bytes[i + 1] < 192) {
i++;
continue;
}
}
suspicious_bytes++;
// Read at least 32 bytes before making a decision
if (i > 32 && (suspicious_bytes * 100) / total_bytes > 20) {
return true;
}
}
}
if ((suspicious_bytes * 100) / total_bytes > 20) {
if ((suspicious_bytes * 100) / total_bytes > 10) {
return true;

@@ -33,0 +56,0 @@ }

5

package.json
{
"name": "isbinaryfile",
"version" : "0.1.3",
"version" : "0.1.4",
"description": "Detects if a file is binary in Node.js. Similar to Perl's -B.",

@@ -16,3 +16,4 @@ "main" : "./lib/panino.js",

"url": "https://github.com/gjtorikian/isBinaryFile"
}
},
"devDependencies" : { "mocha": "" }
}

@@ -10,3 +10,3 @@ isBinaryFile

It's also pretty much taken from [ag](https://github.com/ggreer/the_silver_searcher).
All the logic is also pretty much ported from [ag](https://github.com/ggreer/the_silver_searcher).

@@ -37,3 +37,3 @@ Note: please make sure the file exists before calling this function.

However, if you've already `stat()`-ed a file, you can pass in both the file's raw data and the stat's `size` info to save time:
However, if you've already read and `stat()`-ed a file (for some other reason), you can pass in both the file's raw data and the stat's `size` info to save time:

@@ -49,2 +49,12 @@ ```javascript

});
```
```
## Testing
Install mocha on your machine:
```
npm install mocha -g
```
Then go into the _tests_ directory, and type `mocha test.js`.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc