ultimate-express 1.2.3 → 1.2.5

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
@@ -47,7 +47,7 @@ Tested using [bun-http-framework-benchmark](https://github.com/dimdenGD/bun-http
47
47
  | elysia | bun | 72,112.447 | 82,589.71 | 69,356.08 | 64,391.55 |
48
48
  | hyper-express | node | 66,356.707 | 80,002.53 | 69,953.76 | 49,113.83 |
49
49
  | hono | bun | 63,944.627 | 74,550.47 | 62,810.28 | 54,473.13 |
50
- | **ultimate-express** | **node** | **44,081.737** | **51,753.24** | **48,389.84** | **32,102.13** |
50
+ | **ultimate-express** | **node** | **46,139.797** | **49,010.91** | **49,197.87** | **40,210.61** |
51
51
  | oak | deno | 40,878.467 | 68,429.24 | 28,541.99 | 25,664.17 |
52
52
  | express | bun | 35,937.977 | 41,329.97 | 34,339.79 | 32,144.17 |
53
53
  | h3 | node | 35,423.263 | 41,243.68 | 34,429.26 | 30,596.85 |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ultimate-express",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
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;
@@ -128,6 +128,10 @@ function json(options = {}) {
128
128
  if(!type || contentType !== options.type) {
129
129
  return next();
130
130
  }
131
+ // skip reading body twice
132
+ if(req.body) {
133
+ return next();
134
+ }
131
135
 
132
136
  // skip reading body for non-POST requests
133
137
  // this makes it +10k req/sec faster
@@ -141,36 +145,55 @@ function json(options = {}) {
141
145
  return next();
142
146
  }
143
147
 
144
- const abs = [], totalSize = 0;
145
- req._res.onData((ab, isLast) => {
146
- abs.push(ab);
148
+ const abs = []
149
+ let totalSize = 0;
150
+
151
+ function onData(ab) {
152
+ abs.push(Buffer.from(ab));
147
153
  totalSize += ab.length;
148
154
  if(totalSize > options.limit) {
149
155
  return next(new Error('Request entity too large'));
150
156
  }
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
- }
157
+ }
158
+
159
+ function onEnd() {
160
+ const buf = Buffer.concat(abs);
161
+ if(options.verify) {
162
+ try {
163
+ options.verify(req, res, buf);
164
+ } catch(e) {
165
+ return next(e);
159
166
  }
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
- }
167
+ }
168
+ req.body = JSON.parse(buf, options.reviver);
169
+ if(options.strict) {
170
+ if(req.body && typeof req.body !== 'object') {
171
+ return next(new Error('Invalid body'));
165
172
  }
166
- next();
167
173
  }
168
- });
174
+ next();
175
+ }
176
+
177
+ if(!req.receivedData) {
178
+ // reading data directly from uWS is faster than from a stream
179
+ // if we are fast enough (not async), we can do it
180
+ // otherwise we need to use a stream since it already started streaming it
181
+ req._res.onData((ab, isLast) => {
182
+ onData(ab);
183
+ if(isLast) {
184
+ onEnd();
185
+ }
186
+ });
187
+ } else {
188
+ req.on('data', onData);
189
+ req.on('end', onEnd);
190
+ }
169
191
 
170
192
  }
171
193
 
172
194
  }
173
195
 
174
196
  module.exports = {
175
- static
197
+ static,
198
+ json
176
199
  };