@zenflux/core 0.1.29 → 0.1.30

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 (59) hide show
  1. package/dist/.tsconfig.probe.tsbuildinfo +1 -0
  2. package/dist/src/exports.d.ts +0 -3
  3. package/dist/src/index.d.ts +0 -5
  4. package/dist/src/modules/logger/implementation.d.ts +1 -0
  5. package/dist/zenflux-core.cjs +37 -119
  6. package/dist/zenflux-core.d.ts +3 -32
  7. package/dist/zenflux-core.mjs +38 -117
  8. package/package.json +2 -2
  9. package/src/bases/cache-base.ts +64 -0
  10. package/src/bases/controller-base.ts +96 -0
  11. package/src/bases/index.ts +9 -0
  12. package/src/bases/initialize-base.ts +32 -0
  13. package/src/bases/object-base.ts +55 -0
  14. package/src/bases/singleton-base.ts +18 -0
  15. package/src/clients/http.ts +152 -0
  16. package/src/command-bases/command-base.ts +80 -0
  17. package/src/command-bases/command-internal.ts +11 -0
  18. package/src/command-bases/command-public.ts +12 -0
  19. package/src/command-bases/command-restful.ts +55 -0
  20. package/src/command-bases/index.ts +8 -0
  21. package/src/errors/command-already-registered.ts +13 -0
  22. package/src/errors/command-not-found.ts +12 -0
  23. package/src/errors/controller-already-registered.ts +13 -0
  24. package/src/errors/controller-already-set.ts +13 -0
  25. package/src/errors/error-with-metadata.ts +29 -0
  26. package/src/errors/force-method-implementation.ts +15 -0
  27. package/src/errors/index-public.ts +1 -0
  28. package/src/errors/index.ts +10 -0
  29. package/src/exports-index.ts +15 -0
  30. package/src/exports.ts +55 -0
  31. package/src/index.ts +18 -0
  32. package/src/interfaces/commands.ts +17 -0
  33. package/src/interfaces/config.ts +23 -0
  34. package/src/interfaces/index.ts +8 -0
  35. package/src/interfaces/logger.ts +19 -0
  36. package/src/interfaces/restful.ts +23 -0
  37. package/src/managers/commands.ts +269 -0
  38. package/src/managers/controllers.ts +40 -0
  39. package/src/managers/export.ts +30 -0
  40. package/src/managers/index.ts +8 -0
  41. package/src/managers/internal.ts +11 -0
  42. package/src/managers/restful.ts +117 -0
  43. package/src/modules/debugger.ts +86 -0
  44. package/src/modules/event-bus/event-bus.ts +238 -0
  45. package/src/modules/export.ts +6 -0
  46. package/src/modules/logger/config.ts +66 -0
  47. package/src/modules/logger/create-logger.ts +38 -0
  48. package/src/modules/logger/implementation.ts +371 -0
  49. package/src/modules/logger/index.ts +3 -0
  50. package/src/modules/logger/logger-browser-infra.ts +364 -0
  51. package/src/modules/logger/types/picocolors.d.ts +15 -0
  52. package/src/modules/logger/utils/index.ts +3 -0
  53. package/src/modules/logger/utils/object.ts +19 -0
  54. package/src/modules/logger/utils/rgb.ts +34 -0
  55. package/src/modules/mcp-server/mcp-server.ts +139 -0
  56. package/src/modules/mcp-server/mcp-service.ts +80 -0
  57. package/src/modules/service/service-base.ts +55 -0
  58. package/src/modules/service/service-locator.ts +205 -0
  59. package/src/modules/service/service-with-dependencies-base.ts +51 -0
@@ -0,0 +1,238 @@
1
+ import { ObjectBase } from "../../bases/object-base";
2
+
3
+ // Minimal event emitter that works in both Node and the browser.
4
+ class SimpleEventEmitter {
5
+ private listeners = new Map<string, Set<Function>>();
6
+
7
+ public on( event: string, callback: Function ) {
8
+ if ( ! this.listeners.has( event ) ) {
9
+ this.listeners.set( event, new Set() );
10
+ }
11
+
12
+ this.listeners.get( event )!.add( callback );
13
+ }
14
+
15
+ public off( event: string, callback: Function ) {
16
+ this.listeners.get( event )?.delete( callback );
17
+ }
18
+
19
+ public emit( event: string, ...args: any[] ) {
20
+ const callbacks = this.listeners.get( event );
21
+
22
+ if ( ! callbacks || callbacks.size === 0 ) {
23
+ return false;
24
+ }
25
+
26
+ callbacks.forEach( ( cb ) => cb( ...args ) );
27
+ return true;
28
+ }
29
+
30
+ public removeAllListeners( event: string ) {
31
+ this.listeners.delete( event );
32
+ }
33
+
34
+ public eventNames() {
35
+ return Array.from( this.listeners.keys() );
36
+ }
37
+ }
38
+
39
+ export class EventBus extends ObjectBase {
40
+ protected static instance: EventBus | null = null;
41
+
42
+ private objects = new Map<
43
+ string,
44
+ {
45
+ object: ObjectBase | ObjectBase[];
46
+ methods: Function[];
47
+ }
48
+ >();
49
+
50
+ private eventEmitter = new SimpleEventEmitter();
51
+
52
+ private lastEmittedEvents = new Map<string, any[]>();
53
+
54
+ public static getName() {
55
+ return "ZenFlux/Core/Modules/EventBus";
56
+ }
57
+
58
+ public static getInstance(): EventBus {
59
+ if ( !EventBus.instance ) {
60
+ EventBus.instance = new EventBus();
61
+ }
62
+
63
+ return EventBus.instance;
64
+ }
65
+
66
+ public static get $() {
67
+ return EventBus.getInstance();
68
+ }
69
+
70
+ public getObjectNames() {
71
+ return Array.from( this.objects.keys() );
72
+ }
73
+
74
+ public getEventNames() {
75
+ this.eventEmitter.eventNames();
76
+ }
77
+
78
+ public getEventName( objectName: string, methodName: string ) {
79
+ return `${ objectName }::${ methodName }`;
80
+ }
81
+
82
+ public on( objectName: string, methodName: string, callback: ( ...args: any[] ) => void ) {
83
+ // Validate that event exists
84
+ if ( !this.objects.has( objectName ) ) {
85
+ throw new Error( `Object ${ objectName } is not registered` );
86
+ }
87
+
88
+ this.eventEmitter.on( this.getEventName( objectName, methodName ), callback );
89
+ }
90
+
91
+ /**
92
+ * Function onCalledBeforeDoInvoke(): The difference between this function and on()
93
+ * is that this function will call the callback if the event already happened.
94
+ */
95
+ public onCalledBeforeDoInvoke( objectName: string, methodName: string, callback: ( ...args: any[] ) => void ) {
96
+ this.on( objectName, methodName, callback );
97
+
98
+ // If event already happened, call the callback
99
+ if ( this.objects.has( objectName ) ) {
100
+ const objectEntry = this.objects.get( objectName );
101
+
102
+ if ( objectEntry ) {
103
+ // We don't need to iterate through objects anymore, just check if the event has been emitted
104
+ const method = objectEntry.methods.find( ( method ) => method.name === methodName );
105
+
106
+ if ( method && this.lastEmittedEvents.has( this.getEventName( objectName, methodName ) ) ) {
107
+ callback( ...this.lastEmittedEvents.get( this.getEventName( objectName, methodName ) )! );
108
+ }
109
+ }
110
+ }
111
+ }
112
+
113
+ public off( objectName: string, methodName: string, callback: ( ...args: any[] ) => void ) {
114
+ this.eventEmitter.off( this.getEventName( objectName, methodName ), callback );
115
+ }
116
+
117
+ public register<T extends ObjectBase>( object: T, methods: Function[] ) {
118
+ if ( this.objects.has( object.getName() ) ) {
119
+ throw new Error( `Error in: '${ this.getName() }', object: '${ object.getName() }' is already registered` );
120
+ }
121
+
122
+ this.objects.set( object.getName(), {
123
+ object,
124
+ methods
125
+ } );
126
+
127
+ this.hook( object, methods );
128
+ }
129
+
130
+ public registerMultiInstances<T extends ObjectBase>( object: T, methods: Function[] ) {
131
+ const objectName = object.getName();
132
+
133
+ // If we already have an entry for this name, check if we need to convert to array
134
+ if ( this.objects.has( objectName ) ) {
135
+ const existingEntry = this.objects.get( objectName )!;
136
+
137
+ // If object is already an array, append to it
138
+ if ( Array.isArray( existingEntry.object ) ) {
139
+ ( existingEntry.object as ObjectBase[] ).push( object );
140
+ }
141
+ // Otherwise, convert to array containing both existing and new objects
142
+ else {
143
+ existingEntry.object = [ existingEntry.object as ObjectBase, object ];
144
+ }
145
+ }
146
+ // Otherwise, create a new entry with the object directly (not in an array)
147
+ else {
148
+ this.objects.set( objectName, {
149
+ object,
150
+ methods
151
+ } );
152
+ }
153
+
154
+ // Hook the object
155
+ this.hook( object, methods );
156
+ }
157
+
158
+ public unregister( objectName: string ) {
159
+ const entry = this.objects.get( objectName );
160
+
161
+ if ( !entry ) {
162
+ return false;
163
+ }
164
+
165
+ // Handle both single object and array cases
166
+ if ( Array.isArray( entry.object ) ) {
167
+ ( entry.object as ObjectBase[] ).forEach( obj => {
168
+ this.unhookObject( obj, entry.methods );
169
+ } );
170
+ } else {
171
+ this.unhookObject( entry.object as ObjectBase, entry.methods );
172
+ }
173
+
174
+ this.objects.delete( objectName );
175
+ }
176
+
177
+ // Helper method to unhook a single object
178
+ private unhookObject( object: ObjectBase, methods: Function[] ) {
179
+ methods.forEach( ( method ) => {
180
+ this.ensureFunction( object, method );
181
+
182
+ this.eventEmitter.removeAllListeners( this.getEventName( object.getName(), method.name ) );
183
+
184
+ ( object as any )[ method.name ] = method;
185
+ } );
186
+ }
187
+
188
+ private unhook( entry: { object: ObjectBase | ObjectBase[]; methods: Function[] } ) {
189
+ if ( Array.isArray( entry.object ) ) {
190
+ ( entry.object as ObjectBase[] ).forEach( obj => {
191
+ this.unhookObject( obj, entry.methods );
192
+ } );
193
+ } else {
194
+ this.unhookObject( entry.object as ObjectBase, entry.methods );
195
+ }
196
+ }
197
+
198
+ private emit<T extends ObjectBase>( object: T, method: Function, ...args: any[] ) {
199
+ const eventName = this.getEventName( object.getName(), method.name );
200
+
201
+ this.lastEmittedEvents.set( eventName, args );
202
+
203
+ return this.eventEmitter.emit( eventName, ...args );
204
+ }
205
+
206
+ private hook<T extends ObjectBase>( object: T, methods: Function[] ) {
207
+ methods.forEach( ( method ) => {
208
+ this.ensureFunction( object, method );
209
+
210
+ const originalMethod = method;
211
+
212
+ const eventBusHook = async( ...args: any[] ) => {
213
+ const result = await originalMethod.apply( object, args );
214
+
215
+ this.emit( object, method, ...args );
216
+
217
+ return result;
218
+ };
219
+
220
+ // TODO: Use better design pattern to handle losing of function names.
221
+ ( object as any )[ method.name ] = new Proxy( eventBusHook, {
222
+ get( target, prop ) {
223
+ if ( prop === "name" ) {
224
+ return method.name;
225
+ }
226
+
227
+ return target[ prop as keyof typeof target ];
228
+ }
229
+ } );
230
+ } );
231
+ }
232
+
233
+ private ensureFunction<T extends ObjectBase>( object: T, method: Function ) {
234
+ if ( "function" !== typeof method ) {
235
+ throw new Error( `Method ${ method } is not a function on ${ object.getName() }` );
236
+ }
237
+ }
238
+ }
@@ -0,0 +1,6 @@
1
+ export { EventBus } from "./event-bus/event-bus";
2
+ export { Logger } from "./logger/implementation";
3
+ export { createLogger } from "./logger/create-logger";
4
+ export { ServiceBase } from "./service/service-base";
5
+ export { ServiceLocator } from "./service/service-locator";
6
+ export { ServiceWithDependenciesBase } from "./service/service-with-dependencies-base";
@@ -0,0 +1,66 @@
1
+ const DEFAULT_LOG_LEVEL = "5";
2
+
3
+ // Note: avoid importing `process` so browser bundles (Vite) don't inject an
4
+ // external shim that throws. Instead, safely peek at both process.env (Node)
5
+ // and import.meta.env (bundlers) when present.
6
+ function getEnvVar( key: string ): string | undefined {
7
+ // Node / SSR
8
+ if ( typeof process !== "undefined" && process?.env ) {
9
+ return process.env[ key ] ?? process.env[ `VITE_${ key }` ];
10
+ }
11
+
12
+ // Vite / ESM build-time injected env
13
+ if ( typeof import.meta !== "undefined" && ( import.meta as any ).env ) {
14
+ const env = ( import.meta as any ).env;
15
+ return env[ key ] ?? env[ `VITE_${ key }` ];
16
+ }
17
+
18
+ return undefined;
19
+ }
20
+
21
+ export function getLoggerDefaultLevel(): string {
22
+ return DEFAULT_LOG_LEVEL;
23
+ }
24
+
25
+ export function getLoggerLogLevel(): number {
26
+ const raw = getEnvVar( "LOGGER_LOG_LEVEL" );
27
+ return parseInt( raw ?? DEFAULT_LOG_LEVEL, 10 );
28
+ }
29
+
30
+ export function getLoggerTimeFormat(): string {
31
+ // Example: "[Y-M-D h:m:s]" or "Y/M/D". Empty string disables timestamp prefix.
32
+ return ( getEnvVar( "LOGGER_TIME_FORMAT" ) || "" ).trim();
33
+ }
34
+
35
+ export function getLoggerLogLevelString(): string {
36
+ switch ( getLoggerLogLevel() ) {
37
+ case 0:
38
+ return "NONE";
39
+ case 1:
40
+ return "ERROR";
41
+ case 2:
42
+ return "WARN";
43
+ case 3:
44
+ return "ADMIN";
45
+ case 4:
46
+ return "INFO";
47
+ case 5:
48
+ return "LOG";
49
+ case 6:
50
+ return "DEBUG";
51
+ default:
52
+ return "UNKNOWN";
53
+ }
54
+ }
55
+
56
+ export function isLoggerDebugEnabled(): boolean {
57
+ return getLoggerLogLevel() >= 6;
58
+ }
59
+
60
+ export function isLoggerDisabled(): boolean {
61
+ return getEnvVar( "LOGGER_DISABLED" ) === "true";
62
+ }
63
+
64
+ export function isLoggerPreviousSourceDisabled(): boolean {
65
+ return getEnvVar( "LOGGER_LOG_PREVIOUS_CALLER_SOURCE_DISABLED" ) === "true";
66
+ }
@@ -0,0 +1,38 @@
1
+ import Implementation from "./implementation";
2
+
3
+ type LoggerOptions = {
4
+ skipEventBusHook?: boolean;
5
+ repeatedly?: boolean;
6
+ };
7
+
8
+ export class ZenFluxLogger extends Implementation {}
9
+
10
+ // Cache for HMR support - prevents "Logger already exists" errors during hot reload
11
+ const loggerCache = new Map<string, ZenFluxLogger>();
12
+
13
+ const resolveOwnerName = ( owner: string | object ): string => {
14
+ if ( typeof owner === "string" ) {
15
+ return owner;
16
+ }
17
+
18
+ if ( owner && typeof ( owner as any ).getName === "function" ) {
19
+ return ( owner as any ).getName();
20
+ }
21
+
22
+ return "UnknownOwner";
23
+ };
24
+
25
+ export const createLogger = ( owner: string | object, options?: LoggerOptions ) => {
26
+ const ownerName = resolveOwnerName( owner );
27
+
28
+ // Return cached logger if it exists (HMR support)
29
+ const cached = loggerCache.get( ownerName );
30
+ if ( cached ) {
31
+ return cached;
32
+ }
33
+
34
+ const logger = new ZenFluxLogger( owner, options );
35
+ loggerCache.set( ownerName, logger );
36
+
37
+ return logger;
38
+ };