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