npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

stream-mime-type

v2.0.0

Published

Get the mime type of a stream

Downloads

45,265

Readme

npm version downloads build status coverage status

stream-mime-type

Get the mime type of a stream by inspecting its header. The stream is being consumed, but a new readable stream is returned together with the mime type.

Supports both Node.js ReadableStream (of Buffers, not object-mode) and Streams API ReadableStream of typed arrays (such as e.g. Uint8Array), ArrayBuffers or DataViews.

The first couple of kilobytes are necessary to deduce the filetype. After that, the returned promise is resolved with a new stream (from the beginning of the input stream) and the mime type.

It also supports buffers (of type Uint8Array, such as Node.js Buffers) and file descriptors instead of streams.

Versions

From v2:

  • This package is a pure ESM, no CommonJS support

Usage

getMimeType takes either a number (file descriptor), a buffer (Uint8Array) or a readable stream as first argument. The returned promise contains the mime type and a new readable stream (if the input was a stream) for further streaming, on the form:

interface ReturnType {
    stream: ReadableStream; // If the input was a stream, otherwise undefined
    mime: string;
}

To aid finding the mime-type if it is unclear given the content of the stream (or buffer or file descriptor), a filename can be provided, in which case a mime-type can be deduced as a fallback.

API

getMimeType( stream | buffer | fd, opts? ): Promise<ReturnType>;

Stream example

import { getMimeType } from 'stream-mime-type'

let inStream: NodeJS.ReadableStream; // Get from somewhere
const { stream, mime } = await getMimeType( inStream );

console.log(mime); // e.g. 'image/png'
stream.pipe(outStream); // Keep streaming

Buffer example

import { readFileSync } from 'fs'
import { getMimeType } from 'stream-mime-type'

const imageBuffer = readFileSync("file.png", null); // Returns a Buffer
const { mime } = await getMimeType( imageBuffer, { filename: "file.png" } );

console.log(mime); // 'image/png'

File descriptor

import { openSync } from 'fs'
import { getMimeType } from 'stream-mime-type'

const fd = openSync("file.png", "r");
const { mime } = await getMimeType( fd, { filename: "file.png" } );

console.log(mime); // 'image/png'

Options

The second argument to getMimeType is an options object on the format:

interface GetMimeTypeOptions {
    strict?: boolean;
    filename?: string;
}

The filename can provide a hint to the content type if it's not found by the file contents.

The strict can be set to true (defaults to false) in which case the result will not fallback to application/octet-stream if no other mime type was found. The return value will be undefined instead.