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

Removed minimumBytes #319

Merged
merged 1 commit into from
Feb 1, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion browser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export declare function fromBlob(blob: Blob): Promise<core.FileTypeResult | unde

export {
fromBuffer,
minimumBytes,
extensions,
mimeTypes
} from './core';
5 changes: 0 additions & 5 deletions core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,6 @@ declare namespace core {
*/
function fromTokenizer(tokenizer: ITokenizer): Promise<core.FileTypeResult | undefined>;

/**
Deprecated: 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.
*/
const minimumBytes: number;

/**
Supported file extensions.
*/
Expand Down
5 changes: 2 additions & 3 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ const stream = readableStream => new Promise((resolve, reject) => {
readableStream.on('error', reject);
readableStream.once('readable', async () => {
const pass = new stream.PassThrough();
const chunk = readableStream.read(fileType.minimumBytes) || readableStream.read();
const chunk = readableStream.read(minimumBytes) || readableStream.read();
try {
const fileType = await fromBuffer(chunk);
pass.fileType = fileType;
Expand All @@ -1263,8 +1263,7 @@ const fileType = {
fromStream,
fromTokenizer,
fromBuffer,
stream,
minimumBytes: 4100
stream
};

Object.defineProperty(fileType, 'extensions', {
Expand Down
1 change: 0 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export function fromFile(path: string): Promise<core.FileTypeResult | undefined>
export {
fromBuffer,
fromStream,
minimumBytes,
extensions,
mimeTypes,
stream
Expand Down
3 changes: 0 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,10 @@ expectType<Promise<FileTypeResult | undefined>>(FileType.fromBuffer(new ArrayBuf
}
})();

expectType<number>(FileType.minimumBytes);

expectType<readonly FileType.FileExtension[]>(FileType.extensions);

expectType<readonly FileType.MimeType[]>(FileType.mimeTypes);


const readableStream = fs.createReadStream('file.png');
const streamWithFileType = FileType.stream(readableStream);
expectType<Promise<ReadableStreamWithFileType>>(streamWithFileType);
Expand Down
8 changes: 1 addition & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const FileType = require('file-type');
const readChunk = require('read-chunk');

(async () => {
const buffer = readChunk.sync('Unicorn.png', 0, fileType.minimumBytes);
const buffer = readChunk.sync('Unicorn.png', 0, 4100);

console.log(await FileType.fromBuffer(buffer));
//=> {ext: 'png', mime: 'image/png'}
Expand Down Expand Up @@ -237,12 +237,6 @@ Type: [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream

The input stream.

### FileType.minimumBytes

Type: `number`

The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hardcode it.

### FileType.extensions

Returns a set of supported file extensions.
Expand Down
27 changes: 5 additions & 22 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import path from 'path';
import fs from 'fs';
import stream from 'stream';
import test from 'ava';
import readChunk from 'read-chunk';
import pify from 'pify';
import {readableNoopStream} from 'noop-stream';
import FileType from '.';
Expand Down Expand Up @@ -151,12 +150,6 @@ const names = {
]
};

// Following types cannot be detected within 4k sample size boundary
const cannotDetectInBuffer = [
'fixture-id3v2.aac',
'fixture-id3v2.flac'
];

// Define an entry here only if the file type has potential
// for false-positives
const falsePositives = {
Expand Down Expand Up @@ -188,15 +181,15 @@ async function testFromBuffer(t, ext, name) {
const fixtureName = `${(name || 'fixture')}.${ext}`;

const file = path.join(__dirname, 'fixture', fixtureName);
const chunk = readChunk.sync(file, 0, 4 + 4096);
const chunk = fs.readFileSync(file);
await checkBufferLike(t, ext, chunk);
await checkBufferLike(t, ext, new Uint8Array(chunk));
await checkBufferLike(t, ext, chunk.buffer);
await checkBufferLike(t, ext, chunk.buffer.slice(chunk.byteOffset, chunk.byteOffset + chunk.byteLength));
}

async function testFalsePositive(t, ext, name) {
const file = path.join(__dirname, 'fixture', `${name}.${ext}`);
const chunk = readChunk.sync(file, 0, 4 + 4096);
const chunk = fs.readFileSync(file);

t.is(await FileType.fromBuffer(chunk), undefined);
t.is(await FileType.fromBuffer(new Uint8Array(chunk)), undefined);
Expand Down Expand Up @@ -245,20 +238,14 @@ let i = 0;
for (const type of types) {
if (Object.prototype.hasOwnProperty.call(names, type)) {
for (const name of names[type]) {
const fixtureName = `${name}.${type}`;
const test4k = cannotDetectInBuffer.includes(fixtureName) ? test.failing : test;

test(`${name}.${type} ${i++} .fromFile() method - same fileType`, testFromFile, type, name);
test4k(`${name}.${type} ${i++} .fromBuffer() method - same fileType`, testFromBuffer, type, name);
test(`${name}.${type} ${i++} .fromBuffer() method - same fileType`, testFromBuffer, type, name);
test(`${name}.${type} ${i++} .fromStream() method - same fileType`, testFileFromStream, type, name);
test(`${name}.${type} ${i++} .stream() - identical streams`, testStream, type, name);
}
} else {
const fixtureName = `fixture.${type}`;
const test4k = cannotDetectInBuffer.includes(fixtureName) ? test.failing : test;

test(`${type} ${i++} .fromFile()`, testFromFile, type);
test4k(`${type} ${i++} .fromBuffer()`, testFromBuffer, type);
test(`${type} ${i++} .fromBuffer()`, testFromBuffer, type);
test(`${type} ${i++} .fromStream()`, testFileFromStream, type);
test(`${type} ${i++} .stream() - identical streams`, testStream, type);
}
Expand Down Expand Up @@ -291,10 +278,6 @@ test('.stream() method - error event', async t => {
await t.throwsAsync(FileType.stream(readableStream), errorMessage);
});

test('FileType.minimumBytes', t => {
t.true(FileType.minimumBytes > 4000);
});

test('FileType.extensions.has', t => {
t.true(FileType.extensions.has('jpg'));
t.false(FileType.extensions.has('blah'));
Expand Down