Skip to content

Commit

Permalink
Release 4.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
gjtorikian committed Dec 29, 2019
1 parent 725b9f0 commit db72c42
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "isbinaryfile",
"description": "Detects if a file is binary in Node.js. Similar to Perl's -B.",
"version": "4.0.2",
"version": "4.0.3",
"keywords": [
"text",
"binary",
Expand Down
65 changes: 45 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as fs from 'fs';
import {promisify} from 'util';
import { promisify } from 'util';

const statAsync = promisify(fs.stat);
const openAsync = promisify(fs.open);
const closeAsync = promisify(fs.close);

const MAX_BYTES = 512
const MAX_BYTES = 512;

export async function isBinaryFile(file: string | Buffer, size?: number): Promise<boolean> {
if (isString(file)) {
Expand All @@ -22,13 +22,17 @@ export async function isBinaryFile(file: string | Buffer, size?: number): Promis
return new Promise((fulfill, reject) => {
fs.read(fileDescriptor, allocBuffer, 0, MAX_BYTES, 0, (err, bytesRead, _) => {
closeAsync(fileDescriptor);
if (err) { reject(err) }
else { fulfill(isBinaryCheck(allocBuffer, bytesRead)); }
if (err) {
reject(err);
} else {
fulfill(isBinaryCheck(allocBuffer, bytesRead));
}
});
});
}
else {
if (size === undefined) { size = file.length; }
} else {
if (size === undefined) {
size = file.length;
}
return isBinaryCheck(file, size);
}
}
Expand All @@ -47,16 +51,19 @@ export function isBinaryFileSync(file: string | Buffer, size?: number): boolean
fs.closeSync(fileDescriptor);

return isBinaryCheck(allocBuffer, bytesRead);
}
else {
if (size === undefined) { size = file.length; }
} else {
if (size === undefined) {
size = file.length;
}
return isBinaryCheck(file, size);
}
}

function isBinaryCheck(fileBuffer: Buffer, bytesRead: number): boolean {
// empty file. no clue what it is.
if (bytesRead === 0) { return false; }
if (bytesRead === 0) {
return false;
}

let suspiciousBytes = 0;
const totalBytes = Math.min(bytesRead, MAX_BYTES);
Expand All @@ -67,17 +74,35 @@ function isBinaryCheck(fileBuffer: Buffer, bytesRead: number): boolean {
}

// UTF-32 BOM
if (bytesRead >= 4 && fileBuffer[0] === 0x00 && fileBuffer[1] === 0x00 && fileBuffer[2] === 0xfe && fileBuffer[3] === 0xff) {
if (
bytesRead >= 4 &&
fileBuffer[0] === 0x00 &&
fileBuffer[1] === 0x00 &&
fileBuffer[2] === 0xfe &&
fileBuffer[3] === 0xff
) {
return false;
}

// UTF-32 LE BOM
if (bytesRead >= 4 && fileBuffer[0] === 0xff && fileBuffer[1] === 0xfe && fileBuffer[2] === 0x00 && fileBuffer[3] === 0x00) {
if (
bytesRead >= 4 &&
fileBuffer[0] === 0xff &&
fileBuffer[1] === 0xfe &&
fileBuffer[2] === 0x00 &&
fileBuffer[3] === 0x00
) {
return false;
}

// GB BOM
if (bytesRead >= 4 && fileBuffer[0] === 0x84 && fileBuffer[1] === 0x31 && fileBuffer[2] === 0x95 && fileBuffer[3] === 0x33) {
if (
bytesRead >= 4 &&
fileBuffer[0] === 0x84 &&
fileBuffer[1] === 0x31 &&
fileBuffer[2] === 0x95 &&
fileBuffer[3] === 0x33
) {
return false;
}

Expand All @@ -100,16 +125,14 @@ function isBinaryCheck(fileBuffer: Buffer, bytesRead: number): boolean {
if (fileBuffer[i] === 0) {
// NULL byte--it's binary!
return true;
}
else if ((fileBuffer[i] < 7 || fileBuffer[i] > 14) && (fileBuffer[i] < 32 || fileBuffer[i] > 127)) {
} else if ((fileBuffer[i] < 7 || fileBuffer[i] > 14) && (fileBuffer[i] < 32 || fileBuffer[i] > 127)) {
// UTF-8 detection
if (fileBuffer[i] > 193 && fileBuffer[i] < 224 && i + 1 < totalBytes) {
i++;
if (fileBuffer[i] > 127 && fileBuffer[i] < 192) {
continue;
}
}
else if (fileBuffer[i] > 223 && fileBuffer[i] < 240 && i + 2 < totalBytes) {
} else if (fileBuffer[i] > 223 && fileBuffer[i] < 240 && i + 2 < totalBytes) {
i++;
if (fileBuffer[i] > 127 && fileBuffer[i] < 192 && fileBuffer[i + 1] > 127 && fileBuffer[i + 1] < 192) {
i++;
Expand All @@ -133,9 +156,11 @@ function isBinaryCheck(fileBuffer: Buffer, bytesRead: number): boolean {
}

function isString(x: any): x is string {
return typeof x === "string";
return typeof x === 'string';
}

function isStatFile(stat: fs.Stats): void {
if (!stat.isFile()) { throw new Error(`Path provided was not a file!`); }
if (!stat.isFile()) {
throw new Error(`Path provided was not a file!`);
}
}

0 comments on commit db72c42

Please sign in to comment.