json.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. /**
  3. * Module dependencies.
  4. */
  5. const raw = require('raw-body');
  6. const inflate = require('inflation');
  7. const utils = require('./utils');
  8. // Allowed whitespace is defined in RFC 7159
  9. // http://www.rfc-editor.org/rfc/rfc7159.txt
  10. const strictJSONReg = /^[\x20\x09\x0a\x0d]*(\[|\{)/;
  11. /**
  12. * Return a Promise which parses json requests.
  13. *
  14. * Pass a node request or an object with `.req`,
  15. * such as a koa Context.
  16. *
  17. * @param {Request} req
  18. * @param {Options} [opts]
  19. * @return {Function}
  20. * @api public
  21. */
  22. module.exports = async function(req, opts) {
  23. req = req.req || req;
  24. opts = utils.clone(opts);
  25. // defaults
  26. const len = req.headers['content-length'];
  27. const encoding = req.headers['content-encoding'] || 'identity';
  28. if (len && encoding === 'identity') opts.length = ~~len;
  29. opts.encoding = opts.encoding || 'utf8';
  30. opts.limit = opts.limit || '1mb';
  31. const strict = opts.strict !== false;
  32. const str = await raw(inflate(req), opts);
  33. try {
  34. const parsed = parse(str);
  35. return opts.returnRawBody ? { parsed, raw: str } : parsed;
  36. } catch (err) {
  37. err.status = 400;
  38. err.body = str;
  39. throw err;
  40. }
  41. function parse(str) {
  42. if (!strict) return str ? JSON.parse(str) : str;
  43. // strict mode always return object
  44. if (!str) return {};
  45. // strict JSON test
  46. if (!strictJSONReg.test(str)) {
  47. throw new SyntaxError('invalid JSON, only supports object and array');
  48. }
  49. return JSON.parse(str);
  50. }
  51. };