-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (34 loc) · 1019 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Dependencies.
*/
const inflate = require('inflate-body')
/**
* Return a promise that resolves when x-www-form-urlencoded request is parsed.
* Reject promise with 400 status error otherwise.
*
* @param {HttpIncomingMessage} request
* @param {Object} options (folowing arguments are mixed with options)
* @return {Promise}
* @api public
*/
module.exports = function (request, options, ...args) {
return inflate(request, setup(request, options, ...args))
}
/**
* Clone and setup default options.
*
* @param {HttpIncomingMessage} request
* @param {Object} options
* @param {Object} mixin
* @return {Object}
* @api private
*/
function setup (request, options, ...args) {
const len = request.headers['content-length']
const encoding = request.headers['content-encoding'] || 'identity'
return Object.assign({}, options, {
encoding: options.encoding || 'utf-8',
limit: options.limit || '1mb',
length: (len && encoding === 'identity') ? ~~len : options.length
}, ...args)
}