@zenflux/core 0.1.12 → 0.1.13

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 (48) hide show
  1. package/package.json +3 -4
  2. package/src/bases/controller-base.ts +0 -96
  3. package/src/bases/index.ts +0 -6
  4. package/src/bases/initialize-base.ts +0 -32
  5. package/src/bases/object-base.ts +0 -55
  6. package/src/clients/http.ts +0 -152
  7. package/src/command-bases/command-base.ts +0 -82
  8. package/src/command-bases/command-internal.ts +0 -11
  9. package/src/command-bases/command-public.ts +0 -12
  10. package/src/command-bases/command-restful.ts +0 -55
  11. package/src/command-bases/index.ts +0 -8
  12. package/src/errors/command-already-registered.ts +0 -13
  13. package/src/errors/command-not-found.ts +0 -12
  14. package/src/errors/controller-already-registered.ts +0 -13
  15. package/src/errors/controller-already-set.ts +0 -13
  16. package/src/errors/error-with-metadata.ts +0 -14
  17. package/src/errors/force-method-implementation.ts +0 -15
  18. package/src/errors/index-public.ts +0 -1
  19. package/src/errors/index.ts +0 -10
  20. package/src/exports-index.ts +0 -13
  21. package/src/exports.ts +0 -58
  22. package/src/index.ts +0 -18
  23. package/src/interfaces/commands.ts +0 -17
  24. package/src/interfaces/config.ts +0 -23
  25. package/src/interfaces/index.ts +0 -8
  26. package/src/interfaces/logger.ts +0 -19
  27. package/src/interfaces/restful.ts +0 -23
  28. package/src/managers/commands.ts +0 -268
  29. package/src/managers/controllers.ts +0 -40
  30. package/src/managers/export.ts +0 -30
  31. package/src/managers/index.ts +0 -8
  32. package/src/managers/internal.ts +0 -11
  33. package/src/managers/restful.ts +0 -117
  34. package/src/modules/debugger.ts +0 -89
  35. package/src/modules/event-bus/event-bus.ts +0 -204
  36. package/src/modules/logger/config.ts +0 -44
  37. package/src/modules/logger/implementation.ts +0 -287
  38. package/src/modules/logger/index.ts +0 -2
  39. package/src/modules/logger/logger-browser-infra.ts +0 -344
  40. package/src/modules/logger/types/picocolors.d.ts +0 -15
  41. package/src/modules/logger/utils/index.ts +0 -3
  42. package/src/modules/logger/utils/object.ts +0 -19
  43. package/src/modules/logger/utils/rgb.ts +0 -34
  44. package/src/modules/mcp-server/mcp-server.ts +0 -139
  45. package/src/modules/mcp-server/mcp-service.ts +0 -80
  46. package/src/modules/service/service-base.ts +0 -55
  47. package/src/modules/service/service-locator.ts +0 -205
  48. package/src/modules/service/service-with-dependencies-base.ts +0 -51
@@ -1,89 +0,0 @@
1
- import * as util from "util";
2
-
3
- import { isDebugEnabled } from "@zenflux/utils/src/environment";
4
-
5
- import pc from "picocolors";
6
-
7
- import { Logger } from "./logger";
8
-
9
- import { ObjectBase } from "../bases/object-base";
10
-
11
- // Define options interface for Debugger
12
- interface DebuggerOptions {
13
- skipEventBusHook?: boolean;
14
- }
15
-
16
- export class Debugger extends ObjectBase {
17
- private logger!: Logger;
18
-
19
- declare private finalizationRegistry;
20
-
21
- public static getName() {
22
- return "ZenFlux/Core/Modules/Debugger";
23
- }
24
-
25
- public constructor(
26
- owner: ObjectBase | typeof ObjectBase | string,
27
- prefix?: string,
28
- private shouldDebug = Logger.isDebugEnabled(),
29
- options?: DebuggerOptions // Add options parameter
30
- ) {
31
- super();
32
-
33
- if ( shouldDebug ) {
34
- // Pass logger options down if skipEventBusHook is set
35
- const loggerOptions = options?.skipEventBusHook ? { skipEventBusHook: true } : undefined;
36
- this.logger = new Logger( owner, loggerOptions );
37
-
38
- if ( prefix ) {
39
- this.logger.addMessagePrefix( prefix );
40
- }
41
- } else {
42
- // Bypass all the methods
43
- this.log = () => {};
44
- this.dumpDown = () => {};
45
- this.enableCleanupDebug = () => {};
46
- }
47
- }
48
-
49
- public enableCleanupDebug( handle: ObjectBase, id: string = "" ) {
50
- if ( !this.finalizationRegistry ) {
51
- this.finalizationRegistry = new FinalizationRegistry( ( id: string ) => {
52
- this.log( this.constructor, `FinalizationRegistry: ${ id }` );
53
- } );
54
- }
55
-
56
- if ( !id ) {
57
- id = handle.getName() + ":" + handle.getUniqueId();
58
- }
59
-
60
- this.log( this.enableCleanupDebug, `Initialized: '${ id }'` );
61
-
62
- this.finalizationRegistry.register( handle, id );
63
- }
64
-
65
- public log( source: Function, message: string, ...args: any[] ) {
66
- if ( args && args.length > 0 ) {
67
- return this.logger.debug( source, message, ...args );
68
- }
69
-
70
- this.logger.debug( source, message );
71
- }
72
-
73
- public dumpDown( source: Function, object: any, objectName: string = "" ) {
74
- this.log(
75
- source,
76
- `${ objectName ? objectName + ":" : "" } ` + "🔽" + "\n" + pc.green( util.inspect( object, false, null, true ) )
77
- );
78
- }
79
-
80
- public isEnabled() {
81
- return this.shouldDebug;
82
- }
83
- }
84
-
85
- export function createDebugger( owner: ObjectBase | typeof ObjectBase | string, ownerType: string, prefix?: string ) {
86
- const ownerName = typeof owner === "string" ? owner : owner.getName();
87
-
88
- return new Debugger( owner, prefix, isDebugEnabled( ownerType, ownerName ) );
89
- }
@@ -1,204 +0,0 @@
1
- import EventEmitter from "events";
2
-
3
- import { ObjectBase } from "../../bases/object-base";
4
-
5
- export class EventBus extends ObjectBase {
6
- protected static instance: EventBus | null = null;
7
-
8
- private objects = new Map<
9
- string,
10
- {
11
- object: ObjectBase | ObjectBase[];
12
- methods: Function[];
13
- }
14
- >();
15
-
16
- private eventEmitter = new EventEmitter();
17
-
18
- private lastEmittedEvents = new Map<string, any[]>();
19
-
20
- public static getName() {
21
- return "ZenFlux/Core/Modules/EventBus";
22
- }
23
-
24
- public static getInstance(): EventBus {
25
- if ( !EventBus.instance ) {
26
- EventBus.instance = new EventBus();
27
- }
28
-
29
- return EventBus.instance;
30
- }
31
-
32
- public static get $() {
33
- return EventBus.getInstance();
34
- }
35
-
36
- public getObjectNames() {
37
- return Array.from( this.objects.keys() );
38
- }
39
-
40
- public getEventNames() {
41
- this.eventEmitter.eventNames();
42
- }
43
-
44
- public getEventName( objectName: string, methodName: string ) {
45
- return `${ objectName }::${ methodName }`;
46
- }
47
-
48
- public on( objectName: string, methodName: string, callback: ( ...args: any[] ) => void ) {
49
- // Validate that event exists
50
- if ( !this.objects.has( objectName ) ) {
51
- throw new Error( `Object ${ objectName } is not registered` );
52
- }
53
-
54
- this.eventEmitter.on( this.getEventName( objectName, methodName ), callback );
55
- }
56
-
57
- /**
58
- * Function onCalledBeforeDoInvoke(): The difference between this function and on()
59
- * is that this function will call the callback if the event already happened.
60
- */
61
- public onCalledBeforeDoInvoke( objectName: string, methodName: string, callback: ( ...args: any[] ) => void ) {
62
- this.on( objectName, methodName, callback );
63
-
64
- // If event already happened, call the callback
65
- if ( this.objects.has( objectName ) ) {
66
- const objectEntry = this.objects.get( objectName );
67
-
68
- if ( objectEntry ) {
69
- // We don't need to iterate through objects anymore, just check if the event has been emitted
70
- const method = objectEntry.methods.find( ( method ) => method.name === methodName );
71
-
72
- if ( method && this.lastEmittedEvents.has( this.getEventName( objectName, methodName ) ) ) {
73
- callback( ...this.lastEmittedEvents.get( this.getEventName( objectName, methodName ) )! );
74
- }
75
- }
76
- }
77
- }
78
-
79
- public off( objectName: string, methodName: string, callback: ( ...args: any[] ) => void ) {
80
- this.eventEmitter.off( this.getEventName( objectName, methodName ), callback );
81
- }
82
-
83
- public register<T extends ObjectBase>( object: T, methods: Function[] ) {
84
- if ( this.objects.has( object.getName() ) ) {
85
- throw new Error( `Error in: '${ this.getName() }', object: '${ object.getName() }' is already registered` );
86
- }
87
-
88
- this.objects.set( object.getName(), {
89
- object,
90
- methods
91
- } );
92
-
93
- this.hook( object, methods );
94
- }
95
-
96
- public registerMultiInstances<T extends ObjectBase>( object: T, methods: Function[] ) {
97
- const objectName = object.getName();
98
-
99
- // If we already have an entry for this name, check if we need to convert to array
100
- if ( this.objects.has( objectName ) ) {
101
- const existingEntry = this.objects.get( objectName )!;
102
-
103
- // If object is already an array, append to it
104
- if ( Array.isArray( existingEntry.object ) ) {
105
- ( existingEntry.object as ObjectBase[] ).push( object );
106
- }
107
- // Otherwise, convert to array containing both existing and new objects
108
- else {
109
- existingEntry.object = [ existingEntry.object as ObjectBase, object ];
110
- }
111
- }
112
- // Otherwise, create a new entry with the object directly (not in an array)
113
- else {
114
- this.objects.set( objectName, {
115
- object,
116
- methods
117
- } );
118
- }
119
-
120
- // Hook the object
121
- this.hook( object, methods );
122
- }
123
-
124
- public unregister( objectName: string ) {
125
- const entry = this.objects.get( objectName );
126
-
127
- if ( !entry ) {
128
- return false;
129
- }
130
-
131
- // Handle both single object and array cases
132
- if ( Array.isArray( entry.object ) ) {
133
- ( entry.object as ObjectBase[] ).forEach( obj => {
134
- this.unhookObject( obj, entry.methods );
135
- } );
136
- } else {
137
- this.unhookObject( entry.object as ObjectBase, entry.methods );
138
- }
139
-
140
- this.objects.delete( objectName );
141
- }
142
-
143
- // Helper method to unhook a single object
144
- private unhookObject( object: ObjectBase, methods: Function[] ) {
145
- methods.forEach( ( method ) => {
146
- this.ensureFunction( object, method );
147
-
148
- this.eventEmitter.removeAllListeners( this.getEventName( object.getName(), method.name ) );
149
-
150
- ( object as any )[ method.name ] = method;
151
- } );
152
- }
153
-
154
- private unhook( entry: { object: ObjectBase | ObjectBase[]; methods: Function[] } ) {
155
- if ( Array.isArray( entry.object ) ) {
156
- ( entry.object as ObjectBase[] ).forEach( obj => {
157
- this.unhookObject( obj, entry.methods );
158
- } );
159
- } else {
160
- this.unhookObject( entry.object as ObjectBase, entry.methods );
161
- }
162
- }
163
-
164
- private emit<T extends ObjectBase>( object: T, method: Function, ...args: any[] ) {
165
- const eventName = this.getEventName( object.getName(), method.name );
166
-
167
- this.lastEmittedEvents.set( eventName, args );
168
-
169
- return this.eventEmitter.emit( eventName, ...args );
170
- }
171
-
172
- private hook<T extends ObjectBase>( object: T, methods: Function[] ) {
173
- methods.forEach( ( method ) => {
174
- this.ensureFunction( object, method );
175
-
176
- const originalMethod = method;
177
-
178
- const eventBusHook = async( ...args: any[] ) => {
179
- const result = await originalMethod.apply( object, args );
180
-
181
- this.emit( object, method, ...args );
182
-
183
- return result;
184
- };
185
-
186
- // TODO: Use better design pattern to handle losing of function names.
187
- ( object as any )[ method.name ] = new Proxy( eventBusHook, {
188
- get( target, prop ) {
189
- if ( prop === "name" ) {
190
- return method.name;
191
- }
192
-
193
- return target[ prop as keyof typeof target ];
194
- }
195
- } );
196
- } );
197
- }
198
-
199
- private ensureFunction<T extends ObjectBase>( object: T, method: Function ) {
200
- if ( "function" !== typeof method ) {
201
- throw new Error( `Method ${ method } is not a function on ${ object.getName() }` );
202
- }
203
- }
204
- }
@@ -1,44 +0,0 @@
1
- import process from "process";
2
-
3
- const DEFAULT_LOG_LEVEL = "5";
4
-
5
- export function getLoggerDefaultLevel(): string {
6
- return DEFAULT_LOG_LEVEL;
7
- }
8
-
9
- export function getLoggerLogLevel(): number {
10
- return parseInt( process.env.LOGGER_LOG_LEVEL || DEFAULT_LOG_LEVEL, 10 );
11
- }
12
-
13
- export function getLoggerLogLevelString(): string {
14
- switch ( getLoggerLogLevel() ) {
15
- case 0:
16
- return "NONE";
17
- case 1:
18
- return "ERROR";
19
- case 2:
20
- return "WARN";
21
- case 3:
22
- return "ADMIN";
23
- case 4:
24
- return "INFO";
25
- case 5:
26
- return "LOG";
27
- case 6:
28
- return "DEBUG";
29
- default:
30
- return "UNKNOWN";
31
- }
32
- }
33
-
34
- export function isLoggerDebugEnabled(): boolean {
35
- return getLoggerLogLevel() >= 6;
36
- }
37
-
38
- export function isLoggerDisabled(): boolean {
39
- return process.env.LOGGER_DISABLED === "true";
40
- }
41
-
42
- export function isLoggerPreviousSourceDisabled(): boolean {
43
- return process.env.LOGGER_LOG_PREVIOUS_CALLER_SOURCE_DISABLED === "true";
44
- }
@@ -1,287 +0,0 @@
1
- import pc from "picocolors";
2
-
3
- import { EventBus } from "../event-bus/event-bus";
4
- import { ObjectBase } from "../../bases";
5
-
6
- import {
7
- getLoggerLogLevel,
8
- getLoggerLogLevelString,
9
- isLoggerDebugEnabled,
10
- isLoggerDisabled,
11
- isLoggerPreviousSourceDisabled
12
- } from "./config";
13
-
14
- import { LoggerBrowserInfra } from "./logger-browser-infra";
15
-
16
- import { reduceCircularReferences } from "./utils";
17
-
18
- import type * as interfaces from "../../interfaces";
19
-
20
- const DEFAULT_LOG_PREFIX = pc.white( "⚪ - [LOG]" ),
21
- DEFAULT_INFO_PREFIX = pc.blue( "🔵 - [INFO]" ),
22
- DEFAULT_DEBUG_PREFIX = pc.gray( "🟤 - [DEBUG]" ),
23
- DEFAULT_WARN_PREFIX = pc.yellow( "🟡 - [WARN]" ),
24
- DEFAULT_ERROR_PREFIX = pc.red( "🔴 - [ERROR]" ),
25
- DEFAULT_ADMIN_PREFIX = pc.bold( "🟣 - [ADMIN]" );
26
-
27
- const registeredNames: Record<string, boolean> = {};
28
-
29
- type ICaller = interfaces.TCaller;
30
-
31
- interface LoggerOptions {
32
- skipEventBusHook?: boolean;
33
- repeatedly?: boolean;
34
- }
35
-
36
- export class Logger extends LoggerBrowserInfra implements interfaces.ILogger {
37
- private static lastLogTime: number = Date.now();
38
-
39
- private readonly ownerName: string;
40
-
41
- private messagePrefixes: string[] = [];
42
-
43
- private config: LoggerOptions = {};
44
-
45
- public static getName(): string {
46
- return "ZenFlux/Core/Modules/Logger";
47
- }
48
-
49
- public static getLogLevelString(): string {
50
- return getLoggerLogLevelString();
51
- }
52
-
53
- public static getLogLevel(): number {
54
- return getLoggerLogLevel();
55
- }
56
-
57
- public static isDebugEnabled() {
58
- return isLoggerDebugEnabled();
59
- }
60
-
61
- public constructor( owner: any, options?: LoggerOptions ) {
62
- const { repeatedly = false, skipEventBusHook } = options || {};
63
-
64
- super( owner, { repeatedly } );
65
-
66
- this.config = { skipEventBusHook };
67
- this.ownerName = Logger.resolveOwnerName( owner );
68
-
69
- if ( registeredNames[ this.ownerName ] ) {
70
- throw new Error( `Logger for '${ this.ownerName }' already exists` );
71
- }
72
-
73
- registeredNames[ this.ownerName ] = true;
74
-
75
- if ( isLoggerDisabled() ) {
76
- this.disableLoggers();
77
- return;
78
- }
79
-
80
- if ( isLoggerPreviousSourceDisabled() ) {
81
- this.getPreviousSource = () => "";
82
- }
83
-
84
- switch ( getLoggerLogLevel() ) {
85
- case 0:
86
- this.error = () => {};
87
- // falls through
88
- case 1:
89
- this.warn = () => {};
90
- // falls through
91
- case 2:
92
- this.admin = () => {};
93
- // falls through
94
- case 3:
95
- this.info = () => {};
96
- // falls through
97
- case 4:
98
- this.log = () => {};
99
- // falls through
100
- case 5:
101
- this.debug = () => {};
102
- }
103
-
104
- if ( !this.config.skipEventBusHook ) {
105
- EventBus.$.registerMultiInstances( this, [ this.outputEvent ] );
106
- }
107
- }
108
-
109
- public addMessagePrefix( prefix: string ) {
110
- this.messagePrefixes.push( prefix );
111
- }
112
-
113
- public log( caller: ICaller, message: string, ...params: any[] ): void {
114
- this.writeLog( DEFAULT_LOG_PREFIX, caller, message, ...params );
115
- }
116
-
117
- public info( caller: ICaller, message: string, ...params: any[] ): void {
118
- this.writeLog( DEFAULT_INFO_PREFIX, caller, message, ...params );
119
- }
120
-
121
- public debug( caller: ICaller, message: string, ...params: any[] ): void {
122
- this.writeLog( DEFAULT_DEBUG_PREFIX, caller, message, ...params );
123
- }
124
-
125
- public warn( caller: ICaller, message: string, ...params: any[] ): void {
126
- this.writeLog( DEFAULT_WARN_PREFIX, caller, message, ...params );
127
- }
128
-
129
- public error( caller: ICaller, message: string, ...params: any[] ): void {
130
- this.writeLog( DEFAULT_ERROR_PREFIX, caller, message, ...params );
131
- }
132
-
133
- public admin( caller: ICaller, message: string, ...params: any[] ): void {
134
- this.writeLog( DEFAULT_ADMIN_PREFIX, caller, message, ...params );
135
- }
136
-
137
- public startsEmpty( caller: interfaces.TCaller ) {
138
- this.printFunctionNotify( "▶", caller, "" );
139
- }
140
-
141
- public startsWith( caller: interfaces.TCaller, params: string | object ) {
142
- this.printObjectEfficient( "▶", caller, params );
143
- }
144
-
145
- public dump( caller: interfaces.TCaller, params: { [ key: string ]: object | string } = {}, notice = "" ) {
146
- for ( const key in params ) {
147
- if ( typeof params[ key ] === "object" ) {
148
- params[ key ] = JSON.stringify(
149
- LoggerBrowserInfra.useObjectMapper( params[ key ] as object ),
150
- reduceCircularReferences()
151
- );
152
- }
153
-
154
- this.printInLineElement( "dp", caller, key, params[ key ], notice );
155
- }
156
- }
157
-
158
- public drop( caller: interfaces.TCaller, according: { [ key: string ]: string }, data: any ) {
159
- for ( const key in according ) {
160
- this.printInLineElement( "dr", caller, key, according[ key ], "corresponding" );
161
- }
162
-
163
- this.output( data );
164
- }
165
-
166
- /**
167
- * TODO: Should respect debug levels, when to throw...
168
- */
169
- public throw( caller: interfaces.TCaller, output: string, name: string = "null", params = {} ) {
170
- this.printFunctionNotify( "tw", caller, output );
171
-
172
- if ( params ) {
173
- this.printInNextLineObject( "tw", caller, name, params );
174
- }
175
-
176
- throw new Error().stack;
177
- }
178
-
179
- public beep() {
180
- console.log( "\x07" );
181
- }
182
-
183
- public clone() {
184
- return Object.assign( Object.create( Object.getPrototypeOf( this ) ), this );
185
- }
186
-
187
- public outputEvent( _prefix: string, _timeDiff: string, _source: string, _messagePrefix: string, _message: string, _params: any[] ): void {
188
- // Intentionally left blank, can be hooked via EventBus.
189
- }
190
-
191
- private static resolveOwnerName( owner: any ): string {
192
- if ( typeof owner === "string" ) {
193
- return owner;
194
- }
195
-
196
- if ( owner instanceof ObjectBase ) {
197
- return owner.getName();
198
- }
199
-
200
- if ( owner && typeof owner.getName === "function" ) {
201
- return owner.getName();
202
- }
203
-
204
- return "UnknownOwner";
205
- }
206
-
207
- private disableLoggers() {
208
- this.error = () => {};
209
- this.warn = () => {};
210
- this.admin = () => {};
211
- this.info = () => {};
212
- this.log = () => {};
213
- this.debug = () => {};
214
- }
215
-
216
- private getStackTrace(): any[] {
217
- const stackTrace = ( new Error().stack || "" ).split( "\n" );
218
- const stackLines = stackTrace.slice( 1 );
219
-
220
- const stackRegex = / at (.+?) \((.+?)\)/;
221
- const result = [];
222
-
223
- for ( const line of stackLines ) {
224
- const match = line.match( stackRegex );
225
-
226
- if ( match ) {
227
- const [ , context, file ] = match;
228
- const parsedLine: any = { context, file };
229
-
230
- if ( line.startsWith( "new" ) ) {
231
- parsedLine.isNew = true;
232
- parsedLine.object = context.split( " " )[ 1 ];
233
- } else if ( context !== "Object.<anonymous>" ) {
234
- parsedLine.object = context;
235
- }
236
-
237
- result.push( parsedLine );
238
- }
239
- }
240
-
241
- return result;
242
- }
243
-
244
- public getPreviousSource(): string {
245
- const stack = this.getStackTrace()
246
- .filter( ( line: any ) => line.file.includes( "/src/" ) )
247
- .filter( ( line: any ) => !line.file.includes( "logger.ts" ) )
248
- .filter( ( line: any ) => !line.file.includes( "debugger.ts" ) )
249
- .filter( ( line: any ) => !line.file.includes( "/node_modules/" ) );
250
-
251
- let previousSource = "";
252
-
253
- const previousCaller = stack[ 1 ]?.object?.split( "." );
254
-
255
- if ( previousCaller?.length > 1 ) {
256
- const previousCallerName = previousCaller[ 0 ],
257
- previousCallerMethod = previousCaller[ 1 ];
258
-
259
- previousSource = `${ previousCallerName }::${ previousCallerMethod }]` + "[";
260
- }
261
-
262
- return previousSource;
263
- }
264
-
265
- private writeLog( prefix: string, caller: ICaller, message: string, ...params: any[] ): void {
266
- const source = this.getPreviousSource() + pc.white( `${ this.ownerName }::${ this.getCallerName( caller ) }` );
267
-
268
- let messagePrefix = "";
269
-
270
- if ( this.messagePrefixes.length ) {
271
- messagePrefix = `[${ this.messagePrefixes.join( "][" ) }]`;
272
- }
273
-
274
- const timeDiff = ( Date.now() - Logger.lastLogTime ).toString().padStart( 4, "0" );
275
-
276
- this.outputEvent( prefix, timeDiff, source, messagePrefix, message, params );
277
-
278
- const output = `${ prefix }[+${ timeDiff }ms][${ source }]${ messagePrefix }: ${ message }`;
279
-
280
- console.log( output, ...params );
281
-
282
- Logger.lastLogTime = Date.now();
283
- }
284
- }
285
-
286
- export default Logger;
287
-
@@ -1,2 +0,0 @@
1
- export * from "./config";
2
- export * from "./implementation";