GitXplorerGitXplorer
g

isBinaryFile

public
165 stars
23 forks
5 issues

Commits

List of commits on branch main.
Verified
51b8c3a0b460e39cb6420ce8b7fe1e596887668a

Release 5.0.4

ggjtorikian committed 3 months ago
Verified
856dfd92ed1a5860c2896a00979de7b54528dc74

Merge pull request #79 from zimagen/main

ggjtorikian committed 3 months ago
Verified
e2bb5ec1e640f0c902598701c77c66a4ea31656c

Fix binary detection for text files containing emoji

zzimagen committed 3 months ago
Verified
45bd029dad4a3e43dd1db002a9730a3ceb0273fa

Release 5.0.3

ggjtorikian committed 3 months ago
Verified
c81f07859cc61a56f546e2505c7248645ce5071e

Merge pull request #78 from antonk52/main

ggjtorikian committed 3 months ago
Unverified
38a5d22c41b57e09a09d5e1c1d7ae727381d1b22

Update typescript target according to supported nodejs version

aantonk52 committed 3 months ago

README

The README file for this repository.

isBinaryFile

Detects if a file is binary in Node.js using ✨promises✨. Similar to Perl's -B switch, in that:

  • it reads the first few thousand bytes of a file
  • checks for a null byte; if it's found, it's binary
  • flags non-ASCII characters. After a certain number of "weird" characters, the file is flagged as binary

Much of the logic is pretty much ported from ag.

Note: if the file doesn't exist or is a directory, an error is thrown.

Installation

npm install isbinaryfile

Usage

Returns Promise<boolean> (or just boolean for *Sync). true if the file is binary, false otherwise.

isBinaryFile(filepath)

  • filepath - a string indicating the path to the file.

isBinaryFile(bytes[, size])

  • bytes - a Buffer of the file's contents.
  • size - an optional number indicating the file size.

isBinaryFileSync(filepath)

  • filepath - a string indicating the path to the file.

isBinaryFileSync(bytes[, size])

  • bytes - a Buffer of the file's contents.
  • size - an optional number indicating the file size.

Examples

Here's an arbitrary usage:

const isBinaryFile = require("isbinaryfile").isBinaryFile;
const fs = require("fs");

const filename = "fixtures/pdf.pdf";
const data = fs.readFileSync(filename);
const stat = fs.lstatSync(filename);

isBinaryFile(data, stat.size).then((result) => {
  if (result) {
    console.log("It is binary!")
  }
  else {
    console.log("No it is not.")
  }
});

const isBinaryFileSync = require("isbinaryfile").isBinaryFileSync;
const bytes = fs.readFileSync(filename);
const size = fs.lstatSync(filename).size;
console.log(isBinaryFileSync(bytes, size)); // true or false

Testing

Run npm install, then run npm test.