[Pkg-javascript-commits] [node-mime-types] 01/02: Imported Upstream version 1.0.1+dfsg
Leo Iannacone
l3on-guest at moszumanska.debian.org
Fri Jul 4 08:01:45 UTC 2014
This is an automated email from the git hooks/post-receive script.
l3on-guest pushed a commit to branch master
in repository node-mime-types.
commit f6e6ec0eb3eebbe8eaa2783d879bd13d53c054d9
Author: Leo Iannacone <l3on at ubuntu.com>
Date: Thu Jul 3 12:48:48 2014 +0200
Imported Upstream version 1.0.1+dfsg
---
.npmignore | 14 ++++++++
.travis.yml | 12 +++++++
LICENSE | 22 ++++++++++++
Makefile | 9 +++++
README.md | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SOURCES.md | 17 ++++++++++
build.js | 57 ++++++++++++++++++++++++++++++++
component.json | 16 +++++++++
lib/custom.json | 27 +++++++++++++++
lib/index.js | 74 +++++++++++++++++++++++++++++++++++++++++
package.json | 34 +++++++++++++++++++
test/mime.js | 65 ++++++++++++++++++++++++++++++++++++
test/test.js | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
13 files changed, 548 insertions(+)
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..919d51b
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,14 @@
+test
+build.js
+
+# OS generated files #
+######################
+.DS_Store*
+# Icon?
+ehthumbs.db
+Thumbs.db
+
+# Node.js #
+###########
+node_modules
+npm-debug.log
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..73c85c6
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,12 @@
+language: node_js
+node_js:
+ - "0.8"
+ - "0.10"
+ - "0.11"
+matrix:
+ allow_failures:
+ - node_js: "0.11"
+ fast_finish: true
+before_install:
+ # remove build script deps before install
+ - node -pe 'f="./package.json";p=require(f);d=p.devDependencies;for(k in d){if("co"===k.substr(0,2))delete d[k]}require("fs").writeFileSync(f,JSON.stringify(p,null,2))'
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..a7ae8ee
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+
+The MIT License (MIT)
+
+Copyright (c) 2014 Jonathan Ong me at jongleberry.com
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..ceaf011
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,9 @@
+
+build:
+ node --harmony-generators build.js
+
+test:
+ node test/mime.js
+ mocha --require should --reporter spec test/test.js
+
+.PHONY: build test
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..8e21ee1
--- /dev/null
+++ b/README.md
@@ -0,0 +1,101 @@
+# mime-types
+[![NPM version](https://badge.fury.io/js/mime-types.svg)](https://badge.fury.io/js/mime-types) [![Build Status](https://travis-ci.org/expressjs/mime-types.svg?branch=master)](https://travis-ci.org/expressjs/mime-types)
+
+The ultimate javascript content-type utility.
+
+### Install
+
+```sh
+$ npm install mime-types
+```
+
+#### Similar to [node-mime](https://github.com/broofa/node-mime), except:
+
+- __No fallbacks.__ Instead of naively returning the first available type, `mime-types` simply returns `false`, so do `var type = mime.lookup('unrecognized') || 'application/octet-stream'`.
+- No `new Mime()` business, so you could do `var lookup = require('mime-types').lookup`.
+- Additional mime types are added such as jade and stylus. Feel free to add more!
+- Browser support via Browserify and Component by converting lists to JSON files.
+
+Otherwise, the API is compatible.
+
+### Adding Types
+
+If you'd like to add additional types,
+simply create a PR adding the type to `custom.json` and
+a reference link to the [sources](SOURCES.md).
+
+Do __NOT__ edit `mime.json` or `node.json`.
+Those are pulled using `build.js`.
+You should only touch `custom.json`.
+
+## API
+
+```js
+var mime = require('mime-types')
+```
+
+All functions return `false` if input is invalid or not found.
+
+### mime.lookup(path)
+
+Lookup the content-type associated with a file.
+
+```js
+mime.lookup('json') // 'application/json'
+mime.lookup('.md') // 'text/x-markdown'
+mime.lookup('file.html') // 'text/html'
+mime.lookup('folder/file.js') // 'application/javascript'
+
+mime.lookup('cats') // false
+```
+
+### mime.contentType(type)
+
+Create a full content-type header given a content-type or extension.
+
+```js
+mime.contentType('markdown') // 'text/x-markdown; charset=utf-8'
+mime.contentType('file.json') // 'application/json; charset=utf-8'
+```
+
+### mime.extension(type)
+
+Get the default extension for a content-type.
+
+```js
+mime.extension('application/octet-stream') // 'bin'
+```
+
+### mime.charset(type)
+
+Lookup the implied default charset of a content-type.
+
+```js
+mime.charset('text/x-markdown') // 'UTF-8'
+```
+
+### mime.types[extension] = type
+
+A map of content-types by extension.
+
+### mime.extensions[type] = [extensions]
+
+A map of extensions by content-type.
+
+### mime.define(types)
+
+Globally add definitions.
+`types` must be an object of the form:
+
+```js
+{
+ "<content-type>": [extensions...],
+ "<content-type>": [extensions...]
+}
+```
+
+See the `.json` files in `lib/` for examples.
+
+## License
+
+[MIT](LICENSE)
diff --git a/SOURCES.md b/SOURCES.md
new file mode 100644
index 0000000..1d65012
--- /dev/null
+++ b/SOURCES.md
@@ -0,0 +1,17 @@
+
+### Sources for custom types
+
+This is a list of sources for any custom mime types.
+When adding custom mime types, please link to where you found the mime type,
+even if it's from an unofficial source.
+
+- `text/coffeescript` - http://coffeescript.org/#scripts
+- `text/x-handlebars-template` - https://handlebarsjs.com/#getting-started
+- `text/x-sass` & `text/x-scss` - https://github.com/janlelis/rubybuntu-mime/blob/master/sass.xml
+- `text.jsx` - http://facebook.github.io/react/docs/getting-started.html [[2]](https://github.com/facebook/react/blob/f230e0a03154e6f8a616e0da1fb3d97ffa1a6472/vendor/browser-transforms.js#L210)
+
+[Sources for node.json types](https://github.com/broofa/node-mime/blob/master/types/node.types)
+
+### Notes on weird types
+
+- `font/opentype` - This type is technically invalid according to the spec. No valid types begin with `font/`. No-one uses the official type of `application/vnd.ms-opentype` as the community standardized `application/x-font-otf`. However, chrome logs nonsense warnings unless opentype fonts are served with `font/opentype`. [[1]](http://stackoverflow.com/questions/2871655/proper-mime-type-for-fonts)
diff --git a/build.js b/build.js
new file mode 100644
index 0000000..6ba0171
--- /dev/null
+++ b/build.js
@@ -0,0 +1,57 @@
+
+/**
+ * http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
+ * https://github.com/broofa/node-mime/blob/master/types/node.types
+ *
+ * Convert these text files to JSON for browser usage.
+ */
+
+var co = require('co')
+var fs = require('fs')
+var path = require('path')
+var cogent = require('cogent')
+
+function* get(url) {
+ var res = yield* cogent(url, {
+ string: true
+ })
+
+ if (res.statusCode !== 200)
+ throw new Error('got status code ' + res.statusCode + ' from ' + url)
+
+ var text = res.text
+ var json = {}
+ // http://en.wikipedia.org/wiki/Internet_media_type#Naming
+ /**
+ * Mime types and associated extensions are stored in the form:
+ *
+ * <type> <ext> <ext> <ext>
+ *
+ * And some are commented out with a leading `#` because they have no associated extensions.
+ * This regexp checks whether a single line matches this format, ignoring lines that are just comments.
+ * We could also just remove all lines that start with `#` if we want to make the JSON files smaller
+ * and ignore all mime types without associated extensions.
+ */
+ var re = /^(?:# )?([\w-]+\/[\w\+\.-]+)(?:\s+\w+)*$/
+ text = text.split('\n')
+ .filter(Boolean)
+ .forEach(function (line) {
+ line = line.trim()
+ if (!line) return
+ var match = re.exec(line)
+ if (!match) return
+ // remove the leading # and <type> and return all the <ext>s
+ json[match[1]] = line.replace(/^(?:# )?([\w-]+\/[\w\+\.-]+)/, '')
+ .split(/\s+/)
+ .filter(Boolean)
+ })
+ fs.writeFileSync('lib/' + path.basename(url).split('.')[0] + '.json',
+ JSON.stringify(json, null, 2) + '\n')
+}
+
+co(function* () {
+ yield [
+ get('http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'),
+ get('https://raw.githubusercontent.com/broofa/node-mime/master/types/node.types')
+ ]
+})()
diff --git a/component.json b/component.json
new file mode 100644
index 0000000..fa67a6d
--- /dev/null
+++ b/component.json
@@ -0,0 +1,16 @@
+{
+ "name": "mime-types",
+ "description": "The ultimate javascript content-type utility.",
+ "version": "0.1.0",
+ "author": {
+ "name": "Jonathan Ong",
+ "email": "me at jongleberry.com",
+ "url": "http://jongleberry.com",
+ "twitter": "https://twitter.com/jongleberry"
+ },
+ "repository": "expressjs/mime-types",
+ "license": "MIT",
+ "main": "lib/index.js",
+ "scripts": ["lib/index.js"],
+ "json": ["mime.json", "node.json", "custom.json"]
+}
diff --git a/lib/custom.json b/lib/custom.json
new file mode 100644
index 0000000..6137da3
--- /dev/null
+++ b/lib/custom.json
@@ -0,0 +1,27 @@
+{
+ "text/jade": [
+ "jade"
+ ],
+ "text/stylus": [
+ "stylus",
+ "styl"
+ ],
+ "text/less": [
+ "less"
+ ],
+ "text/x-sass": [
+ "sass"
+ ],
+ "text/x-scss": [
+ "scss"
+ ],
+ "text/coffeescript": [
+ "coffee"
+ ],
+ "text/x-handlebars-template": [
+ "hbs"
+ ],
+ "text/jsx": [
+ "jsx"
+ ]
+}
diff --git a/lib/index.js b/lib/index.js
new file mode 100644
index 0000000..27559ea
--- /dev/null
+++ b/lib/index.js
@@ -0,0 +1,74 @@
+
+// types[extension] = type
+exports.types = Object.create(null)
+// extensions[type] = [extensions]
+exports.extensions = Object.create(null)
+// define more mime types
+exports.define = define
+
+// store the json files
+exports.json = {
+ mime: require('./mime.json'),
+ node: require('./node.json'),
+ custom: require('./custom.json'),
+}
+
+exports.lookup = function (string) {
+ if (!string || typeof string !== "string") return false
+ string = string.replace(/.*[\.\/\\]/, '').toLowerCase()
+ if (!string) return false
+ return exports.types[string] || false
+}
+
+exports.extension = function (type) {
+ if (!type || typeof type !== "string") return false
+ type = type.match(/^\s*([^;\s]*)(?:;|\s|$)/)
+ if (!type) return false
+ var exts = exports.extensions[type[1].toLowerCase()]
+ if (!exts || !exts.length) return false
+ return exts[0]
+}
+
+// type has to be an exact mime type
+exports.charset = function (type) {
+ // special cases
+ switch (type) {
+ case 'application/json': return 'UTF-8'
+ }
+
+ // default text/* to utf-8
+ if (/^text\//.test(type)) return 'UTF-8'
+
+ return false
+}
+
+// backwards compatibility
+exports.charsets = {
+ lookup: exports.charset
+}
+
+exports.contentType = function (type) {
+ if (!type || typeof type !== "string") return false
+ if (!~type.indexOf('/')) type = exports.lookup(type)
+ if (!type) return false
+ if (!~type.indexOf('charset')) {
+ var charset = exports.charset(type)
+ if (charset) type += '; charset=' + charset.toLowerCase()
+ }
+ return type
+}
+
+define(exports.json.mime)
+define(exports.json.node)
+define(exports.json.custom)
+
+function define(json) {
+ Object.keys(json).forEach(function (type) {
+ var exts = json[type] || []
+ exports.extensions[type] = exports.extensions[type] || []
+ exts.forEach(function (ext) {
+ if (!~exports.extensions[type].indexOf(ext)) exports.extensions[type].push(ext)
+ exports.types[ext] = type
+ })
+ })
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..358a671
--- /dev/null
+++ b/package.json
@@ -0,0 +1,34 @@
+{
+ "name": "mime-types",
+ "description": "The ultimate javascript content-type utility.",
+ "version": "1.0.1",
+ "author": {
+ "name": "Jonathan Ong",
+ "email": "me at jongleberry.com",
+ "url": "http://jongleberry.com",
+ "twitter": "https://twitter.com/jongleberry"
+ },
+ "contributors": [
+ {
+ "name": "Jeremiah Senkpiel",
+ "email": "fishrock123 at rocketmail.com",
+ "url": "https://searchbeam.jit.su",
+ "twitter": "https://twitter.com/fishrock123"
+ }
+ ],
+ "repository": "expressjs/mime-types",
+ "license": "MIT",
+ "main": "lib",
+ "devDependencies": {
+ "co": "3",
+ "cogent": "0",
+ "mocha": "1",
+ "should": "3"
+ },
+ "engines": {
+ "node": ">= 0.8.0"
+ },
+ "scripts": {
+ "test": "make test"
+ }
+}
diff --git a/test/mime.js b/test/mime.js
new file mode 100644
index 0000000..3e6bd10
--- /dev/null
+++ b/test/mime.js
@@ -0,0 +1,65 @@
+/**
+ * Usage: node test.js
+ */
+
+var mime = require("..");
+var assert = require('assert');
+var path = require('path');
+
+function eq(a, b) {
+ console.log('Test: ' + a + ' === ' + b);
+ assert.strictEqual.apply(null, arguments);
+}
+
+console.log(Object.keys(mime.extensions).length + ' types');
+console.log(Object.keys(mime.types).length + ' extensions\n');
+
+//
+// Test mime lookups
+//
+
+eq('text/plain', mime.lookup('text.txt')); // normal file
+eq('text/plain', mime.lookup('TEXT.TXT')); // uppercase
+eq('text/plain', mime.lookup('dir/text.txt')); // dir + file
+eq('text/plain', mime.lookup('.text.txt')); // hidden file
+eq('text/plain', mime.lookup('.txt')); // nameless
+eq('text/plain', mime.lookup('txt')); // extension-only
+eq('text/plain', mime.lookup('/txt')); // extension-less ()
+eq('text/plain', mime.lookup('\\txt')); // Windows, extension-less
+// eq('application/octet-stream', mime.lookup('text.nope')); // unrecognized
+// eq('fallback', mime.lookup('text.fallback', 'fallback')); // alternate default
+
+//
+// Test extensions
+//
+
+eq('txt', mime.extension(mime.types.text));
+eq('html', mime.extension(mime.types.htm));
+eq('bin', mime.extension('application/octet-stream'));
+eq('bin', mime.extension('application/octet-stream '));
+eq('html', mime.extension(' text/html; charset=UTF-8'));
+eq('html', mime.extension('text/html; charset=UTF-8 '));
+eq('html', mime.extension('text/html; charset=UTF-8'));
+eq('html', mime.extension('text/html ; charset=UTF-8'));
+eq('html', mime.extension('text/html;charset=UTF-8'));
+eq('html', mime.extension('text/Html;charset=UTF-8'));
+eq(false, mime.extension('unrecognized'));
+
+//
+// Test node.types lookups
+//
+
+eq('application/font-woff', mime.lookup('file.woff'));
+eq('application/octet-stream', mime.lookup('file.buffer'));
+eq('audio/mp4', mime.lookup('file.m4a'));
+eq('font/opentype', mime.lookup('file.otf'));
+
+//
+// Test charsets
+//
+
+eq('UTF-8', mime.charset('text/plain'));
+eq(false, mime.charset(mime.types.js));
+eq('UTF-8', mime.charset('application/json'))
+eq('UTF-8', mime.charsets.lookup('text/something'));
+// eq('fallback', mime.charset('application/octet-stream', 'fallback'));
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..2ecee92
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,100 @@
+
+var assert = require('assert')
+
+var mime = require('..')
+
+var lookup = mime.lookup
+var extension = mime.extension
+var charset = mime.charset
+var contentType = mime.contentType
+
+describe('.lookup()', function () {
+
+ it('jade', function () {
+ assert.equal(lookup('jade'), 'text/jade')
+ assert.equal(lookup('.jade'), 'text/jade')
+ assert.equal(lookup('file.jade'), 'text/jade')
+ assert.equal(lookup('folder/file.jade'), 'text/jade')
+ })
+
+ it('should not error on non-string types', function () {
+ assert.doesNotThrow(function () {
+ lookup({ noteven: "once" })
+ lookup(null)
+ lookup(true)
+ lookup(Infinity)
+ })
+ })
+
+ it('should return false for unknown types', function () {
+ assert.equal(lookup('.jalksdjflakjsdjfasdf'), false)
+ })
+})
+
+describe('.extension()', function () {
+
+ it('should not error on non-string types', function () {
+ assert.doesNotThrow(function () {
+ extension({ noteven: "once" })
+ extension(null)
+ extension(true)
+ extension(Infinity)
+ })
+ })
+
+ it('should return false for unknown types', function () {
+ assert.equal(extension('.jalksdjflakjsdjfasdf'), false)
+ })
+})
+
+describe('.charset()', function () {
+
+ it('should not error on non-string types', function () {
+ assert.doesNotThrow(function () {
+ charset({ noteven: "once" })
+ charset(null)
+ charset(true)
+ charset(Infinity)
+ })
+ })
+
+ it('should return false for unknown types', function () {
+ assert.equal(charset('.jalksdjflakjsdjfasdf'), false)
+ })
+})
+
+describe('.contentType()', function () {
+
+ it('html', function () {
+ assert.equal(contentType('html'), 'text/html; charset=utf-8')
+ })
+
+ it('text/html; charset=ascii', function () {
+ assert.equal(contentType('text/html; charset=ascii'), 'text/html; charset=ascii')
+ })
+
+ it('json', function () {
+ assert.equal(contentType('json'), 'application/json; charset=utf-8')
+ })
+
+ it('application/json', function () {
+ assert.equal(contentType('application/json'), 'application/json; charset=utf-8')
+ })
+
+ it('jade', function () {
+ assert.equal(contentType('jade'), 'text/jade; charset=utf-8')
+ })
+
+ it('should not error on non-string types', function () {
+ assert.doesNotThrow(function () {
+ contentType({ noteven: "once" })
+ contentType(null)
+ contentType(true)
+ contentType(Infinity)
+ })
+ })
+
+ it('should return false for unknown types', function () {
+ assert.equal(contentType('.jalksdjflakjsdjfasdf'), false)
+ })
+})
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-mime-types.git
More information about the Pkg-javascript-commits
mailing list