README.md 2.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
# isBinaryFile

Detects if a file is binary in Node.js. Similar to [Perl's `-B` switch](http://stackoverflow.com/questions/899206/how-does-perl-know-a-file-is-binary), 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](https://github.com/ggreer/the_silver_searcher).

Note: if the file doesn't exist, is a directory, or is empty, the function returns `false`.

## Installation

```
npm install isbinaryfile
```

## Usage

### isBinaryFile(filepath, callback)

* `filepath`, a `string` indicating the path to the file.
* `callback`, a `function` for the callback. It has two arguments:
  - `err`, the typical Node.js error argument
  - `result`, a `boolean` of `true` or `false`, depending on if the file is binary


### isBinaryFile(bytes, size, callback)

* `bytes`, a `Buffer` of the file's contents.
* `size`, an optional `number` indicating the file size.
* `callback`, a `function` for the callback. It has two arguments:
  - `err`, the typical Node.js error argument
  - `result`, a `boolean` of `true` or `false`, depending on if the file is binary


### isBinaryFile.sync(filepath)

* `filepath`, a `string` indicating the path to the file.


### isBinaryFile.sync(bytes, size)

* `bytes`, a `Buffer` of the file's contents.
* `size`, an `number` indicating the file size.


Returns a `boolean` of `true` or `false`, depending on if the file is binary.

### Examples

```javascript
var isBinaryFile = require("isbinaryfile");

fs.readFile("some_file", function(err, data) {
  fs.lstat("some_file", function(err, stat) {
    isBinaryFile(data, stat.size, function (err, result) {
      if (!err) {
        if (result) {
          console.log("It is!")
        }
        else {
          console.log("No.")
        }
      }
    });
  });
});

isBinaryFile.sync("some_file"); // true or false
var bytes = fs.readFileSync(("some_file"));
var size = fs.lstatSync(("some_file").size;
isBinaryFile.sync(bytes, size); // true or false
```

## Testing

Run `npm install` to install `mocha`, then run `npm test`.