@zhin.js/console 1.0.12 → 1.0.14
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 +18 -0
- package/README.md +144 -1
- package/client/src/pages/dashboard-plugin-detail.tsx +33 -5
- package/dist/client.js +1 -9
- package/dist/index.js +5 -13
- package/dist/lucide-react.js +0 -9294
- package/dist/radix-ui-themes.js +272 -288
- package/dist/radix-ui.js +337 -348
- package/dist/react-dom-client.js +22 -76
- package/dist/react-dom.js +1 -10
- package/dist/react-jsx-dev-runtime.js +1 -10
- package/dist/react-jsx-runtime.js +1 -10
- package/dist/react-router.js +5517 -4507
- package/dist/react.js +1 -10
- package/dist/style.css +2 -2
- package/lib/bin.d.ts +0 -2
- package/lib/bin.js +101 -15
- package/lib/build.d.ts +4 -2
- package/lib/build.js +83 -83
- package/lib/dev.d.ts +6 -4
- package/lib/dev.js +80 -69
- package/lib/index.d.ts +23 -10
- package/lib/index.js +405 -147
- package/lib/websocket.d.ts +10 -5
- package/lib/websocket.js +139 -78
- package/package.json +25 -21
- package/lib/bin.d.ts.map +0 -1
- package/lib/bin.js.map +0 -1
- package/lib/build.d.ts.map +0 -1
- package/lib/build.js.map +0 -1
- package/lib/dev.d.ts.map +0 -1
- package/lib/dev.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/websocket.d.ts.map +0 -1
- package/lib/websocket.js.map +0 -1
package/lib/index.js
CHANGED
|
@@ -1,167 +1,425 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
key,
|
|
14
|
-
value,
|
|
15
|
-
},
|
|
16
|
-
};
|
|
1
|
+
import * as path2 from 'path';
|
|
2
|
+
import path2__default from 'path';
|
|
3
|
+
import * as fs2 from 'fs';
|
|
4
|
+
import fs2__default from 'fs';
|
|
5
|
+
import { usePlugin } from '@zhin.js/core';
|
|
6
|
+
import mime from 'mime';
|
|
7
|
+
import WebSocket from 'ws';
|
|
8
|
+
|
|
9
|
+
var __defProp = Object.defineProperty;
|
|
10
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
11
|
+
var __esm = (fn, res) => function __init() {
|
|
12
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
17
13
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
data: {
|
|
22
|
-
key,
|
|
23
|
-
value,
|
|
24
|
-
},
|
|
25
|
-
};
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
26
17
|
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
18
|
+
|
|
19
|
+
// src/dev.ts
|
|
20
|
+
var dev_exports = {};
|
|
21
|
+
__export(dev_exports, {
|
|
22
|
+
createViteDevServer: () => createViteDevServer
|
|
23
|
+
});
|
|
24
|
+
async function createViteDevServer(options) {
|
|
25
|
+
const { root: root3, base = "/vite/", enableTailwind = true } = options;
|
|
26
|
+
try {
|
|
27
|
+
const [
|
|
28
|
+
{ createServer, searchForWorkspaceRoot },
|
|
29
|
+
{ default: react },
|
|
30
|
+
{ default: tailwindcss }
|
|
31
|
+
] = await Promise.all([
|
|
32
|
+
import('vite'),
|
|
33
|
+
import('@vitejs/plugin-react'),
|
|
34
|
+
import('@tailwindcss/vite')
|
|
35
|
+
]);
|
|
36
|
+
const plugins = [react()];
|
|
37
|
+
if (enableTailwind) {
|
|
38
|
+
plugins.push(tailwindcss());
|
|
39
|
+
}
|
|
40
|
+
const clientPath = path2__default.resolve(process.cwd(), "node_modules/@zhin.js/client/client");
|
|
41
|
+
if (!fs2__default.existsSync(clientPath)) {
|
|
42
|
+
throw new Error("@zhin.js/client not found");
|
|
43
|
+
}
|
|
44
|
+
return await createServer({
|
|
45
|
+
root: root3,
|
|
46
|
+
base,
|
|
47
|
+
plugins: [react(), tailwindcss()],
|
|
48
|
+
server: {
|
|
49
|
+
middlewareMode: true,
|
|
50
|
+
allowedHosts: true,
|
|
51
|
+
fs: {
|
|
52
|
+
strict: false,
|
|
53
|
+
// 添加文件访问过滤,避免访问特殊文件
|
|
54
|
+
allow: [
|
|
55
|
+
// 允许访问的目录
|
|
56
|
+
root3,
|
|
57
|
+
searchForWorkspaceRoot(root3),
|
|
58
|
+
path2__default.resolve(process.cwd(), "node_modules"),
|
|
59
|
+
path2__default.resolve(process.cwd(), "client"),
|
|
60
|
+
path2__default.resolve(process.cwd(), "src")
|
|
61
|
+
],
|
|
62
|
+
// 拒绝访问某些文件模式
|
|
63
|
+
deny: [
|
|
64
|
+
"**/.git/**",
|
|
65
|
+
"**/node_modules/.cache/**",
|
|
66
|
+
"**/*.socket",
|
|
67
|
+
"**/*.pipe",
|
|
68
|
+
"**/Dockerfile*",
|
|
69
|
+
"**/.env*"
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
resolve: {
|
|
74
|
+
dedupe: [
|
|
75
|
+
"react",
|
|
76
|
+
"react-dom",
|
|
77
|
+
"clsx",
|
|
78
|
+
"tailwind-merge",
|
|
79
|
+
"@reduxjs/toolkit",
|
|
80
|
+
"react-router",
|
|
81
|
+
"react-redux",
|
|
82
|
+
"redux-persist"
|
|
83
|
+
],
|
|
84
|
+
alias: {
|
|
85
|
+
"@zhin.js/client": path2__default.resolve(process.cwd(), "node_modules/@zhin.js/client/client"),
|
|
86
|
+
"@": path2__default.resolve(root3, "../client/src")
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
optimizeDeps: {
|
|
90
|
+
include: ["react", "react-dom"]
|
|
91
|
+
},
|
|
92
|
+
build: {
|
|
93
|
+
rollupOptions: {
|
|
94
|
+
input: root3 + "/index.html"
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
} catch (error) {
|
|
99
|
+
throw new Error(
|
|
100
|
+
`Failed to create Vite dev server. Make sure all development dependencies are installed: vite, @vitejs/plugin-react, @tailwindcss/vite. Run: pnpm install --include=optional
|
|
101
|
+
Original error: ${error.message}`
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
var init_dev = __esm({
|
|
106
|
+
"src/dev.ts"() {
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
var { root, logger } = usePlugin();
|
|
110
|
+
function setupWebSocket(webServer) {
|
|
111
|
+
webServer.ws.on("connection", (ws) => {
|
|
112
|
+
ws.send(
|
|
113
|
+
JSON.stringify({
|
|
114
|
+
type: "sync",
|
|
30
115
|
data: {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
116
|
+
key: "entries",
|
|
117
|
+
value: Object.values(webServer.entries)
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
);
|
|
121
|
+
ws.send(
|
|
122
|
+
JSON.stringify({
|
|
123
|
+
type: "init-data",
|
|
124
|
+
timestamp: Date.now()
|
|
125
|
+
})
|
|
126
|
+
);
|
|
127
|
+
ws.on("message", async (data) => {
|
|
128
|
+
try {
|
|
129
|
+
const message = JSON.parse(data.toString());
|
|
130
|
+
await handleWebSocketMessage(ws, message, webServer);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
console.error("WebSocket \u6D88\u606F\u5904\u7406\u9519\u8BEF:", error);
|
|
133
|
+
ws.send(
|
|
134
|
+
JSON.stringify({
|
|
135
|
+
error: "Invalid message format"
|
|
136
|
+
})
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
ws.on("close", () => {
|
|
141
|
+
});
|
|
142
|
+
ws.on("error", (error) => {
|
|
143
|
+
console.error("WebSocket \u9519\u8BEF:", error);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
async function handleWebSocketMessage(ws, message, webServer) {
|
|
148
|
+
const { type, requestId, pluginName } = message;
|
|
149
|
+
switch (type) {
|
|
150
|
+
case "ping":
|
|
151
|
+
ws.send(JSON.stringify({ type: "pong", requestId }));
|
|
152
|
+
break;
|
|
153
|
+
case "entries:get":
|
|
154
|
+
ws.send(
|
|
155
|
+
JSON.stringify({
|
|
156
|
+
requestId,
|
|
157
|
+
data: Object.values(webServer.entries)
|
|
158
|
+
})
|
|
159
|
+
);
|
|
160
|
+
break;
|
|
161
|
+
case "config:get":
|
|
162
|
+
try {
|
|
163
|
+
const configService2 = root.inject("config");
|
|
164
|
+
const appConfig2 = configService2.get("zhin.config.yml");
|
|
165
|
+
const config = pluginName ? appConfig2[pluginName] || {} : appConfig2;
|
|
166
|
+
ws.send(JSON.stringify({ requestId, data: config }));
|
|
167
|
+
} catch (error) {
|
|
168
|
+
ws.send(JSON.stringify({ requestId, error: `Failed to get config: ${error.message}` }));
|
|
169
|
+
}
|
|
170
|
+
break;
|
|
171
|
+
case "config:get-all":
|
|
172
|
+
try {
|
|
173
|
+
const configService2 = root.inject("config");
|
|
174
|
+
const appConfig2 = configService2.get("zhin.config.yml");
|
|
175
|
+
ws.send(JSON.stringify({ requestId, data: appConfig2 }));
|
|
176
|
+
} catch (error) {
|
|
177
|
+
ws.send(JSON.stringify({ requestId, error: `Failed to get all configs: ${error.message}` }));
|
|
178
|
+
}
|
|
179
|
+
break;
|
|
180
|
+
case "config:set":
|
|
181
|
+
try {
|
|
182
|
+
const { data } = message;
|
|
183
|
+
if (!pluginName) {
|
|
184
|
+
ws.send(JSON.stringify({ requestId, error: "Plugin name is required" }));
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
const configService2 = root.inject("config");
|
|
188
|
+
const appConfig2 = configService2.get("zhin.config.yml");
|
|
189
|
+
appConfig2[pluginName] = data;
|
|
190
|
+
configService2.set("zhin.config.yml", appConfig2);
|
|
191
|
+
ws.send(JSON.stringify({ requestId, success: true }));
|
|
192
|
+
broadcastToAll(webServer, {
|
|
193
|
+
type: "config:updated",
|
|
194
|
+
data: { pluginName, config: data }
|
|
195
|
+
});
|
|
196
|
+
} catch (error) {
|
|
197
|
+
ws.send(JSON.stringify({ requestId, error: `Failed to set config: ${error.message}` }));
|
|
198
|
+
}
|
|
199
|
+
break;
|
|
200
|
+
case "schema:get":
|
|
201
|
+
try {
|
|
202
|
+
const schemaService = root.inject("schema");
|
|
203
|
+
const schema = pluginName && schemaService ? schemaService.get(pluginName) : null;
|
|
204
|
+
if (schema) {
|
|
205
|
+
ws.send(JSON.stringify({ requestId, data: schema.toJSON() }));
|
|
206
|
+
} else {
|
|
207
|
+
ws.send(JSON.stringify({ requestId, data: null }));
|
|
208
|
+
}
|
|
209
|
+
} catch (error) {
|
|
210
|
+
ws.send(JSON.stringify({ requestId, error: `Failed to get schema: ${error.message}` }));
|
|
211
|
+
}
|
|
212
|
+
break;
|
|
213
|
+
case "schema:get-all":
|
|
214
|
+
try {
|
|
215
|
+
const schemaService = root.inject("schema");
|
|
216
|
+
const schemas = {};
|
|
217
|
+
if (schemaService) {
|
|
218
|
+
for (const [name, schema] of schemaService.items.entries()) {
|
|
219
|
+
schemas[name] = schema.toJSON();
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
ws.send(JSON.stringify({ requestId, data: schemas }));
|
|
223
|
+
} catch (error) {
|
|
224
|
+
ws.send(JSON.stringify({ requestId, error: `Failed to get all schemas: ${error.message}` }));
|
|
225
|
+
}
|
|
226
|
+
break;
|
|
227
|
+
default:
|
|
228
|
+
ws.send(
|
|
229
|
+
JSON.stringify({
|
|
230
|
+
requestId,
|
|
231
|
+
error: `Unknown message type: ${type}`
|
|
232
|
+
})
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
function broadcastToAll(webServer, message) {
|
|
237
|
+
for (const ws of webServer.ws.clients || []) {
|
|
238
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
239
|
+
ws.send(JSON.stringify(message));
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function notifyDataUpdate(webServer) {
|
|
244
|
+
broadcastToAll(webServer, {
|
|
245
|
+
type: "data-update",
|
|
246
|
+
timestamp: Date.now()
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// src/index.ts
|
|
251
|
+
async function loadDevDependencies() {
|
|
252
|
+
try {
|
|
253
|
+
const devModule = await Promise.resolve().then(() => (init_dev(), dev_exports));
|
|
254
|
+
const koaConnectModule = await import('koa-connect');
|
|
255
|
+
return {
|
|
256
|
+
createViteDevServer: devModule.createViteDevServer,
|
|
257
|
+
connect: koaConnectModule.default
|
|
34
258
|
};
|
|
35
|
-
}
|
|
36
|
-
|
|
259
|
+
} catch {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
var { provide, root: root2, useContext, logger: logger2, inject, onDispose } = usePlugin();
|
|
264
|
+
var configService = inject("config");
|
|
265
|
+
var appConfig = configService?.get("zhin.config.yml") || {};
|
|
266
|
+
var consoleConfig = appConfig.plugins?.console || {};
|
|
267
|
+
var {
|
|
268
|
+
enabled = true
|
|
269
|
+
// 默认不延迟加载,避免 addEntry 等功能不可用
|
|
270
|
+
} = consoleConfig;
|
|
271
|
+
if (enabled) {
|
|
272
|
+
const createAddMsg = (key, value) => ({
|
|
273
|
+
type: "add",
|
|
274
|
+
data: { key, value }
|
|
275
|
+
});
|
|
276
|
+
const createDeleteMsg = (key, value) => ({
|
|
277
|
+
type: "delete",
|
|
278
|
+
data: { key, value }
|
|
279
|
+
});
|
|
280
|
+
useContext("router", async (router) => {
|
|
37
281
|
const base = "/vite/";
|
|
282
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
283
|
+
const rootDir = isDev ? path2.join(import.meta.dirname, "../client") : path2.join(import.meta.dirname, "../dist");
|
|
284
|
+
let viteStarting = false;
|
|
285
|
+
let viteStarted = false;
|
|
286
|
+
let devDeps = null;
|
|
38
287
|
const webServer = {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
this.entries[hash]
|
|
288
|
+
entries: {},
|
|
289
|
+
addEntry(entry) {
|
|
290
|
+
const hash = Date.now().toString(16) + Math.random().toString(16).slice(2, 8);
|
|
291
|
+
const entryFile = typeof entry === "string" ? entry : entry[process.env.NODE_ENV || "development"];
|
|
292
|
+
this.entries[hash] = `/vite/@fs/${entryFile}`;
|
|
293
|
+
if (this.ws) {
|
|
294
|
+
for (const ws of this.ws.clients || []) {
|
|
295
|
+
ws.send(JSON.stringify(createAddMsg("entries", this.entries[hash])));
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return () => {
|
|
299
|
+
if (this.ws) {
|
|
47
300
|
for (const ws of this.ws.clients || []) {
|
|
48
|
-
|
|
301
|
+
ws.send(JSON.stringify(createDeleteMsg("entries", this.entries[hash])));
|
|
49
302
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
};
|
|
56
|
-
},
|
|
57
|
-
ws: router.ws("/server"),
|
|
303
|
+
}
|
|
304
|
+
delete this.entries[hash];
|
|
305
|
+
};
|
|
306
|
+
},
|
|
307
|
+
ws: router.ws("/server")
|
|
58
308
|
};
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
309
|
+
const ensureViteStarted = async () => {
|
|
310
|
+
if (viteStarted || viteStarting || !isDev) return;
|
|
311
|
+
viteStarting = true;
|
|
312
|
+
try {
|
|
313
|
+
logger2.info("\u{1F504} \u68C0\u6D4B\u5230\u63A7\u5236\u53F0\u8BBF\u95EE\uFF0C\u6B63\u5728\u542F\u52A8 Vite \u5F00\u53D1\u670D\u52A1\u5668...");
|
|
314
|
+
devDeps = await loadDevDependencies();
|
|
315
|
+
if (devDeps) {
|
|
316
|
+
webServer.vite = await devDeps.createViteDevServer({
|
|
317
|
+
root: rootDir,
|
|
66
318
|
base,
|
|
67
|
-
enableTailwind: true
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
319
|
+
enableTailwind: true
|
|
320
|
+
});
|
|
321
|
+
viteStarted = true;
|
|
322
|
+
logger2.info("\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557");
|
|
323
|
+
logger2.info("\u2551 Web \u63A7\u5236\u53F0\u5DF2\u542F\u52A8 (\u6309\u9700\u52A0\u8F7D) \u2551");
|
|
324
|
+
logger2.info("\u2560\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2563");
|
|
325
|
+
logger2.info("\u2551 \u5730\u5740: http://localhost:8086/ \u2551");
|
|
326
|
+
logger2.info("\u2551 \u6A21\u5F0F: \u5F00\u53D1\u6A21\u5F0F (Vite HMR) \u2551");
|
|
327
|
+
logger2.info("\u2551 \u5185\u5B58: \u5DF2\u52A0\u8F7D (~23MB) \u2551");
|
|
328
|
+
logger2.info("\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D");
|
|
329
|
+
}
|
|
330
|
+
} catch (error) {
|
|
331
|
+
logger2.error("Vite \u542F\u52A8\u5931\u8D25:", error);
|
|
332
|
+
} finally {
|
|
333
|
+
viteStarting = false;
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
if (isDev) {
|
|
337
|
+
await ensureViteStarted();
|
|
338
|
+
router.use(async (ctx, next) => {
|
|
339
|
+
if (ctx.request.originalUrl.startsWith("/api")) return next();
|
|
340
|
+
if (webServer.vite && devDeps) {
|
|
341
|
+
return devDeps.connect(webServer.vite.middlewares)(ctx, next);
|
|
342
|
+
}
|
|
343
|
+
return next();
|
|
344
|
+
});
|
|
345
|
+
} else {
|
|
346
|
+
router.use((ctx, next) => {
|
|
347
|
+
if (ctx.request.originalUrl.startsWith("/api")) return next();
|
|
348
|
+
if (!ctx.path.startsWith("/vite/@fs/")) return next();
|
|
349
|
+
const filename = ctx.path.replace(`/vite/@fs/`, "");
|
|
350
|
+
if (!fs2.existsSync(filename)) return next();
|
|
351
|
+
ctx.type = mime.getType(filename) || path2.extname(filename);
|
|
352
|
+
ctx.body = fs2.createReadStream(filename);
|
|
353
|
+
});
|
|
354
|
+
logger2.info("\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557");
|
|
355
|
+
logger2.info("\u2551 Web \u63A7\u5236\u53F0\u5DF2\u542F\u52A8 \u2551");
|
|
356
|
+
logger2.info("\u2560\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2563");
|
|
357
|
+
logger2.info("\u2551 \u5730\u5740: http://localhost:8086/ \u2551");
|
|
358
|
+
logger2.info("\u2551 \u6A21\u5F0F: \u751F\u4EA7\u6A21\u5F0F (\u9759\u6001\u6587\u4EF6) \u2551");
|
|
359
|
+
logger2.info("\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D");
|
|
88
360
|
}
|
|
89
|
-
// SPA 回退路由 - 处理所有未匹配的路由
|
|
90
361
|
router.all("*all", async (ctx, next) => {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
ctx.status = 404;
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
ctx.type = path.extname(filename);
|
|
107
|
-
ctx.type = mime.getType(filename) || ctx.type;
|
|
108
|
-
return (ctx.body = fs.createReadStream(filename));
|
|
109
|
-
};
|
|
110
|
-
// 1. 检查是否是动态入口
|
|
111
|
-
if (Object.keys(webServer.entries).includes(name)) {
|
|
112
|
-
return sendFile(path.resolve(process.cwd(), webServer.entries[name]));
|
|
362
|
+
const url = ctx.request.originalUrl.replace(base, "");
|
|
363
|
+
const name = ctx.path.slice(1);
|
|
364
|
+
const sendFile = (filename2) => {
|
|
365
|
+
try {
|
|
366
|
+
const stat = fs2.statSync(filename2);
|
|
367
|
+
if (!stat.isFile()) {
|
|
368
|
+
ctx.status = 404;
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
} catch (error) {
|
|
372
|
+
ctx.status = 404;
|
|
373
|
+
return;
|
|
113
374
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
catch (error) {
|
|
129
|
-
// 忽略文件系统错误,继续处理
|
|
130
|
-
console.warn(`文件访问错误: ${filename}`, error.message);
|
|
375
|
+
ctx.type = path2.extname(filename2);
|
|
376
|
+
ctx.type = mime.getType(filename2) || ctx.type;
|
|
377
|
+
return ctx.body = fs2.createReadStream(filename2);
|
|
378
|
+
};
|
|
379
|
+
if (Object.keys(webServer.entries).includes(name)) {
|
|
380
|
+
return sendFile(path2.resolve(process.cwd(), webServer.entries[name]));
|
|
381
|
+
}
|
|
382
|
+
const filename = path2.resolve(rootDir, name);
|
|
383
|
+
if (filename.startsWith(rootDir) || filename.includes("node_modules")) {
|
|
384
|
+
try {
|
|
385
|
+
if (fs2.existsSync(filename)) {
|
|
386
|
+
const fileState = fs2.statSync(filename);
|
|
387
|
+
if (fileState.isFile() && !fileState.isSocket() && !fileState.isFIFO()) {
|
|
388
|
+
return sendFile(filename);
|
|
131
389
|
}
|
|
390
|
+
}
|
|
391
|
+
} catch (error) {
|
|
392
|
+
logger2.warn(`\u6587\u4EF6\u8BBF\u95EE\u9519\u8BEF: ${filename}`, error.message);
|
|
132
393
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return sendFile(indexFile);
|
|
142
|
-
const template = fs.readFileSync(indexFile, "utf8");
|
|
143
|
-
ctx.type = "html";
|
|
144
|
-
ctx.body = await webServer.vite.transformIndexHtml(url, template);
|
|
394
|
+
} else {
|
|
395
|
+
return ctx.status = 403;
|
|
396
|
+
}
|
|
397
|
+
const indexFile = path2.resolve(rootDir, "index.html");
|
|
398
|
+
if (!isDev) return sendFile(indexFile);
|
|
399
|
+
const template = fs2.readFileSync(indexFile, "utf8");
|
|
400
|
+
ctx.type = "html";
|
|
401
|
+
ctx.body = await webServer.vite?.transformIndexHtml(url, template) || template;
|
|
145
402
|
});
|
|
146
|
-
|
|
403
|
+
webServer.ws;
|
|
147
404
|
const dataUpdateInterval = setInterval(() => {
|
|
148
|
-
|
|
149
|
-
},
|
|
405
|
+
notifyDataUpdate(webServer);
|
|
406
|
+
}, 5e3);
|
|
150
407
|
setupWebSocket(webServer);
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
clearInterval(dataUpdateInterval);
|
|
408
|
+
onDispose(() => {
|
|
409
|
+
clearInterval(dataUpdateInterval);
|
|
154
410
|
});
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
server.ws.close();
|
|
164
|
-
|
|
411
|
+
provide({
|
|
412
|
+
name: "web",
|
|
413
|
+
description: "web\u670D\u52A1",
|
|
414
|
+
value: webServer,
|
|
415
|
+
dispose(server) {
|
|
416
|
+
return Promise.all([
|
|
417
|
+
server.vite?.close(),
|
|
418
|
+
new Promise((resolve2) => {
|
|
419
|
+
server.ws.close(() => resolve2());
|
|
420
|
+
})
|
|
421
|
+
]);
|
|
422
|
+
}
|
|
165
423
|
});
|
|
166
|
-
});
|
|
167
|
-
|
|
424
|
+
});
|
|
425
|
+
}
|
package/lib/websocket.d.ts
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { WebServer } from './index.js';
|
|
2
|
+
import '@zhin.js/http';
|
|
3
|
+
import 'ws';
|
|
4
|
+
import 'vite';
|
|
5
|
+
|
|
2
6
|
/**
|
|
3
7
|
* 设置 WebSocket 连接处理
|
|
4
8
|
*/
|
|
5
|
-
|
|
9
|
+
declare function setupWebSocket(webServer: WebServer): void;
|
|
6
10
|
/**
|
|
7
11
|
* 广播消息给所有连接的客户端
|
|
8
12
|
*/
|
|
9
|
-
|
|
13
|
+
declare function broadcastToAll(webServer: WebServer, message: any): void;
|
|
10
14
|
/**
|
|
11
15
|
* 通知数据更新
|
|
12
16
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
declare function notifyDataUpdate(webServer: WebServer): void;
|
|
18
|
+
|
|
19
|
+
export { broadcastToAll, notifyDataUpdate, setupWebSocket };
|