fast-content-type-parse
Version:
Parse HTTP Content-Type header according to RFC 7231
83 lines (55 loc) • 2.9 kB
Markdown
"center">
[](https://www.npmjs.com/package/fast-content-type-parse)
[](https://www.npmjs.com/package/fast-content-type-parse)
[](https://github.com/fastify/fast-content-type-parse/actions/workflows/ci.yml)
[](https://github.com/neostandard/neostandard)
[](https://github.com/fastify/.github/blob/main/SECURITY.md)
</div>
Parse HTTP Content-Type header according to RFC 7231.
```sh
$ npm install fast-content-type-parse
```
```js
const fastContentTypeParse = require('fast-content-type-parse')
```
```js
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'}`
```js
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`.
```sh
node benchmarks/index.js
util
fast-content-type-parse
fast-content-type-parse
content-type
busboy
Fastest is fast-content-type-parse
```
Based on the npm package `content-type`.
Licensed under [MIT](./LICENSE).
<div align=