env-plugin 0.6.1 → 0.7.0
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/dist/Container.js +2 -1
- package/dist/PostProxyServer.js +4 -6
- package/dist/client/assets/element-plus-BN_JcO6B.js +19 -0
- package/dist/client/assets/element-plus-SrDBYmqw.css +1 -0
- package/dist/client/assets/index-CcOgzo-G.js +1 -0
- package/dist/client/assets/index-DcxBCPsx.css +1 -0
- package/dist/client/index.html +4 -4
- package/dist/controllers/DevServerController.js +1 -0
- package/dist/controllers/EnvController.js +1 -0
- package/dist/controllers/ImportExportController.js +1 -0
- package/dist/controllers/PasswordController.js +1 -0
- package/dist/controllers/RequestLogController.js +1 -0
- package/dist/controllers/RouteRuleController.js +1 -0
- package/dist/controllers/SystemSettingController.js +2 -2
- package/dist/index.js +8 -11
- package/dist/models/EnvModel.js +31 -3
- package/dist/repositories/DevServerRepo.js +14 -67
- package/dist/repositories/EnvRepo.js +26 -94
- package/dist/repositories/PasswordRepo.js +40 -104
- package/dist/repositories/RouteRuleRepo.js +22 -77
- package/dist/repositories/SystemSettingRepo.js +12 -33
- package/dist/repositories/database.js +11 -96
- package/dist/repositories/migrate.js +198 -0
- package/dist/repositories/store.js +118 -0
- package/dist/routes/index.js +1 -1
- package/dist/service/DevServerService.js +4 -2
- package/dist/service/EnvService.js +9 -7
- package/dist/service/ImportExportService.js +9 -5
- package/dist/service/PasswordService.js +3 -2
- package/dist/service/PreProxyServer.js +68 -39
- package/dist/service/ProxyAutoStarterService.js +3 -0
- package/dist/service/RequestLogService.js +13 -5
- package/dist/service/RouteRuleService.js +4 -2
- package/dist/service/SystemSettingService.js +17 -17
- package/dist/utils/ResolveConfig.js +4 -2
- package/dist/utils/errors.js +2 -0
- package/package.json +26 -32
- package/dist/client/assets/element-plus-aWS7wRSd.js.gz +0 -0
- package/dist/client/assets/index-CBMR4n-V.js.gz +0 -0
- package/dist/client/assets/index-a3AJbcK4.css.gz +0 -0
- package/dist/client/assets/vue-uvJWWccw.js.gz +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
2
|
import PreProxyServer from "./PreProxyServer.js";
|
|
3
3
|
import { AppError } from "../utils/errors.js";
|
|
4
4
|
import { envLogger } from "../utils/logger.js";
|
|
@@ -9,6 +9,10 @@ import * as os from "os";
|
|
|
9
9
|
* 依赖环境仓库(EnvRepo)和开发服务器仓库(DevServerRepo)实现数据持久化和关联操作
|
|
10
10
|
*/
|
|
11
11
|
class EnvService {
|
|
12
|
+
envRepo;
|
|
13
|
+
devServerRepo;
|
|
14
|
+
routeRuleRepo;
|
|
15
|
+
serverIp;
|
|
12
16
|
/**
|
|
13
17
|
* 构造函数 - 通过依赖注入初始化仓库实例
|
|
14
18
|
* @param envRepo - 环境仓库实例,用于环境数据的持久化操作
|
|
@@ -40,7 +44,7 @@ class EnvService {
|
|
|
40
44
|
// 生成唯一ID并组装完整环境信息
|
|
41
45
|
const newEnvItem = {
|
|
42
46
|
...envItem,
|
|
43
|
-
id:
|
|
47
|
+
id: randomUUID(),
|
|
44
48
|
status: "stopped", // 默认初始状态为停止
|
|
45
49
|
};
|
|
46
50
|
// 保存环境
|
|
@@ -174,11 +178,9 @@ class EnvService {
|
|
|
174
178
|
async stopServerAtSamePort(envItem) {
|
|
175
179
|
// 先查与当前服务同端口的环境,如果启动,则关闭掉
|
|
176
180
|
const samePortAndRunningEnv = this.envRepo.findOne({
|
|
177
|
-
$
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
{ status: "running" }, // 直接匹配等于的值
|
|
181
|
-
],
|
|
181
|
+
id: { $ne: envItem.id },
|
|
182
|
+
port: envItem.port,
|
|
183
|
+
status: "running",
|
|
182
184
|
});
|
|
183
185
|
if (samePortAndRunningEnv) {
|
|
184
186
|
envLogger.info({
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
2
|
import * as crypto from "crypto";
|
|
3
3
|
import { EXPORT_VERSION, } from "../types/index.js";
|
|
4
4
|
import { AppError } from "../utils/errors.js";
|
|
@@ -10,6 +10,10 @@ const MAGIC_BYTES = Buffer.from("ENVM");
|
|
|
10
10
|
* 负责环境配置的导出(打包为 JSON)和导入(解析并重建数据)
|
|
11
11
|
*/
|
|
12
12
|
class ImportExportService {
|
|
13
|
+
envRepo;
|
|
14
|
+
devServerRepo;
|
|
15
|
+
routeRuleRepo;
|
|
16
|
+
passwordRepo;
|
|
13
17
|
constructor(envRepo, devServerRepo, routeRuleRepo, passwordRepo) {
|
|
14
18
|
this.envRepo = envRepo;
|
|
15
19
|
this.devServerRepo = devServerRepo;
|
|
@@ -138,7 +142,7 @@ class ImportExportService {
|
|
|
138
142
|
}
|
|
139
143
|
}
|
|
140
144
|
else {
|
|
141
|
-
const id =
|
|
145
|
+
const id = randomUUID();
|
|
142
146
|
this.devServerRepo.addDevServer({
|
|
143
147
|
id,
|
|
144
148
|
name: ds.name,
|
|
@@ -189,7 +193,7 @@ class ImportExportService {
|
|
|
189
193
|
}
|
|
190
194
|
else {
|
|
191
195
|
// 创建新环境
|
|
192
|
-
envId =
|
|
196
|
+
envId = randomUUID();
|
|
193
197
|
const devServerId = exportEnv.devServerUrl
|
|
194
198
|
? devServerIdMap.get(exportEnv.devServerUrl)
|
|
195
199
|
: undefined;
|
|
@@ -214,7 +218,7 @@ class ImportExportService {
|
|
|
214
218
|
: undefined;
|
|
215
219
|
const now = new Date().toISOString();
|
|
216
220
|
this.routeRuleRepo.create({
|
|
217
|
-
id:
|
|
221
|
+
id: randomUUID(),
|
|
218
222
|
envId,
|
|
219
223
|
pathPrefix: rule.pathPrefix,
|
|
220
224
|
targetEnvId,
|
|
@@ -234,7 +238,7 @@ class ImportExportService {
|
|
|
234
238
|
try {
|
|
235
239
|
const now = new Date().toISOString();
|
|
236
240
|
this.passwordRepo.create({
|
|
237
|
-
id:
|
|
241
|
+
id: randomUUID(),
|
|
238
242
|
envId,
|
|
239
243
|
name: pwd.name,
|
|
240
244
|
username: pwd.username,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { AppError } from "../utils/errors.js";
|
|
3
3
|
/**
|
|
4
4
|
* 密码服务类
|
|
@@ -6,6 +6,7 @@ import { AppError } from "../utils/errors.js";
|
|
|
6
6
|
* 依赖密码仓库(PasswordRepo)实现数据持久化
|
|
7
7
|
*/
|
|
8
8
|
class PasswordService {
|
|
9
|
+
passwordRepo;
|
|
9
10
|
/**
|
|
10
11
|
* 构造函数 - 通过依赖注入初始化仓库实例
|
|
11
12
|
* @param passwordRepo - 密码仓库实例,用于密码数据的持久化操作
|
|
@@ -42,7 +43,7 @@ class PasswordService {
|
|
|
42
43
|
const now = new Date().toISOString();
|
|
43
44
|
const newPassword = {
|
|
44
45
|
...passwordItem,
|
|
45
|
-
id:
|
|
46
|
+
id: randomUUID(),
|
|
46
47
|
createdAt: now,
|
|
47
48
|
updatedAt: now,
|
|
48
49
|
};
|
|
@@ -11,8 +11,23 @@ import { devServerLogger } from "../utils/logger.js";
|
|
|
11
11
|
import { requestLogEmitter } from "./RequestLogService.js";
|
|
12
12
|
import { getInjectEnabled } from "./SystemSettingService.js";
|
|
13
13
|
import { classifyResourceType } from "../utils/resourceType.js";
|
|
14
|
-
import {
|
|
14
|
+
import { randomUUID } from "node:crypto";
|
|
15
|
+
// 以原始请求对象为 key 的请求级缓存,请求结束后自动被 GC 回收
|
|
16
|
+
const requestContextMap = new WeakMap();
|
|
15
17
|
class PreProxyServer {
|
|
18
|
+
envId;
|
|
19
|
+
envRepo;
|
|
20
|
+
devServerRepo;
|
|
21
|
+
routeRuleRepo;
|
|
22
|
+
app;
|
|
23
|
+
/**
|
|
24
|
+
* 代理服务器
|
|
25
|
+
*/
|
|
26
|
+
server;
|
|
27
|
+
/**
|
|
28
|
+
* 保存当前代理的 socket 连接 关闭前先断开所有链接
|
|
29
|
+
*/
|
|
30
|
+
sockets;
|
|
16
31
|
/**
|
|
17
32
|
* 判断该端口是否已经有服务存在
|
|
18
33
|
* @param port
|
|
@@ -21,6 +36,10 @@ class PreProxyServer {
|
|
|
21
36
|
static getAppInsByPort(port) {
|
|
22
37
|
return this.appMap[port];
|
|
23
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* 保存启动的环境实例
|
|
41
|
+
*/
|
|
42
|
+
static appMap = {};
|
|
24
43
|
static async create(envId, envRepo, devServerRepo, routeRuleRepo) {
|
|
25
44
|
if (this.appMap[envId]) {
|
|
26
45
|
devServerLogger.info(`环境 ${envId} 已经启动`);
|
|
@@ -71,15 +90,30 @@ class PreProxyServer {
|
|
|
71
90
|
const envItem = this.envRepo.findOneById(this.envId);
|
|
72
91
|
return envItem;
|
|
73
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* 获取(或创建)请求级上下文,缓存当前环境信息与路由规则,
|
|
95
|
+
* 保证一次代理请求内各钩子共享同一份数据、只查询一次数据库。
|
|
96
|
+
*/
|
|
97
|
+
getRequestContext(req) {
|
|
98
|
+
const cached = requestContextMap.get(req);
|
|
99
|
+
if (cached) {
|
|
100
|
+
return cached;
|
|
101
|
+
}
|
|
102
|
+
const ctx = {
|
|
103
|
+
envItem: this.envRepo.findOneById(this.envId) ?? null,
|
|
104
|
+
routeRules: this.routeRuleRepo.getByEnvId(this.envId),
|
|
105
|
+
};
|
|
106
|
+
requestContextMap.set(req, ctx);
|
|
107
|
+
return ctx;
|
|
108
|
+
}
|
|
74
109
|
/**
|
|
75
110
|
* 获取绑定的服务器详情
|
|
76
111
|
*/
|
|
77
112
|
getDevServer() { }
|
|
78
113
|
/**
|
|
79
|
-
*
|
|
114
|
+
* 根据环境信息生成代理 cookie 后缀
|
|
80
115
|
*/
|
|
81
|
-
|
|
82
|
-
const envItem = this.getEnvItem();
|
|
116
|
+
getCookieSuffix(envItem) {
|
|
83
117
|
return `-${envItem?.port}-${PreProxyServer.configCookieSuffix}`;
|
|
84
118
|
}
|
|
85
119
|
/**
|
|
@@ -93,8 +127,7 @@ class PreProxyServer {
|
|
|
93
127
|
* @param requestPath 请求路径
|
|
94
128
|
* @returns 匹配到的目标环境地址,如果没有匹配则返回 null
|
|
95
129
|
*/
|
|
96
|
-
matchRouteRule(requestPath) {
|
|
97
|
-
const routeRules = this.routeRuleRepo.getByEnvId(this.envId);
|
|
130
|
+
matchRouteRule(requestPath, routeRules) {
|
|
98
131
|
// 过滤出已启用的规则
|
|
99
132
|
const enabledRules = routeRules.filter((rule) => rule.enabled !== false);
|
|
100
133
|
// 按 pathPrefix 长度降序排序,确保最长前缀匹配优先
|
|
@@ -119,8 +152,7 @@ class PreProxyServer {
|
|
|
119
152
|
* @param requestPath 请求路径
|
|
120
153
|
* @returns 命中的规则 pathPrefix,未命中返回 'default'
|
|
121
154
|
*/
|
|
122
|
-
getMatchedRule(requestPath) {
|
|
123
|
-
const routeRules = this.routeRuleRepo.getByEnvId(this.envId);
|
|
155
|
+
getMatchedRule(requestPath, routeRules) {
|
|
124
156
|
// 过滤出已启用的规则
|
|
125
157
|
const enabledRules = routeRules.filter((rule) => rule.enabled !== false);
|
|
126
158
|
// 按 pathPrefix 长度降序排序,确保最长前缀匹配优先
|
|
@@ -144,42 +176,43 @@ class PreProxyServer {
|
|
|
144
176
|
selfHandleResponse: true,
|
|
145
177
|
router: (req) => {
|
|
146
178
|
const requestPath = req.path || "/";
|
|
179
|
+
const ctx = this.getRequestContext(req);
|
|
147
180
|
// 1. 检查是否配置了路由规则
|
|
148
|
-
const customTarget = this.matchRouteRule(requestPath);
|
|
181
|
+
const customTarget = this.matchRouteRule(requestPath, ctx.routeRules);
|
|
149
182
|
if (customTarget) {
|
|
150
183
|
return customTarget;
|
|
151
184
|
}
|
|
152
185
|
// 2. 默认转发到 devServer
|
|
153
|
-
const envItem = this.getEnvItem();
|
|
154
186
|
const devServerConfig = this.devServerRepo.findOneById({
|
|
155
|
-
id: envItem?.devServerId ?? "",
|
|
187
|
+
id: ctx.envItem?.devServerId ?? "",
|
|
156
188
|
});
|
|
157
189
|
return `${devServerConfig?.devServerUrl}`;
|
|
158
190
|
},
|
|
159
191
|
on: {
|
|
160
192
|
proxyReq: (proxyReq, req) => {
|
|
161
193
|
const requestPath = req.path || "/";
|
|
194
|
+
const ctx = this.getRequestContext(req);
|
|
162
195
|
// 记录请求开始时间和日志 ID(用于 proxyRes 中的日志捕获)
|
|
163
196
|
req.__logStartTime = Date.now();
|
|
164
|
-
req.__logId =
|
|
197
|
+
req.__logId = randomUUID();
|
|
165
198
|
// 优先使用路由规则的目标地址,否则使用环境默认的 apiBaseUrl
|
|
166
|
-
const customTarget = this.matchRouteRule(requestPath);
|
|
167
|
-
const target = customTarget || `${
|
|
199
|
+
const customTarget = this.matchRouteRule(requestPath, ctx.routeRules);
|
|
200
|
+
const target = customTarget || `${ctx.envItem?.apiBaseUrl}`;
|
|
168
201
|
proxyReq.setHeader("x-api-server", `${target}`);
|
|
169
|
-
this._rewrieCookieOnProxyReq(proxyReq, req);
|
|
202
|
+
this._rewrieCookieOnProxyReq(proxyReq, req, ctx.envItem);
|
|
170
203
|
},
|
|
171
204
|
proxyRes: (proxyRes, req, res) => {
|
|
205
|
+
const ctx = this.getRequestContext(req);
|
|
172
206
|
// --- 请求日志捕获 ---
|
|
173
207
|
const startTime = req
|
|
174
208
|
.__logStartTime;
|
|
175
209
|
if (startTime) {
|
|
176
210
|
const duration = Date.now() - startTime;
|
|
177
211
|
const requestPath = req.path || "/";
|
|
178
|
-
const envItem = this.getEnvItem();
|
|
179
212
|
// 确定命中的路由规则
|
|
180
|
-
const matchedRule = this.getMatchedRule(requestPath);
|
|
213
|
+
const matchedRule = this.getMatchedRule(requestPath, ctx.routeRules);
|
|
181
214
|
requestLogEmitter.emit("log", {
|
|
182
|
-
id: req.__logId ||
|
|
215
|
+
id: req.__logId || randomUUID(),
|
|
183
216
|
timestamp: startTime,
|
|
184
217
|
method: req.method || "UNKNOWN",
|
|
185
218
|
url: requestPath,
|
|
@@ -188,22 +221,22 @@ class PreProxyServer {
|
|
|
188
221
|
matchedRule,
|
|
189
222
|
resourceType: classifyResourceType(matchedRule, proxyRes.headers["content-type"], requestPath),
|
|
190
223
|
envId: this.envId,
|
|
191
|
-
envName: envItem?.name || envItem?.apiBaseUrl || "",
|
|
224
|
+
envName: ctx.envItem?.name || ctx.envItem?.apiBaseUrl || "",
|
|
192
225
|
});
|
|
193
226
|
}
|
|
194
|
-
this._rewriteSetCookieOnProxyRes(proxyRes);
|
|
227
|
+
this._rewriteSetCookieOnProxyRes(proxyRes, ctx.envItem);
|
|
195
228
|
this._rewriteLoginRedirect(proxyRes, req, res);
|
|
196
229
|
},
|
|
197
230
|
proxyReqWs: (proxyReq, req) => {
|
|
198
231
|
const requestPath = req.path || "/";
|
|
199
|
-
const
|
|
200
|
-
const
|
|
232
|
+
const ctx = this.getRequestContext(req);
|
|
233
|
+
const customTarget = this.matchRouteRule(requestPath, ctx.routeRules);
|
|
234
|
+
const target = customTarget || `${ctx.envItem?.apiBaseUrl}`;
|
|
201
235
|
proxyReq.setHeader("x-api-server", `${target}`);
|
|
202
236
|
// WebSocket 连接日志(无真实 HTTP 状态码,约定 101 Switching Protocols)
|
|
203
|
-
const matchedRule = this.getMatchedRule(requestPath);
|
|
204
|
-
const envItem = this.getEnvItem();
|
|
237
|
+
const matchedRule = this.getMatchedRule(requestPath, ctx.routeRules);
|
|
205
238
|
requestLogEmitter.emit("log", {
|
|
206
|
-
id:
|
|
239
|
+
id: randomUUID(),
|
|
207
240
|
timestamp: Date.now(),
|
|
208
241
|
method: "WS",
|
|
209
242
|
url: requestPath,
|
|
@@ -212,7 +245,7 @@ class PreProxyServer {
|
|
|
212
245
|
matchedRule,
|
|
213
246
|
resourceType: "websocket",
|
|
214
247
|
envId: this.envId,
|
|
215
|
-
envName: envItem?.name || envItem?.apiBaseUrl || "",
|
|
248
|
+
envName: ctx.envItem?.name || ctx.envItem?.apiBaseUrl || "",
|
|
216
249
|
});
|
|
217
250
|
},
|
|
218
251
|
},
|
|
@@ -223,17 +256,17 @@ class PreProxyServer {
|
|
|
223
256
|
* 给 每一个 setcookie 追加保存一个 代理cookie
|
|
224
257
|
* @param proxyRes
|
|
225
258
|
*/
|
|
226
|
-
_rewriteSetCookieOnProxyRes(proxyRes) {
|
|
227
|
-
const envItem = this.getEnvItem();
|
|
259
|
+
_rewriteSetCookieOnProxyRes(proxyRes, envItem) {
|
|
228
260
|
const setCookie = proxyRes.headers["set-cookie"];
|
|
229
261
|
if (envItem && setCookie) {
|
|
230
262
|
const setCookies = setCookieParser.parse(setCookie);
|
|
263
|
+
const cookieSuffix = this.getCookieSuffix(envItem);
|
|
231
264
|
const proxyCookie = setCookies.map((item) => {
|
|
232
265
|
const cookie = {
|
|
233
266
|
...item,
|
|
234
|
-
name: `${item.name}${
|
|
267
|
+
name: `${item.name}${cookieSuffix}`,
|
|
235
268
|
};
|
|
236
|
-
return libCookie.
|
|
269
|
+
return libCookie.stringifySetCookie(cookie);
|
|
237
270
|
});
|
|
238
271
|
setCookie.push(...proxyCookie);
|
|
239
272
|
}
|
|
@@ -241,20 +274,20 @@ class PreProxyServer {
|
|
|
241
274
|
/**
|
|
242
275
|
* 代理 转发的时候,将对应端口的cookie 重写回去
|
|
243
276
|
*/
|
|
244
|
-
_rewrieCookieOnProxyReq(proxyReq, req) {
|
|
245
|
-
const envItem = this.getEnvItem();
|
|
277
|
+
_rewrieCookieOnProxyReq(proxyReq, req, envItem) {
|
|
246
278
|
if (envItem && req.headers.cookie) {
|
|
247
|
-
const cookie = libCookie.
|
|
279
|
+
const cookie = libCookie.parseCookie(req.headers.cookie || "");
|
|
280
|
+
const cookieSuffix = this.getCookieSuffix(envItem);
|
|
248
281
|
const newCookies = [];
|
|
249
282
|
Object.keys(cookie).forEach((item) => {
|
|
250
283
|
if (item.endsWith(PreProxyServer.configCookieSuffix)) {
|
|
251
284
|
return;
|
|
252
285
|
}
|
|
253
|
-
let cookieName = `${item}${
|
|
286
|
+
let cookieName = `${item}${cookieSuffix}`;
|
|
254
287
|
if (!cookie[cookieName]) {
|
|
255
288
|
cookieName = item;
|
|
256
289
|
}
|
|
257
|
-
newCookies.push(libCookie.
|
|
290
|
+
newCookies.push(libCookie.stringifyCookie({ [item]: cookie[cookieName] || "" }));
|
|
258
291
|
});
|
|
259
292
|
proxyReq.setHeader("cookie", newCookies.join(";"));
|
|
260
293
|
}
|
|
@@ -415,8 +448,4 @@ class PreProxyServer {
|
|
|
415
448
|
});
|
|
416
449
|
}
|
|
417
450
|
}
|
|
418
|
-
/**
|
|
419
|
-
* 保存启动的环境实例
|
|
420
|
-
*/
|
|
421
|
-
PreProxyServer.appMap = {};
|
|
422
451
|
export default PreProxyServer;
|
|
@@ -5,6 +5,9 @@ import { logger } from "../utils/logger.js";
|
|
|
5
5
|
* 负责在应用启动时检查并启动数据库中状态为运行的代理
|
|
6
6
|
*/
|
|
7
7
|
export class ProxyAutoStarter {
|
|
8
|
+
envRepo;
|
|
9
|
+
proxyService;
|
|
10
|
+
routeRuleRepo;
|
|
8
11
|
constructor(envRepo, proxyService, routeRuleRepo) {
|
|
9
12
|
this.envRepo = envRepo;
|
|
10
13
|
this.proxyService = proxyService;
|
|
@@ -5,13 +5,21 @@ const MAX_LOG_ENTRIES = 500;
|
|
|
5
5
|
/** 模块级单例 EventEmitter — PreProxyServer 直接导入使用 */
|
|
6
6
|
export const requestLogEmitter = new EventEmitter();
|
|
7
7
|
class RequestLogService {
|
|
8
|
+
logs = [];
|
|
9
|
+
logSubscribers = new Set();
|
|
10
|
+
handleLog = (entry) => {
|
|
11
|
+
this.addLog(entry);
|
|
12
|
+
};
|
|
8
13
|
constructor() {
|
|
9
|
-
this.logs = [];
|
|
10
|
-
this.logSubscribers = new Set();
|
|
11
14
|
// 监听 PreProxyServer 发出的日志事件
|
|
12
|
-
requestLogEmitter.on("log",
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
requestLogEmitter.on("log", this.handleLog);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 释放资源:从全局事件总线移除本实例的日志监听器。
|
|
19
|
+
* 重复实例化(如测试中多次 createTestApp)时务必调用,避免监听器累积。
|
|
20
|
+
*/
|
|
21
|
+
dispose() {
|
|
22
|
+
requestLogEmitter.off("log", this.handleLog);
|
|
15
23
|
}
|
|
16
24
|
/**
|
|
17
25
|
* 添加日志条目(环形缓冲区,最多保留 MAX_LOG_ENTRIES 条)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { AppError } from "../utils/errors.js";
|
|
3
3
|
/**
|
|
4
4
|
* 路由规则服务类
|
|
@@ -6,6 +6,8 @@ import { AppError } from "../utils/errors.js";
|
|
|
6
6
|
* 依赖路由规则仓库(RouteRuleRepo)和环境仓库(EnvRepo)实现数据持久化和关联操作
|
|
7
7
|
*/
|
|
8
8
|
class RouteRuleService {
|
|
9
|
+
routeRuleRepo;
|
|
10
|
+
envRepo;
|
|
9
11
|
/**
|
|
10
12
|
* 构造函数 - 通过依赖注入初始化仓库实例
|
|
11
13
|
* @param routeRuleRepo - 路由规则仓库实例,用于路由规则数据的持久化操作
|
|
@@ -63,7 +65,7 @@ class RouteRuleService {
|
|
|
63
65
|
const now = new Date().toISOString();
|
|
64
66
|
const newRouteRule = {
|
|
65
67
|
...routeRuleItem,
|
|
66
|
-
id:
|
|
68
|
+
id: randomUUID(),
|
|
67
69
|
createdAt: now,
|
|
68
70
|
updatedAt: now,
|
|
69
71
|
};
|
|
@@ -29,6 +29,7 @@ export function getLogEnabled() {
|
|
|
29
29
|
* 负责系统设置的读取与更新,通过 Repository 实现持久化
|
|
30
30
|
*/
|
|
31
31
|
class SystemSettingService {
|
|
32
|
+
repo;
|
|
32
33
|
constructor(repo = new SystemSettingRepo()) {
|
|
33
34
|
this.repo = repo;
|
|
34
35
|
}
|
|
@@ -47,20 +48,15 @@ class SystemSettingService {
|
|
|
47
48
|
return logEnabled;
|
|
48
49
|
}
|
|
49
50
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
51
|
+
* 一次性更新全部系统设置开关。
|
|
52
|
+
* 先落库、成功后再更新内存缓存,避免写入失败导致内存态与库不一致。
|
|
53
|
+
* @param nextInjectEnabled - 是否开启注入资源
|
|
54
|
+
* @param nextLogEnabled - 是否开启请求日志记录
|
|
52
55
|
*/
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* 更新请求日志记录开关状态
|
|
59
|
-
* @param value - 是否开启请求日志记录
|
|
60
|
-
*/
|
|
61
|
-
setLogEnabled(value) {
|
|
62
|
-
logEnabled = value;
|
|
63
|
-
this.persist();
|
|
56
|
+
setSettings(nextInjectEnabled, nextLogEnabled) {
|
|
57
|
+
this.persist(nextInjectEnabled, nextLogEnabled);
|
|
58
|
+
injectEnabled = nextInjectEnabled;
|
|
59
|
+
logEnabled = nextLogEnabled;
|
|
64
60
|
}
|
|
65
61
|
/**
|
|
66
62
|
* 初始化:从数据库读取持久化值同步到内存缓存。
|
|
@@ -77,14 +73,18 @@ class SystemSettingService {
|
|
|
77
73
|
else {
|
|
78
74
|
injectEnabled = false;
|
|
79
75
|
logEnabled = false;
|
|
80
|
-
this.persist();
|
|
76
|
+
this.persist(injectEnabled, logEnabled);
|
|
81
77
|
}
|
|
82
78
|
}
|
|
83
79
|
/**
|
|
84
|
-
*
|
|
80
|
+
* 将给定状态全量写入数据库
|
|
85
81
|
*/
|
|
86
|
-
persist() {
|
|
87
|
-
this.repo.upsert({
|
|
82
|
+
persist(nextInjectEnabled, nextLogEnabled) {
|
|
83
|
+
this.repo.upsert({
|
|
84
|
+
id: "global",
|
|
85
|
+
injectEnabled: nextInjectEnabled,
|
|
86
|
+
logEnabled: nextLogEnabled,
|
|
87
|
+
});
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
export { SystemSettingService };
|
|
@@ -63,9 +63,11 @@ export const loadConfig = (overrideConfig) => {
|
|
|
63
63
|
if (error instanceof z.ZodError) {
|
|
64
64
|
throw new Error(`配置字段错误:${error.issues
|
|
65
65
|
.map((issue) => `${issue.path.join(".")}: ${issue.message}`)
|
|
66
|
-
.join("; ")}
|
|
66
|
+
.join("; ")}`, { cause: error });
|
|
67
67
|
}
|
|
68
|
-
throw new Error(`配置加载异常:${error.message}
|
|
68
|
+
throw new Error(`配置加载异常:${error.message}`, {
|
|
69
|
+
cause: error,
|
|
70
|
+
});
|
|
69
71
|
}
|
|
70
72
|
};
|
|
71
73
|
/**
|
package/dist/utils/errors.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "env-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A dev env plugin that integrates an Express server with request proxying capabilities.",
|
|
6
6
|
"keywords": [
|
|
@@ -23,39 +23,33 @@
|
|
|
23
23
|
"author": "lumos934",
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"commander": "^
|
|
27
|
-
"cookie": "^
|
|
28
|
-
"dotenv": "^
|
|
29
|
-
"express": "^
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"unplugin": "^2.2.0",
|
|
40
|
-
"uuid": "^10.0.0",
|
|
41
|
-
"ws": "^8.18.3",
|
|
42
|
-
"zod": "^4.0.16"
|
|
26
|
+
"commander": "^15.1.0",
|
|
27
|
+
"cookie": "^2.0.2",
|
|
28
|
+
"dotenv": "^18.0.10",
|
|
29
|
+
"express": "^5.3.0",
|
|
30
|
+
"http-proxy-middleware": "^4.2.2",
|
|
31
|
+
"minimatch": "^11.0.10",
|
|
32
|
+
"pino": "^10.4.0",
|
|
33
|
+
"pino-pretty": "^13.2.0",
|
|
34
|
+
"set-cookie-parser": "^4.0.0",
|
|
35
|
+
"tslib": "^2.9.1",
|
|
36
|
+
"unplugin": "^3.7.0",
|
|
37
|
+
"ws": "^8.23.1",
|
|
38
|
+
"zod": "^4.6.7"
|
|
43
39
|
},
|
|
44
40
|
"devDependencies": {
|
|
45
|
-
"@types/express": "^
|
|
46
|
-
"@types/
|
|
47
|
-
"@types/set-cookie-parser": "^2.4.10",
|
|
41
|
+
"@types/express": "^5.0.6",
|
|
42
|
+
"@types/set-cookie-parser": "^3.1.0",
|
|
48
43
|
"@types/supertest": "^7.2.1",
|
|
49
|
-
"@types/
|
|
50
|
-
"@
|
|
51
|
-
"@vitest/
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"vitest": "^4.1.10"
|
|
44
|
+
"@types/ws": "^8.18.2",
|
|
45
|
+
"@vitest/coverage-v8": "^5.1.2",
|
|
46
|
+
"@vitest/ui": "5.1.2",
|
|
47
|
+
"globals": "^17.15.0",
|
|
48
|
+
"nodemon": "^3.1.14",
|
|
49
|
+
"supertest": "^7.3.2",
|
|
50
|
+
"tsx": "^4.23.15",
|
|
51
|
+
"typescript": "^7.0.2",
|
|
52
|
+
"vitest": "^5.1.2"
|
|
59
53
|
},
|
|
60
54
|
"bugs": {
|
|
61
55
|
"url": "https://github.com/lumos934/env-plugin/issues"
|
|
@@ -66,7 +60,7 @@
|
|
|
66
60
|
"url": "https://github.com/lumos934/env-plugin.git"
|
|
67
61
|
},
|
|
68
62
|
"engines": {
|
|
69
|
-
"node": ">=
|
|
63
|
+
"node": ">=26.0.0"
|
|
70
64
|
},
|
|
71
65
|
"files": [
|
|
72
66
|
"bin/index.js",
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|