Socket
Socket
Sign inDemoInstall

mime-kind

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mime-kind

Detect the MIME type of a Buffer, Uint8Array, ArrayBuffer, ReadableStream, file path and file name, with sync and async methods.


Version published
Weekly downloads
23K
increased by6.31%
Maintainers
1
Weekly downloads
 
Created
Source

mime-kind License npm

Build Status node Test Coverage

Detect the MIME type of a Buffer, Uint8Array, ArrayBuffer, ReadableStream, file path and file name, with sync and async methods.

Install

$ npm install mime-kind --save

Usage

const { createReadStream } = require('fs');
const { sync: mime, async: mimeAsync } = require('mime-kind');

mime('c:/anonim.jpeg'); // -> { ext: 'jpeg', mime: 'image/jpeg' }
mime('.fakeext'); // -> null
mime(createReadStream('./anonim.jpg')); // -> { ext: 'jpeg', mime: 'image/jpeg' }
await mimeAsync('c:/anonim.jpeg'); // -> { ext: 'jpeg', mime: 'image/jpeg' }

API

mime(input, [defaultValue])

mime.async(input, [defaultValue])

Returns a Promise with an object (or null when no match) with:

  • ext - file type
  • mime - the MIME type

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'); // -> { ext: 'bin', mime: 'application/octet-stream' }
await mime.async('.fakeext', { ext: 'mp4', mime: 'video/mp4' }); // -> { ext: 'mp4', mime: 'video/mp4' }
await mime.async('.fakeext', 'ogg'); // -> { ext: 'ogg', mime: 'audio/ogg' }
// but
await mime.async('.fakeext', 'ogg3'); // -> null
await mime('.fakeext', { ext: 'fake', mime: 'fake/fake' }); // -> null

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); // -> { ext: 'jpg', mime: 'image/jpeg' }

mime.sync(input, [defaultValue])

Returns an object (or null when no match) with:

This methods supports only fs.ReadStream as ReadableStream.

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');

mime.sync('.fakeext', 'application/octet-stream'); // -> { ext: 'bin', mime: 'application/octet-stream' }
mime.sync('.fakeext', { ext: 'mp4', mime: 'video/mp4' }); // -> { ext: 'mp4', mime: 'video/mp4' }
mime.sync('.fakeext', 'ogg'); // -> { ext: 'ogg', mime: 'audio/ogg' }
// but
mime.sync('.fakeext', 'ogg3'); // -> null
mime.sync('.fakeext', { ext: 'fake', mime: 'fake/fake' }); // -> null

License

The MIT License (MIT)
Copyright (c) 2015-2019 Alexey Bystrov

Keywords

FAQs

Package last updated on 09 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc