mime-kind
Detect the MIME type of a Buffer
, Uint8Array
, ArrayBuffer
, ReadableStream
, file path and file name, with async
method.
Install
$ npm install mime-kind --save
Usage
const { createReadStream } = require('fs');
const mime = require('mime-kind');
await mime('.fakeext');
await mime(createReadStream('./anonim.jpg'));
await mime('c:/anonim.jpeg');
API
mime(input, [defaultValue])
mime.async(input, [defaultValue])
Returns a Promise
with an object (or null
when no match) with:
This methods supports all kind of ReadableStreams
.
Params:
- input (String|Buffer|Uint8Array|ArrayBuffer|ReadableStream) -
Buffer
, Uint8Array
, ArrayBuffer
, ReadableStream
, file path or file name. - [defaultValue] (String|Object) -
String
or Object
with value which will be returned if no match will be found. If defaultValue
is incorrect returns null
.
const mime = require('mime-kind');
await mime('.fakeext', 'application/octet-stream');
await mime.async('.fakeext', { ext: 'mp4', mime: 'video/mp4' });
await mime.async('.fakeext', 'ogg');
await mime.async('.fakeext', 'ogg3');
await mime('.fakeext', { ext: 'fake', mime: 'fake/fake' });
With HTTP(S)
streams:
const mime = require('mime-kind');
const { get } = require('https');
const { PassThrough } = require('stream');
const pass = new PassThrough();
get('https://avatars0.githubusercontent.com/u/2401029', res => res.pipe(pass));
await mime(pass);
License
The MIT License (MIT)
Copyright (c) 2015-2022 Alexey Bystrov