Fast Content-Type Header Parser
fast-content-type-parse is a lightweight and performant utility designed to parse HTTP `Content-Type` headers in strict accordance with RFC 7231. Developed under the Fastify ecosystem, it aims for speed, outperforming Node.js's native `util#MIMEType` and other popular packages like `content-type`. The current stable version is `3.0.0`, with a release cadence that includes major updates for environmental compatibility and minor releases for maintenance. It provides both a strict parsing method that throws errors on invalid input and a 'safe' alternative that returns a default empty object for malformed headers. The package ships with TypeScript type definitions, ensuring robust development in TypeScript projects.
Common errors
-
TypeError: Invalid Content-Type header string
cause The input string provided to the `parse` method does not conform to RFC 7231 specifications for a Content-Type header.fixEnsure the input string is a valid HTTP Content-Type header, or use the `safeParse` method if you want to handle invalid input gracefully without throwing an error. -
ReferenceError: require is not defined
cause Attempting to use `require()` syntax in a pure ESM (ECMAScript Module) context, such as a file with `type: "module"` in `package.json` or an `.mjs` file, without proper transpilation or bundling.fixIn ESM contexts, use `import { parse, safeParse } from 'fast-content-type-parse';`. The package provides dual CommonJS/ESM entry points.
Warnings
- breaking Version 3.0.0 of `fast-content-type-parse` dropped support for Node.js 16 and Node.js 18. Projects running on these End-of-Life Node.js versions will need to update their Node.js runtime or remain on an older version of the package.
- gotcha The `parse` method throws a `TypeError` for invalid Content-Type header strings, while `safeParse` returns an object with `type: ''` and `parameters: {}` without throwing.
Install
-
npm install fast-content-type-parse -
yarn add fast-content-type-parse -
pnpm add fast-content-type-parse
Imports
- parse
const parse = require('fast-content-type-parse').parse;import { parse } from 'fast-content-type-parse'; - safeParse
const safeParse = require('fast-content-type-parse').safeParse;import { safeParse } from 'fast-content-type-parse'; - FastContentTypeParse (CommonJS)
import fastContentTypeParse from 'fast-content-type-parse';
const { parse, safeParse } = require('fast-content-type-parse');
Quickstart
import { parse, safeParse } from 'fast-content-type-parse';
// Example 1: Valid Content-Type header
try {
const contentType = parse('application/json; charset=utf-8');
console.log('Parsed (strict):', contentType); // { type: 'application/json', parameters: { charset: 'utf-8' } }
} catch (error) {
console.error('Error parsing (strict):', error.message);
}
// Example 2: Invalid Content-Type header with strict parse (will throw)
try {
const invalidContentType = parse('invalid-type;');
console.log('Parsed (strict):', invalidContentType);
} catch (error) {
console.error('Error parsing (strict):', error.message); // TypeError: Invalid Content-Type header string
}
// Example 3: Invalid Content-Type header with safeParse (will not throw)
const safeInvalidContentType = safeParse('invalid-type;');
console.log('Parsed (safe):', safeInvalidContentType); // { type: '', parameters: {} }
// Example 4: Another valid header with safeParse
const safeContentType = safeParse('text/html; boundary="foo"');
console.log('Parsed (safe):', safeContentType); // { type: 'text/html', parameters: { boundary: 'foo' } }