Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ArrayBuffer as input #193

Merged
merged 2 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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());
});
});