isbinaryfile
Advanced tools
Comparing version 0.1.3 to 0.1.4
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 @@ } |
{ | ||
"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`. |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
79617
11
89
57
0
1
2
1