mastercontroller 1.3.27 → 1.3.29

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/MasterAction.js CHANGED
@@ -109,7 +109,12 @@ class MasterAction{
109
109
  return;
110
110
  }
111
111
 
112
- this.__response.writeHead(HTTP_STATUS.OK, {
112
+ // Use status field from data as HTTP status code if it's a valid 4xx/5xx code
113
+ const httpStatus = (data && typeof data.status === 'number' && data.status >= 400 && data.status <= 599)
114
+ ? data.status
115
+ : HTTP_STATUS.OK;
116
+
117
+ this.__response.writeHead(httpStatus, {
113
118
  'Content-Type': 'application/json',
114
119
  'Content-Length': byteSize
115
120
  });
package/MasterRouter.js CHANGED
@@ -816,6 +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
820
  const _callEmit = new EventEmitter();
820
821
 
821
822
  _callEmit.once(EVENT_NAMES.CONTROLLER, function(){
@@ -853,7 +854,12 @@ class MasterRouter {
853
854
  }
854
855
  return value;
855
856
  });
856
- requestObject.response.writeHead(200, {
857
+ // Use status field from return value as HTTP status code if it's a valid 4xx/5xx code
858
+ const httpStatus = (returnValue && typeof returnValue.status === 'number' && returnValue.status >= 400 && returnValue.status <= 599)
859
+ ? returnValue.status
860
+ : 200;
861
+
862
+ requestObject.response.writeHead(httpStatus, {
857
863
  'Content-Type': 'application/json',
858
864
  'Content-Length': Buffer.byteLength(json, 'utf8')
859
865
  });
package/README.md CHANGED
@@ -268,7 +268,7 @@ Middleware receives a context object:
268
268
  formData: {}, // POST body data
269
269
  periodId: '123' // Route parameters (e.g., /period/:periodId)
270
270
  },
271
- state: {}, // Custom state to share between middleware
271
+ state: {}, // Custom state shared between middleware and controllers (this.state)
272
272
  master: master, // Framework instance
273
273
  isStatic: false // Is this a static file request?
274
274
  }
@@ -718,10 +718,30 @@ console.log(JSON.stringify(snapshot));
718
718
 
719
719
  ### Use Cases
720
720
 
721
- **Share data between middleware and controllers:**
721
+ **Share data between middleware and controllers via `state`:**
722
+ ```javascript
723
+ // In middleware — set state on ctx
724
+ master.pipeline.use(async (ctx, next) => {
725
+ const token = ctx.request.headers.authorization?.replace('Bearer ', '');
726
+ ctx.state.user = await validateToken(token);
727
+ ctx.state.requestStart = Date.now();
728
+ await next();
729
+ });
730
+
731
+ // In controller — access state via this.state
732
+ async index() {
733
+ const user = this.state.user; // Set by auth middleware
734
+ const start = this.state.requestStart;
735
+ return this.returnJson({ user, loadTime: Date.now() - start });
736
+ }
737
+ ```
738
+
739
+ `this.state` in controllers is the same object reference as `ctx.state` in middleware — mutations in either direction are shared within the request lifecycle.
740
+
741
+ **Share data using temp (key-value store):**
722
742
  ```javascript
723
743
  // In middleware
724
- master.use(async (ctx, next) => {
744
+ master.pipeline.use(async (ctx, next) => {
725
745
  ctx.temp.add('requestStart', Date.now());
726
746
  await next();
727
747
  const duration = Date.now() - ctx.temp.get('requestStart');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mastercontroller",
3
- "version": "1.3.27",
3
+ "version": "1.3.29",
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",