
Security News
Potemkin Understanding in LLMs: New Study Reveals Flaws in AI Benchmarks
New research reveals that LLMs often fake understanding, passing benchmarks but failing to apply concepts or stay internally consistent.
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
$ npm install media-typer
var typer = require('media-typer')
var obj = typer.parse('image/svg+xml; charset=utf-8')
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'
parameters
: An object of the parameters in the media type (name of parameter always lower case). Example: {charset: 'utf-8'}
var obj = typer.parse(req)
Parse the content-type
header from the given req
. Short-cut for
typer.parse(req.headers['content-type'])
.
var obj = typer.parse(res)
Parse the content-type
header set on the given res
. Short-cut for
typer.parse(res.getHeader('content-type'))
.
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)
.
FAQs
Simple RFC 6838 media type parser and formatter
The npm package media-typer receives a total of 47,728,258 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
New research reveals that LLMs often fake understanding, passing benchmarks but failing to apply concepts or stay internally consistent.
Security News
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.