alemonjs 2.1.83 → 2.1.85

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 +4 -4
  27. package/lib/{app/hook-use → application/hooks}/user.js +12 -7
  28. package/lib/application/index.d.ts +29 -0
  29. package/lib/application/index.js +58 -0
  30. package/lib/{app → application}/router/dsl.d.ts +1 -1
  31. package/lib/{app → application}/router/dsl.js +14 -10
  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/{app → application/runtime}/event-error.d.ts +1 -1
  45. package/lib/{app → application/runtime}/event-error.js +1 -1
  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/{app → application/runtime}/event-processor-callHandler.d.ts +1 -1
  50. package/lib/{app → application/runtime}/event-processor-callHandler.js +8 -1
  51. package/lib/{app → application/runtime}/event-processor-cycle.d.ts +1 -1
  52. package/lib/{app → application/runtime}/event-processor-cycleFiles.d.ts +1 -1
  53. package/lib/{app → application/runtime}/event-processor-cycleFiles.js +2 -2
  54. package/lib/{app → application/runtime}/event-processor-cycleRoute.d.ts +1 -1
  55. package/lib/{app → application/runtime}/event-processor-cycleRoute.js +9 -2
  56. package/lib/{app → application/runtime}/event-processor-event.d.ts +1 -1
  57. package/lib/{app → application/runtime}/event-processor-middleware.d.ts +1 -1
  58. package/lib/{app → application/runtime}/event-processor-subscribe.d.ts +1 -1
  59. package/lib/{app → application/runtime}/event-processor-subscribe.js +9 -2
  60. package/lib/{app → application/runtime}/event-processor.d.ts +1 -1
  61. package/lib/{app → application/runtime}/event-processor.js +4 -3
  62. package/lib/{app → application/runtime}/event-response.d.ts +1 -1
  63. package/lib/{app → application/runtime}/event-utils.d.ts +1 -1
  64. package/lib/{app → application/runtime}/event-utils.js +2 -2
  65. package/lib/{app → application/runtime}/hook-event-context.d.ts +2 -2
  66. package/lib/{app → application/runtime}/hook-event-context.js +2 -2
  67. package/lib/application/runtime/http/index.d.ts +4 -0
  68. package/lib/application/runtime/http/index.js +4 -0
  69. package/lib/application/runtime/http/routers/hello.html.d.ts +4 -0
  70. package/lib/application/runtime/http/routers/hello.html.js +363 -0
  71. package/lib/{server → application/runtime/http}/routers/router.js +265 -179
  72. package/lib/{server → application/runtime/http}/routers/utils.d.ts +1 -1
  73. package/lib/{server → application/runtime/http}/routers/utils.js +22 -8
  74. package/lib/{server/main.js → application/runtime/http-server.js} +2 -6
  75. package/lib/{app → application/runtime}/lifecycle-callbacks.d.ts +1 -1
  76. package/lib/{app → application/runtime}/lifecycle-callbacks.js +2 -7
  77. package/lib/{app/load_modules → application/runtime/load-modules}/load.js +2 -2
  78. package/lib/{app/load_modules → application/runtime/load-modules}/loadChild.js +14 -5
  79. package/lib/{app → application/runtime}/schedule-store.d.ts +1 -1
  80. package/lib/{app → application/runtime}/schedule-store.js +1 -1
  81. package/lib/{app → application/runtime}/store.d.ts +14 -19
  82. package/lib/{app → application/runtime}/store.js +8 -103
  83. package/lib/application/schedule.d.ts +17 -0
  84. package/lib/{app/api → application}/schedule.js +1 -1
  85. package/lib/client.d.ts +1 -1
  86. package/lib/client.js +1 -117
  87. package/lib/common/cbp/constants.d.ts +11 -0
  88. package/lib/common/cbp/constants.js +13 -0
  89. package/lib/{cbp/connects/connect.js → common/cbp/heartbeat.js} +3 -6
  90. package/lib/common/cbp/normalize.d.ts +16 -0
  91. package/lib/common/cbp/normalize.js +324 -0
  92. package/lib/common/cbp/runtime.d.ts +2 -0
  93. package/lib/common/cbp/runtime.js +10 -0
  94. package/lib/common/cbp/typings.d.ts +159 -0
  95. package/lib/{cbp/connects/base.js → common/cbp/ws-connector.js} +7 -9
  96. package/lib/{core → common}/config.d.ts +2 -2
  97. package/lib/{core → common}/config.js +5 -4
  98. package/lib/common/identity.d.ts +11 -0
  99. package/lib/common/identity.js +34 -0
  100. package/lib/common/index.d.ts +15 -0
  101. package/lib/common/index.js +14 -0
  102. package/lib/common/logger.d.ts +6 -0
  103. package/lib/common/logger.js +99 -0
  104. package/lib/{core → common}/react.js +22 -3
  105. package/lib/common/result.d.ts +7 -0
  106. package/lib/common/result.js +19 -0
  107. package/lib/{core → common}/utils.d.ts +3 -18
  108. package/lib/{core → common}/utils.js +6 -49
  109. package/lib/{core → common}/variable.js +1 -1
  110. package/lib/core/cbp/index.d.ts +1 -0
  111. package/lib/core/cbp/index.js +1 -0
  112. package/lib/core/cbp/processor/config.d.ts +8 -0
  113. package/lib/core/cbp/processor/config.js +29 -0
  114. package/lib/{cbp → core/cbp}/routers/hello.html.js +1 -1
  115. package/lib/{cbp → core/cbp}/server/main.js +57 -35
  116. package/lib/{cbp → core/cbp}/server/testone.d.ts +1 -1
  117. package/lib/{cbp → core/cbp}/server/testone.js +8 -2
  118. package/lib/core/index.d.ts +1 -3
  119. package/lib/core/index.js +1 -3
  120. package/lib/core/process/index.d.ts +3 -0
  121. package/lib/{process → core/process}/index.js +0 -1
  122. package/lib/{process → core/process}/ipc-bridge.js +16 -2
  123. package/lib/{process → core/process}/module.js +13 -4
  124. package/lib/{process → core/process}/platform.js +12 -3
  125. package/lib/core/start.d.ts +2 -0
  126. package/lib/core/start.js +76 -0
  127. package/lib/global.d.ts +4 -3
  128. package/lib/index.d.ts +10 -2
  129. package/lib/index.js +74 -54
  130. package/lib/main.d.ts +1 -2
  131. package/lib/main.js +1 -76
  132. package/lib/{cbp/connects/platform.d.ts → platform/cbp-platform.d.ts} +2 -2
  133. package/lib/{cbp/connects/platform.js → platform/cbp-platform.js} +52 -75
  134. package/lib/{app → platform}/define-platform.js +2 -0
  135. package/lib/platform/event-value.d.ts +7 -0
  136. package/lib/platform/event-value.js +5 -0
  137. package/lib/platform/index.d.ts +5 -0
  138. package/lib/platform/index.js +5 -0
  139. package/lib/types/actions.d.ts +20 -1
  140. package/lib/types/apis.d.ts +2 -1
  141. package/lib/types/client/index.d.ts +1 -1
  142. package/lib/types/cycle/index.d.ts +1 -1
  143. package/lib/types/event/index.d.ts +1 -1
  144. package/lib/types/subscribe/index.d.ts +1 -1
  145. package/package.json +22 -1
  146. package/lib/app/api/schedule.d.ts +0 -17
  147. package/lib/app/index.d.ts +0 -25
  148. package/lib/app/index.js +0 -47
  149. package/lib/cbp/index.d.ts +0 -3
  150. package/lib/cbp/index.js +0 -3
  151. package/lib/cbp/processor/actions.d.ts +0 -3
  152. package/lib/cbp/processor/actions.js +0 -48
  153. package/lib/cbp/processor/api.d.ts +0 -3
  154. package/lib/cbp/processor/api.js +0 -48
  155. package/lib/cbp/processor/config.d.ts +0 -29
  156. package/lib/cbp/processor/config.js +0 -52
  157. package/lib/cbp/typings.d.ts +0 -20
  158. package/lib/process/index.d.ts +0 -4
  159. package/lib/server/routers/hello.html.d.ts +0 -2
  160. package/lib/server/routers/hello.html.js +0 -31
  161. /package/lib/{app → application}/define-children.d.ts +0 -0
  162. /package/lib/{app → application}/define-middleware.d.ts +0 -0
  163. /package/lib/{app → application}/define-middleware.js +0 -0
  164. /package/lib/{app → application}/define-response.d.ts +0 -0
  165. /package/lib/{app → application}/define-response.js +0 -0
  166. /package/lib/{app → application}/define-router.d.ts +0 -0
  167. /package/lib/{app → application}/define-router.js +0 -0
  168. /package/lib/{app → application}/expose.d.ts +0 -0
  169. /package/lib/{app → application}/expose.js +0 -0
  170. /package/lib/{app → application/format}/message-format-old.js +0 -0
  171. /package/lib/{app/hook-use → application/hooks}/announce.d.ts +0 -0
  172. /package/lib/{app/hook-use → application/hooks}/channel.d.ts +0 -0
  173. /package/lib/{app/hook-use → application/hooks}/event.d.ts +0 -0
  174. /package/lib/{app/hook-use → application/hooks}/guild.d.ts +0 -0
  175. /package/lib/{app/hook-use → application/hooks}/history.d.ts +0 -0
  176. /package/lib/{app/hook-use → application/hooks}/index.d.ts +0 -0
  177. /package/lib/{app/hook-use → application/hooks}/index.js +0 -0
  178. /package/lib/{app/hook-use → application/hooks}/me.d.ts +0 -0
  179. /package/lib/{app/hook-use → application/hooks}/member.d.ts +0 -0
  180. /package/lib/{app/hook-use → application/hooks}/mention.d.ts +0 -0
  181. /package/lib/{app/hook-use → application/hooks}/message.d.ts +0 -0
  182. /package/lib/{app/hook-use → application/hooks}/permission.d.ts +0 -0
  183. /package/lib/{app/hook-use → application/hooks}/reaction.d.ts +0 -0
  184. /package/lib/{app/hook-use → application/hooks}/request.d.ts +0 -0
  185. /package/lib/{app/hook-use → application/hooks}/role.d.ts +0 -0
  186. /package/lib/{app/hook-use → application/hooks}/route.d.ts +0 -0
  187. /package/lib/{app/hook-use → application/hooks}/route.js +0 -0
  188. /package/lib/{app/hook-use → application/hooks}/subscribe.d.ts +0 -0
  189. /package/lib/{app/hook-use → application/hooks}/user.d.ts +0 -0
  190. /package/lib/{app → application}/router/fallback.d.ts +0 -0
  191. /package/lib/{app → application}/router/fallback.js +0 -0
  192. /package/lib/{app → application}/router/main.d.ts +0 -0
  193. /package/lib/{app → application}/router/main.js +0 -0
  194. /package/lib/{app → application}/router/parser.d.ts +0 -0
  195. /package/lib/{app → application}/router/parser.js +0 -0
  196. /package/lib/{app → application}/router/types.d.ts +0 -0
  197. /package/lib/{app → application}/router/types.js +0 -0
  198. /package/lib/{app → application}/router/validator.d.ts +0 -0
  199. /package/lib/{app → application}/router/validator.js +0 -0
  200. /package/lib/{cbp → application/runtime/cbp}/processor/transport.d.ts +0 -0
  201. /package/lib/{cbp → application/runtime/cbp}/processor/transport.js +0 -0
  202. /package/lib/{app → application/runtime}/event-group.js +0 -0
  203. /package/lib/{app → application/runtime}/event-processor-cycle.js +0 -0
  204. /package/lib/{app → application/runtime}/event-processor-event.js +0 -0
  205. /package/lib/{app → application/runtime}/event-processor-middleware.js +0 -0
  206. /package/lib/{app → application/runtime}/event-response.js +0 -0
  207. /package/lib/{server → application/runtime/http}/routers/middleware.d.ts +0 -0
  208. /package/lib/{server → application/runtime/http}/routers/middleware.js +0 -0
  209. /package/lib/{cbp → application/runtime/http}/routers/router.d.ts +0 -0
  210. /package/lib/{server/main.d.ts → application/runtime/http-server.d.ts} +0 -0
  211. /package/lib/{app/load_modules → application/runtime/load-modules}/index.d.ts +0 -0
  212. /package/lib/{app/load_modules → application/runtime/load-modules}/index.js +0 -0
  213. /package/lib/{app/load_modules → application/runtime/load-modules}/load.d.ts +0 -0
  214. /package/lib/{app/load_modules → application/runtime/load-modules}/loadChild.d.ts +0 -0
  215. /package/lib/{app/config.d.ts → application/runtime/subscribe-status.d.ts} +0 -0
  216. /package/lib/{app/config.js → application/runtime/subscribe-status.js} +0 -0
  217. /package/lib/{app → common}/SinglyLinkedList.d.ts +0 -0
  218. /package/lib/{app → common}/SinglyLinkedList.js +0 -0
  219. /package/lib/{cbp/connects/connect.d.ts → common/cbp/heartbeat.d.ts} +0 -0
  220. /package/lib/{cbp → common/cbp}/typings.js +0 -0
  221. /package/lib/{cbp/connects/base.d.ts → common/cbp/ws-connector.d.ts} +0 -0
  222. /package/lib/{process → common}/direct-channel.d.ts +0 -0
  223. /package/lib/{process → common}/direct-channel.js +0 -0
  224. /package/lib/{core → common}/react.d.ts +0 -0
  225. /package/lib/{core → common}/variable.d.ts +0 -0
  226. /package/lib/{cbp → 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,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 };
@@ -32,7 +32,7 @@ class Component {
32
32
  function attrsToString(props = {}) {
33
33
  const parts = [];
34
34
  for (const key of Object.keys(props)) {
35
- if (key === 'children') {
35
+ if (key === 'children' || key === 'dangerouslySetInnerHTML') {
36
36
  continue;
37
37
  }
38
38
  const val = props[key];
@@ -59,6 +59,16 @@ function attrsToString(props = {}) {
59
59
  return parts.length ? ' ' + parts.join(' ') : '';
60
60
  }
61
61
  const voidTags = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']);
62
+ const rawTextTags = new Set(['script', 'style']);
63
+ function renderRawText(vnode) {
64
+ if (vnode === null || vnode === undefined || vnode === false) {
65
+ return '';
66
+ }
67
+ if (typeof vnode === 'string' || typeof vnode === 'number') {
68
+ return String(vnode);
69
+ }
70
+ return renderToString(vnode);
71
+ }
62
72
  function renderToString(vnode) {
63
73
  if (vnode === null || vnode === undefined || vnode === false) {
64
74
  return '';
@@ -92,12 +102,21 @@ function renderToString(vnode) {
92
102
  if (voidTags.has(tag.toLowerCase())) {
93
103
  return `<${tag}${attrs} />`;
94
104
  }
95
- const children = (vnode.children ?? []).map(renderToString).join('');
105
+ const children = rawTextTags.has(tag.toLowerCase()) ? (vnode.children ?? []).map(renderRawText).join('') : (vnode.children ?? []).map(renderToString).join('');
96
106
  return `<${tag}${attrs}>${children}</${tag}>`;
97
107
  }
98
108
  function makeTag(tag) {
99
109
  return function tagFactory(first, ...restChildren) {
100
- const looksLikeProps = first !== null && typeof first === 'object' && !Array.isArray(first) && Object.prototype.toString.call(first) === '[object Object]';
110
+ const looksLikeVNode = first !== null &&
111
+ typeof first === 'object' &&
112
+ !Array.isArray(first) &&
113
+ Object.prototype.toString.call(first) === '[object Object]' &&
114
+ ('type' in first || 'children' in first);
115
+ const looksLikeProps = first !== null &&
116
+ typeof first === 'object' &&
117
+ !Array.isArray(first) &&
118
+ Object.prototype.toString.call(first) === '[object Object]' &&
119
+ !looksLikeVNode;
101
120
  if (looksLikeProps) {
102
121
  return createElement(tag, first, ...restChildren);
103
122
  }
@@ -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,59 @@ 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, isNormalizedActionRequest, toLegacyActionData, isNormalizedApiRequest, toLegacyApiData } 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 stringifyForSocket = (payload) => {
23
+ return flattedJSON.stringify(payload);
24
+ };
25
+ const normalizeTestOneOutboundMessage = (message) => {
26
+ try {
27
+ const inbound = flattedJSON.parse(String(message));
28
+ const normalized = normalizeInboundMessage(inbound);
29
+ if (isNormalizedActionRequest(normalized)) {
30
+ return stringifyForSocket(toLegacyActionData(normalized));
31
+ }
32
+ if (isNormalizedApiRequest(normalized)) {
33
+ return stringifyForSocket(toLegacyApiData(normalized));
34
+ }
35
+ }
36
+ catch {
37
+ }
38
+ return message.toString();
39
+ };
40
+ const routeNormalizedMessage = (message, input) => {
41
+ const normalized = normalizeInboundMessage(input);
42
+ if (!normalized) {
43
+ return { normalized: null, handled: false };
44
+ }
45
+ if (normalized.kind === 'api.res' || normalized.kind === 'action.res') {
46
+ const resolvedDeviceId = getNormalizedDeviceId(normalized);
47
+ if (resolvedDeviceId) {
48
+ routeMessageToDevice(resolvedDeviceId, message);
49
+ }
50
+ return { normalized, handled: true };
51
+ }
52
+ if (normalized.kind === 'event') {
53
+ const routeId = getNormalizedEventRouteId(normalized);
54
+ handleEvent(message, routeId || '');
55
+ return { normalized, handled: true };
56
+ }
57
+ return { normalized, handled: false };
58
+ };
15
59
  const routeMessageToDevice = (DeviceId, message) => {
16
60
  if (childrenClient.has(DeviceId)) {
17
61
  const clientWs = childrenClient.get(DeviceId);
@@ -130,7 +174,7 @@ const setChildrenClient = (originId, ws) => {
130
174
  ws.on('message', (message) => {
131
175
  if (global.__sandbox) {
132
176
  if (global.testoneClient?.readyState === WebSocket.OPEN) {
133
- global.testoneClient.send(message.toString());
177
+ global.testoneClient.send(normalizeTestOneOutboundMessage(message.toString()));
134
178
  }
135
179
  return;
136
180
  }
@@ -167,7 +211,7 @@ const setFullClient = (originId, ws) => {
167
211
  ws.on('message', (message) => {
168
212
  if (global.__sandbox) {
169
213
  if (global.testoneClient?.readyState === WebSocket.OPEN) {
170
- global.testoneClient.send(message.toString());
214
+ global.testoneClient.send(normalizeTestOneOutboundMessage(message.toString()));
171
215
  }
172
216
  return;
173
217
  }
@@ -204,24 +248,13 @@ const setPlatformClient = (originId, ws) => {
204
248
  platformClient.set(originId, ws);
205
249
  ws.on('message', (message) => {
206
250
  try {
207
- const parsedMessage = flattedJSON.parse(message.toString());
251
+ const inbound = flattedJSON.parse(message.toString());
252
+ routeNormalizedMessage(message.toString(), inbound);
208
253
  logger.debug({
209
254
  code: ResultCode.Ok,
210
255
  message: '服务端接收到消息',
211
- data: parsedMessage
256
+ data: inbound
212
257
  });
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
258
  }
226
259
  catch (error) {
227
260
  logger.error({
@@ -255,26 +288,15 @@ const setTestOnePlatformClient = (ws) => {
255
288
  const controller = createTestOneController(ws);
256
289
  ws.on('message', (message) => {
257
290
  try {
258
- const parsedMessage = flattedJSON.parse(message.toString());
291
+ const inbound = flattedJSON.parse(message.toString());
292
+ const { handled } = routeNormalizedMessage(message.toString(), inbound);
259
293
  logger.debug({
260
294
  code: ResultCode.Ok,
261
295
  message: '测试端接收到消息',
262
- data: parsedMessage
296
+ data: inbound
263
297
  });
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);
298
+ if (!handled) {
299
+ controller.onMessage(inbound);
278
300
  }
279
301
  }
280
302
  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,13 @@ 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';
13
+ import { normalizeInboundMessage, isNormalizedActionRequest, toLegacyActionData, isNormalizedApiRequest, toLegacyApiData } from '../../common/cbp/normalize.js';
8
14
 
9
15
  let platformChild = null;
10
16
  let clientChild = null;
@@ -44,7 +50,15 @@ const forwardFromClient = (data) => {
44
50
  }
45
51
  if (global.__sandbox && global.testoneClient) {
46
52
  try {
47
- const messageStr = typeof safeData === 'string' ? safeData : flattedJSON.stringify(safeData);
53
+ const normalized = normalizeInboundMessage(safeData);
54
+ let outbound = safeData;
55
+ if (isNormalizedActionRequest(normalized)) {
56
+ outbound = toLegacyActionData(normalized);
57
+ }
58
+ else if (isNormalizedApiRequest(normalized)) {
59
+ outbound = toLegacyApiData(normalized);
60
+ }
61
+ const messageStr = typeof outbound === 'string' ? outbound : flattedJSON.stringify(outbound);
48
62
  if (global.testoneClient.readyState === WebSocket.OPEN) {
49
63
  global.testoneClient.send(messageStr);
50
64
  }
@@ -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;