@zhin.js/http 1.0.5 → 1.0.6

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/src/index.ts DELETED
@@ -1,682 +0,0 @@
1
- import {AppConfig, register, useApp,useDatabase} from '@zhin.js/core';
2
- import { createServer, Server } from 'http';
3
- import os from 'node:os';
4
- import Koa from 'koa';
5
- import auth from 'koa-basic-auth';
6
- import KoaBodyParser from 'koa-bodyparser';
7
- import { Router } from './router.js';
8
- import * as process from 'process';
9
- import { config } from 'node:process';
10
-
11
- export * from './router.js';
12
-
13
- declare module '@zhin.js/types'{
14
- interface GlobalContext {
15
- koa: Koa,
16
- router: Router,
17
- server: Server
18
- }
19
- }
20
-
21
- const koa = new Koa();
22
- const server = createServer(koa.callback())
23
- const router = new Router(server, { prefix: process.env.routerPrefix || '' });
24
- // 获取当前计算机登录用户名
25
- const getCurrentUsername = () => {
26
- try {
27
- return os.userInfo().username;
28
- } catch {
29
- return 'admin'; // 如果获取失败,使用默认用户名
30
- }
31
- };
32
-
33
- // 生成6位随机密码
34
- const generateRandomPassword = () => {
35
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
36
- let result = '';
37
- for (let i = 0; i < 6; i++) {
38
- result += chars.charAt(Math.floor(Math.random() * chars.length));
39
- }
40
- return result;
41
- };
42
-
43
- const username = process.env.username || getCurrentUsername();
44
- const password = process.env.password || generateRandomPassword();
45
- const app=useApp()
46
-
47
- koa.use(
48
- auth({
49
- name: username,
50
- pass: password,
51
- }),
52
- );
53
- // ============================================================================
54
- // API 路由
55
- // ============================================================================
56
-
57
- // 系统状态 API
58
- router.get('/api/system/status', async (ctx) => {
59
- try {
60
- ctx.body = {
61
- success: true,
62
- data: {
63
- uptime: process.uptime(),
64
- memory: process.memoryUsage(),
65
- cpu: process.cpuUsage(),
66
- platform: process.platform,
67
- nodeVersion: process.version,
68
- pid: process.pid,
69
- timestamp: new Date().toISOString()
70
- }
71
- }
72
- } catch (error) {
73
- ctx.status = 500
74
- ctx.body = { success: false, error: (error as Error).message }
75
- }
76
- })
77
-
78
- // 健康检查 API
79
- router.get('/api/health', async (ctx) => {
80
- ctx.body = {
81
- success: true,
82
- status: 'ok',
83
- timestamp: new Date().toISOString()
84
- }
85
- })
86
-
87
- // 统计信息 API
88
- router.get('/api/stats', async (ctx) => {
89
- try {
90
- // 统计插件数量
91
- const pluginCount = app.dependencyList.length
92
- const activePluginCount = app.dependencyList.filter(dep => (dep as any).mounted).length
93
-
94
- // 统计机器人数量
95
- let botCount = 0
96
- let onlineBotCount = 0
97
- for (const context of app.contextList) {
98
- const adapter = app.getContext(context.name)
99
- if (adapter && typeof adapter === 'object' && 'bots' in adapter) {
100
- const adapterBots = (adapter as any).bots
101
- if (adapterBots && adapterBots instanceof Map) {
102
- botCount += adapterBots.size
103
- for (const bot of adapterBots.values()) {
104
- if (bot.$connected) onlineBotCount++
105
- }
106
- }
107
- }
108
- }
109
-
110
- // 统计命令和组件
111
- let commandCount = 0
112
- let componentCount = 0
113
- for (const dep of app.dependencyList) {
114
- commandCount += dep.commands.length
115
- componentCount += dep.components.size
116
- }
117
-
118
- ctx.body = {
119
- success: true,
120
- data: {
121
- plugins: { total: pluginCount, active: activePluginCount },
122
- bots: { total: botCount, online: onlineBotCount },
123
- commands: commandCount,
124
- components: componentCount,
125
- uptime: process.uptime(),
126
- memory: process.memoryUsage().heapUsed / 1024 / 1024, // MB
127
- }
128
- }
129
- } catch (error) {
130
- ctx.status = 500
131
- ctx.body = { success: false, error: (error as Error).message }
132
- }
133
- })
134
-
135
- // 插件管理 API
136
- router.get('/api/plugins', async (ctx) => {
137
- try {
138
- // 获取详细的插件数据
139
- const plugins = app.dependencyList.map(dep => {
140
- return {
141
- name: dep.name,
142
- status: (dep as any).mounted ? 'active' : 'inactive',
143
- commandCount: dep.commands.length,
144
- componentCount: dep.components.size,
145
- middlewareCount: dep.middlewares.length,
146
- contextCount: dep.contexts.size,
147
- description: (dep as any).description || '无描述',
148
- }
149
- })
150
-
151
- ctx.body = { success: true, data: plugins, total: plugins.length }
152
- } catch (error) {
153
- ctx.status = 500
154
- ctx.body = { success: false, error: (error as Error).message }
155
- }
156
- })
157
-
158
- // 插件详情 API
159
- router.get('/api/plugins/:name', async (ctx) => {
160
- try {
161
- const pluginName = ctx.params.name
162
- const plugin = app.dependencyList.find(dep => dep.name === pluginName)
163
-
164
- if (!plugin) {
165
- ctx.status = 404
166
- ctx.body = { success: false, error: '插件不存在' }
167
- return
168
- }
169
-
170
- // 获取命令详情
171
- const commands = plugin.commands.map(cmd => ({
172
- name: cmd.pattern
173
- }))
174
-
175
- // 获取组件详情
176
- const components = Array.from(plugin.components.entries()).map(([name, comp]) => ({
177
- name,
178
- props: (comp as any).props || {},
179
- type: typeof comp
180
- }))
181
-
182
- // 获取中间件详情
183
- const middlewares = plugin.middlewares.map((_, index) => ({
184
- id: `middleware-${index}`,
185
- type: 'function'
186
- }))
187
-
188
- // 获取上下文详情
189
- const contexts = Array.from(plugin.contexts.entries()).map(([name, ctx]) => ({
190
- name,
191
- description: (ctx as any).description || '无描述'
192
- }))
193
-
194
- // 获取定时任务详情
195
- const crons = plugin.crons.map((cron, index) => ({
196
- id: `cron-${index}`,
197
- pattern: (cron as any).pattern || 'unknown',
198
- running: (cron as any).running || false
199
- }))
200
-
201
- // 获取数据模型详情
202
- const definitions = Array.from(plugin.definitions.entries()).map(([name, definition]) => ({
203
- name,
204
- fields: Object.keys(definition)
205
- }))
206
-
207
- ctx.body = {
208
- success: true,
209
- data: {
210
- name: plugin.name,
211
- filename: plugin.filename,
212
- status: (plugin as any).mounted ? 'active' : 'inactive',
213
- description: (plugin as any).description || '无描述',
214
- commands,
215
- components,
216
- middlewares,
217
- contexts,
218
- crons,
219
- definitions,
220
- statistics: {
221
- commandCount: commands.length,
222
- componentCount: components.length,
223
- middlewareCount: middlewares.length,
224
- contextCount: contexts.length,
225
- cronCount: crons.length,
226
- definitionCount: definitions.length
227
- }
228
- }
229
- }
230
- } catch (error) {
231
- ctx.status = 500
232
- ctx.body = { success: false, error: (error as Error).message }
233
- }
234
- })
235
-
236
- // 适配器和机器人状态 API
237
- router.get('/api/bots', async (ctx) => {
238
- try {
239
- const bots: any[] = []
240
-
241
- // 遍历所有上下文,查找适配器
242
- for (const context of app.contextList) {
243
- const adapter = app.getContext(context.name)
244
- if (adapter && typeof adapter === 'object' && 'bots' in adapter) {
245
- const adapterBots = (adapter as any).bots
246
- if (adapterBots && adapterBots instanceof Map) {
247
- for (const [botName, bot] of adapterBots.entries()) {
248
- bots.push({
249
- name: botName,
250
- adapter: context.name,
251
- connected: bot.$connected || false,
252
- status: bot.$connected ? 'online' : 'offline'
253
- // 移除 config 字段以保护隐私
254
- })
255
- }
256
- }
257
- }
258
- }
259
-
260
- ctx.body = { success: true, data: bots, total: bots.length }
261
- } catch (error) {
262
- ctx.status = 500
263
- ctx.body = { success: false, error: (error as Error).message }
264
- }
265
- })
266
-
267
- // 框架配置信息 API
268
- router.get('/api/config', async (ctx) => {
269
- try {
270
- const config = app.getConfig()
271
-
272
- ctx.body = { success: true, data: config }
273
- } catch (error) {
274
- ctx.status = 500
275
- ctx.body = { success: false, error: (error as Error).message }
276
- }
277
- })
278
-
279
- // 获取所有插件的 Schema API
280
- router.get('/api/schemas', async (ctx) => {
281
- try {
282
- const schemas: Record<string, any> = {};
283
-
284
- // 获取 App 的 Schema
285
- const appSchema = app.schema.toJSON();
286
- if (appSchema) {
287
- schemas['app'] = appSchema;
288
- }
289
-
290
- // 获取所有插件的 Schema
291
- for (const plugin of app.dependencyList) {
292
- const schema = plugin.schema.toJSON();
293
- if (schema) {
294
- schemas[plugin.name] = schema;
295
- }
296
- }
297
-
298
- ctx.body = { success: true, data: schemas, total: Object.keys(schemas).length };
299
- } catch (error) {
300
- ctx.status = 500;
301
- ctx.body = { success: false, error: (error as Error).message };
302
- }
303
- });
304
-
305
- // 获取单个插件的 Schema API
306
- router.get('/api/schemas/:name', async (ctx) => {
307
- try {
308
- const { name } = ctx.params;
309
-
310
- if (name === 'app') {
311
- const schema = app.schema?.toJSON()
312
- if (!schema) {
313
- ctx.status = 404;
314
- ctx.body = { success: false, error: 'App schema not found' };
315
- return;
316
- }
317
- ctx.body = { success: true, data: schema };
318
- return;
319
- }
320
-
321
- const plugin = app.findPluginByName(name);
322
- if (!plugin) {
323
- ctx.status = 404;
324
- ctx.body = { success: false, error: `Plugin ${name} not found` };
325
- return;
326
- }
327
-
328
- const schema = plugin.schema.toJSON();
329
- if (!schema) {
330
- ctx.status = 404;
331
- ctx.body = { success: false, error: `Schema for plugin ${name} not found` };
332
- return;
333
- }
334
-
335
- ctx.body = { success: true, data: schema };
336
- } catch (error) {
337
- ctx.status = 500;
338
- ctx.body = { success: false, error: (error as Error).message };
339
- }
340
- });
341
-
342
- // 获取插件配置 API
343
- router.get('/api/config/:name', async (ctx) => {
344
- try {
345
- const { name } = ctx.params;
346
-
347
- if (name === 'app') {
348
- const config = app.getConfig();
349
- ctx.body = { success: true, data: config };
350
- return;
351
- }
352
-
353
- const plugin = app.findPluginByName(name);
354
- if (!plugin) {
355
- ctx.status = 404;
356
- ctx.body = { success: false, error: `Plugin ${name} not found` };
357
- return;
358
- }
359
-
360
- const config = plugin.config;
361
- ctx.body = { success: true, data: config };
362
- } catch (error) {
363
- ctx.status = 500;
364
- ctx.body = { success: false, error: (error as Error).message };
365
- }
366
- });
367
-
368
- // 更新插件配置 API
369
- router.post('/api/config/:name', async (ctx) => {
370
- try {
371
- const { name } = ctx.params;
372
- const newConfig = ctx.request.body;
373
-
374
- if (name === 'app') {
375
- app.config = newConfig as AppConfig;
376
- ctx.body = {
377
- success: true,
378
- message: 'App configuration updated successfully',
379
- data: app.getConfig()
380
- };
381
- return;
382
- }
383
-
384
- const plugin = app.findPluginByName(name);
385
- if (!plugin) {
386
- ctx.status = 404;
387
- ctx.body = { success: false, error: `Plugin ${name} not found` };
388
- return;
389
- }
390
-
391
- plugin.config = newConfig as Record<string, any>;
392
- ctx.body = {
393
- success: true,
394
- message: `Plugin ${name} configuration updated successfully`,
395
- data: plugin.config
396
- };
397
- } catch (error) {
398
- ctx.status = 500;
399
- ctx.body = { success: false, error: (error as Error).message };
400
- }
401
- });
402
-
403
- // 消息发送 API
404
- router.post('/api/message/send', async (ctx) => {
405
- try {
406
- const body = ctx.request.body as any
407
- const { context, bot, id, type, content } = body
408
-
409
- if (!context || !bot || !id || !type || !content) {
410
- ctx.status = 400
411
- ctx.body = {
412
- success: false,
413
- error: 'Missing required fields: context, bot, id, type, content'
414
- }
415
- return
416
- }
417
-
418
- // 模拟发送消息(实际环境中会调用应用实例的sendMessage方法)
419
- // console.log 已替换为注释
420
-
421
- ctx.body = {
422
- success: true,
423
- message: 'Message sent successfully',
424
- data: { context, bot, id, type, content, timestamp: new Date().toISOString() }
425
- }
426
- } catch (error) {
427
- ctx.status = 500
428
- ctx.body = { success: false, error: (error as Error).message }
429
- }
430
- })
431
-
432
- // 日志 API - 获取日志
433
- router.get('/api/logs', async (ctx) => {
434
- try {
435
- const database = useDatabase()
436
- if (!database) {
437
- ctx.status = 503
438
- ctx.body = { success: false, error: 'Database not available' }
439
- return
440
- }
441
-
442
- // 获取查询参数
443
- const limit = parseInt(ctx.query.limit as string) || 100
444
- const level = ctx.query.level as string
445
-
446
- const LogModel = database.model('SystemLog')
447
- if (!LogModel) {
448
- ctx.status = 500
449
- ctx.body = { success: false, error: 'SystemLog model not found' }
450
- return
451
- }
452
-
453
- // 查询日志
454
- let selection = LogModel.select()
455
-
456
- // 按级别过滤
457
- if (level && level !== 'all') {
458
- selection = selection.where({ level })
459
- }
460
-
461
- // 按时间倒序,限制数量
462
- const logs = await selection.orderBy('timestamp', 'DESC').limit(limit)
463
-
464
- // 格式化返回数据
465
- const formattedLogs = logs.map((log: any) => ({
466
- level: log.level,
467
- name: log.name,
468
- message: log.message,
469
- source: log.source,
470
- timestamp: log.timestamp instanceof Date ? log.timestamp.toISOString() : log.timestamp
471
- }))
472
-
473
- ctx.body = {
474
- success: true,
475
- data: formattedLogs,
476
- total: formattedLogs.length
477
- }
478
- } catch (error) {
479
- ctx.status = 500
480
- ctx.body = { success: false, error: (error as Error).message }
481
- }
482
- })
483
-
484
- // 日志 API - 清空日志
485
- router.delete('/api/logs', async (ctx) => {
486
- try {
487
- const database = useDatabase()
488
- if (!database) {
489
- ctx.status = 503
490
- ctx.body = { success: false, error: 'Database not available' }
491
- return
492
- }
493
-
494
- const LogModel = database.model('SystemLog')
495
- if (!LogModel) {
496
- ctx.status = 500
497
- ctx.body = { success: false, error: 'SystemLog model not found' }
498
- return
499
- }
500
-
501
- // 删除所有日志
502
- await (LogModel as any).delete({})
503
-
504
- ctx.body = {
505
- success: true,
506
- message: '日志已清空'
507
- }
508
- } catch (error) {
509
- ctx.status = 500
510
- ctx.body = { success: false, error: (error as Error).message }
511
- }
512
- })
513
-
514
- // 日志 API - 获取日志统计
515
- router.get('/api/logs/stats', async (ctx) => {
516
- try {
517
- const database = useDatabase()
518
- if (!database) {
519
- ctx.status = 503
520
- ctx.body = { success: false, error: 'Database not available' }
521
- return
522
- }
523
-
524
- const LogModel = database.model('SystemLog')
525
- if (!LogModel) {
526
- ctx.status = 500
527
- ctx.body = { success: false, error: 'SystemLog model not found' }
528
- return
529
- }
530
-
531
- // 获取总日志数
532
- const total = await LogModel.select()
533
- const totalCount = total.length
534
- // 获取各级别日志数
535
- const levels = ['info', 'warn', 'error']
536
- const levelCounts: Record<string, number> = {}
537
-
538
- for (const level of levels) {
539
- const count = await LogModel
540
- .select()
541
- .where({ level })
542
- levelCounts[level] = count.length
543
- }
544
-
545
- // 获取最旧日志时间
546
- const oldestLog = await LogModel
547
- .select('timestamp')
548
- .orderBy('timestamp', 'ASC')
549
- .limit(1)
550
-
551
- const oldestTimestamp = oldestLog.length > 0
552
- ? (oldestLog[0].timestamp instanceof Date
553
- ? oldestLog[0].timestamp.toISOString()
554
- : oldestLog[0].timestamp)
555
- : null
556
-
557
- ctx.body = {
558
- success: true,
559
- data: {
560
- total:totalCount,
561
- byLevel: levelCounts,
562
- oldestTimestamp
563
- }
564
- }
565
- } catch (error) {
566
- ctx.status = 500
567
- ctx.body = { success: false, error: (error as Error).message }
568
- }
569
- })
570
-
571
- // 日志 API - 清理旧日志(手动触发)
572
- router.post('/api/logs/cleanup', async (ctx) => {
573
- try {
574
- const database = useDatabase()
575
- if (!database) {
576
- ctx.status = 503
577
- ctx.body = { success: false, error: 'Database not available' }
578
- return
579
- }
580
-
581
- const LogModel = database.model('SystemLog')
582
- if (!LogModel) {
583
- ctx.status = 500
584
- ctx.body = { success: false, error: 'SystemLog model not found' }
585
- return
586
- }
587
-
588
- const { days, maxRecords } = ctx.request.body as any || {}
589
-
590
- let deletedCount = 0
591
-
592
- // 按天数清理
593
- if (days && typeof days === 'number' && days > 0) {
594
- const cutoffDate = new Date()
595
- cutoffDate.setDate(cutoffDate.getDate() - days)
596
-
597
- const deleted = await LogModel
598
- .delete({ timestamp: { $lt: cutoffDate } })
599
-
600
- deletedCount += deleted || 0
601
- }
602
-
603
- // 按数量清理
604
- if (maxRecords && typeof maxRecords === 'number' && maxRecords > 0) {
605
- const totalCount = await (LogModel as any).select()
606
-
607
- if (totalCount.length > maxRecords) {
608
- const excessCount = totalCount - maxRecords
609
-
610
- const oldestLogs = await LogModel
611
- .select('id','timestamp')
612
- .orderBy('timestamp', 'ASC')
613
- .limit(excessCount)
614
-
615
- const idsToDelete = oldestLogs.map((log: any) => log.id)
616
-
617
- if (idsToDelete.length > 0) {
618
- const deleted = await (LogModel as any)
619
- .delete()
620
- .where({ id: { $in: idsToDelete } })
621
-
622
- deletedCount += deleted || 0
623
- }
624
- }
625
- }
626
-
627
- ctx.body = {
628
- success: true,
629
- message: `已清理 ${deletedCount} 条日志`,
630
- deletedCount
631
- }
632
- } catch (error) {
633
- ctx.status = 500
634
- ctx.body = { success: false, error: (error as Error).message }
635
- }
636
- })
637
-
638
- // ============================================================================
639
- // 上下文注册
640
- // ============================================================================
641
-
642
- register({
643
- name: 'server',
644
- description:"http server",
645
- mounted(p) {
646
- return new Promise<Server>((resolve) => {
647
- server.listen(
648
- {
649
- host: '0.0.0.0',
650
- port: Number((process.env.port ||= '8086')),
651
- },
652
- () => {
653
- const address = server.address();
654
- if (!address) return;
655
- const visitAddress = typeof address === 'string' ? address : `${address.address}:${address.port}`;
656
- p.logger.info(`server is running at http://${visitAddress}`);
657
- p.logger.info('your username is:', username);
658
- p.logger.info('your password is:', password);
659
- resolve(server)
660
- },
661
- )
662
- })
663
- },
664
- dispose(s) {
665
- s.close()
666
- }
667
- })
668
-
669
- register({
670
- name: "koa",
671
- description:"koa instance",
672
- value: koa
673
- })
674
-
675
- register({
676
- name: 'router',
677
- description:"koa router",
678
- value: router
679
- })
680
-
681
- koa.use(KoaBodyParser()).use(router.routes()).use(router.allowedMethods());
682
-
package/tsconfig.json DELETED
@@ -1,23 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "ESNext",
5
- "moduleResolution": "bundler",
6
- "outDir": "./lib",
7
- "rootDir": "./src",
8
- "strict": true,
9
- "esModuleInterop": true,
10
- "skipLibCheck": true,
11
- "forceConsistentCasingInFileNames": true,
12
- "resolveJsonModule": true,
13
- "isolatedModules": true,
14
- "allowSyntheticDefaultImports": true,
15
- "experimentalDecorators": true,
16
- "emitDecoratorMetadata": true,
17
- "declaration": true,
18
- "declarationMap": true,
19
- "sourceMap": true
20
- },
21
- "include": ["src/**/*"],
22
- "exclude": ["lib", "node_modules"]
23
- }
File without changes