@mahameru/diatrema 0.0.2 → 0.0.3

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.
Files changed (101) hide show
  1. package/diatrema-Bk12jngx.d.ts +93 -0
  2. package/diatrema-CyKJKZv_.d.cts +93 -0
  3. package/diatrema.cjs +70 -52
  4. package/diatrema.cjs.map +1 -1
  5. package/diatrema.d.cts +6 -72
  6. package/diatrema.d.ts +4 -71
  7. package/diatrema.js +70 -52
  8. package/diatrema.js.map +1 -1
  9. package/helpers.cjs +2 -18
  10. package/helpers.cjs.map +1 -1
  11. package/helpers.d.cts +1 -6
  12. package/helpers.d.ts +1 -6
  13. package/helpers.js +1 -16
  14. package/helpers.js.map +1 -1
  15. package/index.cjs +4 -18
  16. package/index.cjs.map +1 -1
  17. package/index.d.cts +4 -13
  18. package/index.d.ts +4 -13
  19. package/index.js +2 -9
  20. package/index.js.map +1 -1
  21. package/logger.cjs +10 -5
  22. package/logger.cjs.map +1 -1
  23. package/logger.d.cts +1 -1
  24. package/logger.d.ts +1 -1
  25. package/logger.js +10 -5
  26. package/logger.js.map +1 -1
  27. package/mahameru-plugin.cjs +109 -0
  28. package/mahameru-plugin.cjs.map +1 -0
  29. package/mahameru-plugin.d.cts +4 -0
  30. package/mahameru-plugin.d.ts +4 -0
  31. package/mahameru-plugin.js +85 -0
  32. package/mahameru-plugin.js.map +1 -0
  33. package/package.json +4 -3
  34. package/scripts/preinstall.cjs +10 -0
  35. package/scripts/preinstall.cjs.map +1 -0
  36. package/scripts/preinstall.d.cts +2 -0
  37. package/scripts/preinstall.d.ts +2 -0
  38. package/scripts/preinstall.js +9 -9
  39. package/scripts/preinstall.js.map +1 -0
  40. package/types.cjs +0 -23
  41. package/types.cjs.map +1 -1
  42. package/types.d.cts +5 -3
  43. package/types.d.ts +5 -3
  44. package/types.js +0 -15
  45. package/types.js.map +1 -1
  46. package/container-error.cjs +0 -45
  47. package/container-error.cjs.map +0 -1
  48. package/container-error.d.cts +0 -7
  49. package/container-error.d.ts +0 -7
  50. package/container-error.js +0 -22
  51. package/container-error.js.map +0 -1
  52. package/container.cjs +0 -318
  53. package/container.cjs.map +0 -1
  54. package/container.d.cts +0 -60
  55. package/container.d.ts +0 -60
  56. package/container.js +0 -285
  57. package/container.js.map +0 -1
  58. package/http-server-error.cjs +0 -44
  59. package/http-server-error.cjs.map +0 -1
  60. package/http-server-error.d.cts +0 -7
  61. package/http-server-error.d.ts +0 -7
  62. package/http-server-error.js +0 -21
  63. package/http-server-error.js.map +0 -1
  64. package/http-server.cjs +0 -384
  65. package/http-server.cjs.map +0 -1
  66. package/http-server.d.cts +0 -81
  67. package/http-server.d.ts +0 -81
  68. package/http-server.js +0 -361
  69. package/http-server.js.map +0 -1
  70. package/mahameru-request.cjs +0 -79
  71. package/mahameru-request.cjs.map +0 -1
  72. package/mahameru-request.d.cts +0 -3
  73. package/mahameru-request.d.ts +0 -3
  74. package/mahameru-request.js +0 -56
  75. package/mahameru-request.js.map +0 -1
  76. package/mahameru-response.cjs +0 -71
  77. package/mahameru-response.cjs.map +0 -1
  78. package/mahameru-response.d.cts +0 -17
  79. package/mahameru-response.d.ts +0 -17
  80. package/mahameru-response.js +0 -48
  81. package/mahameru-response.js.map +0 -1
  82. package/mahameru-server-error.cjs +0 -54
  83. package/mahameru-server-error.cjs.map +0 -1
  84. package/mahameru-server-error.d.cts +0 -19
  85. package/mahameru-server-error.d.ts +0 -19
  86. package/mahameru-server-error.js +0 -31
  87. package/mahameru-server-error.js.map +0 -1
  88. package/module-error.cjs +0 -46
  89. package/module-error.cjs.map +0 -1
  90. package/module-error.d.cts +0 -12
  91. package/module-error.d.ts +0 -12
  92. package/module-error.js +0 -23
  93. package/module-error.js.map +0 -1
  94. package/route.cjs +0 -128
  95. package/route.cjs.map +0 -1
  96. package/route.d.cts +0 -46
  97. package/route.d.ts +0 -46
  98. package/route.js +0 -105
  99. package/route.js.map +0 -1
  100. package/types-BmFqDpSs.d.ts +0 -113
  101. package/types-Cd6Gkoxx.d.cts +0 -113
@@ -0,0 +1,93 @@
1
+ import { EventEmitter } from './event-emitter.js';
2
+ import { Instances } from './types.js';
3
+ import { Logger } from './logger.js';
4
+
5
+ interface BasePluginOptions {
6
+ debug?: boolean;
7
+ }
8
+ declare abstract class MahameruPlugin<O extends BasePluginOptions = BasePluginOptions> {
9
+ abstract readonly name: string;
10
+ abstract readonly slugName: string;
11
+ protected logger: Logger;
12
+ protected diatrema: Diatrema;
13
+ protected _options: O;
14
+ protected _initialized: boolean;
15
+ protected _isShuttingDown: boolean;
16
+ protected _typesGenerator?: TypesGenerator;
17
+ constructor(options: Partial<O>);
18
+ get initialized(): boolean;
19
+ get options(): O;
20
+ get typesGenerator(): TypesGenerator<BaseTypesGeneratorOptions> | undefined;
21
+ set outputTypesDirPath(outputTypesDirPath: string);
22
+ setDiatrema(diatrema: Diatrema): void;
23
+ initialize(): Promise<void>;
24
+ destroy(): Promise<void>;
25
+ protected abstract boot(options?: Partial<O>): Promise<void> | void;
26
+ protected abstract terminate(): Promise<void> | void;
27
+ }
28
+ interface BaseTypesGeneratorOptions {
29
+ debug?: boolean;
30
+ }
31
+ declare abstract class TypesGenerator<O extends BaseTypesGeneratorOptions = BaseTypesGeneratorOptions> {
32
+ protected logger: Logger;
33
+ protected _diatrema: Diatrema;
34
+ protected _options: O;
35
+ protected _outputTypesDirPath: string;
36
+ constructor(options: Partial<O>);
37
+ set diatrema(diatrema: Diatrema);
38
+ set outputTypesDirPath(outputTypesDirPath: string);
39
+ generate(): Promise<void>;
40
+ protected abstract _generate(): Promise<Record<string, any>>;
41
+ }
42
+
43
+ type DiatremaEvents = {
44
+ ready: [data: {
45
+ mode: "development" | "production";
46
+ port?: number;
47
+ host?: string;
48
+ }];
49
+ };
50
+ type DiatremaOptions = {
51
+ dev: boolean;
52
+ debug: boolean;
53
+ rootPath: string;
54
+ appPath: string;
55
+ productionDir: string;
56
+ developmentDir: string;
57
+ initiatorFilePath?: string;
58
+ moduleType: "commonjs" | "esm";
59
+ };
60
+ declare const diatremaDefaultConfig: DiatremaOptions;
61
+ /**
62
+ * Main Diatrema class that orchestrates the application lifecycle.
63
+ */
64
+ declare class Diatrema extends EventEmitter<DiatremaEvents> {
65
+ protected _initialized: boolean;
66
+ protected _isShuttingDown: boolean;
67
+ protected _plugins: Map<string, MahameruPlugin<any>>;
68
+ protected logger: Logger;
69
+ instances: Instances;
70
+ readonly options: DiatremaOptions;
71
+ constructor(options?: Partial<DiatremaOptions>);
72
+ /**
73
+ * Indicates whether the Mahameru server has been initialized or not.
74
+ * @returns {boolean}
75
+ */
76
+ get initialized(): boolean;
77
+ /**
78
+ * Indicates whether the Mahameru server is shutting down or not.
79
+ * @returns {boolean}
80
+ */
81
+ get isShuttingDown(): boolean;
82
+ get plugins(): Record<string, MahameruPlugin<any>>;
83
+ initialize(): Promise<void>;
84
+ setPlugin<T extends MahameruPlugin<any>>(pluginName: string, plugin: T): void;
85
+ getPlugin<T extends MahameruPlugin<any>>(pluginName: string): T | undefined;
86
+ devHRM(changedFile?: string): Promise<void>;
87
+ shutdown(): Promise<void>;
88
+ protected loadInitiator(): Promise<void>;
89
+ protected require<T extends Record<string, unknown> = Record<string, unknown>>(type: "commonjs" | "esm", resolvedFilePath: string): Promise<T | undefined>;
90
+ protected getDefaultExport<T>(module: Record<string, T>, filePath: string): T;
91
+ }
92
+
93
+ export { type BasePluginOptions as B, Diatrema as D, MahameruPlugin as M, TypesGenerator as T, type BaseTypesGeneratorOptions as a, type DiatremaEvents as b, type DiatremaOptions as c, diatremaDefaultConfig as d };
@@ -0,0 +1,93 @@
1
+ import { EventEmitter } from './event-emitter.cjs';
2
+ import { Instances } from './types.cjs';
3
+ import { Logger } from './logger.cjs';
4
+
5
+ interface BasePluginOptions {
6
+ debug?: boolean;
7
+ }
8
+ declare abstract class MahameruPlugin<O extends BasePluginOptions = BasePluginOptions> {
9
+ abstract readonly name: string;
10
+ abstract readonly slugName: string;
11
+ protected logger: Logger;
12
+ protected diatrema: Diatrema;
13
+ protected _options: O;
14
+ protected _initialized: boolean;
15
+ protected _isShuttingDown: boolean;
16
+ protected _typesGenerator?: TypesGenerator;
17
+ constructor(options: Partial<O>);
18
+ get initialized(): boolean;
19
+ get options(): O;
20
+ get typesGenerator(): TypesGenerator<BaseTypesGeneratorOptions> | undefined;
21
+ set outputTypesDirPath(outputTypesDirPath: string);
22
+ setDiatrema(diatrema: Diatrema): void;
23
+ initialize(): Promise<void>;
24
+ destroy(): Promise<void>;
25
+ protected abstract boot(options?: Partial<O>): Promise<void> | void;
26
+ protected abstract terminate(): Promise<void> | void;
27
+ }
28
+ interface BaseTypesGeneratorOptions {
29
+ debug?: boolean;
30
+ }
31
+ declare abstract class TypesGenerator<O extends BaseTypesGeneratorOptions = BaseTypesGeneratorOptions> {
32
+ protected logger: Logger;
33
+ protected _diatrema: Diatrema;
34
+ protected _options: O;
35
+ protected _outputTypesDirPath: string;
36
+ constructor(options: Partial<O>);
37
+ set diatrema(diatrema: Diatrema);
38
+ set outputTypesDirPath(outputTypesDirPath: string);
39
+ generate(): Promise<void>;
40
+ protected abstract _generate(): Promise<Record<string, any>>;
41
+ }
42
+
43
+ type DiatremaEvents = {
44
+ ready: [data: {
45
+ mode: "development" | "production";
46
+ port?: number;
47
+ host?: string;
48
+ }];
49
+ };
50
+ type DiatremaOptions = {
51
+ dev: boolean;
52
+ debug: boolean;
53
+ rootPath: string;
54
+ appPath: string;
55
+ productionDir: string;
56
+ developmentDir: string;
57
+ initiatorFilePath?: string;
58
+ moduleType: "commonjs" | "esm";
59
+ };
60
+ declare const diatremaDefaultConfig: DiatremaOptions;
61
+ /**
62
+ * Main Diatrema class that orchestrates the application lifecycle.
63
+ */
64
+ declare class Diatrema extends EventEmitter<DiatremaEvents> {
65
+ protected _initialized: boolean;
66
+ protected _isShuttingDown: boolean;
67
+ protected _plugins: Map<string, MahameruPlugin<any>>;
68
+ protected logger: Logger;
69
+ instances: Instances;
70
+ readonly options: DiatremaOptions;
71
+ constructor(options?: Partial<DiatremaOptions>);
72
+ /**
73
+ * Indicates whether the Mahameru server has been initialized or not.
74
+ * @returns {boolean}
75
+ */
76
+ get initialized(): boolean;
77
+ /**
78
+ * Indicates whether the Mahameru server is shutting down or not.
79
+ * @returns {boolean}
80
+ */
81
+ get isShuttingDown(): boolean;
82
+ get plugins(): Record<string, MahameruPlugin<any>>;
83
+ initialize(): Promise<void>;
84
+ setPlugin<T extends MahameruPlugin<any>>(pluginName: string, plugin: T): void;
85
+ getPlugin<T extends MahameruPlugin<any>>(pluginName: string): T | undefined;
86
+ devHRM(changedFile?: string): Promise<void>;
87
+ shutdown(): Promise<void>;
88
+ protected loadInitiator(): Promise<void>;
89
+ protected require<T extends Record<string, unknown> = Record<string, unknown>>(type: "commonjs" | "esm", resolvedFilePath: string): Promise<T | undefined>;
90
+ protected getDefaultExport<T>(module: Record<string, T>, filePath: string): T;
91
+ }
92
+
93
+ export { type BasePluginOptions as B, Diatrema as D, MahameruPlugin as M, TypesGenerator as T, type BaseTypesGeneratorOptions as a, type DiatremaEvents as b, type DiatremaOptions as c, diatremaDefaultConfig as d };
package/diatrema.cjs CHANGED
@@ -24,103 +24,121 @@ __export(diatrema_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(diatrema_exports);
26
26
  var import_node_path = require("node:path");
27
- var import_http_server = require('./http-server.cjs');
28
27
  var import_event_emitter = require('./event-emitter.cjs');
28
+ var import_node_fs = require("node:fs");
29
+ var import_node_url = require("node:url");
30
+ var import_logger = require('./logger.cjs');
29
31
  const diatremaDefaultConfig = {
30
32
  dev: false,
31
- isStandalone: false,
33
+ debug: false,
32
34
  rootPath: process.cwd(),
33
35
  get appPath() {
34
- if (this.isStandalone)
35
- return this.rootPath;
36
36
  return (0, import_node_path.join)(this.rootPath, this.dev ? this.developmentDir : this.productionDir);
37
37
  },
38
38
  productionDir: ".mahameru",
39
39
  developmentDir: ".mahameru",
40
- routesDir: "routes"
40
+ moduleType: "esm"
41
41
  };
42
42
  class Diatrema extends import_event_emitter.EventEmitter {
43
43
  static {
44
44
  __name(this, "Diatrema");
45
45
  }
46
- #initialized = false;
47
- #isShuttingDown = false;
48
- options = diatremaDefaultConfig;
49
- dependencies;
50
- constructor(arg1, arg2) {
46
+ _initialized = false;
47
+ _isShuttingDown = false;
48
+ _plugins = /* @__PURE__ */ new Map();
49
+ logger;
50
+ instances = {};
51
+ options;
52
+ constructor(options) {
51
53
  super();
52
- if (typeof arg2 !== "undefined")
53
- this.options = { ...this.options, ...arg1 };
54
- if (typeof arg2 !== "undefined") {
55
- this.dependencies = arg2;
56
- } else {
57
- this.dependencies = arg1;
58
- }
54
+ this.options = { ...diatremaDefaultConfig, ...options };
55
+ this.logger = (0, import_logger.createLogger)("Diatrema", this.options.debug);
59
56
  }
60
57
  /**
61
58
  * Indicates whether the Mahameru server has been initialized or not.
62
59
  * @returns {boolean}
63
60
  */
64
61
  get initialized() {
65
- return this.#initialized;
62
+ return this._initialized;
66
63
  }
67
64
  /**
68
65
  * Indicates whether the Mahameru server is shutting down or not.
69
66
  * @returns {boolean}
70
67
  */
71
68
  get isShuttingDown() {
72
- return this.#isShuttingDown;
69
+ return this._isShuttingDown;
70
+ }
71
+ get plugins() {
72
+ return Object.fromEntries(this._plugins);
73
73
  }
74
- /**
75
- * Initialize the Mahameru server.
76
- */
77
74
  async initialize() {
78
75
  try {
79
- if (this.#initialized)
76
+ if (this._initialized)
80
77
  return;
81
- await this.dependencies.container.discover();
78
+ await this.loadInitiator();
79
+ for (const plugin of this._plugins.values()) {
80
+ plugin.setDiatrema(this);
81
+ await plugin.initialize();
82
+ }
82
83
  } catch (error) {
83
84
  throw error;
84
85
  }
85
- const { port, host } = await this.dependencies.httpServer.listen();
86
- this.#initialized = true;
87
- this.emit("ready", { mode: this.options.dev ? "development" : "production", port, host });
86
+ this._initialized = true;
87
+ this.emit("ready", { mode: this.options.dev ? "development" : "production" });
88
+ }
89
+ setPlugin(pluginName, plugin) {
90
+ this._plugins.set(pluginName, plugin);
91
+ }
92
+ getPlugin(pluginName) {
93
+ return this._plugins.get(pluginName);
88
94
  }
89
- /**
90
- * Hot reload the middleware and routes when a file changes in development mode.
91
- */
92
95
  async devHRM(changedFile) {
93
- if (!this.#initialized)
96
+ if (!this._initialized)
94
97
  return;
95
98
  if (changedFile) {
96
- if (await this.dependencies.container.onFileChanged(changedFile))
97
- this.dependencies.logger.debug(`File changed: ${changedFile}`);
98
99
  }
99
100
  }
100
- /**
101
- * Shut down the Mahameru server gracefully.
102
- * @returns {Promise<void>}
103
- */
104
101
  async shutdown() {
105
- if (this.#isShuttingDown)
102
+ if (this._isShuttingDown)
106
103
  return;
107
- this.#isShuttingDown = true;
108
- this.dependencies.logger.info("Shutting down Mahameru server...");
109
- try {
110
- this.dependencies.logger.info("Databases destroyed successfully.");
111
- } catch (error) {
112
- this.dependencies.logger.error("Error destroying databases", error);
104
+ this._isShuttingDown = true;
105
+ this.logger.debug("Shutting down...");
106
+ for (const plugin of this._plugins.values()) {
107
+ await plugin.destroy();
113
108
  }
114
- if (this.dependencies.httpServer.listening) {
115
- try {
116
- await this.dependencies.httpServer.close();
117
- this.dependencies.logger.info("HTTP server closed successfully.");
118
- } catch (error) {
119
- this.dependencies.logger.error("Error closing HTTP server", error);
109
+ this._initialized = false;
110
+ this.logger.debug("Shutting down... Done");
111
+ }
112
+ async loadInitiator() {
113
+ if (!this.options.initiatorFilePath)
114
+ return;
115
+ const result = await this.require(this.options.moduleType, this.options.initiatorFilePath);
116
+ if (result) {
117
+ const handler = this.getDefaultExport(result, this.options.initiatorFilePath);
118
+ this.instances = await handler();
119
+ this.logger.debug(`Initiator loaded successfully. Found ${Object.keys(this.instances).length} instances.`);
120
+ }
121
+ }
122
+ async require(type, resolvedFilePath) {
123
+ const noCache = this.options.dev;
124
+ if (!(0, import_node_fs.existsSync)(resolvedFilePath))
125
+ return;
126
+ if (type === "commonjs") {
127
+ if (noCache) {
128
+ delete require.cache[resolvedFilePath];
120
129
  }
130
+ return require(resolvedFilePath);
121
131
  }
122
- this.#initialized = false;
123
- this.dependencies.logger.info("Mahameru server shut down.");
132
+ let fileUrl = (0, import_node_url.pathToFileURL)(resolvedFilePath).href;
133
+ if (noCache)
134
+ fileUrl += `?update=${Date.now()}`;
135
+ return await import(fileUrl);
136
+ }
137
+ getDefaultExport(module2, filePath) {
138
+ const defaultExportName = Object.keys(module2).find((key) => key === "default");
139
+ if (!defaultExportName)
140
+ throw new Error(`Module in file '${filePath}' does not have a default export.`);
141
+ return module2[defaultExportName];
124
142
  }
125
143
  }
126
144
  // Annotate the CommonJS export names for ESM import in node:
package/diatrema.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/diatrema.ts"],"sourcesContent":["import { join } from 'node:path';\n\nimport { HttpServer } from './http-server';\nimport { EventEmitter } from \"./event-emitter\";\n\nimport type { Container } from './container';\nimport type { Route } from './route';\nimport type { Logger } from './logger';\n\nexport type DiatremaEvents = {\n ready: [data: { mode: \"development\" | \"production\"; port: number; host: string; }];\n};\n\nexport type DiatremaOptions = {\n dev: boolean;\n isStandalone: boolean;\n rootPath: string;\n appPath: string;\n routesDir: string;\n productionDir: string;\n developmentDir: string;\n}\n\nexport type DiatremaDependencies = {\n container: Container;\n route: Route;\n httpServer: HttpServer;\n logger: Logger;\n};\n\nexport const diatremaDefaultConfig: DiatremaOptions = {\n dev: false,\n isStandalone: false,\n rootPath: process.cwd(),\n get appPath(): string {\n if (this.isStandalone)\n return this.rootPath;\n\n return join(this.rootPath, this.dev ? this.developmentDir : this.productionDir);\n },\n productionDir: '.mahameru',\n developmentDir: '.mahameru',\n routesDir: 'routes'\n}\n\n/**\n * Main Diatrema class that orchestrates the application lifecycle.\n */\nexport default class Diatrema extends EventEmitter<DiatremaEvents> {\n #initialized = false;\n #isShuttingDown = false;\n public readonly options: DiatremaOptions = diatremaDefaultConfig;\n protected readonly dependencies: DiatremaDependencies;\n\n constructor(dependencies: DiatremaDependencies)\n constructor(\n initialOptions: Partial<DiatremaOptions>,\n dependencies: DiatremaDependencies\n )\n constructor(\n arg1: Partial<DiatremaOptions> | DiatremaDependencies,\n arg2?: DiatremaDependencies\n ) {\n super();\n\n if (typeof arg2 !== 'undefined')\n this.options = { ...this.options, ...arg1 as Partial<DiatremaOptions> };\n\n if (typeof arg2 !== 'undefined') {\n this.dependencies = arg2;\n } else {\n this.dependencies = arg1 as DiatremaDependencies;\n }\n }\n\n /**\n * Indicates whether the Mahameru server has been initialized or not.\n * @returns {boolean}\n */\n get initialized() {\n return this.#initialized;\n }\n\n /**\n * Indicates whether the Mahameru server is shutting down or not.\n * @returns {boolean}\n */\n get isShuttingDown() {\n return this.#isShuttingDown;\n }\n\n /**\n * Initialize the Mahameru server.\n */\n async initialize(): Promise<void> {\n try {\n if (this.#initialized)\n return;\n\n await this.dependencies.container.discover();\n } catch (error) {\n throw error;\n }\n\n const { port, host } = await this.dependencies.httpServer.listen();\n this.#initialized = true;\n\n this.emit('ready', { mode: this.options.dev ? 'development' : 'production', port: port, host: host });\n }\n\n /**\n * Hot reload the middleware and routes when a file changes in development mode.\n */\n async devHRM(changedFile?: string) {\n if (!this.#initialized)\n return\n\n if (changedFile) {\n if (await this.dependencies.container.onFileChanged(changedFile))\n this.dependencies.logger.debug(`File changed: ${changedFile}`);\n }\n }\n\n /**\n * Shut down the Mahameru server gracefully.\n * @returns {Promise<void>}\n */\n async shutdown(): Promise<void> {\n if (this.#isShuttingDown)\n return\n\n this.#isShuttingDown = true;\n\n this.dependencies.logger.info('Shutting down Mahameru server...');\n\n try {\n this.dependencies.logger.info('Databases destroyed successfully.');\n } catch (error) {\n this.dependencies.logger.error('Error destroying databases', error);\n }\n\n if (this.dependencies.httpServer.listening) {\n try {\n await this.dependencies.httpServer.close();\n this.dependencies.logger.info('HTTP server closed successfully.');\n } catch (error) {\n this.dependencies.logger.error('Error closing HTTP server', error);\n }\n }\n\n this.#initialized = false;\n this.dependencies.logger.info('Mahameru server shut down.');\n }\n}\n"],"mappings":";;;;;;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W,CAAC;AAAA,EACxG;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,OAAO,aAAsB;AAC/B,QAAI,CAAC,KAAK;AACN;AAEJ,QAAI,aAAa;AACb,UAAI,MAAM,KAAK,aAAa,UAAU,cAAc,WAAW;AAC3D,aAAK,aAAa,OAAO,MAAM,iBAAiB,WAAW,EAAE;AAAA,IACrE;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,WAA0B;AAC5B,QAAI,KAAK;AACL;AAEJ,SAAK,kBAAkB;AAEvB,SAAK,aAAa,OAAO,KAAK,kCAAkC;AAEhE,QAAI;AACA,WAAK,aAAa,OAAO,KAAK,mCAAmC;AAAA,IACrE,SAAS,OAAO;AACZ,WAAK,aAAa,OAAO,MAAM,8BAA8B,KAAK;AAAA,IACtE;AAEA,QAAI,KAAK,aAAa,WAAW,WAAW;AACxC,UAAI;AACA,cAAM,KAAK,aAAa,WAAW,MAAM;AACzC,aAAK,aAAa,OAAO,KAAK,kCAAkC;AAAA,MACpE,SAAS,OAAO;AACZ,aAAK,aAAa,OAAO,MAAM,6BAA6B,KAAK;AAAA,MACrE;AAAA,IACJ;AAEA,SAAK,eAAe;AACpB,SAAK,aAAa,OAAO,KAAK,4BAA4B;AAAA,EAC9D;AACJ;","names":[]}
1
+ {"version":3,"sources":["../src/diatrema.ts"],"sourcesContent":["import { join } from 'node:path';\n\nimport { EventEmitter } from \"./event-emitter\";\nimport { existsSync } from 'node:fs';\nimport { pathToFileURL } from 'node:url';\nimport type { InitiatorHandler, Instances } from './types';\nimport type { MahameruPlugin } from './mahameru-plugin';\nimport { createLogger, type Logger } from './logger';\n\nexport type DiatremaEvents = {\n ready: [data: { mode: \"development\" | \"production\"; port?: number; host?: string; }];\n};\n\nexport type DiatremaOptions = {\n dev: boolean;\n debug: boolean;\n rootPath: string;\n appPath: string;\n productionDir: string;\n developmentDir: string;\n initiatorFilePath?: string;\n moduleType: \"commonjs\" | \"esm\";\n}\n\nexport const diatremaDefaultConfig: DiatremaOptions = {\n dev: false,\n debug: false,\n rootPath: process.cwd(),\n get appPath(): string {\n return join(this.rootPath, this.dev ? this.developmentDir : this.productionDir);\n },\n productionDir: '.mahameru',\n developmentDir: '.mahameru',\n moduleType: 'esm'\n}\n\n/**\n * Main Diatrema class that orchestrates the application lifecycle.\n */\nexport default class Diatrema extends EventEmitter<DiatremaEvents> {\n protected _initialized = false;\n protected _isShuttingDown = false;\n protected _plugins = new Map<string, MahameruPlugin<any>>();\n protected logger: Logger;\n public instances: Instances = {};\n public readonly options: DiatremaOptions;\n\n constructor(options?: Partial<DiatremaOptions>) {\n super();\n\n this.options = { ...diatremaDefaultConfig, ...options };\n this.logger = createLogger('Diatrema', this.options.debug);\n }\n\n /**\n * Indicates whether the Mahameru server has been initialized or not.\n * @returns {boolean}\n */\n get initialized() {\n return this._initialized;\n }\n\n /**\n * Indicates whether the Mahameru server is shutting down or not.\n * @returns {boolean}\n */\n get isShuttingDown() {\n return this._isShuttingDown;\n }\n\n get plugins(): Record<string, MahameruPlugin<any>> {\n return Object.fromEntries(this._plugins);\n }\n\n public async initialize(): Promise<void> {\n try {\n if (this._initialized)\n return;\n\n await this.loadInitiator();\n\n for (const plugin of this._plugins.values()) {\n plugin.setDiatrema(this);\n\n await plugin.initialize();\n }\n } catch (error) {\n throw error;\n }\n\n this._initialized = true;\n\n this.emit('ready', { mode: this.options.dev ? 'development' : 'production' });\n }\n\n public setPlugin<T extends MahameruPlugin<any>>(pluginName: string, plugin: T) {\n this._plugins.set(pluginName, plugin);\n }\n\n public getPlugin<T extends MahameruPlugin<any>>(pluginName: string): T | undefined {\n return this._plugins.get(pluginName) as T | undefined;\n }\n\n public async devHRM(changedFile?: string) {\n if (!this._initialized)\n return\n\n if (changedFile) {\n // if (await this.dependencies.container.onFileChanged(changedFile))\n // this.logger.debug(`File changed: ${changedFile}`);\n }\n }\n\n public async shutdown(): Promise<void> {\n if (this._isShuttingDown)\n return\n\n this._isShuttingDown = true;\n\n this.logger.debug('Shutting down...');\n\n for (const plugin of this._plugins.values()) {\n await plugin.destroy();\n }\n\n this._initialized = false;\n this.logger.debug('Shutting down... Done');\n }\n\n protected async loadInitiator() {\n if (!this.options.initiatorFilePath)\n return;\n\n const result = await this.require<Record<'default', InitiatorHandler>>(this.options.moduleType, this.options.initiatorFilePath);\n\n if (result) {\n const handler = this.getDefaultExport<InitiatorHandler>(result, this.options.initiatorFilePath);\n\n this.instances = await handler();\n this.logger.debug(`Initiator loaded successfully. Found ${Object.keys(this.instances).length} instances.`);\n }\n }\n\n protected async require<T extends Record<string, unknown> = Record<string, unknown>>(type: \"commonjs\" | \"esm\", resolvedFilePath: string): Promise<T | undefined> {\n const noCache = this.options.dev;\n\n if (!existsSync(resolvedFilePath))\n return;\n\n if (type === \"commonjs\") {\n if (noCache) {\n delete require.cache[resolvedFilePath];\n }\n\n return require(resolvedFilePath) as T;\n }\n\n let fileUrl = pathToFileURL(resolvedFilePath).href;\n\n if (noCache)\n fileUrl += `?update=${Date.now()}`;\n\n return (await import(fileUrl)) as T;\n }\n\n protected getDefaultExport<T>(module: Record<string, T>, filePath: string) {\n const defaultExportName = Object.keys(module).find((key) => key === 'default');\n\n if (!defaultExportName)\n throw new Error(`Module in file '${filePath}' does not have a default export.`);\n\n return module[defaultExportName];\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAqB;AAErB,2BAA6B;AAC7B,qBAA2B;AAC3B,sBAA8B;AAG9B,oBAA0C;AAiBnC,MAAM,wBAAyC;AAAA,EAClD,KAAK;AAAA,EACL,OAAO;AAAA,EACP,UAAU,QAAQ,IAAI;AAAA,EACtB,IAAI,UAAkB;AAClB,eAAO,uBAAK,KAAK,UAAU,KAAK,MAAM,KAAK,iBAAiB,KAAK,aAAa;AAAA,EAClF;AAAA,EACA,eAAe;AAAA,EACf,gBAAgB;AAAA,EAChB,YAAY;AAChB;AAKA,MAAO,iBAA+B,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js';
3
- import { Container } from './container.cjs';
4
- import { Route } from './route.cjs';
5
- import { Logger } from './logger.cjs';
6
- import 'node:http';
7
- import './mahameru-response.cjs';
8
- import './types-Cd6Gkoxx.cjs';
9
- import './http-server-error.cjs';
10
- import './mahameru-server-error.cjs';
11
- import './container-error.cjs';
12
- import './module-error.cjs';
13
-
14
- type DiatremaEvents = {
15
- ready: [data: {
16
- mode: "development" | "production";
17
- port: number;
18
- host: string;
19
- }];
20
- };
21
- type DiatremaOptions = {
22
- dev: boolean;
23
- isStandalone: boolean;
24
- rootPath: string;
25
- appPath: string;
26
- routesDir: string;
27
- productionDir: string;
28
- developmentDir: string;
29
- };
30
- type DiatremaDependencies = {
31
- container: Container;
32
- route: Route;
33
- httpServer: HttpServer;
34
- logger: Logger;
35
- };
36
- declare const diatremaDefaultConfig: DiatremaOptions;
37
- /**
38
- * Main Diatrema class that orchestrates the application lifecycle.
39
- */
40
- declare class Diatrema extends EventEmitter<DiatremaEvents> {
41
- #private;
42
- readonly options: DiatremaOptions;
43
- protected readonly dependencies: DiatremaDependencies;
44
- constructor(dependencies: DiatremaDependencies);
45
- constructor(initialOptions: Partial<DiatremaOptions>, dependencies: DiatremaDependencies);
46
- /**
47
- * Indicates whether the Mahameru server has been initialized or not.
48
- * @returns {boolean}
49
- */
50
- get initialized(): boolean;
51
- /**
52
- * Indicates whether the Mahameru server is shutting down or not.
53
- * @returns {boolean}
54
- */
55
- get isShuttingDown(): boolean;
56
- /**
57
- * Initialize the Mahameru server.
58
- */
59
- initialize(): Promise<void>;
60
- /**
61
- * Hot reload the middleware and routes when a file changes in development mode.
62
- */
63
- devHRM(changedFile?: string): Promise<void>;
64
- /**
65
- * Shut down the Mahameru server gracefully.
66
- * @returns {Promise<void>}
67
- */
68
- shutdown(): Promise<void>;
69
- }
70
-
1
+ import _default from './diatrema-CyKJKZv_.cjs';
2
+ import './event-emitter.cjs';
3
+ import './types.cjs';
71
4
  // @ts-ignore
72
- export = Diatrema;
73
- export { type DiatremaDependencies, type DiatremaEvents, type DiatremaOptions, diatremaDefaultConfig };
5
+ export = _default;
6
+ export { b as DiatremaEvents, c as DiatremaOptions, d as diatremaDefaultConfig } from './diatrema-CyKJKZv_.cjs';
7
+ import './logger.cjs';
package/diatrema.d.ts CHANGED
@@ -1,71 +1,4 @@
1
- import { HttpServer } from './http-server.js';
2
- import { EventEmitter } from './event-emitter.js';
3
- import { Container } from './container.js';
4
- import { Route } from './route.js';
5
- import { Logger } from './logger.js';
6
- import 'node:http';
7
- import './mahameru-response.js';
8
- import './types-BmFqDpSs.js';
9
- import './http-server-error.js';
10
- import './mahameru-server-error.js';
11
- import './container-error.js';
12
- import './module-error.js';
13
-
14
- type DiatremaEvents = {
15
- ready: [data: {
16
- mode: "development" | "production";
17
- port: number;
18
- host: string;
19
- }];
20
- };
21
- type DiatremaOptions = {
22
- dev: boolean;
23
- isStandalone: boolean;
24
- rootPath: string;
25
- appPath: string;
26
- routesDir: string;
27
- productionDir: string;
28
- developmentDir: string;
29
- };
30
- type DiatremaDependencies = {
31
- container: Container;
32
- route: Route;
33
- httpServer: HttpServer;
34
- logger: Logger;
35
- };
36
- declare const diatremaDefaultConfig: DiatremaOptions;
37
- /**
38
- * Main Diatrema class that orchestrates the application lifecycle.
39
- */
40
- declare class Diatrema extends EventEmitter<DiatremaEvents> {
41
- #private;
42
- readonly options: DiatremaOptions;
43
- protected readonly dependencies: DiatremaDependencies;
44
- constructor(dependencies: DiatremaDependencies);
45
- constructor(initialOptions: Partial<DiatremaOptions>, dependencies: DiatremaDependencies);
46
- /**
47
- * Indicates whether the Mahameru server has been initialized or not.
48
- * @returns {boolean}
49
- */
50
- get initialized(): boolean;
51
- /**
52
- * Indicates whether the Mahameru server is shutting down or not.
53
- * @returns {boolean}
54
- */
55
- get isShuttingDown(): boolean;
56
- /**
57
- * Initialize the Mahameru server.
58
- */
59
- initialize(): Promise<void>;
60
- /**
61
- * Hot reload the middleware and routes when a file changes in development mode.
62
- */
63
- devHRM(changedFile?: string): Promise<void>;
64
- /**
65
- * Shut down the Mahameru server gracefully.
66
- * @returns {Promise<void>}
67
- */
68
- shutdown(): Promise<void>;
69
- }
70
-
71
- export { type DiatremaDependencies, type DiatremaEvents, type DiatremaOptions, Diatrema as default, diatremaDefaultConfig };
1
+ import './event-emitter.js';
2
+ import './types.js';
3
+ export { b as DiatremaEvents, c as DiatremaOptions, D as default, d as diatremaDefaultConfig } from './diatrema-Bk12jngx.js';
4
+ import './logger.js';