function normalizeParams()

in index.js [207:254]


function normalizeParams (params, callback) {
  const normalized = {
    method: params.method,
    path: params.path,
    body: null,
    // querystring.parse returns a null object prototype
    // which break the fast-deep-equal algorithm
    querystring: { ...querystring.parse(params.querystring) }
  }

  const compression = (params.headers['Content-Encoding'] || params.headers['content-encoding']) === 'gzip'
  const type = params.headers['Content-Type'] || params.headers['content-type'] || ''

  if (isStream(params.body)) {
    normalized.body = ''
    const stream = compression ? params.body.pipe(createGunzip()) : params.body
    /* istanbul ignore next */
    stream.on('error', err => callback(err, null))
    stream.on('data', chunk => { normalized.body += chunk })
    stream.on('end', () => {
      normalized.body = type.includes('x-ndjson')
        ? normalized.body.split(/\n|\n\r/).filter(Boolean).map(l => JSON.parse(l))
        : JSON.parse(normalized.body)
      callback(null, normalized)
    })
  } else if (params.body) {
    if (compression) {
      gunzip(params.body, (err, buffer) => {
        /* istanbul ignore next */
        if (err) {
          return callback(err, null)
        }
        buffer = buffer.toString()
        normalized.body = type.includes('x-ndjson')
          ? buffer.split(/\n|\n\r/).filter(Boolean).map(l => JSON.parse(l))
          : JSON.parse(buffer)
        callback(null, normalized)
      })
    } else {
      normalized.body = type.includes('x-ndjson')
        ? params.body.split(/\n|\n\r/).filter(Boolean).map(l => JSON.parse(l))
        : JSON.parse(params.body)
      setImmediate(callback, null, normalized)
    }
  } else {
    setImmediate(callback, null, normalized)
  }
}