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.9 to 1.0.0

LICENSE.txt

63

index.js

@@ -1,21 +0,47 @@

var fs = require('fs');
var fs = require("fs");
var path = require("path");
module.exports = function(bytes, size) {
var max_bytes = 512;
var max_bytes = 512;
// 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);
}
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);
}
return isBinaryCheck(size, bytes);
}
if (size == 0)
function isBinaryCheck (size, bytes) {
if (size === 0)
return false;

@@ -32,3 +58,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;

@@ -65,1 +91,4 @@ }

}
exports.isBinaryFile = isBinaryFile;
exports.isBinaryFileSync = isBinaryFileSync;
{
"name": "isbinaryfile",
"version" : "0.1.9",
"version" : "1.0.0",
"description": "Detects if a file is binary in Node.js. Similar to Perl's -B.",

@@ -5,0 +5,0 @@ "main" : "./lib/panino.js",

@@ -27,3 +27,3 @@ isBinaryFile

if (isBinaryFile(process.argv[2]))
if (isBinaryFileSync(process.argv[2]))
console.log("It is!")

@@ -42,3 +42,3 @@ else

fs.lstat(process.argv[2], function(err, stat) {
if (isBinaryFile(data, stat))
if (isBinaryFileSync(data, stat.size))
console.log("It is!")

@@ -51,2 +51,10 @@ else

### Async
Previous to version 1.0.0, this program always ran in sync mode. Now, there's an
async option. The async option is a method called `isBinaryFile`. Its callback
has two arguements: `err` and `isBinary`.
The sync option is called `isBinaryFileSync`.
## Testing

@@ -61,1 +69,27 @@

Then go into the _tests_ directory, and type `mocha test.js`.
# MIT License
Copyright (c) 2013 Garen J. Torikian
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

@@ -5,52 +5,60 @@ "mocha";

var fs = require("fs");
var isBinaryFile = require("../index");
var isBinaryFile = require("../index").isBinaryFile;
var isBinaryFileSync = require("../index").isBinaryFileSync;
describe('isBinaryFile', function() {
it('should fail on a binary program', function() {
assert(isBinaryFile("tests/fixtures/01_grep"));
assert(isBinaryFileSync("tests/fixtures/01_grep"));
var bytes = fs.readFileSync("tests/fixtures/01_grep");
var stat = fs.lstatSync("tests/fixtures/01_grep");
assert(isBinaryFile(bytes, stat.size));
isBinaryFile("tests/fixtures/01_grep", function (err, isBinary) {
assert(!err);
assert(isBinary);
});
});
it('should not fail on an extensionless script', function() {
assert(!isBinaryFile("tests/fixtures/02_perl_script"));
assert(!isBinaryFileSync("tests/fixtures/02_perl_script"));
var bytes = fs.readFileSync("tests/fixtures/02_perl_script");
var stat = fs.lstatSync("tests/fixtures/02_perl_script");
assert(!isBinaryFile(bytes, stat.size));
isBinaryFile("tests/fixtures/02_perl_script", function (err, isBinary) {
assert(!err);
assert(!isBinary);
});
});
it('should not fail on a russian text', function() {
assert(!isBinaryFile("tests/fixtures/03_Руководство_по_эксплуатации.rst"));
assert(!isBinaryFileSync("tests/fixtures/03_Руководство_по_эксплуатации.rst"));
var bytes = fs.readFileSync("tests/fixtures/03_Руководство_по_эксплуатации.rst");
var stat = fs.lstatSync("tests/fixtures/03_Руководство_по_эксплуатации.rst");
assert(!isBinaryFile(bytes, stat.size));
isBinaryFile("tests/fixtures/03_Руководство_по_эксплуатации.rst", function (err, isBinary) {
assert(!err);
assert(!isBinary);
});
});
it('should not fail on a PDF', function() {
assert(isBinaryFile("tests/fixtures/04_HelloWorld.pdf"));
assert(isBinaryFileSync("tests/fixtures/04_HelloWorld.pdf"));
var bytes = fs.readFileSync("tests/fixtures/04_HelloWorld.pdf");
var stat = fs.lstatSync("tests/fixtures/04_HelloWorld.pdf");
assert(isBinaryFile(bytes, stat.size));
isBinaryFile("tests/fixtures/04_HelloWorld.pdf", function (err, isBinary) {
assert(!err);
assert(isBinary);
});
});
it('should not fail on a zero-byte file', function() {
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));
assert(!isBinaryFileSync("tests/fixtures/05_null_file.gif"));
isBinaryFile("tests/fixtures/05_null_file.gif", function (err, isBinary) {
assert(!err);
assert(!isBinary);
});
});
it('should not fail on a gif', function() {
assert(isBinaryFile("tests/fixtures/06_trunks.gif"));
assert(isBinaryFileSync("tests/fixtures/06_trunks.gif"));
var bytes = fs.readFileSync("tests/fixtures/06_trunks.gif");
var stat = fs.lstatSync("tests/fixtures/06_trunks.gif");
assert(isBinaryFile(bytes, stat.size));
isBinaryFile("tests/fixtures/06_trunks.gif", function (err, isBinary) {
assert(!err);
assert(isBinary);
});
});
});
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