Registry / serialization / media-typer

media-typer

JSON →
library1.1.0jsnpmunverified

media-typer is a focused JavaScript utility designed for strictly parsing and formatting media types according to RFC 6838. It enables developers to deconstruct media type strings (e.g., "image/svg+xml") into their `type`, `subtype`, and optional `suffix` components, and conversely, to construct canonical media type strings from these parts. A key differentiator of this package is its explicit design choice to *not* handle media type parameters (such as `charset=utf-8`) or to parse full HTTP `Content-Type` headers, directing users to the `content-type` module for such broader parsing needs. The current stable version is 1.1.0. While releases are infrequent, this indicates a mature, stable, and narrowly scoped utility that receives updates primarily for maintenance or minor feature enhancements like the `test()` function.

npm install media-typer
INSTALL
IMPORT
SIG · MEDIA-TYPER
M
media-typer
serializationjavascriptv1.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

typer
const typer = require('media-typer')
import typer from 'media-typer'
This package is CommonJS-only. In an ESM context, `import * as typer from 'media-typer'` or `import typer from 'media-typer'` may work depending on your build system or Node.js version.
typer.parse
const typer = require('media-typer'); const parsed = typer.parse('image/jpeg');
import { parse } from 'media-typer';
The `parse` function is a property of the default export, not a named export. Direct destructuring from `import { parse } ...` will fail.
typer.format
const typer = require('media-typer'); const formatted = typer.format({ type: 'text', subtype: 'html' });
import { format } from 'media-typer';
Similar to `parse`, `format` is a property of the default export. Access it via the main module object.

This quickstart demonstrates how to parse, format, and validate media type strings, showcasing the core API and clarifying its scope regarding media type parameters.

const typer = require('media-typer'); // 1. Parsing a media type string into its components const parsedType = typer.parse('application/vnd.apple.mpegurl+json'); console.log('Parsed Type:', parsedType); // { type: 'application', subtype: 'vnd.apple.mpegurl', suffix: 'json' } // 2. Formatting an object of components back into a media type string const formattedType = typer.format({ type: 'text', subtype: 'html', suffix: null }); console.log('Formatted Type:', formattedType); // text/html // 3. Validating a media type string const isValid1 = typer.test('audio/ogg'); console.log('Is "audio/ogg" valid?', isValid1); // true const isValid2 = typer.test('invalid-type'); console.log('Is "invalid-type" valid?', isValid2); // false // 4. Demonstrating normalization (parse then format) const normalizedType = typer.format(typer.parse('IMAGE/JPEG')); console.log('Normalized "IMAGE/JPEG":', normalizedType); // image/jpeg // 5. Important: This module does NOT handle media type parameters (e.g., charset=utf-8) // Attempting to parse with parameters will throw an error. try { typer.parse('text/plain; charset=utf-8'); } catch (e) { console.warn('As expected, parsing with parameters throws:', e.message); }
Debug
Known issues
breakingVersion 1.0.0 introduced significant breaking changes. It removed support for Node.js versions below 0.8, dropped all parameter handling (e.g., `charset=utf-8`), and removed the `parse(req)` and `parse(res)` signatures, which were previously used to parse media types directly from HTTP request/response objects.
fix
Ensure your Node.js environment is `0.8` or newer. If you need to handle parameters or parse from HTTP request/response objects, use the `content-type` package instead. Refactor any direct `parse(req)` or `parse(res)` calls to first extract the Content-Type header string.
affects: >=1.0.0
gotchaThis package is specifically designed for RFC 6838 media types and *does not* parse or handle media type parameters (e.g., the `; charset=utf-8` portion of `text/html; charset=utf-8`). Attempting to pass a string with parameters to `typer.parse()` will result in a `TypeError`.
fix
If your use case requires parsing media types including their parameters, use the `content-type` package (e.g., `require('content-type').parse('text/html; charset=utf-8')`). `media-typer` is intended for the base media type string only.
affects: >=1.0.0
gotchaThe `media-typer` module is not intended for parsing HTTP `Content-Type` headers directly. While it can process the media type portion, it lacks the broader context and parameter handling often needed for HTTP header parsing.
fix
For parsing full `Content-Type` headers from HTTP requests/responses, including parameters and handling potential invalid formats robustly, use the dedicated `content-type` package. It provides methods like `contentType.parse(req)` or `contentType.parse(res)` for this purpose.
affects: *
Errors
Common errors & fixes
TypeError: Invalid media type
The string provided to `typer.parse()` or the object provided to `typer.format()` does not conform to the expected RFC 6838 format or structure.
fix
Ensure the input to `typer.parse()` is a valid 'type/subtype' or 'type/subtype+suffix' string without parameters. For `typer.format()`, ensure the object has `type` and `subtype` properties, and an optional `suffix` property, with valid string values.
TypeError: typer.parse is not a function
This typically occurs when attempting to destructure `parse` directly from an `import` statement (e.g., `import { parse } from 'media-typer'`) in an ESM context, while the package is a CommonJS module that exports an object with methods.
fix
For CommonJS, use `const typer = require('media-typer')` and then access `typer.parse`. In an ESM context, use `import * as typer from 'media-typer'` or `import typer from 'media-typer'` (depending on your build setup and Node.js version's CJS interop) and access as `typer.parse`.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
media-typer — npm install media-typer · libregistry