text.js 776 B

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