{"name":"whatwg-mimetype","description":"Parses, serializes, and manipulates MIME types, according to the WHATWG MIME Sniffing Standard","keywords":["content-type","mime type","mimesniff","http","whatwg"],"version":"5.0.0","author":{"name":"Domenic Denicola","email":"d@domenic.me","url":"https://domenic.me/"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/jsdom/whatwg-mimetype.git"},"main":"lib/index.js","scripts":{"test":"node --test","coverage":"c8 node --test --experimental-test-coverage","lint":"eslint","pretest":"node scripts/get-latest-platform-tests.mjs"},"devDependencies":{"@domenic/eslint-config":"^4.0.1","@exodus/bytes":"^1.9.0","c8":"^10.1.3","eslint":"^9.39.2","printable-string":"^0.3.0"},"engines":{"node":">=20"},"c8":{"reporter":["html"],"exclude":["scripts/","test/"]},"gitHead":"7614048e8bc775796bdaf623cdfe28c4b97fdab9","_id":"whatwg-mimetype@5.0.0","bugs":{"url":"https://github.com/jsdom/whatwg-mimetype/issues"},"homepage":"https://github.com/jsdom/whatwg-mimetype#readme","_nodeVersion":"24.12.0","_npmVersion":"11.6.2","dist":{"integrity":"sha512-sXcNcHOC51uPGF0P/D4NVtrkjSU2fNsm9iog4ZvZJsL3rjoDAzXZhkm2MWt1y+PUdggKAYVoMAIYcs78wJ51Cw==","shasum":"d8232895dbd527ceaee74efd4162008fb8a8cf48","tarball":"http://registry-npm.ywwl.com/whatwg-mimetype/download/whatwg-mimetype-5.0.0.tgz","fileCount":10,"unpackedSize":42618,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/whatwg-mimetype@5.0.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIAUGnDRNFO0Pqn5jNNphPi6+Z+GDnnIUfyuIno9tK7eaAiB1VdDnh4QlnCLW9DjSv1/r+MXAGAybmjB/jz9Ut14R6Q=="}],"size":10895,"noattachment":false,"key":"/whatwg-mimetype/-/whatwg-mimetype-5.0.0.tgz"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:99a8fc3d-333c-4030-98c9-309f1f03dc38"}},"directories":{},"maintainers":[{"name":"timothygu","email":"timothygu99@gmail.com"},{"name":"domenic","email":"d@domenic.me"},{"name":"sebmaster","email":"sebmaster16@gmail.com"},{"name":"zirro","email":"code@zirro.se"},{"name":"tmpvar","email":"tmpvar@gmail.com"},{"name":"joris-van-der-wel","email":"joris@jorisvanderwel.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/whatwg-mimetype_5.0.0_1768878277865_0.6519475557968633"},"_hasShrinkwrap":false,"readme":"# Parse, serialize, manipulate, and sniff MIME types\n\nThis package will parse [MIME types](https://mimesniff.spec.whatwg.org/#understanding-mime-types) into a structured format, which can then be manipulated and serialized:\n\n```js\nconst { MIMEType } = require(\"whatwg-mimetype\");\n\nconst mimeType = new MIMEType(`Text/HTML;Charset=\"utf-8\"`);\n\nconsole.assert(mimeType.toString() === \"text/html;charset=utf-8\");\n\nconsole.assert(mimeType.type === \"text\");\nconsole.assert(mimeType.subtype === \"html\");\nconsole.assert(mimeType.essence === \"text/html\");\nconsole.assert(mimeType.parameters.get(\"charset\") === \"utf-8\");\n\nmimeType.parameters.set(\"charset\", \"windows-1252\");\nconsole.assert(mimeType.parameters.get(\"charset\") === \"windows-1252\");\nconsole.assert(mimeType.toString() === \"text/html;charset=windows-1252\");\n\nconsole.assert(mimeType.isHTML() === true);\nconsole.assert(mimeType.isXML() === false);\n```\n\nIt can also [determine the MIME type of a resource](https://mimesniff.spec.whatwg.org/#determining-the-computed-mime-type-of-a-resource) by sniffing its contents:\n\n```js\nconst { computedMIMEType } = require(\"whatwg-mimetype\");\n\nconst pngBytes = new Uint8Array([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);\n\ncomputedMIMEType(pngBytes).essence;                                    // \"image/png\"\ncomputedMIMEType(pngBytes, { contentTypeHeader: \"image/gif\" }).essence // \"image/png\"\ncomputedMIMEType(pngBytes, { contentTypeHeader: \"text/html\" }).essence // \"text/html\"\n```\n\nParsing, serialization, and sniffing are all fairly complex processes; see the specification for more details:\n\n* [parsing](https://mimesniff.spec.whatwg.org/#parsing-a-mime-type)\n* [serialization](https://mimesniff.spec.whatwg.org/#serializing-a-mime-type)\n* [sniffing](https://mimesniff.spec.whatwg.org/#determining-the-computed-mime-type-of-a-resource)\n\nThis package's algorithms conform to those of the WHATWG [MIME Sniffing Standard](https://mimesniff.spec.whatwg.org/), as of [commit `e7594d5`](https://github.com/whatwg/mimesniff/commit/e7594d531819053508447f688a3bde465531aca5).\n\n## APIs\n\n### `MIMEType`\n\nThe `MIMEType` class's constructor takes a string which it will attempt to parse into a MIME type; if parsing fails, an `Error` will be thrown.\n\n#### The `parse()` static factory method\n\nAs 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.\n\n#### Properties\n\n* `type`: the MIME type's [type](https://mimesniff.spec.whatwg.org/#mime-type-type), e.g. `\"text\"`\n* `subtype`: the MIME type's [subtype](https://mimesniff.spec.whatwg.org/#mime-type-subtype), e.g. `\"html\"`\n* `essence`: the MIME type's [essence](https://mimesniff.spec.whatwg.org/#mime-type-essence), e.g. `\"text/html\"`\n* `parameters`: an instance of `MIMETypeParameters`, containing this MIME type's [parameters](https://mimesniff.spec.whatwg.org/#mime-type-parameters)\n\n`type` and `subtype` can be changed. They will be validated to be non-empty and only contain [HTTP token code points](https://mimesniff.spec.whatwg.org/#http-token-code-point).\n\n`essence` is only a getter, and cannot be changed.\n\n`parameters` is also a getter, but the contents of the `MIMETypeParameters` object are mutable, as described below.\n\n#### Methods\n\n* `toString()` serializes the MIME type to a string\n* `isHTML()`: returns true if this instance represents [a HTML MIME type](https://mimesniff.spec.whatwg.org/#html-mime-type)\n* `isXML()`: returns true if this instance represents [an XML MIME type](https://mimesniff.spec.whatwg.org/#xml-mime-type)\n* `isJavaScript({ prohibitParameters })`: returns true if this instance represents [a JavaScript MIME type](https://html.spec.whatwg.org/multipage/scripting.html#javascript-mime-type). `prohibitParameters` can be set to true to disallow any parameters, i.e. to test if the MIME type's serialization is a [JavaScript MIME type essence match](https://mimesniff.spec.whatwg.org/#javascript-mime-type-essence-match).\n\n_Note: the `isHTML()`, `isXML()`, and `isJavaScript()` methods are speculative, and may be removed or changed in future major versions. See [whatwg/mimesniff#48](https://github.com/whatwg/mimesniff/issues/48) for brainstorming in this area. Currently we implement these mainly because they are useful in jsdom._\n\n### `MIMETypeParameters`\n\nThe `MIMETypeParameters` class, instances of which are returned by `mimeType.parameters`, has equivalent surface API to a [JavaScript `Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map).\n\nHowever, `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.\n\nSome examples:\n\n```js\nconst { MIMEType } = require(\"whatwg-mimetype\");\n\nconst mimeType = new MIMEType(`x/x;a=b;c=D;E=\"F\"`);\n\n// Logs:\n// a b\n// c D\n// e F\nfor (const [name, value] of mimeType.parameters) {\n  console.log(name, value);\n}\n\nconsole.assert(mimeType.parameters.has(\"a\"));\nconsole.assert(mimeType.parameters.has(\"A\"));\nconsole.assert(mimeType.parameters.get(\"A\") === \"b\");\n\nmimeType.parameters.set(\"Q\", \"X\");\nconsole.assert(mimeType.parameters.get(\"q\") === \"X\");\nconsole.assert(mimeType.toString() === \"x/x;a=b;c=d;e=F;q=X\");\n\n// Throws:\nmimeType.parameters.set(\"@\", \"x\");\n```\n\n### `computedMIMEType()`\n\nThe `computedMIMEType(resource, options)` function determines a resource's MIME type using the full [MIME type sniffing algorithm](https://mimesniff.spec.whatwg.org/#determining-the-computed-mime-type-of-a-resource). This includes:\n\n* complexities around how the supplied `Content-Type` header or other external information interacts with (but usually does not override) the sniffing process;\n* the ability to set a \"no-sniff\" flag, like the one used when `X-Content-Type-Options: nosniff` is present, which mostly (but not entirely) prevents sniffing; and\n* the \"Apache bug\" check which occurs when the `Content-Type` header is one of several specific values.\n\nThat is, this doesn't just implement the [rules for identifying an unknown MIME type](https://mimesniff.spec.whatwg.org/#rules-for-identifying-an-unknown-mime-type). It gives you everything you need for a full browser-compatible MIME sniffing procedure.\n\n#### Arguments\n\n* **`resource`** (`Uint8Array`): The resource bytes.\n* **`options.contentTypeHeader`** (`string`): The Content-Type header value, for HTTP resources.\n* **`options.providedType`** (`string`): The MIME type from the filesystem or another protocol (like FTP), for non-HTTP resources.\n* **`options.noSniff`** (`boolean`, default `false`): Whether the `X-Content-Type-Options: nosniff` header was present.\n* **`options.isSupported`** (`function`, default `() => true`): A predicate called with a `MIMEType` instance to check if an image/audio/video MIME type is supported by the user agent. If it returns `false`, sniffing is skipped for that type.\n\nThe `contentTypeHeader` and `providedType` options will be stringified, so you could also supply a `MIMEType` (or a [Node.js `util` module `MIMEType`](https://nodejs.org/api/util.html#class-utilmimetype)).\n\n#### Return value\n\nA `MIMEType` instance representing the computed MIME type.\n","publish_time":1768878278058,"_cnpm_publish_time":1768878278058,"dist-tags":{"latest":"5.0.0"}}