isbinaryfile
Advanced tools
Comparing version 0.1.8 to 0.1.9
29
index.js
var fs = require('fs'); | ||
module.exports = function(bytes, size) { | ||
var max_bytes = 512; | ||
// Read the file with no encoding for raw buffer access. | ||
@@ -9,12 +11,17 @@ if (size === undefined) { | ||
return false; | ||
bytes = fs.readFileSync(file); | ||
size = fs.statSync(file).size; | ||
} | ||
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); | ||
} | ||
} | ||
if (size == 0) | ||
if (size == 0) | ||
return false; | ||
var suspicious_bytes = 0; | ||
var total_bytes = size > 512 ? 512 : size; | ||
var total_bytes = Math.min(size, max_bytes); | ||
if (size >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) { | ||
@@ -25,3 +32,3 @@ // UTF-8 BOM. This isn't binary. | ||
for (var i = 0; i < total_bytes; i++) { | ||
for (var i = 0; i < total_bytes; i++) { | ||
if (bytes[i] == 0) { // NULL byte--it's binary! | ||
@@ -37,3 +44,3 @@ return true; | ||
} | ||
} | ||
} | ||
else if (bytes[i] > 223 && bytes[i] < 239 && i + 2 < total_bytes) { | ||
@@ -50,3 +57,3 @@ i++; | ||
return true; | ||
} | ||
} | ||
} | ||
@@ -58,4 +65,4 @@ } | ||
} | ||
return false; | ||
} | ||
} |
{ | ||
"name": "isbinaryfile", | ||
"version" : "0.1.8", | ||
"version" : "0.1.9", | ||
"description": "Detects if a file is binary in Node.js. Similar to Perl's -B.", | ||
@@ -21,2 +21,2 @@ "main" : "./lib/panino.js", | ||
} | ||
} | ||
} |
80000
97