alemonjs 2.1.17 → 2.1.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/app/SinglyLinkedList.d.ts +1 -0
- package/lib/app/SinglyLinkedList.js +12 -6
- package/lib/app/define-platform.d.ts +1 -0
- package/lib/app/define-platform.js +3 -2
- package/lib/app/define-response.d.ts +0 -3
- package/lib/app/define-response.js +1 -5
- package/lib/app/define-router.d.ts +5 -0
- package/lib/app/define-router.js +21 -0
- package/lib/app/event-processor-callHandler.js +5 -15
- package/lib/app/event-processor-cycleFiles.d.ts +1 -0
- package/lib/app/event-processor-cycleFiles.js +19 -2
- package/lib/app/event-processor-cycleRoute.js +32 -7
- package/lib/app/event-processor-event.js +4 -4
- package/lib/app/event-processor-middleware.js +4 -4
- package/lib/app/event-processor.js +15 -6
- package/lib/app/hook-use-api.d.ts +14 -6
- package/lib/app/hook-use-api.js +80 -82
- package/lib/app/index.d.ts +1 -0
- package/lib/app/index.js +4 -3
- package/lib/app/load_modules/loadChild.js +6 -6
- package/lib/app/message-format.d.ts +30 -1
- package/lib/app/message-format.js +82 -1
- package/lib/app/store.d.ts +3 -3
- package/lib/app/store.js +23 -11
- package/lib/cbp/connects/base.d.ts +15 -0
- package/lib/cbp/connects/base.js +77 -0
- package/lib/cbp/connects/client.js +13 -65
- package/lib/cbp/connects/platform.js +11 -63
- package/lib/cbp/server/main.js +5 -25
- package/lib/client.js +0 -1
- package/lib/core/config.js +17 -8
- package/lib/core/variable.d.ts +2 -1
- package/lib/core/variable.js +3 -2
- package/lib/index.js +4 -3
- package/lib/types/event/index.d.ts +7 -0
- package/package.json +1 -1
- package/lib/app/define-chidren.d.ts +0 -2
- package/lib/app/define-chidren.js +0 -19
- package/lib/app/define-pl.ts +0 -0
- package/lib/cbp/core/connection-manager.d.ts +0 -18
- package/lib/cbp/core/connection-manager.js +0 -67
- package/lib/cbp/core/constants.d.ts +0 -19
- package/lib/cbp/core/constants.js +0 -23
- package/lib/cbp/core/load-balancer.d.ts +0 -37
- package/lib/cbp/core/load-balancer.js +0 -118
- package/lib/cbp/processor/request-handler.d.ts +0 -17
- package/lib/cbp/processor/request-handler.js +0 -65
- package/lib/polyfills/fs-promises.d.ts +0 -41
- package/lib/polyfills/fs-promises.js +0 -138
- package/lib/polyfills/fs.d.ts +0 -103
- package/lib/polyfills/fs.js +0 -229
- package/lib/polyfills/util-types.d.ts +0 -33
- package/lib/polyfills/util-types.js +0 -36
- package/lib/types/event/channal/index.d.ts +0 -10
- package/lib/types/event/channal/index.js +0 -1
- /package/lib/cbp/processor/{heandle.d.ts → handle.d.ts} +0 -0
- /package/lib/cbp/processor/{heandle.js → handle.js} +0 -0
|
@@ -40,7 +40,7 @@ const loadChildren = async (mainPath, appName) => {
|
|
|
40
40
|
else {
|
|
41
41
|
app = await moduleApp.default.callback();
|
|
42
42
|
}
|
|
43
|
-
App.
|
|
43
|
+
App.pushCycle(app);
|
|
44
44
|
const unMounted = async (e) => {
|
|
45
45
|
showErrorModule(e);
|
|
46
46
|
App.un();
|
|
@@ -64,7 +64,7 @@ const loadChildren = async (mainPath, appName) => {
|
|
|
64
64
|
}
|
|
65
65
|
const registerMounted = async () => {
|
|
66
66
|
const res = await app?.register();
|
|
67
|
-
if (res && res?.response) {
|
|
67
|
+
if (res && (res?.response || res?.middleware || res?.responseRouter || res?.middlewareRouter)) {
|
|
68
68
|
App.register(res);
|
|
69
69
|
}
|
|
70
70
|
App.on();
|
|
@@ -87,7 +87,7 @@ const loadChildren = async (mainPath, appName) => {
|
|
|
87
87
|
for (const file of files) {
|
|
88
88
|
const url = file.path.replace(mainDir, '');
|
|
89
89
|
const stateKey = createEventName(url, appName);
|
|
90
|
-
const
|
|
90
|
+
const responseItem = {
|
|
91
91
|
input: mainDir,
|
|
92
92
|
dir: dirname(file.path),
|
|
93
93
|
path: file.path,
|
|
@@ -95,7 +95,7 @@ const loadChildren = async (mainPath, appName) => {
|
|
|
95
95
|
stateKey,
|
|
96
96
|
appName: appName
|
|
97
97
|
};
|
|
98
|
-
resData.push(
|
|
98
|
+
resData.push(responseItem);
|
|
99
99
|
}
|
|
100
100
|
App.pushResponse(resData);
|
|
101
101
|
const responseAndMiddlewareFiles = getRecursiveDirFiles(responseDir, item => fileSuffixMiddleware.test(item.name));
|
|
@@ -103,7 +103,7 @@ const loadChildren = async (mainPath, appName) => {
|
|
|
103
103
|
for (const file of responseAndMiddlewareFiles) {
|
|
104
104
|
const url = file.path.replace(mainDir, '');
|
|
105
105
|
const stateKey = createEventName(url, appName);
|
|
106
|
-
const
|
|
106
|
+
const responseItem = {
|
|
107
107
|
input: mainDir,
|
|
108
108
|
dir: dirname(file.path),
|
|
109
109
|
path: file.path,
|
|
@@ -111,7 +111,7 @@ const loadChildren = async (mainPath, appName) => {
|
|
|
111
111
|
stateKey,
|
|
112
112
|
appName: appName
|
|
113
113
|
};
|
|
114
|
-
resAndMwData[stateKey] =
|
|
114
|
+
resAndMwData[stateKey] = responseItem;
|
|
115
115
|
}
|
|
116
116
|
App.pushResponseMiddleware(resAndMwData);
|
|
117
117
|
const mwDir = join(mainDir, 'middleware');
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DataMention, DataImage, DataText, DataImageURL, DataImageFile, ButtonRow, DataButtonGroup, DataButton, DataArkList, DataArkListTip, DataArkListContent, DataArkListItem, DataArkCard, DataArkBigCard, DataMarkdownTemplate, DataMarkDown, DataMarkdownTitle, DataMarkdownSubtitle, DataMarkdownBold, DataMarkdownItalic, DataMarkdownItalicStar, DataMarkdownStrikethrough, DataMarkdownLink, DataMarkdownImage, DataMarkdownList, DataMarkdownListItem, DataMarkdownBlockquote, DataMarkdownDivider, DataMarkdownNewline, DataLink, DataMarkdownText, DataButtonTemplate, DataMarkdownCode } from '../types';
|
|
1
|
+
import { DataMention, DataImage, DataText, DataImageURL, DataImageFile, ButtonRow, DataButtonGroup, DataButton, DataArkList, DataArkListTip, DataArkListContent, DataArkListItem, DataArkCard, DataArkBigCard, DataMarkdownTemplate, DataMarkDown, DataMarkdownTitle, DataMarkdownSubtitle, DataMarkdownBold, DataMarkdownItalic, DataMarkdownItalicStar, DataMarkdownStrikethrough, DataMarkdownLink, DataMarkdownImage, DataMarkdownList, DataMarkdownListItem, DataMarkdownBlockquote, DataMarkdownDivider, DataMarkdownNewline, DataLink, DataMarkdownText, DataButtonTemplate, DataMarkdownCode, DataEnums, EventKeys, Events } from '../types';
|
|
2
2
|
export declare const Text: (val: DataText["value"], options?: DataText["options"]) => DataText;
|
|
3
3
|
export declare const Link: (val: DataLink["value"], options?: DataText["options"]) => DataText;
|
|
4
4
|
export declare const ImageURL: (val: DataImageURL["value"]) => DataImageURL;
|
|
@@ -48,3 +48,32 @@ declare const MD: {
|
|
|
48
48
|
code(value: DataMarkdownCode["value"], options?: DataMarkdownCode["options"]): DataMarkdownCode;
|
|
49
49
|
};
|
|
50
50
|
export { MD };
|
|
51
|
+
export declare class Format {
|
|
52
|
+
#private;
|
|
53
|
+
static create(): Format;
|
|
54
|
+
get value(): DataEnums[];
|
|
55
|
+
addText(...args: Parameters<typeof Text>): this;
|
|
56
|
+
addTextBreak(): this;
|
|
57
|
+
addLink(...args: Parameters<typeof Link>): this;
|
|
58
|
+
addImage(...args: Parameters<typeof Image>): this;
|
|
59
|
+
addImageFile(...args: Parameters<typeof ImageFile>): this;
|
|
60
|
+
addImageURL(...args: Parameters<typeof ImageURL>): this;
|
|
61
|
+
addMention(...args: Parameters<typeof Mention>): this;
|
|
62
|
+
addButtonGroup(...args: Parameters<typeof BT.group>): this;
|
|
63
|
+
addButtonTemplate(...args: Parameters<typeof BT.template>): this;
|
|
64
|
+
addMarkdown(...args: Parameters<typeof MD>): this;
|
|
65
|
+
addMarkdownTemplate(...args: Parameters<typeof MD.template>): this;
|
|
66
|
+
clear(): this;
|
|
67
|
+
}
|
|
68
|
+
export declare function createEvent<T extends EventKeys>(options: {
|
|
69
|
+
event: any;
|
|
70
|
+
selects: T | T[];
|
|
71
|
+
regular?: RegExp;
|
|
72
|
+
prefix?: string;
|
|
73
|
+
exact?: string;
|
|
74
|
+
}): (Events[T] & {
|
|
75
|
+
selects: boolean;
|
|
76
|
+
regular: boolean;
|
|
77
|
+
prefix: boolean;
|
|
78
|
+
exact: boolean;
|
|
79
|
+
});
|
|
@@ -213,5 +213,86 @@ MD.code = (value, options) => {
|
|
|
213
213
|
options: options
|
|
214
214
|
};
|
|
215
215
|
};
|
|
216
|
+
class Format {
|
|
217
|
+
#data = [];
|
|
218
|
+
static create() {
|
|
219
|
+
return new Format();
|
|
220
|
+
}
|
|
221
|
+
get value() {
|
|
222
|
+
return this.#data;
|
|
223
|
+
}
|
|
224
|
+
addText(...args) {
|
|
225
|
+
this.#data.push(Text(...args));
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
addTextBreak() {
|
|
229
|
+
this.#data.push(Text('\n'));
|
|
230
|
+
return this;
|
|
231
|
+
}
|
|
232
|
+
addLink(...args) {
|
|
233
|
+
this.#data.push(Link(...args));
|
|
234
|
+
return this;
|
|
235
|
+
}
|
|
236
|
+
addImage(...args) {
|
|
237
|
+
this.#data.push(Image(...args));
|
|
238
|
+
return this;
|
|
239
|
+
}
|
|
240
|
+
addImageFile(...args) {
|
|
241
|
+
this.#data.push(ImageFile(...args));
|
|
242
|
+
return this;
|
|
243
|
+
}
|
|
244
|
+
addImageURL(...args) {
|
|
245
|
+
this.#data.push(ImageURL(...args));
|
|
246
|
+
return this;
|
|
247
|
+
}
|
|
248
|
+
addMention(...args) {
|
|
249
|
+
this.#data.push(Mention(...args));
|
|
250
|
+
return this;
|
|
251
|
+
}
|
|
252
|
+
addButtonGroup(...args) {
|
|
253
|
+
this.#data.push(BT.group(...args));
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
256
|
+
addButtonTemplate(...args) {
|
|
257
|
+
this.#data.push(BT.template(...args));
|
|
258
|
+
return this;
|
|
259
|
+
}
|
|
260
|
+
addMarkdown(...args) {
|
|
261
|
+
this.#data.push(MD(...args));
|
|
262
|
+
return this;
|
|
263
|
+
}
|
|
264
|
+
addMarkdownTemplate(...args) {
|
|
265
|
+
this.#data.push(MD.template(...args));
|
|
266
|
+
return this;
|
|
267
|
+
}
|
|
268
|
+
clear() {
|
|
269
|
+
this.#data = [];
|
|
270
|
+
return this;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
function createEvent(options) {
|
|
274
|
+
const { event, selects, regular, prefix, exact } = options;
|
|
275
|
+
const { name, MessageText } = event || {};
|
|
276
|
+
const selectsArr = Array.isArray(selects) ? selects : [selects];
|
|
277
|
+
const o = {
|
|
278
|
+
selects: false,
|
|
279
|
+
regular: false,
|
|
280
|
+
prefix: false,
|
|
281
|
+
exact: false
|
|
282
|
+
};
|
|
283
|
+
if (selectsArr.includes(name)) {
|
|
284
|
+
o.selects = true;
|
|
285
|
+
}
|
|
286
|
+
if (exact && MessageText && MessageText === exact) {
|
|
287
|
+
o.exact = true;
|
|
288
|
+
}
|
|
289
|
+
if (prefix && MessageText?.startsWith(prefix)) {
|
|
290
|
+
o.prefix = true;
|
|
291
|
+
}
|
|
292
|
+
if (regular && MessageText && new RegExp(regular).test(MessageText)) {
|
|
293
|
+
o.regular = true;
|
|
294
|
+
}
|
|
295
|
+
return { ...event, ...o };
|
|
296
|
+
}
|
|
216
297
|
|
|
217
|
-
export { Ark, BT, Image, ImageFile, ImageURL, Link, MD, Mention, Text };
|
|
298
|
+
export { Ark, BT, Format, Image, ImageFile, ImageURL, Link, MD, Mention, Text, createEvent };
|
package/lib/app/store.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export declare class Middleware {
|
|
|
33
33
|
}
|
|
34
34
|
export declare class SubscribeList<T extends EventKeys> {
|
|
35
35
|
#private;
|
|
36
|
-
constructor(
|
|
36
|
+
constructor(choice: EventCycleEnum, select: T);
|
|
37
37
|
get value(): SinglyLinkedList<SubscribeValue>;
|
|
38
38
|
}
|
|
39
39
|
export declare class StateSubscribe {
|
|
@@ -58,13 +58,13 @@ export declare class ChildrenApp {
|
|
|
58
58
|
[key: string]: StoreResponseItem;
|
|
59
59
|
}): void;
|
|
60
60
|
pushMiddleware(data: StoreMiddlewareItem[]): void;
|
|
61
|
-
|
|
61
|
+
pushCycle(data: ChildrenCycle): void;
|
|
62
62
|
on(): void;
|
|
63
63
|
un(): void;
|
|
64
64
|
get value(): import("..").StoreChildrenApp;
|
|
65
65
|
}
|
|
66
66
|
export declare const ProcessorEventAutoClearMap: Map<any, any>;
|
|
67
|
-
export declare const
|
|
67
|
+
export declare const ProcessorEventUserAutoClearMap: Map<any, any>;
|
|
68
68
|
export declare const logger: any;
|
|
69
69
|
export declare const core: {
|
|
70
70
|
storeState: import("..").ResponseState;
|
package/lib/app/store.js
CHANGED
|
@@ -152,7 +152,13 @@ class ResponseRouter {
|
|
|
152
152
|
if (!alemonjsCore.storeChildrenApp[key].register) {
|
|
153
153
|
return [];
|
|
154
154
|
}
|
|
155
|
-
|
|
155
|
+
if (alemonjsCore.storeChildrenApp[key].register?.responseRouter) {
|
|
156
|
+
return alemonjsCore.storeChildrenApp[key].register?.responseRouter?.current ?? [];
|
|
157
|
+
}
|
|
158
|
+
if (alemonjsCore.storeChildrenApp[key].register?.response) {
|
|
159
|
+
return alemonjsCore.storeChildrenApp[key].register?.response?.current ?? [];
|
|
160
|
+
}
|
|
161
|
+
return [];
|
|
156
162
|
});
|
|
157
163
|
return data.flat();
|
|
158
164
|
}
|
|
@@ -163,7 +169,13 @@ class MiddlewareRouter {
|
|
|
163
169
|
if (!alemonjsCore.storeChildrenApp[key].register) {
|
|
164
170
|
return [];
|
|
165
171
|
}
|
|
166
|
-
|
|
172
|
+
if (alemonjsCore.storeChildrenApp[key].register?.middlewareRouter) {
|
|
173
|
+
return alemonjsCore.storeChildrenApp[key].register?.middlewareRouter?.current ?? [];
|
|
174
|
+
}
|
|
175
|
+
if (alemonjsCore.storeChildrenApp[key].register?.middleware) {
|
|
176
|
+
return alemonjsCore.storeChildrenApp[key].register?.middleware?.current ?? [];
|
|
177
|
+
}
|
|
178
|
+
return [];
|
|
167
179
|
});
|
|
168
180
|
return data.flat();
|
|
169
181
|
}
|
|
@@ -178,16 +190,16 @@ class Middleware {
|
|
|
178
190
|
}
|
|
179
191
|
class SubscribeList {
|
|
180
192
|
#select;
|
|
181
|
-
#
|
|
182
|
-
constructor(
|
|
193
|
+
#choice;
|
|
194
|
+
constructor(choice, select) {
|
|
183
195
|
this.#select = select;
|
|
184
|
-
this.#
|
|
185
|
-
if (!alemonjsCore.storeSubscribeList[this.#
|
|
186
|
-
alemonjsCore.storeSubscribeList[this.#
|
|
196
|
+
this.#choice = choice;
|
|
197
|
+
if (!alemonjsCore.storeSubscribeList[this.#choice].has(this.#select)) {
|
|
198
|
+
alemonjsCore.storeSubscribeList[this.#choice].set(this.#select, new SinglyLinkedList());
|
|
187
199
|
}
|
|
188
200
|
}
|
|
189
201
|
get value() {
|
|
190
|
-
return alemonjsCore.storeSubscribeList[this.#
|
|
202
|
+
return alemonjsCore.storeSubscribeList[this.#choice].get(this.#select);
|
|
191
203
|
}
|
|
192
204
|
}
|
|
193
205
|
class StateSubscribe {
|
|
@@ -269,7 +281,7 @@ class ChildrenApp {
|
|
|
269
281
|
pushMiddleware(data) {
|
|
270
282
|
this.#middleware = this.#middleware.concat(data);
|
|
271
283
|
}
|
|
272
|
-
|
|
284
|
+
pushCycle(data) {
|
|
273
285
|
this.#cycle = data;
|
|
274
286
|
}
|
|
275
287
|
on() {
|
|
@@ -293,7 +305,7 @@ class ChildrenApp {
|
|
|
293
305
|
}
|
|
294
306
|
}
|
|
295
307
|
const ProcessorEventAutoClearMap = new Map();
|
|
296
|
-
const
|
|
308
|
+
const ProcessorEventUserAutoClearMap = new Map();
|
|
297
309
|
const logger = new Logger().value;
|
|
298
310
|
const core = new Core().value;
|
|
299
311
|
['SIGINT', 'SIGTERM', 'SIGQUIT', 'disconnect'].forEach(sig => {
|
|
@@ -305,4 +317,4 @@ process?.on?.('exit', code => {
|
|
|
305
317
|
logger.info?.(`[alemonjs][exit] 进程退出,code=${code}`);
|
|
306
318
|
});
|
|
307
319
|
|
|
308
|
-
export { ChildrenApp, Core, Logger, Middleware, MiddlewareRouter, ProcessorEventAutoClearMap,
|
|
320
|
+
export { ChildrenApp, Core, Logger, Middleware, MiddlewareRouter, ProcessorEventAutoClearMap, ProcessorEventUserAutoClearMap, Response, ResponseMiddleware, ResponseRouter, State, StateSubscribe, SubscribeList, core, logger };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type WSConnectorOptions = {
|
|
2
|
+
url: string;
|
|
3
|
+
role: 'client' | 'platform';
|
|
4
|
+
onOpen?: () => void;
|
|
5
|
+
onMessage: (message: string) => void;
|
|
6
|
+
extraHeaders?: Record<string, string>;
|
|
7
|
+
globalKey: 'chatbotClient' | 'chatbotPlatform';
|
|
8
|
+
};
|
|
9
|
+
export declare const createWSConnector: (options: WSConnectorOptions) => {
|
|
10
|
+
heartbeatControl: {
|
|
11
|
+
start: () => void;
|
|
12
|
+
stop: () => void;
|
|
13
|
+
pong: () => void;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
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';
|
|
9
|
+
|
|
10
|
+
const createWSConnector = (options) => {
|
|
11
|
+
const { url, role, onOpen, onMessage, extraHeaders = {}, globalKey } = options;
|
|
12
|
+
if (global[globalKey]) {
|
|
13
|
+
delete global[globalKey];
|
|
14
|
+
}
|
|
15
|
+
const [heartbeatControl] = useHeartbeat({
|
|
16
|
+
ping: () => {
|
|
17
|
+
global?.[globalKey]?.ping?.();
|
|
18
|
+
},
|
|
19
|
+
isConnected: () => {
|
|
20
|
+
return global?.[globalKey] && global?.[globalKey]?.readyState === WebSocket.OPEN;
|
|
21
|
+
},
|
|
22
|
+
terminate: () => {
|
|
23
|
+
try {
|
|
24
|
+
global?.[globalKey]?.terminate?.();
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
logger.debug({
|
|
28
|
+
code: ResultCode.Fail,
|
|
29
|
+
message: '强制断开连接失败',
|
|
30
|
+
data: error
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
const start = () => {
|
|
36
|
+
global[globalKey] = new WebSocket(url, {
|
|
37
|
+
headers: {
|
|
38
|
+
[USER_AGENT_HEADER]: role,
|
|
39
|
+
[DEVICE_ID_HEADER]: deviceId,
|
|
40
|
+
...extraHeaders
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
global[globalKey].on('open', () => {
|
|
44
|
+
onOpen?.();
|
|
45
|
+
heartbeatControl.start();
|
|
46
|
+
});
|
|
47
|
+
global[globalKey].on('pong', () => {
|
|
48
|
+
heartbeatControl.pong();
|
|
49
|
+
});
|
|
50
|
+
global[globalKey].on('message', (message) => {
|
|
51
|
+
onMessage(message.toString());
|
|
52
|
+
});
|
|
53
|
+
global[globalKey].on('close', (code) => {
|
|
54
|
+
heartbeatControl.stop();
|
|
55
|
+
logger.warn({
|
|
56
|
+
code: ResultCode.Fail,
|
|
57
|
+
message: `${role} 连接关闭,尝试重新连接...`,
|
|
58
|
+
data: code
|
|
59
|
+
});
|
|
60
|
+
delete global[globalKey];
|
|
61
|
+
setTimeout(() => {
|
|
62
|
+
start();
|
|
63
|
+
}, reconnectInterval);
|
|
64
|
+
});
|
|
65
|
+
global[globalKey].on('error', (err) => {
|
|
66
|
+
logger.error({
|
|
67
|
+
code: ResultCode.Fail,
|
|
68
|
+
message: `${role} 端错误`,
|
|
69
|
+
data: err
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
start();
|
|
74
|
+
return { heartbeatControl };
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export { createWSConnector };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { WebSocket } from 'ws';
|
|
2
1
|
import * as flattedJSON from 'flatted';
|
|
3
2
|
import '../../app/store.js';
|
|
4
3
|
import 'fs';
|
|
@@ -13,57 +12,26 @@ import '../../app/event-group.js';
|
|
|
13
12
|
import '../../app/event-middleware.js';
|
|
14
13
|
import { onProcessor } from '../../app/event-processor.js';
|
|
15
14
|
import '../../app/event-response.js';
|
|
16
|
-
import 'util/types';
|
|
17
15
|
import { createResult } from '../../core/utils.js';
|
|
18
16
|
import '../../app/hook-use-api.js';
|
|
19
17
|
import '../../app/message-api.js';
|
|
20
18
|
import '../../app/message-format.js';
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
19
|
+
import { apiResolves, apiTimeouts, actionResolves, actionTimeouts, FULL_RECEIVE_HEADER } from '../processor/config.js';
|
|
20
|
+
import { createWSConnector } from './base.js';
|
|
23
21
|
|
|
24
22
|
const cbpClient = (url, options = {}) => {
|
|
25
|
-
if (global.chatbotClient) {
|
|
26
|
-
delete global.chatbotClient;
|
|
27
|
-
}
|
|
28
23
|
const { open = () => { }, isFullReceive = true } = options;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
24
|
+
createWSConnector({
|
|
25
|
+
url,
|
|
26
|
+
role: 'client',
|
|
27
|
+
globalKey: 'chatbotClient',
|
|
28
|
+
extraHeaders: {
|
|
29
|
+
[FULL_RECEIVE_HEADER]: isFullReceive ? '1' : '0'
|
|
32
30
|
},
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
},
|
|
36
|
-
terminate: () => {
|
|
31
|
+
onOpen: open,
|
|
32
|
+
onMessage: (messageStr) => {
|
|
37
33
|
try {
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
catch (error) {
|
|
41
|
-
logger.debug({
|
|
42
|
-
code: ResultCode.Fail,
|
|
43
|
-
message: '强制断开连接失败',
|
|
44
|
-
data: error
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
const start = () => {
|
|
50
|
-
global.chatbotClient = new WebSocket(url, {
|
|
51
|
-
headers: {
|
|
52
|
-
[USER_AGENT_HEADER]: 'client',
|
|
53
|
-
[DEVICE_ID_HEADER]: deviceId,
|
|
54
|
-
[FULL_RECEIVE_HEADER]: isFullReceive ? '1' : '0'
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
global.chatbotClient.on('open', () => {
|
|
58
|
-
open();
|
|
59
|
-
heartbeatControl.start();
|
|
60
|
-
});
|
|
61
|
-
global.chatbotClient.on('pong', () => {
|
|
62
|
-
heartbeatControl.pong();
|
|
63
|
-
});
|
|
64
|
-
global.chatbotClient.on('message', message => {
|
|
65
|
-
try {
|
|
66
|
-
const parsedMessage = flattedJSON.parse(message.toString());
|
|
34
|
+
const parsedMessage = flattedJSON.parse(messageStr);
|
|
67
35
|
if (parsedMessage?.activeId) {
|
|
68
36
|
if (parsedMessage.active === 'sync') {
|
|
69
37
|
const configs = parsedMessage.payload;
|
|
@@ -118,28 +86,8 @@ const cbpClient = (url, options = {}) => {
|
|
|
118
86
|
data: error
|
|
119
87
|
});
|
|
120
88
|
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
heartbeatControl.stop();
|
|
124
|
-
logger.warn({
|
|
125
|
-
code: ResultCode.Fail,
|
|
126
|
-
message: '连接关闭,尝试重新连接...',
|
|
127
|
-
data: null
|
|
128
|
-
});
|
|
129
|
-
delete global.chatbotClient;
|
|
130
|
-
setTimeout(() => {
|
|
131
|
-
start();
|
|
132
|
-
}, reconnectInterval);
|
|
133
|
-
});
|
|
134
|
-
global.chatbotClient.on('error', err => {
|
|
135
|
-
logger.error({
|
|
136
|
-
code: ResultCode.Fail,
|
|
137
|
-
message: '客户端错误',
|
|
138
|
-
data: err
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
};
|
|
142
|
-
start();
|
|
89
|
+
}
|
|
90
|
+
});
|
|
143
91
|
};
|
|
144
92
|
|
|
145
93
|
export { cbpClient };
|
|
@@ -1,40 +1,17 @@
|
|
|
1
1
|
import * as flattedJSON from 'flatted';
|
|
2
2
|
import { WebSocket } from 'ws';
|
|
3
|
-
import { deviceId
|
|
3
|
+
import { deviceId } from '../processor/config.js';
|
|
4
4
|
import { ResultCode } from '../../core/variable.js';
|
|
5
5
|
import 'fs';
|
|
6
6
|
import 'path';
|
|
7
7
|
import 'yaml';
|
|
8
8
|
import '../../core/utils.js';
|
|
9
|
-
import {
|
|
9
|
+
import { createWSConnector } from './base.js';
|
|
10
10
|
|
|
11
11
|
const cbpPlatform = (url, options = {
|
|
12
12
|
open: () => { }
|
|
13
13
|
}) => {
|
|
14
|
-
if (global.chatbotPlatform) {
|
|
15
|
-
delete global.chatbotPlatform;
|
|
16
|
-
}
|
|
17
14
|
const { open = () => { } } = options;
|
|
18
|
-
const [heartbeatControl] = useHeartbeat({
|
|
19
|
-
ping: () => {
|
|
20
|
-
global?.chatbotPlatform?.ping?.();
|
|
21
|
-
},
|
|
22
|
-
isConnected: () => {
|
|
23
|
-
return global?.chatbotPlatform && global?.chatbotPlatform?.readyState === WebSocket.OPEN;
|
|
24
|
-
},
|
|
25
|
-
terminate: () => {
|
|
26
|
-
try {
|
|
27
|
-
global?.chatbotPlatform?.terminate?.();
|
|
28
|
-
}
|
|
29
|
-
catch (error) {
|
|
30
|
-
logger.debug({
|
|
31
|
-
code: ResultCode.Fail,
|
|
32
|
-
message: '强制断开连接失败',
|
|
33
|
-
data: error
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
15
|
const send = (data) => {
|
|
39
16
|
if (global.chatbotPlatform && global.chatbotPlatform.readyState === WebSocket.OPEN) {
|
|
40
17
|
data.DeviceId = deviceId;
|
|
@@ -69,23 +46,14 @@ const cbpPlatform = (url, options = {
|
|
|
69
46
|
const onapis = (reply) => {
|
|
70
47
|
apiReplys.push(reply);
|
|
71
48
|
};
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
});
|
|
79
|
-
global.chatbotPlatform.on('open', () => {
|
|
80
|
-
open();
|
|
81
|
-
heartbeatControl.start();
|
|
82
|
-
});
|
|
83
|
-
global.chatbotPlatform.on('pong', () => {
|
|
84
|
-
heartbeatControl.pong();
|
|
85
|
-
});
|
|
86
|
-
global.chatbotPlatform.on('message', message => {
|
|
49
|
+
createWSConnector({
|
|
50
|
+
url,
|
|
51
|
+
role: 'platform',
|
|
52
|
+
globalKey: 'chatbotPlatform',
|
|
53
|
+
onOpen: open,
|
|
54
|
+
onMessage: (messageStr) => {
|
|
87
55
|
try {
|
|
88
|
-
const data = flattedJSON.parse(
|
|
56
|
+
const data = flattedJSON.parse(messageStr);
|
|
89
57
|
if (data.apiId) {
|
|
90
58
|
for (const cb of apiReplys) {
|
|
91
59
|
void cb(data, val => replyApi(data, val));
|
|
@@ -104,28 +72,8 @@ const cbpPlatform = (url, options = {
|
|
|
104
72
|
data: error
|
|
105
73
|
});
|
|
106
74
|
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
heartbeatControl.stop();
|
|
110
|
-
logger.warn({
|
|
111
|
-
code: ResultCode.Fail,
|
|
112
|
-
message: '平台端连接关闭,尝试重新连接...',
|
|
113
|
-
data: err
|
|
114
|
-
});
|
|
115
|
-
delete global.chatbotPlatform;
|
|
116
|
-
setTimeout(() => {
|
|
117
|
-
start();
|
|
118
|
-
}, reconnectInterval);
|
|
119
|
-
});
|
|
120
|
-
global.chatbotPlatform.on('error', err => {
|
|
121
|
-
logger.error({
|
|
122
|
-
code: ResultCode.Fail,
|
|
123
|
-
message: '平台端错误',
|
|
124
|
-
data: err
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
};
|
|
128
|
-
start();
|
|
75
|
+
}
|
|
76
|
+
});
|
|
129
77
|
const client = {
|
|
130
78
|
send,
|
|
131
79
|
onactions,
|
package/lib/cbp/server/main.js
CHANGED
|
@@ -11,27 +11,7 @@ import '../../core/utils.js';
|
|
|
11
11
|
import { USER_AGENT_HEADER, USER_AGENT_HEADER_VALUE_MAP, DEVICE_ID_HEADER, FULL_RECEIVE_HEADER, platformClient, childrenClient, fullClient, childrenBind } from '../processor/config.js';
|
|
12
12
|
import { createTestOneController } from './testone.js';
|
|
13
13
|
|
|
14
|
-
const
|
|
15
|
-
if (childrenClient.has(DeviceId)) {
|
|
16
|
-
const clientWs = childrenClient.get(DeviceId);
|
|
17
|
-
if (clientWs && clientWs.readyState === WebSocket.OPEN) {
|
|
18
|
-
clientWs.send(message);
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
childrenClient.delete(DeviceId);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
else if (fullClient.has(DeviceId)) {
|
|
25
|
-
const clientWs = fullClient.get(DeviceId);
|
|
26
|
-
if (clientWs && clientWs.readyState === WebSocket.OPEN) {
|
|
27
|
-
clientWs.send(message);
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
fullClient.delete(DeviceId);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
const handleAction = (DeviceId, message) => {
|
|
14
|
+
const routeMessageToDevice = (DeviceId, message) => {
|
|
35
15
|
if (childrenClient.has(DeviceId)) {
|
|
36
16
|
const clientWs = childrenClient.get(DeviceId);
|
|
37
17
|
if (clientWs && clientWs.readyState === WebSocket.OPEN) {
|
|
@@ -209,11 +189,11 @@ const setPlatformClient = (originId, ws) => {
|
|
|
209
189
|
});
|
|
210
190
|
if (parsedMessage.apiId) {
|
|
211
191
|
const DeviceId = parsedMessage.DeviceId;
|
|
212
|
-
|
|
192
|
+
routeMessageToDevice(DeviceId, message);
|
|
213
193
|
}
|
|
214
194
|
else if (parsedMessage?.actionId) {
|
|
215
195
|
const DeviceId = parsedMessage.DeviceId;
|
|
216
|
-
|
|
196
|
+
routeMessageToDevice(DeviceId, message);
|
|
217
197
|
}
|
|
218
198
|
else if (parsedMessage?.name) {
|
|
219
199
|
const ID = parsedMessage.ChannelId || parsedMessage.GuildId || parsedMessage.DeviceId;
|
|
@@ -260,11 +240,11 @@ const setTestOnePlatformClient = (ws) => {
|
|
|
260
240
|
});
|
|
261
241
|
if (parsedMessage.apiId) {
|
|
262
242
|
const DeviceId = parsedMessage.DeviceId;
|
|
263
|
-
|
|
243
|
+
routeMessageToDevice(DeviceId, message);
|
|
264
244
|
}
|
|
265
245
|
else if (parsedMessage?.actionId) {
|
|
266
246
|
const DeviceId = parsedMessage.DeviceId;
|
|
267
|
-
|
|
247
|
+
routeMessageToDevice(DeviceId, message);
|
|
268
248
|
}
|
|
269
249
|
else if (parsedMessage?.name) {
|
|
270
250
|
const ID = parsedMessage.ChannelId || parsedMessage.GuildId || parsedMessage.DeviceId;
|
package/lib/client.js
CHANGED
|
@@ -23,7 +23,6 @@ import './app/event-group.js';
|
|
|
23
23
|
import './app/event-middleware.js';
|
|
24
24
|
import './app/event-processor.js';
|
|
25
25
|
import './app/event-response.js';
|
|
26
|
-
import 'util/types';
|
|
27
26
|
import './app/hook-use-api.js';
|
|
28
27
|
import './app/message-api.js';
|
|
29
28
|
import './app/message-format.js';
|