UNPKG

toxy

Version:

Hackable HTTP proxy to simulate server failure scenarios and network conditions

47 lines (35 loc) 991 B
const getRawBody = require('raw-body') const matchBody = require('../helpers').matchBody module.exports = function body (opts) { opts = opts || {} const match = opts.match const limit = opts.limit || '5mb' const encoding = opts.encoding || 'utf8' return function body (req, res, next) { if (req.method === 'GET' || req.method === 'HEAD') { return next(null, true) } if (req.body) { return handleBody(null, req.body) } const bodyOpts = { limit: limit, encoding: encoding, length: getLength(req) } getRawBody(req, bodyOpts, handleBody) function handleBody (err, body) { if (err) return next(err) // Expose cached body in the request to forward it req.body = body if (typeof body !== 'string') { return next(null, true) } next(null, !matchBody(body, match)) } } function getLength (req) { return +opts.length || +req.headers['content-length'] } }