@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,269 @@
1
+ /**
2
+ * @author Leonid Vinikov <leonidvinikov@gmail.com>
3
+ * @description Responsible for manging/running/hooking commands.
4
+ */
5
+ import { ObjectBase } from "../bases/object-base";
6
+
7
+ import { CommandNotFound } from "../errors/command-not-found";
8
+ import { CommandAlreadyRegistered } from "../errors/command-already-registered";
9
+ import { createLogger } from "../modules/logger/create-logger";
10
+
11
+ import type {
12
+ TCommandCallbackType,
13
+
14
+ IOnHookAffectInterface,
15
+ IOnHookInterface,
16
+
17
+ ICommandArgsInterface,
18
+ ICommandOptionsInterface,
19
+
20
+ ILogger,
21
+ } from "../interfaces";
22
+
23
+ import type { ControllerBase } from "../bases/controller-base";
24
+ import type { CommandBase } from "../command-bases/command-base";
25
+
26
+ export class Commands extends ObjectBase {
27
+ public static readonly trace:string[] = [];
28
+
29
+ private current: { [ key: string ]: CommandBase } = {};
30
+ private currentArgs: { [ key: string ]: ICommandArgsInterface } = {};
31
+
32
+ private trace:string[] = [];
33
+
34
+ private commands: { [ key: string ]: typeof CommandBase } = {};
35
+
36
+ private onBeforeHooks: IOnHookInterface = {};
37
+ private onBeforeUIHooks: IOnHookInterface = {};
38
+
39
+ private onAfterHooks: IOnHookInterface = {};
40
+ private onAfterOnceHooks: IOnHookInterface = {};
41
+ private onAfterUIHooks: IOnHookInterface = {};
42
+ private onAfterAffectHooks: IOnHookAffectInterface = {};
43
+
44
+ private readonly logger: ILogger;
45
+
46
+ public static getName() {
47
+ return "ZenFlux/Core/Managers/Commands";
48
+ }
49
+
50
+ private static async runCallbacks( callbacks: Array<Function>, args: ICommandArgsInterface = {}, options: ICommandOptionsInterface = {} ) {
51
+ const callbacksLength = callbacks?.length || 0;
52
+
53
+ for ( let i = 0; i < callbacksLength; ++i ) {
54
+ const callback = options.pop === true ? callbacks.pop() : callbacks[ i ];
55
+
56
+ if ( ! callback ) {
57
+ throw new Error( "Callback is not defined." );
58
+ }
59
+
60
+ await callback( args, options );
61
+ }
62
+ }
63
+
64
+ private static hookCommand( stack: any, hookCommand: string, context: TCommandCallbackType | string ) {
65
+ if ( ! stack[ hookCommand ] ) {
66
+ stack[ hookCommand ] = [];
67
+ }
68
+
69
+ stack[ hookCommand ].push( context );
70
+ }
71
+
72
+ public constructor() {
73
+ super();
74
+
75
+ const type = ( this.constructor as typeof Commands );
76
+
77
+ this.logger = createLogger( type );
78
+ this.logger.startsEmpty( this.constructor );
79
+ }
80
+
81
+ public async run( command: string | CommandBase, args: ICommandArgsInterface = {}, options: any = {} ) {
82
+ if ( typeof command === "string" ) {
83
+ command = this.getCommandInstance( command, args, options );
84
+ }
85
+
86
+ this.attachCurrent( command, args );
87
+
88
+ const result = await this.runInstance( command, args, options );
89
+
90
+ this.detachCurrent( command );
91
+
92
+ return result;
93
+ }
94
+
95
+ public register( commands: { [ key: string ]: typeof CommandBase }, controller: ControllerBase ) {
96
+ const result: { [ key: string ]: typeof CommandBase } = {};
97
+
98
+ Object.values( commands ).forEach( ( command ) => {
99
+ const commandName = command.getName();
100
+
101
+ if ( this.commands[ commandName ] ) {
102
+ throw new CommandAlreadyRegistered( command );
103
+ }
104
+
105
+ command.setController( controller );
106
+
107
+ this.commands[ commandName ] = command;
108
+
109
+ result[ commandName ] = command;
110
+ } );
111
+
112
+ return result;
113
+ }
114
+
115
+ public getAll() {
116
+ return this.commands;
117
+ }
118
+
119
+ public getByName( name: string ) {
120
+ return this.commands[ name ];
121
+ }
122
+
123
+ public getLogger() {
124
+ return this.logger;
125
+ }
126
+
127
+ public getCommandInstance( name: string, args: ICommandArgsInterface = {}, options = {} ): CommandBase {
128
+ const CommandClass = this.commands[ name ];
129
+
130
+ if ( ! CommandClass ) {
131
+ throw new CommandNotFound( name );
132
+ }
133
+
134
+ // @ts-ignore
135
+ return new CommandClass( args, options );
136
+ }
137
+
138
+ /**
139
+ * Used to set hooks that effects only data and not UI.
140
+ */
141
+ public onBefore( hookCommand: string, callback: TCommandCallbackType ) {
142
+ Commands.hookCommand( this.onBeforeHooks, hookCommand, callback );
143
+ }
144
+
145
+ /**
146
+ * Used to set hooks that effects only UI.
147
+ */
148
+ public onBeforeUI( hookCommand: string, callback: TCommandCallbackType ) {
149
+ Commands.hookCommand( this.onBeforeUIHooks, hookCommand, callback );
150
+ }
151
+
152
+ /**
153
+ * Used to set hooks that effects only data and not UI.
154
+ */
155
+ public onAfter( hookCommand: string, callback: TCommandCallbackType ) {
156
+ Commands.hookCommand( this.onAfterHooks, hookCommand, callback );
157
+ }
158
+
159
+ /**
160
+ * Used to set hooks that effects only UI.
161
+ */
162
+ public onAfterUI( command: string, callback: TCommandCallbackType ) {
163
+ Commands.hookCommand( this.onAfterUIHooks, command, callback );
164
+ }
165
+
166
+ /**
167
+ * Used to set hooks that effects only data and not UI.
168
+ */
169
+ public onAfterOnce( command: string, callback: TCommandCallbackType ) {
170
+ Commands.hookCommand( this.onAfterOnceHooks, command, callback );
171
+ }
172
+
173
+ /**
174
+ * Used to register a trigger that runs command after `hookCommand` run's.
175
+ */
176
+ public onAfterAffect( hookCommand: string, affectCommand: string ) {
177
+ Commands.hookCommand( this.onAfterAffectHooks, hookCommand, affectCommand );
178
+ }
179
+
180
+ protected onBeforeRun( command: CommandBase, args: ICommandArgsInterface = {}, options = {} ) {
181
+ if ( this.onBeforeHooks[ command.getName() ] ) {
182
+ const callbacks = this.onBeforeHooks[ command.getName() ];
183
+ callbacks.forEach( ( callback ) => callback( args, options ) );
184
+ }
185
+
186
+ if ( this.onBeforeUIHooks[ command.getName() ] ) {
187
+ const callbacks = this.onBeforeUIHooks[ command.getName() ];
188
+ callbacks.forEach( ( callback ) => callback( args, options ) );
189
+ }
190
+ }
191
+
192
+ protected async runInstance( command: CommandBase, args: ICommandArgsInterface = {}, options = {} ) {
193
+ let result: any = null;
194
+
195
+ this.logger.startsWith( this.runInstance, {
196
+ command: command.getName(),
197
+ options,
198
+ args,
199
+ } );
200
+
201
+ this.onBeforeRun( command, args, options );
202
+
203
+ result = await command.run();
204
+
205
+ await this.onAfterRun( command, args, options, result );
206
+
207
+ return result;
208
+ }
209
+
210
+ protected async onAfterRun( command: CommandBase, args: ICommandArgsInterface, options: {}, result: any ) {
211
+ options = Object.assign( {}, options );
212
+ if ( this.onAfterAffectHooks[ command.getName() ] ) {
213
+ this.onAfterAffectHooks[ command.getName() ].forEach( ( command ) => {
214
+ args.result = result;
215
+
216
+ result = this.run( command.toString(), args, options );
217
+ } );
218
+
219
+ result = await result;
220
+ }
221
+
222
+ if ( this.onAfterHooks ) {
223
+ args.result = result;
224
+
225
+ await Commands.runCallbacks(
226
+ Object.assign( [], this.onAfterHooks[ command.getName() ] ),
227
+ args,
228
+ options
229
+ );
230
+ }
231
+
232
+ if ( this.onAfterOnceHooks ) {
233
+ await Commands.runCallbacks(
234
+ this.onAfterOnceHooks[ command.getName() ],
235
+ args,
236
+ {
237
+ ... options,
238
+ pop: true, // Delete after run.
239
+ }
240
+ );
241
+ }
242
+
243
+ if ( this.onAfterUIHooks ) {
244
+ Commands.runCallbacks(
245
+ Object.assign( [], this.onAfterUIHooks[ command.getName() ] ),
246
+ args,
247
+ options
248
+ );
249
+ }
250
+
251
+ return result;
252
+ }
253
+
254
+ protected attachCurrent( command: CommandBase, args:any = {} ) {
255
+ this.current[ command.getName() ] = command;
256
+ this.currentArgs[ command.getName() ] = args;
257
+
258
+ this.trace.push( command.getName() );
259
+ Commands.trace.push( command.getName() );
260
+ }
261
+
262
+ protected detachCurrent( command: CommandBase ) {
263
+ delete this.current[ command.getName() ];
264
+ delete this.currentArgs[ command.getName() ];
265
+
266
+ Commands.trace.pop();
267
+ this.trace.pop();
268
+ }
269
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @author Leonid Vinikov <leonidvinikov@gmail.com>
3
+ * @description Responsible for manging controllers, each controller is global singleton instance.
4
+ */
5
+ import { ControllerAlreadyRegistered } from "../errors/controller-already-registered";
6
+
7
+ import { ObjectBase } from "../bases/object-base";
8
+
9
+ import type { ControllerBase } from "../bases/controller-base";
10
+
11
+ export class Controllers extends ObjectBase {
12
+ private controllers: { [ key: string ]: ControllerBase } = {};
13
+
14
+ public constructor() {
15
+ super();
16
+ }
17
+
18
+ public static getName() {
19
+ return "ZenFlux/Core/Managers/Controllers";
20
+ }
21
+
22
+ public get( name: string ) {
23
+ return this.controllers[ name ];
24
+ }
25
+
26
+ public getAll() {
27
+ return this.controllers;
28
+ }
29
+
30
+ public register( controller: ControllerBase ) {
31
+ if ( this.controllers[ controller.getName() ] ) {
32
+ throw new ControllerAlreadyRegistered( controller );
33
+ }
34
+
35
+ // Register.
36
+ this.controllers[ controller.getName() ] = controller;
37
+
38
+ return controller;
39
+ }
40
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @author Leonid Vinikov <leonidvinikov@gmail.com>
3
+ */
4
+ import { Commands } from "./commands";
5
+ import { Controllers } from "./controllers";
6
+ import { Restful } from "./restful";
7
+ import { Internal } from "./internal";
8
+
9
+ import type { IAPIConfig } from "../interfaces";
10
+
11
+ export const afterInitializeCallbacks: ( () => void )[] = [];
12
+
13
+ export function initialize( config: IAPIConfig ) {
14
+ commands = new Commands();
15
+ controllers = new Controllers();
16
+ restful = new Restful( config );
17
+ internal = new Internal();
18
+ }
19
+
20
+ export function destroy() {
21
+ commands = {} as Commands;
22
+ controllers = {} as Controllers;
23
+ restful = {} as Restful;
24
+ internal = {} as Internal;
25
+ }
26
+
27
+ export let commands = {} as Commands;
28
+ export let controllers = {} as Controllers;
29
+ export let restful = {} as Restful;
30
+ export let internal = {} as Internal;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @author Leonid Vinikov <leonidvinikov@gmail.com>
3
+ */
4
+
5
+ export { Commands } from "./commands";
6
+ export { Controllers } from "./controllers";
7
+ export { Restful } from "./restful";
8
+ export { Internal } from "./internal";
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @author Leonid Vinikov <leonidvinikov@gmail.com>
3
+ * @description Responsible for manging internal commands, To serve commands that are not triggered by user.
4
+ */
5
+ import { Commands } from "./commands";
6
+
7
+ export class Internal extends Commands {
8
+ public static getName() {
9
+ return "ZenFlux/Core/Managers/Internal";
10
+ }
11
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * @author Leonid Vinikov <leonidvinikov@gmail.com>
3
+ * @description Responsible for manging restful commands which are wrappers for HTTP requests.
4
+ */
5
+ import { Commands } from "./commands";
6
+
7
+ import { Http } from "../clients/http";
8
+
9
+ import {
10
+ E_RESPONSE_HANDLER_TYPE,
11
+ E_HTTP_METHOD_TYPE
12
+ } from "../interfaces";
13
+
14
+ import type { CommandRestful } from "../command-bases";
15
+
16
+ import type {
17
+ IAPIConfig,
18
+ ICommandArgsInterface,
19
+ ICommandOptionsInterface,
20
+
21
+ TErrorHandlerCallbackType,
22
+ TPossibleHandlersType,
23
+ TResponseFilterCallbackType,
24
+ TResponseHandlerCallbackType
25
+ } from "../interfaces";
26
+
27
+ export class Restful extends Commands {
28
+ private static client: Http;
29
+
30
+ public currentHttpMethod: E_HTTP_METHOD_TYPE;
31
+
32
+ public static getName() {
33
+ return "ZenFlux/Core/Managers/Restful";
34
+ }
35
+
36
+ public constructor( Config: IAPIConfig ) {
37
+ super();
38
+
39
+ Restful.client = new Http( Config.baseURL, Config.requestInit );
40
+ }
41
+
42
+ public getClient() {
43
+ return Restful.client;
44
+ }
45
+
46
+ public get( command: string, args: ICommandArgsInterface = {}, options: ICommandOptionsInterface = {} ) {
47
+ this.currentHttpMethod = E_HTTP_METHOD_TYPE.GET;
48
+
49
+ return super.run( command, args, options );
50
+ }
51
+
52
+ public update( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
53
+ this.currentHttpMethod = E_HTTP_METHOD_TYPE.PATCH;
54
+
55
+ return super.run( command, args, options );
56
+ }
57
+
58
+ public delete( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
59
+ this.currentHttpMethod = E_HTTP_METHOD_TYPE.DELETE;
60
+
61
+ return super.run( command, args, options );
62
+ }
63
+
64
+ public create( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
65
+ this.currentHttpMethod = E_HTTP_METHOD_TYPE.POST;
66
+
67
+ return super.run( command, args, options );
68
+ }
69
+
70
+ protected async runInstance( command: CommandRestful, args: ICommandArgsInterface = {}, options: {} = {} ) {
71
+ if ( ! this.currentHttpMethod ) {
72
+ throw new Error( "Cannot run directly use one of the http methods: \"get\", \"update\", \"delete, \"create" );
73
+ }
74
+
75
+ // New args.
76
+ const newArgs = {
77
+ type: this.currentHttpMethod,
78
+ args: {
79
+ query: {},
80
+ data: {},
81
+ },
82
+ };
83
+
84
+ if ( E_HTTP_METHOD_TYPE.GET === this.currentHttpMethod ) {
85
+ newArgs.args.query = args;
86
+ } else {
87
+ newArgs.args.data = args;
88
+ }
89
+
90
+ args.result = await super.runInstance( command, newArgs, options );
91
+
92
+ // Clear method type.
93
+ this.currentHttpMethod = E_HTTP_METHOD_TYPE.__EMPTY__;
94
+
95
+ return args.result;
96
+ }
97
+
98
+ /**
99
+ * Handlers on return true will swallow the request.
100
+ */
101
+ public setHandler( type: E_RESPONSE_HANDLER_TYPE, callback: TPossibleHandlersType ) {
102
+ switch ( type ) {
103
+ case E_RESPONSE_HANDLER_TYPE.ERROR_HANDLER:
104
+ Restful.client.setErrorHandler( callback as TErrorHandlerCallbackType );
105
+ break;
106
+ case E_RESPONSE_HANDLER_TYPE.RESPONSE_FILTER:
107
+ Restful.client.setResponseFilter( callback as TResponseFilterCallbackType );
108
+ break;
109
+ case E_RESPONSE_HANDLER_TYPE.RESPONSE_HANDLER:
110
+ Restful.client.setResponseHandler( callback as TResponseHandlerCallbackType );
111
+ break;
112
+
113
+ default:
114
+ throw new Error( `Unknown handler type: '${ type }'` );
115
+ }
116
+ }
117
+ }
@@ -0,0 +1,86 @@
1
+ import * as util from "util";
2
+
3
+ import { isDebugEnabled } from "@zenflux/utils/environment";
4
+
5
+ import pc from "picocolors";
6
+
7
+ import { Logger } from "./logger";
8
+
9
+ import { ObjectBase } from "../bases/object-base";
10
+
11
+ interface DebuggerOptions {
12
+ skipEventBusHook?: boolean;
13
+ }
14
+
15
+ export class Debugger extends ObjectBase {
16
+ private logger!: Logger;
17
+
18
+ declare private finalizationRegistry;
19
+
20
+ public static getName() {
21
+ return "ZenFlux/Core/Modules/Debugger";
22
+ }
23
+
24
+ public constructor(
25
+ owner: ObjectBase | typeof ObjectBase | string,
26
+ prefix?: string,
27
+ private shouldDebug = Logger.isDebugEnabled(),
28
+ options?: DebuggerOptions
29
+ ) {
30
+ super();
31
+
32
+ if ( shouldDebug ) {
33
+ const loggerOptions = options?.skipEventBusHook ? { skipEventBusHook: true } : undefined;
34
+ this.logger = new Logger( owner, loggerOptions );
35
+
36
+ if ( prefix ) {
37
+ this.logger.addMessagePrefix( prefix );
38
+ }
39
+ } else {
40
+ this.log = () => {};
41
+ this.dumpDown = () => {};
42
+ this.enableCleanupDebug = () => {};
43
+ }
44
+ }
45
+
46
+ public enableCleanupDebug( handle: ObjectBase, id: string = "" ) {
47
+ if ( !this.finalizationRegistry ) {
48
+ this.finalizationRegistry = new FinalizationRegistry( ( id: string ) => {
49
+ this.log( this.constructor, `FinalizationRegistry: ${ id }` );
50
+ } );
51
+ }
52
+
53
+ if ( !id ) {
54
+ id = handle.getName() + ":" + handle.getUniqueId();
55
+ }
56
+
57
+ this.log( this.enableCleanupDebug, `Initialized: '${ id }'` );
58
+
59
+ this.finalizationRegistry.register( handle, id );
60
+ }
61
+
62
+ public log( source: Function, message: string, ...args: any[] ) {
63
+ if ( args && args.length > 0 ) {
64
+ return this.logger.debug( source, message, ...args );
65
+ }
66
+
67
+ this.logger.debug( source, message );
68
+ }
69
+
70
+ public dumpDown( source: Function, object: any, objectName: string = "" ) {
71
+ this.log(
72
+ source,
73
+ `${ objectName ? objectName + ":" : "" } ` + "🔽" + "\n" + pc.green( util.inspect( object, false, null, true ) )
74
+ );
75
+ }
76
+
77
+ public isEnabled() {
78
+ return this.shouldDebug;
79
+ }
80
+ }
81
+
82
+ export function createDebugger( owner: ObjectBase | typeof ObjectBase | string, ownerType: string, prefix?: string ) {
83
+ const ownerName = typeof owner === "string" ? owner : owner.getName();
84
+
85
+ return new Debugger( owner, prefix, isDebugEnabled( ownerType, ownerName ) );
86
+ }