
Security News
Next.js Patches Critical Middleware Vulnerability (CVE-2025-29927)
Next.js has patched a critical vulnerability (CVE-2025-29927) that allowed attackers to bypass middleware-based authorization checks in self-hosted apps.
media-typer
Advanced tools
The media-typer npm package is a simple utility for parsing and formatting media types as per RFC 6838. It allows you to parse media type strings, extract their components, and format an object representing a media type back into a string. It is useful for handling and validating Content-Type and Accept HTTP headers.
Parsing media types
This feature allows you to parse a media type string into its constituent parts, such as type, subtype, and parameters. The example code demonstrates how to parse the media type 'application/json'.
const mediaTyper = require('media-typer');
const parsed = mediaTyper.parse('application/json');
console.log(parsed);
Formatting media types
This feature enables you to format an object representing a media type back into a string. The example code shows how to format an object with type 'image' and subtype 'png' into the string 'image/png'.
const mediaTyper = require('media-typer');
const formatted = mediaTyper.format({ type: 'image', subtype: 'png' });
console.log(formatted);
Validating media type names
This feature is used to validate media type strings and will throw an error if the media type is invalid. The example code attempts to parse an invalid media type and catches the error.
const mediaTyper = require('media-typer');
try {
mediaTyper.parse('invalid/type');
} catch (e) {
console.error('Invalid media type', e.message);
}
The 'content-type' package is similar to media-typer as it provides utilities for parsing and formatting Content-Type header strings. It differs in that it focuses specifically on the Content-Type header and provides more functionality around charset handling and parameter parsing.
The 'mime-types' package is another similar package that allows for looking up the content-type associated with a file extension and vice versa. It provides a larger database of MIME types but does not focus on parsing and formatting of media type strings as media-typer does.
The 'negotiator' package is used for content negotiation in HTTP transactions. It can parse Accept headers and determine the best match for a response. While it deals with media types, its primary focus is on the negotiation process rather than just parsing and formatting media types.
Simple RFC 6838 media type parser.
This module will parse a given media type into it's component parts, like type, subtype, and suffix. A formatter is also provided to put them back together and the two can be combined to normalize media types into a canonical form.
If you are looking to parse the string that represents a media type and it's
parameters in HTTP (for example, the Content-Type
header), use the
content-type module.
This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
$ npm install media-typer
var typer = require('media-typer')
var obj = typer.parse('image/svg+xml')
Parse a media type string. This will return an object with the following
properties (examples are shown for the string 'image/svg+xml; charset=utf-8'
):
type
: The type of the media type (always lower case). Example: 'image'
subtype
: The subtype of the media type (always lower case). Example: 'svg'
suffix
: The suffix of the media type (always lower case). Example: 'xml'
If the given type string is invalid, then a TypeError
is thrown.
var obj = typer.format({ type: 'image', subtype: 'svg', suffix: 'xml' })
Format an object into a media type string. This will return a string of the
mime type for the given object. For the properties of the object, see the
documentation for typer.parse(string)
.
If any of the given object values are invalid, then a TypeError
is thrown.
var valid = typer.test('image/svg+xml')
Validate a media type string. This will return true
is the string is a well-
formatted media type, or false
otherwise.
FAQs
Simple RFC 6838 media type parser and formatter
The npm package media-typer receives a total of 32,803,827 weekly downloads. As such, media-typer popularity was classified as popular.
We found that media-typer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Next.js has patched a critical vulnerability (CVE-2025-29927) that allowed attackers to bypass middleware-based authorization checks in self-hosted apps.
Security News
A survey of 500 cybersecurity pros reveals high pay isn't enough—lack of growth and flexibility is driving attrition and risking organizational security.
Product
Socket, the leader in open source security, is now available on Google Cloud Marketplace for simplified procurement and enhanced protection against supply chain attacks.