@noxfly/noxus 1.1.4 → 1.1.6
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.d.mts +8 -1
- package/dist/noxus.d.ts +8 -1
- package/dist/noxus.js +119 -113
- package/dist/noxus.mjs +119 -113
- package/package.json +1 -1
- package/src/DI/injector-explorer.ts +0 -1
- package/src/app.ts +2 -1
- package/src/decorators/guards.decorator.ts +0 -2
- package/src/decorators/middleware.decorator.ts +0 -2
- package/src/router.ts +1 -2
- package/src/utils/logger.ts +29 -7
package/dist/noxus.d.mts
CHANGED
|
@@ -577,7 +577,7 @@ declare const MODULE_METADATA_KEY: unique symbol;
|
|
|
577
577
|
/**
|
|
578
578
|
* Logger is a utility class for logging messages to the console.
|
|
579
579
|
*/
|
|
580
|
-
type LogLevel = 'log' | 'info' | 'warn' | 'error' | 'debug';
|
|
580
|
+
type LogLevel = 'log' | 'info' | 'warn' | 'error' | 'debug' | 'comment';
|
|
581
581
|
declare namespace Logger {
|
|
582
582
|
/**
|
|
583
583
|
* Sets the log level for the logger.
|
|
@@ -621,6 +621,13 @@ declare namespace Logger {
|
|
|
621
621
|
* @param args The arguments to log.
|
|
622
622
|
*/
|
|
623
623
|
function debug(...args: any[]): void;
|
|
624
|
+
/**
|
|
625
|
+
* Logs a message to the console with log level COMMENT.
|
|
626
|
+
* This function formats the message with a timestamp, process ID, and the name of the caller function or class.
|
|
627
|
+
* It uses different colors for different log levels to enhance readability.
|
|
628
|
+
* @param args The arguments to log.
|
|
629
|
+
*/
|
|
630
|
+
function comment(...args: any[]): void;
|
|
624
631
|
const colors: {
|
|
625
632
|
black: string;
|
|
626
633
|
grey: string;
|
package/dist/noxus.d.ts
CHANGED
|
@@ -577,7 +577,7 @@ declare const MODULE_METADATA_KEY: unique symbol;
|
|
|
577
577
|
/**
|
|
578
578
|
* Logger is a utility class for logging messages to the console.
|
|
579
579
|
*/
|
|
580
|
-
type LogLevel = 'log' | 'info' | 'warn' | 'error' | 'debug';
|
|
580
|
+
type LogLevel = 'log' | 'info' | 'warn' | 'error' | 'debug' | 'comment';
|
|
581
581
|
declare namespace Logger {
|
|
582
582
|
/**
|
|
583
583
|
* Sets the log level for the logger.
|
|
@@ -621,6 +621,13 @@ declare namespace Logger {
|
|
|
621
621
|
* @param args The arguments to log.
|
|
622
622
|
*/
|
|
623
623
|
function debug(...args: any[]): void;
|
|
624
|
+
/**
|
|
625
|
+
* Logs a message to the console with log level COMMENT.
|
|
626
|
+
* This function formats the message with a timestamp, process ID, and the name of the caller function or class.
|
|
627
|
+
* It uses different colors for different log levels to enhance readability.
|
|
628
|
+
* @param args The arguments to log.
|
|
629
|
+
*/
|
|
630
|
+
function comment(...args: any[]): void;
|
|
624
631
|
const colors: {
|
|
625
632
|
black: string;
|
|
626
633
|
grey: string;
|
package/dist/noxus.js
CHANGED
|
@@ -361,6 +361,107 @@ var RootInjector = new AppInjector("root");
|
|
|
361
361
|
// src/router.ts
|
|
362
362
|
var import_reflect_metadata2 = require("reflect-metadata");
|
|
363
363
|
|
|
364
|
+
// src/decorators/guards.decorator.ts
|
|
365
|
+
function Authorize(...guardClasses) {
|
|
366
|
+
return (target, propertyKey) => {
|
|
367
|
+
let key;
|
|
368
|
+
if (propertyKey) {
|
|
369
|
+
const ctrlName = target.constructor.name;
|
|
370
|
+
const actionName = propertyKey;
|
|
371
|
+
key = `${ctrlName}.${actionName}`;
|
|
372
|
+
} else {
|
|
373
|
+
const ctrlName = target.name;
|
|
374
|
+
key = `${ctrlName}`;
|
|
375
|
+
}
|
|
376
|
+
if (authorizations.has(key)) {
|
|
377
|
+
throw new Error(`Guard(s) already registered for ${key}`);
|
|
378
|
+
}
|
|
379
|
+
authorizations.set(key, guardClasses);
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
__name(Authorize, "Authorize");
|
|
383
|
+
function getGuardForController(controllerName) {
|
|
384
|
+
const key = `${controllerName}`;
|
|
385
|
+
return authorizations.get(key) ?? [];
|
|
386
|
+
}
|
|
387
|
+
__name(getGuardForController, "getGuardForController");
|
|
388
|
+
function getGuardForControllerAction(controllerName, actionName) {
|
|
389
|
+
const key = `${controllerName}.${actionName}`;
|
|
390
|
+
return authorizations.get(key) ?? [];
|
|
391
|
+
}
|
|
392
|
+
__name(getGuardForControllerAction, "getGuardForControllerAction");
|
|
393
|
+
var authorizations = /* @__PURE__ */ new Map();
|
|
394
|
+
|
|
395
|
+
// src/decorators/method.decorator.ts
|
|
396
|
+
function createRouteDecorator(verb) {
|
|
397
|
+
return (path) => {
|
|
398
|
+
return (target, propertyKey) => {
|
|
399
|
+
const existingRoutes = Reflect.getMetadata(ROUTE_METADATA_KEY, target.constructor) || [];
|
|
400
|
+
const metadata = {
|
|
401
|
+
method: verb,
|
|
402
|
+
path: path.trim().replace(/^\/|\/$/g, ""),
|
|
403
|
+
handler: propertyKey,
|
|
404
|
+
guards: getGuardForControllerAction(target.constructor.__controllerName, propertyKey)
|
|
405
|
+
};
|
|
406
|
+
existingRoutes.push(metadata);
|
|
407
|
+
Reflect.defineMetadata(ROUTE_METADATA_KEY, existingRoutes, target.constructor);
|
|
408
|
+
};
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
__name(createRouteDecorator, "createRouteDecorator");
|
|
412
|
+
function getRouteMetadata(target) {
|
|
413
|
+
return Reflect.getMetadata(ROUTE_METADATA_KEY, target) || [];
|
|
414
|
+
}
|
|
415
|
+
__name(getRouteMetadata, "getRouteMetadata");
|
|
416
|
+
var Get = createRouteDecorator("GET");
|
|
417
|
+
var Post = createRouteDecorator("POST");
|
|
418
|
+
var Put = createRouteDecorator("PUT");
|
|
419
|
+
var Patch = createRouteDecorator("PATCH");
|
|
420
|
+
var Delete = createRouteDecorator("DELETE");
|
|
421
|
+
var ROUTE_METADATA_KEY = Symbol("ROUTE_METADATA_KEY");
|
|
422
|
+
|
|
423
|
+
// src/decorators/module.decorator.ts
|
|
424
|
+
function Module(metadata) {
|
|
425
|
+
return (target) => {
|
|
426
|
+
const checkModule = /* @__PURE__ */ __name((arr, arrName) => {
|
|
427
|
+
if (!arr) return;
|
|
428
|
+
for (const clazz of arr) {
|
|
429
|
+
if (!Reflect.getMetadata(MODULE_METADATA_KEY, clazz)) {
|
|
430
|
+
throw new Error(`Class ${clazz.name} in ${arrName} must be decorated with @Module`);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}, "checkModule");
|
|
434
|
+
const checkInjectable = /* @__PURE__ */ __name((arr) => {
|
|
435
|
+
if (!arr) return;
|
|
436
|
+
for (const clazz of arr) {
|
|
437
|
+
if (!Reflect.getMetadata(INJECTABLE_METADATA_KEY, clazz)) {
|
|
438
|
+
throw new Error(`Class ${clazz.name} in providers must be decorated with @Injectable`);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}, "checkInjectable");
|
|
442
|
+
const checkController = /* @__PURE__ */ __name((arr) => {
|
|
443
|
+
if (!arr) return;
|
|
444
|
+
for (const clazz of arr) {
|
|
445
|
+
if (!Reflect.getMetadata(CONTROLLER_METADATA_KEY, clazz)) {
|
|
446
|
+
throw new Error(`Class ${clazz.name} in controllers must be decorated with @Controller`);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}, "checkController");
|
|
450
|
+
checkModule(metadata.imports, "imports");
|
|
451
|
+
checkModule(metadata.exports, "exports");
|
|
452
|
+
checkInjectable(metadata.providers);
|
|
453
|
+
checkController(metadata.controllers);
|
|
454
|
+
Reflect.defineMetadata(MODULE_METADATA_KEY, metadata, target);
|
|
455
|
+
Injectable("singleton")(target);
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
__name(Module, "Module");
|
|
459
|
+
function getModuleMetadata(target) {
|
|
460
|
+
return Reflect.getMetadata(MODULE_METADATA_KEY, target);
|
|
461
|
+
}
|
|
462
|
+
__name(getModuleMetadata, "getModuleMetadata");
|
|
463
|
+
var MODULE_METADATA_KEY = Symbol("MODULE_METADATA_KEY");
|
|
464
|
+
|
|
364
465
|
// src/utils/logger.ts
|
|
365
466
|
function getPrettyTimestamp() {
|
|
366
467
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -392,7 +493,7 @@ function formattedArgs(prefix, args, color) {
|
|
|
392
493
|
__name(formattedArgs, "formattedArgs");
|
|
393
494
|
function getCallee() {
|
|
394
495
|
const stack = new Error().stack?.split("\n") ?? [];
|
|
395
|
-
const caller = stack[3]?.trim().match(/at (.+?)(?:\..+)? .+$/)?.[1]?.replace("Object", "") || "App";
|
|
496
|
+
const caller = stack[3]?.trim().match(/at (.+?)(?:\..+)? .+$/)?.[1]?.replace("Object", "").replace(/^_/, "") || "App";
|
|
396
497
|
return caller;
|
|
397
498
|
}
|
|
398
499
|
__name(getCallee, "getCallee");
|
|
@@ -400,13 +501,14 @@ function canLog(level) {
|
|
|
400
501
|
return logLevelRank[level] >= logLevelRank[logLevel];
|
|
401
502
|
}
|
|
402
503
|
__name(canLog, "canLog");
|
|
403
|
-
var logLevel = "
|
|
504
|
+
var logLevel = "debug";
|
|
404
505
|
var logLevelRank = {
|
|
405
506
|
debug: 0,
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
507
|
+
comment: 1,
|
|
508
|
+
log: 2,
|
|
509
|
+
info: 3,
|
|
510
|
+
warn: 4,
|
|
511
|
+
error: 5
|
|
410
512
|
};
|
|
411
513
|
(function(Logger2) {
|
|
412
514
|
function setLogLevel(level) {
|
|
@@ -454,6 +556,14 @@ var logLevelRank = {
|
|
|
454
556
|
}
|
|
455
557
|
__name(debug, "debug");
|
|
456
558
|
Logger2.debug = debug;
|
|
559
|
+
function comment(...args) {
|
|
560
|
+
if (!canLog("comment")) return;
|
|
561
|
+
const callee = getCallee();
|
|
562
|
+
const prefix = getLogPrefix(callee, "comment", Logger2.colors.grey);
|
|
563
|
+
console.debug(prefix, ...formattedArgs(prefix, args, Logger2.colors.grey));
|
|
564
|
+
}
|
|
565
|
+
__name(comment, "comment");
|
|
566
|
+
Logger2.comment = comment;
|
|
457
567
|
Logger2.colors = {
|
|
458
568
|
black: "\x1B[0;30m",
|
|
459
569
|
grey: "\x1B[0;37m",
|
|
@@ -475,108 +585,6 @@ var logLevelRank = {
|
|
|
475
585
|
})(Logger || (Logger = {}));
|
|
476
586
|
var Logger;
|
|
477
587
|
|
|
478
|
-
// src/decorators/guards.decorator.ts
|
|
479
|
-
function Authorize(...guardClasses) {
|
|
480
|
-
return (target, propertyKey) => {
|
|
481
|
-
let key;
|
|
482
|
-
if (propertyKey) {
|
|
483
|
-
const ctrlName = target.constructor.name;
|
|
484
|
-
const actionName = propertyKey;
|
|
485
|
-
key = `${ctrlName}.${actionName}`;
|
|
486
|
-
} else {
|
|
487
|
-
const ctrlName = target.name;
|
|
488
|
-
key = `${ctrlName}`;
|
|
489
|
-
}
|
|
490
|
-
if (authorizations.has(key)) {
|
|
491
|
-
throw new Error(`Guard(s) already registered for ${key}`);
|
|
492
|
-
}
|
|
493
|
-
Logger.debug(`Registering guard(s) for ${key}: ${guardClasses.map((c) => c.name).join(", ")}`);
|
|
494
|
-
authorizations.set(key, guardClasses);
|
|
495
|
-
};
|
|
496
|
-
}
|
|
497
|
-
__name(Authorize, "Authorize");
|
|
498
|
-
function getGuardForController(controllerName) {
|
|
499
|
-
const key = `${controllerName}`;
|
|
500
|
-
return authorizations.get(key) ?? [];
|
|
501
|
-
}
|
|
502
|
-
__name(getGuardForController, "getGuardForController");
|
|
503
|
-
function getGuardForControllerAction(controllerName, actionName) {
|
|
504
|
-
const key = `${controllerName}.${actionName}`;
|
|
505
|
-
return authorizations.get(key) ?? [];
|
|
506
|
-
}
|
|
507
|
-
__name(getGuardForControllerAction, "getGuardForControllerAction");
|
|
508
|
-
var authorizations = /* @__PURE__ */ new Map();
|
|
509
|
-
|
|
510
|
-
// src/decorators/method.decorator.ts
|
|
511
|
-
function createRouteDecorator(verb) {
|
|
512
|
-
return (path) => {
|
|
513
|
-
return (target, propertyKey) => {
|
|
514
|
-
const existingRoutes = Reflect.getMetadata(ROUTE_METADATA_KEY, target.constructor) || [];
|
|
515
|
-
const metadata = {
|
|
516
|
-
method: verb,
|
|
517
|
-
path: path.trim().replace(/^\/|\/$/g, ""),
|
|
518
|
-
handler: propertyKey,
|
|
519
|
-
guards: getGuardForControllerAction(target.constructor.__controllerName, propertyKey)
|
|
520
|
-
};
|
|
521
|
-
existingRoutes.push(metadata);
|
|
522
|
-
Reflect.defineMetadata(ROUTE_METADATA_KEY, existingRoutes, target.constructor);
|
|
523
|
-
};
|
|
524
|
-
};
|
|
525
|
-
}
|
|
526
|
-
__name(createRouteDecorator, "createRouteDecorator");
|
|
527
|
-
function getRouteMetadata(target) {
|
|
528
|
-
return Reflect.getMetadata(ROUTE_METADATA_KEY, target) || [];
|
|
529
|
-
}
|
|
530
|
-
__name(getRouteMetadata, "getRouteMetadata");
|
|
531
|
-
var Get = createRouteDecorator("GET");
|
|
532
|
-
var Post = createRouteDecorator("POST");
|
|
533
|
-
var Put = createRouteDecorator("PUT");
|
|
534
|
-
var Patch = createRouteDecorator("PATCH");
|
|
535
|
-
var Delete = createRouteDecorator("DELETE");
|
|
536
|
-
var ROUTE_METADATA_KEY = Symbol("ROUTE_METADATA_KEY");
|
|
537
|
-
|
|
538
|
-
// src/decorators/module.decorator.ts
|
|
539
|
-
function Module(metadata) {
|
|
540
|
-
return (target) => {
|
|
541
|
-
const checkModule = /* @__PURE__ */ __name((arr, arrName) => {
|
|
542
|
-
if (!arr) return;
|
|
543
|
-
for (const clazz of arr) {
|
|
544
|
-
if (!Reflect.getMetadata(MODULE_METADATA_KEY, clazz)) {
|
|
545
|
-
throw new Error(`Class ${clazz.name} in ${arrName} must be decorated with @Module`);
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
}, "checkModule");
|
|
549
|
-
const checkInjectable = /* @__PURE__ */ __name((arr) => {
|
|
550
|
-
if (!arr) return;
|
|
551
|
-
for (const clazz of arr) {
|
|
552
|
-
if (!Reflect.getMetadata(INJECTABLE_METADATA_KEY, clazz)) {
|
|
553
|
-
throw new Error(`Class ${clazz.name} in providers must be decorated with @Injectable`);
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
}, "checkInjectable");
|
|
557
|
-
const checkController = /* @__PURE__ */ __name((arr) => {
|
|
558
|
-
if (!arr) return;
|
|
559
|
-
for (const clazz of arr) {
|
|
560
|
-
if (!Reflect.getMetadata(CONTROLLER_METADATA_KEY, clazz)) {
|
|
561
|
-
throw new Error(`Class ${clazz.name} in controllers must be decorated with @Controller`);
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
}, "checkController");
|
|
565
|
-
checkModule(metadata.imports, "imports");
|
|
566
|
-
checkModule(metadata.exports, "exports");
|
|
567
|
-
checkInjectable(metadata.providers);
|
|
568
|
-
checkController(metadata.controllers);
|
|
569
|
-
Reflect.defineMetadata(MODULE_METADATA_KEY, metadata, target);
|
|
570
|
-
Injectable("singleton")(target);
|
|
571
|
-
};
|
|
572
|
-
}
|
|
573
|
-
__name(Module, "Module");
|
|
574
|
-
function getModuleMetadata(target) {
|
|
575
|
-
return Reflect.getMetadata(MODULE_METADATA_KEY, target);
|
|
576
|
-
}
|
|
577
|
-
__name(getModuleMetadata, "getModuleMetadata");
|
|
578
|
-
var MODULE_METADATA_KEY = Symbol("MODULE_METADATA_KEY");
|
|
579
|
-
|
|
580
588
|
// src/DI/injector-explorer.ts
|
|
581
589
|
var _InjectorExplorer = class _InjectorExplorer {
|
|
582
590
|
/**
|
|
@@ -585,7 +593,6 @@ var _InjectorExplorer = class _InjectorExplorer {
|
|
|
585
593
|
* are listed using this method, they will be injected into the class constructor.
|
|
586
594
|
*/
|
|
587
595
|
static register(target, lifetime) {
|
|
588
|
-
Logger.debug(`Registering ${target.name} as ${lifetime}`);
|
|
589
596
|
if (RootInjector.bindings.has(target)) return RootInjector;
|
|
590
597
|
RootInjector.bindings.set(target, {
|
|
591
598
|
implementation: target,
|
|
@@ -668,7 +675,6 @@ function UseMiddlewares(mdlw) {
|
|
|
668
675
|
if (middlewares.has(key)) {
|
|
669
676
|
throw new Error(`Middlewares(s) already registered for ${key}`);
|
|
670
677
|
}
|
|
671
|
-
Logger.debug(`Registering middleware(s) for ${key}: ${mdlw.map((c) => c.name).join(", ")}`);
|
|
672
678
|
middlewares.set(key, mdlw);
|
|
673
679
|
};
|
|
674
680
|
}
|
|
@@ -908,7 +914,6 @@ var _Router = class _Router {
|
|
|
908
914
|
* @param middleware - The middleware class to register.
|
|
909
915
|
*/
|
|
910
916
|
defineRootMiddleware(middleware) {
|
|
911
|
-
Logger.debug(`Registering root middleware: ${middleware.name}`);
|
|
912
917
|
this.rootMiddlewares.push(middleware);
|
|
913
918
|
return this;
|
|
914
919
|
}
|
|
@@ -919,7 +924,7 @@ var _Router = class _Router {
|
|
|
919
924
|
* @param channelSenderId - The ID of the sender channel to shut down.
|
|
920
925
|
*/
|
|
921
926
|
async handle(request) {
|
|
922
|
-
Logger.
|
|
927
|
+
Logger.comment(`> ${request.method} /${request.path}`);
|
|
923
928
|
const t0 = performance.now();
|
|
924
929
|
const response = {
|
|
925
930
|
requestId: request.id,
|
|
@@ -1224,7 +1229,8 @@ var _NoxApp = class _NoxApp {
|
|
|
1224
1229
|
this.shutdownChannel(senderId);
|
|
1225
1230
|
});
|
|
1226
1231
|
this.messagePorts.clear();
|
|
1227
|
-
|
|
1232
|
+
Logger.info("All windows closed, shutting down application...");
|
|
1233
|
+
await this.app?.dispose();
|
|
1228
1234
|
if (process.platform !== "darwin") {
|
|
1229
1235
|
import_main.app.quit();
|
|
1230
1236
|
}
|
package/dist/noxus.mjs
CHANGED
|
@@ -284,6 +284,107 @@ var RootInjector = new AppInjector("root");
|
|
|
284
284
|
// src/router.ts
|
|
285
285
|
import "reflect-metadata";
|
|
286
286
|
|
|
287
|
+
// src/decorators/guards.decorator.ts
|
|
288
|
+
function Authorize(...guardClasses) {
|
|
289
|
+
return (target, propertyKey) => {
|
|
290
|
+
let key;
|
|
291
|
+
if (propertyKey) {
|
|
292
|
+
const ctrlName = target.constructor.name;
|
|
293
|
+
const actionName = propertyKey;
|
|
294
|
+
key = `${ctrlName}.${actionName}`;
|
|
295
|
+
} else {
|
|
296
|
+
const ctrlName = target.name;
|
|
297
|
+
key = `${ctrlName}`;
|
|
298
|
+
}
|
|
299
|
+
if (authorizations.has(key)) {
|
|
300
|
+
throw new Error(`Guard(s) already registered for ${key}`);
|
|
301
|
+
}
|
|
302
|
+
authorizations.set(key, guardClasses);
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
__name(Authorize, "Authorize");
|
|
306
|
+
function getGuardForController(controllerName) {
|
|
307
|
+
const key = `${controllerName}`;
|
|
308
|
+
return authorizations.get(key) ?? [];
|
|
309
|
+
}
|
|
310
|
+
__name(getGuardForController, "getGuardForController");
|
|
311
|
+
function getGuardForControllerAction(controllerName, actionName) {
|
|
312
|
+
const key = `${controllerName}.${actionName}`;
|
|
313
|
+
return authorizations.get(key) ?? [];
|
|
314
|
+
}
|
|
315
|
+
__name(getGuardForControllerAction, "getGuardForControllerAction");
|
|
316
|
+
var authorizations = /* @__PURE__ */ new Map();
|
|
317
|
+
|
|
318
|
+
// src/decorators/method.decorator.ts
|
|
319
|
+
function createRouteDecorator(verb) {
|
|
320
|
+
return (path) => {
|
|
321
|
+
return (target, propertyKey) => {
|
|
322
|
+
const existingRoutes = Reflect.getMetadata(ROUTE_METADATA_KEY, target.constructor) || [];
|
|
323
|
+
const metadata = {
|
|
324
|
+
method: verb,
|
|
325
|
+
path: path.trim().replace(/^\/|\/$/g, ""),
|
|
326
|
+
handler: propertyKey,
|
|
327
|
+
guards: getGuardForControllerAction(target.constructor.__controllerName, propertyKey)
|
|
328
|
+
};
|
|
329
|
+
existingRoutes.push(metadata);
|
|
330
|
+
Reflect.defineMetadata(ROUTE_METADATA_KEY, existingRoutes, target.constructor);
|
|
331
|
+
};
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
__name(createRouteDecorator, "createRouteDecorator");
|
|
335
|
+
function getRouteMetadata(target) {
|
|
336
|
+
return Reflect.getMetadata(ROUTE_METADATA_KEY, target) || [];
|
|
337
|
+
}
|
|
338
|
+
__name(getRouteMetadata, "getRouteMetadata");
|
|
339
|
+
var Get = createRouteDecorator("GET");
|
|
340
|
+
var Post = createRouteDecorator("POST");
|
|
341
|
+
var Put = createRouteDecorator("PUT");
|
|
342
|
+
var Patch = createRouteDecorator("PATCH");
|
|
343
|
+
var Delete = createRouteDecorator("DELETE");
|
|
344
|
+
var ROUTE_METADATA_KEY = Symbol("ROUTE_METADATA_KEY");
|
|
345
|
+
|
|
346
|
+
// src/decorators/module.decorator.ts
|
|
347
|
+
function Module(metadata) {
|
|
348
|
+
return (target) => {
|
|
349
|
+
const checkModule = /* @__PURE__ */ __name((arr, arrName) => {
|
|
350
|
+
if (!arr) return;
|
|
351
|
+
for (const clazz of arr) {
|
|
352
|
+
if (!Reflect.getMetadata(MODULE_METADATA_KEY, clazz)) {
|
|
353
|
+
throw new Error(`Class ${clazz.name} in ${arrName} must be decorated with @Module`);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}, "checkModule");
|
|
357
|
+
const checkInjectable = /* @__PURE__ */ __name((arr) => {
|
|
358
|
+
if (!arr) return;
|
|
359
|
+
for (const clazz of arr) {
|
|
360
|
+
if (!Reflect.getMetadata(INJECTABLE_METADATA_KEY, clazz)) {
|
|
361
|
+
throw new Error(`Class ${clazz.name} in providers must be decorated with @Injectable`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}, "checkInjectable");
|
|
365
|
+
const checkController = /* @__PURE__ */ __name((arr) => {
|
|
366
|
+
if (!arr) return;
|
|
367
|
+
for (const clazz of arr) {
|
|
368
|
+
if (!Reflect.getMetadata(CONTROLLER_METADATA_KEY, clazz)) {
|
|
369
|
+
throw new Error(`Class ${clazz.name} in controllers must be decorated with @Controller`);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}, "checkController");
|
|
373
|
+
checkModule(metadata.imports, "imports");
|
|
374
|
+
checkModule(metadata.exports, "exports");
|
|
375
|
+
checkInjectable(metadata.providers);
|
|
376
|
+
checkController(metadata.controllers);
|
|
377
|
+
Reflect.defineMetadata(MODULE_METADATA_KEY, metadata, target);
|
|
378
|
+
Injectable("singleton")(target);
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
__name(Module, "Module");
|
|
382
|
+
function getModuleMetadata(target) {
|
|
383
|
+
return Reflect.getMetadata(MODULE_METADATA_KEY, target);
|
|
384
|
+
}
|
|
385
|
+
__name(getModuleMetadata, "getModuleMetadata");
|
|
386
|
+
var MODULE_METADATA_KEY = Symbol("MODULE_METADATA_KEY");
|
|
387
|
+
|
|
287
388
|
// src/utils/logger.ts
|
|
288
389
|
function getPrettyTimestamp() {
|
|
289
390
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -315,7 +416,7 @@ function formattedArgs(prefix, args, color) {
|
|
|
315
416
|
__name(formattedArgs, "formattedArgs");
|
|
316
417
|
function getCallee() {
|
|
317
418
|
const stack = new Error().stack?.split("\n") ?? [];
|
|
318
|
-
const caller = stack[3]?.trim().match(/at (.+?)(?:\..+)? .+$/)?.[1]?.replace("Object", "") || "App";
|
|
419
|
+
const caller = stack[3]?.trim().match(/at (.+?)(?:\..+)? .+$/)?.[1]?.replace("Object", "").replace(/^_/, "") || "App";
|
|
319
420
|
return caller;
|
|
320
421
|
}
|
|
321
422
|
__name(getCallee, "getCallee");
|
|
@@ -323,13 +424,14 @@ function canLog(level) {
|
|
|
323
424
|
return logLevelRank[level] >= logLevelRank[logLevel];
|
|
324
425
|
}
|
|
325
426
|
__name(canLog, "canLog");
|
|
326
|
-
var logLevel = "
|
|
427
|
+
var logLevel = "debug";
|
|
327
428
|
var logLevelRank = {
|
|
328
429
|
debug: 0,
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
430
|
+
comment: 1,
|
|
431
|
+
log: 2,
|
|
432
|
+
info: 3,
|
|
433
|
+
warn: 4,
|
|
434
|
+
error: 5
|
|
333
435
|
};
|
|
334
436
|
(function(Logger2) {
|
|
335
437
|
function setLogLevel(level) {
|
|
@@ -377,6 +479,14 @@ var logLevelRank = {
|
|
|
377
479
|
}
|
|
378
480
|
__name(debug, "debug");
|
|
379
481
|
Logger2.debug = debug;
|
|
482
|
+
function comment(...args) {
|
|
483
|
+
if (!canLog("comment")) return;
|
|
484
|
+
const callee = getCallee();
|
|
485
|
+
const prefix = getLogPrefix(callee, "comment", Logger2.colors.grey);
|
|
486
|
+
console.debug(prefix, ...formattedArgs(prefix, args, Logger2.colors.grey));
|
|
487
|
+
}
|
|
488
|
+
__name(comment, "comment");
|
|
489
|
+
Logger2.comment = comment;
|
|
380
490
|
Logger2.colors = {
|
|
381
491
|
black: "\x1B[0;30m",
|
|
382
492
|
grey: "\x1B[0;37m",
|
|
@@ -398,108 +508,6 @@ var logLevelRank = {
|
|
|
398
508
|
})(Logger || (Logger = {}));
|
|
399
509
|
var Logger;
|
|
400
510
|
|
|
401
|
-
// src/decorators/guards.decorator.ts
|
|
402
|
-
function Authorize(...guardClasses) {
|
|
403
|
-
return (target, propertyKey) => {
|
|
404
|
-
let key;
|
|
405
|
-
if (propertyKey) {
|
|
406
|
-
const ctrlName = target.constructor.name;
|
|
407
|
-
const actionName = propertyKey;
|
|
408
|
-
key = `${ctrlName}.${actionName}`;
|
|
409
|
-
} else {
|
|
410
|
-
const ctrlName = target.name;
|
|
411
|
-
key = `${ctrlName}`;
|
|
412
|
-
}
|
|
413
|
-
if (authorizations.has(key)) {
|
|
414
|
-
throw new Error(`Guard(s) already registered for ${key}`);
|
|
415
|
-
}
|
|
416
|
-
Logger.debug(`Registering guard(s) for ${key}: ${guardClasses.map((c) => c.name).join(", ")}`);
|
|
417
|
-
authorizations.set(key, guardClasses);
|
|
418
|
-
};
|
|
419
|
-
}
|
|
420
|
-
__name(Authorize, "Authorize");
|
|
421
|
-
function getGuardForController(controllerName) {
|
|
422
|
-
const key = `${controllerName}`;
|
|
423
|
-
return authorizations.get(key) ?? [];
|
|
424
|
-
}
|
|
425
|
-
__name(getGuardForController, "getGuardForController");
|
|
426
|
-
function getGuardForControllerAction(controllerName, actionName) {
|
|
427
|
-
const key = `${controllerName}.${actionName}`;
|
|
428
|
-
return authorizations.get(key) ?? [];
|
|
429
|
-
}
|
|
430
|
-
__name(getGuardForControllerAction, "getGuardForControllerAction");
|
|
431
|
-
var authorizations = /* @__PURE__ */ new Map();
|
|
432
|
-
|
|
433
|
-
// src/decorators/method.decorator.ts
|
|
434
|
-
function createRouteDecorator(verb) {
|
|
435
|
-
return (path) => {
|
|
436
|
-
return (target, propertyKey) => {
|
|
437
|
-
const existingRoutes = Reflect.getMetadata(ROUTE_METADATA_KEY, target.constructor) || [];
|
|
438
|
-
const metadata = {
|
|
439
|
-
method: verb,
|
|
440
|
-
path: path.trim().replace(/^\/|\/$/g, ""),
|
|
441
|
-
handler: propertyKey,
|
|
442
|
-
guards: getGuardForControllerAction(target.constructor.__controllerName, propertyKey)
|
|
443
|
-
};
|
|
444
|
-
existingRoutes.push(metadata);
|
|
445
|
-
Reflect.defineMetadata(ROUTE_METADATA_KEY, existingRoutes, target.constructor);
|
|
446
|
-
};
|
|
447
|
-
};
|
|
448
|
-
}
|
|
449
|
-
__name(createRouteDecorator, "createRouteDecorator");
|
|
450
|
-
function getRouteMetadata(target) {
|
|
451
|
-
return Reflect.getMetadata(ROUTE_METADATA_KEY, target) || [];
|
|
452
|
-
}
|
|
453
|
-
__name(getRouteMetadata, "getRouteMetadata");
|
|
454
|
-
var Get = createRouteDecorator("GET");
|
|
455
|
-
var Post = createRouteDecorator("POST");
|
|
456
|
-
var Put = createRouteDecorator("PUT");
|
|
457
|
-
var Patch = createRouteDecorator("PATCH");
|
|
458
|
-
var Delete = createRouteDecorator("DELETE");
|
|
459
|
-
var ROUTE_METADATA_KEY = Symbol("ROUTE_METADATA_KEY");
|
|
460
|
-
|
|
461
|
-
// src/decorators/module.decorator.ts
|
|
462
|
-
function Module(metadata) {
|
|
463
|
-
return (target) => {
|
|
464
|
-
const checkModule = /* @__PURE__ */ __name((arr, arrName) => {
|
|
465
|
-
if (!arr) return;
|
|
466
|
-
for (const clazz of arr) {
|
|
467
|
-
if (!Reflect.getMetadata(MODULE_METADATA_KEY, clazz)) {
|
|
468
|
-
throw new Error(`Class ${clazz.name} in ${arrName} must be decorated with @Module`);
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
}, "checkModule");
|
|
472
|
-
const checkInjectable = /* @__PURE__ */ __name((arr) => {
|
|
473
|
-
if (!arr) return;
|
|
474
|
-
for (const clazz of arr) {
|
|
475
|
-
if (!Reflect.getMetadata(INJECTABLE_METADATA_KEY, clazz)) {
|
|
476
|
-
throw new Error(`Class ${clazz.name} in providers must be decorated with @Injectable`);
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
}, "checkInjectable");
|
|
480
|
-
const checkController = /* @__PURE__ */ __name((arr) => {
|
|
481
|
-
if (!arr) return;
|
|
482
|
-
for (const clazz of arr) {
|
|
483
|
-
if (!Reflect.getMetadata(CONTROLLER_METADATA_KEY, clazz)) {
|
|
484
|
-
throw new Error(`Class ${clazz.name} in controllers must be decorated with @Controller`);
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
}, "checkController");
|
|
488
|
-
checkModule(metadata.imports, "imports");
|
|
489
|
-
checkModule(metadata.exports, "exports");
|
|
490
|
-
checkInjectable(metadata.providers);
|
|
491
|
-
checkController(metadata.controllers);
|
|
492
|
-
Reflect.defineMetadata(MODULE_METADATA_KEY, metadata, target);
|
|
493
|
-
Injectable("singleton")(target);
|
|
494
|
-
};
|
|
495
|
-
}
|
|
496
|
-
__name(Module, "Module");
|
|
497
|
-
function getModuleMetadata(target) {
|
|
498
|
-
return Reflect.getMetadata(MODULE_METADATA_KEY, target);
|
|
499
|
-
}
|
|
500
|
-
__name(getModuleMetadata, "getModuleMetadata");
|
|
501
|
-
var MODULE_METADATA_KEY = Symbol("MODULE_METADATA_KEY");
|
|
502
|
-
|
|
503
511
|
// src/DI/injector-explorer.ts
|
|
504
512
|
var _InjectorExplorer = class _InjectorExplorer {
|
|
505
513
|
/**
|
|
@@ -508,7 +516,6 @@ var _InjectorExplorer = class _InjectorExplorer {
|
|
|
508
516
|
* are listed using this method, they will be injected into the class constructor.
|
|
509
517
|
*/
|
|
510
518
|
static register(target, lifetime) {
|
|
511
|
-
Logger.debug(`Registering ${target.name} as ${lifetime}`);
|
|
512
519
|
if (RootInjector.bindings.has(target)) return RootInjector;
|
|
513
520
|
RootInjector.bindings.set(target, {
|
|
514
521
|
implementation: target,
|
|
@@ -591,7 +598,6 @@ function UseMiddlewares(mdlw) {
|
|
|
591
598
|
if (middlewares.has(key)) {
|
|
592
599
|
throw new Error(`Middlewares(s) already registered for ${key}`);
|
|
593
600
|
}
|
|
594
|
-
Logger.debug(`Registering middleware(s) for ${key}: ${mdlw.map((c) => c.name).join(", ")}`);
|
|
595
601
|
middlewares.set(key, mdlw);
|
|
596
602
|
};
|
|
597
603
|
}
|
|
@@ -831,7 +837,6 @@ var _Router = class _Router {
|
|
|
831
837
|
* @param middleware - The middleware class to register.
|
|
832
838
|
*/
|
|
833
839
|
defineRootMiddleware(middleware) {
|
|
834
|
-
Logger.debug(`Registering root middleware: ${middleware.name}`);
|
|
835
840
|
this.rootMiddlewares.push(middleware);
|
|
836
841
|
return this;
|
|
837
842
|
}
|
|
@@ -842,7 +847,7 @@ var _Router = class _Router {
|
|
|
842
847
|
* @param channelSenderId - The ID of the sender channel to shut down.
|
|
843
848
|
*/
|
|
844
849
|
async handle(request) {
|
|
845
|
-
Logger.
|
|
850
|
+
Logger.comment(`> ${request.method} /${request.path}`);
|
|
846
851
|
const t0 = performance.now();
|
|
847
852
|
const response = {
|
|
848
853
|
requestId: request.id,
|
|
@@ -1147,7 +1152,8 @@ var _NoxApp = class _NoxApp {
|
|
|
1147
1152
|
this.shutdownChannel(senderId);
|
|
1148
1153
|
});
|
|
1149
1154
|
this.messagePorts.clear();
|
|
1150
|
-
|
|
1155
|
+
Logger.info("All windows closed, shutting down application...");
|
|
1156
|
+
await this.app?.dispose();
|
|
1151
1157
|
if (process.platform !== "darwin") {
|
|
1152
1158
|
app.quit();
|
|
1153
1159
|
}
|
package/package.json
CHANGED
|
@@ -23,7 +23,6 @@ export class InjectorExplorer {
|
|
|
23
23
|
* are listed using this method, they will be injected into the class constructor.
|
|
24
24
|
*/
|
|
25
25
|
public static register(target: Type<unknown>, lifetime: Lifetime): typeof RootInjector {
|
|
26
|
-
Logger.debug(`Registering ${target.name} as ${lifetime}`);
|
|
27
26
|
if(RootInjector.bindings.has(target)) // already registered
|
|
28
27
|
return RootInjector;
|
|
29
28
|
|
package/src/app.ts
CHANGED
|
@@ -46,8 +46,6 @@ export function Authorize(...guardClasses: Type<IGuard>[]): MethodDecorator & Cl
|
|
|
46
46
|
throw new Error(`Guard(s) already registered for ${key}`);
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
Logger.debug(`Registering guard(s) for ${key}: ${guardClasses.map(c => c.name).join(', ')}`);
|
|
50
|
-
|
|
51
49
|
authorizations.set(key, guardClasses);
|
|
52
50
|
};
|
|
53
51
|
}
|
|
@@ -50,8 +50,6 @@ export function UseMiddlewares(mdlw: Type<IMiddleware>[]): ClassDecorator & Meth
|
|
|
50
50
|
throw new Error(`Middlewares(s) already registered for ${key}`);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
Logger.debug(`Registering middleware(s) for ${key}: ${mdlw.map(c => c.name).join(', ')}`);
|
|
54
|
-
|
|
55
53
|
middlewares.set(key, mdlw);
|
|
56
54
|
};
|
|
57
55
|
}
|
package/src/router.ts
CHANGED
|
@@ -110,7 +110,6 @@ export class Router {
|
|
|
110
110
|
* @param middleware - The middleware class to register.
|
|
111
111
|
*/
|
|
112
112
|
public defineRootMiddleware(middleware: Type<IMiddleware>): Router {
|
|
113
|
-
Logger.debug(`Registering root middleware: ${middleware.name}`);
|
|
114
113
|
this.rootMiddlewares.push(middleware);
|
|
115
114
|
return this;
|
|
116
115
|
}
|
|
@@ -122,7 +121,7 @@ export class Router {
|
|
|
122
121
|
* @param channelSenderId - The ID of the sender channel to shut down.
|
|
123
122
|
*/
|
|
124
123
|
public async handle(request: Request): Promise<IResponse> {
|
|
125
|
-
Logger.
|
|
124
|
+
Logger.comment(`> ${request.method} /${request.path}`);
|
|
126
125
|
|
|
127
126
|
const t0 = performance.now();
|
|
128
127
|
|
package/src/utils/logger.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
/**
|
|
8
8
|
* Logger is a utility class for logging messages to the console.
|
|
9
9
|
*/
|
|
10
|
-
export type LogLevel = 'log' | 'info' | 'warn' | 'error' | 'debug';
|
|
10
|
+
export type LogLevel = 'log' | 'info' | 'warn' | 'error' | 'debug' | 'comment';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Returns a formatted timestamp for logging.
|
|
@@ -84,7 +84,13 @@ function formattedArgs(prefix: string, args: any[], color: string): any[] {
|
|
|
84
84
|
*/
|
|
85
85
|
function getCallee(): string {
|
|
86
86
|
const stack = new Error().stack?.split('\n') ?? [];
|
|
87
|
-
const caller = stack[3]
|
|
87
|
+
const caller = stack[3]
|
|
88
|
+
?.trim()
|
|
89
|
+
.match(/at (.+?)(?:\..+)? .+$/)
|
|
90
|
+
?.[1]
|
|
91
|
+
?.replace('Object', '')
|
|
92
|
+
.replace(/^_/, '')
|
|
93
|
+
|| "App";
|
|
88
94
|
return caller;
|
|
89
95
|
}
|
|
90
96
|
|
|
@@ -99,14 +105,15 @@ function canLog(level: LogLevel): boolean {
|
|
|
99
105
|
}
|
|
100
106
|
|
|
101
107
|
|
|
102
|
-
let logLevel: LogLevel = '
|
|
108
|
+
let logLevel: LogLevel = 'debug';
|
|
103
109
|
|
|
104
110
|
const logLevelRank: Record<LogLevel, number> = {
|
|
105
111
|
debug: 0,
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
112
|
+
comment: 1,
|
|
113
|
+
log: 2,
|
|
114
|
+
info: 3,
|
|
115
|
+
warn: 4,
|
|
116
|
+
error: 5,
|
|
110
117
|
};
|
|
111
118
|
|
|
112
119
|
export namespace Logger {
|
|
@@ -196,6 +203,21 @@ export namespace Logger {
|
|
|
196
203
|
console.debug(prefix, ...formattedArgs(prefix, args, colors.purple));
|
|
197
204
|
}
|
|
198
205
|
|
|
206
|
+
/**
|
|
207
|
+
* Logs a message to the console with log level COMMENT.
|
|
208
|
+
* This function formats the message with a timestamp, process ID, and the name of the caller function or class.
|
|
209
|
+
* It uses different colors for different log levels to enhance readability.
|
|
210
|
+
* @param args The arguments to log.
|
|
211
|
+
*/
|
|
212
|
+
export function comment(...args: any[]): void {
|
|
213
|
+
if(!canLog('comment'))
|
|
214
|
+
return;
|
|
215
|
+
|
|
216
|
+
const callee = getCallee();
|
|
217
|
+
const prefix = getLogPrefix(callee, "comment", colors.grey);
|
|
218
|
+
console.debug(prefix, ...formattedArgs(prefix, args, colors.grey));
|
|
219
|
+
}
|
|
220
|
+
|
|
199
221
|
|
|
200
222
|
export const colors = {
|
|
201
223
|
black: '\x1b[0;30m',
|