Comparing version 1.4.0 to 2.0.0
@@ -5,5 +5,5 @@ #!/usr/bin/env node | ||
var file = process.argv[2]; | ||
var type = mime.lookup(file); | ||
var type = mime.getType(file); | ||
process.stdout.write(type + '\n'); | ||
@@ -21,7 +21,13 @@ { | ||
"devDependencies": { | ||
"mime-db": "1.30.0" | ||
"chalk": "1.1.3", | ||
"github-release-notes": "0.9.0", | ||
"mime-db": "1.30.0", | ||
"mime-types": "2.1.15", | ||
"mocha": "3.5.3", | ||
"runmd": "0.1.8" | ||
}, | ||
"scripts": { | ||
"prepublish": "node build/build.js > types.json", | ||
"test": "node build/test.js" | ||
"prepare": "node src/build.js && gren --action=changelog && runmd --output README.md src/README_js.md", | ||
"md": "runmd --watch --output README.md src/README_js.md", | ||
"test": "mocha src/test.js" | ||
}, | ||
@@ -32,3 +38,2 @@ "keywords": [ | ||
], | ||
"main": "mime.js", | ||
"name": "mime", | ||
@@ -39,3 +44,3 @@ "repository": { | ||
}, | ||
"version": "1.4.0" | ||
"version": "2.0.0" | ||
} |
201
README.md
@@ -1,90 +0,187 @@ | ||
# mime | ||
# Mime | ||
Comprehensive MIME type mapping API based on mime-db module. | ||
A comprehensive, compact MIME type module. | ||
## Install | ||
## Version 2 Notes | ||
Install with [npm](http://github.com/isaacs/npm): | ||
Version 2 is a breaking change from 1.x, as the semver implies. Specifically: | ||
npm install mime | ||
* ES6 support required (requires Node 6+) | ||
* `lookup()` renamed to `getType()` | ||
* `extension()` renamed to `getExtension()` | ||
* `charset()` and `load()` methods have been removed | ||
## Contributing / Testing | ||
If you need the legacy version of this module for any reason, please `npm install mime@^1`. Version 1 docs may be found [here](https://github.com/broofa/node-mime/tree/v1.4.0). | ||
npm run test | ||
## Install - NPM | ||
``` | ||
npm install mime | ||
``` | ||
## Command Line | ||
## Quick Start | ||
mime [path_string] | ||
For the full version (800+ MIME types, 1,000+ extensions): | ||
E.g. | ||
```javascript | ||
const mime = require('mime'); | ||
> mime scripts/jquery.js | ||
application/javascript | ||
mime.getType('txt'); // ⇨ 'text/plain' | ||
mime.getExtension('text/plain'); // ⇨ 'txt' | ||
## API - Queries | ||
``` | ||
### mime.lookup(path) | ||
Get the mime type associated with a file, if no mime type is found `application/octet-stream` is returned. Performs a case-insensitive lookup using the extension in `path` (the substring after the last '/' or '.'). E.g. | ||
See [Mime API](#mime-api) below for API details. | ||
```js | ||
var mime = require('mime'); | ||
## Lite Version | ||
mime.lookup('/path/to/file.txt'); // => 'text/plain' | ||
mime.lookup('file.txt'); // => 'text/plain' | ||
mime.lookup('.TXT'); // => 'text/plain' | ||
mime.lookup('htm'); // => 'text/html' | ||
There is also a "lite" version of this module that 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'); | ||
``` | ||
### mime.default_type | ||
Sets the mime type returned when `mime.lookup` fails to find the extension searched for. (Default is `application/octet-stream`.) | ||
## Browser-ready Versions | ||
### mime.extension(type) | ||
Get the default extension for `type` | ||
To use this module in the browser, you would typlically use | ||
[webpack](https://webpack.github.io/) or [browserify](http://browserify.org/) to | ||
package your code. However, browser-ready versions are available via wzrd.in. | ||
E.g. For the full version: | ||
```js | ||
mime.extension('text/html'); // => 'html' | ||
mime.extension('application/octet-stream'); // => 'bin' | ||
<script src="http://wzrd.in/standalone/mime@latest"></script> | ||
Or, for the "lite" version: | ||
<script src="http://wzrd.in/standalone/mime%2flite@latest"></script> | ||
Then: | ||
```html | ||
<script> | ||
mime.getType(...); // etc. | ||
</script> | ||
``` | ||
### mime.charsets.lookup() | ||
## Mime .vs. mime-types .vs. mime-db modules | ||
Map mime-type to charset | ||
For those of you wondering about the difference between these [popular] NPM modules, | ||
here's a brief rundown ... | ||
```js | ||
mime.charsets.lookup('text/plain'); // => 'UTF-8' | ||
[`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. | ||
[`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: | ||
* Internally consistent type ⇔ extension mapping. I.e. | ||
`mime.getType(mime.getExtension(type)) == type` will always be true | ||
* 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. | ||
### 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' | ||
``` | ||
(The logic for charset lookups is pretty rudimentary. Feel free to suggest improvements.) | ||
If more than one map argument is provided, each map is `define()`ed (see below), in order. | ||
## API - Defining Custom Types | ||
### mime.getType(pathOrExtension) | ||
Custom type mappings can be added on a per-project basis via the following APIs. | ||
Get mime type for the given path or extension. E.g. | ||
### mime.define() | ||
```javascript | ||
mime.getType('js'); // ⇨ 'application/javascript' | ||
mime.getType('json'); // ⇨ 'application/json' | ||
Add custom mime/extension mappings | ||
mime.getType('txt'); // ⇨ 'text/plain' | ||
mime.getType('dir/text.txt'); // ⇨ 'text/plain' | ||
mime.getType('dir\\text.txt'); // ⇨ 'text/plain' | ||
mime.getType('.text.txt'); // ⇨ 'text/plain' | ||
mime.getType('.txt'); // ⇨ 'text/plain' | ||
```js | ||
mime.define({ | ||
'text/x-some-format': ['x-sf', 'x-sft', 'x-sfml'], | ||
'application/x-my-type': ['x-mt', 'x-mtt'], | ||
// etc ... | ||
}); | ||
``` | ||
mime.lookup('x-sft'); // => 'text/x-some-format' | ||
`null` is returned in cases where an extension is not detected or recognized | ||
```javascript | ||
mime.getType('foo/txt'); // ⇨ null | ||
mime.getType('bogus_type'); // ⇨ null | ||
``` | ||
The first entry in the extensions array is returned by `mime.extension()`. E.g. | ||
### mime.getExtension(type) | ||
Get extension for the given mime type. Charset options (often included in | ||
Content-Type headers) are ignored. | ||
```js | ||
mime.extension('text/x-some-format'); // => 'x-sf' | ||
```javascript | ||
mime.getExtension('text/plain'); // ⇨ 'txt' | ||
mime.getExtension('application/json'); // ⇨ 'json' | ||
mime.getExtension('text/html; charset=utf8'); // ⇨ 'html' | ||
``` | ||
### mime.load(filepath) | ||
### mime.define(typeMap[, force = false]) | ||
Load mappings from an Apache ".types" format file | ||
Define [more] type mappings. | ||
```js | ||
mime.load('./my_project.types'); | ||
`typeMap` is a map of type -> extensions, as documented in `new Mime`, above. | ||
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 | ||
mime.define({'text/x-abc': ['abc', 'abcd']}); | ||
mime.getType('abcd'); // ⇨ 'text/x-abc' | ||
mime.getExtension('text/x-abc') // ⇨ 'abc' | ||
``` | ||
The .types file format is simple - See the `types` dir for examples. | ||
## Command Line | ||
mime [path_or_extension] | ||
E.g. | ||
> mime scripts/jquery.js | ||
application/javascript | ||
---- | ||
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) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
99989
18
727
187
1
6
1