alemonjs 2.1.83-alpha.1 → 2.1.83-alpha.2
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/lib/{app → application}/define-children.js +2 -1
- package/lib/{app → application/format}/message-api.d.ts +2 -2
- package/lib/{app → application/format}/message-api.js +12 -4
- package/lib/{app → application/format}/message-format-old.d.ts +1 -1
- package/lib/{app → application/format}/message-format.d.ts +1 -2
- package/lib/{app → application/format}/message-format.js +0 -2
- package/lib/{app/hook-use → application/hooks}/announce.js +4 -3
- package/lib/{app/hook-use → application/hooks}/channel.js +4 -3
- package/lib/{app/hook-use → application/hooks}/client.d.ts +1 -1
- package/lib/{app/hook-use → application/hooks}/client.js +4 -11
- package/lib/{app/hook-use → application/hooks}/common.d.ts +7 -7
- package/lib/{app/hook-use → application/hooks}/common.js +9 -8
- package/lib/{app/hook-use → application/hooks}/event.js +1 -1
- package/lib/{app/hook-use → application/hooks}/guild.js +4 -3
- package/lib/{app/hook-use → application/hooks}/history.js +4 -3
- package/lib/{app/hook-use → application/hooks}/me.js +12 -7
- package/lib/{app/hook-use → application/hooks}/media.d.ts +3 -3
- package/lib/{app/hook-use → application/hooks}/media.js +4 -3
- package/lib/{app/hook-use → application/hooks}/member.js +4 -3
- package/lib/{app/hook-use → application/hooks}/mention.js +4 -3
- package/lib/{app/hook-use → application/hooks}/message.js +6 -5
- package/lib/{app/hook-use → application/hooks}/permission.js +4 -3
- package/lib/{app/hook-use → application/hooks}/reaction.js +4 -3
- package/lib/{app/hook-use → application/hooks}/request.js +12 -7
- package/lib/{app/hook-use → application/hooks}/role.js +4 -3
- package/lib/{app/hook-use → application/hooks}/subscribe.js +4 -4
- package/lib/{app/hook-use → application/hooks}/user.js +12 -7
- package/lib/application/index.d.ts +12 -0
- package/lib/application/index.js +32 -0
- package/lib/{app → application}/router/dsl.d.ts +1 -1
- package/lib/{app → application}/router/dsl.js +11 -7
- package/lib/{cbp → application/runtime/cbp}/connects/client.d.ts +1 -1
- package/lib/{cbp → application/runtime/cbp}/connects/client.js +11 -20
- package/lib/application/runtime/cbp/index.d.ts +3 -0
- package/lib/application/runtime/cbp/index.js +3 -0
- package/lib/application/runtime/cbp/processor/actions.d.ts +3 -0
- package/lib/{cbp → application/runtime/cbp}/processor/actions.js +12 -4
- package/lib/application/runtime/cbp/processor/api.d.ts +3 -0
- package/lib/{cbp → application/runtime/cbp}/processor/api.js +12 -4
- package/lib/application/runtime/cbp/processor/request-registry.d.ts +8 -0
- package/lib/application/runtime/cbp/processor/request-registry.js +6 -0
- package/lib/application/runtime/client-runtime.d.ts +1 -0
- package/lib/application/runtime/client-runtime.js +95 -0
- package/lib/{app → application/runtime}/event-error.d.ts +1 -1
- package/lib/{app → application/runtime}/event-error.js +1 -1
- package/lib/{app → application/runtime}/event-group.d.ts +1 -1
- package/lib/{app → application/runtime}/event-middleware.d.ts +1 -1
- package/lib/{app → application/runtime}/event-middleware.js +1 -1
- package/lib/{app → application/runtime}/event-processor-callHandler.d.ts +1 -1
- package/lib/{app → application/runtime}/event-processor-callHandler.js +8 -1
- package/lib/{app → application/runtime}/event-processor-cycle.d.ts +1 -1
- package/lib/{app → application/runtime}/event-processor-cycleFiles.d.ts +1 -1
- package/lib/{app → application/runtime}/event-processor-cycleFiles.js +2 -2
- package/lib/{app → application/runtime}/event-processor-cycleRoute.d.ts +1 -1
- package/lib/{app → application/runtime}/event-processor-cycleRoute.js +9 -2
- package/lib/{app → application/runtime}/event-processor-event.d.ts +1 -1
- package/lib/{app → application/runtime}/event-processor-middleware.d.ts +1 -1
- package/lib/{app → application/runtime}/event-processor-subscribe.d.ts +1 -1
- package/lib/{app → application/runtime}/event-processor-subscribe.js +9 -2
- package/lib/{app → application/runtime}/event-processor.d.ts +1 -1
- package/lib/{app → application/runtime}/event-processor.js +4 -3
- package/lib/{app → application/runtime}/event-response.d.ts +1 -1
- package/lib/{app → application/runtime}/event-utils.d.ts +1 -1
- package/lib/{app → application/runtime}/event-utils.js +2 -2
- package/lib/{app → application/runtime}/hook-event-context.d.ts +2 -2
- package/lib/{app → application/runtime}/hook-event-context.js +2 -2
- package/lib/application/runtime/http/index.d.ts +4 -0
- package/lib/application/runtime/http/index.js +4 -0
- package/lib/{server → application/runtime/http}/routers/hello.html.js +1 -1
- package/lib/{server → application/runtime/http}/routers/middleware.js +1 -2
- package/lib/{server → application/runtime/http}/routers/router.js +11 -4
- package/lib/{server → application/runtime/http}/routers/utils.d.ts +1 -1
- package/lib/{server → application/runtime/http}/routers/utils.js +6 -6
- package/lib/{server/main.js → application/runtime/http-server.js} +2 -6
- package/lib/{app → application/runtime}/lifecycle-callbacks.d.ts +1 -1
- package/lib/{app → application/runtime}/lifecycle-callbacks.js +2 -7
- package/lib/{app/load_modules → application/runtime/load-modules}/load.js +2 -2
- package/lib/{app/load_modules → application/runtime/load-modules}/loadChild.js +3 -3
- package/lib/{app → application/runtime}/schedule-store.d.ts +1 -1
- package/lib/{app → application/runtime}/schedule-store.js +1 -1
- package/lib/{app → application/runtime}/store.d.ts +14 -19
- package/lib/{app → application/runtime}/store.js +8 -103
- package/lib/application/schedule.d.ts +17 -0
- package/lib/{app/api → application}/schedule.js +1 -1
- package/lib/client.d.ts +1 -1
- package/lib/client.js +1 -117
- package/lib/common/cbp/constants.d.ts +11 -0
- package/lib/common/cbp/constants.js +13 -0
- package/lib/{cbp/connects/connect.js → common/cbp/heartbeat.js} +3 -6
- package/lib/{cbp → common/cbp}/normalize.d.ts +2 -2
- package/lib/{cbp → common/cbp}/normalize.js +1 -5
- package/lib/common/cbp/runtime.d.ts +2 -0
- package/lib/common/cbp/runtime.js +10 -0
- package/lib/{cbp → common/cbp}/typings.d.ts +2 -2
- package/lib/{cbp/connects/base.js → common/cbp/ws-connector.js} +7 -9
- package/lib/{core → common}/config.d.ts +2 -2
- package/lib/{core → common}/config.js +5 -4
- package/lib/common/identity.d.ts +11 -0
- package/lib/common/identity.js +34 -0
- package/lib/common/index.d.ts +15 -0
- package/lib/common/index.js +14 -0
- package/lib/common/logger.d.ts +6 -0
- package/lib/common/logger.js +99 -0
- package/lib/common/result.d.ts +7 -0
- package/lib/common/result.js +19 -0
- package/lib/{core → common}/utils.d.ts +3 -18
- package/lib/{core → common}/utils.js +6 -49
- package/lib/{core → common}/variable.js +1 -1
- package/lib/core/cbp/index.d.ts +1 -0
- package/lib/core/cbp/index.js +1 -0
- package/lib/core/cbp/processor/config.d.ts +8 -0
- package/lib/core/cbp/processor/config.js +29 -0
- package/lib/{cbp → core/cbp}/routers/hello.html.js +1 -1
- package/lib/{cbp → core/cbp}/server/main.js +10 -4
- package/lib/{cbp → core/cbp}/server/testone.d.ts +1 -1
- package/lib/{cbp → core/cbp}/server/testone.js +8 -2
- package/lib/core/index.d.ts +1 -3
- package/lib/core/index.js +1 -3
- package/lib/core/process/index.d.ts +3 -0
- package/lib/{process → core/process}/index.js +0 -1
- package/lib/{process → core/process}/ipc-bridge.js +6 -1
- package/lib/{process → core/process}/module.js +13 -4
- package/lib/{process → core/process}/platform.js +12 -3
- package/lib/core/start.d.ts +2 -0
- package/lib/core/start.js +76 -0
- package/lib/global.d.ts +4 -3
- package/lib/index.d.ts +5 -4
- package/lib/index.js +9 -54
- package/lib/main.d.ts +1 -2
- package/lib/main.js +1 -76
- package/lib/{cbp/connects/platform.d.ts → platform/cbp-platform.d.ts} +2 -2
- package/lib/{cbp/connects/platform.js → platform/cbp-platform.js} +8 -8
- package/lib/{app → platform}/define-platform.js +2 -0
- package/lib/platform/event-value.d.ts +7 -0
- package/lib/platform/event-value.js +5 -0
- package/lib/platform/index.d.ts +5 -0
- package/lib/platform/index.js +5 -0
- package/lib/types/client/index.d.ts +1 -1
- package/lib/types/event/index.d.ts +1 -1
- package/lib/types/subscribe/index.d.ts +1 -1
- package/package.json +22 -1
- package/lib/app/api/schedule.d.ts +0 -17
- package/lib/app/index.d.ts +0 -25
- package/lib/app/index.js +0 -47
- package/lib/cbp/index.d.ts +0 -3
- package/lib/cbp/index.js +0 -3
- package/lib/cbp/processor/actions.d.ts +0 -3
- package/lib/cbp/processor/api.d.ts +0 -3
- package/lib/cbp/processor/config.d.ts +0 -33
- package/lib/cbp/processor/config.js +0 -56
- package/lib/process/index.d.ts +0 -4
- /package/lib/{app → application}/define-children.d.ts +0 -0
- /package/lib/{app → application}/define-middleware.d.ts +0 -0
- /package/lib/{app → application}/define-middleware.js +0 -0
- /package/lib/{app → application}/define-response.d.ts +0 -0
- /package/lib/{app → application}/define-response.js +0 -0
- /package/lib/{app → application}/define-router.d.ts +0 -0
- /package/lib/{app → application}/define-router.js +0 -0
- /package/lib/{app → application}/expose.d.ts +0 -0
- /package/lib/{app → application}/expose.js +0 -0
- /package/lib/{app → application/format}/message-format-old.js +0 -0
- /package/lib/{app/hook-use → application/hooks}/announce.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/channel.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/event.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/guild.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/history.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/index.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/index.js +0 -0
- /package/lib/{app/hook-use → application/hooks}/me.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/member.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/mention.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/message.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/permission.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/reaction.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/request.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/role.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/route.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/route.js +0 -0
- /package/lib/{app/hook-use → application/hooks}/subscribe.d.ts +0 -0
- /package/lib/{app/hook-use → application/hooks}/user.d.ts +0 -0
- /package/lib/{app → application}/router/fallback.d.ts +0 -0
- /package/lib/{app → application}/router/fallback.js +0 -0
- /package/lib/{app → application}/router/main.d.ts +0 -0
- /package/lib/{app → application}/router/main.js +0 -0
- /package/lib/{app → application}/router/parser.d.ts +0 -0
- /package/lib/{app → application}/router/parser.js +0 -0
- /package/lib/{app → application}/router/types.d.ts +0 -0
- /package/lib/{app → application}/router/types.js +0 -0
- /package/lib/{app → application}/router/validator.d.ts +0 -0
- /package/lib/{app → application}/router/validator.js +0 -0
- /package/lib/{cbp → application/runtime/cbp}/processor/transport.d.ts +0 -0
- /package/lib/{cbp → application/runtime/cbp}/processor/transport.js +0 -0
- /package/lib/{app → application/runtime}/event-group.js +0 -0
- /package/lib/{app → application/runtime}/event-processor-cycle.js +0 -0
- /package/lib/{app → application/runtime}/event-processor-event.js +0 -0
- /package/lib/{app → application/runtime}/event-processor-middleware.js +0 -0
- /package/lib/{app → application/runtime}/event-response.js +0 -0
- /package/lib/{cbp → application/runtime/http}/routers/hello.html.d.ts +0 -0
- /package/lib/{server → application/runtime/http}/routers/middleware.d.ts +0 -0
- /package/lib/{cbp → application/runtime/http}/routers/router.d.ts +0 -0
- /package/lib/{server/main.d.ts → application/runtime/http-server.d.ts} +0 -0
- /package/lib/{app/load_modules → application/runtime/load-modules}/index.d.ts +0 -0
- /package/lib/{app/load_modules → application/runtime/load-modules}/index.js +0 -0
- /package/lib/{app/load_modules → application/runtime/load-modules}/load.d.ts +0 -0
- /package/lib/{app/load_modules → application/runtime/load-modules}/loadChild.d.ts +0 -0
- /package/lib/{app/config.d.ts → application/runtime/subscribe-status.d.ts} +0 -0
- /package/lib/{app/config.js → application/runtime/subscribe-status.js} +0 -0
- /package/lib/{app → common}/SinglyLinkedList.d.ts +0 -0
- /package/lib/{app → common}/SinglyLinkedList.js +0 -0
- /package/lib/{cbp/connects/connect.d.ts → common/cbp/heartbeat.d.ts} +0 -0
- /package/lib/{cbp → common/cbp}/typings.js +0 -0
- /package/lib/{cbp/connects/base.d.ts → common/cbp/ws-connector.d.ts} +0 -0
- /package/lib/{process → common}/direct-channel.d.ts +0 -0
- /package/lib/{process → common}/direct-channel.js +0 -0
- /package/lib/{core → common}/react.d.ts +0 -0
- /package/lib/{core → common}/react.js +0 -0
- /package/lib/{core → common}/variable.d.ts +0 -0
- /package/lib/{server → core/cbp}/routers/hello.html.d.ts +0 -0
- /package/lib/{server → core/cbp}/routers/router.d.ts +0 -0
- /package/lib/{cbp → core/cbp}/routers/router.js +0 -0
- /package/lib/{cbp → core/cbp}/server/main.d.ts +0 -0
- /package/lib/{process → core/process}/ipc-bridge.d.ts +0 -0
- /package/lib/{process → core/process}/module.d.ts +0 -0
- /package/lib/{process → core/process}/platform.d.ts +0 -0
- /package/lib/{app → platform}/define-platform.d.ts +0 -0
- /package/lib/{app → platform}/event-format.d.ts +0 -0
- /package/lib/{app → platform}/event-format.js +0 -0
|
@@ -1,102 +1,8 @@
|
|
|
1
|
-
import { SinglyLinkedList } from '
|
|
2
|
-
import {
|
|
3
|
-
import log4js from 'log4js';
|
|
4
|
-
import { disposeExpose } from './expose.js';
|
|
1
|
+
import { SinglyLinkedList } from '../../common/SinglyLinkedList.js';
|
|
2
|
+
import { disposeExpose } from '../expose.js';
|
|
5
3
|
import { dispatchRuntimeStatusChange } from './lifecycle-callbacks.js';
|
|
4
|
+
export { Logger, logger } from '../../common/logger.js';
|
|
6
5
|
|
|
7
|
-
const createLogger = () => {
|
|
8
|
-
if (process.env.BROWSER_ENV === 'browser') {
|
|
9
|
-
return {
|
|
10
|
-
trace: console.trace.bind(console),
|
|
11
|
-
debug: console.debug.bind(console),
|
|
12
|
-
info: console.info.bind(console),
|
|
13
|
-
mark: console.info.bind(console),
|
|
14
|
-
warn: console.warn.bind(console),
|
|
15
|
-
error: console.error.bind(console),
|
|
16
|
-
fatal: console.error.bind(console)
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
const logDir = process.env?.LOG_PATH ?? `./logs/${process.env.LOG_NAME ?? ''}`;
|
|
20
|
-
mkdirSync(logDir, { recursive: true });
|
|
21
|
-
const level = process.env.NODE_ENV === 'development' ? 'trace' : 'info';
|
|
22
|
-
const hideTime = process.env.LOGGER_TIME === 'false' ? true : false;
|
|
23
|
-
const hideLevel = process.env.LOGGER_LEVEL === 'false' ? true : false;
|
|
24
|
-
let pattern = '';
|
|
25
|
-
if (hideTime && hideLevel) {
|
|
26
|
-
pattern = '%m';
|
|
27
|
-
}
|
|
28
|
-
else if (hideTime && !hideLevel) {
|
|
29
|
-
pattern = '[%p] %m';
|
|
30
|
-
}
|
|
31
|
-
else if (!hideTime && hideLevel) {
|
|
32
|
-
pattern = '[%d{yyyy-MM-dd hh:mm:ss}] %m';
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
pattern = '[%d{yyyy-MM-dd hh:mm:ss}][%p] %m';
|
|
36
|
-
}
|
|
37
|
-
log4js.configure({
|
|
38
|
-
appenders: {
|
|
39
|
-
console: {
|
|
40
|
-
type: 'console',
|
|
41
|
-
layout: {
|
|
42
|
-
type: 'pattern',
|
|
43
|
-
pattern: pattern
|
|
44
|
-
}
|
|
45
|
-
},
|
|
46
|
-
command: {
|
|
47
|
-
type: 'dateFile',
|
|
48
|
-
filename: `${logDir}/command`,
|
|
49
|
-
pattern: 'yyyy-MM-dd.log',
|
|
50
|
-
numBackups: 15,
|
|
51
|
-
alwaysIncludePattern: true,
|
|
52
|
-
layout: {
|
|
53
|
-
type: 'pattern',
|
|
54
|
-
pattern: pattern
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
error: {
|
|
58
|
-
type: 'dateFile',
|
|
59
|
-
filename: `${logDir}/error`,
|
|
60
|
-
pattern: 'yyyy-MM-dd.log',
|
|
61
|
-
numBackups: 15,
|
|
62
|
-
alwaysIncludePattern: true,
|
|
63
|
-
layout: {
|
|
64
|
-
type: 'pattern',
|
|
65
|
-
pattern: pattern
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
categories: {
|
|
70
|
-
default: { appenders: ['console'], level: level },
|
|
71
|
-
command: { appenders: ['console', 'command'], level: 'info' },
|
|
72
|
-
error: { appenders: ['console', 'command', 'error'], level: 'warn' }
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
const defaultLogger = log4js.getLogger('default');
|
|
76
|
-
const commandLogger = log4js.getLogger('command');
|
|
77
|
-
const errorLogger = log4js.getLogger('error');
|
|
78
|
-
return {
|
|
79
|
-
trace: defaultLogger.trace.bind(defaultLogger),
|
|
80
|
-
debug: defaultLogger.debug.bind(defaultLogger),
|
|
81
|
-
info: commandLogger.info.bind(commandLogger),
|
|
82
|
-
mark: commandLogger.mark.bind(commandLogger),
|
|
83
|
-
warn: errorLogger.warn.bind(errorLogger),
|
|
84
|
-
error: errorLogger.error.bind(errorLogger),
|
|
85
|
-
fatal: errorLogger.fatal.bind(errorLogger)
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
class Logger {
|
|
89
|
-
#logger = null;
|
|
90
|
-
constructor() {
|
|
91
|
-
this.#logger = createLogger();
|
|
92
|
-
if (!global.logger) {
|
|
93
|
-
global.logger = this.#logger;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
get value() {
|
|
97
|
-
return this.#logger;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
6
|
class Core {
|
|
101
7
|
constructor() {
|
|
102
8
|
if (!global.alemonjsCore) {
|
|
@@ -201,7 +107,7 @@ const registerRuntimeApp = (record) => {
|
|
|
201
107
|
createdAt: current?.createdAt ?? now,
|
|
202
108
|
updatedAt: now
|
|
203
109
|
};
|
|
204
|
-
if (
|
|
110
|
+
if (current?.status !== record.status) {
|
|
205
111
|
logRuntimeAppStatus(record.status === 'failed' ? 'warn' : 'debug', runtimeApps[record.name]);
|
|
206
112
|
}
|
|
207
113
|
return runtimeApps[record.name];
|
|
@@ -255,7 +161,7 @@ const updateRuntimeAppCapabilities = (name, capabilities) => {
|
|
|
255
161
|
const setRuntimeAppKoaRouters = (name, koaRouters) => {
|
|
256
162
|
const koaRouterStore = getRuntimeAppKoaRouterStore();
|
|
257
163
|
if (!koaRouters) {
|
|
258
|
-
|
|
164
|
+
Reflect.deleteProperty(koaRouterStore, name);
|
|
259
165
|
return [];
|
|
260
166
|
}
|
|
261
167
|
const normalizedRouters = (Array.isArray(koaRouters) ? koaRouters : [koaRouters]).filter(Boolean);
|
|
@@ -267,7 +173,7 @@ const getRuntimeAppKoaRouters = (name) => {
|
|
|
267
173
|
};
|
|
268
174
|
const clearRuntimeAppKoaRouters = (name) => {
|
|
269
175
|
const koaRouterStore = getRuntimeAppKoaRouterStore();
|
|
270
|
-
|
|
176
|
+
Reflect.deleteProperty(koaRouterStore, name);
|
|
271
177
|
};
|
|
272
178
|
const listRuntimeAppKoaRouters = () => {
|
|
273
179
|
return Object.entries(getRuntimeAppKoaRouterStore())
|
|
@@ -642,7 +548,7 @@ class ChildrenApp {
|
|
|
642
548
|
}
|
|
643
549
|
un() {
|
|
644
550
|
disposeExpose(this.#name);
|
|
645
|
-
|
|
551
|
+
Reflect.deleteProperty(alemonjsCore.storeChildrenApp, this.#name);
|
|
646
552
|
bumpStoreVersion();
|
|
647
553
|
}
|
|
648
554
|
get value() {
|
|
@@ -660,7 +566,6 @@ const listChildrenApps = () => {
|
|
|
660
566
|
};
|
|
661
567
|
const ProcessorEventAutoClearMap = new Map();
|
|
662
568
|
const ProcessorEventUserAutoClearMap = new Map();
|
|
663
|
-
const logger = new Logger().value;
|
|
664
569
|
const core = new Core().value;
|
|
665
570
|
['SIGINT', 'SIGTERM', 'SIGQUIT', 'disconnect'].forEach(sig => {
|
|
666
571
|
process?.on?.(sig, () => {
|
|
@@ -671,4 +576,4 @@ process?.on?.('exit', code => {
|
|
|
671
576
|
logger.info?.(`[alemonjs][exit] 进程退出,code=${code}`);
|
|
672
577
|
});
|
|
673
578
|
|
|
674
|
-
export { ChildrenApp, Core,
|
|
579
|
+
export { ChildrenApp, Core, Middleware, MiddlewareRouter, MiddlewareTree, ProcessorEventAutoClearMap, ProcessorEventUserAutoClearMap, Response, ResponseMiddleware, ResponseRouter, ResponseTree, State, StateSubscribe, SubscribeList, bumpStoreVersion, clearRuntimeAppKoaRouters, core, disposeAllRuntimeApps, disposeRuntimeApp, getChildrenApp, getRuntimeApp, getRuntimeAppKoaRouters, getSubscribeList, hasRuntimeAppCapability, listChildrenApps, listRuntimeAppKoaRouters, listRuntimeApps, registerRuntimeApp, setRuntimeAppKoaRouters, toRuntimeAppSnapshot, updateRuntimeAppCapabilities, updateRuntimeAppStatus };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ScheduleCallback, CronExpression } from '../types/schedule';
|
|
2
|
+
export declare const setInterval: (callback: ScheduleCallback, ms: number) => string;
|
|
3
|
+
export declare const setTimeout: (callback: ScheduleCallback, ms: number) => string;
|
|
4
|
+
export declare const setCron: (expression: CronExpression, callback: ScheduleCallback) => string;
|
|
5
|
+
export declare const clearInterval: (id: import("./index.js").ScheduleId) => boolean;
|
|
6
|
+
export declare const clearTimeout: (id: import("./index.js").ScheduleId) => boolean;
|
|
7
|
+
export declare const pauseSchedule: (id: import("./index.js").ScheduleId) => boolean;
|
|
8
|
+
export declare const resumeSchedule: (id: import("./index.js").ScheduleId) => boolean;
|
|
9
|
+
export declare const listSchedule: (appName?: string) => Array<{
|
|
10
|
+
id: import("./index.js").ScheduleId;
|
|
11
|
+
type: import("./index.js").ScheduleItem["type"];
|
|
12
|
+
status: import("./index.js").ScheduleStatus;
|
|
13
|
+
ms?: number;
|
|
14
|
+
cron?: string;
|
|
15
|
+
appName?: string;
|
|
16
|
+
createdAt: number;
|
|
17
|
+
}>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { scheduleCancel, scheduleList, schedulePause, scheduleResume, scheduleCron, scheduleInterval, scheduleTimeout } from '
|
|
1
|
+
import { scheduleCancel, scheduleList, schedulePause, scheduleResume, scheduleCron, scheduleInterval, scheduleTimeout } from './runtime/schedule-store.js';
|
|
2
2
|
|
|
3
3
|
const setInterval = (callback, ms) => scheduleInterval(callback, ms);
|
|
4
4
|
const setTimeout = (callback, ms) => scheduleTimeout(callback, ms);
|
package/lib/client.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
import './application/runtime/client-runtime.js';
|
package/lib/client.js
CHANGED
|
@@ -1,117 +1 @@
|
|
|
1
|
-
import './
|
|
2
|
-
import { defaultPort } from './core/variable.js';
|
|
3
|
-
import 'fs';
|
|
4
|
-
import 'path';
|
|
5
|
-
import 'yaml';
|
|
6
|
-
import './core/utils.js';
|
|
7
|
-
import { cbpClient } from './cbp/connects/client.js';
|
|
8
|
-
import 'flatted';
|
|
9
|
-
import 'ws';
|
|
10
|
-
import './cbp/processor/config.js';
|
|
11
|
-
import 'net';
|
|
12
|
-
import 'v8';
|
|
13
|
-
import 'os';
|
|
14
|
-
import 'koa';
|
|
15
|
-
import '@koa/cors';
|
|
16
|
-
import './cbp/routers/router.js';
|
|
17
|
-
import 'lodash';
|
|
18
|
-
import 'fs/promises';
|
|
19
|
-
import { disposeAllRuntimeApps } from './app/store.js';
|
|
20
|
-
import { loadModels } from './app/load_modules/load.js';
|
|
21
|
-
import './app/load_modules/loadChild.js';
|
|
22
|
-
import './app/define-children.js';
|
|
23
|
-
import './app/define-response.js';
|
|
24
|
-
import './app/define-middleware.js';
|
|
25
|
-
import './app/event-group.js';
|
|
26
|
-
import './app/event-middleware.js';
|
|
27
|
-
import './app/event-processor.js';
|
|
28
|
-
import './app/event-response.js';
|
|
29
|
-
import './app/hook-event-context.js';
|
|
30
|
-
import './app/message-format-old.js';
|
|
31
|
-
import { scheduleCancelByApp, unregisterAppDir } from './app/schedule-store.js';
|
|
32
|
-
import './app/event-utils.js';
|
|
33
|
-
import './app/message-api.js';
|
|
34
|
-
import './process/platform.js';
|
|
35
|
-
import './process/module.js';
|
|
36
|
-
import { createServer } from './server/main.js';
|
|
37
|
-
import { dispatchDisposeAllApps } from './app/lifecycle-callbacks.js';
|
|
38
|
-
|
|
39
|
-
global.__client_loaded = true;
|
|
40
|
-
let runtimeDisposed = false;
|
|
41
|
-
const disposeRuntime = async () => {
|
|
42
|
-
if (runtimeDisposed) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
runtimeDisposed = true;
|
|
46
|
-
await dispatchDisposeAllApps();
|
|
47
|
-
const apps = disposeAllRuntimeApps();
|
|
48
|
-
apps.forEach(app => {
|
|
49
|
-
scheduleCancelByApp(app.name);
|
|
50
|
-
unregisterAppDir(app.name);
|
|
51
|
-
});
|
|
52
|
-
};
|
|
53
|
-
const shutdown = async (reason) => {
|
|
54
|
-
logger.info?.(`[alemonjs][${reason}] 收到信号,正在关闭...`);
|
|
55
|
-
await disposeRuntime();
|
|
56
|
-
process.exit(0);
|
|
57
|
-
};
|
|
58
|
-
const mainServer = () => {
|
|
59
|
-
const port = process.env.serverPort;
|
|
60
|
-
if (!port) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
createServer(port, () => {
|
|
64
|
-
const httpURL = `http://127.0.0.1:${port}`;
|
|
65
|
-
logger.info(`应用服务器: ${httpURL}`);
|
|
66
|
-
});
|
|
67
|
-
};
|
|
68
|
-
const main = () => {
|
|
69
|
-
const login = process.env.login ?? '';
|
|
70
|
-
const platform = process.env.platform ?? '';
|
|
71
|
-
const url = process.env.url ?? '';
|
|
72
|
-
const port = process.env.port ?? defaultPort;
|
|
73
|
-
const isFullReceive = process.env.is_full_receive === 'true' || process.env.is_full_receive === '1';
|
|
74
|
-
if (!login && !platform && url) {
|
|
75
|
-
logger.info(`[Connecting to CBP server at ${url}]`);
|
|
76
|
-
cbpClient(url);
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
const httpURL = `http://127.0.0.1:${port}`;
|
|
80
|
-
cbpClient(httpURL, { isFullReceive });
|
|
81
|
-
}
|
|
82
|
-
loadModels();
|
|
83
|
-
};
|
|
84
|
-
const mainProcess = () => {
|
|
85
|
-
process.on('unhandledRejection', (reason) => {
|
|
86
|
-
logger.error('[alemonjs][unhandledRejection] 未捕获的 Promise 拒绝:', reason);
|
|
87
|
-
});
|
|
88
|
-
process.on('uncaughtException', (error) => {
|
|
89
|
-
logger.error('[alemonjs][uncaughtException] 未捕获的异常:', error);
|
|
90
|
-
});
|
|
91
|
-
['SIGINT', 'SIGTERM', 'SIGQUIT', 'disconnect'].forEach(sig => {
|
|
92
|
-
process?.on?.(sig, () => {
|
|
93
|
-
void shutdown(sig);
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
process?.on?.('exit', code => {
|
|
97
|
-
void disposeRuntime();
|
|
98
|
-
logger.info?.(`[alemonjs][exit] 进程退出,code=${code}`);
|
|
99
|
-
});
|
|
100
|
-
process.on('message', msg => {
|
|
101
|
-
try {
|
|
102
|
-
const data = typeof msg === 'string' ? JSON.parse(msg) : msg;
|
|
103
|
-
if (data?.type === 'start') {
|
|
104
|
-
main();
|
|
105
|
-
mainServer();
|
|
106
|
-
}
|
|
107
|
-
else if (data?.type === 'stop') {
|
|
108
|
-
void shutdown('stop');
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
catch { }
|
|
112
|
-
});
|
|
113
|
-
if (process.send) {
|
|
114
|
-
process.send({ type: 'ready' });
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
mainProcess();
|
|
1
|
+
import './application/runtime/client-runtime.js';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const USER_AGENT_HEADER = "user-agent";
|
|
2
|
+
export declare const USER_AGENT_HEADER_VALUE_MAP: {
|
|
3
|
+
readonly platform: "platform";
|
|
4
|
+
readonly client: "client";
|
|
5
|
+
readonly testone: "testone";
|
|
6
|
+
};
|
|
7
|
+
export declare const DEVICE_ID_HEADER = "x-device-id";
|
|
8
|
+
export declare const FULL_RECEIVE_HEADER = "x-full-receive";
|
|
9
|
+
export declare const timeoutTime: number;
|
|
10
|
+
export declare const reconnectInterval: number;
|
|
11
|
+
export declare const HEARTBEAT_INTERVAL: number;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const USER_AGENT_HEADER = 'user-agent';
|
|
2
|
+
const USER_AGENT_HEADER_VALUE_MAP = {
|
|
3
|
+
platform: 'platform',
|
|
4
|
+
client: 'client',
|
|
5
|
+
testone: 'testone'
|
|
6
|
+
};
|
|
7
|
+
const DEVICE_ID_HEADER = 'x-device-id';
|
|
8
|
+
const FULL_RECEIVE_HEADER = 'x-full-receive';
|
|
9
|
+
const timeoutTime = 1000 * 60 * 3;
|
|
10
|
+
const reconnectInterval = 1000 * 6;
|
|
11
|
+
const HEARTBEAT_INTERVAL = 1000 * 18;
|
|
12
|
+
|
|
13
|
+
export { DEVICE_ID_HEADER, FULL_RECEIVE_HEADER, HEARTBEAT_INTERVAL, USER_AGENT_HEADER, USER_AGENT_HEADER_VALUE_MAP, reconnectInterval, timeoutTime };
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import '
|
|
3
|
-
import '
|
|
4
|
-
import 'yaml';
|
|
5
|
-
import '../../core/utils.js';
|
|
6
|
-
import { HEARTBEAT_INTERVAL } from '../processor/config.js';
|
|
1
|
+
import { logger } from '../logger.js';
|
|
2
|
+
import { ResultCode } from '../variable.js';
|
|
3
|
+
import { HEARTBEAT_INTERVAL } from './constants.js';
|
|
7
4
|
|
|
8
5
|
const useHeartbeat = ({ ping, isConnected, terminate }) => {
|
|
9
6
|
let heartbeatTimer = null;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Actions, Apis } from '
|
|
2
|
-
import type { Result } from '../
|
|
1
|
+
import type { Actions, Apis } from '../../types/index.js';
|
|
2
|
+
import type { Result } from '../result.js';
|
|
3
3
|
import type { CBPEnvelope, CBPError, CBPResult, NormalizedActionRequestMessage, NormalizedApiRequestMessage, NormalizedCBPMessage } from './typings';
|
|
4
4
|
export declare const isCBPEnvelope: (input: unknown) => input is CBPEnvelope;
|
|
5
5
|
export declare const normalizeInboundMessage: (input: unknown) => NormalizedCBPMessage | null;
|
|
@@ -304,11 +304,7 @@ const createEventEnvelope = (data) => {
|
|
|
304
304
|
protocol: 'cbp',
|
|
305
305
|
version: 1,
|
|
306
306
|
type: 'event',
|
|
307
|
-
id: typeof data.MessageId === 'string'
|
|
308
|
-
? data.MessageId
|
|
309
|
-
: typeof deviceId === 'string'
|
|
310
|
-
? `${deviceId}:${now()}`
|
|
311
|
-
: `${eventName}:${now()}`,
|
|
307
|
+
id: typeof data.MessageId === 'string' ? data.MessageId : typeof deviceId === 'string' ? `${deviceId}:${now()}` : `${eventName}:${now()}`,
|
|
312
308
|
timestamp: typeof data.CreateAt === 'number' ? data.CreateAt : now(),
|
|
313
309
|
source: {
|
|
314
310
|
role: 'platform',
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
const deviceId = randomUUID();
|
|
4
|
+
let idCounter = 0;
|
|
5
|
+
const idPrefix = process.pid.toString(36) + Date.now().toString(36);
|
|
6
|
+
const generateUniqueId = () => {
|
|
7
|
+
return idPrefix + (++idCounter).toString(36);
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export { deviceId, generateUniqueId };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { EventKeys, Apis, Actions } from '
|
|
2
|
-
import type { Result } from '../
|
|
1
|
+
import type { EventKeys, Apis, Actions } from '../../types/index.js';
|
|
2
|
+
import type { Result } from '../index.js';
|
|
3
3
|
export type ParsedMessage = {
|
|
4
4
|
apiId?: string;
|
|
5
5
|
actionId?: string;
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import { WebSocket } from 'ws';
|
|
2
|
-
import {
|
|
3
|
-
import '
|
|
4
|
-
import '
|
|
5
|
-
import '
|
|
6
|
-
import '
|
|
7
|
-
import { deviceId, DEVICE_ID_HEADER, USER_AGENT_HEADER, reconnectInterval } from '../processor/config.js';
|
|
8
|
-
import { useHeartbeat } from './connect.js';
|
|
2
|
+
import { logger } from '../logger.js';
|
|
3
|
+
import { ResultCode } from '../variable.js';
|
|
4
|
+
import { DEVICE_ID_HEADER, USER_AGENT_HEADER, reconnectInterval } from './constants.js';
|
|
5
|
+
import { deviceId } from './runtime.js';
|
|
6
|
+
import { useHeartbeat } from './heartbeat.js';
|
|
9
7
|
|
|
10
8
|
const createWSConnector = (options) => {
|
|
11
9
|
const { url, role, onOpen, onMessage, extraHeaders = {}, globalKey } = options;
|
|
12
10
|
if (global[globalKey]) {
|
|
13
|
-
|
|
11
|
+
Reflect.deleteProperty(global, globalKey);
|
|
14
12
|
}
|
|
15
13
|
const [heartbeatControl] = useHeartbeat({
|
|
16
14
|
ping: () => {
|
|
@@ -57,7 +55,7 @@ const createWSConnector = (options) => {
|
|
|
57
55
|
message: `${role} 连接关闭,尝试重新连接...`,
|
|
58
56
|
data: code
|
|
59
57
|
});
|
|
60
|
-
|
|
58
|
+
Reflect.deleteProperty(global, globalKey);
|
|
61
59
|
setTimeout(() => {
|
|
62
60
|
start();
|
|
63
61
|
}, reconnectInterval);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Package } from '../types';
|
|
1
|
+
import type { Package } from '../types/index.js';
|
|
2
2
|
type ConfigValue = {
|
|
3
3
|
[key: string]: any;
|
|
4
4
|
apps?: string[] | {
|
|
@@ -55,4 +55,4 @@ declare class ConfigCore<T extends ConfigValue = ConfigValue> {
|
|
|
55
55
|
export declare const getConfig: <T extends ConfigValue = ConfigValue>() => ConfigCore<T>;
|
|
56
56
|
export declare const getConfigValue: <T extends ConfigValue = ConfigValue>() => T;
|
|
57
57
|
export declare const onWatchConfigValue: <T extends ConfigValue = ConfigValue>(callback: ConfigListener<T>) => (() => void);
|
|
58
|
-
export {};
|
|
58
|
+
export type { ConfigCore, ConfigListener, ConfigValue };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { readFileSync, existsSync, watch, mkdirSync, writeFileSync } from 'fs';
|
|
2
2
|
import { join, dirname } from 'path';
|
|
3
3
|
import YAML from 'yaml';
|
|
4
|
+
import { logger } from './logger.js';
|
|
4
5
|
import { ResultCode } from './variable.js';
|
|
5
6
|
|
|
6
7
|
class ConfigCore {
|
|
@@ -13,6 +14,7 @@ class ConfigCore {
|
|
|
13
14
|
#argvProxy = null;
|
|
14
15
|
#resolvedDir = null;
|
|
15
16
|
#initValue = {};
|
|
17
|
+
#package = null;
|
|
16
18
|
constructor(dir) {
|
|
17
19
|
this.#resolvedDir = join(process.cwd(), dir);
|
|
18
20
|
}
|
|
@@ -142,7 +144,6 @@ class ConfigCore {
|
|
|
142
144
|
}
|
|
143
145
|
this.#listeners.clear();
|
|
144
146
|
}
|
|
145
|
-
#package = null;
|
|
146
147
|
get package() {
|
|
147
148
|
if (this.#package) {
|
|
148
149
|
return this.#package;
|
|
@@ -179,9 +180,9 @@ class ConfigCore {
|
|
|
179
180
|
if (typeof key === 'symbol') {
|
|
180
181
|
return undefined;
|
|
181
182
|
}
|
|
182
|
-
const
|
|
183
|
-
if (
|
|
184
|
-
return process.argv[
|
|
183
|
+
const index0 = process.argv.indexOf(key);
|
|
184
|
+
if (index0 !== -1) {
|
|
185
|
+
return process.argv[index0 + 1];
|
|
185
186
|
}
|
|
186
187
|
const index = process.argv.indexOf(`--${key}`);
|
|
187
188
|
if (index !== -1) {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const matchIn: (source: any, key: string) => boolean;
|
|
2
|
+
export declare const fastHash: (str: string) => string;
|
|
3
|
+
export declare const createUserHashKey: (event: {
|
|
4
|
+
UserId: string;
|
|
5
|
+
Platform: string;
|
|
6
|
+
}) => string;
|
|
7
|
+
export declare const useUserHashKey: (event: {
|
|
8
|
+
UserId: string;
|
|
9
|
+
Platform: string;
|
|
10
|
+
}) => string;
|
|
11
|
+
export declare const isMaster: (UserId: string, platform: string) => boolean;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { getConfigValue } from './config.js';
|
|
2
|
+
|
|
3
|
+
const matchIn = (source, key) => {
|
|
4
|
+
if (Array.isArray(source)) {
|
|
5
|
+
return source.includes(key);
|
|
6
|
+
}
|
|
7
|
+
if (source && typeof source === 'object') {
|
|
8
|
+
return Object.prototype.hasOwnProperty.call(source, key) && !!source[key];
|
|
9
|
+
}
|
|
10
|
+
return false;
|
|
11
|
+
};
|
|
12
|
+
const fastHash = (str) => {
|
|
13
|
+
let hash = 0x811c9dc5;
|
|
14
|
+
for (let i = 0; i < str.length; i++) {
|
|
15
|
+
hash ^= str.charCodeAt(i);
|
|
16
|
+
hash = Math.imul(hash, 0x01000193);
|
|
17
|
+
}
|
|
18
|
+
return (hash >>> 0).toString(36);
|
|
19
|
+
};
|
|
20
|
+
const createUserHashKey = (event) => {
|
|
21
|
+
return fastHash(`${event.Platform}:${event.UserId}`);
|
|
22
|
+
};
|
|
23
|
+
const useUserHashKey = createUserHashKey;
|
|
24
|
+
const isMaster = (UserId, platform) => {
|
|
25
|
+
const values = getConfigValue() || {};
|
|
26
|
+
const value = values[platform] && typeof values[platform] === 'object' ? values[platform] : {};
|
|
27
|
+
const UserKey = createUserHashKey({
|
|
28
|
+
Platform: platform,
|
|
29
|
+
UserId
|
|
30
|
+
});
|
|
31
|
+
return matchIn(values.master_key, UserKey) || matchIn(values.master_id, UserId) || matchIn(value.master_key, UserKey) || matchIn(value.master_id, UserId);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export { createUserHashKey, fastHash, isMaster, matchIn, useUserHashKey };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export * from './config.js';
|
|
2
|
+
export { processorRepeatedEventTime, processorRepeatedUserTime, processorRepeatedClearTimeMin, processorRepeatedClearTimeMax, processorRepeatedClearSize, processorMaxMapSize, fileSuffixMiddleware, fileSuffixResponse, filePrefixCommon, defaultPort, defaultLogin, defaultPlatformPrefix, defaultPlatformCommonPrefix, Ok, Warn, Fail, FailParams, FailAuth, FailInternal, EventMessageText, ResultCode } from './variable.js';
|
|
3
|
+
export * from './logger.js';
|
|
4
|
+
export * from './identity.js';
|
|
5
|
+
export * from './result.js';
|
|
6
|
+
export * from './SinglyLinkedList.js';
|
|
7
|
+
export * from './utils.js';
|
|
8
|
+
export * from './direct-channel.js';
|
|
9
|
+
export * from './react.js';
|
|
10
|
+
export * from './cbp/constants.js';
|
|
11
|
+
export * from './cbp/runtime.js';
|
|
12
|
+
export * from './cbp/heartbeat.js';
|
|
13
|
+
export * from './cbp/ws-connector.js';
|
|
14
|
+
export * from './cbp/normalize.js';
|
|
15
|
+
export * from './cbp/typings.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { getConfig, getConfigValue, onWatchConfigValue } from './config.js';
|
|
2
|
+
export { EventMessageText, Fail, FailAuth, FailInternal, FailParams, Ok, ResultCode, Warn, defaultLogin, defaultPlatformCommonPrefix, defaultPlatformPrefix, defaultPort, filePrefixCommon, fileSuffixMiddleware, fileSuffixResponse, processorMaxMapSize, processorRepeatedClearSize, processorRepeatedClearTimeMax, processorRepeatedClearTimeMin, processorRepeatedEventTime, processorRepeatedUserTime } from './variable.js';
|
|
3
|
+
export { Logger, logger } from './logger.js';
|
|
4
|
+
export { createUserHashKey, fastHash, isMaster, matchIn, useUserHashKey } from './identity.js';
|
|
5
|
+
export { createResult } from './result.js';
|
|
6
|
+
export { SinglyLinkedList } from './SinglyLinkedList.js';
|
|
7
|
+
export { createEventName, createHash, getCachedRegExp, getInputExportPath, getRecursiveDirFiles, sanitizeForSerialization, showErrorModule, stringToNumber } from './utils.js';
|
|
8
|
+
export { createDirectClient, createDirectServer, generateSocketPath } from './direct-channel.js';
|
|
9
|
+
export { A, Body, Component, DOCTYPE, Div, H1, Head, Html, P, Style, Title, createElement, renderToString } from './react.js';
|
|
10
|
+
export { DEVICE_ID_HEADER, FULL_RECEIVE_HEADER, HEARTBEAT_INTERVAL, USER_AGENT_HEADER, USER_AGENT_HEADER_VALUE_MAP, reconnectInterval, timeoutTime } from './cbp/constants.js';
|
|
11
|
+
export { deviceId, generateUniqueId } from './cbp/runtime.js';
|
|
12
|
+
export { useHeartbeat } from './cbp/heartbeat.js';
|
|
13
|
+
export { createWSConnector } from './cbp/ws-connector.js';
|
|
14
|
+
export { createActionRequestEnvelope, createActionResponseEnvelope, createApiRequestEnvelope, createApiResponseEnvelope, createEventEnvelope, getNormalizedDeviceId, getNormalizedEventRouteId, isCBPEnvelope, isNormalizedActionRequest, isNormalizedApiRequest, normalizeInboundMessage, toLegacyActionData, toLegacyApiData } from './cbp/normalize.js';
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { mkdirSync } from 'node:fs';
|
|
2
|
+
import log4js from 'log4js';
|
|
3
|
+
|
|
4
|
+
const createLogger = () => {
|
|
5
|
+
if (process.env.BROWSER_ENV === 'browser') {
|
|
6
|
+
return {
|
|
7
|
+
trace: console.trace.bind(console),
|
|
8
|
+
debug: console.debug.bind(console),
|
|
9
|
+
info: console.info.bind(console),
|
|
10
|
+
mark: console.info.bind(console),
|
|
11
|
+
warn: console.warn.bind(console),
|
|
12
|
+
error: console.error.bind(console),
|
|
13
|
+
fatal: console.error.bind(console)
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
const logDir = process.env?.LOG_PATH ?? `./logs/${process.env.LOG_NAME ?? ''}`;
|
|
17
|
+
mkdirSync(logDir, { recursive: true });
|
|
18
|
+
const level = process.env.NODE_ENV === 'development' ? 'trace' : 'info';
|
|
19
|
+
const hideTime = process.env.LOGGER_TIME === 'false';
|
|
20
|
+
const hideLevel = process.env.LOGGER_LEVEL === 'false';
|
|
21
|
+
let pattern = '';
|
|
22
|
+
if (hideTime && hideLevel) {
|
|
23
|
+
pattern = '%m';
|
|
24
|
+
}
|
|
25
|
+
else if (hideTime && !hideLevel) {
|
|
26
|
+
pattern = '[%p] %m';
|
|
27
|
+
}
|
|
28
|
+
else if (!hideTime && hideLevel) {
|
|
29
|
+
pattern = '[%d{yyyy-MM-dd hh:mm:ss}] %m';
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
pattern = '[%d{yyyy-MM-dd hh:mm:ss}][%p] %m';
|
|
33
|
+
}
|
|
34
|
+
log4js.configure({
|
|
35
|
+
appenders: {
|
|
36
|
+
console: {
|
|
37
|
+
type: 'console',
|
|
38
|
+
layout: {
|
|
39
|
+
type: 'pattern',
|
|
40
|
+
pattern
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
command: {
|
|
44
|
+
type: 'dateFile',
|
|
45
|
+
filename: `${logDir}/command`,
|
|
46
|
+
pattern: 'yyyy-MM-dd.log',
|
|
47
|
+
numBackups: 15,
|
|
48
|
+
alwaysIncludePattern: true,
|
|
49
|
+
layout: {
|
|
50
|
+
type: 'pattern',
|
|
51
|
+
pattern
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
error: {
|
|
55
|
+
type: 'dateFile',
|
|
56
|
+
filename: `${logDir}/error`,
|
|
57
|
+
pattern: 'yyyy-MM-dd.log',
|
|
58
|
+
numBackups: 15,
|
|
59
|
+
alwaysIncludePattern: true,
|
|
60
|
+
layout: {
|
|
61
|
+
type: 'pattern',
|
|
62
|
+
pattern
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
categories: {
|
|
67
|
+
default: { appenders: ['console'], level },
|
|
68
|
+
command: { appenders: ['console', 'command'], level: 'info' },
|
|
69
|
+
error: { appenders: ['console', 'command', 'error'], level: 'warn' }
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
const defaultLogger = log4js.getLogger('default');
|
|
73
|
+
const commandLogger = log4js.getLogger('command');
|
|
74
|
+
const errorLogger = log4js.getLogger('error');
|
|
75
|
+
return {
|
|
76
|
+
trace: defaultLogger.trace.bind(defaultLogger),
|
|
77
|
+
debug: defaultLogger.debug.bind(defaultLogger),
|
|
78
|
+
info: commandLogger.info.bind(commandLogger),
|
|
79
|
+
mark: commandLogger.mark.bind(commandLogger),
|
|
80
|
+
warn: errorLogger.warn.bind(errorLogger),
|
|
81
|
+
error: errorLogger.error.bind(errorLogger),
|
|
82
|
+
fatal: errorLogger.fatal.bind(errorLogger)
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
class Logger {
|
|
86
|
+
#logger = null;
|
|
87
|
+
constructor() {
|
|
88
|
+
this.#logger = createLogger();
|
|
89
|
+
if (!global.logger) {
|
|
90
|
+
global.logger = this.#logger;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
get value() {
|
|
94
|
+
return this.#logger;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const logger = new Logger().value;
|
|
98
|
+
|
|
99
|
+
export { Logger, logger };
|