diff options
Diffstat (limited to 'node_modules/mime/src')
| -rw-r--r-- | node_modules/mime/src/README_js.md | 179 | ||||
| -rwxr-xr-x | node_modules/mime/src/build.js | 71 | ||||
| -rw-r--r-- | node_modules/mime/src/test.js | 257 |
3 files changed, 507 insertions, 0 deletions
diff --git a/node_modules/mime/src/README_js.md b/node_modules/mime/src/README_js.md new file mode 100644 index 00000000..f38aca01 --- /dev/null +++ b/node_modules/mime/src/README_js.md @@ -0,0 +1,179 @@ +```javascript --hide +runmd.onRequire = path => path.replace(/^mime/, '..'); +``` +# Mime + +A comprehensive, compact MIME type module. + +## Version 2 Notes + +Version 2 is a breaking change from 1.x as the semver implies. Specifically: + +* `lookup()` renamed to `getType()` +* `extension()` renamed to `getExtension()` +* `charset()` and `load()` methods have been removed + +If you prefer the legacy version of this module please `npm install mime@^1`. Version 1 docs may be found [here](https://github.com/broofa/node-mime/tree/v1.4.0). + +## Install + +### NPM +``` +npm install mime +``` + +### Browser + +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 wzrd.in. +E.g. For the full version: + + <script src="https://wzrd.in/standalone/mime@latest"></script> + <script> + mime.getType(...); // etc. + <script> + +Or, for the `mime/lite` version: + + <script src="https://wzrd.in/standalone/mime%2flite@latest"></script> + <script> + mimelite.getType(...); // (Note `mimelite` here) + <script> + +## Quick Start + +For the full version (800+ MIME types, 1,000+ extensions): + +```javascript --run default +const mime = require('mime'); + +mime.getType('txt'); // RESULT +mime.getExtension('text/plain'); // RESULT +``` + +See [Mime API](#mime-api) below for API details. + +## Lite Version + +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 .vs. mime-types .vs. mime-db modules + +For those of you wondering about the difference between these [popular] NPM modules, +here's a brief rundown ... + +[`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: + +* 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. + +### 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 --run default +// 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'); // RESULT +myMime.getExtension('text/def'); // RESULT +``` + +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 --run default +mime.getType('js'); // RESULT +mime.getType('json'); // RESULT + +mime.getType('txt'); // RESULT +mime.getType('dir/text.txt'); // RESULT +mime.getType('dir\\text.txt'); // RESULT +mime.getType('.text.txt'); // RESULT +mime.getType('.txt'); // RESULT +``` + +`null` is returned in cases where an extension is not detected or recognized + +```javascript --run default +mime.getType('foo/txt'); // RESULT +mime.getType('bogus_type'); // RESULT +``` + +### mime.getExtension(type) +Get extension for the given mime type. Charset options (often included in +Content-Type headers) are ignored. + +```javascript --run default +mime.getExtension('text/plain'); // RESULT +mime.getExtension('application/json'); // RESULT +mime.getExtension('text/html; charset=utf8'); // RESULT +``` + +### mime.define(typeMap[, force = false]) + +Define [more] type mappings. + +`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 --run default +mime.define({'text/x-abc': ['abc', 'abcd']}); + +mime.getType('abcd'); // RESULT +mime.getExtension('text/x-abc') // RESULT +``` + +## Command Line + + mime [path_or_extension] + +E.g. + + > mime scripts/jquery.js + application/javascript diff --git a/node_modules/mime/src/build.js b/node_modules/mime/src/build.js new file mode 100755 index 00000000..c70bc658 --- /dev/null +++ b/node_modules/mime/src/build.js @@ -0,0 +1,71 @@ +#!/usr/bin/env node + +'use strict'; + +var fs = require('fs'); +var path = require('path'); +var mimeScore = require('mime-score'); + +var db = require('mime-db'); +var chalk = require('chalk'); + +var STANDARD_FACET_SCORE = 900; + +var byExtension = {}; + +// Clear out any conflict extensions in mime-db +for (var type in db) { + var entry = db[type]; + entry.type = type; + if (!entry.extensions) continue; + + entry.extensions.forEach(function(ext) { + var drop; + var keep = entry; + if (ext in byExtension) { + var e0 = entry; + var e1 = byExtension[ext]; + + e0.pri = mimeScore(e0.type, e0.source); + e1.pri = mimeScore(e1.type, e1.source); + + drop = e0.pri < e1.pri ? e0 : e1; + keep = e0.pri >= e1.pri ? e0 : e1; + + // Prefix lower-priority extensions with '*' + drop.extensions = drop.extensions.map(function(e) {return e == ext ? '*' + e : e}); + + console.log( + ext + ': Preferring ' + chalk.green(keep.type) + ' (' + keep.pri + + ') over ' + chalk.red(drop.type) + ' (' + drop.pri + ')' + ' for ' + ext + ); + } + + // Cache the hightest ranking type for this extension + if (keep == entry) byExtension[ext] = entry; + }); +} + +function writeTypesFile(types, path) { + fs.writeFileSync(path, JSON.stringify(types)); +} + +// Segregate into standard and non-standard types based on facet per +// https://tools.ietf.org/html/rfc6838#section-3.1 +var standard = {}; +var other = {}; + +Object.keys(db).sort().forEach(function(k) { + var entry = db[k]; + + if (entry.extensions) { + if (mimeScore(entry.type, entry.source) >= STANDARD_FACET_SCORE) { + standard[entry.type] = entry.extensions; + } else { + other[entry.type] = entry.extensions; + } + } +}); + +writeTypesFile(standard, path.join(__dirname, '../types', 'standard.json')); +writeTypesFile(other, path.join(__dirname, '../types', 'other.json')); diff --git a/node_modules/mime/src/test.js b/node_modules/mime/src/test.js new file mode 100644 index 00000000..338e3da0 --- /dev/null +++ b/node_modules/mime/src/test.js @@ -0,0 +1,257 @@ +'use strict'; + +var mime = require('..'); +var mimeTypes = require('../node_modules/mime-types'); +var assert = require('assert'); +var chalk = require('chalk'); + +describe('class Mime', function() { + it('mime and mime/lite coexist', function() { + assert.doesNotThrow(function() { + require('../lite'); + }); + }); + + it('new constructor()', function() { + var Mime = require('../Mime'); + + var mime = new Mime( + {'text/a': ['a', 'a1']}, + {'text/b': ['b', 'b1']} + ); + + assert.deepEqual(mime._types, { + a: 'text/a', + a1: 'text/a', + b: 'text/b', + b1: 'text/b', + }); + + assert.deepEqual(mime._extensions, { + 'text/a': 'a', + 'text/b': 'b', + }); + }); + + it('define()', function() { + var Mime = require('../Mime'); + + var mime = new Mime({'text/a': ['a']}, {'text/b': ['b']}); + + assert.throws(function() { + mime.define({'text/c': ['b']}); + }); + + assert.doesNotThrow(function() { + mime.define({'text/c': ['b']}, true); + }); + + assert.deepEqual(mime._types, { + a: 'text/a', + b: 'text/c', + }); + + assert.deepEqual(mime._extensions, { + 'text/a': 'a', + 'text/b': 'b', + 'text/c': 'b', + }); + }); + + it('define() *\'ed types', function() { + var Mime = require('../Mime'); + + var mime = new Mime( + {'text/a': ['*b']}, + {'text/b': ['b']} + ); + + assert.deepEqual(mime._types, { + b: 'text/b', + }); + + assert.deepEqual(mime._extensions, { + 'text/a': 'b', + 'text/b': 'b', + }); + }); + + it('getType()', function() { + // Upper/lower case + assert.equal(mime.getType('text.txt'), 'text/plain'); + assert.equal(mime.getType('TEXT.TXT'), 'text/plain'); + + // Bare extension + assert.equal(mime.getType('txt'), 'text/plain'); + assert.equal(mime.getType('.txt'), 'text/plain'); + assert.strictEqual(mime.getType('.bogus'), null); + assert.strictEqual(mime.getType('bogus'), null); + + // Non-sensical + assert.strictEqual(mime.getType(null), null); + assert.strictEqual(mime.getType(undefined), null); + assert.strictEqual(mime.getType(42), null); + assert.strictEqual(mime.getType({}), null); + + // File paths + assert.equal(mime.getType('dir/text.txt'), 'text/plain'); + assert.equal(mime.getType('dir\\text.txt'), 'text/plain'); + assert.equal(mime.getType('.text.txt'), 'text/plain'); + assert.equal(mime.getType('.txt'), 'text/plain'); + assert.equal(mime.getType('txt'), 'text/plain'); + assert.equal(mime.getType('/path/to/page.html'), 'text/html'); + assert.equal(mime.getType('c:\\path\\to\\page.html'), 'text/html'); + assert.equal(mime.getType('page.html'), 'text/html'); + assert.equal(mime.getType('path/to/page.html'), 'text/html'); + assert.equal(mime.getType('path\\to\\page.html'), 'text/html'); + assert.strictEqual(mime.getType('/txt'), null); + assert.strictEqual(mime.getType('\\txt'), null); + assert.strictEqual(mime.getType('text.nope'), null); + assert.strictEqual(mime.getType('/path/to/file.bogus'), null); + assert.strictEqual(mime.getType('/path/to/json'), null); + assert.strictEqual(mime.getType('/path/to/.json'), null); + assert.strictEqual(mime.getType('/path/to/.config.json'), 'application/json'); + assert.strictEqual(mime.getType('.config.json'), 'application/json'); + }); + + it('getExtension()', function() { + assert.equal(mime.getExtension('text/html'), 'html'); + assert.equal(mime.getExtension(' text/html'), 'html'); + assert.equal(mime.getExtension('text/html '), 'html'); + assert.strictEqual(mime.getExtension('application/x-bogus'), null); + assert.strictEqual(mime.getExtension('bogus'), null); + assert.strictEqual(mime.getExtension(null), null); + assert.strictEqual(mime.getExtension(undefined), null); + assert.strictEqual(mime.getExtension(42), null); + assert.strictEqual(mime.getExtension({}), null); + }); +}); + +describe('DB', function() { + var diffs = []; + + after(function() { + if (diffs.length) { + console.log('\n[INFO] The following inconsistencies with MDN (https://goo.gl/lHrFU6) and/or mime-types (https://github.com/jshttp/mime-types) are expected:'); + diffs.forEach(function(d) { + console.warn( + ' ' + d[0]+ '[' + chalk.blue(d[1]) + '] = ' + chalk.red(d[2]) + + ', mime[' + d[1] + '] = ' + chalk.green(d[3]) + ); + }); + } + }); + + it('Consistency', function() { + for (var ext in this.types) { + assert.equal(ext, this.extensions[this.types[ext]], '${ext} does not have consistent ext->type->ext mapping'); + } + }); + + it('MDN types', function() { + // MDN types listed at https://goo.gl/lHrFU6 + var MDN = { + 'aac': 'audio/aac', + 'abw': 'application/x-abiword', + 'arc': 'application/octet-stream', + 'avi': 'video/x-msvideo', + 'azw': 'application/vnd.amazon.ebook', + 'bin': 'application/octet-stream', + 'bz': 'application/x-bzip', + 'bz2': 'application/x-bzip2', + 'csh': 'application/x-csh', + 'css': 'text/css', + 'csv': 'text/csv', + 'doc': 'application/msword', + 'epub': 'application/epub+zip', + 'gif': 'image/gif', + 'html': 'text/html', + 'ico': 'image/x-icon', + 'ics': 'text/calendar', + 'jar': 'application/java-archive', + 'jpg': 'image/jpeg', + 'js': 'application/javascript', + 'json': 'application/json', + 'midi': 'audio/midi', + 'mpeg': 'video/mpeg', + 'mpkg': 'application/vnd.apple.installer+xml', + 'odp': 'application/vnd.oasis.opendocument.presentation', + 'ods': 'application/vnd.oasis.opendocument.spreadsheet', + 'odt': 'application/vnd.oasis.opendocument.text', + 'oga': 'audio/ogg', + 'ogv': 'video/ogg', + 'ogx': 'application/ogg', + 'png': 'image/png', + 'pdf': 'application/pdf', + 'ppt': 'application/vnd.ms-powerpoint', + 'rar': 'application/x-rar-compressed', + 'rtf': 'application/rtf', + 'sh': 'application/x-sh', + 'svg': 'image/svg+xml', + 'swf': 'application/x-shockwave-flash', + 'tar': 'application/x-tar', + 'tiff': 'image/tiff', + 'ttf': 'font/ttf', + 'vsd': 'application/vnd.visio', + 'wav': 'audio/x-wav', + 'weba': 'audio/webm', + 'webm': 'video/webm', + 'webp': 'image/webp', + 'woff': 'font/woff', + 'woff2': 'font/woff2', + 'xhtml': 'application/xhtml+xml', + 'xls': 'application/vnd.ms-excel', + 'xml': 'application/xml', + 'xul': 'application/vnd.mozilla.xul+xml', + 'zip': 'application/zip', + '3gp': 'video/3gpp', + '3g2': 'video/3gpp2', + '7z': 'application/x-7z-compressed', + }; + + for (var ext in MDN) { + var expected = MDN[ext]; + var actual = mime.getType(ext); + if (actual !== expected) diffs.push(['MDN', ext, expected, actual]); + } + + for (var ext in mimeTypes.types) { + var expected = mimeTypes.types[ext]; + var actual = mime.getType(ext); + if (actual !== expected) diffs.push(['mime-types', ext, expected, actual]); + } + }); + + it('Specific types', function() { + // Assortment of types we sanity check for good measure + assert.equal(mime.getType('html'), 'text/html'); + assert.equal(mime.getType('js'), 'application/javascript'); + assert.equal(mime.getType('json'), 'application/json'); + assert.equal(mime.getType('rtf'), 'application/rtf'); + assert.equal(mime.getType('txt'), 'text/plain'); + assert.equal(mime.getType('xml'), 'application/xml'); + + assert.equal(mime.getType('wasm'), 'application/wasm'); + }); + + it('Specific extensions', function() { + assert.equal(mime.getExtension('text/html;charset=UTF-8'), 'html'); + assert.equal(mime.getExtension('text/HTML; charset=UTF-8'), 'html'); + assert.equal(mime.getExtension('text/html; charset=UTF-8'), 'html'); + assert.equal(mime.getExtension('text/html; charset=UTF-8 '), 'html'); + assert.equal(mime.getExtension('text/html ; charset=UTF-8'), 'html'); + assert.equal(mime.getExtension(mime._types.text), 'txt'); + assert.equal(mime.getExtension(mime._types.htm), 'html'); + assert.equal(mime.getExtension('application/octet-stream'), 'bin'); + assert.equal(mime.getExtension('application/octet-stream '), 'bin'); + assert.equal(mime.getExtension(' text/html; charset=UTF-8'), 'html'); + assert.equal(mime.getExtension('text/html; charset=UTF-8 '), 'html'); + assert.equal(mime.getExtension('text/html; charset=UTF-8'), 'html'); + assert.equal(mime.getExtension('text/html ; charset=UTF-8'), 'html'); + assert.equal(mime.getExtension('text/html;charset=UTF-8'), 'html'); + assert.equal(mime.getExtension('text/Html;charset=UTF-8'), 'html'); + assert.equal(mime.getExtension('unrecognized'), null); + + assert.equal(mime.getExtension('text/xml'), 'xml'); // See #180 + }); +}); |
