@@ -0,0 +1 @@ | |||
../mime/cli.js |
@@ -0,0 +1,218 @@ | |||
1.3.4 / 2017-08-22 | |||
================== | |||
* deps: mime-types@~2.1.16 | |||
- deps: mime-db@~1.29.0 | |||
1.3.3 / 2016-05-02 | |||
================== | |||
* deps: mime-types@~2.1.11 | |||
- deps: mime-db@~1.23.0 | |||
* deps: negotiator@0.6.1 | |||
- perf: improve `Accept` parsing speed | |||
- perf: improve `Accept-Charset` parsing speed | |||
- perf: improve `Accept-Encoding` parsing speed | |||
- perf: improve `Accept-Language` parsing speed | |||
1.3.2 / 2016-03-08 | |||
================== | |||
* deps: mime-types@~2.1.10 | |||
- Fix extension of `application/dash+xml` | |||
- Update primary extension for `audio/mp4` | |||
- deps: mime-db@~1.22.0 | |||
1.3.1 / 2016-01-19 | |||
================== | |||
* deps: mime-types@~2.1.9 | |||
- deps: mime-db@~1.21.0 | |||
1.3.0 / 2015-09-29 | |||
================== | |||
* deps: mime-types@~2.1.7 | |||
- deps: mime-db@~1.19.0 | |||
* deps: negotiator@0.6.0 | |||
- Fix including type extensions in parameters in `Accept` parsing | |||
- Fix parsing `Accept` parameters with quoted equals | |||
- Fix parsing `Accept` parameters with quoted semicolons | |||
- Lazy-load modules from main entry point | |||
- perf: delay type concatenation until needed | |||
- perf: enable strict mode | |||
- perf: hoist regular expressions | |||
- perf: remove closures getting spec properties | |||
- perf: remove a closure from media type parsing | |||
- perf: remove property delete from media type parsing | |||
1.2.13 / 2015-09-06 | |||
=================== | |||
* deps: mime-types@~2.1.6 | |||
- deps: mime-db@~1.18.0 | |||
1.2.12 / 2015-07-30 | |||
=================== | |||
* deps: mime-types@~2.1.4 | |||
- deps: mime-db@~1.16.0 | |||
1.2.11 / 2015-07-16 | |||
=================== | |||
* deps: mime-types@~2.1.3 | |||
- deps: mime-db@~1.15.0 | |||
1.2.10 / 2015-07-01 | |||
=================== | |||
* deps: mime-types@~2.1.2 | |||
- deps: mime-db@~1.14.0 | |||
1.2.9 / 2015-06-08 | |||
================== | |||
* deps: mime-types@~2.1.1 | |||
- perf: fix deopt during mapping | |||
1.2.8 / 2015-06-07 | |||
================== | |||
* deps: mime-types@~2.1.0 | |||
- deps: mime-db@~1.13.0 | |||
* perf: avoid argument reassignment & argument slice | |||
* perf: avoid negotiator recursive construction | |||
* perf: enable strict mode | |||
* perf: remove unnecessary bitwise operator | |||
1.2.7 / 2015-05-10 | |||
================== | |||
* deps: negotiator@0.5.3 | |||
- Fix media type parameter matching to be case-insensitive | |||
1.2.6 / 2015-05-07 | |||
================== | |||
* deps: mime-types@~2.0.11 | |||
- deps: mime-db@~1.9.1 | |||
* deps: negotiator@0.5.2 | |||
- Fix comparing media types with quoted values | |||
- Fix splitting media types with quoted commas | |||
1.2.5 / 2015-03-13 | |||
================== | |||
* deps: mime-types@~2.0.10 | |||
- deps: mime-db@~1.8.0 | |||
1.2.4 / 2015-02-14 | |||
================== | |||
* Support Node.js 0.6 | |||
* deps: mime-types@~2.0.9 | |||
- deps: mime-db@~1.7.0 | |||
* deps: negotiator@0.5.1 | |||
- Fix preference sorting to be stable for long acceptable lists | |||
1.2.3 / 2015-01-31 | |||
================== | |||
* deps: mime-types@~2.0.8 | |||
- deps: mime-db@~1.6.0 | |||
1.2.2 / 2014-12-30 | |||
================== | |||
* deps: mime-types@~2.0.7 | |||
- deps: mime-db@~1.5.0 | |||
1.2.1 / 2014-12-30 | |||
================== | |||
* deps: mime-types@~2.0.5 | |||
- deps: mime-db@~1.3.1 | |||
1.2.0 / 2014-12-19 | |||
================== | |||
* deps: negotiator@0.5.0 | |||
- Fix list return order when large accepted list | |||
- Fix missing identity encoding when q=0 exists | |||
- Remove dynamic building of Negotiator class | |||
1.1.4 / 2014-12-10 | |||
================== | |||
* deps: mime-types@~2.0.4 | |||
- deps: mime-db@~1.3.0 | |||
1.1.3 / 2014-11-09 | |||
================== | |||
* deps: mime-types@~2.0.3 | |||
- deps: mime-db@~1.2.0 | |||
1.1.2 / 2014-10-14 | |||
================== | |||
* deps: negotiator@0.4.9 | |||
- Fix error when media type has invalid parameter | |||
1.1.1 / 2014-09-28 | |||
================== | |||
* deps: mime-types@~2.0.2 | |||
- deps: mime-db@~1.1.0 | |||
* deps: negotiator@0.4.8 | |||
- Fix all negotiations to be case-insensitive | |||
- Stable sort preferences of same quality according to client order | |||
1.1.0 / 2014-09-02 | |||
================== | |||
* update `mime-types` | |||
1.0.7 / 2014-07-04 | |||
================== | |||
* Fix wrong type returned from `type` when match after unknown extension | |||
1.0.6 / 2014-06-24 | |||
================== | |||
* deps: negotiator@0.4.7 | |||
1.0.5 / 2014-06-20 | |||
================== | |||
* fix crash when unknown extension given | |||
1.0.4 / 2014-06-19 | |||
================== | |||
* use `mime-types` | |||
1.0.3 / 2014-06-11 | |||
================== | |||
* deps: negotiator@0.4.6 | |||
- Order by specificity when quality is the same | |||
1.0.2 / 2014-05-29 | |||
================== | |||
* Fix interpretation when header not in request | |||
* deps: pin negotiator@0.4.5 | |||
1.0.1 / 2014-01-18 | |||
================== | |||
* Identity encoding isn't always acceptable | |||
* deps: negotiator@~0.4.0 | |||
1.0.0 / 2013-12-27 | |||
================== | |||
* Genesis |
@@ -0,0 +1,23 @@ | |||
(The MIT License) | |||
Copyright (c) 2014 Jonathan Ong <me@jongleberry.com> | |||
Copyright (c) 2015 Douglas Christopher Wilson <doug@somethingdoug.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. |
@@ -0,0 +1,143 @@ | |||
# accepts | |||
[![NPM Version][npm-image]][npm-url] | |||
[![NPM Downloads][downloads-image]][downloads-url] | |||
[![Node.js Version][node-version-image]][node-version-url] | |||
[![Build Status][travis-image]][travis-url] | |||
[![Test Coverage][coveralls-image]][coveralls-url] | |||
Higher level content negotiation based on [negotiator](https://www.npmjs.com/package/negotiator). | |||
Extracted from [koa](https://www.npmjs.com/package/koa) for general use. | |||
In addition to negotiator, it allows: | |||
- Allows types as an array or arguments list, ie `(['text/html', 'application/json'])` | |||
as well as `('text/html', 'application/json')`. | |||
- Allows type shorthands such as `json`. | |||
- Returns `false` when no types match | |||
- Treats non-existent headers as `*` | |||
## 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 accepts | |||
``` | |||
## API | |||
<!-- eslint-disable no-unused-vars --> | |||
```js | |||
var accepts = require('accepts') | |||
``` | |||
### accepts(req) | |||
Create a new `Accepts` object for the given `req`. | |||
#### .charset(charsets) | |||
Return the first accepted charset. If nothing in `charsets` is accepted, | |||
then `false` is returned. | |||
#### .charsets() | |||
Return the charsets that the request accepts, in the order of the client's | |||
preference (most preferred first). | |||
#### .encoding(encodings) | |||
Return the first accepted encoding. If nothing in `encodings` is accepted, | |||
then `false` is returned. | |||
#### .encodings() | |||
Return the encodings that the request accepts, in the order of the client's | |||
preference (most preferred first). | |||
#### .language(languages) | |||
Return the first accepted language. If nothing in `languages` is accepted, | |||
then `false` is returned. | |||
#### .languages() | |||
Return the languages that the request accepts, in the order of the client's | |||
preference (most preferred first). | |||
#### .type(types) | |||
Return the first accepted type (and it is returned as the same text as what | |||
appears in the `types` array). If nothing in `types` is accepted, then `false` | |||
is returned. | |||
The `types` array can contain full MIME types or file extensions. Any value | |||
that is not a full MIME types is passed to `require('mime-types').lookup`. | |||
#### .types() | |||
Return the types that the request accepts, in the order of the client's | |||
preference (most preferred first). | |||
## Examples | |||
### Simple type negotiation | |||
This simple example shows how to use `accepts` to return a different typed | |||
respond body based on what the client wants to accept. The server lists it's | |||
preferences in order and will get back the best match between the client and | |||
server. | |||
```js | |||
var accepts = require('accepts') | |||
var http = require('http') | |||
function app (req, res) { | |||
var accept = accepts(req) | |||
// the order of this list is significant; should be server preferred order | |||
switch (accept.type(['json', 'html'])) { | |||
case 'json': | |||
res.setHeader('Content-Type', 'application/json') | |||
res.write('{"hello":"world!"}') | |||
break | |||
case 'html': | |||
res.setHeader('Content-Type', 'text/html') | |||
res.write('<b>hello, world!</b>') | |||
break | |||
default: | |||
// the fallback is text/plain, so no need to specify it above | |||
res.setHeader('Content-Type', 'text/plain') | |||
res.write('hello, world!') | |||
break | |||
} | |||
res.end() | |||
} | |||
http.createServer(app).listen(3000) | |||
``` | |||
You can test this out with the cURL program: | |||
```sh | |||
curl -I -H'Accept: text/html' http://localhost:3000/ | |||
``` | |||
## License | |||
[MIT](LICENSE) | |||
[npm-image]: https://img.shields.io/npm/v/accepts.svg | |||
[npm-url]: https://npmjs.org/package/accepts | |||
[node-version-image]: https://img.shields.io/node/v/accepts.svg | |||
[node-version-url]: https://nodejs.org/en/download/ | |||
[travis-image]: https://img.shields.io/travis/jshttp/accepts/master.svg | |||
[travis-url]: https://travis-ci.org/jshttp/accepts | |||
[coveralls-image]: https://img.shields.io/coveralls/jshttp/accepts/master.svg | |||
[coveralls-url]: https://coveralls.io/r/jshttp/accepts | |||
[downloads-image]: https://img.shields.io/npm/dm/accepts.svg | |||
[downloads-url]: https://npmjs.org/package/accepts |
@@ -0,0 +1,238 @@ | |||
/*! | |||
* accepts | |||
* Copyright(c) 2014 Jonathan Ong | |||
* Copyright(c) 2015 Douglas Christopher Wilson | |||
* MIT Licensed | |||
*/ | |||
'use strict' | |||
/** | |||
* Module dependencies. | |||
* @private | |||
*/ | |||
var Negotiator = require('negotiator') | |||
var mime = require('mime-types') | |||
/** | |||
* Module exports. | |||
* @public | |||
*/ | |||
module.exports = Accepts | |||
/** | |||
* Create a new Accepts object for the given req. | |||
* | |||
* @param {object} req | |||
* @public | |||
*/ | |||
function Accepts (req) { | |||
if (!(this instanceof Accepts)) { | |||
return new Accepts(req) | |||
} | |||
this.headers = req.headers | |||
this.negotiator = new Negotiator(req) | |||
} | |||
/** | |||
* Check if the given `type(s)` is acceptable, returning | |||
* the best match when true, otherwise `undefined`, in which | |||
* case you should respond with 406 "Not Acceptable". | |||
* | |||
* The `type` value may be a single mime type string | |||
* such as "application/json", the extension name | |||
* such as "json" or an array `["json", "html", "text/plain"]`. When a list | |||
* or array is given the _best_ match, if any is returned. | |||
* | |||
* Examples: | |||
* | |||
* // Accept: text/html | |||
* this.types('html'); | |||
* // => "html" | |||
* | |||
* // Accept: text/*, application/json | |||
* this.types('html'); | |||
* // => "html" | |||
* this.types('text/html'); | |||
* // => "text/html" | |||
* this.types('json', 'text'); | |||
* // => "json" | |||
* this.types('application/json'); | |||
* // => "application/json" | |||
* | |||
* // Accept: text/*, application/json | |||
* this.types('image/png'); | |||
* this.types('png'); | |||
* // => undefined | |||
* | |||
* // Accept: text/*;q=.5, application/json | |||
* this.types(['html', 'json']); | |||
* this.types('html', 'json'); | |||
* // => "json" | |||
* | |||
* @param {String|Array} types... | |||
* @return {String|Array|Boolean} | |||
* @public | |||
*/ | |||
Accepts.prototype.type = | |||
Accepts.prototype.types = function (types_) { | |||
var types = types_ | |||
// support flattened arguments | |||
if (types && !Array.isArray(types)) { | |||
types = new Array(arguments.length) | |||
for (var i = 0; i < types.length; i++) { | |||
types[i] = arguments[i] | |||
} | |||
} | |||
// no types, return all requested types | |||
if (!types || types.length === 0) { | |||
return this.negotiator.mediaTypes() | |||
} | |||
// no accept header, return first given type | |||
if (!this.headers.accept) { | |||
return types[0] | |||
} | |||
var mimes = types.map(extToMime) | |||
var accepts = this.negotiator.mediaTypes(mimes.filter(validMime)) | |||
var first = accepts[0] | |||
return first | |||
? types[mimes.indexOf(first)] | |||
: false | |||
} | |||
/** | |||
* Return accepted encodings or best fit based on `encodings`. | |||
* | |||
* Given `Accept-Encoding: gzip, deflate` | |||
* an array sorted by quality is returned: | |||
* | |||
* ['gzip', 'deflate'] | |||
* | |||
* @param {String|Array} encodings... | |||
* @return {String|Array} | |||
* @public | |||
*/ | |||
Accepts.prototype.encoding = | |||
Accepts.prototype.encodings = function (encodings_) { | |||
var encodings = encodings_ | |||
// support flattened arguments | |||
if (encodings && !Array.isArray(encodings)) { | |||
encodings = new Array(arguments.length) | |||
for (var i = 0; i < encodings.length; i++) { | |||
encodings[i] = arguments[i] | |||
} | |||
} | |||
// no encodings, return all requested encodings | |||
if (!encodings || encodings.length === 0) { | |||
return this.negotiator.encodings() | |||
} | |||
return this.negotiator.encodings(encodings)[0] || false | |||
} | |||
/** | |||
* Return accepted charsets or best fit based on `charsets`. | |||
* | |||
* Given `Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5` | |||
* an array sorted by quality is returned: | |||
* | |||
* ['utf-8', 'utf-7', 'iso-8859-1'] | |||
* | |||
* @param {String|Array} charsets... | |||
* @return {String|Array} | |||
* @public | |||
*/ | |||
Accepts.prototype.charset = | |||
Accepts.prototype.charsets = function (charsets_) { | |||
var charsets = charsets_ | |||
// support flattened arguments | |||
if (charsets && !Array.isArray(charsets)) { | |||
charsets = new Array(arguments.length) | |||
for (var i = 0; i < charsets.length; i++) { | |||
charsets[i] = arguments[i] | |||
} | |||
} | |||
// no charsets, return all requested charsets | |||
if (!charsets || charsets.length === 0) { | |||
return this.negotiator.charsets() | |||
} | |||
return this.negotiator.charsets(charsets)[0] || false | |||
} | |||
/** | |||
* Return accepted languages or best fit based on `langs`. | |||
* | |||
* Given `Accept-Language: en;q=0.8, es, pt` | |||
* an array sorted by quality is returned: | |||
* | |||
* ['es', 'pt', 'en'] | |||
* | |||
* @param {String|Array} langs... | |||
* @return {Array|String} | |||
* @public | |||
*/ | |||
Accepts.prototype.lang = | |||
Accepts.prototype.langs = | |||
Accepts.prototype.language = | |||
Accepts.prototype.languages = function (languages_) { | |||
var languages = languages_ | |||
// support flattened arguments | |||
if (languages && !Array.isArray(languages)) { | |||
languages = new Array(arguments.length) | |||
for (var i = 0; i < languages.length; i++) { | |||
languages[i] = arguments[i] | |||
} | |||
} | |||
// no languages, return all requested languages | |||
if (!languages || languages.length === 0) { | |||
return this.negotiator.languages() | |||
} | |||
return this.negotiator.languages(languages)[0] || false | |||
} | |||
/** | |||
* Convert extnames to mime. | |||
* | |||
* @param {String} type | |||
* @return {String} | |||
* @private | |||
*/ | |||
function extToMime (type) { | |||
return type.indexOf('/') === -1 | |||
? mime.lookup(type) | |||
: type | |||
} | |||
/** | |||
* Check if mime is valid. | |||
* | |||
* @param {String} type | |||
* @return {String} | |||
* @private | |||
*/ | |||
function validMime (type) { | |||
return typeof type === 'string' | |||
} |
@@ -0,0 +1,112 @@ | |||
{ | |||
"_args": [ | |||
[ | |||
"accepts@~1.3.4", | |||
"/home/jd/BansheeBot/node_modules/express" | |||
] | |||
], | |||
"_from": "accepts@>=1.3.4 <1.4.0", | |||
"_id": "accepts@1.3.4", | |||
"_inCache": true, | |||
"_installable": true, | |||
"_location": "/accepts", | |||
"_nodeVersion": "6.11.1", | |||
"_npmOperationalInternal": { | |||
"host": "s3://npm-registry-packages", | |||
"tmp": "tmp/accepts-1.3.4.tgz_1503455053008_0.43370609171688557" | |||
}, | |||
"_npmUser": { | |||
"email": "doug@somethingdoug.com", | |||
"name": "dougwilson" | |||
}, | |||
"_npmVersion": "3.10.10", | |||
"_phantomChildren": {}, | |||
"_requested": { | |||
"name": "accepts", | |||
"raw": "accepts@~1.3.4", | |||
"rawSpec": "~1.3.4", | |||
"scope": null, | |||
"spec": ">=1.3.4 <1.4.0", | |||
"type": "range" | |||
}, | |||
"_requiredBy": [ | |||
"/express" | |||
], | |||
"_resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.4.tgz", | |||
"_shasum": "86246758c7dd6d21a6474ff084a4740ec05eb21f", | |||
"_shrinkwrap": null, | |||
"_spec": "accepts@~1.3.4", | |||
"_where": "/home/jd/BansheeBot/node_modules/express", | |||
"bugs": { | |||
"url": "https://github.com/jshttp/accepts/issues" | |||
}, | |||
"contributors": [ | |||
{ | |||
"name": "Douglas Christopher Wilson", | |||
"email": "doug@somethingdoug.com" | |||
}, | |||
{ | |||
"name": "Jonathan Ong", | |||
"email": "me@jongleberry.com", | |||
"url": "http://jongleberry.com" | |||
} | |||
], | |||
"dependencies": { | |||
"mime-types": "~2.1.16", | |||
"negotiator": "0.6.1" | |||
}, | |||
"description": "Higher-level content negotiation", | |||
"devDependencies": { | |||
"eslint": "3.19.0", | |||
"eslint-config-standard": "10.2.1", | |||
"eslint-plugin-import": "2.7.0", | |||
"eslint-plugin-markdown": "1.0.0-beta.6", | |||
"eslint-plugin-node": "5.1.1", | |||
"eslint-plugin-promise": "3.5.0", | |||
"eslint-plugin-standard": "3.0.1", | |||
"istanbul": "0.4.5", | |||
"mocha": "~1.21.5" | |||
}, | |||
"directories": {}, | |||
"dist": { | |||
"shasum": "86246758c7dd6d21a6474ff084a4740ec05eb21f", | |||
"tarball": "https://registry.npmjs.org/accepts/-/accepts-1.3.4.tgz" | |||
}, | |||
"engines": { | |||
"node": ">= 0.6" | |||
}, | |||
"files": [ | |||
"HISTORY.md", | |||
"LICENSE", | |||
"index.js" | |||
], | |||
"gitHead": "71ea430741d6eb5484b6c67c95924540a98186a5", | |||
"homepage": "https://github.com/jshttp/accepts#readme", | |||
"keywords": [ | |||
"accept", | |||
"accepts", | |||
"content", | |||
"negotiation" | |||
], | |||
"license": "MIT", | |||
"maintainers": [ | |||
{ | |||
"name": "dougwilson", | |||
"email": "doug@somethingdoug.com" | |||
} | |||
], | |||
"name": "accepts", | |||
"optionalDependencies": {}, | |||
"readme": "ERROR: No README data found!", | |||
"repository": { | |||
"type": "git", | |||
"url": "git+https://github.com/jshttp/accepts.git" | |||
}, | |||
"scripts": { | |||
"lint": "eslint --plugin markdown --ext js,md .", | |||
"test": "mocha --reporter spec --check-leaks --bail test/", | |||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", | |||
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/" | |||
}, | |||
"version": "1.3.4" | |||
} |
@@ -0,0 +1,21 @@ | |||
The MIT License (MIT) | |||
Copyright (c) 2014 Blake Embrey (hello@blakeembrey.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. |
@@ -0,0 +1,43 @@ | |||
# Array Flatten | |||
[![NPM version][npm-image]][npm-url] | |||
[![NPM downloads][downloads-image]][downloads-url] | |||
[![Build status][travis-image]][travis-url] | |||
[![Test coverage][coveralls-image]][coveralls-url] | |||
> Flatten an array of nested arrays into a single flat array. Accepts an optional depth. | |||
## Installation | |||
``` | |||
npm install array-flatten --save | |||
``` | |||
## Usage | |||
```javascript | |||
var flatten = require('array-flatten') | |||
flatten([1, [2, [3, [4, [5], 6], 7], 8], 9]) | |||
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9] | |||
flatten([1, [2, [3, [4, [5], 6], 7], 8], 9], 2) | |||
//=> [1, 2, 3, [4, [5], 6], 7, 8, 9] | |||
(function () { | |||
flatten(arguments) //=> [1, 2, 3] | |||
})(1, [2, 3]) | |||
``` | |||
## License | |||
MIT | |||
[npm-image]: https://img.shields.io/npm/v/array-flatten.svg?style=flat | |||
[npm-url]: https://npmjs.org/package/array-flatten | |||
[downloads-image]: https://img.shields.io/npm/dm/array-flatten.svg?style=flat | |||
[downloads-url]: https://npmjs.org/package/array-flatten | |||
[travis-image]: https://img.shields.io/travis/blakeembrey/array-flatten.svg?style=flat | |||
[travis-url]: https://travis-ci.org/blakeembrey/array-flatten | |||
[coveralls-image]: https://img.shields.io/coveralls/blakeembrey/array-flatten.svg?style=flat | |||
[coveralls-url]: https://coveralls.io/r/blakeembrey/array-flatten?branch=master |
@@ -0,0 +1,64 @@ | |||
'use strict' | |||
/** | |||
* Expose `arrayFlatten`. | |||
*/ | |||
module.exports = arrayFlatten | |||
/** | |||
* Recursive flatten function with depth. | |||
* | |||
* @param {Array} array | |||
* @param {Array} result | |||
* @param {Number} depth | |||
* @return {Array} | |||
*/ | |||
function flattenWithDepth (array, result, depth) { | |||
for (var i = 0; i < array.length; i++) { | |||
var value = array[i] | |||
if (depth > 0 && Array.isArray(value)) { | |||
flattenWithDepth(value, result, depth - 1) | |||
} else { | |||
result.push(value) | |||
} | |||
} | |||
return result | |||
} | |||
/** | |||
* Recursive flatten function. Omitting depth is slightly faster. | |||
* | |||
* @param {Array} array | |||
* @param {Array} result | |||
* @return {Array} | |||
*/ | |||
function flattenForever (array, result) { | |||
for (var i = 0; i < array.length; i++) { | |||
var value = array[i] | |||
if (Array.isArray(value)) { | |||
flattenForever(value, result) | |||
} else { | |||
result.push(value) | |||
} | |||
} | |||
return result | |||
} | |||
/** | |||
* Flatten an array, with the ability to define a depth. | |||
* | |||
* @param {Array} array | |||
* @param {Number} depth | |||
* @return {Array} | |||
*/ | |||
function arrayFlatten (array, depth) { | |||
if (depth == null) { | |||
return flattenForever(array, []) | |||
} | |||
return flattenWithDepth(array, [], depth) | |||
} |
@@ -0,0 +1,88 @@ | |||
{ | |||
"_args": [ | |||
[ | |||
"array-flatten@1.1.1", | |||
"/home/jd/BansheeBot/node_modules/express" | |||
] | |||
], | |||
"_from": "array-flatten@1.1.1", | |||
"_id": "array-flatten@1.1.1", | |||
"_inCache": true, | |||
"_installable": true, | |||
"_location": "/array-flatten", | |||
"_nodeVersion": "2.3.3", | |||
"_npmUser": { | |||
"email": "hello@blakeembrey.com", | |||
"name": "blakeembrey" | |||
}, | |||
"_npmVersion": "2.11.3", | |||
"_phantomChildren": {}, | |||
"_requested": { | |||
"name": "array-flatten", | |||
"raw": "array-flatten@1.1.1", | |||
"rawSpec": "1.1.1", | |||
"scope": null, | |||
"spec": "1.1.1", | |||
"type": "version" | |||
}, | |||
"_requiredBy": [ | |||
"/express" | |||
], | |||
"_resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", | |||
"_shasum": "9a5f699051b1e7073328f2a008968b64ea2955d2", | |||
"_shrinkwrap": null, | |||
"_spec": "array-flatten@1.1.1", | |||
"_where": "/home/jd/BansheeBot/node_modules/express", | |||
"author": { | |||
"email": "hello@blakeembrey.com", | |||
"name": "Blake Embrey", | |||
"url": "http://blakeembrey.me" | |||
}, | |||
"bugs": { | |||
"url": "https://github.com/blakeembrey/array-flatten/issues" | |||
}, | |||
"dependencies": {}, | |||
"description": "Flatten an array of nested arrays into a single flat array", | |||
"devDependencies": { | |||
"istanbul": "^0.3.13", | |||
"mocha": "^2.2.4", | |||
"pre-commit": "^1.0.7", | |||
"standard": "^3.7.3" | |||
}, | |||
"directories": {}, | |||
"dist": { | |||
"shasum": "9a5f699051b1e7073328f2a008968b64ea2955d2", | |||
"tarball": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz" | |||
}, | |||
"files": [ | |||
"LICENSE", | |||
"array-flatten.js" | |||
], | |||
"gitHead": "1963a9189229d408e1e8f585a00c8be9edbd1803", | |||
"homepage": "https://github.com/blakeembrey/array-flatten", | |||
"keywords": [ | |||
"arguments", | |||
"array", | |||
"depth", | |||
"flatten" | |||
], | |||
"license": "MIT", | |||
"main": "array-flatten.js", | |||
"maintainers": [ | |||
{ | |||
"name": "blakeembrey", | |||
"email": "hello@blakeembrey.com" | |||
} | |||
], | |||
"name": "array-flatten", | |||
"optionalDependencies": {}, | |||
"readme": "ERROR: No README data found!", | |||
"repository": { | |||
"type": "git", | |||
"url": "git://github.com/blakeembrey/array-flatten.git" | |||
}, | |||
"scripts": { | |||
"test": "istanbul cover _mocha -- -R spec" | |||
}, | |||
"version": "1.1.1" | |||
} |
@@ -0,0 +1,568 @@ | |||
1.18.2 / 2017-09-22 | |||
=================== | |||
* deps: debug@2.6.9 | |||
* perf: remove argument reassignment | |||
1.18.1 / 2017-09-12 | |||
=================== | |||
* deps: content-type@~1.0.4 | |||
- perf: remove argument reassignment | |||
- perf: skip parameter parsing when no parameters | |||
* deps: iconv-lite@0.4.19 | |||
- Fix ISO-8859-1 regression | |||
- Update Windows-1255 | |||
* deps: qs@6.5.1 | |||
- Fix parsing & compacting very deep objects | |||
* deps: raw-body@2.3.2 | |||
- deps: iconv-lite@0.4.19 | |||
1.18.0 / 2017-09-08 | |||
=================== | |||
* Fix JSON strict violation error to match native parse error | |||
* Include the `body` property on verify errors | |||
* Include the `type` property on all generated errors | |||
* Use `http-errors` to set status code on errors | |||
* deps: bytes@3.0.0 | |||
* deps: debug@2.6.8 | |||
* deps: depd@~1.1.1 | |||
- Remove unnecessary `Buffer` loading | |||
* deps: http-errors@~1.6.2 | |||
- deps: depd@1.1.1 | |||
* deps: iconv-lite@0.4.18 | |||
- Add support for React Native | |||
- Add a warning if not loaded as utf-8 | |||
- Fix CESU-8 decoding in Node.js 8 | |||
- Improve speed of ISO-8859-1 encoding | |||
* deps: qs@6.5.0 | |||
* deps: raw-body@2.3.1 | |||
- Use `http-errors` for standard emitted errors | |||
- deps: bytes@3.0.0 | |||
- deps: iconv-lite@0.4.18 | |||
- perf: skip buffer decoding on overage chunk | |||
* perf: prevent internal `throw` when missing charset | |||
1.17.2 / 2017-05-17 | |||
=================== | |||
* deps: debug@2.6.7 | |||
- Fix `DEBUG_MAX_ARRAY_LENGTH` | |||
- deps: ms@2.0.0 | |||
* deps: type-is@~1.6.15 | |||
- deps: mime-types@~2.1.15 | |||
1.17.1 / 2017-03-06 | |||
=================== | |||
* deps: qs@6.4.0 | |||
- Fix regression parsing keys starting with `[` | |||
1.17.0 / 2017-03-01 | |||
=================== | |||
* deps: http-errors@~1.6.1 | |||
- Make `message` property enumerable for `HttpError`s | |||
- deps: setprototypeof@1.0.3 | |||
* deps: qs@6.3.1 | |||
- Fix compacting nested arrays | |||
1.16.1 / 2017-02-10 | |||
=================== | |||
* deps: debug@2.6.1 | |||
- Fix deprecation messages in WebStorm and other editors | |||
- Undeprecate `DEBUG_FD` set to `1` or `2` | |||
1.16.0 / 2017-01-17 | |||
=================== | |||
* deps: debug@2.6.0 | |||
- Allow colors in workers | |||
- Deprecated `DEBUG_FD` environment variable | |||
- Fix error when running under React Native | |||
- Use same color for same namespace | |||
- deps: ms@0.7.2 | |||
* deps: http-errors@~1.5.1 | |||
- deps: inherits@2.0.3 | |||
- deps: setprototypeof@1.0.2 | |||
- deps: statuses@'>= 1.3.1 < 2' | |||
* deps: iconv-lite@0.4.15 | |||
- Added encoding MS-31J | |||
- Added encoding MS-932 | |||
- Added encoding MS-936 | |||
- Added encoding MS-949 | |||
- Added encoding MS-950 | |||
- Fix GBK/GB18030 handling of Euro character | |||
* deps: qs@6.2.1 | |||
- Fix array parsing from skipping empty values | |||
* deps: raw-body@~2.2.0 | |||
- deps: iconv-lite@0.4.15 | |||
* deps: type-is@~1.6.14 | |||
- deps: mime-types@~2.1.13 | |||
1.15.2 / 2016-06-19 | |||
=================== | |||
* deps: bytes@2.4.0 | |||
* deps: content-type@~1.0.2 | |||
- perf: enable strict mode | |||
* deps: http-errors@~1.5.0 | |||
- Use `setprototypeof` module to replace `__proto__` setting | |||
- deps: statuses@'>= 1.3.0 < 2' | |||
- perf: enable strict mode | |||
* deps: qs@6.2.0 | |||
* deps: raw-body@~2.1.7 | |||
- deps: bytes@2.4.0 | |||
- perf: remove double-cleanup on happy path | |||
* deps: type-is@~1.6.13 | |||
- deps: mime-types@~2.1.11 | |||
1.15.1 / 2016-05-05 | |||
=================== | |||
* deps: bytes@2.3.0 | |||
- Drop partial bytes on all parsed units | |||
- Fix parsing byte string that looks like hex | |||
* deps: raw-body@~2.1.6 | |||
- deps: bytes@2.3.0 | |||
* deps: type-is@~1.6.12 | |||
- deps: mime-types@~2.1.10 | |||
1.15.0 / 2016-02-10 | |||
=================== | |||
* deps: http-errors@~1.4.0 | |||
- Add `HttpError` export, for `err instanceof createError.HttpError` | |||
- deps: inherits@2.0.1 | |||
- deps: statuses@'>= 1.2.1 < 2' | |||
* deps: qs@6.1.0 | |||
* deps: type-is@~1.6.11 | |||
- deps: mime-types@~2.1.9 | |||
1.14.2 / 2015-12-16 | |||
=================== | |||
* deps: bytes@2.2.0 | |||
* deps: iconv-lite@0.4.13 | |||
* deps: qs@5.2.0 | |||
* deps: raw-body@~2.1.5 | |||
- deps: bytes@2.2.0 | |||
- deps: iconv-lite@0.4.13 | |||
* deps: type-is@~1.6.10 | |||
- deps: mime-types@~2.1.8 | |||
1.14.1 / 2015-09-27 | |||
=================== | |||
* Fix issue where invalid charset results in 400 when `verify` used | |||
* deps: iconv-lite@0.4.12 | |||
- Fix CESU-8 decoding in Node.js 4.x | |||
* deps: raw-body@~2.1.4 | |||
- Fix masking critical errors from `iconv-lite` | |||
- deps: iconv-lite@0.4.12 | |||
* deps: type-is@~1.6.9 | |||
- deps: mime-types@~2.1.7 | |||
1.14.0 / 2015-09-16 | |||
=================== | |||
* Fix JSON strict parse error to match syntax errors | |||
* Provide static `require` analysis in `urlencoded` parser | |||
* deps: depd@~1.1.0 | |||
- Support web browser loading | |||
* deps: qs@5.1.0 | |||
* deps: raw-body@~2.1.3 | |||
- Fix sync callback when attaching data listener causes sync read | |||
* deps: type-is@~1.6.8 | |||
- Fix type error when given invalid type to match against | |||
- deps: mime-types@~2.1.6 | |||
1.13.3 / 2015-07-31 | |||
=================== | |||
* deps: type-is@~1.6.6 | |||
- deps: mime-types@~2.1.4 | |||
1.13.2 / 2015-07-05 | |||
=================== | |||
* deps: iconv-lite@0.4.11 | |||
* deps: qs@4.0.0 | |||
- Fix dropping parameters like `hasOwnProperty` | |||
- Fix user-visible incompatibilities from 3.1.0 | |||
- Fix various parsing edge cases | |||
* deps: raw-body@~2.1.2 | |||
- Fix error stack traces to skip `makeError` | |||
- deps: iconv-lite@0.4.11 | |||
* deps: type-is@~1.6.4 | |||
- deps: mime-types@~2.1.2 | |||
- perf: enable strict mode | |||
- perf: remove argument reassignment | |||
1.13.1 / 2015-06-16 | |||
=================== | |||
* deps: qs@2.4.2 | |||
- Downgraded from 3.1.0 because of user-visible incompatibilities | |||
1.13.0 / 2015-06-14 | |||
=================== | |||
* Add `statusCode` property on `Error`s, in addition to `status` | |||
* Change `type` default to `application/json` for JSON parser | |||
* Change `type` default to `application/x-www-form-urlencoded` for urlencoded parser | |||
* Provide static `require` analysis | |||
* Use the `http-errors` module to generate errors | |||
* deps: bytes@2.1.0 | |||
- Slight optimizations | |||
* deps: iconv-lite@0.4.10 | |||
- The encoding UTF-16 without BOM now defaults to UTF-16LE when detection fails | |||
- Leading BOM is now removed when decoding | |||
* deps: on-finished@~2.3.0 | |||
- Add defined behavior for HTTP `CONNECT` requests | |||
- Add defined behavior for HTTP `Upgrade` requests | |||
- deps: ee-first@1.1.1 | |||
* deps: qs@3.1.0 | |||
- Fix dropping parameters like `hasOwnProperty` | |||
- Fix various parsing edge cases | |||
- Parsed object now has `null` prototype | |||
* deps: raw-body@~2.1.1 | |||
- Use `unpipe` module for unpiping requests | |||
- deps: iconv-lite@0.4.10 | |||
* deps: type-is@~1.6.3 | |||
- deps: mime-types@~2.1.1 | |||
- perf: reduce try block size | |||
- perf: remove bitwise operations | |||
* perf: enable strict mode | |||
* perf: remove argument reassignment | |||
* perf: remove delete call | |||
1.12.4 / 2015-05-10 | |||
=================== | |||
* deps: debug@~2.2.0 | |||
* deps: qs@2.4.2 | |||
- Fix allowing parameters like `constructor` | |||
* deps: on-finished@~2.2.1 | |||
* deps: raw-body@~2.0.1 | |||
- Fix a false-positive when unpiping in Node.js 0.8 | |||
- deps: bytes@2.0.1 | |||
* deps: type-is@~1.6.2 | |||
- deps: mime-types@~2.0.11 | |||
1.12.3 / 2015-04-15 | |||
=================== | |||
* Slight efficiency improvement when not debugging | |||
* deps: depd@~1.0.1 | |||
* deps: iconv-lite@0.4.8 | |||
- Add encoding alias UNICODE-1-1-UTF-7 | |||
* deps: raw-body@1.3.4 | |||
- Fix hanging callback if request aborts during read | |||
- deps: iconv-lite@0.4.8 | |||
1.12.2 / 2015-03-16 | |||
=================== | |||
* deps: qs@2.4.1 | |||
- Fix error when parameter `hasOwnProperty` is present | |||
1.12.1 / 2015-03-15 | |||
=================== | |||
* deps: debug@~2.1.3 | |||
- Fix high intensity foreground color for bold | |||
- deps: ms@0.7.0 | |||
* deps: type-is@~1.6.1 | |||
- deps: mime-types@~2.0.10 | |||
1.12.0 / 2015-02-13 | |||
=================== | |||
* add `debug` messages | |||
* accept a function for the `type` option | |||
* use `content-type` to parse `Content-Type` headers | |||
* deps: iconv-lite@0.4.7 | |||
- Gracefully support enumerables on `Object.prototype` | |||
* deps: raw-body@1.3.3 | |||
- deps: iconv-lite@0.4.7 | |||
* deps: type-is@~1.6.0 | |||
- fix argument reassignment | |||
- fix false-positives in `hasBody` `Transfer-Encoding` check | |||
- support wildcard for both type and subtype (`*/*`) | |||
- deps: mime-types@~2.0.9 | |||
1.11.0 / 2015-01-30 | |||
=================== | |||
* make internal `extended: true` depth limit infinity | |||
* deps: type-is@~1.5.6 | |||
- deps: mime-types@~2.0.8 | |||
1.10.2 / 2015-01-20 | |||
=================== | |||
* deps: iconv-lite@0.4.6 | |||
- Fix rare aliases of single-byte encodings | |||
* deps: raw-body@1.3.2 | |||
- deps: iconv-lite@0.4.6 | |||
1.10.1 / 2015-01-01 | |||
=================== | |||
* deps: on-finished@~2.2.0 | |||
* deps: type-is@~1.5.5 | |||
- deps: mime-types@~2.0.7 | |||
1.10.0 / 2014-12-02 | |||
=================== | |||
* make internal `extended: true` array limit dynamic | |||
1.9.3 / 2014-11-21 | |||
================== | |||
* deps: iconv-lite@0.4.5 | |||
- Fix Windows-31J and X-SJIS encoding support | |||
* deps: qs@2.3.3 | |||
- Fix `arrayLimit` behavior | |||
* deps: raw-body@1.3.1 | |||
- deps: iconv-lite@0.4.5 | |||
* deps: type-is@~1.5.3 | |||
- deps: mime-types@~2.0.3 | |||
1.9.2 / 2014-10-27 | |||
================== | |||
* deps: qs@2.3.2 | |||
- Fix parsing of mixed objects and values | |||
1.9.1 / 2014-10-22 | |||
================== | |||
* deps: on-finished@~2.1.1 | |||
- Fix handling of pipelined requests | |||
* deps: qs@2.3.0 | |||
- Fix parsing of mixed implicit and explicit arrays | |||
* deps: type-is@~1.5.2 | |||
- deps: mime-types@~2.0.2 | |||
1.9.0 / 2014-09-24 | |||
================== | |||
* include the charset in "unsupported charset" error message | |||
* include the encoding in "unsupported content encoding" error message | |||
* deps: depd@~1.0.0 | |||
1.8.4 / 2014-09-23 | |||
================== | |||
* fix content encoding to be case-insensitive | |||
1.8.3 / 2014-09-19 | |||
================== | |||
* deps: qs@2.2.4 | |||
- Fix issue with object keys starting with numbers truncated | |||
1.8.2 / 2014-09-15 | |||
================== | |||
* deps: depd@0.4.5 | |||
1.8.1 / 2014-09-07 | |||
================== | |||
* deps: media-typer@0.3.0 | |||
* deps: type-is@~1.5.1 | |||
1.8.0 / 2014-09-05 | |||
================== | |||
* make empty-body-handling consistent between chunked requests | |||
- empty `json` produces `{}` | |||
- empty `raw` produces `new Buffer(0)` | |||
- empty `text` produces `''` | |||
- empty `urlencoded` produces `{}` | |||
* deps: qs@2.2.3 | |||
- Fix issue where first empty value in array is discarded | |||
* deps: type-is@~1.5.0 | |||
- fix `hasbody` to be true for `content-length: 0` | |||
1.7.0 / 2014-09-01 | |||
================== | |||
* add `parameterLimit` option to `urlencoded` parser | |||
* change `urlencoded` extended array limit to 100 | |||
* respond with 413 when over `parameterLimit` in `urlencoded` | |||
1.6.7 / 2014-08-29 | |||
================== | |||
* deps: qs@2.2.2 | |||
- Remove unnecessary cloning | |||
1.6.6 / 2014-08-27 | |||
================== | |||
* deps: qs@2.2.0 | |||
- Array parsing fix | |||
- Performance improvements | |||
1.6.5 / 2014-08-16 | |||
================== | |||
* deps: on-finished@2.1.0 | |||
1.6.4 / 2014-08-14 | |||
================== | |||
* deps: qs@1.2.2 | |||
1.6.3 / 2014-08-10 | |||
================== | |||
* deps: qs@1.2.1 | |||
1.6.2 / 2014-08-07 | |||
================== | |||
* deps: qs@1.2.0 | |||
- Fix parsing array of objects | |||
1.6.1 / 2014-08-06 | |||
================== | |||
* deps: qs@1.1.0 | |||
- Accept urlencoded square brackets | |||
- Accept empty values in implicit array notation | |||
1.6.0 / 2014-08-05 | |||
================== | |||
* deps: qs@1.0.2 | |||
- Complete rewrite | |||
- Limits array length to 20 | |||
- Limits object depth to 5 | |||
- Limits parameters to 1,000 | |||
1.5.2 / 2014-07-27 | |||
================== | |||
* deps: depd@0.4.4 | |||
- Work-around v8 generating empty stack traces | |||
1.5.1 / 2014-07-26 | |||
================== | |||
* deps: depd@0.4.3 | |||
- Fix exception when global `Error.stackTraceLimit` is too low | |||
1.5.0 / 2014-07-20 | |||
================== | |||
* deps: depd@0.4.2 | |||
- Add `TRACE_DEPRECATION` environment variable | |||
- Remove non-standard grey color from color output | |||
- Support `--no-deprecation` argument | |||
- Support `--trace-deprecation` argument | |||
* deps: iconv-lite@0.4.4 | |||
- Added encoding UTF-7 | |||
* deps: raw-body@1.3.0 | |||
- deps: iconv-lite@0.4.4 | |||
- Added encoding UTF-7 | |||
- Fix `Cannot switch to old mode now` error on Node.js 0.10+ | |||
* deps: type-is@~1.3.2 | |||
1.4.3 / 2014-06-19 | |||
================== | |||
* deps: type-is@1.3.1 | |||
- fix global variable leak | |||
1.4.2 / 2014-06-19 | |||
================== | |||
* deps: type-is@1.3.0 | |||
- improve type parsing | |||
1.4.1 / 2014-06-19 | |||
================== | |||
* fix urlencoded extended deprecation message | |||
1.4.0 / 2014-06-19 | |||
================== | |||
* add `text` parser | |||
* add `raw` parser | |||
* check accepted charset in content-type (accepts utf-8) | |||
* check accepted encoding in content-encoding (accepts identity) | |||
* deprecate `bodyParser()` middleware; use `.json()` and `.urlencoded()` as needed | |||
* deprecate `urlencoded()` without provided `extended` option | |||
* lazy-load urlencoded parsers | |||
* parsers split into files for reduced mem usage | |||
* support gzip and deflate bodies | |||
- set `inflate: false` to turn off | |||
* deps: raw-body@1.2.2 | |||
- Support all encodings from `iconv-lite` | |||
1.3.1 / 2014-06-11 | |||
================== | |||
* deps: type-is@1.2.1 | |||
- Switch dependency from mime to mime-types@1.0.0 | |||
1.3.0 / 2014-05-31 | |||
================== | |||
* add `extended` option to urlencoded parser | |||
1.2.2 / 2014-05-27 | |||
================== | |||
* deps: raw-body@1.1.6 | |||
- assert stream encoding on node.js 0.8 | |||
- assert stream encoding on node.js < 0.10.6 | |||
- deps: bytes@1 | |||
1.2.1 / 2014-05-26 | |||
================== | |||
* invoke `next(err)` after request fully read | |||
- prevents hung responses and socket hang ups | |||
1.2.0 / 2014-05-11 | |||
================== | |||
* add `verify` option | |||
* deps: type-is@1.2.0 | |||
- support suffix matching | |||
1.1.2 / 2014-05-11 | |||
================== | |||
* improve json parser speed | |||
1.1.1 / 2014-05-11 | |||
================== | |||
* fix repeated limit parsing with every request | |||
1.1.0 / 2014-05-10 | |||
================== | |||
* add `type` option | |||
* deps: pin for safety and consistency | |||
1.0.2 / 2014-04-14 | |||
================== | |||
* use `type-is` module | |||
1.0.1 / 2014-03-20 | |||
================== | |||
* lower default limits to 100kb |
@@ -0,0 +1,23 @@ | |||
(The MIT License) | |||
Copyright (c) 2014 Jonathan Ong <me@jongleberry.com> | |||
Copyright (c) 2014-2015 Douglas Christopher Wilson <doug@somethingdoug.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. |
@@ -0,0 +1,438 @@ | |||
# body-parser | |||
[![NPM Version][npm-image]][npm-url] | |||
[![NPM Downloads][downloads-image]][downloads-url] | |||
[![Build Status][travis-image]][travis-url] | |||
[![Test Coverage][coveralls-image]][coveralls-url] | |||
[![Gratipay][gratipay-image]][gratipay-url] | |||
Node.js body parsing middleware. | |||
Parse incoming request bodies in a middleware before your handlers, available | |||
under the `req.body` property. | |||
[Learn about the anatomy of an HTTP transaction in Node.js](https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/). | |||
_This does not handle multipart bodies_, due to their complex and typically | |||
large nature. For multipart bodies, you may be interested in the following | |||
modules: | |||
* [busboy](https://www.npmjs.org/package/busboy#readme) and | |||
[connect-busboy](https://www.npmjs.org/package/connect-busboy#readme) | |||
* [multiparty](https://www.npmjs.org/package/multiparty#readme) and | |||
[connect-multiparty](https://www.npmjs.org/package/connect-multiparty#readme) | |||
* [formidable](https://www.npmjs.org/package/formidable#readme) | |||
* [multer](https://www.npmjs.org/package/multer#readme) | |||
This module provides the following parsers: | |||
* [JSON body parser](#bodyparserjsonoptions) | |||
* [Raw body parser](#bodyparserrawoptions) | |||
* [Text body parser](#bodyparsertextoptions) | |||
* [URL-encoded form body parser](#bodyparserurlencodedoptions) | |||
Other body parsers you might be interested in: | |||
- [body](https://www.npmjs.org/package/body#readme) | |||
- [co-body](https://www.npmjs.org/package/co-body#readme) | |||
## Installation | |||
```sh | |||
$ npm install body-parser | |||
``` | |||
## API | |||
<!-- eslint-disable no-unused-vars --> | |||
```js | |||
var bodyParser = require('body-parser') | |||
``` | |||
The `bodyParser` object exposes various factories to create middlewares. All | |||
middlewares will populate the `req.body` property with the parsed body when | |||
the `Content-Type` request header matches the `type` option, or an empty | |||
object (`{}`) if there was no body to parse, the `Content-Type` was not matched, | |||
or an error occurred. | |||
The various errors returned by this module are described in the | |||
[errors section](#errors). | |||
### bodyParser.json([options]) | |||
Returns middleware that only parses `json` and only looks at requests where | |||
the `Content-Type` header matches the `type` option. This parser accepts any | |||
Unicode encoding of the body and supports automatic inflation of `gzip` and | |||
`deflate` encodings. | |||
A new `body` object containing the parsed data is populated on the `request` | |||
object after the middleware (i.e. `req.body`). | |||
#### Options | |||
The `json` function takes an optional `options` object that may contain any of | |||
the following keys: | |||
##### inflate | |||
When set to `true`, then deflated (compressed) bodies will be inflated; when | |||
`false`, deflated bodies are rejected. Defaults to `true`. | |||
##### limit | |||
Controls the maximum request body size. If this is a number, then the value | |||
specifies the number of bytes; if it is a string, the value is passed to the | |||
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults | |||
to `'100kb'`. | |||
##### reviver | |||
The `reviver` option is passed directly to `JSON.parse` as the second | |||
argument. You can find more information on this argument | |||
[in the MDN documentation about JSON.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter). | |||
##### strict | |||
When set to `true`, will only accept arrays and objects; when `false` will | |||
accept anything `JSON.parse` accepts. Defaults to `true`. | |||
##### type | |||
The `type` option is used to determine what media type the middleware will | |||
parse. This option can be a function or a string. If a string, `type` option | |||
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) | |||
library and this can be an extension name (like `json`), a mime type (like | |||
`application/json`), or a mime type with a wildcard (like `*/*` or `*/json`). | |||
If a function, the `type` option is called as `fn(req)` and the request is | |||
parsed if it returns a truthy value. Defaults to `application/json`. | |||
##### verify | |||
The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`, | |||
where `buf` is a `Buffer` of the raw request body and `encoding` is the | |||
encoding of the request. The parsing can be aborted by throwing an error. | |||
### bodyParser.raw([options]) | |||
Returns middleware that parses all bodies as a `Buffer` and only looks at | |||
requests where the `Content-Type` header matches the `type` option. This | |||
parser supports automatic inflation of `gzip` and `deflate` encodings. | |||
A new `body` object containing the parsed data is populated on the `request` | |||
object after the middleware (i.e. `req.body`). This will be a `Buffer` object | |||
of the body. | |||
#### Options | |||
The `raw` function takes an optional `options` object that may contain any of | |||
the following keys: | |||
##### inflate | |||
When set to `true`, then deflated (compressed) bodies will be inflated; when | |||
`false`, deflated bodies are rejected. Defaults to `true`. | |||
##### limit | |||
Controls the maximum request body size. If this is a number, then the value | |||
specifies the number of bytes; if it is a string, the value is passed to the | |||
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults | |||
to `'100kb'`. | |||
##### type | |||
The `type` option is used to determine what media type the middleware will | |||
parse. This option can be a function or a string. If a string, `type` option | |||
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) | |||
library and this can be an extension name (like `bin`), a mime type (like | |||
`application/octet-stream`), or a mime type with a wildcard (like `*/*` or | |||
`application/*`). If a function, the `type` option is called as `fn(req)` | |||
and the request is parsed if it returns a truthy value. Defaults to | |||
`application/octet-stream`. | |||
##### verify | |||
The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`, | |||
where `buf` is a `Buffer` of the raw request body and `encoding` is the | |||
encoding of the request. The parsing can be aborted by throwing an error. | |||
### bodyParser.text([options]) | |||
Returns middleware that parses all bodies as a string and only looks at | |||
requests where the `Content-Type` header matches the `type` option. This | |||
parser supports automatic inflation of `gzip` and `deflate` encodings. | |||
A new `body` string containing the parsed data is populated on the `request` | |||
object after the middleware (i.e. `req.body`). This will be a string of the | |||
body. | |||
#### Options | |||
The `text` function takes an optional `options` object that may contain any of | |||
the following keys: | |||
##### defaultCharset | |||
Specify the default character set for the text content if the charset is not | |||
specified in the `Content-Type` header of the request. Defaults to `utf-8`. | |||
##### inflate | |||
When set to `true`, then deflated (compressed) bodies will be inflated; when | |||
`false`, deflated bodies are rejected. Defaults to `true`. | |||
##### limit | |||
Controls the maximum request body size. If this is a number, then the value | |||
specifies the number of bytes; if it is a string, the value is passed to the | |||
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults | |||
to `'100kb'`. | |||
##### type | |||
The `type` option is used to determine what media type the middleware will | |||
parse. This option can be a function or a string. If a string, `type` option | |||
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) | |||
library and this can be an extension name (like `txt`), a mime type (like | |||
`text/plain`), or a mime type with a wildcard (like `*/*` or `text/*`). | |||
If a function, the `type` option is called as `fn(req)` and the request is | |||
parsed if it returns a truthy value. Defaults to `text/plain`. | |||
##### verify | |||
The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`, | |||
where `buf` is a `Buffer` of the raw request body and `encoding` is the | |||
encoding of the request. The parsing can be aborted by throwing an error. | |||
### bodyParser.urlencoded([options]) | |||
Returns middleware that only parses `urlencoded` bodies and only looks at | |||
requests where the `Content-Type` header matches the `type` option. This | |||
parser accepts only UTF-8 encoding of the body and supports automatic | |||
inflation of `gzip` and `deflate` encodings. | |||
A new `body` object containing the parsed data is populated on the `request` | |||
object after the middleware (i.e. `req.body`). This object will contain | |||
key-value pairs, where the value can be a string or array (when `extended` is | |||
`false`), or any type (when `extended` is `true`). | |||
#### Options | |||
The `urlencoded` function takes an optional `options` object that may contain | |||
any of the following keys: | |||
##### extended | |||
The `extended` option allows to choose between parsing the URL-encoded data | |||
with the `querystring` library (when `false`) or the `qs` library (when | |||
`true`). The "extended" syntax allows for rich objects and arrays to be | |||
encoded into the URL-encoded format, allowing for a JSON-like experience | |||
with URL-encoded. For more information, please | |||
[see the qs library](https://www.npmjs.org/package/qs#readme). | |||
Defaults to `true`, but using the default has been deprecated. Please | |||
research into the difference between `qs` and `querystring` and choose the | |||
appropriate setting. | |||
##### inflate | |||
When set to `true`, then deflated (compressed) bodies will be inflated; when | |||
`false`, deflated bodies are rejected. Defaults to `true`. | |||
##### limit | |||
Controls the maximum request body size. If this is a number, then the value | |||
specifies the number of bytes; if it is a string, the value is passed to the | |||
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults | |||
to `'100kb'`. | |||
##### parameterLimit | |||
The `parameterLimit` option controls the maximum number of parameters that | |||
are allowed in the URL-encoded data. If a request contains more parameters | |||
than this value, a 413 will be returned to the client. Defaults to `1000`. | |||
##### type | |||
The `type` option is used to determine what media type the middleware will | |||
parse. This option can be a function or a string. If a string, `type` option | |||
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) | |||
library and this can be an extension name (like `urlencoded`), a mime type (like | |||
`application/x-www-form-urlencoded`), or a mime type with a wildcard (like | |||
`*/x-www-form-urlencoded`). If a function, the `type` option is called as | |||
`fn(req)` and the request is parsed if it returns a truthy value. Defaults | |||
to `application/x-www-form-urlencoded`. | |||
##### verify | |||
The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`, | |||
where `buf` is a `Buffer` of the raw request body and `encoding` is the | |||
encoding of the request. The parsing can be aborted by throwing an error. | |||
## Errors | |||
The middlewares provided by this module create errors depending on the error | |||
condition during parsing. The errors will typically have a `status`/`statusCode` | |||
property that contains the suggested HTTP response code, an `expose` property | |||
to determine if the `message` property should be displayed to the client, a | |||
`type` property to determine the type of error without matching against the | |||
`message`, and a `body` property containing the read body, if available. | |||
The following are the common errors emitted, though any error can come through | |||
for various reasons. | |||
### content encoding unsupported | |||
This error will occur when the request had a `Content-Encoding` header that | |||
contained an encoding but the "inflation" option was set to `false`. The | |||
`status` property is set to `415`, the `type` property is set to | |||
`'encoding.unsupported'`, and the `charset` property will be set to the | |||
encoding that is unsupported. | |||
### request aborted | |||
This error will occur when the request is aborted by the client before reading | |||
the body has finished. The `received` property will be set to the number of | |||
bytes received before the request was aborted and the `expected` property is | |||
set to the number of expected bytes. The `status` property is set to `400` | |||
and `type` property is set to `'request.aborted'`. | |||
### request entity too large | |||
This error will occur when the request body's size is larger than the "limit" | |||
option. The `limit` property will be set to the byte limit and the `length` | |||
property will be set to the request body's length. The `status` property is | |||
set to `413` and the `type` property is set to `'entity.too.large'`. | |||
### request size did not match content length | |||
This error will occur when the request's length did not match the length from | |||
the `Content-Length` header. This typically occurs when the request is malformed, | |||
typically when the `Content-Length` header was calculated based on characters | |||
instead of bytes. The `status` property is set to `400` and the `type` property | |||
is set to `'request.size.invalid'`. | |||
### stream encoding should not be set | |||
This error will occur when something called the `req.setEncoding` method prior | |||
to this middleware. This module operates directly on bytes only and you cannot | |||
call `req.setEncoding` when using this module. The `status` property is set to | |||
`500` and the `type` property is set to `'stream.encoding.set'`. | |||
### too many parameters | |||
This error will occur when the content of the request exceeds the configured | |||
`parameterLimit` for the `urlencoded` parser. The `status` property is set to | |||
`413` and the `type` property is set to `'parameters.too.many'`. | |||
### unsupported charset "BOGUS" | |||
This error will occur when the request had a charset parameter in the | |||
`Content-Type` header, but the `iconv-lite` module does not support it OR the | |||
parser does not support it. The charset is contained in the message as well | |||
as in the `charset` property. The `status` property is set to `415`, the | |||
`type` property is set to `'charset.unsupported'`, and the `charset` property | |||
is set to the charset that is unsupported. | |||
### unsupported content encoding "bogus" | |||
This error will occur when the request had a `Content-Encoding` header that | |||
contained an unsupported encoding. The encoding is contained in the message | |||
as well as in the `encoding` property. The `status` property is set to `415`, | |||
the `type` property is set to `'encoding.unsupported'`, and the `encoding` | |||
property is set to the encoding that is unsupported. | |||
## Examples | |||
### Express/Connect top-level generic | |||
This example demonstrates adding a generic JSON and URL-encoded parser as a | |||
top-level middleware, which will parse the bodies of all incoming requests. | |||
This is the simplest setup. | |||
```js | |||
var express = require('express') | |||
var bodyParser = require('body-parser') | |||
var app = express() | |||
// parse application/x-www-form-urlencoded | |||
app.use(bodyParser.urlencoded({ extended: false })) | |||
// parse application/json | |||
app.use(bodyParser.json()) | |||
app.use(function (req, res) { | |||
res.setHeader('Content-Type', 'text/plain') | |||
res.write('you posted:\n') | |||
res.end(JSON.stringify(req.body, null, 2)) | |||
}) | |||
``` | |||
### Express route-specific | |||
This example demonstrates adding body parsers specifically to the routes that | |||
need them. In general, this is the most recommended way to use body-parser with | |||
Express. | |||
```js | |||
var express = require('express') | |||
var bodyParser = require('body-parser') | |||
var app = express() | |||
// create application/json parser | |||
var jsonParser = bodyParser.json() | |||
// create application/x-www-form-urlencoded parser | |||
var urlencodedParser = bodyParser.urlencoded({ extended: false }) | |||
// POST /login gets urlencoded bodies | |||
app.post('/login', urlencodedParser, function (req, res) { | |||
if (!req.body) return res.sendStatus(400) | |||
res.send('welcome, ' + req.body.username) | |||
}) | |||
// POST /api/users gets JSON bodies | |||
app.post('/api/users', jsonParser, function (req, res) { | |||
if (!req.body) return res.sendStatus(400) | |||
// create user in req.body | |||
}) | |||
``` | |||
### Change accepted type for parsers | |||
All the parsers accept a `type` option which allows you to change the | |||
`Content-Type` that the middleware will parse. | |||
```js | |||
var express = require('express') | |||
var bodyParser = require('body-parser') | |||
var app = express() | |||
// parse various different custom JSON types as JSON | |||
app.use(bodyParser.json({ type: 'application/*+json' })) | |||
// parse some custom thing into a Buffer | |||
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' })) | |||
// parse an HTML body into a string | |||
app.use(bodyParser.text({ type: 'text/html' })) | |||
``` | |||
## License | |||
[MIT](LICENSE) | |||
[npm-image]: https://img.shields.io/npm/v/body-parser.svg | |||
[npm-url]: https://npmjs.org/package/body-parser | |||
[travis-image]: https://img.shields.io/travis/expressjs/body-parser/master.svg | |||
[travis-url]: https://travis-ci.org/expressjs/body-parser | |||
[coveralls-image]: https://img.shields.io/coveralls/expressjs/body-parser/master.svg | |||
[coveralls-url]: https://coveralls.io/r/expressjs/body-parser?branch=master | |||
[downloads-image]: https://img.shields.io/npm/dm/body-parser.svg | |||
[downloads-url]: https://npmjs.org/package/body-parser | |||
[gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg | |||
[gratipay-url]: https://www.gratipay.com/dougwilson/ |
@@ -0,0 +1,157 @@ | |||
/*! | |||
* body-parser | |||
* Copyright(c) 2014-2015 Douglas Christopher Wilson | |||
* MIT Licensed | |||
*/ | |||
'use strict' | |||
/** | |||
* Module dependencies. | |||
* @private | |||
*/ | |||
var deprecate = require('depd')('body-parser') | |||
/** | |||
* Cache of loaded parsers. | |||
* @private | |||
*/ | |||
var parsers = Object.create(null) | |||
/** | |||
* @typedef Parsers | |||
* @type {function} | |||
* @property {function} json | |||
* @property {function} raw | |||
* @property {function} text | |||
* @property {function} urlencoded | |||
*/ | |||
/** | |||
* Module exports. | |||
* @type {Parsers} | |||
*/ | |||
exports = module.exports = deprecate.function(bodyParser, | |||
'bodyParser: use individual json/urlencoded middlewares') | |||
/** | |||
* JSON parser. | |||
* @public | |||
*/ | |||
Object.defineProperty(exports, 'json', { | |||
configurable: true, | |||
enumerable: true, | |||
get: createParserGetter('json') | |||
}) | |||
/** | |||
* Raw parser. | |||
* @public | |||
*/ | |||
Object.defineProperty(exports, 'raw', { | |||
configurable: true, | |||
enumerable: true, | |||
get: createParserGetter('raw') | |||
}) | |||
/** | |||
* Text parser. | |||
* @public | |||
*/ | |||
Object.defineProperty(exports, 'text', { | |||
configurable: true, | |||
enumerable: true, | |||
get: createParserGetter('text') | |||
}) | |||
/** | |||
* URL-encoded parser. | |||
* @public | |||
*/ | |||
Object.defineProperty(exports, 'urlencoded', { | |||
configurable: true, | |||
enumerable: true, | |||
get: createParserGetter('urlencoded') | |||
}) | |||
/** | |||
* Create a middleware to parse json and urlencoded bodies. | |||
* | |||
* @param {object} [options] | |||
* @return {function} | |||
* @deprecated | |||