Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stpettersens/node-magic-number

:seven: Node.js module to determine a file's type from its magic number.
https://github.com/stpettersens/node-magic-number

library magic module nodejs number

Last synced: about 1 month ago
JSON representation

:seven: Node.js module to determine a file's type from its magic number.

Awesome Lists containing this project

README

        

#### Magic-number

[![Build Status](https://travis-ci.org/stpettersens/node-magic-number.svg?branch=master)](https://travis-ci.org/stpettersens/node-magic-number) [![npm version](https://badge.fury.io/js/magic-number.svg)](http://npmjs.org/package/magic-number) [![Development Dependency Status](https://david-dm.org/stpettersens/node-magic-number/dev-status.png?theme=shields.io)](https://david-dm.org/stpettersens/node-magic-number?type=dev)

Node.js module to determine a file's type from its magic number.

Use from JavaScript:

```js
var magic = require('magic-number');
magic.detectFile('file.zip'); // ==> 'application/zip'
magic.detectFile('file.7z'); // ==> 'application/x-7z-compressed'
```

Use from [TypeScript](http://www.typescriptlang.org):

```ts
///
import magic = require('magic-number');
magic.detectFile('file.zip'); // ==> 'application/zip'
magic.detectFile('file.7z'); // ==> 'application/x-7z-compressed'
```

Methods:

**magic.detectFile(file: string): string**

Parameter *file* is path string for the file to test.

**magic.detectType(data: any): string**

Parameter *data* is binary data for the file to test.

Returns a MIME type for the tested file, 'unknown' if file type is not detected or
message when file doesn't exist.