@zenflux/core 0.0.0-alpha.0 → 0.0.0-alpha.10
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/es/{src/clients → clients}/http.d.ts +9 -1
- package/dist/es/clients/http.d.ts.map +1 -0
- package/dist/es/{src/command-bases → command-bases}/command-base.d.ts +5 -0
- package/dist/es/command-bases/command-base.d.ts.map +1 -0
- package/dist/es/{src/command-bases → command-bases}/command-data.d.ts +1 -0
- package/dist/es/command-bases/command-data.d.ts.map +1 -0
- package/dist/es/{src/command-bases → command-bases}/command-internal.d.ts +1 -0
- package/dist/es/command-bases/command-internal.d.ts.map +1 -0
- package/dist/es/{src/command-bases → command-bases}/command-public.d.ts +1 -0
- package/dist/es/command-bases/command-public.d.ts.map +1 -0
- package/dist/es/{src/command-bases → command-bases}/index.d.ts +1 -0
- package/dist/es/command-bases/index.d.ts.map +1 -0
- package/dist/es/{src/core → core}/controller.d.ts +1 -0
- package/dist/es/core/controller.d.ts.map +1 -0
- package/dist/es/{src/core → core}/index.d.ts +1 -0
- package/dist/es/core/index.d.ts.map +1 -0
- package/dist/es/{src/core → core}/object-base.d.ts +1 -0
- package/dist/es/core/object-base.d.ts.map +1 -0
- package/dist/es/{src/enums → enums}/http.d.ts +1 -0
- package/dist/es/enums/http.d.ts.map +1 -0
- package/dist/es/{src/errors → errors}/command-already-registered.d.ts +1 -0
- package/dist/es/errors/command-already-registered.d.ts.map +1 -0
- package/dist/es/{src/errors → errors}/command-not-found.d.ts +1 -0
- package/dist/es/errors/command-not-found.d.ts.map +1 -0
- package/dist/es/{src/errors → errors}/controller-already-registered.d.ts +1 -0
- package/dist/es/errors/controller-already-registered.d.ts.map +1 -0
- package/dist/es/{src/errors → errors}/force-method.d.ts +1 -0
- package/dist/es/errors/force-method.d.ts.map +1 -0
- package/dist/es/{src/errors → errors}/index.d.ts +1 -0
- package/dist/es/errors/index.d.ts.map +1 -0
- package/dist/es/{src/exports.d.ts → exports.d.ts} +1 -0
- package/dist/es/exports.d.ts.map +1 -0
- package/dist/es/{src/index.d.ts → index.d.ts} +1 -0
- package/dist/es/index.d.ts.map +1 -0
- package/dist/es/{src/initializer.d.ts → initializer.d.ts} +2 -1
- package/dist/es/initializer.d.ts.map +1 -0
- package/dist/es/{src/interfaces → interfaces}/commands.d.ts +1 -0
- package/dist/es/interfaces/commands.d.ts.map +1 -0
- package/dist/es/{src/interfaces → interfaces}/config.d.ts +1 -0
- package/dist/es/interfaces/config.d.ts.map +1 -0
- package/dist/es/interfaces/data.d.ts +3 -0
- package/dist/es/interfaces/data.d.ts.map +1 -0
- package/dist/es/{src/interfaces → interfaces}/managers.d.ts +1 -0
- package/dist/es/interfaces/managers.d.ts.map +1 -0
- package/dist/es/{src/interfaces → interfaces}/object-base.d.ts +1 -0
- package/dist/es/interfaces/object-base.d.ts.map +1 -0
- package/dist/es/{src/manager-bases → manager-bases}/commands.d.ts +1 -0
- package/dist/es/manager-bases/commands.d.ts.map +1 -0
- package/dist/es/{src/manager-bases → manager-bases}/controllers.d.ts +1 -0
- package/dist/es/manager-bases/controllers.d.ts.map +1 -0
- package/dist/es/{src/manager-bases → manager-bases}/data.d.ts +10 -0
- package/dist/es/manager-bases/data.d.ts.map +1 -0
- package/dist/es/{src/manager-bases → manager-bases}/index.d.ts +1 -0
- package/dist/es/manager-bases/index.d.ts.map +1 -0
- package/dist/es/{src/manager-bases → manager-bases}/internal.d.ts +1 -0
- package/dist/es/manager-bases/internal.d.ts.map +1 -0
- package/dist/es/{src/managers → managers}/index.d.ts +1 -1
- package/dist/es/managers/index.d.ts.map +1 -0
- package/dist/es/{src/modules → modules}/index.d.ts +1 -0
- package/dist/es/modules/index.d.ts.map +1 -0
- package/dist/es/{src/modules → modules}/logger.d.ts +1 -0
- package/dist/es/modules/logger.d.ts.map +1 -0
- package/dist/es/{src/utils → utils}/index.d.ts +1 -0
- package/dist/es/utils/index.d.ts.map +1 -0
- package/dist/es/{src/utils → utils}/object.d.ts +1 -0
- package/dist/es/utils/object.d.ts.map +1 -0
- package/dist/es/{src/utils → utils}/rgb.d.ts +1 -0
- package/dist/es/utils/rgb.d.ts.map +1 -0
- package/dist/es/zenflux-core.js +1930 -855
- package/package.json +23 -30
- package/src/clients/http.ts +50 -7
- package/src/command-bases/command-base.ts +18 -7
- package/src/core/controller.ts +1 -1
- package/src/initializer.ts +9 -12
- package/src/interfaces/data.ts +4 -0
- package/src/manager-bases/data.ts +15 -0
- package/src/managers/index.ts +0 -12
- package/src/modules/logger.ts +53 -7
- package/types/index.d.ts +4 -3
- package/dist/cjs/zenflux-core.js +0 -1123
- package/dist/es/rollup.config.d.ts +0 -23
- package/dist/es/src/globals.d.ts +0 -3
- package/dist/es/zenflux-core.mjs +0 -12
- package/dist/umd/zenflux-core.js +0 -12
package/package.json
CHANGED
|
@@ -1,56 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenflux/core",
|
|
3
|
-
"version": "0.0.0-alpha.
|
|
3
|
+
"version": "0.0.0-alpha.10",
|
|
4
4
|
"description": "ZenFlux core",
|
|
5
|
-
"author": "Leonid Vinikov <leonidvinikov@gmail.com> (https://github.com/iNewLegend)",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "export flow_modules_logger=off && jest",
|
|
8
|
-
"build": "rollup --config rollup.config.ts --configPlugin typescript",
|
|
9
|
-
"watch": "rollup --config rollup.config.ts --configPlugin typescript --watch"
|
|
10
|
-
},
|
|
11
|
-
"repository": {
|
|
12
|
-
"type": "git",
|
|
13
|
-
"url": "git+https://github.com/zenflux/core.git"
|
|
14
|
-
},
|
|
15
5
|
"keywords": [
|
|
16
6
|
"zenflux",
|
|
17
7
|
"zen",
|
|
18
8
|
"flux",
|
|
19
9
|
"commands",
|
|
20
10
|
"manager",
|
|
21
|
-
"commands manager"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"dist",
|
|
25
|
-
"src",
|
|
26
|
-
"types"
|
|
11
|
+
"commands manager",
|
|
12
|
+
"core",
|
|
13
|
+
"zenflux-core"
|
|
27
14
|
],
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/zenflux/core.git"
|
|
18
|
+
},
|
|
28
19
|
"license": "MIT",
|
|
20
|
+
"author": "Leonid Vinikov <leonidvinikov@gmail.com> (https://github.com/iNewLegend)",
|
|
29
21
|
"main": "dist/cjs/zenflux-core.js",
|
|
30
22
|
"unpkg": "dist/umd/zenflux-core.js",
|
|
31
23
|
"module": "dist/es/zenflux-core.js",
|
|
32
24
|
"types": "types/index.d.ts",
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"src",
|
|
28
|
+
"types"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"test": "export flow_modules_logger=off && jest",
|
|
32
|
+
"toolkit-build": "node_modules/@zenflux/rollup-toolkit/bin/run @build",
|
|
33
|
+
"toolkit-build-dev": "node_modules/@zenflux/rollup-toolkit/bin/dev @build",
|
|
34
|
+
"toolkit-watch": "node_modules/@zenflux/rollup-toolkit/bin/run @watch",
|
|
35
|
+
"toolkit-watch-dev": "node_modules/@zenflux/rollup-toolkit/bin/dev @watch"
|
|
36
|
+
},
|
|
33
37
|
"devDependencies": {
|
|
34
|
-
"@babel/
|
|
35
|
-
"@babel/plugin-transform-runtime": "^7.19.6",
|
|
38
|
+
"@babel/plugin-syntax-import-assertions": "^7.20.0",
|
|
36
39
|
"@babel/preset-env": "^7.20.2",
|
|
37
40
|
"@babel/preset-typescript": "^7.18.6",
|
|
38
|
-
"@babel/runtime": "^7.20.7",
|
|
39
|
-
"@rollup/plugin-babel": "^6.0.3",
|
|
40
|
-
"@rollup/plugin-json": "^6.0.0",
|
|
41
|
-
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
42
|
-
"@rollup/plugin-replace": "^5.0.2",
|
|
43
|
-
"@rollup/plugin-terser": "^0.2.1",
|
|
44
|
-
"@rollup/plugin-typescript": "^10.0.1",
|
|
45
41
|
"@types/jest": "^29.2.5",
|
|
46
42
|
"@typescript-eslint/eslint-plugin": "^5.48.0",
|
|
47
43
|
"@typescript-eslint/parser": "^5.48.0",
|
|
44
|
+
"@zenflux/rollup-toolkit": "^0.0.0-alpha.14",
|
|
48
45
|
"babel-jest": "^29.3.1",
|
|
49
|
-
"rollup": "^3.9.1",
|
|
50
46
|
"ts-jest": "^29.0.3",
|
|
51
47
|
"tslib": "^2.4.1"
|
|
52
|
-
},
|
|
53
|
-
"dependencies": {
|
|
54
|
-
"@babel/runtime": "^7.19.0"
|
|
55
48
|
}
|
|
56
49
|
}
|
package/src/clients/http.ts
CHANGED
|
@@ -8,11 +8,21 @@ import Logger from '../modules/logger';
|
|
|
8
8
|
|
|
9
9
|
import { HTTPMethodEnum } from "../enums/http";
|
|
10
10
|
|
|
11
|
+
import {
|
|
12
|
+
TErrorHandlerCallback,
|
|
13
|
+
TResponseHandlerCallback,
|
|
14
|
+
} from "../interfaces/data";
|
|
15
|
+
|
|
16
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
17
|
+
|
|
11
18
|
export class Http extends ObjectBase {
|
|
12
19
|
private readonly logger: Logger;
|
|
13
20
|
|
|
14
21
|
private readonly apiBaseUrl: string;
|
|
15
22
|
|
|
23
|
+
private errorHandler?: TErrorHandlerCallback = undefined;
|
|
24
|
+
private responseHandler?: TResponseHandlerCallback = undefined;
|
|
25
|
+
|
|
16
26
|
static getName() {
|
|
17
27
|
return 'Core/Clients/Http';
|
|
18
28
|
}
|
|
@@ -30,10 +40,26 @@ export class Http extends ObjectBase {
|
|
|
30
40
|
this.apiBaseUrl = apiBaseUrl + '/';
|
|
31
41
|
}
|
|
32
42
|
|
|
43
|
+
public setResponseHandler( callback: TResponseHandlerCallback ) {
|
|
44
|
+
if ( this.responseHandler ) {
|
|
45
|
+
throw new Error( 'Data handler already set.' );
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
this.responseHandler = callback;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public setErrorHandler( callback: TErrorHandlerCallback ) {
|
|
52
|
+
if ( this.errorHandler ) {
|
|
53
|
+
throw new Error( 'Error handler already set.' );
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
this.errorHandler = callback;
|
|
57
|
+
}
|
|
58
|
+
|
|
33
59
|
/**
|
|
34
60
|
* Function fetch() : Fetch api.
|
|
35
61
|
*/
|
|
36
|
-
async fetch( path: string, method: HTTPMethodEnum, body:
|
|
62
|
+
async fetch( path: string, method: HTTPMethodEnum, body: any = {} ) {
|
|
37
63
|
this.logger.startWith( { path, method, body } );
|
|
38
64
|
|
|
39
65
|
const params: RequestInit = { 'credentials': 'include' }, // Support cookies.
|
|
@@ -60,22 +86,39 @@ export class Http extends ObjectBase {
|
|
|
60
86
|
let data = undefined;
|
|
61
87
|
|
|
62
88
|
try {
|
|
63
|
-
|
|
89
|
+
let responseText = await response.text();
|
|
90
|
+
|
|
91
|
+
if ( ! response.ok ) {
|
|
92
|
+
throw response;
|
|
93
|
+
} else if ( this.applyResponseHandler( responseText ) ) {
|
|
94
|
+
return false;
|
|
95
|
+
} else if ( response.headers.get( 'Content-Type' )?.includes( 'application/json' ) ) {
|
|
96
|
+
data = JSON.parse( responseText );
|
|
97
|
+
} else {
|
|
98
|
+
throw response;
|
|
99
|
+
}
|
|
64
100
|
} catch ( e ) {
|
|
101
|
+
if ( this.applyErrorHandler( e ) ) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
|
|
65
105
|
console.error( e );
|
|
66
106
|
|
|
67
107
|
return false;
|
|
68
108
|
}
|
|
69
109
|
|
|
70
|
-
// TODO: This is part should be modular and not hard coded.
|
|
71
|
-
if ( data?.error && data?.global && data?.message ) {
|
|
72
|
-
throw new Error( data.message );
|
|
73
|
-
}
|
|
74
|
-
|
|
75
110
|
this.logger.drop( { path }, data );
|
|
76
111
|
|
|
77
112
|
return data;
|
|
78
113
|
}
|
|
114
|
+
|
|
115
|
+
private applyErrorHandler( error: any ) {
|
|
116
|
+
return this.errorHandler && this.errorHandler( error );
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private applyResponseHandler( text: string ) {
|
|
120
|
+
return this.responseHandler && this.responseHandler( text );
|
|
121
|
+
}
|
|
79
122
|
}
|
|
80
123
|
|
|
81
124
|
export default Http;
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* - Add validateArgs method or find something better.
|
|
6
6
|
*/
|
|
7
7
|
import ObjectBase from "../core/object-base";
|
|
8
|
+
import Controller from "../core/controller";
|
|
8
9
|
|
|
9
10
|
import ForceMethod from "../errors/force-method";
|
|
10
11
|
|
|
@@ -18,6 +19,8 @@ export abstract class CommandBase extends ObjectBase {
|
|
|
18
19
|
|
|
19
20
|
private readonly logger: Logger;
|
|
20
21
|
|
|
22
|
+
private controller: Controller;
|
|
23
|
+
|
|
21
24
|
constructor( args: ICommandArgsInterface = {}, options = {} ) {
|
|
22
25
|
super();
|
|
23
26
|
|
|
@@ -32,16 +35,16 @@ export abstract class CommandBase extends ObjectBase {
|
|
|
32
35
|
this.initialize( args, options );
|
|
33
36
|
}
|
|
34
37
|
|
|
35
|
-
initialize( args: ICommandArgsInterface, options: ICommandOptionsInterface ) {
|
|
38
|
+
public initialize( args: ICommandArgsInterface, options: ICommandOptionsInterface ) {
|
|
36
39
|
this.args = args;
|
|
37
40
|
this.options = options;
|
|
38
41
|
}
|
|
39
42
|
|
|
40
|
-
apply( args = this.args, options = this.options ): any {// eslint-disable-line @typescript-eslint/no-unused-vars
|
|
43
|
+
public apply( args = this.args, options = this.options ): any {// eslint-disable-line @typescript-eslint/no-unused-vars
|
|
41
44
|
throw new ForceMethod( this, "apply" );
|
|
42
45
|
}
|
|
43
46
|
|
|
44
|
-
async run() {
|
|
47
|
+
public async run() {
|
|
45
48
|
this.onBeforeApply && this.onBeforeApply();
|
|
46
49
|
|
|
47
50
|
const result = await this.apply( this.args, this.options );
|
|
@@ -51,17 +54,25 @@ export abstract class CommandBase extends ObjectBase {
|
|
|
51
54
|
return result;
|
|
52
55
|
}
|
|
53
56
|
|
|
54
|
-
getArgs() {
|
|
57
|
+
public getArgs() {
|
|
55
58
|
return this.args;
|
|
56
59
|
}
|
|
57
60
|
|
|
58
|
-
getOptions() {
|
|
61
|
+
public getOptions() {
|
|
59
62
|
return this.options;
|
|
60
63
|
}
|
|
61
64
|
|
|
62
|
-
|
|
65
|
+
public setController( controller: Controller ) {
|
|
66
|
+
this.controller = controller;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public getController() {
|
|
70
|
+
return this.controller;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public onBeforeApply?():void;
|
|
63
74
|
|
|
64
|
-
onAfterApply?():void;
|
|
75
|
+
public onAfterApply?():void;
|
|
65
76
|
}
|
|
66
77
|
|
|
67
78
|
export default CommandBase;
|
package/src/core/controller.ts
CHANGED
|
@@ -6,7 +6,7 @@ import ObjectBase from "./object-base";
|
|
|
6
6
|
|
|
7
7
|
import { CommandData, CommandInternal, CommandPublic } from "../command-bases";
|
|
8
8
|
|
|
9
|
-
import * as managers from "../managers";
|
|
9
|
+
import * as managers from "../managers/";
|
|
10
10
|
|
|
11
11
|
export abstract class Controller extends ObjectBase {
|
|
12
12
|
commands: { [ key: string ]: typeof CommandPublic };
|
package/src/initializer.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import './globals'
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import * as pkg from "../package.json" assert { type: "json" };
|
|
4
3
|
|
|
5
4
|
import { IAPIConfig } from "./interfaces/config";
|
|
6
5
|
|
|
@@ -10,12 +9,11 @@ import * as exported from './exports';
|
|
|
10
9
|
|
|
11
10
|
declare global {
|
|
12
11
|
var ZenCore: any;
|
|
12
|
+
var __ZEN_CORE__IS_INITIALIZED__: boolean;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
let isInitialized = false;
|
|
16
|
-
|
|
17
15
|
function errorInitTwice() {
|
|
18
|
-
if (
|
|
16
|
+
if ( 'undefined' !== typeof __ZEN_CORE__IS_INITIALIZED__ && __ZEN_CORE__IS_INITIALIZED__) {
|
|
19
17
|
throw new Error( 'ZenCore is already initialized.' );
|
|
20
18
|
}
|
|
21
19
|
}
|
|
@@ -24,8 +22,7 @@ errorInitTwice();
|
|
|
24
22
|
|
|
25
23
|
export let config: IAPIConfig = {
|
|
26
24
|
version: pkg.version,
|
|
27
|
-
}
|
|
28
|
-
|
|
25
|
+
};
|
|
29
26
|
|
|
30
27
|
export const CoreAPI = {
|
|
31
28
|
initialize: ( configuration?: IAPIConfig ) => {
|
|
@@ -33,16 +30,16 @@ export const CoreAPI = {
|
|
|
33
30
|
|
|
34
31
|
config = configuration || config;
|
|
35
32
|
|
|
36
|
-
initialize( config )
|
|
33
|
+
initialize( config );
|
|
37
34
|
|
|
38
|
-
|
|
35
|
+
globalThis.__ZEN_CORE__IS_INITIALIZED__ = true;
|
|
39
36
|
|
|
40
37
|
},
|
|
41
38
|
|
|
42
39
|
destroy: () => {
|
|
43
40
|
destroy();
|
|
44
41
|
|
|
45
|
-
|
|
42
|
+
globalThis.__ZEN_CORE__IS_INITIALIZED__ = false;
|
|
46
43
|
},
|
|
47
44
|
|
|
48
45
|
onAfterInitialize: ( callback: () => void ) => {
|
|
@@ -52,7 +49,7 @@ export const CoreAPI = {
|
|
|
52
49
|
config,
|
|
53
50
|
|
|
54
51
|
...exported,
|
|
55
|
-
}
|
|
52
|
+
};
|
|
56
53
|
|
|
57
54
|
// TODO: Make it available only for development.
|
|
58
55
|
if ( ! globalThis?.ZenCore ) globalThis.ZenCore = CoreAPI;
|
|
@@ -12,6 +12,7 @@ import { HTTPMethodEnum } from "../enums/http";
|
|
|
12
12
|
|
|
13
13
|
import { IAPIConfig } from "../interfaces/config";
|
|
14
14
|
import { ICommandArgsInterface, ICommandOptionsInterface } from '../interfaces/commands';
|
|
15
|
+
import { TResponseHandlerCallback, TErrorHandlerCallback } from "../interfaces/data";
|
|
15
16
|
|
|
16
17
|
export class Data extends Commands {
|
|
17
18
|
private static client: Http;
|
|
@@ -84,6 +85,20 @@ export class Data extends Commands {
|
|
|
84
85
|
|
|
85
86
|
return args.result;
|
|
86
87
|
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* On return true, the request will be swallowed.
|
|
91
|
+
*/
|
|
92
|
+
public setErrorHandler( callback: TErrorHandlerCallback ) {
|
|
93
|
+
this.getClient().setErrorHandler( callback );
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* On return true, the request will be swallowed.
|
|
98
|
+
*/
|
|
99
|
+
public setResponseHandler( callback: TResponseHandlerCallback ) {
|
|
100
|
+
this.getClient().setResponseHandler( callback );
|
|
101
|
+
}
|
|
87
102
|
}
|
|
88
103
|
|
|
89
104
|
export default Data;
|
package/src/managers/index.ts
CHANGED
|
@@ -8,8 +8,6 @@ import Internal from "../manager-bases/internal";
|
|
|
8
8
|
|
|
9
9
|
import { IAPIConfig } from "../interfaces/config";
|
|
10
10
|
|
|
11
|
-
import '../globals';
|
|
12
|
-
|
|
13
11
|
export const afterInitializeCallbacks: ( () => void )[] = [];
|
|
14
12
|
|
|
15
13
|
export function initialize( config: IAPIConfig ) {
|
|
@@ -30,13 +28,3 @@ export var commands = {} as Commands;
|
|
|
30
28
|
export var controllers = {} as Controllers;
|
|
31
29
|
export var data = {} as Data;
|
|
32
30
|
export var internal = {} as Internal;
|
|
33
|
-
|
|
34
|
-
/*
|
|
35
|
-
__ZEN_MANAGERS__.initialize = initialize;
|
|
36
|
-
__ZEN_MANAGERS__.destroy = destroy;
|
|
37
|
-
|
|
38
|
-
export var commands = __ZEN_MANAGERS__.commands;
|
|
39
|
-
export var controllers = __ZEN_MANAGERS__.controllers;
|
|
40
|
-
export var data = __ZEN_MANAGERS__.data;
|
|
41
|
-
export var internal = __ZEN_MANAGERS__.internal;
|
|
42
|
-
*/
|
package/src/modules/logger.ts
CHANGED
|
@@ -8,7 +8,20 @@
|
|
|
8
8
|
import { hexColorDelta, getCircularReplacer } from "../utils";
|
|
9
9
|
|
|
10
10
|
// TODO: Should by dynamic/configure-able.
|
|
11
|
-
const MAX_WRAPPING_RECURSIVE_DEPTH = 4
|
|
11
|
+
const MAX_WRAPPING_RECURSIVE_DEPTH = 4,
|
|
12
|
+
STACK_BACKWARD_CALLER_INDEX = 3,
|
|
13
|
+
UNKNOWN_CALLER_NAME = 'anonymous function',
|
|
14
|
+
BABEL_EXCLUDE_FUNCTIONS_NAMES = [
|
|
15
|
+
'_callee',
|
|
16
|
+
'tryCatch',
|
|
17
|
+
'<anonymous>',
|
|
18
|
+
'Generator.next',
|
|
19
|
+
'asyncGeneratorStep',
|
|
20
|
+
'_next',
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
// TODO: Should by dynamic/configure-able.
|
|
24
|
+
Error.stackTraceLimit = 50;
|
|
12
25
|
|
|
13
26
|
export class Logger {
|
|
14
27
|
static wrappers = {};
|
|
@@ -53,7 +66,6 @@ export class Logger {
|
|
|
53
66
|
}
|
|
54
67
|
// @ts-ignore
|
|
55
68
|
Logger.wrappers[ classType.name ].push( { classType, callback } );
|
|
56
|
-
|
|
57
69
|
}
|
|
58
70
|
|
|
59
71
|
/**
|
|
@@ -61,20 +73,54 @@ export class Logger {
|
|
|
61
73
|
*/
|
|
62
74
|
private static getCallerName() {
|
|
63
75
|
const error = new Error(),
|
|
64
|
-
|
|
76
|
+
errorsPerLine = error.stack?.split( "\n" ) || [],
|
|
77
|
+
caller = errorsPerLine.length ? errorsPerLine[ STACK_BACKWARD_CALLER_INDEX ].trim() : UNKNOWN_CALLER_NAME;
|
|
65
78
|
|
|
66
79
|
if ( caller.startsWith( "at new" ) ) {
|
|
67
80
|
return "constructor";
|
|
68
81
|
}
|
|
69
82
|
|
|
70
|
-
|
|
83
|
+
let extractName = caller.split( "." )[ 1 ].split( " " )[ 0 ],
|
|
84
|
+
isBabelAsyncRuntime = false;
|
|
85
|
+
|
|
86
|
+
// Babel compatibility.
|
|
87
|
+
if ( extractName.includes( '_callee' ) ) {
|
|
88
|
+
|
|
89
|
+
// Run till find reliable function name.
|
|
90
|
+
for ( let i = STACK_BACKWARD_CALLER_INDEX; i < errorsPerLine.length; i++ ) {
|
|
91
|
+
const line = errorsPerLine[ i ];
|
|
92
|
+
|
|
93
|
+
const isExcluded = BABEL_EXCLUDE_FUNCTIONS_NAMES.some( ( excludeName ) => {
|
|
94
|
+
return line.includes( excludeName );
|
|
95
|
+
} );
|
|
96
|
+
|
|
97
|
+
if ( ! isExcluded ) {
|
|
98
|
+
const format = line.trim().split(' ');
|
|
99
|
+
|
|
100
|
+
if ( format.length <= 2 ) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
extractName = format[ 1 ].split( "." )[ 1 ];
|
|
105
|
+
isBabelAsyncRuntime = true;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if ( ! isBabelAsyncRuntime ) {
|
|
111
|
+
extractName = UNKNOWN_CALLER_NAME;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return extractName;
|
|
71
117
|
}
|
|
72
118
|
|
|
73
119
|
/**
|
|
74
120
|
* Function functionView() : Return function preview.
|
|
75
121
|
*/
|
|
76
122
|
private static getFunctionView( fn: string | Function ): ( string | Function ) {
|
|
77
|
-
let fReturn =
|
|
123
|
+
let fReturn = UNKNOWN_CALLER_NAME;
|
|
78
124
|
|
|
79
125
|
// TODO: Check if works.
|
|
80
126
|
if ( typeof fn !== "string" && fn.name.length !== 0 ) {
|
|
@@ -157,7 +203,7 @@ export class Logger {
|
|
|
157
203
|
Logger.wrapperDepth++;
|
|
158
204
|
|
|
159
205
|
// Result will lose classType (instanceOf will not work), now based on obj.
|
|
160
|
-
let result = { ...
|
|
206
|
+
let result = { ...obj };
|
|
161
207
|
|
|
162
208
|
// @ts-ignore
|
|
163
209
|
Object.values( Logger.wrappers ).forEach( ( [ wrapper ] ) => {
|
|
@@ -405,7 +451,7 @@ export class Logger {
|
|
|
405
451
|
/**
|
|
406
452
|
* Function out() : Print console log with style
|
|
407
453
|
*/
|
|
408
|
-
private out( ...
|
|
454
|
+
private out( ...args: any ) {
|
|
409
455
|
this.outputHandler.apply( this, args );
|
|
410
456
|
}
|
|
411
457
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export { initialize, destroy, onAfterInitialize } from "../dist/es/
|
|
1
|
+
export { initialize, destroy, onAfterInitialize } from "../dist/es/initializer";
|
|
2
2
|
|
|
3
|
-
export * from '../dist/es/
|
|
4
|
-
|
|
3
|
+
export * from '../dist/es/exports';
|
|
4
|
+
|
|
5
|
+
export * as managers from "../dist/es/managers";
|
|
5
6
|
|