@zenflux/core 0.1.29 → 0.1.31
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/.tsconfig.probe.tsbuildinfo +1 -0
- package/dist/src/modules/logger/implementation.d.ts +2 -1
- package/dist/zenflux-core.cjs +38 -38
- package/dist/zenflux-core.mjs +38 -38
- package/package.json +2 -2
- package/src/bases/cache-base.ts +64 -0
- package/src/bases/controller-base.ts +96 -0
- package/src/bases/index.ts +9 -0
- package/src/bases/initialize-base.ts +32 -0
- package/src/bases/object-base.ts +55 -0
- package/src/bases/singleton-base.ts +18 -0
- package/src/clients/http.ts +152 -0
- package/src/command-bases/command-base.ts +80 -0
- package/src/command-bases/command-internal.ts +11 -0
- package/src/command-bases/command-public.ts +12 -0
- package/src/command-bases/command-restful.ts +55 -0
- package/src/command-bases/index.ts +8 -0
- package/src/errors/command-already-registered.ts +13 -0
- package/src/errors/command-not-found.ts +12 -0
- package/src/errors/controller-already-registered.ts +13 -0
- package/src/errors/controller-already-set.ts +13 -0
- package/src/errors/error-with-metadata.ts +29 -0
- package/src/errors/force-method-implementation.ts +15 -0
- package/src/errors/index-public.ts +1 -0
- package/src/errors/index.ts +10 -0
- package/src/exports-index.ts +15 -0
- package/src/exports.ts +55 -0
- package/src/index.ts +18 -0
- package/src/interfaces/commands.ts +17 -0
- package/src/interfaces/config.ts +23 -0
- package/src/interfaces/index.ts +8 -0
- package/src/interfaces/logger.ts +19 -0
- package/src/interfaces/restful.ts +23 -0
- package/src/managers/commands.ts +269 -0
- package/src/managers/controllers.ts +40 -0
- package/src/managers/export.ts +30 -0
- package/src/managers/index.ts +8 -0
- package/src/managers/internal.ts +11 -0
- package/src/managers/restful.ts +117 -0
- package/src/modules/debugger.ts +86 -0
- package/src/modules/event-bus/event-bus.ts +238 -0
- package/src/modules/export.ts +6 -0
- package/src/modules/logger/config.ts +66 -0
- package/src/modules/logger/create-logger.ts +38 -0
- package/src/modules/logger/implementation.ts +371 -0
- package/src/modules/logger/index.ts +3 -0
- package/src/modules/logger/logger-browser-infra.ts +364 -0
- package/src/modules/logger/types/picocolors.d.ts +15 -0
- package/src/modules/logger/utils/index.ts +3 -0
- package/src/modules/logger/utils/object.ts +19 -0
- package/src/modules/logger/utils/rgb.ts +34 -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
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import pc from "picocolors";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
getLoggerLogLevel,
|
|
5
|
+
getLoggerLogLevelString,
|
|
6
|
+
getLoggerTimeFormat,
|
|
7
|
+
isLoggerDebugEnabled,
|
|
8
|
+
isLoggerDisabled,
|
|
9
|
+
isLoggerPreviousSourceDisabled
|
|
10
|
+
} from "./config";
|
|
11
|
+
|
|
12
|
+
import { LoggerBrowserInfra } from "./logger-browser-infra";
|
|
13
|
+
|
|
14
|
+
import { reduceCircularReferences } from "./utils";
|
|
15
|
+
|
|
16
|
+
import { ObjectBase } from "../../bases/object-base";
|
|
17
|
+
import { EventBus } from "../event-bus/event-bus";
|
|
18
|
+
|
|
19
|
+
import type { LoggerOutputFormat, LoggerOutputMetadata, LoggerOutputSubscriber } from "./logger-browser-infra";
|
|
20
|
+
|
|
21
|
+
import type * as interfaces from "../../interfaces";
|
|
22
|
+
|
|
23
|
+
const DEFAULT_LOG_PREFIX = pc.white( "⚪ - [LOG]" ),
|
|
24
|
+
DEFAULT_INFO_PREFIX = pc.blue( "🔵 - [INFO]" ),
|
|
25
|
+
DEFAULT_DEBUG_PREFIX = pc.gray( "🟤 - [DEBUG]" ),
|
|
26
|
+
DEFAULT_WARN_PREFIX = pc.yellow( "🟡 - [WARN]" ),
|
|
27
|
+
DEFAULT_ERROR_PREFIX = pc.red( "🔴 - [ERROR]" ),
|
|
28
|
+
DEFAULT_ADMIN_PREFIX = pc.bold( "🟣 - [ADMIN]" );
|
|
29
|
+
|
|
30
|
+
type LoggerLevel = "log" | "info" | "debug" | "warn" | "error" | "admin";
|
|
31
|
+
|
|
32
|
+
const LEVEL_LABELS: Record<LoggerLevel, string> = {
|
|
33
|
+
log: "LOG",
|
|
34
|
+
info: "INFO",
|
|
35
|
+
debug: "DEBUG",
|
|
36
|
+
warn: "WARN",
|
|
37
|
+
error: "ERROR",
|
|
38
|
+
admin: "ADMIN",
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const registeredNames: Record<string, boolean> = {};
|
|
42
|
+
|
|
43
|
+
type ICaller = interfaces.TCaller;
|
|
44
|
+
|
|
45
|
+
interface LoggerOptions {
|
|
46
|
+
skipEventBusHook?: boolean;
|
|
47
|
+
repeatedly?: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class Logger extends LoggerBrowserInfra implements interfaces.ILogger {
|
|
51
|
+
private static lastLogTime: number = Date.now();
|
|
52
|
+
private static timestampFormat: string = getLoggerTimeFormat();
|
|
53
|
+
|
|
54
|
+
private readonly ownerName: string;
|
|
55
|
+
|
|
56
|
+
private messagePrefixes: string[] = [];
|
|
57
|
+
|
|
58
|
+
private config: LoggerOptions = {};
|
|
59
|
+
|
|
60
|
+
public static setTimestampFormat( format: string ) {
|
|
61
|
+
Logger.timestampFormat = ( format || "" ).trim();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public static getTimestampFormat() {
|
|
65
|
+
return Logger.timestampFormat;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public static getName(): string {
|
|
69
|
+
return "ZenFlux/Core/Modules/Logger";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public static getLogLevelString(): string {
|
|
73
|
+
return getLoggerLogLevelString();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public static getLogLevel(): number {
|
|
77
|
+
return getLoggerLogLevel();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public static attachOutputListener( listener: LoggerOutputSubscriber ) {
|
|
81
|
+
return LoggerBrowserInfra.attachOutputSubscriber( listener );
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public static isDebugEnabled() {
|
|
85
|
+
return isLoggerDebugEnabled();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public constructor( owner: any, options?: LoggerOptions ) {
|
|
89
|
+
const { repeatedly = false, skipEventBusHook } = options || {};
|
|
90
|
+
|
|
91
|
+
super( owner, { repeatedly } );
|
|
92
|
+
|
|
93
|
+
this.config = { skipEventBusHook };
|
|
94
|
+
this.ownerName = Logger.resolveOwnerName( owner );
|
|
95
|
+
|
|
96
|
+
if ( registeredNames[ this.ownerName ] ) {
|
|
97
|
+
throw new Error( `Logger for '${ this.ownerName }' already exists` );
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
registeredNames[ this.ownerName ] = true;
|
|
101
|
+
|
|
102
|
+
if ( isLoggerDisabled() ) {
|
|
103
|
+
this.disableLoggers();
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if ( isLoggerPreviousSourceDisabled() ) {
|
|
108
|
+
this.getPreviousSource = () => "";
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
switch ( getLoggerLogLevel() ) {
|
|
112
|
+
case 0:
|
|
113
|
+
this.error = () => {};
|
|
114
|
+
// falls through
|
|
115
|
+
case 1:
|
|
116
|
+
this.warn = () => {};
|
|
117
|
+
// falls through
|
|
118
|
+
case 2:
|
|
119
|
+
this.admin = () => {};
|
|
120
|
+
// falls through
|
|
121
|
+
case 3:
|
|
122
|
+
this.info = () => {};
|
|
123
|
+
// falls through
|
|
124
|
+
case 4:
|
|
125
|
+
this.log = () => {};
|
|
126
|
+
// falls through
|
|
127
|
+
case 5:
|
|
128
|
+
this.debug = () => {};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if ( !this.config.skipEventBusHook ) {
|
|
132
|
+
EventBus.$.registerMultiInstances( this, [ this.outputEvent ] );
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public addMessagePrefix( prefix: string ) {
|
|
137
|
+
this.messagePrefixes.push( prefix );
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public log( caller: ICaller, message: string, ...params: any[] ): void {
|
|
141
|
+
this.writeLog( "log", DEFAULT_LOG_PREFIX, caller, message, ...params );
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public info( caller: ICaller, message: string, ...params: any[] ): void {
|
|
145
|
+
this.writeLog( "info", DEFAULT_INFO_PREFIX, caller, message, ...params );
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public debug( caller: ICaller, message: string, ...params: any[] ): void {
|
|
149
|
+
this.writeLog( "debug", DEFAULT_DEBUG_PREFIX, caller, message, ...params );
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public warn( caller: ICaller, message: string, ...params: any[] ): void {
|
|
153
|
+
this.writeLog( "warn", DEFAULT_WARN_PREFIX, caller, message, ...params );
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public error( caller: ICaller, message: string, ...params: any[] ): void {
|
|
157
|
+
this.writeLog( "error", DEFAULT_ERROR_PREFIX, caller, message, ...params );
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
public admin( caller: ICaller, message: string, ...params: any[] ): void {
|
|
161
|
+
this.writeLog( "admin", DEFAULT_ADMIN_PREFIX, caller, message, ...params );
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
public startsEmpty( caller: interfaces.TCaller ) {
|
|
165
|
+
this.printFunctionNotify( "▶", caller, "" );
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
public startsWith( caller: interfaces.TCaller, params: string | object ) {
|
|
169
|
+
this.printObjectEfficient( "▶", caller, params );
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
public dump( caller: interfaces.TCaller, params: { [ key: string ]: object | string } = {}, notice = "" ) {
|
|
173
|
+
for ( const key in params ) {
|
|
174
|
+
if ( typeof params[ key ] === "object" ) {
|
|
175
|
+
params[ key ] = JSON.stringify(
|
|
176
|
+
LoggerBrowserInfra.useObjectMapper( params[ key ] as object ),
|
|
177
|
+
reduceCircularReferences()
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
this.printInLineElement( "dp", caller, key, params[ key ], notice );
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
public drop( caller: interfaces.TCaller, according: { [ key: string ]: string }, data: any ) {
|
|
186
|
+
for ( const key in according ) {
|
|
187
|
+
this.printInLineElement( "dr", caller, key, according[ key ], "corresponding" );
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
this.output( data );
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* TODO: Should respect debug levels, when to throw...
|
|
195
|
+
*/
|
|
196
|
+
public throw( caller: interfaces.TCaller, output: string, name: string = "null", params = {} ) {
|
|
197
|
+
this.printFunctionNotify( "tw", caller, output );
|
|
198
|
+
|
|
199
|
+
if ( params ) {
|
|
200
|
+
this.printInNextLineObject( "tw", caller, name, params );
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
throw new Error().stack;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
public beep() {
|
|
207
|
+
console.log( "\x07" );
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
public clone() {
|
|
211
|
+
return Object.assign( Object.create( Object.getPrototypeOf( this ) ), this );
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
public outputEvent( _prefix: string, _timeDiff: string, _source: string, _messagePrefix: string, _message: string, _params: any[] ): void {
|
|
215
|
+
// Intentionally left blank, can be hooked via EventBus.
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private static resolveOwnerName( owner: any ): string {
|
|
219
|
+
if ( typeof owner === "string" ) {
|
|
220
|
+
return owner;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if ( owner instanceof ObjectBase ) {
|
|
224
|
+
return owner.getName();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if ( owner && typeof owner.getName === "function" ) {
|
|
228
|
+
return owner.getName();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return "UnknownOwner";
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private disableLoggers() {
|
|
235
|
+
this.error = () => {};
|
|
236
|
+
this.warn = () => {};
|
|
237
|
+
this.admin = () => {};
|
|
238
|
+
this.info = () => {};
|
|
239
|
+
this.log = () => {};
|
|
240
|
+
this.debug = () => {};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private getStackTrace(): any[] {
|
|
244
|
+
const stackTrace = ( new Error().stack || "" ).split( "\n" );
|
|
245
|
+
const stackLines = stackTrace.slice( 1 );
|
|
246
|
+
|
|
247
|
+
const stackRegex = / at (.+?) \((.+?)\)/;
|
|
248
|
+
const result = [];
|
|
249
|
+
|
|
250
|
+
for ( const line of stackLines ) {
|
|
251
|
+
const match = line.match( stackRegex );
|
|
252
|
+
|
|
253
|
+
if ( match ) {
|
|
254
|
+
const [ , context, file ] = match;
|
|
255
|
+
const parsedLine: any = { context, file };
|
|
256
|
+
|
|
257
|
+
if ( line.startsWith( "new" ) ) {
|
|
258
|
+
parsedLine.isNew = true;
|
|
259
|
+
parsedLine.object = context.split( " " )[ 1 ];
|
|
260
|
+
} else if ( context !== "Object.<anonymous>" ) {
|
|
261
|
+
parsedLine.object = context;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
result.push( parsedLine );
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return result;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
public getPreviousSource(): string {
|
|
272
|
+
const stack = this.getStackTrace()
|
|
273
|
+
.filter( ( line: any ) => line.file.includes( "/src/" ) )
|
|
274
|
+
.filter( ( line: any ) => !line.file.includes( "logger.ts" ) )
|
|
275
|
+
.filter( ( line: any ) => !line.file.includes( "debugger.ts" ) )
|
|
276
|
+
.filter( ( line: any ) => !line.file.includes( "/node_modules/" ) );
|
|
277
|
+
|
|
278
|
+
let previousSource = "";
|
|
279
|
+
|
|
280
|
+
const previousCaller = stack[ 1 ]?.object?.split( "." );
|
|
281
|
+
|
|
282
|
+
if ( previousCaller?.length > 1 ) {
|
|
283
|
+
const previousCallerName = previousCaller[ 0 ],
|
|
284
|
+
previousCallerMethod = previousCaller[ 1 ];
|
|
285
|
+
|
|
286
|
+
previousSource = `${ previousCallerName }::${ previousCallerMethod }]` + "[";
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return previousSource;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
private writeLog( level: LoggerLevel, prefix: string, caller: ICaller, message: string, ...params: any[] ): void {
|
|
293
|
+
const callerName = this.getCallerName( caller );
|
|
294
|
+
const source = this.getPreviousSource() + pc.white( `${ this.ownerName }::${ callerName }` );
|
|
295
|
+
|
|
296
|
+
let messagePrefix = "";
|
|
297
|
+
|
|
298
|
+
if ( this.messagePrefixes.length ) {
|
|
299
|
+
messagePrefix = `[${ this.messagePrefixes.join( "][" ) }]`;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const timestampValue = Date.now();
|
|
303
|
+
const timeDiff = ( timestampValue - Logger.lastLogTime ).toString().padStart( 4, "0" );
|
|
304
|
+
|
|
305
|
+
const timeFormat = Logger.getTimestampFormat();
|
|
306
|
+
const timestamp = timeFormat ? this.formatTime( new Date( timestampValue ), timeFormat ) : "";
|
|
307
|
+
|
|
308
|
+
this.outputEvent( prefix, timeDiff, source, messagePrefix, message, params );
|
|
309
|
+
|
|
310
|
+
const timestampPart = timestamp ? `${ timestamp } ` : "";
|
|
311
|
+
const output = `${ timestampPart }${ prefix }[+${ timeDiff }ms][${ source }]${ messagePrefix }: ${ message }`;
|
|
312
|
+
|
|
313
|
+
const formatted = this.createBrowserFormat( level, callerName, messagePrefix, message );
|
|
314
|
+
const metadata: LoggerOutputMetadata = {
|
|
315
|
+
id: `${ timestampValue }-${ Math.random().toString( 16 ).slice( 2 ) }`,
|
|
316
|
+
level,
|
|
317
|
+
namespace: this.ownerName,
|
|
318
|
+
callerName,
|
|
319
|
+
message,
|
|
320
|
+
messagePrefix,
|
|
321
|
+
payload: params.length === 0 ? undefined : ( params.length === 1 ? params[ 0 ] : params ),
|
|
322
|
+
timestamp: timestampValue,
|
|
323
|
+
formatted,
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
this.runWithOutputMetadata( metadata, () => {
|
|
327
|
+
this.output( output, ...params );
|
|
328
|
+
} );
|
|
329
|
+
|
|
330
|
+
Logger.lastLogTime = timestampValue;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private createBrowserFormat( level: LoggerLevel, callerName: string, messagePrefix: string, message: string ): LoggerOutputFormat {
|
|
334
|
+
const prefixLabel = LEVEL_LABELS[ level ] || level.toUpperCase();
|
|
335
|
+
const prefixPart = messagePrefix ? `${ messagePrefix } ` : "";
|
|
336
|
+
|
|
337
|
+
return {
|
|
338
|
+
format: `%c(${ prefixLabel })-> %c%c${ this.ownerName }%c::%c${ callerName }%c() ${ prefixPart }${ message }%c`,
|
|
339
|
+
styles: [ ...this.defaultStyle ],
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
private formatTime( date: Date, format: string ): string {
|
|
344
|
+
// Supported tokens (longest matched first):
|
|
345
|
+
// YYYY / Y year
|
|
346
|
+
// M month (01-12)
|
|
347
|
+
// D day (01-31)
|
|
348
|
+
// HH / hh hours 00-23
|
|
349
|
+
// mm minutes 00-59
|
|
350
|
+
// ss seconds 00-59
|
|
351
|
+
// SSS / S milliseconds (000-999)
|
|
352
|
+
const pad = ( num: number, size = 2 ) => num.toString().padStart( size, "0" );
|
|
353
|
+
|
|
354
|
+
const tokens: Record<string, string> = {
|
|
355
|
+
"YYYY": date.getFullYear().toString(),
|
|
356
|
+
"Y": date.getFullYear().toString(),
|
|
357
|
+
"M": pad( date.getMonth() + 1 ),
|
|
358
|
+
"D": pad( date.getDate() ),
|
|
359
|
+
"HH": pad( date.getHours() ),
|
|
360
|
+
"hh": pad( date.getHours() ),
|
|
361
|
+
"mm": pad( date.getMinutes() ),
|
|
362
|
+
"ss": pad( date.getSeconds() ),
|
|
363
|
+
"SSS": pad( date.getMilliseconds(), 3 ),
|
|
364
|
+
"S": pad( date.getMilliseconds(), 3 ),
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
return format.replace( /YYYY|HH|hh|mm|ss|SSS|Y|M|D|S|h|m|s/g, ( token ) => tokens[ token ] ?? token );
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export default Logger;
|