isbinaryfile
Advanced tools
Comparing version 1.0.0 to 1.0.1
63
index.js
@@ -1,47 +0,21 @@ | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var fs = require('fs'); | ||
var max_bytes = 512; | ||
module.exports = function(bytes, size) { | ||
var max_bytes = 512; | ||
function isBinaryFile(file, callback) { | ||
var exists = fs.exists || path.exists; | ||
exists(file, function (exists) { | ||
if (!exists) | ||
return callback(null, false); | ||
fs.open(file, 'r', function(err, descriptor){ | ||
if (err) | ||
return callback(err); | ||
var bytes = new Buffer(max_bytes); | ||
// Read the file with no encoding for raw buffer access. | ||
fs.read(descriptor, bytes, 0, bytes.length, 0, function(err, size, bytes){ | ||
fs.close(descriptor, function(err2){ | ||
if (err || err2) | ||
return callback(err || err2); | ||
return callback(null, isBinaryCheck(size, bytes)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
function isBinaryFileSync(file) { | ||
var existsSync = fs.existsSync || path.existsSync; | ||
var size = 0; | ||
var bytes; | ||
if (!existsSync(file)) | ||
return false; | ||
var descriptor = fs.openSync(file, 'r'); | ||
try { | ||
bytes = new Buffer(max_bytes); | ||
size = fs.readSync(descriptor, bytes, 0, bytes.length, 0); | ||
} finally { | ||
fs.closeSync(descriptor); | ||
// Read the file with no encoding for raw buffer access. | ||
if (size === undefined) { | ||
var file = bytes; | ||
if (!fs.existsSync(file)) | ||
return false; | ||
var descriptor = fs.openSync(file, 'r'); | ||
try { | ||
bytes = new Buffer(max_bytes); | ||
size = fs.readSync(descriptor, bytes, 0, bytes.length, 0); | ||
} finally { | ||
fs.closeSync(descriptor); | ||
} | ||
} | ||
return isBinaryCheck(size, bytes); | ||
} | ||
function isBinaryCheck (size, bytes) { | ||
if (size === 0) | ||
if (size == 0) | ||
return false; | ||
@@ -58,3 +32,3 @@ | ||
for (var i = 0; i < total_bytes; i++) { | ||
if (bytes[i] === 0) { // NULL byte--it's binary! | ||
if (bytes[i] == 0) { // NULL byte--it's binary! | ||
return true; | ||
@@ -91,4 +65,1 @@ } | ||
} | ||
exports.isBinaryFile = isBinaryFile; | ||
exports.isBinaryFileSync = isBinaryFileSync; |
{ | ||
"name": "isbinaryfile", | ||
"version" : "1.0.0", | ||
"version" : "1.0.1", | ||
"description": "Detects if a file is binary in Node.js. Similar to Perl's -B.", | ||
@@ -5,0 +5,0 @@ "main" : "./lib/panino.js", |
@@ -25,3 +25,3 @@ isBinaryFile | ||
```javascript | ||
var isBinaryFile = require("isbinaryfile"); | ||
var isBinaryFileSync = require("isbinaryfile").isBinaryFileSync; | ||
@@ -54,3 +54,3 @@ if (isBinaryFileSync(process.argv[2])) | ||
async option. The async option is a method called `isBinaryFile`. Its callback | ||
has two arguements: `err` and `isBinary`. | ||
has two arguments: `err` and `isBinary`. | ||
@@ -57,0 +57,0 @@ The sync option is called `isBinaryFileSync`. |
@@ -5,60 +5,52 @@ "mocha"; | ||
var fs = require("fs"); | ||
var isBinaryFile = require("../index").isBinaryFile; | ||
var isBinaryFileSync = require("../index").isBinaryFileSync; | ||
var isBinaryFile = require("../index"); | ||
describe('isBinaryFile', function() { | ||
it('should fail on a binary program', function() { | ||
assert(isBinaryFileSync("tests/fixtures/01_grep")); | ||
assert(isBinaryFile("tests/fixtures/01_grep")); | ||
isBinaryFile("tests/fixtures/01_grep", function (err, isBinary) { | ||
assert(!err); | ||
assert(isBinary); | ||
}); | ||
var bytes = fs.readFileSync("tests/fixtures/01_grep"); | ||
var stat = fs.lstatSync("tests/fixtures/01_grep"); | ||
assert(isBinaryFile(bytes, stat.size)); | ||
}); | ||
it('should not fail on an extensionless script', function() { | ||
assert(!isBinaryFileSync("tests/fixtures/02_perl_script")); | ||
assert(!isBinaryFile("tests/fixtures/02_perl_script")); | ||
isBinaryFile("tests/fixtures/02_perl_script", function (err, isBinary) { | ||
assert(!err); | ||
assert(!isBinary); | ||
}); | ||
var bytes = fs.readFileSync("tests/fixtures/02_perl_script"); | ||
var stat = fs.lstatSync("tests/fixtures/02_perl_script"); | ||
assert(!isBinaryFile(bytes, stat.size)); | ||
}); | ||
it('should not fail on a russian text', function() { | ||
assert(!isBinaryFileSync("tests/fixtures/03_Руководство_по_эксплуатации.rst")); | ||
assert(!isBinaryFile("tests/fixtures/03_Руководство_по_эксплуатации.rst")); | ||
isBinaryFile("tests/fixtures/03_Руководство_по_эксплуатации.rst", function (err, isBinary) { | ||
assert(!err); | ||
assert(!isBinary); | ||
}); | ||
var bytes = fs.readFileSync("tests/fixtures/03_Руководство_по_эксплуатации.rst"); | ||
var stat = fs.lstatSync("tests/fixtures/03_Руководство_по_эксплуатации.rst"); | ||
assert(!isBinaryFile(bytes, stat.size)); | ||
}); | ||
it('should not fail on a PDF', function() { | ||
assert(isBinaryFileSync("tests/fixtures/04_HelloWorld.pdf")); | ||
assert(isBinaryFile("tests/fixtures/04_HelloWorld.pdf")); | ||
var bytes = fs.readFileSync("tests/fixtures/04_HelloWorld.pdf"); | ||
isBinaryFile("tests/fixtures/04_HelloWorld.pdf", function (err, isBinary) { | ||
assert(!err); | ||
assert(isBinary); | ||
}); | ||
var stat = fs.lstatSync("tests/fixtures/04_HelloWorld.pdf"); | ||
assert(isBinaryFile(bytes, stat.size)); | ||
}); | ||
it('should not fail on a zero-byte file', function() { | ||
assert(!isBinaryFileSync("tests/fixtures/05_null_file.gif")); | ||
isBinaryFile("tests/fixtures/05_null_file.gif", function (err, isBinary) { | ||
assert(!err); | ||
assert(!isBinary); | ||
}); | ||
assert(!isBinaryFile("tests/fixtures/05_null_file.gif")); | ||
var bytes = fs.readFileSync("tests/fixtures/05_null_file.gif"); | ||
var stat = fs.lstatSync("tests/fixtures/05_null_file.gif"); | ||
assert(!isBinaryFile(bytes, stat.size)); | ||
}); | ||
it('should not fail on a gif', function() { | ||
assert(isBinaryFileSync("tests/fixtures/06_trunks.gif")); | ||
assert(isBinaryFile("tests/fixtures/06_trunks.gif")); | ||
isBinaryFile("tests/fixtures/06_trunks.gif", function (err, isBinary) { | ||
assert(!err); | ||
assert(isBinary); | ||
}); | ||
var bytes = fs.readFileSync("tests/fixtures/06_trunks.gif"); | ||
var stat = fs.lstatSync("tests/fixtures/06_trunks.gif"); | ||
assert(isBinaryFile(bytes, stat.size)); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82444
97