kb-server 0.0.1-beta.41 → 0.0.1-beta.43
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.
|
@@ -33,7 +33,7 @@ const sse_middleware_1 = require("./sse-middleware");
|
|
|
33
33
|
function createServer(params, options) {
|
|
34
34
|
const { apis, sse, authFn, log, middlewares = [] } = params || {};
|
|
35
35
|
const { limit = "10mb" } = options || {};
|
|
36
|
-
const { handlers,
|
|
36
|
+
const { handlers, ...resetSSE } = sse || {};
|
|
37
37
|
const app = (0, express_1.default)();
|
|
38
38
|
// POST 参数获取
|
|
39
39
|
app.use((0, express_1.urlencoded)({ extended: true, limit }));
|
|
@@ -41,7 +41,7 @@ function createServer(params, options) {
|
|
|
41
41
|
// 使用自定义中间件
|
|
42
42
|
middlewares.forEach((middleware) => app.use(middleware));
|
|
43
43
|
// 注入SSE
|
|
44
|
-
handlers && app.use((0, sse_middleware_1.packSSE)(handlers, {
|
|
44
|
+
handlers && app.use((0, sse_middleware_1.packSSE)(handlers, { log, ...resetSSE }));
|
|
45
45
|
// 注入API
|
|
46
46
|
app.use((0, api_middleware_1.packAPI)(apis, { authFn, log }));
|
|
47
47
|
return app;
|
|
@@ -5,6 +5,7 @@ interface IPackSSEOptions {
|
|
|
5
5
|
authFn?: AuthFunction;
|
|
6
6
|
route?: string;
|
|
7
7
|
log?: boolean;
|
|
8
|
+
timeout?: number;
|
|
8
9
|
}
|
|
9
10
|
export declare const packSSE: (sseHandlers: SseHandlers, options?: IPackSSEOptions) => express.RequestHandler;
|
|
10
11
|
export declare const createSSEMsg: (event: string, message: string) => string;
|
|
@@ -6,7 +6,7 @@ const logger_1 = require("../helper/logger");
|
|
|
6
6
|
const create_errors_1 = require("./create-errors");
|
|
7
7
|
const short_id_1 = require("../helper/short-id");
|
|
8
8
|
const packSSE = (sseHandlers, options) => {
|
|
9
|
-
const { authFn, log = true, route = "/sse" } = options || {};
|
|
9
|
+
const { authFn, log = true, route = "/sse", timeout = 1000 * 30, } = options || {};
|
|
10
10
|
return async (req, res, next) => {
|
|
11
11
|
// 生成SSE API映射
|
|
12
12
|
const sseMap = new Map(Object.entries(sseHandlers).map(([action, execution]) => [
|
|
@@ -63,9 +63,12 @@ const packSSE = (sseHandlers, options) => {
|
|
|
63
63
|
throw new create_errors_1.CommonErrors.ResourceNotFound.APINotFound();
|
|
64
64
|
}
|
|
65
65
|
const abortController = new AbortController();
|
|
66
|
+
let timeoutId;
|
|
66
67
|
// 连接断开
|
|
67
68
|
let isConnected = true;
|
|
68
69
|
res.on("close", () => {
|
|
70
|
+
if (timeoutId)
|
|
71
|
+
clearTimeout(timeoutId);
|
|
69
72
|
isConnected = false;
|
|
70
73
|
abortController.abort();
|
|
71
74
|
// 完成响应
|
|
@@ -79,8 +82,18 @@ const packSSE = (sseHandlers, options) => {
|
|
|
79
82
|
"Cache-Control": "no-cache",
|
|
80
83
|
Connection: "keep-alive",
|
|
81
84
|
});
|
|
85
|
+
// 初始化超时定时器
|
|
86
|
+
const resetTimeout = () => {
|
|
87
|
+
if (timeoutId)
|
|
88
|
+
clearTimeout(timeoutId);
|
|
89
|
+
timeoutId = setTimeout(() => {
|
|
90
|
+
logger_1.logger.info(`超时自动关闭:${requestId}`);
|
|
91
|
+
close();
|
|
92
|
+
}, timeout);
|
|
93
|
+
};
|
|
82
94
|
// 推送消息
|
|
83
95
|
const push = (data) => {
|
|
96
|
+
resetTimeout();
|
|
84
97
|
if (!isConnected) {
|
|
85
98
|
logger_1.logger.warning(`连接已关闭: ${requestId}`);
|
|
86
99
|
return;
|
|
@@ -108,6 +121,8 @@ const packSSE = (sseHandlers, options) => {
|
|
|
108
121
|
res.write(response, () => res.end());
|
|
109
122
|
};
|
|
110
123
|
await execution(params, { push, close, abortController }, ctx);
|
|
124
|
+
// 初始化定时器(只有函数本身异步逻辑执行完毕了才会启动计时)
|
|
125
|
+
resetTimeout();
|
|
111
126
|
}
|
|
112
127
|
catch (rawError) {
|
|
113
128
|
logger_1.logger.error(rawError);
|