ultimate-express 1.2.2 → 1.2.4

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.4",
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": {
package/src/index.js CHANGED
@@ -17,7 +17,7 @@ limitations under the License.
17
17
  const Application = require("./application.js");
18
18
  const Router = require("./router.js");
19
19
  const bodyParser = require("body-parser");
20
- const { static } = require("./middlewares.js");
20
+ const { static, json } = require("./middlewares.js");
21
21
  const Request = require("./request.js");
22
22
  const Response = require("./response.js");
23
23
 
@@ -30,7 +30,7 @@ Application.response = Response.prototype;
30
30
 
31
31
  Application.static = static;
32
32
 
33
- Application.json = bodyParser.json;
33
+ Application.json = json;
34
34
  Application.urlencoded = bodyParser.urlencoded;
35
35
  Application.text = bodyParser.text;
36
36
  Application.raw = bodyParser.raw;
@@ -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,74 @@ 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
+ // skip reading body twice
132
+ if(req.body) {
133
+ return next();
134
+ }
135
+
136
+ // skip reading body for non-POST requests
137
+ // this makes it +10k req/sec faster
138
+ const additionalMethods = req.app.get('body methods');
139
+ if(
140
+ req.method !== 'POST' &&
141
+ req.method !== 'PUT' &&
142
+ req.method !== 'PATCH' &&
143
+ (!additionalMethods || !additionalMethods.includes(req.method))
144
+ ) {
145
+ return next();
146
+ }
147
+
148
+ const abs = []
149
+ let totalSize = 0;
150
+ req._res.onData((ab, isLast) => {
151
+ abs.push(Buffer.from(ab));
152
+ totalSize += ab.length;
153
+ if(totalSize > options.limit) {
154
+ return next(new Error('Request entity too large'));
155
+ }
156
+ if(isLast) {
157
+ const buf = Buffer.concat(abs);
158
+ if(options.verify) {
159
+ try {
160
+ options.verify(req, res, buf);
161
+ } catch(e) {
162
+ return next(e);
163
+ }
164
+ }
165
+ req.body = JSON.parse(buf, options.reviver);
166
+ if(options.strict) {
167
+ if(req.body && typeof req.body !== 'object') {
168
+ return next(new Error('Invalid body'));
169
+ }
170
+ }
171
+ next();
172
+ }
173
+ });
174
+
175
+ }
176
+
177
+ }
178
+
111
179
  module.exports = {
112
- static
180
+ static,
181
+ json
113
182
  };