alemonjs 2.1.0-alpha.52 → 2.1.0-alpha.53

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.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ type AppOptions = {
2
+ platform?: string;
3
+ login?: string;
4
+ url?: string;
5
+ is_full_receive?: boolean;
6
+ };
7
+ export declare class App {
8
+ private config;
9
+ private input;
10
+ constructor(config?: AppOptions);
11
+ private createOptionsByKey;
12
+ private startPlatform;
13
+ private startClient;
14
+ private extractModulePathForLogging;
15
+ launch(moduleImport: () => Promise<any>): void;
16
+ listen(port?: number, callback?: () => void): void;
17
+ }
18
+ export {};
package/lib/app.js ADDED
@@ -0,0 +1,82 @@
1
+ import { getConfig } from './core/config.js';
2
+ import { cbpServer } from './cbp/server/main.js';
3
+ import { filePrefixCommon, defaultPlatformCommonPrefix, defaultPort } from './core/variable.js';
4
+ import { startPlatformAdapterWithFallback } from './process/platform.js';
5
+ import { startModuleAdapter } from './process/module.js';
6
+
7
+ class App {
8
+ config;
9
+ input = '';
10
+ constructor(config = {}) {
11
+ this.config = {
12
+ platform: config.platform,
13
+ login: config.login,
14
+ url: config.url,
15
+ is_full_receive: config.is_full_receive ?? true
16
+ };
17
+ }
18
+ createOptionsByKey(key, defaultValue) {
19
+ const cfg = getConfig();
20
+ const configValue = this.config[key];
21
+ const argValue = cfg.argv?.[key];
22
+ return configValue ?? argValue ?? cfg.value?.[key] ?? defaultValue;
23
+ }
24
+ startPlatform() {
25
+ const platform = this.createOptionsByKey('platform');
26
+ const login = this.createOptionsByKey('login');
27
+ if (!platform && !login) {
28
+ global.sandbox = true;
29
+ logger.info('[App] Running in sandbox mode');
30
+ return;
31
+ }
32
+ if (platform) {
33
+ const reg = filePrefixCommon;
34
+ if (reg.test(platform)) {
35
+ process.env.platform = platform;
36
+ process.env.login = platform.replace(reg, '');
37
+ }
38
+ else {
39
+ process.env.platform = platform;
40
+ process.env.login = platform;
41
+ }
42
+ }
43
+ else {
44
+ process.env.platform = `${defaultPlatformCommonPrefix}${login}`;
45
+ process.env.login = login;
46
+ }
47
+ if (login) {
48
+ process.env.login = login;
49
+ }
50
+ logger.info(`[App] Starting platform adapter: ${process.env.platform}`);
51
+ startPlatformAdapterWithFallback();
52
+ }
53
+ startClient() {
54
+ process.env.input = this.input ?? this.createOptionsByKey('input');
55
+ process.env.output = this.createOptionsByKey('output');
56
+ process.env.is_full_receive = String(this.createOptionsByKey('is_full_receive', true));
57
+ process.env.port = String(this.createOptionsByKey('port', defaultPort));
58
+ process.env.url = this.createOptionsByKey('url');
59
+ logger.info('[App] Starting module adapter');
60
+ startModuleAdapter();
61
+ }
62
+ extractModulePathForLogging(moduleImport) {
63
+ const functionString = moduleImport.toString();
64
+ const match = functionString.match(/import\(['"]([^'"]+)['"]\)/);
65
+ return match ? match[1] : 'unknown-module';
66
+ }
67
+ launch(moduleImport) {
68
+ const input = this.extractModulePathForLogging(moduleImport);
69
+ this.input = input;
70
+ }
71
+ listen(port, callback) {
72
+ const actualPort = port ?? this.createOptionsByKey('port', defaultPort);
73
+ process.env.port = String(actualPort);
74
+ cbpServer(actualPort, () => {
75
+ this.startClient();
76
+ this.startPlatform();
77
+ callback?.();
78
+ });
79
+ }
80
+ }
81
+
82
+ export { App };
package/lib/index.d.ts CHANGED
@@ -4,3 +4,4 @@ export * from './core/index.js';
4
4
  export * from './cbp/index.js';
5
5
  export * from './app/index.js';
6
6
  export * from './main.js';
7
+ export * from './app.js';
package/lib/index.js CHANGED
@@ -25,3 +25,4 @@ export { useObserver, useSubscribe } from './app/hook-use-subscribe.js';
25
25
  export { createDataFormat, createEventValue, format, sendToChannel, sendToUser } from './app/message-api.js';
26
26
  export { Ark, BT, Image, ImageFile, ImageURL, Link, MD, Mention, Text } from './app/message-format.js';
27
27
  export { start } from './main.js';
28
+ export { App } from './app.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "alemonjs",
3
- "version": "2.1.0-alpha.52",
3
+ "version": "2.1.0-alpha.53",
4
4
  "description": "bot script",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",