Nobody Pays (for) Magazines
Join us to discuss the challenges, solutions and best practices for in-house JavaScript code sharing. Tuesday, 12/17 at 10am PT/1pm ET.Sign up here »

mime-stream

2.0.0 • Public • Published

mime-stream

Build Status Test Coverage Maintainability

Node pass-through stream for MIME type detection.

Install

npm i mime-stream

Usage

Event: 'type'

This event is emitted as soon as the type is detected. If the type remains unknown, null is passed to the event handler instead of an object.

It is guaranteed that there is always exactly one type event emitted, even when the stream is closed prematurely.

Example:

const fs = require("fs");
const MimeStream = require("mime-stream");
 
let stream = new MimeStream();
stream.on("type", function (type) {
    console.log(type); // { ext: "png", mime: "image/png" }
});
 
fs.createReadStream("myimage.png").pipe(stream);

You could also add more .pipe() calls to the chain. MimeStream is non-destructive and passes on any data it receives.

Listener Function

This is the same as binding a function to the type event, just more concise.

Example:

const fs = require("fs");
const MimeStream = require("mime-stream");
 
// `new` is optional anyway
fs.createReadStream("myimage.png").pipe(MimeStream((type) => {
    console.log(type); // { ext: "png", mime: "image/png" }
}));

Property

After detection, the detection result is also available inside the type property. Example:

stream.on("end", function () {
    console.log(stream.type); // { ext: "png", mime: "image/png" }
});

Install

npm i mime-stream

DownloadsWeekly Downloads

31

Version

2.0.0

License

MIT

Unpacked Size

12.6 kB

Total Files

9

Last publish

Collaborators

  • avatar