Skip to content

Commit

Permalink
Validate the input argument type
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 2, 2018
1 parent 7ab526a commit ba2fdde
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Expand Up @@ -16,6 +16,10 @@ function readUInt64LE(buf, offset = 0) {
}

module.exports = 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}\``);
}

const buf = input instanceof Uint8Array ? input : new Uint8Array(input);

if (!(buf && buf.length > 1)) {
Expand Down
14 changes: 14 additions & 0 deletions test.js
Expand Up @@ -197,3 +197,17 @@ for (const type of types) {
test('fileType.minimumBytes', t => {
t.true(fileType.minimumBytes > 4000);
});

test('validate the input argument type', t => {
t.throws(() => {
fileType('x');
}, /Expected the `input` argument to be of type `Uint8Array`/);

t.notThrows(() => {
fileType(Buffer.from('x'));
});

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

0 comments on commit ba2fdde

Please sign in to comment.