alemonjs 2.1.82 → 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.
Files changed (235) hide show
  1. package/lib/{app → application}/define-children.js +2 -1
  2. package/lib/{app → application/format}/message-api.d.ts +2 -2
  3. package/lib/{app → application/format}/message-api.js +12 -4
  4. package/lib/{app → application/format}/message-format-old.d.ts +1 -1
  5. package/lib/{app → application/format}/message-format.d.ts +1 -2
  6. package/lib/{app → application/format}/message-format.js +0 -2
  7. package/lib/{app/hook-use → application/hooks}/announce.js +4 -3
  8. package/lib/{app/hook-use → application/hooks}/channel.js +4 -3
  9. package/lib/{app/hook-use → application/hooks}/client.d.ts +1 -1
  10. package/lib/{app/hook-use → application/hooks}/client.js +4 -11
  11. package/lib/{app/hook-use → application/hooks}/common.d.ts +7 -7
  12. package/lib/{app/hook-use → application/hooks}/common.js +9 -8
  13. package/lib/{app/hook-use → application/hooks}/event.js +1 -1
  14. package/lib/{app/hook-use → application/hooks}/guild.js +4 -3
  15. package/lib/{app/hook-use → application/hooks}/history.js +4 -3
  16. package/lib/{app/hook-use → application/hooks}/me.js +12 -7
  17. package/lib/{app/hook-use → application/hooks}/media.d.ts +3 -3
  18. package/lib/{app/hook-use → application/hooks}/media.js +4 -3
  19. package/lib/{app/hook-use → application/hooks}/member.js +4 -3
  20. package/lib/{app/hook-use → application/hooks}/mention.js +4 -3
  21. package/lib/{app/hook-use → application/hooks}/message.js +6 -5
  22. package/lib/{app/hook-use → application/hooks}/permission.js +4 -3
  23. package/lib/{app/hook-use → application/hooks}/reaction.js +4 -3
  24. package/lib/{app/hook-use → application/hooks}/request.js +12 -7
  25. package/lib/{app/hook-use → application/hooks}/role.js +4 -3
  26. package/lib/{app/hook-use → application/hooks}/subscribe.js +6 -4
  27. package/lib/{app/hook-use → application/hooks}/user.js +12 -7
  28. package/lib/application/index.d.ts +12 -0
  29. package/lib/application/index.js +32 -0
  30. package/lib/{app → application}/router/dsl.d.ts +1 -1
  31. package/lib/{app → application}/router/dsl.js +13 -9
  32. package/lib/{cbp → application/runtime/cbp}/connects/client.d.ts +1 -1
  33. package/lib/{cbp → application/runtime/cbp}/connects/client.js +43 -50
  34. package/lib/application/runtime/cbp/index.d.ts +3 -0
  35. package/lib/application/runtime/cbp/index.js +3 -0
  36. package/lib/application/runtime/cbp/processor/actions.d.ts +3 -0
  37. package/lib/application/runtime/cbp/processor/actions.js +58 -0
  38. package/lib/application/runtime/cbp/processor/api.d.ts +3 -0
  39. package/lib/application/runtime/cbp/processor/api.js +58 -0
  40. package/lib/application/runtime/cbp/processor/request-registry.d.ts +8 -0
  41. package/lib/application/runtime/cbp/processor/request-registry.js +6 -0
  42. package/lib/application/runtime/client-runtime.d.ts +1 -0
  43. package/lib/application/runtime/client-runtime.js +95 -0
  44. package/lib/application/runtime/event-error.d.ts +2 -0
  45. package/lib/application/runtime/event-error.js +20 -0
  46. package/lib/{app → application/runtime}/event-group.d.ts +1 -1
  47. package/lib/{app → application/runtime}/event-middleware.d.ts +1 -1
  48. package/lib/{app → application/runtime}/event-middleware.js +1 -1
  49. package/lib/application/runtime/event-processor-callHandler.d.ts +5 -0
  50. package/lib/{app → application/runtime}/event-processor-callHandler.js +26 -3
  51. package/lib/{app → application/runtime}/event-processor-cycle.d.ts +1 -1
  52. package/lib/{app → application/runtime}/event-processor-cycleFiles.d.ts +5 -2
  53. package/lib/{app → application/runtime}/event-processor-cycleFiles.js +23 -8
  54. package/lib/application/runtime/event-processor-cycleRoute.d.ts +5 -0
  55. package/lib/{app → application/runtime}/event-processor-cycleRoute.js +25 -4
  56. package/lib/{app → application/runtime}/event-processor-event.d.ts +1 -1
  57. package/lib/{app → application/runtime}/event-processor-event.js +2 -2
  58. package/lib/{app → application/runtime}/event-processor-middleware.d.ts +1 -1
  59. package/lib/{app → application/runtime}/event-processor-middleware.js +2 -2
  60. package/lib/{app → application/runtime}/event-processor-subscribe.d.ts +1 -1
  61. package/lib/{app → application/runtime}/event-processor-subscribe.js +40 -5
  62. package/lib/{app → application/runtime}/event-processor.d.ts +1 -1
  63. package/lib/{app → application/runtime}/event-processor.js +10 -4
  64. package/lib/{app → application/runtime}/event-response.d.ts +1 -1
  65. package/lib/{app → application/runtime}/event-utils.d.ts +1 -1
  66. package/lib/{app → application/runtime}/event-utils.js +2 -2
  67. package/lib/{app → application/runtime}/hook-event-context.d.ts +8 -4
  68. package/lib/{app → application/runtime}/hook-event-context.js +26 -7
  69. package/lib/application/runtime/http/index.d.ts +4 -0
  70. package/lib/application/runtime/http/index.js +4 -0
  71. package/lib/{server → application/runtime/http}/routers/hello.html.js +1 -1
  72. package/lib/{server → application/runtime/http}/routers/middleware.js +1 -2
  73. package/lib/{server → application/runtime/http}/routers/router.js +121 -20
  74. package/lib/{server → application/runtime/http}/routers/utils.d.ts +1 -1
  75. package/lib/{server → application/runtime/http}/routers/utils.js +6 -6
  76. package/lib/{server/main.js → application/runtime/http-server.js} +2 -6
  77. package/lib/application/runtime/lifecycle-callbacks.d.ts +14 -0
  78. package/lib/application/runtime/lifecycle-callbacks.js +94 -0
  79. package/lib/{app/load_modules → application/runtime/load-modules}/load.js +2 -2
  80. package/lib/{app/load_modules → application/runtime/load-modules}/loadChild.js +16 -13
  81. package/lib/{app → application/runtime}/schedule-store.d.ts +1 -1
  82. package/lib/{app → application/runtime}/schedule-store.js +1 -1
  83. package/lib/{app → application/runtime}/store.d.ts +14 -17
  84. package/lib/{app → application/runtime}/store.js +38 -107
  85. package/lib/application/schedule.d.ts +17 -0
  86. package/lib/{app/api → application}/schedule.js +1 -1
  87. package/lib/client.d.ts +1 -1
  88. package/lib/client.js +1 -113
  89. package/lib/common/cbp/constants.d.ts +11 -0
  90. package/lib/common/cbp/constants.js +13 -0
  91. package/lib/{cbp/connects/connect.js → common/cbp/heartbeat.js} +3 -6
  92. package/lib/common/cbp/normalize.d.ts +16 -0
  93. package/lib/common/cbp/normalize.js +324 -0
  94. package/lib/common/cbp/runtime.d.ts +2 -0
  95. package/lib/common/cbp/runtime.js +10 -0
  96. package/lib/common/cbp/typings.d.ts +159 -0
  97. package/lib/{cbp/connects/base.js → common/cbp/ws-connector.js} +7 -9
  98. package/lib/{core → common}/config.d.ts +5 -5
  99. package/lib/{core → common}/config.js +5 -4
  100. package/lib/common/identity.d.ts +11 -0
  101. package/lib/common/identity.js +34 -0
  102. package/lib/common/index.d.ts +15 -0
  103. package/lib/common/index.js +14 -0
  104. package/lib/common/logger.d.ts +6 -0
  105. package/lib/common/logger.js +99 -0
  106. package/lib/common/result.d.ts +7 -0
  107. package/lib/common/result.js +19 -0
  108. package/lib/{core → common}/utils.d.ts +3 -18
  109. package/lib/{core → common}/utils.js +6 -49
  110. package/lib/{core → common}/variable.js +1 -1
  111. package/lib/core/cbp/index.d.ts +1 -0
  112. package/lib/core/cbp/index.js +1 -0
  113. package/lib/core/cbp/processor/config.d.ts +8 -0
  114. package/lib/core/cbp/processor/config.js +29 -0
  115. package/lib/{cbp → core/cbp}/routers/hello.html.js +1 -1
  116. package/lib/{cbp → core/cbp}/server/main.js +37 -33
  117. package/lib/{cbp → core/cbp}/server/testone.d.ts +1 -1
  118. package/lib/{cbp → core/cbp}/server/testone.js +8 -2
  119. package/lib/core/index.d.ts +1 -3
  120. package/lib/core/index.js +1 -3
  121. package/lib/core/process/index.d.ts +3 -0
  122. package/lib/{process → core/process}/index.js +0 -1
  123. package/lib/{process → core/process}/ipc-bridge.js +6 -1
  124. package/lib/{process → core/process}/module.js +13 -4
  125. package/lib/{process → core/process}/platform.js +12 -3
  126. package/lib/core/start.d.ts +2 -0
  127. package/lib/core/start.js +76 -0
  128. package/lib/global.d.ts +4 -3
  129. package/lib/index.d.ts +5 -4
  130. package/lib/index.js +9 -54
  131. package/lib/main.d.ts +1 -2
  132. package/lib/main.js +1 -76
  133. package/lib/{cbp/connects/platform.d.ts → platform/cbp-platform.d.ts} +2 -2
  134. package/lib/{cbp/connects/platform.js → platform/cbp-platform.js} +52 -75
  135. package/lib/{app → platform}/define-platform.js +2 -0
  136. package/lib/platform/event-value.d.ts +7 -0
  137. package/lib/platform/event-value.js +5 -0
  138. package/lib/platform/index.d.ts +5 -0
  139. package/lib/platform/index.js +5 -0
  140. package/lib/types/actions.d.ts +20 -1
  141. package/lib/types/apis.d.ts +2 -1
  142. package/lib/types/client/index.d.ts +1 -1
  143. package/lib/types/cycle/index.d.ts +50 -0
  144. package/lib/types/event/index.d.ts +2 -1
  145. package/lib/types/subscribe/index.d.ts +2 -1
  146. package/package.json +22 -1
  147. package/lib/app/api/schedule.d.ts +0 -17
  148. package/lib/app/event-processor-callHandler.d.ts +0 -1
  149. package/lib/app/event-processor-cycleRoute.d.ts +0 -2
  150. package/lib/app/index.d.ts +0 -25
  151. package/lib/app/index.js +0 -47
  152. package/lib/cbp/index.d.ts +0 -3
  153. package/lib/cbp/index.js +0 -3
  154. package/lib/cbp/processor/actions.d.ts +0 -3
  155. package/lib/cbp/processor/actions.js +0 -48
  156. package/lib/cbp/processor/api.d.ts +0 -3
  157. package/lib/cbp/processor/api.js +0 -48
  158. package/lib/cbp/processor/config.d.ts +0 -29
  159. package/lib/cbp/processor/config.js +0 -52
  160. package/lib/cbp/typings.d.ts +0 -20
  161. package/lib/process/index.d.ts +0 -4
  162. /package/lib/{app → application}/define-children.d.ts +0 -0
  163. /package/lib/{app → application}/define-middleware.d.ts +0 -0
  164. /package/lib/{app → application}/define-middleware.js +0 -0
  165. /package/lib/{app → application}/define-response.d.ts +0 -0
  166. /package/lib/{app → application}/define-response.js +0 -0
  167. /package/lib/{app → application}/define-router.d.ts +0 -0
  168. /package/lib/{app → application}/define-router.js +0 -0
  169. /package/lib/{app → application}/expose.d.ts +0 -0
  170. /package/lib/{app → application}/expose.js +0 -0
  171. /package/lib/{app → application/format}/message-format-old.js +0 -0
  172. /package/lib/{app/hook-use → application/hooks}/announce.d.ts +0 -0
  173. /package/lib/{app/hook-use → application/hooks}/channel.d.ts +0 -0
  174. /package/lib/{app/hook-use → application/hooks}/event.d.ts +0 -0
  175. /package/lib/{app/hook-use → application/hooks}/guild.d.ts +0 -0
  176. /package/lib/{app/hook-use → application/hooks}/history.d.ts +0 -0
  177. /package/lib/{app/hook-use → application/hooks}/index.d.ts +0 -0
  178. /package/lib/{app/hook-use → application/hooks}/index.js +0 -0
  179. /package/lib/{app/hook-use → application/hooks}/me.d.ts +0 -0
  180. /package/lib/{app/hook-use → application/hooks}/member.d.ts +0 -0
  181. /package/lib/{app/hook-use → application/hooks}/mention.d.ts +0 -0
  182. /package/lib/{app/hook-use → application/hooks}/message.d.ts +0 -0
  183. /package/lib/{app/hook-use → application/hooks}/permission.d.ts +0 -0
  184. /package/lib/{app/hook-use → application/hooks}/reaction.d.ts +0 -0
  185. /package/lib/{app/hook-use → application/hooks}/request.d.ts +0 -0
  186. /package/lib/{app/hook-use → application/hooks}/role.d.ts +0 -0
  187. /package/lib/{app/hook-use → application/hooks}/route.d.ts +0 -0
  188. /package/lib/{app/hook-use → application/hooks}/route.js +0 -0
  189. /package/lib/{app/hook-use → application/hooks}/subscribe.d.ts +0 -0
  190. /package/lib/{app/hook-use → application/hooks}/user.d.ts +0 -0
  191. /package/lib/{app → application}/router/fallback.d.ts +0 -0
  192. /package/lib/{app → application}/router/fallback.js +0 -0
  193. /package/lib/{app → application}/router/main.d.ts +0 -0
  194. /package/lib/{app → application}/router/main.js +0 -0
  195. /package/lib/{app → application}/router/parser.d.ts +0 -0
  196. /package/lib/{app → application}/router/parser.js +0 -0
  197. /package/lib/{app → application}/router/types.d.ts +0 -0
  198. /package/lib/{app → application}/router/types.js +0 -0
  199. /package/lib/{app → application}/router/validator.d.ts +0 -0
  200. /package/lib/{app → application}/router/validator.js +0 -0
  201. /package/lib/{cbp → application/runtime/cbp}/processor/transport.d.ts +0 -0
  202. /package/lib/{cbp → application/runtime/cbp}/processor/transport.js +0 -0
  203. /package/lib/{app → application/runtime}/event-group.js +0 -0
  204. /package/lib/{app → application/runtime}/event-processor-cycle.js +0 -0
  205. /package/lib/{app → application/runtime}/event-response.js +0 -0
  206. /package/lib/{cbp → application/runtime/http}/routers/hello.html.d.ts +0 -0
  207. /package/lib/{server → application/runtime/http}/routers/middleware.d.ts +0 -0
  208. /package/lib/{cbp → application/runtime/http}/routers/router.d.ts +0 -0
  209. /package/lib/{server/main.d.ts → application/runtime/http-server.d.ts} +0 -0
  210. /package/lib/{app/load_modules → application/runtime/load-modules}/index.d.ts +0 -0
  211. /package/lib/{app/load_modules → application/runtime/load-modules}/index.js +0 -0
  212. /package/lib/{app/load_modules → application/runtime/load-modules}/load.d.ts +0 -0
  213. /package/lib/{app/load_modules → application/runtime/load-modules}/loadChild.d.ts +0 -0
  214. /package/lib/{app/config.d.ts → application/runtime/subscribe-status.d.ts} +0 -0
  215. /package/lib/{app/config.js → application/runtime/subscribe-status.js} +0 -0
  216. /package/lib/{app → common}/SinglyLinkedList.d.ts +0 -0
  217. /package/lib/{app → common}/SinglyLinkedList.js +0 -0
  218. /package/lib/{cbp/connects/connect.d.ts → common/cbp/heartbeat.d.ts} +0 -0
  219. /package/lib/{cbp → common/cbp}/typings.js +0 -0
  220. /package/lib/{cbp/connects/base.d.ts → common/cbp/ws-connector.d.ts} +0 -0
  221. /package/lib/{process → common}/direct-channel.d.ts +0 -0
  222. /package/lib/{process → common}/direct-channel.js +0 -0
  223. /package/lib/{core → common}/react.d.ts +0 -0
  224. /package/lib/{core → common}/react.js +0 -0
  225. /package/lib/{core → common}/variable.d.ts +0 -0
  226. /package/lib/{server → core/cbp}/routers/hello.html.d.ts +0 -0
  227. /package/lib/{server → core/cbp}/routers/router.d.ts +0 -0
  228. /package/lib/{cbp → core/cbp}/routers/router.js +0 -0
  229. /package/lib/{cbp → core/cbp}/server/main.d.ts +0 -0
  230. /package/lib/{process → core/process}/ipc-bridge.d.ts +0 -0
  231. /package/lib/{process → core/process}/module.d.ts +0 -0
  232. /package/lib/{process → core/process}/platform.d.ts +0 -0
  233. /package/lib/{app → platform}/define-platform.d.ts +0 -0
  234. /package/lib/{app → platform}/event-format.d.ts +0 -0
  235. /package/lib/{app → platform}/event-format.js +0 -0
@@ -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,6 @@
1
+ export declare class Logger {
2
+ #private;
3
+ constructor();
4
+ get value(): any;
5
+ }
6
+ export declare const logger: any;
@@ -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 };
@@ -0,0 +1,7 @@
1
+ import { ResultCode } from './variable.js';
2
+ export type Result<T = any> = {
3
+ code: ResultCode;
4
+ message: string | object;
5
+ data: T;
6
+ };
7
+ export declare const createResult: <T>(code: ResultCode, message: string | object, data?: T) => Result<T>;
@@ -0,0 +1,19 @@
1
+ import { logger } from './logger.js';
2
+ import { ResultCode } from './variable.js';
3
+
4
+ const createResult = (code, message, data) => {
5
+ if (code !== ResultCode.Ok) {
6
+ logger.error({
7
+ code,
8
+ message,
9
+ data
10
+ });
11
+ }
12
+ return {
13
+ code,
14
+ message,
15
+ data
16
+ };
17
+ };
18
+
19
+ export { createResult };
@@ -1,21 +1,12 @@
1
1
  import { Dirent } from 'fs';
2
- import { ResultCode } from './variable';
2
+ export { createUserHashKey, fastHash, isMaster, matchIn, useUserHashKey } from './identity.js';
3
+ export type { Result } from './result.js';
4
+ export { createResult } from './result.js';
3
5
  export declare const createHash: (str: string, options?: {
4
6
  length?: number;
5
7
  algorithm?: string;
6
8
  }) => string;
7
- export declare const fastHash: (str: string) => string;
8
9
  export declare const getCachedRegExp: (pattern: string | RegExp) => RegExp;
9
- export declare const createUserHashKey: (event: {
10
- UserId: string;
11
- Platform: string;
12
- }) => string;
13
- export declare const useUserHashKey: (event: {
14
- UserId: string;
15
- Platform: string;
16
- }) => string;
17
- export declare const matchIn: (source: any, key: string) => boolean;
18
- export declare const isMaster: (UserId: string, platform: string) => boolean;
19
10
  export declare const createEventName: (url: string, appKey: string) => string;
20
11
  export declare const stringToNumber: (str: string, size?: number) => number;
21
12
  export declare const getRecursiveDirFiles: (dir: string, condition?: (func: Dirent) => boolean) => {
@@ -25,9 +16,3 @@ export declare const getRecursiveDirFiles: (dir: string, condition?: (func: Dire
25
16
  export declare const showErrorModule: (e: Error) => void;
26
17
  export declare const sanitizeForSerialization: (data: any) => any;
27
18
  export declare const getInputExportPath: (input?: string) => any;
28
- export type Result<T = any> = {
29
- code: ResultCode;
30
- message: string | object;
31
- data: T;
32
- };
33
- export declare const createResult: <T>(code: ResultCode, message: string | object, data?: T) => Result<T>;
@@ -1,9 +1,11 @@
1
1
  import { createHash as createHash$1 } from 'node:crypto';
2
2
  import fs__default, { existsSync, readdirSync } from 'fs';
3
3
  import path__default, { join } from 'path';
4
- import { ResultCode, fileSuffixResponse } from './variable.js';
5
4
  import module$1 from 'module';
6
- import { getConfigValue } from './config.js';
5
+ import { ResultCode, fileSuffixResponse } from './variable.js';
6
+ import { logger } from './logger.js';
7
+ export { createUserHashKey, fastHash, isMaster, matchIn, useUserHashKey } from './identity.js';
8
+ export { createResult } from './result.js';
7
9
 
8
10
  const initRequire = () => { };
9
11
  initRequire.resolve = () => '';
@@ -13,14 +15,6 @@ const createHash = (str, options = {}) => {
13
15
  const hash = createHash$1(algorithm).update(str).digest('hex');
14
16
  return hash.slice(0, length);
15
17
  };
16
- const fastHash = (str) => {
17
- let hash = 0x811c9dc5;
18
- for (let i = 0; i < str.length; i++) {
19
- hash ^= str.charCodeAt(i);
20
- hash = Math.imul(hash, 0x01000193);
21
- }
22
- return (hash >>> 0).toString(36);
23
- };
24
18
  const _regexpCache = new Map();
25
19
  const getCachedRegExp = (pattern) => {
26
20
  if (pattern instanceof RegExp) {
@@ -33,28 +27,6 @@ const getCachedRegExp = (pattern) => {
33
27
  }
34
28
  return cached;
35
29
  };
36
- const createUserHashKey = (event) => {
37
- return fastHash(`${event.Platform}:${event.UserId}`);
38
- };
39
- const useUserHashKey = createUserHashKey;
40
- const matchIn = (source, key) => {
41
- if (Array.isArray(source)) {
42
- return source.includes(key);
43
- }
44
- if (source && typeof source === 'object') {
45
- return Object.prototype.hasOwnProperty.call(source, key) && !!source[key];
46
- }
47
- return false;
48
- };
49
- const isMaster = (UserId, platform) => {
50
- const values = getConfigValue() || {};
51
- const value = values[platform] && typeof values[platform] === 'object' ? values[platform] : {};
52
- const UserKey = createUserHashKey({
53
- Platform: platform,
54
- UserId: UserId
55
- });
56
- return matchIn(values.master_key, UserKey) || matchIn(values.master_id, UserId) || matchIn(value.master_key, UserKey) || matchIn(value.master_id, UserId);
57
- };
58
30
  const createEventName = (url, appKey) => {
59
31
  let uri = url;
60
32
  if (process.platform === 'win32') {
@@ -62,8 +34,7 @@ const createEventName = (url, appKey) => {
62
34
  }
63
35
  const names = uri.split('/').filter(item => item !== '');
64
36
  names.pop();
65
- const name = `${appKey}:${names.join(':')}`;
66
- return name;
37
+ return `${appKey}:${names.join(':')}`;
67
38
  };
68
39
  const stringToNumber = (str, size = 33) => {
69
40
  let hash = 5381;
@@ -140,19 +111,5 @@ const getInputExportPath = (input) => {
140
111
  }
141
112
  }
142
113
  };
143
- const createResult = (code, message, data) => {
144
- if (code !== ResultCode.Ok) {
145
- logger.error({
146
- code,
147
- message,
148
- data: data
149
- });
150
- }
151
- return {
152
- code,
153
- message,
154
- data: data
155
- };
156
- };
157
114
 
158
- export { createEventName, createHash, createResult, createUserHashKey, fastHash, getCachedRegExp, getInputExportPath, getRecursiveDirFiles, isMaster, matchIn, sanitizeForSerialization, showErrorModule, stringToNumber, useUserHashKey };
115
+ export { createEventName, createHash, getCachedRegExp, getInputExportPath, getRecursiveDirFiles, sanitizeForSerialization, showErrorModule, stringToNumber };
@@ -61,7 +61,7 @@ class Result {
61
61
  this.#data[this.#currentIndex] = {
62
62
  code: ResultCode.Ok,
63
63
  message: '',
64
- data: data
64
+ data
65
65
  };
66
66
  }
67
67
  this.#data[this.#currentIndex].data = data;
@@ -0,0 +1 @@
1
+ export { cbpServer } from './server/main.js';
@@ -0,0 +1 @@
1
+ export { cbpServer } from './server/main.js';
@@ -0,0 +1,8 @@
1
+ import { WebSocket } from 'ws';
2
+ export declare const childrenClient: Map<string, WebSocket>;
3
+ export declare const platformClient: Map<string, WebSocket>;
4
+ export declare const fullClient: Map<string, WebSocket>;
5
+ export declare const childrenBind: Map<string, string>;
6
+ export declare const clientBindCount: Map<string, number>;
7
+ export declare const bindChannelToClient: (channelId: string, clientId: string) => void;
8
+ export declare const unbindClient: (clientId: string) => void;
@@ -0,0 +1,29 @@
1
+ const childrenClient = new Map();
2
+ const platformClient = new Map();
3
+ const fullClient = new Map();
4
+ const childrenBind = new Map();
5
+ const clientBindCount = new Map();
6
+ const bindChannelToClient = (channelId, clientId) => {
7
+ const oldClientId = childrenBind.get(channelId);
8
+ if (oldClientId && oldClientId !== clientId) {
9
+ const oldCount = clientBindCount.get(oldClientId) ?? 0;
10
+ if (oldCount > 1) {
11
+ clientBindCount.set(oldClientId, oldCount - 1);
12
+ }
13
+ else {
14
+ clientBindCount.delete(oldClientId);
15
+ }
16
+ }
17
+ childrenBind.set(channelId, clientId);
18
+ clientBindCount.set(clientId, (clientBindCount.get(clientId) ?? 0) + 1);
19
+ };
20
+ const unbindClient = (clientId) => {
21
+ for (const [channelId, boundClientId] of childrenBind.entries()) {
22
+ if (boundClientId === clientId) {
23
+ childrenBind.delete(channelId);
24
+ }
25
+ }
26
+ clientBindCount.delete(clientId);
27
+ };
28
+
29
+ export { bindChannelToClient, childrenBind, childrenClient, clientBindCount, fullClient, platformClient, unbindClient };
@@ -1,4 +1,4 @@
1
- import { Component, Head, Title, Style, Body, H1, P, A, Div, Html } from '../../core/react.js';
1
+ import { Component, Head, Title, Style, Body, H1, P, A, Div, Html } from '../../../common/react.js';
2
2
 
3
3
  class App extends Component {
4
4
  render() {
@@ -3,15 +3,41 @@ import { WebSocketServer, WebSocket } from 'ws';
3
3
  import * as flattedJSON from 'flatted';
4
4
  import koaCors from '@koa/cors';
5
5
  import router from '../routers/router.js';
6
- import { ResultCode } from '../../core/variable.js';
7
6
  import 'fs';
8
7
  import 'path';
9
8
  import 'yaml';
10
- import '../../core/utils.js';
11
- import { USER_AGENT_HEADER, USER_AGENT_HEADER_VALUE_MAP, DEVICE_ID_HEADER, FULL_RECEIVE_HEADER, platformClient, childrenClient, fullClient, childrenBind, clientBindCount, unbindClient, bindChannelToClient } from '../processor/config.js';
9
+ import '../../../common/logger.js';
10
+ import { ResultCode } from '../../../common/variable.js';
11
+ import '../../../common/utils.js';
12
+ import 'net';
13
+ import 'v8';
14
+ import 'os';
15
+ import { USER_AGENT_HEADER, USER_AGENT_HEADER_VALUE_MAP, DEVICE_ID_HEADER, FULL_RECEIVE_HEADER } from '../../../common/cbp/constants.js';
16
+ import '../../../common/cbp/runtime.js';
17
+ import { normalizeInboundMessage, getNormalizedDeviceId, getNormalizedEventRouteId } from '../../../common/cbp/normalize.js';
18
+ import { platformClient, childrenClient, fullClient, childrenBind, clientBindCount, unbindClient, bindChannelToClient } from '../processor/config.js';
12
19
  import { createTestOneController } from './testone.js';
13
20
  import { getClientChild } from '../../process/ipc-bridge.js';
14
21
 
22
+ const routeNormalizedMessage = (message, input) => {
23
+ const normalized = normalizeInboundMessage(input);
24
+ if (!normalized) {
25
+ return { normalized: null, handled: false };
26
+ }
27
+ if (normalized.kind === 'api.res' || normalized.kind === 'action.res') {
28
+ const resolvedDeviceId = getNormalizedDeviceId(normalized);
29
+ if (resolvedDeviceId) {
30
+ routeMessageToDevice(resolvedDeviceId, message);
31
+ }
32
+ return { normalized, handled: true };
33
+ }
34
+ if (normalized.kind === 'event') {
35
+ const routeId = getNormalizedEventRouteId(normalized);
36
+ handleEvent(message, routeId || '');
37
+ return { normalized, handled: true };
38
+ }
39
+ return { normalized, handled: false };
40
+ };
15
41
  const routeMessageToDevice = (DeviceId, message) => {
16
42
  if (childrenClient.has(DeviceId)) {
17
43
  const clientWs = childrenClient.get(DeviceId);
@@ -204,24 +230,13 @@ const setPlatformClient = (originId, ws) => {
204
230
  platformClient.set(originId, ws);
205
231
  ws.on('message', (message) => {
206
232
  try {
207
- const parsedMessage = flattedJSON.parse(message.toString());
233
+ const inbound = flattedJSON.parse(message.toString());
234
+ routeNormalizedMessage(message.toString(), inbound);
208
235
  logger.debug({
209
236
  code: ResultCode.Ok,
210
237
  message: '服务端接收到消息',
211
- data: parsedMessage
238
+ data: inbound
212
239
  });
213
- if (parsedMessage.apiId) {
214
- const DeviceId = parsedMessage.DeviceId;
215
- routeMessageToDevice(DeviceId, message);
216
- }
217
- else if (parsedMessage?.actionId) {
218
- const DeviceId = parsedMessage.DeviceId;
219
- routeMessageToDevice(DeviceId, message);
220
- }
221
- else if (parsedMessage?.name) {
222
- const ID = parsedMessage.ChannelId || parsedMessage.GuildId || parsedMessage.DeviceId;
223
- handleEvent(message, ID);
224
- }
225
240
  }
226
241
  catch (error) {
227
242
  logger.error({
@@ -255,26 +270,15 @@ const setTestOnePlatformClient = (ws) => {
255
270
  const controller = createTestOneController(ws);
256
271
  ws.on('message', (message) => {
257
272
  try {
258
- const parsedMessage = flattedJSON.parse(message.toString());
273
+ const inbound = flattedJSON.parse(message.toString());
274
+ const { handled } = routeNormalizedMessage(message.toString(), inbound);
259
275
  logger.debug({
260
276
  code: ResultCode.Ok,
261
277
  message: '测试端接收到消息',
262
- data: parsedMessage
278
+ data: inbound
263
279
  });
264
- if (parsedMessage.apiId) {
265
- const DeviceId = parsedMessage.DeviceId;
266
- routeMessageToDevice(DeviceId, message);
267
- }
268
- else if (parsedMessage?.actionId) {
269
- const DeviceId = parsedMessage.DeviceId;
270
- routeMessageToDevice(DeviceId, message);
271
- }
272
- else if (parsedMessage?.name) {
273
- const ID = parsedMessage.ChannelId || parsedMessage.GuildId || parsedMessage.DeviceId;
274
- handleEvent(message, ID);
275
- }
276
- else {
277
- controller.onMessage(parsedMessage);
280
+ if (!handled) {
281
+ controller.onMessage(inbound);
278
282
  }
279
283
  }
280
284
  catch (error) {
@@ -1,6 +1,6 @@
1
1
  import { IncomingMessage } from 'http';
2
2
  import { WebSocket } from 'ws';
3
- import type { ParsedMessage } from '../typings';
3
+ import type { ParsedMessage } from '../../../common/cbp/typings.js';
4
4
  export declare const createTestOneController: (ws: WebSocket, _request: IncomingMessage) => {
5
5
  onMessage: (parsedMessage: ParsedMessage) => void;
6
6
  close: () => void;
@@ -3,9 +3,15 @@ import { join, dirname } from 'path';
3
3
  import { existsSync, mkdirSync, watch, readFileSync, writeFile } from 'fs';
4
4
  import _ from 'lodash';
5
5
  import { readFile } from 'fs/promises';
6
- import { ResultCode } from '../../core/variable.js';
7
6
  import 'yaml';
8
- import '../../core/utils.js';
7
+ import '../../../common/logger.js';
8
+ import { ResultCode } from '../../../common/variable.js';
9
+ import '../../../common/utils.js';
10
+ import 'net';
11
+ import 'v8';
12
+ import 'os';
13
+ import '../../../common/cbp/runtime.js';
14
+ import 'ws';
9
15
 
10
16
  const createTestOneController = (ws, _request) => {
11
17
  const testonePath = join(process.cwd(), 'testone');
@@ -1,3 +1 @@
1
- export { ResultCode } from './variable.js';
2
- export * from './config.js';
3
- export * from './utils.js';
1
+ export { start } from './start.js';
package/lib/core/index.js CHANGED
@@ -1,3 +1 @@
1
- export { ResultCode } from './variable.js';
2
- export { getConfig, getConfigValue, onWatchConfigValue } from './config.js';
3
- export { createEventName, createHash, createResult, createUserHashKey, fastHash, getCachedRegExp, getInputExportPath, getRecursiveDirFiles, isMaster, matchIn, sanitizeForSerialization, showErrorModule, stringToNumber, useUserHashKey } from './utils.js';
1
+ export { start } from './start.js';
@@ -0,0 +1,3 @@
1
+ export * from './module.js';
2
+ export * from './platform.js';
3
+ export * from './ipc-bridge.js';
@@ -1,4 +1,3 @@
1
1
  export { startModuleAdapter } from './module.js';
2
2
  export { startPlatformAdapterWithFallback } from './platform.js';
3
3
  export { forwardFromClient, forwardFromPlatform, getClientChild, getPlatformChild, setClientChild, setPlatformChild } from './ipc-bridge.js';
4
- export { createDirectClient, createDirectServer, generateSocketPath } from './direct-channel.js';
@@ -4,7 +4,12 @@ import * as flattedJSON from 'flatted';
4
4
  import 'fs';
5
5
  import 'path';
6
6
  import 'yaml';
7
- import { sanitizeForSerialization } from '../core/utils.js';
7
+ import '../../common/logger.js';
8
+ import { sanitizeForSerialization } from '../../common/utils.js';
9
+ import 'net';
10
+ import 'v8';
11
+ import 'os';
12
+ import '../../common/cbp/runtime.js';
8
13
 
9
14
  let platformChild = null;
10
15
  let clientChild = null;
@@ -1,7 +1,16 @@
1
1
  import childProcess from 'child_process';
2
- import { ResultCode } from '../core/variable.js';
3
- import { getConfigValue } from '../core/config.js';
4
- import '../core/utils.js';
2
+ import { getConfigValue } from '../../common/config.js';
3
+ import { ResultCode } from '../../common/variable.js';
4
+ import '../../common/logger.js';
5
+ import '../../common/utils.js';
6
+ import 'net';
7
+ import 'v8';
8
+ import 'os';
9
+ import 'path';
10
+ import 'fs';
11
+ import 'flatted';
12
+ import '../../common/cbp/runtime.js';
13
+ import 'ws';
5
14
  import module$1 from 'module';
6
15
  import { setClientChild, forwardFromClient } from './ipc-bridge.js';
7
16
 
@@ -17,7 +26,7 @@ function startModuleAdapter() {
17
26
  FORK_TIMEOUT: pro?.fork_timeout ?? 6000
18
27
  };
19
28
  try {
20
- modulePath = require$1.resolve('../client.js');
29
+ modulePath = require$1.resolve('../../client.js');
21
30
  }
22
31
  catch (error) {
23
32
  logger?.warn?.({
@@ -1,7 +1,16 @@
1
1
  import childProcess from 'child_process';
2
- import { ResultCode } from '../core/variable.js';
3
- import { getConfigValue } from '../core/config.js';
4
- import '../core/utils.js';
2
+ import { getConfigValue } from '../../common/config.js';
3
+ import { ResultCode } from '../../common/variable.js';
4
+ import '../../common/logger.js';
5
+ import '../../common/utils.js';
6
+ import 'net';
7
+ import 'v8';
8
+ import 'os';
9
+ import 'path';
10
+ import 'fs';
11
+ import 'flatted';
12
+ import '../../common/cbp/runtime.js';
13
+ import 'ws';
5
14
  import module$1 from 'module';
6
15
  import { setPlatformChild, forwardFromPlatform } from './ipc-bridge.js';
7
16
 
@@ -0,0 +1,2 @@
1
+ import type { StartOptions } from '../types';
2
+ export declare const start: (options?: StartOptions | string) => void;
@@ -0,0 +1,76 @@
1
+ import { getConfig } from '../common/config.js';
2
+ import { cbpServer } from './cbp/server/main.js';
3
+ import { filePrefixCommon, defaultPlatformCommonPrefix } from '../common/variable.js';
4
+ import { startPlatformAdapterWithFallback } from './process/platform.js';
5
+ import { startModuleAdapter } from './process/module.js';
6
+ import { generateSocketPath } from '../common/direct-channel.js';
7
+
8
+ const createOptionsByKey = (options, key, defaultValue) => {
9
+ const cfg = getConfig();
10
+ const curValue = options?.[key] ?? cfg.argv?.[key];
11
+ const value = curValue ?? cfg.value?.[key] ?? defaultValue;
12
+ return value;
13
+ };
14
+ const startPlatform = (options) => {
15
+ const platform = createOptionsByKey(options, 'platform', '');
16
+ const login = createOptionsByKey(options, 'login', '');
17
+ if (!platform && !login) {
18
+ global.__sandbox = true;
19
+ return;
20
+ }
21
+ void startPlatformAdapterWithFallback();
22
+ };
23
+ const startClient = (options) => {
24
+ process.env.input = createOptionsByKey(options, 'input', '');
25
+ process.env.output = createOptionsByKey(options, 'output', '');
26
+ process.env.is_full_receive = String(createOptionsByKey(options, 'is_full_receive', true));
27
+ process.env.port = String(createOptionsByKey(options, 'port', '') || '');
28
+ process.env.url = createOptionsByKey(options, 'url', '');
29
+ startModuleAdapter();
30
+ };
31
+ const start = (options = {}) => {
32
+ if (typeof options === 'string') {
33
+ options = { input: options };
34
+ }
35
+ global.__options = options;
36
+ const port = createOptionsByKey(options, 'port', '');
37
+ const serverPort = createOptionsByKey(options, 'serverPort', '');
38
+ const platform = createOptionsByKey(options, 'platform', '');
39
+ const login = createOptionsByKey(options, 'login', '');
40
+ if (platform) {
41
+ const reg = filePrefixCommon;
42
+ if (reg.test(platform)) {
43
+ process.env.platform = platform;
44
+ process.env.login = platform.replace(reg, '');
45
+ }
46
+ else {
47
+ process.env.platform = platform;
48
+ process.env.login = platform;
49
+ }
50
+ }
51
+ else if (login) {
52
+ process.env.platform = `${defaultPlatformCommonPrefix}${login}`;
53
+ process.env.login = login;
54
+ }
55
+ process.env.port = port ? String(port) : '';
56
+ process.env.serverPort = serverPort;
57
+ if (port) {
58
+ cbpServer(port, () => {
59
+ const httpURL = `http://127.0.0.1:${port}`;
60
+ const wsURL = `ws://127.0.0.1:${port}`;
61
+ logger.info(`[CBP-Server] ${httpURL}`);
62
+ logger.info(`[CBP-Server] ${wsURL}]`);
63
+ startClient(options);
64
+ startPlatform(options);
65
+ });
66
+ }
67
+ else {
68
+ const sockPath = generateSocketPath();
69
+ process.env.__ALEMON_DIRECT_SOCK = sockPath;
70
+ logger.info('[Direct-IPC] 平台↔客户端直连');
71
+ startClient(options);
72
+ startPlatform(options);
73
+ }
74
+ };
75
+
76
+ export { start };
package/lib/global.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import type { DefineChildrenFunc, OnResponseReversalFunc, OnMiddlewareReversalFunc, OnSelectsFunc, OnDataFormatFunc, OnResponseReversalFuncBack, OnGroupFunc, OnMiddlewareReversalFuncBack, DefineResponseFunc, defineMiddlewareFunc, StoreChildrenApp, StateSubscribeMap, SubscribeKeysMap, LoggerUtils, ResponseState, StartOptions } from './types';
2
- import WebSocket, { Server } from 'ws';
3
- import { IncomingMessage } from 'http';
2
+ import { type Server } from 'ws';
3
+ import type WebSocket from 'ws';
4
+ import type { IncomingMessage } from 'http';
4
5
  import type KoaRouter from 'koa-router';
5
- import type { RuntimeAppRecord } from './app/store.js';
6
+ import type { RuntimeAppRecord } from './application/runtime/store.js';
6
7
  declare global {
7
8
  var __config: any;
8
9
  var __options: StartOptions;