@navios/core 0.1.0 → 0.1.1

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/index.js CHANGED
@@ -78,6 +78,7 @@ __export(src_exports, {
78
78
  AttributeFactory: () => AttributeFactory,
79
79
  BadRequestException: () => BadRequestException,
80
80
  ConflictException: () => ConflictException,
81
+ ConsoleLogger: () => ConsoleLogger,
81
82
  Controller: () => Controller,
82
83
  ControllerAdapterService: () => ControllerAdapterService,
83
84
  ControllerMetadataKey: () => ControllerMetadataKey,
@@ -101,12 +102,19 @@ __export(src_exports, {
101
102
  InstanceExpired: () => InstanceExpired,
102
103
  InstanceNotFound: () => InstanceNotFound,
103
104
  InternalServerErrorException: () => InternalServerErrorException,
105
+ LOG_LEVELS: () => LOG_LEVELS,
106
+ Logger: () => Logger,
107
+ LoggerFactory: () => LoggerFactory,
108
+ LoggerInjectionToken: () => LoggerInjectionToken,
109
+ LoggerInstance: () => LoggerInstance,
110
+ LoggerOptions: () => LoggerOptions,
104
111
  Module: () => Module,
105
112
  ModuleLoaderService: () => ModuleLoaderService,
106
113
  ModuleMetadataKey: () => ModuleMetadataKey,
107
114
  NaviosApplication: () => NaviosApplication,
108
115
  NaviosFactory: () => NaviosFactory,
109
116
  NotFoundException: () => NotFoundException,
117
+ PinoWrapper: () => PinoWrapper,
110
118
  Reply: () => Reply,
111
119
  Request: () => Request,
112
120
  ServiceLocator: () => ServiceLocator,
@@ -117,8 +125,11 @@ __export(src_exports, {
117
125
  UnauthorizedException: () => UnauthorizedException,
118
126
  UnknownError: () => UnknownError,
119
127
  UseGuards: () => UseGuards,
128
+ addLeadingSlash: () => addLeadingSlash,
129
+ clc: () => clc,
120
130
  extractControllerMetadata: () => extractControllerMetadata,
121
131
  extractModuleMetadata: () => extractModuleMetadata,
132
+ filterLogLevels: () => filterLogLevels,
122
133
  getAllEndpointMetadata: () => getAllEndpointMetadata,
123
134
  getControllerMetadata: () => getControllerMetadata,
124
135
  getEndpointMetadata: () => getEndpointMetadata,
@@ -128,10 +139,25 @@ __export(src_exports, {
128
139
  hasControllerMetadata: () => hasControllerMetadata,
129
140
  hasModuleMetadata: () => hasModuleMetadata,
130
141
  inject: () => inject,
142
+ isConstructor: () => isConstructor,
143
+ isEmpty: () => isEmpty,
144
+ isFunction: () => isFunction,
145
+ isLogLevel: () => isLogLevel,
146
+ isLogLevelEnabled: () => isLogLevelEnabled,
147
+ isNil: () => isNil,
148
+ isNumber: () => isNumber,
149
+ isObject: () => isObject,
150
+ isPlainObject: () => isPlainObject,
151
+ isString: () => isString,
152
+ isSymbol: () => isSymbol,
153
+ isUndefined: () => isUndefined,
154
+ normalizePath: () => normalizePath,
131
155
  override: () => override,
132
156
  provideServiceLocator: () => provideServiceLocator,
133
157
  setPromiseCollector: () => setPromiseCollector,
134
- syncInject: () => syncInject
158
+ stripEndSlash: () => stripEndSlash,
159
+ syncInject: () => syncInject,
160
+ yellow: () => yellow
135
161
  });
136
162
  module.exports = __toCommonJS(src_exports);
137
163
 
@@ -253,6 +279,7 @@ var InjectableScope = /* @__PURE__ */ ((InjectableScope2) => {
253
279
 
254
280
  // packages/core/src/service-locator/injection-token.mts
255
281
  var import_crypto = require("crypto");
282
+ var import_zod = require("zod");
256
283
  var InjectionToken = class _InjectionToken {
257
284
  constructor(name2, schema) {
258
285
  this.name = name2;
@@ -1241,6 +1268,655 @@ var ConflictException = class extends HttpException {
1241
1268
  }
1242
1269
  };
1243
1270
 
1271
+ // packages/core/src/logger/utils/cli-colors.util.mts
1272
+ var isColorAllowed = () => !process.env.NO_COLOR;
1273
+ var colorIfAllowed = (colorFn) => (text) => isColorAllowed() ? colorFn(text) : text;
1274
+ var clc = {
1275
+ bold: colorIfAllowed((text) => `\x1B[1m${text}\x1B[0m`),
1276
+ green: colorIfAllowed((text) => `\x1B[32m${text}\x1B[39m`),
1277
+ yellow: colorIfAllowed((text) => `\x1B[33m${text}\x1B[39m`),
1278
+ red: colorIfAllowed((text) => `\x1B[31m${text}\x1B[39m`),
1279
+ magentaBright: colorIfAllowed((text) => `\x1B[95m${text}\x1B[39m`),
1280
+ cyanBright: colorIfAllowed((text) => `\x1B[96m${text}\x1B[39m`)
1281
+ };
1282
+ var yellow = colorIfAllowed(
1283
+ (text) => `\x1B[38;5;3m${text}\x1B[39m`
1284
+ );
1285
+
1286
+ // packages/core/src/logger/log-levels.mts
1287
+ var LOG_LEVELS = [
1288
+ "verbose",
1289
+ "debug",
1290
+ "log",
1291
+ "warn",
1292
+ "error",
1293
+ "fatal"
1294
+ ];
1295
+
1296
+ // packages/core/src/logger/utils/is-log-level.util.mts
1297
+ function isLogLevel(maybeLogLevel) {
1298
+ return LOG_LEVELS.includes(maybeLogLevel);
1299
+ }
1300
+
1301
+ // packages/core/src/logger/utils/filter-log-levelts.util.mts
1302
+ function filterLogLevels(parseableString = "") {
1303
+ const sanitizedString = parseableString.replaceAll(" ", "").toLowerCase();
1304
+ if (sanitizedString[0] === ">") {
1305
+ const orEqual = sanitizedString[1] === "=";
1306
+ const logLevelIndex = LOG_LEVELS.indexOf(
1307
+ sanitizedString.substring(orEqual ? 2 : 1)
1308
+ );
1309
+ if (logLevelIndex === -1) {
1310
+ throw new Error(`parse error (unknown log level): ${sanitizedString}`);
1311
+ }
1312
+ return LOG_LEVELS.slice(orEqual ? logLevelIndex : logLevelIndex + 1);
1313
+ } else if (sanitizedString.includes(",")) {
1314
+ return sanitizedString.split(",").filter(isLogLevel);
1315
+ }
1316
+ return isLogLevel(sanitizedString) ? [sanitizedString] : LOG_LEVELS;
1317
+ }
1318
+
1319
+ // packages/core/src/logger/utils/is-log-level-enabled.mts
1320
+ var LOG_LEVEL_VALUES = {
1321
+ verbose: 0,
1322
+ debug: 1,
1323
+ log: 2,
1324
+ warn: 3,
1325
+ error: 4,
1326
+ fatal: 5
1327
+ };
1328
+ function isLogLevelEnabled(targetLevel, logLevels) {
1329
+ var _a;
1330
+ if (!logLevels || Array.isArray(logLevels) && (logLevels == null ? void 0 : logLevels.length) === 0) {
1331
+ return false;
1332
+ }
1333
+ if (logLevels.includes(targetLevel)) {
1334
+ return true;
1335
+ }
1336
+ const highestLogLevelValue = (_a = logLevels.map((level) => LOG_LEVEL_VALUES[level]).sort((a, b) => b - a)) == null ? void 0 : _a[0];
1337
+ const targetLevelValue = LOG_LEVEL_VALUES[targetLevel];
1338
+ return targetLevelValue >= highestLogLevelValue;
1339
+ }
1340
+
1341
+ // packages/core/src/logger/utils/shared.utils.mts
1342
+ var isUndefined = (obj) => typeof obj === "undefined";
1343
+ var isObject = (fn) => !isNil(fn) && typeof fn === "object";
1344
+ var isPlainObject = (fn) => {
1345
+ if (!isObject(fn)) {
1346
+ return false;
1347
+ }
1348
+ const proto = Object.getPrototypeOf(fn);
1349
+ if (proto === null) {
1350
+ return true;
1351
+ }
1352
+ const ctor = Object.prototype.hasOwnProperty.call(proto, "constructor") && proto.constructor;
1353
+ return typeof ctor === "function" && ctor instanceof ctor && Function.prototype.toString.call(ctor) === Function.prototype.toString.call(Object);
1354
+ };
1355
+ var addLeadingSlash = (path) => path && typeof path === "string" ? path.charAt(0) !== "/" && path.substring(0, 2) !== "{/" ? "/" + path : path : "";
1356
+ var normalizePath = (path) => path ? path.startsWith("/") ? ("/" + path.replace(/\/+$/, "")).replace(/\/+/g, "/") : "/" + path.replace(/\/+$/, "") : "/";
1357
+ var stripEndSlash = (path) => path[path.length - 1] === "/" ? path.slice(0, path.length - 1) : path;
1358
+ var isFunction = (val) => typeof val === "function";
1359
+ var isString = (val) => typeof val === "string";
1360
+ var isNumber = (val) => typeof val === "number";
1361
+ var isConstructor = (val) => val === "constructor";
1362
+ var isNil = (val) => isUndefined(val) || val === null;
1363
+ var isEmpty = (array) => !(array && array.length > 0);
1364
+ var isSymbol = (val) => typeof val === "symbol";
1365
+
1366
+ // packages/core/src/logger/console-logger.service.mts
1367
+ var import_util = require("util");
1368
+ var DEFAULT_DEPTH = 5;
1369
+ var DEFAULT_LOG_LEVELS = [
1370
+ "log",
1371
+ "error",
1372
+ "warn",
1373
+ "debug",
1374
+ "verbose",
1375
+ "fatal"
1376
+ ];
1377
+ var dateTimeFormatter = new Intl.DateTimeFormat(void 0, {
1378
+ year: "numeric",
1379
+ hour: "numeric",
1380
+ minute: "numeric",
1381
+ second: "numeric",
1382
+ day: "2-digit",
1383
+ month: "2-digit"
1384
+ });
1385
+ var _ConsoleLogger_decorators, _init;
1386
+ _ConsoleLogger_decorators = [Injectable()];
1387
+ var _ConsoleLogger = class _ConsoleLogger {
1388
+ /**
1389
+ * The options of the logger.
1390
+ */
1391
+ options;
1392
+ /**
1393
+ * The context of the logger (can be set manually or automatically inferred).
1394
+ */
1395
+ context;
1396
+ /**
1397
+ * The original context of the logger (set in the constructor).
1398
+ */
1399
+ originalContext;
1400
+ /**
1401
+ * The options used for the "inspect" method.
1402
+ */
1403
+ inspectOptions;
1404
+ /**
1405
+ * The last timestamp at which the log message was printed.
1406
+ */
1407
+ static lastTimestampAt;
1408
+ constructor(contextOrOptions, options) {
1409
+ let [context, opts] = isString(contextOrOptions) ? [contextOrOptions, options] : options ? [void 0, options] : [contextOrOptions == null ? void 0 : contextOrOptions.context, contextOrOptions];
1410
+ opts = opts ?? {};
1411
+ opts.logLevels ??= DEFAULT_LOG_LEVELS;
1412
+ opts.colors ??= opts.colors ?? (opts.json ? false : true);
1413
+ opts.prefix ??= "Navios";
1414
+ this.options = opts;
1415
+ this.inspectOptions = this.getInspectOptions();
1416
+ if (context) {
1417
+ this.context = context;
1418
+ this.originalContext = context;
1419
+ }
1420
+ }
1421
+ log(message, ...optionalParams) {
1422
+ if (!this.isLevelEnabled("log")) {
1423
+ return;
1424
+ }
1425
+ const { messages, context } = this.getContextAndMessagesToPrint([
1426
+ message,
1427
+ ...optionalParams
1428
+ ]);
1429
+ this.printMessages(messages, context, "log");
1430
+ }
1431
+ error(message, ...optionalParams) {
1432
+ if (!this.isLevelEnabled("error")) {
1433
+ return;
1434
+ }
1435
+ const { messages, context, stack } = this.getContextAndStackAndMessagesToPrint([message, ...optionalParams]);
1436
+ this.printMessages(messages, context, "error", "stderr", stack);
1437
+ this.printStackTrace(stack);
1438
+ }
1439
+ warn(message, ...optionalParams) {
1440
+ if (!this.isLevelEnabled("warn")) {
1441
+ return;
1442
+ }
1443
+ const { messages, context } = this.getContextAndMessagesToPrint([
1444
+ message,
1445
+ ...optionalParams
1446
+ ]);
1447
+ this.printMessages(messages, context, "warn");
1448
+ }
1449
+ debug(message, ...optionalParams) {
1450
+ if (!this.isLevelEnabled("debug")) {
1451
+ return;
1452
+ }
1453
+ const { messages, context } = this.getContextAndMessagesToPrint([
1454
+ message,
1455
+ ...optionalParams
1456
+ ]);
1457
+ this.printMessages(messages, context, "debug");
1458
+ }
1459
+ verbose(message, ...optionalParams) {
1460
+ if (!this.isLevelEnabled("verbose")) {
1461
+ return;
1462
+ }
1463
+ const { messages, context } = this.getContextAndMessagesToPrint([
1464
+ message,
1465
+ ...optionalParams
1466
+ ]);
1467
+ this.printMessages(messages, context, "verbose");
1468
+ }
1469
+ fatal(message, ...optionalParams) {
1470
+ if (!this.isLevelEnabled("fatal")) {
1471
+ return;
1472
+ }
1473
+ const { messages, context } = this.getContextAndMessagesToPrint([
1474
+ message,
1475
+ ...optionalParams
1476
+ ]);
1477
+ this.printMessages(messages, context, "fatal");
1478
+ }
1479
+ /**
1480
+ * Set log levels
1481
+ * @param levels log levels
1482
+ */
1483
+ setLogLevels(levels) {
1484
+ if (!this.options) {
1485
+ this.options = {};
1486
+ }
1487
+ this.options.logLevels = levels;
1488
+ }
1489
+ /**
1490
+ * Set logger context
1491
+ * @param context context
1492
+ */
1493
+ setContext(context) {
1494
+ this.context = context;
1495
+ }
1496
+ /**
1497
+ * Resets the logger context to the value that was passed in the constructor.
1498
+ */
1499
+ resetContext() {
1500
+ this.context = this.originalContext;
1501
+ }
1502
+ isLevelEnabled(level) {
1503
+ var _a;
1504
+ const logLevels = (_a = this.options) == null ? void 0 : _a.logLevels;
1505
+ return isLogLevelEnabled(level, logLevels);
1506
+ }
1507
+ getTimestamp() {
1508
+ return dateTimeFormatter.format(Date.now());
1509
+ }
1510
+ printMessages(messages, context = "", logLevel = "log", writeStreamType, errorStack) {
1511
+ messages.forEach((message) => {
1512
+ if (this.options.json) {
1513
+ this.printAsJson(message, {
1514
+ context,
1515
+ logLevel,
1516
+ writeStreamType,
1517
+ errorStack
1518
+ });
1519
+ return;
1520
+ }
1521
+ const pidMessage = this.formatPid(process.pid);
1522
+ const contextMessage = this.formatContext(context);
1523
+ const timestampDiff = this.updateAndGetTimestampDiff();
1524
+ const formattedLogLevel = logLevel.toUpperCase().padStart(7, " ");
1525
+ const formattedMessage = this.formatMessage(
1526
+ logLevel,
1527
+ message,
1528
+ pidMessage,
1529
+ formattedLogLevel,
1530
+ contextMessage,
1531
+ timestampDiff
1532
+ );
1533
+ process[writeStreamType ?? "stdout"].write(formattedMessage);
1534
+ });
1535
+ }
1536
+ printAsJson(message, options) {
1537
+ const logObject = {
1538
+ level: options.logLevel,
1539
+ pid: process.pid,
1540
+ timestamp: Date.now(),
1541
+ message
1542
+ };
1543
+ if (options.context) {
1544
+ logObject.context = options.context;
1545
+ }
1546
+ if (options.errorStack) {
1547
+ logObject.stack = options.errorStack;
1548
+ }
1549
+ const formattedMessage = !this.options.colors && this.inspectOptions.compact === true ? JSON.stringify(logObject, this.stringifyReplacer) : (0, import_util.inspect)(logObject, this.inspectOptions);
1550
+ process[options.writeStreamType ?? "stdout"].write(`${formattedMessage}
1551
+ `);
1552
+ }
1553
+ formatPid(pid) {
1554
+ return `[${this.options.prefix}] ${pid} - `;
1555
+ }
1556
+ formatContext(context) {
1557
+ if (!context) {
1558
+ return "";
1559
+ }
1560
+ context = `[${context}] `;
1561
+ return this.options.colors ? yellow(context) : context;
1562
+ }
1563
+ formatMessage(logLevel, message, pidMessage, formattedLogLevel, contextMessage, timestampDiff) {
1564
+ const output = this.stringifyMessage(message, logLevel);
1565
+ pidMessage = this.colorize(pidMessage, logLevel);
1566
+ formattedLogLevel = this.colorize(formattedLogLevel, logLevel);
1567
+ return `${pidMessage}${this.getTimestamp()} ${formattedLogLevel} ${contextMessage}${output}${timestampDiff}
1568
+ `;
1569
+ }
1570
+ stringifyMessage(message, logLevel) {
1571
+ if (isFunction(message)) {
1572
+ const messageAsStr = Function.prototype.toString.call(message);
1573
+ const isClass = messageAsStr.startsWith("class ");
1574
+ if (isClass) {
1575
+ return this.stringifyMessage(message.name, logLevel);
1576
+ }
1577
+ return this.stringifyMessage(message(), logLevel);
1578
+ }
1579
+ if (typeof message === "string") {
1580
+ return this.colorize(message, logLevel);
1581
+ }
1582
+ const outputText = (0, import_util.inspect)(message, this.inspectOptions);
1583
+ if (isPlainObject(message)) {
1584
+ return `Object(${Object.keys(message).length}) ${outputText}`;
1585
+ }
1586
+ if (Array.isArray(message)) {
1587
+ return `Array(${message.length}) ${outputText}`;
1588
+ }
1589
+ return outputText;
1590
+ }
1591
+ colorize(message, logLevel) {
1592
+ if (!this.options.colors || this.options.json) {
1593
+ return message;
1594
+ }
1595
+ const color = this.getColorByLogLevel(logLevel);
1596
+ return color(message);
1597
+ }
1598
+ printStackTrace(stack) {
1599
+ if (!stack || this.options.json) {
1600
+ return;
1601
+ }
1602
+ process.stderr.write(`${stack}
1603
+ `);
1604
+ }
1605
+ updateAndGetTimestampDiff() {
1606
+ var _a;
1607
+ const includeTimestamp = _ConsoleLogger.lastTimestampAt && ((_a = this.options) == null ? void 0 : _a.timestamp);
1608
+ const result = includeTimestamp ? this.formatTimestampDiff(Date.now() - _ConsoleLogger.lastTimestampAt) : "";
1609
+ _ConsoleLogger.lastTimestampAt = Date.now();
1610
+ return result;
1611
+ }
1612
+ formatTimestampDiff(timestampDiff) {
1613
+ const formattedDiff = ` +${timestampDiff}ms`;
1614
+ return this.options.colors ? yellow(formattedDiff) : formattedDiff;
1615
+ }
1616
+ getInspectOptions() {
1617
+ let breakLength = this.options.breakLength;
1618
+ if (typeof breakLength === "undefined") {
1619
+ breakLength = this.options.colors ? this.options.compact ? Infinity : void 0 : this.options.compact === false ? void 0 : Infinity;
1620
+ }
1621
+ const inspectOptions = {
1622
+ depth: this.options.depth ?? DEFAULT_DEPTH,
1623
+ sorted: this.options.sorted,
1624
+ showHidden: this.options.showHidden,
1625
+ compact: this.options.compact ?? (this.options.json ? true : false),
1626
+ colors: this.options.colors,
1627
+ breakLength
1628
+ };
1629
+ if (this.options.maxArrayLength) {
1630
+ inspectOptions.maxArrayLength = this.options.maxArrayLength;
1631
+ }
1632
+ if (this.options.maxStringLength) {
1633
+ inspectOptions.maxStringLength = this.options.maxStringLength;
1634
+ }
1635
+ return inspectOptions;
1636
+ }
1637
+ stringifyReplacer(key, value) {
1638
+ if (typeof value === "bigint") {
1639
+ return value.toString();
1640
+ }
1641
+ if (typeof value === "symbol") {
1642
+ return value.toString();
1643
+ }
1644
+ if (value instanceof Map || value instanceof Set || value instanceof Error) {
1645
+ return `${(0, import_util.inspect)(value, this.inspectOptions)}`;
1646
+ }
1647
+ return value;
1648
+ }
1649
+ getContextAndMessagesToPrint(args) {
1650
+ if ((args == null ? void 0 : args.length) <= 1) {
1651
+ return { messages: args, context: this.context };
1652
+ }
1653
+ const lastElement = args[args.length - 1];
1654
+ const isContext = isString(lastElement);
1655
+ if (!isContext) {
1656
+ return { messages: args, context: this.context };
1657
+ }
1658
+ return {
1659
+ context: lastElement,
1660
+ messages: args.slice(0, args.length - 1)
1661
+ };
1662
+ }
1663
+ getContextAndStackAndMessagesToPrint(args) {
1664
+ if (args.length === 2) {
1665
+ return this.isStackFormat(args[1]) ? {
1666
+ messages: [args[0]],
1667
+ stack: args[1],
1668
+ context: this.context
1669
+ } : {
1670
+ messages: [args[0]],
1671
+ context: args[1]
1672
+ };
1673
+ }
1674
+ const { messages, context } = this.getContextAndMessagesToPrint(args);
1675
+ if ((messages == null ? void 0 : messages.length) <= 1) {
1676
+ return { messages, context };
1677
+ }
1678
+ const lastElement = messages[messages.length - 1];
1679
+ const isStack = isString(lastElement);
1680
+ if (!isStack && !isUndefined(lastElement)) {
1681
+ return { messages, context };
1682
+ }
1683
+ return {
1684
+ stack: lastElement,
1685
+ messages: messages.slice(0, messages.length - 1),
1686
+ context
1687
+ };
1688
+ }
1689
+ isStackFormat(stack) {
1690
+ if (!isString(stack) && !isUndefined(stack)) {
1691
+ return false;
1692
+ }
1693
+ return /^(.)+\n\s+at .+:\d+:\d+/.test(stack);
1694
+ }
1695
+ getColorByLogLevel(level) {
1696
+ switch (level) {
1697
+ case "debug":
1698
+ return clc.magentaBright;
1699
+ case "warn":
1700
+ return clc.yellow;
1701
+ case "error":
1702
+ return clc.red;
1703
+ case "verbose":
1704
+ return clc.cyanBright;
1705
+ case "fatal":
1706
+ return clc.bold;
1707
+ default:
1708
+ return clc.green;
1709
+ }
1710
+ }
1711
+ };
1712
+ _init = __decoratorStart(null);
1713
+ _ConsoleLogger = __decorateElement(_init, 0, "ConsoleLogger", _ConsoleLogger_decorators, _ConsoleLogger);
1714
+ __runInitializers(_init, 1, _ConsoleLogger);
1715
+ var ConsoleLogger = _ConsoleLogger;
1716
+
1717
+ // packages/core/src/logger/logger.factory.mts
1718
+ var import_zod2 = require("zod");
1719
+
1720
+ // packages/core/src/logger/logger.service.mts
1721
+ var DEFAULT_LOGGER = new ConsoleLogger();
1722
+ var dateTimeFormatter2 = new Intl.DateTimeFormat(void 0, {
1723
+ year: "numeric",
1724
+ hour: "numeric",
1725
+ minute: "numeric",
1726
+ second: "numeric",
1727
+ day: "2-digit",
1728
+ month: "2-digit"
1729
+ });
1730
+ var _LoggerInstance_decorators, _init2;
1731
+ _LoggerInstance_decorators = [Injectable()];
1732
+ var _LoggerInstance = class _LoggerInstance {
1733
+ constructor(context, options = {}) {
1734
+ this.context = context;
1735
+ this.options = options;
1736
+ }
1737
+ static staticInstanceRef = DEFAULT_LOGGER;
1738
+ static logLevels;
1739
+ localInstanceRef;
1740
+ get localInstance() {
1741
+ if (_LoggerInstance.staticInstanceRef === DEFAULT_LOGGER) {
1742
+ return this.registerLocalInstanceRef();
1743
+ } else if (_LoggerInstance.staticInstanceRef instanceof _LoggerInstance) {
1744
+ const prototype = Object.getPrototypeOf(_LoggerInstance.staticInstanceRef);
1745
+ if (prototype.constructor === _LoggerInstance) {
1746
+ return this.registerLocalInstanceRef();
1747
+ }
1748
+ }
1749
+ return _LoggerInstance.staticInstanceRef;
1750
+ }
1751
+ error(message, ...optionalParams) {
1752
+ var _a;
1753
+ optionalParams = this.context ? (optionalParams.length ? optionalParams : [void 0]).concat(
1754
+ this.context
1755
+ ) : optionalParams;
1756
+ (_a = this.localInstance) == null ? void 0 : _a.error(message, ...optionalParams);
1757
+ }
1758
+ log(message, ...optionalParams) {
1759
+ var _a;
1760
+ optionalParams = this.context ? optionalParams.concat(this.context) : optionalParams;
1761
+ (_a = this.localInstance) == null ? void 0 : _a.log(message, ...optionalParams);
1762
+ }
1763
+ warn(message, ...optionalParams) {
1764
+ var _a;
1765
+ optionalParams = this.context ? optionalParams.concat(this.context) : optionalParams;
1766
+ (_a = this.localInstance) == null ? void 0 : _a.warn(message, ...optionalParams);
1767
+ }
1768
+ debug(message, ...optionalParams) {
1769
+ var _a, _b;
1770
+ optionalParams = this.context ? optionalParams.concat(this.context) : optionalParams;
1771
+ (_b = (_a = this.localInstance) == null ? void 0 : _a.debug) == null ? void 0 : _b.call(_a, message, ...optionalParams);
1772
+ }
1773
+ verbose(message, ...optionalParams) {
1774
+ var _a, _b;
1775
+ optionalParams = this.context ? optionalParams.concat(this.context) : optionalParams;
1776
+ (_b = (_a = this.localInstance) == null ? void 0 : _a.verbose) == null ? void 0 : _b.call(_a, message, ...optionalParams);
1777
+ }
1778
+ fatal(message, ...optionalParams) {
1779
+ var _a, _b;
1780
+ optionalParams = this.context ? optionalParams.concat(this.context) : optionalParams;
1781
+ (_b = (_a = this.localInstance) == null ? void 0 : _a.fatal) == null ? void 0 : _b.call(_a, message, ...optionalParams);
1782
+ }
1783
+ static error(message, ...optionalParams) {
1784
+ var _a;
1785
+ (_a = this.staticInstanceRef) == null ? void 0 : _a.error(message, ...optionalParams);
1786
+ }
1787
+ static log(message, ...optionalParams) {
1788
+ var _a;
1789
+ (_a = this.staticInstanceRef) == null ? void 0 : _a.log(message, ...optionalParams);
1790
+ }
1791
+ static warn(message, ...optionalParams) {
1792
+ var _a;
1793
+ (_a = this.staticInstanceRef) == null ? void 0 : _a.warn(message, ...optionalParams);
1794
+ }
1795
+ static debug(message, ...optionalParams) {
1796
+ var _a, _b;
1797
+ (_b = (_a = this.staticInstanceRef) == null ? void 0 : _a.debug) == null ? void 0 : _b.call(_a, message, ...optionalParams);
1798
+ }
1799
+ static verbose(message, ...optionalParams) {
1800
+ var _a, _b;
1801
+ (_b = (_a = this.staticInstanceRef) == null ? void 0 : _a.verbose) == null ? void 0 : _b.call(_a, message, ...optionalParams);
1802
+ }
1803
+ static fatal(message, ...optionalParams) {
1804
+ var _a, _b;
1805
+ (_b = (_a = this.staticInstanceRef) == null ? void 0 : _a.fatal) == null ? void 0 : _b.call(_a, message, ...optionalParams);
1806
+ }
1807
+ static getTimestamp() {
1808
+ return dateTimeFormatter2.format(Date.now());
1809
+ }
1810
+ static overrideLogger(logger) {
1811
+ var _a, _b;
1812
+ console.log(logger);
1813
+ if (Array.isArray(logger)) {
1814
+ _LoggerInstance.logLevels = logger;
1815
+ return (_b = (_a = this.staticInstanceRef) == null ? void 0 : _a.setLogLevels) == null ? void 0 : _b.call(_a, logger);
1816
+ }
1817
+ if (isObject(logger)) {
1818
+ this.staticInstanceRef = logger;
1819
+ } else {
1820
+ this.staticInstanceRef = void 0;
1821
+ }
1822
+ }
1823
+ static isLevelEnabled(level) {
1824
+ const logLevels = _LoggerInstance.logLevels;
1825
+ return isLogLevelEnabled(level, logLevels);
1826
+ }
1827
+ registerLocalInstanceRef() {
1828
+ var _a;
1829
+ if (this.localInstanceRef) {
1830
+ return this.localInstanceRef;
1831
+ }
1832
+ this.localInstanceRef = new ConsoleLogger(this.context, {
1833
+ timestamp: (_a = this.options) == null ? void 0 : _a.timestamp,
1834
+ logLevels: _LoggerInstance.logLevels
1835
+ });
1836
+ return this.localInstanceRef;
1837
+ }
1838
+ };
1839
+ _init2 = __decoratorStart(null);
1840
+ _LoggerInstance = __decorateElement(_init2, 0, "LoggerInstance", _LoggerInstance_decorators, _LoggerInstance);
1841
+ __runInitializers(_init2, 1, _LoggerInstance);
1842
+ var LoggerInstance = _LoggerInstance;
1843
+
1844
+ // packages/core/src/logger/logger.factory.mts
1845
+ var LoggerInjectionToken = "LoggerInjectionToken";
1846
+ var LoggerOptions = import_zod2.z.object({
1847
+ context: import_zod2.z.string().optional(),
1848
+ options: import_zod2.z.object({
1849
+ timestamp: import_zod2.z.boolean().optional()
1850
+ }).optional()
1851
+ }).optional();
1852
+ var Logger = InjectionToken.create(LoggerInjectionToken, LoggerOptions);
1853
+ var _LoggerFactory_decorators, _init3;
1854
+ _LoggerFactory_decorators = [Injectable({
1855
+ type: "Factory" /* Factory */,
1856
+ token: Logger
1857
+ })];
1858
+ var LoggerFactory = class {
1859
+ create(ctx, args) {
1860
+ return new LoggerInstance(args == null ? void 0 : args.context, args == null ? void 0 : args.options);
1861
+ }
1862
+ };
1863
+ _init3 = __decoratorStart(null);
1864
+ LoggerFactory = __decorateElement(_init3, 0, "LoggerFactory", _LoggerFactory_decorators, LoggerFactory);
1865
+ __runInitializers(_init3, 1, LoggerFactory);
1866
+
1867
+ // packages/core/src/logger/pino-wrapper.mts
1868
+ var PinoWrapper = class _PinoWrapper {
1869
+ constructor(logger) {
1870
+ this.logger = logger;
1871
+ }
1872
+ fatal(message, ...optionalParams) {
1873
+ if (this.logger.fatal === void 0) {
1874
+ return this.error(message, ...optionalParams);
1875
+ }
1876
+ this.logger.fatal(message, ...optionalParams);
1877
+ }
1878
+ error(message, ...optionalParams) {
1879
+ this.logger.error(message, ...optionalParams);
1880
+ }
1881
+ warn(message, ...optionalParams) {
1882
+ this.logger.warn(message, ...optionalParams);
1883
+ }
1884
+ info(message, ...optionalParams) {
1885
+ this.logger.log(message, ...optionalParams);
1886
+ }
1887
+ debug(message, ...optionalParams) {
1888
+ var _a, _b;
1889
+ (_b = (_a = this.logger).debug) == null ? void 0 : _b.call(_a, message, ...optionalParams);
1890
+ }
1891
+ trace(message, ...optionalParams) {
1892
+ var _a, _b;
1893
+ (_b = (_a = this.logger).verbose) == null ? void 0 : _b.call(_a, message, ...optionalParams);
1894
+ }
1895
+ silent(message, ...optionalParams) {
1896
+ }
1897
+ child(options) {
1898
+ const keys = Object.keys(options);
1899
+ let newContext = this.logger["context"] ?? "";
1900
+ if (keys.length > 1) {
1901
+ newContext = `${this.logger["context"] ?? ""}:${JSON.stringify(options)}`;
1902
+ }
1903
+ return new _PinoWrapper(
1904
+ // @ts-expect-error We don't need to support this in the current version
1905
+ new LoggerInstance(newContext, this.logger["options"])
1906
+ );
1907
+ }
1908
+ get level() {
1909
+ if ("level" in this.logger && this.logger.level) {
1910
+ return this.logger.level;
1911
+ }
1912
+ const levels = LoggerInstance["logLevels"];
1913
+ if (levels) {
1914
+ return levels[0];
1915
+ }
1916
+ return "info";
1917
+ }
1918
+ };
1919
+
1244
1920
  // packages/core/src/tokens/application.token.mts
1245
1921
  var ApplicationInjectionToken = "ApplicationInjectionToken";
1246
1922
  var Application = InjectionToken.create(
@@ -1306,7 +1982,7 @@ var ExecutionContext2 = class {
1306
1982
  };
1307
1983
 
1308
1984
  // packages/core/src/services/guard-runner.service.mts
1309
- var _GuardRunnerService_decorators, _init;
1985
+ var _GuardRunnerService_decorators, _init4;
1310
1986
  _GuardRunnerService_decorators = [Injectable()];
1311
1987
  var GuardRunnerService = class {
1312
1988
  async runGuards(allGuards, executionContext) {
@@ -1369,15 +2045,18 @@ var GuardRunnerService = class {
1369
2045
  return guards;
1370
2046
  }
1371
2047
  };
1372
- _init = __decoratorStart(null);
1373
- GuardRunnerService = __decorateElement(_init, 0, "GuardRunnerService", _GuardRunnerService_decorators, GuardRunnerService);
1374
- __runInitializers(_init, 1, GuardRunnerService);
2048
+ _init4 = __decoratorStart(null);
2049
+ GuardRunnerService = __decorateElement(_init4, 0, "GuardRunnerService", _GuardRunnerService_decorators, GuardRunnerService);
2050
+ __runInitializers(_init4, 1, GuardRunnerService);
1375
2051
 
1376
2052
  // packages/core/src/services/controller-adapter.service.mts
1377
- var _ControllerAdapterService_decorators, _init2;
2053
+ var _ControllerAdapterService_decorators, _init5;
1378
2054
  _ControllerAdapterService_decorators = [Injectable()];
1379
- var ControllerAdapterService = class {
2055
+ var _ControllerAdapterService = class _ControllerAdapterService {
1380
2056
  guardRunner = syncInject(GuardRunnerService);
2057
+ logger = syncInject(Logger, {
2058
+ context: _ControllerAdapterService.name
2059
+ });
1381
2060
  setupController(controller, instance, moduleMetadata) {
1382
2061
  const controllerMetadata = extractControllerMetadata(controller);
1383
2062
  for (const endpoint of controllerMetadata.endpoints) {
@@ -1472,17 +2151,24 @@ var ControllerAdapterService = class {
1472
2151
  }
1473
2152
  }
1474
2153
  });
2154
+ this.logger.debug(
2155
+ `Registered ${httpMethod} ${url} for ${controller.name}:${classMethod}`
2156
+ );
1475
2157
  }
1476
2158
  }
1477
2159
  };
1478
- _init2 = __decoratorStart(null);
1479
- ControllerAdapterService = __decorateElement(_init2, 0, "ControllerAdapterService", _ControllerAdapterService_decorators, ControllerAdapterService);
1480
- __runInitializers(_init2, 1, ControllerAdapterService);
2160
+ _init5 = __decoratorStart(null);
2161
+ _ControllerAdapterService = __decorateElement(_init5, 0, "ControllerAdapterService", _ControllerAdapterService_decorators, _ControllerAdapterService);
2162
+ __runInitializers(_init5, 1, _ControllerAdapterService);
2163
+ var ControllerAdapterService = _ControllerAdapterService;
1481
2164
 
1482
2165
  // packages/core/src/services/module-loader.service.mts
1483
- var _ModuleLoaderService_decorators, _init3;
2166
+ var _ModuleLoaderService_decorators, _init6;
1484
2167
  _ModuleLoaderService_decorators = [Injectable()];
1485
- var ModuleLoaderService = class {
2168
+ var _ModuleLoaderService = class _ModuleLoaderService {
2169
+ logger = syncInject(Logger, {
2170
+ context: _ModuleLoaderService.name
2171
+ });
1486
2172
  modulesMetadata = /* @__PURE__ */ new Map();
1487
2173
  loadedModules = /* @__PURE__ */ new Map();
1488
2174
  initialized = false;
@@ -1512,6 +2198,7 @@ var ModuleLoaderService = class {
1512
2198
  if (instance.onModuleInit) {
1513
2199
  await instance.onModuleInit();
1514
2200
  }
2201
+ this.logger.debug(`Module ${moduleName} loaded`);
1515
2202
  this.loadedModules.set(moduleName, instance);
1516
2203
  }
1517
2204
  mergeMetadata(metadata, parentMetadata) {
@@ -1533,9 +2220,10 @@ var ModuleLoaderService = class {
1533
2220
  return this.modulesMetadata;
1534
2221
  }
1535
2222
  };
1536
- _init3 = __decoratorStart(null);
1537
- ModuleLoaderService = __decorateElement(_init3, 0, "ModuleLoaderService", _ModuleLoaderService_decorators, ModuleLoaderService);
1538
- __runInitializers(_init3, 1, ModuleLoaderService);
2223
+ _init6 = __decoratorStart(null);
2224
+ _ModuleLoaderService = __decorateElement(_init6, 0, "ModuleLoaderService", _ModuleLoaderService_decorators, _ModuleLoaderService);
2225
+ __runInitializers(_init6, 1, _ModuleLoaderService);
2226
+ var ModuleLoaderService = _ModuleLoaderService;
1539
2227
 
1540
2228
  // packages/core/src/attribute.factory.mts
1541
2229
  var AttributeFactory = class {
@@ -1598,11 +2286,14 @@ var AttributeFactory = class {
1598
2286
  var import_cors = __toESM(require("@fastify/cors"), 1);
1599
2287
  var import_fastify = require("fastify");
1600
2288
  var import_fastify_type_provider_zod = require("fastify-type-provider-zod");
1601
- var _NaviosApplication_decorators, _init4;
2289
+ var _NaviosApplication_decorators, _init7;
1602
2290
  _NaviosApplication_decorators = [Injectable()];
1603
- var NaviosApplication = class {
2291
+ var _NaviosApplication = class _NaviosApplication {
1604
2292
  moduleLoader = syncInject(ModuleLoaderService);
1605
2293
  controllerAdapter = syncInject(ControllerAdapterService);
2294
+ logger = syncInject(Logger, {
2295
+ context: _NaviosApplication.name
2296
+ });
1606
2297
  server = null;
1607
2298
  corsOptions = null;
1608
2299
  globalPrefix = null;
@@ -1617,40 +2308,72 @@ var NaviosApplication = class {
1617
2308
  throw new Error("App module is not set. Call setAppModule() first.");
1618
2309
  }
1619
2310
  await this.moduleLoader.loadModules(this.appModule);
1620
- this.server = (0, import_fastify.fastify)(this.options);
2311
+ this.server = await this.getFastifyInstance(this.options);
1621
2312
  getServiceLocator().registerInstance(Application, this.server);
1622
2313
  this.server.setValidatorCompiler(import_fastify_type_provider_zod.validatorCompiler);
1623
2314
  this.server.setSerializerCompiler(import_fastify_type_provider_zod.serializerCompiler);
1624
2315
  if (this.corsOptions) {
1625
2316
  await this.server.register(import_cors.default, this.corsOptions);
1626
2317
  }
2318
+ await this.initModules();
2319
+ this.logger.debug("Navios application initialized");
2320
+ }
2321
+ async getFastifyInstance(rawOptions) {
2322
+ const { logger, ...options } = rawOptions;
2323
+ if (logger) {
2324
+ const fastifyOptions = options;
2325
+ if (typeof logger === "boolean") {
2326
+ if (!logger) {
2327
+ fastifyOptions.logger = false;
2328
+ }
2329
+ } else {
2330
+ fastifyOptions.loggerInstance = new PinoWrapper(
2331
+ await inject(Logger, {
2332
+ context: "FastifyAdapter"
2333
+ })
2334
+ );
2335
+ }
2336
+ return (0, import_fastify.fastify)(fastifyOptions);
2337
+ } else {
2338
+ return (0, import_fastify.fastify)({
2339
+ ...options,
2340
+ loggerInstance: new PinoWrapper(
2341
+ await inject(Logger, {
2342
+ context: "FastifyAdapter"
2343
+ })
2344
+ )
2345
+ });
2346
+ }
2347
+ }
2348
+ async initModules() {
1627
2349
  const modules = this.moduleLoader.getAllModules();
1628
- const globalPrefix = this.globalPrefix ?? "";
2350
+ const promises = [];
1629
2351
  for (const [moduleName, moduleMetadata] of modules) {
1630
2352
  if (!moduleMetadata.controllers || moduleMetadata.controllers.size === 0) {
1631
2353
  continue;
1632
2354
  }
1633
- this.server.register(
1634
- (instance, opts, done) => {
1635
- for (const controller of moduleMetadata.controllers) {
1636
- this.controllerAdapter.setupController(
1637
- controller,
1638
- instance,
1639
- moduleMetadata
1640
- );
2355
+ promises.push(
2356
+ this.server.register(
2357
+ (instance, opts, done) => {
2358
+ for (const controller of moduleMetadata.controllers) {
2359
+ this.controllerAdapter.setupController(
2360
+ controller,
2361
+ instance,
2362
+ moduleMetadata
2363
+ );
2364
+ }
2365
+ done();
2366
+ },
2367
+ {
2368
+ prefix: this.globalPrefix ?? ""
1641
2369
  }
1642
- done();
1643
- },
1644
- {
1645
- prefix: globalPrefix
1646
- }
2370
+ )
1647
2371
  );
1648
2372
  }
2373
+ await Promise.all(promises);
1649
2374
  }
1650
2375
  enableCors(options) {
1651
- var _a;
1652
2376
  this.corsOptions = options;
1653
- (_a = this.server) == null ? void 0 : _a.register;
1654
2377
  }
1655
2378
  setGlobalPrefix(prefix) {
1656
2379
  this.globalPrefix = prefix;
@@ -1668,17 +2391,28 @@ var NaviosApplication = class {
1668
2391
  await this.server.listen(options);
1669
2392
  }
1670
2393
  };
1671
- _init4 = __decoratorStart(null);
1672
- NaviosApplication = __decorateElement(_init4, 0, "NaviosApplication", _NaviosApplication_decorators, NaviosApplication);
1673
- __runInitializers(_init4, 1, NaviosApplication);
2394
+ _init7 = __decoratorStart(null);
2395
+ _NaviosApplication = __decorateElement(_init7, 0, "NaviosApplication", _NaviosApplication_decorators, _NaviosApplication);
2396
+ __runInitializers(_init7, 1, _NaviosApplication);
2397
+ var NaviosApplication = _NaviosApplication;
1674
2398
 
1675
2399
  // packages/core/src/navios.factory.mts
1676
2400
  var NaviosFactory = class {
1677
2401
  static async create(appModule, options = {}) {
1678
2402
  const app = await inject(NaviosApplication);
2403
+ this.registerLoggerConfiguration(options);
1679
2404
  app.setup(appModule, options);
1680
2405
  return app;
1681
2406
  }
2407
+ static registerLoggerConfiguration(options) {
2408
+ if (!options) {
2409
+ return;
2410
+ }
2411
+ const { logger } = options;
2412
+ if (logger !== true && !isNil(logger)) {
2413
+ LoggerInstance.overrideLogger(logger);
2414
+ }
2415
+ }
1682
2416
  };
1683
2417
  // Annotate the CommonJS export names for ESM import in node:
1684
2418
  0 && (module.exports = {
@@ -1686,6 +2420,7 @@ var NaviosFactory = class {
1686
2420
  AttributeFactory,
1687
2421
  BadRequestException,
1688
2422
  ConflictException,
2423
+ ConsoleLogger,
1689
2424
  Controller,
1690
2425
  ControllerAdapterService,
1691
2426
  ControllerMetadataKey,
@@ -1709,12 +2444,19 @@ var NaviosFactory = class {
1709
2444
  InstanceExpired,
1710
2445
  InstanceNotFound,
1711
2446
  InternalServerErrorException,
2447
+ LOG_LEVELS,
2448
+ Logger,
2449
+ LoggerFactory,
2450
+ LoggerInjectionToken,
2451
+ LoggerInstance,
2452
+ LoggerOptions,
1712
2453
  Module,
1713
2454
  ModuleLoaderService,
1714
2455
  ModuleMetadataKey,
1715
2456
  NaviosApplication,
1716
2457
  NaviosFactory,
1717
2458
  NotFoundException,
2459
+ PinoWrapper,
1718
2460
  Reply,
1719
2461
  Request,
1720
2462
  ServiceLocator,
@@ -1725,8 +2467,11 @@ var NaviosFactory = class {
1725
2467
  UnauthorizedException,
1726
2468
  UnknownError,
1727
2469
  UseGuards,
2470
+ addLeadingSlash,
2471
+ clc,
1728
2472
  extractControllerMetadata,
1729
2473
  extractModuleMetadata,
2474
+ filterLogLevels,
1730
2475
  getAllEndpointMetadata,
1731
2476
  getControllerMetadata,
1732
2477
  getEndpointMetadata,
@@ -1736,9 +2481,24 @@ var NaviosFactory = class {
1736
2481
  hasControllerMetadata,
1737
2482
  hasModuleMetadata,
1738
2483
  inject,
2484
+ isConstructor,
2485
+ isEmpty,
2486
+ isFunction,
2487
+ isLogLevel,
2488
+ isLogLevelEnabled,
2489
+ isNil,
2490
+ isNumber,
2491
+ isObject,
2492
+ isPlainObject,
2493
+ isString,
2494
+ isSymbol,
2495
+ isUndefined,
2496
+ normalizePath,
1739
2497
  override,
1740
2498
  provideServiceLocator,
1741
2499
  setPromiseCollector,
1742
- syncInject
2500
+ stripEndSlash,
2501
+ syncInject,
2502
+ yellow
1743
2503
  });
1744
2504
  //# sourceMappingURL=index.js.map