Skip to content

Commit

Permalink
Allow ArrayBuffer as input (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
razor-x authored and sindresorhus committed Apr 11, 2019
1 parent ee0ee82 commit b714ddd
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ declare namespace fileType {

declare const fileType: {
/**
Detect the file type of a `Buffer`/`Uint8Array`. The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer.
Detect the file type of a `Buffer`/`Uint8Array`/`ArrayBuffer`. The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer.
@param buffer - It only needs the first `.minimumBytes` bytes. The exception is detection of `docx`, `pptx`, and `xlsx` which potentially requires reading the whole file.
@returns An object with the detected file type and MIME type or `null` when there was no match.
Expand Down Expand Up @@ -147,7 +147,7 @@ declare const fileType: {
});
```
*/
(buffer: Buffer | Uint8Array): fileType.FileTypeResult | null;
(buffer: Buffer | Uint8Array | ArrayBuffer): fileType.FileTypeResult | null;

/**
The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hard-code it.
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ function readUInt64LE(buf, offset = 0) {
}

const fileType = input => {
if (!(input instanceof Uint8Array || Buffer.isBuffer(input))) {
throw new TypeError(`Expected the \`input\` argument to be of type \`Uint8Array\` or \`Buffer\`, got \`${typeof input}\``);
if (!(input instanceof Uint8Array || input instanceof ArrayBuffer || Buffer.isBuffer(input))) {
throw new TypeError(`Expected the \`input\` argument to be of type \`Uint8Array\` or \`Buffer\` or \`ArrayBuffer\`, got \`${typeof input}\``);
}

const buf = input instanceof Uint8Array ? input : new Uint8Array(input);
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as fs from 'fs';

expectType<FileTypeResult | null>(fileType(new Buffer([0xff, 0xd8, 0xff])));
expectType<FileTypeResult | null>(fileType(new Uint8Array([0xff, 0xd8, 0xff])));
expectType<FileTypeResult | null>(fileType(new ArrayBuffer(42)));

const result = fileType(new Buffer([0xff, 0xd8, 0xff]));
if (result != null) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "file-type",
"version": "10.10.0",
"description": "Detect the file type of a Buffer/Uint8Array",
"description": "Detect the file type of a Buffer/Uint8Array/ArrayBuffer",
"license": "MIT",
"repository": "sindresorhus/file-type",
"author": {
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# file-type [![Build Status](https://travis-ci.org/sindresorhus/file-type.svg?branch=master)](https://travis-ci.org/sindresorhus/file-type)

> Detect the file type of a Buffer/Uint8Array
> Detect the file type of a Buffer/Uint8Array/ArrayBuffer
The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer.

Expand Down Expand Up @@ -99,7 +99,7 @@ Or `null` when there is no match.

#### input

Type: `Buffer` `Uint8Array`
Type: `Buffer | Uint8Array | ArrayBuffer`

It only needs the first `.minimumBytes` bytes. The exception is detection of `docx`, `pptx`, and `xlsx` which potentially requires reading the whole file.

Expand Down
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,8 @@ test('validate the input argument type', t => {
t.notThrows(() => {
fileType(new Uint8Array());
});

t.notThrows(() => {
fileType(new ArrayBuffer());
});
});

0 comments on commit b714ddd

Please sign in to comment.