pax_global_header00006660000000000000000000000064134602224730014515gustar00rootroot0000000000000052 comment=1332b73ed8584b7b25d556c55b6de9d64fa3ce2c media-typer-1.1.0/000077500000000000000000000000001346022247300137345ustar00rootroot00000000000000media-typer-1.1.0/.eslintignore000066400000000000000000000000421346022247300164330ustar00rootroot00000000000000.nyc_output coverage node_modules media-typer-1.1.0/.eslintrc.yml000066400000000000000000000000351346022247300163560ustar00rootroot00000000000000root: true extends: standard media-typer-1.1.0/.gitignore000066400000000000000000000000631346022247300157230ustar00rootroot00000000000000.nyc_output/ coverage/ node_modules/ npm-debug.log media-typer-1.1.0/.travis.yml000066400000000000000000000051411346022247300160460ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" - "0.12" - "1.8" - "2.5" - "3.3" - "4.9" - "5.12" - "6.17" - "7.10" - "8.16" - "9.11" - "10.15" - "11.14" - "12.0" sudo: false cache: directories: - node_modules before_install: - | # Setup utility functions function node_version_lt () { [[ "$(v "$TRAVIS_NODE_VERSION")" -lt "$(v "${1}")" ]] } function npm_module_installed () { npm -lsp ls | grep -Fq "$(pwd)/node_modules/${1}:${1}@" } function npm_remove_module_re () { node -e ' fs = require("fs"); p = JSON.parse(fs.readFileSync("package.json", "utf8")); r = RegExp(process.argv[1]); for (k in p.devDependencies) { if (r.test(k)) delete p.devDependencies[k]; } fs.writeFileSync("package.json", JSON.stringify(p, null, 2) + "\n"); ' "$@" } function npm_use_module () { node -e ' fs = require("fs"); p = JSON.parse(fs.readFileSync("package.json", "utf8")); p.devDependencies[process.argv[1]] = process.argv[2]; fs.writeFileSync("package.json", JSON.stringify(p, null, 2) + "\n"); ' "$@" } function v () { tr '.' '\n' <<< "${1}" \ | awk '{ printf "%03d", $0 }' \ | sed 's/^0*//' } # Configure npm - | # Skip updating shrinkwrap / lock npm config set shrinkwrap false # Setup Node.js version-specific dependencies - | # Configure eslint for linting if node_version_lt '6.0'; then npm_remove_module_re '^eslint(-|$)' fi - | # Configure mocha for testing if node_version_lt '0.10'; then npm_use_module 'mocha' '2.5.3' elif node_version_lt '4.0' ; then npm_use_module 'mocha' '3.5.3' elif node_version_lt '6.0' ; then npm_use_module 'mocha' '5.2.0' fi - | # Configure nyc for testing if node_version_lt '0.10'; then npm_remove_module_re '^nyc$' elif node_version_lt '4.0' ; then npm_use_module 'nyc' '10.3.2' elif node_version_lt '6.0' ; then npm_use_module 'nyc' '11.9.0' fi # Update Node.js modules - | # Prune & rebuild node_modules if [[ -d node_modules ]]; then npm prune npm rebuild fi before_scrpt: - | # Contents of node_modules npm -s ls ||: script: - | # Run test script if npm_module_installed 'nyc'; then npm run-script test-travis else npm test fi - | # Run linting, if eslint exists if npm_module_installed 'eslint'; then npm run-script lint fi after_script: - | # Upload coverage to coveralls npm install coveralls@2 nyc report --reporter=text-lcov | coveralls media-typer-1.1.0/HISTORY.md000066400000000000000000000022031346022247300154140ustar00rootroot000000000000001.1.0 / 2019-04-24 ================== * Add `test(string)` function 1.0.2 / 2019-04-19 ================== * Fix JSDoc comment for `parse` function 1.0.1 / 2018-10-20 ================== * Remove left over `parameters` property from class 1.0.0 / 2018-10-20 ================== This major release brings the module back to it's RFC 6838 roots. If you want a module to parse the `Content-Type` or similar HTTP headers, use the `content-type` module instead. * Drop support for Node.js below 0.8 * Remove parameter handling, which is outside RFC 6838 scope * Remove `parse(req)` and `parse(res)` signatures * perf: enable strict mode * perf: use a class for object creation 0.3.0 / 2014-09-07 ================== * Support Node.js 0.6 * Throw error when parameter format invalid on parse 0.2.0 / 2014-06-18 ================== * Add `typer.format()` to format media types 0.1.0 / 2014-06-17 ================== * Accept `req` as argument to `parse` * Accept `res` as argument to `parse` * Parse media type with extra LWS between type and first parameter 0.0.0 / 2014-06-13 ================== * Initial implementation media-typer-1.1.0/LICENSE000066400000000000000000000021061346022247300147400ustar00rootroot00000000000000(The MIT License) Copyright (c) 2014-2017 Douglas Christopher Wilson 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. media-typer-1.1.0/README.md000066400000000000000000000056601346022247300152220ustar00rootroot00000000000000# media-typer [![NPM Version][npm-version-image]][npm-url] [![NPM Downloads][npm-downloads-image]][npm-url] [![Node.js Version][node-version-image]][node-version-url] [![Build Status][travis-image]][travis-url] [![Test Coverage][coveralls-image]][coveralls-url] Simple RFC 6838 media type parser. This module will parse a given media type into it's component parts, like type, subtype, and suffix. A formatter is also provided to put them back together and the two can be combined to normalize media types into a canonical form. If you are looking to parse the string that represents a media type and it's parameters in HTTP (for example, the `Content-Type` header), use the [content-type module](https://www.npmjs.com/package/content-type). ## Installation This is a [Node.js](https://nodejs.org/en/) module available through the [npm registry](https://www.npmjs.com/). Installation is done using the [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): ```sh $ npm install media-typer ``` ## API ```js var typer = require('media-typer') ``` ### typer.parse(string) ```js var obj = typer.parse('image/svg+xml') ``` Parse a media type string. This will return an object with the following properties (examples are shown for the string `'image/svg+xml; charset=utf-8'`): - `type`: The type of the media type (always lower case). Example: `'image'` - `subtype`: The subtype of the media type (always lower case). Example: `'svg'` - `suffix`: The suffix of the media type (always lower case). Example: `'xml'` If the given type string is invalid, then a `TypeError` is thrown. ### typer.format(obj) ```js var obj = typer.format({ type: 'image', subtype: 'svg', suffix: 'xml' }) ``` Format an object into a media type string. This will return a string of the mime type for the given object. For the properties of the object, see the documentation for `typer.parse(string)`. If any of the given object values are invalid, then a `TypeError` is thrown. ### typer.test(string) ```js var valid = typer.test('image/svg+xml') ``` Validate a media type string. This will return `true` is the string is a well- formatted media type, or `false` otherwise. ## License [MIT](LICENSE) [coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/media-typer/master [coveralls-url]: https://coveralls.io/r/jshttp/media-typer?branch=master [node-version-image]: https://badgen.net/npm/node/media-typer [node-version-url]: https://nodejs.org/en/download [npm-downloads-image]: https://badgen.net/npm/dm/media-typer [npm-url]: https://npmjs.org/package/media-typer [npm-version-image]: https://badgen.net/npm/v/media-typer [travis-image]: https://badgen.net/travis/jshttp/media-typer/master [travis-url]: https://travis-ci.org/jshttp/media-typer media-typer-1.1.0/index.js000066400000000000000000000060221346022247300154010ustar00rootroot00000000000000/*! * media-typer * Copyright(c) 2014-2017 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * RegExp to match type in RFC 6838 * * type-name = restricted-name * subtype-name = restricted-name * restricted-name = restricted-name-first *126restricted-name-chars * restricted-name-first = ALPHA / DIGIT * restricted-name-chars = ALPHA / DIGIT / "!" / "#" / * "$" / "&" / "-" / "^" / "_" * restricted-name-chars =/ "." ; Characters before first dot always * ; specify a facet name * restricted-name-chars =/ "+" ; Characters after last plus always * ; specify a structured syntax suffix * ALPHA = %x41-5A / %x61-7A ; A-Z / a-z * DIGIT = %x30-39 ; 0-9 */ var SUBTYPE_NAME_REGEXP = /^[A-Za-z0-9][A-Za-z0-9!#$&^_.-]{0,126}$/ var TYPE_NAME_REGEXP = /^[A-Za-z0-9][A-Za-z0-9!#$&^_-]{0,126}$/ var TYPE_REGEXP = /^ *([A-Za-z0-9][A-Za-z0-9!#$&^_-]{0,126})\/([A-Za-z0-9][A-Za-z0-9!#$&^_.+-]{0,126}) *$/ /** * Module exports. */ exports.format = format exports.parse = parse exports.test = test /** * Format object to media type. * * @param {object} obj * @return {string} * @public */ function format (obj) { if (!obj || typeof obj !== 'object') { throw new TypeError('argument obj is required') } var subtype = obj.subtype var suffix = obj.suffix var type = obj.type if (!type || !TYPE_NAME_REGEXP.test(type)) { throw new TypeError('invalid type') } if (!subtype || !SUBTYPE_NAME_REGEXP.test(subtype)) { throw new TypeError('invalid subtype') } // format as type/subtype var string = type + '/' + subtype // append +suffix if (suffix) { if (!TYPE_NAME_REGEXP.test(suffix)) { throw new TypeError('invalid suffix') } string += '+' + suffix } return string } /** * Test media type. * * @param {string} string * @return {object} * @public */ function test (string) { if (!string) { throw new TypeError('argument string is required') } if (typeof string !== 'string') { throw new TypeError('argument string is required to be a string') } return TYPE_REGEXP.test(string.toLowerCase()) } /** * Parse media type to object. * * @param {string} string * @return {object} * @public */ function parse (string) { if (!string) { throw new TypeError('argument string is required') } if (typeof string !== 'string') { throw new TypeError('argument string is required to be a string') } var match = TYPE_REGEXP.exec(string.toLowerCase()) if (!match) { throw new TypeError('invalid media type') } var type = match[1] var subtype = match[2] var suffix // suffix after last + var index = subtype.lastIndexOf('+') if (index !== -1) { suffix = subtype.substr(index + 1) subtype = subtype.substr(0, index) } return new MediaType(type, subtype, suffix) } /** * Class for MediaType object. * @public */ function MediaType (type, subtype, suffix) { this.type = type this.subtype = subtype this.suffix = suffix } media-typer-1.1.0/package.json000066400000000000000000000016351346022247300162270ustar00rootroot00000000000000{ "name": "media-typer", "description": "Simple RFC 6838 media type parser and formatter", "version": "1.1.0", "author": "Douglas Christopher Wilson ", "license": "MIT", "repository": "jshttp/media-typer", "devDependencies": { "eslint": "5.16.0", "eslint-config-standard": "12.0.0", "eslint-plugin-import": "2.17.2", "eslint-plugin-markdown": "1.0.0", "eslint-plugin-node": "8.0.1", "eslint-plugin-promise": "4.1.1", "eslint-plugin-standard": "4.0.0", "mocha": "6.1.4", "nyc": "14.0.0" }, "files": [ "LICENSE", "HISTORY.md", "index.js" ], "engines": { "node": ">= 0.8" }, "scripts": { "lint": "eslint --plugin markdown --ext js,md .", "test": "mocha --reporter spec --check-leaks --bail test/", "test-cov": "nyc --reporter=html --reporter=text npm test", "test-travis": "nyc --reporter=text npm test" } } media-typer-1.1.0/test/000077500000000000000000000000001346022247300147135ustar00rootroot00000000000000media-typer-1.1.0/test/.eslintrc.yml000066400000000000000000000000231346022247300173320ustar00rootroot00000000000000env: mocha: true media-typer-1.1.0/test/test.js000066400000000000000000000066441346022247300162420ustar00rootroot00000000000000 var assert = require('assert') var typer = require('..') var invalidTypes = [ ' ', 'null', 'undefined', '/', 'text/;plain', 'text/"plain"', 'text/p£ain', 'text/(plain)', 'text/@plain', 'text/plain,wrong' ] describe('typer.format(obj)', function () { it('should format basic type', function () { var str = typer.format({ type: 'text', subtype: 'html' }) assert.strictEqual(str, 'text/html') }) it('should format type with suffix', function () { var str = typer.format({ type: 'image', subtype: 'svg', suffix: 'xml' }) assert.strictEqual(str, 'image/svg+xml') }) it('should require argument', function () { assert.throws(typer.format.bind(null), /obj.*required/) }) it('should reject non-objects', function () { assert.throws(typer.format.bind(null, 7), /obj.*required/) }) it('should require type', function () { assert.throws(typer.format.bind(null, {}), /invalid type/) }) it('should reject invalid type', function () { assert.throws(typer.format.bind(null, { type: 'text/' }), /invalid type/) }) it('should require subtype', function () { assert.throws(typer.format.bind(null, { type: 'text' }), /invalid subtype/) }) it('should reject invalid subtype', function () { var obj = { type: 'text', subtype: 'html/' } assert.throws(typer.format.bind(null, obj), /invalid subtype/) }) it('should reject invalid suffix', function () { var obj = { type: 'image', subtype: 'svg', suffix: 'xml\\' } assert.throws(typer.format.bind(null, obj), /invalid suffix/) }) }) describe('typer.parse(string)', function () { it('should parse basic type', function () { var type = typer.parse('text/html') assert.strictEqual(type.type, 'text') assert.strictEqual(type.subtype, 'html') }) it('should parse with suffix', function () { var type = typer.parse('image/svg+xml') assert.strictEqual(type.type, 'image') assert.strictEqual(type.subtype, 'svg') assert.strictEqual(type.suffix, 'xml') }) it('should lower-case type', function () { var type = typer.parse('IMAGE/SVG+XML') assert.strictEqual(type.type, 'image') assert.strictEqual(type.subtype, 'svg') assert.strictEqual(type.suffix, 'xml') }) invalidTypes.forEach(function (type) { it('should throw on invalid media type ' + JSON.stringify(type), function () { assert.throws(typer.parse.bind(null, type), /invalid media type/) }) }) it('should require argument', function () { assert.throws(typer.parse.bind(null), /string.*required/) }) it('should reject non-strings', function () { assert.throws(typer.parse.bind(null, 7), /string.*required/) }) }) describe('typer.test(string)', function () { it('should pass basic type', function () { assert.strictEqual(typer.test('text/html'), true) }) it('should pass with suffix', function () { assert.strictEqual(typer.test('image/svg+xml'), true) }) it('should pass upper-case type', function () { assert.strictEqual(typer.test('IMAGE/SVG+XML'), true) }) invalidTypes.forEach(function (type) { it('should fail invalid media type ' + JSON.stringify(type), function () { assert.strictEqual(typer.test(type), false) }) }) it('should require argument', function () { assert.throws(typer.test.bind(null), /string.*required/) }) it('should reject non-strings', function () { assert.throws(typer.test.bind(null, 7), /string.*required/) }) })