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.1 to 0.1.2

34

index.js
var fs = require('fs');
module.exports = function(file, statSize) {
// Read the file with no encoding for raw buffer access.
var bytes = fs.readFileSync(file);
var size = statSize || fs.statSync(file).size;
var suspicious_bytes = 0;
var total_bytes = size > 1024 ? 1024 : size;
for (var i = 0; i < total_bytes; i++) {
if (bytes[i] == '\0') {
// NULL char. It's binary
return 1;
module.exports = function(bytes, statSize) {
var size = statSize || null;
// Read the file with no encoding for raw buffer access.
if (size === undefined) {
process.exit(1)
bytes = fs.readFileSync(file);
size = fs.statSync(file).size;
}
var suspicious_bytes = 0;
var total_bytes = size > 1024 ? 1024 : size;
for (var i = 0; i < total_bytes; i++) {
if (bytes[i] == '0') {
// NULL char. It's binary
return true;
}

@@ -21,7 +27,7 @@ else if ((bytes[i] < 7 || bytes[i] > 14) && (bytes[i] < 32 || bytes[i] > 127)) {

if ((suspicious_bytes * 100) / total_bytes > 10) {
return 1;
return true;
}
}
}
return 0;
}
return false;
}
{
"name": "isbinaryfile",
"version" : "0.1.1",
"version" : "0.1.2",
"description": "Detects if a file is binary in Node.js. Similar to Perl's -B.",

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

"maintainers": [{
"name": "Garen J. Torikisn Hidayat",
"name": "Garen J. Torikian",
"email": "gjtorikian@gmail.com"

@@ -18,2 +18,2 @@ }],

}
}
}

@@ -6,3 +6,3 @@ isBinaryFile

* it reads the first few bytes of a file
* it reads the first few thousand bytes of a file
* checks for a `null` byte; if it's found, it's binary

@@ -21,2 +21,4 @@ * flags non-ASCII characters. After a certain number of "weird" characters, the file is flagged as binary

If you pass in one argument, this module assumes it's just the file path, and performs the appropriate file read and stat functionality internally:
```javascript

@@ -31,7 +33,16 @@ var isBinaryFile = require("isbinaryfile");

Ta da. If you've already `stat()`-ed a file, you can pass the stat's `size` info in to save time:
Ta da.
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:
```javascript
var stat = lstatSync(process.argv[2])
var isBF = isBinaryFile(process.argv[2], stat.size());
fs.readFile(process.argv[2], function(err, data) {
fs.lstat(process.argv[2], function(err, stat) {
if (isBinaryFile(data, stat))
console.log("It is!")
else
console.log("No.")
});
});
```
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