first commit

This commit is contained in:
2026-09-08 20:28:54 +08:00
commit 2ada8d3d5a
37380 changed files with 4886169 additions and 0 deletions
+84
View File
@@ -0,0 +1,84 @@
5.2.0 / 2018-05-02
==================
**features**
* [[`f65a2d8`](http://github.com/cojs/co-body/commit/f65a2d8f7ebf4426138035af6d7e7f02272441f2)] - feat: impl text parser support encoding: false (#64) (killa <<killa123@126.com>>)
5.1.1 / 2017-03-24
==================
* fix: getOptions change to clone
* fix: ensure options are independent in each request
5.1.0 / 2017-03-21
==================
* feat: add options to support return raw body (#56)
5.0.3 / 2017-03-19
==================
* fix: ensure inflate in promise chain (#54)
5.0.2 / 2017-03-10
==================
* fix: keep compatibility with qs@4 (#53)
5.0.1 / 2017-03-06
==================
* dpes: qs@6.4.0
5.0.0 / 2017-03-02
==================
* deps: upgrade qs to 6.x (#52)
4.2.0 / 2016-05-05
==================
* test: test on node 4, 5, 6
* feat: Added support for request body inflation
4.1.0 / 2016-05-05
==================
* feat: form parse support custom qs module
4.0.0 / 2015-08-15
==================
* Switch to Promises instead of thunks
3.1.0 / 2015-08-06
==================
* travis: add v2, v3, remove 0.11
* add custom types options
* use type-is
3.0.0 / 2015-07-25
==================
* Updated dependencies. Added qs options support via queryString option key. (@yanickrochon)
* upgrade qs@4.0.0, raw-body@2.1.2
2.0.0 / 2015-05-04
==================
* json parser support strict mode
1.2.0 / 2015-04-29
==================
* Add JSON-LD as known JSON-Type (@vanthome)
1.1.0 / 2015-02-27
==================
* Fix content-length zero should not parse json
* Bump deps, qs@~2.3.3, raw-body@~1.3.3
* add support for `text/plain`
* json support for `application/json-patch+json`, `application/vnd.api+json` and `application/csp-report`
+84
View File
@@ -0,0 +1,84 @@
# co-body
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url]
[npm-image]: https://img.shields.io/npm/v/co-body.svg?style=flat-square
[npm-url]: https://npmjs.org/package/co-body
[travis-image]: https://img.shields.io/travis/cojs/co-body.svg?style=flat-square
[travis-url]: https://travis-ci.org/cojs/co-body
[coveralls-image]: https://img.shields.io/coveralls/cojs/co-body.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/cojs/co-body?branch=master
[david-image]: https://img.shields.io/david/cojs/co-body.svg?style=flat-square
[david-url]: https://david-dm.org/cojs/co-body
[download-image]: https://img.shields.io/npm/dm/co-body.svg?style=flat-square
[download-url]: https://npmjs.org/package/co-body
Parse request bodies with generators inspired by [Raynos/body](https://github.com/Raynos/body).
## Installation
```bash
$ npm install co-body
```
## Options
- `limit` number or string representing the request size limit (1mb for json and 56kb for form-urlencoded)
- `strict` when set to `true`, JSON parser will only accept arrays and objects; when `false` will accept anything `JSON.parse` accepts. Defaults to `true`. (also `strict` mode will always return object).
- `queryString` an object of options when parsing query strings and form data. See [qs](https://github.com/hapijs/qs) for more information.
- `returnRawBody` when set to `true`, the return value of `co-body` will be an object with two properties: `{ parsed: /* parsed value */, raw: /* raw body */}`.
- `jsonTypes` is used to determine what media type **co-body** will parse as **json**, this option is passed directly to the [type-is](https://github.com/jshttp/type-is) library.
- `formTypes` is used to determine what media type **co-body** will parse as **form**, this option is passed directly to the [type-is](https://github.com/jshttp/type-is) library.
- `textTypes` is used to determine what media type **co-body** will parse as **text**, this option is passed directly to the [type-is](https://github.com/jshttp/type-is) library.
more options available via [raw-body](https://github.com/stream-utils/raw-body#getrawbodystream-options-callback):
## Example
```js
// application/json
var body = yield parse.json(req);
// explicit limit
var body = yield parse.json(req, { limit: '10kb' });
// application/x-www-form-urlencoded
var body = yield parse.form(req);
// text/plain
var body = yield parse.text(req);
// either
var body = yield parse(req);
// custom type
var body = yield parse(req, { textTypes: ['text', 'html'] });
```
## Koa
This lib also supports `ctx.req` in Koa (or other libraries),
so that you may simply use `this` instead of `this.req`.
```js
// application/json
var body = yield parse.json(this);
// application/x-www-form-urlencoded
var body = yield parse.form(this);
// text/plain
var body = yield parse.text(this);
// either
var body = yield parse(this);
```
# License
MIT
+5
View File
@@ -0,0 +1,5 @@
exports = module.exports = require('./lib/any');
exports.json = require('./lib/json');
exports.form = require('./lib/form');
exports.text = require('./lib/text');
+50
View File
@@ -0,0 +1,50 @@
/**
* Module dependencies.
*/
var typeis = require('type-is');
var json = require('./json');
var form = require('./form');
var text = require('./text');
var jsonTypes = ['json', 'application/*+json', 'application/csp-report'];
var formTypes = ['urlencoded'];
var textTypes = ['text'];
/**
* Return a Promise which parses form and json requests
* depending on the Content-Type.
*
* Pass a node request or an object with `.req`,
* such as a koa Context.
*
* @param {Request} req
* @param {Options} [opts]
* @return {Function}
* @api public
*/
module.exports = function(req, opts){
req = req.req || req;
opts = opts || {};
// json
var jsonType = opts.jsonTypes || jsonTypes;
if (typeis(req, jsonType)) return json(req, opts);
// form
var formType = opts.formTypes || formTypes;
if (typeis(req, formType)) return form(req, opts);
// text
var textType = opts.textTypes || textTypes;
if (typeis(req, textType)) return text(req, opts);
// invalid
var type = req.headers['content-type'] || '';
var message = type ? 'Unsupported content-type: ' + type : 'Missing content-type';
var err = new Error(message);
err.status = 415;
return Promise.reject(err);
};
+54
View File
@@ -0,0 +1,54 @@
/**
* Module dependencies.
*/
var raw = require('raw-body');
var inflate = require('inflation');
var qs = require('qs');
var utils = require('./utils');
/**
* Return a Promise which parses x-www-form-urlencoded requests.
*
* Pass a node request or an object with `.req`,
* such as a koa Context.
*
* @param {Request} req
* @param {Options} [opts]
* @return {Function}
* @api public
*/
module.exports = function(req, opts){
req = req.req || req;
opts = utils.clone(opts);
var queryString = opts.queryString || {};
// keep compatibility with qs@4
if (queryString.allowDots === undefined) queryString.allowDots = true;
// defaults
var len = req.headers['content-length'];
var encoding = req.headers['content-encoding'] || 'identity';
if (len && encoding === 'identity') opts.length = ~~len;
opts.encoding = opts.encoding || 'utf8';
opts.limit = opts.limit || '56kb';
opts.qs = opts.qs || qs;
// raw-body returns a Promise when no callback is specified
return Promise.resolve()
.then(function() {
return raw(inflate(req), opts);
})
.then(function(str){
try {
var parsed = opts.qs.parse(str, queryString);
return opts.returnRawBody ? { parsed: parsed, raw: str } : parsed;
} catch (err) {
err.status = 400;
err.body = str;
throw err;
}
});
};
+64
View File
@@ -0,0 +1,64 @@
/**
* Module dependencies.
*/
var raw = require('raw-body');
var inflate = require('inflation');
var utils = require('./utils');
// Allowed whitespace is defined in RFC 7159
// http://www.rfc-editor.org/rfc/rfc7159.txt
var strictJSONReg = /^[\x20\x09\x0a\x0d]*(\[|\{)/;
/**
* Return a Promise which parses json requests.
*
* Pass a node request or an object with `.req`,
* such as a koa Context.
*
* @param {Request} req
* @param {Options} [opts]
* @return {Function}
* @api public
*/
module.exports = function(req, opts){
req = req.req || req;
opts = utils.clone(opts);
// defaults
var len = req.headers['content-length'];
var encoding = req.headers['content-encoding'] || 'identity';
if (len && encoding === 'identity') opts.length = len = ~~len;
opts.encoding = opts.encoding || 'utf8';
opts.limit = opts.limit || '1mb';
var strict = opts.strict !== false;
// raw-body returns a promise when no callback is specified
return Promise.resolve()
.then(function() {
return raw(inflate(req), opts);
})
.then(function(str) {
try {
var parsed = parse(str);
return opts.returnRawBody ? { parsed: parsed, raw: str } : parsed;
} catch (err) {
err.status = 400;
err.body = str;
throw err;
}
});
function parse(str){
if (!strict) return str ? JSON.parse(str) : str;
// strict mode always return object
if (!str) return {};
// strict JSON test
if (!strictJSONReg.test(str)) {
throw new Error('invalid JSON, only supports object and array');
}
return JSON.parse(str);
}
};
+41
View File
@@ -0,0 +1,41 @@
/**
* Module dependencies.
*/
var raw = require('raw-body');
var inflate = require('inflation');
var utils = require('./utils');
/**
* Return a Promise which parses text/plain requests.
*
* Pass a node request or an object with `.req`,
* such as a koa Context.
*
* @param {Request} req
* @param {Options} [opts]
* @return {Function}
* @api public
*/
module.exports = function(req, opts){
req = req.req || req;
opts = utils.clone(opts);
// defaults
var len = req.headers['content-length'];
var encoding = req.headers['content-encoding'] || 'identity';
if (len && encoding === 'identity') opts.length = ~~len;
opts.encoding = opts.encoding === undefined ? 'utf8': opts.encoding;
opts.limit = opts.limit || '1mb';
// raw-body returns a Promise when no callback is specified
return Promise.resolve()
.then(function() {
return raw(inflate(req), opts);
})
.then(str => {
// ensure return the same format with json / form
return opts.returnRawBody ? { parsed: str, raw: str } : str;
});
};
+13
View File
@@ -0,0 +1,13 @@
/**
* Module dependencies.
*/
exports.clone = function (opts) {
var options = {};
opts = opts || {};
for (var key in opts) {
options[key] = opts[key];
}
return options;
}
+38
View File
@@ -0,0 +1,38 @@
{
"name": "co-body",
"version": "5.2.0",
"repository": "cojs/co-body",
"description": "request body parsing for co",
"keywords": [
"request",
"parse",
"parser",
"json",
"co",
"generators",
"urlencoded"
],
"dependencies": {
"inflation": "^2.0.0",
"qs": "^6.4.0",
"raw-body": "^2.2.0",
"type-is": "^1.6.14"
},
"devDependencies": {
"istanbul": "^0.4.5",
"koa": "^1.2.5",
"mocha": "^3.2.0",
"safe-qs": "^6.0.1",
"should": "^11.2.0",
"supertest": "^1.0.1"
},
"license": "MIT",
"scripts": {
"test": "make test",
"test-cov": "make test-cov"
},
"files": [
"index.js",
"lib/"
]
}