@zhin.js/console 1.0.6 → 1.0.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhin.js/console",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "Zhin 控制台",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -9,7 +9,12 @@
9
9
  ".": {
10
10
  "types": "./lib/index.d.ts",
11
11
  "import": "./lib/index.js"
12
- }
12
+ },
13
+ "./client": {
14
+ "types": "./lib/client/index.d.ts",
15
+ "import": "./lib/client/index.js"
16
+ },
17
+ "./browser.tsconfig.json": "./client/tsconfig.json"
13
18
  },
14
19
  "dependencies": {
15
20
  "react": "19.2.0",
@@ -18,17 +23,25 @@
18
23
  "@tailwindcss/vite": "4.1.14",
19
24
  "radix-ui": "^1.4.3",
20
25
  "@radix-ui/themes": "^3.2.1",
21
- "lucide-react": "^0.469.0",
26
+ "@tailwindcss/postcss": "^4.1.11",
27
+ "tailwindcss": "latest",
22
28
  "koa-connect": "^2.1.0",
29
+ "@reduxjs/toolkit": "^2.9.0",
30
+ "lucide-react": "^0.469.0",
31
+ "clsx": "^2.1.1",
32
+ "react-redux": "9.2.0",
33
+ "react-router": "7.0.0",
34
+ "redux-persist": "6.0.0",
35
+ "tailwind-merge": "^3.3.1",
23
36
  "ws": "^8.18.3",
24
37
  "vite": "^7.0.6"
25
38
  },
26
39
  "peerDependencies": {
27
40
  "@types/ws": "^8.18.1",
28
- "@zhin.js/client": "^1.0.4",
29
- "@zhin.js/http": "^1.0.5",
30
- "@zhin.js/core": "^1.0.8",
31
- "@zhin.js/types": "^1.0.3"
41
+ "@zhin.js/client": "^1.0.5",
42
+ "@zhin.js/types": "^1.0.3",
43
+ "@zhin.js/http": "^1.0.6",
44
+ "@zhin.js/core": "^1.0.9"
32
45
  },
33
46
  "peerDependenciesMeta": {
34
47
  "@zhin.js/types": {
@@ -37,9 +50,8 @@
37
50
  },
38
51
  "files": [
39
52
  "lib",
40
- "src",
41
- "global.d.ts",
42
- "app"
53
+ "app",
54
+ "client"
43
55
  ],
44
56
  "scripts": {
45
57
  "build": "tsc",
package/src/index.ts DELETED
@@ -1,370 +0,0 @@
1
- import {register, useContext, useApp} from '@zhin.js/core';
2
- import react from '@vitejs/plugin-react';
3
- import WebSocket,{WebSocketServer} from 'ws';
4
- import {createServer,ViteDevServer,searchForWorkspaceRoot} from 'vite';
5
- import connect from 'koa-connect';
6
- import * as fs from 'fs';
7
- import * as path from 'path';
8
- import {fileURLToPath} from "node:url";
9
- import tailwindcss from '@tailwindcss/vite';
10
-
11
- declare module '@zhin.js/types' {
12
- interface GlobalContext {
13
- web: WebServer;
14
- }
15
- }
16
- export type WebEntry=string|{
17
- production:string
18
- development:string
19
- }
20
- export type WebServer = {
21
- vite:ViteDevServer,
22
- addEntry(entry: WebEntry): () => void;
23
- entries: Record<string, string>;
24
- ws:WebSocketServer
25
- };
26
- const createSyncMsg = (key: string, value: any) => {
27
- return {
28
- type: 'sync',
29
- data: {
30
- key,
31
- value,
32
- },
33
- };
34
- };
35
- const createAddMsg = (key: string, value: any) => {
36
- return {
37
- type: 'add',
38
- data: {
39
- key,
40
- value,
41
- },
42
- };
43
- };
44
- const createDeleteMsg = (key: string, value: any) => {
45
- return {
46
- type: 'delete',
47
- data: {
48
- key,
49
- value,
50
- },
51
- };
52
- };
53
- useContext('router', async (router) => {
54
- const root = path.join(process.cwd(),'node_modules','@zhin.js','client','app');
55
- const base='/vite/'
56
-
57
- const vite = await createServer({
58
- root,
59
- base,
60
- plugins: [
61
- react(),
62
- tailwindcss(),
63
- ],
64
- server: {
65
- middlewareMode: true,
66
- fs: {
67
- strict:false
68
- },
69
- },
70
- resolve: {
71
- dedupe: ['react', 'react-dom'],
72
- alias: {
73
- '@zhin.js/client': path.resolve(root, '../src'),
74
- '@': path.resolve(root, 'src'),
75
- },
76
- },
77
- optimizeDeps: {
78
- include: ['react', 'react-dom'],
79
- },
80
- build: {
81
- rollupOptions: {
82
- input: root + '/index.html',
83
- },
84
- },
85
- });
86
-
87
- // Vite 中间件 - 必须在其他路由之前
88
- router.use((ctx: any, next: any) => {
89
- if(ctx.request.originalUrl.startsWith('/api')) return next()
90
- return connect(vite.middlewares)(ctx,next);
91
- });
92
-
93
- // SPA 回退路由 - 处理所有未匹配的路由
94
- router.all('*all', async (ctx, next) => {
95
- const url = ctx.request.originalUrl.replace(base, '')
96
- const name = ctx.path.slice(1);
97
-
98
- const sendFile = (filename: string) => {
99
- ctx.type = path.extname(filename);
100
- if (filename.endsWith('.ts')) ctx.type = 'text/javascript';
101
- return (ctx.body = fs.createReadStream(filename));
102
- };
103
-
104
- // 1. 检查是否是动态入口
105
- if (Object.keys(webServer.entries).includes(name)) {
106
- return sendFile(path.resolve(process.cwd(), webServer.entries[name]));
107
- }
108
-
109
- // 2. 检查是否是静态文件
110
- const filename = path.resolve(root, name);
111
- if (filename.startsWith(root) || filename.includes('node_modules')) {
112
- if (fs.existsSync(filename)) {
113
- const fileState = fs.statSync(filename);
114
- if (fileState.isFile()) {
115
- return sendFile(filename);
116
- }
117
- }
118
- } else {
119
- // 安全检查:路径不在允许范围内
120
- return (ctx.status = 403);
121
- }
122
-
123
- // 3. 所有其他路径(包括 SPA 路由)都返回 index.html
124
- // 这样前端路由可以正确处理
125
- const template = fs.readFileSync(path.resolve(root, 'index.html'), 'utf8');
126
- ctx.type = 'html';
127
- ctx.body = await vite.transformIndexHtml(url, template);
128
- });
129
-
130
- const webServer:WebServer={
131
- vite,
132
- entries: {},
133
- addEntry(entry) {
134
- const hash = Date.now().toString(16)+Math.random().toString(16).slice(2,8);
135
- const entryFile=typeof entry==="string"?entry:entry[(process.env.NODE_ENV as 'development'|'production')||'development'];
136
- this.entries[hash] = `/vite/@fs/${entryFile}`;
137
- for (const ws of this.ws.clients || []) {
138
- ws.send(JSON.stringify(createAddMsg('entries', this.entries[hash])));
139
- }
140
- return () => {
141
- for (const ws of this.ws.clients || []) {
142
- ws.send(JSON.stringify(createDeleteMsg('entries', this.entries[hash])));
143
- }
144
- delete this.entries[hash];
145
- };
146
- },
147
- ws:router.ws('/server')
148
- }
149
- // 数据推送函数
150
- const broadcastToAll = (message: any) => {
151
- for (const ws of webServer.ws.clients || []) {
152
- if (ws.readyState === WebSocket.OPEN) {
153
- ws.send(JSON.stringify(message));
154
- }
155
- }
156
- }
157
-
158
- // 推送数据更新通知
159
- const notifyDataUpdate = () => {
160
- broadcastToAll({
161
- type: 'data-update',
162
- timestamp: Date.now()
163
- });
164
- }
165
-
166
- // WebSocket 连接处理
167
- webServer.ws.on('connection', (ws: WebSocket) => {
168
- // 发送初始数据
169
- ws.send(JSON.stringify(createSyncMsg('entries', Array.from(new Set(Object.values(webServer.entries))))));
170
-
171
- // 通知客户端进行数据初始化
172
- ws.send(JSON.stringify({
173
- type: 'init-data',
174
- timestamp: Date.now()
175
- }));
176
-
177
- // 处理 WebSocket 消息
178
- ws.on('message', async (data) => {
179
- try {
180
- const message = JSON.parse(data.toString());
181
- const { type, pluginName, requestId } = message;
182
-
183
- // 获取应用实例
184
- const app = useApp();
185
-
186
- switch (type) {
187
- case 'config:get':
188
- try {
189
- let config;
190
- if (pluginName === 'app') {
191
- config = app.getConfig();
192
- } else {
193
- const plugin = app.findPluginByName(pluginName);
194
- if (!plugin) {
195
- throw new Error(`Plugin ${pluginName} not found`);
196
- }
197
- config = plugin.config;
198
- }
199
-
200
- ws.send(JSON.stringify({
201
- requestId,
202
- data: config
203
- }));
204
- } catch (error) {
205
- ws.send(JSON.stringify({
206
- requestId,
207
- error: (error as Error).message
208
- }));
209
- }
210
- break;
211
-
212
- case 'config:set':
213
- try {
214
- const { data: newConfig } = message;
215
-
216
- if (pluginName === 'app') {
217
- app.config = newConfig;
218
- } else {
219
- const plugin = app.findPluginByName(pluginName);
220
- if (!plugin) {
221
- throw new Error(`Plugin ${pluginName} not found`);
222
- }
223
- plugin.config = newConfig;
224
- }
225
-
226
- // 响应成功
227
- ws.send(JSON.stringify({
228
- requestId,
229
- data: 'success'
230
- }));
231
-
232
- // 广播配置更新
233
- webServer.ws.clients.forEach((client) => {
234
- if (client.readyState === 1) { // WebSocket.OPEN
235
- client.send(JSON.stringify({
236
- type: 'config:updated',
237
- pluginName,
238
- data: newConfig
239
- }));
240
- }
241
- });
242
- } catch (error) {
243
- ws.send(JSON.stringify({
244
- requestId,
245
- error: (error as Error).message
246
- }));
247
- }
248
- break;
249
-
250
- case 'schema:get':
251
- try {
252
- let schema;
253
- if (pluginName === 'app') {
254
- schema = app.schema?.toJSON();
255
- } else {
256
- const plugin = app.findPluginByName(pluginName);
257
- if (!plugin) {
258
- throw new Error(`Plugin ${pluginName} not found`);
259
- }
260
- schema = plugin.schema?.toJSON();
261
- }
262
-
263
- ws.send(JSON.stringify({
264
- requestId,
265
- data: schema
266
- }));
267
- } catch (error) {
268
- ws.send(JSON.stringify({
269
- requestId,
270
- error: (error as Error).message
271
- }));
272
- }
273
- break;
274
-
275
- case 'config:get-all':
276
- try {
277
- const configs: Record<string, any> = {};
278
-
279
- // 获取 App 配置
280
- configs['app'] = app.getConfig();
281
-
282
- // 获取所有插件配置
283
- for (const plugin of app.dependencyList) {
284
- if (plugin.config && Object.keys(plugin.config).length > 0) {
285
- configs[plugin.name] = plugin.config;
286
- }
287
- }
288
-
289
- ws.send(JSON.stringify({
290
- requestId,
291
- data: configs
292
- }));
293
- } catch (error) {
294
- ws.send(JSON.stringify({
295
- requestId,
296
- error: (error as Error).message
297
- }));
298
- }
299
- break;
300
-
301
- case 'schema:get-all':
302
- try {
303
- const schemas: Record<string, any> = {};
304
-
305
- // 获取 App Schema
306
- const appSchema = app.schema?.toJSON();
307
- if (appSchema) {
308
- schemas['app'] = appSchema;
309
- }
310
-
311
- // 获取所有插件 Schema
312
- for (const plugin of app.dependencyList) {
313
- const schema = plugin.schema?.toJSON();
314
- if (schema) {
315
- schemas[plugin.name] = schema;
316
- }
317
- }
318
-
319
- ws.send(JSON.stringify({
320
- requestId,
321
- data: schemas
322
- }));
323
- } catch (error) {
324
- ws.send(JSON.stringify({
325
- requestId,
326
- error: (error as Error).message
327
- }));
328
- }
329
- break;
330
-
331
- // 其他消息类型保持不变,让 console 插件自己处理
332
- }
333
- } catch (error) {
334
- console.error('WebSocket 消息处理错误:', error);
335
- ws.send(JSON.stringify({
336
- error: 'Invalid message format'
337
- }));
338
- }
339
- });
340
-
341
- ws.on('close', () => {
342
- });
343
-
344
- ws.on('error', (error) => {
345
- // console.error 已替换为注释
346
- });
347
- });
348
-
349
- // 定时通知客户端更新数据
350
- const dataUpdateInterval = setInterval(() => {
351
- notifyDataUpdate();
352
- }, 5000); // 每5秒通知一次更新
353
-
354
- // 插件卸载时清理定时器
355
- process.on('exit', () => {
356
- clearInterval(dataUpdateInterval);
357
- });
358
- register({
359
- name:'web',
360
- description:"web服务",
361
- async mounted(){
362
- return webServer
363
- },
364
- async dispose(server){
365
- await server.vite.close();
366
- server.ws.close()
367
- }
368
- })
369
-
370
- });