On this page

M

MIME Type Parsing

History
Source Code: lib/web/fetch/data-url.js
Stability: 2Stable

undici exposes helpers for parsing and serializing MIME types according to the WHATWG MIME Sniffing Standard. These are the same primitives undici uses internally to interpret Content-Type headers and data: URLs.

Import the functions from 'undici':

A parsed MIME type is represented as a plain object with the following shape:

Attributes
The lowercased type, for example  'text' .
subtype:<string>
The lowercased subtype, for example  'html' .
parameters:<Map>
< <string> , <string> > The parameters, keyed by lowercased parameter name.
essence:<string>
The concatenation of  type , '/' , and subtype , for example 'text/html' .
M

parseMIMEType

History
parseMIMEType(input): Object | string
Attributes
input:<string>
The MIME type string to parse.
Returns:<Object> | <string>
A parsed MIME type record, or the string literal  'failure' when input cannot be parsed.
The lowercased type.
subtype:<string>
The lowercased subtype.
parameters:<Map>
< <string> , <string> > The parsed parameters, keyed by lowercased parameter name.
essence:<string>
The MIME type essence,  `${type}/${subtype}` .

Implements the WHATWG parse a MIME type algorithm. The type and subtype are lowercased, and only the first occurrence of a duplicate parameter name is retained. When input is empty, lacks a /, or otherwise violates the grammar, the string literal 'failure' is returned instead of a record.

import { parseMIMEType } from 'undici'

parseMIMEType('text/html; charset=gbk')
// {
//   type: 'text',
//   subtype: 'html',
//   parameters: Map(1) { 'charset' => 'gbk' },
//   essence: 'text/html'
// }

parseMIMEType('not a mime type')
// 'failure'
M

serializeAMimeType

History
serializeAMimeType(mimeType): string
Attributes
mimeType:<Object>
A MIME type record, as returned by  parseMIMEType() .
The type.
subtype:<string>
The subtype.
parameters:<Map>
< <string> , <string> > The parameters to serialize, in insertion order.
essence:<string>
The MIME type essence,  `${type}/${subtype}` .
Returns:<string>
The serialized MIME type.

Implements the WHATWG serialize a MIME type algorithm. The essence is emitted followed by each parameter as ;name=value. A parameter value that contains characters outside the HTTP token set is quoted, and any " or \ characters within it are escaped.

import { serializeAMimeType } from 'undici'

serializeAMimeType({
  type: 'text',
  subtype: 'html',
  parameters: new Map([['charset', 'gbk']]),
  essence: 'text/html'
})
// 'text/html;charset=gbk'