mastercontroller 1.3.30 → 1.3.32

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/MasterControl.js CHANGED
@@ -957,34 +957,9 @@ class MasterControl {
957
957
  await next();
958
958
  });
959
959
 
960
- // 5. Routing (TERMINAL - always needed)
961
- $that.pipeline.run(async (ctx) => {
962
- // Load config/load which triggers routing
963
- require(`${$that.root}/config/load`)(ctx);
964
- });
965
-
966
- // 6. Global Error Handler
967
- $that.pipeline.useError(async (error, ctx, next) => {
968
- logger.error({
969
- code: 'MC_ERR_PIPELINE',
970
- message: 'Error in middleware pipeline',
971
- error: error.message,
972
- stack: error.stack,
973
- path: ctx.request.url,
974
- method: ctx.type
975
- });
976
-
977
- if (!ctx.response.headersSent) {
978
- ctx.response.statusCode = 500;
979
- ctx.response.setHeader('Content-Type', 'application/json');
980
- ctx.response.end(JSON.stringify({
981
- error: 'Internal Server Error',
982
- message: process.env.NODE_ENV === 'production'
983
- ? 'An error occurred'
984
- : error.message
985
- }));
986
- }
987
- });
960
+ // 5. Routing and Error Handler are registered in start() so that user
961
+ // middleware (auth, logging, etc.) registered between setupServer() and
962
+ // start() runs BEFORE the terminal routing middleware.
988
963
  }
989
964
 
990
965
  async serverRun(req, res){
@@ -1028,6 +1003,41 @@ class MasterControl {
1028
1003
  this.serverSettings(this._pendingServerSettings);
1029
1004
  this._pendingServerSettings = null;
1030
1005
  }
1006
+
1007
+ // Register terminal routing and error handler LAST so that user middleware
1008
+ // (auth, logging, etc.) registered between setupServer() and start() runs first
1009
+ const $that = this;
1010
+
1011
+ // Terminal routing middleware
1012
+ $that.pipeline.run(async (ctx) => {
1013
+ // Attach pipeline state to raw request object so it survives even if
1014
+ // config/load creates a new requestObject without copying ctx.state
1015
+ ctx.request.__pipelineState = ctx.state;
1016
+ require(`${$that.root}/config/load`)(ctx);
1017
+ });
1018
+
1019
+ // Global error handler
1020
+ $that.pipeline.useError(async (error, ctx, next) => {
1021
+ logger.error({
1022
+ code: 'MC_ERR_PIPELINE',
1023
+ message: 'Error in middleware pipeline',
1024
+ error: error.message,
1025
+ stack: error.stack,
1026
+ path: ctx.request.url,
1027
+ method: ctx.type
1028
+ });
1029
+
1030
+ if (!ctx.response.headersSent) {
1031
+ ctx.response.statusCode = 500;
1032
+ ctx.response.setHeader('Content-Type', 'application/json');
1033
+ ctx.response.end(JSON.stringify({
1034
+ error: 'Internal Server Error',
1035
+ message: process.env.NODE_ENV === 'production'
1036
+ ? 'An error occurred'
1037
+ : error.message
1038
+ }));
1039
+ }
1040
+ });
1031
1041
  }
1032
1042
 
1033
1043
  startMVC(foldername){
package/MasterRouter.js CHANGED
@@ -816,7 +816,7 @@ class MasterRouter {
816
816
  control.__currentRoute = currentRoute;
817
817
  control.__response = requestObject.response;
818
818
  control.__request = requestObject.request;
819
- control.state = requestObject.state || {};
819
+ control.state = requestObject.state || requestObject.request.__pipelineState || {};
820
820
  const _callEmit = new EventEmitter();
821
821
 
822
822
  _callEmit.once(EVENT_NAMES.CONTROLLER, function(){
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mastercontroller",
3
- "version": "1.3.30",
3
+ "version": "1.3.32",
4
4
  "description": "Fortune 500 ready Node.js MVC framework with enterprise security, monitoring, and horizontal scaling",
5
5
  "main": "MasterControl.js",
6
6
  "license": "MIT",