New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

stream-mime-type

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stream-mime-type

Get the mime type of a stream

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
27K
decreased by-28.2%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 07 Feb 2023

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