ultimate-express 1.3.8 → 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.
- package/package.json +1 -1
- package/src/declarative.js +22 -1
package/package.json
CHANGED
package/src/declarative.js
CHANGED
|
@@ -6,6 +6,11 @@ const parser = acorn.Parser;
|
|
|
6
6
|
|
|
7
7
|
const allowedResMethods = ['set', 'header', 'setHeader', 'status', 'send', 'end', 'append'];
|
|
8
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
|
+
}
|
|
9
14
|
|
|
10
15
|
// generates a declarative response from a callback
|
|
11
16
|
// uWS allows creating such responses and they are extremely fast
|
|
@@ -275,10 +280,15 @@ module.exports = function compileDeclarative(cb, app) {
|
|
|
275
280
|
body.push(...stuff.reverse());
|
|
276
281
|
} else if(arg.type === 'ObjectExpression') {
|
|
277
282
|
// only simple objects can be optimized
|
|
283
|
+
let objCode = code;
|
|
278
284
|
for(let property of arg.properties) {
|
|
279
285
|
if(property.key.type !== 'Identifier' && property.key.type !== 'Literal') {
|
|
280
286
|
return false;
|
|
281
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
|
+
}
|
|
282
292
|
if(property.value.type !== 'Literal') {
|
|
283
293
|
return false;
|
|
284
294
|
}
|
|
@@ -286,7 +296,18 @@ module.exports = function compileDeclarative(cb, app) {
|
|
|
286
296
|
if(typeof app.get('json replacer') !== 'undefined' && typeof app.get('json replacer') !== 'string') {
|
|
287
297
|
return false;
|
|
288
298
|
}
|
|
289
|
-
|
|
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
|
+
});
|
|
290
311
|
} else {
|
|
291
312
|
return false;
|
|
292
313
|
}
|