Registry / http-networking / fast-content-type-parse

fast-content-type-parse

JSON →
library3.0.0jsnpmunverified

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.

npm install fast-content-type-parse
INSTALL
IMPORT
SIG · FAST-CONTENT-TYPE-
F
fast-content-type-parse
http-networkingjavascriptv3.0.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.

parse
import { parse } from 'fast-content-type-parse';
const parse = require('fast-content-type-parse').parse;
For ESM projects, use named imports. Direct `require().parse` in ESM contexts can lead to `ReferenceError` unless transpiled. The library provides dual CommonJS/ESM entry points.
safeParse
import { safeParse } from 'fast-content-type-parse';
const safeParse = require('fast-content-type-parse').safeParse;
Named import for `safeParse` in ESM. This function will not throw on invalid input, unlike `parse`.
FastContentTypeParse (CommonJS)
const { parse, safeParse } = require('fast-content-type-parse');
import fastContentTypeParse from 'fast-content-type-parse';
While `fast-content-type-parse` offers dual ESM/CJS compatibility, the CommonJS export is an object containing `parse` and `safeParse`. A default import in ESM would be incorrect if the CJS bundle doesn't provide a default export, though `exports` maps can handle this.

Demonstrates both `parse` (strict, throws on error) and `safeParse` (returns empty object on error) methods for HTTP Content-Type headers.

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' } }
Debug
Known issues
breakingVersion 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.
fix
Upgrade your Node.js runtime to version 20 or newer, or pin `fast-content-type-parse` to a `^2.x` version in your `package.json`.
affects: >=3.0.0
gotchaThe `parse` method throws a `TypeError` for invalid Content-Type header strings, while `safeParse` returns an object with `type: ''` and `parameters: {}` without throwing.
fix
Choose `parse` if you require strict validation and explicit error handling, or `safeParse` for robustness against malformed input without needing `try/catch` blocks.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Invalid Content-Type header string
The input string provided to the `parse` method does not conform to RFC 7231 specifications for a Content-Type header.
fix
Ensure 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
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.
fix
In ESM contexts, use `import { parse, safeParse } from 'fast-content-type-parse';`. The package provides dual CommonJS/ESM entry points.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
fast-content-type-parse — npm install fast-content-type-parse · libregistry