Socket
Socket
Sign inDemoInstall

is-mime

Package Overview
Dependencies
3
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    is-mime

Check if stream or buffer is a valid mime-type


Version published
Weekly downloads
3
increased by50%
Maintainers
1
Install size
94.4 kB
Created
Weekly downloads
 

Readme

Source

Is Mime

Build Status

Checks whether a buffer, stream or file is a valid MIME type. Checks known byte offsets for magic numbers.

Examples

Checking buffers:

var fs = require('fs')
  , assert = require('assert')
  , isMime = require('is-mime')

fs.readFile(__dirname + '/image.png', function(err, buffer) {
  if (err) throw err

  // simply pass a type and buffer to `checkBuffer`
  // it will return the type string or null
  assert.ok(isMime.checkBuffer('image/png', buffer))
  // type argument can be an array too
})

Checking files:

var assert = require('assert')
  , isMime = require('is-mime')
  , types = ['image/jpeg', 'image/png']

isMime.checkFile(types, __dirname + '/image.png')
// returns a Promise for a type string or null
.then(function (mimetype) {
  assert.equal(mimetype, 'image/png')
})

Checking streams:

var fs = require('fs')
  , assert = require('assert')
  , isMime = require('is-mime')
  , input, checker

input = fs.createReadStream(__dirname + '/image.jpg')

// checker is a PassThrough stream that emits `mimetype` event
checker = isMime.checkStream(['image/jpeg', 'image/png'])

checker.on('mimetype', function(mimetype) {
  // emitted once it is certain that stream contains
  // one of the provided types

  // argument is the detected mime type
  assert.equal(mimetype, 'image/jpeg')

  // also sets a `mimetype` property on stream object itself
  assert.equal(checker.mimetype, 'image/jpeg')
})

input.pipe(checker)
checker.resume() // discard data

Copy a file and print the detected MIME type:

var fs = require('fs')
  , isMime = require('is-mime')

fs.createReadStream(__dirname + '/image.png')
.pipe(
  isMime.checkStream([
    'image/jpeg',
    'image/png',
    'image/gif',
  ])
  .on('mimetype', console.log)
)
.pipe(
  fs.createWriteStream(__dirname + '/image2.png')
  .on('end', function() {
    console.log('Copying finished')
  })
)

Supported MIME types

  • image/png
  • image/jpeg
  • image/gif
  • image/webp
  • audio/mpeg (MP3)
  • audio/ogg (Vorbis, Opus)
  • video/webm
  • video/mp4

Keywords

FAQs

Last updated on 28 Jun 2015

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc