mastercontroller 1.3.23 → 1.3.25
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 +18 -20
- package/MasterCors.js +1 -0
- package/MasterRouter.js +41 -6
- package/package.json +1 -1
package/MasterControl.js
CHANGED
|
@@ -490,7 +490,7 @@ class MasterControl {
|
|
|
490
490
|
if(type === "http"){
|
|
491
491
|
$that.serverProtocol = "http";
|
|
492
492
|
const server = http.createServer(async function(req, res) {
|
|
493
|
-
$that.serverRun(req, res);
|
|
493
|
+
await $that.serverRun(req, res);
|
|
494
494
|
});
|
|
495
495
|
// Set server immediately so config can access it
|
|
496
496
|
$that.server = server;
|
|
@@ -534,7 +534,7 @@ class MasterControl {
|
|
|
534
534
|
if(credentials.honorCipherOrder === undefined){ credentials.honorCipherOrder = true; }
|
|
535
535
|
if(!credentials.ALPNProtocols){ credentials.ALPNProtocols = ['h2', 'http/1.1']; }
|
|
536
536
|
const server = https.createServer(credentials, async function(req, res) {
|
|
537
|
-
$that.serverRun(req, res);
|
|
537
|
+
await $that.serverRun(req, res);
|
|
538
538
|
});
|
|
539
539
|
// Set server immediately so config can access it
|
|
540
540
|
$that.server = server;
|
|
@@ -1006,25 +1006,23 @@ class MasterControl {
|
|
|
1006
1006
|
const $that = this;
|
|
1007
1007
|
console.log("path", `${req.method} ${req.url}`);
|
|
1008
1008
|
|
|
1009
|
-
// Create request context for middleware pipeline
|
|
1010
|
-
const parsedUrl = url.parse(req.url);
|
|
1011
|
-
const pathname = parsedUrl.pathname;
|
|
1012
|
-
const ext = path.parse(pathname).ext;
|
|
1013
|
-
|
|
1014
|
-
const context = {
|
|
1015
|
-
request: req,
|
|
1016
|
-
response: res,
|
|
1017
|
-
requrl: url.parse(req.url, true),
|
|
1018
|
-
pathName: pathname.replace(/^\/|\/$/g, '').toLowerCase(),
|
|
1019
|
-
type: req.method.toLowerCase(),
|
|
1020
|
-
params: {},
|
|
1021
|
-
state: {}, // User-defined state shared across middleware
|
|
1022
|
-
master: $that, // Access to framework instance
|
|
1023
|
-
isStatic: ext !== '' // Is this a static file request?
|
|
1024
|
-
};
|
|
1025
|
-
|
|
1026
|
-
// Execute middleware pipeline
|
|
1027
1009
|
try {
|
|
1010
|
+
const parsedUrl = url.parse(req.url);
|
|
1011
|
+
const pathname = parsedUrl.pathname;
|
|
1012
|
+
const ext = path.parse(pathname).ext;
|
|
1013
|
+
|
|
1014
|
+
const context = {
|
|
1015
|
+
request: req,
|
|
1016
|
+
response: res,
|
|
1017
|
+
requrl: url.parse(req.url, true),
|
|
1018
|
+
pathName: pathname.replace(/^\/|\/$/g, '').toLowerCase(),
|
|
1019
|
+
type: req.method.toLowerCase(),
|
|
1020
|
+
params: {},
|
|
1021
|
+
state: {},
|
|
1022
|
+
master: $that,
|
|
1023
|
+
isStatic: ext !== ''
|
|
1024
|
+
};
|
|
1025
|
+
|
|
1028
1026
|
await $that.pipeline.execute(context);
|
|
1029
1027
|
} catch (error) {
|
|
1030
1028
|
console.error('Pipeline execution failed:', error);
|
package/MasterCors.js
CHANGED
|
@@ -235,6 +235,7 @@ class MasterCors{
|
|
|
235
235
|
// Handle preflight OPTIONS request
|
|
236
236
|
if (ctx.type === 'options') {
|
|
237
237
|
$that.load({ request: ctx.request, response: ctx.response });
|
|
238
|
+
ctx.request.resume();
|
|
238
239
|
ctx.response.statusCode = 204;
|
|
239
240
|
ctx.response.end();
|
|
240
241
|
return; // Terminal - don't call next()
|
package/MasterRouter.js
CHANGED
|
@@ -805,11 +805,12 @@ class MasterRouter {
|
|
|
805
805
|
|
|
806
806
|
tools.combineObjectPrototype(Control, this._master.controllerList);
|
|
807
807
|
Control.prototype.__namespace = Control.name;
|
|
808
|
-
Control.prototype.__requestObject = requestObject;
|
|
809
|
-
Control.prototype.__currentRoute = currentRoute;
|
|
810
|
-
Control.prototype.__response = requestObject.response;
|
|
811
|
-
Control.prototype.__request = requestObject.request;
|
|
812
808
|
const control = new Control(requestObject);
|
|
809
|
+
// Set request-specific state on INSTANCE (not prototype) for concurrency safety
|
|
810
|
+
control.__requestObject = requestObject;
|
|
811
|
+
control.__currentRoute = currentRoute;
|
|
812
|
+
control.__response = requestObject.response;
|
|
813
|
+
control.__request = requestObject.request;
|
|
813
814
|
const _callEmit = new EventEmitter();
|
|
814
815
|
|
|
815
816
|
_callEmit.once(EVENT_NAMES.CONTROLLER, function(){
|
|
@@ -832,7 +833,27 @@ class MasterRouter {
|
|
|
832
833
|
|
|
833
834
|
// Execute action
|
|
834
835
|
Promise.resolve(wrappedAction.call(control, requestObject))
|
|
835
|
-
.then(() => {
|
|
836
|
+
.then((returnValue) => {
|
|
837
|
+
// Auto-send return value as JSON if controller returned data
|
|
838
|
+
// and no response was sent yet (e.g., overridden returnJson pattern)
|
|
839
|
+
if (returnValue !== undefined && returnValue !== null
|
|
840
|
+
&& !requestObject.response.headersSent && !requestObject.response._headerSent) {
|
|
841
|
+
const seen = new WeakSet();
|
|
842
|
+
const json = JSON.stringify(returnValue, (key, value) => {
|
|
843
|
+
if (typeof value === 'object' && value !== null) {
|
|
844
|
+
if (seen.has(value)) {
|
|
845
|
+
return '[Circular Reference]';
|
|
846
|
+
}
|
|
847
|
+
seen.add(value);
|
|
848
|
+
}
|
|
849
|
+
return value;
|
|
850
|
+
});
|
|
851
|
+
requestObject.response.writeHead(200, {
|
|
852
|
+
'Content-Type': 'application/json',
|
|
853
|
+
'Content-Length': Buffer.byteLength(json, 'utf8')
|
|
854
|
+
});
|
|
855
|
+
requestObject.response.end(json);
|
|
856
|
+
}
|
|
836
857
|
performanceTracker.end(requestId);
|
|
837
858
|
// MEMORY LEAK FIX: Clean up event listeners
|
|
838
859
|
_callEmit.removeAllListeners();
|
|
@@ -869,7 +890,21 @@ class MasterRouter {
|
|
|
869
890
|
|
|
870
891
|
// check if before function is avaliable and wait for it to return
|
|
871
892
|
if(control.__hasBeforeAction(control, requestObject)){
|
|
872
|
-
control.__callBeforeAction(control, requestObject, _callEmit)
|
|
893
|
+
control.__callBeforeAction(control, requestObject, _callEmit)
|
|
894
|
+
.catch((error) => {
|
|
895
|
+
if (!requestObject.response.headersSent && !requestObject.response._headerSent) {
|
|
896
|
+
const mcError = handleControllerError(
|
|
897
|
+
error,
|
|
898
|
+
requestObject.toController,
|
|
899
|
+
requestObject.toAction,
|
|
900
|
+
requestObject.pathName,
|
|
901
|
+
currentRoute.routeDef
|
|
902
|
+
);
|
|
903
|
+
sendErrorResponse(requestObject.response, mcError, requestObject.pathName);
|
|
904
|
+
}
|
|
905
|
+
performanceTracker.end(requestId);
|
|
906
|
+
_callEmit.removeAllListeners();
|
|
907
|
+
});
|
|
873
908
|
}else{
|
|
874
909
|
_callEmit.emit("controller");
|
|
875
910
|
}
|
package/package.json
CHANGED