mastercontroller 1.3.31 → 1.3.33

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,37 +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
- // Attach pipeline state to raw request object so it survives even if
963
- // config/load creates a new requestObject without copying ctx.state
964
- ctx.request.__pipelineState = ctx.state;
965
- // Load config/load which triggers routing
966
- require(`${$that.root}/config/load`)(ctx);
967
- });
968
-
969
- // 6. Global Error Handler
970
- $that.pipeline.useError(async (error, ctx, next) => {
971
- logger.error({
972
- code: 'MC_ERR_PIPELINE',
973
- message: 'Error in middleware pipeline',
974
- error: error.message,
975
- stack: error.stack,
976
- path: ctx.request.url,
977
- method: ctx.type
978
- });
979
-
980
- if (!ctx.response.headersSent) {
981
- ctx.response.statusCode = 500;
982
- ctx.response.setHeader('Content-Type', 'application/json');
983
- ctx.response.end(JSON.stringify({
984
- error: 'Internal Server Error',
985
- message: process.env.NODE_ENV === 'production'
986
- ? 'An error occurred'
987
- : error.message
988
- }));
989
- }
990
- });
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.
991
963
  }
992
964
 
993
965
  async serverRun(req, res){
@@ -1031,6 +1003,41 @@ class MasterControl {
1031
1003
  this.serverSettings(this._pendingServerSettings);
1032
1004
  this._pendingServerSettings = null;
1033
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
+ });
1034
1041
  }
1035
1042
 
1036
1043
  startMVC(foldername){
package/MasterPipeline.js CHANGED
@@ -115,8 +115,10 @@ class MasterPipeline {
115
115
  if (this._pathMatches(requestPath, path)) {
116
116
  // Execute branch pipeline
117
117
  await branch.execute(ctx);
118
- // After branch completes, continue main pipeline
119
- await next();
118
+ // Stop if response already sent (e.g., auth rejection)
119
+ if (!ctx.response.headersSent && !ctx.response.writableEnded) {
120
+ await next();
121
+ }
120
122
  } else {
121
123
  // Skip branch, continue main pipeline
122
124
  await next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mastercontroller",
3
- "version": "1.3.31",
3
+ "version": "1.3.33",
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",