├── .editorconfig ├── .gitattributes ├── .gitignore ├── .travis.yml ├── index.js ├── license ├── package.json ├── readme.md └── test ├── fixtures ├── test.tar └── test.zip └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '6' 5 | - '4' 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const fileType = require('file-type'); 3 | 4 | const exts = new Set([ 5 | '7z', 6 | 'bz2', 7 | 'gz', 8 | 'rar', 9 | 'tar', 10 | 'zip', 11 | 'xz', 12 | 'gz' 13 | ]); 14 | 15 | module.exports = input => { 16 | const ret = fileType(input); 17 | return exts.has(ret && ret.ext) ? ret : null; 18 | }; 19 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Kevin Mårtensson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "archive-type", 3 | "version": "4.0.0", 4 | "description": "Detect the archive type of a Buffer/Uint8Array", 5 | "license": "MIT", 6 | "repository": "kevva/archive-type", 7 | "author": { 8 | "name": "Kevin Mårtensson", 9 | "email": "kevinmartensson@gmail.com", 10 | "url": "https://github.com/kevva" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "7zip", 23 | "archive", 24 | "buffer", 25 | "bz2", 26 | "bzip2", 27 | "check", 28 | "detect", 29 | "gz", 30 | "gzip", 31 | "mime", 32 | "rar", 33 | "zip", 34 | "file", 35 | "type" 36 | ], 37 | "dependencies": { 38 | "file-type": "^4.2.0" 39 | }, 40 | "devDependencies": { 41 | "ava": "*", 42 | "pify": "^2.3.0", 43 | "xo": "*" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # archive-type [![Build Status](https://travis-ci.org/kevva/archive-type.svg?branch=master)](https://travis-ci.org/kevva/archive-type) 2 | 3 | > Detect the archive type of a Buffer/Uint8Array 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install --save archive-type 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const archiveType = require('archive-type'); 17 | const readChunk = require('read-chunk'); 18 | const buffer = readChunk.sync('unicorn.zip', 0, 262); 19 | 20 | archiveType(buffer); 21 | //=> {ext: 'zip', mime: 'application/zip'} 22 | ``` 23 | 24 | 25 | ## API 26 | 27 | ### archiveType(input) 28 | 29 | Returns an `Object` with: 30 | 31 | - `ext` - One of the [supported file types](#supported-file-types) 32 | - `mime` - The [MIME type](http://en.wikipedia.org/wiki/Internet_media_type) 33 | 34 | Or `null` when no match. 35 | 36 | #### input 37 | 38 | Type: `Buffer` `Uint8Array` 39 | 40 | It only needs the first 262 bytes. 41 | 42 | 43 | ## Supported file types 44 | 45 | - `7z` 46 | - `bz2` 47 | - `gz` 48 | - `rar` 49 | - `tar` 50 | - `zip` 51 | - `xz` 52 | - `gz` 53 | 54 | 55 | ## Related 56 | 57 | - [archive-type-cli](https://github.com/kevva/archive-type-cli) - CLI for this module 58 | 59 | 60 | ## License 61 | 62 | MIT © [Kevin Mårtensson](https://github.com/kevva) 63 | -------------------------------------------------------------------------------- /test/fixtures/test.tar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevva/archive-type/b4d84d12b3a3a4d934c884aef89dccf2b6aaa1c9/test/fixtures/test.tar -------------------------------------------------------------------------------- /test/fixtures/test.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevva/archive-type/b4d84d12b3a3a4d934c884aef89dccf2b6aaa1c9/test/fixtures/test.zip -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | import path from 'path'; 3 | import pify from 'pify'; 4 | import test from 'ava'; 5 | import m from '..'; 6 | 7 | test(async t => { 8 | const tar = await pify(fs.readFile)(path.join(__dirname, 'fixtures/test.tar')); 9 | const zip = await pify(fs.readFile)(path.join(__dirname, 'fixtures/test.zip')); 10 | 11 | t.is(m(tar).ext, 'tar'); 12 | t.is(m(tar).mime, 'application/x-tar'); 13 | t.is(m(zip).ext, 'zip'); 14 | t.is(m(zip).mime, 'application/zip'); 15 | }); 16 | --------------------------------------------------------------------------------