@zhin.js/core 1.0.17 → 1.0.18
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/CHANGELOG.md +11 -0
- package/REFACTORING_COMPLETE.md +178 -0
- package/REFACTORING_STATUS.md +263 -0
- package/lib/adapter.d.ts +44 -19
- package/lib/adapter.d.ts.map +1 -1
- package/lib/adapter.js +81 -50
- package/lib/adapter.js.map +1 -1
- package/lib/bot.d.ts +7 -12
- package/lib/bot.d.ts.map +1 -1
- package/lib/built/adapter-process.d.ts +36 -0
- package/lib/built/adapter-process.d.ts.map +1 -0
- package/lib/built/adapter-process.js +77 -0
- package/lib/built/adapter-process.js.map +1 -0
- package/lib/built/command.d.ts +46 -0
- package/lib/built/command.d.ts.map +1 -0
- package/lib/built/command.js +54 -0
- package/lib/built/command.js.map +1 -0
- package/lib/built/component.d.ts +42 -0
- package/lib/built/component.d.ts.map +1 -0
- package/lib/built/component.js +66 -0
- package/lib/built/component.js.map +1 -0
- package/lib/built/config.d.ts +31 -0
- package/lib/built/config.d.ts.map +1 -0
- package/lib/built/config.js +141 -0
- package/lib/built/config.js.map +1 -0
- package/lib/built/cron.d.ts +53 -0
- package/lib/built/cron.d.ts.map +1 -0
- package/lib/built/cron.js +79 -0
- package/lib/built/cron.js.map +1 -0
- package/lib/built/database.d.ts +17 -0
- package/lib/built/database.d.ts.map +1 -0
- package/lib/built/database.js +28 -0
- package/lib/built/database.js.map +1 -0
- package/lib/{permissions.d.ts → built/permission.d.ts} +5 -10
- package/lib/built/permission.d.ts.map +1 -0
- package/lib/{permissions.js → built/permission.js} +11 -10
- package/lib/built/permission.js.map +1 -0
- package/lib/command.d.ts +7 -7
- package/lib/command.d.ts.map +1 -1
- package/lib/command.js +5 -15
- package/lib/command.js.map +1 -1
- package/lib/component.d.ts.map +1 -1
- package/lib/component.js.map +1 -1
- package/lib/cron.d.ts +1 -0
- package/lib/cron.d.ts.map +1 -1
- package/lib/cron.js +2 -0
- package/lib/cron.js.map +1 -1
- package/lib/index.d.ts +11 -3
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +14 -4
- package/lib/index.js.map +1 -1
- package/lib/jsx-runtime.d.ts +2 -2
- package/lib/message.d.ts +2 -2
- package/lib/message.d.ts.map +1 -1
- package/lib/plugin.d.ts +164 -51
- package/lib/plugin.d.ts.map +1 -1
- package/lib/plugin.js +521 -150
- package/lib/plugin.js.map +1 -1
- package/lib/prompt.d.ts +1 -1
- package/lib/prompt.d.ts.map +1 -1
- package/lib/prompt.js +2 -1
- package/lib/prompt.js.map +1 -1
- package/lib/types.d.ts +33 -33
- package/lib/types.d.ts.map +1 -1
- package/lib/utils.d.ts +16 -1
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +166 -66
- package/lib/utils.js.map +1 -1
- package/package.json +15 -9
- package/src/adapter.ts +131 -80
- package/src/bot.ts +8 -13
- package/src/built/adapter-process.ts +77 -0
- package/src/built/command.ts +102 -0
- package/src/built/component.ts +111 -0
- package/src/built/config.ts +126 -0
- package/src/built/cron.ts +140 -0
- package/src/built/database.ts +38 -0
- package/src/{permissions.ts → built/permission.ts} +9 -12
- package/src/command.ts +11 -20
- package/src/component.ts +0 -1
- package/src/cron.ts +2 -0
- package/src/index.ts +15 -5
- package/src/message.ts +2 -2
- package/src/plugin.ts +671 -202
- package/src/prompt.ts +4 -3
- package/src/types.ts +41 -35
- package/src/utils.ts +418 -296
- package/test/minimal-bot.ts +31 -0
- package/test/stress-test.ts +123 -0
- package/tests/command.test.ts +47 -44
- package/ASYNC-JSX-SUPPORT.md +0 -173
- package/lib/app.d.ts +0 -191
- package/lib/app.d.ts.map +0 -1
- package/lib/app.js +0 -604
- package/lib/app.js.map +0 -1
- package/lib/config.d.ts +0 -54
- package/lib/config.d.ts.map +0 -1
- package/lib/config.js +0 -308
- package/lib/config.js.map +0 -1
- package/lib/log-transport.d.ts +0 -37
- package/lib/log-transport.d.ts.map +0 -1
- package/lib/log-transport.js +0 -136
- package/lib/log-transport.js.map +0 -1
- package/lib/permissions.d.ts.map +0 -1
- package/lib/permissions.js.map +0 -1
- package/src/app.ts +0 -772
- package/src/config.ts +0 -397
- package/src/log-transport.ts +0 -163
- package/tests/app.test.ts +0 -265
- package/tests/permissions.test.ts +0 -358
- package/tests/plugin.test.ts +0 -234
- package/tests/prompt.test.ts +0 -223
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cron Context
|
|
3
|
+
* 管理所有插件注册的定时任务
|
|
4
|
+
*/
|
|
5
|
+
import { Cron } from "../cron.js";
|
|
6
|
+
import { Context, Plugin, getPlugin } from "../plugin.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* CronContext 扩展方法类型
|
|
10
|
+
*/
|
|
11
|
+
export interface CronContextExtensions {
|
|
12
|
+
/** 添加定时任务 */
|
|
13
|
+
addCron(cron: Cron): () => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// 扩展 Plugin 接口
|
|
17
|
+
declare module "../plugin.js" {
|
|
18
|
+
namespace Plugin {
|
|
19
|
+
interface Extensions extends CronContextExtensions {}
|
|
20
|
+
interface Contexts {
|
|
21
|
+
cron: CronService;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 定时任务服务数据
|
|
28
|
+
*/
|
|
29
|
+
export interface CronService {
|
|
30
|
+
/** 任务列表 */
|
|
31
|
+
readonly items: Cron[];
|
|
32
|
+
/** 按 ID/表达式 索引 */
|
|
33
|
+
readonly byName: Map<string, Cron>;
|
|
34
|
+
/** 添加任务 */
|
|
35
|
+
add(cron: Cron, pluginName: string): () => void;
|
|
36
|
+
/** 移除任务 */
|
|
37
|
+
remove(cron: Cron): boolean;
|
|
38
|
+
/** 按名称获取 */
|
|
39
|
+
get(id: string): Cron | undefined;
|
|
40
|
+
/** 停止所有任务 */
|
|
41
|
+
stopAll(): void;
|
|
42
|
+
/** 启动所有任务 */
|
|
43
|
+
startAll(): void;
|
|
44
|
+
/** 获取状态 */
|
|
45
|
+
getStatus(): Array<{
|
|
46
|
+
expression: string;
|
|
47
|
+
running: boolean;
|
|
48
|
+
nextExecution: Date | null;
|
|
49
|
+
plugin: string;
|
|
50
|
+
}>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 创建定时任务 Context
|
|
55
|
+
*/
|
|
56
|
+
export function createCronService(): Context<'cron', CronContextExtensions> {
|
|
57
|
+
const items: Cron[] = [];
|
|
58
|
+
const byName = new Map<string, Cron>();
|
|
59
|
+
const pluginMap = new Map<Cron, string>();
|
|
60
|
+
|
|
61
|
+
const value: CronService = {
|
|
62
|
+
items,
|
|
63
|
+
byName,
|
|
64
|
+
|
|
65
|
+
add(cron, pluginName) {
|
|
66
|
+
items.push(cron);
|
|
67
|
+
byName.set(cron.id || cron.cronExpression, cron);
|
|
68
|
+
pluginMap.set(cron, pluginName);
|
|
69
|
+
// 自动启动
|
|
70
|
+
if (!cron.running) {
|
|
71
|
+
cron.run();
|
|
72
|
+
}
|
|
73
|
+
return () => value.remove(cron);
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
remove(cron) {
|
|
77
|
+
const index = items.indexOf(cron);
|
|
78
|
+
if (index !== -1) {
|
|
79
|
+
// 自动停止
|
|
80
|
+
if (cron.running) {
|
|
81
|
+
cron.stop();
|
|
82
|
+
}
|
|
83
|
+
items.splice(index, 1);
|
|
84
|
+
byName.delete(cron.id || cron.cronExpression);
|
|
85
|
+
pluginMap.delete(cron);
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
return false;
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
get(id) {
|
|
92
|
+
return byName.get(id);
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
stopAll() {
|
|
96
|
+
for (const cron of items) {
|
|
97
|
+
if (cron.running) {
|
|
98
|
+
cron.stop();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
startAll() {
|
|
104
|
+
for (const cron of items) {
|
|
105
|
+
if (!cron.running && !cron.disposed) {
|
|
106
|
+
cron.run();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
getStatus() {
|
|
112
|
+
return items.map(cron => ({
|
|
113
|
+
expression: cron.cronExpression,
|
|
114
|
+
running: cron.running,
|
|
115
|
+
nextExecution: cron.running ? cron.getNextExecutionTime() : null,
|
|
116
|
+
plugin: pluginMap.get(cron) || 'unknown',
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
name: 'cron',
|
|
123
|
+
description: '定时任务服务',
|
|
124
|
+
value,
|
|
125
|
+
|
|
126
|
+
dispose() {
|
|
127
|
+
value.stopAll();
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
extensions: {
|
|
131
|
+
addCron(cron: Cron) {
|
|
132
|
+
const plugin = getPlugin();
|
|
133
|
+
const dispose = value.add(cron, plugin.name);
|
|
134
|
+
plugin.onDispose(dispose);
|
|
135
|
+
return dispose;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Registry,Definition,Databases,Database } from "@zhin.js/database";
|
|
2
|
+
import { DatabaseConfig,Models } from "../types.js";
|
|
3
|
+
import { defineContext,Plugin } from "../plugin.js";
|
|
4
|
+
import { SystemLogDefinition } from "../models/system-log.js";
|
|
5
|
+
import { UserDefinition } from "../models/user.js";
|
|
6
|
+
declare module "../plugin" {
|
|
7
|
+
namespace Plugin {
|
|
8
|
+
interface Extensions {
|
|
9
|
+
defineModel<K extends keyof Models>(name: K, definition: Definition<Models[K]>): void;
|
|
10
|
+
}
|
|
11
|
+
interface Contexts {
|
|
12
|
+
database: Database<any,Models,any>;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function defineDatabaseService<K extends keyof Databases>(config: DatabaseConfig<K>) {
|
|
17
|
+
const db = Registry.create<Models,K>(config.dialect, config,{
|
|
18
|
+
SystemLog: SystemLogDefinition,
|
|
19
|
+
User: UserDefinition,
|
|
20
|
+
});
|
|
21
|
+
return defineContext({
|
|
22
|
+
name: 'database',
|
|
23
|
+
description: '数据库服务',
|
|
24
|
+
mounted: async () => {
|
|
25
|
+
await db.start();
|
|
26
|
+
return db;
|
|
27
|
+
},
|
|
28
|
+
dispose: async () => {
|
|
29
|
+
await db.stop();
|
|
30
|
+
},
|
|
31
|
+
extensions: {
|
|
32
|
+
defineModel<K extends keyof Models>(this:Plugin,name: K, definition: Definition<Models[K]>) {
|
|
33
|
+
db.define(name, definition);
|
|
34
|
+
this.logger.debug(`Model "${String(name)}" defined by plugin "${this.name}"`);
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
}
|
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
import { MaybePromise } from "@zhin.js/
|
|
2
|
-
import { RegisteredAdapter } from "./types.js";
|
|
3
|
-
import { Message } from "./message.js";
|
|
4
|
-
import { AdapterMessage } from "./types.js";
|
|
5
|
-
import { App } from "./app.js";
|
|
1
|
+
import { MaybePromise, RegisteredAdapter, Message, AdapterMessage } from "@zhin.js/core";
|
|
6
2
|
export type PermissionItem<T extends RegisteredAdapter = RegisteredAdapter> = {
|
|
7
3
|
name: string | RegExp
|
|
8
4
|
check: PermissionChecker<T>
|
|
9
5
|
}
|
|
10
6
|
export type PermissionChecker<T extends RegisteredAdapter = RegisteredAdapter> = (name: string, message: Message<AdapterMessage<T>>) => MaybePromise<boolean>
|
|
11
|
-
export class
|
|
12
|
-
constructor(
|
|
7
|
+
export class PermissionService extends Array<PermissionItem>{
|
|
8
|
+
constructor() {
|
|
13
9
|
super();
|
|
14
10
|
this.add(Permissions.define(/^adapter\([^)]+\)$/, (name, message) => {
|
|
15
11
|
return message.$adapter === name.replace(/^adapter\(([^)]+)\)$/, '$1');
|
|
@@ -48,11 +44,12 @@ export class Permissions extends Array<PermissionItem>{
|
|
|
48
44
|
add(permission: PermissionItem) {
|
|
49
45
|
this.push(permission);
|
|
50
46
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return
|
|
55
|
-
}
|
|
47
|
+
async check(name: string, message: Message<AdapterMessage<RegisteredAdapter>>):Promise<boolean> {
|
|
48
|
+
for(const permission of this){
|
|
49
|
+
const passed=await permission.check(name,message);
|
|
50
|
+
if(passed) return true;
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
56
53
|
}
|
|
57
54
|
}
|
|
58
55
|
export namespace Permissions{
|
package/src/command.ts
CHANGED
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
import {MatchResult, SegmentMatcher} from "segment-matcher";
|
|
2
2
|
import {AdapterMessage, SendContent} from "./types.js";
|
|
3
|
-
import {
|
|
3
|
+
import {RegisteredAdapter} from "./types.js";
|
|
4
4
|
import type {Message} from "./message.js";
|
|
5
|
-
import {MaybePromise} from "
|
|
6
|
-
import {
|
|
7
|
-
import { App } from "./app.js";
|
|
5
|
+
import {MaybePromise} from "./types.js";
|
|
6
|
+
import {Plugin} from "./plugin.js";
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* MessageCommand类:命令系统核心,基于segment-matcher实现。
|
|
11
10
|
* 支持多平台命令注册、作用域限制、参数解析、异步处理等。
|
|
12
11
|
*/
|
|
13
|
-
export class MessageCommand<T extends
|
|
12
|
+
export class MessageCommand<T extends RegisteredAdapter=RegisteredAdapter> extends SegmentMatcher{
|
|
14
13
|
#callbacks:MessageCommand.Callback<T>[]=[];
|
|
15
14
|
#desc:string[]=[];
|
|
16
15
|
#usage:string[]=[];
|
|
17
16
|
#examples:string[]=[];
|
|
18
17
|
#permissions:string[]=[];
|
|
19
|
-
#checkers:MessageCommand.Checker<T>[]=[]
|
|
20
18
|
get helpInfo():MessageCommand.HelpInfo{
|
|
21
19
|
return {
|
|
22
20
|
pattern: this.pattern,
|
|
@@ -63,18 +61,11 @@ export class MessageCommand<T extends keyof RegisteredAdapters=keyof RegisteredA
|
|
|
63
61
|
* @param plugin 插件实例
|
|
64
62
|
* @returns 命令返回内容或undefined
|
|
65
63
|
*/
|
|
66
|
-
async handle(message:Message<AdapterMessage<T>>,
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
const result=await permit.check(permission,message)
|
|
73
|
-
if(!result) return;
|
|
74
|
-
}
|
|
75
|
-
for(const check of this.#checkers){
|
|
76
|
-
const result=await check(message)
|
|
77
|
-
if(!result) return;
|
|
64
|
+
async handle(message:Message<AdapterMessage<T>>,plugin:Plugin):Promise<SendContent|undefined>{
|
|
65
|
+
const auth = plugin.contextIsReady('permission') ? plugin.inject('permission') : null
|
|
66
|
+
for(const permit of this.#permissions){
|
|
67
|
+
const passed=await auth?.check(permit,message)
|
|
68
|
+
if(!passed) return;
|
|
78
69
|
}
|
|
79
70
|
const matched=this.match(message.$content);
|
|
80
71
|
if(!matched) return
|
|
@@ -85,8 +76,8 @@ export class MessageCommand<T extends keyof RegisteredAdapters=keyof RegisteredA
|
|
|
85
76
|
}
|
|
86
77
|
}
|
|
87
78
|
export namespace MessageCommand{
|
|
88
|
-
export type Callback<T extends
|
|
89
|
-
export type Checker<T extends
|
|
79
|
+
export type Callback<T extends RegisteredAdapter>=(message:Message<AdapterMessage<T>>,result:MatchResult)=>SendContent|undefined|Promise<SendContent|undefined>;
|
|
80
|
+
export type Checker<T extends RegisteredAdapter>=(message:Message<AdapterMessage<T>>)=>MaybePromise<boolean>
|
|
90
81
|
export type HelpInfo={
|
|
91
82
|
pattern:string;
|
|
92
83
|
desc:string[];
|
package/src/component.ts
CHANGED
package/src/cron.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { Cron as Croner } from 'croner';
|
|
|
6
6
|
*/
|
|
7
7
|
export class Cron {
|
|
8
8
|
private job: Croner | null = null;
|
|
9
|
+
id:string = '';
|
|
9
10
|
private callback: () => void | Promise<void>;
|
|
10
11
|
private isDisposed = false;
|
|
11
12
|
private _cronExpression: string;
|
|
@@ -16,6 +17,7 @@ export class Cron {
|
|
|
16
17
|
* @param callback - 要执行的回调函数
|
|
17
18
|
*/
|
|
18
19
|
constructor(cronExpression: string, callback: () => void | Promise<void>) {
|
|
20
|
+
this.id=Math.random().toString(36).substring(2, 10);
|
|
19
21
|
try {
|
|
20
22
|
this._cronExpression = cronExpression;
|
|
21
23
|
this.callback = callback;
|
package/src/index.ts
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
// Core exports
|
|
2
|
-
export * from './app.js'
|
|
3
2
|
export * from './bot.js'
|
|
4
3
|
export * from './plugin.js'
|
|
5
4
|
export * from './command.js'
|
|
6
5
|
export * from './component.js'
|
|
7
6
|
export * from './adapter.js'
|
|
8
|
-
export * from './config.js'
|
|
9
7
|
export * from './message.js'
|
|
10
|
-
|
|
8
|
+
export * from './prompt.js'
|
|
9
|
+
// Models
|
|
10
|
+
export * from './models/system-log.js'
|
|
11
|
+
export * from './models/user.js'
|
|
12
|
+
// Built-in Contexts
|
|
13
|
+
export * from './built/config.js'
|
|
14
|
+
export * from './built/command.js'
|
|
15
|
+
export * from './built/cron.js'
|
|
16
|
+
export * from './built/permission.js'
|
|
17
|
+
export * from './built/adapter-process.js'
|
|
18
|
+
export * from './built/component.js'
|
|
19
|
+
export * from './built/database.js'
|
|
20
|
+
|
|
11
21
|
export * from './types.js'
|
|
12
22
|
export * from './utils.js'
|
|
13
23
|
export * from './errors.js' // 导出错误处理系统
|
|
14
24
|
export * from './cron.js'
|
|
15
25
|
export * from '@zhin.js/database'
|
|
16
26
|
export * from '@zhin.js/logger'
|
|
17
|
-
|
|
18
|
-
export {
|
|
27
|
+
// 只导出 Schema 类,避免与 utils.js 的 isEmpty 冲突
|
|
28
|
+
export { Schema } from '@zhin.js/schema'
|
package/src/message.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {MessageElement, MessageSender, SendContent} from "./types";
|
|
1
|
+
import {MessageElement, MessageSender, RegisteredAdapter, SendContent} from "./types";
|
|
2
2
|
import { Component } from "./component.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -24,7 +24,7 @@ export type MessageType = 'group' | 'private' | 'channel'
|
|
|
24
24
|
*/
|
|
25
25
|
export interface MessageBase {
|
|
26
26
|
$id: string;
|
|
27
|
-
$adapter:
|
|
27
|
+
$adapter:RegisteredAdapter
|
|
28
28
|
$bot:string
|
|
29
29
|
$content: MessageElement[];
|
|
30
30
|
$sender: MessageSender;
|