json.js 1.6 KB

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