Comparing version 3.0.0 to 4.0.1
@@ -7,7 +7,21 @@ { | ||
}, | ||
"type": "module", | ||
"engines": { | ||
"node": ">=10.0.0" | ||
"node": ">=16" | ||
}, | ||
"main": "./dist/src/index.js", | ||
"exports": { | ||
".": "./dist/src/index.js", | ||
"./lite": "./dist/src/index_lite.js", | ||
"./types/standard.js": "./dist/types/standard.js", | ||
"./types/other.js": "./dist/types/other.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"files": [ | ||
"dist/src/*", | ||
"dist/types/*", | ||
"bin/*" | ||
], | ||
"bin": { | ||
"mime": "cli.js" | ||
"mime": "bin/cli.js" | ||
}, | ||
@@ -17,31 +31,35 @@ "contributors": [], | ||
"license": "MIT", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"benchmark": "*", | ||
"chalk": "4.1.2", | ||
"eslint": "8.1.0", | ||
"mime-db": "1.50.0", | ||
"mime-score": "1.2.0", | ||
"mime-types": "2.1.33", | ||
"mocha": "9.1.3", | ||
"runmd": "*", | ||
"standard-version": "9.3.2" | ||
"@types/mime-db": "1.*", | ||
"@types/mime-types": "2.1.1", | ||
"@types/node": "20.5.9", | ||
"@typescript-eslint/eslint-plugin": "6.6.0", | ||
"@typescript-eslint/parser": "6.6.0", | ||
"chalk": "5.3.0", | ||
"mime-score": "2.0.4", | ||
"mime-types": "2.1.35", | ||
"prettier": "3.0.3", | ||
"runmd": "1.3.9", | ||
"standard-version": "9.5.0", | ||
"typescript": "5.2.2" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"lite.js", | ||
"Mime.js", | ||
"cli.js", | ||
"/types" | ||
], | ||
"scripts": { | ||
"prepare": "node src/build.js && runmd --output README.md src/README_js.md", | ||
"release": "standard-version", | ||
"benchmark": "node src/benchmark.js", | ||
"md": "runmd --watch --output README.md src/README_js.md", | ||
"test": "mocha src/test.js" | ||
"build": "npm run build:clean && tsc", | ||
"build:clean": "rm -fr dist", | ||
"build:types": "node dist/scripts/build.js", | ||
"build:watch": "npm run build:clean && tsc --watch", | ||
"lint": "prettier -c .", | ||
"lint:fix": "prettier -w .", | ||
"prepublishOnly": "npm run build && npm run build:types && npm test", | ||
"release": "# `standard-version --dry-run --prerelease` is the command you're after", | ||
"test": "node --test && ./test/exports_test.sh", | ||
"test:watch": "clear && node --enable-source-maps --test --watch test" | ||
}, | ||
"keywords": [ | ||
"util", | ||
"mime" | ||
"extension", | ||
"file", | ||
"mime", | ||
"mime-db", | ||
"mimetypes", | ||
"util" | ||
], | ||
@@ -53,3 +71,6 @@ "name": "mime", | ||
}, | ||
"version": "3.0.0" | ||
"version": "4.0.1", | ||
"funding": [ | ||
"https://github.com/sponsors/broofa" | ||
] | ||
} |
177
README.md
@@ -6,32 +6,26 @@ <!-- | ||
A comprehensive, compact MIME type module. | ||
[![NPM downloads](https://img.shields.io/npm/dm/mime)](https://www.npmjs.com/package/mime) | ||
[![Mime CI](https://github.com/broofa/mime/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/broofa/mime/actions/workflows/ci.yml?query=branch%3Amain) | ||
[![Build Status](https://travis-ci.org/broofa/mime.svg?branch=master)](https://travis-ci.org/broofa/mime) | ||
An API for MIME type information. | ||
## Install | ||
- All `mime-db` types | ||
- Compact and dependency-free [![mime's badge](https://deno.bundlejs.com/?q=mime&badge)](https://bundlejs.com/?q=mime) | ||
- Full TS support | ||
### NPM | ||
``` | ||
npm install mime | ||
``` | ||
### Browser | ||
> [!Note] | ||
> `mime@4` is now `latest`. If you're upgrading from `mime@3`, note the following: | ||
> * `mime@4` is API-compatible with `mime@3`, with ~~one~~ two exceptions: | ||
> * Direct imports of `mime` properties [no longer supported](https://github.com/broofa/mime/issues/295) | ||
> * `mime.define()` cannot be called on the default `mime` object | ||
> * ESM module support is required. [ESM Module FAQ](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). | ||
> * Requires an [ES2020](https://caniuse.com/?search=es2020) or newer runtime | ||
> * Built-in Typescript types (`@types/mime` no longer needed) | ||
It is recommended that you use a bundler such as | ||
[webpack](https://webpack.github.io/) or [browserify](http://browserify.org/) to | ||
package your code. However, browser-ready versions are available via | ||
skypack.dev as follows: | ||
``` | ||
// Full version | ||
<script type="module"> | ||
import mime from "https://cdn.skypack.dev/mime"; | ||
</script> | ||
``` | ||
## Installation | ||
```bash | ||
npm install mime | ||
``` | ||
// "lite" version | ||
<script type="module"> | ||
import mime from "https://cdn.skypack.dev/mime/lite"; | ||
</script> | ||
``` | ||
@@ -43,3 +37,3 @@ ## Quick Start | ||
```javascript | ||
const mime = require('mime'); | ||
import mime from 'mime'; | ||
@@ -50,78 +44,17 @@ mime.getType('txt'); // ⇨ 'text/plain' | ||
See [Mime API](#mime-api) below for API details. | ||
### Lite Version [![mime/lite's badge](https://deno.bundlejs.com/?q=mime/lite&badge)](https://bundlejs.com/?q=mime/lite) | ||
## Lite Version | ||
`mime/lite` is a drop-in `mime` replacement, stripped of unofficial ("`prs.*`", "`x-*`", "`vnd.*`") types: | ||
The "lite" version of this module omits vendor-specific (`*/vnd.*`) and | ||
experimental (`*/x-*`) types. It weighs in at ~2.5KB, compared to 8KB for the | ||
full version. To load the lite version: | ||
```javascript | ||
const mime = require('mime/lite'); | ||
import mime from 'mime/lite'; | ||
``` | ||
## Mime .vs. mime-types .vs. mime-db modules | ||
## API | ||
For those of you wondering about the difference between these [popular] NPM modules, | ||
here's a brief rundown ... | ||
### `mime.getType(pathOrExtension)` | ||
[`mime-db`](https://github.com/jshttp/mime-db) is "the source of | ||
truth" for MIME type information. It is not an API. Rather, it is a canonical | ||
dataset of mime type definitions pulled from IANA, Apache, NGINX, and custom mappings | ||
submitted by the Node.js community. | ||
Get mime type for the given file path or extension. E.g. | ||
[`mime-types`](https://github.com/jshttp/mime-types) is a thin | ||
wrapper around mime-db that provides an API drop-in compatible(ish) with `mime @ < v1.3.6` API. | ||
`mime` is, as of v2, a self-contained module bundled with a pre-optimized version | ||
of the `mime-db` dataset. It provides a simplified API with the following characteristics: | ||
* Intelligently resolved type conflicts (See [mime-score](https://github.com/broofa/mime-score) for details) | ||
* Method naming consistent with industry best-practices | ||
* Compact footprint. E.g. The minified+compressed sizes of the various modules: | ||
Module | Size | ||
--- | --- | ||
`mime-db` | 18 KB | ||
`mime-types` | same as mime-db | ||
`mime` | 8 KB | ||
`mime/lite` | 2 KB | ||
## Mime API | ||
Both `require('mime')` and `require('mime/lite')` return instances of the MIME | ||
class, documented below. | ||
Note: Inputs to this API are case-insensitive. Outputs (returned values) will | ||
be lowercase. | ||
### new Mime(typeMap, ... more maps) | ||
Most users of this module will not need to create Mime instances directly. | ||
However if you would like to create custom mappings, you may do so as follows | ||
... | ||
```javascript | ||
// Require Mime class | ||
const Mime = require('mime/Mime'); | ||
// Define mime type -> extensions map | ||
const typeMap = { | ||
'text/abc': ['abc', 'alpha', 'bet'], | ||
'text/def': ['leppard'] | ||
}; | ||
// Create and use Mime instance | ||
const myMime = new Mime(typeMap); | ||
myMime.getType('abc'); // ⇨ 'text/abc' | ||
myMime.getExtension('text/def'); // ⇨ 'leppard' | ||
``` | ||
If more than one map argument is provided, each map is `define()`ed (see below), in order. | ||
### mime.getType(pathOrExtension) | ||
Get mime type for the given path or extension. E.g. | ||
```javascript | ||
mime.getType('js'); // ⇨ 'application/javascript' | ||
@@ -144,6 +77,6 @@ mime.getType('json'); // ⇨ 'application/json' | ||
### mime.getExtension(type) | ||
Get extension for the given mime type. Charset options (often included in | ||
Content-Type headers) are ignored. | ||
### `mime.getExtension(type)` | ||
Get file extension for the given mime type. Charset options (often included in Content-Type headers) are ignored. | ||
```javascript | ||
@@ -155,13 +88,41 @@ mime.getExtension('text/plain'); // ⇨ 'txt' | ||
### mime.define(typeMap[, force = false]) | ||
### `mime.getAllExtensions(type)` | ||
Define [more] type mappings. | ||
> [!Note] | ||
> New in `mime@4` | ||
`typeMap` is a map of type -> extensions, as documented in `new Mime`, above. | ||
Get all file extensions for the given mime type. | ||
By default this method will throw an error if you try to map a type to an | ||
extension that is already assigned to another type. Passing `true` for the | ||
`force` argument will suppress this behavior (overriding any previous mapping). | ||
```javascript --run default | ||
mime.getAllExtensions('image/jpeg'); // ⇨ Set(3) { 'jpeg', 'jpg', 'jpe' } | ||
``` | ||
## Custom `Mime` instances | ||
The default `mime` objects are immutable. Custom, mutable versions can be created as follows... | ||
### new Mime(type map [, type map, ...]) | ||
Create a new, custom mime instance. For example, to create a mutable version of the default `mime` instance: | ||
```javascript | ||
import { Mime } from 'mime/lite'; | ||
import standardTypes from 'mime/types/standard.js'; | ||
import otherTypes from 'mime/types/other.js'; | ||
const mime = new Mime(standardTypes, otherTypes); | ||
``` | ||
Each argument is passed to the `define()` method, below. For example `new Mime(standardTypes, otherTypes)` is synonomous with `new Mime().define(standardTypes).define(otherTypes)` | ||
### `mime.define(type map [, force = false])` | ||
> [!Note] | ||
> Only available on custom `Mime` instances | ||
Define MIME type -> extensions. | ||
Attempting to map a type to an already-defined extension will `throw` unless the `force` argument is set to `true`. | ||
```javascript | ||
mime.define({'text/x-abc': ['abc', 'abcd']}); | ||
@@ -175,10 +136,14 @@ | ||
mime [path_or_extension] | ||
### Extension -> type | ||
E.g. | ||
```bash | ||
$ mime scripts/jquery.js | ||
application/javascript | ||
``` | ||
> mime scripts/jquery.js | ||
application/javascript | ||
### Type -> extension | ||
---- | ||
Markdown generated from [src/README_js.md](src/README_js.md) by [![RunMD Logo](http://i.imgur.com/h0FVyzU.png)](https://github.com/broofa/runmd) | ||
```bash | ||
$ mime -r image/jpeg | ||
jpeg | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
93596
22
342
Yes
12
144