ultimate-express 1.3.7 → 1.3.9

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/declarative.js +36 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ultimate-express",
3
- "version": "1.3.7",
3
+ "version": "1.3.9",
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": {
@@ -1,10 +1,16 @@
1
1
  const acorn = require("acorn");
2
+ const { stringify } = require("./utils.js");
2
3
  const uWS = require("uWebSockets.js");
3
4
 
4
5
  const parser = acorn.Parser;
5
6
 
6
7
  const allowedResMethods = ['set', 'header', 'setHeader', 'status', 'send', 'end', 'append'];
7
8
  const allowedIdentifiers = ['query', 'params', ...allowedResMethods];
9
+ const objKeyRegex = /[\s{\n]([A-Za-z-0-9_]+)(\s|\n)*?:/g;
10
+
11
+ function replaceSingleCharacter(str, index, char) {
12
+ return str.slice(0, index) + char + str.slice(index + 1);
13
+ }
8
14
 
9
15
  // generates a declarative response from a callback
10
16
  // uWS allows creating such responses and they are extremely fast
@@ -272,6 +278,36 @@ module.exports = function compileDeclarative(cb, app) {
272
278
  return false;
273
279
  }
274
280
  body.push(...stuff.reverse());
281
+ } else if(arg.type === 'ObjectExpression') {
282
+ // only simple objects can be optimized
283
+ let objCode = code;
284
+ for(let property of arg.properties) {
285
+ if(property.key.type !== 'Identifier' && property.key.type !== 'Literal') {
286
+ return false;
287
+ }
288
+ if(property.value.raw.startsWith("'") && property.value.raw.endsWith("'") && !property.value.value.includes("'")) {
289
+ objCode = replaceSingleCharacter(objCode, property.value.start, '"');
290
+ objCode = replaceSingleCharacter(objCode, property.value.end - 1, '"');
291
+ }
292
+ if(property.value.type !== 'Literal') {
293
+ return false;
294
+ }
295
+ }
296
+ if(typeof app.get('json replacer') !== 'undefined' && typeof app.get('json replacer') !== 'string') {
297
+ return false;
298
+ }
299
+
300
+ headers.push(['content-type', 'application/json; charset=utf-8']);
301
+ body.push({
302
+ type: 'text',
303
+ value:
304
+ stringify(
305
+ JSON.parse(objCode.slice(arg.start, arg.end).replace(objKeyRegex, '"$1":')),
306
+ app.get('json replacer'),
307
+ app.get('json spaces'),
308
+ app.get('json escape')
309
+ )
310
+ });
275
311
  } else {
276
312
  return false;
277
313
  }