@noxfly/noxus 1.1.1 → 1.1.4

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/dist/noxus.js CHANGED
@@ -302,8 +302,10 @@ var _AppInjector = class _AppInjector {
302
302
  this.name = name;
303
303
  }
304
304
  /**
305
- * Utilisé généralement pour créer un scope d'injection de dépendances
306
- * au niveau "scope" (donc durée de vie d'une requête)
305
+ * Typically used to create a dependency injection scope
306
+ * at the "scope" level (i.e., per-request lifetime).
307
+ *
308
+ * SHOULD NOT BE USED by anything else than the framework itself.
307
309
  */
308
310
  createScope() {
309
311
  const scope = new _AppInjector();
@@ -312,8 +314,8 @@ var _AppInjector = class _AppInjector {
312
314
  return scope;
313
315
  }
314
316
  /**
315
- * Appelé lorsqu'on souhaite résoudre une dépendance,
316
- * c'est-à-dire récupérer l'instance d'une classe donnée.
317
+ * Called when resolving a dependency,
318
+ * i.e., retrieving the instance of a given class.
317
319
  */
318
320
  resolve(target) {
319
321
  const binding = this.bindings.get(target);
@@ -340,7 +342,7 @@ Did you forget to use @Injectable() decorator ?`);
340
342
  }
341
343
  }
342
344
  /**
343
- *
345
+ *
344
346
  */
345
347
  instantiate(target) {
346
348
  const paramTypes = Reflect.getMetadata("design:paramtypes", target) || [];
@@ -350,11 +352,11 @@ Did you forget to use @Injectable() decorator ?`);
350
352
  };
351
353
  __name(_AppInjector, "AppInjector");
352
354
  var AppInjector = _AppInjector;
353
- var RootInjector = new AppInjector("root");
354
355
  function inject(t) {
355
356
  return RootInjector.resolve(t);
356
357
  }
357
358
  __name(inject, "inject");
359
+ var RootInjector = new AppInjector("root");
358
360
 
359
361
  // src/router.ts
360
362
  var import_reflect_metadata2 = require("reflect-metadata");
@@ -394,6 +396,11 @@ function getCallee() {
394
396
  return caller;
395
397
  }
396
398
  __name(getCallee, "getCallee");
399
+ function canLog(level) {
400
+ return logLevelRank[level] >= logLevelRank[logLevel];
401
+ }
402
+ __name(canLog, "canLog");
403
+ var logLevel = "log";
397
404
  var logLevelRank = {
398
405
  debug: 0,
399
406
  log: 1,
@@ -401,35 +408,12 @@ var logLevelRank = {
401
408
  warn: 3,
402
409
  error: 4
403
410
  };
404
- function canLog(level) {
405
- return logLevelRank[level] >= logLevelRank[logLevel];
406
- }
407
- __name(canLog, "canLog");
408
- var logLevel = "log";
409
411
  (function(Logger2) {
410
412
  function setLogLevel(level) {
411
413
  logLevel = level;
412
414
  }
413
415
  __name(setLogLevel, "setLogLevel");
414
416
  Logger2.setLogLevel = setLogLevel;
415
- Logger2.colors = {
416
- black: "\x1B[0;30m",
417
- grey: "\x1B[0;37m",
418
- red: "\x1B[0;31m",
419
- green: "\x1B[0;32m",
420
- brown: "\x1B[0;33m",
421
- blue: "\x1B[0;34m",
422
- purple: "\x1B[0;35m",
423
- darkGrey: "\x1B[1;30m",
424
- lightRed: "\x1B[1;31m",
425
- lightGreen: "\x1B[1;32m",
426
- yellow: "\x1B[1;33m",
427
- lightBlue: "\x1B[1;34m",
428
- magenta: "\x1B[1;35m",
429
- cyan: "\x1B[1;36m",
430
- white: "\x1B[1;37m",
431
- initial: "\x1B[0m"
432
- };
433
417
  function log(...args) {
434
418
  if (!canLog("log")) return;
435
419
  const callee = getCallee();
@@ -470,11 +454,28 @@ var logLevel = "log";
470
454
  }
471
455
  __name(debug, "debug");
472
456
  Logger2.debug = debug;
457
+ Logger2.colors = {
458
+ black: "\x1B[0;30m",
459
+ grey: "\x1B[0;37m",
460
+ red: "\x1B[0;31m",
461
+ green: "\x1B[0;32m",
462
+ brown: "\x1B[0;33m",
463
+ blue: "\x1B[0;34m",
464
+ purple: "\x1B[0;35m",
465
+ darkGrey: "\x1B[1;30m",
466
+ lightRed: "\x1B[1;31m",
467
+ lightGreen: "\x1B[1;32m",
468
+ yellow: "\x1B[1;33m",
469
+ lightBlue: "\x1B[1;34m",
470
+ magenta: "\x1B[1;35m",
471
+ cyan: "\x1B[1;36m",
472
+ white: "\x1B[1;37m",
473
+ initial: "\x1B[0m"
474
+ };
473
475
  })(Logger || (Logger = {}));
474
476
  var Logger;
475
477
 
476
478
  // src/decorators/guards.decorator.ts
477
- var authorizations = /* @__PURE__ */ new Map();
478
479
  function Authorize(...guardClasses) {
479
480
  return (target, propertyKey) => {
480
481
  let key;
@@ -504,6 +505,7 @@ function getGuardForControllerAction(controllerName, actionName) {
504
505
  return authorizations.get(key) ?? [];
505
506
  }
506
507
  __name(getGuardForControllerAction, "getGuardForControllerAction");
508
+ var authorizations = /* @__PURE__ */ new Map();
507
509
 
508
510
  // src/decorators/method.decorator.ts
509
511
  function createRouteDecorator(verb) {
@@ -522,16 +524,16 @@ function createRouteDecorator(verb) {
522
524
  };
523
525
  }
524
526
  __name(createRouteDecorator, "createRouteDecorator");
527
+ function getRouteMetadata(target) {
528
+ return Reflect.getMetadata(ROUTE_METADATA_KEY, target) || [];
529
+ }
530
+ __name(getRouteMetadata, "getRouteMetadata");
525
531
  var Get = createRouteDecorator("GET");
526
532
  var Post = createRouteDecorator("POST");
527
533
  var Put = createRouteDecorator("PUT");
528
534
  var Patch = createRouteDecorator("PATCH");
529
535
  var Delete = createRouteDecorator("DELETE");
530
536
  var ROUTE_METADATA_KEY = Symbol("ROUTE_METADATA_KEY");
531
- function getRouteMetadata(target) {
532
- return Reflect.getMetadata(ROUTE_METADATA_KEY, target) || [];
533
- }
534
- __name(getRouteMetadata, "getRouteMetadata");
535
537
 
536
538
  // src/decorators/module.decorator.ts
537
539
  function Module(metadata) {
@@ -569,19 +571,18 @@ function Module(metadata) {
569
571
  };
570
572
  }
571
573
  __name(Module, "Module");
572
- var MODULE_METADATA_KEY = Symbol("MODULE_METADATA_KEY");
573
574
  function getModuleMetadata(target) {
574
575
  return Reflect.getMetadata(MODULE_METADATA_KEY, target);
575
576
  }
576
577
  __name(getModuleMetadata, "getModuleMetadata");
578
+ var MODULE_METADATA_KEY = Symbol("MODULE_METADATA_KEY");
577
579
 
578
580
  // src/DI/injector-explorer.ts
579
581
  var _InjectorExplorer = class _InjectorExplorer {
580
582
  /**
581
- * Enregistre la classe comme étant injectable.
582
- * Lorsqu'une classe sera instanciée, si elle a des dépendances, et que celles-ci
583
- * figurent dans la liste grâce à cette méthode, elles seront injectées dans le
584
- * constructeur de la classe.
583
+ * Registers the class as injectable.
584
+ * When a class is instantiated, if it has dependencies and those dependencies
585
+ * are listed using this method, they will be injected into the class constructor.
585
586
  */
586
587
  static register(target, lifetime) {
587
588
  Logger.debug(`Registering ${target.name} as ${lifetime}`);
@@ -628,11 +629,11 @@ function Injectable(lifetime = "scope") {
628
629
  };
629
630
  }
630
631
  __name(Injectable, "Injectable");
631
- var INJECTABLE_METADATA_KEY = Symbol("INJECTABLE_METADATA_KEY");
632
632
  function getInjectableMetadata(target) {
633
633
  return Reflect.getMetadata(INJECTABLE_METADATA_KEY, target);
634
634
  }
635
635
  __name(getInjectableMetadata, "getInjectableMetadata");
636
+ var INJECTABLE_METADATA_KEY = Symbol("INJECTABLE_METADATA_KEY");
636
637
 
637
638
  // src/decorators/controller.decorator.ts
638
639
  function Controller(path) {
@@ -646,14 +647,13 @@ function Controller(path) {
646
647
  };
647
648
  }
648
649
  __name(Controller, "Controller");
649
- var CONTROLLER_METADATA_KEY = Symbol("CONTROLLER_METADATA_KEY");
650
650
  function getControllerMetadata(target) {
651
651
  return Reflect.getMetadata(CONTROLLER_METADATA_KEY, target);
652
652
  }
653
653
  __name(getControllerMetadata, "getControllerMetadata");
654
+ var CONTROLLER_METADATA_KEY = Symbol("CONTROLLER_METADATA_KEY");
654
655
 
655
656
  // src/decorators/middleware.decorator.ts
656
- var middlewares = /* @__PURE__ */ new Map();
657
657
  function UseMiddlewares(mdlw) {
658
658
  return (target, propertyKey) => {
659
659
  let key;
@@ -683,10 +683,15 @@ function getMiddlewaresForControllerAction(controllerName, actionName) {
683
683
  return middlewares.get(key) ?? [];
684
684
  }
685
685
  __name(getMiddlewaresForControllerAction, "getMiddlewaresForControllerAction");
686
+ var middlewares = /* @__PURE__ */ new Map();
686
687
 
687
688
  // src/utils/radix-tree.ts
688
689
  var _a;
689
690
  var RadixNode = (_a = class {
691
+ /**
692
+ * Creates a new RadixNode.
693
+ * @param segment - The segment of the path this node represents.
694
+ */
690
695
  constructor(segment) {
691
696
  __publicField(this, "segment");
692
697
  __publicField(this, "children", []);
@@ -699,15 +704,32 @@ var RadixNode = (_a = class {
699
704
  this.paramName = segment.slice(1);
700
705
  }
701
706
  }
707
+ /**
708
+ * Matches a child node against a given segment.
709
+ * This method checks if the segment matches any of the children nodes.
710
+ * @param segment - The segment to match against the children of this node.
711
+ * @returns A child node that matches the segment, or undefined if no match is found.
712
+ */
702
713
  matchChild(segment) {
703
714
  for (const child of this.children) {
704
715
  if (child.isParam || segment.startsWith(child.segment)) return child;
705
716
  }
706
717
  return void 0;
707
718
  }
719
+ /**
720
+ * Finds a child node that matches the segment exactly.
721
+ * This method checks if there is a child node that matches the segment exactly.
722
+ * @param segment - The segment to find an exact match for among the children of this node.
723
+ * @returns A child node that matches the segment exactly, or undefined if no match is found.
724
+ */
708
725
  findExactChild(segment) {
709
726
  return this.children.find((c) => c.segment === segment);
710
727
  }
728
+ /**
729
+ * Adds a child node to this node's children.
730
+ * This method adds a new child node to the list of children for this node.
731
+ * @param node - The child node to add to this node's children.
732
+ */
711
733
  addChild(node) {
712
734
  this.children.push(node);
713
735
  }
@@ -716,10 +738,23 @@ var _RadixTree = class _RadixTree {
716
738
  constructor() {
717
739
  __publicField(this, "root", new RadixNode(""));
718
740
  }
741
+ /**
742
+ * Inserts a path and its associated value into the Radix Tree.
743
+ * This method normalizes the path and inserts it into the tree, associating it with
744
+ * @param path - The path to insert into the tree.
745
+ * @param value - The value to associate with the path.
746
+ */
719
747
  insert(path, value) {
720
748
  const segments = this.normalize(path);
721
749
  this.insertRecursive(this.root, segments, value);
722
750
  }
751
+ /**
752
+ * Recursively inserts a path into the Radix Tree.
753
+ * This method traverses the tree and inserts the segments of the path, creating new nodes
754
+ * @param node - The node to start inserting from.
755
+ * @param segments - The segments of the path to insert.
756
+ * @param value - The value to associate with the path.
757
+ */
723
758
  insertRecursive(node, segments, value) {
724
759
  if (segments.length === 0) {
725
760
  node.value = value;
@@ -733,10 +768,24 @@ var _RadixTree = class _RadixTree {
733
768
  }
734
769
  this.insertRecursive(child, segments.slice(1), value);
735
770
  }
771
+ /**
772
+ * Searches for a path in the Radix Tree.
773
+ * This method normalizes the path and searches for it in the tree, returning the node
774
+ * @param path - The path to search for in the Radix Tree.
775
+ * @returns An ISearchResult containing the node and parameters if a match is found, otherwise undefined.
776
+ */
736
777
  search(path) {
737
778
  const segments = this.normalize(path);
738
779
  return this.searchRecursive(this.root, segments, {});
739
780
  }
781
+ /**
782
+ * Recursively searches for a path in the Radix Tree.
783
+ * This method traverses the tree and searches for the segments of the path, collecting parameters
784
+ * @param node - The node to start searching from.
785
+ * @param segments - The segments of the path to search for.
786
+ * @param params - The parameters collected during the search.
787
+ * @returns An ISearchResult containing the node and parameters if a match is found, otherwise undefined.
788
+ */
740
789
  searchRecursive(node, segments, params) {
741
790
  if (segments.length === 0) {
742
791
  if (node.value !== void 0) {
@@ -776,6 +825,12 @@ var _RadixTree = class _RadixTree {
776
825
  }
777
826
  return void 0;
778
827
  }
828
+ /**
829
+ * Normalizes a path into an array of segments.
830
+ * This method removes leading and trailing slashes, splits the path by slashes, and
831
+ * @param path - The path to normalize.
832
+ * @returns An array of normalized path segments.
833
+ */
779
834
  normalize(path) {
780
835
  const segments = path.replace(/^\/+|\/+$/g, "").split("/").filter(Boolean);
781
836
  return [
@@ -801,7 +856,10 @@ var _Router = class _Router {
801
856
  __publicField(this, "rootMiddlewares", []);
802
857
  }
803
858
  /**
804
- *
859
+ * Registers a controller class with the router.
860
+ * This method extracts the route metadata from the controller class and registers it in the routing tree.
861
+ * It also handles the guards and middlewares associated with the controller.
862
+ * @param controllerClass - The controller class to register.
805
863
  */
806
864
  registerController(controllerClass) {
807
865
  const controllerMeta = getControllerMetadata(controllerClass);
@@ -844,7 +902,10 @@ var _Router = class _Router {
844
902
  return this;
845
903
  }
846
904
  /**
847
- *
905
+ * Defines a middleware for the root of the application.
906
+ * This method allows you to register a middleware that will be applied to all requests
907
+ * to the application, regardless of the controller or action.
908
+ * @param middleware - The middleware class to register.
848
909
  */
849
910
  defineRootMiddleware(middleware) {
850
911
  Logger.debug(`Registering root middleware: ${middleware.name}`);
@@ -852,7 +913,10 @@ var _Router = class _Router {
852
913
  return this;
853
914
  }
854
915
  /**
855
- *
916
+ * Shuts down the message channel for a specific sender ID.
917
+ * This method closes the IPC channel for the specified sender ID and
918
+ * removes it from the messagePorts map.
919
+ * @param channelSenderId - The ID of the sender channel to shut down.
856
920
  */
857
921
  async handle(request) {
858
922
  Logger.log(`> Received request: {${request.method} /${request.path}}`);
@@ -893,7 +957,11 @@ var _Router = class _Router {
893
957
  }
894
958
  }
895
959
  /**
896
- *
960
+ * Finds the route definition for a given request.
961
+ * This method searches the routing tree for a matching route based on the request's path and method.
962
+ * If no matching route is found, it throws a NotFoundException.
963
+ * @param request - The Request object containing the method and path to search for.
964
+ * @returns The IRouteDefinition for the matched route.
897
965
  */
898
966
  findRoute(request) {
899
967
  const matchedRoutes = this.routes.search(request.path);
@@ -907,7 +975,14 @@ var _Router = class _Router {
907
975
  return routeDef.value;
908
976
  }
909
977
  /**
910
- *
978
+ * Resolves the controller for a given route definition.
979
+ * This method creates an instance of the controller class and prepares the request parameters.
980
+ * It also runs the request pipeline, which includes executing middlewares and guards.
981
+ * @param request - The Request object containing the request data.
982
+ * @param response - The IResponse object to populate with the response data.
983
+ * @param routeDef - The IRouteDefinition for the matched route.
984
+ * @return A Promise that resolves when the controller action has been executed.
985
+ * @throws UnauthorizedException if the request is not authorized by the guards.
911
986
  */
912
987
  async resolveController(request, response, routeDef) {
913
988
  const controllerInstance = request.context.resolve(routeDef.controller);
@@ -915,7 +990,15 @@ var _Router = class _Router {
915
990
  await this.runRequestPipeline(request, response, routeDef, controllerInstance);
916
991
  }
917
992
  /**
918
- *
993
+ * Runs the request pipeline for a given request.
994
+ * This method executes the middlewares and guards associated with the route,
995
+ * and finally calls the controller action.
996
+ * @param request - The Request object containing the request data.
997
+ * @param response - The IResponse object to populate with the response data.
998
+ * @param routeDef - The IRouteDefinition for the matched route.
999
+ * @param controllerInstance - The instance of the controller class.
1000
+ * @return A Promise that resolves when the request pipeline has been executed.
1001
+ * @throws ResponseException if the response status is not successful.
919
1002
  */
920
1003
  async runRequestPipeline(request, response, routeDef, controllerInstance) {
921
1004
  const middlewares2 = [
@@ -951,14 +1034,27 @@ var _Router = class _Router {
951
1034
  await dispatch(0);
952
1035
  }
953
1036
  /**
954
- *
1037
+ * Runs a middleware function in the request pipeline.
1038
+ * This method creates an instance of the middleware and invokes its `invoke` method,
1039
+ * passing the request, response, and next function.
1040
+ * @param request - The Request object containing the request data.
1041
+ * @param response - The IResponse object to populate with the response data.
1042
+ * @param next - The NextFunction to call to continue the middleware chain.
1043
+ * @param middlewareType - The type of the middleware to run.
1044
+ * @return A Promise that resolves when the middleware has been executed.
955
1045
  */
956
1046
  async runMiddleware(request, response, next, middlewareType) {
957
1047
  const middleware = request.context.resolve(middlewareType);
958
1048
  await middleware.invoke(request, response, next);
959
1049
  }
960
1050
  /**
961
- *
1051
+ * Runs a guard to check if the request is authorized.
1052
+ * This method creates an instance of the guard and calls its `canActivate` method.
1053
+ * If the guard returns false, it throws an UnauthorizedException.
1054
+ * @param request - The Request object containing the request data.
1055
+ * @param guardType - The type of the guard to run.
1056
+ * @return A Promise that resolves if the guard allows the request, or throws an UnauthorizedException if not.
1057
+ * @throws UnauthorizedException if the guard denies access to the request.
962
1058
  */
963
1059
  async runGuard(request, guardType) {
964
1060
  const guard = request.context.resolve(guardType);
@@ -966,7 +1062,12 @@ var _Router = class _Router {
966
1062
  if (!allowed) throw new UnauthorizedException(`Unauthorized for ${request.method} ${request.path}`);
967
1063
  }
968
1064
  /**
969
- *
1065
+ * Extracts parameters from the actual request path based on the template path.
1066
+ * This method splits the actual path and the template path into segments,
1067
+ * then maps the segments to parameters based on the template.
1068
+ * @param actual - The actual request path.
1069
+ * @param template - The template path to extract parameters from.
1070
+ * @returns An object containing the extracted parameters.
970
1071
  */
971
1072
  extractParams(actual, template) {
972
1073
  const aParts = actual.split("/");
@@ -1031,7 +1132,9 @@ var _NoxApp = class _NoxApp {
1031
1132
  this.router = router;
1032
1133
  }
1033
1134
  /**
1034
- *
1135
+ * Initializes the NoxApp instance.
1136
+ * This method sets up the IPC communication, registers event listeners,
1137
+ * and prepares the application for use.
1035
1138
  */
1036
1139
  async init() {
1037
1140
  import_main.ipcMain.on("gimme-my-port", this.giveTheRendererAPort.bind(this));
@@ -1041,7 +1144,10 @@ var _NoxApp = class _NoxApp {
1041
1144
  return this;
1042
1145
  }
1043
1146
  /**
1044
- *
1147
+ * Handles the request from the renderer process.
1148
+ * This method creates a Request object from the IPC event data,
1149
+ * processes it through the Router, and sends the response back
1150
+ * to the renderer process using the MessageChannel.
1045
1151
  */
1046
1152
  giveTheRendererAPort(event) {
1047
1153
  const senderId = event.sender.id;
@@ -1091,7 +1197,14 @@ var _NoxApp = class _NoxApp {
1091
1197
  this.app?.onActivated();
1092
1198
  }
1093
1199
  }
1094
- shutdownChannel(channelSenderId, remove = true) {
1200
+ /**
1201
+ * Shuts down the message channel for a specific sender ID.
1202
+ * This method closes the IPC channel for the specified sender ID and
1203
+ * removes it from the messagePorts map.
1204
+ * @param channelSenderId - The ID of the sender channel to shut down.
1205
+ * @param remove - Whether to remove the channel from the messagePorts map.
1206
+ */
1207
+ shutdownChannel(channelSenderId) {
1095
1208
  const channel = this.messagePorts.get(channelSenderId);
1096
1209
  if (!channel) {
1097
1210
  Logger.warn(`No message channel found for sender ID: ${channelSenderId}`);
@@ -1103,11 +1216,12 @@ var _NoxApp = class _NoxApp {
1103
1216
  this.messagePorts.delete(channelSenderId);
1104
1217
  }
1105
1218
  /**
1106
- *
1219
+ * Handles the application shutdown process.
1220
+ * This method is called when all windows are closed, and it cleans up the message channels
1107
1221
  */
1108
1222
  async onAllWindowsClosed() {
1109
1223
  this.messagePorts.forEach((channel, senderId) => {
1110
- this.shutdownChannel(senderId, false);
1224
+ this.shutdownChannel(senderId);
1111
1225
  });
1112
1226
  this.messagePorts.clear();
1113
1227
  this.app?.dispose();
@@ -1116,16 +1230,29 @@ var _NoxApp = class _NoxApp {
1116
1230
  }
1117
1231
  }
1118
1232
  // ---
1233
+ /**
1234
+ * Configures the NoxApp instance with the provided application class.
1235
+ * This method allows you to set the application class that will handle lifecycle events.
1236
+ * @param app - The application class to configure.
1237
+ * @returns NoxApp instance for method chaining.
1238
+ */
1119
1239
  configure(app3) {
1120
1240
  this.app = inject(app3);
1121
1241
  return this;
1122
1242
  }
1243
+ /**
1244
+ * Registers a middleware for the root of the application.
1245
+ * This method allows you to define a middleware that will be applied to all requests
1246
+ * @param middleware - The middleware class to register.
1247
+ * @returns NoxApp instance for method chaining.
1248
+ */
1123
1249
  use(middleware) {
1124
1250
  this.router.defineRootMiddleware(middleware);
1125
1251
  return this;
1126
1252
  }
1127
1253
  /**
1128
1254
  * Should be called after the bootstrapApplication function is called.
1255
+ * @returns NoxApp instance for method chaining.
1129
1256
  */
1130
1257
  start() {
1131
1258
  this.app?.onReady();