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,324 @@
1
+ const now = () => Date.now();
2
+ const asRecord = (value) => {
3
+ return value && typeof value === 'object' && !Array.isArray(value) ? value : {};
4
+ };
5
+ const asResults = (value) => {
6
+ return Array.isArray(value) ? value : [];
7
+ };
8
+ const inferEventRouteId = (event) => {
9
+ const channelId = event.ChannelId;
10
+ const guildId = event.GuildId;
11
+ const deviceId = event.DeviceId;
12
+ if (typeof channelId === 'string' && channelId) {
13
+ return channelId;
14
+ }
15
+ if (typeof guildId === 'string' && guildId) {
16
+ return guildId;
17
+ }
18
+ if (typeof deviceId === 'string' && deviceId) {
19
+ return deviceId;
20
+ }
21
+ return undefined;
22
+ };
23
+ const createRequestEnvelope = (type, id, deviceId, payload) => {
24
+ return {
25
+ protocol: 'cbp',
26
+ version: 1,
27
+ type,
28
+ id,
29
+ timestamp: now(),
30
+ source: {
31
+ role: 'app-client',
32
+ deviceId
33
+ },
34
+ payload
35
+ };
36
+ };
37
+ const createResponseEnvelope = (type, replyTo, deviceId, payload, error) => {
38
+ return {
39
+ protocol: 'cbp',
40
+ version: 1,
41
+ type,
42
+ id: `${replyTo}:res`,
43
+ replyTo,
44
+ timestamp: now(),
45
+ source: {
46
+ role: 'platform',
47
+ deviceId
48
+ },
49
+ target: {
50
+ role: 'app-client',
51
+ deviceId
52
+ },
53
+ payload: {
54
+ results: payload
55
+ },
56
+ error
57
+ };
58
+ };
59
+ const normalizeEnvelopeMessage = (input) => {
60
+ const payload = input.payload;
61
+ const meta = asRecord(input.meta);
62
+ const deviceId = input.source?.deviceId ?? input.target?.deviceId;
63
+ const sourceRole = input.source?.role;
64
+ const targetRole = input.target?.role;
65
+ switch (input.type) {
66
+ case 'event': {
67
+ const eventPayload = asRecord(payload);
68
+ const event = asRecord(eventPayload.event);
69
+ return {
70
+ kind: 'event',
71
+ id: input.id,
72
+ timestamp: input.timestamp,
73
+ deviceId,
74
+ sourceRole,
75
+ targetRole,
76
+ eventName: String(eventPayload.name ?? ''),
77
+ event,
78
+ raw: eventPayload.raw,
79
+ meta
80
+ };
81
+ }
82
+ case 'action.req': {
83
+ const actionPayload = asRecord(payload);
84
+ return {
85
+ kind: 'action.req',
86
+ id: input.id,
87
+ timestamp: input.timestamp,
88
+ deviceId,
89
+ sourceRole,
90
+ targetRole,
91
+ action: String(actionPayload.action ?? ''),
92
+ input: asRecord(actionPayload.input),
93
+ meta
94
+ };
95
+ }
96
+ case 'action.res':
97
+ return {
98
+ kind: 'action.res',
99
+ id: input.id,
100
+ replyTo: String(input.replyTo ?? ''),
101
+ timestamp: input.timestamp,
102
+ deviceId,
103
+ sourceRole,
104
+ targetRole,
105
+ results: asResults(asRecord(payload).results),
106
+ error: input.error,
107
+ meta
108
+ };
109
+ case 'api.req': {
110
+ const apiPayload = asRecord(payload);
111
+ return {
112
+ kind: 'api.req',
113
+ id: input.id,
114
+ timestamp: input.timestamp,
115
+ deviceId,
116
+ sourceRole,
117
+ targetRole,
118
+ api: String(apiPayload.api ?? ''),
119
+ input: asRecord(apiPayload.input),
120
+ meta
121
+ };
122
+ }
123
+ case 'api.res':
124
+ return {
125
+ kind: 'api.res',
126
+ id: input.id,
127
+ replyTo: String(input.replyTo ?? ''),
128
+ timestamp: input.timestamp,
129
+ deviceId,
130
+ sourceRole,
131
+ targetRole,
132
+ results: asResults(asRecord(payload).results),
133
+ error: input.error,
134
+ meta
135
+ };
136
+ case 'control': {
137
+ const controlPayload = asRecord(payload);
138
+ return {
139
+ kind: 'control',
140
+ id: input.id,
141
+ timestamp: input.timestamp,
142
+ deviceId,
143
+ sourceRole,
144
+ targetRole,
145
+ op: String(controlPayload.op ?? 'error'),
146
+ payload: controlPayload,
147
+ error: input.error,
148
+ meta
149
+ };
150
+ }
151
+ default:
152
+ return null;
153
+ }
154
+ };
155
+ const normalizeLegacyMessage = (input) => {
156
+ if (input?.apiId) {
157
+ if (Array.isArray(input.payload)) {
158
+ return {
159
+ kind: 'api.res',
160
+ id: String(input.apiId),
161
+ replyTo: String(input.apiId),
162
+ timestamp: now(),
163
+ deviceId: typeof input.DeviceId === 'string' ? input.DeviceId : undefined,
164
+ results: input.payload,
165
+ meta: {}
166
+ };
167
+ }
168
+ return {
169
+ kind: 'api.req',
170
+ id: String(input.apiId),
171
+ timestamp: now(),
172
+ deviceId: typeof input.DeviceId === 'string' ? input.DeviceId : undefined,
173
+ api: typeof input.action === 'string' ? input.action : '',
174
+ input: asRecord(input.payload),
175
+ meta: {}
176
+ };
177
+ }
178
+ if (input?.actionId) {
179
+ if (Array.isArray(input.payload)) {
180
+ return {
181
+ kind: 'action.res',
182
+ id: String(input.actionId),
183
+ replyTo: String(input.actionId),
184
+ timestamp: now(),
185
+ deviceId: typeof input.DeviceId === 'string' ? input.DeviceId : undefined,
186
+ results: input.payload,
187
+ meta: {}
188
+ };
189
+ }
190
+ return {
191
+ kind: 'action.req',
192
+ id: String(input.actionId),
193
+ timestamp: now(),
194
+ deviceId: typeof input.DeviceId === 'string' ? input.DeviceId : undefined,
195
+ action: typeof input.action === 'string' ? input.action : '',
196
+ input: asRecord(input.payload),
197
+ meta: {}
198
+ };
199
+ }
200
+ if (input?.activeId && input?.active === 'sync') {
201
+ const payload = asRecord(input.payload);
202
+ return {
203
+ kind: 'control',
204
+ id: String(input.activeId),
205
+ timestamp: now(),
206
+ deviceId: typeof input.DeviceId === 'string' ? input.DeviceId : undefined,
207
+ op: 'sync',
208
+ payload,
209
+ meta: {}
210
+ };
211
+ }
212
+ if (input?.name) {
213
+ const event = input;
214
+ return {
215
+ kind: 'event',
216
+ id: typeof input.MessageId === 'string' ? input.MessageId : typeof input.DeviceId === 'string' ? input.DeviceId : `${input.name}:${now()}`,
217
+ timestamp: typeof input.CreateAt === 'number' ? input.CreateAt : now(),
218
+ deviceId: typeof input.DeviceId === 'string' ? input.DeviceId : undefined,
219
+ eventName: input.name,
220
+ event,
221
+ raw: input.value,
222
+ meta: {}
223
+ };
224
+ }
225
+ return null;
226
+ };
227
+ const isCBPEnvelope = (input) => {
228
+ if (!input || typeof input !== 'object') {
229
+ return false;
230
+ }
231
+ const value = input;
232
+ return value.protocol === 'cbp' && value.version === 1 && typeof value.type === 'string' && typeof value.id === 'string';
233
+ };
234
+ const normalizeInboundMessage = (input) => {
235
+ if (!input || typeof input !== 'object') {
236
+ return null;
237
+ }
238
+ if (isCBPEnvelope(input)) {
239
+ return normalizeEnvelopeMessage(input);
240
+ }
241
+ return normalizeLegacyMessage(input);
242
+ };
243
+ const toLegacyActionData = (message) => {
244
+ return {
245
+ action: message.action,
246
+ payload: message.input,
247
+ actionId: message.id,
248
+ DeviceId: message.deviceId
249
+ };
250
+ };
251
+ const toLegacyApiData = (message) => {
252
+ const input = message.input;
253
+ const key = typeof input.key === 'string' ? input.key : '';
254
+ const params = Array.isArray(input.params) ? input.params : [];
255
+ return {
256
+ action: message.api,
257
+ payload: {
258
+ event: input.event,
259
+ key,
260
+ params
261
+ },
262
+ apiId: message.id,
263
+ DeviceId: message.deviceId
264
+ };
265
+ };
266
+ const isNormalizedActionRequest = (message) => {
267
+ return message?.kind === 'action.req';
268
+ };
269
+ const isNormalizedApiRequest = (message) => {
270
+ return message?.kind === 'api.req';
271
+ };
272
+ const getNormalizedDeviceId = (message) => {
273
+ return message.deviceId;
274
+ };
275
+ const getNormalizedEventRouteId = (message) => {
276
+ if (message.kind !== 'event') {
277
+ return undefined;
278
+ }
279
+ return inferEventRouteId(message.event);
280
+ };
281
+ const createActionRequestEnvelope = (data) => {
282
+ return createRequestEnvelope('action.req', String(data.actionId ?? ''), data.DeviceId, {
283
+ action: data.action,
284
+ input: asRecord(data.payload)
285
+ });
286
+ };
287
+ const createApiRequestEnvelope = (data) => {
288
+ return createRequestEnvelope('api.req', String(data.apiId ?? ''), data.DeviceId, {
289
+ api: data.action,
290
+ input: asRecord(data.payload)
291
+ });
292
+ };
293
+ const createActionResponseEnvelope = (data, payload, error) => {
294
+ return createResponseEnvelope('action.res', String(data.actionId ?? ''), data.DeviceId, payload, error);
295
+ };
296
+ const createApiResponseEnvelope = (data, payload, error) => {
297
+ return createResponseEnvelope('api.res', String(data.apiId ?? ''), data.DeviceId, payload, error);
298
+ };
299
+ const createEventEnvelope = (data) => {
300
+ const deviceId = typeof data.DeviceId === 'string' ? data.DeviceId : undefined;
301
+ const eventName = typeof data.name === 'string' ? data.name : '';
302
+ const event = { ...data };
303
+ return {
304
+ protocol: 'cbp',
305
+ version: 1,
306
+ type: 'event',
307
+ id: typeof data.MessageId === 'string' ? data.MessageId : typeof deviceId === 'string' ? `${deviceId}:${now()}` : `${eventName}:${now()}`,
308
+ timestamp: typeof data.CreateAt === 'number' ? data.CreateAt : now(),
309
+ source: {
310
+ role: 'platform',
311
+ deviceId
312
+ },
313
+ payload: {
314
+ name: eventName,
315
+ event,
316
+ raw: data.value
317
+ },
318
+ meta: {
319
+ routeId: inferEventRouteId(event)
320
+ }
321
+ };
322
+ };
323
+
324
+ export { createActionRequestEnvelope, createActionResponseEnvelope, createApiRequestEnvelope, createApiResponseEnvelope, createEventEnvelope, getNormalizedDeviceId, getNormalizedEventRouteId, isCBPEnvelope, isNormalizedActionRequest, isNormalizedApiRequest, normalizeInboundMessage, toLegacyActionData, toLegacyApiData };
@@ -0,0 +1,2 @@
1
+ export declare const deviceId: `${string}-${string}-${string}-${string}-${string}`;
2
+ export declare const generateUniqueId: () => string;
@@ -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 };
@@ -0,0 +1,159 @@
1
+ import type { EventKeys, Apis, Actions } from '../../types/index.js';
2
+ import type { Result } from '../index.js';
3
+ export type ParsedMessage = {
4
+ apiId?: string;
5
+ actionId?: string;
6
+ testID?: string;
7
+ ChannelId?: string;
8
+ GuildId?: string;
9
+ name?: EventKeys;
10
+ DeviceId?: string;
11
+ activeId?: string;
12
+ payload?: any;
13
+ [key: string]: any;
14
+ };
15
+ export type CBPProtocolRole = 'platform' | 'app-client' | 'server';
16
+ export type CBPMessageType = 'event' | 'action.req' | 'action.res' | 'api.req' | 'api.res' | 'control';
17
+ export type CBPEndpoint = {
18
+ role: CBPProtocolRole;
19
+ deviceId?: string;
20
+ appName?: string;
21
+ platform?: string;
22
+ };
23
+ export type CBPError = {
24
+ code: string;
25
+ message: string;
26
+ retryable?: boolean;
27
+ details?: unknown;
28
+ };
29
+ export type CBPResult = {
30
+ code: number;
31
+ message: string;
32
+ data?: unknown;
33
+ };
34
+ export type CBPCapability = 'event' | 'action' | 'api' | 'full-receive';
35
+ export type CBPControlPayload = {
36
+ op: 'hello';
37
+ capabilities?: CBPCapability[];
38
+ } | {
39
+ op: 'heartbeat';
40
+ } | {
41
+ op: 'goodbye';
42
+ reason?: string;
43
+ } | {
44
+ op: 'error';
45
+ code: string;
46
+ message: string;
47
+ } | {
48
+ op: 'sync';
49
+ env?: Record<string, string>;
50
+ };
51
+ export type CBPEventPayload = {
52
+ name: EventKeys | string;
53
+ event: Record<string, unknown>;
54
+ raw?: unknown;
55
+ };
56
+ export type CBPActionRequestPayload = {
57
+ action: string;
58
+ input: Record<string, unknown>;
59
+ };
60
+ export type CBPActionResponsePayload = {
61
+ results: CBPResult[] | Result[];
62
+ };
63
+ export type CBPApiRequestPayload = {
64
+ api: string;
65
+ input: Record<string, unknown>;
66
+ };
67
+ export type CBPApiResponsePayload = {
68
+ results: CBPResult[] | Result[];
69
+ };
70
+ export type CBPEnvelope = {
71
+ protocol: 'cbp';
72
+ version: 1;
73
+ type: CBPMessageType;
74
+ id: string;
75
+ replyTo?: string;
76
+ timestamp: number;
77
+ source: CBPEndpoint;
78
+ target?: CBPEndpoint;
79
+ payload?: CBPEventPayload | CBPActionRequestPayload | CBPActionResponsePayload | CBPApiRequestPayload | CBPApiResponsePayload | CBPControlPayload | unknown;
80
+ error?: CBPError;
81
+ meta?: Record<string, unknown>;
82
+ };
83
+ export type NormalizedEventMessage = {
84
+ kind: 'event';
85
+ id: string;
86
+ timestamp: number;
87
+ deviceId?: string;
88
+ sourceRole?: CBPProtocolRole;
89
+ targetRole?: CBPProtocolRole;
90
+ eventName: EventKeys | string;
91
+ event: Record<string, unknown>;
92
+ raw?: unknown;
93
+ meta?: Record<string, unknown>;
94
+ };
95
+ export type NormalizedActionRequestMessage = {
96
+ kind: 'action.req';
97
+ id: string;
98
+ timestamp: number;
99
+ deviceId?: string;
100
+ sourceRole?: CBPProtocolRole;
101
+ targetRole?: CBPProtocolRole;
102
+ action: string;
103
+ input: Record<string, unknown>;
104
+ meta?: Record<string, unknown>;
105
+ };
106
+ export type NormalizedActionResponseMessage = {
107
+ kind: 'action.res';
108
+ id: string;
109
+ replyTo: string;
110
+ timestamp: number;
111
+ deviceId?: string;
112
+ sourceRole?: CBPProtocolRole;
113
+ targetRole?: CBPProtocolRole;
114
+ results: Result[];
115
+ error?: CBPError;
116
+ meta?: Record<string, unknown>;
117
+ };
118
+ export type NormalizedApiRequestMessage = {
119
+ kind: 'api.req';
120
+ id: string;
121
+ timestamp: number;
122
+ deviceId?: string;
123
+ sourceRole?: CBPProtocolRole;
124
+ targetRole?: CBPProtocolRole;
125
+ api: string;
126
+ input: Record<string, unknown>;
127
+ meta?: Record<string, unknown>;
128
+ };
129
+ export type NormalizedApiResponseMessage = {
130
+ kind: 'api.res';
131
+ id: string;
132
+ replyTo: string;
133
+ timestamp: number;
134
+ deviceId?: string;
135
+ sourceRole?: CBPProtocolRole;
136
+ targetRole?: CBPProtocolRole;
137
+ results: Result[];
138
+ error?: CBPError;
139
+ meta?: Record<string, unknown>;
140
+ };
141
+ export type NormalizedControlMessage = {
142
+ kind: 'control';
143
+ id: string;
144
+ timestamp: number;
145
+ deviceId?: string;
146
+ sourceRole?: CBPProtocolRole;
147
+ targetRole?: CBPProtocolRole;
148
+ op: CBPControlPayload['op'];
149
+ payload?: Record<string, unknown>;
150
+ error?: CBPError;
151
+ meta?: Record<string, unknown>;
152
+ };
153
+ export type NormalizedCBPMessage = NormalizedEventMessage | NormalizedActionRequestMessage | NormalizedActionResponseMessage | NormalizedApiRequestMessage | NormalizedApiResponseMessage | NormalizedControlMessage;
154
+ export type CBPClientOptions = {
155
+ open?: () => void;
156
+ isFullReceive?: boolean;
157
+ };
158
+ export type ActionReplyFunc = (data: Actions, consume: (payload: Result[]) => void) => void;
159
+ export type ApiReplyFunc = (data: Apis, consume: (payload: Result[]) => void) => void;
@@ -1,16 +1,14 @@
1
1
  import { WebSocket } from 'ws';
2
- import { ResultCode } from '../../core/variable.js';
3
- import 'fs';
4
- import 'path';
5
- import 'yaml';
6
- import '../../core/utils.js';
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
- delete global[globalKey];
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
- delete global[globalKey];
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 index$0 = process.argv.indexOf(key);
183
- if (index$0 !== -1) {
184
- return process.argv[index$0 + 1];
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,6 @@
1
+ export declare class Logger {
2
+ #private;
3
+ constructor();
4
+ get value(): any;
5
+ }
6
+ export declare const logger: any;