ultimate-express 1.2.2 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -136,7 +136,8 @@ In general, basically all features and options are supported. Use [Express 4.x d
136
136
 
137
137
  - ✅ express()
138
138
  - ✅ express.Router()
139
- - express.json()
139
+ - 🚧 express.json()
140
+ - - ❌ options.inflate
140
141
  - ✅ express.urlencoded()
141
142
  - ✅ express.static()
142
143
  - - Additionally you can pass `options.ifModifiedSince` to support If-Modified-Since header (this header is not supported in normal Express, but is supported in µExpress)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ultimate-express",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "description": "The Ultimate Express. Fastest http server with full Express compatibility, based on uWebSockets.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -16,6 +16,7 @@ limitations under the License.
16
16
 
17
17
  const fs = require('fs');
18
18
  const path = require('path');
19
+ const bytes = require('bytes');
19
20
 
20
21
  function static(root, options) {
21
22
  if(!options) options = {};
@@ -108,6 +109,68 @@ function static(root, options) {
108
109
  }
109
110
  }
110
111
 
112
+ function json(options = {}) {
113
+ if(typeof options !== 'object') {
114
+ options = {};
115
+ }
116
+ if(typeof options.limit === 'undefined') options.limit = bytes('100kb');
117
+ else options.limit = bytes(options.limit);
118
+
119
+ if(typeof options.type === 'undefined') options.type = 'application/json';
120
+ else if(typeof options.type !== 'string') {
121
+ throw new Error('type must be a string');
122
+ }
123
+
124
+ return (req, res, next) => {
125
+ const type = req.headers['content-type'];
126
+ const semiColonIndex = type.indexOf(';');
127
+ const contentType = semiColonIndex !== -1 ? type.substring(0, semiColonIndex) : type;
128
+ if(!type || contentType !== options.type) {
129
+ return next();
130
+ }
131
+
132
+ // skip reading body for non-POST requests
133
+ // this makes it +10k req/sec faster
134
+ const additionalMethods = req.app.get('body methods');
135
+ if(
136
+ req.method !== 'POST' &&
137
+ req.method !== 'PUT' &&
138
+ req.method !== 'PATCH' &&
139
+ (!additionalMethods || !additionalMethods.includes(req.method))
140
+ ) {
141
+ return next();
142
+ }
143
+
144
+ const abs = [], totalSize = 0;
145
+ req._res.onData((ab, isLast) => {
146
+ abs.push(ab);
147
+ totalSize += ab.length;
148
+ if(totalSize > options.limit) {
149
+ return next(new Error('Request entity too large'));
150
+ }
151
+ if(isLast) {
152
+ const buf = Buffer.concat(abs);
153
+ if(options.verify) {
154
+ try {
155
+ options.verify(req, res, buf);
156
+ } catch(e) {
157
+ return next(e);
158
+ }
159
+ }
160
+ req.body = JSON.parse(buf, options.reviver);
161
+ if(options.strict) {
162
+ if(req.body && typeof req.body !== 'object') {
163
+ return next(new Error('Invalid body'));
164
+ }
165
+ }
166
+ next();
167
+ }
168
+ });
169
+
170
+ }
171
+
172
+ }
173
+
111
174
  module.exports = {
112
175
  static
113
176
  };