kb-server 0.0.1-beta.37 → 0.0.1-beta.38
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.
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.callService = void 0;
|
|
7
|
-
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
8
4
|
const create_errors_1 = require("./create-errors");
|
|
9
5
|
const callService = async (endpoint, body) => {
|
|
10
|
-
const response = await (
|
|
6
|
+
const response = await fetch(endpoint, {
|
|
11
7
|
headers: {
|
|
12
8
|
"Content-Type": "application/json",
|
|
13
9
|
},
|
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
import { Class } from "utility-types";
|
|
2
2
|
import { ServerContext } from "./create-api";
|
|
3
|
+
import { Session } from "better-sse";
|
|
3
4
|
export type API<P, R> = (param: P) => Promise<R>;
|
|
4
5
|
export type SseExecution<P, R, A> = (
|
|
5
6
|
/** 请求入参 */
|
|
6
7
|
params: P,
|
|
8
|
+
/** SSE 连接对象 */
|
|
9
|
+
session: Session,
|
|
7
10
|
/** 请求上下文 */
|
|
8
|
-
ctx: ServerContext<A
|
|
9
|
-
/**
|
|
10
|
-
* 操作函数
|
|
11
|
-
*/
|
|
12
|
-
operations: {
|
|
13
|
-
/**
|
|
14
|
-
* 向客户端推送消息
|
|
15
|
-
*/
|
|
16
|
-
send: (event: string, message: string) => void;
|
|
17
|
-
}) => Promise<R>;
|
|
11
|
+
ctx: ServerContext<A>) => Promise<R>;
|
|
18
12
|
export type AnySseExecution = SseExecution<any, any, any>;
|
|
19
13
|
export type SseHandlers = Record<string, AnySseExecution>;
|
|
20
14
|
export declare function createSseAPI<P, R, A>(ParamsClass: Class<P>, execution: SseExecution<P, R, A>): API<P, R>;
|
|
@@ -6,7 +6,7 @@ const class_validator_1 = require("class-validator");
|
|
|
6
6
|
const create_errors_1 = require("./create-errors");
|
|
7
7
|
const create_api_1 = require("./create-api");
|
|
8
8
|
function createSseAPI(ParamsClass, execution) {
|
|
9
|
-
const runtime = async (params,
|
|
9
|
+
const runtime = async (params, session, ctx) => {
|
|
10
10
|
if (!params) {
|
|
11
11
|
throw new create_errors_1.CommonErrors.InvalidParameter.EmptyParameter();
|
|
12
12
|
}
|
|
@@ -20,7 +20,7 @@ function createSseAPI(ParamsClass, execution) {
|
|
|
20
20
|
throw new create_errors_1.CommonErrors.InvalidParameter.ValidationError(errorMessages.join("\n"));
|
|
21
21
|
}
|
|
22
22
|
// 执行函数
|
|
23
|
-
return await execution(params,
|
|
23
|
+
return await execution(params, session, ctx);
|
|
24
24
|
};
|
|
25
25
|
return runtime;
|
|
26
26
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createSSEMessage = exports.packSSE = void 0;
|
|
4
|
+
const better_sse_1 = require("better-sse");
|
|
4
5
|
const uuid_1 = require("uuid");
|
|
5
6
|
const logger_1 = require("../helper/logger");
|
|
6
7
|
const create_errors_1 = require("./create-errors");
|
|
7
8
|
const short_id_1 = require("../helper/short-id");
|
|
8
|
-
const sleep_1 = require("../helper/sleep");
|
|
9
9
|
const packSSE = (sseHandlers, options) => {
|
|
10
10
|
const { authFn, log = true, route = "/sse" } = options || {};
|
|
11
11
|
return async (req, res, next) => {
|
|
@@ -63,44 +63,11 @@ const packSSE = (sseHandlers, options) => {
|
|
|
63
63
|
if (typeof execution !== "function") {
|
|
64
64
|
throw new create_errors_1.CommonErrors.ResourceNotFound.APINotFound();
|
|
65
65
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
// 监听客户端断开事件
|
|
69
|
-
res.on("close", () => {
|
|
70
|
-
isClientConnected = false; // 标记连接已断开
|
|
71
|
-
logger_1.logger.log("info", `客户端已断开 - RequestId: ${requestId}`);
|
|
72
|
-
});
|
|
73
|
-
// 写请求头
|
|
74
|
-
res.writeHead(200, {
|
|
75
|
-
"Content-Type": "text/event-stream",
|
|
76
|
-
"Cache-Control": "no-cache",
|
|
77
|
-
Connection: "keep-alive",
|
|
78
|
-
});
|
|
79
|
-
const send = (event, message) => {
|
|
80
|
-
if (!isClientConnected) {
|
|
81
|
-
logger_1.logger.log("warning", "客户端已断开,停止发送");
|
|
82
|
-
return; // 终止发送逻辑
|
|
83
|
-
}
|
|
84
|
-
const response = (0, exports.createSSEMessage)(event, message);
|
|
85
|
-
try {
|
|
86
|
-
res.write(response);
|
|
87
|
-
if (log) {
|
|
88
|
-
logger_1.logger.log("info", `发送消息:\n${response}`);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
catch (err) {
|
|
92
|
-
if (err.code === "EPIPE" || err.code === "ECONNRESET") {
|
|
93
|
-
logger_1.logger.log("error", "客户端连接已关闭");
|
|
94
|
-
isClientConnected = false;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
await execution(params, ctx, { send });
|
|
66
|
+
const session = await (0, better_sse_1.createSession)(req, res);
|
|
67
|
+
await execution(params, session, ctx);
|
|
99
68
|
// 完成响应
|
|
100
69
|
took = Date.now() - start;
|
|
101
70
|
logger_1.logger.log("info", `耗时:${took} ms - RequestId: ${requestId}`);
|
|
102
|
-
// 延迟1ms关闭连接
|
|
103
|
-
await (0, sleep_1.sleep)(1);
|
|
104
71
|
return res.end();
|
|
105
72
|
}
|
|
106
73
|
catch (rawError) {
|
|
@@ -134,8 +101,6 @@ const packSSE = (sseHandlers, options) => {
|
|
|
134
101
|
logger_1.logger.log("info", `发送消息:\n${response}`);
|
|
135
102
|
logger_1.logger.log("info", `耗时:${took} ms - RequestId: ${requestId}`);
|
|
136
103
|
res.write(response);
|
|
137
|
-
// 延迟100ms关闭连接
|
|
138
|
-
await (0, sleep_1.sleep)(100);
|
|
139
104
|
return res.end();
|
|
140
105
|
}
|
|
141
106
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kb-server",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.38",
|
|
4
4
|
"description": "A fast server for Node.JS,made by express.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -16,21 +16,23 @@
|
|
|
16
16
|
"dist/",
|
|
17
17
|
"README.md"
|
|
18
18
|
],
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=18.0.0"
|
|
21
|
+
},
|
|
19
22
|
"author": "broxiang",
|
|
20
23
|
"license": "ISC",
|
|
21
24
|
"dependencies": {
|
|
25
|
+
"better-sse": "^0.13.0",
|
|
22
26
|
"class-transformer": "^0.5.1",
|
|
23
27
|
"class-validator": "^0.14.1",
|
|
24
28
|
"express": "^4.21.1",
|
|
25
29
|
"is-plain-object": "^5.0.0",
|
|
26
30
|
"moment": "^2.30.1",
|
|
27
31
|
"nanoid": "^3.3.9",
|
|
28
|
-
"node-fetch": "^2.7.0",
|
|
29
32
|
"utility-types": "^3.11.0",
|
|
30
33
|
"uuid": "^11.0.3"
|
|
31
34
|
},
|
|
32
35
|
"devDependencies": {
|
|
33
|
-
"@types/express": "^4.17.21"
|
|
34
|
-
"@types/node-fetch": "^2.6.12"
|
|
36
|
+
"@types/express": "^4.17.21"
|
|
35
37
|
}
|
|
36
38
|
}
|