Skip to content

Commit

Permalink
feat: Add more debug messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasdao committed Jan 28, 2018
1 parent efcb8c3 commit c54d1d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,9 @@ const processEvent = (req, res, config={}, endpoints=[], handlers=[], preEvent,
// 3.3. Extract all params from that request, including both the url route params and the payload params.
const validParamsMode = PARAMSMODE[paramsMode] ? paramsMode : 'all'
const paramts = validParamsMode == 'all' || validParamsMode == 'route' ? Object.assign({}, endpoint.winningRoute.parameters) : {}
const getParams = validParamsMode == 'all' || validParamsMode == 'body' ? reqUtil.getParams(req) : Promise.resolve({})
const getParams = validParamsMode == 'all' || validParamsMode == 'body'
? reqUtil.getParams(req, (...args) => debug(config, ...args))
: Promise.resolve({})
debug(config, `Extracting paramaters from the request object (mode: ${validParamsMode}).`)
return getParams.then(parameters => Object.assign(parameters, paramts))
.then(parameters => {
Expand Down
15 changes: 14 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,32 @@ const HEX_BOUNDARY_TRAIL = utf8ToHex('--')
const HEX_DBL_CAR_RTN = utf8ToHex('\r\n\r\n')
const HEX_REGEX_TRAIL_CAR_RTN = new RegExp(HEX_CAR_RTN_02 + '$')

const getParams = req => {
const getParams = (req, debugFn) => {
const debug = debugFn || (() => null)
const headers = req.headers || []
const contentType = (headers['content-type'] || headers['Content-Type'] || headers['ContentType'] || '')
const isMultipart = contentType.match(/form-data|multipart/)
const isXwwwFormUrlencoded = contentType.indexOf('x-www-form-urlencoded') >= 0
debug(`Extracting request's body (contentType: ${contentType}).`)
const getBody = !isMultipart && req.body ? Promise.resolve({ encoded: false, body: req.body}) : getRawBody(req).then(b => ({ encoded: true, body: b}))
return getBody.then(bod => {
debug('Request\'s body extracted.')
let bodyParameters = {}
if (bod.body) {
// Deal with standard encoding
if (!isMultipart) {
debug('Processing a non-multipart body.')
const body = bod.encoded ? bod.body.toString() : bod.body
const bodyType = typeof(body)
if (bodyType == 'object') {
debug('The body is a native JSON object. Simply assign it to \'req.body\'.')
bodyParameters = Object.assign({ body: body }, body)
req.body = body
}
else if (bodyType == 'string') {
try {
if (isXwwwFormUrlencoded) {
debug('The body is a x-www-form-urlencoded string. Trying to parse it to a JSON object before assigning it to \'req.body\'.')
bodyParameters = body.split('&').filter(x => x).reduce((acc,x) => {
const [k,v] = x.split('=')
const key = k ? unescape(k) : null
Expand All @@ -54,19 +60,22 @@ const getParams = req => {
bodyParameters.body = body
}
else{
debug('The body seems like a string JSON. Trying to parse it to a JSON object before assigning it to \'req.body\'.')
const parsedBody = JSON.parse(body)
bodyParameters = Object.assign({ body: bod.body }, JSON.parse(body))
req.body = parsedBody
}
}
catch(err) {
debug('Failed to parse the body to a JSON object. Assigning the raw version to \'req.body\'.')
bodyParameters = { body: bod.body }
req.body = bod.body
}
}
}
// Deal with multipart encoding
else {
debug('Processing a multipart body.')
const bodyHex = bod.body.toString('hex')
const boundary = '--' + (contentType.split('boundary=') || [null, ''])[1]
const boundaryHex = utf8ToHex(boundary)
Expand All @@ -85,11 +94,15 @@ const getParams = req => {
return acc
}, {})

debug('Parsing the different fields into a JSON object before assigning it to \'req.body\'.')
req.body = Object.assign({}, bodyParameters)
bodyParameters.body = bod.body
}
}
else
debug('There was no body in the request.')

debug('Merging the body parameters with the potential query string parameters.')
const parameters = Object.assign((bodyParameters || {}), req.query || {})

return parameters
Expand Down

0 comments on commit c54d1d0

Please sign in to comment.