4.0.0 • Published 6 months ago

whatwg-mimetype v4.0.0

Weekly downloads
10,930,955
License
MIT
Repository
github
Last release
6 months ago

Parse, serialize, and manipulate MIME types

This package will parse MIME types into a structured format, which can then be manipulated and serialized:

const MIMEType = require("whatwg-mimetype");

const mimeType = new MIMEType(`Text/HTML;Charset="utf-8"`);

console.assert(mimeType.toString() === "text/html;charset=utf-8");

console.assert(mimeType.type === "text");
console.assert(mimeType.subtype === "html");
console.assert(mimeType.essence === "text/html");
console.assert(mimeType.parameters.get("charset") === "utf-8");

mimeType.parameters.set("charset", "windows-1252");
console.assert(mimeType.parameters.get("charset") === "windows-1252");
console.assert(mimeType.toString() === "text/html;charset=windows-1252");

console.assert(mimeType.isHTML() === true);
console.assert(mimeType.isXML() === false);

Parsing is a fairly complex process; see the specification for details (and similarly for serialization).

This package's algorithms conform to those of the WHATWG MIME Sniffing Standard, and is aligned up to commit 8e9a7dd.

MIMEType API

This package's main module's default export is a class, MIMEType. Its constructor takes a string which it will attempt to parse into a MIME type; if parsing fails, an Error will be thrown.

The parse() static factory method

As an alternative to the constructor, you can use MIMEType.parse(string). The only difference is that parse() will return null on failed parsing, whereas the constructor will throw. It thus makes the most sense to use the constructor in cases where unparseable MIME types would be exceptional, and use parse() when dealing with input from some unconstrained source.

Properties

  • type: the MIME type's type, e.g. "text"
  • subtype: the MIME type's subtype, e.g. "html"
  • essence: the MIME type's essence, e.g. "text/html"
  • parameters: an instance of MIMETypeParameters, containing this MIME type's parameters

type and subtype can be changed. They will be validated to be non-empty and only contain HTTP token code points.

essence is only a getter, and cannot be changed.

parameters is also a getter, but the contents of the MIMETypeParameters object are mutable, as described below.

Methods

Note: the isHTML(), isXML(), and isJavaScript() methods are speculative, and may be removed or changed in future major versions. See whatwg/mimesniff#48 for brainstorming in this area. Currently we implement these mainly because they are useful in jsdom.

MIMETypeParameters API

The MIMETypeParameters class, instances of which are returned by mimeType.parameters, has equivalent surface API to a JavaScript Map.

However, MIMETypeParameters methods will always interpret their arguments as appropriate for MIME types, so e.g. parameter names will be lowercased, and attempting to set invalid characters will throw.

Some examples:

const mimeType = new MIMEType(`x/x;a=b;c=D;E="F"`);

// Logs:
// a b
// c D
// e F
for (const [name, value] of mimeType.parameters) {
  console.log(name, value);
}

console.assert(mimeType.parameters.has("a"));
console.assert(mimeType.parameters.has("A"));
console.assert(mimeType.parameters.get("A") === "b");

mimeType.parameters.set("Q", "X");
console.assert(mimeType.parameters.get("q") === "X");
console.assert(mimeType.toString() === "x/x;a=b;c=d;e=F;q=X");

// Throws:
mimeType.parameters.set("@", "x");

Raw parsing/serialization APIs

If you want primitives on which to build your own API, you can get direct access to the parsing and serialization algorithms as follows:

const parse = require("whatwg-mimetype/parser");
const serialize = require("whatwg-mimetype/serialize");

parse(string) returns an object containing the type and subtype strings, plus parameters, which is a Map. This is roughly our equivalent of the spec's MIME type record. If parsing fails, it instead returns null.

serialize(record) operates on the such an object, giving back a string according to the serialization algorithm.

jsdomdata-urlsapollo-server-corearchetype-libraryreact-native-bluetooth2killi8n-react-native-fast-imageticket-jsdomspecify-importsbabel-specify-imports@icanpm/api-masterjsdom-exreact-native-template-rfbaseairscanairscan-examplereact-native-esc-pos-sahaab@borisovart/atol-kkt-modulesmart-api-booking-servicedeneme323112@ntt_app/react-native-custom-notificationreact-native-covid-sdkgql_din_modbitgetjsdom-fork@olivervorasai/sliderreact-native-printer-brothers@oats-ts/http@oats-ts/openapi-http-clientwilscannerstretch-rollup@mink-opn/build-tokensreact-native-slider-kfsvelte-slime@infinitebrahmanuniverse/nolb-whahappy-dom-deno@pixiebrix/extensionplginexpand-react-bridgesklif-ui-kitsklif-api@everything-registry/sub-chunk-3143p149-table@pmadhur/jsdomsklif-ui@fun-stack/source-map-loader-no-warn@furgot100/dates-lib@furgot100/string-lib@gaearon/jsdomwebcheztiga@gaofq/utils@garonx/oracle-zkapp@geeky-apo/react-native-advanced-clipboardwac-ldpwarmaitech-gpt-3-encoderzachlankton-happy-domvue-axios-rest@heliosphere/opentelemetry-sdk@hemith/react-native-tnk@markuplint/types@maricode1111/date-lib@luciadias/storybook-notimation@massivepixel/use-query@simstudio/htmldiffzzzxxxyyy321123vz-parserwoven-challenge-deploy@juanaraneta/dept-central-lib-client@knooks/use-title@ko-developerhong/react-native-multiple-image-pickerex-ikon-components-library@kalkanisys/vue-select@lanz1/v-money3@kshitij-sharma/oas-tools-core@leiyulf/gantt-task-react@livecycle.io/rrweb-snapshotdrift-npmdskcorenew@houzy/soap@hproinformatica/functions@humanity.cash/typesgogency-test-2gogencygrids-over-polygongoogle-remake@oats-ts/openapi-express-server-adapter@oats-ts/openapi-fetch-client-adapter@nyanator/chrome-ext-utils@oas-tools/coregot-iconv@object-studio/react-native-web@nosnibor89/cdk-static-website-construct@openpolitica/matomo-next@mazito/zkapp-contracts-add@meklitc7/date-library@meklitc7/string-library@mayuriachewad/pickupbiz-npm-package@maybejaybe/date-libfrdrk-js-semaphore@meysam213/react-leafletfront-and-back
4.0.0

6 months ago

3.0.0

3 years ago

2.3.0

5 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago