fast-content-type-parse

Parse HTTP Content-Type header according to RFC 7231

MIT 6 个版本
安装
npm install fast-content-type-parse
yarn add fast-content-type-parse
pnpm add fast-content-type-parse
bun add fast-content-type-parse
README

fast-content-type-parse

NPM version NPM downloads CI neostandard javascript style Security Responsible Disclosure

Parse HTTP Content-Type header according to RFC 7231.

Installation

$ npm install fast-content-type-parse

Usage

const fastContentTypeParse = require('fast-content-type-parse')

fastContentTypeParse.parse(string)

const contentType = fastContentTypeParse.parse('application/json; charset=utf-8')

Parse a Content-Type header. Throws a TypeError if the string is invalid.

It will return an object with the following properties (examples are shown for the string 'application/json; charset=utf-8'):

  • type: The media type (the type and subtype, always lowercase). Example: 'application/json'

  • parameters: An object of the parameters in the media type (name of parameter always lowercase). Example: {charset: 'utf-8'}

fastContentTypeParse.safeParse(string)

const contentType = fastContentTypeParse.safeParse('application/json; charset=utf-8')

Parse a Content-Type header. It will not throw an Error if the header is invalid.

This will return an object with the following properties (examples are shown for the string 'application/json; charset=utf-8'):

  • type: The media type (the type and subtype, always lowercase). Example: 'application/json'

  • parameters: An object of the parameters in the media type (name of parameter always lowercase). Example: {charset: 'utf-8'}

In case the header is invalid, it will return an object with an empty string '' as type and an empty Object for parameters.

Benchmarks

node benchmarks/index.js
util#MIMEType x 1,206,781 ops/sec ±0.22% (96 runs sampled)
fast-content-type-parse#parse x 3,752,236 ops/sec ±0.42% (96 runs sampled)
fast-content-type-parse#safeParse x 3,675,645 ops/sec ±1.09% (94 runs sampled)
content-type#parse x 1,452,582 ops/sec ±0.37% (95 runs sampled)
busboy#parseContentType x 924,306 ops/sec ±0.43% (94 runs sampled)
Fastest is fast-content-type-parse#parse

Credits

Based on the npm package content-type.

License

Licensed under MIT.

版本列表
3.0.0 2025-03-08
2.0.1 2025-01-03
2.0.0 2024-07-13
1.1.0 2023-09-21
1.0.0 2023-01-09
0.0.1 2023-01-06