Registry /
http-networking / fast-content-type-parse
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
muslnode 18–226 runs
build_error
glibcnode 18–226 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' } }
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.
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
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. Audit
Dependencies
No dependency data recorded yet.