common-rod 1.5.3 → 1.5.7

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/index.js CHANGED
@@ -136,6 +136,7 @@ function _init(callback, commonRodOpt={mongo_create_connect:true, detaillog_add_
136
136
  app.use(bodyParser.json({limit: envCommonRod.body_parser_limit}));
137
137
  app.use(bodyParser.text({type:"text/*", limit: envCommonRod.body_parser_limit}));
138
138
 
139
+
139
140
  //obtainSession + assign invoke
140
141
  app.use(function obtainSession(req, res, next) {
141
142
  if(! logger.isMetrics(req) ){
@@ -159,7 +160,9 @@ function _init(callback, commonRodOpt={mongo_create_connect:true, detaillog_add_
159
160
  // })
160
161
 
161
162
  //enqueue
162
- app.use( q.nq );
163
+ // app.use(function queueMessage(req, res, next){
164
+ // q.nq(req,res,next,appRodSession);
165
+ // });
163
166
 
164
167
  //init before app.utils().app().const($name)
165
168
  appConf.static = require( currentAppPath + '/conf/config.json' );
@@ -393,7 +396,6 @@ function _init(callback, commonRodOpt={mongo_create_connect:true, detaillog_add_
393
396
  req.rodSession.error(req.sessionID,e);
394
397
  }
395
398
  };
396
-
397
399
  if(commonRodOpt.detaillog_add_output_response ||
398
400
  commonRodOpt.summarylog_auto_end ||
399
401
  req.rodSession._cbWaitFinished ){
@@ -423,13 +425,29 @@ function _init(callback, commonRodOpt={mongo_create_connect:true, detaillog_add_
423
425
  if( logger.isDebug() ){
424
426
  maskCredential.incoming_request(ctrlFile.cmd, req, logger);
425
427
  // logger.writeApplogIncoming(req.sessionID, req);
426
- }
427
- ctrlFile.ctrlFile.call(req.rodSession, req, res, next);
428
+ }
429
+ try {
430
+ let fc = ctrlFile.ctrlFile.call(req.rodSession, req, res, next);
431
+ Promise.resolve(fc).then(function(value) {
432
+ if(value instanceof Error){
433
+ next(value);
434
+ }
435
+ }).catch(function(err) {
436
+ next(err);
437
+ });
438
+ } catch (err) {
439
+ next(err)
440
+ }
428
441
  }
429
442
 
430
443
  logger.info("common-rod", `URL Mapping ${ctrlFile.method} ${ctrlFile.url} => ${ctrlFile.path}`);
431
444
  // app[ctrlFile.method](ctrlFile.url, ctrlFile.ctrlFile);
432
- app[ctrlFile.method](ctrlFile.url, assignThis);
445
+
446
+ //enqueue
447
+ function queueMessage(req, res, next){
448
+ q.nq(req,res,next,appRodSession);
449
+ };
450
+ app[ctrlFile.method](ctrlFile.url, queueMessage, assignThis);
433
451
  });
434
452
 
435
453
  });
@@ -441,20 +459,9 @@ function _init(callback, commonRodOpt={mongo_create_connect:true, detaillog_add_
441
459
  }
442
460
 
443
461
  }
444
- //ROUTE AT MATCH ( URL NOT FOUND DEFAULT)
445
- app.use((req,res,next)=>{
446
- q.dq(req);
447
- return res.status(403).send("URL NOT FOUND");
448
- });
449
- //CATCH ERROR
450
- app.use(function (err, req, res, next) {
451
- if(req.rodSession){
452
- req.rodSession.error(req.sessionID,err);
453
- }else{
454
- console.error(err);
455
- }
456
- res.status(500).send('Server Error');
457
- });
462
+
463
+ handleErrorExpress( appRodSession, q );
464
+
458
465
  }
459
466
 
460
467
  function _checkFlagStart(callback, appRodSession){
@@ -904,6 +911,35 @@ function handleError(){
904
911
  logger.warn("common-rod", 'Unhandled Rejection at', reason.stack || reason);
905
912
  });
906
913
  }
914
+ function handleErrorExpress(appRodSession, q) {
915
+ //ROUTE AT MATCH ( URL NOT FOUND DEFAULT)
916
+ app.use((req,res,next)=>{
917
+ q.dq(req);
918
+ return res.status(403).send("URL NOT FOUND");
919
+ });
920
+
921
+ //EXCEED LIMIT PARSER
922
+ app.use(function (err, req, res, next) {
923
+ if(err.name == 'PayloadTooLargeError'){
924
+ appRodSession.info( `${req.url} ${err.message}`);
925
+ res.status(err.status || 413);
926
+ res.send(err.message || 'request entity too large');
927
+ }else{
928
+ next(err);
929
+ }
930
+ });
931
+
932
+ //CATCH ERROR
933
+ app.use(function (err, req, res, next) {
934
+ if (req.rodSession) {
935
+ req.rodSession.error(req.sessionID, err);
936
+ } else {
937
+ console.error(err);
938
+ }
939
+ res.status(err.status || 500);
940
+ res.send(err.message || 'Server Error');
941
+ });
942
+ }
907
943
 
908
944
  function stopServer(){
909
945
  logger.close(()=>{
@@ -46,11 +46,15 @@ function transformResponse(data) {
46
46
  return data;
47
47
  }
48
48
 
49
- let defaultTransformResponse = axios.defaults.transformResponse;
49
+ // build fn, bind this
50
+ let defaultTransformResponse = [];
51
+ for (var i = 0; i < axios.defaults.transformResponse.length; i++) {
52
+ defaultTransformResponse.push(axios.defaults.transformResponse[i].bind(axios.defaults));
53
+ }
54
+
50
55
  axios.interceptors.response.use(function (response) {
51
56
  response.rawData = response.data;
52
57
  for (var i = 0; i < defaultTransformResponse.length; i++) {
53
-
54
58
  response.data = defaultTransformResponse[i](response.data);
55
59
  }
56
60
  return response;
package/lib/queue2.js CHANGED
@@ -127,13 +127,13 @@ module.exports = {
127
127
  }
128
128
  enableQ = true;
129
129
  logger = _logger;
130
- }, nq : function (req,res,next) {
130
+ }, nq : function (req,res,next,appRodSession) {
131
131
  if(!enableQ) return next();
132
132
 
133
133
  let queueKey = [];
134
134
  let logKey = '';
135
135
  for (let index = 0; index < correlateQueueFn.length; index++) {
136
- let k = correlateQueueFn[index](req, res);
136
+ let k = correlateQueueFn[index].call(appRodSession, req, res);
137
137
  logKey += k + '|';
138
138
  queueKey.push({
139
139
  "key":k,
@@ -149,7 +149,7 @@ module.exports = {
149
149
  // queue queueKey=${logKey}
150
150
  // globalQueue=${globalQ}
151
151
  // sessionQueue=${sessionQ}`);
152
- logger.debug(req.sessionID,`queue queueKey=${logKey} globalQueue=${globalQ} sessionQueue=${sessionQ}`);
152
+ logger.debug(req.sessionID,`queue queueKey=${logKey} globalQueue=${globalQ} sessionQueue=${sessionQ} URL=${req.url}`);
153
153
  if( globalQ > queueOpt.maxGlobalQ || sessionQ > queueOpt.maxSessionQ ){
154
154
  logger.debug(req.sessionID,`exceeded queue limit`);
155
155
  return rejectQ(req,res);
@@ -158,7 +158,7 @@ module.exports = {
158
158
  let foundQ = find(queueKey);
159
159
  if( foundQ ){
160
160
  // console.log('found');
161
- logger.debug(req.sessionID,`enqueue queueKey=${logKey}`);
161
+ logger.debug(req.sessionID,`enqueue queueKey=${logKey} URL=${req.url}`);
162
162
  enQueue(queueKey,req,next);
163
163
  // console.log(111, queues );
164
164
 
package/lib/rodSession.js CHANGED
@@ -222,6 +222,9 @@ RodSession.prototype = function () {
222
222
  // this.debug("get cache instance mongo.collection=" + colName)
223
223
  }
224
224
  return this.app._rod.collection[colName];
225
+ },
226
+ getDb : () => {
227
+ return this.db;
225
228
  }
226
229
  }
227
230
  }
@@ -52,7 +52,7 @@ function ipOrHost(obj, key, value){
52
52
  }else{
53
53
  //HOSTS
54
54
  // let port = resolve(key.substring(0,key.lastIndexOf("."))+".port", obj);
55
- console.log(9988899);
55
+ // console.log(9988899);
56
56
  return true;
57
57
  }
58
58
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "common-rod",
3
- "version": "1.5.3",
3
+ "version": "1.5.7",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -9,7 +9,7 @@
9
9
  "author": "Thanakhan Iaocharoen",
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
- "axios": "^0.21.1",
12
+ "axios": "^0.21.4",
13
13
  "body-parser": "^1.19.0",
14
14
  "cli-table": "^0.3.1",
15
15
  "commonlog-kb": "^2.3.0",
@@ -19,7 +19,7 @@
19
19
  "express": "^4.17.1",
20
20
  "js-yaml": "^3.14.0",
21
21
  "lodash": "^4.17.20",
22
- "mongodb": "^3.3.2",
22
+ "mongodb": "^3.7.1",
23
23
  "randomstring": "^1.1.5",
24
24
  "swagger-ui-express": "^4.1.4"
25
25
  }
package/readme.md CHANGED
@@ -248,3 +248,46 @@ app.correlateQueue = [function(req, res){
248
248
  }]
249
249
  ```
250
250
  ### Fixed
251
+
252
+ # [1.5.4] - 2021-07-23
253
+ Allowed this keyword for app.correlateQueue
254
+ ### Added
255
+ ```
256
+ app.correlateQueue = function(req, res){
257
+ //this.debug("log");
258
+ return $correlateQueue;
259
+ }
260
+ ```
261
+ ### Changed
262
+ ### Fixed
263
+
264
+ # [1.5.5] - 2021-08-01
265
+ ```
266
+ Handle case PayloadTooLargeError(request body size bigger than pm2.commonRod.body_parser_limit)
267
+ Handle Throw error from module
268
+ ```
269
+ ### Added
270
+ ### Changed
271
+ ### Fixed
272
+
273
+ ### Changed
274
+ ### Fixed
275
+
276
+ # [1.5.6] - 2021-09-08
277
+ ```
278
+ Handle case axios.defaults.transformResponse axios version 0.21.4
279
+ ```
280
+ ### Added
281
+ ### Changed
282
+ ### Fixed
283
+
284
+ # [1.5.7] - 2021-12-03
285
+ ### Added
286
+ ```
287
+ add mongo utils to get db
288
+
289
+ //example
290
+ let db = this.utils().mongo().getDb();
291
+ ```
292
+ ### Changed
293
+ ### Fixed