UNPKG

838 BJavaScriptView Raw
1/**
2 * Module dependencies.
3 */
4
5var raw = require('raw-body');
6var inflate = require('inflation');
7
8/**
9 * Return a Promise which parses text/plain requests.
10 *
11 * Pass a node request or an object with `.req`,
12 * such as a koa Context.
13 *
14 * @param {Request} req
15 * @param {Options} [opts]
16 * @return {Function}
17 * @api public
18 */
19
20module.exports = function(req, opts){
21 req = req.req || req;
22 opts = opts || {};
23
24 // defaults
25 var len = req.headers['content-length'];
26 var encoding = req.headers['content-encoding'] || 'identity';
27 if (len && encoding === 'identity') opts.length = ~~len;
28 opts.encoding = opts.encoding || 'utf8';
29 opts.limit = opts.limit || '1mb';
30
31 // raw-body returns a Promise when no callback is specified
32 return Promise.resolve()
33 .then(function() {
34 return raw(inflate(req), opts);
35 });
36};