@zenflux/core 0.1.6 → 0.1.8
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/src/bases/controller-base.d.ts +4 -4
- package/dist/src/bases/index.d.ts +2 -2
- package/dist/src/clients/http.d.ts +3 -3
- package/dist/src/command-bases/command-base.d.ts +3 -3
- package/dist/src/command-bases/command-internal.d.ts +1 -1
- package/dist/src/command-bases/command-public.d.ts +1 -1
- package/dist/src/command-bases/command-restful.d.ts +1 -1
- package/dist/src/command-bases/index.d.ts +4 -4
- package/dist/src/errors/command-already-registered.d.ts +1 -1
- package/dist/src/errors/controller-already-registered.d.ts +1 -1
- package/dist/src/errors/controller-already-set.d.ts +1 -1
- package/dist/src/errors/force-method-implementation.d.ts +1 -1
- package/dist/src/errors/index-public.d.ts +1 -1
- package/dist/src/errors/index.d.ts +5 -5
- package/dist/src/exports-index.d.ts +5 -5
- package/dist/src/exports.d.ts +1 -1
- package/dist/src/index.d.ts +2 -2
- package/dist/src/interfaces/index.d.ts +4 -4
- package/dist/src/managers/commands.d.ts +4 -4
- package/dist/src/managers/controllers.d.ts +2 -2
- package/dist/src/managers/export.d.ts +5 -5
- package/dist/src/managers/index.d.ts +4 -4
- package/dist/src/managers/internal.d.ts +1 -1
- package/dist/src/managers/restful.d.ts +5 -5
- package/dist/zenflux-core.cjs +39 -40
- package/dist/zenflux-core.d.ts +448 -3
- package/dist/zenflux-core.mjs +39 -40
- package/package.json +11 -2
- package/src/bases/controller-base.ts +5 -5
- package/src/bases/index.ts +6 -2
- package/src/bases/initialize-base.ts +32 -0
- package/src/bases/object-base.ts +15 -43
- package/src/clients/http.ts +3 -3
- package/src/command-bases/command-base.ts +4 -4
- package/src/command-bases/command-internal.ts +1 -1
- package/src/command-bases/command-public.ts +1 -1
- package/src/command-bases/command-restful.ts +3 -3
- package/src/command-bases/index.ts +5 -4
- package/src/errors/command-already-registered.ts +1 -1
- package/src/errors/controller-already-registered.ts +1 -1
- package/src/errors/controller-already-set.ts +1 -1
- package/src/errors/error-with-metadata.ts +14 -0
- package/src/errors/force-method-implementation.ts +3 -11
- package/src/errors/index-public.ts +1 -1
- package/src/errors/index.ts +9 -5
- package/src/exports-index.ts +5 -5
- package/src/exports.ts +6 -5
- package/src/index.ts +3 -2
- package/src/interfaces/index.ts +5 -4
- package/src/interfaces/logger.ts +3 -2
- package/src/managers/commands.ts +6 -6
- package/src/managers/controllers.ts +3 -3
- package/src/managers/export.ts +5 -5
- package/src/managers/index.ts +5 -4
- package/src/managers/internal.ts +1 -1
- package/src/managers/restful.ts +5 -5
- package/src/modules/debugger.ts +113 -0
- package/src/modules/event-bus/event-bus.ts +204 -0
- package/src/modules/logger/config.ts +44 -0
- package/src/modules/logger/index.ts +82 -0
- package/src/modules/mcp-server/mcp-server.ts +139 -0
- package/src/modules/mcp-server/mcp-service.ts +80 -0
- package/src/modules/service/service-base.ts +55 -0
- package/src/modules/service/service-locator.ts +205 -0
- package/src/modules/service/service-with-dependencies-base.ts +51 -0
package/src/bases/object-base.ts
CHANGED
|
@@ -1,77 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
* The `ObjectBase` class is a foundational class serves as the base for other classes in the system.
|
|
3
|
-
* It is responsible for managing the name and unique identifier of derived classes.
|
|
4
|
-
* This class provides the basic structure for classes that need to have a unique identifier
|
|
5
|
-
* and a common way to retrieve their names.
|
|
6
|
-
*
|
|
7
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
8
|
-
*/
|
|
9
|
-
import { ForceMethodBase } from "@zenflux/core/src/errors/force-method-implementation";
|
|
1
|
+
import { ForceMethodBase } from "../errors";
|
|
10
2
|
|
|
11
3
|
export abstract class ObjectBase {
|
|
12
|
-
private readonly
|
|
4
|
+
private readonly id: string;
|
|
13
5
|
private readonly name: string = "__UNDEFINED_NAME__";
|
|
14
6
|
|
|
15
|
-
/**
|
|
16
|
-
* Initializes the name and unique identifier for the instance.
|
|
17
|
-
*/
|
|
18
7
|
protected constructor() {
|
|
19
|
-
// Initialize the name by calling the abstract method getName()
|
|
20
8
|
this.name = this.getName();
|
|
21
9
|
|
|
22
|
-
// Generate a unique identifier using a combination of timestamp and random numbers
|
|
23
10
|
const timestamp = performance.now() * 1000000;
|
|
24
11
|
const random1 = BigInt( Math.floor( Math.random() * 1000000 ) );
|
|
25
12
|
const random2 = BigInt( Math.floor( Math.random() * 1000000 ) );
|
|
26
13
|
|
|
27
|
-
this.
|
|
14
|
+
this.id = `${ timestamp }${ random1 }${ random2 }`;
|
|
28
15
|
}
|
|
29
16
|
|
|
30
|
-
/**
|
|
31
|
-
* Static method to retrieve the name of the class. This method is abstract and must be implemented
|
|
32
|
-
* by derived classes.
|
|
33
|
-
*/
|
|
34
17
|
public static getName(): string {
|
|
35
|
-
throw new ForceMethodBase(
|
|
18
|
+
throw new ForceMethodBase( this.name, "getName" );
|
|
36
19
|
}
|
|
37
20
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
public getName() {
|
|
21
|
+
public static getSourcePath(): string {
|
|
22
|
+
throw new ForceMethodBase( this.name, "getSourcePath" );
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public getName(): string {
|
|
43
26
|
return ( this.constructor as typeof ObjectBase ).getName();
|
|
44
27
|
}
|
|
45
28
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
*/
|
|
49
|
-
public getUniqueId() {
|
|
50
|
-
return this._id;
|
|
29
|
+
public getUniqueId(): string {
|
|
30
|
+
return this.id;
|
|
51
31
|
}
|
|
52
32
|
|
|
53
|
-
|
|
54
|
-
* Retrieves the initial name that was set during construction. This name may be "__UNDEFINED_NAME__"
|
|
55
|
-
* if the `getName()` method of the derived class does not provide a valid name.
|
|
56
|
-
*/
|
|
57
|
-
public getInitialName() {
|
|
33
|
+
public getInitialName(): string {
|
|
58
34
|
return this.name;
|
|
59
35
|
}
|
|
60
36
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
*/
|
|
64
|
-
public getHierarchyNames() {
|
|
65
|
-
let classNames = [];
|
|
37
|
+
public getHierarchyNames(): string[] {
|
|
38
|
+
const classNames = [];
|
|
66
39
|
let obj = Object.getPrototypeOf( this );
|
|
67
40
|
let className: string;
|
|
68
41
|
|
|
69
|
-
// Traverse the prototype chain and collect class names until the base ObjectBase class is reached.
|
|
70
42
|
while ( ( className = obj.getName() ) !== "Object" ) {
|
|
71
43
|
classNames.push( className );
|
|
72
44
|
obj = Object.getPrototypeOf( obj );
|
|
73
45
|
|
|
74
|
-
// Break the loop if the prototype's constructor is ObjectBase (reached the base class).
|
|
75
46
|
if ( obj.constructor === ObjectBase ) {
|
|
76
47
|
break;
|
|
77
48
|
}
|
|
@@ -81,3 +52,4 @@ export abstract class ObjectBase {
|
|
|
81
52
|
}
|
|
82
53
|
}
|
|
83
54
|
|
|
55
|
+
export default ObjectBase;
|
package/src/clients/http.ts
CHANGED
|
@@ -3,16 +3,16 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
5
5
|
*/
|
|
6
|
-
import { E_HTTP_METHOD_TYPE } from "
|
|
6
|
+
import { E_HTTP_METHOD_TYPE } from "../interfaces";
|
|
7
7
|
|
|
8
|
-
import { ObjectBase } from "
|
|
8
|
+
import { ObjectBase } from "../bases/object-base";
|
|
9
9
|
|
|
10
10
|
import type {
|
|
11
11
|
ILogger,
|
|
12
12
|
TErrorHandlerCallbackType,
|
|
13
13
|
TResponseFilterCallbackType,
|
|
14
14
|
TResponseHandlerCallbackType
|
|
15
|
-
} from "
|
|
15
|
+
} from "../interfaces";
|
|
16
16
|
|
|
17
17
|
// noinspection ExceptionCaughtLocallyJS
|
|
18
18
|
|
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
* TODO
|
|
6
6
|
* - Add validateArgs method or find something better.
|
|
7
7
|
*/
|
|
8
|
-
import { ObjectBase } from "
|
|
9
|
-
import { ControllerAlreadySet, ForceMethodImplementation } from "
|
|
8
|
+
import { ObjectBase } from "../bases";
|
|
9
|
+
import { ControllerAlreadySet, ForceMethodImplementation } from "../errors";
|
|
10
10
|
|
|
11
|
-
import type { ControllerBase } from "
|
|
11
|
+
import type { ControllerBase } from "../bases";
|
|
12
12
|
|
|
13
|
-
import type { ICommandArgsInterface, ICommandOptionsInterface, ILogger } from "
|
|
13
|
+
import type { ICommandArgsInterface, ICommandOptionsInterface, ILogger } from "../interfaces";
|
|
14
14
|
|
|
15
15
|
export abstract class CommandBase extends ObjectBase {
|
|
16
16
|
private static controller: ControllerBase;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
* @description CommandInternal, is used when part of the logic needed to be in the command but not represent a user action.
|
|
4
4
|
*/
|
|
5
|
-
import { CommandBase } from "
|
|
5
|
+
import { CommandBase } from "./command-base";
|
|
6
6
|
|
|
7
7
|
export class CommandInternal extends CommandBase {
|
|
8
8
|
public static getName() {
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
* @description CommandPublic represents a USER action, every class which inherit from this class will USER action.
|
|
4
4
|
* */
|
|
5
|
-
import { CommandBase } from "
|
|
5
|
+
import { CommandBase } from "./command-base";
|
|
6
6
|
|
|
7
7
|
export class CommandPublic extends CommandBase {
|
|
8
8
|
public static getName() {
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
* @description Each rest command should represent a final REST endpoint.
|
|
4
4
|
*/
|
|
5
|
-
import { CommandBase } from "
|
|
5
|
+
import { CommandBase } from "./command-base";
|
|
6
6
|
|
|
7
|
-
import { ForceMethodImplementation } from "
|
|
7
|
+
import { ForceMethodImplementation } from "../errors";
|
|
8
8
|
|
|
9
|
-
import * as managers from "
|
|
9
|
+
import * as managers from "../managers/export";
|
|
10
10
|
|
|
11
11
|
export abstract class CommandRestful extends CommandBase {
|
|
12
12
|
public static getName() {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
*/
|
|
4
|
-
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
4
|
+
|
|
5
|
+
export { CommandPublic } from "./command-public";
|
|
6
|
+
export { CommandRestful } from "./command-restful";
|
|
7
|
+
export { CommandBase } from "./command-base";
|
|
8
|
+
export { CommandInternal } from "./command-internal";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import util from "node:util";
|
|
2
|
+
|
|
3
|
+
export class ErrorWithMetadata extends Error {
|
|
4
|
+
public metadata: any;
|
|
5
|
+
|
|
6
|
+
public constructor( message: string, metadata: any | null = null ) {
|
|
7
|
+
super( message );
|
|
8
|
+
this.metadata = metadata;
|
|
9
|
+
|
|
10
|
+
this.message = null !== metadata ? `${ message } - Metadata: ${ util.inspect( metadata ) }` : message;
|
|
11
|
+
|
|
12
|
+
Object.setPrototypeOf( this, new.target.prototype );
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -1,23 +1,15 @@
|
|
|
1
|
-
import type { ObjectBase } from "
|
|
1
|
+
import type { ObjectBase } from "../bases";
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* @internal
|
|
5
|
-
*/
|
|
6
3
|
export class ForceMethodBase extends Error {
|
|
7
4
|
public constructor( className: string, methodName: string ) {
|
|
8
|
-
super(
|
|
9
|
-
`ForceMethod implementation: at: '${ className }' method: '${ methodName }'`
|
|
10
|
-
);
|
|
5
|
+
super( `ForceMethod implementation: at '${ className }' method: '${ methodName }'` );
|
|
11
6
|
}
|
|
12
7
|
}
|
|
13
8
|
|
|
14
|
-
/**
|
|
15
|
-
* @public
|
|
16
|
-
*/
|
|
17
9
|
export class ForceMethodImplementation extends Error {
|
|
18
10
|
public constructor( context: ObjectBase | typeof ObjectBase | string, methodName: string ) {
|
|
19
11
|
super(
|
|
20
|
-
`ForceMethod implementation: at
|
|
12
|
+
`ForceMethod implementation: at '${ "string" === typeof context ? context : context.getName() }' method: '${ methodName }'`
|
|
21
13
|
);
|
|
22
14
|
}
|
|
23
15
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { ForceMethodImplementation } from "
|
|
1
|
+
export { ForceMethodImplementation } from "./force-method-implementation";
|
package/src/errors/index.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export {
|
|
1
|
+
/**
|
|
2
|
+
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export { CommandAlreadyRegistered } from "./command-already-registered";
|
|
6
|
+
export { CommandNotFound } from "./command-not-found";
|
|
7
|
+
export { ControllerAlreadyRegistered } from "./controller-already-registered";
|
|
8
|
+
export { ControllerAlreadySet } from "./controller-already-set";
|
|
9
|
+
export { ForceMethodImplementation, ForceMethodBase } from "./force-method-implementation";
|
package/src/exports-index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import * as bases from "
|
|
2
|
-
import * as commandBases from "
|
|
3
|
-
import * as errors from "
|
|
4
|
-
import * as interfaces from "
|
|
5
|
-
import * as managers from "
|
|
1
|
+
import * as bases from "./bases";
|
|
2
|
+
import * as commandBases from "./command-bases";
|
|
3
|
+
import * as errors from "./errors/index-public";
|
|
4
|
+
import * as interfaces from "./interfaces";
|
|
5
|
+
import * as managers from "./managers/export";
|
|
6
6
|
|
|
7
7
|
export {
|
|
8
8
|
bases,
|
package/src/exports.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
*/
|
|
4
4
|
// @ts-ignore - TODO handle error in jest
|
|
5
|
-
import { bases, commandBases, errors, interfaces, managers, } from "
|
|
5
|
+
import { bases, commandBases, errors, interfaces, managers, } from "./exports-index";
|
|
6
6
|
|
|
7
|
-
import * as pkg from "
|
|
7
|
+
import * as pkg from "../package.json" assert { type: "json" };
|
|
8
8
|
|
|
9
9
|
declare global {
|
|
10
10
|
var __ZEN_CORE__IS_INITIALIZED__: boolean;
|
|
@@ -15,7 +15,7 @@ export const classes = {
|
|
|
15
15
|
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
|
|
16
16
|
|
|
17
17
|
Logger: class NullLogger implements interfaces.ILogger {
|
|
18
|
-
constructor( owner: typeof bases.ObjectBase, params: any = {} ) {
|
|
18
|
+
constructor( owner: typeof bases.ObjectBase | bases.ObjectBase | string, params: any = {} ) {
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
log( caller: interfaces.TCaller, message: string, ... params: any[] ) {}
|
|
@@ -23,9 +23,10 @@ export const classes = {
|
|
|
23
23
|
error( caller: interfaces.TCaller, message: string, ... params: any[] ) {}
|
|
24
24
|
info( caller: interfaces.TCaller, message: string, ... params: any[] ) {}
|
|
25
25
|
debug( caller: interfaces.TCaller, message: string, ... params: any[] ) {}
|
|
26
|
+
addMessagePrefix( prefix: string ) {}
|
|
26
27
|
startsEmpty( caller: interfaces.TCaller ) {}
|
|
27
|
-
startsWith( caller: interfaces.TCaller, params: object ) {}
|
|
28
|
-
dump( caller: interfaces.TCaller, data: any ) {}
|
|
28
|
+
startsWith( caller: interfaces.TCaller, params: object | string ) {}
|
|
29
|
+
dump( caller: interfaces.TCaller, data: any, notice?: string ) {}
|
|
29
30
|
drop( caller: interfaces.TCaller, according: { [ key: string ]: string }, data: any ) {}
|
|
30
31
|
}
|
|
31
32
|
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
*/
|
|
4
|
-
|
|
4
|
+
|
|
5
|
+
import * as ZenCore from "./exports";
|
|
5
6
|
|
|
6
7
|
// TODO: Only in development mode
|
|
7
8
|
if ( ! globalThis?.zCore ) {
|
|
@@ -12,6 +13,6 @@ declare global {
|
|
|
12
13
|
var zCore: typeof ZenCore;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
export * from "
|
|
16
|
+
export * from "./exports-index";
|
|
16
17
|
|
|
17
18
|
export default ZenCore;
|
package/src/interfaces/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
*/
|
|
4
|
-
|
|
5
|
-
export type * from "
|
|
6
|
-
export type * from "
|
|
7
|
-
export * from "
|
|
4
|
+
|
|
5
|
+
export type * from "./commands";
|
|
6
|
+
export type * from "./config";
|
|
7
|
+
export type * from "./logger";
|
|
8
|
+
export * from "./restful";
|
package/src/interfaces/logger.ts
CHANGED
|
@@ -9,10 +9,11 @@ export interface ILogger {
|
|
|
9
9
|
error( caller: TCaller, message: string, ...params: any[] ): void;
|
|
10
10
|
info( caller: TCaller, message: string, ...params: any[] ): void;
|
|
11
11
|
debug( caller: TCaller, message: string, ...params: any[] ): void;
|
|
12
|
+
addMessagePrefix( prefix: string ): void;
|
|
12
13
|
|
|
13
14
|
startsEmpty( caller: TCaller ): void;
|
|
14
|
-
startsWith( caller: TCaller, params: object ): void;
|
|
15
|
+
startsWith( caller: TCaller, params: object | string ): void;
|
|
15
16
|
|
|
16
|
-
dump( caller: TCaller, params: { [ key: string ]: object|string } ): void;
|
|
17
|
+
dump( caller: TCaller, params: { [ key: string ]: object|string }, notice?: string ): void;
|
|
17
18
|
drop( caller: TCaller, according: { [ key: string ]: string }, data: any ): void;
|
|
18
19
|
}
|
package/src/managers/commands.ts
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
* @description Responsible for manging/running/hooking commands.
|
|
4
4
|
*/
|
|
5
|
-
import { ObjectBase } from "
|
|
5
|
+
import { ObjectBase } from "../bases/object-base";
|
|
6
6
|
|
|
7
|
-
import { CommandNotFound } from "
|
|
8
|
-
import { CommandAlreadyRegistered } from "
|
|
7
|
+
import { CommandNotFound } from "../errors/command-not-found";
|
|
8
|
+
import { CommandAlreadyRegistered } from "../errors/command-already-registered";
|
|
9
9
|
|
|
10
10
|
import type {
|
|
11
11
|
TCommandCallbackType,
|
|
@@ -17,10 +17,10 @@ import type {
|
|
|
17
17
|
ICommandOptionsInterface,
|
|
18
18
|
|
|
19
19
|
ILogger,
|
|
20
|
-
} from "
|
|
20
|
+
} from "../interfaces";
|
|
21
21
|
|
|
22
|
-
import type { ControllerBase } from "
|
|
23
|
-
import type { CommandBase } from "
|
|
22
|
+
import type { ControllerBase } from "../bases/controller-base";
|
|
23
|
+
import type { CommandBase } from "../command-bases/command-base";
|
|
24
24
|
|
|
25
25
|
export class Commands extends ObjectBase {
|
|
26
26
|
public static readonly trace:string[] = [];
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
* @description Responsible for manging controllers, each controller is global singleton instance.
|
|
4
4
|
*/
|
|
5
|
-
import { ControllerAlreadyRegistered } from "
|
|
5
|
+
import { ControllerAlreadyRegistered } from "../errors/controller-already-registered";
|
|
6
6
|
|
|
7
|
-
import { ObjectBase } from "
|
|
7
|
+
import { ObjectBase } from "../bases/object-base";
|
|
8
8
|
|
|
9
|
-
import type { ControllerBase } from "
|
|
9
|
+
import type { ControllerBase } from "../bases/controller-base";
|
|
10
10
|
|
|
11
11
|
export class Controllers extends ObjectBase {
|
|
12
12
|
private controllers: { [ key: string ]: ControllerBase } = {};
|
package/src/managers/export.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
*/
|
|
4
|
-
import { Commands } from "
|
|
5
|
-
import { Controllers } from "
|
|
6
|
-
import { Restful } from "
|
|
7
|
-
import { Internal } from "
|
|
4
|
+
import { Commands } from "./commands";
|
|
5
|
+
import { Controllers } from "./controllers";
|
|
6
|
+
import { Restful } from "./restful";
|
|
7
|
+
import { Internal } from "./internal";
|
|
8
8
|
|
|
9
|
-
import type { IAPIConfig } from "
|
|
9
|
+
import type { IAPIConfig } from "../interfaces";
|
|
10
10
|
|
|
11
11
|
export const afterInitializeCallbacks: ( () => void )[] = [];
|
|
12
12
|
|
package/src/managers/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
*/
|
|
4
|
-
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
4
|
+
|
|
5
|
+
export { Commands } from "./commands";
|
|
6
|
+
export { Controllers } from "./controllers";
|
|
7
|
+
export { Restful } from "./restful";
|
|
8
|
+
export { Internal } from "./internal";
|
package/src/managers/internal.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
* @description Responsible for manging internal commands, To serve commands that are not triggered by user.
|
|
4
4
|
*/
|
|
5
|
-
import { Commands } from "
|
|
5
|
+
import { Commands } from "./commands";
|
|
6
6
|
|
|
7
7
|
export class Internal extends Commands {
|
|
8
8
|
public static getName() {
|
package/src/managers/restful.ts
CHANGED
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
* @description Responsible for manging restful commands which are wrappers for HTTP requests.
|
|
4
4
|
*/
|
|
5
|
-
import { Commands } from "
|
|
5
|
+
import { Commands } from "./commands";
|
|
6
6
|
|
|
7
|
-
import { Http } from "
|
|
7
|
+
import { Http } from "../clients/http";
|
|
8
8
|
|
|
9
9
|
import {
|
|
10
10
|
E_RESPONSE_HANDLER_TYPE,
|
|
11
11
|
E_HTTP_METHOD_TYPE
|
|
12
|
-
} from "
|
|
12
|
+
} from "../interfaces";
|
|
13
13
|
|
|
14
|
-
import type { CommandRestful } from "
|
|
14
|
+
import type { CommandRestful } from "../command-bases";
|
|
15
15
|
|
|
16
16
|
import type {
|
|
17
17
|
IAPIConfig,
|
|
@@ -22,7 +22,7 @@ import type {
|
|
|
22
22
|
TPossibleHandlersType,
|
|
23
23
|
TResponseFilterCallbackType,
|
|
24
24
|
TResponseHandlerCallbackType
|
|
25
|
-
} from "
|
|
25
|
+
} from "../interfaces";
|
|
26
26
|
|
|
27
27
|
export class Restful extends Commands {
|
|
28
28
|
private static client: Http;
|
|
@@ -0,0 +1,113 @@
|
|
|
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
|
+
import type { PermissionOverwriteManager, PermissionOverwrites } from "discord.js";
|
|
12
|
+
|
|
13
|
+
// Define options interface for Debugger
|
|
14
|
+
interface DebuggerOptions {
|
|
15
|
+
skipEventBusHook?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class Debugger extends ObjectBase {
|
|
19
|
+
private logger!: Logger;
|
|
20
|
+
|
|
21
|
+
declare private finalizationRegistry;
|
|
22
|
+
|
|
23
|
+
public static getName() {
|
|
24
|
+
return "ZenFlux/Core/Modules/Debugger";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public constructor(
|
|
28
|
+
owner: ObjectBase | typeof ObjectBase | string,
|
|
29
|
+
prefix?: string,
|
|
30
|
+
private shouldDebug = Logger.isDebugEnabled(),
|
|
31
|
+
options?: DebuggerOptions // Add options parameter
|
|
32
|
+
) {
|
|
33
|
+
super();
|
|
34
|
+
|
|
35
|
+
if ( shouldDebug ) {
|
|
36
|
+
// Pass logger options down if skipEventBusHook is set
|
|
37
|
+
const loggerOptions = options?.skipEventBusHook ? { skipEventBusHook: true } : undefined;
|
|
38
|
+
this.logger = new Logger( owner, loggerOptions );
|
|
39
|
+
|
|
40
|
+
if ( prefix ) {
|
|
41
|
+
this.logger.addMessagePrefix( prefix );
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
// Bypass all the methods
|
|
45
|
+
this.log = () => {};
|
|
46
|
+
this.dumpDown = () => {};
|
|
47
|
+
this.debugPermission = () => {};
|
|
48
|
+
this.debugPermissions = () => {};
|
|
49
|
+
this.enableCleanupDebug = () => {};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public enableCleanupDebug( handle: ObjectBase, id: string = "" ) {
|
|
54
|
+
if ( !this.finalizationRegistry ) {
|
|
55
|
+
this.finalizationRegistry = new FinalizationRegistry( ( id: string ) => {
|
|
56
|
+
this.log( this.constructor, `FinalizationRegistry: ${ id }` );
|
|
57
|
+
} );
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if ( !id ) {
|
|
61
|
+
id = handle.getName() + ":" + handle.getUniqueId();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this.log( this.enableCleanupDebug, `Initialized: '${ id }'` );
|
|
65
|
+
|
|
66
|
+
this.finalizationRegistry.register( handle, id );
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public log( source: Function, message: string, ...args: any[] ) {
|
|
70
|
+
if ( args && args.length > 0 ) {
|
|
71
|
+
return this.logger.debug( source, message, ...args );
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
this.logger.debug( source, message );
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public dumpDown( source: Function, object: any, objectName: string = "" ) {
|
|
78
|
+
this.log(
|
|
79
|
+
source,
|
|
80
|
+
`${ objectName ? objectName + ":" : "" } ` + "🔽" + "\n" + pc.green( util.inspect( object, false, null, true ) )
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public debugPermission( source: Function, overwrite: PermissionOverwrites ) {
|
|
85
|
+
let { id, allow, deny, type } = overwrite;
|
|
86
|
+
|
|
87
|
+
this.log(
|
|
88
|
+
source,
|
|
89
|
+
JSON.stringify( {
|
|
90
|
+
id,
|
|
91
|
+
allow: allow.toArray(),
|
|
92
|
+
deny: deny.toArray(),
|
|
93
|
+
type
|
|
94
|
+
} )
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public debugPermissions( source: Function, permissionOverwrites: PermissionOverwriteManager ) {
|
|
99
|
+
for ( const overwrite of permissionOverwrites.cache.values() ) {
|
|
100
|
+
this.debugPermission( source, overwrite );
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public isEnabled() {
|
|
105
|
+
return this.shouldDebug;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function createDebugger( owner: ObjectBase | typeof ObjectBase | string, ownerType: string, prefix?: string ) {
|
|
110
|
+
const ownerName = typeof owner === "string" ? owner : owner.getName();
|
|
111
|
+
|
|
112
|
+
return new Debugger( owner, prefix, isDebugEnabled( ownerType, ownerName ) );
|
|
113
|
+
}
|