1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | exports.parseBody = parseBody;
|
18 |
|
19 | var _contentType = require('content-type');
|
20 |
|
21 | var _contentType2 = _interopRequireDefault(_contentType);
|
22 |
|
23 | var _rawBody = require('raw-body');
|
24 |
|
25 | var _rawBody2 = _interopRequireDefault(_rawBody);
|
26 |
|
27 | var _httpErrors = require('http-errors');
|
28 |
|
29 | var _httpErrors2 = _interopRequireDefault(_httpErrors);
|
30 |
|
31 | var _querystring = require('querystring');
|
32 |
|
33 | var _querystring2 = _interopRequireDefault(_querystring);
|
34 |
|
35 | var _zlib = require('zlib');
|
36 |
|
37 | var _zlib2 = _interopRequireDefault(_zlib);
|
38 |
|
39 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function parseBody(req) {
|
46 | return new Promise(function (resolve, reject) {
|
47 | var body = req.body;
|
48 |
|
49 |
|
50 | if ((typeof body === 'undefined' ? 'undefined' : _typeof(body)) === 'object' && !(body instanceof Buffer)) {
|
51 | return resolve(body);
|
52 | }
|
53 |
|
54 |
|
55 | if (req.headers['content-type'] === undefined) {
|
56 | return resolve({});
|
57 | }
|
58 |
|
59 | var typeInfo = _contentType2.default.parse(req);
|
60 |
|
61 |
|
62 |
|
63 | if (typeof body === 'string' && typeInfo.type === 'application/graphql') {
|
64 | return resolve(graphqlParser(body));
|
65 | }
|
66 |
|
67 |
|
68 | if (body) {
|
69 | return resolve({});
|
70 | }
|
71 |
|
72 |
|
73 | switch (typeInfo.type) {
|
74 | case 'application/graphql':
|
75 | return read(req, typeInfo, graphqlParser, resolve, reject);
|
76 | case 'application/json':
|
77 | return read(req, typeInfo, jsonEncodedParser, resolve, reject);
|
78 | case 'application/x-www-form-urlencoded':
|
79 | return read(req, typeInfo, urlEncodedParser, resolve, reject);
|
80 | }
|
81 |
|
82 |
|
83 | return resolve({});
|
84 | });
|
85 | }
|
86 |
|
87 | function jsonEncodedParser(body) {
|
88 | if (jsonObjRegex.test(body)) {
|
89 |
|
90 | try {
|
91 | return JSON.parse(body);
|
92 | } catch (error) {}
|
93 |
|
94 |
|
95 |
|
96 | }
|
97 | throw (0, _httpErrors2.default)(400, 'POST body sent invalid JSON.');
|
98 | }
|
99 |
|
100 | function urlEncodedParser(body) {
|
101 | return _querystring2.default.parse(body);
|
102 | }
|
103 |
|
104 | function graphqlParser(body) {
|
105 | return { query: body };
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | var jsonObjRegex = /^[\x20\x09\x0a\x0d]*\{/;
|
118 |
|
119 |
|
120 | function read(req, typeInfo, parseFn, resolve, reject) {
|
121 | var charset = (typeInfo.parameters.charset || 'utf-8').toLowerCase();
|
122 |
|
123 |
|
124 | if (charset.slice(0, 4) !== 'utf-') {
|
125 | throw (0, _httpErrors2.default)(415, 'Unsupported charset "' + charset.toUpperCase() + '".');
|
126 | }
|
127 |
|
128 |
|
129 | var contentEncoding = req.headers['content-encoding'];
|
130 | var encoding = typeof contentEncoding === 'string' ? contentEncoding.toLowerCase() : 'identity';
|
131 | var length = encoding === 'identity' ? req.headers['content-length'] : null;
|
132 | var limit = 100 * 1024;
|
133 | var stream = decompressed(req, encoding);
|
134 |
|
135 |
|
136 | (0, _rawBody2.default)(stream, { encoding: charset, length: length, limit: limit }, function (err, body) {
|
137 | if (err) {
|
138 | return reject(err.type === 'encoding.unsupported' ? (0, _httpErrors2.default)(415, 'Unsupported charset "' + charset.toUpperCase() + '".') : (0, _httpErrors2.default)(400, 'Invalid body: ' + err.message + '.'));
|
139 | }
|
140 |
|
141 | try {
|
142 |
|
143 | return resolve(parseFn(body));
|
144 | } catch (error) {
|
145 | return reject(error);
|
146 | }
|
147 | });
|
148 | }
|
149 |
|
150 |
|
151 | function decompressed(req, encoding) {
|
152 | switch (encoding) {
|
153 | case 'identity':
|
154 | return req;
|
155 | case 'deflate':
|
156 | return req.pipe(_zlib2.default.createInflate());
|
157 | case 'gzip':
|
158 | return req.pipe(_zlib2.default.createGunzip());
|
159 | }
|
160 | throw (0, _httpErrors2.default)(415, 'Unsupported content-encoding "' + encoding + '".');
|
161 | } |
\ | No newline at end of file |