{"id":16026,"library":"fast-content-type-parse","title":"Fast Content-Type Header Parser","description":"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.","status":"active","version":"3.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/fastify/fast-content-type-parse","tags":["javascript","content-type","rfc7231","typescript"],"install":[{"cmd":"npm install fast-content-type-parse","lang":"bash","label":"npm"},{"cmd":"yarn add fast-content-type-parse","lang":"bash","label":"yarn"},{"cmd":"pnpm add fast-content-type-parse","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const parse = require('fast-content-type-parse').parse;","symbol":"parse","correct":"import { parse } from 'fast-content-type-parse';"},{"note":"Named import for `safeParse` in ESM. This function will not throw on invalid input, unlike `parse`.","wrong":"const safeParse = require('fast-content-type-parse').safeParse;","symbol":"safeParse","correct":"import { safeParse } from 'fast-content-type-parse';"},{"note":"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.","wrong":"import fastContentTypeParse from 'fast-content-type-parse';","symbol":"FastContentTypeParse (CommonJS)","correct":"const { parse, safeParse } = require('fast-content-type-parse');"}],"quickstart":{"code":"import { parse, safeParse } from 'fast-content-type-parse';\n\n// Example 1: Valid Content-Type header\ntry {\n  const contentType = parse('application/json; charset=utf-8');\n  console.log('Parsed (strict):', contentType); // { type: 'application/json', parameters: { charset: 'utf-8' } }\n} catch (error) {\n  console.error('Error parsing (strict):', error.message);\n}\n\n// Example 2: Invalid Content-Type header with strict parse (will throw)\ntry {\n  const invalidContentType = parse('invalid-type;');\n  console.log('Parsed (strict):', invalidContentType);\n} catch (error) {\n  console.error('Error parsing (strict):', error.message); // TypeError: Invalid Content-Type header string\n}\n\n// Example 3: Invalid Content-Type header with safeParse (will not throw)\nconst safeInvalidContentType = safeParse('invalid-type;');\nconsole.log('Parsed (safe):', safeInvalidContentType); // { type: '', parameters: {} }\n\n// Example 4: Another valid header with safeParse\nconst safeContentType = safeParse('text/html; boundary=\"foo\"');\nconsole.log('Parsed (safe):', safeContentType); // { type: 'text/html', parameters: { boundary: 'foo' } }","lang":"typescript","description":"Demonstrates both `parse` (strict, throws on error) and `safeParse` (returns empty object on error) methods for HTTP Content-Type headers."},"warnings":[{"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`.","message":"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.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Choose `parse` if you require strict validation and explicit error handling, or `safeParse` for robustness against malformed input without needing `try/catch` blocks.","message":"The `parse` method throws a `TypeError` for invalid Content-Type header strings, while `safeParse` returns an object with `type: ''` and `parameters: {}` without throwing.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"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.","cause":"The input string provided to the `parse` method does not conform to RFC 7231 specifications for a Content-Type header.","error":"TypeError: Invalid Content-Type header string"},{"fix":"In ESM contexts, use `import { parse, safeParse } from 'fast-content-type-parse';`. The package provides dual CommonJS/ESM entry points.","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.","error":"ReferenceError: require is not defined"}],"ecosystem":"npm"}