agent-publish-server 1.0.26 → 1.0.28

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/README.md CHANGED
@@ -92,12 +92,14 @@ interface AgentConfig {
92
92
  port?: number; // 服务器端口(默认:8080)
93
93
  dir?: string; // 静态文件目录(默认:"./")
94
94
  redirect?: string; // 根路径重定向,如 "/app" 表示访问 / 时重定向到 /app
95
+ fallbackRedirect?: string; // 404兜底重定向,当路由匹配不上时重定向到指定路径
95
96
  log?: boolean; // 启用访问日志(默认:true)
96
97
  proxy?: {
97
98
  // API代理配置
98
99
  [path: string]: {
99
100
  target: string; // 目标URL
100
101
  changeOrigin?: boolean; // 更改源头
102
+ ws?: boolean; // 启用 WebSocket 代理
101
103
  pathRewrite?: {
102
104
  // 路径重写规则
103
105
  [pattern: string]: string;
@@ -134,6 +136,30 @@ interface AgentConfig {
134
136
 
135
137
  这样配置后,访问 `http://localhost:3000/` 会自动重定向到 `http://localhost:3000/app`。
136
138
 
139
+ ### 404兜底重定向
140
+
141
+ 通过 `fallbackRedirect` 配置实现当路由匹配不上时自动重定向到指定路径,避免出现黑屏或404页面:
142
+
143
+ ```json
144
+ {
145
+ "port": 3000,
146
+ "redirect": "/app",
147
+ "fallbackRedirect": "/app",
148
+ "staticProxy": {
149
+ "/app": {
150
+ "target": "./dist",
151
+ "type": "static"
152
+ }
153
+ }
154
+ }
155
+ ```
156
+
157
+ 这样配置后:
158
+
159
+ - 访问 `http://localhost:3000/` 会自动重定向到 `/app`
160
+ - 访问不存在的路由(如 `/unknown`)也会重定向到 `/app`
161
+ - 已配置的 `staticProxy` 路径仍正常访问,不受影响
162
+
137
163
  ### 静态网页代理
138
164
 
139
165
  除了 API 代理外,还支持静态网页代理功能,用于代理整个网站或应用。
@@ -248,6 +274,8 @@ agent-publish-server --log false
248
274
 
249
275
  ### 版本历史
250
276
 
277
+ - **v1.0.28**: 新增 proxy.ws 配置,支持 WebSocket upgrade 代理转发
278
+ - **v1.0.27**: 新增404兜底重定向配置(fallbackRedirect),当路由匹配不上时自动重定向到指定路径,避免黑屏或404错误
251
279
  - **v1.0.26**: 新增根路径重定向配置(redirect),支持访问 / 时自动跳转到指定路径
252
280
  - **v1.0.25**: 修复 staticProxy static 类型不支持 SPA History 路由刷新的问题,添加自动 fallback 到 index.html 支持
253
281
  - **v1.0.24**: 修复移动端兼容性问题,优化 HTTP 响应头设置,确保 iOS 和 Android 显示一致性
package/README_EN.md CHANGED
@@ -106,6 +106,7 @@ With this config, accessing `http://localhost:3000/` will redirect to `http://lo
106
106
  interface ProxyConfig {
107
107
  target: string; // Target server URL
108
108
  changeOrigin?: boolean; // Change origin header
109
+ ws?: boolean; // Enable WebSocket proxy
109
110
  pathRewrite?: Record<string, string>; // Path rewriting rules
110
111
  }
111
112
  ```
@@ -249,6 +250,8 @@ startServer(app, config.port);
249
250
 
250
251
  ## Version History
251
252
 
253
+ - **v1.0.28**: Added proxy.ws configuration to support WebSocket upgrade proxy forwarding
254
+ - **v1.0.27**: Added 404 fallback redirect configuration (fallbackRedirect), automatically redirects unmatched routes to the specified path
252
255
  - **v1.0.26**: Added root path redirect configuration (redirect), supports automatic redirect when accessing /
253
256
  - **v1.0.25**: Fixed staticProxy static type not supporting SPA History route refresh, added automatic fallback to index.html
254
257
  - **v1.0.24**: Fixed mobile compatibility issues, optimized HTTP response headers for consistent iOS and Android display
package/dist/server.d.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { createProxyMiddleware } from "http-proxy-middleware";
1
2
  import { AgentConfig } from "./types";
2
- export declare function createServer(config: AgentConfig): import("express-serve-static-core").Express;
3
+ type ProxyMiddleware = ReturnType<typeof createProxyMiddleware>;
4
+ export declare function createServer(config: AgentConfig, wsProxyMiddlewares?: ProxyMiddleware[]): import("express-serve-static-core").Express;
3
5
  export declare function startServer(config: AgentConfig): Promise<void>;
6
+ export {};
package/dist/server.js CHANGED
@@ -9,7 +9,7 @@ const express_1 = __importDefault(require("express"));
9
9
  const http_proxy_middleware_1 = require("http-proxy-middleware");
10
10
  const path_1 = __importDefault(require("path"));
11
11
  const fs_1 = __importDefault(require("fs"));
12
- function createServer(config) {
12
+ function createServer(config, wsProxyMiddlewares = []) {
13
13
  const app = (0, express_1.default)();
14
14
  const staticDir = config.dir || "./";
15
15
  const enableLog = config.log !== false; // 默认为true
@@ -41,8 +41,12 @@ function createServer(config) {
41
41
  }
42
42
  // 配置API代理
43
43
  if (config.proxy) {
44
- Object.entries(config.proxy).forEach(([path, proxyConfig]) => {
45
- app.use(path, (0, http_proxy_middleware_1.createProxyMiddleware)(proxyConfig));
44
+ Object.entries(config.proxy).forEach(([proxyPath, proxyConfig]) => {
45
+ const proxyMiddleware = (0, http_proxy_middleware_1.createProxyMiddleware)(proxyPath, proxyConfig);
46
+ app.use(proxyMiddleware);
47
+ if (proxyConfig.ws === true) {
48
+ wsProxyMiddlewares.push(proxyMiddleware);
49
+ }
46
50
  });
47
51
  }
48
52
  // 配置静态网页代理
@@ -155,7 +159,7 @@ function createServer(config) {
155
159
  next();
156
160
  });
157
161
  // 添加SPA应用的回退路由处理
158
- // 只有当实际文件不存在时才回退到index.html
162
+ // 只有当实际文件不存在时才回退到index.html或执行fallbackRedirect
159
163
  app.use((req, res, next) => {
160
164
  // 排除API和已处理的代理请求
161
165
  if (req.path.startsWith("/api/")) {
@@ -182,21 +186,39 @@ function createServer(config) {
182
186
  if (fs_1.default.existsSync(filePath) && fs_1.default.statSync(filePath).isFile()) {
183
187
  return res.sendFile(filePath);
184
188
  }
185
- // 如果路径不存在,回退到index.html
186
- res.sendFile(path_1.default.join(process.cwd(), staticDir, "index.html"));
189
+ // 如果配置了fallbackRedirect,则重定向到指定路径
190
+ if (config.fallbackRedirect) {
191
+ const redirectPath = config.fallbackRedirect.startsWith("/")
192
+ ? config.fallbackRedirect
193
+ : `/${config.fallbackRedirect}`;
194
+ return res.redirect(302, redirectPath);
195
+ }
196
+ // 如果路径不存在且未配置fallbackRedirect,回退到index.html
197
+ const rootIndexPath = path_1.default.join(process.cwd(), staticDir, "index.html");
198
+ if (fs_1.default.existsSync(rootIndexPath)) {
199
+ return res.sendFile(rootIndexPath);
200
+ }
201
+ // 如果index.html也不存在,返回404
202
+ res.status(404).send("404 Not Found");
187
203
  });
188
204
  return app;
189
205
  }
190
206
  function startServer(config) {
191
207
  try {
192
- const app = createServer(config);
208
+ const wsProxyMiddlewares = [];
209
+ const app = createServer(config, wsProxyMiddlewares);
193
210
  const port = config.port || 8080;
194
211
  return new Promise((resolve, reject) => {
195
212
  try {
196
- app.listen(port, () => {
213
+ const server = app.listen(port, () => {
197
214
  console.log(`Server is running at http://localhost:${port}`);
198
215
  resolve();
199
216
  });
217
+ wsProxyMiddlewares.forEach((proxyMiddleware) => {
218
+ if (proxyMiddleware.upgrade) {
219
+ server.on("upgrade", proxyMiddleware.upgrade);
220
+ }
221
+ });
200
222
  }
201
223
  catch (error) {
202
224
  console.error("Failed to start server:", error);
package/dist/types.d.ts CHANGED
@@ -2,6 +2,7 @@ export interface ProxyConfig {
2
2
  target: string;
3
3
  changeOrigin?: boolean;
4
4
  pathRewrite?: Record<string, string>;
5
+ ws?: boolean;
5
6
  }
6
7
  export interface StaticProxyConfig {
7
8
  target: string;
@@ -12,6 +13,7 @@ export interface AgentConfig {
12
13
  port?: number;
13
14
  dir?: string;
14
15
  redirect?: string;
16
+ fallbackRedirect?: string;
15
17
  proxy?: Record<string, ProxyConfig>;
16
18
  staticProxy?: Record<string, StaticProxyConfig>;
17
19
  log?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-publish-server",
3
- "version": "1.0.26",
3
+ "version": "1.0.28",
4
4
  "description": "A powerful frontend development server with API proxy, static file serving, and dual-mode static proxy support. Built with Node.js + Express + http-proxy-middleware. 基于 Node.js + Express + http-proxy-middleware 的强大前端开发服务器,支持API代理、静态文件服务和双模式静态代理。",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -0,0 +1,18 @@
1
+ {
2
+ "port": 8080,
3
+ "dir": "./",
4
+ "redirect": "/sub_page",
5
+ "fallbackRedirect": "/sub_page",
6
+ "staticProxy": {
7
+ "/sub_page": {
8
+ "target": "./dist",
9
+ "type": "static"
10
+ },
11
+ "/app": {
12
+ "target": "http://localhost:3000",
13
+ "type": "http",
14
+ "changeOrigin": true
15
+ }
16
+ },
17
+ "log": true
18
+ }